root
b4c5f0f0fc
refactor(billing): 计费语义下沉到 Lua(内核只解 YAML/传数据)+ 采纳 PR #2 语义
按 issue #1 的修复方向(维护者评论):内核不再持有任何计费语义,billing 逻辑
全部在 `plugins/billing.lua`;内核只负责解 config.yaml、校验 schema、把
profile + 源 base_url 作为**数据**交给插件,再把插件算出的价目通过 `prices`
侧车注入。计费账目仍持久化在插件状态文件(`plugin_dir/.billing.state.json`)。
## 变更
- **删除** `internal/billing`(compile.go 359 行 + 其 Go 测试);
- 新增 `core.compileBillingPrices`:把 profile/sources JSON 化后调用插件导出
`compile_prices`,返回价目表 + 未匹配规则清单(供错误日志点名 URL);
- `applyBillingDSL` / 规则编辑器改为走同一个插件编译入口(编辑器 lenient,
半成品规则可保存、可报告);
- `billing.lua` 新增 `compile_prices`:URL 归一匹配、首条命中优先、
free/subscription/unpriced/token、USD/M→per-token、peak 窗口、
long_context 边际价、reasoning 可选价、default 策略、未匹配清单。
错误通过载荷 `error` 字段回传(插件导出第二返回值通道不可靠)。
## 与 PR #2(valuelesser)合并后的语义
- **token 规则不产生任何源条目**:未列价的模型落到 unpriced 统计(可见),
不再"静默计 $0"。比 issue 正文里的 `{per_request:0}` 标记更收口;
- `default: free` 由**编译期**给未匹配源写 `{per_request:0}` 标记;
`unpriced`/缺省不写。删除无人读取的 `default_mode` 死键;
- 保留 `unmatched_rules` 清单(只用于降级应用时点名过期 URL)。
## 判据(原 Go 单测平移到 Lua + PR #2 的用例)
- `internal/lua/billing_compile_test.go`:通配符不标记源、token 规则无源条目、
任意源条目不得携带 token 价(根因直防)、free/subscription 保留有意零价、
reasoning 缺席、long_context 三种形态、peak 形状、URL 归一+首条优先、
lenient 未匹配清单、硬错误 5 例、default 两态;
- `internal/lua/billing_test.go`:编译输出驱动真实 priceFor 的 A/B 用例
(旧形状 cost=0 / 新形状 0.75 且 unpriced=0)、default 编译期语义 e2e;
- `internal/gateway`:token 规则断言"无源条目"、模型价 per-token 量级;
- `internal/core`:过期规则 URL 降级应用(有效规则仍生效)。
2026-10-20 18:33:26 +08:00
..
2026-10-19 22:32:18 +08:00
2026-10-20 18:33:26 +08:00
2026-10-16 20:47:14 +08:00
2026-10-15 20:30:33 +08:00
2026-10-16 14:52:13 +08:00
2026-10-15 20:30:33 +08:00
2026-10-13 22:51:50 +08:00
2026-10-20 08:27:28 +08:00
2026-10-13 22:34:13 +08:00
2026-10-20 08:27:28 +08:00
2026-10-06 15:02:42 +08:00
2026-10-12 16:37:23 +08:00
2026-10-11 09:23:34 +08:00
2026-10-11 09:23:34 +08:00
2026-10-17 00:01:00 +08:00
2026-10-20 18:33:26 +08:00
2026-10-02 12:41:35 +08:00
2026-10-20 18:33:26 +08:00
2026-10-13 13:42:57 +08:00
2026-10-13 23:42:04 +08:00
2026-10-13 23:28:27 +08:00
2026-10-12 12:32:10 +08:00
2026-10-13 13:42:57 +08:00
2026-10-02 10:45:20 +08:00
2026-10-13 13:42:57 +08:00
2026-10-02 14:07:44 +08:00
2026-10-13 06:57:28 +08:00
2026-10-13 06:57:28 +08:00
2026-10-14 10:55:50 +08:00
2026-10-13 13:42:57 +08:00
2026-10-04 14:50:46 +08:00
2026-10-11 09:35:32 +08:00
2026-10-05 08:17:57 +08:00
2026-10-05 08:17:57 +08:00
2026-10-11 13:25:11 +08:00
2026-10-11 10:03:43 +08:00
2026-10-12 12:32:10 +08:00
2026-10-13 22:28:34 +08:00
2026-10-14 11:31:41 +08:00
2026-10-05 08:17:57 +08:00
2026-10-05 08:17:57 +08:00
2026-10-05 08:17:57 +08:00
2026-10-11 09:35:32 +08:00
2026-10-11 09:35:32 +08:00
2026-10-15 14:57:13 +08:00
2026-10-13 06:57:28 +08:00
2026-10-13 06:57:28 +08:00
2026-10-19 13:43:18 +08:00
2026-10-13 06:57:28 +08:00
2026-10-16 00:56:14 +08:00
2026-10-14 11:31:41 +08:00
2026-10-16 00:56:14 +08:00
2026-10-11 20:50:52 +08:00
2026-10-07 17:37:22 +08:00
2026-10-05 08:17:57 +08:00
2026-10-11 13:25:11 +08:00
2026-10-15 22:43:08 +08:00
2026-10-13 13:42:57 +08:00
2026-10-11 09:35:32 +08:00
2026-10-04 14:50:46 +08:00
2026-10-13 13:42:57 +08:00
2026-10-16 00:56:14 +08:00
2026-10-07 17:37:22 +08:00
2026-10-11 09:35:32 +08:00
2026-10-12 12:32:10 +08:00
2026-10-14 10:46:05 +08:00
2026-10-13 13:42:57 +08:00
2026-10-12 16:32:30 +08:00
2026-10-14 11:31:41 +08:00
2026-10-11 08:17:56 +08:00
2026-10-11 09:35:32 +08:00
2026-10-19 22:32:18 +08:00
2026-10-12 19:07:09 +08:00
2026-10-19 22:32:18 +08:00