@restregular/dsh-memory 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +69 -2
- package/cordis.patch.yml +10 -0
- package/lib/inject/apply.js +82 -0
- package/lib/inject/inject.js +127 -0
- package/lib/provider/apply.js +108 -0
- package/lib/provider/embed-server.js +296 -0
- package/lib/provider/embed.js +105 -0
- package/lib/provider/index.js +612 -0
- package/lib/provider/rerank-cache.js +64 -0
- package/lib/provider/rerank.js +169 -0
- package/lib/provider/scoring.js +145 -0
- package/lib/provider/secrets.js +57 -0
- package/lib/provider/store.js +485 -0
- package/lib/tools/apply.js +106 -0
- package/lib/tools/index.js +224 -0
- package/package.json +49 -4
|
@@ -0,0 +1,296 @@
|
|
|
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
|
+
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
|
|
@@ -0,0 +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
|
+
*/
|
|
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
|
+
}
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
//# sourceURL=D:\.deepseek\memory-plugin\packages\memory-local\src\embed.ts
|