@mrclrchtr/supi-bash-timeout 0.1.0 → 0.2.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mrclrchtr/supi-bash-timeout",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "SuPi bash-timeout extension — injects default timeout on bash tool calls",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -16,14 +16,28 @@
16
16
  "pi-coding-agent"
17
17
  ],
18
18
  "files": [
19
- "index.ts"
19
+ "src/**/*.ts",
20
+ "!__tests__"
21
+ ],
22
+ "dependencies": {
23
+ "@mrclrchtr/supi-core": "workspace:*"
24
+ },
25
+ "bundledDependencies": [
26
+ "@mrclrchtr/supi-core"
20
27
  ],
21
28
  "peerDependencies": {
22
- "@mariozechner/pi-coding-agent": "~0.66.0"
29
+ "@earendil-works/pi-coding-agent": "*",
30
+ "@earendil-works/pi-tui": "*"
31
+ },
32
+ "devDependencies": {
33
+ "@types/node": "25.6.2",
34
+ "vitest": "4.1.5",
35
+ "@mrclrchtr/supi-test-utils": "workspace:*"
23
36
  },
24
37
  "pi": {
25
38
  "extensions": [
26
- "./index.ts"
39
+ "./src/bash-timeout.ts"
27
40
  ]
28
- }
41
+ },
42
+ "main": "src/index.ts"
29
43
  }
@@ -0,0 +1,28 @@
1
+ // Bash Timeout — inject default timeouts on bash tool calls.
2
+ //
3
+ // The pi bash tool accepts an optional `timeout` parameter, but the LLM
4
+ // doesn't always specify one. In a headless gateway daemon with no human
5
+ // watching, a single hung command (e.g. `find /` over a huge filesystem)
6
+ // blocks all subsequent messages indefinitely.
7
+ //
8
+ // This extension intercepts every bash `tool_call` event and sets a default
9
+ // timeout when the LLM omits one. The timeout is configurable via
10
+ // /supi-settings or the SuPi config system (default 120s).
11
+
12
+ import { type ExtensionAPI, isToolCallEventType } from "@earendil-works/pi-coding-agent";
13
+ import { loadBashTimeoutConfig } from "./config.ts";
14
+ import { registerBashTimeoutSettings } from "./settings-registration.ts";
15
+
16
+ export default function bashTimeout(pi: ExtensionAPI) {
17
+ registerBashTimeoutSettings();
18
+
19
+ pi.on("tool_call", async (event, ctx) => {
20
+ if (!isToolCallEventType("bash", event)) return;
21
+
22
+ // Only inject when the LLM didn't specify a timeout
23
+ if (event.input.timeout !== undefined && event.input.timeout !== null) return;
24
+
25
+ const config = loadBashTimeoutConfig(ctx.cwd);
26
+ event.input.timeout = config.defaultTimeout;
27
+ });
28
+ }
package/src/config.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { loadSupiConfig } from "@mrclrchtr/supi-core";
2
+
3
+ export interface BashTimeoutConfig {
4
+ defaultTimeout: number;
5
+ }
6
+
7
+ export const BASH_TIMEOUT_DEFAULTS: BashTimeoutConfig = {
8
+ defaultTimeout: 120,
9
+ };
10
+
11
+ function isValidTimeout(value: unknown): value is number {
12
+ return typeof value === "number" && Number.isFinite(value) && value > 0;
13
+ }
14
+
15
+ export function loadBashTimeoutConfig(cwd: string, homeDir?: string): BashTimeoutConfig {
16
+ const raw = loadSupiConfig("bash-timeout", cwd, BASH_TIMEOUT_DEFAULTS, { homeDir });
17
+ return {
18
+ defaultTimeout: isValidTimeout(raw.defaultTimeout)
19
+ ? raw.defaultTimeout
20
+ : BASH_TIMEOUT_DEFAULTS.defaultTimeout,
21
+ };
22
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { default } from "./bash-timeout.ts";
@@ -0,0 +1,37 @@
1
+ import type { SettingItem } from "@earendil-works/pi-tui";
2
+ import { createInputSubmenu, registerConfigSettings } from "@mrclrchtr/supi-core";
3
+ import { BASH_TIMEOUT_DEFAULTS, type BashTimeoutConfig } from "./config.ts";
4
+
5
+ export function registerBashTimeoutSettings(): void {
6
+ registerConfigSettings({
7
+ id: "bash-timeout",
8
+ label: "Bash Timeout",
9
+ section: "bash-timeout",
10
+ defaults: BASH_TIMEOUT_DEFAULTS,
11
+ buildItems: (settings) => buildBashTimeoutSettingItems(settings),
12
+ // biome-ignore lint/complexity/useMaxParams: ConfigSettingsOptions interface callback
13
+ persistChange: (_scope, _cwd, settingId, value, helpers) => {
14
+ if (settingId === "defaultTimeout") {
15
+ const num = Number.parseInt(value, 10);
16
+ if (Number.isFinite(num) && num > 0) {
17
+ helpers.set("defaultTimeout", num);
18
+ } else {
19
+ helpers.unset("defaultTimeout");
20
+ }
21
+ }
22
+ },
23
+ });
24
+ }
25
+
26
+ function buildBashTimeoutSettingItems(settings: BashTimeoutConfig): SettingItem[] {
27
+ return [
28
+ {
29
+ id: "defaultTimeout",
30
+ label: "Default Timeout",
31
+ description: "Default timeout for bash tool calls in seconds",
32
+ currentValue: String(settings.defaultTimeout),
33
+ submenu: (currentValue, done) =>
34
+ createInputSubmenu(currentValue, "Timeout in seconds:", done),
35
+ },
36
+ ];
37
+ }
package/index.ts DELETED
@@ -1,39 +0,0 @@
1
- // Bash Timeout — inject default timeouts on bash tool calls.
2
- //
3
- // The pi bash tool accepts an optional `timeout` parameter, but the LLM
4
- // doesn't always specify one. In a headless gateway daemon with no human
5
- // watching, a single hung command (e.g. `find /` over a huge filesystem)
6
- // blocks all subsequent messages indefinitely.
7
- //
8
- // This extension intercepts every bash `tool_call` event and sets a default
9
- // timeout when the LLM omits one. The timeout is configurable via the
10
- // PI_BASH_DEFAULT_TIMEOUT env var (seconds, default 120).
11
- //
12
- // ADR-0049: Gateway TUI via WebSocket
13
-
14
- import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
15
-
16
- const DEFAULT_TIMEOUT_SECONDS = 120;
17
-
18
- function getDefaultTimeout(): number {
19
- const env = process.env.PI_BASH_DEFAULT_TIMEOUT;
20
- if (env) {
21
- const parsed = parseInt(env, 10);
22
- if (Number.isFinite(parsed) && parsed > 0) return parsed;
23
- }
24
- return DEFAULT_TIMEOUT_SECONDS;
25
- }
26
-
27
- export default function bashTimeout(pi: ExtensionAPI) {
28
- pi.on("tool_call", async (event) => {
29
- if (event.toolName !== "bash") return;
30
-
31
- const input = event.input as { command?: string; timeout?: number };
32
-
33
- // Only inject when the LLM didn't specify a timeout
34
- if (input.timeout !== undefined && input.timeout !== null) return;
35
-
36
- const defaultTimeout = getDefaultTimeout();
37
- input.timeout = defaultTimeout;
38
- });
39
- }