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