@copilotkit/react-core 1.4.8-no-pino-redact.1 → 1.5.0-coagents-v0-3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -3
- package/dist/{chunk-JHEAUB3Z.mjs → chunk-35EN6BG4.mjs} +2 -2
- package/dist/{chunk-JHEAUB3Z.mjs.map → chunk-35EN6BG4.mjs.map} +1 -1
- package/dist/{chunk-O22KGHOQ.mjs → chunk-42N5VKIX.mjs} +38 -16
- package/dist/chunk-42N5VKIX.mjs.map +1 -0
- package/dist/{chunk-AG7FH7OD.mjs → chunk-5FYKUKG3.mjs} +2 -2
- package/dist/{chunk-ODN4H66E.mjs → chunk-7LRDVJH5.mjs} +6 -2
- package/dist/chunk-7LRDVJH5.mjs.map +1 -0
- package/dist/{chunk-Y7MI4PBB.mjs → chunk-ALR5W5JK.mjs} +22 -10
- package/dist/chunk-ALR5W5JK.mjs.map +1 -0
- package/dist/{chunk-XBVKTDXP.mjs → chunk-BT6WK2JZ.mjs} +57 -17
- package/dist/chunk-BT6WK2JZ.mjs.map +1 -0
- package/dist/chunk-QCUP6HLK.mjs +37 -0
- package/dist/chunk-QCUP6HLK.mjs.map +1 -0
- package/dist/chunk-QTDCEDOC.mjs +392 -0
- package/dist/chunk-QTDCEDOC.mjs.map +1 -0
- package/dist/{chunk-OT67R4NB.mjs → chunk-QX6V774L.mjs} +6 -8
- package/dist/chunk-QX6V774L.mjs.map +1 -0
- package/dist/{chunk-2KCEHGSI.mjs → chunk-SFPANIOY.mjs} +99 -49
- package/dist/chunk-SFPANIOY.mjs.map +1 -0
- package/dist/{chunk-XQFVXX6R.mjs → chunk-TQN3EZWQ.mjs} +10 -2
- package/dist/chunk-TQN3EZWQ.mjs.map +1 -0
- package/dist/{chunk-UOVONDR6.mjs → chunk-V3PFWGIY.mjs} +2 -2
- package/dist/{chunk-DHGXL5PC.mjs → chunk-VMP6JWBB.mjs} +18 -6
- package/dist/chunk-VMP6JWBB.mjs.map +1 -0
- package/dist/chunk-XERJQUHA.mjs +31 -0
- package/dist/chunk-XERJQUHA.mjs.map +1 -0
- package/dist/components/copilot-provider/copilotkit.js +175 -93
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +5 -4
- package/dist/components/copilot-provider/index.js +175 -93
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +5 -4
- package/dist/components/error-boundary/error-boundary.d.ts +22 -0
- package/dist/components/error-boundary/error-boundary.js +183 -0
- package/dist/components/error-boundary/error-boundary.js.map +1 -0
- package/dist/components/error-boundary/error-boundary.mjs +12 -0
- package/dist/components/error-boundary/error-boundary.mjs.map +1 -0
- package/dist/components/error-boundary/error-utils.d.ts +11 -0
- package/dist/components/error-boundary/error-utils.js +177 -0
- package/dist/components/error-boundary/error-utils.js.map +1 -0
- package/dist/components/error-boundary/error-utils.mjs +13 -0
- package/dist/components/error-boundary/error-utils.mjs.map +1 -0
- package/dist/components/index.js +175 -93
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +5 -4
- package/dist/components/toast/toast-provider.d.ts +2 -1
- package/dist/components/toast/toast-provider.js +76 -62
- package/dist/components/toast/toast-provider.js.map +1 -1
- package/dist/components/toast/toast-provider.mjs +1 -1
- package/dist/context/copilot-context.d.ts +8 -2
- package/dist/context/copilot-context.js +9 -1
- 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 +9 -1
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +1 -1
- package/dist/hooks/index.d.ts +2 -1
- package/dist/hooks/index.js +584 -270
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +19 -11
- package/dist/hooks/use-chat.d.ts +20 -0
- package/dist/hooks/use-chat.js +438 -200
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +4 -3
- package/dist/hooks/use-coagent-state-render.d.ts +2 -2
- package/dist/hooks/use-coagent-state-render.js +9 -1
- 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 +14 -1
- package/dist/hooks/use-coagent.js +547 -242
- package/dist/hooks/use-coagent.js.map +1 -1
- package/dist/hooks/use-coagent.mjs +15 -7
- package/dist/hooks/use-copilot-action.d.ts +12 -2
- package/dist/hooks/use-copilot-action.js +156 -14
- package/dist/hooks/use-copilot-action.js.map +1 -1
- package/dist/hooks/use-copilot-action.mjs +4 -2
- package/dist/hooks/use-copilot-chat.d.ts +1 -0
- package/dist/hooks/use-copilot-chat.js +514 -231
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +8 -6
- package/dist/hooks/use-copilot-readable.js +9 -1
- package/dist/hooks/use-copilot-readable.js.map +1 -1
- package/dist/hooks/use-copilot-readable.mjs +2 -2
- package/dist/hooks/use-copilot-runtime-client.js +110 -4
- package/dist/hooks/use-copilot-runtime-client.js.map +1 -1
- package/dist/hooks/use-copilot-runtime-client.mjs +2 -2
- package/dist/hooks/use-make-copilot-document-readable.js +9 -1
- 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 +2 -2
- package/dist/index.js +642 -363
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +20 -12
- package/dist/lib/copilot-task.d.ts +1 -1
- package/dist/lib/copilot-task.js +33 -13
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +7 -5
- package/dist/lib/index.d.ts +1 -1
- package/dist/lib/index.js +33 -13
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +7 -5
- package/dist/types/frontend-action.d.ts +21 -2
- package/dist/types/frontend-action.js +34 -0
- package/dist/types/frontend-action.js.map +1 -1
- package/dist/types/frontend-action.mjs +7 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.js.map +1 -1
- package/dist/utils/extract.js.map +1 -1
- package/dist/utils/extract.mjs +5 -4
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +5 -4
- package/package.json +3 -3
- package/src/components/copilot-provider/copilotkit.tsx +14 -1
- package/src/components/error-boundary/error-boundary.tsx +42 -0
- package/src/components/error-boundary/error-utils.tsx +95 -0
- package/src/components/toast/toast-provider.tsx +10 -49
- package/src/context/copilot-context.tsx +30 -2
- package/src/hooks/index.ts +1 -1
- package/src/hooks/use-chat.ts +391 -256
- package/src/hooks/use-coagent-state-render.ts +2 -2
- package/src/hooks/use-coagent.ts +35 -15
- package/src/hooks/use-copilot-action.ts +41 -12
- package/src/hooks/use-copilot-chat.ts +52 -11
- package/src/hooks/use-copilot-runtime-client.ts +4 -0
- package/src/lib/copilot-task.ts +2 -8
- package/src/types/frontend-action.ts +55 -2
- package/src/types/index.ts +5 -1
- package/dist/chunk-2KCEHGSI.mjs.map +0 -1
- package/dist/chunk-DHGXL5PC.mjs.map +0 -1
- package/dist/chunk-EUIBVFV6.mjs +0 -294
- package/dist/chunk-EUIBVFV6.mjs.map +0 -1
- package/dist/chunk-O22KGHOQ.mjs.map +0 -1
- package/dist/chunk-ODN4H66E.mjs.map +0 -1
- package/dist/chunk-OT67R4NB.mjs.map +0 -1
- package/dist/chunk-XBVKTDXP.mjs.map +0 -1
- package/dist/chunk-XQFVXX6R.mjs.map +0 -1
- package/dist/chunk-Y7MI4PBB.mjs.map +0 -1
- /package/dist/{chunk-AG7FH7OD.mjs.map → chunk-5FYKUKG3.mjs.map} +0 -0
- /package/dist/{chunk-UOVONDR6.mjs.map → chunk-V3PFWGIY.mjs.map} +0 -0
package/dist/hooks/use-chat.js
CHANGED
|
@@ -60,16 +60,156 @@ __export(use_chat_exports, {
|
|
|
60
60
|
useChat: () => useChat
|
|
61
61
|
});
|
|
62
62
|
module.exports = __toCommonJS(use_chat_exports);
|
|
63
|
-
var
|
|
63
|
+
var import_react4 = require("react");
|
|
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");
|
|
64
69
|
var import_shared = require("@copilotkit/shared");
|
|
65
|
-
|
|
70
|
+
function processActionsForRuntimeRequest(actions) {
|
|
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
|
+
}
|
|
66
91
|
|
|
67
92
|
// src/components/toast/toast-provider.tsx
|
|
93
|
+
var import_react2 = require("react");
|
|
94
|
+
|
|
95
|
+
// src/components/error-boundary/error-utils.tsx
|
|
68
96
|
var import_react = require("react");
|
|
97
|
+
|
|
98
|
+
// src/components/toast/exclamation-mark-icon.tsx
|
|
69
99
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
70
|
-
var
|
|
100
|
+
var ExclamationMarkIcon = ({
|
|
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);
|
|
71
211
|
function useToast() {
|
|
72
|
-
const context = (0,
|
|
212
|
+
const context = (0, import_react2.useContext)(ToastContext);
|
|
73
213
|
if (!context) {
|
|
74
214
|
throw new Error("useToast must be used within a ToastProvider");
|
|
75
215
|
}
|
|
@@ -77,15 +217,18 @@ function useToast() {
|
|
|
77
217
|
}
|
|
78
218
|
|
|
79
219
|
// src/hooks/use-copilot-runtime-client.ts
|
|
80
|
-
var
|
|
81
|
-
var
|
|
220
|
+
var import_runtime_client_gql2 = require("@copilotkit/runtime-client-gql");
|
|
221
|
+
var import_react3 = require("react");
|
|
82
222
|
var useCopilotRuntimeClient = (options) => {
|
|
83
223
|
const { addGraphQLErrorsToast } = useToast();
|
|
84
|
-
const
|
|
85
|
-
|
|
224
|
+
const addErrorToast = useErrorToast();
|
|
225
|
+
const runtimeClient = (0, import_react3.useMemo)(() => {
|
|
226
|
+
return new import_runtime_client_gql2.CopilotRuntimeClient(__spreadProps(__spreadValues({}, options), {
|
|
86
227
|
handleGQLErrors: (error) => {
|
|
87
228
|
if (error.graphQLErrors.length) {
|
|
88
229
|
addGraphQLErrorsToast(error.graphQLErrors);
|
|
230
|
+
} else {
|
|
231
|
+
addErrorToast([error]);
|
|
89
232
|
}
|
|
90
233
|
}
|
|
91
234
|
}));
|
|
@@ -109,227 +252,305 @@ function useChat(options) {
|
|
|
109
252
|
setCoagentStatesWithRef,
|
|
110
253
|
coagentStatesRef,
|
|
111
254
|
agentSession,
|
|
112
|
-
setAgentSession
|
|
255
|
+
setAgentSession,
|
|
256
|
+
threadId,
|
|
257
|
+
setThreadId,
|
|
258
|
+
runId,
|
|
259
|
+
setRunId,
|
|
260
|
+
chatAbortControllerRef
|
|
113
261
|
} = options;
|
|
114
|
-
const abortControllerRef = (0, import_react3.useRef)();
|
|
115
|
-
const threadIdRef = (0, import_react3.useRef)(null);
|
|
116
|
-
const runIdRef = (0, import_react3.useRef)(null);
|
|
117
262
|
const { addGraphQLErrorsToast } = useToast();
|
|
118
|
-
const runChatCompletionRef = (0,
|
|
119
|
-
const agentSessionRef = (0,
|
|
263
|
+
const runChatCompletionRef = (0, import_react4.useRef)();
|
|
264
|
+
const agentSessionRef = (0, import_react4.useRef)(agentSession);
|
|
120
265
|
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;
|
|
121
270
|
const publicApiKey = copilotConfig.publicApiKey;
|
|
122
|
-
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [
|
|
271
|
+
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [import_shared2.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
|
|
123
272
|
const runtimeClient = useCopilotRuntimeClient({
|
|
124
273
|
url: copilotConfig.chatApiEndpoint,
|
|
125
274
|
publicApiKey: copilotConfig.publicApiKey,
|
|
126
275
|
headers,
|
|
127
276
|
credentials: copilotConfig.credentials
|
|
128
277
|
});
|
|
129
|
-
const runChatCompletion = (
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
runtimeClient.
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
(
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
jsonSchema: JSON.stringify((0, import_shared.actionParametersToJsonSchema)(action.parameters || [])),
|
|
162
|
-
available
|
|
163
|
-
};
|
|
164
|
-
}),
|
|
165
|
-
url: window.location.href
|
|
166
|
-
},
|
|
167
|
-
threadId: threadIdRef.current,
|
|
168
|
-
runId: runIdRef.current,
|
|
169
|
-
messages: (0, import_runtime_client_gql2.convertMessagesToGqlInput)((0, import_runtime_client_gql2.filterAgentStateMessages)(messagesWithContext))
|
|
170
|
-
}, copilotConfig.cloud ? {
|
|
171
|
-
cloud: __spreadValues({}, ((_c = (_b = (_a = copilotConfig.cloud.guardrails) == null ? void 0 : _a.input) == null ? void 0 : _b.restrictToTopic) == null ? void 0 : _c.enabled) ? {
|
|
172
|
-
guardrails: {
|
|
173
|
-
inputValidationRules: {
|
|
174
|
-
allowList: copilotConfig.cloud.guardrails.input.restrictToTopic.validTopics,
|
|
175
|
-
denyList: copilotConfig.cloud.guardrails.input.restrictToTopic.invalidTopics
|
|
278
|
+
const runChatCompletion = useAsyncCallback(
|
|
279
|
+
(previousMessages) => __async(this, null, function* () {
|
|
280
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
281
|
+
setIsLoading(true);
|
|
282
|
+
let newMessages = [
|
|
283
|
+
new import_runtime_client_gql3.TextMessage({
|
|
284
|
+
content: "",
|
|
285
|
+
role: import_runtime_client_gql3.Role.Assistant
|
|
286
|
+
})
|
|
287
|
+
];
|
|
288
|
+
chatAbortControllerRef.current = new AbortController();
|
|
289
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
290
|
+
const systemMessage = makeSystemMessageCallback();
|
|
291
|
+
const messagesWithContext = [systemMessage, ...initialMessages || [], ...previousMessages];
|
|
292
|
+
const isAgentRun = agentSessionRef.current !== null;
|
|
293
|
+
const stream = runtimeClient.asStream(
|
|
294
|
+
runtimeClient.generateCopilotResponse({
|
|
295
|
+
data: __spreadProps(__spreadValues(__spreadProps(__spreadValues({
|
|
296
|
+
frontend: {
|
|
297
|
+
actions: processActionsForRuntimeRequest(actions),
|
|
298
|
+
url: window.location.href
|
|
299
|
+
},
|
|
300
|
+
threadId: threadIdRef.current,
|
|
301
|
+
runId: runIdRef.current,
|
|
302
|
+
messages: (0, import_runtime_client_gql3.convertMessagesToGqlInput)((0, import_runtime_client_gql3.filterAgentStateMessages)(messagesWithContext))
|
|
303
|
+
}, copilotConfig.cloud ? {
|
|
304
|
+
cloud: __spreadValues({}, ((_c = (_b = (_a = copilotConfig.cloud.guardrails) == null ? void 0 : _a.input) == null ? void 0 : _b.restrictToTopic) == null ? void 0 : _c.enabled) ? {
|
|
305
|
+
guardrails: {
|
|
306
|
+
inputValidationRules: {
|
|
307
|
+
allowList: copilotConfig.cloud.guardrails.input.restrictToTopic.validTopics,
|
|
308
|
+
denyList: copilotConfig.cloud.guardrails.input.restrictToTopic.invalidTopics
|
|
309
|
+
}
|
|
176
310
|
}
|
|
311
|
+
} : {})
|
|
312
|
+
} : {}), {
|
|
313
|
+
metadata: {
|
|
314
|
+
requestType: import_runtime_client_gql3.CopilotRequestType.Chat
|
|
177
315
|
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
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
|
+
}),
|
|
324
|
+
properties: copilotConfig.properties,
|
|
325
|
+
signal: (_d = chatAbortControllerRef.current) == null ? void 0 : _d.signal
|
|
326
|
+
})
|
|
327
|
+
);
|
|
328
|
+
const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
|
|
329
|
+
const reader = stream.getReader();
|
|
330
|
+
let executedCoAgentStateRenders = [];
|
|
331
|
+
let followUp = void 0;
|
|
332
|
+
let messages2 = [];
|
|
333
|
+
let syncedMessages = [];
|
|
334
|
+
try {
|
|
335
|
+
while (true) {
|
|
336
|
+
let done, value;
|
|
337
|
+
try {
|
|
338
|
+
const readResult = yield reader.read();
|
|
339
|
+
done = readResult.done;
|
|
340
|
+
value = readResult.value;
|
|
341
|
+
} catch (readError) {
|
|
342
|
+
break;
|
|
182
343
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
threadIdRef.current = value.generateCopilotResponse.threadId || null;
|
|
217
|
-
runIdRef.current = value.generateCopilotResponse.runId || null;
|
|
218
|
-
const messages2 = (0, import_runtime_client_gql2.convertGqlOutputToMessages)(
|
|
219
|
-
(0, import_runtime_client_gql2.filterAdjacentAgentStateMessages)(value.generateCopilotResponse.messages)
|
|
220
|
-
);
|
|
221
|
-
if (messages2.length === 0) {
|
|
222
|
-
continue;
|
|
223
|
-
}
|
|
224
|
-
newMessages = [];
|
|
225
|
-
if (((_h = value.generateCopilotResponse.status) == null ? void 0 : _h.__typename) === "FailedResponseStatus" && value.generateCopilotResponse.status.reason === "GUARDRAILS_VALIDATION_FAILED") {
|
|
226
|
-
newMessages = [
|
|
227
|
-
new import_runtime_client_gql2.TextMessage({
|
|
228
|
-
role: import_runtime_client_gql2.MessageRole.Assistant,
|
|
229
|
-
content: ((_i = value.generateCopilotResponse.status.details) == null ? void 0 : _i.guardrailsReason) || ""
|
|
230
|
-
})
|
|
231
|
-
];
|
|
232
|
-
} else {
|
|
233
|
-
for (const message of messages2) {
|
|
234
|
-
newMessages.push(message);
|
|
235
|
-
if (message.isActionExecutionMessage() && message.status.code !== import_runtime_client_gql2.MessageStatusCode.Pending && message.scope === "client" && onFunctionCall) {
|
|
236
|
-
if (!(message.id in actionResults)) {
|
|
344
|
+
if (done) {
|
|
345
|
+
if (chatAbortControllerRef.current.signal.aborted) {
|
|
346
|
+
return [];
|
|
347
|
+
}
|
|
348
|
+
break;
|
|
349
|
+
}
|
|
350
|
+
if (!(value == null ? void 0 : value.generateCopilotResponse)) {
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
threadIdRef.current = value.generateCopilotResponse.threadId || null;
|
|
354
|
+
runIdRef.current = value.generateCopilotResponse.runId || null;
|
|
355
|
+
setThreadId(threadIdRef.current);
|
|
356
|
+
setRunId(runIdRef.current);
|
|
357
|
+
messages2 = (0, import_runtime_client_gql3.convertGqlOutputToMessages)(
|
|
358
|
+
(0, import_runtime_client_gql3.filterAdjacentAgentStateMessages)(value.generateCopilotResponse.messages)
|
|
359
|
+
);
|
|
360
|
+
if (messages2.length === 0) {
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
newMessages = [];
|
|
364
|
+
if (((_h = value.generateCopilotResponse.status) == null ? void 0 : _h.__typename) === "FailedResponseStatus" && value.generateCopilotResponse.status.reason === "GUARDRAILS_VALIDATION_FAILED") {
|
|
365
|
+
newMessages = [
|
|
366
|
+
new import_runtime_client_gql3.TextMessage({
|
|
367
|
+
role: import_runtime_client_gql3.MessageRole.Assistant,
|
|
368
|
+
content: ((_i = value.generateCopilotResponse.status.details) == null ? void 0 : _i.guardrailsReason) || ""
|
|
369
|
+
})
|
|
370
|
+
];
|
|
371
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
372
|
+
break;
|
|
373
|
+
} else {
|
|
374
|
+
newMessages = [...messages2];
|
|
375
|
+
for (const message of messages2) {
|
|
376
|
+
if (message.isAgentStateMessage() && !message.active && !executedCoAgentStateRenders.includes(message.id) && onCoAgentStateRender) {
|
|
237
377
|
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
238
378
|
break;
|
|
239
379
|
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
380
|
+
yield onCoAgentStateRender({
|
|
381
|
+
name: message.agentName,
|
|
382
|
+
nodeName: message.nodeName,
|
|
383
|
+
state: message.state
|
|
384
|
+
});
|
|
385
|
+
executedCoAgentStateRenders.push(message.id);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
const lastAgentStateMessage = [...messages2].reverse().find((message) => message.isAgentStateMessage());
|
|
389
|
+
if (lastAgentStateMessage) {
|
|
390
|
+
if (lastAgentStateMessage.state.messages && lastAgentStateMessage.state.messages.length > 0) {
|
|
391
|
+
syncedMessages = (0, import_runtime_client_gql3.loadMessagesFromJsonRepresentation)(
|
|
392
|
+
lastAgentStateMessage.state.messages
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
setCoagentStatesWithRef((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
396
|
+
[lastAgentStateMessage.agentName]: {
|
|
397
|
+
name: lastAgentStateMessage.agentName,
|
|
398
|
+
state: lastAgentStateMessage.state,
|
|
399
|
+
running: lastAgentStateMessage.running,
|
|
400
|
+
active: lastAgentStateMessage.active,
|
|
401
|
+
threadId: lastAgentStateMessage.threadId,
|
|
402
|
+
nodeName: lastAgentStateMessage.nodeName,
|
|
403
|
+
runId: lastAgentStateMessage.runId
|
|
404
|
+
}
|
|
405
|
+
}));
|
|
406
|
+
if (lastAgentStateMessage.running) {
|
|
407
|
+
setAgentSession({
|
|
408
|
+
threadId: lastAgentStateMessage.threadId,
|
|
409
|
+
agentName: lastAgentStateMessage.agentName,
|
|
410
|
+
nodeName: lastAgentStateMessage.nodeName
|
|
411
|
+
});
|
|
412
|
+
} else {
|
|
413
|
+
setAgentSession(null);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
if (newMessages.length > 0) {
|
|
418
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
const finalMessages = constructFinalMessages(syncedMessages, previousMessages, newMessages);
|
|
422
|
+
let didExecuteAction = false;
|
|
423
|
+
if (onFunctionCall) {
|
|
424
|
+
const lastMessages = [];
|
|
425
|
+
for (let i = finalMessages.length - 1; i >= 0; i--) {
|
|
426
|
+
const message = finalMessages[i];
|
|
427
|
+
if (message.isActionExecutionMessage() && message.status.code !== import_runtime_client_gql3.MessageStatusCode.Pending) {
|
|
428
|
+
lastMessages.unshift(message);
|
|
429
|
+
} else {
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
for (const message of lastMessages) {
|
|
434
|
+
setMessages(finalMessages);
|
|
435
|
+
const action = actions.find((action2) => action2.name === message.name);
|
|
436
|
+
if (action) {
|
|
437
|
+
followUp = action.followUp;
|
|
438
|
+
let result;
|
|
439
|
+
try {
|
|
440
|
+
result = yield Promise.race([
|
|
441
|
+
onFunctionCall({
|
|
247
442
|
messages: previousMessages,
|
|
248
443
|
name: message.name,
|
|
249
444
|
args: message.arguments
|
|
250
|
-
})
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
445
|
+
}),
|
|
446
|
+
new Promise(
|
|
447
|
+
(resolve) => {
|
|
448
|
+
var _a2;
|
|
449
|
+
return (_a2 = chatAbortControllerRef.current) == null ? void 0 : _a2.signal.addEventListener(
|
|
450
|
+
"abort",
|
|
451
|
+
() => resolve("Operation was aborted by the user")
|
|
452
|
+
);
|
|
453
|
+
}
|
|
454
|
+
),
|
|
455
|
+
// if the user stopped generation, we also abort consecutive actions
|
|
456
|
+
new Promise((resolve) => {
|
|
457
|
+
var _a2;
|
|
458
|
+
if ((_a2 = chatAbortControllerRef.current) == null ? void 0 : _a2.signal.aborted) {
|
|
459
|
+
resolve("Operation was aborted by the user");
|
|
460
|
+
}
|
|
461
|
+
})
|
|
462
|
+
]);
|
|
463
|
+
} catch (e) {
|
|
464
|
+
result = `Failed to execute action ${message.name}`;
|
|
465
|
+
console.error(`Failed to execute action ${message.name}: ${e}`);
|
|
256
466
|
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
467
|
+
didExecuteAction = true;
|
|
468
|
+
const messageIndex = finalMessages.findIndex((msg) => msg.id === message.id);
|
|
469
|
+
finalMessages.splice(
|
|
470
|
+
messageIndex + 1,
|
|
471
|
+
0,
|
|
472
|
+
new import_runtime_client_gql3.ResultMessage({
|
|
473
|
+
id: "result-" + message.id,
|
|
474
|
+
result: import_runtime_client_gql3.ResultMessage.encodeResult(result),
|
|
260
475
|
actionExecutionId: message.id,
|
|
261
476
|
actionName: message.name
|
|
262
477
|
})
|
|
263
478
|
);
|
|
264
479
|
}
|
|
265
|
-
if (message.isAgentStateMessage() && !message.active && !executedCoAgentStateRenders.includes(message.id) && onCoAgentStateRender) {
|
|
266
|
-
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
267
|
-
break;
|
|
268
|
-
}
|
|
269
|
-
yield onCoAgentStateRender({
|
|
270
|
-
name: message.agentName,
|
|
271
|
-
nodeName: message.nodeName,
|
|
272
|
-
state: message.state
|
|
273
|
-
});
|
|
274
|
-
executedCoAgentStateRenders.push(message.id);
|
|
275
|
-
}
|
|
276
480
|
}
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
if (
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
nodeName: lastAgentStateMessage.nodeName
|
|
295
|
-
});
|
|
296
|
-
} else {
|
|
297
|
-
setAgentSession(null);
|
|
481
|
+
setMessages(finalMessages);
|
|
482
|
+
}
|
|
483
|
+
if (
|
|
484
|
+
// if followUp is not explicitly false
|
|
485
|
+
followUp !== false && // and we executed an action
|
|
486
|
+
(didExecuteAction || // the last message is a server side result
|
|
487
|
+
!isAgentRun && finalMessages.length && finalMessages[finalMessages.length - 1].isResultMessage()) && // the user did not stop generation
|
|
488
|
+
!((_j = chatAbortControllerRef.current) == null ? void 0 : _j.signal.aborted)
|
|
489
|
+
) {
|
|
490
|
+
yield new Promise((resolve) => setTimeout(resolve, 10));
|
|
491
|
+
return yield runChatCompletionRef.current(finalMessages);
|
|
492
|
+
} else if ((_k = chatAbortControllerRef.current) == null ? void 0 : _k.signal.aborted) {
|
|
493
|
+
const repairedMessages = finalMessages.filter((message, actionExecutionIndex) => {
|
|
494
|
+
if (message.isActionExecutionMessage()) {
|
|
495
|
+
return finalMessages.find(
|
|
496
|
+
(msg, resultIndex) => msg.isResultMessage() && msg.actionExecutionId === message.id && resultIndex === actionExecutionIndex + 1
|
|
497
|
+
);
|
|
298
498
|
}
|
|
499
|
+
return true;
|
|
500
|
+
});
|
|
501
|
+
const repairedMessageIds = repairedMessages.map((message) => message.id);
|
|
502
|
+
setMessages(repairedMessages);
|
|
503
|
+
if ((_l = agentSessionRef.current) == null ? void 0 : _l.nodeName) {
|
|
504
|
+
setAgentSession({
|
|
505
|
+
threadId: agentSessionRef.current.threadId,
|
|
506
|
+
agentName: agentSessionRef.current.agentName,
|
|
507
|
+
nodeName: "__end__"
|
|
508
|
+
});
|
|
299
509
|
}
|
|
510
|
+
return newMessages.filter((message) => repairedMessageIds.includes(message.id));
|
|
511
|
+
} else {
|
|
512
|
+
return newMessages.slice();
|
|
300
513
|
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
if (
|
|
306
|
-
// if followUp is not explicitly false
|
|
307
|
-
followUp !== false && // if we have client side results
|
|
308
|
-
(Object.values(actionResults).length || // or the last message we received is a result
|
|
309
|
-
newMessages.length && newMessages[newMessages.length - 1].isResultMessage())
|
|
310
|
-
) {
|
|
311
|
-
yield new Promise((resolve) => setTimeout(resolve, 10));
|
|
312
|
-
return yield runChatCompletionRef.current([...previousMessages, ...newMessages]);
|
|
313
|
-
} else {
|
|
314
|
-
return newMessages.slice();
|
|
514
|
+
} finally {
|
|
515
|
+
setIsLoading(false);
|
|
315
516
|
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
517
|
+
}),
|
|
518
|
+
[
|
|
519
|
+
messages,
|
|
520
|
+
setMessages,
|
|
521
|
+
makeSystemMessageCallback,
|
|
522
|
+
copilotConfig,
|
|
523
|
+
setIsLoading,
|
|
524
|
+
initialMessages,
|
|
525
|
+
isLoading,
|
|
526
|
+
actions,
|
|
527
|
+
onFunctionCall,
|
|
528
|
+
onCoAgentStateRender,
|
|
529
|
+
setCoagentStatesWithRef,
|
|
530
|
+
coagentStatesRef,
|
|
531
|
+
agentSession,
|
|
532
|
+
setAgentSession
|
|
533
|
+
]
|
|
534
|
+
);
|
|
320
535
|
runChatCompletionRef.current = runChatCompletion;
|
|
321
|
-
const runChatCompletionAndHandleFunctionCall = (
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
536
|
+
const runChatCompletionAndHandleFunctionCall = useAsyncCallback(
|
|
537
|
+
(messages2) => __async(this, null, function* () {
|
|
538
|
+
yield runChatCompletionRef.current(messages2);
|
|
539
|
+
}),
|
|
540
|
+
[messages]
|
|
541
|
+
);
|
|
542
|
+
const append = useAsyncCallback(
|
|
543
|
+
(message) => __async(this, null, function* () {
|
|
544
|
+
if (isLoading) {
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
const newMessages = [...messages, message];
|
|
548
|
+
setMessages(newMessages);
|
|
549
|
+
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
550
|
+
}),
|
|
551
|
+
[isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]
|
|
552
|
+
);
|
|
553
|
+
const reload = useAsyncCallback(() => __async(this, null, function* () {
|
|
333
554
|
if (isLoading || messages.length === 0) {
|
|
334
555
|
return;
|
|
335
556
|
}
|
|
@@ -340,10 +561,10 @@ function useChat(options) {
|
|
|
340
561
|
}
|
|
341
562
|
setMessages(newMessages);
|
|
342
563
|
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
343
|
-
});
|
|
564
|
+
}), [isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]);
|
|
344
565
|
const stop = () => {
|
|
345
566
|
var _a;
|
|
346
|
-
(_a =
|
|
567
|
+
(_a = chatAbortControllerRef.current) == null ? void 0 : _a.abort("Stop was called");
|
|
347
568
|
};
|
|
348
569
|
return {
|
|
349
570
|
append,
|
|
@@ -352,6 +573,23 @@ function useChat(options) {
|
|
|
352
573
|
runChatCompletion: () => runChatCompletionRef.current(messages)
|
|
353
574
|
};
|
|
354
575
|
}
|
|
576
|
+
function constructFinalMessages(syncedMessages, previousMessages, newMessages) {
|
|
577
|
+
const finalMessages = syncedMessages.length > 0 ? [...syncedMessages] : [...previousMessages, ...newMessages];
|
|
578
|
+
if (syncedMessages.length > 0) {
|
|
579
|
+
const messagesWithAgentState = [...previousMessages, ...newMessages];
|
|
580
|
+
let previousMessageId = void 0;
|
|
581
|
+
for (const message of messagesWithAgentState) {
|
|
582
|
+
if (message.isAgentStateMessage()) {
|
|
583
|
+
const index = finalMessages.findIndex((msg) => msg.id === previousMessageId);
|
|
584
|
+
if (index !== -1) {
|
|
585
|
+
finalMessages.splice(index + 1, 0, message);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
previousMessageId = message.id;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
return finalMessages;
|
|
592
|
+
}
|
|
355
593
|
// Annotate the CommonJS export names for ESM import in node:
|
|
356
594
|
0 && (module.exports = {
|
|
357
595
|
useChat
|