@copilotkit/runtime 1.3.12-fix-tool-call-dynamic-parameters.0 → 1.3.12-lgc-alpha-1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -4
- package/dist/{chunk-SIMJ6TZU.mjs → chunk-6B3NPPSR.mjs} +2 -2
- package/dist/{chunk-OSJXQNII.mjs → chunk-6HXQC7IT.mjs} +36 -9
- package/dist/chunk-6HXQC7IT.mjs.map +1 -0
- package/dist/{chunk-24WEOOFX.mjs → chunk-7MQDBRXJ.mjs} +2 -2
- package/dist/{chunk-AOYYOKTP.mjs → chunk-E6ZFCM3B.mjs} +561 -133
- package/dist/chunk-E6ZFCM3B.mjs.map +1 -0
- package/dist/{chunk-UYORVPCQ.mjs → chunk-TM7ZRU3M.mjs} +2 -2
- package/dist/{chunk-ZEHCLFJ2.mjs → chunk-V7SK6QZN.mjs} +6 -9
- package/dist/chunk-V7SK6QZN.mjs.map +1 -0
- package/dist/{chunk-HKLL7TTP.mjs → chunk-XMDH5MKI.mjs} +2 -2
- package/dist/{copilot-runtime-df3527ad.d.ts → copilot-runtime-aba7d4b4.d.ts} +27 -5
- package/dist/index.d.ts +1 -1
- package/dist/index.js +638 -186
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +13 -9
- package/dist/index.mjs.map +1 -1
- package/dist/lib/index.d.ts +1 -1
- package/dist/lib/index.js +638 -186
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +13 -9
- package/dist/lib/integrations/index.d.ts +2 -2
- package/dist/lib/integrations/index.js +5 -4
- 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 +1 -1
- package/dist/lib/integrations/nest/index.js +5 -4
- 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 +1 -1
- package/dist/lib/integrations/node-express/index.js +5 -4
- 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 +1 -1
- package/dist/lib/integrations/node-http/index.js +5 -4
- 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.js +5 -8
- package/dist/service-adapters/index.js.map +1 -1
- package/dist/service-adapters/index.mjs +2 -2
- package/package.json +7 -6
- package/src/agents/langgraph/event-source.ts +22 -67
- package/src/lib/runtime/copilot-runtime.ts +58 -11
- package/src/lib/runtime/remote-action-constructors.ts +283 -0
- package/src/lib/runtime/remote-actions.ts +65 -159
- package/src/lib/runtime/remote-lg-cloud-action.ts +441 -0
- package/src/service-adapters/events.ts +1 -5
- package/src/service-adapters/langchain/utils.ts +5 -10
- package/dist/chunk-AOYYOKTP.mjs.map +0 -1
- package/dist/chunk-OSJXQNII.mjs.map +0 -1
- package/dist/chunk-ZEHCLFJ2.mjs.map +0 -1
- package/src/service-adapters/langchain/utils.test.ts +0 -169
- /package/dist/{chunk-SIMJ6TZU.mjs.map → chunk-6B3NPPSR.mjs.map} +0 -0
- /package/dist/{chunk-24WEOOFX.mjs.map → chunk-7MQDBRXJ.mjs.map} +0 -0
- /package/dist/{chunk-UYORVPCQ.mjs.map → chunk-TM7ZRU3M.mjs.map} +0 -0
- /package/dist/{chunk-HKLL7TTP.mjs.map → chunk-XMDH5MKI.mjs.map} +0 -0
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
import { Client } from "@langchain/langgraph-sdk";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { parse as parsePartialJson } from "partial-json";
|
|
4
|
+
import { ActionInput } from "../../graphql/inputs/action.input";
|
|
5
|
+
import { LangGraphCloudAgent, LangGraphCloudEndpoint } from "./remote-actions";
|
|
6
|
+
import { CopilotRequestContextProperties } from "../integrations";
|
|
7
|
+
import { BaseMessageInput as CopilotKitBaseMessage } from "../../graphql/types/base";
|
|
8
|
+
import { MessageRole } from "../../graphql/types/enums";
|
|
9
|
+
|
|
10
|
+
type State = Record<string, any>;
|
|
11
|
+
|
|
12
|
+
type ExecutionAction = Pick<ActionInput, "name" | "description"> & { parameters: string };
|
|
13
|
+
|
|
14
|
+
interface ExecutionArgs extends Omit<LangGraphCloudEndpoint, "agents"> {
|
|
15
|
+
agent: LangGraphCloudAgent;
|
|
16
|
+
threadId: string;
|
|
17
|
+
nodeName: string;
|
|
18
|
+
messages: CopilotKitBaseMessage[];
|
|
19
|
+
state: State;
|
|
20
|
+
properties: CopilotRequestContextProperties;
|
|
21
|
+
actions: ExecutionAction[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function execute(args: ExecutionArgs): Promise<ReadableStream<Uint8Array>> {
|
|
25
|
+
return new ReadableStream({
|
|
26
|
+
async start(controller) {
|
|
27
|
+
try {
|
|
28
|
+
await streamEvents(controller, args);
|
|
29
|
+
controller.close();
|
|
30
|
+
} catch (err) {}
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function streamEvents(controller: ReadableStreamDefaultController, args: ExecutionArgs) {
|
|
36
|
+
const {
|
|
37
|
+
deploymentUrl,
|
|
38
|
+
langsmithApiKey,
|
|
39
|
+
threadId: agrsInitialThreadId,
|
|
40
|
+
agent,
|
|
41
|
+
nodeName: initialNodeName,
|
|
42
|
+
state: initialState,
|
|
43
|
+
messages,
|
|
44
|
+
actions,
|
|
45
|
+
} = args;
|
|
46
|
+
|
|
47
|
+
let nodeName = initialNodeName;
|
|
48
|
+
let state = initialState;
|
|
49
|
+
const { name, assistantId: initialAssistantId } = agent;
|
|
50
|
+
|
|
51
|
+
// TODO: deploymentUrl is not required in local development
|
|
52
|
+
const client = new Client({ apiUrl: deploymentUrl, apiKey: langsmithApiKey });
|
|
53
|
+
let initialThreadId = agrsInitialThreadId;
|
|
54
|
+
const wasInitiatedWithExistingThread = !!initialThreadId;
|
|
55
|
+
if (initialThreadId && initialThreadId.startsWith("ck-")) {
|
|
56
|
+
initialThreadId = initialThreadId.substring(3);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const assistants = await client.assistants.search();
|
|
60
|
+
const retrievedAssistant = assistants.find((a) => a.name === name);
|
|
61
|
+
const threadId = initialThreadId ?? randomUUID();
|
|
62
|
+
if (initialThreadId === threadId) {
|
|
63
|
+
await client.threads.get(threadId);
|
|
64
|
+
} else {
|
|
65
|
+
await client.threads.create({ threadId: threadId });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let agentState = { values: {} };
|
|
69
|
+
if (wasInitiatedWithExistingThread) {
|
|
70
|
+
agentState = await client.threads.getState(threadId);
|
|
71
|
+
}
|
|
72
|
+
const agentStateValues = agentState.values as State;
|
|
73
|
+
state.messages = agentStateValues.messages;
|
|
74
|
+
const mode = wasInitiatedWithExistingThread && nodeName != "__end__" ? "continue" : "start";
|
|
75
|
+
state = langGraphDefaultMergeState(state, formatMessages(messages), actions);
|
|
76
|
+
|
|
77
|
+
if (mode === "continue") {
|
|
78
|
+
await client.threads.updateState(threadId, { values: state, asNode: nodeName });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const assistantId = initialAssistantId ?? retrievedAssistant.assistant_id;
|
|
82
|
+
const graphInfo = await client.assistants.getGraph(assistantId);
|
|
83
|
+
const streamInput = mode === "start" ? state : null;
|
|
84
|
+
|
|
85
|
+
let streamingStateExtractor = new StreamingStateExtractor([]);
|
|
86
|
+
let prevNodeName = null;
|
|
87
|
+
let emitIntermediateStateUntilEnd = null;
|
|
88
|
+
let shouldExit = null;
|
|
89
|
+
let externalRunId = null;
|
|
90
|
+
|
|
91
|
+
const streamResponse = client.runs.stream(threadId, assistantId, {
|
|
92
|
+
input: streamInput,
|
|
93
|
+
streamMode: ["events", "values"],
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const emit = (message: string) => controller.enqueue(new TextEncoder().encode(message));
|
|
97
|
+
|
|
98
|
+
let latestStateValues = {};
|
|
99
|
+
|
|
100
|
+
for await (const chunk of streamResponse) {
|
|
101
|
+
if (!["events", "values"].includes(chunk.event)) continue;
|
|
102
|
+
|
|
103
|
+
if (chunk.event === "values") {
|
|
104
|
+
latestStateValues = chunk.data;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const event = chunk.data;
|
|
109
|
+
const currentNodeName = event.name;
|
|
110
|
+
const eventType = event.event;
|
|
111
|
+
const runId = event.metadata.run_id;
|
|
112
|
+
externalRunId = runId;
|
|
113
|
+
const metadata = event.metadata;
|
|
114
|
+
|
|
115
|
+
shouldExit = shouldExit != null ? shouldExit : metadata["copilotkit:exit"];
|
|
116
|
+
const emitIntermediateState = metadata["copilotkit:emit-intermediate-state"];
|
|
117
|
+
const forceEmitIntermediateState = metadata["copilotkit:force-emit-intermediate-state"];
|
|
118
|
+
const manuallyEmitMessage = metadata["copilotkit:manually-emit-messages"];
|
|
119
|
+
const manuallyEmitToolCall = metadata["copilotkit:manually-emit-tool-calls"];
|
|
120
|
+
// we only want to update the node name under certain conditions
|
|
121
|
+
// since we don't need any internal node names to be sent to the frontend
|
|
122
|
+
if (graphInfo["nodes"].some((node) => node.id === currentNodeName)) {
|
|
123
|
+
nodeName = currentNodeName;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (!nodeName) {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (forceEmitIntermediateState) {
|
|
131
|
+
if (eventType === "on_chain_end") {
|
|
132
|
+
state = event.data.output;
|
|
133
|
+
emit(
|
|
134
|
+
getStateSyncEvent({
|
|
135
|
+
threadId,
|
|
136
|
+
runId,
|
|
137
|
+
agentName: agent.name,
|
|
138
|
+
nodeName,
|
|
139
|
+
state: event.data.output,
|
|
140
|
+
running: true,
|
|
141
|
+
active: true,
|
|
142
|
+
}),
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (manuallyEmitMessage) {
|
|
149
|
+
if (eventType === "on_chain_end") {
|
|
150
|
+
state = event.data.output;
|
|
151
|
+
emit(
|
|
152
|
+
JSON.stringify({
|
|
153
|
+
event: "on_copilotkit_emit_message",
|
|
154
|
+
message: event.data.output,
|
|
155
|
+
messageId: randomUUID(),
|
|
156
|
+
role: MessageRole.assistant,
|
|
157
|
+
}) + "\n",
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (manuallyEmitToolCall) {
|
|
164
|
+
if (eventType === "on_chain_end") {
|
|
165
|
+
state = event.data.output;
|
|
166
|
+
emit(
|
|
167
|
+
JSON.stringify({
|
|
168
|
+
event: "on_copilotkit_emit_tool_call",
|
|
169
|
+
name: event.data.output.name,
|
|
170
|
+
args: event.data.output.args,
|
|
171
|
+
id: event.data.output.id,
|
|
172
|
+
}) + "\n",
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (emitIntermediateState && emitIntermediateStateUntilEnd == null) {
|
|
179
|
+
emitIntermediateStateUntilEnd = nodeName;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (emitIntermediateState && eventType === "on_chat_model_start") {
|
|
183
|
+
// reset the streaming state extractor
|
|
184
|
+
streamingStateExtractor = new StreamingStateExtractor(emitIntermediateState);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
let updatedState = latestStateValues;
|
|
188
|
+
|
|
189
|
+
if (emitIntermediateState && eventType === "on_chat_model_stream") {
|
|
190
|
+
streamingStateExtractor.bufferToolCalls(event);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (emitIntermediateStateUntilEnd !== null) {
|
|
194
|
+
updatedState = {
|
|
195
|
+
...updatedState,
|
|
196
|
+
...streamingStateExtractor.extractState(),
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (
|
|
201
|
+
!emitIntermediateState &&
|
|
202
|
+
currentNodeName === emitIntermediateStateUntilEnd &&
|
|
203
|
+
eventType === "on_chain_end"
|
|
204
|
+
) {
|
|
205
|
+
// stop emitting function call state
|
|
206
|
+
emitIntermediateStateUntilEnd = null;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const exitingNode = nodeName === currentNodeName && eventType === "on_chain_end";
|
|
210
|
+
|
|
211
|
+
if (
|
|
212
|
+
JSON.stringify(updatedState) !== JSON.stringify(state) ||
|
|
213
|
+
prevNodeName != nodeName ||
|
|
214
|
+
exitingNode
|
|
215
|
+
) {
|
|
216
|
+
state = updatedState;
|
|
217
|
+
prevNodeName = nodeName;
|
|
218
|
+
emit(
|
|
219
|
+
getStateSyncEvent({
|
|
220
|
+
threadId,
|
|
221
|
+
runId,
|
|
222
|
+
agentName: agent.name,
|
|
223
|
+
nodeName,
|
|
224
|
+
state,
|
|
225
|
+
running: true,
|
|
226
|
+
active: !exitingNode,
|
|
227
|
+
}),
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
emit(JSON.stringify(event) + "\n");
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
state = await client.threads.getState(threadId);
|
|
235
|
+
const isEndNode = state.next.length === 0;
|
|
236
|
+
nodeName = Object.keys(state.metadata.writes)[0];
|
|
237
|
+
|
|
238
|
+
emit(
|
|
239
|
+
getStateSyncEvent({
|
|
240
|
+
threadId,
|
|
241
|
+
runId: externalRunId,
|
|
242
|
+
agentName: agent.name,
|
|
243
|
+
nodeName: isEndNode ? "__end__" : nodeName,
|
|
244
|
+
state: state.values,
|
|
245
|
+
running: !shouldExit,
|
|
246
|
+
active: false,
|
|
247
|
+
}),
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
return Promise.resolve();
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function getStateSyncEvent({
|
|
254
|
+
threadId,
|
|
255
|
+
runId,
|
|
256
|
+
agentName,
|
|
257
|
+
nodeName,
|
|
258
|
+
state,
|
|
259
|
+
running,
|
|
260
|
+
active,
|
|
261
|
+
}: {
|
|
262
|
+
threadId: string;
|
|
263
|
+
runId: string;
|
|
264
|
+
agentName: string;
|
|
265
|
+
nodeName: string;
|
|
266
|
+
state: State;
|
|
267
|
+
running: boolean;
|
|
268
|
+
active: boolean;
|
|
269
|
+
}): string {
|
|
270
|
+
const stateWithoutMessages = Object.keys(state).reduce((acc, key) => {
|
|
271
|
+
if (key !== "messages") {
|
|
272
|
+
acc[key] = state[key];
|
|
273
|
+
}
|
|
274
|
+
return acc;
|
|
275
|
+
}, {} as State);
|
|
276
|
+
|
|
277
|
+
return (
|
|
278
|
+
JSON.stringify({
|
|
279
|
+
event: "on_copilotkit_state_sync",
|
|
280
|
+
thread_id: threadId,
|
|
281
|
+
run_id: runId,
|
|
282
|
+
agent_name: agentName,
|
|
283
|
+
node_name: nodeName,
|
|
284
|
+
active: active,
|
|
285
|
+
state: stateWithoutMessages,
|
|
286
|
+
running: running,
|
|
287
|
+
role: "assistant",
|
|
288
|
+
}) + "\n"
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
class StreamingStateExtractor {
|
|
293
|
+
private emitIntermediateState: { [key: string]: any }[];
|
|
294
|
+
private toolCallBuffer: { [key: string]: string };
|
|
295
|
+
private currentToolCall: string | null;
|
|
296
|
+
private previouslyParsableState: { [key: string]: any };
|
|
297
|
+
|
|
298
|
+
constructor(emitIntermediateState: { [key: string]: any }[]) {
|
|
299
|
+
this.emitIntermediateState = emitIntermediateState;
|
|
300
|
+
this.toolCallBuffer = {};
|
|
301
|
+
this.currentToolCall = null;
|
|
302
|
+
this.previouslyParsableState = {};
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
bufferToolCalls(event: {
|
|
306
|
+
data: { chunk: { tool_call_chunks: { name: string | null; args: string }[] } };
|
|
307
|
+
}) {
|
|
308
|
+
if (event.data.chunk.tool_call_chunks.length > 0) {
|
|
309
|
+
const chunk = event.data.chunk.tool_call_chunks[0];
|
|
310
|
+
|
|
311
|
+
if (chunk.name !== null) {
|
|
312
|
+
this.currentToolCall = chunk.name;
|
|
313
|
+
this.toolCallBuffer[this.currentToolCall] = chunk.args;
|
|
314
|
+
} else if (this.currentToolCall !== null) {
|
|
315
|
+
this.toolCallBuffer[this.currentToolCall] += chunk.args;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
getEmitStateConfig(currentToolName: string): [string | null, string | null] {
|
|
321
|
+
for (const config of this.emitIntermediateState) {
|
|
322
|
+
const stateKey = config["state_key"];
|
|
323
|
+
const tool = config["tool"];
|
|
324
|
+
const toolArgument = config["tool_argument"];
|
|
325
|
+
|
|
326
|
+
if (currentToolName === tool) {
|
|
327
|
+
return [toolArgument, stateKey];
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return [null, null];
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
extractState(): State {
|
|
334
|
+
const state: State = {};
|
|
335
|
+
|
|
336
|
+
for (const [key, value] of Object.entries(this.toolCallBuffer)) {
|
|
337
|
+
const [argumentName, stateKey] = this.getEmitStateConfig(key);
|
|
338
|
+
|
|
339
|
+
if (stateKey === null) {
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
let parsedValue;
|
|
344
|
+
try {
|
|
345
|
+
parsedValue = parsePartialJson(value);
|
|
346
|
+
} catch (error) {
|
|
347
|
+
if (key in this.previouslyParsableState) {
|
|
348
|
+
parsedValue = this.previouslyParsableState[key];
|
|
349
|
+
} else {
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
this.previouslyParsableState[key] = parsedValue;
|
|
355
|
+
|
|
356
|
+
if (!argumentName) {
|
|
357
|
+
state[stateKey] = parsedValue;
|
|
358
|
+
} else {
|
|
359
|
+
state[stateKey] = parsedValue[argumentName];
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
return state;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// Start of Selection
|
|
368
|
+
function langGraphDefaultMergeState(
|
|
369
|
+
state: State,
|
|
370
|
+
messages: CopilotKitBaseMessage[],
|
|
371
|
+
actions: ExecutionAction[],
|
|
372
|
+
): State {
|
|
373
|
+
if (messages.length > 0 && "role" in messages[0] && messages[0].role === "system") {
|
|
374
|
+
// remove system message
|
|
375
|
+
messages = messages.slice(1);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// merge with existing messages
|
|
379
|
+
const mergedMessages = state.messages || [];
|
|
380
|
+
const existingMessageIds = new Set(mergedMessages.map((message) => message.id));
|
|
381
|
+
|
|
382
|
+
for (const message of messages) {
|
|
383
|
+
if (!existingMessageIds.has(message.id)) {
|
|
384
|
+
mergedMessages.push(message);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
return deepMerge(state, {
|
|
389
|
+
messages: mergedMessages,
|
|
390
|
+
copilotkit: {
|
|
391
|
+
actions,
|
|
392
|
+
},
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function deepMerge(obj1: State, obj2: State) {
|
|
397
|
+
let result = { ...obj1 };
|
|
398
|
+
for (let key in obj2) {
|
|
399
|
+
if (typeof obj2[key] === "object" && !Array.isArray(obj2[key])) {
|
|
400
|
+
if (obj1[key]) {
|
|
401
|
+
result[key] = deepMerge(obj1[key], obj2[key]);
|
|
402
|
+
} else {
|
|
403
|
+
result[key] = { ...obj2[key] };
|
|
404
|
+
}
|
|
405
|
+
} else {
|
|
406
|
+
result[key] = obj2[key];
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
return result;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function formatMessages(messages: CopilotKitBaseMessage[]): CopilotKitBaseMessage[] {
|
|
413
|
+
return messages.map((message) => {
|
|
414
|
+
if ("content" in message) {
|
|
415
|
+
return message;
|
|
416
|
+
}
|
|
417
|
+
if ("arguments" in message) {
|
|
418
|
+
const toolCall = {
|
|
419
|
+
name: message["name"],
|
|
420
|
+
args: message["arguments"],
|
|
421
|
+
id: message["id"],
|
|
422
|
+
};
|
|
423
|
+
return {
|
|
424
|
+
...message,
|
|
425
|
+
content: "",
|
|
426
|
+
tool_calls: [toolCall],
|
|
427
|
+
role: message["role"] ?? MessageRole.assistant,
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
if ("actionExecutionId" in message) {
|
|
431
|
+
return {
|
|
432
|
+
...message,
|
|
433
|
+
content: message["result"],
|
|
434
|
+
name: message["actionName"],
|
|
435
|
+
tool_call_id: message["actionExecutionId"] as string,
|
|
436
|
+
role: message["role"] ?? MessageRole.user,
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
return message;
|
|
440
|
+
});
|
|
441
|
+
}
|
|
@@ -260,11 +260,7 @@ async function executeAction(
|
|
|
260
260
|
// Prepare arguments for function calling
|
|
261
261
|
let args: Record<string, any>[] = [];
|
|
262
262
|
if (actionArguments) {
|
|
263
|
-
|
|
264
|
-
args = JSON.parse(actionArguments);
|
|
265
|
-
} catch (e) {
|
|
266
|
-
console.warn("Action argument unparsable", { actionArguments });
|
|
267
|
-
}
|
|
263
|
+
args = JSON.parse(actionArguments);
|
|
268
264
|
}
|
|
269
265
|
|
|
270
266
|
// handle LangGraph agents
|
|
@@ -51,11 +51,6 @@ export function convertMessageToLangChainMessage(message: Message): BaseMessage
|
|
|
51
51
|
export function convertJsonSchemaToZodSchema(jsonSchema: any, required: boolean): z.ZodSchema {
|
|
52
52
|
if (jsonSchema.type === "object") {
|
|
53
53
|
const spec: { [key: string]: z.ZodSchema } = {};
|
|
54
|
-
|
|
55
|
-
if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {
|
|
56
|
-
return !required ? z.object(spec).optional() : z.object(spec);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
54
|
for (const [key, value] of Object.entries(jsonSchema.properties)) {
|
|
60
55
|
spec[key] = convertJsonSchemaToZodSchema(
|
|
61
56
|
value,
|
|
@@ -63,20 +58,20 @@ export function convertJsonSchemaToZodSchema(jsonSchema: any, required: boolean)
|
|
|
63
58
|
);
|
|
64
59
|
}
|
|
65
60
|
let schema = z.object(spec);
|
|
66
|
-
return required ? schema : schema
|
|
61
|
+
return !required ? schema.optional() : schema;
|
|
67
62
|
} else if (jsonSchema.type === "string") {
|
|
68
63
|
let schema = z.string().describe(jsonSchema.description);
|
|
69
|
-
return required ? schema : schema
|
|
64
|
+
return !required ? schema.optional() : schema;
|
|
70
65
|
} else if (jsonSchema.type === "number") {
|
|
71
66
|
let schema = z.number().describe(jsonSchema.description);
|
|
72
|
-
return required ? schema : schema
|
|
67
|
+
return !required ? schema.optional() : schema;
|
|
73
68
|
} else if (jsonSchema.type === "boolean") {
|
|
74
69
|
let schema = z.boolean().describe(jsonSchema.description);
|
|
75
|
-
return required ? schema : schema
|
|
70
|
+
return !required ? schema.optional() : schema;
|
|
76
71
|
} else if (jsonSchema.type === "array") {
|
|
77
72
|
let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);
|
|
78
73
|
let schema = z.array(itemSchema);
|
|
79
|
-
return required ? schema : schema
|
|
74
|
+
return !required ? schema.optional() : schema;
|
|
80
75
|
}
|
|
81
76
|
}
|
|
82
77
|
|