import {z} from 'zod' export type ResponsesInputTextPart = {type: 'input_text' | 'text', text: string}; export type ImageUrl = {url: string, detail?: ('low' | 'high' | 'auto') | undefined}; export type ResponsesInputImagePart = {type: 'input_image' | 'image_url', image_url?: (string | ImageUrl) | undefined}; export type ResponsesInputContentPart = ResponsesInputTextPart | ResponsesInputImagePart; export type ResponsesInputItem = {role?: ('system' | 'user' | 'assistant' | 'tool') | undefined, content?: (string | Array) | undefined} | {[key: string]: unknown}; export type ResponsesRequest = {model: string, input?: (string | Array) | undefined, instructions?: string | undefined, stream?: boolean | undefined, temperature?: number | undefined, max_output_tokens?: number | undefined, tools?: Array<{[key: string]: unknown}> | undefined, tool_choice?: (string | {[key: string]: unknown}) | undefined, flagged_categories?: Array | undefined} | {[key: string]: unknown}; export type CreateApiV1ResponsesArgs = {'X-Free-Tier'?: 'true' | undefined, body: ResponsesRequest}; export type CreateApiV1ResponsesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const responsesOutputContentPart = z.object({type: z.literal("output_text").optional(), text: z.string().optional(), annotations: z.array(z.record(z.string(), z.unknown())).optional()}).and(z.record(z.string(), z.unknown())); export const responsesOutputItem = z.object({id: z.string().optional(), type: z.literal("message").optional(), status: z.string().optional(), role: z.literal("assistant").optional(), content: z.array(responsesOutputContentPart).optional()}).and(z.record(z.string(), z.unknown())); export const responsesUsage = z.object({input_tokens: z.number().int().optional(), output_tokens: z.number().int().optional(), total_tokens: z.number().int().optional()}); export const responsesResponse = z.object({id: z.string(), object: z.literal("response"), created_at: z.number().int().optional(), status: z.string(), model: z.string(), output: z.array(responsesOutputItem).optional(), usage: responsesUsage.optional(), remaining_credits: z.number().int().nullable().optional(), estimated_credits_used: z.number().int().optional(), estimated_cost_usd: z.number().optional()}).and(z.record(z.string(), z.unknown())); 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 }; /** * Create a response * * POST /v1/responses */ export const createApiV1Responses = async (args: CreateApiV1ResponsesArgs, options?: CreateApiV1ResponsesOptions) => { const {baseUrl = "https://api.abliteration.ai", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Free-Tier'] != null) headers.set("X-Free-Tier", String(args['X-Free-Tier'])) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/responses${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(responsesResponse, await res.json()) };