@mohammadhprp/system-prompt 0.13.1 → 0.13.2
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/bin/system-prompt.js +1 -1
- package/package.json +1 -1
- package/src/cli/args.js +51 -0
- package/src/cli/clack-ui.js +13 -0
- package/src/cli/index.js +47 -0
- package/src/cli/interactive.js +213 -0
- package/src/cli/plan.js +85 -0
- package/src/cli/tui-preferences.js +118 -0
- package/src/config/mcp.js +25 -0
- package/src/config/opencode.js +47 -0
- package/src/{agent-configs.js → config/tui.js} +0 -76
- package/src/doctor.js +10 -7
- package/src/hash.js +5 -0
- package/src/install/env.js +69 -0
- package/src/install/files.js +151 -0
- package/src/install/index.js +124 -0
- package/src/install/lock.js +159 -0
- package/src/install/merge.js +62 -0
- package/src/install/templates.js +76 -0
- package/src/item-layout.js +5 -0
- package/src/paths.js +59 -0
- package/src/ui.js +32 -0
- package/src/cli.js +0 -508
- package/src/installer.js +0 -653
|
@@ -1,79 +1,3 @@
|
|
|
1
|
-
import { fileURLToPath } from 'node:url';
|
|
2
|
-
import { dirname, resolve } from 'node:path';
|
|
3
|
-
import { readFile } from 'node:fs/promises';
|
|
4
|
-
|
|
5
|
-
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
6
|
-
const packageRoot = resolve(__dirname, '..');
|
|
7
|
-
|
|
8
|
-
export async function loadMcpConfigs(selectedIds) {
|
|
9
|
-
const entries = {};
|
|
10
|
-
for (const id of selectedIds) {
|
|
11
|
-
try {
|
|
12
|
-
const configPath = resolve(packageRoot, `framework/mcps/${id}/configs/opencode.json`);
|
|
13
|
-
const raw = await readFile(configPath, 'utf-8');
|
|
14
|
-
const parsed = JSON.parse(raw);
|
|
15
|
-
if (parsed.mcp) {
|
|
16
|
-
Object.assign(entries, parsed.mcp);
|
|
17
|
-
}
|
|
18
|
-
} catch (error) {
|
|
19
|
-
if (error.code === 'ENOENT') {
|
|
20
|
-
console.warn(` ⚠ No opencode.json config found for MCP: ${id}`);
|
|
21
|
-
continue;
|
|
22
|
-
}
|
|
23
|
-
throw error;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return entries;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function generateOpenCodeConfig({ selections, mcpEntries, includeAgentsMd }) {
|
|
30
|
-
const plugins = [];
|
|
31
|
-
if (selections.plugins?.includes('opencode-goal-plugin')) {
|
|
32
|
-
plugins.push('@prevalentware/opencode-goal-plugin');
|
|
33
|
-
}
|
|
34
|
-
if (selections.plugins?.includes('ponytail')) {
|
|
35
|
-
plugins.push('@dietrichgebert/ponytail');
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const instructions = [];
|
|
39
|
-
if (includeAgentsMd) instructions.push('AGENTS.md');
|
|
40
|
-
if (selections.memory?.length) instructions.push('.opencode/memory/*.md');
|
|
41
|
-
|
|
42
|
-
const config = {
|
|
43
|
-
$schema: 'https://opencode.ai/config.json',
|
|
44
|
-
formatter: true,
|
|
45
|
-
lsp: false,
|
|
46
|
-
instructions,
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
if (plugins.length > 0) {
|
|
50
|
-
config.plugin = plugins;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const references = {};
|
|
54
|
-
if (selections.standards?.length) {
|
|
55
|
-
references.standards = {
|
|
56
|
-
path: 'references/standards',
|
|
57
|
-
description: 'Canonical engineering standards referenced by skills.',
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
if (selections.templates?.length) {
|
|
61
|
-
references.templates = {
|
|
62
|
-
path: 'references/templates',
|
|
63
|
-
description: 'Ready-to-use workflow documents.',
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
if (Object.keys(references).length > 0) {
|
|
67
|
-
config.references = references;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (Object.keys(mcpEntries).length > 0) {
|
|
71
|
-
config.mcp = mcpEntries;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return JSON.stringify(config, null, 4);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
1
|
export const TUI_THEMES = [
|
|
78
2
|
'system',
|
|
79
3
|
'tokyonight',
|
package/src/doctor.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { resolve } from 'node:path';
|
|
2
2
|
import { access, readFile } from 'node:fs/promises';
|
|
3
|
-
import { createHash } from 'node:crypto';
|
|
4
3
|
|
|
5
4
|
import { categories } from './catalog.js';
|
|
6
|
-
import {
|
|
5
|
+
import { hash } from './hash.js';
|
|
7
6
|
import { itemRelativePath } from './item-layout.js';
|
|
7
|
+
import { loadLockFile, lockToSelections } from './install/lock.js';
|
|
8
|
+
import { isMissing } from './paths.js';
|
|
9
|
+
import { status } from './ui.js';
|
|
8
10
|
|
|
9
11
|
async function exists(path) {
|
|
10
12
|
try {
|
|
@@ -17,10 +19,10 @@ async function exists(path) {
|
|
|
17
19
|
|
|
18
20
|
async function checkManagedFile(absTarget, relativePath, expectedHash, issues) {
|
|
19
21
|
try {
|
|
20
|
-
const actual =
|
|
22
|
+
const actual = hash(await readFile(resolve(absTarget, relativePath)));
|
|
21
23
|
if (actual !== expectedHash) issues.push(`Modified managed file: ${relativePath}`);
|
|
22
24
|
} catch (error) {
|
|
23
|
-
if (error
|
|
25
|
+
if (isMissing(error)) issues.push(`Missing managed file: ${relativePath}`);
|
|
24
26
|
else throw error;
|
|
25
27
|
}
|
|
26
28
|
}
|
|
@@ -85,11 +87,12 @@ export async function inspectInstallation(targetDir) {
|
|
|
85
87
|
|
|
86
88
|
export async function doctor(targetDir = '.opencode', output = console.log) {
|
|
87
89
|
const result = await inspectInstallation(targetDir);
|
|
88
|
-
output(`Checking ${result.targetDir}`);
|
|
90
|
+
output(status('info', `Checking ${result.targetDir}`));
|
|
89
91
|
if (result.issues.length === 0) {
|
|
90
|
-
output('No issues found.');
|
|
92
|
+
output(status('success', 'No issues found.'));
|
|
91
93
|
return true;
|
|
92
94
|
}
|
|
93
|
-
|
|
95
|
+
output(status('error', `${result.issues.length} issue${result.issues.length === 1 ? '' : 's'} found:`));
|
|
96
|
+
for (const issue of result.issues) output(` ${status('warn', issue)}`);
|
|
94
97
|
return false;
|
|
95
98
|
}
|
package/src/hash.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { readFile, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
|
|
4
|
+
import { hash } from '../hash.js';
|
|
5
|
+
import { assertSafeDestination, isMissing, resolveSource } from '../paths.js';
|
|
6
|
+
|
|
7
|
+
export async function collectMcpEnvExamples(mcpIds) {
|
|
8
|
+
const combined = new Map();
|
|
9
|
+
for (const id of mcpIds) {
|
|
10
|
+
try {
|
|
11
|
+
const examplePath = resolveSource(`framework/mcps/${id}/configs/.env.example`);
|
|
12
|
+
const content = await readFile(examplePath, 'utf-8');
|
|
13
|
+
const parsed = parseEnv(content);
|
|
14
|
+
for (const [key, value] of parsed) {
|
|
15
|
+
if (!combined.has(key)) combined.set(key, value);
|
|
16
|
+
}
|
|
17
|
+
} catch (error) {
|
|
18
|
+
if (!isMissing(error)) throw error;
|
|
19
|
+
// MCPs may not provide an environment example.
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return combined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function writeMergedEnv(absTarget, examples, options) {
|
|
26
|
+
if (examples.size === 0) return;
|
|
27
|
+
|
|
28
|
+
const envPath = resolve(absTarget, '.env');
|
|
29
|
+
await assertSafeDestination(options.targetDir, envPath);
|
|
30
|
+
const existing = new Map();
|
|
31
|
+
let existingContent = '';
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
existingContent = await readFile(envPath, 'utf-8');
|
|
35
|
+
const parsed = parseEnv(existingContent);
|
|
36
|
+
for (const [key, value] of parsed) existing.set(key, value);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
if (!isMissing(error)) throw error;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const additions = [];
|
|
42
|
+
for (const [key, value] of examples) {
|
|
43
|
+
if (!existing.has(key)) additions.push(`${key}=${value}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (additions.length === 0) return;
|
|
47
|
+
const separator = existingContent && !existingContent.endsWith('\n') ? '\n' : '';
|
|
48
|
+
const content = Buffer.from(`${existingContent}${separator}${additions.join('\n')}\n`);
|
|
49
|
+
if (!options.dryRun) await writeFile(envPath, content);
|
|
50
|
+
options.managedFiles['.env'] = hash(content);
|
|
51
|
+
options.fileOwners['.env'] = { generated: true };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function parseEnv(content) {
|
|
55
|
+
const vars = new Map();
|
|
56
|
+
for (const line of content.split('\n')) {
|
|
57
|
+
const trimmed = line.trim();
|
|
58
|
+
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
59
|
+
const eqIndex = trimmed.indexOf('=');
|
|
60
|
+
if (eqIndex === -1) {
|
|
61
|
+
vars.set(trimmed, '');
|
|
62
|
+
} else {
|
|
63
|
+
const key = trimmed.slice(0, eqIndex).trim();
|
|
64
|
+
const value = trimmed.slice(eqIndex + 1).trim();
|
|
65
|
+
if (key) vars.set(key, value);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return vars;
|
|
69
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { mkdir, readdir, readFile, rm, stat, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { dirname, resolve } from 'node:path';
|
|
3
|
+
|
|
4
|
+
import { categories } from '../catalog.js';
|
|
5
|
+
import { hash } from '../hash.js';
|
|
6
|
+
import {
|
|
7
|
+
isCopyable,
|
|
8
|
+
isFileBased,
|
|
9
|
+
isRemoved,
|
|
10
|
+
itemRelativePath,
|
|
11
|
+
itemSourcePath,
|
|
12
|
+
targetSubdir,
|
|
13
|
+
} from '../item-layout.js';
|
|
14
|
+
import { assertSafeDestination, isMissing, resolveSource } from '../paths.js';
|
|
15
|
+
import { status } from '../ui.js';
|
|
16
|
+
import { getExpectedHash } from './lock.js';
|
|
17
|
+
|
|
18
|
+
async function canWrite(destFile, relativePath, oldLock, force) {
|
|
19
|
+
if (force) return true;
|
|
20
|
+
try {
|
|
21
|
+
const existing = await readFile(destFile);
|
|
22
|
+
const previousHash = getExpectedHash(oldLock, relativePath);
|
|
23
|
+
return Boolean(previousHash && previousHash === hash(existing));
|
|
24
|
+
} catch (error) {
|
|
25
|
+
if (isMissing(error)) return true;
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function writeManagedFile(destFile, content, relativePath, options, owner) {
|
|
31
|
+
await assertSafeDestination(options.targetDir, destFile);
|
|
32
|
+
if (!options.allowExistingMerge && !(await canWrite(destFile, relativePath, options.oldLock, options.force))) {
|
|
33
|
+
console.warn(status('warn', `Preserving existing file: ${relativePath}`));
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
if (!options.dryRun) await mkdir(dirname(destFile), { recursive: true });
|
|
37
|
+
if (!options.dryRun) await writeFile(destFile, content);
|
|
38
|
+
options.managedFiles[relativePath] = hash(content);
|
|
39
|
+
if (owner) options.fileOwners[relativePath] = owner;
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function copyDir(src, dest, relativeDir, options, owner) {
|
|
44
|
+
if (!options.dryRun) await mkdir(dest, { recursive: true });
|
|
45
|
+
const entries = await readdir(src, { withFileTypes: true });
|
|
46
|
+
|
|
47
|
+
for (const entry of entries) {
|
|
48
|
+
if (entry.name === '.DS_Store') continue;
|
|
49
|
+
const srcPath = resolve(src, entry.name);
|
|
50
|
+
const destPath = resolve(dest, entry.name);
|
|
51
|
+
const relativePath = `${relativeDir}/${entry.name}`;
|
|
52
|
+
|
|
53
|
+
if (entry.isDirectory()) {
|
|
54
|
+
await copyDir(srcPath, destPath, relativePath, options, owner);
|
|
55
|
+
} else if (entry.isFile()) {
|
|
56
|
+
const content = await readFile(srcPath);
|
|
57
|
+
await writeManagedFile(destPath, content, relativePath, options, owner);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export async function copySelectedDirs(targetDir, category, selectedIds, options) {
|
|
63
|
+
const catConfig = categories[category];
|
|
64
|
+
if (!catConfig || !selectedIds?.length) return;
|
|
65
|
+
|
|
66
|
+
const destParent = resolve(targetDir, targetSubdir(catConfig.sourceDir));
|
|
67
|
+
|
|
68
|
+
for (const id of selectedIds) {
|
|
69
|
+
if (isRemoved(category, id)) continue;
|
|
70
|
+
|
|
71
|
+
const source = itemSourcePath(category, id);
|
|
72
|
+
const srcPath = resolveSource(source);
|
|
73
|
+
const destPath = resolve(destParent, id);
|
|
74
|
+
await assertSafeDestination(targetDir, destPath);
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
await stat(srcPath);
|
|
78
|
+
await copyDir(srcPath, destPath, itemRelativePath(category, id), options, { category, id });
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (isMissing(error)) {
|
|
81
|
+
console.warn(status('warn', `Source not found: ${source}`));
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export async function copySelectedFiles(targetDir, category, selectedIds, options) {
|
|
90
|
+
const catConfig = categories[category];
|
|
91
|
+
if (!catConfig || !selectedIds?.length) return;
|
|
92
|
+
|
|
93
|
+
const destParent = resolve(targetDir, targetSubdir(catConfig.sourceDir));
|
|
94
|
+
if (!options.dryRun) await mkdir(destParent, { recursive: true });
|
|
95
|
+
|
|
96
|
+
for (const id of selectedIds) {
|
|
97
|
+
if (isRemoved(category, id)) continue;
|
|
98
|
+
|
|
99
|
+
const source = itemSourcePath(category, id);
|
|
100
|
+
const relativePath = itemRelativePath(category, id);
|
|
101
|
+
const destFile = resolve(targetDir, relativePath);
|
|
102
|
+
await assertSafeDestination(targetDir, destFile);
|
|
103
|
+
try {
|
|
104
|
+
const content = await readFile(resolveSource(source));
|
|
105
|
+
await writeManagedFile(destFile, content, relativePath, options, { category, id });
|
|
106
|
+
} catch (error) {
|
|
107
|
+
if (isMissing(error)) {
|
|
108
|
+
console.warn(status('warn', `Source not found: ${source}`));
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
throw error;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export async function deleteSelectedItems(absTarget, category, ids, oldLock, force, dryRun) {
|
|
117
|
+
if (!categories[category] || !ids?.length || !isCopyable(category)) return;
|
|
118
|
+
|
|
119
|
+
for (const id of ids) {
|
|
120
|
+
const relativePath = itemRelativePath(category, id);
|
|
121
|
+
const destPath = resolve(absTarget, relativePath);
|
|
122
|
+
await assertSafeDestination(absTarget, destPath);
|
|
123
|
+
if (!force && oldLock) {
|
|
124
|
+
const managedEntries = Object.entries(oldLock?.[category]?.[id]?.files || {});
|
|
125
|
+
if (managedEntries.length === 0) {
|
|
126
|
+
console.warn(status('warn', `Preserving unmanaged item: ${relativePath}`));
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
let modified = false;
|
|
130
|
+
for (const [path, checksum] of managedEntries) {
|
|
131
|
+
await assertSafeDestination(absTarget, resolve(absTarget, path));
|
|
132
|
+
try {
|
|
133
|
+
if (hash(await readFile(resolve(absTarget, path))) !== checksum) modified = true;
|
|
134
|
+
} catch (error) {
|
|
135
|
+
if (!isMissing(error)) throw error;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (modified) {
|
|
139
|
+
console.warn(status('warn', `Preserving modified item: ${relativePath}`));
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (!isFileBased(category)) {
|
|
143
|
+
if (!dryRun) {
|
|
144
|
+
for (const [path] of managedEntries) await rm(resolve(absTarget, path), { force: true });
|
|
145
|
+
}
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (!dryRun) await rm(destPath, { recursive: true, force: true });
|
|
150
|
+
}
|
|
151
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { mkdir } from 'node:fs/promises';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
|
|
4
|
+
import { categories } from '../catalog.js';
|
|
5
|
+
import { loadMcpConfigs } from '../config/mcp.js';
|
|
6
|
+
import { generateOpenCodeConfig } from '../config/opencode.js';
|
|
7
|
+
import { generateTuiConfig } from '../config/tui.js';
|
|
8
|
+
import { LOCK_CATEGORIES, isCopyable, isFileBased } from '../item-layout.js';
|
|
9
|
+
import { getPackageVersion } from '../paths.js';
|
|
10
|
+
import { collectMcpEnvExamples, writeMergedEnv } from './env.js';
|
|
11
|
+
import { copySelectedDirs, copySelectedFiles, deleteSelectedItems, writeManagedFile } from './files.js';
|
|
12
|
+
import { createLockData, lockToSelections, validateLock, writeLockFile } from './lock.js';
|
|
13
|
+
import { mergeGitignore, mergeJsonFile } from './merge.js';
|
|
14
|
+
import { AGENTS_MD } from './templates.js';
|
|
15
|
+
|
|
16
|
+
export function validateSelections(selections) {
|
|
17
|
+
if (!selections || typeof selections !== 'object' || Array.isArray(selections)) {
|
|
18
|
+
throw new Error('Selections must be an object');
|
|
19
|
+
}
|
|
20
|
+
for (const [category, ids] of Object.entries(selections)) {
|
|
21
|
+
const config = categories[category];
|
|
22
|
+
if (!config || !Array.isArray(ids)) throw new Error(`Unknown or invalid category: ${category}`);
|
|
23
|
+
const knownIds = new Set(config.items.map(item => item.id));
|
|
24
|
+
if (ids.some(id => !knownIds.has(id))) throw new Error(`Unknown ${category} item selected`);
|
|
25
|
+
}
|
|
26
|
+
return selections;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function install({ targetDir, agentType, selections, includeAgentsMd = true, writeAgentsMd, oldSelections, oldLock, force = false, dryRun = false, tuiPreferences }) {
|
|
30
|
+
validateSelections(selections);
|
|
31
|
+
if (oldLock) validateLock(oldLock);
|
|
32
|
+
writeAgentsMd = writeAgentsMd ?? includeAgentsMd;
|
|
33
|
+
const absTarget = resolve(process.cwd(), targetDir);
|
|
34
|
+
if (!dryRun) await mkdir(absTarget, { recursive: true });
|
|
35
|
+
const options = {
|
|
36
|
+
targetDir: absTarget,
|
|
37
|
+
oldLock,
|
|
38
|
+
force,
|
|
39
|
+
dryRun,
|
|
40
|
+
managedFiles: {},
|
|
41
|
+
fileOwners: {},
|
|
42
|
+
};
|
|
43
|
+
if (oldLock) {
|
|
44
|
+
for (const category of LOCK_CATEGORIES) {
|
|
45
|
+
for (const [id, entry] of Object.entries(oldLock[category] || {})) {
|
|
46
|
+
for (const [path, checksum] of Object.entries(entry.files || {})) {
|
|
47
|
+
if (!(path in options.managedFiles)) {
|
|
48
|
+
options.managedFiles[path] = checksum;
|
|
49
|
+
options.fileOwners[path] = { category, id };
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
for (const [path, entry] of Object.entries(oldLock.generated || {})) {
|
|
55
|
+
if (!(path in options.managedFiles)) {
|
|
56
|
+
options.managedFiles[path] = entry.computedHash;
|
|
57
|
+
options.fileOwners[path] = { generated: true };
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Delete items that are no longer selected.
|
|
63
|
+
if (oldSelections) {
|
|
64
|
+
for (const cat of Object.keys(oldSelections)) {
|
|
65
|
+
const oldIds = new Set(oldSelections[cat] || []);
|
|
66
|
+
const newIds = new Set(selections[cat] || []);
|
|
67
|
+
const removedIds = [...oldIds].filter(id => !newIds.has(id));
|
|
68
|
+
await deleteSelectedItems(absTarget, cat, removedIds, oldLock, force, dryRun);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const tasks = [];
|
|
73
|
+
for (const category of LOCK_CATEGORIES) {
|
|
74
|
+
if (!isCopyable(category) || !selections[category]?.length) continue;
|
|
75
|
+
tasks.push(isFileBased(category)
|
|
76
|
+
? copySelectedFiles(absTarget, category, selections[category], options)
|
|
77
|
+
: copySelectedDirs(absTarget, category, selections[category], options));
|
|
78
|
+
}
|
|
79
|
+
await Promise.all(tasks);
|
|
80
|
+
|
|
81
|
+
if (writeAgentsMd) {
|
|
82
|
+
await writeManagedFile(resolve(absTarget, 'AGENTS.md'), Buffer.from(AGENTS_MD), 'AGENTS.md', options, { generated: true });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (agentType === 'opencode') {
|
|
86
|
+
let mcpEntries = {};
|
|
87
|
+
if (selections.mcps?.length) mcpEntries = await loadMcpConfigs(selections.mcps);
|
|
88
|
+
let previousOpenCodeConfig;
|
|
89
|
+
let previousTuiConfig;
|
|
90
|
+
if (oldLock) {
|
|
91
|
+
const previousSelections = lockToSelections(oldLock);
|
|
92
|
+
let previousMcpEntries = {};
|
|
93
|
+
if (previousSelections.mcps?.length) previousMcpEntries = await loadMcpConfigs(previousSelections.mcps);
|
|
94
|
+
previousOpenCodeConfig = JSON.parse(generateOpenCodeConfig({
|
|
95
|
+
selections: previousSelections,
|
|
96
|
+
mcpEntries: previousMcpEntries,
|
|
97
|
+
includeAgentsMd: oldLock.includeAgentsMd ?? true,
|
|
98
|
+
}));
|
|
99
|
+
previousTuiConfig = JSON.parse(generateTuiConfig({ selections: previousSelections, preferences: tuiPreferences }));
|
|
100
|
+
}
|
|
101
|
+
const configJson = generateOpenCodeConfig({ selections, mcpEntries, includeAgentsMd });
|
|
102
|
+
await mergeJsonFile(resolve(absTarget, 'opencode.json'), JSON.parse(configJson), 'opencode.json', options, previousOpenCodeConfig);
|
|
103
|
+
await mergeJsonFile(resolve(absTarget, 'tui.json'), JSON.parse(generateTuiConfig({ selections, preferences: tuiPreferences })), 'tui.json', options, previousTuiConfig);
|
|
104
|
+
await mergeGitignore(resolve(absTarget, '.gitignore'), options);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (selections.mcps?.length) {
|
|
108
|
+
const envExamples = await collectMcpEnvExamples(selections.mcps);
|
|
109
|
+
await writeMergedEnv(absTarget, envExamples, options);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (dryRun) return absTarget;
|
|
113
|
+
const pkgVersion = await getPackageVersion();
|
|
114
|
+
const lockData = createLockData({
|
|
115
|
+
agentType,
|
|
116
|
+
includeAgentsMd,
|
|
117
|
+
selections,
|
|
118
|
+
managedFiles: options.managedFiles,
|
|
119
|
+
fileOwners: options.fileOwners,
|
|
120
|
+
source: `system-prompt@${pkgVersion}`,
|
|
121
|
+
});
|
|
122
|
+
await writeLockFile(absTarget, lockData);
|
|
123
|
+
return absTarget;
|
|
124
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { readFile, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
|
|
4
|
+
import { categories } from '../catalog.js';
|
|
5
|
+
import { hash } from '../hash.js';
|
|
6
|
+
import { LOCK_CATEGORIES, isRemoved, itemSourcePath } from '../item-layout.js';
|
|
7
|
+
import { isMissing } from '../paths.js';
|
|
8
|
+
|
|
9
|
+
export const LOCK_VERSION = 1;
|
|
10
|
+
|
|
11
|
+
export function lockToSelections(lock) {
|
|
12
|
+
const selections = {};
|
|
13
|
+
if (!lock) return selections;
|
|
14
|
+
for (const category of LOCK_CATEGORIES) {
|
|
15
|
+
const ids = Object.keys(lock[category] || {});
|
|
16
|
+
if (ids.length) selections[category] = ids;
|
|
17
|
+
}
|
|
18
|
+
return selections;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function getExpectedHash(oldLock, relativePath) {
|
|
22
|
+
const generated = oldLock?.generated?.[relativePath]?.computedHash;
|
|
23
|
+
if (generated) return generated;
|
|
24
|
+
for (const category of LOCK_CATEGORIES) {
|
|
25
|
+
const entries = oldLock?.[category];
|
|
26
|
+
if (!entries) continue;
|
|
27
|
+
for (const entry of Object.values(entries)) {
|
|
28
|
+
const value = entry?.files?.[relativePath];
|
|
29
|
+
if (value) return value;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function loadLockFile(absTarget) {
|
|
36
|
+
try {
|
|
37
|
+
const content = await readFile(resolve(absTarget, 'system-prompt-lock.json'), 'utf-8');
|
|
38
|
+
const lock = JSON.parse(content);
|
|
39
|
+
validateLock(lock);
|
|
40
|
+
return lock;
|
|
41
|
+
} catch (error) {
|
|
42
|
+
if (isMissing(error)) return null;
|
|
43
|
+
if (error instanceof SyntaxError || error.message?.startsWith('Invalid system-prompt lock')) {
|
|
44
|
+
throw new Error(`${error.message}. Remove or repair system-prompt-lock.json before reinstalling.`);
|
|
45
|
+
}
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function validateLock(lock) {
|
|
51
|
+
if (!lock || typeof lock !== 'object' || Array.isArray(lock)) {
|
|
52
|
+
throw new Error('Invalid system-prompt lock: expected an object');
|
|
53
|
+
}
|
|
54
|
+
if (lock.selections !== undefined || lock.managedFiles !== undefined) {
|
|
55
|
+
throw new Error('Invalid system-prompt lock: legacy lock format no longer supported');
|
|
56
|
+
}
|
|
57
|
+
if (lock.version !== LOCK_VERSION) {
|
|
58
|
+
throw new Error('Invalid system-prompt lock: unsupported version');
|
|
59
|
+
}
|
|
60
|
+
if (typeof lock.agentType !== 'string' || typeof lock.installedAt !== 'string') {
|
|
61
|
+
throw new Error('Invalid system-prompt lock: missing installation metadata');
|
|
62
|
+
}
|
|
63
|
+
if (typeof lock.includeAgentsMd !== 'boolean') {
|
|
64
|
+
throw new Error('Invalid system-prompt lock: includeAgentsMd must be a boolean');
|
|
65
|
+
}
|
|
66
|
+
const allowed = new Set(['version', 'agentType', 'installedAt', 'includeAgentsMd', 'generated', ...LOCK_CATEGORIES]);
|
|
67
|
+
for (const key of Object.keys(lock)) {
|
|
68
|
+
if (!allowed.has(key)) {
|
|
69
|
+
throw new Error(`Invalid system-prompt lock: unknown key ${key}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
for (const category of LOCK_CATEGORIES) {
|
|
73
|
+
const entries = lock[category];
|
|
74
|
+
if (entries === undefined) continue;
|
|
75
|
+
if (!entries || typeof entries !== 'object' || Array.isArray(entries)) {
|
|
76
|
+
throw new Error(`Invalid system-prompt lock: invalid ${category} selection`);
|
|
77
|
+
}
|
|
78
|
+
const config = categories[category];
|
|
79
|
+
const knownIds = new Set(config.items.map(item => item.id));
|
|
80
|
+
for (const [id, entry] of Object.entries(entries)) {
|
|
81
|
+
if (!knownIds.has(id)) {
|
|
82
|
+
throw new Error(`Invalid system-prompt lock: unknown ${category} item`);
|
|
83
|
+
}
|
|
84
|
+
if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
|
|
85
|
+
throw new Error(`Invalid system-prompt lock: invalid ${category} entry`);
|
|
86
|
+
}
|
|
87
|
+
if (typeof entry.source !== 'string' || typeof entry.sourceType !== 'string' || typeof entry.itemPath !== 'string') {
|
|
88
|
+
throw new Error(`Invalid system-prompt lock: invalid ${category} entry source`);
|
|
89
|
+
}
|
|
90
|
+
if (!/^[a-f0-9]{64}$/.test(entry.computedHash || '')) {
|
|
91
|
+
throw new Error(`Invalid system-prompt lock: invalid ${category} entry hash`);
|
|
92
|
+
}
|
|
93
|
+
if (!entry.files || typeof entry.files !== 'object' || Array.isArray(entry.files)) {
|
|
94
|
+
throw new Error(`Invalid system-prompt lock: invalid ${category} entry files`);
|
|
95
|
+
}
|
|
96
|
+
for (const [path, checksum] of Object.entries(entry.files)) {
|
|
97
|
+
if (path.startsWith('/') || path.split('/').includes('..') || !/^[a-f0-9]{64}$/.test(checksum)) {
|
|
98
|
+
throw new Error('Invalid system-prompt lock: unsafe managed file entry');
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const generated = lock.generated;
|
|
104
|
+
if (generated !== undefined) {
|
|
105
|
+
if (!generated || typeof generated !== 'object' || Array.isArray(generated)) {
|
|
106
|
+
throw new Error('Invalid system-prompt lock: generated must be an object');
|
|
107
|
+
}
|
|
108
|
+
for (const [path, entry] of Object.entries(generated)) {
|
|
109
|
+
if (path.startsWith('/') || path.split('/').includes('..') || !/^[a-f0-9]{64}$/.test(entry?.computedHash || '')) {
|
|
110
|
+
throw new Error('Invalid system-prompt lock: unsafe generated file entry');
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function createLockData({ agentType, includeAgentsMd, selections, managedFiles, fileOwners, source }) {
|
|
117
|
+
const lockData = {
|
|
118
|
+
version: LOCK_VERSION,
|
|
119
|
+
agentType,
|
|
120
|
+
installedAt: new Date().toISOString(),
|
|
121
|
+
includeAgentsMd,
|
|
122
|
+
};
|
|
123
|
+
for (const category of LOCK_CATEGORIES) {
|
|
124
|
+
const entries = {};
|
|
125
|
+
for (const id of selections[category] || []) {
|
|
126
|
+
if (isRemoved(category, id)) continue;
|
|
127
|
+
const files = {};
|
|
128
|
+
for (const [path, checksum] of Object.entries(managedFiles)) {
|
|
129
|
+
const owner = fileOwners[path];
|
|
130
|
+
if (owner?.category === category && owner?.id === id) files[path] = checksum;
|
|
131
|
+
}
|
|
132
|
+
entries[id] = {
|
|
133
|
+
source,
|
|
134
|
+
sourceType: 'bundled',
|
|
135
|
+
itemPath: itemSourcePath(category, id),
|
|
136
|
+
computedHash: buildComputedHash(files, id),
|
|
137
|
+
files,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
lockData[category] = entries;
|
|
141
|
+
}
|
|
142
|
+
const generated = {};
|
|
143
|
+
for (const [path, checksum] of Object.entries(managedFiles)) {
|
|
144
|
+
if (fileOwners[path]?.generated) generated[path] = { computedHash: checksum };
|
|
145
|
+
}
|
|
146
|
+
lockData.generated = generated;
|
|
147
|
+
return lockData;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export async function writeLockFile(absTarget, lockData) {
|
|
151
|
+
await writeFile(resolve(absTarget, 'system-prompt-lock.json'), JSON.stringify(lockData, null, 2));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function buildComputedHash(files, fallbackId) {
|
|
155
|
+
const keys = Object.keys(files).sort();
|
|
156
|
+
if (keys.length === 0) return hash(fallbackId);
|
|
157
|
+
if (keys.length === 1) return files[keys[0]];
|
|
158
|
+
return hash(keys.map(path => `${path}:${files[path]}`).join('\n'));
|
|
159
|
+
}
|