import {z} from 'zod' export type ScreenshotBase64 = string | string | string; export type LlmConfig = {provider?: ('managed' | 'anthropic' | 'openai') | undefined, model?: (string | null) | undefined, grounding_model?: (string | null) | undefined, compaction_model?: (string | null) | undefined, code_agent_model?: (string | null) | undefined}; export type GroundRequest = {screenshot: ScreenshotBase64, element: string, screen_width?: (number | null) | undefined, screen_height?: (number | null) | undefined, llm?: LlmConfig | undefined}; export type CreateApiV1GroundArgs = {'Idempotency-Key'?: string | undefined, 'X-LLM-Provider'?: ('anthropic' | 'openai') | undefined, 'X-LLM-Api-Key'?: string | undefined, 'X-LLM-Model'?: string | undefined, body: GroundRequest}; export type CreateApiV1GroundOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const usageInfo = z.object({input_tokens: z.number().int().optional(), output_tokens: z.number().int().optional(), credits_charged: z.number().int().optional(), cost_cents: z.number().int().optional(), breakdown: z.array(z.object({item: z.enum(["base", "trajectory", "hd_images", "engine", "custom_prompt"]), credits: z.number().int(), count: z.number().int().nullable().optional()})).nullable().optional(), billed: z.boolean().optional(), llm_provider: z.enum(["managed", "anthropic", "openai"]).optional(), llm_model: z.string().nullable().optional(), llm_key_fingerprint: z.string().min(12).max(12).nullable().optional(), llm_key_source: z.enum(["header", "stored"]).nullable().optional(), platform_cost_exempt: z.boolean().optional()}); export const groundResponse = z.object({request_id: z.string(), x: z.number().int(), y: z.number().int(), screen_width: z.number().int(), screen_height: z.number().int(), usage: usageInfo}); 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 }; /** * Coordinate grounding * * POST /v1/ground * * Also accepts auth: bearerAuth */ export const createApiV1Ground = async (args: CreateApiV1GroundArgs, options?: CreateApiV1GroundOptions) => { const {baseUrl = "https://coasty.ai", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (args['X-LLM-Provider'] != null) headers.set("X-LLM-Provider", String(args['X-LLM-Provider'])) if (args['X-LLM-Api-Key'] != null) headers.set("X-LLM-Api-Key", String(args['X-LLM-Api-Key'])) if (args['X-LLM-Model'] != null) headers.set("X-LLM-Model", String(args['X-LLM-Model'])) if (apiKey !== undefined) headers.set("X-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/ground${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(groundResponse, await res.json()) };