dsh-mindmap 0.11.0 → 0.13.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/CHANGELOG.md +39 -3
- package/README.md +10 -8
- package/README.zh-CN.md +10 -8
- package/client.js +776 -79
- package/index.js +196 -27
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -9,13 +9,14 @@
|
|
|
9
9
|
// - 四工具都带 path/name 参数(决策 3:多脑图并存,作用于指定那颗)。
|
|
10
10
|
// - 结果 JSON {ok, op, path, rootTitle, content, renamedFrom?}:content 全文
|
|
11
11
|
// 供模型续编辑,client 用同一份重放面板(工具结果即实时通道,002 第二节)。
|
|
12
|
-
// - requireApproval 配置(决策 6):默认 true
|
|
13
|
-
//
|
|
14
|
-
//
|
|
12
|
+
// - requireApproval 配置(决策 6):默认 true;approvalMode 默认按「当前会话 + 当前文件」
|
|
13
|
+
// 首次确认,后续普通更新复用授权;per-operation 可恢复每次确认,off 关闭普通确认。
|
|
14
|
+
// 重命名、清空和大范围重写始终重新确认,关闭普通确认也不能跳过。
|
|
15
|
+
// 015 起经 settings namespace 可运行时切换。
|
|
15
16
|
// - 依赖:仅 @deepseek-ai/schemastery(settings schema;发布包正常解析,
|
|
16
17
|
// link 开发需先 npm i)。工具参数 schema 仍手写 JSON Schema(003 偏差 1)。
|
|
17
18
|
import { access, open, opendir, realpath, rename, stat, unlink, writeFile } from 'node:fs/promises'
|
|
18
|
-
import { randomUUID } from 'node:crypto'
|
|
19
|
+
import { createHash, randomUUID } from 'node:crypto'
|
|
19
20
|
import { dirname, isAbsolute, join, relative, resolve as resolvePath, sep } from 'node:path'
|
|
20
21
|
import Schema from '@deepseek-ai/schemastery'
|
|
21
22
|
|
|
@@ -23,11 +24,23 @@ export const name = 'mindmap'
|
|
|
23
24
|
export const inject = ['tools', 'systemPrompt', 'webServer', 'sessions']
|
|
24
25
|
|
|
25
26
|
// 015 设置面板:settings namespace(dsh-grafana 同款模式)。
|
|
26
|
-
// requireApproval 在 pre-execute 时读当前值(运行时切换即时生效);
|
|
27
|
+
// requireApproval / approvalMode 在 pre-execute 时读当前值(运行时切换即时生效);
|
|
27
28
|
// defaultPanelWidth 供客户端面板取默认宽度(20-80 钳制由客户端执行)。
|
|
28
29
|
export const SETTINGS_NAMESPACE = 'mindmap'
|
|
30
|
+
const APPROVAL_MODES = ['per-operation', 'session', 'off']
|
|
31
|
+
|
|
32
|
+
/** Normalize the public three-mode policy while accepting the previous names. */
|
|
33
|
+
function normalizeApprovalMode(value, requireApproval = true) {
|
|
34
|
+
if (requireApproval === false) return 'off'
|
|
35
|
+
if (value === 'per-operation' || value === 'always') return 'per-operation'
|
|
36
|
+
if (value === 'off') return 'off'
|
|
37
|
+
if (value === 'session' || value === 'once-per-document') return 'session'
|
|
38
|
+
return 'session'
|
|
39
|
+
}
|
|
40
|
+
|
|
29
41
|
export const Config = Schema.object({
|
|
30
|
-
requireApproval: Schema.boolean().default(true).description('
|
|
42
|
+
requireApproval: Schema.boolean().default(true).description('Legacy switch: false skips ordinary confirmations only, and is lifted when the settings panel picks an explicit approval mode. Rename, clearing content, and broad rewrites always require approval.'),
|
|
43
|
+
approvalMode: Schema.union([...APPROVAL_MODES, 'once-per-document', 'always']).default('session').description('Confirm every ordinary write, once per document in the current session, or disable ordinary confirmations. High-risk writes always require approval.'),
|
|
31
44
|
defaultPanelWidth: Schema.number().default(42).description('Default floating-panel width as a percentage of the viewport (clamped 20-80 on the client).'),
|
|
32
45
|
lineStyle: Schema.union(['curve', 'elbow']).default('elbow').description('Connector line style between nodes: curve (bezier) or elbow (orthogonal).'),
|
|
33
46
|
cardStyle: Schema.union(['rounded', 'square']).default('rounded').description('Node card corner style.'),
|
|
@@ -41,25 +54,30 @@ const MAX_NAME_CHARS = 80
|
|
|
41
54
|
const TOOL_TIMEOUT_MS = 15_000
|
|
42
55
|
const MAX_TREE_ENTRIES = 500
|
|
43
56
|
const MAX_BODY_BYTES = 1 << 20
|
|
57
|
+
const BROAD_REWRITE_MIN_BYTES = 16 * 1024
|
|
58
|
+
// 比例判定只对足够大的文档生效:几百字节的脑图整体改写不算「大范围重写」,
|
|
59
|
+
// 否则小文档的每次更新都会被升级成高风险确认,会话授权形同虚设。
|
|
60
|
+
const BROAD_REWRITE_RATIO_MIN_BYTES = 4 * 1024
|
|
44
61
|
|
|
45
62
|
const GUIDANCE = `## Mindmap editing (dsh-mindmap)
|
|
46
63
|
|
|
47
64
|
A mindmap is a plain markdown file in the session working directory. The right-side panel renders it live; the filename (without .md) is the root node title. These files are ordinary documents: the user reviews and commits them with git themselves.
|
|
48
65
|
|
|
49
66
|
Tools:
|
|
50
|
-
- mindmap_create(name): create <name>.md in the working directory (fails if it exists) and show it in the panel.
|
|
67
|
+
- mindmap_create(name, directory?): create <name>.md in the working directory or the selected relative directory (fails if it exists) and show it in the panel.
|
|
51
68
|
- mindmap_open(path): open an existing .md as a mindmap in the panel.
|
|
52
69
|
- mindmap_get(path): read the current markdown content.
|
|
53
|
-
- mindmap_update(path, content, renameRoot?): write the FULL updated markdown. renameRoot renames the file to match a new root title (fails on name collision); use it only when the user asks to rename the root node.
|
|
70
|
+
- mindmap_update(path, content, renameRoot?, expectedRevision?): write the FULL updated markdown. Pass the revision returned by mindmap_get/open when editing; a mismatch stops the write instead of overwriting newer changes. renameRoot renames the file to match a new root title (fails on name collision); use it only when the user asks to rename the root node.
|
|
54
71
|
|
|
55
|
-
Markdown mapping (the panel's parser): headings nest by level (H1 are root children, H2 under the previous H1, ...); list items are child nodes nested by 2-space indentation; a list item with no text after the marker renders as a placeholder node (both "-" and "- " work; no trailing space is required) — use placeholders for planned-but-unwritten nodes; a fenced code block becomes a leaf node titled "[lang] first line"; plain paragraphs become
|
|
72
|
+
Markdown mapping (the panel's parser): headings nest by level (H1 are root children, H2 under the previous H1, ...); list items are child nodes nested by 2-space indentation; a list item with no text after the marker renders as a placeholder node (both "-" and "- " work; no trailing space is required) — use placeholders for planned-but-unwritten nodes; a fenced code block becomes a leaf node titled "[lang] first line"; plain paragraphs become their own text/Markdown block nodes under the nearest heading.
|
|
56
73
|
|
|
57
74
|
Behavior rules:
|
|
58
75
|
- When the user asks to create a mindmap, call mindmap_create. When the user asks to open, view, show, or switch to an existing mindmap, call mindmap_open (do not use mindmap_get alone). Both operations bring that document to the visible mindmap panel automatically.
|
|
59
|
-
- Always mindmap_get before editing, then send the complete updated document to mindmap_update. Every call must carry the FULL document, never a fragment.
|
|
76
|
+
- Always mindmap_get before editing, then send the complete updated document and the returned revision as expectedRevision to mindmap_update. If the tool reports a revision conflict, stop and ask the user whether to reload or merge; never overwrite newer text silently. Every call must carry the FULL document, never a fragment.
|
|
60
77
|
- Update step by step: whenever the request involves several parts, call mindmap_update as soon as each part is ready — several small updates beat one giant update at the end. The panel plays a growth animation on newly added/changed nodes, so step-by-step updates make the tree visibly grow while you work. Do not call mindmap_update twice in a row with identical content.
|
|
61
78
|
- Never delete the whole document or restructure it without an explicit user request. Make the smallest change that answers the request.
|
|
62
79
|
- When the user steps away or pauses (e.g. "我去买咖啡"), stop all mindmap edits immediately and wait — never continue autonomously.
|
|
80
|
+
- Native write approval defaults to once per document in the current session, so step-by-step updates stay fluid after the first confirmation. \`per-operation\` asks every time; \`off\` skips ordinary confirmations. A new document, a new session, or renameRoot requires a fresh confirmation. The current mindmap workspace shows and can revoke the current-session grants. Never treat a rejected or failed write as approved.
|
|
63
81
|
- Never run any git command for these files. The user commits themselves.
|
|
64
82
|
- Mindmap files stay inside the session working directory.`
|
|
65
83
|
|
|
@@ -290,6 +308,25 @@ function byteLength(value) {
|
|
|
290
308
|
return new TextEncoder().encode(value).byteLength
|
|
291
309
|
}
|
|
292
310
|
|
|
311
|
+
/** Stable document revision shared by read and write tool results. */
|
|
312
|
+
function revisionOfContent(value) {
|
|
313
|
+
return createHash('sha256').update(String(value ?? ''), 'utf8').digest('hex')
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** 保守比较公共首尾之间被替换的旧文本;纯增量插入不算重写。 */
|
|
317
|
+
function isBroadRewrite(before, after) {
|
|
318
|
+
if (!before || before === after) return false
|
|
319
|
+
let prefix = 0
|
|
320
|
+
const limit = Math.min(before.length, after.length)
|
|
321
|
+
while (prefix < limit && before[prefix] === after[prefix]) prefix += 1
|
|
322
|
+
let suffix = 0
|
|
323
|
+
while (suffix < limit - prefix && before[before.length - 1 - suffix] === after[after.length - 1 - suffix]) suffix += 1
|
|
324
|
+
const beforeBytes = byteLength(before)
|
|
325
|
+
const removedBytes = byteLength(before.slice(prefix, before.length - suffix))
|
|
326
|
+
if (removedBytes >= BROAD_REWRITE_MIN_BYTES) return true
|
|
327
|
+
return beforeBytes >= BROAD_REWRITE_RATIO_MIN_BYTES && removedBytes / beforeBytes >= 0.5
|
|
328
|
+
}
|
|
329
|
+
|
|
293
330
|
/**
|
|
294
331
|
* mindmap_open / mindmap_get 共用的有界读取。句柄打开后持续分块读取,文件在
|
|
295
332
|
* 初检后增长也不会绕过上限;非常规文件直接拒绝。
|
|
@@ -357,6 +394,7 @@ export function apply(ctx, config = {}) {
|
|
|
357
394
|
// 透传(带 schema 同款默认值),用户设置层仍可在设置面板覆盖。
|
|
358
395
|
const entryConfig = {
|
|
359
396
|
requireApproval: config.requireApproval !== false,
|
|
397
|
+
approvalMode: normalizeApprovalMode(config.approvalMode, config.requireApproval !== false),
|
|
360
398
|
defaultPanelWidth: typeof config.defaultPanelWidth === 'number' ? config.defaultPanelWidth : 42,
|
|
361
399
|
lineStyle: config.lineStyle === 'curve' ? 'curve' : 'elbow',
|
|
362
400
|
cardStyle: config.cardStyle === 'square' ? 'square' : 'rounded',
|
|
@@ -379,37 +417,139 @@ export function apply(ctx, config = {}) {
|
|
|
379
417
|
|
|
380
418
|
ctx.systemPrompt.section({ name: 'tool:mindmap', order: 106, text: GUIDANCE })
|
|
381
419
|
|
|
382
|
-
//
|
|
383
|
-
//
|
|
384
|
-
//
|
|
420
|
+
// 写入授权按 session + canonical document path 缓存;只在 tools/result 确认
|
|
421
|
+
// 成功后写入,拒绝、失败或取消都不会消耗授权。WeakMap 随 session 生命周期
|
|
422
|
+
// 自然释放,不把授权带到别的会话。
|
|
423
|
+
let approvedTargets = new WeakMap()
|
|
424
|
+
const pendingApprovals = new WeakMap()
|
|
425
|
+
let lastMode
|
|
426
|
+
const readApprovalMode = () => {
|
|
427
|
+
const value = activeConfig()
|
|
428
|
+
const mode = normalizeApprovalMode(value.approvalMode, value.requireApproval)
|
|
429
|
+
if (mode !== lastMode) {
|
|
430
|
+
approvedTargets = new WeakMap()
|
|
431
|
+
lastMode = mode
|
|
432
|
+
}
|
|
433
|
+
return mode
|
|
434
|
+
}
|
|
435
|
+
const stateOf = (session) => {
|
|
436
|
+
if (!session || typeof session !== 'object') return null
|
|
437
|
+
let state = approvedTargets.get(session)
|
|
438
|
+
if (!state) {
|
|
439
|
+
state = { paths: new Set(), generation: 0 }
|
|
440
|
+
approvedTargets.set(session, state)
|
|
441
|
+
}
|
|
442
|
+
return state
|
|
443
|
+
}
|
|
444
|
+
const approvalStatusOf = (session) => {
|
|
445
|
+
const state = session && typeof session === 'object' ? approvedTargets.get(session) : null
|
|
446
|
+
return { mode: readApprovalMode(), grantedDocuments: state?.paths.size ?? 0 }
|
|
447
|
+
}
|
|
448
|
+
const revokeApproval = (session) => {
|
|
449
|
+
if (!session || typeof session !== 'object') return approvalStatusOf(session)
|
|
450
|
+
const state = stateOf(session)
|
|
451
|
+
state.paths.clear()
|
|
452
|
+
state.generation += 1
|
|
453
|
+
return approvalStatusOf(session)
|
|
454
|
+
}
|
|
455
|
+
const sessionOf = (exec) => {
|
|
456
|
+
const direct = exec?.agent?.session
|
|
457
|
+
if (direct) return direct
|
|
458
|
+
const id = exec?.agent?.id
|
|
459
|
+
return id && ctx.sessions?.get ? ctx.sessions.get(id) ?? null : null
|
|
460
|
+
}
|
|
461
|
+
const targetOf = async (exec) => {
|
|
462
|
+
const args = exec.arguments ?? {}
|
|
463
|
+
const cwd = sessionCwd(exec, ctx.sessions)
|
|
464
|
+
let raw
|
|
465
|
+
if (exec.name === 'mindmap_create') {
|
|
466
|
+
try {
|
|
467
|
+
const stem = sanitizeStem(args.name)
|
|
468
|
+
const directory = typeof args.directory === 'string' && args.directory.trim() ? args.directory.trim() : ''
|
|
469
|
+
raw = join(directory, `${stem}.md`)
|
|
470
|
+
} catch {
|
|
471
|
+
// Let the tool return its normal argument error; approval lookup must
|
|
472
|
+
// never turn malformed input into a hook-level exception.
|
|
473
|
+
raw = typeof args.name === 'string' ? args.name.trim() : '?'
|
|
474
|
+
}
|
|
475
|
+
} else {
|
|
476
|
+
raw = typeof args.path === 'string' ? args.path.trim() : ''
|
|
477
|
+
}
|
|
478
|
+
if (cwd && raw) {
|
|
479
|
+
try { return await resolveMindmapPath(cwd, raw) } catch { /* 工具本身负责报告参数错误 */ }
|
|
480
|
+
}
|
|
481
|
+
return `${exec.name}:${raw || '?'}`
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
const needsRewriteApproval = async (exec, target, content) => {
|
|
485
|
+
if (exec.name !== 'mindmap_update' || typeof content !== 'string') return false
|
|
486
|
+
// 缺少会话工作目录或参数尚未解析时,交给工具本身报告参数错误;不要让
|
|
487
|
+
// 审批钩子把普通的兼容关闭误判成高风险写入。
|
|
488
|
+
if (!isAbsolute(target)) return false
|
|
489
|
+
if (byteLength(content) > MAX_CONTENT_BYTES) return true
|
|
490
|
+
try {
|
|
491
|
+
const before = await readMindmap(target)
|
|
492
|
+
return isBroadRewrite(before, content)
|
|
493
|
+
} catch (error) {
|
|
494
|
+
if (error?.code === 'ENOENT') return false
|
|
495
|
+
// 无法判断风险时不复用授权;具体读取错误仍由工具报告。
|
|
496
|
+
return true
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// 022:create 同为写路径,一并纳入审批;高风险 renameRoot 不复用普通授权。
|
|
385
501
|
ctx.on('tools/pre-execute', async (exec, next) => {
|
|
386
502
|
const decision = await next()
|
|
387
503
|
if (decision.kind !== 'allow') return decision
|
|
388
|
-
if (
|
|
504
|
+
if (exec.name !== 'mindmap_create' && exec.name !== 'mindmap_update') return decision
|
|
505
|
+
const args = exec.arguments ?? {}
|
|
506
|
+
const session = sessionOf(exec)
|
|
507
|
+
const target = await targetOf(exec)
|
|
508
|
+
const rename = exec.name === 'mindmap_update' && typeof args.renameRoot === 'string' && args.renameRoot.trim()
|
|
509
|
+
const deleteDocument = exec.name === 'mindmap_update' && typeof args.content === 'string' && args.content.trim() === ''
|
|
510
|
+
const mode = readApprovalMode()
|
|
511
|
+
const broadRewrite = await needsRewriteApproval(exec, target, args.content)
|
|
512
|
+
const highRisk = Boolean(rename || deleteDocument || broadRewrite)
|
|
513
|
+
const state = stateOf(session)
|
|
514
|
+
if (mode === 'off' && !highRisk) return decision
|
|
515
|
+
if (mode === 'session' && !highRisk && state?.paths.has(target)) return decision
|
|
516
|
+
if (mode === 'session' && !highRisk && state) pendingApprovals.set(exec, { session, target, state, generation: state.generation })
|
|
389
517
|
if (exec.name === 'mindmap_create') {
|
|
390
|
-
const args = exec.arguments ?? {}
|
|
391
518
|
return {
|
|
392
519
|
kind: 'ask',
|
|
393
|
-
reason: `Create mindmap ${JSON.stringify(String(args.name ?? '?'))}.
|
|
520
|
+
reason: `Create mindmap ${JSON.stringify(String(args.name ?? '?'))} at ${JSON.stringify(target)}. ${mode === 'per-operation' ? 'Confirm this write.' : 'Confirm once for this document in the current session.'}`,
|
|
394
521
|
}
|
|
395
522
|
}
|
|
396
|
-
|
|
397
|
-
const args = exec.arguments ?? {}
|
|
398
|
-
const renameNote = typeof args.renameRoot === 'string' && args.renameRoot ? `, rename root to "${args.renameRoot}"` : ''
|
|
523
|
+
const renameNote = rename ? `, rename root to "${args.renameRoot}"` : ''
|
|
399
524
|
const bytes = typeof args.content === 'string' ? byteLength(args.content) : 0
|
|
400
525
|
return {
|
|
401
526
|
kind: 'ask',
|
|
402
|
-
reason:
|
|
527
|
+
reason: `${rename ? 'Rename and write' : deleteDocument ? 'Delete mindmap content' : broadRewrite ? 'Broad rewrite' : 'Write'} mindmap ${JSON.stringify(String(args.path ?? '?'))} (${bytes} bytes${renameNote}). ${highRisk ? 'This operation always requires confirmation.' : mode === 'per-operation' ? 'Confirm this write.' : 'Confirm once for this document in the current session.'}`,
|
|
403
528
|
}
|
|
404
529
|
})
|
|
405
530
|
|
|
531
|
+
// Mark the document only after the authoritative result is successful. The
|
|
532
|
+
// listener sees the same frozen execution object as pre-execute, so rejected,
|
|
533
|
+
// cancelled, and failed writes never grant future writes.
|
|
534
|
+
ctx.on('tools/result', (exec, result) => {
|
|
535
|
+
const pending = pendingApprovals.get(exec)
|
|
536
|
+
if (!pending) return
|
|
537
|
+
pendingApprovals.delete(exec)
|
|
538
|
+
if (result?.isError !== false || exec.signal?.aborted) return
|
|
539
|
+
if (readApprovalMode() !== 'session') return
|
|
540
|
+
// 撤销或策略切换之后,尚未结束的旧调用不能重新恢复授权。
|
|
541
|
+
if (approvedTargets.get(pending.session) !== pending.state || pending.state.generation !== pending.generation) return
|
|
542
|
+
pending.state.paths.add(pending.target)
|
|
543
|
+
})
|
|
544
|
+
|
|
406
545
|
ctx.tools.register(defineTool({
|
|
407
546
|
name: 'mindmap_create',
|
|
408
|
-
description: 'Create a new mindmap markdown file <name>.md in the session working directory and show it in the mindmap panel. Fails if the file already exists. The filename becomes the root node title.',
|
|
547
|
+
description: 'Create a new mindmap markdown file <name>.md in the session working directory or an optional relative directory and show it in the mindmap panel. Fails if the file already exists. The filename becomes the root node title.',
|
|
409
548
|
parameters: {
|
|
410
549
|
type: 'object',
|
|
411
550
|
properties: {
|
|
412
551
|
name: { type: 'string', description: 'Mindmap document name (without .md). Becomes the filename and the root node title.' },
|
|
552
|
+
directory: { type: 'string', description: 'Optional directory relative to the session working directory. Use this when creating from a directory tree.' },
|
|
413
553
|
},
|
|
414
554
|
required: ['name'],
|
|
415
555
|
},
|
|
@@ -419,7 +559,8 @@ export function apply(ctx, config = {}) {
|
|
|
419
559
|
const cwd = sessionCwd(exec, ctx.sessions)
|
|
420
560
|
if (!cwd) throw new Error('The session has no working directory; cannot create a mindmap.')
|
|
421
561
|
const stem = sanitizeStem(args?.name)
|
|
422
|
-
const
|
|
562
|
+
const directory = typeof args?.directory === 'string' && args.directory.trim() ? args.directory.trim() : ''
|
|
563
|
+
const path = await resolveMindmapPath(cwd, join(directory, `${stem}.md`))
|
|
423
564
|
// wx = 不存在才创建:原子拒绝已存在(含并发竞态)与同名目录,无 TOCTOU 窗口。
|
|
424
565
|
try {
|
|
425
566
|
await writeFile(path, '', { encoding: 'utf8', flag: 'wx' })
|
|
@@ -429,7 +570,7 @@ export function apply(ctx, config = {}) {
|
|
|
429
570
|
}
|
|
430
571
|
throw error
|
|
431
572
|
}
|
|
432
|
-
return buildResult('create', path, { content: '', created: true })
|
|
573
|
+
return buildResult('create', path, { content: '', created: true, revision: revisionOfContent('') })
|
|
433
574
|
},
|
|
434
575
|
}))
|
|
435
576
|
|
|
@@ -448,7 +589,7 @@ export function apply(ctx, config = {}) {
|
|
|
448
589
|
async execute(args, exec) {
|
|
449
590
|
const path = await resolveMindmapPath(sessionCwd(exec, ctx.sessions), args?.path)
|
|
450
591
|
const content = await readMindmap(path)
|
|
451
|
-
return buildResult('open', path, { content })
|
|
592
|
+
return buildResult('open', path, { content, revision: revisionOfContent(content) })
|
|
452
593
|
},
|
|
453
594
|
}))
|
|
454
595
|
|
|
@@ -467,7 +608,7 @@ export function apply(ctx, config = {}) {
|
|
|
467
608
|
async execute(args, exec) {
|
|
468
609
|
const path = await resolveMindmapPath(sessionCwd(exec, ctx.sessions), args?.path)
|
|
469
610
|
const content = await readMindmap(path)
|
|
470
|
-
return buildResult('get', path, { content })
|
|
611
|
+
return buildResult('get', path, { content, revision: revisionOfContent(content) })
|
|
471
612
|
},
|
|
472
613
|
}))
|
|
473
614
|
|
|
@@ -480,6 +621,7 @@ export function apply(ctx, config = {}) {
|
|
|
480
621
|
path: { type: 'string', description: 'Path to the .md file, relative to the session working directory or absolute.' },
|
|
481
622
|
content: { type: 'string', description: 'The complete new markdown content of the document.' },
|
|
482
623
|
renameRoot: { type: 'string', description: 'Optional new root title: renames the file to <renameRoot>.md. Only when the user asks to rename the root node.' },
|
|
624
|
+
expectedRevision: { type: 'string', description: 'Optional revision returned by mindmap_get or mindmap_open. The write is rejected if the file changed since that read.' },
|
|
483
625
|
},
|
|
484
626
|
required: ['path'],
|
|
485
627
|
},
|
|
@@ -496,6 +638,14 @@ export function apply(ctx, config = {}) {
|
|
|
496
638
|
throw new Error(`mindmap content exceeds the ${MAX_CONTENT_BYTES}-byte limit.`)
|
|
497
639
|
}
|
|
498
640
|
if (!(await pathExists(path))) throw new Error(`Mindmap not found: ${JSON.stringify(path)}. Create it with mindmap_create first.`)
|
|
641
|
+
const expectedRevision = typeof args?.expectedRevision === 'string' && args.expectedRevision ? args.expectedRevision : null
|
|
642
|
+
if (expectedRevision) {
|
|
643
|
+
const currentContent = await readMindmap(path)
|
|
644
|
+
const currentRevision = revisionOfContent(currentContent)
|
|
645
|
+
if (currentRevision !== expectedRevision) {
|
|
646
|
+
throw new Error(`Mindmap changed since it was read (expected ${expectedRevision}, found ${currentRevision}). Reload it before writing.`)
|
|
647
|
+
}
|
|
648
|
+
}
|
|
499
649
|
|
|
500
650
|
let finalPath = path
|
|
501
651
|
let renamedFrom
|
|
@@ -517,7 +667,7 @@ export function apply(ctx, config = {}) {
|
|
|
517
667
|
}
|
|
518
668
|
if (hasContent) await writeMindmap(finalPath, args.content)
|
|
519
669
|
const content = hasContent ? args.content : await readMindmap(finalPath)
|
|
520
|
-
return buildResult('update', finalPath, { content, ...(renamedFrom ? { renamedFrom } : {}) })
|
|
670
|
+
return buildResult('update', finalPath, { content, revision: revisionOfContent(content), ...(renamedFrom ? { renamedFrom } : {}) })
|
|
521
671
|
},
|
|
522
672
|
}))
|
|
523
673
|
|
|
@@ -539,7 +689,7 @@ export function apply(ctx, config = {}) {
|
|
|
539
689
|
}
|
|
540
690
|
try {
|
|
541
691
|
const method = new URL(req.url ?? '/', 'http://dsh.internal').pathname.slice('/mindmap/api/'.length)
|
|
542
|
-
if (method !== 'tree') {
|
|
692
|
+
if (method !== 'tree' && method !== 'document' && method !== 'approval') {
|
|
543
693
|
sendJson(res, 404, { ok: false, error: { code: 'not-found', message: `unknown mindmap API method ${JSON.stringify(method)}` } })
|
|
544
694
|
return
|
|
545
695
|
}
|
|
@@ -549,11 +699,27 @@ export function apply(ctx, config = {}) {
|
|
|
549
699
|
sendJson(res, 400, { ok: false, error: { code: 'bad-request', message: 'missing or invalid "sessionId"' } })
|
|
550
700
|
return
|
|
551
701
|
}
|
|
702
|
+
if (method === 'approval') {
|
|
703
|
+
const session = ctx.sessions?.get?.(sessionId)
|
|
704
|
+
if (!session) {
|
|
705
|
+
sendJson(res, 400, { ok: false, error: { code: 'unknown-session', message: 'unknown session' } })
|
|
706
|
+
return
|
|
707
|
+
}
|
|
708
|
+
const value = payload.action === 'revoke' ? revokeApproval(session) : approvalStatusOf(session)
|
|
709
|
+
sendJson(res, 200, { ok: true, value })
|
|
710
|
+
return
|
|
711
|
+
}
|
|
552
712
|
const cwd = sessionCwdOf(ctx.sessions, sessionId)
|
|
553
713
|
if (!cwd) {
|
|
554
714
|
sendJson(res, 400, { ok: false, error: { code: 'no-cwd', message: 'session has no working directory' } })
|
|
555
715
|
return
|
|
556
716
|
}
|
|
717
|
+
if (method === 'document') {
|
|
718
|
+
const path = await resolveMindmapPath(cwd, payload.path)
|
|
719
|
+
const content = await readMindmap(path)
|
|
720
|
+
sendJson(res, 200, { ok: true, value: { path, content, revision: revisionOfContent(content) } })
|
|
721
|
+
return
|
|
722
|
+
}
|
|
557
723
|
const dir = await resolveTreePath(cwd, payload.path)
|
|
558
724
|
const listing = await listDirectoryLevel(dir)
|
|
559
725
|
sendJson(res, 200, { ok: true, value: { ...listing, cwd } })
|
|
@@ -573,6 +739,8 @@ export function apply(ctx, config = {}) {
|
|
|
573
739
|
|
|
574
740
|
export const internals = Object.freeze({
|
|
575
741
|
GUIDANCE,
|
|
742
|
+
APPROVAL_MODES,
|
|
743
|
+
normalizeApprovalMode,
|
|
576
744
|
MAX_CONTENT_BYTES,
|
|
577
745
|
MAX_READ_BYTES,
|
|
578
746
|
readMindmap,
|
|
@@ -585,4 +753,5 @@ export const internals = Object.freeze({
|
|
|
585
753
|
listDirectoryLevel,
|
|
586
754
|
isTrustedRequest,
|
|
587
755
|
buildResult,
|
|
756
|
+
revisionOfContent,
|
|
588
757
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-mindmap",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "Mindmap plugin for DeepSeek Harness: a plain markdown file in the working directory IS the mindmap; the chat edits it step by step and the right-side panel follows live.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|