From ef7b51c9456fe32d175b2456b5069cf8efd6aecb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=A4=E6=9C=88=E6=9C=A8=E5=85=86?= <190677623+679215@users.noreply.github.com> Date: Tue, 3 Nov 2026 18:12:52 +0800 Subject: [PATCH] feat: add GPT model usage and public price estimates --- AGENTS.md | 8 +- CHANGELOG.md | 4 +- DEPLOYMENT.md | 2 +- README.agent.md | 10 +- README.en.md | 4 +- README.md | 24 +- docs/auth-v2.md | 11 +- docs/model-costs.md | 67 ++ history-archive.mjs | 105 +++ install-autostart.ps1 | 2 +- install.ps1 | 8 +- model-usage.mjs | 123 ++++ openrouter-pricing.mjs | 321 +++++++++ package.json | 4 +- protocol.mjs | 93 ++- schemas/payload-v5.schema.json | 973 ++++++++++++++++++++++++++++ scripts/build-source-package.ps1 | 2 +- scripts/stats-snapshot.mjs | 16 +- scripts/verify-audit-findings.mjs | 2 +- scripts/verify-live.mjs | 6 +- scripts/verify-sync.mjs | 5 +- scripts/verify-watchdog.ps1 | 2 +- service-tier-pricing.mjs | 70 ++ skills/tokens-ui-for-codex/SKILL.md | 6 +- test/auth-ui.playwright.test.mjs | 277 +++++++- test/history-archive.test.mjs | 125 ++++ test/live-ui.playwright.test.mjs | 6 +- test/openrouter-pricing.test.mjs | 249 +++++++ test/pricing-packaging.test.mjs | 35 + test/service-tier-pricing.test.mjs | 91 +++ test/token-stats.test.mjs | 151 ++++- token-stats.mjs | 157 ++++- tokens-ui-for-codex-panel.js | 306 ++++++++- 33 files changed, 3206 insertions(+), 59 deletions(-) create mode 100644 docs/model-costs.md create mode 100644 model-usage.mjs create mode 100644 openrouter-pricing.mjs create mode 100644 schemas/payload-v5.schema.json create mode 100644 service-tier-pricing.mjs create mode 100644 test/openrouter-pricing.test.mjs create mode 100644 test/pricing-packaging.test.mjs create mode 100644 test/service-tier-pricing.test.mjs diff --git a/AGENTS.md b/AGENTS.md index 6d758e5..1e54248 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -50,7 +50,7 @@ - 读取本地 Codex rollout JSONL - 增量解析与分片合并 - 统计请求、轮次、缓存、速度和上下文 -- 通过本机 CDP 推送签名 schema v4 / auth v2 负载 +- 通过本机 CDP 推送签名 schema v5 / auth v2 负载 - 写入健康快照、简略快照和日志 会话身份约束: @@ -65,7 +65,7 @@ ### 协议 - `protocol.mjs` -- `schemas/payload-v4.schema.json` +- `schemas/payload-v5.schema.json` - `docs/auth-v2.md` - 页面事件:`tokens-ui-for-codex` - 页面全局:`window.__tuiForCodex` @@ -112,7 +112,7 @@ Codex rollout JSONL ## 4. 协议约束 -schema v4 / auth v2 是严格签名-only: +schema v5 / auth v2 是严格签名-only: - 必须 `authRequired: true` - 必须有 `auth.v=2` @@ -132,7 +132,7 @@ schema v4 / auth v2 是严格签名-only: - nonce 重放 - seq 倒退 - `authRequired !== true` -- 旧 schema v3 / auth v1 +- 旧 schema v4/v3 或 auth v1 ## 5. 统计口径 diff --git a/CHANGELOG.md b/CHANGELOG.md index 74e9de9..91099ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ ## 未发布源码 - 版本号暂保持 1.0.2;本轮生成本地发布候选包,不自动推送 GitHub Release。 -- 页面协议升级为 schema v4 / auth v2。 +- 页面协议升级为 schema v5 / auth v2(认证算法保持不变)。 +- 新增 8 个指定 GPT 型号的请求、轮次、会话和历史时间范围分类记录、公开价费用参考及模型详情。 +- 公开价格匿名获取并本机缓存;未知模型、usage 或价格不猜测,已归档请求费用不随新价重算。 - HMAC 输入覆盖业务正文、认证版本、keyId、时间戳、nonce 与 seq。 - 密钥改为 32 字节原始值,nonce 改为 16 字节随机值。 - 临时密钥只在一次同步事件派发期间存在,页面捕获后立即删除。 diff --git a/DEPLOYMENT.md b/DEPLOYMENT.md index 23e5b02..ed32fd0 100644 --- a/DEPLOYMENT.md +++ b/DEPLOYMENT.md @@ -35,7 +35,7 @@ node plugins\tokens-ui-for-codex\scripts\check-page-state.mjs node plugins\tokens-ui-for-codex\scripts\verify-live.mjs ``` -页面应只有一个 `#tui-native` 面板,协议为 schema v4 / auth v2。 +页面应只有一个 `#tui-native` 面板,协议为 schema v5 / auth v2。新旧面板与监控必须成套替换。 ## 更新 diff --git a/README.agent.md b/README.agent.md index 071f5d1..bcb7ef5 100644 --- a/README.agent.md +++ b/README.agent.md @@ -12,7 +12,7 @@ | --- | --- | | 插件标识 | `tokens-ui-for-codex@tokens-ui-for-codex-local` | | 插件版本 | 1.0.2 本地发布候选;包含未提交到 Git 的 auth v2/context/range 更新 | -| 页面协议 | 签名 schema v4 / auth v2 | +| 页面协议 | 签名 schema v5 / auth v2 | | 页面事件 | `tokens-ui-for-codex` | | 页面全局 | `window.__tuiForCodex` | | 用户脚本 | `%APPDATA%\Codex++\user_scripts\tokens-ui-for-codex-panel.js` | @@ -98,8 +98,8 @@ powershell.exe -NoProfile -ExecutionPolicy Bypass -File "<包根>\install.ps1" 3. 用户脚本存在,并与部署文件 SHA-256 一致 4. `codex plugin list` 显示插件为 installed/enabled 5. 页面 `panelCount=1` -6. `window.__tuiForCodexProtocol.supportedSchemaVersions` 包含 `4` -7. `window.__tuiForCodex.schemaVersion === 4` +6. `window.__tuiForCodexProtocol.supportedSchemaVersions` 包含 `5` +7. `window.__tuiForCodex.schemaVersion === 5` 8. `window.__tuiForCodex.auth.v === 2`,`keyId`/`nonce` 各 32 位十六进制 9. `window.__tuiForCodex.auth.mac` 为 64 位十六进制字符串 10. `__tuiForCodexAuthBootstrap` 与历史 `__tuiForCodexAuthKey` 均不存在 @@ -210,7 +210,7 @@ Get-ChildItem ((Get-Content -Raw -Encoding UTF8 .\runtime-paths.json | ConvertFr | 两条统计条 | 用户脚本目录是否有旧 `.js` | 保留新脚本,把其他面板脚本改为 `.bak` | | 等待数据 | 健康文件、日志、当前对话 | 切到有内容的会话,或重启监控 | | 数字停止 | 分片、定位预警、文件 mtime | 合并分片逻辑;必要时重载页面 | -| 详情不更新 | 对话 ID、schema v4 / auth v2 | 检查控制台安全计数和监控日志 | +| 详情不更新 | 对话 ID、schema v5 / auth v2 | 检查控制台安全计数和监控日志 | | 主题不跟随 | `` | 检查页面主题观察器与宿主属性 | | 中文路径失败 | PS1 BOM、VBS 编码 | PS1= UTF-8 BOM;VBS= UTF-16LE | | 速度不动 | 数据源节奏 | 心跳不产生新数据,等待新的 usage record | @@ -283,7 +283,7 @@ Get-ChildItem ((Get-Content -Raw -Encoding UTF8 .\runtime-paths.json | ConvertFr 1. 校验清单缺失和哈希错误均为 0 2. 重新运行安装器 3. 重载页面 -4. 验证 schema v4 / auth v2 与单实例 +4. 验证 schema v5 / auth v2 与单实例 ## 11. 卸载 diff --git a/README.en.md b/README.en.md index c84c623..becec1f 100644 --- a/README.en.md +++ b/README.en.md @@ -197,7 +197,7 @@ fallback command. The PowerShell confirmation window still requires `Y`. Codex rollout JSONL → incremental parser and statistics → local CDP with dynamic port discovery - → signed schema v4 / auth v2 payload + → signed schema v5 / auth v2 payload → HMAC verification in the Codex++ script → incremental bar/dialog rendering ``` @@ -242,7 +242,7 @@ view does not depend on a heartbeat. ## Security and privacy -- Signed-only schema v4 / auth v2 +- Signed-only schema v5 / auth v2 - HMAC authenticates integrity and origin; it does not encrypt data - Random 256-bit per-monitor session key - HMAC-SHA256 over the payload, auth version, key ID, timestamp, nonce and sequence diff --git a/README.md b/README.md index 7bc682c..39e0db9 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ - 速度使用 `≈XXXT/s`,明确表示估算值 - 指针停留在统计条上时冻结刷新,移出后立即追上最新值 -详情弹层包含六个分区: +详情弹层包含以下分区: | 分区 | 内容 | | --- | --- | @@ -51,6 +51,7 @@ | 监控操作 | 手动启动和安全告警;顶部健康灯可悬浮查看错误码、原因和恢复状态 | | 当前提问请求明细 | 默认最近 10 条,加载更多每次 +20,最多 500 条 | | 各轮摘要 | 默认最近 10 轮,加载更多每次 +50,最多 500 轮 | +| GPT 模型记录与参考费用 | 按模型查看会话、当前轮、请求及所选历史范围的用量、计价覆盖率与单模型详情 | 两个列表均为最新在前。未显示的条目不会进入页面负载,只有点击“加载更多”才按窗口拉取。 页面、监控协议和接收端都严格限制最多 **500** 条;更早的结构化请求与轮次摘要写入本地历史文件, @@ -80,6 +81,23 @@ [docs/context-estimation.md](docs/context-estimation.md)。 如果未归类内容超过 60%,页面会隐藏不可靠的分类,仅保留精确已用量和上限。 +## GPT 模型记录与参考费用(未发布源码) + +仅支持 GPT-6.1 Sol、GPT-6 Astra、GPT-6.1 Terra、GPT-6 Sol、GPT-6 Luna、 +GPT-5.6 Sol、GPT-5.6 Terra、GPT-5.6 Luna。按精确模型 ID 归属,不根据当前模型 +倒填旧请求;无法归属的记录保留未知。各模型分别记录 input、cached、output、total 和请求数。 + +费用为 **按 OpenRouter 公开价折算的 USD 参考值**,不是 Codex 实际账单、账号余额或订阅剩余额度。 +监控只匿名 GET 公开模型目录,价目快照保存到 `stateDir/pricing/openrouter-catalog.json`, +24 小时内复用,失败时标记缓存陈旧;不会向价格服务发送本机用量、消息或凭据。 +缺模型、价目或必要 usage 分类时显示未知;部分结果只显示已知小计和覆盖率。 +未核实的 Fast、Batch、缓存 TTL 档位不猜价,不把 reasoning 再计入 output。 +请求首次归档的费用保留,不用今天的价格回填旧档案。 + +详情提供模型列表及单模型详情,不改变紧凑条布局;原有 auth v2 签名保留,业务 schema 升级为 v5。 +账号管理、额度估值、连接器、密钥管理和外汇换算均不在此功能范围。 +具体口径与已核实价格源见 [docs/model-costs.md](docs/model-costs.md)。 + ## 上下文能力 - 已用量取最后一次请求的 `usage.total_tokens`,并受模型上下文上限约束; @@ -224,7 +242,7 @@ PowerShell 确认窗口,只有输入 `Y` 才启动。页面不会静默启动 本地 Codex 会话 JSONL → token-stats.mjs 增量解析与统计 → 本机 CDP(动态端口) - → 签名 schema v4 / auth v2 负载 + → 签名 schema v5 / auth v2 负载 → Codex++ 用户脚本验签 → 原生风格统计条与详情增量渲染 ``` @@ -275,7 +293,7 @@ PowerShell 确认窗口,只有输入 `Y` 才启动。页面不会静默启动 ## 安全与隐私 -- 页面协议为**签名-only schema v4 / auth v2** +- 页面协议为**签名-only schema v5 / auth v2** - HMAC 只做完整性和来源校验,不提供加密 - 每次监控启动生成随机 256-bit 会话密钥 - HMAC-SHA256 签名覆盖完整负载、认证版本、keyId、时间戳、nonce 与 seq diff --git a/docs/auth-v2.md b/docs/auth-v2.md index 7891c65..f22a906 100644 --- a/docs/auth-v2.md +++ b/docs/auth-v2.md @@ -1,7 +1,7 @@ # HMAC auth v2 本文描述当前工作树的本地推送认证协议。已发布的 1.0.2 标签仍使用 -schema v3 / auth v1;当前未发布源码升级为 schema v4 / auth v2。 +schema v3 / auth v1;当前未发布源码升级为 schema v5 / auth v2。 ## 目标 @@ -14,7 +14,7 @@ schema v3 / auth v1;当前未发布源码升级为 schema v4 / auth v2。 ```json { - "schemaVersion": 4, + "schemaVersion": 5, "authRequired": true, "auth": { "v": 2, @@ -47,7 +47,7 @@ tokens-ui-for-codex/auth-v2 页面必须同时满足: -- `schemaVersion=4` +- `schemaVersion=5`(v4 及更早版本不接受;v5 新增模型与费用结构,认证算法不变) - `auth.v=2` - `keyId` 与 `nonce` 为 32 位小写十六进制 - `mac` 为 64 位小写十六进制 @@ -91,11 +91,12 @@ WebCrypto `CryptoKey`。 ## 兼容性 -schema v4 与 schema v3 不兼容: +schema v5 与 schema v4/v3 不兼容。即使同为 auth v2,业务 schema 不匹配也拒绝: | 面板 | 监控 | 结果 | | --- | --- | --- | -| auth v2 | auth v2 | 正常 | +| schema v5 / auth v2 | schema v5 / auth v2 | 正常 | +| schema v5 / auth v2 | schema v4 / auth v2 | `protocol-mismatch` | | auth v1 | auth v2 | `protocol-mismatch` | | auth v2 | auth v1 | `protocol-mismatch` | diff --git a/docs/model-costs.md b/docs/model-costs.md new file mode 100644 index 0000000..a885ac1 --- /dev/null +++ b/docs/model-costs.md @@ -0,0 +1,67 @@ +# GPT 模型记录与参考费用 + +状态:未发布工作树功能,版本号仍为 `1.0.2+codex.20261020131145`;等待人工验收。 +业务协议为 schema v5,HMAC auth v2 的签名、时窗、nonce、seq 规则不变。 + +## 模型与价格核验 + +2026-11-03(Asia/Shanghai)匿名读取 [OpenRouter 模型目录](https://openrouter.ai/api/v1/models) +返回 HTTP 200,以下 8 个精确 ID 均存在。仅接受表中的 `openai/…` 与无前缀 ID, +不根据子串、相似名称或当前选中模型猜测历史归属。 + +下表为本次核验的 USD / 百万 token 快照,**不是运行时常量,也不是实际账单**: + +| 模型 ID(省略 openai/) | 普通输入 | 缓存读取 | 输出 | 目录缓存写入 | +| --- | ---: | ---: | ---: | ---: | +| gpt-6.1-sol | 2 | 0.1 | 10 | 2.5 | +| gpt-6-astra | 10 | 1 | 50 | 12.5 | +| gpt-6.1-terra | 1 | 0.05 | 5 | 1.25 | +| gpt-6-sol | 2 | 0.2 | 10 | 2.5 | +| gpt-6-luna | 0.1 | 0.01 | 0.5 | 0.125 | +| gpt-5.6-sol | 4 | 0.4 | 20 | 5 | +| gpt-5.6-terra | 2 | 0.2 | 12 | 2.5 | +| gpt-5.6-luna | 0.2 | 0.02 | 1.2 | 0.25 | + +目录单位是 USD / token,运算直接使用该单位。目录同时返回 `min_prompt_tokens=272000` +的上下文档位;只有可验证的完整档位才可选用。参考资料: +[OpenRouter 目录接口](https://openrouter.ai/docs/api/api-reference/models/list-all-models-and-their-properties)、 +[OpenAI 官方定价](https://developers.openai.com/api/docs/pricing)。 + +目录文档的认证示例包含 Bearer token,匿名读取只是当前可用的 best-effort 行为,不承诺永久可用。 +实现不会索取或读取 key;401/403/429、超时、坏 JSON 都使用最后有效缓存或返回不可用。 + +## 计价与未知值 + +每个模型单独记录 input、cached、output、total、请求数、参考费用以及已计价/未计价请求数。 +费用与原有 token 统计独立;不改 F1 合并、速度、计时与原有累计数字。 + +```text +普通输入 = input − cached − 已确认的缓存写入 +参考费用 = 普通输入 × prompt 价格 + + cached × input_cache_read 价格 + + 缓存写入 × input_cache_write 价格 + + output × completion 价格 +``` + +- output 已含 reasoning,不再加一次 reasoning。 +- 缓存分类缺失、非有限/负值或分类合计大于 input 时不可计价,不夹断成貌似准确的金额。 +- 没有可靠模型、价格或 usage 时金额为 null,页面显示 `--` 或未知;未知不是免费。 +- 有部分可计价请求时只列已知小计及覆盖率,不能当成完整费用。 +- Standard 公共价参考不是实际调用渠道的账单。未核实的 Fast/Batch/12h/API profile 不套通用倍率; + API profile 解析支持精确 model/tier/TTL/上下文区间与官方来源及有效期,未提供证据的组合保持未知。 +- 不计算账号余额、订阅剩余额度、真实扣款或外汇换算;不恢复历史账号实验。 + +## 获取、历史与隐私 + +- 唯一外部请求为匿名 GET 公开目录,10 秒超时;不发送本机模型用量、消息、路径或凭据。 +- 缓存位于 `runtimePaths.stateDir/pricing/openrouter-catalog.json`,24 小时复用,临时文件加 rename 原子写入。 +- 失败不会清空最后有效快照;stale 状态和快照时间随签名负载展示。不在每秒心跳中重新联网。 +- 请求首次归档的价格结果保留;新价不重估旧请求,旧档案缺模型/费用时仍未知,不回填猜价。 +- 时间范围沿用 today / 3d / 7d / 30d / all,只读取脱敏的结构化轮次档案;详情关闭时停止范围计算。 +- schema、监控和面板成套使用 v5;新字段经过 auth v2 完整签名并按白名单校验,列表上限仍为 500。 + +## 查看方式 + +在详情中查看模型列表,再点击单个模型查看 token 分类、请求数、费用覆盖率。 +会话、当前轮、请求明细和已有历史范围保留各自统计口径,不将它们再次相加。 +紧凑条布局、宿主模型选择菜单和 Codex 配置保持原样。 diff --git a/history-archive.mjs b/history-archive.mjs index 6f127f1..7a110b0 100644 --- a/history-archive.mjs +++ b/history-archive.mjs @@ -1,6 +1,7 @@ import crypto from "node:crypto"; import fs from "node:fs"; import path from "node:path"; +import { aggregateModelUsage, mergeModelUsage, requestModelMetadata, unknownModelUsage } from "./model-usage.mjs"; export const HISTORY_RETENTION_DAYS = 60; export const HISTORY_FILE_MAX_BYTES = 32 * 1024 * 1024; @@ -75,10 +76,78 @@ function readKeys(file) { const pattern = /"key":"([0-9a-f]{24})"/g; let match; while ((match = pattern.exec(text))) keys.push(match[1]); + // Earlier request archives used a different key. Recognize their numeric + // usage identity in memory only; never migrate or rewrite the old file. + for (const line of text.split(/\r?\n/)) { + try { + const row = JSON.parse(line); + if (row.v !== 2 || row.kind !== "request" || !/^[a-f0-9]{32}$/.test(row.sessionHash)) continue; + if (!Number.isSafeInteger(row.turnIndex) || row.turnIndex < 1 || typeof row.ts !== "string") continue; + keys.push( + hashKey([ + row.sessionHash, + String(row.turnIndex), + row.ts, + String(row.input), + String(row.cached), + String(row.output), + String(row.total), + String(row.threadTotal), + String(row.turnTotal), + ]), + ); + } catch {} + } } catch {} return keys; } +function requestArchiveKey(record, sessionHash, turnIndex) { + return hashKey([ + sessionHash, + String(turnIndex + 1), + record.ts || "", + String(record.input), + String(record.cached), + String(record.output), + String(record.requestTotal), + String(record.threadUsage?.total ?? null), + String(record.turnUsage?.total ?? null), + ]); +} + +function readArchivedMetadata(file, metadata) { + try { + for (const line of fs.readFileSync(file, "utf8").split(/\r?\n/)) { + try { + const row = JSON.parse(line); + if ( + row.kind !== "request" || + !Number.isSafeInteger(row.turnIndex) || + row.turnIndex < 1 || + !/^[a-f0-9]{32}$/.test(row.sessionHash) + ) + continue; + const key = requestArchiveKey( + { + ts: row.ts, + input: row.input, + cached: row.cached, + output: row.output, + requestTotal: row.total, + threadUsage: { total: row.threadTotal }, + turnUsage: { total: row.turnTotal }, + }, + row.sessionHash, + row.turnIndex - 1, + ); + // Only sanitized numeric metadata is recovered; never reprice old rows. + if (!metadata.has(key)) metadata.set(key, requestModelMetadata(row)); + } catch {} + } + } catch {} +} + export class UsageHistoryArchive { constructor( root, @@ -130,14 +199,17 @@ export class UsageHistoryArchive { const dir = path.join(this.root, "sessions", hash); fs.mkdirSync(dir, { recursive: true }); const seen = new Set(); + const requestMetadata = new Map(); for (const name of fs.readdirSync(dir)) { if (!/^requests-\d{6}(?:-\d{3})?\.jsonl$/.test(name)) continue; for (const key of readKeys(path.join(dir, name))) seen.add(key); + readArchivedMetadata(path.join(dir, name), requestMetadata); } const state = { hash, dir, seen, + requestMetadata, turnCounts: new Map(), lastRecordCount: 0, lastTurnsJson: "", @@ -171,6 +243,7 @@ export class UsageHistoryArchive { start: turn.start || "", startLabel: turn.startLabel || "", requests: turn.requestCount || 0, + modelUsage: turn.modelUsage || unknownModelUsage(turn), input: turn.input || 0, cached: turn.cached == null ? null : turn.cached, output: turn.output || 0, @@ -207,6 +280,32 @@ export class UsageHistoryArchive { if (!this.lastCleanupAt || now - this.lastCleanupAt >= this.cleanupIntervalMs) this.cleanup(now); const state = this.loadSession(threadId); + const modelRequests = turns.map(() => []); + for (const record of records) { + const turnIndex = locateTurnIndex(turns, record); + if (turnIndex < 0) continue; + const key = requestArchiveKey(record, state.hash, turnIndex); + const frozen = state.requestMetadata.get(key); + if (frozen) Object.assign(record, frozen); + else state.requestMetadata.set(key, requestModelMetadata(record)); + modelRequests[turnIndex].push(record); + } + turns.forEach((turn, index) => { + turn.modelUsage = modelRequests[index].length + ? aggregateModelUsage(modelRequests[index]) + : turn.modelUsage || unknownModelUsage(turn); + // Retained UI rows must use the same frozen metadata as the archive. + const lookup = new Map( + modelRequests[index].map((record) => [ + JSON.stringify([record.ts, record.input, record.output, record.requestTotal]), + record, + ]), + ); + for (const request of Array.isArray(turn.requests) ? turn.requests : []) { + const match = lookup.get(JSON.stringify([request.ts, request.input, request.output, request.lastTotal])); + if (match) Object.assign(request, requestModelMetadata(match)); + } + }); const cutoffMs = now - this.retentionDays * 24 * 60 * 60 * 1000; const shrink = records.length < state.lastRecordCount; const periodicFullScan = state.archiveCalls > 0 && state.archiveCalls % 100 === 0; @@ -255,6 +354,7 @@ export class UsageHistoryArchive { total: record.requestTotal == null ? null : record.requestTotal, threadTotal, turnTotal, + ...requestModelMetadata(record), }), ); } @@ -307,6 +407,7 @@ export class UsageHistoryArchive { effectiveMs: null, effectiveTps: null, timingSampleCount: 0, + modelUsage: [], }; const sessionsRoot = path.join(this.root, "sessions"); @@ -342,6 +443,10 @@ export class UsageHistoryArchive { summary.input += Number.isFinite(turn.input) ? turn.input : 0; summary.output += Number.isFinite(turn.output) ? turn.output : 0; summary.total += Number.isFinite(turn.total) ? turn.total : 0; + summary.modelUsage = mergeModelUsage([ + ...summary.modelUsage, + ...(Array.isArray(turn.modelUsage) ? turn.modelUsage : unknownModelUsage(turn)), + ]); if (Number.isFinite(turn.cached)) summary.cached += turn.cached; else if (Number.isFinite(turn.input) && turn.input > 0) summary.cacheComplete = false; if (Number.isFinite(turn.wallMs)) { diff --git a/install-autostart.ps1 b/install-autostart.ps1 index 697faed..057944f 100644 --- a/install-autostart.ps1 +++ b/install-autostart.ps1 @@ -571,7 +571,7 @@ $stateJson = [ordered]@{ mechanism = "scheduled-task" supervisorMode = "resident" supervisorIntervalMs = 15000 - schemaVersion = 4 + schemaVersion = 5 launcherEncoding = "utf-8-bom" confirmationScript = "start-monitor.ps1" taskName = $taskName diff --git a/install.ps1 b/install.ps1 index 25b69a8..918d28c 100644 --- a/install.ps1 +++ b/install.ps1 @@ -64,7 +64,7 @@ if (Test-Path -LiteralPath (Join-Path $releasePluginRoot ".codex-plugin\plugin.j New-Item -ItemType Directory -Path (Join-Path $stageNext ".agents\plugins") -Force | Out-Null foreach ($file in @( - "token-stats.mjs", "runtime-paths.mjs", "monitor-watchdog.ps1", "speed-stats.mjs", "history-archive.mjs", "protocol.mjs", "tokens-ui-for-codex-panel.js", + "token-stats.mjs", "runtime-paths.mjs", "monitor-watchdog.ps1", "speed-stats.mjs", "history-archive.mjs", "openrouter-pricing.mjs", "service-tier-pricing.mjs", "model-usage.mjs", "protocol.mjs", "tokens-ui-for-codex-panel.js", "find-codex.ps1", "install-autostart.ps1", "uninstall-autostart.ps1", "install.bat", "install.ps1", "README.md", "README.en.md", "README.agent.md", "MIGRATION-FOR-AGENTS.md", "DEPLOYMENT.md", "AGENTS.md", "LICENSE", "CHANGELOG.md", @@ -138,11 +138,11 @@ if (Test-Path -LiteralPath (Join-Path $releasePluginRoot ".codex-plugin\plugin.j # 先完整校验新 staging,再切换目录。失败时保留旧的 install-stage,不让半成品生效。 $requiredStageFiles = @( - "token-stats.mjs", "runtime-paths.mjs", "monitor-watchdog.ps1", "speed-stats.mjs", "history-archive.mjs", "protocol.mjs", + "token-stats.mjs", "runtime-paths.mjs", "monitor-watchdog.ps1", "speed-stats.mjs", "history-archive.mjs", "openrouter-pricing.mjs", "service-tier-pricing.mjs", "model-usage.mjs", "protocol.mjs", "tokens-ui-for-codex-panel.js", "find-codex.ps1", "install-autostart.ps1", "uninstall-autostart.ps1", "package.json", "DEPLOYMENT.md", "readme01.png", "readme02.png", "readme03.png", ".codex-plugin\plugin.json", - "schemas\payload-v4.schema.json" + "schemas\payload-v5.schema.json" ) foreach ($relative in $requiredStageFiles) { if (-not (Test-Path -LiteralPath (Join-Path $pluginRoot $relative) -PathType Leaf)) { @@ -334,7 +334,7 @@ if ($blocking.Count -gt 0) { exit 1 } -# schema v4 与旧版不兼容:必须先停旧监控,再替换用户脚本,避免旧 v3 推送持续进入新面板。 +# schema v5 与旧版不兼容:必须先停旧监控,再替换用户脚本,避免旧版推送持续进入新面板。 $oldMonitors = @(Get-CimInstance Win32_Process -Filter "Name='node.exe'" -ErrorAction SilentlyContinue | Where-Object { $_.CommandLine -and $_.CommandLine -match 'token-stats\.mjs' -and $_.CommandLine -match '--watch' }) foreach ($proc in $oldMonitors) { diff --git a/model-usage.mjs b/model-usage.mjs new file mode 100644 index 0000000..8167d95 --- /dev/null +++ b/model-usage.mjs @@ -0,0 +1,123 @@ +import { getSupportedModels, resolveOpenRouterModel } from "./openrouter-pricing.mjs"; + +const labels = new Map(getSupportedModels().map((model) => [model.modelId, model.modelLabel])); +export const MODEL_COST_STATUSES = Object.freeze([ + "priced", + "partial", + "unknown-model", + "missing-price", + "missing-usage", + "stale-price", + "unavailable", + "unsupported-tier", + "unknown-mode", +]); + +export function modelIdentity(value) { + const modelId = resolveOpenRouterModel(value); + return { modelId, modelLabel: labels.get(modelId) || "未知模型" }; +} + +export function requestModelMetadata(request = {}) { + const identity = modelIdentity(request.modelId ?? request.model); + const cost = + typeof request.openRouterReferenceUsd === "number" && + Number.isFinite(request.openRouterReferenceUsd) && + request.openRouterReferenceUsd >= 0 && + ["priced", "stale-price"].includes(request.costStatus) + ? request.openRouterReferenceUsd + : null; + return { + ...identity, + openRouterReferenceUsd: identity.modelId ? cost : null, + pricingFetchedAt: + typeof request.pricingFetchedAt === "string" && Number.isFinite(Date.parse(request.pricingFetchedAt)) + ? request.pricingFetchedAt + : null, + costStatus: identity.modelId + ? MODEL_COST_STATUSES.includes(request.costStatus) + ? request.costStatus + : "unavailable" + : "unknown-model", + }; +} + +// Fold final, F1-deduplicated requests only. Missing counters stay unknown. +export function aggregateModelUsage(requests) { + const rows = requests.map((request) => { + const metadata = requestModelMetadata(request); + const priced = metadata.openRouterReferenceUsd !== null; + return { + ...metadata, + input: request.input ?? null, + cached: request.cached ?? null, + output: request.output ?? null, + total: request.requestTotal ?? request.lastTotal ?? request.total ?? null, + requestCount: 1, + pricedRequestCount: priced ? 1 : 0, + unpricedRequestCount: priced ? 0 : 1, + }; + }); + return mergeModelUsage(rows); +} + +export function mergeModelUsage(rows) { + const groups = new Map(); + for (const row of rows) { + const identity = modelIdentity(row.modelId); + const key = identity.modelId || "unknown"; + let group = groups.get(key); + if (!group) { + group = { + ...identity, + input: 0, + cached: 0, + output: 0, + total: 0, + requestCount: 0, + openRouterReferenceUsd: null, + pricedRequestCount: 0, + unpricedRequestCount: 0, + costStatus: "unavailable", + pricingFetchedAt: row.pricingFetchedAt ?? null, + }; + groups.set(key, group); + } + for (const field of ["input", "cached", "output", "total"]) { + group[field] = + group[field] !== null && Number.isFinite(row[field]) && row[field] >= 0 ? group[field] + row[field] : null; + } + const count = Number.isSafeInteger(row.requestCount) && row.requestCount >= 0 ? row.requestCount : 0; + const pricedCount = + identity.modelId && + Number.isSafeInteger(row.pricedRequestCount) && + row.pricedRequestCount >= 0 && + row.pricedRequestCount <= count && + Number.isFinite(row.openRouterReferenceUsd) && + row.openRouterReferenceUsd >= 0 + ? row.pricedRequestCount + : 0; + group.requestCount += count; + group.pricedRequestCount += pricedCount; + group.unpricedRequestCount += count - pricedCount; + if (pricedCount > 0) + group.openRouterReferenceUsd = (group.openRouterReferenceUsd ?? 0) + row.openRouterReferenceUsd; + if (group.pricingFetchedAt !== (row.pricingFetchedAt ?? null)) group.pricingFetchedAt = null; + group.costStatus = + group.pricedRequestCount === 0 ? "unavailable" : group.unpricedRequestCount > 0 ? "partial" : "priced"; + } + return [...groups.values()]; +} + +export function unknownModelUsage(turn) { + return mergeModelUsage([ + { + modelId: null, + input: turn.input ?? null, + cached: turn.cached ?? null, + output: turn.output ?? null, + total: turn.total ?? null, + requestCount: Number.isSafeInteger(turn.requests) ? turn.requests : (turn.requestCount ?? 0), + }, + ]); +} diff --git a/openrouter-pricing.mjs b/openrouter-pricing.mjs new file mode 100644 index 0000000..cb07d41 --- /dev/null +++ b/openrouter-pricing.mjs @@ -0,0 +1,321 @@ +import fs from "node:fs/promises"; +import path from "node:path"; +import { randomUUID } from "node:crypto"; +import { resolveServiceTierRate } from "./service-tier-pricing.mjs"; + +export const OPENROUTER_CATALOG_URL = "https://openrouter.ai/api/v1/models"; +export const PRICE_CACHE_MAX_AGE_MS = 24 * 60 * 60 * 1000; +export const PRICE_FETCH_TIMEOUT_MS = 10_000; +const MODEL_NAMES = [ + ["gpt-6.1-sol", "GPT-6.1 Sol"], + ["gpt-6-astra", "GPT-6 Astra"], + ["gpt-6.1-terra", "GPT-6.1 Terra"], + ["gpt-6-sol", "GPT-6 Sol"], + ["gpt-6-luna", "GPT-6 Luna"], + ["gpt-5.6-sol", "GPT-5.6 Sol"], + ["gpt-5.6-terra", "GPT-5.6 Terra"], + ["gpt-5.6-luna", "GPT-5.6 Luna"], +]; +const aliases = new Map( + MODEL_NAMES.flatMap(([id]) => [ + [id, `openai/${id}`], + [`openai/${id}`, `openai/${id}`], + ]), +); +const PRICE_FIELDS = ["prompt", "completion", "input_cache_read", "input_cache_write"]; +const pendingLoads = new Map(); + +export function getSupportedModels() { + return MODEL_NAMES.map(([id, modelLabel]) => ({ modelId: `openai/${id}`, modelLabel })); +} + +export function resolveOpenRouterModel(modelValue) { + return typeof modelValue === "string" ? (aliases.get(modelValue) ?? null) : null; +} + +function priceNumber(value) { + if (typeof value !== "number" && typeof value !== "string") return null; + if (typeof value === "string" && (!value.trim() || !/^(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i.test(value))) + return null; + const number = Number(value); + return Number.isFinite(number) && number >= 0 ? number : null; +} + +function cleanPricing(value) { + if (!value || typeof value !== "object" || Array.isArray(value)) return null; + const pricing = Object.fromEntries(PRICE_FIELDS.map((key) => [key, priceNumber(value[key])])); + const overrides = []; + let invalidOverrides = false; + if (value.overrides != null && !Array.isArray(value.overrides)) invalidOverrides = true; + for (const row of Array.isArray(value.overrides) ? value.overrides : []) { + if (!row || !Number.isSafeInteger(row.min_prompt_tokens) || row.min_prompt_tokens < 0) { + invalidOverrides = true; + continue; + } + const rates = Object.fromEntries(PRICE_FIELDS.map((key) => [key, priceNumber(row[key])])); + if ( + rates.prompt === null || + rates.completion === null || + overrides.some((r) => r.min_prompt_tokens === row.min_prompt_tokens) + ) { + invalidOverrides = true; + } + overrides.push({ min_prompt_tokens: row.min_prompt_tokens, ...rates }); + } + return { + ...pricing, + overrides: overrides.sort((a, b) => a.min_prompt_tokens - b.min_prompt_tokens), + invalidOverrides, + }; +} + +export function parseOpenRouterCatalog(payload, fetchedAt = Date.now()) { + if (!payload || !Array.isArray(payload.data)) throw new Error("invalid-catalog"); + const timestamp = typeof fetchedAt === "number" ? fetchedAt : Date.parse(fetchedAt); + if (!Number.isFinite(timestamp)) throw new Error("invalid-catalog-time"); + const modelsById = {}; + const seen = new Set(); + for (const row of payload.data) { + const modelId = resolveOpenRouterModel(row?.id); + if (!modelId || row.id !== modelId) continue; + if (seen.has(modelId)) { + delete modelsById[modelId]; + continue; + } + seen.add(modelId); + const pricing = cleanPricing(row.pricing); + if (!pricing) continue; + modelsById[modelId] = { + modelId, + modelLabel: getSupportedModels().find((m) => m.modelId === modelId).modelLabel, + pricing, + }; + } + return { + schemaVersion: 1, + sourceUrl: OPENROUTER_CATALOG_URL, + fetchedAt: new Date(timestamp).toISOString(), + modelsById, + }; +} + +function validateCachedCatalog(value, now) { + if ( + !value || + value.schemaVersion !== 1 || + value.sourceUrl !== OPENROUTER_CATALOG_URL || + !value.modelsById || + Array.isArray(value.modelsById) + ) { + return null; + } + const time = Date.parse(value.fetchedAt); + if (!Number.isFinite(time) || time > now) return null; + const rebuilt = parseOpenRouterCatalog( + { data: Object.entries(value.modelsById).map(([id, model]) => ({ id, pricing: model?.pricing })) }, + time, + ); + // Keep a deliberately invalid override marker through cache validation. + for (const [id, model] of Object.entries(rebuilt.modelsById)) { + model.pricing.invalidOverrides ||= value.modelsById[id]?.pricing?.invalidOverrides === true; + } + return hasUsableModel(rebuilt) ? rebuilt : null; +} + +function hasUsableModel(catalog) { + return Object.values(catalog.modelsById).some( + ({ pricing }) => pricing.prompt !== null && pricing.completion !== null, + ); +} + +async function writeSnapshot(cacheFile, catalog) { + if (!cacheFile) return; + await fs.mkdir(path.dirname(cacheFile), { recursive: true }); + const temporary = `${cacheFile}.${randomUUID()}.tmp`; + try { + await fs.writeFile(temporary, JSON.stringify(catalog), { encoding: "utf8", mode: 0o600 }); + await fs.rename(temporary, cacheFile); + } finally { + await fs.rm(temporary, { force: true }).catch(() => {}); + } +} + +async function loadCatalog({ + cacheFile, + fetchImpl = globalThis.fetch, + now = Date.now(), + maxAgeMs = PRICE_CACHE_MAX_AGE_MS, + timeoutMs = PRICE_FETCH_TIMEOUT_MS, +}) { + const clock = typeof now === "function" ? now() : now; + let cached = null; + if (cacheFile) { + try { + cached = validateCachedCatalog(JSON.parse(await fs.readFile(cacheFile, "utf8")), clock); + } catch { + /* A corrupt cache is never trusted or deleted. */ + } + } + if (cached && clock - Date.parse(cached.fetchedAt) <= maxAgeMs) { + return { catalog: cached, status: "fresh", fetchedAt: cached.fetchedAt, errorCode: null }; + } + let errorCode = null; + try { + const response = await fetchImpl(OPENROUTER_CATALOG_URL, { + method: "GET", + headers: { Accept: "application/json" }, + credentials: "omit", + redirect: "error", + signal: AbortSignal.timeout(timeoutMs), + }); + if (!response.ok) { + errorCode = Number.isInteger(response.status) ? `http-${response.status}` : "http-error"; + throw new Error("catalog-http-error"); + } + let json; + try { + json = await response.json(); + } catch { + errorCode = "invalid-json"; + throw new Error("invalid-json"); + } + let catalog; + try { + catalog = parseOpenRouterCatalog(json, clock); + } catch { + errorCode = "invalid-catalog"; + throw new Error("invalid-catalog"); + } + if (!hasUsableModel(catalog)) { + errorCode = "invalid-catalog"; + throw new Error("invalid-catalog"); + } + try { + await writeSnapshot(cacheFile, catalog); + } catch { + errorCode = "cache-write-failed"; + } + return { catalog, status: "fresh", fetchedAt: catalog.fetchedAt, errorCode }; + } catch (error) { + errorCode ||= ["TimeoutError", "AbortError"].includes(error?.name) ? "timeout" : "network-error"; + return { + catalog: cached, + status: cached ? "stale" : "unavailable", + fetchedAt: cached?.fetchedAt ?? null, + errorCode, + }; + } +} + +export async function loadOpenRouterCatalog(options = {}) { + const key = options.cacheFile; + if (key && pendingLoads.has(key)) return pendingLoads.get(key); + const pending = loadCatalog(options); + if (key) pendingLoads.set(key, pending); + try { + return await pending; + } finally { + if (key && pendingLoads.get(key) === pending) pendingLoads.delete(key); + } +} + +function tokenNumber(value) { + return typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? value : null; +} + +function cacheWrites(request, excluded) { + const hasSingle = request.cacheWrite != null; + const hasSplit = request.cacheWrite30m != null || request.cacheWrite12h != null; + if (hasSingle && hasSplit) return null; + if (hasSingle) return tokenNumber(request.cacheWrite); + if (hasSplit) { + const a = tokenNumber(request.cacheWrite30m); + const b = tokenNumber(request.cacheWrite12h); + return a === null || b === null ? null : a + b; + } + return excluded ? 0 : null; +} + +function calculate(input, cached, output, writes, rates) { + const ordinary = input - cached - writes; + if (ordinary < 0 || !rates) return null; + const categories = [ + [ordinary, rates.prompt], + [cached, rates.input_cache_read], + [writes, rates.input_cache_write], + [output, rates.completion], + ]; + if ( + categories.some(([tokens, rate]) => tokens > 0 && (typeof rate !== "number" || !Number.isFinite(rate) || rate < 0)) + ) + return null; + const result = categories.reduce((sum, [tokens, rate]) => sum + (tokens === 0 ? 0 : tokens * rate), 0); + return Number.isFinite(result) && result >= 0 ? result : null; +} + +export function estimateRequestCost(request = {}, modelPricing, options = {}) { + const billingContext = options.billingContext ?? "openrouter-api"; + const serviceTier = options.serviceTier ?? request.serviceTier ?? null; + const cacheWriteTtl = options.cacheWriteTtl ?? request.cacheWriteTtl ?? null; + const result = { + openRouterReferenceUsd: null, + apiCostUsd: null, + subscriptionAllowanceWeight: null, + subscriptionWeightedReferenceUsd: null, + pricingBasis: "openrouter-public-base", + serviceTier, + cacheWriteTtl, + status: "missing-price", + }; + const modelId = resolveOpenRouterModel(request.model ?? request.modelId); + if (!modelId) return { ...result, status: "unknown-model" }; + if ( + !["openrouter-api", "openai-api", "codex-subscription-allowance", "codex-purchased-credits"].includes( + billingContext, + ) + ) { + return { ...result, status: "unknown-mode" }; + } + if (!modelPricing || (modelPricing.modelId && modelPricing.modelId !== modelId)) return result; + const pricing = modelPricing.pricing ?? modelPricing; + const input = tokenNumber(request.input); + const cached = tokenNumber(request.cached); + const output = tokenNumber(request.output); + const writes = cacheWrites(request, options.cacheWritePolicy === "excluded"); + if ([input, cached, output, writes].includes(null) || cached + writes > input) + return { ...result, status: "missing-usage" }; + const subscription = ["codex-subscription-allowance", "codex-purchased-credits"].includes(billingContext); + let rate = pricing; + if (!subscription) { + if (pricing.invalidOverrides) return { ...result, status: "unsupported-tier" }; + for (const row of pricing.overrides ?? []) if (input >= row.min_prompt_tokens) rate = row; + } + const unsupportedReferenceTtl = + !subscription && ((cacheWriteTtl === "12h" && writes > 0) || request.cacheWrite12h > 0); + if (unsupportedReferenceTtl && billingContext !== "openai-api") { + return { ...result, status: "unsupported-tier" }; + } + if (billingContext === "openrouter-api" && serviceTier && serviceTier !== "standard") + return { ...result, status: "unsupported-tier" }; + result.openRouterReferenceUsd = unsupportedReferenceTtl + ? null + : calculate(input, cached, output, subscription ? 0 : writes, rate); + if (result.openRouterReferenceUsd !== null) + result.status = options.pricingStatus === "stale" ? "stale-price" : "priced"; + if (billingContext === "openai-api") { + const resolved = resolveServiceTierRate({ + billingContext, + modelId, + serviceTier, + cacheWriteTtl, + inputTokens: input, + profile: options.profile, + now: options.now, + }); + result.apiCostUsd = resolved.rate ? calculate(input, cached, output, writes, resolved.rate) : null; + result.status = result.apiCostUsd === null ? resolved.status : "priced"; + } + // Subscription weighting is intentionally unavailable without an independently + // verified funding/mode profile. Public API reference is not a subscription bill. + return result; +} diff --git a/package.json b/package.json index 735809a..545b0f0 100644 --- a/package.json +++ b/package.json @@ -15,11 +15,11 @@ "node": ">=22" }, "scripts": { - "check": "node --check token-stats.mjs && node --check runtime-paths.mjs && node --check protocol.mjs && node --check speed-stats.mjs && node --check history-archive.mjs && node --check tokens-ui-for-codex-panel.js && npm run lint && npm run format:check", + "check": "node --check token-stats.mjs && node --check runtime-paths.mjs && node --check protocol.mjs && node --check speed-stats.mjs && node --check history-archive.mjs && node --check openrouter-pricing.mjs && node --check service-tier-pricing.mjs && node --check model-usage.mjs && node --check tokens-ui-for-codex-panel.js && npm run lint && npm run format:check", "lint": "eslint .", "format:check": "prettier --check \"*.js\" \"*.mjs\" \"scripts/*.mjs\" \"test/*.mjs\"", "format": "prettier --write \"*.js\" \"*.mjs\" \"scripts/*.mjs\" \"test/*.mjs\"", - "test": "node --test --test-force-exit test/runtime-paths.test.mjs test/token-stats.test.mjs test/history-archive.test.mjs && npm run test:snapshots", + "test": "node --test --test-force-exit test/runtime-paths.test.mjs test/token-stats.test.mjs test/history-archive.test.mjs test/openrouter-pricing.test.mjs test/service-tier-pricing.test.mjs test/pricing-packaging.test.mjs && npm run test:snapshots", "test:snapshots": "node scripts/stats-snapshot.mjs --check", "test:auth-ui": "node --test --test-force-exit test/auth-ui.playwright.test.mjs", "test:watchdog": "powershell -NoProfile -ExecutionPolicy Bypass -File scripts/verify-watchdog.ps1", diff --git a/protocol.mjs b/protocol.mjs index bd07a0e..20d467b 100644 --- a/protocol.mjs +++ b/protocol.mjs @@ -1,7 +1,10 @@ +import { getSupportedModels } from "./openrouter-pricing.mjs"; +import { MODEL_COST_STATUSES } from "./model-usage.mjs"; + export const PROTOCOL_NAME = "tokens-ui-for-codex"; // v4 起只接受 auth v2:正文、版本、时间戳、nonce、keyId 与 seq 全部进入 HMAC。 // 旧监控会因 supportedSchemaVersions 不含 4 而明确失败,安装器负责先停旧进程。 -export const PROTOCOL_SCHEMA_VERSION = 4; +export const PROTOCOL_SCHEMA_VERSION = 5; export const SUPPORTED_SCHEMA_VERSIONS = Object.freeze([PROTOCOL_SCHEMA_VERSION]); export const AUTH_VERSION = 2; export const AUTH_DOMAIN = "tokens-ui-for-codex/auth-v2"; @@ -28,6 +31,8 @@ export const PROTOCOL_CAPABILITIES = Object.freeze([ "timing-v1", "history-range-v1", "context-breakdown-estimated-v1", + "model-usage-v1", + "public-price-reference-v1", ]); export const HEALTH_STATUSES = Object.freeze([ @@ -65,6 +70,66 @@ function isNullableString(value) { return value == null || typeof value === "string"; } +const modelLabels = new Map(getSupportedModels().map((model) => [model.modelId, model.modelLabel])); + +function validateModelIdentity(value) { + return value.modelId === null + ? value.modelLabel === "未知模型" + : modelLabels.has(value.modelId) && modelLabels.get(value.modelId) === value.modelLabel; +} + +function validPriceTimestamp(value) { + return value === null || (typeof value === "string" && Number.isFinite(Date.parse(value))); +} + +export function validateModelUsage(rows, label = "modelUsage") { + if (!Array.isArray(rows) || rows.length > 9) return [label + " must be an array of at most 9 models"]; + const errors = []; + const seen = new Set(); + for (const row of rows) { + if (!row || typeof row !== "object" || Array.isArray(row)) { + errors.push(label + " entry is invalid"); + continue; + } + if (!validateModelIdentity(row) || seen.has(row.modelId)) + errors.push(label + " model identity is invalid or repeated"); + seen.add(row.modelId); + for (const key of ["input", "cached", "output", "total"]) { + if (row[key] !== null && (!Number.isSafeInteger(row[key]) || row[key] < 0)) + errors.push(label + "." + key + " is invalid"); + } + if (row.openRouterReferenceUsd !== null && !isNonNegativeNumber(row.openRouterReferenceUsd)) + errors.push(label + ".openRouterReferenceUsd is invalid"); + for (const key of ["requestCount", "pricedRequestCount", "unpricedRequestCount"]) { + if (!Number.isSafeInteger(row[key]) || row[key] < 0) errors.push(label + "." + key + " is invalid"); + } + if (row.pricedRequestCount + row.unpricedRequestCount !== row.requestCount) + errors.push(label + " cost coverage is invalid"); + const status = row.pricedRequestCount === 0 ? "unavailable" : row.unpricedRequestCount > 0 ? "partial" : "priced"; + if (row.costStatus !== status || (row.pricedRequestCount === 0) !== (row.openRouterReferenceUsd === null)) + errors.push(label + " cost status is inconsistent"); + if (row.modelId === null && row.pricedRequestCount !== 0) errors.push(label + " unknown model cannot be priced"); + if (!validPriceTimestamp(row.pricingFetchedAt)) errors.push(label + " pricingFetchedAt is invalid"); + if (row.input !== null && row.cached !== null && row.cached > row.input) + errors.push(label + " cached exceeds input"); + } + return errors; +} + +function validateRequestCost(request) { + const errors = []; + if (!validateModelIdentity(request)) errors.push("request model identity is invalid"); + if (request.openRouterReferenceUsd !== null && !isNonNegativeNumber(request.openRouterReferenceUsd)) + errors.push("request cost is invalid"); + if (!MODEL_COST_STATUSES.includes(request.costStatus)) errors.push("request costStatus is invalid"); + if (["priced", "stale-price"].includes(request.costStatus) !== (request.openRouterReferenceUsd !== null)) + errors.push("request cost status is inconsistent"); + if (!validPriceTimestamp(request.pricingFetchedAt)) errors.push("request pricingFetchedAt is invalid"); + if (request.modelId === null && request.openRouterReferenceUsd !== null) + errors.push("unknown request model cannot be priced"); + return errors; +} + export function canonicalJson(value, stack = new Set()) { if (value === null || typeof value === "string" || typeof value === "boolean") return JSON.stringify(value); if (typeof value === "number") { @@ -197,6 +262,12 @@ function validateTurn(turn, index) { errors.push("turn.timingQuality is invalid"); } if (turn.input != null && turn.cached != null && turn.cached > turn.input) errors.push("turn.cached exceeds input"); + errors.push(...validateModelUsage(turn.modelUsage, "turn.modelUsage")); + if ( + Array.isArray(turn.modelUsage) && + turn.modelUsage.reduce((count, row) => count + (row?.requestCount || 0), 0) !== turn.requests + ) + errors.push("turn.modelUsage request count mismatch"); return errors; } @@ -224,6 +295,12 @@ function validateRangeSummary(summary) { return ["rangeSummary must be null or an object"]; } const errors = []; + errors.push(...validateModelUsage(summary.modelUsage, "rangeSummary.modelUsage")); + if ( + Array.isArray(summary.modelUsage) && + summary.modelUsage.reduce((count, row) => count + (row?.requestCount || 0), 0) !== summary.requestCount + ) + errors.push("rangeSummary.modelUsage request count mismatch"); if (!["today", "3d", "7d", "30d", "all"].includes(summary.range)) { errors.push("rangeSummary.range is invalid"); } @@ -270,6 +347,7 @@ function validateRequest(request, index) { if (request.input != null && request.cached != null && request.cached > request.input) { errors.push("request.cached exceeds input"); } + errors.push(...validateRequestCost(request)); return errors; } @@ -300,6 +378,19 @@ export function validatePayload(payload, { allowLegacy = true, requireAuth = fal errors.push("unsupported schemaVersion: " + String(payload.schemaVersion)); } if (payload.authRequired !== true) errors.push("authRequired must be true"); + errors.push(...validateModelUsage(payload.modelUsage)); + if ( + Array.isArray(payload.modelUsage) && + payload.modelUsage.reduce((count, row) => count + (row?.requestCount || 0), 0) !== payload.requestCount + ) + errors.push("modelUsage request count mismatch"); + if ( + !payload.pricing || + !["fresh", "stale", "unavailable"].includes(payload.pricing.status) || + !validPriceTimestamp(payload.pricing.fetchedAt) || + payload.pricing.sourceUrl !== "https://openrouter.ai/api/v1/models" + ) + errors.push("pricing is invalid"); if (requireAuth) errors.push(...validateAuth(payload.auth)); if (!Array.isArray(payload.capabilities) || payload.capabilities.some((item) => typeof item !== "string")) { errors.push("capabilities must be an array of strings"); diff --git a/schemas/payload-v5.schema.json b/schemas/payload-v5.schema.json new file mode 100644 index 0000000..912a5b8 --- /dev/null +++ b/schemas/payload-v5.schema.json @@ -0,0 +1,973 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Tokens UI For Codex signed schema v5 payload", + "type": "object", + "required": [ + "protocolName", + "schemaVersion", + "authRequired", + "auth", + "capabilities", + "modelContextWindow", + "requestCount", + "currentTurnRequestCount", + "sessionTotal", + "sessionInput", + "sessionCached", + "sessionOutput", + "sessionCacheHitRate", + "sessionTps", + "contextUsed", + "contextBreakdown", + "sessionWallMs", + "sessionToolWallMs", + "sessionToolCallCount", + "sessionTtftAvgMs", + "sessionTimingSampleCount", + "rangeSummary", + "turnTotal", + "turnInput", + "turnWallMs", + "turnToolWallMs", + "turnToolCallCount", + "turnTtftMs", + "turnTimingCoverage", + "turnTimingQuality", + "turnCached", + "turnOutput", + "turnCacheHitRate", + "lastRequestTps", + "turnTps", + "turnWallTps", + "genEstimateTps", + "compactTps", + "compactSpeedSampleCount", + "compactSpeedWindow", + "compactSpeedConfidence", + "effectiveTps", + "turnEffectiveTps", + "effectiveSampleCount", + "effectiveWindow", + "speedTimingConfidence", + "turnsLimit", + "requestsLimit", + "sessionChanged", + "snapshot", + "currentTurnIndex", + "turnTotalCount", + "turnsTruncated", + "turns", + "requestDetailsTruncated", + "requestDetails", + "requestDetailTotal", + "modelSwitchCount", + "cumulativeResetCount", + "health", + "updatedAt", + "modelUsage", + "pricing" + ], + "properties": { + "protocolName": { + "const": "tokens-ui-for-codex" + }, + "schemaVersion": { + "const": 5 + }, + "authRequired": { + "const": true + }, + "auth": { + "type": "object", + "required": ["v", "keyId", "ts", "nonce", "seq", "mac"], + "properties": { + "v": { + "const": 2 + }, + "keyId": { + "type": "string", + "pattern": "^[0-9a-f]{32}$" + }, + "ts": { + "type": "integer", + "minimum": 0 + }, + "nonce": { + "type": "string", + "pattern": "^[0-9a-f]{32}$" + }, + "seq": { + "type": "integer", + "minimum": 1 + }, + "mac": { + "type": "string", + "pattern": "^[0-9a-f]{64}$" + } + }, + "additionalProperties": false + }, + "capabilities": { + "type": "array", + "items": { + "type": "string" + }, + "contains": { + "const": "auth-v2" + } + }, + "modelContextWindow": { + "type": ["number", "null"], + "minimum": 0 + }, + "requestCount": { + "type": "number", + "minimum": 0 + }, + "currentTurnRequestCount": { + "type": "number", + "minimum": 0 + }, + "sessionTotal": { + "type": ["number", "null"], + "minimum": 0 + }, + "sessionInput": { + "type": ["number", "null"], + "minimum": 0 + }, + "sessionCached": { + "type": ["number", "null"], + "minimum": 0 + }, + "sessionOutput": { + "type": ["number", "null"], + "minimum": 0 + }, + "sessionCacheHitRate": { + "type": ["number", "null"], + "minimum": 0, + "maximum": 1 + }, + "sessionTps": { + "type": ["number", "null"], + "minimum": 0 + }, + "contextUsed": { + "type": ["number", "null"], + "minimum": 0 + }, + "contextBreakdown": { + "type": ["object", "null"], + "required": [ + "estimated", + "estimatorVersion", + "systemPromptTokens", + "toolDefinitionTokens", + "conversationTokens", + "otherTokens" + ], + "properties": { + "estimated": { + "const": true + }, + "estimatorVersion": { + "type": "integer", + "minimum": 1 + }, + "systemPromptTokens": { + "type": ["integer", "null"], + "minimum": 0 + }, + "toolDefinitionTokens": { + "type": ["integer", "null"], + "minimum": 0 + }, + "conversationTokens": { + "type": ["integer", "null"], + "minimum": 0 + }, + "otherTokens": { + "type": ["integer", "null"], + "minimum": 0 + } + }, + "additionalProperties": false + }, + "sessionWallMs": { + "type": ["number", "null"], + "minimum": 0 + }, + "sessionToolWallMs": { + "type": ["number", "null"], + "minimum": 0 + }, + "sessionToolCallCount": { + "type": "number", + "minimum": 0 + }, + "sessionTtftAvgMs": { + "type": ["number", "null"], + "minimum": 0 + }, + "sessionTimingSampleCount": { + "type": "number", + "minimum": 0 + }, + "rangeSummary": { + "type": ["object", "null"], + "required": [ + "range", + "available", + "retentionDays", + "cutoffAt", + "generatedAt", + "availableFrom", + "availableThrough", + "sessionCount", + "turnCount", + "requestCount", + "input", + "cached", + "output", + "total", + "cacheComplete", + "wallMs", + "toolWallMs", + "toolCallCount", + "ttftAvgMs", + "effectiveMs", + "effectiveTps", + "timingSampleCount", + "ttftSampleCount", + "modelUsage" + ], + "properties": { + "range": { + "enum": ["today", "3d", "7d", "30d", "all"] + }, + "available": { + "type": "boolean" + }, + "retentionDays": { + "type": "number", + "minimum": 1 + }, + "cutoffAt": { + "type": ["string", "null"] + }, + "generatedAt": { + "type": "string" + }, + "availableFrom": { + "type": ["string", "null"] + }, + "availableThrough": { + "type": ["string", "null"] + }, + "sessionCount": { + "type": "number", + "minimum": 0 + }, + "turnCount": { + "type": "number", + "minimum": 0 + }, + "requestCount": { + "type": "number", + "minimum": 0 + }, + "input": { + "type": "number", + "minimum": 0 + }, + "cached": { + "type": "number", + "minimum": 0 + }, + "output": { + "type": "number", + "minimum": 0 + }, + "total": { + "type": "number", + "minimum": 0 + }, + "cacheComplete": { + "type": "boolean" + }, + "wallMs": { + "type": ["number", "null"], + "minimum": 0 + }, + "toolWallMs": { + "type": ["number", "null"], + "minimum": 0 + }, + "toolCallCount": { + "type": "number", + "minimum": 0 + }, + "ttftAvgMs": { + "type": ["number", "null"], + "minimum": 0 + }, + "effectiveMs": { + "type": ["number", "null"], + "minimum": 0 + }, + "effectiveTps": { + "type": ["number", "null"], + "minimum": 0 + }, + "timingSampleCount": { + "type": "number", + "minimum": 0 + }, + "ttftSampleCount": { + "type": "number", + "minimum": 0 + }, + "modelUsage": { + "type": "array", + "maxItems": 9, + "items": { + "type": "object", + "required": [ + "modelId", + "modelLabel", + "openRouterReferenceUsd", + "pricingFetchedAt", + "costStatus", + "input", + "cached", + "output", + "total", + "requestCount", + "pricedRequestCount", + "unpricedRequestCount" + ], + "properties": { + "modelId": { + "enum": [ + "openai/gpt-6.1-sol", + "openai/gpt-6-astra", + "openai/gpt-6.1-terra", + "openai/gpt-6-sol", + "openai/gpt-6-luna", + "openai/gpt-5.6-sol", + "openai/gpt-5.6-terra", + "openai/gpt-5.6-luna", + null + ] + }, + "modelLabel": { + "type": "string" + }, + "openRouterReferenceUsd": { + "type": ["number", "null"], + "minimum": 0 + }, + "pricingFetchedAt": { + "type": ["string", "null"], + "format": "date-time" + }, + "costStatus": { + "enum": ["priced", "partial", "unavailable"] + }, + "input": { + "type": ["number", "null"], + "minimum": 0 + }, + "cached": { + "type": ["number", "null"], + "minimum": 0 + }, + "output": { + "type": ["number", "null"], + "minimum": 0 + }, + "total": { + "type": ["number", "null"], + "minimum": 0 + }, + "requestCount": { + "type": "integer", + "minimum": 0 + }, + "pricedRequestCount": { + "type": "integer", + "minimum": 0 + }, + "unpricedRequestCount": { + "type": "integer", + "minimum": 0 + } + }, + "additionalProperties": false + } + } + }, + "additionalProperties": false + }, + "turnTotal": { + "type": "number", + "minimum": 0 + }, + "turnInput": { + "type": "number", + "minimum": 0 + }, + "turnWallMs": { + "type": ["number", "null"], + "minimum": 0 + }, + "turnToolWallMs": { + "type": ["number", "null"], + "minimum": 0 + }, + "turnToolCallCount": { + "type": "number", + "minimum": 0 + }, + "turnTtftMs": { + "type": ["number", "null"], + "minimum": 0 + }, + "turnTimingCoverage": { + "type": ["number", "null"], + "minimum": 0 + }, + "turnTimingQuality": { + "enum": ["high", "medium", "low"] + }, + "turnCached": { + "type": ["number", "null"], + "minimum": 0 + }, + "turnOutput": { + "type": "number", + "minimum": 0 + }, + "turnCacheHitRate": { + "type": ["number", "null"], + "minimum": 0, + "maximum": 1 + }, + "lastRequestTps": { + "type": ["number", "null"], + "minimum": 0 + }, + "turnTps": { + "type": ["number", "null"], + "minimum": 0 + }, + "turnWallTps": { + "type": ["number", "null"], + "minimum": 0, + "description": "当前轮输出 tokens ÷ 整轮墙钟耗时;包含工具与网络等待" + }, + "genEstimateTps": { + "type": ["number", "null"], + "minimum": 0 + }, + "compactTps": { + "type": ["number", "null"], + "minimum": 0, + "description": "紧凑条稳健估算:最近有效请求生成速度的中位数;样本不足时为 null" + }, + "compactSpeedSampleCount": { + "type": "number", + "minimum": 0 + }, + "compactSpeedWindow": { + "type": "number", + "minimum": 0 + }, + "compactSpeedConfidence": { + "type": ["string", "null"], + "enum": ["high", "medium", "low", null] + }, + "effectiveTps": { + "type": ["number", "null"], + "minimum": 0 + }, + "turnEffectiveTps": { + "type": ["number", "null"], + "minimum": 0 + }, + "effectiveSampleCount": { + "type": "number", + "minimum": 0 + }, + "effectiveWindow": { + "type": "number", + "minimum": 0 + }, + "speedTimingConfidence": { + "type": ["string", "null"], + "enum": ["high", "medium", "low", null] + }, + "turnsLimit": { + "type": "number", + "minimum": 0 + }, + "requestsLimit": { + "type": "number", + "minimum": 0 + }, + "sessionChanged": { + "type": "boolean" + }, + "snapshot": { + "type": "boolean" + }, + "currentTurnIndex": { + "type": "number", + "minimum": 0 + }, + "turnTotalCount": { + "type": "number", + "minimum": 0 + }, + "turnsTruncated": { + "type": "boolean" + }, + "turns": { + "type": "array", + "maxItems": 500, + "items": { + "type": "object", + "required": ["index", "startLabel", "requests", "input", "cached", "uncached", "output", "total", "modelUsage"], + "properties": { + "index": { + "type": "integer", + "minimum": 1 + }, + "startLabel": { + "type": "string" + }, + "requests": { + "type": "number", + "minimum": 0 + }, + "input": { + "type": "number", + "minimum": 0 + }, + "cached": { + "type": ["number", "null"], + "minimum": 0 + }, + "uncached": { + "type": ["number", "null"], + "minimum": 0 + }, + "output": { + "type": "number", + "minimum": 0 + }, + "total": { + "type": "number", + "minimum": 0 + }, + "wallMs": { + "type": ["number", "null"], + "minimum": 0 + }, + "toolWallMs": { + "type": ["number", "null"], + "minimum": 0 + }, + "toolCallCount": { + "type": "number", + "minimum": 0 + }, + "ttftMs": { + "type": ["number", "null"], + "minimum": 0 + }, + "timingCoverage": { + "type": ["number", "null"], + "minimum": 0 + }, + "timingQuality": { + "enum": ["high", "medium", "low"] + }, + "modelUsage": { + "type": "array", + "maxItems": 9, + "items": { + "type": "object", + "required": [ + "modelId", + "modelLabel", + "openRouterReferenceUsd", + "pricingFetchedAt", + "costStatus", + "input", + "cached", + "output", + "total", + "requestCount", + "pricedRequestCount", + "unpricedRequestCount" + ], + "properties": { + "modelId": { + "enum": [ + "openai/gpt-6.1-sol", + "openai/gpt-6-astra", + "openai/gpt-6.1-terra", + "openai/gpt-6-sol", + "openai/gpt-6-luna", + "openai/gpt-5.6-sol", + "openai/gpt-5.6-terra", + "openai/gpt-5.6-luna", + null + ] + }, + "modelLabel": { + "type": "string" + }, + "openRouterReferenceUsd": { + "type": ["number", "null"], + "minimum": 0 + }, + "pricingFetchedAt": { + "type": ["string", "null"], + "format": "date-time" + }, + "costStatus": { + "enum": ["priced", "partial", "unavailable"] + }, + "input": { + "type": ["number", "null"], + "minimum": 0 + }, + "cached": { + "type": ["number", "null"], + "minimum": 0 + }, + "output": { + "type": ["number", "null"], + "minimum": 0 + }, + "total": { + "type": ["number", "null"], + "minimum": 0 + }, + "requestCount": { + "type": "integer", + "minimum": 0 + }, + "pricedRequestCount": { + "type": "integer", + "minimum": 0 + }, + "unpricedRequestCount": { + "type": "integer", + "minimum": 0 + } + }, + "additionalProperties": false + } + } + }, + "additionalProperties": true + } + }, + "requestDetailsTruncated": { + "type": "boolean" + }, + "requestDetailTotal": { + "type": "number", + "minimum": 0 + }, + "requestDetails": { + "type": "array", + "maxItems": 500, + "items": { + "type": "object", + "required": [ + "index", + "time", + "input", + "cached", + "uncached", + "output", + "total", + "modelId", + "modelLabel", + "openRouterReferenceUsd", + "pricingFetchedAt", + "costStatus" + ], + "properties": { + "index": { + "type": "integer", + "minimum": 1 + }, + "time": { + "type": "string" + }, + "input": { + "type": ["number", "null"], + "minimum": 0 + }, + "cached": { + "type": ["number", "null"], + "minimum": 0 + }, + "uncached": { + "type": ["number", "null"], + "minimum": 0 + }, + "output": { + "type": ["number", "null"], + "minimum": 0 + }, + "total": { + "type": ["number", "null"], + "minimum": 0 + }, + "modelId": { + "enum": [ + "openai/gpt-6.1-sol", + "openai/gpt-6-astra", + "openai/gpt-6.1-terra", + "openai/gpt-6-sol", + "openai/gpt-6-luna", + "openai/gpt-5.6-sol", + "openai/gpt-5.6-terra", + "openai/gpt-5.6-luna", + null + ] + }, + "modelLabel": { + "type": "string" + }, + "openRouterReferenceUsd": { + "type": ["number", "null"], + "minimum": 0 + }, + "pricingFetchedAt": { + "type": ["string", "null"], + "format": "date-time" + }, + "costStatus": { + "enum": [ + "priced", + "partial", + "unknown-model", + "missing-price", + "missing-usage", + "stale-price", + "unavailable", + "unsupported-tier", + "unknown-mode" + ] + } + }, + "additionalProperties": false + } + }, + "modelSwitchCount": { + "type": "number", + "minimum": 0 + }, + "cumulativeResetCount": { + "type": "number", + "minimum": 0 + }, + "updatedAt": { + "type": "string", + "minLength": 1 + }, + "health": { + "type": "object", + "required": [ + "status", + "recoveryState", + "platform", + "platformSupported", + "nodeSupported", + "cdpReachable", + "pageAttached", + "targetMode", + "targetSelection", + "targetCount", + "dataFresh", + "lastSuccessAt", + "lastErrorCode", + "lastErrorAt", + "monitorRunning", + "autostartInstalled", + "installationState" + ], + "properties": { + "status": { + "enum": [ + "starting", + "healthy", + "waiting-for-cdp", + "waiting-for-page", + "waiting-for-data", + "target-selection-required", + "protocol-mismatch", + "stale-data", + "recovering", + "failed" + ] + }, + "recoveryState": { + "enum": ["idle", "retrying", "recovered", "failed"] + }, + "platform": { + "const": "windows" + }, + "platformSupported": { + "type": "boolean" + }, + "nodeSupported": { + "type": "boolean" + }, + "cdpReachable": { + "type": "boolean" + }, + "pageAttached": { + "type": "boolean" + }, + "targetMode": { + "enum": ["auto", "focused", "manual"] + }, + "targetSelection": { + "enum": ["unknown", "single", "focused", "visible", "manual", "required"] + }, + "targetCount": { + "type": "integer", + "minimum": 0 + }, + "dataFresh": { + "type": "boolean" + }, + "lastSuccessAt": { + "type": ["string", "null"] + }, + "lastErrorCode": { + "type": ["string", "null"] + }, + "lastErrorAt": { + "type": ["string", "null"] + }, + "monitorRunning": { + "type": "boolean" + }, + "autostartInstalled": { + "type": ["boolean", "null"] + }, + "installationState": { + "enum": ["unknown", "missing", "ready", "invalid"] + } + }, + "additionalProperties": true + }, + "modelUsage": { + "type": "array", + "maxItems": 9, + "items": { + "type": "object", + "required": [ + "modelId", + "modelLabel", + "openRouterReferenceUsd", + "pricingFetchedAt", + "costStatus", + "input", + "cached", + "output", + "total", + "requestCount", + "pricedRequestCount", + "unpricedRequestCount" + ], + "properties": { + "modelId": { + "enum": [ + "openai/gpt-6.1-sol", + "openai/gpt-6-astra", + "openai/gpt-6.1-terra", + "openai/gpt-6-sol", + "openai/gpt-6-luna", + "openai/gpt-5.6-sol", + "openai/gpt-5.6-terra", + "openai/gpt-5.6-luna", + null + ] + }, + "modelLabel": { + "type": "string" + }, + "openRouterReferenceUsd": { + "type": ["number", "null"], + "minimum": 0 + }, + "pricingFetchedAt": { + "type": ["string", "null"], + "format": "date-time" + }, + "costStatus": { + "enum": ["priced", "partial", "unavailable"] + }, + "input": { + "type": ["number", "null"], + "minimum": 0 + }, + "cached": { + "type": ["number", "null"], + "minimum": 0 + }, + "output": { + "type": ["number", "null"], + "minimum": 0 + }, + "total": { + "type": ["number", "null"], + "minimum": 0 + }, + "requestCount": { + "type": "integer", + "minimum": 0 + }, + "pricedRequestCount": { + "type": "integer", + "minimum": 0 + }, + "unpricedRequestCount": { + "type": "integer", + "minimum": 0 + } + }, + "additionalProperties": false + } + }, + "pricing": { + "type": "object", + "required": ["status", "fetchedAt", "sourceUrl"], + "properties": { + "status": { + "enum": ["fresh", "stale", "unavailable"] + }, + "fetchedAt": { + "type": ["string", "null"], + "format": "date-time" + }, + "sourceUrl": { + "const": "https://openrouter.ai/api/v1/models" + } + }, + "additionalProperties": false + } + }, + "additionalProperties": true +} diff --git a/scripts/build-source-package.ps1 b/scripts/build-source-package.ps1 index 9bbedf6..fc46f47 100644 --- a/scripts/build-source-package.ps1 +++ b/scripts/build-source-package.ps1 @@ -22,7 +22,7 @@ if (-not $RepoRoot) { $resolvedRoot = (Resolve-Path -LiteralPath $RepoRoot).Path # 必需文件(缺任一即中止,避免打出残缺包) -foreach ($required in @("README.md", "README.en.md", "README.agent.md", "DEPLOYMENT.md", "LICENSE", "package.json", "token-stats.mjs", "runtime-paths.mjs", "monitor-watchdog.ps1", "speed-stats.mjs", "history-archive.mjs", "protocol.mjs", "tokens-ui-for-codex-panel.js", "readme01.png", "readme02.png", "readme03.png", "schemas\payload-v4.schema.json", "docs\auth-v2.md", "docs\watchdog.md", "docs\context-estimation.md", "docs\context-and-resources.md", "docs\history-ranges.md", "docs\metric-scopes.md", "skills\tokens-ui-for-codex\SKILL.md", "scripts\verify-watchdog.ps1", "rust-launcher\Cargo.toml", "rust-launcher\README.md", "rust-launcher\build-and-package.ps1", "rust-launcher\tests\supervisor_integration.rs")) { +foreach ($required in @("README.md", "README.en.md", "README.agent.md", "DEPLOYMENT.md", "LICENSE", "package.json", "token-stats.mjs", "runtime-paths.mjs", "monitor-watchdog.ps1", "speed-stats.mjs", "history-archive.mjs", "openrouter-pricing.mjs", "service-tier-pricing.mjs", "model-usage.mjs", "protocol.mjs", "tokens-ui-for-codex-panel.js", "readme01.png", "readme02.png", "readme03.png", "schemas\payload-v5.schema.json", "docs\auth-v2.md", "docs\watchdog.md", "docs\context-estimation.md", "docs\context-and-resources.md", "docs\history-ranges.md", "docs\metric-scopes.md", "skills\tokens-ui-for-codex\SKILL.md", "scripts\verify-watchdog.ps1", "rust-launcher\Cargo.toml", "rust-launcher\README.md", "rust-launcher\build-and-package.ps1", "rust-launcher\tests\supervisor_integration.rs")) { $requiredPath = Join-Path $resolvedRoot $required if (-not (Test-Path -LiteralPath $requiredPath)) { throw "缺少文件: $requiredPath" } } diff --git a/scripts/stats-snapshot.mjs b/scripts/stats-snapshot.mjs index 387bf74..caaf1dd 100644 --- a/scripts/stats-snapshot.mjs +++ b/scripts/stats-snapshot.mjs @@ -143,7 +143,21 @@ const fixtures = { }; function normalizedStats(stats) { - const stable = { ...stats }; + // Keep the original v4 numeric baseline unchanged. v5 model/cost additions + // have independent parser, history, protocol and UI assertions; only these + // named additions are omitted here, never an existing statistic or value. + const modelCostFields = new Set([ + "modelUsage", + "modelId", + "modelLabel", + "openRouterReferenceUsd", + "pricingFetchedAt", + "costStatus", + "cacheWrite", + "cacheWrite30m", + "cacheWrite12h", + ]); + const stable = JSON.parse(JSON.stringify(stats, (key, value) => (modelCostFields.has(key) ? undefined : value))); delete stable.file; delete stable.threadId; delete stable.date; diff --git a/scripts/verify-audit-findings.mjs b/scripts/verify-audit-findings.mjs index 31f97d4..be8cb85 100644 --- a/scripts/verify-audit-findings.mjs +++ b/scripts/verify-audit-findings.mjs @@ -147,7 +147,7 @@ const report = {}; // ---- F4:schema 三份实现 ---- { - const schema = JSON.parse(fs.readFileSync(new URL("../schemas/payload-v4.schema.json", import.meta.url), "utf8")); + const schema = JSON.parse(fs.readFileSync(new URL("../schemas/payload-v5.schema.json", import.meta.url), "utf8")); const required = schema.required || []; const alwaysProduced = [ "protocolName", diff --git a/scripts/verify-live.mjs b/scripts/verify-live.mjs index eede80d..16244c3 100644 --- a/scripts/verify-live.mjs +++ b/scripts/verify-live.mjs @@ -135,15 +135,15 @@ try { record("协议已注册", snapshot.protocolName === "tokens-ui-for-codex", snapshot.protocolName); record( - "支持 schema v4", - Array.isArray(snapshot.supportedSchemaVersions) && snapshot.supportedSchemaVersions.includes(4), + "支持 schema v5", + Array.isArray(snapshot.supportedSchemaVersions) && snapshot.supportedSchemaVersions.includes(5), snapshot.supportedSchemaVersions, ); record("统计条唯一", snapshot.panelCount === 1, snapshot.panelCount); record("样式表唯一", snapshot.styleCount === 1, snapshot.styleCount); record("统计条位于上下文圆圈左侧", snapshot.panelLeftOfRing, { panel: snapshot.panelRect, ring: snapshot.ringRect }); record("负载协议名匹配", snapshot.payloadProtocolName === "tokens-ui-for-codex", snapshot.payloadProtocolName); - record("负载 schema v4", snapshot.payloadSchemaVersion === 4, snapshot.payloadSchemaVersion); + record("负载 schema v5", snapshot.payloadSchemaVersion === 5, snapshot.payloadSchemaVersion); record( "timing-v1 能力已注册", Array.isArray(snapshot.payloadCapabilities) && snapshot.payloadCapabilities.includes("timing-v1"), diff --git a/scripts/verify-sync.mjs b/scripts/verify-sync.mjs index 8bf435c..4b1fc8a 100644 --- a/scripts/verify-sync.mjs +++ b/scripts/verify-sync.mjs @@ -126,9 +126,12 @@ const CHECKED_FILES = [ "monitor-watchdog.ps1", "speed-stats.mjs", "history-archive.mjs", + "openrouter-pricing.mjs", + "service-tier-pricing.mjs", + "model-usage.mjs", "tokens-ui-for-codex-panel.js", "protocol.mjs", - "schemas/payload-v4.schema.json", + "schemas/payload-v5.schema.json", "find-codex.ps1", "install.ps1", "install-autostart.ps1", diff --git a/scripts/verify-watchdog.ps1 b/scripts/verify-watchdog.ps1 index 89607f6..0ac72d4 100644 --- a/scripts/verify-watchdog.ps1 +++ b/scripts/verify-watchdog.ps1 @@ -109,7 +109,7 @@ function Wait-ForDifferentHeartbeat { try { New-Item -ItemType Directory -Path $pluginRoot, $codexHome, $stateDir, $logDir, $historyDir -Force | Out-Null - foreach ($name in @("token-stats.mjs", "runtime-paths.mjs", "speed-stats.mjs", "history-archive.mjs", "protocol.mjs", "monitor-watchdog.ps1")) { + foreach ($name in @("token-stats.mjs", "runtime-paths.mjs", "speed-stats.mjs", "history-archive.mjs", "openrouter-pricing.mjs", "service-tier-pricing.mjs", "model-usage.mjs", "protocol.mjs", "monitor-watchdog.ps1")) { Copy-Item -LiteralPath (Join-Path $RepoRoot $name) -Destination (Join-Path $pluginRoot $name) -Force } diff --git a/service-tier-pricing.mjs b/service-tier-pricing.mjs new file mode 100644 index 0000000..7bea13c --- /dev/null +++ b/service-tier-pricing.mjs @@ -0,0 +1,70 @@ +// Rates for modes not present in the public catalog are never inferred from base prices. +// A caller may supply an explicitly verified, versioned official rate profile. +export const SERVICE_TIER_PROFILE_VERSION = 1; +export const SERVICE_TIER_PROFILE_MAX_AGE_MS = 24 * 60 * 60 * 1000; + +function officialSource(value) { + try { + const url = new URL(value); + return ( + url.protocol === "https:" && ["developers.openai.com", "openai.com", "help.openai.com"].includes(url.hostname) + ); + } catch { + return false; + } +} + +function validRate(rate) { + return ( + rate && + ["prompt", "completion", "input_cache_read", "input_cache_write"].every( + (key) => typeof rate[key] === "number" && Number.isFinite(rate[key]) && rate[key] >= 0, + ) + ); +} + +export function resolveServiceTierRate({ + billingContext, + modelId, + serviceTier, + cacheWriteTtl, + inputTokens, + profile, + now = Date.now(), + maxAgeMs = SERVICE_TIER_PROFILE_MAX_AGE_MS, +} = {}) { + const empty = { rate: null, multiplier: null, verifiedAt: null, sourceUrl: null, status: "unsupported-tier" }; + if (!profile || profile.schemaVersion !== SERVICE_TIER_PROFILE_VERSION || !officialSource(profile.sourceUrl)) { + return empty; + } + const verifiedAt = Date.parse(profile.verifiedAt); + const clock = typeof now === "function" ? now() : now; + if (!Number.isFinite(verifiedAt) || !Number.isFinite(clock) || verifiedAt > clock || clock - verifiedAt > maxAgeMs) { + return { ...empty, status: "stale-price" }; + } + if (!Array.isArray(profile.rates)) return empty; + const matches = profile.rates.filter( + (row) => + row.modelId === modelId && + row.billingContext === billingContext && + row.serviceTier === serviceTier && + (row.cacheWriteTtl ?? null) === (cacheWriteTtl ?? null) && + ((row.minPromptTokens == null && row.maxPromptTokens == null) || + (Number.isSafeInteger(inputTokens) && + inputTokens >= 0 && + (row.minPromptTokens == null || + (Number.isSafeInteger(row.minPromptTokens) && inputTokens >= row.minPromptTokens)) && + (row.maxPromptTokens == null || + (Number.isSafeInteger(row.maxPromptTokens) && inputTokens <= row.maxPromptTokens)))), + ); + if (matches.length !== 1 || !validRate(matches[0].rate)) return empty; + return { + rate: Object.fromEntries( + ["prompt", "completion", "input_cache_read", "input_cache_write"].map((key) => [key, matches[0].rate[key]]), + ), + multiplier: null, + verifiedAt: new Date(verifiedAt).toISOString(), + sourceUrl: profile.sourceUrl, + status: "priced", + }; +} diff --git a/skills/tokens-ui-for-codex/SKILL.md b/skills/tokens-ui-for-codex/SKILL.md index 2831f37..a940ecf 100644 --- a/skills/tokens-ui-for-codex/SKILL.md +++ b/skills/tokens-ui-for-codex/SKILL.md @@ -33,7 +33,7 @@ description: 安装、更新、验证、备份、恢复和排查 Tokens UI For C - 将紧凑统计条放在上下文用量圆圈左侧 - 左键打开详情弹层 - 跟随 Codex 浅色/深色主题 -- 验证 HMAC schema v4 / auth v2 负载 +- 验证 HMAC schema v5 / auth v2 负载 - 增量更新 DOM ### 监控 @@ -42,7 +42,7 @@ description: 安装、更新、验证、备份、恢复和排查 Tokens UI For C - 读取本地 Codex rollout JSONL - 增量解析、分片合并、统计 -- 通过本机 CDP 推送签名 schema v4 / auth v2 负载 +- 通过本机 CDP 推送签名 schema v5 / auth v2 负载 - 会话候选需存在对应 rollout 并稳定 2.5 秒后才切换 - 新对话 placeholder 不允许映射到任意旧文件 - 写入健康快照、简略快照和日志 @@ -152,7 +152,7 @@ watchdog 保持常驻;进程消失或连续心跳停滞时才重启。原地 ## 协议安全 -页面只接受签名 schema v4: +页面只接受签名 schema v5: - `authRequired=true` - `auth.v=2` diff --git a/test/auth-ui.playwright.test.mjs b/test/auth-ui.playwright.test.mjs index 7305e6a..b6c9426 100644 --- a/test/auth-ui.playwright.test.mjs +++ b/test/auth-ui.playwright.test.mjs @@ -42,11 +42,14 @@ function launchOptions() { const { chromium } = loadPlaywright(); const panelSource = fs.readFileSync(new URL("../tokens-ui-for-codex-panel.js", import.meta.url), "utf8"); let browser; +let browserServer; let page; let server; +const artifactDir = process.env.TUI_UI_ARTIFACT_DIR || os.tmpdir(); +fs.mkdirSync(artifactDir, { recursive: true }); function basePayload() { - return { + const payload = { protocolName: PROTOCOL_NAME, schemaVersion: PROTOCOL_SCHEMA_VERSION, authRequired: true, @@ -153,6 +156,46 @@ function basePayload() { }, updatedAt: new Date().toISOString(), }; + payload.pricing = { + status: "fresh", + fetchedAt: new Date().toISOString(), + sourceUrl: "https://openrouter.ai/api/v1/models", + }; + payload.modelUsage = [modelUsageFixture()]; + for (const turn of payload.turns) { + turn.modelUsage = [modelUsageFixture({ ...turn, requestCount: turn.requests })]; + } + for (const request of payload.requestDetails) { + Object.assign(request, { + modelId: "openai/gpt-6.1-sol", + modelLabel: "GPT-6.1 Sol", + openRouterReferenceUsd: 0.001, + costStatus: "priced", + pricingFetchedAt: null, + }); + } + return payload; +} + +function modelUsageFixture(overrides = {}) { + const requestCount = overrides.requestCount ?? 3; + return { + modelId: "openai/gpt-6.1-sol", + modelLabel: "GPT-6.1 Sol", + input: 240, + cached: 120, + output: 60, + total: 300, + requestCount, + openRouterReferenceUsd: 0.003, + pricedRequestCount: requestCount, + unpricedRequestCount: 0, + costStatus: "priced", + pricingFetchedAt: null, + ...Object.fromEntries( + Object.entries(overrides).filter(([key]) => !["index", "startLabel", "requests", "uncached"].includes(key)), + ), + }; } function rangeSummaryFixture(range, total) { @@ -181,6 +224,7 @@ function rangeSummaryFixture(range, total) { effectiveTps: 12.5, timingSampleCount: 4, ttftSampleCount: 4, + modelUsage: [modelUsageFixture({ input: total - 100, cached: total - 200, output: 100, total, requestCount: 6 })], }; } @@ -197,7 +241,8 @@ function sign(body, signer, { ts = Date.now(), seq = 1, nonce = crypto.randomByt } before(async () => { - browser = await chromium.launch(launchOptions()); + browserServer = await chromium.launchServer(launchOptions()); + browser = await chromium.connect(browserServer.wsEndpoint()); page = await browser.newPage({ viewport: { width: 1200, height: 800 } }); const html = ` @@ -235,7 +280,11 @@ before(async () => { after(async () => { if (browser) await browser.close(); - if (server) await new Promise((resolve) => server.close(resolve)); + if (browserServer) await browserServer.kill(); + if (server) { + server.closeAllConnections(); + await new Promise((resolve) => server.close(resolve)); + } }); test("本地隔离页:auth v2 接受有效负载并拒绝正文/seq/重放/过期篡改", async () => { @@ -512,3 +561,225 @@ test("本地隔离页:健康灯悬浮提示包含错误码、原因、恢复 assert.match(String(unknownTitle), /检查本地 watch 日志/); await page.evaluate(() => document.querySelector("#tui-detail-dialog .tui-dialog-close")?.click()); }); + +test("本地隔离页:模型列表与单模型详情覆盖会话、轮次、请求及范围,未知费用不作零", async () => { + const signer = makeSigner(0x66); + const body = basePayload(); + body.pricing.status = "stale"; + body.modelUsage = [ + modelUsageFixture({ input: 140, cached: 60, output: 40, total: 180, requestCount: 2 }), + modelUsageFixture({ + modelId: "openai/gpt-6-luna", + modelLabel: "GPT-6 Luna", + input: 100, + cached: 60, + output: 20, + total: 120, + requestCount: 1, + openRouterReferenceUsd: null, + pricedRequestCount: 0, + unpricedRequestCount: 1, + costStatus: "unavailable", + }), + ]; + body.rangeSummary = rangeSummaryFixture("all", 123456); + const payload = sign(body, signer, { seq: 1 }); + await page.evaluate( + async ({ payload, keyHex, keyId }) => { + window.__tuiForCodexAuthBootstrap = { key: keyHex, keyId }; + window.__tuiForCodex = payload; + window.dispatchEvent(new Event("tokens-ui-for-codex")); + await new Promise((resolve) => setTimeout(resolve, 180)); + if (!document.getElementById("tui-detail-dialog")) document.getElementById("tui-native")?.click(); + }, + { payload, keyHex: signer.key.toString("hex"), keyId: signer.keyId }, + ); + await page.waitForSelector(".tui-model-list button", { timeout: 2500 }); + assert.equal(await page.locator(".tui-model-list button").count(), 2); + assert.match(await page.locator(".tui-model-pricing").textContent(), /陈旧缓存.*价目/); + assert.match(await page.locator(".tui-model-cost-summary").textContent(), /部分.*2\/3/); + await page.getByRole("button", { name: /GPT-6 Luna.*详情/ }).click(); + assert.match(await page.locator(".tui-model-detail").textContent(), /GPT-6 Luna/); + assert.match(await page.locator(".tui-model-detail").textContent(), /参考费用\s*--/); + assert.match(await page.locator(".tui-model-detail").textContent(), /缓存命中\s*60/); + await page.locator(".tui-model-scope").selectOption("turn"); + assert.equal(await page.locator(".tui-model-list button").count(), 1); + await page.getByRole("button", { name: /GPT-6.1 Sol.*详情/ }).click(); + assert.match(await page.locator(".tui-model-detail").textContent(), /请求.*#3.*#2/); + await page.locator(".tui-model-scope").selectOption("range"); + assert.match(await page.locator(".tui-model-detail").textContent(), /123,456/); + await page.locator(".tui-model-scope").selectOption("session"); + await page.getByRole("button", { name: /GPT-6 Luna.*详情/ }).click(); + await page.setViewportSize({ width: 1200, height: 1200 }); + await page.locator(".tui-models").scrollIntoViewIfNeeded(); + await page.locator(".tui-models").screenshot({ path: path.join(artifactDir, "tokens-ui-gpt-model-cost-light.png") }); + await page.evaluate(() => { + document.documentElement.setAttribute("data-theme", "dark"); + document.body.style.background = "#171717"; + document.body.style.color = "#eeeeee"; + }); + await page.waitForFunction( + () => document.getElementById("tui-detail-dialog")?.getAttribute("data-tui-theme") === "dark", + ); + await page.locator(".tui-models").screenshot({ path: path.join(artifactDir, "tokens-ui-gpt-model-cost-dark.png") }); + await page.setViewportSize({ width: 1200, height: 800 }); + await page.evaluate(() => { + document.querySelector(".tui-model-row").__identityMarker = "stable"; + }); + body.modelUsage[0].openRouterReferenceUsd = 0.004; + body.updatedAt = new Date().toISOString(); + const updatedPayload = sign(body, signer, { seq: 2 }); + await page.evaluate(async (payload) => { + window.__tuiForCodex = payload; + window.dispatchEvent(new Event("tokens-ui-for-codex")); + await new Promise((resolve) => setTimeout(resolve, 180)); + }, updatedPayload); + assert.equal( + await page.evaluate(() => document.querySelector(".tui-model-row").__identityMarker), + "stable", + "模型结构未变应复用原行", + ); + assert.match(await page.locator(".tui-model-list").textContent(), /\$0\.004/); + await page.evaluate(() => document.querySelector("#tui-detail-dialog .tui-dialog-close")?.click()); +}); + +test("本地隔离页:未知模型与无价目保持未知,会话切换清除旧模型详情", async () => { + const signer = makeSigner(0x68); + const body = basePayload(); + const unknown = { + modelId: null, + modelLabel: "未知模型", + input: null, + cached: null, + output: null, + total: null, + openRouterReferenceUsd: null, + pricedRequestCount: 0, + costStatus: "unavailable", + }; + body.pricing = { ...body.pricing, status: "unavailable", fetchedAt: null }; + body.sessionChanged = true; + body.modelUsage = [modelUsageFixture({ ...unknown, requestCount: 3, unpricedRequestCount: 3 })]; + body.turns.forEach((turn) => { + turn.modelUsage = [ + modelUsageFixture({ ...unknown, requestCount: turn.requests, unpricedRequestCount: turn.requests }), + ]; + }); + body.requestDetails.forEach((request) => { + Object.assign(request, { + modelId: null, + modelLabel: "未知模型", + openRouterReferenceUsd: null, + costStatus: "unknown-model", + }); + }); + body.rangeSummary = rangeSummaryFixture("all", 123456); + body.rangeSummary.modelUsage = [modelUsageFixture({ ...unknown, requestCount: 6, unpricedRequestCount: 6 })]; + const payload = sign(body, signer, { seq: 1 }); + await page.evaluate( + async ({ payload, keyHex, keyId }) => { + document.documentElement.setAttribute("data-theme", "light"); + document.body.style.background = "#ffffff"; + document.body.style.color = "#111111"; + window.__tuiForCodexAuthBootstrap = { key: keyHex, keyId }; + window.__tuiForCodex = payload; + window.dispatchEvent(new Event("tokens-ui-for-codex")); + await new Promise((resolve) => setTimeout(resolve, 220)); + if (!document.getElementById("tui-detail-dialog")) document.getElementById("tui-native")?.click(); + }, + { payload, keyHex: signer.key.toString("hex"), keyId: signer.keyId }, + ); + await page.waitForSelector(".tui-model-list button"); + assert.equal(await page.locator(".tui-model-list button").count(), 1); + assert.match(await page.locator(".tui-model-detail").textContent(), /未知模型.*输入\s*--/s); + assert.match(await page.locator(".tui-model-cost-summary").textContent(), /已计价 0\/3/); + assert.doesNotMatch(await page.locator(".tui-models").textContent(), /GPT-6\.1 Sol|\$0\.00/); + await page.setViewportSize({ width: 1200, height: 1200 }); + await page.locator(".tui-models").scrollIntoViewIfNeeded(); + await page + .locator(".tui-models") + .screenshot({ path: path.join(artifactDir, "tokens-ui-gpt-model-cost-unknown.png") }); + await page.setViewportSize({ width: 1200, height: 800 }); + await page.evaluate(() => document.querySelector("#tui-detail-dialog .tui-dialog-close")?.click()); +}); + +test("本地隔离页:有效签名也拒绝非法模型费用字段与旧schema", async () => { + const signer = makeSigner(0x77); + for (const [index, mutate] of [ + (body) => { + body.modelUsage[0].openRouterReferenceUsd = -1; + }, + (body) => { + body.modelUsage[0].modelId = "openai/other"; + }, + (body) => { + body.modelUsage.push({ ...body.modelUsage[0] }); + }, + (body) => { + body.modelUsage[0].pricedRequestCount = 4; + }, + (body) => { + body.modelUsage[0].requestCount = 4; + body.modelUsage[0].unpricedRequestCount = 1; + body.modelUsage[0].costStatus = "partial"; + }, + (body) => { + delete body.requestDetails[0].pricingFetchedAt; + }, + (body) => { + body.schemaVersion = 4; + }, + ].entries()) { + const body = basePayload(); + mutate(body); + const payload = sign(body, signer, { seq: index + 1 }); + const result = await page.evaluate( + async ({ payload, keyHex, keyId }) => { + window.__tuiForCodexAuthBootstrap = { key: keyHex, keyId }; + window.__tuiForCodex = payload; + window.dispatchEvent(new Event("tokens-ui-for-codex")); + await new Promise((resolve) => setTimeout(resolve, 180)); + return { + aria: document.getElementById("tui-native")?.getAttribute("aria-label"), + security: Number(document.documentElement.getAttribute("data-tui-security") || "0"), + }; + }, + { payload, keyHex: signer.key.toString("hex"), keyId: signer.keyId }, + ); + if (body.schemaVersion === 4) assert.ok(result.security > 0, "旧schema应在认证层拒绝"); + else assert.match(String(result.aria), /协议不匹配/, `非法用例 ${index} 未拒绝`); + } +}); + +test("本地隔离页:极小正数费用不显示为免费,真实零与未知保持区别", async () => { + const signer = makeSigner(0x88); + const cases = [ + { value: 0.0000000001, expected: "<$0.000001" }, + { value: 0, expected: "$0.00" }, + { value: null, expected: "--" }, + ]; + for (const [index, item] of cases.entries()) { + const body = basePayload(); + Object.assign(body.modelUsage[0], { + openRouterReferenceUsd: item.value, + pricedRequestCount: item.value === null ? 0 : 3, + unpricedRequestCount: item.value === null ? 3 : 0, + costStatus: item.value === null ? "unavailable" : "priced", + }); + const payload = sign(body, signer, { seq: index + 1 }); + assert.equal(validatePayload(payload, { allowLegacy: false, requireAuth: true }).ok, true); + await page.evaluate( + async ({ payload, keyHex, keyId }) => { + window.__tuiForCodexAuthBootstrap = { key: keyHex, keyId }; + window.__tuiForCodex = payload; + window.dispatchEvent(new Event("tokens-ui-for-codex")); + await new Promise((resolve) => setTimeout(resolve, 180)); + if (!document.getElementById("tui-detail-dialog")) document.getElementById("tui-native")?.click(); + }, + { payload, keyHex: signer.key.toString("hex"), keyId: signer.keyId }, + ); + await page.waitForSelector(".tui-model-detail .tui-dialog-card-value"); + assert.equal(await page.locator(".tui-model-detail .tui-dialog-card-value").last().textContent(), item.expected); + } + await page.evaluate(() => document.querySelector("#tui-detail-dialog .tui-dialog-close")?.click()); +}); diff --git a/test/history-archive.test.mjs b/test/history-archive.test.mjs index 6c952af..776357c 100644 --- a/test/history-archive.test.mjs +++ b/test/history-archive.test.mjs @@ -20,6 +20,131 @@ function record(index, turnId, baseMs) { }; } +test("模型历史费用冻结且范围只合并结构化模型统计", () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), "tui-model-history-")); + try { + const now = Date.parse("2026-11-03T10:00:00Z"); + const turns = [ + { + turnId: "T1", + start: new Date(now - 1000).toISOString(), + requestCount: 1, + input: 1000, + cached: 800, + output: 100, + total: 1100, + }, + ]; + const request = { + ...record(0, "T1", now - 1000), + model: "gpt-6.1-sol", + modelId: "openai/gpt-6.1-sol", + openRouterReferenceUsd: 0.002, + costStatus: "priced", + pricingFetchedAt: new Date(now).toISOString(), + }; + const archived = new UsageHistoryArchive(root).archiveSession({ + threadId: "fixture", + now, + turns, + records: [request], + }); + assert.equal(archived.appended, 1); + const nextTurns = structuredClone(turns); + const repriced = { ...request, openRouterReferenceUsd: 99 }; + const archive = new UsageHistoryArchive(root); + assert.equal( + archive.archiveSession({ threadId: "fixture", now: now + 1000, turns: nextTurns, records: [repriced] }).appended, + 0, + ); + assert.equal(nextTurns[0].modelUsage[0].openRouterReferenceUsd, 0.002); + for (const range of ["today", "3d", "7d", "30d", "all"]) { + const summary = archive.aggregateRange(range, now + 1000); + assert.equal(summary.modelUsage[0].openRouterReferenceUsd, 0.002); + assert.equal(summary.modelUsage[0].requestCount, 1); + } + const snapshot = fs.readFileSync(path.join(root, "sessions", archived.sessionHash, "turns.json"), "utf8"); + assert.equal(snapshot.includes('"turnId"'), false); + assert.equal(snapshot.includes('"model":"'), false); + } finally { + fs.rmSync(root, { recursive: true, force: true }); + } +}); + +test("旧历史模型与费用保持未知不以当前价重估", () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), "tui-old-model-history-")); + try { + const now = Date.parse("2026-11-03T10:00:00Z"); + const archive = new UsageHistoryArchive(root); + const result = archive.archiveSession({ + threadId: "fixture", + now, + turns: [ + { start: new Date(now).toISOString(), requestCount: 2, input: 100, cached: null, output: 10, total: 110 }, + ], + records: [], + }); + const file = path.join(root, "sessions", result.sessionHash, "turns.json"); + const snapshot = JSON.parse(fs.readFileSync(file, "utf8")); + delete snapshot.turns[0].modelUsage; + fs.writeFileSync(file, JSON.stringify(snapshot), "utf8"); + const summary = new UsageHistoryArchive(root).aggregateRange("all", now); + assert.equal(summary.modelUsage[0].modelId, null); + assert.equal(summary.modelUsage[0].openRouterReferenceUsd, null); + assert.equal(summary.modelUsage[0].unpricedRequestCount, 2); + } finally { + fs.rmSync(root, { recursive: true, force: true }); + } +}); + +test("历史归档:旧格式请求键不同,重新读取也不重复追加或重写原文件", () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), "tui-history-v2-")); + try { + const now = Date.parse("2026-11-03T10:00:00Z"); + const ts = new Date(now - 1000).toISOString(); + const turns = [{ start: ts, requestCount: 1, input: 100, cached: 40, output: 20, total: 120 }]; + const saved = new UsageHistoryArchive(root).archiveSession({ threadId: "fixture", now, turns, records: [] }); + const file = path.join(root, "sessions", saved.sessionHash, "requests-202611.jsonl"); + const original = + JSON.stringify({ + v: 2, + kind: "request", + key: "a".repeat(24), + sessionHash: saved.sessionHash, + turnIndex: 1, + requestIndex: 1, + ts, + input: 100, + cached: 40, + output: 20, + total: 120, + threadTotal: 120, + turnTotal: 120, + }) + "\n"; + fs.writeFileSync(file, original, "utf8"); + const result = new UsageHistoryArchive(root).archiveSession({ + threadId: "fixture", + now, + turns, + records: [ + { + ts, + input: 100, + cached: 40, + output: 20, + requestTotal: 120, + threadUsage: { total: 120 }, + turnUsage: { total: 120 }, + }, + ], + }); + assert.equal(result.appended, 0); + assert.equal(fs.readFileSync(file, "utf8"), original); + } finally { + fs.rmSync(root, { recursive: true, force: true }); + } +}); + test("历史归档:超过 500 条仍全部写入本地,重复刷新不重复追加", () => { const root = fs.mkdtempSync(path.join(os.tmpdir(), "tui-history-")); try { diff --git a/test/live-ui.playwright.test.mjs b/test/live-ui.playwright.test.mjs index f5a3354..5ce2922 100644 --- a/test/live-ui.playwright.test.mjs +++ b/test/live-ui.playwright.test.mjs @@ -69,6 +69,8 @@ const PAYLOAD_WHITELIST = new Set([ "sessionTtftAvgMs", "sessionTimingSampleCount", "rangeSummary", + "modelUsage", + "pricing", "turnTotal", "turnInput", "turnWallMs", @@ -213,7 +215,7 @@ test("实机:负载协议与隐私白名单", async (t) => { transientKeys, }), ); - assert.equal(payload.schemaVersion, 4, "schema 版本应为 4"); + assert.equal(payload.schemaVersion, 5, "schema 版本应为 5"); assert.equal(payload.authRequired, true, "v4 负载必须声明 authRequired=true"); assert.equal(payload.auth && payload.auth.v, 2, "认证版本应为 2"); assert.ok(payload.auth && /^[0-9a-f]{32}$/.test(payload.auth.keyId), "keyId 格式无效"); @@ -507,7 +509,7 @@ test("实机:Esc 不再关闭弹层(插件不注册键盘监听)", async ( assert.equal(stillOpen, true, "按当前设计 Esc 不应关闭弹层(键盘监听已按用户要求移除)"); }); -test("实机:无 auth 的伪造 schema v4 负载会被拒绝", async (t) => { +test("实机:无 auth 的伪造 schema v5 负载会被拒绝", async (t) => { const result = await state.page.evaluate(async () => { const backup = window.__tuiForCodex; const forged = JSON.parse(JSON.stringify(backup)); diff --git a/test/openrouter-pricing.test.mjs b/test/openrouter-pricing.test.mjs new file mode 100644 index 0000000..0afb680 --- /dev/null +++ b/test/openrouter-pricing.test.mjs @@ -0,0 +1,249 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import fs from "node:fs/promises"; +import os from "node:os"; +import path from "node:path"; +import { + getSupportedModels, + resolveOpenRouterModel, + parseOpenRouterCatalog, + loadOpenRouterCatalog, + estimateRequestCost, +} from "../openrouter-pricing.mjs"; + +const now = Date.parse("2026-11-03T08:00:00Z"); +const pricing = { + prompt: "0.000002", + completion: "0.00001", + input_cache_read: "0.0000001", + input_cache_write: "0.0000025", + overrides: [ + { + min_prompt_tokens: 272000, + prompt: "0.000004", + completion: "0.000015", + input_cache_read: "0.0000002", + input_cache_write: "0.000005", + }, + ], +}; +const payload = () => ({ data: getSupportedModels().map(({ modelId }) => ({ id: modelId, pricing })) }); +const catalog = () => parseOpenRouterCatalog(payload(), now); +const request = (extra = {}) => ({ + model: "gpt-6.1-sol", + input: 1000, + cached: 400, + output: 100, + cacheWrite: 0, + ...extra, +}); +const model = () => catalog().modelsById["openai/gpt-6.1-sol"]; + +test("only the eight exact GPT aliases resolve; suffixes and unrelated models remain unknown", () => { + assert.equal(getSupportedModels().length, 8); + for (const { modelId } of getSupportedModels()) { + assert.equal(resolveOpenRouterModel(modelId), modelId); + assert.equal(resolveOpenRouterModel(modelId.slice(7)), modelId); + } + for (const value of ["gpt-6.1-sol-pro", "gpt-6.1-sol:batch", "prefix-gpt-6-sol", "gpt-4o", "", null]) { + assert.equal(resolveOpenRouterModel(value), null); + } +}); + +test("catalog keeps a sanitized supported-only price snapshot and context tiers", () => { + const data = payload(); + data.data.push({ id: "other/model", pricing }); + data.data[0].description = "must not persist"; + const parsed = parseOpenRouterCatalog(data, now); + assert.equal(Object.keys(parsed.modelsById).length, 8); + assert.equal(model().pricing.prompt, 0.000002); + assert.equal(model().pricing.overrides[0].min_prompt_tokens, 272000); + assert.equal(JSON.stringify(parsed).includes("must not persist"), false); + assert.equal(parsed.fetchedAt, new Date(now).toISOString()); +}); + +test("invalid and missing prices remain null, never zero; duplicate model rows are ambiguous", () => { + const data = payload(); + data.data[0].pricing = { prompt: "", completion: "-1", input_cache_read: "NaN", input_cache_write: false }; + const p = parseOpenRouterCatalog(data, now).modelsById[data.data[0].id].pricing; + assert.equal(p.prompt, null); + assert.equal(p.completion, null); + assert.equal(p.input_cache_read, null); + assert.equal(p.input_cache_write, null); + data.data.push(data.data[0]); + assert.equal(parseOpenRouterCatalog(data, now).modelsById[data.data[0].id], undefined); + assert.throws(() => parseOpenRouterCatalog({ data: {} }, now)); +}); + +test("reference calculation charges input cache classes once and never adds reasoning twice", () => { + const result = estimateRequestCost(request({ cacheWrite: 200, reasoning: 999 }), model()); + assert.ok(Math.abs(result.openRouterReferenceUsd - (400 * 2e-6 + 400 * 1e-7 + 200 * 2.5e-6 + 100 * 1e-5)) < 1e-12); + assert.equal(result.status, "priced"); + assert.equal(result.apiCostUsd, null); +}); + +test("fixed-shape parser null placeholders do not conflict with one observed cache-write representation", () => { + const a = estimateRequestCost(request({ cacheWrite: 0, cacheWrite30m: null, cacheWrite12h: null }), model()); + assert.equal(a.status, "priced"); + const b = estimateRequestCost(request({ cacheWrite: null, cacheWrite30m: 100, cacheWrite12h: 0 }), model()); + assert.equal(b.status, "priced"); + const c = estimateRequestCost(request({ cacheWrite: null, cacheWrite30m: null, cacheWrite12h: null }), model()); + assert.equal(c.status, "missing-usage"); + const d = estimateRequestCost(request({ cacheWrite: 0, cacheWrite30m: 0, cacheWrite12h: 0 }), model()); + assert.equal(d.status, "missing-usage"); +}); + +test("missing and invalid usage, unknown model/price and unsupported tier do not fabricate cost", () => { + for (const extra of [ + { input: null }, + { cached: null }, + { output: null }, + { cached: 1001 }, + { input: -1 }, + { output: Infinity }, + { cacheWrite: null }, + ]) { + assert.equal(estimateRequestCost(request(extra), model()).openRouterReferenceUsd, null); + } + const noWrite = request(); + delete noWrite.cacheWrite; + assert.equal(estimateRequestCost(noWrite, model()).status, "missing-usage"); + assert.equal(estimateRequestCost(noWrite, model(), { cacheWritePolicy: "excluded" }).status, "priced"); + assert.equal(estimateRequestCost(request({ model: "gpt-4o" }), model()).status, "unknown-model"); + assert.equal(estimateRequestCost(request(), null).openRouterReferenceUsd, null); + assert.equal(estimateRequestCost(request(), model(), { serviceTier: "fast" }).status, "unsupported-tier"); + assert.equal( + estimateRequestCost(request({ cacheWrite: 200, cacheWrite12h: 200 }), model()).openRouterReferenceUsd, + null, + ); + assert.equal( + estimateRequestCost(request({ cacheWrite: 200 }), model(), { cacheWriteTtl: "12h" }).status, + "unsupported-tier", + ); +}); + +test("min_prompt_tokens context overrides apply at the exact API boundary, not to subscription reference", () => { + const low = estimateRequestCost(request({ input: 271999 }), model()); + const edge = estimateRequestCost(request({ input: 272000 }), model()); + assert.ok(edge.openRouterReferenceUsd > low.openRouterReferenceUsd * 1.9); + const subscription = estimateRequestCost(request({ input: 272000 }), model(), { + billingContext: "codex-subscription-allowance", + serviceTier: "standard", + }); + assert.ok(subscription.openRouterReferenceUsd < edge.openRouterReferenceUsd); + assert.equal(subscription.subscriptionAllowanceWeight, null); +}); + +test("missing cache price only prevents requests which actually use that category", () => { + const m = model(); + m.pricing.input_cache_read = null; + assert.equal(estimateRequestCost(request(), m).status, "missing-price"); + assert.equal(estimateRequestCost(request({ cached: 0 }), m).status, "priced"); +}); + +test("explicit 12h API rate profiles price only that scenario, never pretend the catalog supplies 12h", () => { + const profile = { + schemaVersion: 1, + verifiedAt: new Date(now).toISOString(), + sourceUrl: "https://developers.openai.com/api/docs/pricing", + rates: [ + { + modelId: "openai/gpt-6.1-sol", + billingContext: "openai-api", + serviceTier: "fast", + cacheWriteTtl: "12h", + rate: { prompt: 4e-6, completion: 20e-6, input_cache_read: 0.2e-6, input_cache_write: 10e-6 }, + }, + ], + }; + const result = estimateRequestCost(request({ cacheWrite: 100 }), model(), { + billingContext: "openai-api", + serviceTier: "fast", + cacheWriteTtl: "12h", + profile, + now, + }); + assert.equal(result.openRouterReferenceUsd, null); + assert.equal(result.status, "priced"); + assert.ok(Math.abs(result.apiCostUsd - (500 * 4e-6 + 400 * 0.2e-6 + 100 * 10e-6 + 100 * 20e-6)) < 1e-12); + assert.equal(estimateRequestCost(request(), model(), { billingContext: "invented" }).status, "unknown-mode"); +}); + +test("cache uses validated 24h snapshot without network; forced refresh is anonymous and sanitized", async (t) => { + const dir = await fs.mkdtemp(path.join(os.tmpdir(), "tui-price-")); + t.after(() => fs.rm(dir, { recursive: true, force: true })); + const cacheFile = path.join(dir, "pricing", "openrouter-catalog.json"); + let calls = 0; + const fetchImpl = async (url, options) => { + calls++; + assert.equal(url, "https://openrouter.ai/api/v1/models"); + assert.equal(options.method, "GET"); + assert.equal(options.credentials, "omit"); + assert.equal(options.redirect, "error"); + assert.equal(options.headers.Authorization, undefined); + return { ok: true, json: async () => payload() }; + }; + assert.equal((await loadOpenRouterCatalog({ cacheFile, fetchImpl, now })).status, "fresh"); + assert.equal((await loadOpenRouterCatalog({ cacheFile, fetchImpl, now: now + 1000 })).status, "fresh"); + assert.equal(calls, 1); + assert.equal(JSON.parse(await fs.readFile(cacheFile, "utf8")).schemaVersion, 1); +}); + +for (const code of [401, 403, 429, 500, "timeout", "json", "shape"]) { + test(`fetch ${code} preserves a stale validated cache and first failure is unavailable`, async (t) => { + const dir = await fs.mkdtemp(path.join(os.tmpdir(), "tui-price-")); + t.after(() => fs.rm(dir, { recursive: true, force: true })); + const cacheFile = path.join(dir, "catalog.json"); + const fetchImpl = async () => { + if (code === "timeout") throw Object.assign(new Error("private url must not leak"), { name: "TimeoutError" }); + if (code === "json") + return { + ok: true, + json: async () => { + throw new Error("invalid JSON"); + }, + }; + if (code === "shape") return { ok: true, json: async () => ({}) }; + return { ok: false, status: code }; + }; + const first = await loadOpenRouterCatalog({ cacheFile, fetchImpl, now }); + assert.equal(first.status, "unavailable"); + assert.equal(first.catalog, null); + assert.equal(JSON.stringify(first).includes("private url"), false); + await fs.writeFile(cacheFile, JSON.stringify(catalog())); + const previous = await fs.readFile(cacheFile, "utf8"); + const result = await loadOpenRouterCatalog({ cacheFile, fetchImpl, now: now + 25 * 3600_000 }); + assert.equal(result.status, "stale"); + assert.equal(await fs.readFile(cacheFile, "utf8"), previous); + assert.equal( + estimateRequestCost(request(), result.catalog.modelsById["openai/gpt-6.1-sol"], { pricingStatus: result.status }) + .status, + "stale-price", + ); + }); +} + +test("corrupt, future and foreign-source caches cannot be used as trusted snapshots", async (t) => { + const dir = await fs.mkdtemp(path.join(os.tmpdir(), "tui-price-")); + t.after(() => fs.rm(dir, { recursive: true, force: true })); + const cacheFile = path.join(dir, "catalog.json"); + for (const value of [ + "not json", + JSON.stringify({ ...catalog(), sourceUrl: "https://evil.invalid" }), + JSON.stringify({ ...catalog(), fetchedAt: new Date(now + 3600_000).toISOString() }), + ]) { + await fs.writeFile(cacheFile, value); + assert.equal( + ( + await loadOpenRouterCatalog({ + cacheFile, + now, + fetchImpl: async () => { + throw new Error("offline"); + }, + }) + ).status, + "unavailable", + ); + } +}); diff --git a/test/pricing-packaging.test.mjs b/test/pricing-packaging.test.mjs new file mode 100644 index 0000000..9d470a0 --- /dev/null +++ b/test/pricing-packaging.test.mjs @@ -0,0 +1,35 @@ +import assert from "node:assert/strict"; +import fs from "node:fs"; +import test from "node:test"; + +const requiredRuntimeFiles = ["openrouter-pricing.mjs", "service-tier-pricing.mjs", "model-usage.mjs"]; + +test("价格模块随安装、源码包、同步检查和隔离 watchdog fixture 一起交付", () => { + for (const file of [ + "install.ps1", + "scripts/build-source-package.ps1", + "scripts/verify-sync.mjs", + "scripts/verify-watchdog.ps1", + ]) { + const source = fs.readFileSync(new URL("../" + file, import.meta.url), "utf8"); + for (const runtimeFile of requiredRuntimeFiles) { + assert.ok(source.includes('"' + runtimeFile + '"'), file + " must include " + runtimeFile); + } + } +}); + +test("安装与审计工具使用当前 v5 schema,PowerShell 文件保留 UTF-8 BOM", () => { + for (const file of [ + "install.ps1", + "scripts/build-source-package.ps1", + "scripts/verify-sync.mjs", + "scripts/verify-audit-findings.mjs", + ]) { + const source = fs.readFileSync(new URL("../" + file, import.meta.url), "utf8"); + assert.ok(source.includes("payload-v5.schema.json"), file + " must check current schema"); + } + for (const file of ["install.ps1", "scripts/build-source-package.ps1", "scripts/verify-watchdog.ps1"]) { + const bytes = fs.readFileSync(new URL("../" + file, import.meta.url)); + assert.deepEqual([...bytes.subarray(0, 3)], [0xef, 0xbb, 0xbf], file + " must preserve BOM"); + } +}); diff --git a/test/service-tier-pricing.test.mjs b/test/service-tier-pricing.test.mjs new file mode 100644 index 0000000..d4e7196 --- /dev/null +++ b/test/service-tier-pricing.test.mjs @@ -0,0 +1,91 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { resolveServiceTierRate } from "../service-tier-pricing.mjs"; + +const now = Date.parse("2026-11-03T08:00:00Z"); +const profile = { + schemaVersion: 1, + verifiedAt: new Date(now).toISOString(), + sourceUrl: "https://developers.openai.com/api/docs/pricing", + rates: [ + { + modelId: "openai/gpt-6.1-sol", + billingContext: "openai-api", + serviceTier: "fast", + cacheWriteTtl: "12h", + rate: { prompt: 4e-6, completion: 20e-6, input_cache_read: 0.2e-6, input_cache_write: 10e-6 }, + }, + ], +}; + +test("exact versioned official profiles resolve without generic multipliers", () => { + const resolved = resolveServiceTierRate({ + billingContext: "openai-api", + modelId: "openai/gpt-6.1-sol", + serviceTier: "fast", + cacheWriteTtl: "12h", + profile, + now, + }); + assert.equal(resolved.status, "priced"); + assert.equal(resolved.rate.prompt, 4e-6); + assert.equal(resolved.multiplier, null); + assert.equal(resolved.sourceUrl, profile.sourceUrl); +}); + +test("unknown model, tier, TTL, funding basis and unverified production profile stay unknown", () => { + const base = { + billingContext: "openai-api", + modelId: "openai/gpt-6.1-sol", + serviceTier: "fast", + cacheWriteTtl: "12h", + profile, + now, + }; + for (const extra of [ + { modelId: "openai/gpt-6-sol" }, + { serviceTier: "batch" }, + { cacheWriteTtl: "30m" }, + { billingContext: "codex-subscription-allowance" }, + { profile: undefined }, + ]) { + assert.equal(resolveServiceTierRate({ ...base, ...extra }).rate, null); + } +}); + +test("expired, future, nonofficial and invalid profiles are rejected", () => { + for (const extra of [ + { verifiedAt: new Date(now - 25 * 3600_000).toISOString() }, + { verifiedAt: new Date(now + 3600_000).toISOString() }, + { sourceUrl: "https://evil.invalid/prices" }, + { rates: [{ ...profile.rates[0], rate: { prompt: -1 } }] }, + ]) { + const resolved = resolveServiceTierRate({ + billingContext: "openai-api", + modelId: "openai/gpt-6.1-sol", + serviceTier: "fast", + cacheWriteTtl: "12h", + profile: { ...profile, ...extra }, + now, + }); + assert.equal(resolved.rate, null); + assert.notEqual(resolved.status, "priced"); + } +}); + +test("API context tiers require an exact profile interval and select its declared min bound", () => { + const short = { ...profile.rates[0], minPromptTokens: 0, maxPromptTokens: 271999 }; + const long = { ...profile.rates[0], minPromptTokens: 272000, rate: { ...profile.rates[0].rate, prompt: 8e-6 } }; + const p = { ...profile, rates: [short, long] }; + const base = { + billingContext: "openai-api", + modelId: "openai/gpt-6.1-sol", + serviceTier: "fast", + cacheWriteTtl: "12h", + profile: p, + now, + }; + assert.equal(resolveServiceTierRate({ ...base, inputTokens: 271999 }).rate.prompt, 4e-6); + assert.equal(resolveServiceTierRate({ ...base, inputTokens: 272000 }).rate.prompt, 8e-6); + assert.equal(resolveServiceTierRate(base).rate, null); +}); diff --git a/test/token-stats.test.mjs b/test/token-stats.test.mjs index 6272c85..02006af 100644 --- a/test/token-stats.test.mjs +++ b/test/token-stats.test.mjs @@ -25,6 +25,143 @@ after(() => { const stats = await import("../token-stats.mjs"); const protocol = await import("../protocol.mjs"); const speedStats = await import("../speed-stats.mjs"); +const pricing = await import("../openrouter-pricing.mjs"); + +function testPriceCatalog(at = Date.now()) { + return pricing.parseOpenRouterCatalog( + { + data: [ + { + id: "openai/gpt-6.1-sol", + pricing: { + prompt: "0.000002", + completion: "0.00001", + input_cache_read: "0.0000001", + input_cache_write: "0.0000025", + }, + }, + ], + }, + at, + ); +} + +test("明确缓存写入零可计价,缺失分类未知,legacy回声不重复计费", () => { + const modern = JSON.parse( + usage("2026-11-03T11:00:01Z", "priced-model", { model: "gpt-6.1-sol", input: 100, cached: 30, output: 20 }), + ); + modern.payload.usage.cache_write_input_tokens = 0; + writeLines([ + JSON.stringify(modern), + event("2026-11-03T11:00:01.010Z", "event_msg", { + type: "token_count", + info: { + last_token_usage: { + input_tokens: 100, + cached_input_tokens: 30, + output_tokens: 20, + total_tokens: 120, + cache_write_input_tokens: 0, + }, + total_token_usage: { input_tokens: 100, cached_input_tokens: 30, output_tokens: 20, total_tokens: 120 }, + }, + }), + usage("2026-11-03T11:00:02Z", "missing-write", { model: "gpt-6.1-sol", input: 50, cached: 10, output: 10 }), + ]); + const catalog = testPriceCatalog(Date.parse("2026-11-03T11:00:00Z")); + const result = stats.buildStats(new stats.IncrementalRolloutParser(file).readAll(), { + pricing: { catalog, status: "fresh", fetchedAt: catalog.fetchedAt }, + }); + assert.equal(result.requestCount, 2); + assert.equal(result.modelUsage[0].pricedRequestCount, 1); + assert.equal(result.modelUsage[0].unpricedRequestCount, 1); + assert.equal(result.modelUsage[0].costStatus, "partial"); + assert.ok(Math.abs(result.modelUsage[0].openRouterReferenceUsd - 0.000343) < 1e-12); + assert.equal(result.turns[0].requests[1].openRouterReferenceUsd, null); +}); + +test("早于价目快照的旧请求不能以当前公开价格追溯计费", () => { + const row = JSON.parse(usage("2026-11-03T11:00:01Z", "before-price", { model: "gpt-6.1-sol" })); + row.payload.usage.cache_write_input_tokens = 0; + writeLines([JSON.stringify(row)]); + const catalog = testPriceCatalog(Date.parse("2026-11-03T12:00:00Z")); + const result = stats.buildStats(new stats.IncrementalRolloutParser(file).readAll(), { + pricing: { catalog, status: "fresh", fetchedAt: catalog.fetchedAt }, + }); + assert.equal(result.modelUsage[0].openRouterReferenceUsd, null); + assert.equal(result.modelUsage[0].unpricedRequestCount, 1); + assert.equal(result.turns[0].requests[0].costStatus, "missing-price"); +}); + +test("价格刷新以缓存原始时间到期,不将23小时旧快照延长至47小时", async () => { + const now = Date.now(); + const hour = 60 * 60 * 1000; + const cacheFile = path.join(testRoot, "price-refresh", "catalog.json"); + fs.mkdirSync(path.dirname(cacheFile), { recursive: true }); + fs.writeFileSync(cacheFile, JSON.stringify(testPriceCatalog(now - 23 * hour)), "utf8"); + let fetches = 0; + const fetchImpl = async () => { + fetches += 1; + return { + ok: true, + json: async () => ({ + data: [{ id: "openai/gpt-6.1-sol", pricing: { prompt: "0.000002", completion: "0.00001" } }], + }), + }; + }; + try { + await stats.refreshPricingIfDue(now, { cacheFile, fetchImpl }); + assert.equal(fetches, 0); + await stats.refreshPricingIfDue(now + hour - 1, { cacheFile, fetchImpl }); + assert.equal(fetches, 0); + await stats.refreshPricingIfDue(now + hour + 1, { cacheFile, fetchImpl }); + assert.equal(fetches, 1); + } finally { + stats.setPricingSnapshot(null); + } +}); + +test("模型分类按请求归属,切换和未知模型不重算或重复累计", () => { + writeLines([ + model("2026-11-03T10:00:00Z", "gpt-6.1-sol"), + usage("2026-11-03T10:00:01Z", "model-a", { input: 100, cached: 30, output: 20 }), + usage("2026-11-03T10:00:01Z", "model-a", { input: 100, cached: 30, output: 20 }), + usage("2026-11-03T10:00:02Z", "model-b", { model: "gpt-6-astra", input: 50, cached: 10, output: 10 }), + usage("2026-11-03T10:00:03Z", "model-c", { model: "gpt-6-astra-unlisted", input: 10, cached: 0, output: 1 }), + ]); + const result = stats.buildStats(new stats.IncrementalRolloutParser(file).readAll()); + assert.equal(result.requestCount, 3); + assert.equal(result.modelUsage.length, 3); + assert.deepEqual( + result.modelUsage.map((row) => [row.modelId, row.input, row.cached, row.output, row.total, row.requestCount]), + [ + ["openai/gpt-6.1-sol", 100, 30, 20, 120, 1], + ["openai/gpt-6-astra", 50, 10, 10, 60, 1], + [null, 10, 0, 1, 11, 1], + ], + ); + assert.equal(result.modelUsage[2].openRouterReferenceUsd, null); + const payload = stats.payloadFor(result); + assert.deepEqual(payload.modelUsage, result.modelUsage); + assert.equal(payload.requestDetails[2].modelId, null); + assert.equal(JSON.stringify(payload).includes("gpt-6-astra-unlisted"), false); +}); + +test("模型费用未知分类不假定零,协议拒绝未知模型和坏覆盖率", () => { + writeLines([usage("2026-11-03T10:00:01Z", "model-unknown-cost", { model: "gpt-6.1-sol" })]); + const result = stats.buildStats(new stats.IncrementalRolloutParser(file).readAll()); + assert.equal(result.modelUsage[0].openRouterReferenceUsd, null); + assert.equal(result.modelUsage[0].unpricedRequestCount, 1); + const payload = stats.payloadFor(result); + assert.equal(payload.schemaVersion, 5); + assert.equal(protocol.validatePayload(payload).ok, true); + const bad = structuredClone(payload); + bad.modelUsage[0].modelId = "openai/other"; + assert.equal(protocol.validatePayload(bad).ok, false); + bad.modelUsage[0].modelId = "openai/gpt-6.1-sol"; + bad.modelUsage[0].pricedRequestCount = 2; + assert.equal(protocol.validatePayload(bad).ok, false); +}); test("CDP 端口自动发现:解析命令行、去重并过滤非法值", () => { const text = [ @@ -378,10 +515,10 @@ test("context window is still detected when only task_started carries it", () => assert.equal(stats.payloadFor(stats.buildStats(parsed)).modelContextWindow, windowSize); }); -test("payload protocol validates and negotiates signed schema v4", () => { +test("payload protocol validates and negotiates signed schema v5", () => { const payload = stats.payloadFor(stats.emptyStats(threadId, file)); assert.equal(payload.protocolName, protocol.PROTOCOL_NAME); - assert.equal(payload.schemaVersion, 4); + assert.equal(payload.schemaVersion, 5); assert.equal(payload.authRequired, true); assert.equal(protocol.validatePayload(payload, { allowLegacy: false }).ok, true); assert.equal( @@ -389,7 +526,8 @@ test("payload protocol validates and negotiates signed schema v4", () => { false, "未签名模板不能作为最终负载", ); - assert.equal(protocol.negotiateSchemaVersion([3, 4]), 4); + assert.equal(protocol.negotiateSchemaVersion([3, 4, 5]), 5); + assert.equal(protocol.negotiateSchemaVersion([3, 4]), null); assert.equal(protocol.negotiateSchemaVersion([1]), null); assert.equal(protocol.validatePayload({ ...payload, protocolName: "wrong" }).ok, false); assert.equal(protocol.validatePayload({ ...payload, health: null }).ok, false); @@ -431,7 +569,7 @@ test("payload protocol validates and negotiates signed schema v4", () => { }); test("checked JSON schema documents the same protocol contract", () => { - const schema = JSON.parse(fs.readFileSync(new URL("../schemas/payload-v4.schema.json", import.meta.url), "utf8")); + const schema = JSON.parse(fs.readFileSync(new URL("../schemas/payload-v5.schema.json", import.meta.url), "utf8")); const template = stats.payloadFor(stats.emptyStats(threadId, file)); const producedKeys = [...Object.keys(template), "auth"].sort(); assert.equal(schema.properties.protocolName.const, protocol.PROTOCOL_NAME); @@ -483,7 +621,10 @@ test("RANGE-1 范围汇总负载通过协议校验且只包含数值与时间标 ttftSampleCount: 4, }; const payload = stats.payloadFor(stats.emptyStats(threadId, file), undefined, null, false, false, rangeSummary); - assert.deepEqual(payload.rangeSummary, rangeSummary); + const { modelUsage, ...originalRange } = payload.rangeSummary; + assert.deepEqual(originalRange, rangeSummary); + assert.equal(modelUsage[0].modelId, null); + assert.equal(modelUsage[0].unpricedRequestCount, rangeSummary.requestCount); assert.equal(protocol.validatePayload(payload, { allowLegacy: false }).ok, true); }); diff --git a/token-stats.mjs b/token-stats.mjs index 002c89b..a38f386 100644 --- a/token-stats.mjs +++ b/token-stats.mjs @@ -38,6 +38,14 @@ import { import { robustSpeedSummary, unionDurationMs, SPEED_SAMPLE_WINDOW } from "./speed-stats.mjs"; import { HISTORY_RANGE_KEYS, UsageHistoryArchive } from "./history-archive.mjs"; import { resolveRuntimePaths } from "./runtime-paths.mjs"; +import { estimateRequestCost, loadOpenRouterCatalog } from "./openrouter-pricing.mjs"; +import { + aggregateModelUsage, + mergeModelUsage, + modelIdentity, + requestModelMetadata, + unknownModelUsage, +} from "./model-usage.mjs"; const SCRIPT_DIR = path.dirname(fileURLToPath(import.meta.url)); const RUNTIME_PATHS = resolveRuntimePaths({ @@ -47,6 +55,82 @@ const RUNTIME_PATHS = resolveRuntimePaths({ scriptDir: SCRIPT_DIR, }); const MONITOR_STARTED_AT = new Date().toISOString(); +let pricingSnapshot = { catalog: null, status: "unavailable", fetchedAt: null, errorCode: null }; +const PRICING_REFRESH_MS = 24 * 60 * 60 * 1000; +let pricingRefreshPromise = null; +let pricingRefreshAt = 0; + +function setPricingSnapshot(snapshot) { + pricingSnapshot = snapshot || { catalog: null, status: "unavailable", fetchedAt: null, errorCode: null }; +} + +async function refreshPricingIfDue(now = Date.now(), options = {}) { + if (pricingRefreshPromise) return pricingRefreshPromise; + if (now < pricingRefreshAt) return pricingSnapshot; + pricingRefreshAt = now + PRICING_REFRESH_MS; + pricingRefreshPromise = loadOpenRouterCatalog({ + cacheFile: path.join(RUNTIME_PATHS.stateDir, "pricing", "openrouter-catalog.json"), + now, + ...options, + }) + .then((snapshot) => { + setPricingSnapshot(snapshot); + const expiresAt = Date.parse(snapshot.fetchedAt) + PRICING_REFRESH_MS; + if (snapshot.status === "fresh" && Number.isFinite(expiresAt)) + pricingRefreshAt = Math.min(now + PRICING_REFRESH_MS, expiresAt); + return snapshot; + }) + .finally(() => { + pricingRefreshPromise = null; + }); + return pricingRefreshPromise; +} + +function pricingPayload() { + const expired = + pricingSnapshot.status === "fresh" && Date.now() - Date.parse(pricingSnapshot.fetchedAt) >= PRICING_REFRESH_MS; + return { + status: expired ? "stale" : pricingSnapshot.status, + fetchedAt: pricingSnapshot.fetchedAt || null, + sourceUrl: "https://openrouter.ai/api/v1/models", + }; +} + +function priceRequest(record, snapshot = pricingSnapshot) { + const identity = modelIdentity(record.model); + // A newly fetched rate is not evidence of the rate for earlier requests. + // Previously archived costs are restored independently by UsageHistoryArchive. + const requestAt = Date.parse(record.ts); + const fetchedAt = Date.parse(snapshot.fetchedAt); + if (identity.modelId && Number.isFinite(fetchedAt) && (!Number.isFinite(requestAt) || requestAt < fetchedAt)) { + return { ...identity, openRouterReferenceUsd: null, costStatus: "missing-price", pricingFetchedAt: null }; + } + const cost = estimateRequestCost(record, snapshot.catalog?.modelsById?.[identity.modelId], { + pricingStatus: snapshot.status, + }); + return { + ...identity, + openRouterReferenceUsd: cost.openRouterReferenceUsd, + costStatus: cost.status, + pricingFetchedAt: snapshot.fetchedAt || null, + }; +} + +function cacheWriteUsage(usage) { + return { + cacheWrite: Number.isFinite(usage.cache_write_input_tokens) ? usage.cache_write_input_tokens : null, + cacheWrite30m: Number.isFinite(usage.cache_write_30m_input_tokens) ? usage.cache_write_30m_input_tokens : null, + cacheWrite12h: Number.isFinite(usage.cache_write_12h_input_tokens) ? usage.cache_write_12h_input_tokens : null, + }; +} + +function cacheWriteUsageFromRecord(record) { + return { + cacheWrite: record.cacheWrite ?? null, + cacheWrite30m: record.cacheWrite30m ?? null, + cacheWrite12h: record.cacheWrite12h ?? null, + }; +} const heartbeatIntervalOverride = Number(process.env.TUI_MONITOR_HEARTBEAT_INTERVAL_MS); const MONITOR_HEARTBEAT_INTERVAL_MS = Number.isFinite(heartbeatIntervalOverride) && heartbeatIntervalOverride >= 1000 ? heartbeatIntervalOverride : 10000; @@ -725,6 +809,7 @@ function captureModernUsage(state, event, timestamp) { responseId, turnId: typeof payload.turn_id === "string" ? payload.turn_id : "", model: state.currentModel, + ...cacheWriteUsage(usage), input: Number.isFinite(usage.input_tokens) ? usage.input_tokens : null, output: Number.isFinite(usage.output_tokens) ? usage.output_tokens : null, cached: Number.isFinite(usage.cached_input_tokens) ? usage.cached_input_tokens : null, @@ -749,6 +834,8 @@ function captureLegacyTokenCount(state, event, payload, timestamp) { if (Number.isFinite(info.model_context_window)) state.modelContextWindow = info.model_context_window; const last = info.last_token_usage || {}; const count = { + model: state.currentModel, + ...cacheWriteUsage(last), ts: timestamp, ordinal: Number.isFinite(event.ordinal) ? event.ordinal : null, genMs: takeGenWindow(state), @@ -1143,6 +1230,7 @@ function requestPayloadFor(request, index) { uncached: input != null && cached != null ? Math.max(0, input - cached) : null, output: request.output != null ? request.output : null, total: request.lastTotal != null ? request.lastTotal : null, + ...requestModelMetadata(request), }; } @@ -1305,7 +1393,8 @@ function collectUnifiedRecords(parsed) { total: count.total, }, turnUsage: null, - model: null, + model: count.model || null, + ...cacheWriteUsageFromRecord(count), }); } @@ -1329,6 +1418,7 @@ function collectUnifiedRecords(parsed) { threadUsage: record.threadUsage || null, turnUsage: record.turnUsage || null, model: record.model || null, + ...cacheWriteUsageFromRecord(record), }); } @@ -1667,7 +1757,7 @@ function locateTurnIndexByTimestamp(turnStarts, ts) { return found >= 0 ? found : 0; } -function assignRecordsToTurns(records, turns, turnIndexById, retainRequests) { +function assignRecordsToTurns(records, turns, turnIndexById, retainRequests, pricing) { const turnCounters = new Map(); const threadCounters = createCumulativeCounters(); const turnStarts = turns.map((turn) => turn.start || ""); @@ -1680,6 +1770,7 @@ function assignRecordsToTurns(records, turns, turnIndexById, retainRequests) { let currentModel = null; const rawSpeedSamples = []; const requestRingStarts = new Map(); + const modelRequests = turns.map(() => []); for (const record of records) { const mappedTurnIndex = record.turnId ? turnIndexById.get(record.turnId) : null; @@ -1755,7 +1846,11 @@ function assignRecordsToTurns(records, turns, turnIndexById, retainRequests) { lastTotal: record.requestTotal != null ? record.requestTotal : null, totalInfo: normalizedThreadUsage, model: record.model || null, + ...cacheWriteUsageFromRecord(record), + ...priceRequest(record, pricing), }; + Object.assign(record, requestModelMetadata(request)); + modelRequests[turnIndex].push(record); turn.lastTs = record.ts || turn.lastTs; if (retainRequests) { turn.requests.push(request); @@ -1812,6 +1907,10 @@ function assignRecordsToTurns(records, turns, turnIndexById, retainRequests) { } } + turns.forEach((turn, index) => { + turn.modelUsage = aggregateModelUsage(modelRequests[index]); + }); + return { turnCounters, threadCounters, @@ -1934,7 +2033,10 @@ function buildSpeedMetrics(turns, recordMetrics, threadCounters, turnCounters) { }; } -function buildStatsUnified(parsed, { retainRequests = true, includeArchiveRecords = false } = {}) { +function buildStatsUnified( + parsed, + { retainRequests = true, includeArchiveRecords = false, pricing = pricingSnapshot } = {}, +) { const collected = collectUnifiedRecords(parsed); const records = collected.records; const { turns, turnIndexById } = seedTurnList(parsed, records); @@ -1943,6 +2045,7 @@ function buildStatsUnified(parsed, { retainRequests = true, includeArchiveRecord turns, turnIndexById, retainRequests, + pricing, ); const latestGenSample = attachTurnTimings(parsed, turns); @@ -1973,6 +2076,7 @@ function buildStatsUnified(parsed, { retainRequests = true, includeArchiveRecord date: parsed.date, modelContextWindow: parsed.modelContextWindow, requestCount: records.length, + modelUsage: mergeModelUsage(turns.flatMap((turn) => turn.modelUsage)), sessionTotal: threadUsage.total != null ? threadUsage.total : null, sessionInput: threadUsage.input != null ? threadUsage.input : null, sessionCached: threadUsage.cached != null ? threadUsage.cached : null, @@ -2012,6 +2116,7 @@ function buildStatsUnified(parsed, { retainRequests = true, includeArchiveRecord startLabel: turn.startLabel, requests: turn.requests, requestCount: turn.requestCount, + modelUsage: turn.modelUsage, input: turn.input, output: turn.output, cached: turn.cacheAvailable ? turn.cached : null, @@ -2030,6 +2135,7 @@ function turnPayloadFor(turn, index) { index: index + 1, startLabel: turn.startLabel, requests: turn.requestCount != null ? turn.requestCount : turn.requests.length, + modelUsage: turn.modelUsage || unknownModelUsage(turn), input: turn.input, cached: turn.cached, uncached: turn.input != null && turn.cached != null ? Math.max(0, turn.input - turn.cached) : null, @@ -2079,6 +2185,16 @@ function payloadFor( schemaVersion: PROTOCOL_SCHEMA_VERSION, capabilities: PROTOCOL_CAPABILITIES, authRequired: true, + modelUsage: + stats.modelUsage || + unknownModelUsage({ + input: stats.sessionInput, + cached: stats.sessionCached, + output: stats.sessionOutput, + total: stats.sessionTotal, + requestCount: stats.requestCount, + }), + pricing: pricingPayload(), modelContextWindow: stats.modelContextWindow, requestCount: stats.requestCount, sessionTotal: stats.sessionTotal, @@ -2103,6 +2219,7 @@ function payloadFor( rangeSummary && typeof rangeSummary === "object" ? { ...rangeSummary, + modelUsage: rangeSummary.modelUsage || unknownModelUsage(rangeSummary), } : null, turnTotal: currentTurn ? currentTurn.total : 0, @@ -3109,6 +3226,7 @@ function resolveFile(threadId) { // Stats for a conversation that exists but has no data yet (shows zeros). function emptyStats(threadId, file) { return { + modelUsage: [], threadId: threadId || "", file: file || "", date: file ? fileDate(file) : "", @@ -3161,6 +3279,8 @@ function writeLastSummary(session, stats, threadId) { sessionInput: stats.sessionInput, sessionCached: stats.sessionCached, sessionOutput: stats.sessionOutput, + modelUsage: stats.modelUsage || [], + turnModelUsage: currentTurn?.modelUsage || [], requestCount: stats.requestCount, currentTurnRequestCount: currentTurn ? currentTurn.requestCount : 0, contextUsed: stats.contextUsed, @@ -3226,6 +3346,15 @@ function snapshotStatsFrom(summary) { sessionInput: summary.sessionInput, sessionCached: summary.sessionCached, sessionOutput: summary.sessionOutput, + modelUsage: + summary.modelUsage || + unknownModelUsage({ + input: summary.sessionInput, + cached: summary.sessionCached, + output: summary.sessionOutput, + total: summary.sessionTotal, + requestCount: summary.requestCount, + }), requestCount: summary.requestCount, contextUsed: summary.contextUsed, contextBreakdown: summary.contextBreakdown || null, @@ -3254,6 +3383,15 @@ function snapshotStatsFrom(summary) { start: "", startLabel: "上次数据", requests: [], + modelUsage: + summary.turnModelUsage || + unknownModelUsage({ + input: summary.turnInput, + cached: summary.turnCached, + output: summary.turnOutput, + total: summary.turnTotal, + requestCount: summary.currentTurnRequestCount || 0, + }), requestCount: Number.isFinite(summary.currentTurnRequestCount) ? summary.currentTurnRequestCount : 0, input: Number.isFinite(summary.turnInput) ? summary.turnInput : 0, cached: Number.isFinite(summary.turnCached) ? summary.turnCached : 0, @@ -4273,7 +4411,6 @@ async function publishWatchRound(args, session, context) { const currentTurn = stats.turns.length ? stats.turns[stats.turns.length - 1] : null; const lastRequest = currentTurn && currentTurn.requests.length ? currentTurn.requests[currentTurn.requests.length - 1] : null; - writeLastSummary(session, stats, resolvedThreadId); if (resolvedThreadId && resolvedThreadId !== NEW_THREAD && stats.archiveRecords) { try { usageHistoryArchive.archiveSession({ @@ -4282,6 +4419,7 @@ async function publishWatchRound(args, session, context) { records: stats.archiveRecords, now: Date.now(), }); + stats.modelUsage = mergeModelUsage(stats.turns.flatMap((turn) => turn.modelUsage || [])); session.lastArchiveErrorAt = 0; } catch (error) { const now = Date.now(); @@ -4292,6 +4430,7 @@ async function publishWatchRound(args, session, context) { } } + writeLastSummary(session, stats, resolvedThreadId); session.health = nextWatchHealth(args, session, context.parsed, rawThread, connectionState); // 健康快照每个巡检周期都落盘(运行中 1s、空闲 5s),即使本周期没有向页面推送。 // 这样 monitor-health.json 的 mtime 与 lastSuccessAt 才能真正反映监控存活。 @@ -4499,6 +4638,12 @@ async function runWatchLoop(args) { } }); const session = createWatchSession(args); + // Fetch once on startup, then on a separate 24-hour timer; never per heartbeat. + await refreshPricingIfDue(); + const pricingTimer = setInterval(() => { + refreshPricingIfDue().catch(() => {}); + }, 60 * 1000); + pricingTimer.unref(); persistMonitorHeartbeat(session, { force: true, phase: "starting" }); persistMonitorHealth(session.health); pruneMonitorLogs(); @@ -4569,6 +4714,8 @@ async function main() { return; } + if (args.cdp) await refreshPricingIfDue(); + const files = args.thread ? locateThreadFiles(findRolloutFiles(), args.thread) : (() => { @@ -4647,6 +4794,8 @@ export { parseFile, buildStats, payloadFor, + setPricingSnapshot, + refreshPricingIfDue, signPayloadWithAuth, snapshotStatsFrom, monitorHealthFor, diff --git a/tokens-ui-for-codex-panel.js b/tokens-ui-for-codex-panel.js index d6a76f6..efaa852 100644 --- a/tokens-ui-for-codex-panel.js +++ b/tokens-ui-for-codex-panel.js @@ -1,8 +1,8 @@ // Tokens UI For Codex —— Codex++ 用户脚本(统计条 + 详情弹层) -// 作者:Littps · 许可:MIT · 协议:tokens-ui-for-codex(签名 schema v4) +// 作者:Littps · 许可:MIT · 协议:tokens-ui-for-codex(签名 schema v5) // // 作用:把紧凑的 Token 统计条挂到输入区右下的「上下文用量圆圈」左侧,并提供左键单击打开的详情弹层。 -// 依赖:本地监控进程 `node token-stats.mjs --watch --cdp`(经本地 CDP 推送签名 schema v4 负载)。 +// 依赖:本地监控进程 `node token-stats.mjs --watch --cdp`(经本地 CDP 推送签名 schema v5 负载)。 // 边界:不修改 Codex 的 React bundle、不注册任何键盘监听、不读取或展示用户消息正文。 // 安装:把本文件放入 %APPDATA%\Codex++\user_scripts\ ,并在 Codex++ 中启用用户脚本。 (() => { @@ -11,7 +11,28 @@ // The widget is inserted beside Codex's native context ring. It deliberately // avoids patching React internals or showing a floating panel. const PROTOCOL_NAME = "tokens-ui-for-codex"; - const PROTOCOL_SCHEMA_VERSION = 4; + const PROTOCOL_SCHEMA_VERSION = 5; + const MODEL_LABELS = { + "openai/gpt-6.1-sol": "GPT-6.1 Sol", + "openai/gpt-6-astra": "GPT-6 Astra", + "openai/gpt-6.1-terra": "GPT-6.1 Terra", + "openai/gpt-6-sol": "GPT-6 Sol", + "openai/gpt-6-luna": "GPT-6 Luna", + "openai/gpt-5.6-sol": "GPT-5.6 Sol", + "openai/gpt-5.6-terra": "GPT-5.6 Terra", + "openai/gpt-5.6-luna": "GPT-5.6 Luna", + }; + const COST_STATUSES = [ + "priced", + "partial", + "unknown-model", + "missing-usage", + "missing-price", + "stale-price", + "unavailable", + "unsupported-tier", + "unknown-mode", + ]; const AUTH_VERSION = 2; const AUTH_DOMAIN = "tokens-ui-for-codex/auth-v2"; const AUTH_KEY_ID_PATTERN = /^[0-9a-f]{32}$/; @@ -35,6 +56,8 @@ "timing-v1", "history-range-v1", "context-breakdown-estimated-v1", + "model-usage-v1", + "public-price-reference-v1", ]; const INSTALL_FLAG = "__tuiForCodexNativeUiInstalled"; if (window[INSTALL_FLAG]) return; @@ -331,6 +354,47 @@ return `rangeSummary.${key} 字段无效`; } } + return validateModelUsage(summary.modelUsage, summary.requestCount); + } + + function validModelIdentity(item) { + return ( + item && + (item.modelId === null + ? item.modelLabel === "未知模型" + : Object.hasOwn(MODEL_LABELS, item.modelId) && item.modelLabel === MODEL_LABELS[item.modelId]) + ); + } + + function validateModelUsage(usage, expectedCount) { + if (!Array.isArray(usage) || usage.length > 9) return "模型汇总数量无效"; + const seen = new Set(); + for (const item of usage) { + if (!validModelIdentity(item) || seen.has(item.modelId)) return "模型归属无效或重复"; + seen.add(item.modelId); + for (const key of ["requestCount", "pricedRequestCount", "unpricedRequestCount"]) { + if (!Number.isSafeInteger(item[key]) || item[key] < 0) return `模型 ${key} 无效`; + } + for (const key of ["input", "cached", "output", "total"]) { + if (item[key] !== null && (!Number.isSafeInteger(item[key]) || item[key] < 0)) return `模型 ${key} 无效`; + } + if (!isValidCachePair(item.input, item.cached)) return "模型缓存超过输入"; + if (item.openRouterReferenceUsd !== null && !isNonNegativeNumber(item.openRouterReferenceUsd)) + return "模型费用无效"; + if (item.modelId === null && item.pricedRequestCount !== 0) return "未知模型不可计价"; + if ( + item.pricingFetchedAt !== null && + (typeof item.pricingFetchedAt !== "string" || !Number.isFinite(Date.parse(item.pricingFetchedAt))) + ) + return "模型计价时间无效"; + if (item.pricedRequestCount + item.unpricedRequestCount !== item.requestCount) return "模型计价覆盖无效"; + const status = + item.pricedRequestCount === 0 ? "unavailable" : item.unpricedRequestCount > 0 ? "partial" : "priced"; + if (item.costStatus !== status || (item.pricedRequestCount === 0) !== (item.openRouterReferenceUsd === null)) + return "模型计价状态无效"; + } + if (expectedCount !== undefined && usage.reduce((sum, item) => sum + item.requestCount, 0) !== expectedCount) + return "模型请求数与汇总不一致"; return ""; } @@ -339,6 +403,17 @@ if (data.protocolName !== PROTOCOL_NAME) return { ok: false, error: "统计协议名称不匹配" }; if (data.schemaVersion !== PROTOCOL_SCHEMA_VERSION) return { ok: false, error: "统计协议版本不匹配" }; if (data.authRequired !== true) return { ok: false, error: "统计协议要求签名负载" }; + const modelError = validateModelUsage(data.modelUsage, data.requestCount); + if (modelError) return { ok: false, error: modelError }; + if ( + !data.pricing || + !["fresh", "stale", "unavailable"].includes(data.pricing.status) || + data.pricing.sourceUrl !== "https://openrouter.ai/api/v1/models" || + (data.pricing.fetchedAt !== null && + (typeof data.pricing.fetchedAt !== "string" || !Number.isFinite(Date.parse(data.pricing.fetchedAt)))) + ) { + return { ok: false, error: "价格快照无效" }; + } if ( !Array.isArray(data.capabilities) || !data.capabilities.includes("health-snapshot") || @@ -449,6 +524,8 @@ if (!Array.isArray(data.requestDetails) || data.requestDetails.length > 500) return { ok: false, error: "请求明细超出限制" }; for (const turn of data.turns) { + const modelError = validateModelUsage(turn && turn.modelUsage, turn && turn.requests); + if (modelError) return { ok: false, error: modelError }; if ( !turn || !Number.isInteger(turn.index) || @@ -479,6 +556,21 @@ } } for (const request of data.requestDetails) { + if ( + !validModelIdentity(request) || + !COST_STATUSES.includes(request.costStatus) || + (request.openRouterReferenceUsd !== null && !isNonNegativeNumber(request.openRouterReferenceUsd)) + ) { + return { ok: false, error: "请求模型费用无效" }; + } + if ( + ["priced", "stale-price"].includes(request.costStatus) !== (request.openRouterReferenceUsd !== null) || + (request.modelId === null && request.openRouterReferenceUsd !== null) || + (request.pricingFetchedAt !== null && + (typeof request.pricingFetchedAt !== "string" || !Number.isFinite(Date.parse(request.pricingFetchedAt)))) + ) { + return { ok: false, error: "请求计价状态或时间无效" }; + } if (!request || !Number.isInteger(request.index) || request.index < 1 || typeof request.time !== "string") { return { ok: false, error: "请求明细字段无效" }; } @@ -579,6 +671,8 @@ state.payloadDataStale = validation.ok && !nextData.snapshot && Date.now() - Date.parse(nextData.updatedAt) > 90000; if (!validation.ok) return; if (nextData.sessionChanged === true) { + if (state.dialog && state.dialog.__tuiParts && state.dialog.__tuiParts.models) + state.dialog.__tuiParts.models.selectedModel = null; state.lastExpandedData = null; state.lastExpandedAt = 0; state.detailRenderFromCache = false; @@ -732,6 +826,12 @@ data.sessionOutput, data.contextUsed, data.contextBreakdown && JSON.stringify(data.contextBreakdown), + JSON.stringify(data.modelUsage), + JSON.stringify(data.pricing), + JSON.stringify(data.turns.map((turn) => turn.modelUsage)), + JSON.stringify( + data.requestDetails.map((request) => [request.modelId, request.openRouterReferenceUsd, request.costStatus]), + ), data.sessionWallMs, data.sessionToolWallMs, data.sessionToolCallCount, @@ -1245,6 +1345,14 @@ font-size: 10px; } #${DIALOG_ID} .tui-dialog-list-total { font-variant-numeric: tabular-nums; font-weight: 550; text-align: right; } +#${DIALOG_ID} .tui-model-list { display: grid; gap: 5px; margin: 8px 0; } +#${DIALOG_ID} .tui-model-row { display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 4px 10px; padding: 8px 10px; border: 1px solid var(--tui-border,rgba(127,127,127,.2)); border-radius: 8px; background: var(--tui-surface-2); color: inherit; font: inherit; text-align: left; cursor: pointer; } +#${DIALOG_ID} .tui-model-row[aria-pressed="true"] { outline: 1px solid var(--tui-text-dim); } +#${DIALOG_ID} .tui-model-row small { grid-column: 1 / -1; font-size: 10px; color: var(--tui-text-dim); } +#${DIALOG_ID} .tui-model-scope { font: inherit; color: inherit; background: var(--tui-surface-2); border: 1px solid var(--tui-border,rgba(127,127,127,.25)); border-radius: 6px; padding: 4px 7px; margin: 7px 0; } +#${DIALOG_ID} .tui-model-detail { margin-top: 10px; } +#${DIALOG_ID} .tui-model-detail h4 { margin: 6px 0; font-size: 12px; } +#${DIALOG_ID} .tui-model-pricing, #${DIALOG_ID} .tui-model-cost-summary, #${DIALOG_ID} .tui-model-records { white-space: pre-line; overflow-wrap: anywhere; font-size: 10px; line-height: 1.6; color: var(--tui-text-dim); } #${DIALOG_ID} .tui-dialog-muted { color: var(--tui-text-dim); font-size: 11px; @@ -1486,7 +1594,7 @@ } function requestNoteText(request) { - return `${request.time || "时间未知"} · 输入 ${fmtShort(request.input)} · 命中 ${fmtShort(request.cached)} · 未命中 ${fmtShort(request.uncached)} · 输出 ${fmtShort(request.output)}`; + return `${request.time || "时间未知"} · ${request.modelLabel} · 输入 ${fmtShort(request.input)} · 命中 ${fmtShort(request.cached)} · 未命中 ${fmtShort(request.uncached)} · 输出 ${fmtShort(request.output)} · 参考 ${usdText(request.openRouterReferenceUsd)}${request.openRouterReferenceUsd == null ? "(未知)" : ""}`; } function buildRequestRow(request) { @@ -1509,7 +1617,191 @@ } function turnNoteText(turn) { - return `${turn.startLabel || "时间未知"} · ${fmtInt(turn.requests)} 次请求 · 输入 ${fmtShort(turn.input)} · 输出 ${fmtShort(turn.output)}`; + return `${turn.startLabel || "时间未知"} · ${fmtInt(turn.requests)} 次请求 · 输入 ${fmtShort(turn.input)} · 输出 ${fmtShort(turn.output)} · ${modelCostText(turn.modelUsage)}`; + } + + function usdText(value) { + if (value == null || !Number.isFinite(value)) return "--"; + if (value > 0 && value < 0.000001) return "<$0.000001"; + return "$" + value.toLocaleString("en-US", { minimumFractionDigits: 2, maximumFractionDigits: 6 }); + } + + function buildModelParts() { + const node = textNode("div", "tui-models"); + const pricing = textNode("div", "tui-model-pricing"); + const costSummary = textNode("div", "tui-model-cost-summary"); + const scope = document.createElement("select"); + scope.className = "tui-model-scope"; + scope.setAttribute("aria-label", "模型统计范围"); + for (const [value, label] of [ + ["session", "当前会话"], + ["turn", "当前提问"], + ["range", "已选时间范围"], + ]) { + const option = document.createElement("option"); + option.value = value; + option.textContent = label; + scope.append(option); + } + const list = textNode("div", "tui-model-list"); + const detail = textNode("div", "tui-model-detail"); + const title = textNode("h4", "", "单模型详情"); + const grid = dialogGrid([ + ["输入", "--"], + ["缓存命中", "--"], + ["输出", "--"], + ["总量", "--"], + ["请求数", "--"], + ["参考费用", "--"], + ]); + const coverage = textNode("div", "tui-model-records"); + const records = textNode("div", "tui-model-records"); + detail.append(title, grid, coverage, records); + node.append( + pricing, + costSummary, + scope, + list, + detail, + textNode( + "div", + "tui-dialog-muted", + "按 OpenRouter 公开价折算,单位 USD;不是 Codex 实际账单或订阅剩余额度。缺少模型、价格或用量时显示未知;Fast、Batch、12h 等未核验档位不猜价。", + ), + ); + const parts = { + node, + pricing, + costSummary, + scope, + list, + detail, + title, + coverage, + records, + cells: [...grid.querySelectorAll(".tui-dialog-card-value")], + selectedModel: null, + listKey: "", + rows: new Map(), + data: null, + rangeSummary: null, + }; + scope.addEventListener("change", () => { + parts.selectedModel = null; + updateModelParts(parts, parts.data, parts.rangeSummary); + }); + return parts; + } + + function updateModelParts(parts, data, rangeSummary) { + parts.data = data; + parts.rangeSummary = rangeSummary; + const current = currentTurnOf(data); + const usage = + parts.scope.value === "range" + ? rangeSummary && rangeSummary.modelUsage + : parts.scope.value === "turn" + ? current && current.modelUsage + : data.modelUsage; + const items = usage || []; + const pricing = data.pricing; + setCellText( + parts.pricing, + `按 OpenRouter 公开价折算 · ${pricing.status === "fresh" ? "价目有效" : pricing.status === "stale" ? "陈旧缓存价目" : "价目不可用"} · 价目更新 ${pricing.fetchedAt ? new Date(pricing.fetchedAt).toLocaleString() : "--"}`, + ); + setCellText( + parts.costSummary, + `会话:${modelCostText(data.modelUsage)}\n当前提问:${modelCostText(current && current.modelUsage)}\n${RANGE_LABELS[state.range]}:${rangeSummary ? modelCostText(rangeSummary.modelUsage) : "等待范围统计"}`, + ); + const key = items.map((item) => item.modelId || "unknown").join("|"); + if (parts.listKey !== key || !parts.rows.size) { + parts.listKey = key; + parts.rows.clear(); + parts.list.replaceChildren(); + for (const item of items) { + const button = textNode("button", "tui-model-row"); + button.type = "button"; + button.setAttribute("aria-label", item.modelLabel + " · 详情"); + const label = textNode("span", "", item.modelLabel); + const cost = textNode("span"); + const note = textNode("small"); + button.append(label, cost, note); + button.addEventListener("click", () => { + parts.selectedModel = item.modelId || "unknown"; + updateModelParts(parts, parts.data, parts.rangeSummary); + }); + parts.rows.set(item.modelId || "unknown", { button, cost, note }); + parts.list.append(button); + } + if (!items.length) parts.list.append(textNode("div", "tui-dialog-muted", "暂无模型记录 / 等待结构化数据")); + } + if (!items.some((item) => (item.modelId || "unknown") === parts.selectedModel)) + parts.selectedModel = items.length ? items[0].modelId || "unknown" : null; + for (const item of items) { + const row = parts.rows.get(item.modelId || "unknown"); + row.button.setAttribute("aria-pressed", String((item.modelId || "unknown") === parts.selectedModel)); + setCellText( + row.cost, + usdText(item.openRouterReferenceUsd) + + (item.costStatus === "partial" ? " 部分" : item.costStatus === "unavailable" ? " 未知" : ""), + ); + setCellText( + row.note, + `${fmtInt(item.requestCount)} 次请求 · 输入 ${item.input == null ? "--" : fmtInt(item.input)} · 命中 ${item.cached == null ? "--" : fmtInt(item.cached)} · 输出 ${item.output == null ? "--" : fmtInt(item.output)} · 总量 ${item.total == null ? "--" : fmtInt(item.total)}`, + ); + } + const selected = items.find((item) => (item.modelId || "unknown") === parts.selectedModel); + parts.detail.hidden = !selected; + if (!selected) return; + setCellText(parts.title, selected.modelLabel + " · 单模型详情"); + const values = [selected.input, selected.cached, selected.output, selected.total, selected.requestCount].map( + (value) => (value == null ? "--" : fmtInt(value)), + ); + values.push(usdText(selected.openRouterReferenceUsd)); + parts.cells.forEach((cell, index) => setCellText(cell, values[index])); + setCellText( + parts.coverage, + modelCostText([selected]) + + `\n历史计价快照:${selected.pricingFetchedAt ? new Date(selected.pricingFetchedAt).toLocaleString() : "未知 / 混合快照"}`, + ); + if (parts.scope.value === "range") { + setCellText( + parts.records, + "历史范围按已归档数值汇总;旧记录缺少归属或历史价格时保持未知,不用当前价格回填。\n请求明细与各轮摘要仅展示当前会话已加载部分。", + ); + return; + } + const requests = data.requestDetails + .filter((request) => request.modelId === selected.modelId) + .slice() + .reverse(); + const turns = data.turns + .filter((turn) => turn.modelUsage.some((item) => item.modelId === selected.modelId)) + .slice() + .reverse(); + const turnText = turns + .map((turn) => { + const item = turn.modelUsage.find((entry) => entry.modelId === selected.modelId); + return `第 ${turn.index} 轮:${item.total == null ? "--" : fmtInt(item.total)} tokens / ${fmtInt(item.requestCount)} 次 / ${usdText(item.openRouterReferenceUsd)}`; + }) + .join(";"); + setCellText( + parts.records, + `请求(当前提问已加载部分):${requests.map((request) => `#${request.index} · ${fmtInt(request.total)} tokens · ${usdText(request.openRouterReferenceUsd)}`).join(";") || "--"}\n轮次(当前会话已加载部分):${turnText || "--"}`, + ); + } + + function modelCostText(usage) { + if (!Array.isArray(usage) || !usage.length) return "参考费用 --(未知)"; + let sum = 0; + let priced = 0; + let count = 0; + for (const item of usage) { + if (item.openRouterReferenceUsd != null) sum += item.openRouterReferenceUsd; + priced += item.pricedRequestCount; + count += item.requestCount; + } + return `参考费用 ${priced ? usdText(sum) : "--"}${priced && priced < count ? "(部分小计)" : priced ? "" : "(未知)"} · 已计价 ${fmtInt(priced)}/${fmtInt(count)} 次请求`; } function buildTurnRow(turn, index, isCurrent) { @@ -1640,6 +1932,9 @@ parts.turn.timingNode = turnTiming.node; parts.sections.push(dialogSection("当前提问统计", turnBody)); + parts.models = buildModelParts(); + parts.sections.push(dialogSection("GPT 模型与参考费用", parts.models.node)); + // 上下文使用 const contextValue = textNode("span", "tui-dialog-context-value", fmtInt(m.contextUsed)); const contextLimit = textNode( @@ -1844,6 +2139,7 @@ const rangeCapable = Array.isArray(data.capabilities) && data.capabilities.includes("history-range-v1"); const rangeState = rangeCapable ? effectiveRangeSummary(data) : null; const rangeSummary = rangeState ? rangeState.summary : null; + updateModelParts(parts.models, data, rangeSummary); if (parts.session.rangeControl) { parts.session.rangeControl.select.value = state.range; parts.session.rangeControl.select.disabled = !rangeCapable;