@firstpick/pi-package-webui 0.11.0 → 0.11.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/DEVELOPMENT.md +181 -12
- package/README.md +41 -9
- package/TECHNICAL.md +91 -8
- package/bin/pi-webui.mjs +5432 -11428
- package/index.ts +10 -3
- package/lib/codex-cli-usage.mjs +232 -0
- package/lib/codex-usage-auth.mjs +39 -3
- package/lib/codex-usage-source.mjs +36 -0
- package/lib/component-update-state.mjs +2 -2
- package/lib/git-workflow-preference-values.mjs +122 -0
- package/lib/git-workflow-preferences.mjs +35 -563
- package/lib/package-updates.mjs +260 -0
- package/lib/rpc-supervisor-client.mjs +8 -4
- package/lib/server/app-runners.mjs +1667 -0
- package/lib/server/codex-fast-mode.mjs +161 -0
- package/lib/server/conversation-mode.mjs +218 -0
- package/lib/server/conversation-voice.mjs +318 -0
- package/lib/server/extension-ui-requests.mjs +114 -0
- package/lib/server/git-generation.mjs +459 -0
- package/lib/server/git-mutations.mjs +1827 -0
- package/lib/server/git-reads.mjs +608 -0
- package/lib/server/git-worktree-sessions.mjs +408 -0
- package/lib/server/helper-requests.mjs +82 -0
- package/lib/server/no-project-workspaces.mjs +103 -0
- package/lib/server/rpc-transports.mjs +394 -0
- package/lib/server/settings-store.mjs +544 -0
- package/lib/server/sse-delivery.mjs +138 -0
- package/lib/server/workspace-files.mjs +563 -0
- package/lib/update/supervisor.mjs +4 -0
- package/lib/webui-workspaces.mjs +2 -0
- package/package.json +5 -5
- package/public/activity-agents-controller.mjs +269 -0
- package/public/activity-events-controller.mjs +236 -0
- package/public/activity-intercom-controller.mjs +627 -0
- package/public/activity-queue-controller.mjs +534 -0
- package/public/activity-todo-view.mjs +89 -0
- package/public/activity-usage-controller.mjs +589 -0
- package/public/app.js +3761 -3878
- package/public/conversation-overview.css +84 -0
- package/public/conversation-overview.mjs +193 -0
- package/public/draft-conversations.mjs +135 -0
- package/public/focused-composer-controls.css +111 -0
- package/public/focused-composer-controls.mjs +143 -0
- package/public/focused-composer-disclosure.mjs +223 -0
- package/public/focused-shell-adapter.css +264 -0
- package/public/focused-shell-adapter.mjs +257 -0
- package/public/index.html +137 -80
- package/public/model-catalog-order.mjs +63 -0
- package/public/platform-event-stream.mjs +64 -0
- package/public/project-artifact-controller.mjs +218 -0
- package/public/project-file-viewer-controller.mjs +383 -0
- package/public/project-git-tools-controller.mjs +181 -0
- package/public/project-runners-controller.mjs +220 -0
- package/public/service-worker.js +52 -1
- package/public/session-attention-scheduler.mjs +182 -0
- package/public/session-composer-store.mjs +56 -0
- package/public/session-context.mjs +51 -0
- package/public/session-extension-requests.mjs +63 -0
- package/public/session-shelf-state.mjs +151 -0
- package/public/session-shelves.css +171 -0
- package/public/session-shelves.mjs +95 -0
- package/public/settings-navigation-controller.mjs +249 -0
- package/public/settings-navigation.css +61 -0
- package/public/settings-resource-controller.mjs +232 -0
- package/public/settings-workspace.css +19 -0
- package/public/settings-workspace.mjs +175 -0
- package/public/shell-actions.mjs +87 -0
- package/public/shell-attention-inbox.css +72 -0
- package/public/shell-attention-inbox.mjs +198 -0
- package/public/shell-context-actions.css +61 -0
- package/public/shell-context-actions.mjs +168 -0
- package/public/shell-inspector-resize.mjs +124 -0
- package/public/shell-navigation-resize.mjs +107 -0
- package/public/shell-navigation.mjs +127 -0
- package/public/shell-preferences.mjs +108 -0
- package/public/shell-surfaces.mjs +99 -0
- package/public/shell-tools-launcher.css +97 -0
- package/public/shell-tools-launcher.mjs +222 -0
- package/public/shell-view-host.mjs +115 -0
- package/public/shell-workspace.css +266 -0
- package/public/shell-workspace.mjs +402 -0
- package/public/sidebar-update-menu.css +190 -0
- package/public/sidebar-update-menu.mjs +512 -0
- package/public/styles.css +320 -169
- package/public/subagent-launch-slot-state.mjs +2 -2
- package/public/theme-contract.mjs +1 -1
- package/public/themed-selects.css +156 -0
- package/public/workspace-choices.mjs +104 -0
- package/tests/activity-agents-controller.test.mjs +652 -0
- package/tests/activity-events-controller.test.mjs +511 -0
- package/tests/app-runner-terminal-static.test.mjs +3 -3
- package/tests/append-system-http.test.mjs +7 -6
- package/tests/append-system-selector-static.test.mjs +6 -6
- package/tests/aur-review-widget-static.test.mjs +11 -3
- package/tests/boot-failure-diagnostics.test.mjs +8 -3
- package/tests/browser/activity-intercom-controller.spec.mjs +457 -0
- package/tests/browser/activity-navigation-integration.spec.mjs +45 -0
- package/tests/browser/activity-owner-integration.spec.mjs +201 -0
- package/tests/browser/activity-queue-controller.spec.mjs +405 -0
- package/tests/browser/activity-section-picker.spec.mjs +294 -0
- package/tests/browser/activity-todo-view.spec.mjs +60 -0
- package/tests/browser/activity-usage-controller.spec.mjs +416 -0
- package/tests/browser/compact-layout-integration.spec.mjs +58 -0
- package/tests/browser/composer-action-grid-reorder.spec.mjs +16 -0
- package/tests/browser/composer-disclosure.spec.mjs +389 -0
- package/tests/browser/composer-toolbar-polish.spec.mjs +343 -0
- package/tests/browser/control-deck-component-updates.spec.mjs +5 -14
- package/tests/browser/control-deck-side-panels.spec.mjs +27 -12
- package/tests/browser/control-visibility.spec.mjs +5 -0
- package/tests/browser/controls-layout.spec.mjs +21 -2
- package/tests/browser/conversation-overview.spec.mjs +251 -0
- package/tests/browser/draft-conversations.spec.mjs +276 -0
- package/tests/browser/empty-startup-fixture.mjs +135 -0
- package/tests/browser/empty-startup.spec.mjs +282 -0
- package/tests/browser/events-tree-context-menu.spec.mjs +1 -0
- package/tests/browser/focused-composer-more.spec.mjs +127 -0
- package/tests/browser/focused-model-shortcuts.spec.mjs +120 -0
- package/tests/browser/focused-navigation-fixture.mjs +32 -0
- package/tests/browser/focused-project-activity.spec.mjs +111 -0
- package/tests/browser/focused-shell-integration.spec.mjs +208 -0
- package/tests/browser/folder-picker-layout-fixture.mjs +13 -0
- package/tests/browser/folder-picker-layout.spec.mjs +302 -0
- package/tests/browser/footer-model-advanced-layout.spec.mjs +27 -14
- package/tests/browser/git-setup-cancel.spec.mjs +251 -0
- package/tests/browser/guided-git-fallback.spec.mjs +40 -21
- package/tests/browser/inspector-header.spec.mjs +221 -0
- package/tests/browser/inspector-overview-integration.spec.mjs +104 -0
- package/tests/browser/inspector-resize.spec.mjs +227 -0
- package/tests/browser/inspector-topic-overflow.spec.mjs +56 -0
- package/tests/browser/interaction-action-discovery.spec.mjs +332 -0
- package/tests/browser/interaction-attention-inbox.spec.mjs +364 -0
- package/tests/browser/interaction-attention-integration.spec.mjs +53 -0
- package/tests/browser/interaction-composer-controls.spec.mjs +267 -0
- package/tests/browser/interaction-composer-integration.spec.mjs +175 -0
- package/tests/browser/interaction-continuity.spec.mjs +50 -26
- package/tests/browser/interaction-model-picker.spec.mjs +86 -0
- package/tests/browser/last-terminal-close.spec.mjs +280 -0
- package/tests/browser/mobile-foundation.spec.mjs +172 -20
- package/tests/browser/model-catalog-groups.spec.mjs +104 -0
- package/tests/browser/more-row-menus.spec.mjs +258 -0
- package/tests/browser/new-tab-hit-target.spec.mjs +228 -0
- package/tests/browser/no-project-conversation.spec.mjs +113 -0
- package/tests/browser/optimistic-prompt.spec.mjs +3 -0
- package/tests/browser/overhaul-w7-accessibility.spec.mjs +227 -0
- package/tests/browser/overhaul-w7-capabilities.spec.mjs +298 -0
- package/tests/browser/overhaul-w7-companions.spec.mjs +290 -0
- package/tests/browser/overhaul-w7-focus-repairs.spec.mjs +210 -0
- package/tests/browser/overhaul-w7-layout-repairs.spec.mjs +183 -0
- package/tests/browser/overhaul-w7-parent-repairs.spec.mjs +97 -0
- package/tests/browser/overhaul-w7-pwa.spec.mjs +247 -0
- package/tests/browser/overhaul-w7-responsive.spec.mjs +450 -0
- package/tests/browser/overhaul-w7-update.spec.mjs +215 -0
- package/tests/browser/overhaul-w7-zoom.spec.mjs +243 -0
- package/tests/browser/overview-agent-ownership.spec.mjs +45 -0
- package/tests/browser/persistent-ui-layout.spec.mjs +3 -0
- package/tests/browser/project-artifact-controller.spec.mjs +324 -0
- package/tests/browser/project-artifact-integration.spec.mjs +103 -0
- package/tests/browser/project-file-buffer-integration.spec.mjs +230 -0
- package/tests/browser/project-git-tools-integration.spec.mjs +369 -0
- package/tests/browser/session-fixture.mjs +118 -0
- package/tests/browser/session-ownership.spec.mjs +175 -0
- package/tests/browser/session-shelves-integration.spec.mjs +219 -0
- package/tests/browser/session-shelves.spec.mjs +340 -0
- package/tests/browser/session-sidebar-resize.spec.mjs +138 -0
- package/tests/browser/session-summary.spec.mjs +17 -12
- package/tests/browser/settings-complete-home.spec.mjs +463 -0
- package/tests/browser/settings-entrypoint.spec.mjs +104 -0
- package/tests/browser/settings-navigation-controller.spec.mjs +383 -0
- package/tests/browser/settings-navigation-integration.spec.mjs +348 -0
- package/tests/browser/settings-resource-lifetime.spec.mjs +349 -0
- package/tests/browser/settings-workspace-integration.spec.mjs +132 -0
- package/tests/browser/shell-ownership.spec.mjs +398 -0
- package/tests/browser/shell-rollout-fixture.mjs +259 -0
- package/tests/browser/shell-rollout.spec.mjs +357 -0
- package/tests/browser/shell-tools-launcher.spec.mjs +260 -0
- package/tests/browser/shell-workspace.spec.mjs +433 -0
- package/tests/browser/side-panel-section-reorder.spec.mjs +3 -1
- package/tests/browser/sidebar-floating-tooltips.spec.mjs +37 -0
- package/tests/browser/sidebar-update-integration.spec.mjs +277 -0
- package/tests/browser/sidebar-update-menu.spec.mjs +402 -0
- package/tests/browser/sidebar-update-progress.spec.mjs +472 -0
- package/tests/browser/stream-output-isolation.spec.mjs +6 -3
- package/tests/browser/themed-selects.spec.mjs +297 -0
- package/tests/browser/thread-start-flow.spec.mjs +481 -0
- package/tests/browser/ui-polish-activity-controls.spec.mjs +177 -0
- package/tests/browser/ui-polish-features.spec.mjs +85 -0
- package/tests/browser/ui-polish-file-header.spec.mjs +43 -0
- package/tests/browser/ui-polish-local-git.spec.mjs +183 -0
- package/tests/browser/ui-polish-mobile-recovery.spec.mjs +36 -0
- package/tests/browser/ui-polish-navigation.spec.mjs +127 -0
- package/tests/browser/ui-polish-session-actions.spec.mjs +282 -0
- package/tests/browser/user-noticeable-fixes.spec.mjs +10 -3
- package/tests/browser/windows-drive-parent.spec.mjs +4 -2
- package/tests/browser/workspace-draft-selection.spec.mjs +266 -0
- package/tests/codex-cli-usage.test.mjs +189 -0
- package/tests/codex-fast-mode-static.test.mjs +16 -11
- package/tests/codex-speed-mode-browser-state.test.mjs +6 -3
- package/tests/codex-usage-auth.test.mjs +168 -9
- package/tests/codex-usage-http.test.mjs +124 -0
- package/tests/codex-usage-source.test.mjs +146 -0
- package/tests/component-update-api-static.test.mjs +2 -2
- package/tests/composer-action-grid-reorder-static.test.mjs +3 -3
- package/tests/control-deck-component-updates-static.test.mjs +9 -4
- package/tests/control-visibility-static.test.mjs +3 -3
- package/tests/controls-layout-static.test.mjs +5 -5
- package/tests/conversation-overview-adapter.test.mjs +100 -0
- package/tests/document-artifact-static.test.mjs +2 -1
- package/tests/draft-conversations.test.mjs +205 -0
- package/tests/empty-startup-launcher.test.mjs +197 -0
- package/tests/events-tree-context-menu-static.test.mjs +22 -18
- package/tests/fake-pi-session-ownership.test.mjs +302 -0
- package/tests/fast-mode-client-static.test.mjs +1 -1
- package/tests/feature-decision-output-popup-static.test.mjs +2 -2
- package/tests/feature-system-prompt-optional-feature-static.test.mjs +1 -1
- package/tests/file-viewer-image-static.test.mjs +15 -11
- package/tests/file-viewer-search-static.test.mjs +5 -5
- package/tests/fixtures/fake-codex-app-server.mjs +61 -0
- package/tests/fixtures/fake-pi.mjs +41 -9
- package/tests/footer-provider-usage-visual-static.test.mjs +7 -6
- package/tests/foreground-transcript-catch-up-static.test.mjs +7 -4
- package/tests/git-footer-context-menu-static.test.mjs +1 -1
- package/tests/git-panel-file-preview-static.test.mjs +41 -48
- package/tests/git-panel-live-updates-static.test.mjs +2 -1
- package/tests/git-workflow-dispatch.test.mjs +66 -0
- package/tests/git-workflow-preferences.test.mjs +9 -1
- package/tests/guided-git-activation-static.test.mjs +14 -11
- package/tests/guided-git-fallback-ui-static.test.mjs +8 -5
- package/tests/guided-git-message-reliability-static.test.mjs +10 -6
- package/tests/guided-git-publication-backend-static.test.mjs +16 -11
- package/tests/http-endpoints-harness.test.mjs +29 -9
- package/tests/interaction-state-stability-static.test.mjs +4 -3
- package/tests/intercom-conversation-viewer-static.test.mjs +29 -17
- package/tests/intercom-tag-layout-static.test.mjs +13 -12
- package/tests/intercom-tag-overflow-layout.test.mjs +11 -10
- package/tests/main-output-loading-static.test.mjs +4 -4
- package/tests/middle-button-drag-scroll-static.test.mjs +3 -3
- package/tests/mobile-compact-layout.test.mjs +2 -2
- package/tests/mobile-continuity-tablet-static.test.mjs +3 -3
- package/tests/mobile-foundation-static.test.mjs +10 -7
- package/tests/mobile-phone-experience-static.test.mjs +3 -2
- package/tests/mobile-static.test.mjs +173 -135
- package/tests/model-catalog-order.test.mjs +103 -0
- package/tests/native-parity.test.mjs +6 -4
- package/tests/native-session-choice-owner.test.mjs +102 -0
- package/tests/native-update-lifecycle.test.mjs +10 -5
- package/tests/no-project-http-harness.test.mjs +328 -0
- package/tests/no-project-navigation.test.mjs +54 -0
- package/tests/no-project-workspaces.test.mjs +77 -0
- package/tests/open-issue-wizard-static.test.mjs +1 -1
- package/tests/optional-feature-migration-frontend.test.mjs +4 -4
- package/tests/optional-feature-prompt-impact-static.test.mjs +3 -3
- package/tests/package-updates.test.mjs +630 -0
- package/tests/persistent-ui-layout-static.test.mjs +3 -3
- package/tests/platform-event-stream.test.mjs +253 -0
- package/tests/project-file-viewer-controller.test.mjs +652 -0
- package/tests/project-git-tools-adapter.test.mjs +103 -0
- package/tests/project-git-tools-controller.test.mjs +577 -0
- package/tests/project-runners-controller.test.mjs +453 -0
- package/tests/prompt-input-vertical-scroll-static.test.mjs +3 -3
- package/tests/prompt-routing-input-clear-static.test.mjs +2 -2
- package/tests/questionnaire-dialog.test.mjs +6 -6
- package/tests/queue-edit-reorder-static.test.mjs +48 -51
- package/tests/rpc-supervisor-host.test.mjs +4 -1
- package/tests/safety-guard-auto-review-static.test.mjs +1 -1
- package/tests/sampling-native-controls-user-flow.test.mjs +7 -1
- package/tests/server-app-runners.test.mjs +536 -0
- package/tests/server-codex-fast-mode.test.mjs +346 -0
- package/tests/server-conversation-mode.test.mjs +386 -0
- package/tests/server-conversation-voice.test.mjs +426 -0
- package/tests/server-extension-ui-requests.test.mjs +357 -0
- package/tests/server-git-generation.test.mjs +558 -0
- package/tests/server-git-mutations.test.mjs +729 -0
- package/tests/server-git-reads.test.mjs +381 -0
- package/tests/server-git-worktree-sessions.test.mjs +502 -0
- package/tests/server-helper-requests.test.mjs +364 -0
- package/tests/server-rpc-transports.test.mjs +553 -0
- package/tests/server-settings-store.test.mjs +132 -0
- package/tests/server-shell-rollout.test.mjs +478 -0
- package/tests/server-sse-delivery.test.mjs +410 -0
- package/tests/server-workspace-files.test.mjs +546 -0
- package/tests/session-attention-scheduler.test.mjs +514 -0
- package/tests/session-composer-store.test.mjs +153 -0
- package/tests/session-context.test.mjs +134 -0
- package/tests/session-extension-requests.test.mjs +193 -0
- package/tests/session-ownership-adapter.test.mjs +130 -0
- package/tests/session-sampling-ui-static.test.mjs +10 -4
- package/tests/session-send-lifetime.test.mjs +140 -0
- package/tests/session-shelf-state.test.mjs +348 -0
- package/tests/session-shelves-adapter.test.mjs +79 -0
- package/tests/session-summary-lifetime.test.mjs +54 -0
- package/tests/settings-resource-controller.test.mjs +548 -0
- package/tests/shell-action-adapter.test.mjs +169 -0
- package/tests/shell-actions.test.mjs +182 -0
- package/tests/shell-navigation.test.mjs +348 -0
- package/tests/shell-preferences.test.mjs +367 -0
- package/tests/side-panel-section-height-removal-static.test.mjs +3 -3
- package/tests/stream-output-isolation-static.test.mjs +1 -1
- package/tests/subagent-gate-visibility.test.mjs +1 -1
- package/tests/subagent-launch-slot-state.test.mjs +2 -2
- package/tests/subagent-launch-slots-static.test.mjs +1 -1
- package/tests/subagent-observability-static.test.mjs +2 -2
- package/tests/subagent-reliability-integration.test.mjs +13 -30
- package/tests/syntax-highlighting-static.test.mjs +3 -3
- package/tests/tab-close-continuity-static.test.mjs +1 -1
- package/tests/tab-metadata-ordering.test.mjs +99 -0
- package/tests/terminal-cwd-grouping-static.test.mjs +2 -2
- package/tests/terminal-tab-prompt-tooltip-static.test.mjs +2 -0
- package/tests/terminal-tab-workspace-static.test.mjs +5 -4
- package/tests/terminal-tabs-resize-static.test.mjs +1 -1
- package/tests/theme-contract.test.mjs +10 -3
- package/tests/theme-customizer-static.test.mjs +1 -1
- package/tests/thread-display-title.test.mjs +44 -0
- package/tests/update-api-harness.test.mjs +15 -14
- package/tests/update-tab-restore-harness.test.mjs +3 -0
- package/tests/webui-settings-locking.test.mjs +2 -2
- package/tests/windows-drive-cwd-picker-static.test.mjs +7 -9
- package/tests/workspace-choices.test.mjs +114 -0
- package/tests/workspace-save-load-static.test.mjs +7 -4
package/index.ts
CHANGED
|
@@ -34,6 +34,7 @@ type WebuiAddress = {
|
|
|
34
34
|
};
|
|
35
35
|
|
|
36
36
|
type StartWebuiOptions = WebuiAddress & {
|
|
37
|
+
cwd?: string;
|
|
37
38
|
open: boolean;
|
|
38
39
|
noSession: boolean;
|
|
39
40
|
remoteAuth: boolean;
|
|
@@ -124,6 +125,11 @@ function parseStartWebuiArgs(args: string): StartWebuiOptions {
|
|
|
124
125
|
i++;
|
|
125
126
|
continue;
|
|
126
127
|
}
|
|
128
|
+
if (token === "--cwd") {
|
|
129
|
+
options.cwd = takeValue(tokens, i, token);
|
|
130
|
+
i++;
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
127
133
|
if (/^\d+$/.test(token)) {
|
|
128
134
|
const port = Number.parseInt(token, 10);
|
|
129
135
|
if (!Number.isFinite(port) || port <= 0 || port > 65535) throw new Error("port must be between 1 and 65535");
|
|
@@ -459,7 +465,8 @@ function waitForWebuiUrl(child: WebuiChild, timeoutMs = START_TIMEOUT_MS): Promi
|
|
|
459
465
|
}
|
|
460
466
|
|
|
461
467
|
async function startWebui(options: StartWebuiOptions, ctx: ExtensionCommandContext, restoreTabs: RestorableWebuiTab[] = []): Promise<string> {
|
|
462
|
-
const args = [webuiBin, "--host", options.host, "--port", String(options.port)
|
|
468
|
+
const args = [webuiBin, "--host", options.host, "--port", String(options.port)];
|
|
469
|
+
if (options.cwd) args.push("--cwd", options.cwd);
|
|
463
470
|
if (options.noSession) args.push("--no-session");
|
|
464
471
|
if (options.remoteAuth) args.push("--remote-auth");
|
|
465
472
|
if (options.outputMode) args.push("--output-mode", options.outputMode);
|
|
@@ -709,8 +716,8 @@ function formatWebuiStatus(result: WebuiStatusFetchResult, requestedDetailed: bo
|
|
|
709
716
|
|
|
710
717
|
function usage(): string {
|
|
711
718
|
return [
|
|
712
|
-
"Usage: /webui-start [port] [--port N] [--no-open] [--no-session] [--remote-auth] [--output-mode normal|compact-v1] [--name NAME] [-- --model provider/model]",
|
|
713
|
-
"Starts the Pi Web UI companion server
|
|
719
|
+
"Usage: /webui-start [port] [--port N] [--cwd PATH] [--no-open] [--no-session] [--remote-auth] [--output-mode normal|compact-v1] [--name NAME] [-- --model provider/model]",
|
|
720
|
+
"Starts the Pi Web UI companion server, prints the localhost URL, and opens it in your default browser. Starts empty unless tabs are restored or --cwd PATH selects a project.",
|
|
714
721
|
].join("\n");
|
|
715
722
|
}
|
|
716
723
|
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { StringDecoder } from "node:string_decoder";
|
|
5
|
+
import { resolveCommandDirectory } from "./npm-command.mjs";
|
|
6
|
+
import { terminateProcessTree } from "./process-tree.mjs";
|
|
7
|
+
|
|
8
|
+
export const CODEX_CLI_USAGE_TIMEOUT_MS = 15_000;
|
|
9
|
+
const MAX_LINE_BYTES = 1024 * 1024;
|
|
10
|
+
const MAX_OUTPUT_BYTES = 4 * 1024 * 1024;
|
|
11
|
+
const MAX_BUCKETS = 16;
|
|
12
|
+
|
|
13
|
+
export class CodexCliUsageError extends Error {
|
|
14
|
+
constructor(code, message, statusCode = 503) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = "CodexCliUsageError";
|
|
17
|
+
this.code = code;
|
|
18
|
+
this.statusCode = statusCode;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function failure(code, message, statusCode) {
|
|
23
|
+
return new CodexCliUsageError(code, message, statusCode);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Resolve native executables and npm shims without a shell or command interpolation. */
|
|
27
|
+
export function resolveCodexCliInvocation(runtime = {}) {
|
|
28
|
+
const env = runtime.env ?? process.env;
|
|
29
|
+
const platform = runtime.platform ?? process.platform;
|
|
30
|
+
const exists = runtime.existsSync ?? existsSync;
|
|
31
|
+
const execPath = runtime.execPath ?? process.execPath;
|
|
32
|
+
const api = platform === "win32" ? path.win32 : path.posix;
|
|
33
|
+
const command = String(env.PI_WEBUI_CODEX_BIN || "codex").trim();
|
|
34
|
+
const args = ["app-server", "--listen", "stdio://"];
|
|
35
|
+
if (/\.(?:js|mjs|cjs)$/i.test(command)) return { command: execPath, args: [command, ...args] };
|
|
36
|
+
if (platform !== "win32" || /\.(?:exe|com)$/i.test(command)) return { command, args };
|
|
37
|
+
const directory = resolveCommandDirectory(command, { ...runtime, env, platform, existsSync: exists });
|
|
38
|
+
if (directory && /^codex(?:\.(?:cmd|ps1))?$/i.test(api.basename(command))) {
|
|
39
|
+
const native = api.join(directory, "codex.exe");
|
|
40
|
+
if (!api.extname(command) && exists(native)) return { command: native, args };
|
|
41
|
+
const script = api.join(directory, "node_modules", "@openai", "codex", "bin", "codex.js");
|
|
42
|
+
if (exists(script)) return { command: execPath, args: [script, ...args] };
|
|
43
|
+
}
|
|
44
|
+
if (/\.(?:cmd|bat|ps1)$/i.test(command) || directory) {
|
|
45
|
+
throw failure("CODEX_CLI_SHIM", "Codex CLI's Windows launcher cannot be used directly. Set PI_WEBUI_CODEX_BIN to codex.exe or the npm codex.js entry point.");
|
|
46
|
+
}
|
|
47
|
+
return { command, args };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function text(value, maximum = 80) {
|
|
51
|
+
return typeof value === "string" && value.length > 0 && value.length <= maximum && !/[\u0000-\u001f\u007f]/.test(value) ? value : null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function windowSnapshot(value) {
|
|
55
|
+
if (!value || typeof value !== "object" || Array.isArray(value) || typeof value.usedPercent !== "number" || !Number.isFinite(value.usedPercent) || value.usedPercent < 0 || value.usedPercent > 100) return null;
|
|
56
|
+
return {
|
|
57
|
+
usedPercent: value.usedPercent,
|
|
58
|
+
windowDurationMins: Number.isInteger(value.windowDurationMins) && value.windowDurationMins > 0 && value.windowDurationMins <= 10_000_000 ? value.windowDurationMins : null,
|
|
59
|
+
resetsAt: Number.isInteger(value.resetsAt) && value.resetsAt >= 0 && value.resetsAt <= 253402300799 ? value.resetsAt : null,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function limitSnapshot(value) {
|
|
64
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
65
|
+
const credits = value.credits;
|
|
66
|
+
return {
|
|
67
|
+
limitId: text(value.limitId),
|
|
68
|
+
limitName: text(value.limitName),
|
|
69
|
+
planType: text(value.planType),
|
|
70
|
+
rateLimitReachedType: text(value.rateLimitReachedType),
|
|
71
|
+
primary: windowSnapshot(value.primary),
|
|
72
|
+
secondary: windowSnapshot(value.secondary),
|
|
73
|
+
credits: credits && typeof credits === "object" ? {
|
|
74
|
+
hasCredits: typeof credits.hasCredits === "boolean" ? credits.hasCredits : undefined,
|
|
75
|
+
unlimited: typeof credits.unlimited === "boolean" ? credits.unlimited : undefined,
|
|
76
|
+
balance: typeof credits.balance === "string" && /^-?\d{1,32}(?:\.\d{1,16})?$/.test(credits.balance) ? credits.balance : null,
|
|
77
|
+
} : null,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Project only displayable quota fields. Account IDs, email and raw payloads stay private. */
|
|
82
|
+
export function projectCodexCliUsage(result, planType) {
|
|
83
|
+
if (!result || typeof result !== "object" || Array.isArray(result)) throw failure("CODEX_CLI_PROTOCOL", "Codex CLI returned an invalid usage response.", 502);
|
|
84
|
+
const rateLimits = limitSnapshot(result.rateLimits);
|
|
85
|
+
const byId = result.rateLimitsByLimitId;
|
|
86
|
+
const entries = byId && typeof byId === "object" && !Array.isArray(byId)
|
|
87
|
+
? Object.entries(byId).slice(0, MAX_BUCKETS).flatMap(([id, value]) => {
|
|
88
|
+
const snapshot = limitSnapshot(value);
|
|
89
|
+
return text(id) && snapshot ? [[id, snapshot]] : [];
|
|
90
|
+
}) : [];
|
|
91
|
+
if (![rateLimits, ...entries.map(([, snapshot]) => snapshot)].some((snapshot) => snapshot?.primary || snapshot?.secondary)) {
|
|
92
|
+
throw failure("CODEX_CLI_NO_USAGE", "Codex CLI returned no supported subscription usage windows.", 502);
|
|
93
|
+
}
|
|
94
|
+
return { planType: text(planType), rateLimits, rateLimitsByLimitId: Object.fromEntries(entries) };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function waitForClose(closed, timeoutMs) {
|
|
98
|
+
return new Promise((resolve) => {
|
|
99
|
+
const timer = setTimeout(() => resolve(false), timeoutMs);
|
|
100
|
+
closed.then(() => { clearTimeout(timer); resolve(true); });
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** A usage-only app-server connection. Never starts threads, turns, login flows or resets. */
|
|
105
|
+
export async function readCodexCliUsage(options = {}) {
|
|
106
|
+
const env = options.env ?? process.env;
|
|
107
|
+
const timeoutMs = options.timeoutMs ?? CODEX_CLI_USAGE_TIMEOUT_MS;
|
|
108
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs < 1 || timeoutMs > 60_000) throw new TypeError("Codex CLI usage timeout must be between 1 and 60000ms");
|
|
109
|
+
if (options.signal?.aborted) throw failure("CODEX_CLI_ABORTED", "Codex CLI usage check was cancelled.");
|
|
110
|
+
const invocation = options.invocation ?? resolveCodexCliInvocation({ env });
|
|
111
|
+
let child;
|
|
112
|
+
try {
|
|
113
|
+
child = (options.spawn ?? spawn)(invocation.command, invocation.args, {
|
|
114
|
+
cwd: options.cwd,
|
|
115
|
+
env,
|
|
116
|
+
windowsHide: true,
|
|
117
|
+
detached: process.platform !== "win32",
|
|
118
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
119
|
+
shell: false,
|
|
120
|
+
});
|
|
121
|
+
} catch {
|
|
122
|
+
throw failure("CODEX_CLI_START", "Codex CLI could not start. Check its installation and PI_WEBUI_CODEX_BIN.");
|
|
123
|
+
}
|
|
124
|
+
const pending = new Map();
|
|
125
|
+
let serial = 0;
|
|
126
|
+
let terminalError;
|
|
127
|
+
let finishClose;
|
|
128
|
+
const closed = new Promise((resolve) => { finishClose = resolve; });
|
|
129
|
+
const decoder = new StringDecoder("utf8");
|
|
130
|
+
let buffer = "";
|
|
131
|
+
let stdoutBytes = 0;
|
|
132
|
+
let stderrBytes = 0;
|
|
133
|
+
const fail = (error) => {
|
|
134
|
+
if (terminalError) return;
|
|
135
|
+
terminalError = error;
|
|
136
|
+
for (const operation of pending.values()) operation.reject(error);
|
|
137
|
+
pending.clear();
|
|
138
|
+
};
|
|
139
|
+
const write = (frame) => {
|
|
140
|
+
if (terminalError) throw terminalError;
|
|
141
|
+
child.stdin.write(`${JSON.stringify(frame)}\n`, (error) => {
|
|
142
|
+
if (error) fail(failure("CODEX_CLI_PIPE", "Codex CLI closed its usage connection."));
|
|
143
|
+
});
|
|
144
|
+
};
|
|
145
|
+
const request = (method, params) => new Promise((resolve, reject) => {
|
|
146
|
+
if (terminalError) { reject(terminalError); return; }
|
|
147
|
+
const id = ++serial;
|
|
148
|
+
pending.set(id, { resolve, reject });
|
|
149
|
+
try { write({ id, method, params }); } catch (error) { pending.delete(id); reject(error); }
|
|
150
|
+
});
|
|
151
|
+
const receive = (line) => {
|
|
152
|
+
if (!line.trim()) return;
|
|
153
|
+
let frame;
|
|
154
|
+
try { frame = JSON.parse(line); } catch { fail(failure("CODEX_CLI_PROTOCOL", "Codex CLI returned invalid usage protocol data.", 502)); return; }
|
|
155
|
+
if (!frame || typeof frame !== "object" || Array.isArray(frame)) { fail(failure("CODEX_CLI_PROTOCOL", "Codex CLI returned invalid usage protocol data.", 502)); return; }
|
|
156
|
+
if (typeof frame.method === "string") {
|
|
157
|
+
// Reject unsolicited server requests rather than launching an auth flow,
|
|
158
|
+
// approving work, or handing over credentials. Ignore notifications.
|
|
159
|
+
if (frame.id !== undefined) {
|
|
160
|
+
try { write({ id: frame.id, error: { code: -32601, message: "Usage-only client does not handle server requests." } }); } catch { /* request already failed */ }
|
|
161
|
+
}
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const operation = pending.get(frame.id);
|
|
165
|
+
if (!operation) return;
|
|
166
|
+
pending.delete(frame.id);
|
|
167
|
+
if (frame.error) operation.reject(failure("CODEX_CLI_REQUEST", "Codex CLI could not read subscription usage. Check codex login status and try again.", 502));
|
|
168
|
+
else if (!Object.hasOwn(frame, "result")) operation.reject(failure("CODEX_CLI_PROTOCOL", "Codex CLI returned an incomplete usage response.", 502));
|
|
169
|
+
else operation.resolve(frame.result);
|
|
170
|
+
};
|
|
171
|
+
child.stdout.on("data", (chunk) => {
|
|
172
|
+
if (terminalError) return;
|
|
173
|
+
stdoutBytes += chunk.length;
|
|
174
|
+
if (stdoutBytes > MAX_OUTPUT_BYTES) { fail(failure("CODEX_CLI_OUTPUT_LIMIT", "Codex CLI usage output exceeded the safe size limit.", 502)); return; }
|
|
175
|
+
buffer += decoder.write(chunk);
|
|
176
|
+
let newline;
|
|
177
|
+
while ((newline = buffer.indexOf("\n")) >= 0) {
|
|
178
|
+
const line = buffer.slice(0, newline);
|
|
179
|
+
buffer = buffer.slice(newline + 1);
|
|
180
|
+
if (Buffer.byteLength(line) > MAX_LINE_BYTES) { fail(failure("CODEX_CLI_OUTPUT_LIMIT", "Codex CLI usage output exceeded the safe size limit.", 502)); return; }
|
|
181
|
+
receive(line);
|
|
182
|
+
if (terminalError) return;
|
|
183
|
+
}
|
|
184
|
+
if (Buffer.byteLength(buffer) > MAX_LINE_BYTES) fail(failure("CODEX_CLI_OUTPUT_LIMIT", "Codex CLI usage output exceeded the safe size limit.", 502));
|
|
185
|
+
});
|
|
186
|
+
child.stderr.on("data", (chunk) => {
|
|
187
|
+
// Drain, but never retain or publish CLI diagnostics. They can contain secrets.
|
|
188
|
+
stderrBytes += chunk.length;
|
|
189
|
+
if (stderrBytes > MAX_OUTPUT_BYTES) fail(failure("CODEX_CLI_OUTPUT_LIMIT", "Codex CLI usage diagnostics exceeded the safe size limit.", 502));
|
|
190
|
+
});
|
|
191
|
+
child.stdin.on("error", () => fail(failure("CODEX_CLI_PIPE", "Codex CLI closed its usage connection.")));
|
|
192
|
+
child.stdout.on("error", () => fail(failure("CODEX_CLI_PIPE", "Codex CLI closed its usage connection.")));
|
|
193
|
+
child.stderr.on("error", () => fail(failure("CODEX_CLI_PIPE", "Codex CLI closed its usage connection.")));
|
|
194
|
+
child.on("error", (error) => fail(error?.code === "ENOENT"
|
|
195
|
+
? failure("CODEX_CLI_NOT_FOUND", "Codex CLI was not found. Install it or set PI_WEBUI_CODEX_BIN, then run codex login.")
|
|
196
|
+
: failure("CODEX_CLI_START", "Codex CLI could not start. Check its installation and PI_WEBUI_CODEX_BIN.")));
|
|
197
|
+
child.on("close", () => { finishClose(); fail(failure("CODEX_CLI_EXIT", "Codex CLI exited before completing the usage check.")); });
|
|
198
|
+
const timer = setTimeout(() => fail(failure("CODEX_CLI_TIMEOUT", "Codex CLI subscription usage check timed out.", 504)), timeoutMs);
|
|
199
|
+
const abort = () => fail(failure("CODEX_CLI_ABORTED", "Codex CLI usage check was cancelled."));
|
|
200
|
+
options.signal?.addEventListener("abort", abort, { once: true });
|
|
201
|
+
if (options.signal?.aborted) abort();
|
|
202
|
+
const terminate = options.terminate ?? terminateProcessTree;
|
|
203
|
+
const onParentExit = () => terminate(child, "SIGKILL");
|
|
204
|
+
process.once("exit", onParentExit);
|
|
205
|
+
try {
|
|
206
|
+
await request("initialize", { clientInfo: { name: "pi_webui_usage", title: "Pi Web UI usage", version: "1.0.0" } });
|
|
207
|
+
write({ method: "initialized" });
|
|
208
|
+
const account = await request("account/read", { refreshToken: false });
|
|
209
|
+
if (!account?.account) throw failure("CODEX_CLI_LOGIN_REQUIRED", "Codex CLI is not logged in. Run codex login and sign in with ChatGPT.", 401);
|
|
210
|
+
if (account.account.type !== "chatgpt") throw failure("CODEX_CLI_AUTH_TYPE", "Codex CLI subscription usage requires a ChatGPT login, not API-key or other provider authentication. Run codex login.", 401);
|
|
211
|
+
const usage = await request("account/rateLimits/read", null);
|
|
212
|
+
return projectCodexCliUsage(usage, account.account.planType);
|
|
213
|
+
} finally {
|
|
214
|
+
clearTimeout(timer);
|
|
215
|
+
options.signal?.removeEventListener("abort", abort);
|
|
216
|
+
fail(failure("CODEX_CLI_CLOSED", "Codex CLI usage connection closed."));
|
|
217
|
+
try {
|
|
218
|
+
child.stdin.end();
|
|
219
|
+
if (!await waitForClose(closed, 300)) {
|
|
220
|
+
terminate(child, "SIGKILL");
|
|
221
|
+
if (!await waitForClose(closed, 1_000)) {
|
|
222
|
+
child.stdin.destroy();
|
|
223
|
+
child.stdout.destroy();
|
|
224
|
+
child.stderr.destroy();
|
|
225
|
+
child.unref();
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
} finally {
|
|
229
|
+
process.removeListener("exit", onParentExit);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
package/lib/codex-usage-auth.mjs
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
export const CODEX_TOKEN_REFRESH_SKEW_MS = 5 * 60 * 1000;
|
|
2
|
+
export const CODEX_USAGE_DIRECT_TOKEN_MESSAGE = "Your ChatGPT login is configured, but Codex usage reporting is unavailable for Pi's Sign in with ChatGPT authentication. Check https://chatgpt.com/settings/usage. Logging in again will not enable this panel.";
|
|
3
|
+
|
|
4
|
+
function isDirectChatGPTCredential(credential) {
|
|
5
|
+
return credential?.type === "oauth" && Array.isArray(credential.scopes) && credential.scopes.includes("chatgpt.tokens.use.direct");
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function unsupportedDirectAuth(providerId, credential) {
|
|
9
|
+
return { providerId, credential, accessToken: undefined, refreshed: false, unsupportedReason: "chatgpt-direct-token" };
|
|
10
|
+
}
|
|
2
11
|
|
|
3
12
|
function privateCredentialStore(modelRuntime) {
|
|
4
13
|
// Compatibility adapter: ModelRuntime intentionally does not expose a
|
|
@@ -11,12 +20,34 @@ function privateCredentialStore(modelRuntime) {
|
|
|
11
20
|
return credentials;
|
|
12
21
|
}
|
|
13
22
|
|
|
23
|
+
export function codexUsageLoginGuidance(modelRuntime) {
|
|
24
|
+
return modelRuntime.getProvider("openai")?.auth?.oauth?.isSubscription === true
|
|
25
|
+
? "Run /login, choose OpenAI, then Sign in with ChatGPT."
|
|
26
|
+
: "Run /login and choose ChatGPT Plus/Pro (Codex Subscription).";
|
|
27
|
+
}
|
|
28
|
+
|
|
14
29
|
export async function resolveCodexUsageAuth(modelRuntime, providerId, options = {}) {
|
|
15
30
|
const now = options.now ?? Date.now();
|
|
16
31
|
const forceRefresh = options.forceRefresh === true;
|
|
17
32
|
const credentials = privateCredentialStore(modelRuntime);
|
|
18
|
-
|
|
33
|
+
let provider = modelRuntime.getProvider(providerId);
|
|
19
34
|
let stored = await credentials.read(providerId);
|
|
35
|
+
// Newer Pi versions store ChatGPT OAuth under the unified OpenAI provider.
|
|
36
|
+
// Only select that alias when the runtime supports subscription OAuth.
|
|
37
|
+
if (providerId === "openai-codex" && (stored?.type !== "oauth" || !provider?.auth?.oauth)) {
|
|
38
|
+
const openai = modelRuntime.getProvider("openai");
|
|
39
|
+
if (openai?.auth?.oauth?.isSubscription === true) {
|
|
40
|
+
const current = await credentials.read("openai");
|
|
41
|
+
if (current?.type === "oauth" || !provider?.auth?.oauth) {
|
|
42
|
+
providerId = "openai";
|
|
43
|
+
provider = openai;
|
|
44
|
+
stored = current;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
// Sign in with ChatGPT tokens target api.openai.com, not the legacy Codex
|
|
49
|
+
// usage endpoint. Do not refresh or forward them just to report this limit.
|
|
50
|
+
if (isDirectChatGPTCredential(stored)) return unsupportedDirectAuth(providerId, stored);
|
|
20
51
|
let refreshed = false;
|
|
21
52
|
|
|
22
53
|
if (stored?.type === "oauth") {
|
|
@@ -28,7 +59,7 @@ export async function resolveCodexUsageAuth(modelRuntime, providerId, options =
|
|
|
28
59
|
|
|
29
60
|
if (shouldRefresh) {
|
|
30
61
|
await credentials.modify(providerId, async (current) => {
|
|
31
|
-
if (current?.type !== "oauth") return undefined;
|
|
62
|
+
if (current?.type !== "oauth" || isDirectChatGPTCredential(current)) return undefined;
|
|
32
63
|
// Another process may have refreshed while this caller waited for the
|
|
33
64
|
// auth-file lock. Reuse that newer token rather than refreshing twice.
|
|
34
65
|
if (forceRefresh && current.access !== rejectedAccess) return undefined;
|
|
@@ -42,18 +73,23 @@ export async function resolveCodexUsageAuth(modelRuntime, providerId, options =
|
|
|
42
73
|
stored = await credentials.read(providerId);
|
|
43
74
|
}
|
|
44
75
|
|
|
76
|
+
if (isDirectChatGPTCredential(stored)) return unsupportedDirectAuth(providerId, stored);
|
|
45
77
|
if (forceRefresh && stored?.type === "oauth" && stored.access === rejectedAccess) {
|
|
46
78
|
throw new Error("Codex OAuth refresh returned the rejected credential.");
|
|
47
79
|
}
|
|
48
80
|
return {
|
|
81
|
+
providerId,
|
|
49
82
|
accessToken: stored?.type === "oauth" ? stored.access : undefined,
|
|
50
83
|
credential: stored,
|
|
51
84
|
refreshed: refreshed || (forceRefresh && stored?.type === "oauth" && stored.access !== rejectedAccess),
|
|
52
85
|
};
|
|
53
86
|
}
|
|
54
87
|
|
|
55
|
-
|
|
88
|
+
// OpenAI API keys are not ChatGPT subscription credentials. Never resolve
|
|
89
|
+
// the unified provider's API key or environment fallback for usage requests.
|
|
90
|
+
const resolved = providerId === "openai" ? undefined : await modelRuntime.getAuth(providerId);
|
|
56
91
|
return {
|
|
92
|
+
providerId,
|
|
57
93
|
accessToken: resolved?.auth?.apiKey,
|
|
58
94
|
credential: stored,
|
|
59
95
|
refreshed: false,
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { CodexCliUsageError } from "./codex-cli-usage.mjs";
|
|
2
|
+
|
|
3
|
+
export const CODEX_USAGE_CACHE_MS = 30_000;
|
|
4
|
+
|
|
5
|
+
/** Coalesce browser polls and cache only successful snapshots, never credentials or failures. */
|
|
6
|
+
export function createCodexUsageReader({ readCli, readLegacy, cacheMs = CODEX_USAGE_CACHE_MS, now = Date.now }) {
|
|
7
|
+
let cached;
|
|
8
|
+
let cachedAt = 0;
|
|
9
|
+
let inFlight;
|
|
10
|
+
return async function read(options = {}) {
|
|
11
|
+
if (inFlight) return inFlight;
|
|
12
|
+
if (!options.forceRefresh && cached && now() - cachedAt < cacheMs) return cached;
|
|
13
|
+
inFlight = (async () => {
|
|
14
|
+
let cliError;
|
|
15
|
+
try {
|
|
16
|
+
const result = await readCli();
|
|
17
|
+
cached = result;
|
|
18
|
+
cachedAt = now();
|
|
19
|
+
return result;
|
|
20
|
+
} catch (error) {
|
|
21
|
+
cliError = error instanceof CodexCliUsageError ? error : new CodexCliUsageError("CODEX_CLI_FAILED", "Codex CLI usage is unavailable.");
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
const result = await readLegacy(options);
|
|
25
|
+
cached = { ...result, sourceNotice: "Codex CLI usage is unavailable. Showing usage from Pi's legacy Codex login." };
|
|
26
|
+
cachedAt = now();
|
|
27
|
+
return cached;
|
|
28
|
+
} catch {
|
|
29
|
+
// Do not expose raw CLI diagnostics, Pi auth errors, account details,
|
|
30
|
+
// stack traces or upstream response bodies when both readers fail.
|
|
31
|
+
throw new CodexCliUsageError(cliError.code, `${cliError.message} Pi's legacy Codex usage fallback also could not provide usage. Check https://chatgpt.com/settings/usage.`, cliError.statusCode);
|
|
32
|
+
}
|
|
33
|
+
})();
|
|
34
|
+
try { return await inFlight; } finally { inFlight = undefined; }
|
|
35
|
+
};
|
|
36
|
+
}
|
|
@@ -41,10 +41,10 @@ export function validateUpdatePlanRequest(body) {
|
|
|
41
41
|
if (!body || typeof body !== "object" || Array.isArray(body)) return { ok: false, error: "Request body must be an object." };
|
|
42
42
|
const keys = Object.keys(body).sort();
|
|
43
43
|
if (keys.some((key) => key !== "targets") || !Array.isArray(body.targets) || body.targets.length !== 1) {
|
|
44
|
-
return { ok: false, error: "targets must contain exactly one value:
|
|
44
|
+
return { ok: false, error: "targets must contain exactly one value: all, webui or pi." };
|
|
45
45
|
}
|
|
46
46
|
const targets = body.targets;
|
|
47
|
-
if (!
|
|
47
|
+
if (!["all", "webui", "pi"].includes(targets[0])) return { ok: false, error: "targets contains an unsupported target." };
|
|
48
48
|
return { ok: true, targets };
|
|
49
49
|
}
|
|
50
50
|
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
export const GIT_WORKFLOW_SETUP_VERSION = 1;
|
|
2
|
+
export const GIT_WORKFLOW_THINKING_LEVELS = ["off", "minimal", "low", "medium", "high", "xhigh", "max"];
|
|
3
|
+
export const GIT_WORKFLOW_LANGUAGES = ["en", "de"];
|
|
4
|
+
export const GIT_WORKFLOW_DEFAULT_VARIANTS = ["short", "long"];
|
|
5
|
+
export const GIT_WORKFLOW_SCOPE_POLICIES = ["auto", "never", "required"];
|
|
6
|
+
export const GIT_WORKFLOW_STAGING_POLICIES = ["review", "preserve", "all"];
|
|
7
|
+
export const GIT_WORKFLOW_DELIVERY_MODES = ["ask", "current", "pr-worktree"];
|
|
8
|
+
export const GIT_WORKFLOW_VERIFICATION_POLICIES = ["ask", "none"];
|
|
9
|
+
export const GIT_WORKFLOW_UNAVAILABLE_POLICIES = ["ask"];
|
|
10
|
+
|
|
11
|
+
export function supportedGitWorkflowThinkingLevels(model) {
|
|
12
|
+
if (!model?.reasoning) return ["off"];
|
|
13
|
+
const mapping = model.thinkingLevelMap && typeof model.thinkingLevelMap === "object" ? model.thinkingLevelMap : {};
|
|
14
|
+
return GIT_WORKFLOW_THINKING_LEVELS.filter((level) => {
|
|
15
|
+
if (mapping[level] === null) return false;
|
|
16
|
+
if (["xhigh", "max"].includes(level)) return typeof mapping[level] === "string";
|
|
17
|
+
return true;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function cleanBoundedString(value, maxLength = 512) {
|
|
22
|
+
return typeof value === "string" ? value.trim().slice(0, maxLength) : "";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function choice(value, choices, fallback) {
|
|
26
|
+
const clean = cleanBoundedString(value, 64);
|
|
27
|
+
return choices.includes(clean) ? clean : fallback;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function defaultGitWorkflowPreferences() {
|
|
31
|
+
return {
|
|
32
|
+
setupVersion: 0,
|
|
33
|
+
generation: {
|
|
34
|
+
provider: "",
|
|
35
|
+
modelId: "",
|
|
36
|
+
thinkingLevel: "low",
|
|
37
|
+
unavailablePolicy: "ask",
|
|
38
|
+
fallback: {
|
|
39
|
+
provider: "",
|
|
40
|
+
modelId: "",
|
|
41
|
+
thinkingLevel: "low",
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
commit: {
|
|
45
|
+
language: "en",
|
|
46
|
+
defaultVariant: "short",
|
|
47
|
+
scope: "auto",
|
|
48
|
+
},
|
|
49
|
+
stagingPolicy: "review",
|
|
50
|
+
reviewProcessEnabled: true,
|
|
51
|
+
deliveryMode: "ask",
|
|
52
|
+
verificationPolicy: "ask",
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function normalizeGitWorkflowPreferences(value) {
|
|
57
|
+
const defaults = defaultGitWorkflowPreferences();
|
|
58
|
+
const provider = cleanBoundedString(value?.generation?.provider, 160);
|
|
59
|
+
const modelId = cleanBoundedString(value?.generation?.modelId, 512);
|
|
60
|
+
const fallbackProvider = cleanBoundedString(value?.generation?.fallback?.provider, 160);
|
|
61
|
+
const fallbackModelId = cleanBoundedString(value?.generation?.fallback?.modelId, 512);
|
|
62
|
+
return {
|
|
63
|
+
setupVersion: provider && modelId ? GIT_WORKFLOW_SETUP_VERSION : 0,
|
|
64
|
+
generation: {
|
|
65
|
+
provider,
|
|
66
|
+
modelId,
|
|
67
|
+
thinkingLevel: choice(value?.generation?.thinkingLevel, GIT_WORKFLOW_THINKING_LEVELS, defaults.generation.thinkingLevel),
|
|
68
|
+
unavailablePolicy: choice(value?.generation?.unavailablePolicy, GIT_WORKFLOW_UNAVAILABLE_POLICIES, defaults.generation.unavailablePolicy),
|
|
69
|
+
fallback: {
|
|
70
|
+
provider: fallbackProvider,
|
|
71
|
+
modelId: fallbackModelId,
|
|
72
|
+
thinkingLevel: choice(value?.generation?.fallback?.thinkingLevel, GIT_WORKFLOW_THINKING_LEVELS, defaults.generation.fallback.thinkingLevel),
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
commit: {
|
|
76
|
+
language: choice(value?.commit?.language, GIT_WORKFLOW_LANGUAGES, defaults.commit.language),
|
|
77
|
+
defaultVariant: choice(value?.commit?.defaultVariant, GIT_WORKFLOW_DEFAULT_VARIANTS, defaults.commit.defaultVariant),
|
|
78
|
+
scope: choice(value?.commit?.scope, GIT_WORKFLOW_SCOPE_POLICIES, defaults.commit.scope),
|
|
79
|
+
},
|
|
80
|
+
stagingPolicy: choice(value?.stagingPolicy, GIT_WORKFLOW_STAGING_POLICIES, defaults.stagingPolicy),
|
|
81
|
+
reviewProcessEnabled: typeof value?.reviewProcessEnabled === "boolean" ? value.reviewProcessEnabled : defaults.reviewProcessEnabled,
|
|
82
|
+
deliveryMode: choice(value?.deliveryMode, GIT_WORKFLOW_DELIVERY_MODES, defaults.deliveryMode),
|
|
83
|
+
verificationPolicy: choice(value?.verificationPolicy, GIT_WORKFLOW_VERIFICATION_POLICIES, defaults.verificationPolicy),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function mergeGitWorkflowPreferences(current, patch) {
|
|
88
|
+
const base = normalizeGitWorkflowPreferences(current);
|
|
89
|
+
return normalizeGitWorkflowPreferences({
|
|
90
|
+
...base,
|
|
91
|
+
...(patch || {}),
|
|
92
|
+
generation: {
|
|
93
|
+
...base.generation,
|
|
94
|
+
...(patch?.generation || {}),
|
|
95
|
+
fallback: { ...base.generation.fallback, ...(patch?.generation?.fallback || {}) },
|
|
96
|
+
},
|
|
97
|
+
commit: { ...base.commit, ...(patch?.commit || {}) },
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function isGitWorkflowSetupComplete(preferences) {
|
|
102
|
+
const normalized = normalizeGitWorkflowPreferences(preferences);
|
|
103
|
+
return normalized.setupVersion === GIT_WORKFLOW_SETUP_VERSION && !!normalized.generation.provider && !!normalized.generation.modelId;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function gitWorkflowPreferencesSummary(preferences) {
|
|
107
|
+
const value = normalizeGitWorkflowPreferences(preferences);
|
|
108
|
+
const model = isGitWorkflowSetupComplete(value) ? `${value.generation.provider}/${value.generation.modelId}` : "not configured";
|
|
109
|
+
const fallback = value.generation.fallback.provider && value.generation.fallback.modelId
|
|
110
|
+
? `${value.generation.fallback.provider}/${value.generation.fallback.modelId} · ${value.generation.fallback.thinkingLevel}`
|
|
111
|
+
: "disabled";
|
|
112
|
+
return [
|
|
113
|
+
`Model: ${model}`,
|
|
114
|
+
`Thinking: ${value.generation.thinkingLevel}`,
|
|
115
|
+
`Fallback: ${fallback}`,
|
|
116
|
+
`Commit: ${value.commit.language} · ${value.commit.defaultVariant} · scope ${value.commit.scope}`,
|
|
117
|
+
`Staging: ${value.stagingPolicy}`,
|
|
118
|
+
`Review process: ${value.reviewProcessEnabled ? "enabled" : "disabled"}`,
|
|
119
|
+
`Delivery: ${value.deliveryMode}`,
|
|
120
|
+
`Verification: ${value.verificationPolicy}`,
|
|
121
|
+
].join("\n");
|
|
122
|
+
}
|