@copilotkit/react-core 1.4.8-next.2 → 1.4.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -13
- package/dist/{chunk-DIX6ISOB.mjs → chunk-6EMLM6WX.mjs} +20 -19
- package/dist/chunk-6EMLM6WX.mjs.map +1 -0
- package/dist/{chunk-ODN4H66E.mjs → chunk-7LRDVJH5.mjs} +6 -2
- package/dist/chunk-7LRDVJH5.mjs.map +1 -0
- package/dist/{chunk-XXCJFOLY.mjs → chunk-AG7FH7OD.mjs} +2 -2
- package/dist/{chunk-XYFTGCMS.mjs → chunk-FSC4A3JN.mjs} +18 -14
- package/dist/chunk-FSC4A3JN.mjs.map +1 -0
- package/dist/{chunk-E3HHCHMQ.mjs → chunk-IFTHM7LF.mjs} +7 -10
- package/dist/chunk-IFTHM7LF.mjs.map +1 -0
- package/dist/{chunk-V6CEKRZA.mjs → chunk-IVYL7JRC.mjs} +12 -11
- package/dist/chunk-IVYL7JRC.mjs.map +1 -0
- package/dist/chunk-NTLCOVE5.mjs +321 -0
- package/dist/chunk-NTLCOVE5.mjs.map +1 -0
- package/dist/chunk-QCUP6HLK.mjs +37 -0
- package/dist/chunk-QCUP6HLK.mjs.map +1 -0
- package/dist/{chunk-2KCEHGSI.mjs → chunk-SFPANIOY.mjs} +99 -49
- package/dist/chunk-SFPANIOY.mjs.map +1 -0
- package/dist/{chunk-CBMCK7UC.mjs → chunk-UOVONDR6.mjs} +2 -2
- package/dist/{chunk-XSORW54K.mjs → chunk-X6ZF5WAX.mjs} +2 -2
- package/dist/{chunk-4P56GAFP.mjs → chunk-XQFVXX6R.mjs} +2 -3
- package/dist/chunk-XQFVXX6R.mjs.map +1 -0
- package/dist/{chunk-ETCKRNXQ.mjs → chunk-YUY5ZAST.mjs} +7 -4
- package/dist/chunk-YUY5ZAST.mjs.map +1 -0
- package/dist/components/copilot-provider/copilot-messages.d.ts +0 -1
- package/dist/components/copilot-provider/copilotkit-props.d.ts +0 -5
- package/dist/components/copilot-provider/copilotkit-props.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.d.ts +0 -1
- package/dist/components/copilot-provider/copilotkit.js +158 -95
- 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.d.ts +0 -1
- package/dist/components/copilot-provider/index.js +158 -95
- 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.d.ts +0 -1
- package/dist/components/index.js +158 -95
- 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 +1 -5
- package/dist/context/copilot-context.js +1 -2
- package/dist/context/copilot-context.js.map +1 -1
- package/dist/context/copilot-context.mjs +1 -1
- package/dist/context/index.js +1 -2
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +1 -1
- package/dist/hooks/index.js +401 -260
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +19 -18
- package/dist/hooks/use-chat.d.ts +1 -2
- package/dist/hooks/use-chat.js +343 -200
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +3 -3
- package/dist/hooks/use-coagent-state-render.js +1 -2
- 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.js +381 -240
- package/dist/hooks/use-coagent.js.map +1 -1
- package/dist/hooks/use-coagent.mjs +9 -8
- package/dist/hooks/use-copilot-action.js +133 -9
- 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.js +370 -230
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +8 -7
- package/dist/hooks/use-copilot-readable.js +1 -2
- 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 +1 -2
- 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 +1 -1
- package/dist/index.js +450 -353
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +20 -19
- package/dist/lib/copilot-task.d.ts +1 -6
- package/dist/lib/copilot-task.js +4 -22
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +6 -5
- package/dist/lib/index.d.ts +1 -1
- package/dist/lib/index.js +4 -22
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +6 -5
- 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 +5 -4
- package/dist/utils/index.js +3 -21
- 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-props.tsx +0 -6
- package/src/components/copilot-provider/copilotkit.tsx +4 -2
- 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 +1 -8
- package/src/hooks/use-chat.ts +286 -266
- package/src/hooks/use-coagent.ts +14 -11
- package/src/hooks/use-copilot-action.ts +3 -2
- package/src/hooks/use-copilot-chat.ts +9 -10
- package/src/hooks/use-copilot-runtime-client.ts +4 -0
- package/src/lib/copilot-task.ts +2 -10
- package/src/utils/extract.ts +0 -4
- package/dist/chunk-2KCEHGSI.mjs.map +0 -1
- package/dist/chunk-4P56GAFP.mjs.map +0 -1
- package/dist/chunk-DIX6ISOB.mjs.map +0 -1
- package/dist/chunk-E3HHCHMQ.mjs.map +0 -1
- package/dist/chunk-ETCKRNXQ.mjs.map +0 -1
- package/dist/chunk-ODN4H66E.mjs.map +0 -1
- package/dist/chunk-PG5XEJVS.mjs +0 -295
- package/dist/chunk-PG5XEJVS.mjs.map +0 -1
- package/dist/chunk-V6CEKRZA.mjs.map +0 -1
- package/dist/chunk-XYFTGCMS.mjs.map +0 -1
- /package/dist/{chunk-XXCJFOLY.mjs.map → chunk-AG7FH7OD.mjs.map} +0 -0
- /package/dist/{chunk-CBMCK7UC.mjs.map → chunk-UOVONDR6.mjs.map} +0 -0
- /package/dist/{chunk-XSORW54K.mjs.map → chunk-X6ZF5WAX.mjs.map} +0 -0
|
@@ -83,7 +83,7 @@ __export(use_copilot_chat_exports, {
|
|
|
83
83
|
useCopilotChat: () => useCopilotChat
|
|
84
84
|
});
|
|
85
85
|
module.exports = __toCommonJS(use_copilot_chat_exports);
|
|
86
|
-
var
|
|
86
|
+
var import_react8 = require("react");
|
|
87
87
|
|
|
88
88
|
// src/context/copilot-context.tsx
|
|
89
89
|
var import_react = __toESM(require("react"));
|
|
@@ -139,8 +139,7 @@ var emptyCopilotContext = {
|
|
|
139
139
|
},
|
|
140
140
|
agentSession: null,
|
|
141
141
|
setAgentSession: () => {
|
|
142
|
-
}
|
|
143
|
-
forwardedParameters: {}
|
|
142
|
+
}
|
|
144
143
|
};
|
|
145
144
|
var CopilotContext = import_react.default.createContext(emptyCopilotContext);
|
|
146
145
|
function useCopilotContext() {
|
|
@@ -159,16 +158,131 @@ function returnAndThrowInDebug(value) {
|
|
|
159
158
|
var import_runtime_client_gql3 = require("@copilotkit/runtime-client-gql");
|
|
160
159
|
|
|
161
160
|
// src/hooks/use-chat.ts
|
|
162
|
-
var
|
|
161
|
+
var import_react5 = require("react");
|
|
163
162
|
var import_shared = require("@copilotkit/shared");
|
|
164
163
|
var import_runtime_client_gql2 = require("@copilotkit/runtime-client-gql");
|
|
165
164
|
|
|
166
165
|
// src/components/toast/toast-provider.tsx
|
|
166
|
+
var import_react3 = require("react");
|
|
167
|
+
|
|
168
|
+
// src/components/error-boundary/error-utils.tsx
|
|
167
169
|
var import_react2 = require("react");
|
|
170
|
+
|
|
171
|
+
// src/components/toast/exclamation-mark-icon.tsx
|
|
168
172
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
169
|
-
var
|
|
173
|
+
var ExclamationMarkIcon = ({
|
|
174
|
+
className,
|
|
175
|
+
style
|
|
176
|
+
}) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
|
|
177
|
+
"svg",
|
|
178
|
+
{
|
|
179
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
180
|
+
width: "24",
|
|
181
|
+
height: "24",
|
|
182
|
+
viewBox: "0 0 24 24",
|
|
183
|
+
fill: "none",
|
|
184
|
+
stroke: "currentColor",
|
|
185
|
+
strokeWidth: "2",
|
|
186
|
+
strokeLinecap: "round",
|
|
187
|
+
strokeLinejoin: "round",
|
|
188
|
+
className: `lucide lucide-circle-alert ${className ? className : ""}`,
|
|
189
|
+
style,
|
|
190
|
+
children: [
|
|
191
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("circle", { cx: "12", cy: "12", r: "10" }),
|
|
192
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("line", { x1: "12", x2: "12", y1: "8", y2: "12" }),
|
|
193
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("line", { x1: "12", x2: "12.01", y1: "16", y2: "16" })
|
|
194
|
+
]
|
|
195
|
+
}
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
// src/components/error-boundary/error-utils.tsx
|
|
199
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
200
|
+
function ErrorToast({ errors }) {
|
|
201
|
+
const errorsToRender = errors.map((error, idx) => {
|
|
202
|
+
var _a, _b, _c;
|
|
203
|
+
const message = "extensions" in error ? (_b = (_a = error.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message : error.message;
|
|
204
|
+
const code = "extensions" in error ? (_c = error.extensions) == null ? void 0 : _c.code : null;
|
|
205
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
206
|
+
"div",
|
|
207
|
+
{
|
|
208
|
+
style: {
|
|
209
|
+
marginTop: idx === 0 ? 0 : 10,
|
|
210
|
+
marginBottom: 14
|
|
211
|
+
},
|
|
212
|
+
children: [
|
|
213
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ExclamationMarkIcon, { style: { marginBottom: 4 } }),
|
|
214
|
+
code && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
215
|
+
"div",
|
|
216
|
+
{
|
|
217
|
+
style: {
|
|
218
|
+
fontWeight: "600",
|
|
219
|
+
marginBottom: 4
|
|
220
|
+
},
|
|
221
|
+
children: [
|
|
222
|
+
"Copilot Cloud Error:",
|
|
223
|
+
" ",
|
|
224
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { style: { fontFamily: "monospace", fontWeight: "normal" }, children: code })
|
|
225
|
+
]
|
|
226
|
+
}
|
|
227
|
+
),
|
|
228
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { children: message })
|
|
229
|
+
]
|
|
230
|
+
},
|
|
231
|
+
idx
|
|
232
|
+
);
|
|
233
|
+
});
|
|
234
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
235
|
+
"div",
|
|
236
|
+
{
|
|
237
|
+
style: {
|
|
238
|
+
fontSize: "13px",
|
|
239
|
+
maxWidth: "600px"
|
|
240
|
+
},
|
|
241
|
+
children: [
|
|
242
|
+
errorsToRender,
|
|
243
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { fontSize: "11px", opacity: 0.75 }, children: "NOTE: This error only displays during local development." })
|
|
244
|
+
]
|
|
245
|
+
}
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
function useErrorToast() {
|
|
249
|
+
const { addToast } = useToast();
|
|
250
|
+
return (0, import_react2.useCallback)(
|
|
251
|
+
(error) => {
|
|
252
|
+
const errorId = error.map((err) => {
|
|
253
|
+
var _a, _b;
|
|
254
|
+
const message = "extensions" in err ? ((_b = (_a = err.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message) || err.message : err.message;
|
|
255
|
+
const stack = err.stack || "";
|
|
256
|
+
return btoa(message + stack).slice(0, 32);
|
|
257
|
+
}).join("|");
|
|
258
|
+
addToast({
|
|
259
|
+
type: "error",
|
|
260
|
+
id: errorId,
|
|
261
|
+
// Toast libraries typically dedupe by id
|
|
262
|
+
message: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ErrorToast, { errors: error })
|
|
263
|
+
});
|
|
264
|
+
},
|
|
265
|
+
[addToast]
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
function useAsyncCallback(callback, deps) {
|
|
269
|
+
const addErrorToast = useErrorToast();
|
|
270
|
+
return (0, import_react2.useCallback)((...args) => __async(this, null, function* () {
|
|
271
|
+
try {
|
|
272
|
+
return yield callback(...args);
|
|
273
|
+
} catch (error) {
|
|
274
|
+
console.error("Error in async callback:", error);
|
|
275
|
+
addErrorToast([error]);
|
|
276
|
+
throw error;
|
|
277
|
+
}
|
|
278
|
+
}), deps);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// src/components/toast/toast-provider.tsx
|
|
282
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
283
|
+
var ToastContext = (0, import_react3.createContext)(void 0);
|
|
170
284
|
function useToast() {
|
|
171
|
-
const context = (0,
|
|
285
|
+
const context = (0, import_react3.useContext)(ToastContext);
|
|
172
286
|
if (!context) {
|
|
173
287
|
throw new Error("useToast must be used within a ToastProvider");
|
|
174
288
|
}
|
|
@@ -177,14 +291,17 @@ function useToast() {
|
|
|
177
291
|
|
|
178
292
|
// src/hooks/use-copilot-runtime-client.ts
|
|
179
293
|
var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
|
|
180
|
-
var
|
|
294
|
+
var import_react4 = require("react");
|
|
181
295
|
var useCopilotRuntimeClient = (options) => {
|
|
182
296
|
const { addGraphQLErrorsToast } = useToast();
|
|
183
|
-
const
|
|
297
|
+
const addErrorToast = useErrorToast();
|
|
298
|
+
const runtimeClient = (0, import_react4.useMemo)(() => {
|
|
184
299
|
return new import_runtime_client_gql.CopilotRuntimeClient(__spreadProps(__spreadValues({}, options), {
|
|
185
300
|
handleGQLErrors: (error) => {
|
|
186
301
|
if (error.graphQLErrors.length) {
|
|
187
302
|
addGraphQLErrorsToast(error.graphQLErrors);
|
|
303
|
+
} else {
|
|
304
|
+
addErrorToast([error]);
|
|
188
305
|
}
|
|
189
306
|
}
|
|
190
307
|
}));
|
|
@@ -210,12 +327,12 @@ function useChat(options) {
|
|
|
210
327
|
agentSession,
|
|
211
328
|
setAgentSession
|
|
212
329
|
} = options;
|
|
213
|
-
const abortControllerRef = (0,
|
|
214
|
-
const threadIdRef = (0,
|
|
215
|
-
const runIdRef = (0,
|
|
330
|
+
const abortControllerRef = (0, import_react5.useRef)();
|
|
331
|
+
const threadIdRef = (0, import_react5.useRef)(null);
|
|
332
|
+
const runIdRef = (0, import_react5.useRef)(null);
|
|
216
333
|
const { addGraphQLErrorsToast } = useToast();
|
|
217
|
-
const runChatCompletionRef = (0,
|
|
218
|
-
const agentSessionRef = (0,
|
|
334
|
+
const runChatCompletionRef = (0, import_react5.useRef)();
|
|
335
|
+
const agentSessionRef = (0, import_react5.useRef)(agentSession);
|
|
219
336
|
agentSessionRef.current = agentSession;
|
|
220
337
|
const publicApiKey = copilotConfig.publicApiKey;
|
|
221
338
|
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [import_shared.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
|
|
@@ -225,211 +342,236 @@ function useChat(options) {
|
|
|
225
342
|
headers,
|
|
226
343
|
credentials: copilotConfig.credentials
|
|
227
344
|
});
|
|
228
|
-
const runChatCompletion = (
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
runtimeClient.
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
available
|
|
254
|
-
|
|
255
|
-
available
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
345
|
+
const runChatCompletion = useAsyncCallback(
|
|
346
|
+
(previousMessages) => __async(this, null, function* () {
|
|
347
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
348
|
+
setIsLoading(true);
|
|
349
|
+
let newMessages = [
|
|
350
|
+
new import_runtime_client_gql2.TextMessage({
|
|
351
|
+
content: "",
|
|
352
|
+
role: import_runtime_client_gql2.Role.Assistant
|
|
353
|
+
})
|
|
354
|
+
];
|
|
355
|
+
const abortController = new AbortController();
|
|
356
|
+
abortControllerRef.current = abortController;
|
|
357
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
358
|
+
const systemMessage = makeSystemMessageCallback();
|
|
359
|
+
const messagesWithContext = [systemMessage, ...initialMessages || [], ...previousMessages];
|
|
360
|
+
const stream = runtimeClient.asStream(
|
|
361
|
+
runtimeClient.generateCopilotResponse({
|
|
362
|
+
data: __spreadProps(__spreadValues(__spreadProps(__spreadValues({
|
|
363
|
+
frontend: {
|
|
364
|
+
actions: actions.filter(
|
|
365
|
+
(action) => action.available !== import_runtime_client_gql2.ActionInputAvailability.Disabled || !action.disabled
|
|
366
|
+
).map((action) => {
|
|
367
|
+
let available = import_runtime_client_gql2.ActionInputAvailability.Enabled;
|
|
368
|
+
if (action.disabled) {
|
|
369
|
+
available = import_runtime_client_gql2.ActionInputAvailability.Disabled;
|
|
370
|
+
} else if (action.available === "disabled") {
|
|
371
|
+
available = import_runtime_client_gql2.ActionInputAvailability.Disabled;
|
|
372
|
+
} else if (action.available === "remote") {
|
|
373
|
+
available = import_runtime_client_gql2.ActionInputAvailability.Remote;
|
|
374
|
+
}
|
|
375
|
+
return {
|
|
376
|
+
name: action.name,
|
|
377
|
+
description: action.description || "",
|
|
378
|
+
jsonSchema: JSON.stringify(
|
|
379
|
+
(0, import_shared.actionParametersToJsonSchema)(action.parameters || [])
|
|
380
|
+
),
|
|
381
|
+
available
|
|
382
|
+
};
|
|
383
|
+
}),
|
|
384
|
+
url: window.location.href
|
|
385
|
+
},
|
|
386
|
+
threadId: threadIdRef.current,
|
|
387
|
+
runId: runIdRef.current,
|
|
388
|
+
messages: (0, import_runtime_client_gql2.convertMessagesToGqlInput)((0, import_runtime_client_gql2.filterAgentStateMessages)(messagesWithContext))
|
|
389
|
+
}, copilotConfig.cloud ? {
|
|
390
|
+
cloud: __spreadValues({}, ((_c = (_b = (_a = copilotConfig.cloud.guardrails) == null ? void 0 : _a.input) == null ? void 0 : _b.restrictToTopic) == null ? void 0 : _c.enabled) ? {
|
|
391
|
+
guardrails: {
|
|
392
|
+
inputValidationRules: {
|
|
393
|
+
allowList: copilotConfig.cloud.guardrails.input.restrictToTopic.validTopics,
|
|
394
|
+
denyList: copilotConfig.cloud.guardrails.input.restrictToTopic.invalidTopics
|
|
395
|
+
}
|
|
275
396
|
}
|
|
397
|
+
} : {})
|
|
398
|
+
} : {}), {
|
|
399
|
+
metadata: {
|
|
400
|
+
requestType: import_runtime_client_gql2.CopilotRequestType.Chat
|
|
276
401
|
}
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
402
|
+
}), agentSessionRef.current ? {
|
|
403
|
+
agentSession: agentSessionRef.current
|
|
404
|
+
} : {}), {
|
|
405
|
+
agentStates: Object.values(coagentStatesRef.current).map((state) => ({
|
|
406
|
+
agentName: state.name,
|
|
407
|
+
state: JSON.stringify(state.state)
|
|
408
|
+
}))
|
|
409
|
+
}),
|
|
410
|
+
properties: copilotConfig.properties,
|
|
411
|
+
signal: (_d = abortControllerRef.current) == null ? void 0 : _d.signal
|
|
412
|
+
})
|
|
413
|
+
);
|
|
414
|
+
const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
|
|
415
|
+
const reader = stream.getReader();
|
|
416
|
+
let actionResults = {};
|
|
417
|
+
let executedCoAgentStateRenders = [];
|
|
418
|
+
let followUp = void 0;
|
|
419
|
+
try {
|
|
420
|
+
while (true) {
|
|
421
|
+
let done, value;
|
|
422
|
+
try {
|
|
423
|
+
const readResult = yield reader.read();
|
|
424
|
+
done = readResult.done;
|
|
425
|
+
value = readResult.value;
|
|
426
|
+
} catch (readError) {
|
|
427
|
+
break;
|
|
281
428
|
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
429
|
+
if (done) {
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
if (!(value == null ? void 0 : value.generateCopilotResponse)) {
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
threadIdRef.current = value.generateCopilotResponse.threadId || null;
|
|
436
|
+
runIdRef.current = value.generateCopilotResponse.runId || null;
|
|
437
|
+
const messages2 = (0, import_runtime_client_gql2.convertGqlOutputToMessages)(
|
|
438
|
+
(0, import_runtime_client_gql2.filterAdjacentAgentStateMessages)(value.generateCopilotResponse.messages)
|
|
439
|
+
);
|
|
440
|
+
if (messages2.length === 0) {
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
newMessages = [];
|
|
444
|
+
if (((_h = value.generateCopilotResponse.status) == null ? void 0 : _h.__typename) === "FailedResponseStatus" && value.generateCopilotResponse.status.reason === "GUARDRAILS_VALIDATION_FAILED") {
|
|
445
|
+
newMessages = [
|
|
446
|
+
new import_runtime_client_gql2.TextMessage({
|
|
447
|
+
role: import_runtime_client_gql2.MessageRole.Assistant,
|
|
448
|
+
content: ((_i = value.generateCopilotResponse.status.details) == null ? void 0 : _i.guardrailsReason) || ""
|
|
449
|
+
})
|
|
450
|
+
];
|
|
451
|
+
} else {
|
|
452
|
+
for (const message of messages2) {
|
|
453
|
+
newMessages.push(message);
|
|
454
|
+
if (message.isActionExecutionMessage() && message.status.code !== import_runtime_client_gql2.MessageStatusCode.Pending && message.scope === "client" && onFunctionCall) {
|
|
455
|
+
if (!(message.id in actionResults)) {
|
|
456
|
+
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
457
|
+
break;
|
|
458
|
+
}
|
|
459
|
+
try {
|
|
460
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
461
|
+
const action = actions.find((action2) => action2.name === message.name);
|
|
462
|
+
if (action) {
|
|
463
|
+
followUp = action.followUp;
|
|
464
|
+
}
|
|
465
|
+
const result = yield onFunctionCall({
|
|
466
|
+
messages: previousMessages,
|
|
467
|
+
name: message.name,
|
|
468
|
+
args: message.arguments
|
|
469
|
+
});
|
|
470
|
+
actionResults[message.id] = result;
|
|
471
|
+
} catch (e) {
|
|
472
|
+
actionResults[message.id] = `Failed to execute action ${message.name}`;
|
|
473
|
+
console.error(`Failed to execute action ${message.name}: ${e}`);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
newMessages.push(
|
|
477
|
+
new import_runtime_client_gql2.ResultMessage({
|
|
478
|
+
result: import_runtime_client_gql2.ResultMessage.encodeResult(actionResults[message.id]),
|
|
479
|
+
actionExecutionId: message.id,
|
|
480
|
+
actionName: message.name
|
|
481
|
+
})
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
if (message.isAgentStateMessage() && !message.active && !executedCoAgentStateRenders.includes(message.id) && onCoAgentStateRender) {
|
|
337
485
|
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
338
486
|
break;
|
|
339
487
|
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
const result = yield onFunctionCall({
|
|
347
|
-
messages: previousMessages,
|
|
348
|
-
name: message.name,
|
|
349
|
-
args: message.arguments
|
|
350
|
-
});
|
|
351
|
-
actionResults[message.id] = result;
|
|
352
|
-
} catch (e) {
|
|
353
|
-
actionResults[message.id] = `Failed to execute action ${message.name}`;
|
|
354
|
-
console.error(`Failed to execute action ${message.name}: ${e}`);
|
|
355
|
-
}
|
|
488
|
+
yield onCoAgentStateRender({
|
|
489
|
+
name: message.agentName,
|
|
490
|
+
nodeName: message.nodeName,
|
|
491
|
+
state: message.state
|
|
492
|
+
});
|
|
493
|
+
executedCoAgentStateRenders.push(message.id);
|
|
356
494
|
}
|
|
357
|
-
newMessages.push(
|
|
358
|
-
new import_runtime_client_gql2.ResultMessage({
|
|
359
|
-
result: import_runtime_client_gql2.ResultMessage.encodeResult(actionResults[message.id]),
|
|
360
|
-
actionExecutionId: message.id,
|
|
361
|
-
actionName: message.name
|
|
362
|
-
})
|
|
363
|
-
);
|
|
364
495
|
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
496
|
+
const lastAgentStateMessage = [...messages2].reverse().find((message) => message.isAgentStateMessage());
|
|
497
|
+
if (lastAgentStateMessage) {
|
|
498
|
+
setCoagentStatesWithRef((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
499
|
+
[lastAgentStateMessage.agentName]: {
|
|
500
|
+
name: lastAgentStateMessage.agentName,
|
|
501
|
+
state: lastAgentStateMessage.state,
|
|
502
|
+
running: lastAgentStateMessage.running,
|
|
503
|
+
active: lastAgentStateMessage.active,
|
|
504
|
+
threadId: lastAgentStateMessage.threadId,
|
|
505
|
+
nodeName: lastAgentStateMessage.nodeName,
|
|
506
|
+
runId: lastAgentStateMessage.runId
|
|
507
|
+
}
|
|
508
|
+
}));
|
|
509
|
+
if (lastAgentStateMessage.running) {
|
|
510
|
+
setAgentSession({
|
|
511
|
+
threadId: lastAgentStateMessage.threadId,
|
|
512
|
+
agentName: lastAgentStateMessage.agentName,
|
|
513
|
+
nodeName: lastAgentStateMessage.nodeName
|
|
514
|
+
});
|
|
515
|
+
} else {
|
|
516
|
+
setAgentSession(null);
|
|
368
517
|
}
|
|
369
|
-
yield onCoAgentStateRender({
|
|
370
|
-
name: message.agentName,
|
|
371
|
-
nodeName: message.nodeName,
|
|
372
|
-
state: message.state
|
|
373
|
-
});
|
|
374
|
-
executedCoAgentStateRenders.push(message.id);
|
|
375
518
|
}
|
|
376
519
|
}
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
setCoagentStatesWithRef((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
380
|
-
[lastAgentStateMessage.agentName]: {
|
|
381
|
-
name: lastAgentStateMessage.agentName,
|
|
382
|
-
state: lastAgentStateMessage.state,
|
|
383
|
-
running: lastAgentStateMessage.running,
|
|
384
|
-
active: lastAgentStateMessage.active,
|
|
385
|
-
threadId: lastAgentStateMessage.threadId,
|
|
386
|
-
nodeName: lastAgentStateMessage.nodeName,
|
|
387
|
-
runId: lastAgentStateMessage.runId
|
|
388
|
-
}
|
|
389
|
-
}));
|
|
390
|
-
if (lastAgentStateMessage.running) {
|
|
391
|
-
setAgentSession({
|
|
392
|
-
threadId: lastAgentStateMessage.threadId,
|
|
393
|
-
agentName: lastAgentStateMessage.agentName,
|
|
394
|
-
nodeName: lastAgentStateMessage.nodeName
|
|
395
|
-
});
|
|
396
|
-
} else {
|
|
397
|
-
setAgentSession(null);
|
|
398
|
-
}
|
|
520
|
+
if (newMessages.length > 0) {
|
|
521
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
399
522
|
}
|
|
400
523
|
}
|
|
401
|
-
if (
|
|
402
|
-
|
|
524
|
+
if (
|
|
525
|
+
// if followUp is not explicitly false
|
|
526
|
+
followUp !== false && // if we have client side results
|
|
527
|
+
(Object.values(actionResults).length || // or the last message we received is a result
|
|
528
|
+
newMessages.length && newMessages[newMessages.length - 1].isResultMessage())
|
|
529
|
+
) {
|
|
530
|
+
yield new Promise((resolve) => setTimeout(resolve, 10));
|
|
531
|
+
return yield runChatCompletionRef.current([...previousMessages, ...newMessages]);
|
|
532
|
+
} else {
|
|
533
|
+
return newMessages.slice();
|
|
403
534
|
}
|
|
535
|
+
} finally {
|
|
536
|
+
setIsLoading(false);
|
|
404
537
|
}
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
538
|
+
}),
|
|
539
|
+
[
|
|
540
|
+
messages,
|
|
541
|
+
setMessages,
|
|
542
|
+
makeSystemMessageCallback,
|
|
543
|
+
copilotConfig,
|
|
544
|
+
setIsLoading,
|
|
545
|
+
initialMessages,
|
|
546
|
+
isLoading,
|
|
547
|
+
actions,
|
|
548
|
+
onFunctionCall,
|
|
549
|
+
onCoAgentStateRender,
|
|
550
|
+
setCoagentStatesWithRef,
|
|
551
|
+
coagentStatesRef,
|
|
552
|
+
agentSession,
|
|
553
|
+
setAgentSession
|
|
554
|
+
]
|
|
555
|
+
);
|
|
420
556
|
runChatCompletionRef.current = runChatCompletion;
|
|
421
|
-
const runChatCompletionAndHandleFunctionCall = (
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
557
|
+
const runChatCompletionAndHandleFunctionCall = useAsyncCallback(
|
|
558
|
+
(messages2) => __async(this, null, function* () {
|
|
559
|
+
yield runChatCompletionRef.current(messages2);
|
|
560
|
+
}),
|
|
561
|
+
[messages]
|
|
562
|
+
);
|
|
563
|
+
const append = useAsyncCallback(
|
|
564
|
+
(message) => __async(this, null, function* () {
|
|
565
|
+
if (isLoading) {
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
const newMessages = [...messages, message];
|
|
569
|
+
setMessages(newMessages);
|
|
570
|
+
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
571
|
+
}),
|
|
572
|
+
[isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]
|
|
573
|
+
);
|
|
574
|
+
const reload = useAsyncCallback(() => __async(this, null, function* () {
|
|
433
575
|
if (isLoading || messages.length === 0) {
|
|
434
576
|
return;
|
|
435
577
|
}
|
|
@@ -440,7 +582,7 @@ function useChat(options) {
|
|
|
440
582
|
}
|
|
441
583
|
setMessages(newMessages);
|
|
442
584
|
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
443
|
-
});
|
|
585
|
+
}), [isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]);
|
|
444
586
|
const stop = () => {
|
|
445
587
|
var _a;
|
|
446
588
|
(_a = abortControllerRef.current) == null ? void 0 : _a.abort();
|
|
@@ -454,19 +596,19 @@ function useChat(options) {
|
|
|
454
596
|
}
|
|
455
597
|
|
|
456
598
|
// src/components/copilot-provider/copilotkit.tsx
|
|
457
|
-
var
|
|
599
|
+
var import_react7 = require("react");
|
|
458
600
|
var import_react_dom = require("react-dom");
|
|
459
601
|
var import_shared2 = require("@copilotkit/shared");
|
|
460
602
|
|
|
461
603
|
// src/context/copilot-messages-context.tsx
|
|
462
|
-
var
|
|
604
|
+
var import_react6 = __toESM(require("react"));
|
|
463
605
|
var emptyCopilotContext2 = {
|
|
464
606
|
messages: [],
|
|
465
607
|
setMessages: () => []
|
|
466
608
|
};
|
|
467
|
-
var CopilotMessagesContext =
|
|
609
|
+
var CopilotMessagesContext = import_react6.default.createContext(emptyCopilotContext2);
|
|
468
610
|
function useCopilotMessagesContext() {
|
|
469
|
-
const context =
|
|
611
|
+
const context = import_react6.default.useContext(CopilotMessagesContext);
|
|
470
612
|
if (context === emptyCopilotContext2) {
|
|
471
613
|
throw new Error(
|
|
472
614
|
"A messages consuming component was not wrapped with `<CopilotMessages> {...} </CopilotMessages>`"
|
|
@@ -476,7 +618,7 @@ function useCopilotMessagesContext() {
|
|
|
476
618
|
}
|
|
477
619
|
|
|
478
620
|
// src/components/copilot-provider/copilotkit.tsx
|
|
479
|
-
var
|
|
621
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
480
622
|
var defaultCopilotContextCategories = ["global"];
|
|
481
623
|
|
|
482
624
|
// src/hooks/use-copilot-chat.ts
|
|
@@ -498,18 +640,17 @@ function useCopilotChat(_a = {}) {
|
|
|
498
640
|
setCoagentStatesWithRef,
|
|
499
641
|
coAgentStateRenders,
|
|
500
642
|
agentSession,
|
|
501
|
-
setAgentSession
|
|
502
|
-
forwardedParameters
|
|
643
|
+
setAgentSession
|
|
503
644
|
} = useCopilotContext();
|
|
504
645
|
const { messages, setMessages } = useCopilotMessagesContext();
|
|
505
646
|
const latestGetContextString = useUpdatedRef(getContextString);
|
|
506
|
-
const deleteMessage = (0,
|
|
647
|
+
const deleteMessage = (0, import_react8.useCallback)(
|
|
507
648
|
(messageId) => {
|
|
508
649
|
setMessages((prev) => prev.filter((message) => message.id !== messageId));
|
|
509
650
|
},
|
|
510
651
|
[setMessages]
|
|
511
652
|
);
|
|
512
|
-
const makeSystemMessageCallback = (0,
|
|
653
|
+
const makeSystemMessageCallback = (0, import_react8.useCallback)(() => {
|
|
513
654
|
const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
|
|
514
655
|
const contextString = latestGetContextString.current([], defaultCopilotContextCategories);
|
|
515
656
|
return new import_runtime_client_gql3.TextMessage({
|
|
@@ -517,7 +658,7 @@ function useCopilotChat(_a = {}) {
|
|
|
517
658
|
role: import_runtime_client_gql3.Role.System
|
|
518
659
|
});
|
|
519
660
|
}, [getContextString, makeSystemMessage, chatInstructions]);
|
|
520
|
-
const onCoAgentStateRender = (
|
|
661
|
+
const onCoAgentStateRender = useAsyncCallback(
|
|
521
662
|
(args) => __async(this, null, function* () {
|
|
522
663
|
var _a2;
|
|
523
664
|
const { name, nodeName, state } = args;
|
|
@@ -549,42 +690,41 @@ function useCopilotChat(_a = {}) {
|
|
|
549
690
|
coagentStatesRef,
|
|
550
691
|
setCoagentStatesWithRef,
|
|
551
692
|
agentSession,
|
|
552
|
-
setAgentSession
|
|
553
|
-
forwardedParameters
|
|
693
|
+
setAgentSession
|
|
554
694
|
}));
|
|
555
695
|
const latestAppend = useUpdatedRef(append);
|
|
556
|
-
const latestAppendFunc = (
|
|
557
|
-
(message) => {
|
|
558
|
-
return latestAppend.current(message);
|
|
559
|
-
},
|
|
696
|
+
const latestAppendFunc = useAsyncCallback(
|
|
697
|
+
(message) => __async(this, null, function* () {
|
|
698
|
+
return yield latestAppend.current(message);
|
|
699
|
+
}),
|
|
560
700
|
[latestAppend]
|
|
561
701
|
);
|
|
562
702
|
const latestReload = useUpdatedRef(reload);
|
|
563
|
-
const latestReloadFunc = (
|
|
564
|
-
return latestReload.current();
|
|
565
|
-
}, [latestReload]);
|
|
703
|
+
const latestReloadFunc = useAsyncCallback(() => __async(this, null, function* () {
|
|
704
|
+
return yield latestReload.current();
|
|
705
|
+
}), [latestReload]);
|
|
566
706
|
const latestStop = useUpdatedRef(stop);
|
|
567
|
-
const latestStopFunc = (0,
|
|
707
|
+
const latestStopFunc = (0, import_react8.useCallback)(() => {
|
|
568
708
|
return latestStop.current();
|
|
569
709
|
}, [latestStop]);
|
|
570
710
|
const latestDelete = useUpdatedRef(deleteMessage);
|
|
571
|
-
const latestDeleteFunc = (0,
|
|
711
|
+
const latestDeleteFunc = (0, import_react8.useCallback)(
|
|
572
712
|
(messageId) => {
|
|
573
713
|
return latestDelete.current(messageId);
|
|
574
714
|
},
|
|
575
715
|
[latestDelete]
|
|
576
716
|
);
|
|
577
717
|
const latestSetMessages = useUpdatedRef(setMessages);
|
|
578
|
-
const latestSetMessagesFunc = (0,
|
|
718
|
+
const latestSetMessagesFunc = (0, import_react8.useCallback)(
|
|
579
719
|
(messages2) => {
|
|
580
720
|
return latestSetMessages.current(messages2);
|
|
581
721
|
},
|
|
582
722
|
[latestSetMessages]
|
|
583
723
|
);
|
|
584
724
|
const latestRunChatCompletion = useUpdatedRef(runChatCompletion);
|
|
585
|
-
const latestRunChatCompletionFunc = (
|
|
586
|
-
return latestRunChatCompletion.current();
|
|
587
|
-
}, [latestRunChatCompletion]);
|
|
725
|
+
const latestRunChatCompletionFunc = useAsyncCallback(() => __async(this, null, function* () {
|
|
726
|
+
return yield latestRunChatCompletion.current();
|
|
727
|
+
}), [latestRunChatCompletion]);
|
|
588
728
|
return {
|
|
589
729
|
visibleMessages: messages,
|
|
590
730
|
appendMessage: latestAppendFunc,
|
|
@@ -597,8 +737,8 @@ function useCopilotChat(_a = {}) {
|
|
|
597
737
|
};
|
|
598
738
|
}
|
|
599
739
|
function useUpdatedRef(value) {
|
|
600
|
-
const ref = (0,
|
|
601
|
-
(0,
|
|
740
|
+
const ref = (0, import_react8.useRef)(value);
|
|
741
|
+
(0, import_react8.useEffect)(() => {
|
|
602
742
|
ref.current = value;
|
|
603
743
|
}, [value]);
|
|
604
744
|
return ref;
|