#!/usr/bin/env node /** * cli.ts —— dobostitch 命令行入口 * * 子命令: * start 起代理,打印首屏体检卡与接入指引 * status 读 data/handoff,列出最近会话 * resume 打印指定会话(或最近会话)的续接摘要 * doctor 自检:Node 版本 / 端口占用 / 路由表 / 交接单目录可写性 * * 说明:本文件只做命令行编排,业务能力都在 usage / handoff / card / server 四个模块里。 */ import fs from "node:fs"; import net from "node:net"; import path from "node:path"; import { findTargets, makeContext, renderGenericSnippet, resolveAttachPort, TARGETS, type AttachResult } from "../attach.js"; import { getHost, loadExternalHosts, renderGuide, listHosts } from "../hosts/registry.js"; import { locateHostFiles, hostAttached } from "../hosts/engine.js"; import { checkHost, summarizeHealth } from "../hosts/health.js"; import { HOST_VERIFICATION, isAutoAttachEligible } from "../hosts/verification.js"; import { probeProxyPort, renderProxyProbe } from "../core/proxyProbe.js"; import { formatGainReport, judgeGain, loadSamples, DEFAULT_MIN_SAMPLES as GAIN_MIN_SAMPLES, DEFAULT_COVERAGE_FLOOR as GAIN_COVERAGE_FLOOR } from "../core/gain.js"; import { resolveLogDir } from "../utils/logDir.js"; import { disable as disableAutostart, enable as enableAutostart, statusTransport } from "../autostart.js"; import { readLastPort, writeLastPort } from "../state.js"; import { renderHealthCard } from "./card.js"; import { defaultHandoffDir, handoffDirNotice, handoffDirWarning, listHandoffs, loadHandoff, renderResumeBrief } from "./handoff.js"; import { DEFAULT_CONTEXT_THRESHOLD_TOKENS, DEFAULT_PORT, resolveRoutes, startProxy } from "./server.js"; import { checkNodeVersion } from "../utils/runtime.js"; import { ensureAgentRules as ensureAgentRulesAt } from "../rules.js"; import type { HealthCard, RewriteMode, RouteConfig } from "./types.js"; const USAGE = `dobostitch —— DoboCache 本地代理层 用法: dobostitch start [--port 27880] [--token <本机token>] [--require-token] [--rewrite ] [--inject-usage ] dobostitch status dobostitch resume [sessionKey] dobostitch doctor 改写档位(默认 stabilize): off 不改请求体,只转发 observe 只看不改,用来验证"原样直通时前缀到底稳不稳" stabilize 规范化 key 序 + 重排 tools,稳定前缀(零语义风险) anchor stabilize + 时间字面量锚定(实验能力,等价 --anchor-time) 流式 usage 注入(默认 on): openai 方言且 stream=true 的请求,自动补 stream_options.include_usage=true, 让流末尾带回 usage——不补的话流式请求基本拿不到 usage,命中率只能写"未判定"。 上游若因该字段回 4xx 且正文说"不认识这个字段",自动去掉该字段重试一次。 客户端自己写了 include_usage 的一律不改;off 用于复现"上游确实不认这个字段"的现场。 接入方式(改一次 baseURL 即可,无需装证书、无需重启换配置): DeepSeek http://127.0.0.1:27880/deepseek OpenAI http://127.0.0.1:27880/openai Anthropic http://127.0.0.1:27880/anthropic 全自动接入(不想手改配置就用这个): dobostitch setup [--dry-run] 一条命令装完,小白只需记这一条 dobostitch attach [--target auto|loomy|opencode|claude-code|generic|<底座id>] [--dry-run] [--revert] [--no-autostart] [--allow-unverified] dobostitch autostart [status|enable|disable] setup = attach(自动找客户端配置、备份、改 baseURL、写入自带拉起代理的 MCP 入口)+ 注册开机自启。 之后换任何底座都不用再写脚本:MCP 入口会自己把代理拉起来。 attach 的 --dry-run 只预览,--revert 原样退回;generic 只打印可粘贴片段,适合自研底座。 auto 只接入「已真机验证」的底座;新底座需 --allow-unverified 显式写入。 看底座清单与逐家填法(v0.7.0): dobostitch providers [list] 列出全部底座(档位 / 起始版本 / 验证状态 / 是否已探测到) dobostitch providers detect 只列本机真实装了的底座 dobostitch providers show 单个底座的配置文件位置与手填片段 dobostitch providers plan 预览会改哪几个文件(不写盘) dobostitch providers health [id] 体检已接入的底座(只读:条目在不在 / 配置能否解析 / 脚本还在不在) 看接入后到底有没有拿到缓存收益(v0.8.0): dobostitch gain [--dir <日志目录>] [--min-samples 20] [--coverage-floor 60] [--json] 只读本地白盒日志里厂商 usage 回传的命中率,给三态结论:观察到增益 / 没观察到明显增益 / 数据不足。 数据不够就明说不够,不硬给结论;不折算金额。 `; /** 改写档位默认值 */ const DEFAULT_REWRITE_MODE: RewriteMode = "stabilize"; /** 流式 usage 注入默认值:开 */ const DEFAULT_INJECT_USAGE = true; /** 校验并解析 --inject-usage (默认 on) */ function resolveInjectUsage(argv: string[]): { enabled: boolean; invalid?: string } { const raw = argValue(argv, "inject-usage"); if (raw === undefined) return { enabled: DEFAULT_INJECT_USAGE }; const text = raw.trim().toLowerCase(); if (text === "on" || text === "true" || text === "1") return { enabled: true }; if (text === "off" || text === "false" || text === "0") return { enabled: false }; return { enabled: DEFAULT_INJECT_USAGE, invalid: raw }; } /** 校验并解析 --rewrite / --anchor-time */ function resolveRewriteMode(argv: string[]): { mode: RewriteMode; invalid?: string } { if (argv.includes("--anchor-time")) return { mode: "anchor" }; const raw = argValue(argv, "rewrite"); if (raw === undefined) return { mode: DEFAULT_REWRITE_MODE }; if (raw === "off" || raw === "observe" || raw === "stabilize" || raw === "anchor") return { mode: raw }; return { mode: DEFAULT_REWRITE_MODE, invalid: raw }; } /** 取命令行参数值:--key value */ function argValue(argv: string[], key: string): string | undefined { const index = argv.indexOf(`--${key}`); if (index === -1) return undefined; const value = argv[index + 1]; return value && !value.startsWith("--") ? value : undefined; } /** 端口是否已被占用(尝试绑定,失败即视为占用) */ function isPortBusy(port: number, host = "127.0.0.1"): Promise { return new Promise((resolve) => { const probe = net.createServer(); probe.once("error", () => resolve(true)); probe.once("listening", () => probe.close(() => resolve(false))); probe.listen(port, host); }); } /** 交接单目录是否可写(建目录 + 写探针 + 删除探针) */ function checkHandoffDirWritable(dir: string): { ok: boolean; detail: string } { try { fs.mkdirSync(dir, { recursive: true }); const probe = path.join(dir, `.dobostitch-write-test-${process.pid}`); fs.writeFileSync(probe, "ok", "utf-8"); fs.rmSync(probe, { force: true }); return { ok: true, detail: dir }; } catch (err) { const code = (err as NodeJS.ErrnoException)?.code ?? "UNKNOWN"; return { ok: false, detail: `${dir}(${code})` }; } } /** 首屏体检卡:进程刚起、还没有真实请求时的占位版 */ function renderStartupCard(port: number, routes: RouteConfig[], threshold: number): string { const prefixes = routes.map((route) => route.prefix).join(" "); const firstPrefix = routes[0]?.prefix ?? "/"; const card: HealthCard = { requestIndex: 0, routePrefix: routes[0]?.prefix ?? "/", upstreamLabel: "待第一次请求", upstreamHost: `127.0.0.1:${port}`, vendor: "unknown", inputTokens: null, cachedTokens: null, hitRatio: null, judged: false, didWhat: `代理已就绪,等待第一次请求;可用前缀 ${prefixes};交接阈值 ${threshold} token`, // 横幅必须给出可直接复制的完整地址,避免对端把"端口/前缀"填错 entryUrl: `http://127.0.0.1:${port}${firstPrefix}`, }; return renderHealthCard(card); } async function commandStart(argv: string[]): Promise { const portArg = argValue(argv, "port"); const port = portArg ? Number(portArg) : DEFAULT_PORT; if (!Number.isInteger(port) || port <= 0 || port > 65535) { console.error(`[dobostitch] 端口不合法:${portArg}`); process.exitCode = 1; return; } const nodeCheck = checkNodeVersion(); if (!nodeCheck.ok) { console.error(nodeCheck.message); process.exitCode = 1; return; } const token = argValue(argv, "token"); const requireToken = argv.includes("--require-token"); const rewrite = resolveRewriteMode(argv); if (rewrite.invalid) { console.error(`[dobostitch] 未知改写档位:${rewrite.invalid}(可用 off/observe/stabilize/anchor),已用默认 stabilize。`); } const inject = resolveInjectUsage(argv); if (inject.invalid) { console.error(`[dobostitch] 未知 --inject-usage 取值:${inject.invalid}(可用 on/off),已用默认 on。`); } const thresholdEnv = Number(process.env.DOBOSTITCH_CONTEXT_THRESHOLD ?? ""); const threshold = Number.isFinite(thresholdEnv) && thresholdEnv > 0 ? thresholdEnv : DEFAULT_CONTEXT_THRESHOLD_TOKENS; const starting = await startProxy({ port, authToken: token, requireToken, contextThresholdTokens: threshold, rewriteMode: rewrite.mode, injectUsage: inject.enabled, }); const routes = resolveRoutes(); // 代理真正跑起来后落一次端口,其它入口(bundle / 自启 / attach)都以此为准 writeLastPort(starting.port); console.log(renderStartupCard(starting.port, routes, threshold)); console.log(""); if (starting.port !== port) { console.log(`[提示] 端口 ${port} 已被占用,已自动顺延到 ${starting.port}(请把下面的地址端口一并改掉)。`); } console.log(`现在把客户端 baseURL 设为 http://127.0.0.1:${starting.port}/<厂商前缀> 即可:`); for (const route of routes) { console.log(` ${route.label.padEnd(10, " ")} http://127.0.0.1:${starting.port}${route.prefix}`); } console.log(""); console.log(`改写档位:${rewrite.mode}`); console.log( inject.enabled ? "流式 usage 注入:on(openai 方言 + stream=true 时自动补 stream_options.include_usage)" : "流式 usage 注入:off(流式响应多半不带 usage,命中率会显示未判定)", ); console.log(`交接单目录:${defaultHandoffDir()}`); const dirNotice = handoffDirNotice(); if (dirNotice) console.log(`[提示] ${dirNotice}`); const dirWarning = handoffDirWarning(); if (dirWarning) console.log(`[警告] ${dirWarning}`); console.log("按 Ctrl+C 停止代理。"); const shutdown = () => { console.log("\n[dobostitch] 正在停止代理…"); void starting.close().finally(() => process.exit(0)); }; process.on("SIGINT", shutdown); process.on("SIGTERM", shutdown); // 保持进程常驻:server 自身已挂住事件循环,这里不再额外定时器 } /** * v0.8.0:增益判定命令。 * 只读本地白盒日志,输出三态结论;参数只影响门槛,不影响口径。 */ function commandGain(argv: string[]): void { const explicitDir = argValue(argv, "dir"); const asJson = argv.includes("--json"); const minSamplesRaw = argValue(argv, "min-samples"); const minSamples = minSamplesRaw === undefined ? GAIN_MIN_SAMPLES : Number.parseInt(minSamplesRaw, 10); if (!Number.isFinite(minSamples) || minSamples <= 0) { console.error(`[dobostitch] --min-samples 需要正整数,收到:${minSamplesRaw}`); process.exitCode = 1; return; } const floorRaw = argValue(argv, "coverage-floor"); const coverageFloor = floorRaw === undefined ? GAIN_COVERAGE_FLOOR : Number.parseFloat(floorRaw); if (!Number.isFinite(coverageFloor) || coverageFloor < 0 || coverageFloor > 100) { console.error(`[dobostitch] --coverage-floor 需要 0-100 的数字,收到:${floorRaw}`); process.exitCode = 1; return; } const resolved = resolveLogDir({ explicitDir }); for (const warning of resolved.warnings) console.error(`[dobostitch] ${warning}`); if (resolved.disabled) { console.error("[dobostitch] 日志目录不可用(没有可写的落点),无法做增益判定。"); process.exitCode = 1; return; } const loaded = loadSamples(resolved.dir); for (const warning of loaded.warnings) console.error(`[dobostitch] ${warning}`); const report = { ...judgeGain(loaded.samples, { minSamples, coverageFloor }), logFiles: loaded.logFiles }; if (asJson) { console.log(JSON.stringify({ logDir: resolved.dir, ...report }, null, 2)); return; } console.log(`日志目录:${resolved.dir}`); console.log(""); console.log(formatGainReport(report)); if (loaded.samples.length === 0) { console.log(""); console.log("提示:还没有请求走过代理时这里是空的。先 dobostitch start,再把客户端 baseURL 指过来。"); } } function commandStatus(): void { const dir = defaultHandoffDir(); const records = listHandoffs(dir); if (records.length === 0) { console.log(`[dobostitch] ${dir} 下还没有交接单(先跑 dobostitch start,并让客户端发一次请求)。`); return; } console.log(`[dobostitch] 最近会话(共 ${records.length} 个,目录 ${dir}):`); for (const record of records.slice(0, 20)) { const inputText = record.inputTokensAtCapture > 0 ? `${record.inputTokensAtCapture} token` : "未判定"; console.log(` ${record.sessionKey} ${record.createdAt} 上游 ${record.upstream} 输入 ${inputText}`); console.log(` 最近指令:${record.lastUserInstruction || "(无)"}`); } } function commandResume(argv: string[]): void { const key = argv[0]; const dir = defaultHandoffDir(); if (key) { const record = loadHandoff(key, dir); if (!record) { console.error(`[dobostitch] 没找到会话 ${key}(目录 ${dir})。可用 dobostitch status 查看全部会话。`); process.exitCode = 1; return; } console.log(renderResumeBrief(record)); return; } const records = listHandoffs(dir); if (records.length === 0) { console.error(`[dobostitch] ${dir} 下还没有交接单可续接。`); process.exitCode = 1; return; } console.log(renderResumeBrief(records[0])); } async function commandDoctor(argv: string[] = []): Promise { const checks: { name: string; ok: boolean; detail: string }[] = []; const nodeCheck = checkNodeVersion(); checks.push({ name: "Node 版本", ok: nodeCheck.ok, detail: nodeCheck.ok ? `v${process.versions.node}(要求 ≥ 18)` : `v${process.versions.node} 过旧,请升级到 18+`, }); const busy = await isPortBusy(DEFAULT_PORT); checks.push({ name: "端口占用", ok: !busy, detail: busy ? `${DEFAULT_PORT} 已被占用,start 会自动跳号(最多 +10)` : `${DEFAULT_PORT} 空闲`, }); const routes = resolveRoutes(); const routeText = routes.map((route) => `${route.prefix}→${route.baseUrl}`).join(";"); checks.push({ name: "路由表", ok: routes.length > 0, detail: routes.length > 0 ? `${routes.length} 条:${routeText}` : "没有任何路由,请检查 DOBOSTITCH_ROUTES", }); const dirCheck = checkHandoffDirWritable(defaultHandoffDir()); checks.push({ name: "交接单目录可写", ok: dirCheck.ok, detail: dirCheck.detail }); const rewrite = resolveRewriteMode(argv); const MODE_DESC: Record = { off: "不改请求体,只转发", observe: "只看不改(用于验证原样直通的前缀稳定性)", stabilize: "规范化 key 序 + 重排 tools(默认)", anchor: "stabilize + 时间字面量锚定(实验)", }; checks.push({ name: "改写档位", ok: rewrite.invalid === undefined, detail: rewrite.invalid === undefined ? `${rewrite.mode}:${MODE_DESC[rewrite.mode]}` : `未知档位 ${rewrite.invalid},将回退到 stabilize`, }); const inject = resolveInjectUsage(argv); checks.push({ name: "流式 usage 注入", ok: inject.invalid === undefined, detail: inject.invalid === undefined ? inject.enabled ? "on:openai 方言 + stream=true 时自动补 stream_options.include_usage" : "off:流式请求不补字段,命中率多为未判定" : `未知取值 ${inject.invalid},将回退到 on`, }); console.log("[dobostitch] 自检结果:"); for (const check of checks) { console.log(` [${check.ok ? "通过" : "失败"}] ${check.name}:${check.detail}`); } // v0.7.3:网关三态提示(listening / refused / listening_other / timeout) // 这条放在接入体检前面:代没起来时,底座配得再对也不会有缓存收益。 const probe = await probeProxyPort(); console.log("[dobostitch] 网关:"); console.log(` [${probe.status === "listening" ? "通过" : probe.status === "listening_other" ? "提示" : "失败"}] ${renderProxyProbe(probe).replace(/\n/g, "\n ")}`); // v0.7.3:底座接入体检(只读,不修不改) const meta = makeContext(readLastPort() ?? DEFAULT_PORT, true); const installed = listHosts() .map((host) => ({ host, files: locateHostFiles(host) })) .filter((item) => item.files.length > 0 || hostAttached(item.host)); console.log("[dobostitch] 底座接入体检:"); if (installed.length === 0) { console.log(" [通过] 本机没探测到已装底座,跳过(装了之后再跑一次 doctor 即可体检)"); } else { const healths = installed.map((item) => checkHost(item.host, item.files, { bundlePath: meta.bundlePath, proxyBase: `http://127.0.0.1:${meta.port}` }), ); const summary = summarizeHealth(healths); for (const health of healths) { const tag = health.level === "ok" ? "通过" : health.level === "warn" ? "提示" : "失败"; console.log(` [${tag}] ${health.label}(${health.hostId})${health.attached ? "" : "· 未接入"}`); for (const file of health.files) { console.log(` ${file.file}`); for (const check of file.checks) { if (check.level === "ok") continue; // 只打有问题的项,正常项不刷屏 console.log(` · [${check.level === "warn" ? "提示" : "失败"}] ${check.name}:${check.detail}`); } } } console.log(` 合计:正常 ${summary.ok} 家 / 提示 ${summary.warn} 家 / 有问题 ${summary.broken} 家`); if (summary.broken + summary.warn > 0) { console.log(" 要修:dobostitch providers health 看单家详情,再 attach --revert 或重跑 attach"); } } const handoffNotice = handoffDirNotice(); if (handoffNotice) console.log(` [提示] 交接单目录:${handoffNotice}`); const handoffWarning = handoffDirWarning(); if (handoffWarning) console.log(` [警告] 交接单目录:${handoffWarning}`); const failed = checks.filter((check) => !check.ok).length; console.log(failed === 0 ? "[dobostitch] 全部通过,可以 start 了。" : `[dobostitch] 有 ${failed} 项失败,建议先处理再 start。`); if (failed > 0) process.exitCode = 1; } /** 打印一条 attach 结果 */ function printAttachResult(result: AttachResult): void { const tag = result.status === "applied" ? "已写入" : result.status === "dry-run" ? "预览" : result.status === "skipped" ? "跳过" : "失败"; console.log(` [${tag}] ${result.label}:${result.configPath}`); for (const change of result.changes) console.log(` · ${change}`); if (result.detail) console.log(` · ${result.detail}`); if (result.backup) console.log(` · 备份:${result.backup}`); } /** providers:看底座清单 / 探测本机装了谁 / 单个底座怎么填(v0.7.0 新增) */ /** v0.9.1:验证等级的中文短标签(list / show / detect 统一口径) */ const LEVEL_SHORT: Record = { "client-confirmed": "客户端确认", "write-rollback": "写入回滚", "docs-only": "仅指路", }; function commandProviders(argv: string[]): void { const action = argv[0] && !argv[0].startsWith("--") ? argv[0] : "list"; const rest = argv.slice(argv[0] && !argv[0].startsWith("--") ? 1 : 0); const portText = argValue(argv, "port"); const parsedPort = portText ? Number(portText) : NaN; const port = Number.isFinite(parsedPort) && parsedPort > 0 ? parsedPort : readLastPort() ?? DEFAULT_PORT; const ctx = makeContext(port, true); if (action === "list") { const warnings = loadExternalHosts().warnings; for (const warning of warnings) console.error(`[dobostitch] 外部描述符:${warning}`); console.log("[dobostitch] 底座清单(P0 自动写 / P1 谨慎写 / P2 只指路;未验证的底座需 --allow-unverified 才写)"); console.log(""); console.log(" 档位 id 底座 起始版本 验证 验证等级 配置文件 状态"); for (const target of TARGETS) { const files = target.locate(); const plan = (target.plan ?? "P0").padEnd(4); const since = (target.since ?? "-").padEnd(9); const confidence = (target.confidence ?? "verified").padEnd(11); const levelText = target.verification ? LEVEL_SHORT[target.verification.level] : "无记录"; const state = files.length === 0 ? "未安装/未找到" : `${files.length} 个`; console.log( ` ${plan} ${target.id.padEnd(16)} ${padLabel(target.label).padEnd(20)} ${since} ${confidence} ${padLabel(levelText).padEnd(12)} ${state}`, ); } console.log(""); console.log(" 验证等级:客户端确认 = 客户端自己认过;写入回滚 = 写入/语法/条目/回滚都复算过;仅指路 = 不写盘只给指引"); console.log(" 看单个底座怎么填:dobostitch providers show "); console.log(" 预览会改哪些文件:dobostitch providers plan "); return; } if (action === "detect") { const found = TARGETS.map((target) => ({ target, files: target.locate() })).filter((row) => row.files.length > 0); if (found.length === 0) { console.log("[dobostitch] 本机没探测到任何已知底座的配置文件。"); console.log(` 通用接入方式见:dobostitch attach --target generic`); return; } console.log(`[dobostitch] 探测到 ${found.length} 个底座:`); for (const row of found) { const hint = (row.target.plan ?? "P0") === "P2" ? "只指路" : isAutoAttachEligible(row.target.confidence ?? "verified", row.target.verification) ? "可自动接入" : "需 --allow-unverified"; console.log(` · ${row.target.id}(${row.target.label})${hint}`); for (const file of row.files) console.log(` ${file}`); } return; } const id = rest[0] ?? ""; if (action === "health") { // v0.7.3:单家 / 全量体检。只读,不修不改。 const targets = id === "" ? listHosts() : listHosts().filter((item) => item.id === id); if (targets.length === 0) { console.error(`[dobostitch] 不认识的底座:${id};先跑 dobostitch providers list 看清单`); process.exitCode = 1; return; } const proxyBase = `http://127.0.0.1:${port}`; let problems = 0; for (const item of targets) { const files = locateHostFiles(item); if (files.length === 0 && !hostAttached(item)) continue; const health = checkHost(item, files, { bundlePath: ctx.bundlePath, proxyBase }); const tag = health.level === "ok" ? "通过" : health.level === "warn" ? "提示" : "失败"; console.log(`[${tag}] ${health.label}(${health.hostId})${health.attached ? "" : "· 未接入"}`); if (files.length === 0) console.log(" 配置文件:未找到(只按接入记录判断)"); for (const file of health.files) { console.log(` 配置文件:${file.file}`); for (const check of file.checks) { const mark = check.level === "ok" ? "通过" : check.level === "warn" ? "提示" : "失败"; console.log(` · [${mark}] ${check.name}:${check.detail}`); } } if (health.level !== "ok") problems += 1; } if (problems === 0) console.log("[dobostitch] 体检没发现问题。"); return; } const target = TARGETS.find((item) => item.id === id); if (!id || !target) { console.error(`[dobostitch] 不认识的底座:${id || "(空)"};先跑 dobostitch providers list 看清单`); process.exitCode = 1; return; } const host = getHost(target.id); if (action === "show") { console.log(`[dobostitch] ${target.label}(${target.id})`); console.log(` 档位:${target.plan ?? "P0"} 起始版本:${target.since ?? "-"} 验证:${target.confidence ?? "verified"}`); const verRecord = HOST_VERIFICATION[target.id]; if (verRecord) { console.log(` 验证等级:${LEVEL_SHORT[verRecord.level]}(复核于 ${verRecord.at})`); console.log(` 证据:${verRecord.basis}`); } if (host?.source) console.log(` 依据:${host.source}`); const files = target.locate(); console.log(` 配置文件:${files.length === 0 ? "未找到(装好该底座后重试)" : ""}`); for (const file of files) console.log(` ${file}`); if (host) { console.log(""); for (const line of renderGuide(host, ctx)) console.log(line); } else { // 老三家走接入层私有实现,不在描述符注册表里:直接给出可执行动作 console.log(""); console.log("该底座为自动写入(改前自动备份,--revert 复原接入前原值):"); console.log(` 直接接入:dobostitch attach --target ${target.id}`); console.log(` 预览不写:dobostitch attach --target ${target.id} --dry-run --allow-unverified`); } return; } if (action === "plan") { const files = target.locate(); if (files.length === 0) { console.log(`[dobostitch] 没找到 ${target.label} 的配置文件,先装/先跑一次该底座再来。`); if (host) for (const line of renderGuide(host, ctx)) console.log(line); return; } console.log(`[dobostitch] ${target.label} 接入预览(不写盘):`); for (const file of files) printAttachResult(target.sync(file, ctx)); return; } console.error(`[dobostitch] providers 不认识的动作:${action}(可选 list / detect / show / plan / health)`); process.exitCode = 1; } function padLabel(label: string): string { // 中文按两个字符宽估算,避免表头错位 let width = 0; for (const char of label) width += /[\u4e00-\u9fa5]/.test(char) ? 2 : 1; return label + " ".repeat(Math.max(0, 18 - width)); } /** attach:自动找配置、备份、改 baseURL、写 MCP 入口 */ async function commandAttach(argv: string[]): Promise { const portText = argValue(argv, "port"); const parsedPort = portText ? Number(portText) : NaN; const explicitPort = Number.isFinite(parsedPort) && parsedPort > 0 ? parsedPort : null; const dryRun = argv.includes("--dry-run"); const revert = argv.includes("--revert"); const allowUnverified = argv.includes("--allow-unverified"); // `attach --target` 不给值 = 想看有哪些底座 if (argv.includes("--target") && argValue(argv, "target") === undefined) { commandProviders(["list"]); return; } const targetId = argValue(argv, "target") ?? "auto"; const targets = findTargets(targetId, { allowUnverified }); if (targets.length === 0) { const known = TARGETS.map((target) => target.id).join(" / "); console.error(`[dobostitch] 不认识的底座:${targetId}(可选 auto / generic / ${known})`); console.error(` 若该底座尚未真机验证,加 --allow-unverified 显式写入。`); process.exitCode = 1; return; } // 端口选择:用户显式指定 > 沿用已在跑的代理 > 默认端口 const locatedFiles = targets.flatMap((target) => target.locate()); const chosen = explicitPort ? { port: explicitPort, reused: false } : await resolveAttachPort(locatedFiles); const port = chosen.port; // 记住最终端口,MCP bundle / 开机自启都读它,保证全机一个端口口径 if (!dryRun) writeLastPort(port); const ctx = makeContext(port, dryRun); const routes = resolveRoutes().map((route) => ({ prefix: route.prefix, baseUrl: route.baseUrl })); if (targetId === "generic") { console.log(renderGenericSnippet(ctx, routes)); return; } console.log(`[dobostitch] ${revert ? "退回" : "接入"}模式${dryRun ? "(预览,不写盘)" : ""};代理端口 ${port}${chosen.reused ? "(沿用已在跑的代理)" : ""}`); console.log(`[dobostitch] MCP 入口:${ctx.bundlePath}`); console.log(""); const results: AttachResult[] = []; for (const target of targets) { const files = target.locate(); if (files.length === 0) { results.push({ target: target.id, label: target.label, configPath: "-", status: "skipped", changes: [], detail: "没找到该底座的配置文件", }); continue; } for (const file of files) { try { results.push(revert ? target.revert(file, ctx) : target.sync(file, ctx)); } catch (err) { results.push({ target: target.id, label: target.label, configPath: file, status: "error", changes: [], detail: (err as Error)?.message ?? String(err), }); } } } for (const result of results) printAttachResult(result); const applied = results.filter((result) => result.status === "applied").length; const failed = results.filter((result) => result.status === "error").length; // v0.8.1:写入前预检拦下的(目录/文件不可写)——这些要改打可粘贴片段 const blocked = results.filter((result) => result.writeBlocked === true).length; const touched = results.some((result) => result.status === "applied" || result.status === "dry-run"); // 已接入状态也属于「识别到了底座」:重复执行时不能再吐 generic 片段,否则用户会以为接入失败 const alreadyReady = results.filter( (result) => result.status === "skipped" && (result.detail ?? "").includes("已是接入状态"), ).length; if (!revert && !dryRun && applied > 0 && !argv.includes("--no-autostart")) { const auto = enableAutostart(port); console.log(""); console.log(`[dobostitch] 开机自启:${auto.registered ? "已注册" : "未注册"} —— ${auto.detail}`); console.log(` 载体:${auto.location}`); } console.log(""); if (!revert && !touched && alreadyReady === 0 && blocked === 0) { console.log("[dobostitch] 没识别到可自动接入的底座,下面这段可直接粘到任意客户端:"); console.log(""); console.log(renderGenericSnippet(ctx, routes)); return; } if (!revert && blocked > 0) { console.log(`[dobostitch] ${blocked} 个文件的写入前预检没通过(目录或文件不可写),这些文件一个字节都没动。`); console.log(" 请手工把下面这段粘进对应配置文件:"); console.log(""); console.log(renderGenericSnippet(ctx, routes)); console.log(""); } if (failed > 0) { console.log(`[dobostitch] ${failed} 个文件处理失败,原文件已备份,按上面提示排查后重跑即可。`); process.exitCode = 1; return; } console.log( dryRun ? "[dobostitch] 预览结束,以上都没写盘;确认无误后去掉 --dry-run 再跑一次即真正接入。" : revert ? "[dobostitch] 已退回接入前状态,重启客户端即生效。" : applied > 0 ? "[dobostitch] 接好了:重启客户端即可,代理会随 MCP 自动起来,不用再单独起进程。" : "[dobostitch] 已是接入状态,无需重复执行;直接重启客户端即可用。", ); } function ensureAgentRules(dryRun: boolean): void { for (const r of ensureAgentRulesAt(process.cwd(), dryRun)) { const verb = r.action === "created" || r.action === "preview-created" ? "新建" : r.action === "appended" || r.action === "preview-appended" ? "追加" : "更新"; if (dryRun) console.log(`[dobostitch] 使用规程:将${verb} ${r.file}(预览,未写入)`); else console.log(`[dobostitch] 使用规程:已${verb} ${r.file}`); } } /** setup:一条命令完成"探测底座 → 写配置 → 注册自启",小白唯一需要记的命令 */ async function commandSetup(argv: string[]): Promise { const dryRun = argv.includes("--dry-run"); console.log("[dobostitch] 一键接入:识别本机 AI 客户端 → 写入 MCP 配置 → 注册开机自启 → 写入使用规程"); console.log(""); await commandAttach(dryRun ? ["--dry-run", "--no-autostart"] : ["--no-autostart"]); ensureAgentRules(dryRun); if (dryRun) return; const located = findTargets("auto").flatMap((target) => target.locate()); const { port } = await resolveAttachPort(located); writeLastPort(port); const st = statusTransport(); if (st.registered) { console.log(`[dobostitch] 开机自启:已注册(${st.detail})`); } else { const r = enableAutostart(port); console.log(`[dobostitch] 开机自启:${r.registered ? "已开启" : "未开启"}(${r.detail})`); if (!r.registered) process.exitCode = 1; } console.log(""); console.log("[dobostitch] 接入完成。重启 AI 客户端即可生效,不需要改代码,也不需要写任何脚本。"); } /** autostart:注册/取消/查看开机自启 */ async function commandAutostart(argv: string[]): Promise { const action = argv[0] ?? "status"; const portText = argValue(argv, "port"); const parsedPort = portText ? Number(portText) : NaN; // 端口口径:显式 --port > 已在跑的代理 > 上次记录的端口 > 默认端口 let port = Number.isFinite(parsedPort) && parsedPort > 0 ? parsedPort : NaN; if (!Number.isFinite(port)) { const located = findTargets("auto").flatMap((target) => target.locate()); port = (await resolveAttachPort(located)).port; } if (!Number.isFinite(port)) port = readLastPort() ?? DEFAULT_PORT; if (action === "enable") { writeLastPort(port); const result = enableAutostart(port); console.log(`[dobostitch] ${result.registered ? "已开启" : "未开启"}:${result.detail}`); console.log(` 载体:${result.location}`); if (!result.registered) process.exitCode = 1; return; } if (action === "disable") { const result = disableAutostart(); console.log(`[dobostitch] 关闭开机自启:${result.detail}`); console.log(` 载体:${result.location}`); return; } if (action !== "status") { console.error(`[dobostitch] 未知动作:${action}(可选 status / enable / disable)`); process.exitCode = 1; return; } const result = statusTransport(); console.log(`[dobostitch] 开机自启状态:${result.registered ? "已注册" : "未注册"}`); console.log(` 平台支持:${result.supported ? "是" : "否"}`); console.log(` 载体:${result.location}`); console.log(` 说明:${result.detail}`); } /** * --log :把本进程的 console 输出写进文件。 * 用途:被 bundle 以后台方式拉起时,日志必须由代理自己落盘, * 否则后台进程会继承调用方的 stdout/stderr,把 MCP 协议流搞脏、还让调用方管道不结束。 */ function redirectConsoleIfRequested(argv: string[]): void { const target = argValue(argv, "log"); if (!target) return; try { fs.mkdirSync(path.dirname(target), { recursive: true }); const stream = fs.createWriteStream(target, { flags: "a" }); const write = (...args: unknown[]): void => { stream.write(`${args.map((item) => (typeof item === "string" ? item : String(item))).join(" ")}\n`); }; console.log = write; console.error = write; } catch { /* 写不了就保持原样,不能因为日志失败影响代理 */ } } async function main(): Promise { const [, , command = "", ...rest] = process.argv; redirectConsoleIfRequested(rest); switch (command) { case "start": await commandStart(rest); return; case "status": commandStatus(); return; case "resume": commandResume(rest); return; case "doctor": await commandDoctor(rest); return; case "attach": await commandAttach(rest); return; case "providers": commandProviders(rest); return; case "gain": commandGain(rest); return; case "setup": await commandSetup(rest); return; case "autostart": await commandAutostart(rest); return; case "": case "--help": case "-h": case "help": console.log(USAGE); return; default: console.error(`[dobostitch] 未知子命令:${command}\n`); console.log(USAGE); process.exitCode = 1; } } main().catch((err: unknown) => { console.error(`[dobostitch] 启动失败:${(err as Error)?.message ?? err}`); process.exitCode = 1; });