import {z} from 'zod' export type TaskStatus = 'IDLE' | 'CANCELED' | 'COMPLETED' | 'FAILED' | 'RUNNING' | 'TERMINATED' | 'TIMED_OUT' | 'DELETED'; export type TaskRelationships = 'agents'; export type GetApiTasksArgs = {agent_id?: (string | null) | undefined, namespace_id?: (string | null) | undefined, agent_name?: (string | null) | undefined, branch_id?: (string | null) | undefined, status?: Array | undefined, created_after?: (string | null) | undefined, created_before?: (string | null) | undefined, include_total?: boolean | undefined, limit?: number | undefined, page_number?: number | undefined, order_by?: (string | null) | undefined, order_direction?: string | undefined, relationships?: Array | undefined}; export type GetApiTasksOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const agentInputType = z.enum(["text", "json"]); export const agentStatus = z.enum(["ACTIVE", "DELETED"]); export const agent = z.object({agent_input_type: agentInputType.nullable().optional(), created_at: z.string(), description: z.string(), id: z.string(), name: z.string(), namespace_id: z.string(), registered_at: z.string().nullable().optional(), registration_metadata: z.record(z.string(), z.unknown()).nullable().optional(), status: agentStatus.optional(), status_reason: z.string().nullable().optional(), updated_at: z.string()}); export const taskStatus = z.enum(["IDLE", "CANCELED", "COMPLETED", "FAILED", "RUNNING", "TERMINATED", "TIMED_OUT", "DELETED"]); export const taskResponse = z.object({agents: z.array(agent).nullable().optional(), created_at: z.string().nullable().optional(), created_version_id: z.string().nullable().optional(), current_version_id: z.string().nullable().optional(), filesystem_id: z.string(), id: z.string(), name: z.string().nullable().optional(), namespace_id: z.string(), params: z.record(z.string(), z.unknown()).nullable().optional(), status: taskStatus.nullable().optional(), status_reason: z.string().nullable().optional(), task_metadata: z.record(z.string(), z.unknown()).nullable().optional(), updated_at: z.string().nullable().optional()}); export const getApiTasksResponse = z.array(taskResponse); 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 /tasks */ export const getApiTasks = async (args: GetApiTasksArgs, options: GetApiTasksOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.agent_id != null) query.append("agent_id", String(args.agent_id)) if (args.namespace_id != null) query.append("namespace_id", String(args.namespace_id)) if (args.agent_name != null) query.append("agent_name", String(args.agent_name)) if (args.branch_id != null) query.append("branch_id", String(args.branch_id)) for (const item of args.status ?? []) query.append("status", String(item)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.include_total != null) query.append("include_total", String(args.include_total)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page_number != null) query.append("page_number", String(args.page_number)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (args.order_direction != null) query.append("order_direction", String(args.order_direction)) for (const item of args.relationships ?? []) query.append("relationships", String(item)) const url = `${baseUrl.replace(/\/+$/, '')}/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(getApiTasksResponse, await res.json()) };