import {z} from 'zod' export type GetApiTrackerArgs = {agent_id?: (string | null) | undefined, limit?: number | undefined, page_number?: number | undefined, order_by?: (string | null) | undefined, order_direction?: string | undefined, task_id: string}; export type GetApiTrackerOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const agentTaskTracker = z.object({agent_id: z.string(), created_at: z.string(), id: z.string(), last_processed_event_id: z.string().nullable().optional(), status: z.string().nullable().optional(), status_reason: z.string().nullable().optional(), task_id: z.string(), updated_at: z.string().nullable().optional()}); export const getApiTrackerResponse = z.array(agentTaskTracker); 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 Agent Task Trackers * * GET /tracker */ export const getApiTracker = async (args: GetApiTrackerArgs, options: GetApiTrackerOptions) => { 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.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)) if (args.task_id != null) query.append("task_id", String(args.task_id)) const url = `${baseUrl.replace(/\/+$/, '')}/tracker${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(getApiTrackerResponse, await res.json()) };