@copilotkit/runtime 1.3.16-mme-reset-chat.3 → 1.4.0-feat-langgraph-cloud.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/CHANGELOG.md +4 -25
  2. package/__snapshots__/schema/schema.graphql +1 -0
  3. package/dist/{chunk-JCKX6XUV.mjs → chunk-AEUECXAW.mjs} +675 -151
  4. package/dist/chunk-AEUECXAW.mjs.map +1 -0
  5. package/dist/{chunk-MYZHUCL6.mjs → chunk-B74M7FXG.mjs} +1 -1
  6. package/dist/chunk-B74M7FXG.mjs.map +1 -0
  7. package/dist/{chunk-PQQNLA6C.mjs → chunk-BLB2XDIL.mjs} +2 -2
  8. package/dist/{chunk-7ADMUSBN.mjs → chunk-DP7YGMSZ.mjs} +37 -10
  9. package/dist/chunk-DP7YGMSZ.mjs.map +1 -0
  10. package/dist/{chunk-NHVRGUZ6.mjs → chunk-ITCPKBTK.mjs} +2 -2
  11. package/dist/{chunk-SMVZ2NQP.mjs → chunk-VGYN7TGZ.mjs} +19 -29
  12. package/dist/{chunk-SMVZ2NQP.mjs.map → chunk-VGYN7TGZ.mjs.map} +1 -1
  13. package/dist/{chunk-PNSKIPTP.mjs → chunk-WQFBGT3B.mjs} +2 -2
  14. package/dist/{copilot-runtime-df3527ad.d.ts → copilot-runtime-543a59ae.d.ts} +29 -7
  15. package/dist/graphql/types/converted/index.d.ts +1 -1
  16. package/dist/graphql/types/converted/index.js.map +1 -1
  17. package/dist/graphql/types/converted/index.mjs +1 -1
  18. package/dist/{groq-adapter-b122e71f.d.ts → groq-adapter-7aa25931.d.ts} +4 -2
  19. package/dist/{index-cff31380.d.ts → index-83ee522f.d.ts} +3 -2
  20. package/dist/index.d.ts +4 -4
  21. package/dist/index.js +765 -224
  22. package/dist/index.js.map +1 -1
  23. package/dist/index.mjs +13 -9
  24. package/dist/index.mjs.map +1 -1
  25. package/dist/{langserve-a14a6849.d.ts → langserve-f00629d2.d.ts} +1 -1
  26. package/dist/lib/index.d.ts +4 -4
  27. package/dist/lib/index.js +765 -224
  28. package/dist/lib/index.js.map +1 -1
  29. package/dist/lib/index.mjs +13 -9
  30. package/dist/lib/integrations/index.d.ts +4 -4
  31. package/dist/lib/integrations/index.js +4 -1
  32. package/dist/lib/integrations/index.js.map +1 -1
  33. package/dist/lib/integrations/index.mjs +5 -5
  34. package/dist/lib/integrations/nest/index.d.ts +3 -3
  35. package/dist/lib/integrations/nest/index.js +4 -1
  36. package/dist/lib/integrations/nest/index.js.map +1 -1
  37. package/dist/lib/integrations/nest/index.mjs +3 -3
  38. package/dist/lib/integrations/node-express/index.d.ts +3 -3
  39. package/dist/lib/integrations/node-express/index.js +4 -1
  40. package/dist/lib/integrations/node-express/index.js.map +1 -1
  41. package/dist/lib/integrations/node-express/index.mjs +3 -3
  42. package/dist/lib/integrations/node-http/index.d.ts +3 -3
  43. package/dist/lib/integrations/node-http/index.js +4 -1
  44. package/dist/lib/integrations/node-http/index.js.map +1 -1
  45. package/dist/lib/integrations/node-http/index.mjs +2 -2
  46. package/dist/service-adapters/index.d.ts +4 -4
  47. package/dist/service-adapters/index.js +18 -28
  48. package/dist/service-adapters/index.js.map +1 -1
  49. package/dist/service-adapters/index.mjs +1 -1
  50. package/package.json +6 -4
  51. package/src/agents/langgraph/event-source.ts +62 -94
  52. package/src/agents/langgraph/events.ts +27 -17
  53. package/src/graphql/types/converted/index.ts +5 -1
  54. package/src/graphql/types/enums.ts +1 -0
  55. package/src/lib/runtime/copilot-runtime.ts +58 -11
  56. package/src/lib/runtime/remote-action-constructors.ts +284 -0
  57. package/src/lib/runtime/remote-actions.ts +65 -159
  58. package/src/lib/runtime/remote-lg-cloud-action.ts +613 -0
  59. package/src/service-adapters/google/google-genai-adapter.ts +2 -2
  60. package/src/service-adapters/langchain/langchain-adapter.ts +19 -23
  61. package/dist/chunk-7ADMUSBN.mjs.map +0 -1
  62. package/dist/chunk-JCKX6XUV.mjs.map +0 -1
  63. package/dist/chunk-MYZHUCL6.mjs.map +0 -1
  64. /package/dist/{chunk-PQQNLA6C.mjs.map → chunk-BLB2XDIL.mjs.map} +0 -0
  65. /package/dist/{chunk-NHVRGUZ6.mjs.map → chunk-ITCPKBTK.mjs.map} +0 -0
  66. /package/dist/{chunk-PNSKIPTP.mjs.map → chunk-WQFBGT3B.mjs.map} +0 -0
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.16-mme-reset-chat.3",
47
+ version: "1.4.0-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");
@@ -674,7 +678,6 @@ __name(encodeResult, "encodeResult");
674
678
 
675
679
  // src/service-adapters/langchain/langchain-adapter.ts
676
680
  var import_shared3 = require("@copilotkit/shared");
677
- var import_promises = require("@langchain/core/callbacks/promises");
678
681
  var LangChainAdapter = class {
679
682
  options;
680
683
  /**
@@ -684,28 +687,23 @@ var LangChainAdapter = class {
684
687
  this.options = options;
685
688
  }
686
689
  async process(request) {
687
- try {
688
- const { eventSource, model, actions, messages, runId } = request;
689
- const threadId = request.threadId ?? (0, import_shared3.randomId)();
690
- const result = await this.options.chainFn({
691
- messages: messages.map(convertMessageToLangChainMessage),
692
- tools: actions.map(convertActionInputToLangChainTool),
693
- model,
694
- threadId,
695
- runId
696
- });
697
- eventSource.stream(async (eventStream$) => {
698
- await streamLangChainResponse({
699
- result,
700
- eventStream$
701
- });
690
+ const { eventSource, model, actions, messages, threadId, runId } = request;
691
+ const result = await this.options.chainFn({
692
+ messages: messages.map(convertMessageToLangChainMessage),
693
+ tools: actions.map(convertActionInputToLangChainTool),
694
+ model,
695
+ threadId,
696
+ runId
697
+ });
698
+ eventSource.stream(async (eventStream$) => {
699
+ await streamLangChainResponse({
700
+ result,
701
+ eventStream$
702
702
  });
703
- return {
704
- threadId
705
- };
706
- } finally {
707
- await (0, import_promises.awaitAllCallbacks)();
708
- }
703
+ });
704
+ return {
705
+ threadId: threadId || (0, import_shared3.randomId)()
706
+ };
709
707
  }
710
708
  };
711
709
  __name(LangChainAdapter, "LangChainAdapter");
@@ -715,16 +713,12 @@ var import_google_gauth = require("@langchain/google-gauth");
715
713
  var GoogleGenerativeAIAdapter = class extends LangChainAdapter {
716
714
  constructor(options) {
717
715
  super({
718
- chainFn: async ({ messages, tools, threadId }) => {
716
+ chainFn: async ({ messages, tools }) => {
719
717
  const model = new import_google_gauth.ChatGoogle({
720
718
  modelName: (options == null ? void 0 : options.model) ?? "gemini-1.5-pro",
721
719
  apiVersion: "v1beta"
722
720
  }).bindTools(tools);
723
- return model.stream(messages, {
724
- metadata: {
725
- conversation_id: threadId
726
- }
727
- });
721
+ return model.stream(messages);
728
722
  }
729
723
  });
730
724
  }
@@ -1360,58 +1354,20 @@ var LangGraphEventTypes;
1360
1354
  LangGraphEventTypes2["OnToolStart"] = "on_tool_start";
1361
1355
  LangGraphEventTypes2["OnToolEnd"] = "on_tool_end";
1362
1356
  LangGraphEventTypes2["OnCopilotKitStateSync"] = "on_copilotkit_state_sync";
1363
- LangGraphEventTypes2["OnCopilotKitEmitMessage"] = "on_copilotkit_emit_message";
1364
- LangGraphEventTypes2["OnCopilotKitEmitToolCall"] = "on_copilotkit_emit_tool_call";
1357
+ LangGraphEventTypes2["OnCustomEvent"] = "on_custom_event";
1365
1358
  })(LangGraphEventTypes || (LangGraphEventTypes = {}));
1359
+ var CustomEventNames;
1360
+ (function(CustomEventNames2) {
1361
+ CustomEventNames2["CopilotKitManuallyEmitMessage"] = "copilotkit_manually_emit_message";
1362
+ CustomEventNames2["CopilotKitManuallyEmitToolCall"] = "copilotkit_manually_emit_tool_call";
1363
+ CustomEventNames2["CopilotKitManuallyEmitIntermediateState"] = "copilotkit_manually_emit_intermediate_state";
1364
+ CustomEventNames2["CopilotKitExit"] = "copilotkit_exit";
1365
+ })(CustomEventNames || (CustomEventNames = {}));
1366
1366
 
1367
1367
  // src/agents/langgraph/event-source.ts
1368
1368
  var import_shared8 = require("@copilotkit/shared");
1369
1369
  var RemoteLangGraphEventSource = class {
1370
1370
  eventStream$ = new import_rxjs.ReplaySubject();
1371
- async streamResponse(response) {
1372
- const reader = response.body.getReader();
1373
- const decoder = new TextDecoder();
1374
- let buffer = [];
1375
- const eventStream$ = this.eventStream$;
1376
- function flushBuffer() {
1377
- const currentBuffer = buffer.join("");
1378
- if (currentBuffer.trim().length === 0) {
1379
- return;
1380
- }
1381
- const parts = currentBuffer.split("\n");
1382
- if (parts.length === 0) {
1383
- return;
1384
- }
1385
- const lastPartIsComplete = currentBuffer.endsWith("\n");
1386
- buffer = [];
1387
- if (!lastPartIsComplete) {
1388
- buffer.push(parts.pop());
1389
- }
1390
- parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
1391
- eventStream$.next(JSON.parse(part));
1392
- });
1393
- }
1394
- __name(flushBuffer, "flushBuffer");
1395
- try {
1396
- while (true) {
1397
- const { done, value } = await reader.read();
1398
- if (!done) {
1399
- buffer.push(decoder.decode(value, {
1400
- stream: true
1401
- }));
1402
- }
1403
- flushBuffer();
1404
- if (done) {
1405
- break;
1406
- }
1407
- }
1408
- } catch (error) {
1409
- console.error("Error in stream", error);
1410
- eventStream$.error(error);
1411
- return;
1412
- }
1413
- eventStream$.complete();
1414
- }
1415
1371
  shouldEmitToolCall(shouldEmitToolCalls, toolCallName) {
1416
1372
  if (typeof shouldEmitToolCalls === "boolean") {
1417
1373
  return shouldEmitToolCalls;
@@ -1434,20 +1390,25 @@ var RemoteLangGraphEventSource = class {
1434
1390
  } else {
1435
1391
  acc.content = null;
1436
1392
  }
1437
- if ((_h = (_g = (_f = event.data) == null ? void 0 : _f.chunk) == null ? void 0 : _g.kwargs) == null ? void 0 : _h.tool_call_chunks) {
1393
+ const toolCallChunks = (
1394
+ // @ts-expect-error -- LangGraph Cloud implementation stores data outside of kwargs
1395
+ ((_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)
1396
+ );
1397
+ 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);
1398
+ if (toolCallChunks && toolCallChunks.length > 0) {
1438
1399
  acc.prevToolCallMessageId = acc.toolCallMessageId;
1439
- acc.toolCallMessageId = (_i = event.data.chunk.kwargs) == null ? void 0 : _i.id;
1440
- if ((_j = event.data.chunk.kwargs.tool_call_chunks[0]) == null ? void 0 : _j.name) {
1441
- acc.toolCallName = event.data.chunk.kwargs.tool_call_chunks[0].name;
1400
+ acc.toolCallMessageId = toolCallMessageId;
1401
+ if ((_p = toolCallChunks[0]) == null ? void 0 : _p.name) {
1402
+ acc.toolCallName = toolCallChunks[0].name;
1442
1403
  }
1443
- if ((_k = event.data.chunk.kwargs.tool_call_chunks[0]) == null ? void 0 : _k.id) {
1444
- acc.toolCallId = event.data.chunk.kwargs.tool_call_chunks[0].id;
1404
+ if ((_q = toolCallChunks[0]) == null ? void 0 : _q.id) {
1405
+ acc.toolCallId = toolCallChunks[0].id;
1445
1406
  }
1446
1407
  acc.prevMessageId = acc.messageId;
1447
- acc.messageId = (_n = (_m = (_l = event.data) == null ? void 0 : _l.chunk) == null ? void 0 : _m.kwargs) == null ? void 0 : _n.id;
1408
+ acc.messageId = toolCallMessageId;
1448
1409
  } else if (acc.content && acc.content != "") {
1449
1410
  acc.prevMessageId = acc.messageId;
1450
- acc.messageId = (_q = (_p = (_o = event.data) == null ? void 0 : _o.chunk) == null ? void 0 : _p.kwargs) == null ? void 0 : _q.id;
1411
+ acc.messageId = toolCallMessageId;
1451
1412
  } else {
1452
1413
  acc.prevToolCallMessageId = acc.toolCallMessageId;
1453
1414
  acc.prevMessageId = acc.messageId;
@@ -1472,7 +1433,7 @@ var RemoteLangGraphEventSource = class {
1472
1433
  prevMessageId: null,
1473
1434
  content: null
1474
1435
  }), (0, import_rxjs.mergeMap)((eventWithState) => {
1475
- var _a, _b, _c, _d, _e;
1436
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1476
1437
  const events = [];
1477
1438
  let shouldEmitMessages = true;
1478
1439
  let shouldEmitToolCalls = false;
@@ -1495,32 +1456,33 @@ var RemoteLangGraphEventSource = class {
1495
1456
  });
1496
1457
  }
1497
1458
  switch (eventWithState.event.event) {
1498
- case LangGraphEventTypes.OnCopilotKitEmitMessage:
1499
- events.push({
1500
- type: RuntimeEventTypes.TextMessageStart,
1501
- messageId: eventWithState.event.message_id
1502
- });
1503
- events.push({
1504
- type: RuntimeEventTypes.TextMessageContent,
1505
- content: eventWithState.event.message
1506
- });
1507
- events.push({
1508
- type: RuntimeEventTypes.TextMessageEnd
1509
- });
1510
- break;
1511
- case LangGraphEventTypes.OnCopilotKitEmitToolCall:
1512
- events.push({
1513
- type: RuntimeEventTypes.ActionExecutionStart,
1514
- actionExecutionId: eventWithState.event.id,
1515
- actionName: eventWithState.event.name
1516
- });
1517
- events.push({
1518
- type: RuntimeEventTypes.ActionExecutionArgs,
1519
- args: JSON.stringify(eventWithState.event.args)
1520
- });
1521
- events.push({
1522
- type: RuntimeEventTypes.ActionExecutionEnd
1523
- });
1459
+ case LangGraphEventTypes.OnCustomEvent:
1460
+ if (eventWithState.event.name === CustomEventNames.CopilotKitManuallyEmitMessage) {
1461
+ events.push({
1462
+ type: RuntimeEventTypes.TextMessageStart,
1463
+ messageId: eventWithState.event.data.message_id
1464
+ });
1465
+ events.push({
1466
+ type: RuntimeEventTypes.TextMessageContent,
1467
+ content: eventWithState.event.data.message
1468
+ });
1469
+ events.push({
1470
+ type: RuntimeEventTypes.TextMessageEnd
1471
+ });
1472
+ } else if (eventWithState.event.name === CustomEventNames.CopilotKitManuallyEmitToolCall) {
1473
+ events.push({
1474
+ type: RuntimeEventTypes.ActionExecutionStart,
1475
+ actionExecutionId: eventWithState.event.data.id,
1476
+ actionName: eventWithState.event.data.name
1477
+ });
1478
+ events.push({
1479
+ type: RuntimeEventTypes.ActionExecutionArgs,
1480
+ args: JSON.stringify(eventWithState.event.data.args)
1481
+ });
1482
+ events.push({
1483
+ type: RuntimeEventTypes.ActionExecutionEnd
1484
+ });
1485
+ }
1524
1486
  break;
1525
1487
  case LangGraphEventTypes.OnCopilotKitStateSync:
1526
1488
  events.push({
@@ -1555,7 +1517,8 @@ var RemoteLangGraphEventSource = class {
1555
1517
  });
1556
1518
  }
1557
1519
  }
1558
- 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;
1520
+ 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
1521
+ ((_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);
1559
1522
  const content = eventWithState.content;
1560
1523
  if (args) {
1561
1524
  if (this.shouldEmitToolCall(shouldEmitToolCalls, eventWithState.toolCallName)) {
@@ -1606,70 +1569,528 @@ var RemoteLangGraphEventSource = class {
1606
1569
  };
1607
1570
  __name(RemoteLangGraphEventSource, "RemoteLangGraphEventSource");
1608
1571
 
1609
- // src/lib/runtime/remote-actions.ts
1610
- function isLangGraphAgentAction(action) {
1611
- if (!action) {
1612
- return false;
1613
- }
1614
- return typeof action.langGraphAgentHandler === "function";
1572
+ // src/lib/runtime/remote-lg-cloud-action.ts
1573
+ var import_langgraph_sdk = require("@langchain/langgraph-sdk");
1574
+ var import_node_crypto = require("crypto");
1575
+ var import_partial_json = require("partial-json");
1576
+
1577
+ // src/graphql/types/enums.ts
1578
+ var import_type_graphql = require("type-graphql");
1579
+ var MessageRole;
1580
+ (function(MessageRole2) {
1581
+ MessageRole2["user"] = "user";
1582
+ MessageRole2["assistant"] = "assistant";
1583
+ MessageRole2["system"] = "system";
1584
+ MessageRole2["tool"] = "tool";
1585
+ })(MessageRole || (MessageRole = {}));
1586
+ var ActionExecutionScope;
1587
+ (function(ActionExecutionScope2) {
1588
+ ActionExecutionScope2["server"] = "server";
1589
+ ActionExecutionScope2["client"] = "client";
1590
+ ActionExecutionScope2["passThrough"] = "passThrough";
1591
+ })(ActionExecutionScope || (ActionExecutionScope = {}));
1592
+ var CopilotRequestType;
1593
+ (function(CopilotRequestType2) {
1594
+ CopilotRequestType2["Chat"] = "Chat";
1595
+ CopilotRequestType2["Task"] = "Task";
1596
+ CopilotRequestType2["TextareaCompletion"] = "TextareaCompletion";
1597
+ CopilotRequestType2["TextareaPopover"] = "TextareaPopover";
1598
+ CopilotRequestType2["Suggestion"] = "Suggestion";
1599
+ })(CopilotRequestType || (CopilotRequestType = {}));
1600
+ (0, import_type_graphql.registerEnumType)(MessageRole, {
1601
+ name: "MessageRole",
1602
+ description: "The role of the message"
1603
+ });
1604
+ (0, import_type_graphql.registerEnumType)(ActionExecutionScope, {
1605
+ name: "ActionExecutionScope",
1606
+ description: "The scope of the action"
1607
+ });
1608
+ (0, import_type_graphql.registerEnumType)(CopilotRequestType, {
1609
+ name: "CopilotRequestType",
1610
+ description: "The type of Copilot request"
1611
+ });
1612
+
1613
+ // src/lib/runtime/remote-lg-cloud-action.ts
1614
+ async function execute(args) {
1615
+ return new ReadableStream({
1616
+ async start(controller) {
1617
+ try {
1618
+ await streamEvents(controller, args);
1619
+ controller.close();
1620
+ } catch (err) {
1621
+ }
1622
+ }
1623
+ });
1615
1624
  }
1616
- __name(isLangGraphAgentAction, "isLangGraphAgentAction");
1617
- function createHeaders(onBeforeRequest, graphqlContext) {
1618
- const headers = {
1619
- "Content-Type": "application/json"
1625
+ __name(execute, "execute");
1626
+ async function streamEvents(controller, args) {
1627
+ const { deploymentUrl, langsmithApiKey, threadId: agrsInitialThreadId, agent, nodeName: initialNodeName, state: initialState, messages, actions, logger: logger2 } = args;
1628
+ let nodeName = initialNodeName;
1629
+ let state = initialState;
1630
+ const { name, assistantId: initialAssistantId } = agent;
1631
+ const client = new import_langgraph_sdk.Client({
1632
+ apiUrl: deploymentUrl,
1633
+ apiKey: langsmithApiKey
1634
+ });
1635
+ let initialThreadId = agrsInitialThreadId;
1636
+ const wasInitiatedWithExistingThread = !!initialThreadId;
1637
+ if (initialThreadId && initialThreadId.startsWith("ck-")) {
1638
+ initialThreadId = initialThreadId.substring(3);
1639
+ }
1640
+ const assistants = await client.assistants.search();
1641
+ const retrievedAssistant = assistants.find((a) => a.name === name);
1642
+ const threadId = initialThreadId ?? (0, import_node_crypto.randomUUID)();
1643
+ if (initialThreadId === threadId) {
1644
+ await client.threads.get(threadId);
1645
+ } else {
1646
+ await client.threads.create({
1647
+ threadId
1648
+ });
1649
+ }
1650
+ let agentState = {
1651
+ values: {}
1620
1652
  };
1621
- if (onBeforeRequest) {
1622
- const { headers: additionalHeaders } = onBeforeRequest({
1623
- ctx: graphqlContext
1653
+ if (wasInitiatedWithExistingThread) {
1654
+ agentState = await client.threads.getState(threadId);
1655
+ }
1656
+ const agentStateValues = agentState.values;
1657
+ state.messages = agentStateValues.messages;
1658
+ const mode = wasInitiatedWithExistingThread && nodeName != "__end__" ? "continue" : "start";
1659
+ let formattedMessages = [];
1660
+ try {
1661
+ formattedMessages = formatMessages(messages);
1662
+ } catch (e) {
1663
+ logger2.error(e, `Error event thrown: ${e.message}`);
1664
+ }
1665
+ state = langGraphDefaultMergeState(state, formattedMessages, actions, name);
1666
+ if (mode === "continue") {
1667
+ await client.threads.updateState(threadId, {
1668
+ values: state,
1669
+ asNode: nodeName
1624
1670
  });
1625
- if (additionalHeaders) {
1626
- Object.assign(headers, additionalHeaders);
1671
+ }
1672
+ const assistantId = initialAssistantId ?? (retrievedAssistant == null ? void 0 : retrievedAssistant.assistant_id);
1673
+ if (!assistantId) {
1674
+ console.error(`
1675
+ No agent found for the agent name specified in CopilotKit provider
1676
+ Please check your available agents or provide an agent ID in the LangGraph Cloud endpoint definition.
1677
+
1678
+
1679
+ These are the available agents: [${assistants.map((a) => `${a.name} (ID: ${a.assistant_id})`).join(", ")}]
1680
+ `);
1681
+ throw new Error("No agent id found");
1682
+ }
1683
+ const graphInfo = await client.assistants.getGraph(assistantId);
1684
+ const streamInput = mode === "start" ? state : null;
1685
+ let streamingStateExtractor = new StreamingStateExtractor([]);
1686
+ let prevNodeName = null;
1687
+ let emitIntermediateStateUntilEnd = null;
1688
+ let shouldExit = null;
1689
+ let externalRunId = null;
1690
+ const streamResponse2 = client.runs.stream(threadId, assistantId, {
1691
+ input: streamInput,
1692
+ streamMode: [
1693
+ "events",
1694
+ "values"
1695
+ ]
1696
+ });
1697
+ const emit = /* @__PURE__ */ __name((message) => controller.enqueue(new TextEncoder().encode(message)), "emit");
1698
+ let latestStateValues = {};
1699
+ try {
1700
+ for await (const chunk of streamResponse2) {
1701
+ if (![
1702
+ "events",
1703
+ "values",
1704
+ "error"
1705
+ ].includes(chunk.event))
1706
+ continue;
1707
+ if (chunk.event === "error") {
1708
+ logger2.error(chunk, `Error event thrown: ${chunk.data.message}`);
1709
+ throw new Error(`Error event thrown: ${chunk.data.message}`);
1710
+ }
1711
+ if (chunk.event === "values") {
1712
+ latestStateValues = chunk.data;
1713
+ continue;
1714
+ }
1715
+ const event = chunk.data;
1716
+ const currentNodeName = event.name;
1717
+ const eventType = event.event;
1718
+ const runId = event.metadata.run_id;
1719
+ externalRunId = runId;
1720
+ const metadata = event.metadata;
1721
+ shouldExit = shouldExit != null ? shouldExit : eventType === LangGraphEventTypes.OnCustomEvent && event.name === CustomEventNames.CopilotKitExit;
1722
+ const emitIntermediateState = metadata["copilotkit:emit-intermediate-state"];
1723
+ const manuallyEmitIntermediateState = eventType === LangGraphEventTypes.OnCustomEvent && event.name === CustomEventNames.CopilotKitManuallyEmitIntermediateState;
1724
+ if (graphInfo["nodes"].some((node) => node.id === currentNodeName)) {
1725
+ nodeName = currentNodeName;
1726
+ }
1727
+ if (!nodeName) {
1728
+ continue;
1729
+ }
1730
+ if (manuallyEmitIntermediateState) {
1731
+ if (eventType === LangGraphEventTypes.OnChainEnd) {
1732
+ state = event.data.output;
1733
+ emit(getStateSyncEvent({
1734
+ threadId,
1735
+ runId,
1736
+ agentName: agent.name,
1737
+ nodeName,
1738
+ state: event.data.output,
1739
+ running: true,
1740
+ active: true
1741
+ }));
1742
+ }
1743
+ continue;
1744
+ }
1745
+ if (emitIntermediateState && emitIntermediateStateUntilEnd == null) {
1746
+ emitIntermediateStateUntilEnd = nodeName;
1747
+ }
1748
+ if (emitIntermediateState && eventType === LangGraphEventTypes.OnChatModelStart) {
1749
+ streamingStateExtractor = new StreamingStateExtractor(emitIntermediateState);
1750
+ }
1751
+ let updatedState = latestStateValues;
1752
+ if (emitIntermediateState && eventType === LangGraphEventTypes.OnChatModelStream) {
1753
+ streamingStateExtractor.bufferToolCalls(event);
1754
+ }
1755
+ if (emitIntermediateStateUntilEnd !== null) {
1756
+ updatedState = {
1757
+ ...updatedState,
1758
+ ...streamingStateExtractor.extractState()
1759
+ };
1760
+ }
1761
+ if (!emitIntermediateState && currentNodeName === emitIntermediateStateUntilEnd && eventType === LangGraphEventTypes.OnChainEnd) {
1762
+ emitIntermediateStateUntilEnd = null;
1763
+ }
1764
+ const exitingNode = nodeName === currentNodeName && eventType === LangGraphEventTypes.OnChainEnd;
1765
+ if (JSON.stringify(updatedState) !== JSON.stringify(state) || prevNodeName != nodeName || exitingNode) {
1766
+ state = updatedState;
1767
+ prevNodeName = nodeName;
1768
+ emit(getStateSyncEvent({
1769
+ threadId,
1770
+ runId,
1771
+ agentName: agent.name,
1772
+ nodeName,
1773
+ state,
1774
+ running: true,
1775
+ active: !exitingNode
1776
+ }));
1777
+ }
1778
+ emit(JSON.stringify(event) + "\n");
1627
1779
  }
1780
+ state = await client.threads.getState(threadId);
1781
+ const isEndNode = state.next.length === 0;
1782
+ nodeName = Object.keys(state.metadata.writes)[0];
1783
+ emit(getStateSyncEvent({
1784
+ threadId,
1785
+ runId: externalRunId,
1786
+ agentName: agent.name,
1787
+ nodeName: isEndNode ? "__end__" : nodeName,
1788
+ state: state.values,
1789
+ running: !shouldExit,
1790
+ active: false
1791
+ }));
1792
+ return Promise.resolve();
1793
+ } catch (e) {
1794
+ return Promise.resolve();
1628
1795
  }
1629
- return headers;
1630
1796
  }
1631
- __name(createHeaders, "createHeaders");
1632
- async function fetchRemoteInfo({ url, onBeforeRequest, graphqlContext, logger: logger2, frontendUrl }) {
1633
- logger2.debug({
1634
- url
1635
- }, "Fetching actions from url");
1636
- const headers = createHeaders(onBeforeRequest, graphqlContext);
1637
- try {
1638
- const response = await fetch(`${url}/info`, {
1639
- method: "POST",
1640
- headers,
1641
- body: JSON.stringify({
1642
- properties: graphqlContext.properties,
1643
- frontendUrl
1644
- })
1645
- });
1646
- if (!response.ok) {
1647
- logger2.error({
1648
- url,
1649
- status: response.status,
1650
- body: await response.text()
1651
- }, "Failed to fetch actions from url");
1797
+ __name(streamEvents, "streamEvents");
1798
+ function getStateSyncEvent({ threadId, runId, agentName, nodeName, state, running, active }) {
1799
+ const stateWithoutMessages = Object.keys(state).reduce((acc, key) => {
1800
+ if (key !== "messages") {
1801
+ acc[key] = state[key];
1802
+ }
1803
+ return acc;
1804
+ }, {});
1805
+ return JSON.stringify({
1806
+ event: LangGraphEventTypes.OnCopilotKitStateSync,
1807
+ thread_id: threadId,
1808
+ run_id: runId,
1809
+ agent_name: agentName,
1810
+ node_name: nodeName,
1811
+ active,
1812
+ state: stateWithoutMessages,
1813
+ running,
1814
+ role: "assistant"
1815
+ }) + "\n";
1816
+ }
1817
+ __name(getStateSyncEvent, "getStateSyncEvent");
1818
+ var StreamingStateExtractor = /* @__PURE__ */ __name(class StreamingStateExtractor2 {
1819
+ emitIntermediateState;
1820
+ toolCallBuffer;
1821
+ currentToolCall;
1822
+ previouslyParsableState;
1823
+ constructor(emitIntermediateState) {
1824
+ this.emitIntermediateState = emitIntermediateState;
1825
+ this.toolCallBuffer = {};
1826
+ this.currentToolCall = null;
1827
+ this.previouslyParsableState = {};
1828
+ }
1829
+ bufferToolCalls(event) {
1830
+ if (event.data.chunk.tool_call_chunks.length > 0) {
1831
+ const chunk = event.data.chunk.tool_call_chunks[0];
1832
+ if (chunk.name !== null) {
1833
+ this.currentToolCall = chunk.name;
1834
+ this.toolCallBuffer[this.currentToolCall] = chunk.args;
1835
+ } else if (this.currentToolCall !== null) {
1836
+ this.toolCallBuffer[this.currentToolCall] += chunk.args;
1837
+ }
1838
+ }
1839
+ }
1840
+ getEmitStateConfig(currentToolName) {
1841
+ for (const config2 of this.emitIntermediateState) {
1842
+ const stateKey = config2["state_key"];
1843
+ const tool = config2["tool"];
1844
+ const toolArgument = config2["tool_argument"];
1845
+ if (currentToolName === tool) {
1846
+ return [
1847
+ toolArgument,
1848
+ stateKey
1849
+ ];
1850
+ }
1851
+ }
1852
+ return [
1853
+ null,
1854
+ null
1855
+ ];
1856
+ }
1857
+ extractState() {
1858
+ const state = {};
1859
+ for (const [key, value] of Object.entries(this.toolCallBuffer)) {
1860
+ const [argumentName, stateKey] = this.getEmitStateConfig(key);
1861
+ if (stateKey === null) {
1862
+ continue;
1863
+ }
1864
+ let parsedValue;
1865
+ try {
1866
+ parsedValue = (0, import_partial_json.parse)(value);
1867
+ } catch (error) {
1868
+ if (key in this.previouslyParsableState) {
1869
+ parsedValue = this.previouslyParsableState[key];
1870
+ } else {
1871
+ continue;
1872
+ }
1873
+ }
1874
+ this.previouslyParsableState[key] = parsedValue;
1875
+ if (!argumentName) {
1876
+ state[stateKey] = parsedValue;
1877
+ } else {
1878
+ state[stateKey] = parsedValue[argumentName];
1879
+ }
1880
+ }
1881
+ return state;
1882
+ }
1883
+ }, "StreamingStateExtractor");
1884
+ function langGraphDefaultMergeState(state, messages, actions, agentName) {
1885
+ if (messages.length > 0 && "role" in messages[0] && messages[0].role === "system") {
1886
+ messages = messages.slice(1);
1887
+ }
1888
+ const mergedMessages = state.messages || [];
1889
+ const existingMessageIds = new Set(mergedMessages.map((message) => message.id));
1890
+ const existingToolCallResults = /* @__PURE__ */ new Set();
1891
+ for (const message of mergedMessages) {
1892
+ if ("tool_call_id" in message) {
1893
+ existingToolCallResults.add(message.tool_call_id);
1894
+ }
1895
+ }
1896
+ for (const message of messages) {
1897
+ if ("tool_calls" in message && message.tool_calls.length > 0 && message.tool_calls[0].name === agentName) {
1898
+ continue;
1899
+ }
1900
+ if ("name" in message && message.name === agentName) {
1901
+ continue;
1902
+ }
1903
+ if (!existingMessageIds.has(message.id)) {
1904
+ if ("tool_call_id" in message && existingToolCallResults.has(message.tool_call_id)) {
1905
+ console.warn("Warning: Duplicate tool call result, skipping:", message.tool_call_id);
1906
+ continue;
1907
+ }
1908
+ mergedMessages.push(message);
1909
+ } else {
1910
+ for (let i = 0; i < mergedMessages.length; i++) {
1911
+ if (mergedMessages[i].id === message.id) {
1912
+ if ("tool_calls" in message) {
1913
+ if (("tool_calls" in mergedMessages[i] || "additional_kwargs" in mergedMessages[i]) && mergedMessages[i].content) {
1914
+ message.tool_calls = mergedMessages[i]["tool_calls"];
1915
+ message.additional_kwargs = mergedMessages[i].additional_kwargs;
1916
+ }
1917
+ }
1918
+ mergedMessages[i] = message;
1919
+ }
1920
+ }
1921
+ }
1922
+ }
1923
+ for (let i = 0; i < mergedMessages.length - 1; i++) {
1924
+ const currentMessage = mergedMessages[i];
1925
+ const nextMessage = mergedMessages[i + 1];
1926
+ if ("tool_calls" in currentMessage && currentMessage.tool_calls.length > 0 && "tool_call_id" in nextMessage) {
1927
+ nextMessage.tool_call_id = currentMessage.tool_calls[0].id;
1928
+ }
1929
+ }
1930
+ const correctedMessages = [];
1931
+ for (let i = 0; i < mergedMessages.length; i++) {
1932
+ const currentMessage = mergedMessages[i];
1933
+ const nextMessage = mergedMessages[i + 1] || null;
1934
+ const prevMessage = mergedMessages[i - 1] || null;
1935
+ if ("tool_calls" in currentMessage && currentMessage.tool_calls.length > 0) {
1936
+ if (!nextMessage) {
1937
+ console.warn("No next message to auto-correct tool call, skipping:", currentMessage.tool_calls[0].id);
1938
+ continue;
1939
+ }
1940
+ if (!("tool_call_id" in nextMessage) || nextMessage.tool_call_id !== currentMessage.tool_calls[0].id) {
1941
+ const toolMessage = mergedMessages.find((m) => "tool_call_id" in m && m.tool_call_id === currentMessage.tool_calls[0].id);
1942
+ if (toolMessage) {
1943
+ console.warn("Auto-corrected tool call alignment issue:", currentMessage.tool_calls[0].id);
1944
+ correctedMessages.push(currentMessage, toolMessage);
1945
+ continue;
1946
+ } else {
1947
+ console.warn("No corresponding tool call result found for tool call, skipping:", currentMessage.tool_calls[0].id);
1948
+ continue;
1949
+ }
1950
+ }
1951
+ correctedMessages.push(currentMessage);
1952
+ continue;
1953
+ }
1954
+ if ("tool_call_id" in currentMessage) {
1955
+ if (!prevMessage || !("tool_calls" in prevMessage)) {
1956
+ console.warn("No previous tool call, skipping tool call result:", currentMessage.id);
1957
+ continue;
1958
+ }
1959
+ if (prevMessage.tool_calls && prevMessage.tool_calls[0].id !== currentMessage.tool_call_id) {
1960
+ console.warn("Tool call id is incorrect, skipping tool call result:", currentMessage.id);
1961
+ continue;
1962
+ }
1963
+ correctedMessages.push(currentMessage);
1964
+ continue;
1965
+ }
1966
+ correctedMessages.push(currentMessage);
1967
+ }
1968
+ return deepMerge(state, {
1969
+ messages: correctedMessages,
1970
+ copilotkit: {
1971
+ actions
1972
+ }
1973
+ });
1974
+ }
1975
+ __name(langGraphDefaultMergeState, "langGraphDefaultMergeState");
1976
+ function deepMerge(obj1, obj2) {
1977
+ let result = {
1978
+ ...obj1
1979
+ };
1980
+ for (let key in obj2) {
1981
+ if (typeof obj2[key] === "object" && !Array.isArray(obj2[key])) {
1982
+ if (obj1[key]) {
1983
+ result[key] = deepMerge(obj1[key], obj2[key]);
1984
+ } else {
1985
+ result[key] = {
1986
+ ...obj2[key]
1987
+ };
1988
+ }
1989
+ } else {
1990
+ result[key] = obj2[key];
1991
+ }
1992
+ }
1993
+ return result;
1994
+ }
1995
+ __name(deepMerge, "deepMerge");
1996
+ function formatMessages(messages) {
1997
+ return messages.map((message) => {
1998
+ if (message.isTextMessage() && message.role === "assistant") {
1999
+ return message;
2000
+ }
2001
+ if (message.isTextMessage() && message.role === "system") {
2002
+ return message;
2003
+ }
2004
+ if (message.isTextMessage() && message.role === "user") {
2005
+ return message;
2006
+ }
2007
+ if (message.isActionExecutionMessage()) {
2008
+ const toolCall = {
2009
+ name: message.name,
2010
+ args: message.arguments,
2011
+ id: message.id
2012
+ };
1652
2013
  return {
1653
- actions: [],
1654
- agents: []
2014
+ type: message.type,
2015
+ content: "",
2016
+ tool_calls: [
2017
+ toolCall
2018
+ ],
2019
+ role: MessageRole.assistant,
2020
+ id: message.id
1655
2021
  };
1656
2022
  }
1657
- const json = await response.json();
1658
- logger2.debug({
1659
- json
1660
- }, "Fetched actions from url");
1661
- return json;
1662
- } catch (error) {
1663
- logger2.error({
1664
- error: error.message ? error.message : error + ""
1665
- }, "Failed to fetch actions from url");
1666
- return {
1667
- actions: [],
1668
- agents: []
1669
- };
1670
- }
2023
+ if (message.isResultMessage()) {
2024
+ return {
2025
+ type: message.type,
2026
+ content: message.result,
2027
+ id: message.id,
2028
+ tool_call_id: message.actionExecutionId,
2029
+ name: message.actionName,
2030
+ role: MessageRole.tool
2031
+ };
2032
+ }
2033
+ throw new Error(`Unknown message type ${message.type}`);
2034
+ });
1671
2035
  }
1672
- __name(fetchRemoteInfo, "fetchRemoteInfo");
2036
+ __name(formatMessages, "formatMessages");
2037
+
2038
+ // src/lib/runtime/remote-action-constructors.ts
2039
+ function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, messages, agentStates }) {
2040
+ const agents = endpoint.agents.map((agent) => ({
2041
+ name: agent.name,
2042
+ description: agent.description,
2043
+ parameters: [],
2044
+ handler: async (_args) => {
2045
+ },
2046
+ langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName }) => {
2047
+ var _a;
2048
+ logger2.debug({
2049
+ actionName: agent.name
2050
+ }, "Executing LangGraph Cloud agent");
2051
+ telemetry_client_default.capture("oss.runtime.remote_action_executed", {});
2052
+ let state = {};
2053
+ if (agentStates) {
2054
+ const jsonState = (_a = agentStates.find((state2) => state2.agentName === name)) == null ? void 0 : _a.state;
2055
+ if (jsonState) {
2056
+ state = JSON.parse(jsonState);
2057
+ }
2058
+ }
2059
+ try {
2060
+ const response = await execute({
2061
+ logger: logger2,
2062
+ deploymentUrl: endpoint.deploymentUrl,
2063
+ langsmithApiKey: endpoint.langsmithApiKey,
2064
+ agent,
2065
+ threadId,
2066
+ nodeName,
2067
+ messages,
2068
+ state,
2069
+ properties: graphqlContext.properties,
2070
+ actions: actionInputsWithoutAgents.map((action) => ({
2071
+ name: action.name,
2072
+ description: action.description,
2073
+ parameters: JSON.parse(action.jsonSchema)
2074
+ }))
2075
+ });
2076
+ const eventSource = new RemoteLangGraphEventSource();
2077
+ streamResponse(response, eventSource.eventStream$);
2078
+ return eventSource.processLangGraphEvents();
2079
+ } catch (error) {
2080
+ logger2.error({
2081
+ url: endpoint.deploymentUrl,
2082
+ status: 500,
2083
+ body: error.message
2084
+ }, "Failed to execute LangGraph Cloud agent");
2085
+ throw new Error("Failed to execute LangGraph Cloud agent");
2086
+ }
2087
+ }
2088
+ }));
2089
+ return [
2090
+ ...agents
2091
+ ];
2092
+ }
2093
+ __name(constructLGCRemoteAction, "constructLGCRemoteAction");
1673
2094
  function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, logger: logger2, messages, agentStates }) {
1674
2095
  const actions = json["actions"].map((action) => ({
1675
2096
  name: action.name,
@@ -1761,7 +2182,7 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
1761
2182
  throw new Error("Failed to execute remote agent");
1762
2183
  }
1763
2184
  const eventSource = new RemoteLangGraphEventSource();
1764
- eventSource.streamResponse(response);
2185
+ streamResponse(response.body, eventSource.eventStream$);
1765
2186
  return eventSource.processLangGraphEvents();
1766
2187
  }
1767
2188
  }));
@@ -1771,34 +2192,165 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
1771
2192
  ];
1772
2193
  }
1773
2194
  __name(constructRemoteActions, "constructRemoteActions");
1774
- async function setupRemoteActions({ remoteActionDefinitions, graphqlContext, messages, agentStates, frontendUrl }) {
2195
+ async function streamResponse(response, eventStream$) {
2196
+ const reader = response.getReader();
2197
+ const decoder = new TextDecoder();
2198
+ let buffer = [];
2199
+ function flushBuffer() {
2200
+ const currentBuffer = buffer.join("");
2201
+ if (currentBuffer.trim().length === 0) {
2202
+ return;
2203
+ }
2204
+ const parts = currentBuffer.split("\n");
2205
+ if (parts.length === 0) {
2206
+ return;
2207
+ }
2208
+ const lastPartIsComplete = currentBuffer.endsWith("\n");
2209
+ buffer = [];
2210
+ if (!lastPartIsComplete) {
2211
+ buffer.push(parts.pop());
2212
+ }
2213
+ parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
2214
+ eventStream$.next(JSON.parse(part));
2215
+ });
2216
+ }
2217
+ __name(flushBuffer, "flushBuffer");
2218
+ try {
2219
+ while (true) {
2220
+ const { done, value } = await reader.read();
2221
+ if (!done) {
2222
+ buffer.push(decoder.decode(value, {
2223
+ stream: true
2224
+ }));
2225
+ }
2226
+ flushBuffer();
2227
+ if (done) {
2228
+ break;
2229
+ }
2230
+ }
2231
+ } catch (error) {
2232
+ console.error("Error in stream", error);
2233
+ eventStream$.error(error);
2234
+ return;
2235
+ }
2236
+ eventStream$.complete();
2237
+ }
2238
+ __name(streamResponse, "streamResponse");
2239
+ function createHeaders(onBeforeRequest, graphqlContext) {
2240
+ const headers = {
2241
+ "Content-Type": "application/json"
2242
+ };
2243
+ if (onBeforeRequest) {
2244
+ const { headers: additionalHeaders } = onBeforeRequest({
2245
+ ctx: graphqlContext
2246
+ });
2247
+ if (additionalHeaders) {
2248
+ Object.assign(headers, additionalHeaders);
2249
+ }
2250
+ }
2251
+ return headers;
2252
+ }
2253
+ __name(createHeaders, "createHeaders");
2254
+
2255
+ // src/lib/runtime/remote-actions.ts
2256
+ var EndpointType;
2257
+ (function(EndpointType2) {
2258
+ EndpointType2["CopilotKit"] = "copilotKit";
2259
+ EndpointType2["LangGraphCloud"] = "langgraph-cloud";
2260
+ })(EndpointType || (EndpointType = {}));
2261
+ function isLangGraphAgentAction(action) {
2262
+ if (!action) {
2263
+ return false;
2264
+ }
2265
+ return typeof action.langGraphAgentHandler === "function";
2266
+ }
2267
+ __name(isLangGraphAgentAction, "isLangGraphAgentAction");
2268
+ async function fetchRemoteInfo({ url, onBeforeRequest, graphqlContext, logger: logger2, frontendUrl }) {
2269
+ logger2.debug({
2270
+ url
2271
+ }, "Fetching actions from url");
2272
+ const headers = createHeaders(onBeforeRequest, graphqlContext);
2273
+ try {
2274
+ const response = await fetch(`${url}/info`, {
2275
+ method: "POST",
2276
+ headers,
2277
+ body: JSON.stringify({
2278
+ properties: graphqlContext.properties,
2279
+ frontendUrl
2280
+ })
2281
+ });
2282
+ if (!response.ok) {
2283
+ logger2.error({
2284
+ url,
2285
+ status: response.status,
2286
+ body: await response.text()
2287
+ }, "Failed to fetch actions from url");
2288
+ return {
2289
+ actions: [],
2290
+ agents: []
2291
+ };
2292
+ }
2293
+ const json = await response.json();
2294
+ logger2.debug({
2295
+ json
2296
+ }, "Fetched actions from url");
2297
+ return json;
2298
+ } catch (error) {
2299
+ logger2.error({
2300
+ error: error.message ? error.message : error + ""
2301
+ }, "Failed to fetch actions from url");
2302
+ return {
2303
+ actions: [],
2304
+ agents: []
2305
+ };
2306
+ }
2307
+ }
2308
+ __name(fetchRemoteInfo, "fetchRemoteInfo");
2309
+ async function setupRemoteActions({ remoteEndpointDefinitions, graphqlContext, messages, agentStates, frontendUrl }) {
1775
2310
  const logger2 = graphqlContext.logger.child({
1776
2311
  component: "remote-actions.fetchRemoteActions"
1777
2312
  });
1778
2313
  logger2.debug({
1779
- remoteActionDefinitions
1780
- }, "Fetching remote actions");
1781
- const filtered = remoteActionDefinitions.filter((value, index, self) => index === self.findIndex((t) => t.url === value.url));
1782
- const result = await Promise.all(filtered.map(async (actionDefinition) => {
2314
+ remoteEndpointDefinitions
2315
+ }, "Fetching from remote endpoints");
2316
+ const filtered = remoteEndpointDefinitions.filter((value, index, self) => {
2317
+ if (value.type === "langgraph-cloud") {
2318
+ return value;
2319
+ }
2320
+ return index === self.findIndex((t) => t.url === value.url);
2321
+ });
2322
+ const result = await Promise.all(filtered.map(async (endpoint) => {
2323
+ if (endpoint.type === "langgraph-cloud") {
2324
+ return constructLGCRemoteAction({
2325
+ endpoint,
2326
+ messages,
2327
+ graphqlContext,
2328
+ logger: logger2.child({
2329
+ component: "remote-actions.constructLGCRemoteAction",
2330
+ endpoint
2331
+ }),
2332
+ agentStates
2333
+ });
2334
+ }
1783
2335
  const json = await fetchRemoteInfo({
1784
- url: actionDefinition.url,
1785
- onBeforeRequest: actionDefinition.onBeforeRequest,
2336
+ url: endpoint.url,
2337
+ onBeforeRequest: endpoint.onBeforeRequest,
1786
2338
  graphqlContext,
1787
2339
  logger: logger2.child({
1788
2340
  component: "remote-actions.fetchActionsFromUrl",
1789
- actionDefinition
2341
+ endpoint
1790
2342
  }),
1791
2343
  frontendUrl
1792
2344
  });
1793
2345
  return constructRemoteActions({
1794
2346
  json,
1795
2347
  messages,
1796
- url: actionDefinition.url,
1797
- onBeforeRequest: actionDefinition.onBeforeRequest,
2348
+ url: endpoint.url,
2349
+ onBeforeRequest: endpoint.onBeforeRequest,
1798
2350
  graphqlContext,
1799
2351
  logger: logger2.child({
1800
2352
  component: "remote-actions.constructActions",
1801
- actionDefinition
2353
+ endpoint
1802
2354
  }),
1803
2355
  agentStates
1804
2356
  });
@@ -1996,7 +2548,7 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
1996
2548
  __name(executeAction, "executeAction");
1997
2549
 
1998
2550
  // src/graphql/types/base/index.ts
1999
- var import_type_graphql = require("type-graphql");
2551
+ var import_type_graphql2 = require("type-graphql");
2000
2552
  function _ts_decorate(decorators, target, key, desc) {
2001
2553
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2002
2554
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -2019,15 +2571,15 @@ var BaseMessageInput = class {
2019
2571
  };
2020
2572
  __name(BaseMessageInput, "BaseMessageInput");
2021
2573
  _ts_decorate([
2022
- (0, import_type_graphql.Field)(() => String),
2574
+ (0, import_type_graphql2.Field)(() => String),
2023
2575
  _ts_metadata("design:type", String)
2024
2576
  ], BaseMessageInput.prototype, "id", void 0);
2025
2577
  _ts_decorate([
2026
- (0, import_type_graphql.Field)(() => Date),
2578
+ (0, import_type_graphql2.Field)(() => Date),
2027
2579
  _ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
2028
2580
  ], BaseMessageInput.prototype, "createdAt", void 0);
2029
2581
  BaseMessageInput = _ts_decorate([
2030
- (0, import_type_graphql.InputType)()
2582
+ (0, import_type_graphql2.InputType)()
2031
2583
  ], BaseMessageInput);
2032
2584
 
2033
2585
  // src/graphql/types/converted/index.ts
@@ -2131,7 +2683,7 @@ __name(convertGqlInputToMessages, "convertGqlInputToMessages");
2131
2683
  var import_rxjs3 = require("rxjs");
2132
2684
  var CopilotRuntime = class {
2133
2685
  actions;
2134
- remoteActionDefinitions;
2686
+ remoteEndpointDefinitions;
2135
2687
  langserve = [];
2136
2688
  onBeforeRequest;
2137
2689
  onAfterRequest;
@@ -2142,7 +2694,7 @@ var CopilotRuntime = class {
2142
2694
  const remoteChain = new RemoteChain(chain);
2143
2695
  this.langserve.push(remoteChain.toAction());
2144
2696
  }
2145
- this.remoteActionDefinitions = (params == null ? void 0 : params.remoteActions) || [];
2697
+ this.remoteEndpointDefinitions = (params == null ? void 0 : params.remoteEndpoints) || [];
2146
2698
  this.onBeforeRequest = (_a = params == null ? void 0 : params.middleware) == null ? void 0 : _a.onBeforeRequest;
2147
2699
  this.onAfterRequest = (_b = params == null ? void 0 : params.middleware) == null ? void 0 : _b.onAfterRequest;
2148
2700
  }
@@ -2211,7 +2763,7 @@ var CopilotRuntime = class {
2211
2763
  async processAgentRequest(request) {
2212
2764
  var _a;
2213
2765
  const { messages: rawMessages, outputMessagesPromise, graphqlContext, agentSession } = request;
2214
- const { threadId = (0, import_shared9.randomId)(), agentName, nodeName } = agentSession;
2766
+ const { threadId, agentName, nodeName } = agentSession;
2215
2767
  const serverSideActions = await this.getServerSideActions(request);
2216
2768
  const messages = convertGqlInputToMessages(rawMessages);
2217
2769
  const agent = serverSideActions.find((action) => action.name === agentName && isLangGraphAgentAction(action));
@@ -2283,8 +2835,12 @@ var CopilotRuntime = class {
2283
2835
  console.error("Error loading langserve chain:", error);
2284
2836
  }
2285
2837
  }
2838
+ const remoteEndpointDefinitions = this.remoteEndpointDefinitions.map((endpoint) => ({
2839
+ ...endpoint,
2840
+ type: this.resolveEndpointType(endpoint)
2841
+ }));
2286
2842
  const remoteActions = await setupRemoteActions({
2287
- remoteActionDefinitions: this.remoteActionDefinitions,
2843
+ remoteEndpointDefinitions,
2288
2844
  graphqlContext,
2289
2845
  messages: inputMessages,
2290
2846
  agentStates,
@@ -2300,6 +2856,12 @@ var CopilotRuntime = class {
2300
2856
  ...remoteActions
2301
2857
  ];
2302
2858
  }
2859
+ resolveEndpointType(endpoint) {
2860
+ if (!endpoint.type && "langsmithApiKey" in endpoint && "deploymentUrl" in endpoint && "agents" in endpoint) {
2861
+ return EndpointType.LangGraphCloud;
2862
+ }
2863
+ return endpoint.type;
2864
+ }
2303
2865
  };
2304
2866
  __name(CopilotRuntime, "CopilotRuntime");
2305
2867
  function flattenToolCallsNoDuplicates(toolsByPriority) {
@@ -2314,6 +2876,20 @@ function flattenToolCallsNoDuplicates(toolsByPriority) {
2314
2876
  return allTools;
2315
2877
  }
2316
2878
  __name(flattenToolCallsNoDuplicates, "flattenToolCallsNoDuplicates");
2879
+ function copilotKitEndpoint(config2) {
2880
+ return {
2881
+ ...config2,
2882
+ type: EndpointType.CopilotKit
2883
+ };
2884
+ }
2885
+ __name(copilotKitEndpoint, "copilotKitEndpoint");
2886
+ function langGraphCloudEndpoint(config2) {
2887
+ return {
2888
+ ...config2,
2889
+ type: EndpointType.LangGraphCloud
2890
+ };
2891
+ }
2892
+ __name(langGraphCloudEndpoint, "langGraphCloudEndpoint");
2317
2893
 
2318
2894
  // src/lib/integrations/shared.ts
2319
2895
  var import_type_graphql16 = require("type-graphql");
@@ -2327,43 +2903,6 @@ var import_type_graphql11 = require("type-graphql");
2327
2903
 
2328
2904
  // src/graphql/inputs/message.input.ts
2329
2905
  var import_type_graphql3 = require("type-graphql");
2330
-
2331
- // src/graphql/types/enums.ts
2332
- var import_type_graphql2 = require("type-graphql");
2333
- var MessageRole;
2334
- (function(MessageRole2) {
2335
- MessageRole2["user"] = "user";
2336
- MessageRole2["assistant"] = "assistant";
2337
- MessageRole2["system"] = "system";
2338
- })(MessageRole || (MessageRole = {}));
2339
- var ActionExecutionScope;
2340
- (function(ActionExecutionScope2) {
2341
- ActionExecutionScope2["server"] = "server";
2342
- ActionExecutionScope2["client"] = "client";
2343
- ActionExecutionScope2["passThrough"] = "passThrough";
2344
- })(ActionExecutionScope || (ActionExecutionScope = {}));
2345
- var CopilotRequestType;
2346
- (function(CopilotRequestType2) {
2347
- CopilotRequestType2["Chat"] = "Chat";
2348
- CopilotRequestType2["Task"] = "Task";
2349
- CopilotRequestType2["TextareaCompletion"] = "TextareaCompletion";
2350
- CopilotRequestType2["TextareaPopover"] = "TextareaPopover";
2351
- CopilotRequestType2["Suggestion"] = "Suggestion";
2352
- })(CopilotRequestType || (CopilotRequestType = {}));
2353
- (0, import_type_graphql2.registerEnumType)(MessageRole, {
2354
- name: "MessageRole",
2355
- description: "The role of the message"
2356
- });
2357
- (0, import_type_graphql2.registerEnumType)(ActionExecutionScope, {
2358
- name: "ActionExecutionScope",
2359
- description: "The scope of the action"
2360
- });
2361
- (0, import_type_graphql2.registerEnumType)(CopilotRequestType, {
2362
- name: "CopilotRequestType",
2363
- description: "The type of Copilot request"
2364
- });
2365
-
2366
- // src/graphql/inputs/message.input.ts
2367
2906
  function _ts_decorate2(decorators, target, key, desc) {
2368
2907
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2369
2908
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -4025,6 +4564,7 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
4025
4564
  UnknownErrorResponse,
4026
4565
  buildSchema,
4027
4566
  config,
4567
+ copilotKitEndpoint,
4028
4568
  copilotRuntimeNestEndpoint,
4029
4569
  copilotRuntimeNextJSAppRouterEndpoint,
4030
4570
  copilotRuntimeNextJSPagesRouterEndpoint,
@@ -4032,6 +4572,7 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
4032
4572
  copilotRuntimeNodeHttpEndpoint,
4033
4573
  createContext,
4034
4574
  flattenToolCallsNoDuplicates,
4035
- getCommonConfig
4575
+ getCommonConfig,
4576
+ langGraphCloudEndpoint
4036
4577
  });
4037
4578
  //# sourceMappingURL=index.js.map