@copilotkit/runtime 1.4.8-coagents-v0-3.1 → 1.4.8-next.1
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 +5 -4
- package/__snapshots__/schema/schema.graphql +10 -7
- package/dist/{chunk-FVYNRYIB.mjs → chunk-5KJYPVQJ.mjs} +307 -423
- package/dist/chunk-5KJYPVQJ.mjs.map +1 -0
- package/dist/{chunk-RFF5IIZJ.mjs → chunk-B74M7FXG.mjs} +2 -3
- package/dist/chunk-B74M7FXG.mjs.map +1 -0
- package/dist/{chunk-BACNNHHI.mjs → chunk-CGGI46KC.mjs} +2 -2
- package/dist/{chunk-YFG3Q3YH.mjs → chunk-EU52BTKR.mjs} +2 -2
- package/dist/{chunk-MQJNZYAH.mjs → chunk-KO4QCMY4.mjs} +2 -2
- package/dist/{chunk-2PK2SFRB.mjs → chunk-W7GP2EOT.mjs} +106 -221
- package/dist/chunk-W7GP2EOT.mjs.map +1 -0
- package/dist/{copilot-runtime-b15b683d.d.ts → copilot-runtime-1c5bf72b.d.ts} +2 -2
- package/dist/graphql/types/converted/index.d.ts +1 -1
- package/dist/graphql/types/converted/index.js +1 -2
- package/dist/graphql/types/converted/index.js.map +1 -1
- package/dist/graphql/types/converted/index.mjs +1 -1
- package/dist/{groq-adapter-50aa9621.d.ts → groq-adapter-b6a40422.d.ts} +1 -1
- package/dist/{index-ff3fbc33.d.ts → index-10b1c870.d.ts} +8 -7
- package/dist/index.d.ts +5 -5
- package/dist/index.js +515 -745
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +10 -6
- package/dist/index.mjs.map +1 -1
- package/dist/{langserve-8ec29cba.d.ts → langserve-6245df39.d.ts} +14 -52
- package/dist/lib/index.d.ts +4 -4
- package/dist/lib/index.js +442 -706
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +6 -6
- package/dist/lib/integrations/index.d.ts +4 -4
- package/dist/lib/integrations/index.js +37 -71
- package/dist/lib/integrations/index.js.map +1 -1
- package/dist/lib/integrations/index.mjs +6 -6
- package/dist/lib/integrations/nest/index.d.ts +3 -3
- package/dist/lib/integrations/nest/index.js +37 -71
- package/dist/lib/integrations/nest/index.js.map +1 -1
- package/dist/lib/integrations/nest/index.mjs +4 -4
- package/dist/lib/integrations/node-express/index.d.ts +3 -3
- package/dist/lib/integrations/node-express/index.js +37 -71
- package/dist/lib/integrations/node-express/index.js.map +1 -1
- package/dist/lib/integrations/node-express/index.mjs +4 -4
- package/dist/lib/integrations/node-http/index.d.ts +3 -3
- package/dist/lib/integrations/node-http/index.js +37 -71
- package/dist/lib/integrations/node-http/index.js.map +1 -1
- package/dist/lib/integrations/node-http/index.mjs +3 -3
- package/dist/service-adapters/index.d.ts +36 -5
- package/dist/service-adapters/index.js +106 -219
- package/dist/service-adapters/index.js.map +1 -1
- package/dist/service-adapters/index.mjs +5 -1
- package/package.json +2 -2
- package/src/agents/langgraph/event-source.ts +148 -140
- package/src/agents/langgraph/events.ts +1 -1
- package/src/graphql/inputs/forwarded-parameters.input.ts +3 -0
- package/src/graphql/inputs/message.input.ts +3 -15
- package/src/graphql/resolvers/copilot.resolver.ts +6 -32
- package/src/graphql/types/converted/index.ts +3 -4
- package/src/graphql/types/copilot-response.type.ts +3 -12
- package/src/graphql/types/enums.ts +11 -0
- package/src/lib/runtime/remote-action-constructors.ts +62 -60
- package/src/lib/runtime/remote-actions.ts +0 -1
- package/src/lib/runtime/remote-lg-action.ts +140 -161
- package/src/service-adapters/anthropic/anthropic-adapter.ts +6 -16
- package/src/service-adapters/conversion.ts +1 -2
- package/src/service-adapters/events.ts +52 -111
- package/src/service-adapters/experimental/empty/empty-adapter.ts +33 -0
- package/src/service-adapters/experimental/ollama/ollama-adapter.ts +3 -7
- package/src/service-adapters/groq/groq-adapter.ts +8 -23
- package/src/service-adapters/index.ts +7 -1
- package/src/service-adapters/langchain/utils.ts +31 -49
- package/src/service-adapters/openai/openai-adapter.ts +9 -22
- package/src/service-adapters/openai/openai-assistant-adapter.ts +8 -22
- package/src/service-adapters/unify/unify-adapter.ts +11 -28
- package/dist/chunk-2PK2SFRB.mjs.map +0 -1
- package/dist/chunk-FVYNRYIB.mjs.map +0 -1
- package/dist/chunk-RFF5IIZJ.mjs.map +0 -1
- /package/dist/{chunk-BACNNHHI.mjs.map → chunk-CGGI46KC.mjs.map} +0 -0
- /package/dist/{chunk-YFG3Q3YH.mjs.map → chunk-EU52BTKR.mjs.map} +0 -0
- /package/dist/{chunk-MQJNZYAH.mjs.map → chunk-KO4QCMY4.mjs.map} +0 -0
|
@@ -15,8 +15,6 @@ import { GuardrailsResult } from "../graphql/types/guardrails-result.type";
|
|
|
15
15
|
import telemetry from "../lib/telemetry-client";
|
|
16
16
|
import { isLangGraphAgentAction } from "../lib/runtime/remote-actions";
|
|
17
17
|
import { ActionInput } from "../graphql/inputs/action.input";
|
|
18
|
-
import { ActionExecutionMessage, ResultMessage } from "../graphql/types/converted";
|
|
19
|
-
import { plainToInstance } from "class-transformer";
|
|
20
18
|
|
|
21
19
|
export enum RuntimeEventTypes {
|
|
22
20
|
TextMessageStart = "TextMessageStart",
|
|
@@ -29,22 +27,23 @@ export enum RuntimeEventTypes {
|
|
|
29
27
|
AgentStateMessage = "AgentStateMessage",
|
|
30
28
|
}
|
|
31
29
|
|
|
30
|
+
type FunctionCallScope = "client" | "server" | "passThrough";
|
|
31
|
+
|
|
32
32
|
export type RuntimeEvent =
|
|
33
|
-
| { type: RuntimeEventTypes.TextMessageStart; messageId: string
|
|
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,86 +77,47 @@ export class RuntimeEventSubject extends ReplaySubject<RuntimeEvent> {
|
|
|
78
77
|
super();
|
|
79
78
|
}
|
|
80
79
|
|
|
81
|
-
sendTextMessageStart({
|
|
82
|
-
messageId
|
|
83
|
-
parentMessageId,
|
|
84
|
-
}: {
|
|
85
|
-
messageId: string;
|
|
86
|
-
parentMessageId?: string;
|
|
87
|
-
}) {
|
|
88
|
-
this.next({ type: RuntimeEventTypes.TextMessageStart, messageId, parentMessageId });
|
|
80
|
+
sendTextMessageStart(messageId: string) {
|
|
81
|
+
this.next({ type: RuntimeEventTypes.TextMessageStart, messageId });
|
|
89
82
|
}
|
|
90
83
|
|
|
91
|
-
sendTextMessageContent(
|
|
92
|
-
this.next({ type: RuntimeEventTypes.TextMessageContent, content
|
|
84
|
+
sendTextMessageContent(content: string) {
|
|
85
|
+
this.next({ type: RuntimeEventTypes.TextMessageContent, content });
|
|
93
86
|
}
|
|
94
87
|
|
|
95
|
-
sendTextMessageEnd(
|
|
96
|
-
this.next({ type: RuntimeEventTypes.TextMessageEnd
|
|
88
|
+
sendTextMessageEnd() {
|
|
89
|
+
this.next({ type: RuntimeEventTypes.TextMessageEnd });
|
|
97
90
|
}
|
|
98
91
|
|
|
99
92
|
sendTextMessage(messageId: string, content: string) {
|
|
100
|
-
this.sendTextMessageStart(
|
|
101
|
-
this.sendTextMessageContent(
|
|
102
|
-
this.sendTextMessageEnd(
|
|
93
|
+
this.sendTextMessageStart(messageId);
|
|
94
|
+
this.sendTextMessageContent(content);
|
|
95
|
+
this.sendTextMessageEnd();
|
|
103
96
|
}
|
|
104
97
|
|
|
105
|
-
sendActionExecutionStart({
|
|
106
|
-
actionExecutionId,
|
|
107
|
-
actionName,
|
|
108
|
-
parentMessageId,
|
|
109
|
-
}: {
|
|
110
|
-
actionExecutionId: string;
|
|
111
|
-
actionName: string;
|
|
112
|
-
parentMessageId?: string;
|
|
113
|
-
}) {
|
|
98
|
+
sendActionExecutionStart(actionExecutionId: string, actionName: string) {
|
|
114
99
|
this.next({
|
|
115
100
|
type: RuntimeEventTypes.ActionExecutionStart,
|
|
116
101
|
actionExecutionId,
|
|
117
102
|
actionName,
|
|
118
|
-
parentMessageId,
|
|
119
103
|
});
|
|
120
104
|
}
|
|
121
105
|
|
|
122
|
-
sendActionExecutionArgs({
|
|
123
|
-
|
|
124
|
-
args,
|
|
125
|
-
}: {
|
|
126
|
-
actionExecutionId: string;
|
|
127
|
-
args: string;
|
|
128
|
-
}) {
|
|
129
|
-
this.next({ type: RuntimeEventTypes.ActionExecutionArgs, args, actionExecutionId });
|
|
106
|
+
sendActionExecutionArgs(args: string) {
|
|
107
|
+
this.next({ type: RuntimeEventTypes.ActionExecutionArgs, args });
|
|
130
108
|
}
|
|
131
109
|
|
|
132
|
-
sendActionExecutionEnd(
|
|
133
|
-
this.next({ type: RuntimeEventTypes.ActionExecutionEnd
|
|
110
|
+
sendActionExecutionEnd() {
|
|
111
|
+
this.next({ type: RuntimeEventTypes.ActionExecutionEnd });
|
|
134
112
|
}
|
|
135
113
|
|
|
136
|
-
sendActionExecution({
|
|
137
|
-
actionExecutionId,
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
parentMessageId,
|
|
141
|
-
}: {
|
|
142
|
-
actionExecutionId: string;
|
|
143
|
-
actionName: string;
|
|
144
|
-
args: string;
|
|
145
|
-
parentMessageId?: string;
|
|
146
|
-
}) {
|
|
147
|
-
this.sendActionExecutionStart({ actionExecutionId, actionName, parentMessageId });
|
|
148
|
-
this.sendActionExecutionArgs({ actionExecutionId, args });
|
|
149
|
-
this.sendActionExecutionEnd({ actionExecutionId });
|
|
114
|
+
sendActionExecution(actionExecutionId: string, toolName: string, args: string) {
|
|
115
|
+
this.sendActionExecutionStart(actionExecutionId, toolName);
|
|
116
|
+
this.sendActionExecutionArgs(args);
|
|
117
|
+
this.sendActionExecutionEnd();
|
|
150
118
|
}
|
|
151
119
|
|
|
152
|
-
sendActionExecutionResult({
|
|
153
|
-
actionExecutionId,
|
|
154
|
-
actionName,
|
|
155
|
-
result,
|
|
156
|
-
}: {
|
|
157
|
-
actionExecutionId: string;
|
|
158
|
-
actionName: string;
|
|
159
|
-
result: string;
|
|
160
|
-
}) {
|
|
120
|
+
sendActionExecutionResult(actionExecutionId: string, actionName: string, result: string) {
|
|
161
121
|
this.next({
|
|
162
122
|
type: RuntimeEventTypes.ActionExecutionResult,
|
|
163
123
|
actionName,
|
|
@@ -166,25 +126,16 @@ export class RuntimeEventSubject extends ReplaySubject<RuntimeEvent> {
|
|
|
166
126
|
});
|
|
167
127
|
}
|
|
168
128
|
|
|
169
|
-
sendAgentStateMessage(
|
|
170
|
-
threadId,
|
|
171
|
-
agentName,
|
|
172
|
-
nodeName,
|
|
173
|
-
runId,
|
|
174
|
-
active,
|
|
175
|
-
role,
|
|
176
|
-
state,
|
|
177
|
-
running,
|
|
178
|
-
|
|
179
|
-
threadId: string;
|
|
180
|
-
agentName: string;
|
|
181
|
-
nodeName: string;
|
|
182
|
-
runId: string;
|
|
183
|
-
active: boolean;
|
|
184
|
-
role: string;
|
|
185
|
-
state: string;
|
|
186
|
-
running: boolean;
|
|
187
|
-
}) {
|
|
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
|
+
) {
|
|
188
139
|
this.next({
|
|
189
140
|
type: RuntimeEventTypes.AgentStateMessage,
|
|
190
141
|
threadId,
|
|
@@ -232,6 +183,17 @@ export class RuntimeEventSource {
|
|
|
232
183
|
this.sendErrorMessageToChat();
|
|
233
184
|
});
|
|
234
185
|
return this.eventStream$.pipe(
|
|
186
|
+
// mark tools for server side execution
|
|
187
|
+
map((event) => {
|
|
188
|
+
if (event.type === RuntimeEventTypes.ActionExecutionStart) {
|
|
189
|
+
if (event.scope !== "passThrough") {
|
|
190
|
+
event.scope = serverSideActions.find((action) => action.name === event.actionName)
|
|
191
|
+
? "server"
|
|
192
|
+
: "client";
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return event;
|
|
196
|
+
}),
|
|
235
197
|
// track state
|
|
236
198
|
scan(
|
|
237
199
|
(acc, event) => {
|
|
@@ -241,8 +203,7 @@ export class RuntimeEventSource {
|
|
|
241
203
|
acc = { ...acc };
|
|
242
204
|
|
|
243
205
|
if (event.type === RuntimeEventTypes.ActionExecutionStart) {
|
|
244
|
-
acc.callActionServerSide =
|
|
245
|
-
serverSideActions.find((action) => action.name === event.actionName) !== undefined;
|
|
206
|
+
acc.callActionServerSide = event.scope === "server";
|
|
246
207
|
acc.args = "";
|
|
247
208
|
acc.actionExecutionId = event.actionExecutionId;
|
|
248
209
|
if (acc.callActionServerSide) {
|
|
@@ -320,34 +281,14 @@ async function executeAction(
|
|
|
320
281
|
|
|
321
282
|
// handle LangGraph agents
|
|
322
283
|
if (isLangGraphAgentAction(action)) {
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
const agentExecution = plainToInstance(ActionExecutionMessage, {
|
|
326
|
-
id: actionExecutionId,
|
|
327
|
-
createdAt: new Date(),
|
|
328
|
-
name: action.name,
|
|
329
|
-
arguments: JSON.parse(actionArguments),
|
|
330
|
-
parentMessageId: actionExecutionId,
|
|
331
|
-
});
|
|
332
|
-
|
|
333
|
-
const agentExecutionResult = plainToInstance(ResultMessage, {
|
|
334
|
-
id: "result-" + actionExecutionId,
|
|
335
|
-
createdAt: new Date(),
|
|
336
|
-
actionExecutionId,
|
|
337
|
-
actionName: action.name,
|
|
338
|
-
result,
|
|
339
|
-
});
|
|
340
|
-
|
|
341
|
-
eventStream$.sendActionExecutionResult({
|
|
284
|
+
eventStream$.sendActionExecutionResult(
|
|
342
285
|
actionExecutionId,
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
286
|
+
action.name,
|
|
287
|
+
`${action.name} agent started`,
|
|
288
|
+
);
|
|
347
289
|
const stream = await action.langGraphAgentHandler({
|
|
348
290
|
name: action.name,
|
|
349
291
|
actionInputsWithoutAgents,
|
|
350
|
-
additionalMessages: [agentExecution, agentExecutionResult],
|
|
351
292
|
});
|
|
352
293
|
|
|
353
294
|
// forward to eventStream$
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CopilotKit Empty Adapter
|
|
3
|
+
*
|
|
4
|
+
* This adapter is meant to preserve adherence to runtime requirements, while doing nothing
|
|
5
|
+
* Ideal if you don't want to connect an LLM the to the runtime, and only use your LangGraph agent.
|
|
6
|
+
* Be aware that Copilot Suggestions will not work if you use this adapter
|
|
7
|
+
*
|
|
8
|
+
* ## Example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { CopilotRuntime, ExperimentalEmptyAdapter } from "@copilotkit/runtime";
|
|
12
|
+
*
|
|
13
|
+
* const copilotKit = new CopilotRuntime();
|
|
14
|
+
*
|
|
15
|
+
* return new ExperimentalEmptyAdapter();
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
import {
|
|
19
|
+
CopilotServiceAdapter,
|
|
20
|
+
CopilotRuntimeChatCompletionRequest,
|
|
21
|
+
CopilotRuntimeChatCompletionResponse,
|
|
22
|
+
} from "../../service-adapter";
|
|
23
|
+
import { randomId } from "@copilotkit/shared";
|
|
24
|
+
|
|
25
|
+
export class ExperimentalEmptyAdapter implements CopilotServiceAdapter {
|
|
26
|
+
async process(
|
|
27
|
+
request: CopilotRuntimeChatCompletionRequest,
|
|
28
|
+
): Promise<CopilotRuntimeChatCompletionResponse> {
|
|
29
|
+
return {
|
|
30
|
+
threadId: request.threadId || randomId(),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -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
|
|
|
@@ -106,9 +106,6 @@ export class GroqAdapter implements CopilotServiceAdapter {
|
|
|
106
106
|
|
|
107
107
|
eventSource.stream(async (eventStream$) => {
|
|
108
108
|
let mode: "function" | "message" | null = null;
|
|
109
|
-
let currentMessageId: string;
|
|
110
|
-
let currentToolCallId: string;
|
|
111
|
-
|
|
112
109
|
for await (const chunk of stream) {
|
|
113
110
|
const toolCall = chunk.choices[0].delta.tool_calls?.[0];
|
|
114
111
|
const content = chunk.choices[0].delta.content;
|
|
@@ -118,48 +115,36 @@ export class GroqAdapter implements CopilotServiceAdapter {
|
|
|
118
115
|
// If toolCall?.id is defined, it means a new tool call starts.
|
|
119
116
|
if (mode === "message" && toolCall?.id) {
|
|
120
117
|
mode = null;
|
|
121
|
-
eventStream$.sendTextMessageEnd(
|
|
118
|
+
eventStream$.sendTextMessageEnd();
|
|
122
119
|
} else if (mode === "function" && (toolCall === undefined || toolCall?.id)) {
|
|
123
120
|
mode = null;
|
|
124
|
-
eventStream$.sendActionExecutionEnd(
|
|
121
|
+
eventStream$.sendActionExecutionEnd();
|
|
125
122
|
}
|
|
126
123
|
|
|
127
124
|
// If we send a new message type, send the appropriate start event.
|
|
128
125
|
if (mode === null) {
|
|
129
126
|
if (toolCall?.id) {
|
|
130
127
|
mode = "function";
|
|
131
|
-
|
|
132
|
-
eventStream$.sendActionExecutionStart({
|
|
133
|
-
actionExecutionId: currentToolCallId,
|
|
134
|
-
actionName: toolCall!.function!.name,
|
|
135
|
-
parentMessageId: chunk.id,
|
|
136
|
-
});
|
|
128
|
+
eventStream$.sendActionExecutionStart(toolCall!.id, toolCall!.function!.name);
|
|
137
129
|
} else if (content) {
|
|
138
130
|
mode = "message";
|
|
139
|
-
|
|
140
|
-
eventStream$.sendTextMessageStart({ messageId: currentMessageId });
|
|
131
|
+
eventStream$.sendTextMessageStart(chunk.id);
|
|
141
132
|
}
|
|
142
133
|
}
|
|
143
134
|
|
|
144
135
|
// send the content events
|
|
145
136
|
if (mode === "message" && content) {
|
|
146
|
-
eventStream$.sendTextMessageContent(
|
|
147
|
-
messageId: currentMessageId,
|
|
148
|
-
content,
|
|
149
|
-
});
|
|
137
|
+
eventStream$.sendTextMessageContent(content);
|
|
150
138
|
} else if (mode === "function" && toolCall?.function?.arguments) {
|
|
151
|
-
eventStream$.sendActionExecutionArgs(
|
|
152
|
-
actionExecutionId: currentToolCallId,
|
|
153
|
-
args: toolCall.function.arguments,
|
|
154
|
-
});
|
|
139
|
+
eventStream$.sendActionExecutionArgs(toolCall.function.arguments);
|
|
155
140
|
}
|
|
156
141
|
}
|
|
157
142
|
|
|
158
143
|
// send the end events
|
|
159
144
|
if (mode === "message") {
|
|
160
|
-
eventStream$.sendTextMessageEnd(
|
|
145
|
+
eventStream$.sendTextMessageEnd();
|
|
161
146
|
} else if (mode === "function") {
|
|
162
|
-
eventStream$.sendActionExecutionEnd(
|
|
147
|
+
eventStream$.sendActionExecutionEnd();
|
|
163
148
|
}
|
|
164
149
|
|
|
165
150
|
eventStream$.complete();
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
export type {
|
|
1
|
+
export type {
|
|
2
|
+
CopilotRuntimeChatCompletionRequest,
|
|
3
|
+
CopilotRuntimeChatCompletionResponse,
|
|
4
|
+
CopilotServiceAdapter,
|
|
5
|
+
} from "./service-adapter";
|
|
2
6
|
export type { RemoteChainParameters } from "./langchain/langserve";
|
|
3
7
|
export { RemoteChain } from "./langchain/langserve";
|
|
4
8
|
export * from "./openai/openai-adapter";
|
|
@@ -8,3 +12,5 @@ export * from "./openai/openai-assistant-adapter";
|
|
|
8
12
|
export * from "./unify/unify-adapter";
|
|
9
13
|
export * from "./groq/groq-adapter";
|
|
10
14
|
export * from "./anthropic/anthropic-adapter";
|
|
15
|
+
export * from "./experimental/ollama/ollama-adapter";
|
|
16
|
+
export * from "./experimental/empty/empty-adapter";
|
|
@@ -97,11 +97,11 @@ function maybeSendActionExecutionResultIsMessage(
|
|
|
97
97
|
// language models need a result after the function call
|
|
98
98
|
// we simply let them know that we are sending a message
|
|
99
99
|
if (actionExecution) {
|
|
100
|
-
eventStream$.sendActionExecutionResult(
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
100
|
+
eventStream$.sendActionExecutionResult(
|
|
101
|
+
actionExecution.id,
|
|
102
|
+
actionExecution.name,
|
|
103
|
+
"Sending a message",
|
|
104
|
+
);
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
107
|
|
|
@@ -120,11 +120,7 @@ export async function streamLangChainResponse({
|
|
|
120
120
|
eventStream$.sendTextMessage(randomId(), result);
|
|
121
121
|
} else {
|
|
122
122
|
// Send as a result
|
|
123
|
-
eventStream$.sendActionExecutionResult(
|
|
124
|
-
actionExecutionId: actionExecution.id,
|
|
125
|
-
actionName: actionExecution.name,
|
|
126
|
-
result: result,
|
|
127
|
-
});
|
|
123
|
+
eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, result);
|
|
128
124
|
}
|
|
129
125
|
}
|
|
130
126
|
|
|
@@ -137,11 +133,11 @@ export async function streamLangChainResponse({
|
|
|
137
133
|
eventStream$.sendTextMessage(randomId(), result.content as string);
|
|
138
134
|
}
|
|
139
135
|
for (const toolCall of result.tool_calls) {
|
|
140
|
-
eventStream$.sendActionExecution(
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
136
|
+
eventStream$.sendActionExecution(
|
|
137
|
+
toolCall.id || randomId(),
|
|
138
|
+
toolCall.name,
|
|
139
|
+
JSON.stringify(toolCall.args),
|
|
140
|
+
);
|
|
145
141
|
}
|
|
146
142
|
}
|
|
147
143
|
|
|
@@ -155,11 +151,11 @@ export async function streamLangChainResponse({
|
|
|
155
151
|
}
|
|
156
152
|
if (result.lc_kwargs?.tool_calls) {
|
|
157
153
|
for (const toolCall of result.lc_kwargs?.tool_calls) {
|
|
158
|
-
eventStream$.sendActionExecution(
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
154
|
+
eventStream$.sendActionExecution(
|
|
155
|
+
toolCall.id || randomId(),
|
|
156
|
+
toolCall.name,
|
|
157
|
+
JSON.stringify(toolCall.args),
|
|
158
|
+
);
|
|
163
159
|
}
|
|
164
160
|
}
|
|
165
161
|
}
|
|
@@ -172,7 +168,6 @@ export async function streamLangChainResponse({
|
|
|
172
168
|
let reader = result.getReader();
|
|
173
169
|
|
|
174
170
|
let mode: "function" | "message" | null = null;
|
|
175
|
-
let currentMessageId: string;
|
|
176
171
|
|
|
177
172
|
const toolCallDetails = {
|
|
178
173
|
name: null,
|
|
@@ -221,10 +216,10 @@ export async function streamLangChainResponse({
|
|
|
221
216
|
// If toolCallName is defined, it means a new tool call starts.
|
|
222
217
|
if (mode === "message" && (toolCallId || done)) {
|
|
223
218
|
mode = null;
|
|
224
|
-
eventStream$.sendTextMessageEnd(
|
|
219
|
+
eventStream$.sendTextMessageEnd();
|
|
225
220
|
} else if (mode === "function" && (!hasToolCall || done)) {
|
|
226
221
|
mode = null;
|
|
227
|
-
eventStream$.sendActionExecutionEnd(
|
|
222
|
+
eventStream$.sendActionExecutionEnd();
|
|
228
223
|
}
|
|
229
224
|
|
|
230
225
|
if (done) {
|
|
@@ -235,39 +230,26 @@ export async function streamLangChainResponse({
|
|
|
235
230
|
if (mode === null) {
|
|
236
231
|
if (hasToolCall && toolCallId && toolCallName) {
|
|
237
232
|
mode = "function";
|
|
238
|
-
eventStream$.sendActionExecutionStart(
|
|
239
|
-
actionExecutionId: toolCallId,
|
|
240
|
-
actionName: toolCallName,
|
|
241
|
-
parentMessageId: value.lc_kwargs?.id,
|
|
242
|
-
});
|
|
233
|
+
eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
|
|
243
234
|
} else if (content) {
|
|
244
235
|
mode = "message";
|
|
245
|
-
|
|
246
|
-
eventStream$.sendTextMessageStart({ messageId: currentMessageId });
|
|
236
|
+
eventStream$.sendTextMessageStart(randomId());
|
|
247
237
|
}
|
|
248
238
|
}
|
|
249
239
|
|
|
250
240
|
// send the content events
|
|
251
241
|
if (mode === "message" && content) {
|
|
252
|
-
eventStream$.sendTextMessageContent(
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
});
|
|
242
|
+
eventStream$.sendTextMessageContent(
|
|
243
|
+
Array.isArray(content) ? (content[0]?.text ?? "") : content,
|
|
244
|
+
);
|
|
256
245
|
} else if (mode === "function" && toolCallArgs) {
|
|
257
246
|
// For calls of the same tool with different index, we seal last tool call and register a new one
|
|
258
247
|
if (toolCallDetails.index !== toolCallDetails.prevIndex) {
|
|
259
|
-
eventStream$.sendActionExecutionEnd(
|
|
260
|
-
eventStream$.sendActionExecutionStart(
|
|
261
|
-
actionExecutionId: toolCallId,
|
|
262
|
-
actionName: toolCallName,
|
|
263
|
-
parentMessageId: value.lc_kwargs?.id,
|
|
264
|
-
});
|
|
248
|
+
eventStream$.sendActionExecutionEnd();
|
|
249
|
+
eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
|
|
265
250
|
toolCallDetails.prevIndex = toolCallDetails.index;
|
|
266
251
|
}
|
|
267
|
-
eventStream$.sendActionExecutionArgs(
|
|
268
|
-
actionExecutionId: toolCallId,
|
|
269
|
-
args: toolCallArgs,
|
|
270
|
-
});
|
|
252
|
+
eventStream$.sendActionExecutionArgs(toolCallArgs);
|
|
271
253
|
}
|
|
272
254
|
} catch (error) {
|
|
273
255
|
console.error("Error reading from stream", error);
|
|
@@ -275,11 +257,11 @@ export async function streamLangChainResponse({
|
|
|
275
257
|
}
|
|
276
258
|
}
|
|
277
259
|
} else if (actionExecution) {
|
|
278
|
-
eventStream$.sendActionExecutionResult(
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
260
|
+
eventStream$.sendActionExecutionResult(
|
|
261
|
+
actionExecution.id,
|
|
262
|
+
actionExecution.name,
|
|
263
|
+
encodeResult(result),
|
|
264
|
+
);
|
|
283
265
|
}
|
|
284
266
|
|
|
285
267
|
// unsupported type
|
|
@@ -140,8 +140,7 @@ export class OpenAIAdapter implements CopilotServiceAdapter {
|
|
|
140
140
|
|
|
141
141
|
eventSource.stream(async (eventStream$) => {
|
|
142
142
|
let mode: "function" | "message" | null = null;
|
|
143
|
-
|
|
144
|
-
let currentToolCallId: string;
|
|
143
|
+
|
|
145
144
|
for await (const chunk of stream) {
|
|
146
145
|
if (chunk.choices.length === 0) {
|
|
147
146
|
continue;
|
|
@@ -155,48 +154,36 @@ export class OpenAIAdapter implements CopilotServiceAdapter {
|
|
|
155
154
|
// If toolCall?.id is defined, it means a new tool call starts.
|
|
156
155
|
if (mode === "message" && toolCall?.id) {
|
|
157
156
|
mode = null;
|
|
158
|
-
eventStream$.sendTextMessageEnd(
|
|
157
|
+
eventStream$.sendTextMessageEnd();
|
|
159
158
|
} else if (mode === "function" && (toolCall === undefined || toolCall?.id)) {
|
|
160
159
|
mode = null;
|
|
161
|
-
eventStream$.sendActionExecutionEnd(
|
|
160
|
+
eventStream$.sendActionExecutionEnd();
|
|
162
161
|
}
|
|
163
162
|
|
|
164
163
|
// If we send a new message type, send the appropriate start event.
|
|
165
164
|
if (mode === null) {
|
|
166
165
|
if (toolCall?.id) {
|
|
167
166
|
mode = "function";
|
|
168
|
-
|
|
169
|
-
eventStream$.sendActionExecutionStart({
|
|
170
|
-
actionExecutionId: currentToolCallId,
|
|
171
|
-
parentMessageId: chunk.id,
|
|
172
|
-
actionName: toolCall!.function!.name,
|
|
173
|
-
});
|
|
167
|
+
eventStream$.sendActionExecutionStart(toolCall!.id, toolCall!.function!.name);
|
|
174
168
|
} else if (content) {
|
|
175
169
|
mode = "message";
|
|
176
|
-
|
|
177
|
-
eventStream$.sendTextMessageStart({ messageId: currentMessageId });
|
|
170
|
+
eventStream$.sendTextMessageStart(chunk.id);
|
|
178
171
|
}
|
|
179
172
|
}
|
|
180
173
|
|
|
181
174
|
// send the content events
|
|
182
175
|
if (mode === "message" && content) {
|
|
183
|
-
eventStream$.sendTextMessageContent(
|
|
184
|
-
messageId: currentMessageId,
|
|
185
|
-
content: content,
|
|
186
|
-
});
|
|
176
|
+
eventStream$.sendTextMessageContent(content);
|
|
187
177
|
} else if (mode === "function" && toolCall?.function?.arguments) {
|
|
188
|
-
eventStream$.sendActionExecutionArgs(
|
|
189
|
-
actionExecutionId: currentToolCallId,
|
|
190
|
-
args: toolCall.function.arguments,
|
|
191
|
-
});
|
|
178
|
+
eventStream$.sendActionExecutionArgs(toolCall.function.arguments);
|
|
192
179
|
}
|
|
193
180
|
}
|
|
194
181
|
|
|
195
182
|
// send the end events
|
|
196
183
|
if (mode === "message") {
|
|
197
|
-
eventStream$.sendTextMessageEnd(
|
|
184
|
+
eventStream$.sendTextMessageEnd();
|
|
198
185
|
} else if (mode === "function") {
|
|
199
|
-
eventStream$.sendActionExecutionEnd(
|
|
186
|
+
eventStream$.sendActionExecutionEnd();
|
|
200
187
|
}
|
|
201
188
|
|
|
202
189
|
eventStream$.complete();
|
|
@@ -226,28 +226,22 @@ export class OpenAIAssistantAdapter implements CopilotServiceAdapter {
|
|
|
226
226
|
private async streamResponse(stream: AssistantStream, eventSource: RuntimeEventSource) {
|
|
227
227
|
eventSource.stream(async (eventStream$) => {
|
|
228
228
|
let inFunctionCall = false;
|
|
229
|
-
let currentMessageId: string;
|
|
230
|
-
let currentToolCallId: string;
|
|
231
229
|
|
|
232
230
|
for await (const chunk of stream) {
|
|
233
231
|
switch (chunk.event) {
|
|
234
232
|
case "thread.message.created":
|
|
235
233
|
if (inFunctionCall) {
|
|
236
|
-
eventStream$.sendActionExecutionEnd(
|
|
234
|
+
eventStream$.sendActionExecutionEnd();
|
|
237
235
|
}
|
|
238
|
-
|
|
239
|
-
eventStream$.sendTextMessageStart({ messageId: currentMessageId });
|
|
236
|
+
eventStream$.sendTextMessageStart(chunk.data.id);
|
|
240
237
|
break;
|
|
241
238
|
case "thread.message.delta":
|
|
242
239
|
if (chunk.data.delta.content?.[0].type === "text") {
|
|
243
|
-
eventStream$.sendTextMessageContent(
|
|
244
|
-
messageId: currentMessageId,
|
|
245
|
-
content: chunk.data.delta.content?.[0].text.value,
|
|
246
|
-
});
|
|
240
|
+
eventStream$.sendTextMessageContent(chunk.data.delta.content?.[0].text.value);
|
|
247
241
|
}
|
|
248
242
|
break;
|
|
249
243
|
case "thread.message.completed":
|
|
250
|
-
eventStream$.sendTextMessageEnd(
|
|
244
|
+
eventStream$.sendTextMessageEnd();
|
|
251
245
|
break;
|
|
252
246
|
case "thread.run.step.delta":
|
|
253
247
|
let toolCallId: string | undefined;
|
|
@@ -264,26 +258,18 @@ export class OpenAIAssistantAdapter implements CopilotServiceAdapter {
|
|
|
264
258
|
|
|
265
259
|
if (toolCallName && toolCallId) {
|
|
266
260
|
if (inFunctionCall) {
|
|
267
|
-
eventStream$.sendActionExecutionEnd(
|
|
261
|
+
eventStream$.sendActionExecutionEnd();
|
|
268
262
|
}
|
|
269
263
|
inFunctionCall = true;
|
|
270
|
-
|
|
271
|
-
eventStream$.sendActionExecutionStart({
|
|
272
|
-
actionExecutionId: currentToolCallId,
|
|
273
|
-
parentMessageId: chunk.data.id,
|
|
274
|
-
actionName: toolCallName,
|
|
275
|
-
});
|
|
264
|
+
eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
|
|
276
265
|
} else if (toolCallArgs) {
|
|
277
|
-
eventStream$.sendActionExecutionArgs(
|
|
278
|
-
actionExecutionId: currentToolCallId,
|
|
279
|
-
args: toolCallArgs,
|
|
280
|
-
});
|
|
266
|
+
eventStream$.sendActionExecutionArgs(toolCallArgs);
|
|
281
267
|
}
|
|
282
268
|
break;
|
|
283
269
|
}
|
|
284
270
|
}
|
|
285
271
|
if (inFunctionCall) {
|
|
286
|
-
eventStream$.sendActionExecutionEnd(
|
|
272
|
+
eventStream$.sendActionExecutionEnd();
|
|
287
273
|
}
|
|
288
274
|
eventStream$.complete();
|
|
289
275
|
});
|