import { defineSource } from '@jaspers-ai/sdk' import { z } from 'zod' import { HOSTS, baseField, iso, list, upper, type Params } from './client' // Tick data: trades and quotes for a stock or option ticker, newest first. const input = z.object({ ticker: z.string().min(1).describe('A stock ticker like AAPL, or an option like O:SPY251219C00600000.'), date: z.string().optional().describe('One day, YYYY-MM-DD. Leave out to use the timestamp bounds or the latest data.'), from: z.string().optional().describe('Earliest timestamp, ISO date-time or nanoseconds (timestamp.gte).'), to: z.string().optional().describe('Latest timestamp, ISO date-time or nanoseconds (timestamp.lte).'), order: z.enum(['asc', 'desc']).default('desc').describe('desc for newest first.'), limit: z.number().int().min(1).max(50000).default(1000).describe('Most rows to answer.'), base: baseField, }) type Args = z.infer function params(args: Args): Params { return { timestamp: args.date, 'timestamp.gte': args.from, 'timestamp.lte': args.to, order: args.order, sort: 'timestamp', limit: Math.min(args.limit, 50000), } } const joined = (v: unknown) => (Array.isArray(v) ? v.join(', ') : null) const trades = defineSource({ description: 'Trades for a ticker: time, price, size, exchange, conditions, tape, sequence number. Newest first.', hosts: HOSTS, input, async run(args, ctx) { const t = upper(args.ticker) const res = await list(ctx, args.base, `/v3/trades/${t}`, params(args), args.limit) const rows = res.rows.map((r) => ({ ticker: t, time: iso(r.sip_timestamp ?? r.participant_timestamp), price: r.price ?? null, size: r.size ?? null, exchange: r.exchange ?? null, conditions: joined(r.conditions), tape: r.tape ?? null, sequence_number: r.sequence_number ?? null, id: r.id ?? null, })) return { rows, more: res.more, pages: res.pages } }, }) const quotes = defineSource({ description: 'NBBO quotes for a ticker: time, bid, bid size, ask, ask size, exchanges, conditions. Newest first.', hosts: HOSTS, input, async run(args, ctx) { const t = upper(args.ticker) const res = await list(ctx, args.base, `/v3/quotes/${t}`, params(args), args.limit) const rows = res.rows.map((r) => ({ ticker: t, time: iso(r.sip_timestamp ?? r.participant_timestamp), bid: r.bid_price ?? null, bid_size: r.bid_size ?? null, bid_exchange: r.bid_exchange ?? null, ask: r.ask_price ?? null, ask_size: r.ask_size ?? null, ask_exchange: r.ask_exchange ?? null, conditions: joined(r.conditions), tape: r.tape ?? null, sequence_number: r.sequence_number ?? null, })) return { rows, more: res.more, pages: res.pages } }, }) export const tickSources = { trades, quotes }