@firstpick/pi-package-webui 0.6.9 → 0.7.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/index.ts CHANGED
@@ -3,6 +3,9 @@ import path from "node:path";
3
3
  import type { Readable } from "node:stream";
4
4
  import { fileURLToPath } from "node:url";
5
5
  import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
6
+ import { delay, takeValue, tokenizeArgs } from "@firstpick/pi-utils";
7
+ import { detachChildProcess as releaseStartedChild, isProcessRunning, terminateChildProcess as terminateFailedChild } from "@firstpick/pi-utils/process";
8
+ import { fetchJsonWithTimeout as fetchJsonWithTimeoutBase } from "@firstpick/pi-utils/http";
6
9
  import {
7
10
  gitWorkflowPreferencesSummary,
8
11
  readGitWorkflowPreferences,
@@ -58,53 +61,6 @@ type RestorableWebuiTab = {
58
61
 
59
62
  type WebuiChild = ChildProcessByStdio<null, Readable, Readable>;
60
63
 
61
- function tokenizeArgs(input: string): string[] {
62
- const tokens: string[] = [];
63
- let current = "";
64
- let quote: '"' | "'" | undefined;
65
- let escaped = false;
66
-
67
- for (const char of input) {
68
- if (escaped) {
69
- current += char;
70
- escaped = false;
71
- continue;
72
- }
73
- if (char === "\\") {
74
- escaped = true;
75
- continue;
76
- }
77
- if (quote) {
78
- if (char === quote) quote = undefined;
79
- else current += char;
80
- continue;
81
- }
82
- if (char === '"' || char === "'") {
83
- quote = char;
84
- continue;
85
- }
86
- if (/\s/.test(char)) {
87
- if (current) {
88
- tokens.push(current);
89
- current = "";
90
- }
91
- continue;
92
- }
93
- current += char;
94
- }
95
-
96
- if (escaped) current += "\\";
97
- if (quote) throw new Error(`Unclosed ${quote} quote`);
98
- if (current) tokens.push(current);
99
- return tokens;
100
- }
101
-
102
- function takeValue(tokens: string[], index: number, flag: string): string {
103
- const value = tokens[index + 1];
104
- if (!value || value.startsWith("--")) throw new Error(`${flag} requires a value`);
105
- return value;
106
- }
107
-
108
64
  function parseStartWebuiArgs(args: string): StartWebuiOptions {
109
65
  const options: StartWebuiOptions = {
110
66
  host: DEFAULT_HOST,
@@ -210,18 +166,8 @@ function urlFor(options: WebuiAddress): string {
210
166
  return `http://${host}:${options.port}/`;
211
167
  }
212
168
 
213
- async function fetchJsonWithTimeout(url: string, init: RequestInit = {}, timeoutMs = 900): Promise<{ ok: boolean; status: number; body: any } | null> {
214
- const controller = new AbortController();
215
- const timeout = setTimeout(() => controller.abort(), timeoutMs);
216
- try {
217
- const response = await fetch(url, { ...init, signal: controller.signal });
218
- const body = await response.json().catch(() => undefined);
219
- return { ok: response.ok, status: response.status, body };
220
- } catch {
221
- return null;
222
- } finally {
223
- clearTimeout(timeout);
224
- }
169
+ async function fetchJsonWithTimeout(url: string, init: RequestInit = {}, timeoutMs = 900): Promise<{ ok: boolean; status: number; body?: any }> {
170
+ return fetchJsonWithTimeoutBase(url, init, { timeoutMs });
225
171
  }
226
172
 
227
173
  async function probeExistingWebui(url: string): Promise<ExistingWebui | null> {
@@ -324,15 +270,11 @@ async function fetchRestorableTabs(url: string, existing: ExistingWebui, options
324
270
  return mergeRestorableTabsFromStatusSources([statusData?.restorableTabs, existing.restorableTabs], options);
325
271
  }
326
272
 
327
- function sleep(ms: number): Promise<void> {
328
- return new Promise((resolve) => setTimeout(resolve, ms));
329
- }
330
-
331
273
  async function waitForWebuiToStop(url: string, timeoutMs = 7_000): Promise<boolean> {
332
274
  const deadline = Date.now() + timeoutMs;
333
275
  while (Date.now() < deadline) {
334
276
  if (!(await probeExistingWebui(url))) return true;
335
- await sleep(180);
277
+ await delay(180);
336
278
  }
337
279
  return !(await probeExistingWebui(url));
338
280
  }
@@ -342,15 +284,6 @@ async function requestWebuiShutdown(url: string): Promise<boolean> {
342
284
  return result?.ok === true && result.body?.ok === true;
343
285
  }
344
286
 
345
- function isProcessRunning(pid: number): boolean {
346
- try {
347
- process.kill(pid, 0);
348
- return true;
349
- } catch (error) {
350
- return (error as NodeJS.ErrnoException)?.code === "EPERM";
351
- }
352
- }
353
-
354
287
  async function terminatePid(pid: number): Promise<void> {
355
288
  if (!Number.isInteger(pid) || pid <= 0 || pid === process.pid || !isProcessRunning(pid)) return;
356
289
  try {
@@ -362,7 +295,7 @@ async function terminatePid(pid: number): Promise<void> {
362
295
  const deadline = Date.now() + 4_000;
363
296
  while (Date.now() < deadline) {
364
297
  if (!isProcessRunning(pid)) return;
365
- await sleep(160);
298
+ await delay(160);
366
299
  }
367
300
 
368
301
  try {
@@ -472,23 +405,6 @@ function openDefaultBrowser(url: string): void {
472
405
  child.unref();
473
406
  }
474
407
 
475
- function releaseStartedChild(child: WebuiChild): void {
476
- child.stdout.removeAllListeners("data");
477
- child.stderr.removeAllListeners("data");
478
- (child.stdout as Readable & { unref?: () => void }).unref?.();
479
- (child.stderr as Readable & { unref?: () => void }).unref?.();
480
- child.unref();
481
- }
482
-
483
- function terminateFailedChild(child: WebuiChild): void {
484
- if (child.exitCode === null) child.kill("SIGTERM");
485
- setTimeout(() => {
486
- if (child.exitCode === null) child.kill("SIGKILL");
487
- }, 2000).unref?.();
488
- child.stdout.destroy();
489
- child.stderr.destroy();
490
- }
491
-
492
408
  function startupTimeoutMs(restoreTabCount: number): number {
493
409
  const extraTabs = Math.max(0, restoreTabCount - 1);
494
410
  return Math.min(START_TIMEOUT_MAX_MS, START_TIMEOUT_MS + extraTabs * START_TIMEOUT_PER_RESTORED_TAB_MS);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@firstpick/pi-package-webui",
3
- "version": "0.6.9",
3
+ "version": "0.7.0",
4
4
  "description": "Pi Web UI companion package with a local browser UI CLI plus /webui-start and /webui-status commands.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/Firstp1ck/npm-packages/tree/main/pi-package-webui#readme",
@@ -51,11 +51,12 @@
51
51
  "pi-webui": "./bin/pi-webui.mjs"
52
52
  },
53
53
  "scripts": {
54
- "check": "node --check public/app.js && node --check public/voice-conversation.mjs && node --check bin/pi-webui.mjs && node --check webui-rpc-helper.mjs && node --check lib/trust-boundaries.mjs && node --check lib/native-command-adapter.mjs && node --check lib/session-actions.mjs && node --check lib/auth-actions.mjs && node --check lib/temp-artifacts.mjs && node tests/run-all.mjs",
54
+ "check": "node --check public/app.js && node --check public/voice-conversation.mjs && node --check public/aur-review-payload.mjs && node --check public/guided-git-command-state.mjs && node --check public/guided-git-review-state.mjs && node --check bin/pi-webui.mjs && node --check webui-rpc-helper.mjs && node --check lib/trust-boundaries.mjs && node --check lib/native-command-adapter.mjs && node --check lib/session-actions.mjs && node --check lib/auth-actions.mjs && node --check lib/temp-artifacts.mjs && node tests/run-all.mjs",
55
55
  "test": "node tests/run-all.mjs"
56
56
  },
57
57
  "dependencies": {
58
- "@earendil-works/pi-coding-agent": "^0.80.9",
58
+ "@earendil-works/pi-coding-agent": "^0.80.10",
59
+ "@firstpick/pi-utils": "^0.2.3",
59
60
  "mermaid": "^11.15.0"
60
61
  },
61
62
  "optionalDependencies": {
@@ -64,7 +65,7 @@
64
65
  "@firstpick/pi-extension-fish-user-bash": "^0.2.1",
65
66
  "@firstpick/pi-extension-git-footer-status": "^0.4.0",
66
67
  "@firstpick/pi-extension-release-aur": "^0.1.7",
67
- "@firstpick/pi-extension-release-npm": "^0.4.1",
68
+ "@firstpick/pi-extension-release-npm": "^0.4.2",
68
69
  "@firstpick/pi-extension-safety-guard": "^0.2.5",
69
70
  "@firstpick/pi-extension-setup-skills": "^0.1.8",
70
71
  "@firstpick/pi-extension-stats": "^0.2.8",