@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
83 lines • 4.56 kB
TypeScript
/**
* The latest limit windows each subscription account reported, kept for the life of the process and
* shared by every session on it (one account's windows are the same whichever session asked). An account
* is named by its credential slot - the stored account's name, `env` for an environment key, `default`
* for a single credential - never by key material. Nothing reported yet means the account is absent.
*/
import type { StreamOptions } from "@earendil-works/pi-ai";
import type { LimitWindow } from "./limit-windows.ts";
export interface AccountLimits {
readonly provider: string;
readonly account: string;
readonly windows: readonly LimitWindow[];
/** ISO time of the response that reported them. */
readonly reportedAt: string;
/** The plan the provider's usage endpoint names (`pro`, `max`, ...), when it names one. */
readonly plan?: string;
/**
* Why the last usage poll for this account failed (`sign-in needed`, `rate limited`, ...). The windows
* beside it are the last ones reported; the next successful report clears it (senpi#3684).
*/
readonly error?: string;
}
/**
* A record as the stores of one process's isolates mirror it to each other (senpi#3695). `updatedAt` is when the
* record last really changed, set only when that differs from `reportedAt` (a failed poll adds an error beside the
* last windows, which keep their time). With `revision` it orders mirrored copies; neither leaves the mirror:
* `snapshot()` and `subscribe()` hand out the public shape.
*/
export interface MirroredLimits extends AccountLimits {
readonly updatedAt?: string;
/**
* How many local writes landed at the same `updatedAt ?? reportedAt` instant before this one, so a same-instant
* write still orders after the record it replaces. Absent means 0. The time stays the real change time.
*/
readonly revision?: number;
}
/** What one usage-endpoint poll learned about an account: its full window set, or why it could not ask. */
export type PollReport = {
readonly windows: readonly LimitWindow[];
readonly plan?: string;
readonly reportedAt: Date;
} | {
readonly error: string;
readonly reportedAt: Date;
};
type Listener = (limits: AccountLimits) => void;
type MirrorListener = (limits: MirroredLimits) => void;
export declare class SubscriptionLimitStore {
#private;
/** Keeps the windows one response reported; a window it did not mention keeps its last value. */
record(provider: string, account: string, windows: readonly LimitWindow[], now?: Date): void;
/**
* Keeps what a usage-endpoint poll learned (senpi#3684). The endpoint answers with the account's whole
* window set, so it replaces the windows rather than merging; a failed poll keeps the last windows and
* says why beside them. An account with no windows and no prior record stays absent on success. A
* reading older than what the store already holds (a sibling's cached poll adopted after a fresher turn)
* is ignored, so the shown usage never goes backwards.
*/
report(provider: string, account: string, report: PollReport): void;
/**
* Takes another isolate's record as it stands (senpi#3695: the host and its session workers each hold a store
* and mirror each other). It goes through the same total order as every local write, so whatever order the
* copies cross in, every store keeps the same record and the echo ends when a copy comes straight back.
*/
adopt(limits: MirroredLimits): void;
snapshot(): readonly AccountLimits[];
/** The records with their ordering key, for mirroring into another isolate's store. */
mirrorSnapshot(): readonly MirroredLimits[];
subscribe(listener: Listener): () => void;
/** Hears every change with its ordering key, for mirroring into another isolate's store. */
subscribeMirror(listener: MirrorListener): () => void;
}
/** The process's store. */
export declare const subscriptionLimits: SubscriptionLimitStore;
/** The account a request ran under, as the store names it. */
export declare function limitAccountOf(slotAuth: {
apiKey?: string;
slotName?: string;
} | undefined): string;
/** Request options whose `onResponse` also records the windows the response headers carry. */
export declare function withLimitCapture<TOptions extends Pick<StreamOptions, "onResponse">>(options: TOptions, provider: string, account: string, store?: SubscriptionLimitStore): TOptions;
export {};
//# sourceMappingURL=limit-store.d.ts.map