@copilotkit/runtime 0.0.0-mme-load-agent-state-20250117154700

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 (176) hide show
  1. package/.eslintrc.js +7 -0
  2. package/CHANGELOG.md +929 -0
  3. package/README.md +46 -0
  4. package/__snapshots__/schema/schema.graphql +306 -0
  5. package/dist/chunk-44O2JGUY.mjs +12 -0
  6. package/dist/chunk-44O2JGUY.mjs.map +1 -0
  7. package/dist/chunk-4BWLNVK4.mjs +80 -0
  8. package/dist/chunk-4BWLNVK4.mjs.map +1 -0
  9. package/dist/chunk-4QJA7OT2.mjs +3395 -0
  10. package/dist/chunk-4QJA7OT2.mjs.map +1 -0
  11. package/dist/chunk-4YJIXJLO.mjs +25 -0
  12. package/dist/chunk-4YJIXJLO.mjs.map +1 -0
  13. package/dist/chunk-67KK2GZ5.mjs +3765 -0
  14. package/dist/chunk-67KK2GZ5.mjs.map +1 -0
  15. package/dist/chunk-7BOVBWKI.mjs +25 -0
  16. package/dist/chunk-7BOVBWKI.mjs.map +1 -0
  17. package/dist/chunk-B5KHNAW5.mjs +25 -0
  18. package/dist/chunk-B5KHNAW5.mjs.map +1 -0
  19. package/dist/chunk-CLGKEUOA.mjs +1408 -0
  20. package/dist/chunk-CLGKEUOA.mjs.map +1 -0
  21. package/dist/chunk-D2WLFQS6.mjs +43 -0
  22. package/dist/chunk-D2WLFQS6.mjs.map +1 -0
  23. package/dist/chunk-DFOKBSIS.mjs +1 -0
  24. package/dist/chunk-DFOKBSIS.mjs.map +1 -0
  25. package/dist/chunk-DKLATJGV.mjs +25 -0
  26. package/dist/chunk-DKLATJGV.mjs.map +1 -0
  27. package/dist/chunk-FYONHPZL.mjs +3397 -0
  28. package/dist/chunk-FYONHPZL.mjs.map +1 -0
  29. package/dist/chunk-HNUNXFTW.mjs +129 -0
  30. package/dist/chunk-HNUNXFTW.mjs.map +1 -0
  31. package/dist/chunk-OKUXS4SE.mjs +25 -0
  32. package/dist/chunk-OKUXS4SE.mjs.map +1 -0
  33. package/dist/chunk-P4PPTGPJ.mjs +25 -0
  34. package/dist/chunk-P4PPTGPJ.mjs.map +1 -0
  35. package/dist/chunk-RFF5IIZJ.mjs +66 -0
  36. package/dist/chunk-RFF5IIZJ.mjs.map +1 -0
  37. package/dist/chunk-U3V2BCGI.mjs +152 -0
  38. package/dist/chunk-U3V2BCGI.mjs.map +1 -0
  39. package/dist/chunk-U7EKYV47.mjs +80 -0
  40. package/dist/chunk-U7EKYV47.mjs.map +1 -0
  41. package/dist/chunk-XXYYCH4X.mjs +80 -0
  42. package/dist/chunk-XXYYCH4X.mjs.map +1 -0
  43. package/dist/chunk-YT7A6V5T.mjs +1420 -0
  44. package/dist/chunk-YT7A6V5T.mjs.map +1 -0
  45. package/dist/copilot-runtime-36700e00.d.ts +196 -0
  46. package/dist/copilot-runtime-8c442d65.d.ts +209 -0
  47. package/dist/graphql/types/base/index.d.ts +6 -0
  48. package/dist/graphql/types/base/index.js +63 -0
  49. package/dist/graphql/types/base/index.js.map +1 -0
  50. package/dist/graphql/types/base/index.mjs +8 -0
  51. package/dist/graphql/types/base/index.mjs.map +1 -0
  52. package/dist/graphql/types/converted/index.d.ts +2 -0
  53. package/dist/graphql/types/converted/index.js +187 -0
  54. package/dist/graphql/types/converted/index.js.map +1 -0
  55. package/dist/graphql/types/converted/index.mjs +17 -0
  56. package/dist/graphql/types/converted/index.mjs.map +1 -0
  57. package/dist/groq-adapter-696b5d29.d.ts +281 -0
  58. package/dist/groq-adapter-7a82cd22.d.ts +301 -0
  59. package/dist/index-a7f37670.d.ts +103 -0
  60. package/dist/index-cc2b17be.d.ts +87 -0
  61. package/dist/index.d.ts +23 -0
  62. package/dist/index.js +5597 -0
  63. package/dist/index.js.map +1 -0
  64. package/dist/index.mjs +76 -0
  65. package/dist/index.mjs.map +1 -0
  66. package/dist/langserve-9125a12e.d.ts +176 -0
  67. package/dist/langserve-e308c437.d.ts +209 -0
  68. package/dist/lib/cloud/index.d.ts +6 -0
  69. package/dist/lib/cloud/index.js +18 -0
  70. package/dist/lib/cloud/index.js.map +1 -0
  71. package/dist/lib/cloud/index.mjs +1 -0
  72. package/dist/lib/cloud/index.mjs.map +1 -0
  73. package/dist/lib/index.d.ts +20 -0
  74. package/dist/lib/index.js +5256 -0
  75. package/dist/lib/index.js.map +1 -0
  76. package/dist/lib/index.mjs +58 -0
  77. package/dist/lib/index.mjs.map +1 -0
  78. package/dist/lib/integrations/index.d.ts +33 -0
  79. package/dist/lib/integrations/index.js +2488 -0
  80. package/dist/lib/integrations/index.js.map +1 -0
  81. package/dist/lib/integrations/index.mjs +34 -0
  82. package/dist/lib/integrations/index.mjs.map +1 -0
  83. package/dist/lib/integrations/nest/index.d.ts +14 -0
  84. package/dist/lib/integrations/nest/index.js +2397 -0
  85. package/dist/lib/integrations/nest/index.js.map +1 -0
  86. package/dist/lib/integrations/nest/index.mjs +13 -0
  87. package/dist/lib/integrations/nest/index.mjs.map +1 -0
  88. package/dist/lib/integrations/node-express/index.d.ts +14 -0
  89. package/dist/lib/integrations/node-express/index.js +2397 -0
  90. package/dist/lib/integrations/node-express/index.js.map +1 -0
  91. package/dist/lib/integrations/node-express/index.mjs +13 -0
  92. package/dist/lib/integrations/node-express/index.mjs.map +1 -0
  93. package/dist/lib/integrations/node-http/index.d.ts +14 -0
  94. package/dist/lib/integrations/node-http/index.js +2383 -0
  95. package/dist/lib/integrations/node-http/index.js.map +1 -0
  96. package/dist/lib/integrations/node-http/index.mjs +12 -0
  97. package/dist/lib/integrations/node-http/index.mjs.map +1 -0
  98. package/dist/service-adapters/index.d.ts +84 -0
  99. package/dist/service-adapters/index.js +1460 -0
  100. package/dist/service-adapters/index.js.map +1 -0
  101. package/dist/service-adapters/index.mjs +26 -0
  102. package/dist/service-adapters/index.mjs.map +1 -0
  103. package/dist/utils/index.d.ts +49 -0
  104. package/dist/utils/index.js +174 -0
  105. package/dist/utils/index.js.map +1 -0
  106. package/dist/utils/index.mjs +12 -0
  107. package/dist/utils/index.mjs.map +1 -0
  108. package/jest.config.js +5 -0
  109. package/package.json +85 -0
  110. package/scripts/generate-gql-schema.ts +13 -0
  111. package/src/agents/langgraph/event-source.ts +287 -0
  112. package/src/agents/langgraph/events.ts +338 -0
  113. package/src/graphql/inputs/action.input.ts +16 -0
  114. package/src/graphql/inputs/agent-session.input.ts +13 -0
  115. package/src/graphql/inputs/agent-state.input.ts +10 -0
  116. package/src/graphql/inputs/cloud-guardrails.input.ts +16 -0
  117. package/src/graphql/inputs/cloud.input.ts +8 -0
  118. package/src/graphql/inputs/context-property.input.ts +10 -0
  119. package/src/graphql/inputs/custom-property.input.ts +15 -0
  120. package/src/graphql/inputs/extensions.input.ts +21 -0
  121. package/src/graphql/inputs/forwarded-parameters.input.ts +22 -0
  122. package/src/graphql/inputs/frontend.input.ts +14 -0
  123. package/src/graphql/inputs/generate-copilot-response.input.ts +51 -0
  124. package/src/graphql/inputs/load-agent-state.input.ts +10 -0
  125. package/src/graphql/inputs/message.input.ts +92 -0
  126. package/src/graphql/resolvers/copilot.resolver.ts +561 -0
  127. package/src/graphql/resolvers/state.resolver.ts +23 -0
  128. package/src/graphql/types/agents-response.type.ts +19 -0
  129. package/src/graphql/types/base/index.ts +10 -0
  130. package/src/graphql/types/converted/index.ts +136 -0
  131. package/src/graphql/types/copilot-response.type.ts +117 -0
  132. package/src/graphql/types/enums.ts +37 -0
  133. package/src/graphql/types/extensions-response.type.ts +23 -0
  134. package/src/graphql/types/guardrails-result.type.ts +20 -0
  135. package/src/graphql/types/load-agent-state-response.type.ts +17 -0
  136. package/src/graphql/types/message-status.type.ts +40 -0
  137. package/src/graphql/types/response-status.type.ts +66 -0
  138. package/src/index.ts +4 -0
  139. package/src/lib/cloud/index.ts +4 -0
  140. package/src/lib/index.ts +8 -0
  141. package/src/lib/integrations/index.ts +6 -0
  142. package/src/lib/integrations/nest/index.ts +17 -0
  143. package/src/lib/integrations/nextjs/app-router.ts +40 -0
  144. package/src/lib/integrations/nextjs/pages-router.ts +49 -0
  145. package/src/lib/integrations/node-express/index.ts +17 -0
  146. package/src/lib/integrations/node-http/index.ts +34 -0
  147. package/src/lib/integrations/shared.ts +110 -0
  148. package/src/lib/logger.ts +28 -0
  149. package/src/lib/runtime/copilot-runtime.ts +571 -0
  150. package/src/lib/runtime/remote-action-constructors.ts +304 -0
  151. package/src/lib/runtime/remote-actions.ts +174 -0
  152. package/src/lib/runtime/remote-lg-action.ts +669 -0
  153. package/src/lib/telemetry-client.ts +52 -0
  154. package/src/service-adapters/anthropic/anthropic-adapter.ts +204 -0
  155. package/src/service-adapters/anthropic/utils.ts +144 -0
  156. package/src/service-adapters/conversion.ts +64 -0
  157. package/src/service-adapters/events.ts +424 -0
  158. package/src/service-adapters/experimental/empty/empty-adapter.ts +33 -0
  159. package/src/service-adapters/experimental/ollama/ollama-adapter.ts +79 -0
  160. package/src/service-adapters/google/google-genai-adapter.ts +39 -0
  161. package/src/service-adapters/groq/groq-adapter.ts +173 -0
  162. package/src/service-adapters/index.ts +16 -0
  163. package/src/service-adapters/langchain/langchain-adapter.ts +106 -0
  164. package/src/service-adapters/langchain/langserve.ts +87 -0
  165. package/src/service-adapters/langchain/types.ts +14 -0
  166. package/src/service-adapters/langchain/utils.ts +306 -0
  167. package/src/service-adapters/openai/openai-adapter.ts +211 -0
  168. package/src/service-adapters/openai/openai-assistant-adapter.ts +315 -0
  169. package/src/service-adapters/openai/utils.ts +161 -0
  170. package/src/service-adapters/service-adapter.ts +34 -0
  171. package/src/service-adapters/unify/unify-adapter.ts +144 -0
  172. package/src/utils/failed-response-status-reasons.ts +48 -0
  173. package/src/utils/index.ts +1 -0
  174. package/tsconfig.json +11 -0
  175. package/tsup.config.ts +16 -0
  176. package/typedoc.json +4 -0
@@ -0,0 +1,1460 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/service-adapters/index.ts
31
+ var service_adapters_exports = {};
32
+ __export(service_adapters_exports, {
33
+ AnthropicAdapter: () => AnthropicAdapter,
34
+ ExperimentalEmptyAdapter: () => ExperimentalEmptyAdapter,
35
+ ExperimentalOllamaAdapter: () => ExperimentalOllamaAdapter,
36
+ GoogleGenerativeAIAdapter: () => GoogleGenerativeAIAdapter,
37
+ GroqAdapter: () => GroqAdapter,
38
+ LangChainAdapter: () => LangChainAdapter,
39
+ OpenAIAdapter: () => OpenAIAdapter,
40
+ OpenAIAssistantAdapter: () => OpenAIAssistantAdapter,
41
+ RemoteChain: () => RemoteChain,
42
+ UnifyAdapter: () => UnifyAdapter
43
+ });
44
+ module.exports = __toCommonJS(service_adapters_exports);
45
+
46
+ // src/service-adapters/langchain/langserve.ts
47
+ var import_remote = require("langchain/runnables/remote");
48
+ var RemoteChain = class {
49
+ name;
50
+ description;
51
+ chainUrl;
52
+ parameters;
53
+ parameterType;
54
+ constructor(options) {
55
+ this.name = options.name;
56
+ this.description = options.description;
57
+ this.chainUrl = options.chainUrl;
58
+ this.parameters = options.parameters;
59
+ this.parameterType = options.parameterType || "multi";
60
+ }
61
+ async toAction() {
62
+ if (!this.parameters) {
63
+ await this.inferLangServeParameters();
64
+ }
65
+ return {
66
+ name: this.name,
67
+ description: this.description,
68
+ parameters: this.parameters,
69
+ handler: async (args) => {
70
+ const runnable = new import_remote.RemoteRunnable({
71
+ url: this.chainUrl
72
+ });
73
+ let input;
74
+ if (this.parameterType === "single") {
75
+ input = args[Object.keys(args)[0]];
76
+ } else {
77
+ input = args;
78
+ }
79
+ return await runnable.invoke(input);
80
+ }
81
+ };
82
+ }
83
+ async inferLangServeParameters() {
84
+ const supportedTypes = [
85
+ "string",
86
+ "number",
87
+ "boolean"
88
+ ];
89
+ let schemaUrl = this.chainUrl.replace(/\/+$/, "") + "/input_schema";
90
+ let schema = await fetch(schemaUrl).then((res) => res.json()).catch(() => {
91
+ throw new Error("Failed to fetch langserve schema at " + schemaUrl);
92
+ });
93
+ if (supportedTypes.includes(schema.type)) {
94
+ this.parameterType = "single";
95
+ this.parameters = [
96
+ {
97
+ name: "input",
98
+ type: schema.type,
99
+ description: "The input to the chain"
100
+ }
101
+ ];
102
+ } else if (schema.type === "object") {
103
+ this.parameterType = "multi";
104
+ this.parameters = Object.keys(schema.properties).map((key) => {
105
+ var _a;
106
+ let property = schema.properties[key];
107
+ if (!supportedTypes.includes(property.type)) {
108
+ throw new Error("Unsupported schema type");
109
+ }
110
+ return {
111
+ name: key,
112
+ type: property.type,
113
+ description: property.description || "",
114
+ required: ((_a = schema.required) == null ? void 0 : _a.includes(key)) || false
115
+ };
116
+ });
117
+ } else {
118
+ throw new Error("Unsupported schema type");
119
+ }
120
+ }
121
+ };
122
+ __name(RemoteChain, "RemoteChain");
123
+
124
+ // src/service-adapters/openai/openai-adapter.ts
125
+ var import_openai = __toESM(require("openai"));
126
+
127
+ // src/service-adapters/openai/utils.ts
128
+ function limitMessagesToTokenCount(messages, tools, model, maxTokens) {
129
+ maxTokens || (maxTokens = maxTokensForOpenAIModel(model));
130
+ const result = [];
131
+ const toolsNumTokens = countToolsTokens(model, tools);
132
+ if (toolsNumTokens > maxTokens) {
133
+ throw new Error(`Too many tokens in function definitions: ${toolsNumTokens} > ${maxTokens}`);
134
+ }
135
+ maxTokens -= toolsNumTokens;
136
+ for (const message of messages) {
137
+ if (message.role === "system") {
138
+ const numTokens = countMessageTokens(model, message);
139
+ maxTokens -= numTokens;
140
+ if (maxTokens < 0) {
141
+ throw new Error("Not enough tokens for system message.");
142
+ }
143
+ }
144
+ }
145
+ let cutoff = false;
146
+ const reversedMessages = [
147
+ ...messages
148
+ ].reverse();
149
+ for (const message of reversedMessages) {
150
+ if (message.role === "system") {
151
+ result.unshift(message);
152
+ continue;
153
+ } else if (cutoff) {
154
+ continue;
155
+ }
156
+ let numTokens = countMessageTokens(model, message);
157
+ if (maxTokens < numTokens) {
158
+ cutoff = true;
159
+ continue;
160
+ }
161
+ result.unshift(message);
162
+ maxTokens -= numTokens;
163
+ }
164
+ return result;
165
+ }
166
+ __name(limitMessagesToTokenCount, "limitMessagesToTokenCount");
167
+ function maxTokensForOpenAIModel(model) {
168
+ return maxTokensByModel[model] || DEFAULT_MAX_TOKENS;
169
+ }
170
+ __name(maxTokensForOpenAIModel, "maxTokensForOpenAIModel");
171
+ var DEFAULT_MAX_TOKENS = 128e3;
172
+ var maxTokensByModel = {
173
+ // GPT-4
174
+ "gpt-4o": 128e3,
175
+ "gpt-4o-2024-05-13": 128e3,
176
+ "gpt-4-turbo": 128e3,
177
+ "gpt-4-turbo-2024-04-09": 128e3,
178
+ "gpt-4-0125-preview": 128e3,
179
+ "gpt-4-turbo-preview": 128e3,
180
+ "gpt-4-1106-preview": 128e3,
181
+ "gpt-4-vision-preview": 128e3,
182
+ "gpt-4-1106-vision-preview": 128e3,
183
+ "gpt-4-32k": 32768,
184
+ "gpt-4-32k-0613": 32768,
185
+ "gpt-4-32k-0314": 32768,
186
+ "gpt-4": 8192,
187
+ "gpt-4-0613": 8192,
188
+ "gpt-4-0314": 8192,
189
+ // GPT-3.5
190
+ "gpt-3.5-turbo-0125": 16385,
191
+ "gpt-3.5-turbo": 16385,
192
+ "gpt-3.5-turbo-1106": 16385,
193
+ "gpt-3.5-turbo-instruct": 4096,
194
+ "gpt-3.5-turbo-16k": 16385,
195
+ "gpt-3.5-turbo-0613": 4096,
196
+ "gpt-3.5-turbo-16k-0613": 16385,
197
+ "gpt-3.5-turbo-0301": 4097
198
+ };
199
+ function countToolsTokens(model, tools) {
200
+ if (tools.length === 0) {
201
+ return 0;
202
+ }
203
+ const json = JSON.stringify(tools);
204
+ return countTokens(model, json);
205
+ }
206
+ __name(countToolsTokens, "countToolsTokens");
207
+ function countMessageTokens(model, message) {
208
+ return countTokens(model, message.content || "");
209
+ }
210
+ __name(countMessageTokens, "countMessageTokens");
211
+ function countTokens(model, text) {
212
+ return text.length / 3;
213
+ }
214
+ __name(countTokens, "countTokens");
215
+ function convertActionInputToOpenAITool(action) {
216
+ return {
217
+ type: "function",
218
+ function: {
219
+ name: action.name,
220
+ description: action.description,
221
+ parameters: JSON.parse(action.jsonSchema)
222
+ }
223
+ };
224
+ }
225
+ __name(convertActionInputToOpenAITool, "convertActionInputToOpenAITool");
226
+ function convertMessageToOpenAIMessage(message) {
227
+ if (message.isTextMessage()) {
228
+ return {
229
+ role: message.role,
230
+ content: message.content
231
+ };
232
+ } else if (message.isActionExecutionMessage()) {
233
+ return {
234
+ role: "assistant",
235
+ tool_calls: [
236
+ {
237
+ id: message.id,
238
+ type: "function",
239
+ function: {
240
+ name: message.name,
241
+ arguments: JSON.stringify(message.arguments)
242
+ }
243
+ }
244
+ ]
245
+ };
246
+ } else if (message.isResultMessage()) {
247
+ return {
248
+ role: "tool",
249
+ content: message.result,
250
+ tool_call_id: message.actionExecutionId
251
+ };
252
+ }
253
+ }
254
+ __name(convertMessageToOpenAIMessage, "convertMessageToOpenAIMessage");
255
+ function convertSystemMessageToAssistantAPI(message) {
256
+ return {
257
+ ...message,
258
+ ...message.role === "system" && {
259
+ role: "assistant",
260
+ content: "THE FOLLOWING MESSAGE IS A SYSTEM MESSAGE: " + message.content
261
+ }
262
+ };
263
+ }
264
+ __name(convertSystemMessageToAssistantAPI, "convertSystemMessageToAssistantAPI");
265
+
266
+ // src/service-adapters/openai/openai-adapter.ts
267
+ var import_shared = require("@copilotkit/shared");
268
+ var DEFAULT_MODEL = "gpt-4o";
269
+ var OpenAIAdapter = class {
270
+ model = DEFAULT_MODEL;
271
+ disableParallelToolCalls = false;
272
+ _openai;
273
+ get openai() {
274
+ return this._openai;
275
+ }
276
+ constructor(params) {
277
+ this._openai = (params == null ? void 0 : params.openai) || new import_openai.default({});
278
+ if (params == null ? void 0 : params.model) {
279
+ this.model = params.model;
280
+ }
281
+ this.disableParallelToolCalls = (params == null ? void 0 : params.disableParallelToolCalls) || false;
282
+ }
283
+ async process(request) {
284
+ const { threadId: threadIdFromRequest, model = this.model, messages, actions, eventSource, forwardedParameters } = request;
285
+ const tools = actions.map(convertActionInputToOpenAITool);
286
+ const threadId = threadIdFromRequest ?? (0, import_shared.randomUUID)();
287
+ let openaiMessages = messages.map(convertMessageToOpenAIMessage);
288
+ openaiMessages = limitMessagesToTokenCount(openaiMessages, tools, model);
289
+ let toolChoice = forwardedParameters == null ? void 0 : forwardedParameters.toolChoice;
290
+ if ((forwardedParameters == null ? void 0 : forwardedParameters.toolChoice) === "function") {
291
+ toolChoice = {
292
+ type: "function",
293
+ function: {
294
+ name: forwardedParameters.toolChoiceFunctionName
295
+ }
296
+ };
297
+ }
298
+ const stream = this.openai.beta.chat.completions.stream({
299
+ model,
300
+ stream: true,
301
+ messages: openaiMessages,
302
+ ...tools.length > 0 && {
303
+ tools
304
+ },
305
+ ...(forwardedParameters == null ? void 0 : forwardedParameters.maxTokens) && {
306
+ max_tokens: forwardedParameters.maxTokens
307
+ },
308
+ ...(forwardedParameters == null ? void 0 : forwardedParameters.stop) && {
309
+ stop: forwardedParameters.stop
310
+ },
311
+ ...toolChoice && {
312
+ tool_choice: toolChoice
313
+ },
314
+ ...this.disableParallelToolCalls && {
315
+ parallel_tool_calls: false
316
+ },
317
+ ...(forwardedParameters == null ? void 0 : forwardedParameters.temperature) && {
318
+ temperature: forwardedParameters.temperature
319
+ }
320
+ });
321
+ eventSource.stream(async (eventStream$) => {
322
+ var _a, _b;
323
+ let mode = null;
324
+ let currentMessageId;
325
+ let currentToolCallId;
326
+ for await (const chunk of stream) {
327
+ if (chunk.choices.length === 0) {
328
+ continue;
329
+ }
330
+ const toolCall = (_a = chunk.choices[0].delta.tool_calls) == null ? void 0 : _a[0];
331
+ const content = chunk.choices[0].delta.content;
332
+ if (mode === "message" && (toolCall == null ? void 0 : toolCall.id)) {
333
+ mode = null;
334
+ eventStream$.sendTextMessageEnd({
335
+ messageId: currentMessageId
336
+ });
337
+ } else if (mode === "function" && (toolCall === void 0 || (toolCall == null ? void 0 : toolCall.id))) {
338
+ mode = null;
339
+ eventStream$.sendActionExecutionEnd({
340
+ actionExecutionId: currentToolCallId
341
+ });
342
+ }
343
+ if (mode === null) {
344
+ if (toolCall == null ? void 0 : toolCall.id) {
345
+ mode = "function";
346
+ currentToolCallId = toolCall.id;
347
+ eventStream$.sendActionExecutionStart({
348
+ actionExecutionId: currentToolCallId,
349
+ parentMessageId: chunk.id,
350
+ actionName: toolCall.function.name
351
+ });
352
+ } else if (content) {
353
+ mode = "message";
354
+ currentMessageId = chunk.id;
355
+ eventStream$.sendTextMessageStart({
356
+ messageId: currentMessageId
357
+ });
358
+ }
359
+ }
360
+ if (mode === "message" && content) {
361
+ eventStream$.sendTextMessageContent({
362
+ messageId: currentMessageId,
363
+ content
364
+ });
365
+ } else if (mode === "function" && ((_b = toolCall == null ? void 0 : toolCall.function) == null ? void 0 : _b.arguments)) {
366
+ eventStream$.sendActionExecutionArgs({
367
+ actionExecutionId: currentToolCallId,
368
+ args: toolCall.function.arguments
369
+ });
370
+ }
371
+ }
372
+ if (mode === "message") {
373
+ eventStream$.sendTextMessageEnd({
374
+ messageId: currentMessageId
375
+ });
376
+ } else if (mode === "function") {
377
+ eventStream$.sendActionExecutionEnd({
378
+ actionExecutionId: currentToolCallId
379
+ });
380
+ }
381
+ eventStream$.complete();
382
+ });
383
+ return {
384
+ threadId
385
+ };
386
+ }
387
+ };
388
+ __name(OpenAIAdapter, "OpenAIAdapter");
389
+
390
+ // src/service-adapters/langchain/utils.ts
391
+ var import_messages = require("@langchain/core/messages");
392
+ var import_tools = require("@langchain/core/tools");
393
+ var import_shared2 = require("@copilotkit/shared");
394
+ function convertMessageToLangChainMessage(message) {
395
+ if (message.isTextMessage()) {
396
+ if (message.role == "user") {
397
+ return new import_messages.HumanMessage(message.content);
398
+ } else if (message.role == "assistant") {
399
+ return new import_messages.AIMessage(message.content);
400
+ } else if (message.role === "system") {
401
+ return new import_messages.SystemMessage(message.content);
402
+ }
403
+ } else if (message.isActionExecutionMessage()) {
404
+ return new import_messages.AIMessage({
405
+ content: "",
406
+ tool_calls: [
407
+ {
408
+ id: message.id,
409
+ args: message.arguments,
410
+ name: message.name
411
+ }
412
+ ]
413
+ });
414
+ } else if (message.isResultMessage()) {
415
+ return new import_messages.ToolMessage({
416
+ content: message.result,
417
+ tool_call_id: message.actionExecutionId
418
+ });
419
+ }
420
+ }
421
+ __name(convertMessageToLangChainMessage, "convertMessageToLangChainMessage");
422
+ function convertActionInputToLangChainTool(actionInput) {
423
+ return new import_tools.DynamicStructuredTool({
424
+ name: actionInput.name,
425
+ description: actionInput.description,
426
+ schema: (0, import_shared2.convertJsonSchemaToZodSchema)(JSON.parse(actionInput.jsonSchema), true),
427
+ func: async () => {
428
+ return "";
429
+ }
430
+ });
431
+ }
432
+ __name(convertActionInputToLangChainTool, "convertActionInputToLangChainTool");
433
+ function isAIMessage(message) {
434
+ return Object.prototype.toString.call(message) === "[object AIMessage]";
435
+ }
436
+ __name(isAIMessage, "isAIMessage");
437
+ function isAIMessageChunk(message) {
438
+ return Object.prototype.toString.call(message) === "[object AIMessageChunk]";
439
+ }
440
+ __name(isAIMessageChunk, "isAIMessageChunk");
441
+ function isBaseMessageChunk(message) {
442
+ return Object.prototype.toString.call(message) === "[object BaseMessageChunk]";
443
+ }
444
+ __name(isBaseMessageChunk, "isBaseMessageChunk");
445
+ function maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution) {
446
+ if (actionExecution) {
447
+ eventStream$.sendActionExecutionResult({
448
+ actionExecutionId: actionExecution.id,
449
+ actionName: actionExecution.name,
450
+ result: "Sending a message"
451
+ });
452
+ }
453
+ }
454
+ __name(maybeSendActionExecutionResultIsMessage, "maybeSendActionExecutionResultIsMessage");
455
+ async function streamLangChainResponse({ result, eventStream$, actionExecution }) {
456
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
457
+ if (typeof result === "string") {
458
+ if (!actionExecution) {
459
+ eventStream$.sendTextMessage((0, import_shared2.randomId)(), result);
460
+ } else {
461
+ eventStream$.sendActionExecutionResult({
462
+ actionExecutionId: actionExecution.id,
463
+ actionName: actionExecution.name,
464
+ result
465
+ });
466
+ }
467
+ } else if (isAIMessage(result)) {
468
+ maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
469
+ if (result.content) {
470
+ eventStream$.sendTextMessage((0, import_shared2.randomId)(), result.content);
471
+ }
472
+ for (const toolCall of result.tool_calls) {
473
+ eventStream$.sendActionExecution({
474
+ actionExecutionId: toolCall.id || (0, import_shared2.randomId)(),
475
+ actionName: toolCall.name,
476
+ args: JSON.stringify(toolCall.args)
477
+ });
478
+ }
479
+ } else if (isBaseMessageChunk(result)) {
480
+ maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
481
+ if ((_a = result.lc_kwargs) == null ? void 0 : _a.content) {
482
+ eventStream$.sendTextMessage((0, import_shared2.randomId)(), result.content);
483
+ }
484
+ if ((_b = result.lc_kwargs) == null ? void 0 : _b.tool_calls) {
485
+ for (const toolCall of (_c = result.lc_kwargs) == null ? void 0 : _c.tool_calls) {
486
+ eventStream$.sendActionExecution({
487
+ actionExecutionId: toolCall.id || (0, import_shared2.randomId)(),
488
+ actionName: toolCall.name,
489
+ args: JSON.stringify(toolCall.args)
490
+ });
491
+ }
492
+ }
493
+ } else if (result && "getReader" in result) {
494
+ maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
495
+ let reader = result.getReader();
496
+ let mode = null;
497
+ let currentMessageId;
498
+ const toolCallDetails = {
499
+ name: null,
500
+ id: null,
501
+ index: null,
502
+ prevIndex: null
503
+ };
504
+ while (true) {
505
+ try {
506
+ const { done, value } = await reader.read();
507
+ let toolCallName = void 0;
508
+ let toolCallId = void 0;
509
+ let toolCallArgs = void 0;
510
+ let hasToolCall = false;
511
+ let content = "";
512
+ if (value && value.content) {
513
+ content = Array.isArray(value.content) ? ((_d = value.content[0]) == null ? void 0 : _d.text) ?? "" : value.content;
514
+ }
515
+ if (isAIMessageChunk(value)) {
516
+ let chunk = (_e = value.tool_call_chunks) == null ? void 0 : _e[0];
517
+ toolCallArgs = chunk == null ? void 0 : chunk.args;
518
+ hasToolCall = chunk != void 0;
519
+ if (chunk == null ? void 0 : chunk.name)
520
+ toolCallDetails.name = chunk.name;
521
+ if ((chunk == null ? void 0 : chunk.index) != null) {
522
+ toolCallDetails.index = chunk.index;
523
+ if (toolCallDetails.prevIndex == null)
524
+ toolCallDetails.prevIndex = chunk.index;
525
+ }
526
+ if (chunk == null ? void 0 : chunk.id)
527
+ toolCallDetails.id = chunk.index != null ? `${chunk.id}-idx-${chunk.index}` : chunk.id;
528
+ toolCallName = toolCallDetails.name;
529
+ toolCallId = toolCallDetails.id;
530
+ } else if (isBaseMessageChunk(value)) {
531
+ let chunk = (_g = (_f = value.additional_kwargs) == null ? void 0 : _f.tool_calls) == null ? void 0 : _g[0];
532
+ toolCallName = (_h = chunk == null ? void 0 : chunk.function) == null ? void 0 : _h.name;
533
+ toolCallId = chunk == null ? void 0 : chunk.id;
534
+ toolCallArgs = (_i = chunk == null ? void 0 : chunk.function) == null ? void 0 : _i.arguments;
535
+ hasToolCall = (chunk == null ? void 0 : chunk.function) != void 0;
536
+ }
537
+ if (mode === "message" && (toolCallId || done)) {
538
+ mode = null;
539
+ eventStream$.sendTextMessageEnd({
540
+ messageId: currentMessageId
541
+ });
542
+ } else if (mode === "function" && (!hasToolCall || done)) {
543
+ mode = null;
544
+ eventStream$.sendActionExecutionEnd({
545
+ actionExecutionId: toolCallId
546
+ });
547
+ }
548
+ if (done) {
549
+ break;
550
+ }
551
+ if (mode === null) {
552
+ if (hasToolCall && toolCallId && toolCallName) {
553
+ mode = "function";
554
+ eventStream$.sendActionExecutionStart({
555
+ actionExecutionId: toolCallId,
556
+ actionName: toolCallName,
557
+ parentMessageId: (_j = value.lc_kwargs) == null ? void 0 : _j.id
558
+ });
559
+ } else if (content) {
560
+ mode = "message";
561
+ currentMessageId = ((_k = value.lc_kwargs) == null ? void 0 : _k.id) || (0, import_shared2.randomId)();
562
+ eventStream$.sendTextMessageStart({
563
+ messageId: currentMessageId
564
+ });
565
+ }
566
+ }
567
+ if (mode === "message" && content) {
568
+ eventStream$.sendTextMessageContent({
569
+ messageId: currentMessageId,
570
+ content
571
+ });
572
+ } else if (mode === "function" && toolCallArgs) {
573
+ if (toolCallDetails.index !== toolCallDetails.prevIndex) {
574
+ eventStream$.sendActionExecutionEnd({
575
+ actionExecutionId: toolCallId
576
+ });
577
+ eventStream$.sendActionExecutionStart({
578
+ actionExecutionId: toolCallId,
579
+ actionName: toolCallName,
580
+ parentMessageId: (_l = value.lc_kwargs) == null ? void 0 : _l.id
581
+ });
582
+ toolCallDetails.prevIndex = toolCallDetails.index;
583
+ }
584
+ eventStream$.sendActionExecutionArgs({
585
+ actionExecutionId: toolCallId,
586
+ args: toolCallArgs
587
+ });
588
+ }
589
+ } catch (error) {
590
+ console.error("Error reading from stream", error);
591
+ break;
592
+ }
593
+ }
594
+ } else if (actionExecution) {
595
+ eventStream$.sendActionExecutionResult({
596
+ actionExecutionId: actionExecution.id,
597
+ actionName: actionExecution.name,
598
+ result: encodeResult(result)
599
+ });
600
+ } else {
601
+ throw new Error("Invalid return type from LangChain function.");
602
+ }
603
+ eventStream$.complete();
604
+ }
605
+ __name(streamLangChainResponse, "streamLangChainResponse");
606
+ function encodeResult(result) {
607
+ if (result === void 0) {
608
+ return "";
609
+ } else if (typeof result === "string") {
610
+ return result;
611
+ } else {
612
+ return JSON.stringify(result);
613
+ }
614
+ }
615
+ __name(encodeResult, "encodeResult");
616
+
617
+ // src/service-adapters/langchain/langchain-adapter.ts
618
+ var import_shared3 = require("@copilotkit/shared");
619
+ var import_promises = require("@langchain/core/callbacks/promises");
620
+ var LangChainAdapter = class {
621
+ options;
622
+ /**
623
+ * To use LangChain as a backend, provide a handler function to the adapter with your custom LangChain logic.
624
+ */
625
+ constructor(options) {
626
+ this.options = options;
627
+ }
628
+ async process(request) {
629
+ try {
630
+ const { eventSource, model, actions, messages, runId, threadId: threadIdFromRequest } = request;
631
+ const threadId = threadIdFromRequest ?? (0, import_shared3.randomUUID)();
632
+ const result = await this.options.chainFn({
633
+ messages: messages.map(convertMessageToLangChainMessage),
634
+ tools: actions.map(convertActionInputToLangChainTool),
635
+ model,
636
+ threadId,
637
+ runId
638
+ });
639
+ eventSource.stream(async (eventStream$) => {
640
+ await streamLangChainResponse({
641
+ result,
642
+ eventStream$
643
+ });
644
+ });
645
+ return {
646
+ threadId
647
+ };
648
+ } finally {
649
+ await (0, import_promises.awaitAllCallbacks)();
650
+ }
651
+ }
652
+ };
653
+ __name(LangChainAdapter, "LangChainAdapter");
654
+
655
+ // src/service-adapters/google/google-genai-adapter.ts
656
+ var import_google_gauth = require("@langchain/google-gauth");
657
+ var GoogleGenerativeAIAdapter = class extends LangChainAdapter {
658
+ constructor(options) {
659
+ super({
660
+ chainFn: async ({ messages, tools, threadId }) => {
661
+ const model = new import_google_gauth.ChatGoogle({
662
+ modelName: (options == null ? void 0 : options.model) ?? "gemini-1.5-pro",
663
+ apiVersion: "v1beta"
664
+ }).bindTools(tools);
665
+ return model.stream(messages, {
666
+ metadata: {
667
+ conversation_id: threadId
668
+ }
669
+ });
670
+ }
671
+ });
672
+ }
673
+ };
674
+ __name(GoogleGenerativeAIAdapter, "GoogleGenerativeAIAdapter");
675
+
676
+ // src/service-adapters/openai/openai-assistant-adapter.ts
677
+ var import_openai2 = __toESM(require("openai"));
678
+ var OpenAIAssistantAdapter = class {
679
+ openai;
680
+ codeInterpreterEnabled;
681
+ assistantId;
682
+ fileSearchEnabled;
683
+ disableParallelToolCalls;
684
+ constructor(params) {
685
+ this.openai = params.openai || new import_openai2.default({});
686
+ this.codeInterpreterEnabled = params.codeInterpreterEnabled === false || true;
687
+ this.fileSearchEnabled = params.fileSearchEnabled === false || true;
688
+ this.assistantId = params.assistantId;
689
+ this.disableParallelToolCalls = (params == null ? void 0 : params.disableParallelToolCalls) || false;
690
+ }
691
+ async process(request) {
692
+ var _a, _b;
693
+ const { messages, actions, eventSource, runId, forwardedParameters } = request;
694
+ let threadId = (_b = (_a = request.extensions) == null ? void 0 : _a.openaiAssistantAPI) == null ? void 0 : _b.threadId;
695
+ if (!threadId) {
696
+ threadId = (await this.openai.beta.threads.create()).id;
697
+ }
698
+ const lastMessage = messages.at(-1);
699
+ let nextRunId = void 0;
700
+ if (lastMessage.isResultMessage() && runId) {
701
+ nextRunId = await this.submitToolOutputs(threadId, runId, messages, eventSource);
702
+ } else if (lastMessage.isTextMessage()) {
703
+ nextRunId = await this.submitUserMessage(threadId, messages, actions, eventSource, forwardedParameters);
704
+ } else {
705
+ throw new Error("No actionable message found in the messages");
706
+ }
707
+ return {
708
+ runId: nextRunId,
709
+ threadId,
710
+ extensions: {
711
+ ...request.extensions,
712
+ openaiAssistantAPI: {
713
+ threadId,
714
+ runId: nextRunId
715
+ }
716
+ }
717
+ };
718
+ }
719
+ async submitToolOutputs(threadId, runId, messages, eventSource) {
720
+ let run = await this.openai.beta.threads.runs.retrieve(threadId, runId);
721
+ if (!run.required_action) {
722
+ throw new Error("No tool outputs required");
723
+ }
724
+ const toolCallsIds = run.required_action.submit_tool_outputs.tool_calls.map((toolCall) => toolCall.id);
725
+ const resultMessages = messages.filter((message) => message.isResultMessage() && toolCallsIds.includes(message.actionExecutionId));
726
+ if (toolCallsIds.length != resultMessages.length) {
727
+ throw new Error("Number of function results does not match the number of tool calls");
728
+ }
729
+ const toolOutputs = resultMessages.map((message) => {
730
+ return {
731
+ tool_call_id: message.actionExecutionId,
732
+ output: message.result
733
+ };
734
+ });
735
+ const stream = this.openai.beta.threads.runs.submitToolOutputsStream(threadId, runId, {
736
+ tool_outputs: toolOutputs,
737
+ ...this.disableParallelToolCalls && {
738
+ parallel_tool_calls: false
739
+ }
740
+ });
741
+ await this.streamResponse(stream, eventSource);
742
+ return runId;
743
+ }
744
+ async submitUserMessage(threadId, messages, actions, eventSource, forwardedParameters) {
745
+ messages = [
746
+ ...messages
747
+ ];
748
+ const instructionsMessage = messages.shift();
749
+ const instructions = instructionsMessage.isTextMessage() ? instructionsMessage.content : "";
750
+ const userMessage = messages.map(convertMessageToOpenAIMessage).map(convertSystemMessageToAssistantAPI).at(-1);
751
+ if (userMessage.role !== "user") {
752
+ throw new Error("No user message found");
753
+ }
754
+ await this.openai.beta.threads.messages.create(threadId, {
755
+ role: "user",
756
+ content: userMessage.content
757
+ });
758
+ const openaiTools = actions.map(convertActionInputToOpenAITool);
759
+ const tools = [
760
+ ...openaiTools,
761
+ ...this.codeInterpreterEnabled ? [
762
+ {
763
+ type: "code_interpreter"
764
+ }
765
+ ] : [],
766
+ ...this.fileSearchEnabled ? [
767
+ {
768
+ type: "file_search"
769
+ }
770
+ ] : []
771
+ ];
772
+ let stream = this.openai.beta.threads.runs.stream(threadId, {
773
+ assistant_id: this.assistantId,
774
+ instructions,
775
+ tools,
776
+ ...(forwardedParameters == null ? void 0 : forwardedParameters.maxTokens) && {
777
+ max_completion_tokens: forwardedParameters.maxTokens
778
+ },
779
+ ...this.disableParallelToolCalls && {
780
+ parallel_tool_calls: false
781
+ }
782
+ });
783
+ await this.streamResponse(stream, eventSource);
784
+ return getRunIdFromStream(stream);
785
+ }
786
+ async streamResponse(stream, eventSource) {
787
+ eventSource.stream(async (eventStream$) => {
788
+ var _a, _b, _c, _d, _e, _f;
789
+ let inFunctionCall = false;
790
+ let currentMessageId;
791
+ let currentToolCallId;
792
+ for await (const chunk of stream) {
793
+ switch (chunk.event) {
794
+ case "thread.message.created":
795
+ if (inFunctionCall) {
796
+ eventStream$.sendActionExecutionEnd({
797
+ actionExecutionId: currentToolCallId
798
+ });
799
+ }
800
+ currentMessageId = chunk.data.id;
801
+ eventStream$.sendTextMessageStart({
802
+ messageId: currentMessageId
803
+ });
804
+ break;
805
+ case "thread.message.delta":
806
+ if (((_a = chunk.data.delta.content) == null ? void 0 : _a[0].type) === "text") {
807
+ eventStream$.sendTextMessageContent({
808
+ messageId: currentMessageId,
809
+ content: (_b = chunk.data.delta.content) == null ? void 0 : _b[0].text.value
810
+ });
811
+ }
812
+ break;
813
+ case "thread.message.completed":
814
+ eventStream$.sendTextMessageEnd({
815
+ messageId: currentMessageId
816
+ });
817
+ break;
818
+ case "thread.run.step.delta":
819
+ let toolCallId;
820
+ let toolCallName;
821
+ let toolCallArgs;
822
+ if (chunk.data.delta.step_details.type === "tool_calls" && ((_c = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _c[0].type) === "function") {
823
+ toolCallId = (_d = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _d[0].id;
824
+ toolCallName = (_e = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _e[0].function.name;
825
+ toolCallArgs = (_f = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _f[0].function.arguments;
826
+ }
827
+ if (toolCallName && toolCallId) {
828
+ if (inFunctionCall) {
829
+ eventStream$.sendActionExecutionEnd({
830
+ actionExecutionId: currentToolCallId
831
+ });
832
+ }
833
+ inFunctionCall = true;
834
+ currentToolCallId = toolCallId;
835
+ eventStream$.sendActionExecutionStart({
836
+ actionExecutionId: currentToolCallId,
837
+ parentMessageId: chunk.data.id,
838
+ actionName: toolCallName
839
+ });
840
+ } else if (toolCallArgs) {
841
+ eventStream$.sendActionExecutionArgs({
842
+ actionExecutionId: currentToolCallId,
843
+ args: toolCallArgs
844
+ });
845
+ }
846
+ break;
847
+ }
848
+ }
849
+ if (inFunctionCall) {
850
+ eventStream$.sendActionExecutionEnd({
851
+ actionExecutionId: currentToolCallId
852
+ });
853
+ }
854
+ eventStream$.complete();
855
+ });
856
+ }
857
+ };
858
+ __name(OpenAIAssistantAdapter, "OpenAIAssistantAdapter");
859
+ function getRunIdFromStream(stream) {
860
+ return new Promise((resolve, reject) => {
861
+ let runIdGetter = /* @__PURE__ */ __name((event) => {
862
+ if (event.event === "thread.run.created") {
863
+ const runId = event.data.id;
864
+ stream.off("event", runIdGetter);
865
+ resolve(runId);
866
+ }
867
+ }, "runIdGetter");
868
+ stream.on("event", runIdGetter);
869
+ });
870
+ }
871
+ __name(getRunIdFromStream, "getRunIdFromStream");
872
+
873
+ // src/service-adapters/unify/unify-adapter.ts
874
+ var import_openai3 = __toESM(require("openai"));
875
+ var import_shared4 = require("@copilotkit/shared");
876
+ var UnifyAdapter = class {
877
+ apiKey;
878
+ model;
879
+ start;
880
+ constructor(options) {
881
+ if (options == null ? void 0 : options.apiKey) {
882
+ this.apiKey = options.apiKey;
883
+ } else {
884
+ this.apiKey = "UNIFY_API_KEY";
885
+ }
886
+ this.model = options == null ? void 0 : options.model;
887
+ this.start = true;
888
+ }
889
+ async process(request) {
890
+ const tools = request.actions.map(convertActionInputToOpenAITool);
891
+ const openai = new import_openai3.default({
892
+ apiKey: this.apiKey,
893
+ baseURL: "https://api.unify.ai/v0/"
894
+ });
895
+ const forwardedParameters = request.forwardedParameters;
896
+ const messages = request.messages.map(convertMessageToOpenAIMessage);
897
+ const stream = await openai.chat.completions.create({
898
+ model: this.model,
899
+ messages,
900
+ stream: true,
901
+ ...tools.length > 0 && {
902
+ tools
903
+ },
904
+ ...(forwardedParameters == null ? void 0 : forwardedParameters.temperature) && {
905
+ temperature: forwardedParameters.temperature
906
+ }
907
+ });
908
+ let model = null;
909
+ let currentMessageId;
910
+ let currentToolCallId;
911
+ request.eventSource.stream(async (eventStream$) => {
912
+ var _a, _b;
913
+ let mode = null;
914
+ for await (const chunk of stream) {
915
+ if (this.start) {
916
+ model = chunk.model;
917
+ currentMessageId = (0, import_shared4.randomId)();
918
+ eventStream$.sendTextMessageStart({
919
+ messageId: currentMessageId
920
+ });
921
+ eventStream$.sendTextMessageContent({
922
+ messageId: currentMessageId,
923
+ content: `Model used: ${model}
924
+ `
925
+ });
926
+ eventStream$.sendTextMessageEnd({
927
+ messageId: currentMessageId
928
+ });
929
+ this.start = false;
930
+ }
931
+ const toolCall = (_a = chunk.choices[0].delta.tool_calls) == null ? void 0 : _a[0];
932
+ const content = chunk.choices[0].delta.content;
933
+ if (mode === "message" && (toolCall == null ? void 0 : toolCall.id)) {
934
+ mode = null;
935
+ eventStream$.sendTextMessageEnd({
936
+ messageId: currentMessageId
937
+ });
938
+ } else if (mode === "function" && (toolCall === void 0 || (toolCall == null ? void 0 : toolCall.id))) {
939
+ mode = null;
940
+ eventStream$.sendActionExecutionEnd({
941
+ actionExecutionId: currentToolCallId
942
+ });
943
+ }
944
+ if (mode === null) {
945
+ if (toolCall == null ? void 0 : toolCall.id) {
946
+ mode = "function";
947
+ currentToolCallId = toolCall.id;
948
+ eventStream$.sendActionExecutionStart({
949
+ actionExecutionId: currentToolCallId,
950
+ actionName: toolCall.function.name
951
+ });
952
+ } else if (content) {
953
+ mode = "message";
954
+ currentMessageId = chunk.id;
955
+ eventStream$.sendTextMessageStart({
956
+ messageId: currentMessageId
957
+ });
958
+ }
959
+ }
960
+ if (mode === "message" && content) {
961
+ eventStream$.sendTextMessageContent({
962
+ messageId: currentMessageId,
963
+ content
964
+ });
965
+ } else if (mode === "function" && ((_b = toolCall == null ? void 0 : toolCall.function) == null ? void 0 : _b.arguments)) {
966
+ eventStream$.sendActionExecutionArgs({
967
+ actionExecutionId: currentToolCallId,
968
+ args: toolCall.function.arguments
969
+ });
970
+ }
971
+ }
972
+ if (mode === "message") {
973
+ eventStream$.sendTextMessageEnd({
974
+ messageId: currentMessageId
975
+ });
976
+ } else if (mode === "function") {
977
+ eventStream$.sendActionExecutionEnd({
978
+ actionExecutionId: currentToolCallId
979
+ });
980
+ }
981
+ eventStream$.complete();
982
+ });
983
+ return {
984
+ threadId: request.threadId || (0, import_shared4.randomUUID)()
985
+ };
986
+ }
987
+ };
988
+ __name(UnifyAdapter, "UnifyAdapter");
989
+
990
+ // src/service-adapters/groq/groq-adapter.ts
991
+ var import_groq_sdk = require("groq-sdk");
992
+ var import_shared5 = require("@copilotkit/shared");
993
+ var DEFAULT_MODEL2 = "llama3-groq-70b-8192-tool-use-preview";
994
+ var GroqAdapter = class {
995
+ model = DEFAULT_MODEL2;
996
+ disableParallelToolCalls = false;
997
+ _groq;
998
+ get groq() {
999
+ return this._groq;
1000
+ }
1001
+ constructor(params) {
1002
+ this._groq = (params == null ? void 0 : params.groq) || new import_groq_sdk.Groq({});
1003
+ if (params == null ? void 0 : params.model) {
1004
+ this.model = params.model;
1005
+ }
1006
+ this.disableParallelToolCalls = (params == null ? void 0 : params.disableParallelToolCalls) || false;
1007
+ }
1008
+ async process(request) {
1009
+ const { threadId, model = this.model, messages, actions, eventSource, forwardedParameters } = request;
1010
+ const tools = actions.map(convertActionInputToOpenAITool);
1011
+ let openaiMessages = messages.map(convertMessageToOpenAIMessage);
1012
+ openaiMessages = limitMessagesToTokenCount(openaiMessages, tools, model);
1013
+ let toolChoice = forwardedParameters == null ? void 0 : forwardedParameters.toolChoice;
1014
+ if ((forwardedParameters == null ? void 0 : forwardedParameters.toolChoice) === "function") {
1015
+ toolChoice = {
1016
+ type: "function",
1017
+ function: {
1018
+ name: forwardedParameters.toolChoiceFunctionName
1019
+ }
1020
+ };
1021
+ }
1022
+ const stream = await this.groq.chat.completions.create({
1023
+ model,
1024
+ stream: true,
1025
+ messages: openaiMessages,
1026
+ ...tools.length > 0 && {
1027
+ tools
1028
+ },
1029
+ ...(forwardedParameters == null ? void 0 : forwardedParameters.maxTokens) && {
1030
+ max_tokens: forwardedParameters.maxTokens
1031
+ },
1032
+ ...(forwardedParameters == null ? void 0 : forwardedParameters.stop) && {
1033
+ stop: forwardedParameters.stop
1034
+ },
1035
+ ...toolChoice && {
1036
+ tool_choice: toolChoice
1037
+ },
1038
+ ...this.disableParallelToolCalls && {
1039
+ parallel_tool_calls: false
1040
+ },
1041
+ ...(forwardedParameters == null ? void 0 : forwardedParameters.temperature) && {
1042
+ temperature: forwardedParameters.temperature
1043
+ }
1044
+ });
1045
+ eventSource.stream(async (eventStream$) => {
1046
+ var _a, _b;
1047
+ let mode = null;
1048
+ let currentMessageId;
1049
+ let currentToolCallId;
1050
+ for await (const chunk of stream) {
1051
+ const toolCall = (_a = chunk.choices[0].delta.tool_calls) == null ? void 0 : _a[0];
1052
+ const content = chunk.choices[0].delta.content;
1053
+ if (mode === "message" && (toolCall == null ? void 0 : toolCall.id)) {
1054
+ mode = null;
1055
+ eventStream$.sendTextMessageEnd({
1056
+ messageId: currentMessageId
1057
+ });
1058
+ } else if (mode === "function" && (toolCall === void 0 || (toolCall == null ? void 0 : toolCall.id))) {
1059
+ mode = null;
1060
+ eventStream$.sendActionExecutionEnd({
1061
+ actionExecutionId: currentToolCallId
1062
+ });
1063
+ }
1064
+ if (mode === null) {
1065
+ if (toolCall == null ? void 0 : toolCall.id) {
1066
+ mode = "function";
1067
+ currentToolCallId = toolCall.id;
1068
+ eventStream$.sendActionExecutionStart({
1069
+ actionExecutionId: currentToolCallId,
1070
+ actionName: toolCall.function.name,
1071
+ parentMessageId: chunk.id
1072
+ });
1073
+ } else if (content) {
1074
+ mode = "message";
1075
+ currentMessageId = chunk.id;
1076
+ eventStream$.sendTextMessageStart({
1077
+ messageId: currentMessageId
1078
+ });
1079
+ }
1080
+ }
1081
+ if (mode === "message" && content) {
1082
+ eventStream$.sendTextMessageContent({
1083
+ messageId: currentMessageId,
1084
+ content
1085
+ });
1086
+ } else if (mode === "function" && ((_b = toolCall == null ? void 0 : toolCall.function) == null ? void 0 : _b.arguments)) {
1087
+ eventStream$.sendActionExecutionArgs({
1088
+ actionExecutionId: currentToolCallId,
1089
+ args: toolCall.function.arguments
1090
+ });
1091
+ }
1092
+ }
1093
+ if (mode === "message") {
1094
+ eventStream$.sendTextMessageEnd({
1095
+ messageId: currentMessageId
1096
+ });
1097
+ } else if (mode === "function") {
1098
+ eventStream$.sendActionExecutionEnd({
1099
+ actionExecutionId: currentToolCallId
1100
+ });
1101
+ }
1102
+ eventStream$.complete();
1103
+ });
1104
+ return {
1105
+ threadId: request.threadId || (0, import_shared5.randomUUID)()
1106
+ };
1107
+ }
1108
+ };
1109
+ __name(GroqAdapter, "GroqAdapter");
1110
+
1111
+ // src/service-adapters/anthropic/anthropic-adapter.ts
1112
+ var import_sdk = __toESM(require("@anthropic-ai/sdk"));
1113
+
1114
+ // src/service-adapters/anthropic/utils.ts
1115
+ function limitMessagesToTokenCount2(messages, tools, model, maxTokens) {
1116
+ maxTokens || (maxTokens = MAX_TOKENS);
1117
+ const result = [];
1118
+ const toolsNumTokens = countToolsTokens2(model, tools);
1119
+ if (toolsNumTokens > maxTokens) {
1120
+ throw new Error(`Too many tokens in function definitions: ${toolsNumTokens} > ${maxTokens}`);
1121
+ }
1122
+ maxTokens -= toolsNumTokens;
1123
+ for (const message of messages) {
1124
+ if (message.role === "system") {
1125
+ const numTokens = countMessageTokens2(model, message);
1126
+ maxTokens -= numTokens;
1127
+ if (maxTokens < 0) {
1128
+ throw new Error("Not enough tokens for system message.");
1129
+ }
1130
+ }
1131
+ }
1132
+ let cutoff = false;
1133
+ const reversedMessages = [
1134
+ ...messages
1135
+ ].reverse();
1136
+ for (const message of reversedMessages) {
1137
+ if (message.role === "system") {
1138
+ result.unshift(message);
1139
+ continue;
1140
+ } else if (cutoff) {
1141
+ continue;
1142
+ }
1143
+ let numTokens = countMessageTokens2(model, message);
1144
+ if (maxTokens < numTokens) {
1145
+ cutoff = true;
1146
+ continue;
1147
+ }
1148
+ result.unshift(message);
1149
+ maxTokens -= numTokens;
1150
+ }
1151
+ return result;
1152
+ }
1153
+ __name(limitMessagesToTokenCount2, "limitMessagesToTokenCount");
1154
+ var MAX_TOKENS = 128e3;
1155
+ function countToolsTokens2(model, tools) {
1156
+ if (tools.length === 0) {
1157
+ return 0;
1158
+ }
1159
+ const json = JSON.stringify(tools);
1160
+ return countTokens2(model, json);
1161
+ }
1162
+ __name(countToolsTokens2, "countToolsTokens");
1163
+ function countMessageTokens2(model, message) {
1164
+ return countTokens2(model, JSON.stringify(message.content) || "");
1165
+ }
1166
+ __name(countMessageTokens2, "countMessageTokens");
1167
+ function countTokens2(model, text) {
1168
+ return text.length / 3;
1169
+ }
1170
+ __name(countTokens2, "countTokens");
1171
+ function convertActionInputToAnthropicTool(action) {
1172
+ return {
1173
+ name: action.name,
1174
+ description: action.description,
1175
+ input_schema: JSON.parse(action.jsonSchema)
1176
+ };
1177
+ }
1178
+ __name(convertActionInputToAnthropicTool, "convertActionInputToAnthropicTool");
1179
+ function convertMessageToAnthropicMessage(message) {
1180
+ if (message.isTextMessage()) {
1181
+ if (message.role === "system") {
1182
+ return {
1183
+ role: "assistant",
1184
+ content: [
1185
+ {
1186
+ type: "text",
1187
+ text: "THE FOLLOWING MESSAGE IS A SYSTEM MESSAGE: " + message.content
1188
+ }
1189
+ ]
1190
+ };
1191
+ } else {
1192
+ return {
1193
+ role: message.role === "user" ? "user" : "assistant",
1194
+ content: [
1195
+ {
1196
+ type: "text",
1197
+ text: message.content
1198
+ }
1199
+ ]
1200
+ };
1201
+ }
1202
+ } else if (message.isActionExecutionMessage()) {
1203
+ return {
1204
+ role: "assistant",
1205
+ content: [
1206
+ {
1207
+ id: message.id,
1208
+ type: "tool_use",
1209
+ input: message.arguments,
1210
+ name: message.name
1211
+ }
1212
+ ]
1213
+ };
1214
+ } else if (message.isResultMessage()) {
1215
+ return {
1216
+ role: "user",
1217
+ content: [
1218
+ {
1219
+ type: "tool_result",
1220
+ content: message.result,
1221
+ tool_use_id: message.actionExecutionId
1222
+ }
1223
+ ]
1224
+ };
1225
+ }
1226
+ }
1227
+ __name(convertMessageToAnthropicMessage, "convertMessageToAnthropicMessage");
1228
+ function groupAnthropicMessagesByRole(messageParams) {
1229
+ return messageParams.reduce((acc, message) => {
1230
+ const lastGroup = acc[acc.length - 1];
1231
+ if (lastGroup && lastGroup.role === message.role) {
1232
+ lastGroup.content = lastGroup.content.concat(message.content);
1233
+ } else {
1234
+ acc.push({
1235
+ role: message.role,
1236
+ content: [
1237
+ ...message.content
1238
+ ]
1239
+ });
1240
+ }
1241
+ return acc;
1242
+ }, []);
1243
+ }
1244
+ __name(groupAnthropicMessagesByRole, "groupAnthropicMessagesByRole");
1245
+
1246
+ // src/service-adapters/anthropic/anthropic-adapter.ts
1247
+ var import_shared6 = require("@copilotkit/shared");
1248
+ var DEFAULT_MODEL3 = "claude-3-sonnet-20240229";
1249
+ var AnthropicAdapter = class {
1250
+ model = DEFAULT_MODEL3;
1251
+ _anthropic;
1252
+ get anthropic() {
1253
+ return this._anthropic;
1254
+ }
1255
+ constructor(params) {
1256
+ this._anthropic = (params == null ? void 0 : params.anthropic) || new import_sdk.default({});
1257
+ if (params == null ? void 0 : params.model) {
1258
+ this.model = params.model;
1259
+ }
1260
+ }
1261
+ async process(request) {
1262
+ const { threadId, model = this.model, messages: rawMessages, actions, eventSource, forwardedParameters } = request;
1263
+ const tools = actions.map(convertActionInputToAnthropicTool);
1264
+ const messages = [
1265
+ ...rawMessages
1266
+ ];
1267
+ const instructionsMessage = messages.shift();
1268
+ const instructions = instructionsMessage.isTextMessage() ? instructionsMessage.content : "";
1269
+ let anthropicMessages = messages.map(convertMessageToAnthropicMessage);
1270
+ anthropicMessages = limitMessagesToTokenCount2(anthropicMessages, tools, model);
1271
+ anthropicMessages = groupAnthropicMessagesByRole(anthropicMessages);
1272
+ let toolChoice = forwardedParameters == null ? void 0 : forwardedParameters.toolChoice;
1273
+ if ((forwardedParameters == null ? void 0 : forwardedParameters.toolChoice) === "function") {
1274
+ toolChoice = {
1275
+ type: "tool",
1276
+ name: forwardedParameters.toolChoiceFunctionName
1277
+ };
1278
+ }
1279
+ const stream = this.anthropic.messages.create({
1280
+ system: instructions,
1281
+ model: this.model,
1282
+ messages: anthropicMessages,
1283
+ max_tokens: (forwardedParameters == null ? void 0 : forwardedParameters.maxTokens) || 1024,
1284
+ ...(forwardedParameters == null ? void 0 : forwardedParameters.temperature) ? {
1285
+ temperature: forwardedParameters.temperature
1286
+ } : {},
1287
+ ...tools.length > 0 && {
1288
+ tools
1289
+ },
1290
+ ...toolChoice && {
1291
+ tool_choice: toolChoice
1292
+ },
1293
+ stream: true
1294
+ });
1295
+ eventSource.stream(async (eventStream$) => {
1296
+ let mode = null;
1297
+ let didOutputText = false;
1298
+ let currentMessageId = (0, import_shared6.randomId)();
1299
+ let currentToolCallId = (0, import_shared6.randomId)();
1300
+ let filterThinkingTextBuffer = new FilterThinkingTextBuffer();
1301
+ for await (const chunk of await stream) {
1302
+ if (chunk.type === "message_start") {
1303
+ currentMessageId = chunk.message.id;
1304
+ } else if (chunk.type === "content_block_start") {
1305
+ if (chunk.content_block.type === "text") {
1306
+ didOutputText = false;
1307
+ filterThinkingTextBuffer.reset();
1308
+ mode = "message";
1309
+ } else if (chunk.content_block.type === "tool_use") {
1310
+ currentToolCallId = chunk.content_block.id;
1311
+ eventStream$.sendActionExecutionStart({
1312
+ actionExecutionId: currentToolCallId,
1313
+ actionName: chunk.content_block.name,
1314
+ parentMessageId: currentMessageId
1315
+ });
1316
+ mode = "function";
1317
+ }
1318
+ } else if (chunk.type === "content_block_delta") {
1319
+ if (chunk.delta.type === "text_delta") {
1320
+ const text = filterThinkingTextBuffer.onTextChunk(chunk.delta.text);
1321
+ if (text.length > 0) {
1322
+ if (!didOutputText) {
1323
+ eventStream$.sendTextMessageStart({
1324
+ messageId: currentMessageId
1325
+ });
1326
+ didOutputText = true;
1327
+ }
1328
+ eventStream$.sendTextMessageContent({
1329
+ messageId: currentMessageId,
1330
+ content: text
1331
+ });
1332
+ }
1333
+ } else if (chunk.delta.type === "input_json_delta") {
1334
+ eventStream$.sendActionExecutionArgs({
1335
+ actionExecutionId: currentToolCallId,
1336
+ args: chunk.delta.partial_json
1337
+ });
1338
+ }
1339
+ } else if (chunk.type === "content_block_stop") {
1340
+ if (mode === "message") {
1341
+ if (didOutputText) {
1342
+ eventStream$.sendTextMessageEnd({
1343
+ messageId: currentMessageId
1344
+ });
1345
+ }
1346
+ } else if (mode === "function") {
1347
+ eventStream$.sendActionExecutionEnd({
1348
+ actionExecutionId: currentToolCallId
1349
+ });
1350
+ }
1351
+ }
1352
+ }
1353
+ eventStream$.complete();
1354
+ });
1355
+ return {
1356
+ threadId: threadId || (0, import_shared6.randomUUID)()
1357
+ };
1358
+ }
1359
+ };
1360
+ __name(AnthropicAdapter, "AnthropicAdapter");
1361
+ var THINKING_TAG = "<thinking>";
1362
+ var THINKING_TAG_END = "</thinking>";
1363
+ var FilterThinkingTextBuffer = /* @__PURE__ */ __name(class FilterThinkingTextBuffer2 {
1364
+ buffer;
1365
+ didFilterThinkingTag = false;
1366
+ constructor() {
1367
+ this.buffer = "";
1368
+ }
1369
+ onTextChunk(text) {
1370
+ this.buffer += text;
1371
+ if (this.didFilterThinkingTag) {
1372
+ return text;
1373
+ }
1374
+ const potentialTag = this.buffer.slice(0, THINKING_TAG.length);
1375
+ if (THINKING_TAG.startsWith(potentialTag)) {
1376
+ if (this.buffer.includes(THINKING_TAG_END)) {
1377
+ const end = this.buffer.indexOf(THINKING_TAG_END);
1378
+ const filteredText = this.buffer.slice(end + THINKING_TAG_END.length);
1379
+ this.buffer = filteredText;
1380
+ this.didFilterThinkingTag = true;
1381
+ return filteredText;
1382
+ } else {
1383
+ return "";
1384
+ }
1385
+ }
1386
+ return text;
1387
+ }
1388
+ reset() {
1389
+ this.buffer = "";
1390
+ this.didFilterThinkingTag = false;
1391
+ }
1392
+ }, "FilterThinkingTextBuffer");
1393
+
1394
+ // src/service-adapters/experimental/ollama/ollama-adapter.ts
1395
+ var import_ollama = require("@langchain/community/llms/ollama");
1396
+ var import_shared7 = require("@copilotkit/shared");
1397
+ var DEFAULT_MODEL4 = "llama3:latest";
1398
+ var ExperimentalOllamaAdapter = class {
1399
+ model;
1400
+ constructor(options) {
1401
+ if (options == null ? void 0 : options.model) {
1402
+ this.model = options.model;
1403
+ } else {
1404
+ this.model = DEFAULT_MODEL4;
1405
+ }
1406
+ }
1407
+ async process(request) {
1408
+ const { messages, actions, eventSource } = request;
1409
+ const ollama = new import_ollama.Ollama({
1410
+ model: this.model
1411
+ });
1412
+ const contents = messages.filter((m) => m.isTextMessage()).map((m) => m.content);
1413
+ const _stream = await ollama.stream(contents);
1414
+ eventSource.stream(async (eventStream$) => {
1415
+ const currentMessageId = (0, import_shared7.randomId)();
1416
+ eventStream$.sendTextMessageStart({
1417
+ messageId: currentMessageId
1418
+ });
1419
+ for await (const chunkText of _stream) {
1420
+ eventStream$.sendTextMessageContent({
1421
+ messageId: currentMessageId,
1422
+ content: chunkText
1423
+ });
1424
+ }
1425
+ eventStream$.sendTextMessageEnd({
1426
+ messageId: currentMessageId
1427
+ });
1428
+ eventStream$.complete();
1429
+ });
1430
+ return {
1431
+ threadId: request.threadId || (0, import_shared7.randomUUID)()
1432
+ };
1433
+ }
1434
+ };
1435
+ __name(ExperimentalOllamaAdapter, "ExperimentalOllamaAdapter");
1436
+
1437
+ // src/service-adapters/experimental/empty/empty-adapter.ts
1438
+ var import_shared8 = require("@copilotkit/shared");
1439
+ var ExperimentalEmptyAdapter = class {
1440
+ async process(request) {
1441
+ return {
1442
+ threadId: request.threadId || (0, import_shared8.randomUUID)()
1443
+ };
1444
+ }
1445
+ };
1446
+ __name(ExperimentalEmptyAdapter, "ExperimentalEmptyAdapter");
1447
+ // Annotate the CommonJS export names for ESM import in node:
1448
+ 0 && (module.exports = {
1449
+ AnthropicAdapter,
1450
+ ExperimentalEmptyAdapter,
1451
+ ExperimentalOllamaAdapter,
1452
+ GoogleGenerativeAIAdapter,
1453
+ GroqAdapter,
1454
+ LangChainAdapter,
1455
+ OpenAIAdapter,
1456
+ OpenAIAssistantAdapter,
1457
+ RemoteChain,
1458
+ UnifyAdapter
1459
+ });
1460
+ //# sourceMappingURL=index.js.map