@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.
Files changed (56) hide show
  1. package/CHANGELOG.md +3 -4
  2. package/dist/{chunk-SIMJ6TZU.mjs → chunk-6B3NPPSR.mjs} +2 -2
  3. package/dist/{chunk-OSJXQNII.mjs → chunk-6HXQC7IT.mjs} +36 -9
  4. package/dist/chunk-6HXQC7IT.mjs.map +1 -0
  5. package/dist/{chunk-24WEOOFX.mjs → chunk-7MQDBRXJ.mjs} +2 -2
  6. package/dist/{chunk-AOYYOKTP.mjs → chunk-E6ZFCM3B.mjs} +561 -133
  7. package/dist/chunk-E6ZFCM3B.mjs.map +1 -0
  8. package/dist/{chunk-UYORVPCQ.mjs → chunk-TM7ZRU3M.mjs} +2 -2
  9. package/dist/{chunk-ZEHCLFJ2.mjs → chunk-V7SK6QZN.mjs} +6 -9
  10. package/dist/chunk-V7SK6QZN.mjs.map +1 -0
  11. package/dist/{chunk-HKLL7TTP.mjs → chunk-XMDH5MKI.mjs} +2 -2
  12. package/dist/{copilot-runtime-df3527ad.d.ts → copilot-runtime-aba7d4b4.d.ts} +27 -5
  13. package/dist/index.d.ts +1 -1
  14. package/dist/index.js +638 -186
  15. package/dist/index.js.map +1 -1
  16. package/dist/index.mjs +13 -9
  17. package/dist/index.mjs.map +1 -1
  18. package/dist/lib/index.d.ts +1 -1
  19. package/dist/lib/index.js +638 -186
  20. package/dist/lib/index.js.map +1 -1
  21. package/dist/lib/index.mjs +13 -9
  22. package/dist/lib/integrations/index.d.ts +2 -2
  23. package/dist/lib/integrations/index.js +5 -4
  24. package/dist/lib/integrations/index.js.map +1 -1
  25. package/dist/lib/integrations/index.mjs +5 -5
  26. package/dist/lib/integrations/nest/index.d.ts +1 -1
  27. package/dist/lib/integrations/nest/index.js +5 -4
  28. package/dist/lib/integrations/nest/index.js.map +1 -1
  29. package/dist/lib/integrations/nest/index.mjs +3 -3
  30. package/dist/lib/integrations/node-express/index.d.ts +1 -1
  31. package/dist/lib/integrations/node-express/index.js +5 -4
  32. package/dist/lib/integrations/node-express/index.js.map +1 -1
  33. package/dist/lib/integrations/node-express/index.mjs +3 -3
  34. package/dist/lib/integrations/node-http/index.d.ts +1 -1
  35. package/dist/lib/integrations/node-http/index.js +5 -4
  36. package/dist/lib/integrations/node-http/index.js.map +1 -1
  37. package/dist/lib/integrations/node-http/index.mjs +2 -2
  38. package/dist/service-adapters/index.js +5 -8
  39. package/dist/service-adapters/index.js.map +1 -1
  40. package/dist/service-adapters/index.mjs +2 -2
  41. package/package.json +7 -6
  42. package/src/agents/langgraph/event-source.ts +22 -67
  43. package/src/lib/runtime/copilot-runtime.ts +58 -11
  44. package/src/lib/runtime/remote-action-constructors.ts +283 -0
  45. package/src/lib/runtime/remote-actions.ts +65 -159
  46. package/src/lib/runtime/remote-lg-cloud-action.ts +441 -0
  47. package/src/service-adapters/events.ts +1 -5
  48. package/src/service-adapters/langchain/utils.ts +5 -10
  49. package/dist/chunk-AOYYOKTP.mjs.map +0 -1
  50. package/dist/chunk-OSJXQNII.mjs.map +0 -1
  51. package/dist/chunk-ZEHCLFJ2.mjs.map +0 -1
  52. package/src/service-adapters/langchain/utils.test.ts +0 -169
  53. /package/dist/{chunk-SIMJ6TZU.mjs.map → chunk-6B3NPPSR.mjs.map} +0 -0
  54. /package/dist/{chunk-24WEOOFX.mjs.map → chunk-7MQDBRXJ.mjs.map} +0 -0
  55. /package/dist/{chunk-UYORVPCQ.mjs.map → chunk-TM7ZRU3M.mjs.map} +0 -0
  56. /package/dist/{chunk-HKLL7TTP.mjs.map → chunk-XMDH5MKI.mjs.map} +0 -0
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  streamLangChainResponse
3
- } from "./chunk-ZEHCLFJ2.mjs";
3
+ } from "./chunk-V7SK6QZN.mjs";
4
4
  import {
5
5
  GuardrailsValidationFailureResponse,
6
6
  MessageStreamInterruptedResponse,
@@ -36,7 +36,7 @@ var require_package = __commonJS({
36
36
  publishConfig: {
37
37
  access: "public"
38
38
  },
39
- version: "1.3.12-fix-tool-call-dynamic-parameters.0",
39
+ version: "1.3.12-lgc-alpha-1.0",
40
40
  sideEffects: false,
41
41
  main: "./dist/index.js",
42
42
  module: "./dist/index.mjs",
@@ -69,16 +69,16 @@ var require_package = __commonJS({
69
69
  "ts-node": "^10.9.2",
70
70
  tsconfig: "workspace:*",
71
71
  tsup: "^6.7.0",
72
- typescript: "^5.2.3",
73
- "zod-to-json-schema": "^3.23.5"
72
+ typescript: "^5.2.3"
74
73
  },
75
74
  dependencies: {
76
75
  "@anthropic-ai/sdk": "^0.27.3",
77
76
  "@copilotkit/shared": "workspace:*",
77
+ "@langchain/google-gauth": "^0.1.0",
78
78
  "@graphql-yoga/plugin-defer-stream": "^3.3.1",
79
79
  "@langchain/community": "^0.0.53",
80
80
  "@langchain/core": "^0.3.13",
81
- "@langchain/google-gauth": "^0.1.0",
81
+ "@langchain/langgraph-sdk": "^0.0.16",
82
82
  "@langchain/openai": "^0.0.28",
83
83
  "class-transformer": "^0.5.1",
84
84
  express: "^4.19.2",
@@ -88,6 +88,7 @@ var require_package = __commonJS({
88
88
  "groq-sdk": "^0.5.0",
89
89
  langchain: "^0.3.3",
90
90
  openai: "^4.50.0",
91
+ "partial-json": "^0.1.7",
91
92
  pino: "^9.2.0",
92
93
  "pino-pretty": "^11.2.1",
93
94
  "reflect-metadata": "^0.2.2",
@@ -1054,50 +1055,6 @@ var LangGraphEventTypes;
1054
1055
  import { randomId } from "@copilotkit/shared";
1055
1056
  var RemoteLangGraphEventSource = class {
1056
1057
  eventStream$ = new ReplaySubject();
1057
- async streamResponse(response) {
1058
- const reader = response.body.getReader();
1059
- const decoder = new TextDecoder();
1060
- let buffer = [];
1061
- const eventStream$ = this.eventStream$;
1062
- function flushBuffer() {
1063
- const currentBuffer = buffer.join("");
1064
- if (currentBuffer.trim().length === 0) {
1065
- return;
1066
- }
1067
- const parts = currentBuffer.split("\n");
1068
- if (parts.length === 0) {
1069
- return;
1070
- }
1071
- const lastPartIsComplete = currentBuffer.endsWith("\n");
1072
- buffer = [];
1073
- if (!lastPartIsComplete) {
1074
- buffer.push(parts.pop());
1075
- }
1076
- parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
1077
- eventStream$.next(JSON.parse(part));
1078
- });
1079
- }
1080
- __name(flushBuffer, "flushBuffer");
1081
- try {
1082
- while (true) {
1083
- const { done, value } = await reader.read();
1084
- if (!done) {
1085
- buffer.push(decoder.decode(value, {
1086
- stream: true
1087
- }));
1088
- }
1089
- flushBuffer();
1090
- if (done) {
1091
- break;
1092
- }
1093
- }
1094
- } catch (error) {
1095
- console.error("Error in stream", error);
1096
- eventStream$.error(error);
1097
- return;
1098
- }
1099
- eventStream$.complete();
1100
- }
1101
1058
  shouldEmitToolCall(shouldEmitToolCalls, toolCallName) {
1102
1059
  if (typeof shouldEmitToolCalls === "boolean") {
1103
1060
  return shouldEmitToolCalls;
@@ -1120,20 +1077,25 @@ var RemoteLangGraphEventSource = class {
1120
1077
  } else {
1121
1078
  acc.content = null;
1122
1079
  }
1123
- if ((_h = (_g = (_f = event.data) == null ? void 0 : _f.chunk) == null ? void 0 : _g.kwargs) == null ? void 0 : _h.tool_call_chunks) {
1080
+ const toolCallChunks = (
1081
+ // @ts-expect-error -- LangGraph Cloud implementation stores data outside of kwargs
1082
+ ((_h = (_g = (_f = event.data) == null ? void 0 : _f.chunk) == null ? void 0 : _g.kwargs) == null ? void 0 : _h.tool_call_chunks) ?? ((_j = (_i = event.data) == null ? void 0 : _i.chunk) == null ? void 0 : _j.tool_call_chunks)
1083
+ );
1084
+ const toolCallMessageId = ((_m = (_l = (_k = event.data) == null ? void 0 : _k.chunk) == null ? void 0 : _l.kwargs) == null ? void 0 : _m.id) ?? ((_o = (_n = event.data) == null ? void 0 : _n.chunk) == null ? void 0 : _o.id);
1085
+ if (toolCallChunks && toolCallChunks.length > 0) {
1124
1086
  acc.prevToolCallMessageId = acc.toolCallMessageId;
1125
- acc.toolCallMessageId = (_i = event.data.chunk.kwargs) == null ? void 0 : _i.id;
1126
- if ((_j = event.data.chunk.kwargs.tool_call_chunks[0]) == null ? void 0 : _j.name) {
1127
- acc.toolCallName = event.data.chunk.kwargs.tool_call_chunks[0].name;
1087
+ acc.toolCallMessageId = toolCallMessageId;
1088
+ if ((_p = toolCallChunks[0]) == null ? void 0 : _p.name) {
1089
+ acc.toolCallName = toolCallChunks[0].name;
1128
1090
  }
1129
- if ((_k = event.data.chunk.kwargs.tool_call_chunks[0]) == null ? void 0 : _k.id) {
1130
- acc.toolCallId = event.data.chunk.kwargs.tool_call_chunks[0].id;
1091
+ if ((_q = toolCallChunks[0]) == null ? void 0 : _q.id) {
1092
+ acc.toolCallId = toolCallChunks[0].id;
1131
1093
  }
1132
1094
  acc.prevMessageId = acc.messageId;
1133
- acc.messageId = (_n = (_m = (_l = event.data) == null ? void 0 : _l.chunk) == null ? void 0 : _m.kwargs) == null ? void 0 : _n.id;
1095
+ acc.messageId = toolCallMessageId;
1134
1096
  } else if (acc.content && acc.content != "") {
1135
1097
  acc.prevMessageId = acc.messageId;
1136
- acc.messageId = (_q = (_p = (_o = event.data) == null ? void 0 : _o.chunk) == null ? void 0 : _p.kwargs) == null ? void 0 : _q.id;
1098
+ acc.messageId = toolCallMessageId;
1137
1099
  } else {
1138
1100
  acc.prevToolCallMessageId = acc.toolCallMessageId;
1139
1101
  acc.prevMessageId = acc.messageId;
@@ -1158,7 +1120,7 @@ var RemoteLangGraphEventSource = class {
1158
1120
  prevMessageId: null,
1159
1121
  content: null
1160
1122
  }), mergeMap((eventWithState) => {
1161
- var _a, _b, _c, _d, _e;
1123
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1162
1124
  const events = [];
1163
1125
  let shouldEmitMessages = true;
1164
1126
  let shouldEmitToolCalls = false;
@@ -1241,7 +1203,8 @@ var RemoteLangGraphEventSource = class {
1241
1203
  });
1242
1204
  }
1243
1205
  }
1244
- const args = (_e = (_d = (_c = (_b = (_a = eventWithState.event.data) == null ? void 0 : _a.chunk) == null ? void 0 : _b.kwargs) == null ? void 0 : _c.tool_call_chunks) == null ? void 0 : _d[0]) == null ? void 0 : _e.args;
1206
+ const args = ((_e = (_d = (_c = (_b = (_a = eventWithState.event.data) == null ? void 0 : _a.chunk) == null ? void 0 : _b.kwargs) == null ? void 0 : _c.tool_call_chunks) == null ? void 0 : _d[0]) == null ? void 0 : _e.args) ?? // @ts-expect-error -- sdf
1207
+ ((_i = (_h = (_g = (_f = eventWithState.event.data) == null ? void 0 : _f.chunk) == null ? void 0 : _g.tool_call_chunks) == null ? void 0 : _h[0]) == null ? void 0 : _i.args);
1245
1208
  const content = eventWithState.content;
1246
1209
  if (args) {
1247
1210
  if (this.shouldEmitToolCall(shouldEmitToolCalls, eventWithState.toolCallName)) {
@@ -1292,70 +1255,409 @@ var RemoteLangGraphEventSource = class {
1292
1255
  };
1293
1256
  __name(RemoteLangGraphEventSource, "RemoteLangGraphEventSource");
1294
1257
 
1295
- // src/lib/runtime/remote-actions.ts
1296
- function isLangGraphAgentAction(action) {
1297
- if (!action) {
1298
- return false;
1299
- }
1300
- return typeof action.langGraphAgentHandler === "function";
1258
+ // src/lib/runtime/remote-lg-cloud-action.ts
1259
+ import { Client } from "@langchain/langgraph-sdk";
1260
+ import { randomUUID } from "crypto";
1261
+ import { parse as parsePartialJson } from "partial-json";
1262
+ async function execute(args) {
1263
+ return new ReadableStream({
1264
+ async start(controller) {
1265
+ try {
1266
+ await streamEvents(controller, args);
1267
+ controller.close();
1268
+ } catch (err) {
1269
+ }
1270
+ }
1271
+ });
1301
1272
  }
1302
- __name(isLangGraphAgentAction, "isLangGraphAgentAction");
1303
- function createHeaders(onBeforeRequest, graphqlContext) {
1304
- const headers = {
1305
- "Content-Type": "application/json"
1273
+ __name(execute, "execute");
1274
+ async function streamEvents(controller, args) {
1275
+ const { deploymentUrl, langsmithApiKey, threadId: agrsInitialThreadId, agent, nodeName: initialNodeName, state: initialState, messages, actions } = args;
1276
+ let nodeName = initialNodeName;
1277
+ let state = initialState;
1278
+ const { name, assistantId: initialAssistantId } = agent;
1279
+ const client = new Client({
1280
+ apiUrl: deploymentUrl,
1281
+ apiKey: langsmithApiKey
1282
+ });
1283
+ let initialThreadId = agrsInitialThreadId;
1284
+ const wasInitiatedWithExistingThread = !!initialThreadId;
1285
+ if (initialThreadId && initialThreadId.startsWith("ck-")) {
1286
+ initialThreadId = initialThreadId.substring(3);
1287
+ }
1288
+ const assistants = await client.assistants.search();
1289
+ const retrievedAssistant = assistants.find((a) => a.name === name);
1290
+ const threadId = initialThreadId ?? randomUUID();
1291
+ if (initialThreadId === threadId) {
1292
+ await client.threads.get(threadId);
1293
+ } else {
1294
+ await client.threads.create({
1295
+ threadId
1296
+ });
1297
+ }
1298
+ let agentState = {
1299
+ values: {}
1306
1300
  };
1307
- if (onBeforeRequest) {
1308
- const { headers: additionalHeaders } = onBeforeRequest({
1309
- ctx: graphqlContext
1301
+ if (wasInitiatedWithExistingThread) {
1302
+ agentState = await client.threads.getState(threadId);
1303
+ }
1304
+ const agentStateValues = agentState.values;
1305
+ state.messages = agentStateValues.messages;
1306
+ const mode = wasInitiatedWithExistingThread && nodeName != "__end__" ? "continue" : "start";
1307
+ state = langGraphDefaultMergeState(state, formatMessages(messages), actions);
1308
+ if (mode === "continue") {
1309
+ await client.threads.updateState(threadId, {
1310
+ values: state,
1311
+ asNode: nodeName
1310
1312
  });
1311
- if (additionalHeaders) {
1312
- Object.assign(headers, additionalHeaders);
1313
+ }
1314
+ const assistantId = initialAssistantId ?? retrievedAssistant.assistant_id;
1315
+ const graphInfo = await client.assistants.getGraph(assistantId);
1316
+ const streamInput = mode === "start" ? state : null;
1317
+ let streamingStateExtractor = new StreamingStateExtractor([]);
1318
+ let prevNodeName = null;
1319
+ let emitIntermediateStateUntilEnd = null;
1320
+ let shouldExit = null;
1321
+ let externalRunId = null;
1322
+ const streamResponse2 = client.runs.stream(threadId, assistantId, {
1323
+ input: streamInput,
1324
+ streamMode: [
1325
+ "events",
1326
+ "values"
1327
+ ]
1328
+ });
1329
+ const emit = /* @__PURE__ */ __name((message) => controller.enqueue(new TextEncoder().encode(message)), "emit");
1330
+ let latestStateValues = {};
1331
+ for await (const chunk of streamResponse2) {
1332
+ if (![
1333
+ "events",
1334
+ "values"
1335
+ ].includes(chunk.event))
1336
+ continue;
1337
+ if (chunk.event === "values") {
1338
+ latestStateValues = chunk.data;
1339
+ continue;
1340
+ }
1341
+ const event = chunk.data;
1342
+ const currentNodeName = event.name;
1343
+ const eventType = event.event;
1344
+ const runId = event.metadata.run_id;
1345
+ externalRunId = runId;
1346
+ const metadata = event.metadata;
1347
+ shouldExit = shouldExit != null ? shouldExit : metadata["copilotkit:exit"];
1348
+ const emitIntermediateState = metadata["copilotkit:emit-intermediate-state"];
1349
+ const forceEmitIntermediateState = metadata["copilotkit:force-emit-intermediate-state"];
1350
+ const manuallyEmitMessage = metadata["copilotkit:manually-emit-messages"];
1351
+ const manuallyEmitToolCall = metadata["copilotkit:manually-emit-tool-calls"];
1352
+ if (graphInfo["nodes"].some((node) => node.id === currentNodeName)) {
1353
+ nodeName = currentNodeName;
1354
+ }
1355
+ if (!nodeName) {
1356
+ continue;
1357
+ }
1358
+ if (forceEmitIntermediateState) {
1359
+ if (eventType === "on_chain_end") {
1360
+ state = event.data.output;
1361
+ emit(getStateSyncEvent({
1362
+ threadId,
1363
+ runId,
1364
+ agentName: agent.name,
1365
+ nodeName,
1366
+ state: event.data.output,
1367
+ running: true,
1368
+ active: true
1369
+ }));
1370
+ }
1371
+ continue;
1372
+ }
1373
+ if (manuallyEmitMessage) {
1374
+ if (eventType === "on_chain_end") {
1375
+ state = event.data.output;
1376
+ emit(JSON.stringify({
1377
+ event: "on_copilotkit_emit_message",
1378
+ message: event.data.output,
1379
+ messageId: randomUUID(),
1380
+ role: MessageRole.assistant
1381
+ }) + "\n");
1382
+ }
1383
+ continue;
1313
1384
  }
1385
+ if (manuallyEmitToolCall) {
1386
+ if (eventType === "on_chain_end") {
1387
+ state = event.data.output;
1388
+ emit(JSON.stringify({
1389
+ event: "on_copilotkit_emit_tool_call",
1390
+ name: event.data.output.name,
1391
+ args: event.data.output.args,
1392
+ id: event.data.output.id
1393
+ }) + "\n");
1394
+ }
1395
+ continue;
1396
+ }
1397
+ if (emitIntermediateState && emitIntermediateStateUntilEnd == null) {
1398
+ emitIntermediateStateUntilEnd = nodeName;
1399
+ }
1400
+ if (emitIntermediateState && eventType === "on_chat_model_start") {
1401
+ streamingStateExtractor = new StreamingStateExtractor(emitIntermediateState);
1402
+ }
1403
+ let updatedState = latestStateValues;
1404
+ if (emitIntermediateState && eventType === "on_chat_model_stream") {
1405
+ streamingStateExtractor.bufferToolCalls(event);
1406
+ }
1407
+ if (emitIntermediateStateUntilEnd !== null) {
1408
+ updatedState = {
1409
+ ...updatedState,
1410
+ ...streamingStateExtractor.extractState()
1411
+ };
1412
+ }
1413
+ if (!emitIntermediateState && currentNodeName === emitIntermediateStateUntilEnd && eventType === "on_chain_end") {
1414
+ emitIntermediateStateUntilEnd = null;
1415
+ }
1416
+ const exitingNode = nodeName === currentNodeName && eventType === "on_chain_end";
1417
+ if (JSON.stringify(updatedState) !== JSON.stringify(state) || prevNodeName != nodeName || exitingNode) {
1418
+ state = updatedState;
1419
+ prevNodeName = nodeName;
1420
+ emit(getStateSyncEvent({
1421
+ threadId,
1422
+ runId,
1423
+ agentName: agent.name,
1424
+ nodeName,
1425
+ state,
1426
+ running: true,
1427
+ active: !exitingNode
1428
+ }));
1429
+ }
1430
+ emit(JSON.stringify(event) + "\n");
1314
1431
  }
1315
- return headers;
1432
+ state = await client.threads.getState(threadId);
1433
+ const isEndNode = state.next.length === 0;
1434
+ nodeName = Object.keys(state.metadata.writes)[0];
1435
+ emit(getStateSyncEvent({
1436
+ threadId,
1437
+ runId: externalRunId,
1438
+ agentName: agent.name,
1439
+ nodeName: isEndNode ? "__end__" : nodeName,
1440
+ state: state.values,
1441
+ running: !shouldExit,
1442
+ active: false
1443
+ }));
1444
+ return Promise.resolve();
1316
1445
  }
1317
- __name(createHeaders, "createHeaders");
1318
- async function fetchRemoteInfo({ url, onBeforeRequest, graphqlContext, logger: logger2, frontendUrl }) {
1319
- logger2.debug({
1320
- url
1321
- }, "Fetching actions from url");
1322
- const headers = createHeaders(onBeforeRequest, graphqlContext);
1323
- try {
1324
- const response = await fetch(`${url}/info`, {
1325
- method: "POST",
1326
- headers,
1327
- body: JSON.stringify({
1328
- properties: graphqlContext.properties,
1329
- frontendUrl
1330
- })
1331
- });
1332
- if (!response.ok) {
1333
- logger2.error({
1334
- url,
1335
- status: response.status,
1336
- body: await response.text()
1337
- }, "Failed to fetch actions from url");
1446
+ __name(streamEvents, "streamEvents");
1447
+ function getStateSyncEvent({ threadId, runId, agentName, nodeName, state, running, active }) {
1448
+ const stateWithoutMessages = Object.keys(state).reduce((acc, key) => {
1449
+ if (key !== "messages") {
1450
+ acc[key] = state[key];
1451
+ }
1452
+ return acc;
1453
+ }, {});
1454
+ return JSON.stringify({
1455
+ event: "on_copilotkit_state_sync",
1456
+ thread_id: threadId,
1457
+ run_id: runId,
1458
+ agent_name: agentName,
1459
+ node_name: nodeName,
1460
+ active,
1461
+ state: stateWithoutMessages,
1462
+ running,
1463
+ role: "assistant"
1464
+ }) + "\n";
1465
+ }
1466
+ __name(getStateSyncEvent, "getStateSyncEvent");
1467
+ var StreamingStateExtractor = /* @__PURE__ */ __name(class StreamingStateExtractor2 {
1468
+ emitIntermediateState;
1469
+ toolCallBuffer;
1470
+ currentToolCall;
1471
+ previouslyParsableState;
1472
+ constructor(emitIntermediateState) {
1473
+ this.emitIntermediateState = emitIntermediateState;
1474
+ this.toolCallBuffer = {};
1475
+ this.currentToolCall = null;
1476
+ this.previouslyParsableState = {};
1477
+ }
1478
+ bufferToolCalls(event) {
1479
+ if (event.data.chunk.tool_call_chunks.length > 0) {
1480
+ const chunk = event.data.chunk.tool_call_chunks[0];
1481
+ if (chunk.name !== null) {
1482
+ this.currentToolCall = chunk.name;
1483
+ this.toolCallBuffer[this.currentToolCall] = chunk.args;
1484
+ } else if (this.currentToolCall !== null) {
1485
+ this.toolCallBuffer[this.currentToolCall] += chunk.args;
1486
+ }
1487
+ }
1488
+ }
1489
+ getEmitStateConfig(currentToolName) {
1490
+ for (const config of this.emitIntermediateState) {
1491
+ const stateKey = config["state_key"];
1492
+ const tool = config["tool"];
1493
+ const toolArgument = config["tool_argument"];
1494
+ if (currentToolName === tool) {
1495
+ return [
1496
+ toolArgument,
1497
+ stateKey
1498
+ ];
1499
+ }
1500
+ }
1501
+ return [
1502
+ null,
1503
+ null
1504
+ ];
1505
+ }
1506
+ extractState() {
1507
+ const state = {};
1508
+ for (const [key, value] of Object.entries(this.toolCallBuffer)) {
1509
+ const [argumentName, stateKey] = this.getEmitStateConfig(key);
1510
+ if (stateKey === null) {
1511
+ continue;
1512
+ }
1513
+ let parsedValue;
1514
+ try {
1515
+ parsedValue = parsePartialJson(value);
1516
+ } catch (error) {
1517
+ if (key in this.previouslyParsableState) {
1518
+ parsedValue = this.previouslyParsableState[key];
1519
+ } else {
1520
+ continue;
1521
+ }
1522
+ }
1523
+ this.previouslyParsableState[key] = parsedValue;
1524
+ if (!argumentName) {
1525
+ state[stateKey] = parsedValue;
1526
+ } else {
1527
+ state[stateKey] = parsedValue[argumentName];
1528
+ }
1529
+ }
1530
+ return state;
1531
+ }
1532
+ }, "StreamingStateExtractor");
1533
+ function langGraphDefaultMergeState(state, messages, actions) {
1534
+ if (messages.length > 0 && "role" in messages[0] && messages[0].role === "system") {
1535
+ messages = messages.slice(1);
1536
+ }
1537
+ const mergedMessages = state.messages || [];
1538
+ const existingMessageIds = new Set(mergedMessages.map((message) => message.id));
1539
+ for (const message of messages) {
1540
+ if (!existingMessageIds.has(message.id)) {
1541
+ mergedMessages.push(message);
1542
+ }
1543
+ }
1544
+ return deepMerge(state, {
1545
+ messages: mergedMessages,
1546
+ copilotkit: {
1547
+ actions
1548
+ }
1549
+ });
1550
+ }
1551
+ __name(langGraphDefaultMergeState, "langGraphDefaultMergeState");
1552
+ function deepMerge(obj1, obj2) {
1553
+ let result = {
1554
+ ...obj1
1555
+ };
1556
+ for (let key in obj2) {
1557
+ if (typeof obj2[key] === "object" && !Array.isArray(obj2[key])) {
1558
+ if (obj1[key]) {
1559
+ result[key] = deepMerge(obj1[key], obj2[key]);
1560
+ } else {
1561
+ result[key] = {
1562
+ ...obj2[key]
1563
+ };
1564
+ }
1565
+ } else {
1566
+ result[key] = obj2[key];
1567
+ }
1568
+ }
1569
+ return result;
1570
+ }
1571
+ __name(deepMerge, "deepMerge");
1572
+ function formatMessages(messages) {
1573
+ return messages.map((message) => {
1574
+ if ("content" in message) {
1575
+ return message;
1576
+ }
1577
+ if ("arguments" in message) {
1578
+ const toolCall = {
1579
+ name: message["name"],
1580
+ args: message["arguments"],
1581
+ id: message["id"]
1582
+ };
1338
1583
  return {
1339
- actions: [],
1340
- agents: []
1584
+ ...message,
1585
+ content: "",
1586
+ tool_calls: [
1587
+ toolCall
1588
+ ],
1589
+ role: message["role"] ?? MessageRole.assistant
1341
1590
  };
1342
1591
  }
1343
- const json = await response.json();
1344
- logger2.debug({
1345
- json
1346
- }, "Fetched actions from url");
1347
- return json;
1348
- } catch (error) {
1349
- logger2.error({
1350
- error: error.message ? error.message : error + ""
1351
- }, "Failed to fetch actions from url");
1352
- return {
1353
- actions: [],
1354
- agents: []
1355
- };
1356
- }
1592
+ if ("actionExecutionId" in message) {
1593
+ return {
1594
+ ...message,
1595
+ content: message["result"],
1596
+ name: message["actionName"],
1597
+ tool_call_id: message["actionExecutionId"],
1598
+ role: message["role"] ?? MessageRole.user
1599
+ };
1600
+ }
1601
+ return message;
1602
+ });
1357
1603
  }
1358
- __name(fetchRemoteInfo, "fetchRemoteInfo");
1604
+ __name(formatMessages, "formatMessages");
1605
+
1606
+ // src/lib/runtime/remote-action-constructors.ts
1607
+ function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, messages, agentStates }) {
1608
+ const agents = endpoint.agents.map((agent) => ({
1609
+ name: agent.name,
1610
+ description: agent.description,
1611
+ parameters: [],
1612
+ handler: async (_args) => {
1613
+ },
1614
+ langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName }) => {
1615
+ var _a;
1616
+ logger2.debug({
1617
+ actionName: agent.name
1618
+ }, "Executing LangGraph Cloud agent");
1619
+ telemetry_client_default.capture("oss.runtime.remote_action_executed", {});
1620
+ let state = {};
1621
+ if (agentStates) {
1622
+ const jsonState = (_a = agentStates.find((state2) => state2.agentName === name)) == null ? void 0 : _a.state;
1623
+ if (jsonState) {
1624
+ state = JSON.parse(jsonState);
1625
+ }
1626
+ }
1627
+ try {
1628
+ const response = await execute({
1629
+ deploymentUrl: endpoint.deploymentUrl,
1630
+ langsmithApiKey: endpoint.langsmithApiKey,
1631
+ agent,
1632
+ threadId,
1633
+ nodeName,
1634
+ messages,
1635
+ state,
1636
+ properties: graphqlContext.properties,
1637
+ actions: actionInputsWithoutAgents.map((action) => ({
1638
+ name: action.name,
1639
+ description: action.description,
1640
+ parameters: JSON.parse(action.jsonSchema)
1641
+ }))
1642
+ });
1643
+ const eventSource = new RemoteLangGraphEventSource();
1644
+ streamResponse(response, eventSource.eventStream$);
1645
+ return eventSource.processLangGraphEvents();
1646
+ } catch (error) {
1647
+ logger2.error({
1648
+ url: endpoint.deploymentUrl,
1649
+ status: 500,
1650
+ body: error.message
1651
+ }, "Failed to execute LangGraph Cloud agent");
1652
+ throw new Error("Failed to execute LangGraph Cloud agent");
1653
+ }
1654
+ }
1655
+ }));
1656
+ return [
1657
+ ...agents
1658
+ ];
1659
+ }
1660
+ __name(constructLGCRemoteAction, "constructLGCRemoteAction");
1359
1661
  function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, logger: logger2, messages, agentStates }) {
1360
1662
  const actions = json["actions"].map((action) => ({
1361
1663
  name: action.name,
@@ -1447,7 +1749,7 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
1447
1749
  throw new Error("Failed to execute remote agent");
1448
1750
  }
1449
1751
  const eventSource = new RemoteLangGraphEventSource();
1450
- eventSource.streamResponse(response);
1752
+ streamResponse(response.body, eventSource.eventStream$);
1451
1753
  return eventSource.processLangGraphEvents();
1452
1754
  }
1453
1755
  }));
@@ -1457,34 +1759,165 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
1457
1759
  ];
1458
1760
  }
1459
1761
  __name(constructRemoteActions, "constructRemoteActions");
1460
- async function setupRemoteActions({ remoteActionDefinitions, graphqlContext, messages, agentStates, frontendUrl }) {
1762
+ async function streamResponse(response, eventStream$) {
1763
+ const reader = response.getReader();
1764
+ const decoder = new TextDecoder();
1765
+ let buffer = [];
1766
+ function flushBuffer() {
1767
+ const currentBuffer = buffer.join("");
1768
+ if (currentBuffer.trim().length === 0) {
1769
+ return;
1770
+ }
1771
+ const parts = currentBuffer.split("\n");
1772
+ if (parts.length === 0) {
1773
+ return;
1774
+ }
1775
+ const lastPartIsComplete = currentBuffer.endsWith("\n");
1776
+ buffer = [];
1777
+ if (!lastPartIsComplete) {
1778
+ buffer.push(parts.pop());
1779
+ }
1780
+ parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
1781
+ eventStream$.next(JSON.parse(part));
1782
+ });
1783
+ }
1784
+ __name(flushBuffer, "flushBuffer");
1785
+ try {
1786
+ while (true) {
1787
+ const { done, value } = await reader.read();
1788
+ if (!done) {
1789
+ buffer.push(decoder.decode(value, {
1790
+ stream: true
1791
+ }));
1792
+ }
1793
+ flushBuffer();
1794
+ if (done) {
1795
+ break;
1796
+ }
1797
+ }
1798
+ } catch (error) {
1799
+ console.error("Error in stream", error);
1800
+ eventStream$.error(error);
1801
+ return;
1802
+ }
1803
+ eventStream$.complete();
1804
+ }
1805
+ __name(streamResponse, "streamResponse");
1806
+ function createHeaders(onBeforeRequest, graphqlContext) {
1807
+ const headers = {
1808
+ "Content-Type": "application/json"
1809
+ };
1810
+ if (onBeforeRequest) {
1811
+ const { headers: additionalHeaders } = onBeforeRequest({
1812
+ ctx: graphqlContext
1813
+ });
1814
+ if (additionalHeaders) {
1815
+ Object.assign(headers, additionalHeaders);
1816
+ }
1817
+ }
1818
+ return headers;
1819
+ }
1820
+ __name(createHeaders, "createHeaders");
1821
+
1822
+ // src/lib/runtime/remote-actions.ts
1823
+ var EndpointType;
1824
+ (function(EndpointType2) {
1825
+ EndpointType2["CopilotKit"] = "copilotKit";
1826
+ EndpointType2["LangGraphCloud"] = "langgraph-cloud";
1827
+ })(EndpointType || (EndpointType = {}));
1828
+ function isLangGraphAgentAction(action) {
1829
+ if (!action) {
1830
+ return false;
1831
+ }
1832
+ return typeof action.langGraphAgentHandler === "function";
1833
+ }
1834
+ __name(isLangGraphAgentAction, "isLangGraphAgentAction");
1835
+ async function fetchRemoteInfo({ url, onBeforeRequest, graphqlContext, logger: logger2, frontendUrl }) {
1836
+ logger2.debug({
1837
+ url
1838
+ }, "Fetching actions from url");
1839
+ const headers = createHeaders(onBeforeRequest, graphqlContext);
1840
+ try {
1841
+ const response = await fetch(`${url}/info`, {
1842
+ method: "POST",
1843
+ headers,
1844
+ body: JSON.stringify({
1845
+ properties: graphqlContext.properties,
1846
+ frontendUrl
1847
+ })
1848
+ });
1849
+ if (!response.ok) {
1850
+ logger2.error({
1851
+ url,
1852
+ status: response.status,
1853
+ body: await response.text()
1854
+ }, "Failed to fetch actions from url");
1855
+ return {
1856
+ actions: [],
1857
+ agents: []
1858
+ };
1859
+ }
1860
+ const json = await response.json();
1861
+ logger2.debug({
1862
+ json
1863
+ }, "Fetched actions from url");
1864
+ return json;
1865
+ } catch (error) {
1866
+ logger2.error({
1867
+ error: error.message ? error.message : error + ""
1868
+ }, "Failed to fetch actions from url");
1869
+ return {
1870
+ actions: [],
1871
+ agents: []
1872
+ };
1873
+ }
1874
+ }
1875
+ __name(fetchRemoteInfo, "fetchRemoteInfo");
1876
+ async function setupRemoteActions({ remoteEndpointDefinitions, graphqlContext, messages, agentStates, frontendUrl }) {
1461
1877
  const logger2 = graphqlContext.logger.child({
1462
1878
  component: "remote-actions.fetchRemoteActions"
1463
1879
  });
1464
1880
  logger2.debug({
1465
- remoteActionDefinitions
1466
- }, "Fetching remote actions");
1467
- const filtered = remoteActionDefinitions.filter((value, index, self) => index === self.findIndex((t) => t.url === value.url));
1468
- const result = await Promise.all(filtered.map(async (actionDefinition) => {
1881
+ remoteEndpointDefinitions
1882
+ }, "Fetching from remote endpoints");
1883
+ const filtered = remoteEndpointDefinitions.filter((value, index, self) => {
1884
+ if (value.type === "langgraph-cloud") {
1885
+ return value;
1886
+ }
1887
+ return index === self.findIndex((t) => t.url === value.url);
1888
+ });
1889
+ const result = await Promise.all(filtered.map(async (endpoint) => {
1890
+ if (endpoint.type === "langgraph-cloud") {
1891
+ return constructLGCRemoteAction({
1892
+ endpoint,
1893
+ messages,
1894
+ graphqlContext,
1895
+ logger: logger2.child({
1896
+ component: "remote-actions.constructLGCRemoteAction",
1897
+ endpoint
1898
+ }),
1899
+ agentStates
1900
+ });
1901
+ }
1469
1902
  const json = await fetchRemoteInfo({
1470
- url: actionDefinition.url,
1471
- onBeforeRequest: actionDefinition.onBeforeRequest,
1903
+ url: endpoint.url,
1904
+ onBeforeRequest: endpoint.onBeforeRequest,
1472
1905
  graphqlContext,
1473
1906
  logger: logger2.child({
1474
1907
  component: "remote-actions.fetchActionsFromUrl",
1475
- actionDefinition
1908
+ endpoint
1476
1909
  }),
1477
1910
  frontendUrl
1478
1911
  });
1479
1912
  return constructRemoteActions({
1480
1913
  json,
1481
1914
  messages,
1482
- url: actionDefinition.url,
1483
- onBeforeRequest: actionDefinition.onBeforeRequest,
1915
+ url: endpoint.url,
1916
+ onBeforeRequest: endpoint.onBeforeRequest,
1484
1917
  graphqlContext,
1485
1918
  logger: logger2.child({
1486
1919
  component: "remote-actions.constructActions",
1487
- actionDefinition
1920
+ endpoint
1488
1921
  }),
1489
1922
  agentStates
1490
1923
  });
@@ -1648,13 +2081,7 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
1648
2081
  }
1649
2082
  let args = [];
1650
2083
  if (actionArguments) {
1651
- try {
1652
- args = JSON.parse(actionArguments);
1653
- } catch (e) {
1654
- console.warn("Action argument unparsable", {
1655
- actionArguments
1656
- });
1657
- }
2084
+ args = JSON.parse(actionArguments);
1658
2085
  }
1659
2086
  if (isLangGraphAgentAction(action)) {
1660
2087
  eventStream$.sendActionExecutionResult(actionExecutionId, action.name, `${action.name} agent started`);
@@ -2243,6 +2670,7 @@ __name(copilotRuntimeNodeHttpEndpoint, "copilotRuntimeNodeHttpEndpoint");
2243
2670
 
2244
2671
  export {
2245
2672
  telemetry_client_default,
2673
+ EndpointType,
2246
2674
  isLangGraphAgentAction,
2247
2675
  setupRemoteActions,
2248
2676
  RuntimeEventSource,
@@ -2251,4 +2679,4 @@ export {
2251
2679
  getCommonConfig,
2252
2680
  copilotRuntimeNodeHttpEndpoint
2253
2681
  };
2254
- //# sourceMappingURL=chunk-AOYYOKTP.mjs.map
2682
+ //# sourceMappingURL=chunk-E6ZFCM3B.mjs.map