@copilotkit/runtime 1.5.1-next.3 → 1.5.1-next.4

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 (82) hide show
  1. package/CHANGELOG.md +10 -2
  2. package/package.json +2 -2
  3. package/__snapshots__/schema/schema.graphql +0 -263
  4. package/dist/chunk-44O2JGUY.mjs +0 -12
  5. package/dist/chunk-44O2JGUY.mjs.map +0 -1
  6. package/dist/chunk-B74M7FXG.mjs +0 -65
  7. package/dist/chunk-B74M7FXG.mjs.map +0 -1
  8. package/dist/chunk-D2WLFQS6.mjs +0 -43
  9. package/dist/chunk-D2WLFQS6.mjs.map +0 -1
  10. package/dist/chunk-DFOKBSIS.mjs +0 -1
  11. package/dist/chunk-DFOKBSIS.mjs.map +0 -1
  12. package/dist/chunk-M4QD67S3.mjs +0 -80
  13. package/dist/chunk-M4QD67S3.mjs.map +0 -1
  14. package/dist/chunk-OKQVDDJ2.mjs +0 -1173
  15. package/dist/chunk-OKQVDDJ2.mjs.map +0 -1
  16. package/dist/chunk-U3AYI5XZ.mjs +0 -25
  17. package/dist/chunk-U3AYI5XZ.mjs.map +0 -1
  18. package/dist/chunk-U3V2BCGI.mjs +0 -152
  19. package/dist/chunk-U3V2BCGI.mjs.map +0 -1
  20. package/dist/chunk-U75F2RAQ.mjs +0 -25
  21. package/dist/chunk-U75F2RAQ.mjs.map +0 -1
  22. package/dist/chunk-ZSWCEADS.mjs +0 -3147
  23. package/dist/chunk-ZSWCEADS.mjs.map +0 -1
  24. package/dist/copilot-runtime-12e7ac40.d.ts +0 -189
  25. package/dist/graphql/types/base/index.d.ts +0 -6
  26. package/dist/graphql/types/base/index.js +0 -63
  27. package/dist/graphql/types/base/index.js.map +0 -1
  28. package/dist/graphql/types/base/index.mjs +0 -8
  29. package/dist/graphql/types/base/index.mjs.map +0 -1
  30. package/dist/graphql/types/converted/index.d.ts +0 -2
  31. package/dist/graphql/types/converted/index.js +0 -123
  32. package/dist/graphql/types/converted/index.js.map +0 -1
  33. package/dist/graphql/types/converted/index.mjs +0 -17
  34. package/dist/graphql/types/converted/index.mjs.map +0 -1
  35. package/dist/groq-adapter-24abe931.d.ts +0 -281
  36. package/dist/index-10b1c870.d.ts +0 -88
  37. package/dist/index.d.ts +0 -23
  38. package/dist/index.js +0 -4667
  39. package/dist/index.js.map +0 -1
  40. package/dist/index.mjs +0 -72
  41. package/dist/index.mjs.map +0 -1
  42. package/dist/langserve-f021ab9c.d.ts +0 -136
  43. package/dist/lib/cloud/index.d.ts +0 -6
  44. package/dist/lib/cloud/index.js +0 -18
  45. package/dist/lib/cloud/index.js.map +0 -1
  46. package/dist/lib/cloud/index.mjs +0 -1
  47. package/dist/lib/cloud/index.mjs.map +0 -1
  48. package/dist/lib/index.d.ts +0 -20
  49. package/dist/lib/index.js +0 -4392
  50. package/dist/lib/index.js.map +0 -1
  51. package/dist/lib/index.mjs +0 -58
  52. package/dist/lib/index.mjs.map +0 -1
  53. package/dist/lib/integrations/index.d.ts +0 -33
  54. package/dist/lib/integrations/index.js +0 -2043
  55. package/dist/lib/integrations/index.js.map +0 -1
  56. package/dist/lib/integrations/index.mjs +0 -34
  57. package/dist/lib/integrations/index.mjs.map +0 -1
  58. package/dist/lib/integrations/nest/index.d.ts +0 -14
  59. package/dist/lib/integrations/nest/index.js +0 -1952
  60. package/dist/lib/integrations/nest/index.js.map +0 -1
  61. package/dist/lib/integrations/nest/index.mjs +0 -13
  62. package/dist/lib/integrations/nest/index.mjs.map +0 -1
  63. package/dist/lib/integrations/node-express/index.d.ts +0 -14
  64. package/dist/lib/integrations/node-express/index.js +0 -1952
  65. package/dist/lib/integrations/node-express/index.js.map +0 -1
  66. package/dist/lib/integrations/node-express/index.mjs +0 -13
  67. package/dist/lib/integrations/node-express/index.mjs.map +0 -1
  68. package/dist/lib/integrations/node-http/index.d.ts +0 -14
  69. package/dist/lib/integrations/node-http/index.js +0 -1938
  70. package/dist/lib/integrations/node-http/index.js.map +0 -1
  71. package/dist/lib/integrations/node-http/index.mjs +0 -12
  72. package/dist/lib/integrations/node-http/index.mjs.map +0 -1
  73. package/dist/service-adapters/index.d.ts +0 -53
  74. package/dist/service-adapters/index.js +0 -1211
  75. package/dist/service-adapters/index.js.map +0 -1
  76. package/dist/service-adapters/index.mjs +0 -22
  77. package/dist/service-adapters/index.mjs.map +0 -1
  78. package/dist/utils/index.d.ts +0 -49
  79. package/dist/utils/index.js +0 -174
  80. package/dist/utils/index.js.map +0 -1
  81. package/dist/utils/index.mjs +0 -12
  82. package/dist/utils/index.mjs.map +0 -1
@@ -1,1173 +0,0 @@
1
- import {
2
- __name
3
- } from "./chunk-44O2JGUY.mjs";
4
-
5
- // src/service-adapters/langchain/langserve.ts
6
- import { RemoteRunnable } from "langchain/runnables/remote";
7
- var RemoteChain = class {
8
- name;
9
- description;
10
- chainUrl;
11
- parameters;
12
- parameterType;
13
- constructor(options) {
14
- this.name = options.name;
15
- this.description = options.description;
16
- this.chainUrl = options.chainUrl;
17
- this.parameters = options.parameters;
18
- this.parameterType = options.parameterType || "multi";
19
- }
20
- async toAction() {
21
- if (!this.parameters) {
22
- await this.inferLangServeParameters();
23
- }
24
- return {
25
- name: this.name,
26
- description: this.description,
27
- parameters: this.parameters,
28
- handler: async (args) => {
29
- const runnable = new RemoteRunnable({
30
- url: this.chainUrl
31
- });
32
- let input;
33
- if (this.parameterType === "single") {
34
- input = args[Object.keys(args)[0]];
35
- } else {
36
- input = args;
37
- }
38
- return await runnable.invoke(input);
39
- }
40
- };
41
- }
42
- async inferLangServeParameters() {
43
- const supportedTypes = [
44
- "string",
45
- "number",
46
- "boolean"
47
- ];
48
- let schemaUrl = this.chainUrl.replace(/\/+$/, "") + "/input_schema";
49
- let schema = await fetch(schemaUrl).then((res) => res.json()).catch(() => {
50
- throw new Error("Failed to fetch langserve schema at " + schemaUrl);
51
- });
52
- if (supportedTypes.includes(schema.type)) {
53
- this.parameterType = "single";
54
- this.parameters = [
55
- {
56
- name: "input",
57
- type: schema.type,
58
- description: "The input to the chain"
59
- }
60
- ];
61
- } else if (schema.type === "object") {
62
- this.parameterType = "multi";
63
- this.parameters = Object.keys(schema.properties).map((key) => {
64
- var _a;
65
- let property = schema.properties[key];
66
- if (!supportedTypes.includes(property.type)) {
67
- throw new Error("Unsupported schema type");
68
- }
69
- return {
70
- name: key,
71
- type: property.type,
72
- description: property.description || "",
73
- required: ((_a = schema.required) == null ? void 0 : _a.includes(key)) || false
74
- };
75
- });
76
- } else {
77
- throw new Error("Unsupported schema type");
78
- }
79
- }
80
- };
81
- __name(RemoteChain, "RemoteChain");
82
-
83
- // src/service-adapters/openai/openai-adapter.ts
84
- import OpenAI from "openai";
85
-
86
- // src/service-adapters/openai/utils.ts
87
- function limitMessagesToTokenCount(messages, tools, model, maxTokens) {
88
- maxTokens || (maxTokens = maxTokensForOpenAIModel(model));
89
- const result = [];
90
- const toolsNumTokens = countToolsTokens(model, tools);
91
- if (toolsNumTokens > maxTokens) {
92
- throw new Error(`Too many tokens in function definitions: ${toolsNumTokens} > ${maxTokens}`);
93
- }
94
- maxTokens -= toolsNumTokens;
95
- for (const message of messages) {
96
- if (message.role === "system") {
97
- const numTokens = countMessageTokens(model, message);
98
- maxTokens -= numTokens;
99
- if (maxTokens < 0) {
100
- throw new Error("Not enough tokens for system message.");
101
- }
102
- }
103
- }
104
- let cutoff = false;
105
- const reversedMessages = [
106
- ...messages
107
- ].reverse();
108
- for (const message of reversedMessages) {
109
- if (message.role === "system") {
110
- result.unshift(message);
111
- continue;
112
- } else if (cutoff) {
113
- continue;
114
- }
115
- let numTokens = countMessageTokens(model, message);
116
- if (maxTokens < numTokens) {
117
- cutoff = true;
118
- continue;
119
- }
120
- result.unshift(message);
121
- maxTokens -= numTokens;
122
- }
123
- return result;
124
- }
125
- __name(limitMessagesToTokenCount, "limitMessagesToTokenCount");
126
- function maxTokensForOpenAIModel(model) {
127
- return maxTokensByModel[model] || DEFAULT_MAX_TOKENS;
128
- }
129
- __name(maxTokensForOpenAIModel, "maxTokensForOpenAIModel");
130
- var DEFAULT_MAX_TOKENS = 128e3;
131
- var maxTokensByModel = {
132
- // GPT-4
133
- "gpt-4o": 128e3,
134
- "gpt-4o-2024-05-13": 128e3,
135
- "gpt-4-turbo": 128e3,
136
- "gpt-4-turbo-2024-04-09": 128e3,
137
- "gpt-4-0125-preview": 128e3,
138
- "gpt-4-turbo-preview": 128e3,
139
- "gpt-4-1106-preview": 128e3,
140
- "gpt-4-vision-preview": 128e3,
141
- "gpt-4-1106-vision-preview": 128e3,
142
- "gpt-4-32k": 32768,
143
- "gpt-4-32k-0613": 32768,
144
- "gpt-4-32k-0314": 32768,
145
- "gpt-4": 8192,
146
- "gpt-4-0613": 8192,
147
- "gpt-4-0314": 8192,
148
- // GPT-3.5
149
- "gpt-3.5-turbo-0125": 16385,
150
- "gpt-3.5-turbo": 16385,
151
- "gpt-3.5-turbo-1106": 16385,
152
- "gpt-3.5-turbo-instruct": 4096,
153
- "gpt-3.5-turbo-16k": 16385,
154
- "gpt-3.5-turbo-0613": 4096,
155
- "gpt-3.5-turbo-16k-0613": 16385,
156
- "gpt-3.5-turbo-0301": 4097
157
- };
158
- function countToolsTokens(model, tools) {
159
- if (tools.length === 0) {
160
- return 0;
161
- }
162
- const json = JSON.stringify(tools);
163
- return countTokens(model, json);
164
- }
165
- __name(countToolsTokens, "countToolsTokens");
166
- function countMessageTokens(model, message) {
167
- return countTokens(model, message.content || "");
168
- }
169
- __name(countMessageTokens, "countMessageTokens");
170
- function countTokens(model, text) {
171
- return text.length / 3;
172
- }
173
- __name(countTokens, "countTokens");
174
- function convertActionInputToOpenAITool(action) {
175
- return {
176
- type: "function",
177
- function: {
178
- name: action.name,
179
- description: action.description,
180
- parameters: JSON.parse(action.jsonSchema)
181
- }
182
- };
183
- }
184
- __name(convertActionInputToOpenAITool, "convertActionInputToOpenAITool");
185
- function convertMessageToOpenAIMessage(message) {
186
- if (message.isTextMessage()) {
187
- return {
188
- role: message.role,
189
- content: message.content
190
- };
191
- } else if (message.isActionExecutionMessage()) {
192
- return {
193
- role: "assistant",
194
- tool_calls: [
195
- {
196
- id: message.id,
197
- type: "function",
198
- function: {
199
- name: message.name,
200
- arguments: JSON.stringify(message.arguments)
201
- }
202
- }
203
- ]
204
- };
205
- } else if (message.isResultMessage()) {
206
- return {
207
- role: "tool",
208
- content: message.result,
209
- tool_call_id: message.actionExecutionId
210
- };
211
- }
212
- }
213
- __name(convertMessageToOpenAIMessage, "convertMessageToOpenAIMessage");
214
- function convertSystemMessageToAssistantAPI(message) {
215
- return {
216
- ...message,
217
- ...message.role === "system" && {
218
- role: "assistant",
219
- content: "THE FOLLOWING MESSAGE IS A SYSTEM MESSAGE: " + message.content
220
- }
221
- };
222
- }
223
- __name(convertSystemMessageToAssistantAPI, "convertSystemMessageToAssistantAPI");
224
-
225
- // src/service-adapters/openai/openai-adapter.ts
226
- import { randomId } from "@copilotkit/shared";
227
- var DEFAULT_MODEL = "gpt-4o";
228
- var OpenAIAdapter = class {
229
- model = DEFAULT_MODEL;
230
- disableParallelToolCalls = false;
231
- _openai;
232
- get openai() {
233
- return this._openai;
234
- }
235
- constructor(params) {
236
- this._openai = (params == null ? void 0 : params.openai) || new OpenAI({});
237
- if (params == null ? void 0 : params.model) {
238
- this.model = params.model;
239
- }
240
- this.disableParallelToolCalls = (params == null ? void 0 : params.disableParallelToolCalls) || false;
241
- }
242
- async process(request) {
243
- const { threadId, model = this.model, messages, actions, eventSource, forwardedParameters } = request;
244
- const tools = actions.map(convertActionInputToOpenAITool);
245
- let openaiMessages = messages.map(convertMessageToOpenAIMessage);
246
- openaiMessages = limitMessagesToTokenCount(openaiMessages, tools, model);
247
- let toolChoice = forwardedParameters == null ? void 0 : forwardedParameters.toolChoice;
248
- if ((forwardedParameters == null ? void 0 : forwardedParameters.toolChoice) === "function") {
249
- toolChoice = {
250
- type: "function",
251
- function: {
252
- name: forwardedParameters.toolChoiceFunctionName
253
- }
254
- };
255
- }
256
- const stream = this.openai.beta.chat.completions.stream({
257
- model,
258
- stream: true,
259
- messages: openaiMessages,
260
- ...tools.length > 0 && {
261
- tools
262
- },
263
- ...(forwardedParameters == null ? void 0 : forwardedParameters.maxTokens) && {
264
- max_tokens: forwardedParameters.maxTokens
265
- },
266
- ...(forwardedParameters == null ? void 0 : forwardedParameters.stop) && {
267
- stop: forwardedParameters.stop
268
- },
269
- ...toolChoice && {
270
- tool_choice: toolChoice
271
- },
272
- ...this.disableParallelToolCalls && {
273
- parallel_tool_calls: false
274
- }
275
- });
276
- eventSource.stream(async (eventStream$) => {
277
- var _a, _b;
278
- let mode = null;
279
- for await (const chunk of stream) {
280
- if (chunk.choices.length === 0) {
281
- continue;
282
- }
283
- const toolCall = (_a = chunk.choices[0].delta.tool_calls) == null ? void 0 : _a[0];
284
- const content = chunk.choices[0].delta.content;
285
- if (mode === "message" && (toolCall == null ? void 0 : toolCall.id)) {
286
- mode = null;
287
- eventStream$.sendTextMessageEnd();
288
- } else if (mode === "function" && (toolCall === void 0 || (toolCall == null ? void 0 : toolCall.id))) {
289
- mode = null;
290
- eventStream$.sendActionExecutionEnd();
291
- }
292
- if (mode === null) {
293
- if (toolCall == null ? void 0 : toolCall.id) {
294
- mode = "function";
295
- eventStream$.sendActionExecutionStart(toolCall.id, toolCall.function.name);
296
- } else if (content) {
297
- mode = "message";
298
- eventStream$.sendTextMessageStart(chunk.id);
299
- }
300
- }
301
- if (mode === "message" && content) {
302
- eventStream$.sendTextMessageContent(content);
303
- } else if (mode === "function" && ((_b = toolCall == null ? void 0 : toolCall.function) == null ? void 0 : _b.arguments)) {
304
- eventStream$.sendActionExecutionArgs(toolCall.function.arguments);
305
- }
306
- }
307
- if (mode === "message") {
308
- eventStream$.sendTextMessageEnd();
309
- } else if (mode === "function") {
310
- eventStream$.sendActionExecutionEnd();
311
- }
312
- eventStream$.complete();
313
- });
314
- return {
315
- threadId: threadId || randomId()
316
- };
317
- }
318
- };
319
- __name(OpenAIAdapter, "OpenAIAdapter");
320
-
321
- // src/service-adapters/langchain/utils.ts
322
- import { AIMessage, HumanMessage, SystemMessage, ToolMessage } from "@langchain/core/messages";
323
- import { DynamicStructuredTool } from "@langchain/core/tools";
324
- import { randomId as randomId2, convertJsonSchemaToZodSchema } from "@copilotkit/shared";
325
- function convertMessageToLangChainMessage(message) {
326
- if (message.isTextMessage()) {
327
- if (message.role == "user") {
328
- return new HumanMessage(message.content);
329
- } else if (message.role == "assistant") {
330
- return new AIMessage(message.content);
331
- } else if (message.role === "system") {
332
- return new SystemMessage(message.content);
333
- }
334
- } else if (message.isActionExecutionMessage()) {
335
- return new AIMessage({
336
- content: "",
337
- tool_calls: [
338
- {
339
- id: message.id,
340
- args: message.arguments,
341
- name: message.name
342
- }
343
- ]
344
- });
345
- } else if (message.isResultMessage()) {
346
- return new ToolMessage({
347
- content: message.result,
348
- tool_call_id: message.actionExecutionId
349
- });
350
- }
351
- }
352
- __name(convertMessageToLangChainMessage, "convertMessageToLangChainMessage");
353
- function convertActionInputToLangChainTool(actionInput) {
354
- return new DynamicStructuredTool({
355
- name: actionInput.name,
356
- description: actionInput.description,
357
- schema: convertJsonSchemaToZodSchema(JSON.parse(actionInput.jsonSchema), true),
358
- func: async () => {
359
- return "";
360
- }
361
- });
362
- }
363
- __name(convertActionInputToLangChainTool, "convertActionInputToLangChainTool");
364
- function isAIMessage(message) {
365
- return Object.prototype.toString.call(message) === "[object AIMessage]";
366
- }
367
- __name(isAIMessage, "isAIMessage");
368
- function isAIMessageChunk(message) {
369
- return Object.prototype.toString.call(message) === "[object AIMessageChunk]";
370
- }
371
- __name(isAIMessageChunk, "isAIMessageChunk");
372
- function isBaseMessageChunk(message) {
373
- return Object.prototype.toString.call(message) === "[object BaseMessageChunk]";
374
- }
375
- __name(isBaseMessageChunk, "isBaseMessageChunk");
376
- function maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution) {
377
- if (actionExecution) {
378
- eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, "Sending a message");
379
- }
380
- }
381
- __name(maybeSendActionExecutionResultIsMessage, "maybeSendActionExecutionResultIsMessage");
382
- async function streamLangChainResponse({ result, eventStream$, actionExecution }) {
383
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
384
- if (typeof result === "string") {
385
- if (!actionExecution) {
386
- eventStream$.sendTextMessage(randomId2(), result);
387
- } else {
388
- eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, result);
389
- }
390
- } else if (isAIMessage(result)) {
391
- maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
392
- if (result.content) {
393
- eventStream$.sendTextMessage(randomId2(), result.content);
394
- }
395
- for (const toolCall of result.tool_calls) {
396
- eventStream$.sendActionExecution(toolCall.id || randomId2(), toolCall.name, JSON.stringify(toolCall.args));
397
- }
398
- } else if (isBaseMessageChunk(result)) {
399
- maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
400
- if ((_a = result.lc_kwargs) == null ? void 0 : _a.content) {
401
- eventStream$.sendTextMessage(randomId2(), result.content);
402
- }
403
- if ((_b = result.lc_kwargs) == null ? void 0 : _b.tool_calls) {
404
- for (const toolCall of (_c = result.lc_kwargs) == null ? void 0 : _c.tool_calls) {
405
- eventStream$.sendActionExecution(toolCall.id || randomId2(), toolCall.name, JSON.stringify(toolCall.args));
406
- }
407
- }
408
- } else if (result && "getReader" in result) {
409
- maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
410
- let reader = result.getReader();
411
- let mode = null;
412
- const toolCallDetails = {
413
- name: null,
414
- id: null,
415
- index: null,
416
- prevIndex: null
417
- };
418
- while (true) {
419
- try {
420
- const { done, value } = await reader.read();
421
- let toolCallName = void 0;
422
- let toolCallId = void 0;
423
- let toolCallArgs = void 0;
424
- let hasToolCall = false;
425
- let content = value == null ? void 0 : value.content;
426
- if (isAIMessageChunk(value)) {
427
- let chunk = (_d = value.tool_call_chunks) == null ? void 0 : _d[0];
428
- toolCallArgs = chunk == null ? void 0 : chunk.args;
429
- hasToolCall = chunk != void 0;
430
- if (chunk == null ? void 0 : chunk.name)
431
- toolCallDetails.name = chunk.name;
432
- if ((chunk == null ? void 0 : chunk.index) != null) {
433
- toolCallDetails.index = chunk.index;
434
- if (toolCallDetails.prevIndex == null)
435
- toolCallDetails.prevIndex = chunk.index;
436
- }
437
- if (chunk == null ? void 0 : chunk.id)
438
- toolCallDetails.id = chunk.index != null ? `${chunk.id}-idx-${chunk.index}` : chunk.id;
439
- toolCallName = toolCallDetails.name;
440
- toolCallId = toolCallDetails.id;
441
- } else if (isBaseMessageChunk(value)) {
442
- let chunk = (_f = (_e = value.additional_kwargs) == null ? void 0 : _e.tool_calls) == null ? void 0 : _f[0];
443
- toolCallName = (_g = chunk == null ? void 0 : chunk.function) == null ? void 0 : _g.name;
444
- toolCallId = chunk == null ? void 0 : chunk.id;
445
- toolCallArgs = (_h = chunk == null ? void 0 : chunk.function) == null ? void 0 : _h.arguments;
446
- hasToolCall = (chunk == null ? void 0 : chunk.function) != void 0;
447
- }
448
- if (mode === "message" && (toolCallId || done)) {
449
- mode = null;
450
- eventStream$.sendTextMessageEnd();
451
- } else if (mode === "function" && (!hasToolCall || done)) {
452
- mode = null;
453
- eventStream$.sendActionExecutionEnd();
454
- }
455
- if (done) {
456
- break;
457
- }
458
- if (mode === null) {
459
- if (hasToolCall && toolCallId && toolCallName) {
460
- mode = "function";
461
- eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
462
- } else if (content) {
463
- mode = "message";
464
- eventStream$.sendTextMessageStart(randomId2());
465
- }
466
- }
467
- if (mode === "message" && content) {
468
- eventStream$.sendTextMessageContent(Array.isArray(content) ? ((_i = content[0]) == null ? void 0 : _i.text) ?? "" : content);
469
- } else if (mode === "function" && toolCallArgs) {
470
- if (toolCallDetails.index !== toolCallDetails.prevIndex) {
471
- eventStream$.sendActionExecutionEnd();
472
- eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
473
- toolCallDetails.prevIndex = toolCallDetails.index;
474
- }
475
- eventStream$.sendActionExecutionArgs(toolCallArgs);
476
- }
477
- } catch (error) {
478
- console.error("Error reading from stream", error);
479
- break;
480
- }
481
- }
482
- } else if (actionExecution) {
483
- eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, encodeResult(result));
484
- } else {
485
- throw new Error("Invalid return type from LangChain function.");
486
- }
487
- eventStream$.complete();
488
- }
489
- __name(streamLangChainResponse, "streamLangChainResponse");
490
- function encodeResult(result) {
491
- if (result === void 0) {
492
- return "";
493
- } else if (typeof result === "string") {
494
- return result;
495
- } else {
496
- return JSON.stringify(result);
497
- }
498
- }
499
- __name(encodeResult, "encodeResult");
500
-
501
- // src/service-adapters/langchain/langchain-adapter.ts
502
- import { randomId as randomId3 } from "@copilotkit/shared";
503
- import { awaitAllCallbacks } from "@langchain/core/callbacks/promises";
504
- var LangChainAdapter = class {
505
- options;
506
- /**
507
- * To use LangChain as a backend, provide a handler function to the adapter with your custom LangChain logic.
508
- */
509
- constructor(options) {
510
- this.options = options;
511
- }
512
- async process(request) {
513
- try {
514
- const { eventSource, model, actions, messages, runId } = request;
515
- const threadId = request.threadId ?? randomId3();
516
- const result = await this.options.chainFn({
517
- messages: messages.map(convertMessageToLangChainMessage),
518
- tools: actions.map(convertActionInputToLangChainTool),
519
- model,
520
- threadId,
521
- runId
522
- });
523
- eventSource.stream(async (eventStream$) => {
524
- await streamLangChainResponse({
525
- result,
526
- eventStream$
527
- });
528
- });
529
- return {
530
- threadId
531
- };
532
- } finally {
533
- await awaitAllCallbacks();
534
- }
535
- }
536
- };
537
- __name(LangChainAdapter, "LangChainAdapter");
538
-
539
- // src/service-adapters/google/google-genai-adapter.ts
540
- import { ChatGoogle } from "@langchain/google-gauth";
541
- var GoogleGenerativeAIAdapter = class extends LangChainAdapter {
542
- constructor(options) {
543
- super({
544
- chainFn: async ({ messages, tools, threadId }) => {
545
- const model = new ChatGoogle({
546
- modelName: (options == null ? void 0 : options.model) ?? "gemini-1.5-pro",
547
- apiVersion: "v1beta"
548
- }).bindTools(tools);
549
- return model.stream(messages, {
550
- metadata: {
551
- conversation_id: threadId
552
- }
553
- });
554
- }
555
- });
556
- }
557
- };
558
- __name(GoogleGenerativeAIAdapter, "GoogleGenerativeAIAdapter");
559
-
560
- // src/service-adapters/openai/openai-assistant-adapter.ts
561
- import OpenAI2 from "openai";
562
- var OpenAIAssistantAdapter = class {
563
- openai;
564
- codeInterpreterEnabled;
565
- assistantId;
566
- fileSearchEnabled;
567
- disableParallelToolCalls;
568
- constructor(params) {
569
- this.openai = params.openai || new OpenAI2({});
570
- this.codeInterpreterEnabled = params.codeInterpreterEnabled === false || true;
571
- this.fileSearchEnabled = params.fileSearchEnabled === false || true;
572
- this.assistantId = params.assistantId;
573
- this.disableParallelToolCalls = (params == null ? void 0 : params.disableParallelToolCalls) || false;
574
- }
575
- async process(request) {
576
- const { messages, actions, eventSource, runId, forwardedParameters } = request;
577
- let threadId = request.threadId || (await this.openai.beta.threads.create()).id;
578
- const lastMessage = messages.at(-1);
579
- let nextRunId = void 0;
580
- if (lastMessage.isResultMessage() && runId) {
581
- nextRunId = await this.submitToolOutputs(threadId, runId, messages, eventSource);
582
- } else if (lastMessage.isTextMessage()) {
583
- nextRunId = await this.submitUserMessage(threadId, messages, actions, eventSource, forwardedParameters);
584
- } else {
585
- throw new Error("No actionable message found in the messages");
586
- }
587
- return {
588
- threadId,
589
- runId: nextRunId
590
- };
591
- }
592
- async submitToolOutputs(threadId, runId, messages, eventSource) {
593
- let run = await this.openai.beta.threads.runs.retrieve(threadId, runId);
594
- if (!run.required_action) {
595
- throw new Error("No tool outputs required");
596
- }
597
- const toolCallsIds = run.required_action.submit_tool_outputs.tool_calls.map((toolCall) => toolCall.id);
598
- const resultMessages = messages.filter((message) => message.isResultMessage() && toolCallsIds.includes(message.actionExecutionId));
599
- if (toolCallsIds.length != resultMessages.length) {
600
- throw new Error("Number of function results does not match the number of tool calls");
601
- }
602
- const toolOutputs = resultMessages.map((message) => {
603
- return {
604
- tool_call_id: message.actionExecutionId,
605
- output: message.result
606
- };
607
- });
608
- const stream = this.openai.beta.threads.runs.submitToolOutputsStream(threadId, runId, {
609
- tool_outputs: toolOutputs,
610
- ...this.disableParallelToolCalls && {
611
- parallel_tool_calls: false
612
- }
613
- });
614
- await this.streamResponse(stream, eventSource);
615
- return runId;
616
- }
617
- async submitUserMessage(threadId, messages, actions, eventSource, forwardedParameters) {
618
- messages = [
619
- ...messages
620
- ];
621
- const instructionsMessage = messages.shift();
622
- const instructions = instructionsMessage.isTextMessage() ? instructionsMessage.content : "";
623
- const userMessage = messages.map(convertMessageToOpenAIMessage).map(convertSystemMessageToAssistantAPI).at(-1);
624
- if (userMessage.role !== "user") {
625
- throw new Error("No user message found");
626
- }
627
- await this.openai.beta.threads.messages.create(threadId, {
628
- role: "user",
629
- content: userMessage.content
630
- });
631
- const openaiTools = actions.map(convertActionInputToOpenAITool);
632
- const tools = [
633
- ...openaiTools,
634
- ...this.codeInterpreterEnabled ? [
635
- {
636
- type: "code_interpreter"
637
- }
638
- ] : [],
639
- ...this.fileSearchEnabled ? [
640
- {
641
- type: "file_search"
642
- }
643
- ] : []
644
- ];
645
- let stream = this.openai.beta.threads.runs.stream(threadId, {
646
- assistant_id: this.assistantId,
647
- instructions,
648
- tools,
649
- ...(forwardedParameters == null ? void 0 : forwardedParameters.maxTokens) && {
650
- max_completion_tokens: forwardedParameters.maxTokens
651
- },
652
- ...this.disableParallelToolCalls && {
653
- parallel_tool_calls: false
654
- }
655
- });
656
- await this.streamResponse(stream, eventSource);
657
- return getRunIdFromStream(stream);
658
- }
659
- async streamResponse(stream, eventSource) {
660
- eventSource.stream(async (eventStream$) => {
661
- var _a, _b, _c, _d, _e, _f;
662
- let inFunctionCall = false;
663
- for await (const chunk of stream) {
664
- switch (chunk.event) {
665
- case "thread.message.created":
666
- if (inFunctionCall) {
667
- eventStream$.sendActionExecutionEnd();
668
- }
669
- eventStream$.sendTextMessageStart(chunk.data.id);
670
- break;
671
- case "thread.message.delta":
672
- if (((_a = chunk.data.delta.content) == null ? void 0 : _a[0].type) === "text") {
673
- eventStream$.sendTextMessageContent((_b = chunk.data.delta.content) == null ? void 0 : _b[0].text.value);
674
- }
675
- break;
676
- case "thread.message.completed":
677
- eventStream$.sendTextMessageEnd();
678
- break;
679
- case "thread.run.step.delta":
680
- let toolCallId;
681
- let toolCallName;
682
- let toolCallArgs;
683
- if (chunk.data.delta.step_details.type === "tool_calls" && ((_c = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _c[0].type) === "function") {
684
- toolCallId = (_d = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _d[0].id;
685
- toolCallName = (_e = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _e[0].function.name;
686
- toolCallArgs = (_f = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _f[0].function.arguments;
687
- }
688
- if (toolCallName && toolCallId) {
689
- if (inFunctionCall) {
690
- eventStream$.sendActionExecutionEnd();
691
- }
692
- inFunctionCall = true;
693
- eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
694
- } else if (toolCallArgs) {
695
- eventStream$.sendActionExecutionArgs(toolCallArgs);
696
- }
697
- break;
698
- }
699
- }
700
- if (inFunctionCall) {
701
- eventStream$.sendActionExecutionEnd();
702
- }
703
- eventStream$.complete();
704
- });
705
- }
706
- };
707
- __name(OpenAIAssistantAdapter, "OpenAIAssistantAdapter");
708
- function getRunIdFromStream(stream) {
709
- return new Promise((resolve, reject) => {
710
- let runIdGetter = /* @__PURE__ */ __name((event) => {
711
- if (event.event === "thread.run.created") {
712
- const runId = event.data.id;
713
- stream.off("event", runIdGetter);
714
- resolve(runId);
715
- }
716
- }, "runIdGetter");
717
- stream.on("event", runIdGetter);
718
- });
719
- }
720
- __name(getRunIdFromStream, "getRunIdFromStream");
721
-
722
- // src/service-adapters/unify/unify-adapter.ts
723
- import OpenAI3 from "openai";
724
- import { randomId as randomId4 } from "@copilotkit/shared";
725
- var UnifyAdapter = class {
726
- apiKey;
727
- model;
728
- start;
729
- constructor(options) {
730
- if (options == null ? void 0 : options.apiKey) {
731
- this.apiKey = options.apiKey;
732
- } else {
733
- this.apiKey = "UNIFY_API_KEY";
734
- }
735
- this.model = options == null ? void 0 : options.model;
736
- this.start = true;
737
- }
738
- async process(request) {
739
- const tools = request.actions.map(convertActionInputToOpenAITool);
740
- const openai = new OpenAI3({
741
- apiKey: this.apiKey,
742
- baseURL: "https://api.unify.ai/v0/"
743
- });
744
- const messages = request.messages.map(convertMessageToOpenAIMessage);
745
- const stream = await openai.chat.completions.create({
746
- model: this.model,
747
- messages,
748
- stream: true,
749
- ...tools.length > 0 && {
750
- tools
751
- }
752
- });
753
- let model = null;
754
- request.eventSource.stream(async (eventStream$) => {
755
- var _a, _b;
756
- let mode = null;
757
- for await (const chunk of stream) {
758
- if (this.start) {
759
- model = chunk.model;
760
- eventStream$.sendTextMessageStart(randomId4());
761
- eventStream$.sendTextMessageContent(`Model used: ${model}
762
- `);
763
- eventStream$.sendTextMessageEnd();
764
- this.start = false;
765
- }
766
- const toolCall = (_a = chunk.choices[0].delta.tool_calls) == null ? void 0 : _a[0];
767
- const content = chunk.choices[0].delta.content;
768
- if (mode === "message" && (toolCall == null ? void 0 : toolCall.id)) {
769
- mode = null;
770
- eventStream$.sendTextMessageEnd();
771
- } else if (mode === "function" && (toolCall === void 0 || (toolCall == null ? void 0 : toolCall.id))) {
772
- mode = null;
773
- eventStream$.sendActionExecutionEnd();
774
- }
775
- if (mode === null) {
776
- if (toolCall == null ? void 0 : toolCall.id) {
777
- mode = "function";
778
- eventStream$.sendActionExecutionStart(toolCall.id, toolCall.function.name);
779
- } else if (content) {
780
- mode = "message";
781
- eventStream$.sendTextMessageStart(chunk.id);
782
- }
783
- }
784
- if (mode === "message" && content) {
785
- eventStream$.sendTextMessageContent(content);
786
- } else if (mode === "function" && ((_b = toolCall == null ? void 0 : toolCall.function) == null ? void 0 : _b.arguments)) {
787
- eventStream$.sendActionExecutionArgs(toolCall.function.arguments);
788
- }
789
- }
790
- if (mode === "message") {
791
- eventStream$.sendTextMessageEnd();
792
- } else if (mode === "function") {
793
- eventStream$.sendActionExecutionEnd();
794
- }
795
- eventStream$.complete();
796
- });
797
- return {
798
- threadId: request.threadId || randomId4()
799
- };
800
- }
801
- };
802
- __name(UnifyAdapter, "UnifyAdapter");
803
-
804
- // src/service-adapters/groq/groq-adapter.ts
805
- import { Groq } from "groq-sdk";
806
- import { randomId as randomId5 } from "@copilotkit/shared";
807
- var DEFAULT_MODEL2 = "llama3-groq-70b-8192-tool-use-preview";
808
- var GroqAdapter = class {
809
- model = DEFAULT_MODEL2;
810
- disableParallelToolCalls = false;
811
- _groq;
812
- get groq() {
813
- return this._groq;
814
- }
815
- constructor(params) {
816
- this._groq = (params == null ? void 0 : params.groq) || new Groq({});
817
- if (params == null ? void 0 : params.model) {
818
- this.model = params.model;
819
- }
820
- this.disableParallelToolCalls = (params == null ? void 0 : params.disableParallelToolCalls) || false;
821
- }
822
- async process(request) {
823
- const { threadId, model = this.model, messages, actions, eventSource, forwardedParameters } = request;
824
- const tools = actions.map(convertActionInputToOpenAITool);
825
- let openaiMessages = messages.map(convertMessageToOpenAIMessage);
826
- openaiMessages = limitMessagesToTokenCount(openaiMessages, tools, model);
827
- let toolChoice = forwardedParameters == null ? void 0 : forwardedParameters.toolChoice;
828
- if ((forwardedParameters == null ? void 0 : forwardedParameters.toolChoice) === "function") {
829
- toolChoice = {
830
- type: "function",
831
- function: {
832
- name: forwardedParameters.toolChoiceFunctionName
833
- }
834
- };
835
- }
836
- const stream = await this.groq.chat.completions.create({
837
- model,
838
- stream: true,
839
- messages: openaiMessages,
840
- ...tools.length > 0 && {
841
- tools
842
- },
843
- ...(forwardedParameters == null ? void 0 : forwardedParameters.maxTokens) && {
844
- max_tokens: forwardedParameters.maxTokens
845
- },
846
- ...(forwardedParameters == null ? void 0 : forwardedParameters.stop) && {
847
- stop: forwardedParameters.stop
848
- },
849
- ...toolChoice && {
850
- tool_choice: toolChoice
851
- },
852
- ...this.disableParallelToolCalls && {
853
- parallel_tool_calls: false
854
- }
855
- });
856
- eventSource.stream(async (eventStream$) => {
857
- var _a, _b;
858
- let mode = null;
859
- for await (const chunk of stream) {
860
- const toolCall = (_a = chunk.choices[0].delta.tool_calls) == null ? void 0 : _a[0];
861
- const content = chunk.choices[0].delta.content;
862
- if (mode === "message" && (toolCall == null ? void 0 : toolCall.id)) {
863
- mode = null;
864
- eventStream$.sendTextMessageEnd();
865
- } else if (mode === "function" && (toolCall === void 0 || (toolCall == null ? void 0 : toolCall.id))) {
866
- mode = null;
867
- eventStream$.sendActionExecutionEnd();
868
- }
869
- if (mode === null) {
870
- if (toolCall == null ? void 0 : toolCall.id) {
871
- mode = "function";
872
- eventStream$.sendActionExecutionStart(toolCall.id, toolCall.function.name);
873
- } else if (content) {
874
- mode = "message";
875
- eventStream$.sendTextMessageStart(chunk.id);
876
- }
877
- }
878
- if (mode === "message" && content) {
879
- eventStream$.sendTextMessageContent(content);
880
- } else if (mode === "function" && ((_b = toolCall == null ? void 0 : toolCall.function) == null ? void 0 : _b.arguments)) {
881
- eventStream$.sendActionExecutionArgs(toolCall.function.arguments);
882
- }
883
- }
884
- if (mode === "message") {
885
- eventStream$.sendTextMessageEnd();
886
- } else if (mode === "function") {
887
- eventStream$.sendActionExecutionEnd();
888
- }
889
- eventStream$.complete();
890
- });
891
- return {
892
- threadId: threadId || randomId5()
893
- };
894
- }
895
- };
896
- __name(GroqAdapter, "GroqAdapter");
897
-
898
- // src/service-adapters/anthropic/anthropic-adapter.ts
899
- import Anthropic from "@anthropic-ai/sdk";
900
-
901
- // src/service-adapters/anthropic/utils.ts
902
- function limitMessagesToTokenCount2(messages, tools, model, maxTokens) {
903
- maxTokens || (maxTokens = MAX_TOKENS);
904
- const result = [];
905
- const toolsNumTokens = countToolsTokens2(model, tools);
906
- if (toolsNumTokens > maxTokens) {
907
- throw new Error(`Too many tokens in function definitions: ${toolsNumTokens} > ${maxTokens}`);
908
- }
909
- maxTokens -= toolsNumTokens;
910
- for (const message of messages) {
911
- if (message.role === "system") {
912
- const numTokens = countMessageTokens2(model, message);
913
- maxTokens -= numTokens;
914
- if (maxTokens < 0) {
915
- throw new Error("Not enough tokens for system message.");
916
- }
917
- }
918
- }
919
- let cutoff = false;
920
- const reversedMessages = [
921
- ...messages
922
- ].reverse();
923
- for (const message of reversedMessages) {
924
- if (message.role === "system") {
925
- result.unshift(message);
926
- continue;
927
- } else if (cutoff) {
928
- continue;
929
- }
930
- let numTokens = countMessageTokens2(model, message);
931
- if (maxTokens < numTokens) {
932
- cutoff = true;
933
- continue;
934
- }
935
- result.unshift(message);
936
- maxTokens -= numTokens;
937
- }
938
- return result;
939
- }
940
- __name(limitMessagesToTokenCount2, "limitMessagesToTokenCount");
941
- var MAX_TOKENS = 128e3;
942
- function countToolsTokens2(model, tools) {
943
- if (tools.length === 0) {
944
- return 0;
945
- }
946
- const json = JSON.stringify(tools);
947
- return countTokens2(model, json);
948
- }
949
- __name(countToolsTokens2, "countToolsTokens");
950
- function countMessageTokens2(model, message) {
951
- return countTokens2(model, JSON.stringify(message.content) || "");
952
- }
953
- __name(countMessageTokens2, "countMessageTokens");
954
- function countTokens2(model, text) {
955
- return text.length / 3;
956
- }
957
- __name(countTokens2, "countTokens");
958
- function convertActionInputToAnthropicTool(action) {
959
- return {
960
- name: action.name,
961
- description: action.description,
962
- input_schema: JSON.parse(action.jsonSchema)
963
- };
964
- }
965
- __name(convertActionInputToAnthropicTool, "convertActionInputToAnthropicTool");
966
- function convertMessageToAnthropicMessage(message) {
967
- if (message.isTextMessage()) {
968
- if (message.role === "system") {
969
- return {
970
- role: "assistant",
971
- content: [
972
- {
973
- type: "text",
974
- text: "THE FOLLOWING MESSAGE IS A SYSTEM MESSAGE: " + message.content
975
- }
976
- ]
977
- };
978
- } else {
979
- return {
980
- role: message.role === "user" ? "user" : "assistant",
981
- content: [
982
- {
983
- type: "text",
984
- text: message.content
985
- }
986
- ]
987
- };
988
- }
989
- } else if (message.isActionExecutionMessage()) {
990
- return {
991
- role: "assistant",
992
- content: [
993
- {
994
- id: message.id,
995
- type: "tool_use",
996
- input: message.arguments,
997
- name: message.name
998
- }
999
- ]
1000
- };
1001
- } else if (message.isResultMessage()) {
1002
- return {
1003
- role: "user",
1004
- content: [
1005
- {
1006
- type: "tool_result",
1007
- content: message.result,
1008
- tool_use_id: message.actionExecutionId
1009
- }
1010
- ]
1011
- };
1012
- }
1013
- }
1014
- __name(convertMessageToAnthropicMessage, "convertMessageToAnthropicMessage");
1015
- function groupAnthropicMessagesByRole(messageParams) {
1016
- return messageParams.reduce((acc, message) => {
1017
- const lastGroup = acc[acc.length - 1];
1018
- if (lastGroup && lastGroup.role === message.role) {
1019
- lastGroup.content = lastGroup.content.concat(message.content);
1020
- } else {
1021
- acc.push({
1022
- role: message.role,
1023
- content: [
1024
- ...message.content
1025
- ]
1026
- });
1027
- }
1028
- return acc;
1029
- }, []);
1030
- }
1031
- __name(groupAnthropicMessagesByRole, "groupAnthropicMessagesByRole");
1032
-
1033
- // src/service-adapters/anthropic/anthropic-adapter.ts
1034
- import { randomId as randomId6 } from "@copilotkit/shared";
1035
- var DEFAULT_MODEL3 = "claude-3-sonnet-20240229";
1036
- var AnthropicAdapter = class {
1037
- model = DEFAULT_MODEL3;
1038
- _anthropic;
1039
- get anthropic() {
1040
- return this._anthropic;
1041
- }
1042
- constructor(params) {
1043
- this._anthropic = (params == null ? void 0 : params.anthropic) || new Anthropic({});
1044
- if (params == null ? void 0 : params.model) {
1045
- this.model = params.model;
1046
- }
1047
- }
1048
- async process(request) {
1049
- const { threadId, model = this.model, messages: rawMessages, actions, eventSource, forwardedParameters } = request;
1050
- const tools = actions.map(convertActionInputToAnthropicTool);
1051
- const messages = [
1052
- ...rawMessages
1053
- ];
1054
- const instructionsMessage = messages.shift();
1055
- const instructions = instructionsMessage.isTextMessage() ? instructionsMessage.content : "";
1056
- let anthropicMessages = messages.map(convertMessageToAnthropicMessage);
1057
- anthropicMessages = limitMessagesToTokenCount2(anthropicMessages, tools, model);
1058
- anthropicMessages = groupAnthropicMessagesByRole(anthropicMessages);
1059
- let toolChoice = forwardedParameters == null ? void 0 : forwardedParameters.toolChoice;
1060
- if ((forwardedParameters == null ? void 0 : forwardedParameters.toolChoice) === "function") {
1061
- toolChoice = {
1062
- type: "tool",
1063
- name: forwardedParameters.toolChoiceFunctionName
1064
- };
1065
- }
1066
- const stream = this.anthropic.messages.create({
1067
- system: instructions,
1068
- model: this.model,
1069
- messages: anthropicMessages,
1070
- max_tokens: (forwardedParameters == null ? void 0 : forwardedParameters.maxTokens) || 1024,
1071
- ...tools.length > 0 && {
1072
- tools
1073
- },
1074
- ...toolChoice && {
1075
- tool_choice: toolChoice
1076
- },
1077
- stream: true
1078
- });
1079
- eventSource.stream(async (eventStream$) => {
1080
- let mode = null;
1081
- let didOutputText = false;
1082
- let currentMessageId = randomId6();
1083
- let currentToolCallId = randomId6();
1084
- let filterThinkingTextBuffer = new FilterThinkingTextBuffer();
1085
- for await (const chunk of await stream) {
1086
- if (chunk.type === "message_start") {
1087
- currentMessageId = chunk.message.id;
1088
- } else if (chunk.type === "content_block_start") {
1089
- if (chunk.content_block.type === "text") {
1090
- didOutputText = false;
1091
- filterThinkingTextBuffer.reset();
1092
- mode = "message";
1093
- } else if (chunk.content_block.type === "tool_use") {
1094
- currentToolCallId = chunk.content_block.id;
1095
- eventStream$.sendActionExecutionStart(currentToolCallId, chunk.content_block.name);
1096
- mode = "function";
1097
- }
1098
- } else if (chunk.type === "content_block_delta") {
1099
- if (chunk.delta.type === "text_delta") {
1100
- const text = filterThinkingTextBuffer.onTextChunk(chunk.delta.text);
1101
- if (text.length > 0) {
1102
- if (!didOutputText) {
1103
- eventStream$.sendTextMessageStart(currentMessageId);
1104
- didOutputText = true;
1105
- }
1106
- eventStream$.sendTextMessageContent(text);
1107
- }
1108
- } else if (chunk.delta.type === "input_json_delta") {
1109
- eventStream$.sendActionExecutionArgs(chunk.delta.partial_json);
1110
- }
1111
- } else if (chunk.type === "content_block_stop") {
1112
- if (mode === "message") {
1113
- if (didOutputText) {
1114
- eventStream$.sendTextMessageEnd();
1115
- }
1116
- } else if (mode === "function") {
1117
- eventStream$.sendActionExecutionEnd();
1118
- }
1119
- }
1120
- }
1121
- eventStream$.complete();
1122
- });
1123
- return {
1124
- threadId: threadId || randomId6()
1125
- };
1126
- }
1127
- };
1128
- __name(AnthropicAdapter, "AnthropicAdapter");
1129
- var THINKING_TAG = "<thinking>";
1130
- var THINKING_TAG_END = "</thinking>";
1131
- var FilterThinkingTextBuffer = /* @__PURE__ */ __name(class FilterThinkingTextBuffer2 {
1132
- buffer;
1133
- didFilterThinkingTag = false;
1134
- constructor() {
1135
- this.buffer = "";
1136
- }
1137
- onTextChunk(text) {
1138
- this.buffer += text;
1139
- if (this.didFilterThinkingTag) {
1140
- return text;
1141
- }
1142
- const potentialTag = this.buffer.slice(0, THINKING_TAG.length);
1143
- if (THINKING_TAG.startsWith(potentialTag)) {
1144
- if (this.buffer.includes(THINKING_TAG_END)) {
1145
- const end = this.buffer.indexOf(THINKING_TAG_END);
1146
- const filteredText = this.buffer.slice(end + THINKING_TAG_END.length);
1147
- this.buffer = filteredText;
1148
- this.didFilterThinkingTag = true;
1149
- return filteredText;
1150
- } else {
1151
- return "";
1152
- }
1153
- }
1154
- return text;
1155
- }
1156
- reset() {
1157
- this.buffer = "";
1158
- this.didFilterThinkingTag = false;
1159
- }
1160
- }, "FilterThinkingTextBuffer");
1161
-
1162
- export {
1163
- RemoteChain,
1164
- OpenAIAdapter,
1165
- streamLangChainResponse,
1166
- LangChainAdapter,
1167
- GoogleGenerativeAIAdapter,
1168
- OpenAIAssistantAdapter,
1169
- UnifyAdapter,
1170
- GroqAdapter,
1171
- AnthropicAdapter
1172
- };
1173
- //# sourceMappingURL=chunk-OKQVDDJ2.mjs.map