import {z} from 'zod' export type GetApiApiPastRunsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const pastRunSummary = z.object({run_id: z.string().nullable(), dir_name: z.string(), status: z.string(), run_mode: z.string().nullable().optional(), started_at: z.string().nullable(), completed_at: z.string().nullable().optional(), simulation_date: z.string().nullable(), llm_provider: z.string().nullable(), tickers: z.array(z.string()).nullable(), initial_cash_balance: z.number().nullable().optional(), broker_start_equity: z.number().nullable().optional(), leverage_multiplier: z.number().nullable().optional(), initial_buying_power: z.number().nullable().optional(), spx_start_price: z.number().nullable().optional(), spx_end_price: z.number().nullable().optional(), broker_account_id: z.string().nullable().optional(), broker_account_name: z.string().nullable().optional(), results_summary: z.record(z.string(), z.unknown()).nullable().optional()}); export const pastRunsListResponse = z.object({runs: z.array(pastRunSummary)}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * List Past Runs * * GET /api/past-runs */ export const getApiApiPastRuns = async (options: GetApiApiPastRunsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/past-runs${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(pastRunsListResponse, await res.json()) };