@cortexkit/common-auth 0.2.4 → 0.2.5
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/auth-menu/accounts.d.ts +88 -0
- package/dist/auth-menu/accounts.js +401 -0
- package/dist/auth-menu/ansi.d.ts +24 -0
- package/dist/auth-menu/ansi.js +67 -0
- package/dist/auth-menu/confirm.d.ts +7 -0
- package/dist/auth-menu/confirm.js +21 -0
- package/dist/auth-menu/doctor.d.ts +52 -0
- package/dist/auth-menu/doctor.js +81 -0
- package/dist/auth-menu/index.d.ts +17 -1
- package/dist/auth-menu/index.js +12 -2
- package/dist/auth-menu/login.d.ts +47 -0
- package/dist/auth-menu/login.js +63 -0
- package/dist/auth-menu/menu.d.ts +59 -0
- package/dist/auth-menu/menu.js +73 -0
- package/dist/auth-menu/opencode-v1.d.ts +49 -0
- package/dist/auth-menu/opencode-v1.js +47 -0
- package/dist/auth-menu/select.d.ts +21 -0
- package/dist/auth-menu/select.js +153 -0
- package/dist/auth-menu/terminal.d.ts +43 -0
- package/dist/auth-menu/terminal.js +19 -0
- package/dist/cachekeep/index.d.ts +4 -1
- package/dist/cachekeep/index.js +2 -2
- package/dist/cachekeep/manager.d.ts +214 -0
- package/dist/cachekeep/manager.js +424 -0
- package/dist/cachekeep/window.d.ts +13 -0
- package/dist/cachekeep/window.js +33 -0
- package/dist/dump/index.d.ts +90 -1
- package/dist/dump/index.js +292 -2
- package/package.json +1 -1
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The terminal the menu draws on and reads keys from. The process's own
|
|
3
|
+
* stdin and stdout satisfy it; tests pass a scripted stand-in, so the full
|
|
4
|
+
* key-in, frame-out path runs without a real TTY.
|
|
5
|
+
*/
|
|
6
|
+
export interface MenuTerminal {
|
|
7
|
+
input: MenuInput;
|
|
8
|
+
output: MenuOutput;
|
|
9
|
+
/**
|
|
10
|
+
* Where SIGINT and SIGTERM are heard while a selector holds raw mode, so a
|
|
11
|
+
* kill restores the terminal. Defaults to nothing: raw mode turns Ctrl-C
|
|
12
|
+
* into a key the selector already handles.
|
|
13
|
+
*/
|
|
14
|
+
signals?: MenuSignals;
|
|
15
|
+
}
|
|
16
|
+
export interface MenuInput {
|
|
17
|
+
/** True only on an interactive terminal; anything else gets a plain list. */
|
|
18
|
+
readonly isTTY?: boolean;
|
|
19
|
+
readonly isRaw?: boolean;
|
|
20
|
+
setRawMode(mode: boolean): unknown;
|
|
21
|
+
resume(): unknown;
|
|
22
|
+
pause(): unknown;
|
|
23
|
+
on(event: 'data', listener: (data: Buffer | string) => void): unknown;
|
|
24
|
+
removeListener(event: 'data', listener: (data: Buffer | string) => void): unknown;
|
|
25
|
+
}
|
|
26
|
+
export interface MenuOutput {
|
|
27
|
+
write(text: string): unknown;
|
|
28
|
+
readonly columns?: number;
|
|
29
|
+
readonly rows?: number;
|
|
30
|
+
}
|
|
31
|
+
export interface MenuSignals {
|
|
32
|
+
once(signal: 'SIGINT' | 'SIGTERM', listener: () => void): unknown;
|
|
33
|
+
removeListener(signal: 'SIGINT' | 'SIGTERM', listener: () => void): unknown;
|
|
34
|
+
}
|
|
35
|
+
/** The current process's terminal, with its signals. */
|
|
36
|
+
export declare function processTerminal(): MenuTerminal;
|
|
37
|
+
/**
|
|
38
|
+
* Whether keys can be read one at a time. Only the input is checked, as the
|
|
39
|
+
* plugins' menus always did: output piped to a file still gets the menu.
|
|
40
|
+
*/
|
|
41
|
+
export declare function isInteractive(terminal: MenuTerminal): boolean;
|
|
42
|
+
/** Writes one line of plain output below the menu. */
|
|
43
|
+
export declare function printLine(terminal: MenuTerminal, line?: string): void;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** The current process's terminal, with its signals. */
|
|
2
|
+
export function processTerminal() {
|
|
3
|
+
return {
|
|
4
|
+
input: process.stdin,
|
|
5
|
+
output: process.stdout,
|
|
6
|
+
signals: process,
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Whether keys can be read one at a time. Only the input is checked, as the
|
|
11
|
+
* plugins' menus always did: output piped to a file still gets the menu.
|
|
12
|
+
*/
|
|
13
|
+
export function isInteractive(terminal) {
|
|
14
|
+
return terminal.input.isTTY === true;
|
|
15
|
+
}
|
|
16
|
+
/** Writes one line of plain output below the menu. */
|
|
17
|
+
export function printLine(terminal, line = '') {
|
|
18
|
+
terminal.output.write(`${line}\n`);
|
|
19
|
+
}
|
|
@@ -1 +1,4 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export type { CacheKeepAdapter, CacheKeepBackoff, CacheKeepLogger, CacheKeepManagerOptions, CacheKeepProfile, CacheKeepSendInput, CacheKeepStatus, CacheKeepTargetStatus, CacheKeepTargetView, CacheKeepTrackInput, CacheKeepTrackResult, } from './manager.js';
|
|
2
|
+
export { CacheKeepManager } from './manager.js';
|
|
3
|
+
export type { CacheKeepWindow } from './window.js';
|
|
4
|
+
export { isWithinCacheKeepWindow, normalizeCacheKeepWindow, } from './window.js';
|
package/dist/cachekeep/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export {};
|
|
1
|
+
export { CacheKeepManager } from './manager.js';
|
|
2
|
+
export { isWithinCacheKeepWindow, normalizeCacheKeepWindow, } from './window.js';
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { type CacheKeepWindow } from './window.js';
|
|
2
|
+
export interface CacheKeepLogger {
|
|
3
|
+
debug(message: string, data?: unknown): void;
|
|
4
|
+
warn(message: string, data?: unknown): void;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Per-target overrides a plugin derives from the captured request, usually
|
|
8
|
+
* from its model: how long the provider keeps that model's prompt cache, how
|
|
9
|
+
* many warms a session of that kind is worth, and how long it may sit idle.
|
|
10
|
+
* Evaluated once at capture time and kept on the target.
|
|
11
|
+
*/
|
|
12
|
+
export interface CacheKeepProfile {
|
|
13
|
+
/** Provider cache lifetime for this request; defaults to the manager's `ttlMs`. */
|
|
14
|
+
ttlMs?: number;
|
|
15
|
+
/** Drop the target after this many successful warms; unlimited when absent. */
|
|
16
|
+
maxWarms?: number;
|
|
17
|
+
/** Idle bound replacing the main or subagent default for this target. */
|
|
18
|
+
maxIdleMs?: number;
|
|
19
|
+
}
|
|
20
|
+
/** What the plugin sees of a tracked target. */
|
|
21
|
+
export interface CacheKeepTargetView<M> {
|
|
22
|
+
sessionKey: string;
|
|
23
|
+
accountId: string | undefined;
|
|
24
|
+
bodyText: string;
|
|
25
|
+
isSubagent: boolean;
|
|
26
|
+
meta: M;
|
|
27
|
+
warmCount: number;
|
|
28
|
+
ttlMs: number;
|
|
29
|
+
}
|
|
30
|
+
export interface CacheKeepSendInput<M> {
|
|
31
|
+
target: CacheKeepTargetView<M>;
|
|
32
|
+
/** The replay body `buildBody` returned. */
|
|
33
|
+
body: string;
|
|
34
|
+
/** Aborted on the warm timeout and when the manager stops. */
|
|
35
|
+
signal: AbortSignal;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The provider half of keep-warm. The manager decides when and what to warm;
|
|
39
|
+
* the plugin turns a captured request into a replay, resolves the account's
|
|
40
|
+
* credential, sends it and reads usage back.
|
|
41
|
+
*/
|
|
42
|
+
export interface CacheKeepAdapter<M> {
|
|
43
|
+
/** Build the replay body from the captured one. A throw backs the target off. */
|
|
44
|
+
buildBody(target: CacheKeepTargetView<M>): string | Promise<string>;
|
|
45
|
+
/**
|
|
46
|
+
* Resolve the target account's credential and send the replay. A throw
|
|
47
|
+
* (including "no credential") or a non-2xx response backs the target off.
|
|
48
|
+
*/
|
|
49
|
+
send(input: CacheKeepSendInput<M>): Promise<Response>;
|
|
50
|
+
/** Read usage from a successful response, for the log line only. */
|
|
51
|
+
readUsage?(input: {
|
|
52
|
+
target: CacheKeepTargetView<M>;
|
|
53
|
+
response: Response;
|
|
54
|
+
text: string;
|
|
55
|
+
}): Record<string, unknown> | undefined;
|
|
56
|
+
/** Per-target TTL, warm cap and idle bound, from the captured request. */
|
|
57
|
+
profile?(input: {
|
|
58
|
+
bodyText: string;
|
|
59
|
+
isSubagent: boolean;
|
|
60
|
+
accountId: string | undefined;
|
|
61
|
+
meta: M;
|
|
62
|
+
}): CacheKeepProfile | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* The account the session routes to now, asked before every warm. A
|
|
65
|
+
* different account than the one that served the captured request drops
|
|
66
|
+
* the target: its cache lives on the old account, which the session has
|
|
67
|
+
* left, and replaying on the new one would only write a cache nobody asked
|
|
68
|
+
* for. `undefined` means the plugin holds no session-to-account binding and
|
|
69
|
+
* the captured account stands. A throw backs the target off.
|
|
70
|
+
*/
|
|
71
|
+
activeAccount?(sessionKey: string, target: CacheKeepTargetView<M>): string | undefined | Promise<string | undefined>;
|
|
72
|
+
}
|
|
73
|
+
export type CacheKeepBackoff = number | ((input: {
|
|
74
|
+
sessionKey: string;
|
|
75
|
+
failures: number;
|
|
76
|
+
}) => number);
|
|
77
|
+
export interface CacheKeepManagerOptions<M> {
|
|
78
|
+
adapter: CacheKeepAdapter<M>;
|
|
79
|
+
now: () => number;
|
|
80
|
+
logger?: CacheKeepLogger;
|
|
81
|
+
/** Default provider cache lifetime. Default 5 minutes. */
|
|
82
|
+
ttlMs?: number;
|
|
83
|
+
/** Warm this long before the cache expires. Default one tick plus 15 s. */
|
|
84
|
+
leadMs?: number;
|
|
85
|
+
/** Idle bound for main sessions, from their last real request. Default 1 h. */
|
|
86
|
+
maxIdleWarmMs?: number;
|
|
87
|
+
/** Idle bound for subagent sessions. Default 30 minutes. */
|
|
88
|
+
maxSubagentIdleMs?: number;
|
|
89
|
+
/** How often the timer runs a tick. Default 60 s. */
|
|
90
|
+
tickIntervalMs?: number;
|
|
91
|
+
/** Most sessions tracked at once. Default 32. */
|
|
92
|
+
maxTargets?: number;
|
|
93
|
+
/** Total captured body bytes (UTF-8). Default 8 MiB. */
|
|
94
|
+
maxBytes?: number;
|
|
95
|
+
/** One deadline for credential resolution and the send. Default 30 s. */
|
|
96
|
+
warmTimeoutMs?: number;
|
|
97
|
+
/** Delay after a failed warm, fixed or by consecutive failures. Default 10 minutes. */
|
|
98
|
+
backoffMs?: CacheKeepBackoff;
|
|
99
|
+
/** The configured clock window; undefined means always warm. Read on every call. */
|
|
100
|
+
getWindow?: () => CacheKeepWindow | undefined;
|
|
101
|
+
/** When true, main sessions are exempt from idle pruning. Read on every call. */
|
|
102
|
+
getSustain?: () => boolean;
|
|
103
|
+
setIntervalImpl?: typeof globalThis.setInterval;
|
|
104
|
+
clearIntervalImpl?: typeof globalThis.clearInterval;
|
|
105
|
+
}
|
|
106
|
+
export type CacheKeepTrackResult = {
|
|
107
|
+
tracked: true;
|
|
108
|
+
} | {
|
|
109
|
+
tracked: false;
|
|
110
|
+
reason: 'body-exceeds-max-bytes' | 'outside-window';
|
|
111
|
+
};
|
|
112
|
+
export type CacheKeepTrackInput<M> = {
|
|
113
|
+
sessionKey: string;
|
|
114
|
+
bodyText: string;
|
|
115
|
+
/** The account that served this request; the one a warm replays on. */
|
|
116
|
+
accountId?: string;
|
|
117
|
+
isSubagent?: boolean;
|
|
118
|
+
} & (undefined extends M ? {
|
|
119
|
+
/** Plugin data handed back to the adapter, e.g. replay headers. */ meta?: M;
|
|
120
|
+
} : {
|
|
121
|
+
/** Plugin data handed back to the adapter, e.g. replay headers. */ meta: M;
|
|
122
|
+
});
|
|
123
|
+
export interface CacheKeepTargetStatus {
|
|
124
|
+
sessionKey: string;
|
|
125
|
+
accountId: string | undefined;
|
|
126
|
+
isSubagent: boolean;
|
|
127
|
+
cacheExpiresAt: number;
|
|
128
|
+
lastRealRequestAt: number;
|
|
129
|
+
lastWarmedAt?: number;
|
|
130
|
+
backoffUntil?: number;
|
|
131
|
+
failures: number;
|
|
132
|
+
warmCount: number;
|
|
133
|
+
ttlMs: number;
|
|
134
|
+
bodyBytes: number;
|
|
135
|
+
}
|
|
136
|
+
export interface CacheKeepStatus {
|
|
137
|
+
running: boolean;
|
|
138
|
+
tracked: number;
|
|
139
|
+
totalBytes: number;
|
|
140
|
+
generatedAt: number;
|
|
141
|
+
startedAt: number | null;
|
|
142
|
+
ttlMs: number;
|
|
143
|
+
leadMs: number;
|
|
144
|
+
maxIdleWarmMs: number;
|
|
145
|
+
maxSubagentIdleMs: number;
|
|
146
|
+
sustain: boolean;
|
|
147
|
+
window?: CacheKeepWindow;
|
|
148
|
+
targets: CacheKeepTargetStatus[];
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Keeps idle prompt caches warm: one target per session holding the latest
|
|
152
|
+
* request body, replayed just before the provider's cache would expire.
|
|
153
|
+
*
|
|
154
|
+
* Bounds, in the order they act: the clock window gates capture and warming
|
|
155
|
+
* (targets captured earlier survive outside it); idle caps prune a target its
|
|
156
|
+
* session stopped using (`sustain` lifts only the main-session cap); the
|
|
157
|
+
* target-count and byte caps evict the least recently touched target; a
|
|
158
|
+
* per-target warm cap retires short-lived sessions; and a failed warm backs
|
|
159
|
+
* that target off without touching the others.
|
|
160
|
+
*/
|
|
161
|
+
export declare class CacheKeepManager<M = undefined> {
|
|
162
|
+
private readonly targets;
|
|
163
|
+
private readonly adapter;
|
|
164
|
+
private readonly now;
|
|
165
|
+
private readonly log;
|
|
166
|
+
private readonly ttlMs;
|
|
167
|
+
private readonly leadMs;
|
|
168
|
+
private readonly maxIdleWarmMs;
|
|
169
|
+
private readonly maxSubagentIdleMs;
|
|
170
|
+
private readonly tickIntervalMs;
|
|
171
|
+
private readonly maxTargets;
|
|
172
|
+
private readonly maxBytes;
|
|
173
|
+
private readonly warmTimeoutMs;
|
|
174
|
+
private readonly backoff;
|
|
175
|
+
private readonly getWindow;
|
|
176
|
+
private readonly getSustain;
|
|
177
|
+
private readonly setIntervalImpl;
|
|
178
|
+
private readonly clearIntervalImpl;
|
|
179
|
+
private timer;
|
|
180
|
+
private startedAt;
|
|
181
|
+
private totalBytes;
|
|
182
|
+
private tickPromise;
|
|
183
|
+
private disposed;
|
|
184
|
+
private abortController;
|
|
185
|
+
constructor(options: CacheKeepManagerOptions<M>);
|
|
186
|
+
/**
|
|
187
|
+
* Capture a session's latest request. The freshest body replaces the
|
|
188
|
+
* previous one and restarts its cache clock, idle clock and warm count.
|
|
189
|
+
* Arms the timer.
|
|
190
|
+
*/
|
|
191
|
+
track(input: CacheKeepTrackInput<M>): CacheKeepTrackResult;
|
|
192
|
+
/** Forget a session, e.g. when the host deletes it. */
|
|
193
|
+
remove(sessionKey: string): void;
|
|
194
|
+
start(): void;
|
|
195
|
+
/** Disarm, abort any warm in flight and forget every target. */
|
|
196
|
+
stop(): void;
|
|
197
|
+
/** A snapshot without captured bodies, which can hold prompt content. */
|
|
198
|
+
status(): CacheKeepStatus;
|
|
199
|
+
/**
|
|
200
|
+
* Prune, then warm every target within `leadMs` of expiry that is not
|
|
201
|
+
* backing off. Overlapping calls share the tick already running, so a slow
|
|
202
|
+
* warm never stacks a second send for the same target.
|
|
203
|
+
*/
|
|
204
|
+
tick(): Promise<void>;
|
|
205
|
+
private runTick;
|
|
206
|
+
private isCurrent;
|
|
207
|
+
private view;
|
|
208
|
+
private fail;
|
|
209
|
+
private warm;
|
|
210
|
+
private idleDefault;
|
|
211
|
+
private pruneStale;
|
|
212
|
+
private leastRecentlyTouched;
|
|
213
|
+
private drop;
|
|
214
|
+
}
|