agentdock-cli 0.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.
- package/LICENSE +21 -0
- package/README.md +150 -0
- package/dist/package.json +60 -0
- package/dist/schemas/agentdock.schema.json +121 -0
- package/dist/src/cli.js +129 -0
- package/dist/src/commands/doctor.js +49 -0
- package/dist/src/commands/export.js +133 -0
- package/dist/src/commands/init.js +65 -0
- package/dist/src/commands/install.js +76 -0
- package/dist/src/commands/list.js +70 -0
- package/dist/src/commands/scan.js +61 -0
- package/dist/src/commands/upgrade.js +243 -0
- package/dist/src/commands/validate.js +58 -0
- package/dist/src/constants/command-error-codes.js +20 -0
- package/dist/src/constants/upgrade-warning-codes.js +9 -0
- package/dist/src/core/doctor.js +501 -0
- package/dist/src/core/exporter.js +81 -0
- package/dist/src/core/installer.js +125 -0
- package/dist/src/core/list.js +204 -0
- package/dist/src/core/scan-export.js +208 -0
- package/dist/src/core/scan.js +129 -0
- package/dist/src/core/source-destination.js +16 -0
- package/dist/src/core/template-renderer.js +30 -0
- package/dist/src/manifest/load.js +19 -0
- package/dist/src/manifest/types.js +2 -0
- package/dist/src/manifest/validate.js +53 -0
- package/dist/src/scanners/claude-scanner.js +196 -0
- package/dist/src/scanners/codex-scanner.js +140 -0
- package/dist/src/scanners/sensitive.js +178 -0
- package/dist/src/types/command-report.js +2 -0
- package/dist/src/types/upgrade-report.js +2 -0
- package/dist/src/utils/command-json.js +23 -0
- package/dist/src/utils/fs.js +160 -0
- package/package.json +60 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.installPackage = installPackage;
|
|
7
|
+
const promises_1 = __importDefault(require("node:fs/promises"));
|
|
8
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
const fs_1 = require("../utils/fs");
|
|
10
|
+
async function isIdenticalFile(targetPath, sourcePath) {
|
|
11
|
+
try {
|
|
12
|
+
const [targetBuf, sourceBuf] = await Promise.all([
|
|
13
|
+
promises_1.default.readFile(targetPath),
|
|
14
|
+
promises_1.default.readFile(sourcePath),
|
|
15
|
+
]);
|
|
16
|
+
return targetBuf.equals(sourceBuf);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async function isIdenticalDirectory(targetPath, sourcePath) {
|
|
23
|
+
const [targetEntries, sourceEntries] = await Promise.all([
|
|
24
|
+
promises_1.default.readdir(targetPath, { withFileTypes: true }),
|
|
25
|
+
promises_1.default.readdir(sourcePath, { withFileTypes: true }),
|
|
26
|
+
]);
|
|
27
|
+
if (targetEntries.length !== sourceEntries.length) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
for (const sourceEntry of sourceEntries) {
|
|
31
|
+
const targetEntry = targetEntries.find((entry) => entry.name === sourceEntry.name);
|
|
32
|
+
if (!targetEntry) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
const targetChild = node_path_1.default.join(targetPath, sourceEntry.name);
|
|
36
|
+
const sourceChild = node_path_1.default.join(sourcePath, sourceEntry.name);
|
|
37
|
+
if (sourceEntry.isDirectory()) {
|
|
38
|
+
if (!targetEntry.isDirectory()) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
if (!(await isIdenticalDirectory(targetChild, sourceChild))) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
if (targetEntry.isDirectory()) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
if (!(await isIdenticalFile(targetChild, sourceChild))) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
async function installPackage(packagePath, explicitTargetPath, overwrite = false) {
|
|
57
|
+
const packageRoot = node_path_1.default.resolve(packagePath);
|
|
58
|
+
const manifestSnapshotPath = node_path_1.default.join(packageRoot, 'manifest.resolved.json');
|
|
59
|
+
const installPlanPath = node_path_1.default.join(packageRoot, 'meta', 'install-plan.json');
|
|
60
|
+
if (!(await (0, fs_1.pathExists)(manifestSnapshotPath))) {
|
|
61
|
+
throw new Error(`Missing package manifest snapshot: ${manifestSnapshotPath}`);
|
|
62
|
+
}
|
|
63
|
+
if (!(await (0, fs_1.pathExists)(installPlanPath))) {
|
|
64
|
+
throw new Error(`Missing install plan: ${installPlanPath}`);
|
|
65
|
+
}
|
|
66
|
+
const installPlan = await (0, fs_1.readJsonFile)(installPlanPath);
|
|
67
|
+
const effectiveOverwrite = overwrite || installPlan.overwrite === true;
|
|
68
|
+
// The target may live anywhere the user chooses (e.g. ~/.claude); only individual
|
|
69
|
+
// entries must stay within it. No boundary check against packageRoot here.
|
|
70
|
+
const targetRoot = node_path_1.default.resolve(explicitTargetPath ?? installPlan.targetPath ?? './installed');
|
|
71
|
+
// Boundary-check every entry against its allowed root (blocks path traversal in
|
|
72
|
+
// both the destination `to` and the source `from` fields of a malicious package).
|
|
73
|
+
const entries = [
|
|
74
|
+
...installPlan.sources.map((source) => ({
|
|
75
|
+
from: (0, fs_1.safeResolveWithin)(packageRoot, source.from, 'source.from'),
|
|
76
|
+
to: (0, fs_1.safeResolveWithin)(targetRoot, source.to, 'source.to'),
|
|
77
|
+
kind: source.kind,
|
|
78
|
+
})),
|
|
79
|
+
...installPlan.templates.map((template) => ({
|
|
80
|
+
from: (0, fs_1.safeResolveWithin)(packageRoot, template.from, 'template.from'),
|
|
81
|
+
to: (0, fs_1.safeResolveWithin)(targetRoot, template.to, 'template.to'),
|
|
82
|
+
kind: 'template',
|
|
83
|
+
})),
|
|
84
|
+
];
|
|
85
|
+
// Create the target root up front so the lock file parent exists.
|
|
86
|
+
await (0, fs_1.ensureDirectory)(targetRoot);
|
|
87
|
+
const lockPath = node_path_1.default.join(targetRoot, '.agentdock.lock');
|
|
88
|
+
const lockHandle = await (0, fs_1.acquireLock)(lockPath);
|
|
89
|
+
try {
|
|
90
|
+
const conflicts = [];
|
|
91
|
+
for (const entry of entries) {
|
|
92
|
+
if (!(await (0, fs_1.pathExists)(entry.to))) {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (effectiveOverwrite) {
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
// Idempotent no-op: an existing entry identical to the source is not a conflict.
|
|
99
|
+
if (entry.kind === 'directory') {
|
|
100
|
+
if (await isIdenticalDirectory(entry.to, entry.from)) {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
else if (await isIdenticalFile(entry.to, entry.from)) {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
conflicts.push(entry.to);
|
|
108
|
+
}
|
|
109
|
+
if (conflicts.length > 0) {
|
|
110
|
+
throw new Error(`Install conflict detected:\n${conflicts.join('\n')}`);
|
|
111
|
+
}
|
|
112
|
+
for (const entry of entries) {
|
|
113
|
+
if (entry.kind === 'directory') {
|
|
114
|
+
await (0, fs_1.copyDirectorySafe)(entry.from, entry.to);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
await (0, fs_1.copyFileSafe)(entry.from, entry.to);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return { targetPath: targetRoot };
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
await (0, fs_1.releaseLock)(lockHandle, lockPath);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.runList = runList;
|
|
7
|
+
const promises_1 = __importDefault(require("node:fs/promises"));
|
|
8
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
const yaml_1 = __importDefault(require("yaml"));
|
|
10
|
+
const fs_1 = require("../utils/fs");
|
|
11
|
+
async function fileExists(targetPath) {
|
|
12
|
+
try {
|
|
13
|
+
await promises_1.default.access(targetPath);
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Turn a `DomainEntry.ref` (e.g. `.claude.json#mcpServers.github`,
|
|
22
|
+
* `skills/my-skill`, `agents/reviewer.md`) into a short display name.
|
|
23
|
+
*/
|
|
24
|
+
function displayName(entry) {
|
|
25
|
+
let name = entry.ref ?? entry.path;
|
|
26
|
+
// strip a leading "<file>#" prefix (e.g. .claude.json#, config.toml#)
|
|
27
|
+
name = name.replace(/^[^#]*#/, '');
|
|
28
|
+
name = name.replace(/^mcpServers\./, '');
|
|
29
|
+
name = name.replace(/^[a-z]+\//, '');
|
|
30
|
+
name = name.replace(/\.(md|json|toml|sh|ts|js|txt)$/i, '');
|
|
31
|
+
return name || node_path_1.default.basename(entry.path);
|
|
32
|
+
}
|
|
33
|
+
function buildAgentInfo(agentName, domain) {
|
|
34
|
+
const mcp = domain.mcp.map(displayName);
|
|
35
|
+
const skills = domain.skills.map(displayName);
|
|
36
|
+
const agents = domain.agents.map(displayName);
|
|
37
|
+
const plugins = domain.plugins.map(displayName);
|
|
38
|
+
const hooks = domain.hooks.map(displayName);
|
|
39
|
+
const memory = domain.memory.map(displayName);
|
|
40
|
+
const settings = domain.settings.map(displayName);
|
|
41
|
+
const all = mcp.length + skills.length + agents.length + plugins.length + hooks.length + memory.length + settings.length;
|
|
42
|
+
return {
|
|
43
|
+
agent: agentName,
|
|
44
|
+
mcp,
|
|
45
|
+
skills,
|
|
46
|
+
agents,
|
|
47
|
+
plugins,
|
|
48
|
+
hooks,
|
|
49
|
+
memory,
|
|
50
|
+
settings,
|
|
51
|
+
totals: {
|
|
52
|
+
mcp: mcp.length,
|
|
53
|
+
skills: skills.length,
|
|
54
|
+
agents: agents.length,
|
|
55
|
+
plugins: plugins.length,
|
|
56
|
+
hooks: hooks.length,
|
|
57
|
+
memory: memory.length,
|
|
58
|
+
settings: settings.length,
|
|
59
|
+
all,
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function summarize(report) {
|
|
64
|
+
const totalEntries = report.agents.reduce((sum, a) => sum + a.totals.all, 0);
|
|
65
|
+
const agentLabels = report.agents.map((a) => a.agent).join(' + ');
|
|
66
|
+
return `已捕获 ${agentLabels || '无'} 环境,共 ${totalEntries} 项定义,隔离 ${report.secretsCount} 个机密`;
|
|
67
|
+
}
|
|
68
|
+
function renderMarkdown(report) {
|
|
69
|
+
const lines = [];
|
|
70
|
+
lines.push('# AgentDock 清单 (list)');
|
|
71
|
+
lines.push('');
|
|
72
|
+
lines.push(`来源: ${report.mode} — ${report.manifestPath}`);
|
|
73
|
+
if (report.project.name) {
|
|
74
|
+
lines.push(`项目: ${report.project.name}`);
|
|
75
|
+
}
|
|
76
|
+
lines.push('');
|
|
77
|
+
if (report.agents.length === 0) {
|
|
78
|
+
lines.push('> 清单为空:未捕获任何助手环境定义。');
|
|
79
|
+
lines.push('');
|
|
80
|
+
}
|
|
81
|
+
for (const info of report.agents) {
|
|
82
|
+
const title = info.agent === 'claude' ? 'Claude Code' : 'Codex';
|
|
83
|
+
lines.push(`## ${title}`);
|
|
84
|
+
const sections = [
|
|
85
|
+
['MCP servers', info.mcp],
|
|
86
|
+
['Skills', info.skills],
|
|
87
|
+
['Agents', info.agents],
|
|
88
|
+
['Plugins', info.plugins],
|
|
89
|
+
['Hooks', info.hooks],
|
|
90
|
+
['Memory', info.memory],
|
|
91
|
+
['Settings', info.settings],
|
|
92
|
+
];
|
|
93
|
+
for (const [label, names] of sections) {
|
|
94
|
+
if (names.length > 0) {
|
|
95
|
+
lines.push(`- ${label} (${names.length}): ${names.join(', ')}`);
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
lines.push(`- ${label}: 无`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
lines.push(`- 合计: ${info.totals.all} 项`);
|
|
102
|
+
lines.push('');
|
|
103
|
+
}
|
|
104
|
+
lines.push('## 机密隔离');
|
|
105
|
+
if (report.secrets.length > 0) {
|
|
106
|
+
for (const secret of report.secrets) {
|
|
107
|
+
lines.push(`- ${secret.key} (${secret.source})`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
lines.push('- 无隔离机密');
|
|
112
|
+
}
|
|
113
|
+
lines.push('');
|
|
114
|
+
if (report.installPlan && report.installPlan.length > 0) {
|
|
115
|
+
lines.push('## 安装计划 (package)');
|
|
116
|
+
for (const entry of report.installPlan) {
|
|
117
|
+
lines.push(`- ${entry.from} → ${entry.to} (${entry.kind})`);
|
|
118
|
+
}
|
|
119
|
+
lines.push('');
|
|
120
|
+
}
|
|
121
|
+
return `${lines.join('\n')}\n`;
|
|
122
|
+
}
|
|
123
|
+
async function writeReport(outDir, report) {
|
|
124
|
+
await (0, fs_1.ensureDirectory)(outDir);
|
|
125
|
+
const reportPath = node_path_1.default.join(outDir, 'list-report.md');
|
|
126
|
+
await (0, fs_1.writeTextFile)(reportPath, renderMarkdown(report));
|
|
127
|
+
return reportPath;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* List what a scan captured (or what a package contains) as a structured
|
|
131
|
+
* inventory. Pure presentation — never re-reads source machines, never
|
|
132
|
+
* mutates files. Consumes the same v3 manifest that `export --from-scan`
|
|
133
|
+
* and `install` already use.
|
|
134
|
+
*/
|
|
135
|
+
async function runList(options) {
|
|
136
|
+
let manifest;
|
|
137
|
+
let mode;
|
|
138
|
+
let manifestPath;
|
|
139
|
+
let installPlan;
|
|
140
|
+
if (options.fromScan) {
|
|
141
|
+
mode = 'scan';
|
|
142
|
+
manifestPath = node_path_1.default.resolve(options.fromScan);
|
|
143
|
+
if (!(await fileExists(manifestPath))) {
|
|
144
|
+
throw new Error(`LIST_NO_SOURCE: scan manifest not found: ${manifestPath}`);
|
|
145
|
+
}
|
|
146
|
+
const raw = await promises_1.default.readFile(manifestPath, 'utf8');
|
|
147
|
+
manifest = yaml_1.default.parse(raw);
|
|
148
|
+
if (manifest.version !== 3) {
|
|
149
|
+
throw new Error(`LIST_UNSUPPORTED: expected v3 scan manifest, got version ${manifest.version}`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
else if (options.package) {
|
|
153
|
+
mode = 'package';
|
|
154
|
+
const pkgDir = node_path_1.default.resolve(options.package);
|
|
155
|
+
manifestPath = node_path_1.default.join(pkgDir, 'manifest.resolved.json');
|
|
156
|
+
if (!(await fileExists(manifestPath))) {
|
|
157
|
+
throw new Error(`LIST_NO_SOURCE: package manifest not found: ${manifestPath}`);
|
|
158
|
+
}
|
|
159
|
+
const raw = await promises_1.default.readFile(manifestPath, 'utf8');
|
|
160
|
+
manifest = JSON.parse(raw);
|
|
161
|
+
if (manifest.version !== 3) {
|
|
162
|
+
throw new Error(`LIST_UNSUPPORTED: package manifest is not v3`);
|
|
163
|
+
}
|
|
164
|
+
const planPath = node_path_1.default.join(pkgDir, 'meta', 'install-plan.json');
|
|
165
|
+
if (await fileExists(planPath)) {
|
|
166
|
+
try {
|
|
167
|
+
const plan = JSON.parse(await promises_1.default.readFile(planPath, 'utf8'));
|
|
168
|
+
installPlan = (plan.sources ?? []).map((s) => ({ from: s.from, to: s.to, kind: s.kind }));
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
installPlan = undefined;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
throw new Error('LIST_NO_SOURCE: provide --from-scan <yml> or --package <dir>');
|
|
177
|
+
}
|
|
178
|
+
const agentFilter = options.agent ?? 'all';
|
|
179
|
+
const agents = [];
|
|
180
|
+
for (const [name, domain] of Object.entries(manifest.agents)) {
|
|
181
|
+
if (!domain) {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
if (agentFilter !== 'all' && agentFilter !== name) {
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
agents.push(buildAgentInfo(name, domain));
|
|
188
|
+
}
|
|
189
|
+
const report = {
|
|
190
|
+
mode,
|
|
191
|
+
manifestPath,
|
|
192
|
+
project: manifest.project,
|
|
193
|
+
agents,
|
|
194
|
+
secretsCount: manifest.secrets.length,
|
|
195
|
+
secrets: manifest.secrets,
|
|
196
|
+
summary: '',
|
|
197
|
+
...(installPlan ? { installPlan } : {}),
|
|
198
|
+
};
|
|
199
|
+
report.summary = summarize(report);
|
|
200
|
+
if (options.out) {
|
|
201
|
+
report.reportPath = await writeReport(node_path_1.default.resolve(options.out), report);
|
|
202
|
+
}
|
|
203
|
+
return report;
|
|
204
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.exportFromScan = exportFromScan;
|
|
7
|
+
const promises_1 = __importDefault(require("node:fs/promises"));
|
|
8
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
const yaml_1 = __importDefault(require("yaml"));
|
|
10
|
+
const sensitive_1 = require("../scanners/sensitive");
|
|
11
|
+
const smol_toml_1 = require("smol-toml");
|
|
12
|
+
const fs_1 = require("../utils/fs");
|
|
13
|
+
function* iterEntries(domain) {
|
|
14
|
+
yield* domain.mcp;
|
|
15
|
+
yield* domain.skills;
|
|
16
|
+
yield* domain.agents;
|
|
17
|
+
yield* domain.plugins;
|
|
18
|
+
yield* domain.hooks;
|
|
19
|
+
yield* domain.memory;
|
|
20
|
+
yield* domain.settings;
|
|
21
|
+
}
|
|
22
|
+
function toPosix(rel) {
|
|
23
|
+
return rel.split(node_path_1.default.sep).join(node_path_1.default.posix.sep);
|
|
24
|
+
}
|
|
25
|
+
function parseEnv(content) {
|
|
26
|
+
const out = {};
|
|
27
|
+
for (const raw of content.split(/\r?\n/)) {
|
|
28
|
+
const line = raw.trim();
|
|
29
|
+
if (!line || line.startsWith('#')) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
const eq = line.indexOf('=');
|
|
33
|
+
if (eq === -1) {
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
const key = line.slice(0, eq).trim();
|
|
37
|
+
let value = line.slice(eq + 1).trim();
|
|
38
|
+
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
|
39
|
+
value = value.slice(1, -1);
|
|
40
|
+
}
|
|
41
|
+
out[key] = value;
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Recover the scanned home directory from entry paths. All entries share the same
|
|
47
|
+
* home; we take the parent of the `.claude` (or `.codex`) segment, or of
|
|
48
|
+
* `.claude.json`.
|
|
49
|
+
*/
|
|
50
|
+
function deriveScanRoot(manifest) {
|
|
51
|
+
for (const domain of Object.values(manifest.agents)) {
|
|
52
|
+
if (!domain) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
for (const entry of iterEntries(domain)) {
|
|
56
|
+
const parts = entry.path.split(node_path_1.default.sep);
|
|
57
|
+
const idxClaude = parts.indexOf('.claude');
|
|
58
|
+
if (idxClaude >= 0) {
|
|
59
|
+
return parts.slice(0, idxClaude).join(node_path_1.default.sep) || node_path_1.default.sep;
|
|
60
|
+
}
|
|
61
|
+
const idxCodex = parts.indexOf('.codex');
|
|
62
|
+
if (idxCodex >= 0) {
|
|
63
|
+
return parts.slice(0, idxCodex).join(node_path_1.default.sep) || node_path_1.default.sep;
|
|
64
|
+
}
|
|
65
|
+
if (node_path_1.default.basename(entry.path) === '.claude.json') {
|
|
66
|
+
return node_path_1.default.dirname(entry.path);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
throw new Error('SCAN_EXPORT_NO_ROOT: cannot derive home root from scanned entries');
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Build an installable package from a v3 scan manifest. Produces the exact layout
|
|
74
|
+
* `install` already consumes (manifest.resolved.json + meta/install-plan.json +
|
|
75
|
+
* payload/sources/*), so the hardened installer works unchanged.
|
|
76
|
+
*
|
|
77
|
+
* Secrets are masked by default (`{{KEY}}` placeholders); pass `--env` to re-inject
|
|
78
|
+
* real values. MCP servers are aggregated into a single `.claude.json` payload
|
|
79
|
+
* (mcpServers only) — restoring to a fresh/target machine is the intended use.
|
|
80
|
+
*/
|
|
81
|
+
async function exportFromScan(options) {
|
|
82
|
+
const manifestPath = node_path_1.default.resolve(options.scanManifestPath);
|
|
83
|
+
const raw = await promises_1.default.readFile(manifestPath, 'utf8');
|
|
84
|
+
const manifest = yaml_1.default.parse(raw);
|
|
85
|
+
if (manifest.version !== 3) {
|
|
86
|
+
throw new Error(`SCAN_EXPORT_UNSUPPORTED: expected v3 scan manifest, got version ${manifest.version}`);
|
|
87
|
+
}
|
|
88
|
+
const scanRoot = deriveScanRoot(manifest);
|
|
89
|
+
const env = options.env ? parseEnv(await promises_1.default.readFile(options.env, 'utf8')) : undefined;
|
|
90
|
+
const outputPath = node_path_1.default.resolve(options.out);
|
|
91
|
+
await (0, fs_1.ensureDirectory)(outputPath);
|
|
92
|
+
const lockPath = node_path_1.default.join(outputPath, '.agentdock-scan-export.lock');
|
|
93
|
+
const lockHandle = await (0, fs_1.acquireLock)(lockPath);
|
|
94
|
+
try {
|
|
95
|
+
const payloadSourcesRoot = node_path_1.default.join(outputPath, 'payload', 'sources');
|
|
96
|
+
await (0, fs_1.ensureDirectory)(payloadSourcesRoot);
|
|
97
|
+
const sources = [];
|
|
98
|
+
const toFrom = (diskRel) => `payload/sources/${diskRel}`;
|
|
99
|
+
for (const [agentName, domain] of Object.entries(manifest.agents)) {
|
|
100
|
+
if (!domain) {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
const agent = agentName;
|
|
104
|
+
// --- MCP: aggregate all captured servers into one .claude.json payload ---
|
|
105
|
+
if (domain.mcp.length > 0) {
|
|
106
|
+
const claudeJsonPath = domain.mcp[0].path;
|
|
107
|
+
if (!(await (0, fs_1.pathExists)(claudeJsonPath))) {
|
|
108
|
+
throw new Error(`SCAN_EXPORT_MISSING_SOURCE: ${claudeJsonPath} (scanned but no longer present)`);
|
|
109
|
+
}
|
|
110
|
+
const claudeJson = JSON.parse(await promises_1.default.readFile(claudeJsonPath, 'utf8'));
|
|
111
|
+
const merged = { mcpServers: {} };
|
|
112
|
+
for (const entry of domain.mcp) {
|
|
113
|
+
const name = (entry.ref ?? '').split('mcpServers.')[1];
|
|
114
|
+
if (!name) {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
const def = claudeJson.mcpServers?.[name];
|
|
118
|
+
if (def == null) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
const cloned = JSON.parse(JSON.stringify(def));
|
|
122
|
+
(0, sensitive_1.maskSecretsInPlace)(cloned, `${claudeJsonPath}#mcpServers.${name}`, agent, env);
|
|
123
|
+
merged.mcpServers[name] = cloned;
|
|
124
|
+
}
|
|
125
|
+
const diskRel = '.agentdock-mcp.json';
|
|
126
|
+
const content = JSON.stringify(merged, null, 2);
|
|
127
|
+
await (0, fs_1.writeTextFile)(node_path_1.default.join(payloadSourcesRoot, diskRel), content);
|
|
128
|
+
sources.push({ id: `${agent}-mcp-merged`, kind: 'file', from: toFrom(diskRel), to: '.claude.json' });
|
|
129
|
+
}
|
|
130
|
+
// --- Everything else ---
|
|
131
|
+
for (const entry of iterEntries(domain)) {
|
|
132
|
+
if (entry.kind === 'mcp') {
|
|
133
|
+
continue; // handled above
|
|
134
|
+
}
|
|
135
|
+
if (!(await (0, fs_1.pathExists)(entry.path))) {
|
|
136
|
+
throw new Error(`SCAN_EXPORT_MISSING_SOURCE: ${entry.path} (scanned but no longer present)`);
|
|
137
|
+
}
|
|
138
|
+
const rel = toPosix(node_path_1.default.relative(scanRoot, entry.path));
|
|
139
|
+
const stat = await promises_1.default.stat(entry.path);
|
|
140
|
+
const kind = stat.isDirectory() ? 'directory' : 'file';
|
|
141
|
+
const targetDisk = node_path_1.default.join(payloadSourcesRoot, rel);
|
|
142
|
+
if (kind === 'directory') {
|
|
143
|
+
// verbatim recursive copy (skill dirs); re-injection skipped for directories in v0.3
|
|
144
|
+
await (0, fs_1.copyDirectorySafe)(entry.path, targetDisk);
|
|
145
|
+
}
|
|
146
|
+
else if (entry.kind === 'settings') {
|
|
147
|
+
if (node_path_1.default.extname(entry.path) === '.toml') {
|
|
148
|
+
// Codex config.toml: parse as TOML, mask secrets at the object
|
|
149
|
+
// level (same detectors as JSON), re-emit as TOML so the restore
|
|
150
|
+
// lands back as a valid .codex/config.toml.
|
|
151
|
+
try {
|
|
152
|
+
const parsed = (0, smol_toml_1.parse)(await promises_1.default.readFile(entry.path, 'utf8'));
|
|
153
|
+
(0, sensitive_1.maskSecretsInPlace)(parsed, entry.path, agent, env);
|
|
154
|
+
await (0, fs_1.writeTextFile)(targetDisk, (0, smol_toml_1.stringify)(parsed));
|
|
155
|
+
}
|
|
156
|
+
catch (parseErr) {
|
|
157
|
+
// Malformed TOML: fall back to a verbatim copy so export never
|
|
158
|
+
// crashes. Secrets in such a file are NOT masked here; `doctor`
|
|
159
|
+
// still flags leaked tokens in the resulting package.
|
|
160
|
+
await (0, fs_1.copyFileSafe)(entry.path, targetDisk);
|
|
161
|
+
console.warn(`[agentdock] 警告:无法解析 ${entry.path}(${String(parseErr.message)}),已按原样复制(机密未脱敏,请用 doctor 检查)`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
try {
|
|
166
|
+
const obj = JSON.parse(await promises_1.default.readFile(entry.path, 'utf8'));
|
|
167
|
+
(0, sensitive_1.maskSecretsInPlace)(obj, entry.path, agent, env);
|
|
168
|
+
await (0, fs_1.writeTextFile)(targetDisk, JSON.stringify(obj, null, 2));
|
|
169
|
+
}
|
|
170
|
+
catch (parseErr) {
|
|
171
|
+
await (0, fs_1.copyFileSafe)(entry.path, targetDisk);
|
|
172
|
+
console.warn(`[agentdock] 警告:无法解析 ${entry.path}(${String(parseErr.message)}),已按原样复制(机密未脱敏,请用 doctor 检查)`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
// skill .md / agent .md / plugin json / hook script / memory md:
|
|
178
|
+
// copied verbatim. In v0.2 `scan` isolates secrets only from settings
|
|
179
|
+
// + mcpServers, so masking free text or plugin registries here would
|
|
180
|
+
// risk false-positive corruption. Re-injection is handled at the
|
|
181
|
+
// object level for JSON secrets above.
|
|
182
|
+
await (0, fs_1.copyFileSafe)(entry.path, targetDisk);
|
|
183
|
+
}
|
|
184
|
+
sources.push({ id: entry.id, kind, from: toFrom(rel), to: rel });
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const installPlan = {
|
|
188
|
+
overwrite: options.overwrite ?? false,
|
|
189
|
+
sources,
|
|
190
|
+
templates: [],
|
|
191
|
+
};
|
|
192
|
+
const snapshotPath = node_path_1.default.join(outputPath, 'manifest.resolved.json');
|
|
193
|
+
const installPlanPath = node_path_1.default.join(outputPath, 'meta', 'install-plan.json');
|
|
194
|
+
const packageMetadataPath = node_path_1.default.join(outputPath, 'package.json');
|
|
195
|
+
await (0, fs_1.writeJsonFile)(snapshotPath, manifest);
|
|
196
|
+
await (0, fs_1.writeJsonFile)(installPlanPath, installPlan);
|
|
197
|
+
await (0, fs_1.writeJsonFile)(packageMetadataPath, {
|
|
198
|
+
name: 'agentdock-scan-package',
|
|
199
|
+
version: '1.0.0',
|
|
200
|
+
project: manifest.project.name,
|
|
201
|
+
manifestVersion: 3,
|
|
202
|
+
});
|
|
203
|
+
return { outputPath, snapshotPath, installPlanPath };
|
|
204
|
+
}
|
|
205
|
+
finally {
|
|
206
|
+
await (0, fs_1.releaseLock)(lockHandle, lockPath);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.runScan = runScan;
|
|
7
|
+
exports.defaultScanRoot = defaultScanRoot;
|
|
8
|
+
const node_os_1 = __importDefault(require("node:os"));
|
|
9
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
10
|
+
const yaml_1 = __importDefault(require("yaml"));
|
|
11
|
+
const claude_scanner_1 = require("../scanners/claude-scanner");
|
|
12
|
+
const codex_scanner_1 = require("../scanners/codex-scanner");
|
|
13
|
+
const sensitive_1 = require("../scanners/sensitive");
|
|
14
|
+
const fs_1 = require("../utils/fs");
|
|
15
|
+
function countDomain(domain) {
|
|
16
|
+
return {
|
|
17
|
+
mcp: domain.mcp.length,
|
|
18
|
+
skills: domain.skills.length,
|
|
19
|
+
agents: domain.agents.length,
|
|
20
|
+
plugins: domain.plugins.length,
|
|
21
|
+
hooks: domain.hooks.length,
|
|
22
|
+
memory: domain.memory.length,
|
|
23
|
+
settings: domain.settings.length,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function renderReport(manifest, reportData) {
|
|
27
|
+
const lines = ['# AgentDock Scan Report', ''];
|
|
28
|
+
lines.push(`- Agent: \`${reportData.agent}\``);
|
|
29
|
+
lines.push(`- Secrets isolated: **${reportData.secretsCount}**`);
|
|
30
|
+
lines.push('');
|
|
31
|
+
const claude = manifest.agents.claude;
|
|
32
|
+
if (claude) {
|
|
33
|
+
lines.push('## Claude Code', '');
|
|
34
|
+
lines.push(`- MCP servers: ${claude.mcp.length}`);
|
|
35
|
+
lines.push(`- Skills: ${claude.skills.length}`);
|
|
36
|
+
lines.push(`- Agents: ${claude.agents.length}`);
|
|
37
|
+
lines.push(`- Plugins: ${claude.plugins.length}`);
|
|
38
|
+
lines.push(`- Hooks: ${claude.hooks.length}`);
|
|
39
|
+
lines.push(`- Memory files: ${claude.memory.length}`);
|
|
40
|
+
lines.push(`- settings.json: ${claude.settings.length ? 'yes' : 'no'}`);
|
|
41
|
+
lines.push('');
|
|
42
|
+
}
|
|
43
|
+
const codex = manifest.agents.codex;
|
|
44
|
+
if (codex) {
|
|
45
|
+
lines.push('## Codex (OpenAI)', '');
|
|
46
|
+
lines.push(`- MCP servers: ${codex.mcp.length} (captured inside config.toml)`);
|
|
47
|
+
lines.push(`- Skills: ${codex.skills.length}`);
|
|
48
|
+
lines.push(`- Agents: ${codex.agents.length}`);
|
|
49
|
+
lines.push(`- Plugins: ${codex.plugins.length}`);
|
|
50
|
+
lines.push(`- Hooks: ${codex.hooks.length}`);
|
|
51
|
+
lines.push(`- Memory files: ${codex.memory.length}`);
|
|
52
|
+
lines.push(`- config.toml: ${codex.settings.length ? 'yes' : 'no'}`);
|
|
53
|
+
lines.push('');
|
|
54
|
+
}
|
|
55
|
+
if (reportData.secretsCount > 0) {
|
|
56
|
+
lines.push('## Isolated secrets', '');
|
|
57
|
+
for (const secret of manifest.secrets) {
|
|
58
|
+
lines.push(`- \`${secret.key}\` — from \`${secret.source}\`${secret.sample ? ` (sample ${secret.sample})` : ''}`);
|
|
59
|
+
}
|
|
60
|
+
lines.push('');
|
|
61
|
+
lines.push('> These values were NOT written to disk. Fill `.env.example` on the target machine.');
|
|
62
|
+
lines.push('');
|
|
63
|
+
}
|
|
64
|
+
if (reportData.skipped.length > 0) {
|
|
65
|
+
lines.push('## Run-state skipped (never exported)', '');
|
|
66
|
+
for (const item of reportData.skipped) {
|
|
67
|
+
lines.push(`- \`${item}\``);
|
|
68
|
+
}
|
|
69
|
+
lines.push('');
|
|
70
|
+
}
|
|
71
|
+
if (reportData.notes.length > 0) {
|
|
72
|
+
lines.push('## Notes', '');
|
|
73
|
+
for (const note of reportData.notes) {
|
|
74
|
+
lines.push(`- ${note}`);
|
|
75
|
+
}
|
|
76
|
+
lines.push('');
|
|
77
|
+
}
|
|
78
|
+
return lines.join('\n');
|
|
79
|
+
}
|
|
80
|
+
async function runScan(options) {
|
|
81
|
+
const agents = {};
|
|
82
|
+
const secrets = [];
|
|
83
|
+
const skipped = [];
|
|
84
|
+
const notes = [];
|
|
85
|
+
const counts = {};
|
|
86
|
+
if (options.agent === 'claude' || options.agent === 'all') {
|
|
87
|
+
const result = await (0, claude_scanner_1.scanClaude)(options.root);
|
|
88
|
+
agents.claude = result.domain;
|
|
89
|
+
secrets.push(...result.secrets);
|
|
90
|
+
skipped.push(...result.skipped);
|
|
91
|
+
notes.push(...result.notes);
|
|
92
|
+
counts.claude = countDomain(result.domain);
|
|
93
|
+
}
|
|
94
|
+
if (options.agent === 'codex' || options.agent === 'all') {
|
|
95
|
+
const result = await (0, codex_scanner_1.scanCodex)(options.root);
|
|
96
|
+
agents.codex = result.domain;
|
|
97
|
+
secrets.push(...result.secrets);
|
|
98
|
+
skipped.push(...result.skipped);
|
|
99
|
+
notes.push(...result.notes);
|
|
100
|
+
counts.codex = countDomain(result.domain);
|
|
101
|
+
}
|
|
102
|
+
const manifest = {
|
|
103
|
+
version: 3,
|
|
104
|
+
project: { name: `${node_path_1.default.basename(options.root) || 'local'}-ai-env` },
|
|
105
|
+
agents,
|
|
106
|
+
secrets,
|
|
107
|
+
outputs: { type: 'directory', path: options.out },
|
|
108
|
+
};
|
|
109
|
+
await (0, fs_1.ensureDirectory)(options.out);
|
|
110
|
+
const manifestPath = node_path_1.default.join(options.out, 'agentdock.scan.yml');
|
|
111
|
+
await (0, fs_1.writeTextFile)(manifestPath, yaml_1.default.stringify(manifest, null, 2));
|
|
112
|
+
const envExamplePath = node_path_1.default.join(options.out, '.env.example');
|
|
113
|
+
await (0, fs_1.writeTextFile)(envExamplePath, (0, sensitive_1.renderEnvExample)(secrets));
|
|
114
|
+
const report = {
|
|
115
|
+
agent: options.agent,
|
|
116
|
+
manifestPath,
|
|
117
|
+
envExamplePath,
|
|
118
|
+
reportPath: node_path_1.default.join(options.out, 'scan-report.md'),
|
|
119
|
+
counts,
|
|
120
|
+
secretsCount: secrets.length,
|
|
121
|
+
skipped,
|
|
122
|
+
notes,
|
|
123
|
+
};
|
|
124
|
+
await (0, fs_1.writeTextFile)(report.reportPath, renderReport(manifest, report));
|
|
125
|
+
return { manifest, report };
|
|
126
|
+
}
|
|
127
|
+
function defaultScanRoot() {
|
|
128
|
+
return node_os_1.default.homedir();
|
|
129
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.resolveSourceDestination = resolveSourceDestination;
|
|
7
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
8
|
+
function resolveSourceDestination(source) {
|
|
9
|
+
if (source.destination && source.destination.trim().length > 0) {
|
|
10
|
+
return source.destination;
|
|
11
|
+
}
|
|
12
|
+
if (source.type === 'file') {
|
|
13
|
+
return `./${node_path_1.default.basename(source.path)}`;
|
|
14
|
+
}
|
|
15
|
+
return `./${source.id}`;
|
|
16
|
+
}
|