@ldelossa/pi-ide 0.2.1 → 0.2.3
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/README.md +44 -3
- package/index.ts +182 -58
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -50,9 +50,11 @@ Once both are running, use `/ide` inside pi to connect to the editor.
|
|
|
50
50
|
|
|
51
51
|
1. Editor starts a loopback MCP server on a free TCP port. Writes a lockfile to
|
|
52
52
|
`$PI_IDE_LOCK_DIR` (default `~/.pi/ide/<port>.lock`). Removes it on exit.
|
|
53
|
-
2.
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
2. On startup, if `pi-ide.autoconnect` is not `false` and exactly one valid
|
|
54
|
+
lockfile matches `cwd`, the extension connects automatically. A sticky
|
|
55
|
+
reconnect target is remembered across `/new` sessions, keeping the same
|
|
56
|
+
IDE when multiple editors are open. Use `/ide` to manually connect,
|
|
57
|
+
switch IDEs, or disconnect.
|
|
56
58
|
3. Extension opens `ws://127.0.0.1:<port>/` with header
|
|
57
59
|
`x-pi-ide-authorization: <authToken>` and runs MCP `initialize`.
|
|
58
60
|
4. While connected:
|
|
@@ -66,6 +68,45 @@ Once both are running, use `/ide` inside pi to connect to the editor.
|
|
|
66
68
|
surfaced to the agent.
|
|
67
69
|
5. Session shutdown closes the socket.
|
|
68
70
|
|
|
71
|
+
## Autoconnect
|
|
72
|
+
|
|
73
|
+
Autoconnect saves a manual `/ide` step by connecting to the IDE automatically.
|
|
74
|
+
It is enabled by default.
|
|
75
|
+
|
|
76
|
+
Connections are made on two occasions:
|
|
77
|
+
|
|
78
|
+
**On start**
|
|
79
|
+
1. `pi-ide.autoconnect` is not `false`
|
|
80
|
+
2. Exactly one valid IDE lockfile exists for the current working directory
|
|
81
|
+
|
|
82
|
+
**On session replacement** (`/new`, `/resume`, `/reload`)
|
|
83
|
+
If the previous session was connected to an IDE, pi-ide tries to reconnect
|
|
84
|
+
to that same IDE. This sticky reconnect bypasses the single-candidate rule,
|
|
85
|
+
which means it can reconnect even when a second IDE has since started in the
|
|
86
|
+
same directory. If the sticky IDE is no longer valid (closed, stopped,
|
|
87
|
+
connection fails, or its project changed), autoconnect falls back to the
|
|
88
|
+
cold-start rule.
|
|
89
|
+
|
|
90
|
+
### Disabling
|
|
91
|
+
|
|
92
|
+
Add to any Pi settings file:
|
|
93
|
+
|
|
94
|
+
```json
|
|
95
|
+
{
|
|
96
|
+
"pi-ide": {
|
|
97
|
+
"autoconnect": false
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
| Scope | File |
|
|
103
|
+
|-------|------|
|
|
104
|
+
| Global | `~/.pi/agent/settings.json` |
|
|
105
|
+
| Project | `.pi/settings.json` |
|
|
106
|
+
|
|
107
|
+
Project settings override global settings. A missing setting is treated as
|
|
108
|
+
enabled.
|
|
109
|
+
|
|
69
110
|
## Lockfile
|
|
70
111
|
|
|
71
112
|
Path: `$PI_IDE_LOCK_DIR/<port>.lock` (default `~/.pi/ide/<port>.lock`).
|
package/index.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { basename, resolve as resolvePath } from "node:path";
|
|
|
3
3
|
import { completeSimple } from "@earendil-works/pi-ai";
|
|
4
4
|
import {
|
|
5
5
|
isToolCallEventType,
|
|
6
|
+
SettingsManager,
|
|
6
7
|
type ExtensionAPI,
|
|
7
8
|
type ExtensionContext,
|
|
8
9
|
type ExtensionUIContext,
|
|
@@ -31,6 +32,60 @@ const WIDGET_KEY = "pi-ide";
|
|
|
31
32
|
const SPINNER = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
32
33
|
const SPINNER_INTERVAL_MS = 80;
|
|
33
34
|
|
|
35
|
+
// --- Autoconnect config & state ---
|
|
36
|
+
const PI_IDE_SETTINGS_KEY = "pi-ide";
|
|
37
|
+
const STICKY_STATE_KEY = Symbol.for("@ldelossa/pi-ide:sticky-state");
|
|
38
|
+
|
|
39
|
+
type PiIdeSettings = {
|
|
40
|
+
autoconnect?: boolean;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
type StickyState = {
|
|
44
|
+
lockfile?: Lockfile;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// --- Settings helpers ---
|
|
48
|
+
|
|
49
|
+
function extractPiIdeSettings(settings: Record<string, unknown>): PiIdeSettings {
|
|
50
|
+
const raw = settings[PI_IDE_SETTINGS_KEY];
|
|
51
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return {};
|
|
52
|
+
const obj = raw as Record<string, unknown>;
|
|
53
|
+
return {
|
|
54
|
+
autoconnect: typeof obj.autoconnect === "boolean" ? obj.autoconnect : undefined,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function isAutoconnectEnabled(cwd: string): boolean {
|
|
59
|
+
const manager = SettingsManager.create(cwd);
|
|
60
|
+
const globalSettings = extractPiIdeSettings(manager.getGlobalSettings() as Record<string, unknown>);
|
|
61
|
+
const projectSettings = extractPiIdeSettings(manager.getProjectSettings() as Record<string, unknown>);
|
|
62
|
+
return projectSettings.autoconnect ?? globalSettings.autoconnect ?? true;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// --- Sticky state helpers ---
|
|
66
|
+
|
|
67
|
+
function getStickyState(): StickyState {
|
|
68
|
+
const g = globalThis as typeof globalThis & Record<symbol, StickyState | undefined>;
|
|
69
|
+
g[STICKY_STATE_KEY] ??= {};
|
|
70
|
+
return g[STICKY_STATE_KEY]!;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function setStickyLockfile(lockfile: Lockfile): void {
|
|
74
|
+
getStickyState().lockfile = lockfile;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function clearStickyLockfile(): void {
|
|
78
|
+
delete getStickyState().lockfile;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function getStickyLockfile(): Lockfile | undefined {
|
|
82
|
+
return getStickyState().lockfile;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function sameLockfile(a: Lockfile, b: Lockfile): boolean {
|
|
86
|
+
return a.port === b.port && a.pid === b.pid && a.authToken === b.authToken;
|
|
87
|
+
}
|
|
88
|
+
|
|
34
89
|
let client: IdeClient | null = null;
|
|
35
90
|
let state: EditorState = { filePath: null, cursorLine: null, selection: null };
|
|
36
91
|
let ui: ExtensionUIContext | null = null;
|
|
@@ -41,6 +96,7 @@ let spinnerFrame = 0;
|
|
|
41
96
|
|
|
42
97
|
function disconnectFromIde(): void {
|
|
43
98
|
if (!client) return;
|
|
99
|
+
clearStickyLockfile();
|
|
44
100
|
const old = client;
|
|
45
101
|
client = null;
|
|
46
102
|
old.close();
|
|
@@ -414,14 +470,7 @@ async function pickLockfile(
|
|
|
414
470
|
ui: { select: (t: string, o: string[]) => Promise<string | undefined> },
|
|
415
471
|
current?: Lockfile,
|
|
416
472
|
): Promise<PickResult> {
|
|
417
|
-
const
|
|
418
|
-
const candidates: Lockfile[] = [];
|
|
419
|
-
for (const lf of all) {
|
|
420
|
-
if (!matchesCwd(lf, cwd)) continue;
|
|
421
|
-
if (!isPidAlive(lf.pid)) continue;
|
|
422
|
-
if (!(await isPortListening(lf.port))) continue;
|
|
423
|
-
candidates.push(lf);
|
|
424
|
-
}
|
|
473
|
+
const candidates = await findCandidateLockfiles(cwd);
|
|
425
474
|
if (candidates.length === 0) return { kind: "none" };
|
|
426
475
|
const labels = candidates.map((c) => {
|
|
427
476
|
const folder = c.workspaceFolders[0] ?? "?";
|
|
@@ -435,6 +484,123 @@ async function pickLockfile(
|
|
|
435
484
|
return lockfile ? { kind: "selected", lockfile } : { kind: "cancelled" };
|
|
436
485
|
}
|
|
437
486
|
|
|
487
|
+
/**
|
|
488
|
+
* Find lockfiles that match the given cwd, have a live PID, and are listening.
|
|
489
|
+
*/
|
|
490
|
+
async function findCandidateLockfiles(cwd: string): Promise<Lockfile[]> {
|
|
491
|
+
const all = await listLockfiles();
|
|
492
|
+
const candidates: Lockfile[] = [];
|
|
493
|
+
for (const lf of all) {
|
|
494
|
+
if (!matchesCwd(lf, cwd)) continue;
|
|
495
|
+
if (!isPidAlive(lf.pid)) continue;
|
|
496
|
+
if (!(await isPortListening(lf.port))) continue;
|
|
497
|
+
candidates.push(lf);
|
|
498
|
+
}
|
|
499
|
+
return candidates;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* Create an IdeClient, wire up handlers, and connect.
|
|
504
|
+
*/
|
|
505
|
+
async function connectToLockfile(lockfile: Lockfile, ctx: ExtensionContext, pi: ExtensionAPI): Promise<IdeClient | null> {
|
|
506
|
+
const next = new IdeClient(lockfile);
|
|
507
|
+
ui = ctx.ui;
|
|
508
|
+
next.onNotification = onNotification;
|
|
509
|
+
next.onRequest("getSuggestions", async (params, signal) => {
|
|
510
|
+
inFlightSuggestions++;
|
|
511
|
+
if (inFlightSuggestions === 1) startSpinner();
|
|
512
|
+
renderWidget();
|
|
513
|
+
try {
|
|
514
|
+
const suggestions = await generateSuggestions(pi, (params ?? {}) as SuggestionParams, signal);
|
|
515
|
+
return { suggestions };
|
|
516
|
+
} finally {
|
|
517
|
+
inFlightSuggestions--;
|
|
518
|
+
if (inFlightSuggestions === 0) stopSpinner();
|
|
519
|
+
renderWidget();
|
|
520
|
+
}
|
|
521
|
+
});
|
|
522
|
+
next.onRequest("listSuggestionModels", async () => {
|
|
523
|
+
if (!sessionCtx) throw new Error("session not yet started");
|
|
524
|
+
const flagValue = pi.getFlag(SUGGESTION_FLAG);
|
|
525
|
+
const cliOverride = (typeof flagValue === "string" && flagValue) ? flagValue : undefined;
|
|
526
|
+
return {
|
|
527
|
+
cliOverride,
|
|
528
|
+
currentModel: sessionCtx.model ? `${sessionCtx.model.provider}/${sessionCtx.model.id}` : undefined,
|
|
529
|
+
models: sessionCtx.modelRegistry.getAvailable().map((model) => ({
|
|
530
|
+
provider: model.provider,
|
|
531
|
+
id: model.id,
|
|
532
|
+
name: model.name,
|
|
533
|
+
model: `${model.provider}/${model.id}`,
|
|
534
|
+
})),
|
|
535
|
+
};
|
|
536
|
+
});
|
|
537
|
+
next.onClose = () => {
|
|
538
|
+
if (client === next) {
|
|
539
|
+
client = null;
|
|
540
|
+
resetState();
|
|
541
|
+
inFlightSuggestions = 0;
|
|
542
|
+
stopSpinner();
|
|
543
|
+
renderWidget();
|
|
544
|
+
ui?.notify("IDE disconnected", "warning");
|
|
545
|
+
}
|
|
546
|
+
};
|
|
547
|
+
try {
|
|
548
|
+
await next.connect();
|
|
549
|
+
} catch (err) {
|
|
550
|
+
ctx.ui.notify(`Failed to connect: ${err instanceof Error ? err.message : String(err)}`, "error");
|
|
551
|
+
return null;
|
|
552
|
+
}
|
|
553
|
+
client = next;
|
|
554
|
+
setStickyLockfile(lockfile);
|
|
555
|
+
renderWidget();
|
|
556
|
+
return next;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* Auto-connect to an IDE.
|
|
561
|
+
*
|
|
562
|
+
* When `preferSticky` is true (session replacement flow), tries the sticky
|
|
563
|
+
* IDE from the previous session first, bypassing the single-candidate rule.
|
|
564
|
+
* Falls back to the cold-start rule when sticky is absent or stale.
|
|
565
|
+
*
|
|
566
|
+
* When `preferSticky` is false (cold startup), connects only when exactly one
|
|
567
|
+
* valid IDE candidate exists for the cwd.
|
|
568
|
+
*/
|
|
569
|
+
async function autoConnect(ctx: ExtensionContext, pi: ExtensionAPI, options: { preferSticky: boolean }): Promise<void> {
|
|
570
|
+
if (client?.isConnected()) return;
|
|
571
|
+
if (!isAutoconnectEnabled(ctx.cwd)) return;
|
|
572
|
+
|
|
573
|
+
let candidates = await findCandidateLockfiles(ctx.cwd);
|
|
574
|
+
|
|
575
|
+
if (options.preferSticky) {
|
|
576
|
+
const sticky = getStickyLockfile();
|
|
577
|
+
if (sticky) {
|
|
578
|
+
const candidate = candidates.find((c) => sameLockfile(c, sticky));
|
|
579
|
+
if (candidate) {
|
|
580
|
+
const connected = await connectToLockfile(candidate, ctx, pi);
|
|
581
|
+
if (connected) {
|
|
582
|
+
ctx.ui.notify(`Reconnected to ${candidate.ideName}`, "info");
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
// Treat a failed sticky reconnect as stale. Do not retry the
|
|
586
|
+
// same lockfile in the cold-start fallback below.
|
|
587
|
+
clearStickyLockfile();
|
|
588
|
+
candidates = candidates.filter((c) => !sameLockfile(c, candidate));
|
|
589
|
+
} else {
|
|
590
|
+
// Sticky IDE is gone; clear it and fall through to cold start logic.
|
|
591
|
+
clearStickyLockfile();
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
if (candidates.length !== 1) return;
|
|
597
|
+
const lockfile = candidates[0];
|
|
598
|
+
const connected = await connectToLockfile(lockfile, ctx, pi);
|
|
599
|
+
if (connected) {
|
|
600
|
+
ctx.ui.notify(`Auto-connected to ${lockfile.ideName}`, "info");
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
|
|
438
604
|
export default function (pi: ExtensionAPI) {
|
|
439
605
|
pi.registerFlag(SUGGESTION_FLAG, {
|
|
440
606
|
type: "string",
|
|
@@ -445,8 +611,14 @@ export default function (pi: ExtensionAPI) {
|
|
|
445
611
|
description: "Path to append raw inline suggestion debug logs.",
|
|
446
612
|
});
|
|
447
613
|
|
|
448
|
-
pi.on("session_start", (
|
|
614
|
+
pi.on("session_start", async (event, ctx) => {
|
|
449
615
|
sessionCtx = ctx;
|
|
616
|
+
if (event.reason === "startup") {
|
|
617
|
+
await autoConnect(ctx, pi, { preferSticky: false });
|
|
618
|
+
}
|
|
619
|
+
if (event.reason === "new" || event.reason === "resume" || event.reason === "reload") {
|
|
620
|
+
await autoConnect(ctx, pi, { preferSticky: true });
|
|
621
|
+
}
|
|
450
622
|
});
|
|
451
623
|
|
|
452
624
|
pi.registerCommand("ide", {
|
|
@@ -480,55 +652,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
480
652
|
return;
|
|
481
653
|
}
|
|
482
654
|
|
|
483
|
-
|
|
484
|
-
next.onNotification = onNotification;
|
|
485
|
-
next.onRequest("getSuggestions", async (params, signal) => {
|
|
486
|
-
inFlightSuggestions++;
|
|
487
|
-
if (inFlightSuggestions === 1) startSpinner();
|
|
488
|
-
renderWidget();
|
|
489
|
-
try {
|
|
490
|
-
const suggestions = await generateSuggestions(pi, (params ?? {}) as SuggestionParams, signal);
|
|
491
|
-
return { suggestions };
|
|
492
|
-
} finally {
|
|
493
|
-
inFlightSuggestions--;
|
|
494
|
-
if (inFlightSuggestions === 0) stopSpinner();
|
|
495
|
-
renderWidget();
|
|
496
|
-
}
|
|
497
|
-
});
|
|
498
|
-
next.onRequest("listSuggestionModels", async () => {
|
|
499
|
-
if (!sessionCtx) throw new Error("session not yet started");
|
|
500
|
-
const flagValue = pi.getFlag(SUGGESTION_FLAG);
|
|
501
|
-
const cliOverride = (typeof flagValue === "string" && flagValue) ? flagValue : undefined;
|
|
502
|
-
return {
|
|
503
|
-
cliOverride,
|
|
504
|
-
currentModel: sessionCtx.model ? `${sessionCtx.model.provider}/${sessionCtx.model.id}` : undefined,
|
|
505
|
-
models: sessionCtx.modelRegistry.getAvailable().map((model) => ({
|
|
506
|
-
provider: model.provider,
|
|
507
|
-
id: model.id,
|
|
508
|
-
name: model.name,
|
|
509
|
-
model: `${model.provider}/${model.id}`,
|
|
510
|
-
})),
|
|
511
|
-
};
|
|
512
|
-
});
|
|
513
|
-
next.onClose = () => {
|
|
514
|
-
if (client === next) {
|
|
515
|
-
client = null;
|
|
516
|
-
resetState();
|
|
517
|
-
inFlightSuggestions = 0;
|
|
518
|
-
stopSpinner();
|
|
519
|
-
renderWidget();
|
|
520
|
-
ctx.ui.notify("IDE disconnected", "warning");
|
|
521
|
-
}
|
|
522
|
-
};
|
|
523
|
-
try {
|
|
524
|
-
await next.connect();
|
|
525
|
-
} catch (err) {
|
|
526
|
-
ctx.ui.notify(`Failed to connect: ${err instanceof Error ? err.message : String(err)}`, "error");
|
|
527
|
-
return;
|
|
528
|
-
}
|
|
529
|
-
client = next;
|
|
530
|
-
ui = ctx.ui;
|
|
531
|
-
renderWidget();
|
|
655
|
+
await connectToLockfile(pick.lockfile, ctx, pi);
|
|
532
656
|
},
|
|
533
657
|
});
|
|
534
658
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ldelossa/pi-ide",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Pi extension that connects to a running editor (Neovim today, more later) over a local WebSocket. Adds ambient editor context to the agent and routes write/edit tool calls through interactive diffs in the editor.",
|
|
5
5
|
"keywords": ["pi-package", "pi", "ide", "neovim"],
|
|
6
6
|
"files": [
|