mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 16:04:01 +00:00
feat(go): add browser device OAuth subscription sign-in
Add WebUI device authorization, organization selection, persisted token refresh, and fail-closed official Go subscription routing. Preserve manual keys without PAYG fallback and keep OAuth quota unknown. Validation: offline regression checks, go vet, JavaScript syntax, and Windows/Linux amd64 compilation passed. Live subscription authorization and inference are not yet verified. Refs #168
This commit is contained in:
1 parent
95575c5fd6
commit
159d7fdf0f
23 files changed
+2187
-84
No files matched your search
@@ -647,6 +647,22 @@
|
||||
<button class="tab" data-tab="free" role="tab" aria-selected="false" data-i18n="quotas.tab.free">免费模型</button>
|
||||
</div>
|
||||
<div data-panel="quota" class="stack" role="tabpanel">
|
||||
<section class="section">
|
||||
<div class="section-head">
|
||||
<div>
|
||||
<h2 class="section-title" data-i18n="goAuth.title">Go 订阅登录</h2>
|
||||
<p class="section-caption" data-i18n="goAuth.caption">通过 OpenCode 官方页面授权,无需复制凭证。手动 Go Key 仍保留,订阅连接期间不作备用通道,断开后恢复原有路由。</p>
|
||||
</div>
|
||||
<button id="go-auth-refresh" type="button" class="btn btn-sm" data-i18n="goAuth.refresh">刷新登录状态</button>
|
||||
</div>
|
||||
<div id="go-auth-content" class="stack" role="status" aria-live="polite"></div>
|
||||
<div class="key-actions">
|
||||
<button id="go-auth-start" type="button" class="btn btn-sm btn-primary" data-i18n="goAuth.start">浏览器登录 Go 订阅</button>
|
||||
<button id="go-auth-cancel" type="button" class="btn btn-sm hidden" data-i18n="goAuth.cancel">取消本次登录</button>
|
||||
<button id="go-auth-disconnect" type="button" class="btn btn-sm btn-danger hidden" data-i18n="goAuth.disconnect">断开 Go 订阅登录</button>
|
||||
</div>
|
||||
<p class="section-caption" data-i18n="goAuth.notice">只接入已确认的 Go 订阅路由,不自动改用余额通道。OAuth 订阅额度查询不可用时显示未知,不推算剩余额度。</p>
|
||||
</section>
|
||||
<section class="section">
|
||||
<div class="section-head">
|
||||
<div>
|
||||
|
||||
@@ -0,0 +1,290 @@
|
||||
import { api, hasSession } from "./api.js";
|
||||
import * as store from "./store.js";
|
||||
import { t, onChange } from "./i18n.js";
|
||||
import { $, confirmDialog, el, localTime } from "./ui.js";
|
||||
|
||||
let state = null;
|
||||
let timer = null;
|
||||
let active = false;
|
||||
let busy = false;
|
||||
let generation = 0;
|
||||
let controller = null;
|
||||
let failure = "";
|
||||
let connectionChanged = null;
|
||||
let synchronizationPending = false;
|
||||
|
||||
function stopPolling() {
|
||||
clearTimeout(timer);
|
||||
timer = null;
|
||||
}
|
||||
|
||||
function verificationURL(value) {
|
||||
try {
|
||||
const parsed = new URL(value, "https://opencode.ai/console/");
|
||||
if (
|
||||
parsed.protocol !== "https:" ||
|
||||
parsed.host !== "opencode.ai" ||
|
||||
parsed.username ||
|
||||
parsed.password
|
||||
)
|
||||
return null;
|
||||
return parsed.href;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function pendingExpired() {
|
||||
const expires = Date.parse(state?.pending?.expires_at);
|
||||
return Number.isFinite(expires) && expires <= Date.now();
|
||||
}
|
||||
|
||||
function connectionID(value) {
|
||||
return value?.connected
|
||||
? JSON.stringify([
|
||||
value.account?.email,
|
||||
value.account?.organization,
|
||||
value.account?.models,
|
||||
!!value.account?.needs_login,
|
||||
])
|
||||
: "";
|
||||
}
|
||||
|
||||
function current(operation) {
|
||||
return operation === generation && hasSession();
|
||||
}
|
||||
|
||||
async function synchronize(operation) {
|
||||
const [config, debug] = await Promise.all([api("/api/config"), api("/api/debug/models")]);
|
||||
if (!current(operation)) return;
|
||||
store.set("config", config);
|
||||
if (debug.last_inference) store.set("lastPlayground", debug.last_inference);
|
||||
store.set("debug", debug);
|
||||
await connectionChanged?.();
|
||||
if (current(operation)) synchronizationPending = false;
|
||||
}
|
||||
|
||||
function schedulePoll() {
|
||||
stopPolling();
|
||||
const pending = state?.pending;
|
||||
if (
|
||||
!active ||
|
||||
document.hidden ||
|
||||
busy ||
|
||||
failure ||
|
||||
!hasSession() ||
|
||||
!pending ||
|
||||
pendingExpired() ||
|
||||
pending.status === "failed" ||
|
||||
pending.status === "choosing_org"
|
||||
)
|
||||
return;
|
||||
const seconds = Number(pending.retry_after_seconds ?? pending.interval_seconds ?? 5);
|
||||
const wait = Number.isFinite(seconds) ? Math.max(1, Math.min(seconds, 86400)) : 5;
|
||||
timer = setTimeout(() => {
|
||||
if (pendingExpired() || state?.pending?.status === "starting") refresh();
|
||||
else poll();
|
||||
}, wait * 1000);
|
||||
}
|
||||
|
||||
async function request(path, options = {}, action = "refresh") {
|
||||
if (busy || !hasSession()) return;
|
||||
stopPolling();
|
||||
const operation = generation;
|
||||
const previous = connectionID(state);
|
||||
busy = true;
|
||||
failure = "";
|
||||
controller = new AbortController();
|
||||
render();
|
||||
try {
|
||||
const next = await api(path, { ...options, signal: controller.signal });
|
||||
if (!current(operation)) return;
|
||||
state = next;
|
||||
render();
|
||||
if (
|
||||
action === "disconnect" ||
|
||||
previous !== connectionID(next) ||
|
||||
(action === "poll" && next.connected && !next.pending)
|
||||
)
|
||||
synchronizationPending = true;
|
||||
if (synchronizationPending) await synchronize(operation);
|
||||
} catch (error) {
|
||||
if (current(operation) && error.name !== "AbortError") failure = error.message;
|
||||
} finally {
|
||||
if (current(operation)) {
|
||||
busy = false;
|
||||
controller = null;
|
||||
render();
|
||||
schedulePoll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function poll(organizationID) {
|
||||
if (!state?.pending?.id) return;
|
||||
const body = { id: state.pending.id };
|
||||
if (organizationID) body.organization_id = organizationID;
|
||||
return request("/api/go/auth/poll", { method: "POST", body: JSON.stringify(body) }, "poll");
|
||||
}
|
||||
|
||||
async function disconnect() {
|
||||
const operation = generation;
|
||||
const confirmed = await confirmDialog({
|
||||
title: t("断开 Go 订阅登录", "goAuth.disconnect"),
|
||||
message: t(
|
||||
"仅移除网关保存的订阅登录,不删除手动 Go Key,也不会撤销 OpenCode 官方会话。是否继续?",
|
||||
"goAuth.disconnectConfirm",
|
||||
),
|
||||
confirmLabel: t("断开连接", "goAuth.disconnectAction"),
|
||||
danger: true,
|
||||
});
|
||||
if (!confirmed || !current(operation)) return;
|
||||
await request("/api/go/connection", { method: "DELETE", body: "{}" }, "disconnect");
|
||||
}
|
||||
|
||||
function render() {
|
||||
const pending = state?.pending;
|
||||
const expired = pendingExpired();
|
||||
const selected = $("#go-auth-organization")?.value;
|
||||
const host = $("#go-auth-content");
|
||||
host.replaceChildren();
|
||||
$("#go-auth-start").textContent = state?.connected
|
||||
? t("重新登录 Go 订阅", "goAuth.reconnect")
|
||||
: t("浏览器登录 Go 订阅", "goAuth.start");
|
||||
$("#go-auth-start").disabled = busy || !!(pending && pending.status !== "failed" && !expired);
|
||||
$("#go-auth-refresh").disabled = busy;
|
||||
$("#go-auth-cancel").classList.toggle("hidden", !pending);
|
||||
$("#go-auth-cancel").disabled = busy;
|
||||
$("#go-auth-disconnect").classList.toggle("hidden", !state?.connected);
|
||||
$("#go-auth-disconnect").disabled = busy;
|
||||
if (state?.connected) {
|
||||
const account = state.account || {};
|
||||
host.append(
|
||||
el("p", {
|
||||
class: account.needs_login ? "warn" : "ok",
|
||||
text: account.needs_login
|
||||
? t("Go 订阅登录已失效,请重新登录。手动 Go Key 不会作为备用通道。", "goAuth.needsLogin")
|
||||
: t("Go 订阅已连接,登录凭证仅保存在网关服务端。", "goAuth.connected"),
|
||||
}),
|
||||
);
|
||||
if (account.error) host.append(el("p", { class: "warn", text: account.error }));
|
||||
if (account.email) host.append(el("p", { class: "mono", text: account.email }));
|
||||
if (account.organization)
|
||||
host.append(el("p", { text: t("组织:{0}", "goAuth.organizationValue", account.organization) }));
|
||||
if (account.models != null)
|
||||
host.append(el("p", { text: t("已获取 {0} 个订阅模型。", "goAuth.models", account.models) }));
|
||||
if (account.expires_at && !account.needs_login)
|
||||
host.append(
|
||||
el("p", {
|
||||
class: "section-caption",
|
||||
text: t("访问凭证到期:{0};网关按需自动刷新。", "goAuth.expires", localTime(account.expires_at)),
|
||||
}),
|
||||
);
|
||||
} else if (!pending) {
|
||||
host.append(el("p", {
|
||||
class: "section-caption",
|
||||
text: t("尚未连接 Go 订阅。手动配置的 Go Key 不受此登录影响。", "goAuth.disconnected"),
|
||||
}));
|
||||
}
|
||||
if (pending) {
|
||||
if (expired) {
|
||||
host.append(el("p", { class: "warn", text: t("本次登录已过期,请重新开始。", "goAuth.expired") }));
|
||||
} else if (pending.status === "failed") {
|
||||
host.append(el("p", {
|
||||
class: "warn",
|
||||
text: pending.error || t("订阅登录未完成,请重新开始。", "goAuth.failed"),
|
||||
}));
|
||||
} else if (pending.status === "choosing_org") {
|
||||
const select = el("select", { id: "go-auth-organization" });
|
||||
select.append(el("option", { value: "", text: t("请选择订阅所在组织", "goAuth.chooseOrganization") }));
|
||||
for (const organization of pending.organizations || [])
|
||||
select.append(el("option", { value: organization.id, text: organization.name || organization.id }));
|
||||
if ([...select.options].some((option) => option.value === selected)) select.value = selected;
|
||||
const confirm = el("button", {
|
||||
type: "button",
|
||||
class: "btn btn-sm btn-primary",
|
||||
text: t("连接所选组织", "goAuth.confirmOrganization"),
|
||||
disabled: busy || !select.value,
|
||||
});
|
||||
select.disabled = busy;
|
||||
select.addEventListener("change", () => (confirm.disabled = !select.value));
|
||||
confirm.addEventListener("click", () => poll(select.value));
|
||||
host.append(el("div", { class: "field" }, [
|
||||
el("label", { for: "go-auth-organization", text: t("订阅所在组织", "goAuth.organization") }),
|
||||
select,
|
||||
]), confirm);
|
||||
} else if (pending.status === "starting") {
|
||||
host.append(el("p", { text: t("正在申请官方登录验证码…", "goAuth.starting") }));
|
||||
} else {
|
||||
host.append(el("p", { text: t("打开官方页面完成登录,并确认这是您发起的授权。", "goAuth.instructions") }));
|
||||
if (pending.user_code)
|
||||
host.append(el("p", {}, [
|
||||
el("span", { text: t("验证码:", "goAuth.userCode") }),
|
||||
el("code", { class: "mono", text: pending.user_code }),
|
||||
]));
|
||||
const url = pending.verification_url && verificationURL(pending.verification_url);
|
||||
if (url)
|
||||
host.append(el("a", {
|
||||
class: "btn btn-sm btn-primary",
|
||||
href: url,
|
||||
target: "_blank",
|
||||
rel: "noopener noreferrer",
|
||||
text: t("打开 OpenCode 官方登录页", "goAuth.openVerification"),
|
||||
}));
|
||||
else
|
||||
host.append(el("p", { class: "warn", text: t("官方验证链接无效,请取消后重试。", "goAuth.invalidURL") }));
|
||||
if (pending.expires_at)
|
||||
host.append(el("p", {
|
||||
class: "section-caption",
|
||||
text: t("验证码有效至 {0};授权后将自动检查结果。", "goAuth.pendingExpires", localTime(pending.expires_at)),
|
||||
}));
|
||||
}
|
||||
}
|
||||
if (failure)
|
||||
host.append(el("p", { class: "warn", role: "alert", text: t("操作失败:{0}", "goAuth.error", failure) }));
|
||||
}
|
||||
|
||||
export function init(onConnectionChanged) {
|
||||
connectionChanged = onConnectionChanged;
|
||||
$("#go-auth-start").addEventListener("click", () =>
|
||||
request("/api/go/auth/start", { method: "POST", body: "{}" }, "start"),
|
||||
);
|
||||
$("#go-auth-refresh").addEventListener("click", refresh);
|
||||
$("#go-auth-cancel").addEventListener("click", () =>
|
||||
request("/api/go/auth", { method: "DELETE", body: "{}" }, "cancel"),
|
||||
);
|
||||
$("#go-auth-disconnect").addEventListener("click", disconnect);
|
||||
document.addEventListener("visibilitychange", () => {
|
||||
if (document.hidden) stopPolling();
|
||||
else if (active) refresh();
|
||||
});
|
||||
onChange(render);
|
||||
render();
|
||||
}
|
||||
|
||||
export function refresh() {
|
||||
return request("/api/go/auth");
|
||||
}
|
||||
|
||||
export function show() {
|
||||
active = true;
|
||||
if (!document.hidden) refresh();
|
||||
}
|
||||
|
||||
export function hide() {
|
||||
active = false;
|
||||
stopPolling();
|
||||
}
|
||||
|
||||
export function reset() {
|
||||
generation++;
|
||||
active = false;
|
||||
stopPolling();
|
||||
controller?.abort();
|
||||
controller = null;
|
||||
state = null;
|
||||
busy = false;
|
||||
failure = "";
|
||||
synchronizationPending = false;
|
||||
render();
|
||||
}
|
||||
@@ -499,6 +499,35 @@ const DICTIONARY = {
|
||||
"diag.noAttempts": "No upstream attempts",
|
||||
"diag.noMatch": "No matching models",
|
||||
|
||||
"goAuth.title": "Go subscription sign-in",
|
||||
"goAuth.caption": "Authorize on the official OpenCode page without copying credentials. Manual Go keys are kept but are not fallback routes while the subscription is connected; their previous routing resumes after disconnecting.",
|
||||
"goAuth.refresh": "Refresh sign-in status",
|
||||
"goAuth.start": "Sign in to Go in your browser",
|
||||
"goAuth.reconnect": "Sign in to Go again",
|
||||
"goAuth.cancel": "Cancel this sign-in",
|
||||
"goAuth.disconnect": "Disconnect Go subscription",
|
||||
"goAuth.disconnectAction": "Disconnect",
|
||||
"goAuth.disconnectConfirm": "This only removes the subscription sign-in saved by the gateway. Manual Go keys are kept and the official OpenCode session is not revoked. Continue?",
|
||||
"goAuth.notice": "Only confirmed Go subscription routes are used; there is no automatic switch to pay-as-you-go. Unavailable OAuth subscription quota stays unknown instead of being estimated.",
|
||||
"goAuth.connected": "Go subscription connected. Sign-in credentials stay on the gateway server.",
|
||||
"goAuth.needsLogin": "Your Go subscription sign-in has expired. Sign in again. Manual Go keys will not be used as fallback routes.",
|
||||
"goAuth.disconnected": "No Go subscription is connected. Manually configured Go keys are not affected.",
|
||||
"goAuth.organizationValue": "Organization: {0}",
|
||||
"goAuth.models": "Loaded {0} subscription models.",
|
||||
"goAuth.expires": "Access credential expires at {0}; the gateway refreshes it when needed.",
|
||||
"goAuth.expired": "This sign-in has expired. Start again.",
|
||||
"goAuth.failed": "Subscription sign-in did not finish. Start again.",
|
||||
"goAuth.organization": "Subscription organization",
|
||||
"goAuth.chooseOrganization": "Select the organization with your subscription",
|
||||
"goAuth.confirmOrganization": "Connect selected organization",
|
||||
"goAuth.starting": "Requesting an official sign-in code…",
|
||||
"goAuth.instructions": "Open the official page to sign in, and confirm that you initiated this authorization.",
|
||||
"goAuth.userCode": "Verification code: ",
|
||||
"goAuth.openVerification": "Open official OpenCode sign-in",
|
||||
"goAuth.invalidURL": "The official verification link is invalid. Cancel and try again.",
|
||||
"goAuth.pendingExpires": "Code valid until {0}; authorization will be checked automatically.",
|
||||
"goAuth.error": "Operation failed: {0}",
|
||||
|
||||
"quotas.noKey": "No Go Key configured",
|
||||
"quotas.addKeyFirst": "Add an OpenCode Go Key in the Configuration center first.",
|
||||
"quotas.queryFailed": "Query failed: {0}. The content shown is only the previous query result.",
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Page: 模型与额度 — Go quota windows and free model availability.
|
||||
import { api, hasSession } from "../api.js";
|
||||
import { loadDebugModels } from "../data.js";
|
||||
import * as goAuth from "../go-auth.js";
|
||||
import { t, onChange } from "../i18n.js";
|
||||
import { $, bindTabs, copyText, el, emptyRow, localTime, toast } from "../ui.js";
|
||||
|
||||
@@ -14,36 +15,44 @@ let data = null;
|
||||
let timer = null;
|
||||
let loading = false;
|
||||
let activeTab = "quota";
|
||||
let generation = 0;
|
||||
|
||||
async function load() {
|
||||
if (loading) return;
|
||||
const operation = generation;
|
||||
loading = true;
|
||||
$("#quota-refresh").disabled = true;
|
||||
$("#quota-refresh").textContent = t("读取中…", "quotas.loading");
|
||||
try {
|
||||
const value = await api("/api/model-quotas");
|
||||
if (!hasSession()) return;
|
||||
if (operation !== generation || !hasSession()) return;
|
||||
data = value;
|
||||
const select = $("#quota-account");
|
||||
const selected = select.value;
|
||||
select.replaceChildren();
|
||||
for (const account of value.accounts || []) {
|
||||
select.append(el("option", { value: account.id, text: `Go Key ${account.display}` }));
|
||||
select.append(el("option", {
|
||||
value: account.id,
|
||||
text: value.source === "" ? account.display : `Go Key ${account.display}`,
|
||||
}));
|
||||
}
|
||||
if ([...select.options].some((option) => option.value === selected)) select.value = selected;
|
||||
if (!select.options.length)
|
||||
select.append(el("option", { value: "", text: t("尚未配置 Go Key", "quotas.noKey") }));
|
||||
render();
|
||||
} catch (error) {
|
||||
if (operation !== generation || !hasSession()) return;
|
||||
$("#quota-status").textContent = t(
|
||||
`查询失败:${error.message}。已有显示仅为上次查询结果。`,
|
||||
"quotas.queryFailed",
|
||||
error.message,
|
||||
);
|
||||
} finally {
|
||||
loading = false;
|
||||
$("#quota-refresh").disabled = false;
|
||||
$("#quota-refresh").textContent = t("刷新额度", "quotas.refresh");
|
||||
if (operation === generation) {
|
||||
loading = false;
|
||||
$("#quota-refresh").disabled = false;
|
||||
$("#quota-refresh").textContent = t("刷新额度", "quotas.refresh");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,12 +267,15 @@ export default {
|
||||
title: "模型与额度",
|
||||
sub: "共享套餐额度与免费模型探测状态。",
|
||||
init() {
|
||||
goAuth.init(load);
|
||||
bindTabs($("#page-model-quotas"), (id) => {
|
||||
activeTab = id;
|
||||
if (id === "quota") {
|
||||
goAuth.show();
|
||||
load();
|
||||
startPolling();
|
||||
} else {
|
||||
goAuth.hide();
|
||||
stopPolling();
|
||||
loadAvailability();
|
||||
}
|
||||
@@ -284,6 +296,7 @@ export default {
|
||||
},
|
||||
show() {
|
||||
if (activeTab === "quota") {
|
||||
goAuth.show();
|
||||
load();
|
||||
startPolling();
|
||||
} else {
|
||||
@@ -291,10 +304,29 @@ export default {
|
||||
}
|
||||
},
|
||||
hide() {
|
||||
goAuth.hide();
|
||||
stopPolling();
|
||||
},
|
||||
refresh() {
|
||||
if (activeTab === "quota") load();
|
||||
else loadAvailability();
|
||||
if (activeTab === "quota") {
|
||||
goAuth.refresh();
|
||||
load();
|
||||
} else loadAvailability();
|
||||
},
|
||||
reset() {
|
||||
generation++;
|
||||
data = null;
|
||||
loading = false;
|
||||
stopPolling();
|
||||
goAuth.reset();
|
||||
$("#quota-account").replaceChildren();
|
||||
$("#quota-cards").replaceChildren();
|
||||
$("#quota-models").replaceChildren();
|
||||
$("#quota-updated").textContent = "";
|
||||
$("#quota-catalog-status").textContent = "";
|
||||
$("#quota-model-count").textContent = "";
|
||||
$("#quota-refresh").disabled = false;
|
||||
$("#quota-refresh").textContent = t("刷新额度", "quotas.refresh");
|
||||
$("#quota-status").textContent = t("进入页面后读取额度。", "quotas.pending");
|
||||
},
|
||||
};
|
||||
Reference in new issue
Block a user