@pi-unipi/utility 2.6.0 → 2.6.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.
- package/package.json +8 -6
- package/src/commands.ts +4 -5
- package/src/herdr-sync.ts +136 -0
- package/src/index.ts +11 -68
- package/src/lifecycle/cleanup.ts +2 -16
- package/src/{diff/settings.ts → settings.ts} +42 -80
- package/src/tools/env.ts +0 -0
- package/src/tui/name-badge-state.ts +18 -2
- package/src/tui/name-badge.ts +5 -4
- package/src/tui/util-settings-tui.ts +41 -224
- package/src/cache/ttl-cache.ts +0 -311
- package/src/diff/highlighter.ts +0 -352
- package/src/diff/parser.ts +0 -191
- package/src/diff/renderer.ts +0 -395
- package/src/diff/theme.ts +0 -316
- package/src/diff/wrapper.ts +0 -339
- package/src/display/capabilities.ts +0 -214
- package/src/display/width.ts +0 -226
- package/src/tools/batch.ts +0 -229
- package/src/tui/badge-settings-tui.ts +0 -388
- package/src/tui/badge-settings.ts +0 -67
- package/src/tui/settings-inspector.ts +0 -303
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @pi-unipi/utility — Terminal Capabilities Detection
|
|
3
|
-
*
|
|
4
|
-
* Detect terminal features for optimal rendering:
|
|
5
|
-
* - Color support (basic, 256, truecolor)
|
|
6
|
-
* - Nerd Font detection
|
|
7
|
-
* - Unicode support
|
|
8
|
-
* - Terminal dimensions
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import type { TerminalCapabilities } from "../types.js";
|
|
12
|
-
|
|
13
|
-
/** Cached capabilities per process */
|
|
14
|
-
let cachedCapabilities: TerminalCapabilities | null = null;
|
|
15
|
-
let cacheTimestamp = 0;
|
|
16
|
-
const CACHE_TTL_MS = 5000; // Re-detect every 5s
|
|
17
|
-
|
|
18
|
-
/** Detect color support level */
|
|
19
|
-
function detectColorSupport(): { color: boolean; truecolor: boolean } {
|
|
20
|
-
const env = process.env;
|
|
21
|
-
|
|
22
|
-
// No color
|
|
23
|
-
if (env.NO_COLOR || env.NODE_DISABLE_COLORS) {
|
|
24
|
-
return { color: false, truecolor: false };
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// Force color
|
|
28
|
-
if (env.FORCE_COLOR) {
|
|
29
|
-
const level = parseInt(env.FORCE_COLOR, 10);
|
|
30
|
-
return {
|
|
31
|
-
color: level >= 1,
|
|
32
|
-
truecolor: level >= 3,
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// CI environments typically support colors
|
|
37
|
-
if (env.CI) {
|
|
38
|
-
return { color: true, truecolor: false };
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// Terminal emulator detection
|
|
42
|
-
const term = env.TERM || "";
|
|
43
|
-
const termProgram = env.TERM_PROGRAM || "";
|
|
44
|
-
|
|
45
|
-
// Apple Terminal.app does NOT support 24-bit truecolor (longstanding
|
|
46
|
-
// limitation as of macOS 26). Force 256-colour even if some wrapper
|
|
47
|
-
// leaked COLORTERM=truecolor into the environment.
|
|
48
|
-
const isAppleTerminal = termProgram === "Apple_Terminal";
|
|
49
|
-
|
|
50
|
-
// Truecolor TERM_PROGRAM allow-list. Note: terminfo names like
|
|
51
|
-
// "xterm-256color" / "screen-256color" / "tmux-256color" advertise
|
|
52
|
-
// 256-colour, NOT truecolor — they must not appear here.
|
|
53
|
-
const truecolorTermPrograms = [
|
|
54
|
-
"iTerm.app",
|
|
55
|
-
"WezTerm",
|
|
56
|
-
"Alacritty",
|
|
57
|
-
"vscode",
|
|
58
|
-
"Hyper",
|
|
59
|
-
"Warp",
|
|
60
|
-
"ghostty",
|
|
61
|
-
"Ghostty",
|
|
62
|
-
"zed",
|
|
63
|
-
"Zed",
|
|
64
|
-
"cursor",
|
|
65
|
-
"Cursor",
|
|
66
|
-
];
|
|
67
|
-
|
|
68
|
-
// Truecolor TERM tokens (the rare TERM that *does* advertise truecolor).
|
|
69
|
-
const truecolorTermTokens = ["truecolor", "24bit", "alacritty", "kitty", "wezterm"];
|
|
70
|
-
|
|
71
|
-
const hasTruecolor = isAppleTerminal
|
|
72
|
-
? false
|
|
73
|
-
: env.COLORTERM === "truecolor" ||
|
|
74
|
-
env.COLORTERM === "24bit" ||
|
|
75
|
-
truecolorTermPrograms.includes(termProgram) ||
|
|
76
|
-
truecolorTermTokens.some((t) => term.includes(t));
|
|
77
|
-
|
|
78
|
-
// Basic color support
|
|
79
|
-
const hasColor =
|
|
80
|
-
hasTruecolor ||
|
|
81
|
-
term.includes("color") ||
|
|
82
|
-
term.includes("ansi") ||
|
|
83
|
-
term.includes("xterm") ||
|
|
84
|
-
term.includes("screen") ||
|
|
85
|
-
term.includes("tmux") ||
|
|
86
|
-
termProgram.length > 0;
|
|
87
|
-
|
|
88
|
-
return { color: hasColor, truecolor: hasTruecolor };
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/** Detect Nerd Font support */
|
|
92
|
-
function detectNerdFont(): boolean {
|
|
93
|
-
const env = process.env;
|
|
94
|
-
|
|
95
|
-
// Explicit override
|
|
96
|
-
if (env.NERD_FONT === "1" || env.NERD_FONT === "true") {
|
|
97
|
-
return true;
|
|
98
|
-
}
|
|
99
|
-
if (env.NERD_FONT === "0" || env.NERD_FONT === "false") {
|
|
100
|
-
return false;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// Terminal emulator hints
|
|
104
|
-
const termProgram = env.TERM_PROGRAM || "";
|
|
105
|
-
const knownNerdFontTerminals = [
|
|
106
|
-
"iTerm.app",
|
|
107
|
-
"WezTerm",
|
|
108
|
-
"Alacritty",
|
|
109
|
-
"Kitty",
|
|
110
|
-
"Ghostty",
|
|
111
|
-
"Warp",
|
|
112
|
-
];
|
|
113
|
-
|
|
114
|
-
if (knownNerdFontTerminals.some((t) => termProgram.includes(t))) {
|
|
115
|
-
return true;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// Default to false for safety
|
|
119
|
-
return false;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/** Detect Unicode support level */
|
|
123
|
-
function detectUnicode(): "none" | "basic" | "full" {
|
|
124
|
-
const env = process.env;
|
|
125
|
-
|
|
126
|
-
// Explicit override
|
|
127
|
-
if (env.UNICODE === "0" || env.NO_UNICODE) {
|
|
128
|
-
return "none";
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// LANG/LC_ALL hints
|
|
132
|
-
const locale = env.LANG || env.LC_ALL || env.LC_CTYPE || "";
|
|
133
|
-
if (locale.includes("UTF-8") || locale.includes("utf8")) {
|
|
134
|
-
return "full";
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// Windows CMD typically has limited Unicode
|
|
138
|
-
if (process.platform === "win32" && !env.WT_SESSION) {
|
|
139
|
-
return "basic";
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// Default to basic (safe middle ground)
|
|
143
|
-
return "basic";
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/** Get terminal dimensions */
|
|
147
|
-
function getTerminalSize(): { width: number; height: number } {
|
|
148
|
-
const stdout = process.stdout;
|
|
149
|
-
if (stdout && stdout.isTTY) {
|
|
150
|
-
const cols = stdout.columns || 80;
|
|
151
|
-
const rows = stdout.rows || 24;
|
|
152
|
-
return { width: cols, height: rows };
|
|
153
|
-
}
|
|
154
|
-
return { width: 80, height: 24 };
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* Detect terminal capabilities.
|
|
159
|
-
* Results are cached for CACHE_TTL_MS to avoid repeated detection.
|
|
160
|
-
*/
|
|
161
|
-
export function detectCapabilities(): TerminalCapabilities {
|
|
162
|
-
const now = Date.now();
|
|
163
|
-
if (cachedCapabilities && now - cacheTimestamp < CACHE_TTL_MS) {
|
|
164
|
-
// Update dimensions even when cached (they change on resize)
|
|
165
|
-
const size = getTerminalSize();
|
|
166
|
-
return {
|
|
167
|
-
...cachedCapabilities,
|
|
168
|
-
width: size.width,
|
|
169
|
-
height: size.height,
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
const colorSupport = detectColorSupport();
|
|
174
|
-
const size = getTerminalSize();
|
|
175
|
-
|
|
176
|
-
cachedCapabilities = {
|
|
177
|
-
color: colorSupport.color,
|
|
178
|
-
truecolor: colorSupport.truecolor,
|
|
179
|
-
nerdFont: detectNerdFont(),
|
|
180
|
-
unicode: detectUnicode(),
|
|
181
|
-
width: size.width,
|
|
182
|
-
height: size.height,
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
cacheTimestamp = now;
|
|
186
|
-
return cachedCapabilities;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/** Force re-detection of capabilities */
|
|
190
|
-
export function refreshCapabilities(): TerminalCapabilities {
|
|
191
|
-
cachedCapabilities = null;
|
|
192
|
-
cacheTimestamp = 0;
|
|
193
|
-
return detectCapabilities();
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/** Check if a specific capability is available */
|
|
197
|
-
export function hasCapability(
|
|
198
|
-
cap: keyof TerminalCapabilities,
|
|
199
|
-
): boolean {
|
|
200
|
-
const caps = detectCapabilities();
|
|
201
|
-
const value = caps[cap];
|
|
202
|
-
if (typeof value === "boolean") {
|
|
203
|
-
return value;
|
|
204
|
-
}
|
|
205
|
-
if (typeof value === "string") {
|
|
206
|
-
return value !== "none";
|
|
207
|
-
}
|
|
208
|
-
return false;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/** Get safe icon based on Nerd Font availability */
|
|
212
|
-
export function getIcon(nerdFont: string, fallback: string): string {
|
|
213
|
-
return detectCapabilities().nerdFont ? nerdFont : fallback;
|
|
214
|
-
}
|
package/src/display/width.ts
DELETED
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @pi-unipi/utility — Width Management Utilities
|
|
3
|
-
*
|
|
4
|
-
* Safe width clamping, line wrapping, and line collapsing.
|
|
5
|
-
* Handles ANSI escape sequences correctly.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import type { WidthOptions } from "../types.js";
|
|
9
|
-
|
|
10
|
-
/** ANSI escape sequence regex */
|
|
11
|
-
const ANSI_REGEX =
|
|
12
|
-
/\u001b\[[\d;]*[a-zA-Z]|\u001b\][^\u0007]*\u0007|\u001b\[[\d;]*[\u0020-\u002f]*[\u0030-\u007e]/g;
|
|
13
|
-
|
|
14
|
-
/** Strip ANSI escape sequences from text */
|
|
15
|
-
export function stripAnsi(text: string): string {
|
|
16
|
-
return text.replace(ANSI_REGEX, "");
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/** Get visual width of text (excluding ANSI codes) */
|
|
20
|
-
export function visualWidth(text: string): number {
|
|
21
|
-
return stripAnsi(text).length;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/** Default width options */
|
|
25
|
-
const DEFAULT_WIDTH_OPTS: Required<WidthOptions> = {
|
|
26
|
-
ellipsis: "…",
|
|
27
|
-
breakWords: false,
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Clamp text to maxWidth visual characters.
|
|
32
|
-
* Preserves ANSI sequences at the end of truncated text.
|
|
33
|
-
*/
|
|
34
|
-
export function clampWidth(
|
|
35
|
-
text: string,
|
|
36
|
-
maxWidth: number,
|
|
37
|
-
options: WidthOptions = {},
|
|
38
|
-
): string {
|
|
39
|
-
const opts = { ...DEFAULT_WIDTH_OPTS, ...options };
|
|
40
|
-
const plain = stripAnsi(text);
|
|
41
|
-
|
|
42
|
-
if (plain.length <= maxWidth) {
|
|
43
|
-
return text;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Need to truncate while preserving ANSI
|
|
47
|
-
const ellipsisWidth = visualWidth(opts.ellipsis);
|
|
48
|
-
const targetWidth = maxWidth - ellipsisWidth;
|
|
49
|
-
|
|
50
|
-
if (targetWidth <= 0) {
|
|
51
|
-
return opts.ellipsis.slice(0, maxWidth);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// Walk through text, tracking ANSI state
|
|
55
|
-
let visualCount = 0;
|
|
56
|
-
let result = "";
|
|
57
|
-
let inAnsi = false;
|
|
58
|
-
let ansiBuffer = "";
|
|
59
|
-
|
|
60
|
-
for (const char of text) {
|
|
61
|
-
if (char === "\u001b") {
|
|
62
|
-
inAnsi = true;
|
|
63
|
-
ansiBuffer = char;
|
|
64
|
-
continue;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (inAnsi) {
|
|
68
|
-
ansiBuffer += char;
|
|
69
|
-
// Check if ANSI sequence is complete
|
|
70
|
-
if (/[a-zA-Z\u0007]/.test(char) || (ansiBuffer.startsWith("\u001b]") && char === "\u0007")) {
|
|
71
|
-
inAnsi = false;
|
|
72
|
-
result += ansiBuffer;
|
|
73
|
-
ansiBuffer = "";
|
|
74
|
-
}
|
|
75
|
-
continue;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (visualCount < targetWidth) {
|
|
79
|
-
result += char;
|
|
80
|
-
visualCount++;
|
|
81
|
-
} else {
|
|
82
|
-
break;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// Add any pending ANSI sequences and reset
|
|
87
|
-
if (ansiBuffer) {
|
|
88
|
-
result += ansiBuffer;
|
|
89
|
-
}
|
|
90
|
-
result += "\u001b[0m"; // Reset ANSI
|
|
91
|
-
result += opts.ellipsis;
|
|
92
|
-
|
|
93
|
-
return result;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Wrap text into lines of maxWidth visual characters.
|
|
98
|
-
* Respects word boundaries unless breakWords is true.
|
|
99
|
-
*/
|
|
100
|
-
export function wrapLines(
|
|
101
|
-
text: string,
|
|
102
|
-
maxWidth: number,
|
|
103
|
-
options: WidthOptions = {},
|
|
104
|
-
): string[] {
|
|
105
|
-
const opts = { ...DEFAULT_WIDTH_OPTS, ...options };
|
|
106
|
-
const lines: string[] = [];
|
|
107
|
-
const paragraphs = text.split("\n");
|
|
108
|
-
|
|
109
|
-
for (const paragraph of paragraphs) {
|
|
110
|
-
if (visualWidth(paragraph) <= maxWidth) {
|
|
111
|
-
lines.push(paragraph);
|
|
112
|
-
continue;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const words = paragraph.split(/(\s+)/);
|
|
116
|
-
let currentLine = "";
|
|
117
|
-
let currentWidth = 0;
|
|
118
|
-
|
|
119
|
-
for (const word of words) {
|
|
120
|
-
const wordWidth = visualWidth(word);
|
|
121
|
-
|
|
122
|
-
if (wordWidth === 0) {
|
|
123
|
-
// Whitespace-only word
|
|
124
|
-
currentLine += word;
|
|
125
|
-
continue;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (currentWidth + wordWidth > maxWidth) {
|
|
129
|
-
if (currentLine) {
|
|
130
|
-
lines.push(currentLine);
|
|
131
|
-
currentLine = "";
|
|
132
|
-
currentWidth = 0;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// Word itself might be longer than maxWidth
|
|
136
|
-
if (!opts.breakWords && wordWidth > maxWidth) {
|
|
137
|
-
// Break the long word
|
|
138
|
-
let remaining = word;
|
|
139
|
-
while (visualWidth(remaining) > maxWidth) {
|
|
140
|
-
let chunk = "";
|
|
141
|
-
let chunkWidth = 0;
|
|
142
|
-
for (const char of remaining) {
|
|
143
|
-
const charWidth = visualWidth(char);
|
|
144
|
-
if (chunkWidth + charWidth > maxWidth) {
|
|
145
|
-
break;
|
|
146
|
-
}
|
|
147
|
-
chunk += char;
|
|
148
|
-
chunkWidth += charWidth;
|
|
149
|
-
}
|
|
150
|
-
lines.push(chunk);
|
|
151
|
-
remaining = remaining.slice(chunk.length);
|
|
152
|
-
}
|
|
153
|
-
if (remaining) {
|
|
154
|
-
currentLine = remaining;
|
|
155
|
-
currentWidth = visualWidth(remaining);
|
|
156
|
-
}
|
|
157
|
-
} else {
|
|
158
|
-
currentLine = word;
|
|
159
|
-
currentWidth = wordWidth;
|
|
160
|
-
}
|
|
161
|
-
} else {
|
|
162
|
-
currentLine += word;
|
|
163
|
-
currentWidth += wordWidth;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (currentLine) {
|
|
168
|
-
lines.push(currentLine);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return lines;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Collapse consecutive empty lines down to maxEmpty.
|
|
177
|
-
*/
|
|
178
|
-
export function collapseLines(
|
|
179
|
-
lines: string[],
|
|
180
|
-
maxEmpty: number = 1,
|
|
181
|
-
): string[] {
|
|
182
|
-
const result: string[] = [];
|
|
183
|
-
let emptyCount = 0;
|
|
184
|
-
|
|
185
|
-
for (const line of lines) {
|
|
186
|
-
const isEmpty = stripAnsi(line).trim().length === 0;
|
|
187
|
-
|
|
188
|
-
if (isEmpty) {
|
|
189
|
-
emptyCount++;
|
|
190
|
-
if (emptyCount <= maxEmpty) {
|
|
191
|
-
result.push(line);
|
|
192
|
-
}
|
|
193
|
-
} else {
|
|
194
|
-
emptyCount = 0;
|
|
195
|
-
result.push(line);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
return result;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Pad text to target width with spaces.
|
|
204
|
-
* Respects ANSI sequences.
|
|
205
|
-
*/
|
|
206
|
-
export function padWidth(text: string, targetWidth: number): string {
|
|
207
|
-
const currentWidth = visualWidth(text);
|
|
208
|
-
if (currentWidth >= targetWidth) {
|
|
209
|
-
return text;
|
|
210
|
-
}
|
|
211
|
-
return text + " ".repeat(targetWidth - currentWidth);
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* Center text within target width.
|
|
216
|
-
*/
|
|
217
|
-
export function centerWidth(text: string, targetWidth: number): string {
|
|
218
|
-
const currentWidth = visualWidth(text);
|
|
219
|
-
if (currentWidth >= targetWidth) {
|
|
220
|
-
return text;
|
|
221
|
-
}
|
|
222
|
-
const padding = targetWidth - currentWidth;
|
|
223
|
-
const left = Math.floor(padding / 2);
|
|
224
|
-
const right = padding - left;
|
|
225
|
-
return " ".repeat(left) + text + " ".repeat(right);
|
|
226
|
-
}
|
package/src/tools/batch.ts
DELETED
|
@@ -1,229 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @pi-unipi/utility — Batch Execution Tool
|
|
3
|
-
*
|
|
4
|
-
* Atomic batch of commands + searches with rollback on failure.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import type {
|
|
8
|
-
BatchCommand,
|
|
9
|
-
BatchOptions,
|
|
10
|
-
BatchResult,
|
|
11
|
-
BatchReport,
|
|
12
|
-
} from "../types.js";
|
|
13
|
-
|
|
14
|
-
/** Default options */
|
|
15
|
-
const DEFAULTS: Required<BatchOptions> = {
|
|
16
|
-
failFast: true,
|
|
17
|
-
commandTimeoutMs: 30000,
|
|
18
|
-
totalTimeoutMs: 300000,
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
/** Executor function type — provided by the host environment */
|
|
22
|
-
export type CommandExecutor = (
|
|
23
|
-
command: BatchCommand,
|
|
24
|
-
) => Promise<unknown>;
|
|
25
|
-
|
|
26
|
-
/** Rollback function type */
|
|
27
|
-
export type RollbackFn = (
|
|
28
|
-
results: BatchResult[],
|
|
29
|
-
) => Promise<void>;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Execute a batch of commands atomically.
|
|
33
|
-
*
|
|
34
|
-
* @param commands - Array of commands to execute
|
|
35
|
-
* @param executor - Function that executes a single command
|
|
36
|
-
* @param options - Batch execution options
|
|
37
|
-
* @param rollback - Optional rollback function called on failure
|
|
38
|
-
*/
|
|
39
|
-
export async function executeBatch(
|
|
40
|
-
commands: BatchCommand[],
|
|
41
|
-
executor: CommandExecutor,
|
|
42
|
-
options: BatchOptions = {},
|
|
43
|
-
rollback?: RollbackFn,
|
|
44
|
-
): Promise<BatchReport> {
|
|
45
|
-
const opts = { ...DEFAULTS, ...options };
|
|
46
|
-
const results: BatchResult[] = [];
|
|
47
|
-
const startTime = Date.now();
|
|
48
|
-
|
|
49
|
-
// Total timeout guard
|
|
50
|
-
const totalDeadline = startTime + opts.totalTimeoutMs;
|
|
51
|
-
|
|
52
|
-
for (let i = 0; i < commands.length; i++) {
|
|
53
|
-
const command = commands[i];
|
|
54
|
-
const cmdStart = Date.now();
|
|
55
|
-
|
|
56
|
-
// Check total timeout
|
|
57
|
-
if (Date.now() > totalDeadline) {
|
|
58
|
-
const timeoutResult: BatchResult = {
|
|
59
|
-
command,
|
|
60
|
-
success: false,
|
|
61
|
-
error: `Total batch timeout exceeded (${opts.totalTimeoutMs}ms)`,
|
|
62
|
-
durationMs: Date.now() - cmdStart,
|
|
63
|
-
};
|
|
64
|
-
results.push(timeoutResult);
|
|
65
|
-
|
|
66
|
-
if (opts.failFast) {
|
|
67
|
-
const report = createReport(results, startTime, !!rollback);
|
|
68
|
-
if (rollback) {
|
|
69
|
-
await rollback(results).catch(() => {
|
|
70
|
-
// Best-effort rollback
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
return report;
|
|
74
|
-
}
|
|
75
|
-
continue;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// Execute with per-command timeout
|
|
79
|
-
try {
|
|
80
|
-
const result = await withTimeout(
|
|
81
|
-
executor(command),
|
|
82
|
-
opts.commandTimeoutMs,
|
|
83
|
-
`Command timeout exceeded (${opts.commandTimeoutMs}ms)`,
|
|
84
|
-
);
|
|
85
|
-
|
|
86
|
-
results.push({
|
|
87
|
-
command,
|
|
88
|
-
success: true,
|
|
89
|
-
result,
|
|
90
|
-
durationMs: Date.now() - cmdStart,
|
|
91
|
-
});
|
|
92
|
-
} catch (err) {
|
|
93
|
-
const errorResult: BatchResult = {
|
|
94
|
-
command,
|
|
95
|
-
success: false,
|
|
96
|
-
error: (err as Error).message,
|
|
97
|
-
durationMs: Date.now() - cmdStart,
|
|
98
|
-
};
|
|
99
|
-
results.push(errorResult);
|
|
100
|
-
|
|
101
|
-
if (opts.failFast) {
|
|
102
|
-
const report = createReport(results, startTime, !!rollback);
|
|
103
|
-
if (rollback) {
|
|
104
|
-
await rollback(results).catch(() => {
|
|
105
|
-
// Best-effort rollback
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
return report;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return createReport(results, startTime, false);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/** Create a batch report from results */
|
|
117
|
-
function createReport(
|
|
118
|
-
results: BatchResult[],
|
|
119
|
-
startTime: number,
|
|
120
|
-
rolledBack: boolean,
|
|
121
|
-
): BatchReport {
|
|
122
|
-
const allSuccess = results.every((r) => r.success);
|
|
123
|
-
return {
|
|
124
|
-
success: allSuccess && !rolledBack,
|
|
125
|
-
results,
|
|
126
|
-
totalDurationMs: Date.now() - startTime,
|
|
127
|
-
rolledBack,
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/** Wrap a promise with a timeout */
|
|
132
|
-
function withTimeout<T>(
|
|
133
|
-
promise: Promise<T>,
|
|
134
|
-
timeoutMs: number,
|
|
135
|
-
message: string,
|
|
136
|
-
): Promise<T> {
|
|
137
|
-
return new Promise((resolve, reject) => {
|
|
138
|
-
const timer = setTimeout(() => {
|
|
139
|
-
reject(new Error(message));
|
|
140
|
-
}, timeoutMs);
|
|
141
|
-
|
|
142
|
-
promise
|
|
143
|
-
.then((value) => {
|
|
144
|
-
clearTimeout(timer);
|
|
145
|
-
resolve(value);
|
|
146
|
-
})
|
|
147
|
-
.catch((err) => {
|
|
148
|
-
clearTimeout(timer);
|
|
149
|
-
reject(err);
|
|
150
|
-
});
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/** Format a batch report as markdown */
|
|
155
|
-
export function formatBatchReport(report: BatchReport): string {
|
|
156
|
-
const lines = [
|
|
157
|
-
"## 📦 Batch Execution Report",
|
|
158
|
-
"",
|
|
159
|
-
`**Success:** ${report.success ? "✓ Yes" : "✗ No"}`,
|
|
160
|
-
`**Commands:** ${report.results.length}`,
|
|
161
|
-
`**Duration:** ${report.totalDurationMs}ms`,
|
|
162
|
-
report.rolledBack ? "**Rolled back:** Yes" : "",
|
|
163
|
-
"",
|
|
164
|
-
].filter(Boolean);
|
|
165
|
-
|
|
166
|
-
for (let i = 0; i < report.results.length; i++) {
|
|
167
|
-
const r = report.results[i];
|
|
168
|
-
const icon = r.success ? "✓" : "✗";
|
|
169
|
-
lines.push(
|
|
170
|
-
`### ${i + 1}. ${icon} ${r.command.type}:${r.command.name}`,
|
|
171
|
-
`**Duration:** ${r.durationMs}ms`,
|
|
172
|
-
);
|
|
173
|
-
if (r.success) {
|
|
174
|
-
lines.push(`**Result:** \`${JSON.stringify(r.result).slice(0, 200)}\``);
|
|
175
|
-
} else {
|
|
176
|
-
lines.push(`**Error:** ${r.error}`);
|
|
177
|
-
}
|
|
178
|
-
lines.push("");
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
return lines.join("\n");
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/** Create a simple command batch builder */
|
|
185
|
-
export class BatchBuilder {
|
|
186
|
-
private commands: BatchCommand[] = [];
|
|
187
|
-
private opts: BatchOptions = {};
|
|
188
|
-
private rollbackFn?: RollbackFn;
|
|
189
|
-
|
|
190
|
-
/** Add a command to the batch */
|
|
191
|
-
addCommand(name: string, args?: Record<string, unknown>): this {
|
|
192
|
-
this.commands.push({ type: "command", name, args });
|
|
193
|
-
return this;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/** Add a tool call to the batch */
|
|
197
|
-
addTool(name: string, args?: Record<string, unknown>): this {
|
|
198
|
-
this.commands.push({ type: "tool", name, args });
|
|
199
|
-
return this;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/** Add a search to the batch */
|
|
203
|
-
addSearch(name: string, args?: Record<string, unknown>): this {
|
|
204
|
-
this.commands.push({ type: "search", name, args });
|
|
205
|
-
return this;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/** Set batch options */
|
|
209
|
-
withOptions(options: BatchOptions): this {
|
|
210
|
-
this.opts = { ...this.opts, ...options };
|
|
211
|
-
return this;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/** Set rollback function */
|
|
215
|
-
withRollback(rollback: RollbackFn): this {
|
|
216
|
-
this.rollbackFn = rollback;
|
|
217
|
-
return this;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/** Execute the batch */
|
|
221
|
-
async execute(executor: CommandExecutor): Promise<BatchReport> {
|
|
222
|
-
return executeBatch(this.commands, executor, this.opts, this.rollbackFn);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/** Get the command list */
|
|
226
|
-
getCommands(): readonly BatchCommand[] {
|
|
227
|
-
return this.commands;
|
|
228
|
-
}
|
|
229
|
-
}
|