agentdock-cli 1.1.0 → 1.4.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.
@@ -0,0 +1,185 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runRecoverCommand = runRecoverCommand;
4
+ const scan_1 = require("../core/scan");
5
+ const snapshot_1 = require("../core/snapshot");
6
+ const command_error_codes_1 = require("../constants/command-error-codes");
7
+ const command_json_1 = require("../utils/command-json");
8
+ const i18n_1 = require("../i18n");
9
+ const ACTION_LABELS = {
10
+ create: 'CREATE ',
11
+ overwrite: 'OVERWRITE',
12
+ skip: 'SKIP ',
13
+ };
14
+ /**
15
+ * 完整度评分:文件齐全度 60 + 顶层键 20 + env 10 + MCP 10,
16
+ * 脱敏快照无法还原真实密钥,扣 20 分。
17
+ */
18
+ function scoreSnapshot(meta, knownFiles) {
19
+ // `all` 快照覆盖多个工具,缺失文件应以"历史快照里出现过的文件"为参照,
20
+ // 否则会把这台机器没装的工具配置全部算成缺失,评分失真。
21
+ const expected = meta.agent === 'all'
22
+ ? [...(knownFiles ?? new Set(meta.files.map((file) => file.relPath)))]
23
+ : (0, snapshot_1.configFilesFor)(meta.agent);
24
+ const present = new Set(meta.files.map((file) => file.relPath));
25
+ const missingFiles = expected.filter((relPath) => !present.has(relPath));
26
+ const fileRatio = expected.length === 0 ? 1 : (expected.length - missingFiles.length) / expected.length;
27
+ const breakdown = {
28
+ files: Math.round(fileRatio * 60),
29
+ keys: Math.min(20, meta.counts.topLevelKeys),
30
+ env: Math.min(10, meta.counts.envKeys),
31
+ mcp: Math.min(10, meta.counts.mcpCount * 5),
32
+ redactedPenalty: meta.redacted ? -20 : 0,
33
+ };
34
+ const raw = breakdown.files + breakdown.keys + breakdown.env + breakdown.mcp + breakdown.redactedPenalty;
35
+ return {
36
+ id: meta.id,
37
+ createdAt: meta.createdAt,
38
+ agent: meta.agent,
39
+ files: meta.files.length,
40
+ redacted: meta.redacted,
41
+ score: Math.max(0, Math.min(100, raw)),
42
+ breakdown,
43
+ missingFiles,
44
+ };
45
+ }
46
+ async function buildCandidates(root) {
47
+ const list = await (0, snapshot_1.listSnapshots)(root);
48
+ const snapshots = [];
49
+ for (const item of list) {
50
+ snapshots.push(await (0, snapshot_1.loadSnapshot)(root, item.id));
51
+ }
52
+ const knownFiles = new Set(snapshots.flatMap((snapshot) => (snapshot.meta.files.map((file) => file.relPath))));
53
+ const candidates = snapshots.map((snapshot) => scoreSnapshot(snapshot.meta, knownFiles));
54
+ return candidates.sort((a, b) => b.score - a.score || b.id.localeCompare(a.id));
55
+ }
56
+ function renderCandidates(candidates, dir) {
57
+ const lines = [(0, i18n_1.t)('recover.header', { count: candidates.length, dir })];
58
+ for (const candidate of candidates) {
59
+ lines.push((0, i18n_1.t)('recover.candidate', {
60
+ score: candidate.score,
61
+ id: candidate.id,
62
+ agent: candidate.agent,
63
+ files: candidate.files,
64
+ redacted: candidate.redacted ? ' [redacted]' : '',
65
+ }));
66
+ if (candidate.missingFiles.length > 0) {
67
+ lines.push((0, i18n_1.t)('recover.missing', { files: candidate.missingFiles.join(', ') }));
68
+ }
69
+ }
70
+ return lines;
71
+ }
72
+ function renderPlan(candidate, entries, applied) {
73
+ const mode = applied ? (0, i18n_1.t)('restore.modeApply') : (0, i18n_1.t)('restore.modeDryRun');
74
+ const lines = [(0, i18n_1.t)('recover.planLine', { id: candidate.id, score: candidate.score, mode })];
75
+ for (const entry of entries) {
76
+ lines.push(` ${ACTION_LABELS[entry.action]} ${entry.relPath}`);
77
+ }
78
+ return lines;
79
+ }
80
+ async function runUndo(root, options) {
81
+ const { snapshot, entries } = await (0, snapshot_1.prepareUndo)(root);
82
+ const apply = options.apply === true && options.dryRun !== true;
83
+ const result = apply ? await (0, snapshot_1.applyUndo)(root, snapshot, entries) : undefined;
84
+ if (options.json === true) {
85
+ const data = {
86
+ dir: (0, snapshot_1.snapshotsDir)(root),
87
+ candidates: [],
88
+ restored: result ?? {
89
+ snapshotId: snapshot.meta.id,
90
+ applied: false,
91
+ entries,
92
+ undo: true,
93
+ },
94
+ };
95
+ return { exitCode: 0, stdout: [(0, command_json_1.toJsonLine)('recover', true, data, [])], stderr: [] };
96
+ }
97
+ const mode = apply ? (0, i18n_1.t)('restore.modeApply') : (0, i18n_1.t)('restore.modeDryRun');
98
+ const lines = [(0, i18n_1.t)('restore.undoPlanHeader', { id: snapshot.meta.id, mode })];
99
+ for (const entry of entries) {
100
+ lines.push(` ${ACTION_LABELS[entry.action]} ${entry.relPath}`);
101
+ }
102
+ if (result) {
103
+ lines.push((0, i18n_1.t)('recover.undone', {
104
+ count: result.entries.filter((entry) => entry.action !== 'skip').length,
105
+ id: snapshot.meta.id,
106
+ }));
107
+ }
108
+ return { exitCode: 0, stdout: lines, stderr: [] };
109
+ }
110
+ async function runRecoverCommand(options = {}) {
111
+ const root = options.root ?? (0, scan_1.defaultScanRoot)();
112
+ try {
113
+ if (options.undo === true) {
114
+ return await runUndo(root, options);
115
+ }
116
+ const candidates = await buildCandidates(root);
117
+ if (candidates.length === 0) {
118
+ if (options.json === true) {
119
+ const data = { dir: (0, snapshot_1.snapshotsDir)(root), candidates: [] };
120
+ return { exitCode: 0, stdout: [(0, command_json_1.toJsonLine)('recover', true, data, [])], stderr: [] };
121
+ }
122
+ return { exitCode: 0, stdout: [(0, i18n_1.t)('recover.empty')], stderr: [] };
123
+ }
124
+ const recommended = candidates[0].id;
125
+ const chosen = options.snapshot
126
+ ? candidates.find((candidate) => candidate.id === options.snapshot)
127
+ ?? scoreSnapshot((await (0, snapshot_1.loadSnapshot)(root, options.snapshot)).meta)
128
+ : candidates[0];
129
+ const apply = options.apply === true && options.dryRun !== true;
130
+ let result;
131
+ let planEntries;
132
+ if (apply) {
133
+ const snapshot = await (0, snapshot_1.loadSnapshot)(root, chosen.id);
134
+ const entries = await (0, snapshot_1.planRestore)(root, snapshot);
135
+ result = await (0, snapshot_1.applyRestore)(root, snapshot, entries);
136
+ planEntries = result.entries;
137
+ }
138
+ else if (options.snapshot) {
139
+ planEntries = await (0, snapshot_1.planRestore)(root, await (0, snapshot_1.loadSnapshot)(root, chosen.id));
140
+ }
141
+ if (options.json === true) {
142
+ const data = {
143
+ dir: (0, snapshot_1.snapshotsDir)(root),
144
+ candidates,
145
+ recommended,
146
+ restored: result,
147
+ };
148
+ return { exitCode: 0, stdout: [(0, command_json_1.toJsonLine)('recover', true, data, [])], stderr: [] };
149
+ }
150
+ const lines = renderCandidates(candidates, (0, snapshot_1.snapshotsDir)(root));
151
+ lines.push((0, i18n_1.t)('recover.recommended', { id: recommended }));
152
+ if (planEntries) {
153
+ lines.push(...renderPlan(chosen, planEntries, apply));
154
+ }
155
+ if (result) {
156
+ lines.push((0, i18n_1.t)('recover.applied', {
157
+ count: result.entries.filter((entry) => entry.action !== 'skip').length,
158
+ id: chosen.id,
159
+ }));
160
+ if (result.backupSnapshotId) {
161
+ lines.push((0, i18n_1.t)('recover.backup', { id: result.backupSnapshotId }));
162
+ }
163
+ }
164
+ else if (!options.snapshot) {
165
+ lines.push((0, i18n_1.t)('recover.hint'));
166
+ }
167
+ return { exitCode: 0, stdout: lines, stderr: [] };
168
+ }
169
+ catch (error) {
170
+ const message = error instanceof Error ? error.message : String(error);
171
+ if (options.json === true) {
172
+ const code = message.startsWith('RESTORE_NO_UNDO')
173
+ ? command_error_codes_1.COMMAND_ERROR_CODES.RESTORE_NO_UNDO
174
+ : message.startsWith('SNAPSHOT_NOT_FOUND')
175
+ ? command_error_codes_1.COMMAND_ERROR_CODES.SNAPSHOT_NOT_FOUND
176
+ : command_error_codes_1.COMMAND_ERROR_CODES.RESTORE_FAILED;
177
+ return {
178
+ exitCode: 1,
179
+ stdout: [(0, command_json_1.toJsonLine)('recover', false, { snapshot: options.snapshot ?? null, undo: options.undo === true }, [(0, command_json_1.toJsonError)(code, message)])],
180
+ stderr: [],
181
+ };
182
+ }
183
+ return { exitCode: 1, stdout: [], stderr: [message] };
184
+ }
185
+ }
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runRestoreCommand = runRestoreCommand;
4
+ const scan_1 = require("../core/scan");
5
+ const snapshot_1 = require("../core/snapshot");
6
+ const command_error_codes_1 = require("../constants/command-error-codes");
7
+ const command_json_1 = require("../utils/command-json");
8
+ const i18n_1 = require("../i18n");
9
+ const ACTION_LABELS = {
10
+ create: 'CREATE ',
11
+ overwrite: 'OVERWRITE',
12
+ skip: 'SKIP ',
13
+ };
14
+ function renderPlan(snapshot, entries, applied, undo) {
15
+ const mode = applied ? (0, i18n_1.t)('restore.modeApply') : (0, i18n_1.t)('restore.modeDryRun');
16
+ const lines = [
17
+ undo
18
+ ? (0, i18n_1.t)('restore.undoPlanHeader', { id: snapshot.meta.id, mode })
19
+ : (0, i18n_1.t)('restore.planHeader', { id: snapshot.meta.id, mode }),
20
+ ];
21
+ for (const entry of entries) {
22
+ lines.push(` ${ACTION_LABELS[entry.action]} ${entry.relPath}`);
23
+ }
24
+ return lines;
25
+ }
26
+ async function runRestoreCommand(options = {}) {
27
+ const root = options.root ?? (0, scan_1.defaultScanRoot)();
28
+ try {
29
+ if (options.undo === true) {
30
+ const { snapshot, entries } = await (0, snapshot_1.prepareUndo)(root);
31
+ const apply = options.apply === true && options.dryRun !== true;
32
+ let result;
33
+ if (apply) {
34
+ result = await (0, snapshot_1.applyUndo)(root, snapshot, entries);
35
+ }
36
+ if (options.json === true) {
37
+ const data = result ?? {
38
+ snapshotId: snapshot.meta.id,
39
+ applied: false,
40
+ entries,
41
+ undo: true,
42
+ };
43
+ return { exitCode: 0, stdout: [(0, command_json_1.toJsonLine)('restore', true, data, [])], stderr: [] };
44
+ }
45
+ const lines = renderPlan(snapshot, entries, apply, true);
46
+ if (result) {
47
+ lines.push((0, i18n_1.t)('restore.undone', { count: result.entries.filter((entry) => entry.action !== 'skip').length, id: snapshot.meta.id }));
48
+ }
49
+ return { exitCode: 0, stdout: lines, stderr: [] };
50
+ }
51
+ if (!options.snapshot) {
52
+ if (options.json === true) {
53
+ return {
54
+ exitCode: 1,
55
+ stdout: [(0, command_json_1.toJsonLine)('restore', false, { snapshot: null, undo: false }, [(0, command_json_1.toJsonError)(command_error_codes_1.COMMAND_ERROR_CODES.MISSING_ARGUMENT, (0, i18n_1.t)('restore.usage'))])],
56
+ stderr: [],
57
+ };
58
+ }
59
+ return { exitCode: 1, stdout: [], stderr: [(0, i18n_1.t)('restore.usage')] };
60
+ }
61
+ const snapshot = await (0, snapshot_1.loadSnapshot)(root, options.snapshot);
62
+ const entries = await (0, snapshot_1.planRestore)(root, snapshot);
63
+ const apply = options.apply === true && options.dryRun !== true;
64
+ let result;
65
+ if (apply) {
66
+ result = await (0, snapshot_1.applyRestore)(root, snapshot, entries);
67
+ }
68
+ if (options.json === true) {
69
+ const data = result ?? {
70
+ snapshotId: snapshot.meta.id,
71
+ applied: false,
72
+ entries,
73
+ undo: false,
74
+ };
75
+ return { exitCode: 0, stdout: [(0, command_json_1.toJsonLine)('restore', true, data, [])], stderr: [] };
76
+ }
77
+ const lines = renderPlan(snapshot, entries, apply, false);
78
+ if (result) {
79
+ const changed = result.entries.filter((entry) => entry.action !== 'skip').length;
80
+ lines.push((0, i18n_1.t)('restore.applied', { count: changed, id: snapshot.meta.id }));
81
+ if (result.backupSnapshotId) {
82
+ lines.push((0, i18n_1.t)('restore.backup', { id: result.backupSnapshotId }));
83
+ }
84
+ }
85
+ return { exitCode: 0, stdout: lines, stderr: [] };
86
+ }
87
+ catch (error) {
88
+ const message = error instanceof Error ? error.message : String(error);
89
+ if (options.json === true) {
90
+ const code = message.startsWith('RESTORE_NO_UNDO')
91
+ ? command_error_codes_1.COMMAND_ERROR_CODES.RESTORE_NO_UNDO
92
+ : message.startsWith('SNAPSHOT_NOT_FOUND')
93
+ ? command_error_codes_1.COMMAND_ERROR_CODES.SNAPSHOT_NOT_FOUND
94
+ : command_error_codes_1.COMMAND_ERROR_CODES.RESTORE_FAILED;
95
+ return {
96
+ exitCode: 1,
97
+ stdout: [(0, command_json_1.toJsonLine)('restore', false, { snapshot: options.snapshot ?? null, undo: options.undo === true }, [(0, command_json_1.toJsonError)(code, message)])],
98
+ stderr: [],
99
+ };
100
+ }
101
+ return { exitCode: 1, stdout: [], stderr: [message] };
102
+ }
103
+ }
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runSnapshotCommand = runSnapshotCommand;
4
+ const scan_1 = require("../core/scan");
5
+ const snapshot_1 = require("../core/snapshot");
6
+ const registry_1 = require("../agents/registry");
7
+ const command_error_codes_1 = require("../constants/command-error-codes");
8
+ const command_json_1 = require("../utils/command-json");
9
+ const i18n_1 = require("../i18n");
10
+ function assertAction(action) {
11
+ if (!action || action === 'create')
12
+ return 'create';
13
+ if (action === 'list' || action === '--list')
14
+ return 'list';
15
+ if (action === 'push')
16
+ return 'push';
17
+ if (action === 'pull')
18
+ return 'pull';
19
+ if (action === 'help' || action === '--help' || action === '-h')
20
+ return 'help';
21
+ return null;
22
+ }
23
+ async function runSnapshotCommand(action, target, options = {}) {
24
+ const root = options.root ?? (0, scan_1.defaultScanRoot)();
25
+ const resolved = assertAction(action);
26
+ if (resolved === null) {
27
+ return {
28
+ exitCode: 1,
29
+ stdout: [],
30
+ stderr: [(0, i18n_1.t)('snapshot.usage')],
31
+ };
32
+ }
33
+ if (resolved === 'help') {
34
+ return { exitCode: 0, stdout: [(0, i18n_1.t)('snapshot.usage')], stderr: [] };
35
+ }
36
+ try {
37
+ if (resolved === 'push' || resolved === 'pull') {
38
+ if (!target) {
39
+ return { exitCode: 1, stdout: [], stderr: [(0, i18n_1.t)(`snapshot.${resolved}Usage`)] };
40
+ }
41
+ if (resolved === 'push') {
42
+ const result = await (0, snapshot_1.exportSnapshot)(root, target, options.snapshot, options.force === true);
43
+ if (options.json === true) {
44
+ return {
45
+ exitCode: 0,
46
+ stdout: [(0, command_json_1.toJsonLine)('snapshot', true, { action: 'push', ...result }, [])],
47
+ stderr: [],
48
+ };
49
+ }
50
+ return { exitCode: 0, stdout: [(0, i18n_1.t)('snapshot.pushed', { id: result.id, dest: result.dest })], stderr: [] };
51
+ }
52
+ const result = await (0, snapshot_1.importSnapshots)(root, target, options.force === true);
53
+ if (options.json === true) {
54
+ return {
55
+ exitCode: 0,
56
+ stdout: [(0, command_json_1.toJsonLine)('snapshot', true, { action: 'pull', ...result }, [])],
57
+ stderr: [],
58
+ };
59
+ }
60
+ const lines = [];
61
+ if (result.imported.length > 0) {
62
+ lines.push((0, i18n_1.t)('snapshot.pulled', { count: result.imported.length, ids: result.imported.join(', ') }));
63
+ }
64
+ if (result.skipped.length > 0) {
65
+ lines.push((0, i18n_1.t)('snapshot.pullSkipped', { ids: result.skipped.join(', ') }));
66
+ }
67
+ if (lines.length === 0) {
68
+ lines.push((0, i18n_1.t)('snapshot.pullNone'));
69
+ }
70
+ return { exitCode: 0, stdout: lines, stderr: [] };
71
+ }
72
+ if (resolved === 'list') {
73
+ const snapshots = await (0, snapshot_1.listSnapshots)(root);
74
+ const dir = (0, snapshot_1.snapshotsDir)(root);
75
+ if (options.json === true) {
76
+ return {
77
+ exitCode: 0,
78
+ stdout: [(0, command_json_1.toJsonLine)('snapshot', true, { action: 'list', dir, snapshots }, [])],
79
+ stderr: [],
80
+ };
81
+ }
82
+ if (snapshots.length === 0) {
83
+ return { exitCode: 0, stdout: [(0, i18n_1.t)('snapshot.empty')], stderr: [] };
84
+ }
85
+ const lines = [(0, i18n_1.t)('snapshot.listHeader', { count: snapshots.length, dir })];
86
+ for (const item of snapshots) {
87
+ lines.push((0, i18n_1.t)('snapshot.listItem', {
88
+ id: item.id,
89
+ agent: item.agent,
90
+ files: item.files,
91
+ redacted: item.redacted ? ' [redacted]' : '',
92
+ }));
93
+ }
94
+ return { exitCode: 0, stdout: lines, stderr: [] };
95
+ }
96
+ const agent = (0, registry_1.normalizeAgentId)(options.agent ?? 'all');
97
+ if (options.ifChanged === true) {
98
+ const unchanged = await (0, snapshot_1.findUnchangedSnapshot)(root, agent);
99
+ if (unchanged) {
100
+ if (options.json === true) {
101
+ return {
102
+ exitCode: 0,
103
+ stdout: [(0, command_json_1.toJsonLine)('snapshot', true, {
104
+ action: 'create',
105
+ created: false,
106
+ skipped: true,
107
+ snapshotId: unchanged.id,
108
+ agent,
109
+ root,
110
+ }, [])],
111
+ stderr: [],
112
+ };
113
+ }
114
+ return { exitCode: 0, stdout: [(0, i18n_1.t)('snapshot.unchanged', { id: unchanged.id })], stderr: [] };
115
+ }
116
+ }
117
+ const meta = await (0, snapshot_1.createSnapshot)({ root, agent, redact: options.redact === true });
118
+ if (options.json === true) {
119
+ return {
120
+ exitCode: 0,
121
+ stdout: [(0, command_json_1.toJsonLine)('snapshot', true, { action: 'create', created: true, ...meta }, [])],
122
+ stderr: [],
123
+ };
124
+ }
125
+ return {
126
+ exitCode: 0,
127
+ stdout: [
128
+ (0, i18n_1.t)('snapshot.created', { id: meta.id }),
129
+ (0, i18n_1.t)('snapshot.location', { path: (0, snapshot_1.snapshotsDir)(root) }),
130
+ (0, i18n_1.t)('snapshot.files', {
131
+ count: meta.files.length,
132
+ files: meta.files.map((file) => file.relPath).join(', '),
133
+ }),
134
+ (0, i18n_1.t)('snapshot.counts', {
135
+ top: meta.counts.topLevelKeys,
136
+ env: meta.counts.envKeys,
137
+ mcp: meta.counts.mcpCount,
138
+ }),
139
+ meta.redacted ? (0, i18n_1.t)('snapshot.redacted') : (0, i18n_1.t)('snapshot.plaintext'),
140
+ ],
141
+ stderr: [],
142
+ };
143
+ }
144
+ catch (error) {
145
+ const message = error instanceof Error ? error.message : String(error);
146
+ if (options.json === true) {
147
+ return {
148
+ exitCode: 1,
149
+ stdout: [(0, command_json_1.toJsonLine)('snapshot', false, { action: resolved }, [(0, command_json_1.toJsonError)(command_error_codes_1.COMMAND_ERROR_CODES.SNAPSHOT_FAILED, message)])],
150
+ stderr: [],
151
+ };
152
+ }
153
+ return { exitCode: 1, stdout: [], stderr: [message] };
154
+ }
155
+ }
@@ -120,10 +120,14 @@ async function runSyncCommand(action, target, options = {}) {
120
120
  }
121
121
  const file = await loadSyncFile(options.config);
122
122
  const config = await (0, config_1.loadConfig)();
123
+ const syncAgent = options.agent ?? file.agent ?? config.agent ?? 'all';
124
+ if (syncAgent !== 'claude' && syncAgent !== 'codex' && syncAgent !== 'all') {
125
+ throw new Error(`SYNC_AGENT_UNSUPPORTED: 同步依赖语义扫描,暂只支持 claude / codex / all;${syncAgent} 可先用 snapshot push/pull 分发配置快照`);
126
+ }
123
127
  const opts = {
124
128
  target,
125
129
  root,
126
- agent: options.agent ?? file.agent ?? config.agent ?? 'all',
130
+ agent: syncAgent,
127
131
  mode: options.mode ?? file.mode ?? 'full',
128
132
  include: options.include ?? file.include ?? [],
129
133
  exclude: options.exclude ?? file.exclude ?? [],
@@ -27,4 +27,8 @@ exports.COMMAND_ERROR_CODES = {
27
27
  SIGN_KEY_EXISTS: 'SIGN_KEY_EXISTS',
28
28
  SIGN_FAILED: 'SIGN_FAILED',
29
29
  SIGNATURE_INVALID: 'SIGNATURE_INVALID',
30
+ SNAPSHOT_FAILED: 'SNAPSHOT_FAILED',
31
+ SNAPSHOT_NOT_FOUND: 'SNAPSHOT_NOT_FOUND',
32
+ RESTORE_FAILED: 'RESTORE_FAILED',
33
+ RESTORE_NO_UNDO: 'RESTORE_NO_UNDO',
30
34
  };