@staff0rd/assist 0.691.2 → 0.693.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/README.md +1 -1
- package/dist/commands/sessions/web/bundle.js +417 -417
- package/dist/index.js +282 -69
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -6,7 +6,7 @@ import { Command } from "commander";
|
|
|
6
6
|
// package.json
|
|
7
7
|
var package_default = {
|
|
8
8
|
name: "@staff0rd/assist",
|
|
9
|
-
version: "0.
|
|
9
|
+
version: "0.693.0",
|
|
10
10
|
type: "module",
|
|
11
11
|
main: "dist/index.js",
|
|
12
12
|
bin: {
|
|
@@ -11976,6 +11976,22 @@ async function listUsagePeaks(db, options2) {
|
|
|
11976
11976
|
return query;
|
|
11977
11977
|
}
|
|
11978
11978
|
|
|
11979
|
+
// src/shared/usageWindowKey.ts
|
|
11980
|
+
var RATE_LIMIT_WINDOWS = ["five_hour", "seven_day"];
|
|
11981
|
+
var HARNESS_KINDS = ["claude", "codex", "pi"];
|
|
11982
|
+
function usageWindowKey(harness, window) {
|
|
11983
|
+
const kind = resolveHarness(harness);
|
|
11984
|
+
return kind === "claude" ? window : `${kind}:${window}`;
|
|
11985
|
+
}
|
|
11986
|
+
function parseUsageWindowKey(key) {
|
|
11987
|
+
const [prefix2, suffix] = key.includes(":") ? key.split(":", 2) : ["claude", key];
|
|
11988
|
+
const harness = HARNESS_KINDS.find((h) => h === prefix2);
|
|
11989
|
+
const window = RATE_LIMIT_WINDOWS.find((w) => w === suffix);
|
|
11990
|
+
if (!harness || !window) return void 0;
|
|
11991
|
+
if (harness === "claude" && key.includes(":")) return void 0;
|
|
11992
|
+
return { harness, window };
|
|
11993
|
+
}
|
|
11994
|
+
|
|
11979
11995
|
// src/commands/sessions/web/respondPagedRows.ts
|
|
11980
11996
|
var DEFAULT_PAGE_SIZE = 30;
|
|
11981
11997
|
async function respondPagedRows(req, res, load3) {
|
|
@@ -11987,7 +12003,9 @@ async function respondPagedRows(req, res, load3) {
|
|
|
11987
12003
|
|
|
11988
12004
|
// src/commands/sessions/web/listUsageHistory.ts
|
|
11989
12005
|
function parseWindow(value) {
|
|
11990
|
-
|
|
12006
|
+
if (!value) return void 0;
|
|
12007
|
+
const parsed = parseUsageWindowKey(value);
|
|
12008
|
+
return parsed ? usageWindowKey(parsed.harness, parsed.window) : void 0;
|
|
11991
12009
|
}
|
|
11992
12010
|
function listUsageHistory(req, res) {
|
|
11993
12011
|
return respondPagedRows(req, res, async (range, params) => {
|
|
@@ -18967,21 +18985,46 @@ function registerCodeComment(parent) {
|
|
|
18967
18985
|
|
|
18968
18986
|
// src/commands/codexHook/parseCodexHookInput.ts
|
|
18969
18987
|
var SUPPORTED_TOOLS = /* @__PURE__ */ new Set(["Bash", "PowerShell"]);
|
|
18988
|
+
function optionalString(value) {
|
|
18989
|
+
return typeof value === "string" ? value : void 0;
|
|
18990
|
+
}
|
|
18970
18991
|
function parseCodexHookInput(raw) {
|
|
18971
18992
|
try {
|
|
18972
18993
|
const data = JSON.parse(raw);
|
|
18973
|
-
const
|
|
18974
|
-
|
|
18994
|
+
const base = {
|
|
18995
|
+
event: data.hook_event_name ?? "PreToolUse",
|
|
18996
|
+
cwd: optionalString(data.cwd)
|
|
18997
|
+
};
|
|
18998
|
+
const transcriptPath2 = optionalString(data.transcript_path);
|
|
18999
|
+
if (transcriptPath2) base.transcriptPath = transcriptPath2;
|
|
18975
19000
|
const command = data.tool_input?.command;
|
|
18976
|
-
if (typeof command !== "string" || !command.trim()) return
|
|
18977
|
-
if (!data.tool_name || !SUPPORTED_TOOLS.has(data.tool_name))
|
|
18978
|
-
|
|
18979
|
-
return { event, cwd, toolName: data.tool_name, command: command.trim() };
|
|
19001
|
+
if (typeof command !== "string" || !command.trim()) return base;
|
|
19002
|
+
if (!data.tool_name || !SUPPORTED_TOOLS.has(data.tool_name)) return base;
|
|
19003
|
+
return { ...base, toolName: data.tool_name, command: command.trim() };
|
|
18980
19004
|
} catch {
|
|
18981
19005
|
return void 0;
|
|
18982
19006
|
}
|
|
18983
19007
|
}
|
|
18984
19008
|
|
|
19009
|
+
// src/commands/codexHook/relayCodexUsage.ts
|
|
19010
|
+
var USAGE_EVENTS = /* @__PURE__ */ new Set(["PostToolUse", "Stop"]);
|
|
19011
|
+
async function relayCodexUsage(event, transcriptPath2) {
|
|
19012
|
+
const sessionId = process.env.ASSIST_SESSION_ID;
|
|
19013
|
+
if (!sessionId || !transcriptPath2 || !USAGE_EVENTS.has(event)) return;
|
|
19014
|
+
try {
|
|
19015
|
+
await sendToDaemonAwaitAck({
|
|
19016
|
+
type: "codex-usage",
|
|
19017
|
+
sessionId,
|
|
19018
|
+
transcriptPath: transcriptPath2,
|
|
19019
|
+
ack: true
|
|
19020
|
+
});
|
|
19021
|
+
} catch (error) {
|
|
19022
|
+
appendDaemonLog(
|
|
19023
|
+
`codex-usage relay failed: id=${sessionId}: ${error instanceof Error ? error.message : String(error)}`
|
|
19024
|
+
);
|
|
19025
|
+
}
|
|
19026
|
+
}
|
|
19027
|
+
|
|
18985
19028
|
// src/commands/codexHook/codexStatusFor.ts
|
|
18986
19029
|
function codexStatusFor(event, autoDecided) {
|
|
18987
19030
|
if (event === "UserPromptSubmit")
|
|
@@ -19041,6 +19084,7 @@ async function codexHook() {
|
|
|
19041
19084
|
const output = outputFor(input, decision);
|
|
19042
19085
|
if (output) console.log(JSON.stringify(output));
|
|
19043
19086
|
await reportCodexStatus(input.event, decision !== void 0);
|
|
19087
|
+
await relayCodexUsage(input.event, input.transcriptPath);
|
|
19044
19088
|
}
|
|
19045
19089
|
|
|
19046
19090
|
// src/commands/registerCodexHook.ts
|
|
@@ -29926,9 +29970,15 @@ function screenshot(processName) {
|
|
|
29926
29970
|
import chalk205 from "chalk";
|
|
29927
29971
|
|
|
29928
29972
|
// src/shared/contextLevel.ts
|
|
29929
|
-
|
|
29930
|
-
|
|
29931
|
-
|
|
29973
|
+
var THRESHOLDS = {
|
|
29974
|
+
claude: { red: 30, yellow: 20 },
|
|
29975
|
+
codex: { red: 75, yellow: 50 },
|
|
29976
|
+
pi: { red: 30, yellow: 20 }
|
|
29977
|
+
};
|
|
29978
|
+
function contextLevel(pct, harness) {
|
|
29979
|
+
const { red, yellow } = THRESHOLDS[resolveHarness(harness)];
|
|
29980
|
+
if (pct >= red) return "red";
|
|
29981
|
+
if (pct >= yellow) return "yellow";
|
|
29932
29982
|
return "dim";
|
|
29933
29983
|
}
|
|
29934
29984
|
|
|
@@ -38964,33 +39014,40 @@ function broadcastSessions(sessions, clients, windowsSessions = [], active) {
|
|
|
38964
39014
|
}
|
|
38965
39015
|
|
|
38966
39016
|
// src/shared/activeWindows.ts
|
|
38967
|
-
|
|
38968
|
-
function activeWindows(rateLimits) {
|
|
39017
|
+
function activeWindows(rateLimits, harness) {
|
|
38969
39018
|
if (!rateLimits) return [];
|
|
38970
39019
|
const out = [];
|
|
38971
|
-
for (const window of
|
|
39020
|
+
for (const window of RATE_LIMIT_WINDOWS) {
|
|
38972
39021
|
const resetsAt = rateLimits[window]?.resets_at;
|
|
38973
|
-
if (typeof resetsAt === "number")
|
|
39022
|
+
if (typeof resetsAt === "number")
|
|
39023
|
+
out.push({ window: usageWindowKey(harness, window), resetsAt });
|
|
38974
39024
|
}
|
|
38975
39025
|
return out;
|
|
38976
39026
|
}
|
|
38977
39027
|
|
|
38978
39028
|
// src/shared/db/recordUsagePeak.ts
|
|
38979
|
-
|
|
38980
|
-
|
|
38981
|
-
for (const window of WINDOWS2) {
|
|
39029
|
+
async function recordUsagePeak(db, rateLimits, now = Math.floor(Date.now() / 1e3), harness) {
|
|
39030
|
+
for (const window of RATE_LIMIT_WINDOWS) {
|
|
38982
39031
|
const w = rateLimits[window];
|
|
38983
39032
|
if (!w || typeof w.resets_at !== "number" || typeof w.used_percentage !== "number")
|
|
38984
39033
|
continue;
|
|
38985
|
-
await recordWindowPeak(
|
|
39034
|
+
await recordWindowPeak(
|
|
39035
|
+
db,
|
|
39036
|
+
usageWindowKey(harness, window),
|
|
39037
|
+
w.resets_at,
|
|
39038
|
+
w.used_percentage,
|
|
39039
|
+
now
|
|
39040
|
+
);
|
|
38986
39041
|
}
|
|
38987
39042
|
}
|
|
38988
39043
|
|
|
38989
39044
|
// src/commands/sessions/daemon/persistUsagePeak.ts
|
|
38990
|
-
async function persistUsagePeak(rateLimits) {
|
|
39045
|
+
async function persistUsagePeak(rateLimits, harness) {
|
|
38991
39046
|
try {
|
|
38992
39047
|
if (!process.env.ASSIST_DATABASE_URL && !loadConfig().database.url) return;
|
|
38993
|
-
|
|
39048
|
+
const db = await getDb();
|
|
39049
|
+
if (harness) await recordUsagePeak(db, rateLimits, void 0, harness);
|
|
39050
|
+
else await recordUsagePeak(db, rateLimits);
|
|
38994
39051
|
} catch (error) {
|
|
38995
39052
|
daemonLog(`usage-peak persist failed: ${String(error)}`);
|
|
38996
39053
|
}
|
|
@@ -39004,6 +39061,7 @@ var ClientHub = class extends Set {
|
|
|
39004
39061
|
this.persistPeak = persistPeak;
|
|
39005
39062
|
}
|
|
39006
39063
|
latestLimits;
|
|
39064
|
+
harnessLimits = /* @__PURE__ */ new Map();
|
|
39007
39065
|
// why: log delivery is opt-in so browser tabs aren't spammed; only connections that ask via subscribe-logs receive daemonLog lines.
|
|
39008
39066
|
logSubscribers = /* @__PURE__ */ new Set();
|
|
39009
39067
|
updateLimits(rateLimits) {
|
|
@@ -39011,13 +39069,26 @@ var ClientHub = class extends Set {
|
|
|
39011
39069
|
broadcast(this, { type: "limits", rateLimits });
|
|
39012
39070
|
this.persistPeak?.(rateLimits);
|
|
39013
39071
|
}
|
|
39014
|
-
|
|
39015
|
-
|
|
39072
|
+
updateHarnessLimits(harness, rateLimits) {
|
|
39073
|
+
if (harness === "claude") {
|
|
39074
|
+
this.updateLimits(rateLimits);
|
|
39075
|
+
return;
|
|
39076
|
+
}
|
|
39077
|
+
this.harnessLimits.set(harness, rateLimits);
|
|
39078
|
+
broadcast(this, { type: "limits", harness, rateLimits });
|
|
39079
|
+
this.persistPeak?.(rateLimits, harness);
|
|
39080
|
+
}
|
|
39081
|
+
currentWindows(harness) {
|
|
39082
|
+
if (!harness || harness === "claude")
|
|
39083
|
+
return activeWindows(this.latestLimits);
|
|
39084
|
+
return activeWindows(this.harnessLimits.get(harness), harness);
|
|
39016
39085
|
}
|
|
39017
39086
|
greet(client) {
|
|
39018
39087
|
if (this.latestLimits) {
|
|
39019
39088
|
sendTo(client, { type: "limits", rateLimits: this.latestLimits });
|
|
39020
39089
|
}
|
|
39090
|
+
for (const [harness, rateLimits] of this.harnessLimits)
|
|
39091
|
+
sendTo(client, { type: "limits", harness, rateLimits });
|
|
39021
39092
|
}
|
|
39022
39093
|
subscribeLogs(client, replay = true) {
|
|
39023
39094
|
if (replay) {
|
|
@@ -39523,6 +39594,33 @@ function killPtyTree(pty2) {
|
|
|
39523
39594
|
}
|
|
39524
39595
|
}
|
|
39525
39596
|
|
|
39597
|
+
// src/commands/sessions/daemon/worktree/liveWatcherFor.ts
|
|
39598
|
+
function liveWatcherFor(sessions, clone) {
|
|
39599
|
+
for (const session of sessions.values())
|
|
39600
|
+
if (session.watcher === true && session.cwd !== void 0 && (clone === void 0 || canonicalTreePath(session.cwd) === clone) && session.status !== "stopped" && session.status !== "error")
|
|
39601
|
+
return session;
|
|
39602
|
+
return void 0;
|
|
39603
|
+
}
|
|
39604
|
+
|
|
39605
|
+
// src/commands/sessions/daemon/worktree/resolveClone.ts
|
|
39606
|
+
function resolveClone(cwd) {
|
|
39607
|
+
const repoRoot2 = findRepoRoot(cwd) ?? cwd;
|
|
39608
|
+
return canonicalTreePath(mainWorktree(repoRoot2) ?? repoRoot2);
|
|
39609
|
+
}
|
|
39610
|
+
|
|
39611
|
+
// src/commands/sessions/daemon/worktree/orphanedWatcherFor.ts
|
|
39612
|
+
function orphanedWatcherFor(sessions, dismissed) {
|
|
39613
|
+
if (dismissed.watcher === true || !dismissed.cwd) return void 0;
|
|
39614
|
+
if (!liveWatcherFor(sessions)) return void 0;
|
|
39615
|
+
const clone = resolveClone(dismissed.cwd);
|
|
39616
|
+
const watcher = liveWatcherFor(sessions, clone);
|
|
39617
|
+
if (!watcher) return void 0;
|
|
39618
|
+
const kept = [...sessions.values()].some(
|
|
39619
|
+
(s) => s.watcher !== true && s.cwd !== void 0 && resolveClone(s.cwd) === clone
|
|
39620
|
+
);
|
|
39621
|
+
return kept ? void 0 : { watcher, clone };
|
|
39622
|
+
}
|
|
39623
|
+
|
|
39526
39624
|
// src/commands/sessions/daemon/worktree/treeUnderClose.ts
|
|
39527
39625
|
function treeUnderClose(session) {
|
|
39528
39626
|
if (session.worktree) return { path: session.worktree.path, removable: true };
|
|
@@ -39552,6 +39650,13 @@ function dismissSession(sessions, id) {
|
|
|
39552
39650
|
if (s.activity?.itemId != null) releaseLock(s.activity.itemId);
|
|
39553
39651
|
sessions.delete(id);
|
|
39554
39652
|
daemonLog(`session ${id} dismissed (${s.name})`);
|
|
39653
|
+
const orphan = orphanedWatcherFor(sessions, s);
|
|
39654
|
+
if (orphan) {
|
|
39655
|
+
daemonLog(
|
|
39656
|
+
`reaping watcher session ${orphan.watcher.id} for the clone ${orphan.clone}: its last session ${id} was dismissed`
|
|
39657
|
+
);
|
|
39658
|
+
dismissSession(sessions, orphan.watcher.id);
|
|
39659
|
+
}
|
|
39555
39660
|
if (!s.worktree) return true;
|
|
39556
39661
|
if (sharedWith.length > 0)
|
|
39557
39662
|
daemonLog(
|
|
@@ -40936,6 +41041,35 @@ function makeSessionSpawner(sessions, clients, counter, onStatusChange, notify2)
|
|
|
40936
41041
|
};
|
|
40937
41042
|
}
|
|
40938
41043
|
|
|
41044
|
+
// src/commands/sessions/shared/transcriptUsage.ts
|
|
41045
|
+
import * as fs54 from "fs";
|
|
41046
|
+
function transcriptUsage(lines2) {
|
|
41047
|
+
const byId = /* @__PURE__ */ new Map();
|
|
41048
|
+
for (const line of lines2) {
|
|
41049
|
+
if (!line.trim()) continue;
|
|
41050
|
+
let entry;
|
|
41051
|
+
try {
|
|
41052
|
+
entry = JSON.parse(line);
|
|
41053
|
+
} catch {
|
|
41054
|
+
continue;
|
|
41055
|
+
}
|
|
41056
|
+
if (entry.type !== "assistant") continue;
|
|
41057
|
+
const id = entry.message?.id;
|
|
41058
|
+
const usage = entry.message?.usage;
|
|
41059
|
+
if (!id || !usage) continue;
|
|
41060
|
+
byId.set(id, {
|
|
41061
|
+
messageId: id,
|
|
41062
|
+
inputTokens: usage.input_tokens ?? 0,
|
|
41063
|
+
outputTokens: usage.output_tokens ?? 0
|
|
41064
|
+
});
|
|
41065
|
+
}
|
|
41066
|
+
return [...byId.values()];
|
|
41067
|
+
}
|
|
41068
|
+
async function readTranscriptUsage(transcriptPath2) {
|
|
41069
|
+
const content = await fs54.promises.readFile(transcriptPath2, "utf8");
|
|
41070
|
+
return transcriptUsage(content.split("\n"));
|
|
41071
|
+
}
|
|
41072
|
+
|
|
40939
41073
|
// src/shared/db/recordPhaseCycleContext.ts
|
|
40940
41074
|
import { sql as sql25 } from "drizzle-orm";
|
|
40941
41075
|
async function recordPhaseCycleContext(db, itemId2, phaseIdx, window, resetsAt, pct) {
|
|
@@ -41009,37 +41143,8 @@ async function recordWindowTokens(db, window, resetsAt, tokensUp, tokensDown) {
|
|
|
41009
41143
|
);
|
|
41010
41144
|
}
|
|
41011
41145
|
|
|
41012
|
-
// src/commands/sessions/shared/transcriptUsage.ts
|
|
41013
|
-
import * as fs54 from "fs";
|
|
41014
|
-
function transcriptUsage(lines2) {
|
|
41015
|
-
const byId = /* @__PURE__ */ new Map();
|
|
41016
|
-
for (const line of lines2) {
|
|
41017
|
-
if (!line.trim()) continue;
|
|
41018
|
-
let entry;
|
|
41019
|
-
try {
|
|
41020
|
-
entry = JSON.parse(line);
|
|
41021
|
-
} catch {
|
|
41022
|
-
continue;
|
|
41023
|
-
}
|
|
41024
|
-
if (entry.type !== "assistant") continue;
|
|
41025
|
-
const id = entry.message?.id;
|
|
41026
|
-
const usage = entry.message?.usage;
|
|
41027
|
-
if (!id || !usage) continue;
|
|
41028
|
-
byId.set(id, {
|
|
41029
|
-
messageId: id,
|
|
41030
|
-
inputTokens: usage.input_tokens ?? 0,
|
|
41031
|
-
outputTokens: usage.output_tokens ?? 0
|
|
41032
|
-
});
|
|
41033
|
-
}
|
|
41034
|
-
return [...byId.values()];
|
|
41035
|
-
}
|
|
41036
|
-
async function readTranscriptUsage(transcriptPath2) {
|
|
41037
|
-
const content = await fs54.promises.readFile(transcriptPath2, "utf8");
|
|
41038
|
-
return transcriptUsage(content.split("\n"));
|
|
41039
|
-
}
|
|
41040
|
-
|
|
41041
41146
|
// src/commands/sessions/daemon/persistPhaseTokens.ts
|
|
41042
|
-
async function persistPhaseTokens(itemId2, phaseIdx,
|
|
41147
|
+
async function persistPhaseTokens(itemId2, phaseIdx, loadResponses, usedPct, windows) {
|
|
41043
41148
|
try {
|
|
41044
41149
|
if (!process.env.ASSIST_DATABASE_URL && !loadConfig().database.url) return;
|
|
41045
41150
|
const db = await getDb();
|
|
@@ -41056,7 +41161,7 @@ async function persistPhaseTokens(itemId2, phaseIdx, transcriptPath2, usedPct, w
|
|
|
41056
41161
|
);
|
|
41057
41162
|
}
|
|
41058
41163
|
}
|
|
41059
|
-
const responses = await
|
|
41164
|
+
const responses = await loadResponses();
|
|
41060
41165
|
if (responses.length === 0) return;
|
|
41061
41166
|
const { tokensUp, tokensDown } = await recordPhaseTranscriptUsage(
|
|
41062
41167
|
db,
|
|
@@ -41082,7 +41187,7 @@ function recordSessionUsage(sessions, claudeSessionId, transcriptPath2, usedPct,
|
|
|
41082
41187
|
void persistPhaseTokens(
|
|
41083
41188
|
phase.itemId,
|
|
41084
41189
|
phase.phaseIdx,
|
|
41085
|
-
transcriptPath2,
|
|
41190
|
+
() => readTranscriptUsage(transcriptPath2),
|
|
41086
41191
|
usedPct,
|
|
41087
41192
|
windows
|
|
41088
41193
|
);
|
|
@@ -42418,7 +42523,7 @@ function ensureWatcher(ctx, cwd) {
|
|
|
42418
42523
|
const repoRoot2 = findRepoRoot(cwd) ?? cwd;
|
|
42419
42524
|
const cfg = worktreeConfigFor(repoRoot2);
|
|
42420
42525
|
if (!cfg.enabled || cfg.watcher !== true) return void 0;
|
|
42421
|
-
const clone =
|
|
42526
|
+
const clone = resolveClone(repoRoot2);
|
|
42422
42527
|
const live = liveWatcherFor(ctx.sessions, clone);
|
|
42423
42528
|
if (live) {
|
|
42424
42529
|
daemonLog(
|
|
@@ -42437,12 +42542,6 @@ function ensureWatcher(ctx, cwd) {
|
|
|
42437
42542
|
);
|
|
42438
42543
|
return id;
|
|
42439
42544
|
}
|
|
42440
|
-
function liveWatcherFor(sessions, clone) {
|
|
42441
|
-
for (const session of sessions.values())
|
|
42442
|
-
if (session.watcher === true && session.cwd !== void 0 && canonicalTreePath(session.cwd) === clone && session.status !== "stopped" && session.status !== "error")
|
|
42443
|
-
return session;
|
|
42444
|
-
return void 0;
|
|
42445
|
-
}
|
|
42446
42545
|
|
|
42447
42546
|
// src/commands/sessions/daemon/worktree/isBacklogRunArgs.ts
|
|
42448
42547
|
var ITEM_ID = /^a?\d+$/;
|
|
@@ -43888,6 +43987,119 @@ function creator(isNew, spawn13) {
|
|
|
43888
43987
|
};
|
|
43889
43988
|
}
|
|
43890
43989
|
|
|
43990
|
+
// src/commands/sessions/shared/codex/codexRolloutUsage.ts
|
|
43991
|
+
import * as fs55 from "fs";
|
|
43992
|
+
|
|
43993
|
+
// src/commands/sessions/shared/codex/applyTokenInfo.ts
|
|
43994
|
+
function applyTokenInfo(usage, byId, conversationId, rawInfo) {
|
|
43995
|
+
if (!rawInfo || typeof rawInfo !== "object") return;
|
|
43996
|
+
const info = rawInfo;
|
|
43997
|
+
const last = info.last_token_usage;
|
|
43998
|
+
const cumulative = info.total_token_usage?.total_tokens;
|
|
43999
|
+
if (!last || typeof cumulative !== "number") return;
|
|
44000
|
+
const messageId = `codex:${conversationId}:${cumulative}`;
|
|
44001
|
+
byId.set(messageId, {
|
|
44002
|
+
messageId,
|
|
44003
|
+
inputTokens: Math.max(
|
|
44004
|
+
0,
|
|
44005
|
+
(last.input_tokens ?? 0) - (last.cached_input_tokens ?? 0)
|
|
44006
|
+
),
|
|
44007
|
+
outputTokens: last.output_tokens ?? 0
|
|
44008
|
+
});
|
|
44009
|
+
const contextWindow = info.model_context_window;
|
|
44010
|
+
if (contextWindow && typeof last.total_tokens === "number")
|
|
44011
|
+
usage.usedPct = Math.min(100, last.total_tokens / contextWindow * 100);
|
|
44012
|
+
}
|
|
44013
|
+
|
|
44014
|
+
// src/commands/sessions/shared/codex/toRateLimits.ts
|
|
44015
|
+
var WINDOW_BY_MINUTES = {
|
|
44016
|
+
300: "five_hour",
|
|
44017
|
+
10080: "seven_day"
|
|
44018
|
+
};
|
|
44019
|
+
function toRateLimits(raw) {
|
|
44020
|
+
if (!raw || typeof raw !== "object") return void 0;
|
|
44021
|
+
const { primary, secondary } = raw;
|
|
44022
|
+
const rateLimits = {};
|
|
44023
|
+
for (const candidate of [primary, secondary]) {
|
|
44024
|
+
if (!candidate || typeof candidate !== "object") continue;
|
|
44025
|
+
const limit = candidate;
|
|
44026
|
+
const window = WINDOW_BY_MINUTES[limit.window_minutes ?? 0];
|
|
44027
|
+
if (!window || typeof limit.used_percent !== "number") continue;
|
|
44028
|
+
rateLimits[window] = {
|
|
44029
|
+
used_percentage: limit.used_percent,
|
|
44030
|
+
resets_at: limit.resets_at
|
|
44031
|
+
};
|
|
44032
|
+
}
|
|
44033
|
+
return Object.keys(rateLimits).length > 0 ? rateLimits : void 0;
|
|
44034
|
+
}
|
|
44035
|
+
|
|
44036
|
+
// src/commands/sessions/shared/codex/codexRolloutUsage.ts
|
|
44037
|
+
function codexRolloutUsage(lines2) {
|
|
44038
|
+
const usage = { responses: [] };
|
|
44039
|
+
const byId = /* @__PURE__ */ new Map();
|
|
44040
|
+
let conversationId = "";
|
|
44041
|
+
for (const line of lines2) {
|
|
44042
|
+
const entry = parseRolloutEntry(line);
|
|
44043
|
+
if (!entry) continue;
|
|
44044
|
+
conversationId ||= sessionMetaId(entry);
|
|
44045
|
+
const payload = tokenCountPayload(entry);
|
|
44046
|
+
if (!payload) continue;
|
|
44047
|
+
applyTokenInfo(usage, byId, conversationId, payload.info);
|
|
44048
|
+
usage.rateLimits = toRateLimits(payload.rate_limits) ?? usage.rateLimits;
|
|
44049
|
+
}
|
|
44050
|
+
usage.responses = [...byId.values()];
|
|
44051
|
+
return usage;
|
|
44052
|
+
}
|
|
44053
|
+
async function readCodexRolloutUsage(rolloutPath) {
|
|
44054
|
+
const content = await fs55.promises.readFile(rolloutPath, "utf8");
|
|
44055
|
+
return codexRolloutUsage(content.split("\n"));
|
|
44056
|
+
}
|
|
44057
|
+
function sessionMetaId(entry) {
|
|
44058
|
+
if (entry.type !== "session_meta") return "";
|
|
44059
|
+
return rolloutString(rolloutPayload(entry).session_id);
|
|
44060
|
+
}
|
|
44061
|
+
function tokenCountPayload(entry) {
|
|
44062
|
+
if (entry.type !== "event_msg") return void 0;
|
|
44063
|
+
const payload = rolloutPayload(entry);
|
|
44064
|
+
return payload.type === "token_count" ? payload : void 0;
|
|
44065
|
+
}
|
|
44066
|
+
|
|
44067
|
+
// src/commands/sessions/daemon/applyCodexUsage.ts
|
|
44068
|
+
function applyCodexUsage(sessions, sessionId, usage, windows) {
|
|
44069
|
+
const session = sessions.get(sessionId);
|
|
44070
|
+
if (!session) return false;
|
|
44071
|
+
if (usage.usedPct !== void 0) session.usedPct = usage.usedPct;
|
|
44072
|
+
const phase = sessionBacklogPhase(session);
|
|
44073
|
+
if (phase)
|
|
44074
|
+
void persistPhaseTokens(
|
|
44075
|
+
phase.itemId,
|
|
44076
|
+
phase.phaseIdx,
|
|
44077
|
+
async () => usage.responses,
|
|
44078
|
+
usage.usedPct,
|
|
44079
|
+
windows
|
|
44080
|
+
);
|
|
44081
|
+
void flushPhaseActiveMs(session);
|
|
44082
|
+
return true;
|
|
44083
|
+
}
|
|
44084
|
+
|
|
44085
|
+
// src/commands/sessions/daemon/handleCodexUsage.ts
|
|
44086
|
+
var handleCodexUsage = (client, m, d) => {
|
|
44087
|
+
if (m.windowsProxy.route(client, d)) return;
|
|
44088
|
+
const sessionId = d.sessionId;
|
|
44089
|
+
if (d.ack) sendTo(client, { type: "ack", sessionId });
|
|
44090
|
+
void readCodexRolloutUsage(d.transcriptPath).then(
|
|
44091
|
+
(usage) => {
|
|
44092
|
+
if (usage.rateLimits)
|
|
44093
|
+
m.clients.updateHarnessLimits("codex", usage.rateLimits);
|
|
44094
|
+
const windows = m.clients.currentWindows("codex");
|
|
44095
|
+
m.update(
|
|
44096
|
+
(sessions) => applyCodexUsage(sessions, sessionId, usage, windows)
|
|
44097
|
+
);
|
|
44098
|
+
},
|
|
44099
|
+
(error) => daemonLog(`codex-usage read failed: ${String(error)}`)
|
|
44100
|
+
);
|
|
44101
|
+
};
|
|
44102
|
+
|
|
43891
44103
|
// src/commands/sessions/daemon/spawnContextFrom.ts
|
|
43892
44104
|
function spawnContextFrom(d) {
|
|
43893
44105
|
return {
|
|
@@ -43948,7 +44160,7 @@ function handleSetStatus(client, m, d) {
|
|
|
43948
44160
|
}
|
|
43949
44161
|
|
|
43950
44162
|
// src/commands/sessions/shared/parseTranscript.ts
|
|
43951
|
-
import * as
|
|
44163
|
+
import * as fs56 from "fs";
|
|
43952
44164
|
|
|
43953
44165
|
// src/commands/sessions/shared/codex/findCodexRolloutPath.ts
|
|
43954
44166
|
import * as path90 from "path";
|
|
@@ -44031,7 +44243,7 @@ async function parseTranscript(sessionId) {
|
|
|
44031
44243
|
}
|
|
44032
44244
|
async function readMessages(filePath, parse5) {
|
|
44033
44245
|
try {
|
|
44034
|
-
const raw = await
|
|
44246
|
+
const raw = await fs56.promises.readFile(filePath, "utf8");
|
|
44035
44247
|
return parse5(raw.split("\n"));
|
|
44036
44248
|
} catch {
|
|
44037
44249
|
return [];
|
|
@@ -44225,6 +44437,7 @@ var messageHandlers = {
|
|
|
44225
44437
|
d.transcriptPath,
|
|
44226
44438
|
d.usedPct
|
|
44227
44439
|
),
|
|
44440
|
+
"codex-usage": handleCodexUsage,
|
|
44228
44441
|
input: routed(
|
|
44229
44442
|
(_client, m, d) => m.writeToSession(d.sessionId, d.data)
|
|
44230
44443
|
),
|
|
@@ -44596,17 +44809,17 @@ async function renameSession(title) {
|
|
|
44596
44809
|
}
|
|
44597
44810
|
|
|
44598
44811
|
// src/commands/sessions/summarise/index.ts
|
|
44599
|
-
import * as
|
|
44812
|
+
import * as fs58 from "fs";
|
|
44600
44813
|
import chalk246 from "chalk";
|
|
44601
44814
|
|
|
44602
44815
|
// src/commands/sessions/summarise/shared.ts
|
|
44603
|
-
import * as
|
|
44816
|
+
import * as fs57 from "fs";
|
|
44604
44817
|
function writeSummary(jsonlPath2, summary) {
|
|
44605
|
-
|
|
44818
|
+
fs57.writeFileSync(summaryPathFor(jsonlPath2), `${summary.trim()}
|
|
44606
44819
|
`, "utf8");
|
|
44607
44820
|
}
|
|
44608
44821
|
function hasSummary(jsonlPath2) {
|
|
44609
|
-
return
|
|
44822
|
+
return fs57.existsSync(summaryPathFor(jsonlPath2));
|
|
44610
44823
|
}
|
|
44611
44824
|
function summaryPathFor(jsonlPath2) {
|
|
44612
44825
|
return jsonlPath2.replace(/\.jsonl$/, ".summary");
|
|
@@ -44678,7 +44891,7 @@ function selectCandidates(files, options2) {
|
|
|
44678
44891
|
const candidates = options2.force ? files : files.filter((f) => !hasSummary(f));
|
|
44679
44892
|
candidates.sort((a, b) => {
|
|
44680
44893
|
try {
|
|
44681
|
-
return
|
|
44894
|
+
return fs58.statSync(b).mtimeMs - fs58.statSync(a).mtimeMs;
|
|
44682
44895
|
} catch {
|
|
44683
44896
|
return 0;
|
|
44684
44897
|
}
|