31 lines
1.4 KiB
JavaScript
31 lines
1.4 KiB
JavaScript
// 把一侧请求里的每条消息,回溯到预设的哪个条目,并判断是「原样 / 前缀截断 / 改写」
|
|
// 用法: node match-msg.mjs <preset.json> <capture.json>
|
|
import { readFileSync } from "node:fs";
|
|
const preset = JSON.parse(readFileSync(process.argv[2], "utf8"));
|
|
const outer = JSON.parse(readFileSync(process.argv[3], "utf8"));
|
|
const body = JSON.parse(outer.body);
|
|
const prompts = (preset.prompts || []).filter((p) => String(p.content ?? "").trim());
|
|
const norm = (s) => String(s ?? "").replace(/\s+/g, " ").trim();
|
|
|
|
body.messages.forEach((m, i) => {
|
|
const text = norm(m.content);
|
|
const head = text.slice(0, 80);
|
|
let best = null;
|
|
for (const p of prompts) {
|
|
const c = norm(p.content);
|
|
const p80 = c.slice(0, 80);
|
|
let score = 0;
|
|
for (let k = 0; k < Math.min(p80.length, head.length); k++) {
|
|
if (p80[k] === head[k]) score++;
|
|
else break;
|
|
}
|
|
if (!best || score > best.score) best = { name: p.name || p.identifier, len: c.length, score };
|
|
}
|
|
const ratio = best && best.len ? (text.length / best.len).toFixed(2) : "-";
|
|
console.log(
|
|
"#" + i + " [" + m.role + "] " + text.length + " 字 ← " + (best ? best.name : "?") + " (预设 " + (best ? best.len : 0) + " 字, 前80字重合 " + (best ? best.score : 0) + ", 长度比 " + ratio + ")",
|
|
);
|
|
console.log(" 头: " + head.slice(0, 70));
|
|
console.log(" 尾: " + text.slice(-70));
|
|
});
|