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:
parent
a6b4f99a83
commit
1c39d2ba27
24
src/index.ts
24
src/index.ts
|
|
@ -417,13 +417,25 @@ function getFlag(args: string[], flag: string): string | undefined {
|
||||||
}
|
}
|
||||||
|
|
||||||
function createVisionModel(config: VisionConfig) {
|
function createVisionModel(config: VisionConfig) {
|
||||||
const headers: Record<string, string> = {
|
const sessionId = config.sessionId || '';
|
||||||
'x-langfuse-tags': 'skill:video-product-snapshot',
|
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) {
|
const openai = createOpenAI({
|
||||||
headers['x-langfuse-session-id'] = config.sessionId;
|
apiKey: config.apiKey, baseURL: config.baseURL,
|
||||||
}
|
fetch: wrapped as typeof globalThis.fetch,
|
||||||
const openai = createOpenAI({ apiKey: config.apiKey, baseURL: config.baseURL, headers });
|
});
|
||||||
return openai(config.model);
|
return openai(config.model);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,13 +34,24 @@ const FILTER_PROMPT = (count: number, description?: string) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
function createModel(config: VisionConfig) {
|
function createModel(config: VisionConfig) {
|
||||||
const headers: Record<string, string> = {
|
const sessionId = config.sessionId || '';
|
||||||
'x-langfuse-tags': 'skill:video-product-snapshot',
|
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) {
|
const provider = createOpenAI({
|
||||||
headers['x-langfuse-session-id'] = config.sessionId;
|
apiKey: config.apiKey, baseURL: config.baseURL,
|
||||||
}
|
fetch: wrapped as typeof globalThis.fetch,
|
||||||
const provider = createOpenAI({ apiKey: config.apiKey, baseURL: config.baseURL, headers });
|
});
|
||||||
return provider(config.model);
|
return provider(config.model);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -78,13 +78,24 @@ Return:
|
||||||
- boundingBox: tight box of the PRODUCT ONLY as [x1, y1, x2, y2] normalized 0.0–1.0, top-left origin. Exclude hands, background, and unrelated objects. The product is near the center of the frame.`;
|
- boundingBox: tight box of the PRODUCT ONLY as [x1, y1, x2, y2] normalized 0.0–1.0, top-left origin. Exclude hands, background, and unrelated objects. The product is near the center of the frame.`;
|
||||||
|
|
||||||
function createVisionModel(config: VisionConfig) {
|
function createVisionModel(config: VisionConfig) {
|
||||||
const headers: Record<string, string> = {
|
const sessionId = config.sessionId || '';
|
||||||
'x-langfuse-tags': 'skill:video-product-snapshot',
|
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) {
|
const provider = createOpenAI({
|
||||||
headers['x-langfuse-session-id'] = config.sessionId;
|
apiKey: config.apiKey, baseURL: config.baseURL,
|
||||||
}
|
fetch: wrapped as typeof globalThis.fetch,
|
||||||
const provider = createOpenAI({ apiKey: config.apiKey, baseURL: config.baseURL, headers });
|
});
|
||||||
return provider(config.model);
|
return provider(config.model);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue