Open History to see the last commit
1import { toolDefinition } from '@tanstack/ai'
2import { z } from 'zod'
3
4// 1. Get current date for relative calculations
5export const getCurrentDateTool = toolDefinition({
6 name: 'getCurrentDate',
7 description:
8 'Get the current date and time. Useful for calculating relative date ranges.',
9 inputSchema: z.object({}),
10 outputSchema: z.object({
11 iso: z.string(),
12 timestamp: z.number(),
13 year: z.number(),
14 month: z.number(),
15 day: z.number(),
16 formatted: z.string(),
17 }),
18}).server(() => {
19 const now = new Date()
20 return {
21 iso: now.toISOString(),
22 timestamp: now.getTime(),
23 year: now.getFullYear(),
24 month: now.getMonth() + 1,
25 day: now.getDate(),
26 formatted: now.toISOString().split('T')[0],
27 }
28})
29
30// 2. Calculate statistics for an array of numbers
31export const calculateStatsTool = toolDefinition({
32 name: 'calculateStats',
33 description:
34 'Calculate statistics (mean, median, min, max, stdDev, sum) for an array of numbers.',
35 inputSchema: z.object({
36 values: z.array(z.number()).describe('Array of numbers to analyze'),
37 }),
38 outputSchema: z.object({
39 mean: z.number(),
40 median: z.number(),
41 min: z.number(),
42 max: z.number(),
43 stdDev: z.number(),
44 sum: z.number(),
45 count: z.number(),
46 }),
47}).server(({ values }) => {
48 if (values.length === 0) {
49 return {
50 mean: 0,
51 median: 0,
52 min: 0,
53 max: 0,
54 stdDev: 0,
55 sum: 0,
56 count: 0,
57 }
58 }
59
60 const sorted = [...values].sort((a, b) => a - b)
61 const sum = values.reduce((acc, val) => acc + val, 0)
62 const mean = sum / values.length
63
64 const mid = Math.floor(sorted.length / 2)
65 const median =
66 sorted.length % 2 !== 0 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2
67
68 const squaredDiffs = values.map((val) => Math.pow(val - mean, 2))
69 const avgSquaredDiff =
70 squaredDiffs.reduce((acc, val) => acc + val, 0) / values.length
71 const stdDev = Math.sqrt(avgSquaredDiff)
72
73 return {
74 mean: Math.round(mean * 100) / 100,
75 median: Math.round(median * 100) / 100,
76 min: Math.min(...values),
77 max: Math.max(...values),
78 stdDev: Math.round(stdDev * 100) / 100,
79 sum,
80 count: values.length,
81 }
82})
83
84// 3. Format date for NPM API calls
85export const formatDateRangeTool = toolDefinition({
86 name: 'formatDateRange',
87 description:
88 'Calculate a date range going back from today. Returns start and end dates formatted for API calls.',
89 inputSchema: z.object({
90 daysBack: z.number().describe('Number of days to go back from today'),
91 }),
92 outputSchema: z.object({
93 startDate: z.string(),
94 endDate: z.string(),
95 }),
96}).server(({ daysBack }) => {
97 const end = new Date()
98 const start = new Date()
99 start.setDate(start.getDate() - daysBack)
100
101 return {
102 startDate: start.toISOString().split('T')[0],
103 endDate: end.toISOString().split('T')[0],
104 }
105})
106
107// Export all utility tools as a collection
108export const utilityTools = [
109 getCurrentDateTool,
110 calculateStatsTool,
111 formatDateRangeTool,
112]