@njuptlzf/agent-chat-server 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/authz.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { DatabaseSync } from "node:sqlite";
2
2
  import { type Model } from "casbin";
3
- import type { MembershipStore } from "./stores.ts";
3
+ import type { MembershipStore, QuotaCaps } from "./stores.ts";
4
4
  /**
5
5
  * 权限内核。
6
6
  *
@@ -56,13 +56,19 @@ export interface LimitsConfig {
56
56
  dailyTokenQuota?: number;
57
57
  memberDailyTokenQuota?: number;
58
58
  memberMonthlyTokenPool?: number;
59
+ /**
60
+ * 单次 run 的 token 上界(T3 严格 token 上限)。reserve 时按它占位,让在飞
61
+ * run 参与 tokenCap 判定;<=0/缺省 = 不启用,行为与 W3 完全一致。
62
+ * 产品级可按 `limits.personas.<id>` 逐人格覆盖。
63
+ */
64
+ perRunTokenCap?: number;
65
+ /** 成本模型(CU 维度)额度:每 run 上界 / 每日总量,独立于 token 体系。 */
66
+ perRunCostCap?: number;
67
+ dailyCostCap?: number;
59
68
  /** 按人格(账本产品)覆盖;缺省继承产品级。 */
60
69
  personas?: Record<string, LimitsConfig>;
61
70
  }
62
- export declare function capsForRoles(roles: string[], limits: LimitsConfig): {
63
- runCap?: number;
64
- tokenCap?: number;
65
- };
71
+ export declare function capsForRoles(roles: string[], limits: LimitsConfig): QuotaCaps;
66
72
  /** 某人格的生效上限 = 产品级 + `limits.personas.<id>` 覆盖(逐字段)。 */
67
73
  export declare function limitsForPersona(limits: LimitsConfig, persona: string): LimitsConfig;
68
74
  /**
@@ -70,10 +76,7 @@ export declare function limitsForPersona(limits: LimitsConfig, persona: string):
70
76
  * `isMember` 必须是「这个人格的账本」是否在期(admin 恒真)——
71
77
  * 否则买了 A 人格会解锁 B 人格的次数。
72
78
  */
73
- export declare function capsForPersona(limits: LimitsConfig, persona: string, isMember: boolean): {
74
- runCap?: number;
75
- tokenCap?: number;
76
- };
79
+ export declare function capsForPersona(limits: LimitsConfig, persona: string, isMember: boolean): QuotaCaps;
77
80
  /**
78
81
  * 结算拆账(唯一算式):池先冲,冲不下的走余额。
79
82
  * 已知边界:部分覆盖时 toolCalls 服务费按全额算(computeSpendCents 的第二项不随
package/dist/authz.js CHANGED
@@ -94,7 +94,15 @@ export function capsForRoles(roles, limits) {
94
94
  const member = roles.includes("member") || roles.includes("admin");
95
95
  const runCap = member ? limits.memberDailyRuns : limits.freeDailyRuns;
96
96
  const tokenCap = member ? (limits.memberDailyTokenQuota ?? limits.dailyTokenQuota) : limits.dailyTokenQuota;
97
- return { runCap: runCap && runCap > 0 ? runCap : undefined, tokenCap };
97
+ return {
98
+ runCap: runCap && runCap > 0 ? runCap : undefined,
99
+ tokenCap,
100
+ // T3:单次 run 上界随人格生效;<=0 视为不启用(ledger 侧钳制)。
101
+ ...(limits.perRunTokenCap !== undefined && limits.perRunTokenCap > 0 ? { perRunTokenCap: limits.perRunTokenCap } : {}),
102
+ // 成本模型 CU 闸(独立于 token);<=0 视为不启用。
103
+ ...(limits.perRunCostCap !== undefined && limits.perRunCostCap > 0 ? { perRunCostCap: limits.perRunCostCap } : {}),
104
+ ...(limits.dailyCostCap !== undefined && limits.dailyCostCap > 0 ? { dailyCostCap: limits.dailyCostCap } : {}),
105
+ };
98
106
  }
99
107
  /** 某人格的生效上限 = 产品级 + `limits.personas.<id>` 覆盖(逐字段)。 */
100
108
  export function limitsForPersona(limits, persona) {
@@ -0,0 +1,114 @@
1
+ /** 平台内部记账单位:非负整数 CU。金额由展示层倍率决定,不进核心计算。 */
2
+ export type CU = number;
3
+ /** 每百万 token 的 CU 单价。 */
4
+ export interface CUTokenRates {
5
+ readonly input: CU;
6
+ readonly output: CU;
7
+ readonly cacheRead: CU;
8
+ readonly cacheWrite: CU;
9
+ }
10
+ /**
11
+ * 供应商计费模型——models.json 声明,启动期强制校验(口径 4:缺声明拒绝启动)。
12
+ *
13
+ * - per-token:四维单价,按 usage 各维度计费;
14
+ * - per-call:每次固定 cuPerCall,含 includedTokens 免费额度,超出部分按 overflow 补价(口径 2);
15
+ * - flat:整 run 一口价(订阅/包月内部结算);
16
+ * - hybrid:base(per-call 或 flat)+ tokenRates 两部分相加。
17
+ */
18
+ export type CostModel = {
19
+ readonly kind: "per-token";
20
+ readonly rates: CUTokenRates;
21
+ } | {
22
+ readonly kind: "per-call";
23
+ readonly cuPerCall: CU;
24
+ readonly includedTokens?: number;
25
+ readonly overflow: CUTokenRates;
26
+ } | {
27
+ readonly kind: "flat";
28
+ readonly cuPerRun: CU;
29
+ } | {
30
+ readonly kind: "hybrid";
31
+ readonly base: "per-call" | "flat";
32
+ readonly cuBase: CU;
33
+ readonly tokenRates: CUTokenRates;
34
+ };
35
+ /** 成本拆分:各维度 CU + 总计。 */
36
+ export interface CostBreakdown {
37
+ readonly cu: CU;
38
+ readonly byDimension: {
39
+ readonly base: CU;
40
+ readonly input: CU;
41
+ readonly output: CU;
42
+ readonly cacheRead: CU;
43
+ readonly cacheWrite: CU;
44
+ };
45
+ readonly kind: CostModel["kind"];
46
+ }
47
+ /** costOfUsage 的最小输入(与 protocol 的 TokenUsage 结构兼容,但不依赖它——保持 server 内部类型独立)。 */
48
+ export interface UsageLike {
49
+ readonly input: number;
50
+ readonly output: number;
51
+ readonly cacheRead: number;
52
+ readonly cacheWrite: number;
53
+ readonly total: number;
54
+ }
55
+ /** 平台唯一的成本口径:把任意 CostModel 的一次用量折算成 CU 拆分。 */
56
+ export declare function costOfUsage(usage: UsageLike, model: CostModel): CostBreakdown;
57
+ /** 显示单位:CU 是内部不可变整数,展示层按需换算(口径 1 的前半:显示单位可切换)。 */
58
+ export type DisplayUnit = "CU" | "US_CENTS" | "CNY_CENTS";
59
+ /** displayRate:1 CU 等于多少个目标单位(倍率可由产品配置调整——口径 1 的后半:计价基准靠改倍率)。 */
60
+ export declare function displayCu(cu: CU, unit: DisplayUnit, rate: {
61
+ US_CENTS: number;
62
+ CNY_CENTS: number;
63
+ }): number;
64
+ /**
65
+ * 从 models.json 的模型条目解析 CostModel(启动期校验用)。
66
+ * 缺 costModel 返回 null——调用方据此拒绝启动并报清晰错误(口径 4)。
67
+ */
68
+ export declare function parseCostModel(raw: unknown): CostModel | null;
69
+ /**
70
+ * 启动期校验(口径 4 · 2026-11-18 修订):逐 provider/model 检查 models.json 的 costModel 声明。
71
+ *
72
+ * - **缺声明 → 放行**(不报错):这类模型走 CostTable 的全局倍率兜底(token × rate);
73
+ * ModelScope 这类按次计费、不在权威定价表的模型因此零配置也能算成本。
74
+ * - **声明了但非法 → fail fast**:有 typo 的配置必须当场暴露(错误矩阵:输入校验失败=fatal)。
75
+ * - providers 形状不对 → fail fast(无法建立成本表)。
76
+ *
77
+ * 返回解析好的逐模型 CostModel 表(只含合法声明),供 costOfUsage 精细折算优先使用。
78
+ */
79
+ export declare function validateModelsCost(modelsJson: unknown): Map<string, CostModel>;
80
+ /**
81
+ * 成本表:按 provider/model 索引 CostModel,供 settle 把 run 的 usage 折算成 CU。
82
+ *
83
+ * 口径边界(诚实):一次 run 可能 failover 换候选、多 phase 用不同 persona/model,
84
+ * 但 usage 是 run 级聚合、拿不到每次调用的归属。这里按 run 的**起始 tier 模型**折算整笔,
85
+ * 与 T3 的 perRunTokenCap 同抽象层(run 维度)。精确分模型计费要改 pi session 每次
86
+ * 调用点记录,属另一个接缝改造——见设计稿 §6.1。
87
+ */
88
+ /** 全局倍率默认值:1 CU / 千 token,开箱即用(口径 4 修订)。 */
89
+ export declare const DEFAULT_COST_RATE = 1;
90
+ /** 全局倍率兜底:token 数 × 倍率 / 1000,向上取整到整数 CU(不白送成本)。 */
91
+ export declare function flatRateCu(usage: UsageLike, ratePerThousand: number): CU;
92
+ export declare class CostTable {
93
+ private readonly models;
94
+ private readonly ratePerThousandTokens;
95
+ /**
96
+ * @param models 逐模型精细声明(优先)
97
+ * @param ratePerThousandTokens 全局倍率兜底(无精细声明时用),默认 DEFAULT_COST_RATE
98
+ */
99
+ constructor(models: ReadonlyMap<string, CostModel>, ratePerThousandTokens?: number);
100
+ /**
101
+ * 按 provider/model 折算 CU:
102
+ * - 有合法逐模型声明 → costOfUsage 精细折算(优先);
103
+ * - 无声明 → 全局倍率兜底 token × rate(口径 4 修订,ModelScope 这类模型零配置也能算)。
104
+ */
105
+ cuFor(provider: string, model: string, usage: UsageLike): CU;
106
+ /** 该 provider/model 是否有精细声明(供测试与诊断;无声明走兜底不等于无成本)。 */
107
+ has(provider: string, model: string): boolean;
108
+ }
109
+ /**
110
+ * 启动期从 dataDir/models.json 加载并校验成本表。
111
+ * @param ratePerThousandTokens 全局倍率兜底,默认 DEFAULT_COST_RATE;由 main.ts 从 manifest 传入。
112
+ * 校验失败(providers 形状不对 / 声明了但非法)直接抛,与 createModelRuntime 同源。
113
+ */
114
+ export declare function loadCostTable(dataDir: string, ratePerThousandTokens?: number): CostTable;
@@ -0,0 +1,198 @@
1
+ /**
2
+ * 统一成本模型(M-14 / 成本模型:平台级成本口径,不绑定供应商)。
3
+ *
4
+ * 为什么要有它:平台曾有三条各说各话的成本线(pi 的 costUsd / 向用户收费的 spendCents /
5
+ * 只数 token 的额度闸),benchmark 被迫去适配"某家按次、某家按 token"——本末倒置。
6
+ * 本模块定义平台唯一的成本口径:任何供应商的计费模型都声明成 CostModel,
7
+ * 统一折算成整数 CU(内部记账单位),与供应商、币种、显示单位解耦。
8
+ *
9
+ * 核心折算纯函数零副作用;loadCostTable 读 models.json 是启动期一次性的 IO。
10
+ * 类型不进 protocol(公开 54 符号不变)。
11
+ *
12
+ * 设计稿:docs/design/统一成本模型设计稿.md(五口径已由用户 2026-11-17 拍板)。
13
+ */
14
+ import { readFileSync } from "node:fs";
15
+ /** 向上取整到整数 CU——宁可多算一 CU,不白送成本。 */
16
+ const ceilCu = (cu) => Math.max(0, Math.ceil(cu - 1e-9));
17
+ /** 按四维单价计算 token 部分;included 额度从 input 开始依次扣减(输入优先,其次输出)。 */
18
+ function tokenCu(usage, rates, included = 0) {
19
+ let remaining = Math.max(0, included);
20
+ const afterIncluded = (v) => {
21
+ const consumed = Math.min(remaining, v);
22
+ remaining -= consumed;
23
+ return v - consumed;
24
+ };
25
+ const input = afterIncluded(usage.input);
26
+ const output = afterIncluded(usage.output);
27
+ const cacheRead = afterIncluded(usage.cacheRead);
28
+ const cacheWrite = afterIncluded(usage.cacheWrite);
29
+ return {
30
+ base: 0,
31
+ input: ceilCu((input * rates.input) / 1_000_000),
32
+ output: ceilCu((output * rates.output) / 1_000_000),
33
+ cacheRead: ceilCu((cacheRead * rates.cacheRead) / 1_000_000),
34
+ cacheWrite: ceilCu((cacheWrite * rates.cacheWrite) / 1_000_000),
35
+ };
36
+ }
37
+ const sum = (d) => d.base + d.input + d.output + d.cacheRead + d.cacheWrite;
38
+ /** 平台唯一的成本口径:把任意 CostModel 的一次用量折算成 CU 拆分。 */
39
+ export function costOfUsage(usage, model) {
40
+ let by = { base: 0, input: 0, output: 0, cacheRead: 0, cacheWrite: 0 };
41
+ switch (model.kind) {
42
+ case "per-token":
43
+ by = tokenCu(usage, model.rates);
44
+ break;
45
+ case "per-call":
46
+ by = tokenCu(usage, model.overflow, model.includedTokens ?? 0);
47
+ by = { ...by, base: ceilCu(model.cuPerCall) };
48
+ break;
49
+ case "flat":
50
+ by = { ...by, base: ceilCu(model.cuPerRun) };
51
+ break;
52
+ case "hybrid":
53
+ by = tokenCu(usage, model.tokenRates);
54
+ by = { ...by, base: ceilCu(model.cuBase) };
55
+ break;
56
+ }
57
+ return { cu: sum(by), byDimension: by, kind: model.kind };
58
+ }
59
+ /** displayRate:1 CU 等于多少个目标单位(倍率可由产品配置调整——口径 1 的后半:计价基准靠改倍率)。 */
60
+ export function displayCu(cu, unit, rate) {
61
+ if (unit === "CU")
62
+ return cu;
63
+ const factor = unit === "US_CENTS" ? rate.US_CENTS : rate.CNY_CENTS;
64
+ return Math.round(cu * factor * 100) / 100;
65
+ }
66
+ /**
67
+ * 从 models.json 的模型条目解析 CostModel(启动期校验用)。
68
+ * 缺 costModel 返回 null——调用方据此拒绝启动并报清晰错误(口径 4)。
69
+ */
70
+ export function parseCostModel(raw) {
71
+ if (!raw || typeof raw !== "object")
72
+ return null;
73
+ const o = raw;
74
+ if (typeof o.kind !== "string")
75
+ return null;
76
+ switch (o.kind) {
77
+ case "per-token": {
78
+ const r = o.rates;
79
+ return isRates(r) ? { kind: "per-token", rates: r } : null;
80
+ }
81
+ case "per-call": {
82
+ if (!isCu(o.cuPerCall) || !isRates(o.overflow))
83
+ return null;
84
+ const included = o.includedTokens;
85
+ if (included !== undefined && (typeof included !== "number" || !Number.isFinite(included) || included < 0))
86
+ return null;
87
+ return { kind: "per-call", cuPerCall: o.cuPerCall, includedTokens: included, overflow: o.overflow };
88
+ }
89
+ case "flat":
90
+ return isCu(o.cuPerRun) ? { kind: "flat", cuPerRun: o.cuPerRun } : null;
91
+ case "hybrid":
92
+ if ((o.base !== "per-call" && o.base !== "flat") || !isCu(o.cuBase) || !isRates(o.tokenRates))
93
+ return null;
94
+ return { kind: "hybrid", base: o.base, cuBase: o.cuBase, tokenRates: o.tokenRates };
95
+ default:
96
+ return null;
97
+ }
98
+ }
99
+ /**
100
+ * 启动期校验(口径 4 · 2026-11-18 修订):逐 provider/model 检查 models.json 的 costModel 声明。
101
+ *
102
+ * - **缺声明 → 放行**(不报错):这类模型走 CostTable 的全局倍率兜底(token × rate);
103
+ * ModelScope 这类按次计费、不在权威定价表的模型因此零配置也能算成本。
104
+ * - **声明了但非法 → fail fast**:有 typo 的配置必须当场暴露(错误矩阵:输入校验失败=fatal)。
105
+ * - providers 形状不对 → fail fast(无法建立成本表)。
106
+ *
107
+ * 返回解析好的逐模型 CostModel 表(只含合法声明),供 costOfUsage 精细折算优先使用。
108
+ */
109
+ export function validateModelsCost(modelsJson) {
110
+ const root = modelsJson;
111
+ if (!root || typeof root !== "object" || !root.providers || typeof root.providers !== "object") {
112
+ throw new Error("[cost-model] models.json 缺少 providers 对象,无法校验成本模型声明");
113
+ }
114
+ const table = new Map();
115
+ const problems = [];
116
+ for (const [provider, cfg] of Object.entries(root.providers)) {
117
+ const models = cfg?.models;
118
+ if (!Array.isArray(models)) {
119
+ problems.push(`provider "${provider}" 没有 models 数组`);
120
+ continue;
121
+ }
122
+ for (const m of models) {
123
+ const id = String(m.id ?? "<无 id>");
124
+ if (m.costModel === undefined)
125
+ continue; // 缺声明放行 → 全局倍率兜底
126
+ const parsed = parseCostModel(m.costModel);
127
+ if (!parsed) {
128
+ problems.push(`provider "${provider}" 的 model "${id}" 的 costModel 声明非法` +
129
+ `(需形如 {kind:"per-token",rates:{input,output,cacheRead,cacheWrite}},` +
130
+ `或 {kind:"per-call",cuPerCall,includedTokens?,overflow} / {kind:"flat",cuPerRun} / {kind:"hybrid",...};CU 为非负整数)。` +
131
+ `若不声明则走全局倍率兜底,但仍建议修正 typo`);
132
+ continue;
133
+ }
134
+ table.set(`${provider}/${id}`, parsed);
135
+ }
136
+ }
137
+ if (problems.length) {
138
+ throw new Error(`[cost-model] models.json 成本模型声明校验失败(声明了但非法;未声明的模型走全局倍率兜底):\n- ${problems.join("\n- ")}`);
139
+ }
140
+ return table;
141
+ }
142
+ function isCu(v) {
143
+ return typeof v === "number" && Number.isFinite(v) && v >= 0 && Number.isInteger(v);
144
+ }
145
+ function isRates(v) {
146
+ if (!v || typeof v !== "object")
147
+ return false;
148
+ const r = v;
149
+ return isCu(r.input) && isCu(r.output) && isCu(r.cacheRead) && isCu(r.cacheWrite);
150
+ }
151
+ /**
152
+ * 成本表:按 provider/model 索引 CostModel,供 settle 把 run 的 usage 折算成 CU。
153
+ *
154
+ * 口径边界(诚实):一次 run 可能 failover 换候选、多 phase 用不同 persona/model,
155
+ * 但 usage 是 run 级聚合、拿不到每次调用的归属。这里按 run 的**起始 tier 模型**折算整笔,
156
+ * 与 T3 的 perRunTokenCap 同抽象层(run 维度)。精确分模型计费要改 pi session 每次
157
+ * 调用点记录,属另一个接缝改造——见设计稿 §6.1。
158
+ */
159
+ /** 全局倍率默认值:1 CU / 千 token,开箱即用(口径 4 修订)。 */
160
+ export const DEFAULT_COST_RATE = 1;
161
+ /** 全局倍率兜底:token 数 × 倍率 / 1000,向上取整到整数 CU(不白送成本)。 */
162
+ export function flatRateCu(usage, ratePerThousand) {
163
+ return ceilCu((usage.total / 1000) * ratePerThousand);
164
+ }
165
+ export class CostTable {
166
+ models;
167
+ ratePerThousandTokens;
168
+ /**
169
+ * @param models 逐模型精细声明(优先)
170
+ * @param ratePerThousandTokens 全局倍率兜底(无精细声明时用),默认 DEFAULT_COST_RATE
171
+ */
172
+ constructor(models, ratePerThousandTokens = DEFAULT_COST_RATE) {
173
+ this.models = models;
174
+ this.ratePerThousandTokens = ratePerThousandTokens;
175
+ }
176
+ /**
177
+ * 按 provider/model 折算 CU:
178
+ * - 有合法逐模型声明 → costOfUsage 精细折算(优先);
179
+ * - 无声明 → 全局倍率兜底 token × rate(口径 4 修订,ModelScope 这类模型零配置也能算)。
180
+ */
181
+ cuFor(provider, model, usage) {
182
+ const declared = this.models.get(`${provider}/${model}`);
183
+ return declared ? costOfUsage(usage, declared).cu : flatRateCu(usage, this.ratePerThousandTokens);
184
+ }
185
+ /** 该 provider/model 是否有精细声明(供测试与诊断;无声明走兜底不等于无成本)。 */
186
+ has(provider, model) {
187
+ return this.models.has(`${provider}/${model}`);
188
+ }
189
+ }
190
+ /**
191
+ * 启动期从 dataDir/models.json 加载并校验成本表。
192
+ * @param ratePerThousandTokens 全局倍率兜底,默认 DEFAULT_COST_RATE;由 main.ts 从 manifest 传入。
193
+ * 校验失败(providers 形状不对 / 声明了但非法)直接抛,与 createModelRuntime 同源。
194
+ */
195
+ export function loadCostTable(dataDir, ratePerThousandTokens = DEFAULT_COST_RATE) {
196
+ const raw = JSON.parse(readFileSync(`${dataDir}/models.json`, "utf8"));
197
+ return new CostTable(validateModelsCost(raw), ratePerThousandTokens);
198
+ }
package/dist/events.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { AgentSessionEvent } from "@earendil-works/pi-coding-agent";
2
2
  import type { ChatEvent, TokenUsage } from "@njuptlzf/agent-chat-protocol";
3
+ import type { UsageLike } from "./cost-model.ts";
3
4
  export declare function emptyUsage(): TokenUsage;
4
5
  /**
5
6
  * 模型报错的用户文案(2026-11-04):pi 的 errorMessage 是原始串(如
@@ -18,7 +19,7 @@ export interface EventMapper {
18
19
  * Translate pi session events into the stable wire protocol.
19
20
  * Stateful (message ids, usage totals), so create one mapper per run.
20
21
  */
21
- export declare function createEventMapper(): EventMapper;
22
+ export declare function createEventMapper(costOf?: (usage: UsageLike) => number): EventMapper;
22
23
  export interface RunAccounting {
23
24
  /** Feed each outgoing protocol event; counts completed tool rounds. */
24
25
  observe(ev: ChatEvent): void;
package/dist/events.js CHANGED
@@ -1,14 +1,28 @@
1
1
  export function emptyUsage() {
2
2
  return { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0, costUsd: 0 };
3
3
  }
4
- function toTokenUsage(u) {
4
+ /**
5
+ * costOf:把 usage 折算成平台统一成本 CU 的回调(由调用方绑定 provider/model 的 CostModel)。
6
+ * 不传 ⇒ 回退 pi 的 u.cost.total(零行为变化,兼容老部署与 trial 旁路)。
7
+ *
8
+ * 注意 TokenUsage.costUsd 这个字段名是历史遗留:传了 costOf 时装的是 CU 整数、不是美元。
9
+ * 协议注释已同步说明(见 protocol/src/index.ts)。
10
+ */
11
+ function toTokenUsage(u, costOf) {
12
+ const asUsage = {
13
+ input: u.input,
14
+ output: u.output,
15
+ cacheRead: u.cacheRead,
16
+ cacheWrite: u.cacheWrite,
17
+ total: u.totalTokens,
18
+ };
5
19
  return {
6
20
  input: u.input,
7
21
  output: u.output,
8
22
  cacheRead: u.cacheRead,
9
23
  cacheWrite: u.cacheWrite,
10
24
  total: u.totalTokens,
11
- costUsd: u.cost.total,
25
+ costUsd: costOf ? costOf(asUsage) : u.cost.total,
12
26
  };
13
27
  }
14
28
  function addUsage(a, b) {
@@ -77,7 +91,7 @@ export function friendlyModelError(raw) {
77
91
  * Translate pi session events into the stable wire protocol.
78
92
  * Stateful (message ids, usage totals), so create one mapper per run.
79
93
  */
80
- export function createEventMapper() {
94
+ export function createEventMapper(costOf) {
81
95
  let counter = 0;
82
96
  let currentMessageId = "";
83
97
  let total = emptyUsage();
@@ -106,7 +120,7 @@ export function createEventMapper() {
106
120
  if (msg.role !== "assistant")
107
121
  return [];
108
122
  const out = [];
109
- const usage = msg.usage ? toTokenUsage(msg.usage) : undefined;
123
+ const usage = msg.usage ? toTokenUsage(msg.usage, costOf) : undefined;
110
124
  if (usage)
111
125
  total = addUsage(total, usage);
112
126
  out.push({
package/dist/ledger.d.ts CHANGED
@@ -17,10 +17,10 @@
17
17
  * 崩溃安全:预留带 `expires_at`,读路径一律只认未过期的预留。
18
18
  * 进程崩了不会永久占着额度——不需要「启动时清理孤儿」这种容易漏的逻辑。
19
19
  *
20
- * **明确的能力边界**:跑的次数(run 位)在并发下是精确的;**token 上限做不到精确**,
21
- * 因为一次 run 会花多少 token 事前不可知。reserve 只能保证「已达上限就不再开新 run」,
22
- * 在飞的那几个仍可能把它顶过去。要精确,得先有「单次 run 的 token 上限」这一配置
23
- * (见架构稿 §6.1 待办 T3)。
20
+ * **能力边界**:跑的次数(run 位)在并发下恒精确。token 总量的精确性取决于是否配置
21
+ * `caps.perRunTokenCap`(T3):配了 ⇒ reserve 按上界占位、在飞 run 计入总量判定,
22
+ * `used + Σ在飞上界 <= tokenCap` 恒成立(正常范围内);没配 ⇒ 预留 token 恒 0,
23
+ * 退化为 W3 现状(只数已结算,在飞 run 可能把当日总量顶过去)。配置缺省零行为变化。
24
24
  */
25
25
  import { type QuotaCaps } from "./stores.ts";
26
26
  /** 一次预留的句柄。`operationId` 由调用方生成(一请求一个),是幂等键。 */
@@ -52,6 +52,8 @@ export interface SettleInput {
52
52
  readonly persona: string;
53
53
  /** 实际消耗的 token;`0` 也照常收口(只释放占位)。 */
54
54
  readonly tokens: number;
55
+ /** 实际消耗的 CU(成本模型独立闸);缺省 0 = 不启用 CU 计量。 */
56
+ readonly cu?: number;
55
57
  }
56
58
  export type SettleOutcome = "settled" | "already";
57
59
  export interface Ledger {
@@ -75,6 +77,7 @@ export declare class MemoryLedger implements Ledger {
75
77
  private readonly now;
76
78
  private readonly rows;
77
79
  private readonly used;
80
+ private readonly usedCu;
78
81
  private readonly runs;
79
82
  constructor(dailyTokens: number | undefined, now?: () => number);
80
83
  private static day;
package/dist/ledger.js CHANGED
@@ -17,12 +17,12 @@
17
17
  * 崩溃安全:预留带 `expires_at`,读路径一律只认未过期的预留。
18
18
  * 进程崩了不会永久占着额度——不需要「启动时清理孤儿」这种容易漏的逻辑。
19
19
  *
20
- * **明确的能力边界**:跑的次数(run 位)在并发下是精确的;**token 上限做不到精确**,
21
- * 因为一次 run 会花多少 token 事前不可知。reserve 只能保证「已达上限就不再开新 run」,
22
- * 在飞的那几个仍可能把它顶过去。要精确,得先有「单次 run 的 token 上限」这一配置
23
- * (见架构稿 §6.1 待办 T3)。
20
+ * **能力边界**:跑的次数(run 位)在并发下恒精确。token 总量的精确性取决于是否配置
21
+ * `caps.perRunTokenCap`(T3):配了 ⇒ reserve 按上界占位、在飞 run 计入总量判定,
22
+ * `used + Σ在飞上界 <= tokenCap` 恒成立(正常范围内);没配 ⇒ 预留 token 恒 0,
23
+ * 退化为 W3 现状(只数已结算,在飞 run 可能把当日总量顶过去)。配置缺省零行为变化。
24
24
  */
25
- import { quotaBlockedReason } from "./stores.js";
25
+ import { quotaBlockedCostReason, quotaBlockedReason } from "./stores.js";
26
26
  /** 预留默认存活时间:1 小时。 */
27
27
  export const RESERVATION_TTL_MS = 60 * 60 * 1000;
28
28
  /** 进程内实现(开发态 / 测试)。并发语义与 SQLite 版一致,只是没有持久化。 */
@@ -31,6 +31,7 @@ export class MemoryLedger {
31
31
  now;
32
32
  rows = new Map();
33
33
  used = new Map();
34
+ usedCu = new Map();
34
35
  runs = new Map();
35
36
  constructor(dailyTokens, now = () => Date.now()) {
36
37
  this.dailyTokens = dailyTokens;
@@ -66,8 +67,35 @@ export class MemoryLedger {
66
67
  }
67
68
  }
68
69
  const tokenCap = input.caps?.tokenCap ?? this.dailyTokens;
69
- if (tokenCap !== undefined && (this.used.get(key) ?? 0) >= tokenCap) {
70
- return { ok: false, reason: quotaBlockedReason("tokens", { runCap, tokenCap }) };
70
+ let reserveTokens = 0;
71
+ if (tokenCap !== undefined) {
72
+ const used = this.used.get(key) ?? 0;
73
+ // T3:在飞预留的 token 上界参与判定(W3 这里只数已结算的 used)。
74
+ const reserved = [...this.rows.values()]
75
+ .filter((r) => r.day === day && r.state === "reserved" && r.expiresAt > now)
76
+ .reduce((a, r) => a + r.tokens, 0);
77
+ if (used + reserved >= tokenCap) {
78
+ return { ok: false, reason: quotaBlockedReason("tokens", { runCap, tokenCap }) };
79
+ }
80
+ // 钳制:上界不超过当日剩余量,保证 "used + reserved <= tokenCap" 不变量。
81
+ const cap = input.caps?.perRunTokenCap;
82
+ if (cap !== undefined && cap > 0)
83
+ reserveTokens = Math.min(cap, tokenCap - used - reserved);
84
+ }
85
+ // 成本模型 CU 闸(独立于 token):同样"已结算 + 在飞上界"判定,任一维触顶即拒。
86
+ let reserveCu = 0;
87
+ const dailyCostCap = input.caps?.dailyCostCap;
88
+ if (dailyCostCap !== undefined && dailyCostCap > 0) {
89
+ const usedCu = this.usedCu.get(key) ?? 0;
90
+ const reservedCu = [...this.rows.values()]
91
+ .filter((r) => r.day === day && r.state === "reserved" && r.expiresAt > now)
92
+ .reduce((a, r) => a + r.costUnits, 0);
93
+ if (usedCu + reservedCu >= dailyCostCap) {
94
+ return { ok: false, reason: quotaBlockedCostReason(input.caps ?? {}) };
95
+ }
96
+ const cap = input.caps?.perRunCostCap;
97
+ if (cap !== undefined && cap > 0)
98
+ reserveCu = Math.min(cap, dailyCostCap - usedCu - reservedCu);
71
99
  }
72
100
  const expiresAt = now + (input.ttlMs ?? RESERVATION_TTL_MS);
73
101
  this.rows.set(input.operationId, {
@@ -76,6 +104,8 @@ export class MemoryLedger {
76
104
  day,
77
105
  state: "reserved",
78
106
  expiresAt,
107
+ tokens: reserveTokens,
108
+ costUnits: reserveCu,
79
109
  });
80
110
  return { ok: true, reservation: { operationId: input.operationId, expiresAt } };
81
111
  }
@@ -86,9 +116,14 @@ export class MemoryLedger {
86
116
  if (!row || row.state !== "reserved")
87
117
  return "already";
88
118
  row.state = "settled";
119
+ // 多退少补:把占位的上界改写为实际消耗(未用部分释放给后续 run)。
120
+ row.tokens = input.tokens;
121
+ row.costUnits = input.cu ?? 0;
89
122
  const key = MemoryLedger.key(input.userId, input.persona, MemoryLedger.day(this.now()));
90
123
  if (input.tokens > 0)
91
124
  this.used.set(key, (this.used.get(key) ?? 0) + input.tokens);
125
+ if (input.cu && input.cu > 0)
126
+ this.usedCu.set(key, (this.usedCu.get(key) ?? 0) + input.cu);
92
127
  this.runs.set(key, (this.runs.get(key) ?? 0) + 1);
93
128
  return "settled";
94
129
  }
package/dist/main.js CHANGED
@@ -7,6 +7,7 @@ import { createMcpConfigStore } from "./mcp-config.js";
7
7
  import { createPersonaConfigStore } from "./persona-config.js";
8
8
  import { initProductRoutes, loadProductAssembly, loadProductRoutes } from "./product-routes.js";
9
9
  import { createChatServer } from "./server.js";
10
+ import { loadCostTable } from "./cost-model.js";
10
11
  import { describeComponents } from "./components.js";
11
12
  import { createModelRuntime } from "./session.js";
12
13
  import { createAdminAuth, createJwtAuth, insecureHeaderAuth } from "./stores.js";
@@ -102,6 +103,9 @@ const deps = {
102
103
  toolFactory,
103
104
  history: new SqliteHistoryStore(db),
104
105
  ledger: new SqliteLedger(db, product.manifest.agent.limits?.dailyTokenQuota),
106
+ // 统一成本模型:启动期已校验 models.json 声明,这里装配成本表供 settle 折算 CU。
107
+ // 全局倍率兜底(无逐模型声明时用)来自 manifest,默认 1 CU/千 token。
108
+ costTable: loadCostTable(dataDir, product.manifest.agent.limits?.costRatePerThousandTokens),
105
109
  authenticate: jwtSecret ? createJwtAuth(jwtSecret) : insecureHeaderAuth,
106
110
  dataDir,
107
111
  runLog: new SqliteRunLog(db),
@@ -7,8 +7,18 @@ export interface LoadedProduct {
7
7
  systemPromptText: string;
8
8
  skillDirs: string[];
9
9
  extensionPaths: string[];
10
- /** `agent.memory` (default true): scope the scopedMemory plugin per session (). */
10
+ /**
11
+ * `agent.memory` (default true): which memory provider this product mounts.
12
+ * Resolved here so the server only ever passes an id to the registry.
13
+ */
11
14
  memoryEnabled: boolean;
15
+ /** Provider id from `agent.memory.provider`; falls back to the platform default. */
16
+ memoryProviderId: string;
17
+ /**
18
+ * 外部提供方模块(`agent.memoryProviders`),原样保留声明顺序。
19
+ * 加载在会话创建前完成——解析与准入扫描要读文件,manifest 解析是同步的。
20
+ */
21
+ memoryProviderModules: string[];
12
22
  /**
13
23
  * Question-bank directories keyed by qualification id (V0). A product with no
14
24
  * `quals` gets the single conventional `<product>/questions` under the empty
package/dist/manifest.js CHANGED
@@ -14,6 +14,7 @@ import { DEFAULT_PERSONA_ID, PERSONA_UI_FIELDS } from "@njuptlzf/agent-chat-prot
14
14
  import { FOLLOW_UP_MODES, parseBrandingUi } from "./branding-ui.js";
15
15
  import { auditExtensionSource } from "./extension-admission.js";
16
16
  import { validateFlows } from "./flows.js";
17
+ import { DEFAULT_MEMORY_PROVIDER_ID } from "./memory-providers.js";
17
18
  const THINKING_LEVELS = ["off", "minimal", "low", "medium", "high"];
18
19
  const TIER_ID = /^[a-z0-9][a-z0-9-]{0,31}$/;
19
20
  const QUAL_LEVELS = ["junior", "middle", "senior"];
@@ -313,6 +314,10 @@ export function loadProduct(dir) {
313
314
  if (maxRounds !== undefined && (!Number.isInteger(maxRounds) || maxRounds < 1)) {
314
315
  fail(file, "`agent.limits.maxToolRounds` must be a positive integer");
315
316
  }
317
+ const costRate = manifest.agent.limits?.costRatePerThousandTokens;
318
+ if (costRate !== undefined && (typeof costRate !== "number" || !Number.isFinite(costRate) || costRate <= 0)) {
319
+ fail(file, "`agent.limits.costRatePerThousandTokens` must be a positive number (CU per 1k tokens)");
320
+ }
316
321
  // Extensions execute in-process with full privileges, so a malformed entry is a
317
322
  // startup error: the operator must see a typo, not a silently skipped module.
318
323
  // Entries must be files: pi resolves an additional path that is a directory by
@@ -336,8 +341,22 @@ export function loadProduct(dir) {
336
341
  }
337
342
  }
338
343
  }
339
- if (manifest.agent.memory !== undefined && typeof manifest.agent.memory !== "boolean") {
340
- fail(file, "`agent.memory` must be a boolean");
344
+ if (typeof manifest.agent.memory !== "boolean" && manifest.agent.memory !== undefined) {
345
+ const memory = manifest.agent.memory;
346
+ if (typeof memory !== "object" || memory === null || Array.isArray(memory)) {
347
+ fail(file, "`agent.memory` must be a boolean or `{ provider }`");
348
+ }
349
+ if (memory.provider !== undefined && (typeof memory.provider !== "string" || memory.provider.trim() === "")) {
350
+ fail(file, "`agent.memory.provider` must be a non-empty string");
351
+ }
352
+ }
353
+ if (manifest.agent.memoryProviders !== undefined) {
354
+ const modules = manifest.agent.memoryProviders;
355
+ if (!Array.isArray(modules) || modules.length === 0 || !modules.every((p) => typeof p === "string" && p.trim() !== "")) {
356
+ fail(file, "`agent.memoryProviders` must be a non-empty list of non-empty strings");
357
+ }
358
+ // 只校验形状:条目可能是包名(靠依赖树解析),存在性与准入扫描留给真正加载它的
359
+ // 那一刻——报错信息里会带上解析后的绝对路径,比在这里猜准得多。
341
360
  }
342
361
  loadTiers(file, manifest);
343
362
  loadQuals(file, manifest);
@@ -482,6 +501,10 @@ export function loadProduct(dir) {
482
501
  skillDirs,
483
502
  extensionPaths: (manifest.agent.extensions ?? []).map(resolve),
484
503
  memoryEnabled: manifest.agent.memory !== false,
504
+ memoryProviderId: typeof manifest.agent.memory === "object" && manifest.agent.memory !== null
505
+ ? manifest.agent.memory.provider ?? DEFAULT_MEMORY_PROVIDER_ID
506
+ : DEFAULT_MEMORY_PROVIDER_ID,
507
+ memoryProviderModules: [...(manifest.agent.memoryProviders ?? [])],
485
508
  questionDirs,
486
509
  caseDirs,
487
510
  essayDirs,