Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

product-tools.ts

243 lines4.8 KB

1import { z } from 'zod'
2import { toolDefinition } from '@tanstack/ai'
3​
4interface Product {
5 id: string
6 name: string
7 brand: string
8 price: number
9 category: string
10 color: string
11 sizeRange: string
13​
14const SHOES: Array<Product> = [
15 {
16 id: 'shoe-01',
17 name: 'Air Max 90',
18 brand: 'Nike',
19 price: 130,
20 category: 'Lifestyle',
21 color: 'White/Red',
22 sizeRange: '6-13',
23 },
24 {
25 id: 'shoe-02',
26 name: 'Ultra Boost 22',
27 brand: 'Adidas',
28 price: 190,
29 category: 'Running',
30 color: 'Core Black',
31 sizeRange: '6-14',
32 },
33 {
34 id: 'shoe-03',
35 name: 'Gel-Kayano 30',
36 brand: 'Asics',
37 price: 160,
38 category: 'Running',
39 color: 'Black/Electric Blue',
40 sizeRange: '7-13',
41 },
42 {
43 id: 'shoe-04',
44 name: 'Fresh Foam X 1080v13',
45 brand: 'New Balance',
46 price: 165,
47 category: 'Running',
48 color: 'Navy/Red',
49 sizeRange: '7-14',
50 },
51 {
52 id: 'shoe-05',
53 name: 'Suede Classic XXI',
54 brand: 'Puma',
55 price: 75,
56 category: 'Lifestyle',
57 color: 'Peacoat/White',
58 sizeRange: '6-13',
59 },
60 {
61 id: 'shoe-06',
62 name: 'Chuck Taylor All Star',
63 brand: 'Converse',
64 price: 60,
65 category: 'Lifestyle',
66 color: 'Optical White',
67 sizeRange: '5-13',
68 },
69 {
70 id: 'shoe-07',
71 name: 'Old Skool',
72 brand: 'Vans',
73 price: 70,
74 category: 'Lifestyle',
75 color: 'Black/White',
76 sizeRange: '5-13',
77 },
78 {
79 id: 'shoe-08',
80 name: 'Pegasus 41',
81 brand: 'Nike',
82 price: 140,
83 category: 'Running',
84 color: 'Volt/Black',
85 sizeRange: '6-15',
86 },
87 {
88 id: 'shoe-09',
89 name: 'Samba OG',
90 brand: 'Adidas',
91 price: 110,
92 category: 'Lifestyle',
93 color: 'White/Black/Gum',
94 sizeRange: '6-13',
95 },
96 {
97 id: 'shoe-10',
98 name: 'GEL-1130',
99 brand: 'Asics',
100 price: 120,
101 category: 'Lifestyle',
102 color: 'White/Clay Canyon',
103 sizeRange: '6-13',
104 },
105 {
106 id: 'shoe-11',
107 name: 'Clifton 9',
108 brand: 'Hoka',
109 price: 145,
110 category: 'Running',
111 color: 'Black/White',
112 sizeRange: '7-14',
113 },
114 {
115 id: 'shoe-12',
116 name: 'Speedcat OG',
117 brand: 'Puma',
118 price: 90,
119 category: 'Lifestyle',
120 color: 'Red/White',
121 sizeRange: '6-12',
122 },
123 {
124 id: 'shoe-13',
125 name: '990v6',
126 brand: 'New Balance',
127 price: 200,
128 category: 'Lifestyle',
129 color: 'Grey',
130 sizeRange: '6-14',
131 },
132 {
133 id: 'shoe-14',
134 name: 'Air Jordan 1 Low',
135 brand: 'Nike',
136 price: 115,
137 category: 'Lifestyle',
138 color: 'Chicago',
139 sizeRange: '6-13',
140 },
141 {
142 id: 'shoe-15',
143 name: 'Bondi 8',
144 brand: 'Hoka',
145 price: 165,
146 category: 'Running',
147 color: 'Coastal Sky/All Aboard',
148 sizeRange: '7-14',
149 },
150 {
151 id: 'shoe-16',
152 name: 'Gazelle',
153 brand: 'Adidas',
154 price: 100,
155 category: 'Lifestyle',
156 color: 'Collegiate Green/White',
157 sizeRange: '6-13',
158 },
159 {
160 id: 'shoe-17',
161 name: 'Ghost 16',
162 brand: 'Brooks',
163 price: 140,
164 category: 'Running',
165 color: 'Peacoat/Silver',
166 sizeRange: '7-14',
167 },
168 {
169 id: 'shoe-18',
170 name: 'Cloud 5',
171 brand: 'On',
172 price: 150,
173 category: 'Running',
174 color: 'All White',
175 sizeRange: '7-14',
176 },
177 {
178 id: 'shoe-19',
179 name: 'Cloudmonster 2',
180 brand: 'On',
181 price: 180,
182 category: 'Running',
183 color: 'Undyed/Frost',
184 sizeRange: '7-14',
185 },
186 {
187 id: 'shoe-20',
188 name: 'Alphafly 3',
189 brand: 'Nike',
190 price: 250,
191 category: 'Racing',
192 color: 'Volt/Concord',
193 sizeRange: '6-13',
194 },
196​
197const PAGE_SIZE = 10
198const TOTAL_PAGES = 2
199​
200export const getProductListPageTool = toolDefinition({
201 name: 'getProductListPage',
202 description:
203 'Get a page of product IDs from the catalog. Returns product IDs for the requested page and the total number of pages. Each page contains up to 10 product IDs.',
204 inputSchema: z.object({
205 page: z.number().describe('1-based page number'),
206 }),
207 outputSchema: z.object({
208 productIds: z.array(z.string()),
209 totalPages: z.number(),
210 }),
211}).server(async ({ page }) => {
212 const start = (page - 1) * PAGE_SIZE
213 const end = start + PAGE_SIZE
214 return {
215 productIds: SHOES.slice(start, end).map((s) => s.id),
216 totalPages: TOTAL_PAGES,
217 }
218})
219​
220export const getProductByIDTool = toolDefinition({
221 name: 'getProductByID',
222 description: 'Get full product details for a single product by its ID.',
223 inputSchema: z.object({
224 id: z.string().describe('Product ID, e.g. "shoe-01"'),
225 }),
226 outputSchema: z.object({
227 id: z.string(),
228 name: z.string(),
229 brand: z.string(),
230 price: z.number(),
231 category: z.string(),
232 color: z.string(),
233 sizeRange: z.string(),
234 }),
235}).server(async ({ id }) => {
236 const shoe = SHOES.find((s) => s.id === id)
237 if (!shoe) {
238 throw new Error(`Product not found: ${id}`)
239 }
240 return shoe
241})
242​
243export const productTools = [getProductListPageTool, getProductByIDTool]