import { defineSource } from '@jaspers-ai/sdk' import { z } from 'zod' // The IMF's DataMapper, which is the World Economic Outlook and friends behind a plain JSON API. // No key. One indicator, many countries, a year at a time. const HOST = 'www.imf.org' const BASE = `https://${HOST}/external/datamapper/api/v1` async function get(ctx: { fetch: typeof fetch }, path: string): Promise { // The IMF refuses a request that does not say who it is. const response = await ctx.fetch(`${BASE}/${path}`, { headers: { 'User-Agent': 'Jaspers Terminal (https://jsprai.com)', Accept: 'application/json' }, }) if (!response.ok) throw new Error(`The IMF answered ${response.status}.`) return response.json() } export const indicators = defineSource({ description: 'Every indicator the IMF DataMapper publishes, with its code and what it measures. Read this to find the code an indicator source needs.', hosts: [HOST], input: z.object({ contains: z.string().optional().describe('Only indicators whose name holds this, like inflation or debt.') }), async run({ contains }, ctx) { const body = (await get(ctx, 'indicators')) as { indicators: Record } const wanted = contains?.trim().toLowerCase() const rows = Object.entries(body.indicators) .map(([code, one]) => ({ code, label: one.label ?? code, unit: one.unit ?? null, source: one.source ?? null })) .filter((one) => !wanted || one.label.toLowerCase().includes(wanted) || one.code.toLowerCase().includes(wanted)) .sort((a, b) => a.code.localeCompare(b.code)) return { rows } }, }) export const indicator = defineSource({ description: 'One IMF indicator for one or more countries, by year. indicator is a DataMapper code like NGDP_RPCH (real GDP growth), PCPIPCH (inflation), GGXWDG_NGDP (government debt to GDP), or LUR (unemployment). Countries are ISO three letter codes. The IMF publishes forecasts as well as history, so later years are projections.', hosts: [HOST], input: z.object({ indicator: z.string().default('NGDP_RPCH'), countries: z.array(z.string()).default(['USA']).describe('ISO three letter codes, like USA, DEU, CHN.'), from: z.number().int().optional().describe('Earliest year.'), }), async run({ indicator: code, countries, from }, ctx) { const wanted = (countries as string[]).map((one) => one.trim().toUpperCase()).filter(Boolean) const body = (await get(ctx, `${encodeURIComponent(code.trim().toUpperCase())}/${wanted.join('/')}`)) as { values?: Record>> } const byCountry = body.values?.[code.trim().toUpperCase()] if (!byCountry) throw new Error(`The IMF has no indicator called ${code}. Run the indicators source to find its code.`) const rows: { country: string; year: number; value: number }[] = [] for (const [country, years] of Object.entries(byCountry)) { // Asking for two countries answers with those two; asking for one answers with every country, // so what was asked for is what is kept. if (wanted.length > 0 && !wanted.includes(country)) continue for (const [year, value] of Object.entries(years)) { if (typeof value !== 'number') continue if (from && Number(year) < from) continue rows.push({ country, year: Number(year), value }) } } rows.sort((a, b) => a.year - b.year || a.country.localeCompare(b.country)) return { indicator: code, rows } }, })