diff --git a/packages/ai/src/providers/mistral.ts b/packages/ai/src/providers/mistral.ts index f5e6c01548b1..3a26982cd258 100644 --- a/packages/ai/src/providers/mistral.ts +++ b/packages/ai/src/providers/mistral.ts @@ -162,7 +162,16 @@ export const streamMistral: StreamFunction<"mistral-conversations", MistralOptio if (nextPayload !== undefined) { payload = nextPayload as ChatCompletionStreamRequest; } - const mistralStream = await mistral.chat.stream(payload, buildRequestOptions(model, options)); + const headers = { ...model.headers, ...options?.headers }; + // Mistral infrastructure uses `x-affinity` for KV-cache reuse (prefix caching). + // Respect explicit caller-provided header values. + if (options?.sessionId) { + headers["x-affinity"] ||= options.sessionId; + } + const mistralStream = await mistral.chat.stream(payload, { + headers, + signal: options?.signal, + }); stream.push({ type: "start", partial: output }); await consumeChatStream(model, output, stream, mistralStream); @@ -311,39 +320,6 @@ function safeJsonStringify(value: unknown): string { } } -function buildRequestOptions(model: Model<"mistral-conversations">, options?: MistralOptions) { - const requestOptions: { - signal?: AbortSignal; - retries: { strategy: "none" }; - headers?: Record; - } = { - retries: { strategy: "none" }, - }; - if (options?.signal) { - requestOptions.signal = options.signal; - } - - const headers: Record = {}; - if (model.headers) { - Object.assign(headers, model.headers); - } - if (options?.headers) { - Object.assign(headers, options.headers); - } - - // Mistral infrastructure uses `x-affinity` for KV-cache reuse (prefix caching). - // Respect explicit caller-provided header values. - if (options?.sessionId && !headers["x-affinity"]) { - headers["x-affinity"] = options.sessionId; - } - - if (Object.keys(headers).length > 0) { - requestOptions.headers = headers; - } - - return requestOptions; -} - function buildChatPayload( model: Model<"mistral-conversations">, context: Context,