@copilotkit/react-core 1.5.1-next.1 → 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 +21 -0
- package/dist/{chunk-SFPANIOY.mjs → chunk-2KCEHGSI.mjs} +49 -99
- package/dist/chunk-2KCEHGSI.mjs.map +1 -0
- package/dist/{chunk-YL6V3QTN.mjs → chunk-6OR25P32.mjs} +15 -42
- package/dist/chunk-6OR25P32.mjs.map +1 -0
- package/dist/{chunk-VQDGBYWQ.mjs → chunk-DDVXFNYA.mjs} +31 -55
- package/dist/chunk-DDVXFNYA.mjs.map +1 -0
- package/dist/{chunk-NYBGR2NK.mjs → chunk-FXHKK52V.mjs} +13 -14
- package/dist/chunk-FXHKK52V.mjs.map +1 -0
- package/dist/{chunk-VPTC36RZ.mjs → chunk-H4VZMKR6.mjs} +2 -2
- package/dist/{chunk-OAF4ASJH.mjs → chunk-H7LSKIDK.mjs} +23 -70
- 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-STZUYPHJ.mjs → chunk-LODRWFMB.mjs} +2 -14
- package/dist/chunk-LODRWFMB.mjs.map +1 -0
- package/dist/{chunk-MKEQ2MJG.mjs → chunk-MWZO6TUR.mjs} +13 -29
- package/dist/chunk-MWZO6TUR.mjs.map +1 -0
- package/dist/{chunk-7LRDVJH5.mjs → chunk-ODN4H66E.mjs} +2 -6
- package/dist/chunk-ODN4H66E.mjs.map +1 -0
- package/dist/{chunk-6KHA5W5R.mjs → chunk-QDGDXRRJ.mjs} +2 -2
- package/dist/{chunk-ODAH4HNG.mjs → chunk-XINZBP3J.mjs} +2 -2
- package/dist/{chunk-ODAH4HNG.mjs.map → chunk-XINZBP3J.mjs.map} +1 -1
- package/dist/components/copilot-provider/copilot-messages.d.ts +0 -1
- package/dist/components/copilot-provider/copilotkit-props.d.ts +0 -5
- package/dist/components/copilot-provider/copilotkit-props.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.d.ts +0 -1
- package/dist/components/copilot-provider/copilotkit.js +91 -191
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +7 -8
- package/dist/components/copilot-provider/index.d.ts +0 -1
- package/dist/components/copilot-provider/index.js +91 -191
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +7 -8
- package/dist/components/index.d.ts +0 -1
- package/dist/components/index.js +91 -191
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +7 -8
- package/dist/components/toast/toast-provider.d.ts +1 -2
- package/dist/components/toast/toast-provider.js +62 -76
- 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 +3 -15
- package/dist/context/copilot-context.js +1 -13
- 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 +1 -13
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +4 -4
- package/dist/hooks/index.d.ts +1 -2
- package/dist/hooks/index.js +293 -624
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +22 -30
- package/dist/hooks/use-chat.d.ts +3 -31
- package/dist/hooks/use-chat.js +205 -443
- 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 +1 -13
- 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 -14
- package/dist/hooks/use-coagent.js +262 -580
- package/dist/hooks/use-coagent.js.map +1 -1
- package/dist/hooks/use-coagent.mjs +11 -19
- package/dist/hooks/use-copilot-action.d.ts +2 -12
- package/dist/hooks/use-copilot-action.js +17 -167
- 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 +238 -532
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +10 -12
- package/dist/hooks/use-copilot-readable.js +1 -13
- 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 +1 -13
- 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 -3
- package/dist/index.js +391 -703
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +30 -38
- 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 +8 -10
- 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 +8 -10
- 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 +7 -8
- 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/src/components/copilot-provider/copilotkit-props.tsx +10 -0
- package/src/components/copilot-provider/copilotkit.tsx +44 -7
- package/src/components/toast/toast-provider.tsx +1 -1
- package/src/context/copilot-context.tsx +28 -2
- package/src/hooks/index.ts +1 -0
- package/src/hooks/use-chat.ts +15 -5
- package/src/hooks/use-coagent-state-render.ts +11 -0
- package/src/hooks/use-coagent.ts +11 -1
- package/src/hooks/use-copilot-authenticated-action.ts +60 -0
- package/src/hooks/use-copilot-chat.ts +1 -0
- package/dist/chunk-7LRDVJH5.mjs.map +0 -1
- package/dist/chunk-M3SYKJ3W.mjs +0 -393
- package/dist/chunk-M3SYKJ3W.mjs.map +0 -1
- package/dist/chunk-MKEQ2MJG.mjs.map +0 -1
- package/dist/chunk-NYBGR2NK.mjs.map +0 -1
- package/dist/chunk-OAF4ASJH.mjs.map +0 -1
- package/dist/chunk-QCUP6HLK.mjs +0 -37
- package/dist/chunk-QCUP6HLK.mjs.map +0 -1
- package/dist/chunk-SFPANIOY.mjs.map +0 -1
- package/dist/chunk-STZUYPHJ.mjs.map +0 -1
- package/dist/chunk-VQDGBYWQ.mjs.map +0 -1
- package/dist/chunk-XERJQUHA.mjs +0 -31
- package/dist/chunk-XERJQUHA.mjs.map +0 -1
- package/dist/chunk-YL6V3QTN.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/{chunk-VPTC36RZ.mjs.map → chunk-H4VZMKR6.mjs.map} +0 -0
- /package/dist/{chunk-6KHA5W5R.mjs.map → chunk-QDGDXRRJ.mjs.map} +0 -0
package/dist/hooks/index.js
CHANGED
|
@@ -79,9 +79,6 @@ 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,
|
|
@@ -93,7 +90,7 @@ __export(hooks_exports, {
|
|
|
93
90
|
module.exports = __toCommonJS(hooks_exports);
|
|
94
91
|
|
|
95
92
|
// src/hooks/use-copilot-chat.ts
|
|
96
|
-
var
|
|
93
|
+
var import_react7 = require("react");
|
|
97
94
|
|
|
98
95
|
// src/context/copilot-context.tsx
|
|
99
96
|
var import_react = __toESM(require("react"));
|
|
@@ -144,21 +141,9 @@ var emptyCopilotContext = {
|
|
|
144
141
|
coagentStates: {},
|
|
145
142
|
setCoagentStates: () => {
|
|
146
143
|
},
|
|
147
|
-
coagentStatesRef: { current: {} },
|
|
148
|
-
setCoagentStatesWithRef: () => {
|
|
149
|
-
},
|
|
150
144
|
agentSession: null,
|
|
151
145
|
setAgentSession: () => {
|
|
152
|
-
}
|
|
153
|
-
forwardedParameters: {},
|
|
154
|
-
agentLock: null,
|
|
155
|
-
threadId: null,
|
|
156
|
-
setThreadId: () => {
|
|
157
|
-
},
|
|
158
|
-
runId: null,
|
|
159
|
-
setRunId: () => {
|
|
160
|
-
},
|
|
161
|
-
chatAbortControllerRef: { current: null }
|
|
146
|
+
}
|
|
162
147
|
};
|
|
163
148
|
var CopilotContext = import_react.default.createContext(emptyCopilotContext);
|
|
164
149
|
function useCopilotContext() {
|
|
@@ -174,159 +159,19 @@ function returnAndThrowInDebug(value) {
|
|
|
174
159
|
}
|
|
175
160
|
|
|
176
161
|
// src/hooks/use-copilot-chat.ts
|
|
177
|
-
var import_runtime_client_gql4 = require("@copilotkit/runtime-client-gql");
|
|
178
|
-
|
|
179
|
-
// src/hooks/use-chat.ts
|
|
180
|
-
var import_react5 = require("react");
|
|
181
|
-
var import_shared2 = require("@copilotkit/shared");
|
|
182
162
|
var import_runtime_client_gql3 = require("@copilotkit/runtime-client-gql");
|
|
183
163
|
|
|
184
|
-
// src/
|
|
185
|
-
var
|
|
164
|
+
// src/hooks/use-chat.ts
|
|
165
|
+
var import_react4 = require("react");
|
|
186
166
|
var import_shared = require("@copilotkit/shared");
|
|
187
|
-
|
|
188
|
-
const filteredActions = actions.filter(
|
|
189
|
-
(action) => action.available !== import_runtime_client_gql.ActionInputAvailability.Disabled && action.disabled !== true && action.name !== "*"
|
|
190
|
-
).map((action) => {
|
|
191
|
-
let available = import_runtime_client_gql.ActionInputAvailability.Enabled;
|
|
192
|
-
if (action.disabled) {
|
|
193
|
-
available = import_runtime_client_gql.ActionInputAvailability.Disabled;
|
|
194
|
-
} else if (action.available === "disabled") {
|
|
195
|
-
available = import_runtime_client_gql.ActionInputAvailability.Disabled;
|
|
196
|
-
} else if (action.available === "remote") {
|
|
197
|
-
available = import_runtime_client_gql.ActionInputAvailability.Remote;
|
|
198
|
-
}
|
|
199
|
-
return {
|
|
200
|
-
name: action.name,
|
|
201
|
-
description: action.description || "",
|
|
202
|
-
jsonSchema: JSON.stringify((0, import_shared.actionParametersToJsonSchema)(action.parameters || [])),
|
|
203
|
-
available
|
|
204
|
-
};
|
|
205
|
-
});
|
|
206
|
-
return filteredActions;
|
|
207
|
-
}
|
|
167
|
+
var import_runtime_client_gql2 = require("@copilotkit/runtime-client-gql");
|
|
208
168
|
|
|
209
169
|
// src/components/toast/toast-provider.tsx
|
|
210
|
-
var import_react3 = require("react");
|
|
211
|
-
|
|
212
|
-
// src/components/error-boundary/error-utils.tsx
|
|
213
170
|
var import_react2 = require("react");
|
|
214
|
-
|
|
215
|
-
// src/components/toast/exclamation-mark-icon.tsx
|
|
216
171
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
217
|
-
var
|
|
218
|
-
className,
|
|
219
|
-
style
|
|
220
|
-
}) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
|
|
221
|
-
"svg",
|
|
222
|
-
{
|
|
223
|
-
xmlns: "http://www.w3.org/2000/svg",
|
|
224
|
-
width: "24",
|
|
225
|
-
height: "24",
|
|
226
|
-
viewBox: "0 0 24 24",
|
|
227
|
-
fill: "none",
|
|
228
|
-
stroke: "currentColor",
|
|
229
|
-
strokeWidth: "2",
|
|
230
|
-
strokeLinecap: "round",
|
|
231
|
-
strokeLinejoin: "round",
|
|
232
|
-
className: `lucide lucide-circle-alert ${className ? className : ""}`,
|
|
233
|
-
style,
|
|
234
|
-
children: [
|
|
235
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("circle", { cx: "12", cy: "12", r: "10" }),
|
|
236
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("line", { x1: "12", x2: "12", y1: "8", y2: "12" }),
|
|
237
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("line", { x1: "12", x2: "12.01", y1: "16", y2: "16" })
|
|
238
|
-
]
|
|
239
|
-
}
|
|
240
|
-
);
|
|
241
|
-
|
|
242
|
-
// src/components/error-boundary/error-utils.tsx
|
|
243
|
-
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
244
|
-
function ErrorToast({ errors }) {
|
|
245
|
-
const errorsToRender = errors.map((error, idx) => {
|
|
246
|
-
var _a, _b, _c;
|
|
247
|
-
const message = "extensions" in error ? (_b = (_a = error.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message : error.message;
|
|
248
|
-
const code = "extensions" in error ? (_c = error.extensions) == null ? void 0 : _c.code : null;
|
|
249
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
250
|
-
"div",
|
|
251
|
-
{
|
|
252
|
-
style: {
|
|
253
|
-
marginTop: idx === 0 ? 0 : 10,
|
|
254
|
-
marginBottom: 14
|
|
255
|
-
},
|
|
256
|
-
children: [
|
|
257
|
-
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ExclamationMarkIcon, { style: { marginBottom: 4 } }),
|
|
258
|
-
code && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
259
|
-
"div",
|
|
260
|
-
{
|
|
261
|
-
style: {
|
|
262
|
-
fontWeight: "600",
|
|
263
|
-
marginBottom: 4
|
|
264
|
-
},
|
|
265
|
-
children: [
|
|
266
|
-
"Copilot Cloud Error:",
|
|
267
|
-
" ",
|
|
268
|
-
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { style: { fontFamily: "monospace", fontWeight: "normal" }, children: code })
|
|
269
|
-
]
|
|
270
|
-
}
|
|
271
|
-
),
|
|
272
|
-
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { children: message })
|
|
273
|
-
]
|
|
274
|
-
},
|
|
275
|
-
idx
|
|
276
|
-
);
|
|
277
|
-
});
|
|
278
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
279
|
-
"div",
|
|
280
|
-
{
|
|
281
|
-
style: {
|
|
282
|
-
fontSize: "13px",
|
|
283
|
-
maxWidth: "600px"
|
|
284
|
-
},
|
|
285
|
-
children: [
|
|
286
|
-
errorsToRender,
|
|
287
|
-
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { fontSize: "11px", opacity: 0.75 }, children: "NOTE: This error only displays during local development." })
|
|
288
|
-
]
|
|
289
|
-
}
|
|
290
|
-
);
|
|
291
|
-
}
|
|
292
|
-
function useErrorToast() {
|
|
293
|
-
const { addToast } = useToast();
|
|
294
|
-
return (0, import_react2.useCallback)(
|
|
295
|
-
(error) => {
|
|
296
|
-
const errorId = error.map((err) => {
|
|
297
|
-
var _a, _b;
|
|
298
|
-
const message = "extensions" in err ? ((_b = (_a = err.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message) || err.message : err.message;
|
|
299
|
-
const stack = err.stack || "";
|
|
300
|
-
return btoa(message + stack).slice(0, 32);
|
|
301
|
-
}).join("|");
|
|
302
|
-
addToast({
|
|
303
|
-
type: "error",
|
|
304
|
-
id: errorId,
|
|
305
|
-
// Toast libraries typically dedupe by id
|
|
306
|
-
message: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ErrorToast, { errors: error })
|
|
307
|
-
});
|
|
308
|
-
},
|
|
309
|
-
[addToast]
|
|
310
|
-
);
|
|
311
|
-
}
|
|
312
|
-
function useAsyncCallback(callback, deps) {
|
|
313
|
-
const addErrorToast = useErrorToast();
|
|
314
|
-
return (0, import_react2.useCallback)((...args) => __async(this, null, function* () {
|
|
315
|
-
try {
|
|
316
|
-
return yield callback(...args);
|
|
317
|
-
} catch (error) {
|
|
318
|
-
console.error("Error in async callback:", error);
|
|
319
|
-
addErrorToast([error]);
|
|
320
|
-
throw error;
|
|
321
|
-
}
|
|
322
|
-
}), deps);
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
// src/components/toast/toast-provider.tsx
|
|
326
|
-
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
327
|
-
var ToastContext = (0, import_react3.createContext)(void 0);
|
|
172
|
+
var ToastContext = (0, import_react2.createContext)(void 0);
|
|
328
173
|
function useToast() {
|
|
329
|
-
const context = (0,
|
|
174
|
+
const context = (0, import_react2.useContext)(ToastContext);
|
|
330
175
|
if (!context) {
|
|
331
176
|
throw new Error("useToast must be used within a ToastProvider");
|
|
332
177
|
}
|
|
@@ -334,18 +179,15 @@ function useToast() {
|
|
|
334
179
|
}
|
|
335
180
|
|
|
336
181
|
// src/hooks/use-copilot-runtime-client.ts
|
|
337
|
-
var
|
|
338
|
-
var
|
|
182
|
+
var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
|
|
183
|
+
var import_react3 = require("react");
|
|
339
184
|
var useCopilotRuntimeClient = (options) => {
|
|
340
185
|
const { addGraphQLErrorsToast } = useToast();
|
|
341
|
-
const
|
|
342
|
-
|
|
343
|
-
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), {
|
|
344
188
|
handleGQLErrors: (error) => {
|
|
345
189
|
if (error.graphQLErrors.length) {
|
|
346
190
|
addGraphQLErrorsToast(error.graphQLErrors);
|
|
347
|
-
} else {
|
|
348
|
-
addErrorToast([error]);
|
|
349
191
|
}
|
|
350
192
|
}
|
|
351
193
|
}));
|
|
@@ -366,309 +208,232 @@ function useChat(options) {
|
|
|
366
208
|
actions,
|
|
367
209
|
onFunctionCall,
|
|
368
210
|
onCoAgentStateRender,
|
|
369
|
-
|
|
370
|
-
|
|
211
|
+
setCoagentStates,
|
|
212
|
+
coagentStates,
|
|
371
213
|
agentSession,
|
|
372
|
-
setAgentSession
|
|
373
|
-
threadId,
|
|
374
|
-
setThreadId,
|
|
375
|
-
runId,
|
|
376
|
-
setRunId,
|
|
377
|
-
chatAbortControllerRef
|
|
214
|
+
setAgentSession
|
|
378
215
|
} = options;
|
|
216
|
+
const abortControllerRef = (0, import_react4.useRef)();
|
|
217
|
+
const threadIdRef = (0, import_react4.useRef)(null);
|
|
218
|
+
const runIdRef = (0, import_react4.useRef)(null);
|
|
379
219
|
const { addGraphQLErrorsToast } = useToast();
|
|
380
|
-
const runChatCompletionRef = (0,
|
|
381
|
-
const
|
|
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);
|
|
382
224
|
agentSessionRef.current = agentSession;
|
|
383
|
-
const threadIdRef = (0, import_react5.useRef)(threadId);
|
|
384
|
-
threadIdRef.current = threadId;
|
|
385
|
-
const runIdRef = (0, import_react5.useRef)(runId);
|
|
386
|
-
runIdRef.current = runId;
|
|
387
225
|
const publicApiKey = copilotConfig.publicApiKey;
|
|
388
|
-
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [
|
|
226
|
+
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [import_shared.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
|
|
389
227
|
const runtimeClient = useCopilotRuntimeClient({
|
|
390
228
|
url: copilotConfig.chatApiEndpoint,
|
|
391
229
|
publicApiKey: copilotConfig.publicApiKey,
|
|
392
230
|
headers,
|
|
393
231
|
credentials: copilotConfig.credentials
|
|
394
232
|
});
|
|
395
|
-
const runChatCompletion =
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
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
|
-
|
|
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
|
|
427
280
|
}
|
|
428
|
-
} : {})
|
|
429
|
-
} : {}), {
|
|
430
|
-
metadata: {
|
|
431
|
-
requestType: import_runtime_client_gql3.CopilotRequestType.Chat
|
|
432
|
-
}
|
|
433
|
-
}), agentSessionRef.current ? {
|
|
434
|
-
agentSession: agentSessionRef.current
|
|
435
|
-
} : {}), {
|
|
436
|
-
agentStates: Object.values(coagentStatesRef.current).map((state) => ({
|
|
437
|
-
agentName: state.name,
|
|
438
|
-
state: JSON.stringify(state.state)
|
|
439
|
-
})),
|
|
440
|
-
forwardedParameters: options.forwardedParameters || {}
|
|
441
|
-
}),
|
|
442
|
-
properties: copilotConfig.properties,
|
|
443
|
-
signal: (_d = chatAbortControllerRef.current) == null ? void 0 : _d.signal
|
|
444
|
-
})
|
|
445
|
-
);
|
|
446
|
-
const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
|
|
447
|
-
const reader = stream.getReader();
|
|
448
|
-
let executedCoAgentStateRenders = [];
|
|
449
|
-
let followUp = void 0;
|
|
450
|
-
let messages2 = [];
|
|
451
|
-
let syncedMessages = [];
|
|
452
|
-
try {
|
|
453
|
-
while (true) {
|
|
454
|
-
let done, value;
|
|
455
|
-
try {
|
|
456
|
-
const readResult = yield reader.read();
|
|
457
|
-
done = readResult.done;
|
|
458
|
-
value = readResult.value;
|
|
459
|
-
} catch (readError) {
|
|
460
|
-
break;
|
|
461
|
-
}
|
|
462
|
-
if (done) {
|
|
463
|
-
if (chatAbortControllerRef.current.signal.aborted) {
|
|
464
|
-
return [];
|
|
465
281
|
}
|
|
466
|
-
|
|
282
|
+
} : {})
|
|
283
|
+
} : {}), {
|
|
284
|
+
metadata: {
|
|
285
|
+
requestType: import_runtime_client_gql2.CopilotRequestType.Chat
|
|
467
286
|
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
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)) {
|
|
495
341
|
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
496
342
|
break;
|
|
497
343
|
}
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
}
|
|
506
|
-
const lastAgentStateMessage = [...messages2].reverse().find((message) => message.isAgentStateMessage());
|
|
507
|
-
if (lastAgentStateMessage) {
|
|
508
|
-
if (lastAgentStateMessage.state.messages && lastAgentStateMessage.state.messages.length > 0) {
|
|
509
|
-
syncedMessages = (0, import_runtime_client_gql3.loadMessagesFromJsonRepresentation)(
|
|
510
|
-
lastAgentStateMessage.state.messages
|
|
511
|
-
);
|
|
512
|
-
}
|
|
513
|
-
setCoagentStatesWithRef((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
514
|
-
[lastAgentStateMessage.agentName]: {
|
|
515
|
-
name: lastAgentStateMessage.agentName,
|
|
516
|
-
state: lastAgentStateMessage.state,
|
|
517
|
-
running: lastAgentStateMessage.running,
|
|
518
|
-
active: lastAgentStateMessage.active,
|
|
519
|
-
threadId: lastAgentStateMessage.threadId,
|
|
520
|
-
nodeName: lastAgentStateMessage.nodeName,
|
|
521
|
-
runId: lastAgentStateMessage.runId
|
|
522
|
-
}
|
|
523
|
-
}));
|
|
524
|
-
if (lastAgentStateMessage.running) {
|
|
525
|
-
setAgentSession({
|
|
526
|
-
threadId: lastAgentStateMessage.threadId,
|
|
527
|
-
agentName: lastAgentStateMessage.agentName,
|
|
528
|
-
nodeName: lastAgentStateMessage.nodeName
|
|
529
|
-
});
|
|
530
|
-
} else {
|
|
531
|
-
setAgentSession(null);
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
if (newMessages.length > 0) {
|
|
536
|
-
setMessages([...previousMessages, ...newMessages]);
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
const finalMessages = constructFinalMessages(syncedMessages, previousMessages, newMessages);
|
|
540
|
-
let didExecuteAction = false;
|
|
541
|
-
if (onFunctionCall) {
|
|
542
|
-
const lastMessages = [];
|
|
543
|
-
for (let i = finalMessages.length - 1; i >= 0; i--) {
|
|
544
|
-
const message = finalMessages[i];
|
|
545
|
-
if (message.isActionExecutionMessage() && message.status.code !== import_runtime_client_gql3.MessageStatusCode.Pending) {
|
|
546
|
-
lastMessages.unshift(message);
|
|
547
|
-
} else {
|
|
548
|
-
break;
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
for (const message of lastMessages) {
|
|
552
|
-
setMessages(finalMessages);
|
|
553
|
-
const action = actions.find((action2) => action2.name === message.name);
|
|
554
|
-
if (action) {
|
|
555
|
-
followUp = action.followUp;
|
|
556
|
-
let result;
|
|
557
|
-
try {
|
|
558
|
-
result = yield Promise.race([
|
|
559
|
-
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({
|
|
560
351
|
messages: previousMessages,
|
|
561
352
|
name: message.name,
|
|
562
353
|
args: message.arguments
|
|
563
|
-
})
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
() => resolve("Operation was aborted by the user")
|
|
570
|
-
);
|
|
571
|
-
}
|
|
572
|
-
),
|
|
573
|
-
// if the user stopped generation, we also abort consecutive actions
|
|
574
|
-
new Promise((resolve) => {
|
|
575
|
-
var _a2;
|
|
576
|
-
if ((_a2 = chatAbortControllerRef.current) == null ? void 0 : _a2.signal.aborted) {
|
|
577
|
-
resolve("Operation was aborted by the user");
|
|
578
|
-
}
|
|
579
|
-
})
|
|
580
|
-
]);
|
|
581
|
-
} catch (e) {
|
|
582
|
-
result = `Failed to execute action ${message.name}`;
|
|
583
|
-
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
|
+
}
|
|
584
360
|
}
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
messageIndex + 1,
|
|
589
|
-
0,
|
|
590
|
-
new import_runtime_client_gql3.ResultMessage({
|
|
591
|
-
id: "result-" + message.id,
|
|
592
|
-
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]),
|
|
593
364
|
actionExecutionId: message.id,
|
|
594
365
|
actionName: message.name
|
|
595
366
|
})
|
|
596
367
|
);
|
|
597
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
|
+
}
|
|
598
380
|
}
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
if (
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
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);
|
|
616
402
|
}
|
|
617
|
-
return true;
|
|
618
|
-
});
|
|
619
|
-
const repairedMessageIds = repairedMessages.map((message) => message.id);
|
|
620
|
-
setMessages(repairedMessages);
|
|
621
|
-
if ((_l = agentSessionRef.current) == null ? void 0 : _l.nodeName) {
|
|
622
|
-
setAgentSession({
|
|
623
|
-
threadId: agentSessionRef.current.threadId,
|
|
624
|
-
agentName: agentSessionRef.current.agentName,
|
|
625
|
-
nodeName: "__end__"
|
|
626
|
-
});
|
|
627
403
|
}
|
|
628
|
-
return newMessages.filter((message) => repairedMessageIds.includes(message.id));
|
|
629
|
-
} else {
|
|
630
|
-
return newMessages.slice();
|
|
631
404
|
}
|
|
632
|
-
|
|
633
|
-
|
|
405
|
+
if (newMessages.length > 0) {
|
|
406
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
407
|
+
}
|
|
634
408
|
}
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
onFunctionCall,
|
|
646
|
-
onCoAgentStateRender,
|
|
647
|
-
setCoagentStatesWithRef,
|
|
648
|
-
coagentStatesRef,
|
|
649
|
-
agentSession,
|
|
650
|
-
setAgentSession
|
|
651
|
-
]
|
|
652
|
-
);
|
|
653
|
-
runChatCompletionRef.current = runChatCompletion;
|
|
654
|
-
const runChatCompletionAndHandleFunctionCall = useAsyncCallback(
|
|
655
|
-
(messages2) => __async(this, null, function* () {
|
|
656
|
-
yield runChatCompletionRef.current(messages2);
|
|
657
|
-
}),
|
|
658
|
-
[messages]
|
|
659
|
-
);
|
|
660
|
-
const append = useAsyncCallback(
|
|
661
|
-
(message) => __async(this, null, function* () {
|
|
662
|
-
if (isLoading) {
|
|
663
|
-
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();
|
|
664
419
|
}
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
)
|
|
671
|
-
|
|
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* () {
|
|
672
437
|
if (isLoading || messages.length === 0) {
|
|
673
438
|
return;
|
|
674
439
|
}
|
|
@@ -679,50 +444,32 @@ function useChat(options) {
|
|
|
679
444
|
}
|
|
680
445
|
setMessages(newMessages);
|
|
681
446
|
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
682
|
-
})
|
|
447
|
+
});
|
|
683
448
|
const stop = () => {
|
|
684
449
|
var _a;
|
|
685
|
-
(_a =
|
|
450
|
+
(_a = abortControllerRef.current) == null ? void 0 : _a.abort();
|
|
686
451
|
};
|
|
687
452
|
return {
|
|
688
453
|
append,
|
|
689
454
|
reload,
|
|
690
|
-
stop
|
|
691
|
-
runChatCompletion: () => runChatCompletionRef.current(messages)
|
|
455
|
+
stop
|
|
692
456
|
};
|
|
693
457
|
}
|
|
694
|
-
function constructFinalMessages(syncedMessages, previousMessages, newMessages) {
|
|
695
|
-
const finalMessages = syncedMessages.length > 0 ? [...syncedMessages] : [...previousMessages, ...newMessages];
|
|
696
|
-
if (syncedMessages.length > 0) {
|
|
697
|
-
const messagesWithAgentState = [...previousMessages, ...newMessages];
|
|
698
|
-
let previousMessageId = void 0;
|
|
699
|
-
for (const message of messagesWithAgentState) {
|
|
700
|
-
if (message.isAgentStateMessage()) {
|
|
701
|
-
const index = finalMessages.findIndex((msg) => msg.id === previousMessageId);
|
|
702
|
-
if (index !== -1) {
|
|
703
|
-
finalMessages.splice(index + 1, 0, message);
|
|
704
|
-
}
|
|
705
|
-
}
|
|
706
|
-
previousMessageId = message.id;
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
return finalMessages;
|
|
710
|
-
}
|
|
711
458
|
|
|
712
459
|
// src/components/copilot-provider/copilotkit.tsx
|
|
713
|
-
var
|
|
460
|
+
var import_react6 = require("react");
|
|
714
461
|
var import_react_dom = require("react-dom");
|
|
715
|
-
var
|
|
462
|
+
var import_shared2 = require("@copilotkit/shared");
|
|
716
463
|
|
|
717
464
|
// src/context/copilot-messages-context.tsx
|
|
718
|
-
var
|
|
465
|
+
var import_react5 = __toESM(require("react"));
|
|
719
466
|
var emptyCopilotContext2 = {
|
|
720
467
|
messages: [],
|
|
721
468
|
setMessages: () => []
|
|
722
469
|
};
|
|
723
|
-
var CopilotMessagesContext =
|
|
470
|
+
var CopilotMessagesContext = import_react5.default.createContext(emptyCopilotContext2);
|
|
724
471
|
function useCopilotMessagesContext() {
|
|
725
|
-
const context =
|
|
472
|
+
const context = import_react5.default.useContext(CopilotMessagesContext);
|
|
726
473
|
if (context === emptyCopilotContext2) {
|
|
727
474
|
throw new Error(
|
|
728
475
|
"A messages consuming component was not wrapped with `<CopilotMessages> {...} </CopilotMessages>`"
|
|
@@ -732,7 +479,7 @@ function useCopilotMessagesContext() {
|
|
|
732
479
|
}
|
|
733
480
|
|
|
734
481
|
// src/components/copilot-provider/copilotkit.tsx
|
|
735
|
-
var
|
|
482
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
736
483
|
var defaultCopilotContextCategories = ["global"];
|
|
737
484
|
|
|
738
485
|
// src/hooks/use-copilot-chat.ts
|
|
@@ -750,36 +497,29 @@ function useCopilotChat(_a = {}) {
|
|
|
750
497
|
setIsLoading,
|
|
751
498
|
chatInstructions,
|
|
752
499
|
actions,
|
|
753
|
-
|
|
754
|
-
|
|
500
|
+
coagentStates,
|
|
501
|
+
setCoagentStates,
|
|
755
502
|
coAgentStateRenders,
|
|
756
503
|
agentSession,
|
|
757
|
-
setAgentSession
|
|
758
|
-
forwardedParameters,
|
|
759
|
-
agentLock,
|
|
760
|
-
threadId,
|
|
761
|
-
setThreadId,
|
|
762
|
-
runId,
|
|
763
|
-
setRunId,
|
|
764
|
-
chatAbortControllerRef
|
|
504
|
+
setAgentSession
|
|
765
505
|
} = useCopilotContext();
|
|
766
506
|
const { messages, setMessages } = useCopilotMessagesContext();
|
|
767
507
|
const latestGetContextString = useUpdatedRef(getContextString);
|
|
768
|
-
const deleteMessage = (0,
|
|
508
|
+
const deleteMessage = (0, import_react7.useCallback)(
|
|
769
509
|
(messageId) => {
|
|
770
510
|
setMessages((prev) => prev.filter((message) => message.id !== messageId));
|
|
771
511
|
},
|
|
772
512
|
[setMessages]
|
|
773
513
|
);
|
|
774
|
-
const makeSystemMessageCallback = (0,
|
|
514
|
+
const makeSystemMessageCallback = (0, import_react7.useCallback)(() => {
|
|
775
515
|
const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
|
|
776
516
|
const contextString = latestGetContextString.current([], defaultCopilotContextCategories);
|
|
777
|
-
return new
|
|
517
|
+
return new import_runtime_client_gql3.TextMessage({
|
|
778
518
|
content: systemMessageMaker(contextString, chatInstructions),
|
|
779
|
-
role:
|
|
519
|
+
role: import_runtime_client_gql3.Role.System
|
|
780
520
|
});
|
|
781
521
|
}, [getContextString, makeSystemMessage, chatInstructions]);
|
|
782
|
-
const onCoAgentStateRender =
|
|
522
|
+
const onCoAgentStateRender = (0, import_react7.useCallback)(
|
|
783
523
|
(args) => __async(this, null, function* () {
|
|
784
524
|
var _a2;
|
|
785
525
|
const { name, nodeName, state } = args;
|
|
@@ -797,7 +537,7 @@ function useCopilotChat(_a = {}) {
|
|
|
797
537
|
}),
|
|
798
538
|
[coAgentStateRenders]
|
|
799
539
|
);
|
|
800
|
-
const { append, reload, stop
|
|
540
|
+
const { append, reload, stop } = useChat(__spreadProps(__spreadValues({}, options), {
|
|
801
541
|
actions: Object.values(actions),
|
|
802
542
|
copilotConfig: copilotApiConfig,
|
|
803
543
|
initialMessages: options.initialMessages || [],
|
|
@@ -808,90 +548,53 @@ function useCopilotChat(_a = {}) {
|
|
|
808
548
|
makeSystemMessageCallback,
|
|
809
549
|
isLoading,
|
|
810
550
|
setIsLoading,
|
|
811
|
-
|
|
812
|
-
|
|
551
|
+
coagentStates,
|
|
552
|
+
setCoagentStates,
|
|
813
553
|
agentSession,
|
|
814
|
-
setAgentSession
|
|
815
|
-
forwardedParameters,
|
|
816
|
-
threadId,
|
|
817
|
-
setThreadId,
|
|
818
|
-
runId,
|
|
819
|
-
setRunId,
|
|
820
|
-
chatAbortControllerRef
|
|
554
|
+
setAgentSession
|
|
821
555
|
}));
|
|
822
556
|
const latestAppend = useUpdatedRef(append);
|
|
823
|
-
const latestAppendFunc =
|
|
824
|
-
(message) =>
|
|
825
|
-
return
|
|
826
|
-
}
|
|
557
|
+
const latestAppendFunc = (0, import_react7.useCallback)(
|
|
558
|
+
(message) => {
|
|
559
|
+
return latestAppend.current(message);
|
|
560
|
+
},
|
|
827
561
|
[latestAppend]
|
|
828
562
|
);
|
|
829
563
|
const latestReload = useUpdatedRef(reload);
|
|
830
|
-
const latestReloadFunc =
|
|
831
|
-
return
|
|
832
|
-
}
|
|
564
|
+
const latestReloadFunc = (0, import_react7.useCallback)(() => {
|
|
565
|
+
return latestReload.current();
|
|
566
|
+
}, [latestReload]);
|
|
833
567
|
const latestStop = useUpdatedRef(stop);
|
|
834
|
-
const latestStopFunc = (0,
|
|
568
|
+
const latestStopFunc = (0, import_react7.useCallback)(() => {
|
|
835
569
|
return latestStop.current();
|
|
836
570
|
}, [latestStop]);
|
|
837
571
|
const latestDelete = useUpdatedRef(deleteMessage);
|
|
838
|
-
const latestDeleteFunc = (0,
|
|
572
|
+
const latestDeleteFunc = (0, import_react7.useCallback)(
|
|
839
573
|
(messageId) => {
|
|
840
574
|
return latestDelete.current(messageId);
|
|
841
575
|
},
|
|
842
576
|
[latestDelete]
|
|
843
577
|
);
|
|
844
578
|
const latestSetMessages = useUpdatedRef(setMessages);
|
|
845
|
-
const latestSetMessagesFunc = (0,
|
|
579
|
+
const latestSetMessagesFunc = (0, import_react7.useCallback)(
|
|
846
580
|
(messages2) => {
|
|
847
581
|
return latestSetMessages.current(messages2);
|
|
848
582
|
},
|
|
849
583
|
[latestSetMessages]
|
|
850
584
|
);
|
|
851
|
-
const latestRunChatCompletion = useUpdatedRef(runChatCompletion);
|
|
852
|
-
const latestRunChatCompletionFunc = useAsyncCallback(() => __async(this, null, function* () {
|
|
853
|
-
return yield latestRunChatCompletion.current();
|
|
854
|
-
}), [latestRunChatCompletion]);
|
|
855
|
-
const reset = (0, import_react8.useCallback)(() => {
|
|
856
|
-
latestStopFunc();
|
|
857
|
-
setMessages([]);
|
|
858
|
-
setThreadId(null);
|
|
859
|
-
setRunId(null);
|
|
860
|
-
setCoagentStatesWithRef({});
|
|
861
|
-
let initialAgentSession = null;
|
|
862
|
-
if (agentLock) {
|
|
863
|
-
initialAgentSession = {
|
|
864
|
-
agentName: agentLock
|
|
865
|
-
};
|
|
866
|
-
}
|
|
867
|
-
setAgentSession(initialAgentSession);
|
|
868
|
-
}, [
|
|
869
|
-
latestStopFunc,
|
|
870
|
-
setMessages,
|
|
871
|
-
setThreadId,
|
|
872
|
-
setCoagentStatesWithRef,
|
|
873
|
-
setAgentSession,
|
|
874
|
-
agentLock
|
|
875
|
-
]);
|
|
876
|
-
const latestReset = useUpdatedRef(reset);
|
|
877
|
-
const latestResetFunc = (0, import_react8.useCallback)(() => {
|
|
878
|
-
return latestReset.current();
|
|
879
|
-
}, [latestReset]);
|
|
880
585
|
return {
|
|
881
586
|
visibleMessages: messages,
|
|
882
587
|
appendMessage: latestAppendFunc,
|
|
883
588
|
setMessages: latestSetMessagesFunc,
|
|
884
589
|
reloadMessages: latestReloadFunc,
|
|
885
590
|
stopGeneration: latestStopFunc,
|
|
886
|
-
reset: latestResetFunc,
|
|
887
591
|
deleteMessage: latestDeleteFunc,
|
|
888
|
-
runChatCompletion: latestRunChatCompletionFunc,
|
|
889
592
|
isLoading
|
|
890
593
|
};
|
|
891
594
|
}
|
|
892
595
|
function useUpdatedRef(value) {
|
|
893
|
-
const ref = (0,
|
|
894
|
-
(0,
|
|
596
|
+
const ref = (0, import_react7.useRef)(value);
|
|
597
|
+
(0, import_react7.useEffect)(() => {
|
|
895
598
|
ref.current = value;
|
|
896
599
|
}, [value]);
|
|
897
600
|
return ref;
|
|
@@ -921,22 +624,18 @@ ${additionalInstructions}` : "");
|
|
|
921
624
|
}
|
|
922
625
|
|
|
923
626
|
// src/hooks/use-copilot-action.ts
|
|
924
|
-
var
|
|
925
|
-
var
|
|
627
|
+
var import_shared3 = require("@copilotkit/shared");
|
|
628
|
+
var import_react8 = require("react");
|
|
926
629
|
function useCopilotAction(action, dependencies) {
|
|
927
630
|
const { setAction, removeAction, actions, chatComponentsCache } = useCopilotContext();
|
|
928
|
-
const idRef = (0,
|
|
929
|
-
const renderAndWaitRef = (0,
|
|
631
|
+
const idRef = (0, import_react8.useRef)((0, import_shared3.randomId)());
|
|
632
|
+
const renderAndWaitRef = (0, import_react8.useRef)(null);
|
|
930
633
|
action = __spreadValues({}, action);
|
|
931
|
-
if (
|
|
932
|
-
// renderAndWaitForResponse is not available for catch all actions
|
|
933
|
-
isFrontendAction(action) && // check if renderAndWaitForResponse is set
|
|
934
|
-
(action.renderAndWait || action.renderAndWaitForResponse)
|
|
935
|
-
) {
|
|
634
|
+
if (action.renderAndWait || action.renderAndWaitForResponse) {
|
|
936
635
|
const renderAndWait = action.renderAndWait || action.renderAndWaitForResponse;
|
|
937
636
|
action.renderAndWait = void 0;
|
|
938
637
|
action.renderAndWaitForResponse = void 0;
|
|
939
|
-
action.handler =
|
|
638
|
+
action.handler = () => __async(this, null, function* () {
|
|
940
639
|
let resolve;
|
|
941
640
|
let reject;
|
|
942
641
|
const promise = new Promise((resolvePromise, rejectPromise) => {
|
|
@@ -945,18 +644,14 @@ function useCopilotAction(action, dependencies) {
|
|
|
945
644
|
});
|
|
946
645
|
renderAndWaitRef.current = { promise, resolve, reject };
|
|
947
646
|
return yield promise;
|
|
948
|
-
})
|
|
647
|
+
});
|
|
949
648
|
action.render = (props) => {
|
|
950
|
-
let status = props.status;
|
|
951
|
-
if (props.status === "executing" && !renderAndWaitRef.current) {
|
|
952
|
-
status = "inProgress";
|
|
953
|
-
}
|
|
954
649
|
const waitProps = {
|
|
955
|
-
status,
|
|
650
|
+
status: props.status,
|
|
956
651
|
args: props.args,
|
|
957
652
|
result: props.result,
|
|
958
|
-
handler: status === "executing" ? renderAndWaitRef.current.resolve : void 0,
|
|
959
|
-
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
|
|
960
655
|
};
|
|
961
656
|
const isNoArgsRenderWait = (_fn) => {
|
|
962
657
|
var _a;
|
|
@@ -969,14 +664,12 @@ function useCopilotAction(action, dependencies) {
|
|
|
969
664
|
return renderAndWait(waitProps);
|
|
970
665
|
}
|
|
971
666
|
}
|
|
972
|
-
return (0,
|
|
667
|
+
return (0, import_react8.createElement)(import_react8.Fragment);
|
|
973
668
|
};
|
|
974
669
|
}
|
|
975
670
|
if (dependencies === void 0) {
|
|
976
671
|
if (actions[idRef.current]) {
|
|
977
|
-
|
|
978
|
-
actions[idRef.current].handler = action.handler;
|
|
979
|
-
}
|
|
672
|
+
actions[idRef.current].handler = action.handler;
|
|
980
673
|
if (typeof action.render === "function") {
|
|
981
674
|
if (chatComponentsCache.current !== null) {
|
|
982
675
|
chatComponentsCache.current.actions[action.name] = action.render;
|
|
@@ -984,7 +677,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
984
677
|
}
|
|
985
678
|
}
|
|
986
679
|
}
|
|
987
|
-
(0,
|
|
680
|
+
(0, import_react8.useEffect)(() => {
|
|
988
681
|
setAction(idRef.current, action);
|
|
989
682
|
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
990
683
|
chatComponentsCache.current.actions[action.name] = action.render;
|
|
@@ -995,34 +688,31 @@ function useCopilotAction(action, dependencies) {
|
|
|
995
688
|
}, [
|
|
996
689
|
setAction,
|
|
997
690
|
removeAction,
|
|
998
|
-
|
|
691
|
+
action.description,
|
|
999
692
|
action.name,
|
|
1000
|
-
|
|
1001
|
-
|
|
693
|
+
action.disabled,
|
|
694
|
+
action.available,
|
|
1002
695
|
// This should be faster than deep equality checking
|
|
1003
696
|
// In addition, all major JS engines guarantee the order of object keys
|
|
1004
|
-
JSON.stringify(
|
|
697
|
+
JSON.stringify(action.parameters),
|
|
1005
698
|
// include render only if it's a string
|
|
1006
699
|
typeof action.render === "string" ? action.render : void 0,
|
|
1007
700
|
// dependencies set by the developer
|
|
1008
701
|
...dependencies || []
|
|
1009
702
|
]);
|
|
1010
703
|
}
|
|
1011
|
-
function isFrontendAction(action) {
|
|
1012
|
-
return action.name !== "*";
|
|
1013
|
-
}
|
|
1014
704
|
|
|
1015
705
|
// src/hooks/use-coagent-state-render.ts
|
|
1016
|
-
var
|
|
1017
|
-
var
|
|
706
|
+
var import_react9 = require("react");
|
|
707
|
+
var import_shared4 = require("@copilotkit/shared");
|
|
1018
708
|
function useCoAgentStateRender(action, dependencies) {
|
|
1019
709
|
const {
|
|
1020
710
|
setCoAgentStateRender,
|
|
1021
711
|
removeCoAgentStateRender,
|
|
1022
712
|
coAgentStateRenders,
|
|
1023
713
|
chatComponentsCache
|
|
1024
|
-
} = (0,
|
|
1025
|
-
const idRef = (0,
|
|
714
|
+
} = (0, import_react9.useContext)(CopilotContext);
|
|
715
|
+
const idRef = (0, import_react9.useRef)((0, import_shared4.randomId)());
|
|
1026
716
|
const key = `${action.name}-${action.nodeName || "global"}`;
|
|
1027
717
|
if (dependencies === void 0) {
|
|
1028
718
|
if (coAgentStateRenders[idRef.current]) {
|
|
@@ -1034,7 +724,7 @@ function useCoAgentStateRender(action, dependencies) {
|
|
|
1034
724
|
}
|
|
1035
725
|
}
|
|
1036
726
|
}
|
|
1037
|
-
(0,
|
|
727
|
+
(0, import_react9.useEffect)(() => {
|
|
1038
728
|
setCoAgentStateRender(idRef.current, action);
|
|
1039
729
|
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
1040
730
|
chatComponentsCache.current.coAgentStateRenders[key] = action.render;
|
|
@@ -1054,11 +744,11 @@ function useCoAgentStateRender(action, dependencies) {
|
|
|
1054
744
|
}
|
|
1055
745
|
|
|
1056
746
|
// src/hooks/use-make-copilot-document-readable.ts
|
|
1057
|
-
var
|
|
747
|
+
var import_react10 = require("react");
|
|
1058
748
|
function useMakeCopilotDocumentReadable(document, categories, dependencies = []) {
|
|
1059
749
|
const { addDocumentContext, removeDocumentContext } = useCopilotContext();
|
|
1060
|
-
const idRef = (0,
|
|
1061
|
-
(0,
|
|
750
|
+
const idRef = (0, import_react10.useRef)();
|
|
751
|
+
(0, import_react10.useEffect)(() => {
|
|
1062
752
|
const id = addDocumentContext(document, categories);
|
|
1063
753
|
idRef.current = id;
|
|
1064
754
|
return () => {
|
|
@@ -1069,16 +759,16 @@ function useMakeCopilotDocumentReadable(document, categories, dependencies = [])
|
|
|
1069
759
|
}
|
|
1070
760
|
|
|
1071
761
|
// src/hooks/use-copilot-readable.ts
|
|
1072
|
-
var
|
|
762
|
+
var import_react11 = require("react");
|
|
1073
763
|
function convertToJSON(description, value) {
|
|
1074
764
|
return `${description}: ${typeof value === "string" ? value : JSON.stringify(value)}`;
|
|
1075
765
|
}
|
|
1076
766
|
function useCopilotReadable({ description, value, parentId, categories, convert }, dependencies) {
|
|
1077
767
|
const { addContext, removeContext } = useCopilotContext();
|
|
1078
|
-
const idRef = (0,
|
|
768
|
+
const idRef = (0, import_react11.useRef)();
|
|
1079
769
|
convert = convert || convertToJSON;
|
|
1080
770
|
const information = convert(description, value);
|
|
1081
|
-
(0,
|
|
771
|
+
(0, import_react11.useEffect)(() => {
|
|
1082
772
|
const id = addContext(information, parentId, categories);
|
|
1083
773
|
idRef.current = id;
|
|
1084
774
|
return () => {
|
|
@@ -1089,7 +779,7 @@ function useCopilotReadable({ description, value, parentId, categories, convert
|
|
|
1089
779
|
}
|
|
1090
780
|
|
|
1091
781
|
// src/hooks/use-coagent.ts
|
|
1092
|
-
var
|
|
782
|
+
var import_react12 = require("react");
|
|
1093
783
|
function useCoAgent(options) {
|
|
1094
784
|
const isExternalStateManagement = (options2) => {
|
|
1095
785
|
return "state" in options2 && "setState" in options2;
|
|
@@ -1101,8 +791,8 @@ function useCoAgent(options) {
|
|
|
1101
791
|
const generalContext = useCopilotContext();
|
|
1102
792
|
const messagesContext = useCopilotMessagesContext();
|
|
1103
793
|
const context = __spreadValues(__spreadValues({}, generalContext), messagesContext);
|
|
1104
|
-
const { coagentStates,
|
|
1105
|
-
const { appendMessage
|
|
794
|
+
const { coagentStates, setCoagentStates } = context;
|
|
795
|
+
const { appendMessage } = useCopilotChat();
|
|
1106
796
|
const getCoagentState = (coagentStates2, name2) => {
|
|
1107
797
|
if (coagentStates2[name2]) {
|
|
1108
798
|
return coagentStates2[name2];
|
|
@@ -1119,43 +809,40 @@ function useCoAgent(options) {
|
|
|
1119
809
|
}
|
|
1120
810
|
};
|
|
1121
811
|
const setState = (newState) => {
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
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
|
+
});
|
|
1129
821
|
};
|
|
1130
822
|
const coagentState = getCoagentState(coagentStates, name);
|
|
1131
823
|
const state = isExternalStateManagement(options) ? options.state : coagentState.state;
|
|
1132
|
-
(0,
|
|
824
|
+
(0, import_react12.useEffect)(() => {
|
|
1133
825
|
if (isExternalStateManagement(options)) {
|
|
1134
826
|
setState(options.state);
|
|
1135
827
|
} else if (coagentStates[name] === void 0) {
|
|
1136
828
|
setState(options.initialState === void 0 ? {} : options.initialState);
|
|
1137
829
|
}
|
|
1138
|
-
}, [
|
|
1139
|
-
isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0,
|
|
1140
|
-
// reset initialstate on reset
|
|
1141
|
-
coagentStates[name] === void 0
|
|
1142
|
-
]);
|
|
1143
|
-
const runAgentCallback = useAsyncCallback(
|
|
1144
|
-
(hint) => __async(this, null, function* () {
|
|
1145
|
-
yield runAgent(name, context, appendMessage, runChatCompletion, hint);
|
|
1146
|
-
}),
|
|
1147
|
-
[name, context, appendMessage, runChatCompletion]
|
|
1148
|
-
);
|
|
830
|
+
}, [isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0]);
|
|
1149
831
|
return {
|
|
1150
832
|
name,
|
|
1151
833
|
nodeName: coagentState.nodeName,
|
|
1152
|
-
|
|
834
|
+
state,
|
|
835
|
+
setState,
|
|
1153
836
|
running: coagentState.running,
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
stop: () =>
|
|
1158
|
-
|
|
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
|
+
}
|
|
1159
846
|
};
|
|
1160
847
|
}
|
|
1161
848
|
function startAgent(name, context) {
|
|
@@ -1168,22 +855,11 @@ function stopAgent(name, context) {
|
|
|
1168
855
|
const { agentSession, setAgentSession } = context;
|
|
1169
856
|
if (agentSession && agentSession.agentName === name) {
|
|
1170
857
|
setAgentSession(null);
|
|
1171
|
-
context.setCoagentStates((prevAgentStates) => {
|
|
1172
|
-
return __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
1173
|
-
[name]: __spreadProps(__spreadValues({}, prevAgentStates[name]), {
|
|
1174
|
-
running: false,
|
|
1175
|
-
active: false,
|
|
1176
|
-
threadId: void 0,
|
|
1177
|
-
nodeName: void 0,
|
|
1178
|
-
runId: void 0
|
|
1179
|
-
})
|
|
1180
|
-
});
|
|
1181
|
-
});
|
|
1182
858
|
} else {
|
|
1183
859
|
console.warn(`No agent session found for ${name}`);
|
|
1184
860
|
}
|
|
1185
861
|
}
|
|
1186
|
-
function runAgent(name, context, appendMessage,
|
|
862
|
+
function runAgent(name, context, appendMessage, hint) {
|
|
1187
863
|
return __async(this, null, function* () {
|
|
1188
864
|
var _a, _b;
|
|
1189
865
|
const { agentSession, setAgentSession } = context;
|
|
@@ -1199,24 +875,17 @@ function runAgent(name, context, appendMessage, runChatCompletion, hint) {
|
|
|
1199
875
|
previousState = message.state;
|
|
1200
876
|
}
|
|
1201
877
|
}
|
|
1202
|
-
let state = ((_b = (_a = context.
|
|
878
|
+
let state = ((_b = (_a = context.coagentStates) == null ? void 0 : _a[name]) == null ? void 0 : _b.state) || {};
|
|
1203
879
|
if (hint) {
|
|
1204
880
|
const hintMessage = hint({ previousState, currentState: state });
|
|
1205
881
|
if (hintMessage) {
|
|
1206
882
|
yield appendMessage(hintMessage);
|
|
1207
|
-
} else {
|
|
1208
|
-
yield runChatCompletion();
|
|
1209
883
|
}
|
|
1210
|
-
} else {
|
|
1211
|
-
yield runChatCompletion();
|
|
1212
884
|
}
|
|
1213
885
|
});
|
|
1214
886
|
}
|
|
1215
887
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1216
888
|
0 && (module.exports = {
|
|
1217
|
-
runAgent,
|
|
1218
|
-
startAgent,
|
|
1219
|
-
stopAgent,
|
|
1220
889
|
useCoAgent,
|
|
1221
890
|
useCoAgentStateRender,
|
|
1222
891
|
useCopilotAction,
|