@copilotkit/react-core 1.4.8-no-pino-redact.1 → 1.5.0-coagents-v0-3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -3
- package/dist/{chunk-JHEAUB3Z.mjs → chunk-35EN6BG4.mjs} +2 -2
- package/dist/{chunk-JHEAUB3Z.mjs.map → chunk-35EN6BG4.mjs.map} +1 -1
- package/dist/{chunk-O22KGHOQ.mjs → chunk-42N5VKIX.mjs} +38 -16
- package/dist/chunk-42N5VKIX.mjs.map +1 -0
- package/dist/{chunk-AG7FH7OD.mjs → chunk-5FYKUKG3.mjs} +2 -2
- package/dist/{chunk-ODN4H66E.mjs → chunk-7LRDVJH5.mjs} +6 -2
- package/dist/chunk-7LRDVJH5.mjs.map +1 -0
- package/dist/{chunk-Y7MI4PBB.mjs → chunk-ALR5W5JK.mjs} +22 -10
- package/dist/chunk-ALR5W5JK.mjs.map +1 -0
- package/dist/{chunk-XBVKTDXP.mjs → chunk-BT6WK2JZ.mjs} +57 -17
- package/dist/chunk-BT6WK2JZ.mjs.map +1 -0
- package/dist/chunk-QCUP6HLK.mjs +37 -0
- package/dist/chunk-QCUP6HLK.mjs.map +1 -0
- package/dist/chunk-QTDCEDOC.mjs +392 -0
- package/dist/chunk-QTDCEDOC.mjs.map +1 -0
- package/dist/{chunk-OT67R4NB.mjs → chunk-QX6V774L.mjs} +6 -8
- package/dist/chunk-QX6V774L.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-XQFVXX6R.mjs → chunk-TQN3EZWQ.mjs} +10 -2
- package/dist/chunk-TQN3EZWQ.mjs.map +1 -0
- package/dist/{chunk-UOVONDR6.mjs → chunk-V3PFWGIY.mjs} +2 -2
- package/dist/{chunk-DHGXL5PC.mjs → chunk-VMP6JWBB.mjs} +18 -6
- package/dist/chunk-VMP6JWBB.mjs.map +1 -0
- package/dist/chunk-XERJQUHA.mjs +31 -0
- package/dist/chunk-XERJQUHA.mjs.map +1 -0
- package/dist/components/copilot-provider/copilotkit.js +175 -93
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +5 -4
- package/dist/components/copilot-provider/index.js +175 -93
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +5 -4
- 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 +175 -93
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +5 -4
- 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/context/copilot-context.d.ts +8 -2
- package/dist/context/copilot-context.js +9 -1
- package/dist/context/copilot-context.js.map +1 -1
- package/dist/context/copilot-context.mjs +1 -1
- package/dist/context/index.d.ts +1 -1
- package/dist/context/index.js +9 -1
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +1 -1
- package/dist/hooks/index.d.ts +2 -1
- package/dist/hooks/index.js +584 -270
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +19 -11
- package/dist/hooks/use-chat.d.ts +20 -0
- package/dist/hooks/use-chat.js +438 -200
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +4 -3
- package/dist/hooks/use-coagent-state-render.d.ts +2 -2
- package/dist/hooks/use-coagent-state-render.js +9 -1
- package/dist/hooks/use-coagent-state-render.js.map +1 -1
- package/dist/hooks/use-coagent-state-render.mjs +2 -2
- package/dist/hooks/use-coagent.d.ts +14 -1
- package/dist/hooks/use-coagent.js +547 -242
- package/dist/hooks/use-coagent.js.map +1 -1
- package/dist/hooks/use-coagent.mjs +15 -7
- package/dist/hooks/use-copilot-action.d.ts +12 -2
- package/dist/hooks/use-copilot-action.js +156 -14
- package/dist/hooks/use-copilot-action.js.map +1 -1
- package/dist/hooks/use-copilot-action.mjs +4 -2
- package/dist/hooks/use-copilot-chat.d.ts +1 -0
- package/dist/hooks/use-copilot-chat.js +514 -231
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +8 -6
- package/dist/hooks/use-copilot-readable.js +9 -1
- package/dist/hooks/use-copilot-readable.js.map +1 -1
- package/dist/hooks/use-copilot-readable.mjs +2 -2
- 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/hooks/use-make-copilot-document-readable.js +9 -1
- package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
- package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +642 -363
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +20 -12
- package/dist/lib/copilot-task.d.ts +1 -1
- package/dist/lib/copilot-task.js +33 -13
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +7 -5
- package/dist/lib/index.d.ts +1 -1
- package/dist/lib/index.js +33 -13
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +7 -5
- package/dist/types/frontend-action.d.ts +21 -2
- package/dist/types/frontend-action.js +34 -0
- package/dist/types/frontend-action.js.map +1 -1
- package/dist/types/frontend-action.mjs +7 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.js.map +1 -1
- package/dist/utils/extract.js.map +1 -1
- package/dist/utils/extract.mjs +5 -4
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +5 -4
- package/package.json +3 -3
- package/src/components/copilot-provider/copilotkit.tsx +14 -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/context/copilot-context.tsx +30 -2
- package/src/hooks/index.ts +1 -1
- package/src/hooks/use-chat.ts +391 -256
- package/src/hooks/use-coagent-state-render.ts +2 -2
- package/src/hooks/use-coagent.ts +35 -15
- package/src/hooks/use-copilot-action.ts +41 -12
- package/src/hooks/use-copilot-chat.ts +52 -11
- package/src/hooks/use-copilot-runtime-client.ts +4 -0
- package/src/lib/copilot-task.ts +2 -8
- package/src/types/frontend-action.ts +55 -2
- package/src/types/index.ts +5 -1
- 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-OT67R4NB.mjs.map +0 -1
- package/dist/chunk-XBVKTDXP.mjs.map +0 -1
- package/dist/chunk-XQFVXX6R.mjs.map +0 -1
- package/dist/chunk-Y7MI4PBB.mjs.map +0 -1
- /package/dist/{chunk-AG7FH7OD.mjs.map → chunk-5FYKUKG3.mjs.map} +0 -0
- /package/dist/{chunk-UOVONDR6.mjs.map → chunk-V3PFWGIY.mjs.map} +0 -0
package/dist/index.js
CHANGED
|
@@ -87,7 +87,10 @@ __export(src_exports, {
|
|
|
87
87
|
CopilotTask: () => CopilotTask,
|
|
88
88
|
defaultCopilotContextCategories: () => defaultCopilotContextCategories,
|
|
89
89
|
extract: () => extract,
|
|
90
|
+
runAgent: () => runAgent,
|
|
90
91
|
shouldShowDevConsole: () => shouldShowDevConsole,
|
|
92
|
+
startAgent: () => startAgent,
|
|
93
|
+
stopAgent: () => stopAgent,
|
|
91
94
|
useCoAgent: () => useCoAgent,
|
|
92
95
|
useCoAgentStateRender: () => useCoAgentStateRender,
|
|
93
96
|
useCopilotAction: () => useCopilotAction,
|
|
@@ -101,7 +104,7 @@ __export(src_exports, {
|
|
|
101
104
|
module.exports = __toCommonJS(src_exports);
|
|
102
105
|
|
|
103
106
|
// src/components/copilot-provider/copilotkit.tsx
|
|
104
|
-
var
|
|
107
|
+
var import_react10 = require("react");
|
|
105
108
|
|
|
106
109
|
// src/context/copilot-context.tsx
|
|
107
110
|
var import_react = __toESM(require("react"));
|
|
@@ -157,7 +160,15 @@ var emptyCopilotContext = {
|
|
|
157
160
|
},
|
|
158
161
|
agentSession: null,
|
|
159
162
|
setAgentSession: () => {
|
|
160
|
-
}
|
|
163
|
+
},
|
|
164
|
+
agentLock: null,
|
|
165
|
+
threadId: null,
|
|
166
|
+
setThreadId: () => {
|
|
167
|
+
},
|
|
168
|
+
runId: null,
|
|
169
|
+
setRunId: () => {
|
|
170
|
+
},
|
|
171
|
+
chatAbortControllerRef: { current: null }
|
|
161
172
|
};
|
|
162
173
|
var CopilotContext = import_react.default.createContext(emptyCopilotContext);
|
|
163
174
|
function useCopilotContext() {
|
|
@@ -411,6 +422,9 @@ function CopilotMessages(_a) {
|
|
|
411
422
|
}
|
|
412
423
|
|
|
413
424
|
// src/components/toast/toast-provider.tsx
|
|
425
|
+
var import_react7 = require("react");
|
|
426
|
+
|
|
427
|
+
// src/components/error-boundary/error-utils.tsx
|
|
414
428
|
var import_react6 = require("react");
|
|
415
429
|
|
|
416
430
|
// src/components/toast/exclamation-mark-icon.tsx
|
|
@@ -440,11 +454,94 @@ var ExclamationMarkIcon = ({
|
|
|
440
454
|
}
|
|
441
455
|
);
|
|
442
456
|
|
|
443
|
-
// src/components/
|
|
457
|
+
// src/components/error-boundary/error-utils.tsx
|
|
444
458
|
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
445
|
-
|
|
459
|
+
function ErrorToast({ errors }) {
|
|
460
|
+
const errorsToRender = errors.map((error, idx) => {
|
|
461
|
+
var _a, _b, _c;
|
|
462
|
+
const message = "extensions" in error ? (_b = (_a = error.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message : error.message;
|
|
463
|
+
const code = "extensions" in error ? (_c = error.extensions) == null ? void 0 : _c.code : null;
|
|
464
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
465
|
+
"div",
|
|
466
|
+
{
|
|
467
|
+
style: {
|
|
468
|
+
marginTop: idx === 0 ? 0 : 10,
|
|
469
|
+
marginBottom: 14
|
|
470
|
+
},
|
|
471
|
+
children: [
|
|
472
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ExclamationMarkIcon, { style: { marginBottom: 4 } }),
|
|
473
|
+
code && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
474
|
+
"div",
|
|
475
|
+
{
|
|
476
|
+
style: {
|
|
477
|
+
fontWeight: "600",
|
|
478
|
+
marginBottom: 4
|
|
479
|
+
},
|
|
480
|
+
children: [
|
|
481
|
+
"Copilot Cloud Error:",
|
|
482
|
+
" ",
|
|
483
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { style: { fontFamily: "monospace", fontWeight: "normal" }, children: code })
|
|
484
|
+
]
|
|
485
|
+
}
|
|
486
|
+
),
|
|
487
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: message })
|
|
488
|
+
]
|
|
489
|
+
},
|
|
490
|
+
idx
|
|
491
|
+
);
|
|
492
|
+
});
|
|
493
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
494
|
+
"div",
|
|
495
|
+
{
|
|
496
|
+
style: {
|
|
497
|
+
fontSize: "13px",
|
|
498
|
+
maxWidth: "600px"
|
|
499
|
+
},
|
|
500
|
+
children: [
|
|
501
|
+
errorsToRender,
|
|
502
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: { fontSize: "11px", opacity: 0.75 }, children: "NOTE: This error only displays during local development." })
|
|
503
|
+
]
|
|
504
|
+
}
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
function useErrorToast() {
|
|
508
|
+
const { addToast } = useToast();
|
|
509
|
+
return (0, import_react6.useCallback)(
|
|
510
|
+
(error) => {
|
|
511
|
+
const errorId = error.map((err) => {
|
|
512
|
+
var _a, _b;
|
|
513
|
+
const message = "extensions" in err ? ((_b = (_a = err.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message) || err.message : err.message;
|
|
514
|
+
const stack = err.stack || "";
|
|
515
|
+
return btoa(message + stack).slice(0, 32);
|
|
516
|
+
}).join("|");
|
|
517
|
+
addToast({
|
|
518
|
+
type: "error",
|
|
519
|
+
id: errorId,
|
|
520
|
+
// Toast libraries typically dedupe by id
|
|
521
|
+
message: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ErrorToast, { errors: error })
|
|
522
|
+
});
|
|
523
|
+
},
|
|
524
|
+
[addToast]
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
function useAsyncCallback(callback, deps) {
|
|
528
|
+
const addErrorToast = useErrorToast();
|
|
529
|
+
return (0, import_react6.useCallback)((...args) => __async(this, null, function* () {
|
|
530
|
+
try {
|
|
531
|
+
return yield callback(...args);
|
|
532
|
+
} catch (error) {
|
|
533
|
+
console.error("Error in async callback:", error);
|
|
534
|
+
addErrorToast([error]);
|
|
535
|
+
throw error;
|
|
536
|
+
}
|
|
537
|
+
}), deps);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// src/components/toast/toast-provider.tsx
|
|
541
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
542
|
+
var ToastContext = (0, import_react7.createContext)(void 0);
|
|
446
543
|
function useToast() {
|
|
447
|
-
const context = (0,
|
|
544
|
+
const context = (0, import_react7.useContext)(ToastContext);
|
|
448
545
|
if (!context) {
|
|
449
546
|
throw new Error("useToast must be used within a ToastProvider");
|
|
450
547
|
}
|
|
@@ -454,68 +551,28 @@ function ToastProvider({
|
|
|
454
551
|
enabled,
|
|
455
552
|
children
|
|
456
553
|
}) {
|
|
457
|
-
const [toasts, setToasts] = (0,
|
|
458
|
-
const addToast = (0,
|
|
459
|
-
|
|
460
|
-
|
|
554
|
+
const [toasts, setToasts] = (0, import_react7.useState)([]);
|
|
555
|
+
const addToast = (0, import_react7.useCallback)((toast) => {
|
|
556
|
+
var _a;
|
|
557
|
+
const id = (_a = toast.id) != null ? _a : Math.random().toString(36).substring(2, 9);
|
|
558
|
+
setToasts((currentToasts) => {
|
|
559
|
+
if (currentToasts.find((toast2) => toast2.id === id))
|
|
560
|
+
return currentToasts;
|
|
561
|
+
return [...currentToasts, __spreadProps(__spreadValues({}, toast), { id })];
|
|
562
|
+
});
|
|
461
563
|
if (toast.duration) {
|
|
462
564
|
setTimeout(() => {
|
|
463
565
|
removeToast(id);
|
|
464
566
|
}, toast.duration);
|
|
465
567
|
}
|
|
466
568
|
}, []);
|
|
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
|
-
});
|
|
569
|
+
const addGraphQLErrorsToast = (0, import_react7.useCallback)((errors) => {
|
|
501
570
|
addToast({
|
|
502
571
|
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
|
-
)
|
|
572
|
+
message: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(ErrorToast, { errors })
|
|
516
573
|
});
|
|
517
574
|
}, []);
|
|
518
|
-
const removeToast = (0,
|
|
575
|
+
const removeToast = (0, import_react7.useCallback)((id) => {
|
|
519
576
|
setToasts((currentToasts) => currentToasts.filter((toast) => toast.id !== id));
|
|
520
577
|
}, []);
|
|
521
578
|
const value = {
|
|
@@ -525,8 +582,8 @@ function ToastProvider({
|
|
|
525
582
|
removeToast,
|
|
526
583
|
enabled
|
|
527
584
|
};
|
|
528
|
-
return /* @__PURE__ */ (0,
|
|
529
|
-
/* @__PURE__ */ (0,
|
|
585
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(ToastContext.Provider, { value, children: [
|
|
586
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
|
|
530
587
|
"div",
|
|
531
588
|
{
|
|
532
589
|
style: {
|
|
@@ -540,7 +597,7 @@ function ToastProvider({
|
|
|
540
597
|
gap: "0.5rem"
|
|
541
598
|
},
|
|
542
599
|
children: [
|
|
543
|
-
toasts.length > 1 && /* @__PURE__ */ (0,
|
|
600
|
+
toasts.length > 1 && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { style: { textAlign: "right" }, children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
544
601
|
"button",
|
|
545
602
|
{
|
|
546
603
|
onClick: () => setToasts([]),
|
|
@@ -555,7 +612,7 @@ function ToastProvider({
|
|
|
555
612
|
children: "Close All"
|
|
556
613
|
}
|
|
557
614
|
) }),
|
|
558
|
-
toasts.map((toast) => /* @__PURE__ */ (0,
|
|
615
|
+
toasts.map((toast) => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
559
616
|
Toast,
|
|
560
617
|
{
|
|
561
618
|
message: toast.message,
|
|
@@ -581,7 +638,7 @@ function Toast({
|
|
|
581
638
|
warning: "#eab308",
|
|
582
639
|
error: "#ef4444"
|
|
583
640
|
};
|
|
584
|
-
return /* @__PURE__ */ (0,
|
|
641
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
|
|
585
642
|
"div",
|
|
586
643
|
{
|
|
587
644
|
style: {
|
|
@@ -594,8 +651,8 @@ function Toast({
|
|
|
594
651
|
minWidth: "200px"
|
|
595
652
|
},
|
|
596
653
|
children: [
|
|
597
|
-
/* @__PURE__ */ (0,
|
|
598
|
-
/* @__PURE__ */ (0,
|
|
654
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { children: message }),
|
|
655
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
599
656
|
"button",
|
|
600
657
|
{
|
|
601
658
|
onClick: onClose,
|
|
@@ -620,14 +677,17 @@ function Toast({
|
|
|
620
677
|
|
|
621
678
|
// src/hooks/use-copilot-runtime-client.ts
|
|
622
679
|
var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
|
|
623
|
-
var
|
|
680
|
+
var import_react8 = require("react");
|
|
624
681
|
var useCopilotRuntimeClient = (options) => {
|
|
625
682
|
const { addGraphQLErrorsToast } = useToast();
|
|
626
|
-
const
|
|
683
|
+
const addErrorToast = useErrorToast();
|
|
684
|
+
const runtimeClient = (0, import_react8.useMemo)(() => {
|
|
627
685
|
return new import_runtime_client_gql.CopilotRuntimeClient(__spreadProps(__spreadValues({}, options), {
|
|
628
686
|
handleGQLErrors: (error) => {
|
|
629
687
|
if (error.graphQLErrors.length) {
|
|
630
688
|
addGraphQLErrorsToast(error.graphQLErrors);
|
|
689
|
+
} else {
|
|
690
|
+
addErrorToast([error]);
|
|
631
691
|
}
|
|
632
692
|
}
|
|
633
693
|
}));
|
|
@@ -782,13 +842,41 @@ function getHostname() {
|
|
|
782
842
|
return "";
|
|
783
843
|
}
|
|
784
844
|
|
|
845
|
+
// src/components/error-boundary/error-boundary.tsx
|
|
846
|
+
var import_react9 = __toESM(require("react"));
|
|
847
|
+
var import_jsx_runtime5 = require("react/jsx-runtime");
|
|
848
|
+
var CopilotErrorBoundary = class extends import_react9.default.Component {
|
|
849
|
+
constructor(props) {
|
|
850
|
+
super(props);
|
|
851
|
+
this.state = { hasError: false };
|
|
852
|
+
}
|
|
853
|
+
static getDerivedStateFromError(error) {
|
|
854
|
+
return { hasError: true, error };
|
|
855
|
+
}
|
|
856
|
+
componentDidCatch(error, errorInfo) {
|
|
857
|
+
console.error("CopilotKit Error:", error, errorInfo);
|
|
858
|
+
}
|
|
859
|
+
render() {
|
|
860
|
+
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(ErrorToast2, { error: this.state.error, children: this.props.children });
|
|
861
|
+
}
|
|
862
|
+
};
|
|
863
|
+
function ErrorToast2({ error, children }) {
|
|
864
|
+
const addErrorToast = useErrorToast();
|
|
865
|
+
(0, import_react9.useEffect)(() => {
|
|
866
|
+
if (error) {
|
|
867
|
+
addErrorToast([error]);
|
|
868
|
+
}
|
|
869
|
+
}, [error, addErrorToast]);
|
|
870
|
+
return children;
|
|
871
|
+
}
|
|
872
|
+
|
|
785
873
|
// src/components/copilot-provider/copilotkit.tsx
|
|
786
|
-
var
|
|
874
|
+
var import_jsx_runtime6 = require("react/jsx-runtime");
|
|
787
875
|
function CopilotKit(_a) {
|
|
788
876
|
var _b = _a, { children } = _b, props = __objRest(_b, ["children"]);
|
|
789
877
|
const showDevConsole = props.showDevConsole === void 0 ? "auto" : props.showDevConsole;
|
|
790
878
|
const enabled = shouldShowDevConsole(showDevConsole);
|
|
791
|
-
return /* @__PURE__ */ (0,
|
|
879
|
+
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
880
|
}
|
|
793
881
|
function CopilotKitInternal(_a) {
|
|
794
882
|
var _b = _a, { children } = _b, props = __objRest(_b, ["children"]);
|
|
@@ -798,49 +886,49 @@ function CopilotKitInternal(_a) {
|
|
|
798
886
|
);
|
|
799
887
|
}
|
|
800
888
|
const chatApiEndpoint = props.runtimeUrl || import_shared4.COPILOT_CLOUD_CHAT_URL;
|
|
801
|
-
const [actions, setActions] = (0,
|
|
802
|
-
const [coAgentStateRenders, setCoAgentStateRenders] = (0,
|
|
803
|
-
const chatComponentsCache = (0,
|
|
889
|
+
const [actions, setActions] = (0, import_react10.useState)({});
|
|
890
|
+
const [coAgentStateRenders, setCoAgentStateRenders] = (0, import_react10.useState)({});
|
|
891
|
+
const chatComponentsCache = (0, import_react10.useRef)({
|
|
804
892
|
actions: {},
|
|
805
893
|
coAgentStateRenders: {}
|
|
806
894
|
});
|
|
807
895
|
const { addElement, removeElement, printTree } = use_tree_default();
|
|
808
|
-
const [isLoading, setIsLoading] = (0,
|
|
809
|
-
const [chatInstructions, setChatInstructions] = (0,
|
|
896
|
+
const [isLoading, setIsLoading] = (0, import_react10.useState)(false);
|
|
897
|
+
const [chatInstructions, setChatInstructions] = (0, import_react10.useState)("");
|
|
810
898
|
const {
|
|
811
899
|
addElement: addDocument,
|
|
812
900
|
removeElement: removeDocument,
|
|
813
901
|
allElements: allDocuments
|
|
814
902
|
} = use_flat_category_store_default();
|
|
815
|
-
const setAction = (0,
|
|
903
|
+
const setAction = (0, import_react10.useCallback)((id, action) => {
|
|
816
904
|
setActions((prevPoints) => {
|
|
817
905
|
return __spreadProps(__spreadValues({}, prevPoints), {
|
|
818
906
|
[id]: action
|
|
819
907
|
});
|
|
820
908
|
});
|
|
821
909
|
}, []);
|
|
822
|
-
const removeAction = (0,
|
|
910
|
+
const removeAction = (0, import_react10.useCallback)((id) => {
|
|
823
911
|
setActions((prevPoints) => {
|
|
824
912
|
const newPoints = __spreadValues({}, prevPoints);
|
|
825
913
|
delete newPoints[id];
|
|
826
914
|
return newPoints;
|
|
827
915
|
});
|
|
828
916
|
}, []);
|
|
829
|
-
const setCoAgentStateRender = (0,
|
|
917
|
+
const setCoAgentStateRender = (0, import_react10.useCallback)((id, stateRender) => {
|
|
830
918
|
setCoAgentStateRenders((prevPoints) => {
|
|
831
919
|
return __spreadProps(__spreadValues({}, prevPoints), {
|
|
832
920
|
[id]: stateRender
|
|
833
921
|
});
|
|
834
922
|
});
|
|
835
923
|
}, []);
|
|
836
|
-
const removeCoAgentStateRender = (0,
|
|
924
|
+
const removeCoAgentStateRender = (0, import_react10.useCallback)((id) => {
|
|
837
925
|
setCoAgentStateRenders((prevPoints) => {
|
|
838
926
|
const newPoints = __spreadValues({}, prevPoints);
|
|
839
927
|
delete newPoints[id];
|
|
840
928
|
return newPoints;
|
|
841
929
|
});
|
|
842
930
|
}, []);
|
|
843
|
-
const getContextString = (0,
|
|
931
|
+
const getContextString = (0, import_react10.useCallback)(
|
|
844
932
|
(documents, categories) => {
|
|
845
933
|
const documentsString = documents.map((document) => {
|
|
846
934
|
return `${document.name} (${document.sourceApplication}):
|
|
@@ -853,37 +941,37 @@ ${nonDocumentStrings}`;
|
|
|
853
941
|
},
|
|
854
942
|
[printTree]
|
|
855
943
|
);
|
|
856
|
-
const addContext = (0,
|
|
944
|
+
const addContext = (0, import_react10.useCallback)(
|
|
857
945
|
(context, parentId, categories = defaultCopilotContextCategories) => {
|
|
858
946
|
return addElement(context, categories, parentId);
|
|
859
947
|
},
|
|
860
948
|
[addElement]
|
|
861
949
|
);
|
|
862
|
-
const removeContext = (0,
|
|
950
|
+
const removeContext = (0, import_react10.useCallback)(
|
|
863
951
|
(id) => {
|
|
864
952
|
removeElement(id);
|
|
865
953
|
},
|
|
866
954
|
[removeElement]
|
|
867
955
|
);
|
|
868
|
-
const getFunctionCallHandler = (0,
|
|
956
|
+
const getFunctionCallHandler = (0, import_react10.useCallback)(
|
|
869
957
|
(customEntryPoints) => {
|
|
870
958
|
return entryPointsToFunctionCallHandler(Object.values(customEntryPoints || actions));
|
|
871
959
|
},
|
|
872
960
|
[actions]
|
|
873
961
|
);
|
|
874
|
-
const getDocumentsContext = (0,
|
|
962
|
+
const getDocumentsContext = (0, import_react10.useCallback)(
|
|
875
963
|
(categories) => {
|
|
876
964
|
return allDocuments(categories);
|
|
877
965
|
},
|
|
878
966
|
[allDocuments]
|
|
879
967
|
);
|
|
880
|
-
const addDocumentContext = (0,
|
|
968
|
+
const addDocumentContext = (0, import_react10.useCallback)(
|
|
881
969
|
(documentPointer, categories = defaultCopilotContextCategories) => {
|
|
882
970
|
return addDocument(documentPointer, categories);
|
|
883
971
|
},
|
|
884
972
|
[addDocument]
|
|
885
973
|
);
|
|
886
|
-
const removeDocumentContext = (0,
|
|
974
|
+
const removeDocumentContext = (0, import_react10.useCallback)(
|
|
887
975
|
(documentId) => {
|
|
888
976
|
removeDocument(documentId);
|
|
889
977
|
},
|
|
@@ -896,7 +984,7 @@ ${nonDocumentStrings}`;
|
|
|
896
984
|
);
|
|
897
985
|
}
|
|
898
986
|
}
|
|
899
|
-
const copilotApiConfig = (0,
|
|
987
|
+
const copilotApiConfig = (0, import_react10.useMemo)(() => {
|
|
900
988
|
var _a2, _b2;
|
|
901
989
|
let cloud = void 0;
|
|
902
990
|
if (props.publicApiKey) {
|
|
@@ -938,7 +1026,7 @@ ${nonDocumentStrings}`;
|
|
|
938
1026
|
headers,
|
|
939
1027
|
credentials: copilotApiConfig.credentials
|
|
940
1028
|
});
|
|
941
|
-
const [chatSuggestionConfiguration, setChatSuggestionConfiguration] = (0,
|
|
1029
|
+
const [chatSuggestionConfiguration, setChatSuggestionConfiguration] = (0, import_react10.useState)({});
|
|
942
1030
|
const addChatSuggestionConfiguration = (id, suggestion) => {
|
|
943
1031
|
setChatSuggestionConfiguration((prev) => __spreadProps(__spreadValues({}, prev), { [id]: suggestion }));
|
|
944
1032
|
};
|
|
@@ -948,9 +1036,9 @@ ${nonDocumentStrings}`;
|
|
|
948
1036
|
return rest;
|
|
949
1037
|
});
|
|
950
1038
|
};
|
|
951
|
-
const [coagentStates, setCoagentStates] = (0,
|
|
952
|
-
const coagentStatesRef = (0,
|
|
953
|
-
const setCoagentStatesWithRef = (0,
|
|
1039
|
+
const [coagentStates, setCoagentStates] = (0, import_react10.useState)({});
|
|
1040
|
+
const coagentStatesRef = (0, import_react10.useRef)({});
|
|
1041
|
+
const setCoagentStatesWithRef = (0, import_react10.useCallback)(
|
|
954
1042
|
(value) => {
|
|
955
1043
|
const newValue = typeof value === "function" ? value(coagentStatesRef.current) : value;
|
|
956
1044
|
coagentStatesRef.current = newValue;
|
|
@@ -966,9 +1054,12 @@ ${nonDocumentStrings}`;
|
|
|
966
1054
|
agentName: props.agent
|
|
967
1055
|
};
|
|
968
1056
|
}
|
|
969
|
-
const [agentSession, setAgentSession] = (0,
|
|
1057
|
+
const [agentSession, setAgentSession] = (0, import_react10.useState)(initialAgentSession);
|
|
1058
|
+
const [threadId, setThreadId] = (0, import_react10.useState)(null);
|
|
1059
|
+
const [runId, setRunId] = (0, import_react10.useState)(null);
|
|
1060
|
+
const chatAbortControllerRef = (0, import_react10.useRef)(null);
|
|
970
1061
|
const showDevConsole = props.showDevConsole === void 0 ? "auto" : props.showDevConsole;
|
|
971
|
-
return /* @__PURE__ */ (0,
|
|
1062
|
+
return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
|
|
972
1063
|
CopilotContext.Provider,
|
|
973
1064
|
{
|
|
974
1065
|
value: {
|
|
@@ -1001,9 +1092,15 @@ ${nonDocumentStrings}`;
|
|
|
1001
1092
|
setCoagentStatesWithRef,
|
|
1002
1093
|
agentSession,
|
|
1003
1094
|
setAgentSession,
|
|
1004
|
-
runtimeClient
|
|
1095
|
+
runtimeClient,
|
|
1096
|
+
agentLock: props.agent || null,
|
|
1097
|
+
threadId,
|
|
1098
|
+
setThreadId,
|
|
1099
|
+
runId,
|
|
1100
|
+
setRunId,
|
|
1101
|
+
chatAbortControllerRef
|
|
1005
1102
|
},
|
|
1006
|
-
children: /* @__PURE__ */ (0,
|
|
1103
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(CopilotMessages, { children })
|
|
1007
1104
|
}
|
|
1008
1105
|
);
|
|
1009
1106
|
}
|
|
@@ -1035,13 +1132,40 @@ function entryPointsToFunctionCallHandler(actions) {
|
|
|
1035
1132
|
}
|
|
1036
1133
|
|
|
1037
1134
|
// src/hooks/use-copilot-chat.ts
|
|
1038
|
-
var
|
|
1039
|
-
var
|
|
1135
|
+
var import_react12 = require("react");
|
|
1136
|
+
var import_runtime_client_gql6 = require("@copilotkit/runtime-client-gql");
|
|
1040
1137
|
|
|
1041
1138
|
// src/hooks/use-chat.ts
|
|
1042
|
-
var
|
|
1043
|
-
var
|
|
1139
|
+
var import_react11 = require("react");
|
|
1140
|
+
var import_shared6 = require("@copilotkit/shared");
|
|
1141
|
+
var import_runtime_client_gql5 = require("@copilotkit/runtime-client-gql");
|
|
1142
|
+
|
|
1143
|
+
// src/types/frontend-action.ts
|
|
1044
1144
|
var import_runtime_client_gql4 = require("@copilotkit/runtime-client-gql");
|
|
1145
|
+
var import_shared5 = require("@copilotkit/shared");
|
|
1146
|
+
function processActionsForRuntimeRequest(actions) {
|
|
1147
|
+
const filteredActions = actions.filter(
|
|
1148
|
+
(action) => action.available !== import_runtime_client_gql4.ActionInputAvailability.Disabled && action.disabled !== true && action.name !== "*"
|
|
1149
|
+
).map((action) => {
|
|
1150
|
+
let available = import_runtime_client_gql4.ActionInputAvailability.Enabled;
|
|
1151
|
+
if (action.disabled) {
|
|
1152
|
+
available = import_runtime_client_gql4.ActionInputAvailability.Disabled;
|
|
1153
|
+
} else if (action.available === "disabled") {
|
|
1154
|
+
available = import_runtime_client_gql4.ActionInputAvailability.Disabled;
|
|
1155
|
+
} else if (action.available === "remote") {
|
|
1156
|
+
available = import_runtime_client_gql4.ActionInputAvailability.Remote;
|
|
1157
|
+
}
|
|
1158
|
+
return {
|
|
1159
|
+
name: action.name,
|
|
1160
|
+
description: action.description || "",
|
|
1161
|
+
jsonSchema: JSON.stringify((0, import_shared5.actionParametersToJsonSchema)(action.parameters || [])),
|
|
1162
|
+
available
|
|
1163
|
+
};
|
|
1164
|
+
});
|
|
1165
|
+
return filteredActions;
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
// src/hooks/use-chat.ts
|
|
1045
1169
|
function useChat(options) {
|
|
1046
1170
|
const {
|
|
1047
1171
|
messages,
|
|
@@ -1057,227 +1181,305 @@ function useChat(options) {
|
|
|
1057
1181
|
setCoagentStatesWithRef,
|
|
1058
1182
|
coagentStatesRef,
|
|
1059
1183
|
agentSession,
|
|
1060
|
-
setAgentSession
|
|
1184
|
+
setAgentSession,
|
|
1185
|
+
threadId,
|
|
1186
|
+
setThreadId,
|
|
1187
|
+
runId,
|
|
1188
|
+
setRunId,
|
|
1189
|
+
chatAbortControllerRef
|
|
1061
1190
|
} = options;
|
|
1062
|
-
const abortControllerRef = (0, import_react9.useRef)();
|
|
1063
|
-
const threadIdRef = (0, import_react9.useRef)(null);
|
|
1064
|
-
const runIdRef = (0, import_react9.useRef)(null);
|
|
1065
1191
|
const { addGraphQLErrorsToast } = useToast();
|
|
1066
|
-
const runChatCompletionRef = (0,
|
|
1067
|
-
const agentSessionRef = (0,
|
|
1192
|
+
const runChatCompletionRef = (0, import_react11.useRef)();
|
|
1193
|
+
const agentSessionRef = (0, import_react11.useRef)(agentSession);
|
|
1068
1194
|
agentSessionRef.current = agentSession;
|
|
1195
|
+
const threadIdRef = (0, import_react11.useRef)(threadId);
|
|
1196
|
+
threadIdRef.current = threadId;
|
|
1197
|
+
const runIdRef = (0, import_react11.useRef)(runId);
|
|
1198
|
+
runIdRef.current = runId;
|
|
1069
1199
|
const publicApiKey = copilotConfig.publicApiKey;
|
|
1070
|
-
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [
|
|
1200
|
+
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [import_shared6.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
|
|
1071
1201
|
const runtimeClient = useCopilotRuntimeClient({
|
|
1072
1202
|
url: copilotConfig.chatApiEndpoint,
|
|
1073
1203
|
publicApiKey: copilotConfig.publicApiKey,
|
|
1074
1204
|
headers,
|
|
1075
1205
|
credentials: copilotConfig.credentials
|
|
1076
1206
|
});
|
|
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
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
jsonSchema: JSON.stringify((0, import_shared5.actionParametersToJsonSchema)(action.parameters || [])),
|
|
1110
|
-
available
|
|
1111
|
-
};
|
|
1112
|
-
}),
|
|
1113
|
-
url: window.location.href
|
|
1114
|
-
},
|
|
1115
|
-
threadId: threadIdRef.current,
|
|
1116
|
-
runId: runIdRef.current,
|
|
1117
|
-
messages: (0, import_runtime_client_gql4.convertMessagesToGqlInput)((0, import_runtime_client_gql4.filterAgentStateMessages)(messagesWithContext))
|
|
1118
|
-
}, copilotConfig.cloud ? {
|
|
1119
|
-
cloud: __spreadValues({}, ((_c = (_b = (_a = copilotConfig.cloud.guardrails) == null ? void 0 : _a.input) == null ? void 0 : _b.restrictToTopic) == null ? void 0 : _c.enabled) ? {
|
|
1120
|
-
guardrails: {
|
|
1121
|
-
inputValidationRules: {
|
|
1122
|
-
allowList: copilotConfig.cloud.guardrails.input.restrictToTopic.validTopics,
|
|
1123
|
-
denyList: copilotConfig.cloud.guardrails.input.restrictToTopic.invalidTopics
|
|
1207
|
+
const runChatCompletion = useAsyncCallback(
|
|
1208
|
+
(previousMessages) => __async(this, null, function* () {
|
|
1209
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
1210
|
+
setIsLoading(true);
|
|
1211
|
+
let newMessages = [
|
|
1212
|
+
new import_runtime_client_gql5.TextMessage({
|
|
1213
|
+
content: "",
|
|
1214
|
+
role: import_runtime_client_gql5.Role.Assistant
|
|
1215
|
+
})
|
|
1216
|
+
];
|
|
1217
|
+
chatAbortControllerRef.current = new AbortController();
|
|
1218
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
1219
|
+
const systemMessage = makeSystemMessageCallback();
|
|
1220
|
+
const messagesWithContext = [systemMessage, ...initialMessages || [], ...previousMessages];
|
|
1221
|
+
const isAgentRun = agentSessionRef.current !== null;
|
|
1222
|
+
const stream = runtimeClient.asStream(
|
|
1223
|
+
runtimeClient.generateCopilotResponse({
|
|
1224
|
+
data: __spreadProps(__spreadValues(__spreadProps(__spreadValues({
|
|
1225
|
+
frontend: {
|
|
1226
|
+
actions: processActionsForRuntimeRequest(actions),
|
|
1227
|
+
url: window.location.href
|
|
1228
|
+
},
|
|
1229
|
+
threadId: threadIdRef.current,
|
|
1230
|
+
runId: runIdRef.current,
|
|
1231
|
+
messages: (0, import_runtime_client_gql5.convertMessagesToGqlInput)((0, import_runtime_client_gql5.filterAgentStateMessages)(messagesWithContext))
|
|
1232
|
+
}, copilotConfig.cloud ? {
|
|
1233
|
+
cloud: __spreadValues({}, ((_c = (_b = (_a = copilotConfig.cloud.guardrails) == null ? void 0 : _a.input) == null ? void 0 : _b.restrictToTopic) == null ? void 0 : _c.enabled) ? {
|
|
1234
|
+
guardrails: {
|
|
1235
|
+
inputValidationRules: {
|
|
1236
|
+
allowList: copilotConfig.cloud.guardrails.input.restrictToTopic.validTopics,
|
|
1237
|
+
denyList: copilotConfig.cloud.guardrails.input.restrictToTopic.invalidTopics
|
|
1238
|
+
}
|
|
1124
1239
|
}
|
|
1240
|
+
} : {})
|
|
1241
|
+
} : {}), {
|
|
1242
|
+
metadata: {
|
|
1243
|
+
requestType: import_runtime_client_gql5.CopilotRequestType.Chat
|
|
1125
1244
|
}
|
|
1126
|
-
}
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1245
|
+
}), agentSessionRef.current ? {
|
|
1246
|
+
agentSession: agentSessionRef.current
|
|
1247
|
+
} : {}), {
|
|
1248
|
+
agentStates: Object.values(coagentStatesRef.current).map((state) => ({
|
|
1249
|
+
agentName: state.name,
|
|
1250
|
+
state: JSON.stringify(state.state)
|
|
1251
|
+
}))
|
|
1252
|
+
}),
|
|
1253
|
+
properties: copilotConfig.properties,
|
|
1254
|
+
signal: (_d = chatAbortControllerRef.current) == null ? void 0 : _d.signal
|
|
1255
|
+
})
|
|
1256
|
+
);
|
|
1257
|
+
const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
|
|
1258
|
+
const reader = stream.getReader();
|
|
1259
|
+
let executedCoAgentStateRenders = [];
|
|
1260
|
+
let followUp = void 0;
|
|
1261
|
+
let messages2 = [];
|
|
1262
|
+
let syncedMessages = [];
|
|
1263
|
+
try {
|
|
1264
|
+
while (true) {
|
|
1265
|
+
let done, value;
|
|
1266
|
+
try {
|
|
1267
|
+
const readResult = yield reader.read();
|
|
1268
|
+
done = readResult.done;
|
|
1269
|
+
value = readResult.value;
|
|
1270
|
+
} catch (readError) {
|
|
1271
|
+
break;
|
|
1130
1272
|
}
|
|
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
|
-
threadIdRef.current = value.generateCopilotResponse.threadId || null;
|
|
1165
|
-
runIdRef.current = value.generateCopilotResponse.runId || null;
|
|
1166
|
-
const messages2 = (0, import_runtime_client_gql4.convertGqlOutputToMessages)(
|
|
1167
|
-
(0, import_runtime_client_gql4.filterAdjacentAgentStateMessages)(value.generateCopilotResponse.messages)
|
|
1168
|
-
);
|
|
1169
|
-
if (messages2.length === 0) {
|
|
1170
|
-
continue;
|
|
1171
|
-
}
|
|
1172
|
-
newMessages = [];
|
|
1173
|
-
if (((_h = value.generateCopilotResponse.status) == null ? void 0 : _h.__typename) === "FailedResponseStatus" && value.generateCopilotResponse.status.reason === "GUARDRAILS_VALIDATION_FAILED") {
|
|
1174
|
-
newMessages = [
|
|
1175
|
-
new import_runtime_client_gql4.TextMessage({
|
|
1176
|
-
role: import_runtime_client_gql4.MessageRole.Assistant,
|
|
1177
|
-
content: ((_i = value.generateCopilotResponse.status.details) == null ? void 0 : _i.guardrailsReason) || ""
|
|
1178
|
-
})
|
|
1179
|
-
];
|
|
1180
|
-
} else {
|
|
1181
|
-
for (const message of messages2) {
|
|
1182
|
-
newMessages.push(message);
|
|
1183
|
-
if (message.isActionExecutionMessage() && message.status.code !== import_runtime_client_gql4.MessageStatusCode.Pending && message.scope === "client" && onFunctionCall) {
|
|
1184
|
-
if (!(message.id in actionResults)) {
|
|
1273
|
+
if (done) {
|
|
1274
|
+
if (chatAbortControllerRef.current.signal.aborted) {
|
|
1275
|
+
return [];
|
|
1276
|
+
}
|
|
1277
|
+
break;
|
|
1278
|
+
}
|
|
1279
|
+
if (!(value == null ? void 0 : value.generateCopilotResponse)) {
|
|
1280
|
+
continue;
|
|
1281
|
+
}
|
|
1282
|
+
threadIdRef.current = value.generateCopilotResponse.threadId || null;
|
|
1283
|
+
runIdRef.current = value.generateCopilotResponse.runId || null;
|
|
1284
|
+
setThreadId(threadIdRef.current);
|
|
1285
|
+
setRunId(runIdRef.current);
|
|
1286
|
+
messages2 = (0, import_runtime_client_gql5.convertGqlOutputToMessages)(
|
|
1287
|
+
(0, import_runtime_client_gql5.filterAdjacentAgentStateMessages)(value.generateCopilotResponse.messages)
|
|
1288
|
+
);
|
|
1289
|
+
if (messages2.length === 0) {
|
|
1290
|
+
continue;
|
|
1291
|
+
}
|
|
1292
|
+
newMessages = [];
|
|
1293
|
+
if (((_h = value.generateCopilotResponse.status) == null ? void 0 : _h.__typename) === "FailedResponseStatus" && value.generateCopilotResponse.status.reason === "GUARDRAILS_VALIDATION_FAILED") {
|
|
1294
|
+
newMessages = [
|
|
1295
|
+
new import_runtime_client_gql5.TextMessage({
|
|
1296
|
+
role: import_runtime_client_gql5.MessageRole.Assistant,
|
|
1297
|
+
content: ((_i = value.generateCopilotResponse.status.details) == null ? void 0 : _i.guardrailsReason) || ""
|
|
1298
|
+
})
|
|
1299
|
+
];
|
|
1300
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
1301
|
+
break;
|
|
1302
|
+
} else {
|
|
1303
|
+
newMessages = [...messages2];
|
|
1304
|
+
for (const message of messages2) {
|
|
1305
|
+
if (message.isAgentStateMessage() && !message.active && !executedCoAgentStateRenders.includes(message.id) && onCoAgentStateRender) {
|
|
1185
1306
|
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
1186
1307
|
break;
|
|
1187
1308
|
}
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1309
|
+
yield onCoAgentStateRender({
|
|
1310
|
+
name: message.agentName,
|
|
1311
|
+
nodeName: message.nodeName,
|
|
1312
|
+
state: message.state
|
|
1313
|
+
});
|
|
1314
|
+
executedCoAgentStateRenders.push(message.id);
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
const lastAgentStateMessage = [...messages2].reverse().find((message) => message.isAgentStateMessage());
|
|
1318
|
+
if (lastAgentStateMessage) {
|
|
1319
|
+
if (lastAgentStateMessage.state.messages && lastAgentStateMessage.state.messages.length > 0) {
|
|
1320
|
+
syncedMessages = (0, import_runtime_client_gql5.loadMessagesFromJsonRepresentation)(
|
|
1321
|
+
lastAgentStateMessage.state.messages
|
|
1322
|
+
);
|
|
1323
|
+
}
|
|
1324
|
+
setCoagentStatesWithRef((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
1325
|
+
[lastAgentStateMessage.agentName]: {
|
|
1326
|
+
name: lastAgentStateMessage.agentName,
|
|
1327
|
+
state: lastAgentStateMessage.state,
|
|
1328
|
+
running: lastAgentStateMessage.running,
|
|
1329
|
+
active: lastAgentStateMessage.active,
|
|
1330
|
+
threadId: lastAgentStateMessage.threadId,
|
|
1331
|
+
nodeName: lastAgentStateMessage.nodeName,
|
|
1332
|
+
runId: lastAgentStateMessage.runId
|
|
1333
|
+
}
|
|
1334
|
+
}));
|
|
1335
|
+
if (lastAgentStateMessage.running) {
|
|
1336
|
+
setAgentSession({
|
|
1337
|
+
threadId: lastAgentStateMessage.threadId,
|
|
1338
|
+
agentName: lastAgentStateMessage.agentName,
|
|
1339
|
+
nodeName: lastAgentStateMessage.nodeName
|
|
1340
|
+
});
|
|
1341
|
+
} else {
|
|
1342
|
+
setAgentSession(null);
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
if (newMessages.length > 0) {
|
|
1347
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
const finalMessages = constructFinalMessages(syncedMessages, previousMessages, newMessages);
|
|
1351
|
+
let didExecuteAction = false;
|
|
1352
|
+
if (onFunctionCall) {
|
|
1353
|
+
const lastMessages = [];
|
|
1354
|
+
for (let i = finalMessages.length - 1; i >= 0; i--) {
|
|
1355
|
+
const message = finalMessages[i];
|
|
1356
|
+
if (message.isActionExecutionMessage() && message.status.code !== import_runtime_client_gql5.MessageStatusCode.Pending) {
|
|
1357
|
+
lastMessages.unshift(message);
|
|
1358
|
+
} else {
|
|
1359
|
+
break;
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
for (const message of lastMessages) {
|
|
1363
|
+
setMessages(finalMessages);
|
|
1364
|
+
const action = actions.find((action2) => action2.name === message.name);
|
|
1365
|
+
if (action) {
|
|
1366
|
+
followUp = action.followUp;
|
|
1367
|
+
let result;
|
|
1368
|
+
try {
|
|
1369
|
+
result = yield Promise.race([
|
|
1370
|
+
onFunctionCall({
|
|
1195
1371
|
messages: previousMessages,
|
|
1196
1372
|
name: message.name,
|
|
1197
1373
|
args: message.arguments
|
|
1198
|
-
})
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1374
|
+
}),
|
|
1375
|
+
new Promise(
|
|
1376
|
+
(resolve) => {
|
|
1377
|
+
var _a2;
|
|
1378
|
+
return (_a2 = chatAbortControllerRef.current) == null ? void 0 : _a2.signal.addEventListener(
|
|
1379
|
+
"abort",
|
|
1380
|
+
() => resolve("Operation was aborted by the user")
|
|
1381
|
+
);
|
|
1382
|
+
}
|
|
1383
|
+
),
|
|
1384
|
+
// if the user stopped generation, we also abort consecutive actions
|
|
1385
|
+
new Promise((resolve) => {
|
|
1386
|
+
var _a2;
|
|
1387
|
+
if ((_a2 = chatAbortControllerRef.current) == null ? void 0 : _a2.signal.aborted) {
|
|
1388
|
+
resolve("Operation was aborted by the user");
|
|
1389
|
+
}
|
|
1390
|
+
})
|
|
1391
|
+
]);
|
|
1392
|
+
} catch (e) {
|
|
1393
|
+
result = `Failed to execute action ${message.name}`;
|
|
1394
|
+
console.error(`Failed to execute action ${message.name}: ${e}`);
|
|
1204
1395
|
}
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1396
|
+
didExecuteAction = true;
|
|
1397
|
+
const messageIndex = finalMessages.findIndex((msg) => msg.id === message.id);
|
|
1398
|
+
finalMessages.splice(
|
|
1399
|
+
messageIndex + 1,
|
|
1400
|
+
0,
|
|
1401
|
+
new import_runtime_client_gql5.ResultMessage({
|
|
1402
|
+
id: "result-" + message.id,
|
|
1403
|
+
result: import_runtime_client_gql5.ResultMessage.encodeResult(result),
|
|
1208
1404
|
actionExecutionId: message.id,
|
|
1209
1405
|
actionName: message.name
|
|
1210
1406
|
})
|
|
1211
1407
|
);
|
|
1212
1408
|
}
|
|
1213
|
-
if (message.isAgentStateMessage() && !message.active && !executedCoAgentStateRenders.includes(message.id) && onCoAgentStateRender) {
|
|
1214
|
-
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
1215
|
-
break;
|
|
1216
|
-
}
|
|
1217
|
-
yield onCoAgentStateRender({
|
|
1218
|
-
name: message.agentName,
|
|
1219
|
-
nodeName: message.nodeName,
|
|
1220
|
-
state: message.state
|
|
1221
|
-
});
|
|
1222
|
-
executedCoAgentStateRenders.push(message.id);
|
|
1223
|
-
}
|
|
1224
1409
|
}
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
if (
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
nodeName: lastAgentStateMessage.nodeName
|
|
1243
|
-
});
|
|
1244
|
-
} else {
|
|
1245
|
-
setAgentSession(null);
|
|
1410
|
+
setMessages(finalMessages);
|
|
1411
|
+
}
|
|
1412
|
+
if (
|
|
1413
|
+
// if followUp is not explicitly false
|
|
1414
|
+
followUp !== false && // and we executed an action
|
|
1415
|
+
(didExecuteAction || // the last message is a server side result
|
|
1416
|
+
!isAgentRun && finalMessages.length && finalMessages[finalMessages.length - 1].isResultMessage()) && // the user did not stop generation
|
|
1417
|
+
!((_j = chatAbortControllerRef.current) == null ? void 0 : _j.signal.aborted)
|
|
1418
|
+
) {
|
|
1419
|
+
yield new Promise((resolve) => setTimeout(resolve, 10));
|
|
1420
|
+
return yield runChatCompletionRef.current(finalMessages);
|
|
1421
|
+
} else if ((_k = chatAbortControllerRef.current) == null ? void 0 : _k.signal.aborted) {
|
|
1422
|
+
const repairedMessages = finalMessages.filter((message, actionExecutionIndex) => {
|
|
1423
|
+
if (message.isActionExecutionMessage()) {
|
|
1424
|
+
return finalMessages.find(
|
|
1425
|
+
(msg, resultIndex) => msg.isResultMessage() && msg.actionExecutionId === message.id && resultIndex === actionExecutionIndex + 1
|
|
1426
|
+
);
|
|
1246
1427
|
}
|
|
1428
|
+
return true;
|
|
1429
|
+
});
|
|
1430
|
+
const repairedMessageIds = repairedMessages.map((message) => message.id);
|
|
1431
|
+
setMessages(repairedMessages);
|
|
1432
|
+
if ((_l = agentSessionRef.current) == null ? void 0 : _l.nodeName) {
|
|
1433
|
+
setAgentSession({
|
|
1434
|
+
threadId: agentSessionRef.current.threadId,
|
|
1435
|
+
agentName: agentSessionRef.current.agentName,
|
|
1436
|
+
nodeName: "__end__"
|
|
1437
|
+
});
|
|
1247
1438
|
}
|
|
1439
|
+
return newMessages.filter((message) => repairedMessageIds.includes(message.id));
|
|
1440
|
+
} else {
|
|
1441
|
+
return newMessages.slice();
|
|
1248
1442
|
}
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
}
|
|
1443
|
+
} finally {
|
|
1444
|
+
setIsLoading(false);
|
|
1252
1445
|
}
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1446
|
+
}),
|
|
1447
|
+
[
|
|
1448
|
+
messages,
|
|
1449
|
+
setMessages,
|
|
1450
|
+
makeSystemMessageCallback,
|
|
1451
|
+
copilotConfig,
|
|
1452
|
+
setIsLoading,
|
|
1453
|
+
initialMessages,
|
|
1454
|
+
isLoading,
|
|
1455
|
+
actions,
|
|
1456
|
+
onFunctionCall,
|
|
1457
|
+
onCoAgentStateRender,
|
|
1458
|
+
setCoagentStatesWithRef,
|
|
1459
|
+
coagentStatesRef,
|
|
1460
|
+
agentSession,
|
|
1461
|
+
setAgentSession
|
|
1462
|
+
]
|
|
1463
|
+
);
|
|
1268
1464
|
runChatCompletionRef.current = runChatCompletion;
|
|
1269
|
-
const runChatCompletionAndHandleFunctionCall = (
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1465
|
+
const runChatCompletionAndHandleFunctionCall = useAsyncCallback(
|
|
1466
|
+
(messages2) => __async(this, null, function* () {
|
|
1467
|
+
yield runChatCompletionRef.current(messages2);
|
|
1468
|
+
}),
|
|
1469
|
+
[messages]
|
|
1470
|
+
);
|
|
1471
|
+
const append = useAsyncCallback(
|
|
1472
|
+
(message) => __async(this, null, function* () {
|
|
1473
|
+
if (isLoading) {
|
|
1474
|
+
return;
|
|
1475
|
+
}
|
|
1476
|
+
const newMessages = [...messages, message];
|
|
1477
|
+
setMessages(newMessages);
|
|
1478
|
+
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
1479
|
+
}),
|
|
1480
|
+
[isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]
|
|
1481
|
+
);
|
|
1482
|
+
const reload = useAsyncCallback(() => __async(this, null, function* () {
|
|
1281
1483
|
if (isLoading || messages.length === 0) {
|
|
1282
1484
|
return;
|
|
1283
1485
|
}
|
|
@@ -1288,10 +1490,10 @@ function useChat(options) {
|
|
|
1288
1490
|
}
|
|
1289
1491
|
setMessages(newMessages);
|
|
1290
1492
|
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
1291
|
-
});
|
|
1493
|
+
}), [isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]);
|
|
1292
1494
|
const stop = () => {
|
|
1293
1495
|
var _a;
|
|
1294
|
-
(_a =
|
|
1496
|
+
(_a = chatAbortControllerRef.current) == null ? void 0 : _a.abort("Stop was called");
|
|
1295
1497
|
};
|
|
1296
1498
|
return {
|
|
1297
1499
|
append,
|
|
@@ -1300,6 +1502,23 @@ function useChat(options) {
|
|
|
1300
1502
|
runChatCompletion: () => runChatCompletionRef.current(messages)
|
|
1301
1503
|
};
|
|
1302
1504
|
}
|
|
1505
|
+
function constructFinalMessages(syncedMessages, previousMessages, newMessages) {
|
|
1506
|
+
const finalMessages = syncedMessages.length > 0 ? [...syncedMessages] : [...previousMessages, ...newMessages];
|
|
1507
|
+
if (syncedMessages.length > 0) {
|
|
1508
|
+
const messagesWithAgentState = [...previousMessages, ...newMessages];
|
|
1509
|
+
let previousMessageId = void 0;
|
|
1510
|
+
for (const message of messagesWithAgentState) {
|
|
1511
|
+
if (message.isAgentStateMessage()) {
|
|
1512
|
+
const index = finalMessages.findIndex((msg) => msg.id === previousMessageId);
|
|
1513
|
+
if (index !== -1) {
|
|
1514
|
+
finalMessages.splice(index + 1, 0, message);
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
previousMessageId = message.id;
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
1520
|
+
return finalMessages;
|
|
1521
|
+
}
|
|
1303
1522
|
|
|
1304
1523
|
// src/hooks/use-copilot-chat.ts
|
|
1305
1524
|
function useCopilotChat(_a = {}) {
|
|
@@ -1320,25 +1539,31 @@ function useCopilotChat(_a = {}) {
|
|
|
1320
1539
|
setCoagentStatesWithRef,
|
|
1321
1540
|
coAgentStateRenders,
|
|
1322
1541
|
agentSession,
|
|
1323
|
-
setAgentSession
|
|
1542
|
+
setAgentSession,
|
|
1543
|
+
agentLock,
|
|
1544
|
+
threadId,
|
|
1545
|
+
setThreadId,
|
|
1546
|
+
runId,
|
|
1547
|
+
setRunId,
|
|
1548
|
+
chatAbortControllerRef
|
|
1324
1549
|
} = useCopilotContext();
|
|
1325
1550
|
const { messages, setMessages } = useCopilotMessagesContext();
|
|
1326
1551
|
const latestGetContextString = useUpdatedRef(getContextString);
|
|
1327
|
-
const deleteMessage = (0,
|
|
1552
|
+
const deleteMessage = (0, import_react12.useCallback)(
|
|
1328
1553
|
(messageId) => {
|
|
1329
1554
|
setMessages((prev) => prev.filter((message) => message.id !== messageId));
|
|
1330
1555
|
},
|
|
1331
1556
|
[setMessages]
|
|
1332
1557
|
);
|
|
1333
|
-
const makeSystemMessageCallback = (0,
|
|
1558
|
+
const makeSystemMessageCallback = (0, import_react12.useCallback)(() => {
|
|
1334
1559
|
const systemMessageMaker = makeSystemMessage2 || defaultSystemMessage;
|
|
1335
1560
|
const contextString = latestGetContextString.current([], defaultCopilotContextCategories);
|
|
1336
|
-
return new
|
|
1561
|
+
return new import_runtime_client_gql6.TextMessage({
|
|
1337
1562
|
content: systemMessageMaker(contextString, chatInstructions),
|
|
1338
|
-
role:
|
|
1563
|
+
role: import_runtime_client_gql6.Role.System
|
|
1339
1564
|
});
|
|
1340
1565
|
}, [getContextString, makeSystemMessage2, chatInstructions]);
|
|
1341
|
-
const onCoAgentStateRender = (
|
|
1566
|
+
const onCoAgentStateRender = useAsyncCallback(
|
|
1342
1567
|
(args) => __async(this, null, function* () {
|
|
1343
1568
|
var _a2;
|
|
1344
1569
|
const { name, nodeName, state } = args;
|
|
@@ -1370,55 +1595,86 @@ function useCopilotChat(_a = {}) {
|
|
|
1370
1595
|
coagentStatesRef,
|
|
1371
1596
|
setCoagentStatesWithRef,
|
|
1372
1597
|
agentSession,
|
|
1373
|
-
setAgentSession
|
|
1598
|
+
setAgentSession,
|
|
1599
|
+
threadId,
|
|
1600
|
+
setThreadId,
|
|
1601
|
+
runId,
|
|
1602
|
+
setRunId,
|
|
1603
|
+
chatAbortControllerRef
|
|
1374
1604
|
}));
|
|
1375
1605
|
const latestAppend = useUpdatedRef(append);
|
|
1376
|
-
const latestAppendFunc = (
|
|
1377
|
-
(message) => {
|
|
1378
|
-
return latestAppend.current(message);
|
|
1379
|
-
},
|
|
1606
|
+
const latestAppendFunc = useAsyncCallback(
|
|
1607
|
+
(message) => __async(this, null, function* () {
|
|
1608
|
+
return yield latestAppend.current(message);
|
|
1609
|
+
}),
|
|
1380
1610
|
[latestAppend]
|
|
1381
1611
|
);
|
|
1382
1612
|
const latestReload = useUpdatedRef(reload);
|
|
1383
|
-
const latestReloadFunc = (
|
|
1384
|
-
return latestReload.current();
|
|
1385
|
-
}, [latestReload]);
|
|
1613
|
+
const latestReloadFunc = useAsyncCallback(() => __async(this, null, function* () {
|
|
1614
|
+
return yield latestReload.current();
|
|
1615
|
+
}), [latestReload]);
|
|
1386
1616
|
const latestStop = useUpdatedRef(stop);
|
|
1387
|
-
const latestStopFunc = (0,
|
|
1617
|
+
const latestStopFunc = (0, import_react12.useCallback)(() => {
|
|
1388
1618
|
return latestStop.current();
|
|
1389
1619
|
}, [latestStop]);
|
|
1390
1620
|
const latestDelete = useUpdatedRef(deleteMessage);
|
|
1391
|
-
const latestDeleteFunc = (0,
|
|
1621
|
+
const latestDeleteFunc = (0, import_react12.useCallback)(
|
|
1392
1622
|
(messageId) => {
|
|
1393
1623
|
return latestDelete.current(messageId);
|
|
1394
1624
|
},
|
|
1395
1625
|
[latestDelete]
|
|
1396
1626
|
);
|
|
1397
1627
|
const latestSetMessages = useUpdatedRef(setMessages);
|
|
1398
|
-
const latestSetMessagesFunc = (0,
|
|
1628
|
+
const latestSetMessagesFunc = (0, import_react12.useCallback)(
|
|
1399
1629
|
(messages2) => {
|
|
1400
1630
|
return latestSetMessages.current(messages2);
|
|
1401
1631
|
},
|
|
1402
1632
|
[latestSetMessages]
|
|
1403
1633
|
);
|
|
1404
1634
|
const latestRunChatCompletion = useUpdatedRef(runChatCompletion);
|
|
1405
|
-
const latestRunChatCompletionFunc = (
|
|
1406
|
-
return latestRunChatCompletion.current();
|
|
1407
|
-
}, [latestRunChatCompletion]);
|
|
1635
|
+
const latestRunChatCompletionFunc = useAsyncCallback(() => __async(this, null, function* () {
|
|
1636
|
+
return yield latestRunChatCompletion.current();
|
|
1637
|
+
}), [latestRunChatCompletion]);
|
|
1638
|
+
const reset = (0, import_react12.useCallback)(() => {
|
|
1639
|
+
latestStopFunc();
|
|
1640
|
+
setMessages([]);
|
|
1641
|
+
setThreadId(null);
|
|
1642
|
+
setRunId(null);
|
|
1643
|
+
setCoagentStatesWithRef({});
|
|
1644
|
+
let initialAgentSession = null;
|
|
1645
|
+
if (agentLock) {
|
|
1646
|
+
initialAgentSession = {
|
|
1647
|
+
agentName: agentLock
|
|
1648
|
+
};
|
|
1649
|
+
}
|
|
1650
|
+
setAgentSession(initialAgentSession);
|
|
1651
|
+
}, [
|
|
1652
|
+
latestStopFunc,
|
|
1653
|
+
setMessages,
|
|
1654
|
+
setThreadId,
|
|
1655
|
+
setCoagentStatesWithRef,
|
|
1656
|
+
setAgentSession,
|
|
1657
|
+
agentLock
|
|
1658
|
+
]);
|
|
1659
|
+
const latestReset = useUpdatedRef(reset);
|
|
1660
|
+
const latestResetFunc = (0, import_react12.useCallback)(() => {
|
|
1661
|
+
return latestReset.current();
|
|
1662
|
+
}, [latestReset]);
|
|
1408
1663
|
return {
|
|
1409
1664
|
visibleMessages: messages,
|
|
1410
1665
|
appendMessage: latestAppendFunc,
|
|
1411
1666
|
setMessages: latestSetMessagesFunc,
|
|
1412
1667
|
reloadMessages: latestReloadFunc,
|
|
1413
1668
|
stopGeneration: latestStopFunc,
|
|
1669
|
+
reset: latestResetFunc,
|
|
1414
1670
|
deleteMessage: latestDeleteFunc,
|
|
1415
1671
|
runChatCompletion: latestRunChatCompletionFunc,
|
|
1416
1672
|
isLoading
|
|
1417
1673
|
};
|
|
1418
1674
|
}
|
|
1419
1675
|
function useUpdatedRef(value) {
|
|
1420
|
-
const ref = (0,
|
|
1421
|
-
(0,
|
|
1676
|
+
const ref = (0, import_react12.useRef)(value);
|
|
1677
|
+
(0, import_react12.useEffect)(() => {
|
|
1422
1678
|
ref.current = value;
|
|
1423
1679
|
}, [value]);
|
|
1424
1680
|
return ref;
|
|
@@ -1448,18 +1704,22 @@ ${additionalInstructions}` : "");
|
|
|
1448
1704
|
}
|
|
1449
1705
|
|
|
1450
1706
|
// src/hooks/use-copilot-action.ts
|
|
1451
|
-
var
|
|
1452
|
-
var
|
|
1707
|
+
var import_shared7 = require("@copilotkit/shared");
|
|
1708
|
+
var import_react13 = require("react");
|
|
1453
1709
|
function useCopilotAction(action, dependencies) {
|
|
1454
1710
|
const { setAction, removeAction, actions, chatComponentsCache } = useCopilotContext();
|
|
1455
|
-
const idRef = (0,
|
|
1456
|
-
const renderAndWaitRef = (0,
|
|
1711
|
+
const idRef = (0, import_react13.useRef)((0, import_shared7.randomId)());
|
|
1712
|
+
const renderAndWaitRef = (0, import_react13.useRef)(null);
|
|
1457
1713
|
action = __spreadValues({}, action);
|
|
1458
|
-
if (
|
|
1714
|
+
if (
|
|
1715
|
+
// renderAndWaitForResponse is not available for catch all actions
|
|
1716
|
+
isFrontendAction(action) && // check if renderAndWaitForResponse is set
|
|
1717
|
+
(action.renderAndWait || action.renderAndWaitForResponse)
|
|
1718
|
+
) {
|
|
1459
1719
|
const renderAndWait = action.renderAndWait || action.renderAndWaitForResponse;
|
|
1460
1720
|
action.renderAndWait = void 0;
|
|
1461
1721
|
action.renderAndWaitForResponse = void 0;
|
|
1462
|
-
action.handler = () => __async(this, null, function* () {
|
|
1722
|
+
action.handler = useAsyncCallback(() => __async(this, null, function* () {
|
|
1463
1723
|
let resolve;
|
|
1464
1724
|
let reject;
|
|
1465
1725
|
const promise = new Promise((resolvePromise, rejectPromise) => {
|
|
@@ -1468,7 +1728,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
1468
1728
|
});
|
|
1469
1729
|
renderAndWaitRef.current = { promise, resolve, reject };
|
|
1470
1730
|
return yield promise;
|
|
1471
|
-
});
|
|
1731
|
+
}), []);
|
|
1472
1732
|
action.render = (props) => {
|
|
1473
1733
|
const waitProps = {
|
|
1474
1734
|
status: props.status,
|
|
@@ -1488,12 +1748,14 @@ function useCopilotAction(action, dependencies) {
|
|
|
1488
1748
|
return renderAndWait(waitProps);
|
|
1489
1749
|
}
|
|
1490
1750
|
}
|
|
1491
|
-
return (0,
|
|
1751
|
+
return (0, import_react13.createElement)(import_react13.Fragment);
|
|
1492
1752
|
};
|
|
1493
1753
|
}
|
|
1494
1754
|
if (dependencies === void 0) {
|
|
1495
1755
|
if (actions[idRef.current]) {
|
|
1496
|
-
|
|
1756
|
+
if (isFrontendAction(action)) {
|
|
1757
|
+
actions[idRef.current].handler = action.handler;
|
|
1758
|
+
}
|
|
1497
1759
|
if (typeof action.render === "function") {
|
|
1498
1760
|
if (chatComponentsCache.current !== null) {
|
|
1499
1761
|
chatComponentsCache.current.actions[action.name] = action.render;
|
|
@@ -1501,7 +1763,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
1501
1763
|
}
|
|
1502
1764
|
}
|
|
1503
1765
|
}
|
|
1504
|
-
(0,
|
|
1766
|
+
(0, import_react13.useEffect)(() => {
|
|
1505
1767
|
setAction(idRef.current, action);
|
|
1506
1768
|
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
1507
1769
|
chatComponentsCache.current.actions[action.name] = action.render;
|
|
@@ -1512,31 +1774,34 @@ function useCopilotAction(action, dependencies) {
|
|
|
1512
1774
|
}, [
|
|
1513
1775
|
setAction,
|
|
1514
1776
|
removeAction,
|
|
1515
|
-
action.description,
|
|
1777
|
+
isFrontendAction(action) ? action.description : void 0,
|
|
1516
1778
|
action.name,
|
|
1517
|
-
action.disabled,
|
|
1518
|
-
action.available,
|
|
1779
|
+
isFrontendAction(action) ? action.disabled : void 0,
|
|
1780
|
+
isFrontendAction(action) ? action.available : void 0,
|
|
1519
1781
|
// This should be faster than deep equality checking
|
|
1520
1782
|
// In addition, all major JS engines guarantee the order of object keys
|
|
1521
|
-
JSON.stringify(action.parameters),
|
|
1783
|
+
JSON.stringify(isFrontendAction(action) ? action.parameters : []),
|
|
1522
1784
|
// include render only if it's a string
|
|
1523
1785
|
typeof action.render === "string" ? action.render : void 0,
|
|
1524
1786
|
// dependencies set by the developer
|
|
1525
1787
|
...dependencies || []
|
|
1526
1788
|
]);
|
|
1527
1789
|
}
|
|
1790
|
+
function isFrontendAction(action) {
|
|
1791
|
+
return action.name !== "*";
|
|
1792
|
+
}
|
|
1528
1793
|
|
|
1529
1794
|
// src/hooks/use-coagent-state-render.ts
|
|
1530
|
-
var
|
|
1531
|
-
var
|
|
1795
|
+
var import_react14 = require("react");
|
|
1796
|
+
var import_shared8 = require("@copilotkit/shared");
|
|
1532
1797
|
function useCoAgentStateRender(action, dependencies) {
|
|
1533
1798
|
const {
|
|
1534
1799
|
setCoAgentStateRender,
|
|
1535
1800
|
removeCoAgentStateRender,
|
|
1536
1801
|
coAgentStateRenders,
|
|
1537
1802
|
chatComponentsCache
|
|
1538
|
-
} = (0,
|
|
1539
|
-
const idRef = (0,
|
|
1803
|
+
} = (0, import_react14.useContext)(CopilotContext);
|
|
1804
|
+
const idRef = (0, import_react14.useRef)((0, import_shared8.randomId)());
|
|
1540
1805
|
const key = `${action.name}-${action.nodeName || "global"}`;
|
|
1541
1806
|
if (dependencies === void 0) {
|
|
1542
1807
|
if (coAgentStateRenders[idRef.current]) {
|
|
@@ -1548,7 +1813,7 @@ function useCoAgentStateRender(action, dependencies) {
|
|
|
1548
1813
|
}
|
|
1549
1814
|
}
|
|
1550
1815
|
}
|
|
1551
|
-
(0,
|
|
1816
|
+
(0, import_react14.useEffect)(() => {
|
|
1552
1817
|
setCoAgentStateRender(idRef.current, action);
|
|
1553
1818
|
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
1554
1819
|
chatComponentsCache.current.coAgentStateRenders[key] = action.render;
|
|
@@ -1568,11 +1833,11 @@ function useCoAgentStateRender(action, dependencies) {
|
|
|
1568
1833
|
}
|
|
1569
1834
|
|
|
1570
1835
|
// src/hooks/use-make-copilot-document-readable.ts
|
|
1571
|
-
var
|
|
1836
|
+
var import_react15 = require("react");
|
|
1572
1837
|
function useMakeCopilotDocumentReadable(document, categories, dependencies = []) {
|
|
1573
1838
|
const { addDocumentContext, removeDocumentContext } = useCopilotContext();
|
|
1574
|
-
const idRef = (0,
|
|
1575
|
-
(0,
|
|
1839
|
+
const idRef = (0, import_react15.useRef)();
|
|
1840
|
+
(0, import_react15.useEffect)(() => {
|
|
1576
1841
|
const id = addDocumentContext(document, categories);
|
|
1577
1842
|
idRef.current = id;
|
|
1578
1843
|
return () => {
|
|
@@ -1583,16 +1848,16 @@ function useMakeCopilotDocumentReadable(document, categories, dependencies = [])
|
|
|
1583
1848
|
}
|
|
1584
1849
|
|
|
1585
1850
|
// src/hooks/use-copilot-readable.ts
|
|
1586
|
-
var
|
|
1851
|
+
var import_react16 = require("react");
|
|
1587
1852
|
function convertToJSON(description, value) {
|
|
1588
1853
|
return `${description}: ${typeof value === "string" ? value : JSON.stringify(value)}`;
|
|
1589
1854
|
}
|
|
1590
1855
|
function useCopilotReadable({ description, value, parentId, categories, convert }, dependencies) {
|
|
1591
1856
|
const { addContext, removeContext } = useCopilotContext();
|
|
1592
|
-
const idRef = (0,
|
|
1857
|
+
const idRef = (0, import_react16.useRef)();
|
|
1593
1858
|
convert = convert || convertToJSON;
|
|
1594
1859
|
const information = convert(description, value);
|
|
1595
|
-
(0,
|
|
1860
|
+
(0, import_react16.useEffect)(() => {
|
|
1596
1861
|
const id = addContext(information, parentId, categories);
|
|
1597
1862
|
idRef.current = id;
|
|
1598
1863
|
return () => {
|
|
@@ -1603,7 +1868,7 @@ function useCopilotReadable({ description, value, parentId, categories, convert
|
|
|
1603
1868
|
}
|
|
1604
1869
|
|
|
1605
1870
|
// src/hooks/use-coagent.ts
|
|
1606
|
-
var
|
|
1871
|
+
var import_react17 = require("react");
|
|
1607
1872
|
function useCoAgent(options) {
|
|
1608
1873
|
const isExternalStateManagement = (options2) => {
|
|
1609
1874
|
return "state" in options2 && "setState" in options2;
|
|
@@ -1643,28 +1908,33 @@ function useCoAgent(options) {
|
|
|
1643
1908
|
};
|
|
1644
1909
|
const coagentState = getCoagentState(coagentStates, name);
|
|
1645
1910
|
const state = isExternalStateManagement(options) ? options.state : coagentState.state;
|
|
1646
|
-
(0,
|
|
1911
|
+
(0, import_react17.useEffect)(() => {
|
|
1647
1912
|
if (isExternalStateManagement(options)) {
|
|
1648
1913
|
setState(options.state);
|
|
1649
1914
|
} else if (coagentStates[name] === void 0) {
|
|
1650
1915
|
setState(options.initialState === void 0 ? {} : options.initialState);
|
|
1651
1916
|
}
|
|
1652
|
-
}, [
|
|
1917
|
+
}, [
|
|
1918
|
+
isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0,
|
|
1919
|
+
// reset initialstate on reset
|
|
1920
|
+
coagentStates[name] === void 0
|
|
1921
|
+
]);
|
|
1922
|
+
const runAgentCallback = useAsyncCallback(
|
|
1923
|
+
(hint) => __async(this, null, function* () {
|
|
1924
|
+
yield runAgent(name, context, appendMessage, runChatCompletion, hint);
|
|
1925
|
+
}),
|
|
1926
|
+
[name, context, appendMessage, runChatCompletion]
|
|
1927
|
+
);
|
|
1653
1928
|
return {
|
|
1654
1929
|
name,
|
|
1655
1930
|
nodeName: coagentState.nodeName,
|
|
1656
|
-
|
|
1657
|
-
setState,
|
|
1931
|
+
threadId: coagentState.threadId,
|
|
1658
1932
|
running: coagentState.running,
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
stop: () =>
|
|
1663
|
-
|
|
1664
|
-
},
|
|
1665
|
-
run: (hint) => {
|
|
1666
|
-
return runAgent(name, context, appendMessage, runChatCompletion, hint);
|
|
1667
|
-
}
|
|
1933
|
+
state: coagentState.state,
|
|
1934
|
+
setState: isExternalStateManagement(options) ? options.setState : setState,
|
|
1935
|
+
start: () => startAgent(name, context),
|
|
1936
|
+
stop: () => stopAgent(name, context),
|
|
1937
|
+
run: runAgentCallback
|
|
1668
1938
|
};
|
|
1669
1939
|
}
|
|
1670
1940
|
function startAgent(name, context) {
|
|
@@ -1677,6 +1947,17 @@ function stopAgent(name, context) {
|
|
|
1677
1947
|
const { agentSession, setAgentSession } = context;
|
|
1678
1948
|
if (agentSession && agentSession.agentName === name) {
|
|
1679
1949
|
setAgentSession(null);
|
|
1950
|
+
context.setCoagentStates((prevAgentStates) => {
|
|
1951
|
+
return __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
1952
|
+
[name]: __spreadProps(__spreadValues({}, prevAgentStates[name]), {
|
|
1953
|
+
running: false,
|
|
1954
|
+
active: false,
|
|
1955
|
+
threadId: void 0,
|
|
1956
|
+
nodeName: void 0,
|
|
1957
|
+
runId: void 0
|
|
1958
|
+
})
|
|
1959
|
+
});
|
|
1960
|
+
});
|
|
1680
1961
|
} else {
|
|
1681
1962
|
console.warn(`No agent session found for ${name}`);
|
|
1682
1963
|
}
|
|
@@ -1712,8 +1993,7 @@ function runAgent(name, context, appendMessage, runChatCompletion, hint) {
|
|
|
1712
1993
|
}
|
|
1713
1994
|
|
|
1714
1995
|
// src/lib/copilot-task.ts
|
|
1715
|
-
var
|
|
1716
|
-
var import_shared8 = require("@copilotkit/shared");
|
|
1996
|
+
var import_runtime_client_gql7 = require("@copilotkit/runtime-client-gql");
|
|
1717
1997
|
var CopilotTask = class {
|
|
1718
1998
|
constructor(config) {
|
|
1719
1999
|
this.instructions = config.instructions;
|
|
@@ -1740,12 +2020,12 @@ var CopilotTask = class {
|
|
|
1740
2020
|
if (this.includeCopilotReadable) {
|
|
1741
2021
|
contextString += context.getContextString([], defaultCopilotContextCategories);
|
|
1742
2022
|
}
|
|
1743
|
-
const systemMessage = new
|
|
2023
|
+
const systemMessage = new import_runtime_client_gql7.TextMessage({
|
|
1744
2024
|
content: taskSystemMessage(contextString, this.instructions),
|
|
1745
|
-
role:
|
|
2025
|
+
role: import_runtime_client_gql7.Role.System
|
|
1746
2026
|
});
|
|
1747
2027
|
const messages = [systemMessage];
|
|
1748
|
-
const runtimeClient = new
|
|
2028
|
+
const runtimeClient = new import_runtime_client_gql7.CopilotRuntimeClient({
|
|
1749
2029
|
url: context.copilotApiConfig.chatApiEndpoint,
|
|
1750
2030
|
publicApiKey: context.copilotApiConfig.publicApiKey,
|
|
1751
2031
|
headers: context.copilotApiConfig.headers,
|
|
@@ -1754,16 +2034,12 @@ var CopilotTask = class {
|
|
|
1754
2034
|
const response = yield runtimeClient.generateCopilotResponse({
|
|
1755
2035
|
data: {
|
|
1756
2036
|
frontend: {
|
|
1757
|
-
actions: Object.values(actions)
|
|
1758
|
-
name: action.name,
|
|
1759
|
-
description: action.description || "",
|
|
1760
|
-
jsonSchema: JSON.stringify((0, import_shared8.actionParametersToJsonSchema)(action.parameters || []))
|
|
1761
|
-
})),
|
|
2037
|
+
actions: processActionsForRuntimeRequest(Object.values(actions)),
|
|
1762
2038
|
url: window.location.href
|
|
1763
2039
|
},
|
|
1764
|
-
messages: (0,
|
|
2040
|
+
messages: (0, import_runtime_client_gql7.convertMessagesToGqlInput)((0, import_runtime_client_gql7.filterAgentStateMessages)(messages)),
|
|
1765
2041
|
metadata: {
|
|
1766
|
-
requestType:
|
|
2042
|
+
requestType: import_runtime_client_gql7.CopilotRequestType.Task
|
|
1767
2043
|
},
|
|
1768
2044
|
forwardedParameters: {
|
|
1769
2045
|
toolChoice: "required"
|
|
@@ -1772,7 +2048,7 @@ var CopilotTask = class {
|
|
|
1772
2048
|
properties: context.copilotApiConfig.properties
|
|
1773
2049
|
}).toPromise();
|
|
1774
2050
|
const functionCallHandler = context.getFunctionCallHandler(actions);
|
|
1775
|
-
const functionCalls = (0,
|
|
2051
|
+
const functionCalls = (0, import_runtime_client_gql7.convertGqlOutputToMessages)(
|
|
1776
2052
|
((_b = (_a = response.data) == null ? void 0 : _a.generateCopilotResponse) == null ? void 0 : _b.messages) || []
|
|
1777
2053
|
).filter((m) => m.isActionExecutionMessage());
|
|
1778
2054
|
for (const functionCall of functionCalls) {
|
|
@@ -1818,7 +2094,10 @@ ${instructions}
|
|
|
1818
2094
|
CopilotTask,
|
|
1819
2095
|
defaultCopilotContextCategories,
|
|
1820
2096
|
extract,
|
|
2097
|
+
runAgent,
|
|
1821
2098
|
shouldShowDevConsole,
|
|
2099
|
+
startAgent,
|
|
2100
|
+
stopAgent,
|
|
1822
2101
|
useCoAgent,
|
|
1823
2102
|
useCoAgentStateRender,
|
|
1824
2103
|
useCopilotAction,
|