@copilotkit/react-core 1.4.8-no-pino-redact.1 → 1.4.8

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 (95) hide show
  1. package/CHANGELOG.md +8 -3
  2. package/dist/{chunk-XBVKTDXP.mjs → chunk-6EMLM6WX.mjs} +17 -14
  3. package/dist/chunk-6EMLM6WX.mjs.map +1 -0
  4. package/dist/{chunk-ODN4H66E.mjs → chunk-7LRDVJH5.mjs} +6 -2
  5. package/dist/chunk-7LRDVJH5.mjs.map +1 -0
  6. package/dist/{chunk-O22KGHOQ.mjs → chunk-FSC4A3JN.mjs} +17 -13
  7. package/dist/chunk-FSC4A3JN.mjs.map +1 -0
  8. package/dist/{chunk-OT67R4NB.mjs → chunk-IFTHM7LF.mjs} +2 -2
  9. package/dist/{chunk-DHGXL5PC.mjs → chunk-IVYL7JRC.mjs} +7 -4
  10. package/dist/chunk-IVYL7JRC.mjs.map +1 -0
  11. package/dist/chunk-NTLCOVE5.mjs +321 -0
  12. package/dist/chunk-NTLCOVE5.mjs.map +1 -0
  13. package/dist/chunk-QCUP6HLK.mjs +37 -0
  14. package/dist/chunk-QCUP6HLK.mjs.map +1 -0
  15. package/dist/{chunk-2KCEHGSI.mjs → chunk-SFPANIOY.mjs} +99 -49
  16. package/dist/chunk-SFPANIOY.mjs.map +1 -0
  17. package/dist/{chunk-JHEAUB3Z.mjs → chunk-X6ZF5WAX.mjs} +1 -1
  18. package/dist/{chunk-JHEAUB3Z.mjs.map → chunk-X6ZF5WAX.mjs.map} +1 -1
  19. package/dist/{chunk-Y7MI4PBB.mjs → chunk-YUY5ZAST.mjs} +6 -3
  20. package/dist/chunk-YUY5ZAST.mjs.map +1 -0
  21. package/dist/components/copilot-provider/copilotkit.js +156 -91
  22. package/dist/components/copilot-provider/copilotkit.js.map +1 -1
  23. package/dist/components/copilot-provider/copilotkit.mjs +4 -3
  24. package/dist/components/copilot-provider/index.js +156 -91
  25. package/dist/components/copilot-provider/index.js.map +1 -1
  26. package/dist/components/copilot-provider/index.mjs +4 -3
  27. package/dist/components/error-boundary/error-boundary.d.ts +22 -0
  28. package/dist/components/error-boundary/error-boundary.js +183 -0
  29. package/dist/components/error-boundary/error-boundary.js.map +1 -0
  30. package/dist/components/error-boundary/error-boundary.mjs +12 -0
  31. package/dist/components/error-boundary/error-boundary.mjs.map +1 -0
  32. package/dist/components/error-boundary/error-utils.d.ts +11 -0
  33. package/dist/components/error-boundary/error-utils.js +177 -0
  34. package/dist/components/error-boundary/error-utils.js.map +1 -0
  35. package/dist/components/error-boundary/error-utils.mjs +13 -0
  36. package/dist/components/error-boundary/error-utils.mjs.map +1 -0
  37. package/dist/components/index.js +156 -91
  38. package/dist/components/index.js.map +1 -1
  39. package/dist/components/index.mjs +4 -3
  40. package/dist/components/toast/toast-provider.d.ts +2 -1
  41. package/dist/components/toast/toast-provider.js +76 -62
  42. package/dist/components/toast/toast-provider.js.map +1 -1
  43. package/dist/components/toast/toast-provider.mjs +1 -1
  44. package/dist/hooks/index.js +398 -253
  45. package/dist/hooks/index.js.map +1 -1
  46. package/dist/hooks/index.mjs +9 -8
  47. package/dist/hooks/use-chat.js +343 -199
  48. package/dist/hooks/use-chat.js.map +1 -1
  49. package/dist/hooks/use-chat.mjs +3 -3
  50. package/dist/hooks/use-coagent-state-render.d.ts +2 -2
  51. package/dist/hooks/use-coagent-state-render.js.map +1 -1
  52. package/dist/hooks/use-coagent-state-render.mjs +1 -1
  53. package/dist/hooks/use-coagent.js +378 -233
  54. package/dist/hooks/use-coagent.js.map +1 -1
  55. package/dist/hooks/use-coagent.mjs +7 -6
  56. package/dist/hooks/use-copilot-action.js +132 -7
  57. package/dist/hooks/use-copilot-action.js.map +1 -1
  58. package/dist/hooks/use-copilot-action.mjs +3 -1
  59. package/dist/hooks/use-copilot-chat.js +367 -223
  60. package/dist/hooks/use-copilot-chat.js.map +1 -1
  61. package/dist/hooks/use-copilot-chat.mjs +6 -5
  62. package/dist/hooks/use-copilot-runtime-client.js +110 -4
  63. package/dist/hooks/use-copilot-runtime-client.js.map +1 -1
  64. package/dist/hooks/use-copilot-runtime-client.mjs +2 -2
  65. package/dist/index.js +439 -335
  66. package/dist/index.js.map +1 -1
  67. package/dist/index.mjs +10 -9
  68. package/dist/lib/copilot-task.js.map +1 -1
  69. package/dist/lib/copilot-task.mjs +5 -4
  70. package/dist/lib/index.js.map +1 -1
  71. package/dist/lib/index.mjs +5 -4
  72. package/dist/utils/extract.js.map +1 -1
  73. package/dist/utils/extract.mjs +4 -3
  74. package/dist/utils/index.js.map +1 -1
  75. package/dist/utils/index.mjs +4 -3
  76. package/package.json +3 -3
  77. package/src/components/copilot-provider/copilotkit.tsx +4 -1
  78. package/src/components/error-boundary/error-boundary.tsx +42 -0
  79. package/src/components/error-boundary/error-utils.tsx +95 -0
  80. package/src/components/toast/toast-provider.tsx +10 -49
  81. package/src/hooks/use-chat.ts +286 -259
  82. package/src/hooks/use-coagent-state-render.ts +2 -2
  83. package/src/hooks/use-coagent.ts +14 -11
  84. package/src/hooks/use-copilot-action.ts +3 -2
  85. package/src/hooks/use-copilot-chat.ts +9 -8
  86. package/src/hooks/use-copilot-runtime-client.ts +4 -0
  87. package/dist/chunk-2KCEHGSI.mjs.map +0 -1
  88. package/dist/chunk-DHGXL5PC.mjs.map +0 -1
  89. package/dist/chunk-EUIBVFV6.mjs +0 -294
  90. package/dist/chunk-EUIBVFV6.mjs.map +0 -1
  91. package/dist/chunk-O22KGHOQ.mjs.map +0 -1
  92. package/dist/chunk-ODN4H66E.mjs.map +0 -1
  93. package/dist/chunk-XBVKTDXP.mjs.map +0 -1
  94. package/dist/chunk-Y7MI4PBB.mjs.map +0 -1
  95. /package/dist/{chunk-OT67R4NB.mjs.map → chunk-IFTHM7LF.mjs.map} +0 -0
@@ -1,3 +1,6 @@
1
+ import {
2
+ useAsyncCallback
3
+ } from "./chunk-SFPANIOY.mjs";
1
4
  import {
2
5
  useCopilotContext
3
6
  } from "./chunk-XQFVXX6R.mjs";
@@ -18,7 +21,7 @@ function useCopilotAction(action, dependencies) {
18
21
  const renderAndWait = action.renderAndWait || action.renderAndWaitForResponse;
19
22
  action.renderAndWait = void 0;
20
23
  action.renderAndWaitForResponse = void 0;
21
- action.handler = () => __async(this, null, function* () {
24
+ action.handler = useAsyncCallback(() => __async(this, null, function* () {
22
25
  let resolve;
23
26
  let reject;
24
27
  const promise = new Promise((resolvePromise, rejectPromise) => {
@@ -27,7 +30,7 @@ function useCopilotAction(action, dependencies) {
27
30
  });
28
31
  renderAndWaitRef.current = { promise, resolve, reject };
29
32
  return yield promise;
30
- });
33
+ }), []);
31
34
  action.render = (props) => {
32
35
  const waitProps = {
33
36
  status: props.status,
@@ -88,4 +91,4 @@ function useCopilotAction(action, dependencies) {
88
91
  export {
89
92
  useCopilotAction
90
93
  };
91
- //# sourceMappingURL=chunk-Y7MI4PBB.mjs.map
94
+ //# sourceMappingURL=chunk-YUY5ZAST.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/use-copilot-action.ts"],"sourcesContent":["/**\n * Example usage of useCopilotAction with complex parameters:\n *\n * @example\n * useCopilotAction({\n * name: \"myAction\",\n * parameters: [\n * { name: \"arg1\", type: \"string\", enum: [\"option1\", \"option2\", \"option3\"], required: false },\n * { name: \"arg2\", type: \"number\" },\n * {\n * name: \"arg3\",\n * type: \"object\",\n * attributes: [\n * { name: \"nestedArg1\", type: \"boolean\" },\n * { name: \"xyz\", required: false },\n * ],\n * },\n * { name: \"arg4\", type: \"number[]\" },\n * ],\n * handler: ({ arg1, arg2, arg3, arg4 }) => {\n * const x = arg3.nestedArg1;\n * const z = arg3.xyz;\n * console.log(arg1, arg2, arg3);\n * },\n * });\n *\n * @example\n * // Simple action without parameters\n * useCopilotAction({\n * name: \"myAction\",\n * handler: () => {\n * console.log(\"No parameters provided.\");\n * },\n * });\n *\n * @example\n * // Interactive action with UI rendering and response handling\n * useCopilotAction({\n * name: \"handleMeeting\",\n * description: \"Handle a meeting by booking or canceling\",\n * parameters: [\n * {\n * name: \"meeting\",\n * type: \"string\",\n * description: \"The meeting to handle\",\n * required: true,\n * },\n * {\n * name: \"date\",\n * type: \"string\",\n * description: \"The date of the meeting\",\n * required: true,\n * },\n * {\n * name: \"title\",\n * type: \"string\",\n * description: \"The title of the meeting\",\n * required: true,\n * },\n * ],\n * renderAndWaitForResponse: ({ args, respond, status }) => {\n * const { meeting, date, title } = args;\n * return (\n * <MeetingConfirmationDialog\n * meeting={meeting}\n * date={date}\n * title={title}\n * onConfirm={() => respond('meeting confirmed')}\n * onCancel={() => respond('meeting canceled')}\n * />\n * );\n * },\n * });\n */\n\n/**\n * <img src=\"/images/use-copilot-action/useCopilotAction.gif\" width=\"500\" />\n * `useCopilotAction` is a React hook that you can use in your application to provide\n * custom actions that can be called by the AI. Essentially, it allows the Copilot to\n * execute these actions contextually during a chat, based on the user's interactions\n * and needs.\n *\n * Here's how it works:\n *\n * Use `useCopilotAction` to set up actions that the Copilot can call. To provide\n * more context to the Copilot, you can provide it with a `description` (for example to explain\n * what the action does, under which conditions it can be called, etc.).\n *\n * Then you define the parameters of the action, which can be simple, e.g. primitives like strings or numbers,\n * or complex, e.g. objects or arrays.\n *\n * Finally, you provide a `handler` function that receives the parameters and returns a result.\n * CopilotKit takes care of automatically inferring the parameter types, so you get type safety\n * and autocompletion for free.\n *\n * To render a custom UI for the action, you can provide a `render()` function. This function\n * lets you render a custom component or return a string to display.\n *\n * ## Usage\n *\n * ### Simple Usage\n *\n * ```tsx\n * useCopilotAction({\n * name: \"sayHello\",\n * description: \"Say hello to someone.\",\n * parameters: [\n * {\n * name: \"name\",\n * type: \"string\",\n * description: \"name of the person to say greet\",\n * },\n * ],\n * handler: async ({ name }) => {\n * alert(`Hello, ${name}!`);\n * },\n * });\n * ```\n *\n * ## Generative UI\n *\n * This hooks enables you to dynamically generate UI elements and render them in the copilot chat. For more information, check out the [Generative UI](/guides/generative-ui) page.\n */\nimport { Parameter, randomId } from \"@copilotkit/shared\";\nimport { createElement, Fragment, useEffect, useRef } from \"react\";\nimport { useCopilotContext } from \"../context/copilot-context\";\nimport { useAsyncCallback } from \"../components/error-boundary/error-utils\";\nimport {\n ActionRenderProps,\n ActionRenderPropsNoArgsWait,\n ActionRenderPropsWait,\n FrontendAction,\n} from \"../types/frontend-action\";\n\n// We implement useCopilotAction dependency handling so that\n// the developer has the option to not provide any dependencies.\n// In this case, we assume they want to update the handler on each rerender.\n// To avoid getting stuck in an infinite loop, we update the handler directly,\n// skipping React state updates.\n// This is ok in this case, because the handler is not part of any UI that\n// needs to be updated.\n// useCallback, useMemo or other memoization techniques are not suitable here,\n// because they will cause a infinite rerender loop.\nexport function useCopilotAction<const T extends Parameter[] | [] = []>(\n action: FrontendAction<T>,\n dependencies?: any[],\n): void {\n const { setAction, removeAction, actions, chatComponentsCache } = useCopilotContext();\n const idRef = useRef<string>(randomId());\n const renderAndWaitRef = useRef<RenderAndWaitForResponse | null>(null);\n\n // clone the action to avoid mutating the original object\n action = { ...action };\n\n // If the developer provides a renderAndWait function, we transform the action\n // to use a promise internally, so that we can treat it like a normal action.\n if (action.renderAndWait || action.renderAndWaitForResponse) {\n const renderAndWait = action.renderAndWait || action.renderAndWaitForResponse;\n // remove the renderAndWait function from the action\n action.renderAndWait = undefined;\n action.renderAndWaitForResponse = undefined;\n // add a handler that will be called when the action is executed\n action.handler = useAsyncCallback(async () => {\n // we create a new promise when the handler is called\n let resolve: (result: any) => void;\n let reject: (error: any) => void;\n const promise = new Promise<any>((resolvePromise, rejectPromise) => {\n resolve = resolvePromise;\n reject = rejectPromise;\n });\n renderAndWaitRef.current = { promise, resolve: resolve!, reject: reject! };\n // then we await the promise (it will be resolved in the original renderAndWait function)\n return await promise;\n }, []) as any;\n\n // add a render function that will be called when the action is rendered\n action.render = ((props: ActionRenderProps<T>): React.ReactElement => {\n // Create type safe waitProps based on whether T extends empty array or not\n const waitProps = {\n status: props.status,\n args: props.args,\n result: props.result,\n handler: props.status === \"executing\" ? renderAndWaitRef.current!.resolve : undefined,\n respond: props.status === \"executing\" ? renderAndWaitRef.current!.resolve : undefined,\n } as T extends [] ? ActionRenderPropsNoArgsWait<T> : ActionRenderPropsWait<T>;\n\n // Type guard to check if renderAndWait is for no args case\n const isNoArgsRenderWait = (\n _fn:\n | ((props: ActionRenderPropsNoArgsWait<T>) => React.ReactElement)\n | ((props: ActionRenderPropsWait<T>) => React.ReactElement),\n ): _fn is (props: ActionRenderPropsNoArgsWait<T>) => React.ReactElement => {\n return action.parameters?.length === 0;\n };\n\n // Safely call renderAndWait with correct props type\n if (renderAndWait) {\n if (isNoArgsRenderWait(renderAndWait)) {\n return renderAndWait(waitProps as ActionRenderPropsNoArgsWait<T>);\n } else {\n return renderAndWait(waitProps as ActionRenderPropsWait<T>);\n }\n }\n\n // Return empty Fragment instead of null\n return createElement(Fragment);\n }) as any;\n }\n\n // If the developer doesn't provide dependencies, we assume they want to\n // update handler and render function when the action object changes.\n // This ensures that any captured variables in the handler are up to date.\n if (dependencies === undefined) {\n if (actions[idRef.current]) {\n actions[idRef.current].handler = action.handler as any;\n if (typeof action.render === \"function\") {\n if (chatComponentsCache.current !== null) {\n chatComponentsCache.current.actions[action.name] = action.render;\n }\n }\n }\n }\n\n useEffect(() => {\n setAction(idRef.current, action as any);\n if (chatComponentsCache.current !== null && action.render !== undefined) {\n chatComponentsCache.current.actions[action.name] = action.render;\n }\n return () => {\n // NOTE: For now, we don't remove the chatComponentsCache entry when the action is removed.\n // This is because we currently don't have access to the messages array in CopilotContext.\n removeAction(idRef.current);\n };\n }, [\n setAction,\n removeAction,\n action.description,\n action.name,\n action.disabled,\n action.available,\n // This should be faster than deep equality checking\n // In addition, all major JS engines guarantee the order of object keys\n JSON.stringify(action.parameters),\n // include render only if it's a string\n typeof action.render === \"string\" ? action.render : undefined,\n // dependencies set by the developer\n ...(dependencies || []),\n ]);\n}\n\ninterface RenderAndWaitForResponse {\n promise: Promise<any>;\n resolve: (result: any) => void;\n reject: (error: any) => void;\n}\n"],"mappings":";;;;;;;;;;;;AA2HA,SAAoB,gBAAgB;AACpC,SAAS,eAAe,UAAU,WAAW,cAAc;AAmBpD,SAAS,iBACd,QACA,cACM;AACN,QAAM,EAAE,WAAW,cAAc,SAAS,oBAAoB,IAAI,kBAAkB;AACpF,QAAM,QAAQ,OAAe,SAAS,CAAC;AACvC,QAAM,mBAAmB,OAAwC,IAAI;AAGrE,WAAS,mBAAK;AAId,MAAI,OAAO,iBAAiB,OAAO,0BAA0B;AAC3D,UAAM,gBAAgB,OAAO,iBAAiB,OAAO;AAErD,WAAO,gBAAgB;AACvB,WAAO,2BAA2B;AAElC,WAAO,UAAU,iBAAiB,MAAY;AAE5C,UAAI;AACJ,UAAI;AACJ,YAAM,UAAU,IAAI,QAAa,CAAC,gBAAgB,kBAAkB;AAClE,kBAAU;AACV,iBAAS;AAAA,MACX,CAAC;AACD,uBAAiB,UAAU,EAAE,SAAS,SAAmB,OAAgB;AAEzE,aAAO,MAAM;AAAA,IACf,IAAG,CAAC,CAAC;AAGL,WAAO,SAAU,CAAC,UAAoD;AAEpE,YAAM,YAAY;AAAA,QAChB,QAAQ,MAAM;AAAA,QACd,MAAM,MAAM;AAAA,QACZ,QAAQ,MAAM;AAAA,QACd,SAAS,MAAM,WAAW,cAAc,iBAAiB,QAAS,UAAU;AAAA,QAC5E,SAAS,MAAM,WAAW,cAAc,iBAAiB,QAAS,UAAU;AAAA,MAC9E;AAGA,YAAM,qBAAqB,CACzB,QAGyE;AA/LjF;AAgMQ,iBAAO,YAAO,eAAP,mBAAmB,YAAW;AAAA,MACvC;AAGA,UAAI,eAAe;AACjB,YAAI,mBAAmB,aAAa,GAAG;AACrC,iBAAO,cAAc,SAA2C;AAAA,QAClE,OAAO;AACL,iBAAO,cAAc,SAAqC;AAAA,QAC5D;AAAA,MACF;AAGA,aAAO,cAAc,QAAQ;AAAA,IAC/B;AAAA,EACF;AAKA,MAAI,iBAAiB,QAAW;AAC9B,QAAI,QAAQ,MAAM,OAAO,GAAG;AAC1B,cAAQ,MAAM,OAAO,EAAE,UAAU,OAAO;AACxC,UAAI,OAAO,OAAO,WAAW,YAAY;AACvC,YAAI,oBAAoB,YAAY,MAAM;AACxC,8BAAoB,QAAQ,QAAQ,OAAO,IAAI,IAAI,OAAO;AAAA,QAC5D;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,YAAU,MAAM;AACd,cAAU,MAAM,SAAS,MAAa;AACtC,QAAI,oBAAoB,YAAY,QAAQ,OAAO,WAAW,QAAW;AACvE,0BAAoB,QAAQ,QAAQ,OAAO,IAAI,IAAI,OAAO;AAAA,IAC5D;AACA,WAAO,MAAM;AAGX,mBAAa,MAAM,OAAO;AAAA,IAC5B;AAAA,EACF,GAAG;AAAA,IACD;AAAA,IACA;AAAA,IACA,OAAO;AAAA,IACP,OAAO;AAAA,IACP,OAAO;AAAA,IACP,OAAO;AAAA;AAAA;AAAA,IAGP,KAAK,UAAU,OAAO,UAAU;AAAA;AAAA,IAEhC,OAAO,OAAO,WAAW,WAAW,OAAO,SAAS;AAAA;AAAA,IAEpD,GAAI,gBAAgB,CAAC;AAAA,EACvB,CAAC;AACH;","names":[]}
@@ -85,7 +85,7 @@ __export(copilotkit_exports, {
85
85
  defaultCopilotContextCategories: () => defaultCopilotContextCategories
86
86
  });
87
87
  module.exports = __toCommonJS(copilotkit_exports);
88
- var import_react8 = require("react");
88
+ var import_react10 = require("react");
89
89
 
90
90
  // src/context/copilot-context.tsx
91
91
  var import_react = __toESM(require("react"));
@@ -379,6 +379,9 @@ function CopilotMessages(_a) {
379
379
  }
380
380
 
381
381
  // src/components/toast/toast-provider.tsx
382
+ var import_react7 = require("react");
383
+
384
+ // src/components/error-boundary/error-utils.tsx
382
385
  var import_react6 = require("react");
383
386
 
384
387
  // src/components/toast/exclamation-mark-icon.tsx
@@ -408,11 +411,82 @@ var ExclamationMarkIcon = ({
408
411
  }
409
412
  );
410
413
 
411
- // src/components/toast/toast-provider.tsx
414
+ // src/components/error-boundary/error-utils.tsx
412
415
  var import_jsx_runtime3 = require("react/jsx-runtime");
413
- var ToastContext = (0, import_react6.createContext)(void 0);
416
+ function ErrorToast({ errors }) {
417
+ const errorsToRender = errors.map((error, idx) => {
418
+ var _a, _b, _c;
419
+ const message = "extensions" in error ? (_b = (_a = error.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message : error.message;
420
+ const code = "extensions" in error ? (_c = error.extensions) == null ? void 0 : _c.code : null;
421
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
422
+ "div",
423
+ {
424
+ style: {
425
+ marginTop: idx === 0 ? 0 : 10,
426
+ marginBottom: 14
427
+ },
428
+ children: [
429
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ExclamationMarkIcon, { style: { marginBottom: 4 } }),
430
+ code && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
431
+ "div",
432
+ {
433
+ style: {
434
+ fontWeight: "600",
435
+ marginBottom: 4
436
+ },
437
+ children: [
438
+ "Copilot Cloud Error:",
439
+ " ",
440
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { style: { fontFamily: "monospace", fontWeight: "normal" }, children: code })
441
+ ]
442
+ }
443
+ ),
444
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: message })
445
+ ]
446
+ },
447
+ idx
448
+ );
449
+ });
450
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
451
+ "div",
452
+ {
453
+ style: {
454
+ fontSize: "13px",
455
+ maxWidth: "600px"
456
+ },
457
+ children: [
458
+ errorsToRender,
459
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: { fontSize: "11px", opacity: 0.75 }, children: "NOTE: This error only displays during local development." })
460
+ ]
461
+ }
462
+ );
463
+ }
464
+ function useErrorToast() {
465
+ const { addToast } = useToast();
466
+ return (0, import_react6.useCallback)(
467
+ (error) => {
468
+ const errorId = error.map((err) => {
469
+ var _a, _b;
470
+ const message = "extensions" in err ? ((_b = (_a = err.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message) || err.message : err.message;
471
+ const stack = err.stack || "";
472
+ return btoa(message + stack).slice(0, 32);
473
+ }).join("|");
474
+ addToast({
475
+ type: "error",
476
+ id: errorId,
477
+ // Toast libraries typically dedupe by id
478
+ message: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ErrorToast, { errors: error })
479
+ });
480
+ },
481
+ [addToast]
482
+ );
483
+ }
484
+
485
+ // src/components/toast/toast-provider.tsx
486
+ var import_jsx_runtime4 = require("react/jsx-runtime");
487
+ var ToastContext = (0, import_react7.createContext)(void 0);
414
488
  function useToast() {
415
- const context = (0, import_react6.useContext)(ToastContext);
489
+ const context = (0, import_react7.useContext)(ToastContext);
416
490
  if (!context) {
417
491
  throw new Error("useToast must be used within a ToastProvider");
418
492
  }
@@ -422,68 +496,28 @@ function ToastProvider({
422
496
  enabled,
423
497
  children
424
498
  }) {
425
- const [toasts, setToasts] = (0, import_react6.useState)([]);
426
- const addToast = (0, import_react6.useCallback)((toast) => {
427
- const id = Math.random().toString(36).substring(2, 9);
428
- setToasts((currentToasts) => [...currentToasts, __spreadProps(__spreadValues({}, toast), { id })]);
499
+ const [toasts, setToasts] = (0, import_react7.useState)([]);
500
+ const addToast = (0, import_react7.useCallback)((toast) => {
501
+ var _a;
502
+ const id = (_a = toast.id) != null ? _a : Math.random().toString(36).substring(2, 9);
503
+ setToasts((currentToasts) => {
504
+ if (currentToasts.find((toast2) => toast2.id === id))
505
+ return currentToasts;
506
+ return [...currentToasts, __spreadProps(__spreadValues({}, toast), { id })];
507
+ });
429
508
  if (toast.duration) {
430
509
  setTimeout(() => {
431
510
  removeToast(id);
432
511
  }, toast.duration);
433
512
  }
434
513
  }, []);
435
- const addGraphQLErrorsToast = (0, import_react6.useCallback)((errors) => {
436
- const errorsToRender = errors.map((error, idx) => {
437
- var _a;
438
- const message = error.message;
439
- const code = (_a = error.extensions) == null ? void 0 : _a.code;
440
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
441
- "div",
442
- {
443
- style: {
444
- marginTop: idx === 0 ? 0 : 10,
445
- marginBottom: 14
446
- },
447
- children: [
448
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ExclamationMarkIcon, { style: { marginBottom: 4 } }),
449
- code && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
450
- "div",
451
- {
452
- style: {
453
- fontWeight: "600",
454
- marginBottom: 4
455
- },
456
- children: [
457
- "Copilot Cloud Error:",
458
- " ",
459
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { style: { fontFamily: "monospace", fontWeight: "normal" }, children: code })
460
- ]
461
- }
462
- ),
463
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: message })
464
- ]
465
- },
466
- idx
467
- );
468
- });
514
+ const addGraphQLErrorsToast = (0, import_react7.useCallback)((errors) => {
469
515
  addToast({
470
516
  type: "error",
471
- message: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
472
- "div",
473
- {
474
- style: {
475
- fontSize: "13px",
476
- maxWidth: "600px"
477
- },
478
- children: [
479
- errorsToRender,
480
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: { fontSize: "11px", opacity: 0.75 }, children: "NOTE: This is a Copilot Cloud error, and it only displays during local development." })
481
- ]
482
- }
483
- )
517
+ message: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(ErrorToast, { errors })
484
518
  });
485
519
  }, []);
486
- const removeToast = (0, import_react6.useCallback)((id) => {
520
+ const removeToast = (0, import_react7.useCallback)((id) => {
487
521
  setToasts((currentToasts) => currentToasts.filter((toast) => toast.id !== id));
488
522
  }, []);
489
523
  const value = {
@@ -493,8 +527,8 @@ function ToastProvider({
493
527
  removeToast,
494
528
  enabled
495
529
  };
496
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(ToastContext.Provider, { value, children: [
497
- /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
530
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(ToastContext.Provider, { value, children: [
531
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
498
532
  "div",
499
533
  {
500
534
  style: {
@@ -508,7 +542,7 @@ function ToastProvider({
508
542
  gap: "0.5rem"
509
543
  },
510
544
  children: [
511
- toasts.length > 1 && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: { textAlign: "right" }, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
545
+ toasts.length > 1 && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { style: { textAlign: "right" }, children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
512
546
  "button",
513
547
  {
514
548
  onClick: () => setToasts([]),
@@ -523,7 +557,7 @@ function ToastProvider({
523
557
  children: "Close All"
524
558
  }
525
559
  ) }),
526
- toasts.map((toast) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
560
+ toasts.map((toast) => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
527
561
  Toast,
528
562
  {
529
563
  message: toast.message,
@@ -549,7 +583,7 @@ function Toast({
549
583
  warning: "#eab308",
550
584
  error: "#ef4444"
551
585
  };
552
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
586
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
553
587
  "div",
554
588
  {
555
589
  style: {
@@ -562,8 +596,8 @@ function Toast({
562
596
  minWidth: "200px"
563
597
  },
564
598
  children: [
565
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: message }),
566
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
599
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { children: message }),
600
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
567
601
  "button",
568
602
  {
569
603
  onClick: onClose,
@@ -588,14 +622,17 @@ function Toast({
588
622
 
589
623
  // src/hooks/use-copilot-runtime-client.ts
590
624
  var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
591
- var import_react7 = require("react");
625
+ var import_react8 = require("react");
592
626
  var useCopilotRuntimeClient = (options) => {
593
627
  const { addGraphQLErrorsToast } = useToast();
594
- const runtimeClient = (0, import_react7.useMemo)(() => {
628
+ const addErrorToast = useErrorToast();
629
+ const runtimeClient = (0, import_react8.useMemo)(() => {
595
630
  return new import_runtime_client_gql.CopilotRuntimeClient(__spreadProps(__spreadValues({}, options), {
596
631
  handleGQLErrors: (error) => {
597
632
  if (error.graphQLErrors.length) {
598
633
  addGraphQLErrorsToast(error.graphQLErrors);
634
+ } else {
635
+ addErrorToast([error]);
599
636
  }
600
637
  }
601
638
  }));
@@ -617,13 +654,41 @@ function getHostname() {
617
654
  return "";
618
655
  }
619
656
 
657
+ // src/components/error-boundary/error-boundary.tsx
658
+ var import_react9 = __toESM(require("react"));
659
+ var import_jsx_runtime5 = require("react/jsx-runtime");
660
+ var CopilotErrorBoundary = class extends import_react9.default.Component {
661
+ constructor(props) {
662
+ super(props);
663
+ this.state = { hasError: false };
664
+ }
665
+ static getDerivedStateFromError(error) {
666
+ return { hasError: true, error };
667
+ }
668
+ componentDidCatch(error, errorInfo) {
669
+ console.error("CopilotKit Error:", error, errorInfo);
670
+ }
671
+ render() {
672
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(ErrorToast2, { error: this.state.error, children: this.props.children });
673
+ }
674
+ };
675
+ function ErrorToast2({ error, children }) {
676
+ const addErrorToast = useErrorToast();
677
+ (0, import_react9.useEffect)(() => {
678
+ if (error) {
679
+ addErrorToast([error]);
680
+ }
681
+ }, [error, addErrorToast]);
682
+ return children;
683
+ }
684
+
620
685
  // src/components/copilot-provider/copilotkit.tsx
621
- var import_jsx_runtime4 = require("react/jsx-runtime");
686
+ var import_jsx_runtime6 = require("react/jsx-runtime");
622
687
  function CopilotKit(_a) {
623
688
  var _b = _a, { children } = _b, props = __objRest(_b, ["children"]);
624
689
  const showDevConsole = props.showDevConsole === void 0 ? "auto" : props.showDevConsole;
625
690
  const enabled = shouldShowDevConsole(showDevConsole);
626
- return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(ToastProvider, { enabled, children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(CopilotKitInternal, __spreadProps(__spreadValues({}, props), { children })) });
691
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(ToastProvider, { enabled, children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(CopilotErrorBoundary, { children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(CopilotKitInternal, __spreadProps(__spreadValues({}, props), { children })) }) });
627
692
  }
628
693
  function CopilotKitInternal(_a) {
629
694
  var _b = _a, { children } = _b, props = __objRest(_b, ["children"]);
@@ -633,49 +698,49 @@ function CopilotKitInternal(_a) {
633
698
  );
634
699
  }
635
700
  const chatApiEndpoint = props.runtimeUrl || import_shared3.COPILOT_CLOUD_CHAT_URL;
636
- const [actions, setActions] = (0, import_react8.useState)({});
637
- const [coAgentStateRenders, setCoAgentStateRenders] = (0, import_react8.useState)({});
638
- const chatComponentsCache = (0, import_react8.useRef)({
701
+ const [actions, setActions] = (0, import_react10.useState)({});
702
+ const [coAgentStateRenders, setCoAgentStateRenders] = (0, import_react10.useState)({});
703
+ const chatComponentsCache = (0, import_react10.useRef)({
639
704
  actions: {},
640
705
  coAgentStateRenders: {}
641
706
  });
642
707
  const { addElement, removeElement, printTree } = use_tree_default();
643
- const [isLoading, setIsLoading] = (0, import_react8.useState)(false);
644
- const [chatInstructions, setChatInstructions] = (0, import_react8.useState)("");
708
+ const [isLoading, setIsLoading] = (0, import_react10.useState)(false);
709
+ const [chatInstructions, setChatInstructions] = (0, import_react10.useState)("");
645
710
  const {
646
711
  addElement: addDocument,
647
712
  removeElement: removeDocument,
648
713
  allElements: allDocuments
649
714
  } = use_flat_category_store_default();
650
- const setAction = (0, import_react8.useCallback)((id, action) => {
715
+ const setAction = (0, import_react10.useCallback)((id, action) => {
651
716
  setActions((prevPoints) => {
652
717
  return __spreadProps(__spreadValues({}, prevPoints), {
653
718
  [id]: action
654
719
  });
655
720
  });
656
721
  }, []);
657
- const removeAction = (0, import_react8.useCallback)((id) => {
722
+ const removeAction = (0, import_react10.useCallback)((id) => {
658
723
  setActions((prevPoints) => {
659
724
  const newPoints = __spreadValues({}, prevPoints);
660
725
  delete newPoints[id];
661
726
  return newPoints;
662
727
  });
663
728
  }, []);
664
- const setCoAgentStateRender = (0, import_react8.useCallback)((id, stateRender) => {
729
+ const setCoAgentStateRender = (0, import_react10.useCallback)((id, stateRender) => {
665
730
  setCoAgentStateRenders((prevPoints) => {
666
731
  return __spreadProps(__spreadValues({}, prevPoints), {
667
732
  [id]: stateRender
668
733
  });
669
734
  });
670
735
  }, []);
671
- const removeCoAgentStateRender = (0, import_react8.useCallback)((id) => {
736
+ const removeCoAgentStateRender = (0, import_react10.useCallback)((id) => {
672
737
  setCoAgentStateRenders((prevPoints) => {
673
738
  const newPoints = __spreadValues({}, prevPoints);
674
739
  delete newPoints[id];
675
740
  return newPoints;
676
741
  });
677
742
  }, []);
678
- const getContextString = (0, import_react8.useCallback)(
743
+ const getContextString = (0, import_react10.useCallback)(
679
744
  (documents, categories) => {
680
745
  const documentsString = documents.map((document) => {
681
746
  return `${document.name} (${document.sourceApplication}):
@@ -688,37 +753,37 @@ ${nonDocumentStrings}`;
688
753
  },
689
754
  [printTree]
690
755
  );
691
- const addContext = (0, import_react8.useCallback)(
756
+ const addContext = (0, import_react10.useCallback)(
692
757
  (context, parentId, categories = defaultCopilotContextCategories) => {
693
758
  return addElement(context, categories, parentId);
694
759
  },
695
760
  [addElement]
696
761
  );
697
- const removeContext = (0, import_react8.useCallback)(
762
+ const removeContext = (0, import_react10.useCallback)(
698
763
  (id) => {
699
764
  removeElement(id);
700
765
  },
701
766
  [removeElement]
702
767
  );
703
- const getFunctionCallHandler = (0, import_react8.useCallback)(
768
+ const getFunctionCallHandler = (0, import_react10.useCallback)(
704
769
  (customEntryPoints) => {
705
770
  return entryPointsToFunctionCallHandler(Object.values(customEntryPoints || actions));
706
771
  },
707
772
  [actions]
708
773
  );
709
- const getDocumentsContext = (0, import_react8.useCallback)(
774
+ const getDocumentsContext = (0, import_react10.useCallback)(
710
775
  (categories) => {
711
776
  return allDocuments(categories);
712
777
  },
713
778
  [allDocuments]
714
779
  );
715
- const addDocumentContext = (0, import_react8.useCallback)(
780
+ const addDocumentContext = (0, import_react10.useCallback)(
716
781
  (documentPointer, categories = defaultCopilotContextCategories) => {
717
782
  return addDocument(documentPointer, categories);
718
783
  },
719
784
  [addDocument]
720
785
  );
721
- const removeDocumentContext = (0, import_react8.useCallback)(
786
+ const removeDocumentContext = (0, import_react10.useCallback)(
722
787
  (documentId) => {
723
788
  removeDocument(documentId);
724
789
  },
@@ -731,7 +796,7 @@ ${nonDocumentStrings}`;
731
796
  );
732
797
  }
733
798
  }
734
- const copilotApiConfig = (0, import_react8.useMemo)(() => {
799
+ const copilotApiConfig = (0, import_react10.useMemo)(() => {
735
800
  var _a2, _b2;
736
801
  let cloud = void 0;
737
802
  if (props.publicApiKey) {
@@ -773,7 +838,7 @@ ${nonDocumentStrings}`;
773
838
  headers,
774
839
  credentials: copilotApiConfig.credentials
775
840
  });
776
- const [chatSuggestionConfiguration, setChatSuggestionConfiguration] = (0, import_react8.useState)({});
841
+ const [chatSuggestionConfiguration, setChatSuggestionConfiguration] = (0, import_react10.useState)({});
777
842
  const addChatSuggestionConfiguration = (id, suggestion) => {
778
843
  setChatSuggestionConfiguration((prev) => __spreadProps(__spreadValues({}, prev), { [id]: suggestion }));
779
844
  };
@@ -783,9 +848,9 @@ ${nonDocumentStrings}`;
783
848
  return rest;
784
849
  });
785
850
  };
786
- const [coagentStates, setCoagentStates] = (0, import_react8.useState)({});
787
- const coagentStatesRef = (0, import_react8.useRef)({});
788
- const setCoagentStatesWithRef = (0, import_react8.useCallback)(
851
+ const [coagentStates, setCoagentStates] = (0, import_react10.useState)({});
852
+ const coagentStatesRef = (0, import_react10.useRef)({});
853
+ const setCoagentStatesWithRef = (0, import_react10.useCallback)(
789
854
  (value) => {
790
855
  const newValue = typeof value === "function" ? value(coagentStatesRef.current) : value;
791
856
  coagentStatesRef.current = newValue;
@@ -801,9 +866,9 @@ ${nonDocumentStrings}`;
801
866
  agentName: props.agent
802
867
  };
803
868
  }
804
- const [agentSession, setAgentSession] = (0, import_react8.useState)(initialAgentSession);
869
+ const [agentSession, setAgentSession] = (0, import_react10.useState)(initialAgentSession);
805
870
  const showDevConsole = props.showDevConsole === void 0 ? "auto" : props.showDevConsole;
806
- return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
871
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
807
872
  CopilotContext.Provider,
808
873
  {
809
874
  value: {
@@ -838,7 +903,7 @@ ${nonDocumentStrings}`;
838
903
  setAgentSession,
839
904
  runtimeClient
840
905
  },
841
- children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(CopilotMessages, { children })
906
+ children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(CopilotMessages, { children })
842
907
  }
843
908
  );
844
909
  }