@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.
- package/CHANGELOG.md +8 -3
- package/dist/{chunk-XBVKTDXP.mjs → chunk-6EMLM6WX.mjs} +17 -14
- package/dist/chunk-6EMLM6WX.mjs.map +1 -0
- package/dist/{chunk-ODN4H66E.mjs → chunk-7LRDVJH5.mjs} +6 -2
- package/dist/chunk-7LRDVJH5.mjs.map +1 -0
- package/dist/{chunk-O22KGHOQ.mjs → chunk-FSC4A3JN.mjs} +17 -13
- package/dist/chunk-FSC4A3JN.mjs.map +1 -0
- package/dist/{chunk-OT67R4NB.mjs → chunk-IFTHM7LF.mjs} +2 -2
- package/dist/{chunk-DHGXL5PC.mjs → chunk-IVYL7JRC.mjs} +7 -4
- package/dist/chunk-IVYL7JRC.mjs.map +1 -0
- package/dist/chunk-NTLCOVE5.mjs +321 -0
- package/dist/chunk-NTLCOVE5.mjs.map +1 -0
- package/dist/chunk-QCUP6HLK.mjs +37 -0
- package/dist/chunk-QCUP6HLK.mjs.map +1 -0
- package/dist/{chunk-2KCEHGSI.mjs → chunk-SFPANIOY.mjs} +99 -49
- package/dist/chunk-SFPANIOY.mjs.map +1 -0
- package/dist/{chunk-JHEAUB3Z.mjs → chunk-X6ZF5WAX.mjs} +1 -1
- package/dist/{chunk-JHEAUB3Z.mjs.map → chunk-X6ZF5WAX.mjs.map} +1 -1
- package/dist/{chunk-Y7MI4PBB.mjs → chunk-YUY5ZAST.mjs} +6 -3
- package/dist/chunk-YUY5ZAST.mjs.map +1 -0
- package/dist/components/copilot-provider/copilotkit.js +156 -91
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +4 -3
- package/dist/components/copilot-provider/index.js +156 -91
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +4 -3
- package/dist/components/error-boundary/error-boundary.d.ts +22 -0
- package/dist/components/error-boundary/error-boundary.js +183 -0
- package/dist/components/error-boundary/error-boundary.js.map +1 -0
- package/dist/components/error-boundary/error-boundary.mjs +12 -0
- package/dist/components/error-boundary/error-boundary.mjs.map +1 -0
- package/dist/components/error-boundary/error-utils.d.ts +11 -0
- package/dist/components/error-boundary/error-utils.js +177 -0
- package/dist/components/error-boundary/error-utils.js.map +1 -0
- package/dist/components/error-boundary/error-utils.mjs +13 -0
- package/dist/components/error-boundary/error-utils.mjs.map +1 -0
- package/dist/components/index.js +156 -91
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +4 -3
- package/dist/components/toast/toast-provider.d.ts +2 -1
- package/dist/components/toast/toast-provider.js +76 -62
- package/dist/components/toast/toast-provider.js.map +1 -1
- package/dist/components/toast/toast-provider.mjs +1 -1
- package/dist/hooks/index.js +398 -253
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +9 -8
- package/dist/hooks/use-chat.js +343 -199
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +3 -3
- package/dist/hooks/use-coagent-state-render.d.ts +2 -2
- package/dist/hooks/use-coagent-state-render.js.map +1 -1
- package/dist/hooks/use-coagent-state-render.mjs +1 -1
- package/dist/hooks/use-coagent.js +378 -233
- package/dist/hooks/use-coagent.js.map +1 -1
- package/dist/hooks/use-coagent.mjs +7 -6
- package/dist/hooks/use-copilot-action.js +132 -7
- package/dist/hooks/use-copilot-action.js.map +1 -1
- package/dist/hooks/use-copilot-action.mjs +3 -1
- package/dist/hooks/use-copilot-chat.js +367 -223
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +6 -5
- package/dist/hooks/use-copilot-runtime-client.js +110 -4
- package/dist/hooks/use-copilot-runtime-client.js.map +1 -1
- package/dist/hooks/use-copilot-runtime-client.mjs +2 -2
- package/dist/index.js +439 -335
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +10 -9
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +5 -4
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +5 -4
- package/dist/utils/extract.js.map +1 -1
- package/dist/utils/extract.mjs +4 -3
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +4 -3
- package/package.json +3 -3
- package/src/components/copilot-provider/copilotkit.tsx +4 -1
- package/src/components/error-boundary/error-boundary.tsx +42 -0
- package/src/components/error-boundary/error-utils.tsx +95 -0
- package/src/components/toast/toast-provider.tsx +10 -49
- package/src/hooks/use-chat.ts +286 -259
- package/src/hooks/use-coagent-state-render.ts +2 -2
- package/src/hooks/use-coagent.ts +14 -11
- package/src/hooks/use-copilot-action.ts +3 -2
- package/src/hooks/use-copilot-chat.ts +9 -8
- package/src/hooks/use-copilot-runtime-client.ts +4 -0
- package/dist/chunk-2KCEHGSI.mjs.map +0 -1
- package/dist/chunk-DHGXL5PC.mjs.map +0 -1
- package/dist/chunk-EUIBVFV6.mjs +0 -294
- package/dist/chunk-EUIBVFV6.mjs.map +0 -1
- package/dist/chunk-O22KGHOQ.mjs.map +0 -1
- package/dist/chunk-ODN4H66E.mjs.map +0 -1
- package/dist/chunk-XBVKTDXP.mjs.map +0 -1
- package/dist/chunk-Y7MI4PBB.mjs.map +0 -1
- /package/dist/{chunk-OT67R4NB.mjs.map → chunk-IFTHM7LF.mjs.map} +0 -0
package/dist/index.js
CHANGED
|
@@ -101,7 +101,7 @@ __export(src_exports, {
|
|
|
101
101
|
module.exports = __toCommonJS(src_exports);
|
|
102
102
|
|
|
103
103
|
// src/components/copilot-provider/copilotkit.tsx
|
|
104
|
-
var
|
|
104
|
+
var import_react10 = require("react");
|
|
105
105
|
|
|
106
106
|
// src/context/copilot-context.tsx
|
|
107
107
|
var import_react = __toESM(require("react"));
|
|
@@ -411,6 +411,9 @@ function CopilotMessages(_a) {
|
|
|
411
411
|
}
|
|
412
412
|
|
|
413
413
|
// src/components/toast/toast-provider.tsx
|
|
414
|
+
var import_react7 = require("react");
|
|
415
|
+
|
|
416
|
+
// src/components/error-boundary/error-utils.tsx
|
|
414
417
|
var import_react6 = require("react");
|
|
415
418
|
|
|
416
419
|
// src/components/toast/exclamation-mark-icon.tsx
|
|
@@ -440,11 +443,94 @@ var ExclamationMarkIcon = ({
|
|
|
440
443
|
}
|
|
441
444
|
);
|
|
442
445
|
|
|
443
|
-
// src/components/
|
|
446
|
+
// src/components/error-boundary/error-utils.tsx
|
|
444
447
|
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
445
|
-
|
|
448
|
+
function ErrorToast({ errors }) {
|
|
449
|
+
const errorsToRender = errors.map((error, idx) => {
|
|
450
|
+
var _a, _b, _c;
|
|
451
|
+
const message = "extensions" in error ? (_b = (_a = error.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message : error.message;
|
|
452
|
+
const code = "extensions" in error ? (_c = error.extensions) == null ? void 0 : _c.code : null;
|
|
453
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
454
|
+
"div",
|
|
455
|
+
{
|
|
456
|
+
style: {
|
|
457
|
+
marginTop: idx === 0 ? 0 : 10,
|
|
458
|
+
marginBottom: 14
|
|
459
|
+
},
|
|
460
|
+
children: [
|
|
461
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ExclamationMarkIcon, { style: { marginBottom: 4 } }),
|
|
462
|
+
code && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
463
|
+
"div",
|
|
464
|
+
{
|
|
465
|
+
style: {
|
|
466
|
+
fontWeight: "600",
|
|
467
|
+
marginBottom: 4
|
|
468
|
+
},
|
|
469
|
+
children: [
|
|
470
|
+
"Copilot Cloud Error:",
|
|
471
|
+
" ",
|
|
472
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { style: { fontFamily: "monospace", fontWeight: "normal" }, children: code })
|
|
473
|
+
]
|
|
474
|
+
}
|
|
475
|
+
),
|
|
476
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: message })
|
|
477
|
+
]
|
|
478
|
+
},
|
|
479
|
+
idx
|
|
480
|
+
);
|
|
481
|
+
});
|
|
482
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
483
|
+
"div",
|
|
484
|
+
{
|
|
485
|
+
style: {
|
|
486
|
+
fontSize: "13px",
|
|
487
|
+
maxWidth: "600px"
|
|
488
|
+
},
|
|
489
|
+
children: [
|
|
490
|
+
errorsToRender,
|
|
491
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: { fontSize: "11px", opacity: 0.75 }, children: "NOTE: This error only displays during local development." })
|
|
492
|
+
]
|
|
493
|
+
}
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
function useErrorToast() {
|
|
497
|
+
const { addToast } = useToast();
|
|
498
|
+
return (0, import_react6.useCallback)(
|
|
499
|
+
(error) => {
|
|
500
|
+
const errorId = error.map((err) => {
|
|
501
|
+
var _a, _b;
|
|
502
|
+
const message = "extensions" in err ? ((_b = (_a = err.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message) || err.message : err.message;
|
|
503
|
+
const stack = err.stack || "";
|
|
504
|
+
return btoa(message + stack).slice(0, 32);
|
|
505
|
+
}).join("|");
|
|
506
|
+
addToast({
|
|
507
|
+
type: "error",
|
|
508
|
+
id: errorId,
|
|
509
|
+
// Toast libraries typically dedupe by id
|
|
510
|
+
message: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ErrorToast, { errors: error })
|
|
511
|
+
});
|
|
512
|
+
},
|
|
513
|
+
[addToast]
|
|
514
|
+
);
|
|
515
|
+
}
|
|
516
|
+
function useAsyncCallback(callback, deps) {
|
|
517
|
+
const addErrorToast = useErrorToast();
|
|
518
|
+
return (0, import_react6.useCallback)((...args) => __async(this, null, function* () {
|
|
519
|
+
try {
|
|
520
|
+
return yield callback(...args);
|
|
521
|
+
} catch (error) {
|
|
522
|
+
console.error("Error in async callback:", error);
|
|
523
|
+
addErrorToast([error]);
|
|
524
|
+
throw error;
|
|
525
|
+
}
|
|
526
|
+
}), deps);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// src/components/toast/toast-provider.tsx
|
|
530
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
531
|
+
var ToastContext = (0, import_react7.createContext)(void 0);
|
|
446
532
|
function useToast() {
|
|
447
|
-
const context = (0,
|
|
533
|
+
const context = (0, import_react7.useContext)(ToastContext);
|
|
448
534
|
if (!context) {
|
|
449
535
|
throw new Error("useToast must be used within a ToastProvider");
|
|
450
536
|
}
|
|
@@ -454,68 +540,28 @@ function ToastProvider({
|
|
|
454
540
|
enabled,
|
|
455
541
|
children
|
|
456
542
|
}) {
|
|
457
|
-
const [toasts, setToasts] = (0,
|
|
458
|
-
const addToast = (0,
|
|
459
|
-
|
|
460
|
-
|
|
543
|
+
const [toasts, setToasts] = (0, import_react7.useState)([]);
|
|
544
|
+
const addToast = (0, import_react7.useCallback)((toast) => {
|
|
545
|
+
var _a;
|
|
546
|
+
const id = (_a = toast.id) != null ? _a : Math.random().toString(36).substring(2, 9);
|
|
547
|
+
setToasts((currentToasts) => {
|
|
548
|
+
if (currentToasts.find((toast2) => toast2.id === id))
|
|
549
|
+
return currentToasts;
|
|
550
|
+
return [...currentToasts, __spreadProps(__spreadValues({}, toast), { id })];
|
|
551
|
+
});
|
|
461
552
|
if (toast.duration) {
|
|
462
553
|
setTimeout(() => {
|
|
463
554
|
removeToast(id);
|
|
464
555
|
}, toast.duration);
|
|
465
556
|
}
|
|
466
557
|
}, []);
|
|
467
|
-
const addGraphQLErrorsToast = (0,
|
|
468
|
-
const errorsToRender = errors.map((error, idx) => {
|
|
469
|
-
var _a;
|
|
470
|
-
const message = error.message;
|
|
471
|
-
const code = (_a = error.extensions) == null ? void 0 : _a.code;
|
|
472
|
-
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
473
|
-
"div",
|
|
474
|
-
{
|
|
475
|
-
style: {
|
|
476
|
-
marginTop: idx === 0 ? 0 : 10,
|
|
477
|
-
marginBottom: 14
|
|
478
|
-
},
|
|
479
|
-
children: [
|
|
480
|
-
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ExclamationMarkIcon, { style: { marginBottom: 4 } }),
|
|
481
|
-
code && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
482
|
-
"div",
|
|
483
|
-
{
|
|
484
|
-
style: {
|
|
485
|
-
fontWeight: "600",
|
|
486
|
-
marginBottom: 4
|
|
487
|
-
},
|
|
488
|
-
children: [
|
|
489
|
-
"Copilot Cloud Error:",
|
|
490
|
-
" ",
|
|
491
|
-
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { style: { fontFamily: "monospace", fontWeight: "normal" }, children: code })
|
|
492
|
-
]
|
|
493
|
-
}
|
|
494
|
-
),
|
|
495
|
-
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: message })
|
|
496
|
-
]
|
|
497
|
-
},
|
|
498
|
-
idx
|
|
499
|
-
);
|
|
500
|
-
});
|
|
558
|
+
const addGraphQLErrorsToast = (0, import_react7.useCallback)((errors) => {
|
|
501
559
|
addToast({
|
|
502
560
|
type: "error",
|
|
503
|
-
message: /* @__PURE__ */ (0,
|
|
504
|
-
"div",
|
|
505
|
-
{
|
|
506
|
-
style: {
|
|
507
|
-
fontSize: "13px",
|
|
508
|
-
maxWidth: "600px"
|
|
509
|
-
},
|
|
510
|
-
children: [
|
|
511
|
-
errorsToRender,
|
|
512
|
-
/* @__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." })
|
|
513
|
-
]
|
|
514
|
-
}
|
|
515
|
-
)
|
|
561
|
+
message: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(ErrorToast, { errors })
|
|
516
562
|
});
|
|
517
563
|
}, []);
|
|
518
|
-
const removeToast = (0,
|
|
564
|
+
const removeToast = (0, import_react7.useCallback)((id) => {
|
|
519
565
|
setToasts((currentToasts) => currentToasts.filter((toast) => toast.id !== id));
|
|
520
566
|
}, []);
|
|
521
567
|
const value = {
|
|
@@ -525,8 +571,8 @@ function ToastProvider({
|
|
|
525
571
|
removeToast,
|
|
526
572
|
enabled
|
|
527
573
|
};
|
|
528
|
-
return /* @__PURE__ */ (0,
|
|
529
|
-
/* @__PURE__ */ (0,
|
|
574
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(ToastContext.Provider, { value, children: [
|
|
575
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
|
|
530
576
|
"div",
|
|
531
577
|
{
|
|
532
578
|
style: {
|
|
@@ -540,7 +586,7 @@ function ToastProvider({
|
|
|
540
586
|
gap: "0.5rem"
|
|
541
587
|
},
|
|
542
588
|
children: [
|
|
543
|
-
toasts.length > 1 && /* @__PURE__ */ (0,
|
|
589
|
+
toasts.length > 1 && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { style: { textAlign: "right" }, children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
544
590
|
"button",
|
|
545
591
|
{
|
|
546
592
|
onClick: () => setToasts([]),
|
|
@@ -555,7 +601,7 @@ function ToastProvider({
|
|
|
555
601
|
children: "Close All"
|
|
556
602
|
}
|
|
557
603
|
) }),
|
|
558
|
-
toasts.map((toast) => /* @__PURE__ */ (0,
|
|
604
|
+
toasts.map((toast) => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
559
605
|
Toast,
|
|
560
606
|
{
|
|
561
607
|
message: toast.message,
|
|
@@ -581,7 +627,7 @@ function Toast({
|
|
|
581
627
|
warning: "#eab308",
|
|
582
628
|
error: "#ef4444"
|
|
583
629
|
};
|
|
584
|
-
return /* @__PURE__ */ (0,
|
|
630
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
|
|
585
631
|
"div",
|
|
586
632
|
{
|
|
587
633
|
style: {
|
|
@@ -594,8 +640,8 @@ function Toast({
|
|
|
594
640
|
minWidth: "200px"
|
|
595
641
|
},
|
|
596
642
|
children: [
|
|
597
|
-
/* @__PURE__ */ (0,
|
|
598
|
-
/* @__PURE__ */ (0,
|
|
643
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { children: message }),
|
|
644
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
599
645
|
"button",
|
|
600
646
|
{
|
|
601
647
|
onClick: onClose,
|
|
@@ -620,14 +666,17 @@ function Toast({
|
|
|
620
666
|
|
|
621
667
|
// src/hooks/use-copilot-runtime-client.ts
|
|
622
668
|
var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
|
|
623
|
-
var
|
|
669
|
+
var import_react8 = require("react");
|
|
624
670
|
var useCopilotRuntimeClient = (options) => {
|
|
625
671
|
const { addGraphQLErrorsToast } = useToast();
|
|
626
|
-
const
|
|
672
|
+
const addErrorToast = useErrorToast();
|
|
673
|
+
const runtimeClient = (0, import_react8.useMemo)(() => {
|
|
627
674
|
return new import_runtime_client_gql.CopilotRuntimeClient(__spreadProps(__spreadValues({}, options), {
|
|
628
675
|
handleGQLErrors: (error) => {
|
|
629
676
|
if (error.graphQLErrors.length) {
|
|
630
677
|
addGraphQLErrorsToast(error.graphQLErrors);
|
|
678
|
+
} else {
|
|
679
|
+
addErrorToast([error]);
|
|
631
680
|
}
|
|
632
681
|
}
|
|
633
682
|
}));
|
|
@@ -782,13 +831,41 @@ function getHostname() {
|
|
|
782
831
|
return "";
|
|
783
832
|
}
|
|
784
833
|
|
|
834
|
+
// src/components/error-boundary/error-boundary.tsx
|
|
835
|
+
var import_react9 = __toESM(require("react"));
|
|
836
|
+
var import_jsx_runtime5 = require("react/jsx-runtime");
|
|
837
|
+
var CopilotErrorBoundary = class extends import_react9.default.Component {
|
|
838
|
+
constructor(props) {
|
|
839
|
+
super(props);
|
|
840
|
+
this.state = { hasError: false };
|
|
841
|
+
}
|
|
842
|
+
static getDerivedStateFromError(error) {
|
|
843
|
+
return { hasError: true, error };
|
|
844
|
+
}
|
|
845
|
+
componentDidCatch(error, errorInfo) {
|
|
846
|
+
console.error("CopilotKit Error:", error, errorInfo);
|
|
847
|
+
}
|
|
848
|
+
render() {
|
|
849
|
+
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(ErrorToast2, { error: this.state.error, children: this.props.children });
|
|
850
|
+
}
|
|
851
|
+
};
|
|
852
|
+
function ErrorToast2({ error, children }) {
|
|
853
|
+
const addErrorToast = useErrorToast();
|
|
854
|
+
(0, import_react9.useEffect)(() => {
|
|
855
|
+
if (error) {
|
|
856
|
+
addErrorToast([error]);
|
|
857
|
+
}
|
|
858
|
+
}, [error, addErrorToast]);
|
|
859
|
+
return children;
|
|
860
|
+
}
|
|
861
|
+
|
|
785
862
|
// src/components/copilot-provider/copilotkit.tsx
|
|
786
|
-
var
|
|
863
|
+
var import_jsx_runtime6 = require("react/jsx-runtime");
|
|
787
864
|
function CopilotKit(_a) {
|
|
788
865
|
var _b = _a, { children } = _b, props = __objRest(_b, ["children"]);
|
|
789
866
|
const showDevConsole = props.showDevConsole === void 0 ? "auto" : props.showDevConsole;
|
|
790
867
|
const enabled = shouldShowDevConsole(showDevConsole);
|
|
791
|
-
return /* @__PURE__ */ (0,
|
|
868
|
+
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 })) }) });
|
|
792
869
|
}
|
|
793
870
|
function CopilotKitInternal(_a) {
|
|
794
871
|
var _b = _a, { children } = _b, props = __objRest(_b, ["children"]);
|
|
@@ -798,49 +875,49 @@ function CopilotKitInternal(_a) {
|
|
|
798
875
|
);
|
|
799
876
|
}
|
|
800
877
|
const chatApiEndpoint = props.runtimeUrl || import_shared4.COPILOT_CLOUD_CHAT_URL;
|
|
801
|
-
const [actions, setActions] = (0,
|
|
802
|
-
const [coAgentStateRenders, setCoAgentStateRenders] = (0,
|
|
803
|
-
const chatComponentsCache = (0,
|
|
878
|
+
const [actions, setActions] = (0, import_react10.useState)({});
|
|
879
|
+
const [coAgentStateRenders, setCoAgentStateRenders] = (0, import_react10.useState)({});
|
|
880
|
+
const chatComponentsCache = (0, import_react10.useRef)({
|
|
804
881
|
actions: {},
|
|
805
882
|
coAgentStateRenders: {}
|
|
806
883
|
});
|
|
807
884
|
const { addElement, removeElement, printTree } = use_tree_default();
|
|
808
|
-
const [isLoading, setIsLoading] = (0,
|
|
809
|
-
const [chatInstructions, setChatInstructions] = (0,
|
|
885
|
+
const [isLoading, setIsLoading] = (0, import_react10.useState)(false);
|
|
886
|
+
const [chatInstructions, setChatInstructions] = (0, import_react10.useState)("");
|
|
810
887
|
const {
|
|
811
888
|
addElement: addDocument,
|
|
812
889
|
removeElement: removeDocument,
|
|
813
890
|
allElements: allDocuments
|
|
814
891
|
} = use_flat_category_store_default();
|
|
815
|
-
const setAction = (0,
|
|
892
|
+
const setAction = (0, import_react10.useCallback)((id, action) => {
|
|
816
893
|
setActions((prevPoints) => {
|
|
817
894
|
return __spreadProps(__spreadValues({}, prevPoints), {
|
|
818
895
|
[id]: action
|
|
819
896
|
});
|
|
820
897
|
});
|
|
821
898
|
}, []);
|
|
822
|
-
const removeAction = (0,
|
|
899
|
+
const removeAction = (0, import_react10.useCallback)((id) => {
|
|
823
900
|
setActions((prevPoints) => {
|
|
824
901
|
const newPoints = __spreadValues({}, prevPoints);
|
|
825
902
|
delete newPoints[id];
|
|
826
903
|
return newPoints;
|
|
827
904
|
});
|
|
828
905
|
}, []);
|
|
829
|
-
const setCoAgentStateRender = (0,
|
|
906
|
+
const setCoAgentStateRender = (0, import_react10.useCallback)((id, stateRender) => {
|
|
830
907
|
setCoAgentStateRenders((prevPoints) => {
|
|
831
908
|
return __spreadProps(__spreadValues({}, prevPoints), {
|
|
832
909
|
[id]: stateRender
|
|
833
910
|
});
|
|
834
911
|
});
|
|
835
912
|
}, []);
|
|
836
|
-
const removeCoAgentStateRender = (0,
|
|
913
|
+
const removeCoAgentStateRender = (0, import_react10.useCallback)((id) => {
|
|
837
914
|
setCoAgentStateRenders((prevPoints) => {
|
|
838
915
|
const newPoints = __spreadValues({}, prevPoints);
|
|
839
916
|
delete newPoints[id];
|
|
840
917
|
return newPoints;
|
|
841
918
|
});
|
|
842
919
|
}, []);
|
|
843
|
-
const getContextString = (0,
|
|
920
|
+
const getContextString = (0, import_react10.useCallback)(
|
|
844
921
|
(documents, categories) => {
|
|
845
922
|
const documentsString = documents.map((document) => {
|
|
846
923
|
return `${document.name} (${document.sourceApplication}):
|
|
@@ -853,37 +930,37 @@ ${nonDocumentStrings}`;
|
|
|
853
930
|
},
|
|
854
931
|
[printTree]
|
|
855
932
|
);
|
|
856
|
-
const addContext = (0,
|
|
933
|
+
const addContext = (0, import_react10.useCallback)(
|
|
857
934
|
(context, parentId, categories = defaultCopilotContextCategories) => {
|
|
858
935
|
return addElement(context, categories, parentId);
|
|
859
936
|
},
|
|
860
937
|
[addElement]
|
|
861
938
|
);
|
|
862
|
-
const removeContext = (0,
|
|
939
|
+
const removeContext = (0, import_react10.useCallback)(
|
|
863
940
|
(id) => {
|
|
864
941
|
removeElement(id);
|
|
865
942
|
},
|
|
866
943
|
[removeElement]
|
|
867
944
|
);
|
|
868
|
-
const getFunctionCallHandler = (0,
|
|
945
|
+
const getFunctionCallHandler = (0, import_react10.useCallback)(
|
|
869
946
|
(customEntryPoints) => {
|
|
870
947
|
return entryPointsToFunctionCallHandler(Object.values(customEntryPoints || actions));
|
|
871
948
|
},
|
|
872
949
|
[actions]
|
|
873
950
|
);
|
|
874
|
-
const getDocumentsContext = (0,
|
|
951
|
+
const getDocumentsContext = (0, import_react10.useCallback)(
|
|
875
952
|
(categories) => {
|
|
876
953
|
return allDocuments(categories);
|
|
877
954
|
},
|
|
878
955
|
[allDocuments]
|
|
879
956
|
);
|
|
880
|
-
const addDocumentContext = (0,
|
|
957
|
+
const addDocumentContext = (0, import_react10.useCallback)(
|
|
881
958
|
(documentPointer, categories = defaultCopilotContextCategories) => {
|
|
882
959
|
return addDocument(documentPointer, categories);
|
|
883
960
|
},
|
|
884
961
|
[addDocument]
|
|
885
962
|
);
|
|
886
|
-
const removeDocumentContext = (0,
|
|
963
|
+
const removeDocumentContext = (0, import_react10.useCallback)(
|
|
887
964
|
(documentId) => {
|
|
888
965
|
removeDocument(documentId);
|
|
889
966
|
},
|
|
@@ -896,7 +973,7 @@ ${nonDocumentStrings}`;
|
|
|
896
973
|
);
|
|
897
974
|
}
|
|
898
975
|
}
|
|
899
|
-
const copilotApiConfig = (0,
|
|
976
|
+
const copilotApiConfig = (0, import_react10.useMemo)(() => {
|
|
900
977
|
var _a2, _b2;
|
|
901
978
|
let cloud = void 0;
|
|
902
979
|
if (props.publicApiKey) {
|
|
@@ -938,7 +1015,7 @@ ${nonDocumentStrings}`;
|
|
|
938
1015
|
headers,
|
|
939
1016
|
credentials: copilotApiConfig.credentials
|
|
940
1017
|
});
|
|
941
|
-
const [chatSuggestionConfiguration, setChatSuggestionConfiguration] = (0,
|
|
1018
|
+
const [chatSuggestionConfiguration, setChatSuggestionConfiguration] = (0, import_react10.useState)({});
|
|
942
1019
|
const addChatSuggestionConfiguration = (id, suggestion) => {
|
|
943
1020
|
setChatSuggestionConfiguration((prev) => __spreadProps(__spreadValues({}, prev), { [id]: suggestion }));
|
|
944
1021
|
};
|
|
@@ -948,9 +1025,9 @@ ${nonDocumentStrings}`;
|
|
|
948
1025
|
return rest;
|
|
949
1026
|
});
|
|
950
1027
|
};
|
|
951
|
-
const [coagentStates, setCoagentStates] = (0,
|
|
952
|
-
const coagentStatesRef = (0,
|
|
953
|
-
const setCoagentStatesWithRef = (0,
|
|
1028
|
+
const [coagentStates, setCoagentStates] = (0, import_react10.useState)({});
|
|
1029
|
+
const coagentStatesRef = (0, import_react10.useRef)({});
|
|
1030
|
+
const setCoagentStatesWithRef = (0, import_react10.useCallback)(
|
|
954
1031
|
(value) => {
|
|
955
1032
|
const newValue = typeof value === "function" ? value(coagentStatesRef.current) : value;
|
|
956
1033
|
coagentStatesRef.current = newValue;
|
|
@@ -966,9 +1043,9 @@ ${nonDocumentStrings}`;
|
|
|
966
1043
|
agentName: props.agent
|
|
967
1044
|
};
|
|
968
1045
|
}
|
|
969
|
-
const [agentSession, setAgentSession] = (0,
|
|
1046
|
+
const [agentSession, setAgentSession] = (0, import_react10.useState)(initialAgentSession);
|
|
970
1047
|
const showDevConsole = props.showDevConsole === void 0 ? "auto" : props.showDevConsole;
|
|
971
|
-
return /* @__PURE__ */ (0,
|
|
1048
|
+
return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
|
|
972
1049
|
CopilotContext.Provider,
|
|
973
1050
|
{
|
|
974
1051
|
value: {
|
|
@@ -1003,7 +1080,7 @@ ${nonDocumentStrings}`;
|
|
|
1003
1080
|
setAgentSession,
|
|
1004
1081
|
runtimeClient
|
|
1005
1082
|
},
|
|
1006
|
-
children: /* @__PURE__ */ (0,
|
|
1083
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(CopilotMessages, { children })
|
|
1007
1084
|
}
|
|
1008
1085
|
);
|
|
1009
1086
|
}
|
|
@@ -1035,11 +1112,11 @@ function entryPointsToFunctionCallHandler(actions) {
|
|
|
1035
1112
|
}
|
|
1036
1113
|
|
|
1037
1114
|
// src/hooks/use-copilot-chat.ts
|
|
1038
|
-
var
|
|
1115
|
+
var import_react12 = require("react");
|
|
1039
1116
|
var import_runtime_client_gql5 = require("@copilotkit/runtime-client-gql");
|
|
1040
1117
|
|
|
1041
1118
|
// src/hooks/use-chat.ts
|
|
1042
|
-
var
|
|
1119
|
+
var import_react11 = require("react");
|
|
1043
1120
|
var import_shared5 = require("@copilotkit/shared");
|
|
1044
1121
|
var import_runtime_client_gql4 = require("@copilotkit/runtime-client-gql");
|
|
1045
1122
|
function useChat(options) {
|
|
@@ -1059,12 +1136,12 @@ function useChat(options) {
|
|
|
1059
1136
|
agentSession,
|
|
1060
1137
|
setAgentSession
|
|
1061
1138
|
} = options;
|
|
1062
|
-
const abortControllerRef = (0,
|
|
1063
|
-
const threadIdRef = (0,
|
|
1064
|
-
const runIdRef = (0,
|
|
1139
|
+
const abortControllerRef = (0, import_react11.useRef)();
|
|
1140
|
+
const threadIdRef = (0, import_react11.useRef)(null);
|
|
1141
|
+
const runIdRef = (0, import_react11.useRef)(null);
|
|
1065
1142
|
const { addGraphQLErrorsToast } = useToast();
|
|
1066
|
-
const runChatCompletionRef = (0,
|
|
1067
|
-
const agentSessionRef = (0,
|
|
1143
|
+
const runChatCompletionRef = (0, import_react11.useRef)();
|
|
1144
|
+
const agentSessionRef = (0, import_react11.useRef)(agentSession);
|
|
1068
1145
|
agentSessionRef.current = agentSession;
|
|
1069
1146
|
const publicApiKey = copilotConfig.publicApiKey;
|
|
1070
1147
|
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [import_shared5.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
|
|
@@ -1074,210 +1151,236 @@ function useChat(options) {
|
|
|
1074
1151
|
headers,
|
|
1075
1152
|
credentials: copilotConfig.credentials
|
|
1076
1153
|
});
|
|
1077
|
-
const runChatCompletion = (
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
runtimeClient.
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
available
|
|
1103
|
-
|
|
1104
|
-
available
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1154
|
+
const runChatCompletion = useAsyncCallback(
|
|
1155
|
+
(previousMessages) => __async(this, null, function* () {
|
|
1156
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
1157
|
+
setIsLoading(true);
|
|
1158
|
+
let newMessages = [
|
|
1159
|
+
new import_runtime_client_gql4.TextMessage({
|
|
1160
|
+
content: "",
|
|
1161
|
+
role: import_runtime_client_gql4.Role.Assistant
|
|
1162
|
+
})
|
|
1163
|
+
];
|
|
1164
|
+
const abortController = new AbortController();
|
|
1165
|
+
abortControllerRef.current = abortController;
|
|
1166
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
1167
|
+
const systemMessage = makeSystemMessageCallback();
|
|
1168
|
+
const messagesWithContext = [systemMessage, ...initialMessages || [], ...previousMessages];
|
|
1169
|
+
const stream = runtimeClient.asStream(
|
|
1170
|
+
runtimeClient.generateCopilotResponse({
|
|
1171
|
+
data: __spreadProps(__spreadValues(__spreadProps(__spreadValues({
|
|
1172
|
+
frontend: {
|
|
1173
|
+
actions: actions.filter(
|
|
1174
|
+
(action) => action.available !== import_runtime_client_gql4.ActionInputAvailability.Disabled || !action.disabled
|
|
1175
|
+
).map((action) => {
|
|
1176
|
+
let available = import_runtime_client_gql4.ActionInputAvailability.Enabled;
|
|
1177
|
+
if (action.disabled) {
|
|
1178
|
+
available = import_runtime_client_gql4.ActionInputAvailability.Disabled;
|
|
1179
|
+
} else if (action.available === "disabled") {
|
|
1180
|
+
available = import_runtime_client_gql4.ActionInputAvailability.Disabled;
|
|
1181
|
+
} else if (action.available === "remote") {
|
|
1182
|
+
available = import_runtime_client_gql4.ActionInputAvailability.Remote;
|
|
1183
|
+
}
|
|
1184
|
+
return {
|
|
1185
|
+
name: action.name,
|
|
1186
|
+
description: action.description || "",
|
|
1187
|
+
jsonSchema: JSON.stringify(
|
|
1188
|
+
(0, import_shared5.actionParametersToJsonSchema)(action.parameters || [])
|
|
1189
|
+
),
|
|
1190
|
+
available
|
|
1191
|
+
};
|
|
1192
|
+
}),
|
|
1193
|
+
url: window.location.href
|
|
1194
|
+
},
|
|
1195
|
+
threadId: threadIdRef.current,
|
|
1196
|
+
runId: runIdRef.current,
|
|
1197
|
+
messages: (0, import_runtime_client_gql4.convertMessagesToGqlInput)((0, import_runtime_client_gql4.filterAgentStateMessages)(messagesWithContext))
|
|
1198
|
+
}, copilotConfig.cloud ? {
|
|
1199
|
+
cloud: __spreadValues({}, ((_c = (_b = (_a = copilotConfig.cloud.guardrails) == null ? void 0 : _a.input) == null ? void 0 : _b.restrictToTopic) == null ? void 0 : _c.enabled) ? {
|
|
1200
|
+
guardrails: {
|
|
1201
|
+
inputValidationRules: {
|
|
1202
|
+
allowList: copilotConfig.cloud.guardrails.input.restrictToTopic.validTopics,
|
|
1203
|
+
denyList: copilotConfig.cloud.guardrails.input.restrictToTopic.invalidTopics
|
|
1204
|
+
}
|
|
1124
1205
|
}
|
|
1206
|
+
} : {})
|
|
1207
|
+
} : {}), {
|
|
1208
|
+
metadata: {
|
|
1209
|
+
requestType: import_runtime_client_gql4.CopilotRequestType.Chat
|
|
1125
1210
|
}
|
|
1126
|
-
}
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1211
|
+
}), agentSessionRef.current ? {
|
|
1212
|
+
agentSession: agentSessionRef.current
|
|
1213
|
+
} : {}), {
|
|
1214
|
+
agentStates: Object.values(coagentStatesRef.current).map((state) => ({
|
|
1215
|
+
agentName: state.name,
|
|
1216
|
+
state: JSON.stringify(state.state)
|
|
1217
|
+
}))
|
|
1218
|
+
}),
|
|
1219
|
+
properties: copilotConfig.properties,
|
|
1220
|
+
signal: (_d = abortControllerRef.current) == null ? void 0 : _d.signal
|
|
1221
|
+
})
|
|
1222
|
+
);
|
|
1223
|
+
const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
|
|
1224
|
+
const reader = stream.getReader();
|
|
1225
|
+
let actionResults = {};
|
|
1226
|
+
let executedCoAgentStateRenders = [];
|
|
1227
|
+
let followUp = void 0;
|
|
1228
|
+
try {
|
|
1229
|
+
while (true) {
|
|
1230
|
+
let done, value;
|
|
1231
|
+
try {
|
|
1232
|
+
const readResult = yield reader.read();
|
|
1233
|
+
done = readResult.done;
|
|
1234
|
+
value = readResult.value;
|
|
1235
|
+
} catch (readError) {
|
|
1236
|
+
break;
|
|
1130
1237
|
}
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1238
|
+
if (done) {
|
|
1239
|
+
break;
|
|
1240
|
+
}
|
|
1241
|
+
if (!(value == null ? void 0 : value.generateCopilotResponse)) {
|
|
1242
|
+
continue;
|
|
1243
|
+
}
|
|
1244
|
+
threadIdRef.current = value.generateCopilotResponse.threadId || null;
|
|
1245
|
+
runIdRef.current = value.generateCopilotResponse.runId || null;
|
|
1246
|
+
const messages2 = (0, import_runtime_client_gql4.convertGqlOutputToMessages)(
|
|
1247
|
+
(0, import_runtime_client_gql4.filterAdjacentAgentStateMessages)(value.generateCopilotResponse.messages)
|
|
1248
|
+
);
|
|
1249
|
+
if (messages2.length === 0) {
|
|
1250
|
+
continue;
|
|
1251
|
+
}
|
|
1252
|
+
newMessages = [];
|
|
1253
|
+
if (((_h = value.generateCopilotResponse.status) == null ? void 0 : _h.__typename) === "FailedResponseStatus" && value.generateCopilotResponse.status.reason === "GUARDRAILS_VALIDATION_FAILED") {
|
|
1254
|
+
newMessages = [
|
|
1255
|
+
new import_runtime_client_gql4.TextMessage({
|
|
1256
|
+
role: import_runtime_client_gql4.MessageRole.Assistant,
|
|
1257
|
+
content: ((_i = value.generateCopilotResponse.status.details) == null ? void 0 : _i.guardrailsReason) || ""
|
|
1258
|
+
})
|
|
1259
|
+
];
|
|
1260
|
+
} else {
|
|
1261
|
+
for (const message of messages2) {
|
|
1262
|
+
newMessages.push(message);
|
|
1263
|
+
if (message.isActionExecutionMessage() && message.status.code !== import_runtime_client_gql4.MessageStatusCode.Pending && message.scope === "client" && onFunctionCall) {
|
|
1264
|
+
if (!(message.id in actionResults)) {
|
|
1265
|
+
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
1266
|
+
break;
|
|
1267
|
+
}
|
|
1268
|
+
try {
|
|
1269
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
1270
|
+
const action = actions.find((action2) => action2.name === message.name);
|
|
1271
|
+
if (action) {
|
|
1272
|
+
followUp = action.followUp;
|
|
1273
|
+
}
|
|
1274
|
+
const result = yield onFunctionCall({
|
|
1275
|
+
messages: previousMessages,
|
|
1276
|
+
name: message.name,
|
|
1277
|
+
args: message.arguments
|
|
1278
|
+
});
|
|
1279
|
+
actionResults[message.id] = result;
|
|
1280
|
+
} catch (e) {
|
|
1281
|
+
actionResults[message.id] = `Failed to execute action ${message.name}`;
|
|
1282
|
+
console.error(`Failed to execute action ${message.name}: ${e}`);
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
newMessages.push(
|
|
1286
|
+
new import_runtime_client_gql4.ResultMessage({
|
|
1287
|
+
result: import_runtime_client_gql4.ResultMessage.encodeResult(actionResults[message.id]),
|
|
1288
|
+
actionExecutionId: message.id,
|
|
1289
|
+
actionName: message.name
|
|
1290
|
+
})
|
|
1291
|
+
);
|
|
1292
|
+
}
|
|
1293
|
+
if (message.isAgentStateMessage() && !message.active && !executedCoAgentStateRenders.includes(message.id) && onCoAgentStateRender) {
|
|
1185
1294
|
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
1186
1295
|
break;
|
|
1187
1296
|
}
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
const result = yield onFunctionCall({
|
|
1195
|
-
messages: previousMessages,
|
|
1196
|
-
name: message.name,
|
|
1197
|
-
args: message.arguments
|
|
1198
|
-
});
|
|
1199
|
-
actionResults[message.id] = result;
|
|
1200
|
-
} catch (e) {
|
|
1201
|
-
actionResults[message.id] = `Failed to execute action ${message.name}`;
|
|
1202
|
-
console.error(`Failed to execute action ${message.name}: ${e}`);
|
|
1203
|
-
}
|
|
1297
|
+
yield onCoAgentStateRender({
|
|
1298
|
+
name: message.agentName,
|
|
1299
|
+
nodeName: message.nodeName,
|
|
1300
|
+
state: message.state
|
|
1301
|
+
});
|
|
1302
|
+
executedCoAgentStateRenders.push(message.id);
|
|
1204
1303
|
}
|
|
1205
|
-
newMessages.push(
|
|
1206
|
-
new import_runtime_client_gql4.ResultMessage({
|
|
1207
|
-
result: import_runtime_client_gql4.ResultMessage.encodeResult(actionResults[message.id]),
|
|
1208
|
-
actionExecutionId: message.id,
|
|
1209
|
-
actionName: message.name
|
|
1210
|
-
})
|
|
1211
|
-
);
|
|
1212
1304
|
}
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1305
|
+
const lastAgentStateMessage = [...messages2].reverse().find((message) => message.isAgentStateMessage());
|
|
1306
|
+
if (lastAgentStateMessage) {
|
|
1307
|
+
setCoagentStatesWithRef((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
1308
|
+
[lastAgentStateMessage.agentName]: {
|
|
1309
|
+
name: lastAgentStateMessage.agentName,
|
|
1310
|
+
state: lastAgentStateMessage.state,
|
|
1311
|
+
running: lastAgentStateMessage.running,
|
|
1312
|
+
active: lastAgentStateMessage.active,
|
|
1313
|
+
threadId: lastAgentStateMessage.threadId,
|
|
1314
|
+
nodeName: lastAgentStateMessage.nodeName,
|
|
1315
|
+
runId: lastAgentStateMessage.runId
|
|
1316
|
+
}
|
|
1317
|
+
}));
|
|
1318
|
+
if (lastAgentStateMessage.running) {
|
|
1319
|
+
setAgentSession({
|
|
1320
|
+
threadId: lastAgentStateMessage.threadId,
|
|
1321
|
+
agentName: lastAgentStateMessage.agentName,
|
|
1322
|
+
nodeName: lastAgentStateMessage.nodeName
|
|
1323
|
+
});
|
|
1324
|
+
} else {
|
|
1325
|
+
setAgentSession(null);
|
|
1216
1326
|
}
|
|
1217
|
-
yield onCoAgentStateRender({
|
|
1218
|
-
name: message.agentName,
|
|
1219
|
-
nodeName: message.nodeName,
|
|
1220
|
-
state: message.state
|
|
1221
|
-
});
|
|
1222
|
-
executedCoAgentStateRenders.push(message.id);
|
|
1223
1327
|
}
|
|
1224
1328
|
}
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
setCoagentStatesWithRef((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
1228
|
-
[lastAgentStateMessage.agentName]: {
|
|
1229
|
-
name: lastAgentStateMessage.agentName,
|
|
1230
|
-
state: lastAgentStateMessage.state,
|
|
1231
|
-
running: lastAgentStateMessage.running,
|
|
1232
|
-
active: lastAgentStateMessage.active,
|
|
1233
|
-
threadId: lastAgentStateMessage.threadId,
|
|
1234
|
-
nodeName: lastAgentStateMessage.nodeName,
|
|
1235
|
-
runId: lastAgentStateMessage.runId
|
|
1236
|
-
}
|
|
1237
|
-
}));
|
|
1238
|
-
if (lastAgentStateMessage.running) {
|
|
1239
|
-
setAgentSession({
|
|
1240
|
-
threadId: lastAgentStateMessage.threadId,
|
|
1241
|
-
agentName: lastAgentStateMessage.agentName,
|
|
1242
|
-
nodeName: lastAgentStateMessage.nodeName
|
|
1243
|
-
});
|
|
1244
|
-
} else {
|
|
1245
|
-
setAgentSession(null);
|
|
1246
|
-
}
|
|
1329
|
+
if (newMessages.length > 0) {
|
|
1330
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
1247
1331
|
}
|
|
1248
1332
|
}
|
|
1249
|
-
if (
|
|
1250
|
-
|
|
1333
|
+
if (
|
|
1334
|
+
// if followUp is not explicitly false
|
|
1335
|
+
followUp !== false && // if we have client side results
|
|
1336
|
+
(Object.values(actionResults).length || // or the last message we received is a result
|
|
1337
|
+
newMessages.length && newMessages[newMessages.length - 1].isResultMessage())
|
|
1338
|
+
) {
|
|
1339
|
+
yield new Promise((resolve) => setTimeout(resolve, 10));
|
|
1340
|
+
return yield runChatCompletionRef.current([...previousMessages, ...newMessages]);
|
|
1341
|
+
} else {
|
|
1342
|
+
return newMessages.slice();
|
|
1251
1343
|
}
|
|
1344
|
+
} finally {
|
|
1345
|
+
setIsLoading(false);
|
|
1252
1346
|
}
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1347
|
+
}),
|
|
1348
|
+
[
|
|
1349
|
+
messages,
|
|
1350
|
+
setMessages,
|
|
1351
|
+
makeSystemMessageCallback,
|
|
1352
|
+
copilotConfig,
|
|
1353
|
+
setIsLoading,
|
|
1354
|
+
initialMessages,
|
|
1355
|
+
isLoading,
|
|
1356
|
+
actions,
|
|
1357
|
+
onFunctionCall,
|
|
1358
|
+
onCoAgentStateRender,
|
|
1359
|
+
setCoagentStatesWithRef,
|
|
1360
|
+
coagentStatesRef,
|
|
1361
|
+
agentSession,
|
|
1362
|
+
setAgentSession
|
|
1363
|
+
]
|
|
1364
|
+
);
|
|
1268
1365
|
runChatCompletionRef.current = runChatCompletion;
|
|
1269
|
-
const runChatCompletionAndHandleFunctionCall = (
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1366
|
+
const runChatCompletionAndHandleFunctionCall = useAsyncCallback(
|
|
1367
|
+
(messages2) => __async(this, null, function* () {
|
|
1368
|
+
yield runChatCompletionRef.current(messages2);
|
|
1369
|
+
}),
|
|
1370
|
+
[messages]
|
|
1371
|
+
);
|
|
1372
|
+
const append = useAsyncCallback(
|
|
1373
|
+
(message) => __async(this, null, function* () {
|
|
1374
|
+
if (isLoading) {
|
|
1375
|
+
return;
|
|
1376
|
+
}
|
|
1377
|
+
const newMessages = [...messages, message];
|
|
1378
|
+
setMessages(newMessages);
|
|
1379
|
+
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
1380
|
+
}),
|
|
1381
|
+
[isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]
|
|
1382
|
+
);
|
|
1383
|
+
const reload = useAsyncCallback(() => __async(this, null, function* () {
|
|
1281
1384
|
if (isLoading || messages.length === 0) {
|
|
1282
1385
|
return;
|
|
1283
1386
|
}
|
|
@@ -1288,7 +1391,7 @@ function useChat(options) {
|
|
|
1288
1391
|
}
|
|
1289
1392
|
setMessages(newMessages);
|
|
1290
1393
|
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
1291
|
-
});
|
|
1394
|
+
}), [isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]);
|
|
1292
1395
|
const stop = () => {
|
|
1293
1396
|
var _a;
|
|
1294
1397
|
(_a = abortControllerRef.current) == null ? void 0 : _a.abort();
|
|
@@ -1324,13 +1427,13 @@ function useCopilotChat(_a = {}) {
|
|
|
1324
1427
|
} = useCopilotContext();
|
|
1325
1428
|
const { messages, setMessages } = useCopilotMessagesContext();
|
|
1326
1429
|
const latestGetContextString = useUpdatedRef(getContextString);
|
|
1327
|
-
const deleteMessage = (0,
|
|
1430
|
+
const deleteMessage = (0, import_react12.useCallback)(
|
|
1328
1431
|
(messageId) => {
|
|
1329
1432
|
setMessages((prev) => prev.filter((message) => message.id !== messageId));
|
|
1330
1433
|
},
|
|
1331
1434
|
[setMessages]
|
|
1332
1435
|
);
|
|
1333
|
-
const makeSystemMessageCallback = (0,
|
|
1436
|
+
const makeSystemMessageCallback = (0, import_react12.useCallback)(() => {
|
|
1334
1437
|
const systemMessageMaker = makeSystemMessage2 || defaultSystemMessage;
|
|
1335
1438
|
const contextString = latestGetContextString.current([], defaultCopilotContextCategories);
|
|
1336
1439
|
return new import_runtime_client_gql5.TextMessage({
|
|
@@ -1338,7 +1441,7 @@ function useCopilotChat(_a = {}) {
|
|
|
1338
1441
|
role: import_runtime_client_gql5.Role.System
|
|
1339
1442
|
});
|
|
1340
1443
|
}, [getContextString, makeSystemMessage2, chatInstructions]);
|
|
1341
|
-
const onCoAgentStateRender = (
|
|
1444
|
+
const onCoAgentStateRender = useAsyncCallback(
|
|
1342
1445
|
(args) => __async(this, null, function* () {
|
|
1343
1446
|
var _a2;
|
|
1344
1447
|
const { name, nodeName, state } = args;
|
|
@@ -1373,38 +1476,38 @@ function useCopilotChat(_a = {}) {
|
|
|
1373
1476
|
setAgentSession
|
|
1374
1477
|
}));
|
|
1375
1478
|
const latestAppend = useUpdatedRef(append);
|
|
1376
|
-
const latestAppendFunc = (
|
|
1377
|
-
(message) => {
|
|
1378
|
-
return latestAppend.current(message);
|
|
1379
|
-
},
|
|
1479
|
+
const latestAppendFunc = useAsyncCallback(
|
|
1480
|
+
(message) => __async(this, null, function* () {
|
|
1481
|
+
return yield latestAppend.current(message);
|
|
1482
|
+
}),
|
|
1380
1483
|
[latestAppend]
|
|
1381
1484
|
);
|
|
1382
1485
|
const latestReload = useUpdatedRef(reload);
|
|
1383
|
-
const latestReloadFunc = (
|
|
1384
|
-
return latestReload.current();
|
|
1385
|
-
}, [latestReload]);
|
|
1486
|
+
const latestReloadFunc = useAsyncCallback(() => __async(this, null, function* () {
|
|
1487
|
+
return yield latestReload.current();
|
|
1488
|
+
}), [latestReload]);
|
|
1386
1489
|
const latestStop = useUpdatedRef(stop);
|
|
1387
|
-
const latestStopFunc = (0,
|
|
1490
|
+
const latestStopFunc = (0, import_react12.useCallback)(() => {
|
|
1388
1491
|
return latestStop.current();
|
|
1389
1492
|
}, [latestStop]);
|
|
1390
1493
|
const latestDelete = useUpdatedRef(deleteMessage);
|
|
1391
|
-
const latestDeleteFunc = (0,
|
|
1494
|
+
const latestDeleteFunc = (0, import_react12.useCallback)(
|
|
1392
1495
|
(messageId) => {
|
|
1393
1496
|
return latestDelete.current(messageId);
|
|
1394
1497
|
},
|
|
1395
1498
|
[latestDelete]
|
|
1396
1499
|
);
|
|
1397
1500
|
const latestSetMessages = useUpdatedRef(setMessages);
|
|
1398
|
-
const latestSetMessagesFunc = (0,
|
|
1501
|
+
const latestSetMessagesFunc = (0, import_react12.useCallback)(
|
|
1399
1502
|
(messages2) => {
|
|
1400
1503
|
return latestSetMessages.current(messages2);
|
|
1401
1504
|
},
|
|
1402
1505
|
[latestSetMessages]
|
|
1403
1506
|
);
|
|
1404
1507
|
const latestRunChatCompletion = useUpdatedRef(runChatCompletion);
|
|
1405
|
-
const latestRunChatCompletionFunc = (
|
|
1406
|
-
return latestRunChatCompletion.current();
|
|
1407
|
-
}, [latestRunChatCompletion]);
|
|
1508
|
+
const latestRunChatCompletionFunc = useAsyncCallback(() => __async(this, null, function* () {
|
|
1509
|
+
return yield latestRunChatCompletion.current();
|
|
1510
|
+
}), [latestRunChatCompletion]);
|
|
1408
1511
|
return {
|
|
1409
1512
|
visibleMessages: messages,
|
|
1410
1513
|
appendMessage: latestAppendFunc,
|
|
@@ -1417,8 +1520,8 @@ function useCopilotChat(_a = {}) {
|
|
|
1417
1520
|
};
|
|
1418
1521
|
}
|
|
1419
1522
|
function useUpdatedRef(value) {
|
|
1420
|
-
const ref = (0,
|
|
1421
|
-
(0,
|
|
1523
|
+
const ref = (0, import_react12.useRef)(value);
|
|
1524
|
+
(0, import_react12.useEffect)(() => {
|
|
1422
1525
|
ref.current = value;
|
|
1423
1526
|
}, [value]);
|
|
1424
1527
|
return ref;
|
|
@@ -1449,17 +1552,17 @@ ${additionalInstructions}` : "");
|
|
|
1449
1552
|
|
|
1450
1553
|
// src/hooks/use-copilot-action.ts
|
|
1451
1554
|
var import_shared6 = require("@copilotkit/shared");
|
|
1452
|
-
var
|
|
1555
|
+
var import_react13 = require("react");
|
|
1453
1556
|
function useCopilotAction(action, dependencies) {
|
|
1454
1557
|
const { setAction, removeAction, actions, chatComponentsCache } = useCopilotContext();
|
|
1455
|
-
const idRef = (0,
|
|
1456
|
-
const renderAndWaitRef = (0,
|
|
1558
|
+
const idRef = (0, import_react13.useRef)((0, import_shared6.randomId)());
|
|
1559
|
+
const renderAndWaitRef = (0, import_react13.useRef)(null);
|
|
1457
1560
|
action = __spreadValues({}, action);
|
|
1458
1561
|
if (action.renderAndWait || action.renderAndWaitForResponse) {
|
|
1459
1562
|
const renderAndWait = action.renderAndWait || action.renderAndWaitForResponse;
|
|
1460
1563
|
action.renderAndWait = void 0;
|
|
1461
1564
|
action.renderAndWaitForResponse = void 0;
|
|
1462
|
-
action.handler = () => __async(this, null, function* () {
|
|
1565
|
+
action.handler = useAsyncCallback(() => __async(this, null, function* () {
|
|
1463
1566
|
let resolve;
|
|
1464
1567
|
let reject;
|
|
1465
1568
|
const promise = new Promise((resolvePromise, rejectPromise) => {
|
|
@@ -1468,7 +1571,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
1468
1571
|
});
|
|
1469
1572
|
renderAndWaitRef.current = { promise, resolve, reject };
|
|
1470
1573
|
return yield promise;
|
|
1471
|
-
});
|
|
1574
|
+
}), []);
|
|
1472
1575
|
action.render = (props) => {
|
|
1473
1576
|
const waitProps = {
|
|
1474
1577
|
status: props.status,
|
|
@@ -1488,7 +1591,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
1488
1591
|
return renderAndWait(waitProps);
|
|
1489
1592
|
}
|
|
1490
1593
|
}
|
|
1491
|
-
return (0,
|
|
1594
|
+
return (0, import_react13.createElement)(import_react13.Fragment);
|
|
1492
1595
|
};
|
|
1493
1596
|
}
|
|
1494
1597
|
if (dependencies === void 0) {
|
|
@@ -1501,7 +1604,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
1501
1604
|
}
|
|
1502
1605
|
}
|
|
1503
1606
|
}
|
|
1504
|
-
(0,
|
|
1607
|
+
(0, import_react13.useEffect)(() => {
|
|
1505
1608
|
setAction(idRef.current, action);
|
|
1506
1609
|
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
1507
1610
|
chatComponentsCache.current.actions[action.name] = action.render;
|
|
@@ -1527,7 +1630,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
1527
1630
|
}
|
|
1528
1631
|
|
|
1529
1632
|
// src/hooks/use-coagent-state-render.ts
|
|
1530
|
-
var
|
|
1633
|
+
var import_react14 = require("react");
|
|
1531
1634
|
var import_shared7 = require("@copilotkit/shared");
|
|
1532
1635
|
function useCoAgentStateRender(action, dependencies) {
|
|
1533
1636
|
const {
|
|
@@ -1535,8 +1638,8 @@ function useCoAgentStateRender(action, dependencies) {
|
|
|
1535
1638
|
removeCoAgentStateRender,
|
|
1536
1639
|
coAgentStateRenders,
|
|
1537
1640
|
chatComponentsCache
|
|
1538
|
-
} = (0,
|
|
1539
|
-
const idRef = (0,
|
|
1641
|
+
} = (0, import_react14.useContext)(CopilotContext);
|
|
1642
|
+
const idRef = (0, import_react14.useRef)((0, import_shared7.randomId)());
|
|
1540
1643
|
const key = `${action.name}-${action.nodeName || "global"}`;
|
|
1541
1644
|
if (dependencies === void 0) {
|
|
1542
1645
|
if (coAgentStateRenders[idRef.current]) {
|
|
@@ -1548,7 +1651,7 @@ function useCoAgentStateRender(action, dependencies) {
|
|
|
1548
1651
|
}
|
|
1549
1652
|
}
|
|
1550
1653
|
}
|
|
1551
|
-
(0,
|
|
1654
|
+
(0, import_react14.useEffect)(() => {
|
|
1552
1655
|
setCoAgentStateRender(idRef.current, action);
|
|
1553
1656
|
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
1554
1657
|
chatComponentsCache.current.coAgentStateRenders[key] = action.render;
|
|
@@ -1568,11 +1671,11 @@ function useCoAgentStateRender(action, dependencies) {
|
|
|
1568
1671
|
}
|
|
1569
1672
|
|
|
1570
1673
|
// src/hooks/use-make-copilot-document-readable.ts
|
|
1571
|
-
var
|
|
1674
|
+
var import_react15 = require("react");
|
|
1572
1675
|
function useMakeCopilotDocumentReadable(document, categories, dependencies = []) {
|
|
1573
1676
|
const { addDocumentContext, removeDocumentContext } = useCopilotContext();
|
|
1574
|
-
const idRef = (0,
|
|
1575
|
-
(0,
|
|
1677
|
+
const idRef = (0, import_react15.useRef)();
|
|
1678
|
+
(0, import_react15.useEffect)(() => {
|
|
1576
1679
|
const id = addDocumentContext(document, categories);
|
|
1577
1680
|
idRef.current = id;
|
|
1578
1681
|
return () => {
|
|
@@ -1583,16 +1686,16 @@ function useMakeCopilotDocumentReadable(document, categories, dependencies = [])
|
|
|
1583
1686
|
}
|
|
1584
1687
|
|
|
1585
1688
|
// src/hooks/use-copilot-readable.ts
|
|
1586
|
-
var
|
|
1689
|
+
var import_react16 = require("react");
|
|
1587
1690
|
function convertToJSON(description, value) {
|
|
1588
1691
|
return `${description}: ${typeof value === "string" ? value : JSON.stringify(value)}`;
|
|
1589
1692
|
}
|
|
1590
1693
|
function useCopilotReadable({ description, value, parentId, categories, convert }, dependencies) {
|
|
1591
1694
|
const { addContext, removeContext } = useCopilotContext();
|
|
1592
|
-
const idRef = (0,
|
|
1695
|
+
const idRef = (0, import_react16.useRef)();
|
|
1593
1696
|
convert = convert || convertToJSON;
|
|
1594
1697
|
const information = convert(description, value);
|
|
1595
|
-
(0,
|
|
1698
|
+
(0, import_react16.useEffect)(() => {
|
|
1596
1699
|
const id = addContext(information, parentId, categories);
|
|
1597
1700
|
idRef.current = id;
|
|
1598
1701
|
return () => {
|
|
@@ -1603,7 +1706,7 @@ function useCopilotReadable({ description, value, parentId, categories, convert
|
|
|
1603
1706
|
}
|
|
1604
1707
|
|
|
1605
1708
|
// src/hooks/use-coagent.ts
|
|
1606
|
-
var
|
|
1709
|
+
var import_react17 = require("react");
|
|
1607
1710
|
function useCoAgent(options) {
|
|
1608
1711
|
const isExternalStateManagement = (options2) => {
|
|
1609
1712
|
return "state" in options2 && "setState" in options2;
|
|
@@ -1643,28 +1746,29 @@ function useCoAgent(options) {
|
|
|
1643
1746
|
};
|
|
1644
1747
|
const coagentState = getCoagentState(coagentStates, name);
|
|
1645
1748
|
const state = isExternalStateManagement(options) ? options.state : coagentState.state;
|
|
1646
|
-
(0,
|
|
1749
|
+
(0, import_react17.useEffect)(() => {
|
|
1647
1750
|
if (isExternalStateManagement(options)) {
|
|
1648
1751
|
setState(options.state);
|
|
1649
1752
|
} else if (coagentStates[name] === void 0) {
|
|
1650
1753
|
setState(options.initialState === void 0 ? {} : options.initialState);
|
|
1651
1754
|
}
|
|
1652
1755
|
}, [isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0]);
|
|
1756
|
+
const runAgentCallback = useAsyncCallback(
|
|
1757
|
+
(hint) => __async(this, null, function* () {
|
|
1758
|
+
yield runAgent(name, context, appendMessage, runChatCompletion, hint);
|
|
1759
|
+
}),
|
|
1760
|
+
[name, context, appendMessage, runChatCompletion]
|
|
1761
|
+
);
|
|
1653
1762
|
return {
|
|
1654
1763
|
name,
|
|
1655
1764
|
nodeName: coagentState.nodeName,
|
|
1656
|
-
|
|
1657
|
-
setState,
|
|
1765
|
+
threadId: coagentState.threadId,
|
|
1658
1766
|
running: coagentState.running,
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
stop: () =>
|
|
1663
|
-
|
|
1664
|
-
},
|
|
1665
|
-
run: (hint) => {
|
|
1666
|
-
return runAgent(name, context, appendMessage, runChatCompletion, hint);
|
|
1667
|
-
}
|
|
1767
|
+
state: coagentState.state,
|
|
1768
|
+
setState: isExternalStateManagement(options) ? options.setState : setState,
|
|
1769
|
+
start: () => startAgent(name, context),
|
|
1770
|
+
stop: () => stopAgent(name, context),
|
|
1771
|
+
run: runAgentCallback
|
|
1668
1772
|
};
|
|
1669
1773
|
}
|
|
1670
1774
|
function startAgent(name, context) {
|