Open History to see the last commit
1import { z } from 'zod'
2import { toolDefinition } from '@tanstack/ai'
3import type { ToolExecutionContext } from '@tanstack/ai'
4import type { Report } from './types'
5import { createReportState, deleteReportState } from './report-storage'
6
7/**
8 * Tool to create a new report.
9 * The report will automatically open in the UI so the user can watch it being built.
10 */
11export const newReportTool = toolDefinition({
12 name: 'new_report',
13 description: `Create a new report for displaying data visualizations.
14The report will automatically open in the UI so the user can watch it being built.
15After creating a report, use execute_typescript with external_report_* functions to add components.`,
16 inputSchema: z.object({
17 id: z
18 .string()
19 .describe('Unique identifier for the report (snake_case recommended)'),
20 title: z.string().describe('Display title shown at the top of the report'),
21 }),
22 outputSchema: z.object({
23 success: z.boolean(),
24 reportId: z.string(),
25 message: z.string(),
26 }),
27}).server(async ({ id, title }, context?: ToolExecutionContext) => {
28 const report: Report = {
29 id,
30 title,
31 createdAt: Date.now(),
32 updatedAt: Date.now(),
33 }
34
35 createReportState(report)
36
37 // Emit event to create report and auto-select it
38 const emitCustomEvent = context?.emitCustomEvent || (() => {})
39 emitCustomEvent('report:created', {
40 report,
41 autoSelect: true,
42 })
43
44 return {
45 success: true,
46 reportId: id,
47 message: `Report "${title}" created and opened. Use external_report_* functions in execute_typescript to add components.`,
48 }
49})
50
51/**
52 * Tool to list all reports in the current session.
53 * Note: Reports are stored in client-side state, so this tool returns
54 * information about what reports exist for the LLM's awareness.
55 */
56export const listReportsTool = toolDefinition({
57 name: 'list_reports',
58 description:
59 'List all reports in the current session. Returns report metadata including IDs and titles.',
60 inputSchema: z.object({}),
61 outputSchema: z.object({
62 message: z.string(),
63 hint: z.string(),
64 }),
65}).server(async () => {
66 // Reports are managed client-side, so we can't enumerate them from the server.
67 // This tool exists mainly for LLM awareness of the report system.
68 return {
69 message:
70 'Reports are managed in the client UI. Check the reports sidebar to see existing reports.',
71 hint: 'Use new_report to create a new report, or reference an existing report ID when adding components.',
72 }
73})
74
75/**
76 * Tool to delete a report by ID.
77 */
78export const deleteReportTool = toolDefinition({
79 name: 'delete_report',
80 description:
81 'Delete a report by ID. This will remove the report from the UI.',
82 inputSchema: z.object({
83 reportId: z.string().describe('ID of the report to delete'),
84 }),
85 outputSchema: z.object({
86 success: z.boolean(),
87 message: z.string(),
88 }),
89}).server(async ({ reportId }, context?: ToolExecutionContext) => {
90 deleteReportState(reportId)
91 const emitCustomEvent = context?.emitCustomEvent || (() => {})
92 emitCustomEvent('report:deleted', { reportId })
93
94 return {
95 success: true,
96 message: `Report "${reportId}" deleted.`,
97 }
98})
99
100// Export all report tools as a collection
101export const reportTools = [newReportTool, listReportsTool, deleteReportTool]