netlify 27.18.0 → 27.20.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/dist/commands/base-command.js +21 -0
- package/dist/commands/init/index.js +15 -1
- package/dist/commands/init/init.d.ts +6 -1
- package/dist/commands/init/init.js +101 -11
- package/dist/commands/link/link.js +7 -0
- package/dist/utils/agent-detection.js +1 -1
- package/dist/utils/init/agent-location.d.ts +6 -0
- package/dist/utils/init/agent-location.js +22 -0
- package/dist/utils/init/agent-mcp-config.d.ts +30 -0
- package/dist/utils/init/agent-mcp-config.js +206 -0
- package/dist/utils/init/agent-mcp.d.ts +57 -0
- package/dist/utils/init/agent-mcp.js +539 -0
- package/dist/utils/init/agent-skills-refresh-worker.d.ts +2 -0
- package/dist/utils/init/agent-skills-refresh-worker.js +17 -0
- package/dist/utils/init/agent-skills-refresh.d.ts +37 -0
- package/dist/utils/init/agent-skills-refresh.js +138 -0
- package/dist/utils/init/agent-skills-state.d.ts +16 -0
- package/dist/utils/init/agent-skills-state.js +42 -0
- package/dist/utils/init/agent-skills.d.ts +41 -2
- package/dist/utils/init/agent-skills.js +104 -21
- package/package.json +2 -1
|
@@ -20,6 +20,7 @@ import { handleOptionError, isOptionError } from '../utils/command-error-handler
|
|
|
20
20
|
import { EXIT_CODES } from '../utils/exit-codes.js';
|
|
21
21
|
import { getFrameworksAPIPaths } from '../utils/frameworks-api.js';
|
|
22
22
|
import { getSiteByName } from '../utils/get-site.js';
|
|
23
|
+
import { showAgentSkillsRefreshNotice, startAgentSkillsRefresh } from '../utils/init/agent-skills-refresh.js';
|
|
23
24
|
import { getLocalApiTestOpts } from '../utils/local-api-test-opts.js';
|
|
24
25
|
import { buildAuthorizeUrl } from '../utils/login-url.js';
|
|
25
26
|
import openBrowser from '../utils/open-browser.js';
|
|
@@ -54,6 +55,25 @@ const COMMANDS_WITHOUT_WORKSPACE_OPTIONS = new Set([
|
|
|
54
55
|
* A list of commands where we need to fetch featureflags for config resolution
|
|
55
56
|
*/
|
|
56
57
|
const COMMANDS_WITH_FEATURE_FLAGS = new Set(['build', 'dev', 'deploy']);
|
|
58
|
+
/**
|
|
59
|
+
* Commands that skip the background refresh of Netlify skills. `init` syncs them itself.
|
|
60
|
+
*/
|
|
61
|
+
const COMMANDS_WITHOUT_SKILLS_REFRESH = new Set(['init', 'completion']);
|
|
62
|
+
const refreshAgentSkillsInBackground = async (command) => {
|
|
63
|
+
const { repositoryRoot } = command.netlify;
|
|
64
|
+
if (!repositoryRoot || COMMANDS_WITHOUT_SKILLS_REFRESH.has(command.name())) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
if (!process.argv.includes('--silent')) {
|
|
69
|
+
await showAgentSkillsRefreshNotice({ repositoryRoot });
|
|
70
|
+
}
|
|
71
|
+
await startAgentSkillsRefresh({ repositoryRoot });
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
debug(`${command.name()}:skills`)(error);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
57
77
|
/**
|
|
58
78
|
* Names of options whose values should be scrubbed
|
|
59
79
|
*/
|
|
@@ -189,6 +209,7 @@ export default class BaseCommand extends Command {
|
|
|
189
209
|
debug(`${commandName}:preAction`)('start');
|
|
190
210
|
this.analytics.startTime = process.hrtime.bigint();
|
|
191
211
|
await this.init(actionCommand);
|
|
212
|
+
await refreshAgentSkillsInBackground(actionCommand);
|
|
192
213
|
debug(`${commandName}:preAction`)('end');
|
|
193
214
|
});
|
|
194
215
|
// Wrap the command's action() to set exitOverride when action is registered.
|
|
@@ -1,21 +1,35 @@
|
|
|
1
|
+
import { Option } from 'commander';
|
|
1
2
|
import terminalLink from 'terminal-link';
|
|
3
|
+
import { AUTO_UPDATE_ENV } from '../../utils/init/agent-skills-refresh.js';
|
|
2
4
|
export const createInitCommand = (program) => program
|
|
3
5
|
.command('init')
|
|
4
6
|
.description('Configure continuous deployment for a new or existing project. To create a new project without continuous deployment, use `netlify sites:create`')
|
|
5
7
|
.option('-m, --manual', 'Manually configure a git remote for CI')
|
|
6
8
|
.option('--git-remote-name <name>', 'Name of Git remote to use. e.g. "origin"')
|
|
7
|
-
.option('--skip-agent-setup', 'Skip installing Netlify skills for AI coding agents
|
|
9
|
+
.option('--skip-agent-setup', 'Skip installing Netlify skills and adding the Netlify MCP server for AI coding agents')
|
|
10
|
+
.option('--skip-mcp-setup', 'Skip adding the Netlify MCP server to your AI coding agents')
|
|
11
|
+
.addOption(new Option('--mcp-scope <scope>', 'Add the Netlify MCP server for this project only, or for your user across all projects')
|
|
12
|
+
.choices(['project', 'user'])
|
|
13
|
+
.default('project'))
|
|
14
|
+
.option('--reset-mcp-setup', 'Forget your saved Netlify MCP server choices and ask which AI coding agents to set up')
|
|
8
15
|
.option('--reset-context', 'Replace locally edited Netlify skills with the latest release, and migrate or delete edited copies under renamed or deprecated names')
|
|
9
16
|
.addHelpText('after', () => {
|
|
10
17
|
const docsUrl = 'https://docs.netlify.com/cli/get-started/';
|
|
11
18
|
return `
|
|
19
|
+
Other commands keep the installed Netlify skills up to date in the background, checking at most once an hour.
|
|
20
|
+
Set ${AUTO_UPDATE_ENV}=false to turn that off.
|
|
21
|
+
|
|
12
22
|
For more information about getting started with Netlify CLI, see ${terminalLink(docsUrl, docsUrl, { fallback: false })}
|
|
13
23
|
`;
|
|
14
24
|
})
|
|
15
25
|
.action(async (options, command) => {
|
|
16
26
|
const { init } = await import('./init.js');
|
|
17
27
|
await init(options, command, {
|
|
28
|
+
skipPromptsWithoutTerminal: true,
|
|
18
29
|
setupAgentSkills: !options.skipAgentSetup,
|
|
30
|
+
setupAgentMcp: !options.skipAgentSetup && !options.skipMcpSetup,
|
|
31
|
+
resetMcpSetup: Boolean(options.resetMcpSetup),
|
|
32
|
+
mcpScope: options.mcpScope === 'user' ? 'user' : 'project',
|
|
19
33
|
resetContext: Boolean(options.resetContext),
|
|
20
34
|
});
|
|
21
35
|
});
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import { OptionValues } from 'commander';
|
|
2
2
|
import type BaseCommand from '../base-command.js';
|
|
3
3
|
import type { SiteInfo } from '../../utils/types.js';
|
|
4
|
+
import { type McpScope } from '../../utils/init/agent-mcp.js';
|
|
4
5
|
import { type InitExitCode } from './constants.js';
|
|
5
6
|
type InitExitMessageCustomizer = (code: InitExitCode, defaultMessage: string) => string | undefined;
|
|
6
7
|
type InitExtraOptions = {
|
|
7
8
|
customizeExitMessage?: InitExitMessageCustomizer | undefined;
|
|
8
9
|
exitAfterConfiguringRepo?: boolean | undefined;
|
|
10
|
+
skipPromptsWithoutTerminal?: boolean | undefined;
|
|
9
11
|
setupAgentSkills?: boolean | undefined;
|
|
12
|
+
setupAgentMcp?: boolean | undefined;
|
|
10
13
|
resetContext?: boolean | undefined;
|
|
14
|
+
resetMcpSetup?: boolean | undefined;
|
|
15
|
+
mcpScope?: McpScope | undefined;
|
|
11
16
|
};
|
|
12
|
-
export declare const init: (options: OptionValues, command: BaseCommand, { customizeExitMessage, exitAfterConfiguringRepo, setupAgentSkills: shouldSetupAgentSkills, resetContext, }?: InitExtraOptions) => Promise<SiteInfo>;
|
|
17
|
+
export declare const init: (options: OptionValues, command: BaseCommand, { customizeExitMessage, exitAfterConfiguringRepo, skipPromptsWithoutTerminal, setupAgentSkills: shouldSetupAgentSkills, setupAgentMcp: shouldSetupAgentMcp, resetContext, resetMcpSetup, mcpScope, }?: InitExtraOptions) => Promise<SiteInfo>;
|
|
13
18
|
export {};
|
|
14
19
|
//# sourceMappingURL=init.d.ts.map
|
|
@@ -4,9 +4,12 @@ import { chalk, exit, log, logAndThrowError, netlifyCommand, warn } from '../../
|
|
|
4
4
|
import getRepoData from '../../utils/get-repo-data.js';
|
|
5
5
|
import { ensureNetlifyIgnore } from '../../utils/gitignore.js';
|
|
6
6
|
import { configureRepo } from '../../utils/init/config.js';
|
|
7
|
+
import { isInteractive } from '../../utils/scripted-commands.js';
|
|
7
8
|
import { track } from '../../utils/telemetry/index.js';
|
|
8
9
|
import { link } from '../link/link.js';
|
|
9
10
|
import { sitesCreate } from '../sites/sites-create.js';
|
|
11
|
+
import { findProjectSetupBlocker } from '../../utils/init/agent-location.js';
|
|
12
|
+
import { setupAgentMcp } from '../../utils/init/agent-mcp.js';
|
|
10
13
|
import { setupAgentSkills } from '../../utils/init/agent-skills.js';
|
|
11
14
|
import { getBuildSettings, saveNetlifyToml } from '../../utils/init/utils.js';
|
|
12
15
|
import { LINKED_EXISTING_SITE_EXIT_CODE, LINKED_NEW_SITE_EXIT_CODE } from './constants.js';
|
|
@@ -44,6 +47,49 @@ const logLinkedWithoutGitAndExit = ({ siteInfo, repoError, }) => {
|
|
|
44
47
|
log(`To link this folder to a different project, run ${chalk.cyanBright.bold(`${netlifyCommand()} unlink`)}, then ${chalk.cyanBright.bold(`${netlifyCommand()} init`)}.`);
|
|
45
48
|
return exit();
|
|
46
49
|
};
|
|
50
|
+
const canAnswerPrompts = () => isInteractive() || process.env.TESTING_PROMPTS === 'true';
|
|
51
|
+
const SHELL_SAFE_URL = /^https:\/\/[\w.-]+(:\d+)?\/[\w./-]+$/;
|
|
52
|
+
const logNextStepsAndExit = ({ command, linkedSiteInfo, repoData, }) => {
|
|
53
|
+
const run = (args) => chalk.cyanBright.bold(`${netlifyCommand()} ${args}`);
|
|
54
|
+
const teams = command.netlify.accounts;
|
|
55
|
+
const teamSlug = teams.length === 1 ? teams[0].slug : '<team-slug>';
|
|
56
|
+
log();
|
|
57
|
+
log('Project setup asks questions and there is no terminal to answer them in, so it was skipped.');
|
|
58
|
+
log();
|
|
59
|
+
if (linkedSiteInfo) {
|
|
60
|
+
log(`This folder is linked to the Netlify project ${chalk.cyan(linkedSiteInfo.name)}.`);
|
|
61
|
+
log();
|
|
62
|
+
log(`To deploy to it, run ${run('deploy')}.`);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
if (process.env.NETLIFY_SITE_ID) {
|
|
66
|
+
log('NETLIFY_SITE_ID names a Netlify project that could not be used. See the warning above.');
|
|
67
|
+
log('Set it to a project this account can access, or unset it. Then run one of:');
|
|
68
|
+
}
|
|
69
|
+
else if (command.netlify.state.get('siteId') === undefined) {
|
|
70
|
+
log('This folder is not linked to a Netlify project yet. To continue without a terminal, run one of:');
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
log('The Netlify project saved as the link for this folder could not be used. See the warning above.');
|
|
74
|
+
log(`If you are logged in to the right account, run ${run('unlink')} to remove the saved link. Then run one of:`);
|
|
75
|
+
}
|
|
76
|
+
log();
|
|
77
|
+
if (!('error' in repoData) && SHELL_SAFE_URL.test(repoData.httpsUrl)) {
|
|
78
|
+
log(` ${run(`link --git-remote-url ${repoData.httpsUrl}`)} links the project connected to this repository`);
|
|
79
|
+
}
|
|
80
|
+
log(` ${run('link --id <project-id>')} links an existing project`);
|
|
81
|
+
log(` ${run(`sites:create --name <name> --account-slug ${teamSlug}`)} creates a project and links it`);
|
|
82
|
+
if (teams.length > 1) {
|
|
83
|
+
log();
|
|
84
|
+
log(`Teams: ${teams.map(({ slug }) => slug).join(', ')}`);
|
|
85
|
+
log('Ask the user which team a new project belongs in.');
|
|
86
|
+
}
|
|
87
|
+
log();
|
|
88
|
+
log(`Then run ${run('deploy')} to deploy.`);
|
|
89
|
+
}
|
|
90
|
+
log(`To deploy on every push, run ${run('init')} in a terminal. It asks for access to the Git provider.`);
|
|
91
|
+
return exit();
|
|
92
|
+
};
|
|
47
93
|
const ignoreLinkedSiteForThisRun = (command) => {
|
|
48
94
|
const { site } = command.netlify;
|
|
49
95
|
command.siteId = undefined;
|
|
@@ -213,9 +259,8 @@ const logExistingRepoSetupAndExit = ({ repoUrl, siteName, customizeExitMessage,
|
|
|
213
259
|
// TODO add support for changing GitHub repo in site:config command
|
|
214
260
|
exit();
|
|
215
261
|
};
|
|
216
|
-
const installAgentSkills = async (
|
|
217
|
-
|
|
218
|
-
const result = await setupAgentSkills({ workingDir: command.netlify.repositoryRoot, reset });
|
|
262
|
+
const installAgentSkills = async (workingDir, reset) => {
|
|
263
|
+
const result = await setupAgentSkills({ workingDir, reset });
|
|
219
264
|
await track('sites_agentSkillsSetup', {
|
|
220
265
|
installed: result.installed,
|
|
221
266
|
directories: result.directories,
|
|
@@ -224,31 +269,76 @@ const installAgentSkills = async (command, reset) => {
|
|
|
224
269
|
...result.summary,
|
|
225
270
|
});
|
|
226
271
|
};
|
|
227
|
-
|
|
272
|
+
const addAgentMcp = async (command, reset, scope) => {
|
|
273
|
+
const { repositoryRoot, state } = command.netlify;
|
|
274
|
+
const { selection, agents, failed = false, } = await setupAgentMcp({
|
|
275
|
+
workingDir: repositoryRoot,
|
|
276
|
+
store: state,
|
|
277
|
+
scope,
|
|
278
|
+
reset,
|
|
279
|
+
});
|
|
280
|
+
await track('sites_agentMcpSetup', { selection, agents, failed, scope, resetMcpSetup: reset });
|
|
281
|
+
};
|
|
282
|
+
const setUpAgents = async (command, { skills, mcp, reset, resetMcp, mcpScope, }) => {
|
|
283
|
+
if (!skills && !mcp) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
const { repositoryRoot } = command.netlify;
|
|
287
|
+
log();
|
|
288
|
+
const blocker = await findProjectSetupBlocker(repositoryRoot);
|
|
289
|
+
const skillsBlocked = skills && blocker !== undefined;
|
|
290
|
+
const mcpBlocked = mcp && blocker !== undefined && (blocker.place === 'root' || mcpScope === 'project');
|
|
291
|
+
if (blocker && (skillsBlocked || mcpBlocked)) {
|
|
292
|
+
const skipped = [skillsBlocked && 'Netlify skills', mcpBlocked && 'the Netlify MCP server']
|
|
293
|
+
.filter(Boolean)
|
|
294
|
+
.join(' and ');
|
|
295
|
+
log(`Skipped setting up ${skipped}: ${blocker.reason}.`);
|
|
296
|
+
log(` Run ${chalk.cyanBright.bold(`${netlifyCommand()} init`)} from your project folder to set up ${skipped} there.`);
|
|
297
|
+
await track('sites_agentSetupSkipped', { skills: skillsBlocked, mcp: mcpBlocked });
|
|
298
|
+
}
|
|
299
|
+
if (skills && !skillsBlocked) {
|
|
300
|
+
await installAgentSkills(repositoryRoot, reset);
|
|
301
|
+
}
|
|
302
|
+
if (mcp && !mcpBlocked) {
|
|
303
|
+
await addAgentMcp(command, resetMcp, mcpScope);
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
export const init = async (options, command, { customizeExitMessage, exitAfterConfiguringRepo = false, skipPromptsWithoutTerminal = false, setupAgentSkills: shouldSetupAgentSkills = false, setupAgentMcp: shouldSetupAgentMcp = false, resetContext = false, resetMcpSetup = false, mcpScope = 'project', } = {}) => {
|
|
228
307
|
command.setAnalyticsPayload({
|
|
229
308
|
manual: options.manual,
|
|
230
309
|
force: options.force,
|
|
231
310
|
skipAgentSetup: options.skipAgentSetup,
|
|
311
|
+
skipMcpSetup: options.skipMcpSetup,
|
|
232
312
|
resetContext: options.resetContext,
|
|
313
|
+
resetMcpSetup: options.resetMcpSetup,
|
|
314
|
+
mcpScope,
|
|
233
315
|
});
|
|
234
316
|
const { repositoryRoot, state } = command.netlify;
|
|
235
317
|
// Check logged in status
|
|
236
318
|
await command.authenticate();
|
|
237
319
|
// Add .netlify to .gitignore file
|
|
238
320
|
await ensureNetlifyIgnore(repositoryRoot);
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
321
|
+
await setUpAgents(command, {
|
|
322
|
+
skills: shouldSetupAgentSkills,
|
|
323
|
+
mcp: shouldSetupAgentMcp,
|
|
324
|
+
reset: resetContext,
|
|
325
|
+
resetMcp: resetMcpSetup,
|
|
326
|
+
mcpScope,
|
|
327
|
+
});
|
|
328
|
+
const promptsSkipped = skipPromptsWithoutTerminal && !canAnswerPrompts();
|
|
242
329
|
const linkedSiteInfo = await getLinkedSiteInfo(command);
|
|
243
|
-
if (linkedSiteInfo && getRepoUrl(linkedSiteInfo) && !options.force) {
|
|
330
|
+
if (linkedSiteInfo && getRepoUrl(linkedSiteInfo) && (!options.force || promptsSkipped)) {
|
|
244
331
|
logExistingAndExit({ siteInfo: linkedSiteInfo });
|
|
245
332
|
}
|
|
246
333
|
// Look for local repo
|
|
247
334
|
const repoData = await getRepoData({ workingDir: command.workingDir, remoteName: options.gitRemoteName });
|
|
335
|
+
if ('error' in repoData && linkedSiteInfo) {
|
|
336
|
+
return logLinkedWithoutGitAndExit({ siteInfo: linkedSiteInfo, repoError: repoData });
|
|
337
|
+
}
|
|
338
|
+
if (promptsSkipped) {
|
|
339
|
+
return logNextStepsAndExit({ command, linkedSiteInfo, repoData });
|
|
340
|
+
}
|
|
248
341
|
if ('error' in repoData) {
|
|
249
|
-
if (linkedSiteInfo) {
|
|
250
|
-
return logLinkedWithoutGitAndExit({ siteInfo: linkedSiteInfo, repoError: repoData });
|
|
251
|
-
}
|
|
252
342
|
// TODO(ndhoule): Custom error messaage here
|
|
253
343
|
return await handleNoGitRemoteAndExit({
|
|
254
344
|
command,
|
|
@@ -43,6 +43,13 @@ To search for projects:
|
|
|
43
43
|
return firstSite;
|
|
44
44
|
}
|
|
45
45
|
spinner.warn({ text: `Found ${matchingSites.length} projects connected to ${repoUrl}` });
|
|
46
|
+
if (!isInteractive() && process.env.TESTING_PROMPTS !== 'true') {
|
|
47
|
+
return logAndThrowError(`More than one project is connected to ${repoUrl}. Link the one you want by ID:
|
|
48
|
+
|
|
49
|
+
${matchingSites
|
|
50
|
+
.map(({ id, name }) => ` ${chalk.cyanBright(`${netlifyCommand()} link --id ${id}`)} ${name}`)
|
|
51
|
+
.join('\n')}`);
|
|
52
|
+
}
|
|
46
53
|
const selectedSite = await select({
|
|
47
54
|
message: 'Which project do you want to link?',
|
|
48
55
|
choices: matchingSites.map((matchingSite) => ({
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Only markers an agent product sets on its own count as a signal; never infer from process names, terminals, or the process tree.
|
|
2
|
-
// The result is untrusted attribution from the environment
|
|
2
|
+
// The result is untrusted attribution from the environment. It may label telemetry or pick which agent's config to write, never authorize an action.
|
|
3
3
|
export const CANONICAL_AGENT_NAMES = [
|
|
4
4
|
'claude',
|
|
5
5
|
'codex',
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { promises as fs } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
const realpathOrSelf = async (target) => {
|
|
5
|
+
try {
|
|
6
|
+
return await fs.realpath(target);
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return path.resolve(target);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
export const findProjectSetupBlocker = async (root, home = homedir()) => {
|
|
13
|
+
const resolvedRoot = await realpathOrSelf(root);
|
|
14
|
+
if (resolvedRoot === (await realpathOrSelf(home))) {
|
|
15
|
+
return { place: 'home', reason: 'this is your home directory, where they would apply to every project' };
|
|
16
|
+
}
|
|
17
|
+
if (resolvedRoot === path.parse(resolvedRoot).root) {
|
|
18
|
+
return { place: 'root', reason: 'this is the root of the file system' };
|
|
19
|
+
}
|
|
20
|
+
return undefined;
|
|
21
|
+
};
|
|
22
|
+
//# sourceMappingURL=agent-location.js.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export type ConfigFormat = 'json' | 'toml';
|
|
2
|
+
export interface ProjectConfigFile {
|
|
3
|
+
path: string;
|
|
4
|
+
alternatives?: string[];
|
|
5
|
+
format: ConfigFormat;
|
|
6
|
+
key: string;
|
|
7
|
+
entry: Record<string, string | boolean>;
|
|
8
|
+
note?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface ConfigServer {
|
|
11
|
+
name: string;
|
|
12
|
+
target: string;
|
|
13
|
+
disabled: boolean;
|
|
14
|
+
}
|
|
15
|
+
export type ConfigRead = {
|
|
16
|
+
readable: true;
|
|
17
|
+
path: string;
|
|
18
|
+
content: string;
|
|
19
|
+
servers: ConfigServer[];
|
|
20
|
+
} | {
|
|
21
|
+
readable: false;
|
|
22
|
+
path: string;
|
|
23
|
+
reason: string;
|
|
24
|
+
};
|
|
25
|
+
export declare const readProjectConfig: (workingDir: string, file: ProjectConfigFile) => Promise<ConfigRead>;
|
|
26
|
+
export declare const addToProjectConfig: (workingDir: string, file: ProjectConfigFile, name: string, { path: resolved, content }: {
|
|
27
|
+
path: string;
|
|
28
|
+
content: string;
|
|
29
|
+
}) => Promise<boolean>;
|
|
30
|
+
//# sourceMappingURL=agent-mcp-config.d.ts.map
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { promises as fs } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { findNodeAtLocation, parse, parseTree } from 'jsonc-parser';
|
|
4
|
+
import toml from 'toml';
|
|
5
|
+
import writeFileAtomic from 'write-file-atomic';
|
|
6
|
+
import { fileExistsAsync } from '../../lib/fs.js';
|
|
7
|
+
const SERVER_FIELDS = new Set(['url', 'httpUrl', 'serverUrl', 'command']);
|
|
8
|
+
const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
9
|
+
const looksLikeServer = (value) => isObject(value) && Object.keys(value).some((field) => SERVER_FIELDS.has(field));
|
|
10
|
+
const stringLeaves = (value) => {
|
|
11
|
+
if (typeof value === 'string') {
|
|
12
|
+
return [value];
|
|
13
|
+
}
|
|
14
|
+
if (Array.isArray(value)) {
|
|
15
|
+
return value.flatMap(stringLeaves);
|
|
16
|
+
}
|
|
17
|
+
if (isObject(value)) {
|
|
18
|
+
return Object.values(value).flatMap(stringLeaves);
|
|
19
|
+
}
|
|
20
|
+
return [];
|
|
21
|
+
};
|
|
22
|
+
const serversIn = (map) => isObject(map)
|
|
23
|
+
? Object.entries(map).map(([name, value]) => ({
|
|
24
|
+
name,
|
|
25
|
+
target: stringLeaves(value).join(' '),
|
|
26
|
+
disabled: isObject(value) && (value.enabled === false || value.disabled === true),
|
|
27
|
+
}))
|
|
28
|
+
: [];
|
|
29
|
+
const readFileOrEmpty = async (file) => {
|
|
30
|
+
try {
|
|
31
|
+
return await fs.readFile(file, 'utf8');
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
if (error.code === 'ENOENT') {
|
|
35
|
+
return '';
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const detectEol = (content) => (content.includes('\r\n') ? '\r\n' : '\n');
|
|
41
|
+
const detectIndent = (content) => /^([ \t]+)"/m.exec(content)?.[1] ?? ' ';
|
|
42
|
+
const parseJson = (content) => {
|
|
43
|
+
const errors = [];
|
|
44
|
+
const parsed = parse(content, errors, { allowTrailingComma: true });
|
|
45
|
+
return errors.length === 0 && isObject(parsed) ? parsed : undefined;
|
|
46
|
+
};
|
|
47
|
+
const readJson = (file, content, key) => {
|
|
48
|
+
if (content.trim() === '') {
|
|
49
|
+
return { readable: true, path: file, content, servers: [] };
|
|
50
|
+
}
|
|
51
|
+
const parsed = parseJson(content);
|
|
52
|
+
if (parsed === undefined) {
|
|
53
|
+
return { readable: false, path: file, reason: 'it is not valid JSON' };
|
|
54
|
+
}
|
|
55
|
+
const bare = !(key in parsed) && Object.values(parsed).some(looksLikeServer);
|
|
56
|
+
if (bare) {
|
|
57
|
+
return { readable: false, path: file, reason: `its servers are not under ${JSON.stringify(key)}` };
|
|
58
|
+
}
|
|
59
|
+
if (key in parsed && !isObject(parsed[key])) {
|
|
60
|
+
return { readable: false, path: file, reason: `its ${JSON.stringify(key)} is not an object` };
|
|
61
|
+
}
|
|
62
|
+
return { readable: true, path: file, content, servers: serversIn(parsed[key]) };
|
|
63
|
+
};
|
|
64
|
+
const parseToml = (content) => {
|
|
65
|
+
try {
|
|
66
|
+
const parsed = toml.parse(content);
|
|
67
|
+
return isObject(parsed) ? parsed : undefined;
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
const readToml = (file, content, key) => {
|
|
74
|
+
const parsed = parseToml(content);
|
|
75
|
+
if (parsed === undefined) {
|
|
76
|
+
return { readable: false, path: file, reason: 'it is not valid TOML' };
|
|
77
|
+
}
|
|
78
|
+
return { readable: true, path: file, content, servers: serversIn(parsed[key]) };
|
|
79
|
+
};
|
|
80
|
+
const existingCandidates = async (workingDir, file) => {
|
|
81
|
+
const present = [];
|
|
82
|
+
for (const candidate of [file.path, ...(file.alternatives ?? [])]) {
|
|
83
|
+
if (await fileExistsAsync(path.join(workingDir, candidate))) {
|
|
84
|
+
present.push(candidate);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return present;
|
|
88
|
+
};
|
|
89
|
+
const realpathOrSelf = async (target) => {
|
|
90
|
+
try {
|
|
91
|
+
return await fs.realpath(target);
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
return target;
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
const realpathThroughMissing = async (target) => {
|
|
98
|
+
const missing = [];
|
|
99
|
+
let existing = target;
|
|
100
|
+
while (!(await fileExistsAsync(existing))) {
|
|
101
|
+
const parent = path.dirname(existing);
|
|
102
|
+
if (parent === existing) {
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
missing.unshift(path.basename(existing));
|
|
106
|
+
existing = parent;
|
|
107
|
+
}
|
|
108
|
+
return path.join(await realpathOrSelf(existing), ...missing);
|
|
109
|
+
};
|
|
110
|
+
const insideProject = async (workingDir, file) => {
|
|
111
|
+
const root = await realpathOrSelf(workingDir);
|
|
112
|
+
const resolved = await realpathThroughMissing(path.join(workingDir, file));
|
|
113
|
+
return resolved.startsWith(`${root}${path.sep}`);
|
|
114
|
+
};
|
|
115
|
+
const readOne = (file, resolved, content) => file.format === 'toml' ? readToml(resolved, content, file.key) : readJson(resolved, content, file.key);
|
|
116
|
+
export const readProjectConfig = async (workingDir, file) => {
|
|
117
|
+
const present = await existingCandidates(workingDir, file);
|
|
118
|
+
const target = present[0] ?? file.path;
|
|
119
|
+
if (!(await insideProject(workingDir, target))) {
|
|
120
|
+
return { readable: false, path: target, reason: 'it points outside this project' };
|
|
121
|
+
}
|
|
122
|
+
const servers = [];
|
|
123
|
+
let first;
|
|
124
|
+
for (const candidate of present.length > 0 ? present : [target]) {
|
|
125
|
+
const content = await readFileOrEmpty(path.join(workingDir, candidate));
|
|
126
|
+
if (content === undefined) {
|
|
127
|
+
return { readable: false, path: candidate, reason: 'it could not be read' };
|
|
128
|
+
}
|
|
129
|
+
const read = readOne(file, candidate, content);
|
|
130
|
+
if (!read.readable) {
|
|
131
|
+
return read;
|
|
132
|
+
}
|
|
133
|
+
servers.push(...read.servers);
|
|
134
|
+
first ??= read.content;
|
|
135
|
+
}
|
|
136
|
+
return { readable: true, path: target, content: first ?? '', servers };
|
|
137
|
+
};
|
|
138
|
+
const tomlValue = (value) => typeof value === 'string' ? JSON.stringify(value) : String(value);
|
|
139
|
+
const objectDepth = (node) => {
|
|
140
|
+
let depth = 0;
|
|
141
|
+
for (let ancestor = node.parent; ancestor; ancestor = ancestor.parent) {
|
|
142
|
+
if (ancestor.type === 'object') {
|
|
143
|
+
depth += 1;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return depth;
|
|
147
|
+
};
|
|
148
|
+
const insertProperty = (content, object, name, value, eol, indent) => {
|
|
149
|
+
const depth = objectDepth(object) + 1;
|
|
150
|
+
const last = object.children?.at(-1);
|
|
151
|
+
const siblingPad = last === undefined ? undefined : /(?:^|\n|\r)([ \t]*)$/.exec(content.slice(0, last.offset))?.[1];
|
|
152
|
+
const pad = siblingPad ?? indent.repeat(depth);
|
|
153
|
+
const closing = object.offset + object.length - 1;
|
|
154
|
+
const property = `${JSON.stringify(name)}: ${JSON.stringify(value, null, indent).split('\n').join(`${eol}${pad}`)}`;
|
|
155
|
+
if (last === undefined) {
|
|
156
|
+
if (content.slice(object.offset + 1, closing).trim() !== '') {
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
159
|
+
return `${content.slice(0, object.offset + 1)}${eol}${pad}${property}${eol}${indent.repeat(depth - 1)}${content.slice(closing)}`;
|
|
160
|
+
}
|
|
161
|
+
const lastEnd = last.offset + last.length;
|
|
162
|
+
const lineEnd = content.indexOf(eol, lastEnd);
|
|
163
|
+
if (lineEnd === -1 || lineEnd > closing) {
|
|
164
|
+
return `${content.slice(0, lastEnd)}, ${JSON.stringify(name)}: ${JSON.stringify(value)}${content.slice(lastEnd)}`;
|
|
165
|
+
}
|
|
166
|
+
const trailingComma = /^\s*,/.test(content.slice(lastEnd, lineEnd));
|
|
167
|
+
const comma = trailingComma ? '' : ',';
|
|
168
|
+
return `${content.slice(0, lastEnd)}${comma}${content.slice(lastEnd, lineEnd)}${eol}${pad}${property}${trailingComma ? ',' : ''}${content.slice(lineEnd)}`;
|
|
169
|
+
};
|
|
170
|
+
const withJsonEntry = (content, { key, entry }, name) => {
|
|
171
|
+
if (content.trim() === '') {
|
|
172
|
+
return `${JSON.stringify({ [key]: { [name]: entry } }, null, 2)}\n`;
|
|
173
|
+
}
|
|
174
|
+
const root = parseTree(content, [], { allowTrailingComma: true });
|
|
175
|
+
if (root?.type !== 'object') {
|
|
176
|
+
return undefined;
|
|
177
|
+
}
|
|
178
|
+
const eol = detectEol(content);
|
|
179
|
+
const indent = detectIndent(content);
|
|
180
|
+
const servers = findNodeAtLocation(root, [key]);
|
|
181
|
+
return servers === undefined
|
|
182
|
+
? insertProperty(content, root, key, { [name]: entry }, eol, indent)
|
|
183
|
+
: insertProperty(content, servers, name, entry, eol, indent);
|
|
184
|
+
};
|
|
185
|
+
const withTomlEntry = (content, { key, entry }, name) => {
|
|
186
|
+
const eol = detectEol(content);
|
|
187
|
+
const lines = Object.entries(entry).map(([field, value]) => `${field} = ${tomlValue(value)}`);
|
|
188
|
+
const table = [`[${key}.${name}]`, ...lines].join(eol);
|
|
189
|
+
const separator = content === '' ? '' : content.endsWith(eol) ? eol : `${eol}${eol}`;
|
|
190
|
+
return `${content}${separator}${table}${eol}`;
|
|
191
|
+
};
|
|
192
|
+
const holdsEntry = (content, { format, key }, name) => {
|
|
193
|
+
const parsed = format === 'toml' ? parseToml(content) : parseJson(content);
|
|
194
|
+
return isObject(parsed?.[key]) && name in parsed[key];
|
|
195
|
+
};
|
|
196
|
+
export const addToProjectConfig = async (workingDir, file, name, { path: resolved, content }) => {
|
|
197
|
+
const updated = file.format === 'toml' ? withTomlEntry(content, file, name) : withJsonEntry(content, file, name);
|
|
198
|
+
if (updated === undefined || !holdsEntry(updated, file, name)) {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
const target = path.join(workingDir, resolved);
|
|
202
|
+
await fs.mkdir(path.dirname(target), { recursive: true });
|
|
203
|
+
await writeFileAtomic(target, updated, 'utf8');
|
|
204
|
+
return true;
|
|
205
|
+
};
|
|
206
|
+
//# sourceMappingURL=agent-mcp-config.js.map
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { type CanonicalAgentName } from '../agent-detection.js';
|
|
2
|
+
import { type ProjectConfigFile } from './agent-mcp-config.js';
|
|
3
|
+
export declare const NETLIFY_MCP_URL = "https://mcp.netlify.com/mcp";
|
|
4
|
+
export declare const NETLIFY_MCP_SERVER_NAME = "netlify";
|
|
5
|
+
export declare const MCP_SCOPES: readonly ['project', 'user'];
|
|
6
|
+
export type McpScope = (typeof MCP_SCOPES)[number];
|
|
7
|
+
type LookupKind = 'list' | 'list-json';
|
|
8
|
+
interface NativeCommands {
|
|
9
|
+
add: string[];
|
|
10
|
+
remove?: string[];
|
|
11
|
+
savedIn?: string;
|
|
12
|
+
}
|
|
13
|
+
interface NativeSetup {
|
|
14
|
+
lookup?: LookupKind;
|
|
15
|
+
scopes: Partial<Record<McpScope, NativeCommands>>;
|
|
16
|
+
}
|
|
17
|
+
export interface McpAgent {
|
|
18
|
+
id: string;
|
|
19
|
+
label: string;
|
|
20
|
+
binary?: string;
|
|
21
|
+
drivenBy?: CanonicalAgentName[];
|
|
22
|
+
projectMarkers?: string[];
|
|
23
|
+
projectFile?: ProjectConfigFile;
|
|
24
|
+
inTerminal?: (env: NodeJS.ProcessEnv) => boolean;
|
|
25
|
+
native?: NativeSetup;
|
|
26
|
+
}
|
|
27
|
+
export declare const MCP_AGENTS: McpAgent[];
|
|
28
|
+
export type AgentMcpStatus = 'added' | 'present' | 'kept' | 'printed' | 'unsupported' | 'declined' | 'failed';
|
|
29
|
+
export type AgentSelection = 'driving' | 'prompt' | 'saved' | 'none';
|
|
30
|
+
export interface AgentMcpSetupSummary {
|
|
31
|
+
scope: McpScope;
|
|
32
|
+
selection: AgentSelection;
|
|
33
|
+
agents: Record<string, AgentMcpStatus>;
|
|
34
|
+
failed?: boolean;
|
|
35
|
+
}
|
|
36
|
+
export interface AgentCandidate {
|
|
37
|
+
agent: McpAgent;
|
|
38
|
+
detected: boolean;
|
|
39
|
+
}
|
|
40
|
+
export type ChooseAgents = (candidates: AgentCandidate[]) => Promise<McpAgent[]>;
|
|
41
|
+
export declare const findCandidates: (workingDir: string, env: NodeJS.ProcessEnv, scope?: McpScope) => Promise<AgentCandidate[]>;
|
|
42
|
+
export interface McpChoiceStore {
|
|
43
|
+
get(key: string): unknown;
|
|
44
|
+
set(key: string, value: unknown): void;
|
|
45
|
+
delete(key: string): void;
|
|
46
|
+
}
|
|
47
|
+
export declare const setupAgentMcp: ({ workingDir, store, scope, env, interactive, choose, reset, }: {
|
|
48
|
+
workingDir: string;
|
|
49
|
+
store: McpChoiceStore;
|
|
50
|
+
scope?: McpScope;
|
|
51
|
+
env?: NodeJS.ProcessEnv;
|
|
52
|
+
interactive?: boolean;
|
|
53
|
+
choose?: ChooseAgents;
|
|
54
|
+
reset?: boolean;
|
|
55
|
+
}) => Promise<AgentMcpSetupSummary>;
|
|
56
|
+
export {};
|
|
57
|
+
//# sourceMappingURL=agent-mcp.d.ts.map
|