{"version":3,"file":"hono.mjs","names":[],"sources":["../../../../src/v2/runtime/endpoints/hono.ts"],"sourcesContent":["import { Hono } from \"hono\";\nimport type { CopilotRuntimeLike } from \"../core/runtime\";\nimport { createCopilotRuntimeHandler } from \"../core/fetch-handler\";\nimport type { CopilotCorsConfig } from \"../core/fetch-cors\";\nimport type { CopilotRuntimeHooks } from \"../core/hooks\";\nimport type {\n  ActivateChannelEngine,\n  ChannelsControl,\n} from \"../core/channel-manager\";\n\n/**\n * A Hono app that may also carry an optional {@link ChannelsControl} surface.\n * Hono's app object is request-scoped routing config, not a long-running\n * process owner — Node (`createCopilotNodeListener`) is the lifecycle-owning\n * surface for `.channels`. It is attached here too, best-effort, for callers\n * that mount the Hono app directly and want to observe/stop managed Channel\n * activation without also standing up a Node listener.\n */\nexport type CopilotHonoApp = Hono & { channels?: ChannelsControl };\n\n/**\n * CORS configuration for CopilotKit endpoints.\n * When using credentials (e.g., HTTP-only cookies), you must specify an explicit origin.\n */\nexport interface CopilotEndpointCorsConfig {\n  /**\n   * Allowed origin(s) for CORS. Can be:\n   * - A string: exact origin (e.g., \"https://myapp.com\")\n   * - An array: list of allowed origins\n   * - A function: dynamic origin resolution\n   *\n   * Note: When credentials is true, origin cannot be \"*\"\n   */\n  origin:\n    | string\n    | string[]\n    | ((origin: string, c: any) => string | undefined | null);\n  /**\n   * Whether to allow credentials (cookies, HTTP authentication).\n   * When true, origin must be explicitly specified (not \"*\").\n   */\n  credentials?: boolean;\n}\n\ninterface CopilotEndpointParams {\n  runtime: CopilotRuntimeLike;\n  basePath: string;\n\n  /**\n   * Endpoint mode.\n   * - `\"multi-route\"` (default): separate routes for each operation\n   * - `\"single-route\"`: single POST endpoint with JSON envelope dispatch\n   */\n  mode?: \"multi-route\" | \"single-route\";\n\n  /**\n   * Optional CORS configuration. When not provided, defaults to allowing all origins without credentials.\n   * To support HTTP-only cookies, provide cors config with credentials: true and explicit origin.\n   */\n  cors?: CopilotEndpointCorsConfig;\n  /**\n   * Lifecycle hooks for request processing.\n   */\n  hooks?: CopilotRuntimeHooks;\n\n  /**\n   * Whether the underlying handler activates the runtime's declared managed\n   * Channels at creation time. Defaults to `true`. See\n   * `CopilotRuntimeHandlerOptions.activateChannels`.\n   */\n  activateChannels?: boolean;\n\n  /**\n   * @internal Test seam: inject a fake Channel activation engine. Forwarded\n   * to `createCopilotRuntimeHandler`. Not part of the public API.\n   */\n  __channelEngine?: ActivateChannelEngine;\n}\n/** @deprecated Use `createCopilotHonoHandler` instead. */\nexport const createCopilotEndpoint = createCopilotHonoHandler;\n\nexport function createCopilotHonoHandler({\n  runtime,\n  basePath,\n  mode = \"multi-route\",\n  cors: corsConfig,\n  hooks,\n  activateChannels,\n  __channelEngine,\n}: CopilotEndpointParams): CopilotHonoApp {\n  const handler = createCopilotRuntimeHandler({\n    runtime,\n    basePath,\n    mode,\n    cors: corsConfig ? toFetchCorsConfig(corsConfig) : true,\n    hooks,\n    activateChannels,\n    __channelEngine,\n  });\n\n  const app = new Hono();\n\n  const scopedApp: CopilotHonoApp = app\n    .basePath(basePath)\n    .all(\"*\", async (c) => handler(c.req.raw));\n  scopedApp.channels = handler.channels;\n  return scopedApp;\n}\n\n/**\n * Convert Hono-specific CORS config to the fetch handler's CopilotCorsConfig.\n */\nexport function toFetchCorsConfig(\n  config: CopilotEndpointCorsConfig,\n): CopilotCorsConfig {\n  const origin = config.origin;\n  return {\n    origin:\n      typeof origin === \"function\"\n        ? (reqOrigin: string) => origin(reqOrigin, undefined) ?? null\n        : origin,\n    credentials: config.credentials,\n  };\n}\n"],"mappings":";;;;;;AA+EA,MAAa,wBAAwB;AAErC,SAAgB,yBAAyB,EACvC,SACA,UACA,OAAO,eACP,MAAM,YACN,OACA,kBACA,mBACwC;CACxC,MAAM,UAAU,4BAA4B;EAC1C;EACA;EACA;EACA,MAAM,aAAa,kBAAkB,WAAW,GAAG;EACnD;EACA;EACA;EACD,CAAC;CAIF,MAAM,YAFM,IAAI,MAAM,CAGnB,SAAS,SAAS,CAClB,IAAI,KAAK,OAAO,MAAM,QAAQ,EAAE,IAAI,IAAI,CAAC;AAC5C,WAAU,WAAW,QAAQ;AAC7B,QAAO;;;;;AAMT,SAAgB,kBACd,QACmB;CACnB,MAAM,SAAS,OAAO;AACtB,QAAO;EACL,QACE,OAAO,WAAW,cACb,cAAsB,OAAO,WAAW,OAAU,IAAI,OACvD;EACN,aAAa,OAAO;EACrB"}