import {z} from 'zod' export type BoundingBox = {x1: number, x2: number, y1: number, y2: number}; export type TextElementInput = {bbox: BoundingBox, text: string}; export type LocateRequest = {image: string, model?: (string | null) | undefined, query: string, texts?: Array | undefined}; export type CreateApiApiV1ScreenshotsLocateArgs = {body: LocateRequest}; export type CreateApiApiV1ScreenshotsLocateOptions = {baseUrl?: string | undefined, AxilioApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const locateBBox = z.object({height: z.number().int().gt(0), width: z.number().int().gt(0), x: z.number().int().gte(0), y: z.number().int().gte(0)}); export const locateResponse = z.object({bbox: locateBBox.nullable().optional(), completion_tokens: z.number().int().gte(0).optional(), confidence: z.number().gte(0).lte(1), cost_microdollars: z.number().int().gte(0), found: z.boolean(), latency_ms: z.number().int().gte(0), matched_text_index: z.number().int().gte(0).nullable().optional(), model: z.string(), prompt_tokens: z.number().int().gte(0).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 }; /** * Locate In Screenshot * * POST /api/v1/screenshots:locate */ export const createApiApiV1ScreenshotsLocate = async (args: CreateApiApiV1ScreenshotsLocateArgs, options?: CreateApiApiV1ScreenshotsLocateOptions) => { 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:locate${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(locateResponse, await res.json()) };