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,30 @@
|
|
|
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.renderTemplateFile = renderTemplateFile;
|
|
7
|
+
const promises_1 = __importDefault(require("node:fs/promises"));
|
|
8
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
const TEMPLATE_VARIABLE_PATTERN = /\{\{\s*([A-Z0-9_]+)\s*\}\}/g;
|
|
10
|
+
async function renderTemplateFile(template, manifestDirectory) {
|
|
11
|
+
const templatePath = node_path_1.default.resolve(manifestDirectory, template.source);
|
|
12
|
+
const rawContent = await promises_1.default.readFile(templatePath, 'utf8');
|
|
13
|
+
const variables = template.variables ?? {};
|
|
14
|
+
const missingVariables = new Set();
|
|
15
|
+
const content = rawContent.replace(TEMPLATE_VARIABLE_PATTERN, (_match, variableName) => {
|
|
16
|
+
const value = variables[variableName];
|
|
17
|
+
if (value === undefined) {
|
|
18
|
+
missingVariables.add(variableName);
|
|
19
|
+
return _match;
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
});
|
|
23
|
+
if (missingVariables.size > 0) {
|
|
24
|
+
throw new Error(`Missing template variable(s) for ${template.id}: ${Array.from(missingVariables).join(', ')}`);
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
fileName: node_path_1.default.basename(template.source),
|
|
28
|
+
content,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
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.loadManifest = loadManifest;
|
|
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
|
+
async function loadManifest(manifestPath) {
|
|
11
|
+
const absolutePath = node_path_1.default.resolve(manifestPath);
|
|
12
|
+
const fileContent = await promises_1.default.readFile(absolutePath, 'utf8');
|
|
13
|
+
const data = yaml_1.default.parse(fileContent);
|
|
14
|
+
return {
|
|
15
|
+
path: absolutePath,
|
|
16
|
+
directory: node_path_1.default.dirname(absolutePath),
|
|
17
|
+
data,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
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.validateManifest = validateManifest;
|
|
7
|
+
const _2020_1 = __importDefault(require("ajv/dist/2020"));
|
|
8
|
+
const agentdock_schema_json_1 = __importDefault(require("../../schemas/agentdock.schema.json"));
|
|
9
|
+
const ajv = new _2020_1.default({ allErrors: true, strict: false });
|
|
10
|
+
const validateSchema = ajv.compile(agentdock_schema_json_1.default);
|
|
11
|
+
function formatErrorPath(error) {
|
|
12
|
+
if ('missingProperty' in (error.params ?? {})) {
|
|
13
|
+
const missingProperty = String(error.params?.missingProperty ?? 'unknown');
|
|
14
|
+
const basePath = error.instancePath.replace(/^\//, '').replace(/\//g, '.');
|
|
15
|
+
return basePath ? `${basePath}.${missingProperty}` : missingProperty;
|
|
16
|
+
}
|
|
17
|
+
if (error.instancePath) {
|
|
18
|
+
return error.instancePath.replace(/^\//, '').replace(/\//g, '/');
|
|
19
|
+
}
|
|
20
|
+
return error.schemaPath.replace(/^#\//, '');
|
|
21
|
+
}
|
|
22
|
+
function validateManifest(manifest) {
|
|
23
|
+
const valid = validateSchema(manifest);
|
|
24
|
+
const errors = [];
|
|
25
|
+
if (!valid) {
|
|
26
|
+
for (const error of validateSchema.errors ?? []) {
|
|
27
|
+
errors.push(`${formatErrorPath(error)}: ${error.message}`);
|
|
28
|
+
}
|
|
29
|
+
return { valid: false, errors };
|
|
30
|
+
}
|
|
31
|
+
const typedManifest = manifest;
|
|
32
|
+
const seenSourceIds = new Set();
|
|
33
|
+
const seenTemplateIds = new Set();
|
|
34
|
+
for (const source of typedManifest.sources) {
|
|
35
|
+
if (seenSourceIds.has(source.id)) {
|
|
36
|
+
errors.push(`Duplicate source id: ${source.id}`);
|
|
37
|
+
}
|
|
38
|
+
seenSourceIds.add(source.id);
|
|
39
|
+
if (source.type === 'file' && ((source.include?.length ?? 0) > 0 || (source.exclude?.length ?? 0) > 0)) {
|
|
40
|
+
errors.push(`Source ${source.id} cannot use include/exclude when type=file`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
for (const template of typedManifest.templates ?? []) {
|
|
44
|
+
if (seenTemplateIds.has(template.id)) {
|
|
45
|
+
errors.push(`Duplicate template id: ${template.id}`);
|
|
46
|
+
}
|
|
47
|
+
seenTemplateIds.add(template.id);
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
valid: errors.length === 0,
|
|
51
|
+
errors,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
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.RUN_STATE_NAMES = void 0;
|
|
7
|
+
exports.scanClaude = scanClaude;
|
|
8
|
+
const promises_1 = __importDefault(require("node:fs/promises"));
|
|
9
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
10
|
+
const sensitive_1 = require("./sensitive");
|
|
11
|
+
/**
|
|
12
|
+
* Directories/files that are runtime state, not definitions. They are never
|
|
13
|
+
* scanned or exported — even with `--include-secrets`.
|
|
14
|
+
*/
|
|
15
|
+
exports.RUN_STATE_NAMES = new Set([
|
|
16
|
+
'auth.json',
|
|
17
|
+
'.credentials.json',
|
|
18
|
+
'logs.sqlite',
|
|
19
|
+
'history.sqlite',
|
|
20
|
+
'cache',
|
|
21
|
+
'goals',
|
|
22
|
+
'projects',
|
|
23
|
+
]);
|
|
24
|
+
const HOOK_EXTENSIONS = ['.ps1', '.sh', '.js', '.ts', '.py'];
|
|
25
|
+
async function exists(targetPath) {
|
|
26
|
+
try {
|
|
27
|
+
await promises_1.default.access(targetPath);
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function recordSecrets(node, basePath, agent, secrets) {
|
|
35
|
+
const found = [];
|
|
36
|
+
(0, sensitive_1.collectSecrets)(node, basePath, found);
|
|
37
|
+
const seen = new Set();
|
|
38
|
+
for (const item of found) {
|
|
39
|
+
if (seen.has(item.jsonPath)) {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
seen.add(item.jsonPath);
|
|
43
|
+
secrets.push({
|
|
44
|
+
key: (0, sensitive_1.secretKeyName)(agent, item.jsonPath),
|
|
45
|
+
source: item.jsonPath,
|
|
46
|
+
sample: (0, sensitive_1.maskSecret)(item.value),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async function collectHookScripts(hooksDir, skipped) {
|
|
51
|
+
const out = [];
|
|
52
|
+
async function walk(dir) {
|
|
53
|
+
const entries = await promises_1.default.readdir(dir, { withFileTypes: true });
|
|
54
|
+
for (const entry of entries) {
|
|
55
|
+
const full = node_path_1.default.join(dir, entry.name);
|
|
56
|
+
if (entry.isDirectory()) {
|
|
57
|
+
if (exports.RUN_STATE_NAMES.has(entry.name)) {
|
|
58
|
+
skipped.push(full);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
await walk(full);
|
|
62
|
+
}
|
|
63
|
+
else if (HOOK_EXTENSIONS.some((ext) => entry.name.endsWith(ext))) {
|
|
64
|
+
out.push(full);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
await walk(hooksDir);
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
function pushEntry(domain, key, entry) {
|
|
72
|
+
domain[key].push(entry);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Scan a Claude Code environment rooted at `homeDir` (~).
|
|
76
|
+
* Discovers mcp / skills / agents / plugins / hooks / memory / settings,
|
|
77
|
+
* isolates secrets, and skips run-state.
|
|
78
|
+
*/
|
|
79
|
+
async function scanClaude(homeDir) {
|
|
80
|
+
const claudeDir = node_path_1.default.join(homeDir, '.claude');
|
|
81
|
+
const claudeJsonPath = node_path_1.default.join(homeDir, '.claude.json');
|
|
82
|
+
const domain = {
|
|
83
|
+
mcp: [],
|
|
84
|
+
skills: [],
|
|
85
|
+
agents: [],
|
|
86
|
+
plugins: [],
|
|
87
|
+
hooks: [],
|
|
88
|
+
memory: [],
|
|
89
|
+
settings: [],
|
|
90
|
+
};
|
|
91
|
+
const secrets = [];
|
|
92
|
+
const skipped = [];
|
|
93
|
+
const notes = [];
|
|
94
|
+
// settings.json (mixed definitions + possibly tokens in `env`)
|
|
95
|
+
const settingsPath = node_path_1.default.join(claudeDir, 'settings.json');
|
|
96
|
+
if (await exists(settingsPath)) {
|
|
97
|
+
pushEntry(domain, 'settings', {
|
|
98
|
+
id: 'claude-settings',
|
|
99
|
+
kind: 'settings',
|
|
100
|
+
path: settingsPath,
|
|
101
|
+
ref: 'settings.json',
|
|
102
|
+
});
|
|
103
|
+
try {
|
|
104
|
+
const obj = JSON.parse(await promises_1.default.readFile(settingsPath, 'utf8'));
|
|
105
|
+
recordSecrets(obj, settingsPath, 'claude', secrets);
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
notes.push('settings.json 解析失败,仅记录路径');
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// .claude.json → mcpServers
|
|
112
|
+
if (await exists(claudeJsonPath)) {
|
|
113
|
+
try {
|
|
114
|
+
const obj = JSON.parse(await promises_1.default.readFile(claudeJsonPath, 'utf8'));
|
|
115
|
+
const mcpServers = obj.mcpServers ?? {};
|
|
116
|
+
for (const [name, definition] of Object.entries(mcpServers)) {
|
|
117
|
+
pushEntry(domain, 'mcp', {
|
|
118
|
+
id: `mcp-${name}`,
|
|
119
|
+
kind: 'mcp',
|
|
120
|
+
path: claudeJsonPath,
|
|
121
|
+
ref: `.claude.json#mcpServers.${name}`,
|
|
122
|
+
});
|
|
123
|
+
recordSecrets(definition, `${claudeJsonPath}#mcpServers.${name}`, 'claude', secrets);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
notes.push('.claude.json 解析失败,跳过 MCP 提取');
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// skills/* (directories containing SKILL.md)
|
|
131
|
+
const skillsDir = node_path_1.default.join(claudeDir, 'skills');
|
|
132
|
+
if (await exists(skillsDir)) {
|
|
133
|
+
const entries = await promises_1.default.readdir(skillsDir, { withFileTypes: true });
|
|
134
|
+
for (const entry of entries) {
|
|
135
|
+
if (entry.isDirectory()) {
|
|
136
|
+
pushEntry(domain, 'skills', {
|
|
137
|
+
id: `skill-${entry.name}`,
|
|
138
|
+
kind: 'skill',
|
|
139
|
+
path: node_path_1.default.join(skillsDir, entry.name),
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
// agents/*.md
|
|
145
|
+
const agentsDir = node_path_1.default.join(claudeDir, 'agents');
|
|
146
|
+
if (await exists(agentsDir)) {
|
|
147
|
+
const files = await promises_1.default.readdir(agentsDir);
|
|
148
|
+
for (const file of files) {
|
|
149
|
+
if (file.endsWith('.md')) {
|
|
150
|
+
pushEntry(domain, 'agents', {
|
|
151
|
+
id: `agent-${file.replace(/\.md$/, '')}`,
|
|
152
|
+
kind: 'agent',
|
|
153
|
+
path: node_path_1.default.join(agentsDir, file),
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
// plugins (registry files only)
|
|
159
|
+
const pluginsDir = node_path_1.default.join(claudeDir, 'plugins');
|
|
160
|
+
if (await exists(pluginsDir)) {
|
|
161
|
+
for (const file of ['installed_plugins.json', 'known_marketplaces.json']) {
|
|
162
|
+
const pluginPath = node_path_1.default.join(pluginsDir, file);
|
|
163
|
+
if (await exists(pluginPath)) {
|
|
164
|
+
pushEntry(domain, 'plugins', {
|
|
165
|
+
id: `plugin-${file.replace(/\.json$/, '')}`,
|
|
166
|
+
kind: 'plugin',
|
|
167
|
+
path: pluginPath,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
// hooks/* (scripts)
|
|
173
|
+
const hooksDir = node_path_1.default.join(claudeDir, 'hooks');
|
|
174
|
+
if (await exists(hooksDir)) {
|
|
175
|
+
const scripts = await collectHookScripts(hooksDir, skipped);
|
|
176
|
+
for (const script of scripts) {
|
|
177
|
+
pushEntry(domain, 'hooks', {
|
|
178
|
+
id: `hook-${node_path_1.default.basename(script)}`,
|
|
179
|
+
kind: 'hook',
|
|
180
|
+
path: script,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
// memory (CLAUDE.md family)
|
|
185
|
+
for (const file of ['CLAUDE.md', 'CLAUDE.local.md']) {
|
|
186
|
+
const memoryPath = node_path_1.default.join(claudeDir, file);
|
|
187
|
+
if (await exists(memoryPath)) {
|
|
188
|
+
pushEntry(domain, 'memory', {
|
|
189
|
+
id: `memory-${file.replace(/\.md$/, '')}`,
|
|
190
|
+
kind: 'memory',
|
|
191
|
+
path: memoryPath,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return { domain, secrets, skipped, notes };
|
|
196
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
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.scanCodex = scanCodex;
|
|
7
|
+
const promises_1 = __importDefault(require("node:fs/promises"));
|
|
8
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
const smol_toml_1 = require("smol-toml");
|
|
10
|
+
const sensitive_1 = require("./sensitive");
|
|
11
|
+
const claude_scanner_1 = require("./claude-scanner");
|
|
12
|
+
/**
|
|
13
|
+
* Codex-specific run-state artifacts. `auth.json` holds live credentials and
|
|
14
|
+
* `logs.sqlite` / `history.sqlite` are per-machine runtime state — none of
|
|
15
|
+
* these should ever be scanned or exported.
|
|
16
|
+
*/
|
|
17
|
+
const CODEX_RUN_STATE_NAMES = new Set([
|
|
18
|
+
'auth.json',
|
|
19
|
+
'logs.sqlite',
|
|
20
|
+
'history.sqlite',
|
|
21
|
+
'cache',
|
|
22
|
+
'projects',
|
|
23
|
+
'goals',
|
|
24
|
+
'todos',
|
|
25
|
+
'sessions',
|
|
26
|
+
'ephemeral',
|
|
27
|
+
]);
|
|
28
|
+
async function exists(targetPath) {
|
|
29
|
+
try {
|
|
30
|
+
await promises_1.default.access(targetPath);
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function recordSecrets(node, basePath, agent, secrets) {
|
|
38
|
+
const found = [];
|
|
39
|
+
(0, sensitive_1.collectSecrets)(node, basePath, found);
|
|
40
|
+
const seen = new Set();
|
|
41
|
+
for (const item of found) {
|
|
42
|
+
if (seen.has(item.jsonPath)) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
seen.add(item.jsonPath);
|
|
46
|
+
secrets.push({
|
|
47
|
+
key: (0, sensitive_1.secretKeyName)(agent, item.jsonPath),
|
|
48
|
+
source: item.jsonPath,
|
|
49
|
+
sample: (0, sensitive_1.maskSecret)(item.value),
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function pushEntry(domain, key, entry) {
|
|
54
|
+
domain[key].push(entry);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Scan a Codex (OpenAI) environment rooted at `homeDir` (~/.codex).
|
|
58
|
+
*
|
|
59
|
+
* Codex stores its configuration in a single TOML file (`config.toml`) that
|
|
60
|
+
* mixes definitions (mcp_servers, model, provider) with secrets (env maps inside
|
|
61
|
+
* mcp_servers, provider tokens). We capture `config.toml` wholesale as a
|
|
62
|
+
* `settings` entry (the scan→export bridge masks secrets in it at the object
|
|
63
|
+
* level and restores it as TOML), and `AGENTS.md` as a memory entry.
|
|
64
|
+
*
|
|
65
|
+
* Live credentials live in `auth.json` and runtime state in `logs.sqlite` /
|
|
66
|
+
* `history.sqlite` — those are never scanned or exported.
|
|
67
|
+
*/
|
|
68
|
+
async function scanCodex(homeDir) {
|
|
69
|
+
const codexDir = node_path_1.default.join(homeDir, '.codex');
|
|
70
|
+
const domain = {
|
|
71
|
+
mcp: [],
|
|
72
|
+
skills: [],
|
|
73
|
+
agents: [],
|
|
74
|
+
plugins: [],
|
|
75
|
+
hooks: [],
|
|
76
|
+
memory: [],
|
|
77
|
+
settings: [],
|
|
78
|
+
};
|
|
79
|
+
const secrets = [];
|
|
80
|
+
const skipped = [];
|
|
81
|
+
const notes = [];
|
|
82
|
+
// config.toml (the whole configuration, including mcp_servers + secrets)
|
|
83
|
+
const configPath = node_path_1.default.join(codexDir, 'config.toml');
|
|
84
|
+
if (await exists(configPath)) {
|
|
85
|
+
pushEntry(domain, 'settings', {
|
|
86
|
+
id: 'codex-config',
|
|
87
|
+
kind: 'settings',
|
|
88
|
+
path: configPath,
|
|
89
|
+
ref: 'config.toml',
|
|
90
|
+
});
|
|
91
|
+
try {
|
|
92
|
+
const parsed = (0, smol_toml_1.parse)(await promises_1.default.readFile(configPath, 'utf8'));
|
|
93
|
+
recordSecrets(parsed, configPath, 'codex', secrets);
|
|
94
|
+
const mcpServers = parsed.mcp_servers;
|
|
95
|
+
if (mcpServers && typeof mcpServers === 'object') {
|
|
96
|
+
const names = Object.keys(mcpServers);
|
|
97
|
+
if (names.length > 0) {
|
|
98
|
+
// mcp_servers live inside config.toml, so they are captured as part
|
|
99
|
+
// of the `settings` entry above (not as separate `mcp` entries, which
|
|
100
|
+
// the bridge would otherwise aggregate into a Claude-style .claude.json)
|
|
101
|
+
notes.push(`Codex mcp_servers (captured in config.toml): ${names.join(', ')}`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
notes.push(`.codex/config.toml 解析失败(${error.message}),仅记录路径`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
notes.push('未找到 .codex/config.toml');
|
|
111
|
+
}
|
|
112
|
+
// AGENTS.md (project/user instructions — memory)
|
|
113
|
+
const agentsMdPath = node_path_1.default.join(codexDir, 'AGENTS.md');
|
|
114
|
+
if (await exists(agentsMdPath)) {
|
|
115
|
+
pushEntry(domain, 'memory', {
|
|
116
|
+
id: 'codex-agents-md',
|
|
117
|
+
kind: 'memory',
|
|
118
|
+
path: agentsMdPath,
|
|
119
|
+
ref: 'AGENTS.md',
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
// Run-state artifacts that must never be exported (note + skip)
|
|
123
|
+
for (const name of CODEX_RUN_STATE_NAMES) {
|
|
124
|
+
const candidate = node_path_1.default.join(codexDir, name);
|
|
125
|
+
if (await exists(candidate)) {
|
|
126
|
+
skipped.push(candidate);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
// also honor the shared run-state names for parity
|
|
130
|
+
for (const name of claude_scanner_1.RUN_STATE_NAMES) {
|
|
131
|
+
if (CODEX_RUN_STATE_NAMES.has(name)) {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
const candidate = node_path_1.default.join(codexDir, name);
|
|
135
|
+
if (await exists(candidate)) {
|
|
136
|
+
skipped.push(candidate);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return { domain, secrets, skipped, notes };
|
|
140
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Sensitive-information detection for `scan`.
|
|
4
|
+
*
|
|
5
|
+
* Two-layer guard: a secret is flagged when EITHER its field name looks like a
|
|
6
|
+
* credential (env / token / apiKey / secret / ...) OR its value matches a known
|
|
7
|
+
* token shape (sk-..., ghp_..., xoxb-..., JWT, ...). This keeps false negatives
|
|
8
|
+
* low for the AI-assistant config files we target, where tokens often sit in
|
|
9
|
+
* generic `env` maps.
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.isSecretKey = isSecretKey;
|
|
13
|
+
exports.looksLikeSecret = looksLikeSecret;
|
|
14
|
+
exports.findSecretLeaks = findSecretLeaks;
|
|
15
|
+
exports.maskSecret = maskSecret;
|
|
16
|
+
exports.collectSecrets = collectSecrets;
|
|
17
|
+
exports.secretKeyName = secretKeyName;
|
|
18
|
+
exports.maskSecretsInPlace = maskSecretsInPlace;
|
|
19
|
+
exports.renderEnvExample = renderEnvExample;
|
|
20
|
+
const SECRET_KEY_PATTERN = /(token|api[_-]?key|secret|password|passwd|auth|private[_-]?key|credential|access[_-]?key)/i;
|
|
21
|
+
const SECRET_VALUE_PATTERNS = [
|
|
22
|
+
/^sk-[a-zA-Z0-9]/, // OpenAI
|
|
23
|
+
/^sk-ant-[a-zA-Z0-9]/, // Anthropic
|
|
24
|
+
/^gh[pousr]_[a-zA-Z0-9]/, // GitHub
|
|
25
|
+
/^xox[baprs]-[a-zA-Z0-9]/, // Slack
|
|
26
|
+
/^eyJ[a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]+\./, // JWT
|
|
27
|
+
/^AKIA[0-9A-Z]{16}/, // AWS
|
|
28
|
+
/^glpat-[a-zA-Z0-9]/, // GitLab
|
|
29
|
+
/^Bearer\s+/i,
|
|
30
|
+
/^AIza[0-9A-Za-z_-]{35}/, // Google
|
|
31
|
+
];
|
|
32
|
+
function isSecretKey(name) {
|
|
33
|
+
return SECRET_KEY_PATTERN.test(name);
|
|
34
|
+
}
|
|
35
|
+
function looksLikeSecret(value) {
|
|
36
|
+
if (typeof value !== 'string' || value.length < 8) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
return SECRET_VALUE_PATTERNS.some((pattern) => pattern.test(value));
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Scan arbitrary text (e.g. a free-text skill/agent/memory file) for leaked
|
|
43
|
+
* secret tokens. Tokenizes on whitespace and common delimiters, then tests each
|
|
44
|
+
* token against the same value patterns used by `looksLikeSecret`.
|
|
45
|
+
*
|
|
46
|
+
* `scan` only masks JSON `settings`/`mcpServers` — it copies free-text files
|
|
47
|
+
* (skills/agents/memory/hooks/plugins) verbatim. `doctor` uses this to catch
|
|
48
|
+
* real tokens that would otherwise ride along on `export`.
|
|
49
|
+
*/
|
|
50
|
+
function findSecretLeaks(text) {
|
|
51
|
+
const found = new Set();
|
|
52
|
+
for (const line of text.split(/\r?\n/)) {
|
|
53
|
+
const tokens = line.split(/[\s"'`=,:;(){}[\]<>]+/).filter(Boolean);
|
|
54
|
+
for (const token of tokens) {
|
|
55
|
+
if (token.length < 8) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
// Skip tokens that are already masked/placeholder — `maskSecret` emits
|
|
59
|
+
// `xxxx****yyyy` and `scan` uses `{{KEY}}` placeholders. A real secret is
|
|
60
|
+
// never masked, so this avoids false positives when `doctor` audits a scan
|
|
61
|
+
// artifact or install package that legitimately contains masked samples.
|
|
62
|
+
if (token.includes('*') || token.includes('{{')) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
for (const pattern of SECRET_VALUE_PATTERNS) {
|
|
66
|
+
const match = pattern.exec(token);
|
|
67
|
+
if (match) {
|
|
68
|
+
found.add(match[0]);
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const bearer = /Bearer\s+([A-Za-z0-9._-]+)/i.exec(line);
|
|
74
|
+
if (bearer) {
|
|
75
|
+
found.add(`Bearer ${bearer[1]}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return [...found];
|
|
79
|
+
}
|
|
80
|
+
function maskSecret(value) {
|
|
81
|
+
if (value.length <= 4) {
|
|
82
|
+
return '****';
|
|
83
|
+
}
|
|
84
|
+
return `${value.slice(0, 4)}****${value.slice(-4)}`;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Recursively walk a parsed JSON value and collect every leaf that looks like a
|
|
88
|
+
* secret. `seen` guards against cyclic objects (e.g. shared sub-objects).
|
|
89
|
+
*/
|
|
90
|
+
function collectSecrets(node, basePath, accumulator, seen = new WeakSet()) {
|
|
91
|
+
if (node === null || typeof node !== 'object') {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (seen.has(node)) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
seen.add(node);
|
|
98
|
+
if (Array.isArray(node)) {
|
|
99
|
+
node.forEach((item, index) => {
|
|
100
|
+
collectSecrets(item, `${basePath}[${index}]`, accumulator, seen);
|
|
101
|
+
});
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
for (const [key, value] of Object.entries(node)) {
|
|
105
|
+
const childPath = `${basePath}.${key}`;
|
|
106
|
+
if (typeof value === 'string') {
|
|
107
|
+
if (isSecretKey(key) || looksLikeSecret(value)) {
|
|
108
|
+
accumulator.push({ key, value, jsonPath: childPath });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else if (typeof value === 'object' && value !== null) {
|
|
112
|
+
collectSecrets(value, childPath, accumulator, seen);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function secretKeyName(agent, jsonPath) {
|
|
117
|
+
const leaf = jsonPath.split('.').pop() ?? 'SECRET';
|
|
118
|
+
const cleaned = leaf.replace(/[^A-Za-z0-9]/g, '_').toUpperCase();
|
|
119
|
+
return `AGENTDOCK_${agent.toUpperCase()}_${cleaned}`;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Replace every secret value in a parsed JSON object with its `{{KEY}}` placeholder,
|
|
123
|
+
* mutating in place. Mirrors the detection used during `scan` so the placeholder names
|
|
124
|
+
* match what `renderEnvExample` emitted. Used by the scan→package bridge.
|
|
125
|
+
*
|
|
126
|
+
* When `secretsEnv` is supplied (keyed by the secret placeholder name, e.g.
|
|
127
|
+
* `AGENTDOCK_CLUDE_GITHUB_TOKEN`), the real value is written back directly instead of
|
|
128
|
+
* the placeholder — this keeps masking and re-injection on the same object so they can
|
|
129
|
+
* never desync (no fragile string placeholder matching).
|
|
130
|
+
*/
|
|
131
|
+
function maskSecretsInPlace(node, basePath, agent, secretsEnv, seen = new WeakSet()) {
|
|
132
|
+
if (node === null || typeof node !== 'object') {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (seen.has(node)) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
seen.add(node);
|
|
139
|
+
if (Array.isArray(node)) {
|
|
140
|
+
node.forEach((item, index) => {
|
|
141
|
+
maskSecretsInPlace(item, `${basePath}[${index}]`, agent, secretsEnv, seen);
|
|
142
|
+
});
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
for (const [key, value] of Object.entries(node)) {
|
|
146
|
+
const childPath = `${basePath}.${key}`;
|
|
147
|
+
if (typeof value === 'string') {
|
|
148
|
+
if (isSecretKey(key) || looksLikeSecret(value)) {
|
|
149
|
+
const secretName = secretKeyName(agent, childPath);
|
|
150
|
+
node[key] =
|
|
151
|
+
secretsEnv && secretsEnv[secretName] !== undefined ? secretsEnv[secretName] : `{{${secretName}}}`;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
else if (typeof value === 'object' && value !== null) {
|
|
155
|
+
maskSecretsInPlace(value, childPath, agent, secretsEnv, seen);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Render a `.env.example` from isolated secrets. Only placeholder keys are
|
|
161
|
+
* written — never the real values, which stay on the source machine.
|
|
162
|
+
*/
|
|
163
|
+
function renderEnvExample(secrets) {
|
|
164
|
+
const lines = ['# AgentDock scan — fill these in before `agentdock install`', ''];
|
|
165
|
+
if (secrets.length === 0) {
|
|
166
|
+
lines.push('# (no secrets detected)');
|
|
167
|
+
return lines.join('\n');
|
|
168
|
+
}
|
|
169
|
+
for (const secret of secrets) {
|
|
170
|
+
lines.push(`# source: ${secret.source}`);
|
|
171
|
+
if (secret.sample) {
|
|
172
|
+
lines.push(`# sample: ${secret.sample}`);
|
|
173
|
+
}
|
|
174
|
+
lines.push(`${secret.key}=`);
|
|
175
|
+
lines.push('');
|
|
176
|
+
}
|
|
177
|
+
return lines.join('\n');
|
|
178
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
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.toJsonError = toJsonError;
|
|
7
|
+
exports.toJsonLine = toJsonLine;
|
|
8
|
+
const package_json_1 = __importDefault(require("../../package.json"));
|
|
9
|
+
function toJsonError(code, message) {
|
|
10
|
+
return { code, message };
|
|
11
|
+
}
|
|
12
|
+
function toJsonLine(command, success, data, errors) {
|
|
13
|
+
const report = {
|
|
14
|
+
schemaVersion: 1,
|
|
15
|
+
generatedAt: new Date().toISOString(),
|
|
16
|
+
toolVersion: package_json_1.default.version,
|
|
17
|
+
command,
|
|
18
|
+
success,
|
|
19
|
+
data,
|
|
20
|
+
errors,
|
|
21
|
+
};
|
|
22
|
+
return JSON.stringify(report);
|
|
23
|
+
}
|