@copilotkit/runtime 0.0.0-0.0.0-max-changeset-10101010101010-20260109191632

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 (122) hide show
  1. package/.eslintrc.js +7 -0
  2. package/CHANGELOG.md +2905 -0
  3. package/LICENSE +21 -0
  4. package/README.md +76 -0
  5. package/__snapshots__/schema/schema.graphql +371 -0
  6. package/dist/index.d.ts +1495 -0
  7. package/dist/index.js +5644 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/index.mjs +5601 -0
  10. package/dist/index.mjs.map +1 -0
  11. package/dist/langgraph.d.ts +284 -0
  12. package/dist/langgraph.js +211 -0
  13. package/dist/langgraph.js.map +1 -0
  14. package/dist/langgraph.mjs +206 -0
  15. package/dist/langgraph.mjs.map +1 -0
  16. package/dist/v2/index.d.ts +2 -0
  17. package/dist/v2/index.js +22 -0
  18. package/dist/v2/index.js.map +1 -0
  19. package/dist/v2/index.mjs +5 -0
  20. package/dist/v2/index.mjs.map +1 -0
  21. package/jest.config.js +10 -0
  22. package/package.json +143 -0
  23. package/scripts/generate-gql-schema.ts +13 -0
  24. package/src/agents/langgraph/event-source.ts +329 -0
  25. package/src/agents/langgraph/events.ts +377 -0
  26. package/src/graphql/inputs/action.input.ts +16 -0
  27. package/src/graphql/inputs/agent-session.input.ts +13 -0
  28. package/src/graphql/inputs/agent-state.input.ts +13 -0
  29. package/src/graphql/inputs/cloud-guardrails.input.ts +16 -0
  30. package/src/graphql/inputs/cloud.input.ts +8 -0
  31. package/src/graphql/inputs/context-property.input.ts +10 -0
  32. package/src/graphql/inputs/copilot-context.input.ts +10 -0
  33. package/src/graphql/inputs/custom-property.input.ts +15 -0
  34. package/src/graphql/inputs/extensions.input.ts +21 -0
  35. package/src/graphql/inputs/forwarded-parameters.input.ts +22 -0
  36. package/src/graphql/inputs/frontend.input.ts +14 -0
  37. package/src/graphql/inputs/generate-copilot-response.input.ts +59 -0
  38. package/src/graphql/inputs/load-agent-state.input.ts +10 -0
  39. package/src/graphql/inputs/message.input.ts +110 -0
  40. package/src/graphql/inputs/meta-event.input.ts +18 -0
  41. package/src/graphql/message-conversion/agui-to-gql.test.ts +1263 -0
  42. package/src/graphql/message-conversion/agui-to-gql.ts +333 -0
  43. package/src/graphql/message-conversion/gql-to-agui.test.ts +1580 -0
  44. package/src/graphql/message-conversion/gql-to-agui.ts +278 -0
  45. package/src/graphql/message-conversion/index.ts +2 -0
  46. package/src/graphql/message-conversion/roundtrip-conversion.test.ts +526 -0
  47. package/src/graphql/resolvers/copilot.resolver.ts +708 -0
  48. package/src/graphql/resolvers/state.resolver.ts +27 -0
  49. package/src/graphql/types/agents-response.type.ts +19 -0
  50. package/src/graphql/types/base/index.ts +10 -0
  51. package/src/graphql/types/converted/index.ts +176 -0
  52. package/src/graphql/types/copilot-response.type.ts +138 -0
  53. package/src/graphql/types/enums.ts +38 -0
  54. package/src/graphql/types/extensions-response.type.ts +23 -0
  55. package/src/graphql/types/guardrails-result.type.ts +20 -0
  56. package/src/graphql/types/load-agent-state-response.type.ts +17 -0
  57. package/src/graphql/types/message-status.type.ts +42 -0
  58. package/src/graphql/types/meta-events.type.ts +71 -0
  59. package/src/graphql/types/response-status.type.ts +66 -0
  60. package/src/index.ts +4 -0
  61. package/src/langgraph.ts +1 -0
  62. package/src/lib/cloud/index.ts +4 -0
  63. package/src/lib/error-messages.ts +200 -0
  64. package/src/lib/index.ts +52 -0
  65. package/src/lib/integrations/index.ts +6 -0
  66. package/src/lib/integrations/nest/index.ts +14 -0
  67. package/src/lib/integrations/nextjs/app-router.ts +38 -0
  68. package/src/lib/integrations/nextjs/pages-router.ts +39 -0
  69. package/src/lib/integrations/node-express/index.ts +14 -0
  70. package/src/lib/integrations/node-http/index.ts +143 -0
  71. package/src/lib/integrations/node-http/request-handler.ts +111 -0
  72. package/src/lib/integrations/shared.ts +161 -0
  73. package/src/lib/logger.ts +28 -0
  74. package/src/lib/observability.ts +160 -0
  75. package/src/lib/runtime/__tests__/copilot-runtime-error.test.ts +169 -0
  76. package/src/lib/runtime/__tests__/mcp-tools-utils.test.ts +464 -0
  77. package/src/lib/runtime/agent-integrations/langgraph/agent.ts +209 -0
  78. package/src/lib/runtime/agent-integrations/langgraph/consts.ts +34 -0
  79. package/src/lib/runtime/agent-integrations/langgraph/index.ts +2 -0
  80. package/src/lib/runtime/copilot-runtime.ts +710 -0
  81. package/src/lib/runtime/mcp-tools-utils.ts +254 -0
  82. package/src/lib/runtime/retry-utils.ts +96 -0
  83. package/src/lib/runtime/telemetry-agent-runner.ts +139 -0
  84. package/src/lib/runtime/types.ts +49 -0
  85. package/src/lib/runtime/utils.ts +87 -0
  86. package/src/lib/streaming.ts +202 -0
  87. package/src/lib/telemetry-client.ts +64 -0
  88. package/src/service-adapters/anthropic/anthropic-adapter.ts +452 -0
  89. package/src/service-adapters/anthropic/utils.ts +152 -0
  90. package/src/service-adapters/bedrock/bedrock-adapter.ts +73 -0
  91. package/src/service-adapters/conversion.ts +67 -0
  92. package/src/service-adapters/empty/empty-adapter.ts +38 -0
  93. package/src/service-adapters/events.ts +294 -0
  94. package/src/service-adapters/experimental/ollama/ollama-adapter.ts +84 -0
  95. package/src/service-adapters/google/google-genai-adapter.test.ts +104 -0
  96. package/src/service-adapters/google/google-genai-adapter.ts +88 -0
  97. package/src/service-adapters/groq/groq-adapter.ts +203 -0
  98. package/src/service-adapters/index.ts +18 -0
  99. package/src/service-adapters/langchain/langchain-adapter.ts +111 -0
  100. package/src/service-adapters/langchain/langserve.ts +88 -0
  101. package/src/service-adapters/langchain/types.ts +14 -0
  102. package/src/service-adapters/langchain/utils.ts +313 -0
  103. package/src/service-adapters/openai/openai-adapter.ts +283 -0
  104. package/src/service-adapters/openai/openai-assistant-adapter.ts +344 -0
  105. package/src/service-adapters/openai/utils.ts +199 -0
  106. package/src/service-adapters/service-adapter.ts +41 -0
  107. package/src/service-adapters/shared/error-utils.ts +61 -0
  108. package/src/service-adapters/shared/index.ts +1 -0
  109. package/src/service-adapters/unify/unify-adapter.ts +151 -0
  110. package/src/utils/failed-response-status-reasons.ts +70 -0
  111. package/src/utils/index.ts +1 -0
  112. package/src/v2/index.ts +3 -0
  113. package/tests/global.d.ts +13 -0
  114. package/tests/service-adapters/anthropic/allowlist-approach.test.ts +226 -0
  115. package/tests/service-adapters/anthropic/anthropic-adapter.test.ts +389 -0
  116. package/tests/service-adapters/openai/allowlist-approach.test.ts +238 -0
  117. package/tests/service-adapters/openai/openai-adapter.test.ts +301 -0
  118. package/tests/setup.jest.ts +21 -0
  119. package/tests/tsconfig.json +10 -0
  120. package/tsconfig.json +13 -0
  121. package/tsup.config.ts +20 -0
  122. package/typedoc.json +4 -0
@@ -0,0 +1,1495 @@
1
+ import { ReplaySubject } from 'rxjs';
2
+ import { Parameter, Action, CopilotKitLowLevelError, CopilotErrorHandler, PartialBy, MaybePromise, NonEmptyRecord } from '@copilotkit/shared';
3
+ import OpenAI from 'openai';
4
+ import { BaseMessageChunk, AIMessage, AIMessageChunk, BaseMessage } from '@langchain/core/messages';
5
+ import { DynamicStructuredTool } from '@langchain/core/tools';
6
+ import { IterableReadableStream, IterableReadableStreamInterface } from '@langchain/core/utils/stream';
7
+ import { Groq } from 'groq-sdk';
8
+ import Anthropic from '@anthropic-ai/sdk';
9
+ import * as graphql from 'graphql';
10
+ import * as createPinoLogger from 'pino';
11
+ import createPinoLogger__default from 'pino';
12
+ import { YogaInitialContext, createYoga } from 'graphql-yoga';
13
+ import { CopilotRuntimeOptions, CopilotRuntime as CopilotRuntime$1, AgentRunner } from '@copilotkitnext/runtime';
14
+ import { AbstractAgent } from '@ag-ui/client';
15
+ import * as http from 'http';
16
+ import { IncomingMessage, ServerResponse } from 'node:http';
17
+
18
+ declare enum MessageRole {
19
+ assistant = "assistant",
20
+ developer = "developer",
21
+ system = "system",
22
+ tool = "tool",
23
+ user = "user"
24
+ }
25
+ declare enum ActionInputAvailability {
26
+ disabled = "disabled",
27
+ enabled = "enabled",
28
+ remote = "remote"
29
+ }
30
+
31
+ declare class BaseMessageInput {
32
+ id: string;
33
+ createdAt: Date;
34
+ }
35
+
36
+ declare class MessageInput extends BaseMessageInput {
37
+ textMessage?: TextMessageInput;
38
+ actionExecutionMessage?: ActionExecutionMessageInput;
39
+ resultMessage?: ResultMessageInput;
40
+ agentStateMessage?: AgentStateMessageInput;
41
+ imageMessage?: ImageMessageInput;
42
+ }
43
+ declare class TextMessageInput {
44
+ content: string;
45
+ parentMessageId?: string;
46
+ role: MessageRole;
47
+ }
48
+ declare class ActionExecutionMessageInput {
49
+ name: string;
50
+ arguments: string;
51
+ parentMessageId?: string;
52
+ scope?: String;
53
+ }
54
+ declare class ResultMessageInput {
55
+ actionExecutionId: string;
56
+ actionName: string;
57
+ parentMessageId?: string;
58
+ result: string;
59
+ }
60
+ declare class AgentStateMessageInput {
61
+ threadId: string;
62
+ agentName: string;
63
+ role: MessageRole;
64
+ state: string;
65
+ running: boolean;
66
+ nodeName: string;
67
+ runId: string;
68
+ active: boolean;
69
+ }
70
+ declare class ImageMessageInput {
71
+ format: string;
72
+ bytes: string;
73
+ parentMessageId?: string;
74
+ role: MessageRole;
75
+ }
76
+
77
+ declare enum MessageStatusCode {
78
+ Pending = "pending",
79
+ Success = "success",
80
+ Failed = "failed"
81
+ }
82
+ declare class BaseMessageStatus {
83
+ code: MessageStatusCode;
84
+ }
85
+ declare class PendingMessageStatus extends BaseMessageStatus {
86
+ code: MessageStatusCode;
87
+ }
88
+ declare class SuccessMessageStatus extends BaseMessageStatus {
89
+ code: MessageStatusCode;
90
+ }
91
+ declare class FailedMessageStatus extends BaseMessageStatus {
92
+ code: MessageStatusCode;
93
+ reason: string;
94
+ }
95
+ declare const MessageStatusUnion: PendingMessageStatus | SuccessMessageStatus | FailedMessageStatus;
96
+ type MessageStatus = typeof MessageStatusUnion;
97
+
98
+ declare enum ResponseStatusCode {
99
+ Pending = "pending",
100
+ Success = "success",
101
+ Failed = "failed"
102
+ }
103
+ declare abstract class BaseResponseStatus {
104
+ code: ResponseStatusCode;
105
+ }
106
+ declare enum FailedResponseStatusReason {
107
+ GUARDRAILS_VALIDATION_FAILED = "GUARDRAILS_VALIDATION_FAILED",
108
+ MESSAGE_STREAM_INTERRUPTED = "MESSAGE_STREAM_INTERRUPTED",
109
+ UNKNOWN_ERROR = "UNKNOWN_ERROR"
110
+ }
111
+ declare class FailedResponseStatus extends BaseResponseStatus {
112
+ code: ResponseStatusCode;
113
+ reason: FailedResponseStatusReason;
114
+ details?: Record<string, any>;
115
+ }
116
+
117
+ /**
118
+ * The extensions response is used to receive additional information from the copilot runtime, specific to a
119
+ * service adapter or agent framework.
120
+ *
121
+ * Next time a request to the runtime is made, the extensions response will be included in the request as input.
122
+ */
123
+ declare class ExtensionsResponse {
124
+ openaiAssistantAPI?: OpenAIApiAssistantAPIResponse;
125
+ }
126
+ declare class OpenAIApiAssistantAPIResponse {
127
+ runId?: string;
128
+ threadId?: string;
129
+ }
130
+
131
+ declare abstract class BaseMessageOutput {
132
+ id: string;
133
+ createdAt: Date;
134
+ status: typeof MessageStatusUnion;
135
+ }
136
+
137
+ type MessageType = "TextMessage" | "ActionExecutionMessage" | "ResultMessage" | "AgentStateMessage" | "ImageMessage";
138
+ declare class Message {
139
+ type: MessageType;
140
+ id: BaseMessageOutput["id"];
141
+ createdAt: BaseMessageOutput["createdAt"];
142
+ status: MessageStatus;
143
+ constructor(props: any);
144
+ isTextMessage(): this is TextMessage;
145
+ isActionExecutionMessage(): this is ActionExecutionMessage;
146
+ isResultMessage(): this is ResultMessage;
147
+ isAgentStateMessage(): this is AgentStateMessage;
148
+ isImageMessage(): this is ImageMessage;
149
+ }
150
+ type MessageConstructorOptions = Partial<Message>;
151
+ type TextMessageConstructorOptions = MessageConstructorOptions & TextMessageInput;
152
+ declare class TextMessage extends Message implements TextMessageConstructorOptions {
153
+ content: TextMessageInput["content"];
154
+ parentMessageId: TextMessageInput["parentMessageId"];
155
+ role: TextMessageInput["role"];
156
+ type: "TextMessage";
157
+ constructor(props: TextMessageConstructorOptions);
158
+ }
159
+ declare class ActionExecutionMessage extends Message implements Omit<ActionExecutionMessageInput, "arguments" | "scope"> {
160
+ type: MessageType;
161
+ name: string;
162
+ arguments: Record<string, any>;
163
+ parentMessageId?: string;
164
+ }
165
+ declare class ResultMessage extends Message implements ResultMessageInput {
166
+ type: MessageType;
167
+ actionExecutionId: string;
168
+ actionName: string;
169
+ result: string;
170
+ static encodeResult(result: any, error?: {
171
+ code: string;
172
+ message: string;
173
+ } | string | Error): string;
174
+ static decodeResult(result: string): {
175
+ error?: {
176
+ code: string;
177
+ message: string;
178
+ };
179
+ result: string;
180
+ };
181
+ hasError(): boolean;
182
+ getError(): {
183
+ code: string;
184
+ message: string;
185
+ } | undefined;
186
+ }
187
+ declare class AgentStateMessage extends Message implements Omit<AgentStateMessageInput, "state"> {
188
+ type: MessageType;
189
+ threadId: string;
190
+ agentName: string;
191
+ nodeName: string;
192
+ runId: string;
193
+ active: boolean;
194
+ role: MessageRole;
195
+ state: any;
196
+ running: boolean;
197
+ }
198
+ declare class ImageMessage extends Message implements ImageMessageInput {
199
+ type: MessageType;
200
+ format: string;
201
+ bytes: string;
202
+ role: MessageRole;
203
+ parentMessageId?: string;
204
+ }
205
+
206
+ declare enum RuntimeEventTypes {
207
+ TextMessageStart = "TextMessageStart",
208
+ TextMessageContent = "TextMessageContent",
209
+ TextMessageEnd = "TextMessageEnd",
210
+ ActionExecutionStart = "ActionExecutionStart",
211
+ ActionExecutionArgs = "ActionExecutionArgs",
212
+ ActionExecutionEnd = "ActionExecutionEnd",
213
+ ActionExecutionResult = "ActionExecutionResult",
214
+ AgentStateMessage = "AgentStateMessage",
215
+ MetaEvent = "MetaEvent",
216
+ RunError = "RunError"
217
+ }
218
+ declare enum RuntimeMetaEventName {
219
+ LangGraphInterruptEvent = "LangGraphInterruptEvent",
220
+ LangGraphInterruptResumeEvent = "LangGraphInterruptResumeEvent",
221
+ CopilotKitLangGraphInterruptEvent = "CopilotKitLangGraphInterruptEvent"
222
+ }
223
+ type RunTimeMetaEvent = {
224
+ type: RuntimeEventTypes.MetaEvent;
225
+ name: RuntimeMetaEventName.LangGraphInterruptEvent;
226
+ value: string;
227
+ } | {
228
+ type: RuntimeEventTypes.MetaEvent;
229
+ name: RuntimeMetaEventName.CopilotKitLangGraphInterruptEvent;
230
+ data: {
231
+ value: string;
232
+ messages: (TextMessage | ActionExecutionMessage | ResultMessage)[];
233
+ };
234
+ } | {
235
+ type: RuntimeEventTypes.MetaEvent;
236
+ name: RuntimeMetaEventName.LangGraphInterruptResumeEvent;
237
+ data: string;
238
+ };
239
+ type RuntimeErrorEvent = {
240
+ type: RuntimeEventTypes.RunError;
241
+ message: string;
242
+ code?: string;
243
+ };
244
+ type RuntimeEvent = {
245
+ type: RuntimeEventTypes.TextMessageStart;
246
+ messageId: string;
247
+ parentMessageId?: string;
248
+ } | {
249
+ type: RuntimeEventTypes.TextMessageContent;
250
+ messageId: string;
251
+ content: string;
252
+ } | {
253
+ type: RuntimeEventTypes.TextMessageEnd;
254
+ messageId: string;
255
+ } | {
256
+ type: RuntimeEventTypes.ActionExecutionStart;
257
+ actionExecutionId: string;
258
+ actionName: string;
259
+ parentMessageId?: string;
260
+ } | {
261
+ type: RuntimeEventTypes.ActionExecutionArgs;
262
+ actionExecutionId: string;
263
+ args: string;
264
+ } | {
265
+ type: RuntimeEventTypes.ActionExecutionEnd;
266
+ actionExecutionId: string;
267
+ } | {
268
+ type: RuntimeEventTypes.ActionExecutionResult;
269
+ actionName: string;
270
+ actionExecutionId: string;
271
+ result: string;
272
+ } | {
273
+ type: RuntimeEventTypes.AgentStateMessage;
274
+ threadId: string;
275
+ agentName: string;
276
+ nodeName: string;
277
+ runId: string;
278
+ active: boolean;
279
+ role: string;
280
+ state: string;
281
+ running: boolean;
282
+ } | RunTimeMetaEvent | RuntimeErrorEvent;
283
+ type EventSourceCallback = (eventStream$: RuntimeEventSubject) => Promise<void>;
284
+ declare class RuntimeEventSubject extends ReplaySubject<RuntimeEvent> {
285
+ constructor();
286
+ sendTextMessageStart({ messageId, parentMessageId, }: {
287
+ messageId: string;
288
+ parentMessageId?: string;
289
+ }): void;
290
+ sendTextMessageContent({ messageId, content }: {
291
+ messageId: string;
292
+ content: string;
293
+ }): void;
294
+ sendTextMessageEnd({ messageId }: {
295
+ messageId: string;
296
+ }): void;
297
+ sendTextMessage(messageId: string, content: string): void;
298
+ sendActionExecutionStart({ actionExecutionId, actionName, parentMessageId, }: {
299
+ actionExecutionId: string;
300
+ actionName: string;
301
+ parentMessageId?: string;
302
+ }): void;
303
+ sendActionExecutionArgs({ actionExecutionId, args, }: {
304
+ actionExecutionId: string;
305
+ args: string;
306
+ }): void;
307
+ sendActionExecutionEnd({ actionExecutionId }: {
308
+ actionExecutionId: string;
309
+ }): void;
310
+ sendActionExecution({ actionExecutionId, actionName, args, parentMessageId, }: {
311
+ actionExecutionId: string;
312
+ actionName: string;
313
+ args: string;
314
+ parentMessageId?: string;
315
+ }): void;
316
+ sendActionExecutionResult({ actionExecutionId, actionName, result, error, }: {
317
+ actionExecutionId: string;
318
+ actionName: string;
319
+ result?: string;
320
+ error?: {
321
+ code: string;
322
+ message: string;
323
+ };
324
+ }): void;
325
+ sendAgentStateMessage({ threadId, agentName, nodeName, runId, active, role, state, running, }: {
326
+ threadId: string;
327
+ agentName: string;
328
+ nodeName: string;
329
+ runId: string;
330
+ active: boolean;
331
+ role: string;
332
+ state: string;
333
+ running: boolean;
334
+ }): void;
335
+ }
336
+ declare class RuntimeEventSource {
337
+ private eventStream$;
338
+ private callback;
339
+ private errorHandler?;
340
+ private errorContext?;
341
+ constructor(params?: {
342
+ errorHandler?: (error: any, context: any) => Promise<void>;
343
+ errorContext?: any;
344
+ });
345
+ stream(callback: EventSourceCallback): Promise<void>;
346
+ }
347
+
348
+ declare class ActionInput {
349
+ name: string;
350
+ description: string;
351
+ jsonSchema: string;
352
+ available?: ActionInputAvailability;
353
+ }
354
+
355
+ declare class ForwardedParametersInput {
356
+ model?: string;
357
+ maxTokens?: number;
358
+ stop?: string[];
359
+ toolChoice?: String;
360
+ toolChoiceFunctionName?: string;
361
+ temperature?: number;
362
+ }
363
+
364
+ /**
365
+ * The extensions input is used to pass additional information to the copilot runtime, specific to a
366
+ * service adapter or agent framework.
367
+ */
368
+ declare class ExtensionsInput {
369
+ openaiAssistantAPI?: OpenAIApiAssistantAPIInput;
370
+ }
371
+ declare class OpenAIApiAssistantAPIInput {
372
+ runId?: string;
373
+ threadId?: string;
374
+ }
375
+
376
+ declare class AgentSessionInput {
377
+ agentName: string;
378
+ threadId?: string;
379
+ nodeName?: string;
380
+ }
381
+
382
+ declare class AgentStateInput {
383
+ agentName: string;
384
+ state: string;
385
+ config?: string;
386
+ }
387
+
388
+ interface CopilotRuntimeChatCompletionRequest {
389
+ eventSource: RuntimeEventSource;
390
+ messages: Message[];
391
+ actions: ActionInput[];
392
+ model?: string;
393
+ threadId?: string;
394
+ runId?: string;
395
+ forwardedParameters?: ForwardedParametersInput;
396
+ extensions?: ExtensionsInput;
397
+ agentSession?: AgentSessionInput;
398
+ agentStates?: AgentStateInput[];
399
+ }
400
+ interface CopilotRuntimeChatCompletionResponse {
401
+ threadId: string;
402
+ runId?: string;
403
+ extensions?: ExtensionsResponse;
404
+ }
405
+ interface CopilotServiceAdapter {
406
+ provider?: string;
407
+ model?: string;
408
+ process(request: CopilotRuntimeChatCompletionRequest): Promise<CopilotRuntimeChatCompletionResponse>;
409
+ name?: string;
410
+ }
411
+
412
+ /**
413
+ * Copilot Runtime adapter for OpenAI.
414
+ *
415
+ * ## Example
416
+ *
417
+ * ```ts
418
+ * import { CopilotRuntime, OpenAIAdapter } from "@copilotkit/runtime";
419
+ * import OpenAI from "openai";
420
+ *
421
+ * const copilotKit = new CopilotRuntime();
422
+ *
423
+ * const openai = new OpenAI({
424
+ * organization: "<your-organization-id>", // optional
425
+ * apiKey: "<your-api-key>",
426
+ * });
427
+ *
428
+ * return new OpenAIAdapter({ openai });
429
+ * ```
430
+ *
431
+ * ## Example with Azure OpenAI
432
+ *
433
+ * ```ts
434
+ * import { CopilotRuntime, OpenAIAdapter } from "@copilotkit/runtime";
435
+ * import OpenAI from "openai";
436
+ *
437
+ * // The name of your Azure OpenAI Instance.
438
+ * // https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal#create-a-resource
439
+ * const instance = "<your instance name>";
440
+ *
441
+ * // Corresponds to your Model deployment within your OpenAI resource, e.g. my-gpt35-16k-deployment
442
+ * // Navigate to the Azure OpenAI Studio to deploy a model.
443
+ * const model = "<your model>";
444
+ *
445
+ * const apiKey = process.env["AZURE_OPENAI_API_KEY"];
446
+ * if (!apiKey) {
447
+ * throw new Error("The AZURE_OPENAI_API_KEY environment variable is missing or empty.");
448
+ * }
449
+ *
450
+ * const copilotKit = new CopilotRuntime();
451
+ *
452
+ * const openai = new OpenAI({
453
+ * apiKey,
454
+ * baseURL: `https://${instance}.openai.azure.com/openai/deployments/${model}`,
455
+ * defaultQuery: { "api-version": "2024-04-01-preview" },
456
+ * defaultHeaders: { "api-key": apiKey },
457
+ * });
458
+ *
459
+ * return new OpenAIAdapter({ openai });
460
+ * ```
461
+ */
462
+
463
+ interface OpenAIAdapterParams {
464
+ /**
465
+ * An optional OpenAI instance to use. If not provided, a new instance will be
466
+ * created.
467
+ */
468
+ openai?: OpenAI;
469
+ /**
470
+ * The model to use.
471
+ */
472
+ model?: string;
473
+ /**
474
+ * Whether to disable parallel tool calls.
475
+ * You can disable parallel tool calls to force the model to execute tool calls sequentially.
476
+ * This is useful if you want to execute tool calls in a specific order so that the state changes
477
+ * introduced by one tool call are visible to the next tool call. (i.e. new actions or readables)
478
+ *
479
+ * @default false
480
+ */
481
+ disableParallelToolCalls?: boolean;
482
+ /**
483
+ * Whether to keep the role in system messages as "System".
484
+ * By default, it is converted to "developer", which is used by newer OpenAI models
485
+ *
486
+ * @default false
487
+ */
488
+ keepSystemRole?: boolean;
489
+ }
490
+ declare class OpenAIAdapter implements CopilotServiceAdapter {
491
+ model: string;
492
+ provider: string;
493
+ private disableParallelToolCalls;
494
+ private _openai;
495
+ private keepSystemRole;
496
+ get openai(): OpenAI;
497
+ get name(): string;
498
+ constructor(params?: OpenAIAdapterParams);
499
+ private ensureOpenAI;
500
+ process(request: CopilotRuntimeChatCompletionRequest): Promise<CopilotRuntimeChatCompletionResponse>;
501
+ }
502
+
503
+ type LangChainBaseMessageChunkStream = IterableReadableStream<BaseMessageChunk>;
504
+ type LangChainAIMessageChunkStream = IterableReadableStreamInterface<AIMessageChunk>;
505
+ type LangChainReturnType = LangChainBaseMessageChunkStream | LangChainAIMessageChunkStream | BaseMessageChunk | string | AIMessage;
506
+
507
+ /**
508
+ * Copilot Runtime adapter for LangChain.
509
+ *
510
+ * ## Example
511
+ *
512
+ * ```ts
513
+ * import { CopilotRuntime, LangChainAdapter } from "@copilotkit/runtime";
514
+ * import { ChatOpenAI } from "@langchain/openai";
515
+ *
516
+ * const copilotKit = new CopilotRuntime();
517
+ *
518
+ * const model = new ChatOpenAI({
519
+ * model: "gpt-4o",
520
+ * apiKey: "<your-api-key>",
521
+ * });
522
+ *
523
+ * return new LangChainAdapter({
524
+ * chainFn: async ({ messages, tools }) => {
525
+ * return model.bindTools(tools).stream(messages);
526
+ * // or optionally enable strict mode
527
+ * // return model.bindTools(tools, { strict: true }).stream(messages);
528
+ * }
529
+ * });
530
+ * ```
531
+ *
532
+ * The asynchronous handler function (`chainFn`) can return any of the following:
533
+ *
534
+ * - A simple `string` response
535
+ * - A LangChain stream (`IterableReadableStream`)
536
+ * - A LangChain `BaseMessageChunk` object
537
+ * - A LangChain `AIMessage` object
538
+ */
539
+
540
+ interface ChainFnParameters {
541
+ model: string;
542
+ messages: BaseMessage[];
543
+ tools: DynamicStructuredTool[];
544
+ threadId?: string;
545
+ runId?: string;
546
+ }
547
+ interface LangChainAdapterOptions {
548
+ /**
549
+ * A function that uses the LangChain API to generate a response.
550
+ */
551
+ chainFn: (parameters: ChainFnParameters) => Promise<LangChainReturnType>;
552
+ }
553
+ declare class LangChainAdapter implements CopilotServiceAdapter {
554
+ private options;
555
+ /**
556
+ * To use LangChain as a backend, provide a handler function to the adapter with your custom LangChain logic.
557
+ */
558
+ get name(): string;
559
+ constructor(options: LangChainAdapterOptions);
560
+ process(request: CopilotRuntimeChatCompletionRequest): Promise<CopilotRuntimeChatCompletionResponse>;
561
+ }
562
+
563
+ /**
564
+ * Copilot Runtime adapter for Google Generative AI (e.g. Gemini).
565
+ *
566
+ * ## Example
567
+ *
568
+ * ```ts
569
+ * import { CopilotRuntime, GoogleGenerativeAIAdapter } from "@copilotkit/runtime";
570
+ * const { GoogleGenerativeAI } = require("@google/generative-ai");
571
+ *
572
+ * const genAI = new GoogleGenerativeAI(process.env["GOOGLE_API_KEY"]);
573
+ *
574
+ * const copilotKit = new CopilotRuntime();
575
+ *
576
+ * return new GoogleGenerativeAIAdapter({ model: "gemini-2.5-flash", apiVersion: "v1" });
577
+ * ```
578
+ */
579
+
580
+ interface GoogleGenerativeAIAdapterOptions {
581
+ /**
582
+ * A custom Google Generative AI model to use.
583
+ */
584
+ model?: string;
585
+ /**
586
+ * The API version to use (e.g. "v1" or "v1beta"). Defaults to "v1".
587
+ */
588
+ apiVersion?: "v1" | "v1beta";
589
+ /**
590
+ * The API key to use.
591
+ */
592
+ apiKey?: string;
593
+ }
594
+ declare class GoogleGenerativeAIAdapter extends LangChainAdapter {
595
+ provider: string;
596
+ model: string;
597
+ constructor(options?: GoogleGenerativeAIAdapterOptions);
598
+ }
599
+
600
+ /**
601
+ * Copilot Runtime adapter for the OpenAI Assistant API.
602
+ *
603
+ * ## Example
604
+ *
605
+ * ```ts
606
+ * import { CopilotRuntime, OpenAIAssistantAdapter } from "@copilotkit/runtime";
607
+ * import OpenAI from "openai";
608
+ *
609
+ * const copilotKit = new CopilotRuntime();
610
+ *
611
+ * const openai = new OpenAI({
612
+ * organization: "<your-organization-id>",
613
+ * apiKey: "<your-api-key>",
614
+ * });
615
+ *
616
+ * return new OpenAIAssistantAdapter({
617
+ * openai,
618
+ * assistantId: "<your-assistant-id>",
619
+ * codeInterpreterEnabled: true,
620
+ * fileSearchEnabled: true,
621
+ * });
622
+ * ```
623
+ */
624
+
625
+ interface OpenAIAssistantAdapterParams {
626
+ /**
627
+ * The ID of the assistant to use.
628
+ */
629
+ assistantId: string;
630
+ /**
631
+ * An optional OpenAI instance to use. If not provided, a new instance will be created.
632
+ */
633
+ openai?: OpenAI;
634
+ /**
635
+ * Whether to enable code interpretation.
636
+ * @default true
637
+ */
638
+ codeInterpreterEnabled?: boolean;
639
+ /**
640
+ * Whether to enable file search.
641
+ * @default true
642
+ */
643
+ fileSearchEnabled?: boolean;
644
+ /**
645
+ * Whether to disable parallel tool calls.
646
+ * You can disable parallel tool calls to force the model to execute tool calls sequentially.
647
+ * This is useful if you want to execute tool calls in a specific order so that the state changes
648
+ * introduced by one tool call are visible to the next tool call. (i.e. new actions or readables)
649
+ *
650
+ * @default false
651
+ */
652
+ disableParallelToolCalls?: boolean;
653
+ /**
654
+ * Whether to keep the role in system messages as "System".
655
+ * By default, it is converted to "developer", which is used by newer OpenAI models
656
+ *
657
+ * @default false
658
+ */
659
+ keepSystemRole?: boolean;
660
+ }
661
+ declare class OpenAIAssistantAdapter implements CopilotServiceAdapter {
662
+ private _openai;
663
+ private codeInterpreterEnabled;
664
+ private assistantId;
665
+ private fileSearchEnabled;
666
+ private disableParallelToolCalls;
667
+ private keepSystemRole;
668
+ get name(): string;
669
+ constructor(params: OpenAIAssistantAdapterParams);
670
+ private ensureOpenAI;
671
+ process(request: CopilotRuntimeChatCompletionRequest): Promise<CopilotRuntimeChatCompletionResponse>;
672
+ private submitToolOutputs;
673
+ private submitUserMessage;
674
+ private streamResponse;
675
+ }
676
+
677
+ /**
678
+ * CopilotKit Adapter for Unify
679
+ *
680
+ * <RequestExample>
681
+ * ```jsx CopilotRuntime Example
682
+ * const copilotKit = new CopilotRuntime();
683
+ * return copilotKit.response(req, new UnifyAdapter());
684
+ * ```
685
+ * </RequestExample>
686
+ *
687
+ * You can easily set the model to use by passing it to the constructor.
688
+ * ```jsx
689
+ * const copilotKit = new CopilotRuntime();
690
+ * return copilotKit.response(
691
+ * req,
692
+ * new UnifyAdapter({ model: "llama-3-8b-chat@fireworks-ai" }),
693
+ * );
694
+ * ```
695
+ */
696
+
697
+ interface UnifyAdapterParams {
698
+ apiKey?: string;
699
+ model: string;
700
+ }
701
+ declare class UnifyAdapter implements CopilotServiceAdapter {
702
+ private apiKey;
703
+ model: string;
704
+ private start;
705
+ provider: string;
706
+ get name(): string;
707
+ constructor(options?: UnifyAdapterParams);
708
+ process(request: CopilotRuntimeChatCompletionRequest): Promise<CopilotRuntimeChatCompletionResponse>;
709
+ }
710
+
711
+ /**
712
+ * Copilot Runtime adapter for Groq.
713
+ *
714
+ * ## Example
715
+ *
716
+ * ```ts
717
+ * import { CopilotRuntime, GroqAdapter } from "@copilotkit/runtime";
718
+ * import { Groq } from "groq-sdk";
719
+ *
720
+ * const groq = new Groq({ apiKey: process.env["GROQ_API_KEY"] });
721
+ *
722
+ * const copilotKit = new CopilotRuntime();
723
+ *
724
+ * return new GroqAdapter({ groq, model: "<model-name>" });
725
+ * ```
726
+ */
727
+
728
+ interface GroqAdapterParams {
729
+ /**
730
+ * An optional Groq instance to use.
731
+ */
732
+ groq?: Groq;
733
+ /**
734
+ * The model to use.
735
+ */
736
+ model?: string;
737
+ /**
738
+ * Whether to disable parallel tool calls.
739
+ * You can disable parallel tool calls to force the model to execute tool calls sequentially.
740
+ * This is useful if you want to execute tool calls in a specific order so that the state changes
741
+ * introduced by one tool call are visible to the next tool call. (i.e. new actions or readables)
742
+ *
743
+ * @default false
744
+ */
745
+ disableParallelToolCalls?: boolean;
746
+ }
747
+ declare class GroqAdapter implements CopilotServiceAdapter {
748
+ model: string;
749
+ provider: string;
750
+ private disableParallelToolCalls;
751
+ private _groq;
752
+ get groq(): Groq;
753
+ get name(): string;
754
+ constructor(params?: GroqAdapterParams);
755
+ private ensureGroq;
756
+ process(request: CopilotRuntimeChatCompletionRequest): Promise<CopilotRuntimeChatCompletionResponse>;
757
+ }
758
+
759
+ interface RemoteChainParameters {
760
+ name: string;
761
+ description: string;
762
+ chainUrl: string;
763
+ parameters?: Parameter[];
764
+ parameterType?: "single" | "multi";
765
+ }
766
+ declare class RemoteChain {
767
+ name: string;
768
+ description: string;
769
+ chainUrl: string;
770
+ parameters?: Parameter[];
771
+ parameterType: "single" | "multi";
772
+ constructor(options: RemoteChainParameters);
773
+ toAction(): Promise<Action<any>>;
774
+ inferLangServeParameters(): Promise<void>;
775
+ }
776
+
777
+ /**
778
+ * Converts service adapter errors to structured CopilotKitError format using HTTP status codes.
779
+ * This provides consistent error classification across all service adapters.
780
+ */
781
+ declare function convertServiceAdapterError(error: any, adapterName: string): CopilotKitLowLevelError;
782
+
783
+ /**
784
+ * Copilot Runtime adapter for Anthropic.
785
+ *
786
+ * ## Example
787
+ *
788
+ * ```ts
789
+ * import { CopilotRuntime, AnthropicAdapter } from "@copilotkit/runtime";
790
+ * import Anthropic from "@anthropic-ai/sdk";
791
+ *
792
+ * const copilotKit = new CopilotRuntime();
793
+ *
794
+ * const anthropic = new Anthropic({
795
+ * apiKey: "<your-api-key>",
796
+ * });
797
+ *
798
+ * return new AnthropicAdapter({
799
+ * anthropic,
800
+ * promptCaching: {
801
+ * enabled: true,
802
+ * debug: true
803
+ * }
804
+ * });
805
+ * ```
806
+ */
807
+
808
+ interface AnthropicPromptCachingConfig {
809
+ /**
810
+ * Whether to enable prompt caching.
811
+ */
812
+ enabled: boolean;
813
+ /**
814
+ * Whether to enable debug logging for cache operations.
815
+ */
816
+ debug?: boolean;
817
+ }
818
+ interface AnthropicAdapterParams {
819
+ /**
820
+ * An optional Anthropic instance to use. If not provided, a new instance will be
821
+ * created.
822
+ */
823
+ anthropic?: Anthropic;
824
+ /**
825
+ * The model to use.
826
+ */
827
+ model?: string;
828
+ /**
829
+ * Configuration for prompt caching.
830
+ * See: https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching
831
+ */
832
+ promptCaching?: AnthropicPromptCachingConfig;
833
+ }
834
+ declare class AnthropicAdapter implements CopilotServiceAdapter {
835
+ model: string;
836
+ provider: string;
837
+ private promptCaching;
838
+ private _anthropic;
839
+ get anthropic(): Anthropic;
840
+ get name(): string;
841
+ constructor(params?: AnthropicAdapterParams);
842
+ private ensureAnthropic;
843
+ /**
844
+ * Adds cache control to system prompt
845
+ */
846
+ private addSystemPromptCaching;
847
+ /**
848
+ * Adds cache control to the final message
849
+ */
850
+ private addIncrementalMessageCaching;
851
+ private shouldGenerateFallbackResponse;
852
+ process(request: CopilotRuntimeChatCompletionRequest): Promise<CopilotRuntimeChatCompletionResponse>;
853
+ }
854
+
855
+ interface OllamaAdapterOptions {
856
+ model?: string;
857
+ }
858
+ declare class ExperimentalOllamaAdapter implements CopilotServiceAdapter {
859
+ model: string;
860
+ provider: string;
861
+ get name(): string;
862
+ constructor(options?: OllamaAdapterOptions);
863
+ process(request: CopilotRuntimeChatCompletionRequest): Promise<CopilotRuntimeChatCompletionResponse>;
864
+ }
865
+
866
+ /**
867
+ * Copilot Runtime adapter for AWS Bedrock.
868
+ *
869
+ * ## Example
870
+ *
871
+ * ```ts
872
+ * import { CopilotRuntime, BedrockAdapter } from "@copilotkit/runtime";
873
+ *
874
+ * const copilotKit = new CopilotRuntime();
875
+ *
876
+ * return new BedrockAdapter({
877
+ * model: "amazon.nova-lite-v1:0",
878
+ * region: "us-east-1",
879
+ * credentials: {
880
+ * accessKeyId: process.env.AWS_ACCESS_KEY_ID,
881
+ * secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
882
+ * }
883
+ * });
884
+ * ```
885
+ */
886
+
887
+ interface BedrockAdapterParams {
888
+ /**
889
+ * AWS Bedrock model ID to use.
890
+ * @default "amazon.nova-lite-v1:0"
891
+ */
892
+ model?: string;
893
+ /**
894
+ * AWS region where Bedrock is available.
895
+ * @default "us-east-1"
896
+ */
897
+ region?: string;
898
+ /**
899
+ * AWS credentials for Bedrock access.
900
+ */
901
+ credentials?: {
902
+ accessKeyId?: string;
903
+ secretAccessKey?: string;
904
+ };
905
+ }
906
+ declare class BedrockAdapter extends LangChainAdapter {
907
+ provider: string;
908
+ model: string;
909
+ constructor(options?: BedrockAdapterParams);
910
+ }
911
+
912
+ /**
913
+ * CopilotKit Empty Adapter
914
+ *
915
+ * This adapter is meant to preserve adherence to runtime requirements, while doing nothing
916
+ * Ideal if you don't want to connect an LLM the to the runtime, and only use your LangGraph agent.
917
+ * Be aware that Copilot Suggestions will not work if you use this adapter
918
+ *
919
+ * ## Example
920
+ *
921
+ * ```ts
922
+ * import { CopilotRuntime, EmptyAdapter } from "@copilotkit/runtime";
923
+ *
924
+ * const copilotKit = new CopilotRuntime();
925
+ *
926
+ * return new EmptyAdapter();
927
+ * ```
928
+ */
929
+
930
+ declare class EmptyAdapter implements CopilotServiceAdapter {
931
+ process(request: CopilotRuntimeChatCompletionRequest): Promise<CopilotRuntimeChatCompletionResponse>;
932
+ get name(): string;
933
+ }
934
+ declare const ExperimentalEmptyAdapter: typeof EmptyAdapter;
935
+
936
+ interface BaseEndpointDefinition<TActionType extends EndpointType> {
937
+ type?: TActionType;
938
+ }
939
+ interface CopilotKitEndpoint extends BaseEndpointDefinition<EndpointType.CopilotKit> {
940
+ url: string;
941
+ onBeforeRequest?: ({ ctx }: {
942
+ ctx: GraphQLContext;
943
+ }) => {
944
+ headers?: Record<string, string> | undefined;
945
+ };
946
+ }
947
+ interface LangGraphPlatformAgent {
948
+ name: string;
949
+ description: string;
950
+ assistantId?: string;
951
+ }
952
+ interface LangGraphPlatformEndpoint extends BaseEndpointDefinition<EndpointType.LangGraphPlatform> {
953
+ deploymentUrl: string;
954
+ langsmithApiKey?: string | null;
955
+ agents: LangGraphPlatformAgent[];
956
+ }
957
+ type EndpointDefinition = CopilotKitEndpoint | LangGraphPlatformEndpoint;
958
+ declare enum EndpointType {
959
+ CopilotKit = "copilotKit",
960
+ LangGraphPlatform = "langgraph-platform"
961
+ }
962
+
963
+ interface LLMRequestData {
964
+ threadId?: string;
965
+ runId?: string;
966
+ model?: string;
967
+ messages: any[];
968
+ actions?: any[];
969
+ forwardedParameters?: any;
970
+ timestamp: number;
971
+ provider?: string;
972
+ [key: string]: any;
973
+ }
974
+ interface LLMResponseData {
975
+ threadId: string;
976
+ runId?: string;
977
+ model?: string;
978
+ output: any;
979
+ latency: number;
980
+ timestamp: number;
981
+ provider?: string;
982
+ isProgressiveChunk?: boolean;
983
+ isFinalResponse?: boolean;
984
+ [key: string]: any;
985
+ }
986
+ interface LLMErrorData {
987
+ threadId?: string;
988
+ runId?: string;
989
+ model?: string;
990
+ error: Error | string;
991
+ timestamp: number;
992
+ provider?: string;
993
+ [key: string]: any;
994
+ }
995
+ interface CopilotObservabilityHooks {
996
+ handleRequest: (data: LLMRequestData) => void | Promise<void>;
997
+ handleResponse: (data: LLMResponseData) => void | Promise<void>;
998
+ handleError: (data: LLMErrorData) => void | Promise<void>;
999
+ }
1000
+ /**
1001
+ * Configuration for CopilotKit logging functionality.
1002
+ *
1003
+ * @remarks
1004
+ * Custom logging handlers require a valid CopilotKit public API key.
1005
+ * Sign up at https://docs.copilotkit.ai/quickstart#get-a-copilot-cloud-public-api-key to get your key.
1006
+ */
1007
+ interface CopilotObservabilityConfig {
1008
+ /**
1009
+ * Enable or disable logging functionality.
1010
+ *
1011
+ * @default false
1012
+ */
1013
+ enabled: boolean;
1014
+ /**
1015
+ * Controls whether logs are streamed progressively or buffered.
1016
+ * - When true: Each token and update is logged as it's generated (real-time)
1017
+ * - When false: Complete responses are logged after completion (batched)
1018
+ *
1019
+ * @default true
1020
+ */
1021
+ progressive: boolean;
1022
+ /**
1023
+ * Custom observability hooks for request, response, and error events.
1024
+ *
1025
+ * @remarks
1026
+ * Using custom observability hooks requires a valid CopilotKit public API key.
1027
+ */
1028
+ hooks: CopilotObservabilityHooks;
1029
+ }
1030
+
1031
+ /**
1032
+ * Represents a tool provided by an MCP server.
1033
+ */
1034
+ interface MCPTool {
1035
+ description?: string;
1036
+ /** Schema defining parameters, mirroring the MCP structure. */
1037
+ schema?: {
1038
+ parameters?: {
1039
+ properties?: Record<string, any>;
1040
+ required?: string[];
1041
+ jsonSchema?: Record<string, any>;
1042
+ };
1043
+ };
1044
+ /** The function to call to execute the tool on the MCP server. */
1045
+ execute(params: any): Promise<any>;
1046
+ }
1047
+ /**
1048
+ * Defines the contract for *any* MCP client implementation the user might provide.
1049
+ */
1050
+ interface MCPClient {
1051
+ /** A method that returns a map of tool names to MCPTool objects available from the connected MCP server. */
1052
+ tools(): Promise<Record<string, MCPTool>>;
1053
+ /** An optional method for cleanup if the underlying client requires explicit disconnection. */
1054
+ close?(): Promise<void>;
1055
+ }
1056
+ /**
1057
+ * Configuration for connecting to an MCP endpoint.
1058
+ */
1059
+ interface MCPEndpointConfig {
1060
+ endpoint: string;
1061
+ apiKey?: string;
1062
+ }
1063
+ /**
1064
+ * Extracts CopilotKit-compatible parameters from an MCP tool schema.
1065
+ * @param toolOrSchema The schema object from an MCPTool or the full MCPTool object.
1066
+ * @returns An array of Parameter objects.
1067
+ */
1068
+ declare function extractParametersFromSchema(toolOrSchema?: MCPTool | MCPTool["schema"]): Parameter[];
1069
+ /**
1070
+ * Converts a map of MCPTools into an array of CopilotKit Actions.
1071
+ * @param mcpTools A record mapping tool names to MCPTool objects.
1072
+ * @param mcpEndpoint The endpoint URL from which these tools were fetched.
1073
+ * @returns An array of Action<any> objects.
1074
+ */
1075
+ declare function convertMCPToolsToActions(mcpTools: Record<string, MCPTool>, mcpEndpoint: string): Action<any>[];
1076
+ /**
1077
+ * Generate better instructions for using MCP tools
1078
+ * This is used to enhance the system prompt with tool documentation
1079
+ */
1080
+ declare function generateMcpToolInstructions(toolsMap: Record<string, MCPTool>): string;
1081
+
1082
+ /**
1083
+ * <Callout type="info">
1084
+ * This is the reference for the `CopilotRuntime` class. For more information and example code snippets, please see [Concept: Copilot Runtime](/concepts/copilot-runtime).
1085
+ * </Callout>
1086
+ *
1087
+ * ## Usage
1088
+ *
1089
+ * ```tsx
1090
+ * import { CopilotRuntime } from "@copilotkit/runtime";
1091
+ *
1092
+ * const copilotKit = new CopilotRuntime();
1093
+ * ```
1094
+ */
1095
+
1096
+ type CreateMCPClientFunction = (config: MCPEndpointConfig) => Promise<MCPClient>;
1097
+ type ActionsConfiguration<T extends Parameter[] | [] = []> = Action<T>[] | ((ctx: {
1098
+ properties: any;
1099
+ url?: string;
1100
+ }) => Action<T>[]);
1101
+ interface OnBeforeRequestOptions {
1102
+ threadId?: string;
1103
+ runId?: string;
1104
+ inputMessages: Message[];
1105
+ properties: any;
1106
+ url?: string;
1107
+ }
1108
+ type OnBeforeRequestHandler = (options: OnBeforeRequestOptions) => void | Promise<void>;
1109
+ interface OnAfterRequestOptions {
1110
+ threadId: string;
1111
+ runId?: string;
1112
+ inputMessages: Message[];
1113
+ outputMessages: Message[];
1114
+ properties: any;
1115
+ url?: string;
1116
+ }
1117
+ type OnAfterRequestHandler = (options: OnAfterRequestOptions) => void | Promise<void>;
1118
+ interface OnStopGenerationOptions {
1119
+ threadId: string;
1120
+ runId?: string;
1121
+ url?: string;
1122
+ agentName?: string;
1123
+ lastMessage: MessageInput;
1124
+ }
1125
+ type OnStopGenerationHandler = (options: OnStopGenerationOptions) => void | Promise<void>;
1126
+ interface Middleware {
1127
+ /**
1128
+ * A function that is called before the request is processed.
1129
+ */
1130
+ /**
1131
+ * @deprecated This middleware hook is deprecated and will be removed in a future version.
1132
+ * Use updated middleware integration methods in CopilotRuntimeVNext instead.
1133
+ */
1134
+ onBeforeRequest?: OnBeforeRequestHandler;
1135
+ /**
1136
+ * A function that is called after the request is processed.
1137
+ */
1138
+ /**
1139
+ * @deprecated This middleware hook is deprecated and will be removed in a future version.
1140
+ * Use updated middleware integration methods in CopilotRuntimeVNext instead.
1141
+ */
1142
+ onAfterRequest?: OnAfterRequestHandler;
1143
+ }
1144
+ interface CopilotRuntimeConstructorParams_BASE<T extends Parameter[] | [] = []> {
1145
+ /**
1146
+ * Middleware to be used by the runtime.
1147
+ *
1148
+ * ```ts
1149
+ * onBeforeRequest: (options: {
1150
+ * threadId?: string;
1151
+ * runId?: string;
1152
+ * inputMessages: Message[];
1153
+ * properties: any;
1154
+ * }) => void | Promise<void>;
1155
+ * ```
1156
+ *
1157
+ * ```ts
1158
+ * onAfterRequest: (options: {
1159
+ * threadId?: string;
1160
+ * runId?: string;
1161
+ * inputMessages: Message[];
1162
+ * outputMessages: Message[];
1163
+ * properties: any;
1164
+ * }) => void | Promise<void>;
1165
+ * ```
1166
+ */
1167
+ /**
1168
+ * @deprecated This middleware hook is deprecated and will be removed in a future version.
1169
+ * Use updated middleware integration methods in CopilotRuntimeVNext instead.
1170
+ */
1171
+ middleware?: Middleware;
1172
+ actions?: ActionsConfiguration<T>;
1173
+ remoteActions?: CopilotKitEndpoint[];
1174
+ remoteEndpoints?: EndpointDefinition[];
1175
+ langserve?: RemoteChainParameters[];
1176
+ agents?: Record<string, AbstractAgent>;
1177
+ delegateAgentProcessingToServiceAdapter?: boolean;
1178
+ /**
1179
+ * Configuration for LLM request/response logging.
1180
+ * Requires publicApiKey from CopilotKit component to be set:
1181
+ *
1182
+ * ```tsx
1183
+ * <CopilotKit publicApiKey="ck_pub_..." />
1184
+ * ```
1185
+ *
1186
+ * Example logging config:
1187
+ * ```ts
1188
+ * logging: {
1189
+ * enabled: true, // Enable or disable logging
1190
+ * progressive: true, // Set to false for buffered logging
1191
+ * logger: {
1192
+ * logRequest: (data) => langfuse.trace({ name: "LLM Request", input: data }),
1193
+ * logResponse: (data) => langfuse.trace({ name: "LLM Response", output: data }),
1194
+ * logError: (errorData) => langfuse.trace({ name: "LLM Error", metadata: errorData }),
1195
+ * },
1196
+ * }
1197
+ * ```
1198
+ */
1199
+ observability_c?: CopilotObservabilityConfig;
1200
+ /**
1201
+ * Configuration for connecting to Model Context Protocol (MCP) servers.
1202
+ * Allows fetching and using tools defined on external MCP-compliant servers.
1203
+ * Requires providing the `createMCPClient` function during instantiation.
1204
+ * @experimental
1205
+ */
1206
+ mcpServers?: MCPEndpointConfig[];
1207
+ /**
1208
+ * A function that creates an MCP client instance for a given endpoint configuration.
1209
+ * This function is responsible for using the appropriate MCP client library
1210
+ * (e.g., `@copilotkit/runtime`, `ai`) to establish a connection.
1211
+ * Required if `mcpServers` is provided.
1212
+ *
1213
+ * ```typescript
1214
+ * import { experimental_createMCPClient } from "ai"; // Import from vercel ai library
1215
+ * // ...
1216
+ * const runtime = new CopilotRuntime({
1217
+ * mcpServers: [{ endpoint: "..." }],
1218
+ * async createMCPClient(config) {
1219
+ * return await experimental_createMCPClient({
1220
+ * transport: {
1221
+ * type: "sse",
1222
+ * url: config.endpoint,
1223
+ * headers: config.apiKey
1224
+ * ? { Authorization: `Bearer ${config.apiKey}` }
1225
+ * : undefined,
1226
+ * },
1227
+ * });
1228
+ * }
1229
+ * });
1230
+ * ```
1231
+ */
1232
+ createMCPClient?: CreateMCPClientFunction;
1233
+ /**
1234
+ * Optional error handler for comprehensive debugging and observability.
1235
+ *
1236
+ * **Requires publicApiKey**: Error handling only works when requests include a valid publicApiKey.
1237
+ * This is a premium Copilot Cloud feature.
1238
+ *
1239
+ * @param errorEvent - Structured error event with rich debugging context
1240
+ *
1241
+ * @example
1242
+ * ```typescript
1243
+ * const runtime = new CopilotRuntime({
1244
+ * onError: (errorEvent) => {
1245
+ * debugDashboard.capture(errorEvent);
1246
+ * }
1247
+ * });
1248
+ * ```
1249
+ */
1250
+ onError?: CopilotErrorHandler;
1251
+ onStopGeneration?: OnStopGenerationHandler;
1252
+ }
1253
+ interface CopilotRuntimeConstructorParams<T extends Parameter[] | [] = []> extends Omit<CopilotRuntimeConstructorParams_BASE<T>, "agents">, Omit<CopilotRuntimeOptions, "agents" | "transcriptionService"> {
1254
+ /**
1255
+ * TODO: un-omit `transcriptionService` above once it's supported
1256
+ *
1257
+ * This satisfies...
1258
+ * – the optional constraint in `CopilotRuntimeConstructorParams_BASE`
1259
+ * – the `MaybePromise<NonEmptyRecord<T>>` constraint in `CopilotRuntimeOptionsVNext`
1260
+ * – the `Record<string, AbstractAgent>` constraint in `both
1261
+ */
1262
+ agents?: MaybePromise<NonEmptyRecord<Record<string, AbstractAgent>>>;
1263
+ }
1264
+ /**
1265
+ * Central runtime object passed to all request handlers.
1266
+ */
1267
+ declare class CopilotRuntime<const T extends Parameter[] | [] = []> {
1268
+ params?: CopilotRuntimeConstructorParams<T>;
1269
+ private observability?;
1270
+ private mcpToolsCache;
1271
+ private runtimeArgs;
1272
+ private _instance;
1273
+ constructor(params?: CopilotRuntimeConstructorParams<T> & PartialBy<CopilotRuntimeOptions, "agents">);
1274
+ get instance(): CopilotRuntime$1;
1275
+ private assignEndpointsToAgents;
1276
+ handleServiceAdapter(serviceAdapter: CopilotServiceAdapter): void;
1277
+ private getToolsFromActions;
1278
+ private assignToolsToAgents;
1279
+ private createOnBeforeRequestHandler;
1280
+ private createOnAfterRequestHandler;
1281
+ /**
1282
+ * Log LLM request if observability is enabled
1283
+ */
1284
+ private logObservabilityBeforeRequest;
1285
+ /**
1286
+ * Log final LLM response after request completes
1287
+ */
1288
+ private logObservabilityAfterRequest;
1289
+ private getToolsFromMCP;
1290
+ }
1291
+ declare function copilotKitEndpoint(config: Omit<CopilotKitEndpoint, "type">): CopilotKitEndpoint;
1292
+ declare function langGraphPlatformEndpoint(config: Omit<LangGraphPlatformEndpoint, "type">): LangGraphPlatformEndpoint;
1293
+ declare function resolveEndpointType(endpoint: EndpointDefinition): EndpointType;
1294
+
1295
+ interface CopilotCloudOptions {
1296
+ baseUrl?: string;
1297
+ publicApiKey?: string;
1298
+ }
1299
+
1300
+ type LogLevel = "debug" | "info" | "warn" | "error";
1301
+ type CopilotRuntimeLogger = ReturnType<typeof createLogger>;
1302
+ declare function createLogger(options?: {
1303
+ level?: LogLevel;
1304
+ component?: string;
1305
+ }): createPinoLogger__default.Logger<never, boolean>;
1306
+
1307
+ declare const logger: createPinoLogger.Logger<never, boolean>;
1308
+ declare const addCustomHeaderPlugin: {
1309
+ onResponse({ response }: {
1310
+ response: any;
1311
+ }): void;
1312
+ };
1313
+ type AnyPrimitive = string | boolean | number | null;
1314
+ type CopilotRequestContextProperties = Record<string, AnyPrimitive | Record<string, AnyPrimitive>>;
1315
+ type GraphQLContext = YogaInitialContext & {
1316
+ _copilotkit: CreateCopilotRuntimeServerOptions;
1317
+ properties: CopilotRequestContextProperties;
1318
+ logger: typeof logger;
1319
+ };
1320
+ interface CreateCopilotRuntimeServerOptions {
1321
+ runtime: CopilotRuntime<any>;
1322
+ serviceAdapter?: CopilotServiceAdapter;
1323
+ endpoint: string;
1324
+ baseUrl?: string;
1325
+ cloud?: CopilotCloudOptions;
1326
+ properties?: CopilotRequestContextProperties;
1327
+ logLevel?: LogLevel;
1328
+ }
1329
+ declare function createContext(initialContext: YogaInitialContext, copilotKitContext: CreateCopilotRuntimeServerOptions, contextLogger: typeof logger, properties?: CopilotRequestContextProperties): Promise<Partial<GraphQLContext>>;
1330
+ declare function buildSchema(options?: {
1331
+ emitSchemaFile?: string;
1332
+ }): graphql.GraphQLSchema;
1333
+ type CommonConfig = {
1334
+ logging: typeof logger;
1335
+ schema: ReturnType<typeof buildSchema>;
1336
+ plugins: Parameters<typeof createYoga>[0]["plugins"];
1337
+ context: (ctx: YogaInitialContext) => Promise<Partial<GraphQLContext>>;
1338
+ maskedErrors: {
1339
+ maskError: (error: any, message: string, isDev?: boolean) => any;
1340
+ };
1341
+ };
1342
+ declare function getCommonConfig(options: CreateCopilotRuntimeServerOptions): CommonConfig;
1343
+
1344
+ declare function copilotRuntimeNextJSAppRouterEndpoint(options: CreateCopilotRuntimeServerOptions): {
1345
+ handleRequest: (req: Request) => Response | Promise<Response>;
1346
+ };
1347
+
1348
+ declare const config: {
1349
+ api: {
1350
+ bodyParser: boolean;
1351
+ };
1352
+ };
1353
+
1354
+ declare function copilotRuntimeNextJSPagesRouterEndpoint(options: CreateCopilotRuntimeServerOptions): (reqOrRequest: http.IncomingMessage | Request, res?: http.ServerResponse) => Promise<void> | Promise<Response> | Response;
1355
+
1356
+ declare function copilotRuntimeNodeHttpEndpoint(options: CreateCopilotRuntimeServerOptions): (reqOrRequest: IncomingMessage | Request, res?: ServerResponse) => Promise<void> | Promise<Response> | Response;
1357
+
1358
+ declare function copilotRuntimeNodeExpressEndpoint(options: CreateCopilotRuntimeServerOptions): (reqOrRequest: http.IncomingMessage | Request, res?: http.ServerResponse) => Promise<void> | Promise<Response> | Response;
1359
+
1360
+ declare function copilotRuntimeNestEndpoint(options: CreateCopilotRuntimeServerOptions): (reqOrRequest: http.IncomingMessage | Request, res?: http.ServerResponse) => Promise<void> | Promise<Response> | Response;
1361
+
1362
+ /**
1363
+ * TelemetryAgentRunner - A wrapper around AgentRunner that adds telemetry
1364
+ * for agent execution streams.
1365
+ *
1366
+ * This captures the following telemetry events:
1367
+ * - oss.runtime.agent_execution_stream_started - when an agent execution starts
1368
+ * - oss.runtime.agent_execution_stream_ended - when an agent execution completes
1369
+ * - oss.runtime.agent_execution_stream_errored - when an agent execution fails
1370
+ */
1371
+
1372
+ /**
1373
+ * Configuration options for TelemetryAgentRunner
1374
+ */
1375
+ interface TelemetryAgentRunnerConfig {
1376
+ /**
1377
+ * The underlying runner to delegate to
1378
+ * If not provided, defaults to InMemoryAgentRunner
1379
+ */
1380
+ runner?: AgentRunner;
1381
+ /**
1382
+ * Optional LangSmith API key (will be hashed for telemetry)
1383
+ */
1384
+ langsmithApiKey?: string;
1385
+ }
1386
+ /**
1387
+ * An AgentRunner wrapper that adds telemetry tracking for agent executions.
1388
+ *
1389
+ * Usage:
1390
+ * ```ts
1391
+ * const runtime = new CopilotRuntime({
1392
+ * runner: new TelemetryAgentRunner(),
1393
+ * // or with custom runner:
1394
+ * runner: new TelemetryAgentRunner({ runner: customRunner }),
1395
+ * });
1396
+ * ```
1397
+ */
1398
+ declare class TelemetryAgentRunner implements AgentRunner {
1399
+ private readonly _runner;
1400
+ private readonly hashedLgcKey;
1401
+ constructor(config?: TelemetryAgentRunnerConfig);
1402
+ /**
1403
+ * Runs an agent with telemetry tracking.
1404
+ * Wraps the underlying runner's Observable stream with telemetry events.
1405
+ */
1406
+ run(...args: Parameters<AgentRunner["run"]>): ReturnType<AgentRunner["run"]>;
1407
+ /**
1408
+ * Delegates to the underlying runner's connect method
1409
+ */
1410
+ connect(...args: Parameters<AgentRunner["connect"]>): ReturnType<AgentRunner["connect"]>;
1411
+ /**
1412
+ * Delegates to the underlying runner's isRunning method
1413
+ */
1414
+ isRunning(...args: Parameters<AgentRunner["isRunning"]>): ReturnType<AgentRunner["isRunning"]>;
1415
+ /**
1416
+ * Delegates to the underlying runner's stop method
1417
+ */
1418
+ stop(...args: Parameters<AgentRunner["stop"]>): ReturnType<AgentRunner["stop"]>;
1419
+ }
1420
+
1421
+ /**
1422
+ * @deprecated LangGraphAgent import from `@copilotkit/runtime` is deprecated. Please import it from `@copilotkit/runtime/langgraph` instead
1423
+ */
1424
+ declare class LangGraphAgent {
1425
+ constructor();
1426
+ }
1427
+ /**
1428
+ * @deprecated LangGraphHttpAgent import from `@copilotkit/runtime` is deprecated. Please import it from `@copilotkit/runtime/langgraph` instead
1429
+ */
1430
+ declare class LangGraphHttpAgent {
1431
+ constructor();
1432
+ }
1433
+ /**
1434
+ * @deprecated TextMessageEvents import from `@copilotkit/runtime` is deprecated. Please import it from `@copilotkit/runtime/langgraph` instead
1435
+ */
1436
+ type TextMessageEvents = any;
1437
+ /**
1438
+ * @deprecated ToolCallEvents import from `@copilotkit/runtime` is deprecated. Please import it from `@copilotkit/runtime/langgraph` instead
1439
+ */
1440
+ type ToolCallEvents = any;
1441
+ /**
1442
+ * @deprecated CustomEventNames import from `@copilotkit/runtime` is deprecated. Please import it from `@copilotkit/runtime/langgraph` instead
1443
+ */
1444
+ type CustomEventNames = any;
1445
+ /**
1446
+ * @deprecated PredictStateTool import from `@copilotkit/runtime` is deprecated. Please import it from `@copilotkit/runtime/langgraph` instead
1447
+ */
1448
+ type PredictStateTool = any;
1449
+
1450
+ declare class GuardrailsValidationFailureResponse extends FailedResponseStatus {
1451
+ reason: FailedResponseStatusReason;
1452
+ details: {
1453
+ guardrailsReason: string;
1454
+ };
1455
+ constructor({ guardrailsReason }: {
1456
+ guardrailsReason: any;
1457
+ });
1458
+ }
1459
+ declare class MessageStreamInterruptedResponse extends FailedResponseStatus {
1460
+ reason: FailedResponseStatusReason;
1461
+ details: {
1462
+ messageId: string;
1463
+ description: string;
1464
+ };
1465
+ constructor({ messageId }: {
1466
+ messageId: string;
1467
+ });
1468
+ }
1469
+ declare class UnknownErrorResponse extends FailedResponseStatus {
1470
+ reason: FailedResponseStatusReason;
1471
+ details: {
1472
+ description?: string;
1473
+ originalError?: {
1474
+ code?: string;
1475
+ statusCode?: number;
1476
+ severity?: string;
1477
+ visibility?: string;
1478
+ originalErrorType?: string;
1479
+ extensions?: any;
1480
+ };
1481
+ };
1482
+ constructor({ description, originalError, }: {
1483
+ description?: string;
1484
+ originalError?: {
1485
+ code?: string;
1486
+ statusCode?: number;
1487
+ severity?: string;
1488
+ visibility?: string;
1489
+ originalErrorType?: string;
1490
+ extensions?: any;
1491
+ };
1492
+ });
1493
+ }
1494
+
1495
+ export { AnthropicAdapter, AnthropicAdapterParams, AnthropicPromptCachingConfig, BedrockAdapter, BedrockAdapterParams, CommonConfig, CopilotRequestContextProperties, CopilotRuntime, CopilotRuntimeChatCompletionRequest, CopilotRuntimeChatCompletionResponse, CopilotRuntimeConstructorParams_BASE, CopilotRuntimeLogger, CopilotServiceAdapter, CreateCopilotRuntimeServerOptions, CustomEventNames, EmptyAdapter, ExperimentalEmptyAdapter, ExperimentalOllamaAdapter, GoogleGenerativeAIAdapter, GraphQLContext, GroqAdapter, GroqAdapterParams, GuardrailsValidationFailureResponse, LangChainAdapter, LangGraphAgent, LangGraphHttpAgent, LogLevel, MCPClient, MCPEndpointConfig, MCPTool, MessageStreamInterruptedResponse, OpenAIAdapter, OpenAIAdapterParams, OpenAIAssistantAdapter, OpenAIAssistantAdapterParams, PredictStateTool, RemoteChain, RemoteChainParameters, TelemetryAgentRunner, TelemetryAgentRunnerConfig, TextMessageEvents, ToolCallEvents, UnifyAdapter, UnifyAdapterParams, UnknownErrorResponse, addCustomHeaderPlugin, buildSchema, config, convertMCPToolsToActions, convertServiceAdapterError, copilotKitEndpoint, copilotRuntimeNestEndpoint, copilotRuntimeNextJSAppRouterEndpoint, copilotRuntimeNextJSPagesRouterEndpoint, copilotRuntimeNodeExpressEndpoint, copilotRuntimeNodeHttpEndpoint, createContext, createLogger, extractParametersFromSchema, generateMcpToolInstructions, getCommonConfig, langGraphPlatformEndpoint, resolveEndpointType };