Attachments upload like the web composer's (signed URL, then bytes) and ride on the next message or a draft's first message; pending names show above the composer and removal deletes the unsent upload. Settings shows provider limit windows, banked reset credits and the last 30 days' usage summary, with a status-line note near a limit and a confirmed reset-credit command. Automations can be created and edited in $EDITOR from a template parsed back against the web form's rules, and deleted behind a confirm. Asks for the diagnostics:read and providers:manage scopes the new RPCs need.
88 lines
5.0 KiB
TypeScript
88 lines
5.0 KiB
TypeScript
import type { ProviderConsumeResetCreditInput, ServerConfig, ServerProviderResetCredits, UsageSummary } from "@t3tools/contracts";
|
|
import { collectLimitAccounts, collectLimitNotices, formatDuration, remainingPercent, type LimitPresentations } from "@t3tools/shared/usageLimits";
|
|
import { formatTokens, formatUsd } from "@t3tools/shared/usageFormat";
|
|
|
|
/** At or above this much of a window used, the status line calls the limit near. */
|
|
export const NEAR_LIMIT_USED_PERCENT = 90;
|
|
|
|
export interface UsageLimitInfo {
|
|
/** One line per account and window, for the settings view. */
|
|
readonly lines: readonly { label: string; value: string }[];
|
|
/** The worst near-or-reached window, for the status line; null when every window has room. */
|
|
readonly note: string | null;
|
|
/** Accounts with a banked reset credit the palette can spend. */
|
|
readonly credits: readonly { label: string; input: ProviderConsumeResetCreditInput; credits: ServerProviderResetCredits }[];
|
|
}
|
|
|
|
/** Provider limits from the server config, collected the way the web's usage page collects them. */
|
|
export function usageLimits(config: ServerConfig | null, now = Date.now()): UsageLimitInfo {
|
|
if (!config) return { lines: [], note: null, credits: [] };
|
|
const presentations: LimitPresentations = new Map([
|
|
[config.environment.environmentId, { entry: { target: { label: config.environment.label } }, serverConfig: config }],
|
|
]);
|
|
const lines: { label: string; value: string }[] = [];
|
|
const credits: UsageLimitInfo["credits"][number][] = [];
|
|
let note: { used: number; text: string } | null = null;
|
|
for (const account of collectLimitAccounts(presentations)) {
|
|
const label = account.displayName ?? account.email ?? account.driver;
|
|
const heading = [label, account.plan, account.sourceLabel].filter(Boolean).join(" · ");
|
|
for (const window of account.limits.windows) {
|
|
const left = remainingPercent(window);
|
|
const resets = window.resetsAt && Date.parse(window.resetsAt) > now ? ` · resets in ${formatDuration(Date.parse(window.resetsAt) - now)}` : "";
|
|
lines.push({ label: `${heading} ${window.label}`, value: `${left}% left${resets}` });
|
|
if (window.usedPercent >= NEAR_LIMIT_USED_PERCENT && (!note || window.usedPercent > note.used)) {
|
|
note = { used: window.usedPercent, text: `${label} ${window.label}: ${left === 0 ? "limit reached" : `${left}% left`}${resets}` };
|
|
}
|
|
}
|
|
const banked = account.limits.resetCredits;
|
|
if (banked && banked.availableCount > 0) {
|
|
lines.push({ label: `${heading} reset credits`, value: resetCreditsSummary(banked, now) });
|
|
if (account.redeem) credits.push({ label: heading, input: account.redeem.input, credits: banked });
|
|
}
|
|
}
|
|
for (const notice of collectLimitNotices(presentations)) lines.push({ label: "Limits", value: notice });
|
|
return { lines, note: note?.text ?? null, credits };
|
|
}
|
|
|
|
/** The web's banked-credits wording: a count and when the next one expires. */
|
|
export function resetCreditsSummary(credits: ServerProviderResetCredits, now = Date.now()): string {
|
|
if (credits.availableCount === 0) return "No reset credits banked";
|
|
const expires = credits.nextExpiresAt ? ` · next expires in ${formatDuration(Date.parse(credits.nextExpiresAt) - now)}` : "";
|
|
return `${credits.availableCount} ${credits.availableCount === 1 ? "reset credit" : "reset credits"} banked${expires}`;
|
|
}
|
|
|
|
const RESET_CREDIT_OUTCOMES: Record<string, string> = {
|
|
reset: "Reset applied. Your windows have cleared.",
|
|
nothingToReset: "Nothing to reset right now.",
|
|
noCredit: "No reset credit left.",
|
|
alreadyRedeemed: "That credit was already redeemed.",
|
|
};
|
|
|
|
/** What spending a reset credit says afterwards, as the web reports it. */
|
|
export function resetCreditOutcome(result: { readonly outcome: string; readonly warning?: string | undefined }): string {
|
|
return result.warning ?? RESET_CREDIT_OUTCOMES[result.outcome] ?? result.outcome;
|
|
}
|
|
|
|
/** The usage summary's totals for the settings view: cost and tokens, then the priciest providers. */
|
|
export function usageSummaryLines(summary: UsageSummary | null): { label: string; value: string }[] {
|
|
if (!summary) return [];
|
|
const byProvider = new Map<string, { cost: number; tokens: number }>();
|
|
let cost = 0;
|
|
let tokens = 0;
|
|
for (const bucket of summary.buckets) {
|
|
const total = bucket.totals.uncachedInputTokens + bucket.totals.cachedInputTokens + bucket.totals.cacheCreationTokens + bucket.totals.outputTokens;
|
|
cost += bucket.costUsd;
|
|
tokens += total;
|
|
const entry = byProvider.get(bucket.provider) ?? { cost: 0, tokens: 0 };
|
|
entry.cost += bucket.costUsd;
|
|
entry.tokens += total;
|
|
byProvider.set(bucket.provider, entry);
|
|
}
|
|
const sessions = summary.sources.reduce((sum, source) => sum + source.distinctSessions, 0);
|
|
const lines = [{ label: "Last 30 days", value: `${formatUsd(cost)} · ${formatTokens(tokens)} tokens · ${sessions} sessions` }];
|
|
for (const [provider, entry] of [...byProvider].sort((a, b) => b[1].cost - a[1].cost)) {
|
|
lines.push({ label: provider, value: `${formatUsd(entry.cost)} · ${formatTokens(entry.tokens)} tokens` });
|
|
}
|
|
return lines;
|
|
}
|