@copilotkit/runtime 1.2.2-feat-runtime-remote-actions.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/CHANGELOG.md +6 -13
  2. package/dist/{chunk-BPEPG56J.mjs → chunk-47TPNJX7.mjs} +2 -2
  3. package/dist/{chunk-Y5TWOZFD.mjs → chunk-4BZ6WXBB.mjs} +3 -3
  4. package/dist/{chunk-3SKYFYY2.mjs → chunk-IRRAL44O.mjs} +4 -5
  5. package/dist/chunk-IRRAL44O.mjs.map +1 -0
  6. package/dist/{chunk-BJ2LVHWA.mjs → chunk-OF6AN6HF.mjs} +521 -256
  7. package/dist/chunk-OF6AN6HF.mjs.map +1 -0
  8. package/dist/{chunk-UL2OKN2O.mjs → chunk-VWS65V7Y.mjs} +2 -2
  9. package/dist/{chunk-U2EKJP47.mjs → chunk-XCGRXAJU.mjs} +2 -2
  10. package/dist/{copilot-runtime-d427e991.d.ts → copilot-runtime-a1b5f1ce.d.ts} +1 -1
  11. package/dist/{index-079752b9.d.ts → groq-adapter-069ac812.d.ts} +82 -82
  12. package/dist/index.d.ts +7 -5
  13. package/dist/index.js +721 -454
  14. package/dist/index.js.map +1 -1
  15. package/dist/index.mjs +10 -6
  16. package/dist/index.mjs.map +1 -1
  17. package/dist/{langserve-d6073a3b.d.ts → langserve-15a1286b.d.ts} +1 -1
  18. package/dist/lib/index.d.ts +4 -4
  19. package/dist/lib/index.js +445 -446
  20. package/dist/lib/index.js.map +1 -1
  21. package/dist/lib/index.mjs +6 -6
  22. package/dist/lib/integrations/index.d.ts +3 -3
  23. package/dist/lib/integrations/index.js +3 -4
  24. package/dist/lib/integrations/index.js.map +1 -1
  25. package/dist/lib/integrations/index.mjs +4 -4
  26. package/dist/lib/integrations/nest/index.d.ts +2 -2
  27. package/dist/lib/integrations/nest/index.js +3 -4
  28. package/dist/lib/integrations/nest/index.js.map +1 -1
  29. package/dist/lib/integrations/nest/index.mjs +2 -2
  30. package/dist/lib/integrations/node-express/index.d.ts +2 -2
  31. package/dist/lib/integrations/node-express/index.js +3 -4
  32. package/dist/lib/integrations/node-express/index.js.map +1 -1
  33. package/dist/lib/integrations/node-express/index.mjs +2 -2
  34. package/dist/lib/integrations/node-http/index.d.ts +2 -2
  35. package/dist/lib/integrations/node-http/index.js +3 -4
  36. package/dist/lib/integrations/node-http/index.js.map +1 -1
  37. package/dist/lib/integrations/node-http/index.mjs +1 -1
  38. package/dist/service-adapters/index.d.ts +47 -3
  39. package/dist/service-adapters/index.js +708 -442
  40. package/dist/service-adapters/index.js.map +1 -1
  41. package/dist/service-adapters/index.mjs +3 -1
  42. package/package.json +6 -7
  43. package/src/index.ts +1 -0
  44. package/src/service-adapters/anthropic/anthropic-adapter.ts +197 -0
  45. package/src/service-adapters/anthropic/utils.ts +144 -0
  46. package/src/service-adapters/index.ts +9 -7
  47. package/dist/chunk-3SKYFYY2.mjs.map +0 -1
  48. package/dist/chunk-BJ2LVHWA.mjs.map +0 -1
  49. /package/dist/{chunk-BPEPG56J.mjs.map → chunk-47TPNJX7.mjs.map} +0 -0
  50. /package/dist/{chunk-Y5TWOZFD.mjs.map → chunk-4BZ6WXBB.mjs.map} +0 -0
  51. /package/dist/{chunk-UL2OKN2O.mjs.map → chunk-VWS65V7Y.mjs.map} +0 -0
  52. /package/dist/{chunk-U2EKJP47.mjs.map → chunk-XCGRXAJU.mjs.map} +0 -0
package/dist/index.js CHANGED
@@ -44,7 +44,7 @@ var require_package = __commonJS({
44
44
  publishConfig: {
45
45
  access: "public"
46
46
  },
47
- version: "1.2.2-feat-runtime-remote-actions.1",
47
+ version: "1.3.0",
48
48
  sideEffects: false,
49
49
  main: "./dist/index.js",
50
50
  module: "./dist/index.mjs",
@@ -59,9 +59,7 @@ var require_package = __commonJS({
59
59
  test: "jest --passWithNoTests",
60
60
  "check-types": "tsc --noEmit",
61
61
  clean: "rm -rf .turbo && rm -rf node_modules && rm -rf dist && rm -rf .next && rm -rf __snapshots__",
62
- "generate-graphql-schema": "rm -rf __snapshots__ && ts-node ./scripts/generate-gql-schema.ts",
63
- "link:global": "pnpm link --global",
64
- "unlink:global": "pnpm unlink --global"
62
+ "generate-graphql-schema": "rm -rf __snapshots__ && ts-node ./scripts/generate-gql-schema.ts"
65
63
  },
66
64
  devDependencies: {
67
65
  "@swc/core": "1.5.28",
@@ -80,6 +78,7 @@ var require_package = __commonJS({
80
78
  typescript: "^5.2.3"
81
79
  },
82
80
  dependencies: {
81
+ "@anthropic-ai/sdk": "^0.27.3",
83
82
  "@copilotkit/shared": "workspace:*",
84
83
  "@google/generative-ai": "^0.11.2",
85
84
  "@graphql-yoga/plugin-defer-stream": "^3.3.1",
@@ -120,6 +119,7 @@ var require_package = __commonJS({
120
119
  // src/index.ts
121
120
  var src_exports = {};
122
121
  __export(src_exports, {
122
+ AnthropicAdapter: () => AnthropicAdapter,
123
123
  CopilotRuntime: () => CopilotRuntime,
124
124
  GoogleGenerativeAIAdapter: () => GoogleGenerativeAIAdapter,
125
125
  GroqAdapter: () => GroqAdapter,
@@ -128,6 +128,7 @@ __export(src_exports, {
128
128
  MessageStreamInterruptedResponse: () => MessageStreamInterruptedResponse,
129
129
  OpenAIAdapter: () => OpenAIAdapter,
130
130
  OpenAIAssistantAdapter: () => OpenAIAssistantAdapter,
131
+ RemoteChain: () => RemoteChain,
131
132
  UnifyAdapter: () => UnifyAdapter,
132
133
  UnknownErrorResponse: () => UnknownErrorResponse,
133
134
  buildSchema: () => buildSchema,
@@ -145,7 +146,85 @@ module.exports = __toCommonJS(src_exports);
145
146
  var import_reflect_metadata = require("reflect-metadata");
146
147
 
147
148
  // src/lib/runtime/copilot-runtime.ts
148
- var import_shared8 = require("@copilotkit/shared");
149
+ var import_shared9 = require("@copilotkit/shared");
150
+
151
+ // src/service-adapters/langchain/langserve.ts
152
+ var import_remote = require("langchain/runnables/remote");
153
+ var RemoteChain = class {
154
+ name;
155
+ description;
156
+ chainUrl;
157
+ parameters;
158
+ parameterType;
159
+ constructor(options) {
160
+ this.name = options.name;
161
+ this.description = options.description;
162
+ this.chainUrl = options.chainUrl;
163
+ this.parameters = options.parameters;
164
+ this.parameterType = options.parameterType || "multi";
165
+ }
166
+ async toAction() {
167
+ if (!this.parameters) {
168
+ await this.inferLangServeParameters();
169
+ }
170
+ return {
171
+ name: this.name,
172
+ description: this.description,
173
+ parameters: this.parameters,
174
+ handler: async (args) => {
175
+ const runnable = new import_remote.RemoteRunnable({
176
+ url: this.chainUrl
177
+ });
178
+ let input;
179
+ if (this.parameterType === "single") {
180
+ input = args[Object.keys(args)[0]];
181
+ } else {
182
+ input = args;
183
+ }
184
+ return await runnable.invoke(input);
185
+ }
186
+ };
187
+ }
188
+ async inferLangServeParameters() {
189
+ const supportedTypes = [
190
+ "string",
191
+ "number",
192
+ "boolean"
193
+ ];
194
+ let schemaUrl = this.chainUrl.replace(/\/+$/, "") + "/input_schema";
195
+ let schema = await fetch(schemaUrl).then((res) => res.json()).catch(() => {
196
+ throw new Error("Failed to fetch langserve schema at " + schemaUrl);
197
+ });
198
+ if (supportedTypes.includes(schema.type)) {
199
+ this.parameterType = "single";
200
+ this.parameters = [
201
+ {
202
+ name: "input",
203
+ type: schema.type,
204
+ description: "The input to the chain"
205
+ }
206
+ ];
207
+ } else if (schema.type === "object") {
208
+ this.parameterType = "multi";
209
+ this.parameters = Object.keys(schema.properties).map((key) => {
210
+ var _a;
211
+ let property = schema.properties[key];
212
+ if (!supportedTypes.includes(property.type)) {
213
+ throw new Error("Unsupported schema type");
214
+ }
215
+ return {
216
+ name: key,
217
+ type: property.type,
218
+ description: property.description || "",
219
+ required: ((_a = schema.required) == null ? void 0 : _a.includes(key)) || false
220
+ };
221
+ });
222
+ } else {
223
+ throw new Error("Unsupported schema type");
224
+ }
225
+ }
226
+ };
227
+ __name(RemoteChain, "RemoteChain");
149
228
 
150
229
  // src/service-adapters/openai/openai-adapter.ts
151
230
  var import_openai = __toESM(require("openai"));
@@ -447,167 +526,229 @@ var OpenAIAdapter = class {
447
526
  };
448
527
  __name(OpenAIAdapter, "OpenAIAdapter");
449
528
 
450
- // src/service-adapters/openai/openai-assistant-adapter.ts
451
- var import_openai2 = __toESM(require("openai"));
452
- var OpenAIAssistantAdapter = class {
453
- openai;
454
- codeInterpreterEnabled;
455
- assistantId;
456
- fileSearchEnabled;
457
- disableParallelToolCalls;
458
- constructor(params) {
459
- this.openai = params.openai || new import_openai2.default({});
460
- this.codeInterpreterEnabled = params.codeInterpreterEnabled === false || true;
461
- this.fileSearchEnabled = params.fileSearchEnabled === false || true;
462
- this.assistantId = params.assistantId;
463
- this.disableParallelToolCalls = (params == null ? void 0 : params.disableParallelToolCalls) || false;
529
+ // src/service-adapters/langchain/utils.ts
530
+ var import_messages = require("@langchain/core/messages");
531
+ var import_tools = require("@langchain/core/tools");
532
+ var import_zod = require("zod");
533
+ var import_shared2 = require("@copilotkit/shared");
534
+ function convertMessageToLangChainMessage(message) {
535
+ if (message instanceof TextMessage) {
536
+ if (message.role == "user") {
537
+ return new import_messages.HumanMessage(message.content);
538
+ } else if (message.role == "assistant") {
539
+ return new import_messages.AIMessage(message.content);
540
+ } else if (message.role === "system") {
541
+ return new import_messages.SystemMessage(message.content);
542
+ }
543
+ } else if (message instanceof ActionExecutionMessage) {
544
+ return new import_messages.AIMessage({
545
+ content: "",
546
+ tool_calls: [
547
+ {
548
+ id: message.id,
549
+ args: message.arguments,
550
+ name: message.name
551
+ }
552
+ ]
553
+ });
554
+ } else if (message instanceof ResultMessage) {
555
+ return new import_messages.ToolMessage({
556
+ content: message.result,
557
+ tool_call_id: message.actionExecutionId
558
+ });
464
559
  }
465
- async process(request) {
466
- const { messages, actions, eventSource, runId, forwardedParameters } = request;
467
- let threadId = request.threadId || (await this.openai.beta.threads.create()).id;
468
- const lastMessage = messages.at(-1);
469
- let nextRunId = void 0;
470
- if (lastMessage instanceof ResultMessage && runId) {
471
- nextRunId = await this.submitToolOutputs(threadId, runId, messages, eventSource);
472
- } else if (lastMessage instanceof TextMessage) {
473
- nextRunId = await this.submitUserMessage(threadId, messages, actions, eventSource, forwardedParameters);
474
- } else {
475
- throw new Error("No actionable message found in the messages");
560
+ }
561
+ __name(convertMessageToLangChainMessage, "convertMessageToLangChainMessage");
562
+ function convertJsonSchemaToZodSchema(jsonSchema, required) {
563
+ if (jsonSchema.type === "object") {
564
+ const spec = {};
565
+ for (const [key, value] of Object.entries(jsonSchema.properties)) {
566
+ spec[key] = convertJsonSchemaToZodSchema(value, jsonSchema.required ? jsonSchema.required.includes(key) : false);
476
567
  }
477
- return {
478
- threadId,
479
- runId: nextRunId
480
- };
568
+ let schema = import_zod.z.object(spec);
569
+ return !required ? schema.optional() : schema;
570
+ } else if (jsonSchema.type === "string") {
571
+ let schema = import_zod.z.string().describe(jsonSchema.description);
572
+ return !required ? schema.optional() : schema;
573
+ } else if (jsonSchema.type === "number") {
574
+ let schema = import_zod.z.number().describe(jsonSchema.description);
575
+ return !required ? schema.optional() : schema;
576
+ } else if (jsonSchema.type === "boolean") {
577
+ let schema = import_zod.z.boolean().describe(jsonSchema.description);
578
+ return !required ? schema.optional() : schema;
579
+ } else if (jsonSchema.type === "array") {
580
+ let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, false);
581
+ let schema = import_zod.z.array(itemSchema);
582
+ return !required ? schema.optional() : schema;
481
583
  }
482
- async submitToolOutputs(threadId, runId, messages, eventSource) {
483
- let run = await this.openai.beta.threads.runs.retrieve(threadId, runId);
484
- if (!run.required_action) {
485
- throw new Error("No tool outputs required");
584
+ }
585
+ __name(convertJsonSchemaToZodSchema, "convertJsonSchemaToZodSchema");
586
+ function convertActionInputToLangChainTool(actionInput) {
587
+ return new import_tools.DynamicStructuredTool({
588
+ name: actionInput.name,
589
+ description: actionInput.description,
590
+ schema: convertJsonSchemaToZodSchema(JSON.parse(actionInput.jsonSchema), true),
591
+ func: async () => {
592
+ return "";
486
593
  }
487
- const toolCallsIds = run.required_action.submit_tool_outputs.tool_calls.map((toolCall) => toolCall.id);
488
- const resultMessages = messages.filter((message) => message instanceof ResultMessage && toolCallsIds.includes(message.actionExecutionId));
489
- if (toolCallsIds.length != resultMessages.length) {
490
- throw new Error("Number of function results does not match the number of tool calls");
594
+ });
595
+ }
596
+ __name(convertActionInputToLangChainTool, "convertActionInputToLangChainTool");
597
+ function getConstructorName(object) {
598
+ if (object && typeof object === "object" && object.constructor && object.constructor.name) {
599
+ return object.constructor.name;
600
+ }
601
+ return "";
602
+ }
603
+ __name(getConstructorName, "getConstructorName");
604
+ function isAIMessage(message) {
605
+ return getConstructorName(message) === "AIMessage";
606
+ }
607
+ __name(isAIMessage, "isAIMessage");
608
+ function isAIMessageChunk(message) {
609
+ return getConstructorName(message) === "AIMessageChunk";
610
+ }
611
+ __name(isAIMessageChunk, "isAIMessageChunk");
612
+ function isBaseMessageChunk(message) {
613
+ return getConstructorName(message) === "BaseMessageChunk";
614
+ }
615
+ __name(isBaseMessageChunk, "isBaseMessageChunk");
616
+ function maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution) {
617
+ if (actionExecution) {
618
+ eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, "Sending a message");
619
+ }
620
+ }
621
+ __name(maybeSendActionExecutionResultIsMessage, "maybeSendActionExecutionResultIsMessage");
622
+ async function streamLangChainResponse({ result, eventStream$, actionExecution }) {
623
+ var _a, _b, _c, _d, _e, _f, _g, _h;
624
+ if (typeof result === "string") {
625
+ if (!actionExecution) {
626
+ eventStream$.sendTextMessage((0, import_shared2.randomId)(), result);
627
+ } else {
628
+ eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, result);
491
629
  }
492
- const toolOutputs = resultMessages.map((message) => {
493
- return {
494
- tool_call_id: message.actionExecutionId,
495
- output: message.result
496
- };
497
- });
498
- const stream = this.openai.beta.threads.runs.submitToolOutputsStream(threadId, runId, {
499
- tool_outputs: toolOutputs,
500
- ...this.disableParallelToolCalls && {
501
- parallel_tool_calls: false
630
+ } else if (isAIMessage(result)) {
631
+ maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
632
+ if (result.content) {
633
+ eventStream$.sendTextMessage((0, import_shared2.randomId)(), result.content);
634
+ }
635
+ for (const toolCall of result.tool_calls) {
636
+ eventStream$.sendActionExecution(toolCall.id || (0, import_shared2.randomId)(), toolCall.name, JSON.stringify(toolCall.args));
637
+ }
638
+ } else if (isBaseMessageChunk(result)) {
639
+ maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
640
+ if ((_a = result.lc_kwargs) == null ? void 0 : _a.content) {
641
+ eventStream$.sendTextMessage((0, import_shared2.randomId)(), result.content);
642
+ }
643
+ if ((_b = result.lc_kwargs) == null ? void 0 : _b.tool_calls) {
644
+ for (const toolCall of (_c = result.lc_kwargs) == null ? void 0 : _c.tool_calls) {
645
+ eventStream$.sendActionExecution(toolCall.id || (0, import_shared2.randomId)(), toolCall.name, JSON.stringify(toolCall.args));
502
646
  }
503
- });
504
- await this.streamResponse(stream, eventSource);
505
- return runId;
506
- }
507
- async submitUserMessage(threadId, messages, actions, eventSource, forwardedParameters) {
508
- messages = [
509
- ...messages
510
- ];
511
- const instructionsMessage = messages.shift();
512
- const instructions = instructionsMessage instanceof TextMessage ? instructionsMessage.content : "";
513
- const userMessage = messages.map(convertMessageToOpenAIMessage).map(convertSystemMessageToAssistantAPI).at(-1);
514
- if (userMessage.role !== "user") {
515
- throw new Error("No user message found");
516
647
  }
517
- await this.openai.beta.threads.messages.create(threadId, {
518
- role: "user",
519
- content: userMessage.content
520
- });
521
- const openaiTools = actions.map(convertActionInputToOpenAITool);
522
- const tools = [
523
- ...openaiTools,
524
- ...this.codeInterpreterEnabled ? [
525
- {
526
- type: "code_interpreter"
648
+ } else if (result && "getReader" in result) {
649
+ maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
650
+ let reader = result.getReader();
651
+ let mode = null;
652
+ while (true) {
653
+ try {
654
+ const { done, value } = await reader.read();
655
+ let toolCallName = void 0;
656
+ let toolCallId = void 0;
657
+ let toolCallArgs = void 0;
658
+ let hasToolCall = false;
659
+ let content = value == null ? void 0 : value.content;
660
+ if (isAIMessageChunk(value)) {
661
+ let chunk = (_d = value.tool_call_chunks) == null ? void 0 : _d[0];
662
+ toolCallName = chunk == null ? void 0 : chunk.name;
663
+ toolCallId = chunk == null ? void 0 : chunk.id;
664
+ toolCallArgs = chunk == null ? void 0 : chunk.args;
665
+ hasToolCall = chunk != void 0;
666
+ } else if (isBaseMessageChunk(value)) {
667
+ let chunk = (_f = (_e = value.additional_kwargs) == null ? void 0 : _e.tool_calls) == null ? void 0 : _f[0];
668
+ toolCallName = (_g = chunk == null ? void 0 : chunk.function) == null ? void 0 : _g.name;
669
+ toolCallId = chunk == null ? void 0 : chunk.id;
670
+ toolCallArgs = (_h = chunk == null ? void 0 : chunk.function) == null ? void 0 : _h.arguments;
671
+ hasToolCall = (chunk == null ? void 0 : chunk.function) != void 0;
527
672
  }
528
- ] : [],
529
- ...this.fileSearchEnabled ? [
530
- {
531
- type: "file_search"
673
+ if (mode === "message" && (toolCallId || done)) {
674
+ mode = null;
675
+ eventStream$.sendTextMessageEnd();
676
+ } else if (mode === "function" && (!hasToolCall || done)) {
677
+ mode = null;
678
+ eventStream$.sendActionExecutionEnd();
532
679
  }
533
- ] : []
534
- ];
535
- let stream = this.openai.beta.threads.runs.stream(threadId, {
536
- assistant_id: this.assistantId,
537
- instructions,
538
- tools,
539
- ...(forwardedParameters == null ? void 0 : forwardedParameters.maxTokens) && {
540
- max_completion_tokens: forwardedParameters.maxTokens
541
- },
542
- ...this.disableParallelToolCalls && {
543
- parallel_tool_calls: false
680
+ if (done) {
681
+ break;
682
+ }
683
+ if (mode === null) {
684
+ if (hasToolCall) {
685
+ mode = "function";
686
+ eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
687
+ } else if (content) {
688
+ mode = "message";
689
+ eventStream$.sendTextMessageStart((0, import_shared2.randomId)());
690
+ }
691
+ }
692
+ if (mode === "message" && content) {
693
+ eventStream$.sendTextMessageContent(content);
694
+ } else if (mode === "function" && toolCallArgs) {
695
+ eventStream$.sendActionExecutionArgs(toolCallArgs);
696
+ }
697
+ } catch (error) {
698
+ console.error("Error reading from stream", error);
699
+ break;
544
700
  }
545
- });
546
- await this.streamResponse(stream, eventSource);
547
- return getRunIdFromStream(stream);
701
+ }
702
+ } else if (actionExecution) {
703
+ eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, encodeResult(result));
704
+ } else {
705
+ throw new Error("Invalid return type from LangChain function.");
548
706
  }
549
- async streamResponse(stream, eventSource) {
707
+ eventStream$.complete();
708
+ }
709
+ __name(streamLangChainResponse, "streamLangChainResponse");
710
+ function encodeResult(result) {
711
+ if (result === void 0) {
712
+ return "";
713
+ } else if (typeof result === "string") {
714
+ return result;
715
+ } else {
716
+ return JSON.stringify(result);
717
+ }
718
+ }
719
+ __name(encodeResult, "encodeResult");
720
+
721
+ // src/service-adapters/langchain/langchain-adapter.ts
722
+ var import_shared3 = require("@copilotkit/shared");
723
+ var LangChainAdapter = class {
724
+ options;
725
+ /**
726
+ * To use LangChain as a backend, provide a handler function to the adapter with your custom LangChain logic.
727
+ */
728
+ constructor(options) {
729
+ this.options = options;
730
+ }
731
+ async process(request) {
732
+ const { eventSource, model, actions, messages, threadId, runId } = request;
733
+ const result = await this.options.chainFn({
734
+ messages: messages.map(convertMessageToLangChainMessage),
735
+ tools: actions.map(convertActionInputToLangChainTool),
736
+ model,
737
+ threadId,
738
+ runId
739
+ });
550
740
  eventSource.stream(async (eventStream$) => {
551
- var _a, _b, _c, _d, _e, _f;
552
- let inFunctionCall = false;
553
- for await (const chunk of stream) {
554
- switch (chunk.event) {
555
- case "thread.message.created":
556
- if (inFunctionCall) {
557
- eventStream$.sendActionExecutionEnd();
558
- }
559
- eventStream$.sendTextMessageStart(chunk.data.id);
560
- break;
561
- case "thread.message.delta":
562
- if (((_a = chunk.data.delta.content) == null ? void 0 : _a[0].type) === "text") {
563
- eventStream$.sendTextMessageContent((_b = chunk.data.delta.content) == null ? void 0 : _b[0].text.value);
564
- }
565
- break;
566
- case "thread.message.completed":
567
- eventStream$.sendTextMessageEnd();
568
- break;
569
- case "thread.run.step.delta":
570
- let toolCallId;
571
- let toolCallName;
572
- let toolCallArgs;
573
- if (chunk.data.delta.step_details.type === "tool_calls" && ((_c = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _c[0].type) === "function") {
574
- toolCallId = (_d = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _d[0].id;
575
- toolCallName = (_e = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _e[0].function.name;
576
- toolCallArgs = (_f = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _f[0].function.arguments;
577
- }
578
- if (toolCallName && toolCallId) {
579
- if (inFunctionCall) {
580
- eventStream$.sendActionExecutionEnd();
581
- }
582
- inFunctionCall = true;
583
- eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
584
- } else if (toolCallArgs) {
585
- eventStream$.sendActionExecutionArgs(toolCallArgs);
586
- }
587
- break;
588
- }
589
- }
590
- if (inFunctionCall) {
591
- eventStream$.sendActionExecutionEnd();
592
- }
593
- eventStream$.complete();
741
+ await streamLangChainResponse({
742
+ result,
743
+ eventStream$
744
+ });
594
745
  });
746
+ return {
747
+ threadId: threadId || (0, import_shared3.randomId)()
748
+ };
595
749
  }
596
750
  };
597
- __name(OpenAIAssistantAdapter, "OpenAIAssistantAdapter");
598
- function getRunIdFromStream(stream) {
599
- return new Promise((resolve, reject) => {
600
- let runIdGetter = /* @__PURE__ */ __name((event) => {
601
- if (event.event === "thread.run.created") {
602
- const runId = event.data.id;
603
- stream.off("event", runIdGetter);
604
- resolve(runId);
605
- }
606
- }, "runIdGetter");
607
- stream.on("event", runIdGetter);
608
- });
609
- }
610
- __name(getRunIdFromStream, "getRunIdFromStream");
751
+ __name(LangChainAdapter, "LangChainAdapter");
611
752
 
612
753
  // src/service-adapters/google/google-genai-adapter.ts
613
754
  var import_generative_ai = require("@google/generative-ai");
@@ -698,7 +839,7 @@ function tryParseJson(str) {
698
839
  __name(tryParseJson, "tryParseJson");
699
840
 
700
841
  // src/service-adapters/google/google-genai-adapter.ts
701
- var import_shared2 = require("@copilotkit/shared");
842
+ var import_shared4 = require("@copilotkit/shared");
702
843
  var GoogleGenerativeAIAdapter = class {
703
844
  model;
704
845
  constructor(options) {
@@ -765,7 +906,7 @@ var GoogleGenerativeAIAdapter = class {
765
906
  }
766
907
  if (!isTextMessage) {
767
908
  isTextMessage = true;
768
- eventStream$.sendTextMessageStart((0, import_shared2.randomId)());
909
+ eventStream$.sendTextMessageStart((0, import_shared4.randomId)());
769
910
  }
770
911
  eventStream$.sendTextMessageContent(chunkText);
771
912
  }
@@ -775,13 +916,13 @@ var GoogleGenerativeAIAdapter = class {
775
916
  let calls = (await result.response).functionCalls();
776
917
  if (calls) {
777
918
  for (let call of calls) {
778
- eventStream$.sendActionExecution((0, import_shared2.randomId)(), call.name, JSON.stringify(replaceNewlinesInObject(call.args)));
919
+ eventStream$.sendActionExecution((0, import_shared4.randomId)(), call.name, JSON.stringify(replaceNewlinesInObject(call.args)));
779
920
  }
780
921
  }
781
922
  eventStream$.complete();
782
923
  });
783
924
  return {
784
- threadId: request.threadId || (0, import_shared2.randomId)()
925
+ threadId: request.threadId || (0, import_shared4.randomId)()
785
926
  };
786
927
  }
787
928
  };
@@ -804,307 +945,167 @@ function replaceNewlinesInObject(obj) {
804
945
  }
805
946
  __name(replaceNewlinesInObject, "replaceNewlinesInObject");
806
947
 
807
- // src/service-adapters/langchain/utils.ts
808
- var import_messages = require("@langchain/core/messages");
809
- var import_tools = require("@langchain/core/tools");
810
- var import_zod = require("zod");
811
- var import_shared3 = require("@copilotkit/shared");
812
- function convertMessageToLangChainMessage(message) {
813
- if (message instanceof TextMessage) {
814
- if (message.role == "user") {
815
- return new import_messages.HumanMessage(message.content);
816
- } else if (message.role == "assistant") {
817
- return new import_messages.AIMessage(message.content);
818
- } else if (message.role === "system") {
819
- return new import_messages.SystemMessage(message.content);
820
- }
821
- } else if (message instanceof ActionExecutionMessage) {
822
- return new import_messages.AIMessage({
823
- content: "",
824
- tool_calls: [
825
- {
826
- id: message.id,
827
- args: message.arguments,
828
- name: message.name
829
- }
830
- ]
831
- });
832
- } else if (message instanceof ResultMessage) {
833
- return new import_messages.ToolMessage({
834
- content: message.result,
835
- tool_call_id: message.actionExecutionId
836
- });
837
- }
838
- }
839
- __name(convertMessageToLangChainMessage, "convertMessageToLangChainMessage");
840
- function convertJsonSchemaToZodSchema(jsonSchema, required) {
841
- if (jsonSchema.type === "object") {
842
- const spec = {};
843
- for (const [key, value] of Object.entries(jsonSchema.properties)) {
844
- spec[key] = convertJsonSchemaToZodSchema(value, jsonSchema.required ? jsonSchema.required.includes(key) : false);
845
- }
846
- let schema = import_zod.z.object(spec);
847
- return !required ? schema.optional() : schema;
848
- } else if (jsonSchema.type === "string") {
849
- let schema = import_zod.z.string().describe(jsonSchema.description);
850
- return !required ? schema.optional() : schema;
851
- } else if (jsonSchema.type === "number") {
852
- let schema = import_zod.z.number().describe(jsonSchema.description);
853
- return !required ? schema.optional() : schema;
854
- } else if (jsonSchema.type === "boolean") {
855
- let schema = import_zod.z.boolean().describe(jsonSchema.description);
856
- return !required ? schema.optional() : schema;
857
- } else if (jsonSchema.type === "array") {
858
- let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, false);
859
- let schema = import_zod.z.array(itemSchema);
860
- return !required ? schema.optional() : schema;
861
- }
862
- }
863
- __name(convertJsonSchemaToZodSchema, "convertJsonSchemaToZodSchema");
864
- function convertActionInputToLangChainTool(actionInput) {
865
- return new import_tools.DynamicStructuredTool({
866
- name: actionInput.name,
867
- description: actionInput.description,
868
- schema: convertJsonSchemaToZodSchema(JSON.parse(actionInput.jsonSchema), true),
869
- func: async () => {
870
- return "";
871
- }
872
- });
873
- }
874
- __name(convertActionInputToLangChainTool, "convertActionInputToLangChainTool");
875
- function getConstructorName(object) {
876
- if (object && typeof object === "object" && object.constructor && object.constructor.name) {
877
- return object.constructor.name;
878
- }
879
- return "";
880
- }
881
- __name(getConstructorName, "getConstructorName");
882
- function isAIMessage(message) {
883
- return getConstructorName(message) === "AIMessage";
884
- }
885
- __name(isAIMessage, "isAIMessage");
886
- function isAIMessageChunk(message) {
887
- return getConstructorName(message) === "AIMessageChunk";
888
- }
889
- __name(isAIMessageChunk, "isAIMessageChunk");
890
- function isBaseMessageChunk(message) {
891
- return getConstructorName(message) === "BaseMessageChunk";
892
- }
893
- __name(isBaseMessageChunk, "isBaseMessageChunk");
894
- function maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution) {
895
- if (actionExecution) {
896
- eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, "Sending a message");
948
+ // src/service-adapters/openai/openai-assistant-adapter.ts
949
+ var import_openai2 = __toESM(require("openai"));
950
+ var OpenAIAssistantAdapter = class {
951
+ openai;
952
+ codeInterpreterEnabled;
953
+ assistantId;
954
+ fileSearchEnabled;
955
+ disableParallelToolCalls;
956
+ constructor(params) {
957
+ this.openai = params.openai || new import_openai2.default({});
958
+ this.codeInterpreterEnabled = params.codeInterpreterEnabled === false || true;
959
+ this.fileSearchEnabled = params.fileSearchEnabled === false || true;
960
+ this.assistantId = params.assistantId;
961
+ this.disableParallelToolCalls = (params == null ? void 0 : params.disableParallelToolCalls) || false;
897
962
  }
898
- }
899
- __name(maybeSendActionExecutionResultIsMessage, "maybeSendActionExecutionResultIsMessage");
900
- async function streamLangChainResponse({ result, eventStream$, actionExecution }) {
901
- var _a, _b, _c, _d, _e, _f, _g, _h;
902
- if (typeof result === "string") {
903
- if (!actionExecution) {
904
- eventStream$.sendTextMessage((0, import_shared3.randomId)(), result);
963
+ async process(request) {
964
+ const { messages, actions, eventSource, runId, forwardedParameters } = request;
965
+ let threadId = request.threadId || (await this.openai.beta.threads.create()).id;
966
+ const lastMessage = messages.at(-1);
967
+ let nextRunId = void 0;
968
+ if (lastMessage instanceof ResultMessage && runId) {
969
+ nextRunId = await this.submitToolOutputs(threadId, runId, messages, eventSource);
970
+ } else if (lastMessage instanceof TextMessage) {
971
+ nextRunId = await this.submitUserMessage(threadId, messages, actions, eventSource, forwardedParameters);
905
972
  } else {
906
- eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, result);
907
- }
908
- } else if (isAIMessage(result)) {
909
- maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
910
- if (result.content) {
911
- eventStream$.sendTextMessage((0, import_shared3.randomId)(), result.content);
912
- }
913
- for (const toolCall of result.tool_calls) {
914
- eventStream$.sendActionExecution(toolCall.id || (0, import_shared3.randomId)(), toolCall.name, JSON.stringify(toolCall.args));
915
- }
916
- } else if (isBaseMessageChunk(result)) {
917
- maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
918
- if ((_a = result.lc_kwargs) == null ? void 0 : _a.content) {
919
- eventStream$.sendTextMessage((0, import_shared3.randomId)(), result.content);
920
- }
921
- if ((_b = result.lc_kwargs) == null ? void 0 : _b.tool_calls) {
922
- for (const toolCall of (_c = result.lc_kwargs) == null ? void 0 : _c.tool_calls) {
923
- eventStream$.sendActionExecution(toolCall.id || (0, import_shared3.randomId)(), toolCall.name, JSON.stringify(toolCall.args));
924
- }
925
- }
926
- } else if (result && "getReader" in result) {
927
- maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
928
- let reader = result.getReader();
929
- let mode = null;
930
- while (true) {
931
- try {
932
- const { done, value } = await reader.read();
933
- let toolCallName = void 0;
934
- let toolCallId = void 0;
935
- let toolCallArgs = void 0;
936
- let hasToolCall = false;
937
- let content = value == null ? void 0 : value.content;
938
- if (isAIMessageChunk(value)) {
939
- let chunk = (_d = value.tool_call_chunks) == null ? void 0 : _d[0];
940
- toolCallName = chunk == null ? void 0 : chunk.name;
941
- toolCallId = chunk == null ? void 0 : chunk.id;
942
- toolCallArgs = chunk == null ? void 0 : chunk.args;
943
- hasToolCall = chunk != void 0;
944
- } else if (isBaseMessageChunk(value)) {
945
- let chunk = (_f = (_e = value.additional_kwargs) == null ? void 0 : _e.tool_calls) == null ? void 0 : _f[0];
946
- toolCallName = (_g = chunk == null ? void 0 : chunk.function) == null ? void 0 : _g.name;
947
- toolCallId = chunk == null ? void 0 : chunk.id;
948
- toolCallArgs = (_h = chunk == null ? void 0 : chunk.function) == null ? void 0 : _h.arguments;
949
- hasToolCall = (chunk == null ? void 0 : chunk.function) != void 0;
950
- }
951
- if (mode === "message" && (toolCallId || done)) {
952
- mode = null;
953
- eventStream$.sendTextMessageEnd();
954
- } else if (mode === "function" && (!hasToolCall || done)) {
955
- mode = null;
956
- eventStream$.sendActionExecutionEnd();
957
- }
958
- if (done) {
959
- break;
960
- }
961
- if (mode === null) {
962
- if (hasToolCall) {
963
- mode = "function";
964
- eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
965
- } else if (content) {
966
- mode = "message";
967
- eventStream$.sendTextMessageStart((0, import_shared3.randomId)());
968
- }
969
- }
970
- if (mode === "message" && content) {
971
- eventStream$.sendTextMessageContent(content);
972
- } else if (mode === "function" && toolCallArgs) {
973
- eventStream$.sendActionExecutionArgs(toolCallArgs);
974
- }
975
- } catch (error) {
976
- console.error("Error reading from stream", error);
977
- break;
978
- }
973
+ throw new Error("No actionable message found in the messages");
979
974
  }
980
- } else if (actionExecution) {
981
- eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, encodeResult(result));
982
- } else {
983
- throw new Error("Invalid return type from LangChain function.");
984
- }
985
- eventStream$.complete();
986
- }
987
- __name(streamLangChainResponse, "streamLangChainResponse");
988
- function encodeResult(result) {
989
- if (result === void 0) {
990
- return "";
991
- } else if (typeof result === "string") {
992
- return result;
993
- } else {
994
- return JSON.stringify(result);
995
- }
996
- }
997
- __name(encodeResult, "encodeResult");
998
-
999
- // src/service-adapters/langchain/langchain-adapter.ts
1000
- var import_shared4 = require("@copilotkit/shared");
1001
- var LangChainAdapter = class {
1002
- options;
1003
- /**
1004
- * To use LangChain as a backend, provide a handler function to the adapter with your custom LangChain logic.
1005
- */
1006
- constructor(options) {
1007
- this.options = options;
1008
- }
1009
- async process(request) {
1010
- const { eventSource, model, actions, messages, threadId, runId } = request;
1011
- const result = await this.options.chainFn({
1012
- messages: messages.map(convertMessageToLangChainMessage),
1013
- tools: actions.map(convertActionInputToLangChainTool),
1014
- model,
1015
- threadId,
1016
- runId
1017
- });
1018
- eventSource.stream(async (eventStream$) => {
1019
- await streamLangChainResponse({
1020
- result,
1021
- eventStream$
1022
- });
1023
- });
1024
975
  return {
1025
- threadId: threadId || (0, import_shared4.randomId)()
976
+ threadId,
977
+ runId: nextRunId
1026
978
  };
1027
979
  }
1028
- };
1029
- __name(LangChainAdapter, "LangChainAdapter");
1030
-
1031
- // src/service-adapters/langchain/langserve.ts
1032
- var import_remote = require("langchain/runnables/remote");
1033
- var RemoteChain = class {
1034
- name;
1035
- description;
1036
- chainUrl;
1037
- parameters;
1038
- parameterType;
1039
- constructor(options) {
1040
- this.name = options.name;
1041
- this.description = options.description;
1042
- this.chainUrl = options.chainUrl;
1043
- this.parameters = options.parameters;
1044
- this.parameterType = options.parameterType || "multi";
1045
- }
1046
- async toAction() {
1047
- if (!this.parameters) {
1048
- await this.inferLangServeParameters();
980
+ async submitToolOutputs(threadId, runId, messages, eventSource) {
981
+ let run = await this.openai.beta.threads.runs.retrieve(threadId, runId);
982
+ if (!run.required_action) {
983
+ throw new Error("No tool outputs required");
1049
984
  }
1050
- return {
1051
- name: this.name,
1052
- description: this.description,
1053
- parameters: this.parameters,
1054
- handler: async (args) => {
1055
- const runnable = new import_remote.RemoteRunnable({
1056
- url: this.chainUrl
1057
- });
1058
- let input;
1059
- if (this.parameterType === "single") {
1060
- input = args[Object.keys(args)[0]];
1061
- } else {
1062
- input = args;
1063
- }
1064
- return await runnable.invoke(input);
985
+ const toolCallsIds = run.required_action.submit_tool_outputs.tool_calls.map((toolCall) => toolCall.id);
986
+ const resultMessages = messages.filter((message) => message instanceof ResultMessage && toolCallsIds.includes(message.actionExecutionId));
987
+ if (toolCallsIds.length != resultMessages.length) {
988
+ throw new Error("Number of function results does not match the number of tool calls");
989
+ }
990
+ const toolOutputs = resultMessages.map((message) => {
991
+ return {
992
+ tool_call_id: message.actionExecutionId,
993
+ output: message.result
994
+ };
995
+ });
996
+ const stream = this.openai.beta.threads.runs.submitToolOutputsStream(threadId, runId, {
997
+ tool_outputs: toolOutputs,
998
+ ...this.disableParallelToolCalls && {
999
+ parallel_tool_calls: false
1065
1000
  }
1066
- };
1001
+ });
1002
+ await this.streamResponse(stream, eventSource);
1003
+ return runId;
1067
1004
  }
1068
- async inferLangServeParameters() {
1069
- const supportedTypes = [
1070
- "string",
1071
- "number",
1072
- "boolean"
1073
- ];
1074
- let schemaUrl = this.chainUrl.replace(/\/+$/, "") + "/input_schema";
1075
- let schema = await fetch(schemaUrl).then((res) => res.json()).catch(() => {
1076
- throw new Error("Failed to fetch langserve schema at " + schemaUrl);
1005
+ async submitUserMessage(threadId, messages, actions, eventSource, forwardedParameters) {
1006
+ messages = [
1007
+ ...messages
1008
+ ];
1009
+ const instructionsMessage = messages.shift();
1010
+ const instructions = instructionsMessage instanceof TextMessage ? instructionsMessage.content : "";
1011
+ const userMessage = messages.map(convertMessageToOpenAIMessage).map(convertSystemMessageToAssistantAPI).at(-1);
1012
+ if (userMessage.role !== "user") {
1013
+ throw new Error("No user message found");
1014
+ }
1015
+ await this.openai.beta.threads.messages.create(threadId, {
1016
+ role: "user",
1017
+ content: userMessage.content
1077
1018
  });
1078
- if (supportedTypes.includes(schema.type)) {
1079
- this.parameterType = "single";
1080
- this.parameters = [
1019
+ const openaiTools = actions.map(convertActionInputToOpenAITool);
1020
+ const tools = [
1021
+ ...openaiTools,
1022
+ ...this.codeInterpreterEnabled ? [
1081
1023
  {
1082
- name: "input",
1083
- type: schema.type,
1084
- description: "The input to the chain"
1024
+ type: "code_interpreter"
1085
1025
  }
1086
- ];
1087
- } else if (schema.type === "object") {
1088
- this.parameterType = "multi";
1089
- this.parameters = Object.keys(schema.properties).map((key) => {
1090
- var _a;
1091
- let property = schema.properties[key];
1092
- if (!supportedTypes.includes(property.type)) {
1093
- throw new Error("Unsupported schema type");
1026
+ ] : [],
1027
+ ...this.fileSearchEnabled ? [
1028
+ {
1029
+ type: "file_search"
1094
1030
  }
1095
- return {
1096
- name: key,
1097
- type: property.type,
1098
- description: property.description || "",
1099
- required: ((_a = schema.required) == null ? void 0 : _a.includes(key)) || false
1100
- };
1101
- });
1102
- } else {
1103
- throw new Error("Unsupported schema type");
1104
- }
1031
+ ] : []
1032
+ ];
1033
+ let stream = this.openai.beta.threads.runs.stream(threadId, {
1034
+ assistant_id: this.assistantId,
1035
+ instructions,
1036
+ tools,
1037
+ ...(forwardedParameters == null ? void 0 : forwardedParameters.maxTokens) && {
1038
+ max_completion_tokens: forwardedParameters.maxTokens
1039
+ },
1040
+ ...this.disableParallelToolCalls && {
1041
+ parallel_tool_calls: false
1042
+ }
1043
+ });
1044
+ await this.streamResponse(stream, eventSource);
1045
+ return getRunIdFromStream(stream);
1046
+ }
1047
+ async streamResponse(stream, eventSource) {
1048
+ eventSource.stream(async (eventStream$) => {
1049
+ var _a, _b, _c, _d, _e, _f;
1050
+ let inFunctionCall = false;
1051
+ for await (const chunk of stream) {
1052
+ switch (chunk.event) {
1053
+ case "thread.message.created":
1054
+ if (inFunctionCall) {
1055
+ eventStream$.sendActionExecutionEnd();
1056
+ }
1057
+ eventStream$.sendTextMessageStart(chunk.data.id);
1058
+ break;
1059
+ case "thread.message.delta":
1060
+ if (((_a = chunk.data.delta.content) == null ? void 0 : _a[0].type) === "text") {
1061
+ eventStream$.sendTextMessageContent((_b = chunk.data.delta.content) == null ? void 0 : _b[0].text.value);
1062
+ }
1063
+ break;
1064
+ case "thread.message.completed":
1065
+ eventStream$.sendTextMessageEnd();
1066
+ break;
1067
+ case "thread.run.step.delta":
1068
+ let toolCallId;
1069
+ let toolCallName;
1070
+ let toolCallArgs;
1071
+ if (chunk.data.delta.step_details.type === "tool_calls" && ((_c = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _c[0].type) === "function") {
1072
+ toolCallId = (_d = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _d[0].id;
1073
+ toolCallName = (_e = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _e[0].function.name;
1074
+ toolCallArgs = (_f = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _f[0].function.arguments;
1075
+ }
1076
+ if (toolCallName && toolCallId) {
1077
+ if (inFunctionCall) {
1078
+ eventStream$.sendActionExecutionEnd();
1079
+ }
1080
+ inFunctionCall = true;
1081
+ eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
1082
+ } else if (toolCallArgs) {
1083
+ eventStream$.sendActionExecutionArgs(toolCallArgs);
1084
+ }
1085
+ break;
1086
+ }
1087
+ }
1088
+ if (inFunctionCall) {
1089
+ eventStream$.sendActionExecutionEnd();
1090
+ }
1091
+ eventStream$.complete();
1092
+ });
1105
1093
  }
1106
1094
  };
1107
- __name(RemoteChain, "RemoteChain");
1095
+ __name(OpenAIAssistantAdapter, "OpenAIAssistantAdapter");
1096
+ function getRunIdFromStream(stream) {
1097
+ return new Promise((resolve, reject) => {
1098
+ let runIdGetter = /* @__PURE__ */ __name((event) => {
1099
+ if (event.event === "thread.run.created") {
1100
+ const runId = event.data.id;
1101
+ stream.off("event", runIdGetter);
1102
+ resolve(runId);
1103
+ }
1104
+ }, "runIdGetter");
1105
+ stream.on("event", runIdGetter);
1106
+ });
1107
+ }
1108
+ __name(getRunIdFromStream, "getRunIdFromStream");
1108
1109
 
1109
1110
  // src/service-adapters/unify/unify-adapter.ts
1110
1111
  var import_openai3 = __toESM(require("openai"));
@@ -1282,13 +1283,277 @@ var GroqAdapter = class {
1282
1283
  };
1283
1284
  __name(GroqAdapter, "GroqAdapter");
1284
1285
 
1286
+ // src/service-adapters/anthropic/anthropic-adapter.ts
1287
+ var import_sdk = __toESM(require("@anthropic-ai/sdk"));
1288
+
1289
+ // src/service-adapters/anthropic/utils.ts
1290
+ function limitMessagesToTokenCount2(messages, tools, model, maxTokens) {
1291
+ maxTokens || (maxTokens = MAX_TOKENS);
1292
+ const result = [];
1293
+ const toolsNumTokens = countToolsTokens2(model, tools);
1294
+ if (toolsNumTokens > maxTokens) {
1295
+ throw new Error(`Too many tokens in function definitions: ${toolsNumTokens} > ${maxTokens}`);
1296
+ }
1297
+ maxTokens -= toolsNumTokens;
1298
+ for (const message of messages) {
1299
+ if (message.role === "system") {
1300
+ const numTokens = countMessageTokens2(model, message);
1301
+ maxTokens -= numTokens;
1302
+ if (maxTokens < 0) {
1303
+ throw new Error("Not enough tokens for system message.");
1304
+ }
1305
+ }
1306
+ }
1307
+ let cutoff = false;
1308
+ const reversedMessages = [
1309
+ ...messages
1310
+ ].reverse();
1311
+ for (const message of reversedMessages) {
1312
+ if (message.role === "system") {
1313
+ result.unshift(message);
1314
+ continue;
1315
+ } else if (cutoff) {
1316
+ continue;
1317
+ }
1318
+ let numTokens = countMessageTokens2(model, message);
1319
+ if (maxTokens < numTokens) {
1320
+ cutoff = true;
1321
+ continue;
1322
+ }
1323
+ result.unshift(message);
1324
+ maxTokens -= numTokens;
1325
+ }
1326
+ return result;
1327
+ }
1328
+ __name(limitMessagesToTokenCount2, "limitMessagesToTokenCount");
1329
+ var MAX_TOKENS = 128e3;
1330
+ function countToolsTokens2(model, tools) {
1331
+ if (tools.length === 0) {
1332
+ return 0;
1333
+ }
1334
+ const json = JSON.stringify(tools);
1335
+ return countTokens2(model, json);
1336
+ }
1337
+ __name(countToolsTokens2, "countToolsTokens");
1338
+ function countMessageTokens2(model, message) {
1339
+ return countTokens2(model, JSON.stringify(message.content) || "");
1340
+ }
1341
+ __name(countMessageTokens2, "countMessageTokens");
1342
+ function countTokens2(model, text) {
1343
+ return text.length / 3;
1344
+ }
1345
+ __name(countTokens2, "countTokens");
1346
+ function convertActionInputToAnthropicTool(action) {
1347
+ return {
1348
+ name: action.name,
1349
+ description: action.description,
1350
+ input_schema: JSON.parse(action.jsonSchema)
1351
+ };
1352
+ }
1353
+ __name(convertActionInputToAnthropicTool, "convertActionInputToAnthropicTool");
1354
+ function convertMessageToAnthropicMessage(message) {
1355
+ if (message instanceof TextMessage) {
1356
+ if (message.role === "system") {
1357
+ return {
1358
+ role: "assistant",
1359
+ content: [
1360
+ {
1361
+ type: "text",
1362
+ text: "THE FOLLOWING MESSAGE IS A SYSTEM MESSAGE: " + message.content
1363
+ }
1364
+ ]
1365
+ };
1366
+ } else {
1367
+ return {
1368
+ role: message.role === "user" ? "user" : "assistant",
1369
+ content: [
1370
+ {
1371
+ type: "text",
1372
+ text: message.content
1373
+ }
1374
+ ]
1375
+ };
1376
+ }
1377
+ } else if (message instanceof ActionExecutionMessage) {
1378
+ return {
1379
+ role: "assistant",
1380
+ content: [
1381
+ {
1382
+ id: message.id,
1383
+ type: "tool_use",
1384
+ input: message.arguments,
1385
+ name: message.name
1386
+ }
1387
+ ]
1388
+ };
1389
+ } else if (message instanceof ResultMessage) {
1390
+ return {
1391
+ role: "user",
1392
+ content: [
1393
+ {
1394
+ type: "tool_result",
1395
+ content: message.result,
1396
+ tool_use_id: message.actionExecutionId
1397
+ }
1398
+ ]
1399
+ };
1400
+ }
1401
+ }
1402
+ __name(convertMessageToAnthropicMessage, "convertMessageToAnthropicMessage");
1403
+ function groupAnthropicMessagesByRole(messageParams) {
1404
+ return messageParams.reduce((acc, message) => {
1405
+ const lastGroup = acc[acc.length - 1];
1406
+ if (lastGroup && lastGroup.role === message.role) {
1407
+ lastGroup.content = lastGroup.content.concat(message.content);
1408
+ } else {
1409
+ acc.push({
1410
+ role: message.role,
1411
+ content: [
1412
+ ...message.content
1413
+ ]
1414
+ });
1415
+ }
1416
+ return acc;
1417
+ }, []);
1418
+ }
1419
+ __name(groupAnthropicMessagesByRole, "groupAnthropicMessagesByRole");
1420
+
1421
+ // src/service-adapters/anthropic/anthropic-adapter.ts
1422
+ var import_shared7 = require("@copilotkit/shared");
1423
+ var DEFAULT_MODEL3 = "claude-3-opus-20240229";
1424
+ var AnthropicAdapter = class {
1425
+ model = DEFAULT_MODEL3;
1426
+ _anthropic;
1427
+ get anthropic() {
1428
+ return this._anthropic;
1429
+ }
1430
+ constructor(params) {
1431
+ this._anthropic = (params == null ? void 0 : params.anthropic) || new import_sdk.default({});
1432
+ if (params == null ? void 0 : params.model) {
1433
+ this.model = params.model;
1434
+ }
1435
+ }
1436
+ async process(request) {
1437
+ const { threadId, model = this.model, messages: rawMessages, actions, eventSource, forwardedParameters } = request;
1438
+ const tools = actions.map(convertActionInputToAnthropicTool);
1439
+ const messages = [
1440
+ ...rawMessages
1441
+ ];
1442
+ const instructionsMessage = messages.shift();
1443
+ const instructions = instructionsMessage instanceof TextMessage ? instructionsMessage.content : "";
1444
+ let anthropicMessages = messages.map(convertMessageToAnthropicMessage);
1445
+ anthropicMessages = limitMessagesToTokenCount2(anthropicMessages, tools, model);
1446
+ anthropicMessages = groupAnthropicMessagesByRole(anthropicMessages);
1447
+ let toolChoice = forwardedParameters == null ? void 0 : forwardedParameters.toolChoice;
1448
+ if ((forwardedParameters == null ? void 0 : forwardedParameters.toolChoice) === "function") {
1449
+ toolChoice = {
1450
+ type: "tool",
1451
+ name: forwardedParameters.toolChoiceFunctionName
1452
+ };
1453
+ }
1454
+ const stream = this.anthropic.messages.create({
1455
+ system: instructions,
1456
+ model: this.model,
1457
+ messages: anthropicMessages,
1458
+ max_tokens: (forwardedParameters == null ? void 0 : forwardedParameters.maxTokens) || 1024,
1459
+ ...tools.length > 0 && {
1460
+ tools
1461
+ },
1462
+ ...toolChoice && {
1463
+ tool_choice: toolChoice
1464
+ },
1465
+ stream: true
1466
+ });
1467
+ eventSource.stream(async (eventStream$) => {
1468
+ let mode = null;
1469
+ let didOutputText = false;
1470
+ let currentMessageId = (0, import_shared7.randomId)();
1471
+ let currentToolCallId = (0, import_shared7.randomId)();
1472
+ let filterThinkingTextBuffer = new FilterThinkingTextBuffer();
1473
+ for await (const chunk of await stream) {
1474
+ if (chunk.type === "message_start") {
1475
+ currentMessageId = chunk.message.id;
1476
+ } else if (chunk.type === "content_block_start") {
1477
+ if (chunk.content_block.type === "text") {
1478
+ didOutputText = false;
1479
+ filterThinkingTextBuffer.reset();
1480
+ mode = "message";
1481
+ } else if (chunk.content_block.type === "tool_use") {
1482
+ currentToolCallId = chunk.content_block.id;
1483
+ eventStream$.sendActionExecutionStart(currentToolCallId, chunk.content_block.name);
1484
+ mode = "function";
1485
+ }
1486
+ } else if (chunk.type === "content_block_delta") {
1487
+ if (chunk.delta.type === "text_delta") {
1488
+ const text = filterThinkingTextBuffer.onTextChunk(chunk.delta.text);
1489
+ if (text.length > 0) {
1490
+ if (!didOutputText) {
1491
+ eventStream$.sendTextMessageStart(currentMessageId);
1492
+ didOutputText = true;
1493
+ }
1494
+ eventStream$.sendTextMessageContent(text);
1495
+ }
1496
+ } else if (chunk.delta.type === "input_json_delta") {
1497
+ eventStream$.sendActionExecutionArgs(chunk.delta.partial_json);
1498
+ }
1499
+ } else if (chunk.type === "content_block_stop") {
1500
+ if (mode === "message") {
1501
+ if (didOutputText) {
1502
+ eventStream$.sendTextMessageEnd();
1503
+ }
1504
+ } else if (mode === "function") {
1505
+ eventStream$.sendActionExecutionEnd();
1506
+ }
1507
+ }
1508
+ }
1509
+ eventStream$.complete();
1510
+ });
1511
+ return {
1512
+ threadId: threadId || (0, import_shared7.randomId)()
1513
+ };
1514
+ }
1515
+ };
1516
+ __name(AnthropicAdapter, "AnthropicAdapter");
1517
+ var THINKING_TAG = "<thinking>";
1518
+ var THINKING_TAG_END = "</thinking>";
1519
+ var FilterThinkingTextBuffer = /* @__PURE__ */ __name(class FilterThinkingTextBuffer2 {
1520
+ buffer;
1521
+ didFilterThinkingTag = false;
1522
+ constructor() {
1523
+ this.buffer = "";
1524
+ }
1525
+ onTextChunk(text) {
1526
+ this.buffer += text;
1527
+ if (this.didFilterThinkingTag) {
1528
+ return text;
1529
+ }
1530
+ const potentialTag = this.buffer.slice(0, THINKING_TAG.length);
1531
+ if (THINKING_TAG.startsWith(potentialTag)) {
1532
+ if (this.buffer.includes(THINKING_TAG_END)) {
1533
+ const end = this.buffer.indexOf(THINKING_TAG_END);
1534
+ const filteredText = this.buffer.slice(end + THINKING_TAG_END.length);
1535
+ this.buffer = filteredText;
1536
+ this.didFilterThinkingTag = true;
1537
+ return filteredText;
1538
+ } else {
1539
+ return "";
1540
+ }
1541
+ }
1542
+ return text;
1543
+ }
1544
+ reset() {
1545
+ this.buffer = "";
1546
+ this.didFilterThinkingTag = false;
1547
+ }
1548
+ }, "FilterThinkingTextBuffer");
1549
+
1285
1550
  // src/service-adapters/events.ts
1286
1551
  var import_rxjs2 = require("rxjs");
1287
1552
 
1288
1553
  // src/lib/telemetry-client.ts
1289
- var import_shared7 = require("@copilotkit/shared");
1554
+ var import_shared8 = require("@copilotkit/shared");
1290
1555
  var packageJson = require_package();
1291
- var telemetryClient = new import_shared7.TelemetryClient({
1556
+ var telemetryClient = new import_shared8.TelemetryClient({
1292
1557
  packageName: packageJson.name,
1293
1558
  packageVersion: packageJson.version
1294
1559
  });
@@ -1902,7 +2167,7 @@ var CopilotRuntime = class {
1902
2167
  const serverSideActionsInput = serverSideActions.map((action) => ({
1903
2168
  name: action.name,
1904
2169
  description: action.description,
1905
- jsonSchema: JSON.stringify((0, import_shared8.actionParametersToJsonSchema)(action.parameters))
2170
+ jsonSchema: JSON.stringify((0, import_shared9.actionParametersToJsonSchema)(action.parameters))
1906
2171
  }));
1907
2172
  const actionInputs = flattenToolCallsNoDuplicates([
1908
2173
  ...serverSideActionsInput,
@@ -1955,7 +2220,7 @@ var CopilotRuntime = class {
1955
2220
  async processAgentRequest(request) {
1956
2221
  var _a;
1957
2222
  const { messages: rawMessages, outputMessagesPromise, graphqlContext, agentSession } = request;
1958
- const { threadId = (0, import_shared8.randomId)(), agentName, nodeName } = agentSession;
2223
+ const { threadId = (0, import_shared9.randomId)(), agentName, nodeName } = agentSession;
1959
2224
  const serverSideActions = await this.getServerSideActions(request);
1960
2225
  const messages = convertGqlInputToMessages(rawMessages);
1961
2226
  const agent = serverSideActions.find((action) => action.name === agentName && isLangGraphAgentAction(action));
@@ -1965,7 +2230,7 @@ var CopilotRuntime = class {
1965
2230
  const serverSideActionsInput = serverSideActions.filter((action) => !isLangGraphAgentAction(action)).map((action) => ({
1966
2231
  name: action.name,
1967
2232
  description: action.description,
1968
- jsonSchema: JSON.stringify((0, import_shared8.actionParametersToJsonSchema)(action.parameters))
2233
+ jsonSchema: JSON.stringify((0, import_shared9.actionParametersToJsonSchema)(action.parameters))
1969
2234
  }));
1970
2235
  const actionInputsWithoutAgents = flattenToolCallsNoDuplicates([
1971
2236
  ...serverSideActionsInput,
@@ -3107,7 +3372,7 @@ var UnknownErrorResponse = class extends FailedResponseStatus {
3107
3372
  __name(UnknownErrorResponse, "UnknownErrorResponse");
3108
3373
 
3109
3374
  // src/graphql/resolvers/copilot.resolver.ts
3110
- var import_shared9 = require("@copilotkit/shared");
3375
+ var import_shared10 = require("@copilotkit/shared");
3111
3376
  function _ts_decorate14(decorators, target, key, desc) {
3112
3377
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3113
3378
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -3224,7 +3489,7 @@ var CopilotResolver = class {
3224
3489
  rejectOutputMessagesPromise = reject;
3225
3490
  });
3226
3491
  logger2.debug("Processing");
3227
- const { eventSource, threadId = (0, import_shared9.randomId)(), runId, serverSideActions, actionInputsWithoutAgents } = await copilotRuntime.processRuntimeRequest({
3492
+ const { eventSource, threadId = (0, import_shared10.randomId)(), runId, serverSideActions, actionInputsWithoutAgents } = await copilotRuntime.processRuntimeRequest({
3228
3493
  serviceAdapter,
3229
3494
  messages: data.messages,
3230
3495
  actions: data.frontend.actions,
@@ -3269,7 +3534,7 @@ var CopilotResolver = class {
3269
3534
  });
3270
3535
  outputMessages = [
3271
3536
  (0, import_class_transformer2.plainToInstance)(TextMessage, {
3272
- id: (0, import_shared9.randomId)(),
3537
+ id: (0, import_shared10.randomId)(),
3273
3538
  createdAt: /* @__PURE__ */ new Date(),
3274
3539
  content: result.reason,
3275
3540
  role: MessageRole.assistant
@@ -3320,7 +3585,7 @@ var CopilotResolver = class {
3320
3585
  (0, import_rxjs4.takeWhile)((e) => e.type != RuntimeEventTypes.TextMessageEnd)
3321
3586
  );
3322
3587
  const streamingTextStatus = new import_rxjs4.Subject();
3323
- const messageId = (0, import_shared9.randomId)();
3588
+ const messageId = (0, import_shared10.randomId)();
3324
3589
  pushMessage({
3325
3590
  id: messageId,
3326
3591
  status: (0, import_rxjs4.firstValueFrom)(streamingTextStatus),
@@ -3432,7 +3697,7 @@ var CopilotResolver = class {
3432
3697
  result: event.result
3433
3698
  }, "Action execution result event received");
3434
3699
  pushMessage({
3435
- id: (0, import_shared9.randomId)(),
3700
+ id: (0, import_shared10.randomId)(),
3436
3701
  status: new SuccessMessageStatus(),
3437
3702
  createdAt: /* @__PURE__ */ new Date(),
3438
3703
  actionExecutionId: event.actionExecutionId,
@@ -3440,7 +3705,7 @@ var CopilotResolver = class {
3440
3705
  result: event.result
3441
3706
  });
3442
3707
  outputMessages.push((0, import_class_transformer2.plainToInstance)(ResultMessage, {
3443
- id: (0, import_shared9.randomId)(),
3708
+ id: (0, import_shared10.randomId)(),
3444
3709
  createdAt: /* @__PURE__ */ new Date(),
3445
3710
  actionExecutionId: event.actionExecutionId,
3446
3711
  actionName: event.actionName,
@@ -3452,7 +3717,7 @@ var CopilotResolver = class {
3452
3717
  event
3453
3718
  }, "Agent message event received");
3454
3719
  pushMessage({
3455
- id: (0, import_shared9.randomId)(),
3720
+ id: (0, import_shared10.randomId)(),
3456
3721
  status: new SuccessMessageStatus(),
3457
3722
  threadId: event.threadId,
3458
3723
  agentName: event.agentName,
@@ -3465,7 +3730,7 @@ var CopilotResolver = class {
3465
3730
  createdAt: /* @__PURE__ */ new Date()
3466
3731
  });
3467
3732
  outputMessages.push((0, import_class_transformer2.plainToInstance)(AgentStateMessage, {
3468
- id: (0, import_shared9.randomId)(),
3733
+ id: (0, import_shared10.randomId)(),
3469
3734
  threadId: event.threadId,
3470
3735
  agentName: event.agentName,
3471
3736
  nodeName: event.nodeName,
@@ -3754,6 +4019,7 @@ function copilotRuntimeNestEndpoint(options) {
3754
4019
  __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
3755
4020
  // Annotate the CommonJS export names for ESM import in node:
3756
4021
  0 && (module.exports = {
4022
+ AnthropicAdapter,
3757
4023
  CopilotRuntime,
3758
4024
  GoogleGenerativeAIAdapter,
3759
4025
  GroqAdapter,
@@ -3762,6 +4028,7 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
3762
4028
  MessageStreamInterruptedResponse,
3763
4029
  OpenAIAdapter,
3764
4030
  OpenAIAssistantAdapter,
4031
+ RemoteChain,
3765
4032
  UnifyAdapter,
3766
4033
  UnknownErrorResponse,
3767
4034
  buildSchema,