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
195 lines (181 loc) · 5.2 KB
/
Copy pathpage.tsx
File metadata and controls
195 lines (181 loc) · 5.2 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"use client";
// Tool Rendering — PRIMARY (per-tool + catch-all) variant.
//
// The most sophisticated point in the three-way progression: every
// "interesting" backend tool gets its own dedicated, branded UI, and a
// catch-all paints anything that slips through.
//
// get_weather → <WeatherCard /> (per-tool renderer)
// search_flights → <FlightListCard /> (per-tool renderer)
// get_stock_price → <StockCard /> (per-tool renderer)
// roll_d20 → <D20Card /> (per-tool renderer)
// * → <CustomCatchallRenderer /> (wildcard fallback)
// @region[render-flight-tool]
// @region[render-weather-tool]
import React from "react";
import {
CopilotKit,
CopilotChat,
useRenderTool,
useDefaultRenderTool,
} from "@copilotkit/react-core/v2";
import { z } from "zod";
import { WeatherCard } from "./weather-card";
import { FlightListCard, type Flight } from "./flight-list-card";
import { StockCard } from "./stock-card";
import { D20Card } from "./d20-card";
import {
CustomCatchallRenderer,
type CatchallToolStatus,
} from "./custom-catchall-renderer";
import { parseJsonResult } from "../_shared/parse-json-result";
import { useSuggestions } from "./suggestions";
interface WeatherResult {
city?: string;
temperature?: number;
humidity?: number;
wind_speed?: number;
conditions?: string;
}
interface FlightSearchResult {
origin?: string;
destination?: string;
flights?: Flight[];
}
interface StockResult {
ticker?: string;
price_usd?: number;
change_pct?: number;
}
interface D20Result {
value?: number;
result?: number;
sides?: number;
}
export default function ToolRenderingDemo() {
return (
<CopilotKit runtimeUrl="/api/copilotkit" agent="tool-rendering">
<div className="flex justify-center items-center h-screen w-full">
<div className="h-full w-full max-w-4xl">
<Chat />
</div>
</div>
</CopilotKit>
);
}
function Chat() {
// Per-tool renderer #1: get_weather → branded WeatherCard.
useRenderTool(
{
name: "get_weather",
parameters: z.object({
location: z.string(),
}),
render: ({ parameters, result, status }) => {
const loading = status !== "complete";
const parsed = parseJsonResult<WeatherResult>(result);
return (
<WeatherCard
loading={loading}
location={parameters?.location ?? parsed.city ?? ""}
temperature={parsed.temperature}
humidity={parsed.humidity}
windSpeed={parsed.wind_speed}
conditions={parsed.conditions}
/>
);
},
},
[],
);
// @endregion[render-weather-tool]
// Per-tool renderer #2: search_flights → branded FlightListCard.
useRenderTool(
{
name: "search_flights",
parameters: z.object({
origin: z.string(),
destination: z.string(),
}),
render: ({ parameters, result, status }) => {
const loading = status !== "complete";
const parsed = parseJsonResult<FlightSearchResult>(result);
return (
<FlightListCard
loading={loading}
origin={parameters?.origin ?? parsed.origin ?? ""}
destination={parameters?.destination ?? parsed.destination ?? ""}
flights={parsed.flights ?? []}
/>
);
},
},
[],
);
// @endregion[render-flight-tool]
// Per-tool renderer #3: get_stock_price → branded StockCard.
useRenderTool(
{
name: "get_stock_price",
parameters: z.object({
ticker: z.string(),
}),
render: ({ parameters, result, status }) => {
const loading = status !== "complete";
const parsed = parseJsonResult<StockResult>(result);
return (
<StockCard
loading={loading}
ticker={parameters?.ticker ?? parsed.ticker ?? ""}
priceUsd={parsed.price_usd}
changePct={parsed.change_pct}
/>
);
},
},
[],
);
// Per-tool renderer #4: roll_d20 → branded D20Card. Each tool call
// mounts its own card so e2e tests can count them.
useRenderTool(
{
name: "roll_d20",
parameters: z.object({
value: z.number().optional(),
}),
render: ({ result, status }) => {
const loading = status !== "complete";
const parsed = parseJsonResult<D20Result>(result);
const value =
typeof parsed.value === "number"
? parsed.value
: typeof parsed.result === "number"
? parsed.result
: undefined;
return <D20Card loading={loading} value={value} />;
},
},
[],
);
// @region[catchall-renderer]
// Wildcard catch-all for anything that doesn't match a per-tool
// renderer above.
useDefaultRenderTool(
{
render: ({ name, parameters, status, result }) => (
<CustomCatchallRenderer
name={name}
parameters={parameters}
status={status as CatchallToolStatus}
result={result}
/>
),
},
[],
);
// @endregion[catchall-renderer]
useSuggestions();
return (
<CopilotChat agentId="tool-rendering" className="h-full rounded-2xl" />
);
}