@copilotkit/runtime 1.4.0-pre-1-4-0.13 → 1.4.0-pre.16
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 +32 -0
- package/dist/{chunk-AQBZWBJD.mjs → chunk-2MEJMEYX.mjs} +2 -2
- package/dist/{chunk-CBZV4PPM.mjs → chunk-5HOOAMQI.mjs} +2 -2
- package/dist/{chunk-P4EZLCB6.mjs → chunk-BNSMQJEH.mjs} +2 -2
- package/dist/{chunk-NKGWZWKJ.mjs → chunk-SQKHB55U.mjs} +33 -76
- package/dist/chunk-SQKHB55U.mjs.map +1 -0
- package/dist/{chunk-56IQ6PGC.mjs → chunk-ZOC3UIQP.mjs} +56 -213
- package/dist/chunk-ZOC3UIQP.mjs.map +1 -0
- package/dist/{copilot-runtime-d9ef5d8c.d.ts → copilot-runtime-335a610d.d.ts} +1 -1
- package/dist/{groq-adapter-192d2413.d.ts → groq-adapter-b6c2b7ec.d.ts} +6 -16
- package/dist/index.d.ts +3 -3
- package/dist/index.js +85 -285
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +5 -5
- package/dist/{langserve-878c62b9.d.ts → langserve-cc06e76e.d.ts} +9 -45
- package/dist/lib/index.d.ts +3 -3
- package/dist/lib/index.js +79 -264
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +5 -5
- package/dist/lib/integrations/index.d.ts +3 -3
- package/dist/lib/integrations/index.js +3 -11
- package/dist/lib/integrations/index.js.map +1 -1
- package/dist/lib/integrations/index.mjs +5 -5
- package/dist/lib/integrations/nest/index.d.ts +2 -2
- package/dist/lib/integrations/nest/index.js +3 -11
- package/dist/lib/integrations/nest/index.js.map +1 -1
- package/dist/lib/integrations/nest/index.mjs +3 -3
- package/dist/lib/integrations/node-express/index.d.ts +2 -2
- package/dist/lib/integrations/node-express/index.js +3 -11
- package/dist/lib/integrations/node-express/index.js.map +1 -1
- package/dist/lib/integrations/node-express/index.mjs +3 -3
- package/dist/lib/integrations/node-http/index.d.ts +2 -2
- package/dist/lib/integrations/node-http/index.js +3 -11
- package/dist/lib/integrations/node-http/index.js.map +1 -1
- package/dist/lib/integrations/node-http/index.mjs +2 -2
- package/dist/service-adapters/index.d.ts +4 -6
- package/dist/service-adapters/index.js +55 -212
- package/dist/service-adapters/index.js.map +1 -1
- package/dist/service-adapters/index.mjs +1 -1
- package/package.json +4 -4
- package/src/agents/langgraph/event-source.ts +0 -12
- package/src/graphql/resolvers/copilot.resolver.ts +2 -28
- package/src/service-adapters/anthropic/anthropic-adapter.ts +7 -18
- package/src/service-adapters/events.ts +35 -73
- package/src/service-adapters/experimental/ollama/ollama-adapter.ts +3 -7
- package/src/service-adapters/google/google-genai-adapter.ts +1 -3
- package/src/service-adapters/groq/groq-adapter.ts +9 -25
- package/src/service-adapters/langchain/langchain-adapter.ts +1 -3
- package/src/service-adapters/langchain/utils.ts +31 -47
- package/src/service-adapters/openai/openai-adapter.ts +11 -27
- package/src/service-adapters/openai/openai-assistant-adapter.ts +9 -24
- package/src/service-adapters/unify/unify-adapter.ts +11 -28
- package/dist/chunk-56IQ6PGC.mjs.map +0 -1
- package/dist/chunk-NKGWZWKJ.mjs.map +0 -1
- /package/dist/{chunk-AQBZWBJD.mjs.map → chunk-2MEJMEYX.mjs.map} +0 -0
- /package/dist/{chunk-CBZV4PPM.mjs.map → chunk-5HOOAMQI.mjs.map} +0 -0
- /package/dist/{chunk-P4EZLCB6.mjs.map → chunk-BNSMQJEH.mjs.map} +0 -0
|
@@ -3,7 +3,6 @@ import {
|
|
|
3
3
|
ReplaySubject,
|
|
4
4
|
Subject,
|
|
5
5
|
Subscription,
|
|
6
|
-
filter,
|
|
7
6
|
finalize,
|
|
8
7
|
firstValueFrom,
|
|
9
8
|
shareReplay,
|
|
@@ -287,19 +286,7 @@ export class CopilotResolver {
|
|
|
287
286
|
// skip until this message start event
|
|
288
287
|
skipWhile((e) => e !== event),
|
|
289
288
|
// take until the message end event
|
|
290
|
-
takeWhile(
|
|
291
|
-
(e) =>
|
|
292
|
-
!(
|
|
293
|
-
e.type === RuntimeEventTypes.TextMessageEnd &&
|
|
294
|
-
e.messageId == event.messageId
|
|
295
|
-
),
|
|
296
|
-
),
|
|
297
|
-
// filter out any other message events or message ids
|
|
298
|
-
filter(
|
|
299
|
-
(e) =>
|
|
300
|
-
e.type == RuntimeEventTypes.TextMessageContent &&
|
|
301
|
-
e.messageId == event.messageId,
|
|
302
|
-
),
|
|
289
|
+
takeWhile((e) => e.type != RuntimeEventTypes.TextMessageEnd),
|
|
303
290
|
);
|
|
304
291
|
|
|
305
292
|
// signal when we are done streaming
|
|
@@ -380,20 +367,7 @@ export class CopilotResolver {
|
|
|
380
367
|
logger.debug("Action execution start event received");
|
|
381
368
|
const actionExecutionArgumentStream = eventStream.pipe(
|
|
382
369
|
skipWhile((e) => e !== event),
|
|
383
|
-
|
|
384
|
-
takeWhile(
|
|
385
|
-
(e) =>
|
|
386
|
-
!(
|
|
387
|
-
e.type === RuntimeEventTypes.ActionExecutionEnd &&
|
|
388
|
-
e.actionExecutionId == event.actionExecutionId
|
|
389
|
-
),
|
|
390
|
-
),
|
|
391
|
-
// filter out any other action execution events or action execution ids
|
|
392
|
-
filter(
|
|
393
|
-
(e) =>
|
|
394
|
-
e.type == RuntimeEventTypes.ActionExecutionArgs &&
|
|
395
|
-
e.actionExecutionId == event.actionExecutionId,
|
|
396
|
-
),
|
|
370
|
+
takeWhile((e) => e.type != RuntimeEventTypes.ActionExecutionEnd),
|
|
397
371
|
);
|
|
398
372
|
const streamingArgumentsStatus = new Subject<typeof MessageStatusUnion>();
|
|
399
373
|
pushMessage({
|
|
@@ -13,9 +13,7 @@
|
|
|
13
13
|
* apiKey: "<your-api-key>",
|
|
14
14
|
* });
|
|
15
15
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* return copilotKit.streamHttpServerResponse(req, res, serviceAdapter);
|
|
16
|
+
* return new AnthropicAdapter({ anthropic });
|
|
19
17
|
* ```
|
|
20
18
|
*/
|
|
21
19
|
import Anthropic from "@anthropic-ai/sdk";
|
|
@@ -122,10 +120,7 @@ export class AnthropicAdapter implements CopilotServiceAdapter {
|
|
|
122
120
|
mode = "message";
|
|
123
121
|
} else if (chunk.content_block.type === "tool_use") {
|
|
124
122
|
currentToolCallId = chunk.content_block.id;
|
|
125
|
-
eventStream$.sendActionExecutionStart(
|
|
126
|
-
actionExecutionId: currentToolCallId,
|
|
127
|
-
actionName: chunk.content_block.name,
|
|
128
|
-
});
|
|
123
|
+
eventStream$.sendActionExecutionStart(currentToolCallId, chunk.content_block.name);
|
|
129
124
|
mode = "function";
|
|
130
125
|
}
|
|
131
126
|
} else if (chunk.type === "content_block_delta") {
|
|
@@ -133,27 +128,21 @@ export class AnthropicAdapter implements CopilotServiceAdapter {
|
|
|
133
128
|
const text = filterThinkingTextBuffer.onTextChunk(chunk.delta.text);
|
|
134
129
|
if (text.length > 0) {
|
|
135
130
|
if (!didOutputText) {
|
|
136
|
-
eventStream$.sendTextMessageStart(
|
|
131
|
+
eventStream$.sendTextMessageStart(currentMessageId);
|
|
137
132
|
didOutputText = true;
|
|
138
133
|
}
|
|
139
|
-
eventStream$.sendTextMessageContent(
|
|
140
|
-
messageId: currentMessageId,
|
|
141
|
-
content: text,
|
|
142
|
-
});
|
|
134
|
+
eventStream$.sendTextMessageContent(text);
|
|
143
135
|
}
|
|
144
136
|
} else if (chunk.delta.type === "input_json_delta") {
|
|
145
|
-
eventStream$.sendActionExecutionArgs(
|
|
146
|
-
actionExecutionId: currentToolCallId,
|
|
147
|
-
args: chunk.delta.partial_json,
|
|
148
|
-
});
|
|
137
|
+
eventStream$.sendActionExecutionArgs(chunk.delta.partial_json);
|
|
149
138
|
}
|
|
150
139
|
} else if (chunk.type === "content_block_stop") {
|
|
151
140
|
if (mode === "message") {
|
|
152
141
|
if (didOutputText) {
|
|
153
|
-
eventStream$.sendTextMessageEnd(
|
|
142
|
+
eventStream$.sendTextMessageEnd();
|
|
154
143
|
}
|
|
155
144
|
} else if (mode === "function") {
|
|
156
|
-
eventStream$.sendActionExecutionEnd(
|
|
145
|
+
eventStream$.sendActionExecutionEnd();
|
|
157
146
|
}
|
|
158
147
|
}
|
|
159
148
|
}
|
|
@@ -33,18 +33,17 @@ export type RuntimeEvent =
|
|
|
33
33
|
| { type: RuntimeEventTypes.TextMessageStart; messageId: string }
|
|
34
34
|
| {
|
|
35
35
|
type: RuntimeEventTypes.TextMessageContent;
|
|
36
|
-
messageId: string;
|
|
37
36
|
content: string;
|
|
38
37
|
}
|
|
39
|
-
| { type: RuntimeEventTypes.TextMessageEnd
|
|
38
|
+
| { type: RuntimeEventTypes.TextMessageEnd }
|
|
40
39
|
| {
|
|
41
40
|
type: RuntimeEventTypes.ActionExecutionStart;
|
|
42
41
|
actionExecutionId: string;
|
|
43
42
|
actionName: string;
|
|
44
43
|
scope?: FunctionCallScope;
|
|
45
44
|
}
|
|
46
|
-
| { type: RuntimeEventTypes.ActionExecutionArgs;
|
|
47
|
-
| { type: RuntimeEventTypes.ActionExecutionEnd
|
|
45
|
+
| { type: RuntimeEventTypes.ActionExecutionArgs; args: string }
|
|
46
|
+
| { type: RuntimeEventTypes.ActionExecutionEnd }
|
|
48
47
|
| {
|
|
49
48
|
type: RuntimeEventTypes.ActionExecutionResult;
|
|
50
49
|
actionName: string;
|
|
@@ -78,31 +77,25 @@ export class RuntimeEventSubject extends ReplaySubject<RuntimeEvent> {
|
|
|
78
77
|
super();
|
|
79
78
|
}
|
|
80
79
|
|
|
81
|
-
sendTextMessageStart(
|
|
80
|
+
sendTextMessageStart(messageId: string) {
|
|
82
81
|
this.next({ type: RuntimeEventTypes.TextMessageStart, messageId });
|
|
83
82
|
}
|
|
84
83
|
|
|
85
|
-
sendTextMessageContent(
|
|
86
|
-
this.next({ type: RuntimeEventTypes.TextMessageContent, content
|
|
84
|
+
sendTextMessageContent(content: string) {
|
|
85
|
+
this.next({ type: RuntimeEventTypes.TextMessageContent, content });
|
|
87
86
|
}
|
|
88
87
|
|
|
89
|
-
sendTextMessageEnd(
|
|
90
|
-
this.next({ type: RuntimeEventTypes.TextMessageEnd
|
|
88
|
+
sendTextMessageEnd() {
|
|
89
|
+
this.next({ type: RuntimeEventTypes.TextMessageEnd });
|
|
91
90
|
}
|
|
92
91
|
|
|
93
92
|
sendTextMessage(messageId: string, content: string) {
|
|
94
|
-
this.sendTextMessageStart(
|
|
95
|
-
this.sendTextMessageContent(
|
|
96
|
-
this.sendTextMessageEnd(
|
|
93
|
+
this.sendTextMessageStart(messageId);
|
|
94
|
+
this.sendTextMessageContent(content);
|
|
95
|
+
this.sendTextMessageEnd();
|
|
97
96
|
}
|
|
98
97
|
|
|
99
|
-
sendActionExecutionStart({
|
|
100
|
-
actionExecutionId,
|
|
101
|
-
actionName,
|
|
102
|
-
}: {
|
|
103
|
-
actionExecutionId: string;
|
|
104
|
-
actionName: string;
|
|
105
|
-
}) {
|
|
98
|
+
sendActionExecutionStart(actionExecutionId: string, actionName: string) {
|
|
106
99
|
this.next({
|
|
107
100
|
type: RuntimeEventTypes.ActionExecutionStart,
|
|
108
101
|
actionExecutionId,
|
|
@@ -110,43 +103,21 @@ export class RuntimeEventSubject extends ReplaySubject<RuntimeEvent> {
|
|
|
110
103
|
});
|
|
111
104
|
}
|
|
112
105
|
|
|
113
|
-
sendActionExecutionArgs({
|
|
114
|
-
|
|
115
|
-
args,
|
|
116
|
-
}: {
|
|
117
|
-
actionExecutionId: string;
|
|
118
|
-
args: string;
|
|
119
|
-
}) {
|
|
120
|
-
this.next({ type: RuntimeEventTypes.ActionExecutionArgs, args, actionExecutionId });
|
|
106
|
+
sendActionExecutionArgs(args: string) {
|
|
107
|
+
this.next({ type: RuntimeEventTypes.ActionExecutionArgs, args });
|
|
121
108
|
}
|
|
122
109
|
|
|
123
|
-
sendActionExecutionEnd(
|
|
124
|
-
this.next({ type: RuntimeEventTypes.ActionExecutionEnd
|
|
110
|
+
sendActionExecutionEnd() {
|
|
111
|
+
this.next({ type: RuntimeEventTypes.ActionExecutionEnd });
|
|
125
112
|
}
|
|
126
113
|
|
|
127
|
-
sendActionExecution({
|
|
128
|
-
actionExecutionId,
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}: {
|
|
132
|
-
actionExecutionId: string;
|
|
133
|
-
actionName: string;
|
|
134
|
-
args: string;
|
|
135
|
-
}) {
|
|
136
|
-
this.sendActionExecutionStart({ actionExecutionId, actionName });
|
|
137
|
-
this.sendActionExecutionArgs({ actionExecutionId, args });
|
|
138
|
-
this.sendActionExecutionEnd({ actionExecutionId });
|
|
114
|
+
sendActionExecution(actionExecutionId: string, toolName: string, args: string) {
|
|
115
|
+
this.sendActionExecutionStart(actionExecutionId, toolName);
|
|
116
|
+
this.sendActionExecutionArgs(args);
|
|
117
|
+
this.sendActionExecutionEnd();
|
|
139
118
|
}
|
|
140
119
|
|
|
141
|
-
sendActionExecutionResult({
|
|
142
|
-
actionExecutionId,
|
|
143
|
-
actionName,
|
|
144
|
-
result,
|
|
145
|
-
}: {
|
|
146
|
-
actionExecutionId: string;
|
|
147
|
-
actionName: string;
|
|
148
|
-
result: string;
|
|
149
|
-
}) {
|
|
120
|
+
sendActionExecutionResult(actionExecutionId: string, actionName: string, result: string) {
|
|
150
121
|
this.next({
|
|
151
122
|
type: RuntimeEventTypes.ActionExecutionResult,
|
|
152
123
|
actionName,
|
|
@@ -155,25 +126,16 @@ export class RuntimeEventSubject extends ReplaySubject<RuntimeEvent> {
|
|
|
155
126
|
});
|
|
156
127
|
}
|
|
157
128
|
|
|
158
|
-
sendAgentStateMessage(
|
|
159
|
-
threadId,
|
|
160
|
-
agentName,
|
|
161
|
-
nodeName,
|
|
162
|
-
runId,
|
|
163
|
-
active,
|
|
164
|
-
role,
|
|
165
|
-
state,
|
|
166
|
-
running,
|
|
167
|
-
|
|
168
|
-
threadId: string;
|
|
169
|
-
agentName: string;
|
|
170
|
-
nodeName: string;
|
|
171
|
-
runId: string;
|
|
172
|
-
active: boolean;
|
|
173
|
-
role: string;
|
|
174
|
-
state: string;
|
|
175
|
-
running: boolean;
|
|
176
|
-
}) {
|
|
129
|
+
sendAgentStateMessage(
|
|
130
|
+
threadId: string,
|
|
131
|
+
agentName: string,
|
|
132
|
+
nodeName: string,
|
|
133
|
+
runId: string,
|
|
134
|
+
active: boolean,
|
|
135
|
+
role: string,
|
|
136
|
+
state: string,
|
|
137
|
+
running: boolean,
|
|
138
|
+
) {
|
|
177
139
|
this.next({
|
|
178
140
|
type: RuntimeEventTypes.AgentStateMessage,
|
|
179
141
|
threadId,
|
|
@@ -319,11 +281,11 @@ async function executeAction(
|
|
|
319
281
|
|
|
320
282
|
// handle LangGraph agents
|
|
321
283
|
if (isLangGraphAgentAction(action)) {
|
|
322
|
-
eventStream$.sendActionExecutionResult(
|
|
284
|
+
eventStream$.sendActionExecutionResult(
|
|
323
285
|
actionExecutionId,
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
286
|
+
action.name,
|
|
287
|
+
`${action.name} agent started`,
|
|
288
|
+
);
|
|
327
289
|
const stream = await action.langGraphAgentHandler({
|
|
328
290
|
name: action.name,
|
|
329
291
|
actionInputsWithoutAgents,
|
|
@@ -58,15 +58,11 @@ export class ExperimentalOllamaAdapter implements CopilotServiceAdapter {
|
|
|
58
58
|
const _stream = await ollama.stream(contents); // [TODO] role info is dropped...
|
|
59
59
|
|
|
60
60
|
eventSource.stream(async (eventStream$) => {
|
|
61
|
-
|
|
62
|
-
eventStream$.sendTextMessageStart({ messageId: currentMessageId });
|
|
61
|
+
eventStream$.sendTextMessageStart(randomId());
|
|
63
62
|
for await (const chunkText of _stream) {
|
|
64
|
-
eventStream$.sendTextMessageContent(
|
|
65
|
-
messageId: currentMessageId,
|
|
66
|
-
content: chunkText,
|
|
67
|
-
});
|
|
63
|
+
eventStream$.sendTextMessageContent(chunkText);
|
|
68
64
|
}
|
|
69
|
-
eventStream$.sendTextMessageEnd(
|
|
65
|
+
eventStream$.sendTextMessageEnd();
|
|
70
66
|
// we may need to add this later.. [nc]
|
|
71
67
|
// let calls = (await result.response).functionCalls();
|
|
72
68
|
|
|
@@ -11,9 +11,7 @@
|
|
|
11
11
|
*
|
|
12
12
|
* const copilotKit = new CopilotRuntime();
|
|
13
13
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* return copilotKit.streamHttpServerResponse(req, res, serviceAdapter);
|
|
14
|
+
* return new GoogleGenerativeAIAdapter({ model: "gemini-1.5-pro" });
|
|
17
15
|
* ```
|
|
18
16
|
*/
|
|
19
17
|
import { ChatGoogle } from "@langchain/google-gauth";
|
|
@@ -11,9 +11,7 @@
|
|
|
11
11
|
*
|
|
12
12
|
* const copilotKit = new CopilotRuntime();
|
|
13
13
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* return copilotKit.streamHttpServerResponse(req, res, serviceAdapter);
|
|
14
|
+
* return new GroqAdapter({ groq, model: "<model-name>" });
|
|
17
15
|
* ```
|
|
18
16
|
*/
|
|
19
17
|
import { Groq } from "groq-sdk";
|
|
@@ -108,9 +106,6 @@ export class GroqAdapter implements CopilotServiceAdapter {
|
|
|
108
106
|
|
|
109
107
|
eventSource.stream(async (eventStream$) => {
|
|
110
108
|
let mode: "function" | "message" | null = null;
|
|
111
|
-
let currentMessageId: string;
|
|
112
|
-
let currentToolCallId: string;
|
|
113
|
-
|
|
114
109
|
for await (const chunk of stream) {
|
|
115
110
|
const toolCall = chunk.choices[0].delta.tool_calls?.[0];
|
|
116
111
|
const content = chunk.choices[0].delta.content;
|
|
@@ -120,47 +115,36 @@ export class GroqAdapter implements CopilotServiceAdapter {
|
|
|
120
115
|
// If toolCall?.id is defined, it means a new tool call starts.
|
|
121
116
|
if (mode === "message" && toolCall?.id) {
|
|
122
117
|
mode = null;
|
|
123
|
-
eventStream$.sendTextMessageEnd(
|
|
118
|
+
eventStream$.sendTextMessageEnd();
|
|
124
119
|
} else if (mode === "function" && (toolCall === undefined || toolCall?.id)) {
|
|
125
120
|
mode = null;
|
|
126
|
-
eventStream$.sendActionExecutionEnd(
|
|
121
|
+
eventStream$.sendActionExecutionEnd();
|
|
127
122
|
}
|
|
128
123
|
|
|
129
124
|
// If we send a new message type, send the appropriate start event.
|
|
130
125
|
if (mode === null) {
|
|
131
126
|
if (toolCall?.id) {
|
|
132
127
|
mode = "function";
|
|
133
|
-
|
|
134
|
-
eventStream$.sendActionExecutionStart({
|
|
135
|
-
actionExecutionId: currentToolCallId,
|
|
136
|
-
actionName: toolCall!.function!.name,
|
|
137
|
-
});
|
|
128
|
+
eventStream$.sendActionExecutionStart(toolCall!.id, toolCall!.function!.name);
|
|
138
129
|
} else if (content) {
|
|
139
130
|
mode = "message";
|
|
140
|
-
|
|
141
|
-
eventStream$.sendTextMessageStart({ messageId: currentMessageId });
|
|
131
|
+
eventStream$.sendTextMessageStart(chunk.id);
|
|
142
132
|
}
|
|
143
133
|
}
|
|
144
134
|
|
|
145
135
|
// send the content events
|
|
146
136
|
if (mode === "message" && content) {
|
|
147
|
-
eventStream$.sendTextMessageContent(
|
|
148
|
-
messageId: currentMessageId,
|
|
149
|
-
content,
|
|
150
|
-
});
|
|
137
|
+
eventStream$.sendTextMessageContent(content);
|
|
151
138
|
} else if (mode === "function" && toolCall?.function?.arguments) {
|
|
152
|
-
eventStream$.sendActionExecutionArgs(
|
|
153
|
-
actionExecutionId: currentToolCallId,
|
|
154
|
-
args: toolCall.function.arguments,
|
|
155
|
-
});
|
|
139
|
+
eventStream$.sendActionExecutionArgs(toolCall.function.arguments);
|
|
156
140
|
}
|
|
157
141
|
}
|
|
158
142
|
|
|
159
143
|
// send the end events
|
|
160
144
|
if (mode === "message") {
|
|
161
|
-
eventStream$.sendTextMessageEnd(
|
|
145
|
+
eventStream$.sendTextMessageEnd();
|
|
162
146
|
} else if (mode === "function") {
|
|
163
|
-
eventStream$.sendActionExecutionEnd(
|
|
147
|
+
eventStream$.sendActionExecutionEnd();
|
|
164
148
|
}
|
|
165
149
|
|
|
166
150
|
eventStream$.complete();
|
|
@@ -14,15 +14,13 @@
|
|
|
14
14
|
* apiKey: "<your-api-key>",
|
|
15
15
|
* });
|
|
16
16
|
*
|
|
17
|
-
*
|
|
17
|
+
* return new LangChainAdapter({
|
|
18
18
|
* chainFn: async ({ messages, tools }) => {
|
|
19
19
|
* return model.bindTools(tools).stream(messages);
|
|
20
20
|
* // or optionally enable strict mode
|
|
21
21
|
* // return model.bindTools(tools, { strict: true }).stream(messages);
|
|
22
22
|
* }
|
|
23
23
|
* });
|
|
24
|
-
*
|
|
25
|
-
* return copilotKit.streamHttpServerResponse(req, res, serviceAdapter);
|
|
26
24
|
* ```
|
|
27
25
|
*
|
|
28
26
|
* The asynchronous handler function (`chainFn`) can return any of the following:
|
|
@@ -129,11 +129,11 @@ function maybeSendActionExecutionResultIsMessage(
|
|
|
129
129
|
// language models need a result after the function call
|
|
130
130
|
// we simply let them know that we are sending a message
|
|
131
131
|
if (actionExecution) {
|
|
132
|
-
eventStream$.sendActionExecutionResult(
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
132
|
+
eventStream$.sendActionExecutionResult(
|
|
133
|
+
actionExecution.id,
|
|
134
|
+
actionExecution.name,
|
|
135
|
+
"Sending a message",
|
|
136
|
+
);
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
139
|
|
|
@@ -152,11 +152,7 @@ export async function streamLangChainResponse({
|
|
|
152
152
|
eventStream$.sendTextMessage(randomId(), result);
|
|
153
153
|
} else {
|
|
154
154
|
// Send as a result
|
|
155
|
-
eventStream$.sendActionExecutionResult(
|
|
156
|
-
actionExecutionId: actionExecution.id,
|
|
157
|
-
actionName: actionExecution.name,
|
|
158
|
-
result: result,
|
|
159
|
-
});
|
|
155
|
+
eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, result);
|
|
160
156
|
}
|
|
161
157
|
}
|
|
162
158
|
|
|
@@ -169,11 +165,11 @@ export async function streamLangChainResponse({
|
|
|
169
165
|
eventStream$.sendTextMessage(randomId(), result.content as string);
|
|
170
166
|
}
|
|
171
167
|
for (const toolCall of result.tool_calls) {
|
|
172
|
-
eventStream$.sendActionExecution(
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
168
|
+
eventStream$.sendActionExecution(
|
|
169
|
+
toolCall.id || randomId(),
|
|
170
|
+
toolCall.name,
|
|
171
|
+
JSON.stringify(toolCall.args),
|
|
172
|
+
);
|
|
177
173
|
}
|
|
178
174
|
}
|
|
179
175
|
|
|
@@ -187,11 +183,11 @@ export async function streamLangChainResponse({
|
|
|
187
183
|
}
|
|
188
184
|
if (result.lc_kwargs?.tool_calls) {
|
|
189
185
|
for (const toolCall of result.lc_kwargs?.tool_calls) {
|
|
190
|
-
eventStream$.sendActionExecution(
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
186
|
+
eventStream$.sendActionExecution(
|
|
187
|
+
toolCall.id || randomId(),
|
|
188
|
+
toolCall.name,
|
|
189
|
+
JSON.stringify(toolCall.args),
|
|
190
|
+
);
|
|
195
191
|
}
|
|
196
192
|
}
|
|
197
193
|
}
|
|
@@ -218,7 +214,6 @@ export async function streamLangChainResponse({
|
|
|
218
214
|
|
|
219
215
|
let toolCallName: string | undefined = undefined;
|
|
220
216
|
let toolCallId: string | undefined = undefined;
|
|
221
|
-
let currentMessageId: string;
|
|
222
217
|
let toolCallArgs: string | undefined = undefined;
|
|
223
218
|
let hasToolCall: boolean = false;
|
|
224
219
|
let content = value?.content as string;
|
|
@@ -253,10 +248,10 @@ export async function streamLangChainResponse({
|
|
|
253
248
|
// If toolCallName is defined, it means a new tool call starts.
|
|
254
249
|
if (mode === "message" && (toolCallId || done)) {
|
|
255
250
|
mode = null;
|
|
256
|
-
eventStream$.sendTextMessageEnd(
|
|
251
|
+
eventStream$.sendTextMessageEnd();
|
|
257
252
|
} else if (mode === "function" && (!hasToolCall || done)) {
|
|
258
253
|
mode = null;
|
|
259
|
-
eventStream$.sendActionExecutionEnd(
|
|
254
|
+
eventStream$.sendActionExecutionEnd();
|
|
260
255
|
}
|
|
261
256
|
|
|
262
257
|
if (done) {
|
|
@@ -267,37 +262,26 @@ export async function streamLangChainResponse({
|
|
|
267
262
|
if (mode === null) {
|
|
268
263
|
if (hasToolCall && toolCallId && toolCallName) {
|
|
269
264
|
mode = "function";
|
|
270
|
-
eventStream$.sendActionExecutionStart(
|
|
271
|
-
actionExecutionId: toolCallId,
|
|
272
|
-
actionName: toolCallName,
|
|
273
|
-
});
|
|
265
|
+
eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
|
|
274
266
|
} else if (content) {
|
|
275
267
|
mode = "message";
|
|
276
|
-
|
|
277
|
-
eventStream$.sendTextMessageStart({ messageId: currentMessageId });
|
|
268
|
+
eventStream$.sendTextMessageStart(randomId());
|
|
278
269
|
}
|
|
279
270
|
}
|
|
280
271
|
|
|
281
272
|
// send the content events
|
|
282
273
|
if (mode === "message" && content) {
|
|
283
|
-
eventStream$.sendTextMessageContent(
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
});
|
|
274
|
+
eventStream$.sendTextMessageContent(
|
|
275
|
+
Array.isArray(content) ? (content[0]?.text ?? "") : content,
|
|
276
|
+
);
|
|
287
277
|
} else if (mode === "function" && toolCallArgs) {
|
|
288
278
|
// For calls of the same tool with different index, we seal last tool call and register a new one
|
|
289
279
|
if (toolCallDetails.index !== toolCallDetails.prevIndex) {
|
|
290
|
-
eventStream$.sendActionExecutionEnd(
|
|
291
|
-
eventStream$.sendActionExecutionStart(
|
|
292
|
-
actionExecutionId: toolCallId,
|
|
293
|
-
actionName: toolCallName,
|
|
294
|
-
});
|
|
280
|
+
eventStream$.sendActionExecutionEnd();
|
|
281
|
+
eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
|
|
295
282
|
toolCallDetails.prevIndex = toolCallDetails.index;
|
|
296
283
|
}
|
|
297
|
-
eventStream$.sendActionExecutionArgs(
|
|
298
|
-
actionExecutionId: toolCallId,
|
|
299
|
-
args: toolCallArgs,
|
|
300
|
-
});
|
|
284
|
+
eventStream$.sendActionExecutionArgs(toolCallArgs);
|
|
301
285
|
}
|
|
302
286
|
} catch (error) {
|
|
303
287
|
console.error("Error reading from stream", error);
|
|
@@ -305,11 +289,11 @@ export async function streamLangChainResponse({
|
|
|
305
289
|
}
|
|
306
290
|
}
|
|
307
291
|
} else if (actionExecution) {
|
|
308
|
-
eventStream$.sendActionExecutionResult(
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
292
|
+
eventStream$.sendActionExecutionResult(
|
|
293
|
+
actionExecution.id,
|
|
294
|
+
actionExecution.name,
|
|
295
|
+
encodeResult(result),
|
|
296
|
+
);
|
|
313
297
|
}
|
|
314
298
|
|
|
315
299
|
// unsupported type
|
|
@@ -14,9 +14,7 @@
|
|
|
14
14
|
* apiKey: "<your-api-key>",
|
|
15
15
|
* });
|
|
16
16
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* return copilotKit.streamHttpServerResponse(req, res, serviceAdapter);
|
|
17
|
+
* return new OpenAIAdapter({ openai });
|
|
20
18
|
* ```
|
|
21
19
|
*
|
|
22
20
|
* ## Example with Azure OpenAI
|
|
@@ -47,9 +45,7 @@
|
|
|
47
45
|
* defaultHeaders: { "api-key": apiKey },
|
|
48
46
|
* });
|
|
49
47
|
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
* return copilotKit.streamHttpServerResponse(req, res, serviceAdapter);
|
|
48
|
+
* return new OpenAIAdapter({ openai });
|
|
53
49
|
* ```
|
|
54
50
|
*/
|
|
55
51
|
import OpenAI from "openai";
|
|
@@ -144,8 +140,7 @@ export class OpenAIAdapter implements CopilotServiceAdapter {
|
|
|
144
140
|
|
|
145
141
|
eventSource.stream(async (eventStream$) => {
|
|
146
142
|
let mode: "function" | "message" | null = null;
|
|
147
|
-
|
|
148
|
-
let currentToolCallId: string;
|
|
143
|
+
|
|
149
144
|
for await (const chunk of stream) {
|
|
150
145
|
if (chunk.choices.length === 0) {
|
|
151
146
|
continue;
|
|
@@ -159,47 +154,36 @@ export class OpenAIAdapter implements CopilotServiceAdapter {
|
|
|
159
154
|
// If toolCall?.id is defined, it means a new tool call starts.
|
|
160
155
|
if (mode === "message" && toolCall?.id) {
|
|
161
156
|
mode = null;
|
|
162
|
-
eventStream$.sendTextMessageEnd(
|
|
157
|
+
eventStream$.sendTextMessageEnd();
|
|
163
158
|
} else if (mode === "function" && (toolCall === undefined || toolCall?.id)) {
|
|
164
159
|
mode = null;
|
|
165
|
-
eventStream$.sendActionExecutionEnd(
|
|
160
|
+
eventStream$.sendActionExecutionEnd();
|
|
166
161
|
}
|
|
167
162
|
|
|
168
163
|
// If we send a new message type, send the appropriate start event.
|
|
169
164
|
if (mode === null) {
|
|
170
165
|
if (toolCall?.id) {
|
|
171
166
|
mode = "function";
|
|
172
|
-
|
|
173
|
-
eventStream$.sendActionExecutionStart({
|
|
174
|
-
actionExecutionId: currentToolCallId,
|
|
175
|
-
actionName: toolCall!.function!.name,
|
|
176
|
-
});
|
|
167
|
+
eventStream$.sendActionExecutionStart(toolCall!.id, toolCall!.function!.name);
|
|
177
168
|
} else if (content) {
|
|
178
169
|
mode = "message";
|
|
179
|
-
|
|
180
|
-
eventStream$.sendTextMessageStart({ messageId: currentMessageId });
|
|
170
|
+
eventStream$.sendTextMessageStart(chunk.id);
|
|
181
171
|
}
|
|
182
172
|
}
|
|
183
173
|
|
|
184
174
|
// send the content events
|
|
185
175
|
if (mode === "message" && content) {
|
|
186
|
-
eventStream$.sendTextMessageContent(
|
|
187
|
-
messageId: currentMessageId,
|
|
188
|
-
content: content,
|
|
189
|
-
});
|
|
176
|
+
eventStream$.sendTextMessageContent(content);
|
|
190
177
|
} else if (mode === "function" && toolCall?.function?.arguments) {
|
|
191
|
-
eventStream$.sendActionExecutionArgs(
|
|
192
|
-
actionExecutionId: currentToolCallId,
|
|
193
|
-
args: toolCall.function.arguments,
|
|
194
|
-
});
|
|
178
|
+
eventStream$.sendActionExecutionArgs(toolCall.function.arguments);
|
|
195
179
|
}
|
|
196
180
|
}
|
|
197
181
|
|
|
198
182
|
// send the end events
|
|
199
183
|
if (mode === "message") {
|
|
200
|
-
eventStream$.sendTextMessageEnd(
|
|
184
|
+
eventStream$.sendTextMessageEnd();
|
|
201
185
|
} else if (mode === "function") {
|
|
202
|
-
eventStream$.sendActionExecutionEnd(
|
|
186
|
+
eventStream$.sendActionExecutionEnd();
|
|
203
187
|
}
|
|
204
188
|
|
|
205
189
|
eventStream$.complete();
|