@copilotkit/runtime 1.55.3 → 1.56.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent/converters/tanstack.cjs.map +1 -1
- package/dist/agent/converters/tanstack.d.cts +6 -19
- package/dist/agent/converters/tanstack.d.cts.map +1 -1
- package/dist/agent/converters/tanstack.d.mts +6 -19
- package/dist/agent/converters/tanstack.d.mts.map +1 -1
- package/dist/agent/converters/tanstack.mjs.map +1 -1
- package/dist/agent/index.cjs +14 -0
- package/dist/agent/index.cjs.map +1 -1
- package/dist/agent/index.d.cts +12 -1
- package/dist/agent/index.d.cts.map +1 -1
- package/dist/agent/index.d.mts +12 -1
- package/dist/agent/index.d.mts.map +1 -1
- package/dist/agent/index.mjs +14 -0
- package/dist/agent/index.mjs.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +3 -2
- package/dist/index.d.mts +3 -2
- package/dist/index.mjs +1 -1
- package/dist/lib/index.cjs +1 -1
- package/dist/lib/index.d.cts +2 -1
- package/dist/lib/index.d.cts.map +1 -1
- package/dist/lib/index.d.mts +2 -1
- package/dist/lib/index.d.mts.map +1 -1
- package/dist/lib/index.mjs +1 -1
- package/dist/lib/integrations/shared.cjs +1 -1
- package/dist/lib/integrations/shared.d.cts +1 -1
- package/dist/lib/integrations/shared.d.mts +1 -1
- package/dist/lib/integrations/shared.mjs +1 -1
- package/dist/lib/runtime/copilot-runtime.cjs +14 -4
- package/dist/lib/runtime/copilot-runtime.cjs.map +1 -1
- package/dist/lib/runtime/copilot-runtime.d.cts +15 -3
- package/dist/lib/runtime/copilot-runtime.d.cts.map +1 -1
- package/dist/lib/runtime/copilot-runtime.d.mts +15 -3
- package/dist/lib/runtime/copilot-runtime.d.mts.map +1 -1
- package/dist/lib/runtime/copilot-runtime.mjs +14 -4
- package/dist/lib/runtime/copilot-runtime.mjs.map +1 -1
- package/dist/package.cjs +6 -5
- package/dist/package.mjs +6 -5
- package/dist/service-adapters/openai/openai-adapter.cjs +1 -1
- package/dist/service-adapters/openai/openai-adapter.cjs.map +1 -1
- package/dist/service-adapters/openai/openai-adapter.d.cts.map +1 -1
- package/dist/service-adapters/openai/openai-adapter.d.mts.map +1 -1
- package/dist/service-adapters/openai/openai-adapter.mjs +2 -2
- package/dist/service-adapters/openai/openai-adapter.mjs.map +1 -1
- package/dist/service-adapters/openai/openai-assistant-adapter.cjs +8 -9
- package/dist/service-adapters/openai/openai-assistant-adapter.cjs.map +1 -1
- package/dist/service-adapters/openai/openai-assistant-adapter.d.cts.map +1 -1
- package/dist/service-adapters/openai/openai-assistant-adapter.d.mts.map +1 -1
- package/dist/service-adapters/openai/openai-assistant-adapter.mjs +9 -10
- package/dist/service-adapters/openai/openai-assistant-adapter.mjs.map +1 -1
- package/dist/service-adapters/openai/utils.cjs +53 -0
- package/dist/service-adapters/openai/utils.cjs.map +1 -1
- package/dist/service-adapters/openai/utils.mjs +51 -1
- package/dist/service-adapters/openai/utils.mjs.map +1 -1
- package/dist/v2/index.cjs +1 -0
- package/dist/v2/index.d.cts +3 -3
- package/dist/v2/index.d.mts +3 -3
- package/dist/v2/index.mjs +2 -2
- package/dist/v2/runtime/core/runtime.cjs +25 -0
- package/dist/v2/runtime/core/runtime.cjs.map +1 -1
- package/dist/v2/runtime/core/runtime.d.cts +53 -4
- package/dist/v2/runtime/core/runtime.d.cts.map +1 -1
- package/dist/v2/runtime/core/runtime.d.mts +53 -4
- package/dist/v2/runtime/core/runtime.d.mts.map +1 -1
- package/dist/v2/runtime/core/runtime.mjs +26 -2
- package/dist/v2/runtime/core/runtime.mjs.map +1 -1
- package/dist/v2/runtime/handlers/get-runtime-info.cjs +18 -10
- package/dist/v2/runtime/handlers/get-runtime-info.cjs.map +1 -1
- package/dist/v2/runtime/handlers/get-runtime-info.mjs +19 -11
- package/dist/v2/runtime/handlers/get-runtime-info.mjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-connect.cjs +1 -1
- package/dist/v2/runtime/handlers/handle-connect.cjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-connect.mjs +1 -1
- package/dist/v2/runtime/handlers/handle-connect.mjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-run.cjs +8 -2
- package/dist/v2/runtime/handlers/handle-run.cjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-run.mjs +8 -2
- package/dist/v2/runtime/handlers/handle-run.mjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-stop.cjs +2 -1
- package/dist/v2/runtime/handlers/handle-stop.cjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-stop.mjs +2 -1
- package/dist/v2/runtime/handlers/handle-stop.mjs.map +1 -1
- package/dist/v2/runtime/handlers/intelligence/thread-names.cjs +1 -1
- package/dist/v2/runtime/handlers/intelligence/thread-names.cjs.map +1 -1
- package/dist/v2/runtime/handlers/intelligence/thread-names.mjs +1 -1
- package/dist/v2/runtime/handlers/intelligence/thread-names.mjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/agent-utils.cjs +3 -2
- package/dist/v2/runtime/handlers/shared/agent-utils.cjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/agent-utils.mjs +3 -2
- package/dist/v2/runtime/handlers/shared/agent-utils.mjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/sse-response.cjs +40 -1
- package/dist/v2/runtime/handlers/shared/sse-response.cjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/sse-response.mjs +40 -1
- package/dist/v2/runtime/handlers/shared/sse-response.mjs.map +1 -1
- package/dist/v2/runtime/handlers/sse/run.cjs +3 -1
- package/dist/v2/runtime/handlers/sse/run.cjs.map +1 -1
- package/dist/v2/runtime/handlers/sse/run.mjs +3 -1
- package/dist/v2/runtime/handlers/sse/run.mjs.map +1 -1
- package/dist/v2/runtime/index.d.cts +1 -1
- package/dist/v2/runtime/index.d.mts +1 -1
- package/package.json +7 -6
- package/src/agent/__tests__/capabilities.test.ts +81 -0
- package/src/agent/converters/tanstack.ts +15 -7
- package/src/agent/index.ts +33 -0
- package/src/lib/runtime/__tests__/v1-agent-factory.test.ts +109 -0
- package/src/lib/runtime/copilot-runtime.ts +38 -2
- package/src/service-adapters/openai/__tests__/openai-v5-compat.test.ts +177 -0
- package/src/service-adapters/openai/openai-adapter.ts +3 -1
- package/src/service-adapters/openai/openai-assistant-adapter.ts +7 -9
- package/src/service-adapters/openai/utils.ts +100 -0
- package/src/v2/runtime/__tests__/agents-factory.test.ts +136 -0
- package/src/v2/runtime/__tests__/debug-sse-response.test.ts +302 -0
- package/src/v2/runtime/__tests__/get-runtime-info.test.ts +134 -1
- package/src/v2/runtime/core/runtime.ts +90 -2
- package/src/v2/runtime/handlers/get-runtime-info.ts +33 -8
- package/src/v2/runtime/handlers/handle-connect.ts +1 -1
- package/src/v2/runtime/handlers/handle-run.ts +16 -2
- package/src/v2/runtime/handlers/handle-stop.ts +2 -1
- package/src/v2/runtime/handlers/intelligence/thread-names.ts +1 -1
- package/src/v2/runtime/handlers/shared/agent-utils.ts +3 -2
- package/src/v2/runtime/handlers/shared/sse-response.ts +69 -0
- package/src/v2/runtime/handlers/sse/run.ts +9 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.cjs","names":[],"sources":["../../../src/service-adapters/openai/utils.ts"],"sourcesContent":["import { Message } from \"../../graphql/types/converted\";\nimport { ActionInput } from \"../../graphql/inputs/action.input\";\nimport {\n ChatCompletionAssistantMessageParam,\n ChatCompletionMessageParam,\n ChatCompletionSystemMessageParam,\n ChatCompletionTool,\n ChatCompletionUserMessageParam,\n ChatCompletionDeveloperMessageParam,\n} from \"openai/resources/chat\";\nimport { parseJson } from \"@copilotkit/shared\";\n\nexport function limitMessagesToTokenCount(\n messages: any[],\n tools: any[],\n model: string,\n maxTokens?: number,\n): any[] {\n maxTokens ||= maxTokensForOpenAIModel(model);\n\n const result: any[] = [];\n const toolsNumTokens = countToolsTokens(model, tools);\n if (toolsNumTokens > maxTokens) {\n throw new Error(\n `Too many tokens in function definitions: ${toolsNumTokens} > ${maxTokens}`,\n );\n }\n maxTokens -= toolsNumTokens;\n\n for (const message of messages) {\n if ([\"system\", \"developer\"].includes(message.role)) {\n const numTokens = countMessageTokens(model, message);\n maxTokens -= numTokens;\n\n if (maxTokens < 0) {\n throw new Error(\"Not enough tokens for system message.\");\n }\n }\n }\n\n let cutoff: boolean = false;\n\n const reversedMessages = [...messages].toReversed();\n for (const message of reversedMessages) {\n if ([\"system\", \"developer\"].includes(message.role)) {\n result.unshift(message);\n continue;\n } else if (cutoff) {\n continue;\n }\n let numTokens = countMessageTokens(model, message);\n if (maxTokens < numTokens) {\n cutoff = true;\n continue;\n }\n result.unshift(message);\n maxTokens -= numTokens;\n }\n\n return result;\n}\n\nexport function maxTokensForOpenAIModel(model: string): number {\n return maxTokensByModel[model] || DEFAULT_MAX_TOKENS;\n}\n\nconst DEFAULT_MAX_TOKENS = 128000;\n\nconst maxTokensByModel: { [key: string]: number } = {\n // o1\n o1: 200000,\n \"o1-2024-12-17\": 200000,\n \"o1-mini\": 128000,\n \"o1-mini-2024-09-12\": 128000,\n \"o1-preview\": 128000,\n \"o1-preview-2024-09-12\": 128000,\n // o3-mini\n \"o3-mini\": 200000,\n \"o3-mini-2025-01-31\": 200000,\n // GPT-4\n \"gpt-4o\": 128000,\n \"chatgpt-4o-latest\": 128000,\n \"gpt-4o-2024-08-06\": 128000,\n \"gpt-4o-2024-05-13\": 128000,\n \"gpt-4o-mini\": 128000,\n \"gpt-4o-mini-2024-07-18\": 128000,\n \"gpt-4-turbo\": 128000,\n \"gpt-4-turbo-2024-04-09\": 128000,\n \"gpt-4-0125-preview\": 128000,\n \"gpt-4-turbo-preview\": 128000,\n \"gpt-4-1106-preview\": 128000,\n \"gpt-4-vision-preview\": 128000,\n \"gpt-4-1106-vision-preview\": 128000,\n \"gpt-4-32k\": 32768,\n \"gpt-4-32k-0613\": 32768,\n \"gpt-4-32k-0314\": 32768,\n \"gpt-4\": 8192,\n \"gpt-4-0613\": 8192,\n \"gpt-4-0314\": 8192,\n\n // GPT-3.5\n \"gpt-3.5-turbo-0125\": 16385,\n \"gpt-3.5-turbo\": 16385,\n \"gpt-3.5-turbo-1106\": 16385,\n \"gpt-3.5-turbo-instruct\": 4096,\n \"gpt-3.5-turbo-16k\": 16385,\n \"gpt-3.5-turbo-0613\": 4096,\n \"gpt-3.5-turbo-16k-0613\": 16385,\n \"gpt-3.5-turbo-0301\": 4097,\n};\n\nfunction countToolsTokens(model: string, tools: any[]): number {\n if (tools.length === 0) {\n return 0;\n }\n const json = JSON.stringify(tools);\n return countTokens(model, json);\n}\n\nfunction countMessageTokens(model: string, message: any): number {\n return countTokens(model, message.content || \"\");\n}\n\nfunction countTokens(model: string, text: string): number {\n return text.length / 3;\n}\n\nexport function convertActionInputToOpenAITool(\n action: ActionInput,\n): ChatCompletionTool {\n return {\n type: \"function\",\n function: {\n name: action.name,\n description: action.description,\n parameters: parseJson(action.jsonSchema, {}),\n },\n };\n}\n\ntype UsedMessageParams =\n | ChatCompletionUserMessageParam\n | ChatCompletionAssistantMessageParam\n | ChatCompletionDeveloperMessageParam\n | ChatCompletionSystemMessageParam;\nexport function convertMessageToOpenAIMessage(\n message: Message,\n options?: { keepSystemRole: boolean },\n): ChatCompletionMessageParam {\n const { keepSystemRole } = options || { keepSystemRole: false };\n if (message.isTextMessage()) {\n let role = message.role as UsedMessageParams[\"role\"];\n if (message.role === \"system\" && !keepSystemRole) {\n role = \"developer\";\n }\n return {\n role,\n content: message.content,\n } satisfies UsedMessageParams;\n } else if (message.isImageMessage()) {\n return {\n role: \"user\",\n content: [\n {\n type: \"image_url\",\n image_url: {\n url: `data:image/${message.format};base64,${message.bytes}`,\n },\n },\n ],\n } satisfies UsedMessageParams;\n } else if (message.isActionExecutionMessage()) {\n return {\n role: \"assistant\",\n tool_calls: [\n {\n id: message.id,\n type: \"function\",\n function: {\n name: message.name,\n arguments: JSON.stringify(message.arguments),\n },\n },\n ],\n };\n } else if (message.isResultMessage()) {\n return {\n role: \"tool\",\n content: message.result,\n tool_call_id: message.actionExecutionId,\n };\n }\n}\n\nexport function convertSystemMessageToAssistantAPI(\n message: ChatCompletionMessageParam,\n) {\n return {\n ...message,\n ...([\"system\", \"developer\"].includes(message.role) && {\n role: \"assistant\",\n content: \"THE FOLLOWING MESSAGE IS A SYSTEM MESSAGE: \" + message.content,\n }),\n };\n}\n"],"mappings":";;;;;AAYA,SAAgB,0BACd,UACA,OACA,OACA,WACO;AACP,eAAc,wBAAwB,MAAM;CAE5C,MAAM,SAAgB,EAAE;CACxB,MAAM,iBAAiB,iBAAiB,OAAO,MAAM;AACrD,KAAI,iBAAiB,UACnB,OAAM,IAAI,MACR,4CAA4C,eAAe,KAAK,YACjE;AAEH,cAAa;AAEb,MAAK,MAAM,WAAW,SACpB,KAAI,CAAC,UAAU,YAAY,CAAC,SAAS,QAAQ,KAAK,EAAE;EAClD,MAAM,YAAY,mBAAmB,OAAO,QAAQ;AACpD,eAAa;AAEb,MAAI,YAAY,EACd,OAAM,IAAI,MAAM,wCAAwC;;CAK9D,IAAI,SAAkB;CAEtB,MAAM,mBAAmB,CAAC,GAAG,SAAS,CAAC,YAAY;AACnD,MAAK,MAAM,WAAW,kBAAkB;AACtC,MAAI,CAAC,UAAU,YAAY,CAAC,SAAS,QAAQ,KAAK,EAAE;AAClD,UAAO,QAAQ,QAAQ;AACvB;aACS,OACT;EAEF,IAAI,YAAY,mBAAmB,OAAO,QAAQ;AAClD,MAAI,YAAY,WAAW;AACzB,YAAS;AACT;;AAEF,SAAO,QAAQ,QAAQ;AACvB,eAAa;;AAGf,QAAO;;AAGT,SAAgB,wBAAwB,OAAuB;AAC7D,QAAO,iBAAiB,UAAU;;AAGpC,MAAM,qBAAqB;AAE3B,MAAM,mBAA8C;CAElD,IAAI;CACJ,iBAAiB;CACjB,WAAW;CACX,sBAAsB;CACtB,cAAc;CACd,yBAAyB;CAEzB,WAAW;CACX,sBAAsB;CAEtB,UAAU;CACV,qBAAqB;CACrB,qB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AAM,QAAQ;IACd,WAAW,KAAK,UAAU,QAAQ,UAAU;IAC7C;GACF,CACF;EACF;UACQ,QAAQ,iBAAiB,CAClC,QAAO;EACL,MAAM;EACN,SAAS,QAAQ;EACjB,cAAc,QAAQ;EACvB;;AAIL,SAAgB,mCACd,SACA;AACA,QAAO;EACL,GAAG;EACH,GAAI,CAAC,UAAU,YAAY,CAAC,SAAS,QAAQ,KAAK,IAAI;GACpD,MAAM;GACN,SAAS,gDAAgD,QAAQ;GAClE;EACF"}
|
|
1
|
+
{"version":3,"file":"utils.cjs","names":[],"sources":["../../../src/service-adapters/openai/utils.ts"],"sourcesContent":["import type OpenAI from \"openai\";\nimport { Message } from \"../../graphql/types/converted\";\nimport { ActionInput } from \"../../graphql/inputs/action.input\";\nimport {\n ChatCompletionAssistantMessageParam,\n ChatCompletionMessageParam,\n ChatCompletionSystemMessageParam,\n ChatCompletionTool,\n ChatCompletionUserMessageParam,\n ChatCompletionDeveloperMessageParam,\n} from \"openai/resources/chat\";\nimport { parseJson } from \"@copilotkit/shared\";\n\n/**\n * OpenAI v4 exposes streaming completions under `beta.chat.completions`.\n * v5 removed `beta.chat` and promoted streaming to `chat.completions`.\n * These interfaces model the v4-specific shape so we can detect and access\n * the beta namespace safely without `as any`.\n */\ninterface OpenAIV4BetaChat {\n chat: {\n completions: OpenAI[\"chat\"][\"completions\"];\n };\n}\n\ninterface OpenAIV4Beta extends OpenAI.Beta {\n chat: OpenAIV4BetaChat[\"chat\"];\n}\n\n/**\n * Type guard: checks whether the OpenAI client has the v4-era `beta.chat`\n * namespace. Returns `false` for v5+ clients where `beta.chat` was removed.\n */\nfunction hasV4BetaChat(beta: OpenAI[\"beta\"] | undefined): beta is OpenAIV4Beta {\n return beta != null && \"chat\" in beta && (beta as OpenAIV4Beta).chat != null;\n}\n\n/**\n * Detects whether the provided OpenAI client is v5+ by checking for the\n * removal of the `beta.chat` namespace (which was promoted to `chat` in v5).\n */\nexport function isOpenAIV5(openai: OpenAI): boolean {\n return !hasV4BetaChat(openai.beta);\n}\n\n/**\n * Returns the chat completions object that supports `.stream()`.\n * In v4 this lives under `openai.beta.chat.completions`;\n * in v5 it was promoted to `openai.chat.completions`.\n */\nexport function getChatCompletionsForStreaming(\n openai: OpenAI,\n): OpenAI[\"chat\"][\"completions\"] {\n if (hasV4BetaChat(openai.beta)) {\n return openai.beta.chat.completions;\n }\n return openai.chat.completions;\n}\n\n/**\n * Retrieves a thread run, handling the v4→v5 API signature change.\n * v4: retrieve(threadId, runId)\n * v5: retrieve(runId, { thread_id: threadId })\n */\nexport async function retrieveThreadRun(\n openai: OpenAI,\n threadId: string,\n runId: string,\n): Promise<OpenAI.Beta.Threads.Runs.Run> {\n if (isOpenAIV5(openai)) {\n // v5 switched to named path params. The type definitions from whichever\n // SDK version is installed won't match both signatures, so we call through\n // a generic function reference. This is the one unavoidable boundary\n // between two incompatible SDK type surfaces.\n const retrieve = openai.beta.threads.runs.retrieve as {\n (...args: unknown[]): Promise<OpenAI.Beta.Threads.Runs.Run>;\n };\n return retrieve(runId, { thread_id: threadId });\n }\n return openai.beta.threads.runs.retrieve(threadId, runId);\n}\n\n/**\n * Submits tool outputs as a stream, handling the v4→v5 API signature change.\n * v4: submitToolOutputsStream(threadId, runId, body)\n * v5: submitToolOutputsStream(runId, { thread_id, ...body })\n */\nexport function submitToolOutputsStream(\n openai: OpenAI,\n threadId: string,\n runId: string,\n body: {\n tool_outputs: Array<{ tool_call_id: string; output: string }>;\n parallel_tool_calls?: false;\n },\n) {\n if (isOpenAIV5(openai)) {\n // Same boundary as retrieveThreadRun — v5 uses named path params.\n const submit = openai.beta.threads.runs.submitToolOutputsStream as {\n (\n ...args: unknown[]\n ): ReturnType<typeof openai.beta.threads.runs.submitToolOutputsStream>;\n };\n return submit(runId, { thread_id: threadId, ...body });\n }\n return openai.beta.threads.runs.submitToolOutputsStream(\n threadId,\n runId,\n body,\n );\n}\n\nexport function limitMessagesToTokenCount(\n messages: any[],\n tools: any[],\n model: string,\n maxTokens?: number,\n): any[] {\n maxTokens ||= maxTokensForOpenAIModel(model);\n\n const result: any[] = [];\n const toolsNumTokens = countToolsTokens(model, tools);\n if (toolsNumTokens > maxTokens) {\n throw new Error(\n `Too many tokens in function definitions: ${toolsNumTokens} > ${maxTokens}`,\n );\n }\n maxTokens -= toolsNumTokens;\n\n for (const message of messages) {\n if ([\"system\", \"developer\"].includes(message.role)) {\n const numTokens = countMessageTokens(model, message);\n maxTokens -= numTokens;\n\n if (maxTokens < 0) {\n throw new Error(\"Not enough tokens for system message.\");\n }\n }\n }\n\n let cutoff: boolean = false;\n\n const reversedMessages = [...messages].toReversed();\n for (const message of reversedMessages) {\n if ([\"system\", \"developer\"].includes(message.role)) {\n result.unshift(message);\n continue;\n } else if (cutoff) {\n continue;\n }\n let numTokens = countMessageTokens(model, message);\n if (maxTokens < numTokens) {\n cutoff = true;\n continue;\n }\n result.unshift(message);\n maxTokens -= numTokens;\n }\n\n return result;\n}\n\nexport function maxTokensForOpenAIModel(model: string): number {\n return maxTokensByModel[model] || DEFAULT_MAX_TOKENS;\n}\n\nconst DEFAULT_MAX_TOKENS = 128000;\n\nconst maxTokensByModel: { [key: string]: number } = {\n // o1\n o1: 200000,\n \"o1-2024-12-17\": 200000,\n \"o1-mini\": 128000,\n \"o1-mini-2024-09-12\": 128000,\n \"o1-preview\": 128000,\n \"o1-preview-2024-09-12\": 128000,\n // o3-mini\n \"o3-mini\": 200000,\n \"o3-mini-2025-01-31\": 200000,\n // GPT-4\n \"gpt-4o\": 128000,\n \"chatgpt-4o-latest\": 128000,\n \"gpt-4o-2024-08-06\": 128000,\n \"gpt-4o-2024-05-13\": 128000,\n \"gpt-4o-mini\": 128000,\n \"gpt-4o-mini-2024-07-18\": 128000,\n \"gpt-4-turbo\": 128000,\n \"gpt-4-turbo-2024-04-09\": 128000,\n \"gpt-4-0125-preview\": 128000,\n \"gpt-4-turbo-preview\": 128000,\n \"gpt-4-1106-preview\": 128000,\n \"gpt-4-vision-preview\": 128000,\n \"gpt-4-1106-vision-preview\": 128000,\n \"gpt-4-32k\": 32768,\n \"gpt-4-32k-0613\": 32768,\n \"gpt-4-32k-0314\": 32768,\n \"gpt-4\": 8192,\n \"gpt-4-0613\": 8192,\n \"gpt-4-0314\": 8192,\n\n // GPT-3.5\n \"gpt-3.5-turbo-0125\": 16385,\n \"gpt-3.5-turbo\": 16385,\n \"gpt-3.5-turbo-1106\": 16385,\n \"gpt-3.5-turbo-instruct\": 4096,\n \"gpt-3.5-turbo-16k\": 16385,\n \"gpt-3.5-turbo-0613\": 4096,\n \"gpt-3.5-turbo-16k-0613\": 16385,\n \"gpt-3.5-turbo-0301\": 4097,\n};\n\nfunction countToolsTokens(model: string, tools: any[]): number {\n if (tools.length === 0) {\n return 0;\n }\n const json = JSON.stringify(tools);\n return countTokens(model, json);\n}\n\nfunction countMessageTokens(model: string, message: any): number {\n return countTokens(model, message.content || \"\");\n}\n\nfunction countTokens(model: string, text: string): number {\n return text.length / 3;\n}\n\nexport function convertActionInputToOpenAITool(\n action: ActionInput,\n): ChatCompletionTool {\n return {\n type: \"function\",\n function: {\n name: action.name,\n description: action.description,\n parameters: parseJson(action.jsonSchema, {}),\n },\n };\n}\n\ntype UsedMessageParams =\n | ChatCompletionUserMessageParam\n | ChatCompletionAssistantMessageParam\n | ChatCompletionDeveloperMessageParam\n | ChatCompletionSystemMessageParam;\nexport function convertMessageToOpenAIMessage(\n message: Message,\n options?: { keepSystemRole: boolean },\n): ChatCompletionMessageParam {\n const { keepSystemRole } = options || { keepSystemRole: false };\n if (message.isTextMessage()) {\n let role = message.role as UsedMessageParams[\"role\"];\n if (message.role === \"system\" && !keepSystemRole) {\n role = \"developer\";\n }\n return {\n role,\n content: message.content,\n } satisfies UsedMessageParams;\n } else if (message.isImageMessage()) {\n return {\n role: \"user\",\n content: [\n {\n type: \"image_url\",\n image_url: {\n url: `data:image/${message.format};base64,${message.bytes}`,\n },\n },\n ],\n } satisfies UsedMessageParams;\n } else if (message.isActionExecutionMessage()) {\n return {\n role: \"assistant\",\n tool_calls: [\n {\n id: message.id,\n type: \"function\",\n function: {\n name: message.name,\n arguments: JSON.stringify(message.arguments),\n },\n },\n ],\n };\n } else if (message.isResultMessage()) {\n return {\n role: \"tool\",\n content: message.result,\n tool_call_id: message.actionExecutionId,\n };\n }\n}\n\nexport function convertSystemMessageToAssistantAPI(\n message: ChatCompletionMessageParam,\n) {\n return {\n ...message,\n ...([\"system\", \"developer\"].includes(message.role) && {\n role: \"assistant\",\n content: \"THE FOLLOWING MESSAGE IS A SYSTEM MESSAGE: \" + message.content,\n }),\n };\n}\n"],"mappings":";;;;;;;;;AAiCA,SAAS,cAAc,MAAwD;AAC7E,QAAO,QAAQ,QAAQ,UAAU,QAAS,KAAsB,QAAQ;;;;;;AAO1E,SAAgB,WAAW,QAAyB;AAClD,QAAO,CAAC,cAAc,OAAO,KAAK;;;;;;;AAQpC,SAAgB,+BACd,QAC+B;AAC/B,KAAI,cAAc,OAAO,KAAK,CAC5B,QAAO,OAAO,KAAK,KAAK;AAE1B,QAAO,OAAO,KAAK;;;;;;;AAQrB,eAAsB,kBACpB,QACA,UACA,OACuC;AACvC,KAAI,WAAW,OAAO,EAAE;EAKtB,MAAM,WAAW,OAAO,KAAK,QAAQ,KAAK;AAG1C,SAAO,SAAS,OAAO,EAAE,WAAW,UAAU,CAAC;;AAEjD,QAAO,OAAO,KAAK,QAAQ,KAAK,SAAS,UAAU,MAAM;;;;;;;AAQ3D,SAAgB,wBACd,QACA,UACA,OACA,MAIA;AACA,KAAI,WAAW,OAAO,EAAE;EAEtB,MAAM,SAAS,OAAO,KAAK,QAAQ,KAAK;AAKxC,SAAO,OAAO,OAAO;GAAE,WAAW;GAAU,GAAG;GAAM,CAAC;;AAExD,QAAO,OAAO,KAAK,QAAQ,KAAK,wBAC9B,UACA,OACA,KACD;;AAGH,SAAgB,0BACd,UACA,OACA,OACA,WACO;AACP,eAAc,wBAAwB,MAAM;CAE5C,MAAM,SAAgB,EAAE;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 import "reflect-metadata";
|
|
|
2
2
|
import { parseJson } from "@copilotkit/shared";
|
|
3
3
|
|
|
4
4
|
//#region src/service-adapters/openai/utils.ts
|
|
5
|
+
/**
|
|
6
|
+
* Type guard: checks whether the OpenAI client has the v4-era `beta.chat`
|
|
7
|
+
* namespace. Returns `false` for v5+ clients where `beta.chat` was removed.
|
|
8
|
+
*/
|
|
9
|
+
function hasV4BetaChat(beta) {
|
|
10
|
+
return beta != null && "chat" in beta && beta.chat != null;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Detects whether the provided OpenAI client is v5+ by checking for the
|
|
14
|
+
* removal of the `beta.chat` namespace (which was promoted to `chat` in v5).
|
|
15
|
+
*/
|
|
16
|
+
function isOpenAIV5(openai) {
|
|
17
|
+
return !hasV4BetaChat(openai.beta);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Returns the chat completions object that supports `.stream()`.
|
|
21
|
+
* In v4 this lives under `openai.beta.chat.completions`;
|
|
22
|
+
* in v5 it was promoted to `openai.chat.completions`.
|
|
23
|
+
*/
|
|
24
|
+
function getChatCompletionsForStreaming(openai) {
|
|
25
|
+
if (hasV4BetaChat(openai.beta)) return openai.beta.chat.completions;
|
|
26
|
+
return openai.chat.completions;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Retrieves a thread run, handling the v4→v5 API signature change.
|
|
30
|
+
* v4: retrieve(threadId, runId)
|
|
31
|
+
* v5: retrieve(runId, { thread_id: threadId })
|
|
32
|
+
*/
|
|
33
|
+
async function retrieveThreadRun(openai, threadId, runId) {
|
|
34
|
+
if (isOpenAIV5(openai)) {
|
|
35
|
+
const retrieve = openai.beta.threads.runs.retrieve;
|
|
36
|
+
return retrieve(runId, { thread_id: threadId });
|
|
37
|
+
}
|
|
38
|
+
return openai.beta.threads.runs.retrieve(threadId, runId);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Submits tool outputs as a stream, handling the v4→v5 API signature change.
|
|
42
|
+
* v4: submitToolOutputsStream(threadId, runId, body)
|
|
43
|
+
* v5: submitToolOutputsStream(runId, { thread_id, ...body })
|
|
44
|
+
*/
|
|
45
|
+
function submitToolOutputsStream(openai, threadId, runId, body) {
|
|
46
|
+
if (isOpenAIV5(openai)) {
|
|
47
|
+
const submit = openai.beta.threads.runs.submitToolOutputsStream;
|
|
48
|
+
return submit(runId, {
|
|
49
|
+
thread_id: threadId,
|
|
50
|
+
...body
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return openai.beta.threads.runs.submitToolOutputsStream(threadId, runId, body);
|
|
54
|
+
}
|
|
5
55
|
function limitMessagesToTokenCount(messages, tools, model, maxTokens) {
|
|
6
56
|
maxTokens ||= maxTokensForOpenAIModel(model);
|
|
7
57
|
const result = [];
|
|
@@ -135,5 +185,5 @@ function convertSystemMessageToAssistantAPI(message) {
|
|
|
135
185
|
}
|
|
136
186
|
|
|
137
187
|
//#endregion
|
|
138
|
-
export { convertActionInputToOpenAITool, convertMessageToOpenAIMessage, convertSystemMessageToAssistantAPI, limitMessagesToTokenCount };
|
|
188
|
+
export { convertActionInputToOpenAITool, convertMessageToOpenAIMessage, convertSystemMessageToAssistantAPI, getChatCompletionsForStreaming, limitMessagesToTokenCount, retrieveThreadRun, submitToolOutputsStream };
|
|
139
189
|
//# sourceMappingURL=utils.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.mjs","names":[],"sources":["../../../src/service-adapters/openai/utils.ts"],"sourcesContent":["import { Message } from \"../../graphql/types/converted\";\nimport { ActionInput } from \"../../graphql/inputs/action.input\";\nimport {\n ChatCompletionAssistantMessageParam,\n ChatCompletionMessageParam,\n ChatCompletionSystemMessageParam,\n ChatCompletionTool,\n ChatCompletionUserMessageParam,\n ChatCompletionDeveloperMessageParam,\n} from \"openai/resources/chat\";\nimport { parseJson } from \"@copilotkit/shared\";\n\nexport function limitMessagesToTokenCount(\n messages: any[],\n tools: any[],\n model: string,\n maxTokens?: number,\n): any[] {\n maxTokens ||= maxTokensForOpenAIModel(model);\n\n const result: any[] = [];\n const toolsNumTokens = countToolsTokens(model, tools);\n if (toolsNumTokens > maxTokens) {\n throw new Error(\n `Too many tokens in function definitions: ${toolsNumTokens} > ${maxTokens}`,\n );\n }\n maxTokens -= toolsNumTokens;\n\n for (const message of messages) {\n if ([\"system\", \"developer\"].includes(message.role)) {\n const numTokens = countMessageTokens(model, message);\n maxTokens -= numTokens;\n\n if (maxTokens < 0) {\n throw new Error(\"Not enough tokens for system message.\");\n }\n }\n }\n\n let cutoff: boolean = false;\n\n const reversedMessages = [...messages].toReversed();\n for (const message of reversedMessages) {\n if ([\"system\", \"developer\"].includes(message.role)) {\n result.unshift(message);\n continue;\n } else if (cutoff) {\n continue;\n }\n let numTokens = countMessageTokens(model, message);\n if (maxTokens < numTokens) {\n cutoff = true;\n continue;\n }\n result.unshift(message);\n maxTokens -= numTokens;\n }\n\n return result;\n}\n\nexport function maxTokensForOpenAIModel(model: string): number {\n return maxTokensByModel[model] || DEFAULT_MAX_TOKENS;\n}\n\nconst DEFAULT_MAX_TOKENS = 128000;\n\nconst maxTokensByModel: { [key: string]: number } = {\n // o1\n o1: 200000,\n \"o1-2024-12-17\": 200000,\n \"o1-mini\": 128000,\n \"o1-mini-2024-09-12\": 128000,\n \"o1-preview\": 128000,\n \"o1-preview-2024-09-12\": 128000,\n // o3-mini\n \"o3-mini\": 200000,\n \"o3-mini-2025-01-31\": 200000,\n // GPT-4\n \"gpt-4o\": 128000,\n \"chatgpt-4o-latest\": 128000,\n \"gpt-4o-2024-08-06\": 128000,\n \"gpt-4o-2024-05-13\": 128000,\n \"gpt-4o-mini\": 128000,\n \"gpt-4o-mini-2024-07-18\": 128000,\n \"gpt-4-turbo\": 128000,\n \"gpt-4-turbo-2024-04-09\": 128000,\n \"gpt-4-0125-preview\": 128000,\n \"gpt-4-turbo-preview\": 128000,\n \"gpt-4-1106-preview\": 128000,\n \"gpt-4-vision-preview\": 128000,\n \"gpt-4-1106-vision-preview\": 128000,\n \"gpt-4-32k\": 32768,\n \"gpt-4-32k-0613\": 32768,\n \"gpt-4-32k-0314\": 32768,\n \"gpt-4\": 8192,\n \"gpt-4-0613\": 8192,\n \"gpt-4-0314\": 8192,\n\n // GPT-3.5\n \"gpt-3.5-turbo-0125\": 16385,\n \"gpt-3.5-turbo\": 16385,\n \"gpt-3.5-turbo-1106\": 16385,\n \"gpt-3.5-turbo-instruct\": 4096,\n \"gpt-3.5-turbo-16k\": 16385,\n \"gpt-3.5-turbo-0613\": 4096,\n \"gpt-3.5-turbo-16k-0613\": 16385,\n \"gpt-3.5-turbo-0301\": 4097,\n};\n\nfunction countToolsTokens(model: string, tools: any[]): number {\n if (tools.length === 0) {\n return 0;\n }\n const json = JSON.stringify(tools);\n return countTokens(model, json);\n}\n\nfunction countMessageTokens(model: string, message: any): number {\n return countTokens(model, message.content || \"\");\n}\n\nfunction countTokens(model: string, text: string): number {\n return text.length / 3;\n}\n\nexport function convertActionInputToOpenAITool(\n action: ActionInput,\n): ChatCompletionTool {\n return {\n type: \"function\",\n function: {\n name: action.name,\n description: action.description,\n parameters: parseJson(action.jsonSchema, {}),\n },\n };\n}\n\ntype UsedMessageParams =\n | ChatCompletionUserMessageParam\n | ChatCompletionAssistantMessageParam\n | ChatCompletionDeveloperMessageParam\n | ChatCompletionSystemMessageParam;\nexport function convertMessageToOpenAIMessage(\n message: Message,\n options?: { keepSystemRole: boolean },\n): ChatCompletionMessageParam {\n const { keepSystemRole } = options || { keepSystemRole: false };\n if (message.isTextMessage()) {\n let role = message.role as UsedMessageParams[\"role\"];\n if (message.role === \"system\" && !keepSystemRole) {\n role = \"developer\";\n }\n return {\n role,\n content: message.content,\n } satisfies UsedMessageParams;\n } else if (message.isImageMessage()) {\n return {\n role: \"user\",\n content: [\n {\n type: \"image_url\",\n image_url: {\n url: `data:image/${message.format};base64,${message.bytes}`,\n },\n },\n ],\n } satisfies UsedMessageParams;\n } else if (message.isActionExecutionMessage()) {\n return {\n role: \"assistant\",\n tool_calls: [\n {\n id: message.id,\n type: \"function\",\n function: {\n name: message.name,\n arguments: JSON.stringify(message.arguments),\n },\n },\n ],\n };\n } else if (message.isResultMessage()) {\n return {\n role: \"tool\",\n content: message.result,\n tool_call_id: message.actionExecutionId,\n };\n }\n}\n\nexport function convertSystemMessageToAssistantAPI(\n message: ChatCompletionMessageParam,\n) {\n return {\n ...message,\n ...([\"system\", \"developer\"].includes(message.role) && {\n role: \"assistant\",\n content: \"THE FOLLOWING MESSAGE IS A SYSTEM MESSAGE: \" + message.content,\n }),\n };\n}\n"],"mappings":";;;;AAYA,SAAgB,0BACd,UACA,OACA,OACA,WACO;AACP,eAAc,wBAAwB,MAAM;CAE5C,MAAM,SAAgB,EAAE;CACxB,MAAM,iBAAiB,iBAAiB,OAAO,MAAM;AACrD,KAAI,iBAAiB,UACnB,OAAM,IAAI,MACR,4CAA4C,eAAe,KAAK,YACjE;AAEH,cAAa;AAEb,MAAK,MAAM,WAAW,SACpB,KAAI,CAAC,UAAU,YAAY,CAAC,SAAS,QAAQ,KAAK,EAAE;EAClD,MAAM,YAAY,mBAAmB,OAAO,QAAQ;AACpD,eAAa;AAEb,MAAI,YAAY,EACd,OAAM,IAAI,MAAM,wCAAwC;;CAK9D,IAAI,SAAkB;CAEtB,MAAM,mBAAmB,CAAC,GAAG,SAAS,CAAC,YAAY;AACnD,MAAK,MAAM,WAAW,kBAAkB;AACtC,MAAI,CAAC,UAAU,YAAY,CAAC,SAAS,QAAQ,KAAK,EAAE;AAClD,UAAO,QAAQ,QAAQ;AACvB;aACS,OACT;EAEF,IAAI,YAAY,mBAAmB,OAAO,QAAQ;AAClD,MAAI,YAAY,WAAW;AACzB,YAAS;AACT;;AAEF,SAAO,QAAQ,QAAQ;AACvB,eAAa;;AAGf,QAAO;;AAGT,SAAgB,wBAAwB,OAAuB;AAC7D,QAAO,iBAAiB,UAAU;;AAGpC,MAAM,qBAAqB;AAE3B,MAAM,mBAA8C;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UAAU,QAAQ,SACrD;GACF,CACF;EACF;UACQ,QAAQ,0BAA0B,CAC3C,QAAO;EACL,MAAM;EACN,YAAY,CACV;GACE,IAAI,QAAQ;GACZ,MAAM;GACN,UAAU;IACR,MAAM,QAAQ;IACd,WAAW,KAAK,UAAU,QAAQ,UAAU;IAC7C;GACF,CACF;EACF;UACQ,QAAQ,iBAAiB,CAClC,QAAO;EACL,MAAM;EACN,SAAS,QAAQ;EACjB,cAAc,QAAQ;EACvB;;AAIL,SAAgB,mCACd,SACA;AACA,QAAO;EACL,GAAG;EACH,GAAI,CAAC,UAAU,YAAY,CAAC,SAAS,QAAQ,KAAK,IAAI;GACpD,MAAM;GACN,SAAS,gDAAgD,QAAQ;GAClE;EACF"}
|
|
1
|
+
{"version":3,"file":"utils.mjs","names":[],"sources":["../../../src/service-adapters/openai/utils.ts"],"sourcesContent":["import type OpenAI from \"openai\";\nimport { Message } from \"../../graphql/types/converted\";\nimport { ActionInput } from \"../../graphql/inputs/action.input\";\nimport {\n ChatCompletionAssistantMessageParam,\n ChatCompletionMessageParam,\n ChatCompletionSystemMessageParam,\n ChatCompletionTool,\n ChatCompletionUserMessageParam,\n ChatCompletionDeveloperMessageParam,\n} from \"openai/resources/chat\";\nimport { parseJson } from \"@copilotkit/shared\";\n\n/**\n * OpenAI v4 exposes streaming completions under `beta.chat.completions`.\n * v5 removed `beta.chat` and promoted streaming to `chat.completions`.\n * These interfaces model the v4-specific shape so we can detect and access\n * the beta namespace safely without `as any`.\n */\ninterface OpenAIV4BetaChat {\n chat: {\n completions: OpenAI[\"chat\"][\"completions\"];\n };\n}\n\ninterface OpenAIV4Beta extends OpenAI.Beta {\n chat: OpenAIV4BetaChat[\"chat\"];\n}\n\n/**\n * Type guard: checks whether the OpenAI client has the v4-era `beta.chat`\n * namespace. Returns `false` for v5+ clients where `beta.chat` was removed.\n */\nfunction hasV4BetaChat(beta: OpenAI[\"beta\"] | undefined): beta is OpenAIV4Beta {\n return beta != null && \"chat\" in beta && (beta as OpenAIV4Beta).chat != null;\n}\n\n/**\n * Detects whether the provided OpenAI client is v5+ by checking for the\n * removal of the `beta.chat` namespace (which was promoted to `chat` in v5).\n */\nexport function isOpenAIV5(openai: OpenAI): boolean {\n return !hasV4BetaChat(openai.beta);\n}\n\n/**\n * Returns the chat completions object that supports `.stream()`.\n * In v4 this lives under `openai.beta.chat.completions`;\n * in v5 it was promoted to `openai.chat.completions`.\n */\nexport function getChatCompletionsForStreaming(\n openai: OpenAI,\n): OpenAI[\"chat\"][\"completions\"] {\n if (hasV4BetaChat(openai.beta)) {\n return openai.beta.chat.completions;\n }\n return openai.chat.completions;\n}\n\n/**\n * Retrieves a thread run, handling the v4→v5 API signature change.\n * v4: retrieve(threadId, runId)\n * v5: retrieve(runId, { thread_id: threadId })\n */\nexport async function retrieveThreadRun(\n openai: OpenAI,\n threadId: string,\n runId: string,\n): Promise<OpenAI.Beta.Threads.Runs.Run> {\n if (isOpenAIV5(openai)) {\n // v5 switched to named path params. The type definitions from whichever\n // SDK version is installed won't match both signatures, so we call through\n // a generic function reference. This is the one unavoidable boundary\n // between two incompatible SDK type surfaces.\n const retrieve = openai.beta.threads.runs.retrieve as {\n (...args: unknown[]): Promise<OpenAI.Beta.Threads.Runs.Run>;\n };\n return retrieve(runId, { thread_id: threadId });\n }\n return openai.beta.threads.runs.retrieve(threadId, runId);\n}\n\n/**\n * Submits tool outputs as a stream, handling the v4→v5 API signature change.\n * v4: submitToolOutputsStream(threadId, runId, body)\n * v5: submitToolOutputsStream(runId, { thread_id, ...body })\n */\nexport function submitToolOutputsStream(\n openai: OpenAI,\n threadId: string,\n runId: string,\n body: {\n tool_outputs: Array<{ tool_call_id: string; output: string }>;\n parallel_tool_calls?: false;\n },\n) {\n if (isOpenAIV5(openai)) {\n // Same boundary as retrieveThreadRun — v5 uses named path params.\n const submit = openai.beta.threads.runs.submitToolOutputsStream as {\n (\n ...args: unknown[]\n ): ReturnType<typeof openai.beta.threads.runs.submitToolOutputsStream>;\n };\n return submit(runId, { thread_id: threadId, ...body });\n }\n return openai.beta.threads.runs.submitToolOutputsStream(\n threadId,\n runId,\n body,\n );\n}\n\nexport function limitMessagesToTokenCount(\n messages: any[],\n tools: any[],\n model: string,\n maxTokens?: number,\n): any[] {\n maxTokens ||= maxTokensForOpenAIModel(model);\n\n const result: any[] = [];\n const toolsNumTokens = countToolsTokens(model, tools);\n if (toolsNumTokens > maxTokens) {\n throw new Error(\n `Too many tokens in function definitions: ${toolsNumTokens} > ${maxTokens}`,\n );\n }\n maxTokens -= toolsNumTokens;\n\n for (const message of messages) {\n if ([\"system\", \"developer\"].includes(message.role)) {\n const numTokens = countMessageTokens(model, message);\n maxTokens -= numTokens;\n\n if (maxTokens < 0) {\n throw new Error(\"Not enough tokens for system message.\");\n }\n }\n }\n\n let cutoff: boolean = false;\n\n const reversedMessages = [...messages].toReversed();\n for (const message of reversedMessages) {\n if ([\"system\", \"developer\"].includes(message.role)) {\n result.unshift(message);\n continue;\n } else if (cutoff) {\n continue;\n }\n let numTokens = countMessageTokens(model, message);\n if (maxTokens < numTokens) {\n cutoff = true;\n continue;\n }\n result.unshift(message);\n maxTokens -= numTokens;\n }\n\n return result;\n}\n\nexport function maxTokensForOpenAIModel(model: string): number {\n return maxTokensByModel[model] || DEFAULT_MAX_TOKENS;\n}\n\nconst DEFAULT_MAX_TOKENS = 128000;\n\nconst maxTokensByModel: { [key: string]: number } = {\n // o1\n o1: 200000,\n \"o1-2024-12-17\": 200000,\n \"o1-mini\": 128000,\n \"o1-mini-2024-09-12\": 128000,\n \"o1-preview\": 128000,\n \"o1-preview-2024-09-12\": 128000,\n // o3-mini\n \"o3-mini\": 200000,\n \"o3-mini-2025-01-31\": 200000,\n // GPT-4\n \"gpt-4o\": 128000,\n \"chatgpt-4o-latest\": 128000,\n \"gpt-4o-2024-08-06\": 128000,\n \"gpt-4o-2024-05-13\": 128000,\n \"gpt-4o-mini\": 128000,\n \"gpt-4o-mini-2024-07-18\": 128000,\n \"gpt-4-turbo\": 128000,\n \"gpt-4-turbo-2024-04-09\": 128000,\n \"gpt-4-0125-preview\": 128000,\n \"gpt-4-turbo-preview\": 128000,\n \"gpt-4-1106-preview\": 128000,\n \"gpt-4-vision-preview\": 128000,\n \"gpt-4-1106-vision-preview\": 128000,\n \"gpt-4-32k\": 32768,\n \"gpt-4-32k-0613\": 32768,\n \"gpt-4-32k-0314\": 32768,\n \"gpt-4\": 8192,\n \"gpt-4-0613\": 8192,\n \"gpt-4-0314\": 8192,\n\n // GPT-3.5\n \"gpt-3.5-turbo-0125\": 16385,\n \"gpt-3.5-turbo\": 16385,\n \"gpt-3.5-turbo-1106\": 16385,\n \"gpt-3.5-turbo-instruct\": 4096,\n \"gpt-3.5-turbo-16k\": 16385,\n \"gpt-3.5-turbo-0613\": 4096,\n \"gpt-3.5-turbo-16k-0613\": 16385,\n \"gpt-3.5-turbo-0301\": 4097,\n};\n\nfunction countToolsTokens(model: string, tools: any[]): number {\n if (tools.length === 0) {\n return 0;\n }\n const json = JSON.stringify(tools);\n return countTokens(model, json);\n}\n\nfunction countMessageTokens(model: string, message: any): number {\n return countTokens(model, message.content || \"\");\n}\n\nfunction countTokens(model: string, text: string): number {\n return text.length / 3;\n}\n\nexport function convertActionInputToOpenAITool(\n action: ActionInput,\n): ChatCompletionTool {\n return {\n type: \"function\",\n function: {\n name: action.name,\n description: action.description,\n parameters: parseJson(action.jsonSchema, {}),\n },\n };\n}\n\ntype UsedMessageParams =\n | ChatCompletionUserMessageParam\n | ChatCompletionAssistantMessageParam\n | ChatCompletionDeveloperMessageParam\n | ChatCompletionSystemMessageParam;\nexport function convertMessageToOpenAIMessage(\n message: Message,\n options?: { keepSystemRole: boolean },\n): ChatCompletionMessageParam {\n const { keepSystemRole } = options || { keepSystemRole: false };\n if (message.isTextMessage()) {\n let role = message.role as UsedMessageParams[\"role\"];\n if (message.role === \"system\" && !keepSystemRole) {\n role = \"developer\";\n }\n return {\n role,\n content: message.content,\n } satisfies UsedMessageParams;\n } else if (message.isImageMessage()) {\n return {\n role: \"user\",\n content: [\n {\n type: \"image_url\",\n image_url: {\n url: `data:image/${message.format};base64,${message.bytes}`,\n },\n },\n ],\n } satisfies UsedMessageParams;\n } else if (message.isActionExecutionMessage()) {\n return {\n role: \"assistant\",\n tool_calls: [\n {\n id: message.id,\n type: \"function\",\n function: {\n name: message.name,\n arguments: JSON.stringify(message.arguments),\n },\n },\n ],\n };\n } else if (message.isResultMessage()) {\n return {\n role: \"tool\",\n content: message.result,\n tool_call_id: message.actionExecutionId,\n };\n }\n}\n\nexport function convertSystemMessageToAssistantAPI(\n message: ChatCompletionMessageParam,\n) {\n return {\n ...message,\n ...([\"system\", \"developer\"].includes(message.role) && {\n role: \"assistant\",\n content: \"THE FOLLOWING MESSAGE IS A SYSTEM MESSAGE: \" + message.content,\n }),\n };\n}\n"],"mappings":";;;;;;;;AAiCA,SAAS,cAAc,MAAwD;AAC7E,QAAO,QAAQ,QAAQ,UAAU,QAAS,KAAsB,QAAQ;;;;;;AAO1E,SAAgB,WAAW,QAAyB;AAClD,QAAO,CAAC,cAAc,OAAO,KAAK;;;;;;;AAQpC,SAAgB,+BACd,QAC+B;AAC/B,KAAI,cAAc,OAAO,KAAK,CAC5B,QAAO,OAAO,KAAK,KAAK;AAE1B,QAAO,OAAO,KAAK;;;;;;;AAQrB,eAAsB,kBACpB,QACA,UACA,OACuC;AACvC,KAAI,WAAW,OAAO,EAAE;EAKtB,MAAM,WAAW,OAAO,KAAK,QAAQ,KAAK;AAG1C,SAAO,SAAS,OAAO,EAAE,WAAW,UAAU,CAAC;;AAEjD,QAAO,OAAO,KAAK,QAAQ,KAAK,SAAS,UAAU,MAAM;;;;;;;AAQ3D,SAAgB,wBACd,QACA,UACA,OACA,MAIA;AACA,KAAI,WAAW,OAAO,EAAE;EAEtB,MAAM,SAAS,OAAO,KAAK,QAAQ,KAAK;AAKxC,SAAO,OAAO,OAAO;GAAE,WAAW;GAAU,GAAG;GAAM,CAAC;;AAExD,QAAO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ACL,GAAG;EACH,GAAI,CAAC,UAAU,YAAY,CAAC,SAAS,QAAQ,KAAK,IAAI;GACpD,MAAM;GACN,SAAS,gDAAgD,QAAQ;GAClE;EACF"}
|
package/dist/v2/index.cjs
CHANGED
|
@@ -50,5 +50,6 @@ Object.defineProperty(exports, 'finalizeRunEvents', {
|
|
|
50
50
|
}
|
|
51
51
|
});
|
|
52
52
|
exports.isIntelligenceRuntime = require_runtime$1.isIntelligenceRuntime;
|
|
53
|
+
exports.resolveAgents = require_runtime$1.resolveAgents;
|
|
53
54
|
exports.resolveModel = require_index.resolveModel;
|
|
54
55
|
exports.toFetchCorsConfig = require_hono.toFetchCorsConfig;
|
package/dist/v2/index.d.cts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { TranscribeFileOptions, TranscriptionService } from "./runtime/transcription-service/transcription-service.cjs";
|
|
3
3
|
import { AgentRunner, AgentRunnerConnectRequest, AgentRunnerIsRunningRequest, AgentRunnerRunRequest, AgentRunnerStopRequest } from "./runtime/runner/agent-runner.cjs";
|
|
4
4
|
import { CopilotKitIntelligence, CopilotKitIntelligenceConfig, CreateThreadRequest, ListThreadsResponse, SubscribeToThreadsRequest, SubscribeToThreadsResponse, ThreadSummary, UpdateThreadRequest } from "./runtime/intelligence-platform/client.cjs";
|
|
5
|
-
import { CopilotIntelligenceRuntime, CopilotIntelligenceRuntimeLike, CopilotIntelligenceRuntimeOptions, CopilotRuntime, CopilotRuntimeLike, CopilotRuntimeOptions, CopilotRuntimeUser, CopilotSseRuntime, CopilotSseRuntimeLike, CopilotSseRuntimeOptions, IdentifyUserCallback, McpAppsConfig, McpAppsServerConfig, OpenGenerativeUIConfig, OpenGenerativeUIOptions, VERSION, isIntelligenceRuntime } from "./runtime/core/runtime.cjs";
|
|
5
|
+
import { AgentFactoryContext, AgentsConfig, AgentsFactory, CopilotIntelligenceRuntime, CopilotIntelligenceRuntimeLike, CopilotIntelligenceRuntimeOptions, CopilotRuntime, CopilotRuntimeLike, CopilotRuntimeOptions, CopilotRuntimeUser, CopilotSseRuntime, CopilotSseRuntimeLike, CopilotSseRuntimeOptions, IdentifyUserCallback, McpAppsConfig, McpAppsServerConfig, OpenGenerativeUIConfig, OpenGenerativeUIOptions, VERSION, isIntelligenceRuntime, resolveAgents } from "./runtime/core/runtime.cjs";
|
|
6
6
|
import { CopilotCorsConfig } from "./runtime/core/fetch-cors.cjs";
|
|
7
7
|
import { CopilotRuntimeHooks, ErrorHookContext, HandlerHookContext, HookContext, ResponseHookContext, RouteInfo } from "./runtime/core/hooks.cjs";
|
|
8
8
|
import { CopilotEndpointCorsConfig, createCopilotEndpoint, createCopilotHonoHandler, toFetchCorsConfig } from "./runtime/endpoints/hono.cjs";
|
|
@@ -16,5 +16,5 @@ import { CopilotRuntimeFetchHandler, CopilotRuntimeHandlerOptions, createCopilot
|
|
|
16
16
|
import { CopilotKitRequestHandler } from "./runtime/index.cjs";
|
|
17
17
|
import { convertAISDKStream } from "../agent/converters/aisdk.cjs";
|
|
18
18
|
import { TanStackChatMessage, TanStackInputResult, convertInputToTanStackAI, convertTanStackStream } from "../agent/converters/tanstack.cjs";
|
|
19
|
-
import {
|
|
20
|
-
export {
|
|
19
|
+
import { BasicAgent, BasicAgentConfiguration, BuiltInAgent, BuiltInAgentAISDKFactoryConfig, BuiltInAgentClassicConfig, BuiltInAgentConfiguration, BuiltInAgentCustomFactoryConfig, BuiltInAgentFactoryConfig, BuiltInAgentModel, BuiltInAgentTanStackFactoryConfig, MCPClientConfig, MCPClientConfigHTTP, MCPClientConfigSSE, MCPClientProvider, MessageConversionOptions, ModelSpecifier, OverridableProperty, ToolDefinition, convertJsonSchemaToZodSchema, convertMessagesToVercelAISDKMessages, convertToolDefinitionsToVercelAITools, convertToolsToVercelAITools, defineTool, resolveModel } from "../agent/index.cjs";
|
|
20
|
+
export { AgentRunner, AgentRunnerConnectRequest, AgentRunnerIsRunningRequest, AgentRunnerRunRequest, AgentRunnerStopRequest, AgentsConfig, AgentsFactory, BasicAgent, BasicAgentConfiguration, BuiltInAgent, BuiltInAgentAISDKFactoryConfig, BuiltInAgentClassicConfig, BuiltInAgentConfiguration, BuiltInAgentCustomFactoryConfig, BuiltInAgentFactoryConfig, BuiltInAgentModel, BuiltInAgentTanStackFactoryConfig, CopilotCorsConfig, CopilotEndpointCorsConfig, CopilotExpressEndpointParams, CopilotIntelligenceRuntime, CopilotIntelligenceRuntimeLike, CopilotIntelligenceRuntimeOptions, CopilotKitIntelligence, CopilotKitIntelligenceConfig, CopilotKitRequestHandler, CopilotRuntime, CopilotRuntimeFetchHandler, CopilotRuntimeHandlerOptions, CopilotRuntimeHooks, CopilotRuntimeLike, CopilotRuntimeOptions, CopilotRuntimeUser, CopilotSseRuntime, CopilotSseRuntimeLike, CopilotSseRuntimeOptions, CreateThreadRequest, ErrorHookContext, HandlerHookContext, HookContext, IdentifyUserCallback, InMemoryAgentRunner, IntelligenceAgentRunner, IntelligenceAgentRunnerOptions, ListThreadsResponse, MCPClientConfig, MCPClientConfigHTTP, MCPClientConfigSSE, MCPClientProvider, McpAppsConfig, McpAppsServerConfig, MessageConversionOptions, ModelSpecifier, OpenGenerativeUIConfig, OpenGenerativeUIOptions, OverridableProperty, ResponseHookContext, RouteInfo, SubscribeToThreadsRequest, SubscribeToThreadsResponse, TanStackChatMessage, TanStackInputResult, ThreadSummary, ToolDefinition, TranscribeFileOptions, TranscriptionService, UpdateThreadRequest, VERSION, convertAISDKStream, convertInputToTanStackAI, convertJsonSchemaToZodSchema, convertMessagesToVercelAISDKMessages, convertTanStackStream, convertToolDefinitionsToVercelAITools, convertToolsToVercelAITools, createCopilotEndpoint, createCopilotExpressHandler as createCopilotEndpointExpress, createCopilotExpressHandler, createCopilotEndpointSingleRoute, createCopilotEndpointSingleRouteExpress, createCopilotHonoHandler, createCopilotRuntimeHandler, defineTool, finalizeRunEvents, isIntelligenceRuntime, resolveAgents, resolveModel, toFetchCorsConfig };
|
package/dist/v2/index.d.mts
CHANGED
|
@@ -2,7 +2,7 @@ import "reflect-metadata";
|
|
|
2
2
|
import { TranscribeFileOptions, TranscriptionService } from "./runtime/transcription-service/transcription-service.mjs";
|
|
3
3
|
import { AgentRunner, AgentRunnerConnectRequest, AgentRunnerIsRunningRequest, AgentRunnerRunRequest, AgentRunnerStopRequest } from "./runtime/runner/agent-runner.mjs";
|
|
4
4
|
import { CopilotKitIntelligence, CopilotKitIntelligenceConfig, CreateThreadRequest, ListThreadsResponse, SubscribeToThreadsRequest, SubscribeToThreadsResponse, ThreadSummary, UpdateThreadRequest } from "./runtime/intelligence-platform/client.mjs";
|
|
5
|
-
import { CopilotIntelligenceRuntime, CopilotIntelligenceRuntimeLike, CopilotIntelligenceRuntimeOptions, CopilotRuntime, CopilotRuntimeLike, CopilotRuntimeOptions, CopilotRuntimeUser, CopilotSseRuntime, CopilotSseRuntimeLike, CopilotSseRuntimeOptions, IdentifyUserCallback, McpAppsConfig, McpAppsServerConfig, OpenGenerativeUIConfig, OpenGenerativeUIOptions, VERSION, isIntelligenceRuntime } from "./runtime/core/runtime.mjs";
|
|
5
|
+
import { AgentFactoryContext, AgentsConfig, AgentsFactory, CopilotIntelligenceRuntime, CopilotIntelligenceRuntimeLike, CopilotIntelligenceRuntimeOptions, CopilotRuntime, CopilotRuntimeLike, CopilotRuntimeOptions, CopilotRuntimeUser, CopilotSseRuntime, CopilotSseRuntimeLike, CopilotSseRuntimeOptions, IdentifyUserCallback, McpAppsConfig, McpAppsServerConfig, OpenGenerativeUIConfig, OpenGenerativeUIOptions, VERSION, isIntelligenceRuntime, resolveAgents } from "./runtime/core/runtime.mjs";
|
|
6
6
|
import { CopilotCorsConfig } from "./runtime/core/fetch-cors.mjs";
|
|
7
7
|
import { CopilotRuntimeHooks, ErrorHookContext, HandlerHookContext, HookContext, ResponseHookContext, RouteInfo } from "./runtime/core/hooks.mjs";
|
|
8
8
|
import { CopilotEndpointCorsConfig, createCopilotEndpoint, createCopilotHonoHandler, toFetchCorsConfig } from "./runtime/endpoints/hono.mjs";
|
|
@@ -16,5 +16,5 @@ import { CopilotRuntimeFetchHandler, CopilotRuntimeHandlerOptions, createCopilot
|
|
|
16
16
|
import { CopilotKitRequestHandler } from "./runtime/index.mjs";
|
|
17
17
|
import { convertAISDKStream } from "../agent/converters/aisdk.mjs";
|
|
18
18
|
import { TanStackChatMessage, TanStackInputResult, convertInputToTanStackAI, convertTanStackStream } from "../agent/converters/tanstack.mjs";
|
|
19
|
-
import {
|
|
20
|
-
export {
|
|
19
|
+
import { BasicAgent, BasicAgentConfiguration, BuiltInAgent, BuiltInAgentAISDKFactoryConfig, BuiltInAgentClassicConfig, BuiltInAgentConfiguration, BuiltInAgentCustomFactoryConfig, BuiltInAgentFactoryConfig, BuiltInAgentModel, BuiltInAgentTanStackFactoryConfig, MCPClientConfig, MCPClientConfigHTTP, MCPClientConfigSSE, MCPClientProvider, MessageConversionOptions, ModelSpecifier, OverridableProperty, ToolDefinition, convertJsonSchemaToZodSchema, convertMessagesToVercelAISDKMessages, convertToolDefinitionsToVercelAITools, convertToolsToVercelAITools, defineTool, resolveModel } from "../agent/index.mjs";
|
|
20
|
+
export { AgentRunner, AgentRunnerConnectRequest, AgentRunnerIsRunningRequest, AgentRunnerRunRequest, AgentRunnerStopRequest, AgentsConfig, AgentsFactory, BasicAgent, BasicAgentConfiguration, BuiltInAgent, BuiltInAgentAISDKFactoryConfig, BuiltInAgentClassicConfig, BuiltInAgentConfiguration, BuiltInAgentCustomFactoryConfig, BuiltInAgentFactoryConfig, BuiltInAgentModel, BuiltInAgentTanStackFactoryConfig, CopilotCorsConfig, CopilotEndpointCorsConfig, CopilotExpressEndpointParams, CopilotIntelligenceRuntime, CopilotIntelligenceRuntimeLike, CopilotIntelligenceRuntimeOptions, CopilotKitIntelligence, CopilotKitIntelligenceConfig, CopilotKitRequestHandler, CopilotRuntime, CopilotRuntimeFetchHandler, CopilotRuntimeHandlerOptions, CopilotRuntimeHooks, CopilotRuntimeLike, CopilotRuntimeOptions, CopilotRuntimeUser, CopilotSseRuntime, CopilotSseRuntimeLike, CopilotSseRuntimeOptions, CreateThreadRequest, ErrorHookContext, HandlerHookContext, HookContext, IdentifyUserCallback, InMemoryAgentRunner, IntelligenceAgentRunner, IntelligenceAgentRunnerOptions, ListThreadsResponse, MCPClientConfig, MCPClientConfigHTTP, MCPClientConfigSSE, MCPClientProvider, McpAppsConfig, McpAppsServerConfig, MessageConversionOptions, ModelSpecifier, OpenGenerativeUIConfig, OpenGenerativeUIOptions, OverridableProperty, ResponseHookContext, RouteInfo, SubscribeToThreadsRequest, SubscribeToThreadsResponse, TanStackChatMessage, TanStackInputResult, ThreadSummary, ToolDefinition, TranscribeFileOptions, TranscriptionService, UpdateThreadRequest, VERSION, convertAISDKStream, convertInputToTanStackAI, convertJsonSchemaToZodSchema, convertMessagesToVercelAISDKMessages, convertTanStackStream, convertToolDefinitionsToVercelAITools, convertToolsToVercelAITools, createCopilotEndpoint, createCopilotExpressHandler as createCopilotEndpointExpress, createCopilotExpressHandler, createCopilotEndpointSingleRoute, createCopilotEndpointSingleRouteExpress, createCopilotHonoHandler, createCopilotRuntimeHandler, defineTool, finalizeRunEvents, isIntelligenceRuntime, resolveAgents, resolveModel, toFetchCorsConfig };
|
package/dist/v2/index.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import "reflect-metadata";
|
|
|
2
2
|
import { AgentRunner } from "./runtime/runner/agent-runner.mjs";
|
|
3
3
|
import { InMemoryAgentRunner } from "./runtime/runner/in-memory.mjs";
|
|
4
4
|
import { IntelligenceAgentRunner } from "./runtime/runner/intelligence.mjs";
|
|
5
|
-
import { CopilotIntelligenceRuntime, CopilotRuntime, CopilotSseRuntime, VERSION, isIntelligenceRuntime } from "./runtime/core/runtime.mjs";
|
|
5
|
+
import { CopilotIntelligenceRuntime, CopilotRuntime, CopilotSseRuntime, VERSION, isIntelligenceRuntime, resolveAgents } from "./runtime/core/runtime.mjs";
|
|
6
6
|
import { CopilotKitIntelligence } from "./runtime/intelligence-platform/client.mjs";
|
|
7
7
|
import { createCopilotRuntimeHandler } from "./runtime/core/fetch-handler.mjs";
|
|
8
8
|
import { createCopilotEndpoint, createCopilotHonoHandler, toFetchCorsConfig } from "./runtime/endpoints/hono.mjs";
|
|
@@ -15,4 +15,4 @@ import { convertAISDKStream } from "../agent/converters/aisdk.mjs";
|
|
|
15
15
|
import { convertInputToTanStackAI, convertTanStackStream } from "../agent/converters/tanstack.mjs";
|
|
16
16
|
import { BasicAgent, BuiltInAgent, convertJsonSchemaToZodSchema, convertMessagesToVercelAISDKMessages, convertToolDefinitionsToVercelAITools, convertToolsToVercelAITools, defineTool, resolveModel } from "../agent/index.mjs";
|
|
17
17
|
|
|
18
|
-
export { AgentRunner, BasicAgent, BuiltInAgent, CopilotIntelligenceRuntime, CopilotKitIntelligence, CopilotRuntime, CopilotSseRuntime, InMemoryAgentRunner, IntelligenceAgentRunner, TranscriptionService, VERSION, convertAISDKStream, convertInputToTanStackAI, convertJsonSchemaToZodSchema, convertMessagesToVercelAISDKMessages, convertTanStackStream, convertToolDefinitionsToVercelAITools, convertToolsToVercelAITools, createCopilotEndpoint, createCopilotExpressHandler as createCopilotEndpointExpress, createCopilotExpressHandler, createCopilotEndpointSingleRoute, createCopilotEndpointSingleRouteExpress, createCopilotHonoHandler, createCopilotRuntimeHandler, defineTool, finalizeRunEvents, isIntelligenceRuntime, resolveModel, toFetchCorsConfig };
|
|
18
|
+
export { AgentRunner, BasicAgent, BuiltInAgent, CopilotIntelligenceRuntime, CopilotKitIntelligence, CopilotRuntime, CopilotSseRuntime, InMemoryAgentRunner, IntelligenceAgentRunner, TranscriptionService, VERSION, convertAISDKStream, convertInputToTanStackAI, convertJsonSchemaToZodSchema, convertMessagesToVercelAISDKMessages, convertTanStackStream, convertToolDefinitionsToVercelAITools, convertToolsToVercelAITools, createCopilotEndpoint, createCopilotExpressHandler as createCopilotEndpointExpress, createCopilotExpressHandler, createCopilotEndpointSingleRoute, createCopilotEndpointSingleRouteExpress, createCopilotHonoHandler, createCopilotRuntimeHandler, defineTool, finalizeRunEvents, isIntelligenceRuntime, resolveAgents, resolveModel, toFetchCorsConfig };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
require("reflect-metadata");
|
|
2
2
|
const require_runtime = require('../../../_virtual/_rolldown/runtime.cjs');
|
|
3
3
|
const require_package$1 = require('../../../package.cjs');
|
|
4
|
+
const require_logger = require('../../../lib/logger.cjs');
|
|
4
5
|
const require_in_memory = require('../runner/in-memory.cjs');
|
|
5
6
|
const require_intelligence = require('../runner/intelligence.cjs');
|
|
6
7
|
let _copilotkit_shared = require("@copilotkit/shared");
|
|
@@ -9,6 +10,18 @@ let _copilotkit_license_verifier = require("@copilotkit/license-verifier");
|
|
|
9
10
|
//#region src/v2/runtime/core/runtime.ts
|
|
10
11
|
var import_package = /* @__PURE__ */ require_runtime.__toESM(require_package$1.default);
|
|
11
12
|
const VERSION = import_package.default.version;
|
|
13
|
+
/**
|
|
14
|
+
* Resolve an AgentsConfig value to a concrete record of agents.
|
|
15
|
+
* If the config is a factory function, it is called with the given request context.
|
|
16
|
+
* Otherwise it is awaited directly (static record or Promise).
|
|
17
|
+
*/
|
|
18
|
+
async function resolveAgents(agents, request) {
|
|
19
|
+
if (typeof agents === "function") {
|
|
20
|
+
if (!request) throw new Error("Agent factory function requires a request context, but none was provided.");
|
|
21
|
+
return agents({ request });
|
|
22
|
+
}
|
|
23
|
+
return agents;
|
|
24
|
+
}
|
|
12
25
|
var BaseCopilotRuntime = class {
|
|
13
26
|
constructor(options, runner) {
|
|
14
27
|
const { agents, transcriptionService, beforeRequestMiddleware, afterRequestMiddleware, a2ui, mcpApps, openGenerativeUI } = options;
|
|
@@ -20,6 +33,11 @@ var BaseCopilotRuntime = class {
|
|
|
20
33
|
this.mcpApps = mcpApps;
|
|
21
34
|
this.openGenerativeUI = openGenerativeUI;
|
|
22
35
|
this.runner = runner;
|
|
36
|
+
this.debug = (0, _copilotkit_shared.resolveDebugConfig)(options.debug);
|
|
37
|
+
if (this.debug.enabled) this.debugLogger = require_logger.createLogger({
|
|
38
|
+
level: "debug",
|
|
39
|
+
component: "copilotkit-debug"
|
|
40
|
+
});
|
|
23
41
|
}
|
|
24
42
|
};
|
|
25
43
|
var CopilotSseRuntime = class extends BaseCopilotRuntime {
|
|
@@ -115,6 +133,12 @@ var CopilotRuntime = class {
|
|
|
115
133
|
get licenseChecker() {
|
|
116
134
|
return this.delegate.licenseChecker;
|
|
117
135
|
}
|
|
136
|
+
get debug() {
|
|
137
|
+
return this.delegate.debug;
|
|
138
|
+
}
|
|
139
|
+
get debugLogger() {
|
|
140
|
+
return this.delegate.debugLogger;
|
|
141
|
+
}
|
|
118
142
|
};
|
|
119
143
|
|
|
120
144
|
//#endregion
|
|
@@ -123,4 +147,5 @@ exports.CopilotRuntime = CopilotRuntime;
|
|
|
123
147
|
exports.CopilotSseRuntime = CopilotSseRuntime;
|
|
124
148
|
exports.VERSION = VERSION;
|
|
125
149
|
exports.isIntelligenceRuntime = isIntelligenceRuntime;
|
|
150
|
+
exports.resolveAgents = resolveAgents;
|
|
126
151
|
//# sourceMappingURL=runtime.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.cjs","names":["pkg","InMemoryAgentRunner","RUNTIME_MODE_SSE","IntelligenceAgentRunner","RUNTIME_MODE_INTELLIGENCE"],"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 licen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Y,SAAgC;AAC1C,OAAK,WAAW,uBAAuB,QAAQ,GAC3C,IAAI,2BAA2B,QAAQ,GACvC,IAAI,kBAAkB,QAAQ;;CAGpC,IAAI,SAA0C;AAC5C,SAAO,KAAK,SAAS;;CAGvB,IAAI,uBAAsE;AACxE,SAAO,KAAK,SAAS;;CAGvB,IAAI,0BAA4E;AAC9E,SAAO,KAAK,SAAS;;CAGvB,IAAI,yBAA0E;AAC5E,SAAO,KAAK,SAAS;;CAGvB,IAAI,SAAsB;AACxB,SAAO,KAAK,SAAS;;CAGvB,IAAI,OAAsC;AACxC,SAAO,KAAK,SAAS;;CAGvB,IAAI,UAA4C;AAC9C,SAAO,KAAK,SAAS;;CAGvB,IAAI,mBAA8D;AAChE,SAAO,KAAK,SAAS;;CAGvB,IAAI,eAAmD;AACrD,SAAO,KAAK,SAAS;;CAGvB,IAAI,sBAA2C;AAC7C,SAAO,sBAAsB,KAAK,SAAS,GACvC,KAAK,SAAS,sBACd;;CAGN,IAAI,eAAiD;AACnD,SAAO,sBAAsB,KAAK,SAAS,GACvC,KAAK,SAAS,eACd;;CAGN,IAAI,iBAAqC;AACvC,SAAO,sBAAsB,KAAK,SAAS,GACvC,KAAK,SAAS,iBACd;;CAGN,IAAI,gBAAoC;AACtC,SAAO,sBAAsB,KAAK,SAAS,GACvC,KAAK,SAAS,gBACd;;CAGN,IAAI,+BAAmD;AACrD,SAAO,sBAAsB,KAAK,SAAS,GACvC,KAAK,SAAS,+BACd;;CAGN,IAAI,OAAoB;AACtB,SAAO,KAAK,SAAS;;CAGvB,IAAI,iBAAiB;AACnB,SAAO,KAAK,SAAS"}
|
|
1
|
+
{"version":3,"file":"runtime.cjs","names":["pkg","createLogger","InMemoryAgentRunner","RUNTIME_MODE_SSE","IntelligenceAgentRunner","RUNTIME_MODE_INTELLIGENCE"],"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 {\n type ResolvedDebugConfig,\n resolveDebugConfig,\n type DebugConfig,\n} from \"@copilotkit/shared\";\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 { createLogger, type CopilotRuntimeLogger } from \"../../../lib/logger\";\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\n/**\n * Context passed to agent factory functions for per-request agent resolution.\n */\nexport interface AgentFactoryContext {\n /** The incoming HTTP request. */\n request: Request;\n}\n\n/**\n * A function that dynamically creates agents on a per-request basis.\n * Useful for multi-tenant scenarios or request-scoped agent configuration.\n */\nexport type AgentsFactory = (\n ctx: AgentFactoryContext,\n) => MaybePromise<NonEmptyRecord<Record<string, AbstractAgent>>>;\n\n/**\n * Agents can be provided as:\n * - A static record of agents\n * - A Promise that resolves to a record of agents\n * - A factory function that receives request context and returns agents (or a Promise of agents)\n */\nexport type AgentsConfig =\n | MaybePromise<NonEmptyRecord<Record<string, AbstractAgent>>>\n | AgentsFactory;\n\n/**\n * Resolve an AgentsConfig value to a concrete record of agents.\n * If the config is a factory function, it is called with the given request context.\n * Otherwise it is awaited directly (static record or Promise).\n */\nexport async function resolveAgents(\n agents: AgentsConfig,\n request?: Request,\n): Promise<Record<string, AbstractAgent>> {\n if (typeof agents === \"function\") {\n if (!request) {\n throw new Error(\n \"Agent factory function requires a request context, but none was provided.\",\n );\n }\n return agents({ request });\n }\n return agents;\n}\n\ninterface BaseCopilotRuntimeOptions extends CopilotRuntimeMiddlewares {\n /**\n * Map of available agents, or a factory function for per-request agent resolution.\n *\n * Static record:\n * ```ts\n * agents: { support: new SupportAgent(), technical: new TechnicalAgent() }\n * ```\n *\n * Factory function (called per-request):\n * ```ts\n * agents: ({ request }) => {\n * const tenantId = request.headers.get(\"x-tenant-id\");\n * return { default: createAgentForTenant(tenantId) };\n * }\n * ```\n */\n agents: AgentsConfig;\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 /** Enable debug logging for the event pipeline. */\n debug?: DebugConfig;\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 debug: ResolvedDebugConfig;\n debugLogger?: CopilotRuntimeLogger;\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 public debug: ResolvedDebugConfig;\n public debugLogger?: CopilotRuntimeLogger;\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 this.debug = resolveDebugConfig(options.debug);\n if (this.debug.enabled) {\n this.debugLogger = createLogger({\n level: \"debug\",\n component: \"copilotkit-debug\",\n });\n }\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 get debug(): ResolvedDebugConfig {\n return this.delegate.debug;\n }\n\n get debugLogger(): CopilotRuntimeLogger | undefined {\n return this.delegate.debugLogger;\n }\n}\n"],"mappings":";;;;;;;;;;;AA+BA,MAAa,UAAUA,uBAAI;;;;;;AAgE3B,eAAsB,cACpB,QACA,SACwC;AACxC,KAAI,OAAO,WAAW,YAAY;AAChC,MAAI,CAAC,QACH,OAAM,IAAI,MACR,4EACD;AAEH,SAAO,OAAO,EAAE,SAAS,CAAC;;AAE5B,QAAO;;AAuGT,IAAe,qBAAf,MAAgE;CAgB9D,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;AACd,OAAK,mDAA2B,QAAQ,MAAM;AAC9C,MAAI,KAAK,MAAM,QACb,MAAK,cAAcC,4BAAa;GAC9B,OAAO;GACP,WAAW;GACZ,CAAC;;;AAKR,IAAa,oBAAb,cACU,mBAEV;CAIE,YAAY,SAAmC;AAC7C,QAAM,SAAS,QAAQ,UAAU,IAAIC,uCAAqB,CAAC;sBAJrC;cACRC;;;AAOlB,IAAa,6BAAb,MAAa,mCACH,mBAEV;;8BAUyC;;;wCAEU;;CAEjD,YAAY,SAA4C;AACtD,QACE,SACA,IAAIC,6CAAwB;GAC1B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CAGN,IAAI,eAAiD;AACnD,SAAO,sBAAsB,KAAK,SAAS,GACvC,KAAK,SAAS,eACd;;CAGN,IAAI,iBAAqC;AACvC,SAAO,sBAAsB,KAAK,SAAS,GACvC,KAAK,SAAS,iBACd;;CAGN,IAAI,gBAAoC;AACtC,SAAO,sBAAsB,KAAK,SAAS,GACvC,KAAK,SAAS,gBACd;;CAGN,IAAI,+BAAmD;AACrD,SAAO,sBAAsB,KAAK,SAAS,GACvC,KAAK,SAAS,+BACd;;CAGN,IAAI,OAAoB;AACtB,SAAO,KAAK,SAAS;;CAGvB,IAAI,iBAAiB;AACnB,SAAO,KAAK,SAAS;;CAGvB,IAAI,QAA6B;AAC/B,SAAO,KAAK,SAAS;;CAGvB,IAAI,cAAgD;AAClD,SAAO,KAAK,SAAS"}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
|
|
2
2
|
import { AfterRequestMiddleware, BeforeRequestMiddleware } from "./middleware.cjs";
|
|
3
|
+
import { CopilotRuntimeLogger } from "../../../lib/logger.cjs";
|
|
3
4
|
import { TranscriptionService } from "../transcription-service/transcription-service.cjs";
|
|
4
5
|
import { AgentRunner } from "../runner/agent-runner.cjs";
|
|
5
6
|
import { CopilotKitIntelligence } from "../intelligence-platform/client.cjs";
|
|
6
|
-
import { MaybePromise, NonEmptyRecord, RuntimeMode } from "@copilotkit/shared";
|
|
7
|
+
import { DebugConfig, MaybePromise, NonEmptyRecord, ResolvedDebugConfig, RuntimeMode } from "@copilotkit/shared";
|
|
7
8
|
import { LicenseChecker } from "@copilotkit/license-verifier";
|
|
8
9
|
import { AbstractAgent } from "@ag-ui/client";
|
|
9
10
|
import { MCPClientConfig } from "@ag-ui/mcp-apps-middleware";
|
|
@@ -35,9 +36,49 @@ interface CopilotRuntimeMiddlewares {
|
|
|
35
36
|
/** Auto-apply OpenGenerativeUIMiddleware to agents at run time. */
|
|
36
37
|
openGenerativeUI?: OpenGenerativeUIConfig;
|
|
37
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Context passed to agent factory functions for per-request agent resolution.
|
|
41
|
+
*/
|
|
42
|
+
interface AgentFactoryContext {
|
|
43
|
+
/** The incoming HTTP request. */
|
|
44
|
+
request: Request;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* A function that dynamically creates agents on a per-request basis.
|
|
48
|
+
* Useful for multi-tenant scenarios or request-scoped agent configuration.
|
|
49
|
+
*/
|
|
50
|
+
type AgentsFactory = (ctx: AgentFactoryContext) => MaybePromise<NonEmptyRecord<Record<string, AbstractAgent>>>;
|
|
51
|
+
/**
|
|
52
|
+
* Agents can be provided as:
|
|
53
|
+
* - A static record of agents
|
|
54
|
+
* - A Promise that resolves to a record of agents
|
|
55
|
+
* - A factory function that receives request context and returns agents (or a Promise of agents)
|
|
56
|
+
*/
|
|
57
|
+
type AgentsConfig = MaybePromise<NonEmptyRecord<Record<string, AbstractAgent>>> | AgentsFactory;
|
|
58
|
+
/**
|
|
59
|
+
* Resolve an AgentsConfig value to a concrete record of agents.
|
|
60
|
+
* If the config is a factory function, it is called with the given request context.
|
|
61
|
+
* Otherwise it is awaited directly (static record or Promise).
|
|
62
|
+
*/
|
|
63
|
+
declare function resolveAgents(agents: AgentsConfig, request?: Request): Promise<Record<string, AbstractAgent>>;
|
|
38
64
|
interface BaseCopilotRuntimeOptions extends CopilotRuntimeMiddlewares {
|
|
39
|
-
/**
|
|
40
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Map of available agents, or a factory function for per-request agent resolution.
|
|
67
|
+
*
|
|
68
|
+
* Static record:
|
|
69
|
+
* ```ts
|
|
70
|
+
* agents: { support: new SupportAgent(), technical: new TechnicalAgent() }
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* Factory function (called per-request):
|
|
74
|
+
* ```ts
|
|
75
|
+
* agents: ({ request }) => {
|
|
76
|
+
* const tenantId = request.headers.get("x-tenant-id");
|
|
77
|
+
* return { default: createAgentForTenant(tenantId) };
|
|
78
|
+
* }
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
agents: AgentsConfig;
|
|
41
82
|
/** Optional transcription service for audio processing. */
|
|
42
83
|
transcriptionService?: TranscriptionService;
|
|
43
84
|
/** Optional *before* middleware – callback function or webhook URL. */
|
|
@@ -46,6 +87,8 @@ interface BaseCopilotRuntimeOptions extends CopilotRuntimeMiddlewares {
|
|
|
46
87
|
afterRequestMiddleware?: AfterRequestMiddleware;
|
|
47
88
|
/** Signed license token for server-side feature verification. Falls back to COPILOTKIT_LICENSE_TOKEN env var. */
|
|
48
89
|
licenseToken?: string;
|
|
90
|
+
/** Enable debug logging for the event pipeline. */
|
|
91
|
+
debug?: DebugConfig;
|
|
49
92
|
}
|
|
50
93
|
interface CopilotRuntimeUser {
|
|
51
94
|
id: string;
|
|
@@ -89,6 +132,8 @@ interface CopilotRuntimeLike {
|
|
|
89
132
|
identifyUser?: IdentifyUserCallback;
|
|
90
133
|
mode: RuntimeMode;
|
|
91
134
|
licenseChecker?: LicenseChecker;
|
|
135
|
+
debug: ResolvedDebugConfig;
|
|
136
|
+
debugLogger?: CopilotRuntimeLogger;
|
|
92
137
|
}
|
|
93
138
|
interface CopilotSseRuntimeLike extends CopilotRuntimeLike {
|
|
94
139
|
intelligence?: undefined;
|
|
@@ -113,6 +158,8 @@ declare abstract class BaseCopilotRuntime implements CopilotRuntimeLike {
|
|
|
113
158
|
mcpApps: CopilotRuntimeOptions["mcpApps"];
|
|
114
159
|
openGenerativeUI: CopilotRuntimeOptions["openGenerativeUI"];
|
|
115
160
|
licenseChecker?: LicenseChecker;
|
|
161
|
+
debug: ResolvedDebugConfig;
|
|
162
|
+
debugLogger?: CopilotRuntimeLogger;
|
|
116
163
|
abstract readonly intelligence?: CopilotKitIntelligence;
|
|
117
164
|
abstract readonly mode: RuntimeMode;
|
|
118
165
|
constructor(options: BaseCopilotRuntimeOptions, runner: AgentRunner);
|
|
@@ -160,7 +207,9 @@ declare class CopilotRuntime implements CopilotRuntimeLike {
|
|
|
160
207
|
get lockHeartbeatIntervalSeconds(): number | undefined;
|
|
161
208
|
get mode(): RuntimeMode;
|
|
162
209
|
get licenseChecker(): LicenseChecker;
|
|
210
|
+
get debug(): ResolvedDebugConfig;
|
|
211
|
+
get debugLogger(): CopilotRuntimeLogger | undefined;
|
|
163
212
|
}
|
|
164
213
|
//#endregion
|
|
165
|
-
export { CopilotIntelligenceRuntime, CopilotIntelligenceRuntimeLike, CopilotIntelligenceRuntimeOptions, CopilotRuntime, CopilotRuntimeLike, CopilotRuntimeOptions, CopilotRuntimeUser, CopilotSseRuntime, CopilotSseRuntimeLike, CopilotSseRuntimeOptions, IdentifyUserCallback, McpAppsConfig, McpAppsServerConfig, OpenGenerativeUIConfig, OpenGenerativeUIOptions, VERSION, isIntelligenceRuntime };
|
|
214
|
+
export { AgentFactoryContext, AgentsConfig, AgentsFactory, CopilotIntelligenceRuntime, CopilotIntelligenceRuntimeLike, CopilotIntelligenceRuntimeOptions, CopilotRuntime, CopilotRuntimeLike, CopilotRuntimeOptions, CopilotRuntimeUser, CopilotSseRuntime, CopilotSseRuntimeLike, CopilotSseRuntimeOptions, IdentifyUserCallback, McpAppsConfig, McpAppsServerConfig, OpenGenerativeUIConfig, OpenGenerativeUIOptions, VERSION, isIntelligenceRuntime, resolveAgents };
|
|
166
215
|
//# sourceMappingURL=runtime.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.d.cts","names":[],"sources":["../../../../src/v2/runtime/core/runtime.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"runtime.d.cts","names":[],"sources":["../../../../src/v2/runtime/core/runtime.ts"],"mappings":";;;;;;;;;;;;;cA+Ba,OAAA;AAAA,UAEH,mCAAA;EAFwB;EAIhC,MAAA;AAAA;AAAA,KAGU,mBAAA,GAAsB,eAAA;EALxB,uFAOR,OAAA;AAAA;AAAA,UAGe,aAAA;EART;EAUN,OAAA,EAAS,mBAAA;AAAA;AAAA,UAGM,uBAAA,SAA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AAA,SAEF,YAAA,EAAc,sBAAA;EAAA,SACd,YAAA,EAAc,oBAAA;EAAA,SACd,mBAAA;EAAA,SACA,cAAA;EAAA,SACA,aAAA;EAAA,SACA,4BAAA;EAAA,SACA,IAAA;EA1HgD;EAAA,gBA6HzC,oBAAA;EA7HkE;EAAA,gBA+HlE,8BAAA;cAEJ,OAAA,EAAS,iCAAA;AAAA;AAAA,iBAgCP,qBAAA,CACd,OAAA,EAAS,kBAAA,GACR,OAAA,IAAW,8BAAA;;;;;cAQD,cAAA,YAA0B,kBAAA;EAAA,QAC7B,QAAA;cAEI,OAAA,EAAS,qBAAA;EAAA,IAMjB,MAAA,CAAA,GAAU,qBAAA;EAAA,IAIV,oBAAA,CAAA,GAAwB,qBAAA;EAAA,IAIxB,uBAAA,CAAA,GAA2B,qBAAA;EAAA,IAI3B,sBAAA,CAAA,GAA0B,qBAAA;EAAA,IAI1B,MAAA,CAAA,GAAU,WAAA;EAAA,IAIV,IAAA,CAAA,GAAQ,qBAAA;EAAA,IAIR,OAAA,CAAA,GAAW,qBAAA;EAAA,IAIX,gBAAA,CAAA,GAAoB,qBAAA;EAAA,IAIpB,YAAA,CAAA,GAAgB,sBAAA;EAAA,IAIhB,mBAAA,CAAA;EAAA,IAMA,YAAA,CAAA,GAAgB,oBAAA;EAAA,IAMhB,cAAA,CAAA;EAAA,IAMA,aAAA,CAAA;EAAA,IAMA,4BAAA,CAAA;EAAA,IAMA,IAAA,CAAA,GAAQ,WAAA;EAAA,IAIR,cAAA,CAAA,GAAc,cAAA;EAAA,IAId,KAAA,CAAA,GAAS,mBAAA;EAAA,IAIT,WAAA,CAAA,GAAe,oBAAA;AAAA"}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
2
|
import { AfterRequestMiddleware, BeforeRequestMiddleware } from "./middleware.mjs";
|
|
3
|
+
import { CopilotRuntimeLogger } from "../../../lib/logger.mjs";
|
|
3
4
|
import { TranscriptionService } from "../transcription-service/transcription-service.mjs";
|
|
4
5
|
import { AgentRunner } from "../runner/agent-runner.mjs";
|
|
5
6
|
import { CopilotKitIntelligence } from "../intelligence-platform/client.mjs";
|
|
6
7
|
import "../intelligence-platform/index.mjs";
|
|
7
|
-
import { MaybePromise, NonEmptyRecord, RuntimeMode } from "@copilotkit/shared";
|
|
8
|
+
import { DebugConfig, MaybePromise, NonEmptyRecord, ResolvedDebugConfig, RuntimeMode } from "@copilotkit/shared";
|
|
8
9
|
import { LicenseChecker } from "@copilotkit/license-verifier";
|
|
9
10
|
import { AbstractAgent } from "@ag-ui/client";
|
|
10
11
|
import { A2UIMiddlewareConfig } from "@ag-ui/a2ui-middleware";
|
|
@@ -36,9 +37,49 @@ interface CopilotRuntimeMiddlewares {
|
|
|
36
37
|
/** Auto-apply OpenGenerativeUIMiddleware to agents at run time. */
|
|
37
38
|
openGenerativeUI?: OpenGenerativeUIConfig;
|
|
38
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Context passed to agent factory functions for per-request agent resolution.
|
|
42
|
+
*/
|
|
43
|
+
interface AgentFactoryContext {
|
|
44
|
+
/** The incoming HTTP request. */
|
|
45
|
+
request: Request;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* A function that dynamically creates agents on a per-request basis.
|
|
49
|
+
* Useful for multi-tenant scenarios or request-scoped agent configuration.
|
|
50
|
+
*/
|
|
51
|
+
type AgentsFactory = (ctx: AgentFactoryContext) => MaybePromise<NonEmptyRecord<Record<string, AbstractAgent>>>;
|
|
52
|
+
/**
|
|
53
|
+
* Agents can be provided as:
|
|
54
|
+
* - A static record of agents
|
|
55
|
+
* - A Promise that resolves to a record of agents
|
|
56
|
+
* - A factory function that receives request context and returns agents (or a Promise of agents)
|
|
57
|
+
*/
|
|
58
|
+
type AgentsConfig = MaybePromise<NonEmptyRecord<Record<string, AbstractAgent>>> | AgentsFactory;
|
|
59
|
+
/**
|
|
60
|
+
* Resolve an AgentsConfig value to a concrete record of agents.
|
|
61
|
+
* If the config is a factory function, it is called with the given request context.
|
|
62
|
+
* Otherwise it is awaited directly (static record or Promise).
|
|
63
|
+
*/
|
|
64
|
+
declare function resolveAgents(agents: AgentsConfig, request?: Request): Promise<Record<string, AbstractAgent>>;
|
|
39
65
|
interface BaseCopilotRuntimeOptions extends CopilotRuntimeMiddlewares {
|
|
40
|
-
/**
|
|
41
|
-
|
|
66
|
+
/**
|
|
67
|
+
* Map of available agents, or a factory function for per-request agent resolution.
|
|
68
|
+
*
|
|
69
|
+
* Static record:
|
|
70
|
+
* ```ts
|
|
71
|
+
* agents: { support: new SupportAgent(), technical: new TechnicalAgent() }
|
|
72
|
+
* ```
|
|
73
|
+
*
|
|
74
|
+
* Factory function (called per-request):
|
|
75
|
+
* ```ts
|
|
76
|
+
* agents: ({ request }) => {
|
|
77
|
+
* const tenantId = request.headers.get("x-tenant-id");
|
|
78
|
+
* return { default: createAgentForTenant(tenantId) };
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
agents: AgentsConfig;
|
|
42
83
|
/** Optional transcription service for audio processing. */
|
|
43
84
|
transcriptionService?: TranscriptionService;
|
|
44
85
|
/** Optional *before* middleware – callback function or webhook URL. */
|
|
@@ -47,6 +88,8 @@ interface BaseCopilotRuntimeOptions extends CopilotRuntimeMiddlewares {
|
|
|
47
88
|
afterRequestMiddleware?: AfterRequestMiddleware;
|
|
48
89
|
/** Signed license token for server-side feature verification. Falls back to COPILOTKIT_LICENSE_TOKEN env var. */
|
|
49
90
|
licenseToken?: string;
|
|
91
|
+
/** Enable debug logging for the event pipeline. */
|
|
92
|
+
debug?: DebugConfig;
|
|
50
93
|
}
|
|
51
94
|
interface CopilotRuntimeUser {
|
|
52
95
|
id: string;
|
|
@@ -90,6 +133,8 @@ interface CopilotRuntimeLike {
|
|
|
90
133
|
identifyUser?: IdentifyUserCallback;
|
|
91
134
|
mode: RuntimeMode;
|
|
92
135
|
licenseChecker?: LicenseChecker;
|
|
136
|
+
debug: ResolvedDebugConfig;
|
|
137
|
+
debugLogger?: CopilotRuntimeLogger;
|
|
93
138
|
}
|
|
94
139
|
interface CopilotSseRuntimeLike extends CopilotRuntimeLike {
|
|
95
140
|
intelligence?: undefined;
|
|
@@ -114,6 +159,8 @@ declare abstract class BaseCopilotRuntime implements CopilotRuntimeLike {
|
|
|
114
159
|
mcpApps: CopilotRuntimeOptions["mcpApps"];
|
|
115
160
|
openGenerativeUI: CopilotRuntimeOptions["openGenerativeUI"];
|
|
116
161
|
licenseChecker?: LicenseChecker;
|
|
162
|
+
debug: ResolvedDebugConfig;
|
|
163
|
+
debugLogger?: CopilotRuntimeLogger;
|
|
117
164
|
abstract readonly intelligence?: CopilotKitIntelligence;
|
|
118
165
|
abstract readonly mode: RuntimeMode;
|
|
119
166
|
constructor(options: BaseCopilotRuntimeOptions, runner: AgentRunner);
|
|
@@ -161,7 +208,9 @@ declare class CopilotRuntime implements CopilotRuntimeLike {
|
|
|
161
208
|
get lockHeartbeatIntervalSeconds(): number | undefined;
|
|
162
209
|
get mode(): RuntimeMode;
|
|
163
210
|
get licenseChecker(): LicenseChecker;
|
|
211
|
+
get debug(): ResolvedDebugConfig;
|
|
212
|
+
get debugLogger(): CopilotRuntimeLogger | undefined;
|
|
164
213
|
}
|
|
165
214
|
//#endregion
|
|
166
|
-
export { CopilotIntelligenceRuntime, CopilotIntelligenceRuntimeLike, CopilotIntelligenceRuntimeOptions, CopilotRuntime, CopilotRuntimeLike, CopilotRuntimeOptions, CopilotRuntimeUser, CopilotSseRuntime, CopilotSseRuntimeLike, CopilotSseRuntimeOptions, IdentifyUserCallback, McpAppsConfig, McpAppsServerConfig, OpenGenerativeUIConfig, OpenGenerativeUIOptions, VERSION, isIntelligenceRuntime };
|
|
215
|
+
export { AgentFactoryContext, AgentsConfig, AgentsFactory, CopilotIntelligenceRuntime, CopilotIntelligenceRuntimeLike, CopilotIntelligenceRuntimeOptions, CopilotRuntime, CopilotRuntimeLike, CopilotRuntimeOptions, CopilotRuntimeUser, CopilotSseRuntime, CopilotSseRuntimeLike, CopilotSseRuntimeOptions, IdentifyUserCallback, McpAppsConfig, McpAppsServerConfig, OpenGenerativeUIConfig, OpenGenerativeUIOptions, VERSION, isIntelligenceRuntime, resolveAgents };
|
|
167
216
|
//# sourceMappingURL=runtime.d.mts.map
|