Repository navigation
Expand file tree
/
Copy pathrender-chart.tsx
More file actions
119 lines (110 loc) · 3.54 KB
/
Copy pathrender-chart.tsx
File metadata and controls
119 lines (110 loc) · 3.54 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
/**
* `render_chart` — an agent-rendered Channel component backed by Slack's native
* data visualization block. This is the "upload a CSV → get a chart" payoff:
* the agent parses the data, then renders this with Slack's documented shape.
*/
import { z } from "zod";
import { Section, defineChannelComponent } from "@copilotkit/channels";
import { Slack } from "@copilotkit/channels/slack";
const shortLabel = z.string().min(1).max(20);
const dataPoint = z.object({
label: shortLabel.describe(
"X-axis category. Must match one axis_config.categories value.",
),
value: z.number().finite().describe("Numeric y-axis value."),
});
const pieChart = z.object({
type: z.literal("pie"),
segments: z
.array(
z.object({
label: shortLabel.describe("Slice label shown in the legend."),
value: z.number().finite().positive().describe("Positive slice weight."),
}),
)
.min(1)
.max(12),
});
const seriesChart = z.object({
type: z.enum(["bar", "area", "line"]),
series: z
.array(
z.object({
name: shortLabel.describe("Unique series name shown in the legend."),
data: z.array(dataPoint).min(1).max(20),
}),
)
.min(1)
.max(12),
axis_config: z.object({
categories: z
.array(shortLabel)
.min(1)
.max(20)
.describe("Unique categories in left-to-right display order."),
x_label: z.string().max(50).optional(),
y_label: z.string().max(50).optional(),
}),
});
const schema = z.object({
title: z
.string()
.min(1)
.max(50)
.describe("Short title displayed above the chart."),
chart: z
.discriminatedUnion("type", [pieChart, seriesChart])
.describe("Slack pie, bar, area, or line chart payload."),
}).superRefine(({ chart }, ctx) => {
if (chart.type === "pie") return;
const categories = chart.axis_config.categories;
if (new Set(categories).size !== categories.length) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ["chart", "axis_config", "categories"],
message: "Category labels must be unique.",
});
}
const seriesNames = chart.series.map(({ name }) => name);
if (new Set(seriesNames).size !== seriesNames.length) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ["chart", "series"],
message: "Series names must be unique.",
});
}
for (const [index, series] of chart.series.entries()) {
const labels = series.data.map(({ label }) => label);
const labelsMatchCategories =
labels.length === categories.length &&
new Set(labels).size === labels.length &&
labels.every((label) => categories.includes(label));
if (!labelsMatchCategories) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ["chart", "series", index, "data"],
message:
"Each series must contain exactly one data point for every axis category.",
});
}
}
});
export const RenderChart = defineChannelComponent({
name: "render_chart",
description:
"Render a native Slack data visualization in the conversation. Use this " +
"after analyzing data such as an uploaded CSV. Supports pie, bar, area, " +
"and line charts. Series data labels must exactly match the ordered axis " +
"categories.",
parameters: schema,
render({ title, chart }, { platform }) {
if (platform !== "slack") {
return (
<Section>
Native data visualizations are currently only available in Slack.
</Section>
);
}
return <Slack.Block.DataVisualization title={title} chart={chart} />;
},
});