import {z} from 'zod' export type GetApiV1RunsRunIdScreenshotsArgs = {limit?: number | undefined, after_index?: number | undefined, include_image?: boolean | undefined}; export type GetApiV1RunsRunIdScreenshotsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const runScreenshot = z.object({index: z.number().int(), attempt: z.number().int(), step: z.number().int(), taken_at: z.string().nullable().optional(), width: z.number().int().optional(), height: z.number().int().optional(), mime_type: z.string().optional(), size_bytes: z.number().int().optional(), sha256: z.string().nullable().optional(), degraded: z.boolean().optional(), encrypted_at_rest: z.boolean().optional(), image_b64: z.string().nullable().optional(), image_unavailable: z.boolean().nullable().optional()}); export const listRunScreenshotsResponse = z.object({object: z.literal("list"), data: z.array(runScreenshot), has_more: 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 }; /** * List run screenshots * * GET /v1/runs/{run_id}/screenshots * * Also accepts auth: bearerAuth */ export const getApiV1RunsRunIdScreenshots = async (args: GetApiV1RunsRunIdScreenshotsArgs, options?: GetApiV1RunsRunIdScreenshotsOptions) => { 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_index != null) query.append("after_index", String(args.after_index)) if (args.include_image != null) query.append("include_image", String(args.include_image)) if (apiKey !== undefined) headers.set("X-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/runs/${encodeURIComponent(String(args.run_id))}/screenshots${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(listRunScreenshotsResponse, await res.json()) };