Open History to see the last commit
1import { AI, tool } from "@tanstack/ai";
2import { OpenAIAdapter } from "@tanstack/ai-openai";
4/**
5 * NEW PATTERN: Define tools with the exact Tool structure.
6 * No magic conversions - you provide exactly what the AI expects!
7 *
8 * Benefits:
9 * - Full transparency - you see the exact structure
10 * - No hidden conversions or transformations
11 * - Complete control over the tool definition
12 * - Types automatically inferred from parameters schema!
13 */
15const tools = {
16 // Simple tool with no arguments
17 getCurrentTime: tool({
18 type: "function",
19 function: {
20 name: "getCurrentTime",
21 description: "Get the current time",
22 parameters: {
23 type: "object",
24 properties: {},
25 required: [],
26 },
27 },
28 execute: async () => {
29 return new Date().toISOString();
30 },
31 }),
33 // Tool with required and optional arguments
34 // Type is automatically inferred from the parameters!
35 getWeather: tool({
36 type: "function",
37 function: {
38 name: "getWeather",
39 description: "Get the current weather for a location",
40 parameters: {
41 type: "object",
42 properties: {
43 location: {
44 type: "string",
45 description: "The city name",
46 },
47 units: {
48 type: "string",
49 description: "Temperature units (celsius or fahrenheit)",
50 enum: ["celsius", "fahrenheit"],
51 },
52 },
53 required: ["location"], // Only location is required
54 },
55 },
56 execute: async (args) => {
57 // ✅ args is automatically typed as { location: string; units?: string }
58 console.log(`Getting weather for ${args.location} in ${args.units || "fahrenheit"}`);
59 return JSON.stringify({
60 location: args.location,
61 temperature: 72,
62 units: args.units || "fahrenheit",
63 condition: "sunny",
64 });
65 },
66 }),
68 // Tool with all required arguments
69 searchPlaces: tool({
70 type: "function",
71 function: {
72 name: "searchPlaces",
73 description: "Search for places near a location",
74 parameters: {
75 type: "object",
76 properties: {
77 query: {
78 type: "string",
79 description: "What to search for (e.g., 'restaurants', 'hotels')",
80 },
81 latitude: {
82 type: "number",
83 description: "Latitude coordinate",
84 },
85 longitude: {
86 type: "number",
87 description: "Longitude coordinate",
88 },
89 radius: {
90 type: "number",
91 description: "Search radius in meters",
92 minimum: 0,
93 maximum: 50000,
94 },
95 },
96 required: ["query", "latitude", "longitude"],
97 },
98 },
99 execute: async (args) => {
100 // ✅ args is automatically typed as { query: string; latitude: number; longitude: number; radius?: number }
101 const radius = args.radius || 1000;
102 console.log(
103 `Searching for "${args.query}" near (${args.latitude}, ${args.longitude}) within ${radius}m`
104 );
105 return JSON.stringify({
106 results: [
107 { name: "Place 1", distance: 100 },
108 { name: "Place 2", distance: 250 },
109 ],
110 });
111 },
112 }),
113} as const;
115// Initialize AI with tools
116const ai = new AI({
117 adapters: {
118 openai: new OpenAIAdapter({
119 apiKey: process.env.OPENAI_API_KEY || "",
120 }),
121 },
122 tools, // Pass the tools object directly - no wrappers!
123 systemPrompts: ["You are a helpful assistant with access to various tools."],
124});
126async function main() {
127 // Use tools in chat
128 const response = await ai.chat({
129 adapter: "openai",
130 model: "gpt-4",
131 messages: [
132 {
133 role: "user",
134 content: "What's the weather like in San Francisco? Also what time is it?",
135 },
136 ],
137 tools: ["getWeather", "getCurrentTime"], // ✅ Type-safe tool names
138 toolChoice: "auto",
139 maxIterations: 5,
140 });
142 console.log("Response:", response.message.content);
143}
145// Uncomment to run:
146// main().catch(console.error);