@copilotkit/runtime 1.3.12 → 1.3.14-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 +16 -0
  2. package/dist/{chunk-WBLYFYMZ.mjs → chunk-44JZSRY7.mjs} +2 -2
  3. package/dist/{chunk-PRG6VWHR.mjs → chunk-7MKHQGLE.mjs} +2 -2
  4. package/dist/{chunk-MYZHUCL6.mjs → chunk-B74M7FXG.mjs} +1 -1
  5. package/dist/chunk-B74M7FXG.mjs.map +1 -0
  6. package/dist/{chunk-ZEHCLFJ2.mjs → chunk-CPAHDRLS.mjs} +28 -4
  7. package/dist/chunk-CPAHDRLS.mjs.map +1 -0
  8. package/dist/{chunk-IZP72K7I.mjs → chunk-PY76HMMJ.mjs} +2 -2
  9. package/dist/{chunk-IXVCESAR.mjs → chunk-UF6TJDZ5.mjs} +37 -10
  10. package/dist/chunk-UF6TJDZ5.mjs.map +1 -0
  11. package/dist/{chunk-24WEOOFX.mjs → chunk-Y6RQG5HF.mjs} +2 -2
  12. package/dist/{chunk-F2KJWMGD.mjs → chunk-ZIHDDSMJ.mjs} +690 -152
  13. package/dist/chunk-ZIHDDSMJ.mjs.map +1 -0
  14. package/dist/{copilot-runtime-df3527ad.d.ts → copilot-runtime-68acb673.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-c2e65c95.d.ts} +1 -1
  19. package/dist/{index-cff31380.d.ts → index-439dfa2a.d.ts} +1 -1
  20. package/dist/index.d.ts +4 -4
  21. package/dist/index.js +788 -199
  22. package/dist/index.js.map +1 -1
  23. package/dist/index.mjs +14 -10
  24. package/dist/index.mjs.map +1 -1
  25. package/dist/{langserve-a14a6849.d.ts → langserve-0d844922.d.ts} +1 -1
  26. package/dist/lib/index.d.ts +4 -4
  27. package/dist/lib/index.js +788 -199
  28. package/dist/lib/index.js.map +1 -1
  29. package/dist/lib/index.mjs +14 -10
  30. package/dist/lib/integrations/index.d.ts +4 -4
  31. package/dist/lib/integrations/index.js +3 -1
  32. package/dist/lib/integrations/index.js.map +1 -1
  33. package/dist/lib/integrations/index.mjs +6 -6
  34. package/dist/lib/integrations/nest/index.d.ts +3 -3
  35. package/dist/lib/integrations/nest/index.js +3 -1
  36. package/dist/lib/integrations/nest/index.js.map +1 -1
  37. package/dist/lib/integrations/nest/index.mjs +4 -4
  38. package/dist/lib/integrations/node-express/index.d.ts +3 -3
  39. package/dist/lib/integrations/node-express/index.js +3 -1
  40. package/dist/lib/integrations/node-express/index.js.map +1 -1
  41. package/dist/lib/integrations/node-express/index.mjs +4 -4
  42. package/dist/lib/integrations/node-http/index.d.ts +3 -3
  43. package/dist/lib/integrations/node-http/index.js +3 -1
  44. package/dist/lib/integrations/node-http/index.js.map +1 -1
  45. package/dist/lib/integrations/node-http/index.mjs +3 -3
  46. package/dist/service-adapters/index.d.ts +4 -4
  47. package/dist/service-adapters/index.js +27 -3
  48. package/dist/service-adapters/index.js.map +1 -1
  49. package/dist/service-adapters/index.mjs +2 -2
  50. package/package.json +6 -4
  51. package/src/agents/langgraph/event-source.ts +62 -94
  52. package/src/agents/langgraph/events.ts +26 -17
  53. package/src/graphql/types/converted/index.ts +5 -1
  54. package/src/lib/runtime/copilot-runtime.ts +58 -11
  55. package/src/lib/runtime/remote-action-constructors.ts +284 -0
  56. package/src/lib/runtime/remote-actions.ts +65 -159
  57. package/src/lib/runtime/remote-lg-cloud-action.ts +624 -0
  58. package/src/service-adapters/langchain/utils.ts +31 -3
  59. package/dist/chunk-F2KJWMGD.mjs.map +0 -1
  60. package/dist/chunk-IXVCESAR.mjs.map +0 -1
  61. package/dist/chunk-MYZHUCL6.mjs.map +0 -1
  62. package/dist/chunk-ZEHCLFJ2.mjs.map +0 -1
  63. /package/dist/{chunk-WBLYFYMZ.mjs.map → chunk-44JZSRY7.mjs.map} +0 -0
  64. /package/dist/{chunk-PRG6VWHR.mjs.map → chunk-7MKHQGLE.mjs.map} +0 -0
  65. /package/dist/{chunk-IZP72K7I.mjs.map → chunk-PY76HMMJ.mjs.map} +0 -0
  66. /package/dist/{chunk-24WEOOFX.mjs.map → chunk-Y6RQG5HF.mjs.map} +0 -0
package/dist/index.js CHANGED
@@ -44,7 +44,7 @@ var require_package = __commonJS({
44
44
  publishConfig: {
45
45
  access: "public"
46
46
  },
47
- version: "1.3.12",
47
+ version: "1.3.14-feat-langgraph-cloud.0",
48
48
  sideEffects: false,
49
49
  main: "./dist/index.js",
50
50
  module: "./dist/index.mjs",
@@ -87,6 +87,7 @@ var require_package = __commonJS({
87
87
  "@langchain/community": "^0.0.53",
88
88
  "@langchain/core": "^0.3.13",
89
89
  "@langchain/google-gauth": "^0.1.0",
90
+ "@langchain/langgraph-sdk": "^0.0.16",
90
91
  "@langchain/openai": "^0.0.28",
91
92
  "class-transformer": "^0.5.1",
92
93
  express: "^4.19.2",
@@ -96,6 +97,7 @@ var require_package = __commonJS({
96
97
  "groq-sdk": "^0.5.0",
97
98
  langchain: "^0.3.3",
98
99
  openai: "^4.50.0",
100
+ "partial-json": "^0.1.7",
99
101
  pino: "^9.2.0",
100
102
  "pino-pretty": "^11.2.1",
101
103
  "reflect-metadata": "^0.2.2",
@@ -136,6 +138,7 @@ __export(src_exports, {
136
138
  UnknownErrorResponse: () => UnknownErrorResponse,
137
139
  buildSchema: () => buildSchema,
138
140
  config: () => config,
141
+ copilotKitEndpoint: () => copilotKitEndpoint,
139
142
  copilotRuntimeNestEndpoint: () => copilotRuntimeNestEndpoint,
140
143
  copilotRuntimeNextJSAppRouterEndpoint: () => copilotRuntimeNextJSAppRouterEndpoint,
141
144
  copilotRuntimeNextJSPagesRouterEndpoint: () => copilotRuntimeNextJSPagesRouterEndpoint,
@@ -143,7 +146,8 @@ __export(src_exports, {
143
146
  copilotRuntimeNodeHttpEndpoint: () => copilotRuntimeNodeHttpEndpoint,
144
147
  createContext: () => createContext,
145
148
  flattenToolCallsNoDuplicates: () => flattenToolCallsNoDuplicates,
146
- getCommonConfig: () => getCommonConfig
149
+ getCommonConfig: () => getCommonConfig,
150
+ langGraphCloudEndpoint: () => langGraphCloudEndpoint
147
151
  });
148
152
  module.exports = __toCommonJS(src_exports);
149
153
  var import_reflect_metadata = require("reflect-metadata");
@@ -583,20 +587,39 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
583
587
  maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
584
588
  let reader = result.getReader();
585
589
  let mode = null;
590
+ const toolCallDetails = {
591
+ name: null,
592
+ id: null,
593
+ index: null,
594
+ prevIndex: null
595
+ };
586
596
  while (true) {
587
597
  try {
588
598
  const { done, value } = await reader.read();
589
599
  let toolCallName = void 0;
590
600
  let toolCallId = void 0;
591
601
  let toolCallArgs = void 0;
602
+ let toolCallIndex = void 0;
603
+ let toolCallPrevIndex = void 0;
592
604
  let hasToolCall = false;
593
605
  let content = value == null ? void 0 : value.content;
594
606
  if (isAIMessageChunk(value)) {
595
607
  let chunk = (_d = value.tool_call_chunks) == null ? void 0 : _d[0];
596
- toolCallName = chunk == null ? void 0 : chunk.name;
597
- toolCallId = chunk == null ? void 0 : chunk.id;
598
608
  toolCallArgs = chunk == null ? void 0 : chunk.args;
599
609
  hasToolCall = chunk != void 0;
610
+ if (chunk == null ? void 0 : chunk.name)
611
+ toolCallDetails.name = chunk.name;
612
+ if ((chunk == null ? void 0 : chunk.index) != null) {
613
+ toolCallDetails.index = chunk.index;
614
+ if (toolCallDetails.prevIndex == null)
615
+ toolCallDetails.prevIndex = chunk.index;
616
+ }
617
+ if (chunk == null ? void 0 : chunk.id)
618
+ toolCallDetails.id = chunk.index != null ? `${chunk.id}-idx-${chunk.index}` : chunk.id;
619
+ toolCallName = toolCallDetails.name;
620
+ toolCallId = toolCallDetails.id;
621
+ toolCallIndex = toolCallDetails.index;
622
+ toolCallPrevIndex = toolCallDetails.prevIndex;
600
623
  } else if (isBaseMessageChunk(value)) {
601
624
  let chunk = (_f = (_e = value.additional_kwargs) == null ? void 0 : _e.tool_calls) == null ? void 0 : _f[0];
602
625
  toolCallName = (_g = chunk == null ? void 0 : chunk.function) == null ? void 0 : _g.name;
@@ -615,7 +638,7 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
615
638
  break;
616
639
  }
617
640
  if (mode === null) {
618
- if (hasToolCall) {
641
+ if (hasToolCall && toolCallId && toolCallName) {
619
642
  mode = "function";
620
643
  eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
621
644
  } else if (content) {
@@ -626,6 +649,11 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
626
649
  if (mode === "message" && content) {
627
650
  eventStream$.sendTextMessageContent(Array.isArray(content) ? ((_i = content[0]) == null ? void 0 : _i.text) ?? "" : content);
628
651
  } else if (mode === "function" && toolCallArgs) {
652
+ if (toolCallIndex !== toolCallPrevIndex) {
653
+ eventStream$.sendActionExecutionEnd();
654
+ eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
655
+ toolCallDetails.prevIndex = toolCallDetails.index;
656
+ }
629
657
  eventStream$.sendActionExecutionArgs(toolCallArgs);
630
658
  }
631
659
  } catch (error) {
@@ -1330,58 +1358,19 @@ var LangGraphEventTypes;
1330
1358
  LangGraphEventTypes2["OnToolStart"] = "on_tool_start";
1331
1359
  LangGraphEventTypes2["OnToolEnd"] = "on_tool_end";
1332
1360
  LangGraphEventTypes2["OnCopilotKitStateSync"] = "on_copilotkit_state_sync";
1333
- LangGraphEventTypes2["OnCopilotKitEmitMessage"] = "on_copilotkit_emit_message";
1334
- LangGraphEventTypes2["OnCopilotKitEmitToolCall"] = "on_copilotkit_emit_tool_call";
1361
+ LangGraphEventTypes2["OnCustomEvent"] = "on_custom_event";
1335
1362
  })(LangGraphEventTypes || (LangGraphEventTypes = {}));
1363
+ var CustomEventNames;
1364
+ (function(CustomEventNames2) {
1365
+ CustomEventNames2["CopilotKitManuallyEmitMessage"] = "copilotkit_manually_emit_message";
1366
+ CustomEventNames2["CopilotKitManuallyEmitToolCall"] = "copilotkit_manually_emit_tool_call";
1367
+ CustomEventNames2["CopilotKitManuallyEmitIntermediateState"] = "copilotkit_manually_emit_intermediate_state";
1368
+ })(CustomEventNames || (CustomEventNames = {}));
1336
1369
 
1337
1370
  // src/agents/langgraph/event-source.ts
1338
1371
  var import_shared8 = require("@copilotkit/shared");
1339
1372
  var RemoteLangGraphEventSource = class {
1340
1373
  eventStream$ = new import_rxjs.ReplaySubject();
1341
- async streamResponse(response) {
1342
- const reader = response.body.getReader();
1343
- const decoder = new TextDecoder();
1344
- let buffer = [];
1345
- const eventStream$ = this.eventStream$;
1346
- function flushBuffer() {
1347
- const currentBuffer = buffer.join("");
1348
- if (currentBuffer.trim().length === 0) {
1349
- return;
1350
- }
1351
- const parts = currentBuffer.split("\n");
1352
- if (parts.length === 0) {
1353
- return;
1354
- }
1355
- const lastPartIsComplete = currentBuffer.endsWith("\n");
1356
- buffer = [];
1357
- if (!lastPartIsComplete) {
1358
- buffer.push(parts.pop());
1359
- }
1360
- parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
1361
- eventStream$.next(JSON.parse(part));
1362
- });
1363
- }
1364
- __name(flushBuffer, "flushBuffer");
1365
- try {
1366
- while (true) {
1367
- const { done, value } = await reader.read();
1368
- if (!done) {
1369
- buffer.push(decoder.decode(value, {
1370
- stream: true
1371
- }));
1372
- }
1373
- flushBuffer();
1374
- if (done) {
1375
- break;
1376
- }
1377
- }
1378
- } catch (error) {
1379
- console.error("Error in stream", error);
1380
- eventStream$.error(error);
1381
- return;
1382
- }
1383
- eventStream$.complete();
1384
- }
1385
1374
  shouldEmitToolCall(shouldEmitToolCalls, toolCallName) {
1386
1375
  if (typeof shouldEmitToolCalls === "boolean") {
1387
1376
  return shouldEmitToolCalls;
@@ -1404,20 +1393,25 @@ var RemoteLangGraphEventSource = class {
1404
1393
  } else {
1405
1394
  acc.content = null;
1406
1395
  }
1407
- if ((_h = (_g = (_f = event.data) == null ? void 0 : _f.chunk) == null ? void 0 : _g.kwargs) == null ? void 0 : _h.tool_call_chunks) {
1396
+ const toolCallChunks = (
1397
+ // @ts-expect-error -- LangGraph Cloud implementation stores data outside of kwargs
1398
+ ((_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)
1399
+ );
1400
+ 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);
1401
+ if (toolCallChunks && toolCallChunks.length > 0) {
1408
1402
  acc.prevToolCallMessageId = acc.toolCallMessageId;
1409
- acc.toolCallMessageId = (_i = event.data.chunk.kwargs) == null ? void 0 : _i.id;
1410
- if ((_j = event.data.chunk.kwargs.tool_call_chunks[0]) == null ? void 0 : _j.name) {
1411
- acc.toolCallName = event.data.chunk.kwargs.tool_call_chunks[0].name;
1403
+ acc.toolCallMessageId = toolCallMessageId;
1404
+ if ((_p = toolCallChunks[0]) == null ? void 0 : _p.name) {
1405
+ acc.toolCallName = toolCallChunks[0].name;
1412
1406
  }
1413
- if ((_k = event.data.chunk.kwargs.tool_call_chunks[0]) == null ? void 0 : _k.id) {
1414
- acc.toolCallId = event.data.chunk.kwargs.tool_call_chunks[0].id;
1407
+ if ((_q = toolCallChunks[0]) == null ? void 0 : _q.id) {
1408
+ acc.toolCallId = toolCallChunks[0].id;
1415
1409
  }
1416
1410
  acc.prevMessageId = acc.messageId;
1417
- acc.messageId = (_n = (_m = (_l = event.data) == null ? void 0 : _l.chunk) == null ? void 0 : _m.kwargs) == null ? void 0 : _n.id;
1411
+ acc.messageId = toolCallMessageId;
1418
1412
  } else if (acc.content && acc.content != "") {
1419
1413
  acc.prevMessageId = acc.messageId;
1420
- acc.messageId = (_q = (_p = (_o = event.data) == null ? void 0 : _o.chunk) == null ? void 0 : _p.kwargs) == null ? void 0 : _q.id;
1414
+ acc.messageId = toolCallMessageId;
1421
1415
  } else {
1422
1416
  acc.prevToolCallMessageId = acc.toolCallMessageId;
1423
1417
  acc.prevMessageId = acc.messageId;
@@ -1442,7 +1436,7 @@ var RemoteLangGraphEventSource = class {
1442
1436
  prevMessageId: null,
1443
1437
  content: null
1444
1438
  }), (0, import_rxjs.mergeMap)((eventWithState) => {
1445
- var _a, _b, _c, _d, _e;
1439
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1446
1440
  const events = [];
1447
1441
  let shouldEmitMessages = true;
1448
1442
  let shouldEmitToolCalls = false;
@@ -1465,32 +1459,33 @@ var RemoteLangGraphEventSource = class {
1465
1459
  });
1466
1460
  }
1467
1461
  switch (eventWithState.event.event) {
1468
- case LangGraphEventTypes.OnCopilotKitEmitMessage:
1469
- events.push({
1470
- type: RuntimeEventTypes.TextMessageStart,
1471
- messageId: eventWithState.event.message_id
1472
- });
1473
- events.push({
1474
- type: RuntimeEventTypes.TextMessageContent,
1475
- content: eventWithState.event.message
1476
- });
1477
- events.push({
1478
- type: RuntimeEventTypes.TextMessageEnd
1479
- });
1480
- break;
1481
- case LangGraphEventTypes.OnCopilotKitEmitToolCall:
1482
- events.push({
1483
- type: RuntimeEventTypes.ActionExecutionStart,
1484
- actionExecutionId: eventWithState.event.id,
1485
- actionName: eventWithState.event.name
1486
- });
1487
- events.push({
1488
- type: RuntimeEventTypes.ActionExecutionArgs,
1489
- args: JSON.stringify(eventWithState.event.args)
1490
- });
1491
- events.push({
1492
- type: RuntimeEventTypes.ActionExecutionEnd
1493
- });
1462
+ case LangGraphEventTypes.OnCustomEvent:
1463
+ if (eventWithState.event.name === CustomEventNames.CopilotKitManuallyEmitMessage) {
1464
+ events.push({
1465
+ type: RuntimeEventTypes.TextMessageStart,
1466
+ messageId: eventWithState.event.data.message_id
1467
+ });
1468
+ events.push({
1469
+ type: RuntimeEventTypes.TextMessageContent,
1470
+ content: eventWithState.event.data.message
1471
+ });
1472
+ events.push({
1473
+ type: RuntimeEventTypes.TextMessageEnd
1474
+ });
1475
+ } else if (eventWithState.event.name === CustomEventNames.CopilotKitManuallyEmitToolCall) {
1476
+ events.push({
1477
+ type: RuntimeEventTypes.ActionExecutionStart,
1478
+ actionExecutionId: eventWithState.event.data.id,
1479
+ actionName: eventWithState.event.data.name
1480
+ });
1481
+ events.push({
1482
+ type: RuntimeEventTypes.ActionExecutionArgs,
1483
+ args: JSON.stringify(eventWithState.event.data.args)
1484
+ });
1485
+ events.push({
1486
+ type: RuntimeEventTypes.ActionExecutionEnd
1487
+ });
1488
+ }
1494
1489
  break;
1495
1490
  case LangGraphEventTypes.OnCopilotKitStateSync:
1496
1491
  events.push({
@@ -1525,7 +1520,8 @@ var RemoteLangGraphEventSource = class {
1525
1520
  });
1526
1521
  }
1527
1522
  }
1528
- 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;
1523
+ 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
1524
+ ((_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);
1529
1525
  const content = eventWithState.content;
1530
1526
  if (args) {
1531
1527
  if (this.shouldEmitToolCall(shouldEmitToolCalls, eventWithState.toolCallName)) {
@@ -1576,70 +1572,543 @@ var RemoteLangGraphEventSource = class {
1576
1572
  };
1577
1573
  __name(RemoteLangGraphEventSource, "RemoteLangGraphEventSource");
1578
1574
 
1579
- // src/lib/runtime/remote-actions.ts
1580
- function isLangGraphAgentAction(action) {
1581
- if (!action) {
1582
- return false;
1583
- }
1584
- return typeof action.langGraphAgentHandler === "function";
1575
+ // src/lib/runtime/remote-lg-cloud-action.ts
1576
+ var import_langgraph_sdk = require("@langchain/langgraph-sdk");
1577
+ var import_node_crypto = require("crypto");
1578
+ var import_partial_json = require("partial-json");
1579
+
1580
+ // src/graphql/types/enums.ts
1581
+ var import_type_graphql = require("type-graphql");
1582
+ var MessageRole;
1583
+ (function(MessageRole2) {
1584
+ MessageRole2["user"] = "user";
1585
+ MessageRole2["assistant"] = "assistant";
1586
+ MessageRole2["system"] = "system";
1587
+ })(MessageRole || (MessageRole = {}));
1588
+ var ActionExecutionScope;
1589
+ (function(ActionExecutionScope2) {
1590
+ ActionExecutionScope2["server"] = "server";
1591
+ ActionExecutionScope2["client"] = "client";
1592
+ ActionExecutionScope2["passThrough"] = "passThrough";
1593
+ })(ActionExecutionScope || (ActionExecutionScope = {}));
1594
+ var CopilotRequestType;
1595
+ (function(CopilotRequestType2) {
1596
+ CopilotRequestType2["Chat"] = "Chat";
1597
+ CopilotRequestType2["Task"] = "Task";
1598
+ CopilotRequestType2["TextareaCompletion"] = "TextareaCompletion";
1599
+ CopilotRequestType2["TextareaPopover"] = "TextareaPopover";
1600
+ CopilotRequestType2["Suggestion"] = "Suggestion";
1601
+ })(CopilotRequestType || (CopilotRequestType = {}));
1602
+ (0, import_type_graphql.registerEnumType)(MessageRole, {
1603
+ name: "MessageRole",
1604
+ description: "The role of the message"
1605
+ });
1606
+ (0, import_type_graphql.registerEnumType)(ActionExecutionScope, {
1607
+ name: "ActionExecutionScope",
1608
+ description: "The scope of the action"
1609
+ });
1610
+ (0, import_type_graphql.registerEnumType)(CopilotRequestType, {
1611
+ name: "CopilotRequestType",
1612
+ description: "The type of Copilot request"
1613
+ });
1614
+
1615
+ // src/lib/runtime/remote-lg-cloud-action.ts
1616
+ async function execute(args) {
1617
+ return new ReadableStream({
1618
+ async start(controller) {
1619
+ try {
1620
+ await streamEvents(controller, args);
1621
+ controller.close();
1622
+ } catch (err) {
1623
+ }
1624
+ }
1625
+ });
1585
1626
  }
1586
- __name(isLangGraphAgentAction, "isLangGraphAgentAction");
1587
- function createHeaders(onBeforeRequest, graphqlContext) {
1588
- const headers = {
1589
- "Content-Type": "application/json"
1627
+ __name(execute, "execute");
1628
+ async function streamEvents(controller, args) {
1629
+ const { deploymentUrl, langsmithApiKey, threadId: agrsInitialThreadId, agent, nodeName: initialNodeName, state: initialState, messages, actions, logger: logger2 } = args;
1630
+ let nodeName = initialNodeName;
1631
+ let state = initialState;
1632
+ const { name, assistantId: initialAssistantId } = agent;
1633
+ const client = new import_langgraph_sdk.Client({
1634
+ apiUrl: deploymentUrl,
1635
+ apiKey: langsmithApiKey
1636
+ });
1637
+ let initialThreadId = agrsInitialThreadId;
1638
+ const wasInitiatedWithExistingThread = !!initialThreadId;
1639
+ if (initialThreadId && initialThreadId.startsWith("ck-")) {
1640
+ initialThreadId = initialThreadId.substring(3);
1641
+ }
1642
+ const assistants = await client.assistants.search();
1643
+ const retrievedAssistant = assistants.find((a) => a.name === name);
1644
+ const threadId = initialThreadId ?? (0, import_node_crypto.randomUUID)();
1645
+ if (initialThreadId === threadId) {
1646
+ await client.threads.get(threadId);
1647
+ } else {
1648
+ await client.threads.create({
1649
+ threadId
1650
+ });
1651
+ }
1652
+ let agentState = {
1653
+ values: {}
1590
1654
  };
1591
- if (onBeforeRequest) {
1592
- const { headers: additionalHeaders } = onBeforeRequest({
1593
- ctx: graphqlContext
1655
+ if (wasInitiatedWithExistingThread) {
1656
+ agentState = await client.threads.getState(threadId);
1657
+ }
1658
+ const agentStateValues = agentState.values;
1659
+ state.messages = agentStateValues.messages;
1660
+ const mode = wasInitiatedWithExistingThread && nodeName != "__end__" ? "continue" : "start";
1661
+ state = langGraphDefaultMergeState(state, formatMessages(messages), actions, name);
1662
+ if (mode === "continue") {
1663
+ await client.threads.updateState(threadId, {
1664
+ values: state,
1665
+ asNode: nodeName
1594
1666
  });
1595
- if (additionalHeaders) {
1596
- Object.assign(headers, additionalHeaders);
1667
+ }
1668
+ const assistantId = initialAssistantId ?? (retrievedAssistant == null ? void 0 : retrievedAssistant.assistant_id);
1669
+ if (!assistantId) {
1670
+ console.error(`
1671
+ No agent found for the agent name specified in CopilotKit provider
1672
+ Please check your available agents or provide an agent ID in the LangGraph Cloud endpoint definition.
1673
+
1674
+
1675
+ These are the available agents: [${assistants.map((a) => `${a.name} (ID: ${a.assistant_id})`).join(", ")}]
1676
+ `);
1677
+ throw new Error("No agent id found");
1678
+ }
1679
+ const graphInfo = await client.assistants.getGraph(assistantId);
1680
+ const streamInput = mode === "start" ? state : null;
1681
+ let streamingStateExtractor = new StreamingStateExtractor([]);
1682
+ let prevNodeName = null;
1683
+ let emitIntermediateStateUntilEnd = null;
1684
+ let shouldExit = null;
1685
+ let externalRunId = null;
1686
+ const streamResponse2 = client.runs.stream(threadId, assistantId, {
1687
+ input: streamInput,
1688
+ streamMode: [
1689
+ "events",
1690
+ "values"
1691
+ ]
1692
+ });
1693
+ const emit = /* @__PURE__ */ __name((message) => controller.enqueue(new TextEncoder().encode(message)), "emit");
1694
+ let latestStateValues = {};
1695
+ for await (const chunk of streamResponse2) {
1696
+ if (![
1697
+ "events",
1698
+ "values",
1699
+ "error"
1700
+ ].includes(chunk.event))
1701
+ continue;
1702
+ if (chunk.event === "error") {
1703
+ logger2.error(chunk, `Error event thrown: ${chunk.data.message}`);
1704
+ throw new Error(`Error event thrown: ${chunk.data.message}`);
1705
+ }
1706
+ if (chunk.event === "values") {
1707
+ latestStateValues = chunk.data;
1708
+ continue;
1709
+ }
1710
+ const event = chunk.data;
1711
+ const currentNodeName = event.name;
1712
+ const eventType = event.event;
1713
+ const runId = event.metadata.run_id;
1714
+ externalRunId = runId;
1715
+ const metadata = event.metadata;
1716
+ shouldExit = shouldExit != null ? shouldExit : metadata["copilotkit:exit"];
1717
+ const emitIntermediateState = metadata["copilotkit:emit-intermediate-state"];
1718
+ const forceEmitIntermediateState = metadata["copilotkit:force-emit-intermediate-state"];
1719
+ const manuallyEmitMessage = metadata["copilotkit:manually-emit-messages"];
1720
+ const manuallyEmitToolCall = metadata["copilotkit:manually-emit-tool-calls"];
1721
+ if (graphInfo["nodes"].some((node) => node.id === currentNodeName)) {
1722
+ nodeName = currentNodeName;
1597
1723
  }
1724
+ if (!nodeName) {
1725
+ continue;
1726
+ }
1727
+ if (forceEmitIntermediateState) {
1728
+ if (eventType === "on_chain_end") {
1729
+ state = event.data.output;
1730
+ emit(getStateSyncEvent({
1731
+ threadId,
1732
+ runId,
1733
+ agentName: agent.name,
1734
+ nodeName,
1735
+ state: event.data.output,
1736
+ running: true,
1737
+ active: true
1738
+ }));
1739
+ }
1740
+ continue;
1741
+ }
1742
+ if (manuallyEmitMessage) {
1743
+ if (eventType === "on_chain_end") {
1744
+ state = event.data.output;
1745
+ emit(JSON.stringify({
1746
+ event: "on_copilotkit_emit_message",
1747
+ message: event.data.output,
1748
+ messageId: (0, import_node_crypto.randomUUID)(),
1749
+ role: MessageRole.assistant
1750
+ }) + "\n");
1751
+ }
1752
+ continue;
1753
+ }
1754
+ if (manuallyEmitToolCall) {
1755
+ if (eventType === "on_chain_end") {
1756
+ state = event.data.output;
1757
+ emit(JSON.stringify({
1758
+ event: "on_copilotkit_emit_tool_call",
1759
+ name: event.data.output.name,
1760
+ args: event.data.output.args,
1761
+ id: event.data.output.id
1762
+ }) + "\n");
1763
+ }
1764
+ continue;
1765
+ }
1766
+ if (emitIntermediateState && emitIntermediateStateUntilEnd == null) {
1767
+ emitIntermediateStateUntilEnd = nodeName;
1768
+ }
1769
+ if (emitIntermediateState && eventType === "on_chat_model_start") {
1770
+ streamingStateExtractor = new StreamingStateExtractor(emitIntermediateState);
1771
+ }
1772
+ let updatedState = latestStateValues;
1773
+ if (emitIntermediateState && eventType === "on_chat_model_stream") {
1774
+ streamingStateExtractor.bufferToolCalls(event);
1775
+ }
1776
+ if (emitIntermediateStateUntilEnd !== null) {
1777
+ updatedState = {
1778
+ ...updatedState,
1779
+ ...streamingStateExtractor.extractState()
1780
+ };
1781
+ }
1782
+ if (!emitIntermediateState && currentNodeName === emitIntermediateStateUntilEnd && eventType === "on_chain_end") {
1783
+ emitIntermediateStateUntilEnd = null;
1784
+ }
1785
+ const exitingNode = nodeName === currentNodeName && eventType === "on_chain_end";
1786
+ if (JSON.stringify(updatedState) !== JSON.stringify(state) || prevNodeName != nodeName || exitingNode) {
1787
+ state = updatedState;
1788
+ prevNodeName = nodeName;
1789
+ emit(getStateSyncEvent({
1790
+ threadId,
1791
+ runId,
1792
+ agentName: agent.name,
1793
+ nodeName,
1794
+ state,
1795
+ running: true,
1796
+ active: !exitingNode
1797
+ }));
1798
+ }
1799
+ emit(JSON.stringify(event) + "\n");
1598
1800
  }
1599
- return headers;
1801
+ state = await client.threads.getState(threadId);
1802
+ const isEndNode = state.next.length === 0;
1803
+ nodeName = Object.keys(state.metadata.writes)[0];
1804
+ emit(getStateSyncEvent({
1805
+ threadId,
1806
+ runId: externalRunId,
1807
+ agentName: agent.name,
1808
+ nodeName: isEndNode ? "__end__" : nodeName,
1809
+ state: state.values,
1810
+ running: !shouldExit,
1811
+ active: false
1812
+ }));
1813
+ return Promise.resolve();
1600
1814
  }
1601
- __name(createHeaders, "createHeaders");
1602
- async function fetchRemoteInfo({ url, onBeforeRequest, graphqlContext, logger: logger2, frontendUrl }) {
1603
- logger2.debug({
1604
- url
1605
- }, "Fetching actions from url");
1606
- const headers = createHeaders(onBeforeRequest, graphqlContext);
1607
- try {
1608
- const response = await fetch(`${url}/info`, {
1609
- method: "POST",
1610
- headers,
1611
- body: JSON.stringify({
1612
- properties: graphqlContext.properties,
1613
- frontendUrl
1614
- })
1615
- });
1616
- if (!response.ok) {
1617
- logger2.error({
1618
- url,
1619
- status: response.status,
1620
- body: await response.text()
1621
- }, "Failed to fetch actions from url");
1815
+ __name(streamEvents, "streamEvents");
1816
+ function getStateSyncEvent({ threadId, runId, agentName, nodeName, state, running, active }) {
1817
+ const stateWithoutMessages = Object.keys(state).reduce((acc, key) => {
1818
+ if (key !== "messages") {
1819
+ acc[key] = state[key];
1820
+ }
1821
+ return acc;
1822
+ }, {});
1823
+ return JSON.stringify({
1824
+ event: "on_copilotkit_state_sync",
1825
+ thread_id: threadId,
1826
+ run_id: runId,
1827
+ agent_name: agentName,
1828
+ node_name: nodeName,
1829
+ active,
1830
+ state: stateWithoutMessages,
1831
+ running,
1832
+ role: "assistant"
1833
+ }) + "\n";
1834
+ }
1835
+ __name(getStateSyncEvent, "getStateSyncEvent");
1836
+ var StreamingStateExtractor = /* @__PURE__ */ __name(class StreamingStateExtractor2 {
1837
+ emitIntermediateState;
1838
+ toolCallBuffer;
1839
+ currentToolCall;
1840
+ previouslyParsableState;
1841
+ constructor(emitIntermediateState) {
1842
+ this.emitIntermediateState = emitIntermediateState;
1843
+ this.toolCallBuffer = {};
1844
+ this.currentToolCall = null;
1845
+ this.previouslyParsableState = {};
1846
+ }
1847
+ bufferToolCalls(event) {
1848
+ if (event.data.chunk.tool_call_chunks.length > 0) {
1849
+ const chunk = event.data.chunk.tool_call_chunks[0];
1850
+ if (chunk.name !== null) {
1851
+ this.currentToolCall = chunk.name;
1852
+ this.toolCallBuffer[this.currentToolCall] = chunk.args;
1853
+ } else if (this.currentToolCall !== null) {
1854
+ this.toolCallBuffer[this.currentToolCall] += chunk.args;
1855
+ }
1856
+ }
1857
+ }
1858
+ getEmitStateConfig(currentToolName) {
1859
+ for (const config2 of this.emitIntermediateState) {
1860
+ const stateKey = config2["state_key"];
1861
+ const tool = config2["tool"];
1862
+ const toolArgument = config2["tool_argument"];
1863
+ if (currentToolName === tool) {
1864
+ return [
1865
+ toolArgument,
1866
+ stateKey
1867
+ ];
1868
+ }
1869
+ }
1870
+ return [
1871
+ null,
1872
+ null
1873
+ ];
1874
+ }
1875
+ extractState() {
1876
+ const state = {};
1877
+ for (const [key, value] of Object.entries(this.toolCallBuffer)) {
1878
+ const [argumentName, stateKey] = this.getEmitStateConfig(key);
1879
+ if (stateKey === null) {
1880
+ continue;
1881
+ }
1882
+ let parsedValue;
1883
+ try {
1884
+ parsedValue = (0, import_partial_json.parse)(value);
1885
+ } catch (error) {
1886
+ if (key in this.previouslyParsableState) {
1887
+ parsedValue = this.previouslyParsableState[key];
1888
+ } else {
1889
+ continue;
1890
+ }
1891
+ }
1892
+ this.previouslyParsableState[key] = parsedValue;
1893
+ if (!argumentName) {
1894
+ state[stateKey] = parsedValue;
1895
+ } else {
1896
+ state[stateKey] = parsedValue[argumentName];
1897
+ }
1898
+ }
1899
+ return state;
1900
+ }
1901
+ }, "StreamingStateExtractor");
1902
+ function langGraphDefaultMergeState(state, messages, actions, agentName) {
1903
+ if (messages.length > 0 && "role" in messages[0] && messages[0].role === "system") {
1904
+ messages = messages.slice(1);
1905
+ }
1906
+ const mergedMessages = state.messages || [];
1907
+ const existingMessageIds = new Set(mergedMessages.map((message) => message.id));
1908
+ const existingToolCallResults = /* @__PURE__ */ new Set();
1909
+ for (const message of mergedMessages) {
1910
+ if ("tool_call_id" in message) {
1911
+ existingToolCallResults.add(message.tool_call_id);
1912
+ }
1913
+ }
1914
+ for (const message of messages) {
1915
+ if ("tool_calls" in message && message.tool_calls.length > 0 && message.tool_calls[0].name === agentName) {
1916
+ continue;
1917
+ }
1918
+ if ("name" in message && message.name === agentName) {
1919
+ continue;
1920
+ }
1921
+ if (!existingMessageIds.has(message.id)) {
1922
+ if ("tool_call_id" in message && existingToolCallResults.has(message.tool_call_id)) {
1923
+ console.warn("Warning: Duplicate tool call result, skipping:", message.tool_call_id);
1924
+ continue;
1925
+ }
1926
+ mergedMessages.push(message);
1927
+ } else {
1928
+ for (let i = 0; i < mergedMessages.length; i++) {
1929
+ if (mergedMessages[i].id === message.id) {
1930
+ if ("tool_calls" in message) {
1931
+ if (("tool_calls" in mergedMessages[i] || "additional_kwargs" in mergedMessages[i]) && mergedMessages[i].content) {
1932
+ message.tool_calls = mergedMessages[i]["tool_calls"];
1933
+ message.additional_kwargs = mergedMessages[i].additional_kwargs;
1934
+ }
1935
+ }
1936
+ mergedMessages[i] = message;
1937
+ }
1938
+ }
1939
+ }
1940
+ }
1941
+ for (let i = 0; i < mergedMessages.length - 1; i++) {
1942
+ const currentMessage = mergedMessages[i];
1943
+ const nextMessage = mergedMessages[i + 1];
1944
+ if ("tool_calls" in currentMessage && currentMessage.tool_calls.length > 0 && "tool_call_id" in nextMessage) {
1945
+ nextMessage.tool_call_id = currentMessage.tool_calls[0].id;
1946
+ }
1947
+ }
1948
+ const correctedMessages = [];
1949
+ for (let i = 0; i < mergedMessages.length; i++) {
1950
+ const currentMessage = mergedMessages[i];
1951
+ const nextMessage = mergedMessages[i + 1] || null;
1952
+ const prevMessage = mergedMessages[i - 1] || null;
1953
+ if ("tool_calls" in currentMessage && currentMessage.tool_calls.length > 0) {
1954
+ if (!nextMessage) {
1955
+ console.warn("No next message to auto-correct tool call, skipping:", currentMessage.tool_calls[0].id);
1956
+ continue;
1957
+ }
1958
+ if (!("tool_call_id" in nextMessage) || nextMessage.tool_call_id !== currentMessage.tool_calls[0].id) {
1959
+ const toolMessage = mergedMessages.find((m) => "tool_call_id" in m && m.tool_call_id === currentMessage.tool_calls[0].id);
1960
+ if (toolMessage) {
1961
+ console.warn("Auto-corrected tool call alignment issue:", currentMessage.tool_calls[0].id);
1962
+ correctedMessages.push(currentMessage, toolMessage);
1963
+ continue;
1964
+ } else {
1965
+ console.warn("No corresponding tool call result found for tool call, skipping:", currentMessage.tool_calls[0].id);
1966
+ continue;
1967
+ }
1968
+ }
1969
+ correctedMessages.push(currentMessage);
1970
+ continue;
1971
+ }
1972
+ if ("tool_call_id" in currentMessage) {
1973
+ if (!prevMessage || !("tool_calls" in prevMessage)) {
1974
+ console.warn("No previous tool call, skipping tool call result:", currentMessage.id);
1975
+ continue;
1976
+ }
1977
+ if (prevMessage.tool_calls && prevMessage.tool_calls[0].id !== currentMessage.tool_call_id) {
1978
+ console.warn("Tool call id is incorrect, skipping tool call result:", currentMessage.id);
1979
+ continue;
1980
+ }
1981
+ correctedMessages.push(currentMessage);
1982
+ continue;
1983
+ }
1984
+ correctedMessages.push(currentMessage);
1985
+ }
1986
+ return deepMerge(state, {
1987
+ messages: correctedMessages,
1988
+ copilotkit: {
1989
+ actions
1990
+ }
1991
+ });
1992
+ }
1993
+ __name(langGraphDefaultMergeState, "langGraphDefaultMergeState");
1994
+ function deepMerge(obj1, obj2) {
1995
+ let result = {
1996
+ ...obj1
1997
+ };
1998
+ for (let key in obj2) {
1999
+ if (typeof obj2[key] === "object" && !Array.isArray(obj2[key])) {
2000
+ if (obj1[key]) {
2001
+ result[key] = deepMerge(obj1[key], obj2[key]);
2002
+ } else {
2003
+ result[key] = {
2004
+ ...obj2[key]
2005
+ };
2006
+ }
2007
+ } else {
2008
+ result[key] = obj2[key];
2009
+ }
2010
+ }
2011
+ return result;
2012
+ }
2013
+ __name(deepMerge, "deepMerge");
2014
+ function formatMessages(messages) {
2015
+ return messages.map((message) => {
2016
+ if (message.isTextMessage() && message.role === "assistant") {
2017
+ return message;
2018
+ }
2019
+ if (message.isTextMessage() && message.role === "system") {
2020
+ return message;
2021
+ }
2022
+ if (message.isTextMessage() && message.role === "user") {
2023
+ return message;
2024
+ }
2025
+ if (message.isActionExecutionMessage()) {
2026
+ const toolCall = {
2027
+ name: message.name,
2028
+ args: message.arguments,
2029
+ id: message.id
2030
+ };
1622
2031
  return {
1623
- actions: [],
1624
- agents: []
2032
+ type: message.type,
2033
+ content: "",
2034
+ tool_calls: [
2035
+ toolCall
2036
+ ],
2037
+ role: MessageRole.assistant,
2038
+ id: message.id
1625
2039
  };
1626
2040
  }
1627
- const json = await response.json();
1628
- logger2.debug({
1629
- json
1630
- }, "Fetched actions from url");
1631
- return json;
1632
- } catch (error) {
1633
- logger2.error({
1634
- error: error.message ? error.message : error + ""
1635
- }, "Failed to fetch actions from url");
1636
- return {
1637
- actions: [],
1638
- agents: []
1639
- };
1640
- }
2041
+ if (message.isResultMessage()) {
2042
+ return {
2043
+ type: message.type,
2044
+ content: message.result,
2045
+ id: message.id,
2046
+ tool_call_id: message.actionExecutionId,
2047
+ name: message.actionName,
2048
+ role: MessageRole.assistant
2049
+ };
2050
+ }
2051
+ throw new Error(`Unknown message type ${message.type}`);
2052
+ });
1641
2053
  }
1642
- __name(fetchRemoteInfo, "fetchRemoteInfo");
2054
+ __name(formatMessages, "formatMessages");
2055
+
2056
+ // src/lib/runtime/remote-action-constructors.ts
2057
+ function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, messages, agentStates }) {
2058
+ const agents = endpoint.agents.map((agent) => ({
2059
+ name: agent.name,
2060
+ description: agent.description,
2061
+ parameters: [],
2062
+ handler: async (_args) => {
2063
+ },
2064
+ langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName }) => {
2065
+ var _a;
2066
+ logger2.debug({
2067
+ actionName: agent.name
2068
+ }, "Executing LangGraph Cloud agent");
2069
+ telemetry_client_default.capture("oss.runtime.remote_action_executed", {});
2070
+ let state = {};
2071
+ if (agentStates) {
2072
+ const jsonState = (_a = agentStates.find((state2) => state2.agentName === name)) == null ? void 0 : _a.state;
2073
+ if (jsonState) {
2074
+ state = JSON.parse(jsonState);
2075
+ }
2076
+ }
2077
+ try {
2078
+ const response = await execute({
2079
+ logger: logger2,
2080
+ deploymentUrl: endpoint.deploymentUrl,
2081
+ langsmithApiKey: endpoint.langsmithApiKey,
2082
+ agent,
2083
+ threadId,
2084
+ nodeName,
2085
+ messages,
2086
+ state,
2087
+ properties: graphqlContext.properties,
2088
+ actions: actionInputsWithoutAgents.map((action) => ({
2089
+ name: action.name,
2090
+ description: action.description,
2091
+ parameters: JSON.parse(action.jsonSchema)
2092
+ }))
2093
+ });
2094
+ const eventSource = new RemoteLangGraphEventSource();
2095
+ streamResponse(response, eventSource.eventStream$);
2096
+ return eventSource.processLangGraphEvents();
2097
+ } catch (error) {
2098
+ logger2.error({
2099
+ url: endpoint.deploymentUrl,
2100
+ status: 500,
2101
+ body: error.message
2102
+ }, "Failed to execute LangGraph Cloud agent");
2103
+ throw new Error("Failed to execute LangGraph Cloud agent");
2104
+ }
2105
+ }
2106
+ }));
2107
+ return [
2108
+ ...agents
2109
+ ];
2110
+ }
2111
+ __name(constructLGCRemoteAction, "constructLGCRemoteAction");
1643
2112
  function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, logger: logger2, messages, agentStates }) {
1644
2113
  const actions = json["actions"].map((action) => ({
1645
2114
  name: action.name,
@@ -1731,7 +2200,7 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
1731
2200
  throw new Error("Failed to execute remote agent");
1732
2201
  }
1733
2202
  const eventSource = new RemoteLangGraphEventSource();
1734
- eventSource.streamResponse(response);
2203
+ streamResponse(response.body, eventSource.eventStream$);
1735
2204
  return eventSource.processLangGraphEvents();
1736
2205
  }
1737
2206
  }));
@@ -1741,34 +2210,165 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
1741
2210
  ];
1742
2211
  }
1743
2212
  __name(constructRemoteActions, "constructRemoteActions");
1744
- async function setupRemoteActions({ remoteActionDefinitions, graphqlContext, messages, agentStates, frontendUrl }) {
2213
+ async function streamResponse(response, eventStream$) {
2214
+ const reader = response.getReader();
2215
+ const decoder = new TextDecoder();
2216
+ let buffer = [];
2217
+ function flushBuffer() {
2218
+ const currentBuffer = buffer.join("");
2219
+ if (currentBuffer.trim().length === 0) {
2220
+ return;
2221
+ }
2222
+ const parts = currentBuffer.split("\n");
2223
+ if (parts.length === 0) {
2224
+ return;
2225
+ }
2226
+ const lastPartIsComplete = currentBuffer.endsWith("\n");
2227
+ buffer = [];
2228
+ if (!lastPartIsComplete) {
2229
+ buffer.push(parts.pop());
2230
+ }
2231
+ parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
2232
+ eventStream$.next(JSON.parse(part));
2233
+ });
2234
+ }
2235
+ __name(flushBuffer, "flushBuffer");
2236
+ try {
2237
+ while (true) {
2238
+ const { done, value } = await reader.read();
2239
+ if (!done) {
2240
+ buffer.push(decoder.decode(value, {
2241
+ stream: true
2242
+ }));
2243
+ }
2244
+ flushBuffer();
2245
+ if (done) {
2246
+ break;
2247
+ }
2248
+ }
2249
+ } catch (error) {
2250
+ console.error("Error in stream", error);
2251
+ eventStream$.error(error);
2252
+ return;
2253
+ }
2254
+ eventStream$.complete();
2255
+ }
2256
+ __name(streamResponse, "streamResponse");
2257
+ function createHeaders(onBeforeRequest, graphqlContext) {
2258
+ const headers = {
2259
+ "Content-Type": "application/json"
2260
+ };
2261
+ if (onBeforeRequest) {
2262
+ const { headers: additionalHeaders } = onBeforeRequest({
2263
+ ctx: graphqlContext
2264
+ });
2265
+ if (additionalHeaders) {
2266
+ Object.assign(headers, additionalHeaders);
2267
+ }
2268
+ }
2269
+ return headers;
2270
+ }
2271
+ __name(createHeaders, "createHeaders");
2272
+
2273
+ // src/lib/runtime/remote-actions.ts
2274
+ var EndpointType;
2275
+ (function(EndpointType2) {
2276
+ EndpointType2["CopilotKit"] = "copilotKit";
2277
+ EndpointType2["LangGraphCloud"] = "langgraph-cloud";
2278
+ })(EndpointType || (EndpointType = {}));
2279
+ function isLangGraphAgentAction(action) {
2280
+ if (!action) {
2281
+ return false;
2282
+ }
2283
+ return typeof action.langGraphAgentHandler === "function";
2284
+ }
2285
+ __name(isLangGraphAgentAction, "isLangGraphAgentAction");
2286
+ async function fetchRemoteInfo({ url, onBeforeRequest, graphqlContext, logger: logger2, frontendUrl }) {
2287
+ logger2.debug({
2288
+ url
2289
+ }, "Fetching actions from url");
2290
+ const headers = createHeaders(onBeforeRequest, graphqlContext);
2291
+ try {
2292
+ const response = await fetch(`${url}/info`, {
2293
+ method: "POST",
2294
+ headers,
2295
+ body: JSON.stringify({
2296
+ properties: graphqlContext.properties,
2297
+ frontendUrl
2298
+ })
2299
+ });
2300
+ if (!response.ok) {
2301
+ logger2.error({
2302
+ url,
2303
+ status: response.status,
2304
+ body: await response.text()
2305
+ }, "Failed to fetch actions from url");
2306
+ return {
2307
+ actions: [],
2308
+ agents: []
2309
+ };
2310
+ }
2311
+ const json = await response.json();
2312
+ logger2.debug({
2313
+ json
2314
+ }, "Fetched actions from url");
2315
+ return json;
2316
+ } catch (error) {
2317
+ logger2.error({
2318
+ error: error.message ? error.message : error + ""
2319
+ }, "Failed to fetch actions from url");
2320
+ return {
2321
+ actions: [],
2322
+ agents: []
2323
+ };
2324
+ }
2325
+ }
2326
+ __name(fetchRemoteInfo, "fetchRemoteInfo");
2327
+ async function setupRemoteActions({ remoteEndpointDefinitions, graphqlContext, messages, agentStates, frontendUrl }) {
1745
2328
  const logger2 = graphqlContext.logger.child({
1746
2329
  component: "remote-actions.fetchRemoteActions"
1747
2330
  });
1748
2331
  logger2.debug({
1749
- remoteActionDefinitions
1750
- }, "Fetching remote actions");
1751
- const filtered = remoteActionDefinitions.filter((value, index, self) => index === self.findIndex((t) => t.url === value.url));
1752
- const result = await Promise.all(filtered.map(async (actionDefinition) => {
2332
+ remoteEndpointDefinitions
2333
+ }, "Fetching from remote endpoints");
2334
+ const filtered = remoteEndpointDefinitions.filter((value, index, self) => {
2335
+ if (value.type === "langgraph-cloud") {
2336
+ return value;
2337
+ }
2338
+ return index === self.findIndex((t) => t.url === value.url);
2339
+ });
2340
+ const result = await Promise.all(filtered.map(async (endpoint) => {
2341
+ if (endpoint.type === "langgraph-cloud") {
2342
+ return constructLGCRemoteAction({
2343
+ endpoint,
2344
+ messages,
2345
+ graphqlContext,
2346
+ logger: logger2.child({
2347
+ component: "remote-actions.constructLGCRemoteAction",
2348
+ endpoint
2349
+ }),
2350
+ agentStates
2351
+ });
2352
+ }
1753
2353
  const json = await fetchRemoteInfo({
1754
- url: actionDefinition.url,
1755
- onBeforeRequest: actionDefinition.onBeforeRequest,
2354
+ url: endpoint.url,
2355
+ onBeforeRequest: endpoint.onBeforeRequest,
1756
2356
  graphqlContext,
1757
2357
  logger: logger2.child({
1758
2358
  component: "remote-actions.fetchActionsFromUrl",
1759
- actionDefinition
2359
+ endpoint
1760
2360
  }),
1761
2361
  frontendUrl
1762
2362
  });
1763
2363
  return constructRemoteActions({
1764
2364
  json,
1765
2365
  messages,
1766
- url: actionDefinition.url,
1767
- onBeforeRequest: actionDefinition.onBeforeRequest,
2366
+ url: endpoint.url,
2367
+ onBeforeRequest: endpoint.onBeforeRequest,
1768
2368
  graphqlContext,
1769
2369
  logger: logger2.child({
1770
2370
  component: "remote-actions.constructActions",
1771
- actionDefinition
2371
+ endpoint
1772
2372
  }),
1773
2373
  agentStates
1774
2374
  });
@@ -1966,7 +2566,7 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
1966
2566
  __name(executeAction, "executeAction");
1967
2567
 
1968
2568
  // src/graphql/types/base/index.ts
1969
- var import_type_graphql = require("type-graphql");
2569
+ var import_type_graphql2 = require("type-graphql");
1970
2570
  function _ts_decorate(decorators, target, key, desc) {
1971
2571
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1972
2572
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -1989,15 +2589,15 @@ var BaseMessageInput = class {
1989
2589
  };
1990
2590
  __name(BaseMessageInput, "BaseMessageInput");
1991
2591
  _ts_decorate([
1992
- (0, import_type_graphql.Field)(() => String),
2592
+ (0, import_type_graphql2.Field)(() => String),
1993
2593
  _ts_metadata("design:type", String)
1994
2594
  ], BaseMessageInput.prototype, "id", void 0);
1995
2595
  _ts_decorate([
1996
- (0, import_type_graphql.Field)(() => Date),
2596
+ (0, import_type_graphql2.Field)(() => Date),
1997
2597
  _ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
1998
2598
  ], BaseMessageInput.prototype, "createdAt", void 0);
1999
2599
  BaseMessageInput = _ts_decorate([
2000
- (0, import_type_graphql.InputType)()
2600
+ (0, import_type_graphql2.InputType)()
2001
2601
  ], BaseMessageInput);
2002
2602
 
2003
2603
  // src/graphql/types/converted/index.ts
@@ -2101,7 +2701,7 @@ __name(convertGqlInputToMessages, "convertGqlInputToMessages");
2101
2701
  var import_rxjs3 = require("rxjs");
2102
2702
  var CopilotRuntime = class {
2103
2703
  actions;
2104
- remoteActionDefinitions;
2704
+ remoteEndpointDefinitions;
2105
2705
  langserve = [];
2106
2706
  onBeforeRequest;
2107
2707
  onAfterRequest;
@@ -2112,7 +2712,7 @@ var CopilotRuntime = class {
2112
2712
  const remoteChain = new RemoteChain(chain);
2113
2713
  this.langserve.push(remoteChain.toAction());
2114
2714
  }
2115
- this.remoteActionDefinitions = (params == null ? void 0 : params.remoteActions) || [];
2715
+ this.remoteEndpointDefinitions = (params == null ? void 0 : params.remoteEndpoints) || [];
2116
2716
  this.onBeforeRequest = (_a = params == null ? void 0 : params.middleware) == null ? void 0 : _a.onBeforeRequest;
2117
2717
  this.onAfterRequest = (_b = params == null ? void 0 : params.middleware) == null ? void 0 : _b.onAfterRequest;
2118
2718
  }
@@ -2181,7 +2781,7 @@ var CopilotRuntime = class {
2181
2781
  async processAgentRequest(request) {
2182
2782
  var _a;
2183
2783
  const { messages: rawMessages, outputMessagesPromise, graphqlContext, agentSession } = request;
2184
- const { threadId = (0, import_shared9.randomId)(), agentName, nodeName } = agentSession;
2784
+ const { threadId, agentName, nodeName } = agentSession;
2185
2785
  const serverSideActions = await this.getServerSideActions(request);
2186
2786
  const messages = convertGqlInputToMessages(rawMessages);
2187
2787
  const agent = serverSideActions.find((action) => action.name === agentName && isLangGraphAgentAction(action));
@@ -2253,8 +2853,12 @@ var CopilotRuntime = class {
2253
2853
  console.error("Error loading langserve chain:", error);
2254
2854
  }
2255
2855
  }
2856
+ const remoteEndpointDefinitions = this.remoteEndpointDefinitions.map((endpoint) => ({
2857
+ ...endpoint,
2858
+ type: this.resolveEndpointType(endpoint)
2859
+ }));
2256
2860
  const remoteActions = await setupRemoteActions({
2257
- remoteActionDefinitions: this.remoteActionDefinitions,
2861
+ remoteEndpointDefinitions,
2258
2862
  graphqlContext,
2259
2863
  messages: inputMessages,
2260
2864
  agentStates,
@@ -2270,6 +2874,12 @@ var CopilotRuntime = class {
2270
2874
  ...remoteActions
2271
2875
  ];
2272
2876
  }
2877
+ resolveEndpointType(endpoint) {
2878
+ if (!endpoint.type && "langsmithApiKey" in endpoint && "deploymentUrl" in endpoint && "agents" in endpoint) {
2879
+ return EndpointType.LangGraphCloud;
2880
+ }
2881
+ return endpoint.type;
2882
+ }
2273
2883
  };
2274
2884
  __name(CopilotRuntime, "CopilotRuntime");
2275
2885
  function flattenToolCallsNoDuplicates(toolsByPriority) {
@@ -2284,6 +2894,20 @@ function flattenToolCallsNoDuplicates(toolsByPriority) {
2284
2894
  return allTools;
2285
2895
  }
2286
2896
  __name(flattenToolCallsNoDuplicates, "flattenToolCallsNoDuplicates");
2897
+ function copilotKitEndpoint(config2) {
2898
+ return {
2899
+ ...config2,
2900
+ type: EndpointType.CopilotKit
2901
+ };
2902
+ }
2903
+ __name(copilotKitEndpoint, "copilotKitEndpoint");
2904
+ function langGraphCloudEndpoint(config2) {
2905
+ return {
2906
+ ...config2,
2907
+ type: EndpointType.LangGraphCloud
2908
+ };
2909
+ }
2910
+ __name(langGraphCloudEndpoint, "langGraphCloudEndpoint");
2287
2911
 
2288
2912
  // src/lib/integrations/shared.ts
2289
2913
  var import_type_graphql16 = require("type-graphql");
@@ -2297,43 +2921,6 @@ var import_type_graphql11 = require("type-graphql");
2297
2921
 
2298
2922
  // src/graphql/inputs/message.input.ts
2299
2923
  var import_type_graphql3 = require("type-graphql");
2300
-
2301
- // src/graphql/types/enums.ts
2302
- var import_type_graphql2 = require("type-graphql");
2303
- var MessageRole;
2304
- (function(MessageRole2) {
2305
- MessageRole2["user"] = "user";
2306
- MessageRole2["assistant"] = "assistant";
2307
- MessageRole2["system"] = "system";
2308
- })(MessageRole || (MessageRole = {}));
2309
- var ActionExecutionScope;
2310
- (function(ActionExecutionScope2) {
2311
- ActionExecutionScope2["server"] = "server";
2312
- ActionExecutionScope2["client"] = "client";
2313
- ActionExecutionScope2["passThrough"] = "passThrough";
2314
- })(ActionExecutionScope || (ActionExecutionScope = {}));
2315
- var CopilotRequestType;
2316
- (function(CopilotRequestType2) {
2317
- CopilotRequestType2["Chat"] = "Chat";
2318
- CopilotRequestType2["Task"] = "Task";
2319
- CopilotRequestType2["TextareaCompletion"] = "TextareaCompletion";
2320
- CopilotRequestType2["TextareaPopover"] = "TextareaPopover";
2321
- CopilotRequestType2["Suggestion"] = "Suggestion";
2322
- })(CopilotRequestType || (CopilotRequestType = {}));
2323
- (0, import_type_graphql2.registerEnumType)(MessageRole, {
2324
- name: "MessageRole",
2325
- description: "The role of the message"
2326
- });
2327
- (0, import_type_graphql2.registerEnumType)(ActionExecutionScope, {
2328
- name: "ActionExecutionScope",
2329
- description: "The scope of the action"
2330
- });
2331
- (0, import_type_graphql2.registerEnumType)(CopilotRequestType, {
2332
- name: "CopilotRequestType",
2333
- description: "The type of Copilot request"
2334
- });
2335
-
2336
- // src/graphql/inputs/message.input.ts
2337
2924
  function _ts_decorate2(decorators, target, key, desc) {
2338
2925
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2339
2926
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -3995,6 +4582,7 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
3995
4582
  UnknownErrorResponse,
3996
4583
  buildSchema,
3997
4584
  config,
4585
+ copilotKitEndpoint,
3998
4586
  copilotRuntimeNestEndpoint,
3999
4587
  copilotRuntimeNextJSAppRouterEndpoint,
4000
4588
  copilotRuntimeNextJSPagesRouterEndpoint,
@@ -4002,6 +4590,7 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
4002
4590
  copilotRuntimeNodeHttpEndpoint,
4003
4591
  createContext,
4004
4592
  flattenToolCallsNoDuplicates,
4005
- getCommonConfig
4593
+ getCommonConfig,
4594
+ langGraphCloudEndpoint
4006
4595
  });
4007
4596
  //# sourceMappingURL=index.js.map