@modelprofile.com/authswitch 6.2.0 → 6.4.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.
Files changed (50) hide show
  1. package/dist_ts/00_commitinfo_data.js +1 -1
  2. package/dist_ts/authority-contract.d.ts +142 -0
  3. package/dist_ts/authority-contract.js +3 -0
  4. package/dist_ts/authority-runtime-contract.d.ts +32 -0
  5. package/dist_ts/authority-runtime-contract.js +2 -0
  6. package/dist_ts/classes.authoritybroker.d.ts +73 -0
  7. package/dist_ts/classes.authoritybroker.js +548 -0
  8. package/dist_ts/classes.authorityclient.d.ts +34 -0
  9. package/dist_ts/classes.authorityclient.js +200 -0
  10. package/dist_ts/classes.authoritydaemon.d.ts +26 -0
  11. package/dist_ts/classes.authoritydaemon.js +249 -0
  12. package/dist_ts/classes.authoritydatabase.d.ts +94 -0
  13. package/dist_ts/classes.authoritydatabase.js +736 -0
  14. package/dist_ts/classes.authorityframing.d.ts +8 -0
  15. package/dist_ts/classes.authorityframing.js +22 -0
  16. package/dist_ts/classes.authoritymodels.d.ts +215 -0
  17. package/dist_ts/classes.authoritymodels.js +818 -0
  18. package/dist_ts/classes.authoritysecrets.d.ts +9 -0
  19. package/dist_ts/classes.authoritysecrets.js +30 -0
  20. package/dist_ts/classes.authorityservice.d.ts +28 -0
  21. package/dist_ts/classes.authorityservice.js +71 -0
  22. package/dist_ts/classes.cli.d.ts +56 -0
  23. package/dist_ts/classes.cli.js +238 -63
  24. package/dist_ts/classes.codexpreuse.js +2 -2
  25. package/dist_ts/classes.tui.js +51 -2
  26. package/dist_ts/index.d.ts +2 -0
  27. package/dist_ts/index.js +26 -1
  28. package/dist_ts/plugins.d.ts +9 -2
  29. package/dist_ts/plugins.js +10 -3
  30. package/dist_ts/preuse.d.ts +89 -2
  31. package/dist_ts/preuse.js +107 -2
  32. package/package.json +17 -3
  33. package/readme.md +118 -16
  34. package/ts/00_commitinfo_data.ts +1 -1
  35. package/ts/authority-contract.ts +109 -0
  36. package/ts/authority-runtime-contract.ts +27 -0
  37. package/ts/classes.authoritybroker.ts +530 -0
  38. package/ts/classes.authorityclient.ts +183 -0
  39. package/ts/classes.authoritydaemon.ts +219 -0
  40. package/ts/classes.authoritydatabase.ts +725 -0
  41. package/ts/classes.authorityframing.ts +21 -0
  42. package/ts/classes.authoritymodels.ts +367 -0
  43. package/ts/classes.authoritysecrets.ts +27 -0
  44. package/ts/classes.authorityservice.ts +83 -0
  45. package/ts/classes.cli.ts +236 -54
  46. package/ts/classes.codexpreuse.ts +1 -1
  47. package/ts/classes.tui.ts +39 -2
  48. package/ts/index.ts +25 -0
  49. package/ts/plugins.ts +9 -2
  50. package/ts/preuse.ts +141 -3
package/ts/preuse.ts CHANGED
@@ -1,9 +1,19 @@
1
- import type { IHarnessPreuseOptions } from './interfaces.harness.js';
1
+ import { orderedUsageWindows } from './accounts.js';
2
+ import type { IAuthHarness, IHarnessAccount, IHarnessPreuseOptions, IHarnessPreuseResult, IHarnessUsageWindow } from './interfaces.harness.js';
2
3
 
3
4
  export const defaultPreusePrompt = 'Write 2000 words about strawberries.';
4
5
 
5
- /** Only fixed local messages may cross the inference error boundary. */
6
- export class PreuseError extends Error {}
6
+ /**
7
+ * Only fixed local messages may cross the inference error boundary.
8
+ *
9
+ * `requestStarted` says whether the prompt may already have reached the provider. False is a refusal that
10
+ * sent nothing -- an unsupported login, a missing credential, a model catalog that could not be read before
11
+ * the prompt -- so the account's quota is untouched. True, and any error that is not a `PreuseError` at all,
12
+ * means tokens may have been consumed, and such a request is never repeated automatically.
13
+ */
14
+ export class PreuseError extends Error {
15
+ constructor(messageArg: string, public readonly requestStarted = false) { super(messageArg); }
16
+ }
7
17
 
8
18
  export const validatePreuseOptions = (optionsArg: IHarnessPreuseOptions): void => {
9
19
  if (typeof optionsArg.prompt !== 'string' || !optionsArg.prompt.trim() || Buffer.byteLength(optionsArg.prompt, 'utf8') > 16_384) {
@@ -13,3 +23,131 @@ export const validatePreuseOptions = (optionsArg: IHarnessPreuseOptions): void =
13
23
  throw new PreuseError('The preuse model must be a valid model identifier of at most 128 characters.');
14
24
  }
15
25
  };
26
+
27
+ /**
28
+ * What one account's turn in a run did.
29
+ *
30
+ * `skipped` is a refusal that sent nothing and consumed nothing, with the reason the adapter stated.
31
+ * `failed` and `interrupted` may both have consumed tokens, so neither is ever retried by the runner.
32
+ */
33
+ export type TPreuseOutcome = 'completed' | 'skipped' | 'failed' | 'interrupted';
34
+
35
+ /**
36
+ * The reset schedule read after a prompt completed.
37
+ *
38
+ * `reported` carries what the provider answered, which may be no window at all; `unavailable` is a lookup
39
+ * that failed, and `notRead` is a run the user interrupted before it asked. None of them says anything
40
+ * about the completed prompt, which is never repeated because its schedule could not be confirmed.
41
+ */
42
+ export type TPreuseSchedule =
43
+ | { kind: 'reported'; windows: readonly IHarnessUsageWindow[] }
44
+ | { kind: 'unavailable' }
45
+ | { kind: 'notRead' };
46
+
47
+ export type TPreuseAccountResult =
48
+ | { account: IHarnessAccount; outcome: 'completed'; result: IHarnessPreuseResult; schedule: TPreuseSchedule }
49
+ | { account: IHarnessAccount; outcome: Exclude<TPreuseOutcome, 'completed'>; reason: string };
50
+
51
+ /** One account starting, and that account's result. A run reports nothing else, and never the generated prose. */
52
+ export type TPreuseRunEvent =
53
+ | { kind: 'started'; index: number; total: number; account: IHarnessAccount }
54
+ | { kind: 'finished'; index: number; total: number; result: TPreuseAccountResult };
55
+
56
+ export interface IPreuseRunSummary {
57
+ results: readonly TPreuseAccountResult[];
58
+ counts: Readonly<Record<TPreuseOutcome, number>>;
59
+ /** Accounts an abort kept the run from reaching. Nothing was sent for them. */
60
+ notRun: readonly IHarnessAccount[];
61
+ }
62
+
63
+ /**
64
+ * Every account a run may preuse, in the order the harness lists them, each at most once.
65
+ *
66
+ * A harness reports an active login and that login's saved record as one account -- Codex keys both by
67
+ * email, the file harness merges the active login into its saved record by account id -- so the walk is
68
+ * the account list itself. The seen set keeps a repeated id from being charged twice regardless.
69
+ */
70
+ export const preuseTargets = (accountsArg: readonly IHarnessAccount[]): IHarnessAccount[] => {
71
+ const seen = new Set<string>();
72
+ const targets: IHarnessAccount[] = [];
73
+ for (const account of accountsArg) {
74
+ if (seen.has(account.id)) continue;
75
+ seen.add(account.id);
76
+ targets.push(account);
77
+ }
78
+ return targets;
79
+ };
80
+
81
+ /**
82
+ * A run's counts in one line, for a command's closing line and for the dashboard footer.
83
+ *
84
+ * Completed accounts are always named, including none of them; every other count appears only when it
85
+ * happened, so a clean run reads as one fact rather than as four.
86
+ */
87
+ export const preuseCountsText = (summaryArg: IPreuseRunSummary): string => [
88
+ `${summaryArg.counts.completed} completed`,
89
+ ...(summaryArg.counts.skipped ? [`${summaryArg.counts.skipped} skipped`] : []),
90
+ ...(summaryArg.counts.failed ? [`${summaryArg.counts.failed} failed`] : []),
91
+ ...(summaryArg.counts.interrupted ? [`${summaryArg.counts.interrupted} interrupted`] : []),
92
+ ...(summaryArg.notRun.length ? [`${summaryArg.notRun.length} not run`] : []),
93
+ ].join(', ');
94
+
95
+ /** An adapter's own fixed message, or the fixed local text for an error that crossed the boundary unnamed. */
96
+ const preuseReason = (errorArg: unknown): string => errorArg instanceof PreuseError
97
+ ? errorArg.message
98
+ : 'Preuse failed. Tokens may already have been consumed; check account status before trying again.';
99
+
100
+ /**
101
+ * The account's reset schedule, read after its prompt completed, exactly as the single-account command reads it.
102
+ *
103
+ * The read never activates or writes a login, and its failure never turns a completed prompt into a failed
104
+ * one: there is nothing to undo and nothing worth repeating. An interrupted run does not ask at all.
105
+ */
106
+ const readPreuseSchedule = async (harnessArg: IAuthHarness, accountArg: IHarnessAccount, signalArg: AbortSignal): Promise<TPreuseSchedule> => {
107
+ if (signalArg.aborted) return { kind: 'notRead' };
108
+ try {
109
+ const status = await harnessArg.readAccountStatus(accountArg.id, { signal: signalArg });
110
+ return { kind: 'reported', windows: orderedUsageWindows(status.summary?.usageWindows) };
111
+ } catch { return { kind: 'unavailable' }; }
112
+ };
113
+
114
+ /**
115
+ * Send one preuse prompt through each account in turn, and report what each one did.
116
+ *
117
+ * Accounts run strictly one after another, each with exactly one inference request and no retry, because
118
+ * every request costs the account's own quota. An aborted signal stops the run before the next account
119
+ * starts, and an account already in flight is reported as interrupted: its tokens may be gone either way,
120
+ * which is why nothing is repeated. Nothing is switched, refreshed, stopped or written anywhere.
121
+ */
122
+ export const preuseAccounts = async (
123
+ harnessArg: IAuthHarness,
124
+ accountsArg: readonly IHarnessAccount[],
125
+ optionsArg: { prompt: string; model?: string; signal: AbortSignal },
126
+ reportArg: (eventArg: TPreuseRunEvent) => void,
127
+ ): Promise<IPreuseRunSummary> => {
128
+ const preuseAccount = harnessArg.preuseAccount?.bind(harnessArg);
129
+ if (!preuseAccount) throw new PreuseError('This harness does not support preuse.');
130
+ validatePreuseOptions(optionsArg);
131
+ const results: TPreuseAccountResult[] = [];
132
+ const counts: Record<TPreuseOutcome, number> = { completed: 0, skipped: 0, failed: 0, interrupted: 0 };
133
+ const notRun: IHarnessAccount[] = [];
134
+ const total = accountsArg.length;
135
+ for (const [index, account] of accountsArg.entries()) {
136
+ if (optionsArg.signal.aborted) { notRun.push(account); continue; }
137
+ reportArg({ kind: 'started', index, total, account });
138
+ let result: TPreuseAccountResult;
139
+ try {
140
+ const completed = await preuseAccount(account.id, { prompt: optionsArg.prompt, model: optionsArg.model, signal: optionsArg.signal });
141
+ result = { account, outcome: 'completed', result: completed, schedule: await readPreuseSchedule(harnessArg, account, optionsArg.signal) };
142
+ } catch (error) {
143
+ // A cancelled account is interrupted whether or not its prompt went out; its reason says which.
144
+ const outcome = optionsArg.signal.aborted ? 'interrupted'
145
+ : error instanceof PreuseError && !error.requestStarted ? 'skipped' : 'failed';
146
+ result = { account, outcome, reason: preuseReason(error) };
147
+ }
148
+ results.push(result);
149
+ counts[result.outcome]++;
150
+ reportArg({ kind: 'finished', index, total, result });
151
+ }
152
+ return { results, counts, notRun };
153
+ };