pi-interactive-shell 0.3.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.
package/config.ts ADDED
@@ -0,0 +1,132 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import { join } from "node:path";
4
+
5
+ export interface InteractiveShellConfig {
6
+ doubleEscapeThreshold: number;
7
+ exitAutoCloseDelay: number;
8
+ overlayWidthPercent: number;
9
+ overlayHeightPercent: number;
10
+ scrollbackLines: number;
11
+ ansiReemit: boolean;
12
+ handoffPreviewEnabled: boolean;
13
+ handoffPreviewLines: number;
14
+ handoffPreviewMaxChars: number;
15
+ handoffSnapshotEnabled: boolean;
16
+ handoffSnapshotLines: number;
17
+ handoffSnapshotMaxChars: number;
18
+ // Hands-free mode defaults
19
+ handsFreeUpdateMode: "on-quiet" | "interval";
20
+ handsFreeUpdateInterval: number;
21
+ handsFreeQuietThreshold: number;
22
+ handsFreeUpdateMaxChars: number;
23
+ handsFreeMaxTotalChars: number;
24
+ }
25
+
26
+ const DEFAULT_CONFIG: InteractiveShellConfig = {
27
+ doubleEscapeThreshold: 300,
28
+ exitAutoCloseDelay: 10,
29
+ overlayWidthPercent: 95,
30
+ overlayHeightPercent: 90,
31
+ scrollbackLines: 5000,
32
+ ansiReemit: true,
33
+ handoffPreviewEnabled: true,
34
+ handoffPreviewLines: 30,
35
+ handoffPreviewMaxChars: 2000,
36
+ handoffSnapshotEnabled: false,
37
+ handoffSnapshotLines: 200,
38
+ handoffSnapshotMaxChars: 12000,
39
+ // Hands-free mode defaults
40
+ handsFreeUpdateMode: "on-quiet" as const,
41
+ handsFreeUpdateInterval: 60000,
42
+ handsFreeQuietThreshold: 5000,
43
+ handsFreeUpdateMaxChars: 1500,
44
+ handsFreeMaxTotalChars: 100000,
45
+ };
46
+
47
+ export function loadConfig(cwd: string): InteractiveShellConfig {
48
+ const projectPath = join(cwd, ".pi", "interactive-shell.json");
49
+ const globalPath = join(homedir(), ".pi", "agent", "interactive-shell.json");
50
+
51
+ let globalConfig: Partial<InteractiveShellConfig> = {};
52
+ let projectConfig: Partial<InteractiveShellConfig> = {};
53
+
54
+ if (existsSync(globalPath)) {
55
+ try {
56
+ globalConfig = JSON.parse(readFileSync(globalPath, "utf-8"));
57
+ } catch (error) {
58
+ console.error(`Warning: Could not parse ${globalPath}: ${String(error)}`);
59
+ }
60
+ }
61
+
62
+ if (existsSync(projectPath)) {
63
+ try {
64
+ projectConfig = JSON.parse(readFileSync(projectPath, "utf-8"));
65
+ } catch (error) {
66
+ console.error(`Warning: Could not parse ${projectPath}: ${String(error)}`);
67
+ }
68
+ }
69
+
70
+ const merged = { ...DEFAULT_CONFIG, ...globalConfig, ...projectConfig };
71
+
72
+ return {
73
+ ...merged,
74
+ overlayWidthPercent: clampPercent(merged.overlayWidthPercent, DEFAULT_CONFIG.overlayWidthPercent),
75
+ overlayHeightPercent: clampPercent(merged.overlayHeightPercent, DEFAULT_CONFIG.overlayHeightPercent),
76
+ scrollbackLines: Math.max(200, merged.scrollbackLines ?? DEFAULT_CONFIG.scrollbackLines),
77
+ ansiReemit: merged.ansiReemit !== false,
78
+ handoffPreviewEnabled: merged.handoffPreviewEnabled !== false,
79
+ handoffPreviewLines: clampInt(merged.handoffPreviewLines, DEFAULT_CONFIG.handoffPreviewLines, 0, 500),
80
+ handoffPreviewMaxChars: clampInt(
81
+ merged.handoffPreviewMaxChars,
82
+ DEFAULT_CONFIG.handoffPreviewMaxChars,
83
+ 0,
84
+ 50000,
85
+ ),
86
+ handoffSnapshotEnabled: merged.handoffSnapshotEnabled === true,
87
+ handoffSnapshotLines: clampInt(merged.handoffSnapshotLines, DEFAULT_CONFIG.handoffSnapshotLines, 0, 5000),
88
+ handoffSnapshotMaxChars: clampInt(
89
+ merged.handoffSnapshotMaxChars,
90
+ DEFAULT_CONFIG.handoffSnapshotMaxChars,
91
+ 0,
92
+ 200000,
93
+ ),
94
+ // Hands-free mode
95
+ handsFreeUpdateMode: merged.handsFreeUpdateMode === "interval" ? "interval" : "on-quiet",
96
+ handsFreeUpdateInterval: clampInt(
97
+ merged.handsFreeUpdateInterval,
98
+ DEFAULT_CONFIG.handsFreeUpdateInterval,
99
+ 5000,
100
+ 300000,
101
+ ),
102
+ handsFreeQuietThreshold: clampInt(
103
+ merged.handsFreeQuietThreshold,
104
+ DEFAULT_CONFIG.handsFreeQuietThreshold,
105
+ 1000,
106
+ 30000,
107
+ ),
108
+ handsFreeUpdateMaxChars: clampInt(
109
+ merged.handsFreeUpdateMaxChars,
110
+ DEFAULT_CONFIG.handsFreeUpdateMaxChars,
111
+ 500,
112
+ 50000,
113
+ ),
114
+ handsFreeMaxTotalChars: clampInt(
115
+ merged.handsFreeMaxTotalChars,
116
+ DEFAULT_CONFIG.handsFreeMaxTotalChars,
117
+ 10000,
118
+ 1000000,
119
+ ),
120
+ };
121
+ }
122
+
123
+ function clampPercent(value: number | undefined, fallback: number): number {
124
+ if (typeof value !== "number" || Number.isNaN(value)) return fallback;
125
+ return Math.min(100, Math.max(10, value));
126
+ }
127
+
128
+ function clampInt(value: number | undefined, fallback: number, min: number, max: number): number {
129
+ if (typeof value !== "number" || Number.isNaN(value)) return fallback;
130
+ const rounded = Math.trunc(value);
131
+ return Math.min(max, Math.max(min, rounded));
132
+ }