@copilotkit/react-core 1.5.1-next.2 → 1.5.1-next.3
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 +9 -0
- package/dist/{chunk-LNI2Z45S.mjs → chunk-2KCEHGSI.mjs} +50 -100
- package/dist/chunk-2KCEHGSI.mjs.map +1 -0
- package/dist/{chunk-2KT73RVG.mjs → chunk-6OR25P32.mjs} +19 -75
- package/dist/chunk-6OR25P32.mjs.map +1 -0
- package/dist/{chunk-3GQPEN4M.mjs → chunk-DDVXFNYA.mjs} +32 -66
- package/dist/chunk-DDVXFNYA.mjs.map +1 -0
- package/dist/{chunk-XOQMISG3.mjs → chunk-FXHKK52V.mjs} +13 -14
- package/dist/chunk-FXHKK52V.mjs.map +1 -0
- package/dist/{chunk-HIUT2NLX.mjs → chunk-H4VZMKR6.mjs} +2 -2
- package/dist/{chunk-X36DNILG.mjs → chunk-H7LSKIDK.mjs} +23 -71
- package/dist/chunk-H7LSKIDK.mjs.map +1 -0
- package/dist/chunk-JDQCJCAQ.mjs +295 -0
- package/dist/chunk-JDQCJCAQ.mjs.map +1 -0
- package/dist/chunk-L34MHAXR.mjs +1 -0
- package/dist/{chunk-WXREY7UP.mjs → chunk-LODRWFMB.mjs} +4 -16
- package/dist/chunk-LODRWFMB.mjs.map +1 -0
- package/dist/{chunk-EWKJMQOF.mjs → chunk-MWZO6TUR.mjs} +13 -29
- package/dist/chunk-MWZO6TUR.mjs.map +1 -0
- package/dist/{chunk-6BQVZFNO.mjs → chunk-ODN4H66E.mjs} +2 -6
- package/dist/chunk-ODN4H66E.mjs.map +1 -0
- package/dist/{chunk-FQ53HQYZ.mjs → chunk-QDGDXRRJ.mjs} +2 -2
- package/dist/{chunk-22XKTBPF.mjs → chunk-XINZBP3J.mjs} +3 -15
- package/dist/chunk-XINZBP3J.mjs.map +1 -0
- package/dist/components/copilot-provider/copilot-messages.d.ts +0 -9
- package/dist/components/copilot-provider/copilotkit-props.d.ts +0 -21
- package/dist/components/copilot-provider/copilotkit-props.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.d.ts +0 -9
- package/dist/components/copilot-provider/copilotkit.js +95 -224
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +8 -9
- package/dist/components/copilot-provider/index.d.ts +0 -9
- package/dist/components/copilot-provider/index.js +95 -224
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +8 -9
- package/dist/components/index.d.ts +0 -9
- package/dist/components/index.js +95 -224
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +8 -9
- package/dist/components/toast/toast-provider.d.ts +1 -2
- package/dist/components/toast/toast-provider.js +63 -77
- 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 -37
- package/dist/context/copilot-context.js +3 -15
- 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 -15
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +4 -4
- package/dist/hooks/index.d.ts +1 -3
- package/dist/hooks/index.js +297 -703
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +23 -35
- package/dist/hooks/use-chat.d.ts +3 -35
- package/dist/hooks/use-chat.js +205 -453
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +3 -4
- package/dist/hooks/use-coagent-state-render.d.ts +2 -2
- package/dist/hooks/use-coagent-state-render.js +8 -43
- package/dist/hooks/use-coagent-state-render.js.map +1 -1
- package/dist/hooks/use-coagent-state-render.mjs +2 -4
- package/dist/hooks/use-coagent.d.ts +1 -14
- package/dist/hooks/use-coagent.js +265 -603
- package/dist/hooks/use-coagent.js.map +1 -1
- package/dist/hooks/use-coagent.mjs +12 -20
- package/dist/hooks/use-copilot-action.d.ts +2 -12
- package/dist/hooks/use-copilot-action.js +19 -169
- package/dist/hooks/use-copilot-action.js.map +1 -1
- package/dist/hooks/use-copilot-action.mjs +2 -4
- package/dist/hooks/use-copilot-chat.d.ts +0 -2
- package/dist/hooks/use-copilot-chat.js +240 -545
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +11 -13
- package/dist/hooks/use-copilot-readable.js +3 -15
- 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 +4 -110
- 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 -15
- 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 +3 -4
- package/dist/index.js +398 -812
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +31 -43
- package/dist/lib/copilot-task.d.ts +1 -6
- package/dist/lib/copilot-task.js +17 -55
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +9 -11
- package/dist/lib/index.d.ts +1 -1
- package/dist/lib/index.js +17 -55
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +9 -11
- package/dist/types/frontend-action.d.ts +2 -21
- package/dist/types/frontend-action.js +0 -34
- package/dist/types/frontend-action.js.map +1 -1
- package/dist/types/frontend-action.mjs +0 -7
- package/dist/types/index.d.ts +1 -2
- package/dist/types/index.js.map +1 -1
- 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 +8 -9
- package/dist/utils/index.js +3 -21
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +9 -10
- package/package.json +5 -5
- package/dist/chunk-22XKTBPF.mjs.map +0 -1
- package/dist/chunk-2KT73RVG.mjs.map +0 -1
- package/dist/chunk-3GQPEN4M.mjs.map +0 -1
- package/dist/chunk-6BQVZFNO.mjs.map +0 -1
- package/dist/chunk-EWKJMQOF.mjs.map +0 -1
- package/dist/chunk-LHKIIAUQ.mjs +0 -59
- package/dist/chunk-LHKIIAUQ.mjs.map +0 -1
- package/dist/chunk-LNI2Z45S.mjs.map +0 -1
- package/dist/chunk-NR2BTA6Z.mjs +0 -1
- package/dist/chunk-VGOFRULA.mjs +0 -401
- package/dist/chunk-VGOFRULA.mjs.map +0 -1
- package/dist/chunk-VNTDIJSG.mjs +0 -37
- package/dist/chunk-VNTDIJSG.mjs.map +0 -1
- package/dist/chunk-WXREY7UP.mjs.map +0 -1
- package/dist/chunk-X36DNILG.mjs.map +0 -1
- package/dist/chunk-XERJQUHA.mjs +0 -31
- package/dist/chunk-XERJQUHA.mjs.map +0 -1
- package/dist/chunk-XOQMISG3.mjs.map +0 -1
- package/dist/components/error-boundary/error-boundary.d.ts +0 -22
- package/dist/components/error-boundary/error-boundary.js +0 -183
- package/dist/components/error-boundary/error-boundary.js.map +0 -1
- package/dist/components/error-boundary/error-boundary.mjs +0 -12
- package/dist/components/error-boundary/error-boundary.mjs.map +0 -1
- package/dist/components/error-boundary/error-utils.d.ts +0 -11
- package/dist/components/error-boundary/error-utils.js +0 -177
- package/dist/components/error-boundary/error-utils.js.map +0 -1
- package/dist/components/error-boundary/error-utils.mjs +0 -13
- package/dist/components/error-boundary/error-utils.mjs.map +0 -1
- package/dist/hooks/use-copilot-authenticated-action.d.ts +0 -8
- package/dist/hooks/use-copilot-authenticated-action.js +0 -418
- package/dist/hooks/use-copilot-authenticated-action.js.map +0 -1
- package/dist/hooks/use-copilot-authenticated-action.mjs +0 -12
- package/dist/hooks/use-copilot-authenticated-action.mjs.map +0 -1
- /package/dist/{chunk-HIUT2NLX.mjs.map → chunk-H4VZMKR6.mjs.map} +0 -0
- /package/dist/{chunk-NR2BTA6Z.mjs.map → chunk-L34MHAXR.mjs.map} +0 -0
- /package/dist/{chunk-FQ53HQYZ.mjs.map → chunk-QDGDXRRJ.mjs.map} +0 -0
package/dist/hooks/index.js
CHANGED
|
@@ -79,13 +79,9 @@ var __async = (__this, __arguments, generator) => {
|
|
|
79
79
|
// src/hooks/index.ts
|
|
80
80
|
var hooks_exports = {};
|
|
81
81
|
__export(hooks_exports, {
|
|
82
|
-
runAgent: () => runAgent,
|
|
83
|
-
startAgent: () => startAgent,
|
|
84
|
-
stopAgent: () => stopAgent,
|
|
85
82
|
useCoAgent: () => useCoAgent,
|
|
86
83
|
useCoAgentStateRender: () => useCoAgentStateRender,
|
|
87
84
|
useCopilotAction: () => useCopilotAction,
|
|
88
|
-
useCopilotAuthenticatedAction: () => useCopilotAuthenticatedAction,
|
|
89
85
|
useCopilotChat: () => useCopilotChat,
|
|
90
86
|
useCopilotReadable: () => useCopilotReadable,
|
|
91
87
|
useCopilotRuntimeClient: () => useCopilotRuntimeClient,
|
|
@@ -94,7 +90,7 @@ __export(hooks_exports, {
|
|
|
94
90
|
module.exports = __toCommonJS(hooks_exports);
|
|
95
91
|
|
|
96
92
|
// src/hooks/use-copilot-chat.ts
|
|
97
|
-
var
|
|
93
|
+
var import_react7 = require("react");
|
|
98
94
|
|
|
99
95
|
// src/context/copilot-context.tsx
|
|
100
96
|
var import_react = __toESM(require("react"));
|
|
@@ -145,22 +141,9 @@ var emptyCopilotContext = {
|
|
|
145
141
|
coagentStates: {},
|
|
146
142
|
setCoagentStates: () => {
|
|
147
143
|
},
|
|
148
|
-
coagentStatesRef: { current: {} },
|
|
149
|
-
setCoagentStatesWithRef: () => {
|
|
150
|
-
},
|
|
151
144
|
agentSession: null,
|
|
152
145
|
setAgentSession: () => {
|
|
153
|
-
}
|
|
154
|
-
forwardedParameters: {},
|
|
155
|
-
agentLock: null,
|
|
156
|
-
threadId: null,
|
|
157
|
-
setThreadId: () => {
|
|
158
|
-
},
|
|
159
|
-
runId: null,
|
|
160
|
-
setRunId: () => {
|
|
161
|
-
},
|
|
162
|
-
chatAbortControllerRef: { current: null },
|
|
163
|
-
availableAgents: []
|
|
146
|
+
}
|
|
164
147
|
};
|
|
165
148
|
var CopilotContext = import_react.default.createContext(emptyCopilotContext);
|
|
166
149
|
function useCopilotContext() {
|
|
@@ -170,167 +153,25 @@ function useCopilotContext() {
|
|
|
170
153
|
}
|
|
171
154
|
return context;
|
|
172
155
|
}
|
|
173
|
-
function returnAndThrowInDebug(
|
|
156
|
+
function returnAndThrowInDebug(value) {
|
|
174
157
|
throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
|
|
158
|
+
return value;
|
|
175
159
|
}
|
|
176
160
|
|
|
177
161
|
// src/hooks/use-copilot-chat.ts
|
|
178
|
-
var import_runtime_client_gql4 = require("@copilotkit/runtime-client-gql");
|
|
179
|
-
|
|
180
|
-
// src/hooks/use-chat.ts
|
|
181
|
-
var import_react5 = require("react");
|
|
182
|
-
var import_shared2 = require("@copilotkit/shared");
|
|
183
162
|
var import_runtime_client_gql3 = require("@copilotkit/runtime-client-gql");
|
|
184
163
|
|
|
185
|
-
// src/
|
|
186
|
-
var
|
|
164
|
+
// src/hooks/use-chat.ts
|
|
165
|
+
var import_react4 = require("react");
|
|
187
166
|
var import_shared = require("@copilotkit/shared");
|
|
188
|
-
function processActionsForRuntimeRequest(actions) {
|
|
189
|
-
const filteredActions = actions.filter(
|
|
190
|
-
(action) => action.available !== import_runtime_client_gql.ActionInputAvailability.Disabled && action.disabled !== true && action.name !== "*"
|
|
191
|
-
).map((action) => {
|
|
192
|
-
let available = import_runtime_client_gql.ActionInputAvailability.Enabled;
|
|
193
|
-
if (action.disabled) {
|
|
194
|
-
available = import_runtime_client_gql.ActionInputAvailability.Disabled;
|
|
195
|
-
} else if (action.available === "disabled") {
|
|
196
|
-
available = import_runtime_client_gql.ActionInputAvailability.Disabled;
|
|
197
|
-
} else if (action.available === "remote") {
|
|
198
|
-
available = import_runtime_client_gql.ActionInputAvailability.Remote;
|
|
199
|
-
}
|
|
200
|
-
return {
|
|
201
|
-
name: action.name,
|
|
202
|
-
description: action.description || "",
|
|
203
|
-
jsonSchema: JSON.stringify((0, import_shared.actionParametersToJsonSchema)(action.parameters || [])),
|
|
204
|
-
available
|
|
205
|
-
};
|
|
206
|
-
});
|
|
207
|
-
return filteredActions;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// src/hooks/use-copilot-runtime-client.ts
|
|
211
167
|
var import_runtime_client_gql2 = require("@copilotkit/runtime-client-gql");
|
|
212
168
|
|
|
213
169
|
// src/components/toast/toast-provider.tsx
|
|
214
|
-
var import_react3 = require("react");
|
|
215
|
-
|
|
216
|
-
// src/components/error-boundary/error-utils.tsx
|
|
217
170
|
var import_react2 = require("react");
|
|
218
|
-
|
|
219
|
-
// src/components/toast/exclamation-mark-icon.tsx
|
|
220
171
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
221
|
-
var
|
|
222
|
-
className,
|
|
223
|
-
style
|
|
224
|
-
}) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
|
|
225
|
-
"svg",
|
|
226
|
-
{
|
|
227
|
-
xmlns: "http://www.w3.org/2000/svg",
|
|
228
|
-
width: "24",
|
|
229
|
-
height: "24",
|
|
230
|
-
viewBox: "0 0 24 24",
|
|
231
|
-
fill: "none",
|
|
232
|
-
stroke: "currentColor",
|
|
233
|
-
strokeWidth: "2",
|
|
234
|
-
strokeLinecap: "round",
|
|
235
|
-
strokeLinejoin: "round",
|
|
236
|
-
className: `lucide lucide-circle-alert ${className ? className : ""}`,
|
|
237
|
-
style,
|
|
238
|
-
children: [
|
|
239
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("circle", { cx: "12", cy: "12", r: "10" }),
|
|
240
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("line", { x1: "12", x2: "12", y1: "8", y2: "12" }),
|
|
241
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("line", { x1: "12", x2: "12.01", y1: "16", y2: "16" })
|
|
242
|
-
]
|
|
243
|
-
}
|
|
244
|
-
);
|
|
245
|
-
|
|
246
|
-
// src/components/error-boundary/error-utils.tsx
|
|
247
|
-
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
248
|
-
function ErrorToast({ errors }) {
|
|
249
|
-
const errorsToRender = errors.map((error, idx) => {
|
|
250
|
-
var _a, _b, _c;
|
|
251
|
-
const message = "extensions" in error ? (_b = (_a = error.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message : error.message;
|
|
252
|
-
const code = "extensions" in error ? (_c = error.extensions) == null ? void 0 : _c.code : null;
|
|
253
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
254
|
-
"div",
|
|
255
|
-
{
|
|
256
|
-
style: {
|
|
257
|
-
marginTop: idx === 0 ? 0 : 10,
|
|
258
|
-
marginBottom: 14
|
|
259
|
-
},
|
|
260
|
-
children: [
|
|
261
|
-
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ExclamationMarkIcon, { style: { marginBottom: 4 } }),
|
|
262
|
-
code && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
263
|
-
"div",
|
|
264
|
-
{
|
|
265
|
-
style: {
|
|
266
|
-
fontWeight: "600",
|
|
267
|
-
marginBottom: 4
|
|
268
|
-
},
|
|
269
|
-
children: [
|
|
270
|
-
"Copilot Cloud Error:",
|
|
271
|
-
" ",
|
|
272
|
-
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { style: { fontFamily: "monospace", fontWeight: "normal" }, children: code })
|
|
273
|
-
]
|
|
274
|
-
}
|
|
275
|
-
),
|
|
276
|
-
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { children: message })
|
|
277
|
-
]
|
|
278
|
-
},
|
|
279
|
-
idx
|
|
280
|
-
);
|
|
281
|
-
});
|
|
282
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
283
|
-
"div",
|
|
284
|
-
{
|
|
285
|
-
style: {
|
|
286
|
-
fontSize: "13px",
|
|
287
|
-
maxWidth: "600px"
|
|
288
|
-
},
|
|
289
|
-
children: [
|
|
290
|
-
errorsToRender,
|
|
291
|
-
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { fontSize: "11px", opacity: 0.75 }, children: "NOTE: This error only displays during local development." })
|
|
292
|
-
]
|
|
293
|
-
}
|
|
294
|
-
);
|
|
295
|
-
}
|
|
296
|
-
function useErrorToast() {
|
|
297
|
-
const { addToast } = useToast();
|
|
298
|
-
return (0, import_react2.useCallback)(
|
|
299
|
-
(error) => {
|
|
300
|
-
const errorId = error.map((err) => {
|
|
301
|
-
var _a, _b;
|
|
302
|
-
const message = "extensions" in err ? ((_b = (_a = err.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message) || err.message : err.message;
|
|
303
|
-
const stack = err.stack || "";
|
|
304
|
-
return btoa(message + stack).slice(0, 32);
|
|
305
|
-
}).join("|");
|
|
306
|
-
addToast({
|
|
307
|
-
type: "error",
|
|
308
|
-
id: errorId,
|
|
309
|
-
// Toast libraries typically dedupe by id
|
|
310
|
-
message: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ErrorToast, { errors: error })
|
|
311
|
-
});
|
|
312
|
-
},
|
|
313
|
-
[addToast]
|
|
314
|
-
);
|
|
315
|
-
}
|
|
316
|
-
function useAsyncCallback(callback, deps) {
|
|
317
|
-
const addErrorToast = useErrorToast();
|
|
318
|
-
return (0, import_react2.useCallback)((...args) => __async(this, null, function* () {
|
|
319
|
-
try {
|
|
320
|
-
return yield callback(...args);
|
|
321
|
-
} catch (error) {
|
|
322
|
-
console.error("Error in async callback:", error);
|
|
323
|
-
addErrorToast([error]);
|
|
324
|
-
throw error;
|
|
325
|
-
}
|
|
326
|
-
}), deps);
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
// src/components/toast/toast-provider.tsx
|
|
330
|
-
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
331
|
-
var ToastContext = (0, import_react3.createContext)(void 0);
|
|
172
|
+
var ToastContext = (0, import_react2.createContext)(void 0);
|
|
332
173
|
function useToast() {
|
|
333
|
-
const context = (0,
|
|
174
|
+
const context = (0, import_react2.useContext)(ToastContext);
|
|
334
175
|
if (!context) {
|
|
335
176
|
throw new Error("useToast must be used within a ToastProvider");
|
|
336
177
|
}
|
|
@@ -338,17 +179,15 @@ function useToast() {
|
|
|
338
179
|
}
|
|
339
180
|
|
|
340
181
|
// src/hooks/use-copilot-runtime-client.ts
|
|
341
|
-
var
|
|
182
|
+
var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
|
|
183
|
+
var import_react3 = require("react");
|
|
342
184
|
var useCopilotRuntimeClient = (options) => {
|
|
343
185
|
const { addGraphQLErrorsToast } = useToast();
|
|
344
|
-
const
|
|
345
|
-
|
|
346
|
-
return new import_runtime_client_gql2.CopilotRuntimeClient(__spreadProps(__spreadValues({}, options), {
|
|
186
|
+
const runtimeClient = (0, import_react3.useMemo)(() => {
|
|
187
|
+
return new import_runtime_client_gql.CopilotRuntimeClient(__spreadProps(__spreadValues({}, options), {
|
|
347
188
|
handleGQLErrors: (error) => {
|
|
348
189
|
if (error.graphQLErrors.length) {
|
|
349
190
|
addGraphQLErrorsToast(error.graphQLErrors);
|
|
350
|
-
} else {
|
|
351
|
-
addErrorToast([error]);
|
|
352
191
|
}
|
|
353
192
|
}
|
|
354
193
|
}));
|
|
@@ -369,317 +208,232 @@ function useChat(options) {
|
|
|
369
208
|
actions,
|
|
370
209
|
onFunctionCall,
|
|
371
210
|
onCoAgentStateRender,
|
|
372
|
-
|
|
373
|
-
|
|
211
|
+
setCoagentStates,
|
|
212
|
+
coagentStates,
|
|
374
213
|
agentSession,
|
|
375
|
-
setAgentSession
|
|
376
|
-
threadId,
|
|
377
|
-
setThreadId,
|
|
378
|
-
runId,
|
|
379
|
-
setRunId,
|
|
380
|
-
chatAbortControllerRef,
|
|
381
|
-
agentLock
|
|
214
|
+
setAgentSession
|
|
382
215
|
} = options;
|
|
383
|
-
const
|
|
384
|
-
const
|
|
216
|
+
const abortControllerRef = (0, import_react4.useRef)();
|
|
217
|
+
const threadIdRef = (0, import_react4.useRef)(null);
|
|
218
|
+
const runIdRef = (0, import_react4.useRef)(null);
|
|
219
|
+
const { addGraphQLErrorsToast } = useToast();
|
|
220
|
+
const runChatCompletionRef = (0, import_react4.useRef)();
|
|
221
|
+
const coagentStatesRef = (0, import_react4.useRef)(coagentStates);
|
|
222
|
+
coagentStatesRef.current = coagentStates;
|
|
223
|
+
const agentSessionRef = (0, import_react4.useRef)(agentSession);
|
|
385
224
|
agentSessionRef.current = agentSession;
|
|
386
|
-
const threadIdRef = (0, import_react5.useRef)(threadId);
|
|
387
|
-
threadIdRef.current = threadId;
|
|
388
|
-
const runIdRef = (0, import_react5.useRef)(runId);
|
|
389
|
-
runIdRef.current = runId;
|
|
390
225
|
const publicApiKey = copilotConfig.publicApiKey;
|
|
391
|
-
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [
|
|
226
|
+
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [import_shared.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
|
|
392
227
|
const runtimeClient = useCopilotRuntimeClient({
|
|
393
228
|
url: copilotConfig.chatApiEndpoint,
|
|
394
229
|
publicApiKey: copilotConfig.publicApiKey,
|
|
395
230
|
headers,
|
|
396
231
|
credentials: copilotConfig.credentials
|
|
397
232
|
});
|
|
398
|
-
const runChatCompletion =
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
233
|
+
const runChatCompletion = (previousMessages) => __async(this, null, function* () {
|
|
234
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
235
|
+
setIsLoading(true);
|
|
236
|
+
let newMessages = [
|
|
237
|
+
new import_runtime_client_gql2.TextMessage({
|
|
238
|
+
content: "",
|
|
239
|
+
role: import_runtime_client_gql2.Role.Assistant
|
|
240
|
+
})
|
|
241
|
+
];
|
|
242
|
+
const abortController = new AbortController();
|
|
243
|
+
abortControllerRef.current = abortController;
|
|
244
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
245
|
+
const systemMessage = makeSystemMessageCallback();
|
|
246
|
+
const messagesWithContext = [systemMessage, ...initialMessages || [], ...previousMessages];
|
|
247
|
+
const stream = runtimeClient.asStream(
|
|
248
|
+
runtimeClient.generateCopilotResponse({
|
|
249
|
+
data: __spreadProps(__spreadValues(__spreadProps(__spreadValues({
|
|
250
|
+
frontend: {
|
|
251
|
+
actions: actions.filter(
|
|
252
|
+
(action) => action.available !== import_runtime_client_gql2.ActionInputAvailability.Disabled || !action.disabled
|
|
253
|
+
).map((action) => {
|
|
254
|
+
let available = import_runtime_client_gql2.ActionInputAvailability.Enabled;
|
|
255
|
+
if (action.disabled) {
|
|
256
|
+
available = import_runtime_client_gql2.ActionInputAvailability.Disabled;
|
|
257
|
+
} else if (action.available === "disabled") {
|
|
258
|
+
available = import_runtime_client_gql2.ActionInputAvailability.Disabled;
|
|
259
|
+
} else if (action.available === "remote") {
|
|
260
|
+
available = import_runtime_client_gql2.ActionInputAvailability.Remote;
|
|
261
|
+
}
|
|
262
|
+
return {
|
|
263
|
+
name: action.name,
|
|
264
|
+
description: action.description || "",
|
|
265
|
+
jsonSchema: JSON.stringify((0, import_shared.actionParametersToJsonSchema)(action.parameters || [])),
|
|
266
|
+
available
|
|
267
|
+
};
|
|
268
|
+
}),
|
|
269
|
+
url: window.location.href
|
|
270
|
+
},
|
|
271
|
+
threadId: threadIdRef.current,
|
|
272
|
+
runId: runIdRef.current,
|
|
273
|
+
messages: (0, import_runtime_client_gql2.convertMessagesToGqlInput)((0, import_runtime_client_gql2.filterAgentStateMessages)(messagesWithContext))
|
|
274
|
+
}, copilotConfig.cloud ? {
|
|
275
|
+
cloud: __spreadValues({}, ((_c = (_b = (_a = copilotConfig.cloud.guardrails) == null ? void 0 : _a.input) == null ? void 0 : _b.restrictToTopic) == null ? void 0 : _c.enabled) ? {
|
|
276
|
+
guardrails: {
|
|
277
|
+
inputValidationRules: {
|
|
278
|
+
allowList: copilotConfig.cloud.guardrails.input.restrictToTopic.validTopics,
|
|
279
|
+
denyList: copilotConfig.cloud.guardrails.input.restrictToTopic.invalidTopics
|
|
430
280
|
}
|
|
431
|
-
} : {})
|
|
432
|
-
} : {}), {
|
|
433
|
-
metadata: {
|
|
434
|
-
requestType: import_runtime_client_gql3.CopilotRequestType.Chat
|
|
435
|
-
}
|
|
436
|
-
}), agentSessionRef.current ? {
|
|
437
|
-
agentSession: agentSessionRef.current
|
|
438
|
-
} : {}), {
|
|
439
|
-
agentStates: Object.values(coagentStatesRef.current).map((state) => ({
|
|
440
|
-
agentName: state.name,
|
|
441
|
-
state: JSON.stringify(state.state)
|
|
442
|
-
})),
|
|
443
|
-
forwardedParameters: options.forwardedParameters || {}
|
|
444
|
-
}),
|
|
445
|
-
properties: copilotConfig.properties,
|
|
446
|
-
signal: (_d = chatAbortControllerRef.current) == null ? void 0 : _d.signal
|
|
447
|
-
})
|
|
448
|
-
);
|
|
449
|
-
const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
|
|
450
|
-
const reader = stream.getReader();
|
|
451
|
-
let executedCoAgentStateRenders = [];
|
|
452
|
-
let followUp = void 0;
|
|
453
|
-
let messages2 = [];
|
|
454
|
-
let syncedMessages = [];
|
|
455
|
-
try {
|
|
456
|
-
while (true) {
|
|
457
|
-
let done, value;
|
|
458
|
-
try {
|
|
459
|
-
const readResult = yield reader.read();
|
|
460
|
-
done = readResult.done;
|
|
461
|
-
value = readResult.value;
|
|
462
|
-
} catch (readError) {
|
|
463
|
-
break;
|
|
464
|
-
}
|
|
465
|
-
if (done) {
|
|
466
|
-
if (chatAbortControllerRef.current.signal.aborted) {
|
|
467
|
-
return [];
|
|
468
281
|
}
|
|
469
|
-
|
|
282
|
+
} : {})
|
|
283
|
+
} : {}), {
|
|
284
|
+
metadata: {
|
|
285
|
+
requestType: import_runtime_client_gql2.CopilotRequestType.Chat
|
|
470
286
|
}
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
287
|
+
}), agentSessionRef.current ? {
|
|
288
|
+
agentSession: agentSessionRef.current
|
|
289
|
+
} : {}), {
|
|
290
|
+
agentStates: Object.values(coagentStatesRef.current).map((state) => ({
|
|
291
|
+
agentName: state.name,
|
|
292
|
+
state: JSON.stringify(state.state)
|
|
293
|
+
}))
|
|
294
|
+
}),
|
|
295
|
+
properties: copilotConfig.properties,
|
|
296
|
+
signal: (_d = abortControllerRef.current) == null ? void 0 : _d.signal
|
|
297
|
+
})
|
|
298
|
+
);
|
|
299
|
+
const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
|
|
300
|
+
const reader = stream.getReader();
|
|
301
|
+
let actionResults = {};
|
|
302
|
+
let executedCoAgentStateRenders = [];
|
|
303
|
+
let followUp = void 0;
|
|
304
|
+
try {
|
|
305
|
+
while (true) {
|
|
306
|
+
let done, value;
|
|
307
|
+
try {
|
|
308
|
+
const readResult = yield reader.read();
|
|
309
|
+
done = readResult.done;
|
|
310
|
+
value = readResult.value;
|
|
311
|
+
} catch (readError) {
|
|
312
|
+
break;
|
|
313
|
+
}
|
|
314
|
+
if (done) {
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
if (!(value == null ? void 0 : value.generateCopilotResponse)) {
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
threadIdRef.current = value.generateCopilotResponse.threadId || null;
|
|
321
|
+
runIdRef.current = value.generateCopilotResponse.runId || null;
|
|
322
|
+
const messages2 = (0, import_runtime_client_gql2.convertGqlOutputToMessages)(
|
|
323
|
+
(0, import_runtime_client_gql2.filterAdjacentAgentStateMessages)(value.generateCopilotResponse.messages)
|
|
324
|
+
);
|
|
325
|
+
if (messages2.length === 0) {
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
newMessages = [];
|
|
329
|
+
if (((_h = value.generateCopilotResponse.status) == null ? void 0 : _h.__typename) === "FailedResponseStatus" && value.generateCopilotResponse.status.reason === "GUARDRAILS_VALIDATION_FAILED") {
|
|
330
|
+
newMessages = [
|
|
331
|
+
new import_runtime_client_gql2.TextMessage({
|
|
332
|
+
role: import_runtime_client_gql2.MessageRole.Assistant,
|
|
333
|
+
content: ((_i = value.generateCopilotResponse.status.details) == null ? void 0 : _i.guardrailsReason) || ""
|
|
334
|
+
})
|
|
335
|
+
];
|
|
336
|
+
} else {
|
|
337
|
+
for (const message of messages2) {
|
|
338
|
+
newMessages.push(message);
|
|
339
|
+
if (message.isActionExecutionMessage() && message.status.code !== import_runtime_client_gql2.MessageStatusCode.Pending && message.scope === "client" && onFunctionCall) {
|
|
340
|
+
if (!(message.id in actionResults)) {
|
|
498
341
|
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
499
342
|
break;
|
|
500
343
|
}
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
}
|
|
509
|
-
const lastAgentStateMessage = [...messages2].reverse().find((message) => message.isAgentStateMessage());
|
|
510
|
-
if (lastAgentStateMessage) {
|
|
511
|
-
if (lastAgentStateMessage.state.messages && lastAgentStateMessage.state.messages.length > 0) {
|
|
512
|
-
syncedMessages = (0, import_runtime_client_gql3.loadMessagesFromJsonRepresentation)(
|
|
513
|
-
lastAgentStateMessage.state.messages
|
|
514
|
-
);
|
|
515
|
-
}
|
|
516
|
-
setCoagentStatesWithRef((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
517
|
-
[lastAgentStateMessage.agentName]: {
|
|
518
|
-
name: lastAgentStateMessage.agentName,
|
|
519
|
-
state: lastAgentStateMessage.state,
|
|
520
|
-
running: lastAgentStateMessage.running,
|
|
521
|
-
active: lastAgentStateMessage.active,
|
|
522
|
-
threadId: lastAgentStateMessage.threadId,
|
|
523
|
-
nodeName: lastAgentStateMessage.nodeName,
|
|
524
|
-
runId: lastAgentStateMessage.runId
|
|
525
|
-
}
|
|
526
|
-
}));
|
|
527
|
-
if (lastAgentStateMessage.running) {
|
|
528
|
-
setAgentSession({
|
|
529
|
-
threadId: lastAgentStateMessage.threadId,
|
|
530
|
-
agentName: lastAgentStateMessage.agentName,
|
|
531
|
-
nodeName: lastAgentStateMessage.nodeName
|
|
532
|
-
});
|
|
533
|
-
} else {
|
|
534
|
-
if (agentLock) {
|
|
535
|
-
setAgentSession({
|
|
536
|
-
threadId: (0, import_shared2.randomId)(),
|
|
537
|
-
agentName: agentLock,
|
|
538
|
-
nodeName: void 0
|
|
539
|
-
});
|
|
540
|
-
} else {
|
|
541
|
-
setAgentSession(null);
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
if (newMessages.length > 0) {
|
|
547
|
-
setMessages([...previousMessages, ...newMessages]);
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
const finalMessages = constructFinalMessages(syncedMessages, previousMessages, newMessages);
|
|
551
|
-
let didExecuteAction = false;
|
|
552
|
-
if (onFunctionCall) {
|
|
553
|
-
const lastMessages = [];
|
|
554
|
-
for (let i = finalMessages.length - 1; i >= 0; i--) {
|
|
555
|
-
const message = finalMessages[i];
|
|
556
|
-
if (message.isActionExecutionMessage() && message.status.code !== import_runtime_client_gql3.MessageStatusCode.Pending) {
|
|
557
|
-
lastMessages.unshift(message);
|
|
558
|
-
} else {
|
|
559
|
-
break;
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
for (const message of lastMessages) {
|
|
563
|
-
setMessages(finalMessages);
|
|
564
|
-
const action = actions.find((action2) => action2.name === message.name);
|
|
565
|
-
if (action) {
|
|
566
|
-
followUp = action.followUp;
|
|
567
|
-
let result;
|
|
568
|
-
try {
|
|
569
|
-
result = yield Promise.race([
|
|
570
|
-
onFunctionCall({
|
|
344
|
+
try {
|
|
345
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
346
|
+
const action = actions.find((action2) => action2.name === message.name);
|
|
347
|
+
if (action) {
|
|
348
|
+
followUp = action.followUp;
|
|
349
|
+
}
|
|
350
|
+
const result = yield onFunctionCall({
|
|
571
351
|
messages: previousMessages,
|
|
572
352
|
name: message.name,
|
|
573
353
|
args: message.arguments
|
|
574
|
-
})
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
() => resolve("Operation was aborted by the user")
|
|
581
|
-
);
|
|
582
|
-
}
|
|
583
|
-
),
|
|
584
|
-
// if the user stopped generation, we also abort consecutive actions
|
|
585
|
-
new Promise((resolve) => {
|
|
586
|
-
var _a2;
|
|
587
|
-
if ((_a2 = chatAbortControllerRef.current) == null ? void 0 : _a2.signal.aborted) {
|
|
588
|
-
resolve("Operation was aborted by the user");
|
|
589
|
-
}
|
|
590
|
-
})
|
|
591
|
-
]);
|
|
592
|
-
} catch (e) {
|
|
593
|
-
result = `Failed to execute action ${message.name}`;
|
|
594
|
-
console.error(`Failed to execute action ${message.name}: ${e}`);
|
|
354
|
+
});
|
|
355
|
+
actionResults[message.id] = result;
|
|
356
|
+
} catch (e) {
|
|
357
|
+
actionResults[message.id] = `Failed to execute action ${message.name}`;
|
|
358
|
+
console.error(`Failed to execute action ${message.name}: ${e}`);
|
|
359
|
+
}
|
|
595
360
|
}
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
messageIndex + 1,
|
|
600
|
-
0,
|
|
601
|
-
new import_runtime_client_gql3.ResultMessage({
|
|
602
|
-
id: "result-" + message.id,
|
|
603
|
-
result: import_runtime_client_gql3.ResultMessage.encodeResult(result),
|
|
361
|
+
newMessages.push(
|
|
362
|
+
new import_runtime_client_gql2.ResultMessage({
|
|
363
|
+
result: import_runtime_client_gql2.ResultMessage.encodeResult(actionResults[message.id]),
|
|
604
364
|
actionExecutionId: message.id,
|
|
605
365
|
actionName: message.name
|
|
606
366
|
})
|
|
607
367
|
);
|
|
608
368
|
}
|
|
369
|
+
if (message.isAgentStateMessage() && !message.active && !executedCoAgentStateRenders.includes(message.id) && onCoAgentStateRender) {
|
|
370
|
+
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
371
|
+
break;
|
|
372
|
+
}
|
|
373
|
+
yield onCoAgentStateRender({
|
|
374
|
+
name: message.agentName,
|
|
375
|
+
nodeName: message.nodeName,
|
|
376
|
+
state: message.state
|
|
377
|
+
});
|
|
378
|
+
executedCoAgentStateRenders.push(message.id);
|
|
379
|
+
}
|
|
609
380
|
}
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
if (
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
381
|
+
const lastAgentStateMessage = [...messages2].reverse().find((message) => message.isAgentStateMessage());
|
|
382
|
+
if (lastAgentStateMessage) {
|
|
383
|
+
setCoagentStates((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
384
|
+
[lastAgentStateMessage.agentName]: {
|
|
385
|
+
name: lastAgentStateMessage.agentName,
|
|
386
|
+
state: lastAgentStateMessage.state,
|
|
387
|
+
running: lastAgentStateMessage.running,
|
|
388
|
+
active: lastAgentStateMessage.active,
|
|
389
|
+
threadId: lastAgentStateMessage.threadId,
|
|
390
|
+
nodeName: lastAgentStateMessage.nodeName,
|
|
391
|
+
runId: lastAgentStateMessage.runId
|
|
392
|
+
}
|
|
393
|
+
}));
|
|
394
|
+
if (lastAgentStateMessage.running) {
|
|
395
|
+
setAgentSession({
|
|
396
|
+
threadId: lastAgentStateMessage.threadId,
|
|
397
|
+
agentName: lastAgentStateMessage.agentName,
|
|
398
|
+
nodeName: lastAgentStateMessage.nodeName
|
|
399
|
+
});
|
|
400
|
+
} else {
|
|
401
|
+
setAgentSession(null);
|
|
627
402
|
}
|
|
628
|
-
return true;
|
|
629
|
-
});
|
|
630
|
-
const repairedMessageIds = repairedMessages.map((message) => message.id);
|
|
631
|
-
setMessages(repairedMessages);
|
|
632
|
-
if ((_l = agentSessionRef.current) == null ? void 0 : _l.nodeName) {
|
|
633
|
-
setAgentSession({
|
|
634
|
-
threadId: agentSessionRef.current.threadId,
|
|
635
|
-
agentName: agentSessionRef.current.agentName,
|
|
636
|
-
nodeName: "__end__"
|
|
637
|
-
});
|
|
638
403
|
}
|
|
639
|
-
return newMessages.filter((message) => repairedMessageIds.includes(message.id));
|
|
640
|
-
} else {
|
|
641
|
-
return newMessages.slice();
|
|
642
404
|
}
|
|
643
|
-
|
|
644
|
-
|
|
405
|
+
if (newMessages.length > 0) {
|
|
406
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
407
|
+
}
|
|
645
408
|
}
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
onFunctionCall,
|
|
657
|
-
onCoAgentStateRender,
|
|
658
|
-
setCoagentStatesWithRef,
|
|
659
|
-
coagentStatesRef,
|
|
660
|
-
agentSession,
|
|
661
|
-
setAgentSession
|
|
662
|
-
]
|
|
663
|
-
);
|
|
664
|
-
runChatCompletionRef.current = runChatCompletion;
|
|
665
|
-
const runChatCompletionAndHandleFunctionCall = useAsyncCallback(
|
|
666
|
-
(messages2) => __async(this, null, function* () {
|
|
667
|
-
yield runChatCompletionRef.current(messages2);
|
|
668
|
-
}),
|
|
669
|
-
[messages]
|
|
670
|
-
);
|
|
671
|
-
const append = useAsyncCallback(
|
|
672
|
-
(message) => __async(this, null, function* () {
|
|
673
|
-
if (isLoading) {
|
|
674
|
-
return;
|
|
409
|
+
if (
|
|
410
|
+
// if followUp is not explicitly false
|
|
411
|
+
followUp !== false && // if we have client side results
|
|
412
|
+
(Object.values(actionResults).length || // or the last message we received is a result
|
|
413
|
+
newMessages.length && newMessages[newMessages.length - 1].isResultMessage())
|
|
414
|
+
) {
|
|
415
|
+
yield new Promise((resolve) => setTimeout(resolve, 10));
|
|
416
|
+
return yield runChatCompletionRef.current([...previousMessages, ...newMessages]);
|
|
417
|
+
} else {
|
|
418
|
+
return newMessages.slice();
|
|
675
419
|
}
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
)
|
|
682
|
-
|
|
420
|
+
} finally {
|
|
421
|
+
setIsLoading(false);
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
runChatCompletionRef.current = runChatCompletion;
|
|
425
|
+
const runChatCompletionAndHandleFunctionCall = (messages2) => __async(this, null, function* () {
|
|
426
|
+
yield runChatCompletionRef.current(messages2);
|
|
427
|
+
});
|
|
428
|
+
const append = (message) => __async(this, null, function* () {
|
|
429
|
+
if (isLoading) {
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
const newMessages = [...messages, message];
|
|
433
|
+
setMessages(newMessages);
|
|
434
|
+
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
435
|
+
});
|
|
436
|
+
const reload = () => __async(this, null, function* () {
|
|
683
437
|
if (isLoading || messages.length === 0) {
|
|
684
438
|
return;
|
|
685
439
|
}
|
|
@@ -690,50 +444,32 @@ function useChat(options) {
|
|
|
690
444
|
}
|
|
691
445
|
setMessages(newMessages);
|
|
692
446
|
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
693
|
-
})
|
|
447
|
+
});
|
|
694
448
|
const stop = () => {
|
|
695
449
|
var _a;
|
|
696
|
-
(_a =
|
|
450
|
+
(_a = abortControllerRef.current) == null ? void 0 : _a.abort();
|
|
697
451
|
};
|
|
698
452
|
return {
|
|
699
453
|
append,
|
|
700
454
|
reload,
|
|
701
|
-
stop
|
|
702
|
-
runChatCompletion: () => runChatCompletionRef.current(messages)
|
|
455
|
+
stop
|
|
703
456
|
};
|
|
704
457
|
}
|
|
705
|
-
function constructFinalMessages(syncedMessages, previousMessages, newMessages) {
|
|
706
|
-
const finalMessages = syncedMessages.length > 0 ? [...syncedMessages] : [...previousMessages, ...newMessages];
|
|
707
|
-
if (syncedMessages.length > 0) {
|
|
708
|
-
const messagesWithAgentState = [...previousMessages, ...newMessages];
|
|
709
|
-
let previousMessageId = void 0;
|
|
710
|
-
for (const message of messagesWithAgentState) {
|
|
711
|
-
if (message.isAgentStateMessage()) {
|
|
712
|
-
const index = finalMessages.findIndex((msg) => msg.id === previousMessageId);
|
|
713
|
-
if (index !== -1) {
|
|
714
|
-
finalMessages.splice(index + 1, 0, message);
|
|
715
|
-
}
|
|
716
|
-
}
|
|
717
|
-
previousMessageId = message.id;
|
|
718
|
-
}
|
|
719
|
-
}
|
|
720
|
-
return finalMessages;
|
|
721
|
-
}
|
|
722
458
|
|
|
723
459
|
// src/components/copilot-provider/copilotkit.tsx
|
|
724
|
-
var
|
|
460
|
+
var import_react6 = require("react");
|
|
725
461
|
var import_react_dom = require("react-dom");
|
|
726
|
-
var
|
|
462
|
+
var import_shared2 = require("@copilotkit/shared");
|
|
727
463
|
|
|
728
464
|
// src/context/copilot-messages-context.tsx
|
|
729
|
-
var
|
|
465
|
+
var import_react5 = __toESM(require("react"));
|
|
730
466
|
var emptyCopilotContext2 = {
|
|
731
467
|
messages: [],
|
|
732
468
|
setMessages: () => []
|
|
733
469
|
};
|
|
734
|
-
var CopilotMessagesContext =
|
|
470
|
+
var CopilotMessagesContext = import_react5.default.createContext(emptyCopilotContext2);
|
|
735
471
|
function useCopilotMessagesContext() {
|
|
736
|
-
const context =
|
|
472
|
+
const context = import_react5.default.useContext(CopilotMessagesContext);
|
|
737
473
|
if (context === emptyCopilotContext2) {
|
|
738
474
|
throw new Error(
|
|
739
475
|
"A messages consuming component was not wrapped with `<CopilotMessages> {...} </CopilotMessages>`"
|
|
@@ -743,7 +479,7 @@ function useCopilotMessagesContext() {
|
|
|
743
479
|
}
|
|
744
480
|
|
|
745
481
|
// src/components/copilot-provider/copilotkit.tsx
|
|
746
|
-
var
|
|
482
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
747
483
|
var defaultCopilotContextCategories = ["global"];
|
|
748
484
|
|
|
749
485
|
// src/hooks/use-copilot-chat.ts
|
|
@@ -761,36 +497,29 @@ function useCopilotChat(_a = {}) {
|
|
|
761
497
|
setIsLoading,
|
|
762
498
|
chatInstructions,
|
|
763
499
|
actions,
|
|
764
|
-
|
|
765
|
-
|
|
500
|
+
coagentStates,
|
|
501
|
+
setCoagentStates,
|
|
766
502
|
coAgentStateRenders,
|
|
767
503
|
agentSession,
|
|
768
|
-
setAgentSession
|
|
769
|
-
forwardedParameters,
|
|
770
|
-
agentLock,
|
|
771
|
-
threadId,
|
|
772
|
-
setThreadId,
|
|
773
|
-
runId,
|
|
774
|
-
setRunId,
|
|
775
|
-
chatAbortControllerRef
|
|
504
|
+
setAgentSession
|
|
776
505
|
} = useCopilotContext();
|
|
777
506
|
const { messages, setMessages } = useCopilotMessagesContext();
|
|
778
507
|
const latestGetContextString = useUpdatedRef(getContextString);
|
|
779
|
-
const deleteMessage = (0,
|
|
508
|
+
const deleteMessage = (0, import_react7.useCallback)(
|
|
780
509
|
(messageId) => {
|
|
781
510
|
setMessages((prev) => prev.filter((message) => message.id !== messageId));
|
|
782
511
|
},
|
|
783
512
|
[setMessages]
|
|
784
513
|
);
|
|
785
|
-
const makeSystemMessageCallback = (0,
|
|
514
|
+
const makeSystemMessageCallback = (0, import_react7.useCallback)(() => {
|
|
786
515
|
const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
|
|
787
516
|
const contextString = latestGetContextString.current([], defaultCopilotContextCategories);
|
|
788
|
-
return new
|
|
517
|
+
return new import_runtime_client_gql3.TextMessage({
|
|
789
518
|
content: systemMessageMaker(contextString, chatInstructions),
|
|
790
|
-
role:
|
|
519
|
+
role: import_runtime_client_gql3.Role.System
|
|
791
520
|
});
|
|
792
521
|
}, [getContextString, makeSystemMessage, chatInstructions]);
|
|
793
|
-
const onCoAgentStateRender =
|
|
522
|
+
const onCoAgentStateRender = (0, import_react7.useCallback)(
|
|
794
523
|
(args) => __async(this, null, function* () {
|
|
795
524
|
var _a2;
|
|
796
525
|
const { name, nodeName, state } = args;
|
|
@@ -808,7 +537,7 @@ function useCopilotChat(_a = {}) {
|
|
|
808
537
|
}),
|
|
809
538
|
[coAgentStateRenders]
|
|
810
539
|
);
|
|
811
|
-
const { append, reload, stop
|
|
540
|
+
const { append, reload, stop } = useChat(__spreadProps(__spreadValues({}, options), {
|
|
812
541
|
actions: Object.values(actions),
|
|
813
542
|
copilotConfig: copilotApiConfig,
|
|
814
543
|
initialMessages: options.initialMessages || [],
|
|
@@ -819,91 +548,53 @@ function useCopilotChat(_a = {}) {
|
|
|
819
548
|
makeSystemMessageCallback,
|
|
820
549
|
isLoading,
|
|
821
550
|
setIsLoading,
|
|
822
|
-
|
|
823
|
-
|
|
551
|
+
coagentStates,
|
|
552
|
+
setCoagentStates,
|
|
824
553
|
agentSession,
|
|
825
|
-
setAgentSession
|
|
826
|
-
forwardedParameters,
|
|
827
|
-
threadId,
|
|
828
|
-
setThreadId,
|
|
829
|
-
runId,
|
|
830
|
-
setRunId,
|
|
831
|
-
chatAbortControllerRef,
|
|
832
|
-
agentLock
|
|
554
|
+
setAgentSession
|
|
833
555
|
}));
|
|
834
556
|
const latestAppend = useUpdatedRef(append);
|
|
835
|
-
const latestAppendFunc =
|
|
836
|
-
(message) =>
|
|
837
|
-
return
|
|
838
|
-
}
|
|
557
|
+
const latestAppendFunc = (0, import_react7.useCallback)(
|
|
558
|
+
(message) => {
|
|
559
|
+
return latestAppend.current(message);
|
|
560
|
+
},
|
|
839
561
|
[latestAppend]
|
|
840
562
|
);
|
|
841
563
|
const latestReload = useUpdatedRef(reload);
|
|
842
|
-
const latestReloadFunc =
|
|
843
|
-
return
|
|
844
|
-
}
|
|
564
|
+
const latestReloadFunc = (0, import_react7.useCallback)(() => {
|
|
565
|
+
return latestReload.current();
|
|
566
|
+
}, [latestReload]);
|
|
845
567
|
const latestStop = useUpdatedRef(stop);
|
|
846
|
-
const latestStopFunc = (0,
|
|
568
|
+
const latestStopFunc = (0, import_react7.useCallback)(() => {
|
|
847
569
|
return latestStop.current();
|
|
848
570
|
}, [latestStop]);
|
|
849
571
|
const latestDelete = useUpdatedRef(deleteMessage);
|
|
850
|
-
const latestDeleteFunc = (0,
|
|
572
|
+
const latestDeleteFunc = (0, import_react7.useCallback)(
|
|
851
573
|
(messageId) => {
|
|
852
574
|
return latestDelete.current(messageId);
|
|
853
575
|
},
|
|
854
576
|
[latestDelete]
|
|
855
577
|
);
|
|
856
578
|
const latestSetMessages = useUpdatedRef(setMessages);
|
|
857
|
-
const latestSetMessagesFunc = (0,
|
|
579
|
+
const latestSetMessagesFunc = (0, import_react7.useCallback)(
|
|
858
580
|
(messages2) => {
|
|
859
581
|
return latestSetMessages.current(messages2);
|
|
860
582
|
},
|
|
861
583
|
[latestSetMessages]
|
|
862
584
|
);
|
|
863
|
-
const latestRunChatCompletion = useUpdatedRef(runChatCompletion);
|
|
864
|
-
const latestRunChatCompletionFunc = useAsyncCallback(() => __async(this, null, function* () {
|
|
865
|
-
return yield latestRunChatCompletion.current();
|
|
866
|
-
}), [latestRunChatCompletion]);
|
|
867
|
-
const reset = (0, import_react8.useCallback)(() => {
|
|
868
|
-
latestStopFunc();
|
|
869
|
-
setMessages([]);
|
|
870
|
-
setThreadId(null);
|
|
871
|
-
setRunId(null);
|
|
872
|
-
setCoagentStatesWithRef({});
|
|
873
|
-
let initialAgentSession = null;
|
|
874
|
-
if (agentLock) {
|
|
875
|
-
initialAgentSession = {
|
|
876
|
-
agentName: agentLock
|
|
877
|
-
};
|
|
878
|
-
}
|
|
879
|
-
setAgentSession(initialAgentSession);
|
|
880
|
-
}, [
|
|
881
|
-
latestStopFunc,
|
|
882
|
-
setMessages,
|
|
883
|
-
setThreadId,
|
|
884
|
-
setCoagentStatesWithRef,
|
|
885
|
-
setAgentSession,
|
|
886
|
-
agentLock
|
|
887
|
-
]);
|
|
888
|
-
const latestReset = useUpdatedRef(reset);
|
|
889
|
-
const latestResetFunc = (0, import_react8.useCallback)(() => {
|
|
890
|
-
return latestReset.current();
|
|
891
|
-
}, [latestReset]);
|
|
892
585
|
return {
|
|
893
586
|
visibleMessages: messages,
|
|
894
587
|
appendMessage: latestAppendFunc,
|
|
895
588
|
setMessages: latestSetMessagesFunc,
|
|
896
589
|
reloadMessages: latestReloadFunc,
|
|
897
590
|
stopGeneration: latestStopFunc,
|
|
898
|
-
reset: latestResetFunc,
|
|
899
591
|
deleteMessage: latestDeleteFunc,
|
|
900
|
-
runChatCompletion: latestRunChatCompletionFunc,
|
|
901
592
|
isLoading
|
|
902
593
|
};
|
|
903
594
|
}
|
|
904
595
|
function useUpdatedRef(value) {
|
|
905
|
-
const ref = (0,
|
|
906
|
-
(0,
|
|
596
|
+
const ref = (0, import_react7.useRef)(value);
|
|
597
|
+
(0, import_react7.useEffect)(() => {
|
|
907
598
|
ref.current = value;
|
|
908
599
|
}, [value]);
|
|
909
600
|
return ref;
|
|
@@ -933,22 +624,18 @@ ${additionalInstructions}` : "");
|
|
|
933
624
|
}
|
|
934
625
|
|
|
935
626
|
// src/hooks/use-copilot-action.ts
|
|
936
|
-
var
|
|
937
|
-
var
|
|
627
|
+
var import_shared3 = require("@copilotkit/shared");
|
|
628
|
+
var import_react8 = require("react");
|
|
938
629
|
function useCopilotAction(action, dependencies) {
|
|
939
630
|
const { setAction, removeAction, actions, chatComponentsCache } = useCopilotContext();
|
|
940
|
-
const idRef = (0,
|
|
941
|
-
const renderAndWaitRef = (0,
|
|
631
|
+
const idRef = (0, import_react8.useRef)((0, import_shared3.randomId)());
|
|
632
|
+
const renderAndWaitRef = (0, import_react8.useRef)(null);
|
|
942
633
|
action = __spreadValues({}, action);
|
|
943
|
-
if (
|
|
944
|
-
// renderAndWaitForResponse is not available for catch all actions
|
|
945
|
-
isFrontendAction(action) && // check if renderAndWaitForResponse is set
|
|
946
|
-
(action.renderAndWait || action.renderAndWaitForResponse)
|
|
947
|
-
) {
|
|
634
|
+
if (action.renderAndWait || action.renderAndWaitForResponse) {
|
|
948
635
|
const renderAndWait = action.renderAndWait || action.renderAndWaitForResponse;
|
|
949
636
|
action.renderAndWait = void 0;
|
|
950
637
|
action.renderAndWaitForResponse = void 0;
|
|
951
|
-
action.handler =
|
|
638
|
+
action.handler = () => __async(this, null, function* () {
|
|
952
639
|
let resolve;
|
|
953
640
|
let reject;
|
|
954
641
|
const promise = new Promise((resolvePromise, rejectPromise) => {
|
|
@@ -957,18 +644,14 @@ function useCopilotAction(action, dependencies) {
|
|
|
957
644
|
});
|
|
958
645
|
renderAndWaitRef.current = { promise, resolve, reject };
|
|
959
646
|
return yield promise;
|
|
960
|
-
})
|
|
647
|
+
});
|
|
961
648
|
action.render = (props) => {
|
|
962
|
-
let status = props.status;
|
|
963
|
-
if (props.status === "executing" && !renderAndWaitRef.current) {
|
|
964
|
-
status = "inProgress";
|
|
965
|
-
}
|
|
966
649
|
const waitProps = {
|
|
967
|
-
status,
|
|
650
|
+
status: props.status,
|
|
968
651
|
args: props.args,
|
|
969
652
|
result: props.result,
|
|
970
|
-
handler: status === "executing" ? renderAndWaitRef.current.resolve : void 0,
|
|
971
|
-
respond: status === "executing" ? renderAndWaitRef.current.resolve : void 0
|
|
653
|
+
handler: props.status === "executing" ? renderAndWaitRef.current.resolve : void 0,
|
|
654
|
+
respond: props.status === "executing" ? renderAndWaitRef.current.resolve : void 0
|
|
972
655
|
};
|
|
973
656
|
const isNoArgsRenderWait = (_fn) => {
|
|
974
657
|
var _a;
|
|
@@ -981,14 +664,12 @@ function useCopilotAction(action, dependencies) {
|
|
|
981
664
|
return renderAndWait(waitProps);
|
|
982
665
|
}
|
|
983
666
|
}
|
|
984
|
-
return (0,
|
|
667
|
+
return (0, import_react8.createElement)(import_react8.Fragment);
|
|
985
668
|
};
|
|
986
669
|
}
|
|
987
670
|
if (dependencies === void 0) {
|
|
988
671
|
if (actions[idRef.current]) {
|
|
989
|
-
|
|
990
|
-
actions[idRef.current].handler = action.handler;
|
|
991
|
-
}
|
|
672
|
+
actions[idRef.current].handler = action.handler;
|
|
992
673
|
if (typeof action.render === "function") {
|
|
993
674
|
if (chatComponentsCache.current !== null) {
|
|
994
675
|
chatComponentsCache.current.actions[action.name] = action.render;
|
|
@@ -996,7 +677,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
996
677
|
}
|
|
997
678
|
}
|
|
998
679
|
}
|
|
999
|
-
(0,
|
|
680
|
+
(0, import_react8.useEffect)(() => {
|
|
1000
681
|
setAction(idRef.current, action);
|
|
1001
682
|
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
1002
683
|
chatComponentsCache.current.actions[action.name] = action.render;
|
|
@@ -1007,43 +688,31 @@ function useCopilotAction(action, dependencies) {
|
|
|
1007
688
|
}, [
|
|
1008
689
|
setAction,
|
|
1009
690
|
removeAction,
|
|
1010
|
-
|
|
691
|
+
action.description,
|
|
1011
692
|
action.name,
|
|
1012
|
-
|
|
1013
|
-
|
|
693
|
+
action.disabled,
|
|
694
|
+
action.available,
|
|
1014
695
|
// This should be faster than deep equality checking
|
|
1015
696
|
// In addition, all major JS engines guarantee the order of object keys
|
|
1016
|
-
JSON.stringify(
|
|
697
|
+
JSON.stringify(action.parameters),
|
|
1017
698
|
// include render only if it's a string
|
|
1018
699
|
typeof action.render === "string" ? action.render : void 0,
|
|
1019
700
|
// dependencies set by the developer
|
|
1020
701
|
...dependencies || []
|
|
1021
702
|
]);
|
|
1022
703
|
}
|
|
1023
|
-
function isFrontendAction(action) {
|
|
1024
|
-
return action.name !== "*";
|
|
1025
|
-
}
|
|
1026
704
|
|
|
1027
705
|
// src/hooks/use-coagent-state-render.ts
|
|
1028
|
-
var
|
|
1029
|
-
var
|
|
706
|
+
var import_react9 = require("react");
|
|
707
|
+
var import_shared4 = require("@copilotkit/shared");
|
|
1030
708
|
function useCoAgentStateRender(action, dependencies) {
|
|
1031
709
|
const {
|
|
1032
710
|
setCoAgentStateRender,
|
|
1033
711
|
removeCoAgentStateRender,
|
|
1034
712
|
coAgentStateRenders,
|
|
1035
|
-
chatComponentsCache
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
const idRef = (0, import_react10.useRef)((0, import_shared5.randomId)());
|
|
1039
|
-
const { addToast } = useToast();
|
|
1040
|
-
(0, import_react10.useEffect)(() => {
|
|
1041
|
-
if ((availableAgents == null ? void 0 : availableAgents.length) && !availableAgents.some((a) => a.name === action.name)) {
|
|
1042
|
-
const message = `(useCoAgentStateRender): Agent "${action.name}" not found. Make sure the agent exists and is properly configured.`;
|
|
1043
|
-
console.warn(message);
|
|
1044
|
-
addToast({ type: "warning", message });
|
|
1045
|
-
}
|
|
1046
|
-
}, [availableAgents]);
|
|
713
|
+
chatComponentsCache
|
|
714
|
+
} = (0, import_react9.useContext)(CopilotContext);
|
|
715
|
+
const idRef = (0, import_react9.useRef)((0, import_shared4.randomId)());
|
|
1047
716
|
const key = `${action.name}-${action.nodeName || "global"}`;
|
|
1048
717
|
if (dependencies === void 0) {
|
|
1049
718
|
if (coAgentStateRenders[idRef.current]) {
|
|
@@ -1055,7 +724,7 @@ function useCoAgentStateRender(action, dependencies) {
|
|
|
1055
724
|
}
|
|
1056
725
|
}
|
|
1057
726
|
}
|
|
1058
|
-
(0,
|
|
727
|
+
(0, import_react9.useEffect)(() => {
|
|
1059
728
|
setCoAgentStateRender(idRef.current, action);
|
|
1060
729
|
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
1061
730
|
chatComponentsCache.current.coAgentStateRenders[key] = action.render;
|
|
@@ -1075,11 +744,11 @@ function useCoAgentStateRender(action, dependencies) {
|
|
|
1075
744
|
}
|
|
1076
745
|
|
|
1077
746
|
// src/hooks/use-make-copilot-document-readable.ts
|
|
1078
|
-
var
|
|
747
|
+
var import_react10 = require("react");
|
|
1079
748
|
function useMakeCopilotDocumentReadable(document, categories, dependencies = []) {
|
|
1080
749
|
const { addDocumentContext, removeDocumentContext } = useCopilotContext();
|
|
1081
|
-
const idRef = (0,
|
|
1082
|
-
(0,
|
|
750
|
+
const idRef = (0, import_react10.useRef)();
|
|
751
|
+
(0, import_react10.useEffect)(() => {
|
|
1083
752
|
const id = addDocumentContext(document, categories);
|
|
1084
753
|
idRef.current = id;
|
|
1085
754
|
return () => {
|
|
@@ -1090,16 +759,16 @@ function useMakeCopilotDocumentReadable(document, categories, dependencies = [])
|
|
|
1090
759
|
}
|
|
1091
760
|
|
|
1092
761
|
// src/hooks/use-copilot-readable.ts
|
|
1093
|
-
var
|
|
762
|
+
var import_react11 = require("react");
|
|
1094
763
|
function convertToJSON(description, value) {
|
|
1095
764
|
return `${description}: ${typeof value === "string" ? value : JSON.stringify(value)}`;
|
|
1096
765
|
}
|
|
1097
766
|
function useCopilotReadable({ description, value, parentId, categories, convert }, dependencies) {
|
|
1098
767
|
const { addContext, removeContext } = useCopilotContext();
|
|
1099
|
-
const idRef = (0,
|
|
768
|
+
const idRef = (0, import_react11.useRef)();
|
|
1100
769
|
convert = convert || convertToJSON;
|
|
1101
770
|
const information = convert(description, value);
|
|
1102
|
-
(0,
|
|
771
|
+
(0, import_react11.useEffect)(() => {
|
|
1103
772
|
const id = addContext(information, parentId, categories);
|
|
1104
773
|
idRef.current = id;
|
|
1105
774
|
return () => {
|
|
@@ -1110,29 +779,20 @@ function useCopilotReadable({ description, value, parentId, categories, convert
|
|
|
1110
779
|
}
|
|
1111
780
|
|
|
1112
781
|
// src/hooks/use-coagent.ts
|
|
1113
|
-
var
|
|
782
|
+
var import_react12 = require("react");
|
|
1114
783
|
function useCoAgent(options) {
|
|
1115
|
-
const generalContext = useCopilotContext();
|
|
1116
|
-
const { availableAgents } = generalContext;
|
|
1117
|
-
const { addToast } = useToast();
|
|
1118
784
|
const isExternalStateManagement = (options2) => {
|
|
1119
785
|
return "state" in options2 && "setState" in options2;
|
|
1120
786
|
};
|
|
1121
787
|
const { name } = options;
|
|
1122
|
-
(0, import_react13.useEffect)(() => {
|
|
1123
|
-
if ((availableAgents == null ? void 0 : availableAgents.length) && !availableAgents.some((a) => a.name === name)) {
|
|
1124
|
-
const message = `(useCoAgent): Agent "${name}" not found. Make sure the agent exists and is properly configured.`;
|
|
1125
|
-
console.warn(message);
|
|
1126
|
-
addToast({ type: "warning", message });
|
|
1127
|
-
}
|
|
1128
|
-
}, [availableAgents]);
|
|
1129
788
|
const isInternalStateManagementWithInitial = (options2) => {
|
|
1130
789
|
return "initialState" in options2;
|
|
1131
790
|
};
|
|
791
|
+
const generalContext = useCopilotContext();
|
|
1132
792
|
const messagesContext = useCopilotMessagesContext();
|
|
1133
793
|
const context = __spreadValues(__spreadValues({}, generalContext), messagesContext);
|
|
1134
|
-
const { coagentStates,
|
|
1135
|
-
const { appendMessage
|
|
794
|
+
const { coagentStates, setCoagentStates } = context;
|
|
795
|
+
const { appendMessage } = useCopilotChat();
|
|
1136
796
|
const getCoagentState = (coagentStates2, name2) => {
|
|
1137
797
|
if (coagentStates2[name2]) {
|
|
1138
798
|
return coagentStates2[name2];
|
|
@@ -1149,43 +809,40 @@ function useCoAgent(options) {
|
|
|
1149
809
|
}
|
|
1150
810
|
};
|
|
1151
811
|
const setState = (newState) => {
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
812
|
+
setCoagentStates((prevAgentStates) => {
|
|
813
|
+
let coagentState2 = getCoagentState(prevAgentStates, name);
|
|
814
|
+
const updatedState = typeof newState === "function" ? newState(coagentState2.state) : newState;
|
|
815
|
+
return __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
816
|
+
[name]: __spreadProps(__spreadValues({}, coagentState2), {
|
|
817
|
+
state: updatedState
|
|
818
|
+
})
|
|
819
|
+
});
|
|
820
|
+
});
|
|
1159
821
|
};
|
|
1160
822
|
const coagentState = getCoagentState(coagentStates, name);
|
|
1161
823
|
const state = isExternalStateManagement(options) ? options.state : coagentState.state;
|
|
1162
|
-
(0,
|
|
824
|
+
(0, import_react12.useEffect)(() => {
|
|
1163
825
|
if (isExternalStateManagement(options)) {
|
|
1164
826
|
setState(options.state);
|
|
1165
827
|
} else if (coagentStates[name] === void 0) {
|
|
1166
828
|
setState(options.initialState === void 0 ? {} : options.initialState);
|
|
1167
829
|
}
|
|
1168
|
-
}, [
|
|
1169
|
-
isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0,
|
|
1170
|
-
// reset initialstate on reset
|
|
1171
|
-
coagentStates[name] === void 0
|
|
1172
|
-
]);
|
|
1173
|
-
const runAgentCallback = useAsyncCallback(
|
|
1174
|
-
(hint) => __async(this, null, function* () {
|
|
1175
|
-
yield runAgent(name, context, appendMessage, runChatCompletion, hint);
|
|
1176
|
-
}),
|
|
1177
|
-
[name, context, appendMessage, runChatCompletion]
|
|
1178
|
-
);
|
|
830
|
+
}, [isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0]);
|
|
1179
831
|
return {
|
|
1180
832
|
name,
|
|
1181
833
|
nodeName: coagentState.nodeName,
|
|
1182
|
-
|
|
834
|
+
state,
|
|
835
|
+
setState,
|
|
1183
836
|
running: coagentState.running,
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
stop: () =>
|
|
1188
|
-
|
|
837
|
+
start: () => {
|
|
838
|
+
startAgent(name, context);
|
|
839
|
+
},
|
|
840
|
+
stop: () => {
|
|
841
|
+
stopAgent(name, context);
|
|
842
|
+
},
|
|
843
|
+
run: (hint) => {
|
|
844
|
+
return runAgent(name, context, appendMessage, hint);
|
|
845
|
+
}
|
|
1189
846
|
};
|
|
1190
847
|
}
|
|
1191
848
|
function startAgent(name, context) {
|
|
@@ -1198,22 +855,11 @@ function stopAgent(name, context) {
|
|
|
1198
855
|
const { agentSession, setAgentSession } = context;
|
|
1199
856
|
if (agentSession && agentSession.agentName === name) {
|
|
1200
857
|
setAgentSession(null);
|
|
1201
|
-
context.setCoagentStates((prevAgentStates) => {
|
|
1202
|
-
return __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
1203
|
-
[name]: __spreadProps(__spreadValues({}, prevAgentStates[name]), {
|
|
1204
|
-
running: false,
|
|
1205
|
-
active: false,
|
|
1206
|
-
threadId: void 0,
|
|
1207
|
-
nodeName: void 0,
|
|
1208
|
-
runId: void 0
|
|
1209
|
-
})
|
|
1210
|
-
});
|
|
1211
|
-
});
|
|
1212
858
|
} else {
|
|
1213
859
|
console.warn(`No agent session found for ${name}`);
|
|
1214
860
|
}
|
|
1215
861
|
}
|
|
1216
|
-
function runAgent(name, context, appendMessage,
|
|
862
|
+
function runAgent(name, context, appendMessage, hint) {
|
|
1217
863
|
return __async(this, null, function* () {
|
|
1218
864
|
var _a, _b;
|
|
1219
865
|
const { agentSession, setAgentSession } = context;
|
|
@@ -1229,72 +875,20 @@ function runAgent(name, context, appendMessage, runChatCompletion, hint) {
|
|
|
1229
875
|
previousState = message.state;
|
|
1230
876
|
}
|
|
1231
877
|
}
|
|
1232
|
-
let state = ((_b = (_a = context.
|
|
878
|
+
let state = ((_b = (_a = context.coagentStates) == null ? void 0 : _a[name]) == null ? void 0 : _b.state) || {};
|
|
1233
879
|
if (hint) {
|
|
1234
880
|
const hintMessage = hint({ previousState, currentState: state });
|
|
1235
881
|
if (hintMessage) {
|
|
1236
882
|
yield appendMessage(hintMessage);
|
|
1237
|
-
} else {
|
|
1238
|
-
yield runChatCompletion();
|
|
1239
883
|
}
|
|
1240
|
-
} else {
|
|
1241
|
-
yield runChatCompletion();
|
|
1242
884
|
}
|
|
1243
885
|
});
|
|
1244
886
|
}
|
|
1245
|
-
|
|
1246
|
-
// src/hooks/use-copilot-authenticated-action.ts
|
|
1247
|
-
var import_react14 = require("react");
|
|
1248
|
-
var import_react15 = __toESM(require("react"));
|
|
1249
|
-
function useCopilotAuthenticatedAction(action, dependencies) {
|
|
1250
|
-
const { authConfig, authStates, setAuthStates } = useCopilotContext();
|
|
1251
|
-
const pendingActionRef = (0, import_react14.useRef)(null);
|
|
1252
|
-
const executeAction = (0, import_react14.useCallback)(
|
|
1253
|
-
(props) => {
|
|
1254
|
-
if (typeof action.render === "function") {
|
|
1255
|
-
return action.render(props);
|
|
1256
|
-
}
|
|
1257
|
-
return action.render || import_react15.default.createElement(import_react14.Fragment);
|
|
1258
|
-
},
|
|
1259
|
-
[action]
|
|
1260
|
-
);
|
|
1261
|
-
const wrappedRender = (0, import_react14.useCallback)(
|
|
1262
|
-
(props) => {
|
|
1263
|
-
const isAuthenticated = Object.values(authStates || {}).some(
|
|
1264
|
-
(state) => state.status === "authenticated"
|
|
1265
|
-
);
|
|
1266
|
-
if (!isAuthenticated) {
|
|
1267
|
-
pendingActionRef.current = props;
|
|
1268
|
-
return (authConfig == null ? void 0 : authConfig.SignInComponent) ? import_react15.default.createElement(authConfig.SignInComponent, {
|
|
1269
|
-
onSignInComplete: (authState) => {
|
|
1270
|
-
setAuthStates == null ? void 0 : setAuthStates((prev) => __spreadProps(__spreadValues({}, prev), { [action.name]: authState }));
|
|
1271
|
-
if (pendingActionRef.current) {
|
|
1272
|
-
executeAction(pendingActionRef.current);
|
|
1273
|
-
pendingActionRef.current = null;
|
|
1274
|
-
}
|
|
1275
|
-
}
|
|
1276
|
-
}) : import_react15.default.createElement(import_react14.Fragment);
|
|
1277
|
-
}
|
|
1278
|
-
return executeAction(props);
|
|
1279
|
-
},
|
|
1280
|
-
[action, authStates, setAuthStates]
|
|
1281
|
-
);
|
|
1282
|
-
useCopilotAction(
|
|
1283
|
-
__spreadProps(__spreadValues({}, action), {
|
|
1284
|
-
render: wrappedRender
|
|
1285
|
-
}),
|
|
1286
|
-
dependencies
|
|
1287
|
-
);
|
|
1288
|
-
}
|
|
1289
887
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1290
888
|
0 && (module.exports = {
|
|
1291
|
-
runAgent,
|
|
1292
|
-
startAgent,
|
|
1293
|
-
stopAgent,
|
|
1294
889
|
useCoAgent,
|
|
1295
890
|
useCoAgentStateRender,
|
|
1296
891
|
useCopilotAction,
|
|
1297
|
-
useCopilotAuthenticatedAction,
|
|
1298
892
|
useCopilotChat,
|
|
1299
893
|
useCopilotReadable,
|
|
1300
894
|
useCopilotRuntimeClient,
|