johns-harness 2026.10.21 ā 2026.10.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/CHANGELOG.md +8 -0
- package/README.md +3 -0
- package/dist/auth-profiles-5CHn7vq1.js +4 -2
- package/dist/compact-B247y5Qt.js +28 -69
- package/dist/config-koj5M_EO.js +4 -2
- package/dist/daemon-cli.js +4 -2
- package/dist/model-selection-BGlGpPgM.js +4 -2
- package/dist/model-selection-BU6wl1le.js +4 -2
- package/dist/model-selection-L7RMwsG-.js +4 -2
- package/dist/pi-embedded-C6ITuRXf.js +29 -84
- package/dist/pi-embedded-DoQsYfIY.js +29 -84
- package/dist/plugin-sdk/bluebubbles.js +2 -1
- package/dist/plugin-sdk/config/types.models.d.ts +1 -0
- package/dist/plugin-sdk/config/zod-schema.core.d.ts +3 -0
- package/dist/plugin-sdk/config/zod-schema.d.ts +1 -0
- package/dist/plugin-sdk/config-4Fgm-yEH.js +4 -2
- package/dist/plugin-sdk/config-Di-lyCdh.js +4 -2
- package/dist/plugin-sdk/discord.js +4 -2
- package/dist/plugin-sdk/dispatch-BP0viZiL.js +29 -84
- package/dist/plugin-sdk/dispatch-CQsjmw7g.js +29 -84
- package/dist/plugin-sdk/dispatch-Cerq29sy.js +29 -84
- package/dist/plugin-sdk/dispatch-Cndjtt0g.js +29 -84
- package/dist/plugin-sdk/dispatch-UogiJYul.js +29 -84
- package/dist/plugin-sdk/feishu.js +2 -1
- package/dist/plugin-sdk/googlechat.js +2 -1
- package/dist/plugin-sdk/imessage.js +2 -1
- package/dist/plugin-sdk/model-auth-CX9cPHdC.js +4 -2
- package/dist/plugin-sdk/model-auth-Ciehz0x5.js +4 -2
- package/dist/plugin-sdk/model-auth-DVyo6JSX.js +4 -2
- package/dist/plugin-sdk/model-auth-kiHYHGrs.js +4 -2
- package/dist/plugin-sdk/model-selection-Dbj4jDYM.js +4 -2
- package/dist/plugin-sdk/nostr.js +2 -1
- package/dist/plugin-sdk/reply-DbZnH8-h.js +29 -84
- package/dist/plugin-sdk/signal.js +2 -1
- package/dist/plugin-sdk/slack.js +2 -1
- package/dist/plugin-sdk/telegram.js +2 -1
- package/dist/plugin-sdk/tlon.js +2 -1
- package/dist/plugin-sdk/twitch.js +2 -1
- package/dist/plugin-sdk/utils/usage-format.d.ts +1 -0
- package/dist/plugin-sdk/voice-call.js +2 -1
- package/dist/plugin-sdk/zalo.js +2 -1
- package/dist/plugin-sdk/zalouser.js +2 -1
- package/dist/reply-C5LKjXcC.js +28 -69
- package/dist/session-cost-usage-5CCZbVhO.js +4 -19
- package/dist/session-cost-usage-CkCo2kVx.js +4 -19
- package/dist/usage-billing.js +88 -0
- package/dist/usage-format-CNGi1eHQ.js +6 -11
- package/dist/usage-format-CvaU1ntA.js +6 -11
- package/docs/PATCHES.md +13 -1
- package/docs/providers/anthropic.md +8 -2
- package/docs/reference/token-use.md +11 -1
- package/models/models.json +33 -33
- package/node_modules/@mariozechner/pi-ai/dist/models.d.ts +1 -0
- package/node_modules/@mariozechner/pi-ai/dist/models.generated.js +32 -32
- package/node_modules/@mariozechner/pi-ai/dist/models.js +16 -5
- package/node_modules/@mariozechner/pi-ai/dist/providers/anthropic.js +15 -0
- package/node_modules/@mariozechner/pi-ai/dist/providers/google-vertex.js +1 -1
- package/node_modules/@mariozechner/pi-ai/dist/providers/google.js +1 -1
- package/node_modules/@mariozechner/pi-ai/dist/providers/openai-completions.js +5 -5
- package/node_modules/@mariozechner/pi-ai/dist/types.d.ts +3 -0
- package/node_modules/@mariozechner/pi-coding-agent/dist/core/model-registry.js +3 -0
- package/package.json +1 -1
- package/scripts/models-sync.mjs +1 -0
- package/vendor/patched-deps/@mariozechner/pi-ai/dist/models.d.ts +25 -0
- package/vendor/patched-deps/@mariozechner/pi-ai/dist/models.generated.js +32 -32
- package/vendor/patched-deps/@mariozechner/pi-ai/dist/models.js +16 -5
- package/vendor/patched-deps/@mariozechner/pi-ai/dist/providers/anthropic.js +15 -0
- package/vendor/patched-deps/@mariozechner/pi-ai/dist/providers/google-vertex.js +371 -0
- package/vendor/patched-deps/@mariozechner/pi-ai/dist/providers/google.js +1 -1
- package/vendor/patched-deps/@mariozechner/pi-ai/dist/providers/openai-completions.js +5 -5
- package/vendor/patched-deps/@mariozechner/pi-ai/dist/types.d.ts +282 -0
- package/vendor/patched-deps/@mariozechner/pi-coding-agent/dist/core/model-registry.js +3 -0
|
@@ -4828,7 +4828,8 @@ function resolveModelCost(raw) {
|
|
|
4828
4828
|
input: typeof raw?.input === "number" ? raw.input : DEFAULT_MODEL_COST.input,
|
|
4829
4829
|
output: typeof raw?.output === "number" ? raw.output : DEFAULT_MODEL_COST.output,
|
|
4830
4830
|
cacheRead: typeof raw?.cacheRead === "number" ? raw.cacheRead : DEFAULT_MODEL_COST.cacheRead,
|
|
4831
|
-
cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite
|
|
4831
|
+
cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite,
|
|
4832
|
+
...typeof raw?.cacheWrite1h === "number" ? { cacheWrite1h: raw.cacheWrite1h } : {} // JOHNNESS_PATCH_USAGE_BILLING_91
|
|
4832
4833
|
};
|
|
4833
4834
|
}
|
|
4834
4835
|
function resolveAnthropicDefaultAuthMode(cfg) {
|
|
@@ -7914,7 +7915,8 @@ const ModelDefinitionSchema = z.object({
|
|
|
7914
7915
|
input: z.number().optional(),
|
|
7915
7916
|
output: z.number().optional(),
|
|
7916
7917
|
cacheRead: z.number().optional(),
|
|
7917
|
-
cacheWrite: z.number().optional()
|
|
7918
|
+
cacheWrite: z.number().optional(),
|
|
7919
|
+
cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
|
|
7918
7920
|
}).strict().optional(),
|
|
7919
7921
|
contextWindow: z.number().positive().optional(),
|
|
7920
7922
|
maxTokens: z.number().positive().optional(),
|
package/dist/plugin-sdk/nostr.js
CHANGED
|
@@ -295,7 +295,8 @@ const ModelDefinitionSchema = z.object({
|
|
|
295
295
|
input: z.number().optional(),
|
|
296
296
|
output: z.number().optional(),
|
|
297
297
|
cacheRead: z.number().optional(),
|
|
298
|
-
cacheWrite: z.number().optional()
|
|
298
|
+
cacheWrite: z.number().optional(),
|
|
299
|
+
cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
|
|
299
300
|
}).strict().optional(),
|
|
300
301
|
contextWindow: z.number().positive().optional(),
|
|
301
302
|
maxTokens: z.number().positive().optional(),
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { normalizeBillingUsage, readLatestBillingUsage, formatBillingCost } from "../usage-billing.js"; // JOHNNESS_PATCH_USAGE_BILLING_91 dist/plugin-sdk/reply-DbZnH8-h.js
|
|
1
2
|
import { importOptional66 } from "../optional-module.js";
|
|
2
3
|
const JOHNNESS_PATCH_SLIM_TREE_66 = true;
|
|
3
4
|
import { createImageGenerateTool } from "../image-generate.js";
|
|
@@ -38302,21 +38303,7 @@ function hasNonzeroUsage(usage) {
|
|
|
38302
38303
|
].some((v) => typeof v === "number" && Number.isFinite(v) && v > 0);
|
|
38303
38304
|
}
|
|
38304
38305
|
function normalizeUsage(raw) {
|
|
38305
|
-
|
|
38306
|
-
const rawInput = asFiniteNumber(raw.input ?? raw.inputTokens ?? raw.input_tokens ?? raw.promptTokens ?? raw.prompt_tokens);
|
|
38307
|
-
const input = rawInput !== void 0 && rawInput < 0 ? 0 : rawInput;
|
|
38308
|
-
const output = asFiniteNumber(raw.output ?? raw.outputTokens ?? raw.output_tokens ?? raw.completionTokens ?? raw.completion_tokens);
|
|
38309
|
-
const cacheRead = asFiniteNumber(raw.cacheRead ?? raw.cache_read ?? raw.cache_read_input_tokens ?? raw.cached_tokens ?? raw.prompt_tokens_details?.cached_tokens);
|
|
38310
|
-
const cacheWrite = asFiniteNumber(raw.cacheWrite ?? raw.cache_write ?? raw.cache_creation_input_tokens);
|
|
38311
|
-
const total = asFiniteNumber(raw.total ?? raw.totalTokens ?? raw.total_tokens);
|
|
38312
|
-
if (input === void 0 && output === void 0 && cacheRead === void 0 && cacheWrite === void 0 && total === void 0) return;
|
|
38313
|
-
return {
|
|
38314
|
-
input,
|
|
38315
|
-
output,
|
|
38316
|
-
cacheRead,
|
|
38317
|
-
cacheWrite,
|
|
38318
|
-
total
|
|
38319
|
-
};
|
|
38306
|
+
return normalizeBillingUsage(raw);
|
|
38320
38307
|
}
|
|
38321
38308
|
function derivePromptTokens(usage) {
|
|
38322
38309
|
if (!usage) return;
|
|
@@ -52897,62 +52884,19 @@ const readUsageFromSessionLog = (sessionId, sessionEntry, agentId, sessionKey, s
|
|
|
52897
52884
|
} catch {
|
|
52898
52885
|
return;
|
|
52899
52886
|
}
|
|
52900
|
-
|
|
52901
|
-
try {
|
|
52902
|
-
const TAIL_BYTES = 8192;
|
|
52903
|
-
const stat = fs.statSync(logPath);
|
|
52904
|
-
const offset = Math.max(0, stat.size - TAIL_BYTES);
|
|
52905
|
-
const buf = Buffer.alloc(Math.min(TAIL_BYTES, stat.size));
|
|
52906
|
-
const fd = fs.openSync(logPath, "r");
|
|
52907
|
-
try {
|
|
52908
|
-
fs.readSync(fd, buf, 0, buf.length, offset);
|
|
52909
|
-
} finally {
|
|
52910
|
-
fs.closeSync(fd);
|
|
52911
|
-
}
|
|
52912
|
-
const tail = buf.toString("utf-8");
|
|
52913
|
-
const lines = (offset > 0 ? tail.slice(tail.indexOf("\n") + 1) : tail).split(/\n+/);
|
|
52914
|
-
let input = 0;
|
|
52915
|
-
let output = 0;
|
|
52916
|
-
let promptTokens = 0;
|
|
52917
|
-
let model;
|
|
52918
|
-
let lastUsage;
|
|
52919
|
-
for (const line of lines) {
|
|
52920
|
-
if (!line.trim()) continue;
|
|
52921
|
-
try {
|
|
52922
|
-
const parsed = JSON.parse(line);
|
|
52923
|
-
const usage = normalizeUsage(parsed.message?.usage ?? parsed.usage);
|
|
52924
|
-
if (usage) lastUsage = usage;
|
|
52925
|
-
model = parsed.message?.model ?? parsed.model ?? model;
|
|
52926
|
-
} catch {}
|
|
52927
|
-
}
|
|
52928
|
-
if (!lastUsage) return;
|
|
52929
|
-
input = lastUsage.input ?? 0;
|
|
52930
|
-
output = lastUsage.output ?? 0;
|
|
52931
|
-
promptTokens = derivePromptTokens(lastUsage) ?? lastUsage.total ?? input + output;
|
|
52932
|
-
const total = lastUsage.total ?? promptTokens + output;
|
|
52933
|
-
if (promptTokens === 0 && total === 0) return;
|
|
52934
|
-
return {
|
|
52935
|
-
input,
|
|
52936
|
-
output,
|
|
52937
|
-
promptTokens,
|
|
52938
|
-
total,
|
|
52939
|
-
model
|
|
52940
|
-
};
|
|
52941
|
-
} catch {
|
|
52942
|
-
return;
|
|
52943
|
-
}
|
|
52887
|
+
return readLatestBillingUsage(logPath);
|
|
52944
52888
|
};
|
|
52945
|
-
const formatUsagePair = (input, output) => {
|
|
52946
|
-
if (input == null && output == null) return null;
|
|
52947
|
-
|
|
52889
|
+
const formatUsagePair = (input, output, cacheRead, cacheWrite) => {
|
|
52890
|
+
if (input == null && output == null && cacheRead == null && cacheWrite == null) return null;
|
|
52891
|
+
const count = value => typeof value === "number" ? value.toLocaleString("en-US") : "?";
|
|
52892
|
+
return `š§® Tokens: ${count((input ?? 0) + (cacheWrite ?? 0))} new in / ${count(cacheRead ?? 0)} cached / ${count(output)} out (last API call)`;
|
|
52948
52893
|
};
|
|
52949
|
-
const formatCacheLine = (input, cacheRead, cacheWrite) => {
|
|
52894
|
+
const formatCacheLine = (input, cacheRead, cacheWrite, usage) => {
|
|
52950
52895
|
if (!cacheRead && !cacheWrite) return null;
|
|
52951
|
-
|
|
52952
|
-
const
|
|
52953
|
-
const
|
|
52954
|
-
|
|
52955
|
-
return `šļø Cache: ${totalInput > 0 && typeof cacheRead === "number" ? Math.round(cacheRead / totalInput * 100) : 0}% hit Ā· ${cachedLabel} cached, ${newLabel} new`;
|
|
52896
|
+
const total = (input ?? 0) + (cacheRead ?? 0) + (cacheWrite ?? 0);
|
|
52897
|
+
const count = value => (value ?? 0).toLocaleString("en-US");
|
|
52898
|
+
const ttl = usage?.cacheWrite5m !== undefined || usage?.cacheWrite1h !== undefined ? ` (${count(usage.cacheWrite5m)} 5m / ${count(usage.cacheWrite1h)} 1h)` : "";
|
|
52899
|
+
return `šļø Cache: ${total > 0 ? Math.round((cacheRead ?? 0) / total * 100) : 0}% hit Ā· ${count(input)} uncached + ${count(cacheWrite)} written${ttl}`;
|
|
52956
52900
|
};
|
|
52957
52901
|
const formatMediaUnderstandingLine = (decisions) => {
|
|
52958
52902
|
if (!decisions || decisions.length === 0) return null;
|
|
@@ -53031,10 +52975,14 @@ function buildStatusMessage(args) {
|
|
|
53031
52975
|
let outputTokens = entry?.outputTokens;
|
|
53032
52976
|
let cacheRead = entry?.cacheRead;
|
|
53033
52977
|
let cacheWrite = entry?.cacheWrite;
|
|
52978
|
+
let billingUsage;
|
|
53034
52979
|
let totalTokens = entry?.totalTokens ?? (entry?.inputTokens ?? 0) + (entry?.outputTokens ?? 0);
|
|
53035
|
-
if (
|
|
52980
|
+
if (entry?.sessionId) { // Always use a coherent latest API call on both status paths.
|
|
53036
52981
|
const logUsage = readUsageFromSessionLog(entry?.sessionId, entry, args.agentId, args.sessionKey, args.sessionStorePath);
|
|
53037
52982
|
if (logUsage) {
|
|
52983
|
+
billingUsage = logUsage;
|
|
52984
|
+
cacheRead = logUsage.cacheRead ?? 0;
|
|
52985
|
+
cacheWrite = logUsage.cacheWrite ?? 0;
|
|
53038
52986
|
const candidate = logUsage.promptTokens || logUsage.total;
|
|
53039
52987
|
if (!totalTokens || totalTokens === 0 || candidate > totalTokens) totalTokens = candidate;
|
|
53040
52988
|
if (!entry?.model && logUsage.model) {
|
|
@@ -53053,8 +53001,10 @@ function buildStatusMessage(args) {
|
|
|
53053
53001
|
model: logUsage.model,
|
|
53054
53002
|
fallbackContextTokens: contextTokens ?? void 0
|
|
53055
53003
|
}) ?? contextTokens;
|
|
53056
|
-
|
|
53057
|
-
|
|
53004
|
+
inputTokens = logUsage.input ?? 0;
|
|
53005
|
+
outputTokens = logUsage.output ?? 0;
|
|
53006
|
+
if (logUsage.provider) activeProvider = logUsage.provider;
|
|
53007
|
+
if (logUsage.model) activeModel = logUsage.model;
|
|
53058
53008
|
}
|
|
53059
53009
|
}
|
|
53060
53010
|
const thinkLevel = args.resolvedThink ?? args.sessionEntry?.thinkingLevel ?? args.agent?.thinkingDefault ?? "off";
|
|
@@ -53090,21 +53040,14 @@ function buildStatusMessage(args) {
|
|
|
53090
53040
|
state: entry
|
|
53091
53041
|
});
|
|
53092
53042
|
const effectiveCostAuthMode = fallbackState.active ? activeAuthMode : selectedAuthMode ?? activeAuthMode;
|
|
53093
|
-
const showCost = effectiveCostAuthMode === "api-key" || effectiveCostAuthMode === "mixed";
|
|
53043
|
+
const showCost = billingUsage?.cost?.total > 0 || effectiveCostAuthMode === "api-key" || effectiveCostAuthMode === "mixed";
|
|
53094
53044
|
const costConfig = showCost ? resolveModelCostConfig({
|
|
53095
53045
|
provider: activeProvider,
|
|
53096
53046
|
model: activeModel,
|
|
53097
53047
|
config: args.config
|
|
53098
53048
|
}) : void 0;
|
|
53099
|
-
const hasUsage =
|
|
53100
|
-
|
|
53101
|
-
usage: {
|
|
53102
|
-
input: inputTokens ?? void 0,
|
|
53103
|
-
output: outputTokens ?? void 0
|
|
53104
|
-
},
|
|
53105
|
-
cost: costConfig
|
|
53106
|
-
}) : void 0;
|
|
53107
|
-
const costLabel = showCost && hasUsage ? formatUsd(cost) : void 0;
|
|
53049
|
+
const hasUsage = [inputTokens, outputTokens, cacheRead, cacheWrite].some(value => typeof value === "number");
|
|
53050
|
+
billingUsage ??= { input: inputTokens, output: outputTokens, cacheRead, cacheWrite };
|
|
53108
53051
|
const selectedAuthLabel = selectedAuthLabelValue ? ` Ā· š ${selectedAuthLabelValue}` : "";
|
|
53109
53052
|
const channelModelNote = (() => {
|
|
53110
53053
|
if (!args.config || !entry) return;
|
|
@@ -53136,9 +53079,9 @@ function buildStatusMessage(args) {
|
|
|
53136
53079
|
const fallbackLine = fallbackState.active ? `āŖļø Fallback: ${activeModelLabel}${showFallbackAuth ? ` Ā· š ${activeAuthLabelValue}` : ""} (${fallbackState.reason ?? "selected model unavailable"})` : null;
|
|
53137
53080
|
const commit = resolveCommitHash();
|
|
53138
53081
|
const versionLine = `š¦ Johnness ${VERSION}${commit ? ` (${commit})` : ""}`;
|
|
53139
|
-
const usagePair = formatUsagePair(inputTokens, outputTokens);
|
|
53140
|
-
const cacheLine = formatCacheLine(inputTokens, cacheRead, cacheWrite);
|
|
53141
|
-
const costLine =
|
|
53082
|
+
const usagePair = formatUsagePair(inputTokens, outputTokens, cacheRead, cacheWrite);
|
|
53083
|
+
const cacheLine = formatCacheLine(inputTokens, cacheRead, cacheWrite, billingUsage);
|
|
53084
|
+
const costLine = showCost && hasUsage ? formatBillingCost(billingUsage, costConfig) : null;
|
|
53142
53085
|
const usageCostLine = usagePair && costLine ? `${usagePair} Ā· ${costLine}` : usagePair ?? costLine;
|
|
53143
53086
|
const mediaLine = formatMediaUnderstandingLine(args.mediaDecisions);
|
|
53144
53087
|
const voiceLine = formatVoiceModeLine(args.config, args.sessionEntry);
|
|
@@ -82724,6 +82667,8 @@ function normalizeAssistantUsageSnapshot(usage) {
|
|
|
82724
82667
|
cacheRead,
|
|
82725
82668
|
cacheWrite,
|
|
82726
82669
|
totalTokens,
|
|
82670
|
+
...normalized.cacheWrite5m !== undefined ? { cacheWrite5m: normalized.cacheWrite5m } : {},
|
|
82671
|
+
...normalized.cacheWrite1h !== undefined ? { cacheWrite1h: normalized.cacheWrite1h } : {},
|
|
82727
82672
|
...cost ? { cost } : {}
|
|
82728
82673
|
};
|
|
82729
82674
|
}
|
|
@@ -4060,7 +4060,8 @@ const ModelDefinitionSchema = z.object({
|
|
|
4060
4060
|
input: z.number().optional(),
|
|
4061
4061
|
output: z.number().optional(),
|
|
4062
4062
|
cacheRead: z.number().optional(),
|
|
4063
|
-
cacheWrite: z.number().optional()
|
|
4063
|
+
cacheWrite: z.number().optional(),
|
|
4064
|
+
cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
|
|
4064
4065
|
}).strict().optional(),
|
|
4065
4066
|
contextWindow: z.number().positive().optional(),
|
|
4066
4067
|
maxTokens: z.number().positive().optional(),
|
package/dist/plugin-sdk/slack.js
CHANGED
|
@@ -4630,7 +4630,8 @@ const ModelDefinitionSchema = z.object({
|
|
|
4630
4630
|
input: z.number().optional(),
|
|
4631
4631
|
output: z.number().optional(),
|
|
4632
4632
|
cacheRead: z.number().optional(),
|
|
4633
|
-
cacheWrite: z.number().optional()
|
|
4633
|
+
cacheWrite: z.number().optional(),
|
|
4634
|
+
cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
|
|
4634
4635
|
}).strict().optional(),
|
|
4635
4636
|
contextWindow: z.number().positive().optional(),
|
|
4636
4637
|
maxTokens: z.number().positive().optional(),
|
|
@@ -4781,7 +4781,8 @@ const ModelDefinitionSchema = z.object({
|
|
|
4781
4781
|
input: z.number().optional(),
|
|
4782
4782
|
output: z.number().optional(),
|
|
4783
4783
|
cacheRead: z.number().optional(),
|
|
4784
|
-
cacheWrite: z.number().optional()
|
|
4784
|
+
cacheWrite: z.number().optional(),
|
|
4785
|
+
cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
|
|
4785
4786
|
}).strict().optional(),
|
|
4786
4787
|
contextWindow: z.number().positive().optional(),
|
|
4787
4788
|
maxTokens: z.number().positive().optional(),
|
package/dist/plugin-sdk/tlon.js
CHANGED
|
@@ -3087,7 +3087,8 @@ const ModelDefinitionSchema = z.object({
|
|
|
3087
3087
|
input: z.number().optional(),
|
|
3088
3088
|
output: z.number().optional(),
|
|
3089
3089
|
cacheRead: z.number().optional(),
|
|
3090
|
-
cacheWrite: z.number().optional()
|
|
3090
|
+
cacheWrite: z.number().optional(),
|
|
3091
|
+
cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
|
|
3091
3092
|
}).strict().optional(),
|
|
3092
3093
|
contextWindow: z.number().positive().optional(),
|
|
3093
3094
|
maxTokens: z.number().positive().optional(),
|
|
@@ -2846,7 +2846,8 @@ const ModelDefinitionSchema = z.object({
|
|
|
2846
2846
|
input: z.number().optional(),
|
|
2847
2847
|
output: z.number().optional(),
|
|
2848
2848
|
cacheRead: z.number().optional(),
|
|
2849
|
-
cacheWrite: z.number().optional()
|
|
2849
|
+
cacheWrite: z.number().optional(),
|
|
2850
|
+
cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
|
|
2850
2851
|
}).strict().optional(),
|
|
2851
2852
|
contextWindow: z.number().positive().optional(),
|
|
2852
2853
|
maxTokens: z.number().positive().optional(),
|
|
@@ -181,7 +181,8 @@ const ModelDefinitionSchema = z.object({
|
|
|
181
181
|
input: z.number().optional(),
|
|
182
182
|
output: z.number().optional(),
|
|
183
183
|
cacheRead: z.number().optional(),
|
|
184
|
-
cacheWrite: z.number().optional()
|
|
184
|
+
cacheWrite: z.number().optional(),
|
|
185
|
+
cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
|
|
185
186
|
}).strict().optional(),
|
|
186
187
|
contextWindow: z.number().positive().optional(),
|
|
187
188
|
maxTokens: z.number().positive().optional(),
|
package/dist/plugin-sdk/zalo.js
CHANGED
|
@@ -3466,7 +3466,8 @@ const ModelDefinitionSchema = z.object({
|
|
|
3466
3466
|
input: z.number().optional(),
|
|
3467
3467
|
output: z.number().optional(),
|
|
3468
3468
|
cacheRead: z.number().optional(),
|
|
3469
|
-
cacheWrite: z.number().optional()
|
|
3469
|
+
cacheWrite: z.number().optional(),
|
|
3470
|
+
cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
|
|
3470
3471
|
}).strict().optional(),
|
|
3471
3472
|
contextWindow: z.number().positive().optional(),
|
|
3472
3473
|
maxTokens: z.number().positive().optional(),
|
|
@@ -3505,7 +3505,8 @@ const ModelDefinitionSchema = z.object({
|
|
|
3505
3505
|
input: z.number().optional(),
|
|
3506
3506
|
output: z.number().optional(),
|
|
3507
3507
|
cacheRead: z.number().optional(),
|
|
3508
|
-
cacheWrite: z.number().optional()
|
|
3508
|
+
cacheWrite: z.number().optional(),
|
|
3509
|
+
cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
|
|
3509
3510
|
}).strict().optional(),
|
|
3510
3511
|
contextWindow: z.number().positive().optional(),
|
|
3511
3512
|
maxTokens: z.number().positive().optional(),
|
package/dist/reply-C5LKjXcC.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { normalizeBillingUsage, readLatestBillingUsage, formatBillingCost } from "./usage-billing.js"; // JOHNNESS_PATCH_USAGE_BILLING_91 dist/reply-C5LKjXcC.js
|
|
1
2
|
import { importOptional66 } from "./optional-module.js";
|
|
2
3
|
const JOHNNESS_PATCH_SLIM_TREE_66 = true;
|
|
3
4
|
import { createImageGenerateTool } from "./image-generate.js";
|
|
@@ -46583,62 +46584,19 @@ const readUsageFromSessionLog = (sessionId, sessionEntry, agentId, sessionKey, s
|
|
|
46583
46584
|
} catch {
|
|
46584
46585
|
return;
|
|
46585
46586
|
}
|
|
46586
|
-
|
|
46587
|
-
try {
|
|
46588
|
-
const TAIL_BYTES = 8192;
|
|
46589
|
-
const stat = fs.statSync(logPath);
|
|
46590
|
-
const offset = Math.max(0, stat.size - TAIL_BYTES);
|
|
46591
|
-
const buf = Buffer.alloc(Math.min(TAIL_BYTES, stat.size));
|
|
46592
|
-
const fd = fs.openSync(logPath, "r");
|
|
46593
|
-
try {
|
|
46594
|
-
fs.readSync(fd, buf, 0, buf.length, offset);
|
|
46595
|
-
} finally {
|
|
46596
|
-
fs.closeSync(fd);
|
|
46597
|
-
}
|
|
46598
|
-
const tail = buf.toString("utf-8");
|
|
46599
|
-
const lines = (offset > 0 ? tail.slice(tail.indexOf("\n") + 1) : tail).split(/\n+/);
|
|
46600
|
-
let input = 0;
|
|
46601
|
-
let output = 0;
|
|
46602
|
-
let promptTokens = 0;
|
|
46603
|
-
let model;
|
|
46604
|
-
let lastUsage;
|
|
46605
|
-
for (const line of lines) {
|
|
46606
|
-
if (!line.trim()) continue;
|
|
46607
|
-
try {
|
|
46608
|
-
const parsed = JSON.parse(line);
|
|
46609
|
-
const usage = normalizeUsage(parsed.message?.usage ?? parsed.usage);
|
|
46610
|
-
if (usage) lastUsage = usage;
|
|
46611
|
-
model = parsed.message?.model ?? parsed.model ?? model;
|
|
46612
|
-
} catch {}
|
|
46613
|
-
}
|
|
46614
|
-
if (!lastUsage) return;
|
|
46615
|
-
input = lastUsage.input ?? 0;
|
|
46616
|
-
output = lastUsage.output ?? 0;
|
|
46617
|
-
promptTokens = derivePromptTokens(lastUsage) ?? lastUsage.total ?? input + output;
|
|
46618
|
-
const total = lastUsage.total ?? promptTokens + output;
|
|
46619
|
-
if (promptTokens === 0 && total === 0) return;
|
|
46620
|
-
return {
|
|
46621
|
-
input,
|
|
46622
|
-
output,
|
|
46623
|
-
promptTokens,
|
|
46624
|
-
total,
|
|
46625
|
-
model
|
|
46626
|
-
};
|
|
46627
|
-
} catch {
|
|
46628
|
-
return;
|
|
46629
|
-
}
|
|
46587
|
+
return readLatestBillingUsage(logPath);
|
|
46630
46588
|
};
|
|
46631
|
-
const formatUsagePair = (input, output) => {
|
|
46632
|
-
if (input == null && output == null) return null;
|
|
46633
|
-
|
|
46589
|
+
const formatUsagePair = (input, output, cacheRead, cacheWrite) => {
|
|
46590
|
+
if (input == null && output == null && cacheRead == null && cacheWrite == null) return null;
|
|
46591
|
+
const count = value => typeof value === "number" ? value.toLocaleString("en-US") : "?";
|
|
46592
|
+
return `š§® Tokens: ${count((input ?? 0) + (cacheWrite ?? 0))} new in / ${count(cacheRead ?? 0)} cached / ${count(output)} out (last API call)`;
|
|
46634
46593
|
};
|
|
46635
|
-
const formatCacheLine = (input, cacheRead, cacheWrite) => {
|
|
46594
|
+
const formatCacheLine = (input, cacheRead, cacheWrite, usage) => {
|
|
46636
46595
|
if (!cacheRead && !cacheWrite) return null;
|
|
46637
|
-
|
|
46638
|
-
const
|
|
46639
|
-
const
|
|
46640
|
-
|
|
46641
|
-
return `šļø Cache: ${totalInput > 0 && typeof cacheRead === "number" ? Math.round(cacheRead / totalInput * 100) : 0}% hit Ā· ${cachedLabel} cached, ${newLabel} new`;
|
|
46596
|
+
const total = (input ?? 0) + (cacheRead ?? 0) + (cacheWrite ?? 0);
|
|
46597
|
+
const count = value => (value ?? 0).toLocaleString("en-US");
|
|
46598
|
+
const ttl = usage?.cacheWrite5m !== undefined || usage?.cacheWrite1h !== undefined ? ` (${count(usage.cacheWrite5m)} 5m / ${count(usage.cacheWrite1h)} 1h)` : "";
|
|
46599
|
+
return `šļø Cache: ${total > 0 ? Math.round((cacheRead ?? 0) / total * 100) : 0}% hit Ā· ${count(input)} uncached + ${count(cacheWrite)} written${ttl}`;
|
|
46642
46600
|
};
|
|
46643
46601
|
const formatMediaUnderstandingLine = (decisions) => {
|
|
46644
46602
|
if (!decisions || decisions.length === 0) return null;
|
|
@@ -46717,10 +46675,14 @@ function buildStatusMessage(args) {
|
|
|
46717
46675
|
let outputTokens = entry?.outputTokens;
|
|
46718
46676
|
let cacheRead = entry?.cacheRead;
|
|
46719
46677
|
let cacheWrite = entry?.cacheWrite;
|
|
46678
|
+
let billingUsage;
|
|
46720
46679
|
let totalTokens = entry?.totalTokens ?? (entry?.inputTokens ?? 0) + (entry?.outputTokens ?? 0);
|
|
46721
|
-
if (
|
|
46680
|
+
if (entry?.sessionId) { // Always use a coherent latest API call on both status paths.
|
|
46722
46681
|
const logUsage = readUsageFromSessionLog(entry?.sessionId, entry, args.agentId, args.sessionKey, args.sessionStorePath);
|
|
46723
46682
|
if (logUsage) {
|
|
46683
|
+
billingUsage = logUsage;
|
|
46684
|
+
cacheRead = logUsage.cacheRead ?? 0;
|
|
46685
|
+
cacheWrite = logUsage.cacheWrite ?? 0;
|
|
46724
46686
|
const candidate = logUsage.promptTokens || logUsage.total;
|
|
46725
46687
|
if (!totalTokens || totalTokens === 0 || candidate > totalTokens) totalTokens = candidate;
|
|
46726
46688
|
if (!entry?.model && logUsage.model) {
|
|
@@ -46739,8 +46701,10 @@ function buildStatusMessage(args) {
|
|
|
46739
46701
|
model: logUsage.model,
|
|
46740
46702
|
fallbackContextTokens: contextTokens ?? void 0
|
|
46741
46703
|
}) ?? contextTokens;
|
|
46742
|
-
|
|
46743
|
-
|
|
46704
|
+
inputTokens = logUsage.input ?? 0;
|
|
46705
|
+
outputTokens = logUsage.output ?? 0;
|
|
46706
|
+
if (logUsage.provider) activeProvider = logUsage.provider;
|
|
46707
|
+
if (logUsage.model) activeModel = logUsage.model;
|
|
46744
46708
|
}
|
|
46745
46709
|
}
|
|
46746
46710
|
const thinkLevel = args.resolvedThink ?? args.sessionEntry?.thinkingLevel ?? args.agent?.thinkingDefault ?? "off";
|
|
@@ -46776,21 +46740,14 @@ function buildStatusMessage(args) {
|
|
|
46776
46740
|
state: entry
|
|
46777
46741
|
});
|
|
46778
46742
|
const effectiveCostAuthMode = fallbackState.active ? activeAuthMode : selectedAuthMode ?? activeAuthMode;
|
|
46779
|
-
const showCost = effectiveCostAuthMode === "api-key" || effectiveCostAuthMode === "mixed";
|
|
46743
|
+
const showCost = billingUsage?.cost?.total > 0 || effectiveCostAuthMode === "api-key" || effectiveCostAuthMode === "mixed";
|
|
46780
46744
|
const costConfig = showCost ? resolveModelCostConfig({
|
|
46781
46745
|
provider: activeProvider,
|
|
46782
46746
|
model: activeModel,
|
|
46783
46747
|
config: args.config
|
|
46784
46748
|
}) : void 0;
|
|
46785
|
-
const hasUsage =
|
|
46786
|
-
|
|
46787
|
-
usage: {
|
|
46788
|
-
input: inputTokens ?? void 0,
|
|
46789
|
-
output: outputTokens ?? void 0
|
|
46790
|
-
},
|
|
46791
|
-
cost: costConfig
|
|
46792
|
-
}) : void 0;
|
|
46793
|
-
const costLabel = showCost && hasUsage ? formatUsd(cost) : void 0;
|
|
46749
|
+
const hasUsage = [inputTokens, outputTokens, cacheRead, cacheWrite].some(value => typeof value === "number");
|
|
46750
|
+
billingUsage ??= { input: inputTokens, output: outputTokens, cacheRead, cacheWrite };
|
|
46794
46751
|
const selectedAuthLabel = selectedAuthLabelValue ? ` Ā· š ${selectedAuthLabelValue}` : "";
|
|
46795
46752
|
const channelModelNote = (() => {
|
|
46796
46753
|
if (!args.config || !entry) return;
|
|
@@ -46822,9 +46779,9 @@ function buildStatusMessage(args) {
|
|
|
46822
46779
|
const fallbackLine = fallbackState.active ? `āŖļø Fallback: ${activeModelLabel}${showFallbackAuth ? ` Ā· š ${activeAuthLabelValue}` : ""} (${fallbackState.reason ?? "selected model unavailable"})` : null;
|
|
46823
46780
|
const commit = resolveCommitHash();
|
|
46824
46781
|
const versionLine = `š¦ Johnness ${VERSION}${commit ? ` (${commit})` : ""}`;
|
|
46825
|
-
const usagePair = formatUsagePair(inputTokens, outputTokens);
|
|
46826
|
-
const cacheLine = formatCacheLine(inputTokens, cacheRead, cacheWrite);
|
|
46827
|
-
const costLine =
|
|
46782
|
+
const usagePair = formatUsagePair(inputTokens, outputTokens, cacheRead, cacheWrite);
|
|
46783
|
+
const cacheLine = formatCacheLine(inputTokens, cacheRead, cacheWrite, billingUsage);
|
|
46784
|
+
const costLine = showCost && hasUsage ? formatBillingCost(billingUsage, costConfig) : null;
|
|
46828
46785
|
const usageCostLine = usagePair && costLine ? `${usagePair} Ā· ${costLine}` : usagePair ?? costLine;
|
|
46829
46786
|
const mediaLine = formatMediaUnderstandingLine(args.mediaDecisions);
|
|
46830
46787
|
const voiceLine = formatVoiceModeLine(args.config, args.sessionEntry);
|
|
@@ -74740,6 +74697,8 @@ function normalizeAssistantUsageSnapshot(usage) {
|
|
|
74740
74697
|
cacheRead,
|
|
74741
74698
|
cacheWrite,
|
|
74742
74699
|
totalTokens,
|
|
74700
|
+
...normalized.cacheWrite5m !== undefined ? { cacheWrite5m: normalized.cacheWrite5m } : {},
|
|
74701
|
+
...normalized.cacheWrite1h !== undefined ? { cacheWrite1h: normalized.cacheWrite1h } : {},
|
|
74743
74702
|
...cost ? { cost } : {}
|
|
74744
74703
|
};
|
|
74745
74704
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { normalizeBillingUsage, readLatestBillingUsage, formatBillingCost, billingCost } from "./usage-billing.js"; // JOHNNESS_PATCH_USAGE_BILLING_91 dist/session-cost-usage-5CCZbVhO.js
|
|
1
2
|
import { t as __exportAll } from "./rolldown-runtime-DUslC3ob.js";
|
|
2
3
|
import { n as resolveSessionFilePath, s as resolveSessionTranscriptsDirForAgent } from "./paths-BCX3TKIK.js";
|
|
3
4
|
import { i as stripInboundMetadata, n as stripMessageIdHints, o as countToolResults, s as extractToolCallNames, t as stripEnvelope } from "./chat-envelope-B0leI413.js";
|
|
@@ -38,21 +39,7 @@ function hasNonzeroUsage(usage) {
|
|
|
38
39
|
].some((v) => typeof v === "number" && Number.isFinite(v) && v > 0);
|
|
39
40
|
}
|
|
40
41
|
function normalizeUsage(raw) {
|
|
41
|
-
|
|
42
|
-
const rawInput = asFiniteNumber(raw.input ?? raw.inputTokens ?? raw.input_tokens ?? raw.promptTokens ?? raw.prompt_tokens);
|
|
43
|
-
const input = rawInput !== void 0 && rawInput < 0 ? 0 : rawInput;
|
|
44
|
-
const output = asFiniteNumber(raw.output ?? raw.outputTokens ?? raw.output_tokens ?? raw.completionTokens ?? raw.completion_tokens);
|
|
45
|
-
const cacheRead = asFiniteNumber(raw.cacheRead ?? raw.cache_read ?? raw.cache_read_input_tokens ?? raw.cached_tokens ?? raw.prompt_tokens_details?.cached_tokens);
|
|
46
|
-
const cacheWrite = asFiniteNumber(raw.cacheWrite ?? raw.cache_write ?? raw.cache_creation_input_tokens);
|
|
47
|
-
const total = asFiniteNumber(raw.total ?? raw.totalTokens ?? raw.total_tokens);
|
|
48
|
-
if (input === void 0 && output === void 0 && cacheRead === void 0 && cacheWrite === void 0 && total === void 0) return;
|
|
49
|
-
return {
|
|
50
|
-
input,
|
|
51
|
-
output,
|
|
52
|
-
cacheRead,
|
|
53
|
-
cacheWrite,
|
|
54
|
-
total
|
|
55
|
-
};
|
|
42
|
+
return normalizeBillingUsage(raw);
|
|
56
43
|
}
|
|
57
44
|
function derivePromptTokens(usage) {
|
|
58
45
|
if (!usage) return;
|
|
@@ -226,10 +213,8 @@ async function scanTranscriptFile(params) {
|
|
|
226
213
|
model: entry.model,
|
|
227
214
|
config: params.config
|
|
228
215
|
});
|
|
229
|
-
entry.
|
|
230
|
-
|
|
231
|
-
cost
|
|
232
|
-
});
|
|
216
|
+
entry.costBreakdown = billingCost(entry.usage, cost);
|
|
217
|
+
entry.costTotal = entry.costBreakdown?.total;
|
|
233
218
|
}
|
|
234
219
|
params.onEntry(entry);
|
|
235
220
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { normalizeBillingUsage, readLatestBillingUsage, formatBillingCost, billingCost } from "./usage-billing.js"; // JOHNNESS_PATCH_USAGE_BILLING_91 dist/session-cost-usage-CkCo2kVx.js
|
|
1
2
|
import { t as __exportAll } from "./rolldown-runtime-DUslC3ob.js";
|
|
2
3
|
import { n as resolveSessionFilePath, s as resolveSessionTranscriptsDirForAgent } from "./paths-BWOXmNIW.js";
|
|
3
4
|
import { i as stripInboundMetadata, n as stripMessageIdHints, o as countToolResults, s as extractToolCallNames, t as stripEnvelope } from "./chat-envelope-BkySjpPY.js";
|
|
@@ -38,21 +39,7 @@ function hasNonzeroUsage(usage) {
|
|
|
38
39
|
].some((v) => typeof v === "number" && Number.isFinite(v) && v > 0);
|
|
39
40
|
}
|
|
40
41
|
function normalizeUsage(raw) {
|
|
41
|
-
|
|
42
|
-
const rawInput = asFiniteNumber(raw.input ?? raw.inputTokens ?? raw.input_tokens ?? raw.promptTokens ?? raw.prompt_tokens);
|
|
43
|
-
const input = rawInput !== void 0 && rawInput < 0 ? 0 : rawInput;
|
|
44
|
-
const output = asFiniteNumber(raw.output ?? raw.outputTokens ?? raw.output_tokens ?? raw.completionTokens ?? raw.completion_tokens);
|
|
45
|
-
const cacheRead = asFiniteNumber(raw.cacheRead ?? raw.cache_read ?? raw.cache_read_input_tokens ?? raw.cached_tokens ?? raw.prompt_tokens_details?.cached_tokens);
|
|
46
|
-
const cacheWrite = asFiniteNumber(raw.cacheWrite ?? raw.cache_write ?? raw.cache_creation_input_tokens);
|
|
47
|
-
const total = asFiniteNumber(raw.total ?? raw.totalTokens ?? raw.total_tokens);
|
|
48
|
-
if (input === void 0 && output === void 0 && cacheRead === void 0 && cacheWrite === void 0 && total === void 0) return;
|
|
49
|
-
return {
|
|
50
|
-
input,
|
|
51
|
-
output,
|
|
52
|
-
cacheRead,
|
|
53
|
-
cacheWrite,
|
|
54
|
-
total
|
|
55
|
-
};
|
|
42
|
+
return normalizeBillingUsage(raw);
|
|
56
43
|
}
|
|
57
44
|
function derivePromptTokens(usage) {
|
|
58
45
|
if (!usage) return;
|
|
@@ -226,10 +213,8 @@ async function scanTranscriptFile(params) {
|
|
|
226
213
|
model: entry.model,
|
|
227
214
|
config: params.config
|
|
228
215
|
});
|
|
229
|
-
entry.
|
|
230
|
-
|
|
231
|
-
cost
|
|
232
|
-
});
|
|
216
|
+
entry.costBreakdown = billingCost(entry.usage, cost);
|
|
217
|
+
entry.costTotal = entry.costBreakdown?.total;
|
|
233
218
|
}
|
|
234
219
|
params.onEntry(entry);
|
|
235
220
|
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// JOHNNESS_PATCH_USAGE_BILLING_91
|
|
2
|
+
// Status, transcript normalization and fallback estimates share the provider's disjoint buckets.
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import { johnnessUsageCost } from "@mariozechner/pi-ai";
|
|
5
|
+
|
|
6
|
+
const number = v => typeof v === "number" && Number.isFinite(v) ? Math.max(0, v) : undefined;
|
|
7
|
+
export function normalizeBillingUsage(raw) {
|
|
8
|
+
if (!raw || typeof raw !== "object") return;
|
|
9
|
+
const cacheRead = number(raw.cacheRead ?? raw.cache_read ?? raw.cache_read_input_tokens ?? raw.cached_tokens ?? raw.input_tokens_details?.cached_tokens ?? raw.prompt_tokens_details?.cached_tokens ?? raw.prompt_cache_hit_tokens ?? raw.cachedContentTokenCount);
|
|
10
|
+
const cacheWrite = number(raw.cacheWrite ?? raw.cache_write ?? raw.cache_creation_input_tokens);
|
|
11
|
+
let input = number(raw.input ?? raw.inputTokens ?? raw.input_tokens ?? raw.promptTokens ?? raw.prompt_tokens ?? raw.promptTokenCount);
|
|
12
|
+
// Native Anthropic input excludes both cache buckets; OpenAI/Google prompt totals include reads.
|
|
13
|
+
if (raw.input === undefined && raw.inputTokens === undefined && (raw.input_tokens_details || raw.prompt_tokens !== undefined || raw.promptTokenCount !== undefined)) input = Math.max(0, (input ?? 0) - (cacheRead ?? 0));
|
|
14
|
+
const output = number(raw.output ?? raw.outputTokens ?? raw.output_tokens ?? raw.completionTokens ?? raw.completion_tokens ?? (raw.candidatesTokenCount !== undefined ? raw.candidatesTokenCount + (raw.thoughtsTokenCount ?? 0) : undefined));
|
|
15
|
+
const total = number(raw.total ?? raw.totalTokens ?? raw.total_tokens ?? raw.totalTokenCount);
|
|
16
|
+
if ([input, output, cacheRead, cacheWrite, total].every(v => v === undefined)) return;
|
|
17
|
+
const usage = { input, output, cacheRead, cacheWrite, total };
|
|
18
|
+
const short = number(raw.cacheWrite5m ?? raw.cache_creation?.ephemeral_5m_input_tokens);
|
|
19
|
+
const long = number(raw.cacheWrite1h ?? raw.cache_creation?.ephemeral_1h_input_tokens);
|
|
20
|
+
if (short !== undefined) usage.cacheWrite5m = short;
|
|
21
|
+
if (long !== undefined) usage.cacheWrite1h = long;
|
|
22
|
+
if (recordedUsageCost(raw)) usage.cost = { ...raw.cost };
|
|
23
|
+
return usage;
|
|
24
|
+
}
|
|
25
|
+
export function recordedUsageCost(usage) {
|
|
26
|
+
const cost = usage?.cost;
|
|
27
|
+
if (!cost || ["input", "output", "cacheRead", "cacheWrite", "total"].some(k => typeof cost[k] !== "number" || !Number.isFinite(cost[k]) || cost[k] < 0)) return;
|
|
28
|
+
return cost;
|
|
29
|
+
}
|
|
30
|
+
export function billingCost(usage, rates) {
|
|
31
|
+
return recordedUsageCost(usage) ?? johnnessUsageCost(usage, rates);
|
|
32
|
+
}
|
|
33
|
+
export function formatBillingUsd(value) {
|
|
34
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return;
|
|
35
|
+
// Do not round a large cache charge to cents or a small input charge to zero.
|
|
36
|
+
return "$" + value.toFixed(8).replace(/(\.\d*?[1-9])0+$|\.0+$/, "$1").replace(/^0$/, "0.00");
|
|
37
|
+
}
|
|
38
|
+
export function formatBillingCost(usage, rates) {
|
|
39
|
+
const cost = billingCost(usage, rates);
|
|
40
|
+
if (!cost) return null;
|
|
41
|
+
const estimate = recordedUsageCost(usage) ? "" : "est. ";
|
|
42
|
+
const line = `šµ Cost: ${estimate}${formatBillingUsd(cost.total)} (input ${formatBillingUsd(cost.input)}, read ${formatBillingUsd(cost.cacheRead)}, write ${formatBillingUsd(cost.cacheWrite)}, output ${formatBillingUsd(cost.output)})`;
|
|
43
|
+
const turn = usage?.turn;
|
|
44
|
+
return turn?.calls > 1 ? `${line}\nšµ Turn total: ${turn.complete ? formatBillingUsd(turn.cost.total) : "unavailable (missing recorded cost)"} across ${turn.calls} API calls` : line;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Read backwards until a complete assistant usage record is found. The old 8 KiB tail
|
|
48
|
+
// missed large text/tool records and accidentally combined different calls' counters.
|
|
49
|
+
export function readLatestBillingUsage(logPath) {
|
|
50
|
+
let fd;
|
|
51
|
+
try {
|
|
52
|
+
fd = fs.openSync(logPath, "r");
|
|
53
|
+
let end = fs.fstatSync(fd).size;
|
|
54
|
+
let carry = Buffer.alloc(0);
|
|
55
|
+
let latest;
|
|
56
|
+
const turn = { calls: 0, complete: true, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } };
|
|
57
|
+
const finish = () => latest ? { ...latest, turn } : undefined;
|
|
58
|
+
while (end > 0) {
|
|
59
|
+
const start = Math.max(0, end - 65536);
|
|
60
|
+
const block = Buffer.alloc(end - start);
|
|
61
|
+
fs.readSync(fd, block, 0, block.length, start);
|
|
62
|
+
const bytes = Buffer.concat([block, carry]);
|
|
63
|
+
let lineEnd = bytes.length;
|
|
64
|
+
for (let i = bytes.length - 1; i >= (start === 0 ? -1 : 0); i--) {
|
|
65
|
+
if (i !== -1 && bytes[i] !== 10) continue;
|
|
66
|
+
const line = bytes.subarray(i + 1, lineEnd).toString("utf8");
|
|
67
|
+
lineEnd = i;
|
|
68
|
+
try {
|
|
69
|
+
const entry = JSON.parse(line);
|
|
70
|
+
const message = entry.message ?? entry;
|
|
71
|
+
if (message.role === "user" && latest) return finish();
|
|
72
|
+
if (message.role !== "assistant") continue;
|
|
73
|
+
const usage = normalizeBillingUsage(message.usage ?? entry.usage);
|
|
74
|
+
if (!usage || ![usage.input, usage.output, usage.cacheRead, usage.cacheWrite].some(v => v > 0)) continue;
|
|
75
|
+
const promptTokens = (usage.input ?? 0) + (usage.cacheRead ?? 0) + (usage.cacheWrite ?? 0);
|
|
76
|
+
latest ??= { ...usage, promptTokens, total: usage.total ?? promptTokens + (usage.output ?? 0), model: message.model, provider: message.provider };
|
|
77
|
+
turn.calls++;
|
|
78
|
+
const cost = recordedUsageCost(usage);
|
|
79
|
+
if (!cost) turn.complete = false;
|
|
80
|
+
else for (const key of Object.keys(turn.cost)) turn.cost[key] += cost[key];
|
|
81
|
+
} catch {}
|
|
82
|
+
}
|
|
83
|
+
carry = bytes.subarray(0, lineEnd);
|
|
84
|
+
end = start;
|
|
85
|
+
}
|
|
86
|
+
return finish();
|
|
87
|
+
} catch {} finally { if (fd !== undefined) fs.closeSync(fd); }
|
|
88
|
+
}
|