@copilotkit/react-core 1.3.16-pre.11 → 1.3.16-pre.13

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 (88) hide show
  1. package/CHANGELOG.md +28 -4
  2. package/dist/{chunk-ZM6HV22S.mjs → chunk-A6THC3F6.mjs} +2 -2
  3. package/dist/{chunk-MX2OYLRS.mjs → chunk-DPWPAGS6.mjs} +36 -6
  4. package/dist/chunk-DPWPAGS6.mjs.map +1 -0
  5. package/dist/{chunk-RYDEG77L.mjs → chunk-FXT6XSUB.mjs} +2 -2
  6. package/dist/{chunk-7DFHFZJT.mjs → chunk-I7XLZC4D.mjs} +2 -2
  7. package/dist/{chunk-TRXKPXDE.mjs → chunk-N57PDRVX.mjs} +2 -2
  8. package/dist/{chunk-27IPQFJP.mjs → chunk-PHP2YMMH.mjs} +2 -2
  9. package/dist/{chunk-XACBOJF5.mjs → chunk-Q3IGIEYP.mjs} +23 -5
  10. package/dist/{chunk-XACBOJF5.mjs.map → chunk-Q3IGIEYP.mjs.map} +1 -1
  11. package/dist/{chunk-6YEMNWKE.mjs → chunk-UFSLLJZY.mjs} +12 -3
  12. package/dist/{chunk-6YEMNWKE.mjs.map → chunk-UFSLLJZY.mjs.map} +1 -1
  13. package/dist/{chunk-G2K6T2QU.mjs → chunk-VUXHWQDJ.mjs} +35 -15
  14. package/dist/chunk-VUXHWQDJ.mjs.map +1 -0
  15. package/dist/{chunk-2MPUQRAY.mjs → chunk-WBBL7MG6.mjs} +10 -2
  16. package/dist/{chunk-2MPUQRAY.mjs.map → chunk-WBBL7MG6.mjs.map} +1 -1
  17. package/dist/{chunk-ANOG3W5S.mjs → chunk-ZDYUMEE5.mjs} +2 -2
  18. package/dist/components/copilot-provider/copilotkit.js +19 -2
  19. package/dist/components/copilot-provider/copilotkit.js.map +1 -1
  20. package/dist/components/copilot-provider/copilotkit.mjs +2 -2
  21. package/dist/components/copilot-provider/index.js +19 -2
  22. package/dist/components/copilot-provider/index.js.map +1 -1
  23. package/dist/components/copilot-provider/index.mjs +2 -2
  24. package/dist/components/index.js +19 -2
  25. package/dist/components/index.js.map +1 -1
  26. package/dist/components/index.mjs +2 -2
  27. package/dist/context/copilot-context.d.ts +6 -0
  28. package/dist/context/copilot-context.js +9 -1
  29. package/dist/context/copilot-context.js.map +1 -1
  30. package/dist/context/copilot-context.mjs +1 -1
  31. package/dist/context/index.js +9 -1
  32. package/dist/context/index.js.map +1 -1
  33. package/dist/context/index.mjs +1 -1
  34. package/dist/hooks/index.d.ts +2 -1
  35. package/dist/hooks/index.js +97 -18
  36. package/dist/hooks/index.js.map +1 -1
  37. package/dist/hooks/index.mjs +15 -9
  38. package/dist/hooks/use-chat.d.ts +20 -0
  39. package/dist/hooks/use-chat.js +34 -14
  40. package/dist/hooks/use-chat.js.map +1 -1
  41. package/dist/hooks/use-chat.mjs +1 -1
  42. package/dist/hooks/use-coagent-state-render.js +9 -1
  43. package/dist/hooks/use-coagent-state-render.js.map +1 -1
  44. package/dist/hooks/use-coagent-state-render.mjs +2 -2
  45. package/dist/hooks/use-coagent.d.ts +14 -1
  46. package/dist/hooks/use-coagent.js +97 -18
  47. package/dist/hooks/use-coagent.js.map +1 -1
  48. package/dist/hooks/use-coagent.mjs +11 -5
  49. package/dist/hooks/use-copilot-action.js +9 -1
  50. package/dist/hooks/use-copilot-action.js.map +1 -1
  51. package/dist/hooks/use-copilot-action.mjs +2 -2
  52. package/dist/hooks/use-copilot-chat.d.ts +1 -0
  53. package/dist/hooks/use-copilot-chat.js +75 -17
  54. package/dist/hooks/use-copilot-chat.js.map +1 -1
  55. package/dist/hooks/use-copilot-chat.mjs +4 -4
  56. package/dist/hooks/use-copilot-readable.js +9 -1
  57. package/dist/hooks/use-copilot-readable.js.map +1 -1
  58. package/dist/hooks/use-copilot-readable.mjs +2 -2
  59. package/dist/hooks/use-make-copilot-document-readable.js +9 -1
  60. package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
  61. package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
  62. package/dist/index.d.ts +1 -1
  63. package/dist/index.js +107 -19
  64. package/dist/index.js.map +1 -1
  65. package/dist/index.mjs +17 -11
  66. package/dist/lib/copilot-task.js.map +1 -1
  67. package/dist/lib/copilot-task.mjs +3 -3
  68. package/dist/lib/index.js.map +1 -1
  69. package/dist/lib/index.mjs +3 -3
  70. package/dist/utils/extract.js.map +1 -1
  71. package/dist/utils/extract.mjs +3 -3
  72. package/dist/utils/index.js.map +1 -1
  73. package/dist/utils/index.mjs +3 -3
  74. package/package.json +5 -5
  75. package/src/components/copilot-provider/copilotkit.tsx +10 -0
  76. package/src/context/copilot-context.tsx +21 -0
  77. package/src/hooks/index.ts +1 -1
  78. package/src/hooks/use-chat.ts +57 -14
  79. package/src/hooks/use-coagent.ts +21 -4
  80. package/src/hooks/use-copilot-chat.ts +35 -4
  81. package/dist/chunk-G2K6T2QU.mjs.map +0 -1
  82. package/dist/chunk-MX2OYLRS.mjs.map +0 -1
  83. /package/dist/{chunk-ZM6HV22S.mjs.map → chunk-A6THC3F6.mjs.map} +0 -0
  84. /package/dist/{chunk-RYDEG77L.mjs.map → chunk-FXT6XSUB.mjs.map} +0 -0
  85. /package/dist/{chunk-7DFHFZJT.mjs.map → chunk-I7XLZC4D.mjs.map} +0 -0
  86. /package/dist/{chunk-TRXKPXDE.mjs.map → chunk-N57PDRVX.mjs.map} +0 -0
  87. /package/dist/{chunk-27IPQFJP.mjs.map → chunk-PHP2YMMH.mjs.map} +0 -0
  88. /package/dist/{chunk-ANOG3W5S.mjs.map → chunk-ZDYUMEE5.mjs.map} +0 -0
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  useMakeCopilotDocumentReadable
3
- } from "../chunk-TRXKPXDE.mjs";
4
- import "../chunk-2MPUQRAY.mjs";
3
+ } from "../chunk-N57PDRVX.mjs";
4
+ import "../chunk-WBBL7MG6.mjs";
5
5
  import "../chunk-SKC7AJIV.mjs";
6
6
  export {
7
7
  useMakeCopilotDocumentReadable
package/dist/index.d.ts CHANGED
@@ -8,7 +8,7 @@ export { useCoAgentStateRender } from './hooks/use-coagent-state-render.js';
8
8
  export { useMakeCopilotDocumentReadable } from './hooks/use-make-copilot-document-readable.js';
9
9
  export { UseChatHelpers } from './hooks/use-chat.js';
10
10
  export { useCopilotReadable } from './hooks/use-copilot-readable.js';
11
- export { useCoAgent } from './hooks/use-coagent.js';
11
+ export { HintFunction, runAgent, startAgent, stopAgent, useCoAgent } from './hooks/use-coagent.js';
12
12
  export { DocumentPointer } from './types/document-pointer.js';
13
13
  export { SystemMessageFunction } from './types/system-message.js';
14
14
  export { ActionRenderProps, RenderFunctionStatus } from './types/frontend-action.js';
package/dist/index.js CHANGED
@@ -87,6 +87,9 @@ __export(src_exports, {
87
87
  CopilotTask: () => CopilotTask,
88
88
  defaultCopilotContextCategories: () => defaultCopilotContextCategories,
89
89
  extract: () => extract,
90
+ runAgent: () => runAgent,
91
+ startAgent: () => startAgent,
92
+ stopAgent: () => stopAgent,
90
93
  useCoAgent: () => useCoAgent,
91
94
  useCoAgentStateRender: () => useCoAgentStateRender,
92
95
  useCopilotAction: () => useCopilotAction,
@@ -151,7 +154,15 @@ var emptyCopilotContext = {
151
154
  },
152
155
  agentSession: null,
153
156
  setAgentSession: () => {
154
- }
157
+ },
158
+ agentLock: null,
159
+ threadId: null,
160
+ setThreadId: () => {
161
+ },
162
+ runId: null,
163
+ setRunId: () => {
164
+ },
165
+ chatAbortControllerRef: { current: null }
155
166
  };
156
167
  var CopilotContext = import_react.default.createContext(emptyCopilotContext);
157
168
  function useCopilotContext() {
@@ -565,6 +576,9 @@ ${nonDocumentStrings}`;
565
576
  };
566
577
  }
567
578
  const [agentSession, setAgentSession] = (0, import_react6.useState)(initialAgentSession);
579
+ const [threadId, setThreadId] = (0, import_react6.useState)(null);
580
+ const [runId, setRunId] = (0, import_react6.useState)(null);
581
+ const chatAbortControllerRef = (0, import_react6.useRef)(null);
568
582
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
569
583
  CopilotContext.Provider,
570
584
  {
@@ -595,7 +609,13 @@ ${nonDocumentStrings}`;
595
609
  coagentStates,
596
610
  setCoagentStates,
597
611
  agentSession,
598
- setAgentSession
612
+ setAgentSession,
613
+ agentLock: props.agent || null,
614
+ threadId,
615
+ setThreadId,
616
+ runId,
617
+ setRunId,
618
+ chatAbortControllerRef
599
619
  },
600
620
  children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(CopilotMessages, { children })
601
621
  }
@@ -651,16 +671,22 @@ function useChat(options) {
651
671
  setCoagentStates,
652
672
  coagentStates,
653
673
  agentSession,
654
- setAgentSession
674
+ setAgentSession,
675
+ threadId,
676
+ setThreadId,
677
+ runId,
678
+ setRunId,
679
+ chatAbortControllerRef
655
680
  } = options;
656
- const abortControllerRef = (0, import_react7.useRef)();
657
- const threadIdRef = (0, import_react7.useRef)(null);
658
- const runIdRef = (0, import_react7.useRef)(null);
659
681
  const runChatCompletionRef = (0, import_react7.useRef)();
660
682
  const coagentStatesRef = (0, import_react7.useRef)(coagentStates);
661
683
  coagentStatesRef.current = coagentStates;
662
684
  const agentSessionRef = (0, import_react7.useRef)(agentSession);
663
685
  agentSessionRef.current = agentSession;
686
+ const threadIdRef = (0, import_react7.useRef)(threadId);
687
+ threadIdRef.current = threadId;
688
+ const runIdRef = (0, import_react7.useRef)(runId);
689
+ runIdRef.current = runId;
664
690
  const publicApiKey = copilotConfig.publicApiKey;
665
691
  const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [import_shared4.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
666
692
  const runtimeClient = new import_runtime_client_gql.CopilotRuntimeClient({
@@ -678,8 +704,7 @@ function useChat(options) {
678
704
  role: import_runtime_client_gql.Role.Assistant
679
705
  })
680
706
  ];
681
- const abortController = new AbortController();
682
- abortControllerRef.current = abortController;
707
+ chatAbortControllerRef.current = new AbortController();
683
708
  setMessages([...previousMessages, ...newMessages]);
684
709
  const systemMessage = makeSystemMessageCallback();
685
710
  const messagesWithContext = [systemMessage, ...initialMessages || [], ...previousMessages];
@@ -719,7 +744,7 @@ function useChat(options) {
719
744
  }))
720
745
  }),
721
746
  properties: copilotConfig.properties,
722
- signal: (_d = abortControllerRef.current) == null ? void 0 : _d.signal
747
+ signal: (_d = chatAbortControllerRef.current) == null ? void 0 : _d.signal
723
748
  })
724
749
  );
725
750
  const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
@@ -731,6 +756,9 @@ function useChat(options) {
731
756
  while (true) {
732
757
  const { done, value } = yield reader.read();
733
758
  if (done) {
759
+ if (chatAbortControllerRef.current.signal.aborted) {
760
+ return newMessages.slice();
761
+ }
734
762
  break;
735
763
  }
736
764
  if (!(value == null ? void 0 : value.generateCopilotResponse)) {
@@ -738,6 +766,8 @@ function useChat(options) {
738
766
  }
739
767
  threadIdRef.current = value.generateCopilotResponse.threadId || null;
740
768
  runIdRef.current = value.generateCopilotResponse.runId || null;
769
+ setThreadId(threadIdRef.current);
770
+ setRunId(runIdRef.current);
741
771
  const messages2 = (0, import_runtime_client_gql.convertGqlOutputToMessages)(
742
772
  (0, import_runtime_client_gql.filterAdjacentAgentStateMessages)(value.generateCopilotResponse.messages)
743
773
  );
@@ -766,12 +796,22 @@ function useChat(options) {
766
796
  if (action) {
767
797
  followUp = action.followUp;
768
798
  }
769
- const result = yield onFunctionCall({
770
- messages: previousMessages,
771
- name: message.name,
772
- args: message.arguments
773
- });
774
- actionResults[message.id] = result;
799
+ const result = yield Promise.race([
800
+ onFunctionCall({
801
+ messages: previousMessages,
802
+ name: message.name,
803
+ args: message.arguments
804
+ }),
805
+ new Promise((_, reject) => {
806
+ var _a2;
807
+ return (_a2 = chatAbortControllerRef.current) == null ? void 0 : _a2.signal.addEventListener("abort", () => reject(new Error("Operation was aborted")));
808
+ })
809
+ ]);
810
+ if (chatAbortControllerRef.current.signal.aborted) {
811
+ actionResults[message.id] = "";
812
+ } else {
813
+ actionResults[message.id] = result;
814
+ }
775
815
  } catch (e) {
776
816
  actionResults[message.id] = `Failed to execute action ${message.name}`;
777
817
  console.error(`Failed to execute action ${message.name}: ${e}`);
@@ -866,7 +906,7 @@ function useChat(options) {
866
906
  });
867
907
  const stop = () => {
868
908
  var _a;
869
- (_a = abortControllerRef.current) == null ? void 0 : _a.abort();
909
+ (_a = chatAbortControllerRef.current) == null ? void 0 : _a.abort("Stop was called");
870
910
  };
871
911
  return {
872
912
  append,
@@ -894,7 +934,13 @@ function useCopilotChat(_a = {}) {
894
934
  setCoagentStates,
895
935
  coAgentStateRenders,
896
936
  agentSession,
897
- setAgentSession
937
+ setAgentSession,
938
+ agentLock,
939
+ threadId,
940
+ setThreadId,
941
+ runId,
942
+ setRunId,
943
+ chatAbortControllerRef
898
944
  } = useCopilotContext();
899
945
  const { messages, setMessages } = useCopilotMessagesContext();
900
946
  const latestGetContextString = useUpdatedRef(getContextString);
@@ -944,7 +990,12 @@ function useCopilotChat(_a = {}) {
944
990
  coagentStates,
945
991
  setCoagentStates,
946
992
  agentSession,
947
- setAgentSession
993
+ setAgentSession,
994
+ threadId,
995
+ setThreadId,
996
+ runId,
997
+ setRunId,
998
+ chatAbortControllerRef
948
999
  }));
949
1000
  const latestAppend = useUpdatedRef(append);
950
1001
  const latestAppendFunc = (0, import_react8.useCallback)(
@@ -975,6 +1026,24 @@ function useCopilotChat(_a = {}) {
975
1026
  },
976
1027
  [latestSetMessages]
977
1028
  );
1029
+ const reset = (0, import_react8.useCallback)(() => {
1030
+ latestStopFunc();
1031
+ setMessages([]);
1032
+ setThreadId(null);
1033
+ setRunId(null);
1034
+ setCoagentStates({});
1035
+ let initialAgentSession = null;
1036
+ if (agentLock) {
1037
+ initialAgentSession = {
1038
+ agentName: agentLock
1039
+ };
1040
+ }
1041
+ setAgentSession(initialAgentSession);
1042
+ }, [latestStopFunc, setMessages, setThreadId, setCoagentStates, setAgentSession, agentLock]);
1043
+ const latestReset = useUpdatedRef(reset);
1044
+ const latestResetFunc = (0, import_react8.useCallback)(() => {
1045
+ return latestReset.current();
1046
+ }, [latestReset]);
978
1047
  return {
979
1048
  visibleMessages: messages,
980
1049
  appendMessage: latestAppendFunc,
@@ -982,6 +1051,7 @@ function useCopilotChat(_a = {}) {
982
1051
  reloadMessages: latestReloadFunc,
983
1052
  stopGeneration: latestStopFunc,
984
1053
  deleteMessage: latestDeleteFunc,
1054
+ reset: latestResetFunc,
985
1055
  isLoading
986
1056
  };
987
1057
  }
@@ -1206,7 +1276,11 @@ function useCoAgent(options) {
1206
1276
  } else if (coagentStates[name] === void 0) {
1207
1277
  setState(options.initialState === void 0 ? {} : options.initialState);
1208
1278
  }
1209
- }, [isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0]);
1279
+ }, [
1280
+ isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0,
1281
+ // reset initial state on reset
1282
+ coagentStates[name] === void 0
1283
+ ]);
1210
1284
  return {
1211
1285
  name,
1212
1286
  nodeName: coagentState.nodeName,
@@ -1234,6 +1308,17 @@ function stopAgent(name, context) {
1234
1308
  const { agentSession, setAgentSession } = context;
1235
1309
  if (agentSession && agentSession.agentName === name) {
1236
1310
  setAgentSession(null);
1311
+ context.setCoagentStates((prevAgentStates) => {
1312
+ return __spreadProps(__spreadValues({}, prevAgentStates), {
1313
+ [name]: __spreadProps(__spreadValues({}, prevAgentStates[name]), {
1314
+ running: false,
1315
+ active: false,
1316
+ threadId: void 0,
1317
+ nodeName: void 0,
1318
+ runId: void 0
1319
+ })
1320
+ });
1321
+ });
1237
1322
  } else {
1238
1323
  console.warn(`No agent session found for ${name}`);
1239
1324
  }
@@ -1512,6 +1597,9 @@ This is not a conversation, so please do not ask questions. Just call the functi
1512
1597
  CopilotTask,
1513
1598
  defaultCopilotContextCategories,
1514
1599
  extract,
1600
+ runAgent,
1601
+ startAgent,
1602
+ stopAgent,
1515
1603
  useCoAgent,
1516
1604
  useCoAgentStateRender,
1517
1605
  useCopilotAction,