@copilotkit/react-core 1.4.8-next.2 → 1.4.8
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 +8 -13
- package/dist/{chunk-DIX6ISOB.mjs → chunk-6EMLM6WX.mjs} +20 -19
- package/dist/chunk-6EMLM6WX.mjs.map +1 -0
- package/dist/{chunk-ODN4H66E.mjs → chunk-7LRDVJH5.mjs} +6 -2
- package/dist/chunk-7LRDVJH5.mjs.map +1 -0
- package/dist/{chunk-XXCJFOLY.mjs → chunk-AG7FH7OD.mjs} +2 -2
- package/dist/{chunk-XYFTGCMS.mjs → chunk-FSC4A3JN.mjs} +18 -14
- package/dist/chunk-FSC4A3JN.mjs.map +1 -0
- package/dist/{chunk-E3HHCHMQ.mjs → chunk-IFTHM7LF.mjs} +7 -10
- package/dist/chunk-IFTHM7LF.mjs.map +1 -0
- package/dist/{chunk-V6CEKRZA.mjs → chunk-IVYL7JRC.mjs} +12 -11
- package/dist/chunk-IVYL7JRC.mjs.map +1 -0
- package/dist/chunk-NTLCOVE5.mjs +321 -0
- package/dist/chunk-NTLCOVE5.mjs.map +1 -0
- package/dist/chunk-QCUP6HLK.mjs +37 -0
- package/dist/chunk-QCUP6HLK.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-CBMCK7UC.mjs → chunk-UOVONDR6.mjs} +2 -2
- package/dist/{chunk-XSORW54K.mjs → chunk-X6ZF5WAX.mjs} +2 -2
- package/dist/{chunk-4P56GAFP.mjs → chunk-XQFVXX6R.mjs} +2 -3
- package/dist/chunk-XQFVXX6R.mjs.map +1 -0
- package/dist/{chunk-ETCKRNXQ.mjs → chunk-YUY5ZAST.mjs} +7 -4
- package/dist/chunk-YUY5ZAST.mjs.map +1 -0
- 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 +158 -95
- 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.d.ts +0 -1
- package/dist/components/copilot-provider/index.js +158 -95
- 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.d.ts +0 -1
- package/dist/components/index.js +158 -95
- 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 +1 -5
- package/dist/context/copilot-context.js +1 -2
- package/dist/context/copilot-context.js.map +1 -1
- package/dist/context/copilot-context.mjs +1 -1
- package/dist/context/index.js +1 -2
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +1 -1
- package/dist/hooks/index.js +401 -260
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +19 -18
- package/dist/hooks/use-chat.d.ts +1 -2
- package/dist/hooks/use-chat.js +343 -200
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +3 -3
- package/dist/hooks/use-coagent-state-render.js +1 -2
- 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.js +381 -240
- package/dist/hooks/use-coagent.js.map +1 -1
- package/dist/hooks/use-coagent.mjs +9 -8
- package/dist/hooks/use-copilot-action.js +133 -9
- 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.js +370 -230
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +8 -7
- package/dist/hooks/use-copilot-readable.js +1 -2
- 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 +1 -2
- 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 +450 -353
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +20 -19
- package/dist/lib/copilot-task.d.ts +1 -6
- package/dist/lib/copilot-task.js +4 -22
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +6 -5
- package/dist/lib/index.d.ts +1 -1
- package/dist/lib/index.js +4 -22
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +6 -5
- 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 +5 -4
- package/dist/utils/index.js +3 -21
- 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-props.tsx +0 -6
- package/src/components/copilot-provider/copilotkit.tsx +4 -2
- 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 +1 -8
- package/src/hooks/use-chat.ts +286 -266
- package/src/hooks/use-coagent.ts +14 -11
- package/src/hooks/use-copilot-action.ts +3 -2
- package/src/hooks/use-copilot-chat.ts +9 -10
- package/src/hooks/use-copilot-runtime-client.ts +4 -0
- package/src/lib/copilot-task.ts +2 -10
- package/src/utils/extract.ts +0 -4
- package/dist/chunk-2KCEHGSI.mjs.map +0 -1
- package/dist/chunk-4P56GAFP.mjs.map +0 -1
- package/dist/chunk-DIX6ISOB.mjs.map +0 -1
- package/dist/chunk-E3HHCHMQ.mjs.map +0 -1
- package/dist/chunk-ETCKRNXQ.mjs.map +0 -1
- package/dist/chunk-ODN4H66E.mjs.map +0 -1
- package/dist/chunk-PG5XEJVS.mjs +0 -295
- package/dist/chunk-PG5XEJVS.mjs.map +0 -1
- package/dist/chunk-V6CEKRZA.mjs.map +0 -1
- package/dist/chunk-XYFTGCMS.mjs.map +0 -1
- /package/dist/{chunk-XXCJFOLY.mjs.map → chunk-AG7FH7OD.mjs.map} +0 -0
- /package/dist/{chunk-CBMCK7UC.mjs.map → chunk-UOVONDR6.mjs.map} +0 -0
- /package/dist/{chunk-XSORW54K.mjs.map → chunk-X6ZF5WAX.mjs.map} +0 -0
package/src/hooks/use-chat.ts
CHANGED
|
@@ -18,7 +18,6 @@ import {
|
|
|
18
18
|
Role,
|
|
19
19
|
CopilotRequestType,
|
|
20
20
|
ActionInputAvailability,
|
|
21
|
-
ForwardedParametersInput,
|
|
22
21
|
} from "@copilotkit/runtime-client-gql";
|
|
23
22
|
|
|
24
23
|
import { CopilotApiConfig } from "../context";
|
|
@@ -27,6 +26,7 @@ import { CoagentState } from "../types/coagent-state";
|
|
|
27
26
|
import { AgentSession } from "../context/copilot-context";
|
|
28
27
|
import { useToast } from "../components/toast/toast-provider";
|
|
29
28
|
import { useCopilotRuntimeClient } from "./use-copilot-runtime-client";
|
|
29
|
+
import { useAsyncCallback } from "../components/error-boundary/error-utils";
|
|
30
30
|
|
|
31
31
|
export type UseChatOptions = {
|
|
32
32
|
/**
|
|
@@ -98,11 +98,6 @@ export type UseChatOptions = {
|
|
|
98
98
|
* setState-powered method to update the agent session
|
|
99
99
|
*/
|
|
100
100
|
setAgentSession: React.Dispatch<React.SetStateAction<AgentSession | null>>;
|
|
101
|
-
|
|
102
|
-
/*
|
|
103
|
-
* This is forwarded to LLM as a parameter.
|
|
104
|
-
*/
|
|
105
|
-
forwardedParameters?: Pick<ForwardedParametersInput, "temperature">;
|
|
106
101
|
};
|
|
107
102
|
|
|
108
103
|
export type UseChatHelpers = {
|
|
@@ -173,293 +168,318 @@ export function useChat(options: UseChatOptions): UseChatHelpers {
|
|
|
173
168
|
credentials: copilotConfig.credentials,
|
|
174
169
|
});
|
|
175
170
|
|
|
176
|
-
const runChatCompletion =
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
runtimeClient.
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
(
|
|
203
|
-
action
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
ActionInputAvailability
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
available
|
|
212
|
-
|
|
213
|
-
available
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
171
|
+
const runChatCompletion = useAsyncCallback(
|
|
172
|
+
async (previousMessages: Message[]): Promise<Message[]> => {
|
|
173
|
+
setIsLoading(true);
|
|
174
|
+
|
|
175
|
+
// this message is just a placeholder. It will disappear once the first real message
|
|
176
|
+
// is received
|
|
177
|
+
let newMessages: Message[] = [
|
|
178
|
+
new TextMessage({
|
|
179
|
+
content: "",
|
|
180
|
+
role: Role.Assistant,
|
|
181
|
+
}),
|
|
182
|
+
];
|
|
183
|
+
const abortController = new AbortController();
|
|
184
|
+
abortControllerRef.current = abortController;
|
|
185
|
+
|
|
186
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
187
|
+
|
|
188
|
+
const systemMessage = makeSystemMessageCallback();
|
|
189
|
+
|
|
190
|
+
const messagesWithContext = [systemMessage, ...(initialMessages || []), ...previousMessages];
|
|
191
|
+
|
|
192
|
+
const stream = runtimeClient.asStream(
|
|
193
|
+
runtimeClient.generateCopilotResponse({
|
|
194
|
+
data: {
|
|
195
|
+
frontend: {
|
|
196
|
+
actions: actions
|
|
197
|
+
.filter(
|
|
198
|
+
(action) =>
|
|
199
|
+
action.available !== ActionInputAvailability.Disabled || !action.disabled,
|
|
200
|
+
)
|
|
201
|
+
.map((action) => {
|
|
202
|
+
let available: ActionInputAvailability | undefined =
|
|
203
|
+
ActionInputAvailability.Enabled;
|
|
204
|
+
if (action.disabled) {
|
|
205
|
+
available = ActionInputAvailability.Disabled;
|
|
206
|
+
} else if (action.available === "disabled") {
|
|
207
|
+
available = ActionInputAvailability.Disabled;
|
|
208
|
+
} else if (action.available === "remote") {
|
|
209
|
+
available = ActionInputAvailability.Remote;
|
|
210
|
+
}
|
|
211
|
+
return {
|
|
212
|
+
name: action.name,
|
|
213
|
+
description: action.description || "",
|
|
214
|
+
jsonSchema: JSON.stringify(
|
|
215
|
+
actionParametersToJsonSchema(action.parameters || []),
|
|
216
|
+
),
|
|
217
|
+
available,
|
|
218
|
+
};
|
|
219
|
+
}),
|
|
220
|
+
url: window.location.href,
|
|
221
|
+
},
|
|
222
|
+
threadId: threadIdRef.current,
|
|
223
|
+
runId: runIdRef.current,
|
|
224
|
+
messages: convertMessagesToGqlInput(filterAgentStateMessages(messagesWithContext)),
|
|
225
|
+
...(copilotConfig.cloud
|
|
226
|
+
? {
|
|
227
|
+
cloud: {
|
|
228
|
+
...(copilotConfig.cloud.guardrails?.input?.restrictToTopic?.enabled
|
|
229
|
+
? {
|
|
230
|
+
guardrails: {
|
|
231
|
+
inputValidationRules: {
|
|
232
|
+
allowList:
|
|
233
|
+
copilotConfig.cloud.guardrails.input.restrictToTopic.validTopics,
|
|
234
|
+
denyList:
|
|
235
|
+
copilotConfig.cloud.guardrails.input.restrictToTopic.invalidTopics,
|
|
236
|
+
},
|
|
238
237
|
},
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
: {
|
|
245
|
-
|
|
246
|
-
|
|
238
|
+
}
|
|
239
|
+
: {}),
|
|
240
|
+
},
|
|
241
|
+
}
|
|
242
|
+
: {}),
|
|
243
|
+
metadata: {
|
|
244
|
+
requestType: CopilotRequestType.Chat,
|
|
245
|
+
},
|
|
246
|
+
...(agentSessionRef.current
|
|
247
|
+
? {
|
|
248
|
+
agentSession: agentSessionRef.current,
|
|
249
|
+
}
|
|
250
|
+
: {}),
|
|
251
|
+
agentStates: Object.values(coagentStatesRef.current!).map((state) => ({
|
|
252
|
+
agentName: state.name,
|
|
253
|
+
state: JSON.stringify(state.state),
|
|
254
|
+
})),
|
|
247
255
|
},
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
try {
|
|
274
|
-
while (true) {
|
|
275
|
-
let done, value;
|
|
276
|
-
|
|
277
|
-
try {
|
|
278
|
-
const readResult = await reader.read();
|
|
279
|
-
done = readResult.done;
|
|
280
|
-
value = readResult.value;
|
|
281
|
-
} catch (readError) {
|
|
282
|
-
break;
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
if (done) {
|
|
286
|
-
break;
|
|
287
|
-
}
|
|
256
|
+
properties: copilotConfig.properties,
|
|
257
|
+
signal: abortControllerRef.current?.signal,
|
|
258
|
+
}),
|
|
259
|
+
);
|
|
260
|
+
|
|
261
|
+
const guardrailsEnabled =
|
|
262
|
+
copilotConfig.cloud?.guardrails?.input?.restrictToTopic.enabled || false;
|
|
263
|
+
|
|
264
|
+
const reader = stream.getReader();
|
|
265
|
+
|
|
266
|
+
let actionResults: { [id: string]: string } = {};
|
|
267
|
+
let executedCoAgentStateRenders: string[] = [];
|
|
268
|
+
let followUp: FrontendAction["followUp"] = undefined;
|
|
269
|
+
|
|
270
|
+
try {
|
|
271
|
+
while (true) {
|
|
272
|
+
let done, value;
|
|
273
|
+
|
|
274
|
+
try {
|
|
275
|
+
const readResult = await reader.read();
|
|
276
|
+
done = readResult.done;
|
|
277
|
+
value = readResult.value;
|
|
278
|
+
} catch (readError) {
|
|
279
|
+
break;
|
|
280
|
+
}
|
|
288
281
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
282
|
+
if (done) {
|
|
283
|
+
break;
|
|
284
|
+
}
|
|
292
285
|
|
|
293
|
-
|
|
294
|
-
|
|
286
|
+
if (!value?.generateCopilotResponse) {
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
295
289
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
);
|
|
290
|
+
threadIdRef.current = value.generateCopilotResponse.threadId || null;
|
|
291
|
+
runIdRef.current = value.generateCopilotResponse.runId || null;
|
|
299
292
|
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
293
|
+
const messages = convertGqlOutputToMessages(
|
|
294
|
+
filterAdjacentAgentStateMessages(value.generateCopilotResponse.messages),
|
|
295
|
+
);
|
|
303
296
|
|
|
304
|
-
|
|
297
|
+
if (messages.length === 0) {
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
305
300
|
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
301
|
+
newMessages = [];
|
|
302
|
+
|
|
303
|
+
// request failed, display error message
|
|
304
|
+
if (
|
|
305
|
+
value.generateCopilotResponse.status?.__typename === "FailedResponseStatus" &&
|
|
306
|
+
value.generateCopilotResponse.status.reason === "GUARDRAILS_VALIDATION_FAILED"
|
|
307
|
+
) {
|
|
308
|
+
newMessages = [
|
|
309
|
+
new TextMessage({
|
|
310
|
+
role: MessageRole.Assistant,
|
|
311
|
+
content: value.generateCopilotResponse.status.details?.guardrailsReason || "",
|
|
312
|
+
}),
|
|
313
|
+
];
|
|
314
|
+
}
|
|
318
315
|
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
316
|
+
// add messages to the chat
|
|
317
|
+
else {
|
|
318
|
+
for (const message of messages) {
|
|
319
|
+
newMessages.push(message);
|
|
320
|
+
// execute regular action executions
|
|
321
|
+
if (
|
|
322
|
+
message.isActionExecutionMessage() &&
|
|
323
|
+
message.status.code !== MessageStatusCode.Pending &&
|
|
324
|
+
message.scope === "client" &&
|
|
325
|
+
onFunctionCall
|
|
326
|
+
) {
|
|
327
|
+
if (!(message.id in actionResults)) {
|
|
328
|
+
// Do not execute a function call if guardrails are enabled but the status is not known
|
|
329
|
+
if (guardrailsEnabled && value.generateCopilotResponse.status === undefined) {
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
// execute action
|
|
333
|
+
try {
|
|
334
|
+
// We update the message state before calling the handler so that the render
|
|
335
|
+
// function can be called with `executing` state
|
|
336
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
337
|
+
|
|
338
|
+
const action = actions.find((action) => action.name === message.name);
|
|
339
|
+
|
|
340
|
+
if (action) {
|
|
341
|
+
followUp = action.followUp;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const result = await onFunctionCall({
|
|
345
|
+
messages: previousMessages,
|
|
346
|
+
name: message.name,
|
|
347
|
+
args: message.arguments,
|
|
348
|
+
});
|
|
349
|
+
actionResults[message.id] = result;
|
|
350
|
+
} catch (e) {
|
|
351
|
+
actionResults[message.id] = `Failed to execute action ${message.name}`;
|
|
352
|
+
console.error(`Failed to execute action ${message.name}: ${e}`);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
// add the result message
|
|
356
|
+
newMessages.push(
|
|
357
|
+
new ResultMessage({
|
|
358
|
+
result: ResultMessage.encodeResult(actionResults[message.id]),
|
|
359
|
+
actionExecutionId: message.id,
|
|
360
|
+
actionName: message.name,
|
|
361
|
+
}),
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
// execute coagent actions
|
|
365
|
+
if (
|
|
366
|
+
message.isAgentStateMessage() &&
|
|
367
|
+
!message.active &&
|
|
368
|
+
!executedCoAgentStateRenders.includes(message.id) &&
|
|
369
|
+
onCoAgentStateRender
|
|
370
|
+
) {
|
|
371
|
+
// Do not execute a coagent action if guardrails are enabled but the status is not known
|
|
332
372
|
if (guardrailsEnabled && value.generateCopilotResponse.status === undefined) {
|
|
333
373
|
break;
|
|
334
374
|
}
|
|
335
|
-
// execute action
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
if (action) {
|
|
344
|
-
followUp = action.followUp;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
const result = await onFunctionCall({
|
|
348
|
-
messages: previousMessages,
|
|
349
|
-
name: message.name,
|
|
350
|
-
args: message.arguments,
|
|
351
|
-
});
|
|
352
|
-
actionResults[message.id] = result;
|
|
353
|
-
} catch (e) {
|
|
354
|
-
actionResults[message.id] = `Failed to execute action ${message.name}`;
|
|
355
|
-
console.error(`Failed to execute action ${message.name}: ${e}`);
|
|
356
|
-
}
|
|
375
|
+
// execute coagent action
|
|
376
|
+
await onCoAgentStateRender({
|
|
377
|
+
name: message.agentName,
|
|
378
|
+
nodeName: message.nodeName,
|
|
379
|
+
state: message.state,
|
|
380
|
+
});
|
|
381
|
+
executedCoAgentStateRenders.push(message.id);
|
|
357
382
|
}
|
|
358
|
-
// add the result message
|
|
359
|
-
newMessages.push(
|
|
360
|
-
new ResultMessage({
|
|
361
|
-
result: ResultMessage.encodeResult(actionResults[message.id]),
|
|
362
|
-
actionExecutionId: message.id,
|
|
363
|
-
actionName: message.name,
|
|
364
|
-
}),
|
|
365
|
-
);
|
|
366
383
|
}
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
384
|
+
|
|
385
|
+
const lastAgentStateMessage = [...messages]
|
|
386
|
+
.reverse()
|
|
387
|
+
.find((message) => message.isAgentStateMessage());
|
|
388
|
+
|
|
389
|
+
if (lastAgentStateMessage) {
|
|
390
|
+
setCoagentStatesWithRef((prevAgentStates) => ({
|
|
391
|
+
...prevAgentStates,
|
|
392
|
+
[lastAgentStateMessage.agentName]: {
|
|
393
|
+
name: lastAgentStateMessage.agentName,
|
|
394
|
+
state: lastAgentStateMessage.state,
|
|
395
|
+
running: lastAgentStateMessage.running,
|
|
396
|
+
active: lastAgentStateMessage.active,
|
|
397
|
+
threadId: lastAgentStateMessage.threadId,
|
|
398
|
+
nodeName: lastAgentStateMessage.nodeName,
|
|
399
|
+
runId: lastAgentStateMessage.runId,
|
|
400
|
+
},
|
|
401
|
+
}));
|
|
402
|
+
if (lastAgentStateMessage.running) {
|
|
403
|
+
setAgentSession({
|
|
404
|
+
threadId: lastAgentStateMessage.threadId,
|
|
405
|
+
agentName: lastAgentStateMessage.agentName,
|
|
406
|
+
nodeName: lastAgentStateMessage.nodeName,
|
|
407
|
+
});
|
|
408
|
+
} else {
|
|
409
|
+
setAgentSession(null);
|
|
377
410
|
}
|
|
378
|
-
// execute coagent action
|
|
379
|
-
await onCoAgentStateRender({
|
|
380
|
-
name: message.agentName,
|
|
381
|
-
nodeName: message.nodeName,
|
|
382
|
-
state: message.state,
|
|
383
|
-
});
|
|
384
|
-
executedCoAgentStateRenders.push(message.id);
|
|
385
411
|
}
|
|
386
412
|
}
|
|
387
413
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
if (lastAgentStateMessage) {
|
|
393
|
-
setCoagentStatesWithRef((prevAgentStates) => ({
|
|
394
|
-
...prevAgentStates,
|
|
395
|
-
[lastAgentStateMessage.agentName]: {
|
|
396
|
-
name: lastAgentStateMessage.agentName,
|
|
397
|
-
state: lastAgentStateMessage.state,
|
|
398
|
-
running: lastAgentStateMessage.running,
|
|
399
|
-
active: lastAgentStateMessage.active,
|
|
400
|
-
threadId: lastAgentStateMessage.threadId,
|
|
401
|
-
nodeName: lastAgentStateMessage.nodeName,
|
|
402
|
-
runId: lastAgentStateMessage.runId,
|
|
403
|
-
},
|
|
404
|
-
}));
|
|
405
|
-
if (lastAgentStateMessage.running) {
|
|
406
|
-
setAgentSession({
|
|
407
|
-
threadId: lastAgentStateMessage.threadId,
|
|
408
|
-
agentName: lastAgentStateMessage.agentName,
|
|
409
|
-
nodeName: lastAgentStateMessage.nodeName,
|
|
410
|
-
});
|
|
411
|
-
} else {
|
|
412
|
-
setAgentSession(null);
|
|
413
|
-
}
|
|
414
|
+
if (newMessages.length > 0) {
|
|
415
|
+
// Update message state
|
|
416
|
+
setMessages([...previousMessages, ...newMessages]);
|
|
414
417
|
}
|
|
415
418
|
}
|
|
416
419
|
|
|
417
|
-
if (
|
|
418
|
-
//
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
420
|
+
if (
|
|
421
|
+
// if followUp is not explicitly false
|
|
422
|
+
followUp !== false &&
|
|
423
|
+
// if we have client side results
|
|
424
|
+
(Object.values(actionResults).length ||
|
|
425
|
+
// or the last message we received is a result
|
|
426
|
+
(newMessages.length && newMessages[newMessages.length - 1].isResultMessage()))
|
|
427
|
+
) {
|
|
428
|
+
// run the completion again and return the result
|
|
429
|
+
|
|
430
|
+
// wait for next tick to make sure all the react state updates
|
|
431
|
+
// - tried using react-dom's flushSync, but it did not work
|
|
432
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
422
433
|
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
(newMessages.length && newMessages[newMessages.length - 1].isResultMessage()))
|
|
430
|
-
) {
|
|
431
|
-
// run the completion again and return the result
|
|
432
|
-
|
|
433
|
-
// wait for next tick to make sure all the react state updates
|
|
434
|
-
// - tried using react-dom's flushSync, but it did not work
|
|
435
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
436
|
-
|
|
437
|
-
return await runChatCompletionRef.current!([...previousMessages, ...newMessages]);
|
|
438
|
-
} else {
|
|
439
|
-
return newMessages.slice();
|
|
434
|
+
return await runChatCompletionRef.current!([...previousMessages, ...newMessages]);
|
|
435
|
+
} else {
|
|
436
|
+
return newMessages.slice();
|
|
437
|
+
}
|
|
438
|
+
} finally {
|
|
439
|
+
setIsLoading(false);
|
|
440
440
|
}
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
441
|
+
},
|
|
442
|
+
[
|
|
443
|
+
messages,
|
|
444
|
+
setMessages,
|
|
445
|
+
makeSystemMessageCallback,
|
|
446
|
+
copilotConfig,
|
|
447
|
+
setIsLoading,
|
|
448
|
+
initialMessages,
|
|
449
|
+
isLoading,
|
|
450
|
+
actions,
|
|
451
|
+
onFunctionCall,
|
|
452
|
+
onCoAgentStateRender,
|
|
453
|
+
setCoagentStatesWithRef,
|
|
454
|
+
coagentStatesRef,
|
|
455
|
+
agentSession,
|
|
456
|
+
setAgentSession,
|
|
457
|
+
],
|
|
458
|
+
);
|
|
445
459
|
|
|
446
460
|
runChatCompletionRef.current = runChatCompletion;
|
|
447
461
|
|
|
448
|
-
const runChatCompletionAndHandleFunctionCall =
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
462
|
+
const runChatCompletionAndHandleFunctionCall = useAsyncCallback(
|
|
463
|
+
async (messages: Message[]): Promise<void> => {
|
|
464
|
+
await runChatCompletionRef.current!(messages);
|
|
465
|
+
},
|
|
466
|
+
[messages],
|
|
467
|
+
);
|
|
468
|
+
|
|
469
|
+
const append = useAsyncCallback(
|
|
470
|
+
async (message: Message): Promise<void> => {
|
|
471
|
+
if (isLoading) {
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
456
474
|
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
475
|
+
const newMessages = [...messages, message];
|
|
476
|
+
setMessages(newMessages);
|
|
477
|
+
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
478
|
+
},
|
|
479
|
+
[isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall],
|
|
480
|
+
);
|
|
461
481
|
|
|
462
|
-
const reload = async (): Promise<void> => {
|
|
482
|
+
const reload = useAsyncCallback(async (): Promise<void> => {
|
|
463
483
|
if (isLoading || messages.length === 0) {
|
|
464
484
|
return;
|
|
465
485
|
}
|
|
@@ -473,7 +493,7 @@ export function useChat(options: UseChatOptions): UseChatHelpers {
|
|
|
473
493
|
setMessages(newMessages);
|
|
474
494
|
|
|
475
495
|
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
476
|
-
};
|
|
496
|
+
}, [isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]);
|
|
477
497
|
|
|
478
498
|
const stop = (): void => {
|
|
479
499
|
abortControllerRef.current?.abort();
|
package/src/hooks/use-coagent.ts
CHANGED
|
@@ -99,6 +99,7 @@ import { CoagentState } from "../types/coagent-state";
|
|
|
99
99
|
import { useCopilotChat } from "./use-copilot-chat";
|
|
100
100
|
import { Message } from "@copilotkit/runtime-client-gql";
|
|
101
101
|
import { flushSync } from "react-dom";
|
|
102
|
+
import { useAsyncCallback } from "../components/error-boundary/error-utils";
|
|
102
103
|
|
|
103
104
|
interface WithInternalStateManagementAndInitial<T> {
|
|
104
105
|
/**
|
|
@@ -266,22 +267,24 @@ export function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentRe
|
|
|
266
267
|
}
|
|
267
268
|
}, [isExternalStateManagement(options) ? JSON.stringify(options.state) : undefined]);
|
|
268
269
|
|
|
270
|
+
const runAgentCallback = useAsyncCallback(
|
|
271
|
+
async (hint?: HintFunction) => {
|
|
272
|
+
await runAgent(name, context, appendMessage, runChatCompletion, hint);
|
|
273
|
+
},
|
|
274
|
+
[name, context, appendMessage, runChatCompletion],
|
|
275
|
+
);
|
|
276
|
+
|
|
269
277
|
// Return the state and setState function
|
|
270
278
|
return {
|
|
271
279
|
name,
|
|
272
280
|
nodeName: coagentState.nodeName,
|
|
273
|
-
|
|
274
|
-
setState,
|
|
281
|
+
threadId: coagentState.threadId,
|
|
275
282
|
running: coagentState.running,
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
stop: () =>
|
|
280
|
-
|
|
281
|
-
},
|
|
282
|
-
run: (hint?: HintFunction) => {
|
|
283
|
-
return runAgent(name, context, appendMessage, runChatCompletion, hint);
|
|
284
|
-
},
|
|
283
|
+
state: coagentState.state,
|
|
284
|
+
setState: isExternalStateManagement(options) ? options.setState : setState,
|
|
285
|
+
start: () => startAgent(name, context),
|
|
286
|
+
stop: () => stopAgent(name, context),
|
|
287
|
+
run: runAgentCallback,
|
|
285
288
|
};
|
|
286
289
|
}
|
|
287
290
|
|