import {z} from 'zod' export type GetApiV1TasksArgs = {status?: ('idle' | 'queued' | 'running' | 'awaiting_input' | 'completed' | 'cancelled' | 'failed') | undefined, projectId?: string | undefined, cursor?: string | undefined, limit?: number | undefined}; export type GetApiV1TasksOptions = {baseUrl?: string | undefined, personalApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const listPublicApiTasksResponseDto = z.object({tasks: z.array(z.object({id: z.string(), title: z.string(), status: z.enum(["idle", "queued", "running", "awaiting_input", "completed", "cancelled", "failed"]), executionMode: z.enum(["adam", "external"]).optional(), projectId: z.string().nullable(), model: z.object({slug: z.string().min(1).max(64).nullable(), fast: z.boolean(), effort: z.enum(["low", "medium", "high", "xhigh", "max"]).nullable()}).nullable(), pendingInteraction: z.object({id: z.string(), type: z.enum(["question", "approval"]), prompt: z.string().nullable()}).nullable(), createdAt: z.string(), updatedAt: z.string()})), nextCursor: z.array(z.string())}); 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 tasks * * GET /v1/tasks * * Also accepts auth: adamId */ export const getApiV1Tasks = async (args: GetApiV1TasksArgs, options?: GetApiV1TasksOptions) => { const {baseUrl = "https://api.adam.new", fetch: fetchFn = fetch, personalApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.projectId != null) query.append("projectId", String(args.projectId)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (personalApiKey !== undefined) headers.set('authorization', `Bearer ${personalApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/tasks${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(listPublicApiTasksResponseDto, await res.json()) };