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

guitar-tools.ts

144 lines4.1 KB

1import { toolDefinition } from '@tanstack/ai'
2import { clientTools as createClientTools } from '@tanstack/ai-client'
3import { z } from 'zod'
4import guitars from '@/data/example-guitars'
5​
6// Tool definition for getting guitars
7export const getGuitarsToolDef = toolDefinition({
8 name: 'getGuitars',
9 description: 'Get all products from the database',
10 inputSchema: z.object({}),
11 outputSchema: z.array(
12 z.object({
13 id: z.number(),
14 name: z.string(),
15 image: z.string(),
16 description: z.string(),
17 shortDescription: z.string(),
18 price: z.number(),
19 }),
20 ),
21})
22​
23// Server implementation
24export const getGuitars = getGuitarsToolDef.server(() => guitars)
25​
26// Tool definition for guitar recommendation
27export const recommendGuitarTool = toolDefinition({
28 name: 'recommendGuitar',
29 description:
30 '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.',
31 inputSchema: z.object({
32 id: z
33 .string()
34 .describe(
35 'The ID of the guitar to recommend (from the getGuitars results)',
36 ),
37 }),
38 outputSchema: z.object({
39 id: z.string(),
40 }),
41})
42​
43export const recommendGuitarToolClient = recommendGuitarTool.client(
44 async (args) => {
45 return { id: args.id }
46 },
48​
49// Tool definition for personal preference
50export const getPersonalGuitarPreferenceTool = toolDefinition({
51 name: 'getPersonalGuitarPreference',
52 description:
53 "Get the user's guitar preference from their local browser storage",
54 inputSchema: z.object({}),
55 outputSchema: z.object({
56 preference: z.string(),
57 }),
58})
59​
60export const getPersonalGuitarPreferenceToolClient =
61 getPersonalGuitarPreferenceTool.client(async () => {
62 return { preference: 'acoustic' }
63 })
64​
65// Tool definition for wish list (needs approval)
66export const addToWishListTool = toolDefinition({
67 name: 'addToWishList',
68 description: "Add a guitar to the user's wish list (requires approval)",
69 inputSchema: z.object({
70 guitarId: z.string(),
71 }),
72 outputSchema: z.object({
73 success: z.boolean(),
74 guitarId: z.string(),
75 totalItems: z.number(),
76 }),
77 needsApproval: true,
78})
79​
80export const addToWishListToolClient = addToWishListTool.client((args) => {
81 const wishList = JSON.parse(localStorage.getItem('wishList') || '[]')
82 wishList.push(args.guitarId)
83 localStorage.setItem('wishList', JSON.stringify(wishList))
84 return {
85 success: true,
86 guitarId: args.guitarId,
87 totalItems: wishList.length,
88 }
89})
90​
91// Tool definition for add to cart (server + client)
92export const addToCartTool = toolDefinition({
93 name: 'addToCart',
94 description: 'Add a guitar to the shopping cart (requires approval)',
95 inputSchema: z.object({
96 guitarId: z.string(),
97 quantity: z.number(),
98 }),
99 outputSchema: z.object({
100 success: z.boolean(),
101 cartId: z.string(),
102 guitarId: z.string(),
103 quantity: z.number(),
104 totalItems: z.number(),
105 }),
106 needsApproval: true,
107})
108​
109export const addToCartToolServer = addToCartTool.server(async (args) => {
110 return {
111 success: true,
112 cartId: 'CART_' + Date.now(),
113 guitarId: args.guitarId,
114 quantity: args.quantity,
115 totalItems: args.quantity,
116 }
117})
118​
119export const addToCartToolClient = addToCartTool.client(async (args) => {
120 return {
121 success: true,
122 cartId: 'CART_CLIENT_' + Date.now(),
123 guitarId: args.guitarId,
124 quantity: args.quantity,
125 totalItems: args.quantity,
126 }
127})
128​
129// Server tools array - can use definitions directly or server implementations
130export const serverTools = [
131 getGuitars, // Server tool
132 recommendGuitarTool, // Definition without execute - server will see it but won't call
133 getPersonalGuitarPreferenceTool, // Definition for client
134 addToWishListTool, // Definition for approval flow
135 addToCartToolServer,
137​
138// Client tools array with proper type inference
139export const clientTools = createClientTools(
140 recommendGuitarToolClient,
141 getPersonalGuitarPreferenceToolClient,
142 addToWishListToolClient,
143 addToCartToolClient,