@copilotkit/runtime 1.2.2-feat-runtime-remote-actions.1 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -13
- package/dist/{chunk-BPEPG56J.mjs → chunk-47TPNJX7.mjs} +2 -2
- package/dist/{chunk-Y5TWOZFD.mjs → chunk-4BZ6WXBB.mjs} +3 -3
- package/dist/{chunk-3SKYFYY2.mjs → chunk-IRRAL44O.mjs} +4 -5
- package/dist/chunk-IRRAL44O.mjs.map +1 -0
- package/dist/{chunk-BJ2LVHWA.mjs → chunk-OF6AN6HF.mjs} +521 -256
- package/dist/chunk-OF6AN6HF.mjs.map +1 -0
- package/dist/{chunk-UL2OKN2O.mjs → chunk-VWS65V7Y.mjs} +2 -2
- package/dist/{chunk-U2EKJP47.mjs → chunk-XCGRXAJU.mjs} +2 -2
- package/dist/{copilot-runtime-d427e991.d.ts → copilot-runtime-a1b5f1ce.d.ts} +1 -1
- package/dist/{index-079752b9.d.ts → groq-adapter-069ac812.d.ts} +82 -82
- package/dist/index.d.ts +7 -5
- package/dist/index.js +721 -454
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +10 -6
- package/dist/index.mjs.map +1 -1
- package/dist/{langserve-d6073a3b.d.ts → langserve-15a1286b.d.ts} +1 -1
- package/dist/lib/index.d.ts +4 -4
- package/dist/lib/index.js +445 -446
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +6 -6
- package/dist/lib/integrations/index.d.ts +3 -3
- package/dist/lib/integrations/index.js +3 -4
- package/dist/lib/integrations/index.js.map +1 -1
- package/dist/lib/integrations/index.mjs +4 -4
- package/dist/lib/integrations/nest/index.d.ts +2 -2
- package/dist/lib/integrations/nest/index.js +3 -4
- package/dist/lib/integrations/nest/index.js.map +1 -1
- package/dist/lib/integrations/nest/index.mjs +2 -2
- package/dist/lib/integrations/node-express/index.d.ts +2 -2
- package/dist/lib/integrations/node-express/index.js +3 -4
- package/dist/lib/integrations/node-express/index.js.map +1 -1
- package/dist/lib/integrations/node-express/index.mjs +2 -2
- package/dist/lib/integrations/node-http/index.d.ts +2 -2
- package/dist/lib/integrations/node-http/index.js +3 -4
- package/dist/lib/integrations/node-http/index.js.map +1 -1
- package/dist/lib/integrations/node-http/index.mjs +1 -1
- package/dist/service-adapters/index.d.ts +47 -3
- package/dist/service-adapters/index.js +708 -442
- package/dist/service-adapters/index.js.map +1 -1
- package/dist/service-adapters/index.mjs +3 -1
- package/package.json +6 -7
- package/src/index.ts +1 -0
- package/src/service-adapters/anthropic/anthropic-adapter.ts +197 -0
- package/src/service-adapters/anthropic/utils.ts +144 -0
- package/src/service-adapters/index.ts +9 -7
- package/dist/chunk-3SKYFYY2.mjs.map +0 -1
- package/dist/chunk-BJ2LVHWA.mjs.map +0 -1
- /package/dist/{chunk-BPEPG56J.mjs.map → chunk-47TPNJX7.mjs.map} +0 -0
- /package/dist/{chunk-Y5TWOZFD.mjs.map → chunk-4BZ6WXBB.mjs.map} +0 -0
- /package/dist/{chunk-UL2OKN2O.mjs.map → chunk-VWS65V7Y.mjs.map} +0 -0
- /package/dist/{chunk-U2EKJP47.mjs.map → chunk-XCGRXAJU.mjs.map} +0 -0
package/dist/lib/index.js
CHANGED
|
@@ -44,7 +44,7 @@ var require_package = __commonJS({
|
|
|
44
44
|
publishConfig: {
|
|
45
45
|
access: "public"
|
|
46
46
|
},
|
|
47
|
-
version: "1.
|
|
47
|
+
version: "1.3.0",
|
|
48
48
|
sideEffects: false,
|
|
49
49
|
main: "./dist/index.js",
|
|
50
50
|
module: "./dist/index.mjs",
|
|
@@ -59,9 +59,7 @@ var require_package = __commonJS({
|
|
|
59
59
|
test: "jest --passWithNoTests",
|
|
60
60
|
"check-types": "tsc --noEmit",
|
|
61
61
|
clean: "rm -rf .turbo && rm -rf node_modules && rm -rf dist && rm -rf .next && rm -rf __snapshots__",
|
|
62
|
-
"generate-graphql-schema": "rm -rf __snapshots__ && ts-node ./scripts/generate-gql-schema.ts"
|
|
63
|
-
"link:global": "pnpm link --global",
|
|
64
|
-
"unlink:global": "pnpm unlink --global"
|
|
62
|
+
"generate-graphql-schema": "rm -rf __snapshots__ && ts-node ./scripts/generate-gql-schema.ts"
|
|
65
63
|
},
|
|
66
64
|
devDependencies: {
|
|
67
65
|
"@swc/core": "1.5.28",
|
|
@@ -80,6 +78,7 @@ var require_package = __commonJS({
|
|
|
80
78
|
typescript: "^5.2.3"
|
|
81
79
|
},
|
|
82
80
|
dependencies: {
|
|
81
|
+
"@anthropic-ai/sdk": "^0.27.3",
|
|
83
82
|
"@copilotkit/shared": "workspace:*",
|
|
84
83
|
"@google/generative-ai": "^0.11.2",
|
|
85
84
|
"@graphql-yoga/plugin-defer-stream": "^3.3.1",
|
|
@@ -143,6 +142,84 @@ module.exports = __toCommonJS(lib_exports);
|
|
|
143
142
|
// src/lib/runtime/copilot-runtime.ts
|
|
144
143
|
var import_shared8 = require("@copilotkit/shared");
|
|
145
144
|
|
|
145
|
+
// src/service-adapters/langchain/langserve.ts
|
|
146
|
+
var import_remote = require("langchain/runnables/remote");
|
|
147
|
+
var RemoteChain = class {
|
|
148
|
+
name;
|
|
149
|
+
description;
|
|
150
|
+
chainUrl;
|
|
151
|
+
parameters;
|
|
152
|
+
parameterType;
|
|
153
|
+
constructor(options) {
|
|
154
|
+
this.name = options.name;
|
|
155
|
+
this.description = options.description;
|
|
156
|
+
this.chainUrl = options.chainUrl;
|
|
157
|
+
this.parameters = options.parameters;
|
|
158
|
+
this.parameterType = options.parameterType || "multi";
|
|
159
|
+
}
|
|
160
|
+
async toAction() {
|
|
161
|
+
if (!this.parameters) {
|
|
162
|
+
await this.inferLangServeParameters();
|
|
163
|
+
}
|
|
164
|
+
return {
|
|
165
|
+
name: this.name,
|
|
166
|
+
description: this.description,
|
|
167
|
+
parameters: this.parameters,
|
|
168
|
+
handler: async (args) => {
|
|
169
|
+
const runnable = new import_remote.RemoteRunnable({
|
|
170
|
+
url: this.chainUrl
|
|
171
|
+
});
|
|
172
|
+
let input;
|
|
173
|
+
if (this.parameterType === "single") {
|
|
174
|
+
input = args[Object.keys(args)[0]];
|
|
175
|
+
} else {
|
|
176
|
+
input = args;
|
|
177
|
+
}
|
|
178
|
+
return await runnable.invoke(input);
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
async inferLangServeParameters() {
|
|
183
|
+
const supportedTypes = [
|
|
184
|
+
"string",
|
|
185
|
+
"number",
|
|
186
|
+
"boolean"
|
|
187
|
+
];
|
|
188
|
+
let schemaUrl = this.chainUrl.replace(/\/+$/, "") + "/input_schema";
|
|
189
|
+
let schema = await fetch(schemaUrl).then((res) => res.json()).catch(() => {
|
|
190
|
+
throw new Error("Failed to fetch langserve schema at " + schemaUrl);
|
|
191
|
+
});
|
|
192
|
+
if (supportedTypes.includes(schema.type)) {
|
|
193
|
+
this.parameterType = "single";
|
|
194
|
+
this.parameters = [
|
|
195
|
+
{
|
|
196
|
+
name: "input",
|
|
197
|
+
type: schema.type,
|
|
198
|
+
description: "The input to the chain"
|
|
199
|
+
}
|
|
200
|
+
];
|
|
201
|
+
} else if (schema.type === "object") {
|
|
202
|
+
this.parameterType = "multi";
|
|
203
|
+
this.parameters = Object.keys(schema.properties).map((key) => {
|
|
204
|
+
var _a;
|
|
205
|
+
let property = schema.properties[key];
|
|
206
|
+
if (!supportedTypes.includes(property.type)) {
|
|
207
|
+
throw new Error("Unsupported schema type");
|
|
208
|
+
}
|
|
209
|
+
return {
|
|
210
|
+
name: key,
|
|
211
|
+
type: property.type,
|
|
212
|
+
description: property.description || "",
|
|
213
|
+
required: ((_a = schema.required) == null ? void 0 : _a.includes(key)) || false
|
|
214
|
+
};
|
|
215
|
+
});
|
|
216
|
+
} else {
|
|
217
|
+
throw new Error("Unsupported schema type");
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
__name(RemoteChain, "RemoteChain");
|
|
222
|
+
|
|
146
223
|
// src/service-adapters/openai/openai-adapter.ts
|
|
147
224
|
var import_openai = __toESM(require("openai"));
|
|
148
225
|
|
|
@@ -443,167 +520,229 @@ var OpenAIAdapter = class {
|
|
|
443
520
|
};
|
|
444
521
|
__name(OpenAIAdapter, "OpenAIAdapter");
|
|
445
522
|
|
|
446
|
-
// src/service-adapters/
|
|
447
|
-
var
|
|
448
|
-
var
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
this.disableParallelToolCalls = (params == null ? void 0 : params.disableParallelToolCalls) || false;
|
|
460
|
-
}
|
|
461
|
-
async process(request) {
|
|
462
|
-
const { messages, actions, eventSource, runId, forwardedParameters } = request;
|
|
463
|
-
let threadId = request.threadId || (await this.openai.beta.threads.create()).id;
|
|
464
|
-
const lastMessage = messages.at(-1);
|
|
465
|
-
let nextRunId = void 0;
|
|
466
|
-
if (lastMessage instanceof ResultMessage && runId) {
|
|
467
|
-
nextRunId = await this.submitToolOutputs(threadId, runId, messages, eventSource);
|
|
468
|
-
} else if (lastMessage instanceof TextMessage) {
|
|
469
|
-
nextRunId = await this.submitUserMessage(threadId, messages, actions, eventSource, forwardedParameters);
|
|
470
|
-
} else {
|
|
471
|
-
throw new Error("No actionable message found in the messages");
|
|
472
|
-
}
|
|
473
|
-
return {
|
|
474
|
-
threadId,
|
|
475
|
-
runId: nextRunId
|
|
476
|
-
};
|
|
477
|
-
}
|
|
478
|
-
async submitToolOutputs(threadId, runId, messages, eventSource) {
|
|
479
|
-
let run = await this.openai.beta.threads.runs.retrieve(threadId, runId);
|
|
480
|
-
if (!run.required_action) {
|
|
481
|
-
throw new Error("No tool outputs required");
|
|
482
|
-
}
|
|
483
|
-
const toolCallsIds = run.required_action.submit_tool_outputs.tool_calls.map((toolCall) => toolCall.id);
|
|
484
|
-
const resultMessages = messages.filter((message) => message instanceof ResultMessage && toolCallsIds.includes(message.actionExecutionId));
|
|
485
|
-
if (toolCallsIds.length != resultMessages.length) {
|
|
486
|
-
throw new Error("Number of function results does not match the number of tool calls");
|
|
487
|
-
}
|
|
488
|
-
const toolOutputs = resultMessages.map((message) => {
|
|
489
|
-
return {
|
|
490
|
-
tool_call_id: message.actionExecutionId,
|
|
491
|
-
output: message.result
|
|
492
|
-
};
|
|
493
|
-
});
|
|
494
|
-
const stream = this.openai.beta.threads.runs.submitToolOutputsStream(threadId, runId, {
|
|
495
|
-
tool_outputs: toolOutputs,
|
|
496
|
-
...this.disableParallelToolCalls && {
|
|
497
|
-
parallel_tool_calls: false
|
|
498
|
-
}
|
|
499
|
-
});
|
|
500
|
-
await this.streamResponse(stream, eventSource);
|
|
501
|
-
return runId;
|
|
502
|
-
}
|
|
503
|
-
async submitUserMessage(threadId, messages, actions, eventSource, forwardedParameters) {
|
|
504
|
-
messages = [
|
|
505
|
-
...messages
|
|
506
|
-
];
|
|
507
|
-
const instructionsMessage = messages.shift();
|
|
508
|
-
const instructions = instructionsMessage instanceof TextMessage ? instructionsMessage.content : "";
|
|
509
|
-
const userMessage = messages.map(convertMessageToOpenAIMessage).map(convertSystemMessageToAssistantAPI).at(-1);
|
|
510
|
-
if (userMessage.role !== "user") {
|
|
511
|
-
throw new Error("No user message found");
|
|
523
|
+
// src/service-adapters/langchain/utils.ts
|
|
524
|
+
var import_messages = require("@langchain/core/messages");
|
|
525
|
+
var import_tools = require("@langchain/core/tools");
|
|
526
|
+
var import_zod = require("zod");
|
|
527
|
+
var import_shared2 = require("@copilotkit/shared");
|
|
528
|
+
function convertMessageToLangChainMessage(message) {
|
|
529
|
+
if (message instanceof TextMessage) {
|
|
530
|
+
if (message.role == "user") {
|
|
531
|
+
return new import_messages.HumanMessage(message.content);
|
|
532
|
+
} else if (message.role == "assistant") {
|
|
533
|
+
return new import_messages.AIMessage(message.content);
|
|
534
|
+
} else if (message.role === "system") {
|
|
535
|
+
return new import_messages.SystemMessage(message.content);
|
|
512
536
|
}
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
content:
|
|
516
|
-
|
|
517
|
-
const openaiTools = actions.map(convertActionInputToOpenAITool);
|
|
518
|
-
const tools = [
|
|
519
|
-
...openaiTools,
|
|
520
|
-
...this.codeInterpreterEnabled ? [
|
|
521
|
-
{
|
|
522
|
-
type: "code_interpreter"
|
|
523
|
-
}
|
|
524
|
-
] : [],
|
|
525
|
-
...this.fileSearchEnabled ? [
|
|
537
|
+
} else if (message instanceof ActionExecutionMessage) {
|
|
538
|
+
return new import_messages.AIMessage({
|
|
539
|
+
content: "",
|
|
540
|
+
tool_calls: [
|
|
526
541
|
{
|
|
527
|
-
|
|
542
|
+
id: message.id,
|
|
543
|
+
args: message.arguments,
|
|
544
|
+
name: message.name
|
|
528
545
|
}
|
|
529
|
-
]
|
|
530
|
-
];
|
|
531
|
-
let stream = this.openai.beta.threads.runs.stream(threadId, {
|
|
532
|
-
assistant_id: this.assistantId,
|
|
533
|
-
instructions,
|
|
534
|
-
tools,
|
|
535
|
-
...(forwardedParameters == null ? void 0 : forwardedParameters.maxTokens) && {
|
|
536
|
-
max_completion_tokens: forwardedParameters.maxTokens
|
|
537
|
-
},
|
|
538
|
-
...this.disableParallelToolCalls && {
|
|
539
|
-
parallel_tool_calls: false
|
|
540
|
-
}
|
|
546
|
+
]
|
|
541
547
|
});
|
|
542
|
-
|
|
543
|
-
return
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
eventSource.stream(async (eventStream$) => {
|
|
547
|
-
var _a, _b, _c, _d, _e, _f;
|
|
548
|
-
let inFunctionCall = false;
|
|
549
|
-
for await (const chunk of stream) {
|
|
550
|
-
switch (chunk.event) {
|
|
551
|
-
case "thread.message.created":
|
|
552
|
-
if (inFunctionCall) {
|
|
553
|
-
eventStream$.sendActionExecutionEnd();
|
|
554
|
-
}
|
|
555
|
-
eventStream$.sendTextMessageStart(chunk.data.id);
|
|
556
|
-
break;
|
|
557
|
-
case "thread.message.delta":
|
|
558
|
-
if (((_a = chunk.data.delta.content) == null ? void 0 : _a[0].type) === "text") {
|
|
559
|
-
eventStream$.sendTextMessageContent((_b = chunk.data.delta.content) == null ? void 0 : _b[0].text.value);
|
|
560
|
-
}
|
|
561
|
-
break;
|
|
562
|
-
case "thread.message.completed":
|
|
563
|
-
eventStream$.sendTextMessageEnd();
|
|
564
|
-
break;
|
|
565
|
-
case "thread.run.step.delta":
|
|
566
|
-
let toolCallId;
|
|
567
|
-
let toolCallName;
|
|
568
|
-
let toolCallArgs;
|
|
569
|
-
if (chunk.data.delta.step_details.type === "tool_calls" && ((_c = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _c[0].type) === "function") {
|
|
570
|
-
toolCallId = (_d = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _d[0].id;
|
|
571
|
-
toolCallName = (_e = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _e[0].function.name;
|
|
572
|
-
toolCallArgs = (_f = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _f[0].function.arguments;
|
|
573
|
-
}
|
|
574
|
-
if (toolCallName && toolCallId) {
|
|
575
|
-
if (inFunctionCall) {
|
|
576
|
-
eventStream$.sendActionExecutionEnd();
|
|
577
|
-
}
|
|
578
|
-
inFunctionCall = true;
|
|
579
|
-
eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
|
|
580
|
-
} else if (toolCallArgs) {
|
|
581
|
-
eventStream$.sendActionExecutionArgs(toolCallArgs);
|
|
582
|
-
}
|
|
583
|
-
break;
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
if (inFunctionCall) {
|
|
587
|
-
eventStream$.sendActionExecutionEnd();
|
|
588
|
-
}
|
|
589
|
-
eventStream$.complete();
|
|
548
|
+
} else if (message instanceof ResultMessage) {
|
|
549
|
+
return new import_messages.ToolMessage({
|
|
550
|
+
content: message.result,
|
|
551
|
+
tool_call_id: message.actionExecutionId
|
|
590
552
|
});
|
|
591
553
|
}
|
|
592
|
-
};
|
|
593
|
-
__name(OpenAIAssistantAdapter, "OpenAIAssistantAdapter");
|
|
594
|
-
function getRunIdFromStream(stream) {
|
|
595
|
-
return new Promise((resolve, reject) => {
|
|
596
|
-
let runIdGetter = /* @__PURE__ */ __name((event) => {
|
|
597
|
-
if (event.event === "thread.run.created") {
|
|
598
|
-
const runId = event.data.id;
|
|
599
|
-
stream.off("event", runIdGetter);
|
|
600
|
-
resolve(runId);
|
|
601
|
-
}
|
|
602
|
-
}, "runIdGetter");
|
|
603
|
-
stream.on("event", runIdGetter);
|
|
604
|
-
});
|
|
605
554
|
}
|
|
606
|
-
__name(
|
|
555
|
+
__name(convertMessageToLangChainMessage, "convertMessageToLangChainMessage");
|
|
556
|
+
function convertJsonSchemaToZodSchema(jsonSchema, required) {
|
|
557
|
+
if (jsonSchema.type === "object") {
|
|
558
|
+
const spec = {};
|
|
559
|
+
for (const [key, value] of Object.entries(jsonSchema.properties)) {
|
|
560
|
+
spec[key] = convertJsonSchemaToZodSchema(value, jsonSchema.required ? jsonSchema.required.includes(key) : false);
|
|
561
|
+
}
|
|
562
|
+
let schema = import_zod.z.object(spec);
|
|
563
|
+
return !required ? schema.optional() : schema;
|
|
564
|
+
} else if (jsonSchema.type === "string") {
|
|
565
|
+
let schema = import_zod.z.string().describe(jsonSchema.description);
|
|
566
|
+
return !required ? schema.optional() : schema;
|
|
567
|
+
} else if (jsonSchema.type === "number") {
|
|
568
|
+
let schema = import_zod.z.number().describe(jsonSchema.description);
|
|
569
|
+
return !required ? schema.optional() : schema;
|
|
570
|
+
} else if (jsonSchema.type === "boolean") {
|
|
571
|
+
let schema = import_zod.z.boolean().describe(jsonSchema.description);
|
|
572
|
+
return !required ? schema.optional() : schema;
|
|
573
|
+
} else if (jsonSchema.type === "array") {
|
|
574
|
+
let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, false);
|
|
575
|
+
let schema = import_zod.z.array(itemSchema);
|
|
576
|
+
return !required ? schema.optional() : schema;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
__name(convertJsonSchemaToZodSchema, "convertJsonSchemaToZodSchema");
|
|
580
|
+
function convertActionInputToLangChainTool(actionInput) {
|
|
581
|
+
return new import_tools.DynamicStructuredTool({
|
|
582
|
+
name: actionInput.name,
|
|
583
|
+
description: actionInput.description,
|
|
584
|
+
schema: convertJsonSchemaToZodSchema(JSON.parse(actionInput.jsonSchema), true),
|
|
585
|
+
func: async () => {
|
|
586
|
+
return "";
|
|
587
|
+
}
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
__name(convertActionInputToLangChainTool, "convertActionInputToLangChainTool");
|
|
591
|
+
function getConstructorName(object) {
|
|
592
|
+
if (object && typeof object === "object" && object.constructor && object.constructor.name) {
|
|
593
|
+
return object.constructor.name;
|
|
594
|
+
}
|
|
595
|
+
return "";
|
|
596
|
+
}
|
|
597
|
+
__name(getConstructorName, "getConstructorName");
|
|
598
|
+
function isAIMessage(message) {
|
|
599
|
+
return getConstructorName(message) === "AIMessage";
|
|
600
|
+
}
|
|
601
|
+
__name(isAIMessage, "isAIMessage");
|
|
602
|
+
function isAIMessageChunk(message) {
|
|
603
|
+
return getConstructorName(message) === "AIMessageChunk";
|
|
604
|
+
}
|
|
605
|
+
__name(isAIMessageChunk, "isAIMessageChunk");
|
|
606
|
+
function isBaseMessageChunk(message) {
|
|
607
|
+
return getConstructorName(message) === "BaseMessageChunk";
|
|
608
|
+
}
|
|
609
|
+
__name(isBaseMessageChunk, "isBaseMessageChunk");
|
|
610
|
+
function maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution) {
|
|
611
|
+
if (actionExecution) {
|
|
612
|
+
eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, "Sending a message");
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
__name(maybeSendActionExecutionResultIsMessage, "maybeSendActionExecutionResultIsMessage");
|
|
616
|
+
async function streamLangChainResponse({ result, eventStream$, actionExecution }) {
|
|
617
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
618
|
+
if (typeof result === "string") {
|
|
619
|
+
if (!actionExecution) {
|
|
620
|
+
eventStream$.sendTextMessage((0, import_shared2.randomId)(), result);
|
|
621
|
+
} else {
|
|
622
|
+
eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, result);
|
|
623
|
+
}
|
|
624
|
+
} else if (isAIMessage(result)) {
|
|
625
|
+
maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
|
|
626
|
+
if (result.content) {
|
|
627
|
+
eventStream$.sendTextMessage((0, import_shared2.randomId)(), result.content);
|
|
628
|
+
}
|
|
629
|
+
for (const toolCall of result.tool_calls) {
|
|
630
|
+
eventStream$.sendActionExecution(toolCall.id || (0, import_shared2.randomId)(), toolCall.name, JSON.stringify(toolCall.args));
|
|
631
|
+
}
|
|
632
|
+
} else if (isBaseMessageChunk(result)) {
|
|
633
|
+
maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
|
|
634
|
+
if ((_a = result.lc_kwargs) == null ? void 0 : _a.content) {
|
|
635
|
+
eventStream$.sendTextMessage((0, import_shared2.randomId)(), result.content);
|
|
636
|
+
}
|
|
637
|
+
if ((_b = result.lc_kwargs) == null ? void 0 : _b.tool_calls) {
|
|
638
|
+
for (const toolCall of (_c = result.lc_kwargs) == null ? void 0 : _c.tool_calls) {
|
|
639
|
+
eventStream$.sendActionExecution(toolCall.id || (0, import_shared2.randomId)(), toolCall.name, JSON.stringify(toolCall.args));
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
} else if (result && "getReader" in result) {
|
|
643
|
+
maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
|
|
644
|
+
let reader = result.getReader();
|
|
645
|
+
let mode = null;
|
|
646
|
+
while (true) {
|
|
647
|
+
try {
|
|
648
|
+
const { done, value } = await reader.read();
|
|
649
|
+
let toolCallName = void 0;
|
|
650
|
+
let toolCallId = void 0;
|
|
651
|
+
let toolCallArgs = void 0;
|
|
652
|
+
let hasToolCall = false;
|
|
653
|
+
let content = value == null ? void 0 : value.content;
|
|
654
|
+
if (isAIMessageChunk(value)) {
|
|
655
|
+
let chunk = (_d = value.tool_call_chunks) == null ? void 0 : _d[0];
|
|
656
|
+
toolCallName = chunk == null ? void 0 : chunk.name;
|
|
657
|
+
toolCallId = chunk == null ? void 0 : chunk.id;
|
|
658
|
+
toolCallArgs = chunk == null ? void 0 : chunk.args;
|
|
659
|
+
hasToolCall = chunk != void 0;
|
|
660
|
+
} else if (isBaseMessageChunk(value)) {
|
|
661
|
+
let chunk = (_f = (_e = value.additional_kwargs) == null ? void 0 : _e.tool_calls) == null ? void 0 : _f[0];
|
|
662
|
+
toolCallName = (_g = chunk == null ? void 0 : chunk.function) == null ? void 0 : _g.name;
|
|
663
|
+
toolCallId = chunk == null ? void 0 : chunk.id;
|
|
664
|
+
toolCallArgs = (_h = chunk == null ? void 0 : chunk.function) == null ? void 0 : _h.arguments;
|
|
665
|
+
hasToolCall = (chunk == null ? void 0 : chunk.function) != void 0;
|
|
666
|
+
}
|
|
667
|
+
if (mode === "message" && (toolCallId || done)) {
|
|
668
|
+
mode = null;
|
|
669
|
+
eventStream$.sendTextMessageEnd();
|
|
670
|
+
} else if (mode === "function" && (!hasToolCall || done)) {
|
|
671
|
+
mode = null;
|
|
672
|
+
eventStream$.sendActionExecutionEnd();
|
|
673
|
+
}
|
|
674
|
+
if (done) {
|
|
675
|
+
break;
|
|
676
|
+
}
|
|
677
|
+
if (mode === null) {
|
|
678
|
+
if (hasToolCall) {
|
|
679
|
+
mode = "function";
|
|
680
|
+
eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
|
|
681
|
+
} else if (content) {
|
|
682
|
+
mode = "message";
|
|
683
|
+
eventStream$.sendTextMessageStart((0, import_shared2.randomId)());
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
if (mode === "message" && content) {
|
|
687
|
+
eventStream$.sendTextMessageContent(content);
|
|
688
|
+
} else if (mode === "function" && toolCallArgs) {
|
|
689
|
+
eventStream$.sendActionExecutionArgs(toolCallArgs);
|
|
690
|
+
}
|
|
691
|
+
} catch (error) {
|
|
692
|
+
console.error("Error reading from stream", error);
|
|
693
|
+
break;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
} else if (actionExecution) {
|
|
697
|
+
eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, encodeResult(result));
|
|
698
|
+
} else {
|
|
699
|
+
throw new Error("Invalid return type from LangChain function.");
|
|
700
|
+
}
|
|
701
|
+
eventStream$.complete();
|
|
702
|
+
}
|
|
703
|
+
__name(streamLangChainResponse, "streamLangChainResponse");
|
|
704
|
+
function encodeResult(result) {
|
|
705
|
+
if (result === void 0) {
|
|
706
|
+
return "";
|
|
707
|
+
} else if (typeof result === "string") {
|
|
708
|
+
return result;
|
|
709
|
+
} else {
|
|
710
|
+
return JSON.stringify(result);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
__name(encodeResult, "encodeResult");
|
|
714
|
+
|
|
715
|
+
// src/service-adapters/langchain/langchain-adapter.ts
|
|
716
|
+
var import_shared3 = require("@copilotkit/shared");
|
|
717
|
+
var LangChainAdapter = class {
|
|
718
|
+
options;
|
|
719
|
+
/**
|
|
720
|
+
* To use LangChain as a backend, provide a handler function to the adapter with your custom LangChain logic.
|
|
721
|
+
*/
|
|
722
|
+
constructor(options) {
|
|
723
|
+
this.options = options;
|
|
724
|
+
}
|
|
725
|
+
async process(request) {
|
|
726
|
+
const { eventSource, model, actions, messages, threadId, runId } = request;
|
|
727
|
+
const result = await this.options.chainFn({
|
|
728
|
+
messages: messages.map(convertMessageToLangChainMessage),
|
|
729
|
+
tools: actions.map(convertActionInputToLangChainTool),
|
|
730
|
+
model,
|
|
731
|
+
threadId,
|
|
732
|
+
runId
|
|
733
|
+
});
|
|
734
|
+
eventSource.stream(async (eventStream$) => {
|
|
735
|
+
await streamLangChainResponse({
|
|
736
|
+
result,
|
|
737
|
+
eventStream$
|
|
738
|
+
});
|
|
739
|
+
});
|
|
740
|
+
return {
|
|
741
|
+
threadId: threadId || (0, import_shared3.randomId)()
|
|
742
|
+
};
|
|
743
|
+
}
|
|
744
|
+
};
|
|
745
|
+
__name(LangChainAdapter, "LangChainAdapter");
|
|
607
746
|
|
|
608
747
|
// src/service-adapters/google/google-genai-adapter.ts
|
|
609
748
|
var import_generative_ai = require("@google/generative-ai");
|
|
@@ -694,7 +833,7 @@ function tryParseJson(str) {
|
|
|
694
833
|
__name(tryParseJson, "tryParseJson");
|
|
695
834
|
|
|
696
835
|
// src/service-adapters/google/google-genai-adapter.ts
|
|
697
|
-
var
|
|
836
|
+
var import_shared4 = require("@copilotkit/shared");
|
|
698
837
|
var GoogleGenerativeAIAdapter = class {
|
|
699
838
|
model;
|
|
700
839
|
constructor(options) {
|
|
@@ -761,7 +900,7 @@ var GoogleGenerativeAIAdapter = class {
|
|
|
761
900
|
}
|
|
762
901
|
if (!isTextMessage) {
|
|
763
902
|
isTextMessage = true;
|
|
764
|
-
eventStream$.sendTextMessageStart((0,
|
|
903
|
+
eventStream$.sendTextMessageStart((0, import_shared4.randomId)());
|
|
765
904
|
}
|
|
766
905
|
eventStream$.sendTextMessageContent(chunkText);
|
|
767
906
|
}
|
|
@@ -771,13 +910,13 @@ var GoogleGenerativeAIAdapter = class {
|
|
|
771
910
|
let calls = (await result.response).functionCalls();
|
|
772
911
|
if (calls) {
|
|
773
912
|
for (let call of calls) {
|
|
774
|
-
eventStream$.sendActionExecution((0,
|
|
913
|
+
eventStream$.sendActionExecution((0, import_shared4.randomId)(), call.name, JSON.stringify(replaceNewlinesInObject(call.args)));
|
|
775
914
|
}
|
|
776
915
|
}
|
|
777
916
|
eventStream$.complete();
|
|
778
917
|
});
|
|
779
918
|
return {
|
|
780
|
-
threadId: request.threadId || (0,
|
|
919
|
+
threadId: request.threadId || (0, import_shared4.randomId)()
|
|
781
920
|
};
|
|
782
921
|
}
|
|
783
922
|
};
|
|
@@ -800,307 +939,167 @@ function replaceNewlinesInObject(obj) {
|
|
|
800
939
|
}
|
|
801
940
|
__name(replaceNewlinesInObject, "replaceNewlinesInObject");
|
|
802
941
|
|
|
803
|
-
// src/service-adapters/
|
|
804
|
-
var
|
|
805
|
-
var
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
} else if (message instanceof ActionExecutionMessage) {
|
|
818
|
-
return new import_messages.AIMessage({
|
|
819
|
-
content: "",
|
|
820
|
-
tool_calls: [
|
|
821
|
-
{
|
|
822
|
-
id: message.id,
|
|
823
|
-
args: message.arguments,
|
|
824
|
-
name: message.name
|
|
825
|
-
}
|
|
826
|
-
]
|
|
827
|
-
});
|
|
828
|
-
} else if (message instanceof ResultMessage) {
|
|
829
|
-
return new import_messages.ToolMessage({
|
|
830
|
-
content: message.result,
|
|
831
|
-
tool_call_id: message.actionExecutionId
|
|
832
|
-
});
|
|
942
|
+
// src/service-adapters/openai/openai-assistant-adapter.ts
|
|
943
|
+
var import_openai2 = __toESM(require("openai"));
|
|
944
|
+
var OpenAIAssistantAdapter = class {
|
|
945
|
+
openai;
|
|
946
|
+
codeInterpreterEnabled;
|
|
947
|
+
assistantId;
|
|
948
|
+
fileSearchEnabled;
|
|
949
|
+
disableParallelToolCalls;
|
|
950
|
+
constructor(params) {
|
|
951
|
+
this.openai = params.openai || new import_openai2.default({});
|
|
952
|
+
this.codeInterpreterEnabled = params.codeInterpreterEnabled === false || true;
|
|
953
|
+
this.fileSearchEnabled = params.fileSearchEnabled === false || true;
|
|
954
|
+
this.assistantId = params.assistantId;
|
|
955
|
+
this.disableParallelToolCalls = (params == null ? void 0 : params.disableParallelToolCalls) || false;
|
|
833
956
|
}
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
957
|
+
async process(request) {
|
|
958
|
+
const { messages, actions, eventSource, runId, forwardedParameters } = request;
|
|
959
|
+
let threadId = request.threadId || (await this.openai.beta.threads.create()).id;
|
|
960
|
+
const lastMessage = messages.at(-1);
|
|
961
|
+
let nextRunId = void 0;
|
|
962
|
+
if (lastMessage instanceof ResultMessage && runId) {
|
|
963
|
+
nextRunId = await this.submitToolOutputs(threadId, runId, messages, eventSource);
|
|
964
|
+
} else if (lastMessage instanceof TextMessage) {
|
|
965
|
+
nextRunId = await this.submitUserMessage(threadId, messages, actions, eventSource, forwardedParameters);
|
|
966
|
+
} else {
|
|
967
|
+
throw new Error("No actionable message found in the messages");
|
|
841
968
|
}
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
return !required ? schema.optional() : schema;
|
|
847
|
-
} else if (jsonSchema.type === "number") {
|
|
848
|
-
let schema = import_zod.z.number().describe(jsonSchema.description);
|
|
849
|
-
return !required ? schema.optional() : schema;
|
|
850
|
-
} else if (jsonSchema.type === "boolean") {
|
|
851
|
-
let schema = import_zod.z.boolean().describe(jsonSchema.description);
|
|
852
|
-
return !required ? schema.optional() : schema;
|
|
853
|
-
} else if (jsonSchema.type === "array") {
|
|
854
|
-
let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, false);
|
|
855
|
-
let schema = import_zod.z.array(itemSchema);
|
|
856
|
-
return !required ? schema.optional() : schema;
|
|
857
|
-
}
|
|
858
|
-
}
|
|
859
|
-
__name(convertJsonSchemaToZodSchema, "convertJsonSchemaToZodSchema");
|
|
860
|
-
function convertActionInputToLangChainTool(actionInput) {
|
|
861
|
-
return new import_tools.DynamicStructuredTool({
|
|
862
|
-
name: actionInput.name,
|
|
863
|
-
description: actionInput.description,
|
|
864
|
-
schema: convertJsonSchemaToZodSchema(JSON.parse(actionInput.jsonSchema), true),
|
|
865
|
-
func: async () => {
|
|
866
|
-
return "";
|
|
867
|
-
}
|
|
868
|
-
});
|
|
869
|
-
}
|
|
870
|
-
__name(convertActionInputToLangChainTool, "convertActionInputToLangChainTool");
|
|
871
|
-
function getConstructorName(object) {
|
|
872
|
-
if (object && typeof object === "object" && object.constructor && object.constructor.name) {
|
|
873
|
-
return object.constructor.name;
|
|
874
|
-
}
|
|
875
|
-
return "";
|
|
876
|
-
}
|
|
877
|
-
__name(getConstructorName, "getConstructorName");
|
|
878
|
-
function isAIMessage(message) {
|
|
879
|
-
return getConstructorName(message) === "AIMessage";
|
|
880
|
-
}
|
|
881
|
-
__name(isAIMessage, "isAIMessage");
|
|
882
|
-
function isAIMessageChunk(message) {
|
|
883
|
-
return getConstructorName(message) === "AIMessageChunk";
|
|
884
|
-
}
|
|
885
|
-
__name(isAIMessageChunk, "isAIMessageChunk");
|
|
886
|
-
function isBaseMessageChunk(message) {
|
|
887
|
-
return getConstructorName(message) === "BaseMessageChunk";
|
|
888
|
-
}
|
|
889
|
-
__name(isBaseMessageChunk, "isBaseMessageChunk");
|
|
890
|
-
function maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution) {
|
|
891
|
-
if (actionExecution) {
|
|
892
|
-
eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, "Sending a message");
|
|
969
|
+
return {
|
|
970
|
+
threadId,
|
|
971
|
+
runId: nextRunId
|
|
972
|
+
};
|
|
893
973
|
}
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
if (typeof result === "string") {
|
|
899
|
-
if (!actionExecution) {
|
|
900
|
-
eventStream$.sendTextMessage((0, import_shared3.randomId)(), result);
|
|
901
|
-
} else {
|
|
902
|
-
eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, result);
|
|
903
|
-
}
|
|
904
|
-
} else if (isAIMessage(result)) {
|
|
905
|
-
maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
|
|
906
|
-
if (result.content) {
|
|
907
|
-
eventStream$.sendTextMessage((0, import_shared3.randomId)(), result.content);
|
|
908
|
-
}
|
|
909
|
-
for (const toolCall of result.tool_calls) {
|
|
910
|
-
eventStream$.sendActionExecution(toolCall.id || (0, import_shared3.randomId)(), toolCall.name, JSON.stringify(toolCall.args));
|
|
974
|
+
async submitToolOutputs(threadId, runId, messages, eventSource) {
|
|
975
|
+
let run = await this.openai.beta.threads.runs.retrieve(threadId, runId);
|
|
976
|
+
if (!run.required_action) {
|
|
977
|
+
throw new Error("No tool outputs required");
|
|
911
978
|
}
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
if (
|
|
915
|
-
|
|
979
|
+
const toolCallsIds = run.required_action.submit_tool_outputs.tool_calls.map((toolCall) => toolCall.id);
|
|
980
|
+
const resultMessages = messages.filter((message) => message instanceof ResultMessage && toolCallsIds.includes(message.actionExecutionId));
|
|
981
|
+
if (toolCallsIds.length != resultMessages.length) {
|
|
982
|
+
throw new Error("Number of function results does not match the number of tool calls");
|
|
916
983
|
}
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
984
|
+
const toolOutputs = resultMessages.map((message) => {
|
|
985
|
+
return {
|
|
986
|
+
tool_call_id: message.actionExecutionId,
|
|
987
|
+
output: message.result
|
|
988
|
+
};
|
|
989
|
+
});
|
|
990
|
+
const stream = this.openai.beta.threads.runs.submitToolOutputsStream(threadId, runId, {
|
|
991
|
+
tool_outputs: toolOutputs,
|
|
992
|
+
...this.disableParallelToolCalls && {
|
|
993
|
+
parallel_tool_calls: false
|
|
920
994
|
}
|
|
995
|
+
});
|
|
996
|
+
await this.streamResponse(stream, eventSource);
|
|
997
|
+
return runId;
|
|
998
|
+
}
|
|
999
|
+
async submitUserMessage(threadId, messages, actions, eventSource, forwardedParameters) {
|
|
1000
|
+
messages = [
|
|
1001
|
+
...messages
|
|
1002
|
+
];
|
|
1003
|
+
const instructionsMessage = messages.shift();
|
|
1004
|
+
const instructions = instructionsMessage instanceof TextMessage ? instructionsMessage.content : "";
|
|
1005
|
+
const userMessage = messages.map(convertMessageToOpenAIMessage).map(convertSystemMessageToAssistantAPI).at(-1);
|
|
1006
|
+
if (userMessage.role !== "user") {
|
|
1007
|
+
throw new Error("No user message found");
|
|
921
1008
|
}
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
let hasToolCall = false;
|
|
933
|
-
let content = value == null ? void 0 : value.content;
|
|
934
|
-
if (isAIMessageChunk(value)) {
|
|
935
|
-
let chunk = (_d = value.tool_call_chunks) == null ? void 0 : _d[0];
|
|
936
|
-
toolCallName = chunk == null ? void 0 : chunk.name;
|
|
937
|
-
toolCallId = chunk == null ? void 0 : chunk.id;
|
|
938
|
-
toolCallArgs = chunk == null ? void 0 : chunk.args;
|
|
939
|
-
hasToolCall = chunk != void 0;
|
|
940
|
-
} else if (isBaseMessageChunk(value)) {
|
|
941
|
-
let chunk = (_f = (_e = value.additional_kwargs) == null ? void 0 : _e.tool_calls) == null ? void 0 : _f[0];
|
|
942
|
-
toolCallName = (_g = chunk == null ? void 0 : chunk.function) == null ? void 0 : _g.name;
|
|
943
|
-
toolCallId = chunk == null ? void 0 : chunk.id;
|
|
944
|
-
toolCallArgs = (_h = chunk == null ? void 0 : chunk.function) == null ? void 0 : _h.arguments;
|
|
945
|
-
hasToolCall = (chunk == null ? void 0 : chunk.function) != void 0;
|
|
946
|
-
}
|
|
947
|
-
if (mode === "message" && (toolCallId || done)) {
|
|
948
|
-
mode = null;
|
|
949
|
-
eventStream$.sendTextMessageEnd();
|
|
950
|
-
} else if (mode === "function" && (!hasToolCall || done)) {
|
|
951
|
-
mode = null;
|
|
952
|
-
eventStream$.sendActionExecutionEnd();
|
|
953
|
-
}
|
|
954
|
-
if (done) {
|
|
955
|
-
break;
|
|
956
|
-
}
|
|
957
|
-
if (mode === null) {
|
|
958
|
-
if (hasToolCall) {
|
|
959
|
-
mode = "function";
|
|
960
|
-
eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
|
|
961
|
-
} else if (content) {
|
|
962
|
-
mode = "message";
|
|
963
|
-
eventStream$.sendTextMessageStart((0, import_shared3.randomId)());
|
|
964
|
-
}
|
|
1009
|
+
await this.openai.beta.threads.messages.create(threadId, {
|
|
1010
|
+
role: "user",
|
|
1011
|
+
content: userMessage.content
|
|
1012
|
+
});
|
|
1013
|
+
const openaiTools = actions.map(convertActionInputToOpenAITool);
|
|
1014
|
+
const tools = [
|
|
1015
|
+
...openaiTools,
|
|
1016
|
+
...this.codeInterpreterEnabled ? [
|
|
1017
|
+
{
|
|
1018
|
+
type: "code_interpreter"
|
|
965
1019
|
}
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
1020
|
+
] : [],
|
|
1021
|
+
...this.fileSearchEnabled ? [
|
|
1022
|
+
{
|
|
1023
|
+
type: "file_search"
|
|
970
1024
|
}
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
1025
|
+
] : []
|
|
1026
|
+
];
|
|
1027
|
+
let stream = this.openai.beta.threads.runs.stream(threadId, {
|
|
1028
|
+
assistant_id: this.assistantId,
|
|
1029
|
+
instructions,
|
|
1030
|
+
tools,
|
|
1031
|
+
...(forwardedParameters == null ? void 0 : forwardedParameters.maxTokens) && {
|
|
1032
|
+
max_completion_tokens: forwardedParameters.maxTokens
|
|
1033
|
+
},
|
|
1034
|
+
...this.disableParallelToolCalls && {
|
|
1035
|
+
parallel_tool_calls: false
|
|
974
1036
|
}
|
|
975
|
-
}
|
|
976
|
-
} else if (actionExecution) {
|
|
977
|
-
eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, encodeResult(result));
|
|
978
|
-
} else {
|
|
979
|
-
throw new Error("Invalid return type from LangChain function.");
|
|
980
|
-
}
|
|
981
|
-
eventStream$.complete();
|
|
982
|
-
}
|
|
983
|
-
__name(streamLangChainResponse, "streamLangChainResponse");
|
|
984
|
-
function encodeResult(result) {
|
|
985
|
-
if (result === void 0) {
|
|
986
|
-
return "";
|
|
987
|
-
} else if (typeof result === "string") {
|
|
988
|
-
return result;
|
|
989
|
-
} else {
|
|
990
|
-
return JSON.stringify(result);
|
|
991
|
-
}
|
|
992
|
-
}
|
|
993
|
-
__name(encodeResult, "encodeResult");
|
|
994
|
-
|
|
995
|
-
// src/service-adapters/langchain/langchain-adapter.ts
|
|
996
|
-
var import_shared4 = require("@copilotkit/shared");
|
|
997
|
-
var LangChainAdapter = class {
|
|
998
|
-
options;
|
|
999
|
-
/**
|
|
1000
|
-
* To use LangChain as a backend, provide a handler function to the adapter with your custom LangChain logic.
|
|
1001
|
-
*/
|
|
1002
|
-
constructor(options) {
|
|
1003
|
-
this.options = options;
|
|
1004
|
-
}
|
|
1005
|
-
async process(request) {
|
|
1006
|
-
const { eventSource, model, actions, messages, threadId, runId } = request;
|
|
1007
|
-
const result = await this.options.chainFn({
|
|
1008
|
-
messages: messages.map(convertMessageToLangChainMessage),
|
|
1009
|
-
tools: actions.map(convertActionInputToLangChainTool),
|
|
1010
|
-
model,
|
|
1011
|
-
threadId,
|
|
1012
|
-
runId
|
|
1013
1037
|
});
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
result,
|
|
1017
|
-
eventStream$
|
|
1018
|
-
});
|
|
1019
|
-
});
|
|
1020
|
-
return {
|
|
1021
|
-
threadId: threadId || (0, import_shared4.randomId)()
|
|
1022
|
-
};
|
|
1023
|
-
}
|
|
1024
|
-
};
|
|
1025
|
-
__name(LangChainAdapter, "LangChainAdapter");
|
|
1026
|
-
|
|
1027
|
-
// src/service-adapters/langchain/langserve.ts
|
|
1028
|
-
var import_remote = require("langchain/runnables/remote");
|
|
1029
|
-
var RemoteChain = class {
|
|
1030
|
-
name;
|
|
1031
|
-
description;
|
|
1032
|
-
chainUrl;
|
|
1033
|
-
parameters;
|
|
1034
|
-
parameterType;
|
|
1035
|
-
constructor(options) {
|
|
1036
|
-
this.name = options.name;
|
|
1037
|
-
this.description = options.description;
|
|
1038
|
-
this.chainUrl = options.chainUrl;
|
|
1039
|
-
this.parameters = options.parameters;
|
|
1040
|
-
this.parameterType = options.parameterType || "multi";
|
|
1038
|
+
await this.streamResponse(stream, eventSource);
|
|
1039
|
+
return getRunIdFromStream(stream);
|
|
1041
1040
|
}
|
|
1042
|
-
async
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1041
|
+
async streamResponse(stream, eventSource) {
|
|
1042
|
+
eventSource.stream(async (eventStream$) => {
|
|
1043
|
+
var _a, _b, _c, _d, _e, _f;
|
|
1044
|
+
let inFunctionCall = false;
|
|
1045
|
+
for await (const chunk of stream) {
|
|
1046
|
+
switch (chunk.event) {
|
|
1047
|
+
case "thread.message.created":
|
|
1048
|
+
if (inFunctionCall) {
|
|
1049
|
+
eventStream$.sendActionExecutionEnd();
|
|
1050
|
+
}
|
|
1051
|
+
eventStream$.sendTextMessageStart(chunk.data.id);
|
|
1052
|
+
break;
|
|
1053
|
+
case "thread.message.delta":
|
|
1054
|
+
if (((_a = chunk.data.delta.content) == null ? void 0 : _a[0].type) === "text") {
|
|
1055
|
+
eventStream$.sendTextMessageContent((_b = chunk.data.delta.content) == null ? void 0 : _b[0].text.value);
|
|
1056
|
+
}
|
|
1057
|
+
break;
|
|
1058
|
+
case "thread.message.completed":
|
|
1059
|
+
eventStream$.sendTextMessageEnd();
|
|
1060
|
+
break;
|
|
1061
|
+
case "thread.run.step.delta":
|
|
1062
|
+
let toolCallId;
|
|
1063
|
+
let toolCallName;
|
|
1064
|
+
let toolCallArgs;
|
|
1065
|
+
if (chunk.data.delta.step_details.type === "tool_calls" && ((_c = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _c[0].type) === "function") {
|
|
1066
|
+
toolCallId = (_d = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _d[0].id;
|
|
1067
|
+
toolCallName = (_e = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _e[0].function.name;
|
|
1068
|
+
toolCallArgs = (_f = chunk.data.delta.step_details.tool_calls) == null ? void 0 : _f[0].function.arguments;
|
|
1069
|
+
}
|
|
1070
|
+
if (toolCallName && toolCallId) {
|
|
1071
|
+
if (inFunctionCall) {
|
|
1072
|
+
eventStream$.sendActionExecutionEnd();
|
|
1073
|
+
}
|
|
1074
|
+
inFunctionCall = true;
|
|
1075
|
+
eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
|
|
1076
|
+
} else if (toolCallArgs) {
|
|
1077
|
+
eventStream$.sendActionExecutionArgs(toolCallArgs);
|
|
1078
|
+
}
|
|
1079
|
+
break;
|
|
1059
1080
|
}
|
|
1060
|
-
return await runnable.invoke(input);
|
|
1061
1081
|
}
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
"string",
|
|
1067
|
-
"number",
|
|
1068
|
-
"boolean"
|
|
1069
|
-
];
|
|
1070
|
-
let schemaUrl = this.chainUrl.replace(/\/+$/, "") + "/input_schema";
|
|
1071
|
-
let schema = await fetch(schemaUrl).then((res) => res.json()).catch(() => {
|
|
1072
|
-
throw new Error("Failed to fetch langserve schema at " + schemaUrl);
|
|
1082
|
+
if (inFunctionCall) {
|
|
1083
|
+
eventStream$.sendActionExecutionEnd();
|
|
1084
|
+
}
|
|
1085
|
+
eventStream$.complete();
|
|
1073
1086
|
});
|
|
1074
|
-
if (supportedTypes.includes(schema.type)) {
|
|
1075
|
-
this.parameterType = "single";
|
|
1076
|
-
this.parameters = [
|
|
1077
|
-
{
|
|
1078
|
-
name: "input",
|
|
1079
|
-
type: schema.type,
|
|
1080
|
-
description: "The input to the chain"
|
|
1081
|
-
}
|
|
1082
|
-
];
|
|
1083
|
-
} else if (schema.type === "object") {
|
|
1084
|
-
this.parameterType = "multi";
|
|
1085
|
-
this.parameters = Object.keys(schema.properties).map((key) => {
|
|
1086
|
-
var _a;
|
|
1087
|
-
let property = schema.properties[key];
|
|
1088
|
-
if (!supportedTypes.includes(property.type)) {
|
|
1089
|
-
throw new Error("Unsupported schema type");
|
|
1090
|
-
}
|
|
1091
|
-
return {
|
|
1092
|
-
name: key,
|
|
1093
|
-
type: property.type,
|
|
1094
|
-
description: property.description || "",
|
|
1095
|
-
required: ((_a = schema.required) == null ? void 0 : _a.includes(key)) || false
|
|
1096
|
-
};
|
|
1097
|
-
});
|
|
1098
|
-
} else {
|
|
1099
|
-
throw new Error("Unsupported schema type");
|
|
1100
|
-
}
|
|
1101
1087
|
}
|
|
1102
1088
|
};
|
|
1103
|
-
__name(
|
|
1089
|
+
__name(OpenAIAssistantAdapter, "OpenAIAssistantAdapter");
|
|
1090
|
+
function getRunIdFromStream(stream) {
|
|
1091
|
+
return new Promise((resolve, reject) => {
|
|
1092
|
+
let runIdGetter = /* @__PURE__ */ __name((event) => {
|
|
1093
|
+
if (event.event === "thread.run.created") {
|
|
1094
|
+
const runId = event.data.id;
|
|
1095
|
+
stream.off("event", runIdGetter);
|
|
1096
|
+
resolve(runId);
|
|
1097
|
+
}
|
|
1098
|
+
}, "runIdGetter");
|
|
1099
|
+
stream.on("event", runIdGetter);
|
|
1100
|
+
});
|
|
1101
|
+
}
|
|
1102
|
+
__name(getRunIdFromStream, "getRunIdFromStream");
|
|
1104
1103
|
|
|
1105
1104
|
// src/service-adapters/unify/unify-adapter.ts
|
|
1106
1105
|
var import_openai3 = __toESM(require("openai"));
|