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 EstimateInputTokensReq = {model: string, input: string | Array, instructions?: string | undefined, tools?: Array | undefined, tool_choice?: 'auto' | undefined, text?: ({format?: ({type?: 'text' | undefined}) | undefined}) | undefined, reasoning?: ({effort?: ('minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'max' | 'none') | undefined}) | undefined}; export type CreateApiV1ResponsesInputTokensArgs = {'Content-Type': 'application/json', body: EstimateInputTokensReq}; export type CreateApiV1ResponsesInputTokensOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const estimateInputTokensResp = z.object({object: z.literal("response.input_tokens"), input_tokens: z.number().int()}); 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 }; /** * Estimate Input Tokens * * POST /v1/responses/input_tokens */ export const createApiV1ResponsesInputTokens = async (args: CreateApiV1ResponsesInputTokensArgs, options?: CreateApiV1ResponsesInputTokensOptions) => { 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/input_tokens${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(estimateInputTokensResp, await res.json()) };