import {z} from 'zod' export type GetApiV1TasksTaskIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const runLlmInfo = z.object({provider: z.enum(["anthropic", "openai"]), model: z.string().min(1).max(256), key_fingerprint: z.string().min(12).max(12), key_source: z.enum(["header", "stored"]), key_scrubbed: z.boolean()}); export const runResponse = z.object({id: z.string(), object: z.literal("agent.run"), status: z.enum(["queued", "running", "awaiting_human", "succeeded", "failed", "cancelled", "timed_out"]), machine_id: z.string().nullable().optional(), machine: z.object({mode: z.literal("automatic").optional(), status: z.enum(["provisioning", "starting", "reconciling", "ready", "terminated", "cleanup_failed"]).optional(), id: z.string().nullable().optional(), cleanup: z.literal("always").optional(), cleanup_status: z.enum(["pending", "terminating", "retrying", "terminated"]).optional(), error: z.record(z.string(), z.unknown()).optional()}).nullable().optional(), task: z.string().optional(), cua_version: z.string().optional(), instructions: z.string().nullable().optional(), max_steps: z.number().int().optional(), deadline_seconds: z.number().int().optional(), awaiting_human_timeout_seconds: z.number().int().optional(), on_awaiting_human: z.string().optional(), steps_completed: z.number().int().optional(), credits_charged: z.number().int().optional(), cost_cents: z.number().int().optional(), result: z.record(z.string(), z.unknown()).nullable().optional(), error: z.record(z.string(), z.unknown()).nullable().optional(), awaiting_human_reason: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), llm: runLlmInfo.nullable().optional(), webhook_url: z.string().nullable().optional(), webhook_secret: z.string().nullable().optional(), created_at: z.string().nullable().optional(), started_at: z.string().nullable().optional(), awaiting_human_since: z.string().nullable().optional(), finished_at: z.string().nullable().optional(), request_id: z.string().nullable().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 }; /** * Get a task * * GET /v1/tasks/{task_id} * * Also accepts auth: bearerAuth */ export const getApiV1TasksTaskId = async (options?: GetApiV1TasksTaskIdOptions) => { const {baseUrl = "https://coasty.ai", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("X-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/tasks/${encodeURIComponent(String(args.task_id))}${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(runResponse, await res.json()) };