import {z} from 'zod' export type FiFastRequest = {prompt: string, system?: (string | null) | undefined, tools?: (Array<{[key: string]: unknown}> | null) | undefined, toolConfig?: ({[key: string]: unknown} | null) | undefined, responseSchema?: ({[key: string]: unknown} | null) | undefined, responseMimeType?: (string | null) | undefined, model?: ('gemini-3.1-pro-preview' | 'gemini-3.1-flash-lite-preview' | 'gemini-3-flash-preview' | 'gemini-2.5-flash-preview-05-20' | 'gemini-2.5-flash') | undefined, thinkingBudget?: (number | null) | undefined, temperature?: number | undefined, maxOutputTokens?: number | undefined}; export type CreateApiV4FiFastArgs = {body: FiFastRequest}; export type CreateApiV4FiFastOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const fiFastResponse = z.object({content: z.record(z.string(), z.unknown()), usage: z.record(z.string(), z.unknown()).nullable().optional(), model: z.string(), finishReason: z.string().nullable().optional()}); export const responseMeta = z.object({requestId: z.string().optional(), timestamp: z.string().optional(), durationMs: z.number().int().nullable().optional()}); export const successResponseFiFastResponse = z.object({success: z.literal(true), data: fiFastResponse, meta: responseMeta.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 }; /** * Thin authenticated proxy to Vertex AI generateContent * * POST /v4/fi-fast */ export const createApiV4FiFast = async (args: CreateApiV4FiFastArgs, options: CreateApiV4FiFastOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v4/fi-fast${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(successResponseFiFastResponse, await res.json()) };