neon 2.45.0 → 2.46.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.
Files changed (52) hide show
  1. package/README.md +62 -1
  2. package/dist/_shared/paths.js +3 -4
  3. package/dist/commands/bootstrap.js +12 -11
  4. package/dist/commands/checkout.js +7 -6
  5. package/dist/commands/config.js +2 -1
  6. package/dist/commands/data_api.js +4 -3
  7. package/dist/commands/env.js +5 -4
  8. package/dist/commands/functions.js +3 -2
  9. package/dist/commands/init.js +82 -37
  10. package/dist/commands/ip_allow.js +3 -2
  11. package/dist/commands/link.js +17 -16
  12. package/dist/commands/projects.js +3 -2
  13. package/dist/commands/set_context.js +5 -4
  14. package/dist/current_branch_fast_path.js +2 -1
  15. package/dist/dev/env.js +8 -7
  16. package/dist/index.js +2 -2
  17. package/dist/init/agents.js +127 -0
  18. package/dist/init/auth.js +77 -0
  19. package/dist/init/bootstrap.js +448 -0
  20. package/dist/init/build_config.js +2 -0
  21. package/dist/init/detect_agent.js +108 -0
  22. package/dist/init/editors.js +62 -0
  23. package/dist/init/enrich_output.js +71 -0
  24. package/dist/init/extension.js +191 -0
  25. package/dist/init/inspect.js +287 -0
  26. package/dist/init/interactive.js +651 -0
  27. package/dist/init/neonctl.js +184 -0
  28. package/dist/init/orchestrate.js +190 -0
  29. package/dist/init/phases/auth.js +209 -0
  30. package/dist/init/phases/cleanup.js +27 -0
  31. package/dist/init/phases/db.js +283 -0
  32. package/dist/init/phases/getting_started.js +228 -0
  33. package/dist/init/phases/mcp.js +227 -0
  34. package/dist/init/phases/migrations.js +251 -0
  35. package/dist/init/phases/neon_auth.js +135 -0
  36. package/dist/init/phases/setup.js +729 -0
  37. package/dist/init/phases/skills.js +89 -0
  38. package/dist/init/phases/status.js +70 -0
  39. package/dist/init/resolve_context.js +107 -0
  40. package/dist/init/route_command.js +100 -0
  41. package/dist/init/skills.js +248 -0
  42. package/dist/init/types.js +1 -0
  43. package/dist/init/vsix.js +111 -0
  44. package/dist/psql/command/cmd_meta.js +2 -2
  45. package/dist/psql/core/mainloop.js +1 -1
  46. package/dist/psql/core/startup.js +1 -1
  47. package/dist/psql/core/syncVars.js +3 -3
  48. package/dist/psql/index.js +1 -1
  49. package/dist/utils/cli_name.js +14 -0
  50. package/dist/utils/esbuild.js +1 -1
  51. package/dist/utils/write_sync.js +39 -0
  52. package/package.json +18 -12
@@ -0,0 +1,448 @@
1
+ import { chmodSync, existsSync, lstatSync, mkdirSync, readdirSync, rmSync, statSync, symlinkSync, writeFileSync, } from "node:fs";
2
+ import { dirname, join } from "node:path";
3
+ import { gunzipSync } from "fflate";
4
+ import YAML from "yaml";
5
+ /** Default features when a template doesn't specify `requires`. */
6
+ const DEFAULT_REQUIRES = ["database"];
7
+ /**
8
+ * Hardcoded fallback used when every remote manifest source is unreachable.
9
+ * Kept in sync with `neondatabase/examples/bootstrap.yaml` (the source of
10
+ * truth) so that, even fully offline from the manifest, the picker still offers
11
+ * the full set of starters rather than a single template.
12
+ */
13
+ export const FALLBACK_TEMPLATES = [
14
+ {
15
+ id: "hono",
16
+ title: "REST API",
17
+ description: "A Hono REST API on Neon Functions, backed by Lakebase Postgres via Drizzle.",
18
+ tools: ["Hono", "Drizzle"],
19
+ services: ["Postgres", "Functions"],
20
+ requires: ["database", "functions"],
21
+ source: {
22
+ owner: "neondatabase",
23
+ repo: "examples",
24
+ ref: "main",
25
+ subdir: "with-hono",
26
+ },
27
+ },
28
+ {
29
+ id: "ai-sdk",
30
+ title: "Image-generation agent",
31
+ description: "A Vercel AI SDK agent that streams chat through the Neon AI Gateway and stores generated images in Neon object storage, indexed in Postgres via Drizzle.",
32
+ tools: ["AI SDK", "Drizzle"],
33
+ services: ["Postgres", "Functions", "Object Storage", "AI Gateway"],
34
+ requires: ["database", "functions", "object-storage", "ai-gateway"],
35
+ source: {
36
+ owner: "neondatabase",
37
+ repo: "examples",
38
+ ref: "main",
39
+ subdir: "with-ai-sdk",
40
+ },
41
+ },
42
+ {
43
+ id: "mastra",
44
+ title: "Personal-assistant agent",
45
+ description: "A Mastra agent that streams chat through the Neon AI Gateway and uses Mastra Memory on Lakebase Postgres to remember you across threads.",
46
+ tools: ["Mastra", "Mastra Memory"],
47
+ services: ["Postgres", "Functions", "AI Gateway"],
48
+ requires: ["database", "functions", "ai-gateway"],
49
+ source: {
50
+ owner: "neondatabase",
51
+ repo: "examples",
52
+ ref: "main",
53
+ subdir: "with-mastra",
54
+ },
55
+ },
56
+ ];
57
+ export const templateIds = (templates) => templates.map((t) => t.id).join(", ");
58
+ export const findTemplate = (templates, id) => templates.find((t) => t.id === id);
59
+ const githubToken = () => process.env.GITHUB_TOKEN ?? process.env.GH_TOKEN ?? "";
60
+ // A token is never required for public templates, but we forward it when
61
+ // present so the same code path works behind proxies that authenticate, and
62
+ // (in future) for private template repos.
63
+ const downloadHeaders = () => ({
64
+ // GitHub rejects a request with no User-Agent; the value is free-form and this
65
+ // one only has to name the client honestly.
66
+ "User-Agent": "neon",
67
+ ...(githubToken() ? { Authorization: `Bearer ${githubToken()}` } : {}),
68
+ });
69
+ // The codeload host is overridable so the e2e tests can point the downloader at
70
+ // a local server (the same trick `--api-host` uses to redirect the Neon API).
71
+ const codeloadBase = () => process.env.NEON_BOOTSTRAP_GITHUB_CODELOAD ?? "https://codeload.github.com";
72
+ const isRecord = (value) => typeof value === "object" && value !== null;
73
+ /**
74
+ * Normalize a manifest entry's string list (`tools` or `services`) into a clean
75
+ * array. Tolerant by design: a missing or non-array value yields `undefined`,
76
+ * and non-string/blank items are dropped, so a malformed list never sinks an
77
+ * otherwise-valid template (it just renders without that detail).
78
+ */
79
+ const parseStringList = (value) => {
80
+ if (!Array.isArray(value))
81
+ return undefined;
82
+ const items = value.filter((item) => typeof item === "string" && item.trim() !== "");
83
+ return items.length > 0 ? items : undefined;
84
+ };
85
+ // ---------------------------------------------------------------------------
86
+ // Remote template manifest
87
+ // ---------------------------------------------------------------------------
88
+ // Primary manifest host is neon.com (CDN-backed, no GitHub rate limiting), with
89
+ // the raw GitHub copy as a fallback and the hardcoded list as the last resort.
90
+ // A single env override (used by tests) short-circuits the chain.
91
+ const NEON_MANIFEST_URL = "https://neon.com/bootstrap/templates.yaml";
92
+ const GITHUB_RAW_MANIFEST_URL = "https://raw.githubusercontent.com/neondatabase/examples/main/bootstrap.yaml";
93
+ function manifestUrls() {
94
+ const override = process.env.NEON_BOOTSTRAP_MANIFEST_URL;
95
+ if (override)
96
+ return [override];
97
+ return [NEON_MANIFEST_URL, GITHUB_RAW_MANIFEST_URL];
98
+ }
99
+ export function parseManifest(text) {
100
+ const data = YAML.parse(text);
101
+ if (!isRecord(data) || !Array.isArray(data.templates)) {
102
+ throw new Error('Invalid bootstrap manifest: missing "templates" array.');
103
+ }
104
+ const templates = [];
105
+ for (const item of data.templates) {
106
+ if (!isRecord(item) ||
107
+ typeof item.id !== "string" ||
108
+ typeof item.title !== "string" ||
109
+ typeof item.description !== "string" ||
110
+ !isRecord(item.source) ||
111
+ typeof item.source.owner !== "string" ||
112
+ typeof item.source.repo !== "string" ||
113
+ typeof item.source.ref !== "string" ||
114
+ typeof item.source.subdir !== "string") {
115
+ continue;
116
+ }
117
+ // Parse requires — accept a string array, default to ["database"].
118
+ const requires = Array.isArray(item.requires) &&
119
+ item.requires.every((r) => typeof r === "string")
120
+ ? item.requires
121
+ : DEFAULT_REQUIRES;
122
+ const tools = parseStringList(item.tools);
123
+ const services = parseStringList(item.services);
124
+ templates.push({
125
+ id: item.id,
126
+ title: item.title,
127
+ description: item.description,
128
+ ...(tools ? { tools } : {}),
129
+ ...(services ? { services } : {}),
130
+ requires,
131
+ source: {
132
+ owner: item.source.owner,
133
+ repo: item.source.repo,
134
+ ref: item.source.ref,
135
+ subdir: item.source.subdir,
136
+ },
137
+ });
138
+ }
139
+ return templates;
140
+ }
141
+ /**
142
+ * Fetch the template manifest, trying each source in {@link manifestUrls} in
143
+ * order and returning the first that yields a non-empty template list. Falls
144
+ * back to the hardcoded list when every source is unreachable or empty, so the
145
+ * picker never fails just because a host is down.
146
+ */
147
+ export async function fetchTemplates() {
148
+ for (const url of manifestUrls()) {
149
+ try {
150
+ const res = await fetch(url, {
151
+ headers: downloadHeaders(),
152
+ signal: AbortSignal.timeout(10000),
153
+ });
154
+ if (!res.ok)
155
+ throw new Error(`HTTP ${res.status}`);
156
+ const templates = parseManifest(await res.text());
157
+ if (templates.length > 0)
158
+ return templates;
159
+ }
160
+ catch {
161
+ // Try the next source.
162
+ }
163
+ }
164
+ return FALLBACK_TEMPLATES;
165
+ }
166
+ const TAR_BLOCK = 512;
167
+ const readTarString = (buf, offset, length) => {
168
+ let end = offset;
169
+ const max = offset + length;
170
+ while (end < max && buf[end] !== 0)
171
+ end++;
172
+ return buf.toString("utf8", offset, end);
173
+ };
174
+ const readTarOctal = (buf, offset, length) => {
175
+ const text = readTarString(buf, offset, length).trim();
176
+ if (text === "")
177
+ return 0;
178
+ const value = parseInt(text, 8);
179
+ return Number.isNaN(value) ? 0 : value;
180
+ };
181
+ const isZeroBlock = (buf, offset) => {
182
+ for (let i = offset; i < offset + TAR_BLOCK; i++) {
183
+ if (buf[i] !== 0)
184
+ return false;
185
+ }
186
+ return true;
187
+ };
188
+ /**
189
+ * Parse pax extended-header records ("<len> <key>=<value>\n"). GitHub uses
190
+ * these for the global header and for any path that doesn't fit the legacy
191
+ * 100-byte name field, so we must honor at least `path` and `linkpath`.
192
+ */
193
+ const parsePaxRecords = (data) => {
194
+ const records = {};
195
+ let pos = 0;
196
+ const text = data.toString("utf8");
197
+ while (pos < text.length) {
198
+ const space = text.indexOf(" ", pos);
199
+ if (space === -1)
200
+ break;
201
+ const len = parseInt(text.slice(pos, space), 10);
202
+ if (Number.isNaN(len) || len <= 0)
203
+ break;
204
+ const record = text.slice(space + 1, pos + len - 1); // drop trailing "\n"
205
+ const eq = record.indexOf("=");
206
+ if (eq !== -1)
207
+ records[record.slice(0, eq)] = record.slice(eq + 1);
208
+ pos += len;
209
+ }
210
+ return records;
211
+ };
212
+ /**
213
+ * Decode a (decompressed) tar archive into its file/symlink entries. Pure and
214
+ * dependency-free so it can be unit tested without touching the network.
215
+ * Handles the ustar `prefix` field, pax extended headers (type 'x'/'g'), and
216
+ * GNU long-name/long-link headers (type 'L'/'K') so deep template paths and
217
+ * long symlink targets round-trip correctly.
218
+ */
219
+ export const parseTar = (buf) => {
220
+ const entries = [];
221
+ // Overrides carried from a preceding pax/GNU header to the next real entry.
222
+ let overridePath;
223
+ let overrideLink;
224
+ let offset = 0;
225
+ while (offset + TAR_BLOCK <= buf.length) {
226
+ if (isZeroBlock(buf, offset))
227
+ break;
228
+ let name = readTarString(buf, offset, 100);
229
+ const mode = readTarOctal(buf, offset + 100, 8);
230
+ const size = readTarOctal(buf, offset + 124, 12);
231
+ const typeByte = buf[offset + 156];
232
+ const type = typeByte === 0 ? "0" : String.fromCharCode(typeByte);
233
+ let linkname = readTarString(buf, offset + 157, 100);
234
+ const magic = readTarString(buf, offset + 257, 6);
235
+ if (magic.startsWith("ustar")) {
236
+ const prefix = readTarString(buf, offset + 345, 155);
237
+ if (prefix !== "")
238
+ name = `${prefix}/${name}`;
239
+ }
240
+ offset += TAR_BLOCK;
241
+ const data = buf.subarray(offset, offset + size);
242
+ offset += Math.ceil(size / TAR_BLOCK) * TAR_BLOCK;
243
+ if (type === "x") {
244
+ const records = parsePaxRecords(data);
245
+ if (records.path !== undefined)
246
+ overridePath = records.path;
247
+ if (records.linkpath !== undefined)
248
+ overrideLink = records.linkpath;
249
+ continue;
250
+ }
251
+ if (type === "g") {
252
+ // Global pax header (e.g. GitHub's comment block): not per-entry state.
253
+ continue;
254
+ }
255
+ if (type === "L" || type === "K") {
256
+ const longValue = data.toString("utf8").replace(/\0+$/, "");
257
+ if (type === "L")
258
+ overridePath = longValue;
259
+ else
260
+ overrideLink = longValue;
261
+ continue;
262
+ }
263
+ if (overridePath !== undefined)
264
+ name = overridePath;
265
+ if (overrideLink !== undefined)
266
+ linkname = overrideLink;
267
+ overridePath = undefined;
268
+ overrideLink = undefined;
269
+ entries.push({ name, type, mode, linkname, data: Buffer.from(data) });
270
+ }
271
+ return entries;
272
+ };
273
+ /**
274
+ * Map decoded tar entries to the files under `subdir`, with the top-level
275
+ * archive directory and the `subdir/` prefix stripped from each path. Pure so
276
+ * it can be unit tested. Directory and other non-regular entries are dropped —
277
+ * writing files re-creates their parent directories.
278
+ */
279
+ export const selectTemplateFiles = (entries, subdir) => {
280
+ const prefix = `${subdir.replace(/^\/+|\/+$/g, "")}/`;
281
+ const files = [];
282
+ for (const entry of entries) {
283
+ // codeload wraps everything in a single top-level dir ("<repo>-<ref>/");
284
+ // strip that first segment to get the repo-relative path.
285
+ const slash = entry.name.indexOf("/");
286
+ if (slash === -1)
287
+ continue;
288
+ const repoPath = entry.name.slice(slash + 1);
289
+ if (!repoPath.startsWith(prefix))
290
+ continue;
291
+ const path = repoPath.slice(prefix.length);
292
+ if (path === "")
293
+ continue;
294
+ if (entry.type === "2") {
295
+ files.push({ kind: "symlink", path, target: entry.linkname });
296
+ }
297
+ else if (entry.type === "0" || entry.type === "7") {
298
+ files.push({
299
+ kind: "file",
300
+ path,
301
+ bytes: entry.data,
302
+ executable: (entry.mode & 0o111) !== 0,
303
+ });
304
+ }
305
+ // Directories ('5') and any other node types are intentionally skipped.
306
+ }
307
+ return files;
308
+ };
309
+ const tarballUrl = (template) => {
310
+ const { owner, repo, ref } = template.source;
311
+ return `${codeloadBase()}/${owner}/${repo}/tar.gz/${ref}`;
312
+ };
313
+ const friendlyGithubError = (status, url) => {
314
+ if (status === 404) {
315
+ return new Error(`GitHub returned 404 for ${url}. The template repo or ref may have moved.`);
316
+ }
317
+ if (status === 403 || status === 429) {
318
+ return new Error(`GitHub rate limited the template download (${url}). Set a GITHUB_TOKEN environment variable to raise the limit, then retry.`);
319
+ }
320
+ return new Error(`GitHub returned HTTP ${status} for ${url}.`);
321
+ };
322
+ /**
323
+ * Download a template and resolve it to the exact set of files to write. The
324
+ * entire subtree is captured in one tarball request, so the copy is atomically
325
+ * consistent: a push to the template repo mid-download cannot produce a
326
+ * mismatched checkout (unlike fetching a file list and then each blob).
327
+ */
328
+ export const downloadTemplate = async (template) => {
329
+ const url = tarballUrl(template);
330
+ let gzipped;
331
+ try {
332
+ const res = await fetch(url, {
333
+ headers: downloadHeaders(),
334
+ signal: AbortSignal.timeout(30000),
335
+ });
336
+ if (!res.ok)
337
+ throw friendlyGithubError(res.status, url);
338
+ gzipped = Buffer.from(await res.arrayBuffer());
339
+ }
340
+ catch (err) {
341
+ throw err instanceof Error ? err : new Error(String(err));
342
+ }
343
+ let tar;
344
+ try {
345
+ tar = Buffer.from(gunzipSync(new Uint8Array(gzipped)));
346
+ }
347
+ catch (err) {
348
+ throw new Error(`Failed to decompress the template archive from ${url}: ${err instanceof Error ? err.message : String(err)}`);
349
+ }
350
+ const { owner, repo, ref, subdir } = template.source;
351
+ const files = selectTemplateFiles(parseTar(tar), subdir);
352
+ if (files.length === 0) {
353
+ throw new Error(`Template subdirectory "${subdir}" was not found in ${owner}/${repo}@${ref}.`);
354
+ }
355
+ return files;
356
+ };
357
+ // ---------------------------------------------------------------------------
358
+ // Target validation + scaffolding to disk
359
+ // ---------------------------------------------------------------------------
360
+ /**
361
+ * A bad caller-supplied input that an agent (or human) can correct: an unknown
362
+ * template id or a non-empty target directory. Carries an `agentCode` so an
363
+ * agent surface can report a precise error code instead of a generic
364
+ * INTERNAL_ERROR, while a human path just surfaces the clear `message`.
365
+ */
366
+ export class BootstrapInputError extends Error {
367
+ constructor(message, agentCode) {
368
+ super(message);
369
+ this.name = "BootstrapInputError";
370
+ this.agentCode = agentCode;
371
+ }
372
+ }
373
+ /**
374
+ * Ensure `dir` is safe to scaffold into: it must be missing, or an empty
375
+ * directory (a lone `.git` is ignored so you can scaffold into a freshly
376
+ * `git init`ed folder). `force` allows scaffolding into a non-empty directory,
377
+ * overwriting colliding files. Throws a {@link BootstrapInputError} otherwise.
378
+ */
379
+ export const ensureTargetUsable = (dir, force) => {
380
+ if (!existsSync(dir))
381
+ return;
382
+ if (!statSync(dir).isDirectory()) {
383
+ throw new BootstrapInputError(`Target ${dir} already exists and is not a directory.`, "TARGET_NOT_DIRECTORY");
384
+ }
385
+ const contents = readdirSync(dir).filter((name) => name !== ".git");
386
+ if (contents.length > 0 && !force) {
387
+ throw new BootstrapInputError(`Target directory ${dir} is not empty. Use --force to scaffold into it anyway (colliding files will be overwritten), or choose an empty directory.`, "TARGET_NOT_EMPTY");
388
+ }
389
+ };
390
+ const isSymlink = (path) => {
391
+ try {
392
+ return lstatSync(path).isSymbolicLink();
393
+ }
394
+ catch {
395
+ return false;
396
+ }
397
+ };
398
+ const errnoCode = (err) => {
399
+ if (typeof err === "object" &&
400
+ err !== null &&
401
+ "code" in err &&
402
+ typeof err.code === "string") {
403
+ return err.code;
404
+ }
405
+ return undefined;
406
+ };
407
+ const writeSymlink = (dest, target, onWarn) => {
408
+ if (isSymlink(dest))
409
+ rmSync(dest, { force: true });
410
+ try {
411
+ symlinkSync(target, dest);
412
+ }
413
+ catch (err) {
414
+ // Windows refuses symlinks without elevated rights / developer mode. The
415
+ // template still works for most tooling if we drop a regular file holding
416
+ // the link target, so we degrade gracefully instead of failing the copy.
417
+ if (errnoCode(err) === "EPERM" || process.platform === "win32") {
418
+ onWarn?.(`Could not create symlink ${dest} -> ${target}; wrote it as a regular file instead.`);
419
+ writeFileSync(dest, target);
420
+ return;
421
+ }
422
+ throw err;
423
+ }
424
+ };
425
+ /**
426
+ * Download `template` and materialize its files into `targetDir`, creating
427
+ * parent directories, preserving executable bits, and recreating symlinks
428
+ * (with a graceful regular-file fallback on platforms that disallow them).
429
+ * Returns the number of files written. The caller is responsible for any
430
+ * target validation ({@link ensureTargetUsable}) and user-facing progress.
431
+ */
432
+ export const scaffoldTemplate = async (template, targetDir, options = {}) => {
433
+ const files = await downloadTemplate(template);
434
+ mkdirSync(targetDir, { recursive: true });
435
+ for (const file of files) {
436
+ const dest = join(targetDir, file.path);
437
+ mkdirSync(dirname(dest), { recursive: true });
438
+ if (file.kind === "symlink") {
439
+ writeSymlink(dest, file.target, options.onWarn);
440
+ }
441
+ else {
442
+ writeFileSync(dest, file.bytes);
443
+ if (file.executable)
444
+ chmodSync(dest, 0o755);
445
+ }
446
+ }
447
+ return files.length;
448
+ };
@@ -0,0 +1,2 @@
1
+ // Auto-generated by scripts/set-vsx-gallery.mjs — do not edit
2
+ export const INTERNAL_VSX_GALLERY = "";
@@ -0,0 +1,108 @@
1
+ import { existsSync } from "node:fs";
2
+ /**
3
+ * Detects the IDE/editor the terminal is running in, regardless of which
4
+ * agent is active. Used for extension installation — the extension goes
5
+ * into the IDE, not the agent.
6
+ *
7
+ * Returns an Editor-compatible string: "Cursor", "VS Code", "Windsurf", or null.
8
+ */
9
+ export function detectIde() {
10
+ const env = process.env;
11
+ // Cursor: check explicit Cursor env vars (set in extension host context)
12
+ if (env.TERM_PROGRAM === "cursor" ||
13
+ env.CURSOR_TRACE_ID !== undefined ||
14
+ env.CURSOR_EXTENSION_HOST_ROLE !== undefined ||
15
+ env.CURSOR_LAYOUT !== undefined ||
16
+ env.CURSOR_SPAWNED_BY_EXTENSION_ID !== undefined) {
17
+ return "Cursor";
18
+ }
19
+ // Cursor: check app paths in env vars (reliable from terminal context)
20
+ if (env.GIT_ASKPASS?.includes("Cursor.app") ||
21
+ env.VSCODE_GIT_ASKPASS_NODE?.includes("Cursor.app") ||
22
+ env.GIT_ASKPASS?.includes("cursor") ||
23
+ env.VSCODE_GIT_ASKPASS_NODE?.includes("cursor")) {
24
+ return "Cursor";
25
+ }
26
+ // Windsurf
27
+ if (env.TERM_PROGRAM === "windsurf") {
28
+ return "Windsurf";
29
+ }
30
+ // At this point we know we're in a VS Code-like environment if any
31
+ // VSCODE_ env vars are set. But is it actually Cursor with generic env vars?
32
+ if (env.TERM_PROGRAM === "vscode" ||
33
+ env.VSCODE_PID !== undefined ||
34
+ env.VSCODE_CWD !== undefined) {
35
+ // Check any remaining env vars that might contain "cursor" (case-insensitive)
36
+ // This catches VSCODE_IPC_HOOK_CLI, PATH additions, etc.
37
+ const envValues = Object.values(env).join("\n").toLowerCase();
38
+ if (envValues.includes("cursor.app") ||
39
+ envValues.includes("/cursor/")) {
40
+ return "Cursor";
41
+ }
42
+ // If only Cursor is installed (not VS Code), it must be Cursor
43
+ if (isCursorInstalled() && !isVSCodeInstalled()) {
44
+ return "Cursor";
45
+ }
46
+ return "VS Code";
47
+ }
48
+ return null;
49
+ }
50
+ export function isCursorInstalled() {
51
+ if (process.platform === "darwin") {
52
+ return existsSync("/Applications/Cursor.app");
53
+ }
54
+ if (process.platform === "linux") {
55
+ return existsSync("/usr/share/cursor") || existsSync("/usr/bin/cursor");
56
+ }
57
+ if (process.platform === "win32") {
58
+ const localAppData = process.env.LOCALAPPDATA || "";
59
+ return (existsSync(`${localAppData}\\Programs\\Cursor\\Cursor.exe`) ||
60
+ existsSync(`${localAppData}\\cursor\\Cursor.exe`));
61
+ }
62
+ return false;
63
+ }
64
+ export function isVSCodeInstalled() {
65
+ if (process.platform === "darwin") {
66
+ return existsSync("/Applications/Visual Studio Code.app");
67
+ }
68
+ if (process.platform === "linux") {
69
+ return existsSync("/usr/share/code") || existsSync("/usr/bin/code");
70
+ }
71
+ if (process.platform === "win32") {
72
+ const localAppData = process.env.LOCALAPPDATA || "";
73
+ const programFiles = process.env.PROGRAMFILES || "C:\\Program Files";
74
+ return (existsSync(`${localAppData}\\Programs\\Microsoft VS Code\\Code.exe`) || existsSync(`${programFiles}\\Microsoft VS Code\\Code.exe`));
75
+ }
76
+ return false;
77
+ }
78
+ /**
79
+ * Detects which agent/IDE is running the CLI from environment variables.
80
+ * Returns the add-mcp compatible agent ID (e.g. "cursor", "claude-code").
81
+ *
82
+ * Delegates IDE detection to detectIde() to avoid duplicating Cursor/VS Code
83
+ * heuristics, then checks for agent-specific env vars.
84
+ */
85
+ export function detectAgent() {
86
+ const env = process.env;
87
+ // Agent-specific env vars (checked first — these are unambiguous)
88
+ if (env.CLAUDECODE === "1" ||
89
+ env.CLAUDE_CODE === "1" ||
90
+ env.CLAUDE_CLI === "1") {
91
+ return "claude-code";
92
+ }
93
+ if (env.CODEX === "1")
94
+ return "codex";
95
+ if (env.CLINE === "1")
96
+ return "cline";
97
+ // Fall back to IDE detection (Cursor, VS Code, Windsurf)
98
+ const ide = detectIde();
99
+ if (ide) {
100
+ const IDE_TO_AGENT = {
101
+ Cursor: "cursor",
102
+ "VS Code": "vscode",
103
+ Windsurf: "windsurf",
104
+ };
105
+ return IDE_TO_AGENT[ide] ?? null;
106
+ }
107
+ return null;
108
+ }
@@ -0,0 +1,62 @@
1
+ import { existsSync } from "node:fs";
2
+ import { resolve } from "node:path";
3
+ import { execa } from "execa";
4
+ /**
5
+ * Gets VS Code's global config directory based on the platform
6
+ */
7
+ export function getVSCodeGlobalConfigDir(homeDir) {
8
+ const platform = process.platform;
9
+ if (platform === "darwin") {
10
+ // macOS: ~/Library/Application Support/Code/User
11
+ return resolve(homeDir, "Library", "Application Support", "Code", "User");
12
+ }
13
+ if (platform === "linux") {
14
+ // Linux: ~/.config/Code/User
15
+ return resolve(homeDir, ".config", "Code", "User");
16
+ }
17
+ if (platform === "win32") {
18
+ // Windows: %APPDATA%\Code\User
19
+ const appData = process.env.APPDATA;
20
+ if (appData) {
21
+ return resolve(appData, "Code", "User");
22
+ }
23
+ }
24
+ return null;
25
+ }
26
+ /**
27
+ * Checks if Claude CLI is installed
28
+ */
29
+ async function isClaudeCLIInstalled() {
30
+ try {
31
+ await execa("claude", ["--version"], {
32
+ stdio: "ignore",
33
+ timeout: 5000,
34
+ });
35
+ return true;
36
+ }
37
+ catch {
38
+ return false;
39
+ }
40
+ }
41
+ /**
42
+ * Detects which editors are installed on the system
43
+ */
44
+ export async function detectAvailableEditors(homeDir) {
45
+ const editors = [];
46
+ // Check for Cursor (global config directory)
47
+ const cursorDir = resolve(homeDir, ".cursor");
48
+ if (existsSync(cursorDir)) {
49
+ editors.push("Cursor");
50
+ }
51
+ // Check if VS Code's global config directory exists
52
+ const vscodeGlobalDir = getVSCodeGlobalConfigDir(homeDir);
53
+ if (vscodeGlobalDir && existsSync(vscodeGlobalDir)) {
54
+ editors.push("VS Code");
55
+ }
56
+ // Check for Claude CLI by running the command
57
+ const claudeInstalled = await isClaudeCLIInstalled();
58
+ if (claudeInstalled) {
59
+ editors.push("Claude CLI");
60
+ }
61
+ return editors;
62
+ }
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Converts a phase's `args` (e.g. ["neon-auth", "--json", "--setup"]) into the
3
+ * `neon init --agent --data` invocation that reaches the same handler.
4
+ */
5
+ function argsToCommand(args) {
6
+ const data = {};
7
+ let i = 0;
8
+ // First non-flag arg is the subcommand → step
9
+ if (args.length > 0 && !args[0].startsWith("-")) {
10
+ data.step = args[0];
11
+ i = 1;
12
+ }
13
+ while (i < args.length) {
14
+ const arg = args[i];
15
+ if (arg.startsWith("--")) {
16
+ const key = arg
17
+ .slice(2)
18
+ .replace(/-([a-z])/g, (_, c) => c.toUpperCase());
19
+ if (key === "json") {
20
+ i += 1;
21
+ continue;
22
+ }
23
+ const next = args[i + 1];
24
+ if (next !== undefined && !next.startsWith("-")) {
25
+ data[key] = next;
26
+ i += 2;
27
+ }
28
+ else {
29
+ data[key] = true;
30
+ i += 1;
31
+ }
32
+ }
33
+ else {
34
+ i += 1;
35
+ }
36
+ }
37
+ return `neon init --agent --data '${JSON.stringify(data)}'`;
38
+ }
39
+ /**
40
+ * Walks a phase response object and:
41
+ * 1. Replaces `args` arrays with `command` strings (neon init --data format)
42
+ * 2. Renames `run_neon_init` → `run_shell_command`
43
+ * 3. Adds a description to finalize steps
44
+ */
45
+ export function enrichResponse(obj) {
46
+ if (obj === null || typeof obj !== "object")
47
+ return obj;
48
+ if (Array.isArray(obj))
49
+ return obj.map(enrichResponse);
50
+ const record = obj;
51
+ const result = {};
52
+ for (const [key, value] of Object.entries(record)) {
53
+ result[key] = enrichResponse(value);
54
+ }
55
+ // Replace args with command in run_neon_init actions and responseMapping entries
56
+ if (Array.isArray(result.args)) {
57
+ result.command = argsToCommand(result.args);
58
+ delete result.args;
59
+ }
60
+ // Rename run_neon_init → run_shell_command so agents don't infer subcommand patterns
61
+ if (result.type === "run_neon_init") {
62
+ result.type = "run_shell_command";
63
+ // Help agents understand finalize is the terminal step
64
+ if (typeof result.command === "string" &&
65
+ result.command.includes('"step":"finalize"')) {
66
+ result.description =
67
+ "Run this command to complete the setup. This is the final step — do not run any other neon init commands after this.";
68
+ }
69
+ }
70
+ return result;
71
+ }