import {z} from 'zod' export type ComplexActionCommand = {game_id: string, guid: string, x: number, y: number, reasoning: unknown}; export type CreateApiApiCmdACTION6Args = {body: ComplexActionCommand}; export type CreateApiApiCmdACTION6Options = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const frameResponse = z.object({game_id: z.string(), guid: z.string(), frame: z.array(z.array(z.array(z.number().int().gte(0).lte(15)))), state: z.enum(["NOT_PLAYED", "NOT_FINISHED", "WIN", "GAME_OVER"]), levels_completed: z.number().int().gte(0).lte(254), win_levels: z.number().int().gte(0).lte(254), action_input: z.object({id: z.number().int().gte(0).lte(7).optional(), data: z.record(z.string(), z.unknown()).optional(), reasoning: z.unknown()}), full_reset: z.boolean().optional(), available_actions: z.array(z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7)]))}); 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 }; /** * Execute complex action (requires x,y) * * POST /api/cmd/ACTION6 */ export const createApiApiCmdACTION6 = async (args: CreateApiApiCmdACTION6Args, options?: CreateApiApiCmdACTION6Options) => { const {baseUrl = "https://three.arcprize.org", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/cmd/ACTION6${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(frameResponse, await res.json()) };