/** * WeatherLookup — A standalone custom tool that fetches weather data. * * This file exports two things and nothing else: * 1. TOOL_METADATA — describes the tool to the smol-js scanner * 2. execute(args) — the actual function; called by the toolHarness adapter * * It has zero knowledge of how it is invoked. The harness handles argument * deserialization, protocol framing, and process lifecycle. This file is * just the logic. * * Dependencies are resolved by Bun at runtime with no package.json or * npm install required — Bun pulls them from the npm registry on demand. * * Usage in YAML: * tools: * weather: * type: WeatherLookup * * Run via CLI: * npx @samrahimi/smol-js workflow.yaml --task "..." --custom-tools-folder ./custom-tools */ // --- Dependencies: resolved by Bun natively, zero config needed --- import chalk from 'chalk'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; dayjs.extend(utc); // --------------------------------------------------------------------------- // TOOL_METADATA — parsed by CustomToolScanner at discovery time. // The `name` field MUST match this file's basename (WeatherLookup). // --------------------------------------------------------------------------- export const TOOL_METADATA = { name: 'WeatherLookup', description: 'Fetches current weather conditions for a given city using the Open-Meteo API. Returns temperature, weather condition, wind speed, and humidity.', inputs: { city: { type: 'string', description: 'The name of the city to look up weather for (e.g. "London", "Tokyo", "New York")', required: true, }, units: { type: 'string', description: 'Temperature units: "celsius" (default) or "fahrenheit"', required: false, default: 'celsius', }, }, outputType: 'string', }; // --------------------------------------------------------------------------- // Type definitions for the API responses // --------------------------------------------------------------------------- interface GeoResponse { results?: Array<{ latitude: number; longitude: number; name: string; country: string }>; } interface WeatherResponse { current_weather: { temperature: number; windspeed: number; weathercode: number; }; hourly: { relativehumidity_2m: number[]; }; } const WMO_CODES: Record = { 0: 'Clear sky', 1: 'Mainly clear', 2: 'Partly cloudy', 3: 'Overcast', 45: 'Fog', 48: 'Depositing rime fog', 51: 'Light drizzle', 53: 'Moderate drizzle', 55: 'Dense drizzle', 61: 'Slight rain', 63: 'Moderate rain', 65: 'Heavy rain', 66: 'Light freezing rain', 67: 'Heavy freezing rain', 71: 'Slight snow', 73: 'Moderate snow', 75: 'Heavy snow', 77: 'Snow grains', 80: 'Slight showers', 81: 'Moderate showers', 82: 'Violent showers', 85: 'Slight snow showers', 86: 'Heavy snow showers', 95: 'Thunderstorm', 96: 'Thunderstorm with slight hail', 99: 'Thunderstorm with heavy hail', }; // --------------------------------------------------------------------------- // execute — the tool's entry point. Just a function. Knows nothing about // how it was called or how its return value will be delivered. // --------------------------------------------------------------------------- export async function execute(args: Record): Promise { const city = args.city as string; const units = (args.units as string) || 'celsius'; if (!city) { throw new Error('"city" argument is required.'); } // Step 1: Geocode the city name const geoUrl = `https://geocoding-api.open-meteo.com/v1/search?name=${encodeURIComponent(city)}&count=1&language=en&format=json`; console.log(chalk.dim(` Geocoding "${city}"...`)); const geoRes = await fetch(geoUrl); if (!geoRes.ok) throw new Error(`Geocoding failed: HTTP ${geoRes.status}`); const geoData: GeoResponse = await geoRes.json(); if (!geoData.results || geoData.results.length === 0) { throw new Error(`City not found: "${city}". Try a different name or add a country (e.g. "Paris, France").`); } const { latitude, longitude, name: resolvedName, country } = geoData.results[0]; console.log(chalk.dim(` Resolved to: ${resolvedName}, ${country} (${latitude.toFixed(2)}, ${longitude.toFixed(2)})`)); // Step 2: Fetch current weather const tempUnit = units === 'fahrenheit' ? 'fahrenheit' : 'celsius'; const weatherUrl = `https://api.open-meteo.com/v1/forecast` + `?latitude=${latitude}&longitude=${longitude}` + `¤t_weather=true` + `&hourly=relativehumidity_2m` + `&temperature_unit=${tempUnit}` + `&windspeed_unit=mph` + `&forecast_days=1`; console.log(chalk.dim(' Fetching weather data...')); const weatherRes = await fetch(weatherUrl); if (!weatherRes.ok) throw new Error(`Weather API failed: HTTP ${weatherRes.status}`); const weatherData: WeatherResponse = await weatherRes.json(); const { temperature, windspeed, weathercode } = weatherData.current_weather; const humidity = weatherData.hourly.relativehumidity_2m[0] ?? 'N/A'; const condition = WMO_CODES[weathercode] ?? `Code ${weathercode}`; const unitLabel = tempUnit === 'fahrenheit' ? '°F' : '°C'; const now = dayjs().utc().format('YYYY-MM-DD HH:mm'); return ( `Weather in ${resolvedName}, ${country} (as of ${now} UTC):\n` + ` Condition: ${condition}\n` + ` Temperature: ${temperature}${unitLabel}\n` + ` Wind Speed: ${windspeed} mph\n` + ` Humidity: ${humidity}%` ); }