@copilotkit/react-core 1.2.1 → 1.2.2-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 +36 -0
- package/dist/chunk-24BNTWFY.mjs +87 -0
- package/dist/chunk-24BNTWFY.mjs.map +1 -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-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 +21 -12
- 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 +38 -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 +24 -16
- 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 +131 -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/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,41 @@
|
|
|
1
1
|
# ui
|
|
2
2
|
|
|
3
|
+
## 1.2.2-feat-runtime-remote-actions.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Fix useCoAgent start/stop bug
|
|
8
|
+
- Updated dependencies
|
|
9
|
+
- @copilotkit/runtime-client-gql@1.2.2-feat-runtime-remote-actions.1
|
|
10
|
+
- @copilotkit/shared@1.2.2-feat-runtime-remote-actions.1
|
|
11
|
+
|
|
12
|
+
## 1.2.2-feat-runtime-remote-actions.0
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- Prerelease
|
|
17
|
+
- 0d54450: Include agent state
|
|
18
|
+
- 8d32e22: Show agent state in the chat window
|
|
19
|
+
- 5b63f55: stream intermediate state
|
|
20
|
+
- b6fd3d8: Better message grouping
|
|
21
|
+
- 89420c6: Rename hooks and bugfixes
|
|
22
|
+
- b6e8824: useCoAgent/useCoAgentAction
|
|
23
|
+
- 91c35b9: useAgentState
|
|
24
|
+
- 00be203: Remote actions preview
|
|
25
|
+
- fb15f72: Reduce request size by skipping intermediate state
|
|
26
|
+
- Updated dependencies
|
|
27
|
+
- Updated dependencies [0d54450]
|
|
28
|
+
- Updated dependencies [8d32e22]
|
|
29
|
+
- Updated dependencies [5b63f55]
|
|
30
|
+
- Updated dependencies [b6fd3d8]
|
|
31
|
+
- Updated dependencies [89420c6]
|
|
32
|
+
- Updated dependencies [b6e8824]
|
|
33
|
+
- Updated dependencies [91c35b9]
|
|
34
|
+
- Updated dependencies [00be203]
|
|
35
|
+
- Updated dependencies [fb15f72]
|
|
36
|
+
- @copilotkit/runtime-client-gql@1.2.2-feat-runtime-remote-actions.0
|
|
37
|
+
- @copilotkit/shared@1.2.2-feat-runtime-remote-actions.0
|
|
38
|
+
|
|
3
39
|
## 1.2.1
|
|
4
40
|
|
|
5
41
|
### Patch Changes
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useCopilotContext
|
|
3
|
+
} from "./chunk-J2YXDQHR.mjs";
|
|
4
|
+
import {
|
|
5
|
+
__spreadProps,
|
|
6
|
+
__spreadValues
|
|
7
|
+
} from "./chunk-SKC7AJIV.mjs";
|
|
8
|
+
|
|
9
|
+
// src/hooks/use-coagent.ts
|
|
10
|
+
import { useEffect } from "react";
|
|
11
|
+
function useCoAgent(options) {
|
|
12
|
+
const isExternalStateManagement = (options2) => {
|
|
13
|
+
return "state" in options2 && "setState" in options2;
|
|
14
|
+
};
|
|
15
|
+
const { name } = options;
|
|
16
|
+
const isInternalStateManagementWithInitial = (options2) => {
|
|
17
|
+
return "initialState" in options2;
|
|
18
|
+
};
|
|
19
|
+
const { coagentStates, setCoagentStates } = useCopilotContext();
|
|
20
|
+
const getCoagentState = (coagentStates2, name2) => {
|
|
21
|
+
if (coagentStates2[name2]) {
|
|
22
|
+
return coagentStates2[name2];
|
|
23
|
+
} else {
|
|
24
|
+
return {
|
|
25
|
+
name: name2,
|
|
26
|
+
state: isInternalStateManagementWithInitial(options) ? options.initialState : {},
|
|
27
|
+
running: false,
|
|
28
|
+
active: false,
|
|
29
|
+
threadId: void 0,
|
|
30
|
+
nodeName: void 0,
|
|
31
|
+
runId: void 0
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const setState = (newState) => {
|
|
36
|
+
setCoagentStates((prevAgentStates) => {
|
|
37
|
+
let coagentState2 = getCoagentState(prevAgentStates, name);
|
|
38
|
+
const updatedState = typeof newState === "function" ? newState(coagentState2.state) : newState;
|
|
39
|
+
return __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
40
|
+
[name]: __spreadProps(__spreadValues({}, coagentState2), {
|
|
41
|
+
state: updatedState
|
|
42
|
+
})
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
const coagentState = getCoagentState(coagentStates, name);
|
|
47
|
+
const state = isExternalStateManagement(options) ? options.state : coagentState.state;
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
if (isExternalStateManagement(options)) {
|
|
50
|
+
setState(options.state);
|
|
51
|
+
} else if (coagentStates[name] === void 0) {
|
|
52
|
+
setState(options.initialState === void 0 ? {} : options.initialState);
|
|
53
|
+
}
|
|
54
|
+
}, [isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0]);
|
|
55
|
+
return {
|
|
56
|
+
name,
|
|
57
|
+
nodeName: coagentState.nodeName,
|
|
58
|
+
state,
|
|
59
|
+
setState,
|
|
60
|
+
running: coagentState.running,
|
|
61
|
+
start: (context) => {
|
|
62
|
+
startAgent(name, context);
|
|
63
|
+
},
|
|
64
|
+
stop: (context) => {
|
|
65
|
+
stopAgent(name, context);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function startAgent(name, context) {
|
|
70
|
+
const { setAgentSession } = context;
|
|
71
|
+
setAgentSession({
|
|
72
|
+
agentName: name
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
function stopAgent(name, context) {
|
|
76
|
+
const { agentSession, setAgentSession } = context;
|
|
77
|
+
if (agentSession && agentSession.agentName === name) {
|
|
78
|
+
setAgentSession(null);
|
|
79
|
+
} else {
|
|
80
|
+
console.warn(`No agent session found for ${name}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export {
|
|
85
|
+
useCoAgent
|
|
86
|
+
};
|
|
87
|
+
//# sourceMappingURL=chunk-24BNTWFY.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/hooks/use-coagent.ts"],"sourcesContent":["import { useEffect } from \"react\";\nimport { CopilotContextParams, useCopilotContext } from \"../context\";\nimport { CoagentState } from \"../types/coagent-state\";\n\ninterface WithInternalStateManagementAndInitial<T> {\n name: string;\n initialState: T;\n}\n\ninterface WithInternalStateManagement {\n name: string;\n initialState?: any; // Optional initialState with default type any\n}\n\ninterface WithExternalStateManagement<T> {\n name: string;\n state: T;\n setState: (newState: T | ((prevState: T | undefined) => T)) => void;\n}\n\ntype UseCoagentOptions<T> =\n | WithInternalStateManagementAndInitial<T>\n | WithInternalStateManagement\n | WithExternalStateManagement<T>;\n\nexport interface UseCoagentReturnType<T> {\n name: string;\n nodeName?: string;\n threadId?: string;\n running: boolean;\n state: T;\n setState: (newState: T | ((prevState: T | undefined) => T)) => void;\n start: (context: CopilotContextParams) => void;\n stop: (context: CopilotContextParams) => void;\n}\n\nexport function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentReturnType<T> {\n const isExternalStateManagement = (\n options: UseCoagentOptions<T>,\n ): options is WithExternalStateManagement<T> => {\n return \"state\" in options && \"setState\" in options;\n };\n\n const { name } = options;\n\n const isInternalStateManagementWithInitial = (\n options: UseCoagentOptions<T>,\n ): options is WithInternalStateManagementAndInitial<T> => {\n return \"initialState\" in options;\n };\n\n const { coagentStates, setCoagentStates } = useCopilotContext();\n\n const getCoagentState = (coagentStates: Record<string, CoagentState>, name: string) => {\n if (coagentStates[name]) {\n return coagentStates[name];\n } else {\n return {\n name,\n state: isInternalStateManagementWithInitial(options) ? options.initialState : {},\n running: false,\n active: false,\n threadId: undefined,\n nodeName: undefined,\n runId: undefined,\n };\n }\n };\n\n // if we manage state internally, we need to provide a function to set the state\n const setState = (newState: T | ((prevState: T | undefined) => T)) => {\n setCoagentStates((prevAgentStates) => {\n let coagentState: CoagentState = getCoagentState(prevAgentStates, name);\n\n const updatedState =\n typeof newState === \"function\" ? (newState as Function)(coagentState.state) : newState;\n\n return {\n ...prevAgentStates,\n [name]: {\n ...coagentState,\n state: updatedState,\n },\n };\n });\n };\n\n const coagentState = getCoagentState(coagentStates, name);\n\n const state = isExternalStateManagement(options) ? options.state : coagentState.state;\n\n // Sync internal state with external state if state management is external\n useEffect(() => {\n if (isExternalStateManagement(options)) {\n setState(options.state);\n } else if (coagentStates[name] === undefined) {\n setState(options.initialState === undefined ? {} : options.initialState);\n }\n }, [isExternalStateManagement(options) ? JSON.stringify(options.state) : undefined]);\n\n // Return the state and setState function\n return {\n name,\n nodeName: coagentState.nodeName,\n state,\n setState,\n running: coagentState.running,\n start: (context: CopilotContextParams) => {\n startAgent(name, context);\n },\n stop: (context: CopilotContextParams) => {\n stopAgent(name, context);\n },\n };\n}\n\nfunction startAgent(name: string, context: CopilotContextParams) {\n const { setAgentSession } = context;\n setAgentSession({\n agentName: name,\n });\n}\n\nfunction stopAgent(name: string, context: CopilotContextParams) {\n const { agentSession, setAgentSession } = context;\n if (agentSession && agentSession.agentName === name) {\n setAgentSession(null);\n } else {\n console.warn(`No agent session found for ${name}`);\n }\n}\n"],"mappings":";;;;;;;;;AAAA,SAAS,iBAAiB;AAoCnB,SAAS,WAAoB,SAAwD;AAC1F,QAAM,4BAA4B,CAChCA,aAC8C;AAC9C,WAAO,WAAWA,YAAW,cAAcA;AAAA,EAC7C;AAEA,QAAM,EAAE,KAAK,IAAI;AAEjB,QAAM,uCAAuC,CAC3CA,aACwD;AACxD,WAAO,kBAAkBA;AAAA,EAC3B;AAEA,QAAM,EAAE,eAAe,iBAAiB,IAAI,kBAAkB;AAE9D,QAAM,kBAAkB,CAACC,gBAA6CC,UAAiB;AACrF,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;AAC/D,QAAM,EAAE,gBAAgB,IAAI;AAC5B,kBAAgB;AAAA,IACd,WAAW;AAAA,EACb,CAAC;AACH;AAEA,SAAS,UAAU,MAAc,SAA+B;AAC9D,QAAM,EAAE,cAAc,gBAAgB,IAAI;AAC1C,MAAI,gBAAgB,aAAa,cAAc,MAAM;AACnD,oBAAgB,IAAI;AAAA,EACtB,OAAO;AACL,YAAQ,KAAK,8BAA8B,MAAM;AAAA,EACnD;AACF;","names":["options","coagentStates","name","coagentState"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
useCopilotContext
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-J2YXDQHR.mjs";
|
|
4
4
|
|
|
5
5
|
// src/hooks/use-make-copilot-document-readable.ts
|
|
6
6
|
import { useEffect, useRef } from "react";
|
|
@@ -20,4 +20,4 @@ function useMakeCopilotDocumentReadable(document, categories, dependencies = [])
|
|
|
20
20
|
export {
|
|
21
21
|
useMakeCopilotDocumentReadable
|
|
22
22
|
};
|
|
23
|
-
//# sourceMappingURL=chunk-
|
|
23
|
+
//# sourceMappingURL=chunk-326LK7LX.mjs.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
defaultCopilotContextCategories
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-RBNULK3U.mjs";
|
|
4
4
|
import {
|
|
5
5
|
__async
|
|
6
6
|
} from "./chunk-SKC7AJIV.mjs";
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
TextMessage,
|
|
14
14
|
convertGqlOutputToMessages,
|
|
15
15
|
convertMessagesToGqlInput,
|
|
16
|
+
filterAgentStateMessages,
|
|
16
17
|
CopilotRequestType
|
|
17
18
|
} from "@copilotkit/runtime-client-gql";
|
|
18
19
|
import { actionParametersToJsonSchema } from "@copilotkit/shared";
|
|
@@ -63,7 +64,7 @@ var CopilotTask = class {
|
|
|
63
64
|
})),
|
|
64
65
|
url: window.location.href
|
|
65
66
|
},
|
|
66
|
-
messages: convertMessagesToGqlInput(messages),
|
|
67
|
+
messages: convertMessagesToGqlInput(filterAgentStateMessages(messages)),
|
|
67
68
|
metadata: {
|
|
68
69
|
requestType: CopilotRequestType.Task
|
|
69
70
|
},
|
|
@@ -116,4 +117,4 @@ ${instructions}
|
|
|
116
117
|
export {
|
|
117
118
|
CopilotTask
|
|
118
119
|
};
|
|
119
|
-
//# sourceMappingURL=chunk-
|
|
120
|
+
//# sourceMappingURL=chunk-3U22BXKC.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/lib/copilot-task.ts"],"sourcesContent":["/**\n * This class is used to execute one-off tasks, for example on button press. It can use the context available via [useCopilotReadable](/reference/hooks/useCopilotReadable) and the actions provided by [useCopilotAction](/reference/hooks/useCopilotAction), or you can provide your own context and actions.\n *\n * ## Example\n * In the simplest case, use CopilotTask in the context of your app by giving it instructions on what to do.\n *\n * ```tsx\n * import { CopilotTask, useCopilotContext } from \"@copilotkit/react-core\";\n *\n * export function MyComponent() {\n * const context = useCopilotContext();\n *\n * const task = new CopilotTask({\n * instructions: \"Set a random message\",\n * actions: [\n * {\n * name: \"setMessage\",\n * description: \"Set the message.\",\n * argumentAnnotations: [\n * {\n * name: \"message\",\n * type: \"string\",\n * description:\n * \"A message to display.\",\n * required: true,\n * },\n * ],\n *\n * implementation: async (message) => {\n * // ...\n * },\n * }\n * ]\n * });\n *\n * const executeTask = async () => {\n * await task.run(context, action);\n * }\n *\n * return (\n * <>\n * <button onClick={executeTask}>\n * Execute task\n * </button>\n * </>\n * )\n * }\n * ```\n *\n * Have a look at the [Presentation Example App](https://github.com/CopilotKit/CopilotKit/blob/main/CopilotKit/examples/next-openai/src/app/presentation/page.tsx) for a more complete example.\n */\n\nimport {\n ActionExecutionMessage,\n CopilotRuntimeClient,\n Message,\n Role,\n TextMessage,\n convertGqlOutputToMessages,\n convertMessagesToGqlInput,\n filterAgentStateMessages,\n CopilotRequestType,\n} from \"@copilotkit/runtime-client-gql\";\nimport { FrontendAction } from \"../types/frontend-action\";\nimport { CopilotContextParams } from \"../context\";\nimport { defaultCopilotContextCategories } from \"../components\";\nimport { MessageStatusCode } from \"@copilotkit/runtime-client-gql\";\nimport { actionParametersToJsonSchema } from \"@copilotkit/shared\";\n\nexport interface CopilotTaskConfig {\n /**\n * The instructions to be given to the assistant.\n */\n instructions: string;\n /**\n * An array of action definitions that can be called.\n */\n actions?: FrontendAction<any>[];\n /**\n * Whether to include the copilot readable context in the task.\n */\n includeCopilotReadable?: boolean;\n\n /**\n * Whether to include actions defined via useCopilotAction in the task.\n */\n includeCopilotActions?: boolean;\n}\n\nexport class CopilotTask<T = any> {\n private instructions: string;\n private actions: FrontendAction<any>[];\n private includeCopilotReadable: boolean;\n private includeCopilotActions: boolean;\n\n constructor(config: CopilotTaskConfig) {\n this.instructions = config.instructions;\n this.actions = config.actions || [];\n this.includeCopilotReadable = config.includeCopilotReadable !== false;\n this.includeCopilotActions = config.includeCopilotActions !== false;\n }\n\n /**\n * Run the task.\n * @param context The CopilotContext to use for the task. Use `useCopilotContext` to obtain the current context.\n * @param data The data to use for the task.\n */\n async run(context: CopilotContextParams, data?: T): Promise<void> {\n const actions = this.includeCopilotActions ? Object.assign({}, context.actions) : {};\n\n // merge functions into entry points\n for (const fn of this.actions) {\n actions[fn.name] = fn;\n }\n\n let contextString = \"\";\n\n if (data) {\n contextString = (typeof data === \"string\" ? data : JSON.stringify(data)) + \"\\n\\n\";\n }\n\n if (this.includeCopilotReadable) {\n contextString += context.getContextString([], defaultCopilotContextCategories);\n }\n\n const systemMessage = new TextMessage({\n content: taskSystemMessage(contextString, this.instructions),\n role: Role.System,\n });\n\n const messages: Message[] = [systemMessage];\n\n const runtimeClient = new CopilotRuntimeClient({\n url: context.copilotApiConfig.chatApiEndpoint,\n publicApiKey: context.copilotApiConfig.publicApiKey,\n headers: context.copilotApiConfig.headers,\n credentials: context.copilotApiConfig.credentials,\n });\n\n const response = await runtimeClient\n .generateCopilotResponse({\n data: {\n frontend: {\n actions: Object.values(actions).map((action) => ({\n name: action.name,\n description: action.description || \"\",\n jsonSchema: JSON.stringify(actionParametersToJsonSchema(action.parameters || [])),\n })),\n url: window.location.href,\n },\n messages: convertMessagesToGqlInput(filterAgentStateMessages(messages)),\n metadata: {\n requestType: CopilotRequestType.Task,\n },\n forwardedParameters: {\n toolChoice: \"required\",\n },\n },\n properties: context.copilotApiConfig.properties,\n })\n .toPromise();\n\n const functionCallHandler = context.getFunctionCallHandler(actions);\n const functionCalls = convertGqlOutputToMessages(\n response.data?.generateCopilotResponse?.messages || [],\n ).filter((m): m is ActionExecutionMessage => m instanceof ActionExecutionMessage);\n\n for (const functionCall of functionCalls) {\n await functionCallHandler({\n messages,\n name: functionCall.name,\n args: functionCall.arguments,\n });\n }\n }\n}\n\nfunction taskSystemMessage(contextString: string, instructions: string): string {\n return `\nPlease act as an efficient, competent, conscientious, and industrious professional assistant.\n\nHelp the user achieve their goals, and you do so in a way that is as efficient as possible, without unnecessary fluff, but also without sacrificing professionalism.\nAlways be polite and respectful, and prefer brevity over verbosity.\n\nThe user has provided you with the following context:\n\\`\\`\\`\n${contextString}\n\\`\\`\\`\n\nThey have also provided you with functions you can call to initiate actions on their behalf.\n\nPlease assist them as best you can.\n\nThis is not a conversation, so please do not ask questions. Just call a function without saying anything else.\n\nThe user has given you the following task to complete:\n\n\\`\\`\\`\n${instructions}\n\\`\\`\\`\n`;\n}\n"],"mappings":";;;;;;;;AAoDA;AAAA,EACE;AAAA,EACA;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAKP,SAAS,oCAAoC;AAsBtC,IAAM,cAAN,MAA2B;AAAA,EAMhC,YAAY,QAA2B;AACrC,SAAK,eAAe,OAAO;AAC3B,SAAK,UAAU,OAAO,WAAW,CAAC;AAClC,SAAK,yBAAyB,OAAO,2BAA2B;AAChE,SAAK,wBAAw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AA,UACA,qBAAqB;AAAA,YACnB,YAAY;AAAA,UACd;AAAA,QACF;AAAA,QACA,YAAY,QAAQ,iBAAiB;AAAA,MACvC,CAAC,EACA,UAAU;AAEb,YAAM,sBAAsB,QAAQ,uBAAuB,OAAO;AAClE,YAAM,gBAAgB;AAAA,UACpB,oBAAS,SAAT,mBAAe,4BAAf,mBAAwC,aAAY,CAAC;AAAA,MACvD,EAAE,OAAO,CAAC,MAAmC,aAAa,sBAAsB;AAEhF,iBAAW,gBAAgB,eAAe;AACxC,cAAM,oBAAoB;AAAA,UACxB;AAAA,UACA,MAAM,aAAa;AAAA,UACnB,MAAM,aAAa;AAAA,QACrB,CAAC;AAAA,MACH;AAAA,IACF;AAAA;AACF;AAEA,SAAS,kBAAkB,eAAuB,cAA8B;AAC9E,SAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQP;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYA;AAAA;AAAA;AAGF;","names":[]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
useCopilotContext
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-J2YXDQHR.mjs";
|
|
4
4
|
|
|
5
5
|
// src/hooks/use-copilot-action.ts
|
|
6
6
|
import { useRef, useEffect } from "react";
|
|
@@ -13,7 +13,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
13
13
|
actions[idRef.current].handler = action.handler;
|
|
14
14
|
if (typeof action.render === "function") {
|
|
15
15
|
if (chatComponentsCache.current !== null) {
|
|
16
|
-
chatComponentsCache.current[action.name] = action.render;
|
|
16
|
+
chatComponentsCache.current.actions[action.name] = action.render;
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
}
|
|
@@ -24,7 +24,7 @@ function useCopilotAction(action, dependencies) {
|
|
|
24
24
|
}
|
|
25
25
|
setAction(idRef.current, action);
|
|
26
26
|
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
27
|
-
chatComponentsCache.current[action.name] = action.render;
|
|
27
|
+
chatComponentsCache.current.actions[action.name] = action.render;
|
|
28
28
|
}
|
|
29
29
|
return () => {
|
|
30
30
|
removeAction(idRef.current);
|
|
@@ -48,4 +48,4 @@ function useCopilotAction(action, dependencies) {
|
|
|
48
48
|
export {
|
|
49
49
|
useCopilotAction
|
|
50
50
|
};
|
|
51
|
-
//# sourceMappingURL=chunk-
|
|
51
|
+
//# sourceMappingURL=chunk-A37GANOW.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/hooks/use-copilot-action.ts"],"sourcesContent":["import { useRef, useEffect } from \"react\";\nimport { FrontendAction } from \"../types/frontend-action\";\nimport { useCopilotContext } from \"../context/copilot-context\";\nimport { Parameter, randomId } from \"@copilotkit/shared\";\n\n// We implement useCopilotAction dependency handling so that\n// the developer has the option to not provide any dependencies.\n// In this case, we assume they want to update the handler on each rerender.\n// To avoid getting stuck in an infinite loop, we update the handler directly,\n// skipping React state updates.\n// This is ok in this case, because the handler is not part of any UI that\n// needs to be updated.\n// useCallback, useMemo or other memoization techniques are not suitable here,\n// because they will cause a infinite rerender loop.\nexport function useCopilotAction<const T extends Parameter[] | [] = []>(\n action: FrontendAction<T>,\n dependencies?: any[],\n): void {\n const { setAction, removeAction, actions, chatComponentsCache } = useCopilotContext();\n const idRef = useRef<string>(randomId());\n\n // If the developer doesn't provide dependencies, we assume they want to\n // update handler and render function when the action object changes.\n // This ensures that any captured variables in the handler are up to date.\n if (dependencies === undefined) {\n if (actions[idRef.current]) {\n actions[idRef.current].handler = action.handler as any;\n if (typeof action.render === \"function\") {\n if (chatComponentsCache.current !== null) {\n chatComponentsCache.current.actions[action.name] = action.render;\n }\n }\n }\n }\n\n useEffect(() => {\n if (action.disabled) {\n return;\n }\n setAction(idRef.current, action as any);\n if (chatComponentsCache.current !== null && action.render !== undefined) {\n chatComponentsCache.current.actions[action.name] = action.render;\n }\n return () => {\n // NOTE: For now, we don't remove the chatComponentsCache entry when the action is removed.\n // This is because we currently don't have access to the messages array in CopilotContext.\n removeAction(idRef.current);\n };\n }, [\n setAction,\n removeAction,\n action.description,\n action.name,\n action.disabled,\n // This should be faster than deep equality checking\n // In addition, all major JS engines guarantee the order of object keys\n JSON.stringify(action.parameters),\n // include render only if it's a string\n typeof action.render === \"string\" ? action.render : undefined,\n // dependencies set by the developer\n ...(dependencies || []),\n ]);\n}\n\n// Usage Example:\n// useCopilotAction({\n// name: \"myAction\",\n// parameters: [\n// { name: \"arg1\", type: \"string\", enum: [\"option1\", \"option2\", \"option3\"], required: false },\n// { name: \"arg2\", type: \"number\" },\n// {\n// name: \"arg3\",\n// type: \"object\",\n// attributes: [\n// { name: \"nestedArg1\", type: \"boolean\" },\n// { name: \"xyz\", required: false },\n// ],\n// },\n// { name: \"arg4\", type: \"number[]\" },\n// ],\n// handler: ({ arg1, arg2, arg3, arg4 }) => {\n// const x = arg3.nestedArg1;\n// const z = arg3.xyz;\n// console.log(arg1, arg2, arg3);\n// },\n// });\n\n// useCopilotAction({\n// name: \"myAction\",\n// handler: () => {\n// console.log(\"No parameters provided.\");\n// },\n// });\n"],"mappings":";;;;;AAAA,SAAS,QAAQ,iBAAiB;AAGlC,SAAoB,gBAAgB;AAW7B,SAAS,iBACd,QACA,cACM;AACN,QAAM,EAAE,WAAW,cAAc,SAAS,oBAAoB,IAAI,kBAAkB;AACpF,QAAM,QAAQ,OAAe,SAAS,CAAC;AAKvC,MAAI,iBAAiB,QAAW;AAC9B,QAAI,QAAQ,MAAM,OAAO,GAAG;AAC1B,cAAQ,MAAM,OAAO,EAAE,UAAU,OAAO;AACxC,UAAI,OAAO,OAAO,WAAW,YAAY;AACvC,YAAI,oBAAoB,YAAY,MAAM;AACxC,8BAAoB,QAAQ,QAAQ,OAAO,IAAI,IAAI,OAAO;AAAA,QAC5D;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,YAAU,MAAM;AACd,QAAI,OAAO,UAAU;AACnB;AAAA,IACF;AACA,cAAU,MAAM,SAAS,MAAa;AACtC,QAAI,oBAAoB,YAAY,QAAQ,OAAO,WAAW,QAAW;AACvE,0BAAoB,QAAQ,QAAQ,OAAO,IAAI,IAAI,OAAO;AAAA,IAC5D;AACA,WAAO,MAAM;AAGX,mBAAa,MAAM,OAAO;AAAA,IAC5B;AAAA,EACF,GAAG;AAAA,IACD;AAAA,IACA;AAAA,IACA,OAAO;AAAA,IACP,OAAO;AAAA,IACP,OAAO;AAAA;AAAA;AAAA,IAGP,KAAK,UAAU,OAAO,UAAU;AAAA;AAAA,IAEhC,OAAO,OAAO,WAAW,WAAW,OAAO,SAAS;AAAA;AAAA,IAEpD,GAAI,gBAAgB,CAAC;AAAA,EACvB,CAAC;AACH;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=chunk-BWYAGPEF.mjs.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
useCopilotContext
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-J2YXDQHR.mjs";
|
|
4
4
|
|
|
5
5
|
// src/hooks/use-copilot-readable.ts
|
|
6
6
|
import { useEffect, useRef } from "react";
|
|
@@ -25,4 +25,4 @@ function useCopilotReadable({ description, value, parentId, categories, convert
|
|
|
25
25
|
export {
|
|
26
26
|
useCopilotReadable
|
|
27
27
|
};
|
|
28
|
-
//# sourceMappingURL=chunk-
|
|
28
|
+
//# sourceMappingURL=chunk-CODXG6KU.mjs.map
|
|
@@ -10,7 +10,12 @@ var emptyCopilotContext = {
|
|
|
10
10
|
},
|
|
11
11
|
removeAction: () => {
|
|
12
12
|
},
|
|
13
|
-
|
|
13
|
+
coagentActions: {},
|
|
14
|
+
setCoagentAction: () => {
|
|
15
|
+
},
|
|
16
|
+
removeCoagentAction: () => {
|
|
17
|
+
},
|
|
18
|
+
chatComponentsCache: { current: { actions: {}, coagentActions: {} } },
|
|
14
19
|
getContextString: (documents, categories) => returnAndThrowInDebug(""),
|
|
15
20
|
addContext: () => "",
|
|
16
21
|
removeContext: () => {
|
|
@@ -43,7 +48,13 @@ var emptyCopilotContext = {
|
|
|
43
48
|
},
|
|
44
49
|
removeChatSuggestionConfiguration: () => {
|
|
45
50
|
},
|
|
46
|
-
showDevConsole: "auto"
|
|
51
|
+
showDevConsole: "auto",
|
|
52
|
+
coagentStates: {},
|
|
53
|
+
setCoagentStates: () => {
|
|
54
|
+
},
|
|
55
|
+
agentSession: null,
|
|
56
|
+
setAgentSession: () => {
|
|
57
|
+
}
|
|
47
58
|
};
|
|
48
59
|
var CopilotContext = React.createContext(emptyCopilotContext);
|
|
49
60
|
function useCopilotContext() {
|
|
@@ -62,4 +73,4 @@ export {
|
|
|
62
73
|
CopilotContext,
|
|
63
74
|
useCopilotContext
|
|
64
75
|
};
|
|
65
|
-
//# sourceMappingURL=chunk-
|
|
76
|
+
//# sourceMappingURL=chunk-J2YXDQHR.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/context/copilot-context.tsx"],"sourcesContent":["import { CopilotCloudConfig, FunctionCallHandler } from \"@copilotkit/shared\";\nimport { Message } from \"@copilotkit/runtime-client-gql\";\nimport { ActionRenderProps, FrontendAction } from \"../types/frontend-action\";\nimport React from \"react\";\nimport { TreeNodeId } from \"../hooks/use-tree\";\nimport { DocumentPointer } from \"../types\";\nimport { CopilotChatSuggestionConfiguration } from \"../types/chat-suggestion-configuration\";\nimport { CoagentAction, CoagentActionRenderProps } from \"../types/coagent-action\";\nimport { CoagentState } from \"../types/coagent-state\";\n\n/**\n * Interface for the configuration of the Copilot API.\n */\nexport interface CopilotApiConfig {\n /**\n * The public API key for Copilot Cloud.\n */\n publicApiKey?: string;\n\n /**\n * The configuration for Copilot Cloud.\n */\n cloud?: CopilotCloudConfig;\n\n /**\n * The endpoint for the chat API.\n */\n chatApiEndpoint: string;\n\n /**\n * The endpoint for the Copilot transcribe audio service.\n */\n transcribeAudioUrl?: string;\n\n /**\n * The endpoint for the Copilot text to speech service.\n */\n textToSpeechUrl?: string;\n\n /**\n * additional headers to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'Authorization': 'Bearer your_token_here'\n * }\n * ```\n */\n headers: Record<string, string>;\n\n /**\n * Custom properties to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'user_id': 'user_id'\n * }\n * ```\n */\n properties?: Record<string, any>;\n\n /**\n * Indicates whether the user agent should send or receive cookies from the other domain\n * in the case of cross-origin requests.\n */\n credentials?: RequestCredentials;\n}\n\nexport type InChatRenderFunction = (props: ActionRenderProps<any>) => string | JSX.Element;\nexport type CoagentInChatRenderFunction = (\n props: CoagentActionRenderProps<any>,\n) => string | JSX.Element;\n\nexport interface ChatComponentsCache {\n actions: Record<string, InChatRenderFunction | string>;\n coagentActions: Record<string, CoagentInChatRenderFunction | string>;\n}\n\nexport interface AgentSession {\n agentName: string;\n threadId?: string;\n nodeName?: string;\n}\n\nexport interface CopilotContextParams {\n // function-calling\n actions: Record<string, FrontendAction<any>>;\n setAction: (id: string, action: FrontendAction<any>) => void;\n removeAction: (id: string) => void;\n\n // coagent actions\n coagentActions: Record<string, CoagentAction<any>>;\n setCoagentAction: (id: string, action: CoagentAction<any>) => void;\n removeCoagentAction: (id: string) => void;\n\n chatComponentsCache: React.RefObject<ChatComponentsCache>;\n\n getFunctionCallHandler: (\n customEntryPoints?: Record<string, FrontendAction<any>>,\n ) => FunctionCallHandler;\n\n // text context\n addContext: (context: string, parentId?: string, categories?: string[]) => TreeNodeId;\n removeContext: (id: TreeNodeId) => void;\n getContextString: (documents: DocumentPointer[], categories: string[]) => string;\n\n // document context\n addDocumentContext: (documentPointer: DocumentPointer, categories?: string[]) => TreeNodeId;\n removeDocumentContext: (documentId: string) => void;\n getDocumentsContext: (categories: string[]) => DocumentPointer[];\n\n // chat\n messages: Message[];\n setMessages: React.Dispatch<React.SetStateAction<Message[]>>;\n\n isLoading: boolean;\n setIsLoading: React.Dispatch<React.SetStateAction<boolean>>;\n\n chatSuggestionConfiguration: { [key: string]: CopilotChatSuggestionConfiguration };\n addChatSuggestionConfiguration: (\n id: string,\n suggestion: CopilotChatSuggestionConfiguration,\n ) => void;\n removeChatSuggestionConfiguration: (id: string) => void;\n\n chatInstructions: string;\n setChatInstructions: React.Dispatch<React.SetStateAction<string>>;\n\n // api endpoints\n copilotApiConfig: CopilotApiConfig;\n\n showDevConsole: boolean | \"auto\";\n\n // agents\n coagentStates: Record<string, CoagentState>;\n setCoagentStates: React.Dispatch<React.SetStateAction<Record<string, CoagentState>>>;\n agentSession: AgentSession | null;\n setAgentSession: React.Dispatch<React.SetStateAction<AgentSession | null>>;\n}\n\nconst emptyCopilotContext: CopilotContextParams = {\n actions: {},\n setAction: () => {},\n removeAction: () => {},\n\n coagentActions: {},\n setCoagentAction: () => {},\n removeCoagentAction: () => {},\n\n chatComponentsCache: { current: { actions: {}, coagentActions: {} } },\n getContextString: (documents: DocumentPointer[], categories: string[]) =>\n returnAndThrowInDebug(\"\"),\n addContext: () => \"\",\n removeContext: () => {},\n\n getFunctionCallHandler: () => returnAndThrowInDebug(async () => {}),\n\n messages: [],\n setMessages: () => returnAndThrowInDebug([]),\n\n isLoading: false,\n setIsLoading: () => returnAndThrowInDebug(false),\n\n chatInstructions: \"\",\n setChatInstructions: () => returnAndThrowInDebug(\"\"),\n\n getDocumentsContext: (categories: string[]) => returnAndThrowInDebug([]),\n addDocumentContext: () => returnAndThrowInDebug(\"\"),\n removeDocumentContext: () => {},\n\n copilotApiConfig: new (class implements CopilotApiConfig {\n get chatApiEndpoint(): string {\n throw new Error(\"Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!\");\n }\n\n get headers(): Record<string, string> {\n return {};\n }\n get body(): Record<string, any> {\n return {};\n }\n })(),\n\n chatSuggestionConfiguration: {},\n addChatSuggestionConfiguration: () => {},\n removeChatSuggestionConfiguration: () => {},\n showDevConsole: \"auto\",\n coagentStates: {},\n setCoagentStates: () => {},\n\n agentSession: null,\n setAgentSession: () => {},\n};\n\nexport const CopilotContext = React.createContext<CopilotContextParams>(emptyCopilotContext);\n\nexport function useCopilotContext(): CopilotContextParams {\n const context = React.useContext(CopilotContext);\n if (context === emptyCopilotContext) {\n throw new Error(\"Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!\");\n }\n return context;\n}\n\nfunction returnAndThrowInDebug<T>(value: T): T {\n throw new Error(\"Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!\");\n return value;\n}\n"],"mappings":";;;;;AAGA,OAAO,WAAW;AA2IlB,IAAM,sBAA4C;AAAA,EAChD,SAAS,CAAC;AAAA,EACV,WAAW,MAAM;AAAA,EAAC;AAAA,EAClB,cAAc,MAAM;AAAA,EAAC;AAAA,EAErB,gBAAgB,CAAC;AAAA,EACjB,kBAAkB,MAAM;AAAA,EAAC;AAAA,EACzB,qBAAqB,MAAM;AAAA,EAAC;AAAA,EAE5B,qBAAqB,EAAE,SAAS,EAAE,SAAS,CAAC,GAAG,gBAAgB,CAAC,EAAE,EAAE;AAAA,EACpE,kBAAkB,CAAC,WAA8B,eAC/C,sBAAsB,EAAE;AAAA,EAC1B,YAAY,MAAM;AAAA,EAClB,eAAe,MAAM;AAAA,EAAC;AAAA,EAEtB,wBAAwB,MAAM,sBAAsB,MAAY;AAAA,EAAC,EAAC;AAAA,EAElE,UAAU,CAAC;AAAA,EACX,aAAa,MAAM,sBAAsB,CAAC,CAAC;AAAA,EAE3C,WAAW;AAAA,EACX,cAAc,MAAM,sBAAsB,KAAK;AAAA,EAE/C,kBAAkB;AAAA,EAClB,qBAAqB,MAAM,sBAAsB,EAAE;AAAA,EAEnD,qBAAqB,CAAC,eAAyB,sBAAsB,CAAC,CAAC;AAAA,EACvE,oBAAoB,MAAM,sBAAsB,EAAE;AAAA,EAClD,uBAAuB,MAAM;AAAA,EAAC;AAAA,EAE9B,kBAAkB,IAAK,MAAkC;AAAA,IACvD,IAAI,kBAA0B;AAC5B,YAAM,IAAI,MAAM,uEAAuE;AAAA,IACzF;AAAA,IAEA,IAAI,UAAkC;AACpC,aAAO,CAAC;AAAA,IACV;AAAA,IACA,IAAI,OAA4B;AAC9B,aAAO,CAAC;AAAA,IACV;AAAA,EACF,EAAG;AAAA,EAEH,6BAA6B,CAAC;AAAA,EAC9B,gCAAgC,MAAM;AAAA,EAAC;AAAA,EACvC,mCAAmC,MAAM;AAAA,EAAC;AAAA,EAC1C,gBAAgB;AAAA,EAChB,eAAe,CAAC;AAAA,EAChB,kBAAkB,MAAM;AAAA,EAAC;AAAA,EAEzB,cAAc;AAAA,EACd,iBAAiB,MAAM;AAAA,EAAC;AAC1B;AAEO,IAAM,iBAAiB,MAAM,cAAoC,mBAAmB;AAEpF,SAAS,oBAA0C;AACxD,QAAM,UAAU,MAAM,WAAW,cAAc;AAC/C,MAAI,YAAY,qBAAqB;AACnC,UAAM,IAAI,MAAM,uEAAuE;AAAA,EACzF;AACA,SAAO;AACT;AAEA,SAAS,sBAAyB,OAAa;AAC7C,QAAM,IAAI,MAAM,uEAAuE;AACvF,SAAO;AACT;","names":[]}
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
} from "./chunk-6U3UH3KO.mjs";
|
|
7
7
|
import {
|
|
8
8
|
CopilotContext
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-J2YXDQHR.mjs";
|
|
10
10
|
import {
|
|
11
11
|
__async,
|
|
12
12
|
__objRest,
|
|
@@ -32,7 +32,11 @@ function CopilotKit(_a) {
|
|
|
32
32
|
}
|
|
33
33
|
const chatApiEndpoint = props.runtimeUrl || COPILOT_CLOUD_CHAT_URL;
|
|
34
34
|
const [actions, setActions] = useState({});
|
|
35
|
-
const
|
|
35
|
+
const [coagentActions, setCoagentActions] = useState({});
|
|
36
|
+
const chatComponentsCache = useRef({
|
|
37
|
+
actions: {},
|
|
38
|
+
coagentActions: {}
|
|
39
|
+
});
|
|
36
40
|
const { addElement, removeElement, printTree } = use_tree_default();
|
|
37
41
|
const [messages, setMessages] = useState([]);
|
|
38
42
|
const [isLoading, setIsLoading] = useState(false);
|
|
@@ -56,6 +60,20 @@ function CopilotKit(_a) {
|
|
|
56
60
|
return newPoints;
|
|
57
61
|
});
|
|
58
62
|
}, []);
|
|
63
|
+
const setCoagentAction = useCallback((id, action) => {
|
|
64
|
+
setCoagentActions((prevPoints) => {
|
|
65
|
+
return __spreadProps(__spreadValues({}, prevPoints), {
|
|
66
|
+
[id]: action
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
}, []);
|
|
70
|
+
const removeCoagentAction = useCallback((id) => {
|
|
71
|
+
setCoagentActions((prevPoints) => {
|
|
72
|
+
const newPoints = __spreadValues({}, prevPoints);
|
|
73
|
+
delete newPoints[id];
|
|
74
|
+
return newPoints;
|
|
75
|
+
});
|
|
76
|
+
}, []);
|
|
59
77
|
const getContextString = useCallback(
|
|
60
78
|
(documents, categories) => {
|
|
61
79
|
const documentsString = documents.map((document) => {
|
|
@@ -146,6 +164,14 @@ ${nonDocumentStrings}`;
|
|
|
146
164
|
return rest;
|
|
147
165
|
});
|
|
148
166
|
};
|
|
167
|
+
const [coagentStates, setCoagentStates] = useState({});
|
|
168
|
+
let initialAgentSession = null;
|
|
169
|
+
if (props.agent) {
|
|
170
|
+
initialAgentSession = {
|
|
171
|
+
agentName: props.agent
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
const [agentSession, setAgentSession] = useState(initialAgentSession);
|
|
149
175
|
return /* @__PURE__ */ jsx(
|
|
150
176
|
CopilotContext.Provider,
|
|
151
177
|
{
|
|
@@ -155,6 +181,9 @@ ${nonDocumentStrings}`;
|
|
|
155
181
|
getFunctionCallHandler,
|
|
156
182
|
setAction,
|
|
157
183
|
removeAction,
|
|
184
|
+
coagentActions,
|
|
185
|
+
setCoagentAction,
|
|
186
|
+
removeCoagentAction,
|
|
158
187
|
getContextString,
|
|
159
188
|
addContext,
|
|
160
189
|
removeContext,
|
|
@@ -171,7 +200,11 @@ ${nonDocumentStrings}`;
|
|
|
171
200
|
removeChatSuggestionConfiguration,
|
|
172
201
|
chatInstructions,
|
|
173
202
|
setChatInstructions,
|
|
174
|
-
showDevConsole: props.showDevConsole === void 0 ? "auto" : props.showDevConsole
|
|
203
|
+
showDevConsole: props.showDevConsole === void 0 ? "auto" : props.showDevConsole,
|
|
204
|
+
coagentStates,
|
|
205
|
+
setCoagentStates,
|
|
206
|
+
agentSession,
|
|
207
|
+
setAgentSession
|
|
175
208
|
},
|
|
176
209
|
children
|
|
177
210
|
}
|
|
@@ -189,8 +222,9 @@ function entryPointsToFunctionCallHandler(actions) {
|
|
|
189
222
|
if (action) {
|
|
190
223
|
yield new Promise((resolve, reject) => {
|
|
191
224
|
flushSync(() => __async(this, null, function* () {
|
|
225
|
+
var _a;
|
|
192
226
|
try {
|
|
193
|
-
result = yield action.handler(args);
|
|
227
|
+
result = yield (_a = action.handler) == null ? void 0 : _a.call(action, args);
|
|
194
228
|
resolve();
|
|
195
229
|
} catch (error) {
|
|
196
230
|
reject(error);
|
|
@@ -207,4 +241,4 @@ export {
|
|
|
207
241
|
CopilotKit,
|
|
208
242
|
defaultCopilotContextCategories
|
|
209
243
|
};
|
|
210
|
-
//# sourceMappingURL=chunk-
|
|
244
|
+
//# sourceMappingURL=chunk-RBNULK3U.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/components/copilot-provider/copilotkit.tsx"],"sourcesContent":["/**\n * This component will typically wrap your entire application (or a sub-tree of your application where you want to have a copilot). It provides the copilot context to all other components and hooks.\n *\n * ## Example\n *\n * <Tabs items={[\"Copilot Cloud (Recommended)\", \"Self-hosted Runtime\"]}>\n * <Tabs.Tab>\n *\n * You can get your Copilot Cloud API key for free by <LinkToCopilotCloud>signing up here</LinkToCopilotCloud>.\n *\n * ```tsx\n * import { CopilotKit } from \"@copilotkit/react-core\";\n *\n * <CopilotKit publicApiKey=\"<your-public-api-key>\">\n * // ... your app ...\n * </CopilotKit>\n * ```\n * </Tabs.Tab>\n *\n * <Tabs.Tab>\n * You can find more information about self-hosting CopilotKit [here](/concepts/copilot-runtime).\n *\n * ```tsx\n * import { CopilotKit } from \"@copilotkit/react-core\";\n *\n * <CopilotKit runtimeUrl=\"<your-runtime-url>\">\n * // ... your app ...\n * </CopilotKit>\n * ```\n * </Tabs.Tab>\n * </Tabs>\n */\nimport { useCallback, useRef, useState } from \"react\";\nimport {\n CopilotContext,\n CopilotApiConfig,\n InChatRenderFunction,\n ChatComponentsCache,\n AgentSession,\n} from \"../../context/copilot-context\";\nimport useTree from \"../../hooks/use-tree\";\nimport { CopilotChatSuggestionConfiguration, DocumentPointer } from \"../../types\";\nimport { flushSync } from \"react-dom\";\nimport {\n COPILOT_CLOUD_CHAT_URL,\n CopilotCloudConfig,\n FunctionCallHandler,\n} from \"@copilotkit/shared\";\nimport { AgentStateMessage, Message } from \"@copilotkit/runtime-client-gql\";\n\nimport { FrontendAction } from \"../../types/frontend-action\";\nimport useFlatCategoryStore from \"../../hooks/use-flat-category-store\";\nimport { CopilotKitProps } from \"./copilotkit-props\";\nimport { CoagentAction } from \"../../types/coagent-action\";\nimport { CoagentState } from \"../../types/coagent-state\";\n\nexport function CopilotKit({ children, ...props }: CopilotKitProps) {\n // Compute all the functions and properties that we need to pass\n // to the CopilotContext.\n\n if (!props.runtimeUrl && !props.publicApiKey) {\n throw new Error(\n \"Please provide either a runtimeUrl or a publicApiKey to the CopilotKit component.\",\n );\n }\n\n const chatApiEndpoint = props.runtimeUrl || COPILOT_CLOUD_CHAT_URL;\n\n const [actions, setActions] = useState<Record<string, FrontendAction<any>>>({});\n const [coagentActions, setCoagentActions] = useState<Record<string, CoagentAction<any>>>({});\n const chatComponentsCache = useRef<ChatComponentsCache>({\n actions: {},\n coagentActions: {},\n });\n const { addElement, removeElement, printTree } = useTree();\n const [messages, setMessages] = useState<Message[]>([]);\n const [isLoading, setIsLoading] = useState(false);\n const [chatInstructions, setChatInstructions] = useState(\"\");\n\n const {\n addElement: addDocument,\n removeElement: removeDocument,\n allElements: allDocuments,\n } = useFlatCategoryStore<DocumentPointer>();\n\n const setAction = useCallback((id: string, action: FrontendAction<any>) => {\n setActions((prevPoints) => {\n return {\n ...prevPoints,\n [id]: action,\n };\n });\n }, []);\n\n const removeAction = useCallback((id: string) => {\n setActions((prevPoints) => {\n const newPoints = { ...prevPoints };\n delete newPoints[id];\n return newPoints;\n });\n }, []);\n\n const setCoagentAction = useCallback((id: string, action: CoagentAction<any>) => {\n setCoagentActions((prevPoints) => {\n return {\n ...prevPoints,\n [id]: action,\n };\n });\n }, []);\n\n const removeCoagentAction = useCallback((id: string) => {\n setCoagentActions((prevPoints) => {\n const newPoints = { ...prevPoints };\n delete newPoints[id];\n return newPoints;\n });\n }, []);\n\n const getContextString = useCallback(\n (documents: DocumentPointer[], categories: string[]) => {\n const documentsString = documents\n .map((document) => {\n return `${document.name} (${document.sourceApplication}):\\n${document.getContents()}`;\n })\n .join(\"\\n\\n\");\n\n const nonDocumentStrings = printTree(categories);\n\n return `${documentsString}\\n\\n${nonDocumentStrings}`;\n },\n [printTree],\n );\n\n const addContext = useCallback(\n (\n context: string,\n parentId?: string,\n categories: string[] = defaultCopilotContextCategories,\n ) => {\n return addElement(context, categories, parentId);\n },\n [addElement],\n );\n\n const removeContext = useCallback(\n (id: string) => {\n removeElement(id);\n },\n [removeElement],\n );\n\n const getFunctionCallHandler = useCallback(\n (customEntryPoints?: Record<string, FrontendAction<any>>) => {\n return entryPointsToFunctionCallHandler(Object.values(customEntryPoints || actions));\n },\n [actions],\n );\n\n const getDocumentsContext = useCallback(\n (categories: string[]) => {\n return allDocuments(categories);\n },\n [allDocuments],\n );\n\n const addDocumentContext = useCallback(\n (documentPointer: DocumentPointer, categories: string[] = defaultCopilotContextCategories) => {\n return addDocument(documentPointer, categories);\n },\n [addDocument],\n );\n\n const removeDocumentContext = useCallback(\n (documentId: string) => {\n removeDocument(documentId);\n },\n [removeDocument],\n );\n\n if (!props.publicApiKey) {\n if (props.cloudRestrictToTopic) {\n throw new Error(\n \"To use the cloudRestrictToTopic feature, please sign up at https://copilotkit.ai and provide a publicApiKey.\",\n );\n }\n }\n\n let cloud: CopilotCloudConfig | undefined = undefined;\n if (props.publicApiKey) {\n cloud = {\n guardrails: {\n input: {\n restrictToTopic: {\n enabled: props.cloudRestrictToTopic ? true : false,\n validTopics: props.cloudRestrictToTopic?.validTopics || [],\n invalidTopics: props.cloudRestrictToTopic?.invalidTopics || [],\n },\n },\n },\n };\n }\n\n // get the appropriate CopilotApiConfig from the props\n const copilotApiConfig: CopilotApiConfig = {\n publicApiKey: props.publicApiKey,\n ...(cloud ? { cloud } : {}),\n chatApiEndpoint: chatApiEndpoint,\n headers: props.headers || {},\n properties: props.properties || {},\n transcribeAudioUrl: props.transcribeAudioUrl,\n textToSpeechUrl: props.textToSpeechUrl,\n credentials: props.credentials,\n };\n\n const [chatSuggestionConfiguration, setChatSuggestionConfiguration] = useState<{\n [key: string]: CopilotChatSuggestionConfiguration;\n }>({});\n\n const addChatSuggestionConfiguration = (\n id: string,\n suggestion: CopilotChatSuggestionConfiguration,\n ) => {\n setChatSuggestionConfiguration((prev) => ({ ...prev, [id]: suggestion }));\n };\n\n const removeChatSuggestionConfiguration = (id: string) => {\n setChatSuggestionConfiguration((prev) => {\n const { [id]: _, ...rest } = prev;\n return rest;\n });\n };\n\n const [coagentStates, setCoagentStates] = useState<Record<string, CoagentState>>({});\n let initialAgentSession: AgentSession | null = null;\n if (props.agent) {\n initialAgentSession = {\n agentName: props.agent,\n };\n }\n\n const [agentSession, setAgentSession] = useState<AgentSession | null>(initialAgentSession);\n\n return (\n <CopilotContext.Provider\n value={{\n actions,\n chatComponentsCache,\n getFunctionCallHandler,\n setAction,\n removeAction,\n coagentActions,\n setCoagentAction,\n removeCoagentAction,\n getContextString,\n addContext,\n removeContext,\n getDocumentsContext,\n addDocumentContext,\n removeDocumentContext,\n copilotApiConfig: copilotApiConfig,\n messages,\n setMessages,\n isLoading,\n setIsLoading,\n chatSuggestionConfiguration,\n addChatSuggestionConfiguration,\n removeChatSuggestionConfiguration,\n chatInstructions,\n setChatInstructions,\n showDevConsole: props.showDevConsole === undefined ? \"auto\" : props.showDevConsole,\n coagentStates,\n setCoagentStates,\n agentSession,\n setAgentSession,\n }}\n >\n {children}\n </CopilotContext.Provider>\n );\n}\n\nexport const defaultCopilotContextCategories = [\"global\"];\n\nfunction entryPointsToFunctionCallHandler(actions: FrontendAction<any>[]): FunctionCallHandler {\n return async ({ messages, name, args }) => {\n let actionsByFunctionName: Record<string, FrontendAction<any>> = {};\n for (let action of actions) {\n actionsByFunctionName[action.name] = action;\n }\n\n const action = actionsByFunctionName[name];\n let result: any = undefined;\n if (action) {\n await new Promise<void>((resolve, reject) => {\n flushSync(async () => {\n try {\n result = await action.handler?.(args);\n resolve();\n } catch (error) {\n reject(error);\n }\n });\n });\n await new Promise((resolve) => setTimeout(resolve, 20));\n }\n return result;\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAgCA,SAAS,aAAa,QAAQ,gBAAgB;AAU9C,SAAS,iBAAiB;AAC1B;AAAA,EACE;AAAA,OAGK;AAqMH;AA5LG,SAAS,WAAW,IAAyC;AAAzC,eAAE,WAxD7B,IAwD2B,IAAe,kBAAf,IAAe,CAAb;AAxD7B,MAAAA,KAAAC;AA4DE,MAAI,CAAC,MAAM,cAAc,CAAC,MAAM,cAAc;AAC5C,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAEA,QAAM,kBAAkB,MAAM,cAAc;AAE5C,QAAM,CAAC,SAAS,UAAU,IAAI,SAA8C,CAAC,CAAC;AAC9E,QAAM,CAAC,gBAAgB,iBAAiB,IAAI,SAA6C,CAAC,CAAC;AAC3F,QAAM,sBAAsB,OAA4B;AAAA,IACtD,SAAS,CAAC;AAAA,IACV,gBAAgB,CAAC;AAAA,EACnB,CAAC;AACD,QAAM,EAAE,YAAY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AAAA,QACA,gBAAgB,MAAM,mBAAmB,SAAY,SAAS,MAAM;AAAA,QACpE;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,MAEC;AAAA;AAAA,EACH;AAEJ;AAEO,IAAM,kCAAkC,CAAC,QAAQ;AAExD,SAAS,iCAAiC,SAAqD;AAC7F,SAAO,CAAO,OAA6B,eAA7B,KAA6B,WAA7B,EAAE,UAAU,MAAM,KAAK,GAAM;AACzC,QAAI,wBAA6D,CAAC;AAClE,aAASE,WAAU,SAAS;AAC1B,4BAAsBA,QAAO,IAAI,IAAIA;AAAA,IACvC;AAEA,UAAM,SAAS,sBAAsB,IAAI;AACzC,QAAI,SAAc;AAClB,QAAI,QAAQ;AACV,YAAM,IAAI,QAAc,CAAC,SAAS,WAAW;AAC3C,kBAAU,MAAY;AAvS9B;AAwSU,cAAI;AACF,qBAAS,OAAM,YAAO,YAAP,gCAAiB;AAChC,oBAAQ;AAAA,UACV,SAAS,OAAP;AACA,mBAAO,KAAK;AAAA,UACd;AAAA,QACF,EAAC;AAAA,MACH,CAAC;AACD,YAAM,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,EAAE,CAAC;AAAA,IACxD;AACA,WAAO;AAAA,EACT;AACF;","names":["_a","_b","action"]}
|
|
@@ -16,11 +16,14 @@ import {
|
|
|
16
16
|
ResultMessage,
|
|
17
17
|
CopilotRuntimeClient,
|
|
18
18
|
convertMessagesToGqlInput,
|
|
19
|
+
filterAdjacentAgentStateMessages,
|
|
20
|
+
filterAgentStateMessages,
|
|
19
21
|
convertGqlOutputToMessages,
|
|
20
22
|
MessageStatusCode,
|
|
21
23
|
MessageRole,
|
|
22
24
|
Role,
|
|
23
|
-
CopilotRequestType
|
|
25
|
+
CopilotRequestType,
|
|
26
|
+
AgentStateMessage
|
|
24
27
|
} from "@copilotkit/runtime-client-gql";
|
|
25
28
|
function useChat(options) {
|
|
26
29
|
const {
|
|
@@ -32,7 +35,12 @@ function useChat(options) {
|
|
|
32
35
|
initialMessages,
|
|
33
36
|
isLoading,
|
|
34
37
|
actions,
|
|
35
|
-
onFunctionCall
|
|
38
|
+
onFunctionCall,
|
|
39
|
+
onCoagentAction,
|
|
40
|
+
setCoagentStates,
|
|
41
|
+
coagentStates,
|
|
42
|
+
agentSession,
|
|
43
|
+
setAgentSession
|
|
36
44
|
} = options;
|
|
37
45
|
const abortControllerRef = useRef();
|
|
38
46
|
const threadIdRef = useRef(null);
|
|
@@ -61,7 +69,7 @@ function useChat(options) {
|
|
|
61
69
|
const messagesWithContext = [systemMessage, ...initialMessages || [], ...previousMessages];
|
|
62
70
|
const stream = CopilotRuntimeClient.asStream(
|
|
63
71
|
runtimeClient.generateCopilotResponse({
|
|
64
|
-
data: __spreadProps(__spreadValues({
|
|
72
|
+
data: __spreadProps(__spreadValues(__spreadProps(__spreadValues({
|
|
65
73
|
frontend: {
|
|
66
74
|
actions: actions.map((action) => ({
|
|
67
75
|
name: action.name,
|
|
@@ -72,7 +80,7 @@ function useChat(options) {
|
|
|
72
80
|
},
|
|
73
81
|
threadId: threadIdRef.current,
|
|
74
82
|
runId: runIdRef.current,
|
|
75
|
-
messages: convertMessagesToGqlInput(messagesWithContext)
|
|
83
|
+
messages: convertMessagesToGqlInput(filterAgentStateMessages(messagesWithContext))
|
|
76
84
|
}, copilotConfig.cloud ? {
|
|
77
85
|
cloud: __spreadValues({}, ((_c = (_b = (_a = copilotConfig.cloud.guardrails) == null ? void 0 : _a.input) == null ? void 0 : _b.restrictToTopic) == null ? void 0 : _c.enabled) ? {
|
|
78
86
|
guardrails: {
|
|
@@ -86,6 +94,13 @@ function useChat(options) {
|
|
|
86
94
|
metadata: {
|
|
87
95
|
requestType: CopilotRequestType.Chat
|
|
88
96
|
}
|
|
97
|
+
}), agentSession ? {
|
|
98
|
+
agentSession
|
|
99
|
+
} : {}), {
|
|
100
|
+
agentStates: Object.values(coagentStates).map((state) => ({
|
|
101
|
+
agentName: state.name,
|
|
102
|
+
state: JSON.stringify(state.state)
|
|
103
|
+
}))
|
|
89
104
|
}),
|
|
90
105
|
properties: copilotConfig.properties,
|
|
91
106
|
signal: (_d = abortControllerRef.current) == null ? void 0 : _d.signal
|
|
@@ -93,7 +108,8 @@ function useChat(options) {
|
|
|
93
108
|
);
|
|
94
109
|
const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
|
|
95
110
|
const reader = stream.getReader();
|
|
96
|
-
let
|
|
111
|
+
let actionResults = {};
|
|
112
|
+
let executedCoagentActions = [];
|
|
97
113
|
try {
|
|
98
114
|
while (true) {
|
|
99
115
|
const { done, value } = yield reader.read();
|
|
@@ -105,7 +121,9 @@ function useChat(options) {
|
|
|
105
121
|
}
|
|
106
122
|
threadIdRef.current = value.generateCopilotResponse.threadId || null;
|
|
107
123
|
runIdRef.current = value.generateCopilotResponse.runId || null;
|
|
108
|
-
const messages2 = convertGqlOutputToMessages(
|
|
124
|
+
const messages2 = convertGqlOutputToMessages(
|
|
125
|
+
filterAdjacentAgentStateMessages(value.generateCopilotResponse.messages)
|
|
126
|
+
);
|
|
109
127
|
if (messages2.length === 0) {
|
|
110
128
|
continue;
|
|
111
129
|
}
|
|
@@ -121,7 +139,7 @@ function useChat(options) {
|
|
|
121
139
|
for (const message of messages2) {
|
|
122
140
|
newMessages.push(message);
|
|
123
141
|
if (message instanceof ActionExecutionMessage && message.status.code !== MessageStatusCode.Pending && message.scope === "client" && onFunctionCall) {
|
|
124
|
-
if (!(message.id in
|
|
142
|
+
if (!(message.id in actionResults)) {
|
|
125
143
|
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
126
144
|
break;
|
|
127
145
|
}
|
|
@@ -130,16 +148,50 @@ function useChat(options) {
|
|
|
130
148
|
name: message.name,
|
|
131
149
|
args: message.arguments
|
|
132
150
|
});
|
|
133
|
-
|
|
151
|
+
actionResults[message.id] = result;
|
|
134
152
|
}
|
|
135
153
|
newMessages.push(
|
|
136
154
|
new ResultMessage({
|
|
137
|
-
result: ResultMessage.encodeResult(
|
|
155
|
+
result: ResultMessage.encodeResult(actionResults[message.id]),
|
|
138
156
|
actionExecutionId: message.id,
|
|
139
157
|
actionName: message.name
|
|
140
158
|
})
|
|
141
159
|
);
|
|
142
160
|
}
|
|
161
|
+
if (message instanceof AgentStateMessage && !message.active && !executedCoagentActions.includes(message.id) && onCoagentAction) {
|
|
162
|
+
if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
yield onCoagentAction({
|
|
166
|
+
name: message.agentName,
|
|
167
|
+
nodeName: message.nodeName,
|
|
168
|
+
state: message.state
|
|
169
|
+
});
|
|
170
|
+
executedCoagentActions.push(message.id);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
const lastAgentStateMessage = [...messages2].reverse().find((message) => message instanceof AgentStateMessage);
|
|
174
|
+
if (lastAgentStateMessage) {
|
|
175
|
+
if (lastAgentStateMessage.running) {
|
|
176
|
+
setCoagentStates((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
177
|
+
[lastAgentStateMessage.agentName]: {
|
|
178
|
+
name: lastAgentStateMessage.agentName,
|
|
179
|
+
state: lastAgentStateMessage.state,
|
|
180
|
+
running: lastAgentStateMessage.running,
|
|
181
|
+
active: lastAgentStateMessage.active,
|
|
182
|
+
threadId: lastAgentStateMessage.threadId,
|
|
183
|
+
nodeName: lastAgentStateMessage.nodeName,
|
|
184
|
+
runId: lastAgentStateMessage.runId
|
|
185
|
+
}
|
|
186
|
+
}));
|
|
187
|
+
setAgentSession({
|
|
188
|
+
threadId: lastAgentStateMessage.threadId,
|
|
189
|
+
agentName: lastAgentStateMessage.agentName,
|
|
190
|
+
nodeName: lastAgentStateMessage.nodeName
|
|
191
|
+
});
|
|
192
|
+
} else {
|
|
193
|
+
setAgentSession(null);
|
|
194
|
+
}
|
|
143
195
|
}
|
|
144
196
|
}
|
|
145
197
|
if (newMessages.length > 0) {
|
|
@@ -148,7 +200,7 @@ function useChat(options) {
|
|
|
148
200
|
}
|
|
149
201
|
if (
|
|
150
202
|
// if we have client side results
|
|
151
|
-
Object.values(
|
|
203
|
+
Object.values(actionResults).length || // or the last message we received is a result
|
|
152
204
|
newMessages.length && newMessages[newMessages.length - 1] instanceof ResultMessage
|
|
153
205
|
) {
|
|
154
206
|
yield new Promise((resolve) => setTimeout(resolve, 10));
|
|
@@ -197,4 +249,4 @@ function useChat(options) {
|
|
|
197
249
|
export {
|
|
198
250
|
useChat
|
|
199
251
|
};
|
|
200
|
-
//# sourceMappingURL=chunk-
|
|
252
|
+
//# sourceMappingURL=chunk-RWPGGPW5.mjs.map
|