@copilotkit/react-core 1.4.8-no-pino-redact.1 → 1.4.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -3
- package/dist/{chunk-XBVKTDXP.mjs → chunk-6EMLM6WX.mjs} +17 -14
- package/dist/chunk-6EMLM6WX.mjs.map +1 -0
- package/dist/{chunk-ODN4H66E.mjs → chunk-7LRDVJH5.mjs} +6 -2
- package/dist/chunk-7LRDVJH5.mjs.map +1 -0
- package/dist/{chunk-O22KGHOQ.mjs → chunk-FSC4A3JN.mjs} +17 -13
- package/dist/chunk-FSC4A3JN.mjs.map +1 -0
- package/dist/{chunk-OT67R4NB.mjs → chunk-IFTHM7LF.mjs} +2 -2
- package/dist/{chunk-DHGXL5PC.mjs → chunk-IVYL7JRC.mjs} +7 -4
- package/dist/chunk-IVYL7JRC.mjs.map +1 -0
- package/dist/chunk-NTLCOVE5.mjs +321 -0
- package/dist/chunk-NTLCOVE5.mjs.map +1 -0
- package/dist/chunk-QCUP6HLK.mjs +37 -0
- package/dist/chunk-QCUP6HLK.mjs.map +1 -0
- package/dist/{chunk-2KCEHGSI.mjs → chunk-SFPANIOY.mjs} +99 -49
- package/dist/chunk-SFPANIOY.mjs.map +1 -0
- package/dist/{chunk-JHEAUB3Z.mjs → chunk-X6ZF5WAX.mjs} +1 -1
- package/dist/{chunk-JHEAUB3Z.mjs.map → chunk-X6ZF5WAX.mjs.map} +1 -1
- package/dist/{chunk-Y7MI4PBB.mjs → chunk-YUY5ZAST.mjs} +6 -3
- package/dist/chunk-YUY5ZAST.mjs.map +1 -0
- package/dist/components/copilot-provider/copilotkit.js +156 -91
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +4 -3
- package/dist/components/copilot-provider/index.js +156 -91
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +4 -3
- package/dist/components/error-boundary/error-boundary.d.ts +22 -0
- package/dist/components/error-boundary/error-boundary.js +183 -0
- package/dist/components/error-boundary/error-boundary.js.map +1 -0
- package/dist/components/error-boundary/error-boundary.mjs +12 -0
- package/dist/components/error-boundary/error-boundary.mjs.map +1 -0
- package/dist/components/error-boundary/error-utils.d.ts +11 -0
- package/dist/components/error-boundary/error-utils.js +177 -0
- package/dist/components/error-boundary/error-utils.js.map +1 -0
- package/dist/components/error-boundary/error-utils.mjs +13 -0
- package/dist/components/error-boundary/error-utils.mjs.map +1 -0
- package/dist/components/index.js +156 -91
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +4 -3
- package/dist/components/toast/toast-provider.d.ts +2 -1
- package/dist/components/toast/toast-provider.js +76 -62
- package/dist/components/toast/toast-provider.js.map +1 -1
- package/dist/components/toast/toast-provider.mjs +1 -1
- package/dist/hooks/index.js +398 -253
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +9 -8
- package/dist/hooks/use-chat.js +343 -199
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +3 -3
- package/dist/hooks/use-coagent-state-render.d.ts +2 -2
- package/dist/hooks/use-coagent-state-render.js.map +1 -1
- package/dist/hooks/use-coagent-state-render.mjs +1 -1
- package/dist/hooks/use-coagent.js +378 -233
- package/dist/hooks/use-coagent.js.map +1 -1
- package/dist/hooks/use-coagent.mjs +7 -6
- package/dist/hooks/use-copilot-action.js +132 -7
- package/dist/hooks/use-copilot-action.js.map +1 -1
- package/dist/hooks/use-copilot-action.mjs +3 -1
- package/dist/hooks/use-copilot-chat.js +367 -223
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +6 -5
- package/dist/hooks/use-copilot-runtime-client.js +110 -4
- package/dist/hooks/use-copilot-runtime-client.js.map +1 -1
- package/dist/hooks/use-copilot-runtime-client.mjs +2 -2
- package/dist/index.js +439 -335
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +10 -9
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +5 -4
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +5 -4
- package/dist/utils/extract.js.map +1 -1
- package/dist/utils/extract.mjs +4 -3
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +4 -3
- package/package.json +3 -3
- package/src/components/copilot-provider/copilotkit.tsx +4 -1
- package/src/components/error-boundary/error-boundary.tsx +42 -0
- package/src/components/error-boundary/error-utils.tsx +95 -0
- package/src/components/toast/toast-provider.tsx +10 -49
- package/src/hooks/use-chat.ts +286 -259
- package/src/hooks/use-coagent-state-render.ts +2 -2
- package/src/hooks/use-coagent.ts +14 -11
- package/src/hooks/use-copilot-action.ts +3 -2
- package/src/hooks/use-copilot-chat.ts +9 -8
- package/src/hooks/use-copilot-runtime-client.ts +4 -0
- package/dist/chunk-2KCEHGSI.mjs.map +0 -1
- package/dist/chunk-DHGXL5PC.mjs.map +0 -1
- package/dist/chunk-EUIBVFV6.mjs +0 -294
- package/dist/chunk-EUIBVFV6.mjs.map +0 -1
- package/dist/chunk-O22KGHOQ.mjs.map +0 -1
- package/dist/chunk-ODN4H66E.mjs.map +0 -1
- package/dist/chunk-XBVKTDXP.mjs.map +0 -1
- package/dist/chunk-Y7MI4PBB.mjs.map +0 -1
- /package/dist/{chunk-OT67R4NB.mjs.map → chunk-IFTHM7LF.mjs.map} +0 -0
package/dist/hooks/index.js
CHANGED
|
@@ -90,7 +90,7 @@ __export(hooks_exports, {
|
|
|
90
90
|
module.exports = __toCommonJS(hooks_exports);
|
|
91
91
|
|
|
92
92
|
// src/hooks/use-copilot-chat.ts
|
|
93
|
-
var
|
|
93
|
+
var import_react8 = require("react");
|
|
94
94
|
|
|
95
95
|
// src/context/copilot-context.tsx
|
|
96
96
|
var import_react = __toESM(require("react"));
|
|
@@ -165,16 +165,131 @@ function returnAndThrowInDebug(value) {
|
|
|
165
165
|
var import_runtime_client_gql3 = require("@copilotkit/runtime-client-gql");
|
|
166
166
|
|
|
167
167
|
// src/hooks/use-chat.ts
|
|
168
|
-
var
|
|
168
|
+
var import_react5 = require("react");
|
|
169
169
|
var import_shared = require("@copilotkit/shared");
|
|
170
170
|
var import_runtime_client_gql2 = require("@copilotkit/runtime-client-gql");
|
|
171
171
|
|
|
172
172
|
// src/components/toast/toast-provider.tsx
|
|
173
|
+
var import_react3 = require("react");
|
|
174
|
+
|
|
175
|
+
// src/components/error-boundary/error-utils.tsx
|
|
173
176
|
var import_react2 = require("react");
|
|
177
|
+
|
|
178
|
+
// src/components/toast/exclamation-mark-icon.tsx
|
|
174
179
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
175
|
-
var
|
|
180
|
+
var ExclamationMarkIcon = ({
|
|
181
|
+
className,
|
|
182
|
+
style
|
|
183
|
+
}) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
|
|
184
|
+
"svg",
|
|
185
|
+
{
|
|
186
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
187
|
+
width: "24",
|
|
188
|
+
height: "24",
|
|
189
|
+
viewBox: "0 0 24 24",
|
|
190
|
+
fill: "none",
|
|
191
|
+
stroke: "currentColor",
|
|
192
|
+
strokeWidth: "2",
|
|
193
|
+
strokeLinecap: "round",
|
|
194
|
+
strokeLinejoin: "round",
|
|
195
|
+
className: `lucide lucide-circle-alert ${className ? className : ""}`,
|
|
196
|
+
style,
|
|
197
|
+
children: [
|
|
198
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("circle", { cx: "12", cy: "12", r: "10" }),
|
|
199
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("line", { x1: "12", x2: "12", y1: "8", y2: "12" }),
|
|
200
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("line", { x1: "12", x2: "12.01", y1: "16", y2: "16" })
|
|
201
|
+
]
|
|
202
|
+
}
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
// src/components/error-boundary/error-utils.tsx
|
|
206
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
207
|
+
function ErrorToast({ errors }) {
|
|
208
|
+
const errorsToRender = errors.map((error, idx) => {
|
|
209
|
+
var _a, _b, _c;
|
|
210
|
+
const message = "extensions" in error ? (_b = (_a = error.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message : error.message;
|
|
211
|
+
const code = "extensions" in error ? (_c = error.extensions) == null ? void 0 : _c.code : null;
|
|
212
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
213
|
+
"div",
|
|
214
|
+
{
|
|
215
|
+
style: {
|
|
216
|
+
marginTop: idx === 0 ? 0 : 10,
|
|
217
|
+
marginBottom: 14
|
|
218
|
+
},
|
|
219
|
+
children: [
|
|
220
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ExclamationMarkIcon, { style: { marginBottom: 4 } }),
|
|
221
|
+
code && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
222
|
+
"div",
|
|
223
|
+
{
|
|
224
|
+
style: {
|
|
225
|
+
fontWeight: "600",
|
|
226
|
+
marginBottom: 4
|
|
227
|
+
},
|
|
228
|
+
children: [
|
|
229
|
+
"Copilot Cloud Error:",
|
|
230
|
+
" ",
|
|
231
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { style: { fontFamily: "monospace", fontWeight: "normal" }, children: code })
|
|
232
|
+
]
|
|
233
|
+
}
|
|
234
|
+
),
|
|
235
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { children: message })
|
|
236
|
+
]
|
|
237
|
+
},
|
|
238
|
+
idx
|
|
239
|
+
);
|
|
240
|
+
});
|
|
241
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
242
|
+
"div",
|
|
243
|
+
{
|
|
244
|
+
style: {
|
|
245
|
+
fontSize: "13px",
|
|
246
|
+
maxWidth: "600px"
|
|
247
|
+
},
|
|
248
|
+
children: [
|
|
249
|
+
errorsToRender,
|
|
250
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { fontSize: "11px", opacity: 0.75 }, children: "NOTE: This error only displays during local development." })
|
|
251
|
+
]
|
|
252
|
+
}
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
function useErrorToast() {
|
|
256
|
+
const { addToast } = useToast();
|
|
257
|
+
return (0, import_react2.useCallback)(
|
|
258
|
+
(error) => {
|
|
259
|
+
const errorId = error.map((err) => {
|
|
260
|
+
var _a, _b;
|
|
261
|
+
const message = "extensions" in err ? ((_b = (_a = err.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message) || err.message : err.message;
|
|
262
|
+
const stack = err.stack || "";
|
|
263
|
+
return btoa(message + stack).slice(0, 32);
|
|
264
|
+
}).join("|");
|
|
265
|
+
addToast({
|
|
266
|
+
type: "error",
|
|
267
|
+
id: errorId,
|
|
268
|
+
// Toast libraries typically dedupe by id
|
|
269
|
+
message: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ErrorToast, { errors: error })
|
|
270
|
+
});
|
|
271
|
+
},
|
|
272
|
+
[addToast]
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
function useAsyncCallback(callback, deps) {
|
|
276
|
+
const addErrorToast = useErrorToast();
|
|
277
|
+
return (0, import_react2.useCallback)((...args) => __async(this, null, function* () {
|
|
278
|
+
try {
|
|
279
|
+
return yield callback(...args);
|
|
280
|
+
} catch (error) {
|
|
281
|
+
console.error("Error in async callback:", error);
|
|
282
|
+
addErrorToast([error]);
|
|
283
|
+
throw error;
|
|
284
|
+
}
|
|
285
|
+
}), deps);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// src/components/toast/toast-provider.tsx
|
|
289
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
290
|
+
var ToastContext = (0, import_react3.createContext)(void 0);
|
|
176
291
|
function useToast() {
|
|
177
|
-
const context = (0,
|
|
292
|
+
const context = (0, import_react3.useContext)(ToastContext);
|
|
178
293
|
if (!context) {
|
|
179
294
|
throw new Error("useToast must be used within a ToastProvider");
|
|
180
295
|
}
|
|
@@ -183,14 +298,17 @@ function useToast() {
|
|
|
183
298
|
|
|
184
299
|
// src/hooks/use-copilot-runtime-client.ts
|
|
185
300
|
var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
|
|
186
|
-
var
|
|
301
|
+
var import_react4 = require("react");
|
|
187
302
|
var useCopilotRuntimeClient = (options) => {
|
|
188
303
|
const { addGraphQLErrorsToast } = useToast();
|
|
189
|
-
const
|
|
304
|
+
const addErrorToast = useErrorToast();
|
|
305
|
+
const runtimeClient = (0, import_react4.useMemo)(() => {
|
|
190
306
|
return new import_runtime_client_gql.CopilotRuntimeClient(__spreadProps(__spreadValues({}, options), {
|
|
191
307
|
handleGQLErrors: (error) => {
|
|
192
308
|
if (error.graphQLErrors.length) {
|
|
193
309
|
addGraphQLErrorsToast(error.graphQLErrors);
|
|
310
|
+
} else {
|
|
311
|
+
addErrorToast([error]);
|
|
194
312
|
}
|
|
195
313
|
}
|
|
196
314
|
}));
|
|
@@ -216,12 +334,12 @@ function useChat(options) {
|
|
|
216
334
|
agentSession,
|
|
217
335
|
setAgentSession
|
|
218
336
|
} = options;
|
|
219
|
-
const abortControllerRef = (0,
|
|
220
|
-
const threadIdRef = (0,
|
|
221
|
-
const runIdRef = (0,
|
|
337
|
+
const abortControllerRef = (0, import_react5.useRef)();
|
|
338
|
+
const threadIdRef = (0, import_react5.useRef)(null);
|
|
339
|
+
const runIdRef = (0, import_react5.useRef)(null);
|
|
222
340
|
const { addGraphQLErrorsToast } = useToast();
|
|
223
|
-
const runChatCompletionRef = (0,
|
|
224
|
-
const agentSessionRef = (0,
|
|
341
|
+
const runChatCompletionRef = (0, import_react5.useRef)();
|
|
342
|
+
const agentSessionRef = (0, import_react5.useRef)(agentSession);
|
|
225
343
|
agentSessionRef.current = agentSession;
|
|
226
344
|
const publicApiKey = copilotConfig.publicApiKey;
|
|
227
345
|
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [import_shared.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
|
|
@@ -231,210 +349,236 @@ function useChat(options) {
|
|
|
231
349
|
headers,
|
|
232
350
|
credentials: copilotConfig.credentials
|
|
233
351
|
});
|
|
234
|
-
const runChatCompletion = (
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
runtimeClient.
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
available
|
|
260
|
-
|
|
261
|
-
available
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
352
|
+
const runChatCompletion = useAsyncCallback(
|
|
353
|
+
(previousMessages) => __async(this, null, function* () {
|
|
354
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
355
|
+
setIsLoading(true);
|
|
356
|
+
let newMessages = [
|
|
357
|
+
new import_runtime_client_gql2.TextMessage({
|
|
358
|
+
content: "",
|
|
359
|
+
role: import_runtime_client_gql2.Role.Assistant
|
|
360
|
+
})
|
|
361
|
+
];
|
|
362
|
+
const abortController = new AbortController();
|
|
363
|
+
abortControllerRef.current = abortController;
|
|
364
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
365
|
+
const systemMessage = makeSystemMessageCallback();
|
|
366
|
+
const messagesWithContext = [systemMessage, ...initialMessages || [], ...previousMessages];
|
|
367
|
+
const stream = runtimeClient.asStream(
|
|
368
|
+
runtimeClient.generateCopilotResponse({
|
|
369
|
+
data: __spreadProps(__spreadValues(__spreadProps(__spreadValues({
|
|
370
|
+
frontend: {
|
|
371
|
+
actions: actions.filter(
|
|
372
|
+
(action) => action.available !== import_runtime_client_gql2.ActionInputAvailability.Disabled || !action.disabled
|
|
373
|
+
).map((action) => {
|
|
374
|
+
let available = import_runtime_client_gql2.ActionInputAvailability.Enabled;
|
|
375
|
+
if (action.disabled) {
|
|
376
|
+
available = import_runtime_client_gql2.ActionInputAvailability.Disabled;
|
|
377
|
+
} else if (action.available === "disabled") {
|
|
378
|
+
available = import_runtime_client_gql2.ActionInputAvailability.Disabled;
|
|
379
|
+
} else if (action.available === "remote") {
|
|
380
|
+
available = import_runtime_client_gql2.ActionInputAvailability.Remote;
|
|
381
|
+
}
|
|
382
|
+
return {
|
|
383
|
+
name: action.name,
|
|
384
|
+
description: action.description || "",
|
|
385
|
+
jsonSchema: JSON.stringify(
|
|
386
|
+
(0, import_shared.actionParametersToJsonSchema)(action.parameters || [])
|
|
387
|
+
),
|
|
388
|
+
available
|
|
389
|
+
};
|
|
390
|
+
}),
|
|
391
|
+
url: window.location.href
|
|
392
|
+
},
|
|
393
|
+
threadId: threadIdRef.current,
|
|
394
|
+
runId: runIdRef.current,
|
|
395
|
+
messages: (0, import_runtime_client_gql2.convertMessagesToGqlInput)((0, import_runtime_client_gql2.filterAgentStateMessages)(messagesWithContext))
|
|
396
|
+
}, copilotConfig.cloud ? {
|
|
397
|
+
cloud: __spreadValues({}, ((_c = (_b = (_a = copilotConfig.cloud.guardrails) == null ? void 0 : _a.input) == null ? void 0 : _b.restrictToTopic) == null ? void 0 : _c.enabled) ? {
|
|
398
|
+
guardrails: {
|
|
399
|
+
inputValidationRules: {
|
|
400
|
+
allowList: copilotConfig.cloud.guardrails.input.restrictToTopic.validTopics,
|
|
401
|
+
denyList: copilotConfig.cloud.guardrails.input.restrictToTopic.invalidTopics
|
|
402
|
+
}
|
|
281
403
|
}
|
|
404
|
+
} : {})
|
|
405
|
+
} : {}), {
|
|
406
|
+
metadata: {
|
|
407
|
+
requestType: import_runtime_client_gql2.CopilotRequestType.Chat
|
|
282
408
|
}
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
409
|
+
}), agentSessionRef.current ? {
|
|
410
|
+
agentSession: agentSessionRef.current
|
|
411
|
+
} : {}), {
|
|
412
|
+
agentStates: Object.values(coagentStatesRef.current).map((state) => ({
|
|
413
|
+
agentName: state.name,
|
|
414
|
+
state: JSON.stringify(state.state)
|
|
415
|
+
}))
|
|
416
|
+
}),
|
|
417
|
+
properties: copilotConfig.properties,
|
|
418
|
+
signal: (_d = abortControllerRef.current) == null ? void 0 : _d.signal
|
|
419
|
+
})
|
|
420
|
+
);
|
|
421
|
+
const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
|
|
422
|
+
const reader = stream.getReader();
|
|
423
|
+
let actionResults = {};
|
|
424
|
+
let executedCoAgentStateRenders = [];
|
|
425
|
+
let followUp = void 0;
|
|
426
|
+
try {
|
|
427
|
+
while (true) {
|
|
428
|
+
let done, value;
|
|
429
|
+
try {
|
|
430
|
+
const readResult = yield reader.read();
|
|
431
|
+
done = readResult.done;
|
|
432
|
+
value = readResult.value;
|
|
433
|
+
} catch (readError) {
|
|
434
|
+
break;
|
|
287
435
|
}
|
|
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
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
436
|
+
if (done) {
|
|
437
|
+
break;
|
|
438
|
+
}
|
|
439
|
+
if (!(value == null ? void 0 : value.generateCopilotResponse)) {
|
|
440
|
+
continue;
|
|
441
|
+
}
|
|
442
|
+
threadIdRef.current = value.generateCopilotResponse.threadId || null;
|
|
443
|
+
runIdRef.current = value.generateCopilotResponse.runId || null;
|
|
444
|
+
const messages2 = (0, import_runtime_client_gql2.convertGqlOutputToMessages)(
|
|
445
|
+
(0, import_runtime_client_gql2.filterAdjacentAgentStateMessages)(value.generateCopilotResponse.messages)
|
|
446
|
+
);
|
|
447
|
+
if (messages2.length === 0) {
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
newMessages = [];
|
|
451
|
+
if (((_h = value.generateCopilotResponse.status) == null ? void 0 : _h.__typename) === "FailedResponseStatus" && value.generateCopilotResponse.status.reason === "GUARDRAILS_VALIDATION_FAILED") {
|
|
452
|
+
newMessages = [
|
|
453
|
+
new import_runtime_client_gql2.TextMessage({
|
|
454
|
+
role: import_runtime_client_gql2.MessageRole.Assistant,
|
|
455
|
+
content: ((_i = value.generateCopilotResponse.status.details) == null ? void 0 : _i.guardrailsReason) || ""
|
|
456
|
+
})
|
|
457
|
+
];
|
|
458
|
+
} else {
|
|
459
|
+
for (const message of messages2) {
|
|
460
|
+
newMessages.push(message);
|
|
461
|
+
if (message.isActionExecutionMessage() && message.status.code !== import_runtime_client_gql2.MessageStatusCode.Pending && message.scope === "client" && onFunctionCall) {
|
|
462
|
+
if (!(message.id in actionResults)) {
|
|
463
|
+
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
464
|
+
break;
|
|
465
|
+
}
|
|
466
|
+
try {
|
|
467
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
468
|
+
const action = actions.find((action2) => action2.name === message.name);
|
|
469
|
+
if (action) {
|
|
470
|
+
followUp = action.followUp;
|
|
471
|
+
}
|
|
472
|
+
const result = yield onFunctionCall({
|
|
473
|
+
messages: previousMessages,
|
|
474
|
+
name: message.name,
|
|
475
|
+
args: message.arguments
|
|
476
|
+
});
|
|
477
|
+
actionResults[message.id] = result;
|
|
478
|
+
} catch (e) {
|
|
479
|
+
actionResults[message.id] = `Failed to execute action ${message.name}`;
|
|
480
|
+
console.error(`Failed to execute action ${message.name}: ${e}`);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
newMessages.push(
|
|
484
|
+
new import_runtime_client_gql2.ResultMessage({
|
|
485
|
+
result: import_runtime_client_gql2.ResultMessage.encodeResult(actionResults[message.id]),
|
|
486
|
+
actionExecutionId: message.id,
|
|
487
|
+
actionName: message.name
|
|
488
|
+
})
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
if (message.isAgentStateMessage() && !message.active && !executedCoAgentStateRenders.includes(message.id) && onCoAgentStateRender) {
|
|
342
492
|
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
343
493
|
break;
|
|
344
494
|
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
const result = yield onFunctionCall({
|
|
352
|
-
messages: previousMessages,
|
|
353
|
-
name: message.name,
|
|
354
|
-
args: message.arguments
|
|
355
|
-
});
|
|
356
|
-
actionResults[message.id] = result;
|
|
357
|
-
} catch (e) {
|
|
358
|
-
actionResults[message.id] = `Failed to execute action ${message.name}`;
|
|
359
|
-
console.error(`Failed to execute action ${message.name}: ${e}`);
|
|
360
|
-
}
|
|
495
|
+
yield onCoAgentStateRender({
|
|
496
|
+
name: message.agentName,
|
|
497
|
+
nodeName: message.nodeName,
|
|
498
|
+
state: message.state
|
|
499
|
+
});
|
|
500
|
+
executedCoAgentStateRenders.push(message.id);
|
|
361
501
|
}
|
|
362
|
-
newMessages.push(
|
|
363
|
-
new import_runtime_client_gql2.ResultMessage({
|
|
364
|
-
result: import_runtime_client_gql2.ResultMessage.encodeResult(actionResults[message.id]),
|
|
365
|
-
actionExecutionId: message.id,
|
|
366
|
-
actionName: message.name
|
|
367
|
-
})
|
|
368
|
-
);
|
|
369
502
|
}
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
503
|
+
const lastAgentStateMessage = [...messages2].reverse().find((message) => message.isAgentStateMessage());
|
|
504
|
+
if (lastAgentStateMessage) {
|
|
505
|
+
setCoagentStatesWithRef((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
506
|
+
[lastAgentStateMessage.agentName]: {
|
|
507
|
+
name: lastAgentStateMessage.agentName,
|
|
508
|
+
state: lastAgentStateMessage.state,
|
|
509
|
+
running: lastAgentStateMessage.running,
|
|
510
|
+
active: lastAgentStateMessage.active,
|
|
511
|
+
threadId: lastAgentStateMessage.threadId,
|
|
512
|
+
nodeName: lastAgentStateMessage.nodeName,
|
|
513
|
+
runId: lastAgentStateMessage.runId
|
|
514
|
+
}
|
|
515
|
+
}));
|
|
516
|
+
if (lastAgentStateMessage.running) {
|
|
517
|
+
setAgentSession({
|
|
518
|
+
threadId: lastAgentStateMessage.threadId,
|
|
519
|
+
agentName: lastAgentStateMessage.agentName,
|
|
520
|
+
nodeName: lastAgentStateMessage.nodeName
|
|
521
|
+
});
|
|
522
|
+
} else {
|
|
523
|
+
setAgentSession(null);
|
|
373
524
|
}
|
|
374
|
-
yield onCoAgentStateRender({
|
|
375
|
-
name: message.agentName,
|
|
376
|
-
nodeName: message.nodeName,
|
|
377
|
-
state: message.state
|
|
378
|
-
});
|
|
379
|
-
executedCoAgentStateRenders.push(message.id);
|
|
380
525
|
}
|
|
381
526
|
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
setCoagentStatesWithRef((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
385
|
-
[lastAgentStateMessage.agentName]: {
|
|
386
|
-
name: lastAgentStateMessage.agentName,
|
|
387
|
-
state: lastAgentStateMessage.state,
|
|
388
|
-
running: lastAgentStateMessage.running,
|
|
389
|
-
active: lastAgentStateMessage.active,
|
|
390
|
-
threadId: lastAgentStateMessage.threadId,
|
|
391
|
-
nodeName: lastAgentStateMessage.nodeName,
|
|
392
|
-
runId: lastAgentStateMessage.runId
|
|
393
|
-
}
|
|
394
|
-
}));
|
|
395
|
-
if (lastAgentStateMessage.running) {
|
|
396
|
-
setAgentSession({
|
|
397
|
-
threadId: lastAgentStateMessage.threadId,
|
|
398
|
-
agentName: lastAgentStateMessage.agentName,
|
|
399
|
-
nodeName: lastAgentStateMessage.nodeName
|
|
400
|
-
});
|
|
401
|
-
} else {
|
|
402
|
-
setAgentSession(null);
|
|
403
|
-
}
|
|
527
|
+
if (newMessages.length > 0) {
|
|
528
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
404
529
|
}
|
|
405
530
|
}
|
|
406
|
-
if (
|
|
407
|
-
|
|
531
|
+
if (
|
|
532
|
+
// if followUp is not explicitly false
|
|
533
|
+
followUp !== false && // if we have client side results
|
|
534
|
+
(Object.values(actionResults).length || // or the last message we received is a result
|
|
535
|
+
newMessages.length && newMessages[newMessages.length - 1].isResultMessage())
|
|
536
|
+
) {
|
|
537
|
+
yield new Promise((resolve) => setTimeout(resolve, 10));
|
|
538
|
+
return yield runChatCompletionRef.current([...previousMessages, ...newMessages]);
|
|
539
|
+
} else {
|
|
540
|
+
return newMessages.slice();
|
|
408
541
|
}
|
|
542
|
+
} finally {
|
|
543
|
+
setIsLoading(false);
|
|
409
544
|
}
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
545
|
+
}),
|
|
546
|
+
[
|
|
547
|
+
messages,
|
|
548
|
+
setMessages,
|
|
549
|
+
makeSystemMessageCallback,
|
|
550
|
+
copilotConfig,
|
|
551
|
+
setIsLoading,
|
|
552
|
+
initialMessages,
|
|
553
|
+
isLoading,
|
|
554
|
+
actions,
|
|
555
|
+
onFunctionCall,
|
|
556
|
+
onCoAgentStateRender,
|
|
557
|
+
setCoagentStatesWithRef,
|
|
558
|
+
coagentStatesRef,
|
|
559
|
+
agentSession,
|
|
560
|
+
setAgentSession
|
|
561
|
+
]
|
|
562
|
+
);
|
|
425
563
|
runChatCompletionRef.current = runChatCompletion;
|
|
426
|
-
const runChatCompletionAndHandleFunctionCall = (
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
564
|
+
const runChatCompletionAndHandleFunctionCall = useAsyncCallback(
|
|
565
|
+
(messages2) => __async(this, null, function* () {
|
|
566
|
+
yield runChatCompletionRef.current(messages2);
|
|
567
|
+
}),
|
|
568
|
+
[messages]
|
|
569
|
+
);
|
|
570
|
+
const append = useAsyncCallback(
|
|
571
|
+
(message) => __async(this, null, function* () {
|
|
572
|
+
if (isLoading) {
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
const newMessages = [...messages, message];
|
|
576
|
+
setMessages(newMessages);
|
|
577
|
+
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
578
|
+
}),
|
|
579
|
+
[isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]
|
|
580
|
+
);
|
|
581
|
+
const reload = useAsyncCallback(() => __async(this, null, function* () {
|
|
438
582
|
if (isLoading || messages.length === 0) {
|
|
439
583
|
return;
|
|
440
584
|
}
|
|
@@ -445,7 +589,7 @@ function useChat(options) {
|
|
|
445
589
|
}
|
|
446
590
|
setMessages(newMessages);
|
|
447
591
|
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
448
|
-
});
|
|
592
|
+
}), [isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]);
|
|
449
593
|
const stop = () => {
|
|
450
594
|
var _a;
|
|
451
595
|
(_a = abortControllerRef.current) == null ? void 0 : _a.abort();
|
|
@@ -459,19 +603,19 @@ function useChat(options) {
|
|
|
459
603
|
}
|
|
460
604
|
|
|
461
605
|
// src/components/copilot-provider/copilotkit.tsx
|
|
462
|
-
var
|
|
606
|
+
var import_react7 = require("react");
|
|
463
607
|
var import_react_dom = require("react-dom");
|
|
464
608
|
var import_shared2 = require("@copilotkit/shared");
|
|
465
609
|
|
|
466
610
|
// src/context/copilot-messages-context.tsx
|
|
467
|
-
var
|
|
611
|
+
var import_react6 = __toESM(require("react"));
|
|
468
612
|
var emptyCopilotContext2 = {
|
|
469
613
|
messages: [],
|
|
470
614
|
setMessages: () => []
|
|
471
615
|
};
|
|
472
|
-
var CopilotMessagesContext =
|
|
616
|
+
var CopilotMessagesContext = import_react6.default.createContext(emptyCopilotContext2);
|
|
473
617
|
function useCopilotMessagesContext() {
|
|
474
|
-
const context =
|
|
618
|
+
const context = import_react6.default.useContext(CopilotMessagesContext);
|
|
475
619
|
if (context === emptyCopilotContext2) {
|
|
476
620
|
throw new Error(
|
|
477
621
|
"A messages consuming component was not wrapped with `<CopilotMessages> {...} </CopilotMessages>`"
|
|
@@ -481,7 +625,7 @@ function useCopilotMessagesContext() {
|
|
|
481
625
|
}
|
|
482
626
|
|
|
483
627
|
// src/components/copilot-provider/copilotkit.tsx
|
|
484
|
-
var
|
|
628
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
485
629
|
var defaultCopilotContextCategories = ["global"];
|
|
486
630
|
|
|
487
631
|
// src/hooks/use-copilot-chat.ts
|
|
@@ -507,13 +651,13 @@ function useCopilotChat(_a = {}) {
|
|
|
507
651
|
} = useCopilotContext();
|
|
508
652
|
const { messages, setMessages } = useCopilotMessagesContext();
|
|
509
653
|
const latestGetContextString = useUpdatedRef(getContextString);
|
|
510
|
-
const deleteMessage = (0,
|
|
654
|
+
const deleteMessage = (0, import_react8.useCallback)(
|
|
511
655
|
(messageId) => {
|
|
512
656
|
setMessages((prev) => prev.filter((message) => message.id !== messageId));
|
|
513
657
|
},
|
|
514
658
|
[setMessages]
|
|
515
659
|
);
|
|
516
|
-
const makeSystemMessageCallback = (0,
|
|
660
|
+
const makeSystemMessageCallback = (0, import_react8.useCallback)(() => {
|
|
517
661
|
const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
|
|
518
662
|
const contextString = latestGetContextString.current([], defaultCopilotContextCategories);
|
|
519
663
|
return new import_runtime_client_gql3.TextMessage({
|
|
@@ -521,7 +665,7 @@ function useCopilotChat(_a = {}) {
|
|
|
521
665
|
role: import_runtime_client_gql3.Role.System
|
|
522
666
|
});
|
|
523
667
|
}, [getContextString, makeSystemMessage, chatInstructions]);
|
|
524
|
-
const onCoAgentStateRender = (
|
|
668
|
+
const onCoAgentStateRender = useAsyncCallback(
|
|
525
669
|
(args) => __async(this, null, function* () {
|
|
526
670
|
var _a2;
|
|
527
671
|
const { name, nodeName, state } = args;
|
|
@@ -556,38 +700,38 @@ function useCopilotChat(_a = {}) {
|
|
|
556
700
|
setAgentSession
|
|
557
701
|
}));
|
|
558
702
|
const latestAppend = useUpdatedRef(append);
|
|
559
|
-
const latestAppendFunc = (
|
|
560
|
-
(message) => {
|
|
561
|
-
return latestAppend.current(message);
|
|
562
|
-
},
|
|
703
|
+
const latestAppendFunc = useAsyncCallback(
|
|
704
|
+
(message) => __async(this, null, function* () {
|
|
705
|
+
return yield latestAppend.current(message);
|
|
706
|
+
}),
|
|
563
707
|
[latestAppend]
|
|
564
708
|
);
|
|
565
709
|
const latestReload = useUpdatedRef(reload);
|
|
566
|
-
const latestReloadFunc = (
|
|
567
|
-
return latestReload.current();
|
|
568
|
-
}, [latestReload]);
|
|
710
|
+
const latestReloadFunc = useAsyncCallback(() => __async(this, null, function* () {
|
|
711
|
+
return yield latestReload.current();
|
|
712
|
+
}), [latestReload]);
|
|
569
713
|
const latestStop = useUpdatedRef(stop);
|
|
570
|
-
const latestStopFunc = (0,
|
|
714
|
+
const latestStopFunc = (0, import_react8.useCallback)(() => {
|
|
571
715
|
return latestStop.current();
|
|
572
716
|
}, [latestStop]);
|
|
573
717
|
const latestDelete = useUpdatedRef(deleteMessage);
|
|
574
|
-
const latestDeleteFunc = (0,
|
|
718
|
+
const latestDeleteFunc = (0, import_react8.useCallback)(
|
|
575
719
|
(messageId) => {
|
|
576
720
|
return latestDelete.current(messageId);
|
|
577
721
|
},
|
|
578
722
|
[latestDelete]
|
|
579
723
|
);
|
|
580
724
|
const latestSetMessages = useUpdatedRef(setMessages);
|
|
581
|
-
const latestSetMessagesFunc = (0,
|
|
725
|
+
const latestSetMessagesFunc = (0, import_react8.useCallback)(
|
|
582
726
|
(messages2) => {
|
|
583
727
|
return latestSetMessages.current(messages2);
|
|
584
728
|
},
|
|
585
729
|
[latestSetMessages]
|
|
586
730
|
);
|
|
587
731
|
const latestRunChatCompletion = useUpdatedRef(runChatCompletion);
|
|
588
|
-
const latestRunChatCompletionFunc = (
|
|
589
|
-
return latestRunChatCompletion.current();
|
|
590
|
-
}, [latestRunChatCompletion]);
|
|
732
|
+
const latestRunChatCompletionFunc = useAsyncCallback(() => __async(this, null, function* () {
|
|
733
|
+
return yield latestRunChatCompletion.current();
|
|
734
|
+
}), [latestRunChatCompletion]);
|
|
591
735
|
return {
|
|
592
736
|
visibleMessages: messages,
|
|
593
737
|
appendMessage: latestAppendFunc,
|
|
@@ -600,8 +744,8 @@ function useCopilotChat(_a = {}) {
|
|
|
600
744
|
};
|
|
601
745
|
}
|
|
602
746
|
function useUpdatedRef(value) {
|
|
603
|
-
const ref = (0,
|
|
604
|
-
(0,
|
|
747
|
+
const ref = (0, import_react8.useRef)(value);
|
|
748
|
+
(0, import_react8.useEffect)(() => {
|
|
605
749
|
ref.current = value;
|
|
606
750
|
}, [value]);
|
|
607
751
|
return ref;
|
|
@@ -632,17 +776,17 @@ ${additionalInstructions}` : "");
|
|
|
632
776
|
|
|
633
777
|
// src/hooks/use-copilot-action.ts
|
|
634
778
|
var import_shared3 = require("@copilotkit/shared");
|
|
635
|
-
var
|
|
779
|
+
var import_react9 = require("react");
|
|
636
780
|
function useCopilotAction(action, dependencies) {
|
|
637
781
|
const { setAction, removeAction, actions, chatComponentsCache } = useCopilotContext();
|
|
638
|
-
const idRef = (0,
|
|
639
|
-
const renderAndWaitRef = (0,
|
|
782
|
+
const idRef = (0, import_react9.useRef)((0, import_shared3.randomId)());
|
|
783
|
+
const renderAndWaitRef = (0, import_react9.useRef)(null);
|
|
640
784
|
action = __spreadValues({}, action);
|
|
641
785
|
if (action.renderAndWait || action.renderAndWaitForResponse) {
|
|
642
786
|
const renderAndWait = action.renderAndWait || action.renderAndWaitForResponse;
|
|
643
787
|
action.renderAndWait = void 0;
|
|
644
788
|
action.renderAndWaitForResponse = void 0;
|
|
645
|
-
action.handler = () => __async(this, null, function* () {
|
|
789
|
+
action.handler = useAsyncCallback(() => __async(this, null, function* () {
|
|
646
790
|
let resolve;
|
|
647
791
|
let reject;
|
|
648
792
|
const promise = new Promise((resolvePromise, rejectPromise) => {
|
|
@@ -651,7 +795,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
651
795
|
});
|
|
652
796
|
renderAndWaitRef.current = { promise, resolve, reject };
|
|
653
797
|
return yield promise;
|
|
654
|
-
});
|
|
798
|
+
}), []);
|
|
655
799
|
action.render = (props) => {
|
|
656
800
|
const waitProps = {
|
|
657
801
|
status: props.status,
|
|
@@ -671,7 +815,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
671
815
|
return renderAndWait(waitProps);
|
|
672
816
|
}
|
|
673
817
|
}
|
|
674
|
-
return (0,
|
|
818
|
+
return (0, import_react9.createElement)(import_react9.Fragment);
|
|
675
819
|
};
|
|
676
820
|
}
|
|
677
821
|
if (dependencies === void 0) {
|
|
@@ -684,7 +828,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
684
828
|
}
|
|
685
829
|
}
|
|
686
830
|
}
|
|
687
|
-
(0,
|
|
831
|
+
(0, import_react9.useEffect)(() => {
|
|
688
832
|
setAction(idRef.current, action);
|
|
689
833
|
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
690
834
|
chatComponentsCache.current.actions[action.name] = action.render;
|
|
@@ -710,7 +854,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
710
854
|
}
|
|
711
855
|
|
|
712
856
|
// src/hooks/use-coagent-state-render.ts
|
|
713
|
-
var
|
|
857
|
+
var import_react10 = require("react");
|
|
714
858
|
var import_shared4 = require("@copilotkit/shared");
|
|
715
859
|
function useCoAgentStateRender(action, dependencies) {
|
|
716
860
|
const {
|
|
@@ -718,8 +862,8 @@ function useCoAgentStateRender(action, dependencies) {
|
|
|
718
862
|
removeCoAgentStateRender,
|
|
719
863
|
coAgentStateRenders,
|
|
720
864
|
chatComponentsCache
|
|
721
|
-
} = (0,
|
|
722
|
-
const idRef = (0,
|
|
865
|
+
} = (0, import_react10.useContext)(CopilotContext);
|
|
866
|
+
const idRef = (0, import_react10.useRef)((0, import_shared4.randomId)());
|
|
723
867
|
const key = `${action.name}-${action.nodeName || "global"}`;
|
|
724
868
|
if (dependencies === void 0) {
|
|
725
869
|
if (coAgentStateRenders[idRef.current]) {
|
|
@@ -731,7 +875,7 @@ function useCoAgentStateRender(action, dependencies) {
|
|
|
731
875
|
}
|
|
732
876
|
}
|
|
733
877
|
}
|
|
734
|
-
(0,
|
|
878
|
+
(0, import_react10.useEffect)(() => {
|
|
735
879
|
setCoAgentStateRender(idRef.current, action);
|
|
736
880
|
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
737
881
|
chatComponentsCache.current.coAgentStateRenders[key] = action.render;
|
|
@@ -751,11 +895,11 @@ function useCoAgentStateRender(action, dependencies) {
|
|
|
751
895
|
}
|
|
752
896
|
|
|
753
897
|
// src/hooks/use-make-copilot-document-readable.ts
|
|
754
|
-
var
|
|
898
|
+
var import_react11 = require("react");
|
|
755
899
|
function useMakeCopilotDocumentReadable(document, categories, dependencies = []) {
|
|
756
900
|
const { addDocumentContext, removeDocumentContext } = useCopilotContext();
|
|
757
|
-
const idRef = (0,
|
|
758
|
-
(0,
|
|
901
|
+
const idRef = (0, import_react11.useRef)();
|
|
902
|
+
(0, import_react11.useEffect)(() => {
|
|
759
903
|
const id = addDocumentContext(document, categories);
|
|
760
904
|
idRef.current = id;
|
|
761
905
|
return () => {
|
|
@@ -766,16 +910,16 @@ function useMakeCopilotDocumentReadable(document, categories, dependencies = [])
|
|
|
766
910
|
}
|
|
767
911
|
|
|
768
912
|
// src/hooks/use-copilot-readable.ts
|
|
769
|
-
var
|
|
913
|
+
var import_react12 = require("react");
|
|
770
914
|
function convertToJSON(description, value) {
|
|
771
915
|
return `${description}: ${typeof value === "string" ? value : JSON.stringify(value)}`;
|
|
772
916
|
}
|
|
773
917
|
function useCopilotReadable({ description, value, parentId, categories, convert }, dependencies) {
|
|
774
918
|
const { addContext, removeContext } = useCopilotContext();
|
|
775
|
-
const idRef = (0,
|
|
919
|
+
const idRef = (0, import_react12.useRef)();
|
|
776
920
|
convert = convert || convertToJSON;
|
|
777
921
|
const information = convert(description, value);
|
|
778
|
-
(0,
|
|
922
|
+
(0, import_react12.useEffect)(() => {
|
|
779
923
|
const id = addContext(information, parentId, categories);
|
|
780
924
|
idRef.current = id;
|
|
781
925
|
return () => {
|
|
@@ -786,7 +930,7 @@ function useCopilotReadable({ description, value, parentId, categories, convert
|
|
|
786
930
|
}
|
|
787
931
|
|
|
788
932
|
// src/hooks/use-coagent.ts
|
|
789
|
-
var
|
|
933
|
+
var import_react13 = require("react");
|
|
790
934
|
function useCoAgent(options) {
|
|
791
935
|
const isExternalStateManagement = (options2) => {
|
|
792
936
|
return "state" in options2 && "setState" in options2;
|
|
@@ -826,28 +970,29 @@ function useCoAgent(options) {
|
|
|
826
970
|
};
|
|
827
971
|
const coagentState = getCoagentState(coagentStates, name);
|
|
828
972
|
const state = isExternalStateManagement(options) ? options.state : coagentState.state;
|
|
829
|
-
(0,
|
|
973
|
+
(0, import_react13.useEffect)(() => {
|
|
830
974
|
if (isExternalStateManagement(options)) {
|
|
831
975
|
setState(options.state);
|
|
832
976
|
} else if (coagentStates[name] === void 0) {
|
|
833
977
|
setState(options.initialState === void 0 ? {} : options.initialState);
|
|
834
978
|
}
|
|
835
979
|
}, [isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0]);
|
|
980
|
+
const runAgentCallback = useAsyncCallback(
|
|
981
|
+
(hint) => __async(this, null, function* () {
|
|
982
|
+
yield runAgent(name, context, appendMessage, runChatCompletion, hint);
|
|
983
|
+
}),
|
|
984
|
+
[name, context, appendMessage, runChatCompletion]
|
|
985
|
+
);
|
|
836
986
|
return {
|
|
837
987
|
name,
|
|
838
988
|
nodeName: coagentState.nodeName,
|
|
839
|
-
|
|
840
|
-
setState,
|
|
989
|
+
threadId: coagentState.threadId,
|
|
841
990
|
running: coagentState.running,
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
stop: () =>
|
|
846
|
-
|
|
847
|
-
},
|
|
848
|
-
run: (hint) => {
|
|
849
|
-
return runAgent(name, context, appendMessage, runChatCompletion, hint);
|
|
850
|
-
}
|
|
991
|
+
state: coagentState.state,
|
|
992
|
+
setState: isExternalStateManagement(options) ? options.setState : setState,
|
|
993
|
+
start: () => startAgent(name, context),
|
|
994
|
+
stop: () => stopAgent(name, context),
|
|
995
|
+
run: runAgentCallback
|
|
851
996
|
};
|
|
852
997
|
}
|
|
853
998
|
function startAgent(name, context) {
|