@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
@@ -1,5 +1,5 @@
1
1
  import { ReplaySubject, scan, mergeMap, catchError } from "rxjs";
2
- import { LangGraphEvent, LangGraphEventTypes } from "./events";
2
+ import { CustomEventNames, LangGraphEvent, LangGraphEventTypes } from "./events";
3
3
  import { RuntimeEvent, RuntimeEventTypes } from "../../service-adapters/events";
4
4
  import { randomId } from "@copilotkit/shared";
5
5
 
@@ -15,63 +15,7 @@ interface LangGraphEventWithState {
15
15
  }
16
16
 
17
17
  export class RemoteLangGraphEventSource {
18
- private eventStream$ = new ReplaySubject<LangGraphEvent>();
19
-
20
- async streamResponse(response: Response) {
21
- const reader = response.body!.getReader();
22
- const decoder = new TextDecoder();
23
- let buffer = [];
24
- const eventStream$ = this.eventStream$;
25
-
26
- function flushBuffer() {
27
- const currentBuffer = buffer.join("");
28
- if (currentBuffer.trim().length === 0) {
29
- return;
30
- }
31
- const parts = currentBuffer.split("\n");
32
- if (parts.length === 0) {
33
- return;
34
- }
35
-
36
- const lastPartIsComplete = currentBuffer.endsWith("\n");
37
-
38
- // truncate buffer
39
- buffer = [];
40
-
41
- if (!lastPartIsComplete) {
42
- // put back the last part
43
- buffer.push(parts.pop());
44
- }
45
-
46
- parts
47
- .map((part) => part.trim())
48
- .filter((part) => part != "")
49
- .forEach((part) => {
50
- eventStream$.next(JSON.parse(part));
51
- });
52
- }
53
-
54
- try {
55
- while (true) {
56
- const { done, value } = await reader.read();
57
-
58
- if (!done) {
59
- buffer.push(decoder.decode(value, { stream: true }));
60
- }
61
-
62
- flushBuffer();
63
-
64
- if (done) {
65
- break;
66
- }
67
- }
68
- } catch (error) {
69
- console.error("Error in stream", error);
70
- eventStream$.error(error);
71
- return;
72
- }
73
- eventStream$.complete();
74
- }
18
+ public eventStream$ = new ReplaySubject<LangGraphEvent>();
75
19
 
76
20
  private shouldEmitToolCall(
77
21
  shouldEmitToolCalls: string | string[] | boolean,
@@ -93,7 +37,7 @@ export class RemoteLangGraphEventSource {
93
37
  scan(
94
38
  (acc, event) => {
95
39
  if (event.event === LangGraphEventTypes.OnChatModelStream) {
96
- // @ts-ignore
40
+ // @ts-expect-error -- LangGraph Cloud implementation stores data outside of kwargs
97
41
  const content = event.data?.chunk?.kwargs?.content ?? event.data?.chunk?.content;
98
42
 
99
43
  if (typeof content === "string") {
@@ -104,20 +48,28 @@ export class RemoteLangGraphEventSource {
104
48
  acc.content = null;
105
49
  }
106
50
 
107
- if (event.data?.chunk?.kwargs?.tool_call_chunks) {
51
+ const toolCallChunks =
52
+ // @ts-expect-error -- LangGraph Cloud implementation stores data outside of kwargs
53
+ event.data?.chunk?.kwargs?.tool_call_chunks ?? event.data?.chunk?.tool_call_chunks;
54
+
55
+ const toolCallMessageId =
56
+ event.data?.chunk?.kwargs?.id ??
57
+ (event.data?.chunk?.id as unknown as string | undefined);
58
+
59
+ if (toolCallChunks && toolCallChunks.length > 0) {
108
60
  acc.prevToolCallMessageId = acc.toolCallMessageId;
109
- acc.toolCallMessageId = event.data.chunk.kwargs?.id;
110
- if (event.data.chunk.kwargs.tool_call_chunks[0]?.name) {
111
- acc.toolCallName = event.data.chunk.kwargs.tool_call_chunks[0].name;
61
+ acc.toolCallMessageId = toolCallMessageId;
62
+ if (toolCallChunks[0]?.name) {
63
+ acc.toolCallName = toolCallChunks[0].name;
112
64
  }
113
- if (event.data.chunk.kwargs.tool_call_chunks[0]?.id) {
114
- acc.toolCallId = event.data.chunk.kwargs.tool_call_chunks[0].id;
65
+ if (toolCallChunks[0]?.id) {
66
+ acc.toolCallId = toolCallChunks[0].id;
115
67
  }
116
68
  acc.prevMessageId = acc.messageId;
117
- acc.messageId = event.data?.chunk?.kwargs?.id;
69
+ acc.messageId = toolCallMessageId;
118
70
  } else if (acc.content && acc.content != "") {
119
71
  acc.prevMessageId = acc.messageId;
120
- acc.messageId = event.data?.chunk?.kwargs?.id;
72
+ acc.messageId = toolCallMessageId;
121
73
  } else {
122
74
  acc.prevToolCallMessageId = acc.toolCallMessageId;
123
75
  acc.prevMessageId = acc.messageId;
@@ -183,32 +135,45 @@ export class RemoteLangGraphEventSource {
183
135
  }
184
136
 
185
137
  switch (eventWithState.event!.event) {
186
- case LangGraphEventTypes.OnCopilotKitEmitMessage:
187
- events.push({
188
- type: RuntimeEventTypes.TextMessageStart,
189
- messageId: eventWithState.event.message_id,
190
- });
191
- events.push({
192
- type: RuntimeEventTypes.TextMessageContent,
193
- content: eventWithState.event.message,
194
- });
195
- events.push({
196
- type: RuntimeEventTypes.TextMessageEnd,
197
- });
198
- break;
199
- case LangGraphEventTypes.OnCopilotKitEmitToolCall:
200
- events.push({
201
- type: RuntimeEventTypes.ActionExecutionStart,
202
- actionExecutionId: eventWithState.event.id,
203
- actionName: eventWithState.event.name,
204
- });
205
- events.push({
206
- type: RuntimeEventTypes.ActionExecutionArgs,
207
- args: JSON.stringify(eventWithState.event.args),
208
- });
209
- events.push({
210
- type: RuntimeEventTypes.ActionExecutionEnd,
211
- });
138
+ //
139
+ // Custom events
140
+ //
141
+ case LangGraphEventTypes.OnCustomEvent:
142
+ //
143
+ // Manually emit a message
144
+ //
145
+ if (eventWithState.event.name === CustomEventNames.CopilotKitManuallyEmitMessage) {
146
+ events.push({
147
+ type: RuntimeEventTypes.TextMessageStart,
148
+ messageId: eventWithState.event.data.message_id,
149
+ });
150
+ events.push({
151
+ type: RuntimeEventTypes.TextMessageContent,
152
+ content: eventWithState.event.data.message,
153
+ });
154
+ events.push({
155
+ type: RuntimeEventTypes.TextMessageEnd,
156
+ });
157
+ }
158
+ //
159
+ // Manually emit a tool call
160
+ //
161
+ else if (
162
+ eventWithState.event.name === CustomEventNames.CopilotKitManuallyEmitToolCall
163
+ ) {
164
+ events.push({
165
+ type: RuntimeEventTypes.ActionExecutionStart,
166
+ actionExecutionId: eventWithState.event.data.id,
167
+ actionName: eventWithState.event.data.name,
168
+ });
169
+ events.push({
170
+ type: RuntimeEventTypes.ActionExecutionArgs,
171
+ args: JSON.stringify(eventWithState.event.data.args),
172
+ });
173
+ events.push({
174
+ type: RuntimeEventTypes.ActionExecutionEnd,
175
+ });
176
+ }
212
177
  break;
213
178
  case LangGraphEventTypes.OnCopilotKitStateSync:
214
179
  events.push({
@@ -266,7 +231,10 @@ export class RemoteLangGraphEventSource {
266
231
  }
267
232
  }
268
233
 
269
- const args = eventWithState.event.data?.chunk?.kwargs?.tool_call_chunks?.[0]?.args;
234
+ const args =
235
+ eventWithState.event.data?.chunk?.kwargs?.tool_call_chunks?.[0]?.args ??
236
+ // @ts-expect-error -- sdf
237
+ eventWithState.event.data?.chunk?.tool_call_chunks?.[0]?.args;
270
238
  const content = eventWithState.content;
271
239
 
272
240
  // Tool call args: emit ActionExecutionArgs
@@ -8,8 +8,14 @@ export enum LangGraphEventTypes {
8
8
  OnToolStart = "on_tool_start",
9
9
  OnToolEnd = "on_tool_end",
10
10
  OnCopilotKitStateSync = "on_copilotkit_state_sync",
11
- OnCopilotKitEmitMessage = "on_copilotkit_emit_message",
12
- OnCopilotKitEmitToolCall = "on_copilotkit_emit_tool_call",
11
+ OnCustomEvent = "on_custom_event",
12
+ }
13
+
14
+ export enum CustomEventNames {
15
+ CopilotKitManuallyEmitMessage = "copilotkit_manually_emit_message",
16
+ CopilotKitManuallyEmitToolCall = "copilotkit_manually_emit_tool_call",
17
+ CopilotKitManuallyEmitIntermediateState = "copilotkit_manually_emit_intermediate_state",
18
+ CopilotKitExit = "copilotkit_exit",
13
19
  }
14
20
 
15
21
  type LangGraphOnCopilotKitStateSyncEvent = {
@@ -24,19 +30,6 @@ type LangGraphOnCopilotKitStateSyncEvent = {
24
30
  running: boolean;
25
31
  };
26
32
 
27
- type LangGraphOnCopilotKitManualMessageEvent = {
28
- event: LangGraphEventTypes.OnCopilotKitEmitMessage;
29
- message: string;
30
- message_id: string;
31
- };
32
-
33
- type LangGraphOnCopilotKitEmitToolCallEvent = {
34
- event: LangGraphEventTypes.OnCopilotKitEmitToolCall;
35
- name: string;
36
- args: any;
37
- id: string;
38
- };
39
-
40
33
  type LangGraphOnChainStartEvent = {
41
34
  event: LangGraphEventTypes.OnChainStart;
42
35
  run_id: string;
@@ -312,6 +305,24 @@ type LangGraphOnToolEndEvent = {
312
305
  parent_ids: string[];
313
306
  };
314
307
 
308
+ type LangGraphOnCustomEvent = {
309
+ event: LangGraphEventTypes.OnCustomEvent;
310
+ run_id: string;
311
+ name: string;
312
+ tags: string[];
313
+ metadata: {
314
+ thread_id: string;
315
+ langgraph_step: number;
316
+ langgraph_node: string;
317
+ langgraph_triggers: string[];
318
+ langgraph_path: [string, string];
319
+ langgraph_checkpoint_ns: string;
320
+ checkpoint_ns: string;
321
+ };
322
+ data: any;
323
+ parent_ids: string[];
324
+ };
325
+
315
326
  export type LangGraphEvent =
316
327
  | LangGraphOnChainStartEvent
317
328
  | LangGraphOnChainStreamEvent
@@ -322,5 +333,4 @@ export type LangGraphEvent =
322
333
  | LangGraphOnToolStartEvent
323
334
  | LangGraphOnToolEndEvent
324
335
  | LangGraphOnCopilotKitStateSyncEvent
325
- | LangGraphOnCopilotKitManualMessageEvent
326
- | LangGraphOnCopilotKitEmitToolCallEvent;
336
+ | LangGraphOnCustomEvent;
@@ -7,7 +7,11 @@ import {
7
7
  import { BaseMessageInput } from "../base";
8
8
  import { ActionExecutionScope, MessageRole } from "../enums";
9
9
 
10
- type MessageType = "TextMessage" | "ActionExecutionMessage" | "ResultMessage" | "AgentStateMessage";
10
+ export type MessageType =
11
+ | "TextMessage"
12
+ | "ActionExecutionMessage"
13
+ | "ResultMessage"
14
+ | "AgentStateMessage";
11
15
 
12
16
  export class Message extends BaseMessageInput {
13
17
  type: MessageType;
@@ -4,6 +4,7 @@ export enum MessageRole {
4
4
  user = "user",
5
5
  assistant = "assistant",
6
6
  system = "system",
7
+ tool = "tool",
7
8
  }
8
9
 
9
10
  export enum ActionExecutionScope {
@@ -12,19 +12,22 @@
12
12
  * ```
13
13
  */
14
14
 
15
- import { Action, actionParametersToJsonSchema, Parameter, randomId } from "@copilotkit/shared";
16
- import { RemoteChain, RemoteChainParameters, CopilotServiceAdapter } from "../../service-adapters";
15
+ import { Action, actionParametersToJsonSchema, Parameter } from "@copilotkit/shared";
16
+ import { CopilotServiceAdapter, RemoteChain, RemoteChainParameters } from "../../service-adapters";
17
17
  import { MessageInput } from "../../graphql/inputs/message.input";
18
18
  import { ActionInput } from "../../graphql/inputs/action.input";
19
19
  import { RuntimeEventSource } from "../../service-adapters/events";
20
20
  import { convertGqlInputToMessages } from "../../service-adapters/conversion";
21
- import { AgentStateMessage, Message } from "../../graphql/types/converted";
21
+ import { Message } from "../../graphql/types/converted";
22
22
  import { ForwardedParametersInput } from "../../graphql/inputs/forwarded-parameters.input";
23
23
  import {
24
- setupRemoteActions,
25
- RemoteActionDefinition,
26
- LangGraphAgentAction,
27
24
  isLangGraphAgentAction,
25
+ LangGraphAgentAction,
26
+ EndpointType,
27
+ setupRemoteActions,
28
+ EndpointDefinition,
29
+ CopilotKitEndpoint,
30
+ LangGraphCloudEndpoint,
28
31
  } from "./remote-actions";
29
32
  import { GraphQLContext } from "../integrations/shared";
30
33
  import { AgentSessionInput } from "../../graphql/inputs/agent-session.input";
@@ -121,10 +124,15 @@ export interface CopilotRuntimeConstructorParams<T extends Parameter[] | [] = []
121
124
  */
122
125
  actions?: ActionsConfiguration<T>;
123
126
 
127
+ /*
128
+ * Deprecated: See `remoteEndpoints`.
129
+ */
130
+ remoteActions?: EndpointDefinition[];
131
+
124
132
  /*
125
133
  * A list of remote actions that can be executed.
126
134
  */
127
- remoteActions?: RemoteActionDefinition[];
135
+ remoteEndpoints?: EndpointDefinition[];
128
136
 
129
137
  /*
130
138
  * An array of LangServer URLs.
@@ -134,7 +142,7 @@ export interface CopilotRuntimeConstructorParams<T extends Parameter[] | [] = []
134
142
 
135
143
  export class CopilotRuntime<const T extends Parameter[] | [] = []> {
136
144
  public actions: ActionsConfiguration<T>;
137
- private remoteActionDefinitions: RemoteActionDefinition[];
145
+ private remoteEndpointDefinitions: EndpointDefinition[];
138
146
  private langserve: Promise<Action<any>>[] = [];
139
147
  private onBeforeRequest?: OnBeforeRequestHandler;
140
148
  private onAfterRequest?: OnAfterRequestHandler;
@@ -147,7 +155,7 @@ export class CopilotRuntime<const T extends Parameter[] | [] = []> {
147
155
  this.langserve.push(remoteChain.toAction());
148
156
  }
149
157
 
150
- this.remoteActionDefinitions = params?.remoteActions || [];
158
+ this.remoteEndpointDefinitions = params?.remoteEndpoints || [];
151
159
 
152
160
  this.onBeforeRequest = params?.middleware?.onBeforeRequest;
153
161
  this.onAfterRequest = params?.middleware?.onAfterRequest;
@@ -244,7 +252,7 @@ export class CopilotRuntime<const T extends Parameter[] | [] = []> {
244
252
  request: CopilotRuntimeRequest,
245
253
  ): Promise<CopilotRuntimeResponse> {
246
254
  const { messages: rawMessages, outputMessagesPromise, graphqlContext, agentSession } = request;
247
- const { threadId = randomId(), agentName, nodeName } = agentSession;
255
+ const { threadId, agentName, nodeName } = agentSession;
248
256
  const serverSideActions = await this.getServerSideActions(request);
249
257
 
250
258
  const messages = convertGqlInputToMessages(rawMessages);
@@ -331,8 +339,17 @@ export class CopilotRuntime<const T extends Parameter[] | [] = []> {
331
339
  console.error("Error loading langserve chain:", error);
332
340
  }
333
341
  }
342
+
343
+ const remoteEndpointDefinitions = this.remoteEndpointDefinitions.map(
344
+ (endpoint) =>
345
+ ({
346
+ ...endpoint,
347
+ type: this.resolveEndpointType(endpoint),
348
+ }) as EndpointDefinition,
349
+ );
350
+
334
351
  const remoteActions = await setupRemoteActions({
335
- remoteActionDefinitions: this.remoteActionDefinitions,
352
+ remoteEndpointDefinitions,
336
353
  graphqlContext,
337
354
  messages: inputMessages,
338
355
  agentStates,
@@ -346,6 +363,19 @@ export class CopilotRuntime<const T extends Parameter[] | [] = []> {
346
363
 
347
364
  return [...configuredActions, ...langserveFunctions, ...remoteActions];
348
365
  }
366
+
367
+ private resolveEndpointType(endpoint: EndpointDefinition) {
368
+ if (
369
+ !endpoint.type &&
370
+ "langsmithApiKey" in endpoint &&
371
+ "deploymentUrl" in endpoint &&
372
+ "agents" in endpoint
373
+ ) {
374
+ return EndpointType.LangGraphCloud;
375
+ }
376
+
377
+ return endpoint.type;
378
+ }
349
379
  }
350
380
 
351
381
  export function flattenToolCallsNoDuplicates(toolsByPriority: ActionInput[]): ActionInput[] {
@@ -359,3 +389,20 @@ export function flattenToolCallsNoDuplicates(toolsByPriority: ActionInput[]): Ac
359
389
  }
360
390
  return allTools;
361
391
  }
392
+
393
+ // The two functions below are "factory functions", meant to create the action objects that adhere to the expected interfaces
394
+ export function copilotKitEndpoint(config: Omit<CopilotKitEndpoint, "type">): CopilotKitEndpoint {
395
+ return {
396
+ ...config,
397
+ type: EndpointType.CopilotKit,
398
+ };
399
+ }
400
+
401
+ export function langGraphCloudEndpoint(
402
+ config: Omit<LangGraphCloudEndpoint, "type">,
403
+ ): LangGraphCloudEndpoint {
404
+ return {
405
+ ...config,
406
+ type: EndpointType.LangGraphCloud,
407
+ };
408
+ }