quotacap 0.0.57 → 0.0.59
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/adapters/muse.d.ts +1 -1
- package/dist/adapters/muse.js +5 -3
- package/dist/advisory/engine.js +9 -5
- package/dist/advisory/snapshot.js +10 -11
- package/dist/cli/advise.d.ts +1 -1
- package/dist/cli/advise.js +9 -4
- package/dist/cli/models.js +2 -2
- package/dist/cli/status.js +2 -2
- package/dist/format/markdown.d.ts +1 -1
- package/dist/format/markdown.js +11 -8
- package/dist/format/rate-limit-wait.d.ts +38 -0
- package/dist/format/rate-limit-wait.js +86 -0
- package/dist/format/rows.d.ts +46 -6
- package/dist/format/rows.js +50 -42
- package/dist/format/terminal.d.ts +1 -1
- package/dist/format/terminal.js +28 -14
- package/dist/format/time.d.ts +19 -0
- package/dist/format/time.js +69 -0
- package/dist/mcp/server.d.ts +25 -5
- package/dist/mcp/server.js +38 -4
- package/dist/store/quotas.d.ts +7 -2
- package/dist/store/quotas.js +94 -15
- package/dist/version.js +1 -1
- package/dist/web/src/components/AdviceDrawer.d.ts +3 -1
- package/dist/web/src/components/AdviceDrawer.js +25 -4
- package/dist/web/src/components/ClosedWeeks.d.ts +4 -1
- package/dist/web/src/components/ClosedWeeks.js +17 -5
- package/dist/web/src/components/PaceBar.d.ts +26 -25
- package/dist/web/src/components/PaceBar.js +57 -119
- package/dist/web/src/components/PacingLegend.js +9 -8
- package/dist/web/src/components/ProviderCard.js +2 -1
- package/dist/web/src/components/ProviderDrawer.d.ts +9 -2
- package/dist/web/src/components/ProviderDrawer.js +38 -27
- package/dist/web/src/components/ProviderRow.js +2 -1
- package/dist/web/src/components/RateLimitNote.d.ts +7 -0
- package/dist/web/src/components/RateLimitNote.js +11 -0
- package/dist/web/src/components/Recommendation.d.ts +4 -3
- package/dist/web/src/components/Recommendation.js +11 -7
- package/dist/web/src/components/ResetRail.d.ts +2 -0
- package/dist/web/src/components/ResetRail.js +15 -5
- package/dist/web/src/components/WindowsRow.d.ts +2 -2
- package/dist/web/src/components/WindowsRow.js +3 -13
- package/dist/web/src/components/settings/ProvidersPanel.d.ts +3 -2
- package/dist/web/src/components/settings/ProvidersPanel.js +4 -1
- package/dist/web/src/pages/Onboarding.js +1 -1
- package/dist/web/src/state.d.ts +5 -0
- package/dist/web/src/state.js +18 -0
- package/dist/webAssets.js +1 -1
- package/dist/webHtml.js +1 -1
- package/package.json +1 -1
- package/web/dist/assets/index-DQ2jUHo6.js +13 -0
- package/web/dist/assets/index-DSNVHbgI.css +1 -0
- package/web/dist/index.html +2 -2
- package/web/dist/assets/index-BFBVZ1r_.js +0 -13
- package/web/dist/assets/index-Co33DTl5.css +0 -1
|
@@ -9,4 +9,4 @@ export interface RenderOptions {
|
|
|
9
9
|
export declare function provWidthFor(ids: string[]): number;
|
|
10
10
|
export declare function renderWide(snapshot: StateSnapshot, opts: RenderOptions): string;
|
|
11
11
|
export declare function renderNarrow(snapshot: StateSnapshot, opts: RenderOptions): string;
|
|
12
|
-
export declare function renderCompact(snapshot: StateSnapshot,
|
|
12
|
+
export declare function renderCompact(snapshot: StateSnapshot, now?: Date): string;
|
package/dist/format/terminal.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { buildRow, compactSuffix, isMonthlyBound, sortProviders, weeklyPctOf, } from "./rows.js";
|
|
1
|
+
import { buildRow, compactSuffix, shownProviders, useName, isMonthlyBound, sortProviders, weeklyPctOf, } from "./rows.js";
|
|
2
|
+
import { rateLimitWaits } from "./rate-limit-wait.js";
|
|
2
3
|
const GAP = " ";
|
|
3
4
|
const MIN_PROV_WIDTH = 12;
|
|
4
5
|
const MAX_PROV_WIDTH = 22;
|
|
@@ -74,10 +75,11 @@ export function renderWide(snapshot, opts) {
|
|
|
74
75
|
const g = opts.ascii ? ASCII : UNICODE;
|
|
75
76
|
const color = !!opts.color && !opts.ascii;
|
|
76
77
|
const use = snapshot.recommendation.use;
|
|
77
|
-
const sorted = sortProviders(snapshot.providers, opts.sort ?? "recommended", use);
|
|
78
|
-
const width = provWidthFor(snapshot.providers.map((p) => p.displayName));
|
|
78
|
+
const sorted = sortProviders(shownProviders(snapshot.providers), opts.sort ?? "recommended", use);
|
|
79
|
+
const width = provWidthFor(shownProviders(snapshot.providers).map((p) => p.displayName));
|
|
79
80
|
const rows = sorted.map((p) => ({ p, row: buildRow(p, opts.now) }));
|
|
80
81
|
const paceWidth = Math.max("PACE (%/DAY)".length, ...rows.map(({ row }) => row.pace.length));
|
|
82
|
+
const resetsWidth = Math.max("RESETS".length, ...rows.map(({ row }) => row.countdown.length));
|
|
81
83
|
const paint = (s, code) => (color ? `${code}${s}${RESET}` : s);
|
|
82
84
|
const resetsIndent = " ".repeat(width + GAP.length + 6 + GAP.length + 8 + GAP.length + paceWidth + GAP.length + 20 + GAP.length);
|
|
83
85
|
const leftoverLine = (p) => {
|
|
@@ -94,7 +96,7 @@ export function renderWide(snapshot, opts) {
|
|
|
94
96
|
"ELAPSED".padStart(8),
|
|
95
97
|
"PACE (%/DAY)".padEnd(paceWidth),
|
|
96
98
|
"USED VS TIME".padEnd(20),
|
|
97
|
-
"RESETS".padEnd(
|
|
99
|
+
"RESETS".padEnd(resetsWidth),
|
|
98
100
|
"STATE".padEnd(19),
|
|
99
101
|
"FORECAST",
|
|
100
102
|
].join(GAP);
|
|
@@ -105,9 +107,12 @@ export function renderWide(snapshot, opts) {
|
|
|
105
107
|
const elapsed = (row.elapsedPct === null ? g.dash : `${row.elapsedPct}%`).padStart(8);
|
|
106
108
|
const pace = (opts.ascii ? asciiText(row.pace) : row.pace).padEnd(paceWidth);
|
|
107
109
|
const rail = renderRail(row.railCells, g, row.state, color);
|
|
108
|
-
const countdown = (opts.ascii ? asciiText(row.countdown) : row.countdown).padEnd(
|
|
110
|
+
const countdown = (opts.ascii ? asciiText(row.countdown) : row.countdown).padEnd(resetsWidth);
|
|
109
111
|
const state = row.state.padEnd(19);
|
|
110
112
|
const forecast = opts.ascii ? asciiText(row.forecast) : row.forecast;
|
|
113
|
+
const wait = row.wait
|
|
114
|
+
? `${GAP}${opts.ascii ? "-" : "·"}${GAP}${paint(opts.ascii ? asciiText(row.wait) : row.wait, STATE_COLORS["Quota exhausted"])}`
|
|
115
|
+
: "";
|
|
111
116
|
const main = [
|
|
112
117
|
color ? colorProviderField(name, recommended, row.state) : name,
|
|
113
118
|
used,
|
|
@@ -116,7 +121,7 @@ export function renderWide(snapshot, opts) {
|
|
|
116
121
|
rail,
|
|
117
122
|
countdown,
|
|
118
123
|
paint(state, STATE_COLORS[row.state]),
|
|
119
|
-
forecast,
|
|
124
|
+
forecast + wait,
|
|
120
125
|
].join(GAP);
|
|
121
126
|
const leftover = leftoverLine(p);
|
|
122
127
|
return leftover ? `${main}\n${leftover}` : main;
|
|
@@ -127,8 +132,8 @@ export function renderNarrow(snapshot, opts) {
|
|
|
127
132
|
const g = opts.ascii ? ASCII : UNICODE;
|
|
128
133
|
const color = !!opts.color && !opts.ascii;
|
|
129
134
|
const use = snapshot.recommendation.use;
|
|
130
|
-
const sorted = sortProviders(snapshot.providers, opts.sort ?? "recommended", use);
|
|
131
|
-
const width = provWidthFor(snapshot.providers.map((p) => p.displayName));
|
|
135
|
+
const sorted = sortProviders(shownProviders(snapshot.providers), opts.sort ?? "recommended", use);
|
|
136
|
+
const width = provWidthFor(shownProviders(snapshot.providers).map((p) => p.displayName));
|
|
132
137
|
const lines = [];
|
|
133
138
|
for (const p of sorted) {
|
|
134
139
|
const row = buildRow(p, opts.now);
|
|
@@ -142,7 +147,10 @@ export function renderNarrow(snapshot, opts) {
|
|
|
142
147
|
const pace = opts.ascii ? asciiText(row.pace) : row.pace;
|
|
143
148
|
const paceBits = row.pace === "—" ? pace : `${pace} %/day`;
|
|
144
149
|
const state = color ? `${STATE_COLORS[row.state]}${row.state}${RESET}` : row.state;
|
|
145
|
-
|
|
150
|
+
const wait = row.wait
|
|
151
|
+
? ` ${g.sep} ${color ? `${STATE_COLORS["Quota exhausted"]}${opts.ascii ? asciiText(row.wait) : row.wait}${RESET}` : opts.ascii ? asciiText(row.wait) : row.wait}`
|
|
152
|
+
: "";
|
|
153
|
+
lines.push(`${color ? colorProviderField(name, recommended, row.state) : name} ${used} used ${g.sep} ${elapsed} elapsed ${g.sep} ${paceBits}`, `${rail} resets ${countdown} ${g.sep} ${state} ${g.sep} ${forecast}${wait}`);
|
|
146
154
|
const close = p.lastCloses?.[0];
|
|
147
155
|
if (close) {
|
|
148
156
|
const indent = " ".repeat(row.railCells.length + " resets ".length);
|
|
@@ -155,18 +163,24 @@ export function renderNarrow(snapshot, opts) {
|
|
|
155
163
|
}
|
|
156
164
|
// Always ASCII, never ANSI, even on a TTY: prompt-safe by construction.
|
|
157
165
|
// Takes the fixed clock for signature parity; compact shows no ages.
|
|
158
|
-
export function renderCompact(snapshot,
|
|
159
|
-
void _now;
|
|
166
|
+
export function renderCompact(snapshot, now = new Date()) {
|
|
160
167
|
const use = snapshot.recommendation.use;
|
|
161
|
-
const sorted = sortProviders(snapshot.providers, "recommended", use);
|
|
168
|
+
const sorted = sortProviders(shownProviders(snapshot.providers), "recommended", use);
|
|
162
169
|
const entries = sorted
|
|
163
170
|
.filter((p) => p.quota !== null)
|
|
164
171
|
.map((p) => {
|
|
165
172
|
const u = weeklyPctOf(p.quota);
|
|
166
173
|
const used = typeof u === "number" && Number.isFinite(u) ? String(Math.round(u)) : "?";
|
|
167
|
-
return `[${p.
|
|
174
|
+
return `[${p.displayName}:${used}%${compactSuffix(p)}]`;
|
|
168
175
|
});
|
|
169
176
|
if (entries.length === 0)
|
|
170
177
|
return "(use: none)";
|
|
171
|
-
|
|
178
|
+
const waits = sorted.flatMap((p) => {
|
|
179
|
+
const found = rateLimitWaits(p, now);
|
|
180
|
+
if (found.length === 0)
|
|
181
|
+
return [];
|
|
182
|
+
return [`${p.displayName} ${found.map((w) => w.text).join(", ")}`];
|
|
183
|
+
});
|
|
184
|
+
const base = `${entries.join(" | ")} (use: ${useName(snapshot)})`;
|
|
185
|
+
return waits.length === 0 ? base : `${base} (wait: ${waits.join("; ")})`;
|
|
172
186
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Floored duration: "6d 23h" from one day up, "5h 12m" from one hour up,
|
|
3
|
+
* else "45m". Sub-minute spans read "1m" so a live window never prints 0m.
|
|
4
|
+
*/
|
|
5
|
+
export declare function durationText(ms: number): string;
|
|
6
|
+
/** Duration from fractional days, for engine fields such as daysLeft. */
|
|
7
|
+
export declare function durationFromDays(days: number): string;
|
|
8
|
+
/**
|
|
9
|
+
* Wall-clock instant: "Sat 14:46" within 7 days of `nowMs`, where the
|
|
10
|
+
* weekday is unambiguous, else "Sun 22 Nov 23:05". Without `nowMs` the
|
|
11
|
+
* dated form is used. Null when unparseable.
|
|
12
|
+
*/
|
|
13
|
+
export declare function clockText(iso: string | null | undefined, nowMs?: number): string | null;
|
|
14
|
+
/** Calendar date without the weekday or clock, e.g. "22 Nov". Empty when unparseable. */
|
|
15
|
+
export declare function dateText(iso: string | null | undefined): string;
|
|
16
|
+
/** Weekday plus calendar date, e.g. "Sun 22 Nov". */
|
|
17
|
+
export declare function dayText(iso: string | null | undefined): string;
|
|
18
|
+
/** The one waste sentence: "16% unused at reset (Fri 13:14)". */
|
|
19
|
+
export declare function unusedAtReset(wastePct: number, resetsAt: string | null | undefined, nowMs?: number): string;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// One duration and one clock vocabulary for every human surface: the
|
|
2
|
+
// dashboard imports these, the CLI rows and the engine reason use them, so
|
|
3
|
+
// one instant never reads "5d 23h" in one place and "6d 0h" or "6.0d" in
|
|
4
|
+
// another. Pinned to en-US with a 24-hour clock: the sentences they feed
|
|
5
|
+
// are English, so a non-English machine must not print localized parts.
|
|
6
|
+
const DAY_MS = 86400000;
|
|
7
|
+
const HOUR_MS = 3600000;
|
|
8
|
+
const MIN_MS = 60000;
|
|
9
|
+
/**
|
|
10
|
+
* Floored duration: "6d 23h" from one day up, "5h 12m" from one hour up,
|
|
11
|
+
* else "45m". Sub-minute spans read "1m" so a live window never prints 0m.
|
|
12
|
+
*/
|
|
13
|
+
export function durationText(ms) {
|
|
14
|
+
if (!Number.isFinite(ms) || ms <= 0)
|
|
15
|
+
return "0m";
|
|
16
|
+
const totalMinutes = Math.floor(ms / MIN_MS);
|
|
17
|
+
const days = Math.floor(ms / DAY_MS);
|
|
18
|
+
const hours = Math.floor((ms % DAY_MS) / HOUR_MS);
|
|
19
|
+
if (days >= 1)
|
|
20
|
+
return `${days}d ${hours}h`;
|
|
21
|
+
if (hours >= 1)
|
|
22
|
+
return `${hours}h ${totalMinutes % 60}m`;
|
|
23
|
+
return `${Math.max(1, totalMinutes)}m`;
|
|
24
|
+
}
|
|
25
|
+
/** Duration from fractional days, for engine fields such as daysLeft. */
|
|
26
|
+
export function durationFromDays(days) {
|
|
27
|
+
return durationText(days * DAY_MS);
|
|
28
|
+
}
|
|
29
|
+
function weekday(when) {
|
|
30
|
+
return when.toLocaleDateString("en-US", { weekday: "short" });
|
|
31
|
+
}
|
|
32
|
+
function clock24(when) {
|
|
33
|
+
return when.toLocaleTimeString("en-US", { hour: "2-digit", minute: "2-digit", hourCycle: "h23" });
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Wall-clock instant: "Sat 14:46" within 7 days of `nowMs`, where the
|
|
37
|
+
* weekday is unambiguous, else "Sun 22 Nov 23:05". Without `nowMs` the
|
|
38
|
+
* dated form is used. Null when unparseable.
|
|
39
|
+
*/
|
|
40
|
+
export function clockText(iso, nowMs) {
|
|
41
|
+
const ms = iso ? Date.parse(iso) : NaN;
|
|
42
|
+
if (!Number.isFinite(ms))
|
|
43
|
+
return null;
|
|
44
|
+
const when = new Date(ms);
|
|
45
|
+
if (nowMs !== undefined && Number.isFinite(nowMs) && Math.abs(ms - nowMs) < 7 * DAY_MS) {
|
|
46
|
+
return `${weekday(when)} ${clock24(when)}`;
|
|
47
|
+
}
|
|
48
|
+
return `${weekday(when)} ${dateText(iso)} ${clock24(when)}`;
|
|
49
|
+
}
|
|
50
|
+
/** Calendar date without the weekday or clock, e.g. "22 Nov". Empty when unparseable. */
|
|
51
|
+
export function dateText(iso) {
|
|
52
|
+
const ms = iso ? Date.parse(iso) : NaN;
|
|
53
|
+
if (!Number.isFinite(ms))
|
|
54
|
+
return "";
|
|
55
|
+
const when = new Date(ms);
|
|
56
|
+
return `${when.getDate()} ${when.toLocaleDateString("en-US", { month: "short" })}`;
|
|
57
|
+
}
|
|
58
|
+
/** Weekday plus calendar date, e.g. "Sun 22 Nov". */
|
|
59
|
+
export function dayText(iso) {
|
|
60
|
+
const ms = iso ? Date.parse(iso) : NaN;
|
|
61
|
+
if (!Number.isFinite(ms))
|
|
62
|
+
return "Invalid Date";
|
|
63
|
+
return `${weekday(new Date(ms))} ${dateText(iso)}`;
|
|
64
|
+
}
|
|
65
|
+
/** The one waste sentence: "16% unused at reset (Fri 13:14)". */
|
|
66
|
+
export function unusedAtReset(wastePct, resetsAt, nowMs) {
|
|
67
|
+
const clock = clockText(resetsAt, nowMs);
|
|
68
|
+
return `${Math.round(wastePct)}% unused at reset${clock ? ` (${clock})` : ""}`;
|
|
69
|
+
}
|
package/dist/mcp/server.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import type { StateSnapshot } from "../advisory/types.js";
|
|
1
2
|
export declare const tools: ({
|
|
2
3
|
name: string;
|
|
3
4
|
description: string;
|
|
4
5
|
inputSchema: {
|
|
5
6
|
type: string;
|
|
6
7
|
properties: {
|
|
7
|
-
|
|
8
|
+
verbose?: undefined;
|
|
8
9
|
provider?: undefined;
|
|
9
10
|
};
|
|
10
11
|
required: never[];
|
|
@@ -15,9 +16,9 @@ export declare const tools: ({
|
|
|
15
16
|
inputSchema: {
|
|
16
17
|
type: string;
|
|
17
18
|
properties: {
|
|
18
|
-
|
|
19
|
+
verbose: {
|
|
19
20
|
type: string;
|
|
20
|
-
|
|
21
|
+
description: string;
|
|
21
22
|
};
|
|
22
23
|
provider?: undefined;
|
|
23
24
|
};
|
|
@@ -29,7 +30,7 @@ export declare const tools: ({
|
|
|
29
30
|
inputSchema: {
|
|
30
31
|
type: string;
|
|
31
32
|
properties: {
|
|
32
|
-
|
|
33
|
+
verbose?: undefined;
|
|
33
34
|
provider: {
|
|
34
35
|
type: string;
|
|
35
36
|
};
|
|
@@ -42,7 +43,7 @@ export declare const tools: ({
|
|
|
42
43
|
inputSchema: {
|
|
43
44
|
type: string;
|
|
44
45
|
properties: {
|
|
45
|
-
|
|
46
|
+
verbose?: undefined;
|
|
46
47
|
provider: {
|
|
47
48
|
type: string;
|
|
48
49
|
};
|
|
@@ -50,6 +51,25 @@ export declare const tools: ({
|
|
|
50
51
|
required: string[];
|
|
51
52
|
};
|
|
52
53
|
})[];
|
|
54
|
+
/**
|
|
55
|
+
* The default get_recommendation payload: the pick, its reason, and the
|
|
56
|
+
* other shown providers in recommended order with what an agent needs to
|
|
57
|
+
* choose. The full projection is behind `verbose: true`.
|
|
58
|
+
*/
|
|
59
|
+
export declare function compactRecommendation(snapshot: StateSnapshot, now: Date): {
|
|
60
|
+
use: string;
|
|
61
|
+
reason: string;
|
|
62
|
+
wastePct: number | null;
|
|
63
|
+
recommendationBasis: import("../advisory/types.js").RecommendationBasis;
|
|
64
|
+
bindingWindow: import("../advisory/types.js").BindingWindow;
|
|
65
|
+
alternatives: {
|
|
66
|
+
provider: string;
|
|
67
|
+
state: import("../format/rows.js").StateWord;
|
|
68
|
+
available: boolean;
|
|
69
|
+
resetsAt: string | null;
|
|
70
|
+
wait?: string | undefined;
|
|
71
|
+
}[];
|
|
72
|
+
};
|
|
53
73
|
export declare function handleTool(name: string, args: any): Promise<{
|
|
54
74
|
content: {
|
|
55
75
|
type: string;
|
package/dist/mcp/server.js
CHANGED
|
@@ -2,7 +2,8 @@ import { adapters } from "../adapters/index.js";
|
|
|
2
2
|
import { projectQuotasResponse, projectRecommendationResponse, } from "../advisory/snapshot.js";
|
|
3
3
|
import { validateForecastProvider, validateTask } from "../advisory/validate.js";
|
|
4
4
|
import { getDbPath, readConfig } from "../config.js";
|
|
5
|
-
import { forecastText, stateWord } from "../format/rows.js";
|
|
5
|
+
import { bindingResetsAt, forecastText, shownProviders, sortProviders, stateWord } from "../format/rows.js";
|
|
6
|
+
import { rateLimitWaits } from "../format/rate-limit-wait.js";
|
|
6
7
|
import { renderMarkdownTable, renderRecommendationSummary, renderModelsMarkdownTable } from "../format/markdown.js";
|
|
7
8
|
import { createServiceClientForBase, ServiceUnavailable } from "../runtime/client.js";
|
|
8
9
|
import { CATALOG_CLIENT_TIMEOUT_MS } from "../catalog/index.js";
|
|
@@ -10,7 +11,7 @@ import { VERSION } from "../version.js";
|
|
|
10
11
|
import { ClientError, OFFLINE_LABEL, resolveSnapshot, } from "../cli/snapshot-source.js";
|
|
11
12
|
export const tools = [
|
|
12
13
|
{ name: "get_quotas", description: "All quotas with resets and health", inputSchema: { type: "object", properties: {}, required: [] } },
|
|
13
|
-
{ name: "get_recommendation", description: "Which provider to use next (same advice for every task)", inputSchema: { type: "object", properties: {
|
|
14
|
+
{ name: "get_recommendation", description: "Which provider to use next (same advice for every task), plus each other provider's state, availability and reset. Also names a full 5h limit or a full week on a monthly plan, and when that limit clears. Pass verbose: true for every quota, advisory and model list.", inputSchema: { type: "object", properties: { verbose: { type: "boolean", description: "Return the full recommendation payload (all quotas, advisories and model lists)" } } } },
|
|
14
15
|
{ name: "get_models", description: "Currently listed models per quota bucket (inventory, not a task ranking)", inputSchema: { type: "object", properties: { provider: { type: "string" } } } },
|
|
15
16
|
{ name: "forecast", description: "Burn (24h + window avg) vs ideal + waste for a provider", inputSchema: { type: "object", properties: { provider: { type: "string" } }, required: ["provider"] } },
|
|
16
17
|
];
|
|
@@ -38,6 +39,34 @@ async function resolveState() {
|
|
|
38
39
|
throw e;
|
|
39
40
|
}
|
|
40
41
|
}
|
|
42
|
+
const UNAVAILABLE = new Set(["Quota exhausted", "Monthly exhausted", "Not reporting"]);
|
|
43
|
+
/**
|
|
44
|
+
* The default get_recommendation payload: the pick, its reason, and the
|
|
45
|
+
* other shown providers in recommended order with what an agent needs to
|
|
46
|
+
* choose. The full projection is behind `verbose: true`.
|
|
47
|
+
*/
|
|
48
|
+
export function compactRecommendation(snapshot, now) {
|
|
49
|
+
const rec = snapshot.recommendation;
|
|
50
|
+
const others = sortProviders(shownProviders(snapshot.providers), "recommended", rec.use).filter((p) => p.id !== rec.use);
|
|
51
|
+
return {
|
|
52
|
+
use: rec.use,
|
|
53
|
+
reason: rec.reason,
|
|
54
|
+
wastePct: rec.wastePct,
|
|
55
|
+
recommendationBasis: rec.recommendationBasis,
|
|
56
|
+
bindingWindow: rec.bindingWindow,
|
|
57
|
+
alternatives: others.map((p) => {
|
|
58
|
+
const state = stateWord(p);
|
|
59
|
+
const waits = rateLimitWaits(p, now);
|
|
60
|
+
return {
|
|
61
|
+
provider: p.id,
|
|
62
|
+
state,
|
|
63
|
+
available: !UNAVAILABLE.has(state) && waits.length === 0,
|
|
64
|
+
resetsAt: bindingResetsAt(p),
|
|
65
|
+
...(waits.length > 0 ? { wait: waits.map((w) => w.text).join(", ") } : {}),
|
|
66
|
+
};
|
|
67
|
+
}),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
41
70
|
function rejectMissingProvider(args) {
|
|
42
71
|
try {
|
|
43
72
|
validateForecastProvider(args?.provider, { registered: [], storedIds: [] });
|
|
@@ -63,14 +92,19 @@ export async function handleTool(name, args) {
|
|
|
63
92
|
};
|
|
64
93
|
}
|
|
65
94
|
if (name === "get_recommendation") {
|
|
95
|
+
// task is no longer advertised; old clients that send it still validate.
|
|
66
96
|
const task = validateTask(args?.task ?? "any");
|
|
67
97
|
const { snapshot, source } = await resolveState();
|
|
68
98
|
if (source === "offline")
|
|
69
99
|
console.error(OFFLINE_LABEL);
|
|
100
|
+
const now = new Date();
|
|
101
|
+
const body = args?.verbose === true
|
|
102
|
+
? projectRecommendationResponse(snapshot, task)
|
|
103
|
+
: compactRecommendation(snapshot, now);
|
|
70
104
|
return {
|
|
71
105
|
content: [
|
|
72
|
-
{ type: "text", text: renderRecommendationSummary(snapshot) },
|
|
73
|
-
{ type: "text", text: JSON.stringify(
|
|
106
|
+
{ type: "text", text: renderRecommendationSummary(snapshot, now) },
|
|
107
|
+
{ type: "text", text: JSON.stringify(body, null, 2) },
|
|
74
108
|
],
|
|
75
109
|
};
|
|
76
110
|
}
|
package/dist/store/quotas.d.ts
CHANGED
|
@@ -8,7 +8,11 @@ export declare function isWeeklyCycleBreak(prev: {
|
|
|
8
8
|
resetsAt: string | null;
|
|
9
9
|
}): boolean;
|
|
10
10
|
export declare function upsertQuota(db: any, q: any): void;
|
|
11
|
-
|
|
11
|
+
interface CloseWindow {
|
|
12
|
+
resetsAt: string | null;
|
|
13
|
+
resetsAtEstimated?: boolean;
|
|
14
|
+
}
|
|
15
|
+
export declare function getWindowCloses(db: any, provider: string, limit?: number, window?: "weekly" | "monthly", current?: CloseWindow | null): Array<{
|
|
12
16
|
provider: string;
|
|
13
17
|
plan: string | null;
|
|
14
18
|
usedPct: number;
|
|
@@ -29,4 +33,5 @@ export declare function getExhaustedTimestamps(db: any, now?: number): {
|
|
|
29
33
|
weekly: Map<string, string>;
|
|
30
34
|
monthly: Map<string, string>;
|
|
31
35
|
};
|
|
32
|
-
export declare function getHistoryBaseline(db: any, limit?: number): Map<string, number>;
|
|
36
|
+
export declare function getHistoryBaseline(db: any, limit?: number, current?: Map<string, CloseWindow>): Map<string, number>;
|
|
37
|
+
export {};
|
package/dist/store/quotas.js
CHANGED
|
@@ -55,7 +55,9 @@ function mapRow(row) {
|
|
|
55
55
|
function cycleBreakReason(prev, cur) {
|
|
56
56
|
const dropped = cur.usedPct < prev.usedPct;
|
|
57
57
|
const prevResets = prev.resetsAt ? new Date(prev.resetsAt).getTime() : NaN;
|
|
58
|
-
|
|
58
|
+
// Instants, not strings: one reset written as "+10:00" and as "Z" is one boundary.
|
|
59
|
+
const curResets = cur.resetsAt ? new Date(cur.resetsAt).getTime() : NaN;
|
|
60
|
+
const boundaryRolled = !Number.isNaN(prevResets) && prevResets <= cur.t && prevResets !== curResets;
|
|
59
61
|
if (dropped && boundaryRolled)
|
|
60
62
|
return "both";
|
|
61
63
|
if (dropped)
|
|
@@ -67,12 +69,28 @@ function cycleBreakReason(prev, cur) {
|
|
|
67
69
|
export function isWeeklyCycleBreak(prev, cur) {
|
|
68
70
|
return cycleBreakReason(prev, cur) !== null;
|
|
69
71
|
}
|
|
72
|
+
// One timestamp format on the wire: adapters report offsets as their
|
|
73
|
+
// source prints them ("+10:00", "Z"), so ingest stores UTC ISO.
|
|
74
|
+
function utcIso(v) {
|
|
75
|
+
if (typeof v !== "string")
|
|
76
|
+
return v;
|
|
77
|
+
const ms = Date.parse(v);
|
|
78
|
+
return Number.isFinite(ms) ? new Date(ms).toISOString() : v;
|
|
79
|
+
}
|
|
70
80
|
export function upsertQuota(db, q) {
|
|
71
81
|
if (Array.isArray(q)) {
|
|
72
82
|
for (const item of q)
|
|
73
83
|
upsertQuota(db, item);
|
|
74
84
|
return;
|
|
75
85
|
}
|
|
86
|
+
q = {
|
|
87
|
+
...q,
|
|
88
|
+
resetsAt: utcIso(q.resetsAt),
|
|
89
|
+
periodStart: utcIso(q.periodStart),
|
|
90
|
+
fetchedAt: utcIso(q.fetchedAt),
|
|
91
|
+
monthlyResetsAt: utcIso(q.monthlyResetsAt),
|
|
92
|
+
fiveHourResetsAt: utcIso(q.fiveHourResetsAt),
|
|
93
|
+
};
|
|
76
94
|
const credits = q.creditsUsd ?? q.credits_usd ?? null;
|
|
77
95
|
const estimated = q.resetsAtEstimated ? 1 : null;
|
|
78
96
|
// Either spelling binds; an explicit null stays null (SQLite binds it),
|
|
@@ -102,6 +120,51 @@ export function upsertQuota(db, q) {
|
|
|
102
120
|
throw e;
|
|
103
121
|
}
|
|
104
122
|
}
|
|
123
|
+
const DAY_MS = 86_400_000;
|
|
124
|
+
// Verified resets this close together name one window: claude reports the
|
|
125
|
+
// same reset as 20:59 and 21:00.
|
|
126
|
+
const SAME_WINDOW_MS = 60 * 60 * 1000;
|
|
127
|
+
// A close that ran under a day says too little about a week's pace.
|
|
128
|
+
const MIN_CLOSE_SPAN_DAYS = 1;
|
|
129
|
+
function sameVerifiedWindow(a, b) {
|
|
130
|
+
if (a.resetsAtEstimated || b.resetsAtEstimated || !a.resetsAt || !b.resetsAt)
|
|
131
|
+
return false;
|
|
132
|
+
const aMs = Date.parse(a.resetsAt);
|
|
133
|
+
const bMs = Date.parse(b.resetsAt);
|
|
134
|
+
return Number.isFinite(aMs) && Number.isFinite(bMs) && Math.abs(aMs - bMs) <= SAME_WINDOW_MS;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Finished windows only, one row each. A usage drop inside the window still
|
|
138
|
+
* open is not a close, and a window that dropped more than once keeps its
|
|
139
|
+
* newest row. Estimated resets pass through: their boundaries move per poll.
|
|
140
|
+
*/
|
|
141
|
+
function settleCloses(newestFirst, current) {
|
|
142
|
+
const out = [];
|
|
143
|
+
for (const c of newestFirst) {
|
|
144
|
+
if (current && sameVerifiedWindow(c, current))
|
|
145
|
+
continue;
|
|
146
|
+
if (out.some((kept) => sameVerifiedWindow(kept, c)))
|
|
147
|
+
continue;
|
|
148
|
+
out.push(c);
|
|
149
|
+
}
|
|
150
|
+
return out;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Daily pace over the days a close actually ran: a window the provider reset
|
|
154
|
+
* early, or one last sampled days before its reset, covers fewer than seven.
|
|
155
|
+
* Null under MIN_CLOSE_SPAN_DAYS. Estimated or undated closes keep used / 7.
|
|
156
|
+
*/
|
|
157
|
+
function closeDailyRate(c) {
|
|
158
|
+
const start = c.periodStart ? Date.parse(c.periodStart) : NaN;
|
|
159
|
+
const end = c.resetsAt ? Date.parse(c.resetsAt) : NaN;
|
|
160
|
+
const sampled = Date.parse(c.sampledAt);
|
|
161
|
+
if (c.resetsAtEstimated || !Number.isFinite(start) || !Number.isFinite(end) || !Number.isFinite(sampled)) {
|
|
162
|
+
return c.usedPct / 7;
|
|
163
|
+
}
|
|
164
|
+
const ranUntil = end - sampled <= SAME_WINDOW_MS ? end : Math.min(sampled, end);
|
|
165
|
+
const days = (ranUntil - start) / DAY_MS;
|
|
166
|
+
return days >= MIN_CLOSE_SPAN_DAYS ? c.usedPct / days : null;
|
|
167
|
+
}
|
|
105
168
|
function monthlyPeriodStart(resetIso) {
|
|
106
169
|
if (!resetIso)
|
|
107
170
|
return null;
|
|
@@ -112,7 +175,7 @@ function monthlyPeriodStart(resetIso) {
|
|
|
112
175
|
start.setMonth(start.getMonth() - 1);
|
|
113
176
|
return start.toISOString();
|
|
114
177
|
}
|
|
115
|
-
export function getWindowCloses(db, provider, limit = 4, window = "weekly") {
|
|
178
|
+
export function getWindowCloses(db, provider, limit = 4, window = "weekly", current) {
|
|
116
179
|
if (window === "monthly") {
|
|
117
180
|
let qRows = [];
|
|
118
181
|
try {
|
|
@@ -164,7 +227,7 @@ export function getWindowCloses(db, provider, limit = 4, window = "weekly") {
|
|
|
164
227
|
}
|
|
165
228
|
let rows = [];
|
|
166
229
|
try {
|
|
167
|
-
rows = db.prepare(`SELECT provider, plan, used_pct, leftover_pct, sampled_at, period_start, resets_at, resets_at_estimated, detected_at, reason FROM window_closes WHERE provider=? ORDER BY detected_at DESC, id DESC
|
|
230
|
+
rows = db.prepare(`SELECT provider, plan, used_pct, leftover_pct, sampled_at, period_start, resets_at, resets_at_estimated, detected_at, reason FROM window_closes WHERE provider=? ORDER BY detected_at DESC, id DESC`).all(provider);
|
|
168
231
|
}
|
|
169
232
|
catch (e) {
|
|
170
233
|
// A pre-observability database read offline is never migrated, so the
|
|
@@ -172,7 +235,7 @@ export function getWindowCloses(db, provider, limit = 4, window = "weekly") {
|
|
|
172
235
|
if (!/no such table/i.test(String(e?.message ?? "")))
|
|
173
236
|
throw e;
|
|
174
237
|
}
|
|
175
|
-
|
|
238
|
+
const mapped = rows.map((r) => ({
|
|
176
239
|
provider: r.provider,
|
|
177
240
|
plan: r.plan ?? null,
|
|
178
241
|
usedPct: r.used_pct,
|
|
@@ -184,6 +247,7 @@ export function getWindowCloses(db, provider, limit = 4, window = "weekly") {
|
|
|
184
247
|
detectedAt: r.detected_at,
|
|
185
248
|
reason: r.reason,
|
|
186
249
|
}));
|
|
250
|
+
return settleCloses(mapped, current).slice(0, limit);
|
|
187
251
|
}
|
|
188
252
|
export function getLatestByProvider(db, provider) {
|
|
189
253
|
const row = db.prepare(`SELECT * FROM quotas WHERE provider=? ORDER BY fetched_at DESC LIMIT 1`).get(provider);
|
|
@@ -213,21 +277,28 @@ function currentCycle(sorted) {
|
|
|
213
277
|
}
|
|
214
278
|
export function getBurnRates(db, now = Date.now()) {
|
|
215
279
|
// Burn is the used-pct delta over a real rolling window of poll history
|
|
216
|
-
// (up to 24h) within the current cycle.
|
|
217
|
-
|
|
280
|
+
// (up to 24h) within the current cycle. Readings taken before the latest
|
|
281
|
+
// window opened are dropped: a rolling window that opens on first use
|
|
282
|
+
// (Codex) would otherwise price its first hours over idle time from before
|
|
283
|
+
// it existed. An estimated reset derives periodStart from the poll time,
|
|
284
|
+
// so the drop applies to verified clocks only.
|
|
285
|
+
const rows = db.prepare(`SELECT provider, used_pct, fetched_at, resets_at, period_start, resets_at_estimated FROM quotas`).all();
|
|
218
286
|
const byProvider = new Map();
|
|
219
287
|
for (const r of rows) {
|
|
220
288
|
const t = new Date(r.fetched_at).getTime();
|
|
221
289
|
if (Number.isNaN(t) || t > now)
|
|
222
290
|
continue;
|
|
223
291
|
const pts = byProvider.get(r.provider) ?? [];
|
|
224
|
-
pts.push({ usedPct: r.used_pct, t, resetsAt: r.resets_at });
|
|
292
|
+
pts.push({ usedPct: r.used_pct, t, resetsAt: r.resets_at, periodStart: r.period_start, estimated: !!r.resets_at_estimated });
|
|
225
293
|
byProvider.set(r.provider, pts);
|
|
226
294
|
}
|
|
227
295
|
const out = new Map();
|
|
228
296
|
for (const [provider, all] of byProvider) {
|
|
229
|
-
|
|
297
|
+
let sorted = currentCycle(all.sort((a, b) => a.t - b.t));
|
|
230
298
|
const latest = sorted[sorted.length - 1];
|
|
299
|
+
const opened = latest.estimated || !latest.periodStart ? NaN : Date.parse(latest.periodStart);
|
|
300
|
+
if (Number.isFinite(opened))
|
|
301
|
+
sorted = sorted.filter((p) => p.t >= opened || p === latest);
|
|
231
302
|
const cutoff = latest.t - 86400000;
|
|
232
303
|
const windowStart = sorted.find((p) => p.t >= cutoff) ?? sorted[0];
|
|
233
304
|
const days = (latest.t - windowStart.t) / 86400000;
|
|
@@ -291,10 +362,10 @@ export function getExhaustedTimestamps(db, now = Date.now()) {
|
|
|
291
362
|
}
|
|
292
363
|
return { weekly, monthly };
|
|
293
364
|
}
|
|
294
|
-
export function getHistoryBaseline(db, limit = 4) {
|
|
365
|
+
export function getHistoryBaseline(db, limit = 4, current) {
|
|
295
366
|
let rows = [];
|
|
296
367
|
try {
|
|
297
|
-
rows = db.prepare(`SELECT provider, used_pct, resets_at_estimated FROM window_closes ORDER BY detected_at DESC, id DESC`).all();
|
|
368
|
+
rows = db.prepare(`SELECT provider, used_pct, sampled_at, period_start, resets_at, resets_at_estimated FROM window_closes ORDER BY detected_at DESC, id DESC`).all();
|
|
298
369
|
}
|
|
299
370
|
catch (e) {
|
|
300
371
|
// A pre-observability database read offline is never migrated, so the
|
|
@@ -305,18 +376,26 @@ export function getHistoryBaseline(db, limit = 4) {
|
|
|
305
376
|
const byProvider = new Map();
|
|
306
377
|
for (const r of rows) {
|
|
307
378
|
const closes = byProvider.get(r.provider) ?? [];
|
|
308
|
-
closes.push({
|
|
379
|
+
closes.push({
|
|
380
|
+
usedPct: r.used_pct,
|
|
381
|
+
sampledAt: r.sampled_at,
|
|
382
|
+
periodStart: r.period_start ?? null,
|
|
383
|
+
resetsAt: r.resets_at ?? null,
|
|
384
|
+
resetsAtEstimated: !!r.resets_at_estimated,
|
|
385
|
+
});
|
|
309
386
|
byProvider.set(r.provider, closes);
|
|
310
387
|
}
|
|
311
388
|
const out = new Map();
|
|
312
389
|
for (const [provider, newestFirst] of byProvider) {
|
|
313
|
-
const pool = newestFirst.slice(0, limit);
|
|
390
|
+
const pool = settleCloses(newestFirst, current?.get(provider)).slice(0, limit);
|
|
314
391
|
if (!pool.length)
|
|
315
392
|
continue;
|
|
316
|
-
const verified = pool.filter((c) => c.
|
|
393
|
+
const verified = pool.filter((c) => !c.resetsAtEstimated);
|
|
317
394
|
const chosen = verified.length ? verified : pool;
|
|
318
|
-
const
|
|
319
|
-
|
|
395
|
+
const rates = chosen.map(closeDailyRate).filter((r) => r !== null);
|
|
396
|
+
if (!rates.length)
|
|
397
|
+
continue;
|
|
398
|
+
const rate = rates.reduce((sum, r) => sum + r, 0) / rates.length;
|
|
320
399
|
if (Number.isFinite(rate))
|
|
321
400
|
out.set(provider, rate);
|
|
322
401
|
}
|
package/dist/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// generated by scripts/build-embed.mjs — do not edit
|
|
2
|
-
export const VERSION = "0.0.
|
|
2
|
+
export const VERSION = "0.0.59";
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
-
import type { ProviderView, RecommendationView } from "../state.js";
|
|
2
|
+
import type { AdvisoryView, ProviderView, RecommendationView } from "../state.js";
|
|
3
|
+
/** Names the rate the forecast is priced at, matching the engine's blend inputs. */
|
|
4
|
+
export declare function forecastBasisWords(advisory: AdvisoryView | null | undefined): string;
|
|
3
5
|
export declare function AdviceDrawer({ open, recommendation, providers, asOf, onClose, onSelectProvider, }: {
|
|
4
6
|
open: boolean;
|
|
5
7
|
recommendation: RecommendationView;
|