Files
opencode2api/webui/js/pages/access.js
T
michaelliunsky a0da31ed08 feat(webui): rebuild console with tabbed pages
Replace the single-file frontend (app.js, styles.css) with a multi-file
ES module structure served from go:embed, and reorganize the 13 pages
into 8 tabbed pages under 4 navigation groups: runtime (overview,
diagnostics), integrations (access guide, API keys), models (quotas,
rotation), and system (configuration, account).

The redesign adopts a monochrome light/dark design system driven by
prefers-color-scheme, replaces prompt()/confirm() flows with in-page
dialogs, and keeps every management API call and deep link (old hashes
alias to the new pages).

Behavior changes: the log SSE connection now spans the whole console
session and buffers while the stream tab is hidden; quota polling runs
only while its tab is active; the first-run checklist appears on the
overview page and hides once ready.

Also fixes a crash on the rotation page: the persisted state serializes
empty orders and maps as JSON null, which is now normalized before
rendering, with the same defensive handling in the quota and API key
pages.

CI now syntax-checks the ES modules via node --input-type=module, and
the README structure notes are updated.
2026-10-16 18:09:42 +08:00

48 lines
2.6 KiB
JavaScript

// Page: 接入手册 — derived API base, protocol and SDK examples with copy buttons.
import { derivedAPIBase } from "../api.js";
import * as store from "../store.js";
import { $, el, copyText } from "../ui.js";
function codeCard(label, text) {
const copy = el("button", { type: "button", class: "btn btn-sm btn-ghost", text: "复制" });
copy.addEventListener("click", () => copyText(text, "示例已复制"));
return el("div", { class: "code-card" }, [
el("div", { class: "code-head" }, [el("span", { text: label }), copy]),
el("pre", {}, [el("code", { text })]),
]);
}
function render() {
const config = store.get("config");
if (!config) return;
const base = derivedAPIBase();
const model = $("#access-model")?.value.trim() || "YOUR_MODEL";
$("#access-base-value").value = base;
const protocols = $("#access-protocol-examples");
const sdks = $("#access-sdk-examples");
protocols.replaceChildren();
sdks.replaceChildren();
const chat = `curl ${base}/v1/chat/completions \\\n -H "Authorization: Bearer YOUR_API_KEY" \\\n -H "Content-Type: application/json" \\\n -d '{"model":"${model}","messages":[{"role":"user","content":"Hello"}]}'`;
const responses = `curl ${base}/v1/responses \\\n -H "Authorization: Bearer YOUR_API_KEY" \\\n -H "Content-Type: application/json" \\\n -d '{"model":"${model}","input":"Hello"}'`;
const anthropic = `curl ${base}/v1/messages \\\n -H "x-api-key: YOUR_API_KEY" \\\n -H "anthropic-version: 2023-06-01" \\\n -H "Content-Type: application/json" \\\n -d '{"model":"${model}","max_tokens":512,"messages":[{"role":"user","content":"Hello"}]}'`;
protocols.append(
codeCard("Chat Completions", chat),
codeCard("Responses", responses),
codeCard("Anthropic Messages", anthropic),
);
const python = `from openai import OpenAI\n\nclient = OpenAI(base_url="${base}/v1", api_key="YOUR_API_KEY")\nresponse = client.chat.completions.create(\n model="${model}",\n messages=[{"role": "user", "content": "Hello"}],\n)\nprint(response.choices[0].message.content)`;
const javascript = `import OpenAI from "openai";\n\nconst client = new OpenAI({\n baseURL: "${base}/v1",\n apiKey: "YOUR_API_KEY",\n});\nconst response = await client.responses.create({\n model: "${model}",\n input: "Hello",\n});\nconsole.log(response.output_text);`;
sdks.append(codeCard("Python · OpenAI SDK", python), codeCard("JavaScript · OpenAI SDK", javascript));
}
export default {
id: "access",
title: "接入手册",
sub: "Chat、Responses、Anthropic 与 SDK 示例。",
init() {
store.on("config", render);
$("#access-model").addEventListener("input", render);
},
show: render,
};