@xynogen/pix-core 0.1.6 → 0.1.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xynogen/pix-core",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "Pi extension — core UI/UX bundle (welcome banner, footer, model picker, self-update)",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -24,6 +24,7 @@ import {
24
24
  visibleWidth,
25
25
  } from "@earendil-works/pi-tui";
26
26
  import { lookupBenchmark, lookupModelsDev } from "../../lib/data";
27
+ import { patchOutBuiltinModelCommand } from "./patch-builtin";
27
28
 
28
29
  // ─── Pure logic (exported for tests) ─────────────────────────────────────────
29
30
 
@@ -352,6 +353,10 @@ export async function showEnhancedPicker(
352
353
  }
353
354
 
354
355
  export default function modelPickerExtension(pi: ExtensionAPI) {
356
+ // Remove Pi's built-in /model so only the enhanced /models picker remains.
357
+ // Self-healing: re-applies on every load, so a Pi upgrade can't restore it.
358
+ patchOutBuiltinModelCommand();
359
+
355
360
  const handler = async (_args: unknown, ctx: ExtensionContext) => {
356
361
  await showEnhancedPicker(pi, ctx);
357
362
  };
@@ -0,0 +1,66 @@
1
+ import { describe, expect, it } from "bun:test";
2
+ import { mkdtempSync, readFileSync, writeFileSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+
6
+ // Pure replacement tested in isolation (the exported fn resolves the host
7
+ // package, which isn't present in the test sandbox).
8
+ const MODEL_COMMAND_LINE =
9
+ '{ name: "model", description: "Select model (opens selector UI)" },';
10
+
11
+ function escapeRegExp(text: string): string {
12
+ return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
13
+ }
14
+
15
+ function patchSource(source: string): string {
16
+ if (!source.includes(MODEL_COMMAND_LINE)) return source;
17
+ return source.replace(
18
+ new RegExp(`[ \\t]*${escapeRegExp(MODEL_COMMAND_LINE)}\\n?`),
19
+ "",
20
+ );
21
+ }
22
+
23
+ const UNPATCHED = `export const BUILTIN_SLASH_COMMANDS = [
24
+ { name: "settings", description: "Open settings menu" },
25
+ { name: "model", description: "Select model (opens selector UI)" },
26
+ { name: "login", description: "Configure provider authentication" },
27
+ ];
28
+ `;
29
+
30
+ describe("patch-builtin /model removal", () => {
31
+ it("removes the built-in /model line and keeps neighbors", () => {
32
+ const out = patchSource(UNPATCHED);
33
+ expect(out).not.toContain('name: "model"');
34
+ expect(out).toContain('name: "settings"');
35
+ expect(out).toContain('name: "login"');
36
+ });
37
+
38
+ it("is idempotent — second pass is a no-op", () => {
39
+ const once = patchSource(UNPATCHED);
40
+ const twice = patchSource(once);
41
+ expect(twice).toBe(once);
42
+ });
43
+
44
+ it("leaves an already-clean file untouched", () => {
45
+ const clean = `export const X = [\n { name: "login" },\n];\n`;
46
+ expect(patchSource(clean)).toBe(clean);
47
+ });
48
+
49
+ it("does not strip the plural /models entry", () => {
50
+ const withPlural = `[
51
+ { name: "models", description: "Enhanced picker" },
52
+ { name: "model", description: "Select model (opens selector UI)" },
53
+ ]`;
54
+ const out = patchSource(withPlural);
55
+ expect(out).toContain('name: "models"');
56
+ expect(out).not.toContain('{ name: "model", description');
57
+ });
58
+
59
+ it("round-trips through disk", () => {
60
+ const dir = mkdtempSync(join(tmpdir(), "pix-patch-"));
61
+ const file = join(dir, "slash-commands.js");
62
+ writeFileSync(file, UNPATCHED, "utf8");
63
+ writeFileSync(file, patchSource(readFileSync(file, "utf8")), "utf8");
64
+ expect(readFileSync(file, "utf8")).not.toContain('name: "model"');
65
+ });
66
+ });
@@ -0,0 +1,60 @@
1
+ /**
2
+ * patch-builtin.ts — strip Pi's built-in /model slash command at load time.
3
+ *
4
+ * Built-in commands can't be removed via the extension API, so we edit Pi's
5
+ * compiled slash-commands.js directly. Done on every load: idempotent and
6
+ * self-healing across Pi upgrades, so no manual repatch is ever needed.
7
+ */
8
+
9
+ import { readFileSync, writeFileSync } from "node:fs";
10
+ import { createRequire } from "node:module";
11
+ import { dirname, resolve } from "node:path";
12
+
13
+ const HOST_PACKAGE = "@earendil-works/pi-coding-agent";
14
+ const MODEL_COMMAND_LINE =
15
+ '{ name: "model", description: "Select model (opens selector UI)" },';
16
+
17
+ /** Locate the host's compiled slash-commands.js, or null if it can't be found. */
18
+ function findSlashCommandsFile(): string | null {
19
+ try {
20
+ const require = createRequire(import.meta.url);
21
+ const entry = require.resolve(HOST_PACKAGE);
22
+ return resolve(dirname(entry), "core", "slash-commands.js");
23
+ } catch {
24
+ return null;
25
+ }
26
+ }
27
+
28
+ /**
29
+ * Remove the built-in /model command line from Pi's slash-commands.js.
30
+ * Idempotent: returns silently if the file is missing or already patched.
31
+ */
32
+ export function patchOutBuiltinModelCommand(): void {
33
+ const file = findSlashCommandsFile();
34
+ if (!file) return;
35
+
36
+ let source: string;
37
+ try {
38
+ source = readFileSync(file, "utf8");
39
+ } catch {
40
+ return; // file not present (different Pi layout) — nothing to do
41
+ }
42
+
43
+ if (!source.includes(MODEL_COMMAND_LINE)) return; // already patched
44
+
45
+ const patched = source.replace(
46
+ new RegExp(`[ \\t]*${escapeRegExp(MODEL_COMMAND_LINE)}\\n?`),
47
+ "",
48
+ );
49
+ if (patched === source) return;
50
+
51
+ try {
52
+ writeFileSync(file, patched, "utf8");
53
+ } catch {
54
+ // Read-only install — leave /model in place rather than crash.
55
+ }
56
+ }
57
+
58
+ function escapeRegExp(text: string): string {
59
+ return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
60
+ }