@copilotkit/runtime 1.4.0-pre-1-4-0.13 → 1.4.0-pre-1-4-0.14
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 +13 -0
- package/dist/{chunk-AQBZWBJD.mjs → chunk-6H45CJUK.mjs} +2 -2
- package/dist/{chunk-CBZV4PPM.mjs → chunk-N4ROMSZN.mjs} +2 -2
- package/dist/{chunk-P4EZLCB6.mjs → chunk-NFMXFL2C.mjs} +2 -2
- package/dist/{chunk-56IQ6PGC.mjs → chunk-WHJ3DAYL.mjs} +56 -213
- package/dist/chunk-WHJ3DAYL.mjs.map +1 -0
- package/dist/{chunk-NKGWZWKJ.mjs → chunk-X4FFTYTH.mjs} +33 -76
- package/dist/chunk-X4FFTYTH.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-2f8fd767.d.ts} +1 -1
- 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 +3 -3
- 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 +6 -15
- package/src/service-adapters/events.ts +35 -73
- package/src/service-adapters/experimental/ollama/ollama-adapter.ts +3 -7
- package/src/service-adapters/groq/groq-adapter.ts +8 -22
- package/src/service-adapters/langchain/utils.ts +31 -47
- package/src/service-adapters/openai/openai-adapter.ts +9 -21
- package/src/service-adapters/openai/openai-assistant-adapter.ts +8 -21
- 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-6H45CJUK.mjs.map} +0 -0
- /package/dist/{chunk-CBZV4PPM.mjs.map → chunk-N4ROMSZN.mjs.map} +0 -0
- /package/dist/{chunk-P4EZLCB6.mjs.map → chunk-NFMXFL2C.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({
|
|
@@ -122,10 +122,7 @@ export class AnthropicAdapter implements CopilotServiceAdapter {
|
|
|
122
122
|
mode = "message";
|
|
123
123
|
} else if (chunk.content_block.type === "tool_use") {
|
|
124
124
|
currentToolCallId = chunk.content_block.id;
|
|
125
|
-
eventStream$.sendActionExecutionStart(
|
|
126
|
-
actionExecutionId: currentToolCallId,
|
|
127
|
-
actionName: chunk.content_block.name,
|
|
128
|
-
});
|
|
125
|
+
eventStream$.sendActionExecutionStart(currentToolCallId, chunk.content_block.name);
|
|
129
126
|
mode = "function";
|
|
130
127
|
}
|
|
131
128
|
} else if (chunk.type === "content_block_delta") {
|
|
@@ -133,27 +130,21 @@ export class AnthropicAdapter implements CopilotServiceAdapter {
|
|
|
133
130
|
const text = filterThinkingTextBuffer.onTextChunk(chunk.delta.text);
|
|
134
131
|
if (text.length > 0) {
|
|
135
132
|
if (!didOutputText) {
|
|
136
|
-
eventStream$.sendTextMessageStart(
|
|
133
|
+
eventStream$.sendTextMessageStart(currentMessageId);
|
|
137
134
|
didOutputText = true;
|
|
138
135
|
}
|
|
139
|
-
eventStream$.sendTextMessageContent(
|
|
140
|
-
messageId: currentMessageId,
|
|
141
|
-
content: text,
|
|
142
|
-
});
|
|
136
|
+
eventStream$.sendTextMessageContent(text);
|
|
143
137
|
}
|
|
144
138
|
} else if (chunk.delta.type === "input_json_delta") {
|
|
145
|
-
eventStream$.sendActionExecutionArgs(
|
|
146
|
-
actionExecutionId: currentToolCallId,
|
|
147
|
-
args: chunk.delta.partial_json,
|
|
148
|
-
});
|
|
139
|
+
eventStream$.sendActionExecutionArgs(chunk.delta.partial_json);
|
|
149
140
|
}
|
|
150
141
|
} else if (chunk.type === "content_block_stop") {
|
|
151
142
|
if (mode === "message") {
|
|
152
143
|
if (didOutputText) {
|
|
153
|
-
eventStream$.sendTextMessageEnd(
|
|
144
|
+
eventStream$.sendTextMessageEnd();
|
|
154
145
|
}
|
|
155
146
|
} else if (mode === "function") {
|
|
156
|
-
eventStream$.sendActionExecutionEnd(
|
|
147
|
+
eventStream$.sendActionExecutionEnd();
|
|
157
148
|
}
|
|
158
149
|
}
|
|
159
150
|
}
|
|
@@ -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
|
|
|
@@ -108,9 +108,6 @@ export class GroqAdapter implements CopilotServiceAdapter {
|
|
|
108
108
|
|
|
109
109
|
eventSource.stream(async (eventStream$) => {
|
|
110
110
|
let mode: "function" | "message" | null = null;
|
|
111
|
-
let currentMessageId: string;
|
|
112
|
-
let currentToolCallId: string;
|
|
113
|
-
|
|
114
111
|
for await (const chunk of stream) {
|
|
115
112
|
const toolCall = chunk.choices[0].delta.tool_calls?.[0];
|
|
116
113
|
const content = chunk.choices[0].delta.content;
|
|
@@ -120,47 +117,36 @@ export class GroqAdapter implements CopilotServiceAdapter {
|
|
|
120
117
|
// If toolCall?.id is defined, it means a new tool call starts.
|
|
121
118
|
if (mode === "message" && toolCall?.id) {
|
|
122
119
|
mode = null;
|
|
123
|
-
eventStream$.sendTextMessageEnd(
|
|
120
|
+
eventStream$.sendTextMessageEnd();
|
|
124
121
|
} else if (mode === "function" && (toolCall === undefined || toolCall?.id)) {
|
|
125
122
|
mode = null;
|
|
126
|
-
eventStream$.sendActionExecutionEnd(
|
|
123
|
+
eventStream$.sendActionExecutionEnd();
|
|
127
124
|
}
|
|
128
125
|
|
|
129
126
|
// If we send a new message type, send the appropriate start event.
|
|
130
127
|
if (mode === null) {
|
|
131
128
|
if (toolCall?.id) {
|
|
132
129
|
mode = "function";
|
|
133
|
-
|
|
134
|
-
eventStream$.sendActionExecutionStart({
|
|
135
|
-
actionExecutionId: currentToolCallId,
|
|
136
|
-
actionName: toolCall!.function!.name,
|
|
137
|
-
});
|
|
130
|
+
eventStream$.sendActionExecutionStart(toolCall!.id, toolCall!.function!.name);
|
|
138
131
|
} else if (content) {
|
|
139
132
|
mode = "message";
|
|
140
|
-
|
|
141
|
-
eventStream$.sendTextMessageStart({ messageId: currentMessageId });
|
|
133
|
+
eventStream$.sendTextMessageStart(chunk.id);
|
|
142
134
|
}
|
|
143
135
|
}
|
|
144
136
|
|
|
145
137
|
// send the content events
|
|
146
138
|
if (mode === "message" && content) {
|
|
147
|
-
eventStream$.sendTextMessageContent(
|
|
148
|
-
messageId: currentMessageId,
|
|
149
|
-
content,
|
|
150
|
-
});
|
|
139
|
+
eventStream$.sendTextMessageContent(content);
|
|
151
140
|
} else if (mode === "function" && toolCall?.function?.arguments) {
|
|
152
|
-
eventStream$.sendActionExecutionArgs(
|
|
153
|
-
actionExecutionId: currentToolCallId,
|
|
154
|
-
args: toolCall.function.arguments,
|
|
155
|
-
});
|
|
141
|
+
eventStream$.sendActionExecutionArgs(toolCall.function.arguments);
|
|
156
142
|
}
|
|
157
143
|
}
|
|
158
144
|
|
|
159
145
|
// send the end events
|
|
160
146
|
if (mode === "message") {
|
|
161
|
-
eventStream$.sendTextMessageEnd(
|
|
147
|
+
eventStream$.sendTextMessageEnd();
|
|
162
148
|
} else if (mode === "function") {
|
|
163
|
-
eventStream$.sendActionExecutionEnd(
|
|
149
|
+
eventStream$.sendActionExecutionEnd();
|
|
164
150
|
}
|
|
165
151
|
|
|
166
152
|
eventStream$.complete();
|
|
@@ -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
|
|
@@ -144,8 +144,7 @@ export class OpenAIAdapter implements CopilotServiceAdapter {
|
|
|
144
144
|
|
|
145
145
|
eventSource.stream(async (eventStream$) => {
|
|
146
146
|
let mode: "function" | "message" | null = null;
|
|
147
|
-
|
|
148
|
-
let currentToolCallId: string;
|
|
147
|
+
|
|
149
148
|
for await (const chunk of stream) {
|
|
150
149
|
if (chunk.choices.length === 0) {
|
|
151
150
|
continue;
|
|
@@ -159,47 +158,36 @@ export class OpenAIAdapter implements CopilotServiceAdapter {
|
|
|
159
158
|
// If toolCall?.id is defined, it means a new tool call starts.
|
|
160
159
|
if (mode === "message" && toolCall?.id) {
|
|
161
160
|
mode = null;
|
|
162
|
-
eventStream$.sendTextMessageEnd(
|
|
161
|
+
eventStream$.sendTextMessageEnd();
|
|
163
162
|
} else if (mode === "function" && (toolCall === undefined || toolCall?.id)) {
|
|
164
163
|
mode = null;
|
|
165
|
-
eventStream$.sendActionExecutionEnd(
|
|
164
|
+
eventStream$.sendActionExecutionEnd();
|
|
166
165
|
}
|
|
167
166
|
|
|
168
167
|
// If we send a new message type, send the appropriate start event.
|
|
169
168
|
if (mode === null) {
|
|
170
169
|
if (toolCall?.id) {
|
|
171
170
|
mode = "function";
|
|
172
|
-
|
|
173
|
-
eventStream$.sendActionExecutionStart({
|
|
174
|
-
actionExecutionId: currentToolCallId,
|
|
175
|
-
actionName: toolCall!.function!.name,
|
|
176
|
-
});
|
|
171
|
+
eventStream$.sendActionExecutionStart(toolCall!.id, toolCall!.function!.name);
|
|
177
172
|
} else if (content) {
|
|
178
173
|
mode = "message";
|
|
179
|
-
|
|
180
|
-
eventStream$.sendTextMessageStart({ messageId: currentMessageId });
|
|
174
|
+
eventStream$.sendTextMessageStart(chunk.id);
|
|
181
175
|
}
|
|
182
176
|
}
|
|
183
177
|
|
|
184
178
|
// send the content events
|
|
185
179
|
if (mode === "message" && content) {
|
|
186
|
-
eventStream$.sendTextMessageContent(
|
|
187
|
-
messageId: currentMessageId,
|
|
188
|
-
content: content,
|
|
189
|
-
});
|
|
180
|
+
eventStream$.sendTextMessageContent(content);
|
|
190
181
|
} else if (mode === "function" && toolCall?.function?.arguments) {
|
|
191
|
-
eventStream$.sendActionExecutionArgs(
|
|
192
|
-
actionExecutionId: currentToolCallId,
|
|
193
|
-
args: toolCall.function.arguments,
|
|
194
|
-
});
|
|
182
|
+
eventStream$.sendActionExecutionArgs(toolCall.function.arguments);
|
|
195
183
|
}
|
|
196
184
|
}
|
|
197
185
|
|
|
198
186
|
// send the end events
|
|
199
187
|
if (mode === "message") {
|
|
200
|
-
eventStream$.sendTextMessageEnd(
|
|
188
|
+
eventStream$.sendTextMessageEnd();
|
|
201
189
|
} else if (mode === "function") {
|
|
202
|
-
eventStream$.sendActionExecutionEnd(
|
|
190
|
+
eventStream$.sendActionExecutionEnd();
|
|
203
191
|
}
|
|
204
192
|
|
|
205
193
|
eventStream$.complete();
|
|
@@ -228,28 +228,22 @@ export class OpenAIAssistantAdapter implements CopilotServiceAdapter {
|
|
|
228
228
|
private async streamResponse(stream: AssistantStream, eventSource: RuntimeEventSource) {
|
|
229
229
|
eventSource.stream(async (eventStream$) => {
|
|
230
230
|
let inFunctionCall = false;
|
|
231
|
-
let currentMessageId: string;
|
|
232
|
-
let currentToolCallId: string;
|
|
233
231
|
|
|
234
232
|
for await (const chunk of stream) {
|
|
235
233
|
switch (chunk.event) {
|
|
236
234
|
case "thread.message.created":
|
|
237
235
|
if (inFunctionCall) {
|
|
238
|
-
eventStream$.sendActionExecutionEnd(
|
|
236
|
+
eventStream$.sendActionExecutionEnd();
|
|
239
237
|
}
|
|
240
|
-
|
|
241
|
-
eventStream$.sendTextMessageStart({ messageId: currentMessageId });
|
|
238
|
+
eventStream$.sendTextMessageStart(chunk.data.id);
|
|
242
239
|
break;
|
|
243
240
|
case "thread.message.delta":
|
|
244
241
|
if (chunk.data.delta.content?.[0].type === "text") {
|
|
245
|
-
eventStream$.sendTextMessageContent(
|
|
246
|
-
messageId: currentMessageId,
|
|
247
|
-
content: chunk.data.delta.content?.[0].text.value,
|
|
248
|
-
});
|
|
242
|
+
eventStream$.sendTextMessageContent(chunk.data.delta.content?.[0].text.value);
|
|
249
243
|
}
|
|
250
244
|
break;
|
|
251
245
|
case "thread.message.completed":
|
|
252
|
-
eventStream$.sendTextMessageEnd(
|
|
246
|
+
eventStream$.sendTextMessageEnd();
|
|
253
247
|
break;
|
|
254
248
|
case "thread.run.step.delta":
|
|
255
249
|
let toolCallId: string | undefined;
|
|
@@ -266,25 +260,18 @@ export class OpenAIAssistantAdapter implements CopilotServiceAdapter {
|
|
|
266
260
|
|
|
267
261
|
if (toolCallName && toolCallId) {
|
|
268
262
|
if (inFunctionCall) {
|
|
269
|
-
eventStream$.sendActionExecutionEnd(
|
|
263
|
+
eventStream$.sendActionExecutionEnd();
|
|
270
264
|
}
|
|
271
265
|
inFunctionCall = true;
|
|
272
|
-
|
|
273
|
-
eventStream$.sendActionExecutionStart({
|
|
274
|
-
actionExecutionId: currentToolCallId,
|
|
275
|
-
actionName: toolCallName,
|
|
276
|
-
});
|
|
266
|
+
eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
|
|
277
267
|
} else if (toolCallArgs) {
|
|
278
|
-
eventStream$.sendActionExecutionArgs(
|
|
279
|
-
actionExecutionId: currentToolCallId,
|
|
280
|
-
args: toolCallArgs,
|
|
281
|
-
});
|
|
268
|
+
eventStream$.sendActionExecutionArgs(toolCallArgs);
|
|
282
269
|
}
|
|
283
270
|
break;
|
|
284
271
|
}
|
|
285
272
|
}
|
|
286
273
|
if (inFunctionCall) {
|
|
287
|
-
eventStream$.sendActionExecutionEnd(
|
|
274
|
+
eventStream$.sendActionExecutionEnd();
|
|
288
275
|
}
|
|
289
276
|
eventStream$.complete();
|
|
290
277
|
});
|