Handle FastAPI request
(request: Request, sdk: CopilotKitRemoteEndpoint)
| 75 | |
| 76 | |
| 77 | async def handler(request: Request, sdk: CopilotKitRemoteEndpoint): |
| 78 | """Handle FastAPI request""" |
| 79 | |
| 80 | try: |
| 81 | body = await request.json() |
| 82 | except: # pylint: disable=bare-except |
| 83 | body = None |
| 84 | |
| 85 | # Propagate x-aimock-* headers to outgoing LLM calls via ContextVar |
| 86 | set_forwarded_headers(dict(request.headers)) |
| 87 | |
| 88 | path = request.path_params.get("path") |
| 89 | method = request.method |
| 90 | context = cast( |
| 91 | CopilotKitContext, |
| 92 | { |
| 93 | "properties": (body or {}).get("properties", {}), |
| 94 | "frontend_url": (body or {}).get("frontendUrl", None), |
| 95 | "headers": request.headers, |
| 96 | }, |
| 97 | ) |
| 98 | |
| 99 | # handle / request for info endpoint |
| 100 | if method in ["GET", "POST"] and path == "": |
| 101 | accept_header = request.headers.get("accept", "") |
| 102 | return await handle_info( |
| 103 | sdk=sdk, |
| 104 | context=context, |
| 105 | as_html="text/html" in accept_header, |
| 106 | ) |
| 107 | |
| 108 | # handle /agent/name request for executing an agent |
| 109 | if method == "POST" and (match := re.match(r"agent/([a-zA-Z0-9_-]+)", path)): |
| 110 | name = match.group(1) |
| 111 | body = body or {} |
| 112 | |
| 113 | thread_id = body.get("threadId", str(uuid.uuid4())) |
| 114 | state = body.get("state", {}) |
| 115 | messages = body.get("messages", []) |
| 116 | actions = body.get("actions", []) |
| 117 | |
| 118 | # used for LangGraph only |
| 119 | node_name = body.get("nodeName") |
| 120 | |
| 121 | return handle_execute_agent( |
| 122 | sdk=sdk, |
| 123 | context=context, |
| 124 | thread_id=thread_id, |
| 125 | node_name=node_name, |
| 126 | name=name, |
| 127 | state=state, |
| 128 | messages=messages, |
| 129 | actions=actions, |
| 130 | ) |
| 131 | |
| 132 | # handle /agent/name/state request for getting agent state |
| 133 | if method == "POST" and (match := re.match(r"agent/([a-zA-Z0-9_-]+)/state", path)): |
| 134 | name = match.group(1) |
no test coverage detected
searching dependent graphs…