加一套抓真实请求做预设对比的工具,并把三份预设的机制差异记进 README。
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
// 连到已开着的酒馆页面:清历史 + 发一句话(走酒馆自己的 Generate)。
|
||||
// 用法: node attach-send.mjs --preset 名字 --text 你好 [--browser-port 8223] [--st-url 127.0.0.1:8123]
|
||||
import { CdpSession, evaluate } from "./cdp.mjs";
|
||||
|
||||
const argv = process.argv.slice(2);
|
||||
const arg = (name, dflt) => {
|
||||
const i = argv.indexOf("--" + name);
|
||||
return i >= 0 ? String(argv[i + 1]) : dflt;
|
||||
};
|
||||
const browserPort = Number(arg("browser-port", "8223"));
|
||||
const stHint = arg("st-url", "127.0.0.1:8123");
|
||||
const presetName = arg("preset", "");
|
||||
const text = arg("text", "你好");
|
||||
|
||||
const version = await (await fetch("http://127.0.0.1:" + browserPort + "/json/version")).json();
|
||||
const cdp = await CdpSession.connect(version.webSocketDebuggerUrl, "browser");
|
||||
const { targetInfos } = await cdp.send("Target.getTargets");
|
||||
const target = targetInfos.find((t) => t.type === "page" && String(t.url).includes(stHint));
|
||||
if (!target) throw new Error("没找到酒馆页面");
|
||||
const { sessionId } = await cdp.send("Target.attachToTarget", { targetId: target.targetId, flatten: true });
|
||||
await cdp.send("Runtime.enable", {}, sessionId);
|
||||
const page = { cdp, sessionId };
|
||||
|
||||
const out = await evaluate(page, `(async () => {
|
||||
const jq = window.jQuery;
|
||||
const names = window.SillyTavern.getContext().getPresetManager("openai").getPresetList().preset_names || {};
|
||||
const want = ${JSON.stringify(presetName)};
|
||||
if (want && names[want] !== undefined) {
|
||||
jq("#settings_preset_openai").val(String(names[want])).trigger("change");
|
||||
await new Promise((r) => setTimeout(r, 900));
|
||||
}
|
||||
await window.SillyTavern.getContext().clearChat({ clearData: true });
|
||||
const before = window.SillyTavern.getContext().chatCompletionSettings;
|
||||
const state = { preset: before.preset_settings_openai, temp: before.temp_openai, maxTokens: before.openai_max_tokens, functionCalling: before.function_calling, squash: before.squash_system_messages };
|
||||
jq("#send_textarea").val(${JSON.stringify(text)}).trigger("input");
|
||||
let error = null;
|
||||
window.SillyTavern.getContext().generate("normal").catch((e) => { error = String((e && e.stack) || e); window.__genErr = error; });
|
||||
for (let i = 0; i < 120; i++) {
|
||||
await new Promise((r) => setTimeout(r, 500));
|
||||
const f = window.SillyTavern.getContext();
|
||||
if (f.chat.length >= 2 || error) break;
|
||||
}
|
||||
const f = window.SillyTavern.getContext();
|
||||
return { state, chatLen: f.chat.length, error: error || window.__genErr || null, last: f.chat.length ? String(f.chat[f.chat.length - 1].mes).slice(0, 60) : "" };
|
||||
})()`);
|
||||
console.log("[send] " + JSON.stringify(out, null, 1));
|
||||
cdp.close();
|
||||
Reference in New Issue
Block a user