@restregular/dsh-memory 0.3.0 → 0.4.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.
@@ -1,296 +1,310 @@
1
- /**
2
- * Lifecycle manager for the local embedding backend (llama-server).
3
- *
4
- * Why: the provider degrades to lexical-only search whenever the embedding
5
- * backend is down, and a machine reboot silently leaves it down (observed
6
- * 2026-10-10 — the panel then shows 待嵌入>0 forever). The backend should live
7
- * exactly as long as the app does: spawned in the background on activation,
8
- * terminated when the plugin is disposed (which cordis does on quit).
9
- *
10
- * Rules:
11
- * - opt-in: `enabled` defaults to false, so dev/test runs never spawn anything
12
- * - an already-healthy endpoint is adopted instead of double-spawning; if we
13
- * have a PID file for it, we also own it and will stop it on exit
14
- * - every failure resolves to a state string; nothing here ever throws
15
- *
16
- * Dependency-injected (spawn/probe/fs/kill) so the whole lifecycle is testable
17
- * without starting a real model server.
18
- */
19
- import { spawn as nodeSpawn, } from 'node:child_process'
20
- import { appendFileSync, existsSync, mkdirSync, openSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
21
- import { dirname, join } from 'node:path'
1
+ /**
2
+ * Lifecycle manager for the local embedding backend (llama-server).
3
+ *
4
+ * Why: the provider degrades to lexical-only search whenever the embedding
5
+ * backend is down, and a machine reboot silently leaves it down (observed
6
+ * 2026-10-10 — the panel then shows 待嵌入>0 forever). The backend should live
7
+ * exactly as long as the app does: spawned in the background on activation,
8
+ * terminated when the plugin is disposed (which cordis does on quit).
9
+ *
10
+ * Rules:
11
+ * - opt-in: `enabled` defaults to false, so dev/test runs never spawn anything
12
+ * - an already-healthy endpoint is adopted instead of double-spawning; if we
13
+ * have a PID file for it, we also own it and will stop it on exit
14
+ * - every failure resolves to a state string; nothing here ever throws
15
+ *
16
+ * Dependency-injected (spawn/probe/fs/kill) so the whole lifecycle is testable
17
+ * without starting a real model server.
18
+ */
19
+ import { spawn as nodeSpawn, } from 'node:child_process'
20
+ import { appendFileSync, existsSync, mkdirSync, openSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
21
+ import { dirname, join } from 'node:path'
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
+
61
+
62
+
63
+
64
+
65
+
66
+
67
+
68
+
69
+
70
+ const DEFAULTS = {
71
+ host: '127.0.0.1',
72
+ port: 8078,
73
+ threads: 6,
74
+ startupTimeoutMs: 60_000,
75
+ }
76
+
77
+ async function defaultProbe(url , timeoutMs ) {
78
+ try {
79
+ const res = await fetch(url, { signal: AbortSignal.timeout(timeoutMs) })
80
+ return res.ok
81
+ } catch {
82
+ return false
83
+ }
84
+ }
85
+
86
+ function defaultKill(pid ) {
87
+ try {
88
+ process.kill(pid)
89
+ } catch {
90
+ // already gone, or not ours to kill
91
+ }
92
+ }
93
+
94
+ export const defaultEmbedServerDeps = {
95
+ spawn: nodeSpawn,
96
+ probe: defaultProbe,
97
+ kill: defaultKill,
98
+ log: (message) => console.log(`[dsh-memory-local] ${message}`),
99
+ sleep: (ms) => new Promise((r) => setTimeout(r, ms)),
100
+ }
101
+
102
+ export class EmbedServerManager {
103
+ cfg
104
+ deps
105
+ child = null
106
+ pid = null
107
+ /** True when this manager started (or adopted a PID-tracked) server. */
108
+ owned = false
109
+ stopping = false
110
+ /** PID recorded by a previous run that we adopted. */
111
+ adoptedPid = null
112
+
113
+ constructor(cfg = {}, deps = {}) {
114
+ this.cfg = cfg
115
+ this.deps = { ...defaultEmbedServerDeps, ...deps }
116
+ }
117
+
118
+ get baseUrl() {
119
+ const host = this.cfg.host ?? DEFAULTS.host
120
+ const port = this.cfg.port ?? DEFAULTS.port
121
+ return `http://${host}:${port}`
122
+ }
123
+
124
+ get pidFile() {
125
+ if (this.cfg.pidFile) return this.cfg.pidFile
126
+ if (this.cfg.logFile) return `${this.cfg.logFile}.pid`
127
+ if (this.cfg.cwd) return join(this.cfg.cwd, 'llama-server.pid')
128
+ if (this.cfg.exe) return join(dirname(this.cfg.exe), 'llama-server.pid')
129
+ return null
130
+ }
131
+
132
+ get isOwned() {
133
+ return this.owned
134
+ }
135
+
136
+ /** Static lifecycle facts for a management UI (no network I/O). */
137
+ info() {
138
+ return {
139
+ enabled: this.cfg.enabled === true,
140
+ owned: this.owned,
141
+ pid: this.pid ?? this.adoptedPid,
142
+ baseUrl: this.baseUrl,
143
+ }
144
+ }
145
+
146
+ /** Probe the health endpoint right now. */
147
+ async healthCheck(timeoutMs = 2000) {
148
+ return this.deps.probe(`${this.baseUrl}/health`, timeoutMs)
149
+ }
150
+
151
+ /**
152
+ * Start (or adopt) the backend. Never throws; resolves to what actually
153
+ * happened so the caller can log it.
154
+ */
155
+ async ensureRunning() {
156
+ if (!this.cfg.enabled) return 'disabled'
157
+ if (!this.cfg.exe || !this.cfg.model) {
158
+ this.deps.log('embed server: spawn.enabled 但缺少 exe/model,跳过自启')
159
+ return 'failed'
160
+ }
161
+ // a manual restart must be possible after a manual stop
162
+ this.stopping = false
163
+ try {
164
+ if (await this.deps.probe(`${this.baseUrl}/health`, 1500)) {
165
+ const recorded = this.readPidFile()
166
+ if (recorded && this.isAlive(recorded)) {
167
+ // leftover from a previous run (or a crash) → adopt and own it
168
+ this.adoptedPid = recorded
169
+ this.pid = recorded
170
+ this.owned = true
171
+ this.deps.log(`embed server: 复用上轮实例 pid=${recorded}(${this.baseUrl}),退出时一并关闭`)
172
+ return 'adopted-managed'
173
+ }
174
+ this.deps.log(`embed server: 复用外部实例(${this.baseUrl}),不接管生命周期`)
175
+ return 'external'
176
+ }
177
+ return this.spawnServer()
178
+ } catch (error) {
179
+ this.deps.log(`embed server: 启动失败(已忽略): ${(error )?.message ?? String(error)}`)
180
+ return 'failed'
181
+ }
182
+ }
183
+
184
+ /** Poll until the health endpoint answers or the budget runs out. */
185
+ async waitUntilReady(timeoutMs ) {
186
+ if (!this.owned && !this.child) return true
187
+ const deadline = Date.now() + (timeoutMs ?? this.cfg.startupTimeoutMs ?? DEFAULTS.startupTimeoutMs)
188
+ while (Date.now() < deadline) {
189
+ if (await this.deps.probe(`${this.baseUrl}/health`, 1500)) return true
190
+ if (this.child && this.child.exitCode !== null) return false
191
+ await this.deps.sleep(500)
192
+ }
193
+ return false
194
+ }
195
+
196
+ /**
197
+ * Stop the backend if (and only if) this manager owns it.
198
+ * @returns true when a process was actually signalled.
199
+ */
200
+ stop() {
201
+ if (!this.owned || this.stopping) return false
202
+ this.stopping = true
203
+ const pid = this.pid ?? this.adoptedPid
204
+ let killed = false
205
+ if (pid !== null) {
206
+ this.deps.kill(pid)
207
+ killed = true
208
+ this.deps.log(`embed server: 已停止 pid=${pid}`)
209
+ }
210
+ this.child = null
211
+ this.pid = null
212
+ this.adoptedPid = null
213
+ this.owned = false
214
+ this.removePidFile()
215
+ return killed
216
+ }
217
+
218
+ spawnServer() {
219
+ const host = this.cfg.host ?? DEFAULTS.host
220
+ const port = this.cfg.port ?? DEFAULTS.port
221
+ const pooling = this.cfg.pooling ?? 'cls'
222
+ const gpuLayers = this.cfg.gpuLayers ?? 0
223
+ const args = [
224
+ '-m', this.cfg.model ,
225
+ '--embeddings',
226
+ // 池化跟着模型走;'auto' 不传,让 llama.cpp 用元数据里的值。
227
+ ...(pooling === 'auto' ? [] : ['--pooling', pooling]),
228
+ '--host', host,
229
+ '--port', String(port),
230
+ '-t', String(this.cfg.threads ?? DEFAULTS.threads),
231
+ // 有显卡时把层卸载到显存;CPU 风味不传(等价于 0)。
232
+ ...(gpuLayers > 0 ? ['-ngl', String(gpuLayers)] : []),
233
+ ...(this.cfg.extraArgs ?? []),
234
+ ]
235
+ const logFile = this.cfg.logFile
236
+ if (logFile) {
237
+ try { mkdirSync(dirname(logFile), { recursive: true }) } catch { /* best effort */ }
238
+ }
239
+ let out = 'ignore'
240
+ if (logFile) {
241
+ try { out = openSync(logFile, 'a') } catch { out = 'ignore' }
242
+ }
243
+ const child = this.deps.spawn(this.cfg.exe , args, {
244
+ cwd: this.cfg.cwd,
245
+ windowsHide: true,
246
+ detached: false,
247
+ stdio: out === 'ignore' ? 'ignore' : ['ignore', out, out],
248
+ })
249
+ this.child = child
250
+ this.owned = true
251
+ this.pid = child.pid ?? null
252
+ if (this.pid !== null) this.writePidFile(this.pid)
253
+ if (logFile) {
254
+ appendLogLine(logFile, `\n=== ${new Date().toISOString()} llama-server 由 DSH 启动 pid=${child.pid ?? '?'} args=${args.join(' ')}`)
255
+ }
256
+ child.once('exit', () => {
257
+ if (!this.stopping) this.deps.log('embed server: 进程已退出')
258
+ this.child = null
259
+ this.owned = false
260
+ this.removePidFile()
261
+ })
262
+ child.once('error', (error) => {
263
+ this.deps.log(`embed server: 进程错误(已忽略): ${error.message}`)
264
+ this.child = null
265
+ this.owned = false
266
+ })
267
+ this.deps.log(`embed server: 已后台启动 pid=${child.pid ?? '?'}(${this.baseUrl})`)
268
+ return 'spawned'
269
+ }
270
+
271
+ readPidFile() {
272
+ const file = this.pidFile
273
+ if (!file || !existsSync(file)) return null
274
+ try {
275
+ const pid = Number.parseInt(readFileSync(file, 'utf8').trim(), 10)
276
+ return Number.isFinite(pid) && pid > 0 ? pid : null
277
+ } catch {
278
+ return null
279
+ }
280
+ }
281
+
282
+ writePidFile(pid ) {
283
+ const file = this.pidFile
284
+ if (!file) return
285
+ try { writeFileSync(file, String(pid), 'utf8') } catch { /* best effort */ }
286
+ }
287
+
288
+ removePidFile() {
289
+ const file = this.pidFile
290
+ if (!file || !existsSync(file)) return
291
+ try { rmSync(file, { force: true }) } catch { /* best effort */ }
292
+ }
293
+
294
+ isAlive(pid ) {
295
+ try {
296
+ process.kill(pid, 0)
297
+ return true
298
+ } catch {
299
+ return false
300
+ }
301
+ }
302
+ }
303
+
304
+ /** Append a line to a log file (used by callers that want their own tracing). */
305
+ export function appendLogLine(file , line ) {
306
+ try { appendFileSync(file, `${line}\n`, 'utf8') } catch { /* best effort */ }
307
+ }
22
308
 
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
309
 
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
- const DEFAULTS = {
62
- host: '127.0.0.1',
63
- port: 8078,
64
- threads: 6,
65
- startupTimeoutMs: 60_000,
66
- }
67
-
68
- async function defaultProbe(url , timeoutMs ) {
69
- try {
70
- const res = await fetch(url, { signal: AbortSignal.timeout(timeoutMs) })
71
- return res.ok
72
- } catch {
73
- return false
74
- }
75
- }
76
-
77
- function defaultKill(pid ) {
78
- try {
79
- process.kill(pid)
80
- } catch {
81
- // already gone, or not ours to kill
82
- }
83
- }
84
-
85
- export const defaultEmbedServerDeps = {
86
- spawn: nodeSpawn,
87
- probe: defaultProbe,
88
- kill: defaultKill,
89
- log: (message) => console.log(`[dsh-memory-local] ${message}`),
90
- sleep: (ms) => new Promise((r) => setTimeout(r, ms)),
91
- }
92
-
93
- export class EmbedServerManager {
94
- cfg
95
- deps
96
- child = null
97
- pid = null
98
- /** True when this manager started (or adopted a PID-tracked) server. */
99
- owned = false
100
- stopping = false
101
- /** PID recorded by a previous run that we adopted. */
102
- adoptedPid = null
103
-
104
- constructor(cfg = {}, deps = {}) {
105
- this.cfg = cfg
106
- this.deps = { ...defaultEmbedServerDeps, ...deps }
107
- }
108
-
109
- get baseUrl() {
110
- const host = this.cfg.host ?? DEFAULTS.host
111
- const port = this.cfg.port ?? DEFAULTS.port
112
- return `http://${host}:${port}`
113
- }
114
-
115
- get pidFile() {
116
- if (this.cfg.pidFile) return this.cfg.pidFile
117
- if (this.cfg.logFile) return `${this.cfg.logFile}.pid`
118
- if (this.cfg.cwd) return join(this.cfg.cwd, 'llama-server.pid')
119
- if (this.cfg.exe) return join(dirname(this.cfg.exe), 'llama-server.pid')
120
- return null
121
- }
122
-
123
- get isOwned() {
124
- return this.owned
125
- }
126
-
127
- /** Static lifecycle facts for a management UI (no network I/O). */
128
- info() {
129
- return {
130
- enabled: this.cfg.enabled === true,
131
- owned: this.owned,
132
- pid: this.pid ?? this.adoptedPid,
133
- baseUrl: this.baseUrl,
134
- }
135
- }
136
-
137
- /** Probe the health endpoint right now. */
138
- async healthCheck(timeoutMs = 2000) {
139
- return this.deps.probe(`${this.baseUrl}/health`, timeoutMs)
140
- }
141
-
142
- /**
143
- * Start (or adopt) the backend. Never throws; resolves to what actually
144
- * happened so the caller can log it.
145
- */
146
- async ensureRunning() {
147
- if (!this.cfg.enabled) return 'disabled'
148
- if (!this.cfg.exe || !this.cfg.model) {
149
- this.deps.log('embed server: spawn.enabled 但缺少 exe/model,跳过自启')
150
- return 'failed'
151
- }
152
- // a manual restart must be possible after a manual stop
153
- this.stopping = false
154
- try {
155
- if (await this.deps.probe(`${this.baseUrl}/health`, 1500)) {
156
- const recorded = this.readPidFile()
157
- if (recorded && this.isAlive(recorded)) {
158
- // leftover from a previous run (or a crash) → adopt and own it
159
- this.adoptedPid = recorded
160
- this.pid = recorded
161
- this.owned = true
162
- this.deps.log(`embed server: 复用上轮实例 pid=${recorded}(${this.baseUrl}),退出时一并关闭`)
163
- return 'adopted-managed'
164
- }
165
- this.deps.log(`embed server: 复用外部实例(${this.baseUrl}),不接管生命周期`)
166
- return 'external'
167
- }
168
- return this.spawnServer()
169
- } catch (error) {
170
- this.deps.log(`embed server: 启动失败(已忽略): ${(error )?.message ?? String(error)}`)
171
- return 'failed'
172
- }
173
- }
174
-
175
- /** Poll until the health endpoint answers or the budget runs out. */
176
- async waitUntilReady(timeoutMs ) {
177
- if (!this.owned && !this.child) return true
178
- const deadline = Date.now() + (timeoutMs ?? this.cfg.startupTimeoutMs ?? DEFAULTS.startupTimeoutMs)
179
- while (Date.now() < deadline) {
180
- if (await this.deps.probe(`${this.baseUrl}/health`, 1500)) return true
181
- if (this.child && this.child.exitCode !== null) return false
182
- await this.deps.sleep(500)
183
- }
184
- return false
185
- }
186
-
187
- /**
188
- * Stop the backend if (and only if) this manager owns it.
189
- * @returns true when a process was actually signalled.
190
- */
191
- stop() {
192
- if (!this.owned || this.stopping) return false
193
- this.stopping = true
194
- const pid = this.pid ?? this.adoptedPid
195
- let killed = false
196
- if (pid !== null) {
197
- this.deps.kill(pid)
198
- killed = true
199
- this.deps.log(`embed server: 已停止 pid=${pid}`)
200
- }
201
- this.child = null
202
- this.pid = null
203
- this.adoptedPid = null
204
- this.owned = false
205
- this.removePidFile()
206
- return killed
207
- }
208
-
209
- spawnServer() {
210
- const host = this.cfg.host ?? DEFAULTS.host
211
- const port = this.cfg.port ?? DEFAULTS.port
212
- const args = [
213
- '-m', this.cfg.model ,
214
- '--embeddings',
215
- '--pooling', 'cls',
216
- '--host', host,
217
- '--port', String(port),
218
- '-t', String(this.cfg.threads ?? DEFAULTS.threads),
219
- ...(this.cfg.extraArgs ?? []),
220
- ]
221
- const logFile = this.cfg.logFile
222
- if (logFile) {
223
- try { mkdirSync(dirname(logFile), { recursive: true }) } catch { /* best effort */ }
224
- }
225
- let out = 'ignore'
226
- if (logFile) {
227
- try { out = openSync(logFile, 'a') } catch { out = 'ignore' }
228
- }
229
- const child = this.deps.spawn(this.cfg.exe , args, {
230
- cwd: this.cfg.cwd,
231
- windowsHide: true,
232
- detached: false,
233
- stdio: out === 'ignore' ? 'ignore' : ['ignore', out, out],
234
- })
235
- this.child = child
236
- this.owned = true
237
- this.pid = child.pid ?? null
238
- if (this.pid !== null) this.writePidFile(this.pid)
239
- if (logFile) {
240
- appendLogLine(logFile, `\n=== ${new Date().toISOString()} llama-server 由 DSH 启动 pid=${child.pid ?? '?'} args=${args.join(' ')}`)
241
- }
242
- child.once('exit', () => {
243
- if (!this.stopping) this.deps.log('embed server: 进程已退出')
244
- this.child = null
245
- this.owned = false
246
- this.removePidFile()
247
- })
248
- child.once('error', (error) => {
249
- this.deps.log(`embed server: 进程错误(已忽略): ${error.message}`)
250
- this.child = null
251
- this.owned = false
252
- })
253
- this.deps.log(`embed server: 已后台启动 pid=${child.pid ?? '?'}(${this.baseUrl})`)
254
- return 'spawned'
255
- }
256
-
257
- readPidFile() {
258
- const file = this.pidFile
259
- if (!file || !existsSync(file)) return null
260
- try {
261
- const pid = Number.parseInt(readFileSync(file, 'utf8').trim(), 10)
262
- return Number.isFinite(pid) && pid > 0 ? pid : null
263
- } catch {
264
- return null
265
- }
266
- }
267
-
268
- writePidFile(pid ) {
269
- const file = this.pidFile
270
- if (!file) return
271
- try { writeFileSync(file, String(pid), 'utf8') } catch { /* best effort */ }
272
- }
273
-
274
- removePidFile() {
275
- const file = this.pidFile
276
- if (!file || !existsSync(file)) return
277
- try { rmSync(file, { force: true }) } catch { /* best effort */ }
278
- }
279
-
280
- isAlive(pid ) {
281
- try {
282
- process.kill(pid, 0)
283
- return true
284
- } catch {
285
- return false
286
- }
287
- }
288
- }
289
-
290
- /** Append a line to a log file (used by callers that want their own tracing). */
291
- export function appendLogLine(file , line ) {
292
- try { appendFileSync(file, `${line}\n`, 'utf8') } catch { /* best effort */ }
293
- }
294
-
295
-
296
- //# sourceURL=D:\.deepseek\memory-plugin\packages\memory-local\src\embed-server.ts
310
+ //# sourceURL=source/memory-local/src/embed-server.ts