-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsynapse_forge.py
More file actions
623 lines (540 loc) · 26.1 KB
/
Copy pathsynapse_forge.py
File metadata and controls
623 lines (540 loc) · 26.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
#!/usr/bin/env python3
"""
synapse_forge.py — Synapse_COR + Forge Engine (Portable Core)
Version: 3.2.3
License: MIT
Author: DJ Generation / FUTRON Project
Context · Objective · Role orchestration kernel.
Works standalone with any OpenAI-compatible LLM endpoint.
No FUTRON installation required. Auto-detects available providers.
Environment variables:
SYNAPSE_FORGE_HOME Base directory (default: ~/.synapse-forge)
SYNAPSE_FORGE_REGISTRY Path to specialists.json
SYNAPSE_FORGE_URL OpenAI-compatible endpoint (optional — auto-detected if unset)
SYNAPSE_FORGE_API_KEY API key for endpoint (default: none)
SYNAPSE_FORGE_MODEL Default model (default: opencode/nemotron-3-super-free)
SYNAPSE_FORGE_FALLBACK Fallback model (default: gpt-4o-mini)
"""
import json
import os
import sys
import time
import datetime
import subprocess
import tempfile
import shutil
from pathlib import Path
# ── Configuration ──────────────────────────────────────────────────────────────
HOME = Path.home()
FORGE_HOME = Path(os.environ.get("SYNAPSE_FORGE_HOME", str(HOME / ".synapse-forge")))
REGISTRY_PATH = Path(os.environ.get("SYNAPSE_FORGE_REGISTRY", str(FORGE_HOME / "specialists.json")))
FORGE_URL = os.environ.get("SYNAPSE_FORGE_URL", "") # empty = auto-detect via detect_providers()
FORGE_API_KEY = os.environ.get("SYNAPSE_FORGE_API_KEY", "")
DEFAULT_MODEL = os.environ.get("SYNAPSE_FORGE_MODEL", "opencode/nemotron-3-super-free")
FALLBACK_MODEL = os.environ.get("SYNAPSE_FORGE_FALLBACK", "gpt-4o-mini")
STATE_DIR = FORGE_HOME / "state"
HISTORY_FILE = STATE_DIR / "plan-history.jsonl"
LOG_FILE = STATE_DIR / "synapse.log"
FORGE_HOME.mkdir(parents=True, exist_ok=True)
STATE_DIR.mkdir(parents=True, exist_ok=True)
# ── Logging ─────────────────────────────────────────────────────────────────────
def log(msg, level="info"):
ts = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
labels = {"info": "[synapse]", "ok": "[synapse:ok]", "warn": "[synapse:warn]", "err": "[synapse:err]"}
label = labels.get(level, "[synapse]")
print(f"{label} {msg}", file=sys.stderr)
with open(LOG_FILE, "a") as f:
f.write(f"[{ts}] {level.upper()}: {msg}\n")
def die(msg):
log(msg, "err")
sys.exit(1)
# ── Registry ─────────────────────────────────────────────────────────────────────
def load_registry():
if REGISTRY_PATH.exists():
with open(REGISTRY_PATH) as f:
return json.load(f)
return {"schema_version": "1.0", "created": str(datetime.date.today()), "specialists": []}
def save_registry(registry):
with open(REGISTRY_PATH, "w") as f:
json.dump(registry, f, indent=2)
def get_specialist(specialist_id):
registry = load_registry()
for s in registry.get("specialists", []):
if s["id"] == specialist_id:
return s
return None
def upsert_specialist(spec):
registry = load_registry()
specialists = registry.get("specialists", [])
for i, s in enumerate(specialists):
if s["id"] == spec["id"]:
specialists[i] = spec
registry["specialists"] = specialists
save_registry(registry)
return True
specialists.append(spec)
registry["specialists"] = specialists
save_registry(registry)
return True
# ── Provider auto-detection (lazy) ────────────────────────────────────────────
_detected_providers = None # cache; populated on first call_llm_with_fallback call
def _get_providers():
"""Lazy-load detect_providers result. Cached for lifetime of process."""
global _detected_providers
if _detected_providers is not None:
return _detected_providers
try:
# Support both package import and standalone file execution
_mod_dir = Path(__file__).parent
_pkg_detect = _mod_dir / "synapse_forge" / "detect.py"
if _pkg_detect.exists():
import importlib.util
spec = importlib.util.spec_from_file_location("synapse_forge_detect", str(_pkg_detect))
mod = importlib.util.load_module_from_spec(spec) if hasattr(importlib.util, "load_module_from_spec") else None
if mod is None:
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
_detected_providers = mod.detect_providers(verbose=False)
else:
_detected_providers = {"available": [], "missing_recommended": []}
except Exception:
_detected_providers = {"available": [], "missing_recommended": []}
return _detected_providers
# ── LLM Call (OpenAI-compatible) ───────────────────────────────────────────────
def call_llm(prompt, model=None, system_prompt=None, timeout=60, forge_url=None, api_key=None):
"""
Call any OpenAI-compatible endpoint.
Tries: requests library, then curl fallback.
"""
model = model or DEFAULT_MODEL
url = (forge_url or FORGE_URL or "").rstrip("/")
key = api_key or FORGE_API_KEY
if not url:
return None
messages = []
if system_prompt:
messages.append({"role": "system", "content": system_prompt})
messages.append({"role": "user", "content": prompt})
payload = {"model": model, "messages": messages, "max_tokens": 2048}
headers = {"Content-Type": "application/json"}
if key:
headers["Authorization"] = f"Bearer {key}"
# Try requests first
try:
import requests
resp = requests.post(
f"{url}/v1/chat/completions",
json=payload, headers=headers, timeout=timeout
)
if resp.status_code == 200:
data = resp.json()
return data["choices"][0]["message"]["content"]
except Exception:
pass
# Curl fallback
try:
payload_str = json.dumps(payload)
header_args = " ".join([f'-H "{k}: {v}"' for k, v in headers.items()])
cmd = (f'curl -s --max-time {timeout} -X POST {url}/v1/chat/completions '
f'{header_args} -d \'{payload_str}\'')
result = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=timeout+5)
if result.returncode == 0 and result.stdout:
data = json.loads(result.stdout)
return data["choices"][0]["message"]["content"]
except Exception:
pass
return None
def _call_gemini_cli_subprocess(prompt, binary="gemini-cli", free_mode=True, timeout=60):
"""Call Gemini CLI via subprocess, returning text or None."""
args = [binary]
if free_mode:
args.append("--free")
try:
r = subprocess.run(
args, input=prompt, capture_output=True, text=True, timeout=timeout
)
output = r.stdout.strip()
if r.returncode == 0 and output:
return output
# Some builds write to stderr on success
if r.returncode == 0 and r.stderr.strip():
return r.stderr.strip()
# Check for auth errors — don't return noise
combined = (r.stdout + r.stderr).lower()
auth_signals = ["sign in", "login", "unauthorized", "401", "403", "credential"]
if any(s in combined for s in auth_signals):
return None
return None
except (subprocess.TimeoutExpired, FileNotFoundError, Exception):
return None
def call_llm_with_fallback(prompt, model=None, system_prompt=None, timeout=60):
"""
Auto-cascade LLM call. Order: SYNAPSE_FORGE_URL → Ollama → Gemini CLI free →
Gemini CLI OAuth → paid API keys → stub.
This is the primary entry point for all dispatch calls in v3.2.0+.
"""
# Augment prompt with system_prompt if provided (for subprocess paths)
full_prompt = prompt
if system_prompt:
full_prompt = f"[System]: {system_prompt}\n\n{prompt}"
# Path 1: Explicit SYNAPSE_FORGE_URL takes absolute priority
if FORGE_URL:
result = call_llm(prompt, model=model, system_prompt=system_prompt,
timeout=timeout, forge_url=FORGE_URL, api_key=FORGE_API_KEY)
if result:
return result
log("SYNAPSE_FORGE_URL endpoint failed — falling through cascade", "warn")
# Load auto-detected providers (cached)
providers = _get_providers().get("available", [])
for provider in providers:
pid = provider["id"]
# Ollama or other local OpenAI-compat
if provider["type"] == "openai_compat" and provider.get("endpoint"):
ep = provider["endpoint"]
key = provider.get("api_key", "")
result = call_llm(prompt, model=model, system_prompt=system_prompt,
timeout=timeout, forge_url=ep, api_key=key)
if result:
log(f"Provider used: {pid}", "ok")
return result
# Gemini CLI free mode (subprocess)
elif pid == "gemini_cli_free":
cmd_str = provider.get("command", "gemini-cli --free")
binary = cmd_str.split()[0]
result = _call_gemini_cli_subprocess(full_prompt, binary=binary,
free_mode=True, timeout=timeout)
if result:
log("Provider used: gemini_cli_free", "ok")
return result
# Gemini CLI OAuth (subprocess)
elif pid == "gemini_cli_oauth":
result = _call_gemini_cli_subprocess(full_prompt, binary="gemini",
free_mode=False, timeout=timeout)
if result:
log("Provider used: gemini_cli_oauth", "ok")
return result
# Native Gemini API
elif pid == "gemini_api":
api_key = os.environ.get("GEMINI_API_KEY", "")
if not api_key:
# Try FUTRON llm.env
futron_env_path = HOME / ".openclaw" / "credentials" / "llm.env"
if futron_env_path.exists():
for line in futron_env_path.read_text().splitlines():
if line.startswith("GEMINI_API_KEY="):
api_key = line.split("=", 1)[1].strip().strip('"').strip("'")
break
if api_key:
result = call_llm(prompt, model=model or "gemini-2.0-flash",
system_prompt=system_prompt, timeout=timeout,
forge_url="https://generativelanguage.googleapis.com/v1beta",
api_key=api_key)
if result:
log("Provider used: gemini_api", "ok")
return result
# Generic OpenAI-compat API keys
elif provider["type"] == "openai_compat" and provider.get("auth") == "api_key":
env_key = provider.get("env_key", "")
key_val = os.environ.get(env_key, "")
if key_val and provider.get("endpoint"):
result = call_llm(prompt, model=model, system_prompt=system_prompt,
timeout=timeout, forge_url=provider["endpoint"],
api_key=key_val)
if result:
log(f"Provider used: {pid}", "ok")
return result
# All paths exhausted
return None
# ── PLAN phase ─────────────────────────────────────────────────────────────────
DETERMINISTIC_KEYWORDS = [
"grep", "count", "list", "find", "diff", "format", "convert",
"parse", "extract field", "check if", "verify exists", "transform"
]
SPECIALIST_DOMAIN_MAP = {
"security": "security-auditor",
"audit": "security-auditor",
"code review": "code-reviewer",
"review code": "code-reviewer",
"content": "content-strategist",
"social media": "content-strategist",
"financial": "financial-analyst",
"trading": "financial-analyst",
"ui": "ui-auditor",
"dashboard": "ui-auditor",
"file recovery": "phantom-file-archaeologist",
}
MCC_DOMAIN_MAP = {
"security-auditor": "S3-INSIGHT-CATALYST",
"code-reviewer": "S5-ENLIGHTENMENT",
"financial-analyst": "S5-ENLIGHTENMENT",
"content-strategist": "S1-DIVERGENT-EXPLORER",
"ui-auditor": "S3-INSIGHT-CATALYST",
"phantom-file-archaeologist": "S3-INSIGHT-CATALYST",
}
def is_deterministic(text):
return any(kw in text.lower() for kw in DETERMINISTIC_KEYWORDS)
def infer_specialist(goal):
goal_lower = goal.lower()
for keyword, sid in SPECIALIST_DOMAIN_MAP.items():
if keyword in goal_lower:
return sid
return None
def plan(goal, output_path=None):
"""PLAN phase: decompose goal into subtasks, route each to appropriate execution path."""
log(f"PLAN: {goal[:80]}")
inferred_id = infer_specialist(goal)
needs_compose = (inferred_id is None) or (get_specialist(inferred_id) is None)
if inferred_id is None:
inferred_id = f"auto-{goal.split()[0].lower()}-specialist"
mcc = MCC_DOMAIN_MAP.get(inferred_id, "S3-INSIGHT-CATALYST")
has_det_step = any(kw in goal.lower() for kw in ["audit", "scan", "review", "check", "verify", "analyze"])
subtasks = []
tid = 1
if has_det_step:
subtasks.append({
"id": f"subtask-{tid:02d}", "type": "deterministic",
"description": f"Deterministic scan for: {goal[:60]}",
"cli_commands": [f"echo 'Scanning for: {goal[:40]}'"],
"estimated_tokens": 0
})
tid += 1
subtasks.append({
"id": f"subtask-{tid:02d}", "type": "probabilistic",
"description": f"Specialist analysis: {goal}",
"target_specialist": inferred_id,
"needs_compose": needs_compose,
"model": DEFAULT_MODEL,
"fallback_model": FALLBACK_MODEL,
"mcc_stack": mcc,
"estimated_tokens": 800
})
tid += 1
subtasks.append({
"id": f"subtask-{tid:02d}", "type": "deterministic",
"description": "ValidationAgent: verify output and synthesize",
"cli_commands": ["# ValidationAgent verification step"],
"estimated_tokens": 0
})
plan_data = {
"plan_id": f"plan-{time.time_ns()}",
"created": datetime.datetime.now().isoformat(),
"goal": goal,
"synapse_cor": {
"context": goal, "objective": "Execute and validate",
"role": "Architect Mode", "operating_mode": "Architect"
},
"cor_template": {
"goal": goal, "progress": 0,
"preferences": ["deterministic-first", "free-tier-preferred"],
"adjustments": "Use cheapest capable model per task",
"strategy": ["1. Deterministic scan", "2. Specialist analysis", "3. Validate"],
"expertise": f"Expertise in {inferred_id.replace('-', ' ')}",
"verbosity": "med"
},
"subtasks": subtasks,
"routing": {
"primary_specialist": inferred_id,
"primary_model": DEFAULT_MODEL,
"fallback_model": FALLBACK_MODEL,
"mcc_stack": mcc
},
"estimated_tokens": {"total": 800, "cost_usd": 0.00}
}
if output_path:
with open(output_path, "w") as f:
json.dump(plan_data, f, indent=2)
log(f"Plan written to {output_path}", "ok")
else:
plan_path = STATE_DIR / f"{plan_data['plan_id']}.json"
with open(plan_path, "w") as f:
json.dump(plan_data, f, indent=2)
log(f"Plan written to {plan_path}", "ok")
with open(HISTORY_FILE, "a") as f:
f.write(json.dumps({"plan_id": plan_data["plan_id"], "goal": goal[:80]}) + "\n")
return plan_data
# ── COMPOSE phase ──────────────────────────────────────────────────────────────
def compose(plan_data):
"""COMPOSE phase: build and register specialist specs for probabilistic subtasks."""
composed = []
for subtask in plan_data.get("subtasks", []):
if subtask.get("type") != "probabilistic":
continue
if not subtask.get("needs_compose", False):
log(f"Specialist '{subtask['target_specialist']}' already registered", "ok")
composed.append({"id": subtask["target_specialist"], "action": "existing"})
continue
sid = subtask["target_specialist"]
log(f"Composing specialist: {sid}")
spec = {
"id": sid, "version": "1.0",
"created": datetime.datetime.now().isoformat(),
"synapse_persona": (
f"I am an expert in {sid.replace('-', ' ')}. "
"I will reason step-by-step to achieve the goal using available tools and frameworks."
),
"mcc_stack": subtask.get("mcc_stack", "S3-INSIGHT-CATALYST"),
"mcc_knobs": {"A_constraint_tightness": "medium", "E_error_sensitivity": "medium-high"},
"cor_template": {
"goal": plan_data.get("goal", ""),
"progress": 0, "preferences": ["evidence-based"],
"adjustments": "Never fabricate; cite sources",
"strategy": ["1. Gather facts", "2. Analyze", "3. Report"],
"expertise": f"Expertise in {sid.replace('-', ' ')}",
"verbosity": "med"
},
"required_agents": [
"PromptAgent", "AssigningAgent", "CreationAgent",
"ErrorHandlingAgent", "ValidationAgent"
],
"required_protocols": [
"QualityControlProtocol", "ValidationProtocol",
"ErrorHandlingProtocol", "DirectiveAlignmentProtocol"
],
"model": subtask.get("model", DEFAULT_MODEL),
"fallback_model": subtask.get("fallback_model", FALLBACK_MODEL),
"tools": [],
"spawn_command": f"synapse-forge dispatch --specialist {sid} '<task>'"
}
upsert_specialist(spec)
log(f"Specialist '{sid}' registered at {REGISTRY_PATH}", "ok")
composed.append({"id": sid, "action": "composed"})
return composed
# ── DISPATCH phase ─────────────────────────────────────────────────────────────
def dispatch(plan_data):
"""DISPATCH phase: fire subtasks, apply ValidationAgent, return synthesis."""
goal = plan_data.get("goal", "")
results = []
for subtask in plan_data.get("subtasks", []):
sub_id = subtask.get("id", "unknown")
sub_type = subtask.get("type", "")
if sub_type == "deterministic":
cmds = subtask.get("cli_commands", [])
sub_results = []
for cmd in cmds:
if cmd.startswith("#"):
sub_results.append({"cmd": cmd, "output": "symbolic"})
continue
try:
r = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=30)
sub_results.append({"cmd": cmd, "rc": r.returncode, "output": r.stdout.strip()[:200]})
except Exception as e:
sub_results.append({"cmd": cmd, "error": str(e)})
results.append({"subtask_id": sub_id, "type": "deterministic", "results": sub_results})
elif sub_type == "probabilistic":
sid = subtask.get("target_specialist")
spec = get_specialist(sid) if sid else None
agents = spec.get("required_agents", []) if spec else []
protocols = spec.get("required_protocols", []) if spec else []
persona = spec.get("synapse_persona", "") if spec else ""
task_brief = (
f"SYNAPSE_COR ACTIVATED — Specialist: {sid}\n"
f"Active Agents: {', '.join(agents)}\n"
f"Active Protocols: {', '.join(protocols)}\n\n"
f"TASK: {goal}"
)
system_prompt = (
"You are a specialist agent operating under the Synapse_COR framework. "
f"{persona} "
"Always announce which agents and protocols you are using in **bold**. "
"Apply ValidationAgent verification after completing your analysis. "
"Use ErrorHandlingProtocol (5-block: What/Why/Impact/Fix/Prevent) on any errors."
)
# v3.2.0: use auto-cascade instead of single call_llm
output = call_llm_with_fallback(task_brief, model=subtask.get("model"),
system_prompt=system_prompt)
if output is None:
output = (
f"**{sid}** activated (stub — no LLM endpoint reachable).\n"
f"**ValidationAgent** confirmed agents active: {agents[:3]}\n"
f"**AutomatedComplianceCheckProtocol** passed.\n"
f"Goal: {goal[:200]}\n\n"
f"[No LLM provider detected. Run: npm install -g @google/gemini-cli\n"
f" or set SYNAPSE_FORGE_URL / OPENAI_API_KEY / GEMINI_API_KEY]"
)
log(f"All LLM backends unavailable — stub output for {sub_id}", "warn")
results.append({
"subtask_id": sub_id, "type": "probabilistic",
"specialist": sid, "output": output[:3000]
})
combined = " ".join(str(r.get("output", "")) for r in results if r.get("type") == "probabilistic")
validation_ok = any(kw.lower() in combined.lower() for kw in ["Agent", "Protocol", "Synapse"])
return {
"plan_id": plan_data.get("plan_id"),
"goal": goal,
"completed_at": datetime.datetime.now().isoformat(),
"subtask_results": results,
"validation_passed": validation_ok,
"agents_activated": ["Synapse_COR", "ValidationAgent"],
"protocols_activated": ["ValidationProtocol", "ErrorHandlingProtocol"]
}
# ── Full pipeline ──────────────────────────────────────────────────────────────
def run(goal, output_path=None):
"""Full pipeline: plan → compose → dispatch."""
plan_data = plan(goal, output_path)
compose(plan_data)
return dispatch(plan_data)
# ── Adapter registry ───────────────────────────────────────────────────────────
_ADAPTER_REGISTRY = {}
def register_adapter(name, adapter_cls):
"""Register a named adapter class. Called at import time by adapter modules."""
_ADAPTER_REGISTRY[name] = adapter_cls
def get_adapter(name=None):
"""Return the active adapter class. Defaults to env var SYNAPSE_FORGE_ADAPTER or 'openai-compat'."""
name = name or os.environ.get("SYNAPSE_FORGE_ADAPTER", "openai-compat")
return _ADAPTER_REGISTRY.get(name)
# ── main() entry point (for pyproject.toml [project.scripts]) ──────────────────
def main():
"""Entry point for `synapse-cor` CLI command installed via pip."""
import argparse
parser = argparse.ArgumentParser(prog="synapse-cor", description="Synapse_COR Engine v3.2.3")
sub = parser.add_subparsers(dest="cmd")
p_plan = sub.add_parser("plan", help="Decompose a goal into a PLAN.json")
p_plan.add_argument("goal")
p_plan.add_argument("--output", "-o", help="Write plan to this path instead of state dir")
p_compose = sub.add_parser("compose", help="Register specialists from a PLAN.json")
p_compose.add_argument("plan_json")
p_dispatch = sub.add_parser("dispatch", help="Execute a PLAN.json")
p_dispatch.add_argument("plan_json")
p_run = sub.add_parser("run", help="Full pipeline: plan → compose → dispatch")
p_run.add_argument("goal")
sub.add_parser("status", help="Show registry and endpoint status")
sub.add_parser("detect", help="Run provider auto-detection and print results")
args = parser.parse_args()
if args.cmd == "plan":
result = plan(args.goal, args.output)
print(json.dumps(result, indent=2))
elif args.cmd == "compose":
with open(args.plan_json) as f:
plan_data = json.load(f)
result = compose(plan_data)
print(json.dumps(result, indent=2))
elif args.cmd == "dispatch":
with open(args.plan_json) as f:
plan_data = json.load(f)
result = dispatch(plan_data)
print(json.dumps(result, indent=2))
elif args.cmd == "run":
result = run(args.goal)
print(json.dumps(result, indent=2))
elif args.cmd == "detect":
providers = _get_providers()
print(json.dumps(providers, indent=2))
elif args.cmd == "status":
registry = load_registry()
providers = _get_providers()
best = providers["available"][0] if providers["available"] else None
print(json.dumps({
"version": "3.2.3",
"forge_url": FORGE_URL or "(auto-detect)",
"registry": str(REGISTRY_PATH),
"specialists": len(registry.get("specialists", [])),
"specialist_ids": [s["id"] for s in registry.get("specialists", [])],
"active_adapter": os.environ.get("SYNAPSE_FORGE_ADAPTER", "openai-compat"),
"detected_providers": len(providers["available"]),
"best_provider": best["id"] if best else None,
}, indent=2))
else:
parser.print_help()
# ── CLI entry point ────────────────────────────────────────────────────────────
if __name__ == "__main__":
main()