92 lines
4.4 KiB
JavaScript
92 lines
4.4 KiB
JavaScript
import { createReadStream } from 'node:fs';
|
||
import { readdir } from 'node:fs/promises';
|
||
import path from 'node:path';
|
||
import { createInterface } from 'node:readline';
|
||
|
||
const [events, sessions] = process.argv.slice(2);
|
||
if (!events || !sessions) {
|
||
console.error('用法 node scripts/audit-xiaoyi-tool-events.mjs <events.jsonl> <sessions目录>');
|
||
process.exit(2);
|
||
}
|
||
|
||
async function* records(file) {
|
||
const input = createInterface({ input: createReadStream(file), crlfDelay: Infinity });
|
||
let line = 0;
|
||
for await (const text of input) {
|
||
line++;
|
||
if (!text.trim()) continue;
|
||
try {
|
||
yield { line, data: JSON.parse(text) };
|
||
} catch (error) {
|
||
console.error(`${file}:${line} ${error.name},该行未计入`);
|
||
}
|
||
}
|
||
}
|
||
|
||
const results = new Map();
|
||
const sizes = new Map();
|
||
for (const file of await readdir(sessions)) {
|
||
if (!file.endsWith('.jsonl')) continue;
|
||
for await (const { line, data } of records(path.join(sessions, file))) {
|
||
if (data.type !== 'message' || data.message?.role !== 'toolResult') continue;
|
||
results.set(data.message.toolCallId, { file, line, ...data.message });
|
||
const { toolName, content, toolCallId } = data.message;
|
||
const chars = content.filter(block => block.type === 'text').reduce((total, block) => total + block.text.length, 0);
|
||
if (chars > (sizes.get(toolName)?.chars ?? -1)) sizes.set(toolName,{chars,toolCallId,file,line});
|
||
}
|
||
}
|
||
|
||
const rows = [];
|
||
const terminalReceipts = new Map();
|
||
const historicalReasons = [
|
||
[/修订已变化/u, 'revision_conflict'],
|
||
[/nothing to transform/u, 'empty_transform_target'],
|
||
[/characters of JSON, more than the limit/u, 'result_too_large'],
|
||
[/路径在本项目工作目录之外/u, 'workspace_path'],
|
||
[/当前花色没有|同款原图编号无效|须指定一个候选号/u, 'image_source'],
|
||
[/expectedHash .*不一致|expectedRevision .*不一致/u, 'request_receipt_mismatch'],
|
||
[/生图请求编号无效/u, 'unknown_image_request'],
|
||
[/本机记录无法读取/u, 'record_unreadable'],
|
||
[/Validation failed for tool/u, 'tool_argument_schema'],
|
||
[/不在本.*清单中|三组问题列表均为空/u, 'review_evidence'],
|
||
];
|
||
for await (const { line, data } of records(events)) {
|
||
if (!['tool_execution_end', 'canvas_receipt'].includes(data.type)) continue;
|
||
const original = data.requestId || data.operationId;
|
||
if (data.type === 'canvas_receipt' && original && ['complete','applied','failed','cancelled'].includes(data.status)) {
|
||
terminalReceipts.set(original,{status:data.status,line});
|
||
}
|
||
const failed = data.isError || data.status === 'failed';
|
||
if (!failed && !['pending', 'unknown', 'waiting_save'].includes(data.status)) continue;
|
||
const session = data.type === 'tool_execution_end' ? results.get(data.toolCallId) : undefined;
|
||
const failure = session?.details?.failure;
|
||
const message = data.message || data.error || failure?.message ||
|
||
(failed ? session?.content?.findLast(item => item.type === 'text')?.text : '');
|
||
const category = data.failureCategory || failure?.category ||
|
||
(failed ? historicalReasons.find(([pattern]) => pattern.test(message ?? ''))?.[1] ?? '历史原因未结构化' : data.status);
|
||
rows.push({
|
||
source: data.type, status: data.status ?? 'failed', category,
|
||
tool: data.toolName ?? data.action, line, id: data.toolCallId ?? data.operationId, requestId: data.requestId,
|
||
operationPhase: data.operationPhase, imageAction: data.imageAction, command: data.command,
|
||
session: session && `${session.file}:${session.line}`,
|
||
message: message?.replace(/\s+/gu, ' ').slice(0, 1000) || (failed ? '历史事件未记录失败原因' : ''),
|
||
});
|
||
}
|
||
for (const row of rows) {
|
||
if (!['pending','unknown','waiting_save'].includes(row.status)) continue;
|
||
const terminal = terminalReceipts.get(row.requestId ?? row.id);
|
||
if (terminal?.line > row.line) row.laterReceipt = terminal;
|
||
}
|
||
|
||
const totals = new Map();
|
||
for (const { source, status, tool, category } of rows) {
|
||
const key = [source, status, tool, category].join('\t');
|
||
totals.set(key, (totals.get(key) ?? 0) + 1);
|
||
}
|
||
console.log('事件类型\t状态\t工具\t原因分类\t次数');
|
||
for (const [key, count] of totals) console.log(`${key}\t${count}`);
|
||
console.log('\n会话中各工具最大文本回执(JSONL)');
|
||
for (const [tool, size] of [...sizes].sort((a,b)=>b[1].chars-a[1].chars)) console.log(JSON.stringify({tool,...size}));
|
||
console.log('\n逐条证据(JSONL)');
|
||
for (const row of rows) console.log(JSON.stringify(row));
|