@copilotkit/react-core 1.3.6 → 1.3.7-fix-chat-interactions-rerenders.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 (107) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/dist/{chunk-3CU2I4W2.mjs → chunk-2J3X7XCA.mjs} +2 -2
  3. package/dist/{chunk-4AF3YGMC.mjs → chunk-2MPUQRAY.mjs} +1 -3
  4. package/dist/chunk-2MPUQRAY.mjs.map +1 -0
  5. package/dist/{chunk-VZ66ENV7.mjs → chunk-6YEMNWKE.mjs} +41 -31
  6. package/dist/chunk-6YEMNWKE.mjs.map +1 -0
  7. package/dist/{chunk-NTNOUYBQ.mjs → chunk-ANOG3W5S.mjs} +2 -2
  8. package/dist/{chunk-ACK2DHWA.mjs → chunk-CQ23Q555.mjs} +2 -2
  9. package/dist/chunk-DCTJZ742.mjs +22 -0
  10. package/dist/chunk-DCTJZ742.mjs.map +1 -0
  11. package/dist/{chunk-IAS5FL4D.mjs → chunk-EEQJ4OYH.mjs} +2 -2
  12. package/dist/chunk-EEQJ4OYH.mjs.map +1 -0
  13. package/dist/{chunk-2Y3VMFJN.mjs → chunk-GDIP6NYH.mjs} +7 -5
  14. package/dist/chunk-GDIP6NYH.mjs.map +1 -0
  15. package/dist/{chunk-UJWNB2I6.mjs → chunk-RYDEG77L.mjs} +2 -2
  16. package/dist/{chunk-QUCTZQ6X.mjs → chunk-TRXKPXDE.mjs} +2 -2
  17. package/dist/{chunk-KK42A2OC.mjs → chunk-WWNDAWQ4.mjs} +9 -4
  18. package/dist/chunk-WWNDAWQ4.mjs.map +1 -0
  19. package/dist/chunk-XXR4QFAQ.mjs +29 -0
  20. package/dist/chunk-XXR4QFAQ.mjs.map +1 -0
  21. package/dist/chunk-YPSGKPDA.mjs +1 -0
  22. package/dist/components/copilot-provider/copilot-messages.d.ts +7 -0
  23. package/dist/components/copilot-provider/copilot-messages.js +80 -0
  24. package/dist/components/copilot-provider/copilot-messages.js.map +1 -0
  25. package/dist/components/copilot-provider/copilot-messages.mjs +9 -0
  26. package/dist/components/copilot-provider/copilot-messages.mjs.map +1 -0
  27. package/dist/components/copilot-provider/copilotkit.js +85 -52
  28. package/dist/components/copilot-provider/copilotkit.js.map +1 -1
  29. package/dist/components/copilot-provider/copilotkit.mjs +4 -2
  30. package/dist/components/copilot-provider/index.js +85 -52
  31. package/dist/components/copilot-provider/index.js.map +1 -1
  32. package/dist/components/copilot-provider/index.mjs +4 -2
  33. package/dist/components/index.js +85 -52
  34. package/dist/components/index.js.map +1 -1
  35. package/dist/components/index.mjs +4 -2
  36. package/dist/context/copilot-context.d.ts +0 -3
  37. package/dist/context/copilot-context.js +0 -2
  38. package/dist/context/copilot-context.js.map +1 -1
  39. package/dist/context/copilot-context.mjs +1 -1
  40. package/dist/context/copilot-messages-context.d.ts +15 -0
  41. package/dist/context/copilot-messages-context.js +57 -0
  42. package/dist/context/copilot-messages-context.js.map +1 -0
  43. package/dist/context/copilot-messages-context.mjs +10 -0
  44. package/dist/context/copilot-messages-context.mjs.map +1 -0
  45. package/dist/context/index.d.ts +2 -1
  46. package/dist/context/index.js +23 -4
  47. package/dist/context/index.js.map +1 -1
  48. package/dist/context/index.mjs +9 -3
  49. package/dist/hooks/index.js +51 -33
  50. package/dist/hooks/index.js.map +1 -1
  51. package/dist/hooks/index.mjs +17 -15
  52. package/dist/hooks/use-coagent-state-render.js +0 -2
  53. package/dist/hooks/use-coagent-state-render.js.map +1 -1
  54. package/dist/hooks/use-coagent-state-render.mjs +2 -2
  55. package/dist/hooks/use-coagent.js +42 -26
  56. package/dist/hooks/use-coagent.js.map +1 -1
  57. package/dist/hooks/use-coagent.mjs +7 -5
  58. package/dist/hooks/use-copilot-action.js +0 -2
  59. package/dist/hooks/use-copilot-action.js.map +1 -1
  60. package/dist/hooks/use-copilot-action.mjs +2 -2
  61. package/dist/hooks/use-copilot-chat.js +32 -16
  62. package/dist/hooks/use-copilot-chat.js.map +1 -1
  63. package/dist/hooks/use-copilot-chat.mjs +6 -3
  64. package/dist/hooks/use-copilot-readable.js +0 -2
  65. package/dist/hooks/use-copilot-readable.js.map +1 -1
  66. package/dist/hooks/use-copilot-readable.mjs +2 -2
  67. package/dist/hooks/use-make-copilot-document-readable.js +0 -2
  68. package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
  69. package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
  70. package/dist/index.d.ts +2 -1
  71. package/dist/index.js +136 -89
  72. package/dist/index.js.map +1 -1
  73. package/dist/index.mjs +25 -18
  74. package/dist/lib/copilot-task.d.ts +0 -1
  75. package/dist/lib/copilot-task.js.map +1 -1
  76. package/dist/lib/copilot-task.mjs +5 -3
  77. package/dist/lib/index.d.ts +0 -1
  78. package/dist/lib/index.js.map +1 -1
  79. package/dist/lib/index.mjs +5 -3
  80. package/dist/utils/extract.d.ts +2 -1
  81. package/dist/utils/extract.js.map +1 -1
  82. package/dist/utils/extract.mjs +5 -3
  83. package/dist/utils/index.d.ts +1 -0
  84. package/dist/utils/index.js.map +1 -1
  85. package/dist/utils/index.mjs +5 -3
  86. package/package.json +5 -5
  87. package/setup.jest.ts +19 -0
  88. package/src/components/copilot-provider/copilot-messages.tsx +23 -0
  89. package/src/components/copilot-provider/copilotkit.tsx +36 -30
  90. package/src/context/copilot-context.tsx +0 -8
  91. package/src/context/copilot-messages-context.tsx +29 -0
  92. package/src/context/index.ts +2 -0
  93. package/src/hooks/use-coagent.ts +10 -3
  94. package/src/hooks/use-copilot-chat.ts +2 -2
  95. package/src/utils/extract.ts +2 -2
  96. package/dist/chunk-2Y3VMFJN.mjs.map +0 -1
  97. package/dist/chunk-4AF3YGMC.mjs.map +0 -1
  98. package/dist/chunk-IAS5FL4D.mjs.map +0 -1
  99. package/dist/chunk-KK42A2OC.mjs.map +0 -1
  100. package/dist/chunk-VNRDQJXW.mjs +0 -1
  101. package/dist/chunk-VZ66ENV7.mjs.map +0 -1
  102. /package/dist/{chunk-3CU2I4W2.mjs.map → chunk-2J3X7XCA.mjs.map} +0 -0
  103. /package/dist/{chunk-NTNOUYBQ.mjs.map → chunk-ANOG3W5S.mjs.map} +0 -0
  104. /package/dist/{chunk-ACK2DHWA.mjs.map → chunk-CQ23Q555.mjs.map} +0 -0
  105. /package/dist/{chunk-UJWNB2I6.mjs.map → chunk-RYDEG77L.mjs.map} +0 -0
  106. /package/dist/{chunk-QUCTZQ6X.mjs.map → chunk-TRXKPXDE.mjs.map} +0 -0
  107. /package/dist/{chunk-VNRDQJXW.mjs.map → chunk-YPSGKPDA.mjs.map} +0 -0
package/dist/index.js CHANGED
@@ -83,6 +83,7 @@ var src_exports = {};
83
83
  __export(src_exports, {
84
84
  CopilotContext: () => CopilotContext,
85
85
  CopilotKit: () => CopilotKit,
86
+ CopilotMessagesContext: () => CopilotMessagesContext,
86
87
  CopilotTask: () => CopilotTask,
87
88
  defaultCopilotContextCategories: () => defaultCopilotContextCategories,
88
89
  extract: () => extract,
@@ -91,13 +92,14 @@ __export(src_exports, {
91
92
  useCopilotAction: () => useCopilotAction,
92
93
  useCopilotChat: () => useCopilotChat,
93
94
  useCopilotContext: () => useCopilotContext,
95
+ useCopilotMessagesContext: () => useCopilotMessagesContext,
94
96
  useCopilotReadable: () => useCopilotReadable,
95
97
  useMakeCopilotDocumentReadable: () => useMakeCopilotDocumentReadable
96
98
  });
97
99
  module.exports = __toCommonJS(src_exports);
98
100
 
99
101
  // src/components/copilot-provider/copilotkit.tsx
100
- var import_react4 = require("react");
102
+ var import_react6 = require("react");
101
103
 
102
104
  // src/context/copilot-context.tsx
103
105
  var import_react = __toESM(require("react"));
@@ -119,8 +121,6 @@ var emptyCopilotContext = {
119
121
  },
120
122
  getFunctionCallHandler: () => returnAndThrowInDebug(() => __async(void 0, null, function* () {
121
123
  })),
122
- messages: [],
123
- setMessages: () => returnAndThrowInDebug([]),
124
124
  isLoading: false,
125
125
  setIsLoading: () => returnAndThrowInDebug(false),
126
126
  chatInstructions: "",
@@ -367,61 +367,96 @@ function setsHaveIntersection2(setA, setB) {
367
367
  return false;
368
368
  }
369
369
 
370
- // src/components/copilot-provider/copilotkit.tsx
370
+ // src/components/copilot-provider/copilot-messages.tsx
371
+ var import_react5 = require("react");
372
+
373
+ // src/context/copilot-messages-context.tsx
374
+ var import_react4 = __toESM(require("react"));
375
+ var emptyCopilotContext2 = {
376
+ messages: [],
377
+ setMessages: () => []
378
+ };
379
+ var CopilotMessagesContext = import_react4.default.createContext(emptyCopilotContext2);
380
+ function useCopilotMessagesContext() {
381
+ const context = import_react4.default.useContext(CopilotMessagesContext);
382
+ if (context === emptyCopilotContext2) {
383
+ throw new Error(
384
+ "A messages consuming component was not wrapped with `<CopilotMessages> {...} </CopilotMessages>`"
385
+ );
386
+ }
387
+ return context;
388
+ }
389
+
390
+ // src/components/copilot-provider/copilot-messages.tsx
371
391
  var import_jsx_runtime = require("react/jsx-runtime");
392
+ function CopilotMessages(_a) {
393
+ var _b = _a, { children } = _b, props = __objRest(_b, ["children"]);
394
+ const [messages, setMessages] = (0, import_react5.useState)([]);
395
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
396
+ CopilotMessagesContext.Provider,
397
+ {
398
+ value: {
399
+ messages,
400
+ setMessages
401
+ },
402
+ children
403
+ }
404
+ );
405
+ }
406
+
407
+ // src/components/copilot-provider/copilotkit.tsx
408
+ var import_jsx_runtime2 = require("react/jsx-runtime");
372
409
  function CopilotKit(_a) {
373
410
  var _b = _a, { children } = _b, props = __objRest(_b, ["children"]);
374
- var _a2, _b2;
375
411
  if (!props.runtimeUrl && !props.publicApiKey) {
376
412
  throw new Error(
377
413
  "Please provide either a runtimeUrl or a publicApiKey to the CopilotKit component."
378
414
  );
379
415
  }
380
416
  const chatApiEndpoint = props.runtimeUrl || import_shared3.COPILOT_CLOUD_CHAT_URL;
381
- const [actions, setActions] = (0, import_react4.useState)({});
382
- const [coAgentStateRenders, setCoAgentStateRenders] = (0, import_react4.useState)({});
383
- const chatComponentsCache = (0, import_react4.useRef)({
417
+ const [actions, setActions] = (0, import_react6.useState)({});
418
+ const [coAgentStateRenders, setCoAgentStateRenders] = (0, import_react6.useState)({});
419
+ const chatComponentsCache = (0, import_react6.useRef)({
384
420
  actions: {},
385
421
  coAgentStateRenders: {}
386
422
  });
387
423
  const { addElement, removeElement, printTree } = use_tree_default();
388
- const [messages, setMessages] = (0, import_react4.useState)([]);
389
- const [isLoading, setIsLoading] = (0, import_react4.useState)(false);
390
- const [chatInstructions, setChatInstructions] = (0, import_react4.useState)("");
424
+ const [isLoading, setIsLoading] = (0, import_react6.useState)(false);
425
+ const [chatInstructions, setChatInstructions] = (0, import_react6.useState)("");
391
426
  const {
392
427
  addElement: addDocument,
393
428
  removeElement: removeDocument,
394
429
  allElements: allDocuments
395
430
  } = use_flat_category_store_default();
396
- const setAction = (0, import_react4.useCallback)((id, action) => {
431
+ const setAction = (0, import_react6.useCallback)((id, action) => {
397
432
  setActions((prevPoints) => {
398
433
  return __spreadProps(__spreadValues({}, prevPoints), {
399
434
  [id]: action
400
435
  });
401
436
  });
402
437
  }, []);
403
- const removeAction = (0, import_react4.useCallback)((id) => {
438
+ const removeAction = (0, import_react6.useCallback)((id) => {
404
439
  setActions((prevPoints) => {
405
440
  const newPoints = __spreadValues({}, prevPoints);
406
441
  delete newPoints[id];
407
442
  return newPoints;
408
443
  });
409
444
  }, []);
410
- const setCoAgentStateRender = (0, import_react4.useCallback)((id, stateRender) => {
445
+ const setCoAgentStateRender = (0, import_react6.useCallback)((id, stateRender) => {
411
446
  setCoAgentStateRenders((prevPoints) => {
412
447
  return __spreadProps(__spreadValues({}, prevPoints), {
413
448
  [id]: stateRender
414
449
  });
415
450
  });
416
451
  }, []);
417
- const removeCoAgentStateRender = (0, import_react4.useCallback)((id) => {
452
+ const removeCoAgentStateRender = (0, import_react6.useCallback)((id) => {
418
453
  setCoAgentStateRenders((prevPoints) => {
419
454
  const newPoints = __spreadValues({}, prevPoints);
420
455
  delete newPoints[id];
421
456
  return newPoints;
422
457
  });
423
458
  }, []);
424
- const getContextString = (0, import_react4.useCallback)(
459
+ const getContextString = (0, import_react6.useCallback)(
425
460
  (documents, categories) => {
426
461
  const documentsString = documents.map((document) => {
427
462
  return `${document.name} (${document.sourceApplication}):
@@ -434,37 +469,37 @@ ${nonDocumentStrings}`;
434
469
  },
435
470
  [printTree]
436
471
  );
437
- const addContext = (0, import_react4.useCallback)(
472
+ const addContext = (0, import_react6.useCallback)(
438
473
  (context, parentId, categories = defaultCopilotContextCategories) => {
439
474
  return addElement(context, categories, parentId);
440
475
  },
441
476
  [addElement]
442
477
  );
443
- const removeContext = (0, import_react4.useCallback)(
478
+ const removeContext = (0, import_react6.useCallback)(
444
479
  (id) => {
445
480
  removeElement(id);
446
481
  },
447
482
  [removeElement]
448
483
  );
449
- const getFunctionCallHandler = (0, import_react4.useCallback)(
484
+ const getFunctionCallHandler = (0, import_react6.useCallback)(
450
485
  (customEntryPoints) => {
451
486
  return entryPointsToFunctionCallHandler(Object.values(customEntryPoints || actions));
452
487
  },
453
488
  [actions]
454
489
  );
455
- const getDocumentsContext = (0, import_react4.useCallback)(
490
+ const getDocumentsContext = (0, import_react6.useCallback)(
456
491
  (categories) => {
457
492
  return allDocuments(categories);
458
493
  },
459
494
  [allDocuments]
460
495
  );
461
- const addDocumentContext = (0, import_react4.useCallback)(
496
+ const addDocumentContext = (0, import_react6.useCallback)(
462
497
  (documentPointer, categories = defaultCopilotContextCategories) => {
463
498
  return addDocument(documentPointer, categories);
464
499
  },
465
500
  [addDocument]
466
501
  );
467
- const removeDocumentContext = (0, import_react4.useCallback)(
502
+ const removeDocumentContext = (0, import_react6.useCallback)(
468
503
  (documentId) => {
469
504
  removeDocument(documentId);
470
505
  },
@@ -477,49 +512,60 @@ ${nonDocumentStrings}`;
477
512
  );
478
513
  }
479
514
  }
480
- let cloud = void 0;
481
- if (props.publicApiKey) {
482
- cloud = {
483
- guardrails: {
484
- input: {
485
- restrictToTopic: {
486
- enabled: props.cloudRestrictToTopic ? true : false,
487
- validTopics: ((_a2 = props.cloudRestrictToTopic) == null ? void 0 : _a2.validTopics) || [],
488
- invalidTopics: ((_b2 = props.cloudRestrictToTopic) == null ? void 0 : _b2.invalidTopics) || []
515
+ const copilotApiConfig = (0, import_react6.useMemo)(() => {
516
+ var _a2, _b2;
517
+ let cloud = void 0;
518
+ if (props.publicApiKey) {
519
+ cloud = {
520
+ guardrails: {
521
+ input: {
522
+ restrictToTopic: {
523
+ enabled: props.cloudRestrictToTopic ? true : false,
524
+ validTopics: ((_a2 = props.cloudRestrictToTopic) == null ? void 0 : _a2.validTopics) || [],
525
+ invalidTopics: ((_b2 = props.cloudRestrictToTopic) == null ? void 0 : _b2.invalidTopics) || []
526
+ }
489
527
  }
490
528
  }
491
- }
492
- };
493
- }
494
- const copilotApiConfig = __spreadProps(__spreadValues({
495
- publicApiKey: props.publicApiKey
496
- }, cloud ? { cloud } : {}), {
497
- chatApiEndpoint,
498
- headers: props.headers || {},
499
- properties: props.properties || {},
500
- transcribeAudioUrl: props.transcribeAudioUrl,
501
- textToSpeechUrl: props.textToSpeechUrl,
502
- credentials: props.credentials
503
- });
504
- const [chatSuggestionConfiguration, setChatSuggestionConfiguration] = (0, import_react4.useState)({});
529
+ };
530
+ }
531
+ return __spreadProps(__spreadValues({
532
+ publicApiKey: props.publicApiKey
533
+ }, cloud ? { cloud } : {}), {
534
+ chatApiEndpoint,
535
+ headers: props.headers || {},
536
+ properties: props.properties || {},
537
+ transcribeAudioUrl: props.transcribeAudioUrl,
538
+ textToSpeechUrl: props.textToSpeechUrl,
539
+ credentials: props.credentials
540
+ });
541
+ }, [
542
+ props.publicApiKey,
543
+ props.headers,
544
+ props.properties,
545
+ props.transcribeAudioUrl,
546
+ props.textToSpeechUrl,
547
+ props.credentials,
548
+ props.cloudRestrictToTopic
549
+ ]);
550
+ const [chatSuggestionConfiguration, setChatSuggestionConfiguration] = (0, import_react6.useState)({});
505
551
  const addChatSuggestionConfiguration = (id, suggestion) => {
506
552
  setChatSuggestionConfiguration((prev) => __spreadProps(__spreadValues({}, prev), { [id]: suggestion }));
507
553
  };
508
554
  const removeChatSuggestionConfiguration = (id) => {
509
555
  setChatSuggestionConfiguration((prev) => {
510
- const _a3 = prev, { [id]: _ } = _a3, rest = __objRest(_a3, [__restKey(id)]);
556
+ const _a2 = prev, { [id]: _ } = _a2, rest = __objRest(_a2, [__restKey(id)]);
511
557
  return rest;
512
558
  });
513
559
  };
514
- const [coagentStates, setCoagentStates] = (0, import_react4.useState)({});
560
+ const [coagentStates, setCoagentStates] = (0, import_react6.useState)({});
515
561
  let initialAgentSession = null;
516
562
  if (props.agent) {
517
563
  initialAgentSession = {
518
564
  agentName: props.agent
519
565
  };
520
566
  }
521
- const [agentSession, setAgentSession] = (0, import_react4.useState)(initialAgentSession);
522
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
567
+ const [agentSession, setAgentSession] = (0, import_react6.useState)(initialAgentSession);
568
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
523
569
  CopilotContext.Provider,
524
570
  {
525
571
  value: {
@@ -538,8 +584,6 @@ ${nonDocumentStrings}`;
538
584
  addDocumentContext,
539
585
  removeDocumentContext,
540
586
  copilotApiConfig,
541
- messages,
542
- setMessages,
543
587
  isLoading,
544
588
  setIsLoading,
545
589
  chatSuggestionConfiguration,
@@ -553,7 +597,7 @@ ${nonDocumentStrings}`;
553
597
  agentSession,
554
598
  setAgentSession
555
599
  },
556
- children
600
+ children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(CopilotMessages, { children })
557
601
  }
558
602
  );
559
603
  }
@@ -585,11 +629,11 @@ function entryPointsToFunctionCallHandler(actions) {
585
629
  }
586
630
 
587
631
  // src/hooks/use-copilot-chat.ts
588
- var import_react6 = require("react");
632
+ var import_react8 = require("react");
589
633
  var import_runtime_client_gql2 = require("@copilotkit/runtime-client-gql");
590
634
 
591
635
  // src/hooks/use-chat.ts
592
- var import_react5 = require("react");
636
+ var import_react7 = require("react");
593
637
  var import_shared4 = require("@copilotkit/shared");
594
638
  var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
595
639
  function useChat(options) {
@@ -609,13 +653,13 @@ function useChat(options) {
609
653
  agentSession,
610
654
  setAgentSession
611
655
  } = options;
612
- const abortControllerRef = (0, import_react5.useRef)();
613
- const threadIdRef = (0, import_react5.useRef)(null);
614
- const runIdRef = (0, import_react5.useRef)(null);
615
- const runChatCompletionRef = (0, import_react5.useRef)();
616
- const coagentStatesRef = (0, import_react5.useRef)(coagentStates);
656
+ const abortControllerRef = (0, import_react7.useRef)();
657
+ const threadIdRef = (0, import_react7.useRef)(null);
658
+ const runIdRef = (0, import_react7.useRef)(null);
659
+ const runChatCompletionRef = (0, import_react7.useRef)();
660
+ const coagentStatesRef = (0, import_react7.useRef)(coagentStates);
617
661
  coagentStatesRef.current = coagentStates;
618
- const agentSessionRef = (0, import_react5.useRef)(agentSession);
662
+ const agentSessionRef = (0, import_react7.useRef)(agentSession);
619
663
  agentSessionRef.current = agentSession;
620
664
  const publicApiKey = copilotConfig.publicApiKey;
621
665
  const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [import_shared4.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
@@ -842,8 +886,6 @@ function useCopilotChat(_a = {}) {
842
886
  getContextString,
843
887
  getFunctionCallHandler,
844
888
  copilotApiConfig,
845
- messages,
846
- setMessages,
847
889
  isLoading,
848
890
  setIsLoading,
849
891
  chatInstructions,
@@ -854,14 +896,15 @@ function useCopilotChat(_a = {}) {
854
896
  agentSession,
855
897
  setAgentSession
856
898
  } = useCopilotContext();
899
+ const { messages, setMessages } = useCopilotMessagesContext();
857
900
  const latestGetContextString = useUpdatedRef(getContextString);
858
- const deleteMessage = (0, import_react6.useCallback)(
901
+ const deleteMessage = (0, import_react8.useCallback)(
859
902
  (messageId) => {
860
903
  setMessages((prev) => prev.filter((message) => message.id !== messageId));
861
904
  },
862
905
  [setMessages]
863
906
  );
864
- const makeSystemMessageCallback = (0, import_react6.useCallback)(() => {
907
+ const makeSystemMessageCallback = (0, import_react8.useCallback)(() => {
865
908
  const systemMessageMaker = makeSystemMessage2 || defaultSystemMessage;
866
909
  const contextString = latestGetContextString.current([], defaultCopilotContextCategories);
867
910
  return new import_runtime_client_gql2.TextMessage({
@@ -869,7 +912,7 @@ function useCopilotChat(_a = {}) {
869
912
  role: import_runtime_client_gql2.Role.System
870
913
  });
871
914
  }, [getContextString, makeSystemMessage2, chatInstructions]);
872
- const onCoAgentStateRender = (0, import_react6.useCallback)(
915
+ const onCoAgentStateRender = (0, import_react8.useCallback)(
873
916
  (args) => __async(this, null, function* () {
874
917
  var _a2;
875
918
  const { name, nodeName, state } = args;
@@ -904,29 +947,29 @@ function useCopilotChat(_a = {}) {
904
947
  setAgentSession
905
948
  }));
906
949
  const latestAppend = useUpdatedRef(append);
907
- const latestAppendFunc = (0, import_react6.useCallback)(
950
+ const latestAppendFunc = (0, import_react8.useCallback)(
908
951
  (message) => {
909
952
  return latestAppend.current(message);
910
953
  },
911
954
  [latestAppend]
912
955
  );
913
956
  const latestReload = useUpdatedRef(reload);
914
- const latestReloadFunc = (0, import_react6.useCallback)(() => {
957
+ const latestReloadFunc = (0, import_react8.useCallback)(() => {
915
958
  return latestReload.current();
916
959
  }, [latestReload]);
917
960
  const latestStop = useUpdatedRef(stop);
918
- const latestStopFunc = (0, import_react6.useCallback)(() => {
961
+ const latestStopFunc = (0, import_react8.useCallback)(() => {
919
962
  return latestStop.current();
920
963
  }, [latestStop]);
921
964
  const latestDelete = useUpdatedRef(deleteMessage);
922
- const latestDeleteFunc = (0, import_react6.useCallback)(
965
+ const latestDeleteFunc = (0, import_react8.useCallback)(
923
966
  (messageId) => {
924
967
  return latestDelete.current(messageId);
925
968
  },
926
969
  [latestDelete]
927
970
  );
928
971
  const latestSetMessages = useUpdatedRef(setMessages);
929
- const latestSetMessagesFunc = (0, import_react6.useCallback)(
972
+ const latestSetMessagesFunc = (0, import_react8.useCallback)(
930
973
  (messages2) => {
931
974
  return latestSetMessages.current(messages2);
932
975
  },
@@ -943,8 +986,8 @@ function useCopilotChat(_a = {}) {
943
986
  };
944
987
  }
945
988
  function useUpdatedRef(value) {
946
- const ref = (0, import_react6.useRef)(value);
947
- (0, import_react6.useEffect)(() => {
989
+ const ref = (0, import_react8.useRef)(value);
990
+ (0, import_react8.useEffect)(() => {
948
991
  ref.current = value;
949
992
  }, [value]);
950
993
  return ref;
@@ -974,12 +1017,12 @@ ${additionalInstructions}` : "");
974
1017
  }
975
1018
 
976
1019
  // src/hooks/use-copilot-action.ts
977
- var import_react7 = require("react");
1020
+ var import_react9 = require("react");
978
1021
  var import_shared5 = require("@copilotkit/shared");
979
1022
  function useCopilotAction(action, dependencies) {
980
1023
  const { setAction, removeAction, actions, chatComponentsCache } = useCopilotContext();
981
- const idRef = (0, import_react7.useRef)((0, import_shared5.randomId)());
982
- const renderAndWaitRef = (0, import_react7.useRef)(null);
1024
+ const idRef = (0, import_react9.useRef)((0, import_shared5.randomId)());
1025
+ const renderAndWaitRef = (0, import_react9.useRef)(null);
983
1026
  action = __spreadValues({}, action);
984
1027
  if (action.renderAndWait) {
985
1028
  const renderAndWait = action.renderAndWait;
@@ -1014,7 +1057,7 @@ function useCopilotAction(action, dependencies) {
1014
1057
  }
1015
1058
  }
1016
1059
  }
1017
- (0, import_react7.useEffect)(() => {
1060
+ (0, import_react9.useEffect)(() => {
1018
1061
  setAction(idRef.current, action);
1019
1062
  if (chatComponentsCache.current !== null && action.render !== void 0) {
1020
1063
  chatComponentsCache.current.actions[action.name] = action.render;
@@ -1039,7 +1082,7 @@ function useCopilotAction(action, dependencies) {
1039
1082
  }
1040
1083
 
1041
1084
  // src/hooks/use-coagent-state-render.ts
1042
- var import_react8 = require("react");
1085
+ var import_react10 = require("react");
1043
1086
  var import_shared6 = require("@copilotkit/shared");
1044
1087
  function useCoAgentStateRender(action, dependencies) {
1045
1088
  const {
@@ -1047,8 +1090,8 @@ function useCoAgentStateRender(action, dependencies) {
1047
1090
  removeCoAgentStateRender,
1048
1091
  coAgentStateRenders,
1049
1092
  chatComponentsCache
1050
- } = (0, import_react8.useContext)(CopilotContext);
1051
- const idRef = (0, import_react8.useRef)((0, import_shared6.randomId)());
1093
+ } = (0, import_react10.useContext)(CopilotContext);
1094
+ const idRef = (0, import_react10.useRef)((0, import_shared6.randomId)());
1052
1095
  const key = `${action.name}-${action.nodeName || "global"}`;
1053
1096
  if (dependencies === void 0) {
1054
1097
  if (coAgentStateRenders[idRef.current]) {
@@ -1060,7 +1103,7 @@ function useCoAgentStateRender(action, dependencies) {
1060
1103
  }
1061
1104
  }
1062
1105
  }
1063
- (0, import_react8.useEffect)(() => {
1106
+ (0, import_react10.useEffect)(() => {
1064
1107
  setCoAgentStateRender(idRef.current, action);
1065
1108
  if (chatComponentsCache.current !== null && action.render !== void 0) {
1066
1109
  chatComponentsCache.current.coAgentStateRenders[key] = action.render;
@@ -1080,11 +1123,11 @@ function useCoAgentStateRender(action, dependencies) {
1080
1123
  }
1081
1124
 
1082
1125
  // src/hooks/use-make-copilot-document-readable.ts
1083
- var import_react9 = require("react");
1126
+ var import_react11 = require("react");
1084
1127
  function useMakeCopilotDocumentReadable(document, categories, dependencies = []) {
1085
1128
  const { addDocumentContext, removeDocumentContext } = useCopilotContext();
1086
- const idRef = (0, import_react9.useRef)();
1087
- (0, import_react9.useEffect)(() => {
1129
+ const idRef = (0, import_react11.useRef)();
1130
+ (0, import_react11.useEffect)(() => {
1088
1131
  const id = addDocumentContext(document, categories);
1089
1132
  idRef.current = id;
1090
1133
  return () => {
@@ -1095,16 +1138,16 @@ function useMakeCopilotDocumentReadable(document, categories, dependencies = [])
1095
1138
  }
1096
1139
 
1097
1140
  // src/hooks/use-copilot-readable.ts
1098
- var import_react10 = require("react");
1141
+ var import_react12 = require("react");
1099
1142
  function convertToJSON(description, value) {
1100
1143
  return `${description}: ${typeof value === "string" ? value : JSON.stringify(value)}`;
1101
1144
  }
1102
1145
  function useCopilotReadable({ description, value, parentId, categories, convert }, dependencies) {
1103
1146
  const { addContext, removeContext } = useCopilotContext();
1104
- const idRef = (0, import_react10.useRef)();
1147
+ const idRef = (0, import_react12.useRef)();
1105
1148
  convert = convert || convertToJSON;
1106
1149
  const information = convert(description, value);
1107
- (0, import_react10.useEffect)(() => {
1150
+ (0, import_react12.useEffect)(() => {
1108
1151
  const id = addContext(information, parentId, categories);
1109
1152
  idRef.current = id;
1110
1153
  return () => {
@@ -1115,7 +1158,7 @@ function useCopilotReadable({ description, value, parentId, categories, convert
1115
1158
  }
1116
1159
 
1117
1160
  // src/hooks/use-coagent.ts
1118
- var import_react11 = require("react");
1161
+ var import_react13 = require("react");
1119
1162
  var import_runtime_client_gql3 = require("@copilotkit/runtime-client-gql");
1120
1163
  function useCoAgent(options) {
1121
1164
  const isExternalStateManagement = (options2) => {
@@ -1125,7 +1168,9 @@ function useCoAgent(options) {
1125
1168
  const isInternalStateManagementWithInitial = (options2) => {
1126
1169
  return "initialState" in options2;
1127
1170
  };
1128
- const context = useCopilotContext();
1171
+ const generalContext = useCopilotContext();
1172
+ const messagesContext = useCopilotMessagesContext();
1173
+ const context = __spreadValues(__spreadValues({}, generalContext), messagesContext);
1129
1174
  const { coagentStates, setCoagentStates } = context;
1130
1175
  const { appendMessage } = useCopilotChat();
1131
1176
  const getCoagentState = (coagentStates2, name2) => {
@@ -1156,7 +1201,7 @@ function useCoAgent(options) {
1156
1201
  };
1157
1202
  const coagentState = getCoagentState(coagentStates, name);
1158
1203
  const state = isExternalStateManagement(options) ? options.state : coagentState.state;
1159
- (0, import_react11.useEffect)(() => {
1204
+ (0, import_react13.useEffect)(() => {
1160
1205
  if (isExternalStateManagement(options)) {
1161
1206
  setState(options.state);
1162
1207
  } else if (coagentStates[name] === void 0) {
@@ -1456,6 +1501,7 @@ Any additional messages provided are for providing context only and should not b
1456
1501
  0 && (module.exports = {
1457
1502
  CopilotContext,
1458
1503
  CopilotKit,
1504
+ CopilotMessagesContext,
1459
1505
  CopilotTask,
1460
1506
  defaultCopilotContextCategories,
1461
1507
  extract,
@@ -1464,6 +1510,7 @@ Any additional messages provided are for providing context only and should not b
1464
1510
  useCopilotAction,
1465
1511
  useCopilotChat,
1466
1512
  useCopilotContext,
1513
+ useCopilotMessagesContext,
1467
1514
  useCopilotReadable,
1468
1515
  useMakeCopilotDocumentReadable
1469
1516
  });