@ldelossa/pi-ide 0.2.1 → 0.2.2

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 (2) hide show
  1. package/index.ts +92 -58
  2. package/package.json +1 -1
package/index.ts CHANGED
@@ -414,14 +414,7 @@ async function pickLockfile(
414
414
  ui: { select: (t: string, o: string[]) => Promise<string | undefined> },
415
415
  current?: Lockfile,
416
416
  ): Promise<PickResult> {
417
- const all = await listLockfiles();
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
- }
417
+ const candidates = await findCandidateLockfiles(cwd);
425
418
  if (candidates.length === 0) return { kind: "none" };
426
419
  const labels = candidates.map((c) => {
427
420
  const folder = c.workspaceFolders[0] ?? "?";
@@ -435,6 +428,92 @@ async function pickLockfile(
435
428
  return lockfile ? { kind: "selected", lockfile } : { kind: "cancelled" };
436
429
  }
437
430
 
431
+ /**
432
+ * Find lockfiles that match the given cwd, have a live PID, and are listening.
433
+ */
434
+ async function findCandidateLockfiles(cwd: string): Promise<Lockfile[]> {
435
+ const all = await listLockfiles();
436
+ const candidates: Lockfile[] = [];
437
+ for (const lf of all) {
438
+ if (!matchesCwd(lf, cwd)) continue;
439
+ if (!isPidAlive(lf.pid)) continue;
440
+ if (!(await isPortListening(lf.port))) continue;
441
+ candidates.push(lf);
442
+ }
443
+ return candidates;
444
+ }
445
+
446
+ /**
447
+ * Create an IdeClient, wire up handlers, and connect.
448
+ */
449
+ async function connectToLockfile(lockfile: Lockfile, ctx: ExtensionContext, pi: ExtensionAPI): Promise<IdeClient | null> {
450
+ const next = new IdeClient(lockfile);
451
+ next.onNotification = onNotification;
452
+ next.onRequest("getSuggestions", async (params, signal) => {
453
+ inFlightSuggestions++;
454
+ if (inFlightSuggestions === 1) startSpinner();
455
+ renderWidget();
456
+ try {
457
+ const suggestions = await generateSuggestions(pi, (params ?? {}) as SuggestionParams, signal);
458
+ return { suggestions };
459
+ } finally {
460
+ inFlightSuggestions--;
461
+ if (inFlightSuggestions === 0) stopSpinner();
462
+ renderWidget();
463
+ }
464
+ });
465
+ next.onRequest("listSuggestionModels", async () => {
466
+ if (!sessionCtx) throw new Error("session not yet started");
467
+ const flagValue = pi.getFlag(SUGGESTION_FLAG);
468
+ const cliOverride = (typeof flagValue === "string" && flagValue) ? flagValue : undefined;
469
+ return {
470
+ cliOverride,
471
+ currentModel: sessionCtx.model ? `${sessionCtx.model.provider}/${sessionCtx.model.id}` : undefined,
472
+ models: sessionCtx.modelRegistry.getAvailable().map((model) => ({
473
+ provider: model.provider,
474
+ id: model.id,
475
+ name: model.name,
476
+ model: `${model.provider}/${model.id}`,
477
+ })),
478
+ };
479
+ });
480
+ next.onClose = () => {
481
+ if (client === next) {
482
+ client = null;
483
+ resetState();
484
+ inFlightSuggestions = 0;
485
+ stopSpinner();
486
+ renderWidget();
487
+ ui?.notify("IDE disconnected", "warning");
488
+ }
489
+ };
490
+ try {
491
+ await next.connect();
492
+ } catch (err) {
493
+ ui?.notify(`Failed to connect: ${err instanceof Error ? err.message : String(err)}`, "error");
494
+ return null;
495
+ }
496
+ client = next;
497
+ ui = ctx.ui;
498
+ renderWidget();
499
+ return next;
500
+ }
501
+
502
+ /**
503
+ * Auto-connect to the single matching IDE for this cwd, if exactly one valid
504
+ * lockfile exists. Does nothing when zero or multiple candidates are found.
505
+ */
506
+ async function autoConnect(ctx: ExtensionContext, pi: ExtensionAPI): Promise<void> {
507
+ if (client?.isConnected()) return;
508
+ const candidates = await findCandidateLockfiles(ctx.cwd);
509
+ if (candidates.length !== 1) return;
510
+ const lockfile = candidates[0];
511
+ const connected = await connectToLockfile(lockfile, ctx, pi);
512
+ if (connected) {
513
+ ui?.notify(`Auto-connected to ${lockfile.ideName}`, "info");
514
+ }
515
+ }
516
+
438
517
  export default function (pi: ExtensionAPI) {
439
518
  pi.registerFlag(SUGGESTION_FLAG, {
440
519
  type: "string",
@@ -445,8 +524,11 @@ export default function (pi: ExtensionAPI) {
445
524
  description: "Path to append raw inline suggestion debug logs.",
446
525
  });
447
526
 
448
- pi.on("session_start", (_event, ctx) => {
527
+ pi.on("session_start", async (event, ctx) => {
449
528
  sessionCtx = ctx;
529
+ if (event.reason === "new" || event.reason === "resume" || event.reason === "reload") {
530
+ await autoConnect(ctx, pi);
531
+ }
450
532
  });
451
533
 
452
534
  pi.registerCommand("ide", {
@@ -480,55 +562,7 @@ export default function (pi: ExtensionAPI) {
480
562
  return;
481
563
  }
482
564
 
483
- const next = new IdeClient(pick.lockfile);
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();
565
+ await connectToLockfile(pick.lockfile, ctx, pi);
532
566
  },
533
567
  });
534
568
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ldelossa/pi-ide",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
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": [