import {z} from 'zod' export type InferenceType = 'detect' | 'ocr' | 'combined'; export type InferenceRequest = {confidence_threshold?: number | undefined, image: string, inference_type?: InferenceType | undefined, nms_iou_threshold?: number | undefined, ocr_engine?: ('free' | 'premium') | undefined}; export type CreateApiApiV1ScreenshotsDetectArgs = {body: InferenceRequest}; export type CreateApiApiV1ScreenshotsDetectOptions = {baseUrl?: string | undefined, AxilioApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const contentBounds = z.object({bottom_y: z.number().int(), top_y: z.number().int()}); export const hashResult = z.object({ahash: z.string(), content_bounds: contentBounds.nullable().optional(), dhash: z.string(), histogram: z.array(z.array(z.number().int())).nullable().optional(), phash: z.string()}); export const boundingBox = z.object({x1: z.number(), x2: z.number(), y1: z.number(), y2: z.number()}); export const detection = z.object({bbox: boundingBox, class_id: z.number().int(), class_name: z.string(), confidence: z.number().gte(0).lte(1)}); export const inferenceMetadata = z.object({detect_ns: z.number().int().nullable().optional(), icon_detections_after_nms: z.number().int(), icon_detections_before_nms: z.number().int(), image_height: z.number().int(), image_width: z.number().int(), inference_time_ms: z.number(), ocr_ns: z.number().int().nullable().optional(), queue_wait_ns: z.number().int().nullable().optional()}); export const oCRResult = z.object({bbox: boundingBox, confidence: z.number().gte(0).lte(1), text: z.string()}); export const inferenceData = z.object({hash_result: hashResult.nullable().optional(), icon_detections: z.array(detection).optional(), metadata: inferenceMetadata, ocr_results: z.array(oCRResult).optional()}); export const inferenceResponse = z.object({analyzer_type: z.string().optional(), data: inferenceData, type: z.string().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 }; /** * Detect Screenshot * * POST /api/v1/screenshots:detect */ export const createApiApiV1ScreenshotsDetect = async (args: CreateApiApiV1ScreenshotsDetectArgs, options?: CreateApiApiV1ScreenshotsDetectOptions) => { const {baseUrl = "https://argus.axilio.ai", fetch: fetchFn = fetch, AxilioApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (AxilioApiKey !== undefined) headers.set("X-Axilio-Api-Key", AxilioApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/screenshots:detect${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(inferenceResponse, await res.json()) };