@copilotkit/react-core 1.5.0-tyler-reset-chat.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +158 -4
- package/README.md +2 -0
- package/dist/{chunk-3AYELZJS.mjs → chunk-35EN6BG4.mjs} +2 -2
- package/dist/{chunk-3AYELZJS.mjs.map → chunk-35EN6BG4.mjs.map} +1 -1
- package/dist/{chunk-SEPYQHH7.mjs → chunk-42N5VKIX.mjs} +34 -28
- package/dist/chunk-42N5VKIX.mjs.map +1 -0
- package/dist/{chunk-USL3EHJB.mjs → chunk-5FYKUKG3.mjs} +2 -2
- package/dist/{chunk-ODN4H66E.mjs → chunk-7LRDVJH5.mjs} +6 -2
- package/dist/chunk-7LRDVJH5.mjs.map +1 -0
- package/dist/{chunk-CZMEZR6F.mjs → chunk-BT6WK2JZ.mjs} +34 -19
- package/dist/chunk-BT6WK2JZ.mjs.map +1 -0
- package/dist/{chunk-3R4J2TPH.mjs → chunk-EUU6NNYU.mjs} +29 -13
- package/dist/chunk-EUU6NNYU.mjs.map +1 -0
- package/dist/chunk-QCUP6HLK.mjs +37 -0
- package/dist/chunk-QCUP6HLK.mjs.map +1 -0
- package/dist/chunk-QTDCEDOC.mjs +392 -0
- package/dist/chunk-QTDCEDOC.mjs.map +1 -0
- package/dist/{chunk-JR55I3FL.mjs → chunk-QX6V774L.mjs} +6 -8
- package/dist/chunk-QX6V774L.mjs.map +1 -0
- package/dist/{chunk-2KCEHGSI.mjs → chunk-SFPANIOY.mjs} +99 -49
- package/dist/chunk-SFPANIOY.mjs.map +1 -0
- package/dist/{chunk-2JP64U3A.mjs → chunk-TQN3EZWQ.mjs} +4 -1
- package/dist/chunk-TQN3EZWQ.mjs.map +1 -0
- package/dist/{chunk-XUPO37VH.mjs → chunk-V3PFWGIY.mjs} +2 -2
- package/dist/{chunk-6QKA3SNN.mjs → chunk-VMP6JWBB.mjs} +21 -5
- package/dist/chunk-VMP6JWBB.mjs.map +1 -0
- package/dist/chunk-XERJQUHA.mjs +31 -0
- package/dist/chunk-XERJQUHA.mjs.map +1 -0
- package/dist/components/copilot-provider/copilotkit.js +173 -92
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +5 -4
- package/dist/components/copilot-provider/index.js +173 -92
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +5 -4
- package/dist/components/error-boundary/error-boundary.d.ts +22 -0
- package/dist/components/error-boundary/error-boundary.js +183 -0
- package/dist/components/error-boundary/error-boundary.js.map +1 -0
- package/dist/components/error-boundary/error-boundary.mjs +12 -0
- package/dist/components/error-boundary/error-boundary.mjs.map +1 -0
- package/dist/components/error-boundary/error-utils.d.ts +11 -0
- package/dist/components/error-boundary/error-utils.js +177 -0
- package/dist/components/error-boundary/error-utils.js.map +1 -0
- package/dist/components/error-boundary/error-utils.mjs +13 -0
- package/dist/components/error-boundary/error-utils.mjs.map +1 -0
- package/dist/components/index.js +173 -92
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +5 -4
- package/dist/components/toast/toast-provider.d.ts +2 -1
- package/dist/components/toast/toast-provider.js +76 -62
- package/dist/components/toast/toast-provider.js.map +1 -1
- package/dist/components/toast/toast-provider.mjs +1 -1
- package/dist/context/copilot-context.d.ts +4 -2
- package/dist/context/copilot-context.js +3 -0
- package/dist/context/copilot-context.js.map +1 -1
- package/dist/context/copilot-context.mjs +1 -1
- package/dist/context/index.d.ts +1 -1
- package/dist/context/index.js +3 -0
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +1 -1
- package/dist/hooks/index.js +554 -308
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +13 -11
- package/dist/hooks/use-chat.d.ts +6 -2
- package/dist/hooks/use-chat.js +434 -219
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +4 -3
- package/dist/hooks/use-coagent-state-render.d.ts +2 -2
- package/dist/hooks/use-coagent-state-render.js +3 -0
- package/dist/hooks/use-coagent-state-render.js.map +1 -1
- package/dist/hooks/use-coagent-state-render.mjs +2 -2
- package/dist/hooks/use-coagent.d.ts +1 -1
- package/dist/hooks/use-coagent.js +510 -277
- package/dist/hooks/use-coagent.js.map +1 -1
- package/dist/hooks/use-coagent.mjs +9 -7
- package/dist/hooks/use-copilot-action.d.ts +12 -2
- package/dist/hooks/use-copilot-action.js +157 -16
- package/dist/hooks/use-copilot-action.js.map +1 -1
- package/dist/hooks/use-copilot-action.mjs +4 -2
- package/dist/hooks/use-copilot-chat.d.ts +1 -0
- package/dist/hooks/use-copilot-chat.js +483 -253
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +8 -6
- package/dist/hooks/use-copilot-readable.js +3 -0
- package/dist/hooks/use-copilot-readable.js.map +1 -1
- package/dist/hooks/use-copilot-readable.mjs +2 -2
- package/dist/hooks/use-copilot-runtime-client.js +110 -4
- package/dist/hooks/use-copilot-runtime-client.js.map +1 -1
- package/dist/hooks/use-copilot-runtime-client.mjs +2 -2
- package/dist/hooks/use-make-copilot-document-readable.js +3 -0
- package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
- package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +616 -401
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +14 -12
- package/dist/lib/copilot-task.d.ts +1 -1
- package/dist/lib/copilot-task.js +33 -13
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +7 -5
- package/dist/lib/index.d.ts +1 -1
- package/dist/lib/index.js +33 -13
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +7 -5
- package/dist/types/frontend-action.d.ts +21 -2
- package/dist/types/frontend-action.js +34 -0
- package/dist/types/frontend-action.js.map +1 -1
- package/dist/types/frontend-action.mjs +7 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.js.map +1 -1
- package/dist/utils/extract.js.map +1 -1
- package/dist/utils/extract.mjs +5 -4
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +5 -4
- package/package.json +5 -5
- package/src/components/copilot-provider/copilotkit.tsx +22 -1
- package/src/components/error-boundary/error-boundary.tsx +42 -0
- package/src/components/error-boundary/error-utils.tsx +95 -0
- package/src/components/toast/toast-provider.tsx +10 -49
- package/src/context/copilot-context.tsx +17 -2
- package/src/hooks/use-chat.ts +375 -279
- package/src/hooks/use-coagent-state-render.ts +2 -2
- package/src/hooks/use-coagent.ts +34 -28
- package/src/hooks/use-copilot-action.ts +50 -15
- package/src/hooks/use-copilot-chat.ts +28 -14
- package/src/hooks/use-copilot-runtime-client.ts +4 -0
- package/src/lib/copilot-task.ts +2 -8
- package/src/types/frontend-action.ts +55 -2
- package/src/types/index.ts +5 -1
- package/dist/chunk-2JP64U3A.mjs.map +0 -1
- package/dist/chunk-2KCEHGSI.mjs.map +0 -1
- package/dist/chunk-3R4J2TPH.mjs.map +0 -1
- package/dist/chunk-6EN7J4V2.mjs +0 -317
- package/dist/chunk-6EN7J4V2.mjs.map +0 -1
- package/dist/chunk-6QKA3SNN.mjs.map +0 -1
- package/dist/chunk-CZMEZR6F.mjs.map +0 -1
- package/dist/chunk-JR55I3FL.mjs.map +0 -1
- package/dist/chunk-ODN4H66E.mjs.map +0 -1
- package/dist/chunk-SEPYQHH7.mjs.map +0 -1
- /package/dist/{chunk-USL3EHJB.mjs.map → chunk-5FYKUKG3.mjs.map} +0 -0
- /package/dist/{chunk-XUPO37VH.mjs.map → chunk-V3PFWGIY.mjs.map} +0 -0
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
|
}));
|
|
@@ -106,8 +249,8 @@ function useChat(options) {
|
|
|
106
249
|
actions,
|
|
107
250
|
onFunctionCall,
|
|
108
251
|
onCoAgentStateRender,
|
|
109
|
-
|
|
110
|
-
|
|
252
|
+
setCoagentStatesWithRef,
|
|
253
|
+
coagentStatesRef,
|
|
111
254
|
agentSession,
|
|
112
255
|
setAgentSession,
|
|
113
256
|
threadId,
|
|
@@ -116,244 +259,298 @@ function useChat(options) {
|
|
|
116
259
|
setRunId,
|
|
117
260
|
chatAbortControllerRef
|
|
118
261
|
} = options;
|
|
119
|
-
const abortController = new AbortController();
|
|
120
|
-
chatAbortControllerRef.current = abortController;
|
|
121
262
|
const { addGraphQLErrorsToast } = useToast();
|
|
122
|
-
const runChatCompletionRef = (0,
|
|
123
|
-
const
|
|
124
|
-
coagentStatesRef.current = coagentStates;
|
|
125
|
-
const agentSessionRef = (0, import_react3.useRef)(agentSession);
|
|
263
|
+
const runChatCompletionRef = (0, import_react4.useRef)();
|
|
264
|
+
const agentSessionRef = (0, import_react4.useRef)(agentSession);
|
|
126
265
|
agentSessionRef.current = agentSession;
|
|
127
|
-
const threadIdRef = (0,
|
|
266
|
+
const threadIdRef = (0, import_react4.useRef)(threadId);
|
|
128
267
|
threadIdRef.current = threadId;
|
|
129
|
-
const runIdRef = (0,
|
|
268
|
+
const runIdRef = (0, import_react4.useRef)(runId);
|
|
130
269
|
runIdRef.current = runId;
|
|
131
270
|
const publicApiKey = copilotConfig.publicApiKey;
|
|
132
|
-
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [
|
|
271
|
+
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [import_shared2.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
|
|
133
272
|
const runtimeClient = useCopilotRuntimeClient({
|
|
134
273
|
url: copilotConfig.chatApiEndpoint,
|
|
135
274
|
publicApiKey: copilotConfig.publicApiKey,
|
|
136
275
|
headers,
|
|
137
276
|
credentials: copilotConfig.credentials
|
|
138
277
|
});
|
|
139
|
-
const runChatCompletion = (
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
available
|
|
172
|
-
};
|
|
173
|
-
}),
|
|
174
|
-
url: window.location.href
|
|
175
|
-
},
|
|
176
|
-
threadId: threadIdRef.current,
|
|
177
|
-
runId: runIdRef.current,
|
|
178
|
-
messages: (0, import_runtime_client_gql2.convertMessagesToGqlInput)((0, import_runtime_client_gql2.filterAgentStateMessages)(messagesWithContext))
|
|
179
|
-
}, copilotConfig.cloud ? {
|
|
180
|
-
cloud: __spreadValues({}, ((_c = (_b = (_a = copilotConfig.cloud.guardrails) == null ? void 0 : _a.input) == null ? void 0 : _b.restrictToTopic) == null ? void 0 : _c.enabled) ? {
|
|
181
|
-
guardrails: {
|
|
182
|
-
inputValidationRules: {
|
|
183
|
-
allowList: copilotConfig.cloud.guardrails.input.restrictToTopic.validTopics,
|
|
184
|
-
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
|
+
}
|
|
185
310
|
}
|
|
311
|
+
} : {})
|
|
312
|
+
} : {}), {
|
|
313
|
+
metadata: {
|
|
314
|
+
requestType: import_runtime_client_gql3.CopilotRequestType.Chat
|
|
186
315
|
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
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;
|
|
191
343
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
state: JSON.stringify(state.state)
|
|
198
|
-
}))
|
|
199
|
-
}),
|
|
200
|
-
properties: copilotConfig.properties,
|
|
201
|
-
signal: (_d = chatAbortControllerRef.current) == null ? void 0 : _d.signal
|
|
202
|
-
})
|
|
203
|
-
);
|
|
204
|
-
const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
|
|
205
|
-
const reader = stream.getReader();
|
|
206
|
-
let actionResults = {};
|
|
207
|
-
let executedCoAgentStateRenders = [];
|
|
208
|
-
let followUp = void 0;
|
|
209
|
-
try {
|
|
210
|
-
while (true) {
|
|
211
|
-
let done, value;
|
|
212
|
-
try {
|
|
213
|
-
const readResult = yield reader.read();
|
|
214
|
-
done = readResult.done;
|
|
215
|
-
value = readResult.value;
|
|
216
|
-
} catch (readError) {
|
|
217
|
-
break;
|
|
218
|
-
}
|
|
219
|
-
if (done) {
|
|
220
|
-
if (chatAbortControllerRef.current.signal.aborted) {
|
|
221
|
-
return newMessages.slice();
|
|
344
|
+
if (done) {
|
|
345
|
+
if (chatAbortControllerRef.current.signal.aborted) {
|
|
346
|
+
return [];
|
|
347
|
+
}
|
|
348
|
+
break;
|
|
222
349
|
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
if (!(message.id in actionResults)) {
|
|
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) {
|
|
251
377
|
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
252
378
|
break;
|
|
253
379
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
console.error(`Failed to execute action ${message.name}: ${e}`);
|
|
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
|
|
279
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({
|
|
442
|
+
messages: previousMessages,
|
|
443
|
+
name: message.name,
|
|
444
|
+
args: message.arguments
|
|
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}`);
|
|
280
466
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
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),
|
|
284
475
|
actionExecutionId: message.id,
|
|
285
476
|
actionName: message.name
|
|
286
477
|
})
|
|
287
478
|
);
|
|
288
479
|
}
|
|
289
|
-
if (message.isAgentStateMessage() && !message.active && !executedCoAgentStateRenders.includes(message.id) && onCoAgentStateRender) {
|
|
290
|
-
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
291
|
-
break;
|
|
292
|
-
}
|
|
293
|
-
yield onCoAgentStateRender({
|
|
294
|
-
name: message.agentName,
|
|
295
|
-
nodeName: message.nodeName,
|
|
296
|
-
state: message.state
|
|
297
|
-
});
|
|
298
|
-
executedCoAgentStateRenders.push(message.id);
|
|
299
|
-
}
|
|
300
480
|
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
if (
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
nodeName: lastAgentStateMessage.nodeName
|
|
319
|
-
});
|
|
320
|
-
} else {
|
|
321
|
-
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
|
+
);
|
|
322
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
|
+
});
|
|
323
509
|
}
|
|
510
|
+
return newMessages.filter((message) => repairedMessageIds.includes(message.id));
|
|
511
|
+
} else {
|
|
512
|
+
return newMessages.slice();
|
|
324
513
|
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
}
|
|
514
|
+
} finally {
|
|
515
|
+
setIsLoading(false);
|
|
328
516
|
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
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
|
+
);
|
|
344
535
|
runChatCompletionRef.current = runChatCompletion;
|
|
345
|
-
const runChatCompletionAndHandleFunctionCall = (
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
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* () {
|
|
357
554
|
if (isLoading || messages.length === 0) {
|
|
358
555
|
return;
|
|
359
556
|
}
|
|
@@ -364,7 +561,7 @@ function useChat(options) {
|
|
|
364
561
|
}
|
|
365
562
|
setMessages(newMessages);
|
|
366
563
|
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
367
|
-
});
|
|
564
|
+
}), [isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]);
|
|
368
565
|
const stop = () => {
|
|
369
566
|
var _a;
|
|
370
567
|
(_a = chatAbortControllerRef.current) == null ? void 0 : _a.abort("Stop was called");
|
|
@@ -372,9 +569,27 @@ function useChat(options) {
|
|
|
372
569
|
return {
|
|
373
570
|
append,
|
|
374
571
|
reload,
|
|
375
|
-
stop
|
|
572
|
+
stop,
|
|
573
|
+
runChatCompletion: () => runChatCompletionRef.current(messages)
|
|
376
574
|
};
|
|
377
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
|
+
}
|
|
378
593
|
// Annotate the CommonJS export names for ESM import in node:
|
|
379
594
|
0 && (module.exports = {
|
|
380
595
|
useChat
|