import {z} from 'zod' export type ResponsesReasoningRequest = {effort?: (string | null) | undefined, summary?: (string | null) | undefined, enabled?: (boolean | null) | undefined, exclude?: (boolean | null) | undefined} | {[key: string]: unknown}; export type ResponsesTextFormatRequest = {type?: (string | null) | undefined, strict?: (boolean | null) | undefined, schema?: ({[key: string]: unknown} | null) | undefined, name?: (string | null) | undefined} | {[key: string]: unknown}; export type ResponsesTextRequest = {verbosity?: (string | null) | undefined, format?: (ResponsesTextFormatRequest | null) | undefined} | {[key: string]: unknown}; export type ResponsesRequest = {model: string, instructions?: string | undefined, input?: (string | Array<{[key: string]: unknown}>) | undefined, tools?: Array<{[key: string]: unknown}> | undefined, tool_choice?: (string | {[key: string]: unknown}) | undefined, parallel_tool_calls?: boolean | undefined, reasoning?: (ResponsesReasoningRequest | null) | undefined, store?: boolean | undefined, stream?: boolean | undefined, previous_response_id?: (string | null) | undefined, include?: Array | undefined, service_tier?: (string | null) | undefined, prompt_cache_key?: (string | null) | undefined, text?: (ResponsesTextRequest | null) | undefined, max_output_tokens?: (number | null) | undefined, temperature?: (number | null) | undefined, top_p?: (number | null) | undefined, metadata?: ({[key: string]: unknown} | null) | undefined, extra_headers?: ({[key: string]: string} | null) | undefined, extra_body?: ({[key: string]: unknown} | null) | undefined, task_type?: (string | null) | undefined, include_confidence?: (boolean | null) | undefined, include_spans?: (boolean | null) | undefined} | {[key: string]: unknown}; export type CreateApiResponsesArgs = {body: ResponsesRequest}; export type CreateApiResponsesOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiResponsesResponse = 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 }; /** * Responses * * POST /responses * * Also accepts auth: BearerAuth */ export const createApiResponses = async (args: CreateApiResponsesArgs, options: CreateApiResponsesOptions) => { const {baseUrl, 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(/\/+$/, '')}/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(createApiResponsesResponse, await res.json()) };