-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbridge.py
More file actions
323 lines (261 loc) · 12.1 KB
/
bridge.py
File metadata and controls
323 lines (261 loc) · 12.1 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env python3
"""STDIO bridge : expose les 4 agents ADK comme tools Gemini/Claude
Usage (CLI) :
echo '{"tool":"watch_collect","params":{}}' | python -u bridge.py
Le script lit **une ligne JSON** sur stdin, exécute l'agent concerné et
écrit la réponse JSON sur stdout (flush immédiat).
"""
import json
import sys
import signal
import logging
import os
import subprocess
from pathlib import Path
from functools import lru_cache
# ---------------------------------------------------------------------------
# 1. Configuration des chemins vers les agents ADK ----------------------
# ---------------------------------------------------------------------------
def get_workspace_root() -> Path:
"""Return the ADK workspace root (can be overridden with ADK_WORKSPACE env)."""
env_override = os.environ.get("ADK_WORKSPACE")
return Path(env_override).expanduser() if env_override else Path.home() / "adk-workspace"
@lru_cache(maxsize=1)
def get_agents_config() -> dict:
"""Build the agents configuration using the current workspace root."""
workspace = get_workspace_root()
return {
"label_github_issue": {
"path": workspace / "github_labeler" / "main.py",
"python": workspace / "adk-env" / "bin" / "python",
"description": "GitHub Issue Labeler Agent"
},
"watch_collect": {
"path": workspace / "veille_agent" / "main.py",
"python": workspace / "veille_agent" / ".venv" / "bin" / "python",
"description": "Watch/Veille Agent for collecting tech updates"
},
"analyse_watch_report": {
"path": workspace / "gemini_analysis" / "main.py",
"python": workspace / "adk-env" / "bin" / "python",
"description": "Gemini Analysis Agent for report analysis"
},
"curate_digest": {
"path": workspace / "curateur_agent" / "main.py",
"python": workspace / "adk-env" / "bin" / "python",
"description": "Curator Agent for content curation"
}
}
ADK_WORKSPACE = get_workspace_root()
AGENTS_CONFIG = get_agents_config()
# ---------------------------------------------------------------------------
# 2. Setup logging --------------------------------------------------------
# ---------------------------------------------------------------------------
logging.basicConfig(
filename=os.path.expanduser("~/.gemini/bridge.log"),
level=logging.INFO,
format="[%(asctime)s] %(levelname)s: %(message)s",
)
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# 3. Agent helpers --------------------------------------------------------
# ---------------------------------------------------------------------------
def validate_agent(agent_name: str) -> dict:
"""Validate that the agent script and Python interpreter exist."""
agent_config = AGENTS_CONFIG[agent_name]
agent_path = agent_config["path"].expanduser()
python_path = agent_config["python"].expanduser()
issues = []
if not agent_path.exists():
issues.append(f"Agent script not found: {agent_path}")
if not python_path.exists():
issues.append(f"Python interpreter not found: {python_path}")
status = "ok" if not issues else "error"
return {
"status": status,
"script": str(agent_path),
"python": str(python_path),
"issues": issues,
}
def healthcheck() -> dict:
"""Return the validation status for all configured agents."""
return {name: validate_agent(name) for name in AGENTS_CONFIG}
# ---------------------------------------------------------------------------
# 4. Agent execution functions --------------------------------------------
# ---------------------------------------------------------------------------
def run_agent_script(agent_name: str, params: dict) -> dict:
"""Execute an ADK agent Python script with parameters."""
try:
agent_config = AGENTS_CONFIG[agent_name]
agent_path = agent_config["path"].expanduser()
python_path = agent_config["python"].expanduser()
validation = validate_agent(agent_name)
if validation["issues"]:
return {"status": "error", "error": "; ".join(validation["issues"])}
# Prepare the environment
env = os.environ.copy()
env["PYTHONPATH"] = str(ADK_WORKSPACE)
env["ADK_WORKSPACE"] = str(ADK_WORKSPACE)
# Convert params to command line arguments or JSON input
cmd = [str(python_path), str(agent_path)]
# If params contain specific keys, pass them as arguments
if "issue_number" in params:
cmd.extend(["--issue", str(params["issue_number"])])
if "repo_name" in params:
cmd.extend(["--repo", params["repo_name"]])
if "dry_run" in params and params["dry_run"]:
cmd.append("--dry-run")
# Execute the agent
result = subprocess.run(
cmd,
input=json.dumps(params),
text=True,
capture_output=True,
env=env,
timeout=300 # 5 minutes timeout
)
if result.returncode == 0:
try:
return json.loads(result.stdout) if result.stdout.strip() else {"status": "success", "output": result.stdout}
except json.JSONDecodeError:
return {"status": "success", "output": result.stdout}
else:
return {
"status": "error",
"error": result.stderr or f"Process failed with code {result.returncode}",
"stdout": result.stdout
}
except subprocess.TimeoutExpired:
return {"status": "error", "error": "Agent execution timed out"}
except FileNotFoundError:
return {"status": "error", "error": f"Agent script not found: {agent_path}"}
except Exception as e:
logger.exception(f"Error running agent {agent_path}")
return {"status": "error", "error": str(e)}
# ---------------------------------------------------------------------------
# 4. Dispatch functions ---------------------------------------------------
# ---------------------------------------------------------------------------
def dispatch_label_github_issue(params: dict) -> dict:
"""Handle GitHub issue labeling."""
required_params = ["repo_name", "issue_number"]
missing = [p for p in required_params if p not in params]
if missing:
return {"status": "error", "error": f"Missing required parameters: {missing}"}
# The GitHub labeler automatically determines labels based on content
# Add dry_run by default to avoid making actual changes unless explicitly requested
if "dry_run" not in params:
params["dry_run"] = True
return run_agent_script("label_github_issue", params)
def dispatch_watch_collect(params: dict) -> dict:
"""Handle watch/veille collection."""
# Default parameters for watch collection
default_params = {
"sources": ["github", "pypi", "npm"],
"output_format": "markdown"
}
merged_params = {**default_params, **params}
return run_agent_script("watch_collect", merged_params)
def dispatch_analyse_watch_report(params: dict) -> dict:
"""Handle watch report analysis."""
if "report" not in params and "report_path" not in params:
return {"status": "error", "error": "Missing 'report' or 'report_path' parameter"}
return run_agent_script("analyse_watch_report", params)
def dispatch_curate_digest(params: dict) -> dict:
"""Handle content curation."""
default_params = {
"format": "newsletter",
"output": "markdown"
}
merged_params = {**default_params, **params}
return run_agent_script("curate_digest", merged_params)
def dispatch_healthcheck(_params: dict) -> dict:
"""Report readiness for every configured agent."""
return {"status": "success", "workspace": str(ADK_WORKSPACE), "agents": healthcheck()}
# ---------------------------------------------------------------------------
# 5. Main dispatch function -----------------------------------------------
# ---------------------------------------------------------------------------
def dispatch(tool: str, params: dict = None) -> dict:
"""Main dispatch function for all agents."""
if params is None:
params = {}
logger.info(f"Dispatching tool: {tool} with params: {params}")
dispatchers = {
"label_github_issue": dispatch_label_github_issue,
"watch_collect": dispatch_watch_collect,
"analyse_watch_report": dispatch_analyse_watch_report,
"curate_digest": dispatch_curate_digest,
"healthcheck": dispatch_healthcheck,
}
if tool not in dispatchers:
available_tools = list(dispatchers.keys())
return {
"status": "error",
"error": f"Unknown tool '{tool}'. Available tools: {available_tools}"
}
try:
result = dispatchers[tool](params)
logger.info(f"Tool {tool} completed with status: {result.get('status', 'unknown')}")
return result
except Exception as e:
logger.exception(f"Error in dispatch for tool {tool}")
return {"status": "error", "error": str(e)}
# ---------------------------------------------------------------------------
# 6. Signal handlers ------------------------------------------------------
# ---------------------------------------------------------------------------
def _signal_handler(_sig, _frm):
logger.info("Bridge process terminated by signal")
sys.exit(0)
signal.signal(signal.SIGTERM, _signal_handler)
signal.signal(signal.SIGINT, _signal_handler)
# ---------------------------------------------------------------------------
# 7. Main execution -------------------------------------------------------
# ---------------------------------------------------------------------------
def main():
logger.info("Bridge started")
# Support direct command line calls: python bridge.py <tool> <json_params>
if len(sys.argv) >= 2 and sys.argv[1] != "-":
tool_name = sys.argv[1]
params_json = sys.argv[2] if len(sys.argv) > 2 else "{}"
try:
params = json.loads(params_json)
result = dispatch(tool_name, params)
print(json.dumps(result), flush=True)
except json.JSONDecodeError as e:
error_result = {"status": "error", "error": f"Invalid JSON parameters: {e}"}
print(json.dumps(error_result), flush=True)
except Exception as e:
error_result = {"status": "error", "error": str(e)}
print(json.dumps(error_result), flush=True)
return
# STDIO mode for Gemini CLI / Claude Code
logger.info("Entering STDIO mode")
try:
for line_num, line in enumerate(sys.stdin, 1):
line = line.strip()
if not line:
continue
try:
payload = json.loads(line)
tool = payload.get("tool")
params = payload.get("params", {})
if not tool:
result = {"status": "error", "error": "Missing 'tool' in payload"}
else:
result = dispatch(tool, params)
print(json.dumps(result), flush=True)
except json.JSONDecodeError as e:
logger.error(f"JSON decode error on line {line_num}: {e}")
error_result = {"status": "error", "error": f"Invalid JSON on line {line_num}: {e}"}
print(json.dumps(error_result), flush=True)
except Exception as e:
logger.exception(f"Unexpected error on line {line_num}")
error_result = {"status": "error", "error": str(e)}
print(json.dumps(error_result), flush=True)
except KeyboardInterrupt:
logger.info("Bridge interrupted by user")
except Exception as e:
logger.exception("Fatal error in STDIO loop")
finally:
logger.info("Bridge stopped")
if __name__ == "__main__":
main()