ai-battery 0.1.19 → 0.1.21
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/LICENSE +21 -21
- package/README.md +369 -365
- package/bin/ai-battery-hud +0 -0
- package/bin/ai-battery-hud.js +0 -0
- package/bin/ai-battery-hud.ps1 +2 -2
- package/bin/ai-battery-run +80 -16
- package/bin/ai-battery-run-win.js +0 -0
- package/bin/ai-battery.js +0 -0
- package/bin/codex-wrapper-win.cjs +167 -167
- package/docs/RELEASING.md +64 -60
- package/docs/claude-statusline-preview.svg +37 -37
- package/docs/terminal-preview.svg +44 -44
- package/lib/cli.js +22 -10
- package/lib/platforms/process-scanner.js +7 -4
- package/lib/platforms/windows/codex-runner.js +62 -8
- package/lib/platforms/windows/hud/controls.ps1 +403 -403
- package/lib/platforms/windows/hud/dock.ps1 +731 -731
- package/lib/platforms/windows/hud/main.ps1 +1277 -1277
- package/lib/platforms/windows/hud/native.ps1 +169 -169
- package/lib/platforms/windows/hud/rendering.ps1 +382 -382
- package/lib/platforms/windows/hud/snapshot.ps1 +358 -358
- package/lib/platforms/windows/hud/windowing.ps1 +331 -331
- package/lib/platforms/windows/path-integration.js +14 -10
- package/package.json +75 -75
- package/scripts/windows-rowpty-smoke.ps1 +766 -766
- package/scripts/wsl-windows-rowpty-smoke.sh +0 -0
- package/vendor/rowpty/RowPty.cs +2668 -2668
- package/lib/README.md +0 -20
package/bin/ai-battery-hud
CHANGED
|
File without changes
|
package/bin/ai-battery-hud.js
CHANGED
|
File without changes
|
package/bin/ai-battery-hud.ps1
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
$implementation = Join-Path $PSScriptRoot "..\lib\platforms\windows\hud\main.ps1"
|
|
2
|
-
. $implementation @args
|
|
1
|
+
$implementation = Join-Path $PSScriptRoot "..\lib\platforms\windows\hud\main.ps1"
|
|
2
|
+
. $implementation @args
|
package/bin/ai-battery-run
CHANGED
|
@@ -40,6 +40,8 @@ def battery_command_base():
|
|
|
40
40
|
return [BATTERY_BIN]
|
|
41
41
|
DEFAULT_COLUMN_GUARD = 4
|
|
42
42
|
DEFAULT_LEFT_PADDING = 2
|
|
43
|
+
STATUS_FETCH_TIMEOUT = 10.0
|
|
44
|
+
STATUS_OUTPUT_LIMIT = 64 * 1024
|
|
43
45
|
|
|
44
46
|
|
|
45
47
|
def usage():
|
|
@@ -54,7 +56,7 @@ def usage():
|
|
|
54
56
|
|
|
55
57
|
|
|
56
58
|
def parse_args(argv):
|
|
57
|
-
interval = float(os.environ.get("AI_BATTERY_INTERVAL") or os.environ.get("CLAUDEX_BATTERY_INTERVAL", "
|
|
59
|
+
interval = float(os.environ.get("AI_BATTERY_INTERVAL") or os.environ.get("CLAUDEX_BATTERY_INTERVAL", "2"))
|
|
58
60
|
bar_width = os.environ.get("AI_BATTERY_BAR_WIDTH") or os.environ.get("CLAUDEX_BATTERY_BAR_WIDTH", "10")
|
|
59
61
|
provider = os.environ.get("AI_BATTERY_PROVIDER") or os.environ.get("CLAUDEX_BATTERY_PROVIDER", "auto")
|
|
60
62
|
layout = os.environ.get("AI_BATTERY_LAYOUT") or os.environ.get("CLAUDEX_BATTERY_LAYOUT", "auto")
|
|
@@ -181,6 +183,12 @@ class StatusLine:
|
|
|
181
183
|
self.last_draw = 0.0
|
|
182
184
|
self.text = "AI Battery starting..."
|
|
183
185
|
self.last_line = None
|
|
186
|
+
self.fetch_process = None
|
|
187
|
+
self.fetch_deadline = 0.0
|
|
188
|
+
self.fetch_output = bytearray()
|
|
189
|
+
self.has_reading = False
|
|
190
|
+
self.last_reading = ""
|
|
191
|
+
self.fetch_failures = 0
|
|
184
192
|
self.cols, self.rows = term_size()
|
|
185
193
|
|
|
186
194
|
def resize(self):
|
|
@@ -188,10 +196,42 @@ class StatusLine:
|
|
|
188
196
|
|
|
189
197
|
def refresh(self, force=False):
|
|
190
198
|
now = time.monotonic()
|
|
199
|
+
previous = self.text
|
|
200
|
+
if self.fetch_process is not None:
|
|
201
|
+
process = self.fetch_process
|
|
202
|
+
try:
|
|
203
|
+
code = process.poll()
|
|
204
|
+
while True:
|
|
205
|
+
try:
|
|
206
|
+
chunk = os.read(process.stdout.fileno(), 8192)
|
|
207
|
+
except BlockingIOError:
|
|
208
|
+
break
|
|
209
|
+
if not chunk:
|
|
210
|
+
break
|
|
211
|
+
self.fetch_output.extend(chunk)
|
|
212
|
+
if len(self.fetch_output) > STATUS_OUTPUT_LIMIT:
|
|
213
|
+
raise ValueError("status output exceeds limit")
|
|
214
|
+
if code is not None:
|
|
215
|
+
text = self.fetch_output.decode("utf-8", errors="replace").rstrip("\r\n")
|
|
216
|
+
self.close()
|
|
217
|
+
if code == 0 and text:
|
|
218
|
+
self.text = text
|
|
219
|
+
self.last_reading = text
|
|
220
|
+
self.has_reading = True
|
|
221
|
+
self.fetch_failures = 0
|
|
222
|
+
else:
|
|
223
|
+
self.fetch_failed()
|
|
224
|
+
elif now >= self.fetch_deadline:
|
|
225
|
+
self.close()
|
|
226
|
+
self.fetch_failed()
|
|
227
|
+
except (OSError, ValueError):
|
|
228
|
+
self.close()
|
|
229
|
+
self.fetch_failed()
|
|
230
|
+
return self.text != previous
|
|
231
|
+
|
|
191
232
|
if not force and now < self.next_fetch:
|
|
192
233
|
return False
|
|
193
234
|
self.next_fetch = now + self.interval
|
|
194
|
-
previous = self.text
|
|
195
235
|
max_width = max(20, self.cols - column_guard())
|
|
196
236
|
try:
|
|
197
237
|
battery_command = battery_command_base() + [
|
|
@@ -207,22 +247,49 @@ class StatusLine:
|
|
|
207
247
|
battery_command.extend(["--active-provider", self.active_provider])
|
|
208
248
|
if self.provider != "all":
|
|
209
249
|
battery_command.extend(["--provider", self.provider])
|
|
210
|
-
|
|
250
|
+
# Usage scans can include slow WSL/PowerShell process queries. Keep
|
|
251
|
+
# them off the terminal relay path so typing and output stay live.
|
|
252
|
+
self.fetch_process = subprocess.Popen(
|
|
211
253
|
battery_command,
|
|
254
|
+
stdin=subprocess.DEVNULL,
|
|
255
|
+
stdout=subprocess.PIPE,
|
|
212
256
|
stderr=subprocess.DEVNULL,
|
|
213
|
-
timeout=1.5,
|
|
214
257
|
)
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
except
|
|
219
|
-
self.
|
|
258
|
+
os.set_blocking(self.fetch_process.stdout.fileno(), False)
|
|
259
|
+
self.fetch_deadline = now + STATUS_FETCH_TIMEOUT
|
|
260
|
+
self.fetch_output = bytearray()
|
|
261
|
+
except (OSError, ValueError):
|
|
262
|
+
self.close()
|
|
263
|
+
self.fetch_failed()
|
|
220
264
|
return self.text != previous
|
|
221
265
|
|
|
266
|
+
def fetch_failed(self):
|
|
267
|
+
self.fetch_failures += 1
|
|
268
|
+
retry_delay = min(self.interval, 2 ** min(self.fetch_failures - 1, 4))
|
|
269
|
+
self.next_fetch = time.monotonic() + retry_delay
|
|
270
|
+
# Retain the reading, but make failed updates visible rather than
|
|
271
|
+
# presenting the old percentage as a successful current reading.
|
|
272
|
+
self.text = ("[stale] " + self.last_reading.lstrip()) if self.has_reading else "AI Battery unavailable"
|
|
273
|
+
|
|
274
|
+
def close(self):
|
|
275
|
+
process = self.fetch_process
|
|
276
|
+
if process is None:
|
|
277
|
+
return
|
|
278
|
+
self.fetch_process = None
|
|
279
|
+
if process.poll() is None:
|
|
280
|
+
try:
|
|
281
|
+
process.kill()
|
|
282
|
+
except ProcessLookupError:
|
|
283
|
+
pass
|
|
284
|
+
process.wait()
|
|
285
|
+
process.stdout.close()
|
|
286
|
+
|
|
222
287
|
def refresh_due(self):
|
|
223
|
-
return time.monotonic() >= self.next_fetch
|
|
288
|
+
return self.fetch_process is not None or time.monotonic() >= self.next_fetch
|
|
224
289
|
|
|
225
290
|
def seconds_until_refresh(self):
|
|
291
|
+
if self.fetch_process is not None:
|
|
292
|
+
return 0.05
|
|
226
293
|
return max(0.0, self.next_fetch - time.monotonic())
|
|
227
294
|
|
|
228
295
|
def _write_row(self, line):
|
|
@@ -239,9 +306,7 @@ class StatusLine:
|
|
|
239
306
|
os.write(sys.stdout.fileno(), payload.encode("utf-8", errors="replace"))
|
|
240
307
|
|
|
241
308
|
def draw_cached(self):
|
|
242
|
-
"""Repaint
|
|
243
|
-
Used on resize so the bar is repositioned instantly, avoiding the ~100ms
|
|
244
|
-
subprocess call that would otherwise delay the repaint."""
|
|
309
|
+
"""Repaint cached text at the new terminal size without starting a poll."""
|
|
245
310
|
paint_width = max(1, self.cols - 1)
|
|
246
311
|
line = fit_ansi(self.text, paint_width, pad=True)
|
|
247
312
|
self.last_draw = time.monotonic()
|
|
@@ -321,9 +386,7 @@ def main():
|
|
|
321
386
|
os.kill(pid, signal.SIGWINCH)
|
|
322
387
|
except ProcessLookupError:
|
|
323
388
|
pass
|
|
324
|
-
# Repaint immediately
|
|
325
|
-
# A subprocess call here blocks signal delivery for ~100ms, causing color
|
|
326
|
-
# flicker and stutter during resize. Schedule a fresh fetch instead.
|
|
389
|
+
# Repaint immediately, then schedule a fresh poll at the new width.
|
|
327
390
|
status.draw_cached()
|
|
328
391
|
status.next_fetch = 0.0
|
|
329
392
|
|
|
@@ -384,6 +447,7 @@ def main():
|
|
|
384
447
|
elif status.refresh_due():
|
|
385
448
|
status.draw(False)
|
|
386
449
|
finally:
|
|
450
|
+
status.close()
|
|
387
451
|
termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, old_term)
|
|
388
452
|
status.clear()
|
|
389
453
|
|
|
File without changes
|
package/bin/ai-battery.js
CHANGED
|
File without changes
|
|
@@ -1,167 +1,167 @@
|
|
|
1
|
-
// Shared PATH lookup and command launch for Windows. Generated bridges embed
|
|
2
|
-
// this file so a removed AI Battery package cannot pin Codex to an old install.
|
|
3
|
-
const fs = require("node:fs");
|
|
4
|
-
const path = require("node:path");
|
|
5
|
-
const { spawn } = require("node:child_process");
|
|
6
|
-
const UNSAFE_CMD_ARGUMENT_RE = /[\r\n&|<>^%!()]/;
|
|
7
|
-
const UNSAFE_CMD_PATH_RE = /[\r\n&|<>^%!]/;
|
|
8
|
-
|
|
9
|
-
function windowsCommandNames(command, env = process.env) {
|
|
10
|
-
const extensions = (env.PATHEXT || ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean);
|
|
11
|
-
return [...extensions.map((ext) => command + ext.toLowerCase()), command];
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function isManagedWrapper(file) {
|
|
15
|
-
let fd;
|
|
16
|
-
try {
|
|
17
|
-
fd = fs.openSync(file, "r");
|
|
18
|
-
const header = Buffer.alloc(512);
|
|
19
|
-
const count = fs.readSync(fd, header, 0, header.length, 0);
|
|
20
|
-
return header.toString("utf8", 0, count).includes("AI_BATTERY_MANAGED_CODEX_WRAPPER");
|
|
21
|
-
} catch {
|
|
22
|
-
return false;
|
|
23
|
-
} finally {
|
|
24
|
-
if (fd !== undefined) fs.closeSync(fd);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function findWindowsPathCommand(command, env = process.env, skipPaths = []) {
|
|
29
|
-
for (const entry of (env.PATH || "").split(path.delimiter)) {
|
|
30
|
-
const dir = entry.replace(/^"(.*)"$/, "$1");
|
|
31
|
-
for (const name of windowsCommandNames(command, env)) {
|
|
32
|
-
const candidate = path.resolve(dir, name);
|
|
33
|
-
try {
|
|
34
|
-
if (!fs.statSync(candidate).isFile()) continue;
|
|
35
|
-
if (skipPaths.some((entry) => {
|
|
36
|
-
try {
|
|
37
|
-
return fs.realpathSync(candidate).toLowerCase() === fs.realpathSync(entry).toLowerCase();
|
|
38
|
-
} catch {
|
|
39
|
-
return path.resolve(candidate).toLowerCase() === path.resolve(entry).toLowerCase();
|
|
40
|
-
}
|
|
41
|
-
})) continue;
|
|
42
|
-
if (command === "codex" && isManagedWrapper(candidate)) continue;
|
|
43
|
-
return candidate;
|
|
44
|
-
} catch {}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return null;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function normalizeWindowsCommandPath(value) {
|
|
51
|
-
let text = String(value || "").trim();
|
|
52
|
-
for (let i = 0; i < 6; i += 1) {
|
|
53
|
-
const before = text;
|
|
54
|
-
text = text.replace(/\\"/g, "\"").trim();
|
|
55
|
-
if (
|
|
56
|
-
(text.startsWith("\"") && text.endsWith("\""))
|
|
57
|
-
|| (text.startsWith("'") && text.endsWith("'"))
|
|
58
|
-
) {
|
|
59
|
-
text = text.slice(1, -1).trim();
|
|
60
|
-
}
|
|
61
|
-
if (text === before) break;
|
|
62
|
-
}
|
|
63
|
-
return text;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function resolveWindowsCommandFile(commandPath) {
|
|
67
|
-
commandPath = normalizeWindowsCommandPath(commandPath);
|
|
68
|
-
if (path.extname(commandPath)) return commandPath;
|
|
69
|
-
for (const suffix of [".cmd", ".exe", ".bat", ".ps1"]) {
|
|
70
|
-
const candidate = `${commandPath}${suffix}`;
|
|
71
|
-
if (fs.existsSync(candidate)) return candidate;
|
|
72
|
-
}
|
|
73
|
-
return commandPath;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function resolveNpmCmdShim(commandPath) {
|
|
77
|
-
if (!/\.cmd$/i.test(commandPath)) return null;
|
|
78
|
-
let source;
|
|
79
|
-
try {
|
|
80
|
-
source = fs.readFileSync(commandPath, "utf8");
|
|
81
|
-
} catch {
|
|
82
|
-
return null;
|
|
83
|
-
}
|
|
84
|
-
if (!source.includes("%dp0%") || !source.includes("%*")) return null;
|
|
85
|
-
const match = source.match(/%dp0%[\\/]([^"\r\n]+?\.(?:cjs|mjs|js))"?\s+%\*/i);
|
|
86
|
-
if (!match) return null;
|
|
87
|
-
const scriptPath = path.resolve(path.dirname(commandPath), match[1].replace(/[\\/]+/g, path.sep));
|
|
88
|
-
if (!fs.existsSync(scriptPath)) return null;
|
|
89
|
-
const localNode = path.join(path.dirname(commandPath), "node.exe");
|
|
90
|
-
return {
|
|
91
|
-
// npm uses its adjacent node.exe, otherwise the caller's PATH Node.
|
|
92
|
-
file: fs.existsSync(localNode) ? localNode : findWindowsPathCommand("node") || "node",
|
|
93
|
-
scriptPath
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function assertSafeCmdArguments(commandPath, args) {
|
|
98
|
-
const unsafePath = UNSAFE_CMD_PATH_RE.test(String(commandPath));
|
|
99
|
-
const unsafeArgument = args.find((value) => UNSAFE_CMD_ARGUMENT_RE.test(String(value)));
|
|
100
|
-
if (!unsafePath && unsafeArgument === undefined) return;
|
|
101
|
-
throw new Error(
|
|
102
|
-
`Cannot safely pass shell metacharacters through legacy batch command ${commandPath}. `
|
|
103
|
-
+ "Use an .exe/.js entrypoint or an npm-generated .cmd shim instead."
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function windowsCommand(command) {
|
|
108
|
-
const exe = resolveWindowsCommandFile(command[0]);
|
|
109
|
-
const rest = command.slice(1);
|
|
110
|
-
if (/\.(cmd|bat)$/i.test(exe)) {
|
|
111
|
-
const npmShim = resolveNpmCmdShim(exe);
|
|
112
|
-
if (npmShim) {
|
|
113
|
-
return {
|
|
114
|
-
file: npmShim.file,
|
|
115
|
-
args: [npmShim.scriptPath, ...rest]
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
assertSafeCmdArguments(exe, rest);
|
|
119
|
-
return {
|
|
120
|
-
file: "cmd.exe",
|
|
121
|
-
args: ["/d", "/s", "/c", "call", exe, ...rest]
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
if (/\.ps1$/i.test(exe)) {
|
|
125
|
-
return {
|
|
126
|
-
file: "powershell.exe",
|
|
127
|
-
args: ["-NoProfile", "-ExecutionPolicy", "Bypass", "-File", exe, ...rest]
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
if (/\.(js|mjs|cjs)$/i.test(exe)) {
|
|
131
|
-
return {
|
|
132
|
-
file: process.execPath,
|
|
133
|
-
args: [exe, ...rest]
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
return { file: exe, args: rest };
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
async function runCodexWrapper(runner) {
|
|
140
|
-
const command = findWindowsPathCommand("codex");
|
|
141
|
-
if (!command) {
|
|
142
|
-
console.error("codex: command not found on PATH");
|
|
143
|
-
process.exitCode = 127;
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
const env = { ...process.env, AI_BATTERY_ORIGINAL_CODEX: command };
|
|
147
|
-
delete env.AI_BATTERY_WRAPPED_CODEX;
|
|
148
|
-
let launch;
|
|
149
|
-
if (process.stdin.isTTY && process.stdout.isTTY
|
|
150
|
-
&& env.AI_BATTERY_DISABLE_WINDOWS_CODEX_RUNNER !== "1" && fs.existsSync(runner)) {
|
|
151
|
-
env.AI_BATTERY_WRAPPED_CODEX = "1";
|
|
152
|
-
launch = { file: process.execPath, args: [runner, "--provider", "all", "--left-padding", "2", "--", command, ...process.argv.slice(2)] };
|
|
153
|
-
} else {
|
|
154
|
-
launch = windowsCommand([command, ...process.argv.slice(2)]);
|
|
155
|
-
}
|
|
156
|
-
const child = spawn(launch.file, launch.args, { stdio: "inherit", env });
|
|
157
|
-
child.on("error", (error) => {
|
|
158
|
-
console.error("codex: " + error.message);
|
|
159
|
-
process.exitCode = error.code === "ENOENT" ? 127 : 1;
|
|
160
|
-
});
|
|
161
|
-
child.on("exit", (code) => { process.exitCode = code ?? 1; });
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
module.exports = {
|
|
165
|
-
findWindowsPathCommand, windowsCommandNames, runCodexWrapper,
|
|
166
|
-
normalizeWindowsCommandPath, resolveWindowsCommandFile, resolveNpmCmdShim, windowsCommand
|
|
167
|
-
};
|
|
1
|
+
// Shared PATH lookup and command launch for Windows. Generated bridges embed
|
|
2
|
+
// this file so a removed AI Battery package cannot pin Codex to an old install.
|
|
3
|
+
const fs = require("node:fs");
|
|
4
|
+
const path = require("node:path");
|
|
5
|
+
const { spawn } = require("node:child_process");
|
|
6
|
+
const UNSAFE_CMD_ARGUMENT_RE = /[\r\n&|<>^%!()]/;
|
|
7
|
+
const UNSAFE_CMD_PATH_RE = /[\r\n&|<>^%!]/;
|
|
8
|
+
|
|
9
|
+
function windowsCommandNames(command, env = process.env) {
|
|
10
|
+
const extensions = (env.PATHEXT || ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean);
|
|
11
|
+
return [...extensions.map((ext) => command + ext.toLowerCase()), command];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function isManagedWrapper(file) {
|
|
15
|
+
let fd;
|
|
16
|
+
try {
|
|
17
|
+
fd = fs.openSync(file, "r");
|
|
18
|
+
const header = Buffer.alloc(512);
|
|
19
|
+
const count = fs.readSync(fd, header, 0, header.length, 0);
|
|
20
|
+
return header.toString("utf8", 0, count).includes("AI_BATTERY_MANAGED_CODEX_WRAPPER");
|
|
21
|
+
} catch {
|
|
22
|
+
return false;
|
|
23
|
+
} finally {
|
|
24
|
+
if (fd !== undefined) fs.closeSync(fd);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function findWindowsPathCommand(command, env = process.env, skipPaths = []) {
|
|
29
|
+
for (const entry of (env.PATH || "").split(path.delimiter)) {
|
|
30
|
+
const dir = entry.replace(/^"(.*)"$/, "$1");
|
|
31
|
+
for (const name of windowsCommandNames(command, env)) {
|
|
32
|
+
const candidate = path.resolve(dir, name);
|
|
33
|
+
try {
|
|
34
|
+
if (!fs.statSync(candidate).isFile()) continue;
|
|
35
|
+
if (skipPaths.some((entry) => {
|
|
36
|
+
try {
|
|
37
|
+
return fs.realpathSync(candidate).toLowerCase() === fs.realpathSync(entry).toLowerCase();
|
|
38
|
+
} catch {
|
|
39
|
+
return path.resolve(candidate).toLowerCase() === path.resolve(entry).toLowerCase();
|
|
40
|
+
}
|
|
41
|
+
})) continue;
|
|
42
|
+
if (command === "codex" && isManagedWrapper(candidate)) continue;
|
|
43
|
+
return candidate;
|
|
44
|
+
} catch {}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function normalizeWindowsCommandPath(value) {
|
|
51
|
+
let text = String(value || "").trim();
|
|
52
|
+
for (let i = 0; i < 6; i += 1) {
|
|
53
|
+
const before = text;
|
|
54
|
+
text = text.replace(/\\"/g, "\"").trim();
|
|
55
|
+
if (
|
|
56
|
+
(text.startsWith("\"") && text.endsWith("\""))
|
|
57
|
+
|| (text.startsWith("'") && text.endsWith("'"))
|
|
58
|
+
) {
|
|
59
|
+
text = text.slice(1, -1).trim();
|
|
60
|
+
}
|
|
61
|
+
if (text === before) break;
|
|
62
|
+
}
|
|
63
|
+
return text;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function resolveWindowsCommandFile(commandPath) {
|
|
67
|
+
commandPath = normalizeWindowsCommandPath(commandPath);
|
|
68
|
+
if (path.extname(commandPath)) return commandPath;
|
|
69
|
+
for (const suffix of [".cmd", ".exe", ".bat", ".ps1"]) {
|
|
70
|
+
const candidate = `${commandPath}${suffix}`;
|
|
71
|
+
if (fs.existsSync(candidate)) return candidate;
|
|
72
|
+
}
|
|
73
|
+
return commandPath;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function resolveNpmCmdShim(commandPath) {
|
|
77
|
+
if (!/\.cmd$/i.test(commandPath)) return null;
|
|
78
|
+
let source;
|
|
79
|
+
try {
|
|
80
|
+
source = fs.readFileSync(commandPath, "utf8");
|
|
81
|
+
} catch {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
if (!source.includes("%dp0%") || !source.includes("%*")) return null;
|
|
85
|
+
const match = source.match(/%dp0%[\\/]([^"\r\n]+?\.(?:cjs|mjs|js))"?\s+%\*/i);
|
|
86
|
+
if (!match) return null;
|
|
87
|
+
const scriptPath = path.resolve(path.dirname(commandPath), match[1].replace(/[\\/]+/g, path.sep));
|
|
88
|
+
if (!fs.existsSync(scriptPath)) return null;
|
|
89
|
+
const localNode = path.join(path.dirname(commandPath), "node.exe");
|
|
90
|
+
return {
|
|
91
|
+
// npm uses its adjacent node.exe, otherwise the caller's PATH Node.
|
|
92
|
+
file: fs.existsSync(localNode) ? localNode : findWindowsPathCommand("node") || "node",
|
|
93
|
+
scriptPath
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function assertSafeCmdArguments(commandPath, args) {
|
|
98
|
+
const unsafePath = UNSAFE_CMD_PATH_RE.test(String(commandPath));
|
|
99
|
+
const unsafeArgument = args.find((value) => UNSAFE_CMD_ARGUMENT_RE.test(String(value)));
|
|
100
|
+
if (!unsafePath && unsafeArgument === undefined) return;
|
|
101
|
+
throw new Error(
|
|
102
|
+
`Cannot safely pass shell metacharacters through legacy batch command ${commandPath}. `
|
|
103
|
+
+ "Use an .exe/.js entrypoint or an npm-generated .cmd shim instead."
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function windowsCommand(command) {
|
|
108
|
+
const exe = resolveWindowsCommandFile(command[0]);
|
|
109
|
+
const rest = command.slice(1);
|
|
110
|
+
if (/\.(cmd|bat)$/i.test(exe)) {
|
|
111
|
+
const npmShim = resolveNpmCmdShim(exe);
|
|
112
|
+
if (npmShim) {
|
|
113
|
+
return {
|
|
114
|
+
file: npmShim.file,
|
|
115
|
+
args: [npmShim.scriptPath, ...rest]
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
assertSafeCmdArguments(exe, rest);
|
|
119
|
+
return {
|
|
120
|
+
file: "cmd.exe",
|
|
121
|
+
args: ["/d", "/s", "/c", "call", exe, ...rest]
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
if (/\.ps1$/i.test(exe)) {
|
|
125
|
+
return {
|
|
126
|
+
file: "powershell.exe",
|
|
127
|
+
args: ["-NoProfile", "-ExecutionPolicy", "Bypass", "-File", exe, ...rest]
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
if (/\.(js|mjs|cjs)$/i.test(exe)) {
|
|
131
|
+
return {
|
|
132
|
+
file: process.execPath,
|
|
133
|
+
args: [exe, ...rest]
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
return { file: exe, args: rest };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async function runCodexWrapper(runner) {
|
|
140
|
+
const command = findWindowsPathCommand("codex");
|
|
141
|
+
if (!command) {
|
|
142
|
+
console.error("codex: command not found on PATH");
|
|
143
|
+
process.exitCode = 127;
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const env = { ...process.env, AI_BATTERY_ORIGINAL_CODEX: command };
|
|
147
|
+
delete env.AI_BATTERY_WRAPPED_CODEX;
|
|
148
|
+
let launch;
|
|
149
|
+
if (process.stdin.isTTY && process.stdout.isTTY
|
|
150
|
+
&& env.AI_BATTERY_DISABLE_WINDOWS_CODEX_RUNNER !== "1" && fs.existsSync(runner)) {
|
|
151
|
+
env.AI_BATTERY_WRAPPED_CODEX = "1";
|
|
152
|
+
launch = { file: process.execPath, args: [runner, "--provider", "all", "--left-padding", "2", "--", command, ...process.argv.slice(2)] };
|
|
153
|
+
} else {
|
|
154
|
+
launch = windowsCommand([command, ...process.argv.slice(2)]);
|
|
155
|
+
}
|
|
156
|
+
const child = spawn(launch.file, launch.args, { stdio: "inherit", env });
|
|
157
|
+
child.on("error", (error) => {
|
|
158
|
+
console.error("codex: " + error.message);
|
|
159
|
+
process.exitCode = error.code === "ENOENT" ? 127 : 1;
|
|
160
|
+
});
|
|
161
|
+
child.on("exit", (code) => { process.exitCode = code ?? 1; });
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
module.exports = {
|
|
165
|
+
findWindowsPathCommand, windowsCommandNames, runCodexWrapper,
|
|
166
|
+
normalizeWindowsCommandPath, resolveWindowsCommandFile, resolveNpmCmdShim, windowsCommand
|
|
167
|
+
};
|
package/docs/RELEASING.md
CHANGED
|
@@ -1,60 +1,64 @@
|
|
|
1
|
-
# npm release
|
|
2
|
-
|
|
3
|
-
The current release candidate is **0.1.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
npm
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
##
|
|
28
|
-
|
|
29
|
-
```
|
|
30
|
-
npm
|
|
31
|
-
npm
|
|
32
|
-
npm
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
npm
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
1
|
+
# npm release
|
|
2
|
+
|
|
3
|
+
The current release candidate is **0.1.21**, prepared from the 0.1.20 main branch. npm `latest` was checked as **0.1.20** on 2026-10-15. This preparation does not publish to npm or create a release tag.
|
|
4
|
+
|
|
5
|
+
## Changes in 0.1.21
|
|
6
|
+
|
|
7
|
+
- Windows default PTY composition and POSIX terminal rows poll every 2 seconds by default. POSIX polling runs asynchronously so usage scans do not block terminal input or output.
|
|
8
|
+
- Polls allow up to 10 seconds by default. Failed updates retry after 1 second, with backoff for repeated failures. A retained reading is marked `[stale]`; an initial failure displays `AI Battery unavailable`.
|
|
9
|
+
- New and changed Codex session logs invalidate the parsing cache before its normal expiry. Account usage sharing between Windows and WSL, separate session stores, and existing runtime selection are preserved.
|
|
10
|
+
- WSL reuses a brief Windows process scan cache. A known active Codex does not require another process scan just to fetch its usage.
|
|
11
|
+
- The package no longer depends on an older copy of `ai-battery` itself.
|
|
12
|
+
|
|
13
|
+
Values remain the latest usage reported by local Codex logs and Claude statusLine. They are not direct live server measurements; delayed source updates can delay the display.
|
|
14
|
+
|
|
15
|
+
## Validation
|
|
16
|
+
|
|
17
|
+
The integrated candidate passed the following checks on 2026-10-15:
|
|
18
|
+
|
|
19
|
+
| Check | Result |
|
|
20
|
+
| --- | --- |
|
|
21
|
+
| WSL `npm run check` | 133 passed, 15 platform skips, 0 failures |
|
|
22
|
+
| Windows native Node 22.23.2 `node --test` | 117 passed, 31 platform skips, 0 failures |
|
|
23
|
+
| Focused polling and freshness tests | Slow polls, failed/empty/partial/oversized output, automatic recovery, zero remaining, new sessions, and cache invalidation |
|
|
24
|
+
|
|
25
|
+
Tarball installation results, package contents, and checksums are saved with the local release artifacts under `.cache/releases/0.1.21/`. Windows ARM64 still requires a device check; native verification here uses x64.
|
|
26
|
+
|
|
27
|
+
## Prepare
|
|
28
|
+
|
|
29
|
+
```text
|
|
30
|
+
npm run check
|
|
31
|
+
npm pack --dry-run
|
|
32
|
+
npm pack --pack-destination .cache/releases/0.1.21
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Inspect the package file list. It must include `lib`, the bin entrypoints and required PowerShell modules. Screenshots, logs, credentials, local runtime binaries, tests and `.cache` must not be included. Test the exact tarball with a clean installation on Windows and POSIX. On Windows, also run `npm run smoke:composite` and `npm run smoke:codex-fullscreen-win` against the installed runner; the Codex fixture uses a local mock server and does not consume account quota.
|
|
36
|
+
|
|
37
|
+
## Publish manually
|
|
38
|
+
|
|
39
|
+
```text
|
|
40
|
+
npm login
|
|
41
|
+
npm whoami
|
|
42
|
+
npm publish .cache/releases/0.1.21/ai-battery-0.1.21.tgz --access public --tag latest
|
|
43
|
+
npm view ai-battery version dist-tags --json
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Use the exact tested tarball. Complete npm's authentication/2FA prompt when requested. Publishing the same name/version twice is not allowed. If 0.1.21 is already published when you release, increment the version and repeat preparation. Use `--tag next` instead of `--tag latest` for a preview release.
|
|
47
|
+
|
|
48
|
+
After successful publication, tag the tested commit (`v0.1.21`) and push the release tag. Keep the remote release branch until the release commit is integrated into main.
|
|
49
|
+
|
|
50
|
+
## Update installed integrations
|
|
51
|
+
|
|
52
|
+
Run separately in Windows and WSL:
|
|
53
|
+
|
|
54
|
+
```text
|
|
55
|
+
npm install -g ai-battery@0.1.21
|
|
56
|
+
ai-battery setup codex
|
|
57
|
+
ai-battery doctor
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Restart the terminal session after updating. A running PTY wrapper retains its loaded code. Existing managed wrappers must be regenerated by setup to adopt the current integration.
|
|
61
|
+
|
|
62
|
+
The private Windows runtime lives under `%LOCALAPPDATA%\ai-battery\runtimes` by default, or the selected AI Battery data directory. `AI_BATTERY_RUNTIME_DOWNLOAD=0` disables downloading. System Node, PATH and the original Codex installation are preserved.
|
|
63
|
+
|
|
64
|
+
Official npm references: [publish](https://docs.npmjs.com/cli/v11/commands/npm-publish/), [login](https://docs.npmjs.com/cli/v11/commands/npm-login/).
|