|
1 | 1 | "use client"; |
2 | 2 |
|
3 | | -import { useCoAgent, useCopilotAction } from "@copilotkit/react-core"; |
| 3 | +import { ProverbsCard } from "@/components/proverbs"; |
| 4 | +import { WeatherCard } from "@/components/weather"; |
| 5 | +import { MoonCard } from "@/components/moon"; |
| 6 | +import { AgentState } from "@/lib/types"; |
| 7 | +import { |
| 8 | + useCoAgent, |
| 9 | + useFrontendTool, |
| 10 | + useHumanInTheLoop, |
| 11 | + useRenderToolCall, |
| 12 | +} from "@copilotkit/react-core"; |
4 | 13 | import { CopilotKitCSSProperties, CopilotSidebar } from "@copilotkit/react-ui"; |
5 | 14 | import { useState } from "react"; |
6 | 15 |
|
7 | 16 | export default function CopilotKitPage() { |
8 | 17 | const [themeColor, setThemeColor] = useState("#6366f1"); |
9 | 18 |
|
10 | | - // 🪁 Frontend Actions: https://docs.copilotkit.ai/guides/frontend-actions |
11 | | - useCopilotAction({ |
| 19 | + // 🪁 Frontend Actions: https://docs.copilotkit.ai/pydantic-ai/frontend-actions |
| 20 | + useFrontendTool({ |
12 | 21 | name: "setThemeColor", |
13 | | - parameters: [{ |
14 | | - name: "themeColor", |
15 | | - description: "The theme color to set. Make sure to pick nice colors.", |
16 | | - required: true, |
17 | | - }], |
| 22 | + parameters: [ |
| 23 | + { |
| 24 | + name: "themeColor", |
| 25 | + description: "The theme color to set. Make sure to pick nice colors.", |
| 26 | + required: true, |
| 27 | + }, |
| 28 | + ], |
18 | 29 | handler({ themeColor }) { |
19 | 30 | setThemeColor(themeColor); |
20 | 31 | }, |
21 | 32 | }); |
22 | 33 |
|
23 | 34 | return ( |
24 | | - <main style={{ "--copilot-kit-primary-color": themeColor } as CopilotKitCSSProperties}> |
25 | | - <YourMainContent themeColor={themeColor} /> |
| 35 | + <main |
| 36 | + style={ |
| 37 | + { "--copilot-kit-primary-color": themeColor } as CopilotKitCSSProperties |
| 38 | + } |
| 39 | + > |
26 | 40 | <CopilotSidebar |
| 41 | + disableSystemMessage={true} |
27 | 42 | clickOutsideToClose={false} |
28 | | - defaultOpen={true} |
29 | 43 | labels={{ |
30 | 44 | title: "Popup Assistant", |
31 | | - initial: "👋 Hi, there! You're chatting with an agent. This agent comes with a few tools to get you started.\n\nFor example you can try:\n- **Frontend Tools**: \"Set the theme to orange\"\n- **Shared State**: \"Write a proverb about AI\"\n- **Generative UI**: \"Get the weather in SF\"\n\nAs you interact with the agent, you'll see the UI update in real-time to reflect the agent's **state**, **tool calls**, and **progress**." |
| 45 | + initial: "👋 Hi, there! You're chatting with an agent.", |
32 | 46 | }} |
33 | | - /> |
| 47 | + suggestions={[ |
| 48 | + { |
| 49 | + title: "Generative UI", |
| 50 | + message: "Get the weather in San Francisco.", |
| 51 | + }, |
| 52 | + { |
| 53 | + title: "Frontend Tools", |
| 54 | + message: "Set the theme to green.", |
| 55 | + }, |
| 56 | + { |
| 57 | + title: "Human In the Loop", |
| 58 | + message: "Please go to the moon.", |
| 59 | + }, |
| 60 | + { |
| 61 | + title: "Write Agent State", |
| 62 | + message: "Add a proverb about AI.", |
| 63 | + }, |
| 64 | + { |
| 65 | + title: "Update Agent State", |
| 66 | + message: |
| 67 | + "Please remove 1 random proverb from the list if there are any.", |
| 68 | + }, |
| 69 | + { |
| 70 | + title: "Read Agent State", |
| 71 | + message: "What are the proverbs?", |
| 72 | + }, |
| 73 | + ]} |
| 74 | + > |
| 75 | + <YourMainContent themeColor={themeColor} /> |
| 76 | + </CopilotSidebar> |
34 | 77 | </main> |
35 | 78 | ); |
36 | 79 | } |
37 | 80 |
|
38 | | -// State of the agent, make sure this aligns with your agent's state. |
39 | | -type AgentState = { |
40 | | - proverbs: string[]; |
41 | | -} |
42 | | - |
43 | 81 | function YourMainContent({ themeColor }: { themeColor: string }) { |
44 | | - // 🪁 Shared State: https://docs.copilotkit.ai/coagents/shared-state |
| 82 | + // 🪁 Shared State: https://docs.copilotkit.ai/pydantic-ai/shared-state |
45 | 83 | const { state, setState } = useCoAgent<AgentState>({ |
46 | 84 | name: "sample_agent", |
47 | 85 | initialState: { |
48 | 86 | proverbs: [ |
49 | 87 | "CopilotKit may be new, but its the best thing since sliced bread.", |
50 | 88 | ], |
51 | 89 | }, |
52 | | - }) |
| 90 | + }); |
53 | 91 |
|
54 | 92 | // 🪁 Frontend Actions: https://docs.copilotkit.ai/coagents/frontend-actions |
55 | | - useCopilotAction({ |
56 | | - name: "addProverb", |
57 | | - parameters: [{ |
58 | | - name: "proverb", |
59 | | - description: "The proverb to add. Make it witty, short and concise.", |
60 | | - required: true, |
61 | | - }], |
62 | | - handler: ({ proverb }) => { |
| 93 | + useFrontendTool({ |
| 94 | + name: "updateProverbs", |
| 95 | + parameters: [ |
| 96 | + { |
| 97 | + name: "proverbs", |
| 98 | + description: "What the current list of proverbs should be updated to", |
| 99 | + type: "string[]", |
| 100 | + required: true, |
| 101 | + }, |
| 102 | + ], |
| 103 | + handler: ({ proverbs }) => { |
63 | 104 | setState({ |
64 | 105 | ...state, |
65 | | - proverbs: [...(state.proverbs || []), proverb], |
| 106 | + proverbs: proverbs, |
66 | 107 | }); |
67 | 108 | }, |
68 | 109 | }); |
69 | 110 |
|
70 | | - //🪁 Generative UI: https://docs.copilotkit.ai/coagents/generative-ui |
71 | | - useCopilotAction({ |
72 | | - name: "get_weather", |
73 | | - description: "Get the weather for a given location.", |
74 | | - available: "disabled", |
75 | | - parameters: [ |
76 | | - { name: "location", type: "string", required: true }, |
77 | | - ], |
78 | | - render: ({ args }) => { |
79 | | - return <WeatherCard location={args.location} themeColor={themeColor} /> |
| 111 | + //🪁 Generative UI: https://docs.copilotkit.ai/pydantic-ai/generative-ui |
| 112 | + useRenderToolCall( |
| 113 | + { |
| 114 | + name: "get_weather", |
| 115 | + description: "Get the weather for a given location.", |
| 116 | + parameters: [{ name: "location", type: "string", required: true }], |
| 117 | + render: ({ args }) => { |
| 118 | + return <WeatherCard location={args.location} themeColor={themeColor} />; |
| 119 | + }, |
80 | 120 | }, |
81 | | - }); |
82 | | - |
83 | | - return ( |
84 | | - <div |
85 | | - style={{ backgroundColor: themeColor }} |
86 | | - className="h-screen w-screen flex justify-center items-center flex-col transition-colors duration-300" |
87 | | - > |
88 | | - <div className="bg-white/20 backdrop-blur-md p-8 rounded-2xl shadow-xl max-w-2xl w-full"> |
89 | | - <h1 className="text-4xl font-bold text-white mb-2 text-center">Proverbs</h1> |
90 | | - <p className="text-gray-200 text-center italic mb-6">This is a demonstrative page, but it could be anything you want! 🪁</p> |
91 | | - <hr className="border-white/20 my-6" /> |
92 | | - <div className="flex flex-col gap-3"> |
93 | | - {state.proverbs?.map((proverb, index) => ( |
94 | | - <div |
95 | | - key={index} |
96 | | - className="bg-white/15 p-4 rounded-xl text-white relative group hover:bg-white/20 transition-all" |
97 | | - > |
98 | | - <p className="pr-8">{proverb}</p> |
99 | | - <button |
100 | | - onClick={() => setState({ |
101 | | - ...state, |
102 | | - proverbs: (state.proverbs || []).filter((_, i) => i !== index), |
103 | | - })} |
104 | | - className="absolute right-3 top-3 opacity-0 group-hover:opacity-100 transition-opacity |
105 | | - bg-red-500 hover:bg-red-600 text-white rounded-full h-6 w-6 flex items-center justify-center" |
106 | | - > |
107 | | - ✕ |
108 | | - </button> |
109 | | - </div> |
110 | | - ))} |
111 | | - </div> |
112 | | - {state.proverbs?.length === 0 && <p className="text-center text-white/80 italic my-8"> |
113 | | - No proverbs yet. Ask the assistant to add some! |
114 | | - </p>} |
115 | | - </div> |
116 | | - </div> |
| 121 | + [themeColor], |
117 | 122 | ); |
118 | | -} |
119 | 123 |
|
120 | | -// Simple sun icon for the weather card |
121 | | -function SunIcon() { |
122 | | - return ( |
123 | | - <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" className="w-14 h-14 text-yellow-200"> |
124 | | - <circle cx="12" cy="12" r="5" /> |
125 | | - <path d="M12 1v2M12 21v2M4.22 4.22l1.42 1.42M18.36 18.36l1.42 1.42M1 12h2M21 12h2M4.22 19.78l1.42-1.42M18.36 5.64l1.42-1.42" strokeWidth="2" stroke="currentColor" /> |
126 | | - </svg> |
| 124 | + // 🪁 Human In the Loop: https://docs.copilotkit.ai/pydantic-ai/human-in-the-loop |
| 125 | + useHumanInTheLoop( |
| 126 | + { |
| 127 | + name: "go_to_moon", |
| 128 | + description: "Go to the moon on request.", |
| 129 | + render: ({ respond, status }) => { |
| 130 | + return ( |
| 131 | + <MoonCard themeColor={themeColor} status={status} respond={respond} /> |
| 132 | + ); |
| 133 | + }, |
| 134 | + }, |
| 135 | + [themeColor], |
127 | 136 | ); |
128 | | -} |
129 | 137 |
|
130 | | -// Weather card component where the location and themeColor are based on what the agent |
131 | | -// sets via tool calls. |
132 | | -function WeatherCard({ location, themeColor }: { location?: string, themeColor: string }) { |
133 | 138 | return ( |
134 | 139 | <div |
135 | | - style={{ backgroundColor: themeColor }} |
136 | | - className="rounded-xl shadow-xl mt-6 mb-4 max-w-md w-full" |
137 | | - > |
138 | | - <div className="bg-white/20 p-4 w-full"> |
139 | | - <div className="flex items-center justify-between"> |
140 | | - <div> |
141 | | - <h3 className="text-xl font-bold text-white capitalize">{location}</h3> |
142 | | - <p className="text-white">Current Weather</p> |
143 | | - </div> |
144 | | - <SunIcon /> |
145 | | - </div> |
146 | | - |
147 | | - <div className="mt-4 flex items-end justify-between"> |
148 | | - <div className="text-3xl font-bold text-white">70°</div> |
149 | | - <div className="text-sm text-white">Clear skies</div> |
150 | | - </div> |
151 | | - |
152 | | - <div className="mt-4 pt-4 border-t border-white"> |
153 | | - <div className="grid grid-cols-3 gap-2 text-center"> |
154 | | - <div> |
155 | | - <p className="text-white text-xs">Humidity</p> |
156 | | - <p className="text-white font-medium">45%</p> |
157 | | - </div> |
158 | | - <div> |
159 | | - <p className="text-white text-xs">Wind</p> |
160 | | - <p className="text-white font-medium">5 mph</p> |
161 | | - </div> |
162 | | - <div> |
163 | | - <p className="text-white text-xs">Feels Like</p> |
164 | | - <p className="text-white font-medium">72°</p> |
165 | | - </div> |
166 | | - </div> |
167 | | - </div> |
| 140 | + style={{ backgroundColor: themeColor }} |
| 141 | + className="h-screen flex justify-center items-center flex-col transition-colors duration-300" |
| 142 | + > |
| 143 | + <ProverbsCard state={state} setState={setState} /> |
168 | 144 | </div> |
169 | | - </div> |
170 | 145 | ); |
171 | 146 | } |
0 commit comments