@modelprofile.com/authswitch 9.2.0 → 10.0.0
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/dist_ts/00_commitinfo_data.js +2 -2
- package/dist_ts/authority-contract.d.ts +52 -6
- package/dist_ts/authority-contract.js +1 -1
- package/dist_ts/classes.authoritybroker.d.ts +20 -4
- package/dist_ts/classes.authoritybroker.js +44 -8
- package/dist_ts/classes.authoritycli.d.ts +4 -1
- package/dist_ts/classes.authoritycli.js +42 -7
- package/dist_ts/classes.authorityclient.d.ts +9 -1
- package/dist_ts/classes.authorityclient.js +9 -3
- package/dist_ts/classes.authoritydaemon.js +8 -5
- package/dist_ts/classes.authoritydatabase.d.ts +6 -2
- package/dist_ts/classes.authoritydatabase.js +13 -6
- package/dist_ts/classes.authorityimport.d.ts +4 -4
- package/dist_ts/classes.authorityimport.js +8 -7
- package/dist_ts/classes.authoritymodels.d.ts +11 -3
- package/dist_ts/classes.authoritymodels.js +43 -13
- package/dist_ts/classes.authoritypreuse.d.ts +8 -2
- package/dist_ts/classes.authoritypreuse.js +27 -17
- package/dist_ts/classes.authorityusage.d.ts +4 -3
- package/dist_ts/classes.authorityusage.js +3 -5
- package/dist_ts/classes.cli.js +3 -5
- package/dist_ts/classes.codexpreuse.d.ts +14 -3
- package/dist_ts/classes.codexpreuse.js +127 -44
- package/dist_ts/classes.codexstatus.js +44 -95
- package/dist_ts/classes.login.d.ts +20 -10
- package/dist_ts/classes.login.js +14 -13
- package/dist_ts/classes.openaiprovider.d.ts +53 -0
- package/dist_ts/classes.openaiprovider.js +340 -0
- package/dist_ts/classes.opencodeharness.js +3 -2
- package/dist_ts/classes.service.js +3 -5
- package/dist_ts/index.d.ts +2 -0
- package/dist_ts/index.js +3 -1
- package/dist_ts/interfaces.openai.d.ts +118 -0
- package/dist_ts/interfaces.openai.js +3 -0
- package/dist_ts/openaiauth.d.ts +76 -0
- package/dist_ts/openaiauth.js +371 -0
- package/dist_ts/openaiusage.d.ts +59 -0
- package/dist_ts/openaiusage.js +222 -0
- package/dist_ts/plugins.d.ts +1 -3
- package/dist_ts/plugins.js +2 -4
- package/dist_ts/preuse.d.ts +50 -0
- package/dist_ts/preuse.js +63 -4
- package/dist_ts/ts_migration/legacysources/shared.js +4 -3
- package/openai-codex-license.txt +201 -0
- package/package.json +6 -4
- package/readme.md +86 -10
- package/third-party-notices.md +29 -0
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/authority-contract.ts +56 -8
- package/ts/classes.authoritybroker.ts +52 -13
- package/ts/classes.authoritycli.ts +48 -6
- package/ts/classes.authorityclient.ts +11 -3
- package/ts/classes.authoritydaemon.ts +7 -4
- package/ts/classes.authoritydatabase.ts +14 -6
- package/ts/classes.authorityimport.ts +12 -12
- package/ts/classes.authoritymodels.ts +42 -14
- package/ts/classes.authoritypreuse.ts +30 -17
- package/ts/classes.authorityusage.ts +8 -9
- package/ts/classes.cli.ts +3 -3
- package/ts/classes.codexpreuse.ts +130 -40
- package/ts/classes.codexstatus.ts +40 -73
- package/ts/classes.login.ts +34 -21
- package/ts/classes.openaiprovider.ts +365 -0
- package/ts/classes.opencodeharness.ts +3 -2
- package/ts/classes.service.ts +3 -4
- package/ts/index.ts +2 -0
- package/ts/interfaces.openai.ts +142 -0
- package/ts/openaiauth.ts +422 -0
- package/ts/openaiusage.ts +247 -0
- package/ts/plugins.ts +1 -3
- package/ts/preuse.ts +88 -3
- package/ts/ts_migration/legacysources/shared.ts +4 -3
package/ts/preuse.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import { orderedUsageWindows } from './accounts.js';
|
|
1
|
+
import { localDateTime, orderedUsageWindows, until } from './accounts.js';
|
|
2
2
|
import type { IAuthHarness, IHarnessAccount, IHarnessPreuseOptions, IHarnessPreuseResult, IHarnessUsageWindow } from './interfaces.harness.js';
|
|
3
3
|
|
|
4
4
|
export const defaultPreusePrompt = 'Write 2000 words about strawberries.';
|
|
5
5
|
|
|
6
|
+
/** The close of every message about a prompt that may have reached the provider. */
|
|
7
|
+
export const preuseMayHaveConsumed = 'Tokens may already have been consumed; no automatic retry was made. Check the account status before trying again.';
|
|
8
|
+
|
|
6
9
|
/**
|
|
7
10
|
* Only fixed local messages may cross the inference error boundary.
|
|
8
11
|
*
|
|
@@ -15,6 +18,84 @@ export class PreuseError extends Error {
|
|
|
15
18
|
constructor(messageArg: string, public readonly requestStarted = false) { super(messageArg); }
|
|
16
19
|
}
|
|
17
20
|
|
|
21
|
+
/**
|
|
22
|
+
* A limit that kept a preuse prompt from being answered.
|
|
23
|
+
*
|
|
24
|
+
* `usage_limit` is the account's quota, spent until it resets; `rate_limit` a short throttle. `window` names the
|
|
25
|
+
* window by its length (`weekly`, `five-hour`) and `resetsAt` is an ISO time; either is null when the account's
|
|
26
|
+
* usage reading or the provider's refusal did not state it.
|
|
27
|
+
*/
|
|
28
|
+
export interface IPreuseLimit {
|
|
29
|
+
kind: 'usage_limit' | 'rate_limit';
|
|
30
|
+
window: string | null;
|
|
31
|
+
resetsAt: string | null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* `Weekly usage limit reached, resets in 16h 46min (2026-09-27 05:12)`: human text, so the reset reads in local time.
|
|
36
|
+
* The limit itself keeps its ISO `resetsAt` for receipts and JSON output.
|
|
37
|
+
*/
|
|
38
|
+
export const describePreuseLimit = (limitArg: IPreuseLimit, nowArg: number): string => {
|
|
39
|
+
const name = `${limitArg.window === null ? '' : `${limitArg.window} `}${limitArg.kind === 'usage_limit' ? 'usage' : 'rate'} limit reached`;
|
|
40
|
+
const countdown = limitArg.resetsAt === null ? null : until(limitArg.resetsAt, nowArg);
|
|
41
|
+
const at = localDateTime(limitArg.resetsAt);
|
|
42
|
+
const reset = limitArg.resetsAt === null ? ''
|
|
43
|
+
: countdown === 'due' ? `, due to reset now (${at})` : `, resets in ${countdown} (${at})`;
|
|
44
|
+
return `${name.charAt(0).toUpperCase()}${name.slice(1)}${reset}`;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A preuse a limit stopped. `usage` is the account's own usage reading before anything was sent; `provider`
|
|
49
|
+
* is the provider's refusal of the prompt, which `generationStarted` says came before or after it began to
|
|
50
|
+
* answer: a refusal before its answer consumed nothing.
|
|
51
|
+
*/
|
|
52
|
+
export class PreuseLimitError extends PreuseError {
|
|
53
|
+
constructor(public readonly limit: IPreuseLimit, public readonly source: 'usage' | 'provider',
|
|
54
|
+
public readonly generationStarted: boolean, nowArg: number) {
|
|
55
|
+
const described = describePreuseLimit(limit, nowArg);
|
|
56
|
+
super(source === 'usage' ? `${described}. No preuse prompt was sent.`
|
|
57
|
+
: generationStarted
|
|
58
|
+
? `The provider stopped the answer: ${described}. Tokens may already have been consumed; no automatic retry was made.`
|
|
59
|
+
: `The provider refused the prompt before answering it: ${described}. No tokens were consumed; no automatic retry was made.`,
|
|
60
|
+
source === 'provider');
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Why a sent preuse prompt failed without a limit, and never with the provider's text.
|
|
66
|
+
*
|
|
67
|
+
* `http` is an answer with an HTTP error status; `stream` an error the answer stream reported after it began,
|
|
68
|
+
* with the machine-readable code the provider stated and no status, since its answer was a success; `response` an answer that could not be read;
|
|
69
|
+
* `network` a connection that failed before any answer; `aborted` an abort that was neither the caller's nor the
|
|
70
|
+
* deadline; `unknown` anything else. `status` and `providerCode` are null when the failure did not state them.
|
|
71
|
+
*/
|
|
72
|
+
export interface IPreuseFailure {
|
|
73
|
+
kind: 'http' | 'stream' | 'response' | 'network' | 'aborted' | 'unknown';
|
|
74
|
+
status: number | null;
|
|
75
|
+
providerCode: string | null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** `the provider answered HTTP 503`: the cause of a failed preuse, as the tail of a sentence. */
|
|
79
|
+
export const describePreuseFailure = (failureArg: IPreuseFailure): string => {
|
|
80
|
+
const status = failureArg.status === null ? null : `HTTP ${failureArg.status}`;
|
|
81
|
+
switch (failureArg.kind) {
|
|
82
|
+
case 'http': return `the provider answered ${status ?? 'with an HTTP error'}`;
|
|
83
|
+
case 'stream':
|
|
84
|
+
return `the answer stream reported an error${failureArg.providerCode === null ? '' : ` (code ${failureArg.providerCode})`}`;
|
|
85
|
+
case 'response': return `the provider's answer${status ? ` (${status})` : ''} could not be read`;
|
|
86
|
+
case 'network': return 'no answer arrived: the connection failed';
|
|
87
|
+
case 'aborted': return 'the request was aborted';
|
|
88
|
+
case 'unknown': return 'the request ended in an error that names no cause';
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/** A sent preuse prompt that failed for the stated cause; its tokens may have been consumed. */
|
|
93
|
+
export class PreuseFailureError extends PreuseError {
|
|
94
|
+
constructor(public readonly failure: IPreuseFailure) {
|
|
95
|
+
super(`Preuse did not complete: ${describePreuseFailure(failure)}. ${preuseMayHaveConsumed}`, true);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
18
99
|
export const validatePreuseOptions = (optionsArg: IHarnessPreuseOptions): void => {
|
|
19
100
|
if (typeof optionsArg.prompt !== 'string' || !optionsArg.prompt.trim() || Buffer.byteLength(optionsArg.prompt, 'utf8') > 16_384) {
|
|
20
101
|
throw new PreuseError('The preuse prompt must contain between 1 and 16384 UTF-8 bytes.');
|
|
@@ -92,10 +173,14 @@ export const preuseCountsText = (summaryArg: IPreuseRunSummary): string => [
|
|
|
92
173
|
...(summaryArg.notRun.length ? [`${summaryArg.notRun.length} not run`] : []),
|
|
93
174
|
].join(', ');
|
|
94
175
|
|
|
95
|
-
/**
|
|
176
|
+
/**
|
|
177
|
+
* An adapter's own fixed message, or, for an error that crossed the boundary unnamed, the fixed local text with
|
|
178
|
+
* the error's class name -- never its message, which may carry a provider body or a token.
|
|
179
|
+
*/
|
|
96
180
|
const preuseReason = (errorArg: unknown): string => errorArg instanceof PreuseError
|
|
97
181
|
? errorArg.message
|
|
98
|
-
:
|
|
182
|
+
: `Preuse failed with an error the adapter did not name (${errorArg instanceof Error && /^[A-Za-z]{1,64}$/.test(errorArg.name) ? errorArg.name : 'unknown'}). `
|
|
183
|
+
+ 'Tokens may already have been consumed; check account status before trying again.';
|
|
99
184
|
|
|
100
185
|
/**
|
|
101
186
|
* The account's reset schedule, read after its prompt completed, exactly as the single-account command reads it.
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as plugins from '../../plugins.js';
|
|
2
2
|
import { readCredentialRaw } from '../../classes.credentialstore.js';
|
|
3
|
+
import { parseOpenAiTokenClaims, type IOpenAiTokenClaims } from '../../openaiauth.js';
|
|
3
4
|
import { resolveClaudeNativeHome, type TClaudeNativeHomeResolution } from '../../classes.claudenative.js';
|
|
4
5
|
import type { TAuthSwitchImportProof, TAuthSwitchImportSourceKind,
|
|
5
6
|
TAuthSwitchImportTreatment } from '../../authority-import-contract.js';
|
|
@@ -128,8 +129,8 @@ export interface IOpenAiSourceIdentity {
|
|
|
128
129
|
* claims the token already carries, which is what lets the inventory run offline.
|
|
129
130
|
*/
|
|
130
131
|
export const openAiSourceIdentity = (idToken: string): IOpenAiSourceIdentity | null => {
|
|
131
|
-
let info:
|
|
132
|
-
try { info =
|
|
132
|
+
let info: IOpenAiTokenClaims;
|
|
133
|
+
try { info = parseOpenAiTokenClaims(idToken); }
|
|
133
134
|
catch { return null; }
|
|
134
135
|
if (!info.chatgptUserId || !info.chatgptAccountId) return null;
|
|
135
136
|
return {
|
|
@@ -202,7 +203,7 @@ export const openCodeOpenAiIdentity = (entry: Record<string, unknown>):
|
|
|
202
203
|
/** The expiry claim of an access token, as the broker reads it. Absent or unparsable means unknown. */
|
|
203
204
|
export const accessTokenExpiry = (accessToken: string | null): string | null => {
|
|
204
205
|
if (!accessToken) return null;
|
|
205
|
-
try { return
|
|
206
|
+
try { return parseOpenAiTokenClaims(accessToken).expiresAt ?? null; }
|
|
206
207
|
catch { return null; }
|
|
207
208
|
};
|
|
208
209
|
|