@copilotkit/runtime 1.3.16-mme-reset-chat.3 → 1.4.0-feat-langgraph-cloud.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.
Files changed (66) hide show
  1. package/CHANGELOG.md +4 -25
  2. package/__snapshots__/schema/schema.graphql +1 -0
  3. package/dist/{chunk-JCKX6XUV.mjs → chunk-AEUECXAW.mjs} +675 -151
  4. package/dist/chunk-AEUECXAW.mjs.map +1 -0
  5. package/dist/{chunk-MYZHUCL6.mjs → chunk-B74M7FXG.mjs} +1 -1
  6. package/dist/chunk-B74M7FXG.mjs.map +1 -0
  7. package/dist/{chunk-PQQNLA6C.mjs → chunk-BLB2XDIL.mjs} +2 -2
  8. package/dist/{chunk-7ADMUSBN.mjs → chunk-DP7YGMSZ.mjs} +37 -10
  9. package/dist/chunk-DP7YGMSZ.mjs.map +1 -0
  10. package/dist/{chunk-NHVRGUZ6.mjs → chunk-ITCPKBTK.mjs} +2 -2
  11. package/dist/{chunk-SMVZ2NQP.mjs → chunk-VGYN7TGZ.mjs} +19 -29
  12. package/dist/{chunk-SMVZ2NQP.mjs.map → chunk-VGYN7TGZ.mjs.map} +1 -1
  13. package/dist/{chunk-PNSKIPTP.mjs → chunk-WQFBGT3B.mjs} +2 -2
  14. package/dist/{copilot-runtime-df3527ad.d.ts → copilot-runtime-543a59ae.d.ts} +29 -7
  15. package/dist/graphql/types/converted/index.d.ts +1 -1
  16. package/dist/graphql/types/converted/index.js.map +1 -1
  17. package/dist/graphql/types/converted/index.mjs +1 -1
  18. package/dist/{groq-adapter-b122e71f.d.ts → groq-adapter-7aa25931.d.ts} +4 -2
  19. package/dist/{index-cff31380.d.ts → index-83ee522f.d.ts} +3 -2
  20. package/dist/index.d.ts +4 -4
  21. package/dist/index.js +765 -224
  22. package/dist/index.js.map +1 -1
  23. package/dist/index.mjs +13 -9
  24. package/dist/index.mjs.map +1 -1
  25. package/dist/{langserve-a14a6849.d.ts → langserve-f00629d2.d.ts} +1 -1
  26. package/dist/lib/index.d.ts +4 -4
  27. package/dist/lib/index.js +765 -224
  28. package/dist/lib/index.js.map +1 -1
  29. package/dist/lib/index.mjs +13 -9
  30. package/dist/lib/integrations/index.d.ts +4 -4
  31. package/dist/lib/integrations/index.js +4 -1
  32. package/dist/lib/integrations/index.js.map +1 -1
  33. package/dist/lib/integrations/index.mjs +5 -5
  34. package/dist/lib/integrations/nest/index.d.ts +3 -3
  35. package/dist/lib/integrations/nest/index.js +4 -1
  36. package/dist/lib/integrations/nest/index.js.map +1 -1
  37. package/dist/lib/integrations/nest/index.mjs +3 -3
  38. package/dist/lib/integrations/node-express/index.d.ts +3 -3
  39. package/dist/lib/integrations/node-express/index.js +4 -1
  40. package/dist/lib/integrations/node-express/index.js.map +1 -1
  41. package/dist/lib/integrations/node-express/index.mjs +3 -3
  42. package/dist/lib/integrations/node-http/index.d.ts +3 -3
  43. package/dist/lib/integrations/node-http/index.js +4 -1
  44. package/dist/lib/integrations/node-http/index.js.map +1 -1
  45. package/dist/lib/integrations/node-http/index.mjs +2 -2
  46. package/dist/service-adapters/index.d.ts +4 -4
  47. package/dist/service-adapters/index.js +18 -28
  48. package/dist/service-adapters/index.js.map +1 -1
  49. package/dist/service-adapters/index.mjs +1 -1
  50. package/package.json +6 -4
  51. package/src/agents/langgraph/event-source.ts +62 -94
  52. package/src/agents/langgraph/events.ts +27 -17
  53. package/src/graphql/types/converted/index.ts +5 -1
  54. package/src/graphql/types/enums.ts +1 -0
  55. package/src/lib/runtime/copilot-runtime.ts +58 -11
  56. package/src/lib/runtime/remote-action-constructors.ts +284 -0
  57. package/src/lib/runtime/remote-actions.ts +65 -159
  58. package/src/lib/runtime/remote-lg-cloud-action.ts +613 -0
  59. package/src/service-adapters/google/google-genai-adapter.ts +2 -2
  60. package/src/service-adapters/langchain/langchain-adapter.ts +19 -23
  61. package/dist/chunk-7ADMUSBN.mjs.map +0 -1
  62. package/dist/chunk-JCKX6XUV.mjs.map +0 -1
  63. package/dist/chunk-MYZHUCL6.mjs.map +0 -1
  64. /package/dist/{chunk-PQQNLA6C.mjs.map → chunk-BLB2XDIL.mjs.map} +0 -0
  65. /package/dist/{chunk-NHVRGUZ6.mjs.map → chunk-ITCPKBTK.mjs.map} +0 -0
  66. /package/dist/{chunk-PNSKIPTP.mjs.map → chunk-WQFBGT3B.mjs.map} +0 -0
@@ -0,0 +1,613 @@
1
+ import { Client } from "@langchain/langgraph-sdk";
2
+ import { randomUUID } from "node:crypto";
3
+ import { parse as parsePartialJson } from "partial-json";
4
+ import { Logger } from "pino";
5
+ import { ActionInput } from "../../graphql/inputs/action.input";
6
+ import { LangGraphCloudAgent, LangGraphCloudEndpoint } from "./remote-actions";
7
+ import { CopilotRequestContextProperties } from "../integrations";
8
+ import { Message, MessageType } from "../../graphql/types/converted";
9
+ import { MessageRole } from "../../graphql/types/enums";
10
+ import { CustomEventNames, LangGraphEventTypes } from "../../agents/langgraph/events";
11
+
12
+ type State = Record<string, any>;
13
+
14
+ type ExecutionAction = Pick<ActionInput, "name" | "description"> & { parameters: string };
15
+
16
+ interface ExecutionArgs extends Omit<LangGraphCloudEndpoint, "agents"> {
17
+ agent: LangGraphCloudAgent;
18
+ threadId: string;
19
+ nodeName: string;
20
+ messages: Message[];
21
+ state: State;
22
+ properties: CopilotRequestContextProperties;
23
+ actions: ExecutionAction[];
24
+ logger: Logger;
25
+ }
26
+
27
+ // The following types are our own definition to the messages accepted by LangGraph cloud, enhanced with some of our extra data.
28
+ interface ToolCall {
29
+ id: string;
30
+ name: string;
31
+ args: Record<string, unknown>;
32
+ }
33
+
34
+ type BaseLangGraphCloudMessage = Omit<
35
+ Message,
36
+ | "isResultMessage"
37
+ | "isTextMessage"
38
+ | "isActionExecutionMessage"
39
+ | "isAgentStateMessage"
40
+ | "type"
41
+ | "createdAt"
42
+ > & {
43
+ content: string;
44
+ role: MessageRole;
45
+ additional_kwargs?: Record<string, unknown>;
46
+ type: MessageType;
47
+ };
48
+
49
+ interface LangGraphCloudResultMessage extends BaseLangGraphCloudMessage {
50
+ tool_call_id: string;
51
+ name: string;
52
+ }
53
+
54
+ interface LangGraphCloudActionExecutionMessage extends BaseLangGraphCloudMessage {
55
+ tool_calls: ToolCall[];
56
+ }
57
+
58
+ type LangGraphCloudMessage =
59
+ | LangGraphCloudActionExecutionMessage
60
+ | LangGraphCloudResultMessage
61
+ | BaseLangGraphCloudMessage;
62
+
63
+ export async function execute(args: ExecutionArgs): Promise<ReadableStream<Uint8Array>> {
64
+ return new ReadableStream({
65
+ async start(controller) {
66
+ try {
67
+ await streamEvents(controller, args);
68
+ controller.close();
69
+ } catch (err) {}
70
+ },
71
+ });
72
+ }
73
+
74
+ async function streamEvents(controller: ReadableStreamDefaultController, args: ExecutionArgs) {
75
+ const {
76
+ deploymentUrl,
77
+ langsmithApiKey,
78
+ threadId: agrsInitialThreadId,
79
+ agent,
80
+ nodeName: initialNodeName,
81
+ state: initialState,
82
+ messages,
83
+ actions,
84
+ logger,
85
+ } = args;
86
+
87
+ let nodeName = initialNodeName;
88
+ let state = initialState;
89
+ const { name, assistantId: initialAssistantId } = agent;
90
+
91
+ // TODO: deploymentUrl is not required in local development
92
+ const client = new Client({ apiUrl: deploymentUrl, apiKey: langsmithApiKey });
93
+ let initialThreadId = agrsInitialThreadId;
94
+ const wasInitiatedWithExistingThread = !!initialThreadId;
95
+ if (initialThreadId && initialThreadId.startsWith("ck-")) {
96
+ initialThreadId = initialThreadId.substring(3);
97
+ }
98
+
99
+ const assistants = await client.assistants.search();
100
+ const retrievedAssistant = assistants.find((a) => a.name === name);
101
+ const threadId = initialThreadId ?? randomUUID();
102
+ if (initialThreadId === threadId) {
103
+ await client.threads.get(threadId);
104
+ } else {
105
+ await client.threads.create({ threadId: threadId });
106
+ }
107
+
108
+ let agentState = { values: {} };
109
+ if (wasInitiatedWithExistingThread) {
110
+ agentState = await client.threads.getState(threadId);
111
+ }
112
+ const agentStateValues = agentState.values as State;
113
+ state.messages = agentStateValues.messages;
114
+ const mode = wasInitiatedWithExistingThread && nodeName != "__end__" ? "continue" : "start";
115
+ let formattedMessages = [];
116
+ try {
117
+ formattedMessages = formatMessages(messages);
118
+ } catch (e) {
119
+ logger.error(e, `Error event thrown: ${e.message}`);
120
+ }
121
+ state = langGraphDefaultMergeState(state, formattedMessages, actions, name);
122
+
123
+ if (mode === "continue") {
124
+ await client.threads.updateState(threadId, { values: state, asNode: nodeName });
125
+ }
126
+
127
+ const assistantId = initialAssistantId ?? retrievedAssistant?.assistant_id;
128
+ if (!assistantId) {
129
+ console.error(`
130
+ No agent found for the agent name specified in CopilotKit provider
131
+ Please check your available agents or provide an agent ID in the LangGraph Cloud endpoint definition.\n
132
+
133
+ These are the available agents: [${assistants.map((a) => `${a.name} (ID: ${a.assistant_id})`).join(", ")}]
134
+ `);
135
+ throw new Error("No agent id found");
136
+ }
137
+ const graphInfo = await client.assistants.getGraph(assistantId);
138
+ const streamInput = mode === "start" ? state : null;
139
+
140
+ let streamingStateExtractor = new StreamingStateExtractor([]);
141
+ let prevNodeName = null;
142
+ let emitIntermediateStateUntilEnd = null;
143
+ let shouldExit = null;
144
+ let externalRunId = null;
145
+
146
+ const streamResponse = client.runs.stream(threadId, assistantId, {
147
+ input: streamInput,
148
+ streamMode: ["events", "values"],
149
+ });
150
+
151
+ const emit = (message: string) => controller.enqueue(new TextEncoder().encode(message));
152
+
153
+ let latestStateValues = {};
154
+
155
+ try {
156
+ for await (const chunk of streamResponse) {
157
+ if (!["events", "values", "error"].includes(chunk.event)) continue;
158
+
159
+ if (chunk.event === "error") {
160
+ logger.error(chunk, `Error event thrown: ${chunk.data.message}`);
161
+ throw new Error(`Error event thrown: ${chunk.data.message}`);
162
+ }
163
+
164
+ if (chunk.event === "values") {
165
+ latestStateValues = chunk.data;
166
+ continue;
167
+ }
168
+
169
+ const event = chunk.data;
170
+ const currentNodeName = event.name;
171
+ const eventType = event.event;
172
+ const runId = event.metadata.run_id;
173
+ externalRunId = runId;
174
+ const metadata = event.metadata;
175
+
176
+ shouldExit =
177
+ shouldExit != null
178
+ ? shouldExit
179
+ : eventType === LangGraphEventTypes.OnCustomEvent &&
180
+ event.name === CustomEventNames.CopilotKitExit;
181
+
182
+ const emitIntermediateState = metadata["copilotkit:emit-intermediate-state"];
183
+ const manuallyEmitIntermediateState =
184
+ eventType === LangGraphEventTypes.OnCustomEvent &&
185
+ event.name === CustomEventNames.CopilotKitManuallyEmitIntermediateState;
186
+
187
+ // we only want to update the node name under certain conditions
188
+ // since we don't need any internal node names to be sent to the frontend
189
+ if (graphInfo["nodes"].some((node) => node.id === currentNodeName)) {
190
+ nodeName = currentNodeName;
191
+ }
192
+
193
+ if (!nodeName) {
194
+ continue;
195
+ }
196
+
197
+ if (manuallyEmitIntermediateState) {
198
+ if (eventType === LangGraphEventTypes.OnChainEnd) {
199
+ state = event.data.output;
200
+ emit(
201
+ getStateSyncEvent({
202
+ threadId,
203
+ runId,
204
+ agentName: agent.name,
205
+ nodeName,
206
+ state: event.data.output,
207
+ running: true,
208
+ active: true,
209
+ }),
210
+ );
211
+ }
212
+ continue;
213
+ }
214
+
215
+ if (emitIntermediateState && emitIntermediateStateUntilEnd == null) {
216
+ emitIntermediateStateUntilEnd = nodeName;
217
+ }
218
+
219
+ if (emitIntermediateState && eventType === LangGraphEventTypes.OnChatModelStart) {
220
+ // reset the streaming state extractor
221
+ streamingStateExtractor = new StreamingStateExtractor(emitIntermediateState);
222
+ }
223
+
224
+ let updatedState = latestStateValues;
225
+
226
+ if (emitIntermediateState && eventType === LangGraphEventTypes.OnChatModelStream) {
227
+ streamingStateExtractor.bufferToolCalls(event);
228
+ }
229
+
230
+ if (emitIntermediateStateUntilEnd !== null) {
231
+ updatedState = {
232
+ ...updatedState,
233
+ ...streamingStateExtractor.extractState(),
234
+ };
235
+ }
236
+
237
+ if (
238
+ !emitIntermediateState &&
239
+ currentNodeName === emitIntermediateStateUntilEnd &&
240
+ eventType === LangGraphEventTypes.OnChainEnd
241
+ ) {
242
+ // stop emitting function call state
243
+ emitIntermediateStateUntilEnd = null;
244
+ }
245
+
246
+ const exitingNode =
247
+ nodeName === currentNodeName && eventType === LangGraphEventTypes.OnChainEnd;
248
+
249
+ if (
250
+ JSON.stringify(updatedState) !== JSON.stringify(state) ||
251
+ prevNodeName != nodeName ||
252
+ exitingNode
253
+ ) {
254
+ state = updatedState;
255
+ prevNodeName = nodeName;
256
+ emit(
257
+ getStateSyncEvent({
258
+ threadId,
259
+ runId,
260
+ agentName: agent.name,
261
+ nodeName,
262
+ state,
263
+ running: true,
264
+ active: !exitingNode,
265
+ }),
266
+ );
267
+ }
268
+
269
+ emit(JSON.stringify(event) + "\n");
270
+ }
271
+
272
+ state = await client.threads.getState(threadId);
273
+ const isEndNode = state.next.length === 0;
274
+ nodeName = Object.keys(state.metadata.writes)[0];
275
+
276
+ emit(
277
+ getStateSyncEvent({
278
+ threadId,
279
+ runId: externalRunId,
280
+ agentName: agent.name,
281
+ nodeName: isEndNode ? "__end__" : nodeName,
282
+ state: state.values,
283
+ running: !shouldExit,
284
+ active: false,
285
+ }),
286
+ );
287
+
288
+ return Promise.resolve();
289
+ } catch (e) {
290
+ // TODO: handle error state here.
291
+ return Promise.resolve();
292
+ }
293
+ }
294
+
295
+ function getStateSyncEvent({
296
+ threadId,
297
+ runId,
298
+ agentName,
299
+ nodeName,
300
+ state,
301
+ running,
302
+ active,
303
+ }: {
304
+ threadId: string;
305
+ runId: string;
306
+ agentName: string;
307
+ nodeName: string;
308
+ state: State;
309
+ running: boolean;
310
+ active: boolean;
311
+ }): string {
312
+ const stateWithoutMessages = Object.keys(state).reduce((acc, key) => {
313
+ if (key !== "messages") {
314
+ acc[key] = state[key];
315
+ }
316
+ return acc;
317
+ }, {} as State);
318
+
319
+ return (
320
+ JSON.stringify({
321
+ event: LangGraphEventTypes.OnCopilotKitStateSync,
322
+ thread_id: threadId,
323
+ run_id: runId,
324
+ agent_name: agentName,
325
+ node_name: nodeName,
326
+ active: active,
327
+ state: stateWithoutMessages,
328
+ running: running,
329
+ role: "assistant",
330
+ }) + "\n"
331
+ );
332
+ }
333
+
334
+ class StreamingStateExtractor {
335
+ private emitIntermediateState: { [key: string]: any }[];
336
+ private toolCallBuffer: { [key: string]: string };
337
+ private currentToolCall: string | null;
338
+ private previouslyParsableState: { [key: string]: any };
339
+
340
+ constructor(emitIntermediateState: { [key: string]: any }[]) {
341
+ this.emitIntermediateState = emitIntermediateState;
342
+ this.toolCallBuffer = {};
343
+ this.currentToolCall = null;
344
+ this.previouslyParsableState = {};
345
+ }
346
+
347
+ bufferToolCalls(event: {
348
+ data: { chunk: { tool_call_chunks: { name: string | null; args: string }[] } };
349
+ }) {
350
+ if (event.data.chunk.tool_call_chunks.length > 0) {
351
+ const chunk = event.data.chunk.tool_call_chunks[0];
352
+
353
+ if (chunk.name !== null) {
354
+ this.currentToolCall = chunk.name;
355
+ this.toolCallBuffer[this.currentToolCall] = chunk.args;
356
+ } else if (this.currentToolCall !== null) {
357
+ this.toolCallBuffer[this.currentToolCall] += chunk.args;
358
+ }
359
+ }
360
+ }
361
+
362
+ getEmitStateConfig(currentToolName: string): [string | null, string | null] {
363
+ for (const config of this.emitIntermediateState) {
364
+ const stateKey = config["state_key"];
365
+ const tool = config["tool"];
366
+ const toolArgument = config["tool_argument"];
367
+
368
+ if (currentToolName === tool) {
369
+ return [toolArgument, stateKey];
370
+ }
371
+ }
372
+ return [null, null];
373
+ }
374
+
375
+ extractState(): State {
376
+ const state: State = {};
377
+
378
+ for (const [key, value] of Object.entries(this.toolCallBuffer)) {
379
+ const [argumentName, stateKey] = this.getEmitStateConfig(key);
380
+
381
+ if (stateKey === null) {
382
+ continue;
383
+ }
384
+
385
+ let parsedValue;
386
+ try {
387
+ parsedValue = parsePartialJson(value);
388
+ } catch (error) {
389
+ if (key in this.previouslyParsableState) {
390
+ parsedValue = this.previouslyParsableState[key];
391
+ } else {
392
+ continue;
393
+ }
394
+ }
395
+
396
+ this.previouslyParsableState[key] = parsedValue;
397
+
398
+ if (!argumentName) {
399
+ state[stateKey] = parsedValue;
400
+ } else {
401
+ state[stateKey] = parsedValue[argumentName];
402
+ }
403
+ }
404
+
405
+ return state;
406
+ }
407
+ }
408
+
409
+ // Start of Selection
410
+ function langGraphDefaultMergeState(
411
+ state: State,
412
+ messages: LangGraphCloudMessage[],
413
+ actions: ExecutionAction[],
414
+ agentName: string,
415
+ ): State {
416
+ if (messages.length > 0 && "role" in messages[0] && messages[0].role === "system") {
417
+ // remove system message
418
+ messages = messages.slice(1);
419
+ }
420
+
421
+ // merge with existing messages
422
+ const mergedMessages: LangGraphCloudMessage[] = state.messages || [];
423
+ const existingMessageIds = new Set(mergedMessages.map((message) => message.id));
424
+ const existingToolCallResults = new Set<string>();
425
+
426
+ for (const message of mergedMessages) {
427
+ if ("tool_call_id" in message) {
428
+ existingToolCallResults.add(message.tool_call_id);
429
+ }
430
+ }
431
+
432
+ for (const message of messages) {
433
+ // filter tool calls to activate the agent itself
434
+ if (
435
+ "tool_calls" in message &&
436
+ message.tool_calls.length > 0 &&
437
+ message.tool_calls[0].name === agentName
438
+ ) {
439
+ continue;
440
+ }
441
+
442
+ // filter results from activating the agent
443
+ if ("name" in message && message.name === agentName) {
444
+ continue;
445
+ }
446
+
447
+ if (!existingMessageIds.has(message.id)) {
448
+ // skip duplicate tool call results
449
+ if ("tool_call_id" in message && existingToolCallResults.has(message.tool_call_id)) {
450
+ console.warn("Warning: Duplicate tool call result, skipping:", message.tool_call_id);
451
+ continue;
452
+ }
453
+
454
+ mergedMessages.push(message);
455
+ } else {
456
+ // Replace the message with the existing one
457
+ for (let i = 0; i < mergedMessages.length; i++) {
458
+ if (mergedMessages[i].id === message.id) {
459
+ if ("tool_calls" in message) {
460
+ if (
461
+ ("tool_calls" in mergedMessages[i] || "additional_kwargs" in mergedMessages[i]) &&
462
+ mergedMessages[i].content
463
+ ) {
464
+ message.tool_calls = mergedMessages[i]["tool_calls"];
465
+ message.additional_kwargs = mergedMessages[i].additional_kwargs;
466
+ }
467
+ }
468
+ mergedMessages[i] = message;
469
+ }
470
+ }
471
+ }
472
+ }
473
+
474
+ // fix wrong tool call ids
475
+ for (let i = 0; i < mergedMessages.length - 1; i++) {
476
+ const currentMessage = mergedMessages[i];
477
+ const nextMessage = mergedMessages[i + 1];
478
+
479
+ if (
480
+ "tool_calls" in currentMessage &&
481
+ currentMessage.tool_calls.length > 0 &&
482
+ "tool_call_id" in nextMessage
483
+ ) {
484
+ nextMessage.tool_call_id = currentMessage.tool_calls[0].id;
485
+ }
486
+ }
487
+
488
+ // try to auto-correct and log alignment issues
489
+ const correctedMessages: LangGraphCloudMessage[] = [];
490
+
491
+ for (let i = 0; i < mergedMessages.length; i++) {
492
+ const currentMessage = mergedMessages[i];
493
+ const nextMessage = mergedMessages[i + 1] || null;
494
+ const prevMessage = mergedMessages[i - 1] || null;
495
+
496
+ if ("tool_calls" in currentMessage && currentMessage.tool_calls.length > 0) {
497
+ if (!nextMessage) {
498
+ console.warn(
499
+ "No next message to auto-correct tool call, skipping:",
500
+ currentMessage.tool_calls[0].id,
501
+ );
502
+ continue;
503
+ }
504
+
505
+ if (
506
+ !("tool_call_id" in nextMessage) ||
507
+ nextMessage.tool_call_id !== currentMessage.tool_calls[0].id
508
+ ) {
509
+ const toolMessage = mergedMessages.find(
510
+ (m) => "tool_call_id" in m && m.tool_call_id === currentMessage.tool_calls[0].id,
511
+ );
512
+
513
+ if (toolMessage) {
514
+ console.warn(
515
+ "Auto-corrected tool call alignment issue:",
516
+ currentMessage.tool_calls[0].id,
517
+ );
518
+ correctedMessages.push(currentMessage, toolMessage);
519
+ continue;
520
+ } else {
521
+ console.warn(
522
+ "No corresponding tool call result found for tool call, skipping:",
523
+ currentMessage.tool_calls[0].id,
524
+ );
525
+ continue;
526
+ }
527
+ }
528
+
529
+ correctedMessages.push(currentMessage);
530
+ continue;
531
+ }
532
+
533
+ if ("tool_call_id" in currentMessage) {
534
+ if (!prevMessage || !("tool_calls" in prevMessage)) {
535
+ console.warn("No previous tool call, skipping tool call result:", currentMessage.id);
536
+ continue;
537
+ }
538
+
539
+ if (prevMessage.tool_calls && prevMessage.tool_calls[0].id !== currentMessage.tool_call_id) {
540
+ console.warn("Tool call id is incorrect, skipping tool call result:", currentMessage.id);
541
+ continue;
542
+ }
543
+
544
+ correctedMessages.push(currentMessage);
545
+ continue;
546
+ }
547
+
548
+ correctedMessages.push(currentMessage);
549
+ }
550
+
551
+ return deepMerge(state, {
552
+ messages: correctedMessages,
553
+ copilotkit: {
554
+ actions,
555
+ },
556
+ });
557
+ }
558
+
559
+ function deepMerge(obj1: State, obj2: State) {
560
+ let result = { ...obj1 };
561
+ for (let key in obj2) {
562
+ if (typeof obj2[key] === "object" && !Array.isArray(obj2[key])) {
563
+ if (obj1[key]) {
564
+ result[key] = deepMerge(obj1[key], obj2[key]);
565
+ } else {
566
+ result[key] = { ...obj2[key] };
567
+ }
568
+ } else {
569
+ result[key] = obj2[key];
570
+ }
571
+ }
572
+ return result;
573
+ }
574
+
575
+ function formatMessages(messages: Message[]): LangGraphCloudMessage[] {
576
+ return messages.map((message) => {
577
+ if (message.isTextMessage() && message.role === "assistant") {
578
+ return message;
579
+ }
580
+ if (message.isTextMessage() && message.role === "system") {
581
+ return message;
582
+ }
583
+ if (message.isTextMessage() && message.role === "user") {
584
+ return message;
585
+ }
586
+ if (message.isActionExecutionMessage()) {
587
+ const toolCall: ToolCall = {
588
+ name: message.name,
589
+ args: message.arguments,
590
+ id: message.id,
591
+ };
592
+ return {
593
+ type: message.type,
594
+ content: "",
595
+ tool_calls: [toolCall],
596
+ role: MessageRole.assistant,
597
+ id: message.id,
598
+ } satisfies LangGraphCloudActionExecutionMessage;
599
+ }
600
+ if (message.isResultMessage()) {
601
+ return {
602
+ type: message.type,
603
+ content: message.result,
604
+ id: message.id,
605
+ tool_call_id: message.actionExecutionId,
606
+ name: message.actionName,
607
+ role: MessageRole.tool,
608
+ } satisfies LangGraphCloudResultMessage;
609
+ }
610
+
611
+ throw new Error(`Unknown message type ${message.type}`);
612
+ });
613
+ }
@@ -29,12 +29,12 @@ interface GoogleGenerativeAIAdapterOptions {
29
29
  export class GoogleGenerativeAIAdapter extends LangChainAdapter {
30
30
  constructor(options?: GoogleGenerativeAIAdapterOptions) {
31
31
  super({
32
- chainFn: async ({ messages, tools, threadId }) => {
32
+ chainFn: async ({ messages, tools }) => {
33
33
  const model = new ChatGoogle({
34
34
  modelName: options?.model ?? "gemini-1.5-pro",
35
35
  apiVersion: "v1beta",
36
36
  }).bindTools(tools);
37
- return model.stream(messages, { metadata: { conversation_id: threadId } });
37
+ return model.stream(messages);
38
38
  },
39
39
  });
40
40
  }
@@ -16,7 +16,9 @@
16
16
  *
17
17
  * const serviceAdapter = new LangChainAdapter({
18
18
  * chainFn: async ({ messages, tools }) => {
19
- * return model.stream(messages, { tools });
19
+ * return model.bindTools(tools).stream(messages);
20
+ * // or optionally enable strict mode
21
+ * // return model.bindTools(tools, { strict: true }).stream(messages);
20
22
  * }
21
23
  * });
22
24
  *
@@ -45,7 +47,6 @@ import {
45
47
  import { DynamicStructuredTool } from "@langchain/core/tools";
46
48
  import { LangChainReturnType } from "./types";
47
49
  import { randomId } from "@copilotkit/shared";
48
- import { awaitAllCallbacks } from "@langchain/core/callbacks/promises";
49
50
 
50
51
  interface ChainFnParameters {
51
52
  model: string;
@@ -71,29 +72,24 @@ export class LangChainAdapter implements CopilotServiceAdapter {
71
72
  async process(
72
73
  request: CopilotRuntimeChatCompletionRequest,
73
74
  ): Promise<CopilotRuntimeChatCompletionResponse> {
74
- try {
75
- const { eventSource, model, actions, messages, runId } = request;
76
- const threadId = request.threadId ?? randomId();
77
- const result = await this.options.chainFn({
78
- messages: messages.map(convertMessageToLangChainMessage),
79
- tools: actions.map(convertActionInputToLangChainTool),
80
- model,
81
- threadId,
82
- runId,
83
- });
75
+ const { eventSource, model, actions, messages, threadId, runId } = request;
76
+ const result = await this.options.chainFn({
77
+ messages: messages.map(convertMessageToLangChainMessage),
78
+ tools: actions.map(convertActionInputToLangChainTool),
79
+ model,
80
+ threadId,
81
+ runId,
82
+ });
84
83
 
85
- eventSource.stream(async (eventStream$) => {
86
- await streamLangChainResponse({
87
- result,
88
- eventStream$,
89
- });
84
+ eventSource.stream(async (eventStream$) => {
85
+ await streamLangChainResponse({
86
+ result,
87
+ eventStream$,
90
88
  });
89
+ });
91
90
 
92
- return {
93
- threadId,
94
- };
95
- } finally {
96
- await awaitAllCallbacks();
97
- }
91
+ return {
92
+ threadId: threadId || randomId(),
93
+ };
98
94
  }
99
95
  }