// 连到已开着的酒馆页面:清历史 + 发一句话(走酒馆自己的 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)); } // 页面重载后可能没选角色:先把探针卡选上 if (window.SillyTavern.getContext().name2 !== "ProbeCard") { await window.SillyTavern.getContext().getCharacters(); const list = window.SillyTavern.getContext().characters || []; const idx = list.findIndex((c) => c.name === "ProbeCard"); const other = list.findIndex((c) => c.name !== "ProbeCard"); if (idx >= 0) { for (let t = 0; t < 40 && window.SillyTavern.getContext().name2 !== "ProbeCard"; t++) { if (t === 0 && other >= 0) await window.SillyTavern.getContext().selectCharacterById(other); await window.SillyTavern.getContext().selectCharacterById(idx); await new Promise((r) => setTimeout(r, 400)); } } } 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();