@kangyishuai/dsh-advisor 0.3.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/README.md +45 -37
- package/README.zh-CN.md +44 -36
- package/lib/budget.js +10 -4
- package/lib/catalog.js +35 -8
- package/lib/client.js +276 -28
- package/lib/guard.js +22 -8
- package/lib/ledger.js +19 -4
- package/lib/money.js +74 -0
- package/lib/service.js +127 -15
- package/lib/settings.js +45 -4
- package/locale/en.json +22 -4
- package/locale/zh.json +22 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -55,46 +55,54 @@ Settings live in one JSON file, `<DSH_HOME>/advisor.settings.json` (`DSH_HOME` d
|
|
|
55
55
|
|
|
56
56
|
**Settings UI.** Settings → General → Advisor. With the default `off`, the card shows only the mode, a one-line status and “Configure advisor”. Expanding or enabling it reveals:
|
|
57
57
|
|
|
58
|
-
- **Basics**: mode, provider, model, reasoning effort, daily budget, send scope, redaction.
|
|
59
|
-
- **Advanced** (folded by default): call limits, workspace limits, prices, output/input caps, advice-item cap, allowlist, auto-review, file reading, shadow dedupe, sent-content recording. The heading shows “Defaults” or “N changed”.
|
|
58
|
+
- **Basics**: mode, currency, provider, model, reasoning effort, daily budget, send scope, redaction.
|
|
59
|
+
- **Advanced** (folded by default): call limits, workspace limits, CNY-per-USD rate, prices (including cache read/write), output/input caps, advice-item cap, allowlist, auto-review, file reading, shadow dedupe, sent-content recording. The heading shows “Defaults” or “N changed”.
|
|
60
60
|
- **Status & diagnostics** (folded by default): calls/tokens/cost, last result/skip reason, send preview, diagnostics, settings and ledger paths.
|
|
61
61
|
|
|
62
62
|
The “?” next to an advanced field expands an inline help block (click or activate with the keyboard) with the description, default/range and technical key; hover is only a supplement. Click **Save** to apply changes to the next consultation.
|
|
63
63
|
|
|
64
64
|
Money fields accept decimals: budgets up to 2, unit prices up to 4.
|
|
65
65
|
|
|
66
|
-
| Key
|
|
67
|
-
|
|
|
68
|
-
| `mode`
|
|
69
|
-
| `provider`
|
|
70
|
-
| `model`
|
|
71
|
-
| `reasoningEffort`
|
|
72
|
-
| `maxTokens`
|
|
73
|
-
| `temperature`
|
|
74
|
-
| `maxCallsPerTurn`
|
|
75
|
-
| `maxCallsPerSession`
|
|
76
|
-
| `maxCallsPerHour`
|
|
77
|
-
| `
|
|
78
|
-
| `
|
|
79
|
-
| `
|
|
80
|
-
| `
|
|
81
|
-
| `
|
|
82
|
-
| `
|
|
83
|
-
| `
|
|
84
|
-
| `
|
|
85
|
-
| `
|
|
86
|
-
| `
|
|
87
|
-
| `
|
|
88
|
-
| `
|
|
89
|
-
| `
|
|
90
|
-
| `
|
|
91
|
-
| `
|
|
92
|
-
| `
|
|
93
|
-
| `
|
|
94
|
-
| `
|
|
95
|
-
| `
|
|
96
|
-
|
|
97
|
-
|
|
66
|
+
| Key | UI label | Default | Range / values | Meaning |
|
|
67
|
+
| ------------------------- | ------------------------------ | ----------- | --------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
|
|
68
|
+
| `mode` | Mode | `"off"` | off / shadow / advisory | Whether the plugin may consult at all. Off by default; opt in explicitly. |
|
|
69
|
+
| `provider` | Provider | `""` | route name from Settings → Models, ≤300 chars | Advisor route. Empty means never call. |
|
|
70
|
+
| `model` | Model | `""` | model id under that route, ≤300 chars | Advisor model. Empty means never call. |
|
|
71
|
+
| `reasoningEffort` | Reasoning effort | `""` | a supported effort id, `""` = model default | An unsupported value is retried once without an effort. |
|
|
72
|
+
| `maxTokens` | Output limit | `4096` | 256–32000 | Maximum output tokens per consultation. |
|
|
73
|
+
| `temperature` | Randomness | `null` | 0–2, `null` = not sent | Empty lets the model decide. |
|
|
74
|
+
| `maxCallsPerTurn` | Calls per turn | `1` | 0–8, 0 = unlimited | Maximum consultations per turn. |
|
|
75
|
+
| `maxCallsPerSession` | Calls per session | `32` | 0–1000, 0 = unlimited | Maximum consultations per session. |
|
|
76
|
+
| `maxCallsPerHour` | Calls per hour | `12` | 0–500, 0 = unlimited | Sliding one-hour window. |
|
|
77
|
+
| `currency` | Currency | `"USD"` | USD / CNY | Amounts and prices are entered in this currency; the ledger/pool stay in USD internally. |
|
|
78
|
+
| `usdToCny` | CNY per USD | `7.2` | 0.1–100 | 1 USD = ? CNY; used only for CNY conversion; manual, never fetched. |
|
|
79
|
+
| `maxCostPerDay` | Daily budget | `0` | 0–10000, 0 = unlimited; up to 2 decimals | Daily spend ceiling in the selected currency; only counted when prices are set. |
|
|
80
|
+
| `priceInputPerMTok` | Input price | `0` | 0–1000; up to 4 decimals | Advisor input price in the selected currency per 1M tokens; 0 = do not price. |
|
|
81
|
+
| `priceOutputPerMTok` | Output price | `0` | 0–1000; up to 4 decimals | Output price in the selected currency per 1M tokens. |
|
|
82
|
+
| `priceCacheReadPerMTok` | Cache-read price | `0` | 0–1000; up to 4 decimals | Input-token price for cache reads/hits in the selected currency; 0 = do not price cache tokens. |
|
|
83
|
+
| `priceCacheWritePerMTok` | Cache-write price | `0` | 0–1000; up to 4 decimals | Input-token price for cache writes/creations in the selected currency; 0 = do not price cache tokens. |
|
|
84
|
+
| `privacyScope` | Send scope | `"summary"` | off / summary / full | How much conversation content is sent — see Cost & privacy. |
|
|
85
|
+
| `redact` | Redact before sending | `true` | boolean | Best-effort masking of common key formats; not a guarantee. |
|
|
86
|
+
| `immuneTurns` | JSON only: Immune turns | `3` | 0–20 | Turns to stay quiet after a delivery; used by shadow delivery. |
|
|
87
|
+
| `dedupe` | Dedupe in shadow mode | `true` | boolean | In shadow mode, the same advice is recorded once; the tool path does not block on it. |
|
|
88
|
+
| `sessionCap` | Advice items per session | `24` | 0–200 | Max advice items shown per session; once reached only blockers get through. |
|
|
89
|
+
| `tiers` | JSON only: Tiers | `{}` | `"provider/model"` → number | Capability ranks for the pairing check; without an entry the pairing is unverified. |
|
|
90
|
+
| `allowedModels` | Model allowlist | `[]` | string array | Empty = no limit; accepts provider/model or a bare model id. |
|
|
91
|
+
| `confirmedHosts` | JSON/button: Confirmed hosts | `[]` | string array | Cross-provider confirmation list; the settings-card button writes it. |
|
|
92
|
+
| `pairingEnforcement` | JSON only: Pairing enforcement | `"warn"` | warn / block | When the advisor tier is lower: warn allows and warns; block refuses. |
|
|
93
|
+
| `workspaceMaxCallsPerDay` | Workspace calls per day | `200` | 0–100000, 0 = unlimited | Workspace-wide, shared across sessions, resets on the local day. |
|
|
94
|
+
| `workspaceMaxCostPerDay` | Workspace daily budget | `0` | 0–10000, 0 = unlimited; up to 2 decimals | Workspace-wide daily ceiling in the selected currency. |
|
|
95
|
+
| `postAnswer` | Auto-review after each turn | `"off"` | off / card | In advisory mode, runs once after each answer; records only, never inserts. |
|
|
96
|
+
| `advisorTools` | Let the advisor read files | `false` | boolean | Lets it read working-directory files (up to 5, read-only); costs one extra call. |
|
|
97
|
+
| `tracePrompt` | Keep sent content | `"preview"` | `preview` / `off` | Stores the first/last 2,000 chars of the text actually sent (after redaction) in the session record; `off` stores metadata only. |
|
|
98
|
+
| `timeoutMs` | JSON only: Timeout | `90000` | 5000–600000 | Per-consultation timeout; `/advisor cancel` aborts sooner. |
|
|
99
|
+
| `maxInputChars` | Input limit | `60000` | 4000–400000 | Total transcript budget; excess in the middle is elided, head/tail kept. |
|
|
100
|
+
|
|
101
|
+
**Cache prices.** The host usage exposes `cacheReadTokens` / `cacheWriteTokens`, both input-side; there is no cache-output token. Cache tokens are additional input tokens and are not subtracted from `inputTokens`. `priceCacheReadPerMTok` / `priceCacheWritePerMTok` = 0 means cache tokens are not priced; if the ledger has cache tokens while both are 0, the status area warns that cache tokens are unpriced. Built-in reference prices have no cache prices and the plugin does not guess.
|
|
102
|
+
|
|
103
|
+
**Currency and ledger.** Amounts and prices in settings are entered in `currency` (USD/CNY); `usdToCny` is a manual rate. The ledger and workspace pool always store USD: limits/prices are converted to USD at call time and displayed back in the selected currency. Switching currency converts the form amounts at the rate before saving; old `maxCostPerDayUsd` / `workspaceMaxCostPerDayUsd` keys migrate as USD to the new keys. A rate change affects future conversion/display, not the historical ledger.
|
|
104
|
+
|
|
105
|
+
> **Note on `sessionCap`, `dedupe`, `immuneTurns`:** `sessionCap` is enforced — after that many delivered items in one session only `blocker` advice is still returned. `dedupe` and `immuneTurns` are persisted and used by the shadow path, but the tool path does not suppress a consultation because of them (you asked for it explicitly). The ceilings that stop _calls_ altogether are `maxCallsPerTurn` / `maxCallsPerSession` / `maxCallsPerHour` / `maxCostPerDay`.
|
|
98
106
|
|
|
99
107
|
Example (placeholder route and model names — use the ones the picker shows you):
|
|
100
108
|
|
|
@@ -110,7 +118,7 @@ Example (placeholder route and model names — use the ones the picker shows you
|
|
|
110
118
|
|
|
111
119
|
**Two-pass evidence (opt-in).** With `advisorTools: true` the advisor may answer with `needs: [{kind:"file", ref:"path"}]`. The plugin reads those files **inside the session working directory only** (≤5 files, ≤64 KiB each, `../` and symlink escapes rejected), then calls the advisor once more with the quoted evidence so it can revise. This is the "advisor can check the facts" mode; it costs one extra model call. A rejected or unreadable path is reported back as unavailable evidence instead of failing the consultation.
|
|
112
120
|
|
|
113
|
-
**Workspace budget.** Besides the per-session ceilings, a shared pool (`<DSH_HOME>/advisor/budget.json`) counts calls and estimated cost per local day across sessions (`workspaceMaxCallsPerDay`, `
|
|
121
|
+
**Workspace budget.** Besides the per-session ceilings, a shared pool (`<DSH_HOME>/advisor/budget.json`) counts calls and estimated cost per local day across sessions (`workspaceMaxCallsPerDay`, `workspaceMaxCostPerDay`).
|
|
114
122
|
|
|
115
123
|
**Evidence anchors.** Each advice item may cite `{kind, ref}` anchors (a tool-call id, a message id, a file path). The plugin checks every ref against the session and marks the item unverified when nothing matches; if **none** of the cited anchors exist, the severity is downgraded one step and the advice says so. This is how a hallucinated "that failing command" gets caught.
|
|
116
124
|
|
|
@@ -143,8 +151,8 @@ The `advisor` **tool** is separate from the command: the model calls it, not you
|
|
|
143
151
|
This is the section to read before turning it on.
|
|
144
152
|
|
|
145
153
|
- **Every consultation is an extra model call**, billed by the advisor route against that provider's quota. The main model's own usage is unaffected.
|
|
146
|
-
- **Token accounting is ours.** DSH records usage only for the main loop's assistant messages, so the plugin writes its own ledger: `<DSH_HOME>/advisor/usage.jsonl`, one JSON object per consultation (time, session id, trigger `tool` / `shadow` / `manual`, provider, model, effort, verdict, severity, input/output/cache tokens, estimated cost, duration, error code). The file is append-only, rolls over to `usage.jsonl.1` at 2 MiB, and totals are restored from its tail on startup.
|
|
147
|
-
- **Money needs your prices.** DSH's model metadata has no price field. Set `priceInputPerMTok` and `
|
|
154
|
+
- **Token accounting is ours.** DSH records usage only for the main loop's assistant messages, so the plugin writes its own ledger: `<DSH_HOME>/advisor/usage.jsonl`, one JSON object per consultation (time, session id, trigger `tool` / `shadow` / `manual`, provider, model, effort, verdict, severity, input/output/cache-read/cache-write tokens, estimated cost, duration, error code). The file is append-only, rolls over to `usage.jsonl.1` at 2 MiB, and totals are restored from its tail on startup.
|
|
155
|
+
- **Money needs your prices.** DSH's model metadata has no price field. Set `priceInputPerMTok`, `priceOutputPerMTok`, `priceCacheReadPerMTok` and `priceCacheWritePerMTok` in the selected `currency`; with zeros the ledger records tokens only and no money amount is shown. The ledger stores the USD equivalent; built-in reference prices are USD and are converted for CNY display, but there are no built-in cache prices. The plugin never guesses prices.
|
|
148
156
|
- **The daily budget counts spend already recorded**, so the one consultation that crosses the ceiling still completes and is charged.
|
|
149
157
|
- **Conversation content leaves the machine when the advisor route is external.** The transcript — with tool calls and results, bounded by `privacyScope` and `maxInputChars` — is sent to whatever base URL that route points at. If that is another vendor, your conversation leaves this machine. Only a route pointing at a local gateway keeps it local. The plugin sends nothing else: no telemetry, no analytics, no network access besides the consultation itself.
|
|
150
158
|
- **`privacyScope` decides how much is sent.** `summary` (default): each text block up to 2,000 characters, tool arguments up to 300, tool results up to 700. `off`: user and assistant text only, up to 1,200 characters, with no tool traffic at all. `full`: text up to 4,000, tool arguments up to 1,200, tool results up to 3,500. The rendered transcript is then capped at `maxInputChars` (60,000 by default): the first 30% and the tail are kept and the middle is replaced by an elision marker.
|
package/README.zh-CN.md
CHANGED
|
@@ -55,46 +55,54 @@ dsh plugin --profile desktop add /tmp/kangyishuai-dsh-advisor-*.tgz
|
|
|
55
55
|
|
|
56
56
|
**设置页。** 设置 → 通用 → 顾问。默认 `off` 时卡片只显示模式、一句状态和「配置顾问」;展开或启用后才显示:
|
|
57
57
|
|
|
58
|
-
-
|
|
59
|
-
-
|
|
58
|
+
- **基础**:模式、币种、服务商、模型、思考档、每日预算、发送范围、发送前脱敏。
|
|
59
|
+
- **高级**(默认折叠):调用限额、工作区限额、人民币汇率、单价(含缓存读取/写入)、输出/输入上限、建议条数、白名单、答后复查、读文件核实、影子去重、记录发送内容;折叠标题会标出「默认值」或「N 项已修改」。
|
|
60
60
|
- **状态与诊断**(默认折叠):累计调用/token/金额、最近结果/跳过原因、发送预览、诊断、设置文件与账本路径。
|
|
61
61
|
|
|
62
62
|
高级字段旁的「?」点开或用键盘激活会显示说明、默认值与范围、技术名;悬停只是补充。改完点「保存」才对下一次徵询生效。
|
|
63
63
|
|
|
64
64
|
金额字段支持小数:预算最多 2 位,单价最多 4 位。
|
|
65
65
|
|
|
66
|
-
| 键
|
|
67
|
-
|
|
|
68
|
-
| `mode`
|
|
69
|
-
| `provider`
|
|
70
|
-
| `model`
|
|
71
|
-
| `reasoningEffort`
|
|
72
|
-
| `maxTokens`
|
|
73
|
-
| `temperature`
|
|
74
|
-
| `maxCallsPerTurn`
|
|
75
|
-
| `maxCallsPerSession`
|
|
76
|
-
| `maxCallsPerHour`
|
|
77
|
-
| `
|
|
78
|
-
| `
|
|
79
|
-
| `
|
|
80
|
-
| `
|
|
81
|
-
| `
|
|
82
|
-
| `
|
|
83
|
-
| `
|
|
84
|
-
| `
|
|
85
|
-
| `
|
|
86
|
-
| `
|
|
87
|
-
| `
|
|
88
|
-
| `
|
|
89
|
-
| `
|
|
90
|
-
| `
|
|
91
|
-
| `
|
|
92
|
-
| `
|
|
93
|
-
| `
|
|
94
|
-
| `
|
|
95
|
-
| `
|
|
96
|
-
|
|
97
|
-
|
|
66
|
+
| 键 | 设置页名称 | 默认 | 范围 / 取值 | 说明 |
|
|
67
|
+
| ------------------------- | ----------------------------- | ----------- | ------------------------------------ | ----------------------------------------------------------------------------- |
|
|
68
|
+
| `mode` | 模式 | `"off"` | off / shadow / advisory | 是否允许徵询。默认关闭,显式开启才生效。 |
|
|
69
|
+
| `provider` | 服务商 | `""` | 设置 → 模型里的路由名,≤300 字符 | 顾问路由。留空 = 永不调用。 |
|
|
70
|
+
| `model` | 模型 | `""` | 该路由下的模型 id,≤300 字符 | 顾问模型。留空 = 永不调用。 |
|
|
71
|
+
| `reasoningEffort` | 思考档 | `""` | 模型支持的思考档 id,`""` = 模型默认 | 填了不支持的值会去掉思考档重试一次。 |
|
|
72
|
+
| `maxTokens` | 输出上限 | `4096` | 256–32000 | 单次徵询最多生成多少 token。 |
|
|
73
|
+
| `temperature` | 随机性 | `null` | 0–2,`null` = 不传 | 留空由模型决定。 |
|
|
74
|
+
| `maxCallsPerTurn` | 每回合调用上限 | `1` | 0–8,0 = 不限 | 每回合最多徵询几次。 |
|
|
75
|
+
| `maxCallsPerSession` | 每会话调用上限 | `32` | 0–1000,0 = 不限 | 每个会话最多徵询几次。 |
|
|
76
|
+
| `maxCallsPerHour` | 每小时调用上限 | `12` | 0–500,0 = 不限 | 滚动一小时内。 |
|
|
77
|
+
| `currency` | 币种 | `"USD"` | USD / CNY | 金额和单价按此币种填写;账本/预算池内部仍记 USD。 |
|
|
78
|
+
| `usdToCny` | 人民币汇率 | `7.2` | 0.1–100 | 1 USD = ? CNY;只在人民币时用于换算;手动维护,不联网。 |
|
|
79
|
+
| `maxCostPerDay` | 每日预算(按币种) | `0` | 0–10000,0 = 不限;最多 2 位小数 | 每天最多花多少(按 `currency`);只有填了单价才计算金额。 |
|
|
80
|
+
| `priceInputPerMTok` | 输入单价 | `0` | 0–1000;最多 4 位小数 | 顾问模型输入价,按 `currency` / 百万 token;0 = 不算钱。 |
|
|
81
|
+
| `priceOutputPerMTok` | 输出单价 | `0` | 0–1000;最多 4 位小数 | 输出价,按 `currency` / 百万 token。 |
|
|
82
|
+
| `priceCacheReadPerMTok` | 缓存读取单价 | `0` | 0–1000;最多 4 位小数 | 缓存读取/命中的输入 token 单价,按 `currency`;0 = 不计缓存部分。 |
|
|
83
|
+
| `priceCacheWritePerMTok` | 缓存写入单价 | `0` | 0–1000;最多 4 位小数 | 缓存写入/创建的输入 token 单价,按 `currency`;0 = 不计缓存部分。 |
|
|
84
|
+
| `privacyScope` | 发送范围 | `"summary"` | off / summary / full | 发多少会话内容,见「成本与隐私」。 |
|
|
85
|
+
| `redact` | 发送前脱敏 | `true` | 布尔 | 尽量遮掉常见密钥格式;不能保证全部遮住。 |
|
|
86
|
+
| `immuneTurns` | 仅 JSON:免疫回合 | `3` | 0–20 | 投递后保持安静的回合数;只用于 shadow 投递。 |
|
|
87
|
+
| `dedupe` | 影子模式去重 | `true` | 布尔 | 影子模式下相同建议只记一次;工具路径不据此拦截。 |
|
|
88
|
+
| `sessionCap` | 建议条数上限 | `24` | 0–200 | 单会话最多展示几条建议;到顶后只有 blocker 继续。 |
|
|
89
|
+
| `tiers` | 仅 JSON:能力档位 | `{}` | `"provider/model"` → 数字 | 配对校验用的档位;没有条目时结论是「未校验」。 |
|
|
90
|
+
| `allowedModels` | 模型白名单 | `[]` | 字符串数组 | 留空 = 不限制;可写 provider/model 或裸 model id。 |
|
|
91
|
+
| `confirmedHosts` | 仅 JSON/按钮:已确认 provider | `[]` | 字符串数组 | 跨 provider 发送的确认名单;设置卡里的按钮会写入。 |
|
|
92
|
+
| `pairingEnforcement` | 仅 JSON:配对校验 | `"warn"` | warn / block | 顾问档位低于主模型时:warn 放行并告警,block 拒绝。 |
|
|
93
|
+
| `workspaceMaxCallsPerDay` | 工作区每日调用上限 | `200` | 0–100000,0 = 不限 | 工作区级,跨会话累计,按本地日重置。 |
|
|
94
|
+
| `workspaceMaxCostPerDay` | 工作区每日预算(按币种) | `0` | 0–10000,0 = 不限;最多 2 位小数 | 工作区级每日金额上限(按 `currency`)。 |
|
|
95
|
+
| `postAnswer` | 回合结束后自动复查 | `"off"` | off / card | advisory 模式下每轮回答后自动复查一次;只记录,不插回对话。 |
|
|
96
|
+
| `advisorTools` | 允许顾问读文件核实 | `false` | 布尔 | 允许顾问读工作目录内的文件(≤5 个、只读);多一次调用。 |
|
|
97
|
+
| `tracePrompt` | 记录发送内容 | `"preview"` | `preview` / `off` | 在会话记录里保留实际发送文本的首尾各 2,000 字符(脱敏后);`off` 只记元数据。 |
|
|
98
|
+
| `timeoutMs` | 仅 JSON:超时 | `90000` | 5000–600000 | 单次徵询超时;`/advisor cancel` 可以更早中止。 |
|
|
99
|
+
| `maxInputChars` | 输入上限 | `60000` | 4000–400000 | 会话文本总预算;超出保留首尾、省略中间。 |
|
|
100
|
+
|
|
101
|
+
**缓存单价。** 宿主 usage 里只有 `cacheReadTokens` / `cacheWriteTokens`(都是输入侧),没有缓存输出 token;缓存 token 是额外的输入 token,不从 `inputTokens` 扣。`priceCacheReadPerMTok` / `priceCacheWritePerMTok` 为 0 时不计缓存部分;如果账本里有缓存 token 而缓存单价为 0,状态区会提示「缓存 token 未计价」。内置参考价没有缓存价,不做猜测。
|
|
102
|
+
|
|
103
|
+
**币种与账本。** 设置里的金额和单价按 `currency` 填写(USD/CNY),`usdToCny` 是手动汇率。账本与工作区预算池内部始终记 USD:调用时把设置金额换算成 USD,展示时再换回当前币种。切换币种会按汇率折算当前表单金额,保存前不会落盘;旧版 `maxCostPerDayUsd` / `workspaceMaxCostPerDayUsd` 键会按 USD 自动迁移到新键。汇率变化只影响之后的换算和展示,不会重写历史账本。
|
|
104
|
+
|
|
105
|
+
> **关于 `sessionCap`、`dedupe`、`immuneTurns`:** `sessionCap` 会生效——单会话投递到上限后,只有 `blocker` 级建议还会返回。`dedupe` 与 `immuneTurns` 会被保存并用于 shadow 路径,但工具路径不会因为它们跳过徵询(是模型显式要求的)。真正会拦住**调用本身**的是 `maxCallsPerTurn` / `maxCallsPerSession` / `maxCallsPerHour` / `maxCostPerDay`。
|
|
98
106
|
|
|
99
107
|
示例(路由名和模型名是占位,请用选择器里看到的真实值):
|
|
100
108
|
|
|
@@ -133,8 +141,8 @@ dsh plugin --profile desktop add /tmp/kangyishuai-dsh-advisor-*.tgz
|
|
|
133
141
|
这一节请在开启前读完。
|
|
134
142
|
|
|
135
143
|
- **每次徵询都是一次额外的模型调用**,按顾问路由计费,走那家 provider 的额度。主模型自身的 usage 不受影响。
|
|
136
|
-
- **token 由插件自己记账。** DSH 只为主动环里的 assistant 消息记 usage,所以插件单写一份账本:`<DSH_HOME>/advisor/usage.jsonl`,一次徵询一行 JSON(时间、会话 id、触发方式 `tool` / `shadow` / `manual`、provider、model、思考档、结论、严重度、输入 / 输出 /
|
|
137
|
-
- **算钱需要你填单价。** DSH
|
|
144
|
+
- **token 由插件自己记账。** DSH 只为主动环里的 assistant 消息记 usage,所以插件单写一份账本:`<DSH_HOME>/advisor/usage.jsonl`,一次徵询一行 JSON(时间、会话 id、触发方式 `tool` / `shadow` / `manual`、provider、model、思考档、结论、严重度、输入 / 输出 / 缓存读/写 token、估算金额、耗时、错误码)。文件只追加,超过 2 MiB 滚动到 `usage.jsonl.1`,启动时从文件尾部恢复累计数。
|
|
145
|
+
- **算钱需要你填单价。** DSH 的模型元数据没有价格字段。按 `currency` 填 `priceInputPerMTok`、`priceOutputPerMTok`、`priceCacheReadPerMTok`、`priceCacheWritePerMTok`;都留 0 时账本只记 token,界面不显示金额。账本内部记 USD 等价,内置参考价是 USD,人民币展示时折算;内置参考价没有缓存价。插件从不猜价格。
|
|
138
146
|
- **每日预算统计的是已经记下的花费**,所以跨过上限的那一次徵询仍会完成并计费。
|
|
139
147
|
- **顾问路由在外部时,会话内容会离开本机。** 渲染后的会话文本(含工具调用与结果,受 `privacyScope` 和 `maxInputChars` 限制)会发到该路由指向的 baseURL。如果那是另一家厂商,你的对话就离开了这台机器;只有指向本地网关的路由才留在本机。除此之外插件什么都不发:没有遥测,没有统计,除了徵询本身不访问网络。
|
|
140
148
|
- **`privacyScope` 决定发多少。** `summary`(默认):每个文本块最多 2,000 字符,工具参数最多 300,工具结果最多 700。`off`:只发用户与助手的文本(最多 1,200 字符),完全不发工具往来。`full`:文本最多 4,000,工具参数最多 1,200,工具结果最多 3,500。渲染完再按 `maxInputChars`(默认 60,000)封顶:保留前 30% 和结尾,中间替换成省略标记。
|
package/lib/budget.js
CHANGED
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
7
7
|
import { join } from "node:path";
|
|
8
8
|
|
|
9
|
+
import { toUsd } from "./money.js";
|
|
10
|
+
|
|
9
11
|
/** 预算池文件名(位于数据根下的 advisor/ 目录)。 */
|
|
10
12
|
export const BUDGET_FILE = "budget.json";
|
|
11
13
|
|
|
@@ -40,7 +42,7 @@ export function normalizePool(raw, today) {
|
|
|
40
42
|
/**
|
|
41
43
|
* 池子是否还能容纳一次调用。
|
|
42
44
|
* @param {{ calls: number, costUsd: number }} pool
|
|
43
|
-
* @param {{ workspaceMaxCallsPerDay: number,
|
|
45
|
+
* @param {{ workspaceMaxCallsPerDay: number, workspaceMaxCostPerDay: number, currency?: string, usdToCny?: number }} settings
|
|
44
46
|
* @param {number} [estimatedCostUsd]
|
|
45
47
|
* @returns {{ ok: true } | { ok: false, code: "workspace_calls"|"workspace_budget", used: number, limit: number }}
|
|
46
48
|
*/
|
|
@@ -49,9 +51,13 @@ export function checkPool(pool, settings, estimatedCostUsd = 0) {
|
|
|
49
51
|
if (maxCalls > 0 && pool.calls >= maxCalls) {
|
|
50
52
|
return { ok: false, code: "workspace_calls", used: pool.calls, limit: maxCalls };
|
|
51
53
|
}
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
const maxCostUsd = toUsd(
|
|
55
|
+
settings?.workspaceMaxCostPerDay,
|
|
56
|
+
settings?.currency,
|
|
57
|
+
settings?.usdToCny,
|
|
58
|
+
);
|
|
59
|
+
if (maxCostUsd > 0 && pool.costUsd + estimatedCostUsd >= maxCostUsd) {
|
|
60
|
+
return { ok: false, code: "workspace_budget", used: pool.costUsd, limit: maxCostUsd };
|
|
55
61
|
}
|
|
56
62
|
return { ok: true };
|
|
57
63
|
}
|
package/lib/catalog.js
CHANGED
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
//
|
|
10
10
|
// 纯数据 + 纯函数,不 import 宿主包。
|
|
11
11
|
|
|
12
|
+
import { toUsd } from "./money.js";
|
|
13
|
+
|
|
12
14
|
/**
|
|
13
15
|
* 能力档位(越大越强)。同一族按代际与规格排序;跨族只保证「明显更强/更弱」的关系。
|
|
14
16
|
* 键是模型 id 的小写匹配片段,按**从长到短**匹配,避免 `deepseek-v4` 抢走 `deepseek-v4.1-flash`。
|
|
@@ -111,18 +113,43 @@ export function effectiveRank(tiers, route) {
|
|
|
111
113
|
}
|
|
112
114
|
|
|
113
115
|
/**
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
+
* 有效单价:输入/输出用「用户声明 > 内置」;缓存读取/写入只有用户声明(内置表没有缓存价)。
|
|
117
|
+
* 0 表示该项不算钱。缓存 token 是额外的输入侧 token,不从 inputTokens 里扣。
|
|
118
|
+
* @param {{ priceInputPerMTok?: number, priceOutputPerMTok?: number, priceCacheReadPerMTok?: number, priceCacheWritePerMTok?: number, currency?: string, usdToCny?: number }} settings
|
|
116
119
|
* @param {string} modelId
|
|
117
|
-
* @returns {{ input: number, output: number, source: "settings"|"builtin"|"none" }}
|
|
120
|
+
* @returns {{ input: number, output: number, cacheRead: number, cacheWrite: number, source: "settings"|"builtin"|"none", cacheSource: "settings"|"none" }}
|
|
118
121
|
*/
|
|
119
122
|
export function effectivePrice(settings, modelId) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
+
// 设置里的单价按 settings.currency 填写;内部估算统一换算成 USD。
|
|
124
|
+
/** @param {unknown} value @returns {number} */
|
|
125
|
+
const toPrice = (value) => toUsd(Number(value ?? 0) || 0, settings?.currency, settings?.usdToCny);
|
|
126
|
+
const input = toPrice(settings?.priceInputPerMTok);
|
|
127
|
+
const output = toPrice(settings?.priceOutputPerMTok);
|
|
128
|
+
const cacheRead = toPrice(settings?.priceCacheReadPerMTok);
|
|
129
|
+
const cacheWrite = toPrice(settings?.priceCacheWritePerMTok);
|
|
130
|
+
const cacheSource = cacheRead > 0 || cacheWrite > 0 ? "settings" : "none";
|
|
123
131
|
const builtin = builtinPrice(modelId);
|
|
124
|
-
if (
|
|
125
|
-
|
|
132
|
+
if (input > 0 || output > 0) {
|
|
133
|
+
return { input, output, cacheRead, cacheWrite, source: "settings", cacheSource };
|
|
134
|
+
}
|
|
135
|
+
if (builtin !== undefined) {
|
|
136
|
+
return {
|
|
137
|
+
input: builtin.input,
|
|
138
|
+
output: builtin.output,
|
|
139
|
+
cacheRead,
|
|
140
|
+
cacheWrite,
|
|
141
|
+
source: "builtin",
|
|
142
|
+
cacheSource,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
input: 0,
|
|
147
|
+
output: 0,
|
|
148
|
+
cacheRead,
|
|
149
|
+
cacheWrite,
|
|
150
|
+
source: "none",
|
|
151
|
+
cacheSource,
|
|
152
|
+
};
|
|
126
153
|
}
|
|
127
154
|
|
|
128
155
|
/**
|