import {z} from 'zod' export type GetApiV2CasesCaseIdRunsArgs = {case_id: string}; export type GetApiV2CasesCaseIdRunsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2CasesCaseIdRunsResponse = z.object({runs: z.array(z.object({id: z.string(), case_queue_item_id: z.string(), agent_id: z.string(), agent_name: z.string(), status: z.enum(["not_started", "pending", "starting", "running", "waiting", "completed", "failed", "interrupted"]), source: z.string().nullable(), created_at: z.string(), started_at: z.string().nullable(), completed_at: z.string().nullable(), claimed_at: z.string(), has_pending_human_request: z.boolean(), pending_human_request_title: z.string().nullable(), evaluation: z.object({passed: z.number(), total: z.number(), final_comment: z.string().optional(), status: z.enum(["ready", "unavailable", "in_progress"]), severityCounts: z.object({critical: z.number().int().gte(0).lte(9007199254740991), medium: z.number().int().gte(0).lte(9007199254740991), low: z.number().int().gte(0).lte(9007199254740991)}).optional()}), user_id: z.string().nullable(), user_name: z.string().nullable(), user_email: z.string().nullable()}))}); 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 Case Runs * * GET /v2/cases/{case_id}/runs */ export const getApiV2CasesCaseIdRuns = async (args: GetApiV2CasesCaseIdRunsArgs, options?: GetApiV2CasesCaseIdRunsOptions) => { const {baseUrl = "https://api.duvo.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/cases/${encodeURIComponent(String(args.case_id))}/runs${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(getApiV2CasesCaseIdRunsResponse, await res.json()) };