forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_server.py
More file actions
413 lines (339 loc) · 14.2 KB
/
Copy pathagent_server.py
File metadata and controls
413 lines (339 loc) · 14.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
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
"""
Agent Server for Claude Agent SDK (Python)
FastAPI server that hosts the Claude agent backend via AG-UI protocol.
The Next.js CopilotKit runtime proxies requests here.
Endpoints:
POST / — default shared Claude agent (sales
assistant). Used by most demos.
POST /byoc-json-render — BYOC json-render demo: emits a
single JSON spec.
POST /byoc-hashbrown — BYOC hashbrown demo: emits
hashbrown UI envelope JSON.
POST /multimodal — vision + PDF support for the
multimodal demo.
POST /agent-config — reads tone/expertise/responseLength
from ``forwarded_props`` for the
agent-config demo.
POST /shared-state-read-write — bidirectional shared state: UI
writes preferences, agent writes
notes back via ``set_notes`` tool.
POST /subagents — supervisor delegates to research /
writing / critique sub-agents,
each its own Anthropic SDK call.
POST /mcp-apps — MCP Apps demo: no bespoke tools,
forwards MCP middleware-injected
tools straight to Claude.
Each dedicated endpoint reuses the shared AG-UI <-> Anthropic streaming
plumbing in ``agents.agent`` but swaps the system prompt and/or tool set
as the demo requires.
"""
from __future__ import annotations
import os
from collections.abc import AsyncIterator, Callable
from typing import Any
# CVDIAG bootstrap — MUST be the first non-stdlib import (folded in from the
# dropped L1-H slot). Importing this module configures the root logger via
# ``logging.basicConfig`` so the ``agents._header_forwarding`` (and sibling
# ``agents.*``) CVDIAG loggers actually EMIT (fixes the silent-drop bug), and
# resolves the verbosity tier + PB writer. It imports pydantic/starlette only
# (NOT anthropic / claude-agent-sdk), so it is safe to run before the agent
# imports below.
import _shared.cvdiag_bootstrap # noqa: F401,E402 (first non-stdlib import — bootstrap side effects)
# ORDER-CRITICAL: install the global httpx hook BEFORE any agent module
# (and before the ``anthropic`` SDK) imports. The Claude SDK Python
# integration constructs ``anthropic.AsyncAnthropic`` inside ``run_agent``
# per request, but installing the hook at module-import time guarantees
# every future httpx client (including sub-agent calls) auto-attaches the
# forwarded-header hook.
from agents._cvdiag_backend import CvdiagBackendMiddleware
from agents._header_forwarding import (
HeaderForwardingHTTPMiddleware,
install_global_httpx_hook,
)
install_global_httpx_hook()
import uvicorn
from ag_ui.core import RunAgentInput
from dotenv import load_dotenv
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
from agents.a2ui_dynamic import run_a2ui_dynamic_agent
from agents.a2ui_fixed import run_a2ui_fixed_agent
from agents.agent import create_app, run_agent
from agents.agent_config_agent import build_system_prompt, read_properties
from agents.byoc_hashbrown_agent import BYOC_HASHBROWN_SYSTEM_PROMPT
from agents.byoc_json_render_agent import BYOC_JSON_RENDER_SYSTEM_PROMPT
from agents.hitl_in_chat_agent import run_hitl_in_chat_agent
from agents.interrupt_agent import run_interrupt_agent
from agents.mcp_apps_agent import run_mcp_apps_agent
from agents.multimodal_agent import SYSTEM_PROMPT as MULTIMODAL_SYSTEM_PROMPT
from agents.multimodal_agent import convert_part_for_claude
from agents.reasoning_agent import run_reasoning_agent
from agents.shared_state_read_write_agent import (
run_shared_state_read_write_agent,
)
from agents.subagents_agent import run_subagents_agent
from agents.tool_rendering_reasoning_chain_agent import (
run_tool_rendering_reasoning_chain_agent,
)
load_dotenv()
def _stream_agent_response(
input_data: RunAgentInput,
*,
system_prompt_override: str | None = None,
disable_tools: bool = False,
preprocess_user_parts: Callable[..., Any] | None = None,
) -> StreamingResponse:
"""Wrap ``run_agent`` in a StreamingResponse with demo-specific overrides.
The shared ``run_agent`` in ``agents.agent`` accepts keyword overrides
that let per-demo endpoints swap the system prompt or tool registry
without duplicating the streaming loop.
"""
async def event_stream() -> AsyncIterator[str]:
async for chunk in run_agent(
input_data,
system_prompt_override=system_prompt_override,
disable_tools=disable_tools,
preprocess_user_parts=preprocess_user_parts,
):
yield chunk
return StreamingResponse(
event_stream(),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"X-Accel-Buffering": "no",
},
)
app = create_app()
# CVDIAG backend emitter (spec §3 Layer 2) — emits the HTTP-observable backend
# boundaries (request.ingress, sse.first_byte, sse.event, sse.aborted,
# response.complete, error.caught) as structured CVDIAG envelopes. Added right
# after ``create_app`` so it is the OUTERMOST layer over the routes + CORS
# ``create_app`` installs: it observes ingress before any inner layer mutates
# the request and wraps the response stream so SSE boundaries fire as chunks
# flow. Gated behind ``CVDIAG_BACKEND_EMITTER`` (default OFF, canary-safe) — the
# middleware fast-paths to a bare pass-through when the flag is unset.
app.add_middleware(CvdiagBackendMiddleware)
# Tighten CORS: the dedicated endpoints share the same CORS policy as the
# default route, which `create_app` already opens up with `*`. No extra
# middleware needed here.
@app.get("/health")
async def health() -> dict[str, str]:
"""Liveness probe.
The Next.js runtime route at ``src/app/api/copilotkit/route.ts`` polls
``GET ${AGENT_URL}/health`` to surface backend reachability in its own
health response. Without this endpoint the probe always reports
``unreachable`` even when FastAPI is healthy.
"""
return {"status": "ok"}
@app.post("/byoc-json-render")
async def byoc_json_render_endpoint(request: Request) -> StreamingResponse:
body = await request.json()
input_data = RunAgentInput(**body)
return _stream_agent_response(
input_data,
system_prompt_override=BYOC_JSON_RENDER_SYSTEM_PROMPT,
disable_tools=True,
)
@app.post("/byoc-hashbrown")
async def byoc_hashbrown_endpoint(request: Request) -> StreamingResponse:
body = await request.json()
input_data = RunAgentInput(**body)
return _stream_agent_response(
input_data,
system_prompt_override=BYOC_HASHBROWN_SYSTEM_PROMPT,
disable_tools=True,
)
@app.post("/multimodal")
async def multimodal_endpoint(request: Request) -> StreamingResponse:
body = await request.json()
input_data = RunAgentInput(**body)
return _stream_agent_response(
input_data,
system_prompt_override=MULTIMODAL_SYSTEM_PROMPT,
disable_tools=True,
preprocess_user_parts=convert_part_for_claude,
)
@app.post("/agent-config")
async def agent_config_endpoint(request: Request) -> StreamingResponse:
body = await request.json()
input_data = RunAgentInput(**body)
props = read_properties(input_data.forwarded_props)
system_prompt = build_system_prompt(
props["tone"], props["expertise"], props["response_length"]
)
return _stream_agent_response(
input_data,
system_prompt_override=system_prompt,
disable_tools=True,
)
@app.post("/shared-state-read-write")
async def shared_state_read_write_endpoint(request: Request) -> StreamingResponse:
"""Bidirectional shared state demo — UI writes preferences, agent writes notes.
Uses its own streaming loop (not the shared sales-assistant
``run_agent``) because the state schema, tools, and prompt-injection
middleware are all demo-specific.
"""
body = await request.json()
input_data = RunAgentInput(**body)
async def event_stream() -> AsyncIterator[str]:
async for chunk in run_shared_state_read_write_agent(input_data):
yield chunk
return StreamingResponse(
event_stream(),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"X-Accel-Buffering": "no",
},
)
@app.post("/reasoning")
async def reasoning_endpoint(request: Request) -> StreamingResponse:
"""Reasoning demo backend — emits AG-UI REASONING_MESSAGE_* events.
Shared by the reasoning-custom and reasoning-default
demos. Both demos hit the same backend; the difference is purely
on the frontend slot configuration.
"""
body = await request.json()
input_data = RunAgentInput(**body)
async def event_stream() -> AsyncIterator[str]:
async for chunk in run_reasoning_agent(input_data):
yield chunk
return StreamingResponse(
event_stream(),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"X-Accel-Buffering": "no",
},
)
@app.post("/tool-rendering-reasoning-chain")
async def tool_rendering_reasoning_chain_endpoint(
request: Request,
) -> StreamingResponse:
"""Sequential tool calls + visible reasoning chain."""
body = await request.json()
input_data = RunAgentInput(**body)
async def event_stream() -> AsyncIterator[str]:
async for chunk in run_tool_rendering_reasoning_chain_agent(input_data):
yield chunk
return StreamingResponse(
event_stream(),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"X-Accel-Buffering": "no",
},
)
@app.post("/mcp-apps")
async def mcp_apps_endpoint(request: Request) -> StreamingResponse:
"""MCP Apps demo — pass-through tools forwarded by the runtime middleware.
The dedicated runtime at ``/api/copilotkit-mcp-apps`` configures
``mcpApps: { servers: [...] }``, which auto-applies the MCP Apps
middleware to the agent. The middleware appends the remote MCP
server's tools to the AG-UI request's ``tools`` array; this endpoint
forwards them straight to Claude.
"""
body = await request.json()
input_data = RunAgentInput(**body)
async def event_stream() -> AsyncIterator[str]:
async for chunk in run_mcp_apps_agent(input_data):
yield chunk
return StreamingResponse(
event_stream(),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"X-Accel-Buffering": "no",
},
)
@app.post("/subagents")
async def subagents_endpoint(request: Request) -> StreamingResponse:
"""Sub-agents demo — supervisor delegates to research/writing/critique."""
body = await request.json()
input_data = RunAgentInput(**body)
async def event_stream() -> AsyncIterator[str]:
async for chunk in run_subagents_agent(input_data):
yield chunk
return StreamingResponse(
event_stream(),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"X-Accel-Buffering": "no",
},
)
@app.post("/hitl-in-chat")
async def hitl_in_chat_endpoint(request: Request) -> StreamingResponse:
"""In-Chat HITL demo — frontend `book_call` tool via `useHumanInTheLoop`."""
body = await request.json()
input_data = RunAgentInput(**body)
async def event_stream() -> AsyncIterator[str]:
async for chunk in run_hitl_in_chat_agent(input_data):
yield chunk
return StreamingResponse(
event_stream(),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"X-Accel-Buffering": "no",
},
)
@app.post("/interrupt-adapted")
async def interrupt_adapted_endpoint(request: Request) -> StreamingResponse:
"""Interrupt-adapted scheduling agent — shared by gen-ui-interrupt and
interrupt-headless. The ``schedule_meeting`` tool is registered on the
frontend via ``useFrontendTool``; the backend only provides the system
prompt and forwards frontend tools to Claude."""
body = await request.json()
input_data = RunAgentInput(**body)
async def event_stream() -> AsyncIterator[str]:
async for chunk in run_interrupt_agent(input_data):
yield chunk
return StreamingResponse(
event_stream(),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"X-Accel-Buffering": "no",
},
)
@app.post("/declarative-gen-ui")
async def declarative_gen_ui_endpoint(request: Request) -> StreamingResponse:
"""Declarative Generative UI (A2UI Dynamic Schema) demo."""
body = await request.json()
input_data = RunAgentInput(**body)
async def event_stream() -> AsyncIterator[str]:
async for chunk in run_a2ui_dynamic_agent(input_data):
yield chunk
return StreamingResponse(
event_stream(),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"X-Accel-Buffering": "no",
},
)
@app.post("/a2ui-fixed-schema")
async def a2ui_fixed_schema_endpoint(request: Request) -> StreamingResponse:
"""A2UI Fixed Schema demo — backend ships flight_schema.json."""
body = await request.json()
input_data = RunAgentInput(**body)
async def event_stream() -> AsyncIterator[str]:
async for chunk in run_a2ui_fixed_agent(input_data):
yield chunk
return StreamingResponse(
event_stream(),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"X-Accel-Buffering": "no",
},
)
def main() -> None:
"""Run the uvicorn server."""
port = int(os.getenv("AGENT_PORT", "8000"))
uvicorn.run("agent_server:app", host="0.0.0.0", port=port, reload=True)
if __name__ == "__main__":
main()