@copilotkit/react-core 1.2.0 → 1.2.2-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 +45 -0
- package/dist/{chunk-QR4XKQOA.mjs → chunk-326LK7LX.mjs} +2 -2
- package/dist/{chunk-EVK5346D.mjs → chunk-3U22BXKC.mjs} +4 -3
- package/dist/chunk-3U22BXKC.mjs.map +1 -0
- package/dist/{chunk-GSL4ZHUB.mjs → chunk-A37GANOW.mjs} +4 -4
- package/dist/chunk-A37GANOW.mjs.map +1 -0
- package/dist/chunk-BWYAGPEF.mjs +1 -0
- package/dist/{chunk-LB53QWLY.mjs → chunk-CODXG6KU.mjs} +2 -2
- package/dist/{chunk-LB7ULLSU.mjs → chunk-J2YXDQHR.mjs} +14 -3
- 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-CFCRJA63.mjs → chunk-RBNULK3U.mjs} +39 -5
- package/dist/chunk-RBNULK3U.mjs.map +1 -0
- package/dist/{chunk-PZOEBYWP.mjs → chunk-RWPGGPW5.mjs} +63 -11
- package/dist/chunk-RWPGGPW5.mjs.map +1 -0
- package/dist/chunk-STUXJ3BN.mjs +44 -0
- package/dist/chunk-STUXJ3BN.mjs.map +1 -0
- package/dist/{chunk-HUC6EZVP.mjs → chunk-TCZBK3HZ.mjs} +7 -4
- package/dist/chunk-TCZBK3HZ.mjs.map +1 -0
- package/dist/{chunk-AIW2RAN2.mjs → chunk-VCEOT4GG.mjs} +35 -6
- package/dist/chunk-VCEOT4GG.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.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.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.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 +13 -2
- 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 +13 -2
- 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 +224 -21
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +17 -8
- package/dist/hooks/use-chat.d.ts +26 -4
- package/dist/hooks/use-chat.js +58 -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 +15 -4
- 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 +101 -13
- 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 +13 -2
- 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 +13 -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 +4 -0
- package/dist/index.js +268 -31
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +20 -12
- package/dist/lib/copilot-task.d.ts +2 -0
- package/dist/lib/copilot-task.js +1 -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 +1 -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/utils/extract.d.ts +2 -0
- package/dist/utils/extract.js +1 -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 +1 -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 +39 -2
- package/src/hooks/index.ts +2 -0
- package/src/hooks/use-chat.ts +111 -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 +2 -2
- package/src/hooks/use-copilot-chat.ts +31 -0
- package/src/lib/copilot-task.ts +2 -1
- package/src/types/coagent-action.ts +17 -0
- package/src/types/coagent-state.ts +9 -0
- package/src/utils/extract.ts +7 -2
- package/dist/chunk-5JB4B2SV.mjs +0 -1
- package/dist/chunk-AIW2RAN2.mjs.map +0 -1
- package/dist/chunk-CFCRJA63.mjs.map +0 -1
- package/dist/chunk-EVK5346D.mjs.map +0 -1
- package/dist/chunk-GSL4ZHUB.mjs.map +0 -1
- package/dist/chunk-HUC6EZVP.mjs.map +0 -1
- package/dist/chunk-LB7ULLSU.mjs.map +0 -1
- package/dist/chunk-PZOEBYWP.mjs.map +0 -1
- /package/dist/{chunk-QR4XKQOA.mjs.map → chunk-326LK7LX.mjs.map} +0 -0
- /package/dist/{chunk-5JB4B2SV.mjs.map → chunk-BWYAGPEF.mjs.map} +0 -0
- /package/dist/{chunk-LB53QWLY.mjs.map → chunk-CODXG6KU.mjs.map} +0 -0
package/dist/index.d.ts
CHANGED
|
@@ -3,9 +3,11 @@ export { CopilotKitProps } from './components/copilot-provider/copilotkit-props.
|
|
|
3
3
|
export { CopilotApiConfig, CopilotContext, CopilotContextParams, useCopilotContext } from './context/copilot-context.js';
|
|
4
4
|
export { UseCopilotChatOptions, UseCopilotChatReturn, useCopilotChat } from './hooks/use-copilot-chat.js';
|
|
5
5
|
export { useCopilotAction } from './hooks/use-copilot-action.js';
|
|
6
|
+
export { useCoAgentAction } from './hooks/use-coagent-action.js';
|
|
6
7
|
export { useMakeCopilotDocumentReadable } from './hooks/use-make-copilot-document-readable.js';
|
|
7
8
|
export { UseChatHelpers } from './hooks/use-chat.js';
|
|
8
9
|
export { useCopilotReadable } from './hooks/use-copilot-readable.js';
|
|
10
|
+
export { useCoAgent } from './hooks/use-coagent.js';
|
|
9
11
|
export { DocumentPointer } from './types/document-pointer.js';
|
|
10
12
|
export { SystemMessageFunction } from './types/system-message.js';
|
|
11
13
|
export { ActionRenderProps, RenderFunctionStatus } from './types/frontend-action.js';
|
|
@@ -17,3 +19,5 @@ import 'react';
|
|
|
17
19
|
import '@copilotkit/shared';
|
|
18
20
|
import '@copilotkit/runtime-client-gql';
|
|
19
21
|
import './hooks/use-tree.js';
|
|
22
|
+
import './types/coagent-action.js';
|
|
23
|
+
import './types/coagent-state.js';
|
package/dist/index.js
CHANGED
|
@@ -86,6 +86,8 @@ __export(src_exports, {
|
|
|
86
86
|
CopilotTask: () => CopilotTask,
|
|
87
87
|
defaultCopilotContextCategories: () => defaultCopilotContextCategories,
|
|
88
88
|
extract: () => extract,
|
|
89
|
+
useCoAgent: () => useCoAgent,
|
|
90
|
+
useCoAgentAction: () => useCoAgentAction,
|
|
89
91
|
useCopilotAction: () => useCopilotAction,
|
|
90
92
|
useCopilotChat: () => useCopilotChat,
|
|
91
93
|
useCopilotContext: () => useCopilotContext,
|
|
@@ -105,7 +107,12 @@ var emptyCopilotContext = {
|
|
|
105
107
|
},
|
|
106
108
|
removeAction: () => {
|
|
107
109
|
},
|
|
108
|
-
|
|
110
|
+
coagentActions: {},
|
|
111
|
+
setCoagentAction: () => {
|
|
112
|
+
},
|
|
113
|
+
removeCoagentAction: () => {
|
|
114
|
+
},
|
|
115
|
+
chatComponentsCache: { current: { actions: {}, coagentActions: {} } },
|
|
109
116
|
getContextString: (documents, categories) => returnAndThrowInDebug(""),
|
|
110
117
|
addContext: () => "",
|
|
111
118
|
removeContext: () => {
|
|
@@ -138,7 +145,13 @@ var emptyCopilotContext = {
|
|
|
138
145
|
},
|
|
139
146
|
removeChatSuggestionConfiguration: () => {
|
|
140
147
|
},
|
|
141
|
-
showDevConsole: "auto"
|
|
148
|
+
showDevConsole: "auto",
|
|
149
|
+
coagentStates: {},
|
|
150
|
+
setCoagentStates: () => {
|
|
151
|
+
},
|
|
152
|
+
agentSession: null,
|
|
153
|
+
setAgentSession: () => {
|
|
154
|
+
}
|
|
142
155
|
};
|
|
143
156
|
var CopilotContext = import_react.default.createContext(emptyCopilotContext);
|
|
144
157
|
function useCopilotContext() {
|
|
@@ -366,7 +379,11 @@ function CopilotKit(_a) {
|
|
|
366
379
|
}
|
|
367
380
|
const chatApiEndpoint = props.runtimeUrl || import_shared3.COPILOT_CLOUD_CHAT_URL;
|
|
368
381
|
const [actions, setActions] = (0, import_react4.useState)({});
|
|
369
|
-
const
|
|
382
|
+
const [coagentActions, setCoagentActions] = (0, import_react4.useState)({});
|
|
383
|
+
const chatComponentsCache = (0, import_react4.useRef)({
|
|
384
|
+
actions: {},
|
|
385
|
+
coagentActions: {}
|
|
386
|
+
});
|
|
370
387
|
const { addElement, removeElement, printTree } = use_tree_default();
|
|
371
388
|
const [messages, setMessages] = (0, import_react4.useState)([]);
|
|
372
389
|
const [isLoading, setIsLoading] = (0, import_react4.useState)(false);
|
|
@@ -390,6 +407,20 @@ function CopilotKit(_a) {
|
|
|
390
407
|
return newPoints;
|
|
391
408
|
});
|
|
392
409
|
}, []);
|
|
410
|
+
const setCoagentAction = (0, import_react4.useCallback)((id, action) => {
|
|
411
|
+
setCoagentActions((prevPoints) => {
|
|
412
|
+
return __spreadProps(__spreadValues({}, prevPoints), {
|
|
413
|
+
[id]: action
|
|
414
|
+
});
|
|
415
|
+
});
|
|
416
|
+
}, []);
|
|
417
|
+
const removeCoagentAction = (0, import_react4.useCallback)((id) => {
|
|
418
|
+
setCoagentActions((prevPoints) => {
|
|
419
|
+
const newPoints = __spreadValues({}, prevPoints);
|
|
420
|
+
delete newPoints[id];
|
|
421
|
+
return newPoints;
|
|
422
|
+
});
|
|
423
|
+
}, []);
|
|
393
424
|
const getContextString = (0, import_react4.useCallback)(
|
|
394
425
|
(documents, categories) => {
|
|
395
426
|
const documentsString = documents.map((document) => {
|
|
@@ -480,6 +511,14 @@ ${nonDocumentStrings}`;
|
|
|
480
511
|
return rest;
|
|
481
512
|
});
|
|
482
513
|
};
|
|
514
|
+
const [coagentStates, setCoagentStates] = (0, import_react4.useState)({});
|
|
515
|
+
let initialAgentSession = null;
|
|
516
|
+
if (props.agent) {
|
|
517
|
+
initialAgentSession = {
|
|
518
|
+
agentName: props.agent
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
const [agentSession, setAgentSession] = (0, import_react4.useState)(initialAgentSession);
|
|
483
522
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
484
523
|
CopilotContext.Provider,
|
|
485
524
|
{
|
|
@@ -489,6 +528,9 @@ ${nonDocumentStrings}`;
|
|
|
489
528
|
getFunctionCallHandler,
|
|
490
529
|
setAction,
|
|
491
530
|
removeAction,
|
|
531
|
+
coagentActions,
|
|
532
|
+
setCoagentAction,
|
|
533
|
+
removeCoagentAction,
|
|
492
534
|
getContextString,
|
|
493
535
|
addContext,
|
|
494
536
|
removeContext,
|
|
@@ -505,7 +547,11 @@ ${nonDocumentStrings}`;
|
|
|
505
547
|
removeChatSuggestionConfiguration,
|
|
506
548
|
chatInstructions,
|
|
507
549
|
setChatInstructions,
|
|
508
|
-
showDevConsole: props.showDevConsole === void 0 ? "auto" : props.showDevConsole
|
|
550
|
+
showDevConsole: props.showDevConsole === void 0 ? "auto" : props.showDevConsole,
|
|
551
|
+
coagentStates,
|
|
552
|
+
setCoagentStates,
|
|
553
|
+
agentSession,
|
|
554
|
+
setAgentSession
|
|
509
555
|
},
|
|
510
556
|
children
|
|
511
557
|
}
|
|
@@ -523,8 +569,9 @@ function entryPointsToFunctionCallHandler(actions) {
|
|
|
523
569
|
if (action) {
|
|
524
570
|
yield new Promise((resolve, reject) => {
|
|
525
571
|
(0, import_react_dom.flushSync)(() => __async(this, null, function* () {
|
|
572
|
+
var _a;
|
|
526
573
|
try {
|
|
527
|
-
result = yield action.handler(args);
|
|
574
|
+
result = yield (_a = action.handler) == null ? void 0 : _a.call(action, args);
|
|
528
575
|
resolve();
|
|
529
576
|
} catch (error) {
|
|
530
577
|
reject(error);
|
|
@@ -555,7 +602,12 @@ function useChat(options) {
|
|
|
555
602
|
initialMessages,
|
|
556
603
|
isLoading,
|
|
557
604
|
actions,
|
|
558
|
-
onFunctionCall
|
|
605
|
+
onFunctionCall,
|
|
606
|
+
onCoagentAction,
|
|
607
|
+
setCoagentStates,
|
|
608
|
+
coagentStates,
|
|
609
|
+
agentSession,
|
|
610
|
+
setAgentSession
|
|
559
611
|
} = options;
|
|
560
612
|
const abortControllerRef = (0, import_react5.useRef)();
|
|
561
613
|
const threadIdRef = (0, import_react5.useRef)(null);
|
|
@@ -584,7 +636,7 @@ function useChat(options) {
|
|
|
584
636
|
const messagesWithContext = [systemMessage, ...initialMessages || [], ...previousMessages];
|
|
585
637
|
const stream = import_runtime_client_gql.CopilotRuntimeClient.asStream(
|
|
586
638
|
runtimeClient.generateCopilotResponse({
|
|
587
|
-
data: __spreadProps(__spreadValues({
|
|
639
|
+
data: __spreadProps(__spreadValues(__spreadProps(__spreadValues({
|
|
588
640
|
frontend: {
|
|
589
641
|
actions: actions.map((action) => ({
|
|
590
642
|
name: action.name,
|
|
@@ -595,7 +647,7 @@ function useChat(options) {
|
|
|
595
647
|
},
|
|
596
648
|
threadId: threadIdRef.current,
|
|
597
649
|
runId: runIdRef.current,
|
|
598
|
-
messages: (0, import_runtime_client_gql.convertMessagesToGqlInput)(messagesWithContext)
|
|
650
|
+
messages: (0, import_runtime_client_gql.convertMessagesToGqlInput)((0, import_runtime_client_gql.filterAgentStateMessages)(messagesWithContext))
|
|
599
651
|
}, copilotConfig.cloud ? {
|
|
600
652
|
cloud: __spreadValues({}, ((_c = (_b = (_a = copilotConfig.cloud.guardrails) == null ? void 0 : _a.input) == null ? void 0 : _b.restrictToTopic) == null ? void 0 : _c.enabled) ? {
|
|
601
653
|
guardrails: {
|
|
@@ -609,6 +661,13 @@ function useChat(options) {
|
|
|
609
661
|
metadata: {
|
|
610
662
|
requestType: import_runtime_client_gql.CopilotRequestType.Chat
|
|
611
663
|
}
|
|
664
|
+
}), agentSession ? {
|
|
665
|
+
agentSession
|
|
666
|
+
} : {}), {
|
|
667
|
+
agentStates: Object.values(coagentStates).map((state) => ({
|
|
668
|
+
agentName: state.name,
|
|
669
|
+
state: JSON.stringify(state.state)
|
|
670
|
+
}))
|
|
612
671
|
}),
|
|
613
672
|
properties: copilotConfig.properties,
|
|
614
673
|
signal: (_d = abortControllerRef.current) == null ? void 0 : _d.signal
|
|
@@ -616,7 +675,8 @@ function useChat(options) {
|
|
|
616
675
|
);
|
|
617
676
|
const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
|
|
618
677
|
const reader = stream.getReader();
|
|
619
|
-
let
|
|
678
|
+
let actionResults = {};
|
|
679
|
+
let executedCoagentActions = [];
|
|
620
680
|
try {
|
|
621
681
|
while (true) {
|
|
622
682
|
const { done, value } = yield reader.read();
|
|
@@ -628,7 +688,9 @@ function useChat(options) {
|
|
|
628
688
|
}
|
|
629
689
|
threadIdRef.current = value.generateCopilotResponse.threadId || null;
|
|
630
690
|
runIdRef.current = value.generateCopilotResponse.runId || null;
|
|
631
|
-
const messages2 = (0, import_runtime_client_gql.convertGqlOutputToMessages)(
|
|
691
|
+
const messages2 = (0, import_runtime_client_gql.convertGqlOutputToMessages)(
|
|
692
|
+
(0, import_runtime_client_gql.filterAdjacentAgentStateMessages)(value.generateCopilotResponse.messages)
|
|
693
|
+
);
|
|
632
694
|
if (messages2.length === 0) {
|
|
633
695
|
continue;
|
|
634
696
|
}
|
|
@@ -644,7 +706,7 @@ function useChat(options) {
|
|
|
644
706
|
for (const message of messages2) {
|
|
645
707
|
newMessages.push(message);
|
|
646
708
|
if (message instanceof import_runtime_client_gql.ActionExecutionMessage && message.status.code !== import_runtime_client_gql.MessageStatusCode.Pending && message.scope === "client" && onFunctionCall) {
|
|
647
|
-
if (!(message.id in
|
|
709
|
+
if (!(message.id in actionResults)) {
|
|
648
710
|
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
649
711
|
break;
|
|
650
712
|
}
|
|
@@ -653,16 +715,50 @@ function useChat(options) {
|
|
|
653
715
|
name: message.name,
|
|
654
716
|
args: message.arguments
|
|
655
717
|
});
|
|
656
|
-
|
|
718
|
+
actionResults[message.id] = result;
|
|
657
719
|
}
|
|
658
720
|
newMessages.push(
|
|
659
721
|
new import_runtime_client_gql.ResultMessage({
|
|
660
|
-
result: import_runtime_client_gql.ResultMessage.encodeResult(
|
|
722
|
+
result: import_runtime_client_gql.ResultMessage.encodeResult(actionResults[message.id]),
|
|
661
723
|
actionExecutionId: message.id,
|
|
662
724
|
actionName: message.name
|
|
663
725
|
})
|
|
664
726
|
);
|
|
665
727
|
}
|
|
728
|
+
if (message instanceof import_runtime_client_gql.AgentStateMessage && !message.active && !executedCoagentActions.includes(message.id) && onCoagentAction) {
|
|
729
|
+
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
730
|
+
break;
|
|
731
|
+
}
|
|
732
|
+
yield onCoagentAction({
|
|
733
|
+
name: message.agentName,
|
|
734
|
+
nodeName: message.nodeName,
|
|
735
|
+
state: message.state
|
|
736
|
+
});
|
|
737
|
+
executedCoagentActions.push(message.id);
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
const lastAgentStateMessage = [...messages2].reverse().find((message) => message instanceof import_runtime_client_gql.AgentStateMessage);
|
|
741
|
+
if (lastAgentStateMessage) {
|
|
742
|
+
if (lastAgentStateMessage.running) {
|
|
743
|
+
setCoagentStates((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
744
|
+
[lastAgentStateMessage.agentName]: {
|
|
745
|
+
name: lastAgentStateMessage.agentName,
|
|
746
|
+
state: lastAgentStateMessage.state,
|
|
747
|
+
running: lastAgentStateMessage.running,
|
|
748
|
+
active: lastAgentStateMessage.active,
|
|
749
|
+
threadId: lastAgentStateMessage.threadId,
|
|
750
|
+
nodeName: lastAgentStateMessage.nodeName,
|
|
751
|
+
runId: lastAgentStateMessage.runId
|
|
752
|
+
}
|
|
753
|
+
}));
|
|
754
|
+
setAgentSession({
|
|
755
|
+
threadId: lastAgentStateMessage.threadId,
|
|
756
|
+
agentName: lastAgentStateMessage.agentName,
|
|
757
|
+
nodeName: lastAgentStateMessage.nodeName
|
|
758
|
+
});
|
|
759
|
+
} else {
|
|
760
|
+
setAgentSession(null);
|
|
761
|
+
}
|
|
666
762
|
}
|
|
667
763
|
}
|
|
668
764
|
if (newMessages.length > 0) {
|
|
@@ -671,7 +767,7 @@ function useChat(options) {
|
|
|
671
767
|
}
|
|
672
768
|
if (
|
|
673
769
|
// if we have client side results
|
|
674
|
-
Object.values(
|
|
770
|
+
Object.values(actionResults).length || // or the last message we received is a result
|
|
675
771
|
newMessages.length && newMessages[newMessages.length - 1] instanceof import_runtime_client_gql.ResultMessage
|
|
676
772
|
) {
|
|
677
773
|
yield new Promise((resolve) => setTimeout(resolve, 10));
|
|
@@ -733,7 +829,12 @@ function useCopilotChat(_a = {}) {
|
|
|
733
829
|
isLoading,
|
|
734
830
|
setIsLoading,
|
|
735
831
|
chatInstructions,
|
|
736
|
-
actions
|
|
832
|
+
actions,
|
|
833
|
+
coagentStates,
|
|
834
|
+
setCoagentStates,
|
|
835
|
+
coagentActions,
|
|
836
|
+
agentSession,
|
|
837
|
+
setAgentSession
|
|
737
838
|
} = useCopilotContext();
|
|
738
839
|
const latestGetContextString = useUpdatedRef(getContextString);
|
|
739
840
|
const deleteMessage = (0, import_react6.useCallback)(
|
|
@@ -750,16 +851,39 @@ function useCopilotChat(_a = {}) {
|
|
|
750
851
|
role: import_runtime_client_gql2.Role.System
|
|
751
852
|
});
|
|
752
853
|
}, [getContextString, makeSystemMessage2, chatInstructions]);
|
|
854
|
+
const onCoagentAction = (0, import_react6.useCallback)(
|
|
855
|
+
(args) => __async(this, null, function* () {
|
|
856
|
+
var _a2;
|
|
857
|
+
const { name, nodeName, state } = args;
|
|
858
|
+
let action = Object.values(coagentActions).find(
|
|
859
|
+
(action2) => action2.name === name && action2.nodeName === nodeName
|
|
860
|
+
);
|
|
861
|
+
if (!action) {
|
|
862
|
+
action = Object.values(coagentActions).find(
|
|
863
|
+
(action2) => action2.name === name && !action2.nodeName
|
|
864
|
+
);
|
|
865
|
+
}
|
|
866
|
+
if (action) {
|
|
867
|
+
yield (_a2 = action.handler) == null ? void 0 : _a2.call(action, { state, nodeName });
|
|
868
|
+
}
|
|
869
|
+
}),
|
|
870
|
+
[coagentActions]
|
|
871
|
+
);
|
|
753
872
|
const { append, reload, stop } = useChat(__spreadProps(__spreadValues({}, options), {
|
|
754
873
|
actions: Object.values(actions),
|
|
755
874
|
copilotConfig: copilotApiConfig,
|
|
756
875
|
initialMessages: options.initialMessages || [],
|
|
757
876
|
onFunctionCall: getFunctionCallHandler(),
|
|
877
|
+
onCoagentAction,
|
|
758
878
|
messages,
|
|
759
879
|
setMessages,
|
|
760
880
|
makeSystemMessageCallback,
|
|
761
881
|
isLoading,
|
|
762
|
-
setIsLoading
|
|
882
|
+
setIsLoading,
|
|
883
|
+
coagentStates,
|
|
884
|
+
setCoagentStates,
|
|
885
|
+
agentSession,
|
|
886
|
+
setAgentSession
|
|
763
887
|
}));
|
|
764
888
|
const latestAppend = useUpdatedRef(append);
|
|
765
889
|
const latestAppendFunc = (0, import_react6.useCallback)(
|
|
@@ -842,7 +966,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
842
966
|
actions[idRef.current].handler = action.handler;
|
|
843
967
|
if (typeof action.render === "function") {
|
|
844
968
|
if (chatComponentsCache.current !== null) {
|
|
845
|
-
chatComponentsCache.current[action.name] = action.render;
|
|
969
|
+
chatComponentsCache.current.actions[action.name] = action.render;
|
|
846
970
|
}
|
|
847
971
|
}
|
|
848
972
|
}
|
|
@@ -853,7 +977,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
853
977
|
}
|
|
854
978
|
setAction(idRef.current, action);
|
|
855
979
|
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
856
|
-
chatComponentsCache.current[action.name] = action.render;
|
|
980
|
+
chatComponentsCache.current.actions[action.name] = action.render;
|
|
857
981
|
}
|
|
858
982
|
return () => {
|
|
859
983
|
removeAction(idRef.current);
|
|
@@ -874,12 +998,48 @@ function useCopilotAction(action, dependencies) {
|
|
|
874
998
|
]);
|
|
875
999
|
}
|
|
876
1000
|
|
|
877
|
-
// src/hooks/use-
|
|
1001
|
+
// src/hooks/use-coagent-action.ts
|
|
878
1002
|
var import_react8 = require("react");
|
|
1003
|
+
var import_shared6 = require("@copilotkit/shared");
|
|
1004
|
+
function useCoAgentAction(action, dependencies) {
|
|
1005
|
+
const { setCoagentAction, removeCoagentAction, coagentActions, chatComponentsCache } = (0, import_react8.useContext)(CopilotContext);
|
|
1006
|
+
const idRef = (0, import_react8.useRef)((0, import_shared6.randomId)());
|
|
1007
|
+
const key = `${action.name}-${action.nodeName || "global"}`;
|
|
1008
|
+
if (dependencies === void 0) {
|
|
1009
|
+
if (coagentActions[idRef.current]) {
|
|
1010
|
+
coagentActions[idRef.current].handler = action.handler;
|
|
1011
|
+
if (typeof action.render === "function") {
|
|
1012
|
+
if (chatComponentsCache.current !== null) {
|
|
1013
|
+
chatComponentsCache.current.coagentActions[key] = action.render;
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
(0, import_react8.useEffect)(() => {
|
|
1019
|
+
setCoagentAction(idRef.current, action);
|
|
1020
|
+
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
1021
|
+
chatComponentsCache.current.coagentActions[key] = action.render;
|
|
1022
|
+
}
|
|
1023
|
+
return () => {
|
|
1024
|
+
removeCoagentAction(idRef.current);
|
|
1025
|
+
};
|
|
1026
|
+
}, [
|
|
1027
|
+
setCoagentAction,
|
|
1028
|
+
removeCoagentAction,
|
|
1029
|
+
action.name,
|
|
1030
|
+
// include render only if it's a string
|
|
1031
|
+
typeof action.render === "string" ? action.render : void 0,
|
|
1032
|
+
// dependencies set by the developer
|
|
1033
|
+
...dependencies || []
|
|
1034
|
+
]);
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
// src/hooks/use-make-copilot-document-readable.ts
|
|
1038
|
+
var import_react9 = require("react");
|
|
879
1039
|
function useMakeCopilotDocumentReadable(document, categories, dependencies = []) {
|
|
880
1040
|
const { addDocumentContext, removeDocumentContext } = useCopilotContext();
|
|
881
|
-
const idRef = (0,
|
|
882
|
-
(0,
|
|
1041
|
+
const idRef = (0, import_react9.useRef)();
|
|
1042
|
+
(0, import_react9.useEffect)(() => {
|
|
883
1043
|
const id = addDocumentContext(document, categories);
|
|
884
1044
|
idRef.current = id;
|
|
885
1045
|
return () => {
|
|
@@ -890,16 +1050,16 @@ function useMakeCopilotDocumentReadable(document, categories, dependencies = [])
|
|
|
890
1050
|
}
|
|
891
1051
|
|
|
892
1052
|
// src/hooks/use-copilot-readable.ts
|
|
893
|
-
var
|
|
1053
|
+
var import_react10 = require("react");
|
|
894
1054
|
function convertToJSON(description, value) {
|
|
895
1055
|
return `${description}: ${typeof value === "string" ? value : JSON.stringify(value)}`;
|
|
896
1056
|
}
|
|
897
1057
|
function useCopilotReadable({ description, value, parentId, categories, convert }, dependencies) {
|
|
898
1058
|
const { addContext, removeContext } = useCopilotContext();
|
|
899
|
-
const idRef = (0,
|
|
1059
|
+
const idRef = (0, import_react10.useRef)();
|
|
900
1060
|
convert = convert || convertToJSON;
|
|
901
1061
|
const information = convert(description, value);
|
|
902
|
-
(0,
|
|
1062
|
+
(0, import_react10.useEffect)(() => {
|
|
903
1063
|
const id = addContext(information, parentId, categories);
|
|
904
1064
|
idRef.current = id;
|
|
905
1065
|
return () => {
|
|
@@ -909,9 +1069,84 @@ function useCopilotReadable({ description, value, parentId, categories, convert
|
|
|
909
1069
|
return idRef.current;
|
|
910
1070
|
}
|
|
911
1071
|
|
|
1072
|
+
// src/hooks/use-coagent.ts
|
|
1073
|
+
var import_react11 = require("react");
|
|
1074
|
+
function useCoAgent(options) {
|
|
1075
|
+
const isExternalStateManagement = (options2) => {
|
|
1076
|
+
return "state" in options2 && "setState" in options2;
|
|
1077
|
+
};
|
|
1078
|
+
const { name } = options;
|
|
1079
|
+
const isInternalStateManagementWithInitial = (options2) => {
|
|
1080
|
+
return "initialState" in options2;
|
|
1081
|
+
};
|
|
1082
|
+
const { coagentStates, setCoagentStates } = useCopilotContext();
|
|
1083
|
+
const getCoagentState = (coagentStates2, name2) => {
|
|
1084
|
+
if (coagentStates2[name2]) {
|
|
1085
|
+
return coagentStates2[name2];
|
|
1086
|
+
} else {
|
|
1087
|
+
return {
|
|
1088
|
+
name: name2,
|
|
1089
|
+
state: isInternalStateManagementWithInitial(options) ? options.initialState : {},
|
|
1090
|
+
running: false,
|
|
1091
|
+
active: false,
|
|
1092
|
+
threadId: void 0,
|
|
1093
|
+
nodeName: void 0,
|
|
1094
|
+
runId: void 0
|
|
1095
|
+
};
|
|
1096
|
+
}
|
|
1097
|
+
};
|
|
1098
|
+
const setState = (newState) => {
|
|
1099
|
+
setCoagentStates((prevAgentStates) => {
|
|
1100
|
+
let coagentState2 = getCoagentState(prevAgentStates, name);
|
|
1101
|
+
const updatedState = typeof newState === "function" ? newState(coagentState2.state) : newState;
|
|
1102
|
+
return __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
1103
|
+
[name]: __spreadProps(__spreadValues({}, coagentState2), {
|
|
1104
|
+
state: updatedState
|
|
1105
|
+
})
|
|
1106
|
+
});
|
|
1107
|
+
});
|
|
1108
|
+
};
|
|
1109
|
+
const coagentState = getCoagentState(coagentStates, name);
|
|
1110
|
+
const state = isExternalStateManagement(options) ? options.state : coagentState.state;
|
|
1111
|
+
(0, import_react11.useEffect)(() => {
|
|
1112
|
+
if (isExternalStateManagement(options)) {
|
|
1113
|
+
setState(options.state);
|
|
1114
|
+
} else if (coagentStates[name] === void 0) {
|
|
1115
|
+
setState(options.initialState === void 0 ? {} : options.initialState);
|
|
1116
|
+
}
|
|
1117
|
+
}, [isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0]);
|
|
1118
|
+
return {
|
|
1119
|
+
name,
|
|
1120
|
+
nodeName: coagentState.nodeName,
|
|
1121
|
+
state,
|
|
1122
|
+
setState,
|
|
1123
|
+
running: coagentState.running,
|
|
1124
|
+
start: () => {
|
|
1125
|
+
startAgent(name);
|
|
1126
|
+
},
|
|
1127
|
+
stop: () => {
|
|
1128
|
+
stopAgent(name);
|
|
1129
|
+
}
|
|
1130
|
+
};
|
|
1131
|
+
}
|
|
1132
|
+
function startAgent(name) {
|
|
1133
|
+
const { setAgentSession } = useCopilotContext();
|
|
1134
|
+
setAgentSession({
|
|
1135
|
+
agentName: name
|
|
1136
|
+
});
|
|
1137
|
+
}
|
|
1138
|
+
function stopAgent(name) {
|
|
1139
|
+
const { agentSession, setAgentSession } = useCopilotContext();
|
|
1140
|
+
if (agentSession && agentSession.agentName === name) {
|
|
1141
|
+
setAgentSession(null);
|
|
1142
|
+
} else {
|
|
1143
|
+
console.warn(`No agent session found for ${name}`);
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
|
|
912
1147
|
// src/lib/copilot-task.ts
|
|
913
1148
|
var import_runtime_client_gql3 = require("@copilotkit/runtime-client-gql");
|
|
914
|
-
var
|
|
1149
|
+
var import_shared7 = require("@copilotkit/shared");
|
|
915
1150
|
var CopilotTask = class {
|
|
916
1151
|
constructor(config) {
|
|
917
1152
|
this.instructions = config.instructions;
|
|
@@ -955,11 +1190,11 @@ var CopilotTask = class {
|
|
|
955
1190
|
actions: Object.values(actions).map((action) => ({
|
|
956
1191
|
name: action.name,
|
|
957
1192
|
description: action.description || "",
|
|
958
|
-
jsonSchema: JSON.stringify((0,
|
|
1193
|
+
jsonSchema: JSON.stringify((0, import_shared7.actionParametersToJsonSchema)(action.parameters || []))
|
|
959
1194
|
})),
|
|
960
1195
|
url: window.location.href
|
|
961
1196
|
},
|
|
962
|
-
messages: (0, import_runtime_client_gql3.convertMessagesToGqlInput)(messages),
|
|
1197
|
+
messages: (0, import_runtime_client_gql3.convertMessagesToGqlInput)((0, import_runtime_client_gql3.filterAgentStateMessages)(messages)),
|
|
963
1198
|
metadata: {
|
|
964
1199
|
requestType: import_runtime_client_gql3.CopilotRequestType.Task
|
|
965
1200
|
},
|
|
@@ -1010,7 +1245,7 @@ ${instructions}
|
|
|
1010
1245
|
}
|
|
1011
1246
|
|
|
1012
1247
|
// src/utils/extract.ts
|
|
1013
|
-
var
|
|
1248
|
+
var import_shared8 = require("@copilotkit/shared");
|
|
1014
1249
|
var import_runtime_client_gql4 = require("@copilotkit/runtime-client-gql");
|
|
1015
1250
|
var import_runtime_client_gql5 = require("@copilotkit/runtime-client-gql");
|
|
1016
1251
|
var import_runtime_client_gql6 = require("@copilotkit/runtime-client-gql");
|
|
@@ -1046,7 +1281,7 @@ function extract(_0) {
|
|
|
1046
1281
|
content: makeSystemMessage(contextString, instructions),
|
|
1047
1282
|
role: import_runtime_client_gql4.Role.System
|
|
1048
1283
|
});
|
|
1049
|
-
const headers = __spreadValues(__spreadValues({}, context.copilotApiConfig.headers || {}), context.copilotApiConfig.publicApiKey ? { [
|
|
1284
|
+
const headers = __spreadValues(__spreadValues({}, context.copilotApiConfig.headers || {}), context.copilotApiConfig.publicApiKey ? { [import_shared8.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: context.copilotApiConfig.publicApiKey } : {});
|
|
1050
1285
|
const runtimeClient = new import_runtime_client_gql5.CopilotRuntimeClient({
|
|
1051
1286
|
url: context.copilotApiConfig.chatApiEndpoint,
|
|
1052
1287
|
publicApiKey: context.copilotApiConfig.publicApiKey,
|
|
@@ -1061,13 +1296,13 @@ function extract(_0) {
|
|
|
1061
1296
|
{
|
|
1062
1297
|
name: action.name,
|
|
1063
1298
|
description: action.description || "",
|
|
1064
|
-
jsonSchema: JSON.stringify((0,
|
|
1299
|
+
jsonSchema: JSON.stringify((0, import_shared8.actionParametersToJsonSchema)(action.parameters || []))
|
|
1065
1300
|
}
|
|
1066
1301
|
],
|
|
1067
1302
|
url: window.location.href
|
|
1068
1303
|
},
|
|
1069
1304
|
messages: (0, import_runtime_client_gql6.convertMessagesToGqlInput)(
|
|
1070
|
-
includeMessages ? [systemMessage, ...messages] : [systemMessage]
|
|
1305
|
+
includeMessages ? [systemMessage, ...(0, import_runtime_client_gql6.filterAgentStateMessages)(messages)] : [systemMessage]
|
|
1071
1306
|
),
|
|
1072
1307
|
metadata: {
|
|
1073
1308
|
requestType
|
|
@@ -1148,6 +1383,8 @@ Any additional messages provided are for providing context only and should not b
|
|
|
1148
1383
|
CopilotTask,
|
|
1149
1384
|
defaultCopilotContextCategories,
|
|
1150
1385
|
extract,
|
|
1386
|
+
useCoAgent,
|
|
1387
|
+
useCoAgentAction,
|
|
1151
1388
|
useCopilotAction,
|
|
1152
1389
|
useCopilotChat,
|
|
1153
1390
|
useCopilotContext,
|