@encoredigitalgroup/opencode 0.2.7 → 0.2.9
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/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 +102 -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/server/index.js +9112 -35
- package/dist/tui/index.js +5916 -213
- 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
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One API key resolver for every API-key vendor.
|
|
3
|
+
*
|
|
4
|
+
* Order (first wins): environment variables, then the key OpenCode ends up with from the config
|
|
5
|
+
* files that count for the session's folder (`providers.<id>.settings.apiKey`, or the legacy
|
|
6
|
+
* `provider.<id>.options.apiKey`, with `{env:NAME}` templates), then OpenCode's stored logins.
|
|
7
|
+
* Each vendor's spec is its `apiKey` in `provider-registration.ts`.
|
|
8
|
+
*/
|
|
9
|
+
import { type CredentialRow } from "./opencode-auth.js";
|
|
10
|
+
import { QUOTA_PROVIDER_REGISTRATION_SOURCE } from "./provider-registration.js";
|
|
11
|
+
/** Where an API key may live for one vendor. */
|
|
12
|
+
export interface ApiKeySpec {
|
|
13
|
+
/** Checked first, in order. Also the only names allowed in `{env:NAME}` config templates. */
|
|
14
|
+
envVars: readonly string[];
|
|
15
|
+
/** Provider ids read from the OpenCode config files, in order. */
|
|
16
|
+
providerKeys: readonly string[];
|
|
17
|
+
/** OpenCode login integration ids, in order. Default: `providerKeys`. */
|
|
18
|
+
authKeys?: readonly string[];
|
|
19
|
+
/** Read key logins only: the same integration can also hold an OAuth sign-in. */
|
|
20
|
+
keyLoginsOnly?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/** An API-key vendor; a broken stored login (wrong type, empty key, unreadable) is an error. */
|
|
23
|
+
export interface ApiKeyLoginSpec extends ApiKeySpec {
|
|
24
|
+
authKeys: readonly string[];
|
|
25
|
+
/** Used in login errors, e.g. "Z.ai auth entry has invalid shape". */
|
|
26
|
+
displayName: string;
|
|
27
|
+
/** Replaces the "Unsupported <name> auth type" error (it would echo the stored type). */
|
|
28
|
+
unsupportedTypeError?: string;
|
|
29
|
+
/** A stored login that is not a usable API key is left out instead of shown as an error. */
|
|
30
|
+
skipBrokenLogins?: boolean;
|
|
31
|
+
}
|
|
32
|
+
type ApiKeyVendorId = Extract<(typeof QUOTA_PROVIDER_REGISTRATION_SOURCE)[number], {
|
|
33
|
+
apiKey: object;
|
|
34
|
+
}>["id"];
|
|
35
|
+
/** Each API-key vendor's spec, from its `apiKey` in provider-registration.ts. */
|
|
36
|
+
export declare const API_KEY_VENDORS: Readonly<Record<ApiKeyVendorId, ApiKeyLoginSpec>>;
|
|
37
|
+
export type ApiKeySource = `env:${string}` | "opencode.json" | "opencode.jsonc" | "opencode.db";
|
|
38
|
+
export interface ApiKeyResult {
|
|
39
|
+
key: string;
|
|
40
|
+
source: ApiKeySource;
|
|
41
|
+
}
|
|
42
|
+
type ApiKeyLoginResult = {
|
|
43
|
+
state: "none";
|
|
44
|
+
} | {
|
|
45
|
+
state: "configured";
|
|
46
|
+
apiKey: string;
|
|
47
|
+
} | {
|
|
48
|
+
state: "invalid";
|
|
49
|
+
error: string;
|
|
50
|
+
};
|
|
51
|
+
type DiagnosticPaths = {
|
|
52
|
+
checkedPaths: string[];
|
|
53
|
+
credentialDatabasePaths: string[];
|
|
54
|
+
};
|
|
55
|
+
export type ApiKeyLoginDiagnostics = ({
|
|
56
|
+
state: "none";
|
|
57
|
+
source: null;
|
|
58
|
+
} & DiagnosticPaths) | ({
|
|
59
|
+
state: "configured";
|
|
60
|
+
source: ApiKeySource;
|
|
61
|
+
} & DiagnosticPaths) | ({
|
|
62
|
+
state: "invalid";
|
|
63
|
+
source: "opencode.db";
|
|
64
|
+
error: string;
|
|
65
|
+
} & DiagnosticPaths);
|
|
66
|
+
/** The login of the first auth key, in order, that has one among `rows`. */
|
|
67
|
+
export declare function firstLoginRow(rows: readonly CredentialRow[], authKeys: readonly string[]): CredentialRow | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* Read one provider's settings the way OpenCode 2 normalizes its config.
|
|
70
|
+
*
|
|
71
|
+
* A native `providers.<id>` entry replaces the legacy `provider.<id>` entry,
|
|
72
|
+
* whose `options` OpenCode 2 migrates into `settings` unless it drops the entry.
|
|
73
|
+
*/
|
|
74
|
+
export declare function getProviderConfigSettings(config: unknown, providerKey: string): Record<string, unknown> | null;
|
|
75
|
+
/**
|
|
76
|
+
* Read one provider string setting from the OpenCode config files that count for `directory`:
|
|
77
|
+
* the value OpenCode ends up with, when it is a non-empty string.
|
|
78
|
+
*/
|
|
79
|
+
export declare function readProviderConfigString(providerKey: string, settingKey: string, directory: string): Promise<string | undefined>;
|
|
80
|
+
/** Environment variables, then the OpenCode config files that count for `directory`. */
|
|
81
|
+
export declare function resolveApiKeyFromEnvAndConfig(spec: ApiKeySpec, directory: string): Promise<ApiKeyResult | null>;
|
|
82
|
+
/** Env vars that are set and the OpenCode config files for `directory` that exist (no values). */
|
|
83
|
+
export declare function getApiKeyCheckedPaths(spec: Pick<ApiKeySpec, "envVars">, directory: string): string[];
|
|
84
|
+
export declare function resolveApiKey(spec: ApiKeySpec, directory: string): Promise<ApiKeyResult | null>;
|
|
85
|
+
/** The vendor's stored login that decides: the active one of its first auth key (cached 5 s). */
|
|
86
|
+
export declare function readApiKeyLoginRow(spec: ApiKeyLoginSpec): Promise<CredentialRow | undefined>;
|
|
87
|
+
/** A stored login as an API key: `none` without one, `invalid` when it is not a usable key. */
|
|
88
|
+
export declare function parseApiKeyLogin(spec: ApiKeyLoginSpec, row: CredentialRow | undefined): ApiKeyLoginResult;
|
|
89
|
+
export declare function resolveApiKeyLoginWithDiagnostics(spec: ApiKeyLoginSpec, directory: string): Promise<{
|
|
90
|
+
auth: ApiKeyLoginResult;
|
|
91
|
+
diagnostics: ApiKeyLoginDiagnostics;
|
|
92
|
+
}>;
|
|
93
|
+
export declare function resolveApiKeyLogin(spec: ApiKeyLoginSpec, directory: string): Promise<ApiKeyLoginResult>;
|
|
94
|
+
export declare function getApiKeyLoginDiagnostics(spec: ApiKeyLoginSpec, directory: string): Promise<ApiKeyLoginDiagnostics>;
|
|
95
|
+
export type KeyLogin = {
|
|
96
|
+
row: CredentialRow;
|
|
97
|
+
auth: Exclude<ApiKeyLoginResult, {
|
|
98
|
+
state: "none";
|
|
99
|
+
}>;
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Every key login of the vendor stored in OpenCode, in OpenCode's order, with what each one
|
|
103
|
+
* holds. `selectRows` narrows the logins further, for example to drop alias duplicates.
|
|
104
|
+
*/
|
|
105
|
+
export declare function readKeyLogins(spec: ApiKeyLoginSpec, options?: {
|
|
106
|
+
selectRows?: (rows: CredentialRow[]) => CredentialRow[];
|
|
107
|
+
}): Promise<KeyLogin[]>;
|
|
108
|
+
/**
|
|
109
|
+
* The keys a vendor checks: the env or config key when one is set, else every stored login.
|
|
110
|
+
* `broken` is the error of the first stored login when it is not a usable key and no stored
|
|
111
|
+
* key login is listed (for example an OAuth login of a key-only vendor); vendors that skip
|
|
112
|
+
* broken logins never have one.
|
|
113
|
+
*/
|
|
114
|
+
export type VendorKeys = {
|
|
115
|
+
envOrConfig: ApiKeyResult | null;
|
|
116
|
+
logins: KeyLogin[];
|
|
117
|
+
broken: string | null;
|
|
118
|
+
};
|
|
119
|
+
export declare function readVendorKeys(spec: ApiKeyLoginSpec, directory: string, options?: {
|
|
120
|
+
selectRows?: (rows: CredentialRow[]) => CredentialRow[];
|
|
121
|
+
}): Promise<VendorKeys>;
|
|
122
|
+
/** True when the vendor has a key to check, or a broken login to show. */
|
|
123
|
+
export declare function hasVendorKey(keys: VendorKeys): boolean;
|
|
124
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
type AtomicWritePolicy = "ordinary" | "configuration";
|
|
2
|
+
export interface WriteJsonAtomicOptions {
|
|
3
|
+
trailingNewline?: boolean;
|
|
4
|
+
directoryMode?: number;
|
|
5
|
+
fileMode?: number;
|
|
6
|
+
/** Default ordinary. Configuration never deletes the destination; it only removes the temp file. */
|
|
7
|
+
policy?: AtomicWritePolicy;
|
|
8
|
+
/** Runs after the temp file is written, right before it replaces `path`; throwing writes nothing. */
|
|
9
|
+
beforeCommit?: () => Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
export declare function writeJsonAtomic(path: string, data: unknown, opts?: WriteJsonAtomicOptions): Promise<void>;
|
|
12
|
+
export declare function writeTextAtomic(path: string, content: string, opts?: Omit<WriteJsonAtomicOptions, "trailingNewline">): Promise<void>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one rule for a date a client sends (the usage report's "between" range), shared by the
|
|
3
|
+
* RPC input check and the report itself: "YYYY-MM-DD" naming a real calendar day. So
|
|
4
|
+
* 2026-02-31 and 2026-02-29 are not dates, and 2028-02-29 is.
|
|
5
|
+
*/
|
|
6
|
+
type CalendarDate = {
|
|
7
|
+
year: number;
|
|
8
|
+
month: number;
|
|
9
|
+
day: number;
|
|
10
|
+
};
|
|
11
|
+
export declare function daysInMonth(year: number, month: number): number;
|
|
12
|
+
/** The day `text` names, or null when it is not a real "YYYY-MM-DD" day. */
|
|
13
|
+
export declare function parseCalendarDate(text: unknown): CalendarDate | null;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function dedupeNonEmptyStrings(items: string[]): string[];
|
|
2
|
+
export declare function findGitWorktreeRoot(startDir: string): string | null;
|
|
3
|
+
/** Reads the package spec from a legacy `plugin` entry or an OpenCode 2 native `plugins` entry. */
|
|
4
|
+
export declare function getPluginSpecFromEntry(entry: unknown): string | null;
|
|
5
|
+
export declare function extractPluginSpecsFromParsedConfig(parsed: unknown): string[];
|
|
6
|
+
export declare function extractProviderIdsFromParsedConfig(parsed: unknown): string[];
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export declare const MAX_CONFIG_SYMLINK_HOPS = 40;
|
|
2
|
+
export declare const NEW_CONFIG_FILE_MODE = 384;
|
|
3
|
+
export declare const NEW_CONFIG_DIRECTORY_MODE = 448;
|
|
4
|
+
type ConfigWriteTargetReason = "dangling" | "loop" | "depth" | "non-regular" | "permission" | "changed" | "bytes-changed";
|
|
5
|
+
interface ConfigWriteHop {
|
|
6
|
+
path: string;
|
|
7
|
+
linkText: string;
|
|
8
|
+
}
|
|
9
|
+
export interface ConfigWriteTargetSnapshot {
|
|
10
|
+
configuredPath: string;
|
|
11
|
+
writePath: string;
|
|
12
|
+
hops: ConfigWriteHop[];
|
|
13
|
+
terminalExisted: boolean;
|
|
14
|
+
}
|
|
15
|
+
export declare class ConfigWriteTargetError extends Error {
|
|
16
|
+
readonly path: string;
|
|
17
|
+
readonly reason: ConfigWriteTargetReason;
|
|
18
|
+
constructor(message: string, path: string, reason: ConfigWriteTargetReason);
|
|
19
|
+
}
|
|
20
|
+
export declare function configWriteTargetsEqual(left: ConfigWriteTargetSnapshot, right: ConfigWriteTargetSnapshot): boolean;
|
|
21
|
+
export declare function resolveConfigWriteTarget(path: string): Promise<ConfigWriteTargetSnapshot>;
|
|
22
|
+
export declare function writeResolvedConfigText(snapshot: ConfigWriteTargetSnapshot, content: string, beforeCommit?: () => Promise<void>): Promise<void>;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { CostBuckets } from "./modelsdev-pricing.js";
|
|
2
|
+
export type CursorQuotaPlan = "none" | "pro" | "pro-plus" | "ultra";
|
|
3
|
+
/** "unpriced": Cursor's own price, which no table here holds. */
|
|
4
|
+
type CursorResolvedModel = {
|
|
5
|
+
kind: "local";
|
|
6
|
+
model: CursorLocalPricingModel;
|
|
7
|
+
pool: "auto_composer";
|
|
8
|
+
} | {
|
|
9
|
+
kind: "official";
|
|
10
|
+
providerHint: string;
|
|
11
|
+
modelHint: string;
|
|
12
|
+
pool: "api";
|
|
13
|
+
} | {
|
|
14
|
+
kind: "unpriced";
|
|
15
|
+
} | {
|
|
16
|
+
kind: "unknown";
|
|
17
|
+
};
|
|
18
|
+
declare const CURSOR_LOCAL_PRICING: {
|
|
19
|
+
auto: {
|
|
20
|
+
input: number;
|
|
21
|
+
output: number;
|
|
22
|
+
cache_read: number;
|
|
23
|
+
};
|
|
24
|
+
"composer-1": {
|
|
25
|
+
input: number;
|
|
26
|
+
output: number;
|
|
27
|
+
cache_read: number;
|
|
28
|
+
};
|
|
29
|
+
"composer-1.5": {
|
|
30
|
+
input: number;
|
|
31
|
+
output: number;
|
|
32
|
+
cache_read: number;
|
|
33
|
+
};
|
|
34
|
+
"composer-2": {
|
|
35
|
+
input: number;
|
|
36
|
+
output: number;
|
|
37
|
+
cache_read: number;
|
|
38
|
+
};
|
|
39
|
+
"composer-2-fast": {
|
|
40
|
+
input: number;
|
|
41
|
+
output: number;
|
|
42
|
+
cache_read: number;
|
|
43
|
+
};
|
|
44
|
+
"composer-2.5": {
|
|
45
|
+
input: number;
|
|
46
|
+
output: number;
|
|
47
|
+
cache_read: number;
|
|
48
|
+
};
|
|
49
|
+
"composer-2.5-fast": {
|
|
50
|
+
input: number;
|
|
51
|
+
output: number;
|
|
52
|
+
cache_read: number;
|
|
53
|
+
};
|
|
54
|
+
"grok-4.5": {
|
|
55
|
+
input: number;
|
|
56
|
+
output: number;
|
|
57
|
+
cache_read: number;
|
|
58
|
+
};
|
|
59
|
+
"grok-4.5-fast": {
|
|
60
|
+
input: number;
|
|
61
|
+
output: number;
|
|
62
|
+
cache_read: number;
|
|
63
|
+
};
|
|
64
|
+
"grok-4.6": {
|
|
65
|
+
input: number;
|
|
66
|
+
output: number;
|
|
67
|
+
cache_read: number;
|
|
68
|
+
};
|
|
69
|
+
"grok-4.6-fast": {
|
|
70
|
+
input: number;
|
|
71
|
+
output: number;
|
|
72
|
+
cache_read: number;
|
|
73
|
+
};
|
|
74
|
+
"grok-4.7": {
|
|
75
|
+
input: number;
|
|
76
|
+
output: number;
|
|
77
|
+
cache_read: number;
|
|
78
|
+
};
|
|
79
|
+
"grok-4.7-fast": {
|
|
80
|
+
input: number;
|
|
81
|
+
output: number;
|
|
82
|
+
cache_read: number;
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
type CursorLocalPricingModel = keyof typeof CURSOR_LOCAL_PRICING;
|
|
86
|
+
export declare const CURSOR_OFFICIAL_MODEL_ALIASES: Readonly<Record<string, {
|
|
87
|
+
providerHint: string;
|
|
88
|
+
modelHint: string;
|
|
89
|
+
}>>;
|
|
90
|
+
export declare function isCursorProviderId(raw?: string): boolean;
|
|
91
|
+
export declare function isCursorModelId(raw?: string): boolean;
|
|
92
|
+
export declare function getCursorPlanDisplayName(plan: CursorQuotaPlan): string | null;
|
|
93
|
+
export declare function getEffectiveCursorIncludedApiUsd(params: {
|
|
94
|
+
plan: CursorQuotaPlan;
|
|
95
|
+
overrideUsd?: number;
|
|
96
|
+
}): number | undefined;
|
|
97
|
+
export declare function lookupCursorLocalCost(model: string): CostBuckets | null;
|
|
98
|
+
/** Whether Cursor bills a request of `rawModelId` with `promptTokens` of prompt at base rates. */
|
|
99
|
+
export declare function cursorBaseRatesCoverPrompt(rawModelId: string | undefined, promptTokens: number): boolean;
|
|
100
|
+
/**
|
|
101
|
+
* `rates` as OpenCode bills a Cursor model through cursor-opencode-provider: a cache write costs
|
|
102
|
+
* Cursor's cache-write rate, nothing when Cursor publishes none. The provider settles each turn
|
|
103
|
+
* at what Cursor billed partly with such free cache writes.
|
|
104
|
+
*/
|
|
105
|
+
export declare function cursorBilledRates(rawModelId: string | undefined, rates: CostBuckets): CostBuckets;
|
|
106
|
+
export declare function resolveCursorModel(rawModelId?: string): CursorResolvedModel;
|
|
107
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared display sanitization for user-visible output.
|
|
3
|
+
*
|
|
4
|
+
* Strips ANSI escape sequences and other control characters so that
|
|
5
|
+
* remote/provider error text cannot inject terminal control codes into
|
|
6
|
+
* toasts or transcript output.
|
|
7
|
+
*/
|
|
8
|
+
export declare function sanitizeDisplayText(text: string): string;
|
|
9
|
+
export declare function sanitizeSingleLineDisplayText(text: string): string;
|
|
10
|
+
export declare function sanitizeDisplaySnippet(text: string, maxLength: number): string;
|
|
11
|
+
export declare function sanitizeSingleLineDisplaySnippet(text: string, maxLength: number): string;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The provider interface and what a provider is given. Providers return the row contract
|
|
3
|
+
* (`ProviderFetchResult`, see `providers/contract.ts`).
|
|
4
|
+
*/
|
|
5
|
+
import type { ProviderFetchResult } from "../providers/contract.js";
|
|
6
|
+
import type { CursorQuotaPlan } from "./cursor-pricing.js";
|
|
7
|
+
import type { QuotaProviderDefinition } from "./quota-providers.js";
|
|
8
|
+
import type { ResolvedAuthIdentity } from "./resolved-auth-identity.js";
|
|
9
|
+
import type { RuntimeProviderIdResolver } from "./runtime-provider-ids.js";
|
|
10
|
+
/** When the current fixed quota window started, as its provider reports it. */
|
|
11
|
+
export interface FixedWindowProjectionEvidence {
|
|
12
|
+
startedAtIso: string;
|
|
13
|
+
}
|
|
14
|
+
export interface QuotaProviderMatchContext {
|
|
15
|
+
enabledProviders: string[] | "auto";
|
|
16
|
+
automaticDiscovery?: boolean;
|
|
17
|
+
quotaProviders?: QuotaProviderDefinition[];
|
|
18
|
+
currentProviderID?: string;
|
|
19
|
+
}
|
|
20
|
+
export interface QuotaProviderContext {
|
|
21
|
+
client: {
|
|
22
|
+
config: {
|
|
23
|
+
providers: () => Promise<{
|
|
24
|
+
data?: {
|
|
25
|
+
providers: Array<{
|
|
26
|
+
id: string;
|
|
27
|
+
}>;
|
|
28
|
+
};
|
|
29
|
+
}>;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
resolveRuntimeProviderIds: RuntimeProviderIdResolver;
|
|
33
|
+
/**
|
|
34
|
+
* The project folder quota is computed for: the git root of the OpenCode location, or the
|
|
35
|
+
* location itself. The terminal command computes quota for the home folder.
|
|
36
|
+
*/
|
|
37
|
+
workspaceRoot: string;
|
|
38
|
+
/**
|
|
39
|
+
* The folder OpenCode runs the session in: OpenCode's config files that count are found from
|
|
40
|
+
* here upward. The terminal command uses the folder it runs in.
|
|
41
|
+
*/
|
|
42
|
+
locationDirectory: string;
|
|
43
|
+
config: {
|
|
44
|
+
anthropicBinaryPath?: string;
|
|
45
|
+
cursorPlan: CursorQuotaPlan;
|
|
46
|
+
cursorIncludedApiUsd?: number;
|
|
47
|
+
cursorBillingCycleStartDay?: number;
|
|
48
|
+
opencodeMonthlyLimit?: number;
|
|
49
|
+
requestTimeoutMs?: number;
|
|
50
|
+
/** True when the settings file sets providers.checking.requestTimeoutMs (not its default). */
|
|
51
|
+
requestTimeoutMsConfigured?: boolean;
|
|
52
|
+
onlyCurrentModel?: boolean;
|
|
53
|
+
currentModel?: string;
|
|
54
|
+
currentProviderID?: string;
|
|
55
|
+
/** The registered providers not turned off in the settings. */
|
|
56
|
+
enabledProviders: string[];
|
|
57
|
+
/** The list filters automatic discovery, rather than explicitly enrolling providers. */
|
|
58
|
+
automaticDiscovery?: boolean;
|
|
59
|
+
quotaProviders?: QuotaProviderDefinition[];
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
export interface QuotaProviderCacheContext {
|
|
63
|
+
runtimeEligibleQuotaProviders?: readonly QuotaProviderDefinition[];
|
|
64
|
+
}
|
|
65
|
+
export type QuotaProviderCachePolicy = {
|
|
66
|
+
kind: "account-neutral";
|
|
67
|
+
} | {
|
|
68
|
+
kind: "uncached";
|
|
69
|
+
} | {
|
|
70
|
+
kind: "resolved-auth";
|
|
71
|
+
resolveIdentity: (ctx: QuotaProviderContext, cacheContext: QuotaProviderCacheContext) => Promise<ResolvedAuthIdentity | null>;
|
|
72
|
+
};
|
|
73
|
+
export interface QuotaProvider {
|
|
74
|
+
/** Stable id used by config.enabledProviders */
|
|
75
|
+
id: string;
|
|
76
|
+
/** Best-effort availability check (no network if possible) */
|
|
77
|
+
isAvailable: (ctx: QuotaProviderContext) => Promise<boolean>;
|
|
78
|
+
/** Fetch quota as the row contract. */
|
|
79
|
+
fetch: (ctx: QuotaProviderContext, cacheContext?: QuotaProviderCacheContext) => Promise<ProviderFetchResult>;
|
|
80
|
+
/**
|
|
81
|
+
* Shared-cache safety policy: how the store tells the signed-in accounts apart. Every
|
|
82
|
+
* provider names its own; account-neutral caching must be explicit.
|
|
83
|
+
*/
|
|
84
|
+
cachePolicy: QuotaProviderCachePolicy;
|
|
85
|
+
/** Optional provider match for onlyCurrentModel filtering */
|
|
86
|
+
matchesCurrentModel?: (model: string, context?: QuotaProviderMatchContext) => boolean;
|
|
87
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Environment variable template resolution.
|
|
3
|
+
*
|
|
4
|
+
* Supports {env:VAR_NAME} syntax for referencing environment variables in config values.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Resolve {env:VAR_NAME} syntax in a string value.
|
|
8
|
+
*
|
|
9
|
+
* If the value matches the pattern `{env:VAR_NAME}`, looks up the environment
|
|
10
|
+
* variable and returns its trimmed value. Returns null if the env var is
|
|
11
|
+
* not set or is empty/whitespace-only.
|
|
12
|
+
*
|
|
13
|
+
* If the value does not match the pattern, returns the original value unchanged.
|
|
14
|
+
*
|
|
15
|
+
* @param value - The string value to resolve
|
|
16
|
+
* @param allowedEnvVars - Optional allowlist for env vars referenced by templates
|
|
17
|
+
* @returns The resolved value, or null if env var is missing/empty/disallowed
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* // With OPENAI_API_KEY="sk-123" in environment:
|
|
21
|
+
* resolveEnvTemplate("{env:OPENAI_API_KEY}") // => "sk-123"
|
|
22
|
+
* resolveEnvTemplate("{env:MISSING_VAR}") // => null
|
|
23
|
+
* resolveEnvTemplate("literal-value") // => "literal-value"
|
|
24
|
+
*/
|
|
25
|
+
export declare function resolveEnvTemplate(value: string, allowedEnvVars?: readonly string[]): string | null;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The lock was taken over, or what the work read changed, while it ran: nothing was written, and
|
|
3
|
+
* the work runs again.
|
|
4
|
+
*/
|
|
5
|
+
export declare class LockLostError extends Error {
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Throws when the lock no longer holds our token. Call it right before the write that commits
|
|
9
|
+
* (the atomic rename over the file).
|
|
10
|
+
*/
|
|
11
|
+
export type LockFence = () => Promise<void>;
|
|
12
|
+
/**
|
|
13
|
+
* Runs `work` while holding the lock file `lock`, making its folder (mode 700) first. `work`
|
|
14
|
+
* calls the fence it gets right before it commits; when the lock was lost, the work runs again
|
|
15
|
+
* under a new lock. After `waitMs` without the lock it fails with `busyMessage`.
|
|
16
|
+
*/
|
|
17
|
+
export declare function withFileLock<T>(lock: string, work: (fence: LockFence) => Promise<T>, options: {
|
|
18
|
+
waitMs: number;
|
|
19
|
+
busyMessage: string;
|
|
20
|
+
}): Promise<T>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Small text helpers for the providers: percents and dollar amounts. */
|
|
2
|
+
/**
|
|
3
|
+
* Clamp a value to a percentage [0..100], rounding to the nearest integer.
|
|
4
|
+
* Returns 0 for non-finite inputs.
|
|
5
|
+
*/
|
|
6
|
+
export declare function clampPercent(n: number): number;
|
|
7
|
+
/** A dollar amount with two decimals ("$12.50"); "$0.00" for a non-finite number. */
|
|
8
|
+
export declare function fmtUsdAmount(n: number): string;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shows the home dir as "~" so pasted reports do not reveal the computer's user name:
|
|
3
|
+
* "/Users/alice/.config" -> "~/.config", "/Users/alice" -> "~". Only whole home-dir
|
|
4
|
+
* matches change; "/Users/alicesmith" stays as it is. Display only, never for reading files.
|
|
5
|
+
*/
|
|
6
|
+
export declare function replaceHomeDirWithTilde(text: string, homeDir: string): string;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP for provider API calls: `requestJson` asks for one JSON reply within a timeout and a
|
|
3
|
+
* size cap, and says why it got none, with the caller's own secrets redacted.
|
|
4
|
+
*/
|
|
5
|
+
import { type ErrorFacts } from "../core/errors.js";
|
|
6
|
+
import type { QuotaErrorCode } from "../core/types.js";
|
|
7
|
+
/** Request timeout in milliseconds. */
|
|
8
|
+
export declare const REQUEST_TIMEOUT_MS = 5000;
|
|
9
|
+
type FetchWithTimeoutOptions<T> = {
|
|
10
|
+
request: Omit<RequestInit, "signal">;
|
|
11
|
+
timeoutMs?: number;
|
|
12
|
+
fetchFn?: typeof fetch;
|
|
13
|
+
consume: (response: Response, timeoutSignal: AbortSignal) => Promise<T> | T;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Fetch and consume a response within one timeout.
|
|
17
|
+
*
|
|
18
|
+
* The response consumer must complete all status handling, body reads, and parsing
|
|
19
|
+
* before returning so the request signal remains active for the full transaction.
|
|
20
|
+
*
|
|
21
|
+
* @throws Error with message "Request timeout after Xs" if the transaction times out
|
|
22
|
+
*/
|
|
23
|
+
export declare function fetchWithTimeout<T>(url: string, options: FetchWithTimeoutOptions<T>): Promise<T>;
|
|
24
|
+
/**
|
|
25
|
+
* Why a request gave no JSON. `http`: a reply that is not 2xx (a redirect too, with
|
|
26
|
+
* `redirect: "manual"`), whatever happened to its body; `timeout`; `network`: no reply;
|
|
27
|
+
* `too_large`: a 2xx reply over `maxBytes`; `invalid_json`: a 2xx reply that is not JSON.
|
|
28
|
+
*/
|
|
29
|
+
export type RequestFailure = {
|
|
30
|
+
kind: "http" | "timeout" | "network" | "too_large" | "invalid_json";
|
|
31
|
+
/**
|
|
32
|
+
* The error code: 401 is `login_expired`, 429 `rate_limited`, any other non-2xx
|
|
33
|
+
* `provider_error`; `timeout`; `network`; a reply too large or not JSON `bad_response`.
|
|
34
|
+
*/
|
|
35
|
+
code: QuotaErrorCode;
|
|
36
|
+
/** When a non-2xx reply's Retry-After says to try again. */
|
|
37
|
+
retryAtMs?: number;
|
|
38
|
+
/** The reply's status and headers, whenever a reply arrived. */
|
|
39
|
+
status?: number;
|
|
40
|
+
headers?: Headers;
|
|
41
|
+
/**
|
|
42
|
+
* For `http`, the reply body (empty when it could not be read); else the error message.
|
|
43
|
+
* Control characters removed and the caller's secrets replaced by "[redacted]"; not cut.
|
|
44
|
+
*/
|
|
45
|
+
detail: string;
|
|
46
|
+
};
|
|
47
|
+
export type JsonReply = {
|
|
48
|
+
ok: true;
|
|
49
|
+
status: number;
|
|
50
|
+
headers: Headers;
|
|
51
|
+
json: unknown;
|
|
52
|
+
} | {
|
|
53
|
+
ok: false;
|
|
54
|
+
failure: RequestFailure;
|
|
55
|
+
};
|
|
56
|
+
type RequestJsonOptions = {
|
|
57
|
+
method?: "GET" | "POST";
|
|
58
|
+
headers?: Record<string, string>;
|
|
59
|
+
body?: string | URLSearchParams;
|
|
60
|
+
redirect?: RequestInit["redirect"];
|
|
61
|
+
/** Values never shown in `detail`: the key, token or cookie the request carries. */
|
|
62
|
+
secrets?: ReadonlyArray<string | undefined>;
|
|
63
|
+
timeoutMs?: number;
|
|
64
|
+
maxBytes?: number;
|
|
65
|
+
/** An empty or blank 2xx body reads as JSON null instead of `invalid_json`. */
|
|
66
|
+
blankAsNull?: boolean;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* A Retry-After header as a wait: delta-seconds or an HTTP date, at most `RETRY_WAIT_MAX_MS`;
|
|
70
|
+
* none when absent, past or unreadable.
|
|
71
|
+
*/
|
|
72
|
+
export declare function parseRetryAfterMs(value: string | null, nowMs?: number): number | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* The facts the error vocabulary keeps of a failure: its code, the HTTP status of a non-2xx
|
|
75
|
+
* reply, and its Retry-After. `code` replaces the failure's own when the caller's words mean
|
|
76
|
+
* another (a 3xx that means "sign in again").
|
|
77
|
+
*/
|
|
78
|
+
export declare function failureFacts(failure: RequestFailure, code?: QuotaErrorCode): ErrorFacts;
|
|
79
|
+
/** ": <words>" to end an "<api> error <status>" text; nothing when the reply had no words. */
|
|
80
|
+
export declare function errorWords(words: string): string;
|
|
81
|
+
/** A failed request as a thrown error, for callers that throw; its facts stay with it. */
|
|
82
|
+
export declare class RequestFailedError extends Error {
|
|
83
|
+
readonly facts: ErrorFacts;
|
|
84
|
+
constructor(message: string, facts: ErrorFacts);
|
|
85
|
+
}
|
|
86
|
+
/** The facts of a thrown request failure; none for any other error. */
|
|
87
|
+
export declare function thrownFacts(error: unknown): ErrorFacts | undefined;
|
|
88
|
+
/**
|
|
89
|
+
* One JSON request: the parsed 2xx reply, or why there is none. Never throws. A non-2xx reply is
|
|
90
|
+
* always `http` with its status and Retry-After; its body is the detail only when it was read
|
|
91
|
+
* (not too large, not stalled past the deadline). A 2xx reply whose body stalls past the
|
|
92
|
+
* deadline is a `timeout` that still has its status and headers.
|
|
93
|
+
*/
|
|
94
|
+
export declare function requestJson(url: string, options?: RequestJsonOptions): Promise<JsonReply>;
|
|
95
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Small guards for reading untyped JSON (API replies, config files, stored logins). */
|
|
2
|
+
/** A non-null object that is not an array. */
|
|
3
|
+
export declare function isRecord(value: unknown): value is Record<string, unknown>;
|
|
4
|
+
export declare function asRecord(value: unknown): Record<string, unknown> | null;
|
|
5
|
+
/** The trimmed string, or undefined when it is not a string or only whitespace. */
|
|
6
|
+
export declare function nonEmptyString(value: unknown): string | undefined;
|
|
7
|
+
/** The number, or undefined when it is not a finite number (strings are not parsed). */
|
|
8
|
+
export declare function finiteNumber(value: unknown): number | undefined;
|
|
9
|
+
/** True when every key of `value` is one of `allowed`. */
|
|
10
|
+
export declare function hasOnlyKeys(value: Record<string, unknown>, allowed: readonly string[]): boolean;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { Settings } from "../settings/schema.js";
|
|
2
|
+
type PricingSettings = Settings["providers"]["pricing"];
|
|
3
|
+
/** Which price list reports use: "auto" (runtime when valid, else bundled), or one of them. */
|
|
4
|
+
export type PricingSnapshotSource = PricingSettings["source"];
|
|
5
|
+
export type CostBuckets = {
|
|
6
|
+
input?: number;
|
|
7
|
+
output?: number;
|
|
8
|
+
cache_read?: number;
|
|
9
|
+
cache_write?: number;
|
|
10
|
+
reasoning?: number;
|
|
11
|
+
};
|
|
12
|
+
type PricingSnapshot = {
|
|
13
|
+
_meta: {
|
|
14
|
+
source: string;
|
|
15
|
+
generatedAt: number;
|
|
16
|
+
providers: string[];
|
|
17
|
+
units: string;
|
|
18
|
+
};
|
|
19
|
+
providers: Record<string, Record<string, CostBuckets>>;
|
|
20
|
+
};
|
|
21
|
+
interface PricingRefreshStateV1 {
|
|
22
|
+
version: 1;
|
|
23
|
+
updatedAt: number;
|
|
24
|
+
lastAttemptAt?: number;
|
|
25
|
+
lastSuccessAt?: number;
|
|
26
|
+
lastFailureAt?: number;
|
|
27
|
+
lastResult?: "success" | "not_modified" | "skipped_fresh" | "skipped_throttled" | "failed";
|
|
28
|
+
lastError?: string;
|
|
29
|
+
etag?: string;
|
|
30
|
+
lastModified?: string;
|
|
31
|
+
}
|
|
32
|
+
type ListSource = "runtime" | "bundled" | "empty";
|
|
33
|
+
/** Everything the status report and `status --json` say about the price list. */
|
|
34
|
+
export interface PriceListDescription {
|
|
35
|
+
/** The settings' choice. */
|
|
36
|
+
selection: PricingSnapshotSource;
|
|
37
|
+
/** The list in use: downloaded, shipped, or none readable. */
|
|
38
|
+
source: ListSource;
|
|
39
|
+
meta: PricingSnapshot["_meta"];
|
|
40
|
+
/** The list's age against the days the settings allow. */
|
|
41
|
+
health: {
|
|
42
|
+
ageMs: number;
|
|
43
|
+
maxAgeMs: number;
|
|
44
|
+
stale: boolean;
|
|
45
|
+
};
|
|
46
|
+
paths: {
|
|
47
|
+
snapshot: string;
|
|
48
|
+
refreshState: string;
|
|
49
|
+
};
|
|
50
|
+
refreshState: PricingRefreshStateV1 | null;
|
|
51
|
+
/** The ids of the list shipped in this package, never a downloaded one. */
|
|
52
|
+
bundledProviderIds: ReadonlySet<string>;
|
|
53
|
+
/** The model ids of the shipped list, under any provider. */
|
|
54
|
+
bundledModelIds: ReadonlySet<string>;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Follows the pricing settings `current` gives, read each time they matter (which list and how
|
|
58
|
+
* many days old it may get before a download), until the returned function unbinds them.
|
|
59
|
+
* Unbinding removes only this binding; the last binding still bound wins, else "auto", 7 days.
|
|
60
|
+
*/
|
|
61
|
+
export declare function usePricingSettings(current: () => PricingSettings): () => void;
|
|
62
|
+
/**
|
|
63
|
+
* Starts a models.dev download when the list is due for one (see `refresh`) and waits for it at
|
|
64
|
+
* most 0.75 s; a slower download goes on in the background. The wait's timer ends with the wait,
|
|
65
|
+
* so it never keeps the process running.
|
|
66
|
+
*/
|
|
67
|
+
export declare function refreshPricesIfDue(): Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* Downloads the price list from models.dev now, whatever its age. With the settings on the
|
|
70
|
+
* bundled list, the download is kept but lookups keep using the bundled one.
|
|
71
|
+
*/
|
|
72
|
+
export declare function downloadPrices(): Promise<{
|
|
73
|
+
ok: boolean;
|
|
74
|
+
message: string;
|
|
75
|
+
}>;
|
|
76
|
+
/** The list lookups use now: the settings' choice, the list it gave and its header. */
|
|
77
|
+
export declare function activePriceList(): Pick<PriceListDescription, "selection" | "source" | "meta">;
|
|
78
|
+
export declare function describePriceList(): Promise<PriceListDescription>;
|
|
79
|
+
/** A model's prices; own keys only, so "constructor" or "__proto__" is never a model. */
|
|
80
|
+
export declare function lookupCost(providerId: string, modelId: string): CostBuckets | null;
|
|
81
|
+
export declare function hasProvider(providerId: string): boolean;
|
|
82
|
+
export declare function hasModel(providerId: string, modelId: string): boolean;
|
|
83
|
+
export declare function listProvidersForModelId(modelId: string): string[];
|
|
84
|
+
export declare function __resetPricingSnapshotForTests(): void;
|
|
85
|
+
export {};
|