-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi_server.py
More file actions
350 lines (269 loc) · 10.2 KB
/
api_server.py
File metadata and controls
350 lines (269 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
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
#!/usr/bin/env python3
import asyncio
import json
import os
import signal
from typing import Optional
import uvicorn
from fastapi import FastAPI, Header, HTTPException, Query, Request
from fastapi.responses import FileResponse, Response
from pydantic import BaseModel, ConfigDict, Field
app = FastAPI()
def _to_camel(name: str) -> str:
"""Convert a snake_case string to camelCase."""
parts = name.split("_")
return parts[0] + "".join(p.capitalize() for p in parts[1:])
def _normalize_keys(obj):
"""Recursively convert all dict keys from snake_case to camelCase."""
if isinstance(obj, dict):
return {_to_camel(k): _normalize_keys(v) for k, v in obj.items()}
if isinstance(obj, list):
return [_normalize_keys(item) for item in obj]
return obj
def _normalize_response(raw: bytes) -> bytes:
"""Parse raw JSON bytes, normalize keys to camelCase, return bytes."""
try:
parsed = json.loads(raw)
normalized = _normalize_keys(parsed)
return json.dumps(normalized).encode()
except (json.JSONDecodeError, ValueError):
return raw
def _shutdown(sig, _frame):
for ws, proc in list(busy_workspaces.items()):
try:
proc.terminate()
except ProcessLookupError:
pass
raise SystemExit(0)
signal.signal(signal.SIGTERM, _shutdown)
signal.signal(signal.SIGINT, _shutdown)
ROOT_WORKSPACE = "/workspaces"
CLAUDE_MD_TEMPLATE = "/home/claude/.claude/CLAUDE.md.template"
SYSTEM_HINT_FILE = "/home/claude/.claude/system-hint.txt"
SYSTEM_HINT = ""
if os.path.isfile(SYSTEM_HINT_FILE):
with open(SYSTEM_HINT_FILE) as _f:
SYSTEM_HINT = _f.read().strip()
API_TOKEN = os.environ.get("CLAUDE_MODE_API_TOKEN", "")
PORT = int(os.environ.get("CLAUDE_MODE_API_PORT", "8080"))
busy_workspaces: dict[str, asyncio.subprocess.Process] = {}
def _check_auth(authorization: Optional[str]):
if not API_TOKEN:
return
if not authorization or authorization != f"Bearer {API_TOKEN}":
raise HTTPException(status_code=401, detail="unauthorized")
def _resolve_workspace(workspace: Optional[str]) -> str:
sub = workspace.lstrip("/") if workspace else ""
resolved = os.path.join(ROOT_WORKSPACE, sub) if sub else ROOT_WORKSPACE
resolved = os.path.realpath(resolved)
if not resolved.startswith(os.path.realpath(ROOT_WORKSPACE)):
raise HTTPException(status_code=400, detail="path outside root workspace")
return resolved
def _resolve_path(path: str) -> str:
full = os.path.realpath(os.path.join(ROOT_WORKSPACE, path.lstrip("/")))
if not full.startswith(os.path.realpath(ROOT_WORKSPACE)):
raise HTTPException(status_code=400, detail="path outside root workspace")
return full
class RunRequest(BaseModel):
model_config = ConfigDict(populate_by_name=True)
prompt: str
workspace: Optional[str] = None
model: Optional[str] = None
system_prompt: Optional[str] = Field(None, alias="systemPrompt")
append_system_prompt: Optional[str] = Field(None, alias="appendSystemPrompt")
json_schema: Optional[str] = Field(None, alias="jsonSchema")
effort: Optional[str] = None
no_continue: bool = Field(False, alias="noContinue")
resume: Optional[str] = None
fire_and_forget: bool = Field(False, alias="fireAndForget")
def _build_args(req: RunRequest, with_continue: bool = False):
args = ["claude", "--dangerously-skip-permissions"]
if req.resume:
args += ["--resume", req.resume]
elif with_continue and not req.no_continue:
args.append("--continue")
args += ["-p", req.prompt, "--output-format", "json"]
if req.model:
args += ["--model", req.model]
if req.system_prompt:
args += ["--system-prompt", req.system_prompt]
# always append system hint + any user append
append_parts = []
if SYSTEM_HINT:
append_parts.append(SYSTEM_HINT)
if req.append_system_prompt:
append_parts.append(req.append_system_prompt)
if append_parts:
args += ["--append-system-prompt", "\n".join(append_parts)]
if req.json_schema:
args += ["--json-schema", req.json_schema]
if req.effort:
args += ["--effort", req.effort]
return args
def _build_env():
return {
**os.environ,
"HOME": "/home/claude",
"CLAUDE_CONFIG_DIR": "/home/claude/.claude",
"PATH": f"/home/claude/.claude/bin:/home/claude/.local/bin:{os.environ.get('PATH', '')}",
}
async def _stream(workspace: str, req: RunRequest):
env = _build_env()
try:
if req.no_continue or req.resume:
proc = await asyncio.create_subprocess_exec(
*_build_args(req, with_continue=False),
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT,
cwd=workspace,
env=env,
)
busy_workspaces[workspace] = proc
if proc.stdout:
async for line in proc.stdout:
yield line
await proc.wait()
return
# try with --continue first, fall back without
proc = await asyncio.create_subprocess_exec(
*_build_args(req, with_continue=True),
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT,
cwd=workspace,
env=env,
)
busy_workspaces[workspace] = proc
output = b""
if proc.stdout:
async for line in proc.stdout:
output += line
yield line
await proc.wait()
if proc.returncode != 0 and not output:
proc = await asyncio.create_subprocess_exec(
*_build_args(req, with_continue=False),
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT,
cwd=workspace,
env=env,
)
busy_workspaces[workspace] = proc
if proc.stdout:
async for line in proc.stdout:
yield line
await proc.wait()
finally:
busy_workspaces.pop(workspace, None)
@app.post("/run")
async def run(
request: Request,
req: RunRequest,
authorization: Optional[str] = Header(None),
):
_check_auth(authorization)
workspace = _resolve_workspace(req.workspace)
if not os.path.isdir(workspace):
raise HTTPException(status_code=400, detail=f"workspace not found: {workspace}")
# seed CLAUDE.md from template if missing
claude_md = os.path.join(workspace, "CLAUDE.md")
if not os.path.isfile(claude_md) and os.path.isfile(CLAUDE_MD_TEMPLATE):
import shutil
shutil.copy2(CLAUDE_MD_TEMPLATE, claude_md)
if workspace in busy_workspaces:
raise HTTPException(status_code=409, detail="workspace busy, retry later")
busy_workspaces[workspace] = None # type: ignore[assignment]
watcher = None
if not req.fire_and_forget:
async def _disconnect_watcher():
"""Kill the claude process if the client disconnects."""
while workspace in busy_workspaces:
if await request.is_disconnected():
proc = busy_workspaces.get(workspace)
if proc:
proc.kill()
return
await asyncio.sleep(1)
watcher = asyncio.create_task(_disconnect_watcher())
output = b""
try:
async for chunk in _stream(workspace, req):
output += chunk
finally:
if watcher:
watcher.cancel()
if not req.fire_and_forget and await request.is_disconnected():
return Response(status_code=499)
return Response(content=_normalize_response(output), media_type="application/json")
@app.get("/files/{path:path}")
@app.get("/files")
async def get_files(
path: str = "",
authorization: Optional[str] = Header(None),
):
_check_auth(authorization)
full = _resolve_path(path) if path else os.path.realpath(ROOT_WORKSPACE)
if not os.path.exists(full):
raise HTTPException(status_code=404, detail=f"not found: {path}")
if os.path.isdir(full):
entries = []
for name in sorted(os.listdir(full)):
entry_path = os.path.join(full, name)
entry: dict = {
"name": name,
"type": "dir" if os.path.isdir(entry_path) else "file",
}
if entry["type"] == "file":
entry["size"] = os.path.getsize(entry_path)
entries.append(entry)
return {"path": path or "/", "entries": entries}
return FileResponse(full)
@app.put("/files/{path:path}")
async def put_files(
request: Request,
path: str,
authorization: Optional[str] = Header(None),
):
_check_auth(authorization)
full = _resolve_path(path)
os.makedirs(os.path.dirname(full), exist_ok=True)
body = await request.body()
with open(full, "wb") as f:
f.write(body)
return {"status": "ok", "path": full, "size": len(body)}
@app.delete("/files/{path:path}")
async def delete_files(
path: str,
authorization: Optional[str] = Header(None),
):
_check_auth(authorization)
full = _resolve_path(path)
if not os.path.exists(full):
raise HTTPException(status_code=404, detail=f"not found: {path}")
if os.path.isdir(full):
raise HTTPException(status_code=400, detail="cannot delete directories")
os.remove(full)
return {"status": "ok", "path": full}
@app.get("/health")
async def health():
return {"status": "ok"}
@app.get("/status")
async def status(authorization: Optional[str] = Header(None)):
_check_auth(authorization)
return {
"busy_workspaces": list(busy_workspaces.keys()),
}
@app.post("/run/cancel")
async def cancel_run(
workspace: Optional[str] = Query(None),
authorization: Optional[str] = Header(None),
):
_check_auth(authorization)
ws = _resolve_workspace(workspace)
proc = busy_workspaces.get(ws)
if not proc:
raise HTTPException(status_code=404, detail="no running process for workspace")
proc.kill()
await proc.wait()
return {"status": "ok", "workspace": ws}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=PORT)