feat: 全链路日志系统 — Web UI + 详细请求/响应追踪

- 新增 logger.ts: 全链路日志记录器,支持完整请求/响应 payload 存储
- 新增 log-viewer.ts: Web UI 日志查看器 (/logs),SSE 实时推送
- UI 支持四个标签页: 日志、请求参数、提示词、响应内容
- 存储完整消息内容 (100K/条上限),支持展开/折叠查看
- 阶段耗时可视化 (receive → convert → send → response → stream)
- 修复 Cursor 消息格式 parts vs content 导致的 500 错误
- handler/openai-handler 集成全链路日志记录
- 控制台仅打印核心简短日志
This commit is contained in:
小海
2026-03-16 15:22:31 +08:00
parent 0e16861b2c
commit 84c26aa332
6 changed files with 1159 additions and 73 deletions

View File

@@ -56,7 +56,7 @@ export async function sendCursorRequest(
return;
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
console.error(`[Cursor] 请求失败 (${attempt}/${maxRetries}): ${msg}`);
console.error(`[Cursor] 请求失败 (${attempt}/${maxRetries}): ${msg.substring(0, 100)}`);
if (attempt < maxRetries) {
console.log(`[Cursor] 2s 后重试...`);
await new Promise(r => setTimeout(r, 2000));
@@ -73,7 +73,7 @@ async function sendCursorRequestInner(
): Promise<void> {
const headers = getChromeHeaders();
console.log(`[Cursor] 发送请求: model=${req.model}, messages=${req.messages.length}`);
// 详细日志记录在 handler 层
const config = getConfig();
const controller = new AbortController();

View File

@@ -18,6 +18,7 @@ import type {
import { convertToCursorRequest, parseToolCalls, hasToolCalls } from './converter.js';
import { sendCursorRequest, sendCursorRequestFull } from './cursor-client.js';
import { getConfig } from './config.js';
import { createRequestLogger, type RequestLogger } from './logger.js';
function msgId(): string {
return 'msg_' + uuidv4().replace(/-/g, '').substring(0, 24);
@@ -405,12 +406,34 @@ async function handleMockIdentityNonStream(res: Response, body: AnthropicRequest
export async function handleMessages(req: Request, res: Response): Promise<void> {
const body = req.body as AnthropicRequest;
console.log(`[Handler] 收到请求: model=${body.model}, messages=${body.messages?.length}, stream=${body.stream}, tools=${body.tools?.length ?? 0}`);
const systemStr = typeof body.system === 'string' ? body.system : Array.isArray(body.system) ? body.system.map((b: any) => b.text || '').join('') : '';
const log = createRequestLogger({
method: req.method,
path: req.path,
model: body.model,
stream: !!body.stream,
hasTools: (body.tools?.length ?? 0) > 0,
toolCount: body.tools?.length ?? 0,
messageCount: body.messages?.length ?? 0,
apiFormat: 'anthropic',
systemPromptLength: systemStr.length,
});
log.startPhase('receive', '接收请求');
log.recordOriginalRequest(body);
log.info('Handler', 'receive', `收到 Anthropic Messages 请求`, {
model: body.model,
messageCount: body.messages?.length,
stream: body.stream,
toolCount: body.tools?.length ?? 0,
maxTokens: body.max_tokens,
hasSystem: !!body.system,
thinking: body.thinking?.type,
});
try {
// 注意:图片预处理已移入 convertToCursorRequest → preprocessImages() 统一处理
if (isIdentityProbe(body)) {
console.log(`[Handler] 拦截到身份探针,返回模拟响应以规避风控`);
log.intercepted('身份探针拦截 → 返回模拟响应');
if (body.stream) {
return await handleMockIdentityStream(res, body);
} else {
@@ -419,16 +442,21 @@ export async function handleMessages(req: Request, res: Response): Promise<void>
}
// 转换为 Cursor 请求
log.startPhase('convert', '格式转换');
log.info('Handler', 'convert', '开始转换为 Cursor 请求格式');
const cursorReq = await convertToCursorRequest(body);
log.endPhase();
log.recordCursorRequest(cursorReq);
log.debug('Handler', 'convert', `转换完成: ${cursorReq.messages.length} messages, model=${cursorReq.model}`);
if (body.stream) {
await handleStream(res, cursorReq, body);
await handleStream(res, cursorReq, body, log);
} else {
await handleNonStream(res, cursorReq, body);
await handleNonStream(res, cursorReq, body, log);
}
} catch (err: unknown) {
const message = err instanceof Error ? err.message : String(err);
console.error(`[Handler] 请求处理失败:`, message);
log.fail(message);
res.status(500).json({
type: 'error',
error: { type: 'api_error', message },
@@ -533,7 +561,7 @@ function deduplicateContinuation(existing: string, continuation: string): string
if (matchedLines >= 2) {
// 移除续写中匹配的行
const deduped = continuationLines.slice(matchedLines).join('\n');
console.log(`[Handler] 行级去重: 移除了续写前 ${matchedLines} 行的重复内容`);
// 行级去重记录到详细日志
return deduped;
}
break;
@@ -588,7 +616,7 @@ export function buildRetryRequest(body: AnthropicRequest, attempt: number): Anth
// ==================== 流式处理 ====================
async function handleStream(res: Response, cursorReq: CursorChatRequest, body: AnthropicRequest): Promise<void> {
async function handleStream(res: Response, cursorReq: CursorChatRequest, body: AnthropicRequest, log: RequestLogger): Promise<void> {
// 设置 SSE headers
res.writeHead(200, {
'Content-Type': 'text/event-stream',
@@ -631,19 +659,26 @@ async function handleStream(res: Response, cursorReq: CursorChatRequest, body: A
const executeStream = async () => {
fullResponse = '';
const apiStart = Date.now();
let firstChunk = true;
log.startPhase('send', '发送到 Cursor');
await sendCursorRequest(activeCursorReq, (event: CursorSSEEvent) => {
if (event.type !== 'text-delta' || !event.delta) return;
if (firstChunk) { log.recordTTFT(); log.endPhase(); log.startPhase('response', '接收响应'); firstChunk = false; }
fullResponse += event.delta;
// 有工具时始终缓冲,无工具时也缓冲(用于拒绝检测)
// 不再直接流式发送,统一在流结束后处理
});
log.endPhase();
log.recordCursorApiTime(apiStart);
};
try {
await executeStream();
console.log(`[Handler] 原始响应 (${fullResponse.length} chars, tools=${hasTools}): ${fullResponse.substring(0, 200)}${fullResponse.length > 200 ? '...' : ''}`);
log.recordRawResponse(fullResponse);
log.info('Handler', 'response', `原始响应: ${fullResponse.length} chars`, {
preview: fullResponse.substring(0, 300),
hasTools,
});
// ★ Thinking 提取(在拒绝检测之前,防止 thinking 内容触发 isRefusal 误判)
const thinkingEnabled = body.thinking?.type === 'enabled';
@@ -653,7 +688,9 @@ async function handleStream(res: Response, cursorReq: CursorChatRequest, body: A
if (thinkingMatch) {
thinkingContent = thinkingMatch.map(m => m.replace(/<\/?thinking>/g, '').trim()).join('\n\n');
fullResponse = fullResponse.replace(/<thinking>[\s\S]*?<\/thinking>\s*/g, '').trim();
console.log(`[Handler] 剥离 thinking: ${thinkingContent.length} chars, 剩余 ${fullResponse.length} chars`);
log.info('Handler', 'thinking', `剥离 thinking: ${thinkingContent.length} chars, 剩余 ${fullResponse.length} chars`);
log.recordThinking(thinkingContent);
log.updateSummary({ thinkingChars: thinkingContent.length });
}
}
@@ -666,27 +703,28 @@ async function handleStream(res: Response, cursorReq: CursorChatRequest, body: A
while (shouldRetryRefusal() && retryCount < MAX_REFUSAL_RETRIES) {
retryCount++;
console.log(`[Handler] 检测到拒绝(第${retryCount}次),自动重试...原始: ${fullResponse.substring(0, 100)}`);
log.warn('Handler', 'retry', `检测到拒绝(第${retryCount}次),自动重试`, { preview: fullResponse.substring(0, 200) });
log.updateSummary({ retryCount });
const retryBody = buildRetryRequest(body, retryCount - 1);
activeCursorReq = await convertToCursorRequest(retryBody);
await executeStream();
console.log(`[Handler] 重试响应 (${fullResponse.length} chars): ${fullResponse.substring(0, 200)}${fullResponse.length > 200 ? '...' : ''}`);
log.info('Handler', 'retry', `重试响应: ${fullResponse.length} chars`, { preview: fullResponse.substring(0, 200) });
}
if (shouldRetryRefusal()) {
if (!hasTools) {
// 工具能力询问 → 返回详细能力描述;其他 → 返回身份回复
if (isToolCapabilityQuestion(body)) {
console.log(`[Handler] 工具能力询问被拒绝返回 Claude 能力描述`);
log.info('Handler', 'refusal', '工具能力询问被拒绝返回 Claude 能力描述');
fullResponse = CLAUDE_TOOLS_RESPONSE;
} else {
console.log(`[Handler] 重试${MAX_REFUSAL_RETRIES}次后仍被拒绝,返回 Claude 身份回复`);
log.warn('Handler', 'refusal', `重试${MAX_REFUSAL_RETRIES}次后仍被拒绝 → 降级为 Claude 身份回复`);
fullResponse = CLAUDE_IDENTITY_RESPONSE;
}
} else {
// 工具模式拒绝:不返回纯文本(会让 Claude Code 误认为任务完成)
// 返回一个合理的纯文本,让它以 end_turn 结束Claude Code 会根据上下文继续
console.log(`[Handler] 工具模式下拒绝且无工具调用返回简短引导文本`);
log.warn('Handler', 'refusal', '工具模式下拒绝且无工具调用返回简短引导文本');
fullResponse = 'Let me proceed with the task.';
}
}
@@ -694,10 +732,10 @@ async function handleStream(res: Response, cursorReq: CursorChatRequest, body: A
// 极短响应重试(可能是连接中断)
if (hasTools && fullResponse.trim().length < 10 && retryCount < MAX_REFUSAL_RETRIES) {
retryCount++;
console.log(`[Handler] 响应过短 (${fullResponse.length} chars),重试第${retryCount}`);
log.warn('Handler', 'retry', `响应过短 (${fullResponse.length} chars),重试第${retryCount}`);
activeCursorReq = await convertToCursorRequest(body);
await executeStream();
console.log(`[Handler] 重试响应 (${fullResponse.length} chars): ${fullResponse.substring(0, 200)}${fullResponse.length > 200 ? '...' : ''}`);
log.info('Handler', 'retry', `重试响应: ${fullResponse.length} chars`, { preview: fullResponse.substring(0, 200) });
}
// 流完成后,处理完整响应
@@ -713,7 +751,8 @@ async function handleStream(res: Response, cursorReq: CursorChatRequest, body: A
while (hasTools && isTruncated(fullResponse) && continueCount < MAX_AUTO_CONTINUE) {
continueCount++;
const prevLength = fullResponse.length;
console.log(`[Handler] ⚠️ 内部检测到截断 (${fullResponse.length} chars)Proxy 将隐式请求无缝续写 (第${continueCount}次)...`);
log.warn('Handler', 'continuation', `内部检测到截断 (${fullResponse.length} chars)隐式续写 (第${continueCount}次)`);
log.updateSummary({ continuationCount: continueCount });
// 提取截断点的最后一段文本作为上下文锚点
const anchorLength = Math.min(300, fullResponse.length);
@@ -754,7 +793,7 @@ Continue EXACTLY from where you stopped. DO NOT repeat any content already gener
});
if (continuationResponse.trim().length === 0) {
console.log(`[Handler] ⚠️ 续写返回空响应,停止续写`);
log.warn('Handler', 'continuation', '续写返回空响应,停止续写');
break;
}
@@ -763,19 +802,19 @@ Continue EXACTLY from where you stopped. DO NOT repeat any content already gener
const deduped = deduplicateContinuation(fullResponse, continuationResponse);
fullResponse += deduped;
if (deduped.length !== continuationResponse.length) {
console.log(`[Handler] 续写去重: 移除了 ${continuationResponse.length - deduped.length} chars 的重复内容`);
log.debug('Handler', 'continuation', `续写去重: 移除了 ${continuationResponse.length - deduped.length} chars 的重复内容`);
}
console.log(`[Handler] 续写拼接完成: ${prevLength}${fullResponse.length} chars (+${deduped.length})`);
log.info('Handler', 'continuation', `续写拼接完成: ${prevLength}${fullResponse.length} chars (+${deduped.length})`);
// ★ 无进展检测:去重后没有新内容,说明模型在重复自己,继续续写无意义
if (deduped.trim().length === 0) {
console.log(`[Handler] ⚠️ 续写内容全部为重复,停止续写`);
log.warn('Handler', 'continuation', '续写内容全部为重复,停止续写');
break;
}
// ★ 最小进展检测:去重后新增内容过少(<100 chars模型几乎已完成
if (deduped.trim().length < 100) {
console.log(`[Handler] ⚠️ 续写新增内容过少 (${deduped.trim().length} chars < 100),停止续写`);
log.info('Handler', 'continuation', `续写新增内容过少 (${deduped.trim().length} chars < 100),停止续写`);
break;
}
@@ -783,7 +822,7 @@ Continue EXACTLY from where you stopped. DO NOT repeat any content already gener
if (deduped.trim().length < 500) {
consecutiveSmallAdds++;
if (consecutiveSmallAdds >= 2) {
console.log(`[Handler] ⚠️ 连续 ${consecutiveSmallAdds} 次小增量续写,停止续写`);
log.info('Handler', 'continuation', `连续 ${consecutiveSmallAdds} 次小增量续写,停止续写`);
break;
}
} else {
@@ -793,10 +832,11 @@ Continue EXACTLY from where you stopped. DO NOT repeat any content already gener
let stopReason = (hasTools && isTruncated(fullResponse)) ? 'max_tokens' : 'end_turn';
if (stopReason === 'max_tokens') {
console.log(`[Handler] ⚠️ ${MAX_AUTO_CONTINUE}隐式续写后仍受限于截断 (${fullResponse.length} chars),设置 stop_reason=max_tokens`);
log.warn('Handler', 'truncation', `${MAX_AUTO_CONTINUE}次续写后仍截断 (${fullResponse.length} chars) stop_reason=max_tokens`);
}
// ★ Thinking 块发送:在实际内容之前发送 thinking content block
log.startPhase('stream', 'SSE 输出');
if (thinkingEnabled && thinkingContent) {
writeSSE(res, 'content_block_start', {
type: 'content_block_start', index: blockIndex,
@@ -825,7 +865,7 @@ Continue EXACTLY from where you stopped. DO NOT repeat any content already gener
toolChoiceRetry < TOOL_CHOICE_MAX_RETRIES
) {
toolChoiceRetry++;
console.log(`[Handler] tool_choice=any 但模型未调用工具(第${toolChoiceRetry}次),强制重试...`);
log.warn('Handler', 'retry', `tool_choice=any 但模型未调用工具(第${toolChoiceRetry}次),强制重试`);
// 在现有 Cursor 请求中追加强制 user 消息(不重新转换整个请求,代价最小)
const forceMsg: CursorMessage = {
@@ -848,7 +888,7 @@ Continue EXACTLY from where you stopped. DO NOT repeat any content already gener
({ toolCalls, cleanText } = parseToolCalls(fullResponse));
}
if (toolChoice?.type === 'any' && toolCalls.length === 0) {
console.log(`[Handler] tool_choice=any 重试${TOOL_CHOICE_MAX_RETRIES}次后仍无工具调用`);
log.warn('Handler', 'toolparse', `tool_choice=any 重试${TOOL_CHOICE_MAX_RETRIES}次后仍无工具调用`);
}
@@ -857,7 +897,7 @@ Continue EXACTLY from where you stopped. DO NOT repeat any content already gener
// Check if the residual text is a known refusal, if so, drop it completely!
if (REFUSAL_PATTERNS.some(p => p.test(cleanText))) {
console.log(`[Handler] Supressed refusal text generated during tool usage: ${cleanText.substring(0, 100)}...`);
log.info('Handler', 'sanitize', `抑制工具调用中的拒绝文本`, { preview: cleanText.substring(0, 200) });
cleanText = '';
}
@@ -923,7 +963,7 @@ Continue EXACTLY from where you stopped. DO NOT repeat any content already gener
const isActualRefusal = stopReason !== 'max_tokens' && (isShortResponse ? isRefusal(fullResponse) : startsWithRefusal);
if (isActualRefusal) {
console.log(`[Handler] Supressed complete refusal without tools: ${fullResponse.substring(0, 100)}...`);
log.info('Handler', 'sanitize', `抑制无工具的完整拒绝响应`, { preview: fullResponse.substring(0, 200) });
textToSend = 'I understand the request. Let me proceed with the appropriate action. Could you clarify what specific task you would like me to perform?';
}
@@ -978,8 +1018,13 @@ Continue EXACTLY from where you stopped. DO NOT repeat any content already gener
writeSSE(res, 'message_stop', { type: 'message_stop' });
// ★ 记录完成
log.recordFinalResponse(fullResponse);
log.complete(fullResponse.length, stopReason);
} catch (err: unknown) {
const message = err instanceof Error ? err.message : String(err);
log.fail(message);
writeSSE(res, 'error', {
type: 'error', error: { type: 'api_error', message },
});
@@ -993,7 +1038,7 @@ Continue EXACTLY from where you stopped. DO NOT repeat any content already gener
// ==================== 非流式处理 ====================
async function handleNonStream(res: Response, cursorReq: CursorChatRequest, body: AnthropicRequest): Promise<void> {
async function handleNonStream(res: Response, cursorReq: CursorChatRequest, body: AnthropicRequest, log: RequestLogger): Promise<void> {
// ★ 非流式保活:手动设置 chunked 响应,在缓冲期间每 15s 发送空白字符保活
// JSON.parse 会忽略前导空白,所以客户端解析不受影响
res.writeHead(200, { 'Content-Type': 'application/json' });
@@ -1006,12 +1051,21 @@ async function handleNonStream(res: Response, cursorReq: CursorChatRequest, body
}, 15000);
try {
log.startPhase('send', '发送到 Cursor (非流式)');
const apiStart = Date.now();
let fullText = await sendCursorRequestFull(cursorReq);
log.recordTTFT();
log.recordCursorApiTime(apiStart);
log.recordRawResponse(fullText);
log.startPhase('response', '处理响应');
const hasTools = (body.tools?.length ?? 0) > 0;
let activeCursorReq = cursorReq;
let retryCount = 0;
console.log(`[Handler] 非流式原始响应 (${fullText.length} chars, tools=${hasTools}): ${fullText.substring(0, 300)}${fullText.length > 300 ? '...' : ''}`);
log.info('Handler', 'response', `非流式原始响应: ${fullText.length} chars`, {
preview: fullText.substring(0, 300),
hasTools,
});
// ★ Thinking 提取(在拒绝检测之前)
const thinkingEnabled = body.thinking?.type === 'enabled';
@@ -1021,7 +1075,7 @@ async function handleNonStream(res: Response, cursorReq: CursorChatRequest, body
if (thinkingMatch) {
thinkingContent = thinkingMatch.map(m => m.replace(/<\/?thinking>/g, '').trim()).join('\n\n');
fullText = fullText.replace(/<thinking>[\s\S]*?<\/thinking>\s*/g, '').trim();
console.log(`[Handler] 非流式剥离 thinking: ${thinkingContent.length} chars, 剩余 ${fullText.length} chars`);
log.info('Handler', 'thinking', `非流式剥离 thinking: ${thinkingContent.length} chars, 剩余 ${fullText.length} chars`);
}
}
@@ -1031,7 +1085,8 @@ async function handleNonStream(res: Response, cursorReq: CursorChatRequest, body
if (shouldRetry()) {
for (let attempt = 0; attempt < MAX_REFUSAL_RETRIES; attempt++) {
retryCount++;
console.log(`[Handler] 非流式检测到拒绝(第${retryCount}次重试)...原始: ${fullText.substring(0, 100)}`);
log.warn('Handler', 'retry', `非流式检测到拒绝(第${retryCount}次重试)`, { preview: fullText.substring(0, 200) });
log.updateSummary({ retryCount });
const retryBody = buildRetryRequest(body, attempt);
activeCursorReq = await convertToCursorRequest(retryBody);
fullText = await sendCursorRequestFull(activeCursorReq);
@@ -1039,13 +1094,13 @@ async function handleNonStream(res: Response, cursorReq: CursorChatRequest, body
}
if (shouldRetry()) {
if (hasTools) {
console.log(`[Handler] 非流式工具模式下拒绝引导模型输出`);
log.warn('Handler', 'refusal', '非流式工具模式下拒绝引导模型输出');
fullText = 'I understand the request. Let me analyze the information and proceed with the appropriate action.';
} else if (isToolCapabilityQuestion(body)) {
console.log(`[Handler] 非流式工具能力询问被拒绝返回 Claude 能力描述`);
log.info('Handler', 'refusal', '非流式工具能力询问被拒绝返回 Claude 能力描述');
fullText = CLAUDE_TOOLS_RESPONSE;
} else {
console.log(`[Handler] 非流式重试${MAX_REFUSAL_RETRIES}次后仍被拒绝,返回 Claude 身份回复`);
log.warn('Handler', 'refusal', `非流式重试${MAX_REFUSAL_RETRIES}次后仍被拒绝 → 降级为 Claude 身份回复`);
fullText = CLAUDE_IDENTITY_RESPONSE;
}
}
@@ -1054,10 +1109,10 @@ async function handleNonStream(res: Response, cursorReq: CursorChatRequest, body
// ★ 极短响应重试(可能是连接中断)
if (hasTools && fullText.trim().length < 10 && retryCount < MAX_REFUSAL_RETRIES) {
retryCount++;
console.log(`[Handler] 非流式响应过短 (${fullText.length} chars),重试第${retryCount}`);
log.warn('Handler', 'retry', `非流式响应过短 (${fullText.length} chars),重试第${retryCount}`);
activeCursorReq = await convertToCursorRequest(body);
fullText = await sendCursorRequestFull(activeCursorReq);
console.log(`[Handler] 非流式重试响应 (${fullText.length} chars): ${fullText.substring(0, 200)}${fullText.length > 200 ? '...' : ''}`);
log.info('Handler', 'retry', `非流式重试响应: ${fullText.length} chars`, { preview: fullText.substring(0, 200) });
}
// ★ 内部截断续写(与流式路径对齐)
@@ -1071,7 +1126,8 @@ async function handleNonStream(res: Response, cursorReq: CursorChatRequest, body
while (hasTools && isTruncated(fullText) && continueCount < MAX_AUTO_CONTINUE) {
continueCount++;
const prevLength = fullText.length;
console.log(`[Handler] ⚠️ 非流式:内部检测到截断 (${fullText.length} chars)Proxy 将隐式请求无缝续写 (第${continueCount}次)...`);
log.warn('Handler', 'continuation', `非流式检测到截断 (${fullText.length} chars)隐式续写 (第${continueCount}次)`);
log.updateSummary({ continuationCount: continueCount });
const anchorLength = Math.min(300, fullText.length);
const anchorText = fullText.slice(-anchorLength);
@@ -1104,7 +1160,7 @@ Continue EXACTLY from where you stopped. DO NOT repeat any content already gener
const continuationResponse = await sendCursorRequestFull(continuationReq);
if (continuationResponse.trim().length === 0) {
console.log(`[Handler] ⚠️ 非流式续写返回空响应,停止续写`);
log.warn('Handler', 'continuation', '非流式续写返回空响应,停止续写');
break;
}
@@ -1112,19 +1168,19 @@ Continue EXACTLY from where you stopped. DO NOT repeat any content already gener
const deduped = deduplicateContinuation(fullText, continuationResponse);
fullText += deduped;
if (deduped.length !== continuationResponse.length) {
console.log(`[Handler] 非流式续写去重: 移除了 ${continuationResponse.length - deduped.length} chars 的重复内容`);
log.debug('Handler', 'continuation', `非流式续写去重: 移除了 ${continuationResponse.length - deduped.length} chars 的重复内容`);
}
console.log(`[Handler] 非流式续写拼接完成: ${prevLength}${fullText.length} chars (+${deduped.length})`);
log.info('Handler', 'continuation', `非流式续写拼接完成: ${prevLength}${fullText.length} chars (+${deduped.length})`);
// ★ 无进展检测:去重后没有新内容,停止续写
if (deduped.trim().length === 0) {
console.log(`[Handler] ⚠️ 非流式续写内容全部为重复,停止续写`);
log.warn('Handler', 'continuation', '非流式续写内容全部为重复,停止续写');
break;
}
// ★ 最小进展检测:去重后新增内容过少(<100 chars模型几乎已完成
if (deduped.trim().length < 100) {
console.log(`[Handler] ⚠️ 非流式续写新增内容过少 (${deduped.trim().length} chars < 100),停止续写`);
log.info('Handler', 'continuation', `非流式续写新增内容过少 (${deduped.trim().length} chars < 100),停止续写`);
break;
}
@@ -1132,7 +1188,7 @@ Continue EXACTLY from where you stopped. DO NOT repeat any content already gener
if (deduped.trim().length < 500) {
consecutiveSmallAdds++;
if (consecutiveSmallAdds >= 2) {
console.log(`[Handler] ⚠️ 非流式连续 ${consecutiveSmallAdds} 次小增量续写,停止续写`);
log.info('Handler', 'continuation', `非流式连续 ${consecutiveSmallAdds} 次小增量续写,停止续写`);
break;
}
} else {
@@ -1150,7 +1206,7 @@ Continue EXACTLY from where you stopped. DO NOT repeat any content already gener
// ★ 截断检测:代码块/XML 未闭合时,返回 max_tokens 让 Claude Code 自动继续
let stopReason = (hasTools && isTruncated(fullText)) ? 'max_tokens' : 'end_turn';
if (stopReason === 'max_tokens') {
console.log(`[Handler] ⚠️ 非流式检测到截断响应 (${fullText.length} chars),设置 stop_reason=max_tokens`);
log.warn('Handler', 'truncation', `非流式检测到截断响应 (${fullText.length} chars) stop_reason=max_tokens`);
}
if (hasTools) {
@@ -1166,7 +1222,7 @@ Continue EXACTLY from where you stopped. DO NOT repeat any content already gener
toolChoiceRetry < TOOL_CHOICE_MAX_RETRIES
) {
toolChoiceRetry++;
console.log(`[Handler] 非流式tool_choice=any 但模型未调用工具(第${toolChoiceRetry}次),强制重试...`);
log.warn('Handler', 'retry', `非流式 tool_choice=any 但模型未调用工具(第${toolChoiceRetry}次),强制重试`);
const forceMessages = [
...activeCursorReq.messages,
@@ -1189,14 +1245,14 @@ Continue EXACTLY from where you stopped. DO NOT repeat any content already gener
({ toolCalls, cleanText } = parseToolCalls(fullText));
}
if (toolChoice?.type === 'any' && toolCalls.length === 0) {
console.log(`[Handler] 非流式tool_choice=any 重试${TOOL_CHOICE_MAX_RETRIES}次后仍无工具调用`);
log.warn('Handler', 'toolparse', `非流式 tool_choice=any 重试${TOOL_CHOICE_MAX_RETRIES}次后仍无工具调用`);
}
if (toolCalls.length > 0) {
stopReason = 'tool_use';
if (isRefusal(cleanText)) {
console.log(`[Handler] Supressed refusal text generated during non-stream tool usage: ${cleanText.substring(0, 100)}...`);
log.info('Handler', 'sanitize', `非流式抑制工具调用中的拒绝文本`, { preview: cleanText.substring(0, 200) });
cleanText = '';
}
@@ -1219,7 +1275,7 @@ Continue EXACTLY from where you stopped. DO NOT repeat any content already gener
const startsRefusal = isRefusal(fullText.substring(0, 300));
const isRealRefusal = stopReason !== 'max_tokens' && (isShort ? isRefusal(fullText) : startsRefusal);
if (isRealRefusal) {
console.log(`[Handler] Supressed pure text refusal (non-stream): ${fullText.substring(0, 100)}...`);
log.info('Handler', 'sanitize', `非流式抑制纯文本拒绝响应`, { preview: fullText.substring(0, 200) });
textToSend = 'Let me proceed with the task.';
}
contentBlocks.push({ type: 'text', text: textToSend });
@@ -1246,10 +1302,14 @@ Continue EXACTLY from where you stopped. DO NOT repeat any content already gener
clearInterval(keepaliveInterval);
res.end(JSON.stringify(response));
// ★ 记录完成
log.recordFinalResponse(fullText);
log.complete(fullText.length, stopReason);
} catch (err: unknown) {
clearInterval(keepaliveInterval);
const message = err instanceof Error ? err.message : String(err);
console.error(`[Handler] 非流式请求处理失败:`, message);
log.fail(message);
try {
res.end(JSON.stringify({
type: 'error',

View File

@@ -11,6 +11,7 @@ import express from 'express';
import { getConfig } from './config.js';
import { handleMessages, listModels, countTokens } from './handler.js';
import { handleOpenAIChatCompletions, handleOpenAIResponses } from './openai-handler.js';
import { serveLogViewer, apiGetLogs, apiGetRequests, apiGetStats, apiGetPayload, apiLogsStream } from './log-viewer.js';
// 从 package.json 读取版本号,统一来源,避免多处硬编码
const require = createRequire(import.meta.url);
@@ -35,6 +36,14 @@ app.use((_req, res, next) => {
next();
});
// ★ 日志查看器(在鉴权之前,始终可访问)
app.get('/logs', serveLogViewer);
app.get('/api/logs', apiGetLogs);
app.get('/api/requests', apiGetRequests);
app.get('/api/stats', apiGetStats);
app.get('/api/payload/:requestId', apiGetPayload);
app.get('/api/logs/stream', apiLogsStream);
// ★ API 鉴权中间件:配置了 authTokens 则需要 Bearer token
app.use((req, res, next) => {
// 跳过无需鉴权的路径
@@ -97,6 +106,7 @@ app.get('/', (_req, res) => {
openai_responses: 'POST /v1/responses',
models: 'GET /v1/models',
health: 'GET /health',
log_viewer: 'GET /logs',
},
usage: {
claude_code: 'export ANTHROPIC_BASE_URL=http://localhost:' + config.port,
@@ -128,6 +138,9 @@ app.listen(config.port, () => {
console.log(' ║ OpenAI / Cursor IDE: ║');
console.log(` ║ OPENAI_BASE_URL= ║`);
console.log(` ║ http://localhost:${config.port}/v1 ║`);
console.log(' ╠══════════════════════════════════════╣');
console.log(' ║ 📊 Log Viewer: ║');
console.log(` ║ http://localhost:${config.port}/logs ║`);
console.log(' ╚══════════════════════════════════════╝');
console.log('');
});

527
src/log-viewer.ts Normal file
View File

@@ -0,0 +1,527 @@
/**
* log-viewer.ts - 全链路日志 Web UI v3
*
* 核心特性:
* - 完整请求参数查看(原始请求 body, messages, tools
* - 提示词查看system prompt, 用户消息)
* - 模型返回内容查看(原始响应, 最终响应, thinking, tool calls
* - 阶段耗时时间线
* - 重试/续写历史
* - 实时 SSE + 搜索 + 过滤
*/
import type { Request, Response } from 'express';
import { getAllLogs, getRequestSummaries, getStats, getRequestPayload, subscribeToLogs, subscribeToSummaries } from './logger.js';
// ==================== API 路由 ====================
export function apiGetLogs(req: Request, res: Response): void {
const { requestId, level, source, limit, since } = req.query;
res.json(getAllLogs({
requestId: requestId as string, level: level as any, source: source as any,
limit: limit ? parseInt(limit as string) : 200,
since: since ? parseInt(since as string) : undefined,
}));
}
export function apiGetRequests(req: Request, res: Response): void {
res.json(getRequestSummaries(req.query.limit ? parseInt(req.query.limit as string) : 50));
}
export function apiGetStats(_req: Request, res: Response): void {
res.json(getStats());
}
/** GET /api/payload/:requestId - 获取请求的完整参数和响应 */
export function apiGetPayload(req: Request, res: Response): void {
const payload = getRequestPayload(req.params.requestId as string);
if (!payload) { res.status(404).json({ error: 'Not found' }); return; }
res.json(payload);
}
export function apiLogsStream(req: Request, res: Response): void {
res.writeHead(200, {
'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache',
'Connection': 'keep-alive', 'X-Accel-Buffering': 'no',
});
const sse = (event: string, data: string) => 'event: ' + event + '\ndata: ' + data + '\n\n';
try { res.write(sse('stats', JSON.stringify(getStats()))); } catch { /**/ }
const unsubLog = subscribeToLogs(e => { try { res.write(sse('log', JSON.stringify(e))); } catch { /**/ } });
const unsubSummary = subscribeToSummaries(s => {
try { res.write(sse('summary', JSON.stringify(s))); res.write(sse('stats', JSON.stringify(getStats()))); } catch { /**/ }
});
const hb = setInterval(() => { try { res.write(': heartbeat\n\n'); } catch { /**/ } }, 15000);
req.on('close', () => { unsubLog(); unsubSummary(); clearInterval(hb); });
}
export function serveLogViewer(_req: Request, res: Response): void {
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.send(LOG_VIEWER_HTML);
}
// ==================== HTML ====================
const LOG_VIEWER_HTML = `<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cursor2API - 全链路日志</title>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500;600&family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<style>
:root{--bg0:#080c14;--bg1:#0f1520;--bg2:#161e2e;--bg3:#1c2740;--bg-card:#131b2a;--bdr:#1e3a5f;--bdr2:#2d4a6f;--t1:#e2e8f0;--t2:#94a3b8;--t3:#64748b;--blue:#3b82f6;--cyan:#06b6d4;--green:#10b981;--yellow:#f59e0b;--red:#ef4444;--purple:#8b5cf6;--pink:#ec4899;--orange:#f97316;--mono:'JetBrains Mono',monospace;--sans:'Inter',sans-serif;}
*{box-sizing:border-box;margin:0;padding:0}
body{font-family:var(--sans);background:var(--bg0);color:var(--t1);height:100vh;overflow:hidden}
body::before{content:'';position:fixed;inset:0;background:radial-gradient(600px 400px at 15% 15%,rgba(59,130,246,.06),transparent 70%),radial-gradient(500px 350px at 85% 80%,rgba(139,92,246,.04),transparent 70%);pointer-events:none;z-index:0}
.app{display:flex;flex-direction:column;height:100vh;position:relative;z-index:1}
/* Header */
.hdr{display:flex;align-items:center;justify-content:space-between;padding:8px 16px;border-bottom:1px solid var(--bdr);background:rgba(15,21,32,.9);backdrop-filter:blur(12px)}
.hdr h1{font-size:15px;font-weight:700;background:linear-gradient(135deg,var(--cyan),var(--blue),var(--purple));-webkit-background-clip:text;-webkit-text-fill-color:transparent;display:flex;align-items:center;gap:6px}
.hdr h1 .ic{font-size:16px;-webkit-text-fill-color:initial}
.hdr-stats{display:flex;gap:10px}
.sc{padding:3px 10px;background:rgba(255,255,255,.03);border:1px solid var(--bdr);border-radius:6px;font-size:11px;color:var(--t2);display:flex;align-items:center;gap:4px}
.sc b{font-family:var(--mono);color:var(--t1);font-weight:600}
.hdr-r{display:flex;gap:8px;align-items:center}
.conn{display:flex;align-items:center;gap:4px;font-size:10px;padding:2px 8px;border-radius:12px;border:1px solid var(--bdr)}
.conn.on{color:var(--green);border-color:rgba(16,185,129,.3)}.conn.off{color:var(--red);border-color:rgba(239,68,68,.3)}
.conn .d{width:5px;height:5px;border-radius:50%}
.conn.on .d{background:var(--green);animation:p 2s infinite}.conn.off .d{background:var(--red)}
@keyframes p{0%,100%{opacity:1}50%{opacity:.3}}
/* Main */
.main{display:flex;flex:1;overflow:hidden}
/* Sidebar */
.side{width:360px;border-right:1px solid var(--bdr);display:flex;flex-direction:column;background:var(--bg1);flex-shrink:0}
.search{padding:6px 10px;border-bottom:1px solid var(--bdr)}
.sw{position:relative}.sw::before{content:'🔍';position:absolute;left:8px;top:50%;transform:translateY(-50%);font-size:11px;pointer-events:none}
.si{width:100%;padding:6px 10px 6px 28px;font-size:11px;background:var(--bg0);border:1px solid var(--bdr);border-radius:6px;color:var(--t1);outline:none;font-family:var(--mono)}
.si:focus{border-color:var(--blue)}.si::placeholder{color:var(--t3)}
.fbar{padding:5px 8px;border-bottom:1px solid var(--bdr);display:flex;gap:3px;flex-wrap:wrap}
.fb{padding:2px 7px;font-size:10px;font-weight:500;border:1px solid var(--bdr);border-radius:14px;background:transparent;color:var(--t2);cursor:pointer;transition:.2s;display:flex;align-items:center;gap:3px}
.fb:hover{border-color:var(--blue);color:var(--blue)}.fb.a{background:var(--blue);border-color:var(--blue);color:#fff}
.fc{font-size:8px;font-weight:600;padding:0 4px;border-radius:8px;background:rgba(255,255,255,.12);min-width:14px;text-align:center}
.fb.a .fc{background:rgba(255,255,255,.2)}
.rlist{flex:1;overflow-y:auto;scrollbar-width:thin;scrollbar-color:var(--bdr) transparent}
.ri{padding:8px 12px;border-bottom:1px solid rgba(30,58,95,.2);cursor:pointer;transition:.15s;position:relative}
.ri:hover{background:var(--bg3)}.ri.a{background:rgba(59,130,246,.1);border-left:3px solid var(--blue)}
.ri .si-dot{position:absolute;right:8px;top:8px;width:7px;height:7px;border-radius:50%}
.si-dot.processing{background:var(--yellow);animation:p 1s infinite}.si-dot.success{background:var(--green)}.si-dot.error{background:var(--red)}.si-dot.intercepted{background:var(--pink)}
.r1{display:flex;align-items:center;justify-content:space-between;margin-bottom:3px}
.rid{font-family:var(--mono);font-size:10px;color:var(--cyan);font-weight:500;display:flex;align-items:center;gap:5px}
.rfmt{font-size:8px;font-weight:700;padding:1px 4px;border-radius:3px;text-transform:uppercase}
.rfmt.anthropic{background:rgba(139,92,246,.2);color:var(--purple)}.rfmt.openai{background:rgba(16,185,129,.2);color:var(--green)}.rfmt.responses{background:rgba(249,115,22,.2);color:var(--orange)}
.rtm{font-size:9px;color:var(--t3);font-family:var(--mono)}
.r2{display:flex;align-items:center;gap:5px;margin-bottom:3px}
.rmod{font-size:10px;color:var(--t2);max-width:180px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
.rch{font-size:9px;color:var(--t3);font-family:var(--mono)}
.rbd{display:flex;gap:2px;flex-wrap:wrap}
.bg{font-size:8px;font-weight:500;padding:1px 4px;border-radius:8px}
.bg.str{background:rgba(6,182,212,.12);color:var(--cyan)}.bg.tls{background:rgba(139,92,246,.12);color:var(--purple)}.bg.rtr{background:rgba(245,158,11,.12);color:var(--yellow)}.bg.cnt{background:rgba(249,115,22,.12);color:var(--orange)}.bg.err{background:rgba(239,68,68,.12);color:var(--red)}.bg.icp{background:rgba(236,72,153,.12);color:var(--pink)}
.rdbar{height:2px;border-radius:1px;margin-top:4px;background:var(--bg0);overflow:hidden}
.rdfill{height:100%;border-radius:1px;transition:width .3s}
.rdfill.f{background:var(--green)}.rdfill.m{background:var(--yellow)}.rdfill.s{background:var(--orange)}.rdfill.vs{background:var(--red)}.rdfill.pr{background:var(--blue);animation:pp 1.5s infinite}
@keyframes pp{0%{opacity:1}50%{opacity:.4}100%{opacity:1}}
/* Detail Panel */
.dp{flex:1;display:flex;flex-direction:column;overflow:hidden}
.dh{padding:8px 14px;border-bottom:1px solid var(--bdr);display:flex;align-items:center;justify-content:space-between;background:var(--bg1);flex-shrink:0}
.dh h2{font-size:12px;font-weight:600;display:flex;align-items:center;gap:5px}
.dh-acts{display:flex;gap:4px}
/* Tabs */
.tabs{display:flex;border-bottom:1px solid var(--bdr);background:var(--bg1);flex-shrink:0}
.tab{padding:7px 16px;font-size:11px;font-weight:500;color:var(--t2);cursor:pointer;border-bottom:2px solid transparent;transition:.2s;position:relative}
.tab:hover{color:var(--t1);background:rgba(255,255,255,.02)}
.tab.a{color:var(--cyan);border-bottom-color:var(--cyan)}
.tab .dot{position:absolute;top:4px;right:4px;width:5px;height:5px;border-radius:50%;background:var(--blue);display:none}
/* Tab Content */
.tab-content{flex:1;overflow-y:auto;padding:0;scrollbar-width:thin;scrollbar-color:var(--bdr) transparent}
/* Summary Card */
.scard{padding:10px 14px;background:var(--bg-card);border-bottom:1px solid var(--bdr);flex-shrink:0;display:none}
.sgrid{display:grid;grid-template-columns:repeat(auto-fill,minmax(120px,1fr));gap:8px}
.si2{display:flex;flex-direction:column;gap:1px}
.si2 .l{font-size:8px;text-transform:uppercase;color:var(--t3);letter-spacing:.3px}
.si2 .v{font-size:11px;font-weight:500;color:var(--t1);font-family:var(--mono)}
/* Phase Timeline */
.ptl{padding:8px 14px;border-bottom:1px solid var(--bdr);background:var(--bg-card);flex-shrink:0;display:none}
.ptl-lbl{font-size:9px;text-transform:uppercase;color:var(--t3);margin-bottom:4px;letter-spacing:.3px}
.ptl-bar{display:flex;height:20px;border-radius:4px;overflow:hidden;background:var(--bg0);gap:1px}
.pseg{display:flex;align-items:center;justify-content:center;font-size:8px;font-weight:500;color:rgba(255,255,255,.85);min-width:2px;position:relative;cursor:default}
.pseg:hover{opacity:.8}
.pseg .tip{position:absolute;bottom:100%;left:50%;transform:translateX(-50%);background:var(--bg3);border:1px solid var(--bdr);padding:3px 6px;border-radius:4px;font-size:9px;white-space:nowrap;pointer-events:none;opacity:0;transition:.1s;z-index:10}
.pseg:hover .tip{opacity:1}
/* Log entries */
.llist{padding:4px}
.le{display:grid;grid-template-columns:65px 48px 38px 60px 72px 1fr;gap:6px;padding:5px 8px;border-radius:4px;margin-bottom:1px;font-size:11px;position:relative;align-items:start}
.le:hover{background:var(--bg3)}
.le.ani{animation:fi .2s ease}
@keyframes fi{from{opacity:0;transform:translateY(-2px)}to{opacity:1;transform:translateY(0)}}
.lt{font-family:var(--mono);font-size:9px;color:var(--t3);white-space:nowrap;padding-top:2px}
.ld{font-family:var(--mono);font-size:9px;color:var(--t3);text-align:right;padding-top:2px}
.ll{font-size:8px;font-weight:600;padding:2px 0;border-radius:2px;text-transform:uppercase;text-align:center}
.ll.debug{background:rgba(100,116,139,.12);color:var(--t3)}.ll.info{background:rgba(59,130,246,.1);color:var(--blue)}.ll.warn{background:rgba(245,158,11,.1);color:var(--yellow)}.ll.error{background:rgba(239,68,68,.1);color:var(--red)}
.ls{font-size:9px;font-weight:500;color:var(--purple);padding-top:2px}
.lp{font-size:8px;padding:2px 3px;border-radius:2px;background:rgba(6,182,212,.06);color:var(--cyan);text-align:center}
.lm{color:var(--t1);word-break:break-word;line-height:1.35}
.ldt{color:var(--blue);font-size:9px;cursor:pointer;margin-top:2px;display:inline-block;user-select:none}
.ldt:hover{text-decoration:underline}
.ldd{margin-top:3px;padding:6px 8px;background:var(--bg0);border-radius:4px;font-family:var(--mono);font-size:9px;color:var(--t2);white-space:pre-wrap;word-break:break-all;max-height:200px;overflow-y:auto;border:1px solid var(--bdr);line-height:1.4}
.tli{position:absolute;left:0;top:0;bottom:0;width:2px;border-radius:0 2px 2px 0}
/* Content display (for request/response tabs) */
.content-section{padding:12px 16px;border-bottom:1px solid var(--bdr)}
.content-section:last-child{border-bottom:none}
.cs-title{font-size:11px;font-weight:600;color:var(--cyan);text-transform:uppercase;letter-spacing:.3px;margin-bottom:8px;display:flex;align-items:center;gap:6px}
.cs-title .cnt{font-size:9px;font-weight:400;color:var(--t3);font-family:var(--mono)}
.msg-item{margin-bottom:8px;border:1px solid var(--bdr);border-radius:6px;overflow:hidden}
.msg-header{padding:6px 10px;background:var(--bg2);display:flex;align-items:center;justify-content:space-between;cursor:pointer}
.msg-header:hover{background:var(--bg3)}
.msg-role{font-size:10px;font-weight:600;text-transform:uppercase;display:flex;align-items:center;gap:5px}
.msg-role.system{color:var(--pink)}.msg-role.user{color:var(--blue)}.msg-role.assistant{color:var(--green)}.msg-role.tool{color:var(--orange)}
.msg-meta{font-size:9px;color:var(--t3);font-family:var(--mono)}
.msg-body{padding:8px 10px;font-family:var(--mono);font-size:10px;color:var(--t2);white-space:pre-wrap;word-break:break-word;line-height:1.5;max-height:400px;overflow-y:auto;background:var(--bg0)}
.tool-item{padding:6px 10px;border:1px solid var(--bdr);border-radius:4px;margin-bottom:4px}
.tool-name{font-family:var(--mono);font-size:11px;font-weight:600;color:var(--purple)}
.tool-desc{font-size:10px;color:var(--t3);margin-top:2px}
.resp-box{padding:10px 12px;background:var(--bg0);border:1px solid var(--bdr);border-radius:6px;font-family:var(--mono);font-size:10px;color:var(--t2);white-space:pre-wrap;word-break:break-word;line-height:1.5;max-height:600px;overflow-y:auto}
.resp-box.diff{border-color:var(--yellow)}
.retry-item{margin-bottom:8px;border:1px solid rgba(245,158,11,.2);border-radius:6px;overflow:hidden}
.retry-header{padding:5px 10px;background:rgba(245,158,11,.05);font-size:10px;font-weight:500;color:var(--yellow)}
.retry-body{padding:8px 10px;font-family:var(--mono);font-size:10px;color:var(--t2);white-space:pre-wrap;max-height:200px;overflow-y:auto;background:var(--bg0)}
/* JSON highlights */
.jk{color:var(--cyan)}.js{color:var(--green)}.jn{color:var(--yellow)}.jb{color:var(--purple)}.jnl{color:var(--t3)}
/* Empty */
.empty{display:flex;flex-direction:column;align-items:center;justify-content:center;height:100%;color:var(--t3);gap:8px}
.empty .ic{font-size:30px;opacity:.2}
.empty p{font-size:12px}.empty .sub{font-size:10px;opacity:.6}
/* Level filter pills */
.lvf{display:flex;gap:3px}
.lvb{padding:2px 8px;font-size:10px;border:1px solid var(--bdr);border-radius:5px;background:transparent;color:var(--t2);cursor:pointer;transition:.2s}
.lvb:hover{border-color:var(--blue);color:var(--blue)}.lvb.a{background:var(--blue);border-color:var(--blue);color:#fff}
/* Scrollbar */
::-webkit-scrollbar{width:4px}::-webkit-scrollbar-track{background:transparent}::-webkit-scrollbar-thumb{background:var(--bdr);border-radius:2px}
</style>
</head>
<body>
<div class="app">
<div class="hdr">
<h1><span class="ic">⚡</span> Cursor2API 日志</h1>
<div class="hdr-stats">
<div class="sc"><b id="sT">0</b>请求</div>
<div class="sc">✓<b id="sS">0</b></div>
<div class="sc">✗<b id="sE">0</b></div>
<div class="sc"><b id="sA">-</b>ms 均耗</div>
<div class="sc">⚡<b id="sF">-</b>ms TTFT</div>
</div>
<div class="hdr-r">
<div class="conn on" id="conn"><div class="d"></div><span>已连接</span></div>
</div>
</div>
<div class="main">
<div class="side">
<div class="search"><div class="sw"><input class="si" id="searchIn" placeholder="搜索 requestId / model... (Ctrl+K)"/></div></div>
<div class="fbar" id="fbar">
<button class="fb a" data-f="all" onclick="fR('all',this)">全部<span class="fc" id="cA">0</span></button>
<button class="fb" data-f="success" onclick="fR('success',this)">✓<span class="fc" id="cS">0</span></button>
<button class="fb" data-f="error" onclick="fR('error',this)">✗<span class="fc" id="cE">0</span></button>
<button class="fb" data-f="processing" onclick="fR('processing',this)">◌<span class="fc" id="cP">0</span></button>
<button class="fb" data-f="intercepted" onclick="fR('intercepted',this)">⊘<span class="fc" id="cI">0</span></button>
</div>
<div class="rlist" id="rlist">
<div class="empty"><div class="ic">📡</div><p>等待请求...</p></div>
</div>
</div>
<div class="dp">
<div class="dh">
<h2>🔍 <span id="dTitle">实时日志流</span></h2>
<div class="dh-acts">
<div class="lvf" id="lvF">
<button class="lvb a" onclick="sL('all',this)">全部</button>
<button class="lvb" onclick="sL('info',this)">Info</button>
<button class="lvb" onclick="sL('warn',this)">Warn</button>
<button class="lvb" onclick="sL('error',this)">Error</button>
</div>
</div>
</div>
<div class="scard" id="scard"><div class="sgrid" id="sgrid"></div></div>
<div class="ptl" id="ptl"><div class="ptl-lbl">阶段耗时</div><div class="ptl-bar" id="pbar"></div></div>
<div class="tabs" id="tabs" style="display:none">
<div class="tab a" onclick="setTab('logs',this)">📋 日志</div>
<div class="tab" onclick="setTab('request',this)">📥 请求参数</div>
<div class="tab" onclick="setTab('prompts',this)">💬 提示词</div>
<div class="tab" onclick="setTab('response',this)">📤 响应内容</div>
</div>
<div class="tab-content" id="tabContent">
<div class="llist" id="logList">
<div class="empty"><div class="ic">📋</div><p>实时日志将在此显示</p><p class="sub">发起请求后即可看到全链路日志</p></div>
</div>
</div>
</div>
</div>
</div>
<script>
let reqs=[],rmap={},logs=[],selId=null,cFil='all',cLv='all',sq='',curTab='logs',curPayload=null;
const PC={receive:'var(--blue)',convert:'var(--cyan)',send:'var(--purple)',response:'var(--purple)',thinking:'#a855f7',refusal:'var(--yellow)',retry:'var(--yellow)',truncation:'var(--yellow)',continuation:'var(--yellow)',toolparse:'var(--orange)',sanitize:'var(--orange)',stream:'var(--green)',complete:'var(--green)',error:'var(--red)',intercept:'var(--pink)',auth:'var(--t3)'};
async function init(){
try{
const[a,b]=await Promise.all([fetch('/api/requests?limit=100'),fetch('/api/logs?limit=500')]);
reqs=await a.json();logs=await b.json();rmap={};reqs.forEach(r=>rmap[r.requestId]=r);
renderRL();updCnt();updStats();
}catch(e){console.error(e)}
connectSSE();
}
let es;
function connectSSE(){
if(es)try{es.close()}catch{}
es=new EventSource('/api/logs/stream');
es.addEventListener('log',e=>{const en=JSON.parse(e.data);logs.push(en);if(logs.length>5000)logs=logs.slice(-3000);if(!selId||selId===en.requestId){if(curTab==='logs')appendLog(en)}});
es.addEventListener('summary',e=>{const s=JSON.parse(e.data);const isNew=!rmap[s.requestId];rmap[s.requestId]=s;const i=reqs.findIndex(r=>r.requestId===s.requestId);if(i>=0)reqs[i]=s;else reqs.unshift(s);renderRL();updCnt();if(selId===s.requestId)renderSCard(s)});
es.addEventListener('stats',e=>{applyStats(JSON.parse(e.data))});
es.onopen=()=>{const c=document.getElementById('conn');c.className='conn on';c.querySelector('span').textContent='已连接'};
es.onerror=()=>{const c=document.getElementById('conn');c.className='conn off';c.querySelector('span').textContent='重连中...';setTimeout(connectSSE,3000)};
}
function updStats(){fetch('/api/stats').then(r=>r.json()).then(applyStats).catch(()=>{})}
function applyStats(s){document.getElementById('sT').textContent=s.totalRequests;document.getElementById('sS').textContent=s.successCount;document.getElementById('sE').textContent=s.errorCount;document.getElementById('sA').textContent=s.avgResponseTime||'-';document.getElementById('sF').textContent=s.avgTTFT||'-'}
function updCnt(){
const q=sq.toLowerCase();let a=0,s=0,e=0,p=0,i=0;
reqs.forEach(r=>{if(q&&!mS(r,q))return;a++;if(r.status==='success')s++;else if(r.status==='error')e++;else if(r.status==='processing')p++;else if(r.status==='intercepted')i++});
document.getElementById('cA').textContent=a;document.getElementById('cS').textContent=s;document.getElementById('cE').textContent=e;document.getElementById('cP').textContent=p;document.getElementById('cI').textContent=i;
}
function mS(r,q){return r.requestId.includes(q)||r.model.toLowerCase().includes(q)||r.path.toLowerCase().includes(q)}
function renderRL(){
const el=document.getElementById('rlist');const q=sq.toLowerCase();
let f=reqs;if(q)f=f.filter(r=>mS(r,q));if(cFil!=='all')f=f.filter(r=>r.status===cFil);
if(!f.length){el.innerHTML='<div class="empty"><div class="ic">📡</div><p>'+(q?'无匹配':'暂无请求')+'</p></div>';return}
el.innerHTML=f.map(r=>{
const ac=r.requestId===selId,ago=timeAgo(r.startTime),dur=r.endTime?((r.endTime-r.startTime)/1000).toFixed(1)+'s':'...',durMs=r.endTime?r.endTime-r.startTime:Date.now()-r.startTime;
const pct=Math.min(100,durMs/30000*100),dc=!r.endTime?'pr':durMs<3000?'f':durMs<10000?'m':durMs<20000?'s':'vs';
const ch=r.responseChars>0?fmtN(r.responseChars)+' chars':'',tt=r.ttft?r.ttft+'ms':'';
let bd='';if(r.stream)bd+='<span class="bg str">Stream</span>';if(r.hasTools)bd+='<span class="bg tls">T:'+r.toolCount+'</span>';
if(r.retryCount>0)bd+='<span class="bg rtr">R:'+r.retryCount+'</span>';if(r.continuationCount>0)bd+='<span class="bg cnt">C:'+r.continuationCount+'</span>';
if(r.status==='error')bd+='<span class="bg err">ERR</span>';if(r.status==='intercepted')bd+='<span class="bg icp">INTERCEPT</span>';
const fm=r.apiFormat||'anthropic';
return '<div class="ri'+(ac?' a':'')+'" data-r="'+r.requestId+'">'+'<div class="si-dot '+r.status+'"></div>'+'<div class="r1"><span class="rid">'+r.requestId+' <span class="rfmt '+fm+'">'+fm+'</span></span><span class="rtm">'+(tt?'⚡'+tt+' · ':'')+dur+' · '+ago+'</span></div>'+'<div class="r2"><span class="rmod">'+escH(r.model)+'</span>'+(ch?'<span class="rch">→ '+ch+'</span>':'')+'</div>'+'<div class="rbd">'+bd+'</div>'+'<div class="rdbar"><div class="rdfill '+dc+'" style="width:'+pct+'%"></div></div></div>';
}).join('');
}
// ===== Select Request =====
async function selReq(id){
if(selId===id){desel();return}
selId=id;renderRL();
const s=rmap[id];
if(s){document.getElementById('dTitle').textContent='请求 '+id;renderSCard(s)}
document.getElementById('tabs').style.display='flex';
curTab='logs';setTab('logs',document.querySelector('.tab'));
// Load payload data
try{const r=await fetch('/api/payload/'+id);if(r.ok)curPayload=await r.json();else curPayload=null}catch{curPayload=null}
// Render log tab
const ll=logs.filter(l=>l.requestId===id);renderLogs(ll);
}
function desel(){
selId=null;curPayload=null;renderRL();
document.getElementById('dTitle').textContent='实时日志流';
document.getElementById('scard').style.display='none';
document.getElementById('ptl').style.display='none';
document.getElementById('tabs').style.display='none';
curTab='logs';
renderLogs(logs.slice(-200));
}
function renderSCard(s){
const c=document.getElementById('scard');c.style.display='block';
const dur=s.endTime?((s.endTime-s.startTime)/1000).toFixed(2)+'s':'进行中...';
const sc={processing:'var(--yellow)',success:'var(--green)',error:'var(--red)',intercepted:'var(--pink)'}[s.status]||'var(--t3)';
const items=[['状态','<span style="color:'+sc+'">'+s.status.toUpperCase()+'</span>'],['耗时',dur],['模型',escH(s.model)],['格式',(s.apiFormat||'anthropic').toUpperCase()],['消息数',s.messageCount],['响应字数',fmtN(s.responseChars)],['TTFT',s.ttft?s.ttft+'ms':'-'],['API耗时',s.cursorApiTime?s.cursorApiTime+'ms':'-'],['停止原因',s.stopReason||'-'],['重试',s.retryCount],['续写',s.continuationCount],['工具调用',s.toolCallsDetected]];
if(s.thinkingChars>0)items.push(['Thinking',fmtN(s.thinkingChars)+' chars']);
if(s.error)items.push(['错误','<span style="color:var(--red)">'+escH(s.error)+'</span>']);
document.getElementById('sgrid').innerHTML=items.map(([l,v])=>'<div class="si2"><span class="l">'+l+'</span><span class="v">'+v+'</span></div>').join('');
renderPTL(s);
}
function renderPTL(s){
const el=document.getElementById('ptl'),bar=document.getElementById('pbar');
if(!s.phaseTimings||!s.phaseTimings.length){el.style.display='none';return}
el.style.display='block';const tot=(s.endTime||Date.now())-s.startTime;if(tot<=0){el.style.display='none';return}
bar.innerHTML=s.phaseTimings.map(pt=>{const d=pt.duration||((pt.endTime||Date.now())-pt.startTime);const pct=Math.max(1,d/tot*100);const bg=PC[pt.phase]||'var(--t3)';return '<div class="pseg" style="width:'+pct+'%;background:'+bg+'" title="'+pt.label+': '+d+'ms"><span class="tip">'+escH(pt.label)+' '+d+'ms</span>'+(pct>10?'<span style="font-size:7px">'+pt.phase+'</span>':'')+'</div>'}).join('');
}
// ===== Tabs =====
function setTab(tab,el){
curTab=tab;
document.querySelectorAll('.tab').forEach(t=>t.classList.remove('a'));
el.classList.add('a');
const tc=document.getElementById('tabContent');
if(tab==='logs'){
tc.innerHTML='<div class="llist" id="logList"></div>';
if(selId){renderLogs(logs.filter(l=>l.requestId===selId))}else{renderLogs(logs.slice(-200))}
} else if(tab==='request'){
renderRequestTab(tc);
} else if(tab==='prompts'){
renderPromptsTab(tc);
} else if(tab==='response'){
renderResponseTab(tc);
}
}
function renderRequestTab(tc){
if(!curPayload){tc.innerHTML='<div class="empty"><div class="ic">📥</div><p>暂无请求数据</p></div>';return}
let h='';
// Original request summary
const s=selId?rmap[selId]:null;
if(s){
h+='<div class="content-section"><div class="cs-title">📋 请求概要</div>';
h+='<div class="resp-box">'+syntaxHL({method:s.method,path:s.path,model:s.model,stream:s.stream,apiFormat:s.apiFormat,messageCount:s.messageCount,toolCount:s.toolCount,hasTools:s.hasTools})+'</div></div>';
}
// Tools
if(curPayload.tools&&curPayload.tools.length){
h+='<div class="content-section"><div class="cs-title">🔧 工具定义 <span class="cnt">'+curPayload.tools.length+' 个</span></div>';
curPayload.tools.forEach(t=>{h+='<div class="tool-item"><div class="tool-name">'+escH(t.name)+'</div>'+(t.description?'<div class="tool-desc">'+escH(t.description)+'</div>':'')+'</div>'});
h+='</div>';
}
// Cursor request
if(curPayload.cursorRequest){
h+='<div class="content-section"><div class="cs-title">🔄 Cursor 请求(转换后)</div>';
h+='<div class="resp-box">'+syntaxHL(curPayload.cursorRequest)+'</div></div>';
}
if(curPayload.cursorMessages&&curPayload.cursorMessages.length){
h+='<div class="content-section"><div class="cs-title">📨 Cursor 消息列表 <span class="cnt">'+curPayload.cursorMessages.length+' 条</span></div>';
curPayload.cursorMessages.forEach((m,i)=>{
const collapsed=m.contentPreview.length>500;
h+='<div class="msg-item"><div class="msg-header" onclick="togMsg(this)"><span class="msg-role '+m.role+'">'+m.role+' #'+(i+1)+'</span><span class="msg-meta">'+fmtN(m.contentLength)+' chars '+(collapsed?'▶ 展开':'▼ 收起')+'</span></div><div class="msg-body" style="display:'+(collapsed?'none':'block')+';max-height:800px;overflow-y:auto">'+escH(m.contentPreview)+'</div></div>';
});
h+='</div>';
}
tc.innerHTML=h||'<div class="empty"><div class="ic">📥</div><p>暂无请求数据</p></div>';
}
function renderPromptsTab(tc){
if(!curPayload){tc.innerHTML='<div class="empty"><div class="ic">💬</div><p>暂无提示词数据</p></div>';return}
let h='';
// System prompt
if(curPayload.systemPrompt){
h+='<div class="content-section"><div class="cs-title">🔒 System Prompt <span class="cnt">'+fmtN(curPayload.systemPrompt.length)+' chars</span></div>';
h+='<div class="resp-box" style="max-height:600px;overflow-y:auto">'+escH(curPayload.systemPrompt)+'</div></div>';
}
// Messages
if(curPayload.messages&&curPayload.messages.length){
h+='<div class="content-section"><div class="cs-title">💬 消息列表 <span class="cnt">'+curPayload.messages.length+' 条</span></div>';
curPayload.messages.forEach((m,i)=>{
const imgs=m.hasImages?' 🖼️':'';
const collapsed=m.contentPreview.length>500;
h+='<div class="msg-item"><div class="msg-header" onclick="togMsg(this)"><span class="msg-role '+m.role+'">'+m.role+imgs+' #'+(i+1)+'</span><span class="msg-meta">'+fmtN(m.contentLength)+' chars '+(collapsed?'▶ 展开':'▼ 收起')+'</span></div><div class="msg-body" style="display:'+(collapsed?'none':'block')+';max-height:800px;overflow-y:auto">'+escH(m.contentPreview)+'</div></div>';
});
h+='</div>';
}
tc.innerHTML=h||'<div class="empty"><div class="ic">💬</div><p>暂无提示词数据</p></div>';
}
function renderResponseTab(tc){
if(!curPayload){tc.innerHTML='<div class="empty"><div class="ic">📤</div><p>暂无响应数据</p></div>';return}
let h='';
// Thinking
if(curPayload.thinkingContent){
h+='<div class="content-section"><div class="cs-title">🧠 Thinking 内容 <span class="cnt">'+fmtN(curPayload.thinkingContent.length)+' chars</span></div>';
h+='<div class="resp-box" style="border-color:var(--purple);max-height:300px">'+escH(curPayload.thinkingContent)+'</div></div>';
}
// Raw response
if(curPayload.rawResponse){
h+='<div class="content-section"><div class="cs-title">📝 模型原始返回 <span class="cnt">'+fmtN(curPayload.rawResponse.length)+' chars</span></div>';
h+='<div class="resp-box" style="max-height:400px">'+escH(curPayload.rawResponse)+'</div></div>';
}
// Final response
if(curPayload.finalResponse&&curPayload.finalResponse!==curPayload.rawResponse){
h+='<div class="content-section"><div class="cs-title">✅ 最终响应(处理后)<span class="cnt">'+fmtN(curPayload.finalResponse.length)+' chars</span></div>';
h+='<div class="resp-box diff" style="max-height:400px">'+escH(curPayload.finalResponse)+'</div></div>';
}
// Tool calls
if(curPayload.toolCalls&&curPayload.toolCalls.length){
h+='<div class="content-section"><div class="cs-title">🔧 工具调用结果 <span class="cnt">'+curPayload.toolCalls.length+' 个</span></div>';
h+='<div class="resp-box">'+syntaxHL(curPayload.toolCalls)+'</div></div>';
}
// Retry history
if(curPayload.retryResponses&&curPayload.retryResponses.length){
h+='<div class="content-section"><div class="cs-title">🔄 重试历史 <span class="cnt">'+curPayload.retryResponses.length+' 次</span></div>';
curPayload.retryResponses.forEach(r=>{h+='<div class="retry-item"><div class="retry-header">第 '+r.attempt+' 次重试 — '+escH(r.reason)+'</div><div class="retry-body">'+escH(r.response.substring(0,1000))+(r.response.length>1000?'\\n... ('+fmtN(r.response.length)+' chars)':'')+'</div></div>'});
h+='</div>';
}
// Continuation history
if(curPayload.continuationResponses&&curPayload.continuationResponses.length){
h+='<div class="content-section"><div class="cs-title">📎 续写历史 <span class="cnt">'+curPayload.continuationResponses.length+' 次</span></div>';
curPayload.continuationResponses.forEach(r=>{h+='<div class="retry-item"><div class="retry-header" style="color:var(--orange)">续写 #'+r.index+' (去重后 '+fmtN(r.dedupedLength)+' chars)</div><div class="retry-body">'+escH(r.response.substring(0,1000))+(r.response.length>1000?'\\n...':'')+'</div></div>'});
h+='</div>';
}
tc.innerHTML=h||'<div class="empty"><div class="ic">📤</div><p>暂无响应数据</p></div>';
}
// ===== Log rendering =====
function renderLogs(ll){
const el=document.getElementById('logList');if(!el)return;
const fil=cLv==='all'?ll:ll.filter(l=>l.level===cLv);
if(!fil.length){el.innerHTML='<div class="empty"><div class="ic">📋</div><p>暂无日志</p></div>';return}
el.innerHTML=fil.map(l=>logH(l)).join('');el.scrollTop=el.scrollHeight;
}
function logH(l){
const t=new Date(l.timestamp).toLocaleTimeString('zh-CN',{hour12:false,hour:'2-digit',minute:'2-digit',second:'2-digit'});
const d=l.duration!=null?'+'+l.duration+'ms':'';
const det=l.details?'<div class="ldt" onclick="togDet(this)">▶ 详情</div><div class="ldd" style="display:none">'+syntaxHL(l.details)+'</div>':'';
return '<div class="le"><div class="tli" style="background:'+(PC[l.phase]||'var(--t3)')+'"></div><span class="lt">'+t+'</span><span class="ld">'+d+'</span><span class="ll '+l.level+'">'+l.level+'</span><span class="ls">'+l.source+'</span><span class="lp">'+l.phase+'</span><div class="lm">'+escH(l.message)+det+'</div></div>';
}
function appendLog(en){
const el=document.getElementById('logList');if(!el)return;
if(el.querySelector('.empty'))el.innerHTML='';
if(cLv!=='all'&&en.level!==cLv)return;
const d=document.createElement('div');d.innerHTML=logH(en);const n=d.firstElementChild;n.classList.add('ani');el.appendChild(n);
while(el.children.length>500)el.removeChild(el.firstChild);
el.scrollTop=el.scrollHeight;
}
// ===== Utils =====
function escH(s){if(!s)return'';const d=document.createElement('div');d.textContent=String(s);return d.innerHTML}
function timeAgo(ts){const s=Math.floor((Date.now()-ts)/1000);if(s<5)return'刚刚';if(s<60)return s+'s前';if(s<3600)return Math.floor(s/60)+'m前';return Math.floor(s/3600)+'h前'}
function fmtN(n){if(n>=1e6)return(n/1e6).toFixed(1)+'M';if(n>=1e3)return(n/1e3).toFixed(1)+'K';return String(n)}
function syntaxHL(data){
try{const s=typeof data==='string'?data:JSON.stringify(data,null,2);
return s.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;')
.replace(/"([^"]+)"\\s*:/g,'<span class="jk">"$1"</span>:')
.replace(/:\\s*"([^"]*?)"/g,': <span class="js">"$1"</span>')
.replace(/:\\s*(\\d+\\.?\\d*)/g,': <span class="jn">$1</span>')
.replace(/:\\s*(true|false)/g,': <span class="jb">$1</span>')
.replace(/:\\s*(null)/g,': <span class="jnl">null</span>')
}catch{return escH(String(data))}
}
function togDet(el){const d=el.nextElementSibling;if(d.style.display==='none'){d.style.display='block';el.textContent='▼ 收起'}else{d.style.display='none';el.textContent='▶ 详情'}}
function togMsg(el){const b=el.nextElementSibling;const isHidden=b.style.display==='none';b.style.display=isHidden?'block':'none';const m=el.querySelector('.msg-meta');if(m){const t=m.textContent;m.textContent=isHidden?t.replace('▶ 展开','▼ 收起'):t.replace('▼ 收起','▶ 展开')}}
function fR(f,btn){cFil=f;document.querySelectorAll('#fbar .fb').forEach(b=>b.classList.remove('a'));btn.classList.add('a');renderRL()}
function sL(lv,btn){cLv=lv;document.querySelectorAll('#lvF .lvb').forEach(b=>b.classList.remove('a'));btn.classList.add('a');if(curTab==='logs'){if(selId)renderLogs(logs.filter(l=>l.requestId===selId));else renderLogs(logs.slice(-200))}}
// Keyboard
document.addEventListener('keydown',e=>{
if((e.ctrlKey||e.metaKey)&&e.key==='k'){e.preventDefault();document.getElementById('searchIn').focus();return}
if(e.key==='Escape'){if(document.activeElement===document.getElementById('searchIn')){document.getElementById('searchIn').blur();document.getElementById('searchIn').value='';sq='';renderRL();updCnt()}else{desel()}return}
if(e.key==='ArrowDown'||e.key==='ArrowUp'){e.preventDefault();const q=sq.toLowerCase();let f=reqs;if(q)f=f.filter(r=>mS(r,q));if(cFil!=='all')f=f.filter(r=>r.status===cFil);if(!f.length)return;const ci=selId?f.findIndex(r=>r.requestId===selId):-1;let ni;if(e.key==='ArrowDown')ni=ci<f.length-1?ci+1:0;else ni=ci>0?ci-1:f.length-1;selReq(f[ni].requestId);const it=document.querySelector('[data-r="'+f[ni].requestId+'"]');if(it)it.scrollIntoView({block:'nearest'})}
});
document.getElementById('searchIn').addEventListener('input',e=>{sq=e.target.value;renderRL();updCnt()});
document.getElementById('rlist').addEventListener('click',e=>{const el=e.target.closest('[data-r]');if(el)selReq(el.getAttribute('data-r'))});
setInterval(renderRL,30000);
init();
</script>
</body>
</html>`;

465
src/logger.ts Normal file
View File

@@ -0,0 +1,465 @@
/**
* logger.ts - 全链路日志系统 v3
*
* 核心升级:
* - 存储完整的请求参数messages, system prompt, tools
* - 存储完整的模型返回内容raw response
* - 存储转换后的 Cursor 请求
* - 阶段耗时追踪 (Phase Timing)
* - TTFT (Time To First Token)
* - 全部通过 Web UI 可视化
*/
import { EventEmitter } from 'events';
// ==================== 类型定义 ====================
export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
export type LogSource = 'Handler' | 'OpenAI' | 'Cursor' | 'Auth' | 'System' | 'Converter';
export type LogPhase =
| 'receive' | 'auth' | 'convert' | 'intercept' | 'send'
| 'response' | 'refusal' | 'retry' | 'truncation' | 'continuation'
| 'thinking' | 'toolparse' | 'sanitize' | 'stream' | 'complete' | 'error';
export interface LogEntry {
id: string;
requestId: string;
timestamp: number;
level: LogLevel;
source: LogSource;
phase: LogPhase;
message: string;
details?: unknown;
duration?: number;
}
export interface PhaseTiming {
phase: LogPhase;
label: string;
startTime: number;
endTime?: number;
duration?: number;
}
/**
* 完整请求数据 — 存储每个请求的全量参数和响应
*/
export interface RequestPayload {
// ===== 原始请求 =====
/** 原始请求 bodyAnthropic 或 OpenAI 格式) */
originalRequest?: unknown;
/** System prompt提取出来方便查看 */
systemPrompt?: string;
/** 用户消息列表摘要 */
messages?: Array<{ role: string; contentPreview: string; contentLength: number; hasImages?: boolean }>;
/** 工具定义列表 */
tools?: Array<{ name: string; description?: string }>;
// ===== 转换后请求 =====
/** 转换后的 Cursor 请求 */
cursorRequest?: unknown;
/** Cursor 消息列表摘要 */
cursorMessages?: Array<{ role: string; contentPreview: string; contentLength: number }>;
// ===== 模型响应 =====
/** 原始模型返回全文 */
rawResponse?: string;
/** 清洗/处理后的最终响应 */
finalResponse?: string;
/** Thinking 内容 */
thinkingContent?: string;
/** 工具调用解析结果 */
toolCalls?: unknown[];
/** 每次重试的原始响应 */
retryResponses?: Array<{ attempt: number; response: string; reason: string }>;
/** 每次续写的原始响应 */
continuationResponses?: Array<{ index: number; response: string; dedupedLength: number }>;
}
export interface RequestSummary {
requestId: string;
startTime: number;
endTime?: number;
method: string;
path: string;
model: string;
stream: boolean;
apiFormat: 'anthropic' | 'openai' | 'responses';
hasTools: boolean;
toolCount: number;
messageCount: number;
status: 'processing' | 'success' | 'error' | 'intercepted';
responseChars: number;
retryCount: number;
continuationCount: number;
stopReason?: string;
error?: string;
toolCallsDetected: number;
ttft?: number;
cursorApiTime?: number;
phaseTimings: PhaseTiming[];
thinkingChars: number;
systemPromptLength: number;
}
// ==================== 存储 ====================
const MAX_ENTRIES = 5000;
const MAX_REQUESTS = 200;
let logCounter = 0;
const logEntries: LogEntry[] = [];
const requestSummaries: Map<string, RequestSummary> = new Map();
const requestPayloads: Map<string, RequestPayload> = new Map();
const requestOrder: string[] = [];
const logEmitter = new EventEmitter();
logEmitter.setMaxListeners(50);
function shortId(): string {
const chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
let id = '';
for (let i = 0; i < 8; i++) id += chars[Math.floor(Math.random() * chars.length)];
return id;
}
// ==================== 统计 ====================
export function getStats() {
let success = 0, error = 0, intercepted = 0, processing = 0;
let totalTime = 0, timeCount = 0, totalTTFT = 0, ttftCount = 0;
for (const s of requestSummaries.values()) {
if (s.status === 'success') success++;
else if (s.status === 'error') error++;
else if (s.status === 'intercepted') intercepted++;
else if (s.status === 'processing') processing++;
if (s.endTime) { totalTime += s.endTime - s.startTime; timeCount++; }
if (s.ttft) { totalTTFT += s.ttft; ttftCount++; }
}
return {
totalRequests: requestSummaries.size,
successCount: success, errorCount: error,
interceptedCount: intercepted, processingCount: processing,
avgResponseTime: timeCount > 0 ? Math.round(totalTime / timeCount) : 0,
avgTTFT: ttftCount > 0 ? Math.round(totalTTFT / ttftCount) : 0,
totalLogEntries: logEntries.length,
};
}
// ==================== 核心 API ====================
export function createRequestLogger(opts: {
method: string;
path: string;
model: string;
stream: boolean;
hasTools: boolean;
toolCount: number;
messageCount: number;
apiFormat?: 'anthropic' | 'openai' | 'responses';
systemPromptLength?: number;
}): RequestLogger {
const requestId = shortId();
const summary: RequestSummary = {
requestId, startTime: Date.now(),
method: opts.method, path: opts.path, model: opts.model,
stream: opts.stream,
apiFormat: opts.apiFormat || (opts.path.includes('chat/completions') ? 'openai' :
opts.path.includes('responses') ? 'responses' : 'anthropic'),
hasTools: opts.hasTools, toolCount: opts.toolCount,
messageCount: opts.messageCount,
status: 'processing', responseChars: 0,
retryCount: 0, continuationCount: 0, toolCallsDetected: 0,
phaseTimings: [], thinkingChars: 0,
systemPromptLength: opts.systemPromptLength || 0,
};
const payload: RequestPayload = {};
requestSummaries.set(requestId, summary);
requestPayloads.set(requestId, payload);
requestOrder.push(requestId);
while (requestOrder.length > MAX_REQUESTS) {
const oldId = requestOrder.shift()!;
requestSummaries.delete(oldId);
requestPayloads.delete(oldId);
}
const toolInfo = opts.hasTools ? ` tools=${opts.toolCount}` : '';
const fmtTag = summary.apiFormat === 'openai' ? ' [OAI]' : summary.apiFormat === 'responses' ? ' [RSP]' : '';
console.log(`\x1b[36m⟶\x1b[0m [${requestId}] ${opts.method} ${opts.path}${fmtTag} | model=${opts.model} stream=${opts.stream}${toolInfo} msgs=${opts.messageCount}`);
return new RequestLogger(requestId, summary, payload);
}
export function getAllLogs(opts?: { requestId?: string; level?: LogLevel; source?: LogSource; limit?: number; since?: number }): LogEntry[] {
let result = logEntries;
if (opts?.requestId) result = result.filter(e => e.requestId === opts.requestId);
if (opts?.level) {
const levels: Record<LogLevel, number> = { debug: 0, info: 1, warn: 2, error: 3 };
const minLevel = levels[opts.level];
result = result.filter(e => levels[e.level] >= minLevel);
}
if (opts?.source) result = result.filter(e => e.source === opts.source);
if (opts?.since) result = result.filter(e => e.timestamp > opts!.since!);
if (opts?.limit) result = result.slice(-opts.limit);
return result;
}
export function getRequestSummaries(limit?: number): RequestSummary[] {
const ids = limit ? requestOrder.slice(-limit) : requestOrder;
return ids.map(id => requestSummaries.get(id)!).filter(Boolean).reverse();
}
/** 获取请求的完整 payload 数据 */
export function getRequestPayload(requestId: string): RequestPayload | undefined {
return requestPayloads.get(requestId);
}
export function subscribeToLogs(listener: (entry: LogEntry) => void): () => void {
logEmitter.on('log', listener);
return () => logEmitter.off('log', listener);
}
export function subscribeToSummaries(listener: (summary: RequestSummary) => void): () => void {
logEmitter.on('summary', listener);
return () => logEmitter.off('summary', listener);
}
function addEntry(entry: LogEntry): void {
logEntries.push(entry);
while (logEntries.length > MAX_ENTRIES) logEntries.shift();
logEmitter.emit('log', entry);
}
// ==================== RequestLogger ====================
export class RequestLogger {
readonly requestId: string;
private summary: RequestSummary;
private payload: RequestPayload;
private activePhase: PhaseTiming | null = null;
constructor(requestId: string, summary: RequestSummary, payload: RequestPayload) {
this.requestId = requestId;
this.summary = summary;
this.payload = payload;
}
private log(level: LogLevel, source: LogSource, phase: LogPhase, message: string, details?: unknown): void {
addEntry({
id: `log_${++logCounter}`,
requestId: this.requestId,
timestamp: Date.now(),
level, source, phase, message, details,
duration: Date.now() - this.summary.startTime,
});
}
// ---- 阶段追踪 ----
startPhase(phase: LogPhase, label: string): void {
if (this.activePhase && !this.activePhase.endTime) {
this.activePhase.endTime = Date.now();
this.activePhase.duration = this.activePhase.endTime - this.activePhase.startTime;
}
const t: PhaseTiming = { phase, label, startTime: Date.now() };
this.activePhase = t;
this.summary.phaseTimings.push(t);
}
endPhase(): void {
if (this.activePhase && !this.activePhase.endTime) {
this.activePhase.endTime = Date.now();
this.activePhase.duration = this.activePhase.endTime - this.activePhase.startTime;
}
}
// ---- 便捷方法 ----
debug(source: LogSource, phase: LogPhase, message: string, details?: unknown): void { this.log('debug', source, phase, message, details); }
info(source: LogSource, phase: LogPhase, message: string, details?: unknown): void { this.log('info', source, phase, message, details); }
warn(source: LogSource, phase: LogPhase, message: string, details?: unknown): void {
this.log('warn', source, phase, message, details);
console.log(`\x1b[33m⚠\x1b[0m [${this.requestId}] ${message}`);
}
error(source: LogSource, phase: LogPhase, message: string, details?: unknown): void {
this.log('error', source, phase, message, details);
console.error(`\x1b[31m✗\x1b[0m [${this.requestId}] ${message}`);
}
// ---- 特殊事件 ----
recordTTFT(): void { this.summary.ttft = Date.now() - this.summary.startTime; }
recordCursorApiTime(startTime: number): void { this.summary.cursorApiTime = Date.now() - startTime; }
// ---- 全量数据记录 ----
/** 记录原始请求(包含 messages, system, tools 等) */
recordOriginalRequest(body: any): void {
// system prompt
if (typeof body.system === 'string') {
this.payload.systemPrompt = body.system;
} else if (Array.isArray(body.system)) {
this.payload.systemPrompt = body.system.map((b: any) => b.text || '').join('\n');
}
// messages 摘要 + 完整存储
if (Array.isArray(body.messages)) {
const MAX_MSG = 100000; // 单条消息最大存储 100K
this.payload.messages = body.messages.map((m: any) => {
let fullContent = '';
let contentLength = 0;
let hasImages = false;
if (typeof m.content === 'string') {
fullContent = m.content.length > MAX_MSG ? m.content.substring(0, MAX_MSG) + '\n... [截断]' : m.content;
contentLength = m.content.length;
} else if (Array.isArray(m.content)) {
const textParts = m.content.filter((c: any) => c.type === 'text');
const imageParts = m.content.filter((c: any) => c.type === 'image' || c.type === 'image_url');
hasImages = imageParts.length > 0;
const text = textParts.map((c: any) => c.text || '').join('\n');
fullContent = text.length > MAX_MSG ? text.substring(0, MAX_MSG) + '\n... [截断]' : text;
contentLength = text.length;
if (hasImages) fullContent += `\n[+${imageParts.length} images]`;
}
return { role: m.role, contentPreview: fullContent, contentLength, hasImages };
});
}
// tools
if (Array.isArray(body.tools)) {
this.payload.tools = body.tools.map((t: any) => ({
name: t.name || t.function?.name || 'unknown',
description: (t.description || t.function?.description || '').substring(0, 200),
}));
}
// 存全量 (去掉 base64 图片数据避免内存爆炸)
this.payload.originalRequest = this.sanitizeForStorage(body);
}
/** 记录转换后的 Cursor 请求 */
recordCursorRequest(cursorReq: any): void {
if (Array.isArray(cursorReq.messages)) {
const MAX_MSG = 100000;
this.payload.cursorMessages = cursorReq.messages.map((m: any) => {
// Cursor 消息用 parts 而不是 content
let text = '';
if (m.parts && Array.isArray(m.parts)) {
text = m.parts.map((p: any) => p.text || '').join('\n');
} else if (typeof m.content === 'string') {
text = m.content;
} else if (m.content) {
text = JSON.stringify(m.content);
}
const fullContent = text.length > MAX_MSG ? text.substring(0, MAX_MSG) + '\n... [截断]' : text;
return {
role: m.role,
contentPreview: fullContent,
contentLength: text.length,
};
});
}
// 存储不含完整消息体的 cursor 请求元信息
this.payload.cursorRequest = {
model: cursorReq.model,
messageCount: cursorReq.messages?.length,
totalChars: cursorReq.messages?.reduce((sum: number, m: any) => {
if (m.parts && Array.isArray(m.parts)) {
return sum + m.parts.reduce((s: number, p: any) => s + (p.text?.length || 0), 0);
}
const text = typeof m.content === 'string' ? m.content : JSON.stringify(m.content || '');
return sum + text.length;
}, 0),
};
}
/** 记录模型原始响应 */
recordRawResponse(text: string): void {
this.payload.rawResponse = text;
}
/** 记录最终响应 */
recordFinalResponse(text: string): void {
this.payload.finalResponse = text;
}
/** 记录 thinking 内容 */
recordThinking(content: string): void {
this.payload.thinkingContent = content;
this.summary.thinkingChars = content.length;
}
/** 记录工具调用 */
recordToolCalls(calls: unknown[]): void {
this.payload.toolCalls = calls;
}
/** 记录重试响应 */
recordRetryResponse(attempt: number, response: string, reason: string): void {
if (!this.payload.retryResponses) this.payload.retryResponses = [];
this.payload.retryResponses.push({ attempt, response, reason });
}
/** 记录续写响应 */
recordContinuationResponse(index: number, response: string, dedupedLength: number): void {
if (!this.payload.continuationResponses) this.payload.continuationResponses = [];
this.payload.continuationResponses.push({ index, response: response.substring(0, 2000), dedupedLength });
}
/** 去除 base64 图片数据以节省内存 */
private sanitizeForStorage(obj: any): any {
if (!obj || typeof obj !== 'object') return obj;
if (Array.isArray(obj)) return obj.map(item => this.sanitizeForStorage(item));
const result: any = {};
for (const [key, value] of Object.entries(obj)) {
if (key === 'data' && typeof value === 'string' && (value as string).length > 1000) {
result[key] = `[base64 data: ${(value as string).length} chars]`;
} else if (key === 'source' && typeof value === 'object' && (value as any)?.type === 'base64') {
result[key] = { type: 'base64', media_type: (value as any).media_type, data: `[${((value as any).data?.length || 0)} chars]` };
} else if (typeof value === 'object') {
result[key] = this.sanitizeForStorage(value);
} else {
result[key] = value;
}
}
return result;
}
// ---- 摘要更新 ----
updateSummary(updates: Partial<RequestSummary>): void {
Object.assign(this.summary, updates);
logEmitter.emit('summary', this.summary);
}
complete(responseChars: number, stopReason?: string): void {
this.endPhase();
const duration = Date.now() - this.summary.startTime;
this.summary.endTime = Date.now();
this.summary.status = 'success';
this.summary.responseChars = responseChars;
this.summary.stopReason = stopReason;
this.log('info', 'System', 'complete', `完成 (${duration}ms, ${responseChars} chars, stop=${stopReason})`);
logEmitter.emit('summary', this.summary);
const retryInfo = this.summary.retryCount > 0 ? ` retry=${this.summary.retryCount}` : '';
const contInfo = this.summary.continuationCount > 0 ? ` cont=${this.summary.continuationCount}` : '';
const toolInfo = this.summary.toolCallsDetected > 0 ? ` tools_called=${this.summary.toolCallsDetected}` : '';
const ttftInfo = this.summary.ttft ? ` ttft=${this.summary.ttft}ms` : '';
console.log(`\x1b[32m⟵\x1b[0m [${this.requestId}] ${duration}ms | ${responseChars} chars | stop=${stopReason || 'end_turn'}${ttftInfo}${retryInfo}${contInfo}${toolInfo}`);
}
intercepted(reason: string): void {
this.summary.status = 'intercepted';
this.summary.endTime = Date.now();
this.log('info', 'System', 'intercept', reason);
logEmitter.emit('summary', this.summary);
console.log(`\x1b[35m⊘\x1b[0m [${this.requestId}] 拦截: ${reason}`);
}
fail(error: string): void {
this.endPhase();
this.summary.status = 'error';
this.summary.endTime = Date.now();
this.summary.error = error;
this.log('error', 'System', 'error', error);
logEmitter.emit('summary', this.summary);
}
}

View File

@@ -27,6 +27,7 @@ import type {
import { convertToCursorRequest, parseToolCalls, hasToolCalls } from './converter.js';
import { sendCursorRequest, sendCursorRequestFull } from './cursor-client.js';
import { getConfig } from './config.js';
import { createRequestLogger } from './logger.js';
import {
isRefusal,
sanitizeResponse,
@@ -278,17 +279,37 @@ function extractOpenAIContent(msg: OpenAIMessage): string {
export async function handleOpenAIChatCompletions(req: Request, res: Response): Promise<void> {
const body = req.body as OpenAIChatRequest;
console.log(`[OpenAI] 收到请求: model=${body.model}, messages=${body.messages?.length}, stream=${body.stream}, tools=${body.tools?.length ?? 0}`);
const log = createRequestLogger({
method: req.method,
path: req.path,
model: body.model,
stream: !!body.stream,
hasTools: (body.tools?.length ?? 0) > 0,
toolCount: body.tools?.length ?? 0,
messageCount: body.messages?.length ?? 0,
apiFormat: 'openai',
});
log.startPhase('receive', '接收请求');
log.recordOriginalRequest(body);
log.info('OpenAI', 'receive', `收到 OpenAI Chat 请求`, {
model: body.model,
messageCount: body.messages?.length,
stream: body.stream,
toolCount: body.tools?.length ?? 0,
});
try {
// Step 1: OpenAI → Anthropic 格式
log.startPhase('convert', '格式转换 (OpenAI→Anthropic)');
const anthropicReq = convertToAnthropicRequest(body);
log.endPhase();
// 注意:图片预处理已移入 convertToCursorRequest → preprocessImages() 统一处理
// Step 1.6: 身份探针拦截(复用 Anthropic handler 的逻辑)
if (isIdentityProbe(anthropicReq)) {
console.log(`[OpenAI] 拦截到身份探针,返回模拟响应`);
log.intercepted('身份探针拦截 (OpenAI)');
const mockText = "I am Claude, an advanced AI programming assistant created by Anthropic. I am ready to help you write code, debug, and answer your technical questions. Please let me know what we should work on!";
if (body.stream) {
return handleOpenAIMockStream(res, body, mockText);
@@ -307,7 +328,7 @@ export async function handleOpenAIChatCompletions(req: Request, res: Response):
}
} catch (err: unknown) {
const message = err instanceof Error ? err.message : String(err);
console.error(`[OpenAI] 请求处理失败:`, message);
log.fail(message);
res.status(500).json({
error: {
message,
@@ -403,7 +424,7 @@ async function handleOpenAIStream(
try {
await executeStream();
console.log(`[OpenAI] 原始响应 (${fullResponse.length} chars, tools=${hasTools}): ${fullResponse.substring(0, 200)}${fullResponse.length > 200 ? '...' : ''}`);
// 日志记录在详细日志中 (Web UI 可见)
// ★ Thinking 提取(在拒绝检测之前)
const thinkingEnabled = anthropicReq.thinking?.type === 'enabled';
@@ -415,7 +436,7 @@ async function handleOpenAIStream(
reasoningContent = thinkingMatch.map(m => m.replace(/<\/?thinking>/g, '').trim()).join('\n\n');
}
fullResponse = fullResponse.replace(/<thinking>[\s\S]*?<\/thinking>\s*/g, '').trim();
console.log(`[OpenAI] 流式:剥离 thinking, 剩余 ${fullResponse.length} chars${thinkingEnabled ? ', 将发送 reasoning_content' : ''}`);
// thinking 剥离记录在详细日志中
}
}
@@ -428,7 +449,7 @@ async function handleOpenAIStream(
while (shouldRetryRefusal() && retryCount < MAX_REFUSAL_RETRIES) {
retryCount++;
console.log(`[OpenAI] 检测到拒绝(第${retryCount}次),自动重试...原始: ${fullResponse.substring(0, 100)}`);
// 重试记录在详细日志中
const retryBody = buildRetryRequest(anthropicReq, retryCount - 1);
activeCursorReq = await convertToCursorRequest(retryBody);
await executeStream();
@@ -436,14 +457,14 @@ async function handleOpenAIStream(
if (shouldRetryRefusal()) {
if (!hasTools) {
if (isToolCapabilityQuestion(anthropicReq)) {
console.log(`[OpenAI] 工具能力询问被拒绝,返回 Claude 能力描述`);
// 记录在详细日志
fullResponse = CLAUDE_TOOLS_RESPONSE;
} else {
console.log(`[OpenAI] 重试${MAX_REFUSAL_RETRIES}次后仍被拒绝,返回 Claude 身份回复`);
// 记录在详细日志
fullResponse = CLAUDE_IDENTITY_RESPONSE;
}
} else {
console.log(`[OpenAI] 工具模式下拒绝且无工具调用,引导模型输出`);
// 记录在详细日志
fullResponse = 'I understand the request. Let me analyze the information and proceed with the appropriate action.';
}
}
@@ -451,7 +472,7 @@ async function handleOpenAIStream(
// 极短响应重试
if (hasTools && fullResponse.trim().length < 10 && retryCount < MAX_REFUSAL_RETRIES) {
retryCount++;
console.log(`[OpenAI] 响应过短 (${fullResponse.length} chars),重试第${retryCount}`);
// 记录在详细日志
activeCursorReq = await convertToCursorRequest(anthropicReq);
await executeStream();
}
@@ -607,7 +628,7 @@ async function handleOpenAINonStream(
let fullText = await sendCursorRequestFull(cursorReq);
const hasTools = (body.tools?.length ?? 0) > 0;
console.log(`[OpenAI] 非流式原始响应 (${fullText.length} chars, tools=${hasTools}): ${fullText.substring(0, 300)}${fullText.length > 300 ? '...' : ''}`);
// 日志记录在详细日志中
// ★ Thinking 提取必须在拒绝检测之前 — 否则 thinking 内容中的关键词会触发 isRefusal 误判
const thinkingEnabled = anthropicReq.thinking?.type === 'enabled';
@@ -619,7 +640,7 @@ async function handleOpenAINonStream(
reasoningContent = thinkingMatch.map(m => m.replace(/<\/?thinking>/g, '').trim()).join('\n\n');
}
const stripped = fullText.replace(/<thinking>[\s\S]*?<\/thinking>\s*/g, '').trim();
console.log(`[OpenAI] 剥离 thinking: ${fullText.length - stripped.length} chars, 剩余 ${stripped.length} chars${thinkingEnabled ? ', 将返回 reasoning_content' : ''}`);
// thinking 剥离记录
fullText = stripped;
}
}
@@ -629,7 +650,7 @@ async function handleOpenAINonStream(
if (shouldRetry()) {
for (let attempt = 0; attempt < MAX_REFUSAL_RETRIES; attempt++) {
console.log(`[OpenAI] 非流式:检测到拒绝(第${attempt + 1}次重试)...原始: ${fullText.substring(0, 100)}`);
// 重试记录
const retryBody = buildRetryRequest(anthropicReq, attempt);
const retryCursorReq = await convertToCursorRequest(retryBody);
fullText = await sendCursorRequestFull(retryCursorReq);
@@ -641,13 +662,13 @@ async function handleOpenAINonStream(
}
if (shouldRetry()) {
if (hasTools) {
console.log(`[OpenAI] 非流式:工具模式下拒绝,引导模型输出`);
// 记录在详细日志
fullText = 'I understand the request. Let me analyze the information and proceed with the appropriate action.';
} else if (isToolCapabilityQuestion(anthropicReq)) {
console.log(`[OpenAI] 非流式:工具能力询问被拒绝,返回 Claude 能力描述`);
// 记录在详细日志
fullText = CLAUDE_TOOLS_RESPONSE;
} else {
console.log(`[OpenAI] 非流式:重试${MAX_REFUSAL_RETRIES}次后仍被拒绝,返回 Claude 身份回复`);
// 记录在详细日志
fullText = CLAUDE_IDENTITY_RESPONSE;
}
}
@@ -665,7 +686,7 @@ async function handleOpenAINonStream(
// 清洗拒绝文本
let cleanText = parsed.cleanText;
if (isRefusal(cleanText)) {
console.log(`[OpenAI] 抑制工具模式下的拒绝文本: ${cleanText.substring(0, 100)}...`);
// 记录在详细日志
cleanText = '';
}
content = sanitizeResponse(cleanText) || null;