@dbx-tools/appkit-mastra 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/chart.ts ADDED
@@ -0,0 +1,425 @@
1
+ /**
2
+ * Chart-rendering primitives.
3
+ *
4
+ * Two surfaces, one shared brain:
5
+ *
6
+ * - {@link buildRenderDataTool}: a Mastra tool the model calls
7
+ * ("here is a dataset, render it as a chart"). The tool is
8
+ * fire-and-forget by design - it generates a short `chartId`,
9
+ * pushes a single `kind: "chart"` event onto `ctx.writer` carrying
10
+ * the raw rows, and returns the id to the model immediately. No
11
+ * chart planning happens inside the agentic loop, so the model
12
+ * never blocks on a downstream LLM call to get its identifier.
13
+ *
14
+ * - {@link runChartPlanner}: the chart-planner Agent + ECOption
15
+ * expansion as a plain async function. The HTTP route in
16
+ * {@link ./render-chart-route.ts} calls this when the client
17
+ * POSTs the dataset back; the result is an `EChartsOption` JSON
18
+ * the React `<ChartSlot>` renders inline. Decoupling the planner
19
+ * from the tool means the planning latency lives entirely
20
+ * client-side: the model can finish writing its report while
21
+ * the client is still rendering the charts.
22
+ *
23
+ * The model wires the chart into its reply by emitting the marker
24
+ * `[[chart:<chartId>]]` on its own line in markdown. The chat
25
+ * client splits the assistant text on these markers and drops a
26
+ * `<ChartSlot>` in at the position the model placed it; the slot
27
+ * then fires the render-chart endpoint on mount and shows a
28
+ * skeleton until the option lands.
29
+ */
30
+
31
+ import { randomUUID } from "node:crypto";
32
+
33
+ import { stringUtils } from "@dbx-tools/appkit-shared";
34
+ import { Agent } from "@mastra/core/agent";
35
+ import type { RequestContext } from "@mastra/core/request-context";
36
+ import { createTool } from "@mastra/core/tools";
37
+ import { z } from "zod";
38
+
39
+ import type { MastraPluginConfig } from "./config.js";
40
+ import { ModelTier, modelForTier, buildModel } from "./model.js";
41
+
42
+ /**
43
+ * Compact, model-friendly representation of an Echarts spec. The
44
+ * planner agent emits this; {@link planToEchartsOption} expands it
45
+ * into a real `EChartsOption` JSON. Two layers because letting the
46
+ * model fill in a fully-typed `EChartsOption` is brittle (hundreds
47
+ * of optional fields, deep unions, version-dependent shapes). A
48
+ * small "chart plan" schema is much more reliable for a fast model
49
+ * and keeps animation / tooltip / styling defaults consistent
50
+ * across charts.
51
+ */
52
+ const chartPlanSchema = z.object({
53
+ chartType: z
54
+ .enum(["bar", "line", "area", "scatter", "pie"])
55
+ .describe(stringUtils.toDescription`
56
+ The chart shape that best matches the data and intent. Use
57
+ \`bar\` for category-vs-value comparisons, \`line\` for
58
+ trends over an ordered axis, \`area\` for stacked-trend
59
+ emphasis, \`scatter\` for two-numeric-axis correlations,
60
+ \`pie\` for parts-of-a-whole when categories are few.
61
+ `),
62
+ title: z.string().optional().describe(stringUtils.toDescription`
63
+ Short title shown above the chart. Optional; defaults to the
64
+ \`title\` argument the caller passed in.
65
+ `),
66
+ xAxisLabel: z.string().optional().describe(stringUtils.toDescription`
67
+ Axis label below the chart. Used for bar / line / area /
68
+ scatter; ignored for pie.
69
+ `),
70
+ yAxisLabel: z.string().optional().describe(stringUtils.toDescription`
71
+ Axis label to the left of the chart. Used for bar / line /
72
+ area / scatter; ignored for pie.
73
+ `),
74
+ categories: z
75
+ .array(z.string())
76
+ .optional()
77
+ .describe(stringUtils.toDescription`
78
+ X-axis category labels for \`bar\` / \`line\` / \`area\`
79
+ charts (one per data point in each series). Omit for
80
+ \`scatter\` (uses [x, y] tuples) and \`pie\` (each slice
81
+ carries its own \`name\`).
82
+ `),
83
+ series: z
84
+ .array(
85
+ z.object({
86
+ name: z.string().describe(stringUtils.toDescription`
87
+ Legend name for this series.
88
+ `),
89
+ data: z
90
+ .array(
91
+ z.union([
92
+ z.number(),
93
+ z.tuple([z.number(), z.number()]),
94
+ z.object({
95
+ name: z.string(),
96
+ value: z.number(),
97
+ }),
98
+ ]),
99
+ )
100
+ .describe(stringUtils.toDescription`
101
+ Data points. For \`bar\` / \`line\` / \`area\`, an
102
+ array of numbers aligned to \`categories\`. For
103
+ \`scatter\`, an array of \`[x, y]\` numeric tuples.
104
+ For \`pie\`, an array of \`{name, value}\` objects.
105
+ `),
106
+ }),
107
+ )
108
+ .min(1)
109
+ .describe(stringUtils.toDescription`
110
+ One or more series to plot. Pie charts use exactly one
111
+ series; bar/line/area can stack multiple series sharing
112
+ the same \`categories\` axis.
113
+ `),
114
+ });
115
+
116
+ type ChartPlan = z.infer<typeof chartPlanSchema>;
117
+
118
+ /**
119
+ * System prompt for the inner chart-planning agent. Tuned for a
120
+ * fast-tier model (Haiku, GPT-5-mini, Gemini Flash Lite).
121
+ */
122
+ const CHART_PLANNER_INSTRUCTIONS = stringUtils.toDescription`
123
+ You design Apache Echarts visualizations. The user gives you a
124
+ tabular dataset (rows of objects) plus a title and an optional
125
+ description of the intent. You produce a small chart plan
126
+ (chart type, axis labels, categories, series) that best
127
+ conveys the data.
128
+
129
+ Decision guide:
130
+
131
+ - bar: comparing a numeric value across a small/medium set of
132
+ discrete categories (top-N, ranking, group-by).
133
+ - line: ordered-axis trend (time series, sequence).
134
+ - area: same as line but emphasises magnitude or stacked
135
+ composition.
136
+ - scatter: two numeric axes, correlation between fields.
137
+ - pie: parts of a whole when 2-7 categories sum to a
138
+ meaningful total.
139
+
140
+ When in doubt between bar and line, prefer bar for unordered
141
+ categories and line for ordered ones (dates, time buckets,
142
+ ranks). Never pick pie for more than 7 slices.
143
+
144
+ For bar / line / area: pick one column as the category axis
145
+ (usually the only string-valued column) and one or more
146
+ numeric columns as series. Sort categories by the primary
147
+ series value descending unless the data is naturally ordered
148
+ (dates, ranks).
149
+
150
+ For pie: pick the category column for slice names and one
151
+ numeric column for slice values. Emit a single series.
152
+
153
+ For scatter: pick two numeric columns and emit \`[x, y]\`
154
+ tuples in a single series.
155
+
156
+ Keep series names human-readable (use the column name; title
157
+ case it lightly if needed). Keep titles concise; do not
158
+ repeat the user's title in xAxisLabel / yAxisLabel.
159
+ `;
160
+
161
+ /**
162
+ * Lazily-constructed inner agent shared across all calls in this
163
+ * process. The agent is stateless (no memory, no tools) so a
164
+ * single instance per plugin config is safe; model resolution
165
+ * still happens per-call against the live `requestContext`, so
166
+ * OBO auth stays user-scoped.
167
+ */
168
+ function createChartPlannerAgent(config: MastraPluginConfig): Agent {
169
+ return new Agent({
170
+ id: "render_chart_planner",
171
+ name: "Chart Planner",
172
+ description: "Picks chart type and axis encodings for a dataset.",
173
+ instructions: CHART_PLANNER_INSTRUCTIONS,
174
+ model: ({ requestContext }) =>
175
+ buildModel(config, requestContext, {
176
+ modelId: modelForTier(ModelTier.Fast),
177
+ }),
178
+ });
179
+ }
180
+
181
+ /** Inputs to {@link runChartPlanner}. */
182
+ export interface RunChartPlannerOptions {
183
+ config: MastraPluginConfig;
184
+ requestContext?: RequestContext;
185
+ title: string;
186
+ description?: string;
187
+ data: ReadonlyArray<Record<string, unknown>>;
188
+ }
189
+
190
+ /** Output of {@link runChartPlanner}: a fully-formed Echarts spec. */
191
+ export interface RunChartPlannerResult {
192
+ option: Record<string, unknown>;
193
+ chartType: ChartPlan["chartType"];
194
+ }
195
+
196
+ /**
197
+ * Module-level cache: one chart-planner agent per plugin config
198
+ * instance. Keyed on the config object identity since each plugin
199
+ * mount provides its own resolver / fallbacks. Re-used across
200
+ * tool invocations and the render-chart HTTP route.
201
+ */
202
+ const _plannerByConfig = new WeakMap<MastraPluginConfig, Agent>();
203
+ function getPlannerAgent(config: MastraPluginConfig): Agent {
204
+ let agent = _plannerByConfig.get(config);
205
+ if (!agent) {
206
+ agent = createChartPlannerAgent(config);
207
+ _plannerByConfig.set(config, agent);
208
+ }
209
+ return agent;
210
+ }
211
+
212
+ /**
213
+ * Run the chart planner against the given dataset and return a
214
+ * full Echarts `EChartsOption` JSON. Used by the HTTP route the
215
+ * client hits when it sees a `[[chart:<chartId>]]` marker; the
216
+ * tool itself does not call this so the model never blocks on
217
+ * planning latency.
218
+ */
219
+ export async function runChartPlanner(
220
+ opts: RunChartPlannerOptions,
221
+ ): Promise<RunChartPlannerResult> {
222
+ const { config, requestContext, title, description, data } = opts;
223
+ const planner = getPlannerAgent(config);
224
+
225
+ const prompt = [
226
+ `Title: ${title}`,
227
+ description ? `Intent: ${description}` : null,
228
+ "",
229
+ "Dataset (JSON, one row per object):",
230
+ "```json",
231
+ JSON.stringify(data, null, 2),
232
+ "```",
233
+ ]
234
+ .filter((line): line is string => line !== null)
235
+ .join("\n");
236
+
237
+ const result = await planner.generate(prompt, {
238
+ structuredOutput: { schema: chartPlanSchema },
239
+ ...(requestContext ? { requestContext } : {}),
240
+ });
241
+ const plan = result.object;
242
+ const option = planToEchartsOption(plan, title);
243
+ return { option, chartType: plan.chartType };
244
+ }
245
+
246
+ const renderDataInputSchema = z.object({
247
+ title: z.string().describe(stringUtils.toDescription`
248
+ Title shown above the rendered chart. Use a concise
249
+ sentence-case label (e.g. "Top 10 SKUs by On-Hand Units").
250
+ `),
251
+ description: z.string().optional().describe(stringUtils.toDescription`
252
+ Optional one-line intent describing what insight the chart
253
+ should convey (e.g. "highlight the steep drop-off after
254
+ position 5", "compare quarterly revenue across regions").
255
+ The chart-planner reads this when picking the chart type and
256
+ axis encodings; the user does not see it directly.
257
+ `),
258
+ data: z
259
+ .array(z.record(z.string(), z.unknown()))
260
+ .min(1)
261
+ .describe(stringUtils.toDescription`
262
+ Tabular dataset to chart. One object per row, keyed by
263
+ column name. Values may be strings, numbers, booleans, or
264
+ null. The chart-planner decides which columns are
265
+ categories vs. numeric series. Cap at a few hundred rows
266
+ for legibility; sample / aggregate larger datasets first.
267
+ `),
268
+ });
269
+
270
+ const renderDataOutputSchema = z.object({
271
+ chartId: z.string().describe(stringUtils.toDescription`
272
+ Identifier of the queued chart. The tool returned
273
+ immediately - actual chart planning happens client-side
274
+ asynchronously. To position the chart in your reply, embed
275
+ the marker \`[[chart:<chartId>]]\` on its own line (with
276
+ blank lines above and below) where the chart should appear.
277
+ The client renders a skeleton there until the chart is
278
+ ready, then swaps in the visualization in place. You can
279
+ keep writing prose around the marker; the agent does not
280
+ need to wait for the chart to render.
281
+ `),
282
+ });
283
+
284
+ /**
285
+ * Build the `render_data` tool bound to the given plugin config.
286
+ *
287
+ * Fire-and-forget by design: the tool returns immediately with a
288
+ * short `chartId` and emits a single `kind: "chart"` event over
289
+ * `ctx.writer` carrying the raw dataset for the client. The
290
+ * client's chart slot then POSTs the data to
291
+ * `/route/render-chart` to get an `EChartsOption` back from the
292
+ * planner agent. This keeps the calling LLM unblocked - it can
293
+ * write the report referencing the chart by id while the client
294
+ * is still rendering it.
295
+ */
296
+ export function buildRenderDataTool(_config: MastraPluginConfig) {
297
+ return createTool({
298
+ id: "render_data",
299
+ description: stringUtils.toDescription`
300
+ Submit a tabular dataset for inline rendering as a chart in
301
+ the user's view. Pass a title, the raw rows (array of
302
+ objects keyed by column name), and an optional one-line
303
+ description of the insight to highlight. Returns a short
304
+ \`chartId\` immediately - chart planning happens
305
+ asynchronously in the client, not in this turn, so the tool
306
+ does not block your prose.
307
+
308
+ Placement contract: embed \`[[chart:<chartId>]]\` on its own
309
+ line (blank lines above and below) wherever you want the
310
+ chart to appear in your reply. The client shows a skeleton
311
+ at that spot until the chart is ready, then swaps in the
312
+ rendered Echarts visualization. You can call
313
+ \`render_data\` multiple times in the same turn (the tool
314
+ is parallel-safe) and interleave the markers with prose so
315
+ each chart sits next to its commentary. A chart whose
316
+ marker is omitted falls through to the end of your reply
317
+ as a fallback - safe but less polished.
318
+
319
+ Use whenever a SQL row set, API response, or hand-built
320
+ dataset would land better as a picture than as a list or
321
+ table. Cap input at a few hundred rows; sample or
322
+ aggregate larger datasets first.
323
+ `,
324
+ inputSchema: renderDataInputSchema,
325
+ outputSchema: renderDataOutputSchema,
326
+ execute: async (input, ctx) => {
327
+ const { title, description, data } = input as z.infer<
328
+ typeof renderDataInputSchema
329
+ >;
330
+
331
+ // Short, marker-friendly id. The LLM has to type this
332
+ // verbatim into the `[[chart:<id>]]` marker; an 8-hex-char
333
+ // prefix is unique within a single assistant turn (collision
334
+ // odds ~1 in 4 billion) and much less error-prone for the
335
+ // model to reproduce.
336
+ const chartId = randomUUID().replace(/-/g, "").slice(0, 8);
337
+
338
+ const writer = (ctx as { writer?: { write: (e: unknown) => unknown } } | undefined)
339
+ ?.writer;
340
+ try {
341
+ await writer?.write({
342
+ kind: "chart",
343
+ chartId,
344
+ title,
345
+ ...(description ? { description } : {}),
346
+ data,
347
+ });
348
+ } catch {
349
+ // Ignore: the parent stream may have closed downstream.
350
+ }
351
+
352
+ return { chartId };
353
+ },
354
+ });
355
+ }
356
+
357
+ /**
358
+ * Expand a {@link ChartPlan} into a full Echarts `EChartsOption`
359
+ * JSON. Centralized here so the planner agent only fills in the
360
+ * compact plan shape; tooltip / animation / color / grid defaults
361
+ * stay consistent across charts and are easy to tune without
362
+ * retraining model behaviour.
363
+ */
364
+ function planToEchartsOption(
365
+ plan: ChartPlan,
366
+ fallbackTitle: string,
367
+ ): Record<string, unknown> {
368
+ const baseTitle = plan.title ?? fallbackTitle;
369
+ const grid = { left: 48, right: 24, top: 56, bottom: 48, containLabel: true };
370
+
371
+ if (plan.chartType === "pie") {
372
+ return {
373
+ title: { text: baseTitle, left: "center" },
374
+ tooltip: { trigger: "item" },
375
+ legend: { bottom: 0 },
376
+ series: [
377
+ {
378
+ name: plan.series[0]?.name ?? baseTitle,
379
+ type: "pie",
380
+ radius: ["35%", "65%"],
381
+ data: plan.series[0]?.data ?? [],
382
+ },
383
+ ],
384
+ };
385
+ }
386
+
387
+ if (plan.chartType === "scatter") {
388
+ return {
389
+ title: { text: baseTitle, left: "center" },
390
+ tooltip: { trigger: "item" },
391
+ legend: { bottom: 0 },
392
+ grid,
393
+ xAxis: { type: "value", name: plan.xAxisLabel },
394
+ yAxis: { type: "value", name: plan.yAxisLabel },
395
+ series: plan.series.map((s) => ({
396
+ name: s.name,
397
+ type: "scatter",
398
+ data: s.data,
399
+ })),
400
+ };
401
+ }
402
+
403
+ // bar / line / area share the same axis layout.
404
+ const isArea = plan.chartType === "area";
405
+ const seriesType = plan.chartType === "bar" ? "bar" : "line";
406
+ return {
407
+ title: { text: baseTitle, left: "center" },
408
+ tooltip: { trigger: "axis" },
409
+ legend: { bottom: 0 },
410
+ grid,
411
+ xAxis: {
412
+ type: "category",
413
+ data: plan.categories ?? [],
414
+ name: plan.xAxisLabel,
415
+ },
416
+ yAxis: { type: "value", name: plan.yAxisLabel },
417
+ series: plan.series.map((s) => ({
418
+ name: s.name,
419
+ type: seriesType,
420
+ data: s.data,
421
+ smooth: seriesType === "line",
422
+ ...(isArea ? { areaStyle: {} } : {}),
423
+ })),
424
+ };
425
+ }