import {z} from 'zod' export type ContentPart = {type: 'input_text' | 'output_text' | 'input_image' | 'input_video', text?: string | undefined, image_url?: (string | {url: string, detail?: ('low' | 'default' | 'high') | undefined}) | undefined, video_url?: (string | {url: string, fps?: number | undefined, detail?: ('low' | 'default' | 'high') | undefined, max_long_side_pixel?: number | undefined}) | undefined}; export type InputItem = {type?: ('message' | 'function_call' | 'function_call_output' | 'reasoning') | undefined, role?: ('user' | 'assistant' | 'system' | 'developer' | 'tool') | undefined, content?: (string | Array) | undefined, call_id?: string | undefined, name?: string | undefined, arguments?: string | undefined, output?: (string | Array) | undefined, summary?: (Array<{type?: 'summary_text' | undefined, text?: string | undefined}>) | undefined}; export type Tool = {type: 'function', name: string, description?: string | undefined, parameters?: Record | undefined}; export type CreateResponseReq = {model: string, service_tier?: ('standard' | 'priority') | undefined, input: string | Array, instructions?: string | undefined, max_output_tokens?: number | undefined, temperature?: number | undefined, top_p?: number | undefined, stream?: boolean | undefined, tools?: Array | undefined, tool_choice?: 'auto' | undefined, metadata?: {[key: string]: string} | undefined, prompt_cache_key?: string | undefined, text?: ({format?: ({type?: 'text' | undefined}) | undefined}) | undefined, reasoning?: ({effort?: ('minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'max' | 'none') | undefined}) | undefined}; export type CreateApiV1ResponsesArgs = {'Content-Type': 'application/json', body: CreateResponseReq}; export type CreateApiV1ResponsesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const outputItem = z.union([z.object({id: z.string().optional(), type: z.literal("message").optional(), status: z.literal("completed").optional(), role: z.literal("assistant").optional(), content: z.array(z.object({type: z.literal("output_text").optional(), text: z.string().optional(), annotations: z.array(z.unknown()).optional()})).optional()}), z.object({id: z.string().optional(), type: z.literal("reasoning").optional(), status: z.literal("completed").optional(), summary: z.array(z.unknown()).optional(), content: z.array(z.object({type: z.literal("reasoning_text").optional(), text: z.string().optional()})).optional()}), z.object({id: z.string().optional(), type: z.literal("function_call").optional(), status: z.literal("completed").optional(), call_id: z.string().optional(), name: z.string().optional(), arguments: z.string().optional()})]); export const usage = z.object({input_tokens: z.number().int().optional(), input_tokens_details: z.object({cached_tokens: z.number().int().optional()}).optional(), output_tokens: z.number().int().optional(), output_tokens_details: z.object({reasoning_tokens: z.number().int().optional()}).optional(), total_tokens: z.number().int().optional()}); export const createResponseResp = z.object({id: z.string().optional(), object: z.literal("response").optional(), created_at: z.number().int().optional(), model: z.string().optional(), status: z.enum(["completed", "incomplete", "failed"]).optional(), output: z.array(outputItem).optional(), output_text: z.string().nullable().optional(), usage: usage.optional(), error: z.object({code: z.string().optional(), message: z.string().optional()}).nullable().optional(), incomplete_details: z.object({reason: z.enum(["max_output_tokens", "content_filter"]).optional()}).nullable().optional(), parallel_tool_calls: z.boolean().optional(), store: z.boolean().optional(), truncation: z.literal("disabled").optional(), previous_response_id: z.string().nullable().optional(), conversation: z.object({}).nullable().optional(), service_tier: z.string().nullable().optional(), safety_identifier: z.string().nullable().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 }; /** * Create Response * * POST /v1/responses */ export const createApiV1Responses = async (args: CreateApiV1ResponsesArgs, options?: CreateApiV1ResponsesOptions) => { const {baseUrl = "https://api.minimax.io", 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['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) 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(createResponseResp, await res.json()) };