fix: align OpenAI cache affinity and use uuidv7 session ids

This commit is contained in:
Mario Zechner
2026-04-14 23:20:13 +02:00
parent d62d22173a
commit 018b40c30c
11 changed files with 245 additions and 13 deletions

View File

@@ -950,6 +950,7 @@ function buildSSEHeaders(
if (sessionId) {
headers.set("session_id", sessionId);
headers.set("x-client-request-id", sessionId);
}
return headers;

View File

@@ -88,7 +88,9 @@ export const streamOpenAIResponses: StreamFunction<"openai-responses", OpenAIRes
try {
// Create OpenAI client
const apiKey = options?.apiKey || getEnvApiKey(model.provider) || "";
const client = createClient(model, context, apiKey, options?.headers);
const cacheRetention = resolveCacheRetention(options?.cacheRetention);
const cacheSessionId = cacheRetention === "none" ? undefined : options?.sessionId;
const client = createClient(model, context, apiKey, options?.headers, cacheSessionId);
let params = buildParams(model, context, options);
const nextParams = await options?.onPayload?.(params, model);
if (nextParams !== undefined) {
@@ -155,6 +157,7 @@ function createClient(
context: Context,
apiKey?: string,
optionsHeaders?: Record<string, string>,
sessionId?: string,
) {
if (!apiKey) {
if (!process.env.OPENAI_API_KEY) {
@@ -175,6 +178,11 @@ function createClient(
Object.assign(headers, copilotHeaders);
}
if (sessionId && model.provider === "openai" && model.baseUrl.includes("api.openai.com")) {
headers.session_id = sessionId;
headers["x-client-request-id"] = sessionId;
}
// Merge options headers last so they can override defaults
if (optionsHeaders) {
Object.assign(headers, optionsHeaders);