forked from LangQi99/NeoFish
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_headless.py
More file actions
305 lines (238 loc) ยท 10.2 KB
/
run_headless.py
File metadata and controls
305 lines (238 loc) ยท 10.2 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
"""
run_headless.py โ Backend-only HTTP API for programmatic integration.
ๅฏๅจไธไธช FastAPI ๆๅกๅจ๏ผ้่ฟ็บฏ HTTP ๆฅๅฃ่ฎฉๅ
ถไปๅบ็จ่ฐ็จ NeoFish ็ Agent ่ฝๅ๏ผ
ๆ ้ๅฏๅจๅ็ซฏใ้ป่ฎคไฝฟ็จๆๅคด็ๆฌๆบ Chrome ๆต่งๅจ๏ผๆนไพฟไบบๅทฅๆฅ็ฎกใ
Usage::
uv run python run_headless.py
# or:
uv run uvicorn run_headless:app --host 0.0.0.0 --port 8100
Environment
-----------
HEADLESS_API_HOST โ bind address (default: 0.0.0.0)
HEADLESS_API_PORT โ port (default: 8100)
NEOFISH_HEADLESS_BROWSER_MODE โ "local_chrome" (default) | "headless"
ANTHROPIC_API_KEY / ANTHROPIC_BASE_URL / MODEL_NAME โ same as main app
See docs/headless_api.md for the protocol.
"""
from __future__ import annotations
import asyncio
import logging
import os
import uuid
from contextlib import asynccontextmanager
from typing import Any, Optional
from dotenv import load_dotenv
load_dotenv()
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from agent import run_agent_loop
from config import WORKDIR
from memory.session_memory import SessionMemory
from playwright_manager import (
BROWSER_MODE_HEADLESS,
BROWSER_MODE_LOCAL_CHROME,
PlaywrightManager,
)
from session import session_store
logger = logging.getLogger("neofish.headless")
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
)
# โโ Configuration โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
HEADLESS_API_HOST = os.getenv("HEADLESS_API_HOST", "0.0.0.0")
HEADLESS_API_PORT = int(os.getenv("HEADLESS_API_PORT", "8100"))
_MODE_RAW = os.getenv("NEOFISH_HEADLESS_BROWSER_MODE", BROWSER_MODE_LOCAL_CHROME).strip()
_INITIAL_MODE = (
BROWSER_MODE_HEADLESS if _MODE_RAW == BROWSER_MODE_HEADLESS else BROWSER_MODE_LOCAL_CHROME
)
PLATFORM = "headless_api"
# โโ State โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
pm = PlaywrightManager(browser_mode=_INITIAL_MODE)
# Per-session channel where terminal outcomes (completed / needs_input) land.
_outcomes: dict[str, asyncio.Queue] = {}
# The background asyncio.Task running run_agent_loop for each session.
_agent_tasks: dict[str, asyncio.Task] = {}
# Session memory instances kept warm so that follow-up calls reuse task_spec.
_memories: dict[str, SessionMemory] = {}
def _outcome_queue(session_id: str) -> asyncio.Queue:
q = _outcomes.get(session_id)
if q is None:
q = asyncio.Queue()
_outcomes[session_id] = q
return q
def _memory_for(session_id: str) -> SessionMemory:
mem = _memories.get(session_id)
if mem is None:
mem = SessionMemory(session_id=session_id)
_memories[session_id] = mem
return mem
# โโ Agent runner โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
async def _run_agent_once(
session_id: str,
message: str,
images: list[str],
) -> None:
"""Drive one invocation of run_agent_loop and feed terminal events into the outcome queue."""
q = _outcome_queue(session_id)
async def _send(msg: Any) -> None:
# Only surface the final task report to the API caller. All other info
# events are swallowed (caller doesn't want the thought stream).
if isinstance(msg, dict) and msg.get("message_key") == "common.task_completed":
report = msg.get("params", {}).get("report") or msg.get("message", "")
await q.put({"status": "completed", "output": report})
async def _request_action(reason: str, image: Optional[str]) -> None:
payload = {"status": "needs_input", "reason": reason}
if image:
payload["screenshot"] = image
await q.put(payload)
async def _send_image(description: str, image_b64: str) -> None:
# Headless API doesn't stream images mid-task โ they're accessible via
# the browser window and will be included in the final report if relevant.
return
async def _send_file(file_path: str, description: str) -> None:
return
try:
await run_agent_loop(
pm,
message,
_send,
_request_action,
_send_image,
_send_file,
images=images,
session_store=session_store,
session_id=session_id,
session_memory=_memory_for(session_id),
source_meta={
"platform": PLATFORM,
"session_id": session_id,
},
)
except Exception as e:
logger.exception("Agent loop crashed for session %s", session_id)
await q.put({"status": "completed", "output": f"[agent error] {e}"})
finally:
session_store.set_running(session_id, False)
# If the loop exited without ever producing a terminal event
# (e.g. max-steps), synthesize a completed outcome so the HTTP call returns.
if q.empty():
await q.put({"status": "completed", "output": "(agent finished without report)"})
# โโ FastAPI lifespan โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
@asynccontextmanager
async def lifespan(app: FastAPI):
logger.info(
"Starting PlaywrightManager (browser_mode=%s)โฆ", pm.browser_mode
)
await pm.start()
logger.info("PlaywrightManager ready. Headless API listening.")
yield
logger.info("Shutting down PlaywrightManagerโฆ")
for task in list(_agent_tasks.values()):
if not task.done():
task.cancel()
await pm.stop()
app = FastAPI(title="NeoFish Headless API", lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# โโ Request / response models โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
class ChatBody(BaseModel):
message: str
session_id: Optional[str] = None
images: Optional[list[str]] = None
timeout_seconds: Optional[float] = None # per-call wait for the next outcome
# โโ Endpoints โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
@app.get("/health")
def health() -> dict:
return {
"ok": True,
"browser_mode": pm.browser_mode,
"sessions": len(_agent_tasks),
}
@app.get("/browser/mode")
def get_browser_mode() -> dict:
return {"mode": pm.browser_mode}
@app.post("/v1/chat")
async def chat(body: ChatBody) -> dict:
if not body.message or not body.message.strip():
raise HTTPException(status_code=400, detail="message must not be empty")
session_id = body.session_id
if not session_id:
chat_id = str(uuid.uuid4())
session_id = session_store.get_or_create(PLATFORM, chat_id)
images = body.images or []
q = _outcome_queue(session_id)
existing = _agent_tasks.get(session_id)
agent_running = existing is not None and not existing.done()
if agent_running:
# Agent is alive โ likely blocked waiting for human assistance.
# Enqueue the new input so drain_queue_nowait picks it up, then unblock.
await session_store.enqueue_message(session_id, body.message, images)
if pm.is_waiting_for_human(session_id):
pm.signal_resume(session_id)
else:
# Fresh run: try_start claims the session slot atomically.
if not await session_store.try_start(session_id):
raise HTTPException(
status_code=409,
detail="Session already busy; retry shortly.",
)
task = asyncio.create_task(
_run_agent_once(session_id, body.message, images),
name=f"neofish-agent-{session_id}",
)
_agent_tasks[session_id] = task
timeout = body.timeout_seconds if body.timeout_seconds and body.timeout_seconds > 0 else None
try:
if timeout is None:
outcome = await q.get()
else:
outcome = await asyncio.wait_for(q.get(), timeout=timeout)
except asyncio.TimeoutError:
raise HTTPException(
status_code=504,
detail={
"status": "timeout",
"session_id": session_id,
"message": "Agent did not reach a terminal state before the deadline; retry POST /v1/chat with the same session_id (and optional new instructions) to keep waiting.",
},
)
outcome["session_id"] = session_id
return outcome
@app.get("/v1/chat/{session_id}")
async def get_session(session_id: str) -> dict:
task = _agent_tasks.get(session_id)
return {
"session_id": session_id,
"active": bool(task and not task.done()),
"waiting_for_human": pm.is_waiting_for_human(session_id),
"browser_mode": pm.browser_mode,
}
@app.delete("/v1/chat/{session_id}")
async def delete_session(session_id: str) -> dict:
task = _agent_tasks.pop(session_id, None)
if task and not task.done():
task.cancel()
try:
await task
except (asyncio.CancelledError, Exception):
pass
_outcomes.pop(session_id, None)
_memories.pop(session_id, None)
session_store.set_running(session_id, False)
await pm.close_tab(session_id)
return {"ok": True}
# โโ Entry โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"run_headless:app",
host=HEADLESS_API_HOST,
port=HEADLESS_API_PORT,
reload=False,
)