@restregular/dsh-memory 0.3.1 → 0.4.1

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.
@@ -1,105 +1,105 @@
1
- /**
2
- * Minimal OpenAI-compatible /v1/embeddings client for a local llama-server.
3
- * Fails soft: every failure resolves to `null` so callers can degrade.
4
- */
1
+ /**
2
+ * Minimal OpenAI-compatible /v1/embeddings client for a local llama-server.
3
+ * Fails soft: every failure resolves to `null` so callers can degrade.
4
+ */
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+ export class EmbedClient {
14
+ healthy = true
15
+ lastProbe = 0
16
+ backoffMs = 1000
17
+ opts
18
+
19
+ constructor(opts ) {
20
+ this.opts = opts
21
+ }
22
+
23
+ /** Cheap circuit breaker: skip the network while marked unhealthy. */
24
+ get usable() {
25
+ const now = Date.now()
26
+ if (this.healthy) return true
27
+ if (now - this.lastProbe >= this.backoffMs) {
28
+ // allow a retry; double the backoff on continued failure
29
+ this.backoffMs = Math.min(this.backoffMs * 2, 30_000)
30
+ return true
31
+ }
32
+ return false
33
+ }
34
+
35
+ markDown() {
36
+ this.healthy = false
37
+ this.lastProbe = Date.now()
38
+ }
39
+
40
+ markUp() {
41
+ this.healthy = true
42
+ this.backoffMs = 1000
43
+ }
44
+
45
+ get isHealthy() {
46
+ return this.healthy
47
+ }
48
+
49
+ /**
50
+ * Embed texts.
51
+ *
52
+ * `opts.timeoutMs` overrides the configured budget per call: the default
53
+ * (interactive) budget is tuned for short search queries, while writing or
54
+ * backfilling FULL record contents on CPU can take seconds. Using the
55
+ * interactive budget there silently failed every long record and poisoned the
56
+ * circuit breaker (measured 3143ms for three ~600-char records on 2026-10-10).
57
+ */
58
+ async embed(texts , signal , opts ) {
59
+ if (texts.length === 0) return []
60
+ if (!this.usable) return null
61
+ const timeout = AbortSignal.timeout(opts?.timeoutMs ?? this.opts.timeoutMs)
62
+ const combined = signal ? AbortSignal.any([signal, timeout]) : timeout
63
+ try {
64
+ const res = await fetch(this.opts.endpoint, {
65
+ method: 'POST',
66
+ headers: { 'content-type': 'application/json' },
67
+ body: JSON.stringify({ input: texts, model: this.opts.model }),
68
+ signal: combined,
69
+ })
70
+ if (!res.ok) {
71
+ this.markDown()
72
+ return null
73
+ }
74
+ const json = (await res.json())
75
+ const data = json.data
76
+ if (!Array.isArray(data) || data.length !== texts.length) {
77
+ this.markDown()
78
+ return null
79
+ }
80
+ const out = []
81
+ for (const item of data) {
82
+ const arr = item.embedding
83
+ if (!Array.isArray(arr) || arr.length !== this.opts.dims) {
84
+ this.markDown()
85
+ return null
86
+ }
87
+ const v = new Float32Array(arr)
88
+ // llama-server returns L2-normalized vectors; normalize defensively anyway
89
+ let norm = 0
90
+ for (let i = 0; i < v.length; i++) norm += v[i] * v[i]
91
+ const n = Math.sqrt(norm)
92
+ if (n > 0) for (let i = 0; i < v.length; i++) v[i] = v[i] / n
93
+ out.push(v)
94
+ }
95
+ this.markUp()
96
+ return out
97
+ } catch {
98
+ this.markDown()
99
+ return null
100
+ }
101
+ }
102
+ }
5
103
 
6
-
7
-
8
-
9
-
10
-
11
-
12
104
 
13
- export class EmbedClient {
14
- healthy = true
15
- lastProbe = 0
16
- backoffMs = 1000
17
- opts
18
-
19
- constructor(opts ) {
20
- this.opts = opts
21
- }
22
-
23
- /** Cheap circuit breaker: skip the network while marked unhealthy. */
24
- get usable() {
25
- const now = Date.now()
26
- if (this.healthy) return true
27
- if (now - this.lastProbe >= this.backoffMs) {
28
- // allow a retry; double the backoff on continued failure
29
- this.backoffMs = Math.min(this.backoffMs * 2, 30_000)
30
- return true
31
- }
32
- return false
33
- }
34
-
35
- markDown() {
36
- this.healthy = false
37
- this.lastProbe = Date.now()
38
- }
39
-
40
- markUp() {
41
- this.healthy = true
42
- this.backoffMs = 1000
43
- }
44
-
45
- get isHealthy() {
46
- return this.healthy
47
- }
48
-
49
- /**
50
- * Embed texts.
51
- *
52
- * `opts.timeoutMs` overrides the configured budget per call: the default
53
- * (interactive) budget is tuned for short search queries, while writing or
54
- * backfilling FULL record contents on CPU can take seconds. Using the
55
- * interactive budget there silently failed every long record and poisoned the
56
- * circuit breaker (measured 3143ms for three ~600-char records on 2026-10-10).
57
- */
58
- async embed(texts , signal , opts ) {
59
- if (texts.length === 0) return []
60
- if (!this.usable) return null
61
- const timeout = AbortSignal.timeout(opts?.timeoutMs ?? this.opts.timeoutMs)
62
- const combined = signal ? AbortSignal.any([signal, timeout]) : timeout
63
- try {
64
- const res = await fetch(this.opts.endpoint, {
65
- method: 'POST',
66
- headers: { 'content-type': 'application/json' },
67
- body: JSON.stringify({ input: texts, model: this.opts.model }),
68
- signal: combined,
69
- })
70
- if (!res.ok) {
71
- this.markDown()
72
- return null
73
- }
74
- const json = (await res.json())
75
- const data = json.data
76
- if (!Array.isArray(data) || data.length !== texts.length) {
77
- this.markDown()
78
- return null
79
- }
80
- const out = []
81
- for (const item of data) {
82
- const arr = item.embedding
83
- if (!Array.isArray(arr) || arr.length !== this.opts.dims) {
84
- this.markDown()
85
- return null
86
- }
87
- const v = new Float32Array(arr)
88
- // llama-server returns L2-normalized vectors; normalize defensively anyway
89
- let norm = 0
90
- for (let i = 0; i < v.length; i++) norm += v[i] * v[i]
91
- const n = Math.sqrt(norm)
92
- if (n > 0) for (let i = 0; i < v.length; i++) v[i] = v[i] / n
93
- out.push(v)
94
- }
95
- this.markUp()
96
- return out
97
- } catch {
98
- this.markDown()
99
- return null
100
- }
101
- }
102
- }
103
-
104
-
105
- //# sourceURL=D:\.deepseek\memory-plugin\packages\memory-local\src\embed.ts
105
+ //# sourceURL=source/memory-local/src/embed.ts
@@ -0,0 +1,225 @@
1
+ /**
2
+ * 宿主硬件探测(用于"按你的机器推荐模型")。
3
+ *
4
+ * 三条纪律:
5
+ * 1. **只在需要时探测**(面板打开"模型与硬件"、或点了重新检测),绝不在插件激活时跑 ——
6
+ * 探测要 spawn 外部命令(Windows 上还要起 PowerShell),那不该挡住 DSH 启动。
7
+ * 2. **任何一项拿不到就说拿不到**(notes 里照实写),绝不猜一个数字冒充事实:
8
+ * 猜错的显存会直接导致"推荐了一个跑不动的模型"。
9
+ * 3. 全程不抛异常:每个字段都是尽力而为,失败即 null/0。
10
+ *
11
+ * 依赖注入(spawn/fs/时间)让生命周期可以在测试里跑,不需要真的装显卡。
12
+ */
13
+ import { existsSync, statfsSync } from 'node:fs'
14
+ import { arch as osArch, cpus, platform as osPlatform, totalmem, homedir } from 'node:os'
15
+ import { execFile } from 'node:child_process'
16
+ import { dirname } from 'node:path'
17
+
18
+
19
+
20
+
21
+
22
+
23
+                        
24
+
25
+                         
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+                    
35
+
36
+
37
+
38
+                  
39
+
40
+                       
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+
50
+             
51
+
52
+                 
53
+
54
+
55
+
56
+
57
+ /**
58
+ * 显存小于这个值就不值得为它换运行时。
59
+ *
60
+ * 门槛取 4GB 而不是 2GB:2026-10-16 在一台 12 核 / 14GB / AMD 核显(2GB 共享显存)
61
+ * 的机器上实测,2GB 的核显会被判成"可用 GPU"并推荐 Vulkan —— 而共享显存的核显
62
+ * 跑 embedding 不会比 12 个 CPU 核更快,只会多一层驱动风险。
63
+ */
64
+ export const MIN_USEFUL_VRAM_MB = 4096
65
+ /** CUDA 12.x 构建要求的最低 NVIDIA 驱动大版本。 */
66
+ export const CUDA_MIN_DRIVER_MAJOR = 550
67
+
68
+ function run(command , args , timeoutMs ) {
69
+ return new Promise((resolve) => {
70
+ try {
71
+ execFile(command, args, { timeout: timeoutMs, windowsHide: true, maxBuffer: 1 << 20 }, (error, stdout) => {
72
+ if (error) return resolve(null)
73
+ resolve(String(stdout ?? ''))
74
+ })
75
+ } catch {
76
+ resolve(null)
77
+ }
78
+ })
79
+ }
80
+
81
+ export const defaultHardwareDeps = {
82
+ platform: osPlatform(),
83
+ arch: osArch(),
84
+ cpuCount: () => cpus().length,
85
+ totalRamMb: () => Math.round(totalmem() / 1048576),
86
+ freeDiskMb: (dir) => {
87
+ // statfs 需要真实存在的路径:从目标目录往上找到第一个存在的祖先。
88
+ let probe = dir
89
+ for (let i = 0; i < 12 && probe !== '' && !existsSync(probe); i++) probe = dirname(probe)
90
+ if (probe === '' || !existsSync(probe)) return null
91
+ try {
92
+ const fs = statfsSync(probe)
93
+ return Math.round((Number(fs.bavail) * Number(fs.bsize)) / 1048576)
94
+ } catch {
95
+ return null
96
+ }
97
+ },
98
+ nvidiaSmi: () => run('nvidia-smi', ['--query-gpu=name,memory.total,driver_version', '--format=csv,noheader,nounits'], 2500),
99
+ windowsGpu: () => run('powershell', [
100
+ '-NoProfile', '-NonInteractive', '-Command',
101
+ 'Get-CimInstance Win32_VideoController | Select-Object Name,AdapterRAM | ConvertTo-Csv -NoTypeInformation',
102
+ ], 5000),
103
+ log: (message) => console.log(`[dsh-memory-local] ${message}`),
104
+ }
105
+
106
+ function vendorOf(name ) {
107
+ const lower = name.toLowerCase()
108
+ if (/nvidia|geforce|quadro|tesla|rtx/.test(lower)) return 'nvidia'
109
+ if (/amd|radeon|ati /.test(lower)) return 'amd'
110
+ if (/intel|arc|iris|uhd graphics/.test(lower)) return 'intel'
111
+ return 'unknown'
112
+ }
113
+
114
+ /**
115
+ * `nvidia-smi --query-gpu=name,memory.total,driver_version --format=csv,noheader,nounits`
116
+ * 的一行输出:`NVIDIA GeForce RTX 4070, 12282, 551.86`。
117
+ */
118
+ export function parseNvidiaSmi(stdout ) {
119
+ const line = (stdout ?? '').split('\n').map((l) => l.trim()).find((l) => l.length > 0)
120
+ if (line === undefined) return null
121
+ const parts = line.split(',').map((p) => p.trim())
122
+ if (parts.length < 2 || parts[0] === '') return null
123
+ const vram = Number.parseInt(parts[1] ?? '', 10)
124
+ return {
125
+ vendor: 'nvidia',
126
+ name: parts[0] ,
127
+ vramMb: Number.isFinite(vram) && vram > 0 ? vram : null,
128
+ driver: parts[2] !== undefined && parts[2] !== '' ? parts[2] : null,
129
+ }
130
+ }
131
+
132
+ /**
133
+ * `Get-CimInstance Win32_VideoController | ConvertTo-Csv` 的输出。
134
+ *
135
+ * AdapterRAM 是 uint32,超过 4GB 的显卡这里读出来是错的(常见 4095 或负数),
136
+ * 所以可疑值一律当作"拿不到",宁可说不知道也不推荐一个塞不下的模型。
137
+ */
138
+ export function parseWindowsGpu(stdout ) {
139
+ const lines = (stdout ?? '').split('\n').map((l) => l.trim()).filter((l) => l.length > 0)
140
+ if (lines.length < 2) return null
141
+ const notes = []
142
+ let best = null
143
+ for (const line of lines.slice(1)) {
144
+ const cells = line.split(',').map((c) => c.replace(/^"|"$/g, '').trim())
145
+ const name = cells[0] ?? ''
146
+ if (name === '' || /microsoft basic|remote|virtual|parsec|displaylink/i.test(name)) continue
147
+ const raw = Number.parseInt(cells[1] ?? '', 10)
148
+ // AdapterRAM 是 uint32:**4GB 及以上的卡这里一律读不准**(常见 4095MB 或 uint32 溢出值)。
149
+ // 4GB 是个可信的读数上限,到了这个量级就当"未知",猜错会导致推荐一个塞不下的模型。
150
+ const plausible = Number.isFinite(raw) && raw > 0 && raw < 4000 * 1048576
151
+ const vramMb = plausible ? Math.round(raw / 1048576) : null
152
+ if (!plausible && raw !== 0) notes.push(`系统报告 ${name} 的显存 ≥ 4GB(Windows 这里读不准),已按"未知"处理`)
153
+ const candidate = { vendor: vendorOf(name), name, vramMb, driver: null }
154
+ // 优先独显:厂商档位优先于"显存读到了没有" —— 一块读不准显存的 RTX 也比核显值得推。
155
+ const rank = (g ) =>
156
+ (g.vendor === 'nvidia' ? 3 : g.vendor === 'amd' ? 2 : g.vendor === 'intel' ? 1 : 0) * 100_000 + (g.vramMb ?? 0)
157
+ if (best === null || rank(candidate) > rank(best)) best = candidate
158
+ }
159
+ return best === null ? null : { gpu: best, notes }
160
+ }
161
+
162
+ /** 该机可用的运行时风味,按推荐顺序(cuda 需要够新的 NVIDIA 驱动)。 */
163
+ export function flavorsFor(hw ) {
164
+ if (hw.platform === 'darwin') return ['metal', 'cpu']
165
+ const gpu = hw.gpu
166
+ if (gpu === null) return ['cpu']
167
+ const usable = gpu.vramMb === null || gpu.vramMb >= MIN_USEFUL_VRAM_MB
168
+ if (!usable) return ['cpu']
169
+ if (gpu.vendor === 'nvidia') {
170
+ const major = Number.parseInt((gpu.driver ?? '').split('.')[0] ?? '', 10)
171
+ const driverOk = Number.isFinite(major) && major >= CUDA_MIN_DRIVER_MAJOR
172
+ // 驱动读不到就不推 CUDA:装完起不来比慢一点更糟。
173
+ return driverOk ? ['cuda', 'vulkan', 'cpu'] : ['vulkan', 'cpu']
174
+ }
175
+ return ['vulkan', 'cpu']
176
+ }
177
+
178
+ export async function probeHardware(
179
+ opts = {},
180
+ deps = defaultHardwareDeps,
181
+ ) {
182
+ const notes = []
183
+ const dir = opts.dir ?? homedir()
184
+ let gpu = null
185
+
186
+ if (deps.platform === 'darwin') {
187
+ // macOS 上 llama.cpp 自带 Metal,不需要探测显卡。
188
+ notes.push('macOS:llama.cpp 走 Metal,无需选择显卡运行时')
189
+ } else {
190
+ const smi = parseNvidiaSmi(await deps.nvidiaSmi().catch(() => null))
191
+ if (smi !== null) gpu = smi
192
+ else if (deps.platform === 'win32') {
193
+ const win = parseWindowsGpu(await deps.windowsGpu().catch(() => null))
194
+ if (win !== null) { gpu = win.gpu; notes.push(...win.notes) }
195
+ else notes.push('未能探测到显卡(nvidia-smi 与系统查询都没结果),将以 CPU 运行')
196
+ } else {
197
+ notes.push('未能探测到显卡,将以 CPU 运行')
198
+ }
199
+ if (gpu !== null && gpu.vramMb === null) notes.push(`${gpu.name} 的显存未能读到,推荐时会按"不确定"处理`)
200
+ if (gpu !== null && gpu.vendor === 'nvidia' && gpu.driver === null) notes.push('NVIDIA 驱动版本未读到,不会推荐 CUDA 运行时')
201
+ }
202
+
203
+ const freeDiskMb = (() => {
204
+ try { return deps.freeDiskMb(dir) } catch { return null } // 探测失败就说不知道,不许把插件带崩
205
+ })()
206
+ if (freeDiskMb === null) notes.push('未能读到磁盘剩余空间')
207
+
208
+ const hw = {
209
+ platform: deps.platform,
210
+ arch: deps.arch,
211
+ cpuCount: Math.max(1, deps.cpuCount()),
212
+ totalRamMb: Math.max(0, deps.totalRamMb()),
213
+ freeDiskMb,
214
+ diskPath: freeDiskMb === null ? null : dir,
215
+ gpu,
216
+ flavors: [],
217
+ notes,
218
+ }
219
+ hw.flavors = flavorsFor(hw)
220
+ deps.log(`硬件:${hw.cpuCount} 核 / ${Math.round(hw.totalRamMb / 1024)}GB 内存 / 显卡 ${gpu?.name ?? '无'} / 可用运行时 ${hw.flavors.join(',')}`)
221
+ return hw
222
+ }
223
+
224
+
225
+ //# sourceURL=source/memory-local/src/hardware.ts