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

realtime-tools.ts

167 lines4.8 KB

1import { toolDefinition } from '@tanstack/ai'
2import { z } from 'zod'
3​
4// Tool to get current time - useful for voice assistants
5export const getCurrentTimeToolDef = toolDefinition({
6 name: 'getCurrentTime',
7 description:
8 'Get the current date and time. Use this when the user asks what time it is or the current date.',
9 inputSchema: z.object({
10 timezone: z
11 .string()
12 .optional()
13 .describe('Optional timezone like "America/New_York" or "Europe/London"'),
14 }),
15 outputSchema: z.object({
16 time: z.string(),
17 date: z.string(),
18 timezone: z.string(),
19 }),
20})
21​
22// Tool to get weather - common voice assistant use case
23export const getWeatherToolDef = toolDefinition({
24 name: 'getWeather',
25 description:
26 'Get the current weather for a location. Use this when the user asks about the weather.',
27 inputSchema: z.object({
28 location: z
29 .string()
30 .describe(
31 'The city and state/country, e.g. "San Francisco, CA" or "London, UK"',
32 ),
33 }),
34 outputSchema: z.object({
35 location: z.string(),
36 temperature: z.number(),
37 unit: z.string(),
38 condition: z.string(),
39 humidity: z.number(),
40 }),
41})
42​
43// Tool to set a reminder - demonstrates user interaction
44export const setReminderToolDef = toolDefinition({
45 name: 'setReminder',
46 description:
47 'Set a reminder for the user. Use this when the user asks to be reminded about something.',
48 inputSchema: z.object({
49 message: z.string().describe('What to remind the user about'),
50 inMinutes: z.number().describe('How many minutes from now to remind'),
51 }),
52 outputSchema: z.object({
53 success: z.boolean(),
54 message: z.string(),
55 remindAt: z.string(),
56 }),
57})
58​
59// Tool to search knowledge base - useful for assistants with specific knowledge
60export const searchKnowledgeToolDef = toolDefinition({
61 name: 'searchKnowledge',
62 description:
63 'Search a knowledge base for information. Use this to find specific facts or documentation.',
64 inputSchema: z.object({
65 query: z.string().describe('The search query'),
66 }),
67 outputSchema: z.object({
68 results: z.array(
69 z.object({
70 title: z.string(),
71 snippet: z.string(),
72 }),
73 ),
74 }),
75})
76​
77// Client-side implementation of getCurrentTime
78export const getCurrentTimeClient = getCurrentTimeToolDef.client(
79 ({ timezone }) => {
80 const now = new Date()
81 const tz = timezone || Intl.DateTimeFormat().resolvedOptions().timeZone
82​
83 return {
84 time: now.toLocaleTimeString('en-US', { timeZone: tz }),
85 date: now.toLocaleDateString('en-US', {
86 weekday: 'long',
87 year: 'numeric',
88 month: 'long',
89 day: 'numeric',
90 timeZone: tz,
91 }),
92 timezone: tz,
93 }
94 },
96​
97// Client-side implementation of getWeather (mock data for demo)
98export const getWeatherClient = getWeatherToolDef.client(({ location }) => {
99 // Mock weather data for demo purposes
100 const conditions = ['Sunny', 'Partly Cloudy', 'Cloudy', 'Rainy', 'Snowy']
101 const randomCondition =
102 conditions[Math.floor(Math.random() * conditions.length)]!
103 const randomTemp = Math.floor(Math.random() * 30) + 50 // 50-80°F
104 const randomHumidity = Math.floor(Math.random() * 50) + 30 // 30-80%
105​
106 return {
107 location,
108 temperature: randomTemp,
109 unit: 'F',
110 condition: randomCondition,
111 humidity: randomHumidity,
112 }
113})
114​
115// Client-side implementation of setReminder
116export const setReminderClient = setReminderToolDef.client(
117 ({ message, inMinutes }) => {
118 const remindAt = new Date(Date.now() + inMinutes * 60 * 1000)
119​
120 // In a real app, you'd schedule a notification here
121 console.log(
122 `[Reminder] Will remind about "${message}" at ${remindAt.toLocaleTimeString()}`,
123 )
124​
125 // For demo purposes, show an alert after the specified time
126 setTimeout(
127 () => {
128 alert(`Reminder: ${message}`)
129 },
130 inMinutes * 60 * 1000,
131 )
132​
133 return {
134 success: true,
135 message: `Reminder set: "${message}"`,
136 remindAt: remindAt.toLocaleTimeString(),
137 }
138 },
140​
141// Client-side implementation of searchKnowledge (mock data for demo)
142export const searchKnowledgeClient = searchKnowledgeToolDef.client(
143 ({ query }) => {
144 // Mock search results for demo
145 const mockResults = [
146 {
147 title: `Result for: ${query}`,
148 snippet: `This is a mock search result for the query "${query}". In a real application, this would return actual search results from a knowledge base.`,
149 },
150 {
151 title: 'Additional Information',
152 snippet:
153 'More relevant information would appear here based on your search query.',
154 },
155 ]
156​
157 return { results: mockResults }
158 },
160​
161// Export all client tools as an array for easy use
162export const realtimeClientTools = [
163 getCurrentTimeClient,
164 getWeatherClient,
165 setReminderClient,
166 searchKnowledgeClient,
167] as const