@copilotkit/react-core 1.5.1-test-custom-tag-prerelease-1.0 → 1.5.2-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (252) hide show
  1. package/CHANGELOG.md +66 -5
  2. package/package.json +3 -3
  3. package/src/components/copilot-provider/copilotkit-props.tsx +10 -0
  4. package/src/components/copilot-provider/copilotkit.tsx +44 -7
  5. package/src/components/toast/toast-provider.tsx +1 -1
  6. package/src/context/copilot-context.tsx +28 -2
  7. package/src/hooks/index.ts +1 -0
  8. package/src/hooks/use-chat.ts +15 -5
  9. package/src/hooks/use-coagent-state-render.ts +11 -0
  10. package/src/hooks/use-coagent.ts +11 -1
  11. package/src/hooks/use-copilot-authenticated-action.ts +60 -0
  12. package/src/hooks/use-copilot-chat.ts +1 -0
  13. package/dist/chunk-5FHSUKQL.mjs +0 -70
  14. package/dist/chunk-5FHSUKQL.mjs.map +0 -1
  15. package/dist/chunk-6KHA5W5R.mjs +0 -28
  16. package/dist/chunk-6KHA5W5R.mjs.map +0 -1
  17. package/dist/chunk-6U3UH3KO.mjs +0 -140
  18. package/dist/chunk-6U3UH3KO.mjs.map +0 -1
  19. package/dist/chunk-7LRDVJH5.mjs +0 -35
  20. package/dist/chunk-7LRDVJH5.mjs.map +0 -1
  21. package/dist/chunk-DCTJZ742.mjs +0 -22
  22. package/dist/chunk-DCTJZ742.mjs.map +0 -1
  23. package/dist/chunk-EFZPSZWO.mjs +0 -1
  24. package/dist/chunk-EFZPSZWO.mjs.map +0 -1
  25. package/dist/chunk-L34MHAXR.mjs +0 -1
  26. package/dist/chunk-L34MHAXR.mjs.map +0 -1
  27. package/dist/chunk-LUGEI4YQ.mjs +0 -1
  28. package/dist/chunk-LUGEI4YQ.mjs.map +0 -1
  29. package/dist/chunk-M3SYKJ3W.mjs +0 -393
  30. package/dist/chunk-M3SYKJ3W.mjs.map +0 -1
  31. package/dist/chunk-MKEQ2MJG.mjs +0 -107
  32. package/dist/chunk-MKEQ2MJG.mjs.map +0 -1
  33. package/dist/chunk-MLAS4QUR.mjs +0 -18
  34. package/dist/chunk-MLAS4QUR.mjs.map +0 -1
  35. package/dist/chunk-NYBGR2NK.mjs +0 -120
  36. package/dist/chunk-NYBGR2NK.mjs.map +0 -1
  37. package/dist/chunk-O7ARI5CV.mjs +0 -31
  38. package/dist/chunk-O7ARI5CV.mjs.map +0 -1
  39. package/dist/chunk-OAF4ASJH.mjs +0 -214
  40. package/dist/chunk-OAF4ASJH.mjs.map +0 -1
  41. package/dist/chunk-ODAH4HNG.mjs +0 -49
  42. package/dist/chunk-ODAH4HNG.mjs.map +0 -1
  43. package/dist/chunk-QCUP6HLK.mjs +0 -37
  44. package/dist/chunk-QCUP6HLK.mjs.map +0 -1
  45. package/dist/chunk-SFPANIOY.mjs +0 -242
  46. package/dist/chunk-SFPANIOY.mjs.map +0 -1
  47. package/dist/chunk-SKC7AJIV.mjs +0 -61
  48. package/dist/chunk-SKC7AJIV.mjs.map +0 -1
  49. package/dist/chunk-STZUYPHJ.mjs +0 -87
  50. package/dist/chunk-STZUYPHJ.mjs.map +0 -1
  51. package/dist/chunk-VPTC36RZ.mjs +0 -23
  52. package/dist/chunk-VPTC36RZ.mjs.map +0 -1
  53. package/dist/chunk-VQDGBYWQ.mjs +0 -150
  54. package/dist/chunk-VQDGBYWQ.mjs.map +0 -1
  55. package/dist/chunk-XERJQUHA.mjs +0 -31
  56. package/dist/chunk-XERJQUHA.mjs.map +0 -1
  57. package/dist/chunk-XXR4QFAQ.mjs +0 -29
  58. package/dist/chunk-XXR4QFAQ.mjs.map +0 -1
  59. package/dist/chunk-YL6V3QTN.mjs +0 -453
  60. package/dist/chunk-YL6V3QTN.mjs.map +0 -1
  61. package/dist/chunk-YPSGKPDA.mjs +0 -1
  62. package/dist/chunk-YPSGKPDA.mjs.map +0 -1
  63. package/dist/components/copilot-provider/copilot-messages.d.ts +0 -8
  64. package/dist/components/copilot-provider/copilot-messages.js +0 -80
  65. package/dist/components/copilot-provider/copilot-messages.js.map +0 -1
  66. package/dist/components/copilot-provider/copilot-messages.mjs +0 -9
  67. package/dist/components/copilot-provider/copilot-messages.mjs.map +0 -1
  68. package/dist/components/copilot-provider/copilotkit-props.d.ts +0 -77
  69. package/dist/components/copilot-provider/copilotkit-props.js +0 -19
  70. package/dist/components/copilot-provider/copilotkit-props.js.map +0 -1
  71. package/dist/components/copilot-provider/copilotkit-props.mjs +0 -1
  72. package/dist/components/copilot-provider/copilotkit-props.mjs.map +0 -1
  73. package/dist/components/copilot-provider/copilotkit.d.ts +0 -10
  74. package/dist/components/copilot-provider/copilotkit.js +0 -961
  75. package/dist/components/copilot-provider/copilotkit.js.map +0 -1
  76. package/dist/components/copilot-provider/copilotkit.mjs +0 -22
  77. package/dist/components/copilot-provider/copilotkit.mjs.map +0 -1
  78. package/dist/components/copilot-provider/index.d.ts +0 -5
  79. package/dist/components/copilot-provider/index.js +0 -961
  80. package/dist/components/copilot-provider/index.js.map +0 -1
  81. package/dist/components/copilot-provider/index.mjs +0 -20
  82. package/dist/components/copilot-provider/index.mjs.map +0 -1
  83. package/dist/components/error-boundary/error-boundary.d.ts +0 -22
  84. package/dist/components/error-boundary/error-boundary.js +0 -183
  85. package/dist/components/error-boundary/error-boundary.js.map +0 -1
  86. package/dist/components/error-boundary/error-boundary.mjs +0 -12
  87. package/dist/components/error-boundary/error-boundary.mjs.map +0 -1
  88. package/dist/components/error-boundary/error-utils.d.ts +0 -11
  89. package/dist/components/error-boundary/error-utils.js +0 -177
  90. package/dist/components/error-boundary/error-utils.js.map +0 -1
  91. package/dist/components/error-boundary/error-utils.mjs +0 -13
  92. package/dist/components/error-boundary/error-utils.mjs.map +0 -1
  93. package/dist/components/index.d.ts +0 -5
  94. package/dist/components/index.js +0 -961
  95. package/dist/components/index.js.map +0 -1
  96. package/dist/components/index.mjs +0 -20
  97. package/dist/components/index.mjs.map +0 -1
  98. package/dist/components/toast/exclamation-mark-icon.d.ts +0 -9
  99. package/dist/components/toast/exclamation-mark-icon.js +0 -55
  100. package/dist/components/toast/exclamation-mark-icon.js.map +0 -1
  101. package/dist/components/toast/exclamation-mark-icon.mjs +0 -8
  102. package/dist/components/toast/exclamation-mark-icon.mjs.map +0 -1
  103. package/dist/components/toast/toast-provider.d.ts +0 -30
  104. package/dist/components/toast/toast-provider.js +0 -269
  105. package/dist/components/toast/toast-provider.js.map +0 -1
  106. package/dist/components/toast/toast-provider.mjs +0 -11
  107. package/dist/components/toast/toast-provider.mjs.map +0 -1
  108. package/dist/context/copilot-context.d.ts +0 -121
  109. package/dist/context/copilot-context.js +0 -138
  110. package/dist/context/copilot-context.js.map +0 -1
  111. package/dist/context/copilot-context.mjs +0 -10
  112. package/dist/context/copilot-context.mjs.map +0 -1
  113. package/dist/context/copilot-messages-context.d.ts +0 -15
  114. package/dist/context/copilot-messages-context.js +0 -57
  115. package/dist/context/copilot-messages-context.js.map +0 -1
  116. package/dist/context/copilot-messages-context.mjs +0 -10
  117. package/dist/context/copilot-messages-context.mjs.map +0 -1
  118. package/dist/context/index.d.ts +0 -11
  119. package/dist/context/index.js +0 -161
  120. package/dist/context/index.js.map +0 -1
  121. package/dist/context/index.mjs +0 -17
  122. package/dist/context/index.mjs.map +0 -1
  123. package/dist/hooks/index.d.ts +0 -20
  124. package/dist/hooks/index.js +0 -1228
  125. package/dist/hooks/index.js.map +0 -1
  126. package/dist/hooks/index.mjs +0 -52
  127. package/dist/hooks/index.mjs.map +0 -1
  128. package/dist/hooks/use-chat.d.ts +0 -120
  129. package/dist/hooks/use-chat.js +0 -598
  130. package/dist/hooks/use-chat.js.map +0 -1
  131. package/dist/hooks/use-chat.mjs +0 -12
  132. package/dist/hooks/use-chat.mjs.map +0 -1
  133. package/dist/hooks/use-coagent-state-render.d.ts +0 -60
  134. package/dist/hooks/use-coagent-state-render.js +0 -172
  135. package/dist/hooks/use-coagent-state-render.js.map +0 -1
  136. package/dist/hooks/use-coagent-state-render.mjs +0 -9
  137. package/dist/hooks/use-coagent-state-render.mjs.map +0 -1
  138. package/dist/hooks/use-coagent.d.ts +0 -199
  139. package/dist/hooks/use-coagent.js +0 -1044
  140. package/dist/hooks/use-coagent.js.map +0 -1
  141. package/dist/hooks/use-coagent.mjs +0 -29
  142. package/dist/hooks/use-coagent.mjs.map +0 -1
  143. package/dist/hooks/use-copilot-action.d.ts +0 -140
  144. package/dist/hooks/use-copilot-action.js +0 -369
  145. package/dist/hooks/use-copilot-action.js.map +0 -1
  146. package/dist/hooks/use-copilot-action.mjs +0 -11
  147. package/dist/hooks/use-copilot-action.mjs.map +0 -1
  148. package/dist/hooks/use-copilot-chat.d.ts +0 -38
  149. package/dist/hooks/use-copilot-chat.js +0 -917
  150. package/dist/hooks/use-copilot-chat.js.map +0 -1
  151. package/dist/hooks/use-copilot-chat.mjs +0 -24
  152. package/dist/hooks/use-copilot-chat.mjs.map +0 -1
  153. package/dist/hooks/use-copilot-readable.d.ts +0 -33
  154. package/dist/hooks/use-copilot-readable.js +0 -158
  155. package/dist/hooks/use-copilot-readable.js.map +0 -1
  156. package/dist/hooks/use-copilot-readable.mjs +0 -9
  157. package/dist/hooks/use-copilot-readable.mjs.map +0 -1
  158. package/dist/hooks/use-copilot-runtime-client.d.ts +0 -5
  159. package/dist/hooks/use-copilot-runtime-client.js +0 -182
  160. package/dist/hooks/use-copilot-runtime-client.js.map +0 -1
  161. package/dist/hooks/use-copilot-runtime-client.mjs +0 -10
  162. package/dist/hooks/use-copilot-runtime-client.mjs.map +0 -1
  163. package/dist/hooks/use-flat-category-store.d.ts +0 -9
  164. package/dist/hooks/use-flat-category-store.js +0 -90
  165. package/dist/hooks/use-flat-category-store.js.map +0 -1
  166. package/dist/hooks/use-flat-category-store.mjs +0 -8
  167. package/dist/hooks/use-flat-category-store.mjs.map +0 -1
  168. package/dist/hooks/use-make-copilot-document-readable.d.ts +0 -12
  169. package/dist/hooks/use-make-copilot-document-readable.js +0 -153
  170. package/dist/hooks/use-make-copilot-document-readable.js.map +0 -1
  171. package/dist/hooks/use-make-copilot-document-readable.mjs +0 -9
  172. package/dist/hooks/use-make-copilot-document-readable.mjs.map +0 -1
  173. package/dist/hooks/use-tree.d.ts +0 -18
  174. package/dist/hooks/use-tree.js +0 -172
  175. package/dist/hooks/use-tree.js.map +0 -1
  176. package/dist/hooks/use-tree.mjs +0 -8
  177. package/dist/hooks/use-tree.mjs.map +0 -1
  178. package/dist/index.d.ts +0 -26
  179. package/dist/index.js +0 -2122
  180. package/dist/index.js.map +0 -1
  181. package/dist/index.mjs +0 -79
  182. package/dist/index.mjs.map +0 -1
  183. package/dist/lib/copilot-task.d.ts +0 -97
  184. package/dist/lib/copilot-task.js +0 -195
  185. package/dist/lib/copilot-task.js.map +0 -1
  186. package/dist/lib/copilot-task.mjs +0 -20
  187. package/dist/lib/copilot-task.mjs.map +0 -1
  188. package/dist/lib/index.d.ts +0 -11
  189. package/dist/lib/index.js +0 -197
  190. package/dist/lib/index.js.map +0 -1
  191. package/dist/lib/index.mjs +0 -21
  192. package/dist/lib/index.mjs.map +0 -1
  193. package/dist/types/chat-suggestion-configuration.d.ts +0 -22
  194. package/dist/types/chat-suggestion-configuration.js +0 -19
  195. package/dist/types/chat-suggestion-configuration.js.map +0 -1
  196. package/dist/types/chat-suggestion-configuration.mjs +0 -1
  197. package/dist/types/chat-suggestion-configuration.mjs.map +0 -1
  198. package/dist/types/coagent-action.d.ts +0 -29
  199. package/dist/types/coagent-action.js +0 -19
  200. package/dist/types/coagent-action.js.map +0 -1
  201. package/dist/types/coagent-action.mjs +0 -1
  202. package/dist/types/coagent-action.mjs.map +0 -1
  203. package/dist/types/coagent-state.d.ts +0 -11
  204. package/dist/types/coagent-state.js +0 -19
  205. package/dist/types/coagent-state.js.map +0 -1
  206. package/dist/types/coagent-state.mjs +0 -1
  207. package/dist/types/coagent-state.mjs.map +0 -1
  208. package/dist/types/document-pointer.d.ts +0 -9
  209. package/dist/types/document-pointer.js +0 -19
  210. package/dist/types/document-pointer.js.map +0 -1
  211. package/dist/types/document-pointer.mjs +0 -1
  212. package/dist/types/document-pointer.mjs.map +0 -1
  213. package/dist/types/frontend-action.d.ts +0 -126
  214. package/dist/types/frontend-action.js +0 -53
  215. package/dist/types/frontend-action.js.map +0 -1
  216. package/dist/types/frontend-action.mjs +0 -8
  217. package/dist/types/frontend-action.mjs.map +0 -1
  218. package/dist/types/index.d.ts +0 -7
  219. package/dist/types/index.js +0 -19
  220. package/dist/types/index.js.map +0 -1
  221. package/dist/types/index.mjs +0 -2
  222. package/dist/types/index.mjs.map +0 -1
  223. package/dist/types/system-message.d.ts +0 -3
  224. package/dist/types/system-message.js +0 -19
  225. package/dist/types/system-message.js.map +0 -1
  226. package/dist/types/system-message.mjs +0 -1
  227. package/dist/types/system-message.mjs.map +0 -1
  228. package/dist/utils/dev-console.d.ts +0 -3
  229. package/dist/utils/dev-console.js +0 -42
  230. package/dist/utils/dev-console.js.map +0 -1
  231. package/dist/utils/dev-console.mjs +0 -8
  232. package/dist/utils/dev-console.mjs.map +0 -1
  233. package/dist/utils/extract.d.ts +0 -43
  234. package/dist/utils/extract.js +0 -208
  235. package/dist/utils/extract.js.map +0 -1
  236. package/dist/utils/extract.mjs +0 -18
  237. package/dist/utils/extract.mjs.map +0 -1
  238. package/dist/utils/index.d.ts +0 -13
  239. package/dist/utils/index.js +0 -226
  240. package/dist/utils/index.js.map +0 -1
  241. package/dist/utils/index.mjs +0 -21
  242. package/dist/utils/index.mjs.map +0 -1
  243. package/dist/utils/utils.d.ts +0 -2
  244. package/dist/utils/utils.js +0 -19
  245. package/dist/utils/utils.js.map +0 -1
  246. package/dist/utils/utils.mjs +0 -1
  247. package/dist/utils/utils.mjs.map +0 -1
  248. package/dist/utils/utils.test.d.ts +0 -2
  249. package/dist/utils/utils.test.js +0 -9
  250. package/dist/utils/utils.test.js.map +0 -1
  251. package/dist/utils/utils.test.mjs +0 -7
  252. package/dist/utils/utils.test.mjs.map +0 -1
package/CHANGELOG.md CHANGED
@@ -1,14 +1,75 @@
1
1
  # ui
2
2
 
3
- ## 1.5.1-test-custom-tag-prerelease-1.0
3
+ ## 1.5.2-next.0
4
4
 
5
5
  ### Patch Changes
6
6
 
7
- - 896c825: testing a prerelease
7
+ - Updated dependencies [b0192c1]
8
+ - @copilotkit/runtime-client-gql@1.5.2-next.0
9
+ - @copilotkit/shared@1.5.2-next.0
10
+
11
+ ## 1.5.1
12
+
13
+ ### Patch Changes
14
+
15
+ - 5c01e9e: test prerelease #4
16
+ - ed39d40: - [CPK-1034] adds `useCopilotAuthenticatedAction`
17
+ - da280ed: Test prerelease script
18
+ - 27e42d7: testing a prerelease
19
+ - 05240a9: test pre #2
20
+ - 33218fe: test prerelease #3
21
+ - 03f3d6f: Test next prerelease
22
+ - 649ebcc: - fix: add warning when using agents that are not available on agent related hooks
23
+ - 6dfa0d2: - feat: add temperature parameter support for LLM completions
24
+ - Updated dependencies [5c01e9e]
25
+ - Updated dependencies [da280ed]
26
+ - Updated dependencies [27e42d7]
27
+ - Updated dependencies [05240a9]
28
+ - Updated dependencies [33218fe]
29
+ - Updated dependencies [03f3d6f]
30
+ - Updated dependencies [649ebcc]
31
+ - @copilotkit/runtime-client-gql@1.5.1
32
+ - @copilotkit/shared@1.5.1
33
+
34
+ ## 1.5.1-next.3
35
+
36
+ ### Patch Changes
37
+
38
+ - 33218fe: test prerelease #3
39
+ - Updated dependencies [33218fe]
40
+ - @copilotkit/runtime-client-gql@1.5.1-next.3
41
+ - @copilotkit/shared@1.5.1-next.3
42
+
43
+ ## 1.5.1-next.2
44
+
45
+ ### Patch Changes
46
+
47
+ - ed39d40: - [CPK-1034] adds `useCopilotAuthenticatedAction`
48
+ - da280ed: Test prerelease script
49
+ - 649ebcc: - fix: add warning when using agents that are not available on agent related hooks
50
+ - Updated dependencies [da280ed]
51
+ - Updated dependencies [649ebcc]
52
+ - @copilotkit/runtime-client-gql@1.5.1-next.2
53
+ - @copilotkit/shared@1.5.1-next.2
54
+
55
+ ## 1.5.1-next.1
56
+
57
+ ### Patch Changes
58
+
59
+ - 03f3d6f: Test next prerelease
60
+ - Updated dependencies [03f3d6f]
61
+ - @copilotkit/runtime-client-gql@1.5.1-next.1
62
+ - @copilotkit/shared@1.5.1-next.1
63
+
64
+ ## 1.5.1-next.0
65
+
66
+ ### Patch Changes
67
+
68
+ - 27e42d7: testing a prerelease
8
69
  - 6dfa0d2: - feat: add temperature parameter support for LLM completions
9
- - Updated dependencies [896c825]
10
- - @copilotkit/runtime-client-gql@1.5.1-test-custom-tag-prerelease-1.0
11
- - @copilotkit/shared@1.5.1-test-custom-tag-prerelease-1.0
70
+ - Updated dependencies [27e42d7]
71
+ - @copilotkit/runtime-client-gql@1.5.1-next.0
72
+ - @copilotkit/shared@1.5.1-next.0
12
73
 
13
74
  ## 1.5.0
14
75
 
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "publishConfig": {
10
10
  "access": "public"
11
11
  },
12
- "version": "1.5.1-test-custom-tag-prerelease-1.0",
12
+ "version": "1.5.2-next.0",
13
13
  "sideEffects": false,
14
14
  "main": "./dist/index.js",
15
15
  "module": "./dist/index.mjs",
@@ -42,8 +42,8 @@
42
42
  "dependencies": {
43
43
  "@scarf/scarf": "^1.3.0",
44
44
  "untruncate-json": "^0.0.1",
45
- "@copilotkit/runtime-client-gql": "1.5.1-test-custom-tag-prerelease-1.0",
46
- "@copilotkit/shared": "1.5.1-test-custom-tag-prerelease-1.0"
45
+ "@copilotkit/runtime-client-gql": "1.5.2-next.0",
46
+ "@copilotkit/shared": "1.5.2-next.0"
47
47
  },
48
48
  "keywords": [
49
49
  "copilotkit",
@@ -1,5 +1,6 @@
1
1
  import { ForwardedParametersInput } from "@copilotkit/runtime-client-gql";
2
2
  import { ReactNode } from "react";
3
+ import { AuthState } from "../../context/copilot-context";
3
4
 
4
5
  /**
5
6
  * Props for CopilotKit.
@@ -84,4 +85,13 @@ export interface CopilotKitProps {
84
85
  * The forwarded parameters to use for the task.
85
86
  */
86
87
  forwardedParameters?: Pick<ForwardedParametersInput, "temperature">;
88
+
89
+ /**
90
+ * The auth config to use for the CopilotKit.
91
+ */
92
+ authConfig?: {
93
+ SignInComponent: React.ComponentType<{
94
+ onSignInComplete: (authState: AuthState) => void;
95
+ }>;
96
+ };
87
97
  }
@@ -14,12 +14,13 @@
14
14
  * ```
15
15
  */
16
16
 
17
- import { useCallback, useMemo, useRef, useState } from "react";
17
+ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
18
18
  import {
19
19
  CopilotContext,
20
20
  CopilotApiConfig,
21
21
  ChatComponentsCache,
22
22
  AgentSession,
23
+ AuthState,
23
24
  } from "../../context/copilot-context";
24
25
  import useTree from "../../hooks/use-tree";
25
26
  import { CopilotChatSuggestionConfiguration, DocumentPointer } from "../../types";
@@ -41,6 +42,7 @@ import { ToastProvider } from "../toast/toast-provider";
41
42
  import { useCopilotRuntimeClient } from "../../hooks/use-copilot-runtime-client";
42
43
  import { shouldShowDevConsole } from "../../utils";
43
44
  import { CopilotErrorBoundary } from "../error-boundary/error-boundary";
45
+ import { Agent } from "@copilotkit/runtime-client-gql";
44
46
 
45
47
  export function CopilotKit({ children, ...props }: CopilotKitProps) {
46
48
  const showDevConsole = props.showDevConsole === undefined ? "auto" : props.showDevConsole;
@@ -77,6 +79,7 @@ export function CopilotKitInternal({ children, ...props }: CopilotKitProps) {
77
79
  const { addElement, removeElement, printTree } = useTree();
78
80
  const [isLoading, setIsLoading] = useState(false);
79
81
  const [chatInstructions, setChatInstructions] = useState("");
82
+ const [authStates, setAuthStates] = useState<Record<string, AuthState>>({});
80
83
 
81
84
  const {
82
85
  addElement: addDocument,
@@ -224,12 +227,31 @@ export function CopilotKitInternal({ children, ...props }: CopilotKitProps) {
224
227
  props.cloudRestrictToTopic,
225
228
  ]);
226
229
 
227
- const headers = {
228
- ...(copilotApiConfig.headers || {}),
229
- ...(copilotApiConfig.publicApiKey
230
- ? { [COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: copilotApiConfig.publicApiKey }
231
- : {}),
232
- };
230
+ const headers = useMemo(() => {
231
+ const authHeaders = Object.values(authStates || {}).reduce((acc, state) => {
232
+ if (state.status === "authenticated" && state.authHeaders) {
233
+ return {
234
+ ...acc,
235
+ ...Object.entries(state.authHeaders).reduce(
236
+ (headers, [key, value]) => ({
237
+ ...headers,
238
+ [key.startsWith("X-Custom-") ? key : `X-Custom-${key}`]: value,
239
+ }),
240
+ {},
241
+ ),
242
+ };
243
+ }
244
+ return acc;
245
+ }, {});
246
+
247
+ return {
248
+ ...(copilotApiConfig.headers || {}),
249
+ ...(copilotApiConfig.publicApiKey
250
+ ? { [COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: copilotApiConfig.publicApiKey }
251
+ : {}),
252
+ ...authHeaders,
253
+ };
254
+ }, [copilotApiConfig.headers, copilotApiConfig.publicApiKey, authStates]);
233
255
 
234
256
  const runtimeClient = useCopilotRuntimeClient({
235
257
  url: copilotApiConfig.chatApiEndpoint,
@@ -256,6 +278,7 @@ export function CopilotKitInternal({ children, ...props }: CopilotKitProps) {
256
278
  });
257
279
  };
258
280
 
281
+ const [availableAgents, setAvailableAgents] = useState<Agent[]>([]);
259
282
  const [coagentStates, setCoagentStates] = useState<Record<string, CoagentState>>({});
260
283
  const coagentStatesRef = useRef<Record<string, CoagentState>>({});
261
284
  const setCoagentStatesWithRef = useCallback(
@@ -273,6 +296,16 @@ export function CopilotKitInternal({ children, ...props }: CopilotKitProps) {
273
296
  [],
274
297
  );
275
298
 
299
+ useEffect(() => {
300
+ const fetchData = async () => {
301
+ const result = await runtimeClient.availableAgents();
302
+ if (result.data?.availableAgents) {
303
+ setAvailableAgents(result.data.availableAgents.agents);
304
+ }
305
+ };
306
+ void fetchData();
307
+ }, []);
308
+
276
309
  let initialAgentSession: AgentSession | null = null;
277
310
  if (props.agent) {
278
311
  initialAgentSession = {
@@ -328,6 +361,10 @@ export function CopilotKitInternal({ children, ...props }: CopilotKitProps) {
328
361
  runId,
329
362
  setRunId,
330
363
  chatAbortControllerRef,
364
+ availableAgents,
365
+ authConfig: props.authConfig,
366
+ authStates,
367
+ setAuthStates,
331
368
  }}
332
369
  >
333
370
  <CopilotMessages>{children}</CopilotMessages>
@@ -141,7 +141,7 @@ function Toast({
141
141
  style={{
142
142
  backgroundColor: bgColors[type],
143
143
  color: "white",
144
- padding: "0.5rem 1rem",
144
+ padding: "0.5rem 1.5rem",
145
145
  borderRadius: "0.25rem",
146
146
  boxShadow: "0 2px 4px rgba(0,0,0,0.1)",
147
147
  position: "relative",
@@ -11,6 +11,7 @@ import { CopilotChatSuggestionConfiguration } from "../types/chat-suggestion-con
11
11
  import { CoAgentStateRender, CoAgentStateRenderProps } from "../types/coagent-action";
12
12
  import { CoagentState } from "../types/coagent-state";
13
13
  import { CopilotRuntimeClient, ForwardedParametersInput } from "@copilotkit/runtime-client-gql";
14
+ import { Agent } from "@copilotkit/runtime-client-gql";
14
15
 
15
16
  /**
16
17
  * Interface for the configuration of the Copilot API.
@@ -90,6 +91,15 @@ export interface AgentSession {
90
91
  nodeName?: string;
91
92
  }
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
+
93
103
  export interface CopilotContextParams {
94
104
  // function-calling
95
105
  actions: Record<string, FrontendAction<any>>;
@@ -167,6 +177,22 @@ export interface CopilotContextParams {
167
177
  * The forwarded parameters to use for the task.
168
178
  */
169
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
+ };
170
196
  }
171
197
 
172
198
  const emptyCopilotContext: CopilotContextParams = {
@@ -227,6 +253,7 @@ const emptyCopilotContext: CopilotContextParams = {
227
253
  runId: null,
228
254
  setRunId: () => {},
229
255
  chatAbortControllerRef: { current: null },
256
+ availableAgents: [],
230
257
  };
231
258
 
232
259
  export const CopilotContext = React.createContext<CopilotContextParams>(emptyCopilotContext);
@@ -239,7 +266,6 @@ export function useCopilotContext(): CopilotContextParams {
239
266
  return context;
240
267
  }
241
268
 
242
- function returnAndThrowInDebug<T>(value: T): T {
269
+ function returnAndThrowInDebug<T>(_value: T): T {
243
270
  throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
244
- return value;
245
271
  }
@@ -9,3 +9,4 @@ export { type UseChatHelpers } from "./use-chat";
9
9
  export { useCopilotReadable } from "./use-copilot-readable";
10
10
  export { useCoAgent, type HintFunction, runAgent, startAgent, stopAgent } from "./use-coagent";
11
11
  export { useCopilotRuntimeClient } from "./use-copilot-runtime-client";
12
+ export { useCopilotAuthenticatedAction } from "./use-copilot-authenticated-action";
@@ -2,8 +2,8 @@ import { useRef } from "react";
2
2
  import {
3
3
  FunctionCallHandler,
4
4
  COPILOT_CLOUD_PUBLIC_API_KEY_HEADER,
5
- actionParametersToJsonSchema,
6
5
  CoAgentStateRenderHandler,
6
+ randomId,
7
7
  } from "@copilotkit/shared";
8
8
  import {
9
9
  Message,
@@ -17,7 +17,6 @@ import {
17
17
  MessageRole,
18
18
  Role,
19
19
  CopilotRequestType,
20
- ActionInputAvailability,
21
20
  ForwardedParametersInput,
22
21
  loadMessagesFromJsonRepresentation,
23
22
  } from "@copilotkit/runtime-client-gql";
@@ -26,7 +25,6 @@ import { CopilotApiConfig } from "../context";
26
25
  import { FrontendAction, processActionsForRuntimeRequest } from "../types/frontend-action";
27
26
  import { CoagentState } from "../types/coagent-state";
28
27
  import { AgentSession } from "../context/copilot-context";
29
- import { useToast } from "../components/toast/toast-provider";
30
28
  import { useCopilotRuntimeClient } from "./use-copilot-runtime-client";
31
29
  import { useAsyncCallback } from "../components/error-boundary/error-utils";
32
30
 
@@ -126,6 +124,10 @@ export type UseChatOptions = {
126
124
  * The global chat abort controller.
127
125
  */
128
126
  chatAbortControllerRef: React.MutableRefObject<AbortController | null>;
127
+ /**
128
+ * The agent lock.
129
+ */
130
+ agentLock: string | null;
129
131
  };
130
132
 
131
133
  export type UseChatHelpers = {
@@ -173,8 +175,8 @@ export function useChat(options: UseChatOptions): UseChatHelpers {
173
175
  runId,
174
176
  setRunId,
175
177
  chatAbortControllerRef,
178
+ agentLock,
176
179
  } = options;
177
- const { addGraphQLErrorsToast } = useToast();
178
180
  const runChatCompletionRef = useRef<(previousMessages: Message[]) => Promise<Message[]>>();
179
181
  // We need to keep a ref of coagent states and session because of renderAndWait - making sure
180
182
  // the latest state is sent to the API
@@ -393,7 +395,15 @@ export function useChat(options: UseChatOptions): UseChatHelpers {
393
395
  nodeName: lastAgentStateMessage.nodeName,
394
396
  });
395
397
  } else {
396
- setAgentSession(null);
398
+ if (agentLock) {
399
+ setAgentSession({
400
+ threadId: randomId(),
401
+ agentName: agentLock,
402
+ nodeName: undefined,
403
+ });
404
+ } else {
405
+ setAgentSession(null);
406
+ }
397
407
  }
398
408
  }
399
409
  }
@@ -50,6 +50,7 @@ import { useRef, useContext, useEffect } from "react";
50
50
  import { CopilotContext } from "../context/copilot-context";
51
51
  import { randomId } from "@copilotkit/shared";
52
52
  import { CoAgentStateRender } from "../types/coagent-action";
53
+ import { useToast } from "../components/toast/toast-provider";
53
54
 
54
55
  /**
55
56
  * This hook is used to render agent state with custom UI components or text. This is particularly
@@ -71,8 +72,18 @@ export function useCoAgentStateRender<T = any>(
71
72
  removeCoAgentStateRender,
72
73
  coAgentStateRenders,
73
74
  chatComponentsCache,
75
+ availableAgents,
74
76
  } = useContext(CopilotContext);
75
77
  const idRef = useRef<string>(randomId());
78
+ const { addToast } = useToast();
79
+
80
+ useEffect(() => {
81
+ if (availableAgents?.length && !availableAgents.some((a) => a.name === action.name)) {
82
+ const message = `(useCoAgentStateRender): Agent "${action.name}" not found. Make sure the agent exists and is properly configured.`;
83
+ console.warn(message);
84
+ addToast({ type: "warning", message });
85
+ }
86
+ }, [availableAgents]);
76
87
 
77
88
  const key = `${action.name}-${action.nodeName || "global"}`;
78
89
 
@@ -100,6 +100,7 @@ import { useCopilotChat } from "./use-copilot-chat";
100
100
  import { Message } from "@copilotkit/runtime-client-gql";
101
101
  import { flushSync } from "react-dom";
102
102
  import { useAsyncCallback } from "../components/error-boundary/error-utils";
103
+ import { useToast } from "../components/toast/toast-provider";
103
104
 
104
105
  interface WithInternalStateManagementAndInitial<T> {
105
106
  /**
@@ -203,6 +204,9 @@ export type HintFunction = (params: HintFunctionParams) => Message | undefined;
203
204
  * we refer to as CoAgents, checkout the documentation at https://docs.copilotkit.ai/coagents/quickstart.
204
205
  */
205
206
  export function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentReturnType<T> {
207
+ const generalContext = useCopilotContext();
208
+ const { availableAgents } = generalContext;
209
+ const { addToast } = useToast();
206
210
  const isExternalStateManagement = (
207
211
  options: UseCoagentOptions<T>,
208
212
  ): options is WithExternalStateManagement<T> => {
@@ -210,6 +214,13 @@ export function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentRe
210
214
  };
211
215
 
212
216
  const { name } = options;
217
+ useEffect(() => {
218
+ if (availableAgents?.length && !availableAgents.some((a) => a.name === name)) {
219
+ const message = `(useCoAgent): Agent "${name}" not found. Make sure the agent exists and is properly configured.`;
220
+ console.warn(message);
221
+ addToast({ type: "warning", message });
222
+ }
223
+ }, [availableAgents]);
213
224
 
214
225
  const isInternalStateManagementWithInitial = (
215
226
  options: UseCoagentOptions<T>,
@@ -217,7 +228,6 @@ export function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentRe
217
228
  return "initialState" in options;
218
229
  };
219
230
 
220
- const generalContext = useCopilotContext();
221
231
  const messagesContext = useCopilotMessagesContext();
222
232
  const context = { ...generalContext, ...messagesContext };
223
233
  const { coagentStates, coagentStatesRef, setCoagentStatesWithRef } = context;
@@ -0,0 +1,60 @@
1
+ import { Parameter } from "@copilotkit/shared";
2
+ import { Fragment, useCallback, useRef } from "react";
3
+ import { useCopilotContext } from "../context/copilot-context";
4
+ import { FrontendAction, ActionRenderProps } from "../types/frontend-action";
5
+ import { useCopilotAction } from "./use-copilot-action";
6
+ import React from "react";
7
+
8
+ export function useCopilotAuthenticatedAction<T extends Parameter[]>(
9
+ action: FrontendAction<T>,
10
+ dependencies?: any[],
11
+ ): void {
12
+ const { authConfig, authStates, setAuthStates } = useCopilotContext();
13
+ const pendingActionRef = useRef<ActionRenderProps<Parameter[]> | null>(null);
14
+
15
+ const executeAction = useCallback(
16
+ (props: ActionRenderProps<Parameter[]>) => {
17
+ if (typeof action.render === "function") {
18
+ return action.render(props);
19
+ }
20
+ return action.render || React.createElement(Fragment);
21
+ },
22
+ [action],
23
+ );
24
+
25
+ const wrappedRender = useCallback(
26
+ (props: ActionRenderProps<Parameter[]>): string | React.ReactElement => {
27
+ const isAuthenticated = Object.values(authStates || {}).some(
28
+ (state) => state.status === "authenticated",
29
+ );
30
+
31
+ if (!isAuthenticated) {
32
+ // Store action details for later execution
33
+ pendingActionRef.current = props;
34
+
35
+ return authConfig?.SignInComponent
36
+ ? React.createElement(authConfig.SignInComponent, {
37
+ onSignInComplete: (authState) => {
38
+ setAuthStates?.((prev) => ({ ...prev, [action.name]: authState }));
39
+ if (pendingActionRef.current) {
40
+ executeAction(pendingActionRef.current);
41
+ pendingActionRef.current = null;
42
+ }
43
+ },
44
+ })
45
+ : React.createElement(Fragment);
46
+ }
47
+
48
+ return executeAction(props);
49
+ },
50
+ [action, authStates, setAuthStates],
51
+ );
52
+
53
+ useCopilotAction(
54
+ {
55
+ ...action,
56
+ render: wrappedRender,
57
+ } as FrontendAction<T>,
58
+ dependencies,
59
+ );
60
+ }
@@ -172,6 +172,7 @@ export function useCopilotChat({
172
172
  runId,
173
173
  setRunId,
174
174
  chatAbortControllerRef,
175
+ agentLock,
175
176
  });
176
177
 
177
178
  // this is a workaround born out of a bug that Athena incessantly ran into.
@@ -1,70 +0,0 @@
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
@@ -1 +0,0 @@
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":[]}
@@ -1,28 +0,0 @@
1
- import {
2
- useCopilotContext
3
- } from "./chunk-STZUYPHJ.mjs";
4
-
5
- // src/hooks/use-copilot-readable.ts
6
- import { useEffect, useRef } from "react";
7
- function convertToJSON(description, value) {
8
- return `${description}: ${typeof value === "string" ? value : JSON.stringify(value)}`;
9
- }
10
- function useCopilotReadable({ description, value, parentId, categories, convert }, dependencies) {
11
- const { addContext, removeContext } = useCopilotContext();
12
- const idRef = useRef();
13
- convert = convert || convertToJSON;
14
- const information = convert(description, value);
15
- useEffect(() => {
16
- const id = addContext(information, parentId, categories);
17
- idRef.current = id;
18
- return () => {
19
- removeContext(id);
20
- };
21
- }, [information, parentId, addContext, removeContext, ...dependencies || []]);
22
- return idRef.current;
23
- }
24
-
25
- export {
26
- useCopilotReadable
27
- };
28
- //# sourceMappingURL=chunk-6KHA5W5R.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/hooks/use-copilot-readable.ts"],"sourcesContent":["/**\n * `useCopilotReadable` is a React hook that provides app-state and other information\n * to the Copilot. Optionally, the hook can also handle hierarchical state within your\n * application, passing these parent-child relationships to the Copilot.\n *\n * ## Usage\n *\n * ### Simple Usage\n *\n * In its most basic usage, useCopilotReadable accepts a single string argument\n * representing any piece of app state, making it available for the Copilot to use\n * as context when responding to user input.\n *\n * ```tsx\n * import { useCopilotReadable } from \"@copilotkit/react-core\";\n *\n * export function MyComponent() {\n * const [employees, setEmployees] = useState([]);\n *\n * useCopilotReadable({\n * description: \"The list of employees\",\n * value: employees,\n * });\n * }\n * ```\n *\n * ### Nested Components\n *\n * Optionally, you can maintain the hierarchical structure of information by passing\n * `parentId`. This allows you to use `useCopilotReadable` in nested components:\n *\n * ```tsx /employeeContextId/1 {17,23}\n * import { useCopilotReadable } from \"@copilotkit/react-core\";\n *\n * function Employee(props: EmployeeProps) {\n * const { employeeName, workProfile, metadata } = props;\n *\n * // propagate any information to copilot\n * const employeeContextId = useCopilotReadable({\n * description: \"Employee name\",\n * value: employeeName\n * });\n *\n * // Pass a parentID to maintain a hierarchical structure.\n * // Especially useful with child React components, list elements, etc.\n * useCopilotReadable({\n * description: \"Work profile\",\n * value: workProfile.description(),\n * parentId: employeeContextId\n * });\n *\n * useCopilotReadable({\n * description: \"Employee metadata\",\n * value: metadata.description(),\n * parentId: employeeContextId\n * });\n *\n * return (\n * // Render as usual...\n * );\n * }\n * ```\n */\nimport { useEffect, useRef } from \"react\";\nimport { useCopilotContext } from \"../context/copilot-context\";\n\n/**\n * Options for the useCopilotReadable hook.\n */\nexport interface UseCopilotReadableOptions {\n /**\n * The description of the information to be added to the Copilot context.\n */\n description: string;\n /**\n * The value to be added to the Copilot context. Object values are automatically stringified.\n */\n value: any;\n /**\n * The ID of the parent context, if any.\n */\n parentId?: string;\n /**\n * An array of categories to control which context are visible where. Particularly useful\n * with CopilotTextarea (see `useMakeAutosuggestionFunction`)\n */\n categories?: string[];\n\n /**\n * A custom conversion function to use to serialize the value to a string. If not provided, the value\n * will be serialized using `JSON.stringify`.\n */\n convert?: (description: string, value: any) => string;\n}\n\nfunction convertToJSON(description: string, value: any): string {\n return `${description}: ${typeof value === \"string\" ? value : JSON.stringify(value)}`;\n}\n\n/**\n * Adds the given information to the Copilot context to make it readable by Copilot.\n */\nexport function useCopilotReadable(\n { description, value, parentId, categories, convert }: UseCopilotReadableOptions,\n dependencies?: any[],\n): string | undefined {\n const { addContext, removeContext } = useCopilotContext();\n const idRef = useRef<string>();\n convert = convert || convertToJSON;\n\n const information = convert(description, value);\n\n useEffect(() => {\n const id = addContext(information, parentId, categories);\n idRef.current = id;\n\n return () => {\n removeContext(id);\n };\n }, [information, parentId, addContext, removeContext, ...(dependencies || [])]);\n\n return idRef.current;\n}\n"],"mappings":";;;;;AA+DA,SAAS,WAAW,cAAc;AAgClC,SAAS,cAAc,aAAqB,OAAoB;AAC9D,SAAO,GAAG,gBAAgB,OAAO,UAAU,WAAW,QAAQ,KAAK,UAAU,KAAK;AACpF;AAKO,SAAS,mBACd,EAAE,aAAa,OAAO,UAAU,YAAY,QAAQ,GACpD,cACoB;AACpB,QAAM,EAAE,YAAY,cAAc,IAAI,kBAAkB;AACxD,QAAM,QAAQ,OAAe;AAC7B,YAAU,WAAW;AAErB,QAAM,cAAc,QAAQ,aAAa,KAAK;AAE9C,YAAU,MAAM;AACd,UAAM,KAAK,WAAW,aAAa,UAAU,UAAU;AACvD,UAAM,UAAU;AAEhB,WAAO,MAAM;AACX,oBAAc,EAAE;AAAA,IAClB;AAAA,EACF,GAAG,CAAC,aAAa,UAAU,YAAY,eAAe,GAAI,gBAAgB,CAAC,CAAE,CAAC;AAE9E,SAAO,MAAM;AACf;","names":[]}