@copilotkit/runtime 1.1.3-feat-runtime-remote-actions.2 → 1.2.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 +17 -13
- package/__snapshots__/schema/schema.graphql +0 -41
- package/dist/{chunk-BJ2LVHWA.mjs → chunk-736EEICU.mjs} +3 -3
- package/dist/{chunk-W7RRKPE2.mjs → chunk-DMO6FA25.mjs} +2 -2
- package/dist/{chunk-JYLUCJP7.mjs → chunk-DYF5MUAH.mjs} +2 -2
- package/dist/{chunk-TBZGOJJX.mjs → chunk-GEIBJJQ4.mjs} +2 -14
- package/dist/chunk-GEIBJJQ4.mjs.map +1 -0
- package/dist/chunk-PB24CCIJ.mjs +158 -0
- package/dist/chunk-PB24CCIJ.mjs.map +1 -0
- package/dist/{chunk-7FSNQXPE.mjs → chunk-Q5ZTE7WH.mjs} +158 -786
- package/dist/chunk-Q5ZTE7WH.mjs.map +1 -0
- package/dist/{chunk-73NMP3DI.mjs → chunk-URMISMK2.mjs} +2 -2
- package/dist/{chunk-WRPRFRSL.mjs → chunk-YMUS43FR.mjs} +2 -2
- package/dist/graphql/types/converted/index.d.ts +1 -1
- package/dist/graphql/types/converted/index.js +0 -13
- package/dist/graphql/types/converted/index.js.map +1 -1
- package/dist/graphql/types/converted/index.mjs +1 -3
- package/dist/{index-079752b9.d.ts → index-13aa818e.d.ts} +1 -1
- package/dist/{index-0476e4f7.d.ts → index-aa091e3c.d.ts} +2 -24
- package/dist/index.d.ts +7 -7
- package/dist/index.js +245 -982
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +8 -8
- package/dist/{langserve-d6073a3b.d.ts → langserve-a54438c6.d.ts} +11 -24
- package/dist/lib/index.d.ts +7 -7
- package/dist/lib/index.js +245 -982
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +8 -8
- package/dist/lib/integrations/index.d.ts +6 -6
- package/dist/lib/integrations/index.js +182 -437
- 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 +5 -5
- package/dist/lib/integrations/nest/index.js +182 -437
- 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 +5 -5
- package/dist/lib/integrations/node-express/index.js +182 -437
- 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 +5 -5
- package/dist/lib/integrations/node-http/index.js +182 -437
- 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 +3 -3
- package/dist/service-adapters/index.js.map +1 -1
- package/dist/service-adapters/index.mjs +3 -3
- package/dist/{copilot-runtime-d427e991.d.ts → shared-c5362338.d.ts} +38 -65
- package/package.json +5 -7
- package/src/graphql/inputs/generate-copilot-response.input.ts +0 -11
- package/src/graphql/inputs/message.input.ts +0 -30
- package/src/graphql/resolvers/copilot.resolver.ts +12 -56
- package/src/graphql/types/converted/index.ts +0 -15
- package/src/graphql/types/copilot-response.type.ts +0 -29
- package/src/graphql/types/enums.ts +0 -1
- package/src/lib/copilot-runtime.ts +231 -0
- package/src/lib/index.ts +1 -1
- package/src/lib/integrations/shared.ts +1 -1
- package/src/service-adapters/conversion.ts +0 -16
- package/src/service-adapters/events.ts +19 -101
- package/dist/chunk-5TYP4A3G.mjs +0 -260
- package/dist/chunk-5TYP4A3G.mjs.map +0 -1
- package/dist/chunk-7FSNQXPE.mjs.map +0 -1
- package/dist/chunk-TBZGOJJX.mjs.map +0 -1
- package/src/agents/langgraph/event-source.ts +0 -222
- package/src/agents/langgraph/events.ts +0 -309
- package/src/graphql/inputs/agent-session.input.ts +0 -13
- package/src/graphql/inputs/agent-state.input.ts +0 -10
- package/src/lib/runtime/copilot-runtime.ts +0 -360
- package/src/lib/runtime/remote-actions.ts +0 -241
- /package/dist/{chunk-BJ2LVHWA.mjs.map → chunk-736EEICU.mjs.map} +0 -0
- /package/dist/{chunk-W7RRKPE2.mjs.map → chunk-DMO6FA25.mjs.map} +0 -0
- /package/dist/{chunk-JYLUCJP7.mjs.map → chunk-DYF5MUAH.mjs.map} +0 -0
- /package/dist/{chunk-73NMP3DI.mjs.map → chunk-URMISMK2.mjs.map} +0 -0
- /package/dist/{chunk-WRPRFRSL.mjs.map → chunk-YMUS43FR.mjs.map} +0 -0
|
@@ -1,309 +0,0 @@
|
|
|
1
|
-
export enum LangGraphEventTypes {
|
|
2
|
-
OnChainStart = "on_chain_start",
|
|
3
|
-
OnChainStream = "on_chain_stream",
|
|
4
|
-
OnChainEnd = "on_chain_end",
|
|
5
|
-
OnChatModelStart = "on_chat_model_start",
|
|
6
|
-
OnChatModelStream = "on_chat_model_stream",
|
|
7
|
-
OnChatModelEnd = "on_chat_model_end",
|
|
8
|
-
OnToolStart = "on_tool_start",
|
|
9
|
-
OnToolEnd = "on_tool_end",
|
|
10
|
-
OnCopilotKitStateSync = "on_copilotkit_state_sync",
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
type LangGraphOnCopilotKitStateSyncEvent = {
|
|
14
|
-
event: LangGraphEventTypes.OnCopilotKitStateSync;
|
|
15
|
-
thread_id: string;
|
|
16
|
-
agent_name: string;
|
|
17
|
-
node_name: string;
|
|
18
|
-
run_id: string;
|
|
19
|
-
active: boolean;
|
|
20
|
-
role: string;
|
|
21
|
-
state: any;
|
|
22
|
-
running: boolean;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
type LangGraphOnChainStartEvent = {
|
|
26
|
-
event: LangGraphEventTypes.OnChainStart;
|
|
27
|
-
run_id: string;
|
|
28
|
-
name: string;
|
|
29
|
-
tags: string[];
|
|
30
|
-
metadata: { thread_id: string };
|
|
31
|
-
data: {
|
|
32
|
-
input: any;
|
|
33
|
-
};
|
|
34
|
-
parent_ids: string[];
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
type LangGraphOnChainEndEvent = {
|
|
38
|
-
event: LangGraphEventTypes.OnChainEnd;
|
|
39
|
-
name: string;
|
|
40
|
-
run_id: string;
|
|
41
|
-
tags: string[];
|
|
42
|
-
metadata: {
|
|
43
|
-
thread_id: string;
|
|
44
|
-
langgraph_step: number;
|
|
45
|
-
langgraph_node: string;
|
|
46
|
-
langgraph_triggers: string[];
|
|
47
|
-
langgraph_task_idx: number;
|
|
48
|
-
thread_ts: string;
|
|
49
|
-
};
|
|
50
|
-
data: {
|
|
51
|
-
input: any;
|
|
52
|
-
output: any;
|
|
53
|
-
};
|
|
54
|
-
parent_ids: string[];
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
type LangGraphOnChatModelStartEvent = {
|
|
58
|
-
event: LangGraphEventTypes.OnChatModelStart;
|
|
59
|
-
name: string;
|
|
60
|
-
run_id: string;
|
|
61
|
-
tags: string[];
|
|
62
|
-
metadata: {
|
|
63
|
-
thread_id: string;
|
|
64
|
-
langgraph_step: number;
|
|
65
|
-
langgraph_node: string;
|
|
66
|
-
langgraph_triggers: string[];
|
|
67
|
-
langgraph_task_idx: number;
|
|
68
|
-
thread_ts: string;
|
|
69
|
-
ls_provider: string;
|
|
70
|
-
ls_model_name: string;
|
|
71
|
-
ls_model_type: string;
|
|
72
|
-
ls_temperature: number;
|
|
73
|
-
};
|
|
74
|
-
data: {
|
|
75
|
-
input: {
|
|
76
|
-
messages: {
|
|
77
|
-
lc: number;
|
|
78
|
-
type: string;
|
|
79
|
-
id: string[];
|
|
80
|
-
kwargs: {
|
|
81
|
-
content: string;
|
|
82
|
-
type: string;
|
|
83
|
-
id: string;
|
|
84
|
-
};
|
|
85
|
-
}[][];
|
|
86
|
-
};
|
|
87
|
-
};
|
|
88
|
-
parent_ids: string[];
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
type LangGraphOnChatModelStreamEvent = {
|
|
92
|
-
event: LangGraphEventTypes.OnChatModelStream;
|
|
93
|
-
name: string;
|
|
94
|
-
run_id: string;
|
|
95
|
-
tags: string[];
|
|
96
|
-
metadata: {
|
|
97
|
-
thread_id: string;
|
|
98
|
-
langgraph_step: number;
|
|
99
|
-
langgraph_node: string;
|
|
100
|
-
langgraph_triggers: string[];
|
|
101
|
-
langgraph_task_idx: number;
|
|
102
|
-
thread_ts: string;
|
|
103
|
-
ls_provider: string;
|
|
104
|
-
ls_model_name: string;
|
|
105
|
-
ls_model_type: string;
|
|
106
|
-
ls_temperature: number;
|
|
107
|
-
};
|
|
108
|
-
data: {
|
|
109
|
-
chunk: {
|
|
110
|
-
lc: number;
|
|
111
|
-
type: string;
|
|
112
|
-
id: string[];
|
|
113
|
-
kwargs: {
|
|
114
|
-
content: string;
|
|
115
|
-
additional_kwargs: {
|
|
116
|
-
tool_calls: {
|
|
117
|
-
index: number;
|
|
118
|
-
id: string;
|
|
119
|
-
function: { arguments: string; name: string };
|
|
120
|
-
type: string;
|
|
121
|
-
}[];
|
|
122
|
-
};
|
|
123
|
-
type: string;
|
|
124
|
-
id: string;
|
|
125
|
-
tool_calls: { name: string; args: {}; id: string; type: string }[];
|
|
126
|
-
tool_call_chunks: {
|
|
127
|
-
name: string;
|
|
128
|
-
args: string;
|
|
129
|
-
id: string;
|
|
130
|
-
index: number;
|
|
131
|
-
type: string;
|
|
132
|
-
}[];
|
|
133
|
-
invalid_tool_calls: any[];
|
|
134
|
-
};
|
|
135
|
-
};
|
|
136
|
-
};
|
|
137
|
-
parent_ids: string[];
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
type LangGraphOnChatModelEndEvent = {
|
|
141
|
-
event: LangGraphEventTypes.OnChatModelEnd;
|
|
142
|
-
name: string;
|
|
143
|
-
run_id: string;
|
|
144
|
-
tags: string[];
|
|
145
|
-
metadata: {
|
|
146
|
-
thread_id: string;
|
|
147
|
-
langgraph_step: number;
|
|
148
|
-
langgraph_node: string;
|
|
149
|
-
langgraph_triggers: string[];
|
|
150
|
-
langgraph_task_idx: number;
|
|
151
|
-
thread_ts: string;
|
|
152
|
-
ls_provider: string;
|
|
153
|
-
ls_model_name: string;
|
|
154
|
-
ls_model_type: string;
|
|
155
|
-
ls_temperature: number;
|
|
156
|
-
};
|
|
157
|
-
data: {
|
|
158
|
-
input: any;
|
|
159
|
-
output: {
|
|
160
|
-
generations: {
|
|
161
|
-
text: string;
|
|
162
|
-
generation_info: {
|
|
163
|
-
finish_reason: string;
|
|
164
|
-
model_name: string;
|
|
165
|
-
system_fingerprint: string;
|
|
166
|
-
};
|
|
167
|
-
type: string;
|
|
168
|
-
message: {
|
|
169
|
-
lc: number;
|
|
170
|
-
type: string;
|
|
171
|
-
id: string[];
|
|
172
|
-
kwargs: {
|
|
173
|
-
content: string;
|
|
174
|
-
additional_kwargs: {
|
|
175
|
-
tool_calls: {
|
|
176
|
-
index: number;
|
|
177
|
-
id: string;
|
|
178
|
-
function: { arguments: string; name: string };
|
|
179
|
-
type: string;
|
|
180
|
-
}[];
|
|
181
|
-
};
|
|
182
|
-
response_metadata: {
|
|
183
|
-
finish_reason: string;
|
|
184
|
-
model_name: string;
|
|
185
|
-
system_fingerprint: string;
|
|
186
|
-
};
|
|
187
|
-
type: string;
|
|
188
|
-
id: string;
|
|
189
|
-
tool_calls: { name: string; args: { query: string }; id: string; type: string }[];
|
|
190
|
-
invalid_tool_calls: any[];
|
|
191
|
-
};
|
|
192
|
-
};
|
|
193
|
-
}[][];
|
|
194
|
-
llm_output: any;
|
|
195
|
-
run: any;
|
|
196
|
-
};
|
|
197
|
-
};
|
|
198
|
-
parent_ids: string[];
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
type LangGraphOnChainStreamEvent = {
|
|
202
|
-
event: LangGraphEventTypes.OnChainStream;
|
|
203
|
-
name: string;
|
|
204
|
-
run_id: string;
|
|
205
|
-
tags: string[];
|
|
206
|
-
metadata: {
|
|
207
|
-
thread_id: string;
|
|
208
|
-
langgraph_step?: number;
|
|
209
|
-
langgraph_node?: string;
|
|
210
|
-
langgraph_triggers?: string[];
|
|
211
|
-
langgraph_task_idx?: number;
|
|
212
|
-
thread_ts?: string;
|
|
213
|
-
};
|
|
214
|
-
data: {
|
|
215
|
-
chunk: {
|
|
216
|
-
messages: {
|
|
217
|
-
lc: number;
|
|
218
|
-
type: string;
|
|
219
|
-
id: string[];
|
|
220
|
-
kwargs: {
|
|
221
|
-
content: string;
|
|
222
|
-
additional_kwargs?: {
|
|
223
|
-
tool_calls?: {
|
|
224
|
-
index: number;
|
|
225
|
-
id: string;
|
|
226
|
-
function: { arguments: string; name: string };
|
|
227
|
-
type: string;
|
|
228
|
-
}[];
|
|
229
|
-
};
|
|
230
|
-
response_metadata?: {
|
|
231
|
-
finish_reason: string;
|
|
232
|
-
model_name: string;
|
|
233
|
-
system_fingerprint: string;
|
|
234
|
-
};
|
|
235
|
-
type: string;
|
|
236
|
-
id: string;
|
|
237
|
-
tool_calls?: { name: string; args: { query: string }; id: string; type: string }[];
|
|
238
|
-
invalid_tool_calls?: any[];
|
|
239
|
-
};
|
|
240
|
-
}[];
|
|
241
|
-
};
|
|
242
|
-
};
|
|
243
|
-
parent_ids: string[];
|
|
244
|
-
};
|
|
245
|
-
|
|
246
|
-
type LangGraphOnToolStartEvent = {
|
|
247
|
-
event: LangGraphEventTypes.OnToolStart;
|
|
248
|
-
name: string;
|
|
249
|
-
run_id: string;
|
|
250
|
-
tags: string[];
|
|
251
|
-
metadata: {
|
|
252
|
-
thread_id: string;
|
|
253
|
-
langgraph_step: number;
|
|
254
|
-
langgraph_node: string;
|
|
255
|
-
langgraph_triggers: string[];
|
|
256
|
-
langgraph_task_idx: number;
|
|
257
|
-
thread_ts: string;
|
|
258
|
-
};
|
|
259
|
-
data: {
|
|
260
|
-
input: {
|
|
261
|
-
query: string;
|
|
262
|
-
};
|
|
263
|
-
};
|
|
264
|
-
parent_ids: string[];
|
|
265
|
-
};
|
|
266
|
-
|
|
267
|
-
type LangGraphOnToolEndEvent = {
|
|
268
|
-
event: LangGraphEventTypes.OnToolEnd;
|
|
269
|
-
name: string;
|
|
270
|
-
run_id: string;
|
|
271
|
-
tags: string[];
|
|
272
|
-
metadata: {
|
|
273
|
-
thread_id: string;
|
|
274
|
-
langgraph_step: number;
|
|
275
|
-
langgraph_node: string;
|
|
276
|
-
langgraph_triggers: string[];
|
|
277
|
-
langgraph_task_idx: number;
|
|
278
|
-
thread_ts: string;
|
|
279
|
-
};
|
|
280
|
-
data: {
|
|
281
|
-
input: {
|
|
282
|
-
query: string;
|
|
283
|
-
};
|
|
284
|
-
output: {
|
|
285
|
-
lc: number;
|
|
286
|
-
type: string;
|
|
287
|
-
id: string[];
|
|
288
|
-
kwargs: {
|
|
289
|
-
content: string[];
|
|
290
|
-
type: string;
|
|
291
|
-
name: string;
|
|
292
|
-
tool_call_id: string;
|
|
293
|
-
status: string;
|
|
294
|
-
};
|
|
295
|
-
};
|
|
296
|
-
};
|
|
297
|
-
parent_ids: string[];
|
|
298
|
-
};
|
|
299
|
-
|
|
300
|
-
export type LangGraphEvent =
|
|
301
|
-
| LangGraphOnChainStartEvent
|
|
302
|
-
| LangGraphOnChainStreamEvent
|
|
303
|
-
| LangGraphOnChainEndEvent
|
|
304
|
-
| LangGraphOnChatModelStartEvent
|
|
305
|
-
| LangGraphOnChatModelStreamEvent
|
|
306
|
-
| LangGraphOnChatModelEndEvent
|
|
307
|
-
| LangGraphOnToolStartEvent
|
|
308
|
-
| LangGraphOnToolEndEvent
|
|
309
|
-
| LangGraphOnCopilotKitStateSyncEvent;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Field, InputType } from "type-graphql";
|
|
2
|
-
|
|
3
|
-
@InputType()
|
|
4
|
-
export class AgentSessionInput {
|
|
5
|
-
@Field(() => String)
|
|
6
|
-
agentName: string;
|
|
7
|
-
|
|
8
|
-
@Field(() => String, { nullable: true })
|
|
9
|
-
threadId?: string;
|
|
10
|
-
|
|
11
|
-
@Field(() => String, { nullable: true })
|
|
12
|
-
nodeName?: string;
|
|
13
|
-
}
|
|
@@ -1,360 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* <Callout type="info">
|
|
3
|
-
* This is the reference for the `CopilotRuntime` class. For more information and example code snippets, please see [Concept: Copilot Runtime](/concepts/copilot-runtime).
|
|
4
|
-
* </Callout>
|
|
5
|
-
*
|
|
6
|
-
* ## Usage
|
|
7
|
-
*
|
|
8
|
-
* ```tsx
|
|
9
|
-
* import { CopilotRuntime } from "@copilotkit/runtime";
|
|
10
|
-
*
|
|
11
|
-
* const copilotKit = new CopilotRuntime();
|
|
12
|
-
* ```
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import { Action, actionParametersToJsonSchema, Parameter, randomId } from "@copilotkit/shared";
|
|
16
|
-
import { RemoteChain, RemoteChainParameters, CopilotServiceAdapter } from "../../service-adapters";
|
|
17
|
-
import { MessageInput } from "../../graphql/inputs/message.input";
|
|
18
|
-
import { ActionInput } from "../../graphql/inputs/action.input";
|
|
19
|
-
import { RuntimeEventSource } from "../../service-adapters/events";
|
|
20
|
-
import { convertGqlInputToMessages } from "../../service-adapters/conversion";
|
|
21
|
-
import { AgentStateMessage, Message } from "../../graphql/types/converted";
|
|
22
|
-
import { ForwardedParametersInput } from "../../graphql/inputs/forwarded-parameters.input";
|
|
23
|
-
import {
|
|
24
|
-
setupRemoteActions,
|
|
25
|
-
RemoteActionDefinition,
|
|
26
|
-
LangGraphAgentAction,
|
|
27
|
-
isLangGraphAgentAction,
|
|
28
|
-
} from "./remote-actions";
|
|
29
|
-
import { GraphQLContext } from "../integrations/shared";
|
|
30
|
-
import { AgentSessionInput } from "../../graphql/inputs/agent-session.input";
|
|
31
|
-
import { from } from "rxjs";
|
|
32
|
-
import { AgentStateInput } from "../../graphql/inputs/agent-state.input";
|
|
33
|
-
|
|
34
|
-
interface CopilotRuntimeRequest {
|
|
35
|
-
serviceAdapter: CopilotServiceAdapter;
|
|
36
|
-
messages: MessageInput[];
|
|
37
|
-
actions: ActionInput[];
|
|
38
|
-
agentSession?: AgentSessionInput;
|
|
39
|
-
agentStates?: AgentStateInput[];
|
|
40
|
-
outputMessagesPromise: Promise<Message[]>;
|
|
41
|
-
threadId?: string;
|
|
42
|
-
runId?: string;
|
|
43
|
-
publicApiKey?: string;
|
|
44
|
-
graphqlContext: GraphQLContext;
|
|
45
|
-
forwardedParameters?: ForwardedParametersInput;
|
|
46
|
-
url?: string;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
interface CopilotRuntimeResponse {
|
|
50
|
-
threadId: string;
|
|
51
|
-
runId?: string;
|
|
52
|
-
eventSource: RuntimeEventSource;
|
|
53
|
-
serverSideActions: Action<any>[];
|
|
54
|
-
actionInputsWithoutAgents: ActionInput[];
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
type ActionsConfiguration<T extends Parameter[] | [] = []> =
|
|
58
|
-
| Action<T>[]
|
|
59
|
-
| ((ctx: { properties: any; url?: string }) => Action<T>[]);
|
|
60
|
-
|
|
61
|
-
interface OnBeforeRequestOptions {
|
|
62
|
-
threadId?: string;
|
|
63
|
-
runId?: string;
|
|
64
|
-
inputMessages: Message[];
|
|
65
|
-
properties: any;
|
|
66
|
-
url?: string;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
type OnBeforeRequestHandler = (options: OnBeforeRequestOptions) => void | Promise<void>;
|
|
70
|
-
|
|
71
|
-
interface OnAfterRequestOptions {
|
|
72
|
-
threadId: string;
|
|
73
|
-
runId?: string;
|
|
74
|
-
inputMessages: Message[];
|
|
75
|
-
outputMessages: Message[];
|
|
76
|
-
properties: any;
|
|
77
|
-
url?: string;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
type OnAfterRequestHandler = (options: OnAfterRequestOptions) => void | Promise<void>;
|
|
81
|
-
|
|
82
|
-
interface Middleware {
|
|
83
|
-
/**
|
|
84
|
-
* A function that is called before the request is processed.
|
|
85
|
-
*/
|
|
86
|
-
onBeforeRequest?: OnBeforeRequestHandler;
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* A function that is called after the request is processed.
|
|
90
|
-
*/
|
|
91
|
-
onAfterRequest?: OnAfterRequestHandler;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export interface CopilotRuntimeConstructorParams<T extends Parameter[] | [] = []> {
|
|
95
|
-
/**
|
|
96
|
-
* Middleware to be used by the runtime.
|
|
97
|
-
*
|
|
98
|
-
* ```ts
|
|
99
|
-
* onBeforeRequest: (options: {
|
|
100
|
-
* threadId?: string;
|
|
101
|
-
* runId?: string;
|
|
102
|
-
* inputMessages: Message[];
|
|
103
|
-
* properties: any;
|
|
104
|
-
* }) => void | Promise<void>;
|
|
105
|
-
* ```
|
|
106
|
-
*
|
|
107
|
-
* ```ts
|
|
108
|
-
* onAfterRequest: (options: {
|
|
109
|
-
* threadId?: string;
|
|
110
|
-
* runId?: string;
|
|
111
|
-
* inputMessages: Message[];
|
|
112
|
-
* outputMessages: Message[];
|
|
113
|
-
* properties: any;
|
|
114
|
-
* }) => void | Promise<void>;
|
|
115
|
-
* ```
|
|
116
|
-
*/
|
|
117
|
-
middleware?: Middleware;
|
|
118
|
-
|
|
119
|
-
/*
|
|
120
|
-
* A list of server side actions that can be executed.
|
|
121
|
-
*/
|
|
122
|
-
actions?: ActionsConfiguration<T>;
|
|
123
|
-
|
|
124
|
-
/*
|
|
125
|
-
* A list of remote actions that can be executed.
|
|
126
|
-
*/
|
|
127
|
-
remoteActions?: RemoteActionDefinition[];
|
|
128
|
-
|
|
129
|
-
/*
|
|
130
|
-
* An array of LangServer URLs.
|
|
131
|
-
*/
|
|
132
|
-
langserve?: RemoteChainParameters[];
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
export class CopilotRuntime<const T extends Parameter[] | [] = []> {
|
|
136
|
-
public actions: ActionsConfiguration<T>;
|
|
137
|
-
private remoteActionDefinitions: RemoteActionDefinition[];
|
|
138
|
-
private langserve: Promise<Action<any>>[] = [];
|
|
139
|
-
private onBeforeRequest?: OnBeforeRequestHandler;
|
|
140
|
-
private onAfterRequest?: OnAfterRequestHandler;
|
|
141
|
-
|
|
142
|
-
constructor(params?: CopilotRuntimeConstructorParams<T>) {
|
|
143
|
-
this.actions = params?.actions || [];
|
|
144
|
-
|
|
145
|
-
for (const chain of params?.langserve || []) {
|
|
146
|
-
const remoteChain = new RemoteChain(chain);
|
|
147
|
-
this.langserve.push(remoteChain.toAction());
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
this.remoteActionDefinitions = params?.remoteActions || [];
|
|
151
|
-
|
|
152
|
-
this.onBeforeRequest = params?.middleware?.onBeforeRequest;
|
|
153
|
-
this.onAfterRequest = params?.middleware?.onAfterRequest;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
async processRuntimeRequest(request: CopilotRuntimeRequest): Promise<CopilotRuntimeResponse> {
|
|
157
|
-
const {
|
|
158
|
-
serviceAdapter,
|
|
159
|
-
messages: rawMessages,
|
|
160
|
-
actions: clientSideActionsInput,
|
|
161
|
-
threadId,
|
|
162
|
-
runId,
|
|
163
|
-
outputMessagesPromise,
|
|
164
|
-
graphqlContext,
|
|
165
|
-
forwardedParameters,
|
|
166
|
-
agentSession,
|
|
167
|
-
url,
|
|
168
|
-
} = request;
|
|
169
|
-
|
|
170
|
-
if (agentSession) {
|
|
171
|
-
return this.processAgentRequest(request);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const messages = rawMessages.filter((message) => !message.agentStateMessage);
|
|
175
|
-
|
|
176
|
-
const inputMessages = convertGqlInputToMessages(messages);
|
|
177
|
-
const serverSideActions = await this.getServerSideActions(request);
|
|
178
|
-
|
|
179
|
-
const serverSideActionsInput: ActionInput[] = serverSideActions.map((action) => ({
|
|
180
|
-
name: action.name,
|
|
181
|
-
description: action.description,
|
|
182
|
-
jsonSchema: JSON.stringify(actionParametersToJsonSchema(action.parameters)),
|
|
183
|
-
}));
|
|
184
|
-
|
|
185
|
-
const actionInputs = flattenToolCallsNoDuplicates([
|
|
186
|
-
...serverSideActionsInput,
|
|
187
|
-
...clientSideActionsInput,
|
|
188
|
-
]);
|
|
189
|
-
|
|
190
|
-
await this.onBeforeRequest?.({
|
|
191
|
-
threadId,
|
|
192
|
-
runId,
|
|
193
|
-
inputMessages,
|
|
194
|
-
properties: graphqlContext.properties,
|
|
195
|
-
url,
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
try {
|
|
199
|
-
const eventSource = new RuntimeEventSource();
|
|
200
|
-
|
|
201
|
-
const result = await serviceAdapter.process({
|
|
202
|
-
messages: inputMessages,
|
|
203
|
-
actions: actionInputs,
|
|
204
|
-
threadId,
|
|
205
|
-
runId,
|
|
206
|
-
eventSource,
|
|
207
|
-
forwardedParameters,
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
outputMessagesPromise
|
|
211
|
-
.then((outputMessages) => {
|
|
212
|
-
this.onAfterRequest?.({
|
|
213
|
-
threadId: result.threadId,
|
|
214
|
-
runId: result.runId,
|
|
215
|
-
inputMessages,
|
|
216
|
-
outputMessages,
|
|
217
|
-
properties: graphqlContext.properties,
|
|
218
|
-
url,
|
|
219
|
-
});
|
|
220
|
-
})
|
|
221
|
-
.catch((_error) => {});
|
|
222
|
-
|
|
223
|
-
return {
|
|
224
|
-
threadId: result.threadId,
|
|
225
|
-
runId: result.runId,
|
|
226
|
-
eventSource,
|
|
227
|
-
serverSideActions,
|
|
228
|
-
actionInputsWithoutAgents: actionInputs.filter(
|
|
229
|
-
(action) =>
|
|
230
|
-
// TODO-AGENTS: do not exclude ALL server side actions
|
|
231
|
-
!serverSideActions.find((serverSideAction) => serverSideAction.name == action.name),
|
|
232
|
-
// !isLangGraphAgentAction(
|
|
233
|
-
// serverSideActions.find((serverSideAction) => serverSideAction.name == action.name),
|
|
234
|
-
// ),
|
|
235
|
-
),
|
|
236
|
-
};
|
|
237
|
-
} catch (error) {
|
|
238
|
-
console.error("Error getting response:", error);
|
|
239
|
-
throw error;
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
private async processAgentRequest(
|
|
244
|
-
request: CopilotRuntimeRequest,
|
|
245
|
-
): Promise<CopilotRuntimeResponse> {
|
|
246
|
-
const { messages: rawMessages, outputMessagesPromise, graphqlContext, agentSession } = request;
|
|
247
|
-
const { threadId = randomId(), agentName, nodeName } = agentSession;
|
|
248
|
-
const serverSideActions = await this.getServerSideActions(request);
|
|
249
|
-
|
|
250
|
-
const messages = convertGqlInputToMessages(rawMessages);
|
|
251
|
-
|
|
252
|
-
const agent = serverSideActions.find(
|
|
253
|
-
(action) => action.name === agentName && isLangGraphAgentAction(action),
|
|
254
|
-
) as LangGraphAgentAction;
|
|
255
|
-
|
|
256
|
-
if (!agent) {
|
|
257
|
-
throw new Error(`Agent ${agentName} not found`);
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
const serverSideActionsInput: ActionInput[] = serverSideActions
|
|
261
|
-
.filter((action) => !isLangGraphAgentAction(action))
|
|
262
|
-
.map((action) => ({
|
|
263
|
-
name: action.name,
|
|
264
|
-
description: action.description,
|
|
265
|
-
jsonSchema: JSON.stringify(actionParametersToJsonSchema(action.parameters)),
|
|
266
|
-
}));
|
|
267
|
-
|
|
268
|
-
const actionInputsWithoutAgents = flattenToolCallsNoDuplicates([
|
|
269
|
-
...serverSideActionsInput,
|
|
270
|
-
...request.actions,
|
|
271
|
-
]);
|
|
272
|
-
|
|
273
|
-
await this.onBeforeRequest?.({
|
|
274
|
-
threadId,
|
|
275
|
-
runId: undefined,
|
|
276
|
-
inputMessages: messages,
|
|
277
|
-
properties: graphqlContext.properties,
|
|
278
|
-
});
|
|
279
|
-
try {
|
|
280
|
-
const eventSource = new RuntimeEventSource();
|
|
281
|
-
const stream = await agent.langGraphAgentHandler({
|
|
282
|
-
name: agentName,
|
|
283
|
-
threadId,
|
|
284
|
-
nodeName,
|
|
285
|
-
actionInputsWithoutAgents,
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
eventSource.stream(async (eventStream$) => {
|
|
289
|
-
from(stream).subscribe({
|
|
290
|
-
next: (event) => eventStream$.next(event),
|
|
291
|
-
error: (err) => console.error("Error in stream", err),
|
|
292
|
-
complete: () => eventStream$.complete(),
|
|
293
|
-
});
|
|
294
|
-
});
|
|
295
|
-
|
|
296
|
-
outputMessagesPromise
|
|
297
|
-
.then((outputMessages) => {
|
|
298
|
-
this.onAfterRequest?.({
|
|
299
|
-
threadId,
|
|
300
|
-
runId: undefined,
|
|
301
|
-
inputMessages: messages,
|
|
302
|
-
outputMessages,
|
|
303
|
-
properties: graphqlContext.properties,
|
|
304
|
-
});
|
|
305
|
-
})
|
|
306
|
-
.catch((_error) => {});
|
|
307
|
-
|
|
308
|
-
return {
|
|
309
|
-
threadId,
|
|
310
|
-
runId: undefined,
|
|
311
|
-
eventSource,
|
|
312
|
-
serverSideActions: [],
|
|
313
|
-
actionInputsWithoutAgents,
|
|
314
|
-
};
|
|
315
|
-
} catch (error) {
|
|
316
|
-
console.error("Error getting response:", error);
|
|
317
|
-
throw error;
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
private async getServerSideActions(request: CopilotRuntimeRequest): Promise<Action<any>[]> {
|
|
322
|
-
const { messages: rawMessages, graphqlContext, agentStates, url } = request;
|
|
323
|
-
const inputMessages = convertGqlInputToMessages(rawMessages);
|
|
324
|
-
const langserveFunctions: Action<any>[] = [];
|
|
325
|
-
|
|
326
|
-
for (const chainPromise of this.langserve) {
|
|
327
|
-
try {
|
|
328
|
-
const chain = await chainPromise;
|
|
329
|
-
langserveFunctions.push(chain);
|
|
330
|
-
} catch (error) {
|
|
331
|
-
console.error("Error loading langserve chain:", error);
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
const remoteActions = await setupRemoteActions({
|
|
335
|
-
remoteActionDefinitions: this.remoteActionDefinitions,
|
|
336
|
-
graphqlContext,
|
|
337
|
-
messages: inputMessages,
|
|
338
|
-
agentStates,
|
|
339
|
-
});
|
|
340
|
-
|
|
341
|
-
const configuredActions =
|
|
342
|
-
typeof this.actions === "function"
|
|
343
|
-
? this.actions({ properties: graphqlContext.properties, url })
|
|
344
|
-
: this.actions;
|
|
345
|
-
|
|
346
|
-
return [...configuredActions, ...langserveFunctions, ...remoteActions];
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
export function flattenToolCallsNoDuplicates(toolsByPriority: ActionInput[]): ActionInput[] {
|
|
351
|
-
let allTools: ActionInput[] = [];
|
|
352
|
-
const allToolNames: string[] = [];
|
|
353
|
-
for (const tool of toolsByPriority) {
|
|
354
|
-
if (!allToolNames.includes(tool.name)) {
|
|
355
|
-
allTools.push(tool);
|
|
356
|
-
allToolNames.push(tool.name);
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
return allTools;
|
|
360
|
-
}
|