replicas-engine 0.1.744 → 0.1.745

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.
@@ -0,0 +1,603 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ createPanelKeys
4
+ } from "./chunk-SFWEF25L.js";
5
+ import {
6
+ KNOWN_SERVER_PRESETS
7
+ } from "./chunk-WYE7EXGQ.js";
8
+ import {
9
+ getConfigDirName,
10
+ matchesKey,
11
+ truncateToWidth,
12
+ visibleWidth
13
+ } from "./chunk-NZRMKTVA.js";
14
+ import "./chunk-5KSXSK7Y.js";
15
+
16
+ // ../node_modules/.bun/pi-mcp-adapter@2.32.1+04b0d7b0bc944965/node_modules/pi-mcp-adapter/mcp-setup-panel.ts
17
+ var DEFAULT_THEME = {
18
+ border: "2",
19
+ title: "36",
20
+ selected: "32",
21
+ hint: "2",
22
+ success: "32",
23
+ warning: "33",
24
+ muted: "2;3"
25
+ };
26
+ var MIN_PANEL_WIDTH = 24;
27
+ var COMPACT_WIDTH = 60;
28
+ var COMPACT_ACTION_ROWS = 7;
29
+ var DESKTOP_PREVIEW_WIDTH = 74;
30
+ function fg(code, text) {
31
+ return code ? `\x1B[${code}m${text}\x1B[0m` : text;
32
+ }
33
+ function wrapText(text, width) {
34
+ if (width <= 8) return [text];
35
+ const words = text.split(/\s+/).filter(Boolean);
36
+ const lines = [];
37
+ let current = "";
38
+ for (const word of words) {
39
+ const candidate = current ? `${current} ${word}` : word;
40
+ if (visibleWidth(candidate) <= width) {
41
+ current = candidate;
42
+ continue;
43
+ }
44
+ if (current) lines.push(current);
45
+ current = word;
46
+ }
47
+ if (current) lines.push(current);
48
+ return lines.length > 0 ? lines : [""];
49
+ }
50
+ var McpSetupPanel = class _McpSetupPanel {
51
+ constructor(discovery, callbacks, options, tui, done) {
52
+ this.discovery = discovery;
53
+ this.callbacks = callbacks;
54
+ this.options = options;
55
+ this.done = done;
56
+ this.tui = tui;
57
+ this.keys = createPanelKeys(options.keybindings);
58
+ this.screen = options.mode;
59
+ for (const entry of discovery.imports) {
60
+ this.selectedImports.add(entry.kind);
61
+ }
62
+ this.resetInactivityTimeout();
63
+ }
64
+ discovery;
65
+ callbacks;
66
+ options;
67
+ done;
68
+ screen;
69
+ actionCursor = 0;
70
+ importCursor = 0;
71
+ pathCursor = 0;
72
+ sharedConfigTarget = "project";
73
+ selectedImports = /* @__PURE__ */ new Set();
74
+ busy = false;
75
+ notice = null;
76
+ tui;
77
+ t = DEFAULT_THEME;
78
+ keys;
79
+ inactivityTimeout = null;
80
+ static INACTIVITY_MS = 6e4;
81
+ resetInactivityTimeout() {
82
+ if (this.inactivityTimeout) clearTimeout(this.inactivityTimeout);
83
+ this.inactivityTimeout = setTimeout(() => {
84
+ this.cleanup();
85
+ this.done();
86
+ }, _McpSetupPanel.INACTIVITY_MS);
87
+ }
88
+ cleanup() {
89
+ if (this.inactivityTimeout) {
90
+ clearTimeout(this.inactivityTimeout);
91
+ this.inactivityTimeout = null;
92
+ }
93
+ }
94
+ getActions() {
95
+ const actions = [];
96
+ if (this.screen === "empty") {
97
+ actions.push({ id: "run-setup", label: "Run setup", description: "Inspect detected configs, adopt imports, and scaffold a minimal `.mcp.json`." });
98
+ }
99
+ if (this.discovery.imports.length > 0) {
100
+ actions.push({ id: "adopt-imports", label: "Adopt detected compatibility imports", description: `Choose which host-specific MCP configs Pi should import into its own override file. ${this.discovery.imports.length} source${this.discovery.imports.length === 1 ? "" : "s"} found.` });
101
+ }
102
+ actions.push(
103
+ { id: "select-shared-target", label: `${this.sharedConfigTarget === "project" ? "\u25CF" : "\u25CB"} Add to this project (.mcp.json)`, description: "Write new shared MCP servers to the project/team config.", target: "project" },
104
+ { id: "select-shared-target", label: `${this.sharedConfigTarget === "global" ? "\u25CF" : "\u25CB"} Add globally (~/.config/mcp/mcp.json)`, description: "Write new shared MCP servers to your all-projects config.", target: "global" }
105
+ );
106
+ actions.push({ id: "view-example", label: "View example shared config", description: "Preview a working shared MCP config you can paste or adapt." });
107
+ if (!this.selectedSharedConfigExists()) {
108
+ actions.push({ id: "scaffold-shared-config", label: `Scaffold ${this.sharedTargetLabel()}`, description: "Write a minimal config at the selected normal MCP setup path, then reload Pi." });
109
+ }
110
+ actions.push({ id: "show-precedence", label: "Explain config precedence", description: "Show the read order and where Pi writes compatibility settings." });
111
+ if (this.getDetectedPaths().length > 0) {
112
+ actions.push({ id: "open-paths", label: "Open detected config paths", description: "Browse the actual config files that Pi discovered on this machine." });
113
+ }
114
+ for (const preset of KNOWN_SERVER_PRESETS) {
115
+ actions.push({ id: "add-known-server", label: preset.name, description: preset.summary, preset });
116
+ }
117
+ if (!this.discovery.repoPrompt.configured && this.discovery.repoPrompt.executablePath && this.discovery.repoPrompt.targetPath && this.discovery.repoPrompt.entry && this.discovery.repoPrompt.serverName) {
118
+ actions.push({ id: "add-repoprompt", label: "Add RepoPrompt to selected shared config", description: "Write a standard MCP entry for RepoPrompt to the selected normal setup path, then reload MCP in-session." });
119
+ }
120
+ actions.push({ id: "close", label: "Close", description: "Exit the onboarding flow." });
121
+ return actions;
122
+ }
123
+ getDetectedPaths() {
124
+ const paths = [
125
+ ...this.discovery.sources.filter((source) => source.exists).map((source) => source.path),
126
+ ...this.discovery.imports.map((entry) => entry.path)
127
+ ];
128
+ return [...new Set(paths)];
129
+ }
130
+ sharedTargetLabel() {
131
+ return this.sharedConfigTarget === "project" ? "project .mcp.json" : "global ~/.config/mcp/mcp.json";
132
+ }
133
+ selectedSharedConfigExists() {
134
+ const sourceId = this.sharedConfigTarget === "project" ? "shared-project" : "shared-global";
135
+ return this.discovery.sources.some((source) => source.id === sourceId && source.exists);
136
+ }
137
+ getSelectedAction() {
138
+ const actions = this.getActions();
139
+ return actions[this.actionCursor];
140
+ }
141
+ handleInput(data) {
142
+ this.resetInactivityTimeout();
143
+ if (!this.busy) this.notice = null;
144
+ if (matchesKey(data, "ctrl+c")) {
145
+ this.cleanup();
146
+ this.done();
147
+ return;
148
+ }
149
+ if (matchesKey(data, "escape")) {
150
+ if (this.screen === "imports" || this.screen === "paths") {
151
+ this.screen = this.discovery.hasAnyConfig ? "setup" : "empty";
152
+ this.tui.requestRender();
153
+ return;
154
+ }
155
+ this.cleanup();
156
+ this.done();
157
+ return;
158
+ }
159
+ if (this.busy) return;
160
+ if (this.screen === "imports") {
161
+ this.handleImportsInput(data);
162
+ return;
163
+ }
164
+ if (this.screen === "paths") {
165
+ this.handlePathsInput(data);
166
+ return;
167
+ }
168
+ const actions = this.getActions();
169
+ if (this.keys.selectUp(data)) {
170
+ this.actionCursor = Math.max(0, this.actionCursor - 1);
171
+ this.tui.requestRender();
172
+ return;
173
+ }
174
+ if (this.keys.selectDown(data)) {
175
+ this.actionCursor = Math.min(actions.length - 1, this.actionCursor + 1);
176
+ this.tui.requestRender();
177
+ return;
178
+ }
179
+ if (this.keys.selectConfirm(data)) {
180
+ const selected = this.getSelectedAction();
181
+ if (selected) void this.runAction(selected);
182
+ }
183
+ }
184
+ handleImportsInput(data) {
185
+ const imports = this.discovery.imports;
186
+ if (this.keys.selectUp(data)) {
187
+ this.importCursor = Math.max(0, this.importCursor - 1);
188
+ this.tui.requestRender();
189
+ return;
190
+ }
191
+ if (this.keys.selectDown(data)) {
192
+ this.importCursor = Math.min(imports.length - 1, this.importCursor + 1);
193
+ this.tui.requestRender();
194
+ return;
195
+ }
196
+ if (matchesKey(data, "space")) {
197
+ const current = imports[this.importCursor];
198
+ if (!current) return;
199
+ if (this.selectedImports.has(current.kind)) {
200
+ this.selectedImports.delete(current.kind);
201
+ } else {
202
+ this.selectedImports.add(current.kind);
203
+ }
204
+ this.tui.requestRender();
205
+ return;
206
+ }
207
+ if (this.keys.selectConfirm(data)) {
208
+ void this.applySelectedImports();
209
+ }
210
+ }
211
+ handlePathsInput(data) {
212
+ const paths = this.getDetectedPaths();
213
+ if (this.keys.selectUp(data)) {
214
+ this.pathCursor = Math.max(0, this.pathCursor - 1);
215
+ this.tui.requestRender();
216
+ return;
217
+ }
218
+ if (this.keys.selectDown(data)) {
219
+ this.pathCursor = Math.min(paths.length - 1, this.pathCursor + 1);
220
+ this.tui.requestRender();
221
+ return;
222
+ }
223
+ if (this.keys.selectConfirm(data)) {
224
+ const selected = paths[this.pathCursor];
225
+ if (!selected) return;
226
+ void this.runBusy(async () => {
227
+ await this.callbacks.openPath(selected);
228
+ this.notice = { text: `Opened ${selected}`, tone: "success" };
229
+ });
230
+ }
231
+ }
232
+ async runAction(action) {
233
+ if (action.id === "run-setup") {
234
+ this.screen = "setup";
235
+ this.actionCursor = 0;
236
+ this.tui.requestRender();
237
+ return;
238
+ }
239
+ if (action.id === "adopt-imports") {
240
+ this.screen = "imports";
241
+ this.importCursor = 0;
242
+ this.tui.requestRender();
243
+ return;
244
+ }
245
+ if (action.id === "open-paths") {
246
+ this.screen = "paths";
247
+ this.pathCursor = 0;
248
+ this.tui.requestRender();
249
+ return;
250
+ }
251
+ if (action.id === "select-shared-target" && action.target) {
252
+ this.sharedConfigTarget = action.target;
253
+ this.notice = { text: `New shared servers will be written to ${this.sharedTargetLabel()}.`, tone: "muted" };
254
+ this.tui.requestRender();
255
+ return;
256
+ }
257
+ if (action.id === "scaffold-shared-config") {
258
+ await this.runBusy(async () => {
259
+ const result = await this.callbacks.scaffoldConfig(this.sharedConfigTarget);
260
+ this.callbacks.markSetupCompleted();
261
+ this.notice = { text: `Wrote starter config to ${result.path}. Pi will reload after this panel closes.`, tone: "success" };
262
+ });
263
+ return;
264
+ }
265
+ if (action.id === "add-repoprompt") {
266
+ await this.runBusy(async () => {
267
+ const result = await this.callbacks.addRepoPrompt(this.sharedConfigTarget);
268
+ this.callbacks.markSetupCompleted();
269
+ this.notice = { text: `Added ${result.serverName} to ${result.path}. Pi will reload after this panel closes.`, tone: "success" };
270
+ });
271
+ return;
272
+ }
273
+ if (action.id === "add-known-server" && action.preset) {
274
+ const preset = action.preset;
275
+ await this.runBusy(async () => {
276
+ const result = await this.callbacks.addKnownServer(preset, this.sharedConfigTarget);
277
+ this.callbacks.markSetupCompleted();
278
+ this.notice = { text: `Added ${result.serverName} to ${result.path}. Pi will reload after this panel closes.`, tone: "success" };
279
+ });
280
+ return;
281
+ }
282
+ if (action.id === "close") {
283
+ this.cleanup();
284
+ this.done();
285
+ return;
286
+ }
287
+ this.notice = { text: "Review the details below. Press Enter on an action with a side effect to apply it.", tone: "muted" };
288
+ this.tui.requestRender();
289
+ }
290
+ async applySelectedImports() {
291
+ const selected = this.discovery.imports.filter((entry) => this.selectedImports.has(entry.kind)).map((entry) => entry.kind);
292
+ if (selected.length === 0) {
293
+ this.notice = { text: "Select at least one compatibility import first.", tone: "warning" };
294
+ this.tui.requestRender();
295
+ return;
296
+ }
297
+ await this.runBusy(async () => {
298
+ const result = await this.callbacks.adoptImports(selected);
299
+ this.callbacks.markSetupCompleted();
300
+ this.notice = result.added.length > 0 ? { text: `Added ${result.added.join(", ")} to ${result.path}. Pi will reload after this panel closes.`, tone: "success" } : { text: `No changes needed in ${result.path}.`, tone: "muted" };
301
+ this.screen = this.discovery.hasAnyConfig ? "setup" : "empty";
302
+ this.actionCursor = 0;
303
+ });
304
+ }
305
+ async runBusy(fn) {
306
+ this.busy = true;
307
+ this.notice = { text: "Working...", tone: "muted" };
308
+ this.tui.requestRender();
309
+ try {
310
+ await fn();
311
+ } catch (error) {
312
+ this.notice = {
313
+ text: error instanceof Error ? error.message : String(error),
314
+ tone: "warning"
315
+ };
316
+ } finally {
317
+ this.busy = false;
318
+ this.tui.requestRender();
319
+ }
320
+ }
321
+ render(width) {
322
+ const panelW = Math.max(MIN_PANEL_WIDTH, width);
323
+ const innerW = panelW - 2;
324
+ const contentW = this.contentWidth(innerW);
325
+ const lines = [];
326
+ const border = fg(this.t.border, "\u2500".repeat(innerW));
327
+ lines.push(`\u250C${border}\u2510`);
328
+ lines.push(this.padLine(fg(this.t.title, "MCP setup"), innerW));
329
+ for (const line of wrapText(this.discoverySummaryLine(), contentW)) {
330
+ lines.push(this.padLine(line, innerW));
331
+ }
332
+ for (const line of wrapText(this.secondarySummaryLine(), contentW)) {
333
+ lines.push(this.padLine(fg(this.t.muted, line), innerW));
334
+ }
335
+ lines.push(this.padLine("", innerW));
336
+ if (this.notice) {
337
+ const tone = this.notice.tone === "success" ? this.t.success : this.notice.tone === "warning" ? this.t.warning : this.t.hint;
338
+ for (const line of wrapText(this.notice.text, contentW)) {
339
+ lines.push(this.padLine(fg(tone, line), innerW));
340
+ }
341
+ lines.push(this.padLine("", innerW));
342
+ }
343
+ lines.push(`\u251C${border}\u2524`);
344
+ if (this.screen === "imports") {
345
+ lines.push(...this.renderImports(innerW));
346
+ } else if (this.screen === "paths") {
347
+ lines.push(...this.renderPaths(innerW));
348
+ } else {
349
+ lines.push(...this.renderActions(innerW));
350
+ }
351
+ lines.push(`\u2514${border}\u2518`);
352
+ return lines;
353
+ }
354
+ renderActions(innerW) {
355
+ const lines = [];
356
+ const actions = this.getActions();
357
+ const compact = innerW < COMPACT_WIDTH;
358
+ const { start, end } = compact ? this.visibleActionRange(actions.length) : { start: 0, end: actions.length };
359
+ if (start > 0) {
360
+ lines.push(this.padLine(fg(this.t.muted, `\u2026 ${start} more above`), innerW));
361
+ }
362
+ for (let index = start; index < end; index++) {
363
+ const action = actions[index];
364
+ if (!action) continue;
365
+ if (action.id === "select-shared-target" && (index === start || actions[index - 1]?.id !== "select-shared-target")) {
366
+ lines.push(this.padLine(fg(this.t.title, "Choose where new shared servers go"), innerW));
367
+ }
368
+ if (action.id === "add-known-server" && (index === start || actions[index - 1]?.id !== "add-known-server")) {
369
+ lines.push(this.padLine(fg(this.t.title, `Add a known server to ${this.sharedTargetLabel()}`), innerW));
370
+ }
371
+ const selected = index === this.actionCursor;
372
+ const cursor = selected ? fg(this.t.selected, "\u203A") : " ";
373
+ lines.push(this.padLine(`${cursor} ${truncateToWidth(action.label, this.contentWidth(innerW) - 2)}`, innerW));
374
+ }
375
+ if (end < actions.length) {
376
+ lines.push(this.padLine(fg(this.t.muted, `\u2026 ${actions.length - end} more below`), innerW));
377
+ }
378
+ lines.push(this.padLine("", innerW));
379
+ const preview = this.getActionPreview(this.getSelectedAction(), this.previewWidth(innerW));
380
+ for (const line of preview) {
381
+ lines.push(this.padLine(line, innerW));
382
+ }
383
+ lines.push(this.padLine("", innerW));
384
+ const hint = compact ? "Enter select \xB7 Esc back" : "Enter selects, Esc goes back, Ctrl+C closes.";
385
+ lines.push(this.padLine(fg(this.t.muted, hint), innerW));
386
+ return lines;
387
+ }
388
+ renderImports(innerW) {
389
+ const lines = [];
390
+ lines.push(this.padLine("Select compatibility imports. Space toggles, Enter saves, Esc goes back.", innerW));
391
+ lines.push(this.padLine("", innerW));
392
+ for (let index = 0; index < this.discovery.imports.length; index++) {
393
+ const entry = this.discovery.imports[index];
394
+ if (!entry) continue;
395
+ const selected2 = this.selectedImports.has(entry.kind) ? "[x]" : "[ ]";
396
+ const cursor = index === this.importCursor ? fg(this.t.selected, "\u203A") : " ";
397
+ lines.push(this.padLine(`${cursor} ${selected2} ${entry.kind} ${entry.path}`, innerW));
398
+ }
399
+ lines.push(this.padLine("", innerW));
400
+ const selected = this.discovery.imports.filter((entry) => this.selectedImports.has(entry.kind)).map((entry) => entry.kind);
401
+ const preview = this.callbacks.previewImports(selected);
402
+ for (const line of this.formatWritePreview("Compatibility import write preview", preview, [], this.previewWidth(innerW))) {
403
+ lines.push(this.padLine(line, innerW));
404
+ }
405
+ return lines;
406
+ }
407
+ renderPaths(innerW) {
408
+ const lines = [];
409
+ lines.push(this.padLine("Select a detected config path to open. Enter opens it, Esc goes back.", innerW));
410
+ lines.push(this.padLine("", innerW));
411
+ const paths = this.getDetectedPaths();
412
+ for (let index = 0; index < paths.length; index++) {
413
+ const cursor = index === this.pathCursor ? fg(this.t.selected, "\u203A") : " ";
414
+ const path = paths[index];
415
+ if (path !== void 0) lines.push(this.padLine(`${cursor} ${path}`, innerW));
416
+ }
417
+ return lines;
418
+ }
419
+ discoverySummaryLine() {
420
+ if (!this.discovery.hasAnyConfig) {
421
+ return fg(this.t.warning, this.options.onboardingState.setupCompleted ? "No MCP servers are active right now." : "No MCP config is active yet.");
422
+ }
423
+ if (this.discovery.totalServerCount === 0 && (this.discovery.imports.length > 0 || !!this.discovery.repoPrompt.executablePath)) {
424
+ return fg(this.t.warning, "Pi found MCP-related setup options, but none are active in Pi yet.");
425
+ }
426
+ const shared = this.discovery.sources.filter((source) => source.kind === "shared" && source.serverCount > 0).length;
427
+ const piOwned = this.discovery.sources.filter((source) => source.kind === "pi" && source.serverCount > 0).length;
428
+ return fg(this.t.hint, `Detected ${this.discovery.totalServerCount} configured servers across ${shared} shared and ${piOwned} Pi-owned source${shared + piOwned === 1 ? "" : "s"}.`);
429
+ }
430
+ secondarySummaryLine() {
431
+ const hostNote = this.discovery.hostConfigs.length > 0 ? ` Host discovery is ${this.discovery.hostConfigDiscovery}; ${this.discovery.hostConfigs.length} host source${this.discovery.hostConfigs.length === 1 ? "" : "s"} detected.` : "";
432
+ const conflictNote = this.discovery.conflicts.length > 0 ? ` ${this.discovery.conflicts.length} same-name conflict${this.discovery.conflicts.length === 1 ? "" : "s"} reported.` : "";
433
+ if (!this.discovery.hasAnyConfig) {
434
+ return `Add shared servers to .mcp.json for this project/team or ~/.config/mcp/mcp.json for all projects. Adopt host imports or quick-add RepoPrompt from this screen.${hostNote}${conflictNote}`;
435
+ }
436
+ if (this.discovery.totalServerCount === 0 && this.discovery.imports.length > 0) {
437
+ return `Detected ${this.discovery.imports.length} compatibility import source${this.discovery.imports.length === 1 ? "" : "s"}. Adopt them into Pi or inspect the underlying files.${hostNote}${conflictNote}`;
438
+ }
439
+ return `Use .mcp.json for project/team servers or ~/.config/mcp/mcp.json for all projects. Pi-owned files are for compatibility imports and adapter-specific overrides, not another normal setup path.${hostNote}${conflictNote}`;
440
+ }
441
+ visibleActionRange(total) {
442
+ if (total <= COMPACT_ACTION_ROWS) return { start: 0, end: total };
443
+ const half = Math.floor(COMPACT_ACTION_ROWS / 2);
444
+ const start = Math.min(Math.max(0, this.actionCursor - half), Math.max(0, total - COMPACT_ACTION_ROWS));
445
+ return { start, end: Math.min(total, start + COMPACT_ACTION_ROWS) };
446
+ }
447
+ contentWidth(innerW) {
448
+ return Math.max(8, innerW - 4);
449
+ }
450
+ previewWidth(innerW) {
451
+ return Math.max(12, Math.min(DESKTOP_PREVIEW_WIDTH, this.contentWidth(innerW)));
452
+ }
453
+ getActionPreview(action, previewW = DESKTOP_PREVIEW_WIDTH) {
454
+ switch (action?.id) {
455
+ case "run-setup":
456
+ return this.formatPreview([
457
+ "Run setup to adopt host-specific imports, inspect detected paths, and scaffold a minimal `.mcp.json` if needed."
458
+ ], previewW);
459
+ case "adopt-imports":
460
+ return this.formatWritePreview(
461
+ "Compatibility import write preview",
462
+ this.callbacks.previewImports(this.discovery.imports.filter((entry) => this.selectedImports.has(entry.kind)).map((entry) => entry.kind)),
463
+ [
464
+ `Detected imports: ${this.discovery.imports.map((entry) => `${entry.kind} (${entry.serverCount} servers)`).join(", ")}`,
465
+ "Selected imports are written into the Pi agent dir config as Pi-owned compatibility state."
466
+ ],
467
+ previewW
468
+ );
469
+ case "select-shared-target":
470
+ return this.formatPreview([
471
+ action.target === "project" ? "Project target: .mcp.json" : "Global target: ~/.config/mcp/mcp.json",
472
+ "Known server presets and starter configs will be written to the selected normal MCP setup path.",
473
+ "Pi-owned mcp.json files remain compatibility and adapter-only override state."
474
+ ], previewW);
475
+ case "view-example":
476
+ return this.formatPreview([
477
+ "Example shared `.mcp.json`:",
478
+ "{",
479
+ ' "mcpServers": {',
480
+ ' "chrome-devtools": {',
481
+ ' "command": "npx",',
482
+ ' "args": ["-y", "chrome-devtools-mcp@1.6.0"]',
483
+ " }",
484
+ " }",
485
+ "}",
486
+ "",
487
+ "Use Scaffold selected config when you want a safe empty shell instead of a live example server."
488
+ ], previewW);
489
+ case "show-precedence":
490
+ return this.formatPreview([
491
+ "Recommended shared config:",
492
+ " project/team: .mcp.json",
493
+ " all projects: ~/.config/mcp/mcp.json",
494
+ "",
495
+ "Advanced compatibility and Pi-owned layers:",
496
+ " host imports, .agents files, package MCP manifests, and Pi overrides",
497
+ "",
498
+ "Read order (later entries win):",
499
+ "0. detected host configs (opt-in lowest-precedence fallback)",
500
+ "1. ~/.config/mcp/mcp.json",
501
+ "2. ~/.agents/mcp.json",
502
+ "3. ~/.agents/mcp/mcp.json",
503
+ "4. <Pi agent dir>/mcp.json",
504
+ "5. .mcp.json",
505
+ `6. ${getConfigDirName()}/mcp.json`,
506
+ `Host discovery: ${this.discovery.hostConfigDiscovery}. Conflicts reported: ${this.discovery.conflicts.length}.`,
507
+ ...this.discovery.conflicts.slice(0, 8).map(
508
+ (conflict) => `${conflict.serverName}: ${conflict.sources.map((source) => source.path).join(" -> ")} (winner: ${conflict.winner.path})`
509
+ ),
510
+ "Pi writes compatibility imports and adapter-only overrides to Pi-owned files."
511
+ ], previewW);
512
+ case "open-paths":
513
+ return this.formatPreview(this.getDetectedPaths().length > 0 ? ["Detected paths:", ...this.getDetectedPaths()] : ["No config paths were detected."], previewW);
514
+ case "add-repoprompt": {
515
+ const repoPrompt = this.discovery.repoPrompt;
516
+ const preview = this.callbacks.previewRepoPrompt(this.sharedConfigTarget);
517
+ if (!preview) {
518
+ return this.formatPreview(["RepoPrompt is not available to add from this setup screen."], previewW);
519
+ }
520
+ return this.formatWritePreview(
521
+ "RepoPrompt write preview",
522
+ preview,
523
+ [
524
+ `Executable: ${repoPrompt.executablePath ?? "not found"}`,
525
+ `Target: ${this.sharedTargetLabel()}`,
526
+ `Server name: ${repoPrompt.serverName ?? "repoprompt"}`
527
+ ],
528
+ previewW
529
+ );
530
+ }
531
+ case "add-known-server": {
532
+ const preset = action.preset;
533
+ if (!preset) return this.formatPreview(["Known server preset is unavailable."], previewW);
534
+ return this.formatWritePreview(
535
+ `${preset.name} write preview`,
536
+ this.callbacks.previewKnownServer(preset, this.sharedConfigTarget),
537
+ [preset.summary, `Target: ${this.sharedTargetLabel()}`],
538
+ previewW
539
+ );
540
+ }
541
+ case "scaffold-shared-config":
542
+ return this.formatWritePreview(
543
+ `${this.sharedTargetLabel()} starter write preview`,
544
+ this.callbacks.previewStarterConfig(this.sharedConfigTarget),
545
+ [
546
+ "This writes a minimal config at the selected normal MCP setup path.",
547
+ "It intentionally avoids adding a fake placeholder server that would fail on first reload."
548
+ ],
549
+ previewW
550
+ );
551
+ case "close":
552
+ default:
553
+ return this.formatPreview(["Close the setup flow."], previewW);
554
+ }
555
+ }
556
+ formatPreview(lines, width = DESKTOP_PREVIEW_WIDTH) {
557
+ const preview = [];
558
+ for (const line of lines) {
559
+ preview.push(...wrapText(line, width));
560
+ }
561
+ return preview;
562
+ }
563
+ formatWritePreview(title, preview, intro = [], width = DESKTOP_PREVIEW_WIDTH) {
564
+ const lines = [];
565
+ for (const line of intro) {
566
+ lines.push(...wrapText(line, width));
567
+ }
568
+ if (intro.length > 0) lines.push("");
569
+ lines.push(...wrapText(`${title}: ${preview.path}`, width));
570
+ lines.push(...wrapText(preview.existed ? "Existing file detected. Showing exact before/after diff." : "New file will be created. Showing exact content diff.", width));
571
+ lines.push("");
572
+ const diffLines = preview.diffText.split("\n");
573
+ const maxLines = 18;
574
+ const shown = diffLines.slice(0, maxLines);
575
+ for (const line of shown) {
576
+ lines.push(...wrapText(line, width));
577
+ }
578
+ if (diffLines.length > maxLines) {
579
+ lines.push(...wrapText(`\u2026 ${diffLines.length - maxLines} more diff line${diffLines.length - maxLines === 1 ? "" : "s"}`, width));
580
+ }
581
+ return lines;
582
+ }
583
+ padLine(text, innerW) {
584
+ const inset = 2;
585
+ const contentW = Math.max(0, innerW - inset * 2);
586
+ const fitted = truncateToWidth(text, contentW, "\u2026", true);
587
+ const plainWidth = visibleWidth(fitted);
588
+ const padding = Math.max(0, contentW - plainWidth);
589
+ return `\u2502${" ".repeat(inset)}${fitted}${" ".repeat(padding)}${" ".repeat(inset)}\u2502`;
590
+ }
591
+ invalidate() {
592
+ }
593
+ dispose() {
594
+ this.cleanup();
595
+ }
596
+ };
597
+ function createMcpSetupPanel(discovery, callbacks, options, tui, done) {
598
+ return new McpSetupPanel(discovery, callbacks, options, tui, done);
599
+ }
600
+ export {
601
+ McpSetupPanel,
602
+ createMcpSetupPanel
603
+ };
@@ -6,6 +6,7 @@ import {
6
6
  isRecord
7
7
  } from "./chunk-2RB7SIP3.js";
8
8
  import "./chunk-HMIVYASC.js";
9
+ import "./chunk-5KSXSK7Y.js";
9
10
 
10
11
  // src/post-tool-pr-hook.ts
11
12
  async function main() {