@copilotkit/react-core 1.4.5 → 1.4.7

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 (228) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/chunk-2KCEHGSI.mjs +192 -0
  3. package/dist/chunk-2KCEHGSI.mjs.map +1 -0
  4. package/dist/chunk-5FHSUKQL.mjs +70 -0
  5. package/dist/chunk-5FHSUKQL.mjs.map +1 -0
  6. package/dist/chunk-6U3UH3KO.mjs +140 -0
  7. package/dist/chunk-6U3UH3KO.mjs.map +1 -0
  8. package/dist/chunk-AG7FH7OD.mjs +28 -0
  9. package/dist/chunk-AG7FH7OD.mjs.map +1 -0
  10. package/dist/chunk-DCTJZ742.mjs +22 -0
  11. package/dist/chunk-DCTJZ742.mjs.map +1 -0
  12. package/dist/chunk-DHGXL5PC.mjs +439 -0
  13. package/dist/chunk-DHGXL5PC.mjs.map +1 -0
  14. package/dist/chunk-EFZPSZWO.mjs +1 -0
  15. package/dist/chunk-EFZPSZWO.mjs.map +1 -0
  16. package/dist/chunk-EUIBVFV6.mjs +294 -0
  17. package/dist/chunk-EUIBVFV6.mjs.map +1 -0
  18. package/dist/chunk-JHEAUB3Z.mjs +49 -0
  19. package/dist/chunk-JHEAUB3Z.mjs.map +1 -0
  20. package/dist/chunk-L34MHAXR.mjs +1 -0
  21. package/dist/chunk-L34MHAXR.mjs.map +1 -0
  22. package/dist/chunk-LUGEI4YQ.mjs +1 -0
  23. package/dist/chunk-LUGEI4YQ.mjs.map +1 -0
  24. package/dist/chunk-MLAS4QUR.mjs +18 -0
  25. package/dist/chunk-MLAS4QUR.mjs.map +1 -0
  26. package/dist/chunk-O22KGHOQ.mjs +128 -0
  27. package/dist/chunk-O22KGHOQ.mjs.map +1 -0
  28. package/dist/chunk-O7ARI5CV.mjs +31 -0
  29. package/dist/chunk-O7ARI5CV.mjs.map +1 -0
  30. package/dist/chunk-ODN4H66E.mjs +31 -0
  31. package/dist/chunk-ODN4H66E.mjs.map +1 -0
  32. package/dist/chunk-OT67R4NB.mjs +119 -0
  33. package/dist/chunk-OT67R4NB.mjs.map +1 -0
  34. package/dist/chunk-SKC7AJIV.mjs +61 -0
  35. package/dist/chunk-SKC7AJIV.mjs.map +1 -0
  36. package/dist/chunk-UOVONDR6.mjs +23 -0
  37. package/dist/chunk-UOVONDR6.mjs.map +1 -0
  38. package/dist/chunk-XBVKTDXP.mjs +172 -0
  39. package/dist/chunk-XBVKTDXP.mjs.map +1 -0
  40. package/dist/chunk-XQFVXX6R.mjs +78 -0
  41. package/dist/chunk-XQFVXX6R.mjs.map +1 -0
  42. package/dist/chunk-XXR4QFAQ.mjs +29 -0
  43. package/dist/chunk-XXR4QFAQ.mjs.map +1 -0
  44. package/dist/chunk-Y7MI4PBB.mjs +91 -0
  45. package/dist/chunk-Y7MI4PBB.mjs.map +1 -0
  46. package/dist/chunk-YPSGKPDA.mjs +1 -0
  47. package/dist/chunk-YPSGKPDA.mjs.map +1 -0
  48. package/dist/components/copilot-provider/copilot-messages.d.ts +7 -0
  49. package/dist/components/copilot-provider/copilot-messages.js +80 -0
  50. package/dist/components/copilot-provider/copilot-messages.js.map +1 -0
  51. package/dist/components/copilot-provider/copilot-messages.mjs +9 -0
  52. package/dist/components/copilot-provider/copilot-messages.mjs.map +1 -0
  53. package/dist/components/copilot-provider/copilotkit-props.d.ts +72 -0
  54. package/dist/components/copilot-provider/copilotkit-props.js +19 -0
  55. package/dist/components/copilot-provider/copilotkit-props.js.map +1 -0
  56. package/dist/components/copilot-provider/copilotkit-props.mjs +1 -0
  57. package/dist/components/copilot-provider/copilotkit-props.mjs.map +1 -0
  58. package/dist/components/copilot-provider/copilotkit.d.ts +9 -0
  59. package/dist/components/copilot-provider/copilotkit.js +877 -0
  60. package/dist/components/copilot-provider/copilotkit.js.map +1 -0
  61. package/dist/components/copilot-provider/copilotkit.mjs +21 -0
  62. package/dist/components/copilot-provider/copilotkit.mjs.map +1 -0
  63. package/dist/components/copilot-provider/index.d.ts +4 -0
  64. package/dist/components/copilot-provider/index.js +877 -0
  65. package/dist/components/copilot-provider/index.js.map +1 -0
  66. package/dist/components/copilot-provider/index.mjs +19 -0
  67. package/dist/components/copilot-provider/index.mjs.map +1 -0
  68. package/dist/components/index.d.ts +4 -0
  69. package/dist/components/index.js +877 -0
  70. package/dist/components/index.js.map +1 -0
  71. package/dist/components/index.mjs +19 -0
  72. package/dist/components/index.mjs.map +1 -0
  73. package/dist/components/toast/exclamation-mark-icon.d.ts +9 -0
  74. package/dist/components/toast/exclamation-mark-icon.js +55 -0
  75. package/dist/components/toast/exclamation-mark-icon.js.map +1 -0
  76. package/dist/components/toast/exclamation-mark-icon.mjs +8 -0
  77. package/dist/components/toast/exclamation-mark-icon.mjs.map +1 -0
  78. package/dist/components/toast/toast-provider.d.ts +29 -0
  79. package/dist/components/toast/toast-provider.js +255 -0
  80. package/dist/components/toast/toast-provider.js.map +1 -0
  81. package/dist/components/toast/toast-provider.mjs +11 -0
  82. package/dist/components/toast/toast-provider.mjs.map +1 -0
  83. package/dist/context/copilot-context.d.ts +111 -0
  84. package/dist/context/copilot-context.js +129 -0
  85. package/dist/context/copilot-context.js.map +1 -0
  86. package/dist/context/copilot-context.mjs +10 -0
  87. package/dist/context/copilot-context.mjs.map +1 -0
  88. package/dist/context/copilot-messages-context.d.ts +15 -0
  89. package/dist/context/copilot-messages-context.js +57 -0
  90. package/dist/context/copilot-messages-context.js.map +1 -0
  91. package/dist/context/copilot-messages-context.mjs +10 -0
  92. package/dist/context/copilot-messages-context.mjs.map +1 -0
  93. package/dist/context/index.d.ts +11 -0
  94. package/dist/context/index.js +152 -0
  95. package/dist/context/index.js.map +1 -0
  96. package/dist/context/index.mjs +17 -0
  97. package/dist/context/index.mjs.map +1 -0
  98. package/dist/hooks/index.d.ts +19 -0
  99. package/dist/hooks/index.js +906 -0
  100. package/dist/hooks/index.js.map +1 -0
  101. package/dist/hooks/index.mjs +44 -0
  102. package/dist/hooks/index.mjs.map +1 -0
  103. package/dist/hooks/use-chat.d.ts +96 -0
  104. package/dist/hooks/use-chat.js +359 -0
  105. package/dist/hooks/use-chat.js.map +1 -0
  106. package/dist/hooks/use-chat.mjs +11 -0
  107. package/dist/hooks/use-chat.mjs.map +1 -0
  108. package/dist/hooks/use-coagent-state-render.d.ts +60 -0
  109. package/dist/hooks/use-coagent-state-render.js +163 -0
  110. package/dist/hooks/use-coagent-state-render.js.map +1 -0
  111. package/dist/hooks/use-coagent-state-render.mjs +9 -0
  112. package/dist/hooks/use-coagent-state-render.mjs.map +1 -0
  113. package/dist/hooks/use-coagent.d.ts +186 -0
  114. package/dist/hooks/use-coagent.js +735 -0
  115. package/dist/hooks/use-coagent.js.map +1 -0
  116. package/dist/hooks/use-coagent.mjs +21 -0
  117. package/dist/hooks/use-coagent.mjs.map +1 -0
  118. package/dist/hooks/use-copilot-action.d.ts +130 -0
  119. package/dist/hooks/use-copilot-action.js +222 -0
  120. package/dist/hooks/use-copilot-action.js.map +1 -0
  121. package/dist/hooks/use-copilot-action.mjs +9 -0
  122. package/dist/hooks/use-copilot-action.mjs.map +1 -0
  123. package/dist/hooks/use-copilot-chat.d.ts +37 -0
  124. package/dist/hooks/use-copilot-chat.js +630 -0
  125. package/dist/hooks/use-copilot-chat.js.map +1 -0
  126. package/dist/hooks/use-copilot-chat.mjs +22 -0
  127. package/dist/hooks/use-copilot-chat.mjs.map +1 -0
  128. package/dist/hooks/use-copilot-readable.d.ts +33 -0
  129. package/dist/hooks/use-copilot-readable.js +149 -0
  130. package/dist/hooks/use-copilot-readable.js.map +1 -0
  131. package/dist/hooks/use-copilot-readable.mjs +9 -0
  132. package/dist/hooks/use-copilot-readable.mjs.map +1 -0
  133. package/dist/hooks/use-copilot-runtime-client.d.ts +5 -0
  134. package/dist/hooks/use-copilot-runtime-client.js +76 -0
  135. package/dist/hooks/use-copilot-runtime-client.js.map +1 -0
  136. package/dist/hooks/use-copilot-runtime-client.mjs +10 -0
  137. package/dist/hooks/use-copilot-runtime-client.mjs.map +1 -0
  138. package/dist/hooks/use-flat-category-store.d.ts +9 -0
  139. package/dist/hooks/use-flat-category-store.js +90 -0
  140. package/dist/hooks/use-flat-category-store.js.map +1 -0
  141. package/dist/hooks/use-flat-category-store.mjs +8 -0
  142. package/dist/hooks/use-flat-category-store.mjs.map +1 -0
  143. package/dist/hooks/use-make-copilot-document-readable.d.ts +12 -0
  144. package/dist/hooks/use-make-copilot-document-readable.js +144 -0
  145. package/dist/hooks/use-make-copilot-document-readable.js.map +1 -0
  146. package/dist/hooks/use-make-copilot-document-readable.mjs +9 -0
  147. package/dist/hooks/use-make-copilot-document-readable.mjs.map +1 -0
  148. package/dist/hooks/use-tree.d.ts +18 -0
  149. package/dist/hooks/use-tree.js +172 -0
  150. package/dist/hooks/use-tree.js.map +1 -0
  151. package/dist/hooks/use-tree.mjs +8 -0
  152. package/dist/hooks/use-tree.mjs.map +1 -0
  153. package/dist/index.d.ts +26 -0
  154. package/dist/index.js +1832 -0
  155. package/dist/index.js.map +1 -0
  156. package/dist/index.mjs +71 -0
  157. package/dist/index.mjs.map +1 -0
  158. package/dist/lib/copilot-task.d.ts +92 -0
  159. package/dist/lib/copilot-task.js +157 -0
  160. package/dist/lib/copilot-task.js.map +1 -0
  161. package/dist/lib/copilot-task.mjs +18 -0
  162. package/dist/lib/copilot-task.mjs.map +1 -0
  163. package/dist/lib/index.d.ts +11 -0
  164. package/dist/lib/index.js +159 -0
  165. package/dist/lib/index.js.map +1 -0
  166. package/dist/lib/index.mjs +19 -0
  167. package/dist/lib/index.mjs.map +1 -0
  168. package/dist/types/chat-suggestion-configuration.d.ts +22 -0
  169. package/dist/types/chat-suggestion-configuration.js +19 -0
  170. package/dist/types/chat-suggestion-configuration.js.map +1 -0
  171. package/dist/types/chat-suggestion-configuration.mjs +1 -0
  172. package/dist/types/chat-suggestion-configuration.mjs.map +1 -0
  173. package/dist/types/coagent-action.d.ts +29 -0
  174. package/dist/types/coagent-action.js +19 -0
  175. package/dist/types/coagent-action.js.map +1 -0
  176. package/dist/types/coagent-action.mjs +1 -0
  177. package/dist/types/coagent-action.mjs.map +1 -0
  178. package/dist/types/coagent-state.d.ts +11 -0
  179. package/dist/types/coagent-state.js +19 -0
  180. package/dist/types/coagent-state.js.map +1 -0
  181. package/dist/types/coagent-state.mjs +1 -0
  182. package/dist/types/coagent-state.mjs.map +1 -0
  183. package/dist/types/document-pointer.d.ts +9 -0
  184. package/dist/types/document-pointer.js +19 -0
  185. package/dist/types/document-pointer.js.map +1 -0
  186. package/dist/types/document-pointer.mjs +1 -0
  187. package/dist/types/document-pointer.mjs.map +1 -0
  188. package/dist/types/frontend-action.d.ts +107 -0
  189. package/dist/types/frontend-action.js +19 -0
  190. package/dist/types/frontend-action.js.map +1 -0
  191. package/dist/types/frontend-action.mjs +1 -0
  192. package/dist/types/frontend-action.mjs.map +1 -0
  193. package/dist/types/index.d.ts +6 -0
  194. package/dist/types/index.js +19 -0
  195. package/dist/types/index.js.map +1 -0
  196. package/dist/types/index.mjs +2 -0
  197. package/dist/types/index.mjs.map +1 -0
  198. package/dist/types/system-message.d.ts +3 -0
  199. package/dist/types/system-message.js +19 -0
  200. package/dist/types/system-message.js.map +1 -0
  201. package/dist/types/system-message.mjs +1 -0
  202. package/dist/types/system-message.mjs.map +1 -0
  203. package/dist/utils/dev-console.d.ts +3 -0
  204. package/dist/utils/dev-console.js +42 -0
  205. package/dist/utils/dev-console.js.map +1 -0
  206. package/dist/utils/dev-console.mjs +8 -0
  207. package/dist/utils/dev-console.mjs.map +1 -0
  208. package/dist/utils/extract.d.ts +42 -0
  209. package/dist/utils/extract.js +190 -0
  210. package/dist/utils/extract.js.map +1 -0
  211. package/dist/utils/extract.mjs +17 -0
  212. package/dist/utils/extract.mjs.map +1 -0
  213. package/dist/utils/index.d.ts +13 -0
  214. package/dist/utils/index.js +208 -0
  215. package/dist/utils/index.js.map +1 -0
  216. package/dist/utils/index.mjs +20 -0
  217. package/dist/utils/index.mjs.map +1 -0
  218. package/dist/utils/utils.d.ts +2 -0
  219. package/dist/utils/utils.js +19 -0
  220. package/dist/utils/utils.js.map +1 -0
  221. package/dist/utils/utils.mjs +1 -0
  222. package/dist/utils/utils.mjs.map +1 -0
  223. package/dist/utils/utils.test.d.ts +2 -0
  224. package/dist/utils/utils.test.js +9 -0
  225. package/dist/utils/utils.test.js.map +1 -0
  226. package/dist/utils/utils.test.mjs +7 -0
  227. package/dist/utils/utils.test.mjs.map +1 -0
  228. package/package.json +5 -5
@@ -0,0 +1,906 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __defProps = Object.defineProperties;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
+ var __getProtoOf = Object.getPrototypeOf;
10
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
11
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
+ var __spreadValues = (a, b) => {
14
+ for (var prop in b || (b = {}))
15
+ if (__hasOwnProp.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ if (__getOwnPropSymbols)
18
+ for (var prop of __getOwnPropSymbols(b)) {
19
+ if (__propIsEnum.call(b, prop))
20
+ __defNormalProp(a, prop, b[prop]);
21
+ }
22
+ return a;
23
+ };
24
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
+ var __objRest = (source, exclude) => {
26
+ var target = {};
27
+ for (var prop in source)
28
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
29
+ target[prop] = source[prop];
30
+ if (source != null && __getOwnPropSymbols)
31
+ for (var prop of __getOwnPropSymbols(source)) {
32
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
33
+ target[prop] = source[prop];
34
+ }
35
+ return target;
36
+ };
37
+ var __export = (target, all) => {
38
+ for (var name in all)
39
+ __defProp(target, name, { get: all[name], enumerable: true });
40
+ };
41
+ var __copyProps = (to, from, except, desc) => {
42
+ if (from && typeof from === "object" || typeof from === "function") {
43
+ for (let key of __getOwnPropNames(from))
44
+ if (!__hasOwnProp.call(to, key) && key !== except)
45
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
46
+ }
47
+ return to;
48
+ };
49
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
50
+ // If the importer is in node compatibility mode or this is not an ESM
51
+ // file that has been converted to a CommonJS file using a Babel-
52
+ // compatible transform (i.e. "__esModule" has not been set), then set
53
+ // "default" to the CommonJS "module.exports" for node compatibility.
54
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
55
+ mod
56
+ ));
57
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
58
+ var __async = (__this, __arguments, generator) => {
59
+ return new Promise((resolve, reject) => {
60
+ var fulfilled = (value) => {
61
+ try {
62
+ step(generator.next(value));
63
+ } catch (e) {
64
+ reject(e);
65
+ }
66
+ };
67
+ var rejected = (value) => {
68
+ try {
69
+ step(generator.throw(value));
70
+ } catch (e) {
71
+ reject(e);
72
+ }
73
+ };
74
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
75
+ step((generator = generator.apply(__this, __arguments)).next());
76
+ });
77
+ };
78
+
79
+ // src/hooks/index.ts
80
+ var hooks_exports = {};
81
+ __export(hooks_exports, {
82
+ useCoAgent: () => useCoAgent,
83
+ useCoAgentStateRender: () => useCoAgentStateRender,
84
+ useCopilotAction: () => useCopilotAction,
85
+ useCopilotChat: () => useCopilotChat,
86
+ useCopilotReadable: () => useCopilotReadable,
87
+ useCopilotRuntimeClient: () => useCopilotRuntimeClient,
88
+ useMakeCopilotDocumentReadable: () => useMakeCopilotDocumentReadable
89
+ });
90
+ module.exports = __toCommonJS(hooks_exports);
91
+
92
+ // src/hooks/use-copilot-chat.ts
93
+ var import_react7 = require("react");
94
+
95
+ // src/context/copilot-context.tsx
96
+ var import_react = __toESM(require("react"));
97
+ var emptyCopilotContext = {
98
+ actions: {},
99
+ setAction: () => {
100
+ },
101
+ removeAction: () => {
102
+ },
103
+ coAgentStateRenders: {},
104
+ setCoAgentStateRender: () => {
105
+ },
106
+ removeCoAgentStateRender: () => {
107
+ },
108
+ chatComponentsCache: { current: { actions: {}, coAgentStateRenders: {} } },
109
+ getContextString: (documents, categories) => returnAndThrowInDebug(""),
110
+ addContext: () => "",
111
+ removeContext: () => {
112
+ },
113
+ getFunctionCallHandler: () => returnAndThrowInDebug(() => __async(void 0, null, function* () {
114
+ })),
115
+ isLoading: false,
116
+ setIsLoading: () => returnAndThrowInDebug(false),
117
+ chatInstructions: "",
118
+ setChatInstructions: () => returnAndThrowInDebug(""),
119
+ getDocumentsContext: (categories) => returnAndThrowInDebug([]),
120
+ addDocumentContext: () => returnAndThrowInDebug(""),
121
+ removeDocumentContext: () => {
122
+ },
123
+ runtimeClient: {},
124
+ copilotApiConfig: new class {
125
+ get chatApiEndpoint() {
126
+ throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
127
+ }
128
+ get headers() {
129
+ return {};
130
+ }
131
+ get body() {
132
+ return {};
133
+ }
134
+ }(),
135
+ chatSuggestionConfiguration: {},
136
+ addChatSuggestionConfiguration: () => {
137
+ },
138
+ removeChatSuggestionConfiguration: () => {
139
+ },
140
+ showDevConsole: "auto",
141
+ coagentStates: {},
142
+ setCoagentStates: () => {
143
+ },
144
+ coagentStatesRef: { current: {} },
145
+ setCoagentStatesWithRef: () => {
146
+ },
147
+ agentSession: null,
148
+ setAgentSession: () => {
149
+ }
150
+ };
151
+ var CopilotContext = import_react.default.createContext(emptyCopilotContext);
152
+ function useCopilotContext() {
153
+ const context = import_react.default.useContext(CopilotContext);
154
+ if (context === emptyCopilotContext) {
155
+ throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
156
+ }
157
+ return context;
158
+ }
159
+ function returnAndThrowInDebug(value) {
160
+ throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
161
+ return value;
162
+ }
163
+
164
+ // src/hooks/use-copilot-chat.ts
165
+ var import_runtime_client_gql3 = require("@copilotkit/runtime-client-gql");
166
+
167
+ // src/hooks/use-chat.ts
168
+ var import_react4 = require("react");
169
+ var import_shared = require("@copilotkit/shared");
170
+ var import_runtime_client_gql2 = require("@copilotkit/runtime-client-gql");
171
+
172
+ // src/components/toast/toast-provider.tsx
173
+ var import_react2 = require("react");
174
+ var import_jsx_runtime = require("react/jsx-runtime");
175
+ var ToastContext = (0, import_react2.createContext)(void 0);
176
+ function useToast() {
177
+ const context = (0, import_react2.useContext)(ToastContext);
178
+ if (!context) {
179
+ throw new Error("useToast must be used within a ToastProvider");
180
+ }
181
+ return context;
182
+ }
183
+
184
+ // src/hooks/use-copilot-runtime-client.ts
185
+ var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
186
+ var import_react3 = require("react");
187
+ var useCopilotRuntimeClient = (options) => {
188
+ const { addGraphQLErrorsToast } = useToast();
189
+ const runtimeClient = (0, import_react3.useMemo)(() => {
190
+ return new import_runtime_client_gql.CopilotRuntimeClient(__spreadProps(__spreadValues({}, options), {
191
+ handleGQLErrors: (error) => {
192
+ if (error.graphQLErrors.length) {
193
+ addGraphQLErrorsToast(error.graphQLErrors);
194
+ }
195
+ }
196
+ }));
197
+ }, [options, addGraphQLErrorsToast]);
198
+ return runtimeClient;
199
+ };
200
+
201
+ // src/hooks/use-chat.ts
202
+ function useChat(options) {
203
+ const {
204
+ messages,
205
+ setMessages,
206
+ makeSystemMessageCallback,
207
+ copilotConfig,
208
+ setIsLoading,
209
+ initialMessages,
210
+ isLoading,
211
+ actions,
212
+ onFunctionCall,
213
+ onCoAgentStateRender,
214
+ setCoagentStatesWithRef,
215
+ coagentStatesRef,
216
+ agentSession,
217
+ setAgentSession
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);
222
+ const { addGraphQLErrorsToast } = useToast();
223
+ const runChatCompletionRef = (0, import_react4.useRef)();
224
+ const agentSessionRef = (0, import_react4.useRef)(agentSession);
225
+ agentSessionRef.current = agentSession;
226
+ const publicApiKey = copilotConfig.publicApiKey;
227
+ const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [import_shared.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
228
+ const runtimeClient = useCopilotRuntimeClient({
229
+ url: copilotConfig.chatApiEndpoint,
230
+ publicApiKey: copilotConfig.publicApiKey,
231
+ headers,
232
+ credentials: copilotConfig.credentials
233
+ });
234
+ const runChatCompletion = (previousMessages) => __async(this, null, function* () {
235
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
236
+ setIsLoading(true);
237
+ let newMessages = [
238
+ new import_runtime_client_gql2.TextMessage({
239
+ content: "",
240
+ role: import_runtime_client_gql2.Role.Assistant
241
+ })
242
+ ];
243
+ const abortController = new AbortController();
244
+ abortControllerRef.current = abortController;
245
+ setMessages([...previousMessages, ...newMessages]);
246
+ const systemMessage = makeSystemMessageCallback();
247
+ const messagesWithContext = [systemMessage, ...initialMessages || [], ...previousMessages];
248
+ const stream = runtimeClient.asStream(
249
+ runtimeClient.generateCopilotResponse({
250
+ data: __spreadProps(__spreadValues(__spreadProps(__spreadValues({
251
+ frontend: {
252
+ actions: actions.filter(
253
+ (action) => action.available !== import_runtime_client_gql2.ActionInputAvailability.Disabled || !action.disabled
254
+ ).map((action) => {
255
+ let available = import_runtime_client_gql2.ActionInputAvailability.Enabled;
256
+ if (action.disabled) {
257
+ available = import_runtime_client_gql2.ActionInputAvailability.Disabled;
258
+ } else if (action.available === "disabled") {
259
+ available = import_runtime_client_gql2.ActionInputAvailability.Disabled;
260
+ } else if (action.available === "remote") {
261
+ available = import_runtime_client_gql2.ActionInputAvailability.Remote;
262
+ }
263
+ return {
264
+ name: action.name,
265
+ description: action.description || "",
266
+ jsonSchema: JSON.stringify((0, import_shared.actionParametersToJsonSchema)(action.parameters || [])),
267
+ available
268
+ };
269
+ }),
270
+ url: window.location.href
271
+ },
272
+ threadId: threadIdRef.current,
273
+ runId: runIdRef.current,
274
+ messages: (0, import_runtime_client_gql2.convertMessagesToGqlInput)((0, import_runtime_client_gql2.filterAgentStateMessages)(messagesWithContext))
275
+ }, copilotConfig.cloud ? {
276
+ cloud: __spreadValues({}, ((_c = (_b = (_a = copilotConfig.cloud.guardrails) == null ? void 0 : _a.input) == null ? void 0 : _b.restrictToTopic) == null ? void 0 : _c.enabled) ? {
277
+ guardrails: {
278
+ inputValidationRules: {
279
+ allowList: copilotConfig.cloud.guardrails.input.restrictToTopic.validTopics,
280
+ denyList: copilotConfig.cloud.guardrails.input.restrictToTopic.invalidTopics
281
+ }
282
+ }
283
+ } : {})
284
+ } : {}), {
285
+ metadata: {
286
+ requestType: import_runtime_client_gql2.CopilotRequestType.Chat
287
+ }
288
+ }), agentSessionRef.current ? {
289
+ agentSession: agentSessionRef.current
290
+ } : {}), {
291
+ agentStates: Object.values(coagentStatesRef.current).map((state) => ({
292
+ agentName: state.name,
293
+ state: JSON.stringify(state.state)
294
+ }))
295
+ }),
296
+ properties: copilotConfig.properties,
297
+ signal: (_d = abortControllerRef.current) == null ? void 0 : _d.signal
298
+ })
299
+ );
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;
301
+ const reader = stream.getReader();
302
+ let actionResults = {};
303
+ let executedCoAgentStateRenders = [];
304
+ let followUp = void 0;
305
+ try {
306
+ while (true) {
307
+ let done, value;
308
+ try {
309
+ const readResult = yield reader.read();
310
+ done = readResult.done;
311
+ value = readResult.value;
312
+ } catch (readError) {
313
+ break;
314
+ }
315
+ if (done) {
316
+ break;
317
+ }
318
+ if (!(value == null ? void 0 : value.generateCopilotResponse)) {
319
+ continue;
320
+ }
321
+ threadIdRef.current = value.generateCopilotResponse.threadId || null;
322
+ runIdRef.current = value.generateCopilotResponse.runId || null;
323
+ const messages2 = (0, import_runtime_client_gql2.convertGqlOutputToMessages)(
324
+ (0, import_runtime_client_gql2.filterAdjacentAgentStateMessages)(value.generateCopilotResponse.messages)
325
+ );
326
+ if (messages2.length === 0) {
327
+ continue;
328
+ }
329
+ newMessages = [];
330
+ if (((_h = value.generateCopilotResponse.status) == null ? void 0 : _h.__typename) === "FailedResponseStatus" && value.generateCopilotResponse.status.reason === "GUARDRAILS_VALIDATION_FAILED") {
331
+ newMessages = [
332
+ new import_runtime_client_gql2.TextMessage({
333
+ role: import_runtime_client_gql2.MessageRole.Assistant,
334
+ content: ((_i = value.generateCopilotResponse.status.details) == null ? void 0 : _i.guardrailsReason) || ""
335
+ })
336
+ ];
337
+ } else {
338
+ for (const message of messages2) {
339
+ newMessages.push(message);
340
+ if (message.isActionExecutionMessage() && message.status.code !== import_runtime_client_gql2.MessageStatusCode.Pending && message.scope === "client" && onFunctionCall) {
341
+ if (!(message.id in actionResults)) {
342
+ if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
343
+ break;
344
+ }
345
+ try {
346
+ setMessages([...previousMessages, ...newMessages]);
347
+ const action = actions.find((action2) => action2.name === message.name);
348
+ if (action) {
349
+ followUp = action.followUp;
350
+ }
351
+ const result = yield onFunctionCall({
352
+ messages: previousMessages,
353
+ name: message.name,
354
+ args: message.arguments
355
+ });
356
+ actionResults[message.id] = result;
357
+ } catch (e) {
358
+ actionResults[message.id] = `Failed to execute action ${message.name}`;
359
+ console.error(`Failed to execute action ${message.name}: ${e}`);
360
+ }
361
+ }
362
+ newMessages.push(
363
+ new import_runtime_client_gql2.ResultMessage({
364
+ result: import_runtime_client_gql2.ResultMessage.encodeResult(actionResults[message.id]),
365
+ actionExecutionId: message.id,
366
+ actionName: message.name
367
+ })
368
+ );
369
+ }
370
+ if (message.isAgentStateMessage() && !message.active && !executedCoAgentStateRenders.includes(message.id) && onCoAgentStateRender) {
371
+ if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
372
+ break;
373
+ }
374
+ yield onCoAgentStateRender({
375
+ name: message.agentName,
376
+ nodeName: message.nodeName,
377
+ state: message.state
378
+ });
379
+ executedCoAgentStateRenders.push(message.id);
380
+ }
381
+ }
382
+ const lastAgentStateMessage = [...messages2].reverse().find((message) => message.isAgentStateMessage());
383
+ if (lastAgentStateMessage) {
384
+ setCoagentStatesWithRef((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
385
+ [lastAgentStateMessage.agentName]: {
386
+ name: lastAgentStateMessage.agentName,
387
+ state: lastAgentStateMessage.state,
388
+ running: lastAgentStateMessage.running,
389
+ active: lastAgentStateMessage.active,
390
+ threadId: lastAgentStateMessage.threadId,
391
+ nodeName: lastAgentStateMessage.nodeName,
392
+ runId: lastAgentStateMessage.runId
393
+ }
394
+ }));
395
+ if (lastAgentStateMessage.running) {
396
+ setAgentSession({
397
+ threadId: lastAgentStateMessage.threadId,
398
+ agentName: lastAgentStateMessage.agentName,
399
+ nodeName: lastAgentStateMessage.nodeName
400
+ });
401
+ } else {
402
+ setAgentSession(null);
403
+ }
404
+ }
405
+ }
406
+ if (newMessages.length > 0) {
407
+ setMessages([...previousMessages, ...newMessages]);
408
+ }
409
+ }
410
+ if (
411
+ // if followUp is not explicitly false
412
+ followUp !== false && // if we have client side results
413
+ (Object.values(actionResults).length || // or the last message we received is a result
414
+ newMessages.length && newMessages[newMessages.length - 1].isResultMessage())
415
+ ) {
416
+ yield new Promise((resolve) => setTimeout(resolve, 10));
417
+ return yield runChatCompletionRef.current([...previousMessages, ...newMessages]);
418
+ } else {
419
+ return newMessages.slice();
420
+ }
421
+ } finally {
422
+ setIsLoading(false);
423
+ }
424
+ });
425
+ runChatCompletionRef.current = runChatCompletion;
426
+ const runChatCompletionAndHandleFunctionCall = (messages2) => __async(this, null, function* () {
427
+ yield runChatCompletionRef.current(messages2);
428
+ });
429
+ const append = (message) => __async(this, null, function* () {
430
+ if (isLoading) {
431
+ return;
432
+ }
433
+ const newMessages = [...messages, message];
434
+ setMessages(newMessages);
435
+ return runChatCompletionAndHandleFunctionCall(newMessages);
436
+ });
437
+ const reload = () => __async(this, null, function* () {
438
+ if (isLoading || messages.length === 0) {
439
+ return;
440
+ }
441
+ let newMessages = [...messages];
442
+ const lastMessage = messages[messages.length - 1];
443
+ if (lastMessage.isTextMessage() && lastMessage.role === "assistant") {
444
+ newMessages = newMessages.slice(0, -1);
445
+ }
446
+ setMessages(newMessages);
447
+ return runChatCompletionAndHandleFunctionCall(newMessages);
448
+ });
449
+ const stop = () => {
450
+ var _a;
451
+ (_a = abortControllerRef.current) == null ? void 0 : _a.abort();
452
+ };
453
+ return {
454
+ append,
455
+ reload,
456
+ stop,
457
+ runChatCompletion: () => runChatCompletionRef.current(messages)
458
+ };
459
+ }
460
+
461
+ // src/components/copilot-provider/copilotkit.tsx
462
+ var import_react6 = require("react");
463
+ var import_react_dom = require("react-dom");
464
+ var import_shared2 = require("@copilotkit/shared");
465
+
466
+ // src/context/copilot-messages-context.tsx
467
+ var import_react5 = __toESM(require("react"));
468
+ var emptyCopilotContext2 = {
469
+ messages: [],
470
+ setMessages: () => []
471
+ };
472
+ var CopilotMessagesContext = import_react5.default.createContext(emptyCopilotContext2);
473
+ function useCopilotMessagesContext() {
474
+ const context = import_react5.default.useContext(CopilotMessagesContext);
475
+ if (context === emptyCopilotContext2) {
476
+ throw new Error(
477
+ "A messages consuming component was not wrapped with `<CopilotMessages> {...} </CopilotMessages>`"
478
+ );
479
+ }
480
+ return context;
481
+ }
482
+
483
+ // src/components/copilot-provider/copilotkit.tsx
484
+ var import_jsx_runtime2 = require("react/jsx-runtime");
485
+ var defaultCopilotContextCategories = ["global"];
486
+
487
+ // src/hooks/use-copilot-chat.ts
488
+ function useCopilotChat(_a = {}) {
489
+ var _b = _a, {
490
+ makeSystemMessage
491
+ } = _b, options = __objRest(_b, [
492
+ "makeSystemMessage"
493
+ ]);
494
+ const {
495
+ getContextString,
496
+ getFunctionCallHandler,
497
+ copilotApiConfig,
498
+ isLoading,
499
+ setIsLoading,
500
+ chatInstructions,
501
+ actions,
502
+ coagentStatesRef,
503
+ setCoagentStatesWithRef,
504
+ coAgentStateRenders,
505
+ agentSession,
506
+ setAgentSession
507
+ } = useCopilotContext();
508
+ const { messages, setMessages } = useCopilotMessagesContext();
509
+ const latestGetContextString = useUpdatedRef(getContextString);
510
+ const deleteMessage = (0, import_react7.useCallback)(
511
+ (messageId) => {
512
+ setMessages((prev) => prev.filter((message) => message.id !== messageId));
513
+ },
514
+ [setMessages]
515
+ );
516
+ const makeSystemMessageCallback = (0, import_react7.useCallback)(() => {
517
+ const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
518
+ const contextString = latestGetContextString.current([], defaultCopilotContextCategories);
519
+ return new import_runtime_client_gql3.TextMessage({
520
+ content: systemMessageMaker(contextString, chatInstructions),
521
+ role: import_runtime_client_gql3.Role.System
522
+ });
523
+ }, [getContextString, makeSystemMessage, chatInstructions]);
524
+ const onCoAgentStateRender = (0, import_react7.useCallback)(
525
+ (args) => __async(this, null, function* () {
526
+ var _a2;
527
+ const { name, nodeName, state } = args;
528
+ let action = Object.values(coAgentStateRenders).find(
529
+ (action2) => action2.name === name && action2.nodeName === nodeName
530
+ );
531
+ if (!action) {
532
+ action = Object.values(coAgentStateRenders).find(
533
+ (action2) => action2.name === name && !action2.nodeName
534
+ );
535
+ }
536
+ if (action) {
537
+ yield (_a2 = action.handler) == null ? void 0 : _a2.call(action, { state, nodeName });
538
+ }
539
+ }),
540
+ [coAgentStateRenders]
541
+ );
542
+ const { append, reload, stop, runChatCompletion } = useChat(__spreadProps(__spreadValues({}, options), {
543
+ actions: Object.values(actions),
544
+ copilotConfig: copilotApiConfig,
545
+ initialMessages: options.initialMessages || [],
546
+ onFunctionCall: getFunctionCallHandler(),
547
+ onCoAgentStateRender,
548
+ messages,
549
+ setMessages,
550
+ makeSystemMessageCallback,
551
+ isLoading,
552
+ setIsLoading,
553
+ coagentStatesRef,
554
+ setCoagentStatesWithRef,
555
+ agentSession,
556
+ setAgentSession
557
+ }));
558
+ const latestAppend = useUpdatedRef(append);
559
+ const latestAppendFunc = (0, import_react7.useCallback)(
560
+ (message) => {
561
+ return latestAppend.current(message);
562
+ },
563
+ [latestAppend]
564
+ );
565
+ const latestReload = useUpdatedRef(reload);
566
+ const latestReloadFunc = (0, import_react7.useCallback)(() => {
567
+ return latestReload.current();
568
+ }, [latestReload]);
569
+ const latestStop = useUpdatedRef(stop);
570
+ const latestStopFunc = (0, import_react7.useCallback)(() => {
571
+ return latestStop.current();
572
+ }, [latestStop]);
573
+ const latestDelete = useUpdatedRef(deleteMessage);
574
+ const latestDeleteFunc = (0, import_react7.useCallback)(
575
+ (messageId) => {
576
+ return latestDelete.current(messageId);
577
+ },
578
+ [latestDelete]
579
+ );
580
+ const latestSetMessages = useUpdatedRef(setMessages);
581
+ const latestSetMessagesFunc = (0, import_react7.useCallback)(
582
+ (messages2) => {
583
+ return latestSetMessages.current(messages2);
584
+ },
585
+ [latestSetMessages]
586
+ );
587
+ const latestRunChatCompletion = useUpdatedRef(runChatCompletion);
588
+ const latestRunChatCompletionFunc = (0, import_react7.useCallback)(() => {
589
+ return latestRunChatCompletion.current();
590
+ }, [latestRunChatCompletion]);
591
+ return {
592
+ visibleMessages: messages,
593
+ appendMessage: latestAppendFunc,
594
+ setMessages: latestSetMessagesFunc,
595
+ reloadMessages: latestReloadFunc,
596
+ stopGeneration: latestStopFunc,
597
+ deleteMessage: latestDeleteFunc,
598
+ runChatCompletion: latestRunChatCompletionFunc,
599
+ isLoading
600
+ };
601
+ }
602
+ function useUpdatedRef(value) {
603
+ const ref = (0, import_react7.useRef)(value);
604
+ (0, import_react7.useEffect)(() => {
605
+ ref.current = value;
606
+ }, [value]);
607
+ return ref;
608
+ }
609
+ function defaultSystemMessage(contextString, additionalInstructions) {
610
+ return `
611
+ Please act as an efficient, competent, conscientious, and industrious professional assistant.
612
+
613
+ Help the user achieve their goals, and you do so in a way that is as efficient as possible, without unnecessary fluff, but also without sacrificing professionalism.
614
+ Always be polite and respectful, and prefer brevity over verbosity.
615
+
616
+ The user has provided you with the following context:
617
+ \`\`\`
618
+ ${contextString}
619
+ \`\`\`
620
+
621
+ They have also provided you with functions you can call to initiate actions on their behalf, or functions you can call to receive more information.
622
+
623
+ Please assist them as best you can.
624
+
625
+ You can ask them for clarifying questions if needed, but don't be annoying about it. If you can reasonably 'fill in the blanks' yourself, do so.
626
+
627
+ If you would like to call a function, call it without saying anything else.
628
+ ` + (additionalInstructions ? `
629
+
630
+ ${additionalInstructions}` : "");
631
+ }
632
+
633
+ // src/hooks/use-copilot-action.ts
634
+ var import_shared3 = require("@copilotkit/shared");
635
+ var import_react8 = require("react");
636
+ function useCopilotAction(action, dependencies) {
637
+ const { setAction, removeAction, actions, chatComponentsCache } = useCopilotContext();
638
+ const idRef = (0, import_react8.useRef)((0, import_shared3.randomId)());
639
+ const renderAndWaitRef = (0, import_react8.useRef)(null);
640
+ action = __spreadValues({}, action);
641
+ if (action.renderAndWait || action.renderAndWaitForResponse) {
642
+ const renderAndWait = action.renderAndWait || action.renderAndWaitForResponse;
643
+ action.renderAndWait = void 0;
644
+ action.renderAndWaitForResponse = void 0;
645
+ action.handler = () => __async(this, null, function* () {
646
+ let resolve;
647
+ let reject;
648
+ const promise = new Promise((resolvePromise, rejectPromise) => {
649
+ resolve = resolvePromise;
650
+ reject = rejectPromise;
651
+ });
652
+ renderAndWaitRef.current = { promise, resolve, reject };
653
+ return yield promise;
654
+ });
655
+ action.render = (props) => {
656
+ const waitProps = {
657
+ status: props.status,
658
+ args: props.args,
659
+ result: props.result,
660
+ handler: props.status === "executing" ? renderAndWaitRef.current.resolve : void 0,
661
+ respond: props.status === "executing" ? renderAndWaitRef.current.resolve : void 0
662
+ };
663
+ const isNoArgsRenderWait = (_fn) => {
664
+ var _a;
665
+ return ((_a = action.parameters) == null ? void 0 : _a.length) === 0;
666
+ };
667
+ if (renderAndWait) {
668
+ if (isNoArgsRenderWait(renderAndWait)) {
669
+ return renderAndWait(waitProps);
670
+ } else {
671
+ return renderAndWait(waitProps);
672
+ }
673
+ }
674
+ return (0, import_react8.createElement)(import_react8.Fragment);
675
+ };
676
+ }
677
+ if (dependencies === void 0) {
678
+ if (actions[idRef.current]) {
679
+ actions[idRef.current].handler = action.handler;
680
+ if (typeof action.render === "function") {
681
+ if (chatComponentsCache.current !== null) {
682
+ chatComponentsCache.current.actions[action.name] = action.render;
683
+ }
684
+ }
685
+ }
686
+ }
687
+ (0, import_react8.useEffect)(() => {
688
+ setAction(idRef.current, action);
689
+ if (chatComponentsCache.current !== null && action.render !== void 0) {
690
+ chatComponentsCache.current.actions[action.name] = action.render;
691
+ }
692
+ return () => {
693
+ removeAction(idRef.current);
694
+ };
695
+ }, [
696
+ setAction,
697
+ removeAction,
698
+ action.description,
699
+ action.name,
700
+ action.disabled,
701
+ action.available,
702
+ // This should be faster than deep equality checking
703
+ // In addition, all major JS engines guarantee the order of object keys
704
+ JSON.stringify(action.parameters),
705
+ // include render only if it's a string
706
+ typeof action.render === "string" ? action.render : void 0,
707
+ // dependencies set by the developer
708
+ ...dependencies || []
709
+ ]);
710
+ }
711
+
712
+ // src/hooks/use-coagent-state-render.ts
713
+ var import_react9 = require("react");
714
+ var import_shared4 = require("@copilotkit/shared");
715
+ function useCoAgentStateRender(action, dependencies) {
716
+ const {
717
+ setCoAgentStateRender,
718
+ removeCoAgentStateRender,
719
+ coAgentStateRenders,
720
+ chatComponentsCache
721
+ } = (0, import_react9.useContext)(CopilotContext);
722
+ const idRef = (0, import_react9.useRef)((0, import_shared4.randomId)());
723
+ const key = `${action.name}-${action.nodeName || "global"}`;
724
+ if (dependencies === void 0) {
725
+ if (coAgentStateRenders[idRef.current]) {
726
+ coAgentStateRenders[idRef.current].handler = action.handler;
727
+ if (typeof action.render === "function") {
728
+ if (chatComponentsCache.current !== null) {
729
+ chatComponentsCache.current.coAgentStateRenders[key] = action.render;
730
+ }
731
+ }
732
+ }
733
+ }
734
+ (0, import_react9.useEffect)(() => {
735
+ setCoAgentStateRender(idRef.current, action);
736
+ if (chatComponentsCache.current !== null && action.render !== void 0) {
737
+ chatComponentsCache.current.coAgentStateRenders[key] = action.render;
738
+ }
739
+ return () => {
740
+ removeCoAgentStateRender(idRef.current);
741
+ };
742
+ }, [
743
+ setCoAgentStateRender,
744
+ removeCoAgentStateRender,
745
+ action.name,
746
+ // include render only if it's a string
747
+ typeof action.render === "string" ? action.render : void 0,
748
+ // dependencies set by the developer
749
+ ...dependencies || []
750
+ ]);
751
+ }
752
+
753
+ // src/hooks/use-make-copilot-document-readable.ts
754
+ var import_react10 = require("react");
755
+ function useMakeCopilotDocumentReadable(document, categories, dependencies = []) {
756
+ const { addDocumentContext, removeDocumentContext } = useCopilotContext();
757
+ const idRef = (0, import_react10.useRef)();
758
+ (0, import_react10.useEffect)(() => {
759
+ const id = addDocumentContext(document, categories);
760
+ idRef.current = id;
761
+ return () => {
762
+ removeDocumentContext(id);
763
+ };
764
+ }, [addDocumentContext, removeDocumentContext, ...dependencies]);
765
+ return idRef.current;
766
+ }
767
+
768
+ // src/hooks/use-copilot-readable.ts
769
+ var import_react11 = require("react");
770
+ function convertToJSON(description, value) {
771
+ return `${description}: ${typeof value === "string" ? value : JSON.stringify(value)}`;
772
+ }
773
+ function useCopilotReadable({ description, value, parentId, categories, convert }, dependencies) {
774
+ const { addContext, removeContext } = useCopilotContext();
775
+ const idRef = (0, import_react11.useRef)();
776
+ convert = convert || convertToJSON;
777
+ const information = convert(description, value);
778
+ (0, import_react11.useEffect)(() => {
779
+ const id = addContext(information, parentId, categories);
780
+ idRef.current = id;
781
+ return () => {
782
+ removeContext(id);
783
+ };
784
+ }, [information, parentId, addContext, removeContext, ...dependencies || []]);
785
+ return idRef.current;
786
+ }
787
+
788
+ // src/hooks/use-coagent.ts
789
+ var import_react12 = require("react");
790
+ function useCoAgent(options) {
791
+ const isExternalStateManagement = (options2) => {
792
+ return "state" in options2 && "setState" in options2;
793
+ };
794
+ const { name } = options;
795
+ const isInternalStateManagementWithInitial = (options2) => {
796
+ return "initialState" in options2;
797
+ };
798
+ const generalContext = useCopilotContext();
799
+ const messagesContext = useCopilotMessagesContext();
800
+ const context = __spreadValues(__spreadValues({}, generalContext), messagesContext);
801
+ const { coagentStates, coagentStatesRef, setCoagentStatesWithRef } = context;
802
+ const { appendMessage, runChatCompletion } = useCopilotChat();
803
+ const getCoagentState = (coagentStates2, name2) => {
804
+ if (coagentStates2[name2]) {
805
+ return coagentStates2[name2];
806
+ } else {
807
+ return {
808
+ name: name2,
809
+ state: isInternalStateManagementWithInitial(options) ? options.initialState : {},
810
+ running: false,
811
+ active: false,
812
+ threadId: void 0,
813
+ nodeName: void 0,
814
+ runId: void 0
815
+ };
816
+ }
817
+ };
818
+ const setState = (newState) => {
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
+ }));
826
+ };
827
+ const coagentState = getCoagentState(coagentStates, name);
828
+ const state = isExternalStateManagement(options) ? options.state : coagentState.state;
829
+ (0, import_react12.useEffect)(() => {
830
+ if (isExternalStateManagement(options)) {
831
+ setState(options.state);
832
+ } else if (coagentStates[name] === void 0) {
833
+ setState(options.initialState === void 0 ? {} : options.initialState);
834
+ }
835
+ }, [isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0]);
836
+ return {
837
+ name,
838
+ nodeName: coagentState.nodeName,
839
+ state,
840
+ setState,
841
+ running: coagentState.running,
842
+ start: () => {
843
+ startAgent(name, context);
844
+ },
845
+ stop: () => {
846
+ stopAgent(name, context);
847
+ },
848
+ run: (hint) => {
849
+ return runAgent(name, context, appendMessage, runChatCompletion, hint);
850
+ }
851
+ };
852
+ }
853
+ function startAgent(name, context) {
854
+ const { setAgentSession } = context;
855
+ setAgentSession({
856
+ agentName: name
857
+ });
858
+ }
859
+ function stopAgent(name, context) {
860
+ const { agentSession, setAgentSession } = context;
861
+ if (agentSession && agentSession.agentName === name) {
862
+ setAgentSession(null);
863
+ } else {
864
+ console.warn(`No agent session found for ${name}`);
865
+ }
866
+ }
867
+ function runAgent(name, context, appendMessage, runChatCompletion, hint) {
868
+ return __async(this, null, function* () {
869
+ var _a, _b;
870
+ const { agentSession, setAgentSession } = context;
871
+ if (!agentSession || agentSession.agentName !== name) {
872
+ setAgentSession({
873
+ agentName: name
874
+ });
875
+ }
876
+ let previousState = null;
877
+ for (let i = context.messages.length - 1; i >= 0; i--) {
878
+ const message = context.messages[i];
879
+ if (message.isAgentStateMessage() && message.agentName === name) {
880
+ previousState = message.state;
881
+ }
882
+ }
883
+ let state = ((_b = (_a = context.coagentStatesRef.current) == null ? void 0 : _a[name]) == null ? void 0 : _b.state) || {};
884
+ if (hint) {
885
+ const hintMessage = hint({ previousState, currentState: state });
886
+ if (hintMessage) {
887
+ yield appendMessage(hintMessage);
888
+ } else {
889
+ yield runChatCompletion();
890
+ }
891
+ } else {
892
+ yield runChatCompletion();
893
+ }
894
+ });
895
+ }
896
+ // Annotate the CommonJS export names for ESM import in node:
897
+ 0 && (module.exports = {
898
+ useCoAgent,
899
+ useCoAgentStateRender,
900
+ useCopilotAction,
901
+ useCopilotChat,
902
+ useCopilotReadable,
903
+ useCopilotRuntimeClient,
904
+ useMakeCopilotDocumentReadable
905
+ });
906
+ //# sourceMappingURL=index.js.map