@copilotkit/react-core 1.5.0-tyler-reset-chat.0 → 1.5.0
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 +158 -4
- package/README.md +2 -0
- package/dist/{chunk-3AYELZJS.mjs → chunk-35EN6BG4.mjs} +2 -2
- package/dist/{chunk-3AYELZJS.mjs.map → chunk-35EN6BG4.mjs.map} +1 -1
- package/dist/{chunk-SEPYQHH7.mjs → chunk-42N5VKIX.mjs} +34 -28
- package/dist/chunk-42N5VKIX.mjs.map +1 -0
- package/dist/{chunk-USL3EHJB.mjs → chunk-5FYKUKG3.mjs} +2 -2
- package/dist/{chunk-ODN4H66E.mjs → chunk-7LRDVJH5.mjs} +6 -2
- package/dist/chunk-7LRDVJH5.mjs.map +1 -0
- package/dist/{chunk-CZMEZR6F.mjs → chunk-BT6WK2JZ.mjs} +34 -19
- package/dist/chunk-BT6WK2JZ.mjs.map +1 -0
- package/dist/{chunk-3R4J2TPH.mjs → chunk-EUU6NNYU.mjs} +29 -13
- package/dist/chunk-EUU6NNYU.mjs.map +1 -0
- package/dist/chunk-QCUP6HLK.mjs +37 -0
- package/dist/chunk-QCUP6HLK.mjs.map +1 -0
- package/dist/chunk-QTDCEDOC.mjs +392 -0
- package/dist/chunk-QTDCEDOC.mjs.map +1 -0
- package/dist/{chunk-JR55I3FL.mjs → chunk-QX6V774L.mjs} +6 -8
- package/dist/chunk-QX6V774L.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-2JP64U3A.mjs → chunk-TQN3EZWQ.mjs} +4 -1
- package/dist/chunk-TQN3EZWQ.mjs.map +1 -0
- package/dist/{chunk-XUPO37VH.mjs → chunk-V3PFWGIY.mjs} +2 -2
- package/dist/{chunk-6QKA3SNN.mjs → chunk-VMP6JWBB.mjs} +21 -5
- package/dist/chunk-VMP6JWBB.mjs.map +1 -0
- package/dist/chunk-XERJQUHA.mjs +31 -0
- package/dist/chunk-XERJQUHA.mjs.map +1 -0
- package/dist/components/copilot-provider/copilotkit.js +173 -92
- 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.js +173 -92
- 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.js +173 -92
- 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 +4 -2
- package/dist/context/copilot-context.js +3 -0
- package/dist/context/copilot-context.js.map +1 -1
- package/dist/context/copilot-context.mjs +1 -1
- package/dist/context/index.d.ts +1 -1
- package/dist/context/index.js +3 -0
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +1 -1
- package/dist/hooks/index.js +554 -308
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +13 -11
- package/dist/hooks/use-chat.d.ts +6 -2
- package/dist/hooks/use-chat.js +434 -219
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +4 -3
- package/dist/hooks/use-coagent-state-render.d.ts +2 -2
- package/dist/hooks/use-coagent-state-render.js +3 -0
- 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.d.ts +1 -1
- package/dist/hooks/use-coagent.js +510 -277
- package/dist/hooks/use-coagent.js.map +1 -1
- package/dist/hooks/use-coagent.mjs +9 -7
- package/dist/hooks/use-copilot-action.d.ts +12 -2
- package/dist/hooks/use-copilot-action.js +157 -16
- 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.d.ts +1 -0
- package/dist/hooks/use-copilot-chat.js +483 -253
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +8 -6
- package/dist/hooks/use-copilot-readable.js +3 -0
- 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 +3 -0
- 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 +616 -401
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +14 -12
- package/dist/lib/copilot-task.d.ts +1 -1
- package/dist/lib/copilot-task.js +33 -13
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +7 -5
- package/dist/lib/index.d.ts +1 -1
- package/dist/lib/index.js +33 -13
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +7 -5
- package/dist/types/frontend-action.d.ts +21 -2
- package/dist/types/frontend-action.js +34 -0
- package/dist/types/frontend-action.js.map +1 -1
- package/dist/types/frontend-action.mjs +7 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.js.map +1 -1
- package/dist/utils/extract.js.map +1 -1
- package/dist/utils/extract.mjs +5 -4
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +5 -4
- package/package.json +5 -5
- package/src/components/copilot-provider/copilotkit.tsx +22 -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/context/copilot-context.tsx +17 -2
- package/src/hooks/use-chat.ts +375 -279
- package/src/hooks/use-coagent-state-render.ts +2 -2
- package/src/hooks/use-coagent.ts +34 -28
- package/src/hooks/use-copilot-action.ts +50 -15
- package/src/hooks/use-copilot-chat.ts +28 -14
- package/src/hooks/use-copilot-runtime-client.ts +4 -0
- package/src/lib/copilot-task.ts +2 -8
- package/src/types/frontend-action.ts +55 -2
- package/src/types/index.ts +5 -1
- package/dist/chunk-2JP64U3A.mjs.map +0 -1
- package/dist/chunk-2KCEHGSI.mjs.map +0 -1
- package/dist/chunk-3R4J2TPH.mjs.map +0 -1
- package/dist/chunk-6EN7J4V2.mjs +0 -317
- package/dist/chunk-6EN7J4V2.mjs.map +0 -1
- package/dist/chunk-6QKA3SNN.mjs.map +0 -1
- package/dist/chunk-CZMEZR6F.mjs.map +0 -1
- package/dist/chunk-JR55I3FL.mjs.map +0 -1
- package/dist/chunk-ODN4H66E.mjs.map +0 -1
- package/dist/chunk-SEPYQHH7.mjs.map +0 -1
- /package/dist/{chunk-USL3EHJB.mjs.map → chunk-5FYKUKG3.mjs.map} +0 -0
- /package/dist/{chunk-XUPO37VH.mjs.map → chunk-V3PFWGIY.mjs.map} +0 -0
|
@@ -83,7 +83,7 @@ __export(use_copilot_chat_exports, {
|
|
|
83
83
|
useCopilotChat: () => useCopilotChat
|
|
84
84
|
});
|
|
85
85
|
module.exports = __toCommonJS(use_copilot_chat_exports);
|
|
86
|
-
var
|
|
86
|
+
var import_react8 = require("react");
|
|
87
87
|
|
|
88
88
|
// src/context/copilot-context.tsx
|
|
89
89
|
var import_react = __toESM(require("react"));
|
|
@@ -134,6 +134,9 @@ var emptyCopilotContext = {
|
|
|
134
134
|
coagentStates: {},
|
|
135
135
|
setCoagentStates: () => {
|
|
136
136
|
},
|
|
137
|
+
coagentStatesRef: { current: {} },
|
|
138
|
+
setCoagentStatesWithRef: () => {
|
|
139
|
+
},
|
|
137
140
|
agentSession: null,
|
|
138
141
|
setAgentSession: () => {
|
|
139
142
|
},
|
|
@@ -160,19 +163,159 @@ function returnAndThrowInDebug(value) {
|
|
|
160
163
|
}
|
|
161
164
|
|
|
162
165
|
// src/hooks/use-copilot-chat.ts
|
|
163
|
-
var
|
|
166
|
+
var import_runtime_client_gql4 = require("@copilotkit/runtime-client-gql");
|
|
164
167
|
|
|
165
168
|
// src/hooks/use-chat.ts
|
|
166
|
-
var
|
|
169
|
+
var import_react5 = require("react");
|
|
170
|
+
var import_shared2 = require("@copilotkit/shared");
|
|
171
|
+
var import_runtime_client_gql3 = require("@copilotkit/runtime-client-gql");
|
|
172
|
+
|
|
173
|
+
// src/types/frontend-action.ts
|
|
174
|
+
var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
|
|
167
175
|
var import_shared = require("@copilotkit/shared");
|
|
168
|
-
|
|
176
|
+
function processActionsForRuntimeRequest(actions) {
|
|
177
|
+
const filteredActions = actions.filter(
|
|
178
|
+
(action) => action.available !== import_runtime_client_gql.ActionInputAvailability.Disabled && action.disabled !== true && action.name !== "*"
|
|
179
|
+
).map((action) => {
|
|
180
|
+
let available = import_runtime_client_gql.ActionInputAvailability.Enabled;
|
|
181
|
+
if (action.disabled) {
|
|
182
|
+
available = import_runtime_client_gql.ActionInputAvailability.Disabled;
|
|
183
|
+
} else if (action.available === "disabled") {
|
|
184
|
+
available = import_runtime_client_gql.ActionInputAvailability.Disabled;
|
|
185
|
+
} else if (action.available === "remote") {
|
|
186
|
+
available = import_runtime_client_gql.ActionInputAvailability.Remote;
|
|
187
|
+
}
|
|
188
|
+
return {
|
|
189
|
+
name: action.name,
|
|
190
|
+
description: action.description || "",
|
|
191
|
+
jsonSchema: JSON.stringify((0, import_shared.actionParametersToJsonSchema)(action.parameters || [])),
|
|
192
|
+
available
|
|
193
|
+
};
|
|
194
|
+
});
|
|
195
|
+
return filteredActions;
|
|
196
|
+
}
|
|
169
197
|
|
|
170
198
|
// src/components/toast/toast-provider.tsx
|
|
199
|
+
var import_react3 = require("react");
|
|
200
|
+
|
|
201
|
+
// src/components/error-boundary/error-utils.tsx
|
|
171
202
|
var import_react2 = require("react");
|
|
203
|
+
|
|
204
|
+
// src/components/toast/exclamation-mark-icon.tsx
|
|
172
205
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
173
|
-
var
|
|
206
|
+
var ExclamationMarkIcon = ({
|
|
207
|
+
className,
|
|
208
|
+
style
|
|
209
|
+
}) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
|
|
210
|
+
"svg",
|
|
211
|
+
{
|
|
212
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
213
|
+
width: "24",
|
|
214
|
+
height: "24",
|
|
215
|
+
viewBox: "0 0 24 24",
|
|
216
|
+
fill: "none",
|
|
217
|
+
stroke: "currentColor",
|
|
218
|
+
strokeWidth: "2",
|
|
219
|
+
strokeLinecap: "round",
|
|
220
|
+
strokeLinejoin: "round",
|
|
221
|
+
className: `lucide lucide-circle-alert ${className ? className : ""}`,
|
|
222
|
+
style,
|
|
223
|
+
children: [
|
|
224
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("circle", { cx: "12", cy: "12", r: "10" }),
|
|
225
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("line", { x1: "12", x2: "12", y1: "8", y2: "12" }),
|
|
226
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("line", { x1: "12", x2: "12.01", y1: "16", y2: "16" })
|
|
227
|
+
]
|
|
228
|
+
}
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
// src/components/error-boundary/error-utils.tsx
|
|
232
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
233
|
+
function ErrorToast({ errors }) {
|
|
234
|
+
const errorsToRender = errors.map((error, idx) => {
|
|
235
|
+
var _a, _b, _c;
|
|
236
|
+
const message = "extensions" in error ? (_b = (_a = error.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message : error.message;
|
|
237
|
+
const code = "extensions" in error ? (_c = error.extensions) == null ? void 0 : _c.code : null;
|
|
238
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
239
|
+
"div",
|
|
240
|
+
{
|
|
241
|
+
style: {
|
|
242
|
+
marginTop: idx === 0 ? 0 : 10,
|
|
243
|
+
marginBottom: 14
|
|
244
|
+
},
|
|
245
|
+
children: [
|
|
246
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ExclamationMarkIcon, { style: { marginBottom: 4 } }),
|
|
247
|
+
code && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
248
|
+
"div",
|
|
249
|
+
{
|
|
250
|
+
style: {
|
|
251
|
+
fontWeight: "600",
|
|
252
|
+
marginBottom: 4
|
|
253
|
+
},
|
|
254
|
+
children: [
|
|
255
|
+
"Copilot Cloud Error:",
|
|
256
|
+
" ",
|
|
257
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { style: { fontFamily: "monospace", fontWeight: "normal" }, children: code })
|
|
258
|
+
]
|
|
259
|
+
}
|
|
260
|
+
),
|
|
261
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { children: message })
|
|
262
|
+
]
|
|
263
|
+
},
|
|
264
|
+
idx
|
|
265
|
+
);
|
|
266
|
+
});
|
|
267
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
268
|
+
"div",
|
|
269
|
+
{
|
|
270
|
+
style: {
|
|
271
|
+
fontSize: "13px",
|
|
272
|
+
maxWidth: "600px"
|
|
273
|
+
},
|
|
274
|
+
children: [
|
|
275
|
+
errorsToRender,
|
|
276
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { fontSize: "11px", opacity: 0.75 }, children: "NOTE: This error only displays during local development." })
|
|
277
|
+
]
|
|
278
|
+
}
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
function useErrorToast() {
|
|
282
|
+
const { addToast } = useToast();
|
|
283
|
+
return (0, import_react2.useCallback)(
|
|
284
|
+
(error) => {
|
|
285
|
+
const errorId = error.map((err) => {
|
|
286
|
+
var _a, _b;
|
|
287
|
+
const message = "extensions" in err ? ((_b = (_a = err.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message) || err.message : err.message;
|
|
288
|
+
const stack = err.stack || "";
|
|
289
|
+
return btoa(message + stack).slice(0, 32);
|
|
290
|
+
}).join("|");
|
|
291
|
+
addToast({
|
|
292
|
+
type: "error",
|
|
293
|
+
id: errorId,
|
|
294
|
+
// Toast libraries typically dedupe by id
|
|
295
|
+
message: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ErrorToast, { errors: error })
|
|
296
|
+
});
|
|
297
|
+
},
|
|
298
|
+
[addToast]
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
function useAsyncCallback(callback, deps) {
|
|
302
|
+
const addErrorToast = useErrorToast();
|
|
303
|
+
return (0, import_react2.useCallback)((...args) => __async(this, null, function* () {
|
|
304
|
+
try {
|
|
305
|
+
return yield callback(...args);
|
|
306
|
+
} catch (error) {
|
|
307
|
+
console.error("Error in async callback:", error);
|
|
308
|
+
addErrorToast([error]);
|
|
309
|
+
throw error;
|
|
310
|
+
}
|
|
311
|
+
}), deps);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// src/components/toast/toast-provider.tsx
|
|
315
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
316
|
+
var ToastContext = (0, import_react3.createContext)(void 0);
|
|
174
317
|
function useToast() {
|
|
175
|
-
const context = (0,
|
|
318
|
+
const context = (0, import_react3.useContext)(ToastContext);
|
|
176
319
|
if (!context) {
|
|
177
320
|
throw new Error("useToast must be used within a ToastProvider");
|
|
178
321
|
}
|
|
@@ -180,15 +323,18 @@ function useToast() {
|
|
|
180
323
|
}
|
|
181
324
|
|
|
182
325
|
// src/hooks/use-copilot-runtime-client.ts
|
|
183
|
-
var
|
|
184
|
-
var
|
|
326
|
+
var import_runtime_client_gql2 = require("@copilotkit/runtime-client-gql");
|
|
327
|
+
var import_react4 = require("react");
|
|
185
328
|
var useCopilotRuntimeClient = (options) => {
|
|
186
329
|
const { addGraphQLErrorsToast } = useToast();
|
|
187
|
-
const
|
|
188
|
-
|
|
330
|
+
const addErrorToast = useErrorToast();
|
|
331
|
+
const runtimeClient = (0, import_react4.useMemo)(() => {
|
|
332
|
+
return new import_runtime_client_gql2.CopilotRuntimeClient(__spreadProps(__spreadValues({}, options), {
|
|
189
333
|
handleGQLErrors: (error) => {
|
|
190
334
|
if (error.graphQLErrors.length) {
|
|
191
335
|
addGraphQLErrorsToast(error.graphQLErrors);
|
|
336
|
+
} else {
|
|
337
|
+
addErrorToast([error]);
|
|
192
338
|
}
|
|
193
339
|
}
|
|
194
340
|
}));
|
|
@@ -209,8 +355,8 @@ function useChat(options) {
|
|
|
209
355
|
actions,
|
|
210
356
|
onFunctionCall,
|
|
211
357
|
onCoAgentStateRender,
|
|
212
|
-
|
|
213
|
-
|
|
358
|
+
setCoagentStatesWithRef,
|
|
359
|
+
coagentStatesRef,
|
|
214
360
|
agentSession,
|
|
215
361
|
setAgentSession,
|
|
216
362
|
threadId,
|
|
@@ -219,244 +365,298 @@ function useChat(options) {
|
|
|
219
365
|
setRunId,
|
|
220
366
|
chatAbortControllerRef
|
|
221
367
|
} = options;
|
|
222
|
-
const abortController = new AbortController();
|
|
223
|
-
chatAbortControllerRef.current = abortController;
|
|
224
368
|
const { addGraphQLErrorsToast } = useToast();
|
|
225
|
-
const runChatCompletionRef = (0,
|
|
226
|
-
const
|
|
227
|
-
coagentStatesRef.current = coagentStates;
|
|
228
|
-
const agentSessionRef = (0, import_react4.useRef)(agentSession);
|
|
369
|
+
const runChatCompletionRef = (0, import_react5.useRef)();
|
|
370
|
+
const agentSessionRef = (0, import_react5.useRef)(agentSession);
|
|
229
371
|
agentSessionRef.current = agentSession;
|
|
230
|
-
const threadIdRef = (0,
|
|
372
|
+
const threadIdRef = (0, import_react5.useRef)(threadId);
|
|
231
373
|
threadIdRef.current = threadId;
|
|
232
|
-
const runIdRef = (0,
|
|
374
|
+
const runIdRef = (0, import_react5.useRef)(runId);
|
|
233
375
|
runIdRef.current = runId;
|
|
234
376
|
const publicApiKey = copilotConfig.publicApiKey;
|
|
235
|
-
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [
|
|
377
|
+
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [import_shared2.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
|
|
236
378
|
const runtimeClient = useCopilotRuntimeClient({
|
|
237
379
|
url: copilotConfig.chatApiEndpoint,
|
|
238
380
|
publicApiKey: copilotConfig.publicApiKey,
|
|
239
381
|
headers,
|
|
240
382
|
credentials: copilotConfig.credentials
|
|
241
383
|
});
|
|
242
|
-
const runChatCompletion = (
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
available
|
|
275
|
-
};
|
|
276
|
-
}),
|
|
277
|
-
url: window.location.href
|
|
278
|
-
},
|
|
279
|
-
threadId: threadIdRef.current,
|
|
280
|
-
runId: runIdRef.current,
|
|
281
|
-
messages: (0, import_runtime_client_gql2.convertMessagesToGqlInput)((0, import_runtime_client_gql2.filterAgentStateMessages)(messagesWithContext))
|
|
282
|
-
}, copilotConfig.cloud ? {
|
|
283
|
-
cloud: __spreadValues({}, ((_c = (_b = (_a = copilotConfig.cloud.guardrails) == null ? void 0 : _a.input) == null ? void 0 : _b.restrictToTopic) == null ? void 0 : _c.enabled) ? {
|
|
284
|
-
guardrails: {
|
|
285
|
-
inputValidationRules: {
|
|
286
|
-
allowList: copilotConfig.cloud.guardrails.input.restrictToTopic.validTopics,
|
|
287
|
-
denyList: copilotConfig.cloud.guardrails.input.restrictToTopic.invalidTopics
|
|
384
|
+
const runChatCompletion = useAsyncCallback(
|
|
385
|
+
(previousMessages) => __async(this, null, function* () {
|
|
386
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
387
|
+
setIsLoading(true);
|
|
388
|
+
let newMessages = [
|
|
389
|
+
new import_runtime_client_gql3.TextMessage({
|
|
390
|
+
content: "",
|
|
391
|
+
role: import_runtime_client_gql3.Role.Assistant
|
|
392
|
+
})
|
|
393
|
+
];
|
|
394
|
+
chatAbortControllerRef.current = new AbortController();
|
|
395
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
396
|
+
const systemMessage = makeSystemMessageCallback();
|
|
397
|
+
const messagesWithContext = [systemMessage, ...initialMessages || [], ...previousMessages];
|
|
398
|
+
const isAgentRun = agentSessionRef.current !== null;
|
|
399
|
+
const stream = runtimeClient.asStream(
|
|
400
|
+
runtimeClient.generateCopilotResponse({
|
|
401
|
+
data: __spreadProps(__spreadValues(__spreadProps(__spreadValues({
|
|
402
|
+
frontend: {
|
|
403
|
+
actions: processActionsForRuntimeRequest(actions),
|
|
404
|
+
url: window.location.href
|
|
405
|
+
},
|
|
406
|
+
threadId: threadIdRef.current,
|
|
407
|
+
runId: runIdRef.current,
|
|
408
|
+
messages: (0, import_runtime_client_gql3.convertMessagesToGqlInput)((0, import_runtime_client_gql3.filterAgentStateMessages)(messagesWithContext))
|
|
409
|
+
}, copilotConfig.cloud ? {
|
|
410
|
+
cloud: __spreadValues({}, ((_c = (_b = (_a = copilotConfig.cloud.guardrails) == null ? void 0 : _a.input) == null ? void 0 : _b.restrictToTopic) == null ? void 0 : _c.enabled) ? {
|
|
411
|
+
guardrails: {
|
|
412
|
+
inputValidationRules: {
|
|
413
|
+
allowList: copilotConfig.cloud.guardrails.input.restrictToTopic.validTopics,
|
|
414
|
+
denyList: copilotConfig.cloud.guardrails.input.restrictToTopic.invalidTopics
|
|
415
|
+
}
|
|
288
416
|
}
|
|
417
|
+
} : {})
|
|
418
|
+
} : {}), {
|
|
419
|
+
metadata: {
|
|
420
|
+
requestType: import_runtime_client_gql3.CopilotRequestType.Chat
|
|
289
421
|
}
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
422
|
+
}), agentSessionRef.current ? {
|
|
423
|
+
agentSession: agentSessionRef.current
|
|
424
|
+
} : {}), {
|
|
425
|
+
agentStates: Object.values(coagentStatesRef.current).map((state) => ({
|
|
426
|
+
agentName: state.name,
|
|
427
|
+
state: JSON.stringify(state.state)
|
|
428
|
+
}))
|
|
429
|
+
}),
|
|
430
|
+
properties: copilotConfig.properties,
|
|
431
|
+
signal: (_d = chatAbortControllerRef.current) == null ? void 0 : _d.signal
|
|
432
|
+
})
|
|
433
|
+
);
|
|
434
|
+
const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
|
|
435
|
+
const reader = stream.getReader();
|
|
436
|
+
let executedCoAgentStateRenders = [];
|
|
437
|
+
let followUp = void 0;
|
|
438
|
+
let messages2 = [];
|
|
439
|
+
let syncedMessages = [];
|
|
440
|
+
try {
|
|
441
|
+
while (true) {
|
|
442
|
+
let done, value;
|
|
443
|
+
try {
|
|
444
|
+
const readResult = yield reader.read();
|
|
445
|
+
done = readResult.done;
|
|
446
|
+
value = readResult.value;
|
|
447
|
+
} catch (readError) {
|
|
448
|
+
break;
|
|
294
449
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
state: JSON.stringify(state.state)
|
|
301
|
-
}))
|
|
302
|
-
}),
|
|
303
|
-
properties: copilotConfig.properties,
|
|
304
|
-
signal: (_d = chatAbortControllerRef.current) == null ? void 0 : _d.signal
|
|
305
|
-
})
|
|
306
|
-
);
|
|
307
|
-
const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
|
|
308
|
-
const reader = stream.getReader();
|
|
309
|
-
let actionResults = {};
|
|
310
|
-
let executedCoAgentStateRenders = [];
|
|
311
|
-
let followUp = void 0;
|
|
312
|
-
try {
|
|
313
|
-
while (true) {
|
|
314
|
-
let done, value;
|
|
315
|
-
try {
|
|
316
|
-
const readResult = yield reader.read();
|
|
317
|
-
done = readResult.done;
|
|
318
|
-
value = readResult.value;
|
|
319
|
-
} catch (readError) {
|
|
320
|
-
break;
|
|
321
|
-
}
|
|
322
|
-
if (done) {
|
|
323
|
-
if (chatAbortControllerRef.current.signal.aborted) {
|
|
324
|
-
return newMessages.slice();
|
|
450
|
+
if (done) {
|
|
451
|
+
if (chatAbortControllerRef.current.signal.aborted) {
|
|
452
|
+
return [];
|
|
453
|
+
}
|
|
454
|
+
break;
|
|
325
455
|
}
|
|
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
|
-
|
|
352
|
-
|
|
353
|
-
if (!(message.id in actionResults)) {
|
|
456
|
+
if (!(value == null ? void 0 : value.generateCopilotResponse)) {
|
|
457
|
+
continue;
|
|
458
|
+
}
|
|
459
|
+
threadIdRef.current = value.generateCopilotResponse.threadId || null;
|
|
460
|
+
runIdRef.current = value.generateCopilotResponse.runId || null;
|
|
461
|
+
setThreadId(threadIdRef.current);
|
|
462
|
+
setRunId(runIdRef.current);
|
|
463
|
+
messages2 = (0, import_runtime_client_gql3.convertGqlOutputToMessages)(
|
|
464
|
+
(0, import_runtime_client_gql3.filterAdjacentAgentStateMessages)(value.generateCopilotResponse.messages)
|
|
465
|
+
);
|
|
466
|
+
if (messages2.length === 0) {
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
newMessages = [];
|
|
470
|
+
if (((_h = value.generateCopilotResponse.status) == null ? void 0 : _h.__typename) === "FailedResponseStatus" && value.generateCopilotResponse.status.reason === "GUARDRAILS_VALIDATION_FAILED") {
|
|
471
|
+
newMessages = [
|
|
472
|
+
new import_runtime_client_gql3.TextMessage({
|
|
473
|
+
role: import_runtime_client_gql3.MessageRole.Assistant,
|
|
474
|
+
content: ((_i = value.generateCopilotResponse.status.details) == null ? void 0 : _i.guardrailsReason) || ""
|
|
475
|
+
})
|
|
476
|
+
];
|
|
477
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
478
|
+
break;
|
|
479
|
+
} else {
|
|
480
|
+
newMessages = [...messages2];
|
|
481
|
+
for (const message of messages2) {
|
|
482
|
+
if (message.isAgentStateMessage() && !message.active && !executedCoAgentStateRenders.includes(message.id) && onCoAgentStateRender) {
|
|
354
483
|
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
355
484
|
break;
|
|
356
485
|
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
console.error(`Failed to execute action ${message.name}: ${e}`);
|
|
486
|
+
yield onCoAgentStateRender({
|
|
487
|
+
name: message.agentName,
|
|
488
|
+
nodeName: message.nodeName,
|
|
489
|
+
state: message.state
|
|
490
|
+
});
|
|
491
|
+
executedCoAgentStateRenders.push(message.id);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
const lastAgentStateMessage = [...messages2].reverse().find((message) => message.isAgentStateMessage());
|
|
495
|
+
if (lastAgentStateMessage) {
|
|
496
|
+
if (lastAgentStateMessage.state.messages && lastAgentStateMessage.state.messages.length > 0) {
|
|
497
|
+
syncedMessages = (0, import_runtime_client_gql3.loadMessagesFromJsonRepresentation)(
|
|
498
|
+
lastAgentStateMessage.state.messages
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
setCoagentStatesWithRef((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
502
|
+
[lastAgentStateMessage.agentName]: {
|
|
503
|
+
name: lastAgentStateMessage.agentName,
|
|
504
|
+
state: lastAgentStateMessage.state,
|
|
505
|
+
running: lastAgentStateMessage.running,
|
|
506
|
+
active: lastAgentStateMessage.active,
|
|
507
|
+
threadId: lastAgentStateMessage.threadId,
|
|
508
|
+
nodeName: lastAgentStateMessage.nodeName,
|
|
509
|
+
runId: lastAgentStateMessage.runId
|
|
382
510
|
}
|
|
511
|
+
}));
|
|
512
|
+
if (lastAgentStateMessage.running) {
|
|
513
|
+
setAgentSession({
|
|
514
|
+
threadId: lastAgentStateMessage.threadId,
|
|
515
|
+
agentName: lastAgentStateMessage.agentName,
|
|
516
|
+
nodeName: lastAgentStateMessage.nodeName
|
|
517
|
+
});
|
|
518
|
+
} else {
|
|
519
|
+
setAgentSession(null);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
if (newMessages.length > 0) {
|
|
524
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
const finalMessages = constructFinalMessages(syncedMessages, previousMessages, newMessages);
|
|
528
|
+
let didExecuteAction = false;
|
|
529
|
+
if (onFunctionCall) {
|
|
530
|
+
const lastMessages = [];
|
|
531
|
+
for (let i = finalMessages.length - 1; i >= 0; i--) {
|
|
532
|
+
const message = finalMessages[i];
|
|
533
|
+
if (message.isActionExecutionMessage() && message.status.code !== import_runtime_client_gql3.MessageStatusCode.Pending) {
|
|
534
|
+
lastMessages.unshift(message);
|
|
535
|
+
} else {
|
|
536
|
+
break;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
for (const message of lastMessages) {
|
|
540
|
+
setMessages(finalMessages);
|
|
541
|
+
const action = actions.find((action2) => action2.name === message.name);
|
|
542
|
+
if (action) {
|
|
543
|
+
followUp = action.followUp;
|
|
544
|
+
let result;
|
|
545
|
+
try {
|
|
546
|
+
result = yield Promise.race([
|
|
547
|
+
onFunctionCall({
|
|
548
|
+
messages: previousMessages,
|
|
549
|
+
name: message.name,
|
|
550
|
+
args: message.arguments
|
|
551
|
+
}),
|
|
552
|
+
new Promise(
|
|
553
|
+
(resolve) => {
|
|
554
|
+
var _a2;
|
|
555
|
+
return (_a2 = chatAbortControllerRef.current) == null ? void 0 : _a2.signal.addEventListener(
|
|
556
|
+
"abort",
|
|
557
|
+
() => resolve("Operation was aborted by the user")
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
),
|
|
561
|
+
// if the user stopped generation, we also abort consecutive actions
|
|
562
|
+
new Promise((resolve) => {
|
|
563
|
+
var _a2;
|
|
564
|
+
if ((_a2 = chatAbortControllerRef.current) == null ? void 0 : _a2.signal.aborted) {
|
|
565
|
+
resolve("Operation was aborted by the user");
|
|
566
|
+
}
|
|
567
|
+
})
|
|
568
|
+
]);
|
|
569
|
+
} catch (e) {
|
|
570
|
+
result = `Failed to execute action ${message.name}`;
|
|
571
|
+
console.error(`Failed to execute action ${message.name}: ${e}`);
|
|
383
572
|
}
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
573
|
+
didExecuteAction = true;
|
|
574
|
+
const messageIndex = finalMessages.findIndex((msg) => msg.id === message.id);
|
|
575
|
+
finalMessages.splice(
|
|
576
|
+
messageIndex + 1,
|
|
577
|
+
0,
|
|
578
|
+
new import_runtime_client_gql3.ResultMessage({
|
|
579
|
+
id: "result-" + message.id,
|
|
580
|
+
result: import_runtime_client_gql3.ResultMessage.encodeResult(result),
|
|
387
581
|
actionExecutionId: message.id,
|
|
388
582
|
actionName: message.name
|
|
389
583
|
})
|
|
390
584
|
);
|
|
391
585
|
}
|
|
392
|
-
if (message.isAgentStateMessage() && !message.active && !executedCoAgentStateRenders.includes(message.id) && onCoAgentStateRender) {
|
|
393
|
-
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
394
|
-
break;
|
|
395
|
-
}
|
|
396
|
-
yield onCoAgentStateRender({
|
|
397
|
-
name: message.agentName,
|
|
398
|
-
nodeName: message.nodeName,
|
|
399
|
-
state: message.state
|
|
400
|
-
});
|
|
401
|
-
executedCoAgentStateRenders.push(message.id);
|
|
402
|
-
}
|
|
403
586
|
}
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
if (
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
nodeName: lastAgentStateMessage.nodeName
|
|
422
|
-
});
|
|
423
|
-
} else {
|
|
424
|
-
setAgentSession(null);
|
|
587
|
+
setMessages(finalMessages);
|
|
588
|
+
}
|
|
589
|
+
if (
|
|
590
|
+
// if followUp is not explicitly false
|
|
591
|
+
followUp !== false && // and we executed an action
|
|
592
|
+
(didExecuteAction || // the last message is a server side result
|
|
593
|
+
!isAgentRun && finalMessages.length && finalMessages[finalMessages.length - 1].isResultMessage()) && // the user did not stop generation
|
|
594
|
+
!((_j = chatAbortControllerRef.current) == null ? void 0 : _j.signal.aborted)
|
|
595
|
+
) {
|
|
596
|
+
yield new Promise((resolve) => setTimeout(resolve, 10));
|
|
597
|
+
return yield runChatCompletionRef.current(finalMessages);
|
|
598
|
+
} else if ((_k = chatAbortControllerRef.current) == null ? void 0 : _k.signal.aborted) {
|
|
599
|
+
const repairedMessages = finalMessages.filter((message, actionExecutionIndex) => {
|
|
600
|
+
if (message.isActionExecutionMessage()) {
|
|
601
|
+
return finalMessages.find(
|
|
602
|
+
(msg, resultIndex) => msg.isResultMessage() && msg.actionExecutionId === message.id && resultIndex === actionExecutionIndex + 1
|
|
603
|
+
);
|
|
425
604
|
}
|
|
605
|
+
return true;
|
|
606
|
+
});
|
|
607
|
+
const repairedMessageIds = repairedMessages.map((message) => message.id);
|
|
608
|
+
setMessages(repairedMessages);
|
|
609
|
+
if ((_l = agentSessionRef.current) == null ? void 0 : _l.nodeName) {
|
|
610
|
+
setAgentSession({
|
|
611
|
+
threadId: agentSessionRef.current.threadId,
|
|
612
|
+
agentName: agentSessionRef.current.agentName,
|
|
613
|
+
nodeName: "__end__"
|
|
614
|
+
});
|
|
426
615
|
}
|
|
616
|
+
return newMessages.filter((message) => repairedMessageIds.includes(message.id));
|
|
617
|
+
} else {
|
|
618
|
+
return newMessages.slice();
|
|
427
619
|
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
if (
|
|
433
|
-
// if followUp is not explicitly false
|
|
434
|
-
followUp !== false && // if we have client side results
|
|
435
|
-
(Object.values(actionResults).length || // or the last message we received is a result
|
|
436
|
-
newMessages.length && newMessages[newMessages.length - 1].isResultMessage())
|
|
437
|
-
) {
|
|
438
|
-
yield new Promise((resolve) => setTimeout(resolve, 10));
|
|
439
|
-
return yield runChatCompletionRef.current([...previousMessages, ...newMessages]);
|
|
440
|
-
} else {
|
|
441
|
-
return newMessages.slice();
|
|
620
|
+
} finally {
|
|
621
|
+
setIsLoading(false);
|
|
442
622
|
}
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
623
|
+
}),
|
|
624
|
+
[
|
|
625
|
+
messages,
|
|
626
|
+
setMessages,
|
|
627
|
+
makeSystemMessageCallback,
|
|
628
|
+
copilotConfig,
|
|
629
|
+
setIsLoading,
|
|
630
|
+
initialMessages,
|
|
631
|
+
isLoading,
|
|
632
|
+
actions,
|
|
633
|
+
onFunctionCall,
|
|
634
|
+
onCoAgentStateRender,
|
|
635
|
+
setCoagentStatesWithRef,
|
|
636
|
+
coagentStatesRef,
|
|
637
|
+
agentSession,
|
|
638
|
+
setAgentSession
|
|
639
|
+
]
|
|
640
|
+
);
|
|
447
641
|
runChatCompletionRef.current = runChatCompletion;
|
|
448
|
-
const runChatCompletionAndHandleFunctionCall = (
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
642
|
+
const runChatCompletionAndHandleFunctionCall = useAsyncCallback(
|
|
643
|
+
(messages2) => __async(this, null, function* () {
|
|
644
|
+
yield runChatCompletionRef.current(messages2);
|
|
645
|
+
}),
|
|
646
|
+
[messages]
|
|
647
|
+
);
|
|
648
|
+
const append = useAsyncCallback(
|
|
649
|
+
(message) => __async(this, null, function* () {
|
|
650
|
+
if (isLoading) {
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
const newMessages = [...messages, message];
|
|
654
|
+
setMessages(newMessages);
|
|
655
|
+
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
656
|
+
}),
|
|
657
|
+
[isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]
|
|
658
|
+
);
|
|
659
|
+
const reload = useAsyncCallback(() => __async(this, null, function* () {
|
|
460
660
|
if (isLoading || messages.length === 0) {
|
|
461
661
|
return;
|
|
462
662
|
}
|
|
@@ -467,7 +667,7 @@ function useChat(options) {
|
|
|
467
667
|
}
|
|
468
668
|
setMessages(newMessages);
|
|
469
669
|
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
470
|
-
});
|
|
670
|
+
}), [isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]);
|
|
471
671
|
const stop = () => {
|
|
472
672
|
var _a;
|
|
473
673
|
(_a = chatAbortControllerRef.current) == null ? void 0 : _a.abort("Stop was called");
|
|
@@ -475,24 +675,42 @@ function useChat(options) {
|
|
|
475
675
|
return {
|
|
476
676
|
append,
|
|
477
677
|
reload,
|
|
478
|
-
stop
|
|
678
|
+
stop,
|
|
679
|
+
runChatCompletion: () => runChatCompletionRef.current(messages)
|
|
479
680
|
};
|
|
480
681
|
}
|
|
682
|
+
function constructFinalMessages(syncedMessages, previousMessages, newMessages) {
|
|
683
|
+
const finalMessages = syncedMessages.length > 0 ? [...syncedMessages] : [...previousMessages, ...newMessages];
|
|
684
|
+
if (syncedMessages.length > 0) {
|
|
685
|
+
const messagesWithAgentState = [...previousMessages, ...newMessages];
|
|
686
|
+
let previousMessageId = void 0;
|
|
687
|
+
for (const message of messagesWithAgentState) {
|
|
688
|
+
if (message.isAgentStateMessage()) {
|
|
689
|
+
const index = finalMessages.findIndex((msg) => msg.id === previousMessageId);
|
|
690
|
+
if (index !== -1) {
|
|
691
|
+
finalMessages.splice(index + 1, 0, message);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
previousMessageId = message.id;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
return finalMessages;
|
|
698
|
+
}
|
|
481
699
|
|
|
482
700
|
// src/components/copilot-provider/copilotkit.tsx
|
|
483
|
-
var
|
|
701
|
+
var import_react7 = require("react");
|
|
484
702
|
var import_react_dom = require("react-dom");
|
|
485
|
-
var
|
|
703
|
+
var import_shared3 = require("@copilotkit/shared");
|
|
486
704
|
|
|
487
705
|
// src/context/copilot-messages-context.tsx
|
|
488
|
-
var
|
|
706
|
+
var import_react6 = __toESM(require("react"));
|
|
489
707
|
var emptyCopilotContext2 = {
|
|
490
708
|
messages: [],
|
|
491
709
|
setMessages: () => []
|
|
492
710
|
};
|
|
493
|
-
var CopilotMessagesContext =
|
|
711
|
+
var CopilotMessagesContext = import_react6.default.createContext(emptyCopilotContext2);
|
|
494
712
|
function useCopilotMessagesContext() {
|
|
495
|
-
const context =
|
|
713
|
+
const context = import_react6.default.useContext(CopilotMessagesContext);
|
|
496
714
|
if (context === emptyCopilotContext2) {
|
|
497
715
|
throw new Error(
|
|
498
716
|
"A messages consuming component was not wrapped with `<CopilotMessages> {...} </CopilotMessages>`"
|
|
@@ -502,7 +720,7 @@ function useCopilotMessagesContext() {
|
|
|
502
720
|
}
|
|
503
721
|
|
|
504
722
|
// src/components/copilot-provider/copilotkit.tsx
|
|
505
|
-
var
|
|
723
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
506
724
|
var defaultCopilotContextCategories = ["global"];
|
|
507
725
|
|
|
508
726
|
// src/hooks/use-copilot-chat.ts
|
|
@@ -520,8 +738,8 @@ function useCopilotChat(_a = {}) {
|
|
|
520
738
|
setIsLoading,
|
|
521
739
|
chatInstructions,
|
|
522
740
|
actions,
|
|
523
|
-
|
|
524
|
-
|
|
741
|
+
coagentStatesRef,
|
|
742
|
+
setCoagentStatesWithRef,
|
|
525
743
|
coAgentStateRenders,
|
|
526
744
|
agentSession,
|
|
527
745
|
setAgentSession,
|
|
@@ -534,21 +752,21 @@ function useCopilotChat(_a = {}) {
|
|
|
534
752
|
} = useCopilotContext();
|
|
535
753
|
const { messages, setMessages } = useCopilotMessagesContext();
|
|
536
754
|
const latestGetContextString = useUpdatedRef(getContextString);
|
|
537
|
-
const deleteMessage = (0,
|
|
755
|
+
const deleteMessage = (0, import_react8.useCallback)(
|
|
538
756
|
(messageId) => {
|
|
539
757
|
setMessages((prev) => prev.filter((message) => message.id !== messageId));
|
|
540
758
|
},
|
|
541
759
|
[setMessages]
|
|
542
760
|
);
|
|
543
|
-
const makeSystemMessageCallback = (0,
|
|
761
|
+
const makeSystemMessageCallback = (0, import_react8.useCallback)(() => {
|
|
544
762
|
const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
|
|
545
763
|
const contextString = latestGetContextString.current([], defaultCopilotContextCategories);
|
|
546
|
-
return new
|
|
764
|
+
return new import_runtime_client_gql4.TextMessage({
|
|
547
765
|
content: systemMessageMaker(contextString, chatInstructions),
|
|
548
|
-
role:
|
|
766
|
+
role: import_runtime_client_gql4.Role.System
|
|
549
767
|
});
|
|
550
768
|
}, [getContextString, makeSystemMessage, chatInstructions]);
|
|
551
|
-
const onCoAgentStateRender = (
|
|
769
|
+
const onCoAgentStateRender = useAsyncCallback(
|
|
552
770
|
(args) => __async(this, null, function* () {
|
|
553
771
|
var _a2;
|
|
554
772
|
const { name, nodeName, state } = args;
|
|
@@ -566,7 +784,7 @@ function useCopilotChat(_a = {}) {
|
|
|
566
784
|
}),
|
|
567
785
|
[coAgentStateRenders]
|
|
568
786
|
);
|
|
569
|
-
const { append, reload, stop } = useChat(__spreadProps(__spreadValues({}, options), {
|
|
787
|
+
const { append, reload, stop, runChatCompletion } = useChat(__spreadProps(__spreadValues({}, options), {
|
|
570
788
|
actions: Object.values(actions),
|
|
571
789
|
copilotConfig: copilotApiConfig,
|
|
572
790
|
initialMessages: options.initialMessages || [],
|
|
@@ -577,8 +795,8 @@ function useCopilotChat(_a = {}) {
|
|
|
577
795
|
makeSystemMessageCallback,
|
|
578
796
|
isLoading,
|
|
579
797
|
setIsLoading,
|
|
580
|
-
|
|
581
|
-
|
|
798
|
+
coagentStatesRef,
|
|
799
|
+
setCoagentStatesWithRef,
|
|
582
800
|
agentSession,
|
|
583
801
|
setAgentSession,
|
|
584
802
|
threadId,
|
|
@@ -588,40 +806,44 @@ function useCopilotChat(_a = {}) {
|
|
|
588
806
|
chatAbortControllerRef
|
|
589
807
|
}));
|
|
590
808
|
const latestAppend = useUpdatedRef(append);
|
|
591
|
-
const latestAppendFunc = (
|
|
592
|
-
(message) => {
|
|
593
|
-
return latestAppend.current(message);
|
|
594
|
-
},
|
|
809
|
+
const latestAppendFunc = useAsyncCallback(
|
|
810
|
+
(message) => __async(this, null, function* () {
|
|
811
|
+
return yield latestAppend.current(message);
|
|
812
|
+
}),
|
|
595
813
|
[latestAppend]
|
|
596
814
|
);
|
|
597
815
|
const latestReload = useUpdatedRef(reload);
|
|
598
|
-
const latestReloadFunc = (
|
|
599
|
-
return latestReload.current();
|
|
600
|
-
}, [latestReload]);
|
|
816
|
+
const latestReloadFunc = useAsyncCallback(() => __async(this, null, function* () {
|
|
817
|
+
return yield latestReload.current();
|
|
818
|
+
}), [latestReload]);
|
|
601
819
|
const latestStop = useUpdatedRef(stop);
|
|
602
|
-
const latestStopFunc = (0,
|
|
820
|
+
const latestStopFunc = (0, import_react8.useCallback)(() => {
|
|
603
821
|
return latestStop.current();
|
|
604
822
|
}, [latestStop]);
|
|
605
823
|
const latestDelete = useUpdatedRef(deleteMessage);
|
|
606
|
-
const latestDeleteFunc = (0,
|
|
824
|
+
const latestDeleteFunc = (0, import_react8.useCallback)(
|
|
607
825
|
(messageId) => {
|
|
608
826
|
return latestDelete.current(messageId);
|
|
609
827
|
},
|
|
610
828
|
[latestDelete]
|
|
611
829
|
);
|
|
612
830
|
const latestSetMessages = useUpdatedRef(setMessages);
|
|
613
|
-
const latestSetMessagesFunc = (0,
|
|
831
|
+
const latestSetMessagesFunc = (0, import_react8.useCallback)(
|
|
614
832
|
(messages2) => {
|
|
615
833
|
return latestSetMessages.current(messages2);
|
|
616
834
|
},
|
|
617
835
|
[latestSetMessages]
|
|
618
836
|
);
|
|
619
|
-
const
|
|
837
|
+
const latestRunChatCompletion = useUpdatedRef(runChatCompletion);
|
|
838
|
+
const latestRunChatCompletionFunc = useAsyncCallback(() => __async(this, null, function* () {
|
|
839
|
+
return yield latestRunChatCompletion.current();
|
|
840
|
+
}), [latestRunChatCompletion]);
|
|
841
|
+
const reset = (0, import_react8.useCallback)(() => {
|
|
620
842
|
latestStopFunc();
|
|
621
843
|
setMessages([]);
|
|
622
844
|
setThreadId(null);
|
|
623
845
|
setRunId(null);
|
|
624
|
-
|
|
846
|
+
setCoagentStatesWithRef({});
|
|
625
847
|
let initialAgentSession = null;
|
|
626
848
|
if (agentLock) {
|
|
627
849
|
initialAgentSession = {
|
|
@@ -629,9 +851,16 @@ function useCopilotChat(_a = {}) {
|
|
|
629
851
|
};
|
|
630
852
|
}
|
|
631
853
|
setAgentSession(initialAgentSession);
|
|
632
|
-
}, [
|
|
854
|
+
}, [
|
|
855
|
+
latestStopFunc,
|
|
856
|
+
setMessages,
|
|
857
|
+
setThreadId,
|
|
858
|
+
setCoagentStatesWithRef,
|
|
859
|
+
setAgentSession,
|
|
860
|
+
agentLock
|
|
861
|
+
]);
|
|
633
862
|
const latestReset = useUpdatedRef(reset);
|
|
634
|
-
const latestResetFunc = (0,
|
|
863
|
+
const latestResetFunc = (0, import_react8.useCallback)(() => {
|
|
635
864
|
return latestReset.current();
|
|
636
865
|
}, [latestReset]);
|
|
637
866
|
return {
|
|
@@ -642,12 +871,13 @@ function useCopilotChat(_a = {}) {
|
|
|
642
871
|
stopGeneration: latestStopFunc,
|
|
643
872
|
reset: latestResetFunc,
|
|
644
873
|
deleteMessage: latestDeleteFunc,
|
|
874
|
+
runChatCompletion: latestRunChatCompletionFunc,
|
|
645
875
|
isLoading
|
|
646
876
|
};
|
|
647
877
|
}
|
|
648
878
|
function useUpdatedRef(value) {
|
|
649
|
-
const ref = (0,
|
|
650
|
-
(0,
|
|
879
|
+
const ref = (0, import_react8.useRef)(value);
|
|
880
|
+
(0, import_react8.useEffect)(() => {
|
|
651
881
|
ref.current = value;
|
|
652
882
|
}, [value]);
|
|
653
883
|
return ref;
|