import {z} from 'zod' export type GetApiV1RunsRunIdLogArgs = {limit?: number | undefined, after_step?: number | undefined, include_events?: boolean | undefined}; export type GetApiV1RunsRunIdLogOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const runAction = z.object({seq: z.number().int(), tool: z.string(), args: z.record(z.string(), z.unknown()).optional(), ok: z.boolean().nullable().optional(), error: z.string().nullable().optional()}); export const runEvent = z.object({seq: z.number().int().optional(), type: z.string().optional(), data: z.record(z.string(), z.unknown()).optional(), created_at: z.string().nullable().optional()}); export const runStepDetail = z.object({step: z.number().int().optional(), attempt: z.number().int().optional(), started_at: z.string().nullable().optional(), ended_at: z.string().nullable().optional(), analysis: z.string().nullable().optional(), next_action: z.string().nullable().optional(), grounded_action: z.string().nullable().optional(), action_result: z.string().nullable().optional(), actions: z.array(runAction).optional(), credits_charged: z.number().int().optional(), cost_cents: z.number().int().optional(), screenshot_index: z.number().int().nullable().optional(), error: z.string().nullable().optional(), events: z.array(runEvent).nullable().optional()}); export const listRunLogResponse = z.object({object: z.literal("list"), data: z.array(runStepDetail), lifecycle: z.array(runEvent).optional(), has_more: z.boolean().optional(), next_after_step: z.number().int().nullable().optional(), live: z.boolean().optional(), steps_completed: z.number().int().optional(), steps_logged: z.number().int().optional(), log_complete: z.boolean().optional(), request_id: z.string().optional()}); 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 }; /** * Read what a run actually did, step by step * * GET /v1/runs/{run_id}/log * * Also accepts auth: bearerAuth */ export const getApiV1RunsRunIdLog = async (args: GetApiV1RunsRunIdLogArgs, options?: GetApiV1RunsRunIdLogOptions) => { const {baseUrl = "https://coasty.ai", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.after_step != null) query.append("after_step", String(args.after_step)) if (args.include_events != null) query.append("include_events", String(args.include_events)) if (apiKey !== undefined) headers.set("X-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/runs/${encodeURIComponent(String(args.run_id))}/log${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(listRunLogResponse, await res.json()) };