refactor(ai): use native Mistral request options

This commit is contained in:
Peter Steinberger
2026-07-14 14:22:29 +01:00
parent 883e357759
commit 3e67d0d08d
+10 -34
View File
@@ -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<string, string>;
} = {
retries: { strategy: "none" },
};
if (options?.signal) {
requestOptions.signal = options.signal;
}
const headers: Record<string, string> = {};
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,