import {z} from 'zod' export type TextPart = {type?: 'text' | undefined, text: string}; export type SystemMessage = {role: 'system', content: string | Array}; export type DeveloperMessage = {role: 'developer', content: string | Array}; export type UserMessage = {role: 'user', content: string | Array}; export type ToolCallFunction = {name: string, arguments: string}; export type MessageToolCall = {id: string, type?: 'function' | undefined, function: ToolCallFunction}; export type FileCitation = {type?: 'file_citation' | undefined, file_id: string, filename: string, index: number, chunk_id: string, store_id: string}; export type UrlCitation = {type?: 'url_citation' | undefined, url: string, title: string, start_index: number, end_index: number, chunk_id: string}; export type AssistantMessageInput = {role: 'assistant', content?: (string | Array | null) | undefined, tool_calls?: Array | undefined, reasoning_content?: (string | null) | undefined, annotations?: (Array) | undefined}; export type ToolMessage = {role: 'tool', content: string | Array, tool_call_id: string}; export type ConditionOperator = 'eq' | 'not_eq' | 'gt' | 'gte' | 'lt' | 'lte' | 'in' | 'not_in' | 'like' | 'contains' | 'starts_with' | 'not_like' | 'regex'; export type SearchFilterCondition = {key: string, value: string, operator: ConditionOperator}; export type SearchFilterInput = {all?: (Array | null) | undefined, any?: (Array | null) | undefined, none?: (Array | null) | undefined}; export type SearchCorpusTool = {store_identifiers?: (Array | null) | undefined, type?: 'search_corpus' | undefined, filters?: (SearchFilterInput | SearchFilterCondition | Array | null) | undefined, score_threshold?: number | undefined, citations?: boolean | undefined}; export type GrepTool = {store_identifiers?: (Array | null) | undefined, type?: 'grep' | undefined, filters?: (SearchFilterInput | SearchFilterCondition | Array | null) | undefined, citations?: boolean | undefined}; export type FilterChunksTool = {store_identifiers?: (Array | null) | undefined, type?: 'filter_chunks' | undefined, filters?: (SearchFilterInput | SearchFilterCondition | Array | null) | undefined, citations?: boolean | undefined}; export type InspectMetadataTool = {store_identifiers?: (Array | null) | undefined, type?: 'inspect_metadata' | undefined, filters?: (SearchFilterInput | SearchFilterCondition | Array | null) | undefined}; export type GetChunksTool = {store_identifiers?: (Array | null) | undefined, type?: 'get_chunks' | undefined}; export type StoreSearchTool = {store_identifiers?: (Array | null) | undefined, type?: 'store_search' | undefined, filters?: (SearchFilterInput | SearchFilterCondition | Array | null) | undefined, score_threshold?: number | undefined, citations?: boolean | undefined}; export type StoreGrepTool = {store_identifiers?: (Array | null) | undefined, type?: 'store_grep' | undefined, filters?: (SearchFilterInput | SearchFilterCondition | Array | null) | undefined, citations?: boolean | undefined}; export type StoreListChunksTool = {store_identifiers?: (Array | null) | undefined, type?: 'store_list_chunks' | undefined, filters?: (SearchFilterInput | SearchFilterCondition | Array | null) | undefined, citations?: boolean | undefined}; export type MetadataFacetsTool = {store_identifiers?: (Array | null) | undefined, type?: 'store_metadata_facets' | undefined, filters?: (SearchFilterInput | SearchFilterCondition | Array | null) | undefined}; export type ListStoresTool = {type?: 'list_stores' | undefined, limit?: number | undefined}; export type FunctionDefinition = {name: string, description?: (string | null) | undefined, parameters?: ({[key: string]: unknown} | null) | undefined, strict?: (boolean | null) | undefined}; export type FunctionTool = {type?: 'function' | undefined, function: FunctionDefinition}; export type ToolChoiceFunctionName = {name: string}; export type ToolChoiceFunction = {type?: 'function' | undefined, function: ToolChoiceFunctionName}; export type ToolChoiceSearchCorpus = {type?: 'search_corpus' | undefined}; export type ToolChoiceGrep = {type?: 'grep' | undefined}; export type ToolChoiceFilterChunks = {type?: 'filter_chunks' | undefined}; export type ToolChoiceInspectMetadata = {type?: 'inspect_metadata' | undefined}; export type ToolChoiceListStores = {type?: 'list_stores' | undefined}; export type ToolChoiceStoreSearch = {type?: 'store_search' | undefined}; export type ToolChoiceStoreGrep = {type?: 'store_grep' | undefined}; export type ToolChoiceStoreListChunks = {type?: 'store_list_chunks' | undefined}; export type ToolChoiceMetadataFacets = {type?: 'store_metadata_facets' | undefined}; export type ResponseFormatText = {type?: 'text' | undefined}; export type ResponseFormatJSONObject = {type?: 'json_object' | undefined}; export type JSONSchemaResponseFormat = {name: string, schema: {[key: string]: unknown}, description?: (string | null) | undefined, strict?: (boolean | null) | undefined}; export type ResponseFormatJSONSchema = {type?: 'json_schema' | undefined, json_schema: JSONSchemaResponseFormat}; export type ContextManagementEdit = {type?: 'prune_context' | undefined}; export type ContextManagementConfig = {edits: Array}; export type CompletionCreateParams = {messages: Array, model?: string | undefined, tools?: (Array) | undefined, tool_choice?: ('auto' | 'none' | 'required' | ToolChoiceFunction | ToolChoiceSearchCorpus | ToolChoiceGrep | ToolChoiceFilterChunks | ToolChoiceInspectMetadata | ToolChoiceListStores | ToolChoiceStoreSearch | ToolChoiceStoreGrep | ToolChoiceStoreListChunks | ToolChoiceMetadataFacets) | undefined, response_format?: (ResponseFormatText | ResponseFormatJSONObject | ResponseFormatJSONSchema | null) | undefined, store?: boolean | undefined, previous_completion_id?: (string | null) | undefined, previous_messages?: (Array | null) | undefined, terminal_tool_name?: (string | null) | undefined, stream?: boolean | undefined, temperature?: (number | null) | undefined, top_p?: (number | null) | undefined, max_completion_tokens?: (number | null) | undefined, max_tokens?: (number | null) | undefined, max_tool_calls?: (number | null) | undefined, context_management?: (ContextManagementConfig | null) | undefined, parallel_tool_calls?: boolean | undefined, metadata?: ({[key: string]: string} | null) | undefined, include?: (Array | null) | undefined}; export type CreateApiV1ChatCompletionsArgs = {body: CompletionCreateParams}; export type CreateApiV1ChatCompletionsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const toolCallFunction = z.object({name: z.string(), arguments: z.string()}); export const messageToolCall = z.object({id: z.string(), type: z.literal("function").optional(), function: toolCallFunction}); export const fileCitation = z.object({type: z.literal("file_citation").optional(), file_id: z.string(), filename: z.string(), index: z.number().int(), chunk_id: z.string(), store_id: z.string()}); export const urlCitation = z.object({type: z.literal("url_citation").optional(), url: z.string(), title: z.string(), start_index: z.number().int(), end_index: z.number().int(), chunk_id: z.string()}); export const chatCompletionMessage = z.object({role: z.literal("assistant").optional(), content: z.string().nullable().optional(), refusal: z.string().nullable().optional(), tool_calls: z.array(messageToolCall).nullable().optional(), reasoning_content: z.string().nullable().optional(), annotations: z.array(z.discriminatedUnion("type", [fileCitation, urlCitation])).nullable().optional()}); export const chatCompletionChoice = z.object({index: z.number().int().optional(), message: chatCompletionMessage, finish_reason: z.enum(["stop", "tool_calls", "length"]).optional(), logprobs: z.unknown()}); export const promptTokensDetails = z.object({cached_tokens: z.number().int().optional()}); export const completionTokensDetails = z.object({reasoning_tokens: z.number().int().optional()}); export const completionUsage = z.object({prompt_tokens: z.number().int().optional(), completion_tokens: z.number().int().optional(), total_tokens: z.number().int().optional(), prompt_tokens_details: promptTokensDetails.optional(), completion_tokens_details: completionTokensDetails.optional()}); export const conditionOperator = z.enum(["eq", "not_eq", "gt", "gte", "lt", "lte", "in", "not_in", "like", "contains", "starts_with", "not_like", "regex"]); export const metadataFilter = z.object({key: z.string().min(1), operator: conditionOperator, value: z.union([z.string(), z.number().int(), z.number(), z.boolean(), z.array(z.union([z.string(), z.number().int(), z.number(), z.boolean()]))]).nullable()}); export const toolCallError = z.object({code: z.enum(["permission_denied", "invalid_arguments", "server_error"]), message: z.string()}); export const searchCorpusCallItem = z.object({type: z.literal("search_corpus_call").optional(), id: z.string(), status: z.enum(["in_progress", "completed", "failed"]).optional(), queries: z.array(z.string()).optional(), metadata_filters: z.array(metadataFilter).nullable().optional(), filter_mode: z.enum(["all", "any"]).optional(), store: z.string().nullable().optional(), results: z.array(z.record(z.string(), z.unknown())).nullable().optional(), error: toolCallError.nullable().optional()}); export const storeChunkGrepTarget = z.enum(["text", "generated"]); export const grepCallItem = z.object({type: z.literal("grep_call").optional(), id: z.string(), status: z.enum(["in_progress", "completed", "failed"]).optional(), pattern: z.string().nullable().optional(), targets: z.array(storeChunkGrepTarget).nullable().optional(), case_sensitive: z.boolean().optional(), metadata_filters: z.array(metadataFilter).nullable().optional(), filter_mode: z.enum(["all", "any"]).optional(), store: z.string().nullable().optional(), results: z.array(z.record(z.string(), z.unknown())).nullable().optional(), error: toolCallError.nullable().optional()}); export const filterChunksCallItem = z.object({type: z.literal("filter_chunks_call").optional(), id: z.string(), status: z.enum(["in_progress", "completed", "failed"]).optional(), metadata_filters: z.array(metadataFilter).nullable().optional(), filter_mode: z.enum(["all", "any"]).optional(), rank_by: z.string().nullable().optional(), direction: z.enum(["asc", "desc"]).optional(), store: z.string().nullable().optional(), results: z.array(z.record(z.string(), z.unknown())).nullable().optional(), error: toolCallError.nullable().optional()}); export const inspectMetadataCallItem = z.object({type: z.literal("inspect_metadata_call").optional(), id: z.string(), status: z.enum(["in_progress", "completed", "failed"]).optional(), store: z.string().nullable().optional(), facets: z.record(z.string(), z.unknown()).nullable().optional(), error: toolCallError.nullable().optional()}); export const getChunksCallItem = z.object({type: z.literal("get_chunks_call").optional(), id: z.string(), status: z.enum(["in_progress", "completed", "failed"]).optional(), chunk_ids: z.array(z.string()).optional(), results: z.array(z.record(z.string(), z.unknown())).nullable().optional(), error: toolCallError.nullable().optional()}); export const storeSearchCallItem = z.object({type: z.literal("store_search_call").optional(), id: z.string(), status: z.enum(["in_progress", "completed", "failed"]).optional(), queries: z.array(z.string()).optional(), metadata_filters: z.array(metadataFilter).nullable().optional(), filter_mode: z.enum(["all", "any"]).optional(), store: z.string().nullable().optional(), results: z.array(z.record(z.string(), z.unknown())).nullable().optional(), error: toolCallError.nullable().optional()}); export const storeGrepCallItem = z.object({type: z.literal("store_grep_call").optional(), id: z.string(), status: z.enum(["in_progress", "completed", "failed"]).optional(), pattern: z.string().nullable().optional(), targets: z.array(storeChunkGrepTarget).nullable().optional(), case_sensitive: z.boolean().optional(), metadata_filters: z.array(metadataFilter).nullable().optional(), filter_mode: z.enum(["all", "any"]).optional(), store: z.string().nullable().optional(), results: z.array(z.record(z.string(), z.unknown())).nullable().optional(), error: toolCallError.nullable().optional()}); export const storeListChunksCallItem = z.object({type: z.literal("store_list_chunks_call").optional(), id: z.string(), status: z.enum(["in_progress", "completed", "failed"]).optional(), metadata_filters: z.array(metadataFilter).nullable().optional(), filter_mode: z.enum(["all", "any"]).optional(), rank_by: z.string().nullable().optional(), direction: z.enum(["asc", "desc"]).optional(), store: z.string().nullable().optional(), results: z.array(z.record(z.string(), z.unknown())).nullable().optional(), error: toolCallError.nullable().optional()}); export const metadataFacetsCallItem = z.object({type: z.literal("store_metadata_facets_call").optional(), id: z.string(), status: z.enum(["in_progress", "completed", "failed"]).optional(), store: z.string().nullable().optional(), facets: z.record(z.string(), z.unknown()).nullable().optional(), error: toolCallError.nullable().optional()}); export const listStoresResult = z.object({name: z.string(), description: z.string().nullable().optional(), connectors: z.array(z.string()).optional()}); export const listStoresCallItem = z.object({type: z.literal("list_stores_call").optional(), id: z.string(), status: z.enum(["in_progress", "completed", "failed"]).optional(), cursor: z.string().nullable().optional(), stores: z.array(listStoresResult).nullable().optional(), has_more: z.boolean().optional(), next_cursor: z.string().nullable().optional(), error: toolCallError.nullable().optional()}); export const appliedPruneContextEdit = z.object({type: z.literal("prune_context").optional(), calls: z.number().int(), cleared_input_tokens: z.number().int()}); export const appliedTruncateToolResultEdit = z.object({type: z.literal("truncate_tool_result").optional(), tool_call_id: z.string(), cleared_input_tokens: z.number().int()}); export const contextManagementResult = z.object({applied_edits: z.array(z.discriminatedUnion("type", [appliedPruneContextEdit, appliedTruncateToolResultEdit]))}); export const toolTicket = z.object({tool_call_id: z.string(), ticket: z.string(), expires_at: z.number().int()}); export const textPart = z.object({type: z.literal("text").optional(), text: z.string()}); export const systemMessage = z.object({role: z.literal("system"), content: z.union([z.string(), z.array(textPart)])}); export const developerMessage = z.object({role: z.literal("developer"), content: z.union([z.string(), z.array(textPart)])}); export const userMessage = z.object({role: z.literal("user"), content: z.union([z.string(), z.array(textPart)])}); export const assistantMessageOutput = z.object({role: z.literal("assistant"), content: z.union([z.string(), z.array(textPart)]).nullable().optional(), tool_calls: z.array(messageToolCall).optional(), reasoning_content: z.string().nullable().optional(), annotations: z.array(z.discriminatedUnion("type", [fileCitation, urlCitation])).optional()}); export const toolMessage = z.object({role: z.literal("tool"), content: z.union([z.string(), z.array(textPart)]), tool_call_id: z.string()}); export const chatCompletion = z.object({id: z.string(), object: z.literal("chat.completion").optional(), created: z.number().int(), model: z.string(), choices: z.array(chatCompletionChoice), usage: completionUsage.nullable().optional(), metadata: z.record(z.string(), z.string()).nullable().optional(), title: z.string().nullable().optional(), hosted_tool_calls: z.array(z.discriminatedUnion("type", [searchCorpusCallItem, grepCallItem, filterChunksCallItem, inspectMetadataCallItem, getChunksCallItem, storeSearchCallItem, storeGrepCallItem, storeListChunksCallItem, metadataFacetsCallItem, listStoresCallItem])).optional(), context_management: contextManagementResult.nullable().optional(), tool_tickets: z.array(toolTicket).optional(), transcript: z.array(z.discriminatedUnion("role", [systemMessage, developerMessage, userMessage, assistantMessageOutput, toolMessage])).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 a chat completion * * POST /v1/chat/completions */ export const createApiV1ChatCompletions = async (args: CreateApiV1ChatCompletionsArgs, options?: CreateApiV1ChatCompletionsOptions) => { const {baseUrl = "https://api.mixedbread.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/chat/completions${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(chatCompletion, await res.json()) };