@copilotkit/react-core 1.1.2 → 1.1.3-feat-runtime-remote-actions.1
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 +24 -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-GE7LAL6V.mjs → chunk-3U22BXKC.mjs} +6 -4
- package/dist/chunk-3U22BXKC.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-KONDJRB4.mjs → chunk-CODXG6KU.mjs} +5 -5
- package/dist/chunk-CODXG6KU.mjs.map +1 -0
- package/dist/{chunk-YYZNR3CS.mjs → chunk-HABY7NLH.mjs} +72 -14
- package/dist/chunk-HABY7NLH.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-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-STUXJ3BN.mjs +44 -0
- package/dist/chunk-STUXJ3BN.mjs.map +1 -0
- package/dist/{chunk-UQRDVZ2N.mjs → chunk-TCZBK3HZ.mjs} +9 -5
- package/dist/chunk-TCZBK3HZ.mjs.map +1 -0
- package/dist/{chunk-AGGKI26A.mjs → chunk-XSZDVRPW.mjs} +64 -12
- package/dist/chunk-XSZDVRPW.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 +274 -31
- 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 +59 -10
- 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 +144 -20
- 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 +320 -44
- 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 +3 -2
- 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 +3 -2
- 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 +3 -2
- 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 +3 -2
- 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 +107 -11
- 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 +3 -1
- 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 +8 -2
- 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-GE7LAL6V.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-UQRDVZ2N.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,17 +209,18 @@ 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,
|
|
197
|
-
messages: (0, import_runtime_client_gql.convertMessagesToGqlInput)(messagesWithContext)
|
|
223
|
+
messages: (0, import_runtime_client_gql.convertMessagesToGqlInput)((0, import_runtime_client_gql.filterAgentStateMessages)(messagesWithContext))
|
|
198
224
|
}, copilotConfig.cloud ? {
|
|
199
225
|
cloud: __spreadValues({}, ((_c = (_b = (_a = copilotConfig.cloud.guardrails) == null ? void 0 : _a.input) == null ? void 0 : _b.restrictToTopic) == null ? void 0 : _c.enabled) ? {
|
|
200
226
|
guardrails: {
|
|
@@ -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();
|
|
@@ -227,7 +261,9 @@ function useChat(options) {
|
|
|
227
261
|
}
|
|
228
262
|
threadIdRef.current = value.generateCopilotResponse.threadId || null;
|
|
229
263
|
runIdRef.current = value.generateCopilotResponse.runId || null;
|
|
230
|
-
const messages2 = (0, import_runtime_client_gql.convertGqlOutputToMessages)(
|
|
264
|
+
const messages2 = (0, import_runtime_client_gql.convertGqlOutputToMessages)(
|
|
265
|
+
(0, import_runtime_client_gql.filterAdjacentAgentStateMessages)(value.generateCopilotResponse.messages)
|
|
266
|
+
);
|
|
231
267
|
if (messages2.length === 0) {
|
|
232
268
|
continue;
|
|
233
269
|
}
|
|
@@ -242,8 +278,30 @@ function useChat(options) {
|
|
|
242
278
|
} else {
|
|
243
279
|
for (const message of messages2) {
|
|
244
280
|
newMessages.push(message);
|
|
281
|
+
if (message instanceof import_runtime_client_gql.AgentStateMessage) {
|
|
282
|
+
if (message.running) {
|
|
283
|
+
setCoagentStates((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
284
|
+
[message.agentName]: {
|
|
285
|
+
name: message.agentName,
|
|
286
|
+
state: message.state,
|
|
287
|
+
running: message.running,
|
|
288
|
+
active: message.active,
|
|
289
|
+
threadId: message.threadId,
|
|
290
|
+
nodeName: message.nodeName,
|
|
291
|
+
runId: message.runId
|
|
292
|
+
}
|
|
293
|
+
}));
|
|
294
|
+
setAgentSession({
|
|
295
|
+
threadId: message.threadId,
|
|
296
|
+
agentName: message.agentName,
|
|
297
|
+
nodeName: message.nodeName
|
|
298
|
+
});
|
|
299
|
+
} else {
|
|
300
|
+
setAgentSession(null);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
245
303
|
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
|
|
304
|
+
if (!(message.id in actionResults)) {
|
|
247
305
|
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
248
306
|
break;
|
|
249
307
|
}
|
|
@@ -252,16 +310,27 @@ function useChat(options) {
|
|
|
252
310
|
name: message.name,
|
|
253
311
|
args: message.arguments
|
|
254
312
|
});
|
|
255
|
-
|
|
313
|
+
actionResults[message.id] = result;
|
|
256
314
|
}
|
|
257
315
|
newMessages.push(
|
|
258
316
|
new import_runtime_client_gql.ResultMessage({
|
|
259
|
-
result: import_runtime_client_gql.ResultMessage.encodeResult(
|
|
317
|
+
result: import_runtime_client_gql.ResultMessage.encodeResult(actionResults[message.id]),
|
|
260
318
|
actionExecutionId: message.id,
|
|
261
319
|
actionName: message.name
|
|
262
320
|
})
|
|
263
321
|
);
|
|
264
322
|
}
|
|
323
|
+
if (message instanceof import_runtime_client_gql.AgentStateMessage && !message.active && !executedCoagentActions.includes(message.id) && onCoagentAction) {
|
|
324
|
+
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
yield onCoagentAction({
|
|
328
|
+
name: message.agentName,
|
|
329
|
+
nodeName: message.nodeName,
|
|
330
|
+
state: message.state
|
|
331
|
+
});
|
|
332
|
+
executedCoagentActions.push(message.id);
|
|
333
|
+
}
|
|
265
334
|
}
|
|
266
335
|
}
|
|
267
336
|
if (newMessages.length > 0) {
|
|
@@ -270,7 +339,7 @@ function useChat(options) {
|
|
|
270
339
|
}
|
|
271
340
|
if (
|
|
272
341
|
// if we have client side results
|
|
273
|
-
Object.values(
|
|
342
|
+
Object.values(actionResults).length || // or the last message we received is a result
|
|
274
343
|
newMessages.length && newMessages[newMessages.length - 1] instanceof import_runtime_client_gql.ResultMessage
|
|
275
344
|
) {
|
|
276
345
|
yield new Promise((resolve) => setTimeout(resolve, 10));
|
|
@@ -339,8 +408,13 @@ function useCopilotChat(_a = {}) {
|
|
|
339
408
|
isLoading,
|
|
340
409
|
setIsLoading,
|
|
341
410
|
chatInstructions,
|
|
342
|
-
actions
|
|
343
|
-
|
|
411
|
+
actions,
|
|
412
|
+
coagentStates,
|
|
413
|
+
setCoagentStates,
|
|
414
|
+
coagentActions,
|
|
415
|
+
agentSession,
|
|
416
|
+
setAgentSession
|
|
417
|
+
} = useCopilotContext();
|
|
344
418
|
const latestGetContextString = useUpdatedRef(getContextString);
|
|
345
419
|
const deleteMessage = (0, import_react4.useCallback)(
|
|
346
420
|
(messageId) => {
|
|
@@ -356,24 +430,76 @@ function useCopilotChat(_a = {}) {
|
|
|
356
430
|
role: import_runtime_client_gql2.Role.System
|
|
357
431
|
});
|
|
358
432
|
}, [getContextString, makeSystemMessage, chatInstructions]);
|
|
433
|
+
const onCoagentAction = (0, import_react4.useCallback)(
|
|
434
|
+
(args) => __async(this, null, function* () {
|
|
435
|
+
var _a2;
|
|
436
|
+
const { name, nodeName, state } = args;
|
|
437
|
+
let action = Object.values(coagentActions).find(
|
|
438
|
+
(action2) => action2.name === name && action2.nodeName === nodeName
|
|
439
|
+
);
|
|
440
|
+
if (!action) {
|
|
441
|
+
action = Object.values(coagentActions).find(
|
|
442
|
+
(action2) => action2.name === name && !action2.nodeName
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
if (action) {
|
|
446
|
+
yield (_a2 = action.handler) == null ? void 0 : _a2.call(action, { state, nodeName });
|
|
447
|
+
}
|
|
448
|
+
}),
|
|
449
|
+
[coagentActions]
|
|
450
|
+
);
|
|
359
451
|
const { append, reload, stop } = useChat(__spreadProps(__spreadValues({}, options), {
|
|
360
452
|
actions: Object.values(actions),
|
|
361
453
|
copilotConfig: copilotApiConfig,
|
|
362
454
|
initialMessages: options.initialMessages || [],
|
|
363
455
|
onFunctionCall: getFunctionCallHandler(),
|
|
456
|
+
onCoagentAction,
|
|
364
457
|
messages,
|
|
365
458
|
setMessages,
|
|
366
459
|
makeSystemMessageCallback,
|
|
367
460
|
isLoading,
|
|
368
|
-
setIsLoading
|
|
461
|
+
setIsLoading,
|
|
462
|
+
coagentStates,
|
|
463
|
+
setCoagentStates,
|
|
464
|
+
agentSession,
|
|
465
|
+
setAgentSession
|
|
369
466
|
}));
|
|
467
|
+
const latestAppend = useUpdatedRef(append);
|
|
468
|
+
const latestAppendFunc = (0, import_react4.useCallback)(
|
|
469
|
+
(message) => {
|
|
470
|
+
return latestAppend.current(message);
|
|
471
|
+
},
|
|
472
|
+
[latestAppend]
|
|
473
|
+
);
|
|
474
|
+
const latestReload = useUpdatedRef(reload);
|
|
475
|
+
const latestReloadFunc = (0, import_react4.useCallback)(() => {
|
|
476
|
+
return latestReload.current();
|
|
477
|
+
}, [latestReload]);
|
|
478
|
+
const latestStop = useUpdatedRef(stop);
|
|
479
|
+
const latestStopFunc = (0, import_react4.useCallback)(() => {
|
|
480
|
+
return latestStop.current();
|
|
481
|
+
}, [latestStop]);
|
|
482
|
+
const latestDelete = useUpdatedRef(deleteMessage);
|
|
483
|
+
const latestDeleteFunc = (0, import_react4.useCallback)(
|
|
484
|
+
(messageId) => {
|
|
485
|
+
return latestDelete.current(messageId);
|
|
486
|
+
},
|
|
487
|
+
[latestDelete]
|
|
488
|
+
);
|
|
489
|
+
const latestSetMessages = useUpdatedRef(setMessages);
|
|
490
|
+
const latestSetMessagesFunc = (0, import_react4.useCallback)(
|
|
491
|
+
(messages2) => {
|
|
492
|
+
return latestSetMessages.current(messages2);
|
|
493
|
+
},
|
|
494
|
+
[latestSetMessages]
|
|
495
|
+
);
|
|
370
496
|
return {
|
|
371
497
|
visibleMessages: messages,
|
|
372
|
-
appendMessage:
|
|
373
|
-
setMessages,
|
|
374
|
-
reloadMessages:
|
|
375
|
-
stopGeneration:
|
|
376
|
-
deleteMessage,
|
|
498
|
+
appendMessage: latestAppendFunc,
|
|
499
|
+
setMessages: latestSetMessagesFunc,
|
|
500
|
+
reloadMessages: latestReloadFunc,
|
|
501
|
+
stopGeneration: latestStopFunc,
|
|
502
|
+
deleteMessage: latestDeleteFunc,
|
|
377
503
|
isLoading
|
|
378
504
|
};
|
|
379
505
|
}
|
|
@@ -412,22 +538,25 @@ ${additionalInstructions}` : "");
|
|
|
412
538
|
var import_react5 = require("react");
|
|
413
539
|
var import_shared3 = require("@copilotkit/shared");
|
|
414
540
|
function useCopilotAction(action, dependencies) {
|
|
415
|
-
const { setAction, removeAction, actions, chatComponentsCache } = (
|
|
541
|
+
const { setAction, removeAction, actions, chatComponentsCache } = useCopilotContext();
|
|
416
542
|
const idRef = (0, import_react5.useRef)((0, import_shared3.randomId)());
|
|
417
543
|
if (dependencies === void 0) {
|
|
418
544
|
if (actions[idRef.current]) {
|
|
419
545
|
actions[idRef.current].handler = action.handler;
|
|
420
546
|
if (typeof action.render === "function") {
|
|
421
547
|
if (chatComponentsCache.current !== null) {
|
|
422
|
-
chatComponentsCache.current[action.name] = action.render;
|
|
548
|
+
chatComponentsCache.current.actions[action.name] = action.render;
|
|
423
549
|
}
|
|
424
550
|
}
|
|
425
551
|
}
|
|
426
552
|
}
|
|
427
553
|
(0, import_react5.useEffect)(() => {
|
|
554
|
+
if (action.disabled) {
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
428
557
|
setAction(idRef.current, action);
|
|
429
558
|
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
430
|
-
chatComponentsCache.current[action.name] = action.render;
|
|
559
|
+
chatComponentsCache.current.actions[action.name] = action.render;
|
|
431
560
|
}
|
|
432
561
|
return () => {
|
|
433
562
|
removeAction(idRef.current);
|
|
@@ -437,6 +566,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
437
566
|
removeAction,
|
|
438
567
|
action.description,
|
|
439
568
|
action.name,
|
|
569
|
+
action.disabled,
|
|
440
570
|
// This should be faster than deep equality checking
|
|
441
571
|
// In addition, all major JS engines guarantee the order of object keys
|
|
442
572
|
JSON.stringify(action.parameters),
|
|
@@ -447,12 +577,48 @@ function useCopilotAction(action, dependencies) {
|
|
|
447
577
|
]);
|
|
448
578
|
}
|
|
449
579
|
|
|
450
|
-
// src/hooks/use-
|
|
580
|
+
// src/hooks/use-coagent-action.ts
|
|
451
581
|
var import_react6 = require("react");
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
const
|
|
582
|
+
var import_shared4 = require("@copilotkit/shared");
|
|
583
|
+
function useCoAgentAction(action, dependencies) {
|
|
584
|
+
const { setCoagentAction, removeCoagentAction, coagentActions, chatComponentsCache } = (0, import_react6.useContext)(CopilotContext);
|
|
585
|
+
const idRef = (0, import_react6.useRef)((0, import_shared4.randomId)());
|
|
586
|
+
const key = `${action.name}-${action.nodeName || "global"}`;
|
|
587
|
+
if (dependencies === void 0) {
|
|
588
|
+
if (coagentActions[idRef.current]) {
|
|
589
|
+
coagentActions[idRef.current].handler = action.handler;
|
|
590
|
+
if (typeof action.render === "function") {
|
|
591
|
+
if (chatComponentsCache.current !== null) {
|
|
592
|
+
chatComponentsCache.current.coagentActions[key] = action.render;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
}
|
|
455
597
|
(0, import_react6.useEffect)(() => {
|
|
598
|
+
setCoagentAction(idRef.current, action);
|
|
599
|
+
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
600
|
+
chatComponentsCache.current.coagentActions[key] = action.render;
|
|
601
|
+
}
|
|
602
|
+
return () => {
|
|
603
|
+
removeCoagentAction(idRef.current);
|
|
604
|
+
};
|
|
605
|
+
}, [
|
|
606
|
+
setCoagentAction,
|
|
607
|
+
removeCoagentAction,
|
|
608
|
+
action.name,
|
|
609
|
+
// include render only if it's a string
|
|
610
|
+
typeof action.render === "string" ? action.render : void 0,
|
|
611
|
+
// dependencies set by the developer
|
|
612
|
+
...dependencies || []
|
|
613
|
+
]);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
// src/hooks/use-make-copilot-document-readable.ts
|
|
617
|
+
var import_react7 = require("react");
|
|
618
|
+
function useMakeCopilotDocumentReadable(document, categories, dependencies = []) {
|
|
619
|
+
const { addDocumentContext, removeDocumentContext } = useCopilotContext();
|
|
620
|
+
const idRef = (0, import_react7.useRef)();
|
|
621
|
+
(0, import_react7.useEffect)(() => {
|
|
456
622
|
const id = addDocumentContext(document, categories);
|
|
457
623
|
idRef.current = id;
|
|
458
624
|
return () => {
|
|
@@ -463,16 +629,16 @@ function useMakeCopilotDocumentReadable(document, categories, dependencies = [])
|
|
|
463
629
|
}
|
|
464
630
|
|
|
465
631
|
// src/hooks/use-copilot-readable.ts
|
|
466
|
-
var
|
|
632
|
+
var import_react8 = require("react");
|
|
467
633
|
function convertToJSON(description, value) {
|
|
468
634
|
return `${description}: ${typeof value === "string" ? value : JSON.stringify(value)}`;
|
|
469
635
|
}
|
|
470
636
|
function useCopilotReadable({ description, value, parentId, categories, convert }, dependencies) {
|
|
471
|
-
const { addContext, removeContext } = (
|
|
472
|
-
const idRef = (0,
|
|
637
|
+
const { addContext, removeContext } = useCopilotContext();
|
|
638
|
+
const idRef = (0, import_react8.useRef)();
|
|
473
639
|
convert = convert || convertToJSON;
|
|
474
640
|
const information = convert(description, value);
|
|
475
|
-
(0,
|
|
641
|
+
(0, import_react8.useEffect)(() => {
|
|
476
642
|
const id = addContext(information, parentId, categories);
|
|
477
643
|
idRef.current = id;
|
|
478
644
|
return () => {
|
|
@@ -481,8 +647,85 @@ function useCopilotReadable({ description, value, parentId, categories, convert
|
|
|
481
647
|
}, [information, parentId, addContext, removeContext, ...dependencies || []]);
|
|
482
648
|
return idRef.current;
|
|
483
649
|
}
|
|
650
|
+
|
|
651
|
+
// src/hooks/use-coagent.ts
|
|
652
|
+
var import_react9 = require("react");
|
|
653
|
+
function useCoAgent(options) {
|
|
654
|
+
const isExternalStateManagement = (options2) => {
|
|
655
|
+
return "state" in options2 && "setState" in options2;
|
|
656
|
+
};
|
|
657
|
+
const { name } = options;
|
|
658
|
+
const isInternalStateManagementWithInitial = (options2) => {
|
|
659
|
+
return "initialState" in options2;
|
|
660
|
+
};
|
|
661
|
+
const { coagentStates, setCoagentStates } = useCopilotContext();
|
|
662
|
+
const getCoagentState = (coagentStates2, name2) => {
|
|
663
|
+
if (coagentStates2[name2]) {
|
|
664
|
+
return coagentStates2[name2];
|
|
665
|
+
} else {
|
|
666
|
+
return {
|
|
667
|
+
name: name2,
|
|
668
|
+
state: isInternalStateManagementWithInitial(options) ? options.initialState : {},
|
|
669
|
+
running: false,
|
|
670
|
+
active: false,
|
|
671
|
+
threadId: void 0,
|
|
672
|
+
nodeName: void 0,
|
|
673
|
+
runId: void 0
|
|
674
|
+
};
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
const setState = (newState) => {
|
|
678
|
+
setCoagentStates((prevAgentStates) => {
|
|
679
|
+
let coagentState2 = getCoagentState(prevAgentStates, name);
|
|
680
|
+
const updatedState = typeof newState === "function" ? newState(coagentState2.state) : newState;
|
|
681
|
+
return __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
682
|
+
[name]: __spreadProps(__spreadValues({}, coagentState2), {
|
|
683
|
+
state: updatedState
|
|
684
|
+
})
|
|
685
|
+
});
|
|
686
|
+
});
|
|
687
|
+
};
|
|
688
|
+
const coagentState = getCoagentState(coagentStates, name);
|
|
689
|
+
const state = isExternalStateManagement(options) ? options.state : coagentState.state;
|
|
690
|
+
(0, import_react9.useEffect)(() => {
|
|
691
|
+
if (isExternalStateManagement(options)) {
|
|
692
|
+
setState(options.state);
|
|
693
|
+
} else if (coagentStates[name] === void 0) {
|
|
694
|
+
setState(options.initialState === void 0 ? {} : options.initialState);
|
|
695
|
+
}
|
|
696
|
+
}, [isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0]);
|
|
697
|
+
return {
|
|
698
|
+
name,
|
|
699
|
+
nodeName: coagentState.nodeName,
|
|
700
|
+
state,
|
|
701
|
+
setState,
|
|
702
|
+
running: coagentState.running,
|
|
703
|
+
start: () => {
|
|
704
|
+
startAgent(name);
|
|
705
|
+
},
|
|
706
|
+
stop: () => {
|
|
707
|
+
stopAgent(name);
|
|
708
|
+
}
|
|
709
|
+
};
|
|
710
|
+
}
|
|
711
|
+
function startAgent(name) {
|
|
712
|
+
const { setAgentSession } = useCopilotContext();
|
|
713
|
+
setAgentSession({
|
|
714
|
+
agentName: name
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
function stopAgent(name) {
|
|
718
|
+
const { agentSession, setAgentSession } = useCopilotContext();
|
|
719
|
+
if (agentSession && agentSession.agentName === name) {
|
|
720
|
+
setAgentSession(null);
|
|
721
|
+
} else {
|
|
722
|
+
console.warn(`No agent session found for ${name}`);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
484
725
|
// Annotate the CommonJS export names for ESM import in node:
|
|
485
726
|
0 && (module.exports = {
|
|
727
|
+
useCoAgent,
|
|
728
|
+
useCoAgentAction,
|
|
486
729
|
useCopilotAction,
|
|
487
730
|
useCopilotChat,
|
|
488
731
|
useCopilotReadable,
|