import {z} from 'zod' export type CreateApiV1ResponsesArgs = {body: {model?: string | undefined, input: string | Array<{role: string, content: string | Array<{type?: string | undefined, text?: string | undefined, image_url?: string | undefined, file_url?: string | undefined}>}>, instructions?: string | undefined, stream?: boolean | undefined, background?: boolean | undefined, previous_response_id?: string | undefined, conversation: unknown, temperature?: number | undefined, max_output_tokens?: number | undefined, top_p?: number | undefined, reasoning?: ({effort?: ('none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh') | undefined, generate_summary?: ('auto' | 'concise' | 'detailed') | undefined, summary?: ('auto' | 'concise' | 'detailed') | undefined}) | undefined, text?: ({format?: ({type?: string | undefined, name?: string | undefined, schema?: Record | undefined}) | undefined, verbosity?: ('low' | 'medium' | 'high') | undefined}) | undefined, tools?: (Array<{type?: string | undefined, name?: string | undefined, description?: string | undefined, parameters?: Record | undefined}>) | undefined, tool_choice: unknown, parallel_tool_calls?: boolean | undefined, include?: Array | undefined, metadata?: Record | undefined, truncation?: ('auto' | 'disabled') | undefined, store?: boolean | undefined, prompt_cache_key?: string | undefined, prompt_cache_retention?: ('in-memory' | '24h') | undefined, max_tool_calls?: number | undefined, top_logprobs?: number | undefined, stream_options?: ({include_obfuscation?: boolean | undefined}) | undefined, prompt?: ({id?: string | undefined, variables?: {[key: string]: unknown} | undefined, version?: string | undefined}) | undefined, safety_identifier?: string | undefined, service_tier?: ('auto' | 'default' | 'flex' | 'priority') | undefined, user?: string | undefined} | {[key: string]: unknown}}; export type CreateApiV1ResponsesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1ResponsesResponse = z.object({id: z.string(), object: z.string(), created_at: z.number().int(), status: z.string(), background: z.boolean(), content_filters: z.unknown(), error: z.unknown(), incomplete_details: z.unknown(), instructions: z.unknown(), max_output_tokens: z.unknown(), max_tool_calls: z.unknown(), model: z.string(), output: z.array(z.object({id: z.string().optional(), type: z.string().optional(), status: z.string().optional(), content: z.array(z.object({type: z.string().optional(), annotations: z.array(z.string()).optional(), logprobs: z.array(z.string()).optional(), text: z.string().optional()})).optional(), role: z.string().optional(), phase: z.string().optional()})), output_text: z.string().nullable().optional(), parallel_tool_calls: z.boolean(), previous_response_id: z.unknown(), prompt_cache_key: z.unknown(), reasoning: z.object({effort: z.unknown(), summary: z.unknown()}), safety_identifier: z.unknown(), service_tier: z.string(), store: z.boolean(), temperature: z.number().int(), text: z.object({format: z.object({type: z.string()}), verbosity: z.string()}), tool_choice: z.string(), tools: z.array(z.string()), top_logprobs: z.number().int(), top_p: z.number().int(), truncation: z.string(), usage: z.object({input_tokens: z.number().int(), input_tokens_details: z.object({cached_tokens: z.number().int()}), output_tokens: z.number().int(), output_tokens_details: z.object({reasoning_tokens: z.number().int()}), total_tokens: z.number().int()}), user: z.unknown(), metadata: 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 }; /** * Responses * * POST /v1/responses */ export const createApiV1Responses = async (args: CreateApiV1ResponsesArgs, options?: CreateApiV1ResponsesOptions) => { const {baseUrl = "https://api.deerapi.com", 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 (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(createApiV1ResponsesResponse, await res.json()) };