@promptbook/cli 0.114.0-21 → 0.114.0-22
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/esm/index.es.js +583 -2
- package/esm/index.es.js.map +1 -1
- package/esm/scripts/run-codex-prompts/runners/claude-code/ClaudeCodeRunner.d.ts +15 -0
- package/esm/scripts/run-codex-prompts/runners/claude-code/parseClaudeCodeOutputEvents.d.ts +3 -0
- package/esm/scripts/run-codex-prompts/runners/claude-code/parseClaudeCodeSubscriptionUsage.d.ts +11 -0
- package/esm/scripts/run-codex-prompts/runners/openai-codex/OpenAiCodexRunner.d.ts +8 -0
- package/esm/scripts/run-codex-prompts/runners/openai-codex/getCodexSubscriptionUsage.d.ts +19 -0
- package/esm/scripts/run-codex-prompts/runners/types/HarnessSubscriptionUsage.d.ts +43 -0
- package/esm/scripts/run-codex-prompts/ui/CoderRunUiState.d.ts +6 -0
- package/esm/scripts/run-codex-prompts/ui/buildCoderRunUiFrame.d.ts +2 -0
- package/esm/scripts/run-codex-prompts/ui/buildSubscriptionUsageSessionRows.d.ts +11 -0
- package/esm/scripts/run-codex-prompts/ui/refreshCoderRunUiSubscriptionUsage.d.ts +14 -0
- package/esm/src/version.d.ts +1 -1
- package/package.json +1 -1
- package/src/other/templates/getTemplatesPipelineCollection.ts +720 -957
- package/src/version.ts +2 -2
- package/src/versions.txt +1 -0
- package/umd/index.umd.js +582 -1
- package/umd/index.umd.js.map +1 -1
- package/umd/scripts/run-codex-prompts/runners/claude-code/ClaudeCodeRunner.d.ts +15 -0
- package/umd/scripts/run-codex-prompts/runners/claude-code/parseClaudeCodeOutputEvents.d.ts +3 -0
- package/umd/scripts/run-codex-prompts/runners/claude-code/parseClaudeCodeSubscriptionUsage.d.ts +11 -0
- package/umd/scripts/run-codex-prompts/runners/openai-codex/OpenAiCodexRunner.d.ts +8 -0
- package/umd/scripts/run-codex-prompts/runners/openai-codex/getCodexSubscriptionUsage.d.ts +19 -0
- package/umd/scripts/run-codex-prompts/runners/types/HarnessSubscriptionUsage.d.ts +43 -0
- package/umd/scripts/run-codex-prompts/ui/CoderRunUiState.d.ts +6 -0
- package/umd/scripts/run-codex-prompts/ui/buildCoderRunUiFrame.d.ts +2 -0
- package/umd/scripts/run-codex-prompts/ui/buildSubscriptionUsageSessionRows.d.ts +11 -0
- package/umd/scripts/run-codex-prompts/ui/refreshCoderRunUiSubscriptionUsage.d.ts +14 -0
- package/umd/src/version.d.ts +1 -1
package/esm/index.es.js
CHANGED
|
@@ -10,7 +10,7 @@ import * as fs from 'fs';
|
|
|
10
10
|
import { createWriteStream, mkdirSync, writeFileSync, statSync, readFileSync, existsSync, promises } from 'fs';
|
|
11
11
|
import * as dotenv from 'dotenv';
|
|
12
12
|
import * as readline from 'readline';
|
|
13
|
-
import {
|
|
13
|
+
import { createInterface, emitKeypressEvents, cursorTo, clearLine } from 'readline';
|
|
14
14
|
import { forTime, forEver } from 'waitasecond';
|
|
15
15
|
import hexEncoder from 'crypto-js/enc-hex';
|
|
16
16
|
import sha256 from 'crypto-js/sha256';
|
|
@@ -48,7 +48,7 @@ const BOOK_LANGUAGE_VERSION = '2.0.0';
|
|
|
48
48
|
* @generated
|
|
49
49
|
* @see https://github.com/webgptorg/promptbook
|
|
50
50
|
*/
|
|
51
|
-
const PROMPTBOOK_ENGINE_VERSION = '0.114.0-
|
|
51
|
+
const PROMPTBOOK_ENGINE_VERSION = '0.114.0-22';
|
|
52
52
|
/**
|
|
53
53
|
* TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
|
|
54
54
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
@@ -25547,6 +25547,29 @@ async function waitForSkippableWorldTimeDeadline(options) {
|
|
|
25547
25547
|
}
|
|
25548
25548
|
}
|
|
25549
25549
|
|
|
25550
|
+
/**
|
|
25551
|
+
* Merges a newly reported subset of subscription windows into the latest complete snapshot.
|
|
25552
|
+
*
|
|
25553
|
+
* Some harnesses emit an update only for the limit whose state changed. Keeping the previous windows means a
|
|
25554
|
+
* five-hour update cannot make a still-valid weekly limit disappear from the dashboard. Existing order is preserved
|
|
25555
|
+
* and newly discovered windows are appended, which keeps the terminal presentation stable between refreshes.
|
|
25556
|
+
*
|
|
25557
|
+
* @private internal utility of `ptbk coder`
|
|
25558
|
+
*/
|
|
25559
|
+
function mergeHarnessSubscriptionUsage(previousSubscriptionUsage, latestSubscriptionUsage) {
|
|
25560
|
+
if (previousSubscriptionUsage === undefined) {
|
|
25561
|
+
return latestSubscriptionUsage;
|
|
25562
|
+
}
|
|
25563
|
+
const latestLimitsByLabel = new Map(latestSubscriptionUsage.limits.map((latestLimit) => [latestLimit.label, latestLimit]));
|
|
25564
|
+
const previousLimitLabels = new Set(previousSubscriptionUsage.limits.map((previousLimit) => previousLimit.label));
|
|
25565
|
+
return {
|
|
25566
|
+
limits: [
|
|
25567
|
+
...previousSubscriptionUsage.limits.map((previousLimit) => { var _a; return (_a = latestLimitsByLabel.get(previousLimit.label)) !== null && _a !== void 0 ? _a : previousLimit; }),
|
|
25568
|
+
...latestSubscriptionUsage.limits.filter((latestLimit) => !previousLimitLabels.has(latestLimit.label)),
|
|
25569
|
+
],
|
|
25570
|
+
};
|
|
25571
|
+
}
|
|
25572
|
+
|
|
25550
25573
|
/**
|
|
25551
25574
|
* Base delimiter used for passing large prompts through stdin.
|
|
25552
25575
|
*/
|
|
@@ -25880,6 +25903,122 @@ function parseClaudeCodeJsonOutput(output) {
|
|
|
25880
25903
|
}
|
|
25881
25904
|
}
|
|
25882
25905
|
|
|
25906
|
+
/**
|
|
25907
|
+
* Human-readable labels for known Claude Code rolling subscription-limit windows.
|
|
25908
|
+
*/
|
|
25909
|
+
const CLAUDE_CODE_RATE_LIMIT_LABELS = {
|
|
25910
|
+
five_hour: '5h',
|
|
25911
|
+
seven_day: '7d',
|
|
25912
|
+
seven_day_opus: '7d Opus',
|
|
25913
|
+
seven_day_sonnet: '7d Sonnet',
|
|
25914
|
+
overage: 'Overage',
|
|
25915
|
+
};
|
|
25916
|
+
/**
|
|
25917
|
+
* Fallback label used when a newer Claude Code release adds a rate-limit window unknown to Promptbook.
|
|
25918
|
+
*/
|
|
25919
|
+
const UNKNOWN_CLAUDE_CODE_RATE_LIMIT_LABEL = 'Limit';
|
|
25920
|
+
/**
|
|
25921
|
+
* Converts Claude Code stream-json rate-limit events into the shared subscription-usage snapshot.
|
|
25922
|
+
*
|
|
25923
|
+
* Claude Code can emit one event for each applicable limit, so the parser preserves every distinct window instead of
|
|
25924
|
+
* assuming the familiar 5-hour and seven-day pair. This lets a new vendor-side limit appear in the dashboard without
|
|
25925
|
+
* changing its renderer.
|
|
25926
|
+
*
|
|
25927
|
+
* @private internal utility of the Claude Code runner
|
|
25928
|
+
*/
|
|
25929
|
+
function parseClaudeCodeSubscriptionUsage(output) {
|
|
25930
|
+
const limitsByType = new Map();
|
|
25931
|
+
for (const event of parseClaudeCodeOutputEvents(output)) {
|
|
25932
|
+
const limit = parseClaudeCodeSubscriptionUsageLimit(event);
|
|
25933
|
+
if (limit) {
|
|
25934
|
+
limitsByType.set(resolveClaudeCodeRateLimitType(event), limit);
|
|
25935
|
+
}
|
|
25936
|
+
}
|
|
25937
|
+
const limits = [...limitsByType.values()];
|
|
25938
|
+
return limits.length === 0 ? undefined : { limits };
|
|
25939
|
+
}
|
|
25940
|
+
/**
|
|
25941
|
+
* Parses one Claude Code stream event when it contains a usable subscription-limit update.
|
|
25942
|
+
*
|
|
25943
|
+
* @private helper of `parseClaudeCodeSubscriptionUsage`
|
|
25944
|
+
*/
|
|
25945
|
+
function parseClaudeCodeSubscriptionUsageLimit(event) {
|
|
25946
|
+
if (event.type !== 'rate_limit_event' || !event.rate_limit_info) {
|
|
25947
|
+
return undefined;
|
|
25948
|
+
}
|
|
25949
|
+
const rateLimitType = resolveClaudeCodeRateLimitType(event);
|
|
25950
|
+
const usedPercentage = resolveClaudeCodeUsedPercentage(event.rate_limit_info);
|
|
25951
|
+
if (usedPercentage === undefined) {
|
|
25952
|
+
return undefined;
|
|
25953
|
+
}
|
|
25954
|
+
const resetsAt = resolveClaudeCodeResetTimestamp(event.rate_limit_info);
|
|
25955
|
+
return {
|
|
25956
|
+
label: formatClaudeCodeRateLimitTypeLabel(rateLimitType),
|
|
25957
|
+
usedPercentage,
|
|
25958
|
+
...(resetsAt !== undefined && { resetsAt }),
|
|
25959
|
+
};
|
|
25960
|
+
}
|
|
25961
|
+
/**
|
|
25962
|
+
* Formats known and newly introduced Claude Code rate-limit types for a stable terminal label.
|
|
25963
|
+
*
|
|
25964
|
+
* A newer limit type remains distinct in the dashboard instead of making several unrelated windows all appear as
|
|
25965
|
+
* a generic `Limit` row.
|
|
25966
|
+
*
|
|
25967
|
+
* @private helper of `parseClaudeCodeSubscriptionUsage`
|
|
25968
|
+
*/
|
|
25969
|
+
function formatClaudeCodeRateLimitTypeLabel(rateLimitType) {
|
|
25970
|
+
const knownLabel = CLAUDE_CODE_RATE_LIMIT_LABELS[rateLimitType];
|
|
25971
|
+
if (knownLabel) {
|
|
25972
|
+
return knownLabel;
|
|
25973
|
+
}
|
|
25974
|
+
const words = rateLimitType.split(/[\s_-]+/u).filter(Boolean);
|
|
25975
|
+
if (words.length === 0) {
|
|
25976
|
+
return UNKNOWN_CLAUDE_CODE_RATE_LIMIT_LABEL;
|
|
25977
|
+
}
|
|
25978
|
+
return words.map((word) => `${word.slice(0, 1).toUpperCase()}${word.slice(1)}`).join(' ');
|
|
25979
|
+
}
|
|
25980
|
+
/**
|
|
25981
|
+
* Resolves the stable rate-limit type key emitted by Claude Code across camel- and snake-case payload versions.
|
|
25982
|
+
*
|
|
25983
|
+
* @private helper of `parseClaudeCodeSubscriptionUsage`
|
|
25984
|
+
*/
|
|
25985
|
+
function resolveClaudeCodeRateLimitType(event) {
|
|
25986
|
+
var _a;
|
|
25987
|
+
const rateLimitInfo = event.rate_limit_info;
|
|
25988
|
+
const rateLimitType = (_a = rateLimitInfo === null || rateLimitInfo === void 0 ? void 0 : rateLimitInfo.rateLimitType) !== null && _a !== void 0 ? _a : rateLimitInfo === null || rateLimitInfo === void 0 ? void 0 : rateLimitInfo.rate_limit_type;
|
|
25989
|
+
return typeof rateLimitType === 'string' && rateLimitType.trim() !== ''
|
|
25990
|
+
? rateLimitType.trim()
|
|
25991
|
+
: UNKNOWN_CLAUDE_CODE_RATE_LIMIT_LABEL;
|
|
25992
|
+
}
|
|
25993
|
+
/**
|
|
25994
|
+
* Converts Claude's fractional utilization value to a displayed percentage.
|
|
25995
|
+
*
|
|
25996
|
+
* A rejected limit event may omit utilization, but it still conveys a precise zero-remaining state and is therefore
|
|
25997
|
+
* rendered as 100 percent consumed.
|
|
25998
|
+
*
|
|
25999
|
+
* @private helper of `parseClaudeCodeSubscriptionUsage`
|
|
26000
|
+
*/
|
|
26001
|
+
function resolveClaudeCodeUsedPercentage(rateLimitInfo) {
|
|
26002
|
+
const utilization = rateLimitInfo.utilization;
|
|
26003
|
+
if (typeof utilization === 'number' && Number.isFinite(utilization)) {
|
|
26004
|
+
const usedPercentage = utilization <= 1 ? utilization * 100 : utilization;
|
|
26005
|
+
if (usedPercentage >= 0 && usedPercentage <= 100) {
|
|
26006
|
+
return usedPercentage;
|
|
26007
|
+
}
|
|
26008
|
+
}
|
|
26009
|
+
return rateLimitInfo.status === 'rejected' ? 100 : undefined;
|
|
26010
|
+
}
|
|
26011
|
+
/**
|
|
26012
|
+
* Reads Claude's optional Unix reset timestamp from either current or earlier stream payload naming.
|
|
26013
|
+
*
|
|
26014
|
+
* @private helper of `parseClaudeCodeSubscriptionUsage`
|
|
26015
|
+
*/
|
|
26016
|
+
function resolveClaudeCodeResetTimestamp(rateLimitInfo) {
|
|
26017
|
+
var _a;
|
|
26018
|
+
const resetsAt = (_a = rateLimitInfo.resetsAt) !== null && _a !== void 0 ? _a : rateLimitInfo.resets_at;
|
|
26019
|
+
return typeof resetsAt === 'number' && Number.isFinite(resetsAt) && resetsAt > 0 ? resetsAt : undefined;
|
|
26020
|
+
}
|
|
26021
|
+
|
|
25883
26022
|
/**
|
|
25884
26023
|
* Polling interval used while waiting for Claude Code session limits to reset.
|
|
25885
26024
|
*/
|
|
@@ -25895,6 +26034,14 @@ class ClaudeCodeRunner {
|
|
|
25895
26034
|
this.options = options;
|
|
25896
26035
|
this.name = 'claude-code';
|
|
25897
26036
|
}
|
|
26037
|
+
/**
|
|
26038
|
+
* Returns the latest subscription-limit snapshot emitted by this Claude Code session.
|
|
26039
|
+
*
|
|
26040
|
+
* Claude exposes these values in the normal stream after a response, so no separate quota-only model call is made.
|
|
26041
|
+
*/
|
|
26042
|
+
async getSubscriptionUsage() {
|
|
26043
|
+
return this.subscriptionUsage;
|
|
26044
|
+
}
|
|
25898
26045
|
/**
|
|
25899
26046
|
* Runs the prompt using Claude Code and parses usage output.
|
|
25900
26047
|
*/
|
|
@@ -25908,6 +26055,7 @@ class ClaudeCodeRunner {
|
|
|
25908
26055
|
prompt,
|
|
25909
26056
|
resumeSessionId,
|
|
25910
26057
|
}).catch(async (error) => {
|
|
26058
|
+
this.updateSubscriptionUsage(error instanceof Error ? error.message : String(error));
|
|
25911
26059
|
const sessionLimit = extractClaudeCodeSessionLimitFromError(error);
|
|
25912
26060
|
if (!sessionLimit) {
|
|
25913
26061
|
throw error;
|
|
@@ -25922,6 +26070,7 @@ class ClaudeCodeRunner {
|
|
|
25922
26070
|
continue;
|
|
25923
26071
|
}
|
|
25924
26072
|
const sessionLimit = extractClaudeCodeSessionLimitFromOutput(output);
|
|
26073
|
+
this.updateSubscriptionUsage(output);
|
|
25925
26074
|
if (sessionLimit) {
|
|
25926
26075
|
resurrectionCount++;
|
|
25927
26076
|
await waitForClaudeCodeSessionLimitReset(sessionLimit, resurrectionCount, options);
|
|
@@ -25951,6 +26100,18 @@ class ClaudeCodeRunner {
|
|
|
25951
26100
|
preserveArtifactsOnSuccess: options.preserveArtifactsOnSuccess,
|
|
25952
26101
|
});
|
|
25953
26102
|
}
|
|
26103
|
+
/**
|
|
26104
|
+
* Keeps the newest usable Claude subscription-limit values reported by the stream.
|
|
26105
|
+
*
|
|
26106
|
+
* A stream can omit these values for API-key users or unsupported plan types; retaining the prior snapshot avoids
|
|
26107
|
+
* a temporary omission erasing a still-valid dashboard value while a long queue is running.
|
|
26108
|
+
*/
|
|
26109
|
+
updateSubscriptionUsage(output) {
|
|
26110
|
+
const subscriptionUsage = parseClaudeCodeSubscriptionUsage(output);
|
|
26111
|
+
if (subscriptionUsage) {
|
|
26112
|
+
this.subscriptionUsage = mergeHarnessSubscriptionUsage(this.subscriptionUsage, subscriptionUsage);
|
|
26113
|
+
}
|
|
26114
|
+
}
|
|
25954
26115
|
}
|
|
25955
26116
|
/**
|
|
25956
26117
|
* Waits until the Claude Code session can be resumed, keeping terminal status clear.
|
|
@@ -26858,6 +27019,292 @@ function resolveCodexPricing(modelName) {
|
|
|
26858
27019
|
return (_c = OPENAI_MODELS.find((model) => model.modelName === CODEX_FALLBACK_PRICING_MODEL)) === null || _c === void 0 ? void 0 : _c.pricing;
|
|
26859
27020
|
}
|
|
26860
27021
|
|
|
27022
|
+
/**
|
|
27023
|
+
* Arguments which start the Codex app server over JSON-RPC stdio.
|
|
27024
|
+
*/
|
|
27025
|
+
const CODEX_APP_SERVER_ARGUMENTS = ['app-server', '--stdio'];
|
|
27026
|
+
/**
|
|
27027
|
+
* Maximum time spent waiting for the optional Codex account-rate-limit snapshot.
|
|
27028
|
+
*/
|
|
27029
|
+
const CODEX_SUBSCRIPTION_USAGE_REQUEST_TIMEOUT_MS = 10 * 1000;
|
|
27030
|
+
/**
|
|
27031
|
+
* JSON-RPC request id used for the required app-server initialization handshake.
|
|
27032
|
+
*/
|
|
27033
|
+
const INITIALIZE_REQUEST_ID = 1;
|
|
27034
|
+
/**
|
|
27035
|
+
* JSON-RPC request id used for the Codex account rate-limit snapshot.
|
|
27036
|
+
*/
|
|
27037
|
+
const RATE_LIMITS_REQUEST_ID = 2;
|
|
27038
|
+
/**
|
|
27039
|
+
* Client name sent to the Codex app server while Promptbook reads subscription limits.
|
|
27040
|
+
*/
|
|
27041
|
+
const PROMPTBOOK_CODEX_APP_SERVER_CLIENT_NAME = 'ptbk-coder';
|
|
27042
|
+
/**
|
|
27043
|
+
* Client version sent to the Codex app server while Promptbook reads subscription limits.
|
|
27044
|
+
*
|
|
27045
|
+
* The app-server protocol only requires a non-empty version string, and the CLI's own version is intentionally not
|
|
27046
|
+
* coupled to Promptbook's package version.
|
|
27047
|
+
*/
|
|
27048
|
+
const PROMPTBOOK_CODEX_APP_SERVER_CLIENT_VERSION = '1';
|
|
27049
|
+
/**
|
|
27050
|
+
* Reads the current Codex subscription limit windows through its local app-server protocol.
|
|
27051
|
+
*
|
|
27052
|
+
* Codex versions without this optional protocol, API-key sessions, and transient account failures simply provide no
|
|
27053
|
+
* snapshot. Subscription usage is contextual UI information, so it must never make a coding prompt fail.
|
|
27054
|
+
*
|
|
27055
|
+
* @private internal utility of the OpenAI Codex runner
|
|
27056
|
+
*/
|
|
27057
|
+
async function getCodexSubscriptionUsage(codexCommand) {
|
|
27058
|
+
try {
|
|
27059
|
+
return await requestCodexSubscriptionUsage(codexCommand);
|
|
27060
|
+
}
|
|
27061
|
+
catch (_a) {
|
|
27062
|
+
return undefined;
|
|
27063
|
+
}
|
|
27064
|
+
}
|
|
27065
|
+
/**
|
|
27066
|
+
* Performs the small Codex app-server JSON-RPC exchange which reads rate limits.
|
|
27067
|
+
*
|
|
27068
|
+
* The app server requires a completed `initialize` handshake before it accepts account methods. Closing standard input
|
|
27069
|
+
* after the response lets the short-lived helper exit without sharing the lifecycle of the coding process itself.
|
|
27070
|
+
*
|
|
27071
|
+
* @private helper of `getCodexSubscriptionUsage`
|
|
27072
|
+
*/
|
|
27073
|
+
function requestCodexSubscriptionUsage(codexCommand) {
|
|
27074
|
+
return new Promise((resolve) => {
|
|
27075
|
+
const codexAppServerProcess = spawn(codexCommand, CODEX_APP_SERVER_ARGUMENTS, {
|
|
27076
|
+
shell: process.platform === 'win32',
|
|
27077
|
+
stdio: 'pipe',
|
|
27078
|
+
});
|
|
27079
|
+
const outputReader = createInterface({ input: codexAppServerProcess.stdout });
|
|
27080
|
+
let requestTimeout;
|
|
27081
|
+
let isSettled = false;
|
|
27082
|
+
// Draining stderr prevents a protocol diagnostic from blocking the short-lived child process. The response is
|
|
27083
|
+
// deliberately not surfaced because missing usage must not distract from an otherwise healthy coding run.
|
|
27084
|
+
codexAppServerProcess.stderr.resume();
|
|
27085
|
+
codexAppServerProcess.stdin.on('error', () => undefined);
|
|
27086
|
+
/**
|
|
27087
|
+
* Stops the helper process and settles this optional request exactly once.
|
|
27088
|
+
*/
|
|
27089
|
+
const settle = (subscriptionUsage) => {
|
|
27090
|
+
if (isSettled) {
|
|
27091
|
+
return;
|
|
27092
|
+
}
|
|
27093
|
+
isSettled = true;
|
|
27094
|
+
if (requestTimeout) {
|
|
27095
|
+
clearTimeout(requestTimeout);
|
|
27096
|
+
}
|
|
27097
|
+
outputReader.close();
|
|
27098
|
+
codexAppServerProcess.stdin.end();
|
|
27099
|
+
if (codexAppServerProcess.exitCode === null && !codexAppServerProcess.killed) {
|
|
27100
|
+
codexAppServerProcess.kill();
|
|
27101
|
+
}
|
|
27102
|
+
resolve(subscriptionUsage);
|
|
27103
|
+
};
|
|
27104
|
+
/**
|
|
27105
|
+
* Sends one JSON-RPC message to the Codex app server.
|
|
27106
|
+
*/
|
|
27107
|
+
const sendJsonRpcMessage = (message) => {
|
|
27108
|
+
try {
|
|
27109
|
+
codexAppServerProcess.stdin.write(`${JSON.stringify(message)}\n`);
|
|
27110
|
+
}
|
|
27111
|
+
catch (_a) {
|
|
27112
|
+
settle(undefined);
|
|
27113
|
+
}
|
|
27114
|
+
};
|
|
27115
|
+
requestTimeout = setTimeout(() => settle(undefined), CODEX_SUBSCRIPTION_USAGE_REQUEST_TIMEOUT_MS);
|
|
27116
|
+
codexAppServerProcess.on('error', () => settle(undefined));
|
|
27117
|
+
codexAppServerProcess.on('close', () => settle(undefined));
|
|
27118
|
+
outputReader.on('line', (line) => {
|
|
27119
|
+
const response = parseJsonRecord(line);
|
|
27120
|
+
if (!response) {
|
|
27121
|
+
return;
|
|
27122
|
+
}
|
|
27123
|
+
if (response.id === INITIALIZE_REQUEST_ID) {
|
|
27124
|
+
if (!isJsonRecord(response.result)) {
|
|
27125
|
+
settle(undefined);
|
|
27126
|
+
return;
|
|
27127
|
+
}
|
|
27128
|
+
sendJsonRpcMessage({ jsonrpc: '2.0', method: 'initialized' });
|
|
27129
|
+
sendJsonRpcMessage({
|
|
27130
|
+
jsonrpc: '2.0',
|
|
27131
|
+
id: RATE_LIMITS_REQUEST_ID,
|
|
27132
|
+
method: 'account/rateLimits/read',
|
|
27133
|
+
});
|
|
27134
|
+
return;
|
|
27135
|
+
}
|
|
27136
|
+
if (response.id === RATE_LIMITS_REQUEST_ID) {
|
|
27137
|
+
settle(buildCodexSubscriptionUsage(response.result));
|
|
27138
|
+
}
|
|
27139
|
+
});
|
|
27140
|
+
sendJsonRpcMessage({
|
|
27141
|
+
jsonrpc: '2.0',
|
|
27142
|
+
id: INITIALIZE_REQUEST_ID,
|
|
27143
|
+
method: 'initialize',
|
|
27144
|
+
params: {
|
|
27145
|
+
clientInfo: {
|
|
27146
|
+
name: PROMPTBOOK_CODEX_APP_SERVER_CLIENT_NAME,
|
|
27147
|
+
version: PROMPTBOOK_CODEX_APP_SERVER_CLIENT_VERSION,
|
|
27148
|
+
},
|
|
27149
|
+
capabilities: {
|
|
27150
|
+
experimentalApi: true,
|
|
27151
|
+
},
|
|
27152
|
+
},
|
|
27153
|
+
});
|
|
27154
|
+
});
|
|
27155
|
+
}
|
|
27156
|
+
/**
|
|
27157
|
+
* Converts the raw Codex app-server rate-limit response into Promptbook's harness-neutral subscription snapshot.
|
|
27158
|
+
*
|
|
27159
|
+
* Modern Codex versions can report several metered buckets, while older versions expose a single compatibility
|
|
27160
|
+
* bucket. In both cases every primary and secondary rolling window is preserved for the terminal dashboard.
|
|
27161
|
+
*
|
|
27162
|
+
* @private internal utility of the OpenAI Codex runner
|
|
27163
|
+
*/
|
|
27164
|
+
function buildCodexSubscriptionUsage(response) {
|
|
27165
|
+
if (!isJsonRecord(response)) {
|
|
27166
|
+
return undefined;
|
|
27167
|
+
}
|
|
27168
|
+
const snapshots = resolveCodexRateLimitSnapshots(response);
|
|
27169
|
+
const hasMultipleSnapshots = snapshots.length > 1;
|
|
27170
|
+
const limits = snapshots.flatMap(({ identifier, value }) => buildCodexSubscriptionUsageLimits({
|
|
27171
|
+
snapshot: value,
|
|
27172
|
+
identifier,
|
|
27173
|
+
hasMultipleSnapshots,
|
|
27174
|
+
}));
|
|
27175
|
+
return limits.length === 0 ? undefined : { limits };
|
|
27176
|
+
}
|
|
27177
|
+
/**
|
|
27178
|
+
* Resolves either Codex's multi-bucket rate-limit response or its compatible single-bucket fallback.
|
|
27179
|
+
*
|
|
27180
|
+
* @private helper of `buildCodexSubscriptionUsage`
|
|
27181
|
+
*/
|
|
27182
|
+
function resolveCodexRateLimitSnapshots(response) {
|
|
27183
|
+
const rateLimitsByLimitId = response.rateLimitsByLimitId;
|
|
27184
|
+
if (isJsonRecord(rateLimitsByLimitId)) {
|
|
27185
|
+
const snapshots = Object.entries(rateLimitsByLimitId)
|
|
27186
|
+
.filter(([, value]) => isJsonRecord(value))
|
|
27187
|
+
.map(([identifier, value]) => ({ identifier, value: value }));
|
|
27188
|
+
if (snapshots.length > 0) {
|
|
27189
|
+
return snapshots;
|
|
27190
|
+
}
|
|
27191
|
+
}
|
|
27192
|
+
return isJsonRecord(response.rateLimits) ? [{ value: response.rateLimits }] : [];
|
|
27193
|
+
}
|
|
27194
|
+
/**
|
|
27195
|
+
* Creates every displayed rolling window from one Codex rate-limit bucket.
|
|
27196
|
+
*
|
|
27197
|
+
* @private helper of `buildCodexSubscriptionUsage`
|
|
27198
|
+
*/
|
|
27199
|
+
function buildCodexSubscriptionUsageLimits(options) {
|
|
27200
|
+
var _a;
|
|
27201
|
+
const { snapshot, identifier, hasMultipleSnapshots } = options;
|
|
27202
|
+
const bucketLabel = hasMultipleSnapshots ? (_a = readString(snapshot.limitName)) !== null && _a !== void 0 ? _a : identifier : undefined;
|
|
27203
|
+
return [
|
|
27204
|
+
buildCodexSubscriptionUsageLimit(snapshot.primary, bucketLabel, 'Primary'),
|
|
27205
|
+
buildCodexSubscriptionUsageLimit(snapshot.secondary, bucketLabel, 'Secondary'),
|
|
27206
|
+
].filter((limit) => limit !== undefined);
|
|
27207
|
+
}
|
|
27208
|
+
/**
|
|
27209
|
+
* Converts one raw Codex rate-limit window to a displayable subscription usage limit.
|
|
27210
|
+
*
|
|
27211
|
+
* @private helper of `buildCodexSubscriptionUsage`
|
|
27212
|
+
*/
|
|
27213
|
+
function buildCodexSubscriptionUsageLimit(rawWindow, bucketLabel, fallbackWindowLabel) {
|
|
27214
|
+
var _a;
|
|
27215
|
+
if (!isJsonRecord(rawWindow)) {
|
|
27216
|
+
return undefined;
|
|
27217
|
+
}
|
|
27218
|
+
const usedPercentage = readPercentage(rawWindow.usedPercent);
|
|
27219
|
+
if (usedPercentage === undefined) {
|
|
27220
|
+
return undefined;
|
|
27221
|
+
}
|
|
27222
|
+
const windowLabel = (_a = formatCodexRateLimitWindowDuration(rawWindow.windowDurationMins)) !== null && _a !== void 0 ? _a : fallbackWindowLabel;
|
|
27223
|
+
const label = bucketLabel ? `${bucketLabel} ${windowLabel}` : windowLabel;
|
|
27224
|
+
const resetsAt = readUnixTimestamp(rawWindow.resetsAt);
|
|
27225
|
+
return {
|
|
27226
|
+
label,
|
|
27227
|
+
usedPercentage,
|
|
27228
|
+
...(resetsAt !== undefined && { resetsAt }),
|
|
27229
|
+
};
|
|
27230
|
+
}
|
|
27231
|
+
/**
|
|
27232
|
+
* Formats the duration of one Codex rolling rate-limit window for compact terminal display.
|
|
27233
|
+
*
|
|
27234
|
+
* @private helper of `buildCodexSubscriptionUsage`
|
|
27235
|
+
*/
|
|
27236
|
+
function formatCodexRateLimitWindowDuration(value) {
|
|
27237
|
+
const durationMinutes = readNonNegativeFiniteNumber(value);
|
|
27238
|
+
if (durationMinutes === undefined || durationMinutes === 0) {
|
|
27239
|
+
return undefined;
|
|
27240
|
+
}
|
|
27241
|
+
const MINUTES_PER_HOUR = 60;
|
|
27242
|
+
const MINUTES_PER_DAY = 24 * MINUTES_PER_HOUR;
|
|
27243
|
+
if (durationMinutes % MINUTES_PER_DAY === 0) {
|
|
27244
|
+
return `${durationMinutes / MINUTES_PER_DAY}d`;
|
|
27245
|
+
}
|
|
27246
|
+
if (durationMinutes % MINUTES_PER_HOUR === 0) {
|
|
27247
|
+
return `${durationMinutes / MINUTES_PER_HOUR}h`;
|
|
27248
|
+
}
|
|
27249
|
+
return `${durationMinutes}m`;
|
|
27250
|
+
}
|
|
27251
|
+
/**
|
|
27252
|
+
* Parses one JSON-RPC line when it is an object.
|
|
27253
|
+
*
|
|
27254
|
+
* @private helper of `getCodexSubscriptionUsage`
|
|
27255
|
+
*/
|
|
27256
|
+
function parseJsonRecord(line) {
|
|
27257
|
+
try {
|
|
27258
|
+
const value = JSON.parse(line);
|
|
27259
|
+
return isJsonRecord(value) ? value : undefined;
|
|
27260
|
+
}
|
|
27261
|
+
catch (_a) {
|
|
27262
|
+
return undefined;
|
|
27263
|
+
}
|
|
27264
|
+
}
|
|
27265
|
+
/**
|
|
27266
|
+
* Checks whether a value is a JSON object.
|
|
27267
|
+
*
|
|
27268
|
+
* @private helper of `getCodexSubscriptionUsage`
|
|
27269
|
+
*/
|
|
27270
|
+
function isJsonRecord(value) {
|
|
27271
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
27272
|
+
}
|
|
27273
|
+
/**
|
|
27274
|
+
* Reads one non-empty string from an untrusted JSON object.
|
|
27275
|
+
*
|
|
27276
|
+
* @private helper of `buildCodexSubscriptionUsage`
|
|
27277
|
+
*/
|
|
27278
|
+
function readString(value) {
|
|
27279
|
+
return typeof value === 'string' && value.trim() !== '' ? value.trim() : undefined;
|
|
27280
|
+
}
|
|
27281
|
+
/**
|
|
27282
|
+
* Reads a finite number which cannot be negative.
|
|
27283
|
+
*
|
|
27284
|
+
* @private helper of `buildCodexSubscriptionUsage`
|
|
27285
|
+
*/
|
|
27286
|
+
function readNonNegativeFiniteNumber(value) {
|
|
27287
|
+
return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : undefined;
|
|
27288
|
+
}
|
|
27289
|
+
/**
|
|
27290
|
+
* Reads a valid percentage from the Codex app-server response.
|
|
27291
|
+
*
|
|
27292
|
+
* @private helper of `buildCodexSubscriptionUsage`
|
|
27293
|
+
*/
|
|
27294
|
+
function readPercentage(value) {
|
|
27295
|
+
const percentage = readNonNegativeFiniteNumber(value);
|
|
27296
|
+
return percentage !== undefined && percentage <= 100 ? percentage : undefined;
|
|
27297
|
+
}
|
|
27298
|
+
/**
|
|
27299
|
+
* Reads a Unix timestamp in seconds from the Codex app-server response.
|
|
27300
|
+
*
|
|
27301
|
+
* @private helper of `buildCodexSubscriptionUsage`
|
|
27302
|
+
*/
|
|
27303
|
+
function readUnixTimestamp(value) {
|
|
27304
|
+
const timestamp = readNonNegativeFiniteNumber(value);
|
|
27305
|
+
return timestamp !== undefined && timestamp > 0 ? timestamp : undefined;
|
|
27306
|
+
}
|
|
27307
|
+
|
|
26861
27308
|
/**
|
|
26862
27309
|
* Detects which login method Codex used from the captured CLI output.
|
|
26863
27310
|
*
|
|
@@ -27045,6 +27492,15 @@ class OpenAiCodexRunner {
|
|
|
27045
27492
|
jitterRatio: RATE_LIMIT_BACKOFF_JITTER_RATIO,
|
|
27046
27493
|
});
|
|
27047
27494
|
}
|
|
27495
|
+
/**
|
|
27496
|
+
* Reads the current ChatGPT subscription quota snapshot through Codex's local app server.
|
|
27497
|
+
*
|
|
27498
|
+
* API-key sessions and unsupported Codex versions simply return no snapshot, which keeps this optional dashboard
|
|
27499
|
+
* information from affecting the actual prompt execution.
|
|
27500
|
+
*/
|
|
27501
|
+
async getSubscriptionUsage() {
|
|
27502
|
+
return await getCodexSubscriptionUsage(this.options.codexCommand);
|
|
27503
|
+
}
|
|
27048
27504
|
/**
|
|
27049
27505
|
* Runs the Codex prompt in a temporary script and waits for completion output.
|
|
27050
27506
|
*/
|
|
@@ -27624,6 +28080,90 @@ function assertPriorityBoundary(optionName, priority) {
|
|
|
27624
28080
|
}
|
|
27625
28081
|
// Note: [🟡] Code for priority filtering [priorityFilter](scripts/run-codex-prompts/prompts/priorityFilter.ts) should never be published outside of `@promptbook/cli`
|
|
27626
28082
|
|
|
28083
|
+
/**
|
|
28084
|
+
* Label shown on the first subscription-usage row in the terminal Session box.
|
|
28085
|
+
*/
|
|
28086
|
+
const SUBSCRIPTION_USAGE_SESSION_LABEL = 'Usage';
|
|
28087
|
+
/**
|
|
28088
|
+
* Number of milliseconds in one calendar-free day used for compact reset countdowns.
|
|
28089
|
+
*/
|
|
28090
|
+
const ONE_DAY_MS = 24 * 60 * 60 * 1000;
|
|
28091
|
+
/**
|
|
28092
|
+
* Builds every available subscription-limit row for the terminal Session box.
|
|
28093
|
+
*
|
|
28094
|
+
* The first row carries the `Usage` label and subsequent rows stay aligned beneath it, allowing a harness to report
|
|
28095
|
+
* any number of windows instead of making the dashboard assume a fixed 5-hour/weekly pair.
|
|
28096
|
+
*
|
|
28097
|
+
* @private internal utility of coder run UI
|
|
28098
|
+
*/
|
|
28099
|
+
function buildSubscriptionUsageSessionRows(subscriptionUsage, currentTimeMs = Date.now()) {
|
|
28100
|
+
if (!subscriptionUsage || subscriptionUsage.limits.length === 0) {
|
|
28101
|
+
return [];
|
|
28102
|
+
}
|
|
28103
|
+
return subscriptionUsage.limits.map((limit, index) => ({
|
|
28104
|
+
label: index === 0 ? SUBSCRIPTION_USAGE_SESSION_LABEL : '',
|
|
28105
|
+
value: formatSubscriptionUsageLimit(limit, currentTimeMs),
|
|
28106
|
+
}));
|
|
28107
|
+
}
|
|
28108
|
+
/**
|
|
28109
|
+
* Formats one remaining subscription limit and its optional reset time for the terminal dashboard.
|
|
28110
|
+
*
|
|
28111
|
+
* @private helper of `buildSubscriptionUsageSessionRows`
|
|
28112
|
+
*/
|
|
28113
|
+
function formatSubscriptionUsageLimit(limit, currentTimeMs) {
|
|
28114
|
+
const remainingPercentage = Math.round(Math.max(0, Math.min(100, 100 - limit.usedPercentage)));
|
|
28115
|
+
const remainingPercentageColor = resolveRemainingPercentageColor(remainingPercentage);
|
|
28116
|
+
const resetText = formatSubscriptionUsageReset(limit.resetsAt, currentTimeMs);
|
|
28117
|
+
const label = limit.label.trim() || SUBSCRIPTION_USAGE_SESSION_LABEL;
|
|
28118
|
+
return [
|
|
28119
|
+
colors.bold(label),
|
|
28120
|
+
remainingPercentageColor(`${remainingPercentage}% remaining`),
|
|
28121
|
+
...(resetText ? [colors.gray(resetText)] : []),
|
|
28122
|
+
].join(' · ');
|
|
28123
|
+
}
|
|
28124
|
+
/**
|
|
28125
|
+
* Chooses an attention color for the remaining subscription percentage.
|
|
28126
|
+
*
|
|
28127
|
+
* @private helper of `buildSubscriptionUsageSessionRows`
|
|
28128
|
+
*/
|
|
28129
|
+
function resolveRemainingPercentageColor(remainingPercentage) {
|
|
28130
|
+
if (remainingPercentage === 0) {
|
|
28131
|
+
return colors.red;
|
|
28132
|
+
}
|
|
28133
|
+
if (remainingPercentage <= 20) {
|
|
28134
|
+
return colors.yellow;
|
|
28135
|
+
}
|
|
28136
|
+
return colors.green;
|
|
28137
|
+
}
|
|
28138
|
+
/**
|
|
28139
|
+
* Formats the optional future reset timestamp of one subscription limit.
|
|
28140
|
+
*
|
|
28141
|
+
* @private helper of `buildSubscriptionUsageSessionRows`
|
|
28142
|
+
*/
|
|
28143
|
+
function formatSubscriptionUsageReset(resetsAt, currentTimeMs) {
|
|
28144
|
+
if (resetsAt === undefined) {
|
|
28145
|
+
return undefined;
|
|
28146
|
+
}
|
|
28147
|
+
const remainingDurationMs = resetsAt * 1000 - currentTimeMs;
|
|
28148
|
+
if (remainingDurationMs <= 0) {
|
|
28149
|
+
return 'resets now';
|
|
28150
|
+
}
|
|
28151
|
+
return `resets in ${formatSubscriptionUsageDuration(remainingDurationMs)}`;
|
|
28152
|
+
}
|
|
28153
|
+
/**
|
|
28154
|
+
* Formats a reset countdown with days when a weekly-style limit is still several days away.
|
|
28155
|
+
*
|
|
28156
|
+
* @private helper of `buildSubscriptionUsageSessionRows`
|
|
28157
|
+
*/
|
|
28158
|
+
function formatSubscriptionUsageDuration(durationMs) {
|
|
28159
|
+
const dayCount = Math.floor(durationMs / ONE_DAY_MS);
|
|
28160
|
+
if (dayCount === 0) {
|
|
28161
|
+
return formatDurationMs(durationMs);
|
|
28162
|
+
}
|
|
28163
|
+
const remainingDurationMs = durationMs - dayCount * ONE_DAY_MS;
|
|
28164
|
+
return remainingDurationMs === 0 ? `${dayCount}d` : `${dayCount}d ${formatDurationMs(remainingDurationMs)}`;
|
|
28165
|
+
}
|
|
28166
|
+
|
|
27627
28167
|
/**
|
|
27628
28168
|
* Minimum width used for the rich coder-run frame.
|
|
27629
28169
|
*/
|
|
@@ -27729,6 +28269,7 @@ function buildSessionRows(options, bodyWidth, pausePresentation) {
|
|
|
27729
28269
|
value: runnerParts.join(' · '),
|
|
27730
28270
|
},
|
|
27731
28271
|
...configurationRows,
|
|
28272
|
+
...buildSubscriptionUsageSessionRows(options.subscriptionUsage),
|
|
27732
28273
|
...buildScriptPathSessionRows(options.currentScriptPaths || [], bodyWidth),
|
|
27733
28274
|
{
|
|
27734
28275
|
label: 'This run',
|
|
@@ -28274,6 +28815,13 @@ class CoderRunUiState extends EventEmitter {
|
|
|
28274
28815
|
this.agentStatusTableRows = [...agentStatusTableRows];
|
|
28275
28816
|
this.emitChange();
|
|
28276
28817
|
}
|
|
28818
|
+
/**
|
|
28819
|
+
* Replaces the optional harness subscription usage shown in the Session box.
|
|
28820
|
+
*/
|
|
28821
|
+
setSubscriptionUsage(subscriptionUsage) {
|
|
28822
|
+
this.subscriptionUsage = subscriptionUsage;
|
|
28823
|
+
this.emitChange();
|
|
28824
|
+
}
|
|
28277
28825
|
/**
|
|
28278
28826
|
* Sets or clears the Enter-key action label shown in the controls panel.
|
|
28279
28827
|
*/
|
|
@@ -29265,6 +29813,7 @@ function renderCoderRunUi(startTime, options = {}) {
|
|
|
29265
29813
|
pauseTargetLabel: getPauseTargetLabel(),
|
|
29266
29814
|
isEndAfterCurrentPromptRequested: getEndAfterCurrentPromptState(),
|
|
29267
29815
|
config: state.config,
|
|
29816
|
+
subscriptionUsage: state.subscriptionUsage,
|
|
29268
29817
|
agentVisual: state.agentVisual,
|
|
29269
29818
|
phase: state.phase,
|
|
29270
29819
|
currentPromptLabel: state.currentPromptLabel,
|
|
@@ -76410,6 +76959,30 @@ async function writePromptFile(file) {
|
|
|
76410
76959
|
await writeFile(file.path, content, 'utf-8');
|
|
76411
76960
|
}
|
|
76412
76961
|
|
|
76962
|
+
/**
|
|
76963
|
+
* Refreshes optional harness subscription usage in the shared coder-run UI state.
|
|
76964
|
+
*
|
|
76965
|
+
* Subscription visibility is an enhancement, not a prerequisite for running prompts. A harness which cannot expose a
|
|
76966
|
+
* snapshot, or whose account endpoint is temporarily unavailable, therefore leaves the existing dashboard unchanged.
|
|
76967
|
+
*
|
|
76968
|
+
* @private internal utility of coder run UI
|
|
76969
|
+
*/
|
|
76970
|
+
async function refreshCoderRunUiSubscriptionUsage(options) {
|
|
76971
|
+
const { runner, uiState } = options;
|
|
76972
|
+
if (!uiState || !runner.getSubscriptionUsage) {
|
|
76973
|
+
return;
|
|
76974
|
+
}
|
|
76975
|
+
try {
|
|
76976
|
+
const subscriptionUsage = await runner.getSubscriptionUsage();
|
|
76977
|
+
if (subscriptionUsage) {
|
|
76978
|
+
uiState.setSubscriptionUsage(subscriptionUsage);
|
|
76979
|
+
}
|
|
76980
|
+
}
|
|
76981
|
+
catch (_a) {
|
|
76982
|
+
// A current subscription snapshot must never interrupt the coding queue.
|
|
76983
|
+
}
|
|
76984
|
+
}
|
|
76985
|
+
|
|
76413
76986
|
/**
|
|
76414
76987
|
* Maximum number of retry attempts performed after a prompt round throws an error.
|
|
76415
76988
|
* After this many retries the round is finalized as failed.
|
|
@@ -76487,6 +77060,10 @@ async function runPromptRound({ options, runner, runnerMetadata, nextPrompt, pro
|
|
|
76487
77060
|
}),
|
|
76488
77061
|
waitForPauseCheckpoint: waitForRequestedPause,
|
|
76489
77062
|
});
|
|
77063
|
+
await refreshCoderRunUiSubscriptionUsage({
|
|
77064
|
+
runner,
|
|
77065
|
+
uiState: uiHandle === null || uiHandle === void 0 ? void 0 : uiHandle.state,
|
|
77066
|
+
});
|
|
76490
77067
|
await finalizeSuccessfulPromptRound({
|
|
76491
77068
|
options,
|
|
76492
77069
|
nextPrompt,
|
|
@@ -77727,6 +78304,10 @@ async function runCodexPrompts(providedOptions) {
|
|
|
77727
78304
|
console.info(colors.green(`Running prompts with ${runner.name}`));
|
|
77728
78305
|
initializeRunUi(uiHandle, runner.name, actualRunnerModel, options);
|
|
77729
78306
|
await initializeRunUiAgentVisual(uiHandle, resolvedCoderAgent === null || resolvedCoderAgent === void 0 ? void 0 : resolvedCoderAgent.agentSource);
|
|
78307
|
+
await refreshCoderRunUiSubscriptionUsage({
|
|
78308
|
+
runner,
|
|
78309
|
+
uiState: uiHandle === null || uiHandle === void 0 ? void 0 : uiHandle.state,
|
|
78310
|
+
});
|
|
77730
78311
|
await seedCachedAveragePromptDuration({
|
|
77731
78312
|
options,
|
|
77732
78313
|
actualRunnerModel,
|