@copilotkit/runtime 1.3.16-mme-azure-openai.0 → 1.3.16-mme-sdk-js.1

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 (67) hide show
  1. package/CHANGELOG.md +11 -3
  2. package/__snapshots__/schema/schema.graphql +1 -0
  3. package/dist/{chunk-MKZBH3Y6.mjs → chunk-2LR3IZ2Q.mjs} +2 -2
  4. package/dist/{chunk-MYZHUCL6.mjs → chunk-B74M7FXG.mjs} +1 -1
  5. package/dist/chunk-B74M7FXG.mjs.map +1 -0
  6. package/dist/{chunk-MVG266E4.mjs → chunk-BTAHXIKM.mjs} +37 -10
  7. package/dist/chunk-BTAHXIKM.mjs.map +1 -0
  8. package/dist/{chunk-CRSCH25P.mjs → chunk-CVLU3KWZ.mjs} +2 -2
  9. package/dist/{chunk-S5SYJ6JC.mjs → chunk-L23AVL2R.mjs} +2 -2
  10. package/dist/{chunk-YVZORQSA.mjs → chunk-VQXA3N5D.mjs} +675 -151
  11. package/dist/chunk-VQXA3N5D.mjs.map +1 -0
  12. package/dist/{chunk-BHSRGDL6.mjs → chunk-XE3SYKK4.mjs} +1 -4
  13. package/dist/chunk-XE3SYKK4.mjs.map +1 -0
  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-798aff23.d.ts → groq-adapter-7aa25931.d.ts} +1 -34
  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 +747 -199
  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 +747 -199
  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 +0 -3
  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 +614 -0
  59. package/src/service-adapters/openai/openai-adapter.ts +0 -37
  60. package/src/service-adapters/openai/utils.ts +13 -9
  61. package/dist/chunk-BHSRGDL6.mjs.map +0 -1
  62. package/dist/chunk-MVG266E4.mjs.map +0 -1
  63. package/dist/chunk-MYZHUCL6.mjs.map +0 -1
  64. package/dist/chunk-YVZORQSA.mjs.map +0 -1
  65. /package/dist/{chunk-MKZBH3Y6.mjs.map → chunk-2LR3IZ2Q.mjs.map} +0 -0
  66. /package/dist/{chunk-CRSCH25P.mjs.map → chunk-CVLU3KWZ.mjs.map} +0 -0
  67. /package/dist/{chunk-S5SYJ6JC.mjs.map → chunk-L23AVL2R.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-azure-openai.0",
47
+ version: "1.3.16-mme-sdk-js.1",
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");
@@ -426,9 +430,6 @@ var OpenAIAdapter = class {
426
430
  var _a, _b;
427
431
  let mode = null;
428
432
  for await (const chunk of stream) {
429
- if (chunk.choices.length === 0) {
430
- continue;
431
- }
432
433
  const toolCall = (_a = chunk.choices[0].delta.tool_calls) == null ? void 0 : _a[0];
433
434
  const content = chunk.choices[0].delta.content;
434
435
  if (mode === "message" && (toolCall == null ? void 0 : toolCall.id)) {
@@ -1353,58 +1354,20 @@ var LangGraphEventTypes;
1353
1354
  LangGraphEventTypes2["OnToolStart"] = "on_tool_start";
1354
1355
  LangGraphEventTypes2["OnToolEnd"] = "on_tool_end";
1355
1356
  LangGraphEventTypes2["OnCopilotKitStateSync"] = "on_copilotkit_state_sync";
1356
- LangGraphEventTypes2["OnCopilotKitEmitMessage"] = "on_copilotkit_emit_message";
1357
- LangGraphEventTypes2["OnCopilotKitEmitToolCall"] = "on_copilotkit_emit_tool_call";
1357
+ LangGraphEventTypes2["OnCustomEvent"] = "on_custom_event";
1358
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 = {}));
1359
1366
 
1360
1367
  // src/agents/langgraph/event-source.ts
1361
1368
  var import_shared8 = require("@copilotkit/shared");
1362
1369
  var RemoteLangGraphEventSource = class {
1363
1370
  eventStream$ = new import_rxjs.ReplaySubject();
1364
- async streamResponse(response) {
1365
- const reader = response.body.getReader();
1366
- const decoder = new TextDecoder();
1367
- let buffer = [];
1368
- const eventStream$ = this.eventStream$;
1369
- function flushBuffer() {
1370
- const currentBuffer = buffer.join("");
1371
- if (currentBuffer.trim().length === 0) {
1372
- return;
1373
- }
1374
- const parts = currentBuffer.split("\n");
1375
- if (parts.length === 0) {
1376
- return;
1377
- }
1378
- const lastPartIsComplete = currentBuffer.endsWith("\n");
1379
- buffer = [];
1380
- if (!lastPartIsComplete) {
1381
- buffer.push(parts.pop());
1382
- }
1383
- parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
1384
- eventStream$.next(JSON.parse(part));
1385
- });
1386
- }
1387
- __name(flushBuffer, "flushBuffer");
1388
- try {
1389
- while (true) {
1390
- const { done, value } = await reader.read();
1391
- if (!done) {
1392
- buffer.push(decoder.decode(value, {
1393
- stream: true
1394
- }));
1395
- }
1396
- flushBuffer();
1397
- if (done) {
1398
- break;
1399
- }
1400
- }
1401
- } catch (error) {
1402
- console.error("Error in stream", error);
1403
- eventStream$.error(error);
1404
- return;
1405
- }
1406
- eventStream$.complete();
1407
- }
1408
1371
  shouldEmitToolCall(shouldEmitToolCalls, toolCallName) {
1409
1372
  if (typeof shouldEmitToolCalls === "boolean") {
1410
1373
  return shouldEmitToolCalls;
@@ -1427,20 +1390,25 @@ var RemoteLangGraphEventSource = class {
1427
1390
  } else {
1428
1391
  acc.content = null;
1429
1392
  }
1430
- 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) {
1431
1399
  acc.prevToolCallMessageId = acc.toolCallMessageId;
1432
- acc.toolCallMessageId = (_i = event.data.chunk.kwargs) == null ? void 0 : _i.id;
1433
- if ((_j = event.data.chunk.kwargs.tool_call_chunks[0]) == null ? void 0 : _j.name) {
1434
- 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;
1435
1403
  }
1436
- if ((_k = event.data.chunk.kwargs.tool_call_chunks[0]) == null ? void 0 : _k.id) {
1437
- 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;
1438
1406
  }
1439
1407
  acc.prevMessageId = acc.messageId;
1440
- 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;
1441
1409
  } else if (acc.content && acc.content != "") {
1442
1410
  acc.prevMessageId = acc.messageId;
1443
- 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;
1444
1412
  } else {
1445
1413
  acc.prevToolCallMessageId = acc.toolCallMessageId;
1446
1414
  acc.prevMessageId = acc.messageId;
@@ -1465,7 +1433,7 @@ var RemoteLangGraphEventSource = class {
1465
1433
  prevMessageId: null,
1466
1434
  content: null
1467
1435
  }), (0, import_rxjs.mergeMap)((eventWithState) => {
1468
- var _a, _b, _c, _d, _e;
1436
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1469
1437
  const events = [];
1470
1438
  let shouldEmitMessages = true;
1471
1439
  let shouldEmitToolCalls = false;
@@ -1488,32 +1456,33 @@ var RemoteLangGraphEventSource = class {
1488
1456
  });
1489
1457
  }
1490
1458
  switch (eventWithState.event.event) {
1491
- case LangGraphEventTypes.OnCopilotKitEmitMessage:
1492
- events.push({
1493
- type: RuntimeEventTypes.TextMessageStart,
1494
- messageId: eventWithState.event.message_id
1495
- });
1496
- events.push({
1497
- type: RuntimeEventTypes.TextMessageContent,
1498
- content: eventWithState.event.message
1499
- });
1500
- events.push({
1501
- type: RuntimeEventTypes.TextMessageEnd
1502
- });
1503
- break;
1504
- case LangGraphEventTypes.OnCopilotKitEmitToolCall:
1505
- events.push({
1506
- type: RuntimeEventTypes.ActionExecutionStart,
1507
- actionExecutionId: eventWithState.event.id,
1508
- actionName: eventWithState.event.name
1509
- });
1510
- events.push({
1511
- type: RuntimeEventTypes.ActionExecutionArgs,
1512
- args: JSON.stringify(eventWithState.event.args)
1513
- });
1514
- events.push({
1515
- type: RuntimeEventTypes.ActionExecutionEnd
1516
- });
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
+ }
1517
1486
  break;
1518
1487
  case LangGraphEventTypes.OnCopilotKitStateSync:
1519
1488
  events.push({
@@ -1548,7 +1517,8 @@ var RemoteLangGraphEventSource = class {
1548
1517
  });
1549
1518
  }
1550
1519
  }
1551
- 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);
1552
1522
  const content = eventWithState.content;
1553
1523
  if (args) {
1554
1524
  if (this.shouldEmitToolCall(shouldEmitToolCalls, eventWithState.toolCallName)) {
@@ -1599,70 +1569,528 @@ var RemoteLangGraphEventSource = class {
1599
1569
  };
1600
1570
  __name(RemoteLangGraphEventSource, "RemoteLangGraphEventSource");
1601
1571
 
1602
- // src/lib/runtime/remote-actions.ts
1603
- function isLangGraphAgentAction(action) {
1604
- if (!action) {
1605
- return false;
1606
- }
1607
- 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
+ });
1608
1624
  }
1609
- __name(isLangGraphAgentAction, "isLangGraphAgentAction");
1610
- function createHeaders(onBeforeRequest, graphqlContext) {
1611
- const headers = {
1612
- "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: {}
1613
1652
  };
1614
- if (onBeforeRequest) {
1615
- const { headers: additionalHeaders } = onBeforeRequest({
1616
- 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
1617
1670
  });
1618
- if (additionalHeaders) {
1619
- 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");
1620
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();
1621
1795
  }
1622
- return headers;
1623
1796
  }
1624
- __name(createHeaders, "createHeaders");
1625
- async function fetchRemoteInfo({ url, onBeforeRequest, graphqlContext, logger: logger2, frontendUrl }) {
1626
- logger2.debug({
1627
- url
1628
- }, "Fetching actions from url");
1629
- const headers = createHeaders(onBeforeRequest, graphqlContext);
1630
- try {
1631
- const response = await fetch(`${url}/info`, {
1632
- method: "POST",
1633
- headers,
1634
- body: JSON.stringify({
1635
- properties: graphqlContext.properties,
1636
- frontendUrl
1637
- })
1638
- });
1639
- if (!response.ok) {
1640
- logger2.error({
1641
- url,
1642
- status: response.status,
1643
- body: await response.text()
1644
- }, "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
+ };
1645
2013
  return {
1646
- actions: [],
1647
- agents: []
2014
+ type: message.type,
2015
+ content: "",
2016
+ tool_calls: [
2017
+ toolCall
2018
+ ],
2019
+ role: MessageRole.assistant,
2020
+ id: message.id
1648
2021
  };
1649
2022
  }
1650
- const json = await response.json();
1651
- logger2.debug({
1652
- json
1653
- }, "Fetched actions from url");
1654
- return json;
1655
- } catch (error) {
1656
- logger2.error({
1657
- error: error.message ? error.message : error + ""
1658
- }, "Failed to fetch actions from url");
1659
- return {
1660
- actions: [],
1661
- agents: []
1662
- };
1663
- }
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
+ });
1664
2035
  }
1665
- __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");
1666
2094
  function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, logger: logger2, messages, agentStates }) {
1667
2095
  const actions = json["actions"].map((action) => ({
1668
2096
  name: action.name,
@@ -1754,7 +2182,7 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
1754
2182
  throw new Error("Failed to execute remote agent");
1755
2183
  }
1756
2184
  const eventSource = new RemoteLangGraphEventSource();
1757
- eventSource.streamResponse(response);
2185
+ streamResponse(response.body, eventSource.eventStream$);
1758
2186
  return eventSource.processLangGraphEvents();
1759
2187
  }
1760
2188
  }));
@@ -1764,34 +2192,165 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
1764
2192
  ];
1765
2193
  }
1766
2194
  __name(constructRemoteActions, "constructRemoteActions");
1767
- 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 }) {
1768
2310
  const logger2 = graphqlContext.logger.child({
1769
2311
  component: "remote-actions.fetchRemoteActions"
1770
2312
  });
1771
2313
  logger2.debug({
1772
- remoteActionDefinitions
1773
- }, "Fetching remote actions");
1774
- const filtered = remoteActionDefinitions.filter((value, index, self) => index === self.findIndex((t) => t.url === value.url));
1775
- 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
+ }
1776
2335
  const json = await fetchRemoteInfo({
1777
- url: actionDefinition.url,
1778
- onBeforeRequest: actionDefinition.onBeforeRequest,
2336
+ url: endpoint.url,
2337
+ onBeforeRequest: endpoint.onBeforeRequest,
1779
2338
  graphqlContext,
1780
2339
  logger: logger2.child({
1781
2340
  component: "remote-actions.fetchActionsFromUrl",
1782
- actionDefinition
2341
+ endpoint
1783
2342
  }),
1784
2343
  frontendUrl
1785
2344
  });
1786
2345
  return constructRemoteActions({
1787
2346
  json,
1788
2347
  messages,
1789
- url: actionDefinition.url,
1790
- onBeforeRequest: actionDefinition.onBeforeRequest,
2348
+ url: endpoint.url,
2349
+ onBeforeRequest: endpoint.onBeforeRequest,
1791
2350
  graphqlContext,
1792
2351
  logger: logger2.child({
1793
2352
  component: "remote-actions.constructActions",
1794
- actionDefinition
2353
+ endpoint
1795
2354
  }),
1796
2355
  agentStates
1797
2356
  });
@@ -1989,7 +2548,7 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
1989
2548
  __name(executeAction, "executeAction");
1990
2549
 
1991
2550
  // src/graphql/types/base/index.ts
1992
- var import_type_graphql = require("type-graphql");
2551
+ var import_type_graphql2 = require("type-graphql");
1993
2552
  function _ts_decorate(decorators, target, key, desc) {
1994
2553
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1995
2554
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -2012,15 +2571,15 @@ var BaseMessageInput = class {
2012
2571
  };
2013
2572
  __name(BaseMessageInput, "BaseMessageInput");
2014
2573
  _ts_decorate([
2015
- (0, import_type_graphql.Field)(() => String),
2574
+ (0, import_type_graphql2.Field)(() => String),
2016
2575
  _ts_metadata("design:type", String)
2017
2576
  ], BaseMessageInput.prototype, "id", void 0);
2018
2577
  _ts_decorate([
2019
- (0, import_type_graphql.Field)(() => Date),
2578
+ (0, import_type_graphql2.Field)(() => Date),
2020
2579
  _ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
2021
2580
  ], BaseMessageInput.prototype, "createdAt", void 0);
2022
2581
  BaseMessageInput = _ts_decorate([
2023
- (0, import_type_graphql.InputType)()
2582
+ (0, import_type_graphql2.InputType)()
2024
2583
  ], BaseMessageInput);
2025
2584
 
2026
2585
  // src/graphql/types/converted/index.ts
@@ -2124,7 +2683,7 @@ __name(convertGqlInputToMessages, "convertGqlInputToMessages");
2124
2683
  var import_rxjs3 = require("rxjs");
2125
2684
  var CopilotRuntime = class {
2126
2685
  actions;
2127
- remoteActionDefinitions;
2686
+ remoteEndpointDefinitions;
2128
2687
  langserve = [];
2129
2688
  onBeforeRequest;
2130
2689
  onAfterRequest;
@@ -2135,7 +2694,7 @@ var CopilotRuntime = class {
2135
2694
  const remoteChain = new RemoteChain(chain);
2136
2695
  this.langserve.push(remoteChain.toAction());
2137
2696
  }
2138
- this.remoteActionDefinitions = (params == null ? void 0 : params.remoteActions) || [];
2697
+ this.remoteEndpointDefinitions = (params == null ? void 0 : params.remoteEndpoints) || [];
2139
2698
  this.onBeforeRequest = (_a = params == null ? void 0 : params.middleware) == null ? void 0 : _a.onBeforeRequest;
2140
2699
  this.onAfterRequest = (_b = params == null ? void 0 : params.middleware) == null ? void 0 : _b.onAfterRequest;
2141
2700
  }
@@ -2204,7 +2763,7 @@ var CopilotRuntime = class {
2204
2763
  async processAgentRequest(request) {
2205
2764
  var _a;
2206
2765
  const { messages: rawMessages, outputMessagesPromise, graphqlContext, agentSession } = request;
2207
- const { threadId = (0, import_shared9.randomId)(), agentName, nodeName } = agentSession;
2766
+ const { threadId, agentName, nodeName } = agentSession;
2208
2767
  const serverSideActions = await this.getServerSideActions(request);
2209
2768
  const messages = convertGqlInputToMessages(rawMessages);
2210
2769
  const agent = serverSideActions.find((action) => action.name === agentName && isLangGraphAgentAction(action));
@@ -2276,8 +2835,12 @@ var CopilotRuntime = class {
2276
2835
  console.error("Error loading langserve chain:", error);
2277
2836
  }
2278
2837
  }
2838
+ const remoteEndpointDefinitions = this.remoteEndpointDefinitions.map((endpoint) => ({
2839
+ ...endpoint,
2840
+ type: this.resolveEndpointType(endpoint)
2841
+ }));
2279
2842
  const remoteActions = await setupRemoteActions({
2280
- remoteActionDefinitions: this.remoteActionDefinitions,
2843
+ remoteEndpointDefinitions,
2281
2844
  graphqlContext,
2282
2845
  messages: inputMessages,
2283
2846
  agentStates,
@@ -2293,6 +2856,12 @@ var CopilotRuntime = class {
2293
2856
  ...remoteActions
2294
2857
  ];
2295
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
+ }
2296
2865
  };
2297
2866
  __name(CopilotRuntime, "CopilotRuntime");
2298
2867
  function flattenToolCallsNoDuplicates(toolsByPriority) {
@@ -2307,6 +2876,20 @@ function flattenToolCallsNoDuplicates(toolsByPriority) {
2307
2876
  return allTools;
2308
2877
  }
2309
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");
2310
2893
 
2311
2894
  // src/lib/integrations/shared.ts
2312
2895
  var import_type_graphql16 = require("type-graphql");
@@ -2320,43 +2903,6 @@ var import_type_graphql11 = require("type-graphql");
2320
2903
 
2321
2904
  // src/graphql/inputs/message.input.ts
2322
2905
  var import_type_graphql3 = require("type-graphql");
2323
-
2324
- // src/graphql/types/enums.ts
2325
- var import_type_graphql2 = require("type-graphql");
2326
- var MessageRole;
2327
- (function(MessageRole2) {
2328
- MessageRole2["user"] = "user";
2329
- MessageRole2["assistant"] = "assistant";
2330
- MessageRole2["system"] = "system";
2331
- })(MessageRole || (MessageRole = {}));
2332
- var ActionExecutionScope;
2333
- (function(ActionExecutionScope2) {
2334
- ActionExecutionScope2["server"] = "server";
2335
- ActionExecutionScope2["client"] = "client";
2336
- ActionExecutionScope2["passThrough"] = "passThrough";
2337
- })(ActionExecutionScope || (ActionExecutionScope = {}));
2338
- var CopilotRequestType;
2339
- (function(CopilotRequestType2) {
2340
- CopilotRequestType2["Chat"] = "Chat";
2341
- CopilotRequestType2["Task"] = "Task";
2342
- CopilotRequestType2["TextareaCompletion"] = "TextareaCompletion";
2343
- CopilotRequestType2["TextareaPopover"] = "TextareaPopover";
2344
- CopilotRequestType2["Suggestion"] = "Suggestion";
2345
- })(CopilotRequestType || (CopilotRequestType = {}));
2346
- (0, import_type_graphql2.registerEnumType)(MessageRole, {
2347
- name: "MessageRole",
2348
- description: "The role of the message"
2349
- });
2350
- (0, import_type_graphql2.registerEnumType)(ActionExecutionScope, {
2351
- name: "ActionExecutionScope",
2352
- description: "The scope of the action"
2353
- });
2354
- (0, import_type_graphql2.registerEnumType)(CopilotRequestType, {
2355
- name: "CopilotRequestType",
2356
- description: "The type of Copilot request"
2357
- });
2358
-
2359
- // src/graphql/inputs/message.input.ts
2360
2906
  function _ts_decorate2(decorators, target, key, desc) {
2361
2907
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2362
2908
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -4018,6 +4564,7 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
4018
4564
  UnknownErrorResponse,
4019
4565
  buildSchema,
4020
4566
  config,
4567
+ copilotKitEndpoint,
4021
4568
  copilotRuntimeNestEndpoint,
4022
4569
  copilotRuntimeNextJSAppRouterEndpoint,
4023
4570
  copilotRuntimeNextJSPagesRouterEndpoint,
@@ -4025,6 +4572,7 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
4025
4572
  copilotRuntimeNodeHttpEndpoint,
4026
4573
  createContext,
4027
4574
  flattenToolCallsNoDuplicates,
4028
- getCommonConfig
4575
+ getCommonConfig,
4576
+ langGraphCloudEndpoint
4029
4577
  });
4030
4578
  //# sourceMappingURL=index.js.map