{"version":3,"file":"connect.mjs","names":[],"sources":["../../../../../src/v2/runtime/handlers/sse/connect.ts"],"sourcesContent":["import type { AbstractAgent } from \"@ag-ui/client\";\nimport type { CopilotRuntimeLike } from \"../../core/runtime\";\nimport { createSseEventResponse } from \"../shared/sse-response\";\nimport {\n  mergeForwardableHeaders,\n  resolveForwardHeadersPolicy,\n} from \"../header-utils\";\n\n/**\n * `headers` lives on the HTTP-backed agent configs (e.g. `HttpAgent`), not on\n * the base `AbstractAgent`. Mirror the runtime's own optional-headers shape so\n * we can read server-configured headers off the per-request clone without a\n * cast. See `agent-utils.ts`.\n */\ntype AgentWithHeaders = AbstractAgent & {\n  headers?: Record<string, string>;\n};\n\ninterface HandleSseConnectParams {\n  runtime: CopilotRuntimeLike;\n  request: Request;\n  agentId: string;\n  threadId: string;\n  /**\n   * The per-request agent clone, carrying any server-configured `agent.headers`\n   * (e.g. service-to-service auth). Used only to compute the merged header set\n   * threaded into `runner.connect` below — see the note there for why that\n   * merge is forward-looking plumbing rather than active outbound auth today.\n   */\n  agent?: AgentWithHeaders;\n}\n\nexport function handleSseConnect({\n  runtime,\n  request,\n  agentId,\n  threadId,\n  agent,\n}: HandleSseConnectParams): Response {\n  return createSseEventResponse({\n    request,\n    debugEventBus: runtime.debugEventBus,\n    // Forward the real agentId so debug envelopes reflect the agent the\n    // route resolved to — not the literal string \"connect\".\n    agentId,\n    observableFactory: () =>\n      runtime.runner.connect({\n        threadId,\n        // Forward-looking plumbing: we compute the merged header set (server\n        // `agent.headers` win on collision, case-insensitively; non-colliding\n        // inbound headers still forward — see `mergeForwardableHeaders`, #5712)\n        // and thread it into `runner.connect`. NO shipped runner consumes the\n        // `headers` field of `AgentRunnerConnectRequest` today — every runner\n        // (in-memory, intelligence, telemetry, sqlite) reads only `threadId`.\n        // The real outbound header forwarding is the /run path, where\n        // `cloneAgentForRequest` mutates `agent.headers` directly\n        // (agent-utils.ts). This wiring exists so a future outbound-connecting\n        // runner can pick the merged headers up without a route change; the\n        // collision precedence noted here is purely about that merge, not about\n        // middleware/mutation parity with /run.\n        headers: mergeForwardableHeaders(\n          agent?.headers,\n          request,\n          // Optional on `CopilotRuntimeLike` (non-breaking minor release);\n          // coalesce a policy-less external implementor to the default resolved\n          // policy (default-on denylist) instead of dereffing undefined.\n          runtime.forwardHeadersPolicy ??\n            resolveForwardHeadersPolicy(undefined),\n        ),\n      }),\n  });\n}\n"],"mappings":";;;;;AAgCA,SAAgB,iBAAiB,EAC/B,SACA,SACA,SACA,UACA,SACmC;AACnC,QAAO,uBAAuB;EAC5B;EACA,eAAe,QAAQ;EAGvB;EACA,yBACE,QAAQ,OAAO,QAAQ;GACrB;GAaA,SAAS,wBACP,OAAO,SACP,SAIA,QAAQ,wBACN,4BAA4B,OAAU,CACzC;GACF,CAAC;EACL,CAAC"}