@copilotkit/react-core 1.1.2 → 1.1.3-feat-runtime-remote-actions.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 +15 -0
- package/README.md +32 -37
- package/dist/{chunk-T6L4EGTD.mjs → chunk-326LK7LX.mjs} +5 -5
- package/dist/chunk-326LK7LX.mjs.map +1 -0
- package/dist/{chunk-VFDR4MIF.mjs → chunk-A37GANOW.mjs} +11 -7
- package/dist/chunk-A37GANOW.mjs.map +1 -0
- package/dist/chunk-BWYAGPEF.mjs +1 -0
- package/dist/{chunk-GE7LAL6V.mjs → chunk-CKOTAKUD.mjs} +4 -3
- package/dist/{chunk-GE7LAL6V.mjs.map → chunk-CKOTAKUD.mjs.map} +1 -1
- package/dist/{chunk-KONDJRB4.mjs → chunk-CODXG6KU.mjs} +5 -5
- package/dist/chunk-CODXG6KU.mjs.map +1 -0
- package/dist/{chunk-3NHEWJ4V.mjs → chunk-J2YXDQHR.mjs} +19 -4
- package/dist/chunk-J2YXDQHR.mjs.map +1 -0
- package/dist/{chunk-UQRDVZ2N.mjs → chunk-OBBH5SHN.mjs} +4 -3
- package/dist/{chunk-UQRDVZ2N.mjs.map → chunk-OBBH5SHN.mjs.map} +1 -1
- package/dist/chunk-Q4TTTAXQ.mjs +87 -0
- package/dist/chunk-Q4TTTAXQ.mjs.map +1 -0
- package/dist/{chunk-TVR5CJ6E.mjs → chunk-RBNULK3U.mjs} +39 -5
- package/dist/chunk-RBNULK3U.mjs.map +1 -0
- package/dist/{chunk-AGGKI26A.mjs → chunk-RK27MXAR.mjs} +76 -11
- package/dist/chunk-RK27MXAR.mjs.map +1 -0
- package/dist/chunk-STUXJ3BN.mjs +44 -0
- package/dist/chunk-STUXJ3BN.mjs.map +1 -0
- package/dist/{chunk-YYZNR3CS.mjs → chunk-XBJINVNA.mjs} +72 -14
- package/dist/chunk-XBJINVNA.mjs.map +1 -0
- package/dist/components/copilot-provider/copilotkit-props.d.ts +4 -0
- package/dist/components/copilot-provider/copilotkit-props.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.d.ts +2 -1
- package/dist/components/copilot-provider/copilotkit.js +50 -5
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +2 -2
- package/dist/components/copilot-provider/index.d.ts +1 -0
- package/dist/components/copilot-provider/index.js +50 -5
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +2 -2
- package/dist/components/index.d.ts +1 -0
- package/dist/components/index.js +50 -5
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +2 -2
- package/dist/context/copilot-context.d.ts +21 -2
- package/dist/context/copilot-context.js +18 -3
- package/dist/context/copilot-context.js.map +1 -1
- package/dist/context/copilot-context.mjs +1 -1
- package/dist/context/index.d.ts +2 -0
- package/dist/context/index.js +18 -3
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +1 -1
- package/dist/hooks/index.d.ts +4 -0
- package/dist/hooks/index.js +288 -30
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +21 -12
- package/dist/hooks/use-chat.d.ts +26 -4
- package/dist/hooks/use-chat.js +73 -9
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +1 -1
- package/dist/hooks/use-coagent-action.d.ts +5 -0
- package/dist/hooks/use-coagent-action.js +156 -0
- package/dist/hooks/use-coagent-action.js.map +1 -0
- package/dist/hooks/use-coagent-action.mjs +9 -0
- package/dist/hooks/use-coagent-action.mjs.map +1 -0
- package/dist/hooks/use-coagent.d.ts +27 -0
- package/dist/hooks/use-coagent.js +219 -0
- package/dist/hooks/use-coagent.js.map +1 -0
- package/dist/hooks/use-coagent.mjs +10 -0
- package/dist/hooks/use-coagent.mjs.map +1 -0
- package/dist/hooks/use-copilot-action.js +27 -5
- package/dist/hooks/use-copilot-action.js.map +1 -1
- package/dist/hooks/use-copilot-action.mjs +2 -2
- package/dist/hooks/use-copilot-chat.js +158 -19
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +4 -4
- package/dist/hooks/use-copilot-readable.js +21 -3
- package/dist/hooks/use-copilot-readable.js.map +1 -1
- package/dist/hooks/use-copilot-readable.mjs +2 -2
- package/dist/hooks/use-make-copilot-document-readable.js +21 -3
- 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 +5 -0
- package/dist/index.js +332 -41
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +24 -16
- package/dist/lib/copilot-task.d.ts +2 -0
- package/dist/lib/copilot-task.js +2 -1
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +3 -3
- package/dist/lib/index.d.ts +2 -0
- package/dist/lib/index.js +2 -1
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +3 -3
- package/dist/types/coagent-action.d.ts +17 -0
- package/dist/types/coagent-action.js +19 -0
- package/dist/types/coagent-action.js.map +1 -0
- package/dist/types/coagent-action.mjs +1 -0
- package/dist/types/coagent-action.mjs.map +1 -0
- package/dist/types/coagent-state.d.ts +11 -0
- package/dist/types/coagent-state.js +19 -0
- package/dist/types/coagent-state.js.map +1 -0
- package/dist/types/coagent-state.mjs +1 -0
- package/dist/types/coagent-state.mjs.map +1 -0
- package/dist/types/frontend-action.d.ts +1 -0
- package/dist/types/frontend-action.js.map +1 -1
- package/dist/utils/extract.d.ts +2 -0
- package/dist/utils/extract.js +2 -1
- package/dist/utils/extract.js.map +1 -1
- package/dist/utils/extract.mjs +3 -3
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +2 -1
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +3 -3
- package/package.json +5 -5
- package/src/components/copilot-provider/copilotkit-props.tsx +5 -0
- package/src/components/copilot-provider/copilotkit.tsx +45 -3
- package/src/context/copilot-context.tsx +44 -3
- package/src/hooks/index.ts +2 -0
- package/src/hooks/use-chat.ts +131 -8
- package/src/hooks/use-coagent-action.ts +44 -0
- package/src/hooks/use-coagent.ts +133 -0
- package/src/hooks/use-copilot-action.ts +9 -5
- package/src/hooks/use-copilot-chat.ts +80 -8
- package/src/hooks/use-copilot-readable.ts +3 -3
- package/src/hooks/use-make-copilot-document-readable.ts +3 -3
- package/src/lib/copilot-task.ts +1 -0
- package/src/types/coagent-action.ts +17 -0
- package/src/types/coagent-state.ts +9 -0
- package/src/types/frontend-action.ts +1 -0
- package/src/utils/extract.ts +1 -0
- package/dist/chunk-3NHEWJ4V.mjs.map +0 -1
- package/dist/chunk-5JB4B2SV.mjs +0 -1
- package/dist/chunk-AGGKI26A.mjs.map +0 -1
- package/dist/chunk-KONDJRB4.mjs.map +0 -1
- package/dist/chunk-T6L4EGTD.mjs.map +0 -1
- package/dist/chunk-TVR5CJ6E.mjs.map +0 -1
- package/dist/chunk-VFDR4MIF.mjs.map +0 -1
- package/dist/chunk-YYZNR3CS.mjs.map +0 -1
- /package/dist/{chunk-5JB4B2SV.mjs.map → chunk-BWYAGPEF.mjs.map} +0 -0
package/dist/hooks/index.js
CHANGED
|
@@ -79,6 +79,8 @@ var __async = (__this, __arguments, generator) => {
|
|
|
79
79
|
// src/hooks/index.ts
|
|
80
80
|
var hooks_exports = {};
|
|
81
81
|
__export(hooks_exports, {
|
|
82
|
+
useCoAgent: () => useCoAgent,
|
|
83
|
+
useCoAgentAction: () => useCoAgentAction,
|
|
82
84
|
useCopilotAction: () => useCopilotAction,
|
|
83
85
|
useCopilotChat: () => useCopilotChat,
|
|
84
86
|
useCopilotReadable: () => useCopilotReadable,
|
|
@@ -97,7 +99,12 @@ var emptyCopilotContext = {
|
|
|
97
99
|
},
|
|
98
100
|
removeAction: () => {
|
|
99
101
|
},
|
|
100
|
-
|
|
102
|
+
coagentActions: {},
|
|
103
|
+
setCoagentAction: () => {
|
|
104
|
+
},
|
|
105
|
+
removeCoagentAction: () => {
|
|
106
|
+
},
|
|
107
|
+
chatComponentsCache: { current: { actions: {}, coagentActions: {} } },
|
|
101
108
|
getContextString: (documents, categories) => returnAndThrowInDebug(""),
|
|
102
109
|
addContext: () => "",
|
|
103
110
|
removeContext: () => {
|
|
@@ -130,9 +137,22 @@ var emptyCopilotContext = {
|
|
|
130
137
|
},
|
|
131
138
|
removeChatSuggestionConfiguration: () => {
|
|
132
139
|
},
|
|
133
|
-
showDevConsole: "auto"
|
|
140
|
+
showDevConsole: "auto",
|
|
141
|
+
coagentStates: {},
|
|
142
|
+
setCoagentStates: () => {
|
|
143
|
+
},
|
|
144
|
+
agentSession: null,
|
|
145
|
+
setAgentSession: () => {
|
|
146
|
+
}
|
|
134
147
|
};
|
|
135
148
|
var CopilotContext = import_react.default.createContext(emptyCopilotContext);
|
|
149
|
+
function useCopilotContext() {
|
|
150
|
+
const context = import_react.default.useContext(CopilotContext);
|
|
151
|
+
if (context === emptyCopilotContext) {
|
|
152
|
+
throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
|
|
153
|
+
}
|
|
154
|
+
return context;
|
|
155
|
+
}
|
|
136
156
|
function returnAndThrowInDebug(value) {
|
|
137
157
|
throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
|
|
138
158
|
return value;
|
|
@@ -155,7 +175,12 @@ function useChat(options) {
|
|
|
155
175
|
initialMessages,
|
|
156
176
|
isLoading,
|
|
157
177
|
actions,
|
|
158
|
-
onFunctionCall
|
|
178
|
+
onFunctionCall,
|
|
179
|
+
onCoagentAction,
|
|
180
|
+
setCoagentStates,
|
|
181
|
+
coagentStates,
|
|
182
|
+
agentSession,
|
|
183
|
+
setAgentSession
|
|
159
184
|
} = options;
|
|
160
185
|
const abortControllerRef = (0, import_react2.useRef)();
|
|
161
186
|
const threadIdRef = (0, import_react2.useRef)(null);
|
|
@@ -184,13 +209,14 @@ function useChat(options) {
|
|
|
184
209
|
const messagesWithContext = [systemMessage, ...initialMessages || [], ...previousMessages];
|
|
185
210
|
const stream = import_runtime_client_gql.CopilotRuntimeClient.asStream(
|
|
186
211
|
runtimeClient.generateCopilotResponse({
|
|
187
|
-
data: __spreadProps(__spreadValues({
|
|
212
|
+
data: __spreadProps(__spreadValues(__spreadProps(__spreadValues({
|
|
188
213
|
frontend: {
|
|
189
214
|
actions: actions.map((action) => ({
|
|
190
215
|
name: action.name,
|
|
191
216
|
description: action.description || "",
|
|
192
217
|
jsonSchema: JSON.stringify((0, import_shared.actionParametersToJsonSchema)(action.parameters || []))
|
|
193
|
-
}))
|
|
218
|
+
})),
|
|
219
|
+
url: window.location.href
|
|
194
220
|
},
|
|
195
221
|
threadId: threadIdRef.current,
|
|
196
222
|
runId: runIdRef.current,
|
|
@@ -208,6 +234,13 @@ function useChat(options) {
|
|
|
208
234
|
metadata: {
|
|
209
235
|
requestType: import_runtime_client_gql.CopilotRequestType.Chat
|
|
210
236
|
}
|
|
237
|
+
}), agentSession ? {
|
|
238
|
+
agentSession
|
|
239
|
+
} : {}), {
|
|
240
|
+
agentStates: Object.values(coagentStates).map((state) => ({
|
|
241
|
+
agentName: state.name,
|
|
242
|
+
state: JSON.stringify(state.state)
|
|
243
|
+
}))
|
|
211
244
|
}),
|
|
212
245
|
properties: copilotConfig.properties,
|
|
213
246
|
signal: (_d = abortControllerRef.current) == null ? void 0 : _d.signal
|
|
@@ -215,7 +248,8 @@ function useChat(options) {
|
|
|
215
248
|
);
|
|
216
249
|
const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
|
|
217
250
|
const reader = stream.getReader();
|
|
218
|
-
let
|
|
251
|
+
let actionResults = {};
|
|
252
|
+
let executedCoagentActions = [];
|
|
219
253
|
try {
|
|
220
254
|
while (true) {
|
|
221
255
|
const { done, value } = yield reader.read();
|
|
@@ -242,8 +276,30 @@ function useChat(options) {
|
|
|
242
276
|
} else {
|
|
243
277
|
for (const message of messages2) {
|
|
244
278
|
newMessages.push(message);
|
|
279
|
+
if (message instanceof import_runtime_client_gql.AgentStateMessage) {
|
|
280
|
+
if (message.running) {
|
|
281
|
+
setCoagentStates((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
282
|
+
[message.agentName]: {
|
|
283
|
+
name: message.agentName,
|
|
284
|
+
state: message.state,
|
|
285
|
+
running: message.running,
|
|
286
|
+
active: message.active,
|
|
287
|
+
threadId: message.threadId,
|
|
288
|
+
nodeName: message.nodeName,
|
|
289
|
+
runId: message.runId
|
|
290
|
+
}
|
|
291
|
+
}));
|
|
292
|
+
setAgentSession({
|
|
293
|
+
threadId: message.threadId,
|
|
294
|
+
agentName: message.agentName,
|
|
295
|
+
nodeName: message.nodeName
|
|
296
|
+
});
|
|
297
|
+
} else {
|
|
298
|
+
setAgentSession(null);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
245
301
|
if (message instanceof import_runtime_client_gql.ActionExecutionMessage && message.status.code !== import_runtime_client_gql.MessageStatusCode.Pending && message.scope === "client" && onFunctionCall) {
|
|
246
|
-
if (!(message.id in
|
|
302
|
+
if (!(message.id in actionResults)) {
|
|
247
303
|
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
248
304
|
break;
|
|
249
305
|
}
|
|
@@ -252,25 +308,53 @@ function useChat(options) {
|
|
|
252
308
|
name: message.name,
|
|
253
309
|
args: message.arguments
|
|
254
310
|
});
|
|
255
|
-
|
|
311
|
+
actionResults[message.id] = result;
|
|
256
312
|
}
|
|
257
313
|
newMessages.push(
|
|
258
314
|
new import_runtime_client_gql.ResultMessage({
|
|
259
|
-
result: import_runtime_client_gql.ResultMessage.encodeResult(
|
|
315
|
+
result: import_runtime_client_gql.ResultMessage.encodeResult(actionResults[message.id]),
|
|
260
316
|
actionExecutionId: message.id,
|
|
261
317
|
actionName: message.name
|
|
262
318
|
})
|
|
263
319
|
);
|
|
264
320
|
}
|
|
321
|
+
if (message instanceof import_runtime_client_gql.AgentStateMessage && !message.active && !executedCoagentActions.includes(message.id) && onCoagentAction) {
|
|
322
|
+
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
323
|
+
break;
|
|
324
|
+
}
|
|
325
|
+
yield onCoagentAction({
|
|
326
|
+
name: message.agentName,
|
|
327
|
+
nodeName: message.nodeName,
|
|
328
|
+
state: message.state
|
|
329
|
+
});
|
|
330
|
+
executedCoagentActions.push(message.id);
|
|
331
|
+
}
|
|
265
332
|
}
|
|
266
333
|
}
|
|
267
334
|
if (newMessages.length > 0) {
|
|
268
|
-
|
|
335
|
+
const filteredMessages = [...previousMessages, ...newMessages].reduce(
|
|
336
|
+
(acc, message) => {
|
|
337
|
+
if (
|
|
338
|
+
// If the current message is an AgentStateMessage
|
|
339
|
+
message instanceof import_runtime_client_gql.AgentStateMessage && // And there is at least one message in the accumulator
|
|
340
|
+
acc.length > 0 && // And the last message in the accumulator is also an AgentStateMessage
|
|
341
|
+
acc[acc.length - 1] instanceof import_runtime_client_gql.AgentStateMessage && // And the agentName, nodeName, and runId are the same
|
|
342
|
+
acc[acc.length - 1].agentName === message.agentName && acc[acc.length - 1].nodeName === message.nodeName && acc[acc.length - 1].runId === message.runId
|
|
343
|
+
) {
|
|
344
|
+
acc[acc.length - 1] = message;
|
|
345
|
+
} else {
|
|
346
|
+
acc.push(message);
|
|
347
|
+
}
|
|
348
|
+
return acc;
|
|
349
|
+
},
|
|
350
|
+
[]
|
|
351
|
+
);
|
|
352
|
+
setMessages(filteredMessages);
|
|
269
353
|
}
|
|
270
354
|
}
|
|
271
355
|
if (
|
|
272
356
|
// if we have client side results
|
|
273
|
-
Object.values(
|
|
357
|
+
Object.values(actionResults).length || // or the last message we received is a result
|
|
274
358
|
newMessages.length && newMessages[newMessages.length - 1] instanceof import_runtime_client_gql.ResultMessage
|
|
275
359
|
) {
|
|
276
360
|
yield new Promise((resolve) => setTimeout(resolve, 10));
|
|
@@ -339,8 +423,13 @@ function useCopilotChat(_a = {}) {
|
|
|
339
423
|
isLoading,
|
|
340
424
|
setIsLoading,
|
|
341
425
|
chatInstructions,
|
|
342
|
-
actions
|
|
343
|
-
|
|
426
|
+
actions,
|
|
427
|
+
coagentStates,
|
|
428
|
+
setCoagentStates,
|
|
429
|
+
coagentActions,
|
|
430
|
+
agentSession,
|
|
431
|
+
setAgentSession
|
|
432
|
+
} = useCopilotContext();
|
|
344
433
|
const latestGetContextString = useUpdatedRef(getContextString);
|
|
345
434
|
const deleteMessage = (0, import_react4.useCallback)(
|
|
346
435
|
(messageId) => {
|
|
@@ -356,24 +445,76 @@ function useCopilotChat(_a = {}) {
|
|
|
356
445
|
role: import_runtime_client_gql2.Role.System
|
|
357
446
|
});
|
|
358
447
|
}, [getContextString, makeSystemMessage, chatInstructions]);
|
|
448
|
+
const onCoagentAction = (0, import_react4.useCallback)(
|
|
449
|
+
(args) => __async(this, null, function* () {
|
|
450
|
+
var _a2;
|
|
451
|
+
const { name, nodeName, state } = args;
|
|
452
|
+
let action = Object.values(coagentActions).find(
|
|
453
|
+
(action2) => action2.name === name && action2.nodeName === nodeName
|
|
454
|
+
);
|
|
455
|
+
if (!action) {
|
|
456
|
+
action = Object.values(coagentActions).find(
|
|
457
|
+
(action2) => action2.name === name && !action2.nodeName
|
|
458
|
+
);
|
|
459
|
+
}
|
|
460
|
+
if (action) {
|
|
461
|
+
yield (_a2 = action.handler) == null ? void 0 : _a2.call(action, { state, nodeName });
|
|
462
|
+
}
|
|
463
|
+
}),
|
|
464
|
+
[coagentActions]
|
|
465
|
+
);
|
|
359
466
|
const { append, reload, stop } = useChat(__spreadProps(__spreadValues({}, options), {
|
|
360
467
|
actions: Object.values(actions),
|
|
361
468
|
copilotConfig: copilotApiConfig,
|
|
362
469
|
initialMessages: options.initialMessages || [],
|
|
363
470
|
onFunctionCall: getFunctionCallHandler(),
|
|
471
|
+
onCoagentAction,
|
|
364
472
|
messages,
|
|
365
473
|
setMessages,
|
|
366
474
|
makeSystemMessageCallback,
|
|
367
475
|
isLoading,
|
|
368
|
-
setIsLoading
|
|
476
|
+
setIsLoading,
|
|
477
|
+
coagentStates,
|
|
478
|
+
setCoagentStates,
|
|
479
|
+
agentSession,
|
|
480
|
+
setAgentSession
|
|
369
481
|
}));
|
|
482
|
+
const latestAppend = useUpdatedRef(append);
|
|
483
|
+
const latestAppendFunc = (0, import_react4.useCallback)(
|
|
484
|
+
(message) => {
|
|
485
|
+
return latestAppend.current(message);
|
|
486
|
+
},
|
|
487
|
+
[latestAppend]
|
|
488
|
+
);
|
|
489
|
+
const latestReload = useUpdatedRef(reload);
|
|
490
|
+
const latestReloadFunc = (0, import_react4.useCallback)(() => {
|
|
491
|
+
return latestReload.current();
|
|
492
|
+
}, [latestReload]);
|
|
493
|
+
const latestStop = useUpdatedRef(stop);
|
|
494
|
+
const latestStopFunc = (0, import_react4.useCallback)(() => {
|
|
495
|
+
return latestStop.current();
|
|
496
|
+
}, [latestStop]);
|
|
497
|
+
const latestDelete = useUpdatedRef(deleteMessage);
|
|
498
|
+
const latestDeleteFunc = (0, import_react4.useCallback)(
|
|
499
|
+
(messageId) => {
|
|
500
|
+
return latestDelete.current(messageId);
|
|
501
|
+
},
|
|
502
|
+
[latestDelete]
|
|
503
|
+
);
|
|
504
|
+
const latestSetMessages = useUpdatedRef(setMessages);
|
|
505
|
+
const latestSetMessagesFunc = (0, import_react4.useCallback)(
|
|
506
|
+
(messages2) => {
|
|
507
|
+
return latestSetMessages.current(messages2);
|
|
508
|
+
},
|
|
509
|
+
[latestSetMessages]
|
|
510
|
+
);
|
|
370
511
|
return {
|
|
371
512
|
visibleMessages: messages,
|
|
372
|
-
appendMessage:
|
|
373
|
-
setMessages,
|
|
374
|
-
reloadMessages:
|
|
375
|
-
stopGeneration:
|
|
376
|
-
deleteMessage,
|
|
513
|
+
appendMessage: latestAppendFunc,
|
|
514
|
+
setMessages: latestSetMessagesFunc,
|
|
515
|
+
reloadMessages: latestReloadFunc,
|
|
516
|
+
stopGeneration: latestStopFunc,
|
|
517
|
+
deleteMessage: latestDeleteFunc,
|
|
377
518
|
isLoading
|
|
378
519
|
};
|
|
379
520
|
}
|
|
@@ -412,22 +553,25 @@ ${additionalInstructions}` : "");
|
|
|
412
553
|
var import_react5 = require("react");
|
|
413
554
|
var import_shared3 = require("@copilotkit/shared");
|
|
414
555
|
function useCopilotAction(action, dependencies) {
|
|
415
|
-
const { setAction, removeAction, actions, chatComponentsCache } = (
|
|
556
|
+
const { setAction, removeAction, actions, chatComponentsCache } = useCopilotContext();
|
|
416
557
|
const idRef = (0, import_react5.useRef)((0, import_shared3.randomId)());
|
|
417
558
|
if (dependencies === void 0) {
|
|
418
559
|
if (actions[idRef.current]) {
|
|
419
560
|
actions[idRef.current].handler = action.handler;
|
|
420
561
|
if (typeof action.render === "function") {
|
|
421
562
|
if (chatComponentsCache.current !== null) {
|
|
422
|
-
chatComponentsCache.current[action.name] = action.render;
|
|
563
|
+
chatComponentsCache.current.actions[action.name] = action.render;
|
|
423
564
|
}
|
|
424
565
|
}
|
|
425
566
|
}
|
|
426
567
|
}
|
|
427
568
|
(0, import_react5.useEffect)(() => {
|
|
569
|
+
if (action.disabled) {
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
428
572
|
setAction(idRef.current, action);
|
|
429
573
|
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
430
|
-
chatComponentsCache.current[action.name] = action.render;
|
|
574
|
+
chatComponentsCache.current.actions[action.name] = action.render;
|
|
431
575
|
}
|
|
432
576
|
return () => {
|
|
433
577
|
removeAction(idRef.current);
|
|
@@ -437,6 +581,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
437
581
|
removeAction,
|
|
438
582
|
action.description,
|
|
439
583
|
action.name,
|
|
584
|
+
action.disabled,
|
|
440
585
|
// This should be faster than deep equality checking
|
|
441
586
|
// In addition, all major JS engines guarantee the order of object keys
|
|
442
587
|
JSON.stringify(action.parameters),
|
|
@@ -447,12 +592,48 @@ function useCopilotAction(action, dependencies) {
|
|
|
447
592
|
]);
|
|
448
593
|
}
|
|
449
594
|
|
|
450
|
-
// src/hooks/use-
|
|
595
|
+
// src/hooks/use-coagent-action.ts
|
|
451
596
|
var import_react6 = require("react");
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
const
|
|
597
|
+
var import_shared4 = require("@copilotkit/shared");
|
|
598
|
+
function useCoAgentAction(action, dependencies) {
|
|
599
|
+
const { setCoagentAction, removeCoagentAction, coagentActions, chatComponentsCache } = (0, import_react6.useContext)(CopilotContext);
|
|
600
|
+
const idRef = (0, import_react6.useRef)((0, import_shared4.randomId)());
|
|
601
|
+
const key = `${action.name}-${action.nodeName || "global"}`;
|
|
602
|
+
if (dependencies === void 0) {
|
|
603
|
+
if (coagentActions[idRef.current]) {
|
|
604
|
+
coagentActions[idRef.current].handler = action.handler;
|
|
605
|
+
if (typeof action.render === "function") {
|
|
606
|
+
if (chatComponentsCache.current !== null) {
|
|
607
|
+
chatComponentsCache.current.coagentActions[key] = action.render;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
}
|
|
455
612
|
(0, import_react6.useEffect)(() => {
|
|
613
|
+
setCoagentAction(idRef.current, action);
|
|
614
|
+
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
615
|
+
chatComponentsCache.current.coagentActions[key] = action.render;
|
|
616
|
+
}
|
|
617
|
+
return () => {
|
|
618
|
+
removeCoagentAction(idRef.current);
|
|
619
|
+
};
|
|
620
|
+
}, [
|
|
621
|
+
setCoagentAction,
|
|
622
|
+
removeCoagentAction,
|
|
623
|
+
action.name,
|
|
624
|
+
// include render only if it's a string
|
|
625
|
+
typeof action.render === "string" ? action.render : void 0,
|
|
626
|
+
// dependencies set by the developer
|
|
627
|
+
...dependencies || []
|
|
628
|
+
]);
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// src/hooks/use-make-copilot-document-readable.ts
|
|
632
|
+
var import_react7 = require("react");
|
|
633
|
+
function useMakeCopilotDocumentReadable(document, categories, dependencies = []) {
|
|
634
|
+
const { addDocumentContext, removeDocumentContext } = useCopilotContext();
|
|
635
|
+
const idRef = (0, import_react7.useRef)();
|
|
636
|
+
(0, import_react7.useEffect)(() => {
|
|
456
637
|
const id = addDocumentContext(document, categories);
|
|
457
638
|
idRef.current = id;
|
|
458
639
|
return () => {
|
|
@@ -463,16 +644,16 @@ function useMakeCopilotDocumentReadable(document, categories, dependencies = [])
|
|
|
463
644
|
}
|
|
464
645
|
|
|
465
646
|
// src/hooks/use-copilot-readable.ts
|
|
466
|
-
var
|
|
647
|
+
var import_react8 = require("react");
|
|
467
648
|
function convertToJSON(description, value) {
|
|
468
649
|
return `${description}: ${typeof value === "string" ? value : JSON.stringify(value)}`;
|
|
469
650
|
}
|
|
470
651
|
function useCopilotReadable({ description, value, parentId, categories, convert }, dependencies) {
|
|
471
|
-
const { addContext, removeContext } = (
|
|
472
|
-
const idRef = (0,
|
|
652
|
+
const { addContext, removeContext } = useCopilotContext();
|
|
653
|
+
const idRef = (0, import_react8.useRef)();
|
|
473
654
|
convert = convert || convertToJSON;
|
|
474
655
|
const information = convert(description, value);
|
|
475
|
-
(0,
|
|
656
|
+
(0, import_react8.useEffect)(() => {
|
|
476
657
|
const id = addContext(information, parentId, categories);
|
|
477
658
|
idRef.current = id;
|
|
478
659
|
return () => {
|
|
@@ -481,8 +662,85 @@ function useCopilotReadable({ description, value, parentId, categories, convert
|
|
|
481
662
|
}, [information, parentId, addContext, removeContext, ...dependencies || []]);
|
|
482
663
|
return idRef.current;
|
|
483
664
|
}
|
|
665
|
+
|
|
666
|
+
// src/hooks/use-coagent.ts
|
|
667
|
+
var import_react9 = require("react");
|
|
668
|
+
function useCoAgent(options) {
|
|
669
|
+
const isExternalStateManagement = (options2) => {
|
|
670
|
+
return "state" in options2 && "setState" in options2;
|
|
671
|
+
};
|
|
672
|
+
const { name } = options;
|
|
673
|
+
const isInternalStateManagementWithInitial = (options2) => {
|
|
674
|
+
return "initialState" in options2;
|
|
675
|
+
};
|
|
676
|
+
const { coagentStates, setCoagentStates } = useCopilotContext();
|
|
677
|
+
const getCoagentState = (coagentStates2, name2) => {
|
|
678
|
+
if (coagentStates2[name2]) {
|
|
679
|
+
return coagentStates2[name2];
|
|
680
|
+
} else {
|
|
681
|
+
return {
|
|
682
|
+
name: name2,
|
|
683
|
+
state: isInternalStateManagementWithInitial(options) ? options.initialState : {},
|
|
684
|
+
running: false,
|
|
685
|
+
active: false,
|
|
686
|
+
threadId: void 0,
|
|
687
|
+
nodeName: void 0,
|
|
688
|
+
runId: void 0
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
};
|
|
692
|
+
const setState = (newState) => {
|
|
693
|
+
setCoagentStates((prevAgentStates) => {
|
|
694
|
+
let coagentState2 = getCoagentState(prevAgentStates, name);
|
|
695
|
+
const updatedState = typeof newState === "function" ? newState(coagentState2.state) : newState;
|
|
696
|
+
return __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
697
|
+
[name]: __spreadProps(__spreadValues({}, coagentState2), {
|
|
698
|
+
state: updatedState
|
|
699
|
+
})
|
|
700
|
+
});
|
|
701
|
+
});
|
|
702
|
+
};
|
|
703
|
+
const coagentState = getCoagentState(coagentStates, name);
|
|
704
|
+
const state = isExternalStateManagement(options) ? options.state : coagentState.state;
|
|
705
|
+
(0, import_react9.useEffect)(() => {
|
|
706
|
+
if (isExternalStateManagement(options)) {
|
|
707
|
+
setState(options.state);
|
|
708
|
+
} else if (coagentStates[name] === void 0) {
|
|
709
|
+
setState(options.initialState === void 0 ? {} : options.initialState);
|
|
710
|
+
}
|
|
711
|
+
}, [isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0]);
|
|
712
|
+
return {
|
|
713
|
+
name,
|
|
714
|
+
nodeName: coagentState.nodeName,
|
|
715
|
+
state,
|
|
716
|
+
setState,
|
|
717
|
+
running: coagentState.running,
|
|
718
|
+
start: () => {
|
|
719
|
+
startAgent(name);
|
|
720
|
+
},
|
|
721
|
+
stop: () => {
|
|
722
|
+
stopAgent(name);
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
}
|
|
726
|
+
function startAgent(name) {
|
|
727
|
+
const { setAgentSession } = useCopilotContext();
|
|
728
|
+
setAgentSession({
|
|
729
|
+
agentName: name
|
|
730
|
+
});
|
|
731
|
+
}
|
|
732
|
+
function stopAgent(name) {
|
|
733
|
+
const { agentSession, setAgentSession } = useCopilotContext();
|
|
734
|
+
if (agentSession && agentSession.agentName === name) {
|
|
735
|
+
setAgentSession(null);
|
|
736
|
+
} else {
|
|
737
|
+
console.warn(`No agent session found for ${name}`);
|
|
738
|
+
}
|
|
739
|
+
}
|
|
484
740
|
// Annotate the CommonJS export names for ESM import in node:
|
|
485
741
|
0 && (module.exports = {
|
|
742
|
+
useCoAgent,
|
|
743
|
+
useCoAgentAction,
|
|
486
744
|
useCopilotAction,
|
|
487
745
|
useCopilotChat,
|
|
488
746
|
useCopilotReadable,
|