A provider can have up to 8 rate windows: days, a span that may cross midnight (on this computer's clock), and a rate. Inside a window a call costs the base price times the rate. The base is the user's model price as set, or the list price times PriceRate with the vendor's own peak (DeepSeek, Command Code, OpenCode Go) already in it. The author gave this order as the spec: a 0.8 window on a shared DeepSeek key takes 0.8 off the peak price at peak. The editor's hint says not to set the official peak hours again as a window, in every language. Each call is priced at its own time on the Usage page and its CSV, in the Requests tab's rows, and against a gateway key's cost limit. The Sessions page, group templates and the model list written into an agent's own config use the base price. /v1/models prices at now. Ported from YangHonghang's 2c9d459ff (rate-windows-2463-v2). The Go pricing, its tests and the translations are theirs. Changed here: - No budget raised. Their commit raised appScriptBudget and coreScriptBudget; core_budget_test.go is main's again. The editor's window rows, time menus, validation and hint moved to ratewindows.js, which app.js imports when the field is drawn (as signsheets.js is). The sliding-thumb and modalDepth comments were cut down to make room. The core is 1,535,528 of 1,536,586 and app.js 1,312,242 of 1,315,316. Until the script has loaded, a save leaves rateWindows out, and the server keeps the saved ones. - Their unrelated TestAssetsHaveNoConflictMarkers is dropped (one commit, one goal). - The row badge shows only for a provider with windows. Before, every provider with a price rate alone got a new ×badge. Its title is shorter, and so are its translations. - AgentPricer (agent model files: Pi, omp, OpenCode cost) uses the base price. Nothing rewrites those files when a window opens or closes, so a price taken at Sync stayed on outside the window. - internal/budget gets a TestMain through testenv, so the new test runs in a home of its own. - old-webkit's list of lazily loaded scripts names ratewindows.js. - The draft keeps each window as a shallow copy, and a day chip replaces its days array, so the saved provider is never changed. - The GUI test waits for the lazy script's hint, not a fixed 300ms. It also checks the peak note in each language. Run first on2f5a3f1c0, then again after rebasing on70d9996c5,8acb0d3a7andd609729cf. The rebase onto70d9996c5met main's ruleedit.js in old-webkit's list, and both are listed. - gofmt -l internal: clean at8acb0d3a7(129578c3dgofmt'd main's pi_api_test.go). - mote, on8acb0d3a7: go vet on usage, provider and gui ok; GOOS=linux and GOOS=windows go vet -tags nogui ./... ok. go test -tags nogui for internal/usage, provider, gui, budget and sessions: ok. The five internal/agent tests that are red on WSL (below) pass on mote there. Ond609729cf(a one-line mover min in internal/provider), internal/provider again: ok. - On2f5a3f1c0: internal/provider, usage, budget and sessions, and the gui RateNow|PriceRate|CoreScript|Budget tests, each under TZ=UTC, Asia/Shanghai and America/Los_Angeles: all ok. - wsl-run (magpie-desktop, Playwright chromium and webkit) on70d9996c5: provider-rate-windows, old-webkit, i18n-keys, provider-price-rate, provider-advanced-fold, agent-hides-editor, key-box-once, provider-many-keys, nokey-editor and editor-short-window: 200/200. On2f5a3f1c0the first five and model-price-edit: 132/132. On mote the same files hit WebKit launch timeouts (180s) under another session's full GUI suite (load 30), and that run was stopped. - Full suite, wsl-run (mote was running another session's full suite), go test -tags nogui -count=1 -timeout 45m ./... at 6e534f546 (this change on70d9996c5): every package ok except internal/agent. internal/agent fails TestInstalledVersion, TestGrokInstalled, TestCursorFolderFromTheEditorIsNotTheCLI, TestOmpDetectedWithPiDir and TestZedAppPath. This change doesn't touch internal/agent. At origin/main8acb0d3a7on the same box, TestInstalledVersion, TestGrokInstalled and TestOmpDetectedWithPiDir fail too: a fake CLI in /tmp exec'd with "Invalid argument". That is #4033 and #3548 (WSL2 binfmt interop). - Broken by hand, red each time: - Pricer without the window: TestPricerRateWindows, TestBlockRowsTakeTheirRateWindow, TestCostLimitCountsTheRateWindow. - Block rows without atWindow: TestBlockRowsTakeTheirRateWindow. - AgentPricer at now: TestAgentPricerLeavesTheRateWindowOut. - Badge for a price rate alone: TestRateNowOf. - Peak note removed: provider-rate-windows, 10 of 10 chromium layouts. - ratewindows.js not in old-webkit's list: old-webkit. Not tried: - A real relay billed by the hour. - The real desktop app (WebKitGTK, WKWebView). - The full GUI suite (mote was running another session's). Known limits: - No rate is stored per record, so changing the windows reprices history, as a changed PriceRate does. - Windows follow this computer's zone, with no zone of their own. - The Sessions page uses the base price. Co-authored-by: YangHonghang <honghang.yang@qq.com>
124 lines
5.8 KiB
JavaScript
124 lines
5.8 KiB
JavaScript
// A provider's rate windows (#2463) in its editor: the hours it bills at
|
||
// its own rate, a peak at 2x or a discount off peak. app.js imports this
|
||
// when an editor draws the field (rateWindowsField), so it weighs nothing
|
||
// on the core. Each window is its days (none ticked: every day), a span
|
||
// that may cross midnight, and the rate the price is times inside it, on
|
||
// top of the price rate; the first window holding a call wins.
|
||
|
||
// rateWindowsPaint draws d's windows into box, and Add a window under
|
||
// them while there is room for one more.
|
||
window.rateWindowsPaint = function paint(box, d) {
|
||
box.replaceChildren();
|
||
if (!box.nextElementSibling) {
|
||
const help = el("div", "hint", t("Inside a window a call costs the official price times its rate, on top of the price rate; outside every window the price rate alone counts. The first window that holds a call wins. Times are on the clock of the computer magpie runs on, and 22:00–6:00 crosses midnight. No day ticked is every day. A price you set for a model is scaled by the window too. Changing the windows prices the calls already counted again, as a changed price rate does."));
|
||
// a list price with the vendor's own peak has it already (#2463,
|
||
// the author): a window over the same hours would double it again
|
||
help.append(el("div", "", t("DeepSeek's own API, Command Code and OpenCode Go already count their DeepSeek models' peak hours at double: don't add those hours as a window, or they count twice.")));
|
||
help.id = "rate-windows-help";
|
||
box.after(help);
|
||
box.setAttribute("role", "group");
|
||
box.setAttribute("aria-describedby", help.id);
|
||
}
|
||
for (const w of d.rateWindows || []) box.append(rateWindowRow(w, d, () => paint(box, d)));
|
||
if ((d.rateWindows || []).length < 8) {
|
||
const add = el("button", "text", t("Add a window"));
|
||
add.type = "button";
|
||
add.onclick = () => { (d.rateWindows ??= []).push({ days: [], start: "", end: "", rate: "" }); paint(box, d); };
|
||
box.append(add);
|
||
}
|
||
};
|
||
|
||
// rateWindowRow is one window of the editor, its draft w kept as typed.
|
||
function rateWindowRow(w, d, repaint) {
|
||
const row = el("div", "rate-win"), chips = el("div", "day-chips");
|
||
const names = [t("Sun"), t("Mon"), t("Tue"), t("Wed"), t("Thu"), t("Fri"), t("Sat")];
|
||
for (let i = 0; i < 7; i++) {
|
||
// a new array each press: the draft's window is a shallow copy of the
|
||
// saved one, which keeps its own days
|
||
const mark = () => {
|
||
const on = (w.days || []).includes(i);
|
||
b.classList.toggle("on", on);
|
||
b.setAttribute("aria-pressed", String(on));
|
||
};
|
||
const b = el("button", "day-chip", names[i]);
|
||
b.type = "button";
|
||
mark();
|
||
b.onclick = () => {
|
||
const days = (w.days || []).filter((x) => x !== i);
|
||
w.days = days.length < (w.days || []).length ? days : [...days, i].sort((a, z) => a - z);
|
||
mark();
|
||
};
|
||
chips.append(b);
|
||
}
|
||
const rate = input(w.rate, "3");
|
||
rate.classList.add("rate-rate");
|
||
rate.inputMode = "decimal";
|
||
rate.setAttribute("aria-label", t("Rate in this window"));
|
||
rate.oninput = () => { w.rate = rate.value; };
|
||
const del = el("button", "text rate-win-del", "×");
|
||
del.type = "button";
|
||
del.title = t("Remove this window");
|
||
del.setAttribute("aria-label", del.title);
|
||
del.onclick = () => {
|
||
const at = (d.rateWindows || []).indexOf(w);
|
||
if (at >= 0) d.rateWindows.splice(at, 1);
|
||
repaint();
|
||
};
|
||
row.append(chips, timePick(w, "start", t("Window starts at")), el("span", "rate-dash", "–"),
|
||
timePick(w, "end", t("Window ends at")), rate, del);
|
||
return row;
|
||
}
|
||
|
||
// timePick is a window's start or end as the app's own menu of half
|
||
// hours, not typed; only an end may be 24:00.
|
||
function timePick(w, key, label) {
|
||
const b = el("button", "sess-pick rate-time");
|
||
b.type = "button";
|
||
b.setAttribute("aria-haspopup", "menu");
|
||
b.setAttribute("aria-expanded", "false");
|
||
b.setAttribute("aria-label", label);
|
||
const paint = () => {
|
||
b.dataset.value = w[key] || "";
|
||
b.replaceChildren(el("span", "", w[key] || "--:--"), svg(CHEV, 11, 1.6));
|
||
};
|
||
b.onclick = (e) => {
|
||
e.preventDefault();
|
||
e.stopPropagation();
|
||
if (b.classList.contains("open")) return closeProtoMenu();
|
||
const times = [];
|
||
for (let m = 0; m < 1440; m += 30) times.push(String(Math.floor(m / 60)).padStart(2, "0") + ":" + String(m % 60).padStart(2, "0"));
|
||
if (key === "end") times.push("24:00");
|
||
openProtoMenu(b, times.map((v) => ({ v, name: v, note: "", literalName: true })), w[key], (v) => { w[key] = v; paint(); }, label, "sess-menu");
|
||
};
|
||
paint();
|
||
return b;
|
||
}
|
||
|
||
// clockOf reads "H:MM" or "HH:MM", 24:00 included; null for what isn't one.
|
||
function clockOf(v) {
|
||
const m = String(v ?? "").trim().match(/^(\d{1,2}):(\d{2})$/);
|
||
if (!m) return null;
|
||
const min = +m[1] * 60 + +m[2];
|
||
return +m[1] > 24 || +m[2] > 59 || min > 1440 ? null : min;
|
||
}
|
||
|
||
// rateWindowsOfDraft is the draft's windows as saved, null for none, or
|
||
// false when one is typed wrong (provider.CheckRateWindows' rules). Not
|
||
// loaded yet, the save leaves them out, which keeps them as they are.
|
||
window.rateWindowsOfDraft = function () {
|
||
const out = [];
|
||
for (const w of draft.rateWindows || []) {
|
||
const start = clockOf(w.start), end = clockOf(w.end);
|
||
if (start === null || start === 1440 || end === null || start === end) return false;
|
||
const n = decimalOf(String(w.rate ?? "").trim());
|
||
if (!(n > 0 && n <= 1000) || Math.abs(n * 1000 - Math.round(n * 1000)) > 1e-6) return false;
|
||
out.push({ days: [...(w.days || [])], start: String(w.start).trim(), end: String(w.end).trim(), rate: n });
|
||
}
|
||
return out.length ? out : null;
|
||
};
|
||
|
||
window.rateWindowsError = function (ed) {
|
||
shown(ed.querySelector(".rate-windows input"))?.focus({ preventScroll: true });
|
||
return editorError(t("Rate windows: every window is days, a time like 09:00, another time for it to end, and a rate from 0 to 1000 with at most three decimals"), "warn");
|
||
};
|