@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
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"));
|
|
@@ -146,8 +146,7 @@ var emptyCopilotContext = {
|
|
|
146
146
|
},
|
|
147
147
|
agentSession: null,
|
|
148
148
|
setAgentSession: () => {
|
|
149
|
-
}
|
|
150
|
-
forwardedParameters: {}
|
|
149
|
+
}
|
|
151
150
|
};
|
|
152
151
|
var CopilotContext = import_react.default.createContext(emptyCopilotContext);
|
|
153
152
|
function useCopilotContext() {
|
|
@@ -166,16 +165,131 @@ function returnAndThrowInDebug(value) {
|
|
|
166
165
|
var import_runtime_client_gql3 = require("@copilotkit/runtime-client-gql");
|
|
167
166
|
|
|
168
167
|
// src/hooks/use-chat.ts
|
|
169
|
-
var
|
|
168
|
+
var import_react5 = require("react");
|
|
170
169
|
var import_shared = require("@copilotkit/shared");
|
|
171
170
|
var import_runtime_client_gql2 = require("@copilotkit/runtime-client-gql");
|
|
172
171
|
|
|
173
172
|
// src/components/toast/toast-provider.tsx
|
|
173
|
+
var import_react3 = require("react");
|
|
174
|
+
|
|
175
|
+
// src/components/error-boundary/error-utils.tsx
|
|
174
176
|
var import_react2 = require("react");
|
|
177
|
+
|
|
178
|
+
// src/components/toast/exclamation-mark-icon.tsx
|
|
175
179
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
176
|
-
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);
|
|
177
291
|
function useToast() {
|
|
178
|
-
const context = (0,
|
|
292
|
+
const context = (0, import_react3.useContext)(ToastContext);
|
|
179
293
|
if (!context) {
|
|
180
294
|
throw new Error("useToast must be used within a ToastProvider");
|
|
181
295
|
}
|
|
@@ -184,14 +298,17 @@ function useToast() {
|
|
|
184
298
|
|
|
185
299
|
// src/hooks/use-copilot-runtime-client.ts
|
|
186
300
|
var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
|
|
187
|
-
var
|
|
301
|
+
var import_react4 = require("react");
|
|
188
302
|
var useCopilotRuntimeClient = (options) => {
|
|
189
303
|
const { addGraphQLErrorsToast } = useToast();
|
|
190
|
-
const
|
|
304
|
+
const addErrorToast = useErrorToast();
|
|
305
|
+
const runtimeClient = (0, import_react4.useMemo)(() => {
|
|
191
306
|
return new import_runtime_client_gql.CopilotRuntimeClient(__spreadProps(__spreadValues({}, options), {
|
|
192
307
|
handleGQLErrors: (error) => {
|
|
193
308
|
if (error.graphQLErrors.length) {
|
|
194
309
|
addGraphQLErrorsToast(error.graphQLErrors);
|
|
310
|
+
} else {
|
|
311
|
+
addErrorToast([error]);
|
|
195
312
|
}
|
|
196
313
|
}
|
|
197
314
|
}));
|
|
@@ -217,12 +334,12 @@ function useChat(options) {
|
|
|
217
334
|
agentSession,
|
|
218
335
|
setAgentSession
|
|
219
336
|
} = options;
|
|
220
|
-
const abortControllerRef = (0,
|
|
221
|
-
const threadIdRef = (0,
|
|
222
|
-
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);
|
|
223
340
|
const { addGraphQLErrorsToast } = useToast();
|
|
224
|
-
const runChatCompletionRef = (0,
|
|
225
|
-
const agentSessionRef = (0,
|
|
341
|
+
const runChatCompletionRef = (0, import_react5.useRef)();
|
|
342
|
+
const agentSessionRef = (0, import_react5.useRef)(agentSession);
|
|
226
343
|
agentSessionRef.current = agentSession;
|
|
227
344
|
const publicApiKey = copilotConfig.publicApiKey;
|
|
228
345
|
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [import_shared.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
|
|
@@ -232,211 +349,236 @@ function useChat(options) {
|
|
|
232
349
|
headers,
|
|
233
350
|
credentials: copilotConfig.credentials
|
|
234
351
|
});
|
|
235
|
-
const runChatCompletion = (
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
runtimeClient.
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
available
|
|
261
|
-
|
|
262
|
-
available
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
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
|
+
}
|
|
282
403
|
}
|
|
404
|
+
} : {})
|
|
405
|
+
} : {}), {
|
|
406
|
+
metadata: {
|
|
407
|
+
requestType: import_runtime_client_gql2.CopilotRequestType.Chat
|
|
283
408
|
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
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;
|
|
288
435
|
}
|
|
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
|
-
|
|
342
|
-
|
|
343
|
-
|
|
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) {
|
|
344
492
|
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
345
493
|
break;
|
|
346
494
|
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
const result = yield onFunctionCall({
|
|
354
|
-
messages: previousMessages,
|
|
355
|
-
name: message.name,
|
|
356
|
-
args: message.arguments
|
|
357
|
-
});
|
|
358
|
-
actionResults[message.id] = result;
|
|
359
|
-
} catch (e) {
|
|
360
|
-
actionResults[message.id] = `Failed to execute action ${message.name}`;
|
|
361
|
-
console.error(`Failed to execute action ${message.name}: ${e}`);
|
|
362
|
-
}
|
|
495
|
+
yield onCoAgentStateRender({
|
|
496
|
+
name: message.agentName,
|
|
497
|
+
nodeName: message.nodeName,
|
|
498
|
+
state: message.state
|
|
499
|
+
});
|
|
500
|
+
executedCoAgentStateRenders.push(message.id);
|
|
363
501
|
}
|
|
364
|
-
newMessages.push(
|
|
365
|
-
new import_runtime_client_gql2.ResultMessage({
|
|
366
|
-
result: import_runtime_client_gql2.ResultMessage.encodeResult(actionResults[message.id]),
|
|
367
|
-
actionExecutionId: message.id,
|
|
368
|
-
actionName: message.name
|
|
369
|
-
})
|
|
370
|
-
);
|
|
371
502
|
}
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
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);
|
|
375
524
|
}
|
|
376
|
-
yield onCoAgentStateRender({
|
|
377
|
-
name: message.agentName,
|
|
378
|
-
nodeName: message.nodeName,
|
|
379
|
-
state: message.state
|
|
380
|
-
});
|
|
381
|
-
executedCoAgentStateRenders.push(message.id);
|
|
382
525
|
}
|
|
383
526
|
}
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
setCoagentStatesWithRef((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
387
|
-
[lastAgentStateMessage.agentName]: {
|
|
388
|
-
name: lastAgentStateMessage.agentName,
|
|
389
|
-
state: lastAgentStateMessage.state,
|
|
390
|
-
running: lastAgentStateMessage.running,
|
|
391
|
-
active: lastAgentStateMessage.active,
|
|
392
|
-
threadId: lastAgentStateMessage.threadId,
|
|
393
|
-
nodeName: lastAgentStateMessage.nodeName,
|
|
394
|
-
runId: lastAgentStateMessage.runId
|
|
395
|
-
}
|
|
396
|
-
}));
|
|
397
|
-
if (lastAgentStateMessage.running) {
|
|
398
|
-
setAgentSession({
|
|
399
|
-
threadId: lastAgentStateMessage.threadId,
|
|
400
|
-
agentName: lastAgentStateMessage.agentName,
|
|
401
|
-
nodeName: lastAgentStateMessage.nodeName
|
|
402
|
-
});
|
|
403
|
-
} else {
|
|
404
|
-
setAgentSession(null);
|
|
405
|
-
}
|
|
527
|
+
if (newMessages.length > 0) {
|
|
528
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
406
529
|
}
|
|
407
530
|
}
|
|
408
|
-
if (
|
|
409
|
-
|
|
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();
|
|
410
541
|
}
|
|
542
|
+
} finally {
|
|
543
|
+
setIsLoading(false);
|
|
411
544
|
}
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
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
|
+
);
|
|
427
563
|
runChatCompletionRef.current = runChatCompletion;
|
|
428
|
-
const runChatCompletionAndHandleFunctionCall = (
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
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* () {
|
|
440
582
|
if (isLoading || messages.length === 0) {
|
|
441
583
|
return;
|
|
442
584
|
}
|
|
@@ -447,7 +589,7 @@ function useChat(options) {
|
|
|
447
589
|
}
|
|
448
590
|
setMessages(newMessages);
|
|
449
591
|
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
450
|
-
});
|
|
592
|
+
}), [isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]);
|
|
451
593
|
const stop = () => {
|
|
452
594
|
var _a;
|
|
453
595
|
(_a = abortControllerRef.current) == null ? void 0 : _a.abort();
|
|
@@ -461,19 +603,19 @@ function useChat(options) {
|
|
|
461
603
|
}
|
|
462
604
|
|
|
463
605
|
// src/components/copilot-provider/copilotkit.tsx
|
|
464
|
-
var
|
|
606
|
+
var import_react7 = require("react");
|
|
465
607
|
var import_react_dom = require("react-dom");
|
|
466
608
|
var import_shared2 = require("@copilotkit/shared");
|
|
467
609
|
|
|
468
610
|
// src/context/copilot-messages-context.tsx
|
|
469
|
-
var
|
|
611
|
+
var import_react6 = __toESM(require("react"));
|
|
470
612
|
var emptyCopilotContext2 = {
|
|
471
613
|
messages: [],
|
|
472
614
|
setMessages: () => []
|
|
473
615
|
};
|
|
474
|
-
var CopilotMessagesContext =
|
|
616
|
+
var CopilotMessagesContext = import_react6.default.createContext(emptyCopilotContext2);
|
|
475
617
|
function useCopilotMessagesContext() {
|
|
476
|
-
const context =
|
|
618
|
+
const context = import_react6.default.useContext(CopilotMessagesContext);
|
|
477
619
|
if (context === emptyCopilotContext2) {
|
|
478
620
|
throw new Error(
|
|
479
621
|
"A messages consuming component was not wrapped with `<CopilotMessages> {...} </CopilotMessages>`"
|
|
@@ -483,7 +625,7 @@ function useCopilotMessagesContext() {
|
|
|
483
625
|
}
|
|
484
626
|
|
|
485
627
|
// src/components/copilot-provider/copilotkit.tsx
|
|
486
|
-
var
|
|
628
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
487
629
|
var defaultCopilotContextCategories = ["global"];
|
|
488
630
|
|
|
489
631
|
// src/hooks/use-copilot-chat.ts
|
|
@@ -505,18 +647,17 @@ function useCopilotChat(_a = {}) {
|
|
|
505
647
|
setCoagentStatesWithRef,
|
|
506
648
|
coAgentStateRenders,
|
|
507
649
|
agentSession,
|
|
508
|
-
setAgentSession
|
|
509
|
-
forwardedParameters
|
|
650
|
+
setAgentSession
|
|
510
651
|
} = useCopilotContext();
|
|
511
652
|
const { messages, setMessages } = useCopilotMessagesContext();
|
|
512
653
|
const latestGetContextString = useUpdatedRef(getContextString);
|
|
513
|
-
const deleteMessage = (0,
|
|
654
|
+
const deleteMessage = (0, import_react8.useCallback)(
|
|
514
655
|
(messageId) => {
|
|
515
656
|
setMessages((prev) => prev.filter((message) => message.id !== messageId));
|
|
516
657
|
},
|
|
517
658
|
[setMessages]
|
|
518
659
|
);
|
|
519
|
-
const makeSystemMessageCallback = (0,
|
|
660
|
+
const makeSystemMessageCallback = (0, import_react8.useCallback)(() => {
|
|
520
661
|
const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
|
|
521
662
|
const contextString = latestGetContextString.current([], defaultCopilotContextCategories);
|
|
522
663
|
return new import_runtime_client_gql3.TextMessage({
|
|
@@ -524,7 +665,7 @@ function useCopilotChat(_a = {}) {
|
|
|
524
665
|
role: import_runtime_client_gql3.Role.System
|
|
525
666
|
});
|
|
526
667
|
}, [getContextString, makeSystemMessage, chatInstructions]);
|
|
527
|
-
const onCoAgentStateRender = (
|
|
668
|
+
const onCoAgentStateRender = useAsyncCallback(
|
|
528
669
|
(args) => __async(this, null, function* () {
|
|
529
670
|
var _a2;
|
|
530
671
|
const { name, nodeName, state } = args;
|
|
@@ -556,42 +697,41 @@ function useCopilotChat(_a = {}) {
|
|
|
556
697
|
coagentStatesRef,
|
|
557
698
|
setCoagentStatesWithRef,
|
|
558
699
|
agentSession,
|
|
559
|
-
setAgentSession
|
|
560
|
-
forwardedParameters
|
|
700
|
+
setAgentSession
|
|
561
701
|
}));
|
|
562
702
|
const latestAppend = useUpdatedRef(append);
|
|
563
|
-
const latestAppendFunc = (
|
|
564
|
-
(message) => {
|
|
565
|
-
return latestAppend.current(message);
|
|
566
|
-
},
|
|
703
|
+
const latestAppendFunc = useAsyncCallback(
|
|
704
|
+
(message) => __async(this, null, function* () {
|
|
705
|
+
return yield latestAppend.current(message);
|
|
706
|
+
}),
|
|
567
707
|
[latestAppend]
|
|
568
708
|
);
|
|
569
709
|
const latestReload = useUpdatedRef(reload);
|
|
570
|
-
const latestReloadFunc = (
|
|
571
|
-
return latestReload.current();
|
|
572
|
-
}, [latestReload]);
|
|
710
|
+
const latestReloadFunc = useAsyncCallback(() => __async(this, null, function* () {
|
|
711
|
+
return yield latestReload.current();
|
|
712
|
+
}), [latestReload]);
|
|
573
713
|
const latestStop = useUpdatedRef(stop);
|
|
574
|
-
const latestStopFunc = (0,
|
|
714
|
+
const latestStopFunc = (0, import_react8.useCallback)(() => {
|
|
575
715
|
return latestStop.current();
|
|
576
716
|
}, [latestStop]);
|
|
577
717
|
const latestDelete = useUpdatedRef(deleteMessage);
|
|
578
|
-
const latestDeleteFunc = (0,
|
|
718
|
+
const latestDeleteFunc = (0, import_react8.useCallback)(
|
|
579
719
|
(messageId) => {
|
|
580
720
|
return latestDelete.current(messageId);
|
|
581
721
|
},
|
|
582
722
|
[latestDelete]
|
|
583
723
|
);
|
|
584
724
|
const latestSetMessages = useUpdatedRef(setMessages);
|
|
585
|
-
const latestSetMessagesFunc = (0,
|
|
725
|
+
const latestSetMessagesFunc = (0, import_react8.useCallback)(
|
|
586
726
|
(messages2) => {
|
|
587
727
|
return latestSetMessages.current(messages2);
|
|
588
728
|
},
|
|
589
729
|
[latestSetMessages]
|
|
590
730
|
);
|
|
591
731
|
const latestRunChatCompletion = useUpdatedRef(runChatCompletion);
|
|
592
|
-
const latestRunChatCompletionFunc = (
|
|
593
|
-
return latestRunChatCompletion.current();
|
|
594
|
-
}, [latestRunChatCompletion]);
|
|
732
|
+
const latestRunChatCompletionFunc = useAsyncCallback(() => __async(this, null, function* () {
|
|
733
|
+
return yield latestRunChatCompletion.current();
|
|
734
|
+
}), [latestRunChatCompletion]);
|
|
595
735
|
return {
|
|
596
736
|
visibleMessages: messages,
|
|
597
737
|
appendMessage: latestAppendFunc,
|
|
@@ -604,8 +744,8 @@ function useCopilotChat(_a = {}) {
|
|
|
604
744
|
};
|
|
605
745
|
}
|
|
606
746
|
function useUpdatedRef(value) {
|
|
607
|
-
const ref = (0,
|
|
608
|
-
(0,
|
|
747
|
+
const ref = (0, import_react8.useRef)(value);
|
|
748
|
+
(0, import_react8.useEffect)(() => {
|
|
609
749
|
ref.current = value;
|
|
610
750
|
}, [value]);
|
|
611
751
|
return ref;
|
|
@@ -636,17 +776,17 @@ ${additionalInstructions}` : "");
|
|
|
636
776
|
|
|
637
777
|
// src/hooks/use-copilot-action.ts
|
|
638
778
|
var import_shared3 = require("@copilotkit/shared");
|
|
639
|
-
var
|
|
779
|
+
var import_react9 = require("react");
|
|
640
780
|
function useCopilotAction(action, dependencies) {
|
|
641
781
|
const { setAction, removeAction, actions, chatComponentsCache } = useCopilotContext();
|
|
642
|
-
const idRef = (0,
|
|
643
|
-
const renderAndWaitRef = (0,
|
|
782
|
+
const idRef = (0, import_react9.useRef)((0, import_shared3.randomId)());
|
|
783
|
+
const renderAndWaitRef = (0, import_react9.useRef)(null);
|
|
644
784
|
action = __spreadValues({}, action);
|
|
645
785
|
if (action.renderAndWait || action.renderAndWaitForResponse) {
|
|
646
786
|
const renderAndWait = action.renderAndWait || action.renderAndWaitForResponse;
|
|
647
787
|
action.renderAndWait = void 0;
|
|
648
788
|
action.renderAndWaitForResponse = void 0;
|
|
649
|
-
action.handler = () => __async(this, null, function* () {
|
|
789
|
+
action.handler = useAsyncCallback(() => __async(this, null, function* () {
|
|
650
790
|
let resolve;
|
|
651
791
|
let reject;
|
|
652
792
|
const promise = new Promise((resolvePromise, rejectPromise) => {
|
|
@@ -655,7 +795,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
655
795
|
});
|
|
656
796
|
renderAndWaitRef.current = { promise, resolve, reject };
|
|
657
797
|
return yield promise;
|
|
658
|
-
});
|
|
798
|
+
}), []);
|
|
659
799
|
action.render = (props) => {
|
|
660
800
|
const waitProps = {
|
|
661
801
|
status: props.status,
|
|
@@ -675,7 +815,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
675
815
|
return renderAndWait(waitProps);
|
|
676
816
|
}
|
|
677
817
|
}
|
|
678
|
-
return (0,
|
|
818
|
+
return (0, import_react9.createElement)(import_react9.Fragment);
|
|
679
819
|
};
|
|
680
820
|
}
|
|
681
821
|
if (dependencies === void 0) {
|
|
@@ -688,7 +828,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
688
828
|
}
|
|
689
829
|
}
|
|
690
830
|
}
|
|
691
|
-
(0,
|
|
831
|
+
(0, import_react9.useEffect)(() => {
|
|
692
832
|
setAction(idRef.current, action);
|
|
693
833
|
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
694
834
|
chatComponentsCache.current.actions[action.name] = action.render;
|
|
@@ -714,7 +854,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
714
854
|
}
|
|
715
855
|
|
|
716
856
|
// src/hooks/use-coagent-state-render.ts
|
|
717
|
-
var
|
|
857
|
+
var import_react10 = require("react");
|
|
718
858
|
var import_shared4 = require("@copilotkit/shared");
|
|
719
859
|
function useCoAgentStateRender(action, dependencies) {
|
|
720
860
|
const {
|
|
@@ -722,8 +862,8 @@ function useCoAgentStateRender(action, dependencies) {
|
|
|
722
862
|
removeCoAgentStateRender,
|
|
723
863
|
coAgentStateRenders,
|
|
724
864
|
chatComponentsCache
|
|
725
|
-
} = (0,
|
|
726
|
-
const idRef = (0,
|
|
865
|
+
} = (0, import_react10.useContext)(CopilotContext);
|
|
866
|
+
const idRef = (0, import_react10.useRef)((0, import_shared4.randomId)());
|
|
727
867
|
const key = `${action.name}-${action.nodeName || "global"}`;
|
|
728
868
|
if (dependencies === void 0) {
|
|
729
869
|
if (coAgentStateRenders[idRef.current]) {
|
|
@@ -735,7 +875,7 @@ function useCoAgentStateRender(action, dependencies) {
|
|
|
735
875
|
}
|
|
736
876
|
}
|
|
737
877
|
}
|
|
738
|
-
(0,
|
|
878
|
+
(0, import_react10.useEffect)(() => {
|
|
739
879
|
setCoAgentStateRender(idRef.current, action);
|
|
740
880
|
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
741
881
|
chatComponentsCache.current.coAgentStateRenders[key] = action.render;
|
|
@@ -755,11 +895,11 @@ function useCoAgentStateRender(action, dependencies) {
|
|
|
755
895
|
}
|
|
756
896
|
|
|
757
897
|
// src/hooks/use-make-copilot-document-readable.ts
|
|
758
|
-
var
|
|
898
|
+
var import_react11 = require("react");
|
|
759
899
|
function useMakeCopilotDocumentReadable(document, categories, dependencies = []) {
|
|
760
900
|
const { addDocumentContext, removeDocumentContext } = useCopilotContext();
|
|
761
|
-
const idRef = (0,
|
|
762
|
-
(0,
|
|
901
|
+
const idRef = (0, import_react11.useRef)();
|
|
902
|
+
(0, import_react11.useEffect)(() => {
|
|
763
903
|
const id = addDocumentContext(document, categories);
|
|
764
904
|
idRef.current = id;
|
|
765
905
|
return () => {
|
|
@@ -770,16 +910,16 @@ function useMakeCopilotDocumentReadable(document, categories, dependencies = [])
|
|
|
770
910
|
}
|
|
771
911
|
|
|
772
912
|
// src/hooks/use-copilot-readable.ts
|
|
773
|
-
var
|
|
913
|
+
var import_react12 = require("react");
|
|
774
914
|
function convertToJSON(description, value) {
|
|
775
915
|
return `${description}: ${typeof value === "string" ? value : JSON.stringify(value)}`;
|
|
776
916
|
}
|
|
777
917
|
function useCopilotReadable({ description, value, parentId, categories, convert }, dependencies) {
|
|
778
918
|
const { addContext, removeContext } = useCopilotContext();
|
|
779
|
-
const idRef = (0,
|
|
919
|
+
const idRef = (0, import_react12.useRef)();
|
|
780
920
|
convert = convert || convertToJSON;
|
|
781
921
|
const information = convert(description, value);
|
|
782
|
-
(0,
|
|
922
|
+
(0, import_react12.useEffect)(() => {
|
|
783
923
|
const id = addContext(information, parentId, categories);
|
|
784
924
|
idRef.current = id;
|
|
785
925
|
return () => {
|
|
@@ -790,7 +930,7 @@ function useCopilotReadable({ description, value, parentId, categories, convert
|
|
|
790
930
|
}
|
|
791
931
|
|
|
792
932
|
// src/hooks/use-coagent.ts
|
|
793
|
-
var
|
|
933
|
+
var import_react13 = require("react");
|
|
794
934
|
function useCoAgent(options) {
|
|
795
935
|
const isExternalStateManagement = (options2) => {
|
|
796
936
|
return "state" in options2 && "setState" in options2;
|
|
@@ -830,28 +970,29 @@ function useCoAgent(options) {
|
|
|
830
970
|
};
|
|
831
971
|
const coagentState = getCoagentState(coagentStates, name);
|
|
832
972
|
const state = isExternalStateManagement(options) ? options.state : coagentState.state;
|
|
833
|
-
(0,
|
|
973
|
+
(0, import_react13.useEffect)(() => {
|
|
834
974
|
if (isExternalStateManagement(options)) {
|
|
835
975
|
setState(options.state);
|
|
836
976
|
} else if (coagentStates[name] === void 0) {
|
|
837
977
|
setState(options.initialState === void 0 ? {} : options.initialState);
|
|
838
978
|
}
|
|
839
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
|
+
);
|
|
840
986
|
return {
|
|
841
987
|
name,
|
|
842
988
|
nodeName: coagentState.nodeName,
|
|
843
|
-
|
|
844
|
-
setState,
|
|
989
|
+
threadId: coagentState.threadId,
|
|
845
990
|
running: coagentState.running,
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
stop: () =>
|
|
850
|
-
|
|
851
|
-
},
|
|
852
|
-
run: (hint) => {
|
|
853
|
-
return runAgent(name, context, appendMessage, runChatCompletion, hint);
|
|
854
|
-
}
|
|
991
|
+
state: coagentState.state,
|
|
992
|
+
setState: isExternalStateManagement(options) ? options.setState : setState,
|
|
993
|
+
start: () => startAgent(name, context),
|
|
994
|
+
stop: () => stopAgent(name, context),
|
|
995
|
+
run: runAgentCallback
|
|
855
996
|
};
|
|
856
997
|
}
|
|
857
998
|
function startAgent(name, context) {
|