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

city-tools.ts

231 lines6.5 KB

1import { toolDefinition } from '@tanstack/ai'
2import { z } from 'zod'
3​
4interface CitySummary {
5 name: string
6 country: string
7 populationMillions: number
8}
9​
10interface CityDetails extends CitySummary {
11 climate: string
12 landmarks: Array<string>
13 cuisine: Array<string>
14 funFacts: Array<string>
16​
17const CITIES: Array<CityDetails> = [
18 {
19 name: 'Tokyo',
20 country: 'Japan',
21 populationMillions: 37.4,
22 climate: 'Humid subtropical with warm summers and mild winters',
23 landmarks: ['Shibuya Crossing', 'Senso-ji Temple', 'Tokyo Skytree'],
24 cuisine: ['Sushi', 'Ramen', 'Monjayaki'],
25 funFacts: [
26 "Tokyo has the world's busiest train stations.",
27 'It hosts more Michelin-starred restaurants than any other city.',
28 ],
29 },
30 {
31 name: 'Paris',
32 country: 'France',
33 populationMillions: 11.0,
34 climate: 'Oceanic climate with cool winters and mild summers',
35 landmarks: ['Eiffel Tower', 'Louvre Museum', 'Notre-Dame Cathedral'],
36 cuisine: ['Croissants', 'Coq au vin', 'Macarons'],
37 funFacts: [
38 'Paris has over 400 parks and gardens.',
39 'The Louvre is the most visited museum in the world.',
40 ],
41 },
42 {
43 name: 'New York City',
44 country: 'United States',
45 populationMillions: 19.6,
46 climate: 'Humid continental with hot summers and cold winters',
47 landmarks: ['Statue of Liberty', 'Central Park', 'Times Square'],
48 cuisine: ['Bagels', 'Pizza', 'Cheesecake'],
49 funFacts: [
50 'NYC has over 800 languages spoken across its boroughs.',
51 'Central Park is larger than the principality of Monaco.',
52 ],
53 },
54 {
55 name: 'Barcelona',
56 country: 'Spain',
57 populationMillions: 5.7,
58 climate: 'Mediterranean with warm summers and mild winters',
59 landmarks: ['Sagrada Familia', 'Park Guell', 'La Rambla'],
60 cuisine: ['Paella', 'Patatas bravas', 'Crema catalana'],
61 funFacts: [
62 'Barcelona has 9 UNESCO World Heritage sites.',
63 'The city has over 4 km of beaches within city limits.',
64 ],
65 },
66 {
67 name: 'Singapore',
68 country: 'Singapore',
69 populationMillions: 5.9,
70 climate: 'Tropical rainforest climate, warm and humid year-round',
71 landmarks: ['Marina Bay Sands', 'Gardens by the Bay', 'Merlion Park'],
72 cuisine: ['Hainanese chicken rice', 'Laksa', 'Chili crab'],
73 funFacts: [
74 "Singapore has one of the world's best airport transit systems.",
75 'It is often called a city in a garden.',
76 ],
77 },
78 {
79 name: 'Cape Town',
80 country: 'South Africa',
81 populationMillions: 4.7,
82 climate: 'Mediterranean with warm dry summers and cool wet winters',
83 landmarks: ['Table Mountain', 'Robben Island', 'Cape Point'],
84 cuisine: ['Bobotie', 'Bunny chow', 'Malva pudding'],
85 funFacts: [
86 'Cape Town sits between ocean and mountains.',
87 'Table Mountain is one of the oldest mountains on Earth.',
88 ],
89 },
91​
92function normalizeCityName(name: string) {
93 return name.trim().toLowerCase()
95​
96export const listCitiesTool = toolDefinition({
97 name: 'listCities',
98 description:
99 'List available cities with country and population in millions for analysis.',
100 inputSchema: z.object({}),
101 outputSchema: z.object({
102 cities: z.array(
103 z.object({
104 name: z.string(),
105 country: z.string(),
106 populationMillions: z.number(),
107 }),
108 ),
109 total: z.number(),
110 }),
111}).server(() => {
112 const cities: Array<CitySummary> = CITIES.map((city) => ({
113 name: city.name,
114 country: city.country,
115 populationMillions: city.populationMillions,
116 }))
117​
118 return {
119 cities,
120 total: cities.length,
121 }
122})
123​
124export const getCityDetailsTool = toolDefinition({
125 name: 'getCityDetails',
126 description:
127 'Get detailed city information such as climate, landmarks, cuisine, and fun facts.',
128 inputSchema: z.object({
129 cityName: z.string().describe('City name to look up'),
130 }),
131 outputSchema: z.object({
132 found: z.boolean(),
133 city: z
134 .object({
135 name: z.string(),
136 country: z.string(),
137 populationMillions: z.number(),
138 climate: z.string(),
139 landmarks: z.array(z.string()),
140 cuisine: z.array(z.string()),
141 funFacts: z.array(z.string()),
142 })
143 .nullable(),
144 }),
145}).server(({ cityName }) => {
146 const city = CITIES.find(
147 (entry) => normalizeCityName(entry.name) === normalizeCityName(cityName),
148 )
149​
150 return {
151 found: Boolean(city),
152 city: city ?? null,
153 }
154})
155​
156export const compareCitiesTool = toolDefinition({
157 name: 'compareCities',
158 description:
159 'Compare two cities by population, climate, and tourism highlights.',
160 inputSchema: z.object({
161 firstCity: z.string().describe('First city name'),
162 secondCity: z.string().describe('Second city name'),
163 }),
164 outputSchema: z.object({
165 foundBoth: z.boolean(),
166 comparison: z
167 .object({
168 firstCity: z.object({
169 name: z.string(),
170 country: z.string(),
171 populationMillions: z.number(),
172 }),
173 secondCity: z.object({
174 name: z.string(),
175 country: z.string(),
176 populationMillions: z.number(),
177 }),
178 populationDifferenceMillions: z.number(),
179 climateSummary: z.string(),
180 travelHighlights: z.array(z.string()),
181 })
182 .nullable(),
183 missingCities: z.array(z.string()),
184 }),
185}).server(({ firstCity, secondCity }) => {
186 const first = CITIES.find(
187 (entry) => normalizeCityName(entry.name) === normalizeCityName(firstCity),
188 )
189 const second = CITIES.find(
190 (entry) => normalizeCityName(entry.name) === normalizeCityName(secondCity),
191 )
192​
193 const missingCities: Array<string> = []
194 if (!first) missingCities.push(firstCity)
195 if (!second) missingCities.push(secondCity)
196​
197 if (!first || !second) {
198 return {
199 foundBoth: false,
200 comparison: null,
201 missingCities,
202 }
203 }
204​
205 return {
206 foundBoth: true,
207 comparison: {
208 firstCity: {
209 name: first.name,
210 country: first.country,
211 populationMillions: first.populationMillions,
212 },
213 secondCity: {
214 name: second.name,
215 country: second.country,
216 populationMillions: second.populationMillions,
217 },
218 populationDifferenceMillions: Math.abs(
219 first.populationMillions - second.populationMillions,
220 ),
221 climateSummary: `${first.name}: ${first.climate}. ${second.name}: ${second.climate}.`,
222 travelHighlights: [
223 `${first.name}: ${first.landmarks[0]}`,
224 `${second.name}: ${second.landmarks[0]}`,
225 ],
226 },
227 missingCities: [],
228 }
229})
230​
231export const cityTools = [listCitiesTool, getCityDetailsTool, compareCitiesTool]