dsh-coding-subscription-oauth 0.6.2 → 0.6.3
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/CHANGELOG.md +222 -206
- package/CONTRIBUTING.md +129 -120
- package/INSTALL.md +218 -218
- package/LICENSE +19 -19
- package/NOTICE +11 -11
- package/README.de.md +289 -289
- package/README.es.md +290 -290
- package/README.fr.md +290 -290
- package/README.ja.md +290 -290
- package/README.ko.md +290 -290
- package/README.md +304 -304
- package/README.pt-BR.md +290 -290
- package/README.ru.md +290 -290
- package/README.zh-CN.md +302 -302
- package/compatibility/dsh-bom.json +0 -0
- package/cordis.patch.yml +13 -13
- package/docs/00-project-rules.md +212 -195
- package/docs/02-architecture.md +130 -130
- package/docs/02-architecture.zh-CN.md +130 -130
- package/lib/bin.js.map +1 -1
- package/lib/capability-settings.d.ts +8 -2
- package/lib/capability-settings.d.ts.map +1 -1
- package/lib/capability-tools.d.ts +7 -2
- package/lib/capability-tools.d.ts.map +1 -1
- package/lib/client.js +3 -3
- package/lib/client.js.map +2 -2
- package/lib/codex-images.d.ts +8 -0
- package/lib/codex-images.d.ts.map +1 -1
- package/lib/index.js +32 -7
- package/lib/index.js.map +2 -2
- package/lib/invariant.js.map +1 -1
- package/media/en/settings_accounts.png +0 -0
- package/media/en/settings_capabilities.png +0 -0
- package/media/en/settings_gateway.png +0 -0
- package/media/settings_accounts.png +0 -0
- package/media/settings_capabilities.png +0 -0
- package/media/settings_gateway.png +0 -0
- package/media/settings_overview.png +0 -0
- package/media/zh-CN/settings_accounts.png +0 -0
- package/media/zh-CN/settings_capabilities.png +0 -0
- package/media/zh-CN/settings_gateway.png +0 -0
- package/package.json +148 -148
- package/patches/dsh-agy@0.1.2.patch +25 -25
- package/scripts/release.mjs +186 -186
- package/scripts/smoke-deployed-routes.mjs +146 -146
- package/scripts/verify-deployed-catalog.mjs +87 -87
- package/src/adapter.ts +273 -273
- package/src/alias-adapter.ts +130 -130
- package/src/auth-routes.ts +921 -921
- package/src/auth.ts +67 -67
- package/src/bin.ts +350 -350
- package/src/capability-routes.ts +279 -279
- package/src/capability-runtime.ts +314 -313
- package/src/capability-settings.ts +671 -658
- package/src/capability-tools.ts +685 -666
- package/src/catalog.ts +271 -271
- package/src/client/GrokBuildSettings.tsx +771 -770
- package/src/client/api.ts +88 -88
- package/src/client/components/AboutTab.tsx +30 -30
- package/src/client/components/AccountsTab.tsx +241 -241
- package/src/client/components/Badge.tsx +33 -33
- package/src/client/components/CapabilitiesTab.tsx +265 -265
- package/src/client/components/CliPullPreview.tsx +116 -116
- package/src/client/components/CopyButton.tsx +57 -57
- package/src/client/components/GatewayTab.tsx +469 -469
- package/src/client/components/NoticeBanner.tsx +46 -46
- package/src/client/components/ProgressBar.tsx +53 -53
- package/src/client/components/ProviderCard.tsx +606 -606
- package/src/client/components/SettingsTabs.tsx +75 -75
- package/src/client/components/ToggleSwitch.tsx +71 -71
- package/src/client/constants.ts +230 -224
- package/src/client/display.ts +61 -61
- package/src/client/dshClientAdapter.ts +127 -127
- package/src/client/gatewaySnippets.ts +37 -37
- package/src/client/index.tsx +156 -156
- package/src/client/locales.ts +540 -535
- package/src/client/microStyles.ts +52 -52
- package/src/client/parsers.ts +398 -396
- package/src/client/styles.ts +325 -325
- package/src/client/types.ts +199 -197
- package/src/codex-http.ts +447 -447
- package/src/codex-images.ts +503 -485
- package/src/codex-model-capabilities.ts +320 -320
- package/src/codex-search.ts +245 -245
- package/src/codex-usage.ts +263 -263
- package/src/compatibility.ts +41 -41
- package/src/dsh-host-adapter.ts +173 -173
- package/src/gateway-anthropic-messages.ts +84 -84
- package/src/gateway-auth.ts +102 -102
- package/src/gateway-backend.ts +274 -274
- package/src/gateway-body.ts +49 -49
- package/src/gateway-config.ts +76 -76
- package/src/gateway-http.ts +104 -104
- package/src/gateway-openai-chat.ts +124 -124
- package/src/gateway-openai-responses.ts +53 -53
- package/src/gateway-parse.ts +224 -224
- package/src/gateway-protocol.ts +52 -52
- package/src/gateway-routes.ts +158 -158
- package/src/gateway.ts +258 -258
- package/src/grok-errors.ts +24 -24
- package/src/grok-imagine.ts +1627 -1627
- package/src/grok-import.ts +151 -151
- package/src/http-json.ts +82 -82
- package/src/ids.ts +59 -59
- package/src/imagine-routes.ts +463 -463
- package/src/index.ts +709 -709
- package/src/invariant.ts +17 -17
- package/src/kimi-errors.ts +26 -26
- package/src/media-store.ts +927 -927
- package/src/oauth-import-routes.ts +324 -324
- package/src/oauth-providers.ts +152 -152
- package/src/oauth-session.ts +183 -183
- package/src/oauth-sources.ts +1104 -1104
- package/src/oauth.ts +620 -620
- package/src/provider.ts +128 -128
- package/src/proxy.ts +11 -11
- package/src/redact.ts +72 -72
- package/src/session.ts +218 -218
- package/src/store.ts +217 -217
- package/src/web-origin.ts +296 -296
- package/src/web-routes.ts +38 -38
package/src/client/parsers.ts
CHANGED
|
@@ -1,396 +1,398 @@
|
|
|
1
|
-
/** Response parsers and small formatting helpers for the settings UI. */
|
|
2
|
-
|
|
3
|
-
import { isRecord } from "./api.ts";
|
|
4
|
-
import {
|
|
5
|
-
GATEWAY_PORT_MAX,
|
|
6
|
-
GATEWAY_PORT_MIN,
|
|
7
|
-
GATEWAY_RANDOM_PORT_MAX,
|
|
8
|
-
GATEWAY_RANDOM_PORT_MIN,
|
|
9
|
-
GATEWAY_RANDOM_RESERVED,
|
|
10
|
-
HOUR_MS,
|
|
11
|
-
IMAGINE_SOURCE_KEY,
|
|
12
|
-
SOURCE_COMMIT_ACTIONS,
|
|
13
|
-
SOURCE_CONFLICTS,
|
|
14
|
-
SOURCE_DEFAULT_PATH,
|
|
15
|
-
SOURCE_KINDS,
|
|
16
|
-
SOURCE_PREVIEW_ACTIONS,
|
|
17
|
-
SOURCE_REASONS,
|
|
18
|
-
} from "./constants.ts";
|
|
19
|
-
import type {
|
|
20
|
-
CapabilitySettingsView,
|
|
21
|
-
CapabilitySnapshot,
|
|
22
|
-
GatewayView,
|
|
23
|
-
GrokBuildSettingsInjected,
|
|
24
|
-
ImagineCredentialView,
|
|
25
|
-
ProviderStatus,
|
|
26
|
-
SourceCommitAction,
|
|
27
|
-
SourceConflict,
|
|
28
|
-
SourceKind,
|
|
29
|
-
SourcePreview,
|
|
30
|
-
SourcePreviewAction,
|
|
31
|
-
SourceReason,
|
|
32
|
-
SourceStatus,
|
|
33
|
-
UsageLimitView,
|
|
34
|
-
UsageView,
|
|
35
|
-
UsageWindowView,
|
|
36
|
-
} from "./types.ts";
|
|
37
|
-
|
|
38
|
-
export function optionalString(value: unknown): string | undefined {
|
|
39
|
-
return typeof value === "string" && value.length > 0 && value.length < 500 ? value : undefined;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function optionalFiniteNumber(value: unknown): number | undefined {
|
|
43
|
-
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function optionalBoolean(value: unknown): boolean | undefined {
|
|
47
|
-
return typeof value === "boolean" ? value : undefined;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function optionalPercent(value: unknown): number | undefined {
|
|
51
|
-
const numeric = optionalFiniteNumber(value);
|
|
52
|
-
return numeric !== undefined && numeric >= 0 && numeric <= 100 ? numeric : undefined;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function isSourceKind(value: string): value is SourceKind {
|
|
56
|
-
return (SOURCE_KINDS as readonly string[]).includes(value);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function isSourceReason(value: string): value is SourceReason {
|
|
60
|
-
return (SOURCE_REASONS as readonly string[]).includes(value);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function isSourceConflict(value: string): value is SourceConflict {
|
|
64
|
-
return (SOURCE_CONFLICTS as readonly string[]).includes(value);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export function isSourcePreviewAction(value: string): value is SourcePreviewAction {
|
|
68
|
-
return (SOURCE_PREVIEW_ACTIONS as readonly string[]).includes(value);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export function isSourceCommitAction(value: string): value is SourceCommitAction {
|
|
72
|
-
return (SOURCE_COMMIT_ACTIONS as readonly string[]).includes(value);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export function looksSecret(value: string): boolean {
|
|
76
|
-
return /eyJ[A-Za-z0-9_-]+\.|sk-[A-Za-z0-9_-]{8,}|Bearer\s+\S+/u.test(value);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export function safeDisplayPath(value: unknown, kind: SourceKind): string {
|
|
80
|
-
const text = optionalString(value);
|
|
81
|
-
if (text === undefined || looksSecret(text) || text.length > 180) return SOURCE_DEFAULT_PATH[kind];
|
|
82
|
-
return text;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export function safeWarning(value: unknown): string | undefined {
|
|
86
|
-
const text = optionalString(value);
|
|
87
|
-
if (text === undefined || looksSecret(text)) return undefined;
|
|
88
|
-
return text;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export function formatEpoch(value: number | undefined): string | undefined {
|
|
92
|
-
if (value === undefined || !Number.isFinite(value) || value <= 0) return undefined;
|
|
93
|
-
const ms = value > 1e12 ? value : value > 1e9 ? value * 1000 : undefined;
|
|
94
|
-
if (ms === undefined) return undefined;
|
|
95
|
-
const formatted = new Date(ms).toLocaleString();
|
|
96
|
-
return formatted.length > 0 ? formatted : undefined;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export function parseSource(value: unknown): SourceStatus | undefined {
|
|
100
|
-
if (!isRecord(value) || typeof value["kind"] !== "string" || !isSourceKind(value["kind"])) return undefined;
|
|
101
|
-
const kind = value["kind"];
|
|
102
|
-
const reasonRaw = optionalString(value["reason"]);
|
|
103
|
-
const expiresAt = optionalFiniteNumber(value["expiresAt"]);
|
|
104
|
-
return {
|
|
105
|
-
kind,
|
|
106
|
-
displayPath: safeDisplayPath(value["displayPath"], kind),
|
|
107
|
-
available: value["available"] === true,
|
|
108
|
-
...(expiresAt === undefined ? {} : { expiresAt }),
|
|
109
|
-
...(reasonRaw !== undefined && isSourceReason(reasonRaw) ? { reason: reasonRaw } : {}),
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export function mergeSources(discovered: readonly SourceStatus[]): SourceStatus[] {
|
|
114
|
-
return SOURCE_KINDS.map((kind) => {
|
|
115
|
-
const found = discovered.find((entry) => entry.kind === kind);
|
|
116
|
-
return found ?? { kind, displayPath: SOURCE_DEFAULT_PATH[kind], available: false, reason: "missing" };
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export function parseSources(value: unknown): SourceStatus[] {
|
|
121
|
-
const rows = Array.isArray(value)
|
|
122
|
-
? value
|
|
123
|
-
: isRecord(value) && Array.isArray(value["sources"])
|
|
124
|
-
? value["sources"]
|
|
125
|
-
: [];
|
|
126
|
-
return mergeSources(rows.map(parseSource).filter((entry): entry is SourceStatus => entry !== undefined));
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
export function parsePreview(value: unknown): SourcePreview | undefined {
|
|
130
|
-
if (!isRecord(value)) return undefined;
|
|
131
|
-
const previewId = optionalString(value["previewId"]);
|
|
132
|
-
const kindRaw = optionalString(value["kind"]);
|
|
133
|
-
if (previewId === undefined || kindRaw === undefined || !isSourceKind(kindRaw)) return undefined;
|
|
134
|
-
const conflictRaw = optionalString(value["conflict"]);
|
|
135
|
-
const actionRaw = optionalString(value["action"]);
|
|
136
|
-
const expiresAt = optionalFiniteNumber(value["expiresAt"]);
|
|
137
|
-
const ticketExpiresAt = optionalFiniteNumber(value["ticketExpiresAt"]);
|
|
138
|
-
const warnings = Array.isArray(value["warnings"])
|
|
139
|
-
? value["warnings"].map(safeWarning).filter((entry): entry is string => entry !== undefined)
|
|
140
|
-
: [];
|
|
141
|
-
return {
|
|
142
|
-
previewId,
|
|
143
|
-
kind: kindRaw,
|
|
144
|
-
displayPath: safeDisplayPath(value["displayPath"], kindRaw),
|
|
145
|
-
confirmOverwriteRequired: value["confirmOverwriteRequired"] === true,
|
|
146
|
-
warnings,
|
|
147
|
-
...(expiresAt === undefined ? {} : { expiresAt }),
|
|
148
|
-
...(ticketExpiresAt === undefined ? {} : { ticketExpiresAt }),
|
|
149
|
-
...(conflictRaw !== undefined && isSourceConflict(conflictRaw) ? { conflict: conflictRaw } : {}),
|
|
150
|
-
...(actionRaw !== undefined && isSourcePreviewAction(actionRaw) ? { action: actionRaw } : {}),
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
export function parseCommitAction(value: unknown): SourceCommitAction | undefined {
|
|
155
|
-
if (!isRecord(value)) return undefined;
|
|
156
|
-
const action = optionalString(value["action"]);
|
|
157
|
-
return action !== undefined && isSourceCommitAction(action) ? action : undefined;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
export function boundedInteger(value: unknown, min: number, max: number, fallback: number): number {
|
|
161
|
-
const numeric = optionalFiniteNumber(value);
|
|
162
|
-
return numeric !== undefined && Number.isInteger(numeric) && numeric >= min && numeric <= max ? numeric : fallback;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export function emptyCapabilitySettings(): CapabilitySettingsView {
|
|
166
|
-
return {
|
|
167
|
-
codexSearch: false,
|
|
168
|
-
codexImages: false,
|
|
169
|
-
codexImageEdits: false,
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
const
|
|
213
|
-
const
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
...(
|
|
226
|
-
...(
|
|
227
|
-
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
const
|
|
235
|
-
const
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
parseUsageWindow(nested),
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
const
|
|
251
|
-
const
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
)
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
const
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
const
|
|
288
|
-
const
|
|
289
|
-
const
|
|
290
|
-
|
|
291
|
-
const
|
|
292
|
-
spend === undefined ? undefined :
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
...(
|
|
301
|
-
...(
|
|
302
|
-
...(
|
|
303
|
-
...(
|
|
304
|
-
...(
|
|
305
|
-
...(
|
|
306
|
-
...(
|
|
307
|
-
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
usage.
|
|
315
|
-
usage.
|
|
316
|
-
usage.
|
|
317
|
-
usage.
|
|
318
|
-
usage.
|
|
319
|
-
usage.
|
|
320
|
-
usage.
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
const
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
const
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
if (
|
|
373
|
-
const
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
if (
|
|
386
|
-
|
|
387
|
-
return t(
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
1
|
+
/** Response parsers and small formatting helpers for the settings UI. */
|
|
2
|
+
|
|
3
|
+
import { isRecord } from "./api.ts";
|
|
4
|
+
import {
|
|
5
|
+
GATEWAY_PORT_MAX,
|
|
6
|
+
GATEWAY_PORT_MIN,
|
|
7
|
+
GATEWAY_RANDOM_PORT_MAX,
|
|
8
|
+
GATEWAY_RANDOM_PORT_MIN,
|
|
9
|
+
GATEWAY_RANDOM_RESERVED,
|
|
10
|
+
HOUR_MS,
|
|
11
|
+
IMAGINE_SOURCE_KEY,
|
|
12
|
+
SOURCE_COMMIT_ACTIONS,
|
|
13
|
+
SOURCE_CONFLICTS,
|
|
14
|
+
SOURCE_DEFAULT_PATH,
|
|
15
|
+
SOURCE_KINDS,
|
|
16
|
+
SOURCE_PREVIEW_ACTIONS,
|
|
17
|
+
SOURCE_REASONS,
|
|
18
|
+
} from "./constants.ts";
|
|
19
|
+
import type {
|
|
20
|
+
CapabilitySettingsView,
|
|
21
|
+
CapabilitySnapshot,
|
|
22
|
+
GatewayView,
|
|
23
|
+
GrokBuildSettingsInjected,
|
|
24
|
+
ImagineCredentialView,
|
|
25
|
+
ProviderStatus,
|
|
26
|
+
SourceCommitAction,
|
|
27
|
+
SourceConflict,
|
|
28
|
+
SourceKind,
|
|
29
|
+
SourcePreview,
|
|
30
|
+
SourcePreviewAction,
|
|
31
|
+
SourceReason,
|
|
32
|
+
SourceStatus,
|
|
33
|
+
UsageLimitView,
|
|
34
|
+
UsageView,
|
|
35
|
+
UsageWindowView,
|
|
36
|
+
} from "./types.ts";
|
|
37
|
+
|
|
38
|
+
export function optionalString(value: unknown): string | undefined {
|
|
39
|
+
return typeof value === "string" && value.length > 0 && value.length < 500 ? value : undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function optionalFiniteNumber(value: unknown): number | undefined {
|
|
43
|
+
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function optionalBoolean(value: unknown): boolean | undefined {
|
|
47
|
+
return typeof value === "boolean" ? value : undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function optionalPercent(value: unknown): number | undefined {
|
|
51
|
+
const numeric = optionalFiniteNumber(value);
|
|
52
|
+
return numeric !== undefined && numeric >= 0 && numeric <= 100 ? numeric : undefined;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function isSourceKind(value: string): value is SourceKind {
|
|
56
|
+
return (SOURCE_KINDS as readonly string[]).includes(value);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function isSourceReason(value: string): value is SourceReason {
|
|
60
|
+
return (SOURCE_REASONS as readonly string[]).includes(value);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function isSourceConflict(value: string): value is SourceConflict {
|
|
64
|
+
return (SOURCE_CONFLICTS as readonly string[]).includes(value);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function isSourcePreviewAction(value: string): value is SourcePreviewAction {
|
|
68
|
+
return (SOURCE_PREVIEW_ACTIONS as readonly string[]).includes(value);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function isSourceCommitAction(value: string): value is SourceCommitAction {
|
|
72
|
+
return (SOURCE_COMMIT_ACTIONS as readonly string[]).includes(value);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function looksSecret(value: string): boolean {
|
|
76
|
+
return /eyJ[A-Za-z0-9_-]+\.|sk-[A-Za-z0-9_-]{8,}|Bearer\s+\S+/u.test(value);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function safeDisplayPath(value: unknown, kind: SourceKind): string {
|
|
80
|
+
const text = optionalString(value);
|
|
81
|
+
if (text === undefined || looksSecret(text) || text.length > 180) return SOURCE_DEFAULT_PATH[kind];
|
|
82
|
+
return text;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function safeWarning(value: unknown): string | undefined {
|
|
86
|
+
const text = optionalString(value);
|
|
87
|
+
if (text === undefined || looksSecret(text)) return undefined;
|
|
88
|
+
return text;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function formatEpoch(value: number | undefined): string | undefined {
|
|
92
|
+
if (value === undefined || !Number.isFinite(value) || value <= 0) return undefined;
|
|
93
|
+
const ms = value > 1e12 ? value : value > 1e9 ? value * 1000 : undefined;
|
|
94
|
+
if (ms === undefined) return undefined;
|
|
95
|
+
const formatted = new Date(ms).toLocaleString();
|
|
96
|
+
return formatted.length > 0 ? formatted : undefined;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function parseSource(value: unknown): SourceStatus | undefined {
|
|
100
|
+
if (!isRecord(value) || typeof value["kind"] !== "string" || !isSourceKind(value["kind"])) return undefined;
|
|
101
|
+
const kind = value["kind"];
|
|
102
|
+
const reasonRaw = optionalString(value["reason"]);
|
|
103
|
+
const expiresAt = optionalFiniteNumber(value["expiresAt"]);
|
|
104
|
+
return {
|
|
105
|
+
kind,
|
|
106
|
+
displayPath: safeDisplayPath(value["displayPath"], kind),
|
|
107
|
+
available: value["available"] === true,
|
|
108
|
+
...(expiresAt === undefined ? {} : { expiresAt }),
|
|
109
|
+
...(reasonRaw !== undefined && isSourceReason(reasonRaw) ? { reason: reasonRaw } : {}),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function mergeSources(discovered: readonly SourceStatus[]): SourceStatus[] {
|
|
114
|
+
return SOURCE_KINDS.map((kind) => {
|
|
115
|
+
const found = discovered.find((entry) => entry.kind === kind);
|
|
116
|
+
return found ?? { kind, displayPath: SOURCE_DEFAULT_PATH[kind], available: false, reason: "missing" };
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function parseSources(value: unknown): SourceStatus[] {
|
|
121
|
+
const rows = Array.isArray(value)
|
|
122
|
+
? value
|
|
123
|
+
: isRecord(value) && Array.isArray(value["sources"])
|
|
124
|
+
? value["sources"]
|
|
125
|
+
: [];
|
|
126
|
+
return mergeSources(rows.map(parseSource).filter((entry): entry is SourceStatus => entry !== undefined));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function parsePreview(value: unknown): SourcePreview | undefined {
|
|
130
|
+
if (!isRecord(value)) return undefined;
|
|
131
|
+
const previewId = optionalString(value["previewId"]);
|
|
132
|
+
const kindRaw = optionalString(value["kind"]);
|
|
133
|
+
if (previewId === undefined || kindRaw === undefined || !isSourceKind(kindRaw)) return undefined;
|
|
134
|
+
const conflictRaw = optionalString(value["conflict"]);
|
|
135
|
+
const actionRaw = optionalString(value["action"]);
|
|
136
|
+
const expiresAt = optionalFiniteNumber(value["expiresAt"]);
|
|
137
|
+
const ticketExpiresAt = optionalFiniteNumber(value["ticketExpiresAt"]);
|
|
138
|
+
const warnings = Array.isArray(value["warnings"])
|
|
139
|
+
? value["warnings"].map(safeWarning).filter((entry): entry is string => entry !== undefined)
|
|
140
|
+
: [];
|
|
141
|
+
return {
|
|
142
|
+
previewId,
|
|
143
|
+
kind: kindRaw,
|
|
144
|
+
displayPath: safeDisplayPath(value["displayPath"], kindRaw),
|
|
145
|
+
confirmOverwriteRequired: value["confirmOverwriteRequired"] === true,
|
|
146
|
+
warnings,
|
|
147
|
+
...(expiresAt === undefined ? {} : { expiresAt }),
|
|
148
|
+
...(ticketExpiresAt === undefined ? {} : { ticketExpiresAt }),
|
|
149
|
+
...(conflictRaw !== undefined && isSourceConflict(conflictRaw) ? { conflict: conflictRaw } : {}),
|
|
150
|
+
...(actionRaw !== undefined && isSourcePreviewAction(actionRaw) ? { action: actionRaw } : {}),
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function parseCommitAction(value: unknown): SourceCommitAction | undefined {
|
|
155
|
+
if (!isRecord(value)) return undefined;
|
|
156
|
+
const action = optionalString(value["action"]);
|
|
157
|
+
return action !== undefined && isSourceCommitAction(action) ? action : undefined;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function boundedInteger(value: unknown, min: number, max: number, fallback: number): number {
|
|
161
|
+
const numeric = optionalFiniteNumber(value);
|
|
162
|
+
return numeric !== undefined && Number.isInteger(numeric) && numeric >= min && numeric <= max ? numeric : fallback;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function emptyCapabilitySettings(): CapabilitySettingsView {
|
|
166
|
+
return {
|
|
167
|
+
codexSearch: false,
|
|
168
|
+
codexImages: false,
|
|
169
|
+
codexImageEdits: false,
|
|
170
|
+
codexImagesAnyModel: false,
|
|
171
|
+
codexUsage: false,
|
|
172
|
+
codexFast: false,
|
|
173
|
+
grokImagineImage: false,
|
|
174
|
+
grokImagineVideo: false,
|
|
175
|
+
searchResults: 5,
|
|
176
|
+
imageCount: 1,
|
|
177
|
+
videoArtifactTtlMs: 7 * 24 * HOUR_MS,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function parseCapabilitySettings(value: unknown): CapabilitySettingsView {
|
|
182
|
+
const source = isRecord(value) ? value : {};
|
|
183
|
+
return {
|
|
184
|
+
codexSearch: source["codexSearch"] === true,
|
|
185
|
+
codexImages: source["codexImages"] === true,
|
|
186
|
+
codexImageEdits: source["codexImageEdits"] === true,
|
|
187
|
+
codexImagesAnyModel: source["codexImagesAnyModel"] === true,
|
|
188
|
+
codexUsage: source["codexUsage"] === true,
|
|
189
|
+
codexFast: source["codexFast"] === true,
|
|
190
|
+
grokImagineImage: source["grokImagineImage"] === true,
|
|
191
|
+
grokImagineVideo: source["grokImagineVideo"] === true,
|
|
192
|
+
searchResults: boundedInteger(source["searchResults"], 1, 20, 5),
|
|
193
|
+
imageCount: boundedInteger(source["imageCount"], 1, 4, 1),
|
|
194
|
+
videoArtifactTtlMs: boundedInteger(source["videoArtifactTtlMs"], HOUR_MS, 7 * 24 * HOUR_MS, 7 * 24 * HOUR_MS),
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function parseCapabilities(value: unknown): CapabilitySnapshot | undefined {
|
|
199
|
+
if (!isRecord(value)) return undefined;
|
|
200
|
+
const nested = isRecord(value["value"]) ? value["value"] : value;
|
|
201
|
+
const revision = optionalFiniteNumber(value["revision"]);
|
|
202
|
+
if (revision === undefined && !isRecord(value["value"]) && value["writable"] === undefined) return undefined;
|
|
203
|
+
return {
|
|
204
|
+
value: parseCapabilitySettings(nested),
|
|
205
|
+
revision: revision ?? 0,
|
|
206
|
+
writable: value["writable"] === true,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function parseUsageWindow(value: unknown): UsageWindowView | undefined {
|
|
211
|
+
if (!isRecord(value)) return undefined;
|
|
212
|
+
const usedPercent = optionalPercent(value["usedPercent"] ?? value["used_percent"]);
|
|
213
|
+
const remainingPercent = optionalPercent(value["remainingPercent"] ?? value["remaining_percent"]);
|
|
214
|
+
const windowSeconds = optionalFiniteNumber(value["windowSeconds"] ?? value["limit_window_seconds"]);
|
|
215
|
+
const resetsAt = optionalFiniteNumber(value["resetsAt"] ?? value["reset_at"]);
|
|
216
|
+
if (
|
|
217
|
+
usedPercent === undefined &&
|
|
218
|
+
remainingPercent === undefined &&
|
|
219
|
+
windowSeconds === undefined &&
|
|
220
|
+
resetsAt === undefined
|
|
221
|
+
) {
|
|
222
|
+
return undefined;
|
|
223
|
+
}
|
|
224
|
+
return {
|
|
225
|
+
...(usedPercent === undefined ? {} : { usedPercent }),
|
|
226
|
+
...(remainingPercent === undefined ? {} : { remainingPercent }),
|
|
227
|
+
...(windowSeconds !== undefined && windowSeconds > 0 ? { windowSeconds } : {}),
|
|
228
|
+
...(resetsAt === undefined ? {} : { resetsAt }),
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function parseUsageLimit(value: unknown, fallbackId: string): UsageLimitView | undefined {
|
|
233
|
+
if (!isRecord(value)) return undefined;
|
|
234
|
+
const id = optionalString(value["id"]) ?? optionalString(value["metered_feature"]) ?? fallbackId;
|
|
235
|
+
const name = optionalString(value["name"]) ?? optionalString(value["limit_name"]);
|
|
236
|
+
const nested = isRecord(value["rate_limit"]) ? value["rate_limit"] : value;
|
|
237
|
+
const windows = Array.isArray(value["windows"])
|
|
238
|
+
? value["windows"].map(parseUsageWindow).filter((entry): entry is UsageWindowView => entry !== undefined)
|
|
239
|
+
: [
|
|
240
|
+
parseUsageWindow(nested["primary_window"]),
|
|
241
|
+
parseUsageWindow(nested["secondary_window"]),
|
|
242
|
+
parseUsageWindow(nested),
|
|
243
|
+
].filter((entry): entry is UsageWindowView => entry !== undefined);
|
|
244
|
+
if (windows.length === 0 && name === undefined && optionalString(value["id"]) === undefined) return undefined;
|
|
245
|
+
return { id, windows, ...(name === undefined ? {} : { name }) };
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export function parseUsage(value: unknown): UsageView | undefined {
|
|
249
|
+
if (!isRecord(value)) return undefined;
|
|
250
|
+
const payload = isRecord(value["usage"]) ? value["usage"] : value;
|
|
251
|
+
const rateLimits: UsageLimitView[] = [];
|
|
252
|
+
const seen = new Set<string>();
|
|
253
|
+
const add = (limit: UsageLimitView | undefined): void => {
|
|
254
|
+
if (limit === undefined || seen.has(limit.id)) return;
|
|
255
|
+
seen.add(limit.id);
|
|
256
|
+
rateLimits.push(limit);
|
|
257
|
+
};
|
|
258
|
+
if (Array.isArray(payload["rateLimits"])) {
|
|
259
|
+
payload["rateLimits"].forEach((entry, index) => add(parseUsageLimit(entry, `limit-${String(index)}`)));
|
|
260
|
+
} else {
|
|
261
|
+
add(parseUsageLimit(payload["rate_limit"], "codex"));
|
|
262
|
+
if (Array.isArray(payload["additional_rate_limits"])) {
|
|
263
|
+
payload["additional_rate_limits"].forEach((entry, index) =>
|
|
264
|
+
add(parseUsageLimit(entry, `extra-${String(index)}`)),
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
add(parseUsageLimit(payload["code_review_rate_limit"], "code_review"));
|
|
268
|
+
}
|
|
269
|
+
const credits = isRecord(payload["credits"]) ? payload["credits"] : undefined;
|
|
270
|
+
const spend = isRecord(payload["individualLimit"])
|
|
271
|
+
? payload["individualLimit"]
|
|
272
|
+
: isRecord(payload["spend_control"])
|
|
273
|
+
? isRecord(payload["spend_control"]["individual_limit"])
|
|
274
|
+
? payload["spend_control"]["individual_limit"]
|
|
275
|
+
: payload["spend_control"]
|
|
276
|
+
: undefined;
|
|
277
|
+
const resetRaw = isRecord(payload["resetCredits"])
|
|
278
|
+
? payload["resetCredits"]["availableCount"]
|
|
279
|
+
: isRecord(payload["rate_limit_reset_credits"])
|
|
280
|
+
? payload["rate_limit_reset_credits"]["available_count"]
|
|
281
|
+
: undefined;
|
|
282
|
+
const resetCredits = optionalFiniteNumber(resetRaw);
|
|
283
|
+
const fetchedAt = optionalFiniteNumber(payload["fetchedAt"]);
|
|
284
|
+
const spendControlReached =
|
|
285
|
+
optionalBoolean(payload["spendControlReached"]) ??
|
|
286
|
+
(isRecord(payload["spend_control"]) ? optionalBoolean(payload["spend_control"]["reached"]) : undefined);
|
|
287
|
+
const creditsBalance = credits === undefined ? undefined : optionalString(credits["balance"]);
|
|
288
|
+
const individualLimit = spend === undefined ? undefined : optionalString(spend["limit"]);
|
|
289
|
+
const individualUsed = spend === undefined ? undefined : optionalString(spend["used"]);
|
|
290
|
+
const individualRemaining = spend === undefined ? undefined : optionalString(spend["remaining"]);
|
|
291
|
+
const individualRemainingPercent =
|
|
292
|
+
spend === undefined ? undefined : optionalPercent(spend["remainingPercent"] ?? spend["remaining_percent"]);
|
|
293
|
+
const individualResetsAt =
|
|
294
|
+
spend === undefined ? undefined : optionalFiniteNumber(spend["resetsAt"] ?? spend["reset_at"]);
|
|
295
|
+
return {
|
|
296
|
+
rateLimits,
|
|
297
|
+
...(credits !== undefined && typeof credits["unlimited"] === "boolean"
|
|
298
|
+
? { creditsUnlimited: credits["unlimited"] }
|
|
299
|
+
: {}),
|
|
300
|
+
...(creditsBalance === undefined ? {} : { creditsBalance }),
|
|
301
|
+
...(individualLimit === undefined ? {} : { individualLimit }),
|
|
302
|
+
...(individualUsed === undefined ? {} : { individualUsed }),
|
|
303
|
+
...(individualRemaining === undefined ? {} : { individualRemaining }),
|
|
304
|
+
...(individualRemainingPercent === undefined ? {} : { individualRemainingPercent }),
|
|
305
|
+
...(individualResetsAt === undefined ? {} : { individualResetsAt }),
|
|
306
|
+
...(spendControlReached === undefined ? {} : { spendControlReached }),
|
|
307
|
+
...(resetCredits !== undefined && resetCredits >= 0 && Number.isSafeInteger(resetCredits) ? { resetCredits } : {}),
|
|
308
|
+
...(fetchedAt === undefined ? {} : { fetchedAt }),
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export function usageHasVisibleFields(usage: UsageView): boolean {
|
|
313
|
+
return (
|
|
314
|
+
usage.rateLimits.some((limit) => limit.windows.length > 0 || limit.name !== undefined) ||
|
|
315
|
+
usage.creditsUnlimited !== undefined ||
|
|
316
|
+
usage.creditsBalance !== undefined ||
|
|
317
|
+
usage.individualLimit !== undefined ||
|
|
318
|
+
usage.individualUsed !== undefined ||
|
|
319
|
+
usage.individualRemaining !== undefined ||
|
|
320
|
+
usage.individualRemainingPercent !== undefined ||
|
|
321
|
+
usage.spendControlReached === true ||
|
|
322
|
+
usage.resetCredits !== undefined
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
export function parseGateway(value: unknown): GatewayView | undefined {
|
|
327
|
+
if (!isRecord(value)) return undefined;
|
|
328
|
+
const bind = optionalString(value["bind"]);
|
|
329
|
+
const port = optionalFiniteNumber(value["port"]);
|
|
330
|
+
if (bind === undefined || port === undefined) return undefined;
|
|
331
|
+
const models = Array.isArray(value["models"])
|
|
332
|
+
? value["models"].filter((model): model is string => typeof model === "string" && model.length > 0)
|
|
333
|
+
: [];
|
|
334
|
+
const model = optionalString(value["model"]) ?? models[0] ?? null;
|
|
335
|
+
if (models.length === 0 && model !== null) models.push(model);
|
|
336
|
+
const keyAvailable = value["keyAvailable"] === true || value["keyConfigured"] === true;
|
|
337
|
+
return {
|
|
338
|
+
enabled: value["enabled"] === true,
|
|
339
|
+
running: value["running"] === true,
|
|
340
|
+
bind,
|
|
341
|
+
port,
|
|
342
|
+
model,
|
|
343
|
+
keyConfigured: keyAvailable,
|
|
344
|
+
keyAvailable,
|
|
345
|
+
keyHint: optionalString(value["keyHint"]) ?? "",
|
|
346
|
+
models,
|
|
347
|
+
warning: optionalString(value["warning"]) ?? "",
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export function formatGatewayBaseUrl(bind: string, port: number): string {
|
|
352
|
+
const host = bind.includes(":") && !bind.startsWith("[") ? `[${bind}]` : bind;
|
|
353
|
+
return `http://${host}:${String(port)}`;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
export function randomGatewayPort(exclude?: number): number {
|
|
357
|
+
for (let attempt = 0; attempt < 32; attempt += 1) {
|
|
358
|
+
const span = GATEWAY_RANDOM_PORT_MAX - GATEWAY_RANDOM_PORT_MIN + 1;
|
|
359
|
+
const candidate = GATEWAY_RANDOM_PORT_MIN + Math.floor(Math.random() * span);
|
|
360
|
+
if (candidate !== exclude && !GATEWAY_RANDOM_RESERVED.has(candidate)) return candidate;
|
|
361
|
+
}
|
|
362
|
+
return exclude === GATEWAY_RANDOM_PORT_MIN ? GATEWAY_RANDOM_PORT_MIN + 1 : GATEWAY_RANDOM_PORT_MIN;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
export function parseGatewayPort(value: string): number | undefined {
|
|
366
|
+
const port = Number(value);
|
|
367
|
+
if (!Number.isInteger(port) || port < GATEWAY_PORT_MIN || port > GATEWAY_PORT_MAX) return undefined;
|
|
368
|
+
return port;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
export function parseImagineCredential(value: unknown): ImagineCredentialView | undefined {
|
|
372
|
+
if (!isRecord(value)) return undefined;
|
|
373
|
+
const configured = optionalBoolean(value["configured"]);
|
|
374
|
+
if (configured === undefined && value["source"] === undefined && value["writable"] === undefined) return undefined;
|
|
375
|
+
const source = optionalString(value["source"]);
|
|
376
|
+
const writable = optionalBoolean(value["writable"]);
|
|
377
|
+
return {
|
|
378
|
+
configured: configured === true,
|
|
379
|
+
...(source === undefined || looksSecret(source) ? {} : { source }),
|
|
380
|
+
...(writable === undefined ? {} : { writable }),
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
export function imagineSourceLabel(source: string | undefined, t: GrokBuildSettingsInjected["t"]): string {
|
|
385
|
+
if (source === undefined) return t("imagineSourceUnknown");
|
|
386
|
+
const mapped = IMAGINE_SOURCE_KEY[source] ?? IMAGINE_SOURCE_KEY[source.toLowerCase()];
|
|
387
|
+
if (mapped !== undefined) return t(mapped);
|
|
388
|
+
if (source.length <= 40 && /^[a-z0-9._-]+$/iu.test(source) && !looksSecret(source)) return source;
|
|
389
|
+
return t("imagineSourceUnknown");
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
export function modelFields(status: ProviderStatus): { available: string[]; selected: string[] } {
|
|
393
|
+
if (status.status !== "signed-in") return { available: [], selected: [] };
|
|
394
|
+
return {
|
|
395
|
+
available: "available" in status ? status.available : [],
|
|
396
|
+
selected: "selected" in status ? status.selected : [],
|
|
397
|
+
};
|
|
398
|
+
}
|