UNPKG

@copilotkit/runtime

Version:

<img src="https://github.com/user-attachments/assets/0a6b64d9-e193-4940-a3f6-60334ac34084" alt="banner" style="border-radius: 12px; border: 2px solid #d6d4fa;" />

1 lines 2.5 kB
{"version":3,"file":"handle-debug-events.cjs","names":[],"sources":["../../../../src/v2/runtime/handlers/handle-debug-events.ts"],"sourcesContent":["import { CopilotRuntimeLike } from \"../core/runtime\";\nimport { DebugEventEnvelope } from \"@copilotkit/shared\";\n\ninterface HandleDebugEventsParams {\n runtime: CopilotRuntimeLike;\n request: Request;\n}\n\nexport function handleDebugEvents({\n runtime,\n request,\n}: HandleDebugEventsParams): Response {\n if (process.env.NODE_ENV === \"production\") {\n return new Response(\"Not Found\", { status: 404 });\n }\n\n if (!runtime.debugEventBus) {\n return new Response(\"Debug event bus not available\", { status: 503 });\n }\n\n const bus = runtime.debugEventBus;\n const encoder = new TextEncoder();\n const stream = new TransformStream();\n const writer = stream.writable.getWriter();\n\n // Send an SSE comment immediately to flush response headers to the client.\n // Without this, some frameworks buffer the response until actual data is written,\n // leaving the client stuck in \"connecting\" state.\n writer.write(encoder.encode(\": connected\\n\\n\")).catch(() => {});\n\n const unsubscribe = bus.subscribe((envelope: DebugEventEnvelope) => {\n if (request.signal.aborted) return;\n const line = `data: ${JSON.stringify(envelope)}\\n\\n`;\n writer.write(encoder.encode(line)).catch(() => {\n // Client disconnected, will be cleaned up by abort handler.\n });\n });\n\n request.signal.addEventListener(\"abort\", () => {\n unsubscribe();\n writer.close().catch(() => {});\n });\n\n return new Response(stream.readable, {\n status: 200,\n headers: {\n \"Content-Type\": \"text/event-stream\",\n \"Cache-Control\": \"no-cache\",\n Connection: \"keep-alive\",\n },\n });\n}\n"],"mappings":";;;AAQA,SAAgB,kBAAkB,EAChC,SACA,WACoC;AACpC,KAAI,QAAQ,IAAI,aAAa,aAC3B,QAAO,IAAI,SAAS,aAAa,EAAE,QAAQ,KAAK,CAAC;AAGnD,KAAI,CAAC,QAAQ,cACX,QAAO,IAAI,SAAS,iCAAiC,EAAE,QAAQ,KAAK,CAAC;CAGvE,MAAM,MAAM,QAAQ;CACpB,MAAM,UAAU,IAAI,aAAa;CACjC,MAAM,SAAS,IAAI,iBAAiB;CACpC,MAAM,SAAS,OAAO,SAAS,WAAW;AAK1C,QAAO,MAAM,QAAQ,OAAO,kBAAkB,CAAC,CAAC,YAAY,GAAG;CAE/D,MAAM,cAAc,IAAI,WAAW,aAAiC;AAClE,MAAI,QAAQ,OAAO,QAAS;EAC5B,MAAM,OAAO,SAAS,KAAK,UAAU,SAAS,CAAC;AAC/C,SAAO,MAAM,QAAQ,OAAO,KAAK,CAAC,CAAC,YAAY,GAE7C;GACF;AAEF,SAAQ,OAAO,iBAAiB,eAAe;AAC7C,eAAa;AACb,SAAO,OAAO,CAAC,YAAY,GAAG;GAC9B;AAEF,QAAO,IAAI,SAAS,OAAO,UAAU;EACnC,QAAQ;EACR,SAAS;GACP,gBAAgB;GAChB,iBAAiB;GACjB,YAAY;GACb;EACF,CAAC"}