dsh-notify-tray 0.1.12 → 0.1.13
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.en.md +3 -3
- package/README.md +3 -3
- package/README.zh-CN.md +3 -3
- package/assets/mascot-peeking.prompt.txt +6 -6
- package/assets/notify-icon.png +0 -0
- package/assets/tray-darwin.js +283 -283
- package/cordis.patch.yml +6 -6
- package/lib/ToastActivator.cs +33 -0
- package/lib/catalog.js +448 -448
- package/lib/client.js +18 -90
- package/lib/format.js +284 -284
- package/lib/host/context.js +11 -3
- package/lib/host/notify.js +26 -21
- package/lib/host/routes.js +9 -42
- package/lib/host/spend.js +190 -190
- package/lib/host/tray.js +120 -120
- package/lib/icon.js +159 -159
- package/lib/index.js +2 -7
- package/lib/ledger.js +285 -285
- package/lib/models-dev.json +2684 -2684
- package/lib/notices.js +3 -4
- package/lib/pricing.js +302 -302
- package/lib/quota.js +290 -290
- package/lib/system.js +11 -22
- package/lib/tracker.js +250 -250
- package/lib/tray/index.js +142 -142
- package/lib/tray/linux.js +393 -393
- package/lib/voice.js +76 -76
- package/lib/whale.js +396 -396
- package/lib/windows-toast.js +92 -0
- package/locale/en.json +7 -7
- package/locale/zh.json +7 -7
- package/package.json +2 -1
package/lib/notices.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Notice queue: turns the tracker's per-session events into what the user is
|
|
3
|
-
* actually shown,
|
|
3
|
+
* actually shown, by the native notifier.
|
|
4
4
|
*
|
|
5
5
|
* - One notification per session: every notice carries `key` (the session
|
|
6
|
-
* id), and
|
|
7
|
-
* the new one (freedesktop replaces_id, terminal-notifier -group, toast Tag
|
|
8
|
-
* web Notification tag).
|
|
6
|
+
* id), and the OS replaces the session's previous notification with
|
|
7
|
+
* the new one (freedesktop replaces_id, terminal-notifier -group, Windows toast Tag).
|
|
9
8
|
* - A "needs you" notification (approval / choice / input) is taken back
|
|
10
9
|
* once the session no longer waits on anything: `settle()` emits a
|
|
11
10
|
* `withdraw` notice for it.
|
package/lib/pricing.js
CHANGED
|
@@ -1,302 +1,302 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Price resolution for the spend estimate.
|
|
3
|
-
*
|
|
4
|
-
* Chain, in `auto` mode: the user's `prices` table (cordis.patch.yml) → the
|
|
5
|
-
* custom price typed on the settings page → the price list bundled with
|
|
6
|
-
* DSH's pi-ai adapter (`@earendil-works/pi-ai`, USD per million tokens, the
|
|
7
|
-
* same catalog the model selector reads) → this plugin's own static catalog
|
|
8
|
-
* (catalog.js, hand-kept, official CNY prices where there are some) → a
|
|
9
|
-
* snapshot of models.dev shipped with the plugin (lib/models-dev.json,
|
|
10
|
-
* refreshed by scripts/update-models-dev.mjs) for every model the first
|
|
11
|
-
* ones do not know. Everything comes out in the display currency.
|
|
12
|
-
*/
|
|
13
|
-
import { execFile } from 'node:child_process'
|
|
14
|
-
import { existsSync, readdirSync, readFileSync, realpathSync } from 'node:fs'
|
|
15
|
-
import { homedir } from 'node:os'
|
|
16
|
-
import { dirname, join } from 'node:path'
|
|
17
|
-
import { pathToFileURL } from 'node:url'
|
|
18
|
-
import { billedToDeepseek, priceFor, splitRoute } from './ledger.js'
|
|
19
|
-
import { DEEPSEEK_OFF_PEAK, isDeepseekOffPeak, staticPriceFor } from './catalog.js'
|
|
20
|
-
|
|
21
|
-
const PI_AI_REL = join('@earendil-works', 'pi-ai', 'dist', 'models.generated.js')
|
|
22
|
-
/** Fallback CNY-per-USD rate when the user leaves the setting blank. */
|
|
23
|
-
export const DEFAULT_RATE = 7.2
|
|
24
|
-
/** Human labels for where a price came from. */
|
|
25
|
-
export const SOURCE_LABEL = { table: '价格表', custom: '自定义价', catalog: '目录价', static: '内置价', modelsDev: 'models.dev 价' }
|
|
26
|
-
|
|
27
|
-
const DSH_PACKAGE = join('@deepseek-ai', 'dsh')
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Where a globally installed `@deepseek-ai/dsh` (the web / CLI profile) may
|
|
31
|
-
* live: npm's configured prefix, the common `~/.npm-global` user prefix, the
|
|
32
|
-
* prefix of the node running us, npm's Windows prefix (`%APPDATA%\npm`),
|
|
33
|
-
* pnpm's global store, and every node a version manager (nvm, nvm-windows,
|
|
34
|
-
* fnm, volta) installed.
|
|
35
|
-
*/
|
|
36
|
-
export function globalDshDirs(env = process.env, home = safeHome(), execPath = process.execPath) {
|
|
37
|
-
const prefixes = [env.npm_config_prefix, env.NPM_CONFIG_PREFIX, env.PREFIX]
|
|
38
|
-
if (home) prefixes.push(join(home, '.npm-global'), join(home, '.npm-packages'), join(home, '.local'))
|
|
39
|
-
if (execPath) prefixes.push(dirname(dirname(execPath)), dirname(execPath))
|
|
40
|
-
if (env.APPDATA) prefixes.push(join(env.APPDATA, 'npm'))
|
|
41
|
-
prefixes.push('/usr/local', '/usr', '/opt/homebrew')
|
|
42
|
-
// Version managers keep one prefix per installed node; DSH may sit under any of them.
|
|
43
|
-
const nvmDir = env.NVM_DIR || (home ? join(home, '.nvm') : undefined)
|
|
44
|
-
if (nvmDir) for (const v of safeReaddir(join(nvmDir, 'versions', 'node'))) prefixes.push(join(nvmDir, 'versions', 'node', v))
|
|
45
|
-
if (env.NVM_HOME) for (const v of safeReaddir(env.NVM_HOME)) prefixes.push(join(env.NVM_HOME, v))
|
|
46
|
-
const fnmDirs = [env.FNM_DIR, home && join(home, '.local', 'share', 'fnm'), home && join(home, '.fnm'), home && join(home, 'Library', 'Application Support', 'fnm'), env.APPDATA && join(env.APPDATA, 'fnm')]
|
|
47
|
-
for (const fnm of fnmDirs) {
|
|
48
|
-
if (!fnm) continue
|
|
49
|
-
for (const v of safeReaddir(join(fnm, 'node-versions'))) prefixes.push(join(fnm, 'node-versions', v, 'installation'))
|
|
50
|
-
}
|
|
51
|
-
const dirs = []
|
|
52
|
-
for (const prefix of prefixes) {
|
|
53
|
-
if (!prefix) continue
|
|
54
|
-
// POSIX prefixes keep packages under lib/; Windows prefixes keep them at the top.
|
|
55
|
-
dirs.push(join(prefix, 'lib', 'node_modules', DSH_PACKAGE), join(prefix, 'node_modules', DSH_PACKAGE))
|
|
56
|
-
}
|
|
57
|
-
// Volta installs each global package into its own image.
|
|
58
|
-
const volta = env.VOLTA_HOME || (home ? join(home, '.volta') : undefined)
|
|
59
|
-
if (volta) {
|
|
60
|
-
const image = join(volta, 'tools', 'image', 'packages', DSH_PACKAGE)
|
|
61
|
-
dirs.push(join(image, 'lib', 'node_modules', DSH_PACKAGE), join(image, 'node_modules', DSH_PACKAGE))
|
|
62
|
-
}
|
|
63
|
-
const pnpmHomes = [env.PNPM_HOME, home && join(home, '.local', 'share', 'pnpm'), home && join(home, 'Library', 'pnpm'), env.LOCALAPPDATA && join(env.LOCALAPPDATA, 'pnpm')]
|
|
64
|
-
for (const pnpmHome of pnpmHomes) {
|
|
65
|
-
if (!pnpmHome) continue
|
|
66
|
-
for (const store of safeReaddir(join(pnpmHome, 'global'))) dirs.push(join(pnpmHome, 'global', store, 'node_modules', DSH_PACKAGE))
|
|
67
|
-
}
|
|
68
|
-
return dirs
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Last resort for an install no guess above covers: ask npm where its global
|
|
73
|
-
* packages live (`npm root -g`). Resolves to the dsh package dir or undefined.
|
|
74
|
-
*/
|
|
75
|
-
export function npmGlobalDshDir({ run = execFile, timeoutMs = 8000 } = {}) {
|
|
76
|
-
return new Promise(resolve => {
|
|
77
|
-
try {
|
|
78
|
-
const npm = process.platform === 'win32' ? 'npm.cmd' : 'npm'
|
|
79
|
-
// npm.cmd is a batch file: Node only runs it through a shell.
|
|
80
|
-
run(npm, ['root', '-g'], { timeout: timeoutMs, windowsHide: true, shell: process.platform === 'win32' }, (error, stdout) => {
|
|
81
|
-
const root = String(stdout ?? '').trim()
|
|
82
|
-
resolve(error || !root ? undefined : join(root, DSH_PACKAGE))
|
|
83
|
-
})
|
|
84
|
-
} catch {
|
|
85
|
-
resolve(undefined)
|
|
86
|
-
}
|
|
87
|
-
})
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function safeHome() {
|
|
91
|
-
try { return homedir() } catch { return undefined }
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function safeRealpath(path) {
|
|
95
|
-
try { return realpathSync(path) } catch { return undefined }
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/** Directories worth walking up from to find the pi-ai package DSH ships. */
|
|
99
|
-
function defaultBases() {
|
|
100
|
-
const bases = []
|
|
101
|
-
if (process.env.DSH_NOTIFY_TRAY_PI_AI) bases.push(process.env.DSH_NOTIFY_TRAY_PI_AI)
|
|
102
|
-
// Desktop host: argv[2] is the DSH runtime dir; CLI: argv[1] is the dsh entry script,
|
|
103
|
-
// usually a bin symlink (~/.npm-global/bin/dsh) whose real path sits inside the package.
|
|
104
|
-
for (const arg of [process.argv[2], process.argv[1]]) {
|
|
105
|
-
if (!arg) continue
|
|
106
|
-
const real = safeRealpath(arg)
|
|
107
|
-
if (real && real !== arg) bases.push(real)
|
|
108
|
-
bases.push(arg)
|
|
109
|
-
}
|
|
110
|
-
// `dsh web` started from a global install leaves neither argv entry pointing into the package.
|
|
111
|
-
bases.push(...globalDshDirs())
|
|
112
|
-
bases.push(dirname(process.execPath), process.cwd())
|
|
113
|
-
return bases
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/** Locate `models.generated.js` of the pi-ai package DSH runs with, or undefined. */
|
|
117
|
-
export function findPiCatalogFile(bases = defaultBases()) {
|
|
118
|
-
const seen = new Set()
|
|
119
|
-
for (const base of bases) {
|
|
120
|
-
let dir = base
|
|
121
|
-
for (let depth = 0; depth < 8 && dir; depth += 1) {
|
|
122
|
-
if (seen.has(dir)) break
|
|
123
|
-
seen.add(dir)
|
|
124
|
-
const direct = join(dir, 'node_modules', PI_AI_REL)
|
|
125
|
-
if (safeExists(direct)) return direct
|
|
126
|
-
// A hoisted copy next to the package (global node_modules, pi-ai as a sibling).
|
|
127
|
-
const sibling = join(dir, PI_AI_REL)
|
|
128
|
-
if (safeExists(sibling)) return sibling
|
|
129
|
-
const pnpm = join(dir, 'node_modules', '.pnpm')
|
|
130
|
-
if (safeExists(pnpm)) {
|
|
131
|
-
for (const entry of safeReaddir(pnpm)) {
|
|
132
|
-
if (!entry.startsWith('@earendil-works+pi-ai@')) continue
|
|
133
|
-
const nested = join(pnpm, entry, 'node_modules', PI_AI_REL)
|
|
134
|
-
if (safeExists(nested)) return nested
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
const parent = dirname(dir)
|
|
138
|
-
if (parent === dir) break
|
|
139
|
-
dir = parent
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
return undefined
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* The version of the `@deepseek-ai/dsh` package this host runs from, read
|
|
147
|
-
* from its package.json (the package itself, or one found in a
|
|
148
|
-
* node_modules on the way up), or undefined when none is found.
|
|
149
|
-
*/
|
|
150
|
-
export function findDshVersion(bases = defaultBases()) {
|
|
151
|
-
const seen = new Set()
|
|
152
|
-
const read = file => {
|
|
153
|
-
try {
|
|
154
|
-
const pkg = JSON.parse(readFileSync(file, 'utf8'))
|
|
155
|
-
return pkg?.name === '@deepseek-ai/dsh' && typeof pkg.version === 'string' ? pkg.version : undefined
|
|
156
|
-
} catch {
|
|
157
|
-
return undefined
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
for (const base of bases) {
|
|
161
|
-
let dir = base
|
|
162
|
-
for (let depth = 0; depth < 8 && dir; depth += 1) {
|
|
163
|
-
if (seen.has(dir)) break
|
|
164
|
-
seen.add(dir)
|
|
165
|
-
const version = read(join(dir, 'package.json')) ?? read(join(dir, 'node_modules', DSH_PACKAGE, 'package.json'))
|
|
166
|
-
if (version) return version
|
|
167
|
-
const parent = dirname(dir)
|
|
168
|
-
if (parent === dir) break
|
|
169
|
-
dir = parent
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
return undefined
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
function safeExists(path) {
|
|
176
|
-
try { return existsSync(path) } catch { return false }
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
function safeReaddir(path) {
|
|
180
|
-
try { return readdirSync(path) } catch { return [] }
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Load pi-ai's model catalog: `{ models: { [provider]: { [id]: { cost, name, … } } }, file }`
|
|
185
|
-
* or undefined when DSH's copy cannot be found (e.g. a non-pi-ai deployment).
|
|
186
|
-
*/
|
|
187
|
-
export async function loadPiCatalog({ file = findPiCatalogFile(), importer = f => import(pathToFileURL(f).href) } = {}) {
|
|
188
|
-
if (!file) return undefined
|
|
189
|
-
try {
|
|
190
|
-
const mod = await importer(file)
|
|
191
|
-
const models = mod?.MODELS ?? mod?.default?.MODELS
|
|
192
|
-
if (!models || typeof models !== 'object') return undefined
|
|
193
|
-
return { models, file }
|
|
194
|
-
} catch {
|
|
195
|
-
return undefined
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/** The catalog's cost row for a route: the provider's own entry first, then any provider listing that model id. */
|
|
200
|
-
export function catalogCost(models, provider, model) {
|
|
201
|
-
if (!models || !model) return undefined
|
|
202
|
-
const own = provider ? models[provider]?.[model] : undefined
|
|
203
|
-
const hit = own ?? Object.values(models).map(list => list?.[model]).find(entry => entry?.cost)
|
|
204
|
-
if (!hit?.cost) return undefined
|
|
205
|
-
const { input = 0, cacheRead = 0, cacheWrite = 0, output = 0 } = hit.cost
|
|
206
|
-
return { input, cacheRead, cacheWrite, output, currency: 'USD', name: hit.name ?? model, provider: hit.provider ?? provider }
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
let modelsDev
|
|
210
|
-
/** The bundled models.dev snapshot: `{ [provider]: { [model]: { name, input, output, cacheRead?, cacheWrite? } } }`. */
|
|
211
|
-
export function modelsDevModels() {
|
|
212
|
-
if (modelsDev === undefined) {
|
|
213
|
-
try {
|
|
214
|
-
modelsDev = JSON.parse(readFileSync(new URL('./models-dev.json', import.meta.url), 'utf8')).models ?? {}
|
|
215
|
-
} catch {
|
|
216
|
-
modelsDev = {}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
return modelsDev
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
/** The models.dev row for a route: the provider's own entry first, then any provider listing that model id. */
|
|
223
|
-
export function modelsDevCost(provider, model, models = modelsDevModels()) {
|
|
224
|
-
if (!model) return undefined
|
|
225
|
-
const own = provider ? models[provider]?.[model] : undefined
|
|
226
|
-
const hit = own ?? Object.values(models).map(list => list?.[model]).find(Boolean)
|
|
227
|
-
if (!hit) return undefined
|
|
228
|
-
const { input, output, cacheRead = input, cacheWrite = input } = hit
|
|
229
|
-
return { input, cacheRead, cacheWrite, output, currency: 'USD', name: hit.name ?? model, provider }
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/** Convert a per-million price row between CNY and USD with `rate` CNY per USD. */
|
|
233
|
-
export function convert(price, from, to, rate = DEFAULT_RATE) {
|
|
234
|
-
if (!price || from === to) return price
|
|
235
|
-
const k = from === 'USD' && to === 'CNY' ? rate : from === 'CNY' && to === 'USD' ? 1 / rate : 1
|
|
236
|
-
const out = {}
|
|
237
|
-
for (const key of ['input', 'cacheRead', 'cacheWrite', 'output']) {
|
|
238
|
-
if (typeof price[key] === 'number') out[key] = Math.round(price[key] * k * 1e6) / 1e6
|
|
239
|
-
}
|
|
240
|
-
return out
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/** The custom price typed on the settings page, or undefined when all three fields are blank. */
|
|
244
|
-
export function customPrice(cfg) {
|
|
245
|
-
const row = { input: cfg.customInput, cacheRead: cfg.customCacheRead, output: cfg.customOutput }
|
|
246
|
-
if (Object.values(row).every(v => typeof v !== 'number')) return undefined
|
|
247
|
-
return { input: row.input ?? 0, cacheRead: row.cacheRead ?? row.input ?? 0, cacheWrite: row.input ?? 0, output: row.output ?? 0, currency: cfg.customPriceCurrency ?? 'CNY' }
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* Build the resolver the ledger prices each `provider/model` route with.
|
|
252
|
-
* Returns rows already converted to `currency`, tagged with their source.
|
|
253
|
-
* Listed prices of DeepSeek routes are peak prices, so those hits carry the
|
|
254
|
-
* off-peak discount; prices the user typed are taken as they are.
|
|
255
|
-
*/
|
|
256
|
-
export function makeResolver({ cfg, models, currency = cfg.currency ?? 'CNY' }) {
|
|
257
|
-
const rate = typeof cfg.exchangeRate === 'number' && cfg.exchangeRate > 0 ? cfg.exchangeRate : DEFAULT_RATE
|
|
258
|
-
const mode = cfg.priceSource ?? 'auto'
|
|
259
|
-
const custom = customPrice(cfg)
|
|
260
|
-
const toDisplay = (row, from) => convert(row, from, currency, rate)
|
|
261
|
-
return key => {
|
|
262
|
-
if (!key || key === 'unknown') {
|
|
263
|
-
// Usage the ledger could not attribute to a route still gets the custom price when there is one.
|
|
264
|
-
return custom && mode !== 'catalog' ? { price: toDisplay(custom, custom.currency), source: 'custom' } : undefined
|
|
265
|
-
}
|
|
266
|
-
const { provider, model } = splitRoute(key)
|
|
267
|
-
const table = priceFor(cfg.prices, key)
|
|
268
|
-
if (table && mode === 'auto') return { price: table, source: 'table' }
|
|
269
|
-
if (custom && (mode === 'auto' || mode === 'custom')) return { price: toDisplay(custom, custom.currency), source: 'custom' }
|
|
270
|
-
if (mode === 'custom') return undefined
|
|
271
|
-
const offPeak = billedToDeepseek(key) ? { offPeakFactor: DEEPSEEK_OFF_PEAK.factor } : {}
|
|
272
|
-
const fromCatalog = catalogCost(models, provider, model)
|
|
273
|
-
if (fromCatalog) return { price: toDisplay(fromCatalog, 'USD'), source: 'catalog', ...offPeak }
|
|
274
|
-
const fallback = staticPriceFor(provider, model)
|
|
275
|
-
if (fallback) return { price: toDisplay(fallback, fallback.currency), source: 'static', ...offPeak }
|
|
276
|
-
const snapshot = modelsDevCost(provider, model)
|
|
277
|
-
if (snapshot) return { price: toDisplay(snapshot, 'USD'), source: 'modelsDev', ...offPeak }
|
|
278
|
-
return undefined
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/** What the settings page shows about the route in use: the listed price and where it comes from. */
|
|
283
|
-
export function describeRoute({ cfg, models, route, now = Date.now() }) {
|
|
284
|
-
if (!route?.model) return undefined
|
|
285
|
-
const resolve = makeResolver({ cfg, models })
|
|
286
|
-
const hit = resolve(`${route.provider ?? ''}/${route.model}`.replace(/^\//u, ''))
|
|
287
|
-
const listed = catalogCost(models, route.provider, route.model) ?? staticPriceFor(route.provider, route.model) ?? modelsDevCost(route.provider, route.model)
|
|
288
|
-
return {
|
|
289
|
-
provider: route.provider,
|
|
290
|
-
model: route.model,
|
|
291
|
-
name: listed?.name,
|
|
292
|
-
listed: listed ? { input: listed.input, cacheRead: listed.cacheRead, cacheWrite: listed.cacheWrite, output: listed.output, currency: listed.currency } : undefined,
|
|
293
|
-
effective: hit
|
|
294
|
-
? {
|
|
295
|
-
...hit.price,
|
|
296
|
-
currency: cfg.currency ?? 'CNY',
|
|
297
|
-
source: hit.source,
|
|
298
|
-
...(hit.offPeakFactor !== undefined ? { offPeakFactor: hit.offPeakFactor, offPeakNow: isDeepseekOffPeak(now) } : {}),
|
|
299
|
-
}
|
|
300
|
-
: undefined,
|
|
301
|
-
}
|
|
302
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Price resolution for the spend estimate.
|
|
3
|
+
*
|
|
4
|
+
* Chain, in `auto` mode: the user's `prices` table (cordis.patch.yml) → the
|
|
5
|
+
* custom price typed on the settings page → the price list bundled with
|
|
6
|
+
* DSH's pi-ai adapter (`@earendil-works/pi-ai`, USD per million tokens, the
|
|
7
|
+
* same catalog the model selector reads) → this plugin's own static catalog
|
|
8
|
+
* (catalog.js, hand-kept, official CNY prices where there are some) → a
|
|
9
|
+
* snapshot of models.dev shipped with the plugin (lib/models-dev.json,
|
|
10
|
+
* refreshed by scripts/update-models-dev.mjs) for every model the first
|
|
11
|
+
* ones do not know. Everything comes out in the display currency.
|
|
12
|
+
*/
|
|
13
|
+
import { execFile } from 'node:child_process'
|
|
14
|
+
import { existsSync, readdirSync, readFileSync, realpathSync } from 'node:fs'
|
|
15
|
+
import { homedir } from 'node:os'
|
|
16
|
+
import { dirname, join } from 'node:path'
|
|
17
|
+
import { pathToFileURL } from 'node:url'
|
|
18
|
+
import { billedToDeepseek, priceFor, splitRoute } from './ledger.js'
|
|
19
|
+
import { DEEPSEEK_OFF_PEAK, isDeepseekOffPeak, staticPriceFor } from './catalog.js'
|
|
20
|
+
|
|
21
|
+
const PI_AI_REL = join('@earendil-works', 'pi-ai', 'dist', 'models.generated.js')
|
|
22
|
+
/** Fallback CNY-per-USD rate when the user leaves the setting blank. */
|
|
23
|
+
export const DEFAULT_RATE = 7.2
|
|
24
|
+
/** Human labels for where a price came from. */
|
|
25
|
+
export const SOURCE_LABEL = { table: '价格表', custom: '自定义价', catalog: '目录价', static: '内置价', modelsDev: 'models.dev 价' }
|
|
26
|
+
|
|
27
|
+
const DSH_PACKAGE = join('@deepseek-ai', 'dsh')
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Where a globally installed `@deepseek-ai/dsh` (the web / CLI profile) may
|
|
31
|
+
* live: npm's configured prefix, the common `~/.npm-global` user prefix, the
|
|
32
|
+
* prefix of the node running us, npm's Windows prefix (`%APPDATA%\npm`),
|
|
33
|
+
* pnpm's global store, and every node a version manager (nvm, nvm-windows,
|
|
34
|
+
* fnm, volta) installed.
|
|
35
|
+
*/
|
|
36
|
+
export function globalDshDirs(env = process.env, home = safeHome(), execPath = process.execPath) {
|
|
37
|
+
const prefixes = [env.npm_config_prefix, env.NPM_CONFIG_PREFIX, env.PREFIX]
|
|
38
|
+
if (home) prefixes.push(join(home, '.npm-global'), join(home, '.npm-packages'), join(home, '.local'))
|
|
39
|
+
if (execPath) prefixes.push(dirname(dirname(execPath)), dirname(execPath))
|
|
40
|
+
if (env.APPDATA) prefixes.push(join(env.APPDATA, 'npm'))
|
|
41
|
+
prefixes.push('/usr/local', '/usr', '/opt/homebrew')
|
|
42
|
+
// Version managers keep one prefix per installed node; DSH may sit under any of them.
|
|
43
|
+
const nvmDir = env.NVM_DIR || (home ? join(home, '.nvm') : undefined)
|
|
44
|
+
if (nvmDir) for (const v of safeReaddir(join(nvmDir, 'versions', 'node'))) prefixes.push(join(nvmDir, 'versions', 'node', v))
|
|
45
|
+
if (env.NVM_HOME) for (const v of safeReaddir(env.NVM_HOME)) prefixes.push(join(env.NVM_HOME, v))
|
|
46
|
+
const fnmDirs = [env.FNM_DIR, home && join(home, '.local', 'share', 'fnm'), home && join(home, '.fnm'), home && join(home, 'Library', 'Application Support', 'fnm'), env.APPDATA && join(env.APPDATA, 'fnm')]
|
|
47
|
+
for (const fnm of fnmDirs) {
|
|
48
|
+
if (!fnm) continue
|
|
49
|
+
for (const v of safeReaddir(join(fnm, 'node-versions'))) prefixes.push(join(fnm, 'node-versions', v, 'installation'))
|
|
50
|
+
}
|
|
51
|
+
const dirs = []
|
|
52
|
+
for (const prefix of prefixes) {
|
|
53
|
+
if (!prefix) continue
|
|
54
|
+
// POSIX prefixes keep packages under lib/; Windows prefixes keep them at the top.
|
|
55
|
+
dirs.push(join(prefix, 'lib', 'node_modules', DSH_PACKAGE), join(prefix, 'node_modules', DSH_PACKAGE))
|
|
56
|
+
}
|
|
57
|
+
// Volta installs each global package into its own image.
|
|
58
|
+
const volta = env.VOLTA_HOME || (home ? join(home, '.volta') : undefined)
|
|
59
|
+
if (volta) {
|
|
60
|
+
const image = join(volta, 'tools', 'image', 'packages', DSH_PACKAGE)
|
|
61
|
+
dirs.push(join(image, 'lib', 'node_modules', DSH_PACKAGE), join(image, 'node_modules', DSH_PACKAGE))
|
|
62
|
+
}
|
|
63
|
+
const pnpmHomes = [env.PNPM_HOME, home && join(home, '.local', 'share', 'pnpm'), home && join(home, 'Library', 'pnpm'), env.LOCALAPPDATA && join(env.LOCALAPPDATA, 'pnpm')]
|
|
64
|
+
for (const pnpmHome of pnpmHomes) {
|
|
65
|
+
if (!pnpmHome) continue
|
|
66
|
+
for (const store of safeReaddir(join(pnpmHome, 'global'))) dirs.push(join(pnpmHome, 'global', store, 'node_modules', DSH_PACKAGE))
|
|
67
|
+
}
|
|
68
|
+
return dirs
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Last resort for an install no guess above covers: ask npm where its global
|
|
73
|
+
* packages live (`npm root -g`). Resolves to the dsh package dir or undefined.
|
|
74
|
+
*/
|
|
75
|
+
export function npmGlobalDshDir({ run = execFile, timeoutMs = 8000 } = {}) {
|
|
76
|
+
return new Promise(resolve => {
|
|
77
|
+
try {
|
|
78
|
+
const npm = process.platform === 'win32' ? 'npm.cmd' : 'npm'
|
|
79
|
+
// npm.cmd is a batch file: Node only runs it through a shell.
|
|
80
|
+
run(npm, ['root', '-g'], { timeout: timeoutMs, windowsHide: true, shell: process.platform === 'win32' }, (error, stdout) => {
|
|
81
|
+
const root = String(stdout ?? '').trim()
|
|
82
|
+
resolve(error || !root ? undefined : join(root, DSH_PACKAGE))
|
|
83
|
+
})
|
|
84
|
+
} catch {
|
|
85
|
+
resolve(undefined)
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function safeHome() {
|
|
91
|
+
try { return homedir() } catch { return undefined }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function safeRealpath(path) {
|
|
95
|
+
try { return realpathSync(path) } catch { return undefined }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Directories worth walking up from to find the pi-ai package DSH ships. */
|
|
99
|
+
function defaultBases() {
|
|
100
|
+
const bases = []
|
|
101
|
+
if (process.env.DSH_NOTIFY_TRAY_PI_AI) bases.push(process.env.DSH_NOTIFY_TRAY_PI_AI)
|
|
102
|
+
// Desktop host: argv[2] is the DSH runtime dir; CLI: argv[1] is the dsh entry script,
|
|
103
|
+
// usually a bin symlink (~/.npm-global/bin/dsh) whose real path sits inside the package.
|
|
104
|
+
for (const arg of [process.argv[2], process.argv[1]]) {
|
|
105
|
+
if (!arg) continue
|
|
106
|
+
const real = safeRealpath(arg)
|
|
107
|
+
if (real && real !== arg) bases.push(real)
|
|
108
|
+
bases.push(arg)
|
|
109
|
+
}
|
|
110
|
+
// `dsh web` started from a global install leaves neither argv entry pointing into the package.
|
|
111
|
+
bases.push(...globalDshDirs())
|
|
112
|
+
bases.push(dirname(process.execPath), process.cwd())
|
|
113
|
+
return bases
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Locate `models.generated.js` of the pi-ai package DSH runs with, or undefined. */
|
|
117
|
+
export function findPiCatalogFile(bases = defaultBases()) {
|
|
118
|
+
const seen = new Set()
|
|
119
|
+
for (const base of bases) {
|
|
120
|
+
let dir = base
|
|
121
|
+
for (let depth = 0; depth < 8 && dir; depth += 1) {
|
|
122
|
+
if (seen.has(dir)) break
|
|
123
|
+
seen.add(dir)
|
|
124
|
+
const direct = join(dir, 'node_modules', PI_AI_REL)
|
|
125
|
+
if (safeExists(direct)) return direct
|
|
126
|
+
// A hoisted copy next to the package (global node_modules, pi-ai as a sibling).
|
|
127
|
+
const sibling = join(dir, PI_AI_REL)
|
|
128
|
+
if (safeExists(sibling)) return sibling
|
|
129
|
+
const pnpm = join(dir, 'node_modules', '.pnpm')
|
|
130
|
+
if (safeExists(pnpm)) {
|
|
131
|
+
for (const entry of safeReaddir(pnpm)) {
|
|
132
|
+
if (!entry.startsWith('@earendil-works+pi-ai@')) continue
|
|
133
|
+
const nested = join(pnpm, entry, 'node_modules', PI_AI_REL)
|
|
134
|
+
if (safeExists(nested)) return nested
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
const parent = dirname(dir)
|
|
138
|
+
if (parent === dir) break
|
|
139
|
+
dir = parent
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return undefined
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The version of the `@deepseek-ai/dsh` package this host runs from, read
|
|
147
|
+
* from its package.json (the package itself, or one found in a
|
|
148
|
+
* node_modules on the way up), or undefined when none is found.
|
|
149
|
+
*/
|
|
150
|
+
export function findDshVersion(bases = defaultBases()) {
|
|
151
|
+
const seen = new Set()
|
|
152
|
+
const read = file => {
|
|
153
|
+
try {
|
|
154
|
+
const pkg = JSON.parse(readFileSync(file, 'utf8'))
|
|
155
|
+
return pkg?.name === '@deepseek-ai/dsh' && typeof pkg.version === 'string' ? pkg.version : undefined
|
|
156
|
+
} catch {
|
|
157
|
+
return undefined
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
for (const base of bases) {
|
|
161
|
+
let dir = base
|
|
162
|
+
for (let depth = 0; depth < 8 && dir; depth += 1) {
|
|
163
|
+
if (seen.has(dir)) break
|
|
164
|
+
seen.add(dir)
|
|
165
|
+
const version = read(join(dir, 'package.json')) ?? read(join(dir, 'node_modules', DSH_PACKAGE, 'package.json'))
|
|
166
|
+
if (version) return version
|
|
167
|
+
const parent = dirname(dir)
|
|
168
|
+
if (parent === dir) break
|
|
169
|
+
dir = parent
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return undefined
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function safeExists(path) {
|
|
176
|
+
try { return existsSync(path) } catch { return false }
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function safeReaddir(path) {
|
|
180
|
+
try { return readdirSync(path) } catch { return [] }
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Load pi-ai's model catalog: `{ models: { [provider]: { [id]: { cost, name, … } } }, file }`
|
|
185
|
+
* or undefined when DSH's copy cannot be found (e.g. a non-pi-ai deployment).
|
|
186
|
+
*/
|
|
187
|
+
export async function loadPiCatalog({ file = findPiCatalogFile(), importer = f => import(pathToFileURL(f).href) } = {}) {
|
|
188
|
+
if (!file) return undefined
|
|
189
|
+
try {
|
|
190
|
+
const mod = await importer(file)
|
|
191
|
+
const models = mod?.MODELS ?? mod?.default?.MODELS
|
|
192
|
+
if (!models || typeof models !== 'object') return undefined
|
|
193
|
+
return { models, file }
|
|
194
|
+
} catch {
|
|
195
|
+
return undefined
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** The catalog's cost row for a route: the provider's own entry first, then any provider listing that model id. */
|
|
200
|
+
export function catalogCost(models, provider, model) {
|
|
201
|
+
if (!models || !model) return undefined
|
|
202
|
+
const own = provider ? models[provider]?.[model] : undefined
|
|
203
|
+
const hit = own ?? Object.values(models).map(list => list?.[model]).find(entry => entry?.cost)
|
|
204
|
+
if (!hit?.cost) return undefined
|
|
205
|
+
const { input = 0, cacheRead = 0, cacheWrite = 0, output = 0 } = hit.cost
|
|
206
|
+
return { input, cacheRead, cacheWrite, output, currency: 'USD', name: hit.name ?? model, provider: hit.provider ?? provider }
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
let modelsDev
|
|
210
|
+
/** The bundled models.dev snapshot: `{ [provider]: { [model]: { name, input, output, cacheRead?, cacheWrite? } } }`. */
|
|
211
|
+
export function modelsDevModels() {
|
|
212
|
+
if (modelsDev === undefined) {
|
|
213
|
+
try {
|
|
214
|
+
modelsDev = JSON.parse(readFileSync(new URL('./models-dev.json', import.meta.url), 'utf8')).models ?? {}
|
|
215
|
+
} catch {
|
|
216
|
+
modelsDev = {}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return modelsDev
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** The models.dev row for a route: the provider's own entry first, then any provider listing that model id. */
|
|
223
|
+
export function modelsDevCost(provider, model, models = modelsDevModels()) {
|
|
224
|
+
if (!model) return undefined
|
|
225
|
+
const own = provider ? models[provider]?.[model] : undefined
|
|
226
|
+
const hit = own ?? Object.values(models).map(list => list?.[model]).find(Boolean)
|
|
227
|
+
if (!hit) return undefined
|
|
228
|
+
const { input, output, cacheRead = input, cacheWrite = input } = hit
|
|
229
|
+
return { input, cacheRead, cacheWrite, output, currency: 'USD', name: hit.name ?? model, provider }
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Convert a per-million price row between CNY and USD with `rate` CNY per USD. */
|
|
233
|
+
export function convert(price, from, to, rate = DEFAULT_RATE) {
|
|
234
|
+
if (!price || from === to) return price
|
|
235
|
+
const k = from === 'USD' && to === 'CNY' ? rate : from === 'CNY' && to === 'USD' ? 1 / rate : 1
|
|
236
|
+
const out = {}
|
|
237
|
+
for (const key of ['input', 'cacheRead', 'cacheWrite', 'output']) {
|
|
238
|
+
if (typeof price[key] === 'number') out[key] = Math.round(price[key] * k * 1e6) / 1e6
|
|
239
|
+
}
|
|
240
|
+
return out
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** The custom price typed on the settings page, or undefined when all three fields are blank. */
|
|
244
|
+
export function customPrice(cfg) {
|
|
245
|
+
const row = { input: cfg.customInput, cacheRead: cfg.customCacheRead, output: cfg.customOutput }
|
|
246
|
+
if (Object.values(row).every(v => typeof v !== 'number')) return undefined
|
|
247
|
+
return { input: row.input ?? 0, cacheRead: row.cacheRead ?? row.input ?? 0, cacheWrite: row.input ?? 0, output: row.output ?? 0, currency: cfg.customPriceCurrency ?? 'CNY' }
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Build the resolver the ledger prices each `provider/model` route with.
|
|
252
|
+
* Returns rows already converted to `currency`, tagged with their source.
|
|
253
|
+
* Listed prices of DeepSeek routes are peak prices, so those hits carry the
|
|
254
|
+
* off-peak discount; prices the user typed are taken as they are.
|
|
255
|
+
*/
|
|
256
|
+
export function makeResolver({ cfg, models, currency = cfg.currency ?? 'CNY' }) {
|
|
257
|
+
const rate = typeof cfg.exchangeRate === 'number' && cfg.exchangeRate > 0 ? cfg.exchangeRate : DEFAULT_RATE
|
|
258
|
+
const mode = cfg.priceSource ?? 'auto'
|
|
259
|
+
const custom = customPrice(cfg)
|
|
260
|
+
const toDisplay = (row, from) => convert(row, from, currency, rate)
|
|
261
|
+
return key => {
|
|
262
|
+
if (!key || key === 'unknown') {
|
|
263
|
+
// Usage the ledger could not attribute to a route still gets the custom price when there is one.
|
|
264
|
+
return custom && mode !== 'catalog' ? { price: toDisplay(custom, custom.currency), source: 'custom' } : undefined
|
|
265
|
+
}
|
|
266
|
+
const { provider, model } = splitRoute(key)
|
|
267
|
+
const table = priceFor(cfg.prices, key)
|
|
268
|
+
if (table && mode === 'auto') return { price: table, source: 'table' }
|
|
269
|
+
if (custom && (mode === 'auto' || mode === 'custom')) return { price: toDisplay(custom, custom.currency), source: 'custom' }
|
|
270
|
+
if (mode === 'custom') return undefined
|
|
271
|
+
const offPeak = billedToDeepseek(key) ? { offPeakFactor: DEEPSEEK_OFF_PEAK.factor } : {}
|
|
272
|
+
const fromCatalog = catalogCost(models, provider, model)
|
|
273
|
+
if (fromCatalog) return { price: toDisplay(fromCatalog, 'USD'), source: 'catalog', ...offPeak }
|
|
274
|
+
const fallback = staticPriceFor(provider, model)
|
|
275
|
+
if (fallback) return { price: toDisplay(fallback, fallback.currency), source: 'static', ...offPeak }
|
|
276
|
+
const snapshot = modelsDevCost(provider, model)
|
|
277
|
+
if (snapshot) return { price: toDisplay(snapshot, 'USD'), source: 'modelsDev', ...offPeak }
|
|
278
|
+
return undefined
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** What the settings page shows about the route in use: the listed price and where it comes from. */
|
|
283
|
+
export function describeRoute({ cfg, models, route, now = Date.now() }) {
|
|
284
|
+
if (!route?.model) return undefined
|
|
285
|
+
const resolve = makeResolver({ cfg, models })
|
|
286
|
+
const hit = resolve(`${route.provider ?? ''}/${route.model}`.replace(/^\//u, ''))
|
|
287
|
+
const listed = catalogCost(models, route.provider, route.model) ?? staticPriceFor(route.provider, route.model) ?? modelsDevCost(route.provider, route.model)
|
|
288
|
+
return {
|
|
289
|
+
provider: route.provider,
|
|
290
|
+
model: route.model,
|
|
291
|
+
name: listed?.name,
|
|
292
|
+
listed: listed ? { input: listed.input, cacheRead: listed.cacheRead, cacheWrite: listed.cacheWrite, output: listed.output, currency: listed.currency } : undefined,
|
|
293
|
+
effective: hit
|
|
294
|
+
? {
|
|
295
|
+
...hit.price,
|
|
296
|
+
currency: cfg.currency ?? 'CNY',
|
|
297
|
+
source: hit.source,
|
|
298
|
+
...(hit.offPeakFactor !== undefined ? { offPeakFactor: hit.offPeakFactor, offPeakNow: isDeepseekOffPeak(now) } : {}),
|
|
299
|
+
}
|
|
300
|
+
: undefined,
|
|
301
|
+
}
|
|
302
|
+
}
|