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

guitar-tools.ts

84 lines2.4 KB

1import { toolDefinition } from '@tanstack/ai'
2import { z } from 'zod'
3import guitars from '@/data/example-guitars'
4​
5// Tool definition for getting guitars
6export const getGuitarsToolDef = toolDefinition({
7 name: 'getGuitars',
8 description: 'Get all products from the database',
9 inputSchema: z.object({}),
10 outputSchema: z.array(
11 z.object({
12 id: z.number(),
13 name: z.string(),
14 image: z.string(),
15 description: z.string(),
16 shortDescription: z.string(),
17 price: z.number(),
18 }),
19 ),
20})
21​
22// Server implementation
23export const getGuitars = getGuitarsToolDef.server(() => guitars)
24​
25// Tool definition for guitar recommendation
26export const recommendGuitarToolDef = toolDefinition({
27 name: 'recommendGuitar',
28 description:
29 'REQUIRED tool to display a guitar recommendation to the user. This tool MUST be used whenever recommending a guitar - do NOT write recommendations yourself. This displays the guitar in a special appealing format with a buy button.',
30 inputSchema: z.object({
31 id: z
32 .union([z.string(), z.number()])
33 .describe(
34 'The ID of the guitar to recommend (from the getGuitars results)',
35 ),
36 }),
37 outputSchema: z.object({
38 id: z.number(),
39 }),
40})
41​
42// Tool definition for personal preference
43export const getPersonalGuitarPreferenceToolDef = toolDefinition({
44 name: 'getPersonalGuitarPreference',
45 description:
46 "Get the user's guitar preference from their local browser storage",
47 inputSchema: z.object({}),
48 outputSchema: z.object({
49 preference: z.string(),
50 }),
51})
52​
53// Tool definition for wish list (needs approval)
54export const addToWishListToolDef = toolDefinition({
55 name: 'addToWishList',
56 description: "Add a guitar to the user's wish list (requires approval)",
57 inputSchema: z.object({
58 guitarId: z.string(),
59 }),
60 outputSchema: z.object({
61 success: z.boolean(),
62 guitarId: z.string(),
63 totalItems: z.number(),
64 }),
65 needsApproval: true,
66})
67​
68// Tool definition for add to cart (server + client)
69export const addToCartToolDef = toolDefinition({
70 name: 'addToCart',
71 description: 'Add a guitar to the shopping cart (requires approval)',
72 inputSchema: z.object({
73 guitarId: z.string(),
74 quantity: z.number(),
75 }),
76 outputSchema: z.object({
77 success: z.boolean(),
78 cartId: z.string(),
79 guitarId: z.string(),
80 quantity: z.number(),
81 totalItems: z.number(),
82 }),
83 needsApproval: true,
84})