-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_flow.json
More file actions
237 lines (207 loc) · 8.88 KB
/
test_flow.json
File metadata and controls
237 lines (207 loc) · 8.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from flask import Flask, request, jsonify, render_template_string
import subprocess
import json
import os
import re
from dotenv import load_dotenv
from groq import Groq
load_dotenv()
app = Flask(__name__)
with open("tools.json") as f:
tools_schema = json.load(f)
process = subprocess.Popen(
["python", "server.py"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
text=True
)
conversation_history = []
def call_tool(tool_name, args):
request_data = {"tool": tool_name, "arguments": args}
process.stdin.write(json.dumps(request_data) + "\n")
process.stdin.flush()
return json.loads(process.stdout.readline())
def run_agent(user_message):
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
conversation_history.append({"role": "user", "content": user_message})
response = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[
{
"role": "system",
"content": f"""You are an assistant that manages GitHub repos.
You have access to these tools: {json.dumps(tools_schema)}
IMPORTANT RULES:
- If ONE action needed: {{"tool": "tool_name", "arguments": {{}}}}
- If MULTIPLE actions needed: [{{"tool": "t1", "arguments": {{}}}}, {{"tool": "t2", "arguments": {{}}}}]
- NEVER put each JSON on a separate line — always use a single array.
- NO extra text — ONLY JSON. Only plain text if no tool needed."""
},
*conversation_history
]
)
reply = response.choices[0].message.content.strip()
conversation_history.append({"role": "assistant", "content": reply})
# parse single or multi tool
try:
parsed = json.loads(reply)
if isinstance(parsed, list):
results = []
for action in parsed:
result = call_tool(action["tool"], action["arguments"])
results.append({"tool": action["tool"], "result": result})
conversation_history.append({"role": "user", "content": f"Tool results: {json.dumps(results)[:600]}"})
return format_results(results)
if isinstance(parsed, dict) and "tool" in parsed:
result = call_tool(parsed["tool"], parsed["arguments"])
conversation_history.append({"role": "user", "content": f"Tool result: {json.dumps(result)[:500]}"})
return format_result(parsed["tool"], result)
except:
pass
# fallback: extract JSON
match = re.search(r'\[.*\]|\{.*"tool".*\}', reply, re.DOTALL)
if match:
try:
parsed = json.loads(match.group())
if isinstance(parsed, list):
results = []
for action in parsed:
result = call_tool(action["tool"], action["arguments"])
results.append({"tool": action["tool"], "result": result})
return format_results(results)
result = call_tool(parsed["tool"], parsed["arguments"])
return format_result(parsed["tool"], result)
except:
pass
# fallback: newline separated
lines = [l.strip() for l in reply.strip().split('\n') if l.strip().startswith('{')]
if len(lines) > 1:
results = []
for line in lines:
try:
action = json.loads(line)
if "tool" in action:
result = call_tool(action["tool"], action["arguments"])
results.append({"tool": action["tool"], "result": result})
except:
continue
if results:
return format_results(results)
return reply
def format_result(tool, result):
if isinstance(result, dict):
if result.get("exists") is True:
return f"✅ Repo **{result['name']}** exists — {result.get('url', '')}"
if result.get("exists") is False:
return f"❌ Repo **{result['name']}** not found"
if "decoded_content" in result:
return f"📄 **{result.get('name', 'file')}**:\n```\n{result['decoded_content']}\n```"
if "html_url" in result:
return f"✅ Done! [{result['name']}]({result['html_url']})"
if "error" in result:
return f"❌ Error: {result['error']}"
if isinstance(result, list):
lines = "\n".join([f"- {f['name']} ({f['size']} bytes)" for f in result])
return f"📁 Files:\n{lines}"
return str(result)
def format_results(results):
lines = []
for r in results:
lines.append(format_result(r["tool"], r["result"]))
return "\n".join(lines)
HTML = """
<!DOCTYPE html>
<html>
<head>
<title>GitHub MCP Agent</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: system-ui, sans-serif; background: #0d1117; color: #e6edf3; height: 100vh; display: flex; flex-direction: column; }
header { padding: 16px 24px; background: #161b22; border-bottom: 1px solid #30363d; display: flex; align-items: center; gap: 10px; }
header h1 { font-size: 16px; font-weight: 600; }
.badge { background: #238636; color: #fff; font-size: 11px; padding: 2px 8px; border-radius: 10px; }
#chat { flex: 1; overflow-y: auto; padding: 24px; display: flex; flex-direction: column; gap: 16px; }
.msg { max-width: 75%; padding: 12px 16px; border-radius: 12px; font-size: 14px; line-height: 1.6; white-space: pre-wrap; word-break: break-word; }
.user { align-self: flex-end; background: #1f6feb; color: #fff; border-radius: 12px 12px 2px 12px; }
.agent { align-self: flex-start; background: #161b22; border: 1px solid #30363d; border-radius: 12px 12px 12px 2px; }
.agent a { color: #58a6ff; }
footer { padding: 16px 24px; background: #161b22; border-top: 1px solid #30363d; display: flex; gap: 10px; }
#input { flex: 1; background: #0d1117; border: 1px solid #30363d; border-radius: 8px; padding: 10px 14px; color: #e6edf3; font-size: 14px; outline: none; }
#input:focus { border-color: #58a6ff; }
#send { background: #238636; color: #fff; border: none; border-radius: 8px; padding: 10px 20px; font-size: 14px; cursor: pointer; }
#send:hover { background: #2ea043; }
.typing { align-self: flex-start; color: #8b949e; font-size: 13px; }
.quick { display: flex; gap: 8px; flex-wrap: wrap; padding: 0 24px 12px; }
.quick button { background: #161b22; border: 1px solid #30363d; color: #8b949e; border-radius: 16px; padding: 4px 12px; font-size: 12px; cursor: pointer; }
.quick button:hover { border-color: #58a6ff; color: #58a6ff; }
</style>
</head>
<body>
<header>
<span style="font-size:20px">⚡</span>
<h1>GitHub MCP Agent</h1>
<span class="badge">live</span>
</header>
<div id="chat">
<div class="msg agent">Hey! I'm your GitHub agent. Ask me anything — create repos, push files, list your projects...</div>
</div>
<div class="quick">
<button onclick="send('list all my repos')">list repos</button>
<button onclick="send('create a repo called test-web')">create repo</button>
<button onclick="send('check if test-repo exists')">check repo</button>
<button onclick="send('list files in test-repo')">list files</button>
</div>
<footer>
<input id="input" placeholder="Ask me anything about your GitHub repos..." onkeydown="if(event.key==='Enter') send()"/>
<button id="send" onclick="send()">Send</button>
</footer>
<script>
const chat = document.getElementById('chat');
const input = document.getElementById('input');
function addMsg(text, role) {
const div = document.createElement('div');
div.className = 'msg ' + role;
div.innerHTML = text.replace(/\*\*(.*?)\*\*/g, '<b>$1</b>')
.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2" target="_blank">$1</a>')
.replace(/```([\s\S]*?)```/g, '<code style="background:#0d1117;padding:4px 8px;border-radius:4px;display:block;margin-top:6px">$1</code>');
chat.appendChild(div);
chat.scrollTop = chat.scrollHeight;
}
function addTyping() {
const div = document.createElement('div');
div.className = 'typing';
div.id = 'typing';
div.textContent = 'Agent is thinking...';
chat.appendChild(div);
chat.scrollTop = chat.scrollHeight;
}
async function send(text) {
const msg = text || input.value.trim();
if (!msg) return;
input.value = '';
addMsg(msg, 'user');
addTyping();
const res = await fetch('/chat', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({message: msg})
});
const data = await res.json();
document.getElementById('typing')?.remove();
addMsg(data.reply, 'agent');
}
</script>
</body>
</html>
"""
@app.route('/')
def index():
return render_template_string(HTML)
@app.route('/chat', methods=['POST'])
def chat():
data = request.get_json()
user_message = data.get('message', '')
reply = run_agent(user_message)
return jsonify({"reply": reply})
if __name__ == '__main__':
app.run(debug=True, port=5000)