first commit
This commit is contained in:
56
examples/tools/weather.js
Normal file
56
examples/tools/weather.js
Normal file
@@ -0,0 +1,56 @@
|
||||
'use strict';
|
||||
|
||||
import { defineTool } from '../../src/index.js';
|
||||
|
||||
/**
|
||||
* Weather tool — returns static weather data for demo/testing.
|
||||
*/
|
||||
const weatherTool = defineTool({
|
||||
name: 'get_weather',
|
||||
description: 'Get the current weather for a given city.',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
additionalProperties: false,
|
||||
required: ['city'],
|
||||
properties: {
|
||||
city: {
|
||||
type: 'string',
|
||||
description: 'City name, e.g. "Berlin"',
|
||||
},
|
||||
unit: {
|
||||
type: 'string',
|
||||
enum: ['celsius', 'fahrenheit'],
|
||||
description: 'Temperature unit (default: celsius)',
|
||||
},
|
||||
},
|
||||
},
|
||||
invoke: async ({ city, unit = 'celsius' }) => {
|
||||
const db = {
|
||||
berlin: { temp: 18, condition: 'partly cloudy', humidity: 62 },
|
||||
munich: { temp: 22, condition: 'sunny', humidity: 45 },
|
||||
hamburg: { temp: 15, condition: 'rainy', humidity: 80 },
|
||||
london: { temp: 14, condition: 'overcast', humidity: 75 },
|
||||
paris: { temp: 20, condition: 'sunny', humidity: 50 },
|
||||
tokyo: { temp: 28, condition: 'humid', humidity: 85 },
|
||||
'new york': { temp: 25, condition: 'clear', humidity: 55 },
|
||||
};
|
||||
|
||||
const entry = db[city.toLowerCase()];
|
||||
if (!entry) {
|
||||
const available = Object.keys(db).map((c) => `"${c}"`).join(', ');
|
||||
throw new Error(`City "${city}" not found in database. Available cities are: ${available}.`);
|
||||
}
|
||||
let temp = entry.temp;
|
||||
if (unit === 'fahrenheit') temp = Math.round(temp * 9 / 5 + 32);
|
||||
|
||||
return {
|
||||
city,
|
||||
temperature: temp,
|
||||
unit,
|
||||
condition: entry.condition,
|
||||
humidity: entry.humidity,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export { weatherTool };
|
||||
Reference in New Issue
Block a user