@copilotkit/react-core 1.5.1-next.2 → 1.5.1-next.3
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 +9 -0
- package/dist/{chunk-LNI2Z45S.mjs → chunk-2KCEHGSI.mjs} +50 -100
- package/dist/chunk-2KCEHGSI.mjs.map +1 -0
- package/dist/{chunk-2KT73RVG.mjs → chunk-6OR25P32.mjs} +19 -75
- package/dist/chunk-6OR25P32.mjs.map +1 -0
- package/dist/{chunk-3GQPEN4M.mjs → chunk-DDVXFNYA.mjs} +32 -66
- package/dist/chunk-DDVXFNYA.mjs.map +1 -0
- package/dist/{chunk-XOQMISG3.mjs → chunk-FXHKK52V.mjs} +13 -14
- package/dist/chunk-FXHKK52V.mjs.map +1 -0
- package/dist/{chunk-HIUT2NLX.mjs → chunk-H4VZMKR6.mjs} +2 -2
- package/dist/{chunk-X36DNILG.mjs → chunk-H7LSKIDK.mjs} +23 -71
- package/dist/chunk-H7LSKIDK.mjs.map +1 -0
- package/dist/chunk-JDQCJCAQ.mjs +295 -0
- package/dist/chunk-JDQCJCAQ.mjs.map +1 -0
- package/dist/chunk-L34MHAXR.mjs +1 -0
- package/dist/{chunk-WXREY7UP.mjs → chunk-LODRWFMB.mjs} +4 -16
- package/dist/chunk-LODRWFMB.mjs.map +1 -0
- package/dist/{chunk-EWKJMQOF.mjs → chunk-MWZO6TUR.mjs} +13 -29
- package/dist/chunk-MWZO6TUR.mjs.map +1 -0
- package/dist/{chunk-6BQVZFNO.mjs → chunk-ODN4H66E.mjs} +2 -6
- package/dist/chunk-ODN4H66E.mjs.map +1 -0
- package/dist/{chunk-FQ53HQYZ.mjs → chunk-QDGDXRRJ.mjs} +2 -2
- package/dist/{chunk-22XKTBPF.mjs → chunk-XINZBP3J.mjs} +3 -15
- package/dist/chunk-XINZBP3J.mjs.map +1 -0
- package/dist/components/copilot-provider/copilot-messages.d.ts +0 -9
- package/dist/components/copilot-provider/copilotkit-props.d.ts +0 -21
- package/dist/components/copilot-provider/copilotkit-props.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.d.ts +0 -9
- package/dist/components/copilot-provider/copilotkit.js +95 -224
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +8 -9
- package/dist/components/copilot-provider/index.d.ts +0 -9
- package/dist/components/copilot-provider/index.js +95 -224
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +8 -9
- package/dist/components/index.d.ts +0 -9
- package/dist/components/index.js +95 -224
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +8 -9
- package/dist/components/toast/toast-provider.d.ts +1 -2
- package/dist/components/toast/toast-provider.js +63 -77
- 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 +4 -37
- package/dist/context/copilot-context.js +3 -15
- 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 +3 -15
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +4 -4
- package/dist/hooks/index.d.ts +1 -3
- package/dist/hooks/index.js +297 -703
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +23 -35
- package/dist/hooks/use-chat.d.ts +3 -35
- package/dist/hooks/use-chat.js +205 -453
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +3 -4
- package/dist/hooks/use-coagent-state-render.d.ts +2 -2
- package/dist/hooks/use-coagent-state-render.js +8 -43
- package/dist/hooks/use-coagent-state-render.js.map +1 -1
- package/dist/hooks/use-coagent-state-render.mjs +2 -4
- package/dist/hooks/use-coagent.d.ts +1 -14
- package/dist/hooks/use-coagent.js +265 -603
- package/dist/hooks/use-coagent.js.map +1 -1
- package/dist/hooks/use-coagent.mjs +12 -20
- package/dist/hooks/use-copilot-action.d.ts +2 -12
- package/dist/hooks/use-copilot-action.js +19 -169
- package/dist/hooks/use-copilot-action.js.map +1 -1
- package/dist/hooks/use-copilot-action.mjs +2 -4
- package/dist/hooks/use-copilot-chat.d.ts +0 -2
- package/dist/hooks/use-copilot-chat.js +240 -545
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +11 -13
- package/dist/hooks/use-copilot-readable.js +3 -15
- 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 +4 -110
- 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 +3 -15
- 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 +3 -4
- package/dist/index.js +398 -812
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +31 -43
- package/dist/lib/copilot-task.d.ts +1 -6
- package/dist/lib/copilot-task.js +17 -55
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +9 -11
- package/dist/lib/index.d.ts +1 -1
- package/dist/lib/index.js +17 -55
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +9 -11
- package/dist/types/frontend-action.d.ts +2 -21
- package/dist/types/frontend-action.js +0 -34
- package/dist/types/frontend-action.js.map +1 -1
- package/dist/types/frontend-action.mjs +0 -7
- package/dist/types/index.d.ts +1 -2
- package/dist/types/index.js.map +1 -1
- package/dist/utils/extract.d.ts +2 -3
- package/dist/utils/extract.js +3 -21
- package/dist/utils/extract.js.map +1 -1
- package/dist/utils/extract.mjs +8 -9
- package/dist/utils/index.js +3 -21
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +9 -10
- package/package.json +5 -5
- package/dist/chunk-22XKTBPF.mjs.map +0 -1
- package/dist/chunk-2KT73RVG.mjs.map +0 -1
- package/dist/chunk-3GQPEN4M.mjs.map +0 -1
- package/dist/chunk-6BQVZFNO.mjs.map +0 -1
- package/dist/chunk-EWKJMQOF.mjs.map +0 -1
- package/dist/chunk-LHKIIAUQ.mjs +0 -59
- package/dist/chunk-LHKIIAUQ.mjs.map +0 -1
- package/dist/chunk-LNI2Z45S.mjs.map +0 -1
- package/dist/chunk-NR2BTA6Z.mjs +0 -1
- package/dist/chunk-VGOFRULA.mjs +0 -401
- package/dist/chunk-VGOFRULA.mjs.map +0 -1
- package/dist/chunk-VNTDIJSG.mjs +0 -37
- package/dist/chunk-VNTDIJSG.mjs.map +0 -1
- package/dist/chunk-WXREY7UP.mjs.map +0 -1
- package/dist/chunk-X36DNILG.mjs.map +0 -1
- package/dist/chunk-XERJQUHA.mjs +0 -31
- package/dist/chunk-XERJQUHA.mjs.map +0 -1
- package/dist/chunk-XOQMISG3.mjs.map +0 -1
- package/dist/components/error-boundary/error-boundary.d.ts +0 -22
- package/dist/components/error-boundary/error-boundary.js +0 -183
- package/dist/components/error-boundary/error-boundary.js.map +0 -1
- package/dist/components/error-boundary/error-boundary.mjs +0 -12
- package/dist/components/error-boundary/error-boundary.mjs.map +0 -1
- package/dist/components/error-boundary/error-utils.d.ts +0 -11
- package/dist/components/error-boundary/error-utils.js +0 -177
- package/dist/components/error-boundary/error-utils.js.map +0 -1
- package/dist/components/error-boundary/error-utils.mjs +0 -13
- package/dist/components/error-boundary/error-utils.mjs.map +0 -1
- package/dist/hooks/use-copilot-authenticated-action.d.ts +0 -8
- package/dist/hooks/use-copilot-authenticated-action.js +0 -418
- package/dist/hooks/use-copilot-authenticated-action.js.map +0 -1
- package/dist/hooks/use-copilot-authenticated-action.mjs +0 -12
- package/dist/hooks/use-copilot-authenticated-action.mjs.map +0 -1
- /package/dist/{chunk-HIUT2NLX.mjs.map → chunk-H4VZMKR6.mjs.map} +0 -0
- /package/dist/{chunk-NR2BTA6Z.mjs.map → chunk-L34MHAXR.mjs.map} +0 -0
- /package/dist/{chunk-FQ53HQYZ.mjs.map → chunk-QDGDXRRJ.mjs.map} +0 -0
package/dist/index.js
CHANGED
|
@@ -87,14 +87,10 @@ __export(src_exports, {
|
|
|
87
87
|
CopilotTask: () => CopilotTask,
|
|
88
88
|
defaultCopilotContextCategories: () => defaultCopilotContextCategories,
|
|
89
89
|
extract: () => extract,
|
|
90
|
-
runAgent: () => runAgent,
|
|
91
90
|
shouldShowDevConsole: () => shouldShowDevConsole,
|
|
92
|
-
startAgent: () => startAgent,
|
|
93
|
-
stopAgent: () => stopAgent,
|
|
94
91
|
useCoAgent: () => useCoAgent,
|
|
95
92
|
useCoAgentStateRender: () => useCoAgentStateRender,
|
|
96
93
|
useCopilotAction: () => useCopilotAction,
|
|
97
|
-
useCopilotAuthenticatedAction: () => useCopilotAuthenticatedAction,
|
|
98
94
|
useCopilotChat: () => useCopilotChat,
|
|
99
95
|
useCopilotContext: () => useCopilotContext,
|
|
100
96
|
useCopilotMessagesContext: () => useCopilotMessagesContext,
|
|
@@ -105,7 +101,7 @@ __export(src_exports, {
|
|
|
105
101
|
module.exports = __toCommonJS(src_exports);
|
|
106
102
|
|
|
107
103
|
// src/components/copilot-provider/copilotkit.tsx
|
|
108
|
-
var
|
|
104
|
+
var import_react8 = require("react");
|
|
109
105
|
|
|
110
106
|
// src/context/copilot-context.tsx
|
|
111
107
|
var import_react = __toESM(require("react"));
|
|
@@ -156,22 +152,9 @@ var emptyCopilotContext = {
|
|
|
156
152
|
coagentStates: {},
|
|
157
153
|
setCoagentStates: () => {
|
|
158
154
|
},
|
|
159
|
-
coagentStatesRef: { current: {} },
|
|
160
|
-
setCoagentStatesWithRef: () => {
|
|
161
|
-
},
|
|
162
155
|
agentSession: null,
|
|
163
156
|
setAgentSession: () => {
|
|
164
|
-
}
|
|
165
|
-
forwardedParameters: {},
|
|
166
|
-
agentLock: null,
|
|
167
|
-
threadId: null,
|
|
168
|
-
setThreadId: () => {
|
|
169
|
-
},
|
|
170
|
-
runId: null,
|
|
171
|
-
setRunId: () => {
|
|
172
|
-
},
|
|
173
|
-
chatAbortControllerRef: { current: null },
|
|
174
|
-
availableAgents: []
|
|
157
|
+
}
|
|
175
158
|
};
|
|
176
159
|
var CopilotContext = import_react.default.createContext(emptyCopilotContext);
|
|
177
160
|
function useCopilotContext() {
|
|
@@ -181,8 +164,9 @@ function useCopilotContext() {
|
|
|
181
164
|
}
|
|
182
165
|
return context;
|
|
183
166
|
}
|
|
184
|
-
function returnAndThrowInDebug(
|
|
167
|
+
function returnAndThrowInDebug(value) {
|
|
185
168
|
throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
|
|
169
|
+
return value;
|
|
186
170
|
}
|
|
187
171
|
|
|
188
172
|
// src/hooks/use-tree.ts
|
|
@@ -424,9 +408,6 @@ function CopilotMessages(_a) {
|
|
|
424
408
|
}
|
|
425
409
|
|
|
426
410
|
// src/components/toast/toast-provider.tsx
|
|
427
|
-
var import_react7 = require("react");
|
|
428
|
-
|
|
429
|
-
// src/components/error-boundary/error-utils.tsx
|
|
430
411
|
var import_react6 = require("react");
|
|
431
412
|
|
|
432
413
|
// src/components/toast/exclamation-mark-icon.tsx
|
|
@@ -456,94 +437,11 @@ var ExclamationMarkIcon = ({
|
|
|
456
437
|
}
|
|
457
438
|
);
|
|
458
439
|
|
|
459
|
-
// src/components/error-boundary/error-utils.tsx
|
|
460
|
-
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
461
|
-
function ErrorToast({ errors }) {
|
|
462
|
-
const errorsToRender = errors.map((error, idx) => {
|
|
463
|
-
var _a, _b, _c;
|
|
464
|
-
const message = "extensions" in error ? (_b = (_a = error.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message : error.message;
|
|
465
|
-
const code = "extensions" in error ? (_c = error.extensions) == null ? void 0 : _c.code : null;
|
|
466
|
-
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
467
|
-
"div",
|
|
468
|
-
{
|
|
469
|
-
style: {
|
|
470
|
-
marginTop: idx === 0 ? 0 : 10,
|
|
471
|
-
marginBottom: 14
|
|
472
|
-
},
|
|
473
|
-
children: [
|
|
474
|
-
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ExclamationMarkIcon, { style: { marginBottom: 4 } }),
|
|
475
|
-
code && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
476
|
-
"div",
|
|
477
|
-
{
|
|
478
|
-
style: {
|
|
479
|
-
fontWeight: "600",
|
|
480
|
-
marginBottom: 4
|
|
481
|
-
},
|
|
482
|
-
children: [
|
|
483
|
-
"Copilot Cloud Error:",
|
|
484
|
-
" ",
|
|
485
|
-
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { style: { fontFamily: "monospace", fontWeight: "normal" }, children: code })
|
|
486
|
-
]
|
|
487
|
-
}
|
|
488
|
-
),
|
|
489
|
-
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: message })
|
|
490
|
-
]
|
|
491
|
-
},
|
|
492
|
-
idx
|
|
493
|
-
);
|
|
494
|
-
});
|
|
495
|
-
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
496
|
-
"div",
|
|
497
|
-
{
|
|
498
|
-
style: {
|
|
499
|
-
fontSize: "13px",
|
|
500
|
-
maxWidth: "600px"
|
|
501
|
-
},
|
|
502
|
-
children: [
|
|
503
|
-
errorsToRender,
|
|
504
|
-
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: { fontSize: "11px", opacity: 0.75 }, children: "NOTE: This error only displays during local development." })
|
|
505
|
-
]
|
|
506
|
-
}
|
|
507
|
-
);
|
|
508
|
-
}
|
|
509
|
-
function useErrorToast() {
|
|
510
|
-
const { addToast } = useToast();
|
|
511
|
-
return (0, import_react6.useCallback)(
|
|
512
|
-
(error) => {
|
|
513
|
-
const errorId = error.map((err) => {
|
|
514
|
-
var _a, _b;
|
|
515
|
-
const message = "extensions" in err ? ((_b = (_a = err.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message) || err.message : err.message;
|
|
516
|
-
const stack = err.stack || "";
|
|
517
|
-
return btoa(message + stack).slice(0, 32);
|
|
518
|
-
}).join("|");
|
|
519
|
-
addToast({
|
|
520
|
-
type: "error",
|
|
521
|
-
id: errorId,
|
|
522
|
-
// Toast libraries typically dedupe by id
|
|
523
|
-
message: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ErrorToast, { errors: error })
|
|
524
|
-
});
|
|
525
|
-
},
|
|
526
|
-
[addToast]
|
|
527
|
-
);
|
|
528
|
-
}
|
|
529
|
-
function useAsyncCallback(callback, deps) {
|
|
530
|
-
const addErrorToast = useErrorToast();
|
|
531
|
-
return (0, import_react6.useCallback)((...args) => __async(this, null, function* () {
|
|
532
|
-
try {
|
|
533
|
-
return yield callback(...args);
|
|
534
|
-
} catch (error) {
|
|
535
|
-
console.error("Error in async callback:", error);
|
|
536
|
-
addErrorToast([error]);
|
|
537
|
-
throw error;
|
|
538
|
-
}
|
|
539
|
-
}), deps);
|
|
540
|
-
}
|
|
541
|
-
|
|
542
440
|
// src/components/toast/toast-provider.tsx
|
|
543
|
-
var
|
|
544
|
-
var ToastContext = (0,
|
|
441
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
442
|
+
var ToastContext = (0, import_react6.createContext)(void 0);
|
|
545
443
|
function useToast() {
|
|
546
|
-
const context = (0,
|
|
444
|
+
const context = (0, import_react6.useContext)(ToastContext);
|
|
547
445
|
if (!context) {
|
|
548
446
|
throw new Error("useToast must be used within a ToastProvider");
|
|
549
447
|
}
|
|
@@ -553,28 +451,68 @@ function ToastProvider({
|
|
|
553
451
|
enabled,
|
|
554
452
|
children
|
|
555
453
|
}) {
|
|
556
|
-
const [toasts, setToasts] = (0,
|
|
557
|
-
const addToast = (0,
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
setToasts((currentToasts) => {
|
|
561
|
-
if (currentToasts.find((toast2) => toast2.id === id))
|
|
562
|
-
return currentToasts;
|
|
563
|
-
return [...currentToasts, __spreadProps(__spreadValues({}, toast), { id })];
|
|
564
|
-
});
|
|
454
|
+
const [toasts, setToasts] = (0, import_react6.useState)([]);
|
|
455
|
+
const addToast = (0, import_react6.useCallback)((toast) => {
|
|
456
|
+
const id = Math.random().toString(36).substring(2, 9);
|
|
457
|
+
setToasts((currentToasts) => [...currentToasts, __spreadProps(__spreadValues({}, toast), { id })]);
|
|
565
458
|
if (toast.duration) {
|
|
566
459
|
setTimeout(() => {
|
|
567
460
|
removeToast(id);
|
|
568
461
|
}, toast.duration);
|
|
569
462
|
}
|
|
570
463
|
}, []);
|
|
571
|
-
const addGraphQLErrorsToast = (0,
|
|
464
|
+
const addGraphQLErrorsToast = (0, import_react6.useCallback)((errors) => {
|
|
465
|
+
const errorsToRender = errors.map((error, idx) => {
|
|
466
|
+
var _a;
|
|
467
|
+
const message = error.message;
|
|
468
|
+
const code = (_a = error.extensions) == null ? void 0 : _a.code;
|
|
469
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
470
|
+
"div",
|
|
471
|
+
{
|
|
472
|
+
style: {
|
|
473
|
+
marginTop: idx === 0 ? 0 : 10,
|
|
474
|
+
marginBottom: 14
|
|
475
|
+
},
|
|
476
|
+
children: [
|
|
477
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ExclamationMarkIcon, { style: { marginBottom: 4 } }),
|
|
478
|
+
code && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
479
|
+
"div",
|
|
480
|
+
{
|
|
481
|
+
style: {
|
|
482
|
+
fontWeight: "600",
|
|
483
|
+
marginBottom: 4
|
|
484
|
+
},
|
|
485
|
+
children: [
|
|
486
|
+
"Copilot Cloud Error:",
|
|
487
|
+
" ",
|
|
488
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { style: { fontFamily: "monospace", fontWeight: "normal" }, children: code })
|
|
489
|
+
]
|
|
490
|
+
}
|
|
491
|
+
),
|
|
492
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: message })
|
|
493
|
+
]
|
|
494
|
+
},
|
|
495
|
+
idx
|
|
496
|
+
);
|
|
497
|
+
});
|
|
572
498
|
addToast({
|
|
573
499
|
type: "error",
|
|
574
|
-
message: /* @__PURE__ */ (0,
|
|
500
|
+
message: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
501
|
+
"div",
|
|
502
|
+
{
|
|
503
|
+
style: {
|
|
504
|
+
fontSize: "13px",
|
|
505
|
+
maxWidth: "600px"
|
|
506
|
+
},
|
|
507
|
+
children: [
|
|
508
|
+
errorsToRender,
|
|
509
|
+
/* @__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." })
|
|
510
|
+
]
|
|
511
|
+
}
|
|
512
|
+
)
|
|
575
513
|
});
|
|
576
514
|
}, []);
|
|
577
|
-
const removeToast = (0,
|
|
515
|
+
const removeToast = (0, import_react6.useCallback)((id) => {
|
|
578
516
|
setToasts((currentToasts) => currentToasts.filter((toast) => toast.id !== id));
|
|
579
517
|
}, []);
|
|
580
518
|
const value = {
|
|
@@ -584,8 +522,8 @@ function ToastProvider({
|
|
|
584
522
|
removeToast,
|
|
585
523
|
enabled
|
|
586
524
|
};
|
|
587
|
-
return /* @__PURE__ */ (0,
|
|
588
|
-
/* @__PURE__ */ (0,
|
|
525
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(ToastContext.Provider, { value, children: [
|
|
526
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
589
527
|
"div",
|
|
590
528
|
{
|
|
591
529
|
style: {
|
|
@@ -599,7 +537,7 @@ function ToastProvider({
|
|
|
599
537
|
gap: "0.5rem"
|
|
600
538
|
},
|
|
601
539
|
children: [
|
|
602
|
-
toasts.length > 1 && /* @__PURE__ */ (0,
|
|
540
|
+
toasts.length > 1 && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: { textAlign: "right" }, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
603
541
|
"button",
|
|
604
542
|
{
|
|
605
543
|
onClick: () => setToasts([]),
|
|
@@ -614,7 +552,7 @@ function ToastProvider({
|
|
|
614
552
|
children: "Close All"
|
|
615
553
|
}
|
|
616
554
|
) }),
|
|
617
|
-
toasts.map((toast) => /* @__PURE__ */ (0,
|
|
555
|
+
toasts.map((toast) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
618
556
|
Toast,
|
|
619
557
|
{
|
|
620
558
|
message: toast.message,
|
|
@@ -640,21 +578,21 @@ function Toast({
|
|
|
640
578
|
warning: "#eab308",
|
|
641
579
|
error: "#ef4444"
|
|
642
580
|
};
|
|
643
|
-
return /* @__PURE__ */ (0,
|
|
581
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
644
582
|
"div",
|
|
645
583
|
{
|
|
646
584
|
style: {
|
|
647
585
|
backgroundColor: bgColors[type],
|
|
648
586
|
color: "white",
|
|
649
|
-
padding: "0.5rem
|
|
587
|
+
padding: "0.5rem 1rem",
|
|
650
588
|
borderRadius: "0.25rem",
|
|
651
589
|
boxShadow: "0 2px 4px rgba(0,0,0,0.1)",
|
|
652
590
|
position: "relative",
|
|
653
591
|
minWidth: "200px"
|
|
654
592
|
},
|
|
655
593
|
children: [
|
|
656
|
-
/* @__PURE__ */ (0,
|
|
657
|
-
/* @__PURE__ */ (0,
|
|
594
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: message }),
|
|
595
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
658
596
|
"button",
|
|
659
597
|
{
|
|
660
598
|
onClick: onClose,
|
|
@@ -679,17 +617,14 @@ function Toast({
|
|
|
679
617
|
|
|
680
618
|
// src/hooks/use-copilot-runtime-client.ts
|
|
681
619
|
var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
|
|
682
|
-
var
|
|
620
|
+
var import_react7 = require("react");
|
|
683
621
|
var useCopilotRuntimeClient = (options) => {
|
|
684
622
|
const { addGraphQLErrorsToast } = useToast();
|
|
685
|
-
const
|
|
686
|
-
const runtimeClient = (0, import_react8.useMemo)(() => {
|
|
623
|
+
const runtimeClient = (0, import_react7.useMemo)(() => {
|
|
687
624
|
return new import_runtime_client_gql.CopilotRuntimeClient(__spreadProps(__spreadValues({}, options), {
|
|
688
625
|
handleGQLErrors: (error) => {
|
|
689
626
|
if (error.graphQLErrors.length) {
|
|
690
627
|
addGraphQLErrorsToast(error.graphQLErrors);
|
|
691
|
-
} else {
|
|
692
|
-
addErrorToast([error]);
|
|
693
628
|
}
|
|
694
629
|
}
|
|
695
630
|
}));
|
|
@@ -710,8 +645,7 @@ function extract(_0) {
|
|
|
710
645
|
data,
|
|
711
646
|
abortSignal,
|
|
712
647
|
stream,
|
|
713
|
-
requestType = import_runtime_client_gql2.CopilotRequestType.Task
|
|
714
|
-
forwardedParameters
|
|
648
|
+
requestType = import_runtime_client_gql2.CopilotRequestType.Task
|
|
715
649
|
}) {
|
|
716
650
|
var _a, _b;
|
|
717
651
|
const { messages } = context;
|
|
@@ -758,10 +692,10 @@ function extract(_0) {
|
|
|
758
692
|
metadata: {
|
|
759
693
|
requestType
|
|
760
694
|
},
|
|
761
|
-
forwardedParameters:
|
|
695
|
+
forwardedParameters: {
|
|
762
696
|
toolChoice: "function",
|
|
763
697
|
toolChoiceFunctionName: action.name
|
|
764
|
-
}
|
|
698
|
+
}
|
|
765
699
|
},
|
|
766
700
|
properties: context.copilotApiConfig.properties,
|
|
767
701
|
signal: abortSignal
|
|
@@ -845,41 +779,13 @@ function getHostname() {
|
|
|
845
779
|
return "";
|
|
846
780
|
}
|
|
847
781
|
|
|
848
|
-
// src/components/error-boundary/error-boundary.tsx
|
|
849
|
-
var import_react9 = __toESM(require("react"));
|
|
850
|
-
var import_jsx_runtime5 = require("react/jsx-runtime");
|
|
851
|
-
var CopilotErrorBoundary = class extends import_react9.default.Component {
|
|
852
|
-
constructor(props) {
|
|
853
|
-
super(props);
|
|
854
|
-
this.state = { hasError: false };
|
|
855
|
-
}
|
|
856
|
-
static getDerivedStateFromError(error) {
|
|
857
|
-
return { hasError: true, error };
|
|
858
|
-
}
|
|
859
|
-
componentDidCatch(error, errorInfo) {
|
|
860
|
-
console.error("CopilotKit Error:", error, errorInfo);
|
|
861
|
-
}
|
|
862
|
-
render() {
|
|
863
|
-
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(ErrorToast2, { error: this.state.error, children: this.props.children });
|
|
864
|
-
}
|
|
865
|
-
};
|
|
866
|
-
function ErrorToast2({ error, children }) {
|
|
867
|
-
const addErrorToast = useErrorToast();
|
|
868
|
-
(0, import_react9.useEffect)(() => {
|
|
869
|
-
if (error) {
|
|
870
|
-
addErrorToast([error]);
|
|
871
|
-
}
|
|
872
|
-
}, [error, addErrorToast]);
|
|
873
|
-
return children;
|
|
874
|
-
}
|
|
875
|
-
|
|
876
782
|
// src/components/copilot-provider/copilotkit.tsx
|
|
877
|
-
var
|
|
783
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
878
784
|
function CopilotKit(_a) {
|
|
879
785
|
var _b = _a, { children } = _b, props = __objRest(_b, ["children"]);
|
|
880
786
|
const showDevConsole = props.showDevConsole === void 0 ? "auto" : props.showDevConsole;
|
|
881
787
|
const enabled = shouldShowDevConsole(showDevConsole);
|
|
882
|
-
return /* @__PURE__ */ (0,
|
|
788
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(ToastProvider, { enabled, children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(CopilotKitInternal, __spreadProps(__spreadValues({}, props), { children })) });
|
|
883
789
|
}
|
|
884
790
|
function CopilotKitInternal(_a) {
|
|
885
791
|
var _b = _a, { children } = _b, props = __objRest(_b, ["children"]);
|
|
@@ -889,50 +795,49 @@ function CopilotKitInternal(_a) {
|
|
|
889
795
|
);
|
|
890
796
|
}
|
|
891
797
|
const chatApiEndpoint = props.runtimeUrl || import_shared4.COPILOT_CLOUD_CHAT_URL;
|
|
892
|
-
const [actions, setActions] = (0,
|
|
893
|
-
const [coAgentStateRenders, setCoAgentStateRenders] = (0,
|
|
894
|
-
const chatComponentsCache = (0,
|
|
798
|
+
const [actions, setActions] = (0, import_react8.useState)({});
|
|
799
|
+
const [coAgentStateRenders, setCoAgentStateRenders] = (0, import_react8.useState)({});
|
|
800
|
+
const chatComponentsCache = (0, import_react8.useRef)({
|
|
895
801
|
actions: {},
|
|
896
802
|
coAgentStateRenders: {}
|
|
897
803
|
});
|
|
898
804
|
const { addElement, removeElement, printTree } = use_tree_default();
|
|
899
|
-
const [isLoading, setIsLoading] = (0,
|
|
900
|
-
const [chatInstructions, setChatInstructions] = (0,
|
|
901
|
-
const [authStates, setAuthStates] = (0, import_react10.useState)({});
|
|
805
|
+
const [isLoading, setIsLoading] = (0, import_react8.useState)(false);
|
|
806
|
+
const [chatInstructions, setChatInstructions] = (0, import_react8.useState)("");
|
|
902
807
|
const {
|
|
903
808
|
addElement: addDocument,
|
|
904
809
|
removeElement: removeDocument,
|
|
905
810
|
allElements: allDocuments
|
|
906
811
|
} = use_flat_category_store_default();
|
|
907
|
-
const setAction = (0,
|
|
812
|
+
const setAction = (0, import_react8.useCallback)((id, action) => {
|
|
908
813
|
setActions((prevPoints) => {
|
|
909
814
|
return __spreadProps(__spreadValues({}, prevPoints), {
|
|
910
815
|
[id]: action
|
|
911
816
|
});
|
|
912
817
|
});
|
|
913
818
|
}, []);
|
|
914
|
-
const removeAction = (0,
|
|
819
|
+
const removeAction = (0, import_react8.useCallback)((id) => {
|
|
915
820
|
setActions((prevPoints) => {
|
|
916
821
|
const newPoints = __spreadValues({}, prevPoints);
|
|
917
822
|
delete newPoints[id];
|
|
918
823
|
return newPoints;
|
|
919
824
|
});
|
|
920
825
|
}, []);
|
|
921
|
-
const setCoAgentStateRender = (0,
|
|
826
|
+
const setCoAgentStateRender = (0, import_react8.useCallback)((id, stateRender) => {
|
|
922
827
|
setCoAgentStateRenders((prevPoints) => {
|
|
923
828
|
return __spreadProps(__spreadValues({}, prevPoints), {
|
|
924
829
|
[id]: stateRender
|
|
925
830
|
});
|
|
926
831
|
});
|
|
927
832
|
}, []);
|
|
928
|
-
const removeCoAgentStateRender = (0,
|
|
833
|
+
const removeCoAgentStateRender = (0, import_react8.useCallback)((id) => {
|
|
929
834
|
setCoAgentStateRenders((prevPoints) => {
|
|
930
835
|
const newPoints = __spreadValues({}, prevPoints);
|
|
931
836
|
delete newPoints[id];
|
|
932
837
|
return newPoints;
|
|
933
838
|
});
|
|
934
839
|
}, []);
|
|
935
|
-
const getContextString = (0,
|
|
840
|
+
const getContextString = (0, import_react8.useCallback)(
|
|
936
841
|
(documents, categories) => {
|
|
937
842
|
const documentsString = documents.map((document) => {
|
|
938
843
|
return `${document.name} (${document.sourceApplication}):
|
|
@@ -945,37 +850,37 @@ ${nonDocumentStrings}`;
|
|
|
945
850
|
},
|
|
946
851
|
[printTree]
|
|
947
852
|
);
|
|
948
|
-
const addContext = (0,
|
|
853
|
+
const addContext = (0, import_react8.useCallback)(
|
|
949
854
|
(context, parentId, categories = defaultCopilotContextCategories) => {
|
|
950
855
|
return addElement(context, categories, parentId);
|
|
951
856
|
},
|
|
952
857
|
[addElement]
|
|
953
858
|
);
|
|
954
|
-
const removeContext = (0,
|
|
859
|
+
const removeContext = (0, import_react8.useCallback)(
|
|
955
860
|
(id) => {
|
|
956
861
|
removeElement(id);
|
|
957
862
|
},
|
|
958
863
|
[removeElement]
|
|
959
864
|
);
|
|
960
|
-
const getFunctionCallHandler = (0,
|
|
865
|
+
const getFunctionCallHandler = (0, import_react8.useCallback)(
|
|
961
866
|
(customEntryPoints) => {
|
|
962
867
|
return entryPointsToFunctionCallHandler(Object.values(customEntryPoints || actions));
|
|
963
868
|
},
|
|
964
869
|
[actions]
|
|
965
870
|
);
|
|
966
|
-
const getDocumentsContext = (0,
|
|
871
|
+
const getDocumentsContext = (0, import_react8.useCallback)(
|
|
967
872
|
(categories) => {
|
|
968
873
|
return allDocuments(categories);
|
|
969
874
|
},
|
|
970
875
|
[allDocuments]
|
|
971
876
|
);
|
|
972
|
-
const addDocumentContext = (0,
|
|
877
|
+
const addDocumentContext = (0, import_react8.useCallback)(
|
|
973
878
|
(documentPointer, categories = defaultCopilotContextCategories) => {
|
|
974
879
|
return addDocument(documentPointer, categories);
|
|
975
880
|
},
|
|
976
881
|
[addDocument]
|
|
977
882
|
);
|
|
978
|
-
const removeDocumentContext = (0,
|
|
883
|
+
const removeDocumentContext = (0, import_react8.useCallback)(
|
|
979
884
|
(documentId) => {
|
|
980
885
|
removeDocument(documentId);
|
|
981
886
|
},
|
|
@@ -988,7 +893,7 @@ ${nonDocumentStrings}`;
|
|
|
988
893
|
);
|
|
989
894
|
}
|
|
990
895
|
}
|
|
991
|
-
const copilotApiConfig = (0,
|
|
896
|
+
const copilotApiConfig = (0, import_react8.useMemo)(() => {
|
|
992
897
|
var _a2, _b2;
|
|
993
898
|
let cloud = void 0;
|
|
994
899
|
if (props.publicApiKey) {
|
|
@@ -1023,27 +928,14 @@ ${nonDocumentStrings}`;
|
|
|
1023
928
|
props.credentials,
|
|
1024
929
|
props.cloudRestrictToTopic
|
|
1025
930
|
]);
|
|
1026
|
-
const headers = (
|
|
1027
|
-
const authHeaders = Object.values(authStates || {}).reduce((acc, state) => {
|
|
1028
|
-
if (state.status === "authenticated" && state.authHeaders) {
|
|
1029
|
-
return __spreadValues(__spreadValues({}, acc), Object.entries(state.authHeaders).reduce(
|
|
1030
|
-
(headers2, [key, value]) => __spreadProps(__spreadValues({}, headers2), {
|
|
1031
|
-
[key.startsWith("X-Custom-") ? key : `X-Custom-${key}`]: value
|
|
1032
|
-
}),
|
|
1033
|
-
{}
|
|
1034
|
-
));
|
|
1035
|
-
}
|
|
1036
|
-
return acc;
|
|
1037
|
-
}, {});
|
|
1038
|
-
return __spreadValues(__spreadValues(__spreadValues({}, copilotApiConfig.headers || {}), copilotApiConfig.publicApiKey ? { [import_shared4.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: copilotApiConfig.publicApiKey } : {}), authHeaders);
|
|
1039
|
-
}, [copilotApiConfig.headers, copilotApiConfig.publicApiKey, authStates]);
|
|
931
|
+
const headers = __spreadValues(__spreadValues({}, copilotApiConfig.headers || {}), copilotApiConfig.publicApiKey ? { [import_shared4.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: copilotApiConfig.publicApiKey } : {});
|
|
1040
932
|
const runtimeClient = useCopilotRuntimeClient({
|
|
1041
933
|
url: copilotApiConfig.chatApiEndpoint,
|
|
1042
934
|
publicApiKey: copilotApiConfig.publicApiKey,
|
|
1043
935
|
headers,
|
|
1044
936
|
credentials: copilotApiConfig.credentials
|
|
1045
937
|
});
|
|
1046
|
-
const [chatSuggestionConfiguration, setChatSuggestionConfiguration] = (0,
|
|
938
|
+
const [chatSuggestionConfiguration, setChatSuggestionConfiguration] = (0, import_react8.useState)({});
|
|
1047
939
|
const addChatSuggestionConfiguration = (id, suggestion) => {
|
|
1048
940
|
setChatSuggestionConfiguration((prev) => __spreadProps(__spreadValues({}, prev), { [id]: suggestion }));
|
|
1049
941
|
};
|
|
@@ -1053,41 +945,16 @@ ${nonDocumentStrings}`;
|
|
|
1053
945
|
return rest;
|
|
1054
946
|
});
|
|
1055
947
|
};
|
|
1056
|
-
const [
|
|
1057
|
-
const [coagentStates, setCoagentStates] = (0, import_react10.useState)({});
|
|
1058
|
-
const coagentStatesRef = (0, import_react10.useRef)({});
|
|
1059
|
-
const setCoagentStatesWithRef = (0, import_react10.useCallback)(
|
|
1060
|
-
(value) => {
|
|
1061
|
-
const newValue = typeof value === "function" ? value(coagentStatesRef.current) : value;
|
|
1062
|
-
coagentStatesRef.current = newValue;
|
|
1063
|
-
setCoagentStates((prev) => {
|
|
1064
|
-
return newValue;
|
|
1065
|
-
});
|
|
1066
|
-
},
|
|
1067
|
-
[]
|
|
1068
|
-
);
|
|
1069
|
-
(0, import_react10.useEffect)(() => {
|
|
1070
|
-
const fetchData = () => __async(this, null, function* () {
|
|
1071
|
-
var _a2;
|
|
1072
|
-
const result = yield runtimeClient.availableAgents();
|
|
1073
|
-
if ((_a2 = result.data) == null ? void 0 : _a2.availableAgents) {
|
|
1074
|
-
setAvailableAgents(result.data.availableAgents.agents);
|
|
1075
|
-
}
|
|
1076
|
-
});
|
|
1077
|
-
void fetchData();
|
|
1078
|
-
}, []);
|
|
948
|
+
const [coagentStates, setCoagentStates] = (0, import_react8.useState)({});
|
|
1079
949
|
let initialAgentSession = null;
|
|
1080
950
|
if (props.agent) {
|
|
1081
951
|
initialAgentSession = {
|
|
1082
952
|
agentName: props.agent
|
|
1083
953
|
};
|
|
1084
954
|
}
|
|
1085
|
-
const [agentSession, setAgentSession] = (0,
|
|
1086
|
-
const [threadId, setThreadId] = (0, import_react10.useState)(null);
|
|
1087
|
-
const [runId, setRunId] = (0, import_react10.useState)(null);
|
|
1088
|
-
const chatAbortControllerRef = (0, import_react10.useRef)(null);
|
|
955
|
+
const [agentSession, setAgentSession] = (0, import_react8.useState)(initialAgentSession);
|
|
1089
956
|
const showDevConsole = props.showDevConsole === void 0 ? "auto" : props.showDevConsole;
|
|
1090
|
-
return /* @__PURE__ */ (0,
|
|
957
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
1091
958
|
CopilotContext.Provider,
|
|
1092
959
|
{
|
|
1093
960
|
value: {
|
|
@@ -1116,24 +983,11 @@ ${nonDocumentStrings}`;
|
|
|
1116
983
|
showDevConsole,
|
|
1117
984
|
coagentStates,
|
|
1118
985
|
setCoagentStates,
|
|
1119
|
-
coagentStatesRef,
|
|
1120
|
-
setCoagentStatesWithRef,
|
|
1121
986
|
agentSession,
|
|
1122
987
|
setAgentSession,
|
|
1123
|
-
runtimeClient
|
|
1124
|
-
forwardedParameters: props.forwardedParameters || {},
|
|
1125
|
-
agentLock: props.agent || null,
|
|
1126
|
-
threadId,
|
|
1127
|
-
setThreadId,
|
|
1128
|
-
runId,
|
|
1129
|
-
setRunId,
|
|
1130
|
-
chatAbortControllerRef,
|
|
1131
|
-
availableAgents,
|
|
1132
|
-
authConfig: props.authConfig,
|
|
1133
|
-
authStates,
|
|
1134
|
-
setAuthStates
|
|
988
|
+
runtimeClient
|
|
1135
989
|
},
|
|
1136
|
-
children: /* @__PURE__ */ (0,
|
|
990
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(CopilotMessages, { children })
|
|
1137
991
|
}
|
|
1138
992
|
);
|
|
1139
993
|
}
|
|
@@ -1165,40 +1019,13 @@ function entryPointsToFunctionCallHandler(actions) {
|
|
|
1165
1019
|
}
|
|
1166
1020
|
|
|
1167
1021
|
// src/hooks/use-copilot-chat.ts
|
|
1168
|
-
var
|
|
1169
|
-
var import_runtime_client_gql6 = require("@copilotkit/runtime-client-gql");
|
|
1170
|
-
|
|
1171
|
-
// src/hooks/use-chat.ts
|
|
1172
|
-
var import_react11 = require("react");
|
|
1173
|
-
var import_shared6 = require("@copilotkit/shared");
|
|
1022
|
+
var import_react10 = require("react");
|
|
1174
1023
|
var import_runtime_client_gql5 = require("@copilotkit/runtime-client-gql");
|
|
1175
1024
|
|
|
1176
|
-
// src/types/frontend-action.ts
|
|
1177
|
-
var import_runtime_client_gql4 = require("@copilotkit/runtime-client-gql");
|
|
1178
|
-
var import_shared5 = require("@copilotkit/shared");
|
|
1179
|
-
function processActionsForRuntimeRequest(actions) {
|
|
1180
|
-
const filteredActions = actions.filter(
|
|
1181
|
-
(action) => action.available !== import_runtime_client_gql4.ActionInputAvailability.Disabled && action.disabled !== true && action.name !== "*"
|
|
1182
|
-
).map((action) => {
|
|
1183
|
-
let available = import_runtime_client_gql4.ActionInputAvailability.Enabled;
|
|
1184
|
-
if (action.disabled) {
|
|
1185
|
-
available = import_runtime_client_gql4.ActionInputAvailability.Disabled;
|
|
1186
|
-
} else if (action.available === "disabled") {
|
|
1187
|
-
available = import_runtime_client_gql4.ActionInputAvailability.Disabled;
|
|
1188
|
-
} else if (action.available === "remote") {
|
|
1189
|
-
available = import_runtime_client_gql4.ActionInputAvailability.Remote;
|
|
1190
|
-
}
|
|
1191
|
-
return {
|
|
1192
|
-
name: action.name,
|
|
1193
|
-
description: action.description || "",
|
|
1194
|
-
jsonSchema: JSON.stringify((0, import_shared5.actionParametersToJsonSchema)(action.parameters || [])),
|
|
1195
|
-
available
|
|
1196
|
-
};
|
|
1197
|
-
});
|
|
1198
|
-
return filteredActions;
|
|
1199
|
-
}
|
|
1200
|
-
|
|
1201
1025
|
// src/hooks/use-chat.ts
|
|
1026
|
+
var import_react9 = require("react");
|
|
1027
|
+
var import_shared5 = require("@copilotkit/shared");
|
|
1028
|
+
var import_runtime_client_gql4 = require("@copilotkit/runtime-client-gql");
|
|
1202
1029
|
function useChat(options) {
|
|
1203
1030
|
const {
|
|
1204
1031
|
messages,
|
|
@@ -1211,317 +1038,232 @@ function useChat(options) {
|
|
|
1211
1038
|
actions,
|
|
1212
1039
|
onFunctionCall,
|
|
1213
1040
|
onCoAgentStateRender,
|
|
1214
|
-
|
|
1215
|
-
|
|
1041
|
+
setCoagentStates,
|
|
1042
|
+
coagentStates,
|
|
1216
1043
|
agentSession,
|
|
1217
|
-
setAgentSession
|
|
1218
|
-
threadId,
|
|
1219
|
-
setThreadId,
|
|
1220
|
-
runId,
|
|
1221
|
-
setRunId,
|
|
1222
|
-
chatAbortControllerRef,
|
|
1223
|
-
agentLock
|
|
1044
|
+
setAgentSession
|
|
1224
1045
|
} = options;
|
|
1225
|
-
const
|
|
1226
|
-
const
|
|
1046
|
+
const abortControllerRef = (0, import_react9.useRef)();
|
|
1047
|
+
const threadIdRef = (0, import_react9.useRef)(null);
|
|
1048
|
+
const runIdRef = (0, import_react9.useRef)(null);
|
|
1049
|
+
const { addGraphQLErrorsToast } = useToast();
|
|
1050
|
+
const runChatCompletionRef = (0, import_react9.useRef)();
|
|
1051
|
+
const coagentStatesRef = (0, import_react9.useRef)(coagentStates);
|
|
1052
|
+
coagentStatesRef.current = coagentStates;
|
|
1053
|
+
const agentSessionRef = (0, import_react9.useRef)(agentSession);
|
|
1227
1054
|
agentSessionRef.current = agentSession;
|
|
1228
|
-
const threadIdRef = (0, import_react11.useRef)(threadId);
|
|
1229
|
-
threadIdRef.current = threadId;
|
|
1230
|
-
const runIdRef = (0, import_react11.useRef)(runId);
|
|
1231
|
-
runIdRef.current = runId;
|
|
1232
1055
|
const publicApiKey = copilotConfig.publicApiKey;
|
|
1233
|
-
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [
|
|
1056
|
+
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [import_shared5.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
|
|
1234
1057
|
const runtimeClient = useCopilotRuntimeClient({
|
|
1235
1058
|
url: copilotConfig.chatApiEndpoint,
|
|
1236
1059
|
publicApiKey: copilotConfig.publicApiKey,
|
|
1237
1060
|
headers,
|
|
1238
1061
|
credentials: copilotConfig.credentials
|
|
1239
1062
|
});
|
|
1240
|
-
const runChatCompletion =
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1063
|
+
const runChatCompletion = (previousMessages) => __async(this, null, function* () {
|
|
1064
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
1065
|
+
setIsLoading(true);
|
|
1066
|
+
let newMessages = [
|
|
1067
|
+
new import_runtime_client_gql4.TextMessage({
|
|
1068
|
+
content: "",
|
|
1069
|
+
role: import_runtime_client_gql4.Role.Assistant
|
|
1070
|
+
})
|
|
1071
|
+
];
|
|
1072
|
+
const abortController = new AbortController();
|
|
1073
|
+
abortControllerRef.current = abortController;
|
|
1074
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
1075
|
+
const systemMessage = makeSystemMessageCallback();
|
|
1076
|
+
const messagesWithContext = [systemMessage, ...initialMessages || [], ...previousMessages];
|
|
1077
|
+
const stream = runtimeClient.asStream(
|
|
1078
|
+
runtimeClient.generateCopilotResponse({
|
|
1079
|
+
data: __spreadProps(__spreadValues(__spreadProps(__spreadValues({
|
|
1080
|
+
frontend: {
|
|
1081
|
+
actions: actions.filter(
|
|
1082
|
+
(action) => action.available !== import_runtime_client_gql4.ActionInputAvailability.Disabled || !action.disabled
|
|
1083
|
+
).map((action) => {
|
|
1084
|
+
let available = import_runtime_client_gql4.ActionInputAvailability.Enabled;
|
|
1085
|
+
if (action.disabled) {
|
|
1086
|
+
available = import_runtime_client_gql4.ActionInputAvailability.Disabled;
|
|
1087
|
+
} else if (action.available === "disabled") {
|
|
1088
|
+
available = import_runtime_client_gql4.ActionInputAvailability.Disabled;
|
|
1089
|
+
} else if (action.available === "remote") {
|
|
1090
|
+
available = import_runtime_client_gql4.ActionInputAvailability.Remote;
|
|
1091
|
+
}
|
|
1092
|
+
return {
|
|
1093
|
+
name: action.name,
|
|
1094
|
+
description: action.description || "",
|
|
1095
|
+
jsonSchema: JSON.stringify((0, import_shared5.actionParametersToJsonSchema)(action.parameters || [])),
|
|
1096
|
+
available
|
|
1097
|
+
};
|
|
1098
|
+
}),
|
|
1099
|
+
url: window.location.href
|
|
1100
|
+
},
|
|
1101
|
+
threadId: threadIdRef.current,
|
|
1102
|
+
runId: runIdRef.current,
|
|
1103
|
+
messages: (0, import_runtime_client_gql4.convertMessagesToGqlInput)((0, import_runtime_client_gql4.filterAgentStateMessages)(messagesWithContext))
|
|
1104
|
+
}, copilotConfig.cloud ? {
|
|
1105
|
+
cloud: __spreadValues({}, ((_c = (_b = (_a = copilotConfig.cloud.guardrails) == null ? void 0 : _a.input) == null ? void 0 : _b.restrictToTopic) == null ? void 0 : _c.enabled) ? {
|
|
1106
|
+
guardrails: {
|
|
1107
|
+
inputValidationRules: {
|
|
1108
|
+
allowList: copilotConfig.cloud.guardrails.input.restrictToTopic.validTopics,
|
|
1109
|
+
denyList: copilotConfig.cloud.guardrails.input.restrictToTopic.invalidTopics
|
|
1272
1110
|
}
|
|
1273
|
-
} : {})
|
|
1274
|
-
} : {}), {
|
|
1275
|
-
metadata: {
|
|
1276
|
-
requestType: import_runtime_client_gql5.CopilotRequestType.Chat
|
|
1277
|
-
}
|
|
1278
|
-
}), agentSessionRef.current ? {
|
|
1279
|
-
agentSession: agentSessionRef.current
|
|
1280
|
-
} : {}), {
|
|
1281
|
-
agentStates: Object.values(coagentStatesRef.current).map((state) => ({
|
|
1282
|
-
agentName: state.name,
|
|
1283
|
-
state: JSON.stringify(state.state)
|
|
1284
|
-
})),
|
|
1285
|
-
forwardedParameters: options.forwardedParameters || {}
|
|
1286
|
-
}),
|
|
1287
|
-
properties: copilotConfig.properties,
|
|
1288
|
-
signal: (_d = chatAbortControllerRef.current) == null ? void 0 : _d.signal
|
|
1289
|
-
})
|
|
1290
|
-
);
|
|
1291
|
-
const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
|
|
1292
|
-
const reader = stream.getReader();
|
|
1293
|
-
let executedCoAgentStateRenders = [];
|
|
1294
|
-
let followUp = void 0;
|
|
1295
|
-
let messages2 = [];
|
|
1296
|
-
let syncedMessages = [];
|
|
1297
|
-
try {
|
|
1298
|
-
while (true) {
|
|
1299
|
-
let done, value;
|
|
1300
|
-
try {
|
|
1301
|
-
const readResult = yield reader.read();
|
|
1302
|
-
done = readResult.done;
|
|
1303
|
-
value = readResult.value;
|
|
1304
|
-
} catch (readError) {
|
|
1305
|
-
break;
|
|
1306
|
-
}
|
|
1307
|
-
if (done) {
|
|
1308
|
-
if (chatAbortControllerRef.current.signal.aborted) {
|
|
1309
|
-
return [];
|
|
1310
1111
|
}
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
}
|
|
1316
|
-
threadIdRef.current = value.generateCopilotResponse.threadId || null;
|
|
1317
|
-
runIdRef.current = value.generateCopilotResponse.runId || null;
|
|
1318
|
-
setThreadId(threadIdRef.current);
|
|
1319
|
-
setRunId(runIdRef.current);
|
|
1320
|
-
messages2 = (0, import_runtime_client_gql5.convertGqlOutputToMessages)(
|
|
1321
|
-
(0, import_runtime_client_gql5.filterAdjacentAgentStateMessages)(value.generateCopilotResponse.messages)
|
|
1322
|
-
);
|
|
1323
|
-
if (messages2.length === 0) {
|
|
1324
|
-
continue;
|
|
1112
|
+
} : {})
|
|
1113
|
+
} : {}), {
|
|
1114
|
+
metadata: {
|
|
1115
|
+
requestType: import_runtime_client_gql4.CopilotRequestType.Chat
|
|
1325
1116
|
}
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1117
|
+
}), agentSessionRef.current ? {
|
|
1118
|
+
agentSession: agentSessionRef.current
|
|
1119
|
+
} : {}), {
|
|
1120
|
+
agentStates: Object.values(coagentStatesRef.current).map((state) => ({
|
|
1121
|
+
agentName: state.name,
|
|
1122
|
+
state: JSON.stringify(state.state)
|
|
1123
|
+
}))
|
|
1124
|
+
}),
|
|
1125
|
+
properties: copilotConfig.properties,
|
|
1126
|
+
signal: (_d = abortControllerRef.current) == null ? void 0 : _d.signal
|
|
1127
|
+
})
|
|
1128
|
+
);
|
|
1129
|
+
const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
|
|
1130
|
+
const reader = stream.getReader();
|
|
1131
|
+
let actionResults = {};
|
|
1132
|
+
let executedCoAgentStateRenders = [];
|
|
1133
|
+
let followUp = void 0;
|
|
1134
|
+
try {
|
|
1135
|
+
while (true) {
|
|
1136
|
+
let done, value;
|
|
1137
|
+
try {
|
|
1138
|
+
const readResult = yield reader.read();
|
|
1139
|
+
done = readResult.done;
|
|
1140
|
+
value = readResult.value;
|
|
1141
|
+
} catch (readError) {
|
|
1142
|
+
break;
|
|
1143
|
+
}
|
|
1144
|
+
if (done) {
|
|
1145
|
+
break;
|
|
1146
|
+
}
|
|
1147
|
+
if (!(value == null ? void 0 : value.generateCopilotResponse)) {
|
|
1148
|
+
continue;
|
|
1149
|
+
}
|
|
1150
|
+
threadIdRef.current = value.generateCopilotResponse.threadId || null;
|
|
1151
|
+
runIdRef.current = value.generateCopilotResponse.runId || null;
|
|
1152
|
+
const messages2 = (0, import_runtime_client_gql4.convertGqlOutputToMessages)(
|
|
1153
|
+
(0, import_runtime_client_gql4.filterAdjacentAgentStateMessages)(value.generateCopilotResponse.messages)
|
|
1154
|
+
);
|
|
1155
|
+
if (messages2.length === 0) {
|
|
1156
|
+
continue;
|
|
1157
|
+
}
|
|
1158
|
+
newMessages = [];
|
|
1159
|
+
if (((_h = value.generateCopilotResponse.status) == null ? void 0 : _h.__typename) === "FailedResponseStatus" && value.generateCopilotResponse.status.reason === "GUARDRAILS_VALIDATION_FAILED") {
|
|
1160
|
+
newMessages = [
|
|
1161
|
+
new import_runtime_client_gql4.TextMessage({
|
|
1162
|
+
role: import_runtime_client_gql4.MessageRole.Assistant,
|
|
1163
|
+
content: ((_i = value.generateCopilotResponse.status.details) == null ? void 0 : _i.guardrailsReason) || ""
|
|
1164
|
+
})
|
|
1165
|
+
];
|
|
1166
|
+
} else {
|
|
1167
|
+
for (const message of messages2) {
|
|
1168
|
+
newMessages.push(message);
|
|
1169
|
+
if (message.isActionExecutionMessage() && message.status.code !== import_runtime_client_gql4.MessageStatusCode.Pending && message.scope === "client" && onFunctionCall) {
|
|
1170
|
+
if (!(message.id in actionResults)) {
|
|
1340
1171
|
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
1341
1172
|
break;
|
|
1342
1173
|
}
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
}
|
|
1351
|
-
const lastAgentStateMessage = [...messages2].reverse().find((message) => message.isAgentStateMessage());
|
|
1352
|
-
if (lastAgentStateMessage) {
|
|
1353
|
-
if (lastAgentStateMessage.state.messages && lastAgentStateMessage.state.messages.length > 0) {
|
|
1354
|
-
syncedMessages = (0, import_runtime_client_gql5.loadMessagesFromJsonRepresentation)(
|
|
1355
|
-
lastAgentStateMessage.state.messages
|
|
1356
|
-
);
|
|
1357
|
-
}
|
|
1358
|
-
setCoagentStatesWithRef((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
1359
|
-
[lastAgentStateMessage.agentName]: {
|
|
1360
|
-
name: lastAgentStateMessage.agentName,
|
|
1361
|
-
state: lastAgentStateMessage.state,
|
|
1362
|
-
running: lastAgentStateMessage.running,
|
|
1363
|
-
active: lastAgentStateMessage.active,
|
|
1364
|
-
threadId: lastAgentStateMessage.threadId,
|
|
1365
|
-
nodeName: lastAgentStateMessage.nodeName,
|
|
1366
|
-
runId: lastAgentStateMessage.runId
|
|
1367
|
-
}
|
|
1368
|
-
}));
|
|
1369
|
-
if (lastAgentStateMessage.running) {
|
|
1370
|
-
setAgentSession({
|
|
1371
|
-
threadId: lastAgentStateMessage.threadId,
|
|
1372
|
-
agentName: lastAgentStateMessage.agentName,
|
|
1373
|
-
nodeName: lastAgentStateMessage.nodeName
|
|
1374
|
-
});
|
|
1375
|
-
} else {
|
|
1376
|
-
if (agentLock) {
|
|
1377
|
-
setAgentSession({
|
|
1378
|
-
threadId: (0, import_shared6.randomId)(),
|
|
1379
|
-
agentName: agentLock,
|
|
1380
|
-
nodeName: void 0
|
|
1381
|
-
});
|
|
1382
|
-
} else {
|
|
1383
|
-
setAgentSession(null);
|
|
1384
|
-
}
|
|
1385
|
-
}
|
|
1386
|
-
}
|
|
1387
|
-
}
|
|
1388
|
-
if (newMessages.length > 0) {
|
|
1389
|
-
setMessages([...previousMessages, ...newMessages]);
|
|
1390
|
-
}
|
|
1391
|
-
}
|
|
1392
|
-
const finalMessages = constructFinalMessages(syncedMessages, previousMessages, newMessages);
|
|
1393
|
-
let didExecuteAction = false;
|
|
1394
|
-
if (onFunctionCall) {
|
|
1395
|
-
const lastMessages = [];
|
|
1396
|
-
for (let i = finalMessages.length - 1; i >= 0; i--) {
|
|
1397
|
-
const message = finalMessages[i];
|
|
1398
|
-
if (message.isActionExecutionMessage() && message.status.code !== import_runtime_client_gql5.MessageStatusCode.Pending) {
|
|
1399
|
-
lastMessages.unshift(message);
|
|
1400
|
-
} else {
|
|
1401
|
-
break;
|
|
1402
|
-
}
|
|
1403
|
-
}
|
|
1404
|
-
for (const message of lastMessages) {
|
|
1405
|
-
setMessages(finalMessages);
|
|
1406
|
-
const action = actions.find((action2) => action2.name === message.name);
|
|
1407
|
-
if (action) {
|
|
1408
|
-
followUp = action.followUp;
|
|
1409
|
-
let result;
|
|
1410
|
-
try {
|
|
1411
|
-
result = yield Promise.race([
|
|
1412
|
-
onFunctionCall({
|
|
1174
|
+
try {
|
|
1175
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
1176
|
+
const action = actions.find((action2) => action2.name === message.name);
|
|
1177
|
+
if (action) {
|
|
1178
|
+
followUp = action.followUp;
|
|
1179
|
+
}
|
|
1180
|
+
const result = yield onFunctionCall({
|
|
1413
1181
|
messages: previousMessages,
|
|
1414
1182
|
name: message.name,
|
|
1415
1183
|
args: message.arguments
|
|
1416
|
-
})
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
() => resolve("Operation was aborted by the user")
|
|
1423
|
-
);
|
|
1424
|
-
}
|
|
1425
|
-
),
|
|
1426
|
-
// if the user stopped generation, we also abort consecutive actions
|
|
1427
|
-
new Promise((resolve) => {
|
|
1428
|
-
var _a2;
|
|
1429
|
-
if ((_a2 = chatAbortControllerRef.current) == null ? void 0 : _a2.signal.aborted) {
|
|
1430
|
-
resolve("Operation was aborted by the user");
|
|
1431
|
-
}
|
|
1432
|
-
})
|
|
1433
|
-
]);
|
|
1434
|
-
} catch (e) {
|
|
1435
|
-
result = `Failed to execute action ${message.name}`;
|
|
1436
|
-
console.error(`Failed to execute action ${message.name}: ${e}`);
|
|
1184
|
+
});
|
|
1185
|
+
actionResults[message.id] = result;
|
|
1186
|
+
} catch (e) {
|
|
1187
|
+
actionResults[message.id] = `Failed to execute action ${message.name}`;
|
|
1188
|
+
console.error(`Failed to execute action ${message.name}: ${e}`);
|
|
1189
|
+
}
|
|
1437
1190
|
}
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
messageIndex + 1,
|
|
1442
|
-
0,
|
|
1443
|
-
new import_runtime_client_gql5.ResultMessage({
|
|
1444
|
-
id: "result-" + message.id,
|
|
1445
|
-
result: import_runtime_client_gql5.ResultMessage.encodeResult(result),
|
|
1191
|
+
newMessages.push(
|
|
1192
|
+
new import_runtime_client_gql4.ResultMessage({
|
|
1193
|
+
result: import_runtime_client_gql4.ResultMessage.encodeResult(actionResults[message.id]),
|
|
1446
1194
|
actionExecutionId: message.id,
|
|
1447
1195
|
actionName: message.name
|
|
1448
1196
|
})
|
|
1449
1197
|
);
|
|
1450
1198
|
}
|
|
1199
|
+
if (message.isAgentStateMessage() && !message.active && !executedCoAgentStateRenders.includes(message.id) && onCoAgentStateRender) {
|
|
1200
|
+
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
1201
|
+
break;
|
|
1202
|
+
}
|
|
1203
|
+
yield onCoAgentStateRender({
|
|
1204
|
+
name: message.agentName,
|
|
1205
|
+
nodeName: message.nodeName,
|
|
1206
|
+
state: message.state
|
|
1207
|
+
});
|
|
1208
|
+
executedCoAgentStateRenders.push(message.id);
|
|
1209
|
+
}
|
|
1451
1210
|
}
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
if (
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1211
|
+
const lastAgentStateMessage = [...messages2].reverse().find((message) => message.isAgentStateMessage());
|
|
1212
|
+
if (lastAgentStateMessage) {
|
|
1213
|
+
setCoagentStates((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
1214
|
+
[lastAgentStateMessage.agentName]: {
|
|
1215
|
+
name: lastAgentStateMessage.agentName,
|
|
1216
|
+
state: lastAgentStateMessage.state,
|
|
1217
|
+
running: lastAgentStateMessage.running,
|
|
1218
|
+
active: lastAgentStateMessage.active,
|
|
1219
|
+
threadId: lastAgentStateMessage.threadId,
|
|
1220
|
+
nodeName: lastAgentStateMessage.nodeName,
|
|
1221
|
+
runId: lastAgentStateMessage.runId
|
|
1222
|
+
}
|
|
1223
|
+
}));
|
|
1224
|
+
if (lastAgentStateMessage.running) {
|
|
1225
|
+
setAgentSession({
|
|
1226
|
+
threadId: lastAgentStateMessage.threadId,
|
|
1227
|
+
agentName: lastAgentStateMessage.agentName,
|
|
1228
|
+
nodeName: lastAgentStateMessage.nodeName
|
|
1229
|
+
});
|
|
1230
|
+
} else {
|
|
1231
|
+
setAgentSession(null);
|
|
1469
1232
|
}
|
|
1470
|
-
return true;
|
|
1471
|
-
});
|
|
1472
|
-
const repairedMessageIds = repairedMessages.map((message) => message.id);
|
|
1473
|
-
setMessages(repairedMessages);
|
|
1474
|
-
if ((_l = agentSessionRef.current) == null ? void 0 : _l.nodeName) {
|
|
1475
|
-
setAgentSession({
|
|
1476
|
-
threadId: agentSessionRef.current.threadId,
|
|
1477
|
-
agentName: agentSessionRef.current.agentName,
|
|
1478
|
-
nodeName: "__end__"
|
|
1479
|
-
});
|
|
1480
1233
|
}
|
|
1481
|
-
return newMessages.filter((message) => repairedMessageIds.includes(message.id));
|
|
1482
|
-
} else {
|
|
1483
|
-
return newMessages.slice();
|
|
1484
1234
|
}
|
|
1485
|
-
|
|
1486
|
-
|
|
1235
|
+
if (newMessages.length > 0) {
|
|
1236
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
1237
|
+
}
|
|
1487
1238
|
}
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
onFunctionCall,
|
|
1499
|
-
onCoAgentStateRender,
|
|
1500
|
-
setCoagentStatesWithRef,
|
|
1501
|
-
coagentStatesRef,
|
|
1502
|
-
agentSession,
|
|
1503
|
-
setAgentSession
|
|
1504
|
-
]
|
|
1505
|
-
);
|
|
1506
|
-
runChatCompletionRef.current = runChatCompletion;
|
|
1507
|
-
const runChatCompletionAndHandleFunctionCall = useAsyncCallback(
|
|
1508
|
-
(messages2) => __async(this, null, function* () {
|
|
1509
|
-
yield runChatCompletionRef.current(messages2);
|
|
1510
|
-
}),
|
|
1511
|
-
[messages]
|
|
1512
|
-
);
|
|
1513
|
-
const append = useAsyncCallback(
|
|
1514
|
-
(message) => __async(this, null, function* () {
|
|
1515
|
-
if (isLoading) {
|
|
1516
|
-
return;
|
|
1239
|
+
if (
|
|
1240
|
+
// if followUp is not explicitly false
|
|
1241
|
+
followUp !== false && // if we have client side results
|
|
1242
|
+
(Object.values(actionResults).length || // or the last message we received is a result
|
|
1243
|
+
newMessages.length && newMessages[newMessages.length - 1].isResultMessage())
|
|
1244
|
+
) {
|
|
1245
|
+
yield new Promise((resolve) => setTimeout(resolve, 10));
|
|
1246
|
+
return yield runChatCompletionRef.current([...previousMessages, ...newMessages]);
|
|
1247
|
+
} else {
|
|
1248
|
+
return newMessages.slice();
|
|
1517
1249
|
}
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
)
|
|
1524
|
-
|
|
1250
|
+
} finally {
|
|
1251
|
+
setIsLoading(false);
|
|
1252
|
+
}
|
|
1253
|
+
});
|
|
1254
|
+
runChatCompletionRef.current = runChatCompletion;
|
|
1255
|
+
const runChatCompletionAndHandleFunctionCall = (messages2) => __async(this, null, function* () {
|
|
1256
|
+
yield runChatCompletionRef.current(messages2);
|
|
1257
|
+
});
|
|
1258
|
+
const append = (message) => __async(this, null, function* () {
|
|
1259
|
+
if (isLoading) {
|
|
1260
|
+
return;
|
|
1261
|
+
}
|
|
1262
|
+
const newMessages = [...messages, message];
|
|
1263
|
+
setMessages(newMessages);
|
|
1264
|
+
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
1265
|
+
});
|
|
1266
|
+
const reload = () => __async(this, null, function* () {
|
|
1525
1267
|
if (isLoading || messages.length === 0) {
|
|
1526
1268
|
return;
|
|
1527
1269
|
}
|
|
@@ -1532,35 +1274,17 @@ function useChat(options) {
|
|
|
1532
1274
|
}
|
|
1533
1275
|
setMessages(newMessages);
|
|
1534
1276
|
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
1535
|
-
})
|
|
1277
|
+
});
|
|
1536
1278
|
const stop = () => {
|
|
1537
1279
|
var _a;
|
|
1538
|
-
(_a =
|
|
1280
|
+
(_a = abortControllerRef.current) == null ? void 0 : _a.abort();
|
|
1539
1281
|
};
|
|
1540
1282
|
return {
|
|
1541
1283
|
append,
|
|
1542
1284
|
reload,
|
|
1543
|
-
stop
|
|
1544
|
-
runChatCompletion: () => runChatCompletionRef.current(messages)
|
|
1285
|
+
stop
|
|
1545
1286
|
};
|
|
1546
1287
|
}
|
|
1547
|
-
function constructFinalMessages(syncedMessages, previousMessages, newMessages) {
|
|
1548
|
-
const finalMessages = syncedMessages.length > 0 ? [...syncedMessages] : [...previousMessages, ...newMessages];
|
|
1549
|
-
if (syncedMessages.length > 0) {
|
|
1550
|
-
const messagesWithAgentState = [...previousMessages, ...newMessages];
|
|
1551
|
-
let previousMessageId = void 0;
|
|
1552
|
-
for (const message of messagesWithAgentState) {
|
|
1553
|
-
if (message.isAgentStateMessage()) {
|
|
1554
|
-
const index = finalMessages.findIndex((msg) => msg.id === previousMessageId);
|
|
1555
|
-
if (index !== -1) {
|
|
1556
|
-
finalMessages.splice(index + 1, 0, message);
|
|
1557
|
-
}
|
|
1558
|
-
}
|
|
1559
|
-
previousMessageId = message.id;
|
|
1560
|
-
}
|
|
1561
|
-
}
|
|
1562
|
-
return finalMessages;
|
|
1563
|
-
}
|
|
1564
1288
|
|
|
1565
1289
|
// src/hooks/use-copilot-chat.ts
|
|
1566
1290
|
function useCopilotChat(_a = {}) {
|
|
@@ -1577,36 +1301,29 @@ function useCopilotChat(_a = {}) {
|
|
|
1577
1301
|
setIsLoading,
|
|
1578
1302
|
chatInstructions,
|
|
1579
1303
|
actions,
|
|
1580
|
-
|
|
1581
|
-
|
|
1304
|
+
coagentStates,
|
|
1305
|
+
setCoagentStates,
|
|
1582
1306
|
coAgentStateRenders,
|
|
1583
1307
|
agentSession,
|
|
1584
|
-
setAgentSession
|
|
1585
|
-
forwardedParameters,
|
|
1586
|
-
agentLock,
|
|
1587
|
-
threadId,
|
|
1588
|
-
setThreadId,
|
|
1589
|
-
runId,
|
|
1590
|
-
setRunId,
|
|
1591
|
-
chatAbortControllerRef
|
|
1308
|
+
setAgentSession
|
|
1592
1309
|
} = useCopilotContext();
|
|
1593
1310
|
const { messages, setMessages } = useCopilotMessagesContext();
|
|
1594
1311
|
const latestGetContextString = useUpdatedRef(getContextString);
|
|
1595
|
-
const deleteMessage = (0,
|
|
1312
|
+
const deleteMessage = (0, import_react10.useCallback)(
|
|
1596
1313
|
(messageId) => {
|
|
1597
1314
|
setMessages((prev) => prev.filter((message) => message.id !== messageId));
|
|
1598
1315
|
},
|
|
1599
1316
|
[setMessages]
|
|
1600
1317
|
);
|
|
1601
|
-
const makeSystemMessageCallback = (0,
|
|
1318
|
+
const makeSystemMessageCallback = (0, import_react10.useCallback)(() => {
|
|
1602
1319
|
const systemMessageMaker = makeSystemMessage2 || defaultSystemMessage;
|
|
1603
1320
|
const contextString = latestGetContextString.current([], defaultCopilotContextCategories);
|
|
1604
|
-
return new
|
|
1321
|
+
return new import_runtime_client_gql5.TextMessage({
|
|
1605
1322
|
content: systemMessageMaker(contextString, chatInstructions),
|
|
1606
|
-
role:
|
|
1323
|
+
role: import_runtime_client_gql5.Role.System
|
|
1607
1324
|
});
|
|
1608
1325
|
}, [getContextString, makeSystemMessage2, chatInstructions]);
|
|
1609
|
-
const onCoAgentStateRender =
|
|
1326
|
+
const onCoAgentStateRender = (0, import_react10.useCallback)(
|
|
1610
1327
|
(args) => __async(this, null, function* () {
|
|
1611
1328
|
var _a2;
|
|
1612
1329
|
const { name, nodeName, state } = args;
|
|
@@ -1624,7 +1341,7 @@ function useCopilotChat(_a = {}) {
|
|
|
1624
1341
|
}),
|
|
1625
1342
|
[coAgentStateRenders]
|
|
1626
1343
|
);
|
|
1627
|
-
const { append, reload, stop
|
|
1344
|
+
const { append, reload, stop } = useChat(__spreadProps(__spreadValues({}, options), {
|
|
1628
1345
|
actions: Object.values(actions),
|
|
1629
1346
|
copilotConfig: copilotApiConfig,
|
|
1630
1347
|
initialMessages: options.initialMessages || [],
|
|
@@ -1635,91 +1352,53 @@ function useCopilotChat(_a = {}) {
|
|
|
1635
1352
|
makeSystemMessageCallback,
|
|
1636
1353
|
isLoading,
|
|
1637
1354
|
setIsLoading,
|
|
1638
|
-
|
|
1639
|
-
|
|
1355
|
+
coagentStates,
|
|
1356
|
+
setCoagentStates,
|
|
1640
1357
|
agentSession,
|
|
1641
|
-
setAgentSession
|
|
1642
|
-
forwardedParameters,
|
|
1643
|
-
threadId,
|
|
1644
|
-
setThreadId,
|
|
1645
|
-
runId,
|
|
1646
|
-
setRunId,
|
|
1647
|
-
chatAbortControllerRef,
|
|
1648
|
-
agentLock
|
|
1358
|
+
setAgentSession
|
|
1649
1359
|
}));
|
|
1650
1360
|
const latestAppend = useUpdatedRef(append);
|
|
1651
|
-
const latestAppendFunc =
|
|
1652
|
-
(message) =>
|
|
1653
|
-
return
|
|
1654
|
-
}
|
|
1361
|
+
const latestAppendFunc = (0, import_react10.useCallback)(
|
|
1362
|
+
(message) => {
|
|
1363
|
+
return latestAppend.current(message);
|
|
1364
|
+
},
|
|
1655
1365
|
[latestAppend]
|
|
1656
1366
|
);
|
|
1657
1367
|
const latestReload = useUpdatedRef(reload);
|
|
1658
|
-
const latestReloadFunc =
|
|
1659
|
-
return
|
|
1660
|
-
}
|
|
1368
|
+
const latestReloadFunc = (0, import_react10.useCallback)(() => {
|
|
1369
|
+
return latestReload.current();
|
|
1370
|
+
}, [latestReload]);
|
|
1661
1371
|
const latestStop = useUpdatedRef(stop);
|
|
1662
|
-
const latestStopFunc = (0,
|
|
1372
|
+
const latestStopFunc = (0, import_react10.useCallback)(() => {
|
|
1663
1373
|
return latestStop.current();
|
|
1664
1374
|
}, [latestStop]);
|
|
1665
1375
|
const latestDelete = useUpdatedRef(deleteMessage);
|
|
1666
|
-
const latestDeleteFunc = (0,
|
|
1376
|
+
const latestDeleteFunc = (0, import_react10.useCallback)(
|
|
1667
1377
|
(messageId) => {
|
|
1668
1378
|
return latestDelete.current(messageId);
|
|
1669
1379
|
},
|
|
1670
1380
|
[latestDelete]
|
|
1671
1381
|
);
|
|
1672
1382
|
const latestSetMessages = useUpdatedRef(setMessages);
|
|
1673
|
-
const latestSetMessagesFunc = (0,
|
|
1383
|
+
const latestSetMessagesFunc = (0, import_react10.useCallback)(
|
|
1674
1384
|
(messages2) => {
|
|
1675
1385
|
return latestSetMessages.current(messages2);
|
|
1676
1386
|
},
|
|
1677
1387
|
[latestSetMessages]
|
|
1678
1388
|
);
|
|
1679
|
-
const latestRunChatCompletion = useUpdatedRef(runChatCompletion);
|
|
1680
|
-
const latestRunChatCompletionFunc = useAsyncCallback(() => __async(this, null, function* () {
|
|
1681
|
-
return yield latestRunChatCompletion.current();
|
|
1682
|
-
}), [latestRunChatCompletion]);
|
|
1683
|
-
const reset = (0, import_react12.useCallback)(() => {
|
|
1684
|
-
latestStopFunc();
|
|
1685
|
-
setMessages([]);
|
|
1686
|
-
setThreadId(null);
|
|
1687
|
-
setRunId(null);
|
|
1688
|
-
setCoagentStatesWithRef({});
|
|
1689
|
-
let initialAgentSession = null;
|
|
1690
|
-
if (agentLock) {
|
|
1691
|
-
initialAgentSession = {
|
|
1692
|
-
agentName: agentLock
|
|
1693
|
-
};
|
|
1694
|
-
}
|
|
1695
|
-
setAgentSession(initialAgentSession);
|
|
1696
|
-
}, [
|
|
1697
|
-
latestStopFunc,
|
|
1698
|
-
setMessages,
|
|
1699
|
-
setThreadId,
|
|
1700
|
-
setCoagentStatesWithRef,
|
|
1701
|
-
setAgentSession,
|
|
1702
|
-
agentLock
|
|
1703
|
-
]);
|
|
1704
|
-
const latestReset = useUpdatedRef(reset);
|
|
1705
|
-
const latestResetFunc = (0, import_react12.useCallback)(() => {
|
|
1706
|
-
return latestReset.current();
|
|
1707
|
-
}, [latestReset]);
|
|
1708
1389
|
return {
|
|
1709
1390
|
visibleMessages: messages,
|
|
1710
1391
|
appendMessage: latestAppendFunc,
|
|
1711
1392
|
setMessages: latestSetMessagesFunc,
|
|
1712
1393
|
reloadMessages: latestReloadFunc,
|
|
1713
1394
|
stopGeneration: latestStopFunc,
|
|
1714
|
-
reset: latestResetFunc,
|
|
1715
1395
|
deleteMessage: latestDeleteFunc,
|
|
1716
|
-
runChatCompletion: latestRunChatCompletionFunc,
|
|
1717
1396
|
isLoading
|
|
1718
1397
|
};
|
|
1719
1398
|
}
|
|
1720
1399
|
function useUpdatedRef(value) {
|
|
1721
|
-
const ref = (0,
|
|
1722
|
-
(0,
|
|
1400
|
+
const ref = (0, import_react10.useRef)(value);
|
|
1401
|
+
(0, import_react10.useEffect)(() => {
|
|
1723
1402
|
ref.current = value;
|
|
1724
1403
|
}, [value]);
|
|
1725
1404
|
return ref;
|
|
@@ -1749,22 +1428,18 @@ ${additionalInstructions}` : "");
|
|
|
1749
1428
|
}
|
|
1750
1429
|
|
|
1751
1430
|
// src/hooks/use-copilot-action.ts
|
|
1752
|
-
var
|
|
1753
|
-
var
|
|
1431
|
+
var import_shared6 = require("@copilotkit/shared");
|
|
1432
|
+
var import_react11 = require("react");
|
|
1754
1433
|
function useCopilotAction(action, dependencies) {
|
|
1755
1434
|
const { setAction, removeAction, actions, chatComponentsCache } = useCopilotContext();
|
|
1756
|
-
const idRef = (0,
|
|
1757
|
-
const renderAndWaitRef = (0,
|
|
1435
|
+
const idRef = (0, import_react11.useRef)((0, import_shared6.randomId)());
|
|
1436
|
+
const renderAndWaitRef = (0, import_react11.useRef)(null);
|
|
1758
1437
|
action = __spreadValues({}, action);
|
|
1759
|
-
if (
|
|
1760
|
-
// renderAndWaitForResponse is not available for catch all actions
|
|
1761
|
-
isFrontendAction(action) && // check if renderAndWaitForResponse is set
|
|
1762
|
-
(action.renderAndWait || action.renderAndWaitForResponse)
|
|
1763
|
-
) {
|
|
1438
|
+
if (action.renderAndWait || action.renderAndWaitForResponse) {
|
|
1764
1439
|
const renderAndWait = action.renderAndWait || action.renderAndWaitForResponse;
|
|
1765
1440
|
action.renderAndWait = void 0;
|
|
1766
1441
|
action.renderAndWaitForResponse = void 0;
|
|
1767
|
-
action.handler =
|
|
1442
|
+
action.handler = () => __async(this, null, function* () {
|
|
1768
1443
|
let resolve;
|
|
1769
1444
|
let reject;
|
|
1770
1445
|
const promise = new Promise((resolvePromise, rejectPromise) => {
|
|
@@ -1773,18 +1448,14 @@ function useCopilotAction(action, dependencies) {
|
|
|
1773
1448
|
});
|
|
1774
1449
|
renderAndWaitRef.current = { promise, resolve, reject };
|
|
1775
1450
|
return yield promise;
|
|
1776
|
-
})
|
|
1451
|
+
});
|
|
1777
1452
|
action.render = (props) => {
|
|
1778
|
-
let status = props.status;
|
|
1779
|
-
if (props.status === "executing" && !renderAndWaitRef.current) {
|
|
1780
|
-
status = "inProgress";
|
|
1781
|
-
}
|
|
1782
1453
|
const waitProps = {
|
|
1783
|
-
status,
|
|
1454
|
+
status: props.status,
|
|
1784
1455
|
args: props.args,
|
|
1785
1456
|
result: props.result,
|
|
1786
|
-
handler: status === "executing" ? renderAndWaitRef.current.resolve : void 0,
|
|
1787
|
-
respond: status === "executing" ? renderAndWaitRef.current.resolve : void 0
|
|
1457
|
+
handler: props.status === "executing" ? renderAndWaitRef.current.resolve : void 0,
|
|
1458
|
+
respond: props.status === "executing" ? renderAndWaitRef.current.resolve : void 0
|
|
1788
1459
|
};
|
|
1789
1460
|
const isNoArgsRenderWait = (_fn) => {
|
|
1790
1461
|
var _a;
|
|
@@ -1797,14 +1468,12 @@ function useCopilotAction(action, dependencies) {
|
|
|
1797
1468
|
return renderAndWait(waitProps);
|
|
1798
1469
|
}
|
|
1799
1470
|
}
|
|
1800
|
-
return (0,
|
|
1471
|
+
return (0, import_react11.createElement)(import_react11.Fragment);
|
|
1801
1472
|
};
|
|
1802
1473
|
}
|
|
1803
1474
|
if (dependencies === void 0) {
|
|
1804
1475
|
if (actions[idRef.current]) {
|
|
1805
|
-
|
|
1806
|
-
actions[idRef.current].handler = action.handler;
|
|
1807
|
-
}
|
|
1476
|
+
actions[idRef.current].handler = action.handler;
|
|
1808
1477
|
if (typeof action.render === "function") {
|
|
1809
1478
|
if (chatComponentsCache.current !== null) {
|
|
1810
1479
|
chatComponentsCache.current.actions[action.name] = action.render;
|
|
@@ -1812,7 +1481,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
1812
1481
|
}
|
|
1813
1482
|
}
|
|
1814
1483
|
}
|
|
1815
|
-
(0,
|
|
1484
|
+
(0, import_react11.useEffect)(() => {
|
|
1816
1485
|
setAction(idRef.current, action);
|
|
1817
1486
|
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
1818
1487
|
chatComponentsCache.current.actions[action.name] = action.render;
|
|
@@ -1823,43 +1492,31 @@ function useCopilotAction(action, dependencies) {
|
|
|
1823
1492
|
}, [
|
|
1824
1493
|
setAction,
|
|
1825
1494
|
removeAction,
|
|
1826
|
-
|
|
1495
|
+
action.description,
|
|
1827
1496
|
action.name,
|
|
1828
|
-
|
|
1829
|
-
|
|
1497
|
+
action.disabled,
|
|
1498
|
+
action.available,
|
|
1830
1499
|
// This should be faster than deep equality checking
|
|
1831
1500
|
// In addition, all major JS engines guarantee the order of object keys
|
|
1832
|
-
JSON.stringify(
|
|
1501
|
+
JSON.stringify(action.parameters),
|
|
1833
1502
|
// include render only if it's a string
|
|
1834
1503
|
typeof action.render === "string" ? action.render : void 0,
|
|
1835
1504
|
// dependencies set by the developer
|
|
1836
1505
|
...dependencies || []
|
|
1837
1506
|
]);
|
|
1838
1507
|
}
|
|
1839
|
-
function isFrontendAction(action) {
|
|
1840
|
-
return action.name !== "*";
|
|
1841
|
-
}
|
|
1842
1508
|
|
|
1843
1509
|
// src/hooks/use-coagent-state-render.ts
|
|
1844
|
-
var
|
|
1845
|
-
var
|
|
1510
|
+
var import_react12 = require("react");
|
|
1511
|
+
var import_shared7 = require("@copilotkit/shared");
|
|
1846
1512
|
function useCoAgentStateRender(action, dependencies) {
|
|
1847
1513
|
const {
|
|
1848
1514
|
setCoAgentStateRender,
|
|
1849
1515
|
removeCoAgentStateRender,
|
|
1850
1516
|
coAgentStateRenders,
|
|
1851
|
-
chatComponentsCache
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
const idRef = (0, import_react14.useRef)((0, import_shared8.randomId)());
|
|
1855
|
-
const { addToast } = useToast();
|
|
1856
|
-
(0, import_react14.useEffect)(() => {
|
|
1857
|
-
if ((availableAgents == null ? void 0 : availableAgents.length) && !availableAgents.some((a) => a.name === action.name)) {
|
|
1858
|
-
const message = `(useCoAgentStateRender): Agent "${action.name}" not found. Make sure the agent exists and is properly configured.`;
|
|
1859
|
-
console.warn(message);
|
|
1860
|
-
addToast({ type: "warning", message });
|
|
1861
|
-
}
|
|
1862
|
-
}, [availableAgents]);
|
|
1517
|
+
chatComponentsCache
|
|
1518
|
+
} = (0, import_react12.useContext)(CopilotContext);
|
|
1519
|
+
const idRef = (0, import_react12.useRef)((0, import_shared7.randomId)());
|
|
1863
1520
|
const key = `${action.name}-${action.nodeName || "global"}`;
|
|
1864
1521
|
if (dependencies === void 0) {
|
|
1865
1522
|
if (coAgentStateRenders[idRef.current]) {
|
|
@@ -1871,7 +1528,7 @@ function useCoAgentStateRender(action, dependencies) {
|
|
|
1871
1528
|
}
|
|
1872
1529
|
}
|
|
1873
1530
|
}
|
|
1874
|
-
(0,
|
|
1531
|
+
(0, import_react12.useEffect)(() => {
|
|
1875
1532
|
setCoAgentStateRender(idRef.current, action);
|
|
1876
1533
|
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
1877
1534
|
chatComponentsCache.current.coAgentStateRenders[key] = action.render;
|
|
@@ -1891,11 +1548,11 @@ function useCoAgentStateRender(action, dependencies) {
|
|
|
1891
1548
|
}
|
|
1892
1549
|
|
|
1893
1550
|
// src/hooks/use-make-copilot-document-readable.ts
|
|
1894
|
-
var
|
|
1551
|
+
var import_react13 = require("react");
|
|
1895
1552
|
function useMakeCopilotDocumentReadable(document, categories, dependencies = []) {
|
|
1896
1553
|
const { addDocumentContext, removeDocumentContext } = useCopilotContext();
|
|
1897
|
-
const idRef = (0,
|
|
1898
|
-
(0,
|
|
1554
|
+
const idRef = (0, import_react13.useRef)();
|
|
1555
|
+
(0, import_react13.useEffect)(() => {
|
|
1899
1556
|
const id = addDocumentContext(document, categories);
|
|
1900
1557
|
idRef.current = id;
|
|
1901
1558
|
return () => {
|
|
@@ -1906,16 +1563,16 @@ function useMakeCopilotDocumentReadable(document, categories, dependencies = [])
|
|
|
1906
1563
|
}
|
|
1907
1564
|
|
|
1908
1565
|
// src/hooks/use-copilot-readable.ts
|
|
1909
|
-
var
|
|
1566
|
+
var import_react14 = require("react");
|
|
1910
1567
|
function convertToJSON(description, value) {
|
|
1911
1568
|
return `${description}: ${typeof value === "string" ? value : JSON.stringify(value)}`;
|
|
1912
1569
|
}
|
|
1913
1570
|
function useCopilotReadable({ description, value, parentId, categories, convert }, dependencies) {
|
|
1914
1571
|
const { addContext, removeContext } = useCopilotContext();
|
|
1915
|
-
const idRef = (0,
|
|
1572
|
+
const idRef = (0, import_react14.useRef)();
|
|
1916
1573
|
convert = convert || convertToJSON;
|
|
1917
1574
|
const information = convert(description, value);
|
|
1918
|
-
(0,
|
|
1575
|
+
(0, import_react14.useEffect)(() => {
|
|
1919
1576
|
const id = addContext(information, parentId, categories);
|
|
1920
1577
|
idRef.current = id;
|
|
1921
1578
|
return () => {
|
|
@@ -1926,29 +1583,20 @@ function useCopilotReadable({ description, value, parentId, categories, convert
|
|
|
1926
1583
|
}
|
|
1927
1584
|
|
|
1928
1585
|
// src/hooks/use-coagent.ts
|
|
1929
|
-
var
|
|
1586
|
+
var import_react15 = require("react");
|
|
1930
1587
|
function useCoAgent(options) {
|
|
1931
|
-
const generalContext = useCopilotContext();
|
|
1932
|
-
const { availableAgents } = generalContext;
|
|
1933
|
-
const { addToast } = useToast();
|
|
1934
1588
|
const isExternalStateManagement = (options2) => {
|
|
1935
1589
|
return "state" in options2 && "setState" in options2;
|
|
1936
1590
|
};
|
|
1937
1591
|
const { name } = options;
|
|
1938
|
-
(0, import_react17.useEffect)(() => {
|
|
1939
|
-
if ((availableAgents == null ? void 0 : availableAgents.length) && !availableAgents.some((a) => a.name === name)) {
|
|
1940
|
-
const message = `(useCoAgent): Agent "${name}" not found. Make sure the agent exists and is properly configured.`;
|
|
1941
|
-
console.warn(message);
|
|
1942
|
-
addToast({ type: "warning", message });
|
|
1943
|
-
}
|
|
1944
|
-
}, [availableAgents]);
|
|
1945
1592
|
const isInternalStateManagementWithInitial = (options2) => {
|
|
1946
1593
|
return "initialState" in options2;
|
|
1947
1594
|
};
|
|
1595
|
+
const generalContext = useCopilotContext();
|
|
1948
1596
|
const messagesContext = useCopilotMessagesContext();
|
|
1949
1597
|
const context = __spreadValues(__spreadValues({}, generalContext), messagesContext);
|
|
1950
|
-
const { coagentStates,
|
|
1951
|
-
const { appendMessage
|
|
1598
|
+
const { coagentStates, setCoagentStates } = context;
|
|
1599
|
+
const { appendMessage } = useCopilotChat();
|
|
1952
1600
|
const getCoagentState = (coagentStates2, name2) => {
|
|
1953
1601
|
if (coagentStates2[name2]) {
|
|
1954
1602
|
return coagentStates2[name2];
|
|
@@ -1965,43 +1613,40 @@ function useCoAgent(options) {
|
|
|
1965
1613
|
}
|
|
1966
1614
|
};
|
|
1967
1615
|
const setState = (newState) => {
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1616
|
+
setCoagentStates((prevAgentStates) => {
|
|
1617
|
+
let coagentState2 = getCoagentState(prevAgentStates, name);
|
|
1618
|
+
const updatedState = typeof newState === "function" ? newState(coagentState2.state) : newState;
|
|
1619
|
+
return __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
1620
|
+
[name]: __spreadProps(__spreadValues({}, coagentState2), {
|
|
1621
|
+
state: updatedState
|
|
1622
|
+
})
|
|
1623
|
+
});
|
|
1624
|
+
});
|
|
1975
1625
|
};
|
|
1976
1626
|
const coagentState = getCoagentState(coagentStates, name);
|
|
1977
1627
|
const state = isExternalStateManagement(options) ? options.state : coagentState.state;
|
|
1978
|
-
(0,
|
|
1628
|
+
(0, import_react15.useEffect)(() => {
|
|
1979
1629
|
if (isExternalStateManagement(options)) {
|
|
1980
1630
|
setState(options.state);
|
|
1981
1631
|
} else if (coagentStates[name] === void 0) {
|
|
1982
1632
|
setState(options.initialState === void 0 ? {} : options.initialState);
|
|
1983
1633
|
}
|
|
1984
|
-
}, [
|
|
1985
|
-
isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0,
|
|
1986
|
-
// reset initialstate on reset
|
|
1987
|
-
coagentStates[name] === void 0
|
|
1988
|
-
]);
|
|
1989
|
-
const runAgentCallback = useAsyncCallback(
|
|
1990
|
-
(hint) => __async(this, null, function* () {
|
|
1991
|
-
yield runAgent(name, context, appendMessage, runChatCompletion, hint);
|
|
1992
|
-
}),
|
|
1993
|
-
[name, context, appendMessage, runChatCompletion]
|
|
1994
|
-
);
|
|
1634
|
+
}, [isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0]);
|
|
1995
1635
|
return {
|
|
1996
1636
|
name,
|
|
1997
1637
|
nodeName: coagentState.nodeName,
|
|
1998
|
-
|
|
1638
|
+
state,
|
|
1639
|
+
setState,
|
|
1999
1640
|
running: coagentState.running,
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
stop: () =>
|
|
2004
|
-
|
|
1641
|
+
start: () => {
|
|
1642
|
+
startAgent(name, context);
|
|
1643
|
+
},
|
|
1644
|
+
stop: () => {
|
|
1645
|
+
stopAgent(name, context);
|
|
1646
|
+
},
|
|
1647
|
+
run: (hint) => {
|
|
1648
|
+
return runAgent(name, context, appendMessage, hint);
|
|
1649
|
+
}
|
|
2005
1650
|
};
|
|
2006
1651
|
}
|
|
2007
1652
|
function startAgent(name, context) {
|
|
@@ -2014,22 +1659,11 @@ function stopAgent(name, context) {
|
|
|
2014
1659
|
const { agentSession, setAgentSession } = context;
|
|
2015
1660
|
if (agentSession && agentSession.agentName === name) {
|
|
2016
1661
|
setAgentSession(null);
|
|
2017
|
-
context.setCoagentStates((prevAgentStates) => {
|
|
2018
|
-
return __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
2019
|
-
[name]: __spreadProps(__spreadValues({}, prevAgentStates[name]), {
|
|
2020
|
-
running: false,
|
|
2021
|
-
active: false,
|
|
2022
|
-
threadId: void 0,
|
|
2023
|
-
nodeName: void 0,
|
|
2024
|
-
runId: void 0
|
|
2025
|
-
})
|
|
2026
|
-
});
|
|
2027
|
-
});
|
|
2028
1662
|
} else {
|
|
2029
1663
|
console.warn(`No agent session found for ${name}`);
|
|
2030
1664
|
}
|
|
2031
1665
|
}
|
|
2032
|
-
function runAgent(name, context, appendMessage,
|
|
1666
|
+
function runAgent(name, context, appendMessage, hint) {
|
|
2033
1667
|
return __async(this, null, function* () {
|
|
2034
1668
|
var _a, _b;
|
|
2035
1669
|
const { agentSession, setAgentSession } = context;
|
|
@@ -2045,73 +1679,25 @@ function runAgent(name, context, appendMessage, runChatCompletion, hint) {
|
|
|
2045
1679
|
previousState = message.state;
|
|
2046
1680
|
}
|
|
2047
1681
|
}
|
|
2048
|
-
let state = ((_b = (_a = context.
|
|
1682
|
+
let state = ((_b = (_a = context.coagentStates) == null ? void 0 : _a[name]) == null ? void 0 : _b.state) || {};
|
|
2049
1683
|
if (hint) {
|
|
2050
1684
|
const hintMessage = hint({ previousState, currentState: state });
|
|
2051
1685
|
if (hintMessage) {
|
|
2052
1686
|
yield appendMessage(hintMessage);
|
|
2053
|
-
} else {
|
|
2054
|
-
yield runChatCompletion();
|
|
2055
1687
|
}
|
|
2056
|
-
} else {
|
|
2057
|
-
yield runChatCompletion();
|
|
2058
1688
|
}
|
|
2059
1689
|
});
|
|
2060
1690
|
}
|
|
2061
1691
|
|
|
2062
|
-
// src/hooks/use-copilot-authenticated-action.ts
|
|
2063
|
-
var import_react18 = require("react");
|
|
2064
|
-
var import_react19 = __toESM(require("react"));
|
|
2065
|
-
function useCopilotAuthenticatedAction(action, dependencies) {
|
|
2066
|
-
const { authConfig, authStates, setAuthStates } = useCopilotContext();
|
|
2067
|
-
const pendingActionRef = (0, import_react18.useRef)(null);
|
|
2068
|
-
const executeAction = (0, import_react18.useCallback)(
|
|
2069
|
-
(props) => {
|
|
2070
|
-
if (typeof action.render === "function") {
|
|
2071
|
-
return action.render(props);
|
|
2072
|
-
}
|
|
2073
|
-
return action.render || import_react19.default.createElement(import_react18.Fragment);
|
|
2074
|
-
},
|
|
2075
|
-
[action]
|
|
2076
|
-
);
|
|
2077
|
-
const wrappedRender = (0, import_react18.useCallback)(
|
|
2078
|
-
(props) => {
|
|
2079
|
-
const isAuthenticated = Object.values(authStates || {}).some(
|
|
2080
|
-
(state) => state.status === "authenticated"
|
|
2081
|
-
);
|
|
2082
|
-
if (!isAuthenticated) {
|
|
2083
|
-
pendingActionRef.current = props;
|
|
2084
|
-
return (authConfig == null ? void 0 : authConfig.SignInComponent) ? import_react19.default.createElement(authConfig.SignInComponent, {
|
|
2085
|
-
onSignInComplete: (authState) => {
|
|
2086
|
-
setAuthStates == null ? void 0 : setAuthStates((prev) => __spreadProps(__spreadValues({}, prev), { [action.name]: authState }));
|
|
2087
|
-
if (pendingActionRef.current) {
|
|
2088
|
-
executeAction(pendingActionRef.current);
|
|
2089
|
-
pendingActionRef.current = null;
|
|
2090
|
-
}
|
|
2091
|
-
}
|
|
2092
|
-
}) : import_react19.default.createElement(import_react18.Fragment);
|
|
2093
|
-
}
|
|
2094
|
-
return executeAction(props);
|
|
2095
|
-
},
|
|
2096
|
-
[action, authStates, setAuthStates]
|
|
2097
|
-
);
|
|
2098
|
-
useCopilotAction(
|
|
2099
|
-
__spreadProps(__spreadValues({}, action), {
|
|
2100
|
-
render: wrappedRender
|
|
2101
|
-
}),
|
|
2102
|
-
dependencies
|
|
2103
|
-
);
|
|
2104
|
-
}
|
|
2105
|
-
|
|
2106
1692
|
// src/lib/copilot-task.ts
|
|
2107
|
-
var
|
|
1693
|
+
var import_runtime_client_gql6 = require("@copilotkit/runtime-client-gql");
|
|
1694
|
+
var import_shared8 = require("@copilotkit/shared");
|
|
2108
1695
|
var CopilotTask = class {
|
|
2109
1696
|
constructor(config) {
|
|
2110
1697
|
this.instructions = config.instructions;
|
|
2111
1698
|
this.actions = config.actions || [];
|
|
2112
1699
|
this.includeCopilotReadable = config.includeCopilotReadable !== false;
|
|
2113
1700
|
this.includeCopilotActions = config.includeCopilotActions !== false;
|
|
2114
|
-
this.forwardedParameters = config.forwardedParameters;
|
|
2115
1701
|
}
|
|
2116
1702
|
/**
|
|
2117
1703
|
* Run the task.
|
|
@@ -2120,7 +1706,7 @@ var CopilotTask = class {
|
|
|
2120
1706
|
*/
|
|
2121
1707
|
run(context, data) {
|
|
2122
1708
|
return __async(this, null, function* () {
|
|
2123
|
-
var _a, _b
|
|
1709
|
+
var _a, _b;
|
|
2124
1710
|
const actions = this.includeCopilotActions ? Object.assign({}, context.actions) : {};
|
|
2125
1711
|
for (const fn of this.actions) {
|
|
2126
1712
|
actions[fn.name] = fn;
|
|
@@ -2132,12 +1718,12 @@ var CopilotTask = class {
|
|
|
2132
1718
|
if (this.includeCopilotReadable) {
|
|
2133
1719
|
contextString += context.getContextString([], defaultCopilotContextCategories);
|
|
2134
1720
|
}
|
|
2135
|
-
const systemMessage = new
|
|
1721
|
+
const systemMessage = new import_runtime_client_gql6.TextMessage({
|
|
2136
1722
|
content: taskSystemMessage(contextString, this.instructions),
|
|
2137
|
-
role:
|
|
1723
|
+
role: import_runtime_client_gql6.Role.System
|
|
2138
1724
|
});
|
|
2139
1725
|
const messages = [systemMessage];
|
|
2140
|
-
const runtimeClient = new
|
|
1726
|
+
const runtimeClient = new import_runtime_client_gql6.CopilotRuntimeClient({
|
|
2141
1727
|
url: context.copilotApiConfig.chatApiEndpoint,
|
|
2142
1728
|
publicApiKey: context.copilotApiConfig.publicApiKey,
|
|
2143
1729
|
headers: context.copilotApiConfig.headers,
|
|
@@ -2146,22 +1732,26 @@ var CopilotTask = class {
|
|
|
2146
1732
|
const response = yield runtimeClient.generateCopilotResponse({
|
|
2147
1733
|
data: {
|
|
2148
1734
|
frontend: {
|
|
2149
|
-
actions:
|
|
1735
|
+
actions: Object.values(actions).map((action) => ({
|
|
1736
|
+
name: action.name,
|
|
1737
|
+
description: action.description || "",
|
|
1738
|
+
jsonSchema: JSON.stringify((0, import_shared8.actionParametersToJsonSchema)(action.parameters || []))
|
|
1739
|
+
})),
|
|
2150
1740
|
url: window.location.href
|
|
2151
1741
|
},
|
|
2152
|
-
messages: (0,
|
|
1742
|
+
messages: (0, import_runtime_client_gql6.convertMessagesToGqlInput)((0, import_runtime_client_gql6.filterAgentStateMessages)(messages)),
|
|
2153
1743
|
metadata: {
|
|
2154
|
-
requestType:
|
|
1744
|
+
requestType: import_runtime_client_gql6.CopilotRequestType.Task
|
|
2155
1745
|
},
|
|
2156
|
-
forwardedParameters:
|
|
1746
|
+
forwardedParameters: {
|
|
2157
1747
|
toolChoice: "required"
|
|
2158
|
-
}
|
|
1748
|
+
}
|
|
2159
1749
|
},
|
|
2160
1750
|
properties: context.copilotApiConfig.properties
|
|
2161
1751
|
}).toPromise();
|
|
2162
1752
|
const functionCallHandler = context.getFunctionCallHandler(actions);
|
|
2163
|
-
const functionCalls = (0,
|
|
2164
|
-
((
|
|
1753
|
+
const functionCalls = (0, import_runtime_client_gql6.convertGqlOutputToMessages)(
|
|
1754
|
+
((_b = (_a = response.data) == null ? void 0 : _a.generateCopilotResponse) == null ? void 0 : _b.messages) || []
|
|
2165
1755
|
).filter((m) => m.isActionExecutionMessage());
|
|
2166
1756
|
for (const functionCall of functionCalls) {
|
|
2167
1757
|
yield functionCallHandler({
|
|
@@ -2206,14 +1796,10 @@ ${instructions}
|
|
|
2206
1796
|
CopilotTask,
|
|
2207
1797
|
defaultCopilotContextCategories,
|
|
2208
1798
|
extract,
|
|
2209
|
-
runAgent,
|
|
2210
1799
|
shouldShowDevConsole,
|
|
2211
|
-
startAgent,
|
|
2212
|
-
stopAgent,
|
|
2213
1800
|
useCoAgent,
|
|
2214
1801
|
useCoAgentStateRender,
|
|
2215
1802
|
useCopilotAction,
|
|
2216
|
-
useCopilotAuthenticatedAction,
|
|
2217
1803
|
useCopilotChat,
|
|
2218
1804
|
useCopilotContext,
|
|
2219
1805
|
useCopilotMessagesContext,
|