@copilotkit/react-core 1.1.1-feat-runtime-remote-actions.4 → 1.1.1-feat-runtime-remote-actions.5
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 +9 -0
- package/dist/{chunk-2MQX7BJL.mjs → chunk-6GOM3DN7.mjs} +2 -2
- package/dist/{chunk-IF6P5ZXR.mjs → chunk-6UNB66M3.mjs} +38 -10
- package/dist/chunk-6UNB66M3.mjs.map +1 -0
- package/dist/{chunk-73KBO5JG.mjs → chunk-73YBF52D.mjs} +2 -2
- package/dist/chunk-BWYAGPEF.mjs +1 -0
- package/dist/{chunk-DZH3HSXW.mjs → chunk-CHOQ5C7K.mjs} +35 -8
- package/dist/chunk-CHOQ5C7K.mjs.map +1 -0
- package/dist/{chunk-33ERMMNX.mjs → chunk-DU7PVINC.mjs} +2 -2
- package/dist/chunk-IQH77T4S.mjs +44 -0
- package/dist/chunk-IQH77T4S.mjs.map +1 -0
- package/dist/{chunk-DB4VWZ5Q.mjs → chunk-LTA2O5WH.mjs} +11 -6
- package/dist/chunk-LTA2O5WH.mjs.map +1 -0
- package/dist/chunk-MTOHRBPJ.mjs +87 -0
- package/dist/chunk-MTOHRBPJ.mjs.map +1 -0
- package/dist/{chunk-52M7642J.mjs → chunk-PPYZB3IY.mjs} +52 -31
- package/dist/chunk-PPYZB3IY.mjs.map +1 -0
- package/dist/{chunk-H4XENJME.mjs → chunk-SYJIZLOI.mjs} +2 -2
- package/dist/{chunk-3K7JAFGM.mjs → chunk-TFSMXQYS.mjs} +2 -2
- 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 +46 -13
- 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 +46 -13
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +2 -2
- package/dist/components/index.js +46 -13
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +2 -2
- package/dist/context/copilot-context.d.ts +18 -8
- package/dist/context/copilot-context.js +10 -5
- package/dist/context/copilot-context.js.map +1 -1
- package/dist/context/copilot-context.mjs +1 -1
- package/dist/context/index.d.ts +2 -1
- package/dist/context/index.js +10 -5
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +1 -1
- package/dist/hooks/index.d.ts +4 -2
- package/dist/hooks/index.js +205 -89
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +17 -17
- package/dist/hooks/use-chat.d.ts +24 -7
- package/dist/hooks/use-chat.js +51 -30
- 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-copilot-chat-ui.js → use-coagent-action.js} +48 -22
- package/dist/hooks/use-coagent-action.js.map +1 -0
- package/dist/hooks/use-coagent-action.mjs +9 -0
- package/dist/hooks/use-coagent.d.ts +14 -18
- package/dist/hooks/use-coagent.js +184 -17
- package/dist/hooks/use-coagent.js.map +1 -1
- package/dist/hooks/use-coagent.mjs +6 -20
- package/dist/hooks/use-coagent.mjs.map +1 -1
- package/dist/hooks/use-copilot-action.js +10 -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 +89 -37
- 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 +10 -5
- 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 +10 -5
- 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 -2
- package/dist/index.js +263 -119
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +21 -21
- package/dist/lib/copilot-task.d.ts +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 -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-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 -1
- package/dist/utils/extract.js.map +1 -1
- package/dist/utils/extract.mjs +3 -3
- package/dist/utils/index.d.ts +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 +41 -9
- package/src/context/copilot-context.tsx +34 -15
- package/src/hooks/index.ts +2 -2
- package/src/hooks/use-chat.ts +88 -38
- package/src/hooks/use-coagent-action.ts +44 -0
- package/src/hooks/use-coagent.ts +111 -38
- package/src/hooks/use-copilot-chat.ts +29 -2
- package/src/types/coagent-action.ts +17 -0
- package/src/types/coagent-state.ts +9 -0
- package/dist/chunk-52M7642J.mjs.map +0 -1
- package/dist/chunk-6YOQY4WD.mjs +0 -20
- package/dist/chunk-6YOQY4WD.mjs.map +0 -1
- package/dist/chunk-A47L32JN.mjs +0 -52
- package/dist/chunk-A47L32JN.mjs.map +0 -1
- package/dist/chunk-AIWDXM7L.mjs +0 -1
- package/dist/chunk-DB4VWZ5Q.mjs.map +0 -1
- package/dist/chunk-DZH3HSXW.mjs.map +0 -1
- package/dist/chunk-IF6P5ZXR.mjs.map +0 -1
- package/dist/hooks/use-agent-state.d.ts +0 -12
- package/dist/hooks/use-agent-state.js +0 -499
- package/dist/hooks/use-agent-state.js.map +0 -1
- package/dist/hooks/use-agent-state.mjs +0 -19
- package/dist/hooks/use-copilot-chat-ui.d.ts +0 -13
- package/dist/hooks/use-copilot-chat-ui.js.map +0 -1
- package/dist/hooks/use-copilot-chat-ui.mjs +0 -10
- package/src/hooks/use-agent-state.ts +0 -63
- package/src/hooks/use-copilot-chat-ui.ts +0 -26
- /package/dist/{chunk-2MQX7BJL.mjs.map → chunk-6GOM3DN7.mjs.map} +0 -0
- /package/dist/{chunk-73KBO5JG.mjs.map → chunk-73YBF52D.mjs.map} +0 -0
- /package/dist/{chunk-AIWDXM7L.mjs.map → chunk-BWYAGPEF.mjs.map} +0 -0
- /package/dist/{chunk-33ERMMNX.mjs.map → chunk-DU7PVINC.mjs.map} +0 -0
- /package/dist/{chunk-H4XENJME.mjs.map → chunk-SYJIZLOI.mjs.map} +0 -0
- /package/dist/{chunk-3K7JAFGM.mjs.map → chunk-TFSMXQYS.mjs.map} +0 -0
- /package/dist/hooks/{use-agent-state.mjs.map → use-coagent-action.mjs.map} +0 -0
- /package/dist/{hooks/use-copilot-chat-ui.mjs.map → types/coagent-action.mjs.map} +0 -0
package/dist/index.js
CHANGED
|
@@ -86,10 +86,10 @@ __export(src_exports, {
|
|
|
86
86
|
CopilotTask: () => CopilotTask,
|
|
87
87
|
defaultCopilotContextCategories: () => defaultCopilotContextCategories,
|
|
88
88
|
extract: () => extract,
|
|
89
|
-
|
|
89
|
+
useCoagent: () => useCoagent,
|
|
90
|
+
useCoagentAction: () => useCoagentAction,
|
|
90
91
|
useCopilotAction: () => useCopilotAction,
|
|
91
92
|
useCopilotChat: () => useCopilotChat,
|
|
92
|
-
useCopilotChatUI: () => useCopilotChatUI,
|
|
93
93
|
useCopilotContext: () => useCopilotContext,
|
|
94
94
|
useCopilotReadable: () => useCopilotReadable,
|
|
95
95
|
useMakeCopilotDocumentReadable: () => useMakeCopilotDocumentReadable
|
|
@@ -107,7 +107,12 @@ var emptyCopilotContext = {
|
|
|
107
107
|
},
|
|
108
108
|
removeAction: () => {
|
|
109
109
|
},
|
|
110
|
-
|
|
110
|
+
coagentActions: {},
|
|
111
|
+
setCoagentAction: () => {
|
|
112
|
+
},
|
|
113
|
+
removeCoagentAction: () => {
|
|
114
|
+
},
|
|
115
|
+
chatComponentsCache: { current: { actions: {}, coagentActions: {} } },
|
|
111
116
|
getContextString: (documents, categories) => returnAndThrowInDebug(""),
|
|
112
117
|
addContext: () => "",
|
|
113
118
|
removeContext: () => {
|
|
@@ -141,11 +146,11 @@ var emptyCopilotContext = {
|
|
|
141
146
|
removeChatSuggestionConfiguration: () => {
|
|
142
147
|
},
|
|
143
148
|
showDevConsole: "auto",
|
|
144
|
-
|
|
145
|
-
|
|
149
|
+
coagentStates: {},
|
|
150
|
+
setCoagentStates: () => {
|
|
146
151
|
},
|
|
147
|
-
|
|
148
|
-
|
|
152
|
+
agentSession: null,
|
|
153
|
+
setAgentSession: () => {
|
|
149
154
|
}
|
|
150
155
|
};
|
|
151
156
|
var CopilotContext = import_react.default.createContext(emptyCopilotContext);
|
|
@@ -370,7 +375,11 @@ function CopilotKit(_a) {
|
|
|
370
375
|
}
|
|
371
376
|
const chatApiEndpoint = props.runtimeUrl || import_shared3.COPILOT_CLOUD_CHAT_URL;
|
|
372
377
|
const [actions, setActions] = (0, import_react4.useState)({});
|
|
373
|
-
const
|
|
378
|
+
const [coagentActions, setCoagentActions] = (0, import_react4.useState)({});
|
|
379
|
+
const chatComponentsCache = (0, import_react4.useRef)({
|
|
380
|
+
actions: {},
|
|
381
|
+
coagentActions: {}
|
|
382
|
+
});
|
|
374
383
|
const { addElement, removeElement, printTree } = use_tree_default();
|
|
375
384
|
const [messages, setMessages] = (0, import_react4.useState)([]);
|
|
376
385
|
const [isLoading, setIsLoading] = (0, import_react4.useState)(false);
|
|
@@ -394,6 +403,20 @@ function CopilotKit(_a) {
|
|
|
394
403
|
return newPoints;
|
|
395
404
|
});
|
|
396
405
|
}, []);
|
|
406
|
+
const setCoagentAction = (0, import_react4.useCallback)((id, action) => {
|
|
407
|
+
setCoagentActions((prevPoints) => {
|
|
408
|
+
return __spreadProps(__spreadValues({}, prevPoints), {
|
|
409
|
+
[id]: action
|
|
410
|
+
});
|
|
411
|
+
});
|
|
412
|
+
}, []);
|
|
413
|
+
const removeCoagentAction = (0, import_react4.useCallback)((id) => {
|
|
414
|
+
setCoagentActions((prevPoints) => {
|
|
415
|
+
const newPoints = __spreadValues({}, prevPoints);
|
|
416
|
+
delete newPoints[id];
|
|
417
|
+
return newPoints;
|
|
418
|
+
});
|
|
419
|
+
}, []);
|
|
397
420
|
const getContextString = (0, import_react4.useCallback)(
|
|
398
421
|
(documents, categories) => {
|
|
399
422
|
const documentsString = documents.map((document) => {
|
|
@@ -484,8 +507,14 @@ ${nonDocumentStrings}`;
|
|
|
484
507
|
return rest;
|
|
485
508
|
});
|
|
486
509
|
};
|
|
487
|
-
const [
|
|
488
|
-
|
|
510
|
+
const [coagentStates, setCoagentStates] = (0, import_react4.useState)({});
|
|
511
|
+
let initialAgentSession = null;
|
|
512
|
+
if (props.agent) {
|
|
513
|
+
initialAgentSession = {
|
|
514
|
+
agentName: props.agent
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
const [agentSession, setAgentSession] = (0, import_react4.useState)(initialAgentSession);
|
|
489
518
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
490
519
|
CopilotContext.Provider,
|
|
491
520
|
{
|
|
@@ -495,6 +524,9 @@ ${nonDocumentStrings}`;
|
|
|
495
524
|
getFunctionCallHandler,
|
|
496
525
|
setAction,
|
|
497
526
|
removeAction,
|
|
527
|
+
coagentActions,
|
|
528
|
+
setCoagentAction,
|
|
529
|
+
removeCoagentAction,
|
|
498
530
|
getContextString,
|
|
499
531
|
addContext,
|
|
500
532
|
removeContext,
|
|
@@ -512,10 +544,10 @@ ${nonDocumentStrings}`;
|
|
|
512
544
|
chatInstructions,
|
|
513
545
|
setChatInstructions,
|
|
514
546
|
showDevConsole: props.showDevConsole === void 0 ? "auto" : props.showDevConsole,
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
547
|
+
coagentStates,
|
|
548
|
+
setCoagentStates,
|
|
549
|
+
agentSession,
|
|
550
|
+
setAgentSession
|
|
519
551
|
},
|
|
520
552
|
children
|
|
521
553
|
}
|
|
@@ -533,8 +565,9 @@ function entryPointsToFunctionCallHandler(actions) {
|
|
|
533
565
|
if (action) {
|
|
534
566
|
yield new Promise((resolve, reject) => {
|
|
535
567
|
(0, import_react_dom.flushSync)(() => __async(this, null, function* () {
|
|
568
|
+
var _a;
|
|
536
569
|
try {
|
|
537
|
-
result = yield action.handler(args);
|
|
570
|
+
result = yield (_a = action.handler) == null ? void 0 : _a.call(action, args);
|
|
538
571
|
resolve();
|
|
539
572
|
} catch (error) {
|
|
540
573
|
reject(error);
|
|
@@ -566,12 +599,15 @@ function useChat(options) {
|
|
|
566
599
|
isLoading,
|
|
567
600
|
actions,
|
|
568
601
|
onFunctionCall,
|
|
569
|
-
|
|
602
|
+
onCoagentAction,
|
|
603
|
+
setCoagentStates,
|
|
604
|
+
coagentStates,
|
|
605
|
+
agentSession,
|
|
606
|
+
setAgentSession
|
|
570
607
|
} = options;
|
|
571
608
|
const abortControllerRef = (0, import_react5.useRef)();
|
|
572
609
|
const threadIdRef = (0, import_react5.useRef)(null);
|
|
573
610
|
const runIdRef = (0, import_react5.useRef)(null);
|
|
574
|
-
const agentSessionRef = (0, import_react5.useRef)(null);
|
|
575
611
|
const publicApiKey = copilotConfig.publicApiKey;
|
|
576
612
|
const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [import_shared4.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
|
|
577
613
|
const runtimeClient = new import_runtime_client_gql.CopilotRuntimeClient({
|
|
@@ -596,7 +632,7 @@ function useChat(options) {
|
|
|
596
632
|
const messagesWithContext = [systemMessage, ...initialMessages || [], ...previousMessages];
|
|
597
633
|
const stream = import_runtime_client_gql.CopilotRuntimeClient.asStream(
|
|
598
634
|
runtimeClient.generateCopilotResponse({
|
|
599
|
-
data: __spreadValues(__spreadProps(__spreadValues({
|
|
635
|
+
data: __spreadProps(__spreadValues(__spreadProps(__spreadValues({
|
|
600
636
|
frontend: {
|
|
601
637
|
actions: actions.map((action) => ({
|
|
602
638
|
name: action.name,
|
|
@@ -620,20 +656,22 @@ function useChat(options) {
|
|
|
620
656
|
metadata: {
|
|
621
657
|
requestType: import_runtime_client_gql.CopilotRequestType.Chat
|
|
622
658
|
}
|
|
623
|
-
}),
|
|
624
|
-
agentSession
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
659
|
+
}), agentSession ? {
|
|
660
|
+
agentSession
|
|
661
|
+
} : {}), {
|
|
662
|
+
agentStates: Object.values(coagentStates).map((state) => ({
|
|
663
|
+
agentName: state.name,
|
|
664
|
+
state: JSON.stringify(state.state)
|
|
665
|
+
}))
|
|
666
|
+
}),
|
|
630
667
|
properties: copilotConfig.properties,
|
|
631
668
|
signal: (_d = abortControllerRef.current) == null ? void 0 : _d.signal
|
|
632
669
|
})
|
|
633
670
|
);
|
|
634
671
|
const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
|
|
635
672
|
const reader = stream.getReader();
|
|
636
|
-
let
|
|
673
|
+
let actionResults = {};
|
|
674
|
+
let executedCoagentActions = [];
|
|
637
675
|
try {
|
|
638
676
|
while (true) {
|
|
639
677
|
const { done, value } = yield reader.read();
|
|
@@ -662,25 +700,28 @@ function useChat(options) {
|
|
|
662
700
|
newMessages.push(message);
|
|
663
701
|
if (message instanceof import_runtime_client_gql.AgentStateMessage) {
|
|
664
702
|
if (message.running) {
|
|
665
|
-
|
|
666
|
-
[message.agentName]:
|
|
703
|
+
setCoagentStates((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
704
|
+
[message.agentName]: {
|
|
705
|
+
name: message.agentName,
|
|
706
|
+
state: message.state,
|
|
707
|
+
running: message.running,
|
|
708
|
+
active: message.active,
|
|
709
|
+
threadId: message.threadId,
|
|
710
|
+
nodeName: message.nodeName,
|
|
711
|
+
runId: message.runId
|
|
712
|
+
}
|
|
667
713
|
}));
|
|
668
|
-
|
|
714
|
+
setAgentSession({
|
|
669
715
|
threadId: message.threadId,
|
|
670
716
|
agentName: message.agentName,
|
|
671
717
|
nodeName: message.nodeName
|
|
672
|
-
};
|
|
673
|
-
} else {
|
|
674
|
-
setAgentStates((prevAgentStates) => {
|
|
675
|
-
const newAgentStates = __spreadValues({}, prevAgentStates);
|
|
676
|
-
delete newAgentStates[message.agentName];
|
|
677
|
-
return newAgentStates;
|
|
678
718
|
});
|
|
679
|
-
|
|
719
|
+
} else {
|
|
720
|
+
setAgentSession(null);
|
|
680
721
|
}
|
|
681
722
|
}
|
|
682
723
|
if (message instanceof import_runtime_client_gql.ActionExecutionMessage && message.status.code !== import_runtime_client_gql.MessageStatusCode.Pending && message.scope === "client" && onFunctionCall) {
|
|
683
|
-
if (!(message.id in
|
|
724
|
+
if (!(message.id in actionResults)) {
|
|
684
725
|
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
685
726
|
break;
|
|
686
727
|
}
|
|
@@ -689,26 +730,39 @@ function useChat(options) {
|
|
|
689
730
|
name: message.name,
|
|
690
731
|
args: message.arguments
|
|
691
732
|
});
|
|
692
|
-
|
|
733
|
+
actionResults[message.id] = result;
|
|
693
734
|
}
|
|
694
735
|
newMessages.push(
|
|
695
736
|
new import_runtime_client_gql.ResultMessage({
|
|
696
|
-
result: import_runtime_client_gql.ResultMessage.encodeResult(
|
|
737
|
+
result: import_runtime_client_gql.ResultMessage.encodeResult(actionResults[message.id]),
|
|
697
738
|
actionExecutionId: message.id,
|
|
698
739
|
actionName: message.name
|
|
699
740
|
})
|
|
700
741
|
);
|
|
701
742
|
}
|
|
743
|
+
if (message instanceof import_runtime_client_gql.AgentStateMessage && !message.active && !executedCoagentActions.includes(message.id) && onCoagentAction) {
|
|
744
|
+
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
745
|
+
break;
|
|
746
|
+
}
|
|
747
|
+
yield onCoagentAction({
|
|
748
|
+
name: message.agentName,
|
|
749
|
+
nodeName: message.nodeName,
|
|
750
|
+
state: message.state
|
|
751
|
+
});
|
|
752
|
+
executedCoagentActions.push(message.id);
|
|
753
|
+
}
|
|
702
754
|
}
|
|
703
755
|
}
|
|
704
756
|
if (newMessages.length > 0) {
|
|
705
757
|
const filteredMessages = [...previousMessages, ...newMessages].reduce(
|
|
706
758
|
(acc, message) => {
|
|
707
|
-
if (
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
759
|
+
if (
|
|
760
|
+
// If the current message is an AgentStateMessage
|
|
761
|
+
message instanceof import_runtime_client_gql.AgentStateMessage && // And there is at least one message in the accumulator
|
|
762
|
+
acc.length > 0 && // And the last message in the accumulator is also an AgentStateMessage
|
|
763
|
+
acc[acc.length - 1] instanceof import_runtime_client_gql.AgentStateMessage && // And the agentName, nodeName, and runId are the same
|
|
764
|
+
acc[acc.length - 1].agentName === message.agentName && acc[acc.length - 1].nodeName === message.nodeName && acc[acc.length - 1].runId === message.runId
|
|
765
|
+
) {
|
|
712
766
|
acc[acc.length - 1] = message;
|
|
713
767
|
} else {
|
|
714
768
|
acc.push(message);
|
|
@@ -722,7 +776,7 @@ function useChat(options) {
|
|
|
722
776
|
}
|
|
723
777
|
if (
|
|
724
778
|
// if we have client side results
|
|
725
|
-
Object.values(
|
|
779
|
+
Object.values(actionResults).length || // or the last message we received is a result
|
|
726
780
|
newMessages.length && newMessages[newMessages.length - 1] instanceof import_runtime_client_gql.ResultMessage
|
|
727
781
|
) {
|
|
728
782
|
yield new Promise((resolve) => setTimeout(resolve, 10));
|
|
@@ -785,7 +839,11 @@ function useCopilotChat(_a = {}) {
|
|
|
785
839
|
setIsLoading,
|
|
786
840
|
chatInstructions,
|
|
787
841
|
actions,
|
|
788
|
-
|
|
842
|
+
coagentStates,
|
|
843
|
+
setCoagentStates,
|
|
844
|
+
coagentActions,
|
|
845
|
+
agentSession,
|
|
846
|
+
setAgentSession
|
|
789
847
|
} = (0, import_react6.useContext)(CopilotContext);
|
|
790
848
|
const latestGetContextString = useUpdatedRef(getContextString);
|
|
791
849
|
const deleteMessage = (0, import_react6.useCallback)(
|
|
@@ -802,17 +860,39 @@ function useCopilotChat(_a = {}) {
|
|
|
802
860
|
role: import_runtime_client_gql2.Role.System
|
|
803
861
|
});
|
|
804
862
|
}, [getContextString, makeSystemMessage2, chatInstructions]);
|
|
863
|
+
const onCoagentAction = (0, import_react6.useCallback)(
|
|
864
|
+
(args) => __async(this, null, function* () {
|
|
865
|
+
var _a2;
|
|
866
|
+
const { name, nodeName, state } = args;
|
|
867
|
+
let action = Object.values(coagentActions).find(
|
|
868
|
+
(action2) => action2.name === name && action2.nodeName === nodeName
|
|
869
|
+
);
|
|
870
|
+
if (!action) {
|
|
871
|
+
action = Object.values(coagentActions).find(
|
|
872
|
+
(action2) => action2.name === name && !action2.nodeName
|
|
873
|
+
);
|
|
874
|
+
}
|
|
875
|
+
if (action) {
|
|
876
|
+
yield (_a2 = action.handler) == null ? void 0 : _a2.call(action, { state, nodeName });
|
|
877
|
+
}
|
|
878
|
+
}),
|
|
879
|
+
[coagentActions]
|
|
880
|
+
);
|
|
805
881
|
const { append, reload, stop } = useChat(__spreadProps(__spreadValues({}, options), {
|
|
806
882
|
actions: Object.values(actions),
|
|
807
883
|
copilotConfig: copilotApiConfig,
|
|
808
884
|
initialMessages: options.initialMessages || [],
|
|
809
885
|
onFunctionCall: getFunctionCallHandler(),
|
|
886
|
+
onCoagentAction,
|
|
810
887
|
messages,
|
|
811
888
|
setMessages,
|
|
812
889
|
makeSystemMessageCallback,
|
|
813
890
|
isLoading,
|
|
814
891
|
setIsLoading,
|
|
815
|
-
|
|
892
|
+
coagentStates,
|
|
893
|
+
setCoagentStates,
|
|
894
|
+
agentSession,
|
|
895
|
+
setAgentSession
|
|
816
896
|
}));
|
|
817
897
|
return {
|
|
818
898
|
visibleMessages: messages,
|
|
@@ -894,12 +974,48 @@ function useCopilotAction(action, dependencies) {
|
|
|
894
974
|
]);
|
|
895
975
|
}
|
|
896
976
|
|
|
897
|
-
// src/hooks/use-
|
|
977
|
+
// src/hooks/use-coagent-action.ts
|
|
898
978
|
var import_react8 = require("react");
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
const
|
|
979
|
+
var import_shared6 = require("@copilotkit/shared");
|
|
980
|
+
function useCoagentAction(action, dependencies) {
|
|
981
|
+
const { setCoagentAction, removeCoagentAction, coagentActions, chatComponentsCache } = (0, import_react8.useContext)(CopilotContext);
|
|
982
|
+
const idRef = (0, import_react8.useRef)((0, import_shared6.randomId)());
|
|
983
|
+
const key = `${action.name}-${action.nodeName || "global"}`;
|
|
984
|
+
if (dependencies === void 0) {
|
|
985
|
+
if (coagentActions[idRef.current]) {
|
|
986
|
+
coagentActions[idRef.current].handler = action.handler;
|
|
987
|
+
if (typeof action.render === "function") {
|
|
988
|
+
if (chatComponentsCache.current !== null) {
|
|
989
|
+
chatComponentsCache.current.coagentActions[key] = action.render;
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
}
|
|
902
994
|
(0, import_react8.useEffect)(() => {
|
|
995
|
+
setCoagentAction(idRef.current, action);
|
|
996
|
+
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
997
|
+
chatComponentsCache.current.coagentActions[key] = action.render;
|
|
998
|
+
}
|
|
999
|
+
return () => {
|
|
1000
|
+
removeCoagentAction(idRef.current);
|
|
1001
|
+
};
|
|
1002
|
+
}, [
|
|
1003
|
+
setCoagentAction,
|
|
1004
|
+
removeCoagentAction,
|
|
1005
|
+
action.name,
|
|
1006
|
+
// include render only if it's a string
|
|
1007
|
+
typeof action.render === "string" ? action.render : void 0,
|
|
1008
|
+
// dependencies set by the developer
|
|
1009
|
+
...dependencies || []
|
|
1010
|
+
]);
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
// src/hooks/use-make-copilot-document-readable.ts
|
|
1014
|
+
var import_react9 = require("react");
|
|
1015
|
+
function useMakeCopilotDocumentReadable(document, categories, dependencies = []) {
|
|
1016
|
+
const { addDocumentContext, removeDocumentContext } = (0, import_react9.useContext)(CopilotContext);
|
|
1017
|
+
const idRef = (0, import_react9.useRef)();
|
|
1018
|
+
(0, import_react9.useEffect)(() => {
|
|
903
1019
|
const id = addDocumentContext(document, categories);
|
|
904
1020
|
idRef.current = id;
|
|
905
1021
|
return () => {
|
|
@@ -910,16 +1026,16 @@ function useMakeCopilotDocumentReadable(document, categories, dependencies = [])
|
|
|
910
1026
|
}
|
|
911
1027
|
|
|
912
1028
|
// src/hooks/use-copilot-readable.ts
|
|
913
|
-
var
|
|
1029
|
+
var import_react10 = require("react");
|
|
914
1030
|
function convertToJSON(description, value) {
|
|
915
1031
|
return `${description}: ${typeof value === "string" ? value : JSON.stringify(value)}`;
|
|
916
1032
|
}
|
|
917
1033
|
function useCopilotReadable({ description, value, parentId, categories, convert }, dependencies) {
|
|
918
|
-
const { addContext, removeContext } = (0,
|
|
919
|
-
const idRef = (0,
|
|
1034
|
+
const { addContext, removeContext } = (0, import_react10.useContext)(CopilotContext);
|
|
1035
|
+
const idRef = (0, import_react10.useRef)();
|
|
920
1036
|
convert = convert || convertToJSON;
|
|
921
1037
|
const information = convert(description, value);
|
|
922
|
-
(0,
|
|
1038
|
+
(0, import_react10.useEffect)(() => {
|
|
923
1039
|
const id = addContext(information, parentId, categories);
|
|
924
1040
|
idRef.current = id;
|
|
925
1041
|
return () => {
|
|
@@ -929,56 +1045,84 @@ function useCopilotReadable({ description, value, parentId, categories, convert
|
|
|
929
1045
|
return idRef.current;
|
|
930
1046
|
}
|
|
931
1047
|
|
|
932
|
-
// src/hooks/use-
|
|
933
|
-
var
|
|
934
|
-
function
|
|
935
|
-
const
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
1048
|
+
// src/hooks/use-coagent.ts
|
|
1049
|
+
var import_react11 = require("react");
|
|
1050
|
+
function useCoagent(options) {
|
|
1051
|
+
const isExternalStateManagement = (options2) => {
|
|
1052
|
+
return "state" in options2 && "setState" in options2;
|
|
1053
|
+
};
|
|
1054
|
+
const { name } = options;
|
|
1055
|
+
const isInternalStateManagementWithInitial = (options2) => {
|
|
1056
|
+
return "initialState" in options2;
|
|
1057
|
+
};
|
|
1058
|
+
const { coagentStates, setCoagentStates } = useCopilotContext();
|
|
1059
|
+
const getCoagentState = (coagentStates2, name2) => {
|
|
1060
|
+
if (coagentStates2[name2]) {
|
|
1061
|
+
return coagentStates2[name2];
|
|
1062
|
+
} else {
|
|
1063
|
+
return {
|
|
1064
|
+
name: name2,
|
|
1065
|
+
state: isInternalStateManagementWithInitial(options) ? options.initialState : {},
|
|
1066
|
+
running: false,
|
|
1067
|
+
active: false,
|
|
1068
|
+
threadId: void 0,
|
|
1069
|
+
nodeName: void 0,
|
|
1070
|
+
runId: void 0
|
|
1071
|
+
};
|
|
1072
|
+
}
|
|
1073
|
+
};
|
|
1074
|
+
const setState = (newState) => {
|
|
1075
|
+
setCoagentStates((prevAgentStates) => {
|
|
1076
|
+
let coagentState2 = getCoagentState(prevAgentStates, name);
|
|
1077
|
+
const updatedState = typeof newState === "function" ? newState(coagentState2.state) : newState;
|
|
954
1078
|
return __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
955
|
-
[
|
|
1079
|
+
[name]: __spreadProps(__spreadValues({}, coagentState2), {
|
|
1080
|
+
state: updatedState
|
|
1081
|
+
})
|
|
956
1082
|
});
|
|
957
1083
|
});
|
|
958
1084
|
};
|
|
959
|
-
const
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
1085
|
+
const coagentState = getCoagentState(coagentStates, name);
|
|
1086
|
+
const state = isExternalStateManagement(options) ? options.state : coagentState.state;
|
|
1087
|
+
(0, import_react11.useEffect)(() => {
|
|
1088
|
+
if (isExternalStateManagement(options)) {
|
|
1089
|
+
setState(options.state);
|
|
1090
|
+
} else if (coagentStates[name] === void 0) {
|
|
1091
|
+
setState(options.initialState === void 0 ? {} : options.initialState);
|
|
1092
|
+
}
|
|
1093
|
+
}, [isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0]);
|
|
1094
|
+
return {
|
|
1095
|
+
name,
|
|
1096
|
+
nodeName: coagentState.nodeName,
|
|
1097
|
+
state,
|
|
1098
|
+
setState,
|
|
1099
|
+
running: coagentState.running,
|
|
1100
|
+
start: () => {
|
|
1101
|
+
startAgent(name);
|
|
1102
|
+
},
|
|
1103
|
+
stop: () => {
|
|
1104
|
+
stopAgent(name);
|
|
1105
|
+
}
|
|
1106
|
+
};
|
|
965
1107
|
}
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
}
|
|
1108
|
+
function startAgent(name) {
|
|
1109
|
+
const { setAgentSession } = useCopilotContext();
|
|
1110
|
+
setAgentSession({
|
|
1111
|
+
agentName: name
|
|
1112
|
+
});
|
|
1113
|
+
}
|
|
1114
|
+
function stopAgent(name) {
|
|
1115
|
+
const { agentSession, setAgentSession } = useCopilotContext();
|
|
1116
|
+
if (agentSession && agentSession.agentName === name) {
|
|
1117
|
+
setAgentSession(null);
|
|
1118
|
+
} else {
|
|
1119
|
+
console.warn(`No agent session found for ${name}`);
|
|
1120
|
+
}
|
|
977
1121
|
}
|
|
978
1122
|
|
|
979
1123
|
// src/lib/copilot-task.ts
|
|
980
|
-
var
|
|
981
|
-
var
|
|
1124
|
+
var import_runtime_client_gql3 = require("@copilotkit/runtime-client-gql");
|
|
1125
|
+
var import_shared7 = require("@copilotkit/shared");
|
|
982
1126
|
var CopilotTask = class {
|
|
983
1127
|
constructor(config) {
|
|
984
1128
|
this.instructions = config.instructions;
|
|
@@ -1005,12 +1149,12 @@ var CopilotTask = class {
|
|
|
1005
1149
|
if (this.includeCopilotReadable) {
|
|
1006
1150
|
contextString += context.getContextString([], defaultCopilotContextCategories);
|
|
1007
1151
|
}
|
|
1008
|
-
const systemMessage = new
|
|
1152
|
+
const systemMessage = new import_runtime_client_gql3.TextMessage({
|
|
1009
1153
|
content: taskSystemMessage(contextString, this.instructions),
|
|
1010
|
-
role:
|
|
1154
|
+
role: import_runtime_client_gql3.Role.System
|
|
1011
1155
|
});
|
|
1012
1156
|
const messages = [systemMessage];
|
|
1013
|
-
const runtimeClient = new
|
|
1157
|
+
const runtimeClient = new import_runtime_client_gql3.CopilotRuntimeClient({
|
|
1014
1158
|
url: context.copilotApiConfig.chatApiEndpoint,
|
|
1015
1159
|
publicApiKey: context.copilotApiConfig.publicApiKey,
|
|
1016
1160
|
headers: context.copilotApiConfig.headers,
|
|
@@ -1022,12 +1166,12 @@ var CopilotTask = class {
|
|
|
1022
1166
|
actions: Object.values(actions).map((action) => ({
|
|
1023
1167
|
name: action.name,
|
|
1024
1168
|
description: action.description || "",
|
|
1025
|
-
jsonSchema: JSON.stringify((0,
|
|
1169
|
+
jsonSchema: JSON.stringify((0, import_shared7.actionParametersToJsonSchema)(action.parameters || []))
|
|
1026
1170
|
}))
|
|
1027
1171
|
},
|
|
1028
|
-
messages: (0,
|
|
1172
|
+
messages: (0, import_runtime_client_gql3.convertMessagesToGqlInput)(messages),
|
|
1029
1173
|
metadata: {
|
|
1030
|
-
requestType:
|
|
1174
|
+
requestType: import_runtime_client_gql3.CopilotRequestType.Task
|
|
1031
1175
|
},
|
|
1032
1176
|
forwardedParameters: {
|
|
1033
1177
|
toolChoice: "required"
|
|
@@ -1036,9 +1180,9 @@ var CopilotTask = class {
|
|
|
1036
1180
|
properties: context.copilotApiConfig.properties
|
|
1037
1181
|
}).toPromise();
|
|
1038
1182
|
const functionCallHandler = context.getFunctionCallHandler(actions);
|
|
1039
|
-
const functionCalls = (0,
|
|
1183
|
+
const functionCalls = (0, import_runtime_client_gql3.convertGqlOutputToMessages)(
|
|
1040
1184
|
((_b = (_a = response.data) == null ? void 0 : _a.generateCopilotResponse) == null ? void 0 : _b.messages) || []
|
|
1041
|
-
).filter((m) => m instanceof
|
|
1185
|
+
).filter((m) => m instanceof import_runtime_client_gql3.ActionExecutionMessage);
|
|
1042
1186
|
for (const functionCall of functionCalls) {
|
|
1043
1187
|
yield functionCallHandler({
|
|
1044
1188
|
messages,
|
|
@@ -1076,10 +1220,10 @@ ${instructions}
|
|
|
1076
1220
|
}
|
|
1077
1221
|
|
|
1078
1222
|
// src/utils/extract.ts
|
|
1079
|
-
var
|
|
1223
|
+
var import_shared8 = require("@copilotkit/shared");
|
|
1224
|
+
var import_runtime_client_gql4 = require("@copilotkit/runtime-client-gql");
|
|
1080
1225
|
var import_runtime_client_gql5 = require("@copilotkit/runtime-client-gql");
|
|
1081
1226
|
var import_runtime_client_gql6 = require("@copilotkit/runtime-client-gql");
|
|
1082
|
-
var import_runtime_client_gql7 = require("@copilotkit/runtime-client-gql");
|
|
1083
1227
|
function extract(_0) {
|
|
1084
1228
|
return __async(this, arguments, function* ({
|
|
1085
1229
|
context,
|
|
@@ -1089,7 +1233,7 @@ function extract(_0) {
|
|
|
1089
1233
|
data,
|
|
1090
1234
|
abortSignal,
|
|
1091
1235
|
stream,
|
|
1092
|
-
requestType =
|
|
1236
|
+
requestType = import_runtime_client_gql4.CopilotRequestType.Task
|
|
1093
1237
|
}) {
|
|
1094
1238
|
var _a, _b;
|
|
1095
1239
|
const { messages } = context;
|
|
@@ -1108,18 +1252,18 @@ function extract(_0) {
|
|
|
1108
1252
|
if (includeReadable) {
|
|
1109
1253
|
contextString += context.getContextString([], defaultCopilotContextCategories);
|
|
1110
1254
|
}
|
|
1111
|
-
const systemMessage = new
|
|
1255
|
+
const systemMessage = new import_runtime_client_gql4.TextMessage({
|
|
1112
1256
|
content: makeSystemMessage(contextString, instructions),
|
|
1113
|
-
role:
|
|
1257
|
+
role: import_runtime_client_gql4.Role.System
|
|
1114
1258
|
});
|
|
1115
|
-
const headers = __spreadValues(__spreadValues({}, context.copilotApiConfig.headers || {}), context.copilotApiConfig.publicApiKey ? { [
|
|
1116
|
-
const runtimeClient = new
|
|
1259
|
+
const headers = __spreadValues(__spreadValues({}, context.copilotApiConfig.headers || {}), context.copilotApiConfig.publicApiKey ? { [import_shared8.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: context.copilotApiConfig.publicApiKey } : {});
|
|
1260
|
+
const runtimeClient = new import_runtime_client_gql5.CopilotRuntimeClient({
|
|
1117
1261
|
url: context.copilotApiConfig.chatApiEndpoint,
|
|
1118
1262
|
publicApiKey: context.copilotApiConfig.publicApiKey,
|
|
1119
1263
|
headers,
|
|
1120
1264
|
credentials: context.copilotApiConfig.credentials
|
|
1121
1265
|
});
|
|
1122
|
-
const response =
|
|
1266
|
+
const response = import_runtime_client_gql5.CopilotRuntimeClient.asStream(
|
|
1123
1267
|
runtimeClient.generateCopilotResponse({
|
|
1124
1268
|
data: {
|
|
1125
1269
|
frontend: {
|
|
@@ -1127,11 +1271,11 @@ function extract(_0) {
|
|
|
1127
1271
|
{
|
|
1128
1272
|
name: action.name,
|
|
1129
1273
|
description: action.description || "",
|
|
1130
|
-
jsonSchema: JSON.stringify((0,
|
|
1274
|
+
jsonSchema: JSON.stringify((0, import_shared8.actionParametersToJsonSchema)(action.parameters || []))
|
|
1131
1275
|
}
|
|
1132
1276
|
]
|
|
1133
1277
|
},
|
|
1134
|
-
messages: (0,
|
|
1278
|
+
messages: (0, import_runtime_client_gql6.convertMessagesToGqlInput)(
|
|
1135
1279
|
includeMessages ? [systemMessage, ...messages] : [systemMessage]
|
|
1136
1280
|
),
|
|
1137
1281
|
metadata: {
|
|
@@ -1157,9 +1301,9 @@ function extract(_0) {
|
|
|
1157
1301
|
if (abortSignal == null ? void 0 : abortSignal.aborted) {
|
|
1158
1302
|
throw new Error("Aborted");
|
|
1159
1303
|
}
|
|
1160
|
-
actionExecutionMessage = (0,
|
|
1304
|
+
actionExecutionMessage = (0, import_runtime_client_gql4.convertGqlOutputToMessages)(
|
|
1161
1305
|
value.generateCopilotResponse.messages
|
|
1162
|
-
).find((msg) => msg instanceof
|
|
1306
|
+
).find((msg) => msg instanceof import_runtime_client_gql4.ActionExecutionMessage);
|
|
1163
1307
|
if (!actionExecutionMessage) {
|
|
1164
1308
|
continue;
|
|
1165
1309
|
}
|
|
@@ -1213,10 +1357,10 @@ Any additional messages provided are for providing context only and should not b
|
|
|
1213
1357
|
CopilotTask,
|
|
1214
1358
|
defaultCopilotContextCategories,
|
|
1215
1359
|
extract,
|
|
1216
|
-
|
|
1360
|
+
useCoagent,
|
|
1361
|
+
useCoagentAction,
|
|
1217
1362
|
useCopilotAction,
|
|
1218
1363
|
useCopilotChat,
|
|
1219
|
-
useCopilotChatUI,
|
|
1220
1364
|
useCopilotContext,
|
|
1221
1365
|
useCopilotReadable,
|
|
1222
1366
|
useMakeCopilotDocumentReadable
|