@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/use-chat.js
CHANGED
|
@@ -60,156 +60,16 @@ __export(use_chat_exports, {
|
|
|
60
60
|
useChat: () => useChat
|
|
61
61
|
});
|
|
62
62
|
module.exports = __toCommonJS(use_chat_exports);
|
|
63
|
-
var
|
|
64
|
-
var import_shared2 = require("@copilotkit/shared");
|
|
65
|
-
var import_runtime_client_gql3 = require("@copilotkit/runtime-client-gql");
|
|
66
|
-
|
|
67
|
-
// src/types/frontend-action.ts
|
|
68
|
-
var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
|
|
63
|
+
var import_react3 = require("react");
|
|
69
64
|
var import_shared = require("@copilotkit/shared");
|
|
70
|
-
|
|
71
|
-
const filteredActions = actions.filter(
|
|
72
|
-
(action) => action.available !== import_runtime_client_gql.ActionInputAvailability.Disabled && action.disabled !== true && action.name !== "*"
|
|
73
|
-
).map((action) => {
|
|
74
|
-
let available = import_runtime_client_gql.ActionInputAvailability.Enabled;
|
|
75
|
-
if (action.disabled) {
|
|
76
|
-
available = import_runtime_client_gql.ActionInputAvailability.Disabled;
|
|
77
|
-
} else if (action.available === "disabled") {
|
|
78
|
-
available = import_runtime_client_gql.ActionInputAvailability.Disabled;
|
|
79
|
-
} else if (action.available === "remote") {
|
|
80
|
-
available = import_runtime_client_gql.ActionInputAvailability.Remote;
|
|
81
|
-
}
|
|
82
|
-
return {
|
|
83
|
-
name: action.name,
|
|
84
|
-
description: action.description || "",
|
|
85
|
-
jsonSchema: JSON.stringify((0, import_shared.actionParametersToJsonSchema)(action.parameters || [])),
|
|
86
|
-
available
|
|
87
|
-
};
|
|
88
|
-
});
|
|
89
|
-
return filteredActions;
|
|
90
|
-
}
|
|
65
|
+
var import_runtime_client_gql2 = require("@copilotkit/runtime-client-gql");
|
|
91
66
|
|
|
92
67
|
// src/components/toast/toast-provider.tsx
|
|
93
|
-
var import_react2 = require("react");
|
|
94
|
-
|
|
95
|
-
// src/components/error-boundary/error-utils.tsx
|
|
96
68
|
var import_react = require("react");
|
|
97
|
-
|
|
98
|
-
// src/components/toast/exclamation-mark-icon.tsx
|
|
99
69
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
100
|
-
var
|
|
101
|
-
className,
|
|
102
|
-
style
|
|
103
|
-
}) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
|
|
104
|
-
"svg",
|
|
105
|
-
{
|
|
106
|
-
xmlns: "http://www.w3.org/2000/svg",
|
|
107
|
-
width: "24",
|
|
108
|
-
height: "24",
|
|
109
|
-
viewBox: "0 0 24 24",
|
|
110
|
-
fill: "none",
|
|
111
|
-
stroke: "currentColor",
|
|
112
|
-
strokeWidth: "2",
|
|
113
|
-
strokeLinecap: "round",
|
|
114
|
-
strokeLinejoin: "round",
|
|
115
|
-
className: `lucide lucide-circle-alert ${className ? className : ""}`,
|
|
116
|
-
style,
|
|
117
|
-
children: [
|
|
118
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("circle", { cx: "12", cy: "12", r: "10" }),
|
|
119
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("line", { x1: "12", x2: "12", y1: "8", y2: "12" }),
|
|
120
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("line", { x1: "12", x2: "12.01", y1: "16", y2: "16" })
|
|
121
|
-
]
|
|
122
|
-
}
|
|
123
|
-
);
|
|
124
|
-
|
|
125
|
-
// src/components/error-boundary/error-utils.tsx
|
|
126
|
-
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
127
|
-
function ErrorToast({ errors }) {
|
|
128
|
-
const errorsToRender = errors.map((error, idx) => {
|
|
129
|
-
var _a, _b, _c;
|
|
130
|
-
const message = "extensions" in error ? (_b = (_a = error.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message : error.message;
|
|
131
|
-
const code = "extensions" in error ? (_c = error.extensions) == null ? void 0 : _c.code : null;
|
|
132
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
133
|
-
"div",
|
|
134
|
-
{
|
|
135
|
-
style: {
|
|
136
|
-
marginTop: idx === 0 ? 0 : 10,
|
|
137
|
-
marginBottom: 14
|
|
138
|
-
},
|
|
139
|
-
children: [
|
|
140
|
-
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ExclamationMarkIcon, { style: { marginBottom: 4 } }),
|
|
141
|
-
code && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
142
|
-
"div",
|
|
143
|
-
{
|
|
144
|
-
style: {
|
|
145
|
-
fontWeight: "600",
|
|
146
|
-
marginBottom: 4
|
|
147
|
-
},
|
|
148
|
-
children: [
|
|
149
|
-
"Copilot Cloud Error:",
|
|
150
|
-
" ",
|
|
151
|
-
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { style: { fontFamily: "monospace", fontWeight: "normal" }, children: code })
|
|
152
|
-
]
|
|
153
|
-
}
|
|
154
|
-
),
|
|
155
|
-
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { children: message })
|
|
156
|
-
]
|
|
157
|
-
},
|
|
158
|
-
idx
|
|
159
|
-
);
|
|
160
|
-
});
|
|
161
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
162
|
-
"div",
|
|
163
|
-
{
|
|
164
|
-
style: {
|
|
165
|
-
fontSize: "13px",
|
|
166
|
-
maxWidth: "600px"
|
|
167
|
-
},
|
|
168
|
-
children: [
|
|
169
|
-
errorsToRender,
|
|
170
|
-
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { fontSize: "11px", opacity: 0.75 }, children: "NOTE: This error only displays during local development." })
|
|
171
|
-
]
|
|
172
|
-
}
|
|
173
|
-
);
|
|
174
|
-
}
|
|
175
|
-
function useErrorToast() {
|
|
176
|
-
const { addToast } = useToast();
|
|
177
|
-
return (0, import_react.useCallback)(
|
|
178
|
-
(error) => {
|
|
179
|
-
const errorId = error.map((err) => {
|
|
180
|
-
var _a, _b;
|
|
181
|
-
const message = "extensions" in err ? ((_b = (_a = err.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message) || err.message : err.message;
|
|
182
|
-
const stack = err.stack || "";
|
|
183
|
-
return btoa(message + stack).slice(0, 32);
|
|
184
|
-
}).join("|");
|
|
185
|
-
addToast({
|
|
186
|
-
type: "error",
|
|
187
|
-
id: errorId,
|
|
188
|
-
// Toast libraries typically dedupe by id
|
|
189
|
-
message: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ErrorToast, { errors: error })
|
|
190
|
-
});
|
|
191
|
-
},
|
|
192
|
-
[addToast]
|
|
193
|
-
);
|
|
194
|
-
}
|
|
195
|
-
function useAsyncCallback(callback, deps) {
|
|
196
|
-
const addErrorToast = useErrorToast();
|
|
197
|
-
return (0, import_react.useCallback)((...args) => __async(this, null, function* () {
|
|
198
|
-
try {
|
|
199
|
-
return yield callback(...args);
|
|
200
|
-
} catch (error) {
|
|
201
|
-
console.error("Error in async callback:", error);
|
|
202
|
-
addErrorToast([error]);
|
|
203
|
-
throw error;
|
|
204
|
-
}
|
|
205
|
-
}), deps);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// src/components/toast/toast-provider.tsx
|
|
209
|
-
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
210
|
-
var ToastContext = (0, import_react2.createContext)(void 0);
|
|
70
|
+
var ToastContext = (0, import_react.createContext)(void 0);
|
|
211
71
|
function useToast() {
|
|
212
|
-
const context = (0,
|
|
72
|
+
const context = (0, import_react.useContext)(ToastContext);
|
|
213
73
|
if (!context) {
|
|
214
74
|
throw new Error("useToast must be used within a ToastProvider");
|
|
215
75
|
}
|
|
@@ -217,18 +77,15 @@ function useToast() {
|
|
|
217
77
|
}
|
|
218
78
|
|
|
219
79
|
// src/hooks/use-copilot-runtime-client.ts
|
|
220
|
-
var
|
|
221
|
-
var
|
|
80
|
+
var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
|
|
81
|
+
var import_react2 = require("react");
|
|
222
82
|
var useCopilotRuntimeClient = (options) => {
|
|
223
83
|
const { addGraphQLErrorsToast } = useToast();
|
|
224
|
-
const
|
|
225
|
-
|
|
226
|
-
return new import_runtime_client_gql2.CopilotRuntimeClient(__spreadProps(__spreadValues({}, options), {
|
|
84
|
+
const runtimeClient = (0, import_react2.useMemo)(() => {
|
|
85
|
+
return new import_runtime_client_gql.CopilotRuntimeClient(__spreadProps(__spreadValues({}, options), {
|
|
227
86
|
handleGQLErrors: (error) => {
|
|
228
87
|
if (error.graphQLErrors.length) {
|
|
229
88
|
addGraphQLErrorsToast(error.graphQLErrors);
|
|
230
|
-
} else {
|
|
231
|
-
addErrorToast([error]);
|
|
232
89
|
}
|
|
233
90
|
}
|
|
234
91
|
}));
|
|
@@ -249,309 +106,232 @@ function useChat(options) {
|
|
|
249
106
|
actions,
|
|
250
107
|
onFunctionCall,
|
|
251
108
|
onCoAgentStateRender,
|
|
252
|
-
|
|
253
|
-
|
|
109
|
+
setCoagentStates,
|
|
110
|
+
coagentStates,
|
|
254
111
|
agentSession,
|
|
255
|
-
setAgentSession
|
|
256
|
-
threadId,
|
|
257
|
-
setThreadId,
|
|
258
|
-
runId,
|
|
259
|
-
setRunId,
|
|
260
|
-
chatAbortControllerRef
|
|
112
|
+
setAgentSession
|
|
261
113
|
} = options;
|
|
114
|
+
const abortControllerRef = (0, import_react3.useRef)();
|
|
115
|
+
const threadIdRef = (0, import_react3.useRef)(null);
|
|
116
|
+
const runIdRef = (0, import_react3.useRef)(null);
|
|
262
117
|
const { addGraphQLErrorsToast } = useToast();
|
|
263
|
-
const runChatCompletionRef = (0,
|
|
264
|
-
const
|
|
118
|
+
const runChatCompletionRef = (0, import_react3.useRef)();
|
|
119
|
+
const coagentStatesRef = (0, import_react3.useRef)(coagentStates);
|
|
120
|
+
coagentStatesRef.current = coagentStates;
|
|
121
|
+
const agentSessionRef = (0, import_react3.useRef)(agentSession);
|
|
265
122
|
agentSessionRef.current = agentSession;
|
|
266
|
-
const threadIdRef = (0, import_react4.useRef)(threadId);
|
|
267
|
-
threadIdRef.current = threadId;
|
|
268
|
-
const runIdRef = (0, import_react4.useRef)(runId);
|
|
269
|
-
runIdRef.current = runId;
|
|
270
123
|
const publicApiKey = copilotConfig.publicApiKey;
|
|
271
|
-
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [
|
|
124
|
+
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [import_shared.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
|
|
272
125
|
const runtimeClient = useCopilotRuntimeClient({
|
|
273
126
|
url: copilotConfig.chatApiEndpoint,
|
|
274
127
|
publicApiKey: copilotConfig.publicApiKey,
|
|
275
128
|
headers,
|
|
276
129
|
credentials: copilotConfig.credentials
|
|
277
130
|
});
|
|
278
|
-
const runChatCompletion =
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
131
|
+
const runChatCompletion = (previousMessages) => __async(this, null, function* () {
|
|
132
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
133
|
+
setIsLoading(true);
|
|
134
|
+
let newMessages = [
|
|
135
|
+
new import_runtime_client_gql2.TextMessage({
|
|
136
|
+
content: "",
|
|
137
|
+
role: import_runtime_client_gql2.Role.Assistant
|
|
138
|
+
})
|
|
139
|
+
];
|
|
140
|
+
const abortController = new AbortController();
|
|
141
|
+
abortControllerRef.current = abortController;
|
|
142
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
143
|
+
const systemMessage = makeSystemMessageCallback();
|
|
144
|
+
const messagesWithContext = [systemMessage, ...initialMessages || [], ...previousMessages];
|
|
145
|
+
const stream = runtimeClient.asStream(
|
|
146
|
+
runtimeClient.generateCopilotResponse({
|
|
147
|
+
data: __spreadProps(__spreadValues(__spreadProps(__spreadValues({
|
|
148
|
+
frontend: {
|
|
149
|
+
actions: actions.filter(
|
|
150
|
+
(action) => action.available !== import_runtime_client_gql2.ActionInputAvailability.Disabled || !action.disabled
|
|
151
|
+
).map((action) => {
|
|
152
|
+
let available = import_runtime_client_gql2.ActionInputAvailability.Enabled;
|
|
153
|
+
if (action.disabled) {
|
|
154
|
+
available = import_runtime_client_gql2.ActionInputAvailability.Disabled;
|
|
155
|
+
} else if (action.available === "disabled") {
|
|
156
|
+
available = import_runtime_client_gql2.ActionInputAvailability.Disabled;
|
|
157
|
+
} else if (action.available === "remote") {
|
|
158
|
+
available = import_runtime_client_gql2.ActionInputAvailability.Remote;
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
name: action.name,
|
|
162
|
+
description: action.description || "",
|
|
163
|
+
jsonSchema: JSON.stringify((0, import_shared.actionParametersToJsonSchema)(action.parameters || [])),
|
|
164
|
+
available
|
|
165
|
+
};
|
|
166
|
+
}),
|
|
167
|
+
url: window.location.href
|
|
168
|
+
},
|
|
169
|
+
threadId: threadIdRef.current,
|
|
170
|
+
runId: runIdRef.current,
|
|
171
|
+
messages: (0, import_runtime_client_gql2.convertMessagesToGqlInput)((0, import_runtime_client_gql2.filterAgentStateMessages)(messagesWithContext))
|
|
172
|
+
}, copilotConfig.cloud ? {
|
|
173
|
+
cloud: __spreadValues({}, ((_c = (_b = (_a = copilotConfig.cloud.guardrails) == null ? void 0 : _a.input) == null ? void 0 : _b.restrictToTopic) == null ? void 0 : _c.enabled) ? {
|
|
174
|
+
guardrails: {
|
|
175
|
+
inputValidationRules: {
|
|
176
|
+
allowList: copilotConfig.cloud.guardrails.input.restrictToTopic.validTopics,
|
|
177
|
+
denyList: copilotConfig.cloud.guardrails.input.restrictToTopic.invalidTopics
|
|
310
178
|
}
|
|
311
|
-
} : {})
|
|
312
|
-
} : {}), {
|
|
313
|
-
metadata: {
|
|
314
|
-
requestType: import_runtime_client_gql3.CopilotRequestType.Chat
|
|
315
|
-
}
|
|
316
|
-
}), agentSessionRef.current ? {
|
|
317
|
-
agentSession: agentSessionRef.current
|
|
318
|
-
} : {}), {
|
|
319
|
-
agentStates: Object.values(coagentStatesRef.current).map((state) => ({
|
|
320
|
-
agentName: state.name,
|
|
321
|
-
state: JSON.stringify(state.state)
|
|
322
|
-
})),
|
|
323
|
-
forwardedParameters: options.forwardedParameters || {}
|
|
324
|
-
}),
|
|
325
|
-
properties: copilotConfig.properties,
|
|
326
|
-
signal: (_d = chatAbortControllerRef.current) == null ? void 0 : _d.signal
|
|
327
|
-
})
|
|
328
|
-
);
|
|
329
|
-
const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
|
|
330
|
-
const reader = stream.getReader();
|
|
331
|
-
let executedCoAgentStateRenders = [];
|
|
332
|
-
let followUp = void 0;
|
|
333
|
-
let messages2 = [];
|
|
334
|
-
let syncedMessages = [];
|
|
335
|
-
try {
|
|
336
|
-
while (true) {
|
|
337
|
-
let done, value;
|
|
338
|
-
try {
|
|
339
|
-
const readResult = yield reader.read();
|
|
340
|
-
done = readResult.done;
|
|
341
|
-
value = readResult.value;
|
|
342
|
-
} catch (readError) {
|
|
343
|
-
break;
|
|
344
|
-
}
|
|
345
|
-
if (done) {
|
|
346
|
-
if (chatAbortControllerRef.current.signal.aborted) {
|
|
347
|
-
return [];
|
|
348
179
|
}
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
180
|
+
} : {})
|
|
181
|
+
} : {}), {
|
|
182
|
+
metadata: {
|
|
183
|
+
requestType: import_runtime_client_gql2.CopilotRequestType.Chat
|
|
353
184
|
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
)
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
185
|
+
}), agentSessionRef.current ? {
|
|
186
|
+
agentSession: agentSessionRef.current
|
|
187
|
+
} : {}), {
|
|
188
|
+
agentStates: Object.values(coagentStatesRef.current).map((state) => ({
|
|
189
|
+
agentName: state.name,
|
|
190
|
+
state: JSON.stringify(state.state)
|
|
191
|
+
}))
|
|
192
|
+
}),
|
|
193
|
+
properties: copilotConfig.properties,
|
|
194
|
+
signal: (_d = abortControllerRef.current) == null ? void 0 : _d.signal
|
|
195
|
+
})
|
|
196
|
+
);
|
|
197
|
+
const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
|
|
198
|
+
const reader = stream.getReader();
|
|
199
|
+
let actionResults = {};
|
|
200
|
+
let executedCoAgentStateRenders = [];
|
|
201
|
+
let followUp = void 0;
|
|
202
|
+
try {
|
|
203
|
+
while (true) {
|
|
204
|
+
let done, value;
|
|
205
|
+
try {
|
|
206
|
+
const readResult = yield reader.read();
|
|
207
|
+
done = readResult.done;
|
|
208
|
+
value = readResult.value;
|
|
209
|
+
} catch (readError) {
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
if (done) {
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
if (!(value == null ? void 0 : value.generateCopilotResponse)) {
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
threadIdRef.current = value.generateCopilotResponse.threadId || null;
|
|
219
|
+
runIdRef.current = value.generateCopilotResponse.runId || null;
|
|
220
|
+
const messages2 = (0, import_runtime_client_gql2.convertGqlOutputToMessages)(
|
|
221
|
+
(0, import_runtime_client_gql2.filterAdjacentAgentStateMessages)(value.generateCopilotResponse.messages)
|
|
222
|
+
);
|
|
223
|
+
if (messages2.length === 0) {
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
newMessages = [];
|
|
227
|
+
if (((_h = value.generateCopilotResponse.status) == null ? void 0 : _h.__typename) === "FailedResponseStatus" && value.generateCopilotResponse.status.reason === "GUARDRAILS_VALIDATION_FAILED") {
|
|
228
|
+
newMessages = [
|
|
229
|
+
new import_runtime_client_gql2.TextMessage({
|
|
230
|
+
role: import_runtime_client_gql2.MessageRole.Assistant,
|
|
231
|
+
content: ((_i = value.generateCopilotResponse.status.details) == null ? void 0 : _i.guardrailsReason) || ""
|
|
232
|
+
})
|
|
233
|
+
];
|
|
234
|
+
} else {
|
|
235
|
+
for (const message of messages2) {
|
|
236
|
+
newMessages.push(message);
|
|
237
|
+
if (message.isActionExecutionMessage() && message.status.code !== import_runtime_client_gql2.MessageStatusCode.Pending && message.scope === "client" && onFunctionCall) {
|
|
238
|
+
if (!(message.id in actionResults)) {
|
|
378
239
|
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
379
240
|
break;
|
|
380
241
|
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
}
|
|
389
|
-
const lastAgentStateMessage = [...messages2].reverse().find((message) => message.isAgentStateMessage());
|
|
390
|
-
if (lastAgentStateMessage) {
|
|
391
|
-
if (lastAgentStateMessage.state.messages && lastAgentStateMessage.state.messages.length > 0) {
|
|
392
|
-
syncedMessages = (0, import_runtime_client_gql3.loadMessagesFromJsonRepresentation)(
|
|
393
|
-
lastAgentStateMessage.state.messages
|
|
394
|
-
);
|
|
395
|
-
}
|
|
396
|
-
setCoagentStatesWithRef((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
397
|
-
[lastAgentStateMessage.agentName]: {
|
|
398
|
-
name: lastAgentStateMessage.agentName,
|
|
399
|
-
state: lastAgentStateMessage.state,
|
|
400
|
-
running: lastAgentStateMessage.running,
|
|
401
|
-
active: lastAgentStateMessage.active,
|
|
402
|
-
threadId: lastAgentStateMessage.threadId,
|
|
403
|
-
nodeName: lastAgentStateMessage.nodeName,
|
|
404
|
-
runId: lastAgentStateMessage.runId
|
|
405
|
-
}
|
|
406
|
-
}));
|
|
407
|
-
if (lastAgentStateMessage.running) {
|
|
408
|
-
setAgentSession({
|
|
409
|
-
threadId: lastAgentStateMessage.threadId,
|
|
410
|
-
agentName: lastAgentStateMessage.agentName,
|
|
411
|
-
nodeName: lastAgentStateMessage.nodeName
|
|
412
|
-
});
|
|
413
|
-
} else {
|
|
414
|
-
setAgentSession(null);
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
if (newMessages.length > 0) {
|
|
419
|
-
setMessages([...previousMessages, ...newMessages]);
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
const finalMessages = constructFinalMessages(syncedMessages, previousMessages, newMessages);
|
|
423
|
-
let didExecuteAction = false;
|
|
424
|
-
if (onFunctionCall) {
|
|
425
|
-
const lastMessages = [];
|
|
426
|
-
for (let i = finalMessages.length - 1; i >= 0; i--) {
|
|
427
|
-
const message = finalMessages[i];
|
|
428
|
-
if (message.isActionExecutionMessage() && message.status.code !== import_runtime_client_gql3.MessageStatusCode.Pending) {
|
|
429
|
-
lastMessages.unshift(message);
|
|
430
|
-
} else {
|
|
431
|
-
break;
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
for (const message of lastMessages) {
|
|
435
|
-
setMessages(finalMessages);
|
|
436
|
-
const action = actions.find((action2) => action2.name === message.name);
|
|
437
|
-
if (action) {
|
|
438
|
-
followUp = action.followUp;
|
|
439
|
-
let result;
|
|
440
|
-
try {
|
|
441
|
-
result = yield Promise.race([
|
|
442
|
-
onFunctionCall({
|
|
242
|
+
try {
|
|
243
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
244
|
+
const action = actions.find((action2) => action2.name === message.name);
|
|
245
|
+
if (action) {
|
|
246
|
+
followUp = action.followUp;
|
|
247
|
+
}
|
|
248
|
+
const result = yield onFunctionCall({
|
|
443
249
|
messages: previousMessages,
|
|
444
250
|
name: message.name,
|
|
445
251
|
args: message.arguments
|
|
446
|
-
})
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
() => resolve("Operation was aborted by the user")
|
|
453
|
-
);
|
|
454
|
-
}
|
|
455
|
-
),
|
|
456
|
-
// if the user stopped generation, we also abort consecutive actions
|
|
457
|
-
new Promise((resolve) => {
|
|
458
|
-
var _a2;
|
|
459
|
-
if ((_a2 = chatAbortControllerRef.current) == null ? void 0 : _a2.signal.aborted) {
|
|
460
|
-
resolve("Operation was aborted by the user");
|
|
461
|
-
}
|
|
462
|
-
})
|
|
463
|
-
]);
|
|
464
|
-
} catch (e) {
|
|
465
|
-
result = `Failed to execute action ${message.name}`;
|
|
466
|
-
console.error(`Failed to execute action ${message.name}: ${e}`);
|
|
252
|
+
});
|
|
253
|
+
actionResults[message.id] = result;
|
|
254
|
+
} catch (e) {
|
|
255
|
+
actionResults[message.id] = `Failed to execute action ${message.name}`;
|
|
256
|
+
console.error(`Failed to execute action ${message.name}: ${e}`);
|
|
257
|
+
}
|
|
467
258
|
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
messageIndex + 1,
|
|
472
|
-
0,
|
|
473
|
-
new import_runtime_client_gql3.ResultMessage({
|
|
474
|
-
id: "result-" + message.id,
|
|
475
|
-
result: import_runtime_client_gql3.ResultMessage.encodeResult(result),
|
|
259
|
+
newMessages.push(
|
|
260
|
+
new import_runtime_client_gql2.ResultMessage({
|
|
261
|
+
result: import_runtime_client_gql2.ResultMessage.encodeResult(actionResults[message.id]),
|
|
476
262
|
actionExecutionId: message.id,
|
|
477
263
|
actionName: message.name
|
|
478
264
|
})
|
|
479
265
|
);
|
|
480
266
|
}
|
|
267
|
+
if (message.isAgentStateMessage() && !message.active && !executedCoAgentStateRenders.includes(message.id) && onCoAgentStateRender) {
|
|
268
|
+
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
yield onCoAgentStateRender({
|
|
272
|
+
name: message.agentName,
|
|
273
|
+
nodeName: message.nodeName,
|
|
274
|
+
state: message.state
|
|
275
|
+
});
|
|
276
|
+
executedCoAgentStateRenders.push(message.id);
|
|
277
|
+
}
|
|
481
278
|
}
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
if (
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
279
|
+
const lastAgentStateMessage = [...messages2].reverse().find((message) => message.isAgentStateMessage());
|
|
280
|
+
if (lastAgentStateMessage) {
|
|
281
|
+
setCoagentStates((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
282
|
+
[lastAgentStateMessage.agentName]: {
|
|
283
|
+
name: lastAgentStateMessage.agentName,
|
|
284
|
+
state: lastAgentStateMessage.state,
|
|
285
|
+
running: lastAgentStateMessage.running,
|
|
286
|
+
active: lastAgentStateMessage.active,
|
|
287
|
+
threadId: lastAgentStateMessage.threadId,
|
|
288
|
+
nodeName: lastAgentStateMessage.nodeName,
|
|
289
|
+
runId: lastAgentStateMessage.runId
|
|
290
|
+
}
|
|
291
|
+
}));
|
|
292
|
+
if (lastAgentStateMessage.running) {
|
|
293
|
+
setAgentSession({
|
|
294
|
+
threadId: lastAgentStateMessage.threadId,
|
|
295
|
+
agentName: lastAgentStateMessage.agentName,
|
|
296
|
+
nodeName: lastAgentStateMessage.nodeName
|
|
297
|
+
});
|
|
298
|
+
} else {
|
|
299
|
+
setAgentSession(null);
|
|
499
300
|
}
|
|
500
|
-
return true;
|
|
501
|
-
});
|
|
502
|
-
const repairedMessageIds = repairedMessages.map((message) => message.id);
|
|
503
|
-
setMessages(repairedMessages);
|
|
504
|
-
if ((_l = agentSessionRef.current) == null ? void 0 : _l.nodeName) {
|
|
505
|
-
setAgentSession({
|
|
506
|
-
threadId: agentSessionRef.current.threadId,
|
|
507
|
-
agentName: agentSessionRef.current.agentName,
|
|
508
|
-
nodeName: "__end__"
|
|
509
|
-
});
|
|
510
301
|
}
|
|
511
|
-
return newMessages.filter((message) => repairedMessageIds.includes(message.id));
|
|
512
|
-
} else {
|
|
513
|
-
return newMessages.slice();
|
|
514
302
|
}
|
|
515
|
-
|
|
516
|
-
|
|
303
|
+
if (newMessages.length > 0) {
|
|
304
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
305
|
+
}
|
|
517
306
|
}
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
onFunctionCall,
|
|
529
|
-
onCoAgentStateRender,
|
|
530
|
-
setCoagentStatesWithRef,
|
|
531
|
-
coagentStatesRef,
|
|
532
|
-
agentSession,
|
|
533
|
-
setAgentSession
|
|
534
|
-
]
|
|
535
|
-
);
|
|
536
|
-
runChatCompletionRef.current = runChatCompletion;
|
|
537
|
-
const runChatCompletionAndHandleFunctionCall = useAsyncCallback(
|
|
538
|
-
(messages2) => __async(this, null, function* () {
|
|
539
|
-
yield runChatCompletionRef.current(messages2);
|
|
540
|
-
}),
|
|
541
|
-
[messages]
|
|
542
|
-
);
|
|
543
|
-
const append = useAsyncCallback(
|
|
544
|
-
(message) => __async(this, null, function* () {
|
|
545
|
-
if (isLoading) {
|
|
546
|
-
return;
|
|
307
|
+
if (
|
|
308
|
+
// if followUp is not explicitly false
|
|
309
|
+
followUp !== false && // if we have client side results
|
|
310
|
+
(Object.values(actionResults).length || // or the last message we received is a result
|
|
311
|
+
newMessages.length && newMessages[newMessages.length - 1].isResultMessage())
|
|
312
|
+
) {
|
|
313
|
+
yield new Promise((resolve) => setTimeout(resolve, 10));
|
|
314
|
+
return yield runChatCompletionRef.current([...previousMessages, ...newMessages]);
|
|
315
|
+
} else {
|
|
316
|
+
return newMessages.slice();
|
|
547
317
|
}
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
)
|
|
554
|
-
|
|
318
|
+
} finally {
|
|
319
|
+
setIsLoading(false);
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
runChatCompletionRef.current = runChatCompletion;
|
|
323
|
+
const runChatCompletionAndHandleFunctionCall = (messages2) => __async(this, null, function* () {
|
|
324
|
+
yield runChatCompletionRef.current(messages2);
|
|
325
|
+
});
|
|
326
|
+
const append = (message) => __async(this, null, function* () {
|
|
327
|
+
if (isLoading) {
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
const newMessages = [...messages, message];
|
|
331
|
+
setMessages(newMessages);
|
|
332
|
+
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
333
|
+
});
|
|
334
|
+
const reload = () => __async(this, null, function* () {
|
|
555
335
|
if (isLoading || messages.length === 0) {
|
|
556
336
|
return;
|
|
557
337
|
}
|
|
@@ -562,35 +342,17 @@ function useChat(options) {
|
|
|
562
342
|
}
|
|
563
343
|
setMessages(newMessages);
|
|
564
344
|
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
565
|
-
})
|
|
345
|
+
});
|
|
566
346
|
const stop = () => {
|
|
567
347
|
var _a;
|
|
568
|
-
(_a =
|
|
348
|
+
(_a = abortControllerRef.current) == null ? void 0 : _a.abort();
|
|
569
349
|
};
|
|
570
350
|
return {
|
|
571
351
|
append,
|
|
572
352
|
reload,
|
|
573
|
-
stop
|
|
574
|
-
runChatCompletion: () => runChatCompletionRef.current(messages)
|
|
353
|
+
stop
|
|
575
354
|
};
|
|
576
355
|
}
|
|
577
|
-
function constructFinalMessages(syncedMessages, previousMessages, newMessages) {
|
|
578
|
-
const finalMessages = syncedMessages.length > 0 ? [...syncedMessages] : [...previousMessages, ...newMessages];
|
|
579
|
-
if (syncedMessages.length > 0) {
|
|
580
|
-
const messagesWithAgentState = [...previousMessages, ...newMessages];
|
|
581
|
-
let previousMessageId = void 0;
|
|
582
|
-
for (const message of messagesWithAgentState) {
|
|
583
|
-
if (message.isAgentStateMessage()) {
|
|
584
|
-
const index = finalMessages.findIndex((msg) => msg.id === previousMessageId);
|
|
585
|
-
if (index !== -1) {
|
|
586
|
-
finalMessages.splice(index + 1, 0, message);
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
previousMessageId = message.id;
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
return finalMessages;
|
|
593
|
-
}
|
|
594
356
|
// Annotate the CommonJS export names for ESM import in node:
|
|
595
357
|
0 && (module.exports = {
|
|
596
358
|
useChat
|