dsh-cooper-memory 0.0.0-stage → 0.1.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.
- package/CHANGELOG.md +24 -0
- package/LICENSE +21 -0
- package/README.md +107 -2
- package/cli.mjs +177 -0
- package/cordis.patch.yml +12 -0
- package/docs/DETAIL.md +83 -0
- package/icon.svg +10 -0
- package/lib/chunk.mjs +283 -0
- package/lib/core.mjs +439 -0
- package/lib/db.mjs +138 -0
- package/lib/extract.mjs +383 -0
- package/lib/import-lingshu.mjs +164 -0
- package/lib/llm.mjs +280 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/mcp.mjs +298 -0
- package/package.json +37 -3
- package/web.mjs +230 -0
package/lib/llm.mjs
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
// lib/llm.mjs —— 给服务进程用的最小 LLM 客户端
|
|
2
|
+
//
|
|
3
|
+
// 配置只从本机读取,**不内置任何开发者默认地址 / 模型 / 凭据名**:
|
|
4
|
+
// baseURL : LLM_BASE_URL | DEEPSEEK_BASE_URL > profile 里当前 provider 的 baseURL
|
|
5
|
+
// model : LLM_MODEL > profile 里 agent-default-model 的 model
|
|
6
|
+
// key : LLM_KEY_ENV 指定的 env/凭据 ref > profile 里 provider 的 apiKeyEnv > DEEPSEEK_API_KEY
|
|
7
|
+
//
|
|
8
|
+
// 三项缺任何一项都直接报错,绝不回落到某个内置网关 —— 否则使用者的凭据会被发到与
|
|
9
|
+
// 他无关的上游。provider 由 agent-default-model.provider 决定;该 provider 找不到时
|
|
10
|
+
// 不会去猜别的 provider 的地址,而是按"缺少 baseURL"处理。
|
|
11
|
+
//
|
|
12
|
+
// 两个已知坑:
|
|
13
|
+
// 1. ~/.dsh/.credentials.yaml 是 flow mapping,值可能带尾随逗号或右花括号,必须剥掉
|
|
14
|
+
// 2. 推理模型的 max_tokens 太小会被 reasoning 吃光,content 返回空串
|
|
15
|
+
|
|
16
|
+
import fs from 'node:fs';
|
|
17
|
+
import os from 'node:os';
|
|
18
|
+
import path from 'node:path';
|
|
19
|
+
|
|
20
|
+
const FALLBACK_KEY_ENV = 'DEEPSEEK_API_KEY';
|
|
21
|
+
|
|
22
|
+
function readText(p) {
|
|
23
|
+
try { return fs.readFileSync(p, 'utf8'); } catch { return ''; }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function profilePatchPath() {
|
|
27
|
+
const profile = process.env.DSH_PROFILE || 'desktop';
|
|
28
|
+
return path.join(os.homedir(), '.dsh', 'profiles', profile, 'cordis.patch.yml');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const indentOf = (line) => line.match(/^ */)[0].length;
|
|
32
|
+
|
|
33
|
+
/** 取 startIdx 这一行下面、缩进更深的那一段(跳过空行与整行注释) */
|
|
34
|
+
function blockLines(lines, startIdx) {
|
|
35
|
+
const baseIndent = indentOf(lines[startIdx]);
|
|
36
|
+
const out = [];
|
|
37
|
+
for (let i = startIdx + 1; i < lines.length; i++) {
|
|
38
|
+
const line = lines[i];
|
|
39
|
+
if (!line.trim() || line.trim().startsWith('#')) { out.push(line); continue; }
|
|
40
|
+
if (indentOf(line) <= baseIndent) break;
|
|
41
|
+
out.push(line);
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 在块内取 `key: value` 的标量值(去掉成对引号) */
|
|
47
|
+
function scalar(block, key) {
|
|
48
|
+
const re = new RegExp('^\\s*' + key + ':\\s*(\\S.*?)\\s*$');
|
|
49
|
+
for (const line of block) {
|
|
50
|
+
const m = line.match(re);
|
|
51
|
+
if (m) return m[1].replace(/^['"]|['"]$/g, '');
|
|
52
|
+
}
|
|
53
|
+
return '';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function firstScalar(lines, key) {
|
|
57
|
+
const re = new RegExp('^\\s*' + key + ':\\s*(\\S.*?)\\s*$');
|
|
58
|
+
for (const line of lines) {
|
|
59
|
+
const m = line.match(re);
|
|
60
|
+
if (m) return m[1].replace(/^['"]|['"]$/g, '');
|
|
61
|
+
}
|
|
62
|
+
return '';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** providers: 下面的 name → 该 provider 的配置块 */
|
|
66
|
+
export function providerMap(lines) {
|
|
67
|
+
const idx = lines.findIndex((l) => /^\s*providers:\s*$/.test(l));
|
|
68
|
+
if (idx < 0) return {};
|
|
69
|
+
const inner = blockLines(lines, idx);
|
|
70
|
+
const entries = inner
|
|
71
|
+
.map((line, i) => ({ line, i }))
|
|
72
|
+
.filter(({ line }) => /^\s*[A-Za-z0-9_.-]+:\s*$/.test(line));
|
|
73
|
+
if (!entries.length) return {};
|
|
74
|
+
const baseIndent = Math.min(...entries.map(({ line }) => indentOf(line)));
|
|
75
|
+
const tops = entries.filter(({ line }) => indentOf(line) === baseIndent);
|
|
76
|
+
const map = {};
|
|
77
|
+
tops.forEach(({ line, i }, k) => {
|
|
78
|
+
const end = k + 1 < tops.length ? tops[k + 1].i : inner.length;
|
|
79
|
+
map[line.trim().replace(/:\s*$/, '')] = inner.slice(i + 1, end);
|
|
80
|
+
});
|
|
81
|
+
return map;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** 从 profile patch 里取 agent-default-model 与它指向的 provider 的 baseURL / apiKeyEnv */
|
|
85
|
+
export function readProviderConfig() {
|
|
86
|
+
const yaml = readText(profilePatchPath());
|
|
87
|
+
const out = { base: '', keyEnv: '', model: '', provider: '' };
|
|
88
|
+
if (!yaml) return out;
|
|
89
|
+
const lines = yaml.split(/\r?\n/);
|
|
90
|
+
|
|
91
|
+
const idx = lines.findIndex((l) => /^\s*-?\s*id:\s*agent-default-model\s*$/.test(l));
|
|
92
|
+
if (idx >= 0) {
|
|
93
|
+
const block = blockLines(lines, idx);
|
|
94
|
+
out.provider = scalar(block, 'provider');
|
|
95
|
+
out.model = scalar(block, 'model');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const providers = providerMap(lines);
|
|
99
|
+
const names = Object.keys(providers);
|
|
100
|
+
const chosen = out.provider ? providers[out.provider] : null;
|
|
101
|
+
if (chosen) {
|
|
102
|
+
out.base = scalar(chosen, 'baseURL');
|
|
103
|
+
out.keyEnv = scalar(chosen, 'apiKeyEnv');
|
|
104
|
+
out.models = chosen
|
|
105
|
+
.filter((l) => /^\s*-\s*id:/.test(l))
|
|
106
|
+
.map((l) => l.replace(/^.*?-\s*id:\s*/, '').replace(/['"]/g, '').trim());
|
|
107
|
+
} else if (!names.length) {
|
|
108
|
+
// 旧版扁平写法:整个文件没有 providers 映射时,退回首个 baseURL / apiKeyEnv
|
|
109
|
+
out.base = firstScalar(lines, 'baseURL');
|
|
110
|
+
out.keyEnv = firstScalar(lines, 'apiKeyEnv');
|
|
111
|
+
}
|
|
112
|
+
// out.provider 存在但匹配不到 provider 时,base 保持为空 → 由 resolveConfig 报缺配置
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 凭据:只解析调用方点名要的 ref,不把整个凭据文件读进内存对象。
|
|
118
|
+
* refs 是 flow mapping,值可能带尾巴的逗号或右花括号。
|
|
119
|
+
*/
|
|
120
|
+
export function readCredentials(allow = []) {
|
|
121
|
+
const yaml = readText(path.join(os.homedir(), '.dsh', '.credentials.yaml'));
|
|
122
|
+
const want = new Set(allow.filter(Boolean));
|
|
123
|
+
const refs = {};
|
|
124
|
+
if (!yaml || !want.size) return refs;
|
|
125
|
+
for (const line of yaml.split(/\r?\n/)) {
|
|
126
|
+
const m = line.match(/^\s*([A-Za-z0-9_.-]+)\s*:\s*(\S+)\s*$/);
|
|
127
|
+
if (!m || !want.has(m[1])) continue;
|
|
128
|
+
refs[m[1]] = m[2].replace(/[,}]+$/, '');
|
|
129
|
+
}
|
|
130
|
+
return refs;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function resolveConfig() {
|
|
134
|
+
const prov = readProviderConfig();
|
|
135
|
+
const keyEnv = process.env.LLM_KEY_ENV || prov.keyEnv || FALLBACK_KEY_ENV;
|
|
136
|
+
const refs = readCredentials([keyEnv, FALLBACK_KEY_ENV]);
|
|
137
|
+
|
|
138
|
+
let key = null;
|
|
139
|
+
let usedKeyEnv = keyEnv;
|
|
140
|
+
let keyFrom = 'none';
|
|
141
|
+
if (process.env[keyEnv]) { key = process.env[keyEnv]; keyFrom = 'env'; }
|
|
142
|
+
else if (refs[keyEnv]) { key = refs[keyEnv]; keyFrom = 'file'; }
|
|
143
|
+
else if (refs[FALLBACK_KEY_ENV]) { key = refs[FALLBACK_KEY_ENV]; usedKeyEnv = FALLBACK_KEY_ENV; keyFrom = 'file'; }
|
|
144
|
+
|
|
145
|
+
const base = (process.env.LLM_BASE_URL || process.env.DEEPSEEK_BASE_URL || prov.base || '').replace(/\/+$/, '');
|
|
146
|
+
const model = process.env.LLM_MODEL || prov.model || '';
|
|
147
|
+
|
|
148
|
+
const missing = [];
|
|
149
|
+
if (!key) missing.push(`凭据 ${keyEnv}(环境变量或 ~/.dsh/.credentials.yaml 里的同名 ref)`);
|
|
150
|
+
if (!base) missing.push('baseURL(profile 的 providers.<名字>.baseURL,或环境变量 LLM_BASE_URL)');
|
|
151
|
+
if (!model) missing.push('model(profile 的 agent-default-model.model,或环境变量 LLM_MODEL)');
|
|
152
|
+
|
|
153
|
+
return { key, base, model, keyEnv: usedKeyEnv, keyFrom, missing, profilePath: profilePatchPath() };
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const configError = (cfg) => '未配置 LLM:缺少 ' + cfg.missing.join(';');
|
|
157
|
+
|
|
158
|
+
/** 探测:只回答"这个进程能不能用 LLM";返回里永远不含 key 本身 */
|
|
159
|
+
export async function probe({ timeoutMs = 30000 } = {}) {
|
|
160
|
+
const cfg = resolveConfig();
|
|
161
|
+
if (cfg.missing.length) {
|
|
162
|
+
return {
|
|
163
|
+
ok: false,
|
|
164
|
+
stage: 'config',
|
|
165
|
+
error: configError(cfg),
|
|
166
|
+
missing: cfg.missing,
|
|
167
|
+
base: cfg.base,
|
|
168
|
+
model: cfg.model,
|
|
169
|
+
keyFrom: cfg.keyFrom,
|
|
170
|
+
profilePath: cfg.profilePath,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
const ctl = new AbortController();
|
|
174
|
+
const t = setTimeout(() => ctl.abort(), timeoutMs);
|
|
175
|
+
try {
|
|
176
|
+
const r = await fetch(cfg.base + '/chat/completions', {
|
|
177
|
+
method: 'POST',
|
|
178
|
+
signal: ctl.signal,
|
|
179
|
+
headers: { 'content-type': 'application/json', authorization: 'Bearer ' + cfg.key },
|
|
180
|
+
body: JSON.stringify({
|
|
181
|
+
model: cfg.model,
|
|
182
|
+
messages: [{ role: 'user', content: '只回答两个字:可用' }],
|
|
183
|
+
max_tokens: 64,
|
|
184
|
+
stream: false,
|
|
185
|
+
}),
|
|
186
|
+
});
|
|
187
|
+
const text = await r.text();
|
|
188
|
+
if (r.ok) {
|
|
189
|
+
let content = '';
|
|
190
|
+
try { content = JSON.parse(text).choices?.[0]?.message?.content ?? ''; } catch {}
|
|
191
|
+
return { ok: true, stage: 'ok', status: r.status, base: cfg.base, model: cfg.model, keyFrom: cfg.keyFrom, sample: content.slice(0, 40) };
|
|
192
|
+
}
|
|
193
|
+
if (r.status === 401 || r.status === 403) {
|
|
194
|
+
return { ok: false, stage: 'auth', status: r.status, base: cfg.base, keyFrom: cfg.keyFrom, error: text.slice(0, 200) };
|
|
195
|
+
}
|
|
196
|
+
return { ok: false, stage: 'http', status: r.status, base: cfg.base, error: text.slice(0, 200) };
|
|
197
|
+
} catch (e) {
|
|
198
|
+
const code = e?.cause?.code || e?.name || 'unknown';
|
|
199
|
+
return {
|
|
200
|
+
ok: false, stage: 'network', code,
|
|
201
|
+
error: code === 'EACCES' ? '出网被拦(沙箱/白名单)' : String(e.message || e),
|
|
202
|
+
base: cfg.base, keyFrom: cfg.keyFrom,
|
|
203
|
+
};
|
|
204
|
+
} finally { clearTimeout(t); }
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** 一次非流式对话,返回文本 */
|
|
208
|
+
export async function chat(messages, { model, maxTokens = 3000, timeoutMs = 90000, temperature = 0 } = {}) {
|
|
209
|
+
const cfg = resolveConfig();
|
|
210
|
+
if (cfg.missing.length) throw new Error(configError(cfg));
|
|
211
|
+
const ctl = new AbortController();
|
|
212
|
+
const t = setTimeout(() => ctl.abort(), timeoutMs);
|
|
213
|
+
try {
|
|
214
|
+
const r = await fetch(cfg.base + '/chat/completions', {
|
|
215
|
+
method: 'POST',
|
|
216
|
+
signal: ctl.signal,
|
|
217
|
+
headers: { 'content-type': 'application/json', authorization: 'Bearer ' + cfg.key },
|
|
218
|
+
body: JSON.stringify({
|
|
219
|
+
model: model || cfg.model,
|
|
220
|
+
messages,
|
|
221
|
+
max_tokens: maxTokens,
|
|
222
|
+
temperature,
|
|
223
|
+
stream: false,
|
|
224
|
+
}),
|
|
225
|
+
});
|
|
226
|
+
const raw = await r.text();
|
|
227
|
+
if (!r.ok) throw new Error(`LLM HTTP ${r.status}: ${raw.slice(0, 300)}`);
|
|
228
|
+
const j = JSON.parse(raw);
|
|
229
|
+
const msg = j.choices?.[0]?.message ?? {};
|
|
230
|
+
// 推理模型可能把额度全花在 reasoning 上
|
|
231
|
+
if (!msg.content && msg.reasoning_content) throw new Error('模型只返回了 reasoning,没有正文;请调大 max_tokens');
|
|
232
|
+
return msg.content ?? '';
|
|
233
|
+
} catch (e) {
|
|
234
|
+
const code = e?.cause?.code;
|
|
235
|
+
if (code === 'EACCES') throw new Error('LLM 出网被拦(EACCES):' + cfg.base);
|
|
236
|
+
throw e;
|
|
237
|
+
} finally { clearTimeout(t); }
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** 哪些失败值得重试:连接类错误与 429/5xx;鉴权错、参数错不重试 */
|
|
241
|
+
export function isTransient(err) {
|
|
242
|
+
const m = String(err?.message || err || '');
|
|
243
|
+
return /ECONNRESET|ECONNREFUSED|ETIMEDOUT|ENOTFOUND|EPIPE|socket hang up|fetch failed|HTTP 429|HTTP 5\d\d/i.test(m);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* 带指数退避的对话调用
|
|
248
|
+
* @param {object} retry {retries, baseDelayMs, maxDelayMs}
|
|
249
|
+
*/
|
|
250
|
+
export async function chatWithRetry(messages, opts = {}, retry = {}) {
|
|
251
|
+
const retries = retry.retries ?? 2;
|
|
252
|
+
const baseDelayMs = retry.baseDelayMs ?? 800;
|
|
253
|
+
const maxDelayMs = retry.maxDelayMs ?? 8000;
|
|
254
|
+
let lastErr;
|
|
255
|
+
const attempts = [];
|
|
256
|
+
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
257
|
+
const t0 = Date.now();
|
|
258
|
+
try {
|
|
259
|
+
const out = await chat(messages, opts);
|
|
260
|
+
attempts.push({ attempt: attempt + 1, ms: Date.now() - t0, ok: true });
|
|
261
|
+
if (process.env.DEBUG_DISTILL && attempt > 0) {
|
|
262
|
+
process.stderr.write(`RETRY_OK>>>attempt ${attempt + 1} after ${attempts.length - 1} failures\n`);
|
|
263
|
+
}
|
|
264
|
+
return out;
|
|
265
|
+
} catch (e) {
|
|
266
|
+
lastErr = e;
|
|
267
|
+
attempts.push({ attempt: attempt + 1, ms: Date.now() - t0, ok: false, error: String(e.message || e).slice(0, 120) });
|
|
268
|
+
const canRetry = attempt < retries && isTransient(e);
|
|
269
|
+
if (process.env.DEBUG_DISTILL) {
|
|
270
|
+
process.stderr.write(`ATTEMPT ${attempt + 1} failed: ${String(e.message || e).slice(0, 120)} | retry=${canRetry}\n`);
|
|
271
|
+
}
|
|
272
|
+
if (!canRetry) break;
|
|
273
|
+
const delay = Math.min(maxDelayMs, baseDelayMs * 2 ** attempt);
|
|
274
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
const err = new Error(String(lastErr?.message || lastErr));
|
|
278
|
+
err.attempts = attempts;
|
|
279
|
+
throw err;
|
|
280
|
+
}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Local memory",
|
|
4
|
+
"description": "Cross-session memory and a folder knowledge base stored in a local SQLite file. Extracts preferences and facts from session logs, indexes folders with structure preserved, and lets you edit, diff, and roll back retrieval chunks.",
|
|
5
|
+
"detail": "docs/DETAIL.md"
|
|
6
|
+
}
|
|
7
|
+
}
|
package/locale/zh.json
ADDED
package/mcp.mjs
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// mcp.mjs —— stdio MCP 服务:DSH 通过子进程直接调用,不走任何网络
|
|
3
|
+
// 协议:JSON-RPC 2.0,stdin/stdout 每行一个消息(MCP stdio transport)
|
|
4
|
+
import readline from 'node:readline';
|
|
5
|
+
import { readFileSync } from 'node:fs';
|
|
6
|
+
import { openDb } from './lib/db.mjs';
|
|
7
|
+
import {
|
|
8
|
+
ingestFolder, searchChunks, getChunk, editChunk, chunkHistory,
|
|
9
|
+
chunkVersionText, rollbackChunk, diffTexts, tree,
|
|
10
|
+
remember, confirmMemory, rejectMemory, editMemory, listMemories, recall,
|
|
11
|
+
} from './lib/core.mjs';
|
|
12
|
+
import { runAutoExtract, recentTurns, llmDistill } from './lib/extract.mjs';
|
|
13
|
+
import { probe as llmProbe } from './lib/llm.mjs';
|
|
14
|
+
|
|
15
|
+
const db = openDb();
|
|
16
|
+
// 版本号以 package.json 为准,避免两处漂移(旧版写死 0.1.0)
|
|
17
|
+
const PKG_VERSION = (() => {
|
|
18
|
+
try { return JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8')).version; } catch { return '0.0.0'; }
|
|
19
|
+
})();
|
|
20
|
+
const SERVER = { name: 'dsh-cooper-memory', version: PKG_VERSION };
|
|
21
|
+
const PROTOCOL = '2024-11-05';
|
|
22
|
+
|
|
23
|
+
const TOOLS = [
|
|
24
|
+
{
|
|
25
|
+
name: 'memory_recall',
|
|
26
|
+
description: '召回长期记忆(跨会话)。返回常驻资料/偏好 + 与本轮问题相关的事实/事项。',
|
|
27
|
+
inputSchema: {
|
|
28
|
+
type: 'object',
|
|
29
|
+
properties: {
|
|
30
|
+
query: { type: 'string', description: '当前问题,用于匹配事实类记忆' },
|
|
31
|
+
budget_chars: { type: 'number', description: '字符预算,默认 1200' },
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
name: 'memory_remember',
|
|
37
|
+
description: '写入一条记忆。status=pending 时不参与召回,需用户确认后生效。',
|
|
38
|
+
inputSchema: {
|
|
39
|
+
type: 'object',
|
|
40
|
+
properties: {
|
|
41
|
+
kind: { type: 'string', enum: ['profile', 'preference', 'fact', 'todo', 'interest'] },
|
|
42
|
+
text: { type: 'string' },
|
|
43
|
+
subject: { type: 'string' },
|
|
44
|
+
status: { type: 'string', enum: ['pending', 'active'], description: '默认 pending' },
|
|
45
|
+
confidence: { type: 'number' },
|
|
46
|
+
tags: { type: 'string' },
|
|
47
|
+
supersedes: { type: 'number', description: '要替代的旧记忆 id;省略则自动探测同主题旧条目' },
|
|
48
|
+
},
|
|
49
|
+
required: ['kind', 'text'],
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
name: 'memory_confirm',
|
|
54
|
+
description: '确认一条待确认记忆,使其进入召回。',
|
|
55
|
+
inputSchema: { type: 'object', properties: { id: { type: 'number' } }, required: ['id'] },
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
name: 'memory_reject',
|
|
59
|
+
description: '拒绝一条记忆(保留抑制记录,不再召回)。',
|
|
60
|
+
inputSchema: { type: 'object', properties: { id: { type: 'number' }, note: { type: 'string' } }, required: ['id'] },
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
name: 'memory_list',
|
|
64
|
+
description: '列出记忆,可按状态/类别过滤。',
|
|
65
|
+
inputSchema: {
|
|
66
|
+
type: 'object',
|
|
67
|
+
properties: { status: { type: 'string' }, kind: { type: 'string' }, limit: { type: 'number' } },
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: 'memory_edit',
|
|
72
|
+
description: '编辑记忆正文(生成新版本)。',
|
|
73
|
+
inputSchema: {
|
|
74
|
+
type: 'object',
|
|
75
|
+
properties: { id: { type: 'number' }, text: { type: 'string' }, note: { type: 'string' } },
|
|
76
|
+
required: ['id', 'text'],
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
name: 'kb_search',
|
|
81
|
+
description: '知识库检索。可传 variants 数组(查询改写结果),服务端会用加权 RRF 融合多路结果,并用 MMR 去冗余。',
|
|
82
|
+
inputSchema: {
|
|
83
|
+
type: 'object',
|
|
84
|
+
properties: {
|
|
85
|
+
query: { type: 'string' },
|
|
86
|
+
variants: { type: 'array', items: { type: 'string' }, description: '同义改写/扩展查询' },
|
|
87
|
+
limit: { type: 'number' },
|
|
88
|
+
},
|
|
89
|
+
required: ['query'],
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
name: 'kb_get',
|
|
94
|
+
description: '读取单个分块全文。',
|
|
95
|
+
inputSchema: { type: 'object', properties: { chunk_id: { type: 'number' } }, required: ['chunk_id'] },
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
name: 'kb_edit',
|
|
99
|
+
description: '编辑分块正文,生成新版本。base_version 用于乐观锁:与当前版本不一致时拒绝。',
|
|
100
|
+
inputSchema: {
|
|
101
|
+
type: 'object',
|
|
102
|
+
properties: {
|
|
103
|
+
chunk_id: { type: 'number' },
|
|
104
|
+
text: { type: 'string' },
|
|
105
|
+
note: { type: 'string' },
|
|
106
|
+
base_version: { type: 'number' },
|
|
107
|
+
},
|
|
108
|
+
required: ['chunk_id', 'text'],
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
name: 'kb_history',
|
|
113
|
+
description: '分块版本历史。',
|
|
114
|
+
inputSchema: { type: 'object', properties: { chunk_id: { type: 'number' } }, required: ['chunk_id'] },
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
name: 'kb_diff',
|
|
118
|
+
description: '分块两版本逐行比对。',
|
|
119
|
+
inputSchema: {
|
|
120
|
+
type: 'object',
|
|
121
|
+
properties: { chunk_id: { type: 'number' }, from: { type: 'number' }, to: { type: 'number' } },
|
|
122
|
+
required: ['chunk_id', 'from'],
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
name: 'kb_rollback',
|
|
127
|
+
description: '把分块回滚到指定版本(生成新版本,不删除历史)。',
|
|
128
|
+
inputSchema: {
|
|
129
|
+
type: 'object',
|
|
130
|
+
properties: { chunk_id: { type: 'number' }, version: { type: 'number' } },
|
|
131
|
+
required: ['chunk_id', 'version'],
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
name: 'kb_tree',
|
|
136
|
+
description: '已入库文档的目录树(保留原始目录结构)。',
|
|
137
|
+
inputSchema: { type: 'object', properties: {} },
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
name: 'kb_ingest',
|
|
141
|
+
description: '导入一个目录(递归,保留相对路径),按三层策略自动分块。',
|
|
142
|
+
inputSchema: {
|
|
143
|
+
type: 'object',
|
|
144
|
+
properties: {
|
|
145
|
+
path: { type: 'string' },
|
|
146
|
+
chunk_size: { type: 'number' },
|
|
147
|
+
chunk_overlap: { type: 'number' },
|
|
148
|
+
strategy: { type: 'string', enum: ['auto', 'heading', 'heuristic', 'legacy'] },
|
|
149
|
+
},
|
|
150
|
+
required: ['path'],
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
name: 'memory_extract',
|
|
155
|
+
description: '自动提取:扫描 DSH 会话日志,把检测到的偏好/事实/画像写成「待确认」记忆。水位线去重,重复调用不会重复写入。',
|
|
156
|
+
inputSchema: {
|
|
157
|
+
type: 'object',
|
|
158
|
+
properties: {
|
|
159
|
+
sessions_dir: { type: 'string', description: '默认 ~/.dsh/sessions' },
|
|
160
|
+
limit: { type: 'number', description: '本轮最多新增条数,默认 30' },
|
|
161
|
+
dry_run: { type: 'boolean', description: '只看候选、不落库' },
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
name: 'memory_probe_llm',
|
|
167
|
+
description: '探测本服务进程能否调用 LLM。区分四种情况:config=没配置(缺 baseURL/model/key)、network=出网被拦、auth=凭据不对、ok=可用。用于决定走 LLM 蒸馏还是规则提取。',
|
|
168
|
+
inputSchema: { type: 'object', properties: {} },
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
name: 'memory_distill',
|
|
172
|
+
description: '用 LLM 蒸馏最近的用户发言,提取长期记忆并写入待确认队列(比规则提取准)。若探针返回 network/auth 失败,请改用 memory_extract 或直接 memory_remember。',
|
|
173
|
+
inputSchema: {
|
|
174
|
+
type: 'object',
|
|
175
|
+
properties: {
|
|
176
|
+
turns_limit: { type: 'number', description: '取最近多少条用户发言,默认 60' },
|
|
177
|
+
limit: { type: 'number', description: '最多写入条数,默认 30' },
|
|
178
|
+
dry_run: { type: 'boolean' },
|
|
179
|
+
sessions_dir: { type: 'string' },
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
];
|
|
184
|
+
|
|
185
|
+
async function callTool(name, a = {}) {
|
|
186
|
+
switch (name) {
|
|
187
|
+
case 'memory_recall': return recall(db, a.query || '', { budgetChars: a.budget_chars || 1200 });
|
|
188
|
+
case 'memory_remember': return remember(db, {
|
|
189
|
+
kind: a.kind, text: a.text, subject: a.subject || '',
|
|
190
|
+
status: a.status || 'pending', confidence: a.confidence ?? 0.6, source: 'mcp',
|
|
191
|
+
supersedes: a.supersedes ?? null,
|
|
192
|
+
});
|
|
193
|
+
case 'memory_confirm': return confirmMemory(db, Number(a.id));
|
|
194
|
+
case 'memory_reject': return rejectMemory(db, Number(a.id), { note: a.note || '' });
|
|
195
|
+
case 'memory_list': return listMemories(db, { status: a.status || null, kind: a.kind || null, limit: a.limit || 100 });
|
|
196
|
+
case 'memory_edit': return editMemory(db, Number(a.id), a.text, { note: a.note || '' });
|
|
197
|
+
case 'kb_search': {
|
|
198
|
+
const r = searchChunks(db, { query: a.query, variants: a.variants || [], limit: a.limit || 8 });
|
|
199
|
+
return {
|
|
200
|
+
fused: r.fused,
|
|
201
|
+
results: r.results.map((x) => ({
|
|
202
|
+
chunk_id: x.id, path: x.rel_path, ordinal: x.ordinal,
|
|
203
|
+
score: Number(x.score.toFixed(4)), heading: x.heading, text: x.text,
|
|
204
|
+
})),
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
case 'kb_get': return getChunk(db, Number(a.chunk_id));
|
|
208
|
+
case 'kb_edit': return editChunk(db, Number(a.chunk_id), a.text, {
|
|
209
|
+
note: a.note || '', baseVersion: a.base_version ?? null,
|
|
210
|
+
});
|
|
211
|
+
case 'kb_history': return chunkHistory(db, Number(a.chunk_id));
|
|
212
|
+
case 'kb_diff': {
|
|
213
|
+
const va = chunkVersionText(db, Number(a.chunk_id), Number(a.from));
|
|
214
|
+
const vb = a.to ? chunkVersionText(db, Number(a.chunk_id), Number(a.to)) : getChunk(db, Number(a.chunk_id));
|
|
215
|
+
if (!va || !vb) throw new Error('版本不存在');
|
|
216
|
+
return diffTexts(va.text, vb.text);
|
|
217
|
+
}
|
|
218
|
+
case 'kb_rollback': return rollbackChunk(db, Number(a.chunk_id), Number(a.version));
|
|
219
|
+
case 'kb_tree': return { tree: tree(db) };
|
|
220
|
+
case 'kb_ingest': return ingestFolder(db, a.path, {
|
|
221
|
+
chunk_size: a.chunk_size ?? 512,
|
|
222
|
+
chunk_overlap: a.chunk_overlap ?? 80,
|
|
223
|
+
strategy: a.strategy || 'auto',
|
|
224
|
+
});
|
|
225
|
+
case 'memory_extract': return runAutoExtract(db, {
|
|
226
|
+
sessionsDir: a.sessions_dir || undefined,
|
|
227
|
+
limit: a.limit ?? 30,
|
|
228
|
+
dryRun: !!a.dry_run,
|
|
229
|
+
});
|
|
230
|
+
case 'memory_probe_llm': return await llmProbe();
|
|
231
|
+
case 'memory_distill': {
|
|
232
|
+
const turns = Array.isArray(a.turns) && a.turns.length
|
|
233
|
+
? a.turns.map((t) => (typeof t === 'string' ? { text: t } : t))
|
|
234
|
+
: recentTurns(a.sessions_dir || undefined, a.turns_limit ?? 60);
|
|
235
|
+
if (!turns.length) return { candidates: [], added: [], note: '没有可蒸馏的用户发言' };
|
|
236
|
+
return await llmDistill(db, { turns, limit: a.limit ?? 30, dryRun: !!a.dry_run });
|
|
237
|
+
}
|
|
238
|
+
default: throw new Error(`unknown tool: ${name}`);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function send(msg) {
|
|
243
|
+
process.stdout.write(JSON.stringify(msg) + '\n');
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
async function handle(req) {
|
|
247
|
+
const { id, method, params } = req;
|
|
248
|
+
if (method === undefined) return; // notification
|
|
249
|
+
try {
|
|
250
|
+
if (method === 'initialize') {
|
|
251
|
+
send({
|
|
252
|
+
jsonrpc: '2.0', id,
|
|
253
|
+
result: {
|
|
254
|
+
protocolVersion: PROTOCOL,
|
|
255
|
+
capabilities: { tools: { listChanged: false } },
|
|
256
|
+
serverInfo: SERVER,
|
|
257
|
+
},
|
|
258
|
+
});
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
if (method === 'tools/list') {
|
|
262
|
+
send({ jsonrpc: '2.0', id, result: { tools: TOOLS } });
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
if (method === 'tools/call') {
|
|
266
|
+
const { name, arguments: args } = params || {};
|
|
267
|
+
try {
|
|
268
|
+
const data = await callTool(name, args || {});
|
|
269
|
+
send({
|
|
270
|
+
jsonrpc: '2.0', id,
|
|
271
|
+
result: { content: [{ type: 'text', text: JSON.stringify(data, null, 2) }] },
|
|
272
|
+
});
|
|
273
|
+
} catch (e) {
|
|
274
|
+
send({
|
|
275
|
+
jsonrpc: '2.0', id,
|
|
276
|
+
result: { content: [{ type: 'text', text: 'ERROR: ' + (e.message || e) }], isError: true },
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
if (method === 'ping') { send({ jsonrpc: '2.0', id, result: {} }); return; }
|
|
282
|
+
send({ jsonrpc: '2.0', id, error: { code: -32601, message: `method not found: ${method}` } });
|
|
283
|
+
} catch (e) {
|
|
284
|
+
send({ jsonrpc: '2.0', id, error: { code: -32603, message: String(e.message || e) } });
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const rl = readline.createInterface({ input: process.stdin, terminal: false });
|
|
289
|
+
let queue = Promise.resolve();
|
|
290
|
+
rl.on('line', (line) => {
|
|
291
|
+
const s = line.trim();
|
|
292
|
+
if (!s) return;
|
|
293
|
+
let req;
|
|
294
|
+
try { req = JSON.parse(s); } catch { return; }
|
|
295
|
+
const run = async () => { if (Array.isArray(req)) { for (const r of req) await handle(r); } else await handle(req); };
|
|
296
|
+
queue = queue.then(run).catch(() => {});
|
|
297
|
+
});
|
|
298
|
+
process.on('SIGINT', () => process.exit(0));
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-cooper-memory",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Cross-session memory and a folder knowledge base for DeepSeek Harness — local SQLite, no Docker, no vector service.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"engines": { "node": ">=22.5" },
|
|
8
|
+
"keywords": ["dsh", "dsh-plugin", "memory", "knowledge-base", "sqlite", "mcp", "rag", "cross-session"],
|
|
9
|
+
"bin": {
|
|
10
|
+
"dsh-cooper-memory": "./cli.mjs",
|
|
11
|
+
"dsh-cooper-memory-web": "./web.mjs",
|
|
12
|
+
"dsh-cooper-memory-mcp": "./mcp.mjs"
|
|
13
|
+
},
|
|
14
|
+
"exports": {
|
|
15
|
+
".": "./mcp.mjs",
|
|
16
|
+
"./cli": "./cli.mjs",
|
|
17
|
+
"./web": "./web.mjs",
|
|
18
|
+
"./lib/*": "./lib/*",
|
|
19
|
+
"./locale/*.json": "./locale/*.json",
|
|
20
|
+
"./icon": "./icon.svg"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"mcp.mjs",
|
|
24
|
+
"cli.mjs",
|
|
25
|
+
"web.mjs",
|
|
26
|
+
"lib",
|
|
27
|
+
"cordis.patch.yml",
|
|
28
|
+
"locale/*.json",
|
|
29
|
+
"docs",
|
|
30
|
+
"icon.svg",
|
|
31
|
+
"README.md",
|
|
32
|
+
"LICENSE",
|
|
33
|
+
"CHANGELOG.md"
|
|
34
|
+
],
|
|
35
|
+
"scripts": { "test": "node test/run.mjs" },
|
|
36
|
+
"dsh": {
|
|
37
|
+
"bundle": { "patch": "./cordis.patch.yml" }
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": { "access": "public", "registry": "https://registry.npmjs.org/" }
|
|
6
40
|
}
|