import {z} from 'zod' export type ResponsesOutputTextLogprobTopLogprob = {token: string, bytes: Array, logprob: number} | {[key: string]: unknown}; export type ResponsesOutputTextLogprob = {token: string, bytes: Array, logprob: number, top_logprobs: Array} | {[key: string]: unknown}; export type ResponsesSummary = {text: string, type: 'summary_text'} | {[key: string]: unknown}; export type ResponsesContent = {text: string, type: 'reasoning_text'} | {[key: string]: unknown}; export type CountResponseInputTokensRequest = {model: string, input: string | Array<{content: string | Array<{text: string, type: 'input_text', prompt_cache_breakpoint?: ({mode: 'explicit'} | {[key: string]: unknown} | null) | undefined} | {[key: string]: unknown} | {detail: 'low' | 'high' | 'auto' | 'original', type: 'input_image', file_id?: (string | null) | undefined, image_url?: (string | null) | undefined, prompt_cache_breakpoint?: ({mode: 'explicit'} | {[key: string]: unknown} | null) | undefined} | {[key: string]: unknown} | {type: 'input_file', detail?: ('auto' | 'low' | 'high' | null) | undefined, file_data?: (string | null) | undefined, file_id?: (string | null) | undefined, file_url?: (string | null) | undefined, filename?: (string | null) | undefined, prompt_cache_breakpoint?: ({mode: 'explicit'} | {[key: string]: unknown} | null) | undefined} | {[key: string]: unknown}>, role: 'user' | 'assistant' | 'system' | 'developer', phase?: ('commentary' | 'final_answer' | null) | undefined, type?: ('message' | null) | undefined} | {[key: string]: unknown} | {content: Array<{text: string, type: 'input_text', prompt_cache_breakpoint?: ({mode: 'explicit'} | {[key: string]: unknown} | null) | undefined} | {[key: string]: unknown} | {detail: 'low' | 'high' | 'auto' | 'original', type: 'input_image', file_id?: (string | null) | undefined, image_url?: (string | null) | undefined, prompt_cache_breakpoint?: ({mode: 'explicit'} | {[key: string]: unknown} | null) | undefined} | {[key: string]: unknown} | {type: 'input_file', detail?: ('auto' | 'low' | 'high' | null) | undefined, file_data?: (string | null) | undefined, file_id?: (string | null) | undefined, file_url?: (string | null) | undefined, filename?: (string | null) | undefined, prompt_cache_breakpoint?: ({mode: 'explicit'} | {[key: string]: unknown} | null) | undefined} | {[key: string]: unknown}>, role: 'user' | 'system' | 'developer', status?: ('in_progress' | 'completed' | 'incomplete' | null) | undefined, type?: ('message' | null) | undefined} | {[key: string]: unknown} | {id: string, content: Array<{annotations: Array<{file_id: string, filename: string, index: number, type: 'file_citation'} | {[key: string]: unknown} | {end_index: number, start_index: number, title: string, type: 'url_citation', url: string} | {[key: string]: unknown} | {container_id: string, end_index: number, file_id: string, filename: string, start_index: number, type: 'container_file_citation'} | {[key: string]: unknown} | {file_id: string, index: number, type: 'file_path'} | {[key: string]: unknown}>, text: string, type: 'output_text', logprobs?: (Array | null) | undefined} | {[key: string]: unknown} | {refusal: string, type: 'refusal'} | {[key: string]: unknown}>, role: 'assistant', status: 'in_progress' | 'completed' | 'incomplete', type: 'message', phase?: ('commentary' | 'final_answer' | null) | undefined} | {[key: string]: unknown} | {arguments: string, call_id: string, name: string, type: 'function_call', id?: (string | null) | undefined, caller?: ({type: 'direct'} | {[key: string]: unknown} | {caller_id: string, type: 'program'} | {[key: string]: unknown} | null) | undefined, namespace?: (string | null) | undefined, status?: ('in_progress' | 'completed' | 'incomplete' | null) | undefined} | {[key: string]: unknown} | {call_id: string, output: string | Array<{text: string, type: 'input_text', prompt_cache_breakpoint?: ({mode: 'explicit'} | {[key: string]: unknown} | null) | undefined} | {[key: string]: unknown} | {type: 'input_image', detail?: ('low' | 'high' | 'auto' | 'original' | null) | undefined, file_id?: (string | null) | undefined, image_url?: (string | null) | undefined, prompt_cache_breakpoint?: ({mode: 'explicit'} | {[key: string]: unknown} | null) | undefined} | {[key: string]: unknown} | {type: 'input_file', detail?: ('auto' | 'low' | 'high' | null) | undefined, file_data?: (string | null) | undefined, file_id?: (string | null) | undefined, file_url?: (string | null) | undefined, filename?: (string | null) | undefined, prompt_cache_breakpoint?: ({mode: 'explicit'} | {[key: string]: unknown} | null) | undefined} | {[key: string]: unknown}>, type: 'function_call_output', id?: (string | null) | undefined, caller?: ({type: 'direct'} | {[key: string]: unknown} | {caller_id: string, type: 'program'} | {[key: string]: unknown} | null) | undefined, status?: ('in_progress' | 'completed' | 'incomplete' | null) | undefined} | {[key: string]: unknown} | {id: string, summary: Array, type: 'reasoning', content?: (Array | null) | undefined, encrypted_content?: (string | null) | undefined, status?: ('in_progress' | 'completed' | 'incomplete' | null) | undefined} | {[key: string]: unknown}>, instructions?: (string | null) | undefined, parallel_tool_calls?: boolean | undefined, previous_response_id: unknown, truncation?: 'disabled' | undefined, tools?: (Array<{name: string, parameters?: ({[key: string]: unknown} | null) | undefined, strict?: (boolean | null) | undefined, type: 'function', allowed_callers?: (Array<'direct' | 'programmatic'> | null) | undefined, defer_loading?: (boolean | null) | undefined, description?: (string | null) | undefined, output_schema?: ({[key: string]: unknown} | null) | undefined} | {[key: string]: unknown} | {description: string, name: string, tools: Array<{name: string, type: 'function', allowed_callers?: (Array<'direct' | 'programmatic'> | null) | undefined, defer_loading?: (boolean | null) | undefined, description?: (string | null) | undefined, output_schema?: ({[key: string]: unknown} | null) | undefined, parameters: unknown, strict?: (boolean | null) | undefined} | {[key: string]: unknown} | {name: string, type: 'custom', allowed_callers?: (Array<'direct' | 'programmatic'> | null) | undefined, defer_loading?: (boolean | null) | undefined, description?: (string | null) | undefined, format?: ({type: 'text'} | {[key: string]: unknown} | {definition: string, syntax: 'lark' | 'regex', type: 'grammar'} | {[key: string]: unknown} | null) | undefined} | {[key: string]: unknown}>, type: 'namespace'} | {[key: string]: unknown}>) | undefined, tool_choice?: ('none' | 'auto' | 'required' | {name: string, type: 'function'} | {[key: string]: unknown}) | undefined, reasoning?: ({effort?: ('none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'max' | null) | undefined} | null) | undefined, conversation: unknown, prompt: unknown, text?: ({format?: ({type: 'text'} | {[key: string]: unknown} | {name: string, schema: {[key: string]: unknown}, type: 'json_schema', description?: (string | null) | undefined, strict?: (boolean | null) | undefined} | {[key: string]: unknown} | {type: 'json_object'} | {[key: string]: unknown} | null) | undefined, verbosity?: ('low' | 'medium' | 'high' | null) | undefined} | {[key: string]: unknown} | null) | undefined} | {[key: string]: unknown}; export type CreateApiV1ResponsesInputTokensArgs = {body: CountResponseInputTokensRequest}; export type CreateApiV1ResponsesInputTokensOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const countResponseInputTokensResponse = z.object({input_tokens: z.number().int(), object: z.literal("response.input_tokens")}).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 }; /** * Count Input Tokens * * POST /v1/responses/input_tokens */ export const createApiV1ResponsesInputTokens = async (args: CreateApiV1ResponsesInputTokensArgs, options?: CreateApiV1ResponsesInputTokensOptions) => { const {baseUrl = "https://api.mistral.ai", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) 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(countResponseInputTokensResponse, await res.json()) };