@linxin666/dsh-liangshen 0.1.13
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 +202 -0
- package/README.i18n.yaml +6 -0
- package/README.md +85 -0
- package/README.zh.md +85 -0
- package/cordis.patch.yml +11 -0
- package/lib/index.js +212 -0
- package/lib/types/index.d.ts +42 -0
- package/lib/types/sync.d.ts +31 -0
- package/package.json +61 -0
- package/presets/liangshen/NOTICE +3 -0
- package/presets/liangshen/agent.cordis.yml +289 -0
- package/presets/liangshen/preset.yml +3 -0
- package/presets/liangshen/tool-bootstrap.mjs +293 -0
- package/presets/liangshen-exact/NOTICE +4 -0
- package/presets/liangshen-exact/agent.cordis.yml +321 -0
- package/presets/liangshen-exact/preset.yml +3 -0
- package/presets/liangshen-exact/tool-bootstrap.mjs +10 -0
- package/src/index.test.ts +61 -0
- package/src/index.ts +119 -0
- package/src/sync.test.ts +125 -0
- package/src/sync.ts +131 -0
- package/tools/analyze-session.mjs +289 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { homedir } from 'node:os'
|
|
3
|
+
import { join } from 'node:path'
|
|
4
|
+
import process from 'node:process'
|
|
5
|
+
import { dshHome } from './index.ts'
|
|
6
|
+
|
|
7
|
+
/** Run `body` with the given DSH_HOME overrides, restoring the env afterwards. */
|
|
8
|
+
function withEnv(values: Record<string, string | undefined>, body: () => void): void {
|
|
9
|
+
const previous = new Map<string, string | undefined>()
|
|
10
|
+
for (const [key, value] of Object.entries(values)) {
|
|
11
|
+
previous.set(key, process.env[key])
|
|
12
|
+
if (value === undefined) delete process.env[key]
|
|
13
|
+
else process.env[key] = value
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
body()
|
|
17
|
+
} finally {
|
|
18
|
+
for (const [key, value] of previous) {
|
|
19
|
+
if (value === undefined) delete process.env[key]
|
|
20
|
+
else process.env[key] = value
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe('dshHome', () => {
|
|
26
|
+
it('expands a bare tilde to the user home directory', () => {
|
|
27
|
+
withEnv({ DSH_HOME: '~' }, () => {
|
|
28
|
+
expect(dshHome()).toBe(homedir())
|
|
29
|
+
})
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('expands ~/ and ~\\ prefixes to home-relative paths', () => {
|
|
33
|
+
withEnv({ DSH_HOME: '~/liangshen-home' }, () => {
|
|
34
|
+
expect(dshHome()).toBe(join(homedir(), 'liangshen-home'))
|
|
35
|
+
})
|
|
36
|
+
withEnv({ DSH_HOME: '~\\liangshen-home' }, () => {
|
|
37
|
+
expect(dshHome()).toBe(join(homedir(), 'liangshen-home'))
|
|
38
|
+
})
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('trims the override before expanding', () => {
|
|
42
|
+
withEnv({ DSH_HOME: ' ~/liangshen-home ' }, () => {
|
|
43
|
+
expect(dshHome()).toBe(join(homedir(), 'liangshen-home'))
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('falls back to ~/.dsh for a missing or blank override', () => {
|
|
48
|
+
withEnv({ DSH_HOME: undefined }, () => {
|
|
49
|
+
expect(dshHome()).toBe(join(homedir(), '.dsh'))
|
|
50
|
+
})
|
|
51
|
+
withEnv({ DSH_HOME: ' ' }, () => {
|
|
52
|
+
expect(dshHome()).toBe(join(homedir(), '.dsh'))
|
|
53
|
+
})
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('keeps absolute overrides untouched', () => {
|
|
57
|
+
withEnv({ DSH_HOME: '/srv/dsh-home' }, () => {
|
|
58
|
+
expect(dshHome()).toBe('/srv/dsh-home')
|
|
59
|
+
})
|
|
60
|
+
})
|
|
61
|
+
})
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-liangshen — LiangShen (梁神) agent preset plugin.
|
|
3
|
+
*
|
|
4
|
+
* Host half only: on startup it syncs the bundled `presets/` tree into the
|
|
5
|
+
* harness-home agent-presets root (`~/.dsh/.agent-presets`), making the
|
|
6
|
+
* LiangShen preset selectable for new sessions without copying files by hand,
|
|
7
|
+
* and announces the capability through a system-prompt section. No browser
|
|
8
|
+
* half, no routes, no agent tools — the preset itself provides the tools.
|
|
9
|
+
*
|
|
10
|
+
* The preset is the "anchored-standard" idea shipped as a named mode: the
|
|
11
|
+
* first model request sees only the platform shell plus `read` (Minimal
|
|
12
|
+
* prompt condition), and the full Standard tool catalog opens after the
|
|
13
|
+
* session's first durable tool call. Derived from
|
|
14
|
+
* https://github.com/xiaobright/dsh-anchored-standard (MIT).
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { existsSync, mkdirSync } from 'node:fs'
|
|
18
|
+
import { homedir } from 'node:os'
|
|
19
|
+
import { join } from 'node:path'
|
|
20
|
+
import { fileURLToPath } from 'node:url'
|
|
21
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
22
|
+
import type {} from '@deepseek-ai/dsh-system-prompt'
|
|
23
|
+
import z from 'schemastery'
|
|
24
|
+
import { syncPresetTrees } from './sync.ts'
|
|
25
|
+
|
|
26
|
+
/** Stable cordis plugin name. */
|
|
27
|
+
export const name = 'liangshen'
|
|
28
|
+
|
|
29
|
+
/** Prompt assembly must exist before the announcement section can register. */
|
|
30
|
+
export const inject = ['systemPrompt']
|
|
31
|
+
|
|
32
|
+
/** Plugin config, validated by the same-named schemastery schema. */
|
|
33
|
+
export interface Config {
|
|
34
|
+
/** Master switch: when false, neither sync nor announcement runs. */
|
|
35
|
+
enabled?: boolean
|
|
36
|
+
/** When true (default), a system-prompt section announces the plugin. */
|
|
37
|
+
announceToAgent?: boolean
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const Config: z<Config> = z.object({
|
|
41
|
+
enabled: z.boolean().default(true),
|
|
42
|
+
announceToAgent: z.boolean().default(true),
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
/** Schema default, re-read for hand-built test contexts. */
|
|
46
|
+
const DEFAULT_ANNOUNCE = true
|
|
47
|
+
|
|
48
|
+
/** Order of the announcement section within the tool-guidance band. */
|
|
49
|
+
const SECTION_ORDER = 150
|
|
50
|
+
|
|
51
|
+
/** Model-facing announcement: plugin presence, principle, and limits. */
|
|
52
|
+
export const LIANGSHEN_GUIDANCE = '本机已安装 dsh-liangshen 插件(梁神模式 agent preset):新建会话的预设选择器中可选「梁神模式」与「梁神模式-精确实验」。原理:两阶段锚定——首轮模型请求仅暴露平台 shell 与 read(精确实验版为持久 bash 与 str_replace_editor),只保留一行 persona,清空运行时上下文并只放行用户的直接消息,锚定 Minimal 推理轨迹;晋升受首块锚定门控(首块包含 we 且无 let me,四步兜底),无工具首轮会在响应后自动晋升,晋升后 wire 切换为 Code Mode(PTC,单一 run_code),workspace 指令与 skill 目录在晋升后再延迟一步注入。preset 文件由插件维护于 ~/.dsh/.agent-presets,升级插件时自动更新;默认预设由用户自行选择。用户提到「梁神模式 / 锚定模式 / anchored standard」时即指本插件,请据此协作。'
|
|
53
|
+
|
|
54
|
+
/** Expand a leading `~`, `~/` or `~\` to the current user's home directory. */
|
|
55
|
+
function expandTilde(path: string): string {
|
|
56
|
+
if (path === '~') return homedir()
|
|
57
|
+
if (path.startsWith('~/') || path.startsWith('~\\')) return join(homedir(), path.slice(2))
|
|
58
|
+
return path
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Resolve the harness home (DSH_HOME overrides the conventional ~/.dsh). */
|
|
62
|
+
export function dshHome(): string {
|
|
63
|
+
const override = process.env.DSH_HOME
|
|
64
|
+
if (override === undefined) return join(homedir(), '.dsh')
|
|
65
|
+
const trimmed = override.trim()
|
|
66
|
+
return trimmed === '' ? join(homedir(), '.dsh') : expandTilde(trimmed)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Absolute path of the bundled preset tree inside this package. */
|
|
70
|
+
export function bundledPresetsRoot(): string {
|
|
71
|
+
return fileURLToPath(new URL('../presets/', import.meta.url))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Mount the plugin: sync bundled presets into the harness-home agent-presets
|
|
76
|
+
* root, then announce through a system-prompt section.
|
|
77
|
+
* @param ctx - host plugin context carrying systemPrompt.
|
|
78
|
+
* @param config - resolved plugin config (schema defaults applied by the loader).
|
|
79
|
+
*/
|
|
80
|
+
export function apply(ctx: Context, config?: Config): void {
|
|
81
|
+
const resolve = (): Config => ({
|
|
82
|
+
announceToAgent: config?.announceToAgent ?? DEFAULT_ANNOUNCE,
|
|
83
|
+
enabled: config?.enabled ?? true,
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
const sync = (): void => {
|
|
87
|
+
const targetRoot = join(dshHome(), '.agent-presets')
|
|
88
|
+
try {
|
|
89
|
+
mkdirSync(targetRoot, { recursive: true })
|
|
90
|
+
const result = syncPresetTrees(bundledPresetsRoot(), targetRoot)
|
|
91
|
+
for (const { id, error } of result.failed) {
|
|
92
|
+
ctx.logger?.warn?.(`dsh-liangshen: preset ${id} sync failed: ${error}`)
|
|
93
|
+
}
|
|
94
|
+
if (result.synced.length > 0) {
|
|
95
|
+
ctx.logger?.info?.(`dsh-liangshen: presets synced into ${targetRoot}: ${result.synced.join(', ')}`)
|
|
96
|
+
}
|
|
97
|
+
} catch (error) {
|
|
98
|
+
ctx.logger?.warn?.(`dsh-liangshen: preset sync failed: ${error instanceof Error ? error.message : String(error)}`)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
let disposeSection: (() => void) | undefined
|
|
103
|
+
const refresh = (): void => {
|
|
104
|
+
disposeSection?.()
|
|
105
|
+
disposeSection = undefined
|
|
106
|
+
if (!resolve().enabled) return
|
|
107
|
+
sync()
|
|
108
|
+
if (resolve().announceToAgent) {
|
|
109
|
+
disposeSection = ctx.systemPrompt.section({
|
|
110
|
+
name: 'plugin:dsh-liangshen',
|
|
111
|
+
order: SECTION_ORDER,
|
|
112
|
+
text: LIANGSHEN_GUIDANCE,
|
|
113
|
+
})
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
refresh()
|
|
118
|
+
ctx.effect(() => () => { disposeSection?.(); disposeSection = undefined }, 'dsh-liangshen: announcement')
|
|
119
|
+
}
|
package/src/sync.test.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { existsSync, mkdirSync, mkdtempSync, rmSync, statSync, utimesSync, writeFileSync, readFileSync } from 'node:fs'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { syncPresetTrees } from './sync.ts'
|
|
6
|
+
|
|
7
|
+
function fixture(): { source: string; target: string; dispose: () => void } {
|
|
8
|
+
const base = mkdtempSync(join(tmpdir(), 'dsh-liangshen-'))
|
|
9
|
+
const source = join(base, 'presets')
|
|
10
|
+
const target = join(base, 'agent-presets')
|
|
11
|
+
mkdirSync(join(source, 'liangshen'), { recursive: true })
|
|
12
|
+
writeFileSync(join(source, 'liangshen', 'agent.cordis.yml'), 'rows: []\n')
|
|
13
|
+
writeFileSync(join(source, 'liangshen', 'tool-bootstrap.mjs'), 'export const name = "x"\n')
|
|
14
|
+
writeFileSync(join(source, 'liangshen', 'preset.yml'), 'name: 梁神模式\n')
|
|
15
|
+
return { source, target, dispose: () => rmSync(base, { recursive: true, force: true }) }
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe('syncPresetTrees', () => {
|
|
19
|
+
it('copies the bundled preset tree into the target root', () => {
|
|
20
|
+
const f = fixture()
|
|
21
|
+
try {
|
|
22
|
+
const result = syncPresetTrees(f.source, f.target)
|
|
23
|
+
expect(result.synced).toEqual(['liangshen'])
|
|
24
|
+
expect(result.current).toEqual([])
|
|
25
|
+
expect(result.failed).toEqual([])
|
|
26
|
+
expect(readFileSync(join(f.target, 'liangshen', 'preset.yml'), 'utf8')).toContain('梁神模式')
|
|
27
|
+
expect(readFileSync(join(f.target, 'liangshen', 'tool-bootstrap.mjs'), 'utf8')).toContain('x')
|
|
28
|
+
} finally { f.dispose() }
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('is idempotent — a second run copies nothing', () => {
|
|
32
|
+
const f = fixture()
|
|
33
|
+
try {
|
|
34
|
+
syncPresetTrees(f.source, f.target)
|
|
35
|
+
const second = syncPresetTrees(f.source, f.target)
|
|
36
|
+
expect(second.synced).toEqual([])
|
|
37
|
+
expect(second.current).toEqual(['liangshen'])
|
|
38
|
+
} finally { f.dispose() }
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('rewrites the tree when a file changed', () => {
|
|
42
|
+
const f = fixture()
|
|
43
|
+
try {
|
|
44
|
+
syncPresetTrees(f.source, f.target)
|
|
45
|
+
writeFileSync(join(f.target, 'liangshen', 'agent.cordis.yml'), 'changed\n')
|
|
46
|
+
const third = syncPresetTrees(f.source, f.target)
|
|
47
|
+
expect(third.synced).toEqual(['liangshen'])
|
|
48
|
+
expect(readFileSync(join(f.target, 'liangshen', 'agent.cordis.yml'), 'utf8')).toBe('rows: []\n')
|
|
49
|
+
} finally { f.dispose() }
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('never touches directories it does not own', () => {
|
|
53
|
+
const f = fixture()
|
|
54
|
+
try {
|
|
55
|
+
syncPresetTrees(f.source, f.target)
|
|
56
|
+
mkdirSync(join(f.target, 'user-authored'), { recursive: true })
|
|
57
|
+
writeFileSync(join(f.target, 'user-authored', 'x.txt'), 'mine\n')
|
|
58
|
+
const result = syncPresetTrees(f.source, f.target)
|
|
59
|
+
expect(result.synced).toEqual([])
|
|
60
|
+
expect(readFileSync(join(f.target, 'user-authored', 'x.txt'), 'utf8')).toBe('mine\n')
|
|
61
|
+
} finally { f.dispose() }
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('removes target files whose source file was deleted', () => {
|
|
65
|
+
const f = fixture()
|
|
66
|
+
try {
|
|
67
|
+
syncPresetTrees(f.source, f.target)
|
|
68
|
+
rmSync(join(f.source, 'liangshen', 'tool-bootstrap.mjs'))
|
|
69
|
+
const second = syncPresetTrees(f.source, f.target)
|
|
70
|
+
expect(second.synced).toEqual(['liangshen'])
|
|
71
|
+
expect(existsSync(join(f.target, 'liangshen', 'tool-bootstrap.mjs'))).toBe(false)
|
|
72
|
+
expect(readFileSync(join(f.target, 'liangshen', 'agent.cordis.yml'), 'utf8')).toBe('rows: []\n')
|
|
73
|
+
} finally { f.dispose() }
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('rewrites a same-size, same-mtime file whose bytes differ', () => {
|
|
77
|
+
const f = fixture()
|
|
78
|
+
try {
|
|
79
|
+
syncPresetTrees(f.source, f.target)
|
|
80
|
+
const source = join(f.source, 'liangshen', 'agent.cordis.yml')
|
|
81
|
+
const dest = join(f.target, 'liangshen', 'agent.cordis.yml')
|
|
82
|
+
writeFileSync(dest, 'rowx: []\n')
|
|
83
|
+
const stat = statSync(source)
|
|
84
|
+
utimesSync(dest, stat.atime, stat.mtime)
|
|
85
|
+
expect(statSync(dest).size).toBe(stat.size)
|
|
86
|
+
expect(Math.abs(statSync(dest).mtimeMs - stat.mtimeMs)).toBeLessThan(1)
|
|
87
|
+
const second = syncPresetTrees(f.source, f.target)
|
|
88
|
+
expect(second.synced).toEqual(['liangshen'])
|
|
89
|
+
expect(readFileSync(dest, 'utf8')).toBe('rows: []\n')
|
|
90
|
+
} finally { f.dispose() }
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('removes extra nested files and the directories they leave empty', () => {
|
|
94
|
+
const f = fixture()
|
|
95
|
+
try {
|
|
96
|
+
syncPresetTrees(f.source, f.target)
|
|
97
|
+
mkdirSync(join(f.target, 'liangshen', 'extra', 'nested'), { recursive: true })
|
|
98
|
+
writeFileSync(join(f.target, 'liangshen', 'extra', 'nested', 'leftover.txt'), 'x\n')
|
|
99
|
+
const second = syncPresetTrees(f.source, f.target)
|
|
100
|
+
expect(second.synced).toEqual(['liangshen'])
|
|
101
|
+
expect(existsSync(join(f.target, 'liangshen', 'extra'))).toBe(false)
|
|
102
|
+
expect(readFileSync(join(f.target, 'liangshen', 'agent.cordis.yml'), 'utf8')).toBe('rows: []\n')
|
|
103
|
+
} finally { f.dispose() }
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it('replaces a regular file at the target preset path with a directory', () => {
|
|
107
|
+
const f = fixture()
|
|
108
|
+
try {
|
|
109
|
+
mkdirSync(f.target, { recursive: true })
|
|
110
|
+
writeFileSync(join(f.target, 'liangshen'), 'not a directory\n')
|
|
111
|
+
const result = syncPresetTrees(f.source, f.target)
|
|
112
|
+
expect(result.synced).toEqual(['liangshen'])
|
|
113
|
+
expect(statSync(join(f.target, 'liangshen')).isDirectory()).toBe(true)
|
|
114
|
+
expect(readFileSync(join(f.target, 'liangshen', 'preset.yml'), 'utf8')).toContain('梁神模式')
|
|
115
|
+
} finally { f.dispose() }
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it('reports a missing source root as an empty run', () => {
|
|
119
|
+
const f = fixture()
|
|
120
|
+
try {
|
|
121
|
+
const result = syncPresetTrees(join(f.source, 'nope'), f.target)
|
|
122
|
+
expect(result).toEqual({ synced: [], current: [], failed: [] })
|
|
123
|
+
} finally { f.dispose() }
|
|
124
|
+
})
|
|
125
|
+
})
|
package/src/sync.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sync every preset directory under `sourceRoot` into `targetRoot` — the
|
|
3
|
+
* dsh agent-presets discovery root (harness-home `.agent-presets`).
|
|
4
|
+
*
|
|
5
|
+
* A preset is a directory holding `agent.cordis.yml`; the directory name is
|
|
6
|
+
* the preset id. Copy is per-directory and idempotent: a preset whose target
|
|
7
|
+
* tree is byte-identical to the source tree is skipped, otherwise the source
|
|
8
|
+
* tree is copied and any target files the source does not contain are removed.
|
|
9
|
+
* Directories the plugin does not own (other presets the user authored) are
|
|
10
|
+
* never touched.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { cpSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync } from 'node:fs'
|
|
14
|
+
import { basename, dirname, join, relative } from 'node:path'
|
|
15
|
+
|
|
16
|
+
/** One sync run's outcome, grouped for diagnostics. */
|
|
17
|
+
export interface SyncResult {
|
|
18
|
+
/** Preset ids whose tree was (re)written this run. */
|
|
19
|
+
synced: string[]
|
|
20
|
+
/** Preset ids already current — nothing copied. */
|
|
21
|
+
current: string[]
|
|
22
|
+
/** Preset ids that failed, with the underlying error message. */
|
|
23
|
+
failed: { id: string; error: string }[]
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function filesUnder(root: string): string[] {
|
|
27
|
+
const out: string[] = []
|
|
28
|
+
const walk = (dir: string): void => {
|
|
29
|
+
for (const entry of readdirSync(dir)) {
|
|
30
|
+
const path = join(dir, entry)
|
|
31
|
+
if (statSync(path).isDirectory()) walk(path)
|
|
32
|
+
else out.push(path)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
walk(root)
|
|
36
|
+
return out
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Preset files are small; identity means identical bytes, not just size/mtime. */
|
|
40
|
+
function sameFile(a: string, b: string): boolean {
|
|
41
|
+
return readFileSync(a).equals(readFileSync(b))
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Remove files not in `keep` (relative paths), then remove only the
|
|
46
|
+
* directories those removals left empty — still strictly inside `root`, so
|
|
47
|
+
* sibling presets are never touched.
|
|
48
|
+
*/
|
|
49
|
+
function pruneExtras(root: string, keep: ReadonlySet<string>): void {
|
|
50
|
+
const parents = new Set<string>()
|
|
51
|
+
for (const file of filesUnder(root)) {
|
|
52
|
+
if (!keep.has(relative(root, file))) {
|
|
53
|
+
parents.add(dirname(file))
|
|
54
|
+
rmSync(file, { force: true })
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
for (const start of parents) {
|
|
58
|
+
let dir: string | undefined = start
|
|
59
|
+
while (dir !== undefined && relative(root, dir) !== '') {
|
|
60
|
+
if (existsSync(dir) && readdirSync(dir).length === 0) {
|
|
61
|
+
rmSync(dir, { recursive: true, force: true })
|
|
62
|
+
dir = dirname(dir)
|
|
63
|
+
} else {
|
|
64
|
+
dir = undefined
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Copy `sourceRoot/<id>` into `targetRoot/<id>`, idempotently. */
|
|
71
|
+
export function syncOnePreset(sourceDir: string, targetDir: string): 'synced' | 'current' {
|
|
72
|
+
const sourceFiles = filesUnder(sourceDir)
|
|
73
|
+
const sourceSet = new Set(sourceFiles.map(file => relative(sourceDir, file)))
|
|
74
|
+
|
|
75
|
+
if (existsSync(targetDir) && !statSync(targetDir).isDirectory()) {
|
|
76
|
+
rmSync(targetDir, { recursive: true, force: true })
|
|
77
|
+
}
|
|
78
|
+
if (!existsSync(targetDir)) {
|
|
79
|
+
cpSync(sourceDir, targetDir, { recursive: true, preserveTimestamps: true })
|
|
80
|
+
pruneExtras(targetDir, sourceSet)
|
|
81
|
+
return 'synced'
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
let dirty = false
|
|
85
|
+
for (const file of sourceFiles) {
|
|
86
|
+
const dest = join(targetDir, relative(sourceDir, file))
|
|
87
|
+
if (!existsSync(dest) || !sameFile(file, dest)) {
|
|
88
|
+
dirty = true
|
|
89
|
+
break
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (!dirty) {
|
|
93
|
+
for (const file of filesUnder(targetDir)) {
|
|
94
|
+
if (!sourceSet.has(relative(targetDir, file))) {
|
|
95
|
+
dirty = true
|
|
96
|
+
break
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (!dirty) return 'current'
|
|
101
|
+
|
|
102
|
+
// Drop target-only entries first so file/dir type clashes never reach cpSync,
|
|
103
|
+
// then copy and prune again per the post-copy contract.
|
|
104
|
+
pruneExtras(targetDir, sourceSet)
|
|
105
|
+
cpSync(sourceDir, targetDir, { recursive: true, preserveTimestamps: true })
|
|
106
|
+
pruneExtras(targetDir, sourceSet)
|
|
107
|
+
return 'synced'
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Sync every preset under `sourceRoot` into `targetRoot`.
|
|
112
|
+
* @param sourceRoot - plugin-owned preset tree (bundled in the package).
|
|
113
|
+
* @param targetRoot - dsh agent-presets discovery root (e.g. <home>/.dsh/.agent-presets).
|
|
114
|
+
*/
|
|
115
|
+
export function syncPresetTrees(sourceRoot: string, targetRoot: string): SyncResult {
|
|
116
|
+
const result: SyncResult = { synced: [], current: [], failed: [] }
|
|
117
|
+
if (!existsSync(sourceRoot)) return result
|
|
118
|
+
mkdirSync(targetRoot, { recursive: true })
|
|
119
|
+
for (const entry of readdirSync(sourceRoot)) {
|
|
120
|
+
const source = join(sourceRoot, entry)
|
|
121
|
+
if (!statSync(source).isDirectory()) continue
|
|
122
|
+
const id = basename(source)
|
|
123
|
+
try {
|
|
124
|
+
const outcome = syncOnePreset(source, join(targetRoot, id))
|
|
125
|
+
;(outcome === 'synced' ? result.synced : result.current).push(id)
|
|
126
|
+
} catch (error) {
|
|
127
|
+
result.failed.push({ id, error: error instanceof Error ? error.message : String(error) })
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return result
|
|
131
|
+
}
|