forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.tsx
More file actions
159 lines (149 loc) · 4.61 KB
/
Copy pathpage.tsx
File metadata and controls
159 lines (149 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"use client";
import {
useDefaultTool,
useRenderToolCall,
useFrontendTool,
useCoAgent,
} from "@copilotkit/react-core";
import { CopilotKitCSSProperties, CopilotSidebar } from "@copilotkit/react-ui";
import { useEffect, useState } from "react";
import { DefaultToolComponent } from "@/components/default-tool-ui";
import { WeatherCard } from "@/components/weather";
export default function CopilotKitPage() {
const [themeColor, setThemeColor] = useState("#6366f1");
useEffect(() => {
console.log(themeColor);
}, [themeColor]);
// 🪁 Frontend Actions: https://docs.copilotkit.ai/guides/frontend-actions
useFrontendTool({
name: "set_theme_color",
parameters: [
{
name: "theme_color",
description: "The theme color to set. Make sure to pick nice colors.",
required: true,
},
],
handler({ theme_color }) {
setThemeColor(theme_color);
},
});
return (
<main
style={
{ "--copilot-kit-primary-color": themeColor } as CopilotKitCSSProperties
}
>
<CopilotSidebar
clickOutsideToClose={false}
defaultOpen={true}
// Adds an initial message to the chat
labels={{
title: "Popup Assistant",
initial: "👋 Hi, there! You're chatting with an Strands agent.",
}}
// Suggestions for guiding users
suggestions={[
{
title: "Generative UI",
message: "What's the weather in San Francisco?",
},
{
title: "Frontend Tools",
message: "Set the theme to green.",
},
{
title: "Writing Agent State",
message: "Add a proverb about AI.",
},
]}
>
{/* Wrapping your content in the sidebar pushes it to the side*/}
<YourMainContent themeColor={themeColor} />
</CopilotSidebar>
</main>
);
}
function YourMainContent({ themeColor }: { themeColor: string }) {
// 🪁 Use CoAgent to get shared state from backend
const { state, setState } = useCoAgent({
name: "strands_agent",
initialState: {
proverbs: [
"CopilotKit may be new, but its the best thing since sliced bread.",
],
},
});
useEffect(() => {
console.log("State updated:", state);
}, [state]);
//🪁 Generative UI: https://docs.copilotkit.ai/strands/generative-ui/backend-tools
useRenderToolCall(
{
name: "get_weather",
parameters: [
{
name: "location",
description: "The location to get the weather for.",
required: true,
},
],
render: (props) => (
<WeatherCard themeColor={themeColor} location={props.args.location} />
),
},
[themeColor],
);
//🪁 Default Generative UI: https://docs.copilotkit.ai/strands/generative-ui/backend-tools
useDefaultTool(
{
render: (props) => (
<DefaultToolComponent themeColor={themeColor} {...props} />
),
},
[themeColor],
);
return (
<div
style={{ backgroundColor: themeColor }}
className="h-screen flex justify-center items-center flex-col transition-colors duration-300"
>
<div className="bg-white/20 backdrop-blur-md p-8 rounded-2xl shadow-xl max-w-2xl w-full">
<h1 className="text-4xl font-bold text-white mb-2 text-center">
Proverbs
</h1>
<p className="text-gray-200 text-center italic mb-6">
This is a demonstrative page, but it could be anything you want! 🪁
</p>
<hr className="border-white/20 my-6" />
<div className="flex flex-col gap-3">
{state.proverbs?.map((proverb, index) => (
<div
key={index}
className="bg-white/15 p-4 rounded-xl text-white relative group hover:bg-white/20 transition-all"
>
<p className="pr-8">{proverb}</p>
<button
onClick={() =>
setState({
...state,
proverbs: state.proverbs?.filter((_, i) => i !== index),
})
}
className="absolute right-3 top-3 opacity-0 group-hover:opacity-100 transition-opacity
bg-red-500 hover:bg-red-600 text-white rounded-full h-6 w-6 flex items-center justify-center"
>
✕
</button>
</div>
))}
</div>
{state.proverbs?.length === 0 && (
<p className="text-center text-white/80 italic my-8">
No proverbs yet. Ask the assistant to add some!
</p>
)}
</div>
</div>
);
}