@copilotkit/runtime 1.55.1 → 1.55.2-next.1

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.
Files changed (74) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/agent/converters/aisdk.cjs +215 -0
  3. package/dist/agent/converters/aisdk.cjs.map +1 -0
  4. package/dist/agent/converters/aisdk.d.cts +18 -0
  5. package/dist/agent/converters/aisdk.d.cts.map +1 -0
  6. package/dist/agent/converters/aisdk.d.mts +18 -0
  7. package/dist/agent/converters/aisdk.d.mts.map +1 -0
  8. package/dist/agent/converters/aisdk.mjs +214 -0
  9. package/dist/agent/converters/aisdk.mjs.map +1 -0
  10. package/dist/agent/converters/index.d.mts +3 -0
  11. package/dist/agent/converters/tanstack.cjs +180 -0
  12. package/dist/agent/converters/tanstack.cjs.map +1 -0
  13. package/dist/agent/converters/tanstack.d.cts +68 -0
  14. package/dist/agent/converters/tanstack.d.cts.map +1 -0
  15. package/dist/agent/converters/tanstack.d.mts +68 -0
  16. package/dist/agent/converters/tanstack.d.mts.map +1 -0
  17. package/dist/agent/converters/tanstack.mjs +178 -0
  18. package/dist/agent/converters/tanstack.mjs.map +1 -0
  19. package/dist/agent/index.cjs +111 -17
  20. package/dist/agent/index.cjs.map +1 -1
  21. package/dist/agent/index.d.cts +61 -4
  22. package/dist/agent/index.d.cts.map +1 -1
  23. package/dist/agent/index.d.mts +62 -4
  24. package/dist/agent/index.d.mts.map +1 -1
  25. package/dist/agent/index.mjs +111 -17
  26. package/dist/agent/index.mjs.map +1 -1
  27. package/dist/lib/integrations/nextjs/pages-router.cjs.map +1 -1
  28. package/dist/lib/integrations/nextjs/pages-router.d.cts.map +1 -1
  29. package/dist/lib/integrations/nextjs/pages-router.d.mts.map +1 -1
  30. package/dist/lib/integrations/nextjs/pages-router.mjs.map +1 -1
  31. package/dist/lib/runtime/copilot-runtime.cjs +4 -2
  32. package/dist/lib/runtime/copilot-runtime.cjs.map +1 -1
  33. package/dist/lib/runtime/copilot-runtime.d.cts.map +1 -1
  34. package/dist/lib/runtime/copilot-runtime.d.mts.map +1 -1
  35. package/dist/lib/runtime/copilot-runtime.mjs +4 -2
  36. package/dist/lib/runtime/copilot-runtime.mjs.map +1 -1
  37. package/dist/lib/runtime/mcp-tools-utils.cjs +1 -1
  38. package/dist/lib/runtime/mcp-tools-utils.cjs.map +1 -1
  39. package/dist/lib/runtime/mcp-tools-utils.mjs +1 -1
  40. package/dist/lib/runtime/mcp-tools-utils.mjs.map +1 -1
  41. package/dist/package.cjs +3 -2
  42. package/dist/package.mjs +3 -2
  43. package/dist/service-adapters/anthropic/utils.cjs +1 -1
  44. package/dist/service-adapters/anthropic/utils.cjs.map +1 -1
  45. package/dist/service-adapters/anthropic/utils.mjs +1 -1
  46. package/dist/service-adapters/anthropic/utils.mjs.map +1 -1
  47. package/dist/service-adapters/openai/utils.cjs +1 -1
  48. package/dist/service-adapters/openai/utils.cjs.map +1 -1
  49. package/dist/service-adapters/openai/utils.mjs +1 -1
  50. package/dist/service-adapters/openai/utils.mjs.map +1 -1
  51. package/dist/v2/index.cjs +5 -0
  52. package/dist/v2/index.d.cts +4 -2
  53. package/dist/v2/index.d.mts +4 -2
  54. package/dist/v2/index.mjs +3 -1
  55. package/package.json +4 -3
  56. package/src/agent/__tests__/agent-test-helpers.ts +446 -0
  57. package/src/agent/__tests__/agent.test.ts +593 -0
  58. package/src/agent/__tests__/converter-aisdk.test.ts +692 -0
  59. package/src/agent/__tests__/converter-custom.test.ts +319 -0
  60. package/src/agent/__tests__/converter-tanstack-input.test.ts +211 -0
  61. package/src/agent/__tests__/converter-tanstack.test.ts +314 -0
  62. package/src/agent/__tests__/mcp-servers-integration.test.ts +373 -0
  63. package/src/agent/__tests__/multimodal-tanstack.test.ts +284 -0
  64. package/src/agent/__tests__/test-helpers.ts +12 -8
  65. package/src/agent/converters/aisdk.ts +326 -0
  66. package/src/agent/converters/index.ts +7 -0
  67. package/src/agent/converters/tanstack.ts +286 -0
  68. package/src/agent/index.ts +245 -26
  69. package/src/lib/integrations/nextjs/pages-router.ts +1 -0
  70. package/src/lib/runtime/copilot-runtime.ts +21 -12
  71. package/src/lib/runtime/mcp-tools-utils.ts +1 -1
  72. package/src/service-adapters/anthropic/utils.ts +1 -1
  73. package/src/service-adapters/openai/utils.ts +1 -1
  74. package/src/v2/runtime/__tests__/mcp-apps-middleware-integration.test.ts +275 -0
@@ -1 +1 @@
1
- {"version":3,"file":"copilot-runtime.mjs","names":["CopilotRuntimeVNext"],"sources":["../../../src/lib/runtime/copilot-runtime.ts"],"sourcesContent":["/**\n * <Callout type=\"info\">\n * This is the reference for the `CopilotRuntime` class. For more information and example code snippets, please see [Concept: Copilot Runtime](/concepts/copilot-runtime).\n * </Callout>\n *\n * ## Usage\n *\n * ```tsx\n * import { CopilotRuntime } from \"@copilotkit/runtime\";\n *\n * const copilotKit = new CopilotRuntime();\n * ```\n */\n\nimport {\n type Action,\n type CopilotErrorHandler,\n CopilotKitMisuseError,\n type MaybePromise,\n type NonEmptyRecord,\n type Parameter,\n readBody,\n getZodParameters,\n type PartialBy,\n isTelemetryDisabled,\n} from \"@copilotkit/shared\";\nimport type { RunAgentInput } from \"@ag-ui/core\";\nimport { aguiToGQL } from \"../../graphql/message-conversion/agui-to-gql\";\nimport type {\n CopilotServiceAdapter,\n RemoteChainParameters,\n} from \"../../service-adapters\";\nimport {\n CopilotRuntime as CopilotRuntimeVNext,\n type CopilotRuntimeOptions,\n type CopilotRuntimeOptions as CopilotRuntimeOptionsVNext,\n type AgentRunner,\n InMemoryAgentRunner,\n} from \"../../v2/runtime\";\nimport { TelemetryAgentRunner } from \"./telemetry-agent-runner\";\nimport telemetry from \"../telemetry-client\";\n\nimport type { MessageInput } from \"../../graphql/inputs/message.input\";\nimport type { Message } from \"../../graphql/types/converted\";\n\nimport {\n EndpointType,\n type EndpointDefinition,\n type CopilotKitEndpoint,\n type LangGraphPlatformEndpoint,\n} from \"./types\";\n\nimport type {\n CopilotObservabilityConfig,\n LLMRequestData,\n LLMResponseData,\n} from \"../observability\";\nimport type { AbstractAgent } from \"@ag-ui/client\";\n\n// +++ MCP Imports +++\nimport {\n type MCPClient,\n type MCPEndpointConfig,\n type MCPTool,\n extractParametersFromSchema,\n} from \"./mcp-tools-utils\";\nimport { BuiltInAgent, type BuiltInAgentConfiguration } from \"../../agent\";\n// Define the function type alias here or import if defined elsewhere\ntype CreateMCPClientFunction = (\n config: MCPEndpointConfig,\n) => Promise<MCPClient>;\n\ntype ActionsConfiguration<T extends Parameter[] | [] = []> =\n | Action<T>[]\n | ((ctx: { properties: any; url?: string }) => Action<T>[]);\n\ninterface OnBeforeRequestOptions {\n threadId?: string;\n runId?: string;\n inputMessages: Message[];\n properties: any;\n url?: string;\n}\n\ntype OnBeforeRequestHandler = (\n options: OnBeforeRequestOptions,\n) => void | Promise<void>;\n\ninterface OnAfterRequestOptions {\n threadId: string;\n runId?: string;\n inputMessages: Message[];\n outputMessages: Message[];\n properties: any;\n url?: string;\n}\n\ntype OnAfterRequestHandler = (\n options: OnAfterRequestOptions,\n) => void | Promise<void>;\n\ninterface OnStopGenerationOptions {\n threadId: string;\n runId?: string;\n url?: string;\n agentName?: string;\n lastMessage: MessageInput;\n}\ntype OnStopGenerationHandler = (\n options: OnStopGenerationOptions,\n) => void | Promise<void>;\n\ninterface Middleware {\n /**\n * A function that is called before the request is processed.\n */\n /**\n * @deprecated This middleware hook is deprecated and will be removed in a future version.\n * Use updated middleware integration methods in CopilotRuntimeVNext instead.\n */\n onBeforeRequest?: OnBeforeRequestHandler;\n\n /**\n * A function that is called after the request is processed.\n */\n /**\n * @deprecated This middleware hook is deprecated and will be removed in a future version.\n * Use updated middleware integration methods in CopilotRuntimeVNext instead.\n */\n onAfterRequest?: OnAfterRequestHandler;\n}\n\nexport interface CopilotRuntimeConstructorParams_BASE<\n T extends Parameter[] | [] = [],\n> {\n /**\n * Middleware to be used by the runtime.\n *\n * ```ts\n * onBeforeRequest: (options: {\n * threadId?: string;\n * runId?: string;\n * inputMessages: Message[];\n * properties: any;\n * }) => void | Promise<void>;\n * ```\n *\n * ```ts\n * onAfterRequest: (options: {\n * threadId?: string;\n * runId?: string;\n * inputMessages: Message[];\n * outputMessages: Message[];\n * properties: any;\n * }) => void | Promise<void>;\n * ```\n */\n /**\n * @deprecated This middleware hook is deprecated and will be removed in a future version.\n * Use updated middleware integration methods in CopilotRuntimeVNext instead.\n */\n middleware?: Middleware;\n\n /*\n * A list of server side actions that can be executed. Will be ignored when remoteActions are set\n */\n actions?: ActionsConfiguration<T>;\n\n /*\n * Deprecated: Use `remoteEndpoints`.\n */\n remoteActions?: CopilotKitEndpoint[];\n\n /*\n * A list of remote actions that can be executed.\n */\n remoteEndpoints?: EndpointDefinition[];\n\n /*\n * An array of LangServer URLs.\n */\n langserve?: RemoteChainParameters[];\n\n /**\n * Optional agent runner to use for SSE runtime.\n */\n runner?: AgentRunner;\n\n /*\n * A map of agent names to AGUI agents.\n * Example agent config:\n * ```ts\n * import { AbstractAgent } from \"@ag-ui/client\";\n * // ...\n * agents: {\n * \"support\": new CustomerSupportAgent(),\n * \"technical\": new TechnicalAgent()\n * }\n * ```\n */\n agents?: Record<string, AbstractAgent>;\n\n /*\n * Delegates agent state processing to the service adapter.\n *\n * When enabled, individual agent state requests will not be processed by the agent itself.\n * Instead, all processing will be handled by the service adapter.\n */\n delegateAgentProcessingToServiceAdapter?: boolean;\n\n /**\n * Configuration for LLM request/response logging.\n * Requires publicApiKey from CopilotKit component to be set:\n *\n * ```tsx\n * <CopilotKit publicApiKey=\"ck_pub_...\" />\n * ```\n *\n * Example logging config:\n * ```ts\n * logging: {\n * enabled: true, // Enable or disable logging\n * progressive: true, // Set to false for buffered logging\n * logger: {\n * logRequest: (data) => langfuse.trace({ name: \"LLM Request\", input: data }),\n * logResponse: (data) => langfuse.trace({ name: \"LLM Response\", output: data }),\n * logError: (errorData) => langfuse.trace({ name: \"LLM Error\", metadata: errorData }),\n * },\n * }\n * ```\n */\n observability_c?: CopilotObservabilityConfig;\n\n /**\n * Configuration for connecting to Model Context Protocol (MCP) servers.\n * Allows fetching and using tools defined on external MCP-compliant servers.\n * Requires providing the `createMCPClient` function during instantiation.\n * @experimental\n */\n mcpServers?: MCPEndpointConfig[];\n\n /**\n * A function that creates an MCP client instance for a given endpoint configuration.\n * This function is responsible for using the appropriate MCP client library\n * (e.g., `@copilotkit/runtime`, `ai`) to establish a connection.\n * Required if `mcpServers` is provided.\n *\n * ```typescript\n * import { experimental_createMCPClient } from \"ai\"; // Import from vercel ai library\n * // ...\n * const runtime = new CopilotRuntime({\n * mcpServers: [{ endpoint: \"...\" }],\n * async createMCPClient(config) {\n * return await experimental_createMCPClient({\n * transport: {\n * type: \"sse\",\n * url: config.endpoint,\n * headers: config.apiKey\n * ? { Authorization: `Bearer ${config.apiKey}` }\n * : undefined,\n * },\n * });\n * }\n * });\n * ```\n */\n createMCPClient?: CreateMCPClientFunction;\n\n /**\n * Optional error handler for comprehensive debugging and observability.\n *\n * **Requires publicApiKey**: Error handling only works when requests include a valid publicApiKey.\n * This is a premium Copilot Cloud feature.\n *\n * @param errorEvent - Structured error event with rich debugging context\n *\n * @example\n * ```typescript\n * const runtime = new CopilotRuntime({\n * onError: (errorEvent) => {\n * debugDashboard.capture(errorEvent);\n * }\n * });\n * ```\n */\n onError?: CopilotErrorHandler;\n\n onStopGeneration?: OnStopGenerationHandler;\n\n // /** Optional transcription service for audio processing. */\n // transcriptionService?: CopilotRuntimeOptionsVNext[\"transcriptionService\"];\n // /** Optional *before* middleware – callback function or webhook URL. */\n // beforeRequestMiddleware?: CopilotRuntimeOptionsVNext[\"beforeRequestMiddleware\"];\n // /** Optional *after* middleware – callback function or webhook URL. */\n // afterRequestMiddleware?: CopilotRuntimeOptionsVNext[\"afterRequestMiddleware\"];\n}\n\ntype BeforeRequestMiddleware =\n CopilotRuntimeOptionsVNext[\"beforeRequestMiddleware\"];\ntype AfterRequestMiddleware =\n CopilotRuntimeOptionsVNext[\"afterRequestMiddleware\"];\ntype BeforeRequestMiddlewareFn = Exclude<BeforeRequestMiddleware, string>;\ntype BeforeRequestMiddlewareFnParameters =\n Parameters<BeforeRequestMiddlewareFn>;\ntype BeforeRequestMiddlewareFnResult = ReturnType<BeforeRequestMiddlewareFn>;\ntype AfterRequestMiddlewareFn = Exclude<AfterRequestMiddleware, string>;\ntype AfterRequestMiddlewareFnParameters = Parameters<AfterRequestMiddlewareFn>;\n\ninterface CopilotRuntimeConstructorParams<T extends Parameter[] | [] = []>\n extends\n Omit<CopilotRuntimeConstructorParams_BASE<T>, \"agents\">,\n Omit<CopilotRuntimeOptionsVNext, \"agents\" | \"transcriptionService\"> {\n /**\n * TODO: un-omit `transcriptionService` above once it's supported\n *\n * This satisfies...\n * – the optional constraint in `CopilotRuntimeConstructorParams_BASE`\n * – the `MaybePromise<NonEmptyRecord<T>>` constraint in `CopilotRuntimeOptionsVNext`\n * – the `Record<string, AbstractAgent>` constraint in `both\n */\n agents?: MaybePromise<NonEmptyRecord<Record<string, AbstractAgent>>>;\n}\n\n/**\n * Central runtime object passed to all request handlers.\n */\nexport class CopilotRuntime<const T extends Parameter[] | [] = []> {\n params?: CopilotRuntimeConstructorParams<T>;\n private observability?: CopilotObservabilityConfig;\n // Cache MCP tools per endpoint to avoid re-fetching repeatedly\n private mcpToolsCache: Map<string, BuiltInAgentConfiguration[\"tools\"]> =\n new Map();\n private runtimeArgs: CopilotRuntimeOptions;\n private _instance: CopilotRuntimeVNext;\n\n constructor(\n params?: CopilotRuntimeConstructorParams<T> &\n PartialBy<CopilotRuntimeOptions, \"agents\">,\n ) {\n const agents = params?.agents ?? {};\n const endpointAgents = this.assignEndpointsToAgents(\n params?.remoteEndpoints ?? [],\n );\n\n // Determine the base runner (user-provided or default)\n const baseRunner = params?.runner ?? new InMemoryAgentRunner();\n\n // Wrap with TelemetryAgentRunner unless telemetry is disabled\n // This ensures we always capture agent execution telemetry when enabled,\n // even if the user provides their own custom runner\n const runner = isTelemetryDisabled()\n ? baseRunner\n : new TelemetryAgentRunner({ runner: baseRunner });\n\n this.runtimeArgs = {\n agents: { ...endpointAgents, ...agents },\n runner,\n licenseToken: params?.licenseToken,\n // TODO: add support for transcriptionService from CopilotRuntimeOptionsVNext once it is ready\n // transcriptionService: params?.transcriptionService,\n\n beforeRequestMiddleware:\n this.createOnBeforeRequestHandler(params).bind(this),\n ...(params?.afterRequestMiddleware || params?.middleware?.onAfterRequest\n ? {\n afterRequestMiddleware:\n this.createOnAfterRequestHandler(params).bind(this),\n }\n : {}),\n a2ui: params?.a2ui,\n mcpApps: params?.mcpApps,\n openGenerativeUI: params?.openGenerativeUI,\n };\n this.params = params;\n this.observability = params?.observability_c;\n }\n\n get instance() {\n if (!this._instance) {\n this._instance = new CopilotRuntimeVNext(this.runtimeArgs);\n }\n\n return this._instance;\n }\n\n private assignEndpointsToAgents(\n endpoints: CopilotRuntimeConstructorParams<T>[\"remoteEndpoints\"],\n ): Record<string, AbstractAgent> {\n let result: Record<string, AbstractAgent> = {};\n\n if (\n endpoints.some(\n (endpoint) =>\n resolveEndpointType(endpoint) == EndpointType.LangGraphPlatform,\n )\n ) {\n throw new CopilotKitMisuseError({\n message:\n \"LangGraphPlatformEndpoint in remoteEndpoints is deprecated. \" +\n 'Please use the \"agents\" option instead with LangGraphAgent from \"@copilotkit/runtime/langgraph\". ' +\n 'Example: agents: { myAgent: new LangGraphAgent({ deploymentUrl: \"...\", graphId: \"...\" }) }',\n });\n }\n\n return result;\n }\n\n handleServiceAdapter(serviceAdapter: CopilotServiceAdapter) {\n this.runtimeArgs.agents = Promise.resolve(\n this.runtimeArgs.agents ?? {},\n ).then(async (agents) => {\n let agentsList = agents;\n const isAgentsListEmpty = !Object.keys(agents).length;\n const hasServiceAdapter = Boolean(serviceAdapter);\n const illegalServiceAdapterNames = [\"EmptyAdapter\"];\n const serviceAdapterCanBeUsedForAgent =\n !illegalServiceAdapterNames.includes(serviceAdapter.name);\n\n if (\n isAgentsListEmpty &&\n (!hasServiceAdapter || !serviceAdapterCanBeUsedForAgent)\n ) {\n throw new CopilotKitMisuseError({\n message:\n \"No default agent provided. Please provide a default agent in the runtime config.\",\n });\n }\n\n if (isAgentsListEmpty) {\n const model =\n serviceAdapter.getLanguageModel?.() ??\n `${serviceAdapter.provider}/${serviceAdapter.model}`;\n agentsList.default = new BuiltInAgent({ model });\n }\n\n const actions = this.params?.actions;\n if (actions) {\n const mcpTools = await this.getToolsFromMCP();\n agentsList = this.assignToolsToAgents(agents, [\n ...this.getToolsFromActions(actions),\n ...mcpTools,\n ]);\n }\n\n return agentsList;\n });\n }\n\n // Receive this.params.action and turn it into the AbstractAgent tools\n private getToolsFromActions(\n actions: ActionsConfiguration<any>,\n ): BuiltInAgentConfiguration[\"tools\"] {\n // Resolve actions to an array (handle function case)\n const actionsArray =\n typeof actions === \"function\"\n ? actions({ properties: {}, url: undefined })\n : actions;\n\n // Convert each Action to a ToolDefinition\n return actionsArray.map((action) => {\n // Convert JSON schema to Zod schema\n const zodSchema = getZodParameters(action.parameters || []);\n\n return {\n name: action.name,\n description: action.description || \"\",\n parameters: zodSchema,\n execute: () => Promise.resolve(),\n };\n });\n }\n\n private assignToolsToAgents(\n agents: Record<string, AbstractAgent>,\n tools: BuiltInAgentConfiguration[\"tools\"],\n ): Record<string, AbstractAgent> {\n if (!tools?.length) {\n return agents;\n }\n\n const enrichedAgents: Record<string, AbstractAgent> = { ...agents };\n\n for (const [agentId, agent] of Object.entries(enrichedAgents)) {\n const existingConfig = (Reflect.get(agent, \"config\") ??\n {}) as BuiltInAgentConfiguration;\n const existingTools = existingConfig.tools ?? [];\n\n const updatedConfig: BuiltInAgentConfiguration = {\n ...existingConfig,\n tools: [...existingTools, ...tools],\n };\n\n Reflect.set(agent, \"config\", updatedConfig);\n enrichedAgents[agentId] = agent;\n }\n\n return enrichedAgents;\n }\n\n private createOnBeforeRequestHandler(\n params?: CopilotRuntimeConstructorParams<T> &\n PartialBy<CopilotRuntimeOptions, \"agents\">,\n ) {\n return async (hookParams: BeforeRequestMiddlewareFnParameters[0]) => {\n const { request } = hookParams;\n\n // Capture telemetry for copilot request creation\n const publicApiKey = request.headers.get(\"x-copilotcloud-public-api-key\");\n const body = (await readBody(request)) as RunAgentInput;\n\n const forwardedProps = body?.forwardedProps as\n | {\n cloud?: { guardrails?: unknown };\n metadata?: { requestType?: string };\n }\n | undefined;\n\n // Get cloud base URL from environment or default\n const cloudBaseUrl =\n process.env.COPILOT_CLOUD_BASE_URL || \"https://api.cloud.copilotkit.ai\";\n\n telemetry.capture(\"oss.runtime.copilot_request_created\", {\n \"cloud.guardrails.enabled\":\n forwardedProps?.cloud?.guardrails !== undefined,\n requestType: forwardedProps?.metadata?.requestType ?? \"unknown\",\n \"cloud.api_key_provided\": !!publicApiKey,\n ...(publicApiKey ? { \"cloud.public_api_key\": publicApiKey } : {}),\n \"cloud.base_url\": cloudBaseUrl,\n });\n\n // We do not process middleware for the internal GET requests\n if (request.method === \"GET\" || !body) return;\n\n // TODO: get public api key and run with expected data\n // if (this.observability?.enabled && this.params.publicApiKey) {\n // this.logObservabilityBeforeRequest()\n // }\n\n // TODO: replace hooksParams top argument type with BeforeRequestMiddlewareParameters when exported\n const middlewareResult =\n await params?.beforeRequestMiddleware?.(hookParams);\n\n if (params?.middleware?.onBeforeRequest) {\n const { request, runtime, path } = hookParams;\n const gqlMessages = (aguiToGQL(body.messages) as Message[]).reduce(\n (acc, msg) => {\n if (\"role\" in msg && msg.role === \"user\") {\n acc.inputMessages.push(msg);\n } else {\n acc.outputMessages.push(msg);\n }\n return acc;\n },\n { inputMessages: [] as Message[], outputMessages: [] as Message[] },\n );\n const { inputMessages, outputMessages } = gqlMessages;\n params.middleware.onBeforeRequest({\n threadId: body.threadId,\n runId: body.runId,\n inputMessages,\n properties: body.forwardedProps,\n url: request.url,\n } satisfies OnBeforeRequestOptions);\n }\n\n return middlewareResult;\n };\n }\n\n private createOnAfterRequestHandler(\n params?: CopilotRuntimeConstructorParams<T> &\n PartialBy<CopilotRuntimeOptions, \"agents\">,\n ) {\n return async (hookParams: AfterRequestMiddlewareFnParameters[0]) => {\n // TODO: get public api key and run with expected data\n // if (this.observability?.enabled && publicApiKey) {\n // this.logObservabilityAfterRequest()\n // }\n\n // TODO: replace hooksParams top argument type with AfterRequestMiddlewareParameters when exported\n params?.afterRequestMiddleware?.(hookParams);\n\n if (params?.middleware?.onAfterRequest) {\n // TODO: provide old expected params here when available\n // @ts-expect-error -- missing arguments.\n params.middleware.onAfterRequest({});\n }\n };\n }\n\n // Observability Methods\n\n /**\n * Log LLM request if observability is enabled\n */\n private async logObservabilityBeforeRequest(\n requestData: LLMRequestData,\n ): Promise<void> {\n try {\n await this.observability.hooks.handleRequest(requestData);\n } catch (error) {\n console.error(\"Error logging LLM request:\", error);\n }\n }\n\n /**\n * Log final LLM response after request completes\n */\n private logObservabilityAfterRequest(\n outputMessagesPromise: Promise<Message[]>,\n baseData: {\n threadId: string;\n runId?: string;\n model?: string;\n provider?: string;\n agentName?: string;\n nodeName?: string;\n },\n streamedChunks: any[],\n requestStartTime: number,\n publicApiKey?: string,\n ): void {\n try {\n outputMessagesPromise\n .then((outputMessages) => {\n const responseData: LLMResponseData = {\n threadId: baseData.threadId,\n runId: baseData.runId,\n model: baseData.model,\n // Use collected chunks for progressive mode or outputMessages for regular mode\n output: this.observability.progressive\n ? streamedChunks\n : outputMessages,\n latency: Date.now() - requestStartTime,\n timestamp: Date.now(),\n provider: baseData.provider,\n isFinalResponse: true,\n agentName: baseData.agentName,\n nodeName: baseData.nodeName,\n };\n\n try {\n this.observability.hooks.handleResponse(responseData);\n } catch (logError) {\n console.error(\"Error logging LLM response:\", logError);\n }\n })\n .catch((error) => {\n console.error(\"Failed to get output messages for logging:\", error);\n });\n } catch (error) {\n console.error(\"Error setting up logging for LLM response:\", error);\n }\n }\n\n // Resolve MCP tools to BuiltInAgent tool definitions\n // Optionally accepts request-scoped properties to merge request-provided mcpServers\n private async getToolsFromMCP(options?: {\n properties?: Record<string, unknown>;\n }): Promise<BuiltInAgentConfiguration[\"tools\"]> {\n const runtimeMcpServers = (this.params?.mcpServers ??\n []) as MCPEndpointConfig[];\n const createMCPClient = this.params?.createMCPClient as\n | CreateMCPClientFunction\n | undefined;\n\n // If no runtime config and no request overrides, nothing to do\n const requestMcpServers = ((\n options?.properties as { mcpServers?: MCPEndpointConfig[] } | undefined\n )?.mcpServers ??\n (\n options?.properties as\n | { mcpEndpoints?: MCPEndpointConfig[] }\n | undefined\n )?.mcpEndpoints ??\n []) as MCPEndpointConfig[];\n\n const hasAnyServers =\n (runtimeMcpServers?.length ?? 0) > 0 ||\n (requestMcpServers?.length ?? 0) > 0;\n if (!hasAnyServers) {\n return [];\n }\n\n if (!createMCPClient) {\n // Mirror legacy behavior: when servers are provided without a factory, treat as misconfiguration\n throw new CopilotKitMisuseError({\n message:\n \"MCP Integration Error: `mcpServers` were provided, but the `createMCPClient` function was not passed to the CopilotRuntime constructor. Please provide an implementation for `createMCPClient`.\",\n });\n }\n\n // Merge and dedupe endpoints by URL; request-level overrides take precedence\n const effectiveEndpoints = (() => {\n const byUrl = new Map<string, MCPEndpointConfig>();\n for (const ep of runtimeMcpServers) {\n if (ep?.endpoint) byUrl.set(ep.endpoint, ep);\n }\n for (const ep of requestMcpServers) {\n if (ep?.endpoint) byUrl.set(ep.endpoint, ep);\n }\n return Array.from(byUrl.values());\n })();\n\n const allTools: BuiltInAgentConfiguration[\"tools\"] = [];\n\n for (const config of effectiveEndpoints) {\n const endpointUrl = config.endpoint;\n // Return cached tool definitions when available\n const cached = this.mcpToolsCache.get(endpointUrl);\n if (cached) {\n allTools.push(...cached);\n continue;\n }\n\n try {\n const client = await createMCPClient(config);\n const toolsMap = await client.tools();\n\n const toolDefs: BuiltInAgentConfiguration[\"tools\"] = Object.entries(\n toolsMap,\n ).map(([toolName, tool]: [string, MCPTool]) => {\n const params: Parameter[] = extractParametersFromSchema(tool);\n const zodSchema = getZodParameters(params);\n return {\n name: toolName,\n description:\n tool.description || `MCP tool: ${toolName} (from ${endpointUrl})`,\n parameters: zodSchema,\n execute: () => Promise.resolve(),\n };\n });\n\n // Cache per endpoint and add to aggregate\n this.mcpToolsCache.set(endpointUrl, toolDefs);\n allTools.push(...toolDefs);\n } catch (error) {\n console.error(\n `MCP: Failed to fetch tools from endpoint ${endpointUrl}. Skipping. Error:`,\n error,\n );\n // Cache empty to prevent repeated attempts within lifecycle\n this.mcpToolsCache.set(endpointUrl, []);\n }\n }\n\n // Dedupe tools by name while preserving last-in wins (request overrides)\n const dedupedByName = new Map<string, (typeof allTools)[number]>();\n for (const tool of allTools) {\n dedupedByName.set(tool.name, tool);\n }\n\n return Array.from(dedupedByName.values());\n }\n}\n\n// The two functions below are \"factory functions\", meant to create the action objects that adhere to the expected interfaces\nexport function copilotKitEndpoint(\n config: Omit<CopilotKitEndpoint, \"type\">,\n): CopilotKitEndpoint {\n return {\n ...config,\n type: EndpointType.CopilotKit,\n };\n}\n\nexport function langGraphPlatformEndpoint(\n config: Omit<LangGraphPlatformEndpoint, \"type\">,\n): LangGraphPlatformEndpoint {\n return {\n ...config,\n type: EndpointType.LangGraphPlatform,\n };\n}\n\nexport function resolveEndpointType(endpoint: EndpointDefinition) {\n if (!endpoint.type) {\n if (\"deploymentUrl\" in endpoint && \"agents\" in endpoint) {\n return EndpointType.LangGraphPlatform;\n } else {\n return EndpointType.CopilotKit;\n }\n }\n\n return endpoint.type;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsUA,IAAa,iBAAb,MAAmE;CASjE,YACE,QAEA;uCAPA,IAAI,KAAK;EAQT,MAAM,SAAS,QAAQ,UAAU,EAAE;EACnC,MAAM,iBAAiB,KAAK,wBAC1B,QAAQ,mBAAmB,EAAE,CAC9B;EAGD,MAAM,aAAa,QAAQ,UAAU,IAAI,qBAAqB;EAK9D,MAAM,SAAS,qBAAqB,GAChC,aACA,IAAI,qBAAqB,EAAE,QAAQ,YAAY,CAAC;AAEpD,O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AAa,SAAS;AAC7C,aAAS,KAAK,GAAG,SAAS;YACnB,OAAO;AACd,YAAQ,MACN,4CAA4C,YAAY,qBACxD,MACD;AAED,SAAK,cAAc,IAAI,aAAa,EAAE,CAAC;;;EAK3C,MAAM,gCAAgB,IAAI,KAAwC;AAClE,OAAK,MAAM,QAAQ,SACjB,eAAc,IAAI,KAAK,MAAM,KAAK;AAGpC,SAAO,MAAM,KAAK,cAAc,QAAQ,CAAC;;;AAK7C,SAAgB,mBACd,QACoB;AACpB,QAAO;EACL,GAAG;EACH,MAAM,aAAa;EACpB;;AAGH,SAAgB,0BACd,QAC2B;AAC3B,QAAO;EACL,GAAG;EACH,MAAM,aAAa;EACpB;;AAGH,SAAgB,oBAAoB,UAA8B;AAChE,KAAI,CAAC,SAAS,KACZ,KAAI,mBAAmB,YAAY,YAAY,SAC7C,QAAO,aAAa;KAEpB,QAAO,aAAa;AAIxB,QAAO,SAAS"}
1
+ {"version":3,"file":"copilot-runtime.mjs","names":["CopilotRuntimeVNext"],"sources":["../../../src/lib/runtime/copilot-runtime.ts"],"sourcesContent":["/**\n * <Callout type=\"info\">\n * This is the reference for the `CopilotRuntime` class. For more information and example code snippets, please see [Concept: Copilot Runtime](/concepts/copilot-runtime).\n * </Callout>\n *\n * ## Usage\n *\n * ```tsx\n * import { CopilotRuntime } from \"@copilotkit/runtime\";\n *\n * const copilotKit = new CopilotRuntime();\n * ```\n */\n\nimport {\n type Action,\n type CopilotErrorHandler,\n CopilotKitMisuseError,\n type MaybePromise,\n type NonEmptyRecord,\n type Parameter,\n readBody,\n getZodParameters,\n type PartialBy,\n isTelemetryDisabled,\n} from \"@copilotkit/shared\";\nimport type { RunAgentInput } from \"@ag-ui/core\";\nimport { aguiToGQL } from \"../../graphql/message-conversion/agui-to-gql\";\nimport type {\n CopilotServiceAdapter,\n RemoteChainParameters,\n} from \"../../service-adapters\";\nimport {\n CopilotRuntime as CopilotRuntimeVNext,\n type CopilotRuntimeOptions,\n type CopilotRuntimeOptions as CopilotRuntimeOptionsVNext,\n type AgentRunner,\n InMemoryAgentRunner,\n} from \"../../v2/runtime\";\nimport { TelemetryAgentRunner } from \"./telemetry-agent-runner\";\nimport telemetry from \"../telemetry-client\";\n\nimport type { MessageInput } from \"../../graphql/inputs/message.input\";\nimport type { Message } from \"../../graphql/types/converted\";\n\nimport {\n EndpointType,\n type EndpointDefinition,\n type CopilotKitEndpoint,\n type LangGraphPlatformEndpoint,\n} from \"./types\";\n\nimport type {\n CopilotObservabilityConfig,\n LLMRequestData,\n LLMResponseData,\n} from \"../observability\";\nimport type { AbstractAgent } from \"@ag-ui/client\";\n\n// +++ MCP Imports +++\nimport {\n type MCPClient,\n type MCPEndpointConfig,\n type MCPTool,\n extractParametersFromSchema,\n} from \"./mcp-tools-utils\";\nimport { BuiltInAgent, type BuiltInAgentClassicConfig } from \"../../agent\";\n// Define the function type alias here or import if defined elsewhere\ntype CreateMCPClientFunction = (\n config: MCPEndpointConfig,\n) => Promise<MCPClient>;\n\ntype ActionsConfiguration<T extends Parameter[] | [] = []> =\n | Action<T>[]\n | ((ctx: { properties: any; url?: string }) => Action<T>[]);\n\ninterface OnBeforeRequestOptions {\n threadId?: string;\n runId?: string;\n inputMessages: Message[];\n properties: any;\n url?: string;\n}\n\ntype OnBeforeRequestHandler = (\n options: OnBeforeRequestOptions,\n) => void | Promise<void>;\n\ninterface OnAfterRequestOptions {\n threadId: string;\n runId?: string;\n inputMessages: Message[];\n outputMessages: Message[];\n properties: any;\n url?: string;\n}\n\ntype OnAfterRequestHandler = (\n options: OnAfterRequestOptions,\n) => void | Promise<void>;\n\ninterface OnStopGenerationOptions {\n threadId: string;\n runId?: string;\n url?: string;\n agentName?: string;\n lastMessage: MessageInput;\n}\ntype OnStopGenerationHandler = (\n options: OnStopGenerationOptions,\n) => void | Promise<void>;\n\ninterface Middleware {\n /**\n * A function that is called before the request is processed.\n */\n /**\n * @deprecated This middleware hook is deprecated and will be removed in a future version.\n * Use updated middleware integration methods in CopilotRuntimeVNext instead.\n */\n onBeforeRequest?: OnBeforeRequestHandler;\n\n /**\n * A function that is called after the request is processed.\n */\n /**\n * @deprecated This middleware hook is deprecated and will be removed in a future version.\n * Use updated middleware integration methods in CopilotRuntimeVNext instead.\n */\n onAfterRequest?: OnAfterRequestHandler;\n}\n\nexport interface CopilotRuntimeConstructorParams_BASE<\n T extends Parameter[] | [] = [],\n> {\n /**\n * Middleware to be used by the runtime.\n *\n * ```ts\n * onBeforeRequest: (options: {\n * threadId?: string;\n * runId?: string;\n * inputMessages: Message[];\n * properties: any;\n * }) => void | Promise<void>;\n * ```\n *\n * ```ts\n * onAfterRequest: (options: {\n * threadId?: string;\n * runId?: string;\n * inputMessages: Message[];\n * outputMessages: Message[];\n * properties: any;\n * }) => void | Promise<void>;\n * ```\n */\n /**\n * @deprecated This middleware hook is deprecated and will be removed in a future version.\n * Use updated middleware integration methods in CopilotRuntimeVNext instead.\n */\n middleware?: Middleware;\n\n /*\n * A list of server side actions that can be executed. Will be ignored when remoteActions are set\n */\n actions?: ActionsConfiguration<T>;\n\n /*\n * Deprecated: Use `remoteEndpoints`.\n */\n remoteActions?: CopilotKitEndpoint[];\n\n /*\n * A list of remote actions that can be executed.\n */\n remoteEndpoints?: EndpointDefinition[];\n\n /*\n * An array of LangServer URLs.\n */\n langserve?: RemoteChainParameters[];\n\n /**\n * Optional agent runner to use for SSE runtime.\n */\n runner?: AgentRunner;\n\n /*\n * A map of agent names to AGUI agents.\n * Example agent config:\n * ```ts\n * import { AbstractAgent } from \"@ag-ui/client\";\n * // ...\n * agents: {\n * \"support\": new CustomerSupportAgent(),\n * \"technical\": new TechnicalAgent()\n * }\n * ```\n */\n agents?: Record<string, AbstractAgent>;\n\n /*\n * Delegates agent state processing to the service adapter.\n *\n * When enabled, individual agent state requests will not be processed by the agent itself.\n * Instead, all processing will be handled by the service adapter.\n */\n delegateAgentProcessingToServiceAdapter?: boolean;\n\n /**\n * Configuration for LLM request/response logging.\n * Requires publicApiKey from CopilotKit component to be set:\n *\n * ```tsx\n * <CopilotKit publicApiKey=\"ck_pub_...\" />\n * ```\n *\n * Example logging config:\n * ```ts\n * logging: {\n * enabled: true, // Enable or disable logging\n * progressive: true, // Set to false for buffered logging\n * logger: {\n * logRequest: (data) => langfuse.trace({ name: \"LLM Request\", input: data }),\n * logResponse: (data) => langfuse.trace({ name: \"LLM Response\", output: data }),\n * logError: (errorData) => langfuse.trace({ name: \"LLM Error\", metadata: errorData }),\n * },\n * }\n * ```\n */\n observability_c?: CopilotObservabilityConfig;\n\n /**\n * Configuration for connecting to Model Context Protocol (MCP) servers.\n * Allows fetching and using tools defined on external MCP-compliant servers.\n * Requires providing the `createMCPClient` function during instantiation.\n * @experimental\n */\n mcpServers?: MCPEndpointConfig[];\n\n /**\n * A function that creates an MCP client instance for a given endpoint configuration.\n * This function is responsible for using the appropriate MCP client library\n * (e.g., `@copilotkit/runtime`, `ai`) to establish a connection.\n * Required if `mcpServers` is provided.\n *\n * ```typescript\n * import { experimental_createMCPClient } from \"ai\"; // Import from vercel ai library\n * // ...\n * const runtime = new CopilotRuntime({\n * mcpServers: [{ endpoint: \"...\" }],\n * async createMCPClient(config) {\n * return await experimental_createMCPClient({\n * transport: {\n * type: \"sse\",\n * url: config.endpoint,\n * headers: config.apiKey\n * ? { Authorization: `Bearer ${config.apiKey}` }\n * : undefined,\n * },\n * });\n * }\n * });\n * ```\n */\n createMCPClient?: CreateMCPClientFunction;\n\n /**\n * Optional error handler for comprehensive debugging and observability.\n *\n * **Requires publicApiKey**: Error handling only works when requests include a valid publicApiKey.\n * This is a premium Copilot Cloud feature.\n *\n * @param errorEvent - Structured error event with rich debugging context\n *\n * @example\n * ```typescript\n * const runtime = new CopilotRuntime({\n * onError: (errorEvent) => {\n * debugDashboard.capture(errorEvent);\n * }\n * });\n * ```\n */\n onError?: CopilotErrorHandler;\n\n onStopGeneration?: OnStopGenerationHandler;\n\n // /** Optional transcription service for audio processing. */\n // transcriptionService?: CopilotRuntimeOptionsVNext[\"transcriptionService\"];\n // /** Optional *before* middleware – callback function or webhook URL. */\n // beforeRequestMiddleware?: CopilotRuntimeOptionsVNext[\"beforeRequestMiddleware\"];\n // /** Optional *after* middleware – callback function or webhook URL. */\n // afterRequestMiddleware?: CopilotRuntimeOptionsVNext[\"afterRequestMiddleware\"];\n}\n\ntype BeforeRequestMiddleware =\n CopilotRuntimeOptionsVNext[\"beforeRequestMiddleware\"];\ntype AfterRequestMiddleware =\n CopilotRuntimeOptionsVNext[\"afterRequestMiddleware\"];\ntype BeforeRequestMiddlewareFn = Exclude<BeforeRequestMiddleware, string>;\ntype BeforeRequestMiddlewareFnParameters =\n Parameters<BeforeRequestMiddlewareFn>;\ntype BeforeRequestMiddlewareFnResult = ReturnType<BeforeRequestMiddlewareFn>;\ntype AfterRequestMiddlewareFn = Exclude<AfterRequestMiddleware, string>;\ntype AfterRequestMiddlewareFnParameters = Parameters<AfterRequestMiddlewareFn>;\n\ninterface CopilotRuntimeConstructorParams<T extends Parameter[] | [] = []>\n extends\n Omit<CopilotRuntimeConstructorParams_BASE<T>, \"agents\">,\n Omit<CopilotRuntimeOptionsVNext, \"agents\" | \"transcriptionService\"> {\n /**\n * TODO: un-omit `transcriptionService` above once it's supported\n *\n * This satisfies...\n * – the optional constraint in `CopilotRuntimeConstructorParams_BASE`\n * – the `MaybePromise<NonEmptyRecord<T>>` constraint in `CopilotRuntimeOptionsVNext`\n * – the `Record<string, AbstractAgent>` constraint in `both\n */\n agents?: MaybePromise<NonEmptyRecord<Record<string, AbstractAgent>>>;\n}\n\n/**\n * Central runtime object passed to all request handlers.\n */\nexport class CopilotRuntime<const T extends Parameter[] | [] = []> {\n params?: CopilotRuntimeConstructorParams<T>;\n private observability?: CopilotObservabilityConfig;\n // Cache MCP tools per endpoint to avoid re-fetching repeatedly\n private mcpToolsCache: Map<string, BuiltInAgentClassicConfig[\"tools\"]> =\n new Map();\n private runtimeArgs: CopilotRuntimeOptions;\n private _instance: CopilotRuntimeVNext;\n\n constructor(\n params?: CopilotRuntimeConstructorParams<T> &\n PartialBy<CopilotRuntimeOptions, \"agents\">,\n ) {\n const agents = params?.agents ?? {};\n const endpointAgents = this.assignEndpointsToAgents(\n params?.remoteEndpoints ?? [],\n );\n\n // Determine the base runner (user-provided or default)\n const baseRunner = params?.runner ?? new InMemoryAgentRunner();\n\n // Wrap with TelemetryAgentRunner unless telemetry is disabled\n // This ensures we always capture agent execution telemetry when enabled,\n // even if the user provides their own custom runner\n const runner = isTelemetryDisabled()\n ? baseRunner\n : new TelemetryAgentRunner({ runner: baseRunner });\n\n this.runtimeArgs = {\n agents: { ...endpointAgents, ...agents },\n runner,\n licenseToken: params?.licenseToken,\n // TODO: add support for transcriptionService from CopilotRuntimeOptionsVNext once it is ready\n // transcriptionService: params?.transcriptionService,\n\n beforeRequestMiddleware:\n this.createOnBeforeRequestHandler(params).bind(this),\n ...(params?.afterRequestMiddleware || params?.middleware?.onAfterRequest\n ? {\n afterRequestMiddleware:\n this.createOnAfterRequestHandler(params).bind(this),\n }\n : {}),\n a2ui: params?.a2ui,\n mcpApps: params?.mcpApps,\n openGenerativeUI: params?.openGenerativeUI,\n };\n this.params = params;\n this.observability = params?.observability_c;\n }\n\n get instance() {\n if (!this._instance) {\n this._instance = new CopilotRuntimeVNext(this.runtimeArgs);\n }\n\n return this._instance;\n }\n\n private assignEndpointsToAgents(\n endpoints: CopilotRuntimeConstructorParams<T>[\"remoteEndpoints\"],\n ): Record<string, AbstractAgent> {\n let result: Record<string, AbstractAgent> = {};\n\n if (\n endpoints.some(\n (endpoint) =>\n resolveEndpointType(endpoint) == EndpointType.LangGraphPlatform,\n )\n ) {\n throw new CopilotKitMisuseError({\n message:\n \"LangGraphPlatformEndpoint in remoteEndpoints is deprecated. \" +\n 'Please use the \"agents\" option instead with LangGraphAgent from \"@copilotkit/runtime/langgraph\". ' +\n 'Example: agents: { myAgent: new LangGraphAgent({ deploymentUrl: \"...\", graphId: \"...\" }) }',\n });\n }\n\n return result;\n }\n\n handleServiceAdapter(serviceAdapter: CopilotServiceAdapter) {\n this.runtimeArgs.agents = Promise.resolve(\n this.runtimeArgs.agents ?? {},\n ).then(async (agents) => {\n let agentsList = agents;\n const isAgentsListEmpty = !Object.keys(agents).length;\n const hasServiceAdapter = Boolean(serviceAdapter);\n const illegalServiceAdapterNames = [\"EmptyAdapter\"];\n const serviceAdapterCanBeUsedForAgent =\n !illegalServiceAdapterNames.includes(serviceAdapter.name);\n\n if (\n isAgentsListEmpty &&\n (!hasServiceAdapter || !serviceAdapterCanBeUsedForAgent)\n ) {\n throw new CopilotKitMisuseError({\n message:\n \"No default agent provided. Please provide a default agent in the runtime config.\",\n });\n }\n\n if (isAgentsListEmpty) {\n const model =\n serviceAdapter.getLanguageModel?.() ??\n `${serviceAdapter.provider}/${serviceAdapter.model}`;\n agentsList.default = new BuiltInAgent({ model });\n }\n\n const actions = this.params?.actions;\n if (actions) {\n const mcpTools = await this.getToolsFromMCP();\n agentsList = this.assignToolsToAgents(agents, [\n ...this.getToolsFromActions(actions),\n ...mcpTools,\n ]);\n }\n\n return agentsList;\n });\n }\n\n // Receive this.params.action and turn it into the AbstractAgent tools\n private getToolsFromActions(\n actions: ActionsConfiguration<any>,\n ): BuiltInAgentClassicConfig[\"tools\"] {\n // Resolve actions to an array (handle function case)\n const actionsArray =\n typeof actions === \"function\"\n ? actions({ properties: {}, url: undefined })\n : actions;\n\n // Convert each Action to a ToolDefinition\n return actionsArray.map((action) => {\n // Convert JSON schema to Zod schema\n const zodSchema = getZodParameters(action.parameters || []);\n\n return {\n name: action.name,\n description: action.description || \"\",\n parameters: zodSchema,\n execute: () => Promise.resolve(),\n };\n });\n }\n\n private assignToolsToAgents(\n agents: Record<string, AbstractAgent>,\n tools: BuiltInAgentClassicConfig[\"tools\"],\n ): Record<string, AbstractAgent> {\n if (!tools?.length) {\n return agents;\n }\n\n const enrichedAgents: Record<string, AbstractAgent> = { ...agents };\n\n for (const [agentId, agent] of Object.entries(enrichedAgents)) {\n const existingConfig = (Reflect.get(agent, \"config\") ?? {}) as Record<\n string,\n unknown\n >;\n\n // Skip factory-mode agents — they don't have a tools property\n if (\"factory\" in existingConfig) {\n continue;\n }\n\n const classicConfig = existingConfig as BuiltInAgentClassicConfig;\n const existingTools = classicConfig.tools ?? [];\n\n const updatedConfig: BuiltInAgentClassicConfig = {\n ...classicConfig,\n tools: [...existingTools, ...tools],\n };\n\n Reflect.set(agent, \"config\", updatedConfig);\n enrichedAgents[agentId] = agent;\n }\n\n return enrichedAgents;\n }\n\n private createOnBeforeRequestHandler(\n params?: CopilotRuntimeConstructorParams<T> &\n PartialBy<CopilotRuntimeOptions, \"agents\">,\n ) {\n return async (hookParams: BeforeRequestMiddlewareFnParameters[0]) => {\n const { request } = hookParams;\n\n // Capture telemetry for copilot request creation\n const publicApiKey = request.headers.get(\"x-copilotcloud-public-api-key\");\n const body = (await readBody(request)) as RunAgentInput;\n\n const forwardedProps = body?.forwardedProps as\n | {\n cloud?: { guardrails?: unknown };\n metadata?: { requestType?: string };\n }\n | undefined;\n\n // Get cloud base URL from environment or default\n const cloudBaseUrl =\n process.env.COPILOT_CLOUD_BASE_URL || \"https://api.cloud.copilotkit.ai\";\n\n telemetry.capture(\"oss.runtime.copilot_request_created\", {\n \"cloud.guardrails.enabled\":\n forwardedProps?.cloud?.guardrails !== undefined,\n requestType: forwardedProps?.metadata?.requestType ?? \"unknown\",\n \"cloud.api_key_provided\": !!publicApiKey,\n ...(publicApiKey ? { \"cloud.public_api_key\": publicApiKey } : {}),\n \"cloud.base_url\": cloudBaseUrl,\n });\n\n // We do not process middleware for the internal GET requests\n if (request.method === \"GET\" || !body) return;\n\n // TODO: get public api key and run with expected data\n // if (this.observability?.enabled && this.params.publicApiKey) {\n // this.logObservabilityBeforeRequest()\n // }\n\n // TODO: replace hooksParams top argument type with BeforeRequestMiddlewareParameters when exported\n const middlewareResult =\n await params?.beforeRequestMiddleware?.(hookParams);\n\n if (params?.middleware?.onBeforeRequest) {\n const { request, runtime, path } = hookParams;\n const gqlMessages = (aguiToGQL(body.messages) as Message[]).reduce(\n (acc, msg) => {\n if (\"role\" in msg && msg.role === \"user\") {\n acc.inputMessages.push(msg);\n } else {\n acc.outputMessages.push(msg);\n }\n return acc;\n },\n { inputMessages: [] as Message[], outputMessages: [] as Message[] },\n );\n const { inputMessages, outputMessages } = gqlMessages;\n params.middleware.onBeforeRequest({\n threadId: body.threadId,\n runId: body.runId,\n inputMessages,\n properties: body.forwardedProps,\n url: request.url,\n } satisfies OnBeforeRequestOptions);\n }\n\n return middlewareResult;\n };\n }\n\n private createOnAfterRequestHandler(\n params?: CopilotRuntimeConstructorParams<T> &\n PartialBy<CopilotRuntimeOptions, \"agents\">,\n ) {\n return async (hookParams: AfterRequestMiddlewareFnParameters[0]) => {\n // TODO: get public api key and run with expected data\n // if (this.observability?.enabled && publicApiKey) {\n // this.logObservabilityAfterRequest()\n // }\n\n // TODO: replace hooksParams top argument type with AfterRequestMiddlewareParameters when exported\n params?.afterRequestMiddleware?.(hookParams);\n\n if (params?.middleware?.onAfterRequest) {\n // TODO: provide old expected params here when available\n // @ts-expect-error -- missing arguments.\n params.middleware.onAfterRequest({});\n }\n };\n }\n\n // Observability Methods\n\n /**\n * Log LLM request if observability is enabled\n */\n private async logObservabilityBeforeRequest(\n requestData: LLMRequestData,\n ): Promise<void> {\n try {\n await this.observability.hooks.handleRequest(requestData);\n } catch (error) {\n console.error(\"Error logging LLM request:\", error);\n }\n }\n\n /**\n * Log final LLM response after request completes\n */\n private logObservabilityAfterRequest(\n outputMessagesPromise: Promise<Message[]>,\n baseData: {\n threadId: string;\n runId?: string;\n model?: string;\n provider?: string;\n agentName?: string;\n nodeName?: string;\n },\n streamedChunks: any[],\n requestStartTime: number,\n publicApiKey?: string,\n ): void {\n try {\n outputMessagesPromise\n .then((outputMessages) => {\n const responseData: LLMResponseData = {\n threadId: baseData.threadId,\n runId: baseData.runId,\n model: baseData.model,\n // Use collected chunks for progressive mode or outputMessages for regular mode\n output: this.observability.progressive\n ? streamedChunks\n : outputMessages,\n latency: Date.now() - requestStartTime,\n timestamp: Date.now(),\n provider: baseData.provider,\n isFinalResponse: true,\n agentName: baseData.agentName,\n nodeName: baseData.nodeName,\n };\n\n try {\n this.observability.hooks.handleResponse(responseData);\n } catch (logError) {\n console.error(\"Error logging LLM response:\", logError);\n }\n })\n .catch((error) => {\n console.error(\"Failed to get output messages for logging:\", error);\n });\n } catch (error) {\n console.error(\"Error setting up logging for LLM response:\", error);\n }\n }\n\n // Resolve MCP tools to BuiltInAgent tool definitions\n // Optionally accepts request-scoped properties to merge request-provided mcpServers\n private async getToolsFromMCP(options?: {\n properties?: Record<string, unknown>;\n }): Promise<BuiltInAgentClassicConfig[\"tools\"]> {\n const runtimeMcpServers = (this.params?.mcpServers ??\n []) as MCPEndpointConfig[];\n const createMCPClient = this.params?.createMCPClient as\n | CreateMCPClientFunction\n | undefined;\n\n // If no runtime config and no request overrides, nothing to do\n const requestMcpServers = ((\n options?.properties as { mcpServers?: MCPEndpointConfig[] } | undefined\n )?.mcpServers ??\n (\n options?.properties as\n | { mcpEndpoints?: MCPEndpointConfig[] }\n | undefined\n )?.mcpEndpoints ??\n []) as MCPEndpointConfig[];\n\n const hasAnyServers =\n (runtimeMcpServers?.length ?? 0) > 0 ||\n (requestMcpServers?.length ?? 0) > 0;\n if (!hasAnyServers) {\n return [];\n }\n\n if (!createMCPClient) {\n // Mirror legacy behavior: when servers are provided without a factory, treat as misconfiguration\n throw new CopilotKitMisuseError({\n message:\n \"MCP Integration Error: `mcpServers` were provided, but the `createMCPClient` function was not passed to the CopilotRuntime constructor. Please provide an implementation for `createMCPClient`.\",\n });\n }\n\n // Merge and dedupe endpoints by URL; request-level overrides take precedence\n const effectiveEndpoints = (() => {\n const byUrl = new Map<string, MCPEndpointConfig>();\n for (const ep of runtimeMcpServers) {\n if (ep?.endpoint) byUrl.set(ep.endpoint, ep);\n }\n for (const ep of requestMcpServers) {\n if (ep?.endpoint) byUrl.set(ep.endpoint, ep);\n }\n return Array.from(byUrl.values());\n })();\n\n const allTools: BuiltInAgentClassicConfig[\"tools\"] = [];\n\n for (const config of effectiveEndpoints) {\n const endpointUrl = config.endpoint;\n // Return cached tool definitions when available\n const cached = this.mcpToolsCache.get(endpointUrl);\n if (cached) {\n allTools.push(...cached);\n continue;\n }\n\n try {\n const client = await createMCPClient(config);\n const toolsMap = await client.tools();\n\n const toolDefs: BuiltInAgentClassicConfig[\"tools\"] = Object.entries(\n toolsMap,\n ).map(([toolName, tool]: [string, MCPTool]) => {\n const params: Parameter[] = extractParametersFromSchema(tool);\n const zodSchema = getZodParameters(params);\n return {\n name: toolName,\n description:\n tool.description || `MCP tool: ${toolName} (from ${endpointUrl})`,\n parameters: zodSchema,\n execute: () => Promise.resolve(),\n };\n });\n\n // Cache per endpoint and add to aggregate\n this.mcpToolsCache.set(endpointUrl, toolDefs);\n allTools.push(...toolDefs);\n } catch (error) {\n console.error(\n `MCP: Failed to fetch tools from endpoint ${endpointUrl}. Skipping. Error:`,\n error,\n );\n // Cache empty to prevent repeated attempts within lifecycle\n this.mcpToolsCache.set(endpointUrl, []);\n }\n }\n\n // Dedupe tools by name while preserving last-in wins (request overrides)\n const dedupedByName = new Map<string, (typeof allTools)[number]>();\n for (const tool of allTools) {\n dedupedByName.set(tool.name, tool);\n }\n\n return Array.from(dedupedByName.values());\n }\n}\n\n// The two functions below are \"factory functions\", meant to create the action objects that adhere to the expected interfaces\nexport function copilotKitEndpoint(\n config: Omit<CopilotKitEndpoint, \"type\">,\n): CopilotKitEndpoint {\n return {\n ...config,\n type: EndpointType.CopilotKit,\n };\n}\n\nexport function langGraphPlatformEndpoint(\n config: Omit<LangGraphPlatformEndpoint, \"type\">,\n): LangGraphPlatformEndpoint {\n return {\n ...config,\n type: EndpointType.LangGraphPlatform,\n };\n}\n\nexport function resolveEndpointType(endpoint: EndpointDefinition) {\n if (!endpoint.type) {\n if (\"deploymentUrl\" in endpoint && \"agents\" in endpoint) {\n return EndpointType.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B;GACvC,MAAM,cAAc,OAAO;GAE3B,MAAM,SAAS,KAAK,cAAc,IAAI,YAAY;AAClD,OAAI,QAAQ;AACV,aAAS,KAAK,GAAG,OAAO;AACxB;;AAGF,OAAI;IAEF,MAAM,WAAW,OADF,MAAM,gBAAgB,OAAO,EACd,OAAO;IAErC,MAAM,WAA+C,OAAO,QAC1D,SACD,CAAC,KAAK,CAAC,UAAU,UAA6B;KAE7C,MAAM,YAAY,iBADU,4BAA4B,KAAK,CACnB;AAC1C,YAAO;MACL,MAAM;MACN,aACE,KAAK,eAAe,aAAa,SAAS,SAAS,YAAY;MACjE,YAAY;MACZ,eAAe,QAAQ,SAAS;MACjC;MACD;AAGF,SAAK,cAAc,IAAI,aAAa,SAAS;AAC7C,aAAS,KAAK,GAAG,SAAS;YACnB,OAAO;AACd,YAAQ,MACN,4CAA4C,YAAY,qBACxD,MACD;AAED,SAAK,cAAc,IAAI,aAAa,EAAE,CAAC;;;EAK3C,MAAM,gCAAgB,IAAI,KAAwC;AAClE,OAAK,MAAM,QAAQ,SACjB,eAAc,IAAI,KAAK,MAAM,KAAK;AAGpC,SAAO,MAAM,KAAK,cAAc,QAAQ,CAAC;;;AAK7C,SAAgB,mBACd,QACoB;AACpB,QAAO;EACL,GAAG;EACH,MAAM,aAAa;EACpB;;AAGH,SAAgB,0BACd,QAC2B;AAC3B,QAAO;EACL,GAAG;EACH,MAAM,aAAa;EACpB;;AAGH,SAAgB,oBAAoB,UAA8B;AAChE,KAAI,CAAC,SAAS,KACZ,KAAI,mBAAmB,YAAY,YAAY,SAC7C,QAAO,aAAa;KAEpB,QAAO,aAAa;AAIxB,QAAO,SAAS"}
@@ -57,7 +57,7 @@ function convertMCPToolsToActions(mcpTools, mcpEndpoint) {
57
57
  return typeof result === "string" ? result : JSON.stringify(result);
58
58
  } catch (error) {
59
59
  console.error(`Error executing MCP tool '${toolName}' from endpoint ${mcpEndpoint}:`, error);
60
- throw new Error(`Execution failed for MCP tool '${toolName}': ${error instanceof Error ? error.message : String(error)}`);
60
+ throw new Error(`Execution failed for MCP tool '${toolName}': ${error instanceof Error ? error.message : String(error)}`, { cause: error });
61
61
  }
62
62
  };
63
63
  actions.push({
@@ -1 +1 @@
1
- {"version":3,"file":"mcp-tools-utils.cjs","names":[],"sources":["../../../src/lib/runtime/mcp-tools-utils.ts"],"sourcesContent":["import { Action, Parameter } from \"@copilotkit/shared\";\n\n/**\n * Represents a tool provided by an MCP server.\n */\nexport interface MCPTool {\n description?: string;\n /** Schema defining parameters, mirroring the MCP structure. */\n schema?: {\n parameters?: {\n properties?: Record<string, any>;\n required?: string[];\n jsonSchema?: Record<string, any>;\n };\n };\n /** The function to call to execute the tool on the MCP server. */\n execute(params: any): Promise<any>;\n}\n\n/**\n * Defines the contract for *any* MCP client implementation the user might provide.\n */\nexport interface MCPClient {\n /** A method that returns a map of tool names to MCPTool objects available from the connected MCP server. */\n tools(): Promise<Record<string, MCPTool>>;\n /** An optional method for cleanup if the underlying client requires explicit disconnection. */\n close?(): Promise<void>;\n}\n\n/**\n * Configuration for connecting to an MCP endpoint.\n */\nexport interface MCPEndpointConfig {\n endpoint: string;\n apiKey?: string;\n}\n\n/**\n * Extracts CopilotKit-compatible parameters from an MCP tool schema.\n * @param toolOrSchema The schema object from an MCPTool or the full MCPTool object.\n * @returns An array of Parameter objects.\n */\nexport function extractParametersFromSchema(\n toolOrSchema?: MCPTool | MCPTool[\"schema\"],\n): Parameter[] {\n const parameters: Parameter[] = [];\n\n // Handle either full tool object or just schema\n const schema =\n \"schema\" in (toolOrSchema || {})\n ? (toolOrSchema as MCPTool).schema\n : (toolOrSchema as MCPTool[\"schema\"]);\n\n const toolParameters = schema?.parameters?.jsonSchema || schema?.parameters;\n const properties = toolParameters?.properties;\n const requiredParams = new Set(toolParameters?.required || []);\n\n if (!properties) {\n return parameters;\n }\n\n for (const paramName in properties) {\n if (Object.prototype.hasOwnProperty.call(properties, paramName)) {\n const paramDef = properties[paramName];\n\n // Enhanced type extraction with support for complex types\n let type = paramDef.type || \"string\";\n let description = paramDef.description || \"\";\n\n // Handle arrays with items\n if (type === \"array\" && paramDef.items) {\n const itemType = paramDef.items.type || \"object\";\n if (itemType === \"object\" && paramDef.items.properties) {\n // For arrays of objects, describe the structure\n const itemProperties = Object.keys(paramDef.items.properties).join(\n \", \",\n );\n description =\n description +\n (description ? \" \" : \"\") +\n `Array of objects with properties: ${itemProperties}`;\n } else {\n // For arrays of primitives\n type = `array<${itemType}>`;\n }\n }\n\n // Handle enums\n if (paramDef.enum && Array.isArray(paramDef.enum)) {\n const enumValues = paramDef.enum.join(\" | \");\n description =\n description +\n (description ? \" \" : \"\") +\n `Allowed values: ${enumValues}`;\n }\n\n // Handle objects with properties\n if (type === \"object\" && paramDef.properties) {\n const objectProperties = Object.keys(paramDef.properties).join(\", \");\n description =\n description +\n (description ? \" \" : \"\") +\n `Object with properties: ${objectProperties}`;\n }\n\n parameters.push({\n name: paramName,\n type: type,\n description: description,\n required: requiredParams.has(paramName),\n });\n }\n }\n\n return parameters;\n}\n\n/**\n * Converts a map of MCPTools into an array of CopilotKit Actions.\n * @param mcpTools A record mapping tool names to MCPTool objects.\n * @param mcpEndpoint The endpoint URL from which these tools were fetched.\n * @returns An array of Action<any> objects.\n */\nexport function convertMCPToolsToActions(\n mcpTools: Record<string, MCPTool>,\n mcpEndpoint: string,\n): Action<any>[] {\n const actions: Action<any>[] = [];\n\n for (const [toolName, tool] of Object.entries(mcpTools)) {\n const parameters = extractParametersFromSchema(tool);\n\n const handler = async (params: any): Promise<any> => {\n try {\n const result = await tool.execute(params);\n // Ensure the result is a string or stringify it, as required by many LLMs.\n // This might need adjustment depending on how different LLMs handle tool results.\n return typeof result === \"string\" ? result : JSON.stringify(result);\n } catch (error) {\n console.error(\n `Error executing MCP tool '${toolName}' from endpoint ${mcpEndpoint}:`,\n error,\n );\n // Re-throw or format the error for the LLM\n throw new Error(\n `Execution failed for MCP tool '${toolName}': ${\n error instanceof Error ? error.message : String(error)\n }`,\n );\n }\n };\n\n actions.push({\n name: toolName,\n description:\n tool.description || `MCP tool: ${toolName} (from ${mcpEndpoint})`,\n parameters: parameters,\n handler: handler,\n // Add metadata for easier identification/debugging\n _isMCPTool: true,\n _mcpEndpoint: mcpEndpoint,\n } as Action<any> & { _isMCPTool: boolean; _mcpEndpoint: string }); // Type assertion for metadata\n }\n\n return actions;\n}\n\n/**\n * Generate better instructions for using MCP tools\n * This is used to enhance the system prompt with tool documentation\n */\nexport function generateMcpToolInstructions(\n toolsMap: Record<string, MCPTool>,\n): string {\n if (!toolsMap || Object.keys(toolsMap).length === 0) {\n return \"\";\n }\n\n const toolEntries = Object.entries(toolsMap);\n\n // Generate documentation for each tool\n const toolsDoc = toolEntries\n .map(([name, tool]) => {\n // Extract schema information if available\n let paramsDoc = \" No parameters required\";\n\n try {\n if (tool.schema && typeof tool.schema === \"object\") {\n const schema = tool.schema as any;\n\n // Extract parameters from JSON Schema - check both schema.parameters.properties and schema.properties\n const toolParameters =\n schema.parameters?.jsonSchema || schema.parameters;\n const properties = toolParameters?.properties || schema.properties;\n const requiredParams =\n toolParameters?.required || schema.required || [];\n\n if (properties) {\n // Build parameter documentation from properties with enhanced type information\n const paramsList = Object.entries(properties).map(\n ([paramName, propSchema]) => {\n const propDetails = propSchema as any;\n const requiredMark = requiredParams.includes(paramName)\n ? \"*\"\n : \"\";\n let typeInfo = propDetails.type || \"any\";\n let description = propDetails.description\n ? ` - ${propDetails.description}`\n : \"\";\n\n // Enhanced type display for complex schemas\n if (typeInfo === \"array\" && propDetails.items) {\n const itemType = propDetails.items.type || \"object\";\n if (itemType === \"object\" && propDetails.items.properties) {\n const itemProps = Object.keys(\n propDetails.items.properties,\n ).join(\", \");\n typeInfo = `array<object>`;\n description =\n description +\n (description ? \" \" : \" - \") +\n `Array of objects with properties: ${itemProps}`;\n } else {\n typeInfo = `array<${itemType}>`;\n }\n }\n\n // Handle enums\n if (propDetails.enum && Array.isArray(propDetails.enum)) {\n const enumValues = propDetails.enum.join(\" | \");\n description =\n description +\n (description ? \" \" : \" - \") +\n `Allowed values: ${enumValues}`;\n }\n\n // Handle objects\n if (typeInfo === \"object\" && propDetails.properties) {\n const objectProps = Object.keys(propDetails.properties).join(\n \", \",\n );\n description =\n description +\n (description ? \" \" : \" - \") +\n `Object with properties: ${objectProps}`;\n }\n\n return ` - ${paramName}${requiredMark} (${typeInfo})${description}`;\n },\n );\n\n if (paramsList.length > 0) {\n paramsDoc = paramsList.join(\"\\n\");\n }\n }\n }\n } catch (e) {\n console.error(`Error parsing schema for tool ${name}:`, e);\n }\n\n return `- ${name}: ${tool.description || \"\"}\n${paramsDoc}`;\n })\n .join(\"\\n\\n\");\n\n return `You have access to the following external tools provided by Model Context Protocol (MCP) servers:\n\n${toolsDoc}\n\nWhen using these tools:\n1. Only provide valid parameters according to their type requirements\n2. Required parameters are marked with *\n3. For array parameters, provide data in the correct array format\n4. For object parameters, include all required nested properties\n5. For enum parameters, use only the allowed values listed\n6. Format API calls correctly with the expected parameter structure\n7. Always check tool responses to determine your next action`;\n}\n"],"mappings":";;;;;;;;AA0CA,SAAgB,4BACd,cACa;CACb,MAAM,aAA0B,EAAE;CAGlC,MAAM,SACJ,aAAa,gBAAgB,EAAE,IAC1B,aAAyB,SACzB;CAEP,MAAM,iBAAiB,QAAQ,YAAY,cAAc,QAAQ;CACjE,MAAM,aAAa,gBAAgB;CACnC,MAAM,iBAAiB,IAAI,IAAI,gBAAgB,YAAY,EAAE,CAAC;AAE9D,KAAI,CAAC,WACH,QAAO;AAGT,MAAK,MAAM,aAAa,WACtB,KAAI,OAAO,UAAU,eAAe,KAAK,YAAY,UAAU,EAAE;EAC/D,MAAM,WAAW,WAAW;EAG5B,IAAI,OAAO,SAAS,QAAQ;EAC5B,IAAI,cAAc,SAAS,eAAe;AAG1C,MAAI,SAAS,WAAW,SAAS,OAAO;GACtC,MAAM,WAAW,SAAS,MAAM,QAAQ;AACxC,OAAI,aAAa,YAAY,SAAS,MAAM,YAAY;IAEtD,M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IAAI,EAAE;;AAG5D,SAAO,KAAK,KAAK,IAAI,KAAK,eAAe,GAAG;EAChD;GACI,CACD,KAAK,OAAO,CAIN"}
1
+ {"version":3,"file":"mcp-tools-utils.cjs","names":[],"sources":["../../../src/lib/runtime/mcp-tools-utils.ts"],"sourcesContent":["import { Action, Parameter } from \"@copilotkit/shared\";\n\n/**\n * Represents a tool provided by an MCP server.\n */\nexport interface MCPTool {\n description?: string;\n /** Schema defining parameters, mirroring the MCP structure. */\n schema?: {\n parameters?: {\n properties?: Record<string, any>;\n required?: string[];\n jsonSchema?: Record<string, any>;\n };\n };\n /** The function to call to execute the tool on the MCP server. */\n execute(params: any): Promise<any>;\n}\n\n/**\n * Defines the contract for *any* MCP client implementation the user might provide.\n */\nexport interface MCPClient {\n /** A method that returns a map of tool names to MCPTool objects available from the connected MCP server. */\n tools(): Promise<Record<string, MCPTool>>;\n /** An optional method for cleanup if the underlying client requires explicit disconnection. */\n close?(): Promise<void>;\n}\n\n/**\n * Configuration for connecting to an MCP endpoint.\n */\nexport interface MCPEndpointConfig {\n endpoint: string;\n apiKey?: string;\n}\n\n/**\n * Extracts CopilotKit-compatible parameters from an MCP tool schema.\n * @param toolOrSchema The schema object from an MCPTool or the full MCPTool object.\n * @returns An array of Parameter objects.\n */\nexport function extractParametersFromSchema(\n toolOrSchema?: MCPTool | MCPTool[\"schema\"],\n): Parameter[] {\n const parameters: Parameter[] = [];\n\n // Handle either full tool object or just schema\n const schema =\n \"schema\" in (toolOrSchema || {})\n ? (toolOrSchema as MCPTool).schema\n : (toolOrSchema as MCPTool[\"schema\"]);\n\n const toolParameters = schema?.parameters?.jsonSchema || schema?.parameters;\n const properties = toolParameters?.properties;\n const requiredParams = new Set(toolParameters?.required || []);\n\n if (!properties) {\n return parameters;\n }\n\n for (const paramName in properties) {\n if (Object.prototype.hasOwnProperty.call(properties, paramName)) {\n const paramDef = properties[paramName];\n\n // Enhanced type extraction with support for complex types\n let type = paramDef.type || \"string\";\n let description = paramDef.description || \"\";\n\n // Handle arrays with items\n if (type === \"array\" && paramDef.items) {\n const itemType = paramDef.items.type || \"object\";\n if (itemType === \"object\" && paramDef.items.properties) {\n // For arrays of objects, describe the structure\n const itemProperties = Object.keys(paramDef.items.properties).join(\n \", \",\n );\n description =\n description +\n (description ? \" \" : \"\") +\n `Array of objects with properties: ${itemProperties}`;\n } else {\n // For arrays of primitives\n type = `array<${itemType}>`;\n }\n }\n\n // Handle enums\n if (paramDef.enum && Array.isArray(paramDef.enum)) {\n const enumValues = paramDef.enum.join(\" | \");\n description =\n description +\n (description ? \" \" : \"\") +\n `Allowed values: ${enumValues}`;\n }\n\n // Handle objects with properties\n if (type === \"object\" && paramDef.properties) {\n const objectProperties = Object.keys(paramDef.properties).join(\", \");\n description =\n description +\n (description ? \" \" : \"\") +\n `Object with properties: ${objectProperties}`;\n }\n\n parameters.push({\n name: paramName,\n type: type,\n description: description,\n required: requiredParams.has(paramName),\n });\n }\n }\n\n return parameters;\n}\n\n/**\n * Converts a map of MCPTools into an array of CopilotKit Actions.\n * @param mcpTools A record mapping tool names to MCPTool objects.\n * @param mcpEndpoint The endpoint URL from which these tools were fetched.\n * @returns An array of Action<any> objects.\n */\nexport function convertMCPToolsToActions(\n mcpTools: Record<string, MCPTool>,\n mcpEndpoint: string,\n): Action<any>[] {\n const actions: Action<any>[] = [];\n\n for (const [toolName, tool] of Object.entries(mcpTools)) {\n const parameters = extractParametersFromSchema(tool);\n\n const handler = async (params: any): Promise<any> => {\n try {\n const result = await tool.execute(params);\n // Ensure the result is a string or stringify it, as required by many LLMs.\n // This might need adjustment depending on how different LLMs handle tool results.\n return typeof result === \"string\" ? result : JSON.stringify(result);\n } catch (error) {\n console.error(\n `Error executing MCP tool '${toolName}' from endpoint ${mcpEndpoint}:`,\n error,\n );\n // Re-throw or format the error for the LLM\n throw new Error(\n `Execution failed for MCP tool '${toolName}': ${\n error instanceof Error ? error.message : String(error)\n }`, { cause: error },\n );\n }\n };\n\n actions.push({\n name: toolName,\n description:\n tool.description || `MCP tool: ${toolName} (from ${mcpEndpoint})`,\n parameters: parameters,\n handler: handler,\n // Add metadata for easier identification/debugging\n _isMCPTool: true,\n _mcpEndpoint: mcpEndpoint,\n } as Action<any> & { _isMCPTool: boolean; _mcpEndpoint: string }); // Type assertion for metadata\n }\n\n return actions;\n}\n\n/**\n * Generate better instructions for using MCP tools\n * This is used to enhance the system prompt with tool documentation\n */\nexport function generateMcpToolInstructions(\n toolsMap: Record<string, MCPTool>,\n): string {\n if (!toolsMap || Object.keys(toolsMap).length === 0) {\n return \"\";\n }\n\n const toolEntries = Object.entries(toolsMap);\n\n // Generate documentation for each tool\n const toolsDoc = toolEntries\n .map(([name, tool]) => {\n // Extract schema information if available\n let paramsDoc = \" No parameters required\";\n\n try {\n if (tool.schema && typeof tool.schema === \"object\") {\n const schema = tool.schema as any;\n\n // Extract parameters from JSON Schema - check both schema.parameters.properties and schema.properties\n const toolParameters =\n schema.parameters?.jsonSchema || schema.parameters;\n const properties = toolParameters?.properties || schema.properties;\n const requiredParams =\n toolParameters?.required || schema.required || [];\n\n if (properties) {\n // Build parameter documentation from properties with enhanced type information\n const paramsList = Object.entries(properties).map(\n ([paramName, propSchema]) => {\n const propDetails = propSchema as any;\n const requiredMark = requiredParams.includes(paramName)\n ? \"*\"\n : \"\";\n let typeInfo = propDetails.type || \"any\";\n let description = propDetails.description\n ? ` - ${propDetails.description}`\n : \"\";\n\n // Enhanced type display for complex schemas\n if (typeInfo === \"array\" && propDetails.items) {\n const itemType = propDetails.items.type || \"object\";\n if (itemType === \"object\" && propDetails.items.properties) {\n const itemProps = Object.keys(\n propDetails.items.properties,\n ).join(\", \");\n typeInfo = `array<object>`;\n description =\n description +\n (description ? \" \" : \" - \") +\n `Array of objects with properties: ${itemProps}`;\n } else {\n typeInfo = `array<${itemType}>`;\n }\n }\n\n // Handle enums\n if (propDetails.enum && Array.isArray(propDetails.enum)) {\n const enumValues = propDetails.enum.join(\" | \");\n description =\n description +\n (description ? \" \" : \" - \") +\n `Allowed values: ${enumValues}`;\n }\n\n // Handle objects\n if (typeInfo === \"object\" && propDetails.properties) {\n const objectProps = Object.keys(propDetails.properties).join(\n \", \",\n );\n description =\n description +\n (description ? \" \" : \" - \") +\n `Object with properties: ${objectProps}`;\n }\n\n return ` - ${paramName}${requiredMark} (${typeInfo})${description}`;\n },\n );\n\n if (paramsList.length > 0) {\n paramsDoc = paramsList.join(\"\\n\");\n }\n }\n }\n } catch (e) {\n console.error(`Error parsing schema for tool ${name}:`, e);\n }\n\n return `- ${name}: ${tool.description || \"\"}\n${paramsDoc}`;\n })\n .join(\"\\n\\n\");\n\n return `You have access to the following external tools provided by Model Context Protocol (MCP) servers:\n\n${toolsDoc}\n\nWhen using these tools:\n1. Only provide valid parameters according to their type requirements\n2. Required parameters are marked with *\n3. For array parameters, provide data in the correct array format\n4. For object parameters, include all required nested properties\n5. For enum parameters, use only the allowed values listed\n6. Format API calls correctly with the expected parameter structure\n7. Always check tool responses to determine your next action`;\n}\n"],"mappings":";;;;;;;;AA0CA,SAAgB,4BACd,cACa;CACb,MAAM,aAA0B,EAAE;CAGlC,MAAM,SACJ,aAAa,gBAAgB,EAAE,IAC1B,aAAyB,SACzB;CAEP,MAAM,iBAAiB,QAAQ,YAAY,cAAc,QAAQ;CACjE,MAAM,aAAa,gBAAgB;CACnC,MAAM,iBAAiB,IAAI,IAAI,gBAAgB,YAAY,EAAE,CAAC;AAE9D,KAAI,CAAC,WACH,QAAO;AAGT,MAAK,MAAM,aAAa,WACtB,KAAI,OAAO,UAAU,eAAe,KAAK,YAAY,UAAU,EAAE;EAC/D,MAAM,WAAW,WAAW;EAG5B,IAAI,OAAO,SAAS,QAAQ;EAC5B,IAAI,cAAc,SAAS,eAAe;AAG1C,MAAI,SAAS,WAAW,SAAS,OAAO;GACtC,MAAM,WAAW,SAAS,MAAM,QAAQ;AACxC,OAAI,aAAa,YAAY,SAAS,MAAM,YAAY;IAEtD,MAAM,iBAAiB,OAAO,KAAK,S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I,EAAE;;AAG5D,SAAO,KAAK,KAAK,IAAI,KAAK,eAAe,GAAG;EAChD;GACI,CACD,KAAK,OAAO,CAIN"}
@@ -56,7 +56,7 @@ function convertMCPToolsToActions(mcpTools, mcpEndpoint) {
56
56
  return typeof result === "string" ? result : JSON.stringify(result);
57
57
  } catch (error) {
58
58
  console.error(`Error executing MCP tool '${toolName}' from endpoint ${mcpEndpoint}:`, error);
59
- throw new Error(`Execution failed for MCP tool '${toolName}': ${error instanceof Error ? error.message : String(error)}`);
59
+ throw new Error(`Execution failed for MCP tool '${toolName}': ${error instanceof Error ? error.message : String(error)}`, { cause: error });
60
60
  }
61
61
  };
62
62
  actions.push({
@@ -1 +1 @@
1
- {"version":3,"file":"mcp-tools-utils.mjs","names":[],"sources":["../../../src/lib/runtime/mcp-tools-utils.ts"],"sourcesContent":["import { Action, Parameter } from \"@copilotkit/shared\";\n\n/**\n * Represents a tool provided by an MCP server.\n */\nexport interface MCPTool {\n description?: string;\n /** Schema defining parameters, mirroring the MCP structure. */\n schema?: {\n parameters?: {\n properties?: Record<string, any>;\n required?: string[];\n jsonSchema?: Record<string, any>;\n };\n };\n /** The function to call to execute the tool on the MCP server. */\n execute(params: any): Promise<any>;\n}\n\n/**\n * Defines the contract for *any* MCP client implementation the user might provide.\n */\nexport interface MCPClient {\n /** A method that returns a map of tool names to MCPTool objects available from the connected MCP server. */\n tools(): Promise<Record<string, MCPTool>>;\n /** An optional method for cleanup if the underlying client requires explicit disconnection. */\n close?(): Promise<void>;\n}\n\n/**\n * Configuration for connecting to an MCP endpoint.\n */\nexport interface MCPEndpointConfig {\n endpoint: string;\n apiKey?: string;\n}\n\n/**\n * Extracts CopilotKit-compatible parameters from an MCP tool schema.\n * @param toolOrSchema The schema object from an MCPTool or the full MCPTool object.\n * @returns An array of Parameter objects.\n */\nexport function extractParametersFromSchema(\n toolOrSchema?: MCPTool | MCPTool[\"schema\"],\n): Parameter[] {\n const parameters: Parameter[] = [];\n\n // Handle either full tool object or just schema\n const schema =\n \"schema\" in (toolOrSchema || {})\n ? (toolOrSchema as MCPTool).schema\n : (toolOrSchema as MCPTool[\"schema\"]);\n\n const toolParameters = schema?.parameters?.jsonSchema || schema?.parameters;\n const properties = toolParameters?.properties;\n const requiredParams = new Set(toolParameters?.required || []);\n\n if (!properties) {\n return parameters;\n }\n\n for (const paramName in properties) {\n if (Object.prototype.hasOwnProperty.call(properties, paramName)) {\n const paramDef = properties[paramName];\n\n // Enhanced type extraction with support for complex types\n let type = paramDef.type || \"string\";\n let description = paramDef.description || \"\";\n\n // Handle arrays with items\n if (type === \"array\" && paramDef.items) {\n const itemType = paramDef.items.type || \"object\";\n if (itemType === \"object\" && paramDef.items.properties) {\n // For arrays of objects, describe the structure\n const itemProperties = Object.keys(paramDef.items.properties).join(\n \", \",\n );\n description =\n description +\n (description ? \" \" : \"\") +\n `Array of objects with properties: ${itemProperties}`;\n } else {\n // For arrays of primitives\n type = `array<${itemType}>`;\n }\n }\n\n // Handle enums\n if (paramDef.enum && Array.isArray(paramDef.enum)) {\n const enumValues = paramDef.enum.join(\" | \");\n description =\n description +\n (description ? \" \" : \"\") +\n `Allowed values: ${enumValues}`;\n }\n\n // Handle objects with properties\n if (type === \"object\" && paramDef.properties) {\n const objectProperties = Object.keys(paramDef.properties).join(\", \");\n description =\n description +\n (description ? \" \" : \"\") +\n `Object with properties: ${objectProperties}`;\n }\n\n parameters.push({\n name: paramName,\n type: type,\n description: description,\n required: requiredParams.has(paramName),\n });\n }\n }\n\n return parameters;\n}\n\n/**\n * Converts a map of MCPTools into an array of CopilotKit Actions.\n * @param mcpTools A record mapping tool names to MCPTool objects.\n * @param mcpEndpoint The endpoint URL from which these tools were fetched.\n * @returns An array of Action<any> objects.\n */\nexport function convertMCPToolsToActions(\n mcpTools: Record<string, MCPTool>,\n mcpEndpoint: string,\n): Action<any>[] {\n const actions: Action<any>[] = [];\n\n for (const [toolName, tool] of Object.entries(mcpTools)) {\n const parameters = extractParametersFromSchema(tool);\n\n const handler = async (params: any): Promise<any> => {\n try {\n const result = await tool.execute(params);\n // Ensure the result is a string or stringify it, as required by many LLMs.\n // This might need adjustment depending on how different LLMs handle tool results.\n return typeof result === \"string\" ? result : JSON.stringify(result);\n } catch (error) {\n console.error(\n `Error executing MCP tool '${toolName}' from endpoint ${mcpEndpoint}:`,\n error,\n );\n // Re-throw or format the error for the LLM\n throw new Error(\n `Execution failed for MCP tool '${toolName}': ${\n error instanceof Error ? error.message : String(error)\n }`,\n );\n }\n };\n\n actions.push({\n name: toolName,\n description:\n tool.description || `MCP tool: ${toolName} (from ${mcpEndpoint})`,\n parameters: parameters,\n handler: handler,\n // Add metadata for easier identification/debugging\n _isMCPTool: true,\n _mcpEndpoint: mcpEndpoint,\n } as Action<any> & { _isMCPTool: boolean; _mcpEndpoint: string }); // Type assertion for metadata\n }\n\n return actions;\n}\n\n/**\n * Generate better instructions for using MCP tools\n * This is used to enhance the system prompt with tool documentation\n */\nexport function generateMcpToolInstructions(\n toolsMap: Record<string, MCPTool>,\n): string {\n if (!toolsMap || Object.keys(toolsMap).length === 0) {\n return \"\";\n }\n\n const toolEntries = Object.entries(toolsMap);\n\n // Generate documentation for each tool\n const toolsDoc = toolEntries\n .map(([name, tool]) => {\n // Extract schema information if available\n let paramsDoc = \" No parameters required\";\n\n try {\n if (tool.schema && typeof tool.schema === \"object\") {\n const schema = tool.schema as any;\n\n // Extract parameters from JSON Schema - check both schema.parameters.properties and schema.properties\n const toolParameters =\n schema.parameters?.jsonSchema || schema.parameters;\n const properties = toolParameters?.properties || schema.properties;\n const requiredParams =\n toolParameters?.required || schema.required || [];\n\n if (properties) {\n // Build parameter documentation from properties with enhanced type information\n const paramsList = Object.entries(properties).map(\n ([paramName, propSchema]) => {\n const propDetails = propSchema as any;\n const requiredMark = requiredParams.includes(paramName)\n ? \"*\"\n : \"\";\n let typeInfo = propDetails.type || \"any\";\n let description = propDetails.description\n ? ` - ${propDetails.description}`\n : \"\";\n\n // Enhanced type display for complex schemas\n if (typeInfo === \"array\" && propDetails.items) {\n const itemType = propDetails.items.type || \"object\";\n if (itemType === \"object\" && propDetails.items.properties) {\n const itemProps = Object.keys(\n propDetails.items.properties,\n ).join(\", \");\n typeInfo = `array<object>`;\n description =\n description +\n (description ? \" \" : \" - \") +\n `Array of objects with properties: ${itemProps}`;\n } else {\n typeInfo = `array<${itemType}>`;\n }\n }\n\n // Handle enums\n if (propDetails.enum && Array.isArray(propDetails.enum)) {\n const enumValues = propDetails.enum.join(\" | \");\n description =\n description +\n (description ? \" \" : \" - \") +\n `Allowed values: ${enumValues}`;\n }\n\n // Handle objects\n if (typeInfo === \"object\" && propDetails.properties) {\n const objectProps = Object.keys(propDetails.properties).join(\n \", \",\n );\n description =\n description +\n (description ? \" \" : \" - \") +\n `Object with properties: ${objectProps}`;\n }\n\n return ` - ${paramName}${requiredMark} (${typeInfo})${description}`;\n },\n );\n\n if (paramsList.length > 0) {\n paramsDoc = paramsList.join(\"\\n\");\n }\n }\n }\n } catch (e) {\n console.error(`Error parsing schema for tool ${name}:`, e);\n }\n\n return `- ${name}: ${tool.description || \"\"}\n${paramsDoc}`;\n })\n .join(\"\\n\\n\");\n\n return `You have access to the following external tools provided by Model Context Protocol (MCP) servers:\n\n${toolsDoc}\n\nWhen using these tools:\n1. Only provide valid parameters according to their type requirements\n2. Required parameters are marked with *\n3. For array parameters, provide data in the correct array format\n4. For object parameters, include all required nested properties\n5. For enum parameters, use only the allowed values listed\n6. Format API calls correctly with the expected parameter structure\n7. Always check tool responses to determine your next action`;\n}\n"],"mappings":";;;;;;;AA0CA,SAAgB,4BACd,cACa;CACb,MAAM,aAA0B,EAAE;CAGlC,MAAM,SACJ,aAAa,gBAAgB,EAAE,IAC1B,aAAyB,SACzB;CAEP,MAAM,iBAAiB,QAAQ,YAAY,cAAc,QAAQ;CACjE,MAAM,aAAa,gBAAgB;CACnC,MAAM,iBAAiB,IAAI,IAAI,gBAAgB,YAAY,EAAE,CAAC;AAE9D,KAAI,CAAC,WACH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;;AAIvB,UAAI,aAAa,YAAY,YAAY,YAAY;OACnD,MAAM,cAAc,OAAO,KAAK,YAAY,WAAW,CAAC,KACtD,KACD;AACD,qBACE,eACC,cAAc,MAAM,SACrB,2BAA2B;;AAG/B,aAAO,SAAS,YAAY,aAAa,IAAI,SAAS,GAAG;OAE5D;AAED,SAAI,WAAW,SAAS,EACtB,aAAY,WAAW,KAAK,KAAK;;;WAIhC,GAAG;AACV,WAAQ,MAAM,iCAAiC,KAAK,IAAI,EAAE;;AAG5D,SAAO,KAAK,KAAK,IAAI,KAAK,eAAe,GAAG;EAChD;GACI,CACD,KAAK,OAAO,CAIN"}
1
+ {"version":3,"file":"mcp-tools-utils.mjs","names":[],"sources":["../../../src/lib/runtime/mcp-tools-utils.ts"],"sourcesContent":["import { Action, Parameter } from \"@copilotkit/shared\";\n\n/**\n * Represents a tool provided by an MCP server.\n */\nexport interface MCPTool {\n description?: string;\n /** Schema defining parameters, mirroring the MCP structure. */\n schema?: {\n parameters?: {\n properties?: Record<string, any>;\n required?: string[];\n jsonSchema?: Record<string, any>;\n };\n };\n /** The function to call to execute the tool on the MCP server. */\n execute(params: any): Promise<any>;\n}\n\n/**\n * Defines the contract for *any* MCP client implementation the user might provide.\n */\nexport interface MCPClient {\n /** A method that returns a map of tool names to MCPTool objects available from the connected MCP server. */\n tools(): Promise<Record<string, MCPTool>>;\n /** An optional method for cleanup if the underlying client requires explicit disconnection. */\n close?(): Promise<void>;\n}\n\n/**\n * Configuration for connecting to an MCP endpoint.\n */\nexport interface MCPEndpointConfig {\n endpoint: string;\n apiKey?: string;\n}\n\n/**\n * Extracts CopilotKit-compatible parameters from an MCP tool schema.\n * @param toolOrSchema The schema object from an MCPTool or the full MCPTool object.\n * @returns An array of Parameter objects.\n */\nexport function extractParametersFromSchema(\n toolOrSchema?: MCPTool | MCPTool[\"schema\"],\n): Parameter[] {\n const parameters: Parameter[] = [];\n\n // Handle either full tool object or just schema\n const schema =\n \"schema\" in (toolOrSchema || {})\n ? (toolOrSchema as MCPTool).schema\n : (toolOrSchema as MCPTool[\"schema\"]);\n\n const toolParameters = schema?.parameters?.jsonSchema || schema?.parameters;\n const properties = toolParameters?.properties;\n const requiredParams = new Set(toolParameters?.required || []);\n\n if (!properties) {\n return parameters;\n }\n\n for (const paramName in properties) {\n if (Object.prototype.hasOwnProperty.call(properties, paramName)) {\n const paramDef = properties[paramName];\n\n // Enhanced type extraction with support for complex types\n let type = paramDef.type || \"string\";\n let description = paramDef.description || \"\";\n\n // Handle arrays with items\n if (type === \"array\" && paramDef.items) {\n const itemType = paramDef.items.type || \"object\";\n if (itemType === \"object\" && paramDef.items.properties) {\n // For arrays of objects, describe the structure\n const itemProperties = Object.keys(paramDef.items.properties).join(\n \", \",\n );\n description =\n description +\n (description ? \" \" : \"\") +\n `Array of objects with properties: ${itemProperties}`;\n } else {\n // For arrays of primitives\n type = `array<${itemType}>`;\n }\n }\n\n // Handle enums\n if (paramDef.enum && Array.isArray(paramDef.enum)) {\n const enumValues = paramDef.enum.join(\" | \");\n description =\n description +\n (description ? \" \" : \"\") +\n `Allowed values: ${enumValues}`;\n }\n\n // Handle objects with properties\n if (type === \"object\" && paramDef.properties) {\n const objectProperties = Object.keys(paramDef.properties).join(\", \");\n description =\n description +\n (description ? \" \" : \"\") +\n `Object with properties: ${objectProperties}`;\n }\n\n parameters.push({\n name: paramName,\n type: type,\n description: description,\n required: requiredParams.has(paramName),\n });\n }\n }\n\n return parameters;\n}\n\n/**\n * Converts a map of MCPTools into an array of CopilotKit Actions.\n * @param mcpTools A record mapping tool names to MCPTool objects.\n * @param mcpEndpoint The endpoint URL from which these tools were fetched.\n * @returns An array of Action<any> objects.\n */\nexport function convertMCPToolsToActions(\n mcpTools: Record<string, MCPTool>,\n mcpEndpoint: string,\n): Action<any>[] {\n const actions: Action<any>[] = [];\n\n for (const [toolName, tool] of Object.entries(mcpTools)) {\n const parameters = extractParametersFromSchema(tool);\n\n const handler = async (params: any): Promise<any> => {\n try {\n const result = await tool.execute(params);\n // Ensure the result is a string or stringify it, as required by many LLMs.\n // This might need adjustment depending on how different LLMs handle tool results.\n return typeof result === \"string\" ? result : JSON.stringify(result);\n } catch (error) {\n console.error(\n `Error executing MCP tool '${toolName}' from endpoint ${mcpEndpoint}:`,\n error,\n );\n // Re-throw or format the error for the LLM\n throw new Error(\n `Execution failed for MCP tool '${toolName}': ${\n error instanceof Error ? error.message : String(error)\n }`, { cause: error },\n );\n }\n };\n\n actions.push({\n name: toolName,\n description:\n tool.description || `MCP tool: ${toolName} (from ${mcpEndpoint})`,\n parameters: parameters,\n handler: handler,\n // Add metadata for easier identification/debugging\n _isMCPTool: true,\n _mcpEndpoint: mcpEndpoint,\n } as Action<any> & { _isMCPTool: boolean; _mcpEndpoint: string }); // Type assertion for metadata\n }\n\n return actions;\n}\n\n/**\n * Generate better instructions for using MCP tools\n * This is used to enhance the system prompt with tool documentation\n */\nexport function generateMcpToolInstructions(\n toolsMap: Record<string, MCPTool>,\n): string {\n if (!toolsMap || Object.keys(toolsMap).length === 0) {\n return \"\";\n }\n\n const toolEntries = Object.entries(toolsMap);\n\n // Generate documentation for each tool\n const toolsDoc = toolEntries\n .map(([name, tool]) => {\n // Extract schema information if available\n let paramsDoc = \" No parameters required\";\n\n try {\n if (tool.schema && typeof tool.schema === \"object\") {\n const schema = tool.schema as any;\n\n // Extract parameters from JSON Schema - check both schema.parameters.properties and schema.properties\n const toolParameters =\n schema.parameters?.jsonSchema || schema.parameters;\n const properties = toolParameters?.properties || schema.properties;\n const requiredParams =\n toolParameters?.required || schema.required || [];\n\n if (properties) {\n // Build parameter documentation from properties with enhanced type information\n const paramsList = Object.entries(properties).map(\n ([paramName, propSchema]) => {\n const propDetails = propSchema as any;\n const requiredMark = requiredParams.includes(paramName)\n ? \"*\"\n : \"\";\n let typeInfo = propDetails.type || \"any\";\n let description = propDetails.description\n ? ` - ${propDetails.description}`\n : \"\";\n\n // Enhanced type display for complex schemas\n if (typeInfo === \"array\" && propDetails.items) {\n const itemType = propDetails.items.type || \"object\";\n if (itemType === \"object\" && propDetails.items.properties) {\n const itemProps = Object.keys(\n propDetails.items.properties,\n ).join(\", \");\n typeInfo = `array<object>`;\n description =\n description +\n (description ? \" \" : \" - \") +\n `Array of objects with properties: ${itemProps}`;\n } else {\n typeInfo = `array<${itemType}>`;\n }\n }\n\n // Handle enums\n if (propDetails.enum && Array.isArray(propDetails.enum)) {\n const enumValues = propDetails.enum.join(\" | \");\n description =\n description +\n (description ? \" \" : \" - \") +\n `Allowed values: ${enumValues}`;\n }\n\n // Handle objects\n if (typeInfo === \"object\" && propDetails.properties) {\n const objectProps = Object.keys(propDetails.properties).join(\n \", \",\n );\n description =\n description +\n (description ? \" \" : \" - \") +\n `Object with properties: ${objectProps}`;\n }\n\n return ` - ${paramName}${requiredMark} (${typeInfo})${description}`;\n },\n );\n\n if (paramsList.length > 0) {\n paramsDoc = paramsList.join(\"\\n\");\n }\n }\n }\n } catch (e) {\n console.error(`Error parsing schema for tool ${name}:`, e);\n }\n\n return `- ${name}: ${tool.description || \"\"}\n${paramsDoc}`;\n })\n .join(\"\\n\\n\");\n\n return `You have access to the following external tools provided by Model Context Protocol (MCP) servers:\n\n${toolsDoc}\n\nWhen using these tools:\n1. Only provide valid parameters according to their type requirements\n2. Required parameters are marked with *\n3. For array parameters, provide data in the correct array format\n4. For object parameters, include all required nested properties\n5. For enum parameters, use only the allowed values listed\n6. Format API calls correctly with the expected parameter structure\n7. Always check tool responses to determine your next action`;\n}\n"],"mappings":";;;;;;;AA0CA,SAAgB,4BACd,cACa;CACb,MAAM,aAA0B,EAAE;CAGlC,MAAM,SACJ,aAAa,gBAAgB,EAAE,IAC1B,aAAyB,SACzB;CAEP,MAAM,iBAAiB,QAAQ,YAAY,cAAc,QAAQ;CACjE,MAAM,aAAa,gBAAgB;CACnC,MAAM,iBAAiB,IAAI,IAAI,gBAAgB,YAAY,EAAE,CAAC;AAE9D,KAAI,CAAC,WACH,QAAO;AAGT,MAAK,MAAM,aA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IvB,UAAI,aAAa,YAAY,YAAY,YAAY;OACnD,MAAM,cAAc,OAAO,KAAK,YAAY,WAAW,CAAC,KACtD,KACD;AACD,qBACE,eACC,cAAc,MAAM,SACrB,2BAA2B;;AAG/B,aAAO,SAAS,YAAY,aAAa,IAAI,SAAS,GAAG;OAE5D;AAED,SAAI,WAAW,SAAS,EACtB,aAAY,WAAW,KAAK,KAAK;;;WAIhC,GAAG;AACV,WAAQ,MAAM,iCAAiC,KAAK,IAAI,EAAE;;AAG5D,SAAO,KAAK,KAAK,IAAI,KAAK,eAAe,GAAG;EAChD;GACI,CACD,KAAK,OAAO,CAIN"}
package/dist/package.cjs CHANGED
@@ -5,7 +5,7 @@ const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
5
5
  var require_package = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, module) => {
6
6
  module.exports = {
7
7
  "name": "@copilotkit/runtime",
8
- "version": "1.55.1",
8
+ "version": "1.55.2-next.1",
9
9
  "private": false,
10
10
  "keywords": [
11
11
  "ai",
@@ -81,7 +81,7 @@ var require_package = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, m
81
81
  "attw": "attw --pack . --profile node16"
82
82
  },
83
83
  "dependencies": {
84
- "@ag-ui/a2ui-middleware": "0.0.3",
84
+ "@ag-ui/a2ui-middleware": "0.0.4",
85
85
  "@ag-ui/client": "0.0.52",
86
86
  "@ag-ui/core": "0.0.52",
87
87
  "@ag-ui/encoder": "0.0.52",
@@ -123,6 +123,7 @@ var require_package = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, m
123
123
  "zod": "^3.23.3"
124
124
  },
125
125
  "devDependencies": {
126
+ "@copilotkit/aimock": "^1.10.0",
126
127
  "@swc/core": "1.5.28",
127
128
  "@types/cors": "^2.8.17",
128
129
  "@types/express": "^4.17.21",
package/dist/package.mjs CHANGED
@@ -5,7 +5,7 @@ import { __commonJSMin } from "./_virtual/_rolldown/runtime.mjs";
5
5
  var require_package = /* @__PURE__ */ __commonJSMin(((exports, module) => {
6
6
  module.exports = {
7
7
  "name": "@copilotkit/runtime",
8
- "version": "1.55.1",
8
+ "version": "1.55.2-next.1",
9
9
  "private": false,
10
10
  "keywords": [
11
11
  "ai",
@@ -81,7 +81,7 @@ var require_package = /* @__PURE__ */ __commonJSMin(((exports, module) => {
81
81
  "attw": "attw --pack . --profile node16"
82
82
  },
83
83
  "dependencies": {
84
- "@ag-ui/a2ui-middleware": "0.0.3",
84
+ "@ag-ui/a2ui-middleware": "0.0.4",
85
85
  "@ag-ui/client": "0.0.52",
86
86
  "@ag-ui/core": "0.0.52",
87
87
  "@ag-ui/encoder": "0.0.52",
@@ -123,6 +123,7 @@ var require_package = /* @__PURE__ */ __commonJSMin(((exports, module) => {
123
123
  "zod": "^3.23.3"
124
124
  },
125
125
  "devDependencies": {
126
+ "@copilotkit/aimock": "^1.10.0",
126
127
  "@swc/core": "1.5.28",
127
128
  "@types/cors": "^2.8.17",
128
129
  "@types/express": "^4.17.21",
@@ -13,7 +13,7 @@ function limitMessagesToTokenCount(messages, tools, model, maxTokens) {
13
13
  if (maxTokens < 0) throw new Error("Not enough tokens for system message.");
14
14
  }
15
15
  let cutoff = false;
16
- const reversedMessages = [...messages].reverse();
16
+ const reversedMessages = [...messages].toReversed();
17
17
  for (const message of reversedMessages) {
18
18
  if (message.role === "system") {
19
19
  result.unshift(message);
@@ -1 +1 @@
1
- {"version":3,"file":"utils.cjs","names":[],"sources":["../../../src/service-adapters/anthropic/utils.ts"],"sourcesContent":["import { Anthropic } from \"@anthropic-ai/sdk\";\nimport { ActionInput } from \"../../graphql/inputs/action.input\";\nimport { Message } from \"../../graphql/types/converted\";\n\nexport function limitMessagesToTokenCount(\n messages: any[],\n tools: any[],\n model: string,\n maxTokens?: number,\n): any[] {\n maxTokens ||= MAX_TOKENS;\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 (message.role === \"system\") {\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].reverse();\n for (const message of reversedMessages) {\n if (message.role === \"system\") {\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\nconst MAX_TOKENS = 128000;\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, JSON.stringify(message.content) || \"\");\n}\n\nfunction countTokens(model: string, text: string): number {\n return text.length / 3;\n}\n\nexport function convertActionInputToAnthropicTool(\n action: ActionInput,\n): Anthropic.Messages.Tool {\n return {\n name: action.name,\n description: action.description,\n input_schema: JSON.parse(action.jsonSchema),\n };\n}\n\nexport function convertMessageToAnthropicMessage(\n message: Message,\n): Anthropic.Messages.MessageParam {\n if (message.isTextMessage()) {\n if (message.role === \"system\") {\n return {\n role: \"assistant\",\n content: [\n {\n type: \"text\",\n text:\n \"THE FOLLOWING MESSAGE IS A SYSTEM MESSAGE: \" + message.content,\n },\n ],\n };\n } else {\n return {\n role: message.role === \"user\" ? \"user\" : \"assistant\",\n content: [{ type: \"text\", text: message.content }],\n };\n }\n } else if (message.isImageMessage()) {\n let mediaType: \"image/jpeg\" | \"image/png\" | \"image/webp\" | \"image/gif\";\n switch (message.format) {\n case \"jpeg\":\n mediaType = \"image/jpeg\";\n break;\n case \"png\":\n mediaType = \"image/png\";\n break;\n case \"webp\":\n mediaType = \"image/webp\";\n break;\n case \"gif\":\n mediaType = \"image/gif\";\n break;\n default:\n throw new Error(`Unsupported image format: ${message.format}`);\n }\n\n return {\n role: \"user\",\n content: [\n {\n type: \"image\",\n source: {\n type: \"base64\",\n media_type: mediaType,\n data: message.bytes,\n },\n },\n ],\n };\n } else if (message.isActionExecutionMessage()) {\n return {\n role: \"assistant\",\n content: [\n {\n id: message.id,\n type: \"tool_use\",\n input: message.arguments,\n name: message.name,\n },\n ],\n };\n } else if (message.isResultMessage()) {\n return {\n role: \"user\",\n content: [\n {\n type: \"tool_result\",\n content: message.result || \"Action completed successfully\",\n tool_use_id: message.actionExecutionId,\n },\n ],\n };\n }\n}\n"],"mappings":";;;AAIA,SAAgB,0BACd,UACA,OACA,OACA,WACO;AACP,eAAc;CAEd,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,QAAQ,SAAS,UAAU;EAC7B,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,SAAS;AAChD,MAAK,MAAM,WAAW,kBAAkB;AACtC,MAAI,QAAQ,SAAS,UAAU;AAC7B,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,MAAM,aAAa;AAEnB,SAAS,iBAAiB,OAAe,OAAsB;AAC7D,KAAI,MAAM,WAAW,EACnB,QAAO;AAGT,QAAO,YAAY,OADN,KAAK,UAAU,MAAM,CACH;;AAGjC,SAAS,mBAAmB,OAA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esContent":["import { Anthropic } from \"@anthropic-ai/sdk\";\nimport { ActionInput } from \"../../graphql/inputs/action.input\";\nimport { Message } from \"../../graphql/types/converted\";\n\nexport function limitMessagesToTokenCount(\n messages: any[],\n tools: any[],\n model: string,\n maxTokens?: number,\n): any[] {\n maxTokens ||= MAX_TOKENS;\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 (message.role === \"system\") {\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 (message.role === \"system\") {\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\nconst MAX_TOKENS = 128000;\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, JSON.stringify(message.content) || \"\");\n}\n\nfunction countTokens(model: string, text: string): number {\n return text.length / 3;\n}\n\nexport function convertActionInputToAnthropicTool(\n action: ActionInput,\n): Anthropic.Messages.Tool {\n return {\n name: action.name,\n description: action.description,\n input_schema: JSON.parse(action.jsonSchema),\n };\n}\n\nexport function convertMessageToAnthropicMessage(\n message: Message,\n): Anthropic.Messages.MessageParam {\n if (message.isTextMessage()) {\n if (message.role === \"system\") {\n return {\n role: \"assistant\",\n content: [\n {\n type: \"text\",\n text:\n \"THE FOLLOWING MESSAGE IS A SYSTEM MESSAGE: \" + message.content,\n },\n ],\n };\n } else {\n return {\n role: message.role === \"user\" ? \"user\" : \"assistant\",\n content: [{ type: \"text\", text: message.content }],\n };\n }\n } else if (message.isImageMessage()) {\n let mediaType: \"image/jpeg\" | \"image/png\" | \"image/webp\" | \"image/gif\";\n switch (message.format) {\n case \"jpeg\":\n mediaType = \"image/jpeg\";\n break;\n case \"png\":\n mediaType = \"image/png\";\n break;\n case \"webp\":\n mediaType = \"image/webp\";\n break;\n case \"gif\":\n mediaType = \"image/gif\";\n break;\n default:\n throw new Error(`Unsupported image format: ${message.format}`);\n }\n\n return {\n role: \"user\",\n content: [\n {\n type: \"image\",\n source: {\n type: \"base64\",\n media_type: mediaType,\n data: message.bytes,\n },\n },\n ],\n };\n } else if (message.isActionExecutionMessage()) {\n return {\n role: \"assistant\",\n content: [\n {\n id: message.id,\n type: \"tool_use\",\n input: message.arguments,\n name: message.name,\n },\n ],\n };\n } else if (message.isResultMessage()) {\n return {\n role: \"user\",\n content: [\n {\n type: \"tool_result\",\n content: message.result || \"Action completed successfully\",\n tool_use_id: message.actionExecutionId,\n },\n ],\n };\n }\n}\n"],"mappings":";;;AAIA,SAAgB,0BACd,UACA,OACA,OACA,WACO;AACP,eAAc;CAEd,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,QAAQ,SAAS,UAAU;EAC7B,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,QAAQ,SAAS,UAAU;AAC7B,UAAO,QAAQ,QAAQ;AAC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;EACL,MAAM;EACN,SAAS,CACP;GACE,IAAI,QAAQ;GACZ,MAAM;GACN,OAAO,QAAQ;GACf,MAAM,QAAQ;GACf,CACF;EACF;UACQ,QAAQ,iBAAiB,CAClC,QAAO;EACL,MAAM;EACN,SAAS,CACP;GACE,MAAM;GACN,SAAS,QAAQ,UAAU;GAC3B,aAAa,QAAQ;GACtB,CACF;EACF"}
@@ -12,7 +12,7 @@ function limitMessagesToTokenCount(messages, tools, model, maxTokens) {
12
12
  if (maxTokens < 0) throw new Error("Not enough tokens for system message.");
13
13
  }
14
14
  let cutoff = false;
15
- const reversedMessages = [...messages].reverse();
15
+ const reversedMessages = [...messages].toReversed();
16
16
  for (const message of reversedMessages) {
17
17
  if (message.role === "system") {
18
18
  result.unshift(message);
@@ -1 +1 @@
1
- {"version":3,"file":"utils.mjs","names":[],"sources":["../../../src/service-adapters/anthropic/utils.ts"],"sourcesContent":["import { Anthropic } from \"@anthropic-ai/sdk\";\nimport { ActionInput } from \"../../graphql/inputs/action.input\";\nimport { Message } from \"../../graphql/types/converted\";\n\nexport function limitMessagesToTokenCount(\n messages: any[],\n tools: any[],\n model: string,\n maxTokens?: number,\n): any[] {\n maxTokens ||= MAX_TOKENS;\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 (message.role === \"system\") {\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].reverse();\n for (const message of reversedMessages) {\n if (message.role === \"system\") {\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\nconst MAX_TOKENS = 128000;\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, JSON.stringify(message.content) || \"\");\n}\n\nfunction countTokens(model: string, text: string): number {\n return text.length / 3;\n}\n\nexport function convertActionInputToAnthropicTool(\n action: ActionInput,\n): Anthropic.Messages.Tool {\n return {\n name: action.name,\n description: action.description,\n input_schema: JSON.parse(action.jsonSchema),\n };\n}\n\nexport function convertMessageToAnthropicMessage(\n message: Message,\n): Anthropic.Messages.MessageParam {\n if (message.isTextMessage()) {\n if (message.role === \"system\") {\n return {\n role: \"assistant\",\n content: [\n {\n type: \"text\",\n text:\n \"THE FOLLOWING MESSAGE IS A SYSTEM MESSAGE: \" + message.content,\n },\n ],\n };\n } else {\n return {\n role: message.role === \"user\" ? \"user\" : \"assistant\",\n content: [{ type: \"text\", text: message.content }],\n };\n }\n } else if (message.isImageMessage()) {\n let mediaType: \"image/jpeg\" | \"image/png\" | \"image/webp\" | \"image/gif\";\n switch (message.format) {\n case \"jpeg\":\n mediaType = \"image/jpeg\";\n break;\n case \"png\":\n mediaType = \"image/png\";\n break;\n case \"webp\":\n mediaType = \"image/webp\";\n break;\n case \"gif\":\n mediaType = \"image/gif\";\n break;\n default:\n throw new Error(`Unsupported image format: ${message.format}`);\n }\n\n return {\n role: \"user\",\n content: [\n {\n type: \"image\",\n source: {\n type: \"base64\",\n media_type: mediaType,\n data: message.bytes,\n },\n },\n ],\n };\n } else if (message.isActionExecutionMessage()) {\n return {\n role: \"assistant\",\n content: [\n {\n id: message.id,\n type: \"tool_use\",\n input: message.arguments,\n name: message.name,\n },\n ],\n };\n } else if (message.isResultMessage()) {\n return {\n role: \"user\",\n content: [\n {\n type: \"tool_result\",\n content: message.result || \"Action completed successfully\",\n tool_use_id: message.actionExecutionId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E,MAAM;GACN,MACE,gDAAgD,QAAQ;GAC3D,CACF;EACF;KAED,QAAO;EACL,MAAM,QAAQ,SAAS,SAAS,SAAS;EACzC,SAAS,CAAC;GAAE,MAAM;GAAQ,MAAM,QAAQ;GAAS,CAAC;EACnD;UAEM,QAAQ,gBAAgB,EAAE;EACnC,IAAI;AACJ,UAAQ,QAAQ,QAAhB;GACE,KAAK;AACH,gBAAY;AACZ;GACF,KAAK;AACH,gBAAY;AACZ;GACF,KAAK;AACH,gBAAY;AACZ;GACF,KAAK;AACH,gBAAY;AACZ;GACF,QACE,OAAM,IAAI,MAAM,6BAA6B,QAAQ,SAAS;;AAGlE,SAAO;GACL,MAAM;GACN,SAAS,CACP;IACE,MAAM;IACN,QAAQ;KACN,MAAM;KACN,YAAY;KACZ,MAAM,QAAQ;KACf;IACF,CACF;GACF;YACQ,QAAQ,0BAA0B,CAC3C,QAAO;EACL,MAAM;EACN,SAAS,CACP;GACE,IAAI,QAAQ;GACZ,MAAM;GACN,OAAO,QAAQ;GACf,MAAM,QAAQ;GACf,CACF;EACF;UACQ,QAAQ,iBAAiB,CAClC,QAAO;EACL,MAAM;EACN,SAAS,CACP;GACE,MAAM;GACN,SAAS,QAAQ,UAAU;GAC3B,aAAa,QAAQ;GACtB,CACF;EACF"}
1
+ {"version":3,"file":"utils.mjs","names":[],"sources":["../../../src/service-adapters/anthropic/utils.ts"],"sourcesContent":["import { Anthropic } from \"@anthropic-ai/sdk\";\nimport { ActionInput } from \"../../graphql/inputs/action.input\";\nimport { Message } from \"../../graphql/types/converted\";\n\nexport function limitMessagesToTokenCount(\n messages: any[],\n tools: any[],\n model: string,\n maxTokens?: number,\n): any[] {\n maxTokens ||= MAX_TOKENS;\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 (message.role === \"system\") {\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 (message.role === \"system\") {\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\nconst MAX_TOKENS = 128000;\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, JSON.stringify(message.content) || \"\");\n}\n\nfunction countTokens(model: string, text: string): number {\n return text.length / 3;\n}\n\nexport function convertActionInputToAnthropicTool(\n action: ActionInput,\n): Anthropic.Messages.Tool {\n return {\n name: action.name,\n description: action.description,\n input_schema: JSON.parse(action.jsonSchema),\n };\n}\n\nexport function convertMessageToAnthropicMessage(\n message: Message,\n): Anthropic.Messages.MessageParam {\n if (message.isTextMessage()) {\n if (message.role === \"system\") {\n return {\n role: \"assistant\",\n content: [\n {\n type: \"text\",\n text:\n \"THE FOLLOWING MESSAGE IS A SYSTEM MESSAGE: \" + message.content,\n },\n ],\n };\n } else {\n return {\n role: message.role === \"user\" ? \"user\" : \"assistant\",\n content: [{ type: \"text\", text: message.content }],\n };\n }\n } else if (message.isImageMessage()) {\n let mediaType: \"image/jpeg\" | \"image/png\" | \"image/webp\" | \"image/gif\";\n switch (message.format) {\n case \"jpeg\":\n mediaType = \"image/jpeg\";\n break;\n case \"png\":\n mediaType = \"image/png\";\n break;\n case \"webp\":\n mediaType = \"image/webp\";\n break;\n case \"gif\":\n mediaType = \"image/gif\";\n break;\n default:\n throw new Error(`Unsupported image format: ${message.format}`);\n }\n\n return {\n role: \"user\",\n content: [\n {\n type: \"image\",\n source: {\n type: \"base64\",\n media_type: mediaType,\n data: message.bytes,\n },\n },\n ],\n };\n } else if (message.isActionExecutionMessage()) {\n return {\n role: \"assistant\",\n content: [\n {\n id: message.id,\n type: \"tool_use\",\n input: message.arguments,\n name: message.name,\n },\n ],\n };\n } else if (message.isResultMessage()) {\n return {\n role: \"user\",\n content: [\n {\n type: \"tool_result\",\n content: message.result || \"Action completed successfully\",\n tool_use_id: message.actionExecutionId,\n },\n ],\n };\n }\n}\n"],"mappings":";;AAIA,SAAgB,0BACd,UACA,OACA,OACA,WACO;AACP,eAAc;CAEd,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,QAAQ,SAAS,UAAU;EAC7B,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,QAAQ,SAAS,UAAU;AAC7B,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,MAAM,aAAa;AAEnB,SAAS,iBAAiB,OAAe,OAAsB;AAC7D,KAAI,MAAM,WAAW,EACnB,QAAO;AAGT,QAAO,YAAY,OADN,KAAK,UAAU,MAAM,CACH;;AAGjC,SAAS,mBAAmB,OAAe,SAAsB;AAC/D,QAAO,YAAY,OAAO,KAAK,UAAU,QAAQ,QAAQ,IAAI,GAAG;;AAGlE,SAAS,YAAY,OAAe,MAAsB;AACxD,QAAO,KAAK,SAAS;;AAGvB,SAAgB,kCACd,QACyB;AACzB,QAAO;EACL,MAAM,OAAO;EACb,aAAa,OAAO;EACpB,cAAc,KAAK,MAAM,OAAO,WAAW;EAC5C;;AAGH,SAAgB,iCACd,SACiC;AACjC,KAAI,QAAQ,eAAe,CACzB,KAAI,QAAQ,SAAS,SACnB,QAAO;EACL,MAAM;EACN,SAAS,CACP;GACE,MAAM;GACN,MACE,gDAAgD,QAAQ;GAC3D,CACF;EACF;KAED,QAAO;EACL,MAAM,QAAQ,SAAS,SAAS,SAAS;EACzC,SAAS,CAAC;GAAE,MAAM;GAAQ,MAAM,QAAQ;GAAS,CAAC;EACnD;UAEM,QAAQ,gBAAgB,EAAE;EACnC,IAAI;AACJ,UAAQ,QAAQ,QAAhB;GACE,KAAK;AACH,gBAAY;AACZ;GACF,KAAK;AACH,gBAAY;AACZ;GACF,KAAK;AACH,gBAAY;AACZ;GACF,KAAK;AACH,gBAAY;AACZ;GACF,QACE,OAAM,IAAI,MAAM,6BAA6B,QAAQ,SAAS;;AAGlE,SAAO;GACL,MAAM;GACN,SAAS,CACP;IACE,MAAM;IACN,QAAQ;KACN,MAAM;KACN,YAAY;KACZ,MAAM,QAAQ;KACf;IACF,CACF;GACF;YACQ,QAAQ,0BAA0B,CAC3C,QAAO;EACL,MAAM;EACN,SAAS,CACP;GACE,IAAI,QAAQ;GACZ,MAAM;GACN,OAAO,QAAQ;GACf,MAAM,QAAQ;GACf,CACF;EACF;UACQ,QAAQ,iBAAiB,CAClC,QAAO;EACL,MAAM;EACN,SAAS,CACP;GACE,MAAM;GACN,SAAS,QAAQ,UAAU;GAC3B,aAAa,QAAQ;GACtB,CACF;EACF"}
@@ -15,7 +15,7 @@ function limitMessagesToTokenCount(messages, tools, model, maxTokens) {
15
15
  if (maxTokens < 0) throw new Error("Not enough tokens for system message.");
16
16
  }
17
17
  let cutoff = false;
18
- const reversedMessages = [...messages].reverse();
18
+ const reversedMessages = [...messages].toReversed();
19
19
  for (const message of reversedMessages) {
20
20
  if (["system", "developer"].includes(message.role)) {
21
21
  result.unshift(message);
@@ -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].reverse();\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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;AAGvB,SAAgB,+BACd,QACoB;AACpB,QAAO;EACL,MAAM;EACN,UAAU;GACR,MAAM,OAAO;GACb,aAAa,OAAO;GACpB,8CAAsB,OAAO,YAAY,EAAE,CAAC;GAC7C;EACF;;AAQH,SAAgB,8BACd,SACA,SAC4B;CAC5B,MAAM,EAAE,mBAAmB,WAAW,EAAE,gBAAgB,OAAO;AAC/D,KAAI,QAAQ,eAAe,EAAE;EAC3B,IAAI,OAAO,QAAQ;AACnB,MAAI,QAAQ,SAAS,YAAY,CAAC,eAChC,QAAO;AAET,SAAO;GACL;GACA,SAAS,QAAQ;GAClB;YACQ,QAAQ,gBAAgB,CACjC,QAAO;EACL,MAAM;EACN,SAAS,CACP;GACE,MAAM;GACN,WAAW,EACT,KAAK,cAAc,QAAQ,OAAO,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.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,wBAAw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EACL,MAAM;EACN,SAAS,CACP;GACE,MAAM;GACN,WAAW,EACT,KAAK,cAAc,QAAQ,OAAO,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"}
@@ -14,7 +14,7 @@ function limitMessagesToTokenCount(messages, tools, model, maxTokens) {
14
14
  if (maxTokens < 0) throw new Error("Not enough tokens for system message.");
15
15
  }
16
16
  let cutoff = false;
17
- const reversedMessages = [...messages].reverse();
17
+ const reversedMessages = [...messages].toReversed();
18
18
  for (const message of reversedMessages) {
19
19
  if (["system", "developer"].includes(message.role)) {
20
20
  result.unshift(message);
@@ -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].reverse();\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,SAAS;AAChD,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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AM;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 { 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,MAA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,6 +12,8 @@ const require_hono_single = require('./runtime/endpoints/hono-single.cjs');
12
12
  const require_express = require('./runtime/endpoints/express.cjs');
13
13
  const require_express_single = require('./runtime/endpoints/express-single.cjs');
14
14
  const require_transcription_service = require('./runtime/transcription-service/transcription-service.cjs');
15
+ const require_aisdk = require('../agent/converters/aisdk.cjs');
16
+ const require_tanstack = require('../agent/converters/tanstack.cjs');
15
17
  const require_index = require('../agent/index.cjs');
16
18
  let _copilotkit_shared = require("@copilotkit/shared");
17
19
 
@@ -26,8 +28,11 @@ exports.InMemoryAgentRunner = require_in_memory.InMemoryAgentRunner;
26
28
  exports.IntelligenceAgentRunner = require_intelligence.IntelligenceAgentRunner;
27
29
  exports.TranscriptionService = require_transcription_service.TranscriptionService;
28
30
  exports.VERSION = require_runtime$1.VERSION;
31
+ exports.convertAISDKStream = require_aisdk.convertAISDKStream;
32
+ exports.convertInputToTanStackAI = require_tanstack.convertInputToTanStackAI;
29
33
  exports.convertJsonSchemaToZodSchema = require_index.convertJsonSchemaToZodSchema;
30
34
  exports.convertMessagesToVercelAISDKMessages = require_index.convertMessagesToVercelAISDKMessages;
35
+ exports.convertTanStackStream = require_tanstack.convertTanStackStream;
31
36
  exports.convertToolDefinitionsToVercelAITools = require_index.convertToolDefinitionsToVercelAITools;
32
37
  exports.convertToolsToVercelAITools = require_index.convertToolsToVercelAITools;
33
38
  exports.createCopilotEndpoint = require_hono.createCopilotEndpoint;