{"version":3,"file":"runtime.mjs","names":["pkg"],"sources":["../../../../src/v2/runtime/core/runtime.ts"],"sourcesContent":["import {\n  MaybePromise,\n  NonEmptyRecord,\n  RuntimeMode,\n  RUNTIME_MODE_SSE,\n  RUNTIME_MODE_INTELLIGENCE,\n} from \"@copilotkit/shared\";\nimport {\n  createLicenseChecker,\n  type LicenseChecker,\n} from \"@copilotkit/license-verifier\";\nimport { AbstractAgent } from \"@ag-ui/client\";\nimport type { MCPClientConfig } from \"@ag-ui/mcp-apps-middleware\";\nimport { A2UIMiddlewareConfig } from \"@ag-ui/a2ui-middleware\";\nimport pkg from \"../../../../package.json\";\nimport type {\n  BeforeRequestMiddleware,\n  AfterRequestMiddleware,\n} from \"./middleware\";\nimport { TranscriptionService } from \"../transcription-service/transcription-service\";\nimport { AgentRunner } from \"../runner/agent-runner\";\nimport { InMemoryAgentRunner } from \"../runner/in-memory\";\nimport { IntelligenceAgentRunner } from \"../runner/intelligence\";\nimport { CopilotKitIntelligence } from \"../intelligence-platform\";\n\nexport const VERSION = pkg.version;\n\ninterface BaseCopilotRuntimeMiddlewareOptions {\n  /** If set, middleware only applies to these named agents. Applies to all agents if omitted. */\n  agents?: string[];\n}\n\nexport type McpAppsServerConfig = MCPClientConfig & {\n  /** Agent to bind this server to. If omitted, the server is available to all agents. */\n  agentId?: string;\n};\n\nexport interface McpAppsConfig {\n  /** List of MCP server configurations. */\n  servers: McpAppsServerConfig[];\n}\n\nexport interface OpenGenerativeUIOptions extends BaseCopilotRuntimeMiddlewareOptions {}\n\nexport type OpenGenerativeUIConfig = boolean | OpenGenerativeUIOptions;\n\ninterface CopilotRuntimeMiddlewares {\n  /**\n   * Auto-apply A2UIMiddleware to agents at run time.\n   * Pass an object to enable and customise behaviour, or omit to disable.\n   */\n  a2ui?: BaseCopilotRuntimeMiddlewareOptions & A2UIMiddlewareConfig;\n  /** Auto-apply MCPAppsMiddleware to agents at run time. */\n  mcpApps?: McpAppsConfig;\n  /** Auto-apply OpenGenerativeUIMiddleware to agents at run time. */\n  openGenerativeUI?: OpenGenerativeUIConfig;\n}\n\ninterface BaseCopilotRuntimeOptions extends CopilotRuntimeMiddlewares {\n  /** Map of available agents (loaded lazily is fine). */\n  agents: MaybePromise<NonEmptyRecord<Record<string, AbstractAgent>>>;\n  /** Optional transcription service for audio processing. */\n  transcriptionService?: TranscriptionService;\n  /** Optional *before* middleware – callback function or webhook URL. */\n  beforeRequestMiddleware?: BeforeRequestMiddleware;\n  /** Optional *after* middleware – callback function or webhook URL. */\n  afterRequestMiddleware?: AfterRequestMiddleware;\n  /** Signed license token for server-side feature verification. Falls back to COPILOTKIT_LICENSE_TOKEN env var. */\n  licenseToken?: string;\n}\n\nexport interface CopilotRuntimeUser {\n  id: string;\n}\n\nexport type IdentifyUserCallback = (\n  request: Request,\n) => MaybePromise<CopilotRuntimeUser>;\n\nexport interface CopilotSseRuntimeOptions extends BaseCopilotRuntimeOptions {\n  /** The runner to use for running agents in SSE mode. */\n  runner?: AgentRunner;\n  intelligence?: undefined;\n  generateThreadNames?: undefined;\n}\n\nexport interface CopilotIntelligenceRuntimeOptions extends BaseCopilotRuntimeOptions {\n  /** Configures Intelligence mode for durable threads and realtime events. */\n  intelligence: CopilotKitIntelligence;\n  /** Resolves the authenticated user for intelligence requests. */\n  identifyUser: IdentifyUserCallback;\n  /** Auto-generate short names for newly created threads. */\n  generateThreadNames?: boolean;\n  /** Max delay (ms) for WebSocket reconnect backoff. @default 10_000 */\n  maxReconnectMs?: number;\n  /** Max delay (ms) for channel rejoin backoff. @default 30_000 */\n  maxRejoinMs?: number;\n  /** Lock TTL in seconds. Clamped to a maximum of 3600 (1 hour). @default 20 */\n  lockTtlSeconds?: number;\n  /** Custom Redis key prefix for the thread lock. */\n  lockKeyPrefix?: string;\n  /** Interval in seconds at which the runtime renews the thread lock. Clamped to a maximum of 3000 (50 minutes). @default 15 */\n  lockHeartbeatIntervalSeconds?: number;\n}\n\nexport type CopilotRuntimeOptions =\n  | CopilotSseRuntimeOptions\n  | CopilotIntelligenceRuntimeOptions;\n\nexport interface CopilotRuntimeLike {\n  agents: CopilotRuntimeOptions[\"agents\"];\n  transcriptionService: CopilotRuntimeOptions[\"transcriptionService\"];\n  beforeRequestMiddleware: CopilotRuntimeOptions[\"beforeRequestMiddleware\"];\n  afterRequestMiddleware: CopilotRuntimeOptions[\"afterRequestMiddleware\"];\n  runner: AgentRunner;\n  a2ui: CopilotRuntimeOptions[\"a2ui\"];\n  mcpApps: CopilotRuntimeOptions[\"mcpApps\"];\n  openGenerativeUI: CopilotRuntimeOptions[\"openGenerativeUI\"];\n  intelligence?: CopilotKitIntelligence;\n  identifyUser?: IdentifyUserCallback;\n  mode: RuntimeMode;\n  licenseChecker?: LicenseChecker;\n}\n\nexport interface CopilotSseRuntimeLike extends CopilotRuntimeLike {\n  intelligence?: undefined;\n  mode: RUNTIME_MODE_SSE;\n}\n\nexport interface CopilotIntelligenceRuntimeLike extends CopilotRuntimeLike {\n  intelligence: CopilotKitIntelligence;\n  identifyUser: IdentifyUserCallback;\n  generateThreadNames: boolean;\n  lockTtlSeconds: number;\n  lockKeyPrefix?: string;\n  lockHeartbeatIntervalSeconds: number;\n  mode: RUNTIME_MODE_INTELLIGENCE;\n}\n\nabstract class BaseCopilotRuntime implements CopilotRuntimeLike {\n  public agents: CopilotRuntimeOptions[\"agents\"];\n  public transcriptionService: CopilotRuntimeOptions[\"transcriptionService\"];\n  public beforeRequestMiddleware: CopilotRuntimeOptions[\"beforeRequestMiddleware\"];\n  public afterRequestMiddleware: CopilotRuntimeOptions[\"afterRequestMiddleware\"];\n  public runner: AgentRunner;\n  public a2ui: CopilotRuntimeOptions[\"a2ui\"];\n  public mcpApps: CopilotRuntimeOptions[\"mcpApps\"];\n  public openGenerativeUI: CopilotRuntimeOptions[\"openGenerativeUI\"];\n  public licenseChecker?: LicenseChecker;\n\n  abstract readonly intelligence?: CopilotKitIntelligence;\n  abstract readonly mode: RuntimeMode;\n\n  constructor(options: BaseCopilotRuntimeOptions, runner: AgentRunner) {\n    const {\n      agents,\n      transcriptionService,\n      beforeRequestMiddleware,\n      afterRequestMiddleware,\n      a2ui,\n      mcpApps,\n      openGenerativeUI,\n    } = options;\n\n    this.agents = agents;\n    this.transcriptionService = transcriptionService;\n    this.beforeRequestMiddleware = beforeRequestMiddleware;\n    this.afterRequestMiddleware = afterRequestMiddleware;\n    this.a2ui = a2ui || undefined;\n    this.mcpApps = mcpApps;\n    this.openGenerativeUI = openGenerativeUI;\n    this.runner = runner;\n  }\n}\n\nexport class CopilotSseRuntime\n  extends BaseCopilotRuntime\n  implements CopilotSseRuntimeLike\n{\n  readonly intelligence = undefined;\n  readonly mode = RUNTIME_MODE_SSE;\n\n  constructor(options: CopilotSseRuntimeOptions) {\n    super(options, options.runner ?? new InMemoryAgentRunner());\n  }\n}\n\nexport class CopilotIntelligenceRuntime\n  extends BaseCopilotRuntime\n  implements CopilotIntelligenceRuntimeLike\n{\n  readonly intelligence: CopilotKitIntelligence;\n  readonly identifyUser: IdentifyUserCallback;\n  readonly generateThreadNames: boolean;\n  readonly lockTtlSeconds: number;\n  readonly lockKeyPrefix?: string;\n  readonly lockHeartbeatIntervalSeconds: number;\n  readonly mode = RUNTIME_MODE_INTELLIGENCE;\n\n  /** Maximum allowed lock TTL in seconds (1 hour). */\n  static readonly MAX_LOCK_TTL_SECONDS = 3_600;\n  /** Maximum allowed heartbeat interval in seconds (50 minutes). */\n  static readonly MAX_HEARTBEAT_INTERVAL_SECONDS = 3_000;\n\n  constructor(options: CopilotIntelligenceRuntimeOptions) {\n    super(\n      options,\n      new IntelligenceAgentRunner({\n        url: options.intelligence.ɵgetRunnerWsUrl(),\n        authToken: options.intelligence.ɵgetRunnerAuthToken(),\n        maxReconnectMs: options.maxReconnectMs,\n        maxRejoinMs: options.maxRejoinMs,\n      }),\n    );\n    this.intelligence = options.intelligence;\n    this.identifyUser = options.identifyUser;\n    this.generateThreadNames = options.generateThreadNames ?? true;\n    this.licenseChecker = createLicenseChecker(options.licenseToken);\n    this.lockTtlSeconds = Math.min(\n      options.lockTtlSeconds ?? 20,\n      CopilotIntelligenceRuntime.MAX_LOCK_TTL_SECONDS,\n    );\n    this.lockKeyPrefix = options.lockKeyPrefix;\n    this.lockHeartbeatIntervalSeconds = Math.min(\n      options.lockHeartbeatIntervalSeconds ?? 15,\n      CopilotIntelligenceRuntime.MAX_HEARTBEAT_INTERVAL_SECONDS,\n    );\n  }\n}\n\nfunction hasIntelligenceOptions(\n  options: CopilotRuntimeOptions,\n): options is CopilotIntelligenceRuntimeOptions {\n  return \"intelligence\" in options && !!options.intelligence;\n}\n\nexport function isIntelligenceRuntime(\n  runtime: CopilotRuntimeLike,\n): runtime is CopilotIntelligenceRuntimeLike {\n  return runtime.mode === RUNTIME_MODE_INTELLIGENCE && !!runtime.intelligence;\n}\n\n/**\n * Compatibility shim that preserves the legacy `CopilotRuntime` entrypoint.\n * New code should prefer `CopilotSseRuntime` or `CopilotIntelligenceRuntime`.\n */\nexport class CopilotRuntime implements CopilotRuntimeLike {\n  private delegate: CopilotRuntimeLike;\n\n  constructor(options: CopilotRuntimeOptions) {\n    this.delegate = hasIntelligenceOptions(options)\n      ? new CopilotIntelligenceRuntime(options)\n      : new CopilotSseRuntime(options);\n  }\n\n  get agents(): CopilotRuntimeOptions[\"agents\"] {\n    return this.delegate.agents;\n  }\n\n  get transcriptionService(): CopilotRuntimeOptions[\"transcriptionService\"] {\n    return this.delegate.transcriptionService;\n  }\n\n  get beforeRequestMiddleware(): CopilotRuntimeOptions[\"beforeRequestMiddleware\"] {\n    return this.delegate.beforeRequestMiddleware;\n  }\n\n  get afterRequestMiddleware(): CopilotRuntimeOptions[\"afterRequestMiddleware\"] {\n    return this.delegate.afterRequestMiddleware;\n  }\n\n  get runner(): AgentRunner {\n    return this.delegate.runner;\n  }\n\n  get a2ui(): CopilotRuntimeOptions[\"a2ui\"] {\n    return this.delegate.a2ui;\n  }\n\n  get mcpApps(): CopilotRuntimeOptions[\"mcpApps\"] {\n    return this.delegate.mcpApps;\n  }\n\n  get openGenerativeUI(): CopilotRuntimeOptions[\"openGenerativeUI\"] {\n    return this.delegate.openGenerativeUI;\n  }\n\n  get intelligence(): CopilotKitIntelligence | undefined {\n    return this.delegate.intelligence;\n  }\n\n  get generateThreadNames(): boolean | undefined {\n    return isIntelligenceRuntime(this.delegate)\n      ? this.delegate.generateThreadNames\n      : undefined;\n  }\n\n  get identifyUser(): IdentifyUserCallback | undefined {\n    return isIntelligenceRuntime(this.delegate)\n      ? this.delegate.identifyUser\n      : undefined;\n  }\n\n  get lockTtlSeconds(): number | undefined {\n    return isIntelligenceRuntime(this.delegate)\n      ? this.delegate.lockTtlSeconds\n      : undefined;\n  }\n\n  get lockKeyPrefix(): string | undefined {\n    return isIntelligenceRuntime(this.delegate)\n      ? this.delegate.lockKeyPrefix\n      : undefined;\n  }\n\n  get lockHeartbeatIntervalSeconds(): number | undefined {\n    return isIntelligenceRuntime(this.delegate)\n      ? this.delegate.lockHeartbeatIntervalSeconds\n      : undefined;\n  }\n\n  get mode(): RuntimeMode {\n    return this.delegate.mode;\n  }\n\n  get licenseChecker() {\n    return this.delegate.licenseChecker;\n  }\n}\n"],"mappings":";;;;;;;;;;AAyBA,MAAa,UAAUA,uBAAI;AAkH3B,IAAe,qBAAf,MAAgE;CAc9D,YAAY,SAAoC,QAAqB;EACnE,MAAM,EACJ,QACA,sBACA,yBACA,wBACA,MACA,SACA,qBACE;AAEJ,OAAK,SAAS;AACd,OAAK,uBAAuB;AAC5B,OAAK,0BAA0B;AAC/B,OAAK,yBAAyB;AAC9B,OAAK,OAAO,QAAQ;AACpB,OAAK,UAAU;AACf,OAAK,mBAAmB;AACxB,OAAK,SAAS;;;AAIlB,IAAa,oBAAb,cACU,mBAEV;CAIE,YAAY,SAAmC;AAC7C,QAAM,SAAS,QAAQ,UAAU,IAAI,qBAAqB,CAAC;sBAJrC;cACR;;;AAOlB,IAAa,6BAAb,MAAa,mCACH,mBAEV;;8BAUyC;;;wC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