@copilotkit/react-core 0.0.0-feat-dynamic-copilotcloud-qa-20250117190454

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 (293) hide show
  1. package/CHANGELOG.md +2001 -0
  2. package/README.md +48 -0
  3. package/dist/chunk-22XKTBPF.mjs +61 -0
  4. package/dist/chunk-22XKTBPF.mjs.map +1 -0
  5. package/dist/chunk-2KT73RVG.mjs +482 -0
  6. package/dist/chunk-2KT73RVG.mjs.map +1 -0
  7. package/dist/chunk-4TDYSNUD.mjs +215 -0
  8. package/dist/chunk-4TDYSNUD.mjs.map +1 -0
  9. package/dist/chunk-5FHSUKQL.mjs +70 -0
  10. package/dist/chunk-5FHSUKQL.mjs.map +1 -0
  11. package/dist/chunk-6BQVZFNO.mjs +35 -0
  12. package/dist/chunk-6BQVZFNO.mjs.map +1 -0
  13. package/dist/chunk-6U3UH3KO.mjs +140 -0
  14. package/dist/chunk-6U3UH3KO.mjs.map +1 -0
  15. package/dist/chunk-DCTJZ742.mjs +22 -0
  16. package/dist/chunk-DCTJZ742.mjs.map +1 -0
  17. package/dist/chunk-EFZPSZWO.mjs +1 -0
  18. package/dist/chunk-EFZPSZWO.mjs.map +1 -0
  19. package/dist/chunk-EWKJMQOF.mjs +107 -0
  20. package/dist/chunk-EWKJMQOF.mjs.map +1 -0
  21. package/dist/chunk-FQ53HQYZ.mjs +28 -0
  22. package/dist/chunk-FQ53HQYZ.mjs.map +1 -0
  23. package/dist/chunk-HIUT2NLX.mjs +23 -0
  24. package/dist/chunk-HIUT2NLX.mjs.map +1 -0
  25. package/dist/chunk-KSPLXKSX.mjs +160 -0
  26. package/dist/chunk-KSPLXKSX.mjs.map +1 -0
  27. package/dist/chunk-LHKIIAUQ.mjs +59 -0
  28. package/dist/chunk-LHKIIAUQ.mjs.map +1 -0
  29. package/dist/chunk-LNI2Z45S.mjs +242 -0
  30. package/dist/chunk-LNI2Z45S.mjs.map +1 -0
  31. package/dist/chunk-LUGEI4YQ.mjs +1 -0
  32. package/dist/chunk-LUGEI4YQ.mjs.map +1 -0
  33. package/dist/chunk-MLAS4QUR.mjs +18 -0
  34. package/dist/chunk-MLAS4QUR.mjs.map +1 -0
  35. package/dist/chunk-NR2BTA6Z.mjs +1 -0
  36. package/dist/chunk-NR2BTA6Z.mjs.map +1 -0
  37. package/dist/chunk-O7ARI5CV.mjs +31 -0
  38. package/dist/chunk-O7ARI5CV.mjs.map +1 -0
  39. package/dist/chunk-SKC7AJIV.mjs +61 -0
  40. package/dist/chunk-SKC7AJIV.mjs.map +1 -0
  41. package/dist/chunk-U6JICUGO.mjs +405 -0
  42. package/dist/chunk-U6JICUGO.mjs.map +1 -0
  43. package/dist/chunk-VNTDIJSG.mjs +37 -0
  44. package/dist/chunk-VNTDIJSG.mjs.map +1 -0
  45. package/dist/chunk-WXREY7UP.mjs +87 -0
  46. package/dist/chunk-WXREY7UP.mjs.map +1 -0
  47. package/dist/chunk-XERJQUHA.mjs +31 -0
  48. package/dist/chunk-XERJQUHA.mjs.map +1 -0
  49. package/dist/chunk-XOQMISG3.mjs +120 -0
  50. package/dist/chunk-XOQMISG3.mjs.map +1 -0
  51. package/dist/chunk-XXR4QFAQ.mjs +29 -0
  52. package/dist/chunk-XXR4QFAQ.mjs.map +1 -0
  53. package/dist/chunk-YPSGKPDA.mjs +1 -0
  54. package/dist/chunk-YPSGKPDA.mjs.map +1 -0
  55. package/dist/components/copilot-provider/copilot-messages.d.ts +16 -0
  56. package/dist/components/copilot-provider/copilot-messages.js +80 -0
  57. package/dist/components/copilot-provider/copilot-messages.js.map +1 -0
  58. package/dist/components/copilot-provider/copilot-messages.mjs +9 -0
  59. package/dist/components/copilot-provider/copilot-messages.mjs.map +1 -0
  60. package/dist/components/copilot-provider/copilotkit-props.d.ts +93 -0
  61. package/dist/components/copilot-provider/copilotkit-props.js +19 -0
  62. package/dist/components/copilot-provider/copilotkit-props.js.map +1 -0
  63. package/dist/components/copilot-provider/copilotkit-props.mjs +1 -0
  64. package/dist/components/copilot-provider/copilotkit-props.mjs.map +1 -0
  65. package/dist/components/copilot-provider/copilotkit.d.ts +18 -0
  66. package/dist/components/copilot-provider/copilotkit.js +990 -0
  67. package/dist/components/copilot-provider/copilotkit.js.map +1 -0
  68. package/dist/components/copilot-provider/copilotkit.mjs +22 -0
  69. package/dist/components/copilot-provider/copilotkit.mjs.map +1 -0
  70. package/dist/components/copilot-provider/index.d.ts +13 -0
  71. package/dist/components/copilot-provider/index.js +990 -0
  72. package/dist/components/copilot-provider/index.js.map +1 -0
  73. package/dist/components/copilot-provider/index.mjs +20 -0
  74. package/dist/components/copilot-provider/index.mjs.map +1 -0
  75. package/dist/components/error-boundary/error-boundary.d.ts +22 -0
  76. package/dist/components/error-boundary/error-boundary.js +183 -0
  77. package/dist/components/error-boundary/error-boundary.js.map +1 -0
  78. package/dist/components/error-boundary/error-boundary.mjs +12 -0
  79. package/dist/components/error-boundary/error-boundary.mjs.map +1 -0
  80. package/dist/components/error-boundary/error-utils.d.ts +11 -0
  81. package/dist/components/error-boundary/error-utils.js +177 -0
  82. package/dist/components/error-boundary/error-utils.js.map +1 -0
  83. package/dist/components/error-boundary/error-utils.mjs +13 -0
  84. package/dist/components/error-boundary/error-utils.mjs.map +1 -0
  85. package/dist/components/index.d.ts +13 -0
  86. package/dist/components/index.js +990 -0
  87. package/dist/components/index.js.map +1 -0
  88. package/dist/components/index.mjs +20 -0
  89. package/dist/components/index.mjs.map +1 -0
  90. package/dist/components/toast/exclamation-mark-icon.d.ts +9 -0
  91. package/dist/components/toast/exclamation-mark-icon.js +55 -0
  92. package/dist/components/toast/exclamation-mark-icon.js.map +1 -0
  93. package/dist/components/toast/exclamation-mark-icon.mjs +8 -0
  94. package/dist/components/toast/exclamation-mark-icon.mjs.map +1 -0
  95. package/dist/components/toast/toast-provider.d.ts +30 -0
  96. package/dist/components/toast/toast-provider.js +269 -0
  97. package/dist/components/toast/toast-provider.js.map +1 -0
  98. package/dist/components/toast/toast-provider.mjs +11 -0
  99. package/dist/components/toast/toast-provider.mjs.map +1 -0
  100. package/dist/context/copilot-context.d.ts +142 -0
  101. package/dist/context/copilot-context.js +138 -0
  102. package/dist/context/copilot-context.js.map +1 -0
  103. package/dist/context/copilot-context.mjs +10 -0
  104. package/dist/context/copilot-context.mjs.map +1 -0
  105. package/dist/context/copilot-messages-context.d.ts +15 -0
  106. package/dist/context/copilot-messages-context.js +57 -0
  107. package/dist/context/copilot-messages-context.js.map +1 -0
  108. package/dist/context/copilot-messages-context.mjs +10 -0
  109. package/dist/context/copilot-messages-context.mjs.map +1 -0
  110. package/dist/context/index.d.ts +11 -0
  111. package/dist/context/index.js +161 -0
  112. package/dist/context/index.js.map +1 -0
  113. package/dist/context/index.mjs +17 -0
  114. package/dist/context/index.mjs.map +1 -0
  115. package/dist/hooks/index.d.ts +21 -0
  116. package/dist/hooks/index.js +1307 -0
  117. package/dist/hooks/index.js.map +1 -0
  118. package/dist/hooks/index.mjs +56 -0
  119. package/dist/hooks/index.mjs.map +1 -0
  120. package/dist/hooks/use-chat.d.ts +130 -0
  121. package/dist/hooks/use-chat.js +612 -0
  122. package/dist/hooks/use-chat.js.map +1 -0
  123. package/dist/hooks/use-chat.mjs +12 -0
  124. package/dist/hooks/use-chat.mjs.map +1 -0
  125. package/dist/hooks/use-coagent-state-render.d.ts +60 -0
  126. package/dist/hooks/use-coagent-state-render.js +195 -0
  127. package/dist/hooks/use-coagent-state-render.js.map +1 -0
  128. package/dist/hooks/use-coagent-state-render.mjs +11 -0
  129. package/dist/hooks/use-coagent-state-render.mjs.map +1 -0
  130. package/dist/hooks/use-coagent.d.ts +199 -0
  131. package/dist/hooks/use-coagent.js +1068 -0
  132. package/dist/hooks/use-coagent.js.map +1 -0
  133. package/dist/hooks/use-coagent.mjs +29 -0
  134. package/dist/hooks/use-coagent.mjs.map +1 -0
  135. package/dist/hooks/use-copilot-action.d.ts +140 -0
  136. package/dist/hooks/use-copilot-action.js +369 -0
  137. package/dist/hooks/use-copilot-action.js.map +1 -0
  138. package/dist/hooks/use-copilot-action.mjs +11 -0
  139. package/dist/hooks/use-copilot-action.mjs.map +1 -0
  140. package/dist/hooks/use-copilot-authenticated-action.d.ts +8 -0
  141. package/dist/hooks/use-copilot-authenticated-action.js +418 -0
  142. package/dist/hooks/use-copilot-authenticated-action.js.map +1 -0
  143. package/dist/hooks/use-copilot-authenticated-action.mjs +12 -0
  144. package/dist/hooks/use-copilot-authenticated-action.mjs.map +1 -0
  145. package/dist/hooks/use-copilot-chat.d.ts +48 -0
  146. package/dist/hooks/use-copilot-chat.js +932 -0
  147. package/dist/hooks/use-copilot-chat.js.map +1 -0
  148. package/dist/hooks/use-copilot-chat.mjs +24 -0
  149. package/dist/hooks/use-copilot-chat.mjs.map +1 -0
  150. package/dist/hooks/use-copilot-readable.d.ts +33 -0
  151. package/dist/hooks/use-copilot-readable.js +158 -0
  152. package/dist/hooks/use-copilot-readable.js.map +1 -0
  153. package/dist/hooks/use-copilot-readable.mjs +9 -0
  154. package/dist/hooks/use-copilot-readable.mjs.map +1 -0
  155. package/dist/hooks/use-copilot-runtime-client.d.ts +5 -0
  156. package/dist/hooks/use-copilot-runtime-client.js +182 -0
  157. package/dist/hooks/use-copilot-runtime-client.js.map +1 -0
  158. package/dist/hooks/use-copilot-runtime-client.mjs +10 -0
  159. package/dist/hooks/use-copilot-runtime-client.mjs.map +1 -0
  160. package/dist/hooks/use-flat-category-store.d.ts +9 -0
  161. package/dist/hooks/use-flat-category-store.js +90 -0
  162. package/dist/hooks/use-flat-category-store.js.map +1 -0
  163. package/dist/hooks/use-flat-category-store.mjs +8 -0
  164. package/dist/hooks/use-flat-category-store.mjs.map +1 -0
  165. package/dist/hooks/use-make-copilot-document-readable.d.ts +12 -0
  166. package/dist/hooks/use-make-copilot-document-readable.js +153 -0
  167. package/dist/hooks/use-make-copilot-document-readable.js.map +1 -0
  168. package/dist/hooks/use-make-copilot-document-readable.mjs +9 -0
  169. package/dist/hooks/use-make-copilot-document-readable.mjs.map +1 -0
  170. package/dist/hooks/use-tree.d.ts +18 -0
  171. package/dist/hooks/use-tree.js +172 -0
  172. package/dist/hooks/use-tree.js.map +1 -0
  173. package/dist/hooks/use-tree.mjs +8 -0
  174. package/dist/hooks/use-tree.mjs.map +1 -0
  175. package/dist/index.d.ts +27 -0
  176. package/dist/index.js +2228 -0
  177. package/dist/index.js.map +1 -0
  178. package/dist/index.mjs +83 -0
  179. package/dist/index.mjs.map +1 -0
  180. package/dist/lib/copilot-task.d.ts +97 -0
  181. package/dist/lib/copilot-task.js +195 -0
  182. package/dist/lib/copilot-task.js.map +1 -0
  183. package/dist/lib/copilot-task.mjs +20 -0
  184. package/dist/lib/copilot-task.mjs.map +1 -0
  185. package/dist/lib/index.d.ts +11 -0
  186. package/dist/lib/index.js +197 -0
  187. package/dist/lib/index.js.map +1 -0
  188. package/dist/lib/index.mjs +21 -0
  189. package/dist/lib/index.mjs.map +1 -0
  190. package/dist/types/chat-suggestion-configuration.d.ts +22 -0
  191. package/dist/types/chat-suggestion-configuration.js +19 -0
  192. package/dist/types/chat-suggestion-configuration.js.map +1 -0
  193. package/dist/types/chat-suggestion-configuration.mjs +1 -0
  194. package/dist/types/chat-suggestion-configuration.mjs.map +1 -0
  195. package/dist/types/coagent-action.d.ts +29 -0
  196. package/dist/types/coagent-action.js +19 -0
  197. package/dist/types/coagent-action.js.map +1 -0
  198. package/dist/types/coagent-action.mjs +1 -0
  199. package/dist/types/coagent-action.mjs.map +1 -0
  200. package/dist/types/coagent-state.d.ts +11 -0
  201. package/dist/types/coagent-state.js +19 -0
  202. package/dist/types/coagent-state.js.map +1 -0
  203. package/dist/types/coagent-state.mjs +1 -0
  204. package/dist/types/coagent-state.mjs.map +1 -0
  205. package/dist/types/document-pointer.d.ts +9 -0
  206. package/dist/types/document-pointer.js +19 -0
  207. package/dist/types/document-pointer.js.map +1 -0
  208. package/dist/types/document-pointer.mjs +1 -0
  209. package/dist/types/document-pointer.mjs.map +1 -0
  210. package/dist/types/frontend-action.d.ts +126 -0
  211. package/dist/types/frontend-action.js +53 -0
  212. package/dist/types/frontend-action.js.map +1 -0
  213. package/dist/types/frontend-action.mjs +8 -0
  214. package/dist/types/frontend-action.mjs.map +1 -0
  215. package/dist/types/index.d.ts +7 -0
  216. package/dist/types/index.js +19 -0
  217. package/dist/types/index.js.map +1 -0
  218. package/dist/types/index.mjs +2 -0
  219. package/dist/types/index.mjs.map +1 -0
  220. package/dist/types/system-message.d.ts +3 -0
  221. package/dist/types/system-message.js +19 -0
  222. package/dist/types/system-message.js.map +1 -0
  223. package/dist/types/system-message.mjs +1 -0
  224. package/dist/types/system-message.mjs.map +1 -0
  225. package/dist/utils/dev-console.d.ts +3 -0
  226. package/dist/utils/dev-console.js +42 -0
  227. package/dist/utils/dev-console.js.map +1 -0
  228. package/dist/utils/dev-console.mjs +8 -0
  229. package/dist/utils/dev-console.mjs.map +1 -0
  230. package/dist/utils/extract.d.ts +43 -0
  231. package/dist/utils/extract.js +208 -0
  232. package/dist/utils/extract.js.map +1 -0
  233. package/dist/utils/extract.mjs +18 -0
  234. package/dist/utils/extract.mjs.map +1 -0
  235. package/dist/utils/index.d.ts +13 -0
  236. package/dist/utils/index.js +226 -0
  237. package/dist/utils/index.js.map +1 -0
  238. package/dist/utils/index.mjs +21 -0
  239. package/dist/utils/index.mjs.map +1 -0
  240. package/dist/utils/utils.d.ts +2 -0
  241. package/dist/utils/utils.js +19 -0
  242. package/dist/utils/utils.js.map +1 -0
  243. package/dist/utils/utils.mjs +1 -0
  244. package/dist/utils/utils.mjs.map +1 -0
  245. package/dist/utils/utils.test.d.ts +2 -0
  246. package/dist/utils/utils.test.js +9 -0
  247. package/dist/utils/utils.test.js.map +1 -0
  248. package/dist/utils/utils.test.mjs +7 -0
  249. package/dist/utils/utils.test.mjs.map +1 -0
  250. package/jest.config.js +5 -0
  251. package/package.json +69 -0
  252. package/src/components/copilot-provider/copilot-messages.tsx +23 -0
  253. package/src/components/copilot-provider/copilotkit-props.tsx +97 -0
  254. package/src/components/copilot-provider/copilotkit.tsx +401 -0
  255. package/src/components/copilot-provider/index.ts +3 -0
  256. package/src/components/error-boundary/error-boundary.tsx +42 -0
  257. package/src/components/error-boundary/error-utils.tsx +95 -0
  258. package/src/components/index.ts +1 -0
  259. package/src/components/toast/exclamation-mark-icon.tsx +27 -0
  260. package/src/components/toast/toast-provider.tsx +170 -0
  261. package/src/context/copilot-context.tsx +271 -0
  262. package/src/context/copilot-messages-context.tsx +29 -0
  263. package/src/context/index.ts +8 -0
  264. package/src/hooks/index.ts +12 -0
  265. package/src/hooks/use-chat.ts +649 -0
  266. package/src/hooks/use-coagent-state-render.ts +118 -0
  267. package/src/hooks/use-coagent.ts +368 -0
  268. package/src/hooks/use-copilot-action.ts +289 -0
  269. package/src/hooks/use-copilot-authenticated-action.ts +60 -0
  270. package/src/hooks/use-copilot-chat.ts +304 -0
  271. package/src/hooks/use-copilot-readable.ts +123 -0
  272. package/src/hooks/use-copilot-runtime-client.ts +28 -0
  273. package/src/hooks/use-flat-category-store.ts +104 -0
  274. package/src/hooks/use-make-copilot-document-readable.ts +30 -0
  275. package/src/hooks/use-tree.ts +202 -0
  276. package/src/index.tsx +7 -0
  277. package/src/lib/copilot-task.ts +201 -0
  278. package/src/lib/index.ts +1 -0
  279. package/src/types/chat-suggestion-configuration.ts +23 -0
  280. package/src/types/coagent-action.ts +31 -0
  281. package/src/types/coagent-state.ts +9 -0
  282. package/src/types/document-pointer.ts +7 -0
  283. package/src/types/frontend-action.ts +199 -0
  284. package/src/types/index.ts +8 -0
  285. package/src/types/system-message.ts +4 -0
  286. package/src/utils/dev-console.ts +18 -0
  287. package/src/utils/extract.ts +215 -0
  288. package/src/utils/index.ts +2 -0
  289. package/src/utils/utils.test.ts +7 -0
  290. package/src/utils/utils.ts +1 -0
  291. package/tsconfig.json +12 -0
  292. package/tsup.config.ts +16 -0
  293. package/typedoc.json +4 -0
@@ -0,0 +1,215 @@
1
+ import {
2
+ defaultCopilotContextCategories
3
+ } from "./chunk-2KT73RVG.mjs";
4
+ import {
5
+ useCopilotMessagesContext
6
+ } from "./chunk-DCTJZ742.mjs";
7
+ import {
8
+ useChat
9
+ } from "./chunk-U6JICUGO.mjs";
10
+ import {
11
+ useCopilotContext
12
+ } from "./chunk-WXREY7UP.mjs";
13
+ import {
14
+ useAsyncCallback
15
+ } from "./chunk-LNI2Z45S.mjs";
16
+ import {
17
+ __async,
18
+ __objRest,
19
+ __spreadProps,
20
+ __spreadValues
21
+ } from "./chunk-SKC7AJIV.mjs";
22
+
23
+ // src/hooks/use-copilot-chat.ts
24
+ import { useRef, useEffect, useCallback } from "react";
25
+ import { Role, TextMessage } from "@copilotkit/runtime-client-gql";
26
+ function useCopilotChat(_a = {}) {
27
+ var _b = _a, {
28
+ makeSystemMessage
29
+ } = _b, options = __objRest(_b, [
30
+ "makeSystemMessage"
31
+ ]);
32
+ const {
33
+ getContextString,
34
+ getFunctionCallHandler,
35
+ copilotApiConfig,
36
+ isLoading,
37
+ setIsLoading,
38
+ chatInstructions,
39
+ actions,
40
+ coagentStatesRef,
41
+ setCoagentStatesWithRef,
42
+ coAgentStateRenders,
43
+ agentSession,
44
+ setAgentSession,
45
+ forwardedParameters,
46
+ agentLock,
47
+ threadId,
48
+ setThreadId,
49
+ runId,
50
+ setRunId,
51
+ chatAbortControllerRef
52
+ } = useCopilotContext();
53
+ const { messages, setMessages } = useCopilotMessagesContext();
54
+ const latestGetContextString = useUpdatedRef(getContextString);
55
+ const deleteMessage = useCallback(
56
+ (messageId) => {
57
+ setMessages((prev) => prev.filter((message) => message.id !== messageId));
58
+ },
59
+ [setMessages]
60
+ );
61
+ const makeSystemMessageCallback = useCallback(() => {
62
+ const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
63
+ const contextString = latestGetContextString.current([], defaultCopilotContextCategories);
64
+ return new TextMessage({
65
+ content: systemMessageMaker(contextString, chatInstructions),
66
+ role: Role.System
67
+ });
68
+ }, [getContextString, makeSystemMessage, chatInstructions]);
69
+ const onCoAgentStateRender = useAsyncCallback(
70
+ (args) => __async(this, null, function* () {
71
+ var _a2;
72
+ const { name, nodeName, state } = args;
73
+ let action = Object.values(coAgentStateRenders).find(
74
+ (action2) => action2.name === name && action2.nodeName === nodeName
75
+ );
76
+ if (!action) {
77
+ action = Object.values(coAgentStateRenders).find(
78
+ (action2) => action2.name === name && !action2.nodeName
79
+ );
80
+ }
81
+ if (action) {
82
+ yield (_a2 = action.handler) == null ? void 0 : _a2.call(action, { state, nodeName });
83
+ }
84
+ }),
85
+ [coAgentStateRenders]
86
+ );
87
+ const { append, reload, stop, runChatCompletion } = useChat(__spreadProps(__spreadValues({}, options), {
88
+ actions: Object.values(actions),
89
+ copilotConfig: copilotApiConfig,
90
+ initialMessages: options.initialMessages || [],
91
+ onFunctionCall: getFunctionCallHandler(),
92
+ onCoAgentStateRender,
93
+ messages,
94
+ setMessages,
95
+ makeSystemMessageCallback,
96
+ isLoading,
97
+ setIsLoading,
98
+ coagentStatesRef,
99
+ setCoagentStatesWithRef,
100
+ agentSession,
101
+ setAgentSession,
102
+ forwardedParameters,
103
+ threadId,
104
+ setThreadId,
105
+ runId,
106
+ setRunId,
107
+ chatAbortControllerRef,
108
+ agentLock
109
+ }));
110
+ const latestAppend = useUpdatedRef(append);
111
+ const latestAppendFunc = useAsyncCallback(
112
+ (message, options2) => __async(this, null, function* () {
113
+ return yield latestAppend.current(message, options2);
114
+ }),
115
+ [latestAppend]
116
+ );
117
+ const latestReload = useUpdatedRef(reload);
118
+ const latestReloadFunc = useAsyncCallback(() => __async(this, null, function* () {
119
+ return yield latestReload.current();
120
+ }), [latestReload]);
121
+ const latestStop = useUpdatedRef(stop);
122
+ const latestStopFunc = useCallback(() => {
123
+ return latestStop.current();
124
+ }, [latestStop]);
125
+ const latestDelete = useUpdatedRef(deleteMessage);
126
+ const latestDeleteFunc = useCallback(
127
+ (messageId) => {
128
+ return latestDelete.current(messageId);
129
+ },
130
+ [latestDelete]
131
+ );
132
+ const latestSetMessages = useUpdatedRef(setMessages);
133
+ const latestSetMessagesFunc = useCallback(
134
+ (messages2) => {
135
+ return latestSetMessages.current(messages2);
136
+ },
137
+ [latestSetMessages]
138
+ );
139
+ const latestRunChatCompletion = useUpdatedRef(runChatCompletion);
140
+ const latestRunChatCompletionFunc = useAsyncCallback(() => __async(this, null, function* () {
141
+ return yield latestRunChatCompletion.current();
142
+ }), [latestRunChatCompletion]);
143
+ const reset = useCallback(() => {
144
+ latestStopFunc();
145
+ setMessages([]);
146
+ setThreadId(null);
147
+ setRunId(null);
148
+ setCoagentStatesWithRef({});
149
+ let initialAgentSession = null;
150
+ if (agentLock) {
151
+ initialAgentSession = {
152
+ agentName: agentLock
153
+ };
154
+ }
155
+ setAgentSession(initialAgentSession);
156
+ }, [
157
+ latestStopFunc,
158
+ setMessages,
159
+ setThreadId,
160
+ setCoagentStatesWithRef,
161
+ setAgentSession,
162
+ agentLock
163
+ ]);
164
+ const latestReset = useUpdatedRef(reset);
165
+ const latestResetFunc = useCallback(() => {
166
+ return latestReset.current();
167
+ }, [latestReset]);
168
+ return {
169
+ visibleMessages: messages,
170
+ appendMessage: latestAppendFunc,
171
+ setMessages: latestSetMessagesFunc,
172
+ reloadMessages: latestReloadFunc,
173
+ stopGeneration: latestStopFunc,
174
+ reset: latestResetFunc,
175
+ deleteMessage: latestDeleteFunc,
176
+ runChatCompletion: latestRunChatCompletionFunc,
177
+ isLoading
178
+ };
179
+ }
180
+ function useUpdatedRef(value) {
181
+ const ref = useRef(value);
182
+ useEffect(() => {
183
+ ref.current = value;
184
+ }, [value]);
185
+ return ref;
186
+ }
187
+ function defaultSystemMessage(contextString, additionalInstructions) {
188
+ return `
189
+ Please act as an efficient, competent, conscientious, and industrious professional assistant.
190
+
191
+ 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.
192
+ Always be polite and respectful, and prefer brevity over verbosity.
193
+
194
+ The user has provided you with the following context:
195
+ \`\`\`
196
+ ${contextString}
197
+ \`\`\`
198
+
199
+ 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.
200
+
201
+ Please assist them as best you can.
202
+
203
+ 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.
204
+
205
+ If you would like to call a function, call it without saying anything else.
206
+ ` + (additionalInstructions ? `
207
+
208
+ ${additionalInstructions}` : "");
209
+ }
210
+
211
+ export {
212
+ useCopilotChat,
213
+ defaultSystemMessage
214
+ };
215
+ //# sourceMappingURL=chunk-4TDYSNUD.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/use-copilot-chat.ts"],"sourcesContent":["/**\n * `useCopilotChat` is a React hook that lets you directly interact with the\n * Copilot instance. Use to implement a fully custom UI (headless UI) or to\n * programmatically interact with the Copilot instance managed by the default\n * UI.\n *\n * ## Usage\n *\n * ### Simple Usage\n *\n * ```tsx\n * import { useCopilotChat } from \"@copilotkit/react-core\";\n * import { Role, TextMessage } from \"@copilotkit/runtime-client-gql\";\n *\n * export function YourComponent() {\n * const { appendMessage } = useCopilotChat();\n *\n * appendMessage(\n * new TextMessage({\n * content: \"Hello World\",\n * role: Role.User,\n * }),\n * );\n *\n * // optionally, you can append a message without running chat completion\n * appendMessage(yourMessage, { followUp: false });\n * }\n * ```\n *\n * `useCopilotChat` returns an object with the following properties:\n *\n * ```tsx\n * const {\n * visibleMessages, // An array of messages that are currently visible in the chat.\n * appendMessage, // A function to append a message to the chat.\n * setMessages, // A function to set the messages in the chat.\n * deleteMessage, // A function to delete a message from the chat.\n * reloadMessages, // A function to reload the messages from the API.\n * stopGeneration, // A function to stop the generation of the next message.\n * isLoading, // A boolean indicating if the chat is loading.\n * } = useCopilotChat();\n * ```\n */\nimport { useRef, useEffect, useCallback } from \"react\";\nimport { AgentSession, useCopilotContext } from \"../context/copilot-context\";\nimport { Message, Role, TextMessage } from \"@copilotkit/runtime-client-gql\";\nimport { SystemMessageFunction } from \"../types\";\nimport { useChat, AppendMessageOptions } from \"./use-chat\";\nimport { defaultCopilotContextCategories } from \"../components\";\nimport { CoAgentStateRenderHandlerArguments } from \"@copilotkit/shared\";\nimport { useCopilotMessagesContext } from \"../context\";\nimport { useAsyncCallback } from \"../components/error-boundary/error-utils\";\n\nexport interface UseCopilotChatOptions {\n /**\n * A unique identifier for the chat. If not provided, a random one will be\n * generated. When provided, the `useChat` hook with the same `id` will\n * have shared states across components.\n */\n id?: string;\n\n /**\n * HTTP headers to be sent with the API request.\n */\n headers?: Record<string, string> | Headers;\n /**\n * System messages of the chat. Defaults to an empty array.\n */\n initialMessages?: Message[];\n\n /**\n * A function to generate the system message. Defaults to `defaultSystemMessage`.\n */\n makeSystemMessage?: SystemMessageFunction;\n}\n\nexport interface UseCopilotChatReturn {\n visibleMessages: Message[];\n appendMessage: (message: Message, options?: AppendMessageOptions) => Promise<void>;\n setMessages: (messages: Message[]) => void;\n deleteMessage: (messageId: string) => void;\n reloadMessages: () => Promise<void>;\n stopGeneration: () => void;\n reset: () => void;\n isLoading: boolean;\n runChatCompletion: () => Promise<Message[]>;\n}\n\nexport function useCopilotChat({\n makeSystemMessage,\n ...options\n}: UseCopilotChatOptions = {}): UseCopilotChatReturn {\n const {\n getContextString,\n getFunctionCallHandler,\n copilotApiConfig,\n isLoading,\n setIsLoading,\n chatInstructions,\n actions,\n coagentStatesRef,\n setCoagentStatesWithRef,\n coAgentStateRenders,\n agentSession,\n setAgentSession,\n forwardedParameters,\n agentLock,\n threadId,\n setThreadId,\n runId,\n setRunId,\n chatAbortControllerRef,\n } = useCopilotContext();\n const { messages, setMessages } = useCopilotMessagesContext();\n\n // We need to ensure that makeSystemMessageCallback always uses the latest\n // useCopilotReadable data.\n const latestGetContextString = useUpdatedRef(getContextString);\n const deleteMessage = useCallback(\n (messageId: string) => {\n setMessages((prev) => prev.filter((message) => message.id !== messageId));\n },\n [setMessages],\n );\n\n const makeSystemMessageCallback = useCallback(() => {\n const systemMessageMaker = makeSystemMessage || defaultSystemMessage;\n // this always gets the latest context string\n const contextString = latestGetContextString.current([], defaultCopilotContextCategories); // TODO: make the context categories configurable\n\n return new TextMessage({\n content: systemMessageMaker(contextString, chatInstructions),\n role: Role.System,\n });\n }, [getContextString, makeSystemMessage, chatInstructions]);\n\n const onCoAgentStateRender = useAsyncCallback(\n async (args: CoAgentStateRenderHandlerArguments) => {\n const { name, nodeName, state } = args;\n let action = Object.values(coAgentStateRenders).find(\n (action) => action.name === name && action.nodeName === nodeName,\n );\n if (!action) {\n action = Object.values(coAgentStateRenders).find(\n (action) => action.name === name && !action.nodeName,\n );\n }\n if (action) {\n await action.handler?.({ state, nodeName });\n }\n },\n [coAgentStateRenders],\n );\n\n const { append, reload, stop, runChatCompletion } = useChat({\n ...options,\n actions: Object.values(actions),\n copilotConfig: copilotApiConfig,\n initialMessages: options.initialMessages || [],\n onFunctionCall: getFunctionCallHandler(),\n onCoAgentStateRender,\n messages,\n setMessages,\n makeSystemMessageCallback,\n isLoading,\n setIsLoading,\n coagentStatesRef,\n setCoagentStatesWithRef,\n agentSession,\n setAgentSession,\n forwardedParameters,\n threadId,\n setThreadId,\n runId,\n setRunId,\n chatAbortControllerRef,\n agentLock,\n });\n\n // this is a workaround born out of a bug that Athena incessantly ran into.\n // We could not find the origin of the bug, however, it was clear that an outdated version of the append function was being used somehow --\n // it referenced the old state of the messages array, and not the latest one.\n //\n // We want to make copilotkit as abuse-proof as possible, so we are adding this workaround to ensure that the latest version of the append function is always used.\n //\n // How does this work?\n // we store the relevant function in a ref that is always up-to-date, and then we use that ref in the callback.\n const latestAppend = useUpdatedRef(append);\n const latestAppendFunc = useAsyncCallback(\n async (message: Message, options?: AppendMessageOptions) => {\n return await latestAppend.current(message, options);\n },\n [latestAppend],\n );\n\n const latestReload = useUpdatedRef(reload);\n const latestReloadFunc = useAsyncCallback(async () => {\n return await latestReload.current();\n }, [latestReload]);\n\n const latestStop = useUpdatedRef(stop);\n const latestStopFunc = useCallback(() => {\n return latestStop.current();\n }, [latestStop]);\n\n const latestDelete = useUpdatedRef(deleteMessage);\n const latestDeleteFunc = useCallback(\n (messageId: string) => {\n return latestDelete.current(messageId);\n },\n [latestDelete],\n );\n\n const latestSetMessages = useUpdatedRef(setMessages);\n const latestSetMessagesFunc = useCallback(\n (messages: Message[]) => {\n return latestSetMessages.current(messages);\n },\n [latestSetMessages],\n );\n\n const latestRunChatCompletion = useUpdatedRef(runChatCompletion);\n const latestRunChatCompletionFunc = useAsyncCallback(async () => {\n return await latestRunChatCompletion.current!();\n }, [latestRunChatCompletion]);\n\n const reset = useCallback(() => {\n latestStopFunc();\n setMessages([]);\n setThreadId(null);\n setRunId(null);\n setCoagentStatesWithRef({});\n let initialAgentSession: AgentSession | null = null;\n if (agentLock) {\n initialAgentSession = {\n agentName: agentLock,\n };\n }\n setAgentSession(initialAgentSession);\n }, [\n latestStopFunc,\n setMessages,\n setThreadId,\n setCoagentStatesWithRef,\n setAgentSession,\n agentLock,\n ]);\n\n const latestReset = useUpdatedRef(reset);\n const latestResetFunc = useCallback(() => {\n return latestReset.current();\n }, [latestReset]);\n\n return {\n visibleMessages: messages,\n appendMessage: latestAppendFunc,\n setMessages: latestSetMessagesFunc,\n reloadMessages: latestReloadFunc,\n stopGeneration: latestStopFunc,\n reset: latestResetFunc,\n deleteMessage: latestDeleteFunc,\n runChatCompletion: latestRunChatCompletionFunc,\n isLoading,\n };\n}\n\n// store `value` in a ref and update\n// it whenever it changes.\nfunction useUpdatedRef<T>(value: T) {\n const ref = useRef(value);\n\n useEffect(() => {\n ref.current = value;\n }, [value]);\n\n return ref;\n}\n\nexport function defaultSystemMessage(\n contextString: string,\n additionalInstructions?: string,\n): string {\n return (\n `\nPlease act as an efficient, competent, conscientious, and industrious professional assistant.\n\nHelp 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.\nAlways be polite and respectful, and prefer brevity over verbosity.\n\nThe user has provided you with the following context:\n\\`\\`\\`\n${contextString}\n\\`\\`\\`\n\nThey have also provided you with functions you can call to initiate actions on their behalf, or functions you can call to receive more information.\n\nPlease assist them as best you can.\n\nYou 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.\n\nIf you would like to call a function, call it without saying anything else.\n` + (additionalInstructions ? `\\n\\n${additionalInstructions}` : \"\")\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA2CA,SAAS,QAAQ,WAAW,mBAAmB;AAE/C,SAAkB,MAAM,mBAAmB;AA2CpC,SAAS,eAAe,KAGJ,CAAC,GAAyB;AAHtB,eAC7B;AAAA;AAAA,EAzFF,IAwF+B,IAE1B,oBAF0B,IAE1B;AAAA,IADH;AAAA;AAGA,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI,kBAAkB;AACtB,QAAM,EAAE,UAAU,YAAY,IAAI,0BAA0B;AAI5D,QAAM,yBAAyB,cAAc,gBAAgB;AAC7D,QAAM,gBAAgB;AAAA,IACpB,CAAC,cAAsB;AACrB,kBAAY,CAAC,SAAS,KAAK,OAAO,CAAC,YAAY,QAAQ,OAAO,SAAS,CAAC;AAAA,IAC1E;AAAA,IACA,CAAC,WAAW;AAAA,EACd;AAEA,QAAM,4BAA4B,YAAY,MAAM;AAClD,UAAM,qBAAqB,qBAAqB;AAEhD,UAAM,gBAAgB,uBAAuB,QAAQ,CAAC,GAAG,+BAA+B;AAExF,WAAO,IAAI,YAAY;AAAA,MACrB,SAAS,mBAAmB,eAAe,gBAAgB;AAAA,MAC3D,MAAM,KAAK;AAAA,IACb,CAAC;AAAA,EACH,GAAG,CAAC,kBAAkB,mBAAmB,gBAAgB,CAAC;AAE1D,QAA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AIA,SAAS,cAAiB,OAAU;AAClC,QAAM,MAAM,OAAO,KAAK;AAExB,YAAU,MAAM;AACd,QAAI,UAAU;AAAA,EAChB,GAAG,CAAC,KAAK,CAAC;AAEV,SAAO;AACT;AAEO,SAAS,qBACd,eACA,wBACQ;AACR,SACE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,KAUG,yBAAyB;AAAA;AAAA,EAAO,2BAA2B;AAEhE;","names":["_a","action","options","messages"]}
@@ -0,0 +1,70 @@
1
+ // src/hooks/use-flat-category-store.ts
2
+ import { useCallback, useReducer } from "react";
3
+ import { randomId } from "@copilotkit/shared";
4
+ var useFlatCategoryStore = () => {
5
+ const [elements, dispatch] = useReducer(flatCategoryStoreReducer, /* @__PURE__ */ new Map());
6
+ const addElement = useCallback((value, categories) => {
7
+ const newId = randomId();
8
+ dispatch({
9
+ type: "ADD_ELEMENT",
10
+ value,
11
+ id: newId,
12
+ categories
13
+ });
14
+ return newId;
15
+ }, []);
16
+ const removeElement = useCallback((id) => {
17
+ dispatch({ type: "REMOVE_ELEMENT", id });
18
+ }, []);
19
+ const allElements = useCallback(
20
+ (categories) => {
21
+ const categoriesSet = new Set(categories);
22
+ const result = [];
23
+ elements.forEach((element) => {
24
+ if (setsHaveIntersection(categoriesSet, element.categories)) {
25
+ result.push(element.value);
26
+ }
27
+ });
28
+ return result;
29
+ },
30
+ [elements]
31
+ );
32
+ return { addElement, removeElement, allElements };
33
+ };
34
+ var use_flat_category_store_default = useFlatCategoryStore;
35
+ function flatCategoryStoreReducer(state, action) {
36
+ switch (action.type) {
37
+ case "ADD_ELEMENT": {
38
+ const { value, id, categories } = action;
39
+ const newElement = {
40
+ id,
41
+ value,
42
+ categories: new Set(categories)
43
+ };
44
+ const newState = new Map(state);
45
+ newState.set(id, newElement);
46
+ return newState;
47
+ }
48
+ case "REMOVE_ELEMENT": {
49
+ const newState = new Map(state);
50
+ newState.delete(action.id);
51
+ return newState;
52
+ }
53
+ default:
54
+ return state;
55
+ }
56
+ }
57
+ function setsHaveIntersection(setA, setB) {
58
+ const [smallerSet, largerSet] = setA.size <= setB.size ? [setA, setB] : [setB, setA];
59
+ for (let item of smallerSet) {
60
+ if (largerSet.has(item)) {
61
+ return true;
62
+ }
63
+ }
64
+ return false;
65
+ }
66
+
67
+ export {
68
+ use_flat_category_store_default
69
+ };
70
+ //# sourceMappingURL=chunk-5FHSUKQL.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/use-flat-category-store.ts"],"sourcesContent":["import { useCallback, useReducer } from \"react\";\nimport { randomId } from \"@copilotkit/shared\";\n\nexport type FlatCategoryStoreId = string;\n\nexport interface UseFlatCategoryStoreReturn<T> {\n addElement: (value: T, categories: string[]) => FlatCategoryStoreId;\n removeElement: (id: FlatCategoryStoreId) => void;\n allElements: (categories: string[]) => T[];\n}\n\ninterface FlatCategoryStoreElement<T> {\n id: FlatCategoryStoreId;\n value: T;\n categories: Set<string>;\n}\n\nconst useFlatCategoryStore = <T>(): UseFlatCategoryStoreReturn<T> => {\n const [elements, dispatch] = useReducer<\n React.Reducer<Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>>, Action<T>>\n >(flatCategoryStoreReducer, new Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>>());\n\n const addElement = useCallback((value: T, categories: string[]): FlatCategoryStoreId => {\n const newId = randomId();\n dispatch({\n type: \"ADD_ELEMENT\",\n value,\n id: newId,\n categories,\n });\n return newId;\n }, []);\n\n const removeElement = useCallback((id: FlatCategoryStoreId): void => {\n dispatch({ type: \"REMOVE_ELEMENT\", id });\n }, []);\n\n const allElements = useCallback(\n (categories: string[]): T[] => {\n const categoriesSet = new Set(categories);\n const result: T[] = [];\n elements.forEach((element) => {\n if (setsHaveIntersection(categoriesSet, element.categories)) {\n result.push(element.value);\n }\n });\n return result;\n },\n [elements],\n );\n\n return { addElement, removeElement, allElements };\n};\n\nexport default useFlatCategoryStore;\n\n// Action types\ntype Action<T> =\n | {\n type: \"ADD_ELEMENT\";\n value: T;\n id: FlatCategoryStoreId;\n categories: string[];\n }\n | { type: \"REMOVE_ELEMENT\"; id: FlatCategoryStoreId };\n\n// Reducer\nfunction flatCategoryStoreReducer<T>(\n state: Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>>,\n action: Action<T>,\n): Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>> {\n switch (action.type) {\n case \"ADD_ELEMENT\": {\n const { value, id, categories } = action;\n const newElement: FlatCategoryStoreElement<T> = {\n id,\n value,\n categories: new Set(categories),\n };\n const newState = new Map(state);\n newState.set(id, newElement);\n return newState;\n }\n case \"REMOVE_ELEMENT\": {\n const newState = new Map(state);\n newState.delete(action.id);\n return newState;\n }\n default:\n return state;\n }\n}\n\nfunction setsHaveIntersection<T>(setA: Set<T>, setB: Set<T>): boolean {\n const [smallerSet, largerSet] = setA.size <= setB.size ? [setA, setB] : [setB, setA];\n\n for (let item of smallerSet) {\n if (largerSet.has(item)) {\n return true;\n }\n }\n\n return false;\n}\n"],"mappings":";AAAA,SAAS,aAAa,kBAAkB;AACxC,SAAS,gBAAgB;AAgBzB,IAAM,uBAAuB,MAAwC;AACnE,QAAM,CAAC,UAAU,QAAQ,IAAI,WAE3B,0BAA0B,oBAAI,IAAsD,CAAC;AAEvF,QAAM,aAAa,YAAY,CAAC,OAAU,eAA8C;AACtF,UAAM,QAAQ,SAAS;AACvB,aAAS;AAAA,MACP,MAAM;AAAA,MACN;AAAA,MACA,IAAI;AAAA,MACJ;AAAA,IACF,CAAC;AACD,WAAO;AAAA,EACT,GAAG,CAAC,CAAC;AAEL,QAAM,gBAAgB,YAAY,CAAC,OAAkC;AACnE,aAAS,EAAE,MAAM,kBAAkB,GAAG,CAAC;AAAA,EACzC,GAAG,CAAC,CAAC;AAEL,QAAM,cAAc;AAAA,IAClB,CAAC,eAA8B;AAC7B,YAAM,gBAAgB,IAAI,IAAI,UAAU;AACxC,YAAM,SAAc,CAAC;AACrB,eAAS,QAAQ,CAAC,YAAY;AAC5B,YAAI,qBAAqB,eAAe,QAAQ,UAAU,GAAG;AAC3D,iBAAO,KAAK,QAAQ,KAAK;AAAA,QAC3B;AAAA,MACF,CAAC;AACD,aAAO;AAAA,IACT;AAAA,IACA,CAAC,QAAQ;AAAA,EACX;AAEA,SAAO,EAAE,YAAY,eAAe,YAAY;AAClD;AAEA,IAAO,kCAAQ;AAaf,SAAS,yBACP,OACA,QACuD;AACvD,UAAQ,OAAO,MAAM;AAAA,IACnB,KAAK,eAAe;AAClB,YAAM,EAAE,OAAO,IAAI,WAAW,IAAI;AAClC,YAAM,aAA0C;AAAA,QAC9C;AAAA,QACA;AAAA,QACA,YAAY,IAAI,IAAI,UAAU;AAAA,MAChC;AACA,YAAM,WAAW,IAAI,IAAI,KAAK;AAC9B,eAAS,IAAI,IAAI,UAAU;AAC3B,aAAO;AAAA,IACT;AAAA,IACA,KAAK,kBAAkB;AACrB,YAAM,WAAW,IAAI,IAAI,KAAK;AAC9B,eAAS,OAAO,OAAO,EAAE;AACzB,aAAO;AAAA,IACT;AAAA,IACA;AACE,aAAO;AAAA,EACX;AACF;AAEA,SAAS,qBAAwB,MAAc,MAAuB;AACpE,QAAM,CAAC,YAAY,SAAS,IAAI,KAAK,QAAQ,KAAK,OAAO,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,IAAI;AAEnF,WAAS,QAAQ,YAAY;AAC3B,QAAI,UAAU,IAAI,IAAI,GAAG;AACvB,aAAO;AAAA,IACT;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
@@ -0,0 +1,35 @@
1
+ import {
2
+ useErrorToast,
3
+ useToast
4
+ } from "./chunk-LNI2Z45S.mjs";
5
+ import {
6
+ __spreadProps,
7
+ __spreadValues
8
+ } from "./chunk-SKC7AJIV.mjs";
9
+
10
+ // src/hooks/use-copilot-runtime-client.ts
11
+ import {
12
+ CopilotRuntimeClient
13
+ } from "@copilotkit/runtime-client-gql";
14
+ import { useMemo } from "react";
15
+ var useCopilotRuntimeClient = (options) => {
16
+ const { addGraphQLErrorsToast } = useToast();
17
+ const addErrorToast = useErrorToast();
18
+ const runtimeClient = useMemo(() => {
19
+ return new CopilotRuntimeClient(__spreadProps(__spreadValues({}, options), {
20
+ handleGQLErrors: (error) => {
21
+ if (error.graphQLErrors.length) {
22
+ addGraphQLErrorsToast(error.graphQLErrors);
23
+ } else {
24
+ addErrorToast([error]);
25
+ }
26
+ }
27
+ }));
28
+ }, [options, addGraphQLErrorsToast]);
29
+ return runtimeClient;
30
+ };
31
+
32
+ export {
33
+ useCopilotRuntimeClient
34
+ };
35
+ //# sourceMappingURL=chunk-6BQVZFNO.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/use-copilot-runtime-client.ts"],"sourcesContent":["import {\n CopilotRuntimeClient,\n CopilotRuntimeClientOptions,\n GraphQLError,\n} from \"@copilotkit/runtime-client-gql\";\nimport { useToast } from \"../components/toast/toast-provider\";\nimport { useMemo } from \"react\";\nimport { useErrorToast } from \"../components/error-boundary/error-utils\";\n\nexport const useCopilotRuntimeClient = (options: CopilotRuntimeClientOptions) => {\n const { addGraphQLErrorsToast } = useToast();\n const addErrorToast = useErrorToast();\n\n const runtimeClient = useMemo(() => {\n return new CopilotRuntimeClient({\n ...options,\n handleGQLErrors: (error) => {\n if ((error as any).graphQLErrors.length) {\n addGraphQLErrorsToast((error as any).graphQLErrors as GraphQLError[]);\n } else {\n addErrorToast([error]);\n }\n },\n });\n }, [options, addGraphQLErrorsToast]);\n\n return runtimeClient;\n};\n"],"mappings":";;;;;;;;;;AAAA;AAAA,EACE;AAAA,OAGK;AAEP,SAAS,eAAe;AAGjB,IAAM,0BAA0B,CAAC,YAAyC;AAC/E,QAAM,EAAE,sBAAsB,IAAI,SAAS;AAC3C,QAAM,gBAAgB,cAAc;AAEpC,QAAM,gBAAgB,QAAQ,MAAM;AAClC,WAAO,IAAI,qBAAqB,iCAC3B,UAD2B;AAAA,MAE9B,iBAAiB,CAAC,UAAU;AAC1B,YAAK,MAAc,cAAc,QAAQ;AACvC,gCAAuB,MAAc,aAA+B;AAAA,QACtE,OAAO;AACL,wBAAc,CAAC,KAAK,CAAC;AAAA,QACvB;AAAA,MACF;AAAA,IACF,EAAC;AAAA,EACH,GAAG,CAAC,SAAS,qBAAqB,CAAC;AAEnC,SAAO;AACT;","names":[]}
@@ -0,0 +1,140 @@
1
+ import {
2
+ __spreadProps,
3
+ __spreadValues
4
+ } from "./chunk-SKC7AJIV.mjs";
5
+
6
+ // src/hooks/use-tree.ts
7
+ import { randomId } from "@copilotkit/shared";
8
+ import { useCallback, useReducer } from "react";
9
+ var removeNode = (nodes, id) => {
10
+ return nodes.reduce((result, node) => {
11
+ if (node.id !== id) {
12
+ const newNode = __spreadProps(__spreadValues({}, node), { children: removeNode(node.children, id) });
13
+ result.push(newNode);
14
+ }
15
+ return result;
16
+ }, []);
17
+ };
18
+ var addNode = (nodes, newNode, parentId) => {
19
+ if (!parentId) {
20
+ return [...nodes, newNode];
21
+ }
22
+ return nodes.map((node) => {
23
+ if (node.id === parentId) {
24
+ return __spreadProps(__spreadValues({}, node), { children: [...node.children, newNode] });
25
+ } else if (node.children.length) {
26
+ return __spreadProps(__spreadValues({}, node), { children: addNode(node.children, newNode, parentId) });
27
+ }
28
+ return node;
29
+ });
30
+ };
31
+ var treeIndentationRepresentation = (index, indentLevel) => {
32
+ if (indentLevel === 0) {
33
+ return (index + 1).toString();
34
+ } else if (indentLevel === 1) {
35
+ return String.fromCharCode(65 + index);
36
+ } else if (indentLevel === 2) {
37
+ return String.fromCharCode(97 + index);
38
+ } else {
39
+ return "-";
40
+ }
41
+ };
42
+ var printNode = (node, prefix = "", indentLevel = 0) => {
43
+ const indent = " ".repeat(3).repeat(indentLevel);
44
+ const prefixPlusIndentLength = prefix.length + indent.length;
45
+ const subsequentLinesPrefix = " ".repeat(prefixPlusIndentLength);
46
+ const valueLines = node.value.split("\n");
47
+ const outputFirstLine = `${indent}${prefix}${valueLines[0]}`;
48
+ const outputSubsequentLines = valueLines.slice(1).map((line) => `${subsequentLinesPrefix}${line}`).join("\n");
49
+ let output = `${outputFirstLine}
50
+ `;
51
+ if (outputSubsequentLines) {
52
+ output += `${outputSubsequentLines}
53
+ `;
54
+ }
55
+ const childPrePrefix = " ".repeat(prefix.length);
56
+ node.children.forEach(
57
+ (child, index) => output += printNode(
58
+ child,
59
+ `${childPrePrefix}${treeIndentationRepresentation(index, indentLevel + 1)}. `,
60
+ indentLevel + 1
61
+ )
62
+ );
63
+ return output;
64
+ };
65
+ function treeReducer(state, action) {
66
+ switch (action.type) {
67
+ case "ADD_NODE": {
68
+ const { value, parentId, id: newNodeId } = action;
69
+ const newNode = {
70
+ id: newNodeId,
71
+ value,
72
+ children: [],
73
+ categories: new Set(action.categories)
74
+ };
75
+ try {
76
+ return addNode(state, newNode, parentId);
77
+ } catch (error) {
78
+ console.error(`Error while adding node with id ${newNodeId}: ${error}`);
79
+ return state;
80
+ }
81
+ }
82
+ case "REMOVE_NODE":
83
+ return removeNode(state, action.id);
84
+ default:
85
+ return state;
86
+ }
87
+ }
88
+ var useTree = () => {
89
+ const [tree, dispatch] = useReducer(treeReducer, []);
90
+ const addElement = useCallback(
91
+ (value, categories, parentId) => {
92
+ const newNodeId = randomId();
93
+ dispatch({
94
+ type: "ADD_NODE",
95
+ value,
96
+ parentId,
97
+ id: newNodeId,
98
+ categories
99
+ });
100
+ return newNodeId;
101
+ },
102
+ []
103
+ );
104
+ const removeElement = useCallback((id) => {
105
+ dispatch({ type: "REMOVE_NODE", id });
106
+ }, []);
107
+ const printTree = useCallback(
108
+ (categories) => {
109
+ const categoriesSet = new Set(categories);
110
+ let output = "";
111
+ tree.forEach((node, index) => {
112
+ if (!setsHaveIntersection(categoriesSet, node.categories)) {
113
+ return;
114
+ }
115
+ if (index !== 0) {
116
+ output += "\n";
117
+ }
118
+ output += printNode(node, `${treeIndentationRepresentation(index, 0)}. `);
119
+ });
120
+ return output;
121
+ },
122
+ [tree]
123
+ );
124
+ return { tree, addElement, printTree, removeElement };
125
+ };
126
+ var use_tree_default = useTree;
127
+ function setsHaveIntersection(setA, setB) {
128
+ const [smallerSet, largerSet] = setA.size <= setB.size ? [setA, setB] : [setB, setA];
129
+ for (let item of smallerSet) {
130
+ if (largerSet.has(item)) {
131
+ return true;
132
+ }
133
+ }
134
+ return false;
135
+ }
136
+
137
+ export {
138
+ use_tree_default
139
+ };
140
+ //# sourceMappingURL=chunk-6U3UH3KO.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/use-tree.ts"],"sourcesContent":["import { randomId } from \"@copilotkit/shared\";\nimport { useCallback, useReducer } from \"react\";\n\nexport type TreeNodeId = string;\n\nexport interface TreeNode {\n id: TreeNodeId;\n value: string;\n children: TreeNode[];\n parentId?: TreeNodeId;\n categories: Set<string>;\n}\n\nexport type Tree = TreeNode[];\n\nexport interface UseTreeReturn {\n tree: Tree;\n addElement: (value: string, categories: string[], parentId?: TreeNodeId) => TreeNodeId;\n printTree: (categories: string[]) => string;\n removeElement: (id: TreeNodeId) => void;\n}\n\nconst findNode = (nodes: Tree, id: TreeNodeId): TreeNode | undefined => {\n for (const node of nodes) {\n if (node.id === id) {\n return node;\n }\n const result = findNode(node.children, id);\n if (result) {\n return result;\n }\n }\n return undefined;\n};\n\nconst removeNode = (nodes: Tree, id: TreeNodeId): Tree => {\n return nodes.reduce((result: Tree, node) => {\n if (node.id !== id) {\n const newNode = { ...node, children: removeNode(node.children, id) };\n result.push(newNode);\n }\n return result;\n }, []);\n};\n\nconst addNode = (nodes: Tree, newNode: TreeNode, parentId?: TreeNodeId): Tree => {\n if (!parentId) {\n return [...nodes, newNode];\n }\n return nodes.map((node) => {\n if (node.id === parentId) {\n return { ...node, children: [...node.children, newNode] };\n } else if (node.children.length) {\n return { ...node, children: addNode(node.children, newNode, parentId) };\n }\n return node;\n });\n};\n\nconst treeIndentationRepresentation = (index: number, indentLevel: number): string => {\n if (indentLevel === 0) {\n return (index + 1).toString();\n } else if (indentLevel === 1) {\n return String.fromCharCode(65 + index); // 65 is the ASCII value for 'A'\n } else if (indentLevel === 2) {\n return String.fromCharCode(97 + index); // 97 is the ASCII value for 'a'\n } else {\n return \"-\";\n }\n};\n\nconst printNode = (node: TreeNode, prefix = \"\", indentLevel = 0): string => {\n const indent = \" \".repeat(3).repeat(indentLevel);\n\n const prefixPlusIndentLength = prefix.length + indent.length;\n const subsequentLinesPrefix = \" \".repeat(prefixPlusIndentLength);\n\n const valueLines = node.value.split(\"\\n\");\n\n const outputFirstLine = `${indent}${prefix}${valueLines[0]}`;\n const outputSubsequentLines = valueLines\n .slice(1)\n .map((line) => `${subsequentLinesPrefix}${line}`)\n .join(\"\\n\");\n\n let output = `${outputFirstLine}\\n`;\n if (outputSubsequentLines) {\n output += `${outputSubsequentLines}\\n`;\n }\n\n const childPrePrefix = \" \".repeat(prefix.length);\n\n node.children.forEach(\n (child, index) =>\n (output += printNode(\n child,\n `${childPrePrefix}${treeIndentationRepresentation(index, indentLevel + 1)}. `,\n indentLevel + 1,\n )),\n );\n return output;\n};\n\n// Action types\ntype Action =\n | {\n type: \"ADD_NODE\";\n value: string;\n parentId?: string;\n id: string;\n categories: string[];\n }\n | { type: \"REMOVE_NODE\"; id: string };\n\n// Reducer function\nfunction treeReducer(state: Tree, action: Action): Tree {\n switch (action.type) {\n case \"ADD_NODE\": {\n const { value, parentId, id: newNodeId } = action;\n const newNode: TreeNode = {\n id: newNodeId,\n value,\n children: [],\n categories: new Set(action.categories),\n };\n\n try {\n return addNode(state, newNode, parentId);\n } catch (error) {\n console.error(`Error while adding node with id ${newNodeId}: ${error}`);\n return state;\n }\n }\n case \"REMOVE_NODE\":\n return removeNode(state, action.id);\n default:\n return state;\n }\n}\n\n// useTree hook\nconst useTree = (): UseTreeReturn => {\n const [tree, dispatch] = useReducer(treeReducer, []);\n\n const addElement = useCallback(\n (value: string, categories: string[], parentId?: string): TreeNodeId => {\n const newNodeId = randomId(); // Generate new ID outside of dispatch\n dispatch({\n type: \"ADD_NODE\",\n value,\n parentId,\n id: newNodeId,\n categories: categories,\n });\n return newNodeId; // Return the new ID\n },\n [],\n );\n\n const removeElement = useCallback((id: TreeNodeId): void => {\n dispatch({ type: \"REMOVE_NODE\", id });\n }, []);\n\n const printTree = useCallback(\n (categories: string[]): string => {\n const categoriesSet = new Set(categories);\n\n let output = \"\";\n tree.forEach((node, index) => {\n // if the node does not have any of the desired categories, continue to the next node\n if (!setsHaveIntersection(categoriesSet, node.categories)) {\n return;\n }\n\n // add a new line before each node except the first one\n if (index !== 0) {\n output += \"\\n\";\n }\n\n output += printNode(node, `${treeIndentationRepresentation(index, 0)}. `);\n });\n return output;\n },\n [tree],\n );\n\n return { tree, addElement, printTree, removeElement };\n};\n\nexport default useTree;\n\nfunction setsHaveIntersection<T>(setA: Set<T>, setB: Set<T>): boolean {\n const [smallerSet, largerSet] = setA.size <= setB.size ? [setA, setB] : [setB, setA];\n\n for (let item of smallerSet) {\n if (largerSet.has(item)) {\n return true;\n }\n }\n\n return false;\n}\n"],"mappings":";;;;;;AAAA,SAAS,gBAAgB;AACzB,SAAS,aAAa,kBAAkB;AAkCxC,IAAM,aAAa,CAAC,OAAa,OAAyB;AACxD,SAAO,MAAM,OAAO,CAAC,QAAc,SAAS;AAC1C,QAAI,KAAK,OAAO,IAAI;AAClB,YAAM,UAAU,iCAAK,OAAL,EAAW,UAAU,WAAW,KAAK,UAAU,EAAE,EAAE;AACnE,aAAO,KAAK,OAAO;AAAA,IACrB;AACA,WAAO;AAAA,EACT,GAAG,CAAC,CAAC;AACP;AAEA,IAAM,UAAU,CAAC,OAAa,SAAmB,aAAgC;AAC/E,MAAI,CAAC,UAAU;AACb,WAAO,CAAC,GAAG,OAAO,OAAO;AAAA,EAC3B;AACA,SAAO,MAAM,IAAI,CAAC,SAAS;AACzB,QAAI,KAAK,OAAO,UAAU;AACxB,aAAO,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,IACT;AAAA,IACA,CAAC,IAAI;AAAA,EACP;AAEA,SAAO,EAAE,MAAM,YAAY,WAAW,cAAc;AACtD;AAEA,IAAO,mBAAQ;AAEf,SAAS,qBAAwB,MAAc,MAAuB;AACpE,QAAM,CAAC,YAAY,SAAS,IAAI,KAAK,QAAQ,KAAK,OAAO,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,IAAI;AAEnF,WAAS,QAAQ,YAAY;AAC3B,QAAI,UAAU,IAAI,IAAI,GAAG;AACvB,aAAO;AAAA,IACT;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
@@ -0,0 +1,22 @@
1
+ // src/context/copilot-messages-context.tsx
2
+ import React from "react";
3
+ var emptyCopilotContext = {
4
+ messages: [],
5
+ setMessages: () => []
6
+ };
7
+ var CopilotMessagesContext = React.createContext(emptyCopilotContext);
8
+ function useCopilotMessagesContext() {
9
+ const context = React.useContext(CopilotMessagesContext);
10
+ if (context === emptyCopilotContext) {
11
+ throw new Error(
12
+ "A messages consuming component was not wrapped with `<CopilotMessages> {...} </CopilotMessages>`"
13
+ );
14
+ }
15
+ return context;
16
+ }
17
+
18
+ export {
19
+ CopilotMessagesContext,
20
+ useCopilotMessagesContext
21
+ };
22
+ //# sourceMappingURL=chunk-DCTJZ742.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/context/copilot-messages-context.tsx"],"sourcesContent":["/**\n * An internal context to separate the messages state (which is constantly changing) from the rest of CopilotKit context\n */\n\nimport { Message } from \"@copilotkit/runtime-client-gql\";\nimport React from \"react\";\n\nexport interface CopilotMessagesContextParams {\n messages: Message[];\n setMessages: React.Dispatch<React.SetStateAction<Message[]>>;\n}\n\nconst emptyCopilotContext: CopilotMessagesContextParams = {\n messages: [],\n setMessages: () => [],\n};\n\nexport const CopilotMessagesContext =\n React.createContext<CopilotMessagesContextParams>(emptyCopilotContext);\n\nexport function useCopilotMessagesContext(): CopilotMessagesContextParams {\n const context = React.useContext(CopilotMessagesContext);\n if (context === emptyCopilotContext) {\n throw new Error(\n \"A messages consuming component was not wrapped with `<CopilotMessages> {...} </CopilotMessages>`\",\n );\n }\n return context;\n}\n"],"mappings":";AAKA,OAAO,WAAW;AAOlB,IAAM,sBAAoD;AAAA,EACxD,UAAU,CAAC;AAAA,EACX,aAAa,MAAM,CAAC;AACtB;AAEO,IAAM,yBACX,MAAM,cAA4C,mBAAmB;AAEhE,SAAS,4BAA0D;AACxE,QAAM,UAAU,MAAM,WAAW,sBAAsB;AACvD,MAAI,YAAY,qBAAqB;AACnC,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,SAAO;AACT;","names":[]}
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=chunk-EFZPSZWO.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -0,0 +1,107 @@
1
+ import {
2
+ useCopilotContext
3
+ } from "./chunk-WXREY7UP.mjs";
4
+ import {
5
+ useAsyncCallback
6
+ } from "./chunk-LNI2Z45S.mjs";
7
+ import {
8
+ __async,
9
+ __spreadValues
10
+ } from "./chunk-SKC7AJIV.mjs";
11
+
12
+ // src/hooks/use-copilot-action.ts
13
+ import { randomId } from "@copilotkit/shared";
14
+ import { createElement, Fragment, useEffect, useRef } from "react";
15
+ function useCopilotAction(action, dependencies) {
16
+ const { setAction, removeAction, actions, chatComponentsCache } = useCopilotContext();
17
+ const idRef = useRef(randomId());
18
+ const renderAndWaitRef = useRef(null);
19
+ action = __spreadValues({}, action);
20
+ if (
21
+ // renderAndWaitForResponse is not available for catch all actions
22
+ isFrontendAction(action) && // check if renderAndWaitForResponse is set
23
+ (action.renderAndWait || action.renderAndWaitForResponse)
24
+ ) {
25
+ const renderAndWait = action.renderAndWait || action.renderAndWaitForResponse;
26
+ action.renderAndWait = void 0;
27
+ action.renderAndWaitForResponse = void 0;
28
+ action.handler = useAsyncCallback(() => __async(this, null, function* () {
29
+ let resolve;
30
+ let reject;
31
+ const promise = new Promise((resolvePromise, rejectPromise) => {
32
+ resolve = resolvePromise;
33
+ reject = rejectPromise;
34
+ });
35
+ renderAndWaitRef.current = { promise, resolve, reject };
36
+ return yield promise;
37
+ }), []);
38
+ action.render = (props) => {
39
+ let status = props.status;
40
+ if (props.status === "executing" && !renderAndWaitRef.current) {
41
+ status = "inProgress";
42
+ }
43
+ const waitProps = {
44
+ status,
45
+ args: props.args,
46
+ result: props.result,
47
+ handler: status === "executing" ? renderAndWaitRef.current.resolve : void 0,
48
+ respond: status === "executing" ? renderAndWaitRef.current.resolve : void 0
49
+ };
50
+ const isNoArgsRenderWait = (_fn) => {
51
+ var _a;
52
+ return ((_a = action.parameters) == null ? void 0 : _a.length) === 0;
53
+ };
54
+ if (renderAndWait) {
55
+ if (isNoArgsRenderWait(renderAndWait)) {
56
+ return renderAndWait(waitProps);
57
+ } else {
58
+ return renderAndWait(waitProps);
59
+ }
60
+ }
61
+ return createElement(Fragment);
62
+ };
63
+ }
64
+ if (dependencies === void 0) {
65
+ if (actions[idRef.current]) {
66
+ if (isFrontendAction(action)) {
67
+ actions[idRef.current].handler = action.handler;
68
+ }
69
+ if (typeof action.render === "function") {
70
+ if (chatComponentsCache.current !== null) {
71
+ chatComponentsCache.current.actions[action.name] = action.render;
72
+ }
73
+ }
74
+ }
75
+ }
76
+ useEffect(() => {
77
+ setAction(idRef.current, action);
78
+ if (chatComponentsCache.current !== null && action.render !== void 0) {
79
+ chatComponentsCache.current.actions[action.name] = action.render;
80
+ }
81
+ return () => {
82
+ removeAction(idRef.current);
83
+ };
84
+ }, [
85
+ setAction,
86
+ removeAction,
87
+ isFrontendAction(action) ? action.description : void 0,
88
+ action.name,
89
+ isFrontendAction(action) ? action.disabled : void 0,
90
+ isFrontendAction(action) ? action.available : void 0,
91
+ // This should be faster than deep equality checking
92
+ // In addition, all major JS engines guarantee the order of object keys
93
+ JSON.stringify(isFrontendAction(action) ? action.parameters : []),
94
+ // include render only if it's a string
95
+ typeof action.render === "string" ? action.render : void 0,
96
+ // dependencies set by the developer
97
+ ...dependencies || []
98
+ ]);
99
+ }
100
+ function isFrontendAction(action) {
101
+ return action.name !== "*";
102
+ }
103
+
104
+ export {
105
+ useCopilotAction
106
+ };
107
+ //# sourceMappingURL=chunk-EWKJMQOF.mjs.map