@copilotkit/runtime 1.6.0-next.3 → 1.6.0-next.4

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 (41) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/{chunk-ZM4JOETB.mjs → chunk-4FCUC27H.mjs} +2 -2
  3. package/dist/{chunk-OS5YD32G.mjs → chunk-7EXH7PVD.mjs} +3 -2
  4. package/dist/chunk-7EXH7PVD.mjs.map +1 -0
  5. package/dist/{chunk-25Z2ZUVM.mjs → chunk-ROFUPT7E.mjs} +2 -2
  6. package/dist/{chunk-J6E3ZTJ3.mjs → chunk-YUCVJM6E.mjs} +22 -25
  7. package/dist/chunk-YUCVJM6E.mjs.map +1 -0
  8. package/dist/{chunk-7HPGWUP7.mjs → chunk-Z5VUD7NL.mjs} +2 -2
  9. package/dist/index.js +88 -90
  10. package/dist/index.js.map +1 -1
  11. package/dist/index.mjs +10 -10
  12. package/dist/lib/index.js +81 -83
  13. package/dist/lib/index.js.map +1 -1
  14. package/dist/lib/index.mjs +6 -6
  15. package/dist/lib/integrations/index.js +2 -2
  16. package/dist/lib/integrations/index.js.map +1 -1
  17. package/dist/lib/integrations/index.mjs +5 -5
  18. package/dist/lib/integrations/nest/index.js +2 -2
  19. package/dist/lib/integrations/nest/index.js.map +1 -1
  20. package/dist/lib/integrations/nest/index.mjs +3 -3
  21. package/dist/lib/integrations/node-express/index.js +2 -2
  22. package/dist/lib/integrations/node-express/index.js.map +1 -1
  23. package/dist/lib/integrations/node-express/index.mjs +3 -3
  24. package/dist/lib/integrations/node-http/index.js +2 -2
  25. package/dist/lib/integrations/node-http/index.js.map +1 -1
  26. package/dist/lib/integrations/node-http/index.mjs +2 -2
  27. package/dist/service-adapters/index.js +28 -27
  28. package/dist/service-adapters/index.js.map +1 -1
  29. package/dist/service-adapters/index.mjs +1 -1
  30. package/package.json +3 -3
  31. package/src/lib/runtime/__tests__/remote-action-constructors.test.ts +236 -0
  32. package/src/lib/runtime/remote-action-constructors.ts +7 -6
  33. package/src/lib/runtime/remote-lg-action.ts +2 -6
  34. package/src/service-adapters/conversion.ts +3 -2
  35. package/src/service-adapters/openai/utils.ts +2 -1
  36. package/tsconfig.json +3 -2
  37. package/dist/chunk-J6E3ZTJ3.mjs.map +0 -1
  38. package/dist/chunk-OS5YD32G.mjs.map +0 -1
  39. /package/dist/{chunk-ZM4JOETB.mjs.map → chunk-4FCUC27H.mjs.map} +0 -0
  40. /package/dist/{chunk-25Z2ZUVM.mjs.map → chunk-ROFUPT7E.mjs.map} +0 -0
  41. /package/dist/{chunk-7HPGWUP7.mjs.map → chunk-Z5VUD7NL.mjs.map} +0 -0
@@ -0,0 +1,236 @@
1
+ import { TextEncoder } from "util";
2
+ import { RemoteLangGraphEventSource } from "../../../agents/langgraph/event-source";
3
+ import telemetry from "../../telemetry-client";
4
+ import {
5
+ constructLGCRemoteAction,
6
+ constructRemoteActions,
7
+ createHeaders,
8
+ } from "../remote-action-constructors";
9
+ import { execute } from "../remote-lg-action";
10
+
11
+ // Mock external dependencies
12
+ jest.mock("../remote-lg-action", () => ({
13
+ execute: jest.fn(),
14
+ }));
15
+
16
+ jest.mock("../../telemetry-client", () => ({
17
+ capture: jest.fn(),
18
+ }));
19
+
20
+ jest.mock("../../../agents/langgraph/event-source", () => ({
21
+ RemoteLangGraphEventSource: jest.fn(),
22
+ }));
23
+
24
+ // Dummy logger
25
+ const logger = {
26
+ debug: jest.fn(),
27
+ error: jest.fn(),
28
+ child: jest.fn(() => logger),
29
+ };
30
+
31
+ // Dummy graphqlContext
32
+ const graphqlContext = { properties: { dummyProp: "value" } } as any;
33
+
34
+ // Dummy agent state
35
+ const agentStates = [{ agentName: "agent1", state: "{}", configurable: "{}" }];
36
+
37
+ // Dummy agent used in constructLGCRemoteAction
38
+ const dummyAgent = { name: "agent1", description: "test agent" };
39
+ const endpoint = {
40
+ agents: [dummyAgent],
41
+ deploymentUrl: "http://dummy.deployment",
42
+ langsmithApiKey: "dummykey",
43
+ };
44
+
45
+ // Clear mocks before each test
46
+ beforeEach(() => {
47
+ jest.clearAllMocks();
48
+ });
49
+
50
+ describe("remote action constructors", () => {
51
+ describe("constructLGCRemoteAction", () => {
52
+ it("should create an agent with langGraphAgentHandler that processes events", async () => {
53
+ // Arrange: simulate execute returning a dummy ReadableStream
54
+ const dummyEncodedEvent = new TextEncoder().encode(JSON.stringify({ event: "test" }) + "\n");
55
+ const readerMock = {
56
+ read: jest
57
+ .fn()
58
+ .mockResolvedValueOnce({ done: false, value: dummyEncodedEvent })
59
+ .mockResolvedValueOnce({ done: true, value: new Uint8Array() }),
60
+ };
61
+
62
+ const dummyResponse = {
63
+ getReader: () => readerMock,
64
+ };
65
+
66
+ (execute as jest.Mock).mockResolvedValue(dummyResponse);
67
+
68
+ // Mock RemoteLangGraphEventSource to return a dummy processed result
69
+ const processLangGraphEventsMock = jest.fn(() => "processed events");
70
+ (RemoteLangGraphEventSource as jest.Mock).mockImplementation(() => ({
71
+ eventStream$: { next: jest.fn(), complete: jest.fn(), error: jest.fn() },
72
+ processLangGraphEvents: processLangGraphEventsMock,
73
+ }));
74
+
75
+ // Act: build the action and call langGraphAgentHandler
76
+ const actions = constructLGCRemoteAction({
77
+ endpoint,
78
+ graphqlContext,
79
+ logger,
80
+ messages: [],
81
+ agentStates,
82
+ });
83
+ expect(actions).toHaveLength(1);
84
+ const action = actions[0];
85
+ expect(action.name).toEqual(dummyAgent.name);
86
+
87
+ const result = await action.langGraphAgentHandler({
88
+ name: dummyAgent.name,
89
+ actionInputsWithoutAgents: [],
90
+ threadId: "thread1",
91
+ nodeName: "node1",
92
+ additionalMessages: [],
93
+ metaEvents: [],
94
+ });
95
+
96
+ // Assert: processLangGraphEvents is called and result returned
97
+ expect(processLangGraphEventsMock).toHaveBeenCalled();
98
+ expect(result).toBe("processed events");
99
+
100
+ // Check telemetry.capture was called with agentExecution true
101
+ expect(telemetry.capture).toHaveBeenCalledWith(
102
+ "oss.runtime.remote_action_executed",
103
+ expect.objectContaining({
104
+ agentExecution: true,
105
+ type: "langgraph-platform",
106
+ agentsAmount: 1,
107
+ }),
108
+ );
109
+ });
110
+ });
111
+
112
+ describe("constructRemoteActions", () => {
113
+ const json = {
114
+ agents: [{ name: "agent2", description: "agent desc" }],
115
+ actions: [
116
+ {
117
+ name: "action1",
118
+ description: "action desc",
119
+ parameters: { param: "value" },
120
+ },
121
+ ],
122
+ };
123
+ const url = "http://dummy.api";
124
+ const onBeforeRequest = jest.fn(() => ({ headers: { Authorization: "Bearer token" } }));
125
+
126
+ it("should create remote action handler that calls fetch and returns the result", async () => {
127
+ // Arrange: mock fetch for action handler
128
+ global.fetch = jest.fn().mockResolvedValue({
129
+ ok: true,
130
+ json: jest.fn().mockResolvedValue({ result: "action result" }),
131
+ });
132
+
133
+ const actionsArray = constructRemoteActions({
134
+ json,
135
+ url,
136
+ onBeforeRequest,
137
+ graphqlContext,
138
+ logger,
139
+ messages: [],
140
+ agentStates,
141
+ });
142
+ // There should be one action (from json.actions) and one agent (from json.agents)
143
+ expect(actionsArray).toHaveLength(2);
144
+ const actionHandler = actionsArray[0].handler;
145
+
146
+ const result = await actionHandler({ foo: "bar" });
147
+ expect(result).toEqual("action result");
148
+
149
+ expect(global.fetch).toHaveBeenCalledWith(
150
+ `${url}/actions/execute`,
151
+ expect.objectContaining({
152
+ method: "POST",
153
+ headers: expect.objectContaining({
154
+ "Content-Type": "application/json",
155
+ Authorization: "Bearer token",
156
+ }),
157
+ body: expect.any(String),
158
+ }),
159
+ );
160
+ });
161
+
162
+ it("should create remote agent handler that processes events", async () => {
163
+ // Arrange: mock fetch for agent handler to return a dummy stream
164
+ const dummyEncodedAgentEvent = new TextEncoder().encode('{"event":"data"}\n');
165
+ const agentReaderMock = {
166
+ read: jest
167
+ .fn()
168
+ .mockResolvedValueOnce({ done: false, value: dummyEncodedAgentEvent })
169
+ .mockResolvedValueOnce({ done: true, value: new Uint8Array() }),
170
+ };
171
+ const dummyStreamResponse = {
172
+ getReader: () => agentReaderMock,
173
+ };
174
+ global.fetch = jest.fn().mockResolvedValue({
175
+ ok: true,
176
+ text: jest.fn().mockResolvedValue("ok"),
177
+ body: dummyStreamResponse,
178
+ });
179
+
180
+ const processLangGraphEventsMock = jest.fn(() => "agent events processed");
181
+ (RemoteLangGraphEventSource as jest.Mock).mockImplementation(() => ({
182
+ eventStream$: { next: jest.fn(), complete: jest.fn(), error: jest.fn() },
183
+ processLangGraphEvents: processLangGraphEventsMock,
184
+ }));
185
+
186
+ const actionsArray = constructRemoteActions({
187
+ json,
188
+ url,
189
+ onBeforeRequest,
190
+ graphqlContext,
191
+ logger,
192
+ messages: [],
193
+ agentStates,
194
+ });
195
+ // The remote agent is the second item in the array
196
+ expect(actionsArray).toHaveLength(2);
197
+ const remoteAgentHandler = (actionsArray[1] as any).langGraphAgentHandler;
198
+ const result = await remoteAgentHandler({
199
+ name: "agent2",
200
+ actionInputsWithoutAgents: [],
201
+ threadId: "thread2",
202
+ nodeName: "node2",
203
+ additionalMessages: [],
204
+ metaEvents: [],
205
+ });
206
+ expect(processLangGraphEventsMock).toHaveBeenCalled();
207
+ expect(result).toBe("agent events processed");
208
+
209
+ // Check telemetry.capture for agent execution
210
+ expect(telemetry.capture).toHaveBeenCalledWith(
211
+ "oss.runtime.remote_action_executed",
212
+ expect.objectContaining({
213
+ agentExecution: true,
214
+ type: "self-hosted",
215
+ agentsAmount: 1,
216
+ }),
217
+ );
218
+ });
219
+ });
220
+
221
+ describe("createHeaders", () => {
222
+ it("should merge headers from onBeforeRequest", () => {
223
+ const onBeforeRequest = jest.fn(() => ({ headers: { "X-Test": "123" } }));
224
+ const headers = createHeaders(onBeforeRequest, graphqlContext);
225
+ expect(headers).toEqual({
226
+ "Content-Type": "application/json",
227
+ "X-Test": "123",
228
+ });
229
+ });
230
+
231
+ it("should return only Content-Type if no additional headers", () => {
232
+ const headers = createHeaders(undefined, graphqlContext);
233
+ expect(headers).toEqual({ "Content-Type": "application/json" });
234
+ });
235
+ });
236
+ });
@@ -18,6 +18,7 @@ import { LangGraphEvent } from "../../agents/langgraph/events";
18
18
  import { execute } from "./remote-lg-action";
19
19
  import { CopilotKitError, CopilotKitLowLevelError } from "@copilotkit/shared";
20
20
  import { CopilotKitApiDiscoveryError, ResolvedCopilotKitError } from "@copilotkit/shared";
21
+ import { parseJson } from "@copilotkit/shared";
21
22
 
22
23
  export function constructLGCRemoteAction({
23
24
  endpoint,
@@ -59,8 +60,8 @@ export function constructLGCRemoteAction({
59
60
  if (agentStates) {
60
61
  const jsonState = agentStates.find((state) => state.agentName === name);
61
62
  if (jsonState) {
62
- state = JSON.parse(jsonState.state);
63
- configurable = JSON.parse(jsonState.configurable);
63
+ state = parseJson(jsonState.state, {});
64
+ configurable = parseJson(jsonState.configurable, {});
64
65
  }
65
66
  }
66
67
 
@@ -79,7 +80,7 @@ export function constructLGCRemoteAction({
79
80
  actions: actionInputsWithoutAgents.map((action) => ({
80
81
  name: action.name,
81
82
  description: action.description,
82
- parameters: JSON.parse(action.jsonSchema) as string,
83
+ parameters: parseJson(action.jsonSchema, "") as string,
83
84
  })),
84
85
  metaEvents,
85
86
  });
@@ -203,8 +204,8 @@ export function constructRemoteActions({
203
204
  if (agentStates) {
204
205
  const jsonState = agentStates.find((state) => state.agentName === name);
205
206
  if (jsonState) {
206
- state = JSON.parse(jsonState.state);
207
- configurable = JSON.parse(jsonState.configurable);
207
+ state = parseJson(jsonState.state, {});
208
+ configurable = parseJson(jsonState.configurable, {});
208
209
  }
209
210
  }
210
211
 
@@ -224,7 +225,7 @@ export function constructRemoteActions({
224
225
  actions: actionInputsWithoutAgents.map((action) => ({
225
226
  name: action.name,
226
227
  description: action.description,
227
- parameters: JSON.parse(action.jsonSchema),
228
+ parameters: parseJson(action.jsonSchema, {}),
228
229
  })),
229
230
  metaEvents,
230
231
  }),
@@ -13,6 +13,7 @@ import telemetry from "../telemetry-client";
13
13
  import { MetaEventInput } from "../../graphql/inputs/meta-event.input";
14
14
  import { MetaEventName } from "../../graphql/types/meta-events.type";
15
15
  import { RunsStreamPayload } from "@langchain/langgraph-sdk/dist/types";
16
+ import { parseJson } from "@copilotkit/shared";
16
17
 
17
18
  type State = Record<string, any>;
18
19
 
@@ -164,12 +165,7 @@ async function streamEvents(controller: ReadableStreamDefaultController, args: E
164
165
  }
165
166
  if (lgInterruptMetaEvent?.response) {
166
167
  let response = lgInterruptMetaEvent.response;
167
- try {
168
- payload.command = { resume: JSON.parse(response) };
169
- // In case of unparsable string, we keep the event as is
170
- } catch (e) {
171
- payload.command = { resume: response };
172
- }
168
+ payload.command = { resume: parseJson(response, response) };
173
169
  }
174
170
 
175
171
  if (mode === "continue" && !activeInterruptEvent) {
@@ -7,6 +7,7 @@ import {
7
7
  } from "../graphql/types/converted";
8
8
  import { MessageInput } from "../graphql/inputs/message.input";
9
9
  import { plainToInstance } from "class-transformer";
10
+ import { parseJson } from "@copilotkit/shared";
10
11
 
11
12
  export function convertGqlInputToMessages(inputMessages: MessageInput[]): Message[] {
12
13
  const messages: Message[] = [];
@@ -28,7 +29,7 @@ export function convertGqlInputToMessages(inputMessages: MessageInput[]): Messag
28
29
  id: message.id,
29
30
  createdAt: message.createdAt,
30
31
  name: message.actionExecutionMessage.name,
31
- arguments: JSON.parse(message.actionExecutionMessage.arguments),
32
+ arguments: parseJson(message.actionExecutionMessage.arguments, {}),
32
33
  parentMessageId: message.actionExecutionMessage.parentMessageId,
33
34
  }),
34
35
  );
@@ -53,7 +54,7 @@ export function convertGqlInputToMessages(inputMessages: MessageInput[]): Messag
53
54
  runId: message.agentStateMessage.runId,
54
55
  active: message.agentStateMessage.active,
55
56
  role: message.agentStateMessage.role,
56
- state: JSON.parse(message.agentStateMessage.state),
57
+ state: parseJson(message.agentStateMessage.state, {}),
57
58
  running: message.agentStateMessage.running,
58
59
  }),
59
60
  );
@@ -7,6 +7,7 @@ import {
7
7
  ChatCompletionAssistantMessageParam,
8
8
  ChatCompletionSystemMessageParam,
9
9
  } from "openai/resources";
10
+ import { parseJson } from "@copilotkit/shared";
10
11
 
11
12
  export function limitMessagesToTokenCount(
12
13
  messages: any[],
@@ -113,7 +114,7 @@ export function convertActionInputToOpenAITool(action: ActionInput): ChatComplet
113
114
  function: {
114
115
  name: action.name,
115
116
  description: action.description,
116
- parameters: JSON.parse(action.jsonSchema),
117
+ parameters: parseJson(action.jsonSchema, {}),
117
118
  },
118
119
  };
119
120
  }
package/tsconfig.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "extends": "../../utilities/tsconfig/base.json",
3
3
  "compilerOptions": {
4
+ "types": ["node", "jest"],
4
5
  "lib": ["es2017", "dom"],
5
6
  "emitDecoratorMetadata": true,
6
7
  "experimentalDecorators": true,
7
8
  "strict": false,
8
9
  "resolveJsonModule": true
9
10
  },
10
- "include": ["./src/**/*.ts"],
11
- "exclude": ["dist", "build", "node_modules", "**/*.test.ts", "**/*.test.tsx", "**/__tests__/*"]
11
+ "include": ["./src/**/*.ts", "./src/**/*.test.ts", "./src/**/__tests__/*"],
12
+ "exclude": ["dist", "build", "node_modules"]
12
13
  }