@mrclrchtr/supi-rtk 0.1.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.
@@ -0,0 +1,226 @@
1
+ // Generic settings overlay for SuPi extensions.
2
+ //
3
+ // Uses pi-tui's SettingsList with scope toggle (Tab), extension grouping,
4
+ // and search. Each extension declares its settings via registerSettings().
5
+
6
+ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
7
+ import { getSettingsListTheme } from "@earendil-works/pi-coding-agent";
8
+ import {
9
+ Container,
10
+ Input,
11
+ Key,
12
+ matchesKey,
13
+ type SettingItem,
14
+ SettingsList,
15
+ Text,
16
+ } from "@earendil-works/pi-tui";
17
+ import {
18
+ getRegisteredSettings,
19
+ type SettingsScope,
20
+ type SettingsSection,
21
+ } from "./settings-registry.ts";
22
+
23
+ // ── Input submenu component ──────────────────────────────────
24
+
25
+ /**
26
+ * Creates a pi-tui Input-backed submenu component with enter-to-confirm
27
+ * and escape-to-cancel handling.
28
+ *
29
+ * @param currentValue - Initial value for the text input.
30
+ * @param label - Label text displayed above the input.
31
+ * @param done - Callback invoked with the confirmed value, or undefined on cancel.
32
+ */
33
+ export function createInputSubmenu(
34
+ currentValue: string,
35
+ label: string,
36
+ done: (selectedValue?: string) => void,
37
+ ): {
38
+ render: (width: number) => string[];
39
+ invalidate: () => void;
40
+ handleInput: (data: string) => boolean;
41
+ } {
42
+ const input = new Input();
43
+ input.setValue(currentValue);
44
+
45
+ return {
46
+ render: (_width: number) => {
47
+ const lines = [` ${label}`];
48
+ lines.push(...input.render(_width));
49
+ lines.push(" enter confirm • esc cancel");
50
+ return lines;
51
+ },
52
+ invalidate: () => {
53
+ input.invalidate();
54
+ },
55
+ handleInput: (data: string) => {
56
+ if (matchesKey(data, Key.escape)) {
57
+ done();
58
+ return true;
59
+ }
60
+ if (matchesKey(data, Key.enter)) {
61
+ done(input.getValue());
62
+ return true;
63
+ }
64
+ input.handleInput(data);
65
+ return true;
66
+ },
67
+ };
68
+ }
69
+
70
+ // ── Types ────────────────────────────────────────────────────
71
+
72
+ interface OverlayState {
73
+ scope: SettingsScope;
74
+ cwd: string;
75
+ }
76
+
77
+ // ── Pure helpers ─────────────────────────────────────────────
78
+
79
+ function getScopeLabel(scope: SettingsScope): string {
80
+ return scope === "project" ? "Project" : "Global";
81
+ }
82
+
83
+ function buildFlatItems(
84
+ sections: SettingsSection[],
85
+ scope: SettingsScope,
86
+ cwd: string,
87
+ ): SettingItem[] {
88
+ const items: SettingItem[] = [];
89
+ for (const section of sections) {
90
+ const sectionItems = section.loadValues(scope, cwd);
91
+ for (const item of sectionItems) {
92
+ items.push({
93
+ ...item,
94
+ id: `${section.id}.${item.id}`,
95
+ label: `${section.label}: ${item.label}`,
96
+ });
97
+ }
98
+ }
99
+ return items;
100
+ }
101
+
102
+ function findSectionAndId(
103
+ sections: SettingsSection[],
104
+ flatId: string,
105
+ ): { section: SettingsSection; itemId: string } | null {
106
+ const dotIndex = flatId.indexOf(".");
107
+ if (dotIndex === -1) return null;
108
+ const sectionId = flatId.slice(0, dotIndex);
109
+ const itemId = flatId.slice(dotIndex + 1);
110
+ const section = sections.find((s) => s.id === sectionId);
111
+ if (!section) return null;
112
+ return { section, itemId };
113
+ }
114
+
115
+ // ── Component ────────────────────────────────────────────────
116
+
117
+ interface SettingsOverlayDeps {
118
+ state: OverlayState;
119
+ container: Container;
120
+ settingsList: SettingsList | null;
121
+ tui: Parameters<Parameters<ExtensionContext["ui"]["custom"]>[0]>[0];
122
+ theme: Parameters<Parameters<ExtensionContext["ui"]["custom"]>[0]>[1];
123
+ done: () => void;
124
+ }
125
+
126
+ function createSettingsList(deps: SettingsOverlayDeps): SettingsList {
127
+ const sections = getRegisteredSettings();
128
+ const items = buildFlatItems(sections, deps.state.scope, deps.state.cwd);
129
+ const onChange = (flatId: string, newValue: string) => {
130
+ const found = findSectionAndId(sections, flatId);
131
+ if (found) {
132
+ found.section.persistChange(deps.state.scope, deps.state.cwd, found.itemId, newValue);
133
+ }
134
+ // Re-read all values to reflect persisted changes, but keep the list
135
+ // instance (and its selectedIndex) intact.
136
+ const updatedItems = buildFlatItems(sections, deps.state.scope, deps.state.cwd);
137
+ for (const updated of updatedItems) {
138
+ const existing = items.find((i) => i.id === updated.id);
139
+ if (existing && existing.currentValue !== updated.currentValue) {
140
+ settingsList.updateValue(updated.id, updated.currentValue);
141
+ }
142
+ }
143
+ deps.tui.requestRender();
144
+ };
145
+ const settingsList = new SettingsList(
146
+ items,
147
+ Math.min(items.length + 4, 20),
148
+ getSettingsListTheme(),
149
+ onChange,
150
+ () => deps.done(),
151
+ { enableSearch: true },
152
+ );
153
+ return settingsList;
154
+ }
155
+
156
+ function rebuildSettingsList(deps: SettingsOverlayDeps): SettingsList {
157
+ const settingsList = createSettingsList(deps);
158
+ deps.settingsList = settingsList;
159
+
160
+ deps.container.clear();
161
+ deps.container.addChild(createHeaderComponent(deps));
162
+ deps.container.addChild(settingsList);
163
+
164
+ return settingsList;
165
+ }
166
+
167
+ function createHeaderComponent(deps: SettingsOverlayDeps): Text {
168
+ const { theme, state } = deps;
169
+ const scopeLabel = getScopeLabel(state.scope);
170
+ const otherScope = state.scope === "project" ? "Global" : "Project";
171
+ const headerText = new Text(
172
+ `${theme.fg("accent", theme.bold("SuPi Settings"))} ${theme.fg("text", `Scope: ${scopeLabel}`)} ${theme.fg("dim", `(tab → ${otherScope})`)}`,
173
+ 0,
174
+ 0,
175
+ );
176
+ return headerText;
177
+ }
178
+
179
+ function handleScopeToggle(deps: SettingsOverlayDeps): void {
180
+ deps.state.scope = deps.state.scope === "project" ? "global" : "project";
181
+ rebuildSettingsList(deps);
182
+ deps.tui.requestRender();
183
+ }
184
+
185
+ // ── Entry point ──────────────────────────────────────────────
186
+
187
+ export function openSettingsOverlay(ctx: ExtensionContext): void {
188
+ const sections = getRegisteredSettings();
189
+ if (sections.length === 0) {
190
+ ctx.ui.notify("No settings registered by SuPi extensions", "info");
191
+ return;
192
+ }
193
+
194
+ void ctx.ui.custom<void>((tui, theme, _kb, done) => {
195
+ const state: OverlayState = { scope: "project", cwd: ctx.cwd };
196
+ const container = new Container();
197
+
198
+ const deps: SettingsOverlayDeps = {
199
+ state,
200
+ container,
201
+ settingsList: null,
202
+ tui,
203
+ theme,
204
+ done,
205
+ };
206
+
207
+ rebuildSettingsList(deps);
208
+
209
+ const component = {
210
+ render: (width: number) => container.render(width),
211
+ invalidate: () => container.invalidate(),
212
+ handleInput: (data: string) => {
213
+ if (matchesKey(data, Key.tab)) {
214
+ handleScopeToggle(deps);
215
+ return true;
216
+ }
217
+ // Delegate input to the settings list (always set after rebuildSettingsList)
218
+ deps.settingsList?.handleInput?.(data);
219
+ deps.tui.requestRender();
220
+ return true;
221
+ },
222
+ };
223
+
224
+ return component;
225
+ });
226
+ }
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Shared terminal title formatting and signaling utilities.
3
+ *
4
+ * Centralized place for pi title convention (π prefix), completion (✓)
5
+ * and waiting (●) indicators, and the audible terminal bell.
6
+ */
7
+ import path from "node:path";
8
+
9
+ /** Unicode checkmark shown when the agent finishes a turn. */
10
+ export const DONE_SYMBOL = "\u2713";
11
+ /** Unicode dot shown when waiting for user input. */
12
+ export const WAITING_SYMBOL = "\u25CF";
13
+
14
+ /** Minimal UI surface needed for title operations. */
15
+ export interface TitleTarget {
16
+ ui: {
17
+ setTitle?(title: string): void;
18
+ };
19
+ }
20
+
21
+ /**
22
+ * Format pi's canonical terminal title from session name and cwd.
23
+ * Falls back gracefully when either is missing.
24
+ *
25
+ * @example
26
+ * formatTitle("my-session", "/home/projects/foo") // "π - my-session - foo"
27
+ * formatTitle(undefined, "/home/projects/foo") // "π - foo"
28
+ * formatTitle("my-session") // "π - my-session"
29
+ * formatTitle() // "π"
30
+ */
31
+ export function formatTitle(sessionName?: string, cwd?: string): string {
32
+ const base = cwd ? path.basename(cwd) : undefined;
33
+ if (sessionName && base) return `π - ${sessionName} - ${base}`;
34
+ if (sessionName) return `π - ${sessionName}`;
35
+ if (base) return `π - ${base}`;
36
+ return "π";
37
+ }
38
+
39
+ /** Sound the audible terminal bell (ASCII BEL). */
40
+ export function signalBell(): void {
41
+ process.stdout.write("\x07");
42
+ }
43
+
44
+ /**
45
+ * Set the terminal title to indicate the agent is waiting for user input.
46
+ * Prefixes with ● and sounds the terminal bell.
47
+ */
48
+ export function signalWaiting(ctx: TitleTarget, title: string): void {
49
+ ctx.ui.setTitle?.(`${WAITING_SYMBOL} ${title}`);
50
+ signalBell();
51
+ }
52
+
53
+ /**
54
+ * Set the terminal title to indicate the agent turn has completed.
55
+ * Prefixes with ✓ and sounds the terminal bell.
56
+ */
57
+ export function signalDone(ctx: TitleTarget, title: string): void {
58
+ ctx.ui.setTitle?.(`${DONE_SYMBOL} ${title}`);
59
+ signalBell();
60
+ }
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@mrclrchtr/supi-rtk",
3
+ "version": "0.1.0",
4
+ "description": "SuPi RTK extension — transparent bash command rewriting via RTK for token savings",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/mrclrchtr/supi.git"
9
+ },
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "keywords": [
14
+ "pi-package",
15
+ "pi",
16
+ "pi-coding-agent"
17
+ ],
18
+ "files": [
19
+ "src/**/*.ts",
20
+ "README.md"
21
+ ],
22
+ "dependencies": {
23
+ "@mrclrchtr/supi-core": "workspace:*"
24
+ },
25
+ "bundledDependencies": [
26
+ "@mrclrchtr/supi-core"
27
+ ],
28
+ "peerDependencies": {
29
+ "@earendil-works/pi-coding-agent": "*"
30
+ },
31
+ "devDependencies": {
32
+ "vitest": "^4.1.4",
33
+ "@mrclrchtr/supi-test-utils": "workspace:*"
34
+ },
35
+ "pi": {
36
+ "extensions": [
37
+ "./src/rtk.ts"
38
+ ]
39
+ },
40
+ "main": "src/index.ts"
41
+ }
package/src/guards.ts ADDED
@@ -0,0 +1,74 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+
4
+ // rtk-ai/rtk#665, rtk-ai/rtk#1489 — upstream biome → rtk lint routing gap
5
+ const BIOME_RE = /biome(?:\s|$)/;
6
+ // rtk-ai/rtk#1367, rtk-ai/rtk#1604 — rg → grep rewrite is lossy (grep has no -g, -U, --glob, etc.)
7
+ const RG_RE = /^rg(?:\s|$)/;
8
+ const PACKAGE_LINT_RE = /^(?:pnpm|npm|yarn|bun)\s+(?:run\s+)?lint(?:\s|$)/;
9
+ const BIOME_CONFIG_FILES = ["biome.json", "biome.jsonc"];
10
+
11
+ /**
12
+ * Normalize a command string by stripping leading shell wrappers that
13
+ * would interfere with `^`-anchored guard regexes:
14
+ * - `cd /path && command`
15
+ * - `rtk command` (from a previous RTK rewrite pass)
16
+ */
17
+ function stripShellWrappers(command: string): string {
18
+ let s = command.trimStart();
19
+ // Strip leading `cd /some/path && ` or `cd /some/path;`
20
+ while (/^cd\s+\S+(?:\s*[;&]\s*|\s+&&\s+)/.test(s)) {
21
+ s = s.replace(/^cd\s+\S+(?:\s*[;&]\s*|\s+&&\s+)/, "");
22
+ }
23
+ // Strip leading `rtk ` prefix from a prior rewrite attempt
24
+ s = s.replace(/^rtk\s+/, "");
25
+ return s.trimStart();
26
+ }
27
+
28
+ /**
29
+ * Return whether SuPi should bypass RTK's rewrite registry for commands with
30
+ * known lossy rewrites. RTK 0.37.x collapses several Biome invocations into
31
+ * `rtk lint ...`, which can drop the `biome` subcommand and produce misleading
32
+ * lint-wrapper warnings. Passing these forms through preserves correctness while
33
+ * upstream rewrite routing is fixed.
34
+ *
35
+ * TODO: Remove this workaround after rtk-ai/rtk#665 and rtk-ai/rtk#1489 are
36
+ * closed and the `pnpm exec biome ...` routing gap is fixed upstream.
37
+ *
38
+ * `rg` (ripgrep) commands are also guarded: RTK rewrites `rg` to `rtk grep` or
39
+ * native `grep`, but doesn't translate ripgrep-specific flags (`-g`, `-U`,
40
+ * `--glob`, `--type`, etc.), producing broken commands like `grep -g '*.ts'`.
41
+ * See rtk-ai/rtk#1367 and rtk-ai/rtk#1604.
42
+ *
43
+ * TODO: Remove this workaround after RTK properly handles rg-native flags.
44
+ */
45
+ export function shouldBypassRtkRewrite(command: string, cwd: string): boolean {
46
+ const normalized = stripShellWrappers(command);
47
+ if (hasRtkDisabledPrefix(normalized)) return true;
48
+ if (BIOME_RE.test(normalized)) return true;
49
+ if (RG_RE.test(normalized)) return true;
50
+ if (PACKAGE_LINT_RE.test(normalized) && projectUsesBiome(cwd)) return true;
51
+ return false;
52
+ }
53
+
54
+ function hasRtkDisabledPrefix(command: string): boolean {
55
+ if (command.startsWith("RTK_DISABLED=1 ")) return true;
56
+ return command.startsWith("env RTK_DISABLED=1 ");
57
+ }
58
+
59
+ function projectUsesBiome(cwd: string): boolean {
60
+ if (BIOME_CONFIG_FILES.some((file) => existsSync(join(cwd, file)))) {
61
+ return true;
62
+ }
63
+
64
+ try {
65
+ const packageJson = JSON.parse(readFileSync(join(cwd, "package.json"), "utf-8")) as {
66
+ scripts?: Record<string, unknown>;
67
+ };
68
+ return Object.values(packageJson.scripts ?? {}).some(
69
+ (script) => typeof script === "string" && /(^|\s)biome(\s|$)/.test(script),
70
+ );
71
+ } catch {
72
+ return false;
73
+ }
74
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { default } from "./rtk.ts";
package/src/rewrite.ts ADDED
@@ -0,0 +1,113 @@
1
+ import { execFileSync } from "node:child_process";
2
+
3
+ export type RtkRewriteFailureReason =
4
+ | "timeout"
5
+ | "unavailable"
6
+ | "non-zero-exit"
7
+ | "empty-output"
8
+ | "error";
9
+
10
+ export type RtkRewriteResult =
11
+ | {
12
+ kind: "rewritten" | "unchanged";
13
+ command: string;
14
+ durationMs: number;
15
+ stdout: string;
16
+ stderr?: string;
17
+ }
18
+ | {
19
+ kind: "failed";
20
+ reason: RtkRewriteFailureReason;
21
+ durationMs: number;
22
+ stdout?: string;
23
+ stderr?: string;
24
+ exitCode?: number;
25
+ errorMessage?: string;
26
+ };
27
+
28
+ interface ExecErrorShape {
29
+ code?: string;
30
+ status?: number;
31
+ signal?: string;
32
+ stdout?: string | Buffer;
33
+ stderr?: string | Buffer;
34
+ message?: string;
35
+ }
36
+
37
+ function toText(value: string | Buffer | undefined): string | undefined {
38
+ return typeof value === "string" ? value : value?.toString("utf-8");
39
+ }
40
+
41
+ function classifyError(err: ExecErrorShape): RtkRewriteFailureReason {
42
+ const message = err.message?.toLowerCase() ?? "";
43
+ if (err.code === "ENOENT") return "unavailable";
44
+ if (err.code === "ETIMEDOUT" || message.includes("timeout") || message.includes("timed out")) {
45
+ return "timeout";
46
+ }
47
+ if (typeof err.status === "number") return "non-zero-exit";
48
+ return "error";
49
+ }
50
+
51
+ /**
52
+ * Rewrite a bash command through RTK's `rtk rewrite` CLI with structured diagnostics.
53
+ *
54
+ * Non-zero RTK exits can still emit a usable rewrite on stdout; those are treated
55
+ * as successful rewrites so callers preserve existing RTK behavior.
56
+ */
57
+ export function rtkRewriteDetailed(command: string, timeoutMs: number): RtkRewriteResult {
58
+ const started = Date.now();
59
+ try {
60
+ const stdout = execFileSync("rtk", ["rewrite", command], {
61
+ encoding: "utf-8",
62
+ timeout: timeoutMs,
63
+ });
64
+ const durationMs = Date.now() - started;
65
+ const rewritten = stdout.trim();
66
+ if (!rewritten) {
67
+ return { kind: "failed", reason: "empty-output", durationMs, stdout };
68
+ }
69
+ return {
70
+ kind: rewritten === command ? "unchanged" : "rewritten",
71
+ command: rewritten,
72
+ durationMs,
73
+ stdout,
74
+ };
75
+ } catch (error: unknown) {
76
+ const err = error as ExecErrorShape;
77
+ const stdout = toText(err.stdout);
78
+ const stderr = toText(err.stderr);
79
+ const durationMs = Date.now() - started;
80
+ const rewritten = stdout?.trim();
81
+ if (rewritten) {
82
+ return {
83
+ kind: rewritten === command ? "unchanged" : "rewritten",
84
+ command: rewritten,
85
+ durationMs,
86
+ stdout: stdout ?? "",
87
+ stderr,
88
+ };
89
+ }
90
+ return {
91
+ kind: "failed",
92
+ reason: classifyError(err),
93
+ durationMs,
94
+ stdout,
95
+ stderr,
96
+ exitCode: err.status,
97
+ errorMessage: err.message,
98
+ };
99
+ }
100
+ }
101
+
102
+ /**
103
+ * Rewrite a bash command through RTK's `rtk rewrite` CLI.
104
+ *
105
+ * @param command The original shell command.
106
+ * @param timeoutMs Timeout in milliseconds for the rewrite call.
107
+ * @returns The rewritten command string, or `undefined` if RTK could not rewrite it
108
+ * (timeout, binary missing, or non-zero exit without usable stdout).
109
+ */
110
+ export function rtkRewrite(command: string, timeoutMs: number): string | undefined {
111
+ const result = rtkRewriteDetailed(command, timeoutMs);
112
+ return result.kind === "failed" ? undefined : result.command;
113
+ }