import {z} from 'zod' export type SimpleActionCommand = {game_id: string, guid: string, reasoning: unknown}; export type CreateApiApiCmdACTION7Args = {body: SimpleActionCommand}; export type CreateApiApiCmdACTION7Options = {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 simple action 7 * * POST /api/cmd/ACTION7 */ export const createApiApiCmdACTION7 = async (args: CreateApiApiCmdACTION7Args, options?: CreateApiApiCmdACTION7Options) => { 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/ACTION7${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()) };