@bonesofspring/ai-rules 0.2.52 → 0.2.53
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/CHANGELOG.md +24 -0
- package/README.md +27 -3
- package/bin/cli.js +551 -60
- package/bin/lib/engine-scripts.js +10 -0
- package/bin/lib/mcp/clients.js +168 -0
- package/bin/lib/mcp/credentials.js +298 -0
- package/bin/lib/mcp/doctor.js +394 -0
- package/bin/lib/mcp/prompt.js +115 -0
- package/bin/lib/mcp/registry.js +170 -0
- package/bin/lib/mcp/run.js +123 -0
- package/bin/lib/merged-config.js +45 -0
- package/bin/lib/seed-ai-local-team.js +5 -0
- package/bin/lib/tool-installs.js +27 -0
- package/docs/MCP-SETUP.md +62 -0
- package/docs/PRESET-CONTRIBUTION.md +3 -1
- package/docs/TEAM-DB.md +173 -0
- package/docs/openapi/team-db.v1.json +5085 -0
- package/mcp/gitlab.mjs +123 -0
- package/mcp/lib/envfile.mjs +126 -0
- package/mcp/lib/yandex.mjs +384 -0
- package/mcp/yandex-mcp-proxy.mjs +155 -0
- package/mcp/yandex-token-refresh.mjs +65 -0
- package/package.json +14 -1
- package/presets/_shared/ai-local/scripts/manifest.mjs +99 -0
- package/presets/_shared/ai-local/scripts/team-db-api.mjs +1275 -0
- package/presets/_shared/ai-local/scripts/team-db-dashboard.mjs +1067 -0
- package/presets/_shared/ai-local/scripts/team-db-openapi.mjs +1301 -0
- package/presets/_shared/ai-local/scripts/team-db-project.mjs +1506 -0
- package/presets/_shared/ai-local/scripts/team-db-schema.mjs +831 -0
- package/presets/_shared/ai-local/scripts/team-db.mjs +251 -0
- package/presets/_shared/hooks/chain-team-phases.sh.tmpl +109 -0
- package/presets/claude/android-kotlin/hooks/chain-team-phases.sh +109 -0
- package/presets/claude/android-kotlin/team/README.md +10 -0
- package/presets/claude/go/hooks/chain-team-phases.sh +109 -0
- package/presets/claude/go/team/README.md +10 -0
- package/presets/claude/ios-swift/hooks/chain-team-phases.sh +109 -0
- package/presets/claude/ios-swift/team/README.md +10 -0
- package/presets/claude/java/hooks/chain-team-phases.sh +109 -0
- package/presets/claude/java/team/README.md +10 -0
- package/presets/claude/mcp-ts/hooks/chain-team-phases.sh +109 -0
- package/presets/claude/mcp-ts/team/README.md +10 -0
- package/presets/claude/next/hooks/chain-team-phases.sh +109 -0
- package/presets/claude/next/team/README.md +10 -0
- package/presets/claude/nuxt/hooks/chain-team-phases.sh +109 -0
- package/presets/claude/nuxt/team/README.md +10 -0
- package/presets/claude/php-hexagonal/hooks/chain-team-phases.sh +109 -0
- package/presets/claude/php-hexagonal/team/README.md +10 -0
- package/presets/claude/php-laravel/hooks/chain-team-phases.sh +109 -0
- package/presets/claude/php-laravel/team/README.md +10 -0
- package/presets/claude/svelte/hooks/chain-team-phases.sh +109 -0
- package/presets/claude/svelte/team/README.md +10 -0
- package/presets/cursor/android-kotlin/hooks/chain-team-phases.sh +109 -0
- package/presets/cursor/android-kotlin/team/README.md +10 -0
- package/presets/cursor/go/hooks/chain-team-phases.sh +109 -0
- package/presets/cursor/go/team/README.md +10 -0
- package/presets/cursor/ios-swift/hooks/chain-team-phases.sh +109 -0
- package/presets/cursor/ios-swift/team/README.md +10 -0
- package/presets/cursor/java/hooks/chain-team-phases.sh +109 -0
- package/presets/cursor/java/team/README.md +10 -0
- package/presets/cursor/mcp-ts/hooks/chain-team-phases.sh +109 -0
- package/presets/cursor/mcp-ts/team/README.md +10 -0
- package/presets/cursor/next/MCP.md +7 -0
- package/presets/cursor/next/hooks/chain-team-phases.sh +109 -0
- package/presets/cursor/next/team/README.md +10 -0
- package/presets/cursor/nuxt/hooks/chain-team-phases.sh +109 -0
- package/presets/cursor/nuxt/team/README.md +10 -0
- package/presets/cursor/php-hexagonal/hooks/chain-team-phases.sh +109 -0
- package/presets/cursor/php-hexagonal/team/README.md +10 -0
- package/presets/cursor/php-laravel/hooks/chain-team-phases.sh +109 -0
- package/presets/cursor/php-laravel/team/README.md +10 -0
- package/presets/cursor/svelte/hooks/chain-team-phases.sh +109 -0
- package/presets/cursor/svelte/team/README.md +10 -0
- package/presets/mavis/android-kotlin/adapter-map.json +1 -1
- package/presets/mavis/android-kotlin/hooks/chain-team-phases.sh +109 -0
- package/presets/mavis/android-kotlin/team/README.md +10 -0
- package/presets/mavis/go/adapter-map.json +1 -1
- package/presets/mavis/go/hooks/chain-team-phases.sh +109 -0
- package/presets/mavis/go/team/README.md +10 -0
- package/presets/mavis/ios-swift/adapter-map.json +1 -1
- package/presets/mavis/ios-swift/hooks/chain-team-phases.sh +109 -0
- package/presets/mavis/ios-swift/team/README.md +10 -0
- package/presets/mavis/java/adapter-map.json +1 -1
- package/presets/mavis/java/hooks/chain-team-phases.sh +109 -0
- package/presets/mavis/java/team/README.md +10 -0
- package/presets/mavis/mcp-ts/adapter-map.json +1 -1
- package/presets/mavis/mcp-ts/hooks/chain-team-phases.sh +109 -0
- package/presets/mavis/mcp-ts/team/README.md +10 -0
- package/presets/mavis/next/MCP.md +7 -0
- package/presets/mavis/next/adapter-map.json +1 -1
- package/presets/mavis/next/hooks/chain-team-phases.sh +109 -0
- package/presets/mavis/next/team/README.md +10 -0
- package/presets/mavis/nuxt/adapter-map.json +1 -1
- package/presets/mavis/nuxt/hooks/chain-team-phases.sh +109 -0
- package/presets/mavis/nuxt/team/README.md +10 -0
- package/presets/mavis/php-hexagonal/adapter-map.json +1 -1
- package/presets/mavis/php-hexagonal/hooks/chain-team-phases.sh +109 -0
- package/presets/mavis/php-hexagonal/team/README.md +10 -0
- package/presets/mavis/php-laravel/adapter-map.json +1 -1
- package/presets/mavis/php-laravel/hooks/chain-team-phases.sh +109 -0
- package/presets/mavis/php-laravel/team/README.md +10 -0
- package/presets/mavis/svelte/adapter-map.json +1 -1
- package/presets/mavis/svelte/hooks/chain-team-phases.sh +109 -0
- package/presets/mavis/svelte/team/README.md +10 -0
- package/presets/opencode/android-kotlin/adapter-map.json +1 -1
- package/presets/opencode/android-kotlin/hooks/chain-team-phases.sh +109 -0
- package/presets/opencode/android-kotlin/team/README.md +10 -0
- package/presets/opencode/go/adapter-map.json +1 -1
- package/presets/opencode/go/hooks/chain-team-phases.sh +109 -0
- package/presets/opencode/go/team/README.md +10 -0
- package/presets/opencode/ios-swift/adapter-map.json +1 -1
- package/presets/opencode/ios-swift/hooks/chain-team-phases.sh +109 -0
- package/presets/opencode/ios-swift/team/README.md +10 -0
- package/presets/opencode/java/adapter-map.json +1 -1
- package/presets/opencode/java/hooks/chain-team-phases.sh +109 -0
- package/presets/opencode/java/team/README.md +10 -0
- package/presets/opencode/mcp-ts/adapter-map.json +1 -1
- package/presets/opencode/mcp-ts/hooks/chain-team-phases.sh +109 -0
- package/presets/opencode/mcp-ts/team/README.md +10 -0
- package/presets/opencode/next/MCP.md +7 -0
- package/presets/opencode/next/adapter-map.json +1 -1
- package/presets/opencode/next/hooks/chain-team-phases.sh +109 -0
- package/presets/opencode/next/team/README.md +10 -0
- package/presets/opencode/nuxt/adapter-map.json +1 -1
- package/presets/opencode/nuxt/hooks/chain-team-phases.sh +109 -0
- package/presets/opencode/nuxt/team/README.md +10 -0
- package/presets/opencode/php-hexagonal/adapter-map.json +1 -1
- package/presets/opencode/php-hexagonal/hooks/chain-team-phases.sh +109 -0
- package/presets/opencode/php-hexagonal/team/README.md +10 -0
- package/presets/opencode/php-laravel/adapter-map.json +1 -1
- package/presets/opencode/php-laravel/hooks/chain-team-phases.sh +109 -0
- package/presets/opencode/php-laravel/team/README.md +10 -0
- package/presets/opencode/svelte/adapter-map.json +1 -1
- package/presets/opencode/svelte/hooks/chain-team-phases.sh +109 -0
- package/presets/opencode/svelte/team/README.md +10 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client registration for the MCP pack.
|
|
3
|
+
*
|
|
4
|
+
* One logical registry (registry.js), four native renderings. Everything is
|
|
5
|
+
* validated before the first write, a second run is byte-identical (the text
|
|
6
|
+
* guard keeps the mtime), and an entry is ours only when it carries the exact
|
|
7
|
+
* ownership argv — no state file, no absolute path, no token.
|
|
8
|
+
*/
|
|
9
|
+
const fs = require('fs');
|
|
10
|
+
const path = require('path');
|
|
11
|
+
const { assertSafeDest } = require('../safe-path');
|
|
12
|
+
const { createInstallTransaction, isPlainObject, readMergeableJson, runStamp } = require('../fs-utils');
|
|
13
|
+
const {
|
|
14
|
+
CLIENT_IDS,
|
|
15
|
+
clientById,
|
|
16
|
+
clientPresent,
|
|
17
|
+
isLegacyEntry,
|
|
18
|
+
ownsEntry,
|
|
19
|
+
renderEntry,
|
|
20
|
+
} = require('./registry');
|
|
21
|
+
|
|
22
|
+
function detectClientIds(cwd, markers) {
|
|
23
|
+
return CLIENT_IDS.filter((id) => clientPresent(cwd, clientById(id), markers));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @returns {{ clients: object[], warnings: string[] }} Explicit `--tools` may name
|
|
28
|
+
* an undetected client (the user may be about to install it) but says so.
|
|
29
|
+
*/
|
|
30
|
+
function selectClients({ cwd, tools, markers }) {
|
|
31
|
+
const warnings = [];
|
|
32
|
+
if (tools && tools.length > 0) {
|
|
33
|
+
const clients = tools.map((id) => clientById(id));
|
|
34
|
+
for (const client of clients) {
|
|
35
|
+
if (!clientPresent(cwd, client, markers)) {
|
|
36
|
+
warnings.push(`${client.id} is not installed here; registered anyway`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return { clients, warnings };
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
clients: detectClientIds(cwd, markers).map((id) => clientById(id)),
|
|
43
|
+
warnings,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function seedDocument(client) {
|
|
48
|
+
if (client.shape === 'local') {
|
|
49
|
+
return { $schema: 'https://opencode.ai/config.json', [client.key]: {} };
|
|
50
|
+
}
|
|
51
|
+
return { [client.key]: {} };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function readClientConfig(cwd, client) {
|
|
55
|
+
const rel = client.file;
|
|
56
|
+
const abs = assertSafeDest(cwd, path.join(cwd, rel), rel);
|
|
57
|
+
const existing = readMergeableJson(abs, rel, client.key);
|
|
58
|
+
const text = existing === null ? null : fs.readFileSync(abs, 'utf8');
|
|
59
|
+
return { rel, abs, existing, text };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function planClient({ cwd, client, servers, version, replace }) {
|
|
63
|
+
const { rel, abs, existing, text } = readClientConfig(cwd, client);
|
|
64
|
+
const doc = existing === null ? seedDocument(client) : structuredClone(existing);
|
|
65
|
+
if (!isPlainObject(doc[client.key])) {
|
|
66
|
+
doc[client.key] = {};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const warnings = [];
|
|
70
|
+
for (const server of servers) {
|
|
71
|
+
const current = doc[client.key][server.id];
|
|
72
|
+
if (current === undefined || ownsEntry(current) || isLegacyEntry(current, server)) {
|
|
73
|
+
doc[client.key][server.id] = renderEntry(client, server, version);
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (replace) {
|
|
77
|
+
doc[client.key][server.id] = renderEntry(client, server, version);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
warnings.push(`${client.id} "${server.id}" is a custom entry; left untouched`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const nextText = `${JSON.stringify(doc, null, 2)}\n`;
|
|
84
|
+
return { client, rel, abs, text, nextText, changed: nextText !== text, warnings };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Validate every target, then write only the files whose text changed. */
|
|
88
|
+
function registerServers({ cwd, clients, servers, version, replace = false }) {
|
|
89
|
+
const plans = clients.map((client) => planClient({ cwd, client, servers, version, replace }));
|
|
90
|
+
const changed = [];
|
|
91
|
+
if (plans.some((plan) => plan.changed)) {
|
|
92
|
+
const tx = createInstallTransaction(cwd, path.join(cwd, '.ai-rules', '.rollback', runStamp()));
|
|
93
|
+
for (const plan of plans) {
|
|
94
|
+
if (plan.changed) {
|
|
95
|
+
tx.writeFile(plan.abs, plan.nextText, plan.rel);
|
|
96
|
+
changed.push(plan.rel);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
tx.commit();
|
|
100
|
+
}
|
|
101
|
+
return { changed, warnings: plans.flatMap((plan) => plan.warnings) };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function planRemoval({ cwd, client, servers }) {
|
|
105
|
+
const { rel, abs, existing, text } = readClientConfig(cwd, client);
|
|
106
|
+
if (existing === null) {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
const doc = structuredClone(existing);
|
|
110
|
+
if (!isPlainObject(doc[client.key])) {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const warnings = [];
|
|
115
|
+
const removed = [];
|
|
116
|
+
for (const server of servers) {
|
|
117
|
+
const current = doc[client.key][server.id];
|
|
118
|
+
if (current === undefined) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (ownsEntry(current) || isLegacyEntry(current, server)) {
|
|
122
|
+
delete doc[client.key][server.id];
|
|
123
|
+
removed.push(server.id);
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
warnings.push(`${client.id} "${server.id}" is a custom entry; left untouched`);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const nextText = `${JSON.stringify(doc, null, 2)}\n`;
|
|
130
|
+
return {
|
|
131
|
+
client,
|
|
132
|
+
rel,
|
|
133
|
+
abs,
|
|
134
|
+
nextText,
|
|
135
|
+
changed: removed.length > 0 && nextText !== text,
|
|
136
|
+
removed,
|
|
137
|
+
warnings,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Delete only entries whose ownership argv proves they are ours; never the file. */
|
|
142
|
+
function removeServers({ cwd, clients, servers }) {
|
|
143
|
+
const plans = clients.map((client) => planRemoval({ cwd, client, servers })).filter(Boolean);
|
|
144
|
+
const changed = [];
|
|
145
|
+
const removed = [];
|
|
146
|
+
if (plans.some((plan) => plan.changed)) {
|
|
147
|
+
const tx = createInstallTransaction(cwd, path.join(cwd, '.ai-rules', '.rollback', runStamp()));
|
|
148
|
+
for (const plan of plans) {
|
|
149
|
+
if (plan.changed) {
|
|
150
|
+
tx.writeFile(plan.abs, plan.nextText, plan.rel);
|
|
151
|
+
changed.push(plan.rel);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
tx.commit();
|
|
155
|
+
}
|
|
156
|
+
for (const plan of plans) {
|
|
157
|
+
removed.push(...plan.removed);
|
|
158
|
+
}
|
|
159
|
+
return { changed, removed, warnings: plans.flatMap((plan) => plan.warnings) };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
module.exports = {
|
|
163
|
+
detectClientIds,
|
|
164
|
+
selectClients,
|
|
165
|
+
readClientConfig,
|
|
166
|
+
registerServers,
|
|
167
|
+
removeServers,
|
|
168
|
+
};
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider credential setup for the MCP pack.
|
|
3
|
+
*
|
|
4
|
+
* GitLab: URL + token, validated with `GET <url>/api/v4/user` before the write,
|
|
5
|
+
* stored in `.ai-rules/mcp/credentials.env` (0600) inside a self-ignoring dir.
|
|
6
|
+
* Yandex: `yc` detection and one `yc iam create-token` proof; only non-secret
|
|
7
|
+
* settings are written to `.ai-rules/mcp/config.env`. No token is ever stored.
|
|
8
|
+
*
|
|
9
|
+
* Both commands never write the legacy root `.env.gitlab` / `.env.yandex`.
|
|
10
|
+
*/
|
|
11
|
+
const fs = require('fs');
|
|
12
|
+
const path = require('path');
|
|
13
|
+
const { spawnSync } = require('child_process');
|
|
14
|
+
const { assertSafeDest, resolveManagedPath } = require('../safe-path');
|
|
15
|
+
const { createInstallTransaction, runStamp } = require('../fs-utils');
|
|
16
|
+
const { readSecret, ask } = require('./prompt');
|
|
17
|
+
|
|
18
|
+
const CREDENTIALS_REL = '.ai-rules/mcp/credentials.env';
|
|
19
|
+
const CONFIG_REL = '.ai-rules/mcp/config.env';
|
|
20
|
+
const IGNORE_REL = '.ai-rules/.gitignore';
|
|
21
|
+
const IGNORE_TEXT = [
|
|
22
|
+
'# Local ai-rules state (MCP credentials, install snapshots).',
|
|
23
|
+
'# Never commit.',
|
|
24
|
+
'*',
|
|
25
|
+
'',
|
|
26
|
+
].join('\n');
|
|
27
|
+
|
|
28
|
+
function writeFile(cwd, rel, content, mode) {
|
|
29
|
+
const abs = assertSafeDest(cwd, path.join(cwd, rel), rel);
|
|
30
|
+
const tx = createInstallTransaction(cwd, path.join(cwd, '.ai-rules', '.rollback', runStamp()));
|
|
31
|
+
tx.writeFile(abs, content, rel);
|
|
32
|
+
// rename keeps the temp file's mode; chmod makes the guarantee umask-independent.
|
|
33
|
+
fs.chmodSync(abs, mode);
|
|
34
|
+
tx.commit();
|
|
35
|
+
return abs;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Self-ignoring `.ai-rules/` (same mechanism as `.rollback/.gitignore`). Never overwrites. */
|
|
39
|
+
function ensureAiRulesIgnored(cwd) {
|
|
40
|
+
const ignoreAbs = path.join(cwd, IGNORE_REL);
|
|
41
|
+
if (fs.existsSync(ignoreAbs)) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
assertSafeDest(cwd, path.join(cwd, '.ai-rules'), '.ai-rules');
|
|
45
|
+
fs.mkdirSync(path.join(cwd, '.ai-rules'), { recursive: true });
|
|
46
|
+
fs.writeFileSync(ignoreAbs, IGNORE_TEXT);
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* `git check-ignore -q` is the sound probe: exit 0 = ignored, exit 1 = not
|
|
52
|
+
* ignored. `-v` also exits 0 for a negated pattern, which would report an
|
|
53
|
+
* untracked credential file as ignored.
|
|
54
|
+
* @returns {{ status: 'ok' | 'warn' | 'fail', detail: string }}
|
|
55
|
+
*/
|
|
56
|
+
function checkIgnored(cwd, rel) {
|
|
57
|
+
const inside = spawnSync('git', ['rev-parse', '--is-inside-work-tree'], {
|
|
58
|
+
cwd,
|
|
59
|
+
encoding: 'utf8',
|
|
60
|
+
});
|
|
61
|
+
if (inside.status !== 0 || inside.stdout.trim() !== 'true') {
|
|
62
|
+
return { status: 'warn', detail: 'not a git repository; ignore rule not provable here' };
|
|
63
|
+
}
|
|
64
|
+
const result = spawnSync('git', ['check-ignore', '-q', rel], { cwd, encoding: 'utf8' });
|
|
65
|
+
if (result.status === 0) {
|
|
66
|
+
return { status: 'ok', detail: 'ignored' };
|
|
67
|
+
}
|
|
68
|
+
if (result.status === 1) {
|
|
69
|
+
return { status: 'fail', detail: 'git check-ignore: not ignored' };
|
|
70
|
+
}
|
|
71
|
+
const stderr = String(result.stderr ?? '').replace(/\s+/g, ' ').trim();
|
|
72
|
+
return {
|
|
73
|
+
status: 'fail',
|
|
74
|
+
detail: stderr ? `git check-ignore failed: ${stderr}` : `git check-ignore failed (exit ${result.status})`,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function normalizeGitlabUrl(value) {
|
|
79
|
+
const trimmed = String(value ?? '').trim();
|
|
80
|
+
if (trimmed === '') {
|
|
81
|
+
return { url: null, reason: 'empty' };
|
|
82
|
+
}
|
|
83
|
+
const stripped = trimmed.replace(/\/+$/, '');
|
|
84
|
+
let parsed;
|
|
85
|
+
try {
|
|
86
|
+
parsed = new URL(stripped);
|
|
87
|
+
} catch {
|
|
88
|
+
return { url: null, reason: 'not a URL' };
|
|
89
|
+
}
|
|
90
|
+
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
|
|
91
|
+
return { url: null, reason: 'not http(s)' };
|
|
92
|
+
}
|
|
93
|
+
return { url: stripped, reason: parsed.protocol === 'https:' ? null : 'not-https' };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function readCredentials(cwd, envfile) {
|
|
97
|
+
const abs = resolveManagedPath(cwd, CREDENTIALS_REL);
|
|
98
|
+
return abs ? envfile.readEnvFile(abs) : {};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function validateGitlabToken(url, token) {
|
|
102
|
+
let response;
|
|
103
|
+
try {
|
|
104
|
+
response = await fetch(`${url}/api/v4/user`, {
|
|
105
|
+
headers: { 'PRIVATE-TOKEN': token },
|
|
106
|
+
signal: AbortSignal.timeout(15_000),
|
|
107
|
+
});
|
|
108
|
+
} catch {
|
|
109
|
+
return { ok: false, status: 'network', detail: 'instance not reachable' };
|
|
110
|
+
}
|
|
111
|
+
if (response.status === 200) {
|
|
112
|
+
let username = null;
|
|
113
|
+
try {
|
|
114
|
+
const body = await response.json();
|
|
115
|
+
username = typeof body?.username === 'string' ? body.username : null;
|
|
116
|
+
} catch {
|
|
117
|
+
username = null;
|
|
118
|
+
}
|
|
119
|
+
return { ok: true, status: 200, username };
|
|
120
|
+
}
|
|
121
|
+
return { ok: false, status: response.status, detail: `token rejected (${response.status})` };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** @returns {Promise<object>} `{ ok, detail, next?, warnings }` */
|
|
125
|
+
async function setupGitlab({ cwd, options = {}, io = {}, envfile }) {
|
|
126
|
+
const existing = readCredentials(cwd, envfile);
|
|
127
|
+
const warnings = [];
|
|
128
|
+
const prefillUrl = existing.GITLAB_URL ?? '';
|
|
129
|
+
|
|
130
|
+
let urlValue;
|
|
131
|
+
if (options.url) {
|
|
132
|
+
urlValue = options.url;
|
|
133
|
+
} else if (options.yes) {
|
|
134
|
+
if (!prefillUrl) {
|
|
135
|
+
return { ok: false, detail: '--yes requires --url <https-url> or an existing credentials file', next: 'ai-rules mcp setup gitlab --url <https-url>' };
|
|
136
|
+
}
|
|
137
|
+
urlValue = prefillUrl;
|
|
138
|
+
} else {
|
|
139
|
+
urlValue = await (io.ask || ask)('GitLab instance URL', { defaultValue: prefillUrl });
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const normalized = normalizeGitlabUrl(urlValue);
|
|
143
|
+
if (!normalized.url) {
|
|
144
|
+
return { ok: false, detail: `GitLab URL ${normalized.reason}`, next: 'ai-rules mcp setup gitlab --url <https-url>' };
|
|
145
|
+
}
|
|
146
|
+
if (normalized.reason === 'not-https') {
|
|
147
|
+
warnings.push('not https — the token is sent in clear text');
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
let token;
|
|
151
|
+
if (options.yes) {
|
|
152
|
+
if (process.stdin.isTTY) {
|
|
153
|
+
return { ok: false, detail: '--yes reads the token from stdin; pipe it or run without --yes', next: 'ai-rules mcp setup gitlab' };
|
|
154
|
+
}
|
|
155
|
+
token = (await (io.readSecret || readSecret)('GitLab token')).trim();
|
|
156
|
+
} else {
|
|
157
|
+
token = (await (io.readSecret || readSecret)('GitLab token')).trim();
|
|
158
|
+
}
|
|
159
|
+
if (token === '') {
|
|
160
|
+
return { ok: false, detail: 'a GitLab token is required', next: 'ai-rules mcp setup gitlab' };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const validation = await validateGitlabToken(normalized.url, token);
|
|
164
|
+
if (!validation.ok) {
|
|
165
|
+
return {
|
|
166
|
+
ok: false,
|
|
167
|
+
detail: validation.status === 'network' ? validation.detail : `token rejected (${validation.status})`,
|
|
168
|
+
next: 'ai-rules mcp setup gitlab',
|
|
169
|
+
warnings,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
writeFile(
|
|
174
|
+
cwd,
|
|
175
|
+
CREDENTIALS_REL,
|
|
176
|
+
[
|
|
177
|
+
'# GitLab MCP credentials — written by @bonesofspring/ai-rules. Never commit.',
|
|
178
|
+
`GITLAB_URL=${normalized.url}`,
|
|
179
|
+
`GITLAB_TOKEN=${token}`,
|
|
180
|
+
'',
|
|
181
|
+
].join('\n'),
|
|
182
|
+
0o600,
|
|
183
|
+
);
|
|
184
|
+
ensureAiRulesIgnored(cwd);
|
|
185
|
+
const ignored = checkIgnored(cwd, CREDENTIALS_REL);
|
|
186
|
+
if (ignored.status === 'fail') {
|
|
187
|
+
return {
|
|
188
|
+
ok: false,
|
|
189
|
+
detail: `${CREDENTIALS_REL} is not ignored`,
|
|
190
|
+
next: 'ai-rules mcp setup gitlab',
|
|
191
|
+
warnings,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const legacy = envfile.readEnvFile(path.join(cwd, '.env.gitlab'));
|
|
196
|
+
if (typeof legacy.GITLAB_TOKEN === 'string' && legacy.GITLAB_TOKEN !== '' && legacy.GITLAB_TOKEN !== token) {
|
|
197
|
+
warnings.push('.env.gitlab still holds a different token; rotate it too');
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
ok: true,
|
|
202
|
+
username: validation.username,
|
|
203
|
+
detail: validation.username ? `token valid for ${validation.username} — saved to ${CREDENTIALS_REL} (0600, ignored)` : `saved to ${CREDENTIALS_REL} (0600, ignored)`,
|
|
204
|
+
warnings,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async function setupYandex({ cwd, options = {}, io = {}, yandex, envfile }) {
|
|
209
|
+
const existingAbs = resolveManagedPath(cwd, CONFIG_REL);
|
|
210
|
+
const existing = existingAbs ? envfile.readEnvFile(existingAbs) : {};
|
|
211
|
+
const warnings = [];
|
|
212
|
+
|
|
213
|
+
let orgId = options.orgId || existing.YANDEX_ORG_ID || '';
|
|
214
|
+
if (!orgId) {
|
|
215
|
+
if (options.yes) {
|
|
216
|
+
return { ok: false, detail: '--yes requires --org-id <id>', next: 'ai-rules mcp setup yandex --org-id <id>' };
|
|
217
|
+
}
|
|
218
|
+
orgId = await (io.ask || ask)('Yandex organization id');
|
|
219
|
+
}
|
|
220
|
+
if (!orgId) {
|
|
221
|
+
return { ok: false, detail: 'YANDEX_ORG_ID is required', next: 'ai-rules mcp setup yandex --org-id <id>' };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const ycBin = options.ycBin || process.env.YC_BIN || existing.YC_BIN || yandex.DEFAULT_YC_BIN;
|
|
225
|
+
if (!yandex.ycAvailable(ycBin)) {
|
|
226
|
+
return {
|
|
227
|
+
ok: false,
|
|
228
|
+
kind: 'runtime',
|
|
229
|
+
detail: 'yc not found',
|
|
230
|
+
next: 'install the Yandex Cloud CLI (https://yandex.cloud/en/docs/cli/operations/install-cli)',
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const noProxy = existing.NO_PROXY || process.env.NO_PROXY || yandex.DEFAULT_NO_PROXY;
|
|
235
|
+
try {
|
|
236
|
+
await yandex.fetchToken({ interactive: true, ycBin, noProxy });
|
|
237
|
+
} catch (error) {
|
|
238
|
+
const name = error instanceof Error ? error.name : '';
|
|
239
|
+
if (name === 'YcSpawnError') {
|
|
240
|
+
return {
|
|
241
|
+
ok: false,
|
|
242
|
+
kind: 'runtime',
|
|
243
|
+
detail: 'yc not found',
|
|
244
|
+
next: 'install the Yandex Cloud CLI (https://yandex.cloud/en/docs/cli/operations/install-cli)',
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
return {
|
|
248
|
+
ok: false,
|
|
249
|
+
detail: 'yc iam create-token did not return a token',
|
|
250
|
+
next: 'yc init --federation-id <id>',
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const lines = [
|
|
255
|
+
'# Yandex MCP settings — written by @bonesofspring/ai-rules. No token is stored here.',
|
|
256
|
+
`YANDEX_ORG_ID=${orgId}`,
|
|
257
|
+
`NO_PROXY=${noProxy}`,
|
|
258
|
+
];
|
|
259
|
+
const federationId = existing.YANDEX_FEDERATION_ID;
|
|
260
|
+
if (federationId) {
|
|
261
|
+
lines.push(`YANDEX_FEDERATION_ID=${federationId}`);
|
|
262
|
+
}
|
|
263
|
+
if (options.ycBin || existing.YC_BIN) {
|
|
264
|
+
lines.push(`YC_BIN=${options.ycBin || existing.YC_BIN}`);
|
|
265
|
+
}
|
|
266
|
+
lines.push('');
|
|
267
|
+
|
|
268
|
+
writeFile(cwd, CONFIG_REL, lines.join('\n'), 0o644);
|
|
269
|
+
ensureAiRulesIgnored(cwd);
|
|
270
|
+
|
|
271
|
+
return { ok: true, detail: `organization ${orgId} saved to ${CONFIG_REL} (0644, ignored)`, warnings };
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** `mcp remove --purge`: credentials and config only, never `.ai-rules/.gitignore`. */
|
|
275
|
+
function purgeCredentials(cwd) {
|
|
276
|
+
const removed = [];
|
|
277
|
+
for (const rel of [CREDENTIALS_REL, CONFIG_REL]) {
|
|
278
|
+
const abs = resolveManagedPath(cwd, rel);
|
|
279
|
+
if (abs && fs.existsSync(abs)) {
|
|
280
|
+
fs.unlinkSync(abs);
|
|
281
|
+
removed.push(rel);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return removed;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
module.exports = {
|
|
288
|
+
CREDENTIALS_REL,
|
|
289
|
+
CONFIG_REL,
|
|
290
|
+
IGNORE_REL,
|
|
291
|
+
ensureAiRulesIgnored,
|
|
292
|
+
checkIgnored,
|
|
293
|
+
normalizeGitlabUrl,
|
|
294
|
+
readCredentials,
|
|
295
|
+
setupGitlab,
|
|
296
|
+
setupYandex,
|
|
297
|
+
purgeCredentials,
|
|
298
|
+
};
|