fix: 用 fetch wrapper 注入 metadata.session_id 替代 HTTP header

LiteLLM 不处理 x-langfuse-session-id header。改用 fetch 拦截器在请求体
metadata 里注入 session_id,LiteLLM 直接透传给 Langfuse 创建 session。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
ywkj 2026-04-26 19:45:08 +08:00
parent a6b4f99a83
commit 1c39d2ba27
3 changed files with 52 additions and 18 deletions

View File

@ -417,13 +417,25 @@ function getFlag(args: string[], flag: string): string | undefined {
}
function createVisionModel(config: VisionConfig) {
const headers: Record<string, string> = {
'x-langfuse-tags': 'skill:video-product-snapshot',
const sessionId = config.sessionId || '';
const originFetch = globalThis.fetch;
// Inject metadata.session_id into request body so LiteLLM → Langfuse creates sessions
const wrapped = async (input: RequestInfo | URL, init?: RequestInit) => {
if (init?.body && typeof init.body === 'string') {
try {
const body = JSON.parse(init.body);
if (!body.metadata) body.metadata = {};
if (!body.metadata.session_id) body.metadata.session_id = sessionId;
body.metadata.tags = ['skill:video-product-snapshot'];
init = { ...init, body: JSON.stringify(body) };
} catch {}
}
return originFetch(input, init);
};
if (config.sessionId) {
headers['x-langfuse-session-id'] = config.sessionId;
}
const openai = createOpenAI({ apiKey: config.apiKey, baseURL: config.baseURL, headers });
const openai = createOpenAI({
apiKey: config.apiKey, baseURL: config.baseURL,
fetch: wrapped as typeof globalThis.fetch,
});
return openai(config.model);
}

View File

@ -34,13 +34,24 @@ const FILTER_PROMPT = (count: number, description?: string) => {
};
function createModel(config: VisionConfig) {
const headers: Record<string, string> = {
'x-langfuse-tags': 'skill:video-product-snapshot',
const sessionId = config.sessionId || '';
const originFetch = globalThis.fetch;
const wrapped = async (input: RequestInfo | URL, init?: RequestInit) => {
if (init?.body && typeof init.body === 'string') {
try {
const body = JSON.parse(init.body);
if (!body.metadata) body.metadata = {};
if (!body.metadata.session_id) body.metadata.session_id = sessionId;
body.metadata.tags = ['skill:video-product-snapshot'];
init = { ...init, body: JSON.stringify(body) };
} catch {}
}
return originFetch(input, init);
};
if (config.sessionId) {
headers['x-langfuse-session-id'] = config.sessionId;
}
const provider = createOpenAI({ apiKey: config.apiKey, baseURL: config.baseURL, headers });
const provider = createOpenAI({
apiKey: config.apiKey, baseURL: config.baseURL,
fetch: wrapped as typeof globalThis.fetch,
});
return provider(config.model);
}

View File

@ -78,13 +78,24 @@ Return:
- boundingBox: tight box of the PRODUCT ONLY as [x1, y1, x2, y2] normalized 0.01.0, top-left origin. Exclude hands, background, and unrelated objects. The product is near the center of the frame.`;
function createVisionModel(config: VisionConfig) {
const headers: Record<string, string> = {
'x-langfuse-tags': 'skill:video-product-snapshot',
const sessionId = config.sessionId || '';
const originFetch = globalThis.fetch;
const wrapped = async (input: RequestInfo | URL, init?: RequestInit) => {
if (init?.body && typeof init.body === 'string') {
try {
const body = JSON.parse(init.body);
if (!body.metadata) body.metadata = {};
if (!body.metadata.session_id) body.metadata.session_id = sessionId;
body.metadata.tags = ['skill:video-product-snapshot'];
init = { ...init, body: JSON.stringify(body) };
} catch {}
}
return originFetch(input, init);
};
if (config.sessionId) {
headers['x-langfuse-session-id'] = config.sessionId;
}
const provider = createOpenAI({ apiKey: config.apiKey, baseURL: config.baseURL, headers });
const provider = createOpenAI({
apiKey: config.apiKey, baseURL: config.baseURL,
fetch: wrapped as typeof globalThis.fetch,
});
return provider(config.model);
}