@copilotkit/react-core 1.5.15 → 1.5.16-next.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 +16 -0
- package/dist/{chunk-HZCROITJ.mjs → chunk-22ENANUU.mjs} +21 -15
- package/dist/chunk-22ENANUU.mjs.map +1 -0
- package/dist/{chunk-WU53OU3E.mjs → chunk-2VIL3F36.mjs} +2 -2
- package/dist/chunk-2VIL3F36.mjs.map +1 -0
- package/dist/{chunk-UMQQA6HK.mjs → chunk-5K2FUI34.mjs} +2 -2
- package/dist/{chunk-E64HFWRV.mjs → chunk-6ZFOXYPW.mjs} +2 -2
- package/dist/{chunk-AL3MOOKF.mjs → chunk-CUSSPNG5.mjs} +2 -2
- package/dist/{chunk-CQWEXISS.mjs → chunk-F7CT7ISL.mjs} +2 -2
- package/dist/{chunk-VQFN2PUH.mjs → chunk-I26H7OZO.mjs} +3 -3
- package/dist/{chunk-YG5MITQX.mjs → chunk-J3VBYQS4.mjs} +3 -3
- package/dist/{chunk-GANI4ZZM.mjs → chunk-MIUILUVX.mjs} +2 -2
- package/dist/{chunk-NXHL6AOE.mjs → chunk-MOWA6EG3.mjs} +4 -4
- package/dist/{chunk-MUO7UFSI.mjs → chunk-QAOIQER4.mjs} +4 -4
- package/dist/chunk-QAOIQER4.mjs.map +1 -0
- package/dist/{chunk-KBCZA4FC.mjs → chunk-V3UIPF2O.mjs} +4 -4
- package/dist/components/copilot-provider/copilotkit.js +20 -14
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +4 -4
- package/dist/components/copilot-provider/index.js +20 -14
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +4 -4
- package/dist/components/error-boundary/error-boundary.js.map +1 -1
- package/dist/components/error-boundary/error-boundary.mjs +2 -2
- package/dist/components/error-boundary/error-utils.js.map +1 -1
- package/dist/components/error-boundary/error-utils.mjs +1 -1
- package/dist/components/index.js +20 -14
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +4 -4
- package/dist/components/toast/toast-provider.js +20 -14
- package/dist/components/toast/toast-provider.js.map +1 -1
- package/dist/components/toast/toast-provider.mjs +1 -1
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +11 -11
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +3 -3
- package/dist/hooks/use-coagent-state-render.d.ts +1 -6
- package/dist/hooks/use-coagent-state-render.js.map +1 -1
- package/dist/hooks/use-coagent-state-render.mjs +2 -2
- package/dist/hooks/use-coagent.d.ts +2 -2
- package/dist/hooks/use-coagent.js.map +1 -1
- package/dist/hooks/use-coagent.mjs +7 -7
- package/dist/hooks/use-copilot-action.js.map +1 -1
- package/dist/hooks/use-copilot-action.mjs +2 -2
- package/dist/hooks/use-copilot-authenticated-action.js.map +1 -1
- package/dist/hooks/use-copilot-authenticated-action.mjs +3 -3
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +6 -6
- package/dist/hooks/use-copilot-runtime-client.js.map +1 -1
- package/dist/hooks/use-copilot-runtime-client.mjs +2 -2
- package/dist/hooks/use-langgraph-interrupt.js.map +1 -1
- package/dist/hooks/use-langgraph-interrupt.mjs +7 -7
- package/dist/index.js +20 -14
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +12 -12
- package/dist/lib/copilot-task.mjs +5 -5
- package/dist/lib/index.mjs +5 -5
- package/dist/utils/extract.mjs +4 -4
- package/dist/utils/index.mjs +4 -4
- package/package.json +3 -3
- package/src/components/toast/toast-provider.tsx +20 -17
- package/src/hooks/use-coagent-state-render.ts +1 -6
- package/src/hooks/use-coagent.ts +2 -2
- package/dist/chunk-HZCROITJ.mjs.map +0 -1
- package/dist/chunk-MUO7UFSI.mjs.map +0 -1
- package/dist/chunk-WU53OU3E.mjs.map +0 -1
- /package/dist/{chunk-UMQQA6HK.mjs.map → chunk-5K2FUI34.mjs.map} +0 -0
- /package/dist/{chunk-E64HFWRV.mjs.map → chunk-6ZFOXYPW.mjs.map} +0 -0
- /package/dist/{chunk-AL3MOOKF.mjs.map → chunk-CUSSPNG5.mjs.map} +0 -0
- /package/dist/{chunk-CQWEXISS.mjs.map → chunk-F7CT7ISL.mjs.map} +0 -0
- /package/dist/{chunk-VQFN2PUH.mjs.map → chunk-I26H7OZO.mjs.map} +0 -0
- /package/dist/{chunk-YG5MITQX.mjs.map → chunk-J3VBYQS4.mjs.map} +0 -0
- /package/dist/{chunk-GANI4ZZM.mjs.map → chunk-MIUILUVX.mjs.map} +0 -0
- /package/dist/{chunk-NXHL6AOE.mjs.map → chunk-MOWA6EG3.mjs.map} +0 -0
- /package/dist/{chunk-KBCZA4FC.mjs.map → chunk-V3UIPF2O.mjs.map} +0 -0
package/dist/index.mjs
CHANGED
|
@@ -3,35 +3,35 @@ import "./chunk-EFZPSZWO.mjs";
|
|
|
3
3
|
import "./chunk-LUGEI4YQ.mjs";
|
|
4
4
|
import {
|
|
5
5
|
CopilotTask
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-6ZFOXYPW.mjs";
|
|
7
7
|
import "./chunk-IGLITGLC.mjs";
|
|
8
8
|
import {
|
|
9
9
|
useLangGraphInterruptRender
|
|
10
10
|
} from "./chunk-ZAU4EMBM.mjs";
|
|
11
11
|
import {
|
|
12
12
|
useLangGraphInterrupt
|
|
13
|
-
} from "./chunk-
|
|
13
|
+
} from "./chunk-J3VBYQS4.mjs";
|
|
14
14
|
import {
|
|
15
15
|
useMakeCopilotDocumentReadable
|
|
16
16
|
} from "./chunk-XOHLZIZK.mjs";
|
|
17
17
|
import {
|
|
18
18
|
useCoAgentStateRender
|
|
19
|
-
} from "./chunk-
|
|
19
|
+
} from "./chunk-2VIL3F36.mjs";
|
|
20
20
|
import {
|
|
21
21
|
runAgent,
|
|
22
22
|
startAgent,
|
|
23
23
|
stopAgent,
|
|
24
24
|
useCoAgent
|
|
25
|
-
} from "./chunk-
|
|
25
|
+
} from "./chunk-QAOIQER4.mjs";
|
|
26
26
|
import {
|
|
27
27
|
useCopilotAuthenticatedAction_c
|
|
28
|
-
} from "./chunk-
|
|
28
|
+
} from "./chunk-MIUILUVX.mjs";
|
|
29
29
|
import {
|
|
30
30
|
useCopilotAction
|
|
31
|
-
} from "./chunk-
|
|
31
|
+
} from "./chunk-CUSSPNG5.mjs";
|
|
32
32
|
import {
|
|
33
33
|
useCopilotChat
|
|
34
|
-
} from "./chunk-
|
|
34
|
+
} from "./chunk-V3UIPF2O.mjs";
|
|
35
35
|
import {
|
|
36
36
|
useCopilotReadable
|
|
37
37
|
} from "./chunk-7HWSAEOY.mjs";
|
|
@@ -39,8 +39,8 @@ import {
|
|
|
39
39
|
CopilotKit,
|
|
40
40
|
defaultCopilotContextCategories,
|
|
41
41
|
extract
|
|
42
|
-
} from "./chunk-
|
|
43
|
-
import "./chunk-
|
|
42
|
+
} from "./chunk-MOWA6EG3.mjs";
|
|
43
|
+
import "./chunk-5K2FUI34.mjs";
|
|
44
44
|
import "./chunk-5M47BILE.mjs";
|
|
45
45
|
import {
|
|
46
46
|
shouldShowDevConsole
|
|
@@ -58,12 +58,12 @@ import {
|
|
|
58
58
|
CopilotMessagesContext,
|
|
59
59
|
useCopilotMessagesContext
|
|
60
60
|
} from "./chunk-DCTJZ742.mjs";
|
|
61
|
-
import "./chunk-
|
|
61
|
+
import "./chunk-I26H7OZO.mjs";
|
|
62
62
|
import "./chunk-XERJQUHA.mjs";
|
|
63
63
|
import {
|
|
64
64
|
useCopilotRuntimeClient
|
|
65
|
-
} from "./chunk-
|
|
66
|
-
import "./chunk-
|
|
65
|
+
} from "./chunk-F7CT7ISL.mjs";
|
|
66
|
+
import "./chunk-22ENANUU.mjs";
|
|
67
67
|
import "./chunk-O7ARI5CV.mjs";
|
|
68
68
|
import "./chunk-SKC7AJIV.mjs";
|
|
69
69
|
export {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
CopilotTask
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
5
|
-
import "../chunk-
|
|
3
|
+
} from "../chunk-6ZFOXYPW.mjs";
|
|
4
|
+
import "../chunk-MOWA6EG3.mjs";
|
|
5
|
+
import "../chunk-5K2FUI34.mjs";
|
|
6
6
|
import "../chunk-5M47BILE.mjs";
|
|
7
7
|
import "../chunk-MLAS4QUR.mjs";
|
|
8
8
|
import "../chunk-6U3UH3KO.mjs";
|
|
@@ -12,8 +12,8 @@ import "../chunk-D34OH4VN.mjs";
|
|
|
12
12
|
import "../chunk-ZM56S3LJ.mjs";
|
|
13
13
|
import "../chunk-DCTJZ742.mjs";
|
|
14
14
|
import "../chunk-XERJQUHA.mjs";
|
|
15
|
-
import "../chunk-
|
|
16
|
-
import "../chunk-
|
|
15
|
+
import "../chunk-F7CT7ISL.mjs";
|
|
16
|
+
import "../chunk-22ENANUU.mjs";
|
|
17
17
|
import "../chunk-O7ARI5CV.mjs";
|
|
18
18
|
import "../chunk-SKC7AJIV.mjs";
|
|
19
19
|
export {
|
package/dist/lib/index.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import "../chunk-LUGEI4YQ.mjs";
|
|
2
2
|
import {
|
|
3
3
|
CopilotTask
|
|
4
|
-
} from "../chunk-
|
|
5
|
-
import "../chunk-
|
|
6
|
-
import "../chunk-
|
|
4
|
+
} from "../chunk-6ZFOXYPW.mjs";
|
|
5
|
+
import "../chunk-MOWA6EG3.mjs";
|
|
6
|
+
import "../chunk-5K2FUI34.mjs";
|
|
7
7
|
import "../chunk-5M47BILE.mjs";
|
|
8
8
|
import "../chunk-MLAS4QUR.mjs";
|
|
9
9
|
import "../chunk-6U3UH3KO.mjs";
|
|
@@ -13,8 +13,8 @@ import "../chunk-D34OH4VN.mjs";
|
|
|
13
13
|
import "../chunk-ZM56S3LJ.mjs";
|
|
14
14
|
import "../chunk-DCTJZ742.mjs";
|
|
15
15
|
import "../chunk-XERJQUHA.mjs";
|
|
16
|
-
import "../chunk-
|
|
17
|
-
import "../chunk-
|
|
16
|
+
import "../chunk-F7CT7ISL.mjs";
|
|
17
|
+
import "../chunk-22ENANUU.mjs";
|
|
18
18
|
import "../chunk-O7ARI5CV.mjs";
|
|
19
19
|
import "../chunk-SKC7AJIV.mjs";
|
|
20
20
|
export {
|
package/dist/utils/extract.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
extract
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
3
|
+
} from "../chunk-MOWA6EG3.mjs";
|
|
4
|
+
import "../chunk-5K2FUI34.mjs";
|
|
5
5
|
import "../chunk-5M47BILE.mjs";
|
|
6
6
|
import "../chunk-MLAS4QUR.mjs";
|
|
7
7
|
import "../chunk-6U3UH3KO.mjs";
|
|
@@ -10,8 +10,8 @@ import "../chunk-5FHSUKQL.mjs";
|
|
|
10
10
|
import "../chunk-D34OH4VN.mjs";
|
|
11
11
|
import "../chunk-ZM56S3LJ.mjs";
|
|
12
12
|
import "../chunk-DCTJZ742.mjs";
|
|
13
|
-
import "../chunk-
|
|
14
|
-
import "../chunk-
|
|
13
|
+
import "../chunk-F7CT7ISL.mjs";
|
|
14
|
+
import "../chunk-22ENANUU.mjs";
|
|
15
15
|
import "../chunk-O7ARI5CV.mjs";
|
|
16
16
|
import "../chunk-SKC7AJIV.mjs";
|
|
17
17
|
export {
|
package/dist/utils/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
extract
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
3
|
+
} from "../chunk-MOWA6EG3.mjs";
|
|
4
|
+
import "../chunk-5K2FUI34.mjs";
|
|
5
5
|
import "../chunk-5M47BILE.mjs";
|
|
6
6
|
import {
|
|
7
7
|
shouldShowDevConsole
|
|
@@ -12,8 +12,8 @@ import "../chunk-5FHSUKQL.mjs";
|
|
|
12
12
|
import "../chunk-D34OH4VN.mjs";
|
|
13
13
|
import "../chunk-ZM56S3LJ.mjs";
|
|
14
14
|
import "../chunk-DCTJZ742.mjs";
|
|
15
|
-
import "../chunk-
|
|
16
|
-
import "../chunk-
|
|
15
|
+
import "../chunk-F7CT7ISL.mjs";
|
|
16
|
+
import "../chunk-22ENANUU.mjs";
|
|
17
17
|
import "../chunk-O7ARI5CV.mjs";
|
|
18
18
|
import "../chunk-SKC7AJIV.mjs";
|
|
19
19
|
export {
|
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"publishConfig": {
|
|
10
10
|
"access": "public"
|
|
11
11
|
},
|
|
12
|
-
"version": "1.5.
|
|
12
|
+
"version": "1.5.16-next.1",
|
|
13
13
|
"sideEffects": false,
|
|
14
14
|
"main": "./dist/index.js",
|
|
15
15
|
"module": "./dist/index.mjs",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"@scarf/scarf": "^1.3.0",
|
|
44
44
|
"react-markdown": "^8.0.7",
|
|
45
45
|
"untruncate-json": "^0.0.1",
|
|
46
|
-
"@copilotkit/runtime-client-gql": "1.5.
|
|
47
|
-
"@copilotkit/shared": "1.5.
|
|
46
|
+
"@copilotkit/runtime-client-gql": "1.5.16-next.1",
|
|
47
|
+
"@copilotkit/shared": "1.5.16-next.1"
|
|
48
48
|
},
|
|
49
49
|
"keywords": [
|
|
50
50
|
"copilotkit",
|
|
@@ -36,27 +36,30 @@ export function ToastProvider({
|
|
|
36
36
|
children: React.ReactNode;
|
|
37
37
|
}) {
|
|
38
38
|
const [toasts, setToasts] = useState<Toast[]>([]);
|
|
39
|
-
const addToast = useCallback(
|
|
40
|
-
|
|
39
|
+
const addToast = useCallback(
|
|
40
|
+
(toast: PartialBy<Toast, "id">) => {
|
|
41
|
+
// We do not display these errors unless we are in dev mode.
|
|
42
|
+
if (!enabled) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
41
45
|
|
|
42
|
-
|
|
43
|
-
if (currentToasts.find((toast) => toast.id === id)) return currentToasts;
|
|
44
|
-
return [...currentToasts, { ...toast, id }];
|
|
45
|
-
});
|
|
46
|
+
const id = toast.id ?? Math.random().toString(36).substring(2, 9);
|
|
46
47
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}, []);
|
|
48
|
+
setToasts((currentToasts) => {
|
|
49
|
+
if (currentToasts.find((toast) => toast.id === id)) return currentToasts;
|
|
50
|
+
return [...currentToasts, { ...toast, id }];
|
|
51
|
+
});
|
|
53
52
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
53
|
+
if (toast.duration) {
|
|
54
|
+
setTimeout(() => {
|
|
55
|
+
removeToast(id);
|
|
56
|
+
}, toast.duration);
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
[enabled],
|
|
60
|
+
);
|
|
59
61
|
|
|
62
|
+
const addGraphQLErrorsToast = useCallback((errors: GraphQLError[]) => {
|
|
60
63
|
addToast({
|
|
61
64
|
type: "error",
|
|
62
65
|
message: <ErrorToast errors={errors} />,
|
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* <Callout type="info">
|
|
3
|
-
* Usage of this hook assumes some additional setup in your application, for more information
|
|
4
|
-
* on that see the CoAgents <span className="text-blue-500">[Agentic Generative UI documentation](/coagents/chat-ui/render-agent-state)</span>.
|
|
5
|
-
* </Callout>
|
|
6
|
-
*
|
|
7
2
|
* The useCoAgentStateRender hook allows you to render UI components or text based on a Agentic Copilot's state.
|
|
8
3
|
* This is particularly useful for showing intermediate state or progress during Agentic Copilot operations.
|
|
9
4
|
*
|
|
@@ -57,7 +52,7 @@ import { useToast } from "../components/toast/toast-provider";
|
|
|
57
52
|
* useful for showing intermediate state or progress during Agentic Copilot operations.
|
|
58
53
|
* To get started using rendering intermediate state through this hook, checkout the documentation.
|
|
59
54
|
*
|
|
60
|
-
* https://docs.copilotkit.ai/coagents/
|
|
55
|
+
* https://docs.copilotkit.ai/coagents/shared-state/predictive-state-updates
|
|
61
56
|
*/
|
|
62
57
|
|
|
63
58
|
// We implement useCoAgentStateRender dependency handling so that
|
package/src/hooks/use-coagent.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* <Callout type="info">
|
|
3
3
|
* Usage of this hook assumes some additional setup in your application, for more information
|
|
4
|
-
* on that see the CoAgents <span className="text-blue-500">[getting started guide](/coagents/quickstart)</span>.
|
|
4
|
+
* on that see the CoAgents <span className="text-blue-500">[getting started guide](/coagents/quickstart/langgraph)</span>.
|
|
5
5
|
* </Callout>
|
|
6
6
|
* <Frame className="my-12">
|
|
7
7
|
* <img
|
|
@@ -201,7 +201,7 @@ export type HintFunction = (params: HintFunctionParams) => Message | undefined;
|
|
|
201
201
|
* This hook is used to integrate an agent into your application. With its use, you can
|
|
202
202
|
* render and update the state of the agent, allowing for a dynamic and interactive experience.
|
|
203
203
|
* We call these shared state experiences "agentic copilots". To get started using agentic copilots, which
|
|
204
|
-
* we refer to as CoAgents, checkout the documentation at https://docs.copilotkit.ai/coagents/quickstart.
|
|
204
|
+
* we refer to as CoAgents, checkout the documentation at https://docs.copilotkit.ai/coagents/quickstart/langgraph.
|
|
205
205
|
*/
|
|
206
206
|
export function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentReturnType<T> {
|
|
207
207
|
const generalContext = useCopilotContext();
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/components/error-boundary/error-utils.tsx","../src/components/toast/toast-provider.tsx"],"sourcesContent":["import React, { useCallback } from \"react\";\nimport { GraphQLError } from \"@copilotkit/runtime-client-gql\";\nimport { useToast } from \"../toast/toast-provider\";\nimport { ExclamationMarkIcon } from \"../toast/exclamation-mark-icon\";\nimport ReactMarkdown from \"react-markdown\";\n\ninterface OriginalError {\n message?: string;\n stack?: string;\n}\n\nexport function ErrorToast({ errors }: { errors: (Error | GraphQLError)[] }) {\n const errorsToRender = errors.map((error, idx) => {\n const originalError =\n \"extensions\" in error ? (error.extensions?.originalError as undefined | OriginalError) : {};\n const message = originalError?.message ?? error.message;\n const code = \"extensions\" in error ? (error.extensions?.code as string) : null;\n\n return (\n <div\n key={idx}\n style={{\n marginTop: idx === 0 ? 0 : 10,\n marginBottom: 14,\n }}\n >\n <ExclamationMarkIcon style={{ marginBottom: 4 }} />\n\n {code && (\n <div\n style={{\n fontWeight: \"600\",\n marginBottom: 4,\n }}\n >\n Copilot Cloud Error:{\" \"}\n <span style={{ fontFamily: \"monospace\", fontWeight: \"normal\" }}>{code}</span>\n </div>\n )}\n <ReactMarkdown>{message}</ReactMarkdown>\n </div>\n );\n });\n return (\n <div\n style={{\n fontSize: \"13px\",\n maxWidth: \"600px\",\n }}\n >\n {errorsToRender}\n <div style={{ fontSize: \"11px\", opacity: 0.75 }}>\n NOTE: This error only displays during local development.\n </div>\n </div>\n );\n}\n\nexport function useErrorToast() {\n const { addToast } = useToast();\n\n return useCallback(\n (error: (Error | GraphQLError)[]) => {\n const errorId = error\n .map((err) => {\n const message =\n \"extensions\" in err\n ? (err.extensions?.originalError as any)?.message || err.message\n : err.message;\n const stack = err.stack || \"\";\n return btoa(message + stack).slice(0, 32); // Create hash from message + stack\n })\n .join(\"|\");\n\n addToast({\n type: \"error\",\n id: errorId, // Toast libraries typically dedupe by id\n message: <ErrorToast errors={error} />,\n });\n },\n [addToast],\n );\n}\n\nexport function useAsyncCallback<T extends (...args: any[]) => Promise<any>>(\n callback: T,\n deps: Parameters<typeof useCallback>[1],\n) {\n const addErrorToast = useErrorToast();\n return useCallback(async (...args: Parameters<T>) => {\n try {\n return await callback(...args);\n } catch (error) {\n console.error(\"Error in async callback:\", error);\n // @ts-ignore\n addErrorToast([error]);\n throw error;\n }\n }, deps);\n}\n","import { GraphQLError } from \"@copilotkit/runtime-client-gql\";\nimport React, { createContext, useContext, useState, useCallback } from \"react\";\nimport { ErrorToast } from \"../error-boundary/error-utils\";\nimport { PartialBy } from \"@copilotkit/shared\";\n\ninterface Toast {\n id: string;\n message: string | React.ReactNode;\n type: \"info\" | \"success\" | \"warning\" | \"error\";\n duration?: number;\n}\n\ninterface ToastContextValue {\n toasts: Toast[];\n addToast: (toast: PartialBy<Toast, \"id\">) => void;\n addGraphQLErrorsToast: (errors: GraphQLError[]) => void;\n removeToast: (id: string) => void;\n enabled: boolean;\n}\n\nconst ToastContext = createContext<ToastContextValue | undefined>(undefined);\n\nexport function useToast() {\n const context = useContext(ToastContext);\n if (!context) {\n throw new Error(\"useToast must be used within a ToastProvider\");\n }\n return context;\n}\n\nexport function ToastProvider({\n enabled,\n children,\n}: {\n enabled: boolean;\n children: React.ReactNode;\n}) {\n const [toasts, setToasts] = useState<Toast[]>([]);\n const addToast = useCallback((toast: PartialBy<Toast, \"id\">) => {\n const id = toast.id ?? Math.random().toString(36).substring(2, 9);\n\n setToasts((currentToasts) => {\n if (currentToasts.find((toast) => toast.id === id)) return currentToasts;\n return [...currentToasts, { ...toast, id }];\n });\n\n if (toast.duration) {\n setTimeout(() => {\n removeToast(id);\n }, toast.duration);\n }\n }, []);\n\n const addGraphQLErrorsToast = useCallback((errors: GraphQLError[]) => {\n // We do not display these errors unless we are in dev mode.\n // if (!showDevConsole) {\n // return;\n // }\n\n addToast({\n type: \"error\",\n message: <ErrorToast errors={errors} />,\n });\n }, []);\n\n const removeToast = useCallback((id: string) => {\n setToasts((currentToasts) => currentToasts.filter((toast) => toast.id !== id));\n }, []);\n\n const value = {\n toasts,\n addToast,\n addGraphQLErrorsToast,\n removeToast,\n enabled,\n };\n\n return (\n <ToastContext.Provider value={value}>\n <div\n style={{\n position: \"fixed\",\n bottom: \"1rem\",\n left: \"50%\",\n transform: \"translateX(-50%)\",\n zIndex: 50,\n display: \"flex\",\n flexDirection: \"column\",\n gap: \"0.5rem\",\n }}\n >\n {toasts.length > 1 && (\n <div style={{ textAlign: \"right\" }}>\n <button\n onClick={() => setToasts([])}\n style={{\n padding: \"4px 8px\",\n fontSize: \"12px\",\n cursor: \"pointer\",\n background: \"white\",\n border: \"1px solid rgba(0,0,0,0.2)\",\n borderRadius: \"4px\",\n }}\n >\n Close All\n </button>\n </div>\n )}\n {toasts.map((toast) => (\n <Toast\n key={toast.id}\n message={toast.message}\n type={toast.type}\n onClose={() => removeToast(toast.id)}\n />\n ))}\n </div>\n {children}\n </ToastContext.Provider>\n );\n}\n\nfunction Toast({\n message,\n type = \"info\",\n onClose,\n}: {\n message: string | React.ReactNode;\n type: \"info\" | \"success\" | \"warning\" | \"error\";\n onClose: () => void;\n}) {\n const bgColors = {\n info: \"#3b82f6\",\n success: \"#22c55e\",\n warning: \"#eab308\",\n error: \"#ef4444\",\n };\n\n return (\n <div\n style={{\n backgroundColor: bgColors[type],\n color: \"white\",\n padding: \"0.5rem 1.5rem\",\n borderRadius: \"0.25rem\",\n boxShadow: \"0 2px 4px rgba(0,0,0,0.1)\",\n position: \"relative\",\n minWidth: \"200px\",\n }}\n >\n <div>{message}</div>\n <button\n onClick={onClose}\n style={{\n position: \"absolute\",\n top: \"0\",\n right: \"0\",\n background: \"none\",\n border: \"none\",\n color: \"white\",\n cursor: \"pointer\",\n padding: \"0.5rem\",\n fontSize: \"1rem\",\n }}\n >\n ✕\n </button>\n </div>\n );\n}\n"],"mappings":";;;;;;;;;;AAAA,SAAgB,eAAAA,oBAAmB;;;ACCnC,SAAgB,eAAe,YAAY,UAAU,mBAAmB;AA4DzD,cAkBT,YAlBS;AAzCf,IAAM,eAAe,cAA6C,MAAS;AAEpE,SAAS,WAAW;AACzB,QAAM,UAAU,WAAW,YAAY;AACvC,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,MAAM,8CAA8C;AAAA,EAChE;AACA,SAAO;AACT;AAEO,SAAS,cAAc;AAAA,EAC5B;AAAA,EACA;AACF,GAGG;AACD,QAAM,CAAC,QAAQ,SAAS,IAAI,SAAkB,CAA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AK,UAAU,KAAK,EAAE,MAAM,GAAG,EAAE;AAAA,MAC1C,CAAC,EACA,KAAK,GAAG;AAEX,eAAS;AAAA,QACP,MAAM;AAAA,QACN,IAAI;AAAA;AAAA,QACJ,SAAS,gBAAAF,KAAC,cAAW,QAAQ,OAAO;AAAA,MACtC,CAAC;AAAA,IACH;AAAA,IACA,CAAC,QAAQ;AAAA,EACX;AACF;AAEO,SAAS,iBACd,UACA,MACA;AACA,QAAM,gBAAgB,cAAc;AACpC,SAAOE,aAAY,IAAU,SAAwB;AACnD,QAAI;AACF,aAAO,MAAM,SAAS,GAAG,IAAI;AAAA,IAC/B,SAAS,OAAP;AACA,cAAQ,MAAM,4BAA4B,KAAK;AAE/C,oBAAc,CAAC,KAAK,CAAC;AACrB,YAAM;AAAA,IACR;AAAA,EACF,IAAG,IAAI;AACT;","names":["useCallback","toast","jsx","jsxs","useCallback"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/hooks/use-coagent.ts"],"sourcesContent":["/**\n * <Callout type=\"info\">\n * Usage of this hook assumes some additional setup in your application, for more information\n * on that see the CoAgents <span className=\"text-blue-500\">[getting started guide](/coagents/quickstart)</span>.\n * </Callout>\n * <Frame className=\"my-12\">\n * <img\n * src=\"/images/coagents/SharedStateCoAgents.gif\"\n * alt=\"CoAgents demonstration\"\n * className=\"w-auto\"\n * />\n * </Frame>\n *\n * This hook is used to integrate an agent into your application. With its use, you can\n * render and update the state of an agent, allowing for a dynamic and interactive experience.\n * We call these shared state experiences agentic copilots, or CoAgents for short.\n *\n * ## Usage\n *\n * ### Simple Usage\n *\n * ```tsx\n * import { useCoAgent } from \"@copilotkit/react-core\";\n *\n * type AgentState = {\n * count: number;\n * }\n *\n * const agent = useCoAgent<AgentState>({\n * name: \"my-agent\",\n * initialState: {\n * count: 0,\n * },\n * });\n *\n * ```\n *\n * `useCoAgent` returns an object with the following properties:\n *\n * ```tsx\n * const {\n * name, // The name of the agent currently being used.\n * nodeName, // The name of the current LangGraph node.\n * state, // The current state of the agent.\n * setState, // A function to update the state of the agent.\n * running, // A boolean indicating if the agent is currently running.\n * start, // A function to start the agent.\n * stop, // A function to stop the agent.\n * run, // A function to re-run the agent. Takes a HintFunction to inform the agent why it is being re-run.\n * } = agent;\n * ```\n *\n * Finally we can leverage these properties to create reactive experiences with the agent!\n *\n * ```tsx\n * const { state, setState } = useCoAgent<AgentState>({\n * name: \"my-agent\",\n * initialState: {\n * count: 0,\n * },\n * });\n *\n * return (\n * <div>\n * <p>Count: {state.count}</p>\n * <button onClick={() => setState({ count: state.count + 1 })}>Increment</button>\n * </div>\n * );\n * ```\n *\n * This reactivity is bidirectional, meaning that changes to the state from the agent will be reflected in the UI and vice versa.\n *\n * ## Parameters\n * <PropertyReference name=\"options\" type=\"UseCoagentOptions<T>\" required>\n * The options to use when creating the coagent.\n * <PropertyReference name=\"name\" type=\"string\" required>\n * The name of the agent to use.\n * </PropertyReference>\n * <PropertyReference name=\"initialState\" type=\"T | any\">\n * The initial state of the agent.\n * </PropertyReference>\n * <PropertyReference name=\"state\" type=\"T | any\">\n * State to manage externally if you are using this hook with external state management.\n * </PropertyReference>\n * <PropertyReference name=\"setState\" type=\"(newState: T | ((prevState: T | undefined) => T)) => void\">\n * A function to update the state of the agent if you are using this hook with external state management.\n * </PropertyReference>\n * </PropertyReference>\n */\n\nimport { useEffect, useRef } from \"react\";\nimport {\n CopilotContextParams,\n CopilotMessagesContextParams,\n useCopilotContext,\n useCopilotMessagesContext,\n} from \"../context\";\nimport { CoagentState } from \"../types/coagent-state\";\nimport { useCopilotChat } from \"./use-copilot-chat\";\nimport { Message } from \"@copilotkit/runtime-client-gql\";\nimport { useAsyncCallback } from \"../components/error-boundary/error-utils\";\nimport { useToast } from \"../components/toast/toast-provider\";\nimport { useCopilotRuntimeClient } from \"./use-copilot-runtime-client\";\n\ninterface WithInternalStateManagementAndInitial<T> {\n /**\n * The name of the agent being used.\n */\n name: string;\n /**\n * The initial state of the agent.\n */\n initialState: T;\n}\n\ninterface WithInternalStateManagement {\n /**\n * The name of the agent being used.\n */\n name: string;\n /**\n * Optional initialState with default type any\n */\n initialState?: any;\n}\n\ninterface WithExternalStateManagement<T> {\n /**\n * The name of the agent being used.\n */\n name: string;\n /**\n * The current state of the agent.\n */\n state: T;\n /**\n * A function to update the state of the agent.\n */\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 /**\n * The name of the agent being used.\n */\n name: string;\n /**\n * The name of the current LangGraph node.\n */\n nodeName?: string;\n /**\n * The ID of the thread the agent is running in.\n */\n threadId?: string;\n /**\n * A boolean indicating if the agent is currently running.\n */\n running: boolean;\n /**\n * The current state of the agent.\n */\n state: T;\n /**\n * A function to update the state of the agent.\n */\n setState: (newState: T | ((prevState: T | undefined) => T)) => void;\n /**\n * A function to start the agent.\n */\n start: () => void;\n /**\n * A function to stop the agent.\n */\n stop: () => void;\n /**\n * A function to re-run the agent. The hint function can be used to provide a hint to the agent\n * about why it is being re-run again.\n */\n run: (hint?: HintFunction) => Promise<void>;\n}\n\nexport interface HintFunctionParams {\n /**\n * The previous state of the agent.\n */\n previousState: any;\n /**\n * The current state of the agent.\n */\n currentState: any;\n}\n\nexport type HintFunction = (params: HintFunctionParams) => Message | undefined;\n\n/**\n * This hook is used to integrate an agent into your application. With its use, you can\n * render and update the state of the agent, allowing for a dynamic and interactive experience.\n * We call these shared state experiences \"agentic copilots\". To get started using agentic copilots, which\n * we refer to as CoAgents, checkout the documentation at https://docs.copilotkit.ai/coagents/quickstart.\n */\nexport function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentReturnType<T> {\n const generalContext = useCopilotContext();\n const { availableAgents } = generalContext;\n const { addToast } = useToast();\n const lastLoadedThreadId = useRef<string>();\n const lastLoadedState = useRef<any>();\n\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 useEffect(() => {\n if (availableAgents?.length && !availableAgents.some((a) => a.name === name)) {\n const message = `(useCoAgent): Agent \"${name}\" not found. Make sure the agent exists and is properly configured.`;\n console.warn(message);\n addToast({ type: \"warning\", message });\n }\n }, [availableAgents]);\n\n const isInternalStateManagementWithInitial = (\n options: UseCoagentOptions<T>,\n ): options is WithInternalStateManagementAndInitial<T> => {\n return \"initialState\" in options;\n };\n\n const messagesContext = useCopilotMessagesContext();\n const context = { ...generalContext, ...messagesContext };\n const { coagentStates, coagentStatesRef, setCoagentStatesWithRef, threadId, copilotApiConfig } =\n context;\n const { appendMessage, runChatCompletion } = useCopilotChat();\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 const runtimeClient = useCopilotRuntimeClient({\n url: copilotApiConfig.chatApiEndpoint,\n publicApiKey: copilotApiConfig.publicApiKey,\n credentials: copilotApiConfig.credentials,\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 let coagentState: CoagentState = getCoagentState(coagentStatesRef.current || {}, name);\n const updatedState =\n typeof newState === \"function\" ? (newState as Function)(coagentState.state) : newState;\n\n setCoagentStatesWithRef({\n ...coagentStatesRef.current,\n [name]: {\n ...coagentState,\n state: updatedState,\n },\n });\n };\n\n useEffect(() => {\n const fetchAgentState = async () => {\n if (!threadId || threadId === lastLoadedThreadId.current) return;\n\n const result = await runtimeClient.loadAgentState({\n threadId,\n agentName: name,\n });\n\n const newState = result.data?.loadAgentState?.state;\n if (newState === lastLoadedState.current) return;\n\n if (result.data?.loadAgentState?.threadExists && newState && newState != \"{}\") {\n lastLoadedState.current = newState;\n lastLoadedThreadId.current = threadId;\n const fetchedState = JSON.parse(newState);\n isExternalStateManagement(options)\n ? options.setState(fetchedState)\n : setState(fetchedState);\n }\n };\n void fetchAgentState();\n }, [threadId]);\n\n const coagentState = getCoagentState(coagentStates, name);\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 }, [\n isExternalStateManagement(options) ? JSON.stringify(options.state) : undefined,\n // reset initialstate on reset\n coagentStates[name] === undefined,\n ]);\n\n const runAgentCallback = useAsyncCallback(\n async (hint?: HintFunction) => {\n await runAgent(name, context, appendMessage, runChatCompletion, hint);\n },\n [name, context, appendMessage, runChatCompletion],\n );\n\n // Return the state and setState function\n return {\n name,\n nodeName: coagentState.nodeName,\n threadId: coagentState.threadId,\n running: coagentState.running,\n state: coagentState.state,\n setState: isExternalStateManagement(options) ? options.setState : setState,\n start: () => startAgent(name, context),\n stop: () => stopAgent(name, context),\n run: runAgentCallback,\n };\n}\n\nexport function startAgent(name: string, context: CopilotContextParams) {\n const { setAgentSession } = context;\n setAgentSession({\n agentName: name,\n });\n}\n\nexport function stopAgent(name: string, context: CopilotContextParams) {\n const { agentSession, setAgentSession } = context;\n if (agentSession && agentSession.agentName === name) {\n setAgentSession(null);\n context.setCoagentStates((prevAgentStates) => {\n return {\n ...prevAgentStates,\n [name]: {\n ...prevAgentStates[name],\n running: false,\n active: false,\n threadId: undefined,\n nodeName: undefined,\n runId: undefined,\n },\n };\n });\n } else {\n console.warn(`No agent session found for ${name}`);\n }\n}\n\nexport async function runAgent(\n name: string,\n context: CopilotContextParams & CopilotMessagesContextParams,\n appendMessage: (message: Message) => Promise<void>,\n runChatCompletion: () => Promise<Message[]>,\n hint?: HintFunction,\n) {\n const { agentSession, setAgentSession } = context;\n if (!agentSession || agentSession.agentName !== name) {\n setAgentSession({\n agentName: name,\n });\n }\n\n let previousState: any = null;\n for (let i = context.messages.length - 1; i >= 0; i--) {\n const message = context.messages[i];\n if (message.isAgentStateMessage() && message.agentName === name) {\n previousState = message.state;\n }\n }\n\n let state = context.coagentStatesRef.current?.[name]?.state || {};\n\n if (hint) {\n const hintMessage = hint({ previousState, currentState: state });\n if (hintMessage) {\n await appendMessage(hintMessage);\n } else {\n await runChatCompletion();\n }\n } else {\n await runChatCompletion();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA0FA,SAAS,WAAW,cAAc;AAmH3B,SAAS,WAAoB,SAAwD;AAC1F,QAAM,iBAAiB,kBAAkB;AACzC,QAAM,EAAE,gBAAgB,IAAI;AAC5B,QAAM,EAAE,SAAS,IAAI,SAAS;AAC9B,QAAM,qBAAqB,OAAe;AAC1C,QAAM,kBAAkB,OAAY;AAEpC,QAAM,4BAA4B,CAChCA,aAC8C;AAC9C,WAAO,WAAWA,YAAW,cAAcA;AAAA,EAC7C;AAEA,QAAM,EAAE,KAAK,IAAI;AACjB,YAAU,MAAM;AACd,SAAI,mDAAiB,WAAU,CAAC,gBAAgB,KAAK,CAAC,MAAM,EAAE,SAAS,IAAI,GAAG;AAC5E,YAAM,UAAU,wBAAwB;AACxC,cAAQ,KAAK,OAAO;AACpB,eAAS,EAAE,MAAM,WAAW,QAAQ,CAAC;AAAA,IACvC;AAAA,EACF,GAAG,CAAC,eAAe,CAAC;AAEpB,QAAM,uCAAuC,CAC3CA,aACwD;AACxD,WAAO,kBAAkBA;AAAA,EAC3B;AAEA,QAAM,kBAAkB,0BAA0B;AAClD,QAAM,UAAU,kCAAK,iBAAmB;AACxC,QAAM,EAAE,eAAe,kBAAkB,yBAAyB,UAAU,iBAAiB,IAC3F;AACF,QAAM,EAAE,eAAe,kBAAkB,IAAI,eAAe;AAE5D,QAAM,kBAAkB,CAACC,gBAA6CC,UAAiB;AACrF,QAAID,eAAcC,KAAI,GAAG;AACvB,aAAOD,eAAcC,KAAI;AAAA,IAC3B,OA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AAO;AAAA,QACT;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH,OAAO;AACL,YAAQ,KAAK,8BAA8B,MAAM;AAAA,EACnD;AACF;AAEA,SAAsB,SACpB,MACA,SACA,eACA,mBACA,MACA;AAAA;AAnXF;AAoXE,UAAM,EAAE,cAAc,gBAAgB,IAAI;AAC1C,QAAI,CAAC,gBAAgB,aAAa,cAAc,MAAM;AACpD,sBAAgB;AAAA,QACd,WAAW;AAAA,MACb,CAAC;AAAA,IACH;AAEA,QAAI,gBAAqB;AACzB,aAAS,IAAI,QAAQ,SAAS,SAAS,GAAG,KAAK,GAAG,KAAK;AACrD,YAAM,UAAU,QAAQ,SAAS,CAAC;AAClC,UAAI,QAAQ,oBAAoB,KAAK,QAAQ,cAAc,MAAM;AAC/D,wBAAgB,QAAQ;AAAA,MAC1B;AAAA,IACF;AAEA,QAAI,UAAQ,mBAAQ,iBAAiB,YAAzB,mBAAmC,UAAnC,mBAA0C,UAAS,CAAC;AAEhE,QAAI,MAAM;AACR,YAAM,cAAc,KAAK,EAAE,eAAe,cAAc,MAAM,CAAC;AAC/D,UAAI,aAAa;AACf,cAAM,cAAc,WAAW;AAAA,MACjC,OAAO;AACL,cAAM,kBAAkB;AAAA,MAC1B;AAAA,IACF,OAAO;AACL,YAAM,kBAAkB;AAAA,IAC1B;AAAA,EACF;AAAA;","names":["options","coagentStates","name","coagentState"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/hooks/use-coagent-state-render.ts"],"sourcesContent":["/**\n * <Callout type=\"info\">\n * Usage of this hook assumes some additional setup in your application, for more information\n * on that see the CoAgents <span className=\"text-blue-500\">[Agentic Generative UI documentation](/coagents/chat-ui/render-agent-state)</span>.\n * </Callout>\n *\n * The useCoAgentStateRender hook allows you to render UI components or text based on a Agentic Copilot's state.\n * This is particularly useful for showing intermediate state or progress during Agentic Copilot operations.\n *\n * ## Usage\n *\n * ### Simple Usage\n *\n * ```tsx\n * import { useCoAgentStateRender } from \"@copilotkit/react-core\";\n *\n * type YourAgentState = {\n * agent_state_property: string;\n * }\n *\n * useCoAgentStateRender<YourAgentState>({\n * name: \"basic_agent\",\n * nodeName: \"optionally_specify_a_specific_node\",\n * render: ({ status, state, nodeName }) => {\n * return (\n * <YourComponent\n * agentStateProperty={state.agent_state_property}\n * status={status}\n * nodeName={nodeName}\n * />\n * );\n * },\n * });\n * ```\n *\n * This allows for you to render UI components or text based on what is happening within the agent.\n *\n * ### Example\n * A great example of this is in our Perplexity Clone where we render the progress of an agent's internet search as it is happening.\n * You can play around with it below or learn how to build it with its [demo](/coagents/videos/perplexity-clone).\n *\n * <Callout type=\"info\">\n * This example is hosted on Vercel and may take a few seconds to load.\n * </Callout>\n *\n * <iframe src=\"https://examples-coagents-ai-researcher-ui.vercel.app/\" className=\"w-full rounded-lg border h-[700px] my-4\" />\n */\n\nimport { useRef, useContext, useEffect } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\nimport { randomId } from \"@copilotkit/shared\";\nimport { CoAgentStateRender } from \"../types/coagent-action\";\nimport { useToast } from \"../components/toast/toast-provider\";\n\n/**\n * This hook is used to render agent state with custom UI components or text. This is particularly\n * useful for showing intermediate state or progress during Agentic Copilot operations.\n * To get started using rendering intermediate state through this hook, checkout the documentation.\n *\n * https://docs.copilotkit.ai/coagents/chat-ui/render-agent-state.\n */\n\n// We implement useCoAgentStateRender dependency handling so that\n// the developer has the option to not provide any dependencies.\n// see useCopilotAction for more details about this approach.\nexport function useCoAgentStateRender<T = any>(\n action: CoAgentStateRender<T>,\n dependencies?: any[],\n): void {\n const {\n setCoAgentStateRender,\n removeCoAgentStateRender,\n coAgentStateRenders,\n chatComponentsCache,\n availableAgents,\n } = useContext(CopilotContext);\n const idRef = useRef<string>(randomId());\n const { addToast } = useToast();\n\n useEffect(() => {\n if (availableAgents?.length && !availableAgents.some((a) => a.name === action.name)) {\n const message = `(useCoAgentStateRender): Agent \"${action.name}\" not found. Make sure the agent exists and is properly configured.`;\n console.warn(message);\n addToast({ type: \"warning\", message });\n }\n }, [availableAgents]);\n\n const key = `${action.name}-${action.nodeName || \"global\"}`;\n\n if (dependencies === undefined) {\n if (coAgentStateRenders[idRef.current]) {\n coAgentStateRenders[idRef.current].handler = action.handler as any;\n if (typeof action.render === \"function\") {\n if (chatComponentsCache.current !== null) {\n chatComponentsCache.current.coAgentStateRenders[key] = action.render;\n }\n }\n }\n }\n\n useEffect(() => {\n setCoAgentStateRender(idRef.current, action as any);\n if (chatComponentsCache.current !== null && action.render !== undefined) {\n chatComponentsCache.current.coAgentStateRenders[key] = action.render;\n }\n return () => {\n removeCoAgentStateRender(idRef.current);\n };\n }, [\n setCoAgentStateRender,\n removeCoAgentStateRender,\n action.name,\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"],"mappings":";;;;;;;;AAgDA,SAAS,QAAQ,YAAY,iBAAiB;AAE9C,SAAS,gBAAgB;AAelB,SAAS,sBACd,QACA,cACM;AACN,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI,WAAW,cAAc;AAC7B,QAAM,QAAQ,OAAe,SAAS,CAAC;AACvC,QAAM,EAAE,SAAS,IAAI,SAAS;AAE9B,YAAU,MAAM;AACd,SAAI,mDAAiB,WAAU,CAAC,gBAAgB,KAAK,CAAC,MAAM,EAAE,SAAS,OAAO,IAAI,GAAG;AACnF,YAAM,UAAU,mCAAmC,OAAO;AAC1D,cAAQ,KAAK,OAAO;AACpB,eAAS,EAAE,MAAM,WAAW,QAAQ,CAAC;AAAA,IACvC;AAAA,EACF,GAAG,CAAC,eAAe,CAAC;AAEpB,QAAM,MAAM,GAAG,OAAO,QAAQ,OAAO,YAAY;AAEjD,MAAI,iBAAiB,QAAW;AAC9B,QAAI,oBAAoB,MAAM,OAAO,GAAG;AACtC,0BAAoB,MAAM,OAAO,EAAE,UAAU,OAAO;AACpD,UAAI,OAAO,OAAO,WAAW,YAAY;AACvC,YAAI,oBAAoB,YAAY,MAAM;AACxC,8BAAoB,QAAQ,oBAAoB,GAAG,IAAI,OAAO;AAAA,QAChE;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,YAAU,MAAM;AACd,0BAAsB,MAAM,SAAS,MAAa;AAClD,QAAI,oBAAoB,YAAY,QAAQ,OAAO,WAAW,QAAW;AACvE,0BAAoB,QAAQ,oBAAoB,GAAG,IAAI,OAAO;AAAA,IAChE;AACA,WAAO,MAAM;AACX,+BAAyB,MAAM,OAAO;AAAA,IACxC;AAAA,EACF,GAAG;AAAA,IACD;AAAA,IACA;AAAA,IACA,OAAO;AAAA;AAAA,IAEP,OAAO,OAAO,WAAW,WAAW,OAAO,SAAS;AAAA;AAAA,IAEpD,GAAI,gBAAgB,CAAC;AAAA,EACvB,CAAC;AACH;","names":[]}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|