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