opencode-usage-insights 0.3.1 → 0.4.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 +66 -542
- package/dist/core.d.ts +15 -1
- package/dist/gui-experimental.cjs +3 -3
- package/dist/index.d.ts +0 -47
- package/dist/index.js +1715 -1195
- package/dist/index.js.map +23 -16
- package/dist/line.d.ts +1 -0
- package/dist/panel-data.d.ts +8 -1
- package/dist/recorder.d.ts +1 -0
- package/dist/rpc.d.ts +4 -4
- package/dist/rpc.js +5 -5
- package/dist/rpc.js.map +3 -3
- package/dist/settings-client.d.ts +29 -0
- package/dist/settings-rpc.d.ts +746 -0
- package/dist/settings.d.ts +114 -0
- package/dist/sidebar.d.ts +3 -3
- package/dist/stats/aggregates.d.ts +15 -3
- package/dist/stats/constants.d.ts +7 -0
- package/dist/stats/db.d.ts +8 -128
- package/dist/stats/legacy.d.ts +25 -0
- package/dist/stats/maintenance.d.ts +1 -1
- package/dist/stats/migrate.d.ts +7 -0
- package/dist/stats/ndjson.d.ts +16 -0
- package/dist/stats/open.d.ts +15 -0
- package/dist/stats/paths.d.ts +5 -4
- package/dist/stats/queries.d.ts +25 -31
- package/dist/stats/records.d.ts +94 -0
- package/dist/stats/schema.d.ts +26 -0
- package/dist/stats-client.d.ts +9 -0
- package/dist/stats-worker.js +577 -352
- package/dist/stats-worker.js.map +17 -12
- package/dist/text.d.ts +2 -0
- package/dist/tui-settings.d.ts +18 -0
- package/dist/tui.d.ts +1 -57
- package/dist/tui.js +954 -144
- package/dist/tui.js.map +17 -13
- package/docs/cli.md +24 -0
- package/docs/configuration.md +108 -0
- package/docs/costs.md +44 -0
- package/docs/demo.md +94 -68
- package/docs/desktop.md +91 -0
- package/docs/images/desktop-overview-annotated.png +0 -0
- package/docs/images/desktop-overview.png +0 -0
- package/docs/images/desktop-settings.png +0 -0
- package/docs/images/desktop-turns-go-annotated.png +0 -0
- package/docs/images/desktop-turns.png +0 -0
- package/docs/images/line.png +0 -0
- package/docs/images/panel-cache.png +0 -0
- package/docs/images/panel-help.png +0 -0
- package/docs/images/panel-latency.png +0 -0
- package/docs/images/panel-overview.png +0 -0
- package/docs/images/panel-providers.png +0 -0
- package/docs/images/sidebar.png +0 -0
- package/docs/internals.md +87 -36
- package/docs/privacy.md +60 -0
- package/docs/tui.md +175 -0
- package/package.json +2 -1
package/dist/tui.js
CHANGED
|
@@ -351,23 +351,9 @@ function formatLine(stats, options) {
|
|
|
351
351
|
return formatLineSegments(stats, options).map((segment) => segment.text).join(" \xB7 ");
|
|
352
352
|
}
|
|
353
353
|
|
|
354
|
-
// src/limits.ts
|
|
355
|
-
var MAX_PANEL_WIDTH = 200;
|
|
356
|
-
var MAX_HISTORY_WIDTH = 120;
|
|
357
|
-
var MAX_TURNS = 50;
|
|
358
|
-
function clampPanelWidth(value) {
|
|
359
|
-
if (!Number.isFinite(value))
|
|
360
|
-
return 20;
|
|
361
|
-
return Math.min(MAX_PANEL_WIDTH, Math.max(20, Math.round(value)));
|
|
362
|
-
}
|
|
363
|
-
function clampTurnsLimit(value) {
|
|
364
|
-
if (!Number.isFinite(value))
|
|
365
|
-
return 1;
|
|
366
|
-
return Math.min(MAX_TURNS, Math.max(1, Math.round(value)));
|
|
367
|
-
}
|
|
368
|
-
|
|
369
354
|
// src/text.ts
|
|
370
355
|
var TEXT_LIMIT = 512;
|
|
356
|
+
var LABEL_LIMIT = 4 * TEXT_LIMIT;
|
|
371
357
|
function sanitizeText(value, limit = TEXT_LIMIT) {
|
|
372
358
|
if (limit <= 0)
|
|
373
359
|
return "";
|
|
@@ -497,7 +483,6 @@ function lineText(parts) {
|
|
|
497
483
|
}
|
|
498
484
|
|
|
499
485
|
// src/stats/queries.ts
|
|
500
|
-
var COMPACTION_KIND = "compaction";
|
|
501
486
|
var DEFAULT_TTL_HINT_MS = 10 * 60 * 1000;
|
|
502
487
|
function spend(usage) {
|
|
503
488
|
return usage.cost + usage.listCost;
|
|
@@ -567,9 +552,10 @@ function otherUsageRows(db, range, filters) {
|
|
|
567
552
|
}
|
|
568
553
|
}
|
|
569
554
|
var STEP_TIME = "COALESCE(created_at, recorded_at)";
|
|
570
|
-
var
|
|
555
|
+
var MODEL_KEY_SEPARATOR = "#";
|
|
556
|
+
var MODEL_KEY_SQL = `COALESCE(model_id, '(unknown)') || '${MODEL_KEY_SEPARATOR}' || COALESCE(variant, '')`;
|
|
571
557
|
function modelKey(modelID, variant) {
|
|
572
|
-
return `${modelID}
|
|
558
|
+
return `${modelID}${MODEL_KEY_SEPARATOR}${variant ?? ""}`;
|
|
573
559
|
}
|
|
574
560
|
function modelLabel(modelID, variant) {
|
|
575
561
|
return variant === undefined ? modelID : `${modelID} ${variant}`;
|
|
@@ -640,7 +626,10 @@ function overview(db, range, filters) {
|
|
|
640
626
|
MIN(${STEP_TIME}) AS firstAt,
|
|
641
627
|
MAX(COALESCE(completed_at, ${STEP_TIME})) AS lastAt,
|
|
642
628
|
COALESCE(SUM(CASE WHEN streamed_at > created_at THEN output_tokens + reasoning_tokens ELSE 0 END), 0) AS produced,
|
|
643
|
-
COALESCE(SUM(CASE WHEN streamed_at > created_at THEN streamed_at - created_at ELSE 0 END), 0) AS window
|
|
629
|
+
COALESCE(SUM(CASE WHEN streamed_at > created_at THEN streamed_at - created_at ELSE 0 END), 0) AS window,
|
|
630
|
+
COALESCE(SUM(CASE WHEN streamed_at > COALESCE(first_token_at, created_at) THEN output_tokens + reasoning_tokens ELSE 0 END), 0) AS decodeProduced,
|
|
631
|
+
COALESCE(SUM(CASE WHEN streamed_at > COALESCE(first_token_at, created_at)
|
|
632
|
+
THEN streamed_at - COALESCE(first_token_at, created_at) ELSE 0 END), 0) AS decodeWindow
|
|
644
633
|
FROM steps ${steps.where}`).get(...steps.params);
|
|
645
634
|
const prompts = rangeWhere(range, "created_at");
|
|
646
635
|
const promptClauses = prompts.where === "" ? [] : [prompts.where.slice("WHERE ".length)];
|
|
@@ -651,6 +640,10 @@ function overview(db, range, filters) {
|
|
|
651
640
|
}
|
|
652
641
|
const promptWhere = promptClauses.length > 0 ? `WHERE ${promptClauses.join(" AND ")}` : "";
|
|
653
642
|
const promptRow = db.query(`SELECT COUNT(*) AS prompts FROM prompts ${promptWhere}`).get(...promptParams);
|
|
643
|
+
const ttftFilter = steps.where === "" ? "WHERE" : `${steps.where} AND`;
|
|
644
|
+
const ttftRows = db.query(`SELECT MAX(0, first_token_at - created_at) AS ttft
|
|
645
|
+
FROM steps ${ttftFilter} created_at IS NOT NULL AND first_token_at IS NOT NULL`).all(...steps.params);
|
|
646
|
+
const ttft = ttftRows.length === 0 ? undefined : percentile(ttftRows.map((entry) => entry.ttft), 50);
|
|
654
647
|
const usage = usageFrom(row ?? { steps: 0, input: 0, cached: 0, cacheWrite: 0, output: 0, reasoning: 0, cost: 0, listCost: 0 });
|
|
655
648
|
const other = otherUsage(db, range, filters);
|
|
656
649
|
return {
|
|
@@ -662,7 +655,9 @@ function overview(db, range, filters) {
|
|
|
662
655
|
firstAt: row?.firstAt ?? undefined,
|
|
663
656
|
lastAt: row?.lastAt ?? undefined,
|
|
664
657
|
cacheHitRatio: cacheHitRatio(usage),
|
|
665
|
-
tps: row === null ? undefined : tokensPerSecond(row.
|
|
658
|
+
tps: row === null ? undefined : tokensPerSecond(row.decodeProduced ?? 0, 0, row.decodeWindow ?? 0),
|
|
659
|
+
requestTps: row === null ? undefined : tokensPerSecond(row.produced ?? 0, 0, row.window ?? 0),
|
|
660
|
+
ttft
|
|
666
661
|
};
|
|
667
662
|
}
|
|
668
663
|
function models(db, range, filters) {
|
|
@@ -817,7 +812,11 @@ function stepMessage(row) {
|
|
|
817
812
|
return {
|
|
818
813
|
type: "assistant",
|
|
819
814
|
id: row.message_id,
|
|
820
|
-
time: {
|
|
815
|
+
time: {
|
|
816
|
+
created: row.created_at ?? row.recorded_at,
|
|
817
|
+
firstToken: row.first_token_at ?? undefined,
|
|
818
|
+
completed: row.completed_at ?? undefined
|
|
819
|
+
},
|
|
821
820
|
model: row.provider_id !== null && row.model_id !== null ? { providerID: row.provider_id, id: row.model_id, variant: row.variant ?? undefined } : undefined,
|
|
822
821
|
tokens: {
|
|
823
822
|
input: row.input_tokens ?? 0,
|
|
@@ -848,7 +847,7 @@ function turns(db, options) {
|
|
|
848
847
|
}
|
|
849
848
|
const steps = db.query(`SELECT message_id, session_id, provider_id, model_id, variant,
|
|
850
849
|
input_tokens, cache_read, cache_write, output_tokens, reasoning_tokens, cost, list_cost,
|
|
851
|
-
created_at, streamed_at, recorded_at, completed_at, finish
|
|
850
|
+
created_at, first_token_at, streamed_at, recorded_at, completed_at, finish
|
|
852
851
|
FROM steps ${clauses.length > 0 ? `WHERE ${clauses.join(" AND ")}` : ""}
|
|
853
852
|
ORDER BY session_id, ${STEP_TIME}, message_id`).all(...params);
|
|
854
853
|
const promptClause = options?.sessionID === undefined ? "" : `WHERE session_id = ?${floor === undefined ? "" : " AND created_at >= ?"}`;
|
|
@@ -891,12 +890,22 @@ function turns(db, options) {
|
|
|
891
890
|
const variant = last.variant ?? undefined;
|
|
892
891
|
let produced = 0;
|
|
893
892
|
let window = 0;
|
|
893
|
+
let decodeProduced = 0;
|
|
894
|
+
let decodeWindow = 0;
|
|
894
895
|
for (const row of bucket) {
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
896
|
+
const tokens = (row.output_tokens ?? 0) + (row.reasoning_tokens ?? 0);
|
|
897
|
+
if (row.created_at !== null && row.streamed_at !== null && row.streamed_at > row.created_at) {
|
|
898
|
+
produced += tokens;
|
|
899
|
+
window += row.streamed_at - row.created_at;
|
|
900
|
+
}
|
|
901
|
+
const start = row.first_token_at ?? row.created_at;
|
|
902
|
+
if (start !== null && row.streamed_at !== null && row.streamed_at > start) {
|
|
903
|
+
decodeProduced += tokens;
|
|
904
|
+
decodeWindow += row.streamed_at - start;
|
|
905
|
+
}
|
|
899
906
|
}
|
|
907
|
+
const first = bucket[0];
|
|
908
|
+
const ttft = first !== undefined && first.created_at !== null && first.first_token_at !== null ? Math.max(0, first.first_token_at - first.created_at) : undefined;
|
|
900
909
|
const previous = result.length > 0 && result[result.length - 1].sessionID === last.session_id ? result[result.length - 1] : undefined;
|
|
901
910
|
const endAt = last.completed_at ?? last.created_at ?? last.recorded_at;
|
|
902
911
|
const firstAt = bucket[0]?.created_at ?? promptAt;
|
|
@@ -916,7 +925,9 @@ function turns(db, options) {
|
|
|
916
925
|
totals,
|
|
917
926
|
drops,
|
|
918
927
|
finish: last.finish ?? undefined,
|
|
919
|
-
tps: tokensPerSecond(
|
|
928
|
+
tps: tokensPerSecond(decodeProduced, 0, decodeWindow),
|
|
929
|
+
requestTps: tokensPerSecond(produced, 0, window),
|
|
930
|
+
ttft
|
|
920
931
|
});
|
|
921
932
|
bucket = [];
|
|
922
933
|
}
|
|
@@ -1093,8 +1104,8 @@ var RANGE_KEYS = ["session", "today", "7d", "30d", "all"];
|
|
|
1093
1104
|
var GRANULARITIES = ["day", "week", "month"];
|
|
1094
1105
|
var SORT_KEYS = {
|
|
1095
1106
|
models: ["steps", "cached", "hit", "out", "cost"],
|
|
1096
|
-
providers: ["steps", "cached", "hit", "cost", "share", "tps"],
|
|
1097
|
-
latency: ["n", "tps", "wall"],
|
|
1107
|
+
providers: ["steps", "cached", "hit", "cost", "share", "tps", "ttft"],
|
|
1108
|
+
latency: ["n", "tps", "ttft", "wall"],
|
|
1098
1109
|
sessions: ["last", "steps", "cached", "cost"],
|
|
1099
1110
|
turns: ["time", "cost", "tokens"]
|
|
1100
1111
|
};
|
|
@@ -1192,10 +1203,10 @@ function formatTps(value) {
|
|
|
1192
1203
|
return "\u2013";
|
|
1193
1204
|
return `${value.toFixed(1)}/s`;
|
|
1194
1205
|
}
|
|
1195
|
-
function
|
|
1206
|
+
function formatRateValue(value) {
|
|
1196
1207
|
if (value === undefined || !Number.isFinite(value) || value < 0)
|
|
1197
1208
|
return "\u2013";
|
|
1198
|
-
return value >= 10 ?
|
|
1209
|
+
return value >= 10 ? String(Math.round(value)) : value.toFixed(1);
|
|
1199
1210
|
}
|
|
1200
1211
|
function formatGap(value) {
|
|
1201
1212
|
if (value === undefined || !Number.isFinite(value))
|
|
@@ -1426,7 +1437,11 @@ function helpLines(width) {
|
|
|
1426
1437
|
{ text: " \u2248 list-price estimate for steps the host reported as $0;" },
|
|
1427
1438
|
{ text: " `$0.12 + \u2248 $3.40` keeps charged and estimated money apart" },
|
|
1428
1439
|
{ text: " wall request \u2192 completed step \xB7 stream request \u2192 last chunk" },
|
|
1429
|
-
{ text: "
|
|
1440
|
+
{ text: " ttft request \u2192 first streamed token" },
|
|
1441
|
+
{ text: " tps output+reasoning / first token \u2192 last chunk (OpenCode)" },
|
|
1442
|
+
{ text: " rtps output+reasoning / request \u2192 last chunk (includes TTFT)" },
|
|
1443
|
+
{ text: " turn 200 tps \xB7 2.1s ttft \xB7 cost" },
|
|
1444
|
+
{ text: " session 200 tps \xB7 167 rtps \xB7 ttft p50 \xB7 cost" },
|
|
1430
1445
|
{ text: " switches how often a step's model or provider differs from the step" },
|
|
1431
1446
|
{ text: " before it in the same session (the models view's detail), with" },
|
|
1432
1447
|
{ text: " the cache a provider switch cost" },
|
|
@@ -1555,12 +1570,16 @@ function usagePairs(usage, estimates = true) {
|
|
|
1555
1570
|
];
|
|
1556
1571
|
if (buckets.reasoning > 0)
|
|
1557
1572
|
pairs.push(["reasoning", formatTokens(buckets.reasoning)]);
|
|
1573
|
+
if ("tps" in usage && usage.tps !== undefined)
|
|
1574
|
+
pairs.push(["tps", formatTps(usage.tps)]);
|
|
1575
|
+
if ("requestTps" in usage && usage.requestTps !== undefined)
|
|
1576
|
+
pairs.push(["rtps", formatTps(usage.requestTps)]);
|
|
1577
|
+
if ("ttft" in usage && usage.ttft !== undefined)
|
|
1578
|
+
pairs.push(["ttft p50", formatMs(usage.ttft)]);
|
|
1558
1579
|
pairs.push(["cost", (estimates ? formatMoney(buckets.cost, buckets.listCost) : undefined) ?? formatCost(buckets.cost)]);
|
|
1559
1580
|
const other = usage.otherCost ?? 0;
|
|
1560
1581
|
if (other > 0)
|
|
1561
1582
|
pairs.push(["other", `${formatCost(other)} comp/title`]);
|
|
1562
|
-
if ("tps" in usage && usage.tps !== undefined)
|
|
1563
|
-
pairs.push(["tps", formatTps(usage.tps)]);
|
|
1564
1583
|
return pairs;
|
|
1565
1584
|
}
|
|
1566
1585
|
function overviewLines(data, width) {
|
|
@@ -1613,6 +1632,7 @@ function coveragePairs(coverage) {
|
|
|
1613
1632
|
["models", `${formatCount(coverage.models)} \xB7 unknown ${share(coverage.missingModel)}`],
|
|
1614
1633
|
["providers", formatCount(coverage.providers)],
|
|
1615
1634
|
["unsettled", `${formatCount(coverage.unsettled)} \xB7 no tps ${share(coverage.missingTps)}`],
|
|
1635
|
+
["no ttft", share(coverage.missingFirstToken)],
|
|
1616
1636
|
[
|
|
1617
1637
|
"window",
|
|
1618
1638
|
`${coverage.firstAt === undefined ? "\u2013" : new Date(coverage.firstAt).toLocaleString("en-GB")} \u2192 ${coverage.lastAt === undefined ? "\u2013" : new Date(coverage.lastAt).toLocaleString("en-GB")}`
|
|
@@ -1723,7 +1743,8 @@ function providerSummaryLines(rows, width, limit = 40, estimates = true) {
|
|
|
1723
1743
|
{ header: "cached", width: 7, max: 10, align: "right", cell: (row) => formatTokens(row.cache.cached) },
|
|
1724
1744
|
{ header: "hit", width: 6, max: 7, align: "right", priority: 2, cell: (row) => formatPercent(row.cache.hit) },
|
|
1725
1745
|
{ header: "tps p50", width: 8, align: "right", priority: 5, cell: (row) => formatTps(row.tps) },
|
|
1726
|
-
{ header: "
|
|
1746
|
+
{ header: "ttft p50", width: 8, max: 9, align: "right", priority: 6, cell: (row) => formatMs(row.ttft) },
|
|
1747
|
+
{ header: "wall", width: 7, align: "right", priority: 7, cell: (row) => formatMs(row.wall) },
|
|
1727
1748
|
{ header: "cost", width: 8, max: 9, align: "right", priority: 1, cell: (row) => formatSpend(row.usage.cost, estimates ? row.usage.listCost : 0) }
|
|
1728
1749
|
], width, {
|
|
1729
1750
|
limit,
|
|
@@ -1743,7 +1764,8 @@ function providerLines(rows, width, limit = 40, estimates = true) {
|
|
|
1743
1764
|
{ header: "cached", width: 7, max: 10, align: "right", priority: 3, cell: (row) => formatTokens(row.cache.cached) },
|
|
1744
1765
|
{ header: "hit", width: 6, max: 7, align: "right", priority: 4, cell: (row) => formatPercent(row.cache.hit) },
|
|
1745
1766
|
{ header: "tps p50", width: 8, align: "right", priority: 6, cell: (row) => formatTps(row.tps) },
|
|
1746
|
-
{ header: "
|
|
1767
|
+
{ header: "ttft p50", width: 8, max: 9, align: "right", priority: 7, cell: (row) => formatMs(row.ttft) },
|
|
1768
|
+
{ header: "wall", width: 7, align: "right", priority: 8, cell: (row) => formatMs(row.wall) },
|
|
1747
1769
|
{ header: "cost", width: 8, max: 9, align: "right", priority: 1, cell: (row) => formatSpend(row.usage.cost, estimates ? row.usage.listCost : 0) }
|
|
1748
1770
|
], width, {
|
|
1749
1771
|
limit,
|
|
@@ -1981,7 +2003,15 @@ function latencyLines(report, width, groups, limit = 12, estimates = true) {
|
|
|
1981
2003
|
tone: "base"
|
|
1982
2004
|
});
|
|
1983
2005
|
lines.push({
|
|
1984
|
-
text: formatTableRow(["
|
|
2006
|
+
text: formatTableRow(["ttft", "p50", formatMs(report.ttft.p50), "p90", formatMs(report.ttft.p90), "p99", formatMs(report.ttft.p99)], [10, 4, 9, 4, 9, 4, 9]),
|
|
2007
|
+
tone: "base"
|
|
2008
|
+
});
|
|
2009
|
+
lines.push({
|
|
2010
|
+
text: formatTableRow(["tps", "p50", formatTps(report.tps.p50), "p90", formatTps(report.tps.p90), "p99", formatTps(report.tps.p99)], [10, 4, 9, 4, 9, 4, 9]),
|
|
2011
|
+
tone: "base"
|
|
2012
|
+
});
|
|
2013
|
+
lines.push({
|
|
2014
|
+
text: formatTableRow(["rtps", "p50", formatTps(report.requestTps.p50), "p90", formatTps(report.requestTps.p90), "p99", formatTps(report.requestTps.p99)], [10, 4, 9, 4, 9, 4, 9]),
|
|
1985
2015
|
tone: "base"
|
|
1986
2016
|
});
|
|
1987
2017
|
} else {
|
|
@@ -1994,13 +2024,25 @@ function latencyLines(report, width, groups, limit = 12, estimates = true) {
|
|
|
1994
2024
|
tone: "base"
|
|
1995
2025
|
});
|
|
1996
2026
|
lines.push({
|
|
1997
|
-
text: latencyMetric("
|
|
2027
|
+
text: latencyMetric("ttft", [`p50 ${formatMs(report.ttft.p50)}`, `p90 ${formatMs(report.ttft.p90)}`, `p99 ${formatMs(report.ttft.p99)}`], width),
|
|
2028
|
+
tone: "base"
|
|
2029
|
+
});
|
|
2030
|
+
lines.push({
|
|
2031
|
+
text: latencyMetric("tps", [`p50 ${formatTps(report.tps.p50)}`, `p90 ${formatTps(report.tps.p90)}`, `p99 ${formatTps(report.tps.p99)}`], width),
|
|
2032
|
+
tone: "base"
|
|
2033
|
+
});
|
|
2034
|
+
lines.push({
|
|
2035
|
+
text: latencyMetric("rtps", [`p50 ${formatTps(report.requestTps.p50)}`, `p90 ${formatTps(report.requestTps.p90)}`, `p99 ${formatTps(report.requestTps.p99)}`], width),
|
|
1998
2036
|
tone: "base"
|
|
1999
2037
|
});
|
|
2000
2038
|
}
|
|
2001
2039
|
lines.push({ text: "", tone: "muted" });
|
|
2002
2040
|
lines.push({
|
|
2003
|
-
text: truncate("wall = request to completed \xB7 stream = request to last chunk
|
|
2041
|
+
text: truncate("wall = request to completed \xB7 stream = request to last chunk", width),
|
|
2042
|
+
tone: "muted"
|
|
2043
|
+
});
|
|
2044
|
+
lines.push({
|
|
2045
|
+
text: truncate("ttft = request to first token \xB7 tps = first token to last chunk \xB7 rtps = request to last chunk", width),
|
|
2004
2046
|
tone: "muted"
|
|
2005
2047
|
});
|
|
2006
2048
|
if (report.wall.count === 0)
|
|
@@ -2015,7 +2057,8 @@ function latencyLines(report, width, groups, limit = 12, estimates = true) {
|
|
|
2015
2057
|
{ header: "wall p90", width: 8, max: 9, align: "right", priority: 1, cell: (row) => formatMs(row.wall.p90) },
|
|
2016
2058
|
{ header: "stream", width: 6, max: 9, align: "right", priority: 2, cell: (row) => formatMs(row.stream.p50) },
|
|
2017
2059
|
{ header: "tps p50", width: 8, max: 9, align: "right", cell: (row) => formatTps(row.tps.p50) },
|
|
2018
|
-
{ header: "tps p90", width: 8, max: 9, align: "right", priority:
|
|
2060
|
+
{ header: "tps p90", width: 8, max: 9, align: "right", priority: 4, cell: (row) => formatTps(row.tps.p90) },
|
|
2061
|
+
{ header: "ttft p50", width: 8, max: 9, align: "right", priority: 3, cell: (row) => formatMs(row.ttft.p50) },
|
|
2019
2062
|
{ header: "cost", width: 8, max: 9, align: "right", cell: (row) => formatSpend(row.usage.cost, estimates ? row.usage.listCost : 0) }
|
|
2020
2063
|
], width);
|
|
2021
2064
|
if (columns.length > 0) {
|
|
@@ -2244,7 +2287,9 @@ function sessionSummaryLines(usage, budget = 34, estimates = true) {
|
|
|
2244
2287
|
const cached = formatTokens(buckets.cached);
|
|
2245
2288
|
const out = formatTokens(buckets.output);
|
|
2246
2289
|
const hit = formatPercent(buckets.hit);
|
|
2247
|
-
const tps =
|
|
2290
|
+
const tps = usage.tps === undefined ? undefined : `${formatRateValue(usage.tps)} tps`;
|
|
2291
|
+
const rtps = usage.requestTps === undefined ? undefined : `${formatRateValue(usage.requestTps)} rtps`;
|
|
2292
|
+
const ttft = usage.ttft === undefined ? undefined : `ttft p50 ${formatMs(usage.ttft)}`;
|
|
2248
2293
|
const cost = (estimates ? formatMoney(buckets.cost, buckets.listCost) : undefined) ?? formatCost(buckets.cost);
|
|
2249
2294
|
const first = [
|
|
2250
2295
|
` ${steps} steps \xB7 in ${input}`,
|
|
@@ -2254,16 +2299,22 @@ function sessionSummaryLines(usage, budget = 34, estimates = true) {
|
|
|
2254
2299
|
` cached ${cached} \xB7 out ${out}`,
|
|
2255
2300
|
` cached ${cached}`
|
|
2256
2301
|
].find((text) => text.length <= budget);
|
|
2257
|
-
const
|
|
2258
|
-
|
|
2259
|
-
` hit ${hit} \xB7 ${
|
|
2260
|
-
` ${
|
|
2302
|
+
const rates = [tps, rtps].filter((part) => part !== undefined);
|
|
2303
|
+
const rateLine = [
|
|
2304
|
+
` hit ${hit}${rates.length > 0 ? ` \xB7 ${rates.join(" \xB7 ")}` : ""}`,
|
|
2305
|
+
` hit ${hit}${rates[0] !== undefined ? ` \xB7 ${rates[0]}` : ""}`,
|
|
2306
|
+
` hit ${hit}`
|
|
2261
2307
|
].find((text) => text.length <= budget);
|
|
2308
|
+
const tail = [
|
|
2309
|
+
ttft === undefined ? undefined : ` ${ttft} \xB7 ${cost}`,
|
|
2310
|
+
` ${cost}`
|
|
2311
|
+
].filter((text) => text !== undefined).find((text) => text.length <= budget);
|
|
2262
2312
|
return [
|
|
2263
2313
|
{ text: truncate("session", budget), tone: HEADER_TONE },
|
|
2264
2314
|
{ text: truncate(first ?? ` ${steps} steps`, budget), tone: "muted" },
|
|
2265
2315
|
{ text: truncate(second ?? ` cached ${cached}`, budget), tone: "base" },
|
|
2266
|
-
{ text: truncate(
|
|
2316
|
+
{ text: truncate(rateLine ?? ` hit ${hit}`, budget), tone: "base" },
|
|
2317
|
+
{ text: truncate(tail ?? ` ${cost}`, budget), tone: "base" },
|
|
2267
2318
|
{ text: "", tone: "muted" }
|
|
2268
2319
|
];
|
|
2269
2320
|
}
|
|
@@ -2280,20 +2331,28 @@ function sidebarLines(rows, options) {
|
|
|
2280
2331
|
const output = formatTokens(display.output);
|
|
2281
2332
|
const cost = options?.estimates === false ? formatCost(turn.totals.cost) : formatMoney(turn.totals.cost, turn.totals.listCost) ?? formatCost(turn.totals.cost);
|
|
2282
2333
|
const hit = formatPercent(display.prompt > 0 ? display.cached / display.prompt : undefined).replace(/\.0%$/, "%");
|
|
2283
|
-
const tps = turn.tps === undefined ? undefined :
|
|
2334
|
+
const tps = turn.tps === undefined ? undefined : `${formatRateValue(turn.tps)} tps`;
|
|
2335
|
+
const ttft = turn.ttft === undefined ? undefined : `${formatMs(turn.ttft)} ttft`;
|
|
2336
|
+
const rate = [tps, ttft].filter((part) => part !== undefined).join(" \xB7 ");
|
|
2284
2337
|
const suffix = turn.drops.length > 0 ? " \xB7 \u26A0" : "";
|
|
2285
2338
|
const header = `${clock} ${provider}`;
|
|
2286
2339
|
const timed = turn.durationMs === undefined ? undefined : `${header} \xB7 ${formatMs(turn.durationMs)}`;
|
|
2340
|
+
const join = (...parts) => parts.filter((part) => part !== undefined && part !== "").join(" \xB7 ");
|
|
2287
2341
|
const volumes = [
|
|
2288
|
-
` ${labels.input} ${input} (${prompt}) \xB7 ${labels.output} ${output}`,
|
|
2342
|
+
` ${labels.input} ${input} (${prompt}) \xB7 ${labels.output} ${output} \xB7 hit ${hit}`,
|
|
2343
|
+
` ${labels.input} ${input} \xB7 ${labels.output} ${output} \xB7 hit ${hit}`,
|
|
2289
2344
|
` ${labels.input} ${input} \xB7 ${labels.output} ${output}`,
|
|
2290
2345
|
` ${labels.input} ${input}`
|
|
2291
2346
|
];
|
|
2347
|
+
const volume = volumes.find((text) => text.length <= budget) ?? volumes[volumes.length - 1];
|
|
2348
|
+
const hitPart = volume.includes(`hit ${hit}`) ? undefined : `hit ${hit}`;
|
|
2292
2349
|
const facts = [
|
|
2293
|
-
`
|
|
2294
|
-
`
|
|
2295
|
-
` ${tps
|
|
2296
|
-
`
|
|
2350
|
+
` ${join(hitPart, rate, `${cost}${suffix}`)}`,
|
|
2351
|
+
` ${join(hitPart, tps, `${cost}${suffix}`)}`,
|
|
2352
|
+
` ${join(tps, `${cost}${suffix}`)}`,
|
|
2353
|
+
` ${join(tps)}`,
|
|
2354
|
+
` ${join(hitPart, `${cost}${suffix}`)}`,
|
|
2355
|
+
` ${join(`${cost}${suffix}`)}`
|
|
2297
2356
|
];
|
|
2298
2357
|
lines.push({ text: truncate(timed !== undefined && timed.length <= budget ? timed : header, budget), tone: turn.providerChanged ? "warning" : "base" });
|
|
2299
2358
|
const edge = index === 0 || index === rows.length - 1;
|
|
@@ -2301,10 +2360,7 @@ function sidebarLines(rows, options) {
|
|
|
2301
2360
|
const model = turn.modelID === undefined ? "unknown" : `${turn.modelID}${turn.variant === undefined ? "" : ` ${turn.variant}`}`;
|
|
2302
2361
|
lines.push({ text: truncate(` ${model}`, budget), tone: "muted" });
|
|
2303
2362
|
}
|
|
2304
|
-
lines.push({
|
|
2305
|
-
text: truncate(volumes.find((text) => text.length <= budget) ?? volumes[volumes.length - 1], budget),
|
|
2306
|
-
tone: "muted"
|
|
2307
|
-
});
|
|
2363
|
+
lines.push({ text: truncate(volume, budget), tone: "muted" });
|
|
2308
2364
|
lines.push({
|
|
2309
2365
|
text: truncate(facts.find((text) => text.length <= budget) ?? facts[facts.length - 1], budget),
|
|
2310
2366
|
tone: turn.drops.length > 0 ? "warning" : "muted"
|
|
@@ -2327,6 +2383,21 @@ import { createElement as _$createElement } from "@opentui/solid";
|
|
|
2327
2383
|
import { TextAttributes } from "@opentui/core";
|
|
2328
2384
|
import { For, Show, createEffect, createMemo, createRenderEffect, createSignal } from "solid-js";
|
|
2329
2385
|
|
|
2386
|
+
// src/limits.ts
|
|
2387
|
+
var MAX_PANEL_WIDTH = 200;
|
|
2388
|
+
var MAX_HISTORY_WIDTH = 120;
|
|
2389
|
+
var MAX_TURNS = 50;
|
|
2390
|
+
function clampPanelWidth(value) {
|
|
2391
|
+
if (!Number.isFinite(value))
|
|
2392
|
+
return 20;
|
|
2393
|
+
return Math.min(MAX_PANEL_WIDTH, Math.max(20, Math.round(value)));
|
|
2394
|
+
}
|
|
2395
|
+
function clampTurnsLimit(value) {
|
|
2396
|
+
if (!Number.isFinite(value))
|
|
2397
|
+
return 1;
|
|
2398
|
+
return Math.min(MAX_TURNS, Math.max(1, Math.round(value)));
|
|
2399
|
+
}
|
|
2400
|
+
|
|
2330
2401
|
// src/rpc.ts
|
|
2331
2402
|
import { Rpc } from "@opencode/plugin/rpc";
|
|
2332
2403
|
var Tone = { type: "string", enum: ["base", "muted", "warning", "accent"] };
|
|
@@ -2410,10 +2481,10 @@ var PruneResultSchema = {
|
|
|
2410
2481
|
properties: {
|
|
2411
2482
|
steps: { type: "number" },
|
|
2412
2483
|
prompts: { type: "number" },
|
|
2413
|
-
|
|
2484
|
+
compactions: { type: "number" },
|
|
2414
2485
|
usage: { type: "number" }
|
|
2415
2486
|
},
|
|
2416
|
-
required: ["steps", "prompts", "
|
|
2487
|
+
required: ["steps", "prompts", "compactions", "usage"],
|
|
2417
2488
|
additionalProperties: false
|
|
2418
2489
|
};
|
|
2419
2490
|
var WipeResultSchema = {
|
|
@@ -2421,11 +2492,11 @@ var WipeResultSchema = {
|
|
|
2421
2492
|
properties: {
|
|
2422
2493
|
steps: { type: "number" },
|
|
2423
2494
|
prompts: { type: "number" },
|
|
2424
|
-
|
|
2495
|
+
compactions: { type: "number" },
|
|
2425
2496
|
usage: { type: "number" },
|
|
2426
2497
|
sessions: { type: "number" }
|
|
2427
2498
|
},
|
|
2428
|
-
required: ["steps", "prompts", "
|
|
2499
|
+
required: ["steps", "prompts", "compactions", "usage", "sessions"],
|
|
2429
2500
|
additionalProperties: false
|
|
2430
2501
|
};
|
|
2431
2502
|
var BackfillResultSchema = {
|
|
@@ -2825,7 +2896,7 @@ function Panel(props) {
|
|
|
2825
2896
|
confirm: true
|
|
2826
2897
|
});
|
|
2827
2898
|
props.ctx.ui.toast.show({
|
|
2828
|
-
message: `pruned ${result.steps} steps \xB7 ${result.prompts} prompts \xB7 ${result.
|
|
2899
|
+
message: `pruned ${result.steps} steps \xB7 ${result.prompts} prompts \xB7 ${result.compactions} compactions \xB7 ${result.usage} other usage`,
|
|
2829
2900
|
variant: "success"
|
|
2830
2901
|
});
|
|
2831
2902
|
} catch (cause) {
|
|
@@ -2871,7 +2942,7 @@ function Panel(props) {
|
|
|
2871
2942
|
confirm: true
|
|
2872
2943
|
});
|
|
2873
2944
|
props.ctx.ui.toast.show({
|
|
2874
|
-
message: `wiped ${result.steps} steps \xB7 ${result.prompts} prompts \xB7 ${result.
|
|
2945
|
+
message: `wiped ${result.steps} steps \xB7 ${result.prompts} prompts \xB7 ${result.compactions} compactions \xB7 ${result.usage} other usage \xB7 ${result.sessions} sessions`,
|
|
2875
2946
|
variant: "success"
|
|
2876
2947
|
});
|
|
2877
2948
|
} catch (cause) {
|
|
@@ -3320,6 +3391,383 @@ async function fetchGoQuota(client, location, signal) {
|
|
|
3320
3391
|
return normalizeQuota(data, Date.now());
|
|
3321
3392
|
}
|
|
3322
3393
|
|
|
3394
|
+
// src/settings-rpc.ts
|
|
3395
|
+
import { Rpc as Rpc2 } from "@opencode/plugin/rpc";
|
|
3396
|
+
|
|
3397
|
+
// src/settings.ts
|
|
3398
|
+
var SETTINGS_STORAGE_KEY = "settings";
|
|
3399
|
+
var MAX_LABEL_LENGTH = 80;
|
|
3400
|
+
var MAX_QUOTA_NAME_LENGTH = 120;
|
|
3401
|
+
var DEFAULT_SIDEBAR_TURNS = 8;
|
|
3402
|
+
var TURN_TOTALS = ["latest", "sum"];
|
|
3403
|
+
var FIELD_KEYS = Object.keys(DEFAULT_FIELDS);
|
|
3404
|
+
var LABEL_KEYS = Object.keys(DEFAULT_LABELS);
|
|
3405
|
+
var DEFAULT_SERVER_POLICY = {
|
|
3406
|
+
maintenance: true,
|
|
3407
|
+
exposePaths: false
|
|
3408
|
+
};
|
|
3409
|
+
var DEFAULT_USAGE_SETTINGS = {
|
|
3410
|
+
pricing: true,
|
|
3411
|
+
turnTotals: "latest",
|
|
3412
|
+
fields: { ...DEFAULT_FIELDS },
|
|
3413
|
+
labels: { ...DEFAULT_LABELS },
|
|
3414
|
+
panel: true,
|
|
3415
|
+
sidebar: true,
|
|
3416
|
+
sidebarTurns: DEFAULT_SIDEBAR_TURNS,
|
|
3417
|
+
quota: true,
|
|
3418
|
+
quotaName: ""
|
|
3419
|
+
};
|
|
3420
|
+
function defaultUsageSettings() {
|
|
3421
|
+
return {
|
|
3422
|
+
...DEFAULT_USAGE_SETTINGS,
|
|
3423
|
+
fields: { ...DEFAULT_USAGE_SETTINGS.fields },
|
|
3424
|
+
labels: { ...DEFAULT_USAGE_SETTINGS.labels }
|
|
3425
|
+
};
|
|
3426
|
+
}
|
|
3427
|
+
function defaultSettingsEnvelope(policy = DEFAULT_SERVER_POLICY) {
|
|
3428
|
+
return { settings: defaultUsageSettings(), policy: { ...policy } };
|
|
3429
|
+
}
|
|
3430
|
+
function isRecord(value) {
|
|
3431
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3432
|
+
}
|
|
3433
|
+
function sanitizeSidebarTurns(value) {
|
|
3434
|
+
if (typeof value === "number" && Number.isFinite(value))
|
|
3435
|
+
return clampTurnsLimit(value);
|
|
3436
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
3437
|
+
const parsed = Number(value);
|
|
3438
|
+
if (Number.isFinite(parsed))
|
|
3439
|
+
return clampTurnsLimit(parsed);
|
|
3440
|
+
}
|
|
3441
|
+
return DEFAULT_SIDEBAR_TURNS;
|
|
3442
|
+
}
|
|
3443
|
+
function sanitizeLabels(value) {
|
|
3444
|
+
const labels = { ...DEFAULT_LABELS };
|
|
3445
|
+
if (!isRecord(value))
|
|
3446
|
+
return labels;
|
|
3447
|
+
for (const key of LABEL_KEYS) {
|
|
3448
|
+
const entry = value[key];
|
|
3449
|
+
if (typeof entry === "string")
|
|
3450
|
+
labels[key] = sanitizeText(entry, MAX_LABEL_LENGTH);
|
|
3451
|
+
}
|
|
3452
|
+
return labels;
|
|
3453
|
+
}
|
|
3454
|
+
function sanitizeFields(value) {
|
|
3455
|
+
const fields = { ...DEFAULT_FIELDS };
|
|
3456
|
+
if (!isRecord(value))
|
|
3457
|
+
return fields;
|
|
3458
|
+
for (const key of FIELD_KEYS) {
|
|
3459
|
+
const entry = value[key];
|
|
3460
|
+
if (typeof entry === "boolean")
|
|
3461
|
+
fields[key] = entry;
|
|
3462
|
+
}
|
|
3463
|
+
return fields;
|
|
3464
|
+
}
|
|
3465
|
+
function sanitizeUsageSettings(value) {
|
|
3466
|
+
if (!isRecord(value))
|
|
3467
|
+
return defaultUsageSettings();
|
|
3468
|
+
return {
|
|
3469
|
+
pricing: typeof value.pricing === "boolean" ? value.pricing : DEFAULT_USAGE_SETTINGS.pricing,
|
|
3470
|
+
turnTotals: TURN_TOTALS.includes(value.turnTotals) ? value.turnTotals : DEFAULT_USAGE_SETTINGS.turnTotals,
|
|
3471
|
+
fields: sanitizeFields(value.fields),
|
|
3472
|
+
labels: sanitizeLabels(value.labels),
|
|
3473
|
+
panel: typeof value.panel === "boolean" ? value.panel : DEFAULT_USAGE_SETTINGS.panel,
|
|
3474
|
+
sidebar: typeof value.sidebar === "boolean" ? value.sidebar : DEFAULT_USAGE_SETTINGS.sidebar,
|
|
3475
|
+
sidebarTurns: sanitizeSidebarTurns(value.sidebarTurns),
|
|
3476
|
+
quota: typeof value.quota === "boolean" ? value.quota : DEFAULT_USAGE_SETTINGS.quota,
|
|
3477
|
+
quotaName: typeof value.quotaName === "string" ? sanitizeText(value.quotaName.trim(), MAX_QUOTA_NAME_LENGTH) : DEFAULT_USAGE_SETTINGS.quotaName
|
|
3478
|
+
};
|
|
3479
|
+
}
|
|
3480
|
+
function sanitizeServerPolicy(value) {
|
|
3481
|
+
if (!isRecord(value))
|
|
3482
|
+
return { ...DEFAULT_SERVER_POLICY };
|
|
3483
|
+
return {
|
|
3484
|
+
maintenance: typeof value.maintenance === "boolean" ? value.maintenance : DEFAULT_SERVER_POLICY.maintenance,
|
|
3485
|
+
exposePaths: typeof value.exposePaths === "boolean" ? value.exposePaths : DEFAULT_SERVER_POLICY.exposePaths
|
|
3486
|
+
};
|
|
3487
|
+
}
|
|
3488
|
+
function sanitizeSettingsEnvelope(value) {
|
|
3489
|
+
return {
|
|
3490
|
+
settings: sanitizeUsageSettings(isRecord(value) ? value.settings : undefined),
|
|
3491
|
+
policy: sanitizeServerPolicy(isRecord(value) ? value.policy : undefined)
|
|
3492
|
+
};
|
|
3493
|
+
}
|
|
3494
|
+
function mergeUsageSettings(current, patch) {
|
|
3495
|
+
const next = {
|
|
3496
|
+
...current,
|
|
3497
|
+
...typeof patch.pricing === "boolean" ? { pricing: patch.pricing } : {},
|
|
3498
|
+
...TURN_TOTALS.includes(patch.turnTotals) ? { turnTotals: patch.turnTotals } : {},
|
|
3499
|
+
...isRecord(patch.fields) ? { fields: { ...current.fields, ...patch.fields } } : {},
|
|
3500
|
+
...isRecord(patch.labels) ? { labels: { ...current.labels, ...patch.labels } } : {},
|
|
3501
|
+
...typeof patch.panel === "boolean" ? { panel: patch.panel } : {},
|
|
3502
|
+
...typeof patch.sidebar === "boolean" ? { sidebar: patch.sidebar } : {},
|
|
3503
|
+
...patch.sidebarTurns !== undefined ? { sidebarTurns: patch.sidebarTurns } : {},
|
|
3504
|
+
...typeof patch.quota === "boolean" ? { quota: patch.quota } : {},
|
|
3505
|
+
...typeof patch.quotaName === "string" ? { quotaName: patch.quotaName } : {}
|
|
3506
|
+
};
|
|
3507
|
+
return sanitizeUsageSettings(next);
|
|
3508
|
+
}
|
|
3509
|
+
function resolveUsageSettings(settings) {
|
|
3510
|
+
return {
|
|
3511
|
+
format: {
|
|
3512
|
+
labels: settings.labels,
|
|
3513
|
+
fields: settings.fields,
|
|
3514
|
+
turnTotals: settings.turnTotals,
|
|
3515
|
+
estimates: settings.pricing
|
|
3516
|
+
},
|
|
3517
|
+
sidebarTurns: clampTurnsLimit(settings.sidebarTurns),
|
|
3518
|
+
quotaName: settings.quotaName.trim() || undefined
|
|
3519
|
+
};
|
|
3520
|
+
}
|
|
3521
|
+
function isValidSidebarTurns(raw) {
|
|
3522
|
+
const value = Number(raw);
|
|
3523
|
+
return raw.trim() !== "" && Number.isInteger(value) && value >= 1 && value <= 50;
|
|
3524
|
+
}
|
|
3525
|
+
function parseSidebarTurns(raw) {
|
|
3526
|
+
if (!isValidSidebarTurns(raw))
|
|
3527
|
+
return;
|
|
3528
|
+
return clampTurnsLimit(Number(raw));
|
|
3529
|
+
}
|
|
3530
|
+
var TRUE_VALUES = new Set(["1", "true", "on", "yes", "y"]);
|
|
3531
|
+
var FALSE_VALUES = new Set(["0", "false", "off", "no", "n"]);
|
|
3532
|
+
function envFlag(value, fallback) {
|
|
3533
|
+
if (value === undefined || value.trim() === "")
|
|
3534
|
+
return fallback;
|
|
3535
|
+
const normalized = value.trim().toLowerCase();
|
|
3536
|
+
if (TRUE_VALUES.has(normalized))
|
|
3537
|
+
return true;
|
|
3538
|
+
if (FALSE_VALUES.has(normalized))
|
|
3539
|
+
return false;
|
|
3540
|
+
return fallback;
|
|
3541
|
+
}
|
|
3542
|
+
function serverPolicy(env = process.env) {
|
|
3543
|
+
return {
|
|
3544
|
+
maintenance: envFlag(env.OPENCODE_USAGE_INSIGHTS_MAINTENANCE, true),
|
|
3545
|
+
exposePaths: envFlag(env.OPENCODE_USAGE_INSIGHTS_EXPOSE_PATHS, false)
|
|
3546
|
+
};
|
|
3547
|
+
}
|
|
3548
|
+
|
|
3549
|
+
// src/settings-rpc.ts
|
|
3550
|
+
var LineFields = {
|
|
3551
|
+
type: "object",
|
|
3552
|
+
properties: {
|
|
3553
|
+
clock: { type: "boolean" },
|
|
3554
|
+
delta: { type: "boolean" },
|
|
3555
|
+
model: { type: "boolean" },
|
|
3556
|
+
variant: { type: "boolean" },
|
|
3557
|
+
turn: { type: "boolean" },
|
|
3558
|
+
session: { type: "boolean" },
|
|
3559
|
+
reasoning: { type: "boolean" },
|
|
3560
|
+
cost: { type: "boolean" },
|
|
3561
|
+
cacheExpiry: { type: "boolean" }
|
|
3562
|
+
},
|
|
3563
|
+
additionalProperties: false
|
|
3564
|
+
};
|
|
3565
|
+
var LineLabels = {
|
|
3566
|
+
type: "object",
|
|
3567
|
+
properties: {
|
|
3568
|
+
delta: { type: "string", maxLength: MAX_LABEL_LENGTH },
|
|
3569
|
+
turn: { type: "string", maxLength: MAX_LABEL_LENGTH },
|
|
3570
|
+
session: { type: "string", maxLength: MAX_LABEL_LENGTH },
|
|
3571
|
+
step: { type: "string", maxLength: MAX_LABEL_LENGTH },
|
|
3572
|
+
steps: { type: "string", maxLength: MAX_LABEL_LENGTH },
|
|
3573
|
+
input: { type: "string", maxLength: MAX_LABEL_LENGTH },
|
|
3574
|
+
cached: { type: "string", maxLength: MAX_LABEL_LENGTH },
|
|
3575
|
+
output: { type: "string", maxLength: MAX_LABEL_LENGTH },
|
|
3576
|
+
reasoning: { type: "string", maxLength: MAX_LABEL_LENGTH },
|
|
3577
|
+
cost: { type: "string", maxLength: MAX_LABEL_LENGTH },
|
|
3578
|
+
estimate: { type: "string", maxLength: MAX_LABEL_LENGTH },
|
|
3579
|
+
cacheExpired: { type: "string", maxLength: MAX_LABEL_LENGTH },
|
|
3580
|
+
providerChanged: { type: "string", maxLength: MAX_LABEL_LENGTH },
|
|
3581
|
+
modelChanged: { type: "string", maxLength: MAX_LABEL_LENGTH }
|
|
3582
|
+
},
|
|
3583
|
+
additionalProperties: false
|
|
3584
|
+
};
|
|
3585
|
+
var Settings = {
|
|
3586
|
+
type: "object",
|
|
3587
|
+
properties: {
|
|
3588
|
+
pricing: { type: "boolean" },
|
|
3589
|
+
turnTotals: { type: "string", enum: ["latest", "sum"] },
|
|
3590
|
+
fields: LineFields,
|
|
3591
|
+
labels: LineLabels,
|
|
3592
|
+
panel: { type: "boolean" },
|
|
3593
|
+
sidebar: { type: "boolean" },
|
|
3594
|
+
sidebarTurns: { type: "number", minimum: 1, maximum: 50 },
|
|
3595
|
+
quota: { type: "boolean" },
|
|
3596
|
+
quotaName: { type: "string", maxLength: MAX_QUOTA_NAME_LENGTH }
|
|
3597
|
+
},
|
|
3598
|
+
required: ["pricing", "turnTotals", "fields", "labels", "panel", "sidebar", "sidebarTurns", "quota", "quotaName"],
|
|
3599
|
+
additionalProperties: false
|
|
3600
|
+
};
|
|
3601
|
+
var Patch = {
|
|
3602
|
+
type: "object",
|
|
3603
|
+
properties: {
|
|
3604
|
+
pricing: { type: "boolean" },
|
|
3605
|
+
turnTotals: { type: "string", enum: ["latest", "sum"] },
|
|
3606
|
+
fields: LineFields,
|
|
3607
|
+
labels: LineLabels,
|
|
3608
|
+
panel: { type: "boolean" },
|
|
3609
|
+
sidebar: { type: "boolean" },
|
|
3610
|
+
sidebarTurns: { type: "number", minimum: 1, maximum: 50 },
|
|
3611
|
+
quota: { type: "boolean" },
|
|
3612
|
+
quotaName: { type: "string", maxLength: MAX_QUOTA_NAME_LENGTH }
|
|
3613
|
+
},
|
|
3614
|
+
additionalProperties: false
|
|
3615
|
+
};
|
|
3616
|
+
var Policy = {
|
|
3617
|
+
type: "object",
|
|
3618
|
+
properties: {
|
|
3619
|
+
maintenance: { type: "boolean" },
|
|
3620
|
+
exposePaths: { type: "boolean" }
|
|
3621
|
+
},
|
|
3622
|
+
required: ["maintenance", "exposePaths"],
|
|
3623
|
+
additionalProperties: false
|
|
3624
|
+
};
|
|
3625
|
+
var Envelope = {
|
|
3626
|
+
type: "object",
|
|
3627
|
+
properties: {
|
|
3628
|
+
settings: Settings,
|
|
3629
|
+
policy: Policy
|
|
3630
|
+
},
|
|
3631
|
+
required: ["settings", "policy"],
|
|
3632
|
+
additionalProperties: false
|
|
3633
|
+
};
|
|
3634
|
+
var Empty = { type: "object", properties: {}, additionalProperties: false };
|
|
3635
|
+
var SetInput = {
|
|
3636
|
+
type: "object",
|
|
3637
|
+
properties: { patch: Patch },
|
|
3638
|
+
required: ["patch"],
|
|
3639
|
+
additionalProperties: false
|
|
3640
|
+
};
|
|
3641
|
+
var UsageInsightsSettings = Rpc2.define({
|
|
3642
|
+
id: "usage-insights.settings",
|
|
3643
|
+
methods: {
|
|
3644
|
+
get: { input: Empty, output: Envelope },
|
|
3645
|
+
set: { input: SetInput, output: Envelope },
|
|
3646
|
+
reset: { input: Empty, output: Envelope }
|
|
3647
|
+
},
|
|
3648
|
+
events: {
|
|
3649
|
+
updated: { schema: Envelope }
|
|
3650
|
+
}
|
|
3651
|
+
});
|
|
3652
|
+
|
|
3653
|
+
// src/settings-client.ts
|
|
3654
|
+
function settingsError(cause) {
|
|
3655
|
+
return cause instanceof Error ? cause.message : String(cause);
|
|
3656
|
+
}
|
|
3657
|
+
function createSettingsSync(options) {
|
|
3658
|
+
let stopEvents;
|
|
3659
|
+
let stopped = false;
|
|
3660
|
+
function rpc() {
|
|
3661
|
+
const client = options.client();
|
|
3662
|
+
if (typeof client.rpc !== "function")
|
|
3663
|
+
return;
|
|
3664
|
+
try {
|
|
3665
|
+
return client.rpc(UsageInsightsSettings);
|
|
3666
|
+
} catch {
|
|
3667
|
+
return;
|
|
3668
|
+
}
|
|
3669
|
+
}
|
|
3670
|
+
async function refresh(force = false) {
|
|
3671
|
+
const client = rpc();
|
|
3672
|
+
if (!client) {
|
|
3673
|
+
if (!stopped)
|
|
3674
|
+
options.onLoadError?.("the settings service is unavailable");
|
|
3675
|
+
return;
|
|
3676
|
+
}
|
|
3677
|
+
try {
|
|
3678
|
+
const next = sanitizeSettingsEnvelope(await client.get({}));
|
|
3679
|
+
if (stopped)
|
|
3680
|
+
return;
|
|
3681
|
+
if (!force && options.canApply && !options.canApply(next))
|
|
3682
|
+
return;
|
|
3683
|
+
options.set(next);
|
|
3684
|
+
} catch (cause) {
|
|
3685
|
+
if (!stopped)
|
|
3686
|
+
options.onLoadError?.(settingsError(cause));
|
|
3687
|
+
}
|
|
3688
|
+
}
|
|
3689
|
+
function connect() {
|
|
3690
|
+
stopped = false;
|
|
3691
|
+
stopEvents?.();
|
|
3692
|
+
stopEvents = undefined;
|
|
3693
|
+
const client = rpc();
|
|
3694
|
+
if (client) {
|
|
3695
|
+
try {
|
|
3696
|
+
stopEvents = client.events.on("updated", (event) => {
|
|
3697
|
+
if (stopped)
|
|
3698
|
+
return;
|
|
3699
|
+
try {
|
|
3700
|
+
const next = sanitizeSettingsEnvelope(event.data);
|
|
3701
|
+
if (options.acceptEvent) {
|
|
3702
|
+
if (!options.acceptEvent(next))
|
|
3703
|
+
return;
|
|
3704
|
+
} else if (options.canApply && !options.canApply(next)) {
|
|
3705
|
+
return;
|
|
3706
|
+
}
|
|
3707
|
+
if (!stopped)
|
|
3708
|
+
options.set(next);
|
|
3709
|
+
} catch (cause) {
|
|
3710
|
+
if (!stopped)
|
|
3711
|
+
options.onLoadError?.(settingsError(cause));
|
|
3712
|
+
}
|
|
3713
|
+
});
|
|
3714
|
+
} catch (cause) {
|
|
3715
|
+
options.onLoadError?.(settingsError(cause));
|
|
3716
|
+
}
|
|
3717
|
+
}
|
|
3718
|
+
refresh();
|
|
3719
|
+
}
|
|
3720
|
+
async function update(patch) {
|
|
3721
|
+
if (stopped)
|
|
3722
|
+
return;
|
|
3723
|
+
const client = rpc();
|
|
3724
|
+
if (!client) {
|
|
3725
|
+
options.onSaveError?.("the settings service is unavailable");
|
|
3726
|
+
return;
|
|
3727
|
+
}
|
|
3728
|
+
try {
|
|
3729
|
+
const saved = sanitizeSettingsEnvelope(await client.set({ patch }));
|
|
3730
|
+
if (!stopped)
|
|
3731
|
+
options.set(saved);
|
|
3732
|
+
} catch (cause) {
|
|
3733
|
+
if (stopped)
|
|
3734
|
+
return;
|
|
3735
|
+
options.onSaveError?.(settingsError(cause));
|
|
3736
|
+
await refresh(true);
|
|
3737
|
+
}
|
|
3738
|
+
}
|
|
3739
|
+
async function reset() {
|
|
3740
|
+
if (stopped)
|
|
3741
|
+
return;
|
|
3742
|
+
const client = rpc();
|
|
3743
|
+
if (!client) {
|
|
3744
|
+
options.onSaveError?.("the settings service is unavailable");
|
|
3745
|
+
return;
|
|
3746
|
+
}
|
|
3747
|
+
try {
|
|
3748
|
+
const saved = sanitizeSettingsEnvelope(await client.reset({}));
|
|
3749
|
+
if (!stopped)
|
|
3750
|
+
options.set(saved);
|
|
3751
|
+
} catch (cause) {
|
|
3752
|
+
if (stopped)
|
|
3753
|
+
return;
|
|
3754
|
+
options.onSaveError?.(settingsError(cause));
|
|
3755
|
+
await refresh(true);
|
|
3756
|
+
}
|
|
3757
|
+
}
|
|
3758
|
+
return {
|
|
3759
|
+
connect,
|
|
3760
|
+
refresh,
|
|
3761
|
+
update,
|
|
3762
|
+
reset,
|
|
3763
|
+
stop: () => {
|
|
3764
|
+
stopped = true;
|
|
3765
|
+
stopEvents?.();
|
|
3766
|
+
stopEvents = undefined;
|
|
3767
|
+
}
|
|
3768
|
+
};
|
|
3769
|
+
}
|
|
3770
|
+
|
|
3323
3771
|
// src/sidebar.tsx
|
|
3324
3772
|
import { insert as _$insert2 } from "@opentui/solid";
|
|
3325
3773
|
import { effect as _$effect2 } from "@opentui/solid";
|
|
@@ -3351,7 +3799,7 @@ function TurnsSidebar(props) {
|
|
|
3351
3799
|
props.deps.refresh();
|
|
3352
3800
|
const input = {
|
|
3353
3801
|
sessionID: props.sessionID,
|
|
3354
|
-
limit: props.deps.limit,
|
|
3802
|
+
limit: props.deps.limit(),
|
|
3355
3803
|
width: props.deps.budget ?? 34
|
|
3356
3804
|
};
|
|
3357
3805
|
const current = ++generation;
|
|
@@ -3423,6 +3871,294 @@ function TurnsSidebar(props) {
|
|
|
3423
3871
|
})();
|
|
3424
3872
|
}
|
|
3425
3873
|
|
|
3874
|
+
// src/tui-settings.ts
|
|
3875
|
+
var FIELD_TITLES = {
|
|
3876
|
+
clock: "Clock",
|
|
3877
|
+
delta: "Time since previous prompt",
|
|
3878
|
+
model: "Provider and model",
|
|
3879
|
+
variant: "Model variant",
|
|
3880
|
+
turn: "Turn label prefix",
|
|
3881
|
+
session: "Session totals",
|
|
3882
|
+
reasoning: "Reasoning tokens",
|
|
3883
|
+
cost: "Cost",
|
|
3884
|
+
cacheExpiry: "Cache expiry warning"
|
|
3885
|
+
};
|
|
3886
|
+
var LABEL_TITLES = {
|
|
3887
|
+
delta: "Time since previous prompt",
|
|
3888
|
+
turn: "Turn prefix",
|
|
3889
|
+
session: "Session prefix",
|
|
3890
|
+
step: "Single step",
|
|
3891
|
+
steps: "Several steps",
|
|
3892
|
+
input: "Uncached input",
|
|
3893
|
+
cached: "Cached input",
|
|
3894
|
+
output: "Output",
|
|
3895
|
+
reasoning: "Reasoning",
|
|
3896
|
+
cost: "Cost",
|
|
3897
|
+
estimate: "Estimate marker",
|
|
3898
|
+
cacheExpired: "Cache expired warning",
|
|
3899
|
+
providerChanged: "Provider changed warning",
|
|
3900
|
+
modelChanged: "Model changed warning"
|
|
3901
|
+
};
|
|
3902
|
+
function onOff(value) {
|
|
3903
|
+
return value ? "on" : "off";
|
|
3904
|
+
}
|
|
3905
|
+
function fieldsSummary(settings) {
|
|
3906
|
+
const enabled = Object.keys(DEFAULT_FIELDS).filter((key) => settings.fields[key]).length;
|
|
3907
|
+
return `${enabled}/${Object.keys(DEFAULT_FIELDS).length} on`;
|
|
3908
|
+
}
|
|
3909
|
+
function labelsSummary(settings) {
|
|
3910
|
+
const custom = Object.keys(DEFAULT_LABELS).filter((key) => {
|
|
3911
|
+
const name = key;
|
|
3912
|
+
return settings.labels[name] !== DEFAULT_LABELS[name];
|
|
3913
|
+
}).length;
|
|
3914
|
+
return custom === 0 ? "defaults" : `${custom} custom`;
|
|
3915
|
+
}
|
|
3916
|
+
async function openUsageInsightsSettings(ctx, state) {
|
|
3917
|
+
for (;; ) {
|
|
3918
|
+
const current = state.settings();
|
|
3919
|
+
const error = state.error();
|
|
3920
|
+
const choice = await ctx.ui.dialog.select({
|
|
3921
|
+
title: "Usage insights settings",
|
|
3922
|
+
placeholder: "Filter settings",
|
|
3923
|
+
options: [
|
|
3924
|
+
...error === undefined ? [] : [
|
|
3925
|
+
{
|
|
3926
|
+
title: "Settings service unavailable",
|
|
3927
|
+
value: "done",
|
|
3928
|
+
description: error,
|
|
3929
|
+
category: "Connection",
|
|
3930
|
+
disabled: true
|
|
3931
|
+
},
|
|
3932
|
+
{
|
|
3933
|
+
title: "Retry connection",
|
|
3934
|
+
value: "retry",
|
|
3935
|
+
description: "Reload the shared settings",
|
|
3936
|
+
category: "Connection"
|
|
3937
|
+
}
|
|
3938
|
+
],
|
|
3939
|
+
{
|
|
3940
|
+
title: "List-price estimates",
|
|
3941
|
+
value: "pricing",
|
|
3942
|
+
description: onOff(current.pricing),
|
|
3943
|
+
category: "Recording"
|
|
3944
|
+
},
|
|
3945
|
+
{
|
|
3946
|
+
title: "Turn totals",
|
|
3947
|
+
value: "turnTotals",
|
|
3948
|
+
description: current.turnTotals === "sum" ? "sum every step" : "latest step",
|
|
3949
|
+
category: "Line"
|
|
3950
|
+
},
|
|
3951
|
+
{
|
|
3952
|
+
title: "Fields",
|
|
3953
|
+
value: "fields",
|
|
3954
|
+
description: fieldsSummary(current),
|
|
3955
|
+
category: "Line"
|
|
3956
|
+
},
|
|
3957
|
+
{
|
|
3958
|
+
title: "Labels",
|
|
3959
|
+
value: "labels",
|
|
3960
|
+
description: labelsSummary(current),
|
|
3961
|
+
category: "Line"
|
|
3962
|
+
},
|
|
3963
|
+
{
|
|
3964
|
+
title: "Panel",
|
|
3965
|
+
value: "panel",
|
|
3966
|
+
description: onOff(current.panel),
|
|
3967
|
+
category: "Surfaces"
|
|
3968
|
+
},
|
|
3969
|
+
{
|
|
3970
|
+
title: "Sidebar history",
|
|
3971
|
+
value: "sidebar",
|
|
3972
|
+
description: onOff(current.sidebar),
|
|
3973
|
+
category: "Surfaces"
|
|
3974
|
+
},
|
|
3975
|
+
{
|
|
3976
|
+
title: "Sidebar turns",
|
|
3977
|
+
value: "sidebarTurns",
|
|
3978
|
+
description: String(current.sidebarTurns),
|
|
3979
|
+
category: "Surfaces"
|
|
3980
|
+
},
|
|
3981
|
+
{
|
|
3982
|
+
title: "OpenCode Go allowance",
|
|
3983
|
+
value: "quota",
|
|
3984
|
+
description: onOff(current.quota),
|
|
3985
|
+
category: "Allowance"
|
|
3986
|
+
},
|
|
3987
|
+
{
|
|
3988
|
+
title: "Allowance label",
|
|
3989
|
+
value: "quotaName",
|
|
3990
|
+
description: current.quotaName || "provider default",
|
|
3991
|
+
category: "Allowance"
|
|
3992
|
+
},
|
|
3993
|
+
{
|
|
3994
|
+
title: "Server maintenance",
|
|
3995
|
+
value: "done",
|
|
3996
|
+
description: state.policy().maintenance ? "allowed" : "disabled by the server policy",
|
|
3997
|
+
category: "Server policy",
|
|
3998
|
+
disabled: true
|
|
3999
|
+
},
|
|
4000
|
+
{
|
|
4001
|
+
title: "Expose database paths",
|
|
4002
|
+
value: "done",
|
|
4003
|
+
description: state.policy().exposePaths ? "yes" : "no",
|
|
4004
|
+
category: "Server policy",
|
|
4005
|
+
disabled: true
|
|
4006
|
+
},
|
|
4007
|
+
{
|
|
4008
|
+
title: "Database path",
|
|
4009
|
+
value: "done",
|
|
4010
|
+
description: "OPENCODE_USAGE_INSIGHTS_DB",
|
|
4011
|
+
category: "Server policy",
|
|
4012
|
+
disabled: true
|
|
4013
|
+
},
|
|
4014
|
+
{
|
|
4015
|
+
title: "Reset to defaults",
|
|
4016
|
+
value: "reset",
|
|
4017
|
+
description: "Restore every shared setting",
|
|
4018
|
+
category: "Reset"
|
|
4019
|
+
},
|
|
4020
|
+
{ title: "Done", value: "done", category: "Actions" }
|
|
4021
|
+
]
|
|
4022
|
+
});
|
|
4023
|
+
if (choice === undefined || choice === "done")
|
|
4024
|
+
return;
|
|
4025
|
+
switch (choice) {
|
|
4026
|
+
case "pricing":
|
|
4027
|
+
await state.update({ pricing: !current.pricing });
|
|
4028
|
+
break;
|
|
4029
|
+
case "turnTotals":
|
|
4030
|
+
await editTurnTotals(ctx, state, current);
|
|
4031
|
+
break;
|
|
4032
|
+
case "fields":
|
|
4033
|
+
await editFields(ctx, state, current);
|
|
4034
|
+
break;
|
|
4035
|
+
case "labels":
|
|
4036
|
+
await editLabels(ctx, state, current);
|
|
4037
|
+
break;
|
|
4038
|
+
case "panel":
|
|
4039
|
+
await state.update({ panel: !current.panel });
|
|
4040
|
+
break;
|
|
4041
|
+
case "sidebar":
|
|
4042
|
+
await state.update({ sidebar: !current.sidebar });
|
|
4043
|
+
break;
|
|
4044
|
+
case "sidebarTurns":
|
|
4045
|
+
await editSidebarTurns(ctx, state, current);
|
|
4046
|
+
break;
|
|
4047
|
+
case "quota":
|
|
4048
|
+
await state.update({ quota: !current.quota });
|
|
4049
|
+
break;
|
|
4050
|
+
case "quotaName":
|
|
4051
|
+
await editQuotaName(ctx, state, current);
|
|
4052
|
+
break;
|
|
4053
|
+
case "retry":
|
|
4054
|
+
await state.refresh();
|
|
4055
|
+
break;
|
|
4056
|
+
case "reset":
|
|
4057
|
+
await reset(ctx, state);
|
|
4058
|
+
break;
|
|
4059
|
+
}
|
|
4060
|
+
}
|
|
4061
|
+
}
|
|
4062
|
+
async function editTurnTotals(ctx, state, current) {
|
|
4063
|
+
const value = await ctx.ui.dialog.select({
|
|
4064
|
+
title: "Turn totals",
|
|
4065
|
+
current: current.turnTotals,
|
|
4066
|
+
options: [
|
|
4067
|
+
{
|
|
4068
|
+
title: "Latest step",
|
|
4069
|
+
value: "latest",
|
|
4070
|
+
description: "The prompt size the turn ended with",
|
|
4071
|
+
category: "Current"
|
|
4072
|
+
},
|
|
4073
|
+
{
|
|
4074
|
+
title: "Sum every step",
|
|
4075
|
+
value: "sum",
|
|
4076
|
+
description: "Add the prompt of each step in the turn",
|
|
4077
|
+
category: "Current"
|
|
4078
|
+
}
|
|
4079
|
+
]
|
|
4080
|
+
});
|
|
4081
|
+
if (value === undefined)
|
|
4082
|
+
return;
|
|
4083
|
+
await state.update({ turnTotals: value });
|
|
4084
|
+
}
|
|
4085
|
+
async function editFields(ctx, state, current) {
|
|
4086
|
+
const key = await ctx.ui.dialog.select({
|
|
4087
|
+
title: "Line fields",
|
|
4088
|
+
current: undefined,
|
|
4089
|
+
options: Object.keys(DEFAULT_FIELDS).map((name) => ({
|
|
4090
|
+
title: FIELD_TITLES[name],
|
|
4091
|
+
value: name,
|
|
4092
|
+
description: onOff(current.fields[name]),
|
|
4093
|
+
category: "Current"
|
|
4094
|
+
}))
|
|
4095
|
+
});
|
|
4096
|
+
if (key === undefined)
|
|
4097
|
+
return;
|
|
4098
|
+
await state.update({ fields: { [key]: !current.fields[key] } });
|
|
4099
|
+
}
|
|
4100
|
+
async function editLabels(ctx, state, current) {
|
|
4101
|
+
const key = await ctx.ui.dialog.select({
|
|
4102
|
+
title: "Line labels",
|
|
4103
|
+
placeholder: "Filter labels",
|
|
4104
|
+
current: undefined,
|
|
4105
|
+
options: Object.keys(DEFAULT_LABELS).map((name) => ({
|
|
4106
|
+
title: LABEL_TITLES[name],
|
|
4107
|
+
value: name,
|
|
4108
|
+
description: current.labels[name] || "(empty)",
|
|
4109
|
+
category: "Current"
|
|
4110
|
+
}))
|
|
4111
|
+
});
|
|
4112
|
+
if (key === undefined)
|
|
4113
|
+
return;
|
|
4114
|
+
const value = await ctx.ui.dialog.prompt({
|
|
4115
|
+
title: LABEL_TITLES[key],
|
|
4116
|
+
description: "Text shown on the line. Leave it empty to hide the label.",
|
|
4117
|
+
value: current.labels[key]
|
|
4118
|
+
});
|
|
4119
|
+
if (value === undefined)
|
|
4120
|
+
return;
|
|
4121
|
+
await state.update({ labels: { [key]: sanitizeText(value, MAX_LABEL_LENGTH) } });
|
|
4122
|
+
}
|
|
4123
|
+
async function editSidebarTurns(ctx, state, current) {
|
|
4124
|
+
const value = await ctx.ui.dialog.prompt({
|
|
4125
|
+
title: "Sidebar turns",
|
|
4126
|
+
description: "How many turns the TUI sidebar and the Desktop Turns tab show (1-50).",
|
|
4127
|
+
value: String(current.sidebarTurns)
|
|
4128
|
+
});
|
|
4129
|
+
if (value === undefined)
|
|
4130
|
+
return;
|
|
4131
|
+
const turns = parseSidebarTurns(value);
|
|
4132
|
+
if (turns === undefined) {
|
|
4133
|
+
await ctx.ui.dialog.alert({
|
|
4134
|
+
title: "Invalid number of turns",
|
|
4135
|
+
message: "Use a whole number from 1 to 50. No change was saved."
|
|
4136
|
+
});
|
|
4137
|
+
return;
|
|
4138
|
+
}
|
|
4139
|
+
await state.update({ sidebarTurns: turns });
|
|
4140
|
+
}
|
|
4141
|
+
async function editQuotaName(ctx, state, current) {
|
|
4142
|
+
const value = await ctx.ui.dialog.prompt({
|
|
4143
|
+
title: "Allowance label",
|
|
4144
|
+
description: "Shown above the Go meters. Leave it empty for the provider name or OpenCode Go.",
|
|
4145
|
+
value: current.quotaName
|
|
4146
|
+
});
|
|
4147
|
+
if (value === undefined)
|
|
4148
|
+
return;
|
|
4149
|
+
await state.update({ quotaName: sanitizeText(value.trim(), MAX_QUOTA_NAME_LENGTH) });
|
|
4150
|
+
}
|
|
4151
|
+
async function reset(ctx, state) {
|
|
4152
|
+
const current = state.policy();
|
|
4153
|
+
const confirmed = await ctx.ui.dialog.confirm({
|
|
4154
|
+
title: "Reset Usage insights settings",
|
|
4155
|
+
message: `Restore the built-in line, panel and allowance settings${current.maintenance ? "" : " (server maintenance is disabled)"}?`,
|
|
4156
|
+
label: { confirm: "Reset", cancel: "Cancel" }
|
|
4157
|
+
});
|
|
4158
|
+
if (confirmed)
|
|
4159
|
+
await state.reset();
|
|
4160
|
+
}
|
|
4161
|
+
|
|
3426
4162
|
// src/tui.tsx
|
|
3427
4163
|
var REFRESH_THROTTLE_MS = 300;
|
|
3428
4164
|
var __test = {
|
|
@@ -3434,14 +4170,81 @@ var __test = {
|
|
|
3434
4170
|
var tui_default = Plugin.define({
|
|
3435
4171
|
id: "usage-insights",
|
|
3436
4172
|
setup(ctx) {
|
|
3437
|
-
const
|
|
3438
|
-
|
|
3439
|
-
|
|
3440
|
-
const
|
|
3441
|
-
|
|
3442
|
-
|
|
3443
|
-
const
|
|
3444
|
-
|
|
4173
|
+
const [usageSettings, setUsageSettings] = createSignal3(defaultUsageSettings());
|
|
4174
|
+
const [policy, setPolicy] = createSignal3(defaultSettingsEnvelope().policy);
|
|
4175
|
+
const [settingsError, setSettingsError] = createSignal3(undefined);
|
|
4176
|
+
const [settingsLoaded, setSettingsLoaded] = createSignal3(false);
|
|
4177
|
+
let firstSettings = true;
|
|
4178
|
+
let saving = false;
|
|
4179
|
+
const envelope = () => ({
|
|
4180
|
+
settings: usageSettings(),
|
|
4181
|
+
policy: policy()
|
|
4182
|
+
});
|
|
4183
|
+
const sync = createSettingsSync({
|
|
4184
|
+
client: () => ctx.client,
|
|
4185
|
+
set: (next) => {
|
|
4186
|
+
setUsageSettings(next.settings);
|
|
4187
|
+
setPolicy(next.policy);
|
|
4188
|
+
setSettingsError(undefined);
|
|
4189
|
+
setSettingsLoaded(true);
|
|
4190
|
+
if (!firstSettings)
|
|
4191
|
+
bumpRefresh();
|
|
4192
|
+
firstSettings = false;
|
|
4193
|
+
},
|
|
4194
|
+
canApply: () => !saving,
|
|
4195
|
+
acceptEvent: (next) => {
|
|
4196
|
+
if (saving)
|
|
4197
|
+
return false;
|
|
4198
|
+
return JSON.stringify(next) !== JSON.stringify(envelope());
|
|
4199
|
+
},
|
|
4200
|
+
onLoadError: (message) => {
|
|
4201
|
+
setSettingsError(message);
|
|
4202
|
+
setSettingsLoaded(true);
|
|
4203
|
+
firstSettings = false;
|
|
4204
|
+
},
|
|
4205
|
+
onSaveError: (message) => {
|
|
4206
|
+
setSettingsError(message);
|
|
4207
|
+
ctx.ui.toast.show({
|
|
4208
|
+
message: `Usage insights settings: ${message}`,
|
|
4209
|
+
variant: "error"
|
|
4210
|
+
});
|
|
4211
|
+
}
|
|
4212
|
+
});
|
|
4213
|
+
sync.connect();
|
|
4214
|
+
const panelEnabled = () => usageSettings().panel;
|
|
4215
|
+
const pricing = () => usageSettings().pricing;
|
|
4216
|
+
const quotaEnabled = () => usageSettings().quota;
|
|
4217
|
+
const quotaActive = () => settingsLoaded() && quotaEnabled() && (usageSettings().sidebar || panelEnabled());
|
|
4218
|
+
const views = createMemo3(() => quotaEnabled() ? CLIENT_VIEWS : PANEL_VIEWS);
|
|
4219
|
+
const resolved = createMemo3(() => resolveUsageSettings(usageSettings()));
|
|
4220
|
+
async function saveSettings(patch) {
|
|
4221
|
+
setUsageSettings((current) => mergeUsageSettings(current, patch));
|
|
4222
|
+
saving = true;
|
|
4223
|
+
try {
|
|
4224
|
+
await sync.update(patch);
|
|
4225
|
+
bumpRefresh();
|
|
4226
|
+
} finally {
|
|
4227
|
+
saving = false;
|
|
4228
|
+
}
|
|
4229
|
+
}
|
|
4230
|
+
async function resetSettings() {
|
|
4231
|
+
setUsageSettings(defaultUsageSettings());
|
|
4232
|
+
saving = true;
|
|
4233
|
+
try {
|
|
4234
|
+
await sync.reset();
|
|
4235
|
+
bumpRefresh();
|
|
4236
|
+
} finally {
|
|
4237
|
+
saving = false;
|
|
4238
|
+
}
|
|
4239
|
+
}
|
|
4240
|
+
const settingsState = {
|
|
4241
|
+
settings: usageSettings,
|
|
4242
|
+
policy,
|
|
4243
|
+
error: settingsError,
|
|
4244
|
+
refresh: () => sync.refresh(true),
|
|
4245
|
+
update: saveSettings,
|
|
4246
|
+
reset: resetSettings
|
|
4247
|
+
};
|
|
3445
4248
|
function providerName() {
|
|
3446
4249
|
try {
|
|
3447
4250
|
const providers = ctx.data.location?.provider?.list(ctx.location);
|
|
@@ -3450,7 +4253,7 @@ var tui_default = Plugin.define({
|
|
|
3450
4253
|
return;
|
|
3451
4254
|
}
|
|
3452
4255
|
}
|
|
3453
|
-
const quotaName = quotaNameOf(
|
|
4256
|
+
const quotaName = () => quotaNameOf(resolved().quotaName, providerName());
|
|
3454
4257
|
const PRICE_TTL_MS = 5 * 60 * 1000;
|
|
3455
4258
|
let priceCache;
|
|
3456
4259
|
function errorMessage(cause) {
|
|
@@ -3462,16 +4265,16 @@ var tui_default = Plugin.define({
|
|
|
3462
4265
|
return "the usage limits request failed";
|
|
3463
4266
|
}
|
|
3464
4267
|
const [quotaState, setQuotaState] = createSignal3(undefined);
|
|
3465
|
-
const [quotaLoading, setQuotaLoading] = createSignal3(quotaActive);
|
|
4268
|
+
const [quotaLoading, setQuotaLoading] = createSignal3(quotaActive());
|
|
3466
4269
|
const [quotaError, setQuotaError] = createSignal3(undefined);
|
|
3467
4270
|
const [quotaTick, setQuotaTick] = createSignal3(0);
|
|
3468
4271
|
let quotaGeneration = 0;
|
|
3469
4272
|
const refreshQuota = () => {
|
|
3470
|
-
if (quotaActive)
|
|
4273
|
+
if (quotaActive())
|
|
3471
4274
|
setQuotaTick((value) => value + 1);
|
|
3472
4275
|
};
|
|
3473
4276
|
createEffect3(() => {
|
|
3474
|
-
if (!quotaActive)
|
|
4277
|
+
if (!quotaActive())
|
|
3475
4278
|
return;
|
|
3476
4279
|
quotaTick();
|
|
3477
4280
|
const generation = ++quotaGeneration;
|
|
@@ -3493,7 +4296,7 @@ var tui_default = Plugin.define({
|
|
|
3493
4296
|
});
|
|
3494
4297
|
});
|
|
3495
4298
|
createEffect3(() => {
|
|
3496
|
-
if (!quotaActive)
|
|
4299
|
+
if (!quotaActive())
|
|
3497
4300
|
return;
|
|
3498
4301
|
const timer = setInterval(() => setQuotaTick((value) => value + 1), QUOTA_POLL_MS);
|
|
3499
4302
|
onCleanup2(() => clearInterval(timer));
|
|
@@ -3505,7 +4308,7 @@ var tui_default = Plugin.define({
|
|
|
3505
4308
|
refresh: refreshQuota
|
|
3506
4309
|
};
|
|
3507
4310
|
function priceTableFor() {
|
|
3508
|
-
if (!pricing)
|
|
4311
|
+
if (!pricing())
|
|
3509
4312
|
return;
|
|
3510
4313
|
const now = Date.now();
|
|
3511
4314
|
if (priceCache !== undefined && now - priceCache.at < PRICE_TTL_MS)
|
|
@@ -3526,11 +4329,11 @@ var tui_default = Plugin.define({
|
|
|
3526
4329
|
const table = priceTableFor();
|
|
3527
4330
|
return table === undefined ? undefined : (ref, tokens) => costOf(table, ref, tokens);
|
|
3528
4331
|
}
|
|
3529
|
-
const [
|
|
4332
|
+
const [panelSettings, updatePanelSettings] = ctx.storage.store("panel", {
|
|
3530
4333
|
initial: DEFAULT_SETTINGS
|
|
3531
4334
|
});
|
|
3532
|
-
const
|
|
3533
|
-
|
|
4335
|
+
const patchPanelSettings = (mutation) => {
|
|
4336
|
+
updatePanelSettings(mutation);
|
|
3534
4337
|
};
|
|
3535
4338
|
const [refresh, setRefresh] = createSignal3(0);
|
|
3536
4339
|
const [listCosts, setListCosts] = createSignal3({});
|
|
@@ -3557,12 +4360,12 @@ var tui_default = Plugin.define({
|
|
|
3557
4360
|
refreshTimer.unref?.();
|
|
3558
4361
|
}
|
|
3559
4362
|
function listCostFor(sessionID) {
|
|
3560
|
-
if (!pricing)
|
|
4363
|
+
if (!pricing())
|
|
3561
4364
|
return 0;
|
|
3562
4365
|
return listCosts()[sessionID] ?? 0;
|
|
3563
4366
|
}
|
|
3564
4367
|
async function loadListCost(sessionID) {
|
|
3565
|
-
if (!pricing)
|
|
4368
|
+
if (!pricing())
|
|
3566
4369
|
return;
|
|
3567
4370
|
if (pendingListCost.has(sessionID)) {
|
|
3568
4371
|
dirtyListCost.add(sessionID);
|
|
@@ -3600,7 +4403,7 @@ var tui_default = Plugin.define({
|
|
|
3600
4403
|
function lineFor(sessionID) {
|
|
3601
4404
|
if (!sessionIdle(sessionID))
|
|
3602
4405
|
return;
|
|
3603
|
-
if (pricing)
|
|
4406
|
+
if (pricing())
|
|
3604
4407
|
refresh();
|
|
3605
4408
|
const messages = ctx.data.session.message.list(sessionID);
|
|
3606
4409
|
const info = ctx.data.session.get(sessionID);
|
|
@@ -3615,12 +4418,7 @@ var tui_default = Plugin.define({
|
|
|
3615
4418
|
return ctx.theme.text.base;
|
|
3616
4419
|
}
|
|
3617
4420
|
function Line(props) {
|
|
3618
|
-
const parts = createMemo3(() => lineParts(lineFor(props.sessionID),
|
|
3619
|
-
labels: options.labels,
|
|
3620
|
-
fields: options.fields,
|
|
3621
|
-
turnTotals: options.turnTotals,
|
|
3622
|
-
estimates: pricing
|
|
3623
|
-
}));
|
|
4421
|
+
const parts = createMemo3(() => lineParts(lineFor(props.sessionID), resolved().format));
|
|
3624
4422
|
createEffect3(() => {
|
|
3625
4423
|
props.sessionID;
|
|
3626
4424
|
refresh();
|
|
@@ -3658,46 +4456,50 @@ var tui_default = Plugin.define({
|
|
|
3658
4456
|
}
|
|
3659
4457
|
})
|
|
3660
4458
|
});
|
|
3661
|
-
const disposePanel =
|
|
4459
|
+
const disposePanel = ctx.ui.slot({
|
|
3662
4460
|
append: "session.panel",
|
|
3663
|
-
render: (input) => input.name === PANEL_NAME ? _$createComponent3(Panel, {
|
|
4461
|
+
render: (input) => input.name === PANEL_NAME && panelEnabled() ? _$createComponent3(Panel, {
|
|
3664
4462
|
input,
|
|
3665
4463
|
ctx,
|
|
3666
4464
|
deps: {
|
|
3667
4465
|
refresh,
|
|
3668
4466
|
bump: bumpRefresh
|
|
3669
4467
|
},
|
|
3670
|
-
settings,
|
|
3671
|
-
update:
|
|
4468
|
+
settings: panelSettings,
|
|
4469
|
+
update: patchPanelSettings,
|
|
3672
4470
|
get maintenance() {
|
|
3673
|
-
return
|
|
4471
|
+
return policy().maintenance;
|
|
4472
|
+
},
|
|
4473
|
+
get views() {
|
|
4474
|
+
return views();
|
|
3674
4475
|
},
|
|
3675
|
-
views,
|
|
3676
4476
|
quota: quotaResource,
|
|
3677
|
-
quotaName
|
|
4477
|
+
get quotaName() {
|
|
4478
|
+
return quotaName();
|
|
4479
|
+
}
|
|
3678
4480
|
}) : null
|
|
3679
|
-
})
|
|
4481
|
+
});
|
|
3680
4482
|
const disposeSidebar = ctx.ui.slot({
|
|
3681
4483
|
append: "sidebar.content",
|
|
3682
|
-
render: (input) =>
|
|
4484
|
+
render: (input) => usageSettings().sidebar ? _$createComponent3(TurnsSidebar, {
|
|
3683
4485
|
get sessionID() {
|
|
3684
4486
|
return input.sessionID;
|
|
3685
4487
|
},
|
|
3686
4488
|
ctx,
|
|
3687
|
-
|
|
3688
|
-
|
|
3689
|
-
|
|
3690
|
-
limit: clampTurnsLimit(options.sidebarTurns ?? 8)
|
|
3691
|
-
};
|
|
4489
|
+
deps: {
|
|
4490
|
+
refresh,
|
|
4491
|
+
limit: () => resolved().sidebarTurns
|
|
3692
4492
|
},
|
|
3693
|
-
quota: quotaEnabled ? quotaState : undefined,
|
|
3694
|
-
quotaName
|
|
3695
|
-
|
|
4493
|
+
quota: () => quotaEnabled() ? quotaState() : undefined,
|
|
4494
|
+
get quotaName() {
|
|
4495
|
+
return quotaName();
|
|
4496
|
+
}
|
|
4497
|
+
}) : null
|
|
3696
4498
|
});
|
|
3697
4499
|
function open(input) {
|
|
3698
|
-
const parsed = parseArgs(input, views);
|
|
4500
|
+
const parsed = parseArgs(input, views());
|
|
3699
4501
|
if (parsed.view !== undefined || parsed.range !== undefined) {
|
|
3700
|
-
|
|
4502
|
+
patchPanelSettings((draft) => {
|
|
3701
4503
|
if (parsed.view !== undefined)
|
|
3702
4504
|
draft.view = parsed.view;
|
|
3703
4505
|
if (parsed.range !== undefined)
|
|
@@ -3711,34 +4513,45 @@ var tui_default = Plugin.define({
|
|
|
3711
4513
|
}
|
|
3712
4514
|
return ctx.ui.panel.open(PANEL_NAME);
|
|
3713
4515
|
}
|
|
3714
|
-
|
|
3715
|
-
|
|
3716
|
-
|
|
3717
|
-
|
|
3718
|
-
|
|
3719
|
-
|
|
3720
|
-
|
|
3721
|
-
|
|
3722
|
-
|
|
3723
|
-
|
|
3724
|
-
|
|
3725
|
-
|
|
3726
|
-
|
|
3727
|
-
|
|
3728
|
-
|
|
3729
|
-
|
|
3730
|
-
|
|
3731
|
-
|
|
3732
|
-
|
|
3733
|
-
|
|
3734
|
-
|
|
3735
|
-
|
|
3736
|
-
}
|
|
4516
|
+
ctx.keymap.layer(() => {
|
|
4517
|
+
return {
|
|
4518
|
+
mode: "global",
|
|
4519
|
+
commands: [{
|
|
4520
|
+
id: "usage-insights.stats",
|
|
4521
|
+
title: "Usage insights",
|
|
4522
|
+
description: "Open the usage statistics panel",
|
|
4523
|
+
group: "Usage insights",
|
|
4524
|
+
bind: false,
|
|
4525
|
+
palette: true,
|
|
4526
|
+
enabled: () => panelEnabled(),
|
|
4527
|
+
slash: {
|
|
4528
|
+
name: "usage-insights",
|
|
4529
|
+
aliases: ["insights"],
|
|
4530
|
+
arguments: true
|
|
4531
|
+
},
|
|
4532
|
+
run: (input) => {
|
|
4533
|
+
if (!open(input)) {
|
|
4534
|
+
ctx.ui.toast.show({
|
|
4535
|
+
message: "Open a session first, then run /usage-insights",
|
|
4536
|
+
variant: "warning"
|
|
4537
|
+
});
|
|
3737
4538
|
}
|
|
3738
|
-
}
|
|
3739
|
-
}
|
|
3740
|
-
|
|
3741
|
-
|
|
4539
|
+
}
|
|
4540
|
+
}, {
|
|
4541
|
+
id: "usage-insights.settings",
|
|
4542
|
+
title: "Usage insights settings",
|
|
4543
|
+
description: "Line, panel and allowance settings shared with the Desktop",
|
|
4544
|
+
group: "Usage insights",
|
|
4545
|
+
bind: false,
|
|
4546
|
+
palette: true,
|
|
4547
|
+
slash: {
|
|
4548
|
+
name: "insights-settings",
|
|
4549
|
+
aliases: ["usage-insights-settings"]
|
|
4550
|
+
},
|
|
4551
|
+
run: () => void openUsageInsightsSettings(ctx, settingsState)
|
|
4552
|
+
}]
|
|
4553
|
+
};
|
|
4554
|
+
});
|
|
3742
4555
|
const watchRecorded = () => {
|
|
3743
4556
|
try {
|
|
3744
4557
|
return api.onRecorded(() => bumpRefresh());
|
|
@@ -3755,21 +4568,17 @@ var tui_default = Plugin.define({
|
|
|
3755
4568
|
ctx.data.on("session.compaction.ended", () => bumpRefresh()),
|
|
3756
4569
|
ctx.data.on("model.updated", () => {
|
|
3757
4570
|
priceCache = undefined;
|
|
3758
|
-
})
|
|
4571
|
+
}),
|
|
4572
|
+
ctx.data.on("server.connected", () => sync.connect())
|
|
3759
4573
|
];
|
|
3760
4574
|
__test.line = lineFor;
|
|
3761
4575
|
__test.lineText = (sessionID) => {
|
|
3762
4576
|
loadListCost(sessionID);
|
|
3763
|
-
return lineText(lineParts(lineFor(sessionID),
|
|
3764
|
-
labels: options.labels,
|
|
3765
|
-
fields: options.fields,
|
|
3766
|
-
turnTotals: options.turnTotals,
|
|
3767
|
-
estimates: pricing
|
|
3768
|
-
}));
|
|
4577
|
+
return lineText(lineParts(lineFor(sessionID), resolved().format));
|
|
3769
4578
|
};
|
|
3770
4579
|
__test.open = open;
|
|
3771
4580
|
__test.state = () => {
|
|
3772
|
-
const current = sanitizeSettings(
|
|
4581
|
+
const current = sanitizeSettings(panelSettings, views());
|
|
3773
4582
|
return {
|
|
3774
4583
|
view: current.view,
|
|
3775
4584
|
range: current.range
|
|
@@ -3778,6 +4587,7 @@ var tui_default = Plugin.define({
|
|
|
3778
4587
|
return () => {
|
|
3779
4588
|
if (refreshTimer !== undefined)
|
|
3780
4589
|
clearTimeout(refreshTimer);
|
|
4590
|
+
sync.stop();
|
|
3781
4591
|
for (const dispose of disposeEvents)
|
|
3782
4592
|
dispose();
|
|
3783
4593
|
disposeLine();
|
|
@@ -3791,4 +4601,4 @@ export {
|
|
|
3791
4601
|
tui_default as default
|
|
3792
4602
|
};
|
|
3793
4603
|
|
|
3794
|
-
//# debugId=
|
|
4604
|
+
//# debugId=7CDA7A48D491D99D64756E2164756E21
|