@jixing475/magpie-marvis 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +255 -0
- package/CHANGELOG.md +726 -0
- package/CHECKLIST.md +161 -0
- package/CONTRIBUTING.md +83 -0
- package/GUIDE.md +275 -0
- package/HOOKS.md +1333 -0
- package/LICENSE +21 -0
- package/MULTI-PROVIDER.md +414 -0
- package/NOTICE.md +46 -0
- package/OFFICIAL-SOURCES.md +142 -0
- package/README-TEMPLATE.md +271 -0
- package/README.md +300 -3
- package/SCORING.md +151 -0
- package/UPDATING.md +660 -0
- package/examples/split/README.md +66 -0
- package/examples/split/index.mjs +75 -0
- package/examples/split/src/hooks/config.mjs +18 -0
- package/examples/split/src/hooks/models.mjs +49 -0
- package/examples/split/src/hooks/usage.mjs +55 -0
- package/extract-contract.py +56 -0
- package/index.mjs +48 -0
- package/package.json +95 -4
- package/panel/first-line.css +112 -0
- package/panel/first-line.html +37 -0
- package/panel/first-line.js +137 -0
- package/registry-entry.json +11 -0
- package/scripts/check-magpie-stable.mjs +109 -0
- package/scripts/check-maintenance.mjs +146 -0
- package/scripts/check.mjs +521 -0
- package/scripts/cli.mjs +54 -0
- package/scripts/contract-diff.mjs +112 -0
- package/scripts/create.mjs +270 -0
- package/scripts/npm-name.mjs +19 -0
- package/scripts/probe-served-model.mjs +172 -0
- package/scripts/probe-vision.mjs +173 -0
- package/scripts/publish-npm.mjs +44 -0
- package/scripts/release.mjs +442 -0
- package/scripts/score.mjs +280 -0
- package/src/account-name.mjs +145 -0
- package/src/auth.mjs +27 -0
- package/src/context.mjs +369 -0
- package/src/hooks/config.mjs +236 -0
- package/src/hooks/loader.mjs +593 -0
- package/src/hooks/methods.mjs +398 -0
- package/src/hooks/models.mjs +25 -0
- package/src/hooks/refresh.mjs +149 -0
- package/src/hooks/usage.mjs +117 -0
- package/src/marvis/beacon-mac.mjs +213 -0
- package/src/marvis/beacon-shim.mjs +143 -0
- package/src/marvis/browser.mjs +598 -0
- package/src/marvis/desktop-mac.mjs +232 -0
- package/src/marvis/desktop.mjs +273 -0
- package/src/marvis/device.mjs +85 -0
- package/src/marvis/expiry.mjs +60 -0
- package/src/marvis/fingerprint.mjs +168 -0
- package/src/marvis/images.mjs +230 -0
- package/src/marvis/login.mjs +512 -0
- package/src/marvis/profile.mjs +113 -0
- package/src/marvis/protocol.mjs +160 -0
- package/src/marvis/tool-schema.mjs +97 -0
- package/src/quota.mjs +212 -0
- package/src/refresh-lock.mjs +115 -0
- package/tests/device-registry.test.mjs +48 -0
- package/tests/e2e-live.test.mjs +120 -0
- package/tests/image-input.test.mjs +314 -0
- package/tests/legacy-model-config.test.mjs +349 -0
- package/tests/legacy-model-loader.test.mjs +306 -0
- package/tests/loader-model-support.test.mjs +823 -0
- package/tests/manual-paste-login.test.mjs +228 -0
- package/tests/model-alias.test.mjs +59 -0
- package/tests/model-catalog-support.test.mjs +241 -0
- package/tests/mount.test.mjs +259 -0
- package/tests/oauth-exchange.test.mjs +362 -0
- package/tests/panel-manifest.test.mjs +73 -0
- package/tests/plugin-options.test.mjs +100 -0
- package/tests/profile.test.mjs +96 -0
- package/tests/refresh-contract.test.mjs +318 -0
- package/tests/request-identity.test.mjs +18 -0
- package/tests/tool-schema.test.mjs +66 -0
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// probe-vision.mjs —— 模型「说自己能收图」,是不是真的?发一张图问它
|
|
3
|
+
//
|
|
4
|
+
// 为什么:magpie 的模型图片能力有三个来源,谁说了算不一样 ——
|
|
5
|
+
// ① 厂商列表(插件的 capabilities.input.image,宿主按它算 imageSaid)
|
|
6
|
+
// ② models.dev 的**裸模型 id 投票**(插件没说时的兜底)
|
|
7
|
+
// ③ 用户在 Providers › Names & levels 手动勾的 Accepts images(压过前两者)
|
|
8
|
+
// ② 会错(同名模型在别家标了 image,实为文本的也被带成能收图),
|
|
9
|
+
// ③ 会错(手勾成 true 而厂商说 false)。两种错都不报错:截图直发上游,
|
|
10
|
+
// 上游要么回 not multimodal(400/502),要么干脆当没有图,模型答「我看不到图片」。
|
|
11
|
+
//
|
|
12
|
+
// 实测(2026-10-29,magpie 0.1.1932):CodeArts 8 个模型漏写 image,
|
|
13
|
+
// 四天 161 次被 `The request model is not multimodal (InferHub.001001020.406)` 拒绝;
|
|
14
|
+
// ima / magicrew 三个模型厂商说 image:false 却被勾成 true,
|
|
15
|
+
// 一个回「图片似乎无法查看」,一个回 400「不支持多模态输入」。
|
|
16
|
+
//
|
|
17
|
+
// 用法:
|
|
18
|
+
// node scripts/probe-vision.mjs # 测网关 /v1/models 里的每个模型
|
|
19
|
+
// node scripts/probe-vision.mjs <provider/model> … # 只测这几个
|
|
20
|
+
// MAGPIE_GATEWAY=http://127.0.0.1:3425 node scripts/probe-vision.mjs
|
|
21
|
+
// node scripts/probe-vision.mjs --vision-only # 只测 magpie 设置里的识图模型
|
|
22
|
+
//
|
|
23
|
+
// 它只读 ~/.config/magpie/settings.json 和本机网关,不发外部请求,不改任何配置。
|
|
24
|
+
//
|
|
25
|
+
// 它回答的是**端到端**那个问题:「我发一张图,它能不能看见」——
|
|
26
|
+
// 直读和「magpie 用识图模型描述后给它」都算能看见(后者是 magpie 的正常机制)。
|
|
27
|
+
// 想区分是哪一种、以及这个模型的声明是哪来的,用 `magpie model spec <provider/model>`
|
|
28
|
+
// 看 `vision` 一行的来源列:`yours`(手勾)/`the vendor's list / models.dev`。
|
|
29
|
+
//
|
|
30
|
+
// 退出码:0 = 每个模型都读到了图,或被明确定义为文本模型(400 does not support image input)
|
|
31
|
+
// 2 = 发现假声明(图发出去了,模型答「看不见」)—— 这正是要修的那一类
|
|
32
|
+
// 1 = 连不上网关,或设置里没识图模型
|
|
33
|
+
|
|
34
|
+
import { readFileSync } from "node:fs"
|
|
35
|
+
import { homedir } from "node:os"
|
|
36
|
+
import { join } from "node:path"
|
|
37
|
+
|
|
38
|
+
const GATEWAY = (process.env.MAGPIE_GATEWAY ?? "http://127.0.0.1:3425").replace(/\/+$/, "")
|
|
39
|
+
const CONFIG = join(homedir(), ".config", "magpie", "settings.json")
|
|
40
|
+
|
|
41
|
+
// 一张 320×120 的白底 PNG,上面写着 MAGPIE-VISION-TEST 42。
|
|
42
|
+
// 内嵌是为了自包含:探针要能在任何机器上跑,不依赖工作目录里有没有那张图。
|
|
43
|
+
const PROBE_PNG = "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"
|
|
44
|
+
const QUESTION = "图片里写了什么字?只回图上的原文,不要解释。"
|
|
45
|
+
|
|
46
|
+
const C = {
|
|
47
|
+
dim: (s) => `\x1b[2m${s}\x1b[0m`,
|
|
48
|
+
green: (s) => `\x1b[32m${s}\x1b[0m`,
|
|
49
|
+
red: (s) => `\x1b[31m${s}\x1b[0m`,
|
|
50
|
+
yellow: (s) => `\x1b[33m${s}\x1b[0m`,
|
|
51
|
+
bold: (s) => `\x1b[1m${s}\x1b[0m`,
|
|
52
|
+
cyan: (s) => `\x1b[36m${s}\x1b[0m`,
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** magpie 设置里那个「识图模型」:Settings › Models › Image recognition */
|
|
56
|
+
function visionModel() {
|
|
57
|
+
try {
|
|
58
|
+
const j = JSON.parse(readFileSync(CONFIG, "utf8"))
|
|
59
|
+
const v = String(j?.vision ?? "").trim()
|
|
60
|
+
return !v || v === "off" || v === "none" ? "" : v
|
|
61
|
+
} catch {
|
|
62
|
+
return ""
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function modelsFromGateway() {
|
|
67
|
+
const res = await fetch(`${GATEWAY}/v1/models`, { signal: AbortSignal.timeout(15000) })
|
|
68
|
+
if (!res.ok) throw new Error(`/v1/models answered ${res.status}`)
|
|
69
|
+
const j = await res.json()
|
|
70
|
+
return (j?.data ?? []).map((m) => m.id).filter(Boolean)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** 问模型一次:这张图里写了什么 */
|
|
74
|
+
async function ask(model) {
|
|
75
|
+
const body = {
|
|
76
|
+
model,
|
|
77
|
+
stream: false,
|
|
78
|
+
messages: [
|
|
79
|
+
{
|
|
80
|
+
role: "user",
|
|
81
|
+
content: [
|
|
82
|
+
{ type: "text", text: QUESTION },
|
|
83
|
+
{ type: "image_url", image_url: { url: `data:image/png;base64,${PROBE_PNG}` } },
|
|
84
|
+
],
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
}
|
|
88
|
+
const started = Date.now()
|
|
89
|
+
let res
|
|
90
|
+
try {
|
|
91
|
+
res = await fetch(`${GATEWAY}/v1/chat/completions`, {
|
|
92
|
+
method: "POST",
|
|
93
|
+
headers: { "content-type": "application/json" },
|
|
94
|
+
body: JSON.stringify(body),
|
|
95
|
+
signal: AbortSignal.timeout(180000),
|
|
96
|
+
})
|
|
97
|
+
} catch (e) {
|
|
98
|
+
return { kind: "error", ms: Date.now() - started, detail: String(e?.message ?? e) }
|
|
99
|
+
}
|
|
100
|
+
const text = await res.text()
|
|
101
|
+
const ms = Date.now() - started
|
|
102
|
+
if (!res.ok) {
|
|
103
|
+
if (res.status === 400 && /does not support image input/i.test(text)) return { kind: "text-only", ms, detail: "" }
|
|
104
|
+
return { kind: "error", ms, detail: `${res.status}: ${text.slice(0, 200)}` }
|
|
105
|
+
}
|
|
106
|
+
let answer = ""
|
|
107
|
+
try {
|
|
108
|
+
answer = String(JSON.parse(text)?.choices?.[0]?.message?.content ?? "")
|
|
109
|
+
} catch {
|
|
110
|
+
answer = text.slice(0, 200)
|
|
111
|
+
}
|
|
112
|
+
if (/MAGPIE-VISION-TEST/i.test(answer)) return { kind: "sees", ms, detail: answer.slice(0, 120) }
|
|
113
|
+
if (/不支持多模态|not multimodal|does not support image|无法查看|看不到|无法获取[\s\S]{0,6}图片|cannot (see|view|read) (the )?image|no image|没有(收到|看到)图/i.test(answer)) {
|
|
114
|
+
return { kind: "blind", ms, detail: answer.slice(0, 160) }
|
|
115
|
+
}
|
|
116
|
+
return { kind: "unclear", ms, detail: answer.slice(0, 160) }
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const argv = process.argv.slice(2)
|
|
120
|
+
const onlyVision = argv.includes("--vision-only")
|
|
121
|
+
const wanted = argv.filter((a) => !a.startsWith("--"))
|
|
122
|
+
|
|
123
|
+
let models = wanted
|
|
124
|
+
if (models.length === 0) {
|
|
125
|
+
const vision = visionModel()
|
|
126
|
+
if (onlyVision) {
|
|
127
|
+
if (!vision) {
|
|
128
|
+
console.error(C.red("✗ magpie 的 Settings › Models › Image recognition 没设模型(或设为 off)"))
|
|
129
|
+
process.exit(1)
|
|
130
|
+
}
|
|
131
|
+
models = [vision]
|
|
132
|
+
} else {
|
|
133
|
+
try {
|
|
134
|
+
models = await modelsFromGateway()
|
|
135
|
+
} catch (e) {
|
|
136
|
+
console.error(C.red(`✗ 连不上 magpie 网关 ${GATEWAY} —— ${e?.message ?? e}`))
|
|
137
|
+
console.error(C.dim(` 先确认 magpie 在跑:curl -s -o /dev/null -w 'HTTP %{http_code}\n' ${GATEWAY}/v1/models`))
|
|
138
|
+
process.exit(1)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const vision = visionModel()
|
|
144
|
+
console.log(C.bold(`探针:${GATEWAY} 共 ${models.length} 个模型`))
|
|
145
|
+
console.log(C.dim(vision ? `识图模型(Settings):${vision}` : "识图模型(Settings):未设置"))
|
|
146
|
+
console.log("")
|
|
147
|
+
|
|
148
|
+
const tally = { sees: 0, blind: 0, "text-only": 0, unclear: 0, error: 0 }
|
|
149
|
+
for (const model of models) {
|
|
150
|
+
const r = await ask(model)
|
|
151
|
+
tally[r.kind] = (tally[r.kind] ?? 0) + 1
|
|
152
|
+
const ms = C.dim(`${(r.ms / 1000).toFixed(1)}s`)
|
|
153
|
+
if (r.kind === "sees") console.log(`${C.green("✓ 能读到图")} ${model} ${ms}`)
|
|
154
|
+
else if (r.kind === "blind") console.log(`${C.red("✗ 假声明 ")} ${model} ${ms}\n ${C.dim(r.detail)}`)
|
|
155
|
+
else if (r.kind === "text-only") console.log(`${C.cyan("· 已标文本")} ${model} ${ms} ${C.dim("magpie 会替它描述图片或把图片略去")}`)
|
|
156
|
+
else if (r.kind === "unclear") console.log(`${C.yellow("? 看不出 ")} ${model} ${ms}\n ${C.dim(r.detail)}`)
|
|
157
|
+
else console.log(`${C.yellow("! 请求失败")} ${model} ${ms}\n ${C.dim(r.detail)}`)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
console.log("")
|
|
161
|
+
console.log(
|
|
162
|
+
`能读到图 ${tally.sees} · 假声明 ${tally.blind} · 已标文本 ${tally["text-only"]} · 看不出 ${tally.unclear} · 失败 ${tally.error}`,
|
|
163
|
+
)
|
|
164
|
+
console.log(C.dim("「能读到图」含两种:模型自己看,或 magpie 用识图模型描述后给它(两者都是正常机制)。"))
|
|
165
|
+
|
|
166
|
+
if (tally.blind > 0) {
|
|
167
|
+
console.log("")
|
|
168
|
+
console.log(C.red("「假声明」= magpie 当它能收图、把图直发上游,而上游看不见(或直接拒绝)。修法二选一:"))
|
|
169
|
+
console.log(" · 厂商真的收不了图 → 插件里把 image 写成 false(HOOKS 十一之三的 TEXT_ONLY 模板);")
|
|
170
|
+
console.log(" · 只是 models.dev 投票带偏 / 手勾错了 → magpie model spec <model> --vision off")
|
|
171
|
+
process.exit(2)
|
|
172
|
+
}
|
|
173
|
+
process.exit(0)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// 发布时临时换 name:仓库里的 name 是 slug(bun 依赖键),
|
|
3
|
+
// npm 上必须使用真正的 npm 包名。发布后无论成败都恢复原文件。
|
|
4
|
+
// 用法:npm run publish:npm
|
|
5
|
+
//
|
|
6
|
+
// 为什么不能直接 npm publish:package.json.name 是 bun 依赖键(slug),
|
|
7
|
+
// npm 名可能不同;发布前临时换成 PUBLISH_AS,发布完(包括失败)都恢复。
|
|
8
|
+
//
|
|
9
|
+
// CI=true 时,npm 报「同版本已存在」(E409 / previously staged)按成功处理:
|
|
10
|
+
// 本地 release.mjs --npm 可能已经发过,tag 触发的 workflow 不该因此变红。
|
|
11
|
+
// 本地仍报错并提示 bump。
|
|
12
|
+
|
|
13
|
+
import { spawnSync } from "node:child_process"
|
|
14
|
+
import { readFileSync, writeFileSync } from "node:fs"
|
|
15
|
+
|
|
16
|
+
const PUBLISH_AS = "magpie-marvis"
|
|
17
|
+
const file = new URL("../package.json", import.meta.url)
|
|
18
|
+
const original = readFileSync(file, "utf8")
|
|
19
|
+
const pkg = JSON.parse(original)
|
|
20
|
+
|
|
21
|
+
pkg.name = PUBLISH_AS
|
|
22
|
+
writeFileSync(file, JSON.stringify(pkg, null, 2) + "\n")
|
|
23
|
+
try {
|
|
24
|
+
const r = spawnSync("npm publish --access public", { shell: true, encoding: "utf8" })
|
|
25
|
+
if (r.stdout) process.stdout.write(r.stdout)
|
|
26
|
+
if (r.stderr) process.stderr.write(r.stderr)
|
|
27
|
+
if (r.status !== 0) {
|
|
28
|
+
const text = `${r.stdout ?? ""}${r.stderr ?? ""}`
|
|
29
|
+
const exists = /E409|previously staged|EPUBLISHCONFLICT|cannot publish over/i.test(text)
|
|
30
|
+
if (exists && process.env.CI === "true") {
|
|
31
|
+
console.warn(`\n! npm 已有 ${PUBLISH_AS}@${pkg.version},按已发布处理(CI)\n`)
|
|
32
|
+
process.exitCode = 0
|
|
33
|
+
} else {
|
|
34
|
+
console.error(
|
|
35
|
+
`\n✗ npm 发布失败:${PUBLISH_AS}@${pkg.version}\n` +
|
|
36
|
+
` 如果 npm 说版本已存在(E409 / EPUBLISHCONFLICT / cannot publish over),先 bump version 再重试。\n` +
|
|
37
|
+
` 原始退出码:${r.status ?? "?"}\n`,
|
|
38
|
+
)
|
|
39
|
+
process.exitCode = 1
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
} finally {
|
|
43
|
+
writeFileSync(file, original)
|
|
44
|
+
}
|
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// release.mjs —— 只从「已经通过 PR 合进默认分支」的提交发布
|
|
3
|
+
//
|
|
4
|
+
// 为什么要有它:push 不是发布,未验证的 push 更不能直接变成用户手里的版本。
|
|
5
|
+
// 这个脚本把第 5~6 步(npm / tag / Release)连同前置条件一起做成可执行、可预演的动作。
|
|
6
|
+
// 它**不会再推默认分支**:功能分支 → PR → CI 通过 → 合并,必须在运行本脚本之前完成。
|
|
7
|
+
//
|
|
8
|
+
// node scripts/release.mjs # 预演:列计划 + 逐项体检,什么都不改
|
|
9
|
+
// node scripts/release.mjs --yes # 真做:tag + push tag + Release
|
|
10
|
+
// node scripts/release.mjs --yes --npm # 再加 npm 发布(有 publish:npm 脚本就用它)
|
|
11
|
+
//
|
|
12
|
+
// 体检项(任一不过就退出码 1,且 --yes 也不动手):
|
|
13
|
+
// ① package.json 有 version ② CHANGELOG 有这一版那一段
|
|
14
|
+
// ③ 工作区干净 ④ 当前在默认分支
|
|
15
|
+
// ⑤ HEAD 与 origin/<默认分支> 完全一致 ⑥ tag 还没被占
|
|
16
|
+
// ⑦ 远端可达 ⑧ 合规检查 / 测试 / pack 预演全过
|
|
17
|
+
//
|
|
18
|
+
// 需要 GitHub token 才能建 Release:
|
|
19
|
+
// GITHUB_TOKEN / GH_TOKEN 环境变量。没有也能跑 —— 它会打印手工建 Release 的命令。
|
|
20
|
+
|
|
21
|
+
import { execSync, execFileSync } from "node:child_process"
|
|
22
|
+
import { readFileSync, existsSync } from "node:fs"
|
|
23
|
+
import { join } from "node:path"
|
|
24
|
+
|
|
25
|
+
const ROOT = process.cwd()
|
|
26
|
+
const argv = new Set(process.argv.slice(2))
|
|
27
|
+
const DRY = !argv.has("--yes")
|
|
28
|
+
const ALSO_NPM = argv.has("--npm")
|
|
29
|
+
|
|
30
|
+
const C = {
|
|
31
|
+
dim: (s) => `\x1b[2m${s}\x1b[0m`,
|
|
32
|
+
green: (s) => `\x1b[32m${s}\x1b[0m`,
|
|
33
|
+
red: (s) => `\x1b[31m${s}\x1b[0m`,
|
|
34
|
+
yellow: (s) => `\x1b[33m${s}\x1b[0m`,
|
|
35
|
+
bold: (s) => `\x1b[1m${s}\x1b[0m`,
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function sh(cmd, opts = {}) {
|
|
39
|
+
return execSync(cmd, { cwd: ROOT, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], ...opts }).trim()
|
|
40
|
+
}
|
|
41
|
+
function shOk(cmd) {
|
|
42
|
+
try {
|
|
43
|
+
sh(cmd)
|
|
44
|
+
return true
|
|
45
|
+
} catch {
|
|
46
|
+
return false
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const problems = []
|
|
51
|
+
const notes = []
|
|
52
|
+
function bad(msg) {
|
|
53
|
+
problems.push(msg)
|
|
54
|
+
}
|
|
55
|
+
function note(msg) {
|
|
56
|
+
notes.push(msg)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// ── ①② 版本与 CHANGELOG ──────────────────────────────────────────────
|
|
60
|
+
|
|
61
|
+
if (!existsSync(join(ROOT, "package.json"))) {
|
|
62
|
+
console.error(C.red("✗ 这里没有 package.json —— 请在插件仓库根目录跑"))
|
|
63
|
+
process.exit(1)
|
|
64
|
+
}
|
|
65
|
+
const pkg = JSON.parse(readFileSync(join(ROOT, "package.json"), "utf8"))
|
|
66
|
+
const version = pkg.version
|
|
67
|
+
const tag = `v${version}`
|
|
68
|
+
if (!version) bad("package.json 里没有 version")
|
|
69
|
+
|
|
70
|
+
/** 这个版本是否已经在 npm 上(本地发过、重跑 workflow 时很常见)。 */
|
|
71
|
+
function npmHasVersion(name, v) {
|
|
72
|
+
try {
|
|
73
|
+
const out = execFileSync("npm", ["view", `${name}@${v}`, "version"], {
|
|
74
|
+
encoding: "utf8",
|
|
75
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
76
|
+
timeout: 20_000,
|
|
77
|
+
}).trim()
|
|
78
|
+
return out === v
|
|
79
|
+
} catch {
|
|
80
|
+
return false
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const npmName = publishAsName(pkg)
|
|
84
|
+
const npmAlready = ALSO_NPM && npmHasVersion(npmName, version)
|
|
85
|
+
|
|
86
|
+
const changelogPath = join(ROOT, "CHANGELOG.md")
|
|
87
|
+
if (!existsSync(changelogPath)) {
|
|
88
|
+
bad("没有 CHANGELOG.md —— 用户感知不到这一版改了什么(UPDATING.md 第 0 步)")
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** 从 CHANGELOG 里抽出某个版本那一节(到下一个 ## 为止) */
|
|
92
|
+
function changelogSection(v) {
|
|
93
|
+
if (!existsSync(changelogPath)) return ""
|
|
94
|
+
const text = readFileSync(changelogPath, "utf8").replace(/\r\n/g, "\n")
|
|
95
|
+
const esc = v.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
|
|
96
|
+
const re = new RegExp(`^##\\s+${esc}\\b[^\\n]*\\n`, "m")
|
|
97
|
+
const m = re.exec(text)
|
|
98
|
+
if (!m) return ""
|
|
99
|
+
const rest = text.slice(m.index + m[0].length)
|
|
100
|
+
const next = rest.search(/^##\s/m)
|
|
101
|
+
const body = (next >= 0 ? rest.slice(0, next) : rest).trim()
|
|
102
|
+
return body
|
|
103
|
+
}
|
|
104
|
+
const section = version ? changelogSection(version) : ""
|
|
105
|
+
if (version && !section) bad(`CHANGELOG.md 里没有「## ${version}」那一段 —— 先写更新内容再发版`)
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 去掉 `> 起因:…` 那段 —— 它写给维护者,不进 Release 正文。
|
|
109
|
+
* (UPDATING 四:一条一行说的是改了什么,`> 起因` 说的是为什么。用户感知不到后者。)
|
|
110
|
+
*/
|
|
111
|
+
function stripCause(md) {
|
|
112
|
+
const out = []
|
|
113
|
+
let skipping = false
|
|
114
|
+
for (const line of md.split("\n")) {
|
|
115
|
+
if (/^>\s*起因[::]/.test(line)) {
|
|
116
|
+
skipping = true
|
|
117
|
+
continue
|
|
118
|
+
}
|
|
119
|
+
if (skipping) {
|
|
120
|
+
if (/^>/.test(line)) continue // 引用块续行
|
|
121
|
+
skipping = false
|
|
122
|
+
if (line.trim() === "") continue
|
|
123
|
+
}
|
|
124
|
+
out.push(line)
|
|
125
|
+
}
|
|
126
|
+
return out.join("\n").replace(/\n{3,}/g, "\n\n").trim()
|
|
127
|
+
}
|
|
128
|
+
const releaseBody = stripCause(section)
|
|
129
|
+
|
|
130
|
+
// ── ③④⑤ git 体检 ───────────────────────────────────────────────────
|
|
131
|
+
|
|
132
|
+
let branch = ""
|
|
133
|
+
let remote = ""
|
|
134
|
+
let defaultBranch = "main"
|
|
135
|
+
try {
|
|
136
|
+
branch = sh("git rev-parse --abbrev-ref HEAD")
|
|
137
|
+
remote = sh("git config --get remote.origin.url")
|
|
138
|
+
} catch {
|
|
139
|
+
bad("这不是一个 git 仓库(或没有 origin)")
|
|
140
|
+
}
|
|
141
|
+
if (branch) {
|
|
142
|
+
try {
|
|
143
|
+
defaultBranch = sh("git symbolic-ref --short refs/remotes/origin/HEAD").replace(/^origin\//, "")
|
|
144
|
+
} catch {}
|
|
145
|
+
if (branch !== defaultBranch) bad(`当前分支是 ${branch},默认分支是 ${defaultBranch} —— 先提 PR 并合并到默认分支`)
|
|
146
|
+
}
|
|
147
|
+
if (branch) {
|
|
148
|
+
const dirty = shOk("git diff --quiet") && shOk("git diff --cached --quiet")
|
|
149
|
+
if (!dirty) bad("工作区有未提交的改动 —— 发布的内容必须和提交一致")
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** owner/repo —— 支持 ssh 与 https 两种 remote */
|
|
153
|
+
function parseRepo(url) {
|
|
154
|
+
const m = String(url).match(/github\.com[:/]([^/]+)\/([^/.]+?)(?:\.git)?$/)
|
|
155
|
+
return m ? { owner: m[1], repo: m[2] } : null
|
|
156
|
+
}
|
|
157
|
+
const gh = remote ? parseRepo(remote) : null
|
|
158
|
+
if (!gh) bad(`remote.origin 不是 GitHub 仓库(${remote || "空"})—— 这个脚本只处理 GitHub`)
|
|
159
|
+
|
|
160
|
+
let behind = 0
|
|
161
|
+
let ahead = 0
|
|
162
|
+
if (gh && branch) {
|
|
163
|
+
try {
|
|
164
|
+
sh("git fetch origin --quiet")
|
|
165
|
+
const counts = sh(`git rev-list --left-right --count origin/${defaultBranch}...HEAD`)
|
|
166
|
+
const [b, a] = counts.split(/\s+/).map(Number)
|
|
167
|
+
behind = b || 0
|
|
168
|
+
ahead = a || 0
|
|
169
|
+
if (behind > 0) bad(`落后远端 ${behind} 个提交 —— 先同步已合并的默认分支,别在旧基线上发版`)
|
|
170
|
+
if (ahead > 0) bad(`本地领先远端 ${ahead} 个提交 —— 不许直接推默认分支;先起分支、提 PR、等 CI 通过后合并`)
|
|
171
|
+
} catch (e) {
|
|
172
|
+
bad(`git fetch 失败,无法证明 HEAD 是远端已合并提交:${String(e.stderr || e.message).trim()}`)
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// ── ⑥ tag 是否已被占 ─────────────────────────────────────────────────
|
|
177
|
+
|
|
178
|
+
let tagExistsLocal = false
|
|
179
|
+
let tagExistsRemote = false
|
|
180
|
+
if (gh) {
|
|
181
|
+
tagExistsLocal = shOk(`git rev-parse -q --verify refs/tags/${tag}`)
|
|
182
|
+
try {
|
|
183
|
+
const out = sh(`git ls-remote --tags origin refs/tags/${tag}`)
|
|
184
|
+
tagExistsRemote = out.length > 0
|
|
185
|
+
} catch {}
|
|
186
|
+
if (tagExistsLocal || tagExistsRemote) {
|
|
187
|
+
bad(`tag ${tag} 已存在 —— 同一个版本号不能发两次;先 bump version`)
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// ── ⑧ 发布前真实检查:测试不是「可选」,失败就不产生任何远端副作用 ─────
|
|
192
|
+
|
|
193
|
+
const preflight = []
|
|
194
|
+
const checkCommand = pkg.scripts?.["check:template"] ? "npm run check:template" : "node scripts/check.mjs"
|
|
195
|
+
if (existsSync(join(ROOT, "scripts", "check.mjs"))) preflight.push(checkCommand)
|
|
196
|
+
if (pkg.scripts?.test) {
|
|
197
|
+
preflight.push("npm test")
|
|
198
|
+
} else if (existsSync(join(ROOT, "tests"))) {
|
|
199
|
+
preflight.push("node --test")
|
|
200
|
+
}
|
|
201
|
+
preflight.push("npm pack --dry-run")
|
|
202
|
+
|
|
203
|
+
if (problems.length === 0) {
|
|
204
|
+
for (const cmd of preflight) {
|
|
205
|
+
console.log(C.dim(`\n发布前检查:${cmd}`))
|
|
206
|
+
try {
|
|
207
|
+
execSync(cmd, { cwd: ROOT, stdio: "inherit" })
|
|
208
|
+
} catch {
|
|
209
|
+
bad(`发布前检查失败:${cmd}`)
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// ── 体检结果 ─────────────────────────────────────────────────────────
|
|
215
|
+
|
|
216
|
+
const CHANGELOG_STEPS = [
|
|
217
|
+
"第 0 步 写 CHANGELOG",
|
|
218
|
+
"第 1 步 bump version",
|
|
219
|
+
"第 2 步 合规检查 / 测试 / pack 预演",
|
|
220
|
+
"第 3 步 提交到功能分支",
|
|
221
|
+
"第 4 步 push 功能分支 → PR → CI 通过 → 合并",
|
|
222
|
+
"第 5 步 npm publish(本脚本的 --npm)",
|
|
223
|
+
"第 6 步 tag + Release(本脚本)",
|
|
224
|
+
"第 7 步 五条验证",
|
|
225
|
+
]
|
|
226
|
+
|
|
227
|
+
console.log(C.bold(`\n发布预演 · ${gh ? `${gh.owner}/${gh.repo}` : "(未知仓库)"} · ${tag}\n`))
|
|
228
|
+
console.log(C.dim("发布流程里这一步在做什么:"))
|
|
229
|
+
console.log(C.dim(" 0 写 CHANGELOG → 1 bump → 2 检查 → 3 功能分支提交 → 4 PR 合并 →"))
|
|
230
|
+
console.log(C.dim(" 5 npm publish → 6 tag+Release → 7 五条验证"))
|
|
231
|
+
console.log(C.dim(` ${CHANGELOG_STEPS.join(" · ")}\n`))
|
|
232
|
+
|
|
233
|
+
if (problems.length) {
|
|
234
|
+
console.log(C.red("✗ 体检没通过,下面这些先解决:\n"))
|
|
235
|
+
for (const p of problems) console.log(C.red(` · ${p}`))
|
|
236
|
+
console.log()
|
|
237
|
+
process.exit(1)
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
console.log(C.green("✓ 体检通过:版本 / CHANGELOG / 工作区 / 已合并 HEAD / 检查与测试 / tag 都没问题\n"))
|
|
241
|
+
for (const n of notes) console.log(C.yellow(` ! ${n}`))
|
|
242
|
+
|
|
243
|
+
// ── 计划 ─────────────────────────────────────────────────────────────
|
|
244
|
+
|
|
245
|
+
const plan = []
|
|
246
|
+
plan.push({ what: `git tag ${tag} && git push origin ${tag}`, why: "第 6 步:只给已合并的 HEAD 打 tag;不推默认分支" })
|
|
247
|
+
plan.push({
|
|
248
|
+
what: `POST /repos/${gh.owner}/${gh.repo}/releases`,
|
|
249
|
+
why: `第 6 步:Release 正文 = CHANGELOG 的 ${version} 段(${releaseBody.split("\n").length} 行,已去掉 > 起因)`,
|
|
250
|
+
})
|
|
251
|
+
if (ALSO_NPM) {
|
|
252
|
+
const script = pkg.scripts?.["publish:npm"] ? "npm run publish:npm" : "npm publish"
|
|
253
|
+
if (npmAlready) {
|
|
254
|
+
plan.push({ what: `npm ${npmName}@${version} 已存在,跳过`, why: "第 5 步:发布已完成;重跑是安全的,不中断 tag/Release" })
|
|
255
|
+
} else {
|
|
256
|
+
plan.push({ what: script, why: "第 5 步:npm 源的用户比 registry 上的 version" })
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
plan.push({ what: "五条验证", why: "第 7 步:维护状态 / npm 版本 / tarball 内容 / magpie plugin list / Release 在" })
|
|
260
|
+
|
|
261
|
+
console.log(C.bold("计划:\n"))
|
|
262
|
+
for (const [i, s] of plan.entries()) {
|
|
263
|
+
console.log(` ${i + 1}. ${s.what}`)
|
|
264
|
+
console.log(C.dim(` ${s.why}`))
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (DRY) {
|
|
268
|
+
console.log(C.yellow("\n这是预演(dry-run),什么都没改。真做:node scripts/release.mjs --yes [--npm]\n"))
|
|
269
|
+
process.exit(0)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// ── 真做 ─────────────────────────────────────────────────────────────
|
|
273
|
+
|
|
274
|
+
const token = process.env.GITHUB_TOKEN || process.env.GH_TOKEN || ""
|
|
275
|
+
console.log()
|
|
276
|
+
|
|
277
|
+
function run(cmd, label) {
|
|
278
|
+
process.stdout.write(` ${label} … `)
|
|
279
|
+
try {
|
|
280
|
+
sh(cmd, { stdio: "pipe" })
|
|
281
|
+
console.log(C.green("ok"))
|
|
282
|
+
return true
|
|
283
|
+
} catch (e) {
|
|
284
|
+
console.log(C.red("失败"))
|
|
285
|
+
console.log(C.dim(String(e.stderr || e.message).trim().split("\n").slice(0, 4).join("\n")))
|
|
286
|
+
return false
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const tagged = run(`git tag ${tag} && git push origin ${tag}`, `tag ${tag}`)
|
|
291
|
+
|
|
292
|
+
let released = false
|
|
293
|
+
|
|
294
|
+
/** 这个仓库有没有 release.yml(有的话推 tag 就会自动建 Release,我们不该再建一遍) */
|
|
295
|
+
const hasReleaseWorkflow = existsSync(join(ROOT, ".github", "workflows", "release.yml"))
|
|
296
|
+
|
|
297
|
+
/** GET /releases/tags/<tag>,返回 html_url 或 null */
|
|
298
|
+
function existingRelease() {
|
|
299
|
+
if (!token) return null
|
|
300
|
+
try {
|
|
301
|
+
const out = execFileSync(
|
|
302
|
+
"curl",
|
|
303
|
+
[
|
|
304
|
+
"-sS",
|
|
305
|
+
"--max-time",
|
|
306
|
+
"20",
|
|
307
|
+
"-H",
|
|
308
|
+
`Authorization: Bearer ${token}`,
|
|
309
|
+
"-H",
|
|
310
|
+
"Accept: application/vnd.github+json",
|
|
311
|
+
"-H",
|
|
312
|
+
"User-Agent: magpie-plugin-release",
|
|
313
|
+
`https://api.github.com/repos/${gh.owner}/${gh.repo}/releases/tags/${tag}`,
|
|
314
|
+
],
|
|
315
|
+
{ encoding: "utf8" },
|
|
316
|
+
)
|
|
317
|
+
const j = JSON.parse(out)
|
|
318
|
+
return j?.html_url ?? null
|
|
319
|
+
} catch {
|
|
320
|
+
return null
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
if (token) {
|
|
324
|
+
// ⚠ 这里**故意不用 node 的 fetch**。实测(Windows + 本机证书库)node 的 https 栈打
|
|
325
|
+
// api.github.com 会报 `fetch failed` / `UNABLE_TO_GET_ISSUER_CERT_LOCALLY`,
|
|
326
|
+
// 而系统 curl 走 schannel 正常。发布这一步失败会留下「tag 有了、Release 没有」的
|
|
327
|
+
// 半截状态,不值得赌运行时。
|
|
328
|
+
// 有 release.yml 的仓库:推 tag 就会自动建 Release,我们自己再建一遍会 422。
|
|
329
|
+
// 没 workflow 的:先查一次(可能上次手工建过 / 上次跑到一半),没有才建。
|
|
330
|
+
if (hasReleaseWorkflow) {
|
|
331
|
+
const url = existingRelease()
|
|
332
|
+
if (url) {
|
|
333
|
+
console.log(` Release … ${C.green("ok")}(release.yml 已建)${url}`)
|
|
334
|
+
} else {
|
|
335
|
+
console.log(C.yellow(" Release … 交给 release.yml(推 tag 已触发),约 1 分钟后按第 ④ 条确认"))
|
|
336
|
+
}
|
|
337
|
+
released = true
|
|
338
|
+
} else if (existingRelease()) {
|
|
339
|
+
console.log(` Release … ${C.green("ok")}(这个 tag 的 Release 已经存在,不重复建)`)
|
|
340
|
+
released = true
|
|
341
|
+
} else {
|
|
342
|
+
const payload = JSON.stringify({
|
|
343
|
+
tag_name: tag,
|
|
344
|
+
target_commitish: branch,
|
|
345
|
+
name: version,
|
|
346
|
+
body: releaseBody,
|
|
347
|
+
draft: false,
|
|
348
|
+
prerelease: false,
|
|
349
|
+
})
|
|
350
|
+
try {
|
|
351
|
+
const out = execFileSync(
|
|
352
|
+
"curl",
|
|
353
|
+
[
|
|
354
|
+
"-sS",
|
|
355
|
+
"-X",
|
|
356
|
+
"POST",
|
|
357
|
+
"--max-time",
|
|
358
|
+
"40",
|
|
359
|
+
"-H",
|
|
360
|
+
`Authorization: Bearer ${token}`,
|
|
361
|
+
"-H",
|
|
362
|
+
"Accept: application/vnd.github+json",
|
|
363
|
+
"-H",
|
|
364
|
+
"User-Agent: magpie-plugin-release",
|
|
365
|
+
"-H",
|
|
366
|
+
"Content-Type: application/json; charset=utf-8",
|
|
367
|
+
"--data-binary",
|
|
368
|
+
"@-",
|
|
369
|
+
`https://api.github.com/repos/${gh.owner}/${gh.repo}/releases`,
|
|
370
|
+
],
|
|
371
|
+
{ input: payload, encoding: "utf8" },
|
|
372
|
+
)
|
|
373
|
+
let body = {}
|
|
374
|
+
try {
|
|
375
|
+
body = JSON.parse(out)
|
|
376
|
+
} catch {}
|
|
377
|
+
if (body?.html_url) {
|
|
378
|
+
console.log(` Release … ${C.green("ok")} ${body.html_url}`)
|
|
379
|
+
released = true
|
|
380
|
+
} else {
|
|
381
|
+
console.log(` Release … ${C.red("失败")} ${body?.message || String(out).slice(0, 160)}`)
|
|
382
|
+
}
|
|
383
|
+
} catch (e) {
|
|
384
|
+
const detail = String(e.stderr || e.message).trim().slice(0, 160)
|
|
385
|
+
console.log(` Release … ${C.red("失败")} ${detail}`)
|
|
386
|
+
if (/curl/i.test(detail)) {
|
|
387
|
+
console.log(C.dim(" 这台机器没有 curl?手工建:GitHub → Releases → Draft a new release"))
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
} else {
|
|
392
|
+
console.log(C.yellow(" Release … 跳过(没有 GITHUB_TOKEN / GH_TOKEN)"))
|
|
393
|
+
console.log(C.dim(" 手工建:GitHub → Releases → Draft a new release → 选 " + tag))
|
|
394
|
+
console.log(C.dim(" 正文直接把 CHANGELOG 里 " + version + " 那一段粘过去"))
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
let published = null
|
|
398
|
+
if (ALSO_NPM) {
|
|
399
|
+
const script = pkg.scripts?.["publish:npm"] ? "npm run publish:npm" : "npm publish"
|
|
400
|
+
console.log(C.dim(`\n ${script}(npm 登记有 1~2 分钟延迟,别在这窗口里重试)\n`))
|
|
401
|
+
try {
|
|
402
|
+
execSync(script, { cwd: ROOT, stdio: "inherit" })
|
|
403
|
+
published = true
|
|
404
|
+
} catch {
|
|
405
|
+
published = false
|
|
406
|
+
console.log(C.red(" npm 发布失败"))
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// ── 第 7 步留给人看的清单 ─────────────────────────────────────────────
|
|
411
|
+
|
|
412
|
+
/** npm 上的包名:显示名 ≠ 包名时藏在 publish 脚本的 PUBLISH_AS 里。 */
|
|
413
|
+
function publishAsName(manifest) {
|
|
414
|
+
for (const rel of ["scripts/publish-npm.mjs", "publish-npm.mjs"]) {
|
|
415
|
+
const file = join(ROOT, rel)
|
|
416
|
+
if (!existsSync(file)) continue
|
|
417
|
+
const src = readFileSync(file, "utf8")
|
|
418
|
+
const m = src.match(/(?:PUBLISH_AS|NPM_NAME)\s*=\s*["']([^"']+)["']/)
|
|
419
|
+
if (m) return m[1]
|
|
420
|
+
}
|
|
421
|
+
return manifest.name
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
console.log(C.bold("\n第 7 步 · 五条验证(逐条跑,别只看版本号):\n"))
|
|
425
|
+
const name = publishAsName(pkg)
|
|
426
|
+
console.log(" ⓪ node scripts/check-maintenance.mjs # 维护状态一致?")
|
|
427
|
+
console.log(` ① npm view ${name} version # 已是 ${version}?`)
|
|
428
|
+
console.log(` ② curl -s $(npm view ${name}@${version} dist.tarball) | tar -xzOf - package/CHANGELOG.md | head -5`)
|
|
429
|
+
console.log(` # 要看到新内容,不只看版本号`)
|
|
430
|
+
console.log(` ③ magpie plugin list # 本机认得新版本 / 新 commit?`)
|
|
431
|
+
console.log(` ④ curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \\`)
|
|
432
|
+
console.log(` https://api.github.com/repos/${gh.owner}/${gh.repo}/releases/tags/${tag}`)
|
|
433
|
+
console.log(` # 200 且 "draft": false`)
|
|
434
|
+
console.log()
|
|
435
|
+
console.log(
|
|
436
|
+
C.dim(
|
|
437
|
+
"没过这五条,不要说「已发布」。detail: https://github.com/MYCF711/magpie-plugin-scaffold/blob/main/UPDATING.md\n",
|
|
438
|
+
),
|
|
439
|
+
)
|
|
440
|
+
|
|
441
|
+
const failed = !tagged || (ALSO_NPM && !published)
|
|
442
|
+
process.exit(failed ? 1 : 0)
|