@copilotkit/react-core 1.4.2-tyler-reset-chat.2 → 1.4.3-pre.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 (87) hide show
  1. package/CHANGELOG.md +10 -17
  2. package/dist/{chunk-USL3EHJB.mjs → chunk-AG7FH7OD.mjs} +2 -2
  3. package/dist/{chunk-6QKA3SNN.mjs → chunk-DHGXL5PC.mjs} +16 -12
  4. package/dist/{chunk-6QKA3SNN.mjs.map → chunk-DHGXL5PC.mjs.map} +1 -1
  5. package/dist/{chunk-24AHWFXB.mjs → chunk-EUIBVFV6.mjs} +20 -41
  6. package/dist/chunk-EUIBVFV6.mjs.map +1 -0
  7. package/dist/{chunk-3AYELZJS.mjs → chunk-JHEAUB3Z.mjs} +2 -2
  8. package/dist/{chunk-YTYNTCQL.mjs → chunk-O22KGHOQ.mjs} +21 -37
  9. package/dist/chunk-O22KGHOQ.mjs.map +1 -0
  10. package/dist/{chunk-JR55I3FL.mjs → chunk-OT67R4NB.mjs} +2 -2
  11. package/dist/{chunk-XUPO37VH.mjs → chunk-UOVONDR6.mjs} +2 -2
  12. package/dist/{chunk-UYKBEKKH.mjs → chunk-XBVKTDXP.mjs} +16 -41
  13. package/dist/chunk-XBVKTDXP.mjs.map +1 -0
  14. package/dist/{chunk-2JP64U3A.mjs → chunk-XQFVXX6R.mjs} +5 -10
  15. package/dist/{chunk-2JP64U3A.mjs.map → chunk-XQFVXX6R.mjs.map} +1 -1
  16. package/dist/{chunk-3R4J2TPH.mjs → chunk-Y7MI4PBB.mjs} +2 -2
  17. package/dist/components/copilot-provider/copilotkit.js +18 -19
  18. package/dist/components/copilot-provider/copilotkit.js.map +1 -1
  19. package/dist/components/copilot-provider/copilotkit.mjs +2 -2
  20. package/dist/components/copilot-provider/index.js +18 -19
  21. package/dist/components/copilot-provider/index.js.map +1 -1
  22. package/dist/components/copilot-provider/index.mjs +2 -2
  23. package/dist/components/index.js +18 -19
  24. package/dist/components/index.js.map +1 -1
  25. package/dist/components/index.mjs +2 -2
  26. package/dist/context/copilot-context.d.ts +2 -6
  27. package/dist/context/copilot-context.js +4 -9
  28. package/dist/context/copilot-context.js.map +1 -1
  29. package/dist/context/copilot-context.mjs +1 -1
  30. package/dist/context/index.js +4 -9
  31. package/dist/context/index.js.map +1 -1
  32. package/dist/context/index.mjs +1 -1
  33. package/dist/hooks/index.d.ts +1 -2
  34. package/dist/hooks/index.js +52 -122
  35. package/dist/hooks/index.js.map +1 -1
  36. package/dist/hooks/index.mjs +16 -22
  37. package/dist/hooks/use-chat.d.ts +6 -22
  38. package/dist/hooks/use-chat.js +19 -40
  39. package/dist/hooks/use-chat.js.map +1 -1
  40. package/dist/hooks/use-chat.mjs +1 -1
  41. package/dist/hooks/use-coagent-state-render.js +4 -9
  42. package/dist/hooks/use-coagent-state-render.js.map +1 -1
  43. package/dist/hooks/use-coagent-state-render.mjs +2 -2
  44. package/dist/hooks/use-coagent.d.ts +1 -14
  45. package/dist/hooks/use-coagent.js +52 -122
  46. package/dist/hooks/use-coagent.js.map +1 -1
  47. package/dist/hooks/use-coagent.mjs +6 -12
  48. package/dist/hooks/use-copilot-action.js +4 -9
  49. package/dist/hooks/use-copilot-action.js.map +1 -1
  50. package/dist/hooks/use-copilot-action.mjs +2 -2
  51. package/dist/hooks/use-copilot-chat.d.ts +1 -1
  52. package/dist/hooks/use-copilot-chat.js +35 -86
  53. package/dist/hooks/use-copilot-chat.js.map +1 -1
  54. package/dist/hooks/use-copilot-chat.mjs +5 -5
  55. package/dist/hooks/use-copilot-readable.js +4 -9
  56. package/dist/hooks/use-copilot-readable.js.map +1 -1
  57. package/dist/hooks/use-copilot-readable.mjs +2 -2
  58. package/dist/hooks/use-make-copilot-document-readable.js +4 -9
  59. package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
  60. package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
  61. package/dist/index.d.ts +1 -1
  62. package/dist/index.js +66 -132
  63. package/dist/index.js.map +1 -1
  64. package/dist/index.mjs +17 -23
  65. package/dist/lib/copilot-task.js.map +1 -1
  66. package/dist/lib/copilot-task.mjs +3 -3
  67. package/dist/lib/index.js.map +1 -1
  68. package/dist/lib/index.mjs +3 -3
  69. package/dist/utils/extract.js.map +1 -1
  70. package/dist/utils/extract.mjs +2 -2
  71. package/dist/utils/index.js.map +1 -1
  72. package/dist/utils/index.mjs +2 -2
  73. package/package.json +5 -5
  74. package/src/components/copilot-provider/copilotkit.tsx +18 -10
  75. package/src/context/copilot-context.tsx +9 -22
  76. package/src/hooks/index.ts +1 -1
  77. package/src/hooks/use-chat.ts +28 -62
  78. package/src/hooks/use-coagent.ts +24 -38
  79. package/src/hooks/use-copilot-chat.ts +14 -41
  80. package/dist/chunk-24AHWFXB.mjs.map +0 -1
  81. package/dist/chunk-UYKBEKKH.mjs.map +0 -1
  82. package/dist/chunk-YTYNTCQL.mjs.map +0 -1
  83. /package/dist/{chunk-USL3EHJB.mjs.map → chunk-AG7FH7OD.mjs.map} +0 -0
  84. /package/dist/{chunk-3AYELZJS.mjs.map → chunk-JHEAUB3Z.mjs.map} +0 -0
  85. /package/dist/{chunk-JR55I3FL.mjs.map → chunk-OT67R4NB.mjs.map} +0 -0
  86. /package/dist/{chunk-XUPO37VH.mjs.map → chunk-UOVONDR6.mjs.map} +0 -0
  87. /package/dist/{chunk-3R4J2TPH.mjs.map → chunk-Y7MI4PBB.mjs.map} +0 -0
@@ -79,9 +79,6 @@ var __async = (__this, __arguments, generator) => {
79
79
  // src/hooks/index.ts
80
80
  var hooks_exports = {};
81
81
  __export(hooks_exports, {
82
- runAgent: () => runAgent,
83
- startAgent: () => startAgent,
84
- stopAgent: () => stopAgent,
85
82
  useCoAgent: () => useCoAgent,
86
83
  useCoAgentStateRender: () => useCoAgentStateRender,
87
84
  useCopilotAction: () => useCopilotAction,
@@ -144,17 +141,12 @@ var emptyCopilotContext = {
144
141
  coagentStates: {},
145
142
  setCoagentStates: () => {
146
143
  },
144
+ coagentStatesRef: { current: {} },
145
+ setCoagentStatesWithRef: () => {
146
+ },
147
147
  agentSession: null,
148
148
  setAgentSession: () => {
149
- },
150
- agentLock: null,
151
- threadId: null,
152
- setThreadId: () => {
153
- },
154
- runId: null,
155
- setRunId: () => {
156
- },
157
- chatAbortControllerRef: { current: null }
149
+ }
158
150
  };
159
151
  var CopilotContext = import_react.default.createContext(emptyCopilotContext);
160
152
  function useCopilotContext() {
@@ -219,26 +211,18 @@ function useChat(options) {
219
211
  actions,
220
212
  onFunctionCall,
221
213
  onCoAgentStateRender,
222
- setCoagentStates,
223
- coagentStates,
214
+ setCoagentStatesWithRef,
215
+ coagentStatesRef,
224
216
  agentSession,
225
- setAgentSession,
226
- threadId,
227
- setThreadId,
228
- runId,
229
- setRunId,
230
- chatAbortControllerRef
217
+ setAgentSession
231
218
  } = options;
219
+ const abortControllerRef = (0, import_react4.useRef)();
220
+ const threadIdRef = (0, import_react4.useRef)(null);
221
+ const runIdRef = (0, import_react4.useRef)(null);
232
222
  const { addGraphQLErrorsToast } = useToast();
233
223
  const runChatCompletionRef = (0, import_react4.useRef)();
234
- const coagentStatesRef = (0, import_react4.useRef)(coagentStates);
235
- coagentStatesRef.current = coagentStates;
236
224
  const agentSessionRef = (0, import_react4.useRef)(agentSession);
237
225
  agentSessionRef.current = agentSession;
238
- const threadIdRef = (0, import_react4.useRef)(threadId);
239
- threadIdRef.current = threadId;
240
- const runIdRef = (0, import_react4.useRef)(runId);
241
- runIdRef.current = runId;
242
226
  const publicApiKey = copilotConfig.publicApiKey;
243
227
  const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [import_shared.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
244
228
  const runtimeClient = useCopilotRuntimeClient({
@@ -256,7 +240,8 @@ function useChat(options) {
256
240
  role: import_runtime_client_gql2.Role.Assistant
257
241
  })
258
242
  ];
259
- chatAbortControllerRef.current = new AbortController();
243
+ const abortController = new AbortController();
244
+ abortControllerRef.current = abortController;
260
245
  setMessages([...previousMessages, ...newMessages]);
261
246
  const systemMessage = makeSystemMessageCallback();
262
247
  const messagesWithContext = [systemMessage, ...initialMessages || [], ...previousMessages];
@@ -309,7 +294,7 @@ function useChat(options) {
309
294
  }))
310
295
  }),
311
296
  properties: copilotConfig.properties,
312
- signal: (_d = chatAbortControllerRef.current) == null ? void 0 : _d.signal
297
+ signal: (_d = abortControllerRef.current) == null ? void 0 : _d.signal
313
298
  })
314
299
  );
315
300
  const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
@@ -328,9 +313,6 @@ function useChat(options) {
328
313
  break;
329
314
  }
330
315
  if (done) {
331
- if (chatAbortControllerRef.current.signal.aborted) {
332
- return [];
333
- }
334
316
  break;
335
317
  }
336
318
  if (!(value == null ? void 0 : value.generateCopilotResponse)) {
@@ -338,8 +320,6 @@ function useChat(options) {
338
320
  }
339
321
  threadIdRef.current = value.generateCopilotResponse.threadId || null;
340
322
  runIdRef.current = value.generateCopilotResponse.runId || null;
341
- setThreadId(threadIdRef.current);
342
- setRunId(runIdRef.current);
343
323
  const messages2 = (0, import_runtime_client_gql2.convertGqlOutputToMessages)(
344
324
  (0, import_runtime_client_gql2.filterAdjacentAgentStateMessages)(value.generateCopilotResponse.messages)
345
325
  );
@@ -368,22 +348,12 @@ function useChat(options) {
368
348
  if (action) {
369
349
  followUp = action.followUp;
370
350
  }
371
- const result = yield Promise.race([
372
- onFunctionCall({
373
- messages: previousMessages,
374
- name: message.name,
375
- args: message.arguments
376
- }),
377
- new Promise((_, reject) => {
378
- var _a2;
379
- return (_a2 = chatAbortControllerRef.current) == null ? void 0 : _a2.signal.addEventListener("abort", () => reject(new Error("Operation was aborted")));
380
- })
381
- ]);
382
- if (chatAbortControllerRef.current.signal.aborted) {
383
- actionResults[message.id] = "";
384
- } else {
385
- actionResults[message.id] = result;
386
- }
351
+ const result = yield onFunctionCall({
352
+ messages: previousMessages,
353
+ name: message.name,
354
+ args: message.arguments
355
+ });
356
+ actionResults[message.id] = result;
387
357
  } catch (e) {
388
358
  actionResults[message.id] = `Failed to execute action ${message.name}`;
389
359
  console.error(`Failed to execute action ${message.name}: ${e}`);
@@ -411,7 +381,7 @@ function useChat(options) {
411
381
  }
412
382
  const lastAgentStateMessage = [...messages2].reverse().find((message) => message.isAgentStateMessage());
413
383
  if (lastAgentStateMessage) {
414
- setCoagentStates((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
384
+ setCoagentStatesWithRef((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
415
385
  [lastAgentStateMessage.agentName]: {
416
386
  name: lastAgentStateMessage.agentName,
417
387
  state: lastAgentStateMessage.state,
@@ -478,12 +448,13 @@ function useChat(options) {
478
448
  });
479
449
  const stop = () => {
480
450
  var _a;
481
- (_a = chatAbortControllerRef.current) == null ? void 0 : _a.abort("Stop was called");
451
+ (_a = abortControllerRef.current) == null ? void 0 : _a.abort();
482
452
  };
483
453
  return {
484
454
  append,
485
455
  reload,
486
- stop
456
+ stop,
457
+ runChatCompletion: () => runChatCompletionRef.current(messages)
487
458
  };
488
459
  }
489
460
 
@@ -528,17 +499,11 @@ function useCopilotChat(_a = {}) {
528
499
  setIsLoading,
529
500
  chatInstructions,
530
501
  actions,
531
- coagentStates,
532
- setCoagentStates,
502
+ coagentStatesRef,
503
+ setCoagentStatesWithRef,
533
504
  coAgentStateRenders,
534
505
  agentSession,
535
- setAgentSession,
536
- agentLock,
537
- threadId,
538
- setThreadId,
539
- runId,
540
- setRunId,
541
- chatAbortControllerRef
506
+ setAgentSession
542
507
  } = useCopilotContext();
543
508
  const { messages, setMessages } = useCopilotMessagesContext();
544
509
  const latestGetContextString = useUpdatedRef(getContextString);
@@ -574,7 +539,7 @@ function useCopilotChat(_a = {}) {
574
539
  }),
575
540
  [coAgentStateRenders]
576
541
  );
577
- const { append, reload, stop } = useChat(__spreadProps(__spreadValues({}, options), {
542
+ const { append, reload, stop, runChatCompletion } = useChat(__spreadProps(__spreadValues({}, options), {
578
543
  actions: Object.values(actions),
579
544
  copilotConfig: copilotApiConfig,
580
545
  initialMessages: options.initialMessages || [],
@@ -585,15 +550,10 @@ function useCopilotChat(_a = {}) {
585
550
  makeSystemMessageCallback,
586
551
  isLoading,
587
552
  setIsLoading,
588
- coagentStates,
589
- setCoagentStates,
553
+ coagentStatesRef,
554
+ setCoagentStatesWithRef,
590
555
  agentSession,
591
- setAgentSession,
592
- threadId,
593
- setThreadId,
594
- runId,
595
- setRunId,
596
- chatAbortControllerRef
556
+ setAgentSession
597
557
  }));
598
558
  const latestAppend = useUpdatedRef(append);
599
559
  const latestAppendFunc = (0, import_react7.useCallback)(
@@ -624,32 +584,18 @@ function useCopilotChat(_a = {}) {
624
584
  },
625
585
  [latestSetMessages]
626
586
  );
627
- const reset = (0, import_react7.useCallback)(() => {
628
- latestStopFunc();
629
- setMessages([]);
630
- setThreadId(null);
631
- setRunId(null);
632
- setCoagentStates({});
633
- let initialAgentSession = null;
634
- if (agentLock) {
635
- initialAgentSession = {
636
- agentName: agentLock
637
- };
638
- }
639
- setAgentSession(initialAgentSession);
640
- }, [latestStopFunc, setMessages, setThreadId, setCoagentStates, setAgentSession, agentLock]);
641
- const latestReset = useUpdatedRef(reset);
642
- const latestResetFunc = (0, import_react7.useCallback)(() => {
643
- return latestReset.current();
644
- }, [latestReset]);
587
+ const latestRunChatCompletion = useUpdatedRef(runChatCompletion);
588
+ const latestRunChatCompletionFunc = (0, import_react7.useCallback)(() => {
589
+ return latestRunChatCompletion.current();
590
+ }, [latestRunChatCompletion]);
645
591
  return {
646
592
  visibleMessages: messages,
647
593
  appendMessage: latestAppendFunc,
648
594
  setMessages: latestSetMessagesFunc,
649
595
  reloadMessages: latestReloadFunc,
650
596
  stopGeneration: latestStopFunc,
651
- reset: latestResetFunc,
652
597
  deleteMessage: latestDeleteFunc,
598
+ runChatCompletion: latestRunChatCompletionFunc,
653
599
  isLoading
654
600
  };
655
601
  }
@@ -852,8 +798,8 @@ function useCoAgent(options) {
852
798
  const generalContext = useCopilotContext();
853
799
  const messagesContext = useCopilotMessagesContext();
854
800
  const context = __spreadValues(__spreadValues({}, generalContext), messagesContext);
855
- const { coagentStates, setCoagentStates } = context;
856
- const { appendMessage } = useCopilotChat();
801
+ const { coagentStates, coagentStatesRef, setCoagentStatesWithRef } = context;
802
+ const { appendMessage, runChatCompletion } = useCopilotChat();
857
803
  const getCoagentState = (coagentStates2, name2) => {
858
804
  if (coagentStates2[name2]) {
859
805
  return coagentStates2[name2];
@@ -870,15 +816,13 @@ function useCoAgent(options) {
870
816
  }
871
817
  };
872
818
  const setState = (newState) => {
873
- setCoagentStates((prevAgentStates) => {
874
- let coagentState2 = getCoagentState(prevAgentStates, name);
875
- const updatedState = typeof newState === "function" ? newState(coagentState2.state) : newState;
876
- return __spreadProps(__spreadValues({}, prevAgentStates), {
877
- [name]: __spreadProps(__spreadValues({}, coagentState2), {
878
- state: updatedState
879
- })
880
- });
881
- });
819
+ let coagentState2 = getCoagentState(coagentStatesRef.current || {}, name);
820
+ const updatedState = typeof newState === "function" ? newState(coagentState2.state) : newState;
821
+ setCoagentStatesWithRef(__spreadProps(__spreadValues({}, coagentStatesRef.current), {
822
+ [name]: __spreadProps(__spreadValues({}, coagentState2), {
823
+ state: updatedState
824
+ })
825
+ }));
882
826
  };
883
827
  const coagentState = getCoagentState(coagentStates, name);
884
828
  const state = isExternalStateManagement(options) ? options.state : coagentState.state;
@@ -888,11 +832,7 @@ function useCoAgent(options) {
888
832
  } else if (coagentStates[name] === void 0) {
889
833
  setState(options.initialState === void 0 ? {} : options.initialState);
890
834
  }
891
- }, [
892
- isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0,
893
- // reset initialstate on reset
894
- coagentStates[name] === void 0
895
- ]);
835
+ }, [isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0]);
896
836
  return {
897
837
  name,
898
838
  nodeName: coagentState.nodeName,
@@ -906,7 +846,7 @@ function useCoAgent(options) {
906
846
  stopAgent(name, context);
907
847
  },
908
848
  run: (hint) => {
909
- return runAgent(name, context, appendMessage, hint);
849
+ return runAgent(name, context, appendMessage, runChatCompletion, hint);
910
850
  }
911
851
  };
912
852
  }
@@ -920,22 +860,11 @@ function stopAgent(name, context) {
920
860
  const { agentSession, setAgentSession } = context;
921
861
  if (agentSession && agentSession.agentName === name) {
922
862
  setAgentSession(null);
923
- context.setCoagentStates((prevAgentStates) => {
924
- return __spreadProps(__spreadValues({}, prevAgentStates), {
925
- [name]: __spreadProps(__spreadValues({}, prevAgentStates[name]), {
926
- running: false,
927
- active: false,
928
- threadId: void 0,
929
- nodeName: void 0,
930
- runId: void 0
931
- })
932
- });
933
- });
934
863
  } else {
935
864
  console.warn(`No agent session found for ${name}`);
936
865
  }
937
866
  }
938
- function runAgent(name, context, appendMessage, hint) {
867
+ function runAgent(name, context, appendMessage, runChatCompletion, hint) {
939
868
  return __async(this, null, function* () {
940
869
  var _a, _b;
941
870
  const { agentSession, setAgentSession } = context;
@@ -951,20 +880,21 @@ function runAgent(name, context, appendMessage, hint) {
951
880
  previousState = message.state;
952
881
  }
953
882
  }
954
- let state = ((_b = (_a = context.coagentStates) == null ? void 0 : _a[name]) == null ? void 0 : _b.state) || {};
883
+ let state = ((_b = (_a = context.coagentStatesRef.current) == null ? void 0 : _a[name]) == null ? void 0 : _b.state) || {};
955
884
  if (hint) {
956
885
  const hintMessage = hint({ previousState, currentState: state });
957
886
  if (hintMessage) {
958
887
  yield appendMessage(hintMessage);
888
+ } else {
889
+ yield runChatCompletion();
959
890
  }
891
+ } else {
892
+ yield runChatCompletion();
960
893
  }
961
894
  });
962
895
  }
963
896
  // Annotate the CommonJS export names for ESM import in node:
964
897
  0 && (module.exports = {
965
- runAgent,
966
- startAgent,
967
- stopAgent,
968
898
  useCoAgent,
969
899
  useCoAgentStateRender,
970
900
  useCopilotAction,