@encoredigitalgroup/opencode 0.2.6 → 0.2.8
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 +9 -3
- package/dist/lib/Quota/core/errors.d.ts +21 -0
- package/dist/lib/Quota/core/pace.d.ts +14 -0
- package/dist/lib/Quota/core/persist.d.ts +55 -0
- package/dist/lib/Quota/core/provider-source.d.ts +43 -0
- package/dist/lib/Quota/core/redact.d.ts +30 -0
- package/dist/lib/Quota/core/rows.d.ts +19 -0
- package/dist/lib/Quota/core/selection.d.ts +14 -0
- package/dist/lib/Quota/core/store.d.ts +99 -0
- package/dist/lib/Quota/core/types.d.ts +167 -0
- package/dist/lib/Quota/core/validate.d.ts +11 -0
- package/dist/lib/Quota/core/windows.d.ts +23 -0
- package/dist/lib/Quota/lib/accounting-format.d.ts +3 -0
- package/dist/lib/Quota/lib/anthropic.d.ts +113 -0
- package/dist/lib/Quota/lib/api-key-resolver.d.ts +124 -0
- package/dist/lib/Quota/lib/atomic-json.d.ts +13 -0
- package/dist/lib/Quota/lib/calendar-date.d.ts +14 -0
- package/dist/lib/Quota/lib/config-file-utils.d.ts +6 -0
- package/dist/lib/Quota/lib/config-write-target.d.ts +23 -0
- package/dist/lib/Quota/lib/cursor-pricing.d.ts +107 -0
- package/dist/lib/Quota/lib/display-sanitize.d.ts +11 -0
- package/dist/lib/Quota/lib/entries.d.ts +87 -0
- package/dist/lib/Quota/lib/env-template.d.ts +25 -0
- package/dist/lib/Quota/lib/file-lock.d.ts +20 -0
- package/dist/lib/Quota/lib/format-utils.d.ts +8 -0
- package/dist/lib/Quota/lib/home-dir-display.d.ts +6 -0
- package/dist/lib/Quota/lib/http.d.ts +95 -0
- package/dist/lib/Quota/lib/json-guards.d.ts +10 -0
- package/dist/lib/Quota/lib/modelsdev-pricing.d.ts +85 -0
- package/dist/lib/Quota/lib/openai.d.ts +65 -0
- package/dist/lib/Quota/lib/opencode-auth.d.ts +154 -0
- package/dist/lib/Quota/lib/opencode-config-files.d.ts +33 -0
- package/dist/lib/Quota/lib/opencode-db-path.d.ts +8 -0
- package/dist/lib/Quota/lib/opencode-runtime-paths.d.ts +16 -0
- package/dist/lib/Quota/lib/pricing-key.d.ts +55 -0
- package/dist/lib/Quota/lib/provider-availability.d.ts +12 -0
- package/dist/lib/Quota/lib/provider-metadata.d.ts +25 -0
- package/dist/lib/Quota/lib/provider-model-matching.d.ts +12 -0
- package/dist/lib/Quota/lib/provider-registration.d.ts +222 -0
- package/dist/lib/Quota/lib/quota-export-types.d.ts +101 -0
- package/dist/lib/Quota/lib/quota-export.d.ts +33 -0
- package/dist/lib/Quota/lib/quota-providers.d.ts +201 -0
- package/dist/lib/Quota/lib/resolved-auth-identity.d.ts +52 -0
- package/dist/lib/Quota/lib/runtime-provider-ids.d.ts +3 -0
- package/dist/lib/Quota/lib/token-buckets.d.ts +22 -0
- package/dist/lib/Quota/lib/types.d.ts +208 -0
- package/dist/lib/Quota/plugin.d.ts +8 -0
- package/dist/lib/Quota/providers/anthropic.d.ts +6 -0
- package/dist/lib/Quota/providers/contract.d.ts +104 -0
- package/dist/lib/Quota/providers/openai.d.ts +5 -0
- package/dist/lib/Quota/providers/registry.d.ts +6 -0
- package/dist/lib/Quota/providers/result-helpers.d.ts +37 -0
- package/dist/lib/Quota/providers/xai.d.ts +8 -0
- package/dist/lib/Quota/rpc.d.ts +50 -0
- package/dist/lib/Quota/settings/convert.d.ts +59 -0
- package/dist/lib/Quota/settings/file.d.ts +89 -0
- package/dist/lib/Quota/settings/index.d.ts +10 -0
- package/dist/lib/Quota/settings/live.d.ts +42 -0
- package/dist/lib/Quota/settings/schema.d.ts +638 -0
- package/dist/lib/Quota/tui/client.d.ts +56 -0
- package/dist/lib/Quota/tui/index.d.ts +5 -0
- package/dist/lib/Quota/tui/sidebar.d.ts +42 -0
- package/dist/lib/Quota/tui/theme.d.ts +43 -0
- package/dist/lib/Quota/view/columns.d.ts +14 -0
- package/dist/lib/Quota/view/format.d.ts +80 -0
- package/dist/lib/Quota/view/row-facts.d.ts +52 -0
- package/dist/lib/Quota/view/select.d.ts +68 -0
- package/dist/lib/Quota/view/text.d.ts +160 -0
- package/dist/lib/TodoTracker.d.ts +3 -1
- package/dist/server/index.js +9182 -66
- package/dist/tui/index.js +5963 -224
- package/package.json +7 -3
- package/dist/lib/FilePickerCommands.test.d.ts +0 -1
- package/dist/lib/TodoTracker.test.d.ts +0 -1
- package/dist/package.integration.test.d.ts +0 -1
- package/dist/server/index.test.d.ts +0 -1
- package/dist/tui/index.test.d.ts +0 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# OpenCode planning commands
|
|
2
2
|
|
|
3
|
-
`@encoredigitalgroup/opencode` adds file pickers for OpenCode planning commands.
|
|
3
|
+
`@encoredigitalgroup/opencode` adds file pickers for OpenCode planning commands, a todo tracker, and quota reporting for Anthropic, OpenAI, and xAI (Grok).
|
|
4
4
|
|
|
5
5
|
The TUI provides `/plan-review`, `/plan-resync`, `/plan-split`, and `/plan-execute` commands. Each command opens a picker for plans in `docs/_planning` and
|
|
6
6
|
inserts the selected plan into the prompt. The server entry attaches the matching installed skill to the prompt.
|
|
@@ -67,11 +67,17 @@ Each todo requires:
|
|
|
67
67
|
- `status`: `pending`, `in_progress`, `completed`, or `cancelled`.
|
|
68
68
|
- `priority`: `high`, `medium`, or `low`.
|
|
69
69
|
|
|
70
|
+
Each tracker call replaces the complete ordered list. Include discovered work and current statuses in every replacement. Mark a todo completed only after
|
|
71
|
+
verifying its result. Mark a todo cancelled only when the user explicitly cancels that work. Never clear a list or change a status to avoid tracker updates,
|
|
72
|
+
tool restrictions, or difficult work.
|
|
73
|
+
|
|
70
74
|
The checklist renders only in the OpenCode terminal TUI sidebar. After a restart, it appears only when the completed `todotracker` result is in the host's loaded
|
|
71
75
|
message cache. That cache starts with the newest 20 messages, so older results are not shown until the host has loaded them.
|
|
72
76
|
|
|
73
|
-
|
|
74
|
-
|
|
77
|
+
Automatic refresh enforcement runs only while the latest valid list contains nonterminal work. After a session's first successful `todotracker` call, the plugin
|
|
78
|
+
requires another update at least once per minute while that work remains. When an update is due, it blocks every other tool until the tracker is updated. A valid
|
|
79
|
+
empty list or a list containing only `completed` and `cancelled` todos ends automatic refresh enforcement. A tracked subagent completing also requires its parent
|
|
80
|
+
session to update the tracker before continuing. Malformed tracker results remain enforcement-active because completion was not verified. Enforcement is disabled
|
|
75
81
|
for sessions that have never used `todotracker`, and can be disabled for all sessions with `"enforceTodoTracker": false`.
|
|
76
82
|
|
|
77
83
|
When a root session is idle, the sidebar renders the final in-progress todo as completed only when every earlier todo is completed. It restores the original status
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { QuotaError, QuotaErrorCode } from "./types.js";
|
|
2
|
+
export declare function isQuotaErrorCode(code: unknown): code is QuotaErrorCode;
|
|
3
|
+
/** An HTTP status as shown: a whole number from 100 to 599. */
|
|
4
|
+
export declare function httpStatus(status: unknown): number | undefined;
|
|
5
|
+
/** An error's code and HTTP status, "provider_error, http 503": never its message's words. */
|
|
6
|
+
export declare function errorKind(error: QuotaError): string;
|
|
7
|
+
/** Local programs named in provider messages, longest names first. */
|
|
8
|
+
export declare const LOCAL_TOOLS: readonly string[];
|
|
9
|
+
export declare function classifyError(message: string, nowMs?: number): QuotaError;
|
|
10
|
+
/** What a failed request says about itself: its code, HTTP status and when to try again. */
|
|
11
|
+
export type ErrorFacts = Pick<QuotaError, "code" | "status" | "retryAtMs">;
|
|
12
|
+
/** The latest retry time a provider's reply may set: a daily quota resets within it. */
|
|
13
|
+
export declare const RETRY_WAIT_MAX_MS: number;
|
|
14
|
+
/**
|
|
15
|
+
* A provider's failure: its own facts when its request gave them, else read from its words.
|
|
16
|
+
* When the facts only say `provider_error`, words that name a rate limit, a missing plan, an
|
|
17
|
+
* expired login or a timeout win; the status and retry time stay.
|
|
18
|
+
*/
|
|
19
|
+
export declare function providerError(message: string, facts: ErrorFacts | undefined, nowMs?: number): QuotaError;
|
|
20
|
+
/** The short words every surface shows for an error, for example "Sign in again". */
|
|
21
|
+
export declare function describeError(error: QuotaError, providerLabel: string, nowMs: number): string;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pace of a quota window: how much would be left now if the limit were used evenly until
|
|
3
|
+
* it resets (the Limits tab's │ marker), and whether it runs out before then. Pure.
|
|
4
|
+
*/
|
|
5
|
+
import type { QuotaRow } from "./types.js";
|
|
6
|
+
export type Pace = {
|
|
7
|
+
/** The percent that would be left now with even use: the share of the window still to come. */
|
|
8
|
+
evenLeft: number;
|
|
9
|
+
kind: "spare" | "on_track" | "fast";
|
|
10
|
+
/** Only for "fast": when it runs out at the pace so far (always before the reset). */
|
|
11
|
+
runsOutInMs?: number;
|
|
12
|
+
};
|
|
13
|
+
/** The pace of a row with a percent and a known window; undefined for every other row. */
|
|
14
|
+
export declare function paceOf(row: QuotaRow, nowMs: number): Pace | undefined;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { QuotaError, QuotaRow, ResetNotice } from "./types.js";
|
|
2
|
+
/** The snapshot file the server and the terminal command share. */
|
|
3
|
+
export declare function snapshotFilePath(): string;
|
|
4
|
+
/**
|
|
5
|
+
* A row and the login it came from: a hash of the provider's source id, "" when the provider
|
|
6
|
+
* has one login. The row's account is the login's own name, before the store numbers logins.
|
|
7
|
+
*/
|
|
8
|
+
export type LoginRow = {
|
|
9
|
+
login: string;
|
|
10
|
+
row: QuotaRow;
|
|
11
|
+
};
|
|
12
|
+
/** What the file keeps per provider and account, keyed `${providerId}|${identity hash}`. */
|
|
13
|
+
export type SavedEntry = {
|
|
14
|
+
providerId: string;
|
|
15
|
+
rows: LoginRow[];
|
|
16
|
+
okAtMs?: number;
|
|
17
|
+
checkedAtMs?: number;
|
|
18
|
+
error?: QuotaError;
|
|
19
|
+
/** Failed attempts in a row; drives the error backoff. */
|
|
20
|
+
failures: number;
|
|
21
|
+
/** Windows of this account that reset recently; they belong to it like its rows. */
|
|
22
|
+
notices: ResetNotice[];
|
|
23
|
+
/** Per row (see `rowSlot`): when its current reset time was first seen. */
|
|
24
|
+
resetSeenAtMs: Record<string, number>;
|
|
25
|
+
};
|
|
26
|
+
/** When a provider was last checked, its failures in a row and the retry time it asked for. */
|
|
27
|
+
export type LastCheck = {
|
|
28
|
+
checkedAtMs: number;
|
|
29
|
+
failures: number;
|
|
30
|
+
retryAtMs?: number;
|
|
31
|
+
};
|
|
32
|
+
export type SavedState = {
|
|
33
|
+
entries: Map<string, SavedEntry>;
|
|
34
|
+
/**
|
|
35
|
+
* By provider id, the last check of a login that can't be saved (its rows stay in memory), so
|
|
36
|
+
* a restarted store waits it out too. Nothing in it tells whose login it was.
|
|
37
|
+
*/
|
|
38
|
+
lastChecks: Map<string, LastCheck>;
|
|
39
|
+
};
|
|
40
|
+
/** One row's slot: rows of two checks match by id, account and login. */
|
|
41
|
+
export declare function rowSlot({ login, row }: LoginRow): string;
|
|
42
|
+
/** What the file holds now; empty when it is missing or does not parse. */
|
|
43
|
+
export declare function readSavedState(file: string): Promise<SavedState>;
|
|
44
|
+
export declare function joinNotices(first: readonly ResetNotice[], second: readonly ResetNotice[]): ResetNotice[];
|
|
45
|
+
/**
|
|
46
|
+
* Merges `state` into the file (per entry and per last check the newer check wins; notices are
|
|
47
|
+
* joined by id), drops entries and last checks older than a week, saved last checks dated after
|
|
48
|
+
* now and notices older than `noticeMaxAgeMs`, writes it atomically (owner-only), and returns
|
|
49
|
+
* the merged state. It holds the file's lock throughout and reads `now` under it; when another
|
|
50
|
+
* process keeps the lock 2 s, it writes nothing and rejects.
|
|
51
|
+
*/
|
|
52
|
+
export declare function writeSavedState(file: string, state: SavedState, options: {
|
|
53
|
+
now: () => number;
|
|
54
|
+
noticeMaxAgeMs: number;
|
|
55
|
+
}): Promise<SavedState>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { QuotaProvider, QuotaProviderContext } from "../lib/entries.js";
|
|
2
|
+
import type { LoadedSettings } from "../settings/index.js";
|
|
3
|
+
import type { StoreSource } from "./store.js";
|
|
4
|
+
export type ProviderSourceHost = {
|
|
5
|
+
/** OpenCode's provider list, for availability and model checks. */
|
|
6
|
+
client: QuotaProviderContext["client"];
|
|
7
|
+
/** The project folder quota is computed for. */
|
|
8
|
+
workspaceRoot: string;
|
|
9
|
+
/** The folder OpenCode runs the session in; see `QuotaProviderContext.locationDirectory`. */
|
|
10
|
+
locationDirectory: string;
|
|
11
|
+
/** The server settings now. */
|
|
12
|
+
settings: () => LoadedSettings;
|
|
13
|
+
/** The registered providers in display order; `getProviders()` when absent. */
|
|
14
|
+
providers?: () => readonly QuotaProvider[];
|
|
15
|
+
};
|
|
16
|
+
/** The settings the server reads besides the providers themselves. */
|
|
17
|
+
export type ServerSettings = {
|
|
18
|
+
waitForQuotaReset: boolean;
|
|
19
|
+
/** The export file, written after each snapshot change when on. */
|
|
20
|
+
export: {
|
|
21
|
+
enabled: boolean;
|
|
22
|
+
path: string;
|
|
23
|
+
};
|
|
24
|
+
/** Quota gauges through the host's OpenTelemetry MeterProvider. */
|
|
25
|
+
telemetry: boolean;
|
|
26
|
+
};
|
|
27
|
+
/** The start of every row id of the custom definition `definitionId`. */
|
|
28
|
+
export declare function customRowPrefix(definitionId: string): string;
|
|
29
|
+
/** The host's registered providers. */
|
|
30
|
+
export declare function registeredProviders(host: ProviderSourceHost): readonly QuotaProvider[];
|
|
31
|
+
/** What every provider reads from the settings when it checks its quota. */
|
|
32
|
+
export declare function providerContext(host: ProviderSourceHost, loaded?: LoadedSettings): QuotaProviderContext;
|
|
33
|
+
/**
|
|
34
|
+
* Each login (a source id: an account number, a credential id, often an email) as an opaque id
|
|
35
|
+
* keyed with the identity key (the install's protected key, or one held in memory where it
|
|
36
|
+
* can't be protected), so the snapshot file and notice ids can't be matched against guessed
|
|
37
|
+
* emails. Without that key no account identity exists either, so the store keeps such rows in
|
|
38
|
+
* memory only, and a key of this process is enough.
|
|
39
|
+
*/
|
|
40
|
+
export declare function keyedLogins(providerId: string, logins: readonly string[]): Promise<string[]>;
|
|
41
|
+
/** The store's source: the enabled and available providers, with the settings now. */
|
|
42
|
+
export declare function loadProviderSource(host: ProviderSourceHost): Promise<StoreSource>;
|
|
43
|
+
export declare function loadServerSettings(host: ProviderSourceHost): Promise<ServerSettings>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export declare const REDACTED = "[redacted]";
|
|
2
|
+
/**
|
|
3
|
+
* True when a text holds something shaped like a key: a known key shape, a random run of
|
|
4
|
+
* letters and digits, or (outside a name: an id, model name, URL or JSON path) any run of 24+
|
|
5
|
+
* of them. Any other character ends a run, so model names such as
|
|
6
|
+
* "Qwen3-Coder-480B-A35B-Instruct-2507", UUIDs and version strings pass.
|
|
7
|
+
*/
|
|
8
|
+
export declare function looksSecret(text: string, { isName }: {
|
|
9
|
+
isName: boolean;
|
|
10
|
+
}): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Replaces, in one text, each of `secrets` and each key shape the agent tools know. A secret under
|
|
13
|
+
* LONG_SECRET_LENGTH counts only as a whole token (letters, digits, `_` and `-`).
|
|
14
|
+
*/
|
|
15
|
+
export declare function secretValuesRedactor(secrets: readonly string[]): (text: string) => string;
|
|
16
|
+
/** A copy of a JSON value with `redact` applied to every string in it, property names too. */
|
|
17
|
+
export declare function redactJson(value: unknown, redact: (text: string) => string): unknown;
|
|
18
|
+
/** One safe line: control characters removed, credentials redacted, then cut to `maxLength`. */
|
|
19
|
+
export declare function safeDetail(text: string, maxLength: number): string;
|
|
20
|
+
/** An error's message (or any thrown value) as one safe line of at most 120 characters. */
|
|
21
|
+
export declare function safeErrorText(error: unknown): string;
|
|
22
|
+
/**
|
|
23
|
+
* Text a public issue may show: control characters removed first, then the home dir as "~"
|
|
24
|
+
* (found before spaces are joined), then one line with credentials as "[redacted]", email
|
|
25
|
+
* addresses as "[email]", and account, team, org, user and session ids as "[id]". It is cut to
|
|
26
|
+
* `maxLength` last, so a cut never leaves part of a hidden value.
|
|
27
|
+
*/
|
|
28
|
+
export declare function publicDetail(text: string, maxLength: number, homeDir?: string): string;
|
|
29
|
+
/** A detail's value as `publicDetail` shows it; all hidden when its key holds a secret or an id. */
|
|
30
|
+
export declare function publicDebugValue(key: string, value: string, maxLength: number, homeDir?: string): string;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider results as v6 rows. Every provider returns the row contract (`ProviderFetchResult`,
|
|
3
|
+
* see `providers/contract.ts`); each row gets its id (`${providerId}.${key}`), its provider and
|
|
4
|
+
* its account's account text.
|
|
5
|
+
*/
|
|
6
|
+
import type { ProviderFetchResult } from "../providers/contract.js";
|
|
7
|
+
import type { ProviderFetch } from "./store.js";
|
|
8
|
+
/**
|
|
9
|
+
* The account or plan in a legacy group: "OpenAI (Pro)" → "Pro", "[Claude Work] (active)" →
|
|
10
|
+
* "Work", "[OpenAI Work] (Business)" → "Work (Business)". "(active)" is dropped: it moves
|
|
11
|
+
* when the user switches logins, and the account must stay the same.
|
|
12
|
+
*/
|
|
13
|
+
export declare function accountFromGroup(providerId: string, group: string | undefined): string | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* A provider result as v6 rows, the error to show and the debug details. Each row is checked on
|
|
16
|
+
* its own, so it keeps its login. `rowLogins` holds the logins as the provider gave them; the
|
|
17
|
+
* provider source keys them before the store gets them.
|
|
18
|
+
*/
|
|
19
|
+
export declare function rowsFromProviderResult(providerId: string, result: ProviderFetchResult, nowMs?: number): ProviderFetch;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which providers serve a session's model: the snapshot's `currentModelProviderIds` and the
|
|
3
|
+
* `matchesCurrentModel` column of /quota_status.
|
|
4
|
+
*/
|
|
5
|
+
import type { QuotaProvider } from "../lib/entries.js";
|
|
6
|
+
import type { QuotaProviderDefinition } from "../lib/quota-providers.js";
|
|
7
|
+
export declare function matchesQuotaProviderCurrentSelection(params: {
|
|
8
|
+
provider: QuotaProvider;
|
|
9
|
+
currentModel?: string;
|
|
10
|
+
currentProviderID?: string;
|
|
11
|
+
enabledProviders?: string[] | "auto";
|
|
12
|
+
automaticDiscovery?: boolean;
|
|
13
|
+
quotaProviders?: QuotaProviderDefinition[];
|
|
14
|
+
}): boolean;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { QuotaExportProvider } from "../lib/quota-export-types.js";
|
|
2
|
+
import type { DebugDetail, QuotaError, QuotaRow, QuotaSnapshot, SessionModel } from "./types.js";
|
|
3
|
+
/** One provider fetch, already turned into rows. */
|
|
4
|
+
export type ProviderFetch = {
|
|
5
|
+
rows: QuotaRow[];
|
|
6
|
+
/**
|
|
7
|
+
* The login each row came from, in the order of `rows`; omitted for one login. An opaque id
|
|
8
|
+
* keyed with the identity key (`keyedLogins`), never a source id such as an email.
|
|
9
|
+
*/
|
|
10
|
+
rowLogins?: string[];
|
|
11
|
+
error?: QuotaError;
|
|
12
|
+
debug: DebugDetail[];
|
|
13
|
+
/** Writes this check's part of the export file from its rows as shown and its error. */
|
|
14
|
+
buildExportPart?: (check: CheckForExport) => QuotaExportProvider;
|
|
15
|
+
};
|
|
16
|
+
/** One check as the store records it: its rows as shown, its error, and when it was. */
|
|
17
|
+
export type CheckForExport = {
|
|
18
|
+
rows: QuotaRow[];
|
|
19
|
+
error?: QuotaError;
|
|
20
|
+
checkedAtMs: number;
|
|
21
|
+
};
|
|
22
|
+
/** Whose rows a fetch returns. */
|
|
23
|
+
export type ProviderIdentity = {
|
|
24
|
+
/** Changes whenever the rows would differ: another account or other provider settings. */
|
|
25
|
+
key: string;
|
|
26
|
+
/**
|
|
27
|
+
* False when the account can't be told apart safely, or only until a reload (its key is held
|
|
28
|
+
* in memory): rows stay in memory only.
|
|
29
|
+
*/
|
|
30
|
+
persist: boolean;
|
|
31
|
+
};
|
|
32
|
+
export type StoreProvider = {
|
|
33
|
+
id: string;
|
|
34
|
+
/** Registration label, for example "Anthropic". */
|
|
35
|
+
label: string;
|
|
36
|
+
/** Overrides the source's minimum interval (0 for providers that only read local data). */
|
|
37
|
+
minIntervalMs?: number;
|
|
38
|
+
identity: () => Promise<ProviderIdentity>;
|
|
39
|
+
fetch: () => Promise<ProviderFetch>;
|
|
40
|
+
};
|
|
41
|
+
/** The providers to show and refresh now (enabled and available, in registration order). */
|
|
42
|
+
export type StoreSource = {
|
|
43
|
+
providers: StoreProvider[];
|
|
44
|
+
minIntervalMs: number;
|
|
45
|
+
/** The providers serving a session's model, from the same settings read as `providers`. */
|
|
46
|
+
providerIdsForModel?: (model: SessionModel) => string[];
|
|
47
|
+
/** By provider id, the row id prefixes serving a session's model (`currentModelRowPrefixes`). */
|
|
48
|
+
rowPrefixesForModel?: (model: SessionModel) => Record<string, string[]>;
|
|
49
|
+
};
|
|
50
|
+
type RefreshReason = "poll" | "turn" | "manual" | "retry";
|
|
51
|
+
/**
|
|
52
|
+
* With a model, the snapshot also names the providers serving it. `refresh: false` answers the
|
|
53
|
+
* saved rows without asking any provider, also for the due ones.
|
|
54
|
+
*/
|
|
55
|
+
type SnapshotOptions = {
|
|
56
|
+
model?: SessionModel;
|
|
57
|
+
refresh?: boolean;
|
|
58
|
+
};
|
|
59
|
+
type RefreshOptions = {
|
|
60
|
+
model?: SessionModel;
|
|
61
|
+
reason: RefreshReason;
|
|
62
|
+
providerIds?: readonly string[];
|
|
63
|
+
/** Refreshes only the providers serving `model` (the snapshot's `currentModelProviderIds`). */
|
|
64
|
+
onlyModelProviders?: boolean;
|
|
65
|
+
};
|
|
66
|
+
type QuotaStoreDeps = {
|
|
67
|
+
source: () => Promise<StoreSource>;
|
|
68
|
+
/** The snapshot file; null keeps everything in memory. */
|
|
69
|
+
file: string | null;
|
|
70
|
+
now?: () => number;
|
|
71
|
+
log?: (message: string) => void;
|
|
72
|
+
/** Time limits for reading providers and logins, and for one fetch (tests shorten them). */
|
|
73
|
+
timeouts?: {
|
|
74
|
+
readMs: number;
|
|
75
|
+
fetchMs: number;
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
export type QuotaStore = {
|
|
79
|
+
snapshot(options?: SnapshotOptions): Promise<QuotaSnapshot>;
|
|
80
|
+
/** Refreshes the due providers (all, or `providerIds`) and answers when they are done. */
|
|
81
|
+
refresh(options: RefreshOptions): Promise<QuotaSnapshot>;
|
|
82
|
+
subscribe(listener: (snapshot: QuotaSnapshot) => void): () => void;
|
|
83
|
+
/** A login changed: identities are resolved again and memory-only rows are dropped. */
|
|
84
|
+
invalidateIdentity(): void;
|
|
85
|
+
/**
|
|
86
|
+
* The settings changed: running work is superseded and the providers are read again. With
|
|
87
|
+
* `recheck` (another time limit, interval or price list), every provider's backoff is cleared
|
|
88
|
+
* and each one is checked again at once.
|
|
89
|
+
*/
|
|
90
|
+
settingsChanged(options: {
|
|
91
|
+
recheck: boolean;
|
|
92
|
+
}): void;
|
|
93
|
+
/** Refuses new work and waits for running operations and the last file write. */
|
|
94
|
+
dispose(): Promise<void>;
|
|
95
|
+
};
|
|
96
|
+
/** `promise`, or a rejection with `message` once `ms` have passed. */
|
|
97
|
+
export declare function withTimeout<T>(promise: Promise<T>, ms: number, message: string): Promise<T>;
|
|
98
|
+
export declare function createQuotaStore(deps: QuotaStoreDeps): QuotaStore;
|
|
99
|
+
export {};
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The v6 quota data model: rows with stable ids, one snapshot per provider, and one error
|
|
3
|
+
* vocabulary. The server builds these; the TUI, Web text and the CLI only read them. Every
|
|
4
|
+
* value is plain JSON, because snapshots travel over the RPC and are saved to disk.
|
|
5
|
+
*/
|
|
6
|
+
import type { QuotaExportProvider } from "../lib/quota-export-types.js";
|
|
7
|
+
import type { ClientSettings, Settings } from "../settings/schema.js";
|
|
8
|
+
/** What a row's number means (ADR 0001: the 7 legacy result types become 5). */
|
|
9
|
+
export type RowType = "quota" | "budget" | "usage" | "balance" | "status";
|
|
10
|
+
/** How the number was acquired (ADR 0001: the 5 legacy methods become 4). */
|
|
11
|
+
export type RowMethod = "remote_api" | "dashboard_scrape" | "local_cli" | "local_estimation";
|
|
12
|
+
export type RowAuthority = "provider_reported" | "locally_derived";
|
|
13
|
+
/** The period a quota row covers; drives short labels, preferred windows and reset notices. */
|
|
14
|
+
export type QuotaWindow = "rpm" | "hour" | "five_hour" | "session" | "day" | "week" | "month" | "year" | "mcp" | "code_review";
|
|
15
|
+
/** A count unit is singular, for example "request"; the text adds the plural. */
|
|
16
|
+
export type AmountUnit = {
|
|
17
|
+
kind: "currency";
|
|
18
|
+
code: string;
|
|
19
|
+
} | {
|
|
20
|
+
kind: "count";
|
|
21
|
+
unit: string;
|
|
22
|
+
} | {
|
|
23
|
+
kind: "custom";
|
|
24
|
+
symbol: string;
|
|
25
|
+
};
|
|
26
|
+
/** An exact number: `decimal` is a canonical decimal string such as "12.5" or "-3". */
|
|
27
|
+
export type Amount = {
|
|
28
|
+
decimal: string;
|
|
29
|
+
unit: AmountUnit;
|
|
30
|
+
};
|
|
31
|
+
export type QuotaRow = {
|
|
32
|
+
/** `${providerId}.${key}`, for example "anthropic.weekly_fable"; the hide/rename/order key. */
|
|
33
|
+
id: string;
|
|
34
|
+
providerId: string;
|
|
35
|
+
/** Account or plan shown after the provider name, for example "work" or "SuperGrok". */
|
|
36
|
+
account?: string;
|
|
37
|
+
/** Default label, for example "5h", "Weekly", "Fable", "Balance". */
|
|
38
|
+
label: string;
|
|
39
|
+
type: RowType;
|
|
40
|
+
method: RowMethod;
|
|
41
|
+
authority: RowAuthority;
|
|
42
|
+
window?: QuotaWindow;
|
|
43
|
+
prominence: "primary" | "supplementary";
|
|
44
|
+
/** Raw percent left; display rounds and clamps. May be below 0 when over quota. */
|
|
45
|
+
percentRemaining?: number;
|
|
46
|
+
used?: Amount;
|
|
47
|
+
limit?: Amount;
|
|
48
|
+
remaining?: Amount;
|
|
49
|
+
/** The value of a usage or balance row. */
|
|
50
|
+
amount?: Amount;
|
|
51
|
+
/** The value of a yes/no status row. */
|
|
52
|
+
flag?: boolean;
|
|
53
|
+
/** Text value, for example "Unlimited" or a custom provider's own summary. */
|
|
54
|
+
text?: string;
|
|
55
|
+
/** ISO time the window resets. */
|
|
56
|
+
resetsAt?: string;
|
|
57
|
+
/** ISO time the window started (pace marker and runs-out estimate). */
|
|
58
|
+
windowStartedAt?: string;
|
|
59
|
+
};
|
|
60
|
+
export type QuotaErrorCode = "not_configured" | "login_expired" | "no_plan" | "rate_limited" | "timeout" | "network" | "provider_error" | "bad_response" | "no_data" | "local_tool" | "internal";
|
|
61
|
+
export type QuotaError = {
|
|
62
|
+
code: QuotaErrorCode;
|
|
63
|
+
/** HTTP status, when the provider answered with one. */
|
|
64
|
+
status?: number;
|
|
65
|
+
/** When the provider said to try again. */
|
|
66
|
+
retryAtMs?: number;
|
|
67
|
+
/** The local program involved, for `local_tool`, for example "Claude CLI". */
|
|
68
|
+
tool?: string;
|
|
69
|
+
/** For `local_tool`: true when the program is missing rather than failing. */
|
|
70
|
+
missing?: boolean;
|
|
71
|
+
/** The provider's own message, scrubbed; no report shows it. */
|
|
72
|
+
detail?: string;
|
|
73
|
+
};
|
|
74
|
+
export type DebugDetail = {
|
|
75
|
+
key: string;
|
|
76
|
+
value: string;
|
|
77
|
+
};
|
|
78
|
+
export type ProviderSnapshot = {
|
|
79
|
+
providerId: string;
|
|
80
|
+
/** Registration label, for example "Anthropic" or "Copilot". */
|
|
81
|
+
label: string;
|
|
82
|
+
/** The last good rows; a failed refresh keeps them. */
|
|
83
|
+
rows: QuotaRow[];
|
|
84
|
+
/** When `rows` were fetched. */
|
|
85
|
+
okAtMs?: number;
|
|
86
|
+
/** When the last attempt ended. */
|
|
87
|
+
checkedAtMs?: number;
|
|
88
|
+
/** Why the last attempt failed. */
|
|
89
|
+
error?: QuotaError;
|
|
90
|
+
refreshing: boolean;
|
|
91
|
+
/**
|
|
92
|
+
* Provider facts for /quota_status. Like `exportPart`, from the last check in this server
|
|
93
|
+
* process: the store keeps both in memory only, never in the snapshot file, and a client
|
|
94
|
+
* snapshot holds neither.
|
|
95
|
+
*/
|
|
96
|
+
debug?: DebugDetail[];
|
|
97
|
+
/** This provider's part of the export file. */
|
|
98
|
+
exportPart?: QuotaExportProvider;
|
|
99
|
+
};
|
|
100
|
+
/** A quota window that reset since the last check; each TUI shows it once (by `id`). */
|
|
101
|
+
export type ResetNotice = {
|
|
102
|
+
id: string;
|
|
103
|
+
rowId: string;
|
|
104
|
+
providerId: string;
|
|
105
|
+
account?: string;
|
|
106
|
+
label: string;
|
|
107
|
+
window: QuotaWindow;
|
|
108
|
+
percentRemaining: number;
|
|
109
|
+
atMs: number;
|
|
110
|
+
};
|
|
111
|
+
/** One model's input tokens in a session: new input, cache reads and cache writes. */
|
|
112
|
+
export type SessionModelInput = {
|
|
113
|
+
modelID: string;
|
|
114
|
+
input: number;
|
|
115
|
+
cacheRead: number;
|
|
116
|
+
cacheWrite: number;
|
|
117
|
+
};
|
|
118
|
+
/** Tokens a session used so far (with its subagents when the words say "tree"). */
|
|
119
|
+
export type SessionTokens = {
|
|
120
|
+
input: number;
|
|
121
|
+
cachedInput: number;
|
|
122
|
+
output: number;
|
|
123
|
+
/** Each model with input tokens, for the sidebar's low cache line; absent when none had any. */
|
|
124
|
+
models?: SessionModelInput[];
|
|
125
|
+
};
|
|
126
|
+
/** The words every surface uses, on every client (TUI, Web, Desktop, CLI). */
|
|
127
|
+
export type Words = Settings["words"];
|
|
128
|
+
/** A session's model: its id and OpenCode's provider id. */
|
|
129
|
+
export type SessionModel = {
|
|
130
|
+
id?: string;
|
|
131
|
+
providerID?: string;
|
|
132
|
+
};
|
|
133
|
+
export type QuotaSnapshot = {
|
|
134
|
+
version: 6;
|
|
135
|
+
/** Enabled and available providers, in registration order. */
|
|
136
|
+
providers: ProviderSnapshot[];
|
|
137
|
+
/** With a session: the providers serving its current model (the "current model only" filter). */
|
|
138
|
+
currentModelProviderIds?: string[];
|
|
139
|
+
/**
|
|
140
|
+
* With a session: for a provider whose rows serve different models (the custom providers,
|
|
141
|
+
* one definition each), the row id prefixes that serve its current model, by provider id.
|
|
142
|
+
* Every row is still checked; under "current model only" just these show.
|
|
143
|
+
*/
|
|
144
|
+
currentModelRowPrefixes?: Record<string, string[]>;
|
|
145
|
+
/** With a session: the tokens it used so far. */
|
|
146
|
+
sessionTokens?: SessionTokens;
|
|
147
|
+
/**
|
|
148
|
+
* The OpenCode provider id each custom definition serves, by the prefix of its row ids
|
|
149
|
+
* (`quota-providers.custom.<definition id>.`); absent without custom definitions.
|
|
150
|
+
*/
|
|
151
|
+
customRowProviders?: Record<string, string>;
|
|
152
|
+
/**
|
|
153
|
+
* The server's words, for the text the server and CLI print (absent means the default words).
|
|
154
|
+
* The TUI takes its words from its settings store instead.
|
|
155
|
+
*/
|
|
156
|
+
words?: Words;
|
|
157
|
+
/** All editable file settings, refreshed with every settings change. */
|
|
158
|
+
settings?: ClientSettings;
|
|
159
|
+
/** This server converted v5 settings into the file; a TUI announces it once. */
|
|
160
|
+
settingsConverted?: boolean;
|
|
161
|
+
resetNotices: ResetNotice[];
|
|
162
|
+
generatedAtMs: number;
|
|
163
|
+
/** Random for each server start; revisions compare only within one server. */
|
|
164
|
+
serverId: string;
|
|
165
|
+
/** Grows with every snapshot the server builds: of two, the higher revision is newer. */
|
|
166
|
+
revision: number;
|
|
167
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { DebugDetail, QuotaRow } from "./types.js";
|
|
2
|
+
/** Longest label or account, in code points. Custom provider names and labels use it too. */
|
|
3
|
+
export declare const LABEL_MAX_CODE_POINTS = 80;
|
|
4
|
+
export declare function isIsoTime(value: unknown): value is string;
|
|
5
|
+
/** A text's length in Unicode code points, the way every row text limit counts. */
|
|
6
|
+
export declare function codePointLength(text: string): number;
|
|
7
|
+
/** Keeps every valid row (as a clean copy) and notes each dropped one. */
|
|
8
|
+
export declare function validateRows(providerId: string, candidates: readonly unknown[]): {
|
|
9
|
+
rows: QuotaRow[];
|
|
10
|
+
dropped: DebugDetail[];
|
|
11
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one table of quota windows: labels, nominal length, whether the window resets on a
|
|
3
|
+
* schedule, and the classifier that reads a window from a legacy label such as "5h:".
|
|
4
|
+
*/
|
|
5
|
+
import type { QuotaWindow } from "./types.js";
|
|
6
|
+
type WindowInfo = {
|
|
7
|
+
label: string;
|
|
8
|
+
short: string;
|
|
9
|
+
/** Fixed length, when the window has one (months and years do not). */
|
|
10
|
+
lengthMs?: number;
|
|
11
|
+
/** True when the window refills at a reset time, so a reset notice makes sense. */
|
|
12
|
+
resets: boolean;
|
|
13
|
+
};
|
|
14
|
+
export declare const WINDOWS: Readonly<Record<QuotaWindow, WindowInfo>>;
|
|
15
|
+
/**
|
|
16
|
+
* The key of `collapsed.window` and `statusLine.window` that picks the window of every provider
|
|
17
|
+
* without a pick of its own.
|
|
18
|
+
*/
|
|
19
|
+
export declare const ALL_PROVIDERS = "*";
|
|
20
|
+
export declare function isQuotaWindow(value: unknown): value is QuotaWindow;
|
|
21
|
+
/** The window a label names, for example "5h:" → five_hour, "Weekly" → week. */
|
|
22
|
+
export declare function classifyWindow(text: string): QuotaWindow | undefined;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Anthropic Claude quota probing.
|
|
3
|
+
*
|
|
4
|
+
* Each Claude login stored in OpenCode is asked once at Anthropic's OAuth usage endpoint.
|
|
5
|
+
* Without one, the local Claude CLI/runtime is probed for install/auth state; when Claude auth
|
|
6
|
+
* is confirmed but local quota windows are missing, the usage endpoint is asked with Claude's
|
|
7
|
+
* own OAuth credentials (macOS Keychain first, then the local credentials file).
|
|
8
|
+
*/
|
|
9
|
+
import type { ErrorFacts } from "../core/errors.js";
|
|
10
|
+
import { type CredentialRow } from "./opencode-auth.js";
|
|
11
|
+
import { type ResolvedAuthIdentity } from "./resolved-auth-identity.js";
|
|
12
|
+
export interface AnthropicQuotaResult {
|
|
13
|
+
success: true;
|
|
14
|
+
five_hour: {
|
|
15
|
+
percentRemaining: number;
|
|
16
|
+
resetTimeIso?: string;
|
|
17
|
+
};
|
|
18
|
+
seven_day: {
|
|
19
|
+
percentRemaining: number;
|
|
20
|
+
resetTimeIso?: string;
|
|
21
|
+
};
|
|
22
|
+
extra_usage?: {
|
|
23
|
+
percentRemaining: number;
|
|
24
|
+
};
|
|
25
|
+
fable_weekly?: {
|
|
26
|
+
percentRemaining: number;
|
|
27
|
+
resetTimeIso?: string;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
interface AnthropicQuotaError {
|
|
31
|
+
success: false;
|
|
32
|
+
error: string;
|
|
33
|
+
facts?: ErrorFacts;
|
|
34
|
+
/** Anthropic rejected the token (HTTP 401 or 403). */
|
|
35
|
+
rejected?: true;
|
|
36
|
+
}
|
|
37
|
+
type AnthropicAuthStatus = "authenticated" | "unauthenticated" | "unknown";
|
|
38
|
+
type AnthropicQuotaSource = "claude-auth-status-json" | "claude-credentials-oauth-api" | "none";
|
|
39
|
+
/** What the Claude CLI and Claude's own credentials gave, without OpenCode's logins. */
|
|
40
|
+
export interface AnthropicDiagnostics {
|
|
41
|
+
installed: boolean;
|
|
42
|
+
version: string | null;
|
|
43
|
+
authStatus: AnthropicAuthStatus;
|
|
44
|
+
quotaSupported: boolean;
|
|
45
|
+
quotaSource: AnthropicQuotaSource;
|
|
46
|
+
/** Set when Claude's own OAuth token was read for the usage probe. */
|
|
47
|
+
oauthCredentialSource?: "claude-credentials";
|
|
48
|
+
/**
|
|
49
|
+
* The Claude CLI the probe ran: the configured path, `claude (PATH)`, or an
|
|
50
|
+
* install-folder path. Null when no Claude CLI was found.
|
|
51
|
+
*/
|
|
52
|
+
binaryPath: string | null;
|
|
53
|
+
checkedCommands: string[];
|
|
54
|
+
message?: string;
|
|
55
|
+
/** The facts of the usage request that failed. */
|
|
56
|
+
facts?: ErrorFacts;
|
|
57
|
+
quota?: AnthropicQuotaResult;
|
|
58
|
+
}
|
|
59
|
+
interface AnthropicProbeOptions {
|
|
60
|
+
binaryPath?: string;
|
|
61
|
+
requestTimeoutMs?: number;
|
|
62
|
+
}
|
|
63
|
+
type ClaudeCommandInvocation = {
|
|
64
|
+
file: string;
|
|
65
|
+
args: string[];
|
|
66
|
+
display: string;
|
|
67
|
+
};
|
|
68
|
+
type ResolvedAnthropicOAuth = {
|
|
69
|
+
state: "none";
|
|
70
|
+
} | {
|
|
71
|
+
state: "failed";
|
|
72
|
+
error: string;
|
|
73
|
+
} | {
|
|
74
|
+
state: "expired";
|
|
75
|
+
expiresAt: number;
|
|
76
|
+
} | {
|
|
77
|
+
state: "configured";
|
|
78
|
+
accessToken: string;
|
|
79
|
+
expiresAt?: number;
|
|
80
|
+
};
|
|
81
|
+
/** A Claude sign-in's state; `none` for a row that is not an OAuth login or has no token. */
|
|
82
|
+
export declare function resolveAnthropicOAuth(row: CredentialRow, options?: {
|
|
83
|
+
nowMs?: number;
|
|
84
|
+
}): ResolvedAnthropicOAuth;
|
|
85
|
+
export type AnthropicLogin = {
|
|
86
|
+
row: CredentialRow;
|
|
87
|
+
auth: Exclude<ResolvedAnthropicOAuth, {
|
|
88
|
+
state: "none";
|
|
89
|
+
}>;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Every OpenCode Claude sign-in with a token, in OpenCode's order (the one it uses first). A
|
|
93
|
+
* sign-in OpenCode could not return, or whose token expired, stays, so it shows as its own error.
|
|
94
|
+
*/
|
|
95
|
+
export declare function readAnthropicLogins(): Promise<AnthropicLogin[]>;
|
|
96
|
+
export declare function buildClaudeCommandInvocation(binaryPath: string, args: string[], runtime?: {
|
|
97
|
+
platform?: NodeJS.Platform;
|
|
98
|
+
comspec?: string;
|
|
99
|
+
}): ClaudeCommandInvocation;
|
|
100
|
+
/** Quota from Claude CLI `auth status --json`: only the 5h and 7d windows. */
|
|
101
|
+
declare function parseUsageResponse(data: unknown): AnthropicQuotaResult | null;
|
|
102
|
+
/** Quota from the OAuth usage endpoint: the 5h and 7d windows, Usage Credits and Fable weekly. */
|
|
103
|
+
declare function parseOAuthUsageResponse(data: unknown): AnthropicQuotaResult | null;
|
|
104
|
+
export declare function queryAnthropicQuotaWithOAuth(accessToken: string, requestTimeoutMs?: number): Promise<AnthropicQuotaResult | AnthropicQuotaError>;
|
|
105
|
+
export declare function clearAnthropicDiagnosticsCacheForTests(): void;
|
|
106
|
+
export declare function getAnthropicDiagnostics(options?: AnthropicProbeOptions): Promise<AnthropicDiagnostics>;
|
|
107
|
+
export declare function hasAnthropicCredentialsConfigured(options?: AnthropicProbeOptions): Promise<boolean>;
|
|
108
|
+
/**
|
|
109
|
+
* Who Claude is signed in as: every OpenCode login when there are some, since only they are
|
|
110
|
+
* checked then; else the Claude CLI's credentials.
|
|
111
|
+
*/
|
|
112
|
+
export declare function anthropicLoginsIdentity(logins: readonly AnthropicLogin[], options?: AnthropicProbeOptions): Promise<ResolvedAuthIdentity | null>;
|
|
113
|
+
export { parseOAuthUsageResponse, parseUsageResponse };
|