@copilotkit/runtime 1.6.0 → 1.7.0-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +75 -0
  3. package/dist/{chunk-WUMAYJP3.mjs → chunk-D6YNY2XB.mjs} +2 -2
  4. package/dist/chunk-PTC5JN3P.mjs +1 -0
  5. package/dist/{chunk-DUW72ZZB.mjs → chunk-QTRO3GPV.mjs} +206 -70
  6. package/dist/chunk-QTRO3GPV.mjs.map +1 -0
  7. package/dist/{chunk-MPI4JZZR.mjs → chunk-QZ6X33MR.mjs} +2 -2
  8. package/dist/{chunk-2RP2NR4F.mjs → chunk-RQS3BGAT.mjs} +2 -2
  9. package/dist/{copilot-runtime-15bfc4f4.d.ts → copilot-runtime-5103c7e7.d.ts} +66 -1
  10. package/dist/index.d.ts +1 -1
  11. package/dist/index.js +206 -69
  12. package/dist/index.js.map +1 -1
  13. package/dist/index.mjs +7 -5
  14. package/dist/index.mjs.map +1 -1
  15. package/dist/lib/index.d.ts +1 -1
  16. package/dist/lib/index.js +206 -69
  17. package/dist/lib/index.js.map +1 -1
  18. package/dist/lib/index.mjs +7 -5
  19. package/dist/lib/integrations/index.d.ts +2 -2
  20. package/dist/lib/integrations/index.js +4 -1
  21. package/dist/lib/integrations/index.js.map +1 -1
  22. package/dist/lib/integrations/index.mjs +4 -4
  23. package/dist/lib/integrations/nest/index.d.ts +1 -1
  24. package/dist/lib/integrations/nest/index.js +4 -1
  25. package/dist/lib/integrations/nest/index.js.map +1 -1
  26. package/dist/lib/integrations/nest/index.mjs +2 -2
  27. package/dist/lib/integrations/node-express/index.d.ts +1 -1
  28. package/dist/lib/integrations/node-express/index.js +4 -1
  29. package/dist/lib/integrations/node-express/index.js.map +1 -1
  30. package/dist/lib/integrations/node-express/index.mjs +2 -2
  31. package/dist/lib/integrations/node-http/index.d.ts +1 -1
  32. package/dist/lib/integrations/node-http/index.js +4 -1
  33. package/dist/lib/integrations/node-http/index.js.map +1 -1
  34. package/dist/lib/integrations/node-http/index.mjs +1 -1
  35. package/package.json +2 -2
  36. package/src/graphql/resolvers/copilot.resolver.ts +4 -0
  37. package/src/lib/index.ts +1 -0
  38. package/src/lib/logger.ts +48 -0
  39. package/src/lib/runtime/__tests__/remote-action-constructors.test.ts +45 -35
  40. package/src/lib/runtime/copilot-runtime.ts +176 -16
  41. package/src/lib/runtime/remote-action-constructors.ts +28 -68
  42. package/src/lib/runtime/remote-actions.ts +5 -5
  43. package/src/lib/streaming.ts +59 -0
  44. package/src/service-adapters/events.ts +3 -3
  45. package/dist/chunk-DFOKBSIS.mjs +0 -1
  46. package/dist/chunk-DUW72ZZB.mjs.map +0 -1
  47. /package/dist/{chunk-WUMAYJP3.mjs.map → chunk-D6YNY2XB.mjs.map} +0 -0
  48. /package/dist/{chunk-DFOKBSIS.mjs.map → chunk-PTC5JN3P.mjs.map} +0 -0
  49. /package/dist/{chunk-MPI4JZZR.mjs.map → chunk-QZ6X33MR.mjs.map} +0 -0
  50. /package/dist/{chunk-2RP2NR4F.mjs.map → chunk-RQS3BGAT.mjs.map} +0 -0
@@ -1,7 +1,7 @@
1
1
  import { createHash } from "node:crypto";
2
2
  import {
3
3
  CopilotKitEndpoint,
4
- LangGraphAgentHandlerParams,
4
+ RemoteAgentHandlerParams,
5
5
  RemoteActionInfoResponse,
6
6
  LangGraphPlatformEndpoint,
7
7
  } from "./remote-actions";
@@ -10,13 +10,18 @@ import { Logger } from "pino";
10
10
  import { Message } from "../../graphql/types/converted";
11
11
  import { AgentStateInput } from "../../graphql/inputs/agent-state.input";
12
12
  import { Observable, ReplaySubject } from "rxjs";
13
- import { RuntimeEvent } from "../../service-adapters/events";
13
+ import {
14
+ RuntimeEvent,
15
+ RuntimeEventSource,
16
+ RuntimeEventSubject,
17
+ } from "../../service-adapters/events";
14
18
  import telemetry from "../telemetry-client";
15
19
  import { RemoteLangGraphEventSource } from "../../agents/langgraph/event-source";
16
20
  import { Action } from "@copilotkit/shared";
17
21
  import { LangGraphEvent } from "../../agents/langgraph/events";
18
22
  import { execute } from "./remote-lg-action";
19
23
  import { CopilotKitError, CopilotKitLowLevelError } from "@copilotkit/shared";
24
+ import { writeJsonLineResponseToEventStream } from "../streaming";
20
25
  import { CopilotKitApiDiscoveryError, ResolvedCopilotKitError } from "@copilotkit/shared";
21
26
  import { parseJson, tryMap } from "@copilotkit/shared";
22
27
  import { ActionInput } from "../../graphql/inputs/action.input";
@@ -39,14 +44,14 @@ export function constructLGCRemoteAction({
39
44
  description: agent.description,
40
45
  parameters: [],
41
46
  handler: async (_args: any) => {},
42
- langGraphAgentHandler: async ({
47
+ remoteAgentHandler: async ({
43
48
  name,
44
49
  actionInputsWithoutAgents,
45
50
  threadId,
46
51
  nodeName,
47
52
  additionalMessages = [],
48
53
  metaEvents,
49
- }: LangGraphAgentHandlerParams): Promise<Observable<RuntimeEvent>> => {
54
+ }: RemoteAgentHandlerParams): Promise<Observable<RuntimeEvent>> => {
50
55
  logger.debug({ actionName: agent.name }, "Executing LangGraph Platform agent");
51
56
 
52
57
  telemetry.capture("oss.runtime.remote_action_executed", {
@@ -87,7 +92,7 @@ export function constructLGCRemoteAction({
87
92
  });
88
93
 
89
94
  const eventSource = new RemoteLangGraphEventSource();
90
- streamResponse(response, eventSource.eventStream$);
95
+ writeJsonLineResponseToEventStream(response, eventSource.eventStream$);
91
96
  return eventSource.processLangGraphEvents();
92
97
  } catch (error) {
93
98
  logger.error(
@@ -102,6 +107,11 @@ export function constructLGCRemoteAction({
102
107
  return [...agents];
103
108
  }
104
109
 
110
+ export enum RemoteAgentType {
111
+ LangGraph = "langgraph",
112
+ CrewAI = "crewai",
113
+ }
114
+
105
115
  export function constructRemoteActions({
106
116
  json,
107
117
  url,
@@ -183,14 +193,14 @@ export function constructRemoteActions({
183
193
  parameters: [],
184
194
  handler: async (_args: any) => {},
185
195
 
186
- langGraphAgentHandler: async ({
196
+ remoteAgentHandler: async ({
187
197
  name,
188
198
  actionInputsWithoutAgents,
189
199
  threadId,
190
200
  nodeName,
191
201
  additionalMessages = [],
192
202
  metaEvents,
193
- }: LangGraphAgentHandlerParams): Promise<Observable<RuntimeEvent>> => {
203
+ }: RemoteAgentHandlerParams): Promise<Observable<RuntimeEvent>> => {
194
204
  logger.debug({ actionName: agent.name }, "Executing remote agent");
195
205
 
196
206
  const headers = createHeaders(onBeforeRequest, graphqlContext);
@@ -247,9 +257,17 @@ export function constructRemoteActions({
247
257
  });
248
258
  }
249
259
 
250
- const eventSource = new RemoteLangGraphEventSource();
251
- streamResponse(response.body!, eventSource.eventStream$);
252
- return eventSource.processLangGraphEvents();
260
+ if (agent.type === RemoteAgentType.LangGraph) {
261
+ const eventSource = new RemoteLangGraphEventSource();
262
+ writeJsonLineResponseToEventStream(response.body!, eventSource.eventStream$);
263
+ return eventSource.processLangGraphEvents();
264
+ } else if (agent.type === RemoteAgentType.CrewAI) {
265
+ const eventStream$ = new RuntimeEventSubject();
266
+ writeJsonLineResponseToEventStream(response.body!, eventStream$);
267
+ return eventStream$;
268
+ } else {
269
+ throw new Error("Unsupported agent type");
270
+ }
253
271
  } catch (error) {
254
272
  if (error instanceof CopilotKitError) {
255
273
  throw error;
@@ -263,64 +281,6 @@ export function constructRemoteActions({
263
281
  return [...actions, ...agents];
264
282
  }
265
283
 
266
- async function streamResponse(
267
- response: ReadableStream<Uint8Array>,
268
- eventStream$: ReplaySubject<LangGraphEvent>,
269
- ) {
270
- const reader = response.getReader();
271
- const decoder = new TextDecoder();
272
- let buffer = [];
273
-
274
- function flushBuffer() {
275
- const currentBuffer = buffer.join("");
276
- if (currentBuffer.trim().length === 0) {
277
- return;
278
- }
279
- const parts = currentBuffer.split("\n");
280
- if (parts.length === 0) {
281
- return;
282
- }
283
-
284
- const lastPartIsComplete = currentBuffer.endsWith("\n");
285
-
286
- // truncate buffer
287
- buffer = [];
288
-
289
- if (!lastPartIsComplete) {
290
- // put back the last part
291
- buffer.push(parts.pop());
292
- }
293
-
294
- parts
295
- .map((part) => part.trim())
296
- .filter((part) => part != "")
297
- .forEach((part) => {
298
- eventStream$.next(JSON.parse(part));
299
- });
300
- }
301
-
302
- try {
303
- while (true) {
304
- const { done, value } = await reader.read();
305
-
306
- if (!done) {
307
- buffer.push(decoder.decode(value, { stream: true }));
308
- }
309
-
310
- flushBuffer();
311
-
312
- if (done) {
313
- break;
314
- }
315
- }
316
- } catch (error) {
317
- console.error("Error in stream", error);
318
- eventStream$.error(error);
319
- return;
320
- }
321
- eventStream$.complete();
322
- }
323
-
324
284
  export function createHeaders(
325
285
  onBeforeRequest: CopilotKitEndpoint["onBeforeRequest"],
326
286
  graphqlContext: GraphQLContext,
@@ -54,7 +54,7 @@ export type RemoteActionInfoResponse = {
54
54
  agents: any[];
55
55
  };
56
56
 
57
- export type LangGraphAgentHandlerParams = {
57
+ export type RemoteAgentHandlerParams = {
58
58
  name: string;
59
59
  actionInputsWithoutAgents: ActionInput[];
60
60
  threadId?: string;
@@ -63,15 +63,15 @@ export type LangGraphAgentHandlerParams = {
63
63
  metaEvents?: MetaEventInput[];
64
64
  };
65
65
 
66
- export type LangGraphAgentAction = Action<any> & {
67
- langGraphAgentHandler: (params: LangGraphAgentHandlerParams) => Promise<Observable<RuntimeEvent>>;
66
+ export type RemoteAgentAction = Action<any> & {
67
+ remoteAgentHandler: (params: RemoteAgentHandlerParams) => Promise<Observable<RuntimeEvent>>;
68
68
  };
69
69
 
70
- export function isLangGraphAgentAction(action: Action<any>): action is LangGraphAgentAction {
70
+ export function isRemoteAgentAction(action: Action<any>): action is RemoteAgentAction {
71
71
  if (!action) {
72
72
  return false;
73
73
  }
74
- return typeof (action as LangGraphAgentAction).langGraphAgentHandler === "function";
74
+ return typeof (action as RemoteAgentAction).remoteAgentHandler === "function";
75
75
  }
76
76
 
77
77
  async function fetchRemoteInfo({
@@ -0,0 +1,59 @@
1
+ import { ReplaySubject } from "rxjs";
2
+
3
+ export async function writeJsonLineResponseToEventStream<T>(
4
+ response: ReadableStream<Uint8Array>,
5
+ eventStream$: ReplaySubject<T>,
6
+ ) {
7
+ const reader = response.getReader();
8
+ const decoder = new TextDecoder();
9
+ let buffer = [];
10
+
11
+ function flushBuffer() {
12
+ const currentBuffer = buffer.join("");
13
+ if (currentBuffer.trim().length === 0) {
14
+ return;
15
+ }
16
+ const parts = currentBuffer.split("\n");
17
+ if (parts.length === 0) {
18
+ return;
19
+ }
20
+
21
+ const lastPartIsComplete = currentBuffer.endsWith("\n");
22
+
23
+ // truncate buffer
24
+ buffer = [];
25
+
26
+ if (!lastPartIsComplete) {
27
+ // put back the last part
28
+ buffer.push(parts.pop());
29
+ }
30
+
31
+ parts
32
+ .map((part) => part.trim())
33
+ .filter((part) => part != "")
34
+ .forEach((part) => {
35
+ eventStream$.next(JSON.parse(part));
36
+ });
37
+ }
38
+
39
+ try {
40
+ while (true) {
41
+ const { done, value } = await reader.read();
42
+
43
+ if (!done) {
44
+ buffer.push(decoder.decode(value, { stream: true }));
45
+ }
46
+
47
+ flushBuffer();
48
+
49
+ if (done) {
50
+ break;
51
+ }
52
+ }
53
+ } catch (error) {
54
+ console.error("Error in stream", error);
55
+ eventStream$.error(error);
56
+ return;
57
+ }
58
+ eventStream$.complete();
59
+ }
@@ -14,7 +14,7 @@ import {
14
14
  import { streamLangChainResponse } from "./langchain/utils";
15
15
  import { GuardrailsResult } from "../graphql/types/guardrails-result.type";
16
16
  import telemetry from "../lib/telemetry-client";
17
- import { isLangGraphAgentAction } from "../lib/runtime/remote-actions";
17
+ import { isRemoteAgentAction } from "../lib/runtime/remote-actions";
18
18
  import { ActionInput } from "../graphql/inputs/action.input";
19
19
  import { ActionExecutionMessage, ResultMessage, TextMessage } from "../graphql/types/converted";
20
20
  import { plainToInstance } from "class-transformer";
@@ -372,7 +372,7 @@ async function executeAction(
372
372
  }
373
373
 
374
374
  // handle LangGraph agents
375
- if (isLangGraphAgentAction(action)) {
375
+ if (isRemoteAgentAction(action)) {
376
376
  const result = `${action.name} agent started`;
377
377
 
378
378
  const agentExecution = plainToInstance(ActionExecutionMessage, {
@@ -397,7 +397,7 @@ async function executeAction(
397
397
  result,
398
398
  });
399
399
 
400
- const stream = await action.langGraphAgentHandler({
400
+ const stream = await action.remoteAgentHandler({
401
401
  name: action.name,
402
402
  threadId,
403
403
  actionInputsWithoutAgents,
@@ -1 +0,0 @@
1
- //# sourceMappingURL=chunk-DFOKBSIS.mjs.map