forked from mshumer/autonomous-researcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_server.py
More file actions
727 lines (616 loc) · 20.8 KB
/
api_server.py
File metadata and controls
727 lines (616 loc) · 20.8 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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
from __future__ import annotations
import json
import queue
import re
import subprocess
import sys
import threading
import os
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Dict, List, Optional, Literal
from dotenv import load_dotenv, set_key
from fastapi import FastAPI, HTTPException
from fastapi.responses import StreamingResponse
from pydantic import BaseModel, Field
# Load environment variables from the repo's .env file so spawned processes inherit them.
BASE_DIR = Path(__file__).resolve().parent
ENV_PATH = BASE_DIR / ".env"
load_dotenv(ENV_PATH)
MAIN_PATH = BASE_DIR / "main.py"
# Regex for stripping ANSI escape sequences (Rich colour codes, etc.).
ANSI_ESCAPE_RE = re.compile(r"\x1B\[[0-?]*[ -/]*[@-~]")
def strip_ansi(text: str) -> str:
"""
Remove ANSI colour / style escape sequences from terminal output.
This is useful for building UIs that want plain text, while still
retaining the original coloured output for advanced terminals.
"""
return ANSI_ESCAPE_RE.sub("", text)
class SingleExperimentRequest(BaseModel):
"""
Request body for running a single-agent experiment.
This directly maps to:
python main.py "<task>" --mode single [--gpu GPU]
"""
task: str = Field(
...,
description=(
"A short natural-language hypothesis to test. This is passed to the "
"single research agent as the main experiment description."
),
examples=[
"Fine-tuning a small transformer on CIFAR-10 improves accuracy by more than 5%."
],
)
gpu: Optional[str] = Field(
None,
description=(
"Optional GPU type to request for the Modal sandbox. "
"Examples: 'T4', 'A10G', 'A100', 'any'. "
"If omitted, the system uses its default (CPU-only or configured GPU)."
),
examples=["T4"],
)
test_mode: bool = Field(
False,
description="If true, runs in test mode with mock data (no LLM/GPU usage).",
)
class OrchestratorExperimentRequest(BaseModel):
"""
Request body for running the multi-agent orchestrator.
This maps to:
python main.py "<task>" --mode orchestrator \\
--num-agents N --max-rounds R --max-parallel P [--gpu GPU]
"""
task: str = Field(
...,
description=(
"High-level research question for the orchestrator to investigate. "
"The orchestrator will decompose this into multiple hypotheses and "
"launch single-agent experiments as needed."
),
examples=[
"Characterize the scaling behaviour of depth vs width in small transformers."
],
)
gpu: Optional[str] = Field(
None,
description=(
"Default GPU hint for experiments spawned by the orchestrator "
"(e.g. 'T4', 'A10G', 'A100', 'any'). If omitted, falls back to the "
"host default (CPU-only or configured GPU)."
),
examples=["A10G"],
)
num_agents: int = Field(
3,
ge=1,
le=16,
description=(
"How many distinct single-agent researchers to launch in the first wave. "
"This is passed as --num-agents."
),
)
max_rounds: int = Field(
3,
ge=1,
le=10,
description=(
"Maximum number of orchestration rounds (waves of experiments). "
"This is passed as --max-rounds."
),
)
max_parallel: int = Field(
2,
ge=1,
le=16,
description=(
"Maximum number of experiments to run in parallel in a single wave. "
"This is passed as --max-parallel."
),
)
test_mode: bool = Field(
False,
description="If true, runs in test mode with mock data (no LLM/GPU usage).",
)
class SummaryHistoryItem(BaseModel):
"""Minimal view of an agent step for sidebar summarization."""
type: Literal["thought", "code", "result", "text"] = Field(
...,
description="Type of step (kept small to control context size).",
)
content: str = Field(
..., description="Truncated text content of the step (agent thought or tool output)."
)
class AgentSummaryRequest(BaseModel):
"""Request body for Gemini-lite sidebar summaries."""
agent_id: str = Field(..., description="Sub-agent identifier")
history: List[SummaryHistoryItem] = Field(
..., description="Last ~5 steps for this agent (already truncated on client)."
)
class AgentSummaryResponse(BaseModel):
"""Shape returned to the frontend for sidebar rendering."""
summary: str = Field(..., description="Short markdown-friendly finding")
chart: Optional[Dict[str, Any]] = Field(
None,
description=(
"Optional chart spec with keys: title, type(line|bar), labels, series. "
"Omitted if no obvious numeric progression."
),
)
class CredentialStatus(BaseModel):
"""Report which required API credentials are present."""
has_google_api_key: bool = Field(
...,
description="True when GOOGLE_API_KEY is set to a non-placeholder value.",
)
has_modal_token: bool = Field(
...,
description="True when both MODAL_TOKEN_ID and MODAL_TOKEN_SECRET are set.",
)
class CredentialUpdateRequest(BaseModel):
"""Payload for setting API credentials via the UI."""
google_api_key: Optional[str] = Field(
None, description="Full Google API key from AI Studio"
)
modal_token_id: Optional[str] = Field(
None, description="Modal token ID from https://modal.com/settings/tokens"
)
modal_token_secret: Optional[str] = Field(
None, description="Modal token secret from https://modal.com/settings/tokens"
)
class ProcessSummary(BaseModel):
"""
Structured view of a completed CLI run.
The stdout/stderr fields preserve all Rich formatting escape codes,
while the *_plain variants provide the same content with ANSI codes stripped
for easy rendering in front-ends.
"""
mode: Literal["single", "orchestrator"] = Field(
...,
description="Which execution mode was used.",
)
task: str = Field(
...,
description="Original task/hypothesis passed to main.py.",
)
gpu: Optional[str] = Field(
None,
description="GPU hint passed to main.py (if any).",
)
command: List[str] = Field(
...,
description="Exact command invoked by the API to run the experiment.",
)
started_at: datetime = Field(
...,
description="UTC timestamp when the subprocess started.",
)
finished_at: datetime = Field(
...,
description="UTC timestamp when the subprocess finished.",
)
duration_seconds: float = Field(
...,
description="Wall-clock run duration in seconds.",
)
exit_code: int = Field(
...,
description="Subprocess exit code. Zero usually indicates success.",
)
stdout: str = Field(
...,
description="Raw stdout produced by main.py (including ANSI colour codes).",
)
stderr: str = Field(
...,
description="Raw stderr produced by main.py (including ANSI colour codes).",
)
stdout_plain: str = Field(
...,
description="Stdout with ANSI escape codes stripped for simple rendering.",
)
stderr_plain: str = Field(
...,
description="Stderr with ANSI escape codes stripped for simple rendering.",
)
app = FastAPI(
title="AI Researcher API",
description=(
"Thin HTTP wrapper around the existing CLI-based AI Researcher agents.\n\n"
"The API does not reimplement any research logic; it simply shells out "
"to `main.py` and returns everything the CLI prints so that a front-end "
"can visualise it in rich ways."
),
version="0.1.0",
)
from fastapi.middleware.cors import CORSMiddleware
# Optional, lightweight summarizer (kept outside agent logic)
from insights import summarize_agent_findings
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:5173", "http://127.0.0.1:5173"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def _env_value_present(value: Optional[str]) -> bool:
"""Treat empty or placeholder values as missing."""
if value is None:
return False
cleaned = value.strip()
if not cleaned:
return False
lower = cleaned.lower()
# Ignore common placeholder patterns (e.g., "your_google_api_key_here").
if lower.startswith("your_") or lower.endswith("_here"):
return False
if lower in {"changeme", "example"}:
return False
return True
def _persist_env(key: str, value: str) -> None:
"""Persist a credential to both the running process and the .env file."""
os.environ[key] = value
set_key(str(ENV_PATH), key, value)
def _credential_status() -> CredentialStatus:
"""Summarize which credentials are available."""
has_google = _env_value_present(os.environ.get("GOOGLE_API_KEY"))
has_modal_id = _env_value_present(os.environ.get("MODAL_TOKEN_ID"))
has_modal_secret = _env_value_present(os.environ.get("MODAL_TOKEN_SECRET"))
return CredentialStatus(
has_google_api_key=has_google,
has_modal_token=has_modal_id and has_modal_secret,
)
def _ensure_main_exists() -> None:
"""
Verify that main.py exists at the expected location.
If not, raise an HTTPException so callers get a clear error.
"""
if not MAIN_PATH.exists():
raise HTTPException(
status_code=500,
detail=f"main.py not found at expected path: {MAIN_PATH}",
)
def _build_single_command(req: SingleExperimentRequest) -> List[str]:
"""
Build the command to run a single-agent experiment via main.py.
"""
cmd: List[str] = [
sys.executable,
str(MAIN_PATH),
req.task,
"--mode",
"single",
]
if req.gpu:
cmd.extend(["--gpu", req.gpu])
if req.test_mode:
cmd.append("--test-mode")
return cmd
def _build_orchestrator_command(req: OrchestratorExperimentRequest) -> List[str]:
"""
Build the command to run the orchestrator via main.py.
"""
cmd: List[str] = [
sys.executable,
str(MAIN_PATH),
req.task,
"--mode",
"orchestrator",
"--num-agents",
str(req.num_agents),
"--max-rounds",
str(req.max_rounds),
"--max-parallel",
str(req.max_parallel),
]
if req.gpu:
cmd.extend(["--gpu", req.gpu])
if req.test_mode:
cmd.append("--test-mode")
return cmd
def _run_and_capture(
cmd: List[str],
*,
mode: Literal["single", "orchestrator"],
task: str,
gpu: Optional[str],
) -> ProcessSummary:
"""
Run `main.py` as a subprocess and capture all of its stdout/stderr.
This leaves the underlying CLI behaviour untouched and simply wraps it.
"""
_ensure_main_exists()
started_at = datetime.now(timezone.utc)
# Enable structured event emission in the child process so the frontend
# can consume ::EVENT::-prefixed messages.
env = dict(os.environ)
env["AI_RESEARCHER_ENABLE_EVENTS"] = "1"
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
bufsize=1,
env=env,
)
stdout_chunks: List[str] = []
stderr_chunks: List[str] = []
def _reader(stream, chunks: List[str]) -> None:
for line in stream:
chunks.append(line)
t_out = threading.Thread(
target=_reader, args=(proc.stdout, stdout_chunks), daemon=True
)
t_err = threading.Thread(
target=_reader, args=(proc.stderr, stderr_chunks), daemon=True
)
t_out.start()
t_err.start()
exit_code = proc.wait()
t_out.join()
t_err.join()
finished_at = datetime.now(timezone.utc)
stdout_text = "".join(stdout_chunks)
stderr_text = "".join(stderr_chunks)
return ProcessSummary(
mode=mode,
task=task,
gpu=gpu,
command=cmd,
started_at=started_at,
finished_at=finished_at,
duration_seconds=(finished_at - started_at).total_seconds(),
exit_code=exit_code,
stdout=stdout_text,
stderr=stderr_text,
stdout_plain=strip_ansi(stdout_text),
stderr_plain=strip_ansi(stderr_text),
)
def _stream_subprocess(
cmd: List[str],
*,
meta: Dict[str, Any],
) -> StreamingResponse:
"""
Stream stdout/stderr from `main.py` as newline-delimited JSON (NDJSON).
Each line of output is sent as:
{
"type": "line",
"stream": "stdout" | "stderr",
"timestamp": "<ISO-8601>",
"raw": "<raw line>",
"plain": "<line without ANSI>",
...meta
}
When the process finishes, a final summary event is sent:
{
"type": "summary",
"exit_code": <int>,
"started_at": "<ISO-8601>",
"finished_at": "<ISO-8601>",
"duration_seconds": <float>,
...meta
}
"""
_ensure_main_exists()
started_at = datetime.now(timezone.utc)
# Enable structured event emission in the child process so the frontend
# can consume ::EVENT::-prefixed messages.
env = dict(os.environ)
env["AI_RESEARCHER_ENABLE_EVENTS"] = "1"
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
bufsize=1,
env=env,
)
# Queue is used to multiplex stdout and stderr into a single ordered stream.
q: "queue.Queue[Optional[Dict[str, Any]]]" = queue.Queue()
def _push_line(stream, stream_name: str) -> None:
for line in stream:
event: Dict[str, Any] = {
"type": "line",
"stream": stream_name,
"timestamp": datetime.now(timezone.utc).isoformat(),
"raw": line,
"plain": strip_ansi(line),
}
event.update(meta)
q.put(event)
def _wait_for_exit() -> None:
exit_code = proc.wait()
finished_at = datetime.now(timezone.utc)
summary: Dict[str, Any] = {
"type": "summary",
"timestamp": finished_at.isoformat(),
"exit_code": exit_code,
"started_at": started_at.isoformat(),
"finished_at": finished_at.isoformat(),
"duration_seconds": (finished_at - started_at).total_seconds(),
}
summary.update(meta)
q.put(summary)
# Sentinel to tell the iterator to stop.
q.put(None)
threading.Thread(
target=_push_line, args=(proc.stdout, "stdout"), daemon=True
).start()
threading.Thread(
target=_push_line, args=(proc.stderr, "stderr"), daemon=True
).start()
threading.Thread(target=_wait_for_exit, daemon=True).start()
def event_iterator():
while True:
item = q.get()
if item is None:
break
yield json.dumps(item, ensure_ascii=False) + "\n"
return StreamingResponse(
event_iterator(),
media_type="application/x-ndjson",
)
@app.get("/api/health", summary="Simple health probe")
def health_check() -> Dict[str, Any]:
"""
Lightweight health check.
This does not call Gemini or Modal; it only verifies that `main.py`
exists on disk and that the API process can see it.
"""
exists = MAIN_PATH.exists()
return {
"status": "ok" if exists else "error",
"main_py": str(MAIN_PATH),
"main_py_exists": exists,
}
@app.get("/api/state", summary="Get global system state")
def get_state() -> Dict[str, Any]:
"""
Return the current global state of the orchestrator.
This is primarily to support legacy clients or status monitors.
"""
# Import here to avoid circular imports if orchestrator imports api_server
try:
from orchestrator import _experiment_counter, _default_gpu
return {
"status": "active",
"experiments_run": _experiment_counter,
"default_gpu": _default_gpu,
}
except ImportError:
return {
"status": "active",
"info": "Orchestrator module not loaded",
}
@app.get(
"/api/credentials/status",
response_model=CredentialStatus,
summary="Check whether required API keys are set",
)
def credentials_status() -> CredentialStatus:
"""
Return a minimal view of credential readiness.
Used by the frontend to gate runs and prompt users for missing keys.
"""
return _credential_status()
@app.post(
"/api/credentials",
response_model=CredentialStatus,
summary="Set Google/Modal credentials (persists to .env)",
)
def update_credentials(req: CredentialUpdateRequest) -> CredentialStatus:
"""
Allow the UI to persist credentials locally.
Keys are written to the running process and the .env file so subsequent
subprocesses inherit them.
"""
try:
if req.google_api_key and req.google_api_key.strip():
_persist_env("GOOGLE_API_KEY", req.google_api_key.strip())
if req.modal_token_id and req.modal_token_id.strip():
_persist_env("MODAL_TOKEN_ID", req.modal_token_id.strip())
if req.modal_token_secret and req.modal_token_secret.strip():
_persist_env("MODAL_TOKEN_SECRET", req.modal_token_secret.strip())
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to persist credentials: {e}") from e
return _credential_status()
@app.post(
"/api/experiments/single",
response_model=ProcessSummary,
summary="Run a single-agent experiment (blocking)",
)
def run_single_experiment(req: SingleExperimentRequest) -> ProcessSummary:
"""
Run the original single-agent researcher and wait for it to finish.
This endpoint blocks until the underlying CLI command exits, then returns
a fully structured `ProcessSummary` containing all CLI output.
"""
cmd = _build_single_command(req)
return _run_and_capture(
cmd,
mode="single",
task=req.task,
gpu=req.gpu,
)
@app.post(
"/api/experiments/orchestrator",
response_model=ProcessSummary,
summary="Run the multi-agent orchestrator (blocking)",
)
def run_orchestrator_experiment(req: OrchestratorExperimentRequest) -> ProcessSummary:
"""
Run the orchestrator mode end-to-end and wait for it to finish.
The returned `ProcessSummary` includes all orchestrator logs, experiment
transcripts, and the final paper generated at the end of the run.
"""
cmd = _build_orchestrator_command(req)
return _run_and_capture(
cmd,
mode="orchestrator",
task=req.task,
gpu=req.gpu,
)
@app.post(
"/api/experiments/single/stream",
summary="Stream a single-agent experiment as newline-delimited JSON",
)
def stream_single_experiment(req: SingleExperimentRequest) -> StreamingResponse:
"""
Run the single-agent researcher and stream all logs as NDJSON.
This is ideal for front-ends that want to show real-time logs or
progressively render the final report as it is produced.
"""
cmd = _build_single_command(req)
meta = {
"mode": "single",
"task": req.task,
"gpu": req.gpu,
"command": cmd,
}
return _stream_subprocess(cmd, meta=meta)
@app.post(
"/api/experiments/orchestrator/stream",
summary="Stream the orchestrator as newline-delimited JSON",
)
def stream_orchestrator_experiment(
req: OrchestratorExperimentRequest,
) -> StreamingResponse:
"""
Run the orchestrator mode and stream all logs as NDJSON.
The stream includes orchestrator thinking, tool calls, and nested
single-agent transcripts exactly as printed by the CLI.
"""
cmd = _build_orchestrator_command(req)
meta = {
"mode": "orchestrator",
"task": req.task,
"gpu": req.gpu,
"command": cmd,
}
return _stream_subprocess(cmd, meta=meta)
@app.post(
"/api/agents/summarize",
response_model=AgentSummaryResponse,
summary="Summarize the last few sub-agent turns for the sidebar",
)
def summarize_agent(req: AgentSummaryRequest) -> AgentSummaryResponse:
"""Run a cheap Gemini call that condenses recent agent thoughts/tool outputs."""
try:
result = summarize_agent_findings(req.agent_id, [item.model_dump() for item in req.history])
except Exception as e:
raise HTTPException(status_code=500, detail=str(e)) from e
return AgentSummaryResponse(**result)
if __name__ == "__main__":
# Convenience entrypoint so you can run:
# python api_server.py
# during development instead of calling uvicorn manually.
import uvicorn
uvicorn.run(
"api_server:app",
host="0.0.0.0",
port=8000,
reload=True,
)