@copilotkit/react-core 1.5.9 → 1.5.10

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 (249) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/dist/chunk-22XKTBPF.mjs +61 -0
  3. package/dist/chunk-22XKTBPF.mjs.map +1 -0
  4. package/dist/chunk-5FHSUKQL.mjs +70 -0
  5. package/dist/chunk-5FHSUKQL.mjs.map +1 -0
  6. package/dist/chunk-6BQVZFNO.mjs +35 -0
  7. package/dist/chunk-6BQVZFNO.mjs.map +1 -0
  8. package/dist/chunk-6U3UH3KO.mjs +140 -0
  9. package/dist/chunk-6U3UH3KO.mjs.map +1 -0
  10. package/dist/chunk-DCTJZ742.mjs +22 -0
  11. package/dist/chunk-DCTJZ742.mjs.map +1 -0
  12. package/dist/chunk-EFZPSZWO.mjs +1 -0
  13. package/dist/chunk-EFZPSZWO.mjs.map +1 -0
  14. package/dist/chunk-EWKJMQOF.mjs +107 -0
  15. package/dist/chunk-EWKJMQOF.mjs.map +1 -0
  16. package/dist/chunk-FQ53HQYZ.mjs +28 -0
  17. package/dist/chunk-FQ53HQYZ.mjs.map +1 -0
  18. package/dist/chunk-HIUT2NLX.mjs +23 -0
  19. package/dist/chunk-HIUT2NLX.mjs.map +1 -0
  20. package/dist/chunk-KXH7NSOV.mjs +215 -0
  21. package/dist/chunk-KXH7NSOV.mjs.map +1 -0
  22. package/dist/chunk-LHKIIAUQ.mjs +59 -0
  23. package/dist/chunk-LHKIIAUQ.mjs.map +1 -0
  24. package/dist/chunk-LNI2Z45S.mjs +242 -0
  25. package/dist/chunk-LNI2Z45S.mjs.map +1 -0
  26. package/dist/chunk-LUGEI4YQ.mjs +1 -0
  27. package/dist/chunk-LUGEI4YQ.mjs.map +1 -0
  28. package/dist/chunk-LZWH4OG5.mjs +482 -0
  29. package/dist/chunk-LZWH4OG5.mjs.map +1 -0
  30. package/dist/chunk-M2NT7NOO.mjs +120 -0
  31. package/dist/chunk-M2NT7NOO.mjs.map +1 -0
  32. package/dist/chunk-MLAS4QUR.mjs +18 -0
  33. package/dist/chunk-MLAS4QUR.mjs.map +1 -0
  34. package/dist/chunk-NR2BTA6Z.mjs +1 -0
  35. package/dist/chunk-NR2BTA6Z.mjs.map +1 -0
  36. package/dist/chunk-O7ARI5CV.mjs +31 -0
  37. package/dist/chunk-O7ARI5CV.mjs.map +1 -0
  38. package/dist/chunk-SKC7AJIV.mjs +61 -0
  39. package/dist/chunk-SKC7AJIV.mjs.map +1 -0
  40. package/dist/chunk-VGOFRULA.mjs +401 -0
  41. package/dist/chunk-VGOFRULA.mjs.map +1 -0
  42. package/dist/chunk-VNTDIJSG.mjs +37 -0
  43. package/dist/chunk-VNTDIJSG.mjs.map +1 -0
  44. package/dist/chunk-WXREY7UP.mjs +87 -0
  45. package/dist/chunk-WXREY7UP.mjs.map +1 -0
  46. package/dist/chunk-XERJQUHA.mjs +31 -0
  47. package/dist/chunk-XERJQUHA.mjs.map +1 -0
  48. package/dist/chunk-XXR4QFAQ.mjs +29 -0
  49. package/dist/chunk-XXR4QFAQ.mjs.map +1 -0
  50. package/dist/chunk-YMG27BON.mjs +160 -0
  51. package/dist/chunk-YMG27BON.mjs.map +1 -0
  52. package/dist/chunk-YPSGKPDA.mjs +1 -0
  53. package/dist/chunk-YPSGKPDA.mjs.map +1 -0
  54. package/dist/components/copilot-provider/copilot-messages.d.ts +16 -0
  55. package/dist/components/copilot-provider/copilot-messages.js +80 -0
  56. package/dist/components/copilot-provider/copilot-messages.js.map +1 -0
  57. package/dist/components/copilot-provider/copilot-messages.mjs +9 -0
  58. package/dist/components/copilot-provider/copilot-messages.mjs.map +1 -0
  59. package/dist/components/copilot-provider/copilotkit-props.d.ts +93 -0
  60. package/dist/components/copilot-provider/copilotkit-props.js +19 -0
  61. package/dist/components/copilot-provider/copilotkit-props.js.map +1 -0
  62. package/dist/components/copilot-provider/copilotkit-props.mjs +1 -0
  63. package/dist/components/copilot-provider/copilotkit-props.mjs.map +1 -0
  64. package/dist/components/copilot-provider/copilotkit.d.ts +18 -0
  65. package/dist/components/copilot-provider/copilotkit.js +990 -0
  66. package/dist/components/copilot-provider/copilotkit.js.map +1 -0
  67. package/dist/components/copilot-provider/copilotkit.mjs +22 -0
  68. package/dist/components/copilot-provider/copilotkit.mjs.map +1 -0
  69. package/dist/components/copilot-provider/index.d.ts +13 -0
  70. package/dist/components/copilot-provider/index.js +990 -0
  71. package/dist/components/copilot-provider/index.js.map +1 -0
  72. package/dist/components/copilot-provider/index.mjs +20 -0
  73. package/dist/components/copilot-provider/index.mjs.map +1 -0
  74. package/dist/components/error-boundary/error-boundary.d.ts +22 -0
  75. package/dist/components/error-boundary/error-boundary.js +183 -0
  76. package/dist/components/error-boundary/error-boundary.js.map +1 -0
  77. package/dist/components/error-boundary/error-boundary.mjs +12 -0
  78. package/dist/components/error-boundary/error-boundary.mjs.map +1 -0
  79. package/dist/components/error-boundary/error-utils.d.ts +11 -0
  80. package/dist/components/error-boundary/error-utils.js +177 -0
  81. package/dist/components/error-boundary/error-utils.js.map +1 -0
  82. package/dist/components/error-boundary/error-utils.mjs +13 -0
  83. package/dist/components/error-boundary/error-utils.mjs.map +1 -0
  84. package/dist/components/index.d.ts +13 -0
  85. package/dist/components/index.js +990 -0
  86. package/dist/components/index.js.map +1 -0
  87. package/dist/components/index.mjs +20 -0
  88. package/dist/components/index.mjs.map +1 -0
  89. package/dist/components/toast/exclamation-mark-icon.d.ts +9 -0
  90. package/dist/components/toast/exclamation-mark-icon.js +55 -0
  91. package/dist/components/toast/exclamation-mark-icon.js.map +1 -0
  92. package/dist/components/toast/exclamation-mark-icon.mjs +8 -0
  93. package/dist/components/toast/exclamation-mark-icon.mjs.map +1 -0
  94. package/dist/components/toast/toast-provider.d.ts +30 -0
  95. package/dist/components/toast/toast-provider.js +269 -0
  96. package/dist/components/toast/toast-provider.js.map +1 -0
  97. package/dist/components/toast/toast-provider.mjs +11 -0
  98. package/dist/components/toast/toast-provider.mjs.map +1 -0
  99. package/dist/context/copilot-context.d.ts +142 -0
  100. package/dist/context/copilot-context.js +138 -0
  101. package/dist/context/copilot-context.js.map +1 -0
  102. package/dist/context/copilot-context.mjs +10 -0
  103. package/dist/context/copilot-context.mjs.map +1 -0
  104. package/dist/context/copilot-messages-context.d.ts +15 -0
  105. package/dist/context/copilot-messages-context.js +57 -0
  106. package/dist/context/copilot-messages-context.js.map +1 -0
  107. package/dist/context/copilot-messages-context.mjs +10 -0
  108. package/dist/context/copilot-messages-context.mjs.map +1 -0
  109. package/dist/context/index.d.ts +11 -0
  110. package/dist/context/index.js +161 -0
  111. package/dist/context/index.js.map +1 -0
  112. package/dist/context/index.mjs +17 -0
  113. package/dist/context/index.mjs.map +1 -0
  114. package/dist/hooks/index.d.ts +21 -0
  115. package/dist/hooks/index.js +1303 -0
  116. package/dist/hooks/index.js.map +1 -0
  117. package/dist/hooks/index.mjs +56 -0
  118. package/dist/hooks/index.mjs.map +1 -0
  119. package/dist/hooks/use-chat.d.ts +124 -0
  120. package/dist/hooks/use-chat.js +608 -0
  121. package/dist/hooks/use-chat.js.map +1 -0
  122. package/dist/hooks/use-chat.mjs +12 -0
  123. package/dist/hooks/use-chat.mjs.map +1 -0
  124. package/dist/hooks/use-coagent-state-render.d.ts +60 -0
  125. package/dist/hooks/use-coagent-state-render.js +195 -0
  126. package/dist/hooks/use-coagent-state-render.js.map +1 -0
  127. package/dist/hooks/use-coagent-state-render.mjs +11 -0
  128. package/dist/hooks/use-coagent-state-render.mjs.map +1 -0
  129. package/dist/hooks/use-coagent.d.ts +199 -0
  130. package/dist/hooks/use-coagent.js +1064 -0
  131. package/dist/hooks/use-coagent.js.map +1 -0
  132. package/dist/hooks/use-coagent.mjs +29 -0
  133. package/dist/hooks/use-coagent.mjs.map +1 -0
  134. package/dist/hooks/use-copilot-action.d.ts +140 -0
  135. package/dist/hooks/use-copilot-action.js +369 -0
  136. package/dist/hooks/use-copilot-action.js.map +1 -0
  137. package/dist/hooks/use-copilot-action.mjs +11 -0
  138. package/dist/hooks/use-copilot-action.mjs.map +1 -0
  139. package/dist/hooks/use-copilot-authenticated-action.d.ts +8 -0
  140. package/dist/hooks/use-copilot-authenticated-action.js +418 -0
  141. package/dist/hooks/use-copilot-authenticated-action.js.map +1 -0
  142. package/dist/hooks/use-copilot-authenticated-action.mjs +12 -0
  143. package/dist/hooks/use-copilot-authenticated-action.mjs.map +1 -0
  144. package/dist/hooks/use-copilot-chat.d.ts +38 -0
  145. package/dist/hooks/use-copilot-chat.js +928 -0
  146. package/dist/hooks/use-copilot-chat.js.map +1 -0
  147. package/dist/hooks/use-copilot-chat.mjs +24 -0
  148. package/dist/hooks/use-copilot-chat.mjs.map +1 -0
  149. package/dist/hooks/use-copilot-readable.d.ts +33 -0
  150. package/dist/hooks/use-copilot-readable.js +158 -0
  151. package/dist/hooks/use-copilot-readable.js.map +1 -0
  152. package/dist/hooks/use-copilot-readable.mjs +9 -0
  153. package/dist/hooks/use-copilot-readable.mjs.map +1 -0
  154. package/dist/hooks/use-copilot-runtime-client.d.ts +5 -0
  155. package/dist/hooks/use-copilot-runtime-client.js +182 -0
  156. package/dist/hooks/use-copilot-runtime-client.js.map +1 -0
  157. package/dist/hooks/use-copilot-runtime-client.mjs +10 -0
  158. package/dist/hooks/use-copilot-runtime-client.mjs.map +1 -0
  159. package/dist/hooks/use-flat-category-store.d.ts +9 -0
  160. package/dist/hooks/use-flat-category-store.js +90 -0
  161. package/dist/hooks/use-flat-category-store.js.map +1 -0
  162. package/dist/hooks/use-flat-category-store.mjs +8 -0
  163. package/dist/hooks/use-flat-category-store.mjs.map +1 -0
  164. package/dist/hooks/use-make-copilot-document-readable.d.ts +12 -0
  165. package/dist/hooks/use-make-copilot-document-readable.js +153 -0
  166. package/dist/hooks/use-make-copilot-document-readable.js.map +1 -0
  167. package/dist/hooks/use-make-copilot-document-readable.mjs +9 -0
  168. package/dist/hooks/use-make-copilot-document-readable.mjs.map +1 -0
  169. package/dist/hooks/use-tree.d.ts +18 -0
  170. package/dist/hooks/use-tree.js +172 -0
  171. package/dist/hooks/use-tree.js.map +1 -0
  172. package/dist/hooks/use-tree.mjs +8 -0
  173. package/dist/hooks/use-tree.mjs.map +1 -0
  174. package/dist/index.d.ts +27 -0
  175. package/dist/index.js +2224 -0
  176. package/dist/index.js.map +1 -0
  177. package/dist/index.mjs +83 -0
  178. package/dist/index.mjs.map +1 -0
  179. package/dist/lib/copilot-task.d.ts +97 -0
  180. package/dist/lib/copilot-task.js +195 -0
  181. package/dist/lib/copilot-task.js.map +1 -0
  182. package/dist/lib/copilot-task.mjs +20 -0
  183. package/dist/lib/copilot-task.mjs.map +1 -0
  184. package/dist/lib/index.d.ts +11 -0
  185. package/dist/lib/index.js +197 -0
  186. package/dist/lib/index.js.map +1 -0
  187. package/dist/lib/index.mjs +21 -0
  188. package/dist/lib/index.mjs.map +1 -0
  189. package/dist/types/chat-suggestion-configuration.d.ts +22 -0
  190. package/dist/types/chat-suggestion-configuration.js +19 -0
  191. package/dist/types/chat-suggestion-configuration.js.map +1 -0
  192. package/dist/types/chat-suggestion-configuration.mjs +1 -0
  193. package/dist/types/chat-suggestion-configuration.mjs.map +1 -0
  194. package/dist/types/coagent-action.d.ts +29 -0
  195. package/dist/types/coagent-action.js +19 -0
  196. package/dist/types/coagent-action.js.map +1 -0
  197. package/dist/types/coagent-action.mjs +1 -0
  198. package/dist/types/coagent-action.mjs.map +1 -0
  199. package/dist/types/coagent-state.d.ts +11 -0
  200. package/dist/types/coagent-state.js +19 -0
  201. package/dist/types/coagent-state.js.map +1 -0
  202. package/dist/types/coagent-state.mjs +1 -0
  203. package/dist/types/coagent-state.mjs.map +1 -0
  204. package/dist/types/document-pointer.d.ts +9 -0
  205. package/dist/types/document-pointer.js +19 -0
  206. package/dist/types/document-pointer.js.map +1 -0
  207. package/dist/types/document-pointer.mjs +1 -0
  208. package/dist/types/document-pointer.mjs.map +1 -0
  209. package/dist/types/frontend-action.d.ts +126 -0
  210. package/dist/types/frontend-action.js +53 -0
  211. package/dist/types/frontend-action.js.map +1 -0
  212. package/dist/types/frontend-action.mjs +8 -0
  213. package/dist/types/frontend-action.mjs.map +1 -0
  214. package/dist/types/index.d.ts +7 -0
  215. package/dist/types/index.js +19 -0
  216. package/dist/types/index.js.map +1 -0
  217. package/dist/types/index.mjs +2 -0
  218. package/dist/types/index.mjs.map +1 -0
  219. package/dist/types/system-message.d.ts +3 -0
  220. package/dist/types/system-message.js +19 -0
  221. package/dist/types/system-message.js.map +1 -0
  222. package/dist/types/system-message.mjs +1 -0
  223. package/dist/types/system-message.mjs.map +1 -0
  224. package/dist/utils/dev-console.d.ts +3 -0
  225. package/dist/utils/dev-console.js +42 -0
  226. package/dist/utils/dev-console.js.map +1 -0
  227. package/dist/utils/dev-console.mjs +8 -0
  228. package/dist/utils/dev-console.mjs.map +1 -0
  229. package/dist/utils/extract.d.ts +43 -0
  230. package/dist/utils/extract.js +208 -0
  231. package/dist/utils/extract.js.map +1 -0
  232. package/dist/utils/extract.mjs +18 -0
  233. package/dist/utils/extract.mjs.map +1 -0
  234. package/dist/utils/index.d.ts +13 -0
  235. package/dist/utils/index.js +226 -0
  236. package/dist/utils/index.js.map +1 -0
  237. package/dist/utils/index.mjs +21 -0
  238. package/dist/utils/index.mjs.map +1 -0
  239. package/dist/utils/utils.d.ts +2 -0
  240. package/dist/utils/utils.js +19 -0
  241. package/dist/utils/utils.js.map +1 -0
  242. package/dist/utils/utils.mjs +1 -0
  243. package/dist/utils/utils.mjs.map +1 -0
  244. package/dist/utils/utils.test.d.ts +2 -0
  245. package/dist/utils/utils.test.js +9 -0
  246. package/dist/utils/utils.test.js.map +1 -0
  247. package/dist/utils/utils.test.mjs +7 -0
  248. package/dist/utils/utils.test.mjs.map +1 -0
  249. package/package.json +3 -3
@@ -0,0 +1,608 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
10
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
+ var __spreadValues = (a, b) => {
12
+ for (var prop in b || (b = {}))
13
+ if (__hasOwnProp.call(b, prop))
14
+ __defNormalProp(a, prop, b[prop]);
15
+ if (__getOwnPropSymbols)
16
+ for (var prop of __getOwnPropSymbols(b)) {
17
+ if (__propIsEnum.call(b, prop))
18
+ __defNormalProp(a, prop, b[prop]);
19
+ }
20
+ return a;
21
+ };
22
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
23
+ var __export = (target, all) => {
24
+ for (var name in all)
25
+ __defProp(target, name, { get: all[name], enumerable: true });
26
+ };
27
+ var __copyProps = (to, from, except, desc) => {
28
+ if (from && typeof from === "object" || typeof from === "function") {
29
+ for (let key of __getOwnPropNames(from))
30
+ if (!__hasOwnProp.call(to, key) && key !== except)
31
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
32
+ }
33
+ return to;
34
+ };
35
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
36
+ var __async = (__this, __arguments, generator) => {
37
+ return new Promise((resolve, reject) => {
38
+ var fulfilled = (value) => {
39
+ try {
40
+ step(generator.next(value));
41
+ } catch (e) {
42
+ reject(e);
43
+ }
44
+ };
45
+ var rejected = (value) => {
46
+ try {
47
+ step(generator.throw(value));
48
+ } catch (e) {
49
+ reject(e);
50
+ }
51
+ };
52
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
53
+ step((generator = generator.apply(__this, __arguments)).next());
54
+ });
55
+ };
56
+
57
+ // src/hooks/use-chat.ts
58
+ var use_chat_exports = {};
59
+ __export(use_chat_exports, {
60
+ useChat: () => useChat
61
+ });
62
+ module.exports = __toCommonJS(use_chat_exports);
63
+ var import_react4 = require("react");
64
+ var import_shared2 = require("@copilotkit/shared");
65
+ var import_runtime_client_gql3 = require("@copilotkit/runtime-client-gql");
66
+
67
+ // src/types/frontend-action.ts
68
+ var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
69
+ var import_shared = require("@copilotkit/shared");
70
+ function processActionsForRuntimeRequest(actions) {
71
+ const filteredActions = actions.filter(
72
+ (action) => action.available !== import_runtime_client_gql.ActionInputAvailability.Disabled && action.disabled !== true && action.name !== "*"
73
+ ).map((action) => {
74
+ let available = import_runtime_client_gql.ActionInputAvailability.Enabled;
75
+ if (action.disabled) {
76
+ available = import_runtime_client_gql.ActionInputAvailability.Disabled;
77
+ } else if (action.available === "disabled") {
78
+ available = import_runtime_client_gql.ActionInputAvailability.Disabled;
79
+ } else if (action.available === "remote") {
80
+ available = import_runtime_client_gql.ActionInputAvailability.Remote;
81
+ }
82
+ return {
83
+ name: action.name,
84
+ description: action.description || "",
85
+ jsonSchema: JSON.stringify((0, import_shared.actionParametersToJsonSchema)(action.parameters || [])),
86
+ available
87
+ };
88
+ });
89
+ return filteredActions;
90
+ }
91
+
92
+ // src/hooks/use-copilot-runtime-client.ts
93
+ var import_runtime_client_gql2 = require("@copilotkit/runtime-client-gql");
94
+
95
+ // src/components/toast/toast-provider.tsx
96
+ var import_react2 = require("react");
97
+
98
+ // src/components/error-boundary/error-utils.tsx
99
+ var import_react = require("react");
100
+
101
+ // src/components/toast/exclamation-mark-icon.tsx
102
+ var import_jsx_runtime = require("react/jsx-runtime");
103
+ var ExclamationMarkIcon = ({
104
+ className,
105
+ style
106
+ }) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
107
+ "svg",
108
+ {
109
+ xmlns: "http://www.w3.org/2000/svg",
110
+ width: "24",
111
+ height: "24",
112
+ viewBox: "0 0 24 24",
113
+ fill: "none",
114
+ stroke: "currentColor",
115
+ strokeWidth: "2",
116
+ strokeLinecap: "round",
117
+ strokeLinejoin: "round",
118
+ className: `lucide lucide-circle-alert ${className ? className : ""}`,
119
+ style,
120
+ children: [
121
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("circle", { cx: "12", cy: "12", r: "10" }),
122
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("line", { x1: "12", x2: "12", y1: "8", y2: "12" }),
123
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("line", { x1: "12", x2: "12.01", y1: "16", y2: "16" })
124
+ ]
125
+ }
126
+ );
127
+
128
+ // src/components/error-boundary/error-utils.tsx
129
+ var import_jsx_runtime2 = require("react/jsx-runtime");
130
+ function ErrorToast({ errors }) {
131
+ const errorsToRender = errors.map((error, idx) => {
132
+ var _a, _b, _c;
133
+ const message = "extensions" in error ? (_b = (_a = error.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message : error.message;
134
+ const code = "extensions" in error ? (_c = error.extensions) == null ? void 0 : _c.code : null;
135
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
136
+ "div",
137
+ {
138
+ style: {
139
+ marginTop: idx === 0 ? 0 : 10,
140
+ marginBottom: 14
141
+ },
142
+ children: [
143
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ExclamationMarkIcon, { style: { marginBottom: 4 } }),
144
+ code && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
145
+ "div",
146
+ {
147
+ style: {
148
+ fontWeight: "600",
149
+ marginBottom: 4
150
+ },
151
+ children: [
152
+ "Copilot Cloud Error:",
153
+ " ",
154
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { style: { fontFamily: "monospace", fontWeight: "normal" }, children: code })
155
+ ]
156
+ }
157
+ ),
158
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { children: message })
159
+ ]
160
+ },
161
+ idx
162
+ );
163
+ });
164
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
165
+ "div",
166
+ {
167
+ style: {
168
+ fontSize: "13px",
169
+ maxWidth: "600px"
170
+ },
171
+ children: [
172
+ errorsToRender,
173
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { fontSize: "11px", opacity: 0.75 }, children: "NOTE: This error only displays during local development." })
174
+ ]
175
+ }
176
+ );
177
+ }
178
+ function useErrorToast() {
179
+ const { addToast } = useToast();
180
+ return (0, import_react.useCallback)(
181
+ (error) => {
182
+ const errorId = error.map((err) => {
183
+ var _a, _b;
184
+ const message = "extensions" in err ? ((_b = (_a = err.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message) || err.message : err.message;
185
+ const stack = err.stack || "";
186
+ return btoa(message + stack).slice(0, 32);
187
+ }).join("|");
188
+ addToast({
189
+ type: "error",
190
+ id: errorId,
191
+ // Toast libraries typically dedupe by id
192
+ message: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ErrorToast, { errors: error })
193
+ });
194
+ },
195
+ [addToast]
196
+ );
197
+ }
198
+ function useAsyncCallback(callback, deps) {
199
+ const addErrorToast = useErrorToast();
200
+ return (0, import_react.useCallback)((...args) => __async(this, null, function* () {
201
+ try {
202
+ return yield callback(...args);
203
+ } catch (error) {
204
+ console.error("Error in async callback:", error);
205
+ addErrorToast([error]);
206
+ throw error;
207
+ }
208
+ }), deps);
209
+ }
210
+
211
+ // src/components/toast/toast-provider.tsx
212
+ var import_jsx_runtime3 = require("react/jsx-runtime");
213
+ var ToastContext = (0, import_react2.createContext)(void 0);
214
+ function useToast() {
215
+ const context = (0, import_react2.useContext)(ToastContext);
216
+ if (!context) {
217
+ throw new Error("useToast must be used within a ToastProvider");
218
+ }
219
+ return context;
220
+ }
221
+
222
+ // src/hooks/use-copilot-runtime-client.ts
223
+ var import_react3 = require("react");
224
+ var useCopilotRuntimeClient = (options) => {
225
+ const { addGraphQLErrorsToast } = useToast();
226
+ const addErrorToast = useErrorToast();
227
+ const runtimeClient = (0, import_react3.useMemo)(() => {
228
+ return new import_runtime_client_gql2.CopilotRuntimeClient(__spreadProps(__spreadValues({}, options), {
229
+ handleGQLErrors: (error) => {
230
+ if (error.graphQLErrors.length) {
231
+ addGraphQLErrorsToast(error.graphQLErrors);
232
+ } else {
233
+ addErrorToast([error]);
234
+ }
235
+ }
236
+ }));
237
+ }, [options, addGraphQLErrorsToast]);
238
+ return runtimeClient;
239
+ };
240
+
241
+ // src/hooks/use-chat.ts
242
+ function useChat(options) {
243
+ const {
244
+ messages,
245
+ setMessages,
246
+ makeSystemMessageCallback,
247
+ copilotConfig,
248
+ setIsLoading,
249
+ initialMessages,
250
+ isLoading,
251
+ actions,
252
+ onFunctionCall,
253
+ onCoAgentStateRender,
254
+ setCoagentStatesWithRef,
255
+ coagentStatesRef,
256
+ agentSession,
257
+ setAgentSession,
258
+ threadId,
259
+ setThreadId,
260
+ runId,
261
+ setRunId,
262
+ chatAbortControllerRef,
263
+ agentLock
264
+ } = options;
265
+ const runChatCompletionRef = (0, import_react4.useRef)();
266
+ const agentSessionRef = (0, import_react4.useRef)(agentSession);
267
+ agentSessionRef.current = agentSession;
268
+ const threadIdRef = (0, import_react4.useRef)(threadId);
269
+ threadIdRef.current = threadId;
270
+ const runIdRef = (0, import_react4.useRef)(runId);
271
+ runIdRef.current = runId;
272
+ const publicApiKey = copilotConfig.publicApiKey;
273
+ const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [import_shared2.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
274
+ const runtimeClient = useCopilotRuntimeClient({
275
+ url: copilotConfig.chatApiEndpoint,
276
+ publicApiKey: copilotConfig.publicApiKey,
277
+ headers,
278
+ credentials: copilotConfig.credentials
279
+ });
280
+ const runChatCompletion = useAsyncCallback(
281
+ (previousMessages) => __async(this, null, function* () {
282
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
283
+ setIsLoading(true);
284
+ let newMessages = [
285
+ new import_runtime_client_gql3.TextMessage({
286
+ content: "",
287
+ role: import_runtime_client_gql3.Role.Assistant
288
+ })
289
+ ];
290
+ chatAbortControllerRef.current = new AbortController();
291
+ setMessages([...previousMessages, ...newMessages]);
292
+ const systemMessage = makeSystemMessageCallback();
293
+ const messagesWithContext = [systemMessage, ...initialMessages || [], ...previousMessages];
294
+ const isAgentRun = agentSessionRef.current !== null;
295
+ const stream = runtimeClient.asStream(
296
+ runtimeClient.generateCopilotResponse({
297
+ data: __spreadProps(__spreadValues(__spreadProps(__spreadValues({
298
+ frontend: {
299
+ actions: processActionsForRuntimeRequest(actions),
300
+ url: window.location.href
301
+ },
302
+ threadId: threadIdRef.current,
303
+ runId: runIdRef.current,
304
+ messages: (0, import_runtime_client_gql3.convertMessagesToGqlInput)((0, import_runtime_client_gql3.filterAgentStateMessages)(messagesWithContext))
305
+ }, copilotConfig.cloud ? {
306
+ cloud: __spreadValues({}, ((_c = (_b = (_a = copilotConfig.cloud.guardrails) == null ? void 0 : _a.input) == null ? void 0 : _b.restrictToTopic) == null ? void 0 : _c.enabled) ? {
307
+ guardrails: {
308
+ inputValidationRules: {
309
+ allowList: copilotConfig.cloud.guardrails.input.restrictToTopic.validTopics,
310
+ denyList: copilotConfig.cloud.guardrails.input.restrictToTopic.invalidTopics
311
+ }
312
+ }
313
+ } : {})
314
+ } : {}), {
315
+ metadata: {
316
+ requestType: import_runtime_client_gql3.CopilotRequestType.Chat
317
+ }
318
+ }), agentSessionRef.current ? {
319
+ agentSession: agentSessionRef.current
320
+ } : {}), {
321
+ agentStates: Object.values(coagentStatesRef.current).map((state) => ({
322
+ agentName: state.name,
323
+ state: JSON.stringify(state.state)
324
+ })),
325
+ forwardedParameters: options.forwardedParameters || {}
326
+ }),
327
+ properties: copilotConfig.properties,
328
+ signal: (_d = chatAbortControllerRef.current) == null ? void 0 : _d.signal
329
+ })
330
+ );
331
+ const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
332
+ const reader = stream.getReader();
333
+ let executedCoAgentStateRenders = [];
334
+ let followUp = void 0;
335
+ let messages2 = [];
336
+ let syncedMessages = [];
337
+ try {
338
+ while (true) {
339
+ let done, value;
340
+ try {
341
+ const readResult = yield reader.read();
342
+ done = readResult.done;
343
+ value = readResult.value;
344
+ } catch (readError) {
345
+ break;
346
+ }
347
+ if (done) {
348
+ if (chatAbortControllerRef.current.signal.aborted) {
349
+ return [];
350
+ }
351
+ break;
352
+ }
353
+ if (!(value == null ? void 0 : value.generateCopilotResponse)) {
354
+ continue;
355
+ }
356
+ threadIdRef.current = value.generateCopilotResponse.threadId || null;
357
+ runIdRef.current = value.generateCopilotResponse.runId || null;
358
+ setThreadId(threadIdRef.current);
359
+ setRunId(runIdRef.current);
360
+ messages2 = (0, import_runtime_client_gql3.convertGqlOutputToMessages)(
361
+ (0, import_runtime_client_gql3.filterAdjacentAgentStateMessages)(value.generateCopilotResponse.messages)
362
+ );
363
+ if (messages2.length === 0) {
364
+ continue;
365
+ }
366
+ newMessages = [];
367
+ if (((_h = value.generateCopilotResponse.status) == null ? void 0 : _h.__typename) === "FailedResponseStatus" && value.generateCopilotResponse.status.reason === "GUARDRAILS_VALIDATION_FAILED") {
368
+ newMessages = [
369
+ new import_runtime_client_gql3.TextMessage({
370
+ role: import_runtime_client_gql3.MessageRole.Assistant,
371
+ content: ((_i = value.generateCopilotResponse.status.details) == null ? void 0 : _i.guardrailsReason) || ""
372
+ })
373
+ ];
374
+ setMessages([...previousMessages, ...newMessages]);
375
+ break;
376
+ } else {
377
+ newMessages = [...messages2];
378
+ for (const message of messages2) {
379
+ if (message.isAgentStateMessage() && !message.active && !executedCoAgentStateRenders.includes(message.id) && onCoAgentStateRender) {
380
+ if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
381
+ break;
382
+ }
383
+ yield onCoAgentStateRender({
384
+ name: message.agentName,
385
+ nodeName: message.nodeName,
386
+ state: message.state
387
+ });
388
+ executedCoAgentStateRenders.push(message.id);
389
+ }
390
+ }
391
+ const lastAgentStateMessage = [...messages2].reverse().find((message) => message.isAgentStateMessage());
392
+ if (lastAgentStateMessage) {
393
+ if (lastAgentStateMessage.state.messages && lastAgentStateMessage.state.messages.length > 0) {
394
+ syncedMessages = (0, import_runtime_client_gql3.loadMessagesFromJsonRepresentation)(
395
+ lastAgentStateMessage.state.messages
396
+ );
397
+ }
398
+ setCoagentStatesWithRef((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
399
+ [lastAgentStateMessage.agentName]: {
400
+ name: lastAgentStateMessage.agentName,
401
+ state: lastAgentStateMessage.state,
402
+ running: lastAgentStateMessage.running,
403
+ active: lastAgentStateMessage.active,
404
+ threadId: lastAgentStateMessage.threadId,
405
+ nodeName: lastAgentStateMessage.nodeName,
406
+ runId: lastAgentStateMessage.runId
407
+ }
408
+ }));
409
+ if (lastAgentStateMessage.running) {
410
+ setAgentSession({
411
+ threadId: lastAgentStateMessage.threadId,
412
+ agentName: lastAgentStateMessage.agentName,
413
+ nodeName: lastAgentStateMessage.nodeName
414
+ });
415
+ } else {
416
+ if (agentLock) {
417
+ setAgentSession({
418
+ threadId: (0, import_shared2.randomId)(),
419
+ agentName: agentLock,
420
+ nodeName: void 0
421
+ });
422
+ } else {
423
+ setAgentSession(null);
424
+ }
425
+ }
426
+ }
427
+ }
428
+ if (newMessages.length > 0) {
429
+ setMessages([...previousMessages, ...newMessages]);
430
+ }
431
+ }
432
+ const finalMessages = constructFinalMessages(syncedMessages, previousMessages, newMessages);
433
+ let didExecuteAction = false;
434
+ if (onFunctionCall) {
435
+ const lastMessages = [];
436
+ for (let i = finalMessages.length - 1; i >= 0; i--) {
437
+ const message = finalMessages[i];
438
+ if (message.isActionExecutionMessage() && message.status.code !== import_runtime_client_gql3.MessageStatusCode.Pending) {
439
+ lastMessages.unshift(message);
440
+ } else {
441
+ break;
442
+ }
443
+ }
444
+ for (const message of lastMessages) {
445
+ setMessages(finalMessages);
446
+ const action = actions.find((action2) => action2.name === message.name);
447
+ if (action) {
448
+ followUp = action.followUp;
449
+ let result;
450
+ try {
451
+ result = yield Promise.race([
452
+ onFunctionCall({
453
+ messages: previousMessages,
454
+ name: message.name,
455
+ args: message.arguments
456
+ }),
457
+ new Promise(
458
+ (resolve) => {
459
+ var _a2;
460
+ return (_a2 = chatAbortControllerRef.current) == null ? void 0 : _a2.signal.addEventListener(
461
+ "abort",
462
+ () => resolve("Operation was aborted by the user")
463
+ );
464
+ }
465
+ ),
466
+ // if the user stopped generation, we also abort consecutive actions
467
+ new Promise((resolve) => {
468
+ var _a2;
469
+ if ((_a2 = chatAbortControllerRef.current) == null ? void 0 : _a2.signal.aborted) {
470
+ resolve("Operation was aborted by the user");
471
+ }
472
+ })
473
+ ]);
474
+ } catch (e) {
475
+ result = `Failed to execute action ${message.name}`;
476
+ console.error(`Failed to execute action ${message.name}: ${e}`);
477
+ }
478
+ didExecuteAction = true;
479
+ const messageIndex = finalMessages.findIndex((msg) => msg.id === message.id);
480
+ finalMessages.splice(
481
+ messageIndex + 1,
482
+ 0,
483
+ new import_runtime_client_gql3.ResultMessage({
484
+ id: "result-" + message.id,
485
+ result: import_runtime_client_gql3.ResultMessage.encodeResult(result),
486
+ actionExecutionId: message.id,
487
+ actionName: message.name
488
+ })
489
+ );
490
+ }
491
+ }
492
+ setMessages(finalMessages);
493
+ }
494
+ if (
495
+ // if followUp is not explicitly false
496
+ followUp !== false && // and we executed an action
497
+ (didExecuteAction || // the last message is a server side result
498
+ !isAgentRun && finalMessages.length && finalMessages[finalMessages.length - 1].isResultMessage()) && // the user did not stop generation
499
+ !((_j = chatAbortControllerRef.current) == null ? void 0 : _j.signal.aborted)
500
+ ) {
501
+ yield new Promise((resolve) => setTimeout(resolve, 10));
502
+ return yield runChatCompletionRef.current(finalMessages);
503
+ } else if ((_k = chatAbortControllerRef.current) == null ? void 0 : _k.signal.aborted) {
504
+ const repairedMessages = finalMessages.filter((message, actionExecutionIndex) => {
505
+ if (message.isActionExecutionMessage()) {
506
+ return finalMessages.find(
507
+ (msg, resultIndex) => msg.isResultMessage() && msg.actionExecutionId === message.id && resultIndex === actionExecutionIndex + 1
508
+ );
509
+ }
510
+ return true;
511
+ });
512
+ const repairedMessageIds = repairedMessages.map((message) => message.id);
513
+ setMessages(repairedMessages);
514
+ if ((_l = agentSessionRef.current) == null ? void 0 : _l.nodeName) {
515
+ setAgentSession({
516
+ threadId: agentSessionRef.current.threadId,
517
+ agentName: agentSessionRef.current.agentName,
518
+ nodeName: "__end__"
519
+ });
520
+ }
521
+ return newMessages.filter((message) => repairedMessageIds.includes(message.id));
522
+ } else {
523
+ return newMessages.slice();
524
+ }
525
+ } finally {
526
+ setIsLoading(false);
527
+ }
528
+ }),
529
+ [
530
+ messages,
531
+ setMessages,
532
+ makeSystemMessageCallback,
533
+ copilotConfig,
534
+ setIsLoading,
535
+ initialMessages,
536
+ isLoading,
537
+ actions,
538
+ onFunctionCall,
539
+ onCoAgentStateRender,
540
+ setCoagentStatesWithRef,
541
+ coagentStatesRef,
542
+ agentSession,
543
+ setAgentSession
544
+ ]
545
+ );
546
+ runChatCompletionRef.current = runChatCompletion;
547
+ const runChatCompletionAndHandleFunctionCall = useAsyncCallback(
548
+ (messages2) => __async(this, null, function* () {
549
+ yield runChatCompletionRef.current(messages2);
550
+ }),
551
+ [messages]
552
+ );
553
+ const append = useAsyncCallback(
554
+ (message) => __async(this, null, function* () {
555
+ if (isLoading) {
556
+ return;
557
+ }
558
+ const newMessages = [...messages, message];
559
+ setMessages(newMessages);
560
+ return runChatCompletionAndHandleFunctionCall(newMessages);
561
+ }),
562
+ [isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]
563
+ );
564
+ const reload = useAsyncCallback(() => __async(this, null, function* () {
565
+ if (isLoading || messages.length === 0) {
566
+ return;
567
+ }
568
+ let newMessages = [...messages];
569
+ const lastMessage = messages[messages.length - 1];
570
+ if (lastMessage.isTextMessage() && lastMessage.role === "assistant") {
571
+ newMessages = newMessages.slice(0, -1);
572
+ }
573
+ setMessages(newMessages);
574
+ return runChatCompletionAndHandleFunctionCall(newMessages);
575
+ }), [isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]);
576
+ const stop = () => {
577
+ var _a;
578
+ (_a = chatAbortControllerRef.current) == null ? void 0 : _a.abort("Stop was called");
579
+ };
580
+ return {
581
+ append,
582
+ reload,
583
+ stop,
584
+ runChatCompletion: () => runChatCompletionRef.current(messages)
585
+ };
586
+ }
587
+ function constructFinalMessages(syncedMessages, previousMessages, newMessages) {
588
+ const finalMessages = syncedMessages.length > 0 ? [...syncedMessages] : [...previousMessages, ...newMessages];
589
+ if (syncedMessages.length > 0) {
590
+ const messagesWithAgentState = [...previousMessages, ...newMessages];
591
+ let previousMessageId = void 0;
592
+ for (const message of messagesWithAgentState) {
593
+ if (message.isAgentStateMessage()) {
594
+ const index = finalMessages.findIndex((msg) => msg.id === previousMessageId);
595
+ if (index !== -1) {
596
+ finalMessages.splice(index + 1, 0, message);
597
+ }
598
+ }
599
+ previousMessageId = message.id;
600
+ }
601
+ }
602
+ return finalMessages;
603
+ }
604
+ // Annotate the CommonJS export names for ESM import in node:
605
+ 0 && (module.exports = {
606
+ useChat
607
+ });
608
+ //# sourceMappingURL=use-chat.js.map