sage-mem 2.25.0 → 2.26.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/README.en.md +34 -1
- package/README.md +34 -1
- package/lib/assembly/mirror-mount.js +14 -3
- package/lib/client.js +269 -0
- package/lib/index.js +19 -0
- package/lib/intake/merge.js +193 -0
- package/lib/intake/policy.js +238 -0
- package/lib/intake/store.js +247 -0
- package/lib/mirror/mcp.js +53 -6
- package/lib/mirror/policy.js +17 -6
- package/lib/typert/schemas.js +47 -0
- package/lib/typert.host.js +39 -1
- package/package.json +1 -1
package/lib/mirror/mcp.js
CHANGED
|
@@ -19,6 +19,7 @@ import { buildStats, queryTermsOf, scoreDoc, termFreq, tokenize } from '../memor
|
|
|
19
19
|
import { stripFrontmatterBlock } from '../memory/frontmatter.js'
|
|
20
20
|
import { warnLog, writeFileAtomic } from '../memory/util.js'
|
|
21
21
|
import { MIRROR_MCP_NAME, isMirrorReadableName } from './policy.js'
|
|
22
|
+
import { validateSubmission } from '../intake/policy.js'
|
|
22
23
|
import { currentVersion } from '../update.js'
|
|
23
24
|
|
|
24
25
|
/** 认识且愿意应答的协议版本(按新到旧)。客户端报别的 → 回落最新一个。 */
|
|
@@ -59,13 +60,40 @@ const TOOL_DEFS = Object.freeze([
|
|
|
59
60
|
description: '列出副本里全部可读条目(名字 + 一句话 + 类型 + 标签)。只读。',
|
|
60
61
|
inputSchema: { type: 'object', properties: {} },
|
|
61
62
|
},
|
|
63
|
+
{
|
|
64
|
+
// 2.26.0:写侧的唯一入口 —— 它不写记忆库,只把提交放进归并队列。
|
|
65
|
+
name: 'memory_submit',
|
|
66
|
+
description: '向用户提交一条记忆(进入归并队列,由用户逐条批准后才可能进主库;不会直接写入记忆库)。kind=new 提交一条完整的新记忆(只接受 type=project/reference);kind=patch 提交对现有条目的修改建议(永不自动写入)。格式见 initialize 的 instructions。',
|
|
67
|
+
inputSchema: {
|
|
68
|
+
type: 'object',
|
|
69
|
+
properties: {
|
|
70
|
+
kind: { type: 'string', enum: ['new', 'patch'], description: 'new = 新记忆;patch = 对现有条目的修改建议' },
|
|
71
|
+
source: { type: 'string', description: '你的名字,小写字母 / 数字 / 连字符,如 claude-code' },
|
|
72
|
+
name: { type: 'string', description: 'new:文件名(不带 .md,必须以 project_ 或 reference_ 开头);patch:目标条目的名字' },
|
|
73
|
+
description: { type: 'string', description: 'new 必填:一句话说明这条记忆是什么' },
|
|
74
|
+
type: { type: 'string', enum: ['project', 'reference'], description: 'new 必填:只接受 project / reference' },
|
|
75
|
+
tags: { type: 'array', items: { type: 'string' }, description: 'new 可选:最多 8 个标签' },
|
|
76
|
+
body: { type: 'string', description: 'new 必填:正文 markdown(不要自带 frontmatter)' },
|
|
77
|
+
changes: { type: 'string', description: 'patch 必填:要改的地方(引用原文片段)' },
|
|
78
|
+
suggested: { type: 'string', description: 'patch 必填:建议改成什么' },
|
|
79
|
+
reason: { type: 'string', description: 'patch 可选:为什么' },
|
|
80
|
+
},
|
|
81
|
+
required: ['kind', 'source', 'name'],
|
|
82
|
+
},
|
|
83
|
+
},
|
|
62
84
|
])
|
|
63
85
|
|
|
64
86
|
const INSTRUCTIONS = [
|
|
65
|
-
'
|
|
66
|
-
'可用工具:memory_list(有哪些)/ memory_search(按关键词找)/ memory_read
|
|
87
|
+
'这是一个记忆库端点:默认只能**读**(读的是主记忆库按白名单导出的副本)。',
|
|
88
|
+
'可用工具:memory_list(有哪些)/ memory_search(按关键词找)/ memory_read(读全文)/ memory_submit(提交一条记忆或修改建议)。',
|
|
67
89
|
'引用某条记忆时请带上它的 name(如 project_xxx.md),不要假设文件路径 —— 这里不提供路径。',
|
|
68
|
-
'
|
|
90
|
+
'',
|
|
91
|
+
'## memory_submit 的格式',
|
|
92
|
+
'- 提交**不会**直接写入用户的记忆库:它进入归并队列,由用户逐条批准。',
|
|
93
|
+
'- kind=new:必填 source / name / description / type / body;type 只能是 project 或 reference,且 name 要以对应的 project_ / reference_ 开头。正文不要自带 frontmatter。',
|
|
94
|
+
'- kind=patch:必填 source / name / changes / suggested;name 是**现有条目**的名字(不带 .md)。patch 永远不会自动写入,用户只会看到一份建议。',
|
|
95
|
+
'- 体积上限:正文 32 KB,整条 64 KB。格式不对会被当场拒绝(isError 里写明原因)。',
|
|
96
|
+
'- source 是你的**声明**(如 claude-code),不是凭证。',
|
|
69
97
|
].join('\n')
|
|
70
98
|
|
|
71
99
|
/** 正文 → 片段:去掉 frontmatter、压平空白、截断。 */
|
|
@@ -90,7 +118,7 @@ function textResult(value) {
|
|
|
90
118
|
* @param {object} opts.ctx — 宿主上下文(只为 `warnLog`)
|
|
91
119
|
* @param {object} opts.exporter — `createMirrorExporter()` 的产物(只读面)
|
|
92
120
|
*/
|
|
93
|
-
export function createMirrorMcpServer({ ctx, exporter }) {
|
|
121
|
+
export function createMirrorMcpServer({ ctx, exporter, intake }) {
|
|
94
122
|
let server = null
|
|
95
123
|
let listening = false
|
|
96
124
|
let boundPort = 0
|
|
@@ -110,7 +138,9 @@ export function createMirrorMcpServer({ ctx, exporter }) {
|
|
|
110
138
|
url: url(),
|
|
111
139
|
transport: 'http',
|
|
112
140
|
tools: TOOL_DEFS.map((t) => t.name),
|
|
141
|
+
// 对**记忆库**只读;提交只进归并队列,不写主库(2.26.0)。
|
|
113
142
|
readOnly: true,
|
|
143
|
+
acceptsSubmissions: true,
|
|
114
144
|
generatedAt: new Date().toISOString(),
|
|
115
145
|
}
|
|
116
146
|
try {
|
|
@@ -176,8 +206,25 @@ export function createMirrorMcpServer({ ctx, exporter }) {
|
|
|
176
206
|
})
|
|
177
207
|
}
|
|
178
208
|
|
|
179
|
-
/**
|
|
180
|
-
|
|
209
|
+
/**
|
|
210
|
+
* 提交一条记忆(2.26.0)。**全端点唯一的写入口**,而且它只写 intake/ —— 主库要人批准。
|
|
211
|
+
*
|
|
212
|
+
* 两道闸:开关(用户是否允许接收)+ 校验(格式不对当场拒,连 pending 都进不去)。
|
|
213
|
+
*/
|
|
214
|
+
async function toolSubmit(args) {
|
|
215
|
+
if (!intake || !intake.store) return toolError('这个端点没有开启接收提交')
|
|
216
|
+
if (!intake.acceptSubmissions()) {
|
|
217
|
+
return toolError('用户没有打开「接收外来提交」(external.acceptSubmissions=0)。请把这条内容直接告诉用户,或请他们开启后再提交。')
|
|
218
|
+
}
|
|
219
|
+
const check = validateSubmission(args)
|
|
220
|
+
if (!check.ok) return toolError(`提交被拒:${check.error}`)
|
|
221
|
+
const saved = await intake.store.save(check.submission)
|
|
222
|
+
if (!saved.ok) return toolError(`提交没落盘:${saved.error}`)
|
|
223
|
+
return textResult({ ok: true, id: saved.id, status: 'pending', note: '已进入归并队列,等待用户批准;不会直接写入记忆库' })
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** 工具名 → 实现。读的三个 + 一个只写 intake 的提交口。 */
|
|
227
|
+
const TOOLS = Object.freeze({ memory_search: toolSearch, memory_read: toolRead, memory_list: toolList, memory_submit: toolSubmit })
|
|
181
228
|
|
|
182
229
|
// ── JSON-RPC 分派 ──────────────────────────────────────────────────
|
|
183
230
|
|
package/lib/mirror/policy.js
CHANGED
|
@@ -34,6 +34,8 @@ export const MIRROR_DEBOUNCE_MS = 10_000
|
|
|
34
34
|
*/
|
|
35
35
|
export const MIRROR_SPECS = Object.freeze({
|
|
36
36
|
enabled: { env: 'SAGE_MEM_EXTERNAL', dflt: 0, lo: 0, hi: 1 },
|
|
37
|
+
// 2.26.0:收不收外来提交。与 nabled 是两件事 —— 「能不能读副本」不等于「能不能往里塞东西」。
|
|
38
|
+
acceptSubmissions: { env: 'SAGE_MEM_EXTERNAL_SUBMIT', dflt: 0, lo: 0, hi: 1 },
|
|
37
39
|
port: { env: 'SAGE_MEM_EXTERNAL_PORT', dflt: 7461, lo: 1024, hi: 65535 },
|
|
38
40
|
pollSeconds: { env: 'SAGE_MEM_EXTERNAL_POLL', dflt: 45, lo: 15, hi: 600 },
|
|
39
41
|
})
|
|
@@ -53,6 +55,7 @@ export const DEFAULT_PREFIXES = Object.freeze(['project_', 'reference_'])
|
|
|
53
55
|
*/
|
|
54
56
|
export const DEFAULT_EXTERNAL = Object.freeze({
|
|
55
57
|
enabled: MIRROR_SPECS.enabled.dflt,
|
|
58
|
+
acceptSubmissions: MIRROR_SPECS.acceptSubmissions.dflt,
|
|
56
59
|
prefixes: DEFAULT_PREFIXES,
|
|
57
60
|
tags: [],
|
|
58
61
|
include: [],
|
|
@@ -108,10 +111,18 @@ function normalizeList(value, kind) {
|
|
|
108
111
|
return { list: out, dropped }
|
|
109
112
|
}
|
|
110
113
|
|
|
111
|
-
/**
|
|
114
|
+
/**
|
|
115
|
+
* 0/1 型开关的两个键:`enabled`(读侧端点)与 `acceptSubmissions`(收不收外来提交)。
|
|
116
|
+
* 归一与严格校验都走同一套 —— 两个开关的语义不同,但**取值范围与笔误形态完全一样**。
|
|
117
|
+
*/
|
|
118
|
+
const FLAG_KEYS = new Set(['enabled', 'acceptSubmissions'])
|
|
119
|
+
|
|
120
|
+
/** 开关收数字或布尔(手改 JSON 时写成 `true` 是常见笔误,不该让整份设置失效)。 */
|
|
112
121
|
function normalizeEnabled(raw) {
|
|
113
122
|
if (typeof raw === 'boolean') return raw ? 1 : 0
|
|
114
|
-
|
|
123
|
+
// 两个开关的取值范围都是 {0, 1};这里不借用任何一个 key 的 spec,免得日后某个开关改了边界
|
|
124
|
+
// 把另一个开关的语义悄悄带走。
|
|
125
|
+
return parseLimitValue(raw, { lo: 0, hi: 1 })
|
|
115
126
|
}
|
|
116
127
|
|
|
117
128
|
/**
|
|
@@ -131,10 +142,10 @@ export function resolveExternal(saved, env = process.env) {
|
|
|
131
142
|
const external = { ...DEFAULT_EXTERNAL, prefixes: [...DEFAULT_PREFIXES], tags: [], include: [], exclude: [] }
|
|
132
143
|
|
|
133
144
|
// ── 三个整数项 ─────────────────────────────────────────────────────
|
|
134
|
-
for (const key of ['enabled', 'port', 'pollSeconds']) {
|
|
145
|
+
for (const key of ['enabled', 'acceptSubmissions', 'port', 'pollSeconds']) {
|
|
135
146
|
const spec = MIRROR_SPECS[key]
|
|
136
147
|
if (savedObj !== null && Object.prototype.hasOwnProperty.call(savedObj, key)) {
|
|
137
|
-
const value = key
|
|
148
|
+
const value = FLAG_KEYS.has(key) ? normalizeEnabled(savedObj[key]) : parseLimitValue(savedObj[key], spec)
|
|
138
149
|
if (value !== undefined) {
|
|
139
150
|
external[key] = value
|
|
140
151
|
// 手写成 `true` / `"1"` 是合法的(归一成功),但写成 `" 1 "` 这种「看着像、其实被收敛过」
|
|
@@ -146,7 +157,7 @@ export function resolveExternal(saved, env = process.env) {
|
|
|
146
157
|
}
|
|
147
158
|
diagnostics.push(`settings.json 的 external.${key}=${JSON.stringify(savedObj[key])} 不是有效数字,已回落环境变量/默认值`)
|
|
148
159
|
}
|
|
149
|
-
const envValue = key
|
|
160
|
+
const envValue = FLAG_KEYS.has(key) ? normalizeEnabled(env?.[spec.env]) : parseLimitValue(env?.[spec.env], spec)
|
|
150
161
|
external[key] = envValue === undefined ? spec.dflt : envValue
|
|
151
162
|
}
|
|
152
163
|
|
|
@@ -179,7 +190,7 @@ export function parseExternalPatch(external) {
|
|
|
179
190
|
}
|
|
180
191
|
const patch = {}
|
|
181
192
|
for (const [key, value] of Object.entries(external)) {
|
|
182
|
-
if (key
|
|
193
|
+
if (FLAG_KEYS.has(key)) {
|
|
183
194
|
// ⚠️ 这里**不用** `parseLimitValue`:那条路的语义是「越界收敛到边界」,
|
|
184
195
|
// 而补丁的语义是「笔误就报错」。用前者会让 `enabled: 2` 静默变成 1。
|
|
185
196
|
if (value === true || value === 1) { patch.enabled = 1; continue }
|
package/lib/typert/schemas.js
CHANGED
|
@@ -308,6 +308,8 @@ export const settingsUiSchema = z.object({ 'entry': z.enum(['both', 'sidebar', '
|
|
|
308
308
|
// `resolveExternal`),这里只声明传输形状;`settingsPatchSchema` 里还有一份可选字段版。
|
|
309
309
|
export const settingsExternalSchema = z.object({
|
|
310
310
|
'enabled': z.number().readonly(),
|
|
311
|
+
// 2.26.0:收不收外来提交(与 enabled 是两件事)。
|
|
312
|
+
'acceptSubmissions': z.number().readonly(),
|
|
311
313
|
'prefixes': z.array(z.string()).readonly(),
|
|
312
314
|
'tags': z.array(z.string()).readonly(),
|
|
313
315
|
'include': z.array(z.string()).readonly(),
|
|
@@ -351,6 +353,7 @@ export const settingsPatchSchema = z.object({
|
|
|
351
353
|
// 没碰过的字段不该被旧值覆盖(与 limits / ui / experimental 同一套语义)。
|
|
352
354
|
'external': z.object({
|
|
353
355
|
'enabled': z.number().optional(),
|
|
356
|
+
'acceptSubmissions': z.number().optional(),
|
|
354
357
|
'prefixes': z.array(z.string()).optional(),
|
|
355
358
|
'tags': z.array(z.string()).optional(),
|
|
356
359
|
'include': z.array(z.string()).optional(),
|
|
@@ -399,6 +402,50 @@ export const externalStatusSchema = z.object({
|
|
|
399
402
|
}).readonly(),
|
|
400
403
|
}).readonly()
|
|
401
404
|
|
|
405
|
+
// ── 2.26.0 外部来件(写侧)────────────────────────────────────────────────
|
|
406
|
+
/** 提交 id(形如 20261201-183000-1abc)。不是文件名,所以不走 fileNameSchema 的正则。 */
|
|
407
|
+
export const subIdSchema = z.string().max(80)
|
|
408
|
+
/** 拒绝理由(人读的一行)。 */
|
|
409
|
+
export const subReasonSchema = z.string().max(1000)
|
|
410
|
+
// 一条待处理提交 + 它的提案。`action` 是**现算**的(主库随时会变),所以每次读都是一份新判断。
|
|
411
|
+
export const intakeProposalItemSchema = z.object({
|
|
412
|
+
'id': z.string().readonly(),
|
|
413
|
+
'source': z.string().readonly(),
|
|
414
|
+
'kind': z.string().readonly(),
|
|
415
|
+
'name': z.string().readonly(),
|
|
416
|
+
'description': z.string().readonly(),
|
|
417
|
+
'type': z.string().readonly(),
|
|
418
|
+
'submittedAt': z.string().readonly(),
|
|
419
|
+
'action': z.enum(['create', 'conflict', 'manual', 'reject']).readonly(),
|
|
420
|
+
'targetFile': z.string().readonly(),
|
|
421
|
+
'reason': z.string().readonly(),
|
|
422
|
+
'excerpt': z.string().readonly(),
|
|
423
|
+
// patch 才有内容:面板「复制建议」复制的就是这一段(空串 = 没有)。
|
|
424
|
+
'suggestion': z.string().readonly(),
|
|
425
|
+
}).readonly()
|
|
426
|
+
|
|
427
|
+
export const intakeOverviewSchema = z.object({
|
|
428
|
+
'pending': z.number().readonly(),
|
|
429
|
+
'items': z.array(intakeProposalItemSchema).readonly(),
|
|
430
|
+
// 主库读不出来时这里非空 —— 面板据此禁用「批准」,而不是假装没有冲突。
|
|
431
|
+
'scanError': z.string().readonly(),
|
|
432
|
+
'reports': z.array(z.object({ 'name': z.string().readonly(), 'text': z.string().readonly() })).readonly(),
|
|
433
|
+
}).readonly()
|
|
434
|
+
|
|
435
|
+
export const intakeApproveResultSchema = z.object({
|
|
436
|
+
'ok': z.boolean().readonly(),
|
|
437
|
+
'action': z.string().readonly().optional(),
|
|
438
|
+
'targetFile': z.string().readonly().optional(),
|
|
439
|
+
'error': z.string().readonly().optional(),
|
|
440
|
+
'report': z.string().readonly().optional(),
|
|
441
|
+
}).readonly()
|
|
442
|
+
|
|
443
|
+
export const intakeRejectResultSchema = z.object({
|
|
444
|
+
'ok': z.boolean().readonly(),
|
|
445
|
+
'id': z.string().readonly().optional(),
|
|
446
|
+
'error': z.string().readonly().optional(),
|
|
447
|
+
}).readonly()
|
|
448
|
+
|
|
402
449
|
// 保留名原样读写。读的两条分支(成功 {name,content} / 拒绝 {ok:false,error})用可选字段
|
|
403
450
|
// 的并集接住;写的失败分支可能没有 file,所以 file 也是 optional。
|
|
404
451
|
export const rawReadResultSchema = z.object({
|
package/lib/typert.host.js
CHANGED
|
@@ -19,7 +19,7 @@ import {
|
|
|
19
19
|
archiveResultSchema, archivedReadResultSchema, archivedWriteResultSchema, updateCheckSchema, auditItemSchema, auditLinkSchema,
|
|
20
20
|
auditTypeMismatchSchema, auditHazardSchema, auditOrphanSchema, auditIndexProposalSchema, auditReportSchema, openFolderResultSchema,
|
|
21
21
|
archiveCandidateSchema, archiveCandidatesResultSchema, lockResultSchema, settingsLimitsSchema, settingsUiSchema, settingsSchema,
|
|
22
|
-
settingsPatchSchema, settingsResultSchema, externalStatusSchema, rawReadResultSchema, rawWriteResultSchema, rawFileContentSchema, autodreamConfigSchema,
|
|
22
|
+
settingsPatchSchema, settingsResultSchema, externalStatusSchema, intakeOverviewSchema, intakeApproveResultSchema, intakeRejectResultSchema, subIdSchema, subReasonSchema, rawReadResultSchema, rawWriteResultSchema, rawFileContentSchema, autodreamConfigSchema,
|
|
23
23
|
autodreamPathsSchema, autodreamRouteSchema, autodreamLastResultSchema, autodreamConfigResultSchema, autodreamStepSchema, autodreamStatusSchema,
|
|
24
24
|
autodreamRunResultSchema, autodreamPatchSchema, reportListSchema, snapshotListSchema, runListSchema, declarationSchema,
|
|
25
25
|
rollbackResultSchema, modelListSchema, autodreamOptsSchema, conflictIdSchema, conflictOptsSchema, conflictItemBase,
|
|
@@ -249,6 +249,44 @@ export const TYPERT = {
|
|
|
249
249
|
result: { mode: 'strict', typeSymbol: 'sage-mem/types#MemorySettingsResult', schema: settingsResultSchema, create: () => settingsResultSchema },
|
|
250
250
|
sourceLocation: { "file": "lib/index.js", "line": 1, "column": 1 },
|
|
251
251
|
},
|
|
252
|
+
{
|
|
253
|
+
// 2.26.0:外部来件(写侧)—— 待处理提交 + 每条现算的提案。只读。
|
|
254
|
+
id: 'sage-mem#memory/externalSubmissions',
|
|
255
|
+
service: 'memory',
|
|
256
|
+
namespace: 'memory',
|
|
257
|
+
method: 'externalSubmissions',
|
|
258
|
+
invocation: { kind: 'direct' },
|
|
259
|
+
parameters: [],
|
|
260
|
+
result: { mode: 'strict', typeSymbol: 'sage-mem/types#IntakeOverview', schema: intakeOverviewSchema, create: () => intakeOverviewSchema },
|
|
261
|
+
sourceLocation: { "file": "lib/index.js", "line": 1, "column": 1 },
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
// 批准:只对 `create` 生效;其余动作会回 {ok:false, action} 说明原因。
|
|
265
|
+
id: 'sage-mem#memory/intakeApprove',
|
|
266
|
+
service: 'memory',
|
|
267
|
+
namespace: 'memory',
|
|
268
|
+
method: 'intakeApprove',
|
|
269
|
+
invocation: { kind: 'direct' },
|
|
270
|
+
parameters: [
|
|
271
|
+
{ name: 'id', wire: 'id', source: 'json', codec: { mode: 'strict', typeSymbol: 'sage-mem/types#SubId', schema: subIdSchema, create: () => subIdSchema } },
|
|
272
|
+
],
|
|
273
|
+
result: { mode: 'strict', typeSymbol: 'sage-mem/types#IntakeApproveResult', schema: intakeApproveResultSchema, create: () => intakeApproveResultSchema },
|
|
274
|
+
sourceLocation: { "file": "lib/index.js", "line": 1, "column": 1 },
|
|
275
|
+
},
|
|
276
|
+
{
|
|
277
|
+
// 拒绝:原件挪进 rejected/(不删),理由进报告与审计。
|
|
278
|
+
id: 'sage-mem#memory/intakeReject',
|
|
279
|
+
service: 'memory',
|
|
280
|
+
namespace: 'memory',
|
|
281
|
+
method: 'intakeReject',
|
|
282
|
+
invocation: { kind: 'direct' },
|
|
283
|
+
parameters: [
|
|
284
|
+
{ name: 'id', wire: 'id', source: 'json', codec: { mode: 'strict', typeSymbol: 'sage-mem/types#SubId', schema: subIdSchema, create: () => subIdSchema } },
|
|
285
|
+
{ name: 'reason', wire: 'reason', source: 'json', acceptsUndefined: true, codec: { mode: 'strict', typeSymbol: 'sage-mem/types#SubReason', schema: subIdSchema, create: () => subIdSchema } },
|
|
286
|
+
],
|
|
287
|
+
result: { mode: 'strict', typeSymbol: 'sage-mem/types#IntakeRejectResult', schema: intakeRejectResultSchema, create: () => intakeRejectResultSchema },
|
|
288
|
+
sourceLocation: { "file": "lib/index.js", "line": 1, "column": 1 },
|
|
289
|
+
},
|
|
252
290
|
{
|
|
253
291
|
// 2.25.0:外部访问读侧的运行状态(只读、无入参)。设置走 setSettings({ external }),
|
|
254
292
|
// 这里只回答「现在跑成什么样」—— 暴露条目数、最近一轮的时间与错误、端点是否在听。
|
package/package.json
CHANGED