-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathuse-generative-ui-examples.tsx
More file actions
108 lines (98 loc) · 3.8 KB
/
use-generative-ui-examples.tsx
File metadata and controls
108 lines (98 loc) · 3.8 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
import { z } from "zod";
import { useTheme } from "@/hooks/use-theme";
// CopiotKit imports
import {
useComponent,
useFrontendTool,
useHumanInTheLoop,
useDefaultRenderTool,
useRenderTool,
} from "@copilotkit/react-core/v2";
// Generative UI imports
import { PieChart, PieChartProps } from "@/components/generative-ui/charts/pie-chart";
import { BarChart, BarChartProps } from "@/components/generative-ui/charts/bar-chart";
import { WidgetRenderer, WidgetRendererProps } from "@/components/generative-ui/widget-renderer";
import { MeetingTimePicker } from "@/components/generative-ui/meeting-time-picker";
import { ToolReasoning } from "@/components/tool-rendering";
import { PlanCard } from "@/components/generative-ui/plan-card";
export const useGenerativeUIExamples = () => {
const { theme, setTheme } = useTheme();
// ------------------
// 🪁 Frontend Tools: https://docs.copilotkit.ai/langgraph/frontend-actions
// ------------------
useFrontendTool({
name: "toggleTheme",
description: "Frontend tool for toggling the theme of the app.",
parameters: z.object({}),
handler: async () => {
setTheme(theme === "dark" ? "light" : "dark")
},
}, [theme, setTheme]);
// --------------------------
// 🪁 Frontend Generative UI: https://docs.copilotkit.ai/langgraph/generative-ui/frontend-tools
// --------------------------
useComponent({
name: "pieChart",
description: "Controlled Generative UI that displays data as a pie chart.",
parameters: PieChartProps,
render: PieChart,
});
useComponent({
name: "barChart",
description: "Controlled Generative UI that displays data as a bar chart.",
parameters: BarChartProps,
render: BarChart,
});
// --------------------------
// 🪁 Widget Renderer: Sandboxed HTML/SVG visualizations
// --------------------------
useComponent({
name: "widgetRenderer",
description:
"Renders interactive HTML/SVG visualizations in a sandboxed iframe. " +
"Use for algorithm visualizations, diagrams, interactive widgets, " +
"simulations, math plots, and any visual explanation.",
parameters: WidgetRendererProps,
render: WidgetRenderer,
});
// --------------------------
// 🪁 Plan Visualization: Custom rendering for the planning step
// --------------------------
const PlanVisualizationParams = z.object({
approach: z.string(),
technology: z.string(),
key_elements: z.array(z.string()),
});
useRenderTool({
name: "plan_visualization",
parameters: PlanVisualizationParams,
render: ({ status, parameters }) => {
const { key_elements: keyElements, ...rest } = parameters;
return <PlanCard status={status} keyElements={keyElements} {...rest} />;
},
});
// --------------------------
// 🪁 Default Tool Rendering: https://docs.copilotkit.ai/langgraph/generative-ui/backend-tools
// --------------------------
const ignoredTools = ["generate_form"]
useDefaultRenderTool({
render: ({ name, status, parameters }) => {
if(ignoredTools.includes(name)) return <></>;
return <ToolReasoning name={name} status={status} args={parameters} />;
},
});
// -------------------------------------
// 🪁 Frontend-tools - Human-in-the-loop: https://docs.copilotkit.ai/langgraph/human-in-the-loop/frontend-tool-based
// -------------------------------------
useHumanInTheLoop({
name: "scheduleTime",
description: "Use human-in-the-loop to schedule a meeting with the user.",
parameters: z.object({
reasonForScheduling: z.string().describe("Reason for scheduling, very brief - 5 words."),
meetingDuration: z.number().describe("Duration of the meeting in minutes"),
}),
render: ({ respond, status, args }) => {
return <MeetingTimePicker status={status} respond={respond} {...args} />;
},
});
};