dsh-coding-subscription-oauth 0.6.3 → 0.6.4
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 +258 -250
- package/CONTRIBUTING.md +129 -129
- package/INSTALL.md +256 -255
- package/LICENSE +19 -19
- package/NOTICE +11 -11
- package/README.de.md +303 -303
- package/README.es.md +304 -304
- package/README.fr.md +304 -304
- package/README.ja.md +304 -304
- package/README.ko.md +304 -304
- package/README.md +320 -320
- package/README.pt-BR.md +304 -304
- package/README.ru.md +304 -304
- package/README.zh-CN.md +318 -318
- package/compatibility/dsh-bom.json +30 -30
- package/cordis.patch.yml +13 -13
- package/docs/00-project-rules.md +212 -212
- package/docs/02-architecture.md +138 -138
- package/docs/02-architecture.zh-CN.md +138 -138
- package/lib/bin.js +1333 -569
- package/lib/bin.js.map +4 -4
- package/lib/client.js +1 -1
- package/lib/client.js.map +2 -2
- package/lib/index.js +689 -25492
- package/lib/index.js.map +4 -4
- 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 +223 -223
- 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 +348 -348
- package/src/alias-adapter.ts +147 -147
- 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 -314
- package/src/capability-settings.ts +671 -671
- package/src/capability-tools.ts +685 -685
- package/src/catalog.ts +271 -271
- package/src/client/GrokBuildSettings.tsx +771 -771
- 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 -230
- 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 -540
- package/src/client/microStyles.ts +52 -52
- package/src/client/parsers.ts +398 -398
- package/src/client/styles.ts +325 -325
- package/src/client/types.ts +199 -199
- package/src/codex-http.ts +447 -447
- package/src/codex-images.ts +503 -503
- 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 +55 -55
- 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 +735 -735
- 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
|
@@ -1,771 +1,771 @@
|
|
|
1
|
-
/** Plugin-owned coding subscription account section inside the dsh Settings shell. */
|
|
2
|
-
|
|
3
|
-
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
4
|
-
import { cancelPreviewTicket, copyText, isConflictError, isConsumedPreviewError, jsonRequest } from "./api.ts";
|
|
5
|
-
import { AboutTab } from "./components/AboutTab.tsx";
|
|
6
|
-
import { AccountsTab } from "./components/AccountsTab.tsx";
|
|
7
|
-
import { CapabilitiesTab } from "./components/CapabilitiesTab.tsx";
|
|
8
|
-
import { GatewayTab } from "./components/GatewayTab.tsx";
|
|
9
|
-
import type { SettingsTabHint } from "./components/SettingsTabs.tsx";
|
|
10
|
-
import { SettingsTabs } from "./components/SettingsTabs.tsx";
|
|
11
|
-
import {
|
|
12
|
-
CAPABILITIES_PATH,
|
|
13
|
-
CODEX_USAGE_PATH,
|
|
14
|
-
GATEWAY_PATH,
|
|
15
|
-
GATEWAY_REVEAL_PATH,
|
|
16
|
-
GATEWAY_ROTATE_PATH,
|
|
17
|
-
IMAGINE_CREDENTIAL_PATH,
|
|
18
|
-
LOGIN_CANCEL_PATH,
|
|
19
|
-
LOGIN_CODE_PATH,
|
|
20
|
-
LOGIN_PATH,
|
|
21
|
-
LOGOUT_PATH,
|
|
22
|
-
MODELS_PATH,
|
|
23
|
-
POLL_INTERVAL_MS,
|
|
24
|
-
SOURCE_COMMIT_ACTION_KEY,
|
|
25
|
-
SOURCES_CANCEL_PATH,
|
|
26
|
-
SOURCES_COMMIT_PATH,
|
|
27
|
-
SOURCES_PATH,
|
|
28
|
-
SOURCES_PREVIEW_PATH,
|
|
29
|
-
STATUS_PATH,
|
|
30
|
-
} from "./constants.ts";
|
|
31
|
-
import { ensureMicroStyles } from "./microStyles.ts";
|
|
32
|
-
import {
|
|
33
|
-
emptyCapabilitySettings,
|
|
34
|
-
mergeSources,
|
|
35
|
-
parseCapabilities,
|
|
36
|
-
parseCommitAction,
|
|
37
|
-
parseGateway,
|
|
38
|
-
parseGatewayPort,
|
|
39
|
-
parseImagineCredential,
|
|
40
|
-
parsePreview,
|
|
41
|
-
parseSources,
|
|
42
|
-
parseUsage,
|
|
43
|
-
} from "./parsers.ts";
|
|
44
|
-
import { bodyStyle, buttonStyle, errorStyle, pageStyle, panelStyle, titleStyle } from "./styles.ts";
|
|
45
|
-
import type {
|
|
46
|
-
CapabilitySettingKey,
|
|
47
|
-
CapabilitySettingsView,
|
|
48
|
-
CapabilitySnapshot,
|
|
49
|
-
CodingOAuthStatus,
|
|
50
|
-
CopyField,
|
|
51
|
-
GatewayView,
|
|
52
|
-
GrokBuildSettingsProps,
|
|
53
|
-
ImagineCredentialView,
|
|
54
|
-
LoginChallenge,
|
|
55
|
-
LoginMethod,
|
|
56
|
-
ProviderSlug,
|
|
57
|
-
SettingsTabId,
|
|
58
|
-
SourceKind,
|
|
59
|
-
SourcePreview,
|
|
60
|
-
SourceStatus,
|
|
61
|
-
UsageView,
|
|
62
|
-
} from "./types.ts";
|
|
63
|
-
|
|
64
|
-
export type { GrokBuildSettingsInjected, GrokBuildSettingsProps } from "./types.ts";
|
|
65
|
-
|
|
66
|
-
const REMOTE_TIP_STORAGE_KEY = "dsh-coding-oauth-remote-tip-dismissed";
|
|
67
|
-
|
|
68
|
-
function readRemoteTipDismissed(): boolean {
|
|
69
|
-
try {
|
|
70
|
-
return globalThis.sessionStorage?.getItem(REMOTE_TIP_STORAGE_KEY) === "1";
|
|
71
|
-
} catch {
|
|
72
|
-
return false;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/** Multi-provider coding subscription status and OAuth actions. */
|
|
77
|
-
export function GrokBuildSettings({ t }: GrokBuildSettingsProps) {
|
|
78
|
-
if (t === undefined) throw new Error("Coding OAuth settings requires its translation function");
|
|
79
|
-
|
|
80
|
-
const [status, setStatus] = useState<CodingOAuthStatus | undefined>(undefined);
|
|
81
|
-
const [requestError, setRequestError] = useState<string | undefined>(undefined);
|
|
82
|
-
const [busyProvider, setBusyProvider] = useState<ProviderSlug | undefined>(undefined);
|
|
83
|
-
const [codeInputs, setCodeInputs] = useState<Partial<Record<ProviderSlug, string>>>({});
|
|
84
|
-
const [popupBlocked, setPopupBlocked] = useState<Partial<Record<ProviderSlug, boolean>>>({});
|
|
85
|
-
const [sources, setSources] = useState<SourceStatus[] | undefined>(undefined);
|
|
86
|
-
const [sourcesError, setSourcesError] = useState<string | undefined>(undefined);
|
|
87
|
-
const [sourcesBusy, setSourcesBusy] = useState(false);
|
|
88
|
-
const [preview, setPreview] = useState<SourcePreview | undefined>(undefined);
|
|
89
|
-
const previewRef = useRef<SourcePreview | undefined>(undefined);
|
|
90
|
-
const previewEpochRef = useRef(0);
|
|
91
|
-
const mountedRef = useRef(true);
|
|
92
|
-
const [confirmOverwrite, setConfirmOverwrite] = useState(false);
|
|
93
|
-
const [sourcesNotice, setSourcesNotice] = useState<string | undefined>(undefined);
|
|
94
|
-
const [capabilities, setCapabilities] = useState<CapabilitySnapshot | undefined>(undefined);
|
|
95
|
-
const [capabilitiesError, setCapabilitiesError] = useState<string | undefined>(undefined);
|
|
96
|
-
const [capabilitiesBusy, setCapabilitiesBusy] = useState(false);
|
|
97
|
-
const [capabilitiesLoaded, setCapabilitiesLoaded] = useState(false);
|
|
98
|
-
const [usage, setUsage] = useState<UsageView | undefined>(undefined);
|
|
99
|
-
const [usageError, setUsageError] = useState<string | undefined>(undefined);
|
|
100
|
-
const [usageLoading, setUsageLoading] = useState(false);
|
|
101
|
-
const [imagine, setImagine] = useState<ImagineCredentialView | undefined>(undefined);
|
|
102
|
-
const [imagineError, setImagineError] = useState<string | undefined>(undefined);
|
|
103
|
-
const [gateway, setGateway] = useState<GatewayView | undefined>(undefined);
|
|
104
|
-
const [gatewayError, setGatewayError] = useState<string | undefined>(undefined);
|
|
105
|
-
const [gatewayBusy, setGatewayBusy] = useState(false);
|
|
106
|
-
const [gatewayLoaded, setGatewayLoaded] = useState(false);
|
|
107
|
-
const [gatewayOnceKey, setGatewayOnceKey] = useState<string | undefined>(undefined);
|
|
108
|
-
const [gatewayKeyVisible, setGatewayKeyVisible] = useState(false);
|
|
109
|
-
const [gatewayRotateConfirm, setGatewayRotateConfirm] = useState(false);
|
|
110
|
-
const [gatewayRevealError, setGatewayRevealError] = useState<string | undefined>(undefined);
|
|
111
|
-
const [portDraft, setPortDraft] = useState("");
|
|
112
|
-
const [activeTab, setActiveTab] = useState<SettingsTabId>("accounts");
|
|
113
|
-
const [copiedField, setCopiedField] = useState<CopyField | undefined>(undefined);
|
|
114
|
-
const [copyFailedField, setCopyFailedField] = useState<CopyField | undefined>(undefined);
|
|
115
|
-
const [expandedProviders, setExpandedProviders] = useState<Partial<Record<ProviderSlug, boolean>>>({});
|
|
116
|
-
const [remoteTipDismissed, setRemoteTipDismissed] = useState(readRemoteTipDismissed);
|
|
117
|
-
const copiedTimerRef = useRef<number | undefined>(undefined);
|
|
118
|
-
const remote = status?.accessMode === "ssh-tunnel" || status?.accessMode === "trusted-https-proxy";
|
|
119
|
-
|
|
120
|
-
const refresh = useCallback(async () => {
|
|
121
|
-
try {
|
|
122
|
-
const next = await jsonRequest<CodingOAuthStatus>(STATUS_PATH);
|
|
123
|
-
setStatus(next);
|
|
124
|
-
setRequestError(undefined);
|
|
125
|
-
return next;
|
|
126
|
-
} catch (error: unknown) {
|
|
127
|
-
setRequestError(error instanceof Error ? error.message : t("requestFailed"));
|
|
128
|
-
return undefined;
|
|
129
|
-
}
|
|
130
|
-
}, [t]);
|
|
131
|
-
|
|
132
|
-
const refreshSources = useCallback(async () => {
|
|
133
|
-
try {
|
|
134
|
-
setSources(parseSources(await jsonRequest<unknown>(SOURCES_PATH)));
|
|
135
|
-
setSourcesError(undefined);
|
|
136
|
-
} catch (error: unknown) {
|
|
137
|
-
setSources(mergeSources([]));
|
|
138
|
-
setSourcesError(error instanceof Error ? error.message : t("sourcesLoadFailed"));
|
|
139
|
-
}
|
|
140
|
-
}, [t]);
|
|
141
|
-
|
|
142
|
-
const refreshCapabilities = useCallback(async () => {
|
|
143
|
-
try {
|
|
144
|
-
const parsed = parseCapabilities(await jsonRequest<unknown>(CAPABILITIES_PATH));
|
|
145
|
-
setCapabilities(parsed ?? { value: emptyCapabilitySettings(), revision: 0, writable: false });
|
|
146
|
-
setCapabilitiesError(undefined);
|
|
147
|
-
return parsed;
|
|
148
|
-
} catch (error: unknown) {
|
|
149
|
-
setCapabilities({ value: emptyCapabilitySettings(), revision: 0, writable: false });
|
|
150
|
-
setCapabilitiesError(error instanceof Error ? error.message : t("capabilitiesLoadFailed"));
|
|
151
|
-
return undefined;
|
|
152
|
-
} finally {
|
|
153
|
-
setCapabilitiesLoaded(true);
|
|
154
|
-
}
|
|
155
|
-
}, [t]);
|
|
156
|
-
|
|
157
|
-
const refreshGateway = useCallback(async () => {
|
|
158
|
-
try {
|
|
159
|
-
setGateway(parseGateway(await jsonRequest<unknown>(GATEWAY_PATH)));
|
|
160
|
-
setGatewayError(undefined);
|
|
161
|
-
} catch (error: unknown) {
|
|
162
|
-
setGatewayError(error instanceof Error ? error.message : t("gatewayLoadFailed"));
|
|
163
|
-
} finally {
|
|
164
|
-
setGatewayLoaded(true);
|
|
165
|
-
}
|
|
166
|
-
}, [t]);
|
|
167
|
-
|
|
168
|
-
const refreshImagine = useCallback(async () => {
|
|
169
|
-
try {
|
|
170
|
-
setImagine(parseImagineCredential(await jsonRequest<unknown>(IMAGINE_CREDENTIAL_PATH)));
|
|
171
|
-
setImagineError(undefined);
|
|
172
|
-
} catch (error: unknown) {
|
|
173
|
-
setImagineError(error instanceof Error ? error.message : t("imagineLoadFailed"));
|
|
174
|
-
}
|
|
175
|
-
}, [t]);
|
|
176
|
-
|
|
177
|
-
const refreshUsage = useCallback(async () => {
|
|
178
|
-
setUsageLoading(true);
|
|
179
|
-
try {
|
|
180
|
-
setUsage(parseUsage(await jsonRequest<unknown>(CODEX_USAGE_PATH)));
|
|
181
|
-
setUsageError(undefined);
|
|
182
|
-
} catch (error: unknown) {
|
|
183
|
-
setUsage(undefined);
|
|
184
|
-
setUsageError(error instanceof Error ? error.message : t("usageUnavailable"));
|
|
185
|
-
} finally {
|
|
186
|
-
setUsageLoading(false);
|
|
187
|
-
}
|
|
188
|
-
}, [t]);
|
|
189
|
-
|
|
190
|
-
// Accounts: status immediately; sources shortly after (non-blocking for first paint).
|
|
191
|
-
useEffect(() => {
|
|
192
|
-
ensureMicroStyles();
|
|
193
|
-
let active = true;
|
|
194
|
-
let timer: number | undefined;
|
|
195
|
-
void refresh().then((next) => {
|
|
196
|
-
if (!active || next?.uiOwner === "hub") return;
|
|
197
|
-
timer = window.setTimeout(() => void refreshSources(), 0);
|
|
198
|
-
});
|
|
199
|
-
return () => {
|
|
200
|
-
active = false;
|
|
201
|
-
if (timer !== undefined) window.clearTimeout(timer);
|
|
202
|
-
};
|
|
203
|
-
}, [refresh, refreshSources]);
|
|
204
|
-
|
|
205
|
-
// Capabilities tab: load settings + Imagine status on first visit.
|
|
206
|
-
useEffect(() => {
|
|
207
|
-
if (activeTab !== "capabilities") return;
|
|
208
|
-
if (!capabilitiesLoaded) void refreshCapabilities();
|
|
209
|
-
void refreshImagine();
|
|
210
|
-
}, [activeTab, capabilitiesLoaded, refreshCapabilities, refreshImagine]);
|
|
211
|
-
|
|
212
|
-
// Soft-fetch capabilities when Codex is signed in so Accounts can show quota without opening the tab.
|
|
213
|
-
useEffect(() => {
|
|
214
|
-
if (capabilitiesLoaded) return;
|
|
215
|
-
if (status?.providers.codex.status !== "signed-in") return;
|
|
216
|
-
void refreshCapabilities();
|
|
217
|
-
}, [capabilitiesLoaded, refreshCapabilities, status?.providers.codex.status]);
|
|
218
|
-
|
|
219
|
-
// Gateway: load only when that tab is opened.
|
|
220
|
-
useEffect(() => {
|
|
221
|
-
if (activeTab !== "gateway" || gatewayLoaded) return;
|
|
222
|
-
void refreshGateway();
|
|
223
|
-
}, [activeTab, gatewayLoaded, refreshGateway]);
|
|
224
|
-
|
|
225
|
-
useEffect(() => {
|
|
226
|
-
previewRef.current = preview;
|
|
227
|
-
}, [preview]);
|
|
228
|
-
|
|
229
|
-
useEffect(() => {
|
|
230
|
-
mountedRef.current = true;
|
|
231
|
-
return () => {
|
|
232
|
-
mountedRef.current = false;
|
|
233
|
-
if (copiedTimerRef.current !== undefined) window.clearTimeout(copiedTimerRef.current);
|
|
234
|
-
previewEpochRef.current += 1;
|
|
235
|
-
const active = previewRef.current;
|
|
236
|
-
if (active !== undefined) cancelPreviewTicket(active.previewId, true);
|
|
237
|
-
};
|
|
238
|
-
}, []);
|
|
239
|
-
|
|
240
|
-
useEffect(() => {
|
|
241
|
-
const signingIn =
|
|
242
|
-
status !== undefined && Object.values(status.providers).some((provider) => provider.status === "signing-in");
|
|
243
|
-
if (!signingIn) return;
|
|
244
|
-
const timer = window.setInterval(() => {
|
|
245
|
-
void refresh();
|
|
246
|
-
}, POLL_INTERVAL_MS);
|
|
247
|
-
return () => {
|
|
248
|
-
window.clearInterval(timer);
|
|
249
|
-
};
|
|
250
|
-
}, [refresh, status]);
|
|
251
|
-
|
|
252
|
-
useEffect(() => {
|
|
253
|
-
const signedIn = status?.providers.codex.status === "signed-in";
|
|
254
|
-
if (capabilities?.value.codexUsage === true && signedIn) {
|
|
255
|
-
void refreshUsage();
|
|
256
|
-
return;
|
|
257
|
-
}
|
|
258
|
-
setUsage(undefined);
|
|
259
|
-
setUsageError(undefined);
|
|
260
|
-
setUsageLoading(false);
|
|
261
|
-
}, [capabilities?.value.codexUsage, refreshUsage, status?.providers.codex.status]);
|
|
262
|
-
|
|
263
|
-
useEffect(() => {
|
|
264
|
-
if (gateway !== undefined) setPortDraft(String(gateway.port));
|
|
265
|
-
}, [gateway]);
|
|
266
|
-
|
|
267
|
-
const signIn = async (provider: ProviderSlug, method: LoginMethod): Promise<void> => {
|
|
268
|
-
const popup = window.open("about:blank", "_blank");
|
|
269
|
-
if (popup !== null) popup.opener = null;
|
|
270
|
-
setBusyProvider(provider);
|
|
271
|
-
setRequestError(undefined);
|
|
272
|
-
setPopupBlocked((current) => ({ ...current, [provider]: popup === null }));
|
|
273
|
-
try {
|
|
274
|
-
const challenge = await jsonRequest<LoginChallenge>(LOGIN_PATH, "POST", { provider, method });
|
|
275
|
-
if (popup !== null) popup.location.replace(challenge.url);
|
|
276
|
-
await refresh();
|
|
277
|
-
} catch (error: unknown) {
|
|
278
|
-
popup?.close();
|
|
279
|
-
setRequestError(error instanceof Error ? error.message : t("requestFailed"));
|
|
280
|
-
await refresh();
|
|
281
|
-
} finally {
|
|
282
|
-
setBusyProvider(undefined);
|
|
283
|
-
}
|
|
284
|
-
};
|
|
285
|
-
|
|
286
|
-
const submitCode = async (provider: ProviderSlug): Promise<void> => {
|
|
287
|
-
const code = codeInputs[provider]?.trim() ?? "";
|
|
288
|
-
if (code.length === 0) return;
|
|
289
|
-
setBusyProvider(provider);
|
|
290
|
-
try {
|
|
291
|
-
await jsonRequest<{ ok: true }>(LOGIN_CODE_PATH, "POST", { provider, code });
|
|
292
|
-
setCodeInputs((current) => ({ ...current, [provider]: "" }));
|
|
293
|
-
await refresh();
|
|
294
|
-
} catch (error: unknown) {
|
|
295
|
-
setRequestError(error instanceof Error ? error.message : t("requestFailed"));
|
|
296
|
-
} finally {
|
|
297
|
-
setBusyProvider(undefined);
|
|
298
|
-
}
|
|
299
|
-
};
|
|
300
|
-
|
|
301
|
-
const cancelLogin = async (provider: ProviderSlug): Promise<void> => {
|
|
302
|
-
setBusyProvider(provider);
|
|
303
|
-
try {
|
|
304
|
-
setStatus(await jsonRequest<CodingOAuthStatus>(LOGIN_CANCEL_PATH, "POST", { provider }));
|
|
305
|
-
} catch (error: unknown) {
|
|
306
|
-
setRequestError(error instanceof Error ? error.message : t("requestFailed"));
|
|
307
|
-
} finally {
|
|
308
|
-
setBusyProvider(undefined);
|
|
309
|
-
}
|
|
310
|
-
};
|
|
311
|
-
|
|
312
|
-
const signOut = async (provider: ProviderSlug): Promise<void> => {
|
|
313
|
-
setBusyProvider(provider);
|
|
314
|
-
try {
|
|
315
|
-
setStatus(await jsonRequest<CodingOAuthStatus>(LOGOUT_PATH, "POST", { provider }));
|
|
316
|
-
} catch (error: unknown) {
|
|
317
|
-
setRequestError(error instanceof Error ? error.message : t("requestFailed"));
|
|
318
|
-
} finally {
|
|
319
|
-
setBusyProvider(undefined);
|
|
320
|
-
}
|
|
321
|
-
};
|
|
322
|
-
|
|
323
|
-
const saveModels = async (provider: ProviderSlug, selected: string[]): Promise<void> => {
|
|
324
|
-
setBusyProvider(provider);
|
|
325
|
-
try {
|
|
326
|
-
setStatus(await jsonRequest<CodingOAuthStatus>(MODELS_PATH, "POST", { provider, selected }));
|
|
327
|
-
} catch (error: unknown) {
|
|
328
|
-
setRequestError(error instanceof Error ? error.message : t("requestFailed"));
|
|
329
|
-
} finally {
|
|
330
|
-
setBusyProvider(undefined);
|
|
331
|
-
}
|
|
332
|
-
};
|
|
333
|
-
|
|
334
|
-
const previewSource = async (kind: SourceKind): Promise<void> => {
|
|
335
|
-
const epoch = ++previewEpochRef.current;
|
|
336
|
-
const previous = previewRef.current;
|
|
337
|
-
if (previous !== undefined) {
|
|
338
|
-
previewRef.current = undefined;
|
|
339
|
-
setPreview(undefined);
|
|
340
|
-
cancelPreviewTicket(previous.previewId);
|
|
341
|
-
}
|
|
342
|
-
setSourcesBusy(true);
|
|
343
|
-
setSourcesNotice(undefined);
|
|
344
|
-
setConfirmOverwrite(false);
|
|
345
|
-
try {
|
|
346
|
-
const next = parsePreview(await jsonRequest<unknown>(SOURCES_PREVIEW_PATH, "POST", { kind }));
|
|
347
|
-
if (next === undefined) throw new Error(t("sourcesPreviewFailed"));
|
|
348
|
-
if (!mountedRef.current || epoch !== previewEpochRef.current) {
|
|
349
|
-
cancelPreviewTicket(next.previewId, !mountedRef.current);
|
|
350
|
-
return;
|
|
351
|
-
}
|
|
352
|
-
previewRef.current = next;
|
|
353
|
-
setPreview(next);
|
|
354
|
-
setSourcesError(undefined);
|
|
355
|
-
} catch (error: unknown) {
|
|
356
|
-
if (!mountedRef.current || epoch !== previewEpochRef.current) return;
|
|
357
|
-
setPreview(undefined);
|
|
358
|
-
setSourcesError(error instanceof Error ? error.message : t("sourcesPreviewFailed"));
|
|
359
|
-
} finally {
|
|
360
|
-
if (mountedRef.current && epoch === previewEpochRef.current) setSourcesBusy(false);
|
|
361
|
-
}
|
|
362
|
-
};
|
|
363
|
-
|
|
364
|
-
const commitSource = async (): Promise<void> => {
|
|
365
|
-
if (preview === undefined) return;
|
|
366
|
-
if (preview.action === "blocked") return;
|
|
367
|
-
if (preview.confirmOverwriteRequired && !confirmOverwrite) return;
|
|
368
|
-
setSourcesBusy(true);
|
|
369
|
-
try {
|
|
370
|
-
const result = await jsonRequest<unknown>(SOURCES_COMMIT_PATH, "POST", {
|
|
371
|
-
kind: preview.kind,
|
|
372
|
-
previewId: preview.previewId,
|
|
373
|
-
confirmOverwrite,
|
|
374
|
-
});
|
|
375
|
-
const action = parseCommitAction(result);
|
|
376
|
-
previewRef.current = undefined;
|
|
377
|
-
setPreview(undefined);
|
|
378
|
-
setConfirmOverwrite(false);
|
|
379
|
-
setSourcesNotice(
|
|
380
|
-
t("sourcesCommitSuccess", { action: action === undefined ? "" : t(SOURCE_COMMIT_ACTION_KEY[action]) }),
|
|
381
|
-
);
|
|
382
|
-
setSourcesError(undefined);
|
|
383
|
-
await refreshSources();
|
|
384
|
-
await refresh();
|
|
385
|
-
} catch (error: unknown) {
|
|
386
|
-
if (isConsumedPreviewError(error)) {
|
|
387
|
-
previewRef.current = undefined;
|
|
388
|
-
setPreview(undefined);
|
|
389
|
-
setConfirmOverwrite(false);
|
|
390
|
-
}
|
|
391
|
-
setSourcesError(error instanceof Error ? error.message : t("sourcesCommitFailed"));
|
|
392
|
-
await refreshSources();
|
|
393
|
-
} finally {
|
|
394
|
-
setSourcesBusy(false);
|
|
395
|
-
}
|
|
396
|
-
};
|
|
397
|
-
|
|
398
|
-
const cancelSourcePreview = async (): Promise<void> => {
|
|
399
|
-
if (preview === undefined) return;
|
|
400
|
-
const previewId = preview.previewId;
|
|
401
|
-
previewEpochRef.current += 1;
|
|
402
|
-
previewRef.current = undefined;
|
|
403
|
-
setPreview(undefined);
|
|
404
|
-
setConfirmOverwrite(false);
|
|
405
|
-
setSourcesBusy(true);
|
|
406
|
-
try {
|
|
407
|
-
await jsonRequest<unknown>(SOURCES_CANCEL_PATH, "POST", { previewId });
|
|
408
|
-
setSourcesError(undefined);
|
|
409
|
-
} catch (error: unknown) {
|
|
410
|
-
setSourcesError(error instanceof Error ? error.message : t("requestFailed"));
|
|
411
|
-
} finally {
|
|
412
|
-
setSourcesBusy(false);
|
|
413
|
-
}
|
|
414
|
-
};
|
|
415
|
-
|
|
416
|
-
const patchCapability = async (key: CapabilitySettingKey, value: boolean | number): Promise<boolean> => {
|
|
417
|
-
if (capabilities === undefined || !capabilities.writable) return false;
|
|
418
|
-
if (key === "codexImageEdits" && value === true && !capabilities.value.codexImages) return false;
|
|
419
|
-
if (key === "codexImagesAnyModel" && value === true && !capabilities.value.codexImages) return false;
|
|
420
|
-
const patch: Partial<CapabilitySettingsView> =
|
|
421
|
-
key === "codexImages" && value === false
|
|
422
|
-
? { codexImages: false, codexImageEdits: false, codexImagesAnyModel: false }
|
|
423
|
-
: ({ [key]: value } as Partial<CapabilitySettingsView>);
|
|
424
|
-
setCapabilitiesBusy(true);
|
|
425
|
-
try {
|
|
426
|
-
const updated = parseCapabilities(
|
|
427
|
-
await jsonRequest<unknown>(CAPABILITIES_PATH, "PATCH", {
|
|
428
|
-
expectedRevision: capabilities.revision,
|
|
429
|
-
patch,
|
|
430
|
-
}),
|
|
431
|
-
);
|
|
432
|
-
if (updated !== undefined) setCapabilities(updated);
|
|
433
|
-
else await refreshCapabilities();
|
|
434
|
-
setCapabilitiesError(undefined);
|
|
435
|
-
return true;
|
|
436
|
-
} catch (error: unknown) {
|
|
437
|
-
await refreshCapabilities();
|
|
438
|
-
setCapabilitiesError(
|
|
439
|
-
isConflictError(error)
|
|
440
|
-
? t("capabilitiesConflictRefreshed")
|
|
441
|
-
: error instanceof Error
|
|
442
|
-
? error.message
|
|
443
|
-
: t("capabilitiesSaveFailed"),
|
|
444
|
-
);
|
|
445
|
-
return false;
|
|
446
|
-
} finally {
|
|
447
|
-
setCapabilitiesBusy(false);
|
|
448
|
-
}
|
|
449
|
-
};
|
|
450
|
-
|
|
451
|
-
const showUsage = capabilities?.value.codexUsage === true && status?.providers.codex.status === "signed-in";
|
|
452
|
-
const signedInCount =
|
|
453
|
-
status === undefined
|
|
454
|
-
? 0
|
|
455
|
-
: Object.values(status.providers).filter((provider) => provider.status === "signed-in").length;
|
|
456
|
-
|
|
457
|
-
const tabHints = useMemo((): readonly SettingsTabHint[] => {
|
|
458
|
-
const hints: SettingsTabHint[] = [];
|
|
459
|
-
if (status !== undefined) {
|
|
460
|
-
const signedInCount = Object.values(status.providers).filter(
|
|
461
|
-
(provider) => provider.status === "signed-in",
|
|
462
|
-
).length;
|
|
463
|
-
if (signedInCount > 0) {
|
|
464
|
-
hints.push({ id: "accounts", suffix: String(signedInCount) });
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
if (gateway?.running === true) {
|
|
468
|
-
hints.push({ id: "gateway", suffix: t("tabGatewayActive") });
|
|
469
|
-
}
|
|
470
|
-
return hints;
|
|
471
|
-
}, [gateway?.running, status, t]);
|
|
472
|
-
|
|
473
|
-
const markCopied = (field: CopyField): void => {
|
|
474
|
-
if (copiedTimerRef.current !== undefined) window.clearTimeout(copiedTimerRef.current);
|
|
475
|
-
setCopiedField(field);
|
|
476
|
-
setCopyFailedField(undefined);
|
|
477
|
-
copiedTimerRef.current = window.setTimeout(() => {
|
|
478
|
-
if (mountedRef.current) {
|
|
479
|
-
setCopiedField(undefined);
|
|
480
|
-
setCopyFailedField(undefined);
|
|
481
|
-
}
|
|
482
|
-
copiedTimerRef.current = undefined;
|
|
483
|
-
}, 2000);
|
|
484
|
-
};
|
|
485
|
-
|
|
486
|
-
const handleCopy = async (field: CopyField, text: string): Promise<void> => {
|
|
487
|
-
const ok = await copyText(text);
|
|
488
|
-
if (ok) {
|
|
489
|
-
markCopied(field);
|
|
490
|
-
return;
|
|
491
|
-
}
|
|
492
|
-
setCopiedField(undefined);
|
|
493
|
-
setCopyFailedField(field);
|
|
494
|
-
};
|
|
495
|
-
|
|
496
|
-
const ensureGatewayKey = async (): Promise<string> => {
|
|
497
|
-
if (gatewayOnceKey !== undefined) return gatewayOnceKey;
|
|
498
|
-
const value = await jsonRequest<{ apiKey?: string }>(GATEWAY_REVEAL_PATH, "POST");
|
|
499
|
-
if (typeof value.apiKey !== "string" || value.apiKey.length === 0) {
|
|
500
|
-
throw new Error(t("gatewayRevealFailed"));
|
|
501
|
-
}
|
|
502
|
-
setGatewayOnceKey(value.apiKey);
|
|
503
|
-
return value.apiKey;
|
|
504
|
-
};
|
|
505
|
-
|
|
506
|
-
const copyGatewayKey = async (): Promise<void> => {
|
|
507
|
-
try {
|
|
508
|
-
const key = await ensureGatewayKey();
|
|
509
|
-
await handleCopy("key", key);
|
|
510
|
-
setGatewayRevealError(undefined);
|
|
511
|
-
} catch {
|
|
512
|
-
setGatewayRevealError(t("gatewayRevealFailed"));
|
|
513
|
-
}
|
|
514
|
-
};
|
|
515
|
-
|
|
516
|
-
const toggleGatewayKeyVisible = async (): Promise<void> => {
|
|
517
|
-
if (gatewayKeyVisible) {
|
|
518
|
-
setGatewayKeyVisible(false);
|
|
519
|
-
return;
|
|
520
|
-
}
|
|
521
|
-
try {
|
|
522
|
-
await ensureGatewayKey();
|
|
523
|
-
setGatewayKeyVisible(true);
|
|
524
|
-
setGatewayRevealError(undefined);
|
|
525
|
-
} catch {
|
|
526
|
-
setGatewayRevealError(t("gatewayRevealFailed"));
|
|
527
|
-
}
|
|
528
|
-
};
|
|
529
|
-
|
|
530
|
-
const applyGatewayPort = async (): Promise<void> => {
|
|
531
|
-
const port = parseGatewayPort(portDraft);
|
|
532
|
-
if (port === undefined) {
|
|
533
|
-
setGatewayError(t("gatewayPortInvalid"));
|
|
534
|
-
return;
|
|
535
|
-
}
|
|
536
|
-
if (gateway !== undefined && port === gateway.port) return;
|
|
537
|
-
setGatewayBusy(true);
|
|
538
|
-
try {
|
|
539
|
-
setGateway(parseGateway(await jsonRequest<unknown>(GATEWAY_PATH, "PATCH", { port })) ?? gateway);
|
|
540
|
-
setGatewayError(undefined);
|
|
541
|
-
} catch (error: unknown) {
|
|
542
|
-
setGatewayError(error instanceof Error ? error.message : t("gatewaySaveFailed"));
|
|
543
|
-
if (gateway !== undefined) setPortDraft(String(gateway.port));
|
|
544
|
-
} finally {
|
|
545
|
-
setGatewayBusy(false);
|
|
546
|
-
}
|
|
547
|
-
};
|
|
548
|
-
|
|
549
|
-
const rotateGatewayKey = async (): Promise<void> => {
|
|
550
|
-
setGatewayBusy(true);
|
|
551
|
-
try {
|
|
552
|
-
const value = await jsonRequest<{ apiKey?: string; keyHint?: string }>(GATEWAY_ROTATE_PATH, "POST");
|
|
553
|
-
if (typeof value.apiKey === "string") setGatewayOnceKey(value.apiKey);
|
|
554
|
-
setGatewayKeyVisible(true);
|
|
555
|
-
setGatewayRotateConfirm(false);
|
|
556
|
-
setGatewayRevealError(undefined);
|
|
557
|
-
await refreshGateway();
|
|
558
|
-
} catch (error: unknown) {
|
|
559
|
-
setGatewayError(error instanceof Error ? error.message : t("gatewaySaveFailed"));
|
|
560
|
-
} finally {
|
|
561
|
-
setGatewayBusy(false);
|
|
562
|
-
}
|
|
563
|
-
};
|
|
564
|
-
|
|
565
|
-
const setGatewayEnabled = (enabled: boolean): void => {
|
|
566
|
-
setGatewayBusy(true);
|
|
567
|
-
void jsonRequest<unknown>(GATEWAY_PATH, "PATCH", { enabled })
|
|
568
|
-
.then((value) => {
|
|
569
|
-
setGateway(parseGateway(value) ?? gateway);
|
|
570
|
-
setGatewayError(undefined);
|
|
571
|
-
})
|
|
572
|
-
.catch((error: unknown) => {
|
|
573
|
-
setGatewayError(error instanceof Error ? error.message : t("gatewaySaveFailed"));
|
|
574
|
-
})
|
|
575
|
-
.finally(() => {
|
|
576
|
-
setGatewayBusy(false);
|
|
577
|
-
});
|
|
578
|
-
};
|
|
579
|
-
|
|
580
|
-
const dismissRemoteTip = (): void => {
|
|
581
|
-
setRemoteTipDismissed(true);
|
|
582
|
-
try {
|
|
583
|
-
globalThis.sessionStorage?.setItem(REMOTE_TIP_STORAGE_KEY, "1");
|
|
584
|
-
} catch {
|
|
585
|
-
// ignore storage failures
|
|
586
|
-
}
|
|
587
|
-
};
|
|
588
|
-
|
|
589
|
-
const openAccountsForCodex = (): void => {
|
|
590
|
-
setActiveTab("accounts");
|
|
591
|
-
window.setTimeout(() => document.getElementById("coding-oauth-login-codex")?.focus(), 0);
|
|
592
|
-
};
|
|
593
|
-
|
|
594
|
-
const focusCapabilityDependency = (target: "codexImages" | "imagineCredential"): void => {
|
|
595
|
-
const id = target === "codexImages" ? "cap-switch-codexImages" : "coding-oauth-imagine-credential";
|
|
596
|
-
document.getElementById(id)?.focus();
|
|
597
|
-
};
|
|
598
|
-
|
|
599
|
-
if (status?.uiOwner === "hub") {
|
|
600
|
-
return (
|
|
601
|
-
<section data-dsh-coding-oauth="compact" style={pageStyle} aria-labelledby="coding-oauth-settings-title">
|
|
602
|
-
<h2 id="coding-oauth-settings-title" style={titleStyle}>
|
|
603
|
-
{t("coinstallTitle")}
|
|
604
|
-
</h2>
|
|
605
|
-
<p style={bodyStyle}>{t("coinstallSummary", { count: signedInCount })}</p>
|
|
606
|
-
<button
|
|
607
|
-
type="button"
|
|
608
|
-
style={buttonStyle}
|
|
609
|
-
onClick={() => window.dispatchEvent(new CustomEvent("usage-stats:open-dashboard"))}
|
|
610
|
-
>
|
|
611
|
-
{t("coinstallOpenHub")}
|
|
612
|
-
</button>
|
|
613
|
-
</section>
|
|
614
|
-
);
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
return (
|
|
618
|
-
<section data-dsh-coding-oauth="" style={pageStyle} aria-labelledby="coding-oauth-settings-title">
|
|
619
|
-
<div>
|
|
620
|
-
<h2 id="coding-oauth-settings-title" style={titleStyle}>
|
|
621
|
-
{t("title")}
|
|
622
|
-
</h2>
|
|
623
|
-
{activeTab === "accounts" ? <p style={{ ...bodyStyle, marginTop: 6 }}>{t("intro")}</p> : null}
|
|
624
|
-
</div>
|
|
625
|
-
{requestError === undefined ? null : (
|
|
626
|
-
<div role="alert" style={{ display: "flex", flexWrap: "wrap", alignItems: "center", gap: 8 }}>
|
|
627
|
-
<p style={errorStyle}>{requestError}</p>
|
|
628
|
-
{status === undefined ? (
|
|
629
|
-
<button type="button" style={buttonStyle} onClick={() => void refresh()}>
|
|
630
|
-
{t("retry")}
|
|
631
|
-
</button>
|
|
632
|
-
) : null}
|
|
633
|
-
</div>
|
|
634
|
-
)}
|
|
635
|
-
<SettingsTabs t={t} activeTab={activeTab} onChange={setActiveTab} hints={tabHints} />
|
|
636
|
-
<div
|
|
637
|
-
id={`coding-oauth-panel-${activeTab}`}
|
|
638
|
-
role="tabpanel"
|
|
639
|
-
aria-labelledby={`coding-oauth-tab-${activeTab}`}
|
|
640
|
-
style={panelStyle}
|
|
641
|
-
>
|
|
642
|
-
{activeTab === "accounts" && !(status === undefined && requestError !== undefined) ? (
|
|
643
|
-
<AccountsTab
|
|
644
|
-
t={t}
|
|
645
|
-
status={status}
|
|
646
|
-
remote={remote}
|
|
647
|
-
remoteTipDismissed={remoteTipDismissed}
|
|
648
|
-
onDismissRemoteTip={dismissRemoteTip}
|
|
649
|
-
sources={sources}
|
|
650
|
-
sourcesError={sourcesError}
|
|
651
|
-
sourcesNotice={sourcesNotice}
|
|
652
|
-
sourcesBusy={sourcesBusy}
|
|
653
|
-
preview={preview}
|
|
654
|
-
confirmOverwrite={confirmOverwrite}
|
|
655
|
-
busyProvider={busyProvider}
|
|
656
|
-
codeInputs={codeInputs}
|
|
657
|
-
popupBlocked={popupBlocked}
|
|
658
|
-
expandedProviders={expandedProviders}
|
|
659
|
-
showUsage={showUsage === true}
|
|
660
|
-
usage={usage}
|
|
661
|
-
usageError={usageError}
|
|
662
|
-
usageLoading={usageLoading}
|
|
663
|
-
onSignIn={(slug, method) => {
|
|
664
|
-
void signIn(slug, method);
|
|
665
|
-
}}
|
|
666
|
-
onSignOut={(slug) => {
|
|
667
|
-
void signOut(slug);
|
|
668
|
-
}}
|
|
669
|
-
onCancelLogin={(slug) => {
|
|
670
|
-
void cancelLogin(slug);
|
|
671
|
-
}}
|
|
672
|
-
onSubmitCode={(slug) => {
|
|
673
|
-
void submitCode(slug);
|
|
674
|
-
}}
|
|
675
|
-
onCodeChange={(slug, value) => {
|
|
676
|
-
setCodeInputs((current) => ({ ...current, [slug]: value }));
|
|
677
|
-
}}
|
|
678
|
-
onToggleExpanded={(slug) => {
|
|
679
|
-
setExpandedProviders((current) => ({
|
|
680
|
-
...current,
|
|
681
|
-
[slug]: !(current[slug] === true),
|
|
682
|
-
}));
|
|
683
|
-
}}
|
|
684
|
-
onPreviewSource={(slug) => {
|
|
685
|
-
void previewSource(slug);
|
|
686
|
-
}}
|
|
687
|
-
onSaveModels={(slug, selected) => {
|
|
688
|
-
void saveModels(slug, selected);
|
|
689
|
-
}}
|
|
690
|
-
onConfirmOverwriteChange={setConfirmOverwrite}
|
|
691
|
-
onCommitSource={() => {
|
|
692
|
-
void commitSource();
|
|
693
|
-
}}
|
|
694
|
-
onCancelSourcePreview={() => {
|
|
695
|
-
void cancelSourcePreview();
|
|
696
|
-
}}
|
|
697
|
-
onRefreshSources={() => {
|
|
698
|
-
void refreshSources();
|
|
699
|
-
}}
|
|
700
|
-
onDismissSourcesNotice={() => {
|
|
701
|
-
setSourcesNotice(undefined);
|
|
702
|
-
}}
|
|
703
|
-
/>
|
|
704
|
-
) : null}
|
|
705
|
-
{activeTab === "capabilities" ? (
|
|
706
|
-
<CapabilitiesTab
|
|
707
|
-
t={t}
|
|
708
|
-
capabilities={capabilities}
|
|
709
|
-
capabilitiesError={capabilitiesError}
|
|
710
|
-
capabilitiesBusy={capabilitiesBusy}
|
|
711
|
-
imagine={imagine}
|
|
712
|
-
imagineError={imagineError}
|
|
713
|
-
codexSignedIn={status?.providers.codex.status === "signed-in"}
|
|
714
|
-
onRetry={() => {
|
|
715
|
-
void refreshCapabilities();
|
|
716
|
-
void refreshImagine();
|
|
717
|
-
}}
|
|
718
|
-
onOpenAccounts={openAccountsForCodex}
|
|
719
|
-
onFocusDependency={focusCapabilityDependency}
|
|
720
|
-
onPatchCapability={(key, value) => patchCapability(key, value)}
|
|
721
|
-
/>
|
|
722
|
-
) : null}
|
|
723
|
-
{activeTab === "gateway" ? (
|
|
724
|
-
<GatewayTab
|
|
725
|
-
t={t}
|
|
726
|
-
gateway={gateway}
|
|
727
|
-
gatewayError={gatewayError}
|
|
728
|
-
gatewayBusy={gatewayBusy}
|
|
729
|
-
gatewayOnceKey={gatewayOnceKey}
|
|
730
|
-
gatewayKeyVisible={gatewayKeyVisible}
|
|
731
|
-
gatewayRotateConfirm={gatewayRotateConfirm}
|
|
732
|
-
gatewayRevealError={gatewayRevealError}
|
|
733
|
-
portDraft={portDraft}
|
|
734
|
-
copiedField={copiedField}
|
|
735
|
-
copyFailedField={copyFailedField}
|
|
736
|
-
onRetry={() => {
|
|
737
|
-
void refreshGateway();
|
|
738
|
-
}}
|
|
739
|
-
onEnabledChange={setGatewayEnabled}
|
|
740
|
-
onPortDraftChange={setPortDraft}
|
|
741
|
-
onApplyPort={() => {
|
|
742
|
-
void applyGatewayPort();
|
|
743
|
-
}}
|
|
744
|
-
onRandomPort={(port) => {
|
|
745
|
-
setPortDraft(String(port));
|
|
746
|
-
}}
|
|
747
|
-
onCopy={(field, text) => {
|
|
748
|
-
void handleCopy(field, text);
|
|
749
|
-
}}
|
|
750
|
-
onCopyKey={() => {
|
|
751
|
-
void copyGatewayKey();
|
|
752
|
-
}}
|
|
753
|
-
onToggleKeyVisible={() => {
|
|
754
|
-
void toggleGatewayKeyVisible();
|
|
755
|
-
}}
|
|
756
|
-
onRotateConfirm={() => {
|
|
757
|
-
setGatewayRotateConfirm(true);
|
|
758
|
-
}}
|
|
759
|
-
onRotateCancel={() => {
|
|
760
|
-
setGatewayRotateConfirm(false);
|
|
761
|
-
}}
|
|
762
|
-
onRotate={() => {
|
|
763
|
-
void rotateGatewayKey();
|
|
764
|
-
}}
|
|
765
|
-
/>
|
|
766
|
-
) : null}
|
|
767
|
-
{activeTab === "about" ? <AboutTab t={t} /> : null}
|
|
768
|
-
</div>
|
|
769
|
-
</section>
|
|
770
|
-
);
|
|
771
|
-
}
|
|
1
|
+
/** Plugin-owned coding subscription account section inside the dsh Settings shell. */
|
|
2
|
+
|
|
3
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
4
|
+
import { cancelPreviewTicket, copyText, isConflictError, isConsumedPreviewError, jsonRequest } from "./api.ts";
|
|
5
|
+
import { AboutTab } from "./components/AboutTab.tsx";
|
|
6
|
+
import { AccountsTab } from "./components/AccountsTab.tsx";
|
|
7
|
+
import { CapabilitiesTab } from "./components/CapabilitiesTab.tsx";
|
|
8
|
+
import { GatewayTab } from "./components/GatewayTab.tsx";
|
|
9
|
+
import type { SettingsTabHint } from "./components/SettingsTabs.tsx";
|
|
10
|
+
import { SettingsTabs } from "./components/SettingsTabs.tsx";
|
|
11
|
+
import {
|
|
12
|
+
CAPABILITIES_PATH,
|
|
13
|
+
CODEX_USAGE_PATH,
|
|
14
|
+
GATEWAY_PATH,
|
|
15
|
+
GATEWAY_REVEAL_PATH,
|
|
16
|
+
GATEWAY_ROTATE_PATH,
|
|
17
|
+
IMAGINE_CREDENTIAL_PATH,
|
|
18
|
+
LOGIN_CANCEL_PATH,
|
|
19
|
+
LOGIN_CODE_PATH,
|
|
20
|
+
LOGIN_PATH,
|
|
21
|
+
LOGOUT_PATH,
|
|
22
|
+
MODELS_PATH,
|
|
23
|
+
POLL_INTERVAL_MS,
|
|
24
|
+
SOURCE_COMMIT_ACTION_KEY,
|
|
25
|
+
SOURCES_CANCEL_PATH,
|
|
26
|
+
SOURCES_COMMIT_PATH,
|
|
27
|
+
SOURCES_PATH,
|
|
28
|
+
SOURCES_PREVIEW_PATH,
|
|
29
|
+
STATUS_PATH,
|
|
30
|
+
} from "./constants.ts";
|
|
31
|
+
import { ensureMicroStyles } from "./microStyles.ts";
|
|
32
|
+
import {
|
|
33
|
+
emptyCapabilitySettings,
|
|
34
|
+
mergeSources,
|
|
35
|
+
parseCapabilities,
|
|
36
|
+
parseCommitAction,
|
|
37
|
+
parseGateway,
|
|
38
|
+
parseGatewayPort,
|
|
39
|
+
parseImagineCredential,
|
|
40
|
+
parsePreview,
|
|
41
|
+
parseSources,
|
|
42
|
+
parseUsage,
|
|
43
|
+
} from "./parsers.ts";
|
|
44
|
+
import { bodyStyle, buttonStyle, errorStyle, pageStyle, panelStyle, titleStyle } from "./styles.ts";
|
|
45
|
+
import type {
|
|
46
|
+
CapabilitySettingKey,
|
|
47
|
+
CapabilitySettingsView,
|
|
48
|
+
CapabilitySnapshot,
|
|
49
|
+
CodingOAuthStatus,
|
|
50
|
+
CopyField,
|
|
51
|
+
GatewayView,
|
|
52
|
+
GrokBuildSettingsProps,
|
|
53
|
+
ImagineCredentialView,
|
|
54
|
+
LoginChallenge,
|
|
55
|
+
LoginMethod,
|
|
56
|
+
ProviderSlug,
|
|
57
|
+
SettingsTabId,
|
|
58
|
+
SourceKind,
|
|
59
|
+
SourcePreview,
|
|
60
|
+
SourceStatus,
|
|
61
|
+
UsageView,
|
|
62
|
+
} from "./types.ts";
|
|
63
|
+
|
|
64
|
+
export type { GrokBuildSettingsInjected, GrokBuildSettingsProps } from "./types.ts";
|
|
65
|
+
|
|
66
|
+
const REMOTE_TIP_STORAGE_KEY = "dsh-coding-oauth-remote-tip-dismissed";
|
|
67
|
+
|
|
68
|
+
function readRemoteTipDismissed(): boolean {
|
|
69
|
+
try {
|
|
70
|
+
return globalThis.sessionStorage?.getItem(REMOTE_TIP_STORAGE_KEY) === "1";
|
|
71
|
+
} catch {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Multi-provider coding subscription status and OAuth actions. */
|
|
77
|
+
export function GrokBuildSettings({ t }: GrokBuildSettingsProps) {
|
|
78
|
+
if (t === undefined) throw new Error("Coding OAuth settings requires its translation function");
|
|
79
|
+
|
|
80
|
+
const [status, setStatus] = useState<CodingOAuthStatus | undefined>(undefined);
|
|
81
|
+
const [requestError, setRequestError] = useState<string | undefined>(undefined);
|
|
82
|
+
const [busyProvider, setBusyProvider] = useState<ProviderSlug | undefined>(undefined);
|
|
83
|
+
const [codeInputs, setCodeInputs] = useState<Partial<Record<ProviderSlug, string>>>({});
|
|
84
|
+
const [popupBlocked, setPopupBlocked] = useState<Partial<Record<ProviderSlug, boolean>>>({});
|
|
85
|
+
const [sources, setSources] = useState<SourceStatus[] | undefined>(undefined);
|
|
86
|
+
const [sourcesError, setSourcesError] = useState<string | undefined>(undefined);
|
|
87
|
+
const [sourcesBusy, setSourcesBusy] = useState(false);
|
|
88
|
+
const [preview, setPreview] = useState<SourcePreview | undefined>(undefined);
|
|
89
|
+
const previewRef = useRef<SourcePreview | undefined>(undefined);
|
|
90
|
+
const previewEpochRef = useRef(0);
|
|
91
|
+
const mountedRef = useRef(true);
|
|
92
|
+
const [confirmOverwrite, setConfirmOverwrite] = useState(false);
|
|
93
|
+
const [sourcesNotice, setSourcesNotice] = useState<string | undefined>(undefined);
|
|
94
|
+
const [capabilities, setCapabilities] = useState<CapabilitySnapshot | undefined>(undefined);
|
|
95
|
+
const [capabilitiesError, setCapabilitiesError] = useState<string | undefined>(undefined);
|
|
96
|
+
const [capabilitiesBusy, setCapabilitiesBusy] = useState(false);
|
|
97
|
+
const [capabilitiesLoaded, setCapabilitiesLoaded] = useState(false);
|
|
98
|
+
const [usage, setUsage] = useState<UsageView | undefined>(undefined);
|
|
99
|
+
const [usageError, setUsageError] = useState<string | undefined>(undefined);
|
|
100
|
+
const [usageLoading, setUsageLoading] = useState(false);
|
|
101
|
+
const [imagine, setImagine] = useState<ImagineCredentialView | undefined>(undefined);
|
|
102
|
+
const [imagineError, setImagineError] = useState<string | undefined>(undefined);
|
|
103
|
+
const [gateway, setGateway] = useState<GatewayView | undefined>(undefined);
|
|
104
|
+
const [gatewayError, setGatewayError] = useState<string | undefined>(undefined);
|
|
105
|
+
const [gatewayBusy, setGatewayBusy] = useState(false);
|
|
106
|
+
const [gatewayLoaded, setGatewayLoaded] = useState(false);
|
|
107
|
+
const [gatewayOnceKey, setGatewayOnceKey] = useState<string | undefined>(undefined);
|
|
108
|
+
const [gatewayKeyVisible, setGatewayKeyVisible] = useState(false);
|
|
109
|
+
const [gatewayRotateConfirm, setGatewayRotateConfirm] = useState(false);
|
|
110
|
+
const [gatewayRevealError, setGatewayRevealError] = useState<string | undefined>(undefined);
|
|
111
|
+
const [portDraft, setPortDraft] = useState("");
|
|
112
|
+
const [activeTab, setActiveTab] = useState<SettingsTabId>("accounts");
|
|
113
|
+
const [copiedField, setCopiedField] = useState<CopyField | undefined>(undefined);
|
|
114
|
+
const [copyFailedField, setCopyFailedField] = useState<CopyField | undefined>(undefined);
|
|
115
|
+
const [expandedProviders, setExpandedProviders] = useState<Partial<Record<ProviderSlug, boolean>>>({});
|
|
116
|
+
const [remoteTipDismissed, setRemoteTipDismissed] = useState(readRemoteTipDismissed);
|
|
117
|
+
const copiedTimerRef = useRef<number | undefined>(undefined);
|
|
118
|
+
const remote = status?.accessMode === "ssh-tunnel" || status?.accessMode === "trusted-https-proxy";
|
|
119
|
+
|
|
120
|
+
const refresh = useCallback(async () => {
|
|
121
|
+
try {
|
|
122
|
+
const next = await jsonRequest<CodingOAuthStatus>(STATUS_PATH);
|
|
123
|
+
setStatus(next);
|
|
124
|
+
setRequestError(undefined);
|
|
125
|
+
return next;
|
|
126
|
+
} catch (error: unknown) {
|
|
127
|
+
setRequestError(error instanceof Error ? error.message : t("requestFailed"));
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
}, [t]);
|
|
131
|
+
|
|
132
|
+
const refreshSources = useCallback(async () => {
|
|
133
|
+
try {
|
|
134
|
+
setSources(parseSources(await jsonRequest<unknown>(SOURCES_PATH)));
|
|
135
|
+
setSourcesError(undefined);
|
|
136
|
+
} catch (error: unknown) {
|
|
137
|
+
setSources(mergeSources([]));
|
|
138
|
+
setSourcesError(error instanceof Error ? error.message : t("sourcesLoadFailed"));
|
|
139
|
+
}
|
|
140
|
+
}, [t]);
|
|
141
|
+
|
|
142
|
+
const refreshCapabilities = useCallback(async () => {
|
|
143
|
+
try {
|
|
144
|
+
const parsed = parseCapabilities(await jsonRequest<unknown>(CAPABILITIES_PATH));
|
|
145
|
+
setCapabilities(parsed ?? { value: emptyCapabilitySettings(), revision: 0, writable: false });
|
|
146
|
+
setCapabilitiesError(undefined);
|
|
147
|
+
return parsed;
|
|
148
|
+
} catch (error: unknown) {
|
|
149
|
+
setCapabilities({ value: emptyCapabilitySettings(), revision: 0, writable: false });
|
|
150
|
+
setCapabilitiesError(error instanceof Error ? error.message : t("capabilitiesLoadFailed"));
|
|
151
|
+
return undefined;
|
|
152
|
+
} finally {
|
|
153
|
+
setCapabilitiesLoaded(true);
|
|
154
|
+
}
|
|
155
|
+
}, [t]);
|
|
156
|
+
|
|
157
|
+
const refreshGateway = useCallback(async () => {
|
|
158
|
+
try {
|
|
159
|
+
setGateway(parseGateway(await jsonRequest<unknown>(GATEWAY_PATH)));
|
|
160
|
+
setGatewayError(undefined);
|
|
161
|
+
} catch (error: unknown) {
|
|
162
|
+
setGatewayError(error instanceof Error ? error.message : t("gatewayLoadFailed"));
|
|
163
|
+
} finally {
|
|
164
|
+
setGatewayLoaded(true);
|
|
165
|
+
}
|
|
166
|
+
}, [t]);
|
|
167
|
+
|
|
168
|
+
const refreshImagine = useCallback(async () => {
|
|
169
|
+
try {
|
|
170
|
+
setImagine(parseImagineCredential(await jsonRequest<unknown>(IMAGINE_CREDENTIAL_PATH)));
|
|
171
|
+
setImagineError(undefined);
|
|
172
|
+
} catch (error: unknown) {
|
|
173
|
+
setImagineError(error instanceof Error ? error.message : t("imagineLoadFailed"));
|
|
174
|
+
}
|
|
175
|
+
}, [t]);
|
|
176
|
+
|
|
177
|
+
const refreshUsage = useCallback(async () => {
|
|
178
|
+
setUsageLoading(true);
|
|
179
|
+
try {
|
|
180
|
+
setUsage(parseUsage(await jsonRequest<unknown>(CODEX_USAGE_PATH)));
|
|
181
|
+
setUsageError(undefined);
|
|
182
|
+
} catch (error: unknown) {
|
|
183
|
+
setUsage(undefined);
|
|
184
|
+
setUsageError(error instanceof Error ? error.message : t("usageUnavailable"));
|
|
185
|
+
} finally {
|
|
186
|
+
setUsageLoading(false);
|
|
187
|
+
}
|
|
188
|
+
}, [t]);
|
|
189
|
+
|
|
190
|
+
// Accounts: status immediately; sources shortly after (non-blocking for first paint).
|
|
191
|
+
useEffect(() => {
|
|
192
|
+
ensureMicroStyles();
|
|
193
|
+
let active = true;
|
|
194
|
+
let timer: number | undefined;
|
|
195
|
+
void refresh().then((next) => {
|
|
196
|
+
if (!active || next?.uiOwner === "hub") return;
|
|
197
|
+
timer = window.setTimeout(() => void refreshSources(), 0);
|
|
198
|
+
});
|
|
199
|
+
return () => {
|
|
200
|
+
active = false;
|
|
201
|
+
if (timer !== undefined) window.clearTimeout(timer);
|
|
202
|
+
};
|
|
203
|
+
}, [refresh, refreshSources]);
|
|
204
|
+
|
|
205
|
+
// Capabilities tab: load settings + Imagine status on first visit.
|
|
206
|
+
useEffect(() => {
|
|
207
|
+
if (activeTab !== "capabilities") return;
|
|
208
|
+
if (!capabilitiesLoaded) void refreshCapabilities();
|
|
209
|
+
void refreshImagine();
|
|
210
|
+
}, [activeTab, capabilitiesLoaded, refreshCapabilities, refreshImagine]);
|
|
211
|
+
|
|
212
|
+
// Soft-fetch capabilities when Codex is signed in so Accounts can show quota without opening the tab.
|
|
213
|
+
useEffect(() => {
|
|
214
|
+
if (capabilitiesLoaded) return;
|
|
215
|
+
if (status?.providers.codex.status !== "signed-in") return;
|
|
216
|
+
void refreshCapabilities();
|
|
217
|
+
}, [capabilitiesLoaded, refreshCapabilities, status?.providers.codex.status]);
|
|
218
|
+
|
|
219
|
+
// Gateway: load only when that tab is opened.
|
|
220
|
+
useEffect(() => {
|
|
221
|
+
if (activeTab !== "gateway" || gatewayLoaded) return;
|
|
222
|
+
void refreshGateway();
|
|
223
|
+
}, [activeTab, gatewayLoaded, refreshGateway]);
|
|
224
|
+
|
|
225
|
+
useEffect(() => {
|
|
226
|
+
previewRef.current = preview;
|
|
227
|
+
}, [preview]);
|
|
228
|
+
|
|
229
|
+
useEffect(() => {
|
|
230
|
+
mountedRef.current = true;
|
|
231
|
+
return () => {
|
|
232
|
+
mountedRef.current = false;
|
|
233
|
+
if (copiedTimerRef.current !== undefined) window.clearTimeout(copiedTimerRef.current);
|
|
234
|
+
previewEpochRef.current += 1;
|
|
235
|
+
const active = previewRef.current;
|
|
236
|
+
if (active !== undefined) cancelPreviewTicket(active.previewId, true);
|
|
237
|
+
};
|
|
238
|
+
}, []);
|
|
239
|
+
|
|
240
|
+
useEffect(() => {
|
|
241
|
+
const signingIn =
|
|
242
|
+
status !== undefined && Object.values(status.providers).some((provider) => provider.status === "signing-in");
|
|
243
|
+
if (!signingIn) return;
|
|
244
|
+
const timer = window.setInterval(() => {
|
|
245
|
+
void refresh();
|
|
246
|
+
}, POLL_INTERVAL_MS);
|
|
247
|
+
return () => {
|
|
248
|
+
window.clearInterval(timer);
|
|
249
|
+
};
|
|
250
|
+
}, [refresh, status]);
|
|
251
|
+
|
|
252
|
+
useEffect(() => {
|
|
253
|
+
const signedIn = status?.providers.codex.status === "signed-in";
|
|
254
|
+
if (capabilities?.value.codexUsage === true && signedIn) {
|
|
255
|
+
void refreshUsage();
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
setUsage(undefined);
|
|
259
|
+
setUsageError(undefined);
|
|
260
|
+
setUsageLoading(false);
|
|
261
|
+
}, [capabilities?.value.codexUsage, refreshUsage, status?.providers.codex.status]);
|
|
262
|
+
|
|
263
|
+
useEffect(() => {
|
|
264
|
+
if (gateway !== undefined) setPortDraft(String(gateway.port));
|
|
265
|
+
}, [gateway]);
|
|
266
|
+
|
|
267
|
+
const signIn = async (provider: ProviderSlug, method: LoginMethod): Promise<void> => {
|
|
268
|
+
const popup = window.open("about:blank", "_blank");
|
|
269
|
+
if (popup !== null) popup.opener = null;
|
|
270
|
+
setBusyProvider(provider);
|
|
271
|
+
setRequestError(undefined);
|
|
272
|
+
setPopupBlocked((current) => ({ ...current, [provider]: popup === null }));
|
|
273
|
+
try {
|
|
274
|
+
const challenge = await jsonRequest<LoginChallenge>(LOGIN_PATH, "POST", { provider, method });
|
|
275
|
+
if (popup !== null) popup.location.replace(challenge.url);
|
|
276
|
+
await refresh();
|
|
277
|
+
} catch (error: unknown) {
|
|
278
|
+
popup?.close();
|
|
279
|
+
setRequestError(error instanceof Error ? error.message : t("requestFailed"));
|
|
280
|
+
await refresh();
|
|
281
|
+
} finally {
|
|
282
|
+
setBusyProvider(undefined);
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
const submitCode = async (provider: ProviderSlug): Promise<void> => {
|
|
287
|
+
const code = codeInputs[provider]?.trim() ?? "";
|
|
288
|
+
if (code.length === 0) return;
|
|
289
|
+
setBusyProvider(provider);
|
|
290
|
+
try {
|
|
291
|
+
await jsonRequest<{ ok: true }>(LOGIN_CODE_PATH, "POST", { provider, code });
|
|
292
|
+
setCodeInputs((current) => ({ ...current, [provider]: "" }));
|
|
293
|
+
await refresh();
|
|
294
|
+
} catch (error: unknown) {
|
|
295
|
+
setRequestError(error instanceof Error ? error.message : t("requestFailed"));
|
|
296
|
+
} finally {
|
|
297
|
+
setBusyProvider(undefined);
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
const cancelLogin = async (provider: ProviderSlug): Promise<void> => {
|
|
302
|
+
setBusyProvider(provider);
|
|
303
|
+
try {
|
|
304
|
+
setStatus(await jsonRequest<CodingOAuthStatus>(LOGIN_CANCEL_PATH, "POST", { provider }));
|
|
305
|
+
} catch (error: unknown) {
|
|
306
|
+
setRequestError(error instanceof Error ? error.message : t("requestFailed"));
|
|
307
|
+
} finally {
|
|
308
|
+
setBusyProvider(undefined);
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
const signOut = async (provider: ProviderSlug): Promise<void> => {
|
|
313
|
+
setBusyProvider(provider);
|
|
314
|
+
try {
|
|
315
|
+
setStatus(await jsonRequest<CodingOAuthStatus>(LOGOUT_PATH, "POST", { provider }));
|
|
316
|
+
} catch (error: unknown) {
|
|
317
|
+
setRequestError(error instanceof Error ? error.message : t("requestFailed"));
|
|
318
|
+
} finally {
|
|
319
|
+
setBusyProvider(undefined);
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
const saveModels = async (provider: ProviderSlug, selected: string[]): Promise<void> => {
|
|
324
|
+
setBusyProvider(provider);
|
|
325
|
+
try {
|
|
326
|
+
setStatus(await jsonRequest<CodingOAuthStatus>(MODELS_PATH, "POST", { provider, selected }));
|
|
327
|
+
} catch (error: unknown) {
|
|
328
|
+
setRequestError(error instanceof Error ? error.message : t("requestFailed"));
|
|
329
|
+
} finally {
|
|
330
|
+
setBusyProvider(undefined);
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
const previewSource = async (kind: SourceKind): Promise<void> => {
|
|
335
|
+
const epoch = ++previewEpochRef.current;
|
|
336
|
+
const previous = previewRef.current;
|
|
337
|
+
if (previous !== undefined) {
|
|
338
|
+
previewRef.current = undefined;
|
|
339
|
+
setPreview(undefined);
|
|
340
|
+
cancelPreviewTicket(previous.previewId);
|
|
341
|
+
}
|
|
342
|
+
setSourcesBusy(true);
|
|
343
|
+
setSourcesNotice(undefined);
|
|
344
|
+
setConfirmOverwrite(false);
|
|
345
|
+
try {
|
|
346
|
+
const next = parsePreview(await jsonRequest<unknown>(SOURCES_PREVIEW_PATH, "POST", { kind }));
|
|
347
|
+
if (next === undefined) throw new Error(t("sourcesPreviewFailed"));
|
|
348
|
+
if (!mountedRef.current || epoch !== previewEpochRef.current) {
|
|
349
|
+
cancelPreviewTicket(next.previewId, !mountedRef.current);
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
previewRef.current = next;
|
|
353
|
+
setPreview(next);
|
|
354
|
+
setSourcesError(undefined);
|
|
355
|
+
} catch (error: unknown) {
|
|
356
|
+
if (!mountedRef.current || epoch !== previewEpochRef.current) return;
|
|
357
|
+
setPreview(undefined);
|
|
358
|
+
setSourcesError(error instanceof Error ? error.message : t("sourcesPreviewFailed"));
|
|
359
|
+
} finally {
|
|
360
|
+
if (mountedRef.current && epoch === previewEpochRef.current) setSourcesBusy(false);
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
const commitSource = async (): Promise<void> => {
|
|
365
|
+
if (preview === undefined) return;
|
|
366
|
+
if (preview.action === "blocked") return;
|
|
367
|
+
if (preview.confirmOverwriteRequired && !confirmOverwrite) return;
|
|
368
|
+
setSourcesBusy(true);
|
|
369
|
+
try {
|
|
370
|
+
const result = await jsonRequest<unknown>(SOURCES_COMMIT_PATH, "POST", {
|
|
371
|
+
kind: preview.kind,
|
|
372
|
+
previewId: preview.previewId,
|
|
373
|
+
confirmOverwrite,
|
|
374
|
+
});
|
|
375
|
+
const action = parseCommitAction(result);
|
|
376
|
+
previewRef.current = undefined;
|
|
377
|
+
setPreview(undefined);
|
|
378
|
+
setConfirmOverwrite(false);
|
|
379
|
+
setSourcesNotice(
|
|
380
|
+
t("sourcesCommitSuccess", { action: action === undefined ? "" : t(SOURCE_COMMIT_ACTION_KEY[action]) }),
|
|
381
|
+
);
|
|
382
|
+
setSourcesError(undefined);
|
|
383
|
+
await refreshSources();
|
|
384
|
+
await refresh();
|
|
385
|
+
} catch (error: unknown) {
|
|
386
|
+
if (isConsumedPreviewError(error)) {
|
|
387
|
+
previewRef.current = undefined;
|
|
388
|
+
setPreview(undefined);
|
|
389
|
+
setConfirmOverwrite(false);
|
|
390
|
+
}
|
|
391
|
+
setSourcesError(error instanceof Error ? error.message : t("sourcesCommitFailed"));
|
|
392
|
+
await refreshSources();
|
|
393
|
+
} finally {
|
|
394
|
+
setSourcesBusy(false);
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
const cancelSourcePreview = async (): Promise<void> => {
|
|
399
|
+
if (preview === undefined) return;
|
|
400
|
+
const previewId = preview.previewId;
|
|
401
|
+
previewEpochRef.current += 1;
|
|
402
|
+
previewRef.current = undefined;
|
|
403
|
+
setPreview(undefined);
|
|
404
|
+
setConfirmOverwrite(false);
|
|
405
|
+
setSourcesBusy(true);
|
|
406
|
+
try {
|
|
407
|
+
await jsonRequest<unknown>(SOURCES_CANCEL_PATH, "POST", { previewId });
|
|
408
|
+
setSourcesError(undefined);
|
|
409
|
+
} catch (error: unknown) {
|
|
410
|
+
setSourcesError(error instanceof Error ? error.message : t("requestFailed"));
|
|
411
|
+
} finally {
|
|
412
|
+
setSourcesBusy(false);
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
const patchCapability = async (key: CapabilitySettingKey, value: boolean | number): Promise<boolean> => {
|
|
417
|
+
if (capabilities === undefined || !capabilities.writable) return false;
|
|
418
|
+
if (key === "codexImageEdits" && value === true && !capabilities.value.codexImages) return false;
|
|
419
|
+
if (key === "codexImagesAnyModel" && value === true && !capabilities.value.codexImages) return false;
|
|
420
|
+
const patch: Partial<CapabilitySettingsView> =
|
|
421
|
+
key === "codexImages" && value === false
|
|
422
|
+
? { codexImages: false, codexImageEdits: false, codexImagesAnyModel: false }
|
|
423
|
+
: ({ [key]: value } as Partial<CapabilitySettingsView>);
|
|
424
|
+
setCapabilitiesBusy(true);
|
|
425
|
+
try {
|
|
426
|
+
const updated = parseCapabilities(
|
|
427
|
+
await jsonRequest<unknown>(CAPABILITIES_PATH, "PATCH", {
|
|
428
|
+
expectedRevision: capabilities.revision,
|
|
429
|
+
patch,
|
|
430
|
+
}),
|
|
431
|
+
);
|
|
432
|
+
if (updated !== undefined) setCapabilities(updated);
|
|
433
|
+
else await refreshCapabilities();
|
|
434
|
+
setCapabilitiesError(undefined);
|
|
435
|
+
return true;
|
|
436
|
+
} catch (error: unknown) {
|
|
437
|
+
await refreshCapabilities();
|
|
438
|
+
setCapabilitiesError(
|
|
439
|
+
isConflictError(error)
|
|
440
|
+
? t("capabilitiesConflictRefreshed")
|
|
441
|
+
: error instanceof Error
|
|
442
|
+
? error.message
|
|
443
|
+
: t("capabilitiesSaveFailed"),
|
|
444
|
+
);
|
|
445
|
+
return false;
|
|
446
|
+
} finally {
|
|
447
|
+
setCapabilitiesBusy(false);
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
const showUsage = capabilities?.value.codexUsage === true && status?.providers.codex.status === "signed-in";
|
|
452
|
+
const signedInCount =
|
|
453
|
+
status === undefined
|
|
454
|
+
? 0
|
|
455
|
+
: Object.values(status.providers).filter((provider) => provider.status === "signed-in").length;
|
|
456
|
+
|
|
457
|
+
const tabHints = useMemo((): readonly SettingsTabHint[] => {
|
|
458
|
+
const hints: SettingsTabHint[] = [];
|
|
459
|
+
if (status !== undefined) {
|
|
460
|
+
const signedInCount = Object.values(status.providers).filter(
|
|
461
|
+
(provider) => provider.status === "signed-in",
|
|
462
|
+
).length;
|
|
463
|
+
if (signedInCount > 0) {
|
|
464
|
+
hints.push({ id: "accounts", suffix: String(signedInCount) });
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
if (gateway?.running === true) {
|
|
468
|
+
hints.push({ id: "gateway", suffix: t("tabGatewayActive") });
|
|
469
|
+
}
|
|
470
|
+
return hints;
|
|
471
|
+
}, [gateway?.running, status, t]);
|
|
472
|
+
|
|
473
|
+
const markCopied = (field: CopyField): void => {
|
|
474
|
+
if (copiedTimerRef.current !== undefined) window.clearTimeout(copiedTimerRef.current);
|
|
475
|
+
setCopiedField(field);
|
|
476
|
+
setCopyFailedField(undefined);
|
|
477
|
+
copiedTimerRef.current = window.setTimeout(() => {
|
|
478
|
+
if (mountedRef.current) {
|
|
479
|
+
setCopiedField(undefined);
|
|
480
|
+
setCopyFailedField(undefined);
|
|
481
|
+
}
|
|
482
|
+
copiedTimerRef.current = undefined;
|
|
483
|
+
}, 2000);
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
const handleCopy = async (field: CopyField, text: string): Promise<void> => {
|
|
487
|
+
const ok = await copyText(text);
|
|
488
|
+
if (ok) {
|
|
489
|
+
markCopied(field);
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
setCopiedField(undefined);
|
|
493
|
+
setCopyFailedField(field);
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
const ensureGatewayKey = async (): Promise<string> => {
|
|
497
|
+
if (gatewayOnceKey !== undefined) return gatewayOnceKey;
|
|
498
|
+
const value = await jsonRequest<{ apiKey?: string }>(GATEWAY_REVEAL_PATH, "POST");
|
|
499
|
+
if (typeof value.apiKey !== "string" || value.apiKey.length === 0) {
|
|
500
|
+
throw new Error(t("gatewayRevealFailed"));
|
|
501
|
+
}
|
|
502
|
+
setGatewayOnceKey(value.apiKey);
|
|
503
|
+
return value.apiKey;
|
|
504
|
+
};
|
|
505
|
+
|
|
506
|
+
const copyGatewayKey = async (): Promise<void> => {
|
|
507
|
+
try {
|
|
508
|
+
const key = await ensureGatewayKey();
|
|
509
|
+
await handleCopy("key", key);
|
|
510
|
+
setGatewayRevealError(undefined);
|
|
511
|
+
} catch {
|
|
512
|
+
setGatewayRevealError(t("gatewayRevealFailed"));
|
|
513
|
+
}
|
|
514
|
+
};
|
|
515
|
+
|
|
516
|
+
const toggleGatewayKeyVisible = async (): Promise<void> => {
|
|
517
|
+
if (gatewayKeyVisible) {
|
|
518
|
+
setGatewayKeyVisible(false);
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
try {
|
|
522
|
+
await ensureGatewayKey();
|
|
523
|
+
setGatewayKeyVisible(true);
|
|
524
|
+
setGatewayRevealError(undefined);
|
|
525
|
+
} catch {
|
|
526
|
+
setGatewayRevealError(t("gatewayRevealFailed"));
|
|
527
|
+
}
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
const applyGatewayPort = async (): Promise<void> => {
|
|
531
|
+
const port = parseGatewayPort(portDraft);
|
|
532
|
+
if (port === undefined) {
|
|
533
|
+
setGatewayError(t("gatewayPortInvalid"));
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
if (gateway !== undefined && port === gateway.port) return;
|
|
537
|
+
setGatewayBusy(true);
|
|
538
|
+
try {
|
|
539
|
+
setGateway(parseGateway(await jsonRequest<unknown>(GATEWAY_PATH, "PATCH", { port })) ?? gateway);
|
|
540
|
+
setGatewayError(undefined);
|
|
541
|
+
} catch (error: unknown) {
|
|
542
|
+
setGatewayError(error instanceof Error ? error.message : t("gatewaySaveFailed"));
|
|
543
|
+
if (gateway !== undefined) setPortDraft(String(gateway.port));
|
|
544
|
+
} finally {
|
|
545
|
+
setGatewayBusy(false);
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
const rotateGatewayKey = async (): Promise<void> => {
|
|
550
|
+
setGatewayBusy(true);
|
|
551
|
+
try {
|
|
552
|
+
const value = await jsonRequest<{ apiKey?: string; keyHint?: string }>(GATEWAY_ROTATE_PATH, "POST");
|
|
553
|
+
if (typeof value.apiKey === "string") setGatewayOnceKey(value.apiKey);
|
|
554
|
+
setGatewayKeyVisible(true);
|
|
555
|
+
setGatewayRotateConfirm(false);
|
|
556
|
+
setGatewayRevealError(undefined);
|
|
557
|
+
await refreshGateway();
|
|
558
|
+
} catch (error: unknown) {
|
|
559
|
+
setGatewayError(error instanceof Error ? error.message : t("gatewaySaveFailed"));
|
|
560
|
+
} finally {
|
|
561
|
+
setGatewayBusy(false);
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
|
|
565
|
+
const setGatewayEnabled = (enabled: boolean): void => {
|
|
566
|
+
setGatewayBusy(true);
|
|
567
|
+
void jsonRequest<unknown>(GATEWAY_PATH, "PATCH", { enabled })
|
|
568
|
+
.then((value) => {
|
|
569
|
+
setGateway(parseGateway(value) ?? gateway);
|
|
570
|
+
setGatewayError(undefined);
|
|
571
|
+
})
|
|
572
|
+
.catch((error: unknown) => {
|
|
573
|
+
setGatewayError(error instanceof Error ? error.message : t("gatewaySaveFailed"));
|
|
574
|
+
})
|
|
575
|
+
.finally(() => {
|
|
576
|
+
setGatewayBusy(false);
|
|
577
|
+
});
|
|
578
|
+
};
|
|
579
|
+
|
|
580
|
+
const dismissRemoteTip = (): void => {
|
|
581
|
+
setRemoteTipDismissed(true);
|
|
582
|
+
try {
|
|
583
|
+
globalThis.sessionStorage?.setItem(REMOTE_TIP_STORAGE_KEY, "1");
|
|
584
|
+
} catch {
|
|
585
|
+
// ignore storage failures
|
|
586
|
+
}
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
const openAccountsForCodex = (): void => {
|
|
590
|
+
setActiveTab("accounts");
|
|
591
|
+
window.setTimeout(() => document.getElementById("coding-oauth-login-codex")?.focus(), 0);
|
|
592
|
+
};
|
|
593
|
+
|
|
594
|
+
const focusCapabilityDependency = (target: "codexImages" | "imagineCredential"): void => {
|
|
595
|
+
const id = target === "codexImages" ? "cap-switch-codexImages" : "coding-oauth-imagine-credential";
|
|
596
|
+
document.getElementById(id)?.focus();
|
|
597
|
+
};
|
|
598
|
+
|
|
599
|
+
if (status?.uiOwner === "hub") {
|
|
600
|
+
return (
|
|
601
|
+
<section data-dsh-coding-oauth="compact" style={pageStyle} aria-labelledby="coding-oauth-settings-title">
|
|
602
|
+
<h2 id="coding-oauth-settings-title" style={titleStyle}>
|
|
603
|
+
{t("coinstallTitle")}
|
|
604
|
+
</h2>
|
|
605
|
+
<p style={bodyStyle}>{t("coinstallSummary", { count: signedInCount })}</p>
|
|
606
|
+
<button
|
|
607
|
+
type="button"
|
|
608
|
+
style={buttonStyle}
|
|
609
|
+
onClick={() => window.dispatchEvent(new CustomEvent("usage-stats:open-dashboard"))}
|
|
610
|
+
>
|
|
611
|
+
{t("coinstallOpenHub")}
|
|
612
|
+
</button>
|
|
613
|
+
</section>
|
|
614
|
+
);
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
return (
|
|
618
|
+
<section data-dsh-coding-oauth="" style={pageStyle} aria-labelledby="coding-oauth-settings-title">
|
|
619
|
+
<div>
|
|
620
|
+
<h2 id="coding-oauth-settings-title" style={titleStyle}>
|
|
621
|
+
{t("title")}
|
|
622
|
+
</h2>
|
|
623
|
+
{activeTab === "accounts" ? <p style={{ ...bodyStyle, marginTop: 6 }}>{t("intro")}</p> : null}
|
|
624
|
+
</div>
|
|
625
|
+
{requestError === undefined ? null : (
|
|
626
|
+
<div role="alert" style={{ display: "flex", flexWrap: "wrap", alignItems: "center", gap: 8 }}>
|
|
627
|
+
<p style={errorStyle}>{requestError}</p>
|
|
628
|
+
{status === undefined ? (
|
|
629
|
+
<button type="button" style={buttonStyle} onClick={() => void refresh()}>
|
|
630
|
+
{t("retry")}
|
|
631
|
+
</button>
|
|
632
|
+
) : null}
|
|
633
|
+
</div>
|
|
634
|
+
)}
|
|
635
|
+
<SettingsTabs t={t} activeTab={activeTab} onChange={setActiveTab} hints={tabHints} />
|
|
636
|
+
<div
|
|
637
|
+
id={`coding-oauth-panel-${activeTab}`}
|
|
638
|
+
role="tabpanel"
|
|
639
|
+
aria-labelledby={`coding-oauth-tab-${activeTab}`}
|
|
640
|
+
style={panelStyle}
|
|
641
|
+
>
|
|
642
|
+
{activeTab === "accounts" && !(status === undefined && requestError !== undefined) ? (
|
|
643
|
+
<AccountsTab
|
|
644
|
+
t={t}
|
|
645
|
+
status={status}
|
|
646
|
+
remote={remote}
|
|
647
|
+
remoteTipDismissed={remoteTipDismissed}
|
|
648
|
+
onDismissRemoteTip={dismissRemoteTip}
|
|
649
|
+
sources={sources}
|
|
650
|
+
sourcesError={sourcesError}
|
|
651
|
+
sourcesNotice={sourcesNotice}
|
|
652
|
+
sourcesBusy={sourcesBusy}
|
|
653
|
+
preview={preview}
|
|
654
|
+
confirmOverwrite={confirmOverwrite}
|
|
655
|
+
busyProvider={busyProvider}
|
|
656
|
+
codeInputs={codeInputs}
|
|
657
|
+
popupBlocked={popupBlocked}
|
|
658
|
+
expandedProviders={expandedProviders}
|
|
659
|
+
showUsage={showUsage === true}
|
|
660
|
+
usage={usage}
|
|
661
|
+
usageError={usageError}
|
|
662
|
+
usageLoading={usageLoading}
|
|
663
|
+
onSignIn={(slug, method) => {
|
|
664
|
+
void signIn(slug, method);
|
|
665
|
+
}}
|
|
666
|
+
onSignOut={(slug) => {
|
|
667
|
+
void signOut(slug);
|
|
668
|
+
}}
|
|
669
|
+
onCancelLogin={(slug) => {
|
|
670
|
+
void cancelLogin(slug);
|
|
671
|
+
}}
|
|
672
|
+
onSubmitCode={(slug) => {
|
|
673
|
+
void submitCode(slug);
|
|
674
|
+
}}
|
|
675
|
+
onCodeChange={(slug, value) => {
|
|
676
|
+
setCodeInputs((current) => ({ ...current, [slug]: value }));
|
|
677
|
+
}}
|
|
678
|
+
onToggleExpanded={(slug) => {
|
|
679
|
+
setExpandedProviders((current) => ({
|
|
680
|
+
...current,
|
|
681
|
+
[slug]: !(current[slug] === true),
|
|
682
|
+
}));
|
|
683
|
+
}}
|
|
684
|
+
onPreviewSource={(slug) => {
|
|
685
|
+
void previewSource(slug);
|
|
686
|
+
}}
|
|
687
|
+
onSaveModels={(slug, selected) => {
|
|
688
|
+
void saveModels(slug, selected);
|
|
689
|
+
}}
|
|
690
|
+
onConfirmOverwriteChange={setConfirmOverwrite}
|
|
691
|
+
onCommitSource={() => {
|
|
692
|
+
void commitSource();
|
|
693
|
+
}}
|
|
694
|
+
onCancelSourcePreview={() => {
|
|
695
|
+
void cancelSourcePreview();
|
|
696
|
+
}}
|
|
697
|
+
onRefreshSources={() => {
|
|
698
|
+
void refreshSources();
|
|
699
|
+
}}
|
|
700
|
+
onDismissSourcesNotice={() => {
|
|
701
|
+
setSourcesNotice(undefined);
|
|
702
|
+
}}
|
|
703
|
+
/>
|
|
704
|
+
) : null}
|
|
705
|
+
{activeTab === "capabilities" ? (
|
|
706
|
+
<CapabilitiesTab
|
|
707
|
+
t={t}
|
|
708
|
+
capabilities={capabilities}
|
|
709
|
+
capabilitiesError={capabilitiesError}
|
|
710
|
+
capabilitiesBusy={capabilitiesBusy}
|
|
711
|
+
imagine={imagine}
|
|
712
|
+
imagineError={imagineError}
|
|
713
|
+
codexSignedIn={status?.providers.codex.status === "signed-in"}
|
|
714
|
+
onRetry={() => {
|
|
715
|
+
void refreshCapabilities();
|
|
716
|
+
void refreshImagine();
|
|
717
|
+
}}
|
|
718
|
+
onOpenAccounts={openAccountsForCodex}
|
|
719
|
+
onFocusDependency={focusCapabilityDependency}
|
|
720
|
+
onPatchCapability={(key, value) => patchCapability(key, value)}
|
|
721
|
+
/>
|
|
722
|
+
) : null}
|
|
723
|
+
{activeTab === "gateway" ? (
|
|
724
|
+
<GatewayTab
|
|
725
|
+
t={t}
|
|
726
|
+
gateway={gateway}
|
|
727
|
+
gatewayError={gatewayError}
|
|
728
|
+
gatewayBusy={gatewayBusy}
|
|
729
|
+
gatewayOnceKey={gatewayOnceKey}
|
|
730
|
+
gatewayKeyVisible={gatewayKeyVisible}
|
|
731
|
+
gatewayRotateConfirm={gatewayRotateConfirm}
|
|
732
|
+
gatewayRevealError={gatewayRevealError}
|
|
733
|
+
portDraft={portDraft}
|
|
734
|
+
copiedField={copiedField}
|
|
735
|
+
copyFailedField={copyFailedField}
|
|
736
|
+
onRetry={() => {
|
|
737
|
+
void refreshGateway();
|
|
738
|
+
}}
|
|
739
|
+
onEnabledChange={setGatewayEnabled}
|
|
740
|
+
onPortDraftChange={setPortDraft}
|
|
741
|
+
onApplyPort={() => {
|
|
742
|
+
void applyGatewayPort();
|
|
743
|
+
}}
|
|
744
|
+
onRandomPort={(port) => {
|
|
745
|
+
setPortDraft(String(port));
|
|
746
|
+
}}
|
|
747
|
+
onCopy={(field, text) => {
|
|
748
|
+
void handleCopy(field, text);
|
|
749
|
+
}}
|
|
750
|
+
onCopyKey={() => {
|
|
751
|
+
void copyGatewayKey();
|
|
752
|
+
}}
|
|
753
|
+
onToggleKeyVisible={() => {
|
|
754
|
+
void toggleGatewayKeyVisible();
|
|
755
|
+
}}
|
|
756
|
+
onRotateConfirm={() => {
|
|
757
|
+
setGatewayRotateConfirm(true);
|
|
758
|
+
}}
|
|
759
|
+
onRotateCancel={() => {
|
|
760
|
+
setGatewayRotateConfirm(false);
|
|
761
|
+
}}
|
|
762
|
+
onRotate={() => {
|
|
763
|
+
void rotateGatewayKey();
|
|
764
|
+
}}
|
|
765
|
+
/>
|
|
766
|
+
) : null}
|
|
767
|
+
{activeTab === "about" ? <AboutTab t={t} /> : null}
|
|
768
|
+
</div>
|
|
769
|
+
</section>
|
|
770
|
+
);
|
|
771
|
+
}
|