#!/usr/bin/env node // Streamline 命令行。结果一律以 JSON 打到 stdout(给 Agent 读),提示和进度打到 stderr。 // 以登录用户本人的身份操作(/api/cli/v1/*);任何输出里都不出现登录会话的明文。 // 系统接入(API Key、Webhook、飞书机器人)不在这里:那是服务端程序按接入文档直接调 HTTP 接口的事。 import { readFileSync } from "node:fs"; import { CliError, call } from "../lib/http.js"; import { login } from "../lib/login.js"; import { install, uninstall } from "../lib/install.js"; import { gitContext, parseHandoff } from "../lib/handoff.js"; import { changeUnderTest, parseVerifyCases } from "../lib/verify.js"; import { apiBase, load, save } from "../lib/store.js"; const HELP = `Streamline 命令行(Skill 背后的工具) 登录(以你本人的身份用 Streamline,和在面板上操作一样) streamline auth login [--no-browser] 浏览器 SSO 授权这台电脑(30 天) streamline auth status | logout 提单与查进展 streamline task import [--watchers a,b] streamline task create --title <标题> --description <描述> [--platform android|ios] [--repro <复现步骤>] [--watchers a,b] streamline task list [--limit 20] 我建的单 streamline task get streamline task execute 确认执行(只能确认自己建的单) streamline task handoff --title <标题> --file <总结.md|-> [--platform android|ios] [--watchers a,b] [--no-git] 接力:把本地 Agent 的调研总结交给 Streamline 从头修 安装 Skill(Claude Code / Codex / Cursor) streamline install [--agent claude,codex,cursor|all] [--project | --dir <目录>] 默认装给本机检测到的所有 Agent:正本在 ~/.agents/skills/streamline-openapi, 再软链到 ~/.claude/skills、~/.codex/skills、~/.cursor/skills;--project 拷进当前仓库 streamline uninstall [--agent …] [--project] 环境变量:STREAMLINE_API_URL、STREAMLINE_WEB_URL(连自建环境时用)`; function parse(argv) { const pos = [], flags = {}; for (let i = 0; i < argv.length; i++) { const a = argv[i]; if (!a.startsWith("--")) { pos.push(a); continue; } const eq = a.indexOf("="); if (eq > 0) { flags[a.slice(2, eq)] = a.slice(eq + 1); continue; } const name = a.slice(2); if (name.startsWith("no-")) { flags[name.slice(3)] = false; continue; } const next = argv[i + 1]; if (next === undefined || next.startsWith("--")) flags[name] = true; else { flags[name] = next; i++; } } return { pos, flags }; } const out = (v) => process.stdout.write(JSON.stringify(v, null, 2) + "\n"); const note = (s) => process.stderr.write(s + "\n"); const list = (v) => (typeof v === "string" ? v.split(",").map((s) => s.trim()).filter(Boolean) : undefined); function need(flags, name) { const v = flags[name]; if (typeof v !== "string" || !v.trim()) throw new CliError(`缺少 --${name}`); return v.trim(); } const readInput = (file) => readFileSync(file === "-" ? 0 : file, "utf8"); async function auth(sub, flags) { if (sub === "login") { const r = await login({ openBrowser: flags.browser !== false }); note(`已登录:${r.email}`); return out({ loggedIn: true, email: r.email, expiresAt: new Date(r.expiresAt).toISOString() }); } if (sub === "status") { const s = load().session; if (!s) return out({ loggedIn: false, api: apiBase() }); try { const r = await call("GET", "/api/openapi-cli/auth/session", { auth: "session" }); return out({ loggedIn: true, email: r.email, expiresAt: new Date(r.expiresAt).toISOString(), api: apiBase() }); } catch (err) { return out({ loggedIn: false, api: apiBase(), error: err.message }); } } if (sub === "logout") { if (load().session) await call("POST", "/api/openapi-cli/auth/logout", { auth: "session", body: {} }).catch(() => {}); save(({ session, ...rest }) => rest); return out({ loggedOut: true }); } throw new CliError("用法:streamline auth login | status | logout"); } // 改动验证暂时下线:--help 和 Skill 里都不再出现,服务端也拒绝新建(routes_cli.ts 的 CHANGE_RUN_OPEN)。 // 命令留着:报错里带着服务端的说明,比「未知命令」好懂。机器数量、按需独占实例、热编译就绪后再放回。 async function verify(sub, pos, flags) { if (sub === "create") { const platform = need(flags, "platform"); if (platform !== "android" && platform !== "ios") throw new CliError("--platform 只能是 android 或 ios"); const parsed = parseVerifyCases(readInput(need(flags, "file"))); const change = changeUnderTest({ allowDirty: flags["allow-dirty"] === true }); note(`被测提交 ${change.commit.slice(0, 12)}${change.branch ? `(分支 ${change.branch})` : ""},${parsed.cases.length} 条用例`); return out(await call("POST", "/api/cli/v1/verifications", { auth: "session", body: { title: need(flags, "title"), platform, background: parsed.background, cases: parsed.cases, change }, })); } if (sub === "rerun") { if (!pos[0]) throw new CliError("用法:streamline verify rerun <上一次验证的 id>"); const change = changeUnderTest({ allowDirty: flags["allow-dirty"] === true }); note(`复验:被测提交 ${change.commit.slice(0, 12)}${change.branch ? `(分支 ${change.branch})` : ""},沿用上一次的用例`); return out(await call("POST", `/api/cli/v1/verifications/${encodeURIComponent(pos[0])}/rerun`, { auth: "session", body: { change } })); } if (sub === "get") { if (!pos[0]) throw new CliError("用法:streamline verify get "); return out(await call("GET", `/api/cli/v1/verifications/${encodeURIComponent(pos[0])}`, { auth: "session" })); } throw new CliError("用法:streamline verify create | rerun | get"); } async function task(sub, pos, flags) { const base = "/api/cli/v1"; if (sub === "create") { const body = { title: need(flags, "title"), description: need(flags, "description"), platform: typeof flags.platform === "string" ? flags.platform : undefined, reproSteps: typeof flags.repro === "string" ? flags.repro : undefined, watchers: list(flags.watchers), }; return out(await call("POST", `${base}/tasks`, { auth: "session", body })); } if (sub === "import") { if (!pos[0]) throw new CliError("用法:streamline task import "); return out(await call("POST", `${base}/meego-import`, { auth: "session", body: { meegoLink: pos[0], watchers: list(flags.watchers) } })); } if (sub === "handoff") { const body = { title: need(flags, "title"), platform: typeof flags.platform === "string" ? flags.platform : undefined, watchers: list(flags.watchers), ...parseHandoff(readInput(need(flags, "file"))), context: flags.git === false ? undefined : gitContext(), }; return out(await call("POST", `${base}/handoff`, { auth: "session", body })); } if (sub === "get") { if (!pos[0]) throw new CliError("用法:streamline task get "); return out(await call("GET", `${base}/tasks/${encodeURIComponent(pos[0])}`, { auth: "session" })); } if (sub === "list") { return out(await call("GET", `${base}/tasks`, { auth: "session", query: { limit: flags.limit } })); } if (sub === "execute") { if (!pos[0]) throw new CliError("用法:streamline task execute "); return out(await call("POST", `${base}/tasks/${encodeURIComponent(pos[0])}/execute`, { auth: "session", body: {} })); } throw new CliError("用法:streamline task create | import | handoff | get | list | execute"); } async function main() { const [cmd, sub, ...rest] = process.argv.slice(2); const { pos, flags } = parse(rest); switch (cmd) { case "auth": return auth(sub, flags); case "verify": return verify(sub, pos, flags); case "task": return task(sub, pos, flags); case "install": return out(install(parse([sub, ...rest].filter((x) => x !== undefined)).flags)); case "uninstall": return out(uninstall(parse([sub, ...rest].filter((x) => x !== undefined)).flags)); case undefined: case "help": case "--help": case "-h": return note(HELP); default: throw new CliError(`未知命令 ${cmd}`, "运行 streamline help"); } } main().catch((err) => { const e = { error: err.message }; if (err.hint) e.hint = err.hint; process.stderr.write(JSON.stringify(e) + "\n"); process.exit(1); });