@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,95 @@
1
+ import React, { useCallback, useEffect } from "react";
2
+ import { GraphQLError } from "@copilotkit/runtime-client-gql";
3
+ import { useToast } from "../toast/toast-provider";
4
+ import { ExclamationMarkIcon } from "../toast/exclamation-mark-icon";
5
+
6
+ export function ErrorToast({ errors }: { errors: (Error | GraphQLError)[] }) {
7
+ const errorsToRender = errors.map((error, idx) => {
8
+ const message =
9
+ "extensions" in error
10
+ ? (error.extensions?.originalError as undefined | { message?: string })?.message
11
+ : error.message;
12
+ const code = "extensions" in error ? (error.extensions?.code as string) : null;
13
+
14
+ return (
15
+ <div
16
+ key={idx}
17
+ style={{
18
+ marginTop: idx === 0 ? 0 : 10,
19
+ marginBottom: 14,
20
+ }}
21
+ >
22
+ <ExclamationMarkIcon style={{ marginBottom: 4 }} />
23
+
24
+ {code && (
25
+ <div
26
+ style={{
27
+ fontWeight: "600",
28
+ marginBottom: 4,
29
+ }}
30
+ >
31
+ Copilot Cloud Error:{" "}
32
+ <span style={{ fontFamily: "monospace", fontWeight: "normal" }}>{code}</span>
33
+ </div>
34
+ )}
35
+ <div>{message}</div>
36
+ </div>
37
+ );
38
+ });
39
+ return (
40
+ <div
41
+ style={{
42
+ fontSize: "13px",
43
+ maxWidth: "600px",
44
+ }}
45
+ >
46
+ {errorsToRender}
47
+ <div style={{ fontSize: "11px", opacity: 0.75 }}>
48
+ NOTE: This error only displays during local development.
49
+ </div>
50
+ </div>
51
+ );
52
+ }
53
+
54
+ export function useErrorToast() {
55
+ const { addToast } = useToast();
56
+
57
+ return useCallback(
58
+ (error: (Error | GraphQLError)[]) => {
59
+ const errorId = error
60
+ .map((err) => {
61
+ const message =
62
+ "extensions" in err
63
+ ? (err.extensions?.originalError as any)?.message || err.message
64
+ : err.message;
65
+ const stack = err.stack || "";
66
+ return btoa(message + stack).slice(0, 32); // Create hash from message + stack
67
+ })
68
+ .join("|");
69
+
70
+ addToast({
71
+ type: "error",
72
+ id: errorId, // Toast libraries typically dedupe by id
73
+ message: <ErrorToast errors={error} />,
74
+ });
75
+ },
76
+ [addToast],
77
+ );
78
+ }
79
+
80
+ export function useAsyncCallback<T extends (...args: any[]) => Promise<any>>(
81
+ callback: T,
82
+ deps: Parameters<typeof useCallback>[1],
83
+ ) {
84
+ const addErrorToast = useErrorToast();
85
+ return useCallback(async (...args: Parameters<T>) => {
86
+ try {
87
+ return await callback(...args);
88
+ } catch (error) {
89
+ console.error("Error in async callback:", error);
90
+ // @ts-ignore
91
+ addErrorToast([error]);
92
+ throw error;
93
+ }
94
+ }, deps);
95
+ }
@@ -0,0 +1 @@
1
+ export * from "./copilot-provider";
@@ -0,0 +1,27 @@
1
+ import React from "react";
2
+
3
+ export const ExclamationMarkIcon = ({
4
+ className,
5
+ style,
6
+ }: {
7
+ className?: string;
8
+ style?: React.CSSProperties;
9
+ }) => (
10
+ <svg
11
+ xmlns="http://www.w3.org/2000/svg"
12
+ width="24"
13
+ height="24"
14
+ viewBox="0 0 24 24"
15
+ fill="none"
16
+ stroke="currentColor"
17
+ strokeWidth="2"
18
+ strokeLinecap="round"
19
+ strokeLinejoin="round"
20
+ className={`lucide lucide-circle-alert ${className ? className : ""}`}
21
+ style={style}
22
+ >
23
+ <circle cx="12" cy="12" r="10" />
24
+ <line x1="12" x2="12" y1="8" y2="12" />
25
+ <line x1="12" x2="12.01" y1="16" y2="16" />
26
+ </svg>
27
+ );
@@ -0,0 +1,170 @@
1
+ import { GraphQLError } from "@copilotkit/runtime-client-gql";
2
+ import React, { createContext, useContext, useState, useCallback } from "react";
3
+ import { ErrorToast } from "../error-boundary/error-utils";
4
+ import { PartialBy } from "@copilotkit/shared";
5
+
6
+ interface Toast {
7
+ id: string;
8
+ message: string | React.ReactNode;
9
+ type: "info" | "success" | "warning" | "error";
10
+ duration?: number;
11
+ }
12
+
13
+ interface ToastContextValue {
14
+ toasts: Toast[];
15
+ addToast: (toast: PartialBy<Toast, "id">) => void;
16
+ addGraphQLErrorsToast: (errors: GraphQLError[]) => void;
17
+ removeToast: (id: string) => void;
18
+ enabled: boolean;
19
+ }
20
+
21
+ const ToastContext = createContext<ToastContextValue | undefined>(undefined);
22
+
23
+ export function useToast() {
24
+ const context = useContext(ToastContext);
25
+ if (!context) {
26
+ throw new Error("useToast must be used within a ToastProvider");
27
+ }
28
+ return context;
29
+ }
30
+
31
+ export function ToastProvider({
32
+ enabled,
33
+ children,
34
+ }: {
35
+ enabled: boolean;
36
+ children: React.ReactNode;
37
+ }) {
38
+ const [toasts, setToasts] = useState<Toast[]>([]);
39
+ const addToast = useCallback((toast: PartialBy<Toast, "id">) => {
40
+ const id = toast.id ?? Math.random().toString(36).substring(2, 9);
41
+
42
+ setToasts((currentToasts) => {
43
+ if (currentToasts.find((toast) => toast.id === id)) return currentToasts;
44
+ return [...currentToasts, { ...toast, id }];
45
+ });
46
+
47
+ if (toast.duration) {
48
+ setTimeout(() => {
49
+ removeToast(id);
50
+ }, toast.duration);
51
+ }
52
+ }, []);
53
+
54
+ const addGraphQLErrorsToast = useCallback((errors: GraphQLError[]) => {
55
+ // We do not display these errors unless we are in dev mode.
56
+ // if (!showDevConsole) {
57
+ // return;
58
+ // }
59
+
60
+ addToast({
61
+ type: "error",
62
+ message: <ErrorToast errors={errors} />,
63
+ });
64
+ }, []);
65
+
66
+ const removeToast = useCallback((id: string) => {
67
+ setToasts((currentToasts) => currentToasts.filter((toast) => toast.id !== id));
68
+ }, []);
69
+
70
+ const value = {
71
+ toasts,
72
+ addToast,
73
+ addGraphQLErrorsToast,
74
+ removeToast,
75
+ enabled,
76
+ };
77
+
78
+ return (
79
+ <ToastContext.Provider value={value}>
80
+ <div
81
+ style={{
82
+ position: "fixed",
83
+ bottom: "1rem",
84
+ left: "50%",
85
+ transform: "translateX(-50%)",
86
+ zIndex: 50,
87
+ display: "flex",
88
+ flexDirection: "column",
89
+ gap: "0.5rem",
90
+ }}
91
+ >
92
+ {toasts.length > 1 && (
93
+ <div style={{ textAlign: "right" }}>
94
+ <button
95
+ onClick={() => setToasts([])}
96
+ style={{
97
+ padding: "4px 8px",
98
+ fontSize: "12px",
99
+ cursor: "pointer",
100
+ background: "white",
101
+ border: "1px solid rgba(0,0,0,0.2)",
102
+ borderRadius: "4px",
103
+ }}
104
+ >
105
+ Close All
106
+ </button>
107
+ </div>
108
+ )}
109
+ {toasts.map((toast) => (
110
+ <Toast
111
+ key={toast.id}
112
+ message={toast.message}
113
+ type={toast.type}
114
+ onClose={() => removeToast(toast.id)}
115
+ />
116
+ ))}
117
+ </div>
118
+ {children}
119
+ </ToastContext.Provider>
120
+ );
121
+ }
122
+
123
+ function Toast({
124
+ message,
125
+ type = "info",
126
+ onClose,
127
+ }: {
128
+ message: string | React.ReactNode;
129
+ type: "info" | "success" | "warning" | "error";
130
+ onClose: () => void;
131
+ }) {
132
+ const bgColors = {
133
+ info: "#3b82f6",
134
+ success: "#22c55e",
135
+ warning: "#eab308",
136
+ error: "#ef4444",
137
+ };
138
+
139
+ return (
140
+ <div
141
+ style={{
142
+ backgroundColor: bgColors[type],
143
+ color: "white",
144
+ padding: "0.5rem 1.5rem",
145
+ borderRadius: "0.25rem",
146
+ boxShadow: "0 2px 4px rgba(0,0,0,0.1)",
147
+ position: "relative",
148
+ minWidth: "200px",
149
+ }}
150
+ >
151
+ <div>{message}</div>
152
+ <button
153
+ onClick={onClose}
154
+ style={{
155
+ position: "absolute",
156
+ top: "0",
157
+ right: "0",
158
+ background: "none",
159
+ border: "none",
160
+ color: "white",
161
+ cursor: "pointer",
162
+ padding: "0.5rem",
163
+ fontSize: "1rem",
164
+ }}
165
+ >
166
+
167
+ </button>
168
+ </div>
169
+ );
170
+ }
@@ -0,0 +1,271 @@
1
+ import { CopilotCloudConfig, FunctionCallHandler } from "@copilotkit/shared";
2
+ import {
3
+ ActionRenderProps,
4
+ CatchAllActionRenderProps,
5
+ FrontendAction,
6
+ } from "../types/frontend-action";
7
+ import React from "react";
8
+ import { TreeNodeId } from "../hooks/use-tree";
9
+ import { DocumentPointer } from "../types";
10
+ import { CopilotChatSuggestionConfiguration } from "../types/chat-suggestion-configuration";
11
+ import { CoAgentStateRender, CoAgentStateRenderProps } from "../types/coagent-action";
12
+ import { CoagentState } from "../types/coagent-state";
13
+ import { CopilotRuntimeClient, ForwardedParametersInput } from "@copilotkit/runtime-client-gql";
14
+ import { Agent } from "@copilotkit/runtime-client-gql";
15
+
16
+ /**
17
+ * Interface for the configuration of the Copilot API.
18
+ */
19
+ export interface CopilotApiConfig {
20
+ /**
21
+ * The public API key for Copilot Cloud.
22
+ */
23
+ publicApiKey?: string;
24
+
25
+ /**
26
+ * The configuration for Copilot Cloud.
27
+ */
28
+ cloud?: CopilotCloudConfig;
29
+
30
+ /**
31
+ * The endpoint for the chat API.
32
+ */
33
+ chatApiEndpoint: string;
34
+
35
+ /**
36
+ * The endpoint for the Copilot transcribe audio service.
37
+ */
38
+ transcribeAudioUrl?: string;
39
+
40
+ /**
41
+ * The endpoint for the Copilot text to speech service.
42
+ */
43
+ textToSpeechUrl?: string;
44
+
45
+ /**
46
+ * additional headers to be sent with the request
47
+ * @default {}
48
+ * @example
49
+ * ```
50
+ * {
51
+ * 'Authorization': 'Bearer your_token_here'
52
+ * }
53
+ * ```
54
+ */
55
+ headers: Record<string, string>;
56
+
57
+ /**
58
+ * Custom properties to be sent with the request
59
+ * @default {}
60
+ * @example
61
+ * ```
62
+ * {
63
+ * 'user_id': 'user_id'
64
+ * }
65
+ * ```
66
+ */
67
+ properties?: Record<string, any>;
68
+
69
+ /**
70
+ * Indicates whether the user agent should send or receive cookies from the other domain
71
+ * in the case of cross-origin requests.
72
+ */
73
+ credentials?: RequestCredentials;
74
+ }
75
+
76
+ export type InChatRenderFunction = (
77
+ props: ActionRenderProps<any> | CatchAllActionRenderProps<any>,
78
+ ) => string | JSX.Element;
79
+ export type CoagentInChatRenderFunction = (
80
+ props: CoAgentStateRenderProps<any>,
81
+ ) => string | JSX.Element | undefined | null;
82
+
83
+ export interface ChatComponentsCache {
84
+ actions: Record<string, InChatRenderFunction | string>;
85
+ coAgentStateRenders: Record<string, CoagentInChatRenderFunction | string>;
86
+ }
87
+
88
+ export interface AgentSession {
89
+ agentName: string;
90
+ threadId?: string;
91
+ nodeName?: string;
92
+ }
93
+
94
+ export interface AuthState {
95
+ status: "authenticated" | "unauthenticated";
96
+ authHeaders: Record<string, string>;
97
+ userId?: string;
98
+ metadata?: Record<string, any>;
99
+ }
100
+
101
+ export type ActionName = string;
102
+
103
+ export interface CopilotContextParams {
104
+ // function-calling
105
+ actions: Record<string, FrontendAction<any>>;
106
+ setAction: (id: string, action: FrontendAction<any>) => void;
107
+ removeAction: (id: string) => void;
108
+
109
+ // coagent actions
110
+ coAgentStateRenders: Record<string, CoAgentStateRender<any>>;
111
+ setCoAgentStateRender: (id: string, stateRender: CoAgentStateRender<any>) => void;
112
+ removeCoAgentStateRender: (id: string) => void;
113
+
114
+ chatComponentsCache: React.RefObject<ChatComponentsCache>;
115
+
116
+ getFunctionCallHandler: (
117
+ customEntryPoints?: Record<string, FrontendAction<any>>,
118
+ ) => FunctionCallHandler;
119
+
120
+ // text context
121
+ addContext: (context: string, parentId?: string, categories?: string[]) => TreeNodeId;
122
+ removeContext: (id: TreeNodeId) => void;
123
+ getContextString: (documents: DocumentPointer[], categories: string[]) => string;
124
+
125
+ // document context
126
+ addDocumentContext: (documentPointer: DocumentPointer, categories?: string[]) => TreeNodeId;
127
+ removeDocumentContext: (documentId: string) => void;
128
+ getDocumentsContext: (categories: string[]) => DocumentPointer[];
129
+
130
+ isLoading: boolean;
131
+ setIsLoading: React.Dispatch<React.SetStateAction<boolean>>;
132
+
133
+ chatSuggestionConfiguration: { [key: string]: CopilotChatSuggestionConfiguration };
134
+ addChatSuggestionConfiguration: (
135
+ id: string,
136
+ suggestion: CopilotChatSuggestionConfiguration,
137
+ ) => void;
138
+ removeChatSuggestionConfiguration: (id: string) => void;
139
+
140
+ chatInstructions: string;
141
+ setChatInstructions: React.Dispatch<React.SetStateAction<string>>;
142
+
143
+ // api endpoints
144
+ copilotApiConfig: CopilotApiConfig;
145
+
146
+ showDevConsole: boolean | "auto";
147
+
148
+ // agents
149
+ coagentStates: Record<string, CoagentState>;
150
+ setCoagentStates: React.Dispatch<React.SetStateAction<Record<string, CoagentState>>>;
151
+ coagentStatesRef: React.RefObject<Record<string, CoagentState>>;
152
+ setCoagentStatesWithRef: (
153
+ value:
154
+ | Record<string, CoagentState>
155
+ | ((prev: Record<string, CoagentState>) => Record<string, CoagentState>),
156
+ ) => void;
157
+
158
+ agentSession: AgentSession | null;
159
+ setAgentSession: React.Dispatch<React.SetStateAction<AgentSession | null>>;
160
+
161
+ agentLock: string | null;
162
+
163
+ threadId: string | null;
164
+ setThreadId: React.Dispatch<React.SetStateAction<string | null>>;
165
+
166
+ runId: string | null;
167
+ setRunId: React.Dispatch<React.SetStateAction<string | null>>;
168
+
169
+ // The chat abort controller can be used to stop generation globally,
170
+ // i.e. when using `stop()` from `useChat`
171
+ chatAbortControllerRef: React.MutableRefObject<AbortController | null>;
172
+
173
+ // runtime
174
+ runtimeClient: CopilotRuntimeClient;
175
+
176
+ /**
177
+ * The forwarded parameters to use for the task.
178
+ */
179
+ forwardedParameters?: Pick<ForwardedParametersInput, "temperature">;
180
+ availableAgents: Agent[];
181
+
182
+ /**
183
+ * The auth states for the CopilotKit.
184
+ */
185
+ authStates?: Record<ActionName, AuthState>;
186
+ setAuthStates?: React.Dispatch<React.SetStateAction<Record<ActionName, AuthState>>>;
187
+
188
+ /**
189
+ * The auth config for the CopilotKit.
190
+ */
191
+ authConfig?: {
192
+ SignInComponent: React.ComponentType<{
193
+ onSignInComplete: (authState: AuthState) => void;
194
+ }>;
195
+ };
196
+ }
197
+
198
+ const emptyCopilotContext: CopilotContextParams = {
199
+ actions: {},
200
+ setAction: () => {},
201
+ removeAction: () => {},
202
+
203
+ coAgentStateRenders: {},
204
+ setCoAgentStateRender: () => {},
205
+ removeCoAgentStateRender: () => {},
206
+
207
+ chatComponentsCache: { current: { actions: {}, coAgentStateRenders: {} } },
208
+ getContextString: (documents: DocumentPointer[], categories: string[]) =>
209
+ returnAndThrowInDebug(""),
210
+ addContext: () => "",
211
+ removeContext: () => {},
212
+
213
+ getFunctionCallHandler: () => returnAndThrowInDebug(async () => {}),
214
+
215
+ isLoading: false,
216
+ setIsLoading: () => returnAndThrowInDebug(false),
217
+
218
+ chatInstructions: "",
219
+ setChatInstructions: () => returnAndThrowInDebug(""),
220
+
221
+ getDocumentsContext: (categories: string[]) => returnAndThrowInDebug([]),
222
+ addDocumentContext: () => returnAndThrowInDebug(""),
223
+ removeDocumentContext: () => {},
224
+ runtimeClient: {} as any,
225
+
226
+ copilotApiConfig: new (class implements CopilotApiConfig {
227
+ get chatApiEndpoint(): string {
228
+ throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
229
+ }
230
+
231
+ get headers(): Record<string, string> {
232
+ return {};
233
+ }
234
+ get body(): Record<string, any> {
235
+ return {};
236
+ }
237
+ })(),
238
+
239
+ chatSuggestionConfiguration: {},
240
+ addChatSuggestionConfiguration: () => {},
241
+ removeChatSuggestionConfiguration: () => {},
242
+ showDevConsole: "auto",
243
+ coagentStates: {},
244
+ setCoagentStates: () => {},
245
+ coagentStatesRef: { current: {} },
246
+ setCoagentStatesWithRef: () => {},
247
+ agentSession: null,
248
+ setAgentSession: () => {},
249
+ forwardedParameters: {},
250
+ agentLock: null,
251
+ threadId: null,
252
+ setThreadId: () => {},
253
+ runId: null,
254
+ setRunId: () => {},
255
+ chatAbortControllerRef: { current: null },
256
+ availableAgents: [],
257
+ };
258
+
259
+ export const CopilotContext = React.createContext<CopilotContextParams>(emptyCopilotContext);
260
+
261
+ export function useCopilotContext(): CopilotContextParams {
262
+ const context = React.useContext(CopilotContext);
263
+ if (context === emptyCopilotContext) {
264
+ throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
265
+ }
266
+ return context;
267
+ }
268
+
269
+ function returnAndThrowInDebug<T>(_value: T): T {
270
+ throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
271
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * An internal context to separate the messages state (which is constantly changing) from the rest of CopilotKit context
3
+ */
4
+
5
+ import { Message } from "@copilotkit/runtime-client-gql";
6
+ import React from "react";
7
+
8
+ export interface CopilotMessagesContextParams {
9
+ messages: Message[];
10
+ setMessages: React.Dispatch<React.SetStateAction<Message[]>>;
11
+ }
12
+
13
+ const emptyCopilotContext: CopilotMessagesContextParams = {
14
+ messages: [],
15
+ setMessages: () => [],
16
+ };
17
+
18
+ export const CopilotMessagesContext =
19
+ React.createContext<CopilotMessagesContextParams>(emptyCopilotContext);
20
+
21
+ export function useCopilotMessagesContext(): CopilotMessagesContextParams {
22
+ const context = React.useContext(CopilotMessagesContext);
23
+ if (context === emptyCopilotContext) {
24
+ throw new Error(
25
+ "A messages consuming component was not wrapped with `<CopilotMessages> {...} </CopilotMessages>`",
26
+ );
27
+ }
28
+ return context;
29
+ }
@@ -0,0 +1,8 @@
1
+ export { CopilotContext, useCopilotContext } from "./copilot-context";
2
+ export { CopilotMessagesContext, useCopilotMessagesContext } from "./copilot-messages-context";
3
+ export type {
4
+ CopilotContextParams,
5
+ CoagentInChatRenderFunction,
6
+ CopilotApiConfig,
7
+ } from "./copilot-context";
8
+ export type { CopilotMessagesContextParams } from "./copilot-messages-context";
@@ -0,0 +1,12 @@
1
+ export { useCopilotChat } from "./use-copilot-chat";
2
+ export type { UseCopilotChatOptions } from "./use-copilot-chat";
3
+ export type { UseCopilotChatReturn } from "./use-copilot-chat";
4
+
5
+ export { useCopilotAction } from "./use-copilot-action";
6
+ export { useCoAgentStateRender } from "./use-coagent-state-render";
7
+ export { useMakeCopilotDocumentReadable } from "./use-make-copilot-document-readable";
8
+ export { type UseChatHelpers } from "./use-chat";
9
+ export { useCopilotReadable } from "./use-copilot-readable";
10
+ export { useCoAgent, type HintFunction, runAgent, startAgent, stopAgent } from "./use-coagent";
11
+ export { useCopilotRuntimeClient } from "./use-copilot-runtime-client";
12
+ export { useCopilotAuthenticatedAction } from "./use-copilot-authenticated-action";