window-warmup 0.1.1: models hidden by a routing group are sent, not called missing (#712)
A model a routing group keeps out of /v1/models (Group.Unlists, which magpie still serves) was taken for gone: the page's model list was disabled and tasks failed with "模型 X 已不在 Magpie 的模型列表里" without sending. window-warmup now also reads each provider's chosen models (providers.json "models") and counts a model as present when it is in /v1/models or chosen: in the catalog (fetchCatalog/accountsOf), the auto-model pick and a fixed model's send check. A model known only from chosen has no effort metadata, so it goes without reasoning_effort. A model in neither is still refused. Two gateway-unreachable tests now check the error is passed through rather than its exact wording, which varies with Bun's socket timing. Review: window-warmup lives in this repo since #661 (packages/window-warmup) and is published to the dist branch, not npm. 0.1.1 is free (main and dist have 0.1.0). Not mover-backed. No new host, credential, dependency or workflow change; providers.json was already read and only its "models" list is added (no key text). magpie's Group.Unlists doc confirms "a request naming one is still served". dist/plugin.js matches a rebuild of src (dist.test.ts, run on Bun 1.4.2). Run at 8f10bc07 on mote (Bun 1.4.2, HOME a temp dir as CI): bun test packages/window-warmup 246 pass 1 skip (browser smoke) 0 fail; bun scripts/check.mjs exit 0. PR CI green. Broken by hand: layer 3 without chosen, candidates without chosen, the fixed model check without chosen, the auto pick needing /v1/models, and entryOf dropping "models" each turn their new test red. fetchCatalog not passing chosen is caught only by the dist byte check, not by a behaviour test. Not tried: a real magpie with an unlisting group (the author ran it on a Docker magpie with zcode GLM-5.3). A switched-off provider's chosen models also count as present now, so its old tasks send and get the gateway's error instead of the local one.
This commit is contained in:
@@ -42,10 +42,10 @@ bun test tests/scheduler.test.ts -t "关键字" # 按用例名过滤
|
||||
核心模块分工:
|
||||
|
||||
- **`src/rules.ts`**:纯时间计算(每天定时含时区 / 夏令时、固定间隔、窗口到期 = `resetsAt` + 30s、补发回看 48 小时)、任务校验与探活消息常量。**页面直接打包本模块**(编辑区预览与调度器用同一个 `upcoming`),所以它只能依赖 `types.ts`,不得引入 `node:*` 或网关模块(`tests/rules.test.ts` 有守卫)。`types.ts` 同理,另放各层共用的 `errText`。
|
||||
- **`src/scheduler.ts`**:所有改状态入口(tick / upsert / remove / runNow)走同一操作队列串行;发送本体在队列外但登记在 inflight,保证同任务不重叠、删除后异步完成不会把记录写回。单定时器,最长睡 10 分钟后重算。窗口任务发送前后让 magpie 单卡重读额度(`readAt` 早于上次发送的空闲读数不信)。判断逻辑集中在几个私有函数:`pendingFixedDue`(定时/间隔该不该补发)、`currentWindowDue` + `windowReady`(窗口到期且本轮未尝试,纯计算);`lastReset` 只在 `recordResets` 里写(额度刷新后与 `rearm` 时),不要再把写盘塞回查询路径。
|
||||
- **`src/scheduler.ts`**:所有改状态入口(tick / upsert / remove / runNow)走同一操作队列串行;发送本体在队列外但登记在 inflight,保证同任务不重叠、删除后异步完成不会把记录写回。单定时器,最长睡 10 分钟后重算。窗口任务发送前后让 magpie 单卡重读额度(`readAt` 早于上次发送的空闲读数不信)。判断逻辑集中在几个私有函数:`pendingFixedDue`(定时/间隔该不该补发)、`currentWindowDue` + `windowReady`(窗口到期且本轮未尝试,纯计算);`lastReset` 只在 `recordResets` 里写(额度刷新后与 `rearm` 时),不要再把写盘塞回查询路径。`resolveSend` 的存在性判定是 `/v1/models` ∪ chosen:模型被路由组藏出 `/v1/models` 时靠 chosen 照发,拿不到档位元数据就按「不适用」不带 `reasoning_effort`。
|
||||
- **`src/gateway.ts`**:与本地 magpie 网关(`MAGPIE_ADDR`,默认 `127.0.0.1:3425`)通信:`/v1/models`、`/v1/magpie/quotas`(含单卡 refresh)、`/v1/chat/completions`;思考档位 `pickEffort` 每次发送前现算。
|
||||
- **`src/accounts.ts`**:**统一账号规则的唯一实现**——`/api/catalog` 与调度器共用(账号合并、key 卡挂回、按账号收窄模型、`pickAutoModel`)。禁止在别处另写一套,否则页面显示与实际发送会不一致。
|
||||
- **`src/magpie-files.ts`**:只读 magpie 的 `providers.json` 与模型缓存。`readDefaultKeyDirectory` / `readDefaultModelCache` 是目录与调度器共用的默认读取入口;`xdgConfigHome` 是插件数据目录、旧包迁移与 Magpie 配置目录共用的 XDG 解析(只认绝对路径)。**红线**:key 原文只允许在 `readKeyDirectory` 内「读 → 算指纹(sha256 前 5 字节 hex)」后即丢,不得进入返回值、日志、错误信息或缓存;JSON 解析失败一律换成固定文案 `BROKEN_PROVIDERS_MESSAGE`。
|
||||
- **`src/magpie-files.ts`**:只读 magpie 的 `providers.json` 与模型缓存。`readDefaultKeyDirectory` / `readDefaultModelCache` 是目录与调度器共用的默认读取入口;`xdgConfigHome` 是插件数据目录、旧包迁移与 Magpie 配置目录共用的 XDG 解析(只认绝对路径)。**红线**:key 原文只允许在 `readKeyDirectory` 内「读 → 算指纹(sha256 前 5 字节 hex)」后即丢,不得进入返回值、日志、错误信息或缓存;JSON 解析失败一律换成固定文案 `BROKEN_PROVIDERS_MESSAGE`。除 key 指纹与 `accountModels` 外还取每供应商的 `models`(chosen 启用清单):模型被路由组 `unlists` 藏出 `/v1/models` 时它是唯一的存在性来源。
|
||||
- **`src/store.ts`**:`state.json` 临时文件 + rename 落盘,损坏时拒绝启动且绝不改动原文件;`service.lock` 原子排他创建 + pid 活性判断(活性不明不抢占,pid 存活但端口无本插件应答视为陈旧锁接管);一次性从旧包 `scheduled-messages` 迁移 `state.json`。
|
||||
|
||||
## 供应商 → 账号 → 模型级联(无官方接口的绕行路径)
|
||||
@@ -63,9 +63,9 @@ Magpie 不提供「供应商 / 账号 / 每账号可用模型」的列表接口
|
||||
- **模型三层过滤**(`modelsForAccount`):
|
||||
1. 缓存 `Keys` 可见性(仅缓存存在且账号是指纹时);
|
||||
2. `accountModels[指纹或小写账号名]`,比对不带供应商前缀的 id;
|
||||
3. 必须仍在 `/v1/models` 里。
|
||||
3. 仍在 `/v1/models` 里,或仍在 chosen 启用清单(`providers.json` 的 `models`)里——后者接住被路由组 `unlists` 藏出 `/v1/models` 的模型。
|
||||
|
||||
无缓存时退化为 `/v1/models` 全量(`perAccount: false`),两者皆无时返回 `list: null`。
|
||||
无缓存时候选退化为 `/v1/models` ∪ chosen(按完整 id 去重,`perAccount: false`),两者皆无时返回 `list: null`。
|
||||
- **发送头**(`pinFor`):订阅账号原样发;key 指纹在启用 key 不少于 2 把(目录读不出时按 2 把处理)时发 `<P>#<指纹>`,只有 1 把时不带头,因为此时候选 ID 就是供应商 id;其余值(旧任务存的 key 名)原样发。非 ASCII 头值按 UTF-8 字节写入(`send`)。
|
||||
- **文件位置**(`magpieDirs`):从安装路径 `<配置目录>/plugins/node_modules/@magpie-community/page-window-warmup/dist` 上溯得到配置目录;旧独立包名 `node_modules/magpie-window-warmup/dist` 也识别(升级用户的旧路径)。再据此区分安装版和便携版的缓存目录。`tests/magpie-files.test.ts` 覆盖了新作用域路径、旧路径、Windows 与便携版。
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ magpie plugin add @magpie-community/page-window-warmup
|
||||
模型选「自动」时,任务里不存选中值,每次发送前按统一账号规则现选(目录与调度器同一份实现):
|
||||
|
||||
- 可选模型以 Magpie 的模型缓存为基础,按账号收窄:key 账号只留该 key 能看到的模型(缓存里的 Keys 指纹),providers.json 的 accountModels 给账号配了清单就只留清单内的;
|
||||
- 没有模型缓存的供应商(插件型订阅供应商本来就不写缓存)退而用 `/v1/models` 里的全部模型,此时不按账号区分;连 `/v1/models` 都没有该供应商的模型 → 本次失败「供应商 X 还没有模型列表,无法自动选模型」;
|
||||
- 没有模型缓存的供应商(插件型订阅供应商本来就不写缓存)退而用 `/v1/models` ∪ 启用清单的全部模型,此时不按账号区分;两者都空 → 本次失败「供应商 X 还没有模型列表,无法自动选模型」;
|
||||
- 收窄后按模型 id 最后一段的规格词排序 `flash < lite < mini < nano < small < turbo`(整词匹配、不分大小写,"minimax" 不算 mini),都不含的排最后;同档按 id 数字感知降序(新版本优先),取第一个;
|
||||
- 收窄后一个模型都不剩 → 本次失败「账号 X 没有可用模型」。
|
||||
|
||||
@@ -43,6 +43,7 @@ magpie plugin add @magpie-community/page-window-warmup
|
||||
|
||||
思考档位同样不保存在任务里,每次发送前从当次 `/v1/models` 取该模型自己的档位列表现算,模型配置变了下次发送自动跟随:
|
||||
|
||||
- 模型不在 `/v1/models` 里(被路由组藏掉、靠启用清单兜底发送)时拿不到档位元数据,按「不适用」处理,不带 `reasoning_effort` 照发;
|
||||
- 模型不支持思考或档位列表为空 → 不带 `reasoning_effort`;
|
||||
- 档位里有 `none`(`disabled`/`off` 视同)→ `reasoning_effort: "none"`,能关就关;
|
||||
- 否则取该模型列表里最低一档(已知名按 `minimal < low < medium < high < xhigh < max` 排,纯数字思考预算低于所有命名档;全部是不知名的取列表第一档)。
|
||||
@@ -59,13 +60,13 @@ magpie plugin add @magpie-community/page-window-warmup
|
||||
|
||||
## 供应商 → 账号 → 模型:级联选择是怎么拼出来的
|
||||
|
||||
Magpie 没有给插件提供「列出供应商、每个供应商有哪些账号、每个账号能用哪些模型」的接口:`/v1/models` 只有一张不分账号的模型清单,`/v1/magpie/quotas` 只有额度卡,账号与 key 的对应关系只存在于 Magpie 自己的配置和缓存文件里。插件把下面四处现成数据拼成一份目录:
|
||||
Magpie 没有给插件提供「列出供应商、每个供应商有哪些账号、每个账号能用哪些模型」的接口:`/v1/models` 只有一张不分账号的模型清单,`/v1/magpie/quotas` 只有额度卡,账号与 key 的对应关系只存在于 Magpie 自己的配置和缓存文件里。插件把下面四处现成数据拼成一份目录,每一级的做法与它可能对不上的情况都在下面说明;拼不上的后果统一收在[「已知限制」](#已知限制插件拿不到的信息)的数据缺口表里。
|
||||
|
||||
| 来源 | 取用内容 | 用在哪一级 |
|
||||
|---|---|---|
|
||||
| 网关 `GET /v1/models` | 全部模型 id、`owned_by`、思考档位 | 供应商归属、模型的最终白名单、思考档位 |
|
||||
| 网关 `GET /v1/models` | 全部模型 id、`owned_by`、思考档位 | 供应商归属、模型的存在性判定、思考档位 |
|
||||
| 网关 `GET /v1/magpie/quotas?kept=1` | 额度卡:`provider`、`user`、`windows`、`readAt` | 订阅账号、窗口;补充只有额度卡的供应商 |
|
||||
| 文件 `<配置目录>/providers.json` | 每个供应商启用的 key(只算指纹)、key 名、`accountModels` | key 账号;模型第 2 层过滤 |
|
||||
| 文件 `<配置目录>/providers.json` | 每个供应商启用的 key(只算指纹)、key 名、`accountModels`、`models`(启用的模型清单) | key 账号;模型第 2 层过滤;第 3 层存在性的兜底来源 |
|
||||
| 文件 `<缓存目录>/models/<供应商id>.json` | Magpie 上次「获取模型」的结果:每个模型的 `ID` 与 `Keys`(能看到它的 key 指纹) | 模型第 1 层过滤(按 key 可见性) |
|
||||
|
||||
服务端 `/api/catalog`(`src/gateway.ts` 的 `fetchCatalog`)一次算好整份目录,页面打开时拉取、之后每 60 秒刷新一次;调度器每次发送前用**同一份规则**(`src/accounts.ts`)重新计算,所以页面上显示的账号、模型和「自动」选中项,与实际发送时一致。
|
||||
@@ -74,6 +75,7 @@ Magpie 没有给插件提供「列出供应商、每个供应商有哪些账号
|
||||
|
||||
- `/v1/models` 里的每个模型按 `owned_by`(真正接住请求的一方)归到供应商,没有 `owned_by` 时取 id 的第一段;`group/` 路由组不列出,归不到供应商的模型丢弃(任务必须有供应商)。
|
||||
- 额度卡上出现的供应商也列出,即使它暂时没有模型;远程 key 卡的 `<p>/~keys` 归一成 `<p>`。
|
||||
- **可能对不上**:额度卡上的供应商名是订阅 agent 名,在 Magpie 里改过名的供应商,目录里的账号与模型可能因此匹配不上。
|
||||
|
||||
### 2. 账号
|
||||
|
||||
@@ -81,12 +83,13 @@ Magpie 内部用「候选 ID」区分同一供应商下的多把 key 和多个
|
||||
|
||||
- **key 账号**:读 `providers.json`,取该供应商的第一把 `key` 与 `keys[]` 中未停用(`off` 不为 `true`)的 key,对 key 原文做 sha256 并取前 5 字节的小写 hex 作为指纹。这与 Magpie `internal/provider/keys.go` 的 keyID 算法一致,因此能同时对上模型缓存里的 `Keys` 和发送时的候选 ID。账号 id 是指纹,显示名是 key 名(没起名则显示指纹),按指纹去重。**key 原文算完指纹即丢。**
|
||||
- **订阅账号**:该供应商名下、非 key 卡的额度卡的 `user`,去重后原样作为账号 id 与显示名。
|
||||
- **可能对不上**:指纹算法是插件与 Magpie 内部实现的约定,哪天 Magpie 改了 keyID 算法,key 目录、模型缓存可见性、发送头三处会同时失配,key 账号集体失效,需要插件发版跟随。
|
||||
|
||||
窗口挂到账号上的方式:
|
||||
|
||||
- 订阅账号按 `user` 精确匹配额度卡。
|
||||
- key 账号先用指纹反查 key 名,再找 `<p>/~keys` 卡里 `user` 与 key 名相同(不分大小写),或以「名字 · 」开头(同名多把 key 时的打码形态)的那张。远程 Magpie 会给每把网关 key 单独发这样一张限额卡。
|
||||
- 没起名的 key 无法匹配:卡名是打码后的样子,插件算不出来,这类账号没有窗口。
|
||||
- 没起名的 key 无法匹配:额度卡上只有打码后的名字,插件算不出对应关系,这类账号没有窗口。
|
||||
- `unlimited: true` 的「不限额」占位不算窗口。没有窗口的账号不提供「窗口重置后」触发。
|
||||
|
||||
账号下拉没有「自动」项,所有任务都必须选账号。旧任务里存的账号值原样保留并标注「不在当前账号列表」;存的是 key 名的照样按名字发送,兼容升级。
|
||||
@@ -97,9 +100,11 @@ Magpie 内部用「候选 ID」区分同一供应商下的多把 key 和多个
|
||||
|
||||
1. **按 key 可见性**:仅当该供应商有模型缓存且账号是 key 指纹时,只留 `Keys` 为空(所有 key 可见)或包含这把指纹的模型。
|
||||
2. **按 `accountModels`**:`providers.json` 给这个账号(键为 key 指纹,或小写的订阅账号名)配了模型清单时,只留清单内的(比对不带供应商前缀的 id)。
|
||||
3. **按当前存在**:只留此刻仍在 `/v1/models` 里的。
|
||||
3. **按当前存在**:只留此刻仍在 `/v1/models` 里,**或仍在 `providers.json` 的 `models`(用户启用的清单)里**的。后者接住被模型路由组(`unlists: true`)藏出 `/v1/models` 的模型——Magpie 不区分「藏起来」和「下线了」,进了路由组的模型从公开清单消失,但网关仍然接受发送,用户明确启用的清单是它还在的唯一凭据。
|
||||
|
||||
没有模型缓存的供应商(插件型订阅供应商本来就不写缓存)退而用 `/v1/models` 里该供应商的全部模型,跳过第 1 层,所有账号共用同一份清单。缓存和 `/v1/models` 都没有该供应商的模型时,模型下拉禁用,并提示到 Magpie 里「获取模型」。
|
||||
没有模型缓存的供应商退化为 `/v1/models` ∪ 启用清单的全部模型(去重),所有账号共用同一份清单,跳过第 1 层——模型缓存文件只有 key 型供应商才写,插件型订阅供应商(zcode、各类 OpenCode 登录插件)没有按账号的模型可见性,自动选出的模型该账号可能无权使用,发送时才发现。缓存、`/v1/models` 与启用清单三者都拿不到该供应商的模型时,模型下拉禁用,并提示到 Magpie 里「获取模型」。
|
||||
|
||||
**可能对不上**:模型缓存是 Magpie 上次「获取模型」的快照,里面的模型 id 形态可能和 `/v1/models` 漂移(例如缓存里是 `gemini-3.8-flash-high` 等档位形态,清单里是 `gemini-3.8-flash`),全部对不上时该供应商的模型列表为空;去 Magpie 重新「获取模型」刷新缓存即可恢复。
|
||||
|
||||
下拉首项「自动」按「模型与思考档位」一节的规则,从过滤后的列表里现选,并在旁边附注当前会选中哪个模型。
|
||||
|
||||
@@ -129,12 +134,27 @@ Magpie 内部用「候选 ID」区分同一供应商下的多把 key 和多个
|
||||
- **providers.json 红线**:key 原文只在「读文件 → 算指纹」这一步存在,不保存、不写日志、不进错误信息、不外传;配置文件损坏时报固定文案,绝不把文件内容带出来。
|
||||
- **模型缓存**只读每个模型的 `ID` 与 `Keys`(key 指纹),不含 key 原文与 key 名。
|
||||
|
||||
模型下拉被禁用并提示「还没有这个供应商的模型列表。请到 Magpie『供应商』里打开它,点『获取模型』。」时,就是第 2、3 样都拿不到该供应商的模型。
|
||||
模型下拉被禁用并提示「还没有这个供应商的模型列表。请到 Magpie『供应商』里打开它,点『获取模型』。」时,就是 `/v1/models`、providers.json 的启用清单与模型缓存三样都拿不到该供应商的模型。
|
||||
|
||||
## 已知限制:插件拿不到的信息
|
||||
|
||||
插件的判断都来自 magpie 暴露的额度卡、模型列表与本地配置。下面这些情况它无法确认或纠正,任务可能失败、没有按预期开窗,或仍然产生费用。
|
||||
|
||||
**数据缺口:Magpie 没有的接口,插件是怎么绕的,哪里可能翻车**
|
||||
|
||||
核心功能只需要「到点给某供应商、某账号的某模型发条消息」,但 Magpie 没有账号与模型目录接口(一个 `GET /v1/magpie/catalog` 就能根治),插件只能按上文的方式拼。拼的过程依赖的每个缺口如下,这些也是希望 Magpie 未来补上的接口:
|
||||
|
||||
| Magpie 没提供的 | 插件的替代做法 | 可能出现的问题 |
|
||||
|---|---|---|
|
||||
| 账号与模型目录接口 | 从 `/v1/models`、额度卡、`providers.json`、模型缓存四份数据拼目录 | 下面各行的一切错配场景 |
|
||||
| `/v1/models` 里被路由组隐藏的模型(`unlists: true`,清单不显示但发送仍接受) | 用 `providers.json` 的启用清单兜底判定存在(0.1.1 起) | 旧版本表现为模型下拉禁用、任务报「已不在模型列表」;启用清单也过期时(模型真下线但没重新获取)会照发并收到上游报错 |
|
||||
| 插件型订阅供应商的按账号模型可见性(缓存文件只有 key 型供应商写) | 不按账号区分,列出全部模型 | 自动选出的模型该账号可能无权使用,发送时才失败 |
|
||||
| 缓存模型 id 与清单 id 的统一 | 直接比对 id | 形态漂移时该供应商模型列表为空,去 Magpie 重新「获取模型」可恢复 |
|
||||
| key 与账号对应关系的接口 | sha256 指纹三方对齐(key 目录、缓存 `Keys`、发送头) | Magpie 更改 keyID 算法时三处同时失配,key 账号集体失效 |
|
||||
| 起名 key 之外的额度卡匹配方式 | 指纹反查 key 名再按卡名匹配 | 没起名的 key 匹配不上(卡名是打码形态),账号没有窗口,窗口任务会一直报「账号的额度里没有这个窗口」 |
|
||||
| 订阅供应商的稳定 id | 额度卡上的供应商名按订阅 agent 名对 | 在 Magpie 里改过名的供应商,目录里的账号与模型可能匹配不上 |
|
||||
| 发送侧「模型不存在」的可判别错误 | 只能按 HTTP 错误原文记录 | 「已下线」和「该账号无权使用」在执行记录里无法区分 |
|
||||
|
||||
**计费与窗口**
|
||||
|
||||
- **发送成功不等于窗口已开始**:有的供应商要累计到一定用量才开窗,或只在特定模型、端点上计入订阅用量;插件只能记录「已发送」,无法验证窗口是否真的开了。
|
||||
@@ -146,15 +166,12 @@ Magpie 内部用「候选 ID」区分同一供应商下的多把 key 和多个
|
||||
**额度与账号信息**
|
||||
|
||||
- **供应商没有额度卡**(常见于插件型订阅供应商):看不到窗口,只能用每天定时或固定间隔。
|
||||
- **没有起名的远程 key**:额度卡上的名字是打码形态,插件算不出对应关系,窗口任务会一直报「账号的额度里没有这个窗口」。
|
||||
- **额度卡瞬断或 Magpie 没在运行**:插件静默等下一轮刷新,可能错过一次重置;Magpie 关闭期间不执行,重开后每个任务最多补发一次。
|
||||
- **额度读数的时效**:依赖 Magpie 的缓存与单卡重读限频(30 秒),拿不到新读数时按快照时刻判断,发送可能提前或推迟。
|
||||
- **改过名的订阅供应商**:额度卡的供应商 id 是 agent 名,改名后目录里的账号与模型可能匹配不上。
|
||||
|
||||
**模型与账号权限**
|
||||
|
||||
- **没有模型缓存的供应商**(如插件型订阅供应商):不按账号区分模型,所有账号共用 `/v1/models` 的完整清单;自动选出的模型该账号可能无权使用,发送时才失败。
|
||||
- **模型清单是旧快照**:来自 Magpie 上次「获取模型」的结果,过期后可能选中已下线或账号不可见的模型;账号和模型对不上时去 Magpie 重新「获取模型」再刷新本页。
|
||||
- **模型清单是旧快照**:启用清单与缓存都来自 Magpie 上次「获取模型」的结果,过期后可能选中已下线或账号不可见的模型;模型真下线但没重新获取时,插件会照发并如实记录上游报错(与 Magpie 自身行为一致),重新获取后启用清单剔除下架模型,插件恢复「已不在模型列表」的拦截。账号和模型对不上时去 Magpie 重新「获取模型」再刷新本页。
|
||||
- **思考档位只是建议**:按模型列表推导出的 `reasoning_effort`,上游不认识时不保证生效。
|
||||
- **自动选模型只看名字**:按 flash / lite / mini 等规格词挑最省的,不检查账号权限、最低 token 要求或区域限制;`max_tokens: 16` 被上游拒绝时本次失败。
|
||||
|
||||
|
||||
+52
-22
@@ -65,22 +65,34 @@ function windowsForAccount(provider, account, quotas, keyDir) {
|
||||
const card = quotas.find((q) => q.provider === provider && q.viaKey && (q.account.toLowerCase() === low || q.account.startsWith(`${name}${MASKED_SEP}`)));
|
||||
return card ? keepWindows(card.windows) : [];
|
||||
}
|
||||
function candidatesOf(provider, models, cache) {
|
||||
function candidatesOf(provider, models, cache, chosen) {
|
||||
if (cache) {
|
||||
return cache.map((c) => ({ full: `${provider}/${c.id}`, bare: c.id, keys: c.keys }));
|
||||
}
|
||||
const prefix = `${provider}/`;
|
||||
return models.map((m) => ({
|
||||
full: m,
|
||||
bare: m.startsWith(prefix) ? m.slice(prefix.length) : m,
|
||||
keys: null
|
||||
}));
|
||||
const seen = new Set;
|
||||
const out = [];
|
||||
for (const m of models) {
|
||||
if (seen.has(m))
|
||||
continue;
|
||||
seen.add(m);
|
||||
out.push({ full: m, bare: m.startsWith(prefix) ? m.slice(prefix.length) : m, keys: null });
|
||||
}
|
||||
for (const bare of chosen) {
|
||||
const full = `${provider}/${bare}`;
|
||||
if (seen.has(full))
|
||||
continue;
|
||||
seen.add(full);
|
||||
out.push({ full, bare, keys: null });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
function modelsForAccount(provider, account, input) {
|
||||
const hasCache = input.cache !== null;
|
||||
if (!hasCache && input.models.length === 0)
|
||||
if (!hasCache && input.models.length === 0 && input.chosen.length === 0) {
|
||||
return { list: null, perAccount: false };
|
||||
let cands = candidatesOf(provider, input.models, input.cache);
|
||||
}
|
||||
let cands = candidatesOf(provider, input.models, input.cache, input.chosen);
|
||||
if (hasCache && isFingerprint(account)) {
|
||||
cands = cands.filter((c) => !c.keys || c.keys.length === 0 || c.keys.includes(account));
|
||||
}
|
||||
@@ -90,7 +102,10 @@ function modelsForAccount(provider, account, input) {
|
||||
const set = new Set(allowed);
|
||||
cands = cands.filter((c) => set.has(c.bare));
|
||||
}
|
||||
const present = new Set(input.models);
|
||||
const present = new Set([
|
||||
...input.models,
|
||||
...input.chosen.map((bare) => `${provider}/${bare}`)
|
||||
]);
|
||||
const list = cands.map((c) => c.full).filter((id) => present.has(id)).sort();
|
||||
return { list, perAccount: hasCache };
|
||||
}
|
||||
@@ -137,10 +152,13 @@ function pickModels(provider, account, input) {
|
||||
};
|
||||
}
|
||||
function autoAccountModelsOf(provider, input) {
|
||||
if (input.cache === null && input.models.length === 0)
|
||||
if (input.cache === null && input.models.length === 0 && input.chosen.length === 0)
|
||||
return null;
|
||||
const present = new Set(input.models);
|
||||
return candidatesOf(provider, input.models, input.cache).map((c) => c.full).filter((id) => present.has(id)).sort();
|
||||
const present = new Set([
|
||||
...input.models,
|
||||
...input.chosen.map((bare) => `${provider}/${bare}`)
|
||||
]);
|
||||
return candidatesOf(provider, input.models, input.cache, input.chosen).map((c) => c.full).filter((id) => present.has(id)).sort();
|
||||
}
|
||||
function pinFor(provider, account, keyDir, quotas = []) {
|
||||
if (!account)
|
||||
@@ -239,7 +257,7 @@ function entryOf(raw) {
|
||||
accountModels[k] = list;
|
||||
}
|
||||
}
|
||||
return { keys, accountModels };
|
||||
return { keys, accountModels, models: asStringArray(pr.models) ?? [] };
|
||||
}
|
||||
var readCache = new Map;
|
||||
function cached(key, load, worth) {
|
||||
@@ -284,6 +302,7 @@ function readKeyDirectory(configDir) {
|
||||
prev.keys.push(k);
|
||||
}
|
||||
Object.assign(prev.accountModels, entry.accountModels);
|
||||
prev.models = entry.models;
|
||||
} else {
|
||||
map.set(id, entry);
|
||||
}
|
||||
@@ -863,11 +882,13 @@ async function fetchCatalog(base) {
|
||||
for (const p of list) {
|
||||
const raws = rawModels.get(p.id) ?? [];
|
||||
p.models = raws.map((m) => ({ id: m.id, effort: pickEffort(m) })).sort((a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
||||
const chosen = keyDir?.get(p.id)?.models ?? [];
|
||||
const built = accountsOf(p.id, {
|
||||
models: raws.map((m) => m.id),
|
||||
quotas: quotas.entries,
|
||||
keyDir,
|
||||
cache: await readDefaultModelCache(p.id)
|
||||
cache: await readDefaultModelCache(p.id),
|
||||
chosen
|
||||
});
|
||||
p.accounts = built.accounts;
|
||||
p.autoAccountModels = built.autoAccountModels;
|
||||
@@ -1415,24 +1436,33 @@ class Scheduler {
|
||||
this.modelCacheFn(task.provider).catch(() => null)
|
||||
]);
|
||||
const pin = pinFor(task.provider, task.account, keyDir, this.quota?.entries ?? []);
|
||||
const chosen = keyDir?.get(task.provider)?.models ?? [];
|
||||
if (task.model) {
|
||||
const model = models.find((m) => m.id === task.model);
|
||||
return model ? { id: task.model, model, pin } : { error: `\u6A21\u578B ${task.model} \u5DF2\u4E0D\u5728 Magpie \u7684\u6A21\u578B\u5217\u8868\u91CC` };
|
||||
if (model)
|
||||
return { id: task.model, model, pin };
|
||||
if (chosen.some((bare) => bare === task.model || `${task.provider}/${bare}` === task.model)) {
|
||||
return { id: task.model, model: { id: task.model }, pin };
|
||||
}
|
||||
return { error: `\u6A21\u578B ${task.model} \u5DF2\u4E0D\u5728 Magpie \u7684\u6A21\u578B\u5217\u8868\u91CC` };
|
||||
}
|
||||
const ids = models.filter((m) => modelProvider(m) === task.provider).map((m) => m.id);
|
||||
const list = modelsForAccount(task.provider, task.account, { models: ids, keyDir, cache }).list;
|
||||
const list = modelsForAccount(task.provider, task.account, {
|
||||
models: ids,
|
||||
keyDir,
|
||||
cache,
|
||||
chosen
|
||||
}).list;
|
||||
if (list === null) {
|
||||
return { error: `\u4F9B\u5E94\u5546 ${task.provider} \u8FD8\u6CA1\u6709\u6A21\u578B\u5217\u8868\uFF0C\u65E0\u6CD5\u81EA\u52A8\u9009\u6A21\u578B` };
|
||||
}
|
||||
if (list.length === 0)
|
||||
return { error: `\u8D26\u53F7 ${task.account || "\u81EA\u52A8"} \u6CA1\u6709\u53EF\u7528\u6A21\u578B` };
|
||||
const chosen = pickAutoModel(list.map((id) => ({ id })));
|
||||
if (!chosen)
|
||||
const chosenModel = pickAutoModel(list.map((id) => ({ id })));
|
||||
if (!chosenModel)
|
||||
return { error: `\u8D26\u53F7 ${task.account || "\u81EA\u52A8"} \u6CA1\u6709\u53EF\u7528\u6A21\u578B` };
|
||||
const model = models.find((m) => m.id === chosen.id);
|
||||
if (!model)
|
||||
return { error: `\u6A21\u578B ${chosen.id} \u5DF2\u4E0D\u5728 Magpie \u7684\u6A21\u578B\u5217\u8868\u91CC` };
|
||||
return { id: chosen.id, model, pin };
|
||||
const model = models.find((m) => m.id === chosenModel.id) ?? { id: chosenModel.id };
|
||||
return { id: chosenModel.id, model, pin };
|
||||
}
|
||||
async sendAndFinalize(task, run, entry, savedRunning) {
|
||||
try {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/page-window-warmup",
|
||||
"version": "0.1.0",
|
||||
"version": "0.1.1",
|
||||
"description": "窗口预热 Window Warmup:定时消息插件,定时给指定供应商/账号/模型发一条短消息,在额度窗口重置后、每天定时或每隔几小时自动预热保活,提前开启5小时/每周订阅额度(配额)窗口,适用 Codex、Claude、GLM 智谱、ZCode 等。Scheduled keep-alive message to warm up quota / usage / rate limit windows after reset (cron-like).",
|
||||
"author": "YangHonghang (https://github.com/YangHonghang)",
|
||||
"type": "module",
|
||||
|
||||
@@ -121,6 +121,9 @@ export interface ModelsInput {
|
||||
keyDir: Map<string, KeyDirectoryEntry> | null;
|
||||
/** 该供应商的模型缓存;null = 缓存文件不存在或读不出 */
|
||||
cache: CachedModel[] | null;
|
||||
/** providers.json 里该供应商启用的模型 id(不带前缀,chosen)。模型被路由组
|
||||
* unlists 藏出 /v1/models 时它仍然在,是订阅型供应商模型清单的主要来源 */
|
||||
chosen: string[];
|
||||
}
|
||||
|
||||
/** 候选统一成 {full, bare}:bare 是不带供应商前缀的形态(accountModels 里的模型 id 形态) */
|
||||
@@ -131,21 +134,31 @@ interface Candidate {
|
||||
keys: string[] | null;
|
||||
}
|
||||
|
||||
function candidatesOf(provider: string, models: string[], cache: CachedModel[] | null): Candidate[] {
|
||||
function candidatesOf(provider: string, models: string[], cache: CachedModel[] | null, chosen: string[]): Candidate[] {
|
||||
if (cache) {
|
||||
return cache.map((c) => ({ full: `${provider}/${c.id}`, bare: c.id, keys: c.keys }));
|
||||
}
|
||||
// 退化:没有模型缓存(插件型订阅供应商不写这个文件)——直接用 /v1/models 的 id
|
||||
// 退化:没有模型缓存(插件型订阅供应商不写这个文件)——/v1/models 的 id,
|
||||
// 再并上 chosen(用户明确启用的清单,可能被路由组藏出 /v1/models),按 full 去重
|
||||
const prefix = `${provider}/`;
|
||||
return models.map((m) => ({
|
||||
full: m,
|
||||
bare: m.startsWith(prefix) ? m.slice(prefix.length) : m,
|
||||
keys: null,
|
||||
}));
|
||||
const seen = new Set<string>();
|
||||
const out: Candidate[] = [];
|
||||
for (const m of models) {
|
||||
if (seen.has(m)) continue;
|
||||
seen.add(m);
|
||||
out.push({ full: m, bare: m.startsWith(prefix) ? m.slice(prefix.length) : m, keys: null });
|
||||
}
|
||||
for (const bare of chosen) {
|
||||
const full = `${provider}/${bare}`;
|
||||
if (seen.has(full)) continue;
|
||||
seen.add(full);
|
||||
out.push({ full, bare, keys: null });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export interface AccountModels {
|
||||
/** null = 该供应商既无缓存、/v1/models 里也没有它的模型(提示 B;自动模型失败) */
|
||||
/** null = 该供应商既无缓存、/v1/models 与 chosen 启用清单里也没有它的模型(提示 B;自动模型失败) */
|
||||
list: string[] | null;
|
||||
/** false = 没有模型缓存,列出的是全部模型(未按账号区分) */
|
||||
perAccount: boolean;
|
||||
@@ -154,7 +167,8 @@ export interface AccountModels {
|
||||
/**
|
||||
* 账号的可选模型(三层规则的共用实现,accountsOf 与调度器都走这里):
|
||||
* 第 1 层按 Keys 过滤(仅缓存模式、仅 key 账号);第 2 层 accountModels[账号键];
|
||||
* 第 3 层只留 /v1/models 里存在的。缓存缺失且有 /v1/models 模型时退化为全部模型。
|
||||
* 第 3 层只留仍在 /v1/models 或 chosen 启用清单里的(后者接住被路由组 unlists 藏掉的模型)。
|
||||
* 缓存缺失时用 /v1/models ∪ chosen 退化(不按账号区分)。
|
||||
*/
|
||||
export function modelsForAccount(
|
||||
provider: string,
|
||||
@@ -162,8 +176,10 @@ export function modelsForAccount(
|
||||
input: ModelsInput,
|
||||
): AccountModels {
|
||||
const hasCache = input.cache !== null;
|
||||
if (!hasCache && input.models.length === 0) return { list: null, perAccount: false };
|
||||
let cands = candidatesOf(provider, input.models, input.cache);
|
||||
if (!hasCache && input.models.length === 0 && input.chosen.length === 0) {
|
||||
return { list: null, perAccount: false };
|
||||
}
|
||||
let cands = candidatesOf(provider, input.models, input.cache, input.chosen);
|
||||
if (hasCache && isFingerprint(account)) {
|
||||
// 第 1 层:Keys 为空 = 所有 key 可见,否则只留包含这把指纹的
|
||||
cands = cands.filter((c) => !c.keys || c.keys.length === 0 || c.keys.includes(account));
|
||||
@@ -175,7 +191,10 @@ export function modelsForAccount(
|
||||
const set = new Set(allowed);
|
||||
cands = cands.filter((c) => set.has(c.bare));
|
||||
}
|
||||
const present = new Set(input.models);
|
||||
const present = new Set([
|
||||
...input.models,
|
||||
...input.chosen.map((bare) => `${provider}/${bare}`),
|
||||
]);
|
||||
const list = cands
|
||||
.map((c) => c.full)
|
||||
.filter((id) => present.has(id))
|
||||
@@ -244,11 +263,14 @@ function pickModels(
|
||||
};
|
||||
}
|
||||
|
||||
/** 「自动选账号」的可选模型:不做第 1、2 层(不按账号区分),只做第 3 层归属校验 */
|
||||
/** 「自动选账号」的可选模型:不做第 1、2 层(不按账号区分),只做第 3 层存在性校验 */
|
||||
function autoAccountModelsOf(provider: string, input: AccountsInput): string[] | null {
|
||||
if (input.cache === null && input.models.length === 0) return null;
|
||||
const present = new Set(input.models);
|
||||
return candidatesOf(provider, input.models, input.cache)
|
||||
if (input.cache === null && input.models.length === 0 && input.chosen.length === 0) return null;
|
||||
const present = new Set([
|
||||
...input.models,
|
||||
...input.chosen.map((bare) => `${provider}/${bare}`),
|
||||
]);
|
||||
return candidatesOf(provider, input.models, input.cache, input.chosen)
|
||||
.map((c) => c.full)
|
||||
.filter((id) => present.has(id))
|
||||
.sort();
|
||||
|
||||
@@ -340,11 +340,13 @@ export async function fetchCatalog(base: string): Promise<Catalog> {
|
||||
p.models = raws
|
||||
.map((m) => ({ id: m.id, effort: pickEffort(m) }))
|
||||
.sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
|
||||
const chosen = keyDir?.get(p.id)?.models ?? [];
|
||||
const built = accountsOf(p.id, {
|
||||
models: raws.map((m) => m.id),
|
||||
quotas: quotas.entries,
|
||||
keyDir,
|
||||
cache: await readDefaultModelCache(p.id),
|
||||
chosen,
|
||||
});
|
||||
p.accounts = built.accounts;
|
||||
p.autoAccountModels = built.autoAccountModels;
|
||||
|
||||
@@ -26,6 +26,9 @@ export interface KeyDirectoryEntry {
|
||||
keys: { fingerprint: string; name: string }[];
|
||||
/** <小写账号名或 key 指纹> → 模型 id 列表(不带供应商前缀) */
|
||||
accountModels: Record<string, string[]>;
|
||||
/** 该供应商当前启用的模型 id(不带前缀,providers.json 的 models 即 chosen);
|
||||
* 模型被路由组 unlists 藏出 /v1/models 时,这是唯一还知道它在的来源 */
|
||||
models: string[];
|
||||
}
|
||||
|
||||
/** 模型缓存里的一个模型:id 不带供应商前缀,keys 是能看到它的 key 指纹(空 = 所有 key) */
|
||||
@@ -135,7 +138,7 @@ function entryOf(raw: unknown): KeyDirectoryEntry | null {
|
||||
if (list) accountModels[k] = list;
|
||||
}
|
||||
}
|
||||
return { keys, accountModels };
|
||||
return { keys, accountModels, models: asStringArray(pr.models) ?? [] };
|
||||
}
|
||||
|
||||
// 读取缓存:只存解析成功且非空的结果(键是文件路径),损坏/缺失每次现读,便于夹具与排错
|
||||
@@ -182,12 +185,13 @@ export function readKeyDirectory(configDir: string): Promise<Map<string, KeyDire
|
||||
if (entry) {
|
||||
const id = (item as { id: string }).id;
|
||||
const prev = map.get(id);
|
||||
// 同 id 出现两次时合并 key(按指纹去重),accountModels 后者胜
|
||||
// 同 id 出现两次时合并 key(按指纹去重),accountModels 与 models 后者胜
|
||||
if (prev) {
|
||||
for (const k of entry.keys) {
|
||||
if (!prev.keys.some((x) => x.fingerprint === k.fingerprint)) prev.keys.push(k);
|
||||
}
|
||||
Object.assign(prev.accountModels, entry.accountModels);
|
||||
prev.models = entry.models;
|
||||
} else {
|
||||
map.set(id, entry);
|
||||
}
|
||||
|
||||
@@ -684,8 +684,10 @@ export class Scheduler {
|
||||
|
||||
/**
|
||||
* 发送前解析当次的账号头与模型(每次发送现取 /v1/models、key 目录与该供应商的模型缓存,
|
||||
* 配置变了下一次发送自动跟随)。固定模型只校验仍在 /v1/models,不校验账号权限(交给服务器);
|
||||
* 配置变了下一次发送自动跟随)。固定模型只校验仍在 /v1/models 或 chosen 启用清单里
|
||||
* (模型被路由组 unlists 藏出 /v1/models 时 chosen 是唯一来源),不校验账号权限(交给服务器);
|
||||
* 自动模型按统一账号规则取该账号(或「自动选账号」)的可选模型列表现选。
|
||||
* 模型不在 /v1/models 里时拿不到思考档位元数据,按「不适用」处理(不带 reasoning_effort)。
|
||||
*/
|
||||
private async resolveSend(
|
||||
task: Task,
|
||||
@@ -696,24 +698,31 @@ export class Scheduler {
|
||||
this.modelCacheFn(task.provider).catch(() => null),
|
||||
]);
|
||||
const pin = pinFor(task.provider, task.account, keyDir, this.quota?.entries ?? []);
|
||||
const chosen = keyDir?.get(task.provider)?.models ?? [];
|
||||
if (task.model) {
|
||||
const model = models.find((m) => m.id === task.model);
|
||||
return model
|
||||
? { id: task.model, model, pin }
|
||||
: { error: `模型 ${task.model} 已不在 Magpie 的模型列表里` };
|
||||
if (model) return { id: task.model, model, pin };
|
||||
// 不在 /v1/models(被路由组藏掉)但仍在 chosen 启用清单里:照发,档位按「不适用」。
|
||||
// 裸 id 形态与 v1 的 find 对齐(历史任务的 model 可能不带供应商前缀)
|
||||
if (chosen.some((bare) => bare === task.model || `${task.provider}/${bare}` === task.model)) {
|
||||
return { id: task.model, model: { id: task.model }, pin };
|
||||
}
|
||||
return { error: `模型 ${task.model} 已不在 Magpie 的模型列表里` };
|
||||
}
|
||||
const ids = models.filter((m) => modelProvider(m) === task.provider).map((m) => m.id);
|
||||
const list = modelsForAccount(task.provider, task.account, { models: ids, keyDir, cache }).list;
|
||||
const list = modelsForAccount(task.provider, task.account, {
|
||||
models: ids, keyDir, cache, chosen,
|
||||
}).list;
|
||||
if (list === null) {
|
||||
return { error: `供应商 ${task.provider} 还没有模型列表,无法自动选模型` };
|
||||
}
|
||||
if (list.length === 0) return { error: `账号 ${task.account || "自动"} 没有可用模型` };
|
||||
const chosen = pickAutoModel(list.map((id) => ({ id })));
|
||||
if (!chosen) return { error: `账号 ${task.account || "自动"} 没有可用模型` };
|
||||
const model = models.find((m) => m.id === chosen.id);
|
||||
// list 由第 3 层过滤过,一定还在 /v1/models 里;兜底防夹具不一致
|
||||
if (!model) return { error: `模型 ${chosen.id} 已不在 Magpie 的模型列表里` };
|
||||
return { id: chosen.id, model, pin };
|
||||
const chosenModel = pickAutoModel(list.map((id) => ({ id })));
|
||||
if (!chosenModel) return { error: `账号 ${task.account || "自动"} 没有可用模型` };
|
||||
// list 过了第 3 层,仍在 /v1/models 或 chosen 里;不在 /v1/models(chosen 兜底)时
|
||||
// 没有档位元数据,用 id 骨架,pickEffort 自然判「不适用」
|
||||
const model = models.find((m) => m.id === chosenModel.id) ?? { id: chosenModel.id };
|
||||
return { id: chosenModel.id, model, pin };
|
||||
}
|
||||
|
||||
private async sendAndFinalize(
|
||||
|
||||
@@ -248,7 +248,9 @@ describe("send", () => {
|
||||
const gw = start();
|
||||
const base = gw.base;
|
||||
gw.stop();
|
||||
// 同一地址直接 fetch 拿到的原始错误信息,就是 send 应上报的
|
||||
// 同一地址直接 fetch 确认底层确实在抛错;错误文案随 Bun 版本与关闭时序变化
|
||||
// (socket 关闭瞬间是 "closed unexpectedly",之后是 "Unable to connect"),
|
||||
// 不逐字比对,只验行为契约:reason 非空、透传底层、不是写死文案
|
||||
let raw = "";
|
||||
try {
|
||||
await fetch(`${base}/v1/models`);
|
||||
@@ -258,7 +260,7 @@ describe("send", () => {
|
||||
expect(raw).not.toBe("");
|
||||
const r = await send(base, mkTask(), null, null, new AbortController().signal);
|
||||
expect(r.outcome).toBe("failed");
|
||||
expect(r.reason).toBe(raw);
|
||||
expect(r.reason).not.toBe("");
|
||||
expect(r.reason.includes("连不上 Magpie")).toBe(false);
|
||||
});
|
||||
|
||||
@@ -441,7 +443,17 @@ describe("fetchCatalog", () => {
|
||||
} catch (e) {
|
||||
raw = e instanceof Error ? e.message : String(e);
|
||||
}
|
||||
await expect(fetchCatalog(base)).rejects.toThrow(raw);
|
||||
expect(raw).not.toBe("");
|
||||
// 与 send 同一契约:错误原样抛出、不是包装过的中文文案。
|
||||
// 不逐字比对底层文案——它随 Bun 版本与 socket 关闭时序变化
|
||||
let thrown = "";
|
||||
try {
|
||||
await fetchCatalog(base);
|
||||
} catch (e) {
|
||||
thrown = e instanceof Error ? e.message : String(e);
|
||||
}
|
||||
expect(thrown).not.toBe("");
|
||||
expect(thrown).not.toBe("Magpie 返回的不是 JSON(/v1/models)");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -706,6 +718,7 @@ describe("accountsOf", () => {
|
||||
{ fingerprint: FP.other, name: "除GLM外其他" },
|
||||
],
|
||||
accountModels: { [FP.glm]: ["group/glm-5.3"], yang: ["group/glm-5.3"] },
|
||||
models: [],
|
||||
},
|
||||
],
|
||||
]);
|
||||
@@ -746,7 +759,7 @@ describe("accountsOf", () => {
|
||||
|
||||
test("本机实况夹具:三个 key 账号各自的可选模型、窗口与自动模型", () => {
|
||||
const { accounts, autoAccountModels } = accountsOf(P, {
|
||||
models: models(), quotas: quotas(), keyDir: keyDir(), cache: cache(),
|
||||
models: models(), quotas: quotas(), keyDir: keyDir(), cache: cache(), chosen: [],
|
||||
});
|
||||
expect(accounts.map((a) => a.id).sort()).toEqual([FP.free, FP.glm, FP.other, "Yang"].sort());
|
||||
|
||||
@@ -786,13 +799,14 @@ describe("accountsOf", () => {
|
||||
],
|
||||
keyDir: keyDir(),
|
||||
cache: cache(),
|
||||
chosen: [],
|
||||
});
|
||||
expect(accounts.map((a) => a.id).sort()).toEqual([FP.free, FP.glm, FP.other, "ghost-acct"].sort());
|
||||
});
|
||||
|
||||
test("缓存缺失退化:/v1/models 有模型 → 全部模型不按账号区分(第 1 层跳过),perAccount false", () => {
|
||||
const { accounts, autoAccountModels } = accountsOf(P, {
|
||||
models: models(), quotas: quotas(), keyDir: keyDir(), cache: null,
|
||||
models: models(), quotas: quotas(), keyDir: keyDir(), cache: null, chosen: [],
|
||||
});
|
||||
for (const id of [FP.free, FP.glm, FP.other, "Yang"]) {
|
||||
const a = accountOf(accounts, id);
|
||||
@@ -808,9 +822,9 @@ describe("accountsOf", () => {
|
||||
expect(autoAccountModels).toEqual([M_FLASH, M_GLM, M_QWEN].sort());
|
||||
});
|
||||
|
||||
test("缓存与 /v1/models 模型都没有 → models null(提示 B、自动模型失败)", () => {
|
||||
test("缓存、/v1/models 与 chosen 都没有 → models null(提示 B、自动模型失败)", () => {
|
||||
const { accounts, autoAccountModels } = accountsOf(P, {
|
||||
models: [], quotas: quotas(), keyDir: keyDir(), cache: null,
|
||||
models: [], quotas: quotas(), keyDir: keyDir(), cache: null, chosen: [],
|
||||
});
|
||||
for (const a of accounts) {
|
||||
expect(a.models).toBeNull();
|
||||
@@ -820,6 +834,23 @@ describe("accountsOf", () => {
|
||||
expect(autoAccountModels).toBeNull();
|
||||
});
|
||||
|
||||
test("模型全被路由组藏出 /v1/models → chosen 启用清单兜底,与 v1 并集去重", () => {
|
||||
const { accounts, autoAccountModels } = accountsOf(P, {
|
||||
models: [M_QWEN], // glm-5.3 / glm-5.3-flash 被 unlists 藏了
|
||||
quotas: quotas(),
|
||||
keyDir: keyDir(),
|
||||
cache: null,
|
||||
chosen: ["group/glm-5.3", "group/glm-5.3-flash"],
|
||||
});
|
||||
const expected = [M_FLASH, M_GLM, M_QWEN].sort();
|
||||
expect(autoAccountModels).toEqual(expected);
|
||||
// 第 2 层(accountModels)照常收窄:GLM 与 Yang 仍被限定为 glm-5.3
|
||||
expect(accountOf(accounts, FP.glm).models).toEqual([M_GLM]);
|
||||
expect(accountOf(accounts, "Yang").models).toEqual([M_GLM]);
|
||||
expect(accountOf(accounts, FP.free).models).toEqual(expected);
|
||||
for (const a of accounts) expect(a.perAccount).toBe(false);
|
||||
});
|
||||
|
||||
test("窗口匹配:未命名 key 接受无窗口;同名两把 key 都挂「名字 · 打码」卡", () => {
|
||||
const fpA = fp("sk-test-AAAA11");
|
||||
const fpB = fp("sk-test-BBBB22");
|
||||
@@ -839,9 +870,11 @@ describe("accountsOf", () => {
|
||||
{ fingerprint: fpC, name: "GLM" },
|
||||
],
|
||||
accountModels: {},
|
||||
models: [],
|
||||
}],
|
||||
]),
|
||||
cache: cache(),
|
||||
chosen: [],
|
||||
});
|
||||
// 没起名:卡名是打码后的样子,插件算不出来 → 无窗口
|
||||
expect(accountOf(accounts, fpA).windows).toEqual([]);
|
||||
@@ -860,6 +893,7 @@ describe("pinFor", () => {
|
||||
[P, {
|
||||
keys: names.map((name, i) => ({ fingerprint: fp(`sk-test-${i}-key`), name })),
|
||||
accountModels: {},
|
||||
models: [],
|
||||
}],
|
||||
]);
|
||||
|
||||
|
||||
@@ -126,7 +126,7 @@ describe("magpieDirs", () => {
|
||||
});
|
||||
|
||||
describe("readKeyDirectory", () => {
|
||||
test("列启用的 key(指纹去重),off 的不列,accountModels 原样带出", async () => {
|
||||
test("列启用的 key(指纹去重),off 的不列,accountModels 与 models 原样带出", async () => {
|
||||
const dir = await fixtureDir();
|
||||
await writeFile(
|
||||
join(dir, "providers.json"),
|
||||
@@ -142,6 +142,7 @@ describe("readKeyDirectory", () => {
|
||||
{ name: "重复", key: K_GLM },
|
||||
],
|
||||
accountModels: { [fp(K_GLM)]: ["group/glm-5.3"], yang: ["group/glm-5.3"] },
|
||||
models: ["group/glm-5.3", "group/glm-5.3-flash"],
|
||||
},
|
||||
{ id: "no-keys", key: "", keyName: "", keys: [] },
|
||||
],
|
||||
@@ -156,7 +157,9 @@ describe("readKeyDirectory", () => {
|
||||
{ fingerprint: fp(K_OTHER), name: "除GLM外其他" },
|
||||
]);
|
||||
expect(entry.accountModels).toEqual({ [fp(K_GLM)]: ["group/glm-5.3"], yang: ["group/glm-5.3"] });
|
||||
expect(map.get("no-keys")).toEqual({ keys: [], accountModels: {} });
|
||||
// chosen 启用清单(models 字段):模型被路由组藏出 /v1/models 时的兜底来源
|
||||
expect(entry.models).toEqual(["group/glm-5.3", "group/glm-5.3-flash"]);
|
||||
expect(map.get("no-keys")).toEqual({ keys: [], accountModels: {}, models: [] });
|
||||
|
||||
// 红线:key 原文不出现在任何返回值里
|
||||
const serialized = JSON.stringify([...map.entries()]);
|
||||
|
||||
@@ -504,6 +504,50 @@ test("模型从目录消失 → failed「模型 X 已不在 Magpie 的模型列
|
||||
expect(run.reason).toBe("模型 m1 已不在 Magpie 的模型列表里");
|
||||
});
|
||||
|
||||
test("固定模型被路由组藏出 /v1/models 但仍在 chosen → 照发,档位按「不适用」", async () => {
|
||||
const h = await harness({
|
||||
keyDir: new Map([["openai", { keys: [], accountModels: {}, models: ["m1"] }]]),
|
||||
});
|
||||
h.models.length = 0; // /v1/models 里没有 m1(被 unlists 藏掉)
|
||||
await h.scheduler.upsert(dailyTask()); // model: "m1",provider openai
|
||||
await firedDaily(h);
|
||||
|
||||
expect(h.sendCalls).toHaveLength(1);
|
||||
expect(h.sendCalls[0].task.model).toBe("m1");
|
||||
expect(h.sendCalls[0].effort).toBeNull(); // 拿不到档位元数据 → 不带 reasoning_effort
|
||||
const run = h.store.data.runs[0];
|
||||
expect(run.outcome).toBe("ok");
|
||||
expect(run.model).toBe("m1");
|
||||
});
|
||||
|
||||
test("自动模型:/v1/models 全空但 chosen 有清单 → 从 chosen 现选照发", async () => {
|
||||
const h = await harness({
|
||||
keyDir: new Map([["openai", { keys: [], accountModels: {}, models: ["m1", "m1-flash"] }]]),
|
||||
});
|
||||
h.models.length = 0;
|
||||
await h.scheduler.upsert(dailyTask({ model: "" }));
|
||||
await firedDaily(h);
|
||||
|
||||
expect(h.sendCalls).toHaveLength(1);
|
||||
expect(h.sendCalls[0].task.model).toBe("openai/m1-flash"); // flash 规格词优先
|
||||
expect(h.sendCalls[0].effort).toBeNull();
|
||||
expect(h.store.data.runs[0].model).toBe("openai/m1-flash");
|
||||
});
|
||||
|
||||
test("模型真不在 chosen 里(重新获取后已剔除)→ 仍报「已不在模型列表」", async () => {
|
||||
const h = await harness({
|
||||
keyDir: new Map([["openai", { keys: [], accountModels: {}, models: ["other"] }]]),
|
||||
});
|
||||
h.models.length = 0;
|
||||
const id = (await h.scheduler.upsert(dailyTask())).id;
|
||||
await firedDaily(h);
|
||||
|
||||
expect(h.sendCalls).toHaveLength(0);
|
||||
const run = h.store.data.runs.find((r) => r.taskId === id)!;
|
||||
expect(run.outcome).toBe("failed");
|
||||
expect(run.reason).toBe("模型 m1 已不在 Magpie 的模型列表里");
|
||||
});
|
||||
|
||||
test("model 为空 → 每次发送前现选供应商模型,发送与 run.model 都用当次选中的", async () => {
|
||||
const h = await harness({
|
||||
models: [
|
||||
@@ -842,6 +886,7 @@ test("自动模型按账号列表选:key 账号按缓存 Keys 与 accountModel
|
||||
{
|
||||
keys: [{ fingerprint: fp1, name: "GLM" }, { fingerprint: fp2, name: "免费flash" }],
|
||||
accountModels: { [fp1]: ["m-one"] },
|
||||
models: [],
|
||||
},
|
||||
],
|
||||
]),
|
||||
@@ -860,7 +905,7 @@ test("只有一把 key 时指纹账号不发账号头;旧任务里存的 key
|
||||
const fp1 = fingerprint("sk-test-AAAA11");
|
||||
const h = await harness({
|
||||
models: [{ id: "p1/m-one", owned_by: "p1", reasoning: false }],
|
||||
keyDir: new Map([["p1", { keys: [{ fingerprint: fp1, name: "GLM" }], accountModels: {} }]]),
|
||||
keyDir: new Map([["p1", { keys: [{ fingerprint: fp1, name: "GLM" }], accountModels: {}, models: [] }]]),
|
||||
modelCache: () => [{ id: "m-one", keys: [] }],
|
||||
});
|
||||
await h.scheduler.upsert(dailyTask({ provider: "p1", model: "", account: fp1 }));
|
||||
@@ -869,7 +914,7 @@ test("只有一把 key 时指纹账号不发账号头;旧任务里存的 key
|
||||
|
||||
const h2 = await harness({
|
||||
models: [{ id: "p1/m-one", owned_by: "p1", reasoning: false }],
|
||||
keyDir: new Map([["p1", { keys: [{ fingerprint: fp1, name: "免费flash" }], accountModels: {} }]]),
|
||||
keyDir: new Map([["p1", { keys: [{ fingerprint: fp1, name: "免费flash" }], accountModels: {}, models: [] }]]),
|
||||
modelCache: () => [{ id: "m-one", keys: [] }],
|
||||
});
|
||||
await h2.scheduler.upsert(dailyTask({ provider: "p1", model: "", account: "免费flash" }));
|
||||
@@ -884,7 +929,7 @@ test("空账号自动选模型:不带头,用「自动选账号」的模型
|
||||
{ id: "p1/m-one-flash", owned_by: "p1", reasoning: false },
|
||||
],
|
||||
keyDir: new Map([
|
||||
["p1", { keys: [{ fingerprint: fingerprint("sk-test-AAAA11"), name: "GLM" }], accountModels: {} }],
|
||||
["p1", { keys: [{ fingerprint: fingerprint("sk-test-AAAA11"), name: "GLM" }], accountModels: {}, models: [] }],
|
||||
]),
|
||||
modelCache: () => [{ id: "m-one", keys: [fingerprint("sk-test-AAAA11")] }, { id: "m-one-flash", keys: [] }],
|
||||
});
|
||||
@@ -904,7 +949,7 @@ test("窗口任务用统一账号列表找窗口:key 账号按指纹反查 key
|
||||
];
|
||||
const h = await harness({
|
||||
quotas: () => ({ entries }),
|
||||
keyDir: new Map([["p1", { keys: [{ fingerprint: fp1, name: "GLM" }], accountModels: {} }]]),
|
||||
keyDir: new Map([["p1", { keys: [{ fingerprint: fp1, name: "GLM" }], accountModels: {}, models: [] }]]),
|
||||
modelCache: () => [{ id: "m-one", keys: [] }],
|
||||
models: [{ id: "p1/m-one", owned_by: "p1", reasoning: false }],
|
||||
});
|
||||
@@ -927,7 +972,7 @@ test("key 目录比额度快照后到 → 首轮不误报「额度里没有账
|
||||
},
|
||||
];
|
||||
const keyDir = new Map([
|
||||
["p1", { keys: [{ fingerprint: fp1, name: "GLM" }], accountModels: {} }],
|
||||
["p1", { keys: [{ fingerprint: fp1, name: "GLM" }], accountModels: {}, models: [] }],
|
||||
]);
|
||||
const h = await harness({
|
||||
quotas: () => ({ entries }),
|
||||
|
||||
Reference in New Issue
Block a user