// 往"已经开着的"酒馆页面里再导入一个预设并选中(不动浏览器,不重启酒馆)。 // 用法: node attach-preset.mjs --preset "C:/Users/zed/Downloads/Reborn2.4.json" [--browser-port 8223] [--st-url 8123] import { existsSync, readFileSync } from "node:fs"; import path from "node:path"; import { CdpSession, evaluate, sleep } 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 presetFile = arg("preset", ""); const browserPort = Number(arg("browser-port", "8223")); const stHint = arg("st-url", "127.0.0.1:8123"); if (!presetFile || !existsSync(presetFile)) throw new Error("缺少 --preset"); const presetName = path.basename(presetFile, ".json"); const presetText = readFileSync(presetFile, "utf8"); 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("没找到酒馆页面,现有 target: " + targetInfos.map((t) => t.type + " " + t.url).join(" | ")); const { sessionId } = await cdp.send("Target.attachToTarget", { targetId: target.targetId, flatten: true }); await cdp.send("Runtime.enable", {}, sessionId); const page = { cdp, sessionId }; await evaluate(page, `(async () => { const input = document.getElementById("openai_preset_import_file"); const file = new File([${JSON.stringify(presetText)}], ${JSON.stringify(presetName + ".json")}, { type: "application/json" }); const dt = new DataTransfer(); dt.items.add(file); input.files = dt.files; window.jQuery(input).trigger("input"); return true; })()`); console.log("[attach] 已把 " + presetName + " 丢进导入框,等落盘…"); for (let i = 0; i < 120; i++) { const s = await evaluate(page, `(() => { const ctx = window.SillyTavern.getContext(); const names = ctx.getPresetManager("openai").getPresetList().preset_names || {}; const popup = document.querySelector(".popup-button-cancel"); if (popup) popup.click(); return { has: Object.keys(names).includes(${JSON.stringify(presetName)}), keys: Object.keys(names) }; })()`); if (s.has) { const applied = await evaluate(page, `(async () => { const ctx = window.SillyTavern.getContext(); const names = ctx.getPresetManager("openai").getPresetList().preset_names || {}; const idx = names[${JSON.stringify(presetName)}]; const jq = window.jQuery; ctx.chatCompletionSettings.bind_preset_to_connection = false; jq("#settings_preset_openai").val(String(idx)).trigger("change"); const s = ctx.chatCompletionSettings; return { preset: s.preset_settings_openai, temp: s.temp_openai, maxTokens: s.openai_max_tokens, functionCalling: s.function_calling, squash: s.squash_system_messages, charName: window.SillyTavern.getContext().name2, chatLen: window.SillyTavern.getContext().chat.length }; })()`); console.log("[attach] 已导入并选中: " + JSON.stringify(applied)); cdp.close(); process.exit(0); } await sleep(500); } console.log("[attach] 超时:预设没出现在列表里"); cdp.close(); process.exit(1);