import {z} from 'zod' export type TextChunk = {type?: 'text' | undefined, text: string}; export type BuiltInConnectors = 'web_search' | 'web_search_premium' | 'code_interpreter' | 'image_generation' | 'document_library' | 'mistral_mcp'; export type ToolReferenceChunk = {type?: 'tool_reference' | undefined, tool: BuiltInConnectors | string, title: string, url?: (string | null) | undefined, favicon?: (string | null) | undefined, description?: (string | null) | undefined}; export type ReferenceChunk = {type?: 'reference' | undefined, reference_ids: Array}; export type ThinkChunk = {type?: 'thinking' | undefined, thinking: Array, signature?: (string | null) | undefined, closed?: boolean | undefined}; export type SystemMessageContentChunks = TextChunk | ThinkChunk; export type SystemMessage = {role?: 'system' | undefined, content: string | Array}; export type ImageDetail = 'low' | 'auto' | 'high'; export type ImageURL = {url: string, detail?: (ImageDetail | null) | undefined}; export type ImageURLChunk = {type?: 'image_url' | undefined, image_url: ImageURL | string}; export type DocumentURLChunk = {type?: 'document_url' | undefined, document_url: string, document_name?: (string | null) | undefined}; export type FileChunk = {type?: 'file' | undefined, file_id: string}; export type AudioChunk = {type?: 'input_audio' | undefined, input_audio: string | string}; export type ResourceChunk = {type?: 'resource' | undefined, uri: string, content: TextChunk | ImageURLChunk}; export type ResourceLinkChunk = {type?: 'resource_link' | undefined, uri: string, metadata?: ({[key: string]: string | number | number | boolean} | null) | undefined}; export type ContentChunk = TextChunk | ImageURLChunk | DocumentURLChunk | ReferenceChunk | FileChunk | ThinkChunk | AudioChunk | ResourceChunk | ResourceLinkChunk; export type UserMessage = {role?: 'user' | undefined, content: string | Array | null}; export type ToolTypes = 'function'; export type FunctionCall = {name: string, arguments: {[key: string]: unknown} | string}; export type ToolCall = {id?: string | undefined, type?: ToolTypes | undefined, function: FunctionCall, index?: number | undefined}; export type AssistantMessage = {role?: 'assistant' | undefined, content?: (string | Array | null) | undefined, tool_calls?: (Array | null) | undefined, prefix?: boolean | undefined}; export type ToolMessage = {role?: 'tool' | undefined, content: string | Array | null, tool_call_id?: (string | null) | undefined, name?: (string | null) | undefined}; export type ChatModerationRequest = {input: Array | Array>, model: string}; export type CreateApiV1ChatModerationsArgs = {body: ChatModerationRequest}; export type CreateApiV1ChatModerationsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const moderationObject = z.object({categories: z.record(z.string(), z.boolean()).optional(), category_scores: z.record(z.string(), z.number()).optional()}); export const moderationResponse = z.object({id: z.string(), model: z.string(), results: z.array(moderationObject)}); 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 }; /** * Chat Moderations * * POST /v1/chat/moderations */ export const createApiV1ChatModerations = async (args: CreateApiV1ChatModerationsArgs, options?: CreateApiV1ChatModerationsOptions) => { 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/chat/moderations${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(moderationResponse, await res.json()) };