Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions static/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
const $ = (s) => document.querySelector(s);
const $$ = (s) => document.querySelectorAll(s);

/**
* Fetch with one automatic retry on network errors (not HTTP errors).
* Shows a toast during the retry wait.
*/
async function fetchWithRetry(url, options = {}) {
try {
return await fetch(url, options);
} catch (err) {
if (err instanceof TypeError) {
// Network error — retry once after 2s
toast("网络波动,正在重试...", 2500);
await new Promise((r) => setTimeout(r, 2000));
return await fetch(url, options);
}
throw err;
}
}

let selectedAvatar = null;
let avatarData = {};
let allAvatars = [];
Expand Down Expand Up @@ -33,7 +51,7 @@ async function loadVoices() {

async function loadCategories() {
try {
const r = await fetch("/api/categories");
const r = await fetchWithRetry("/api/categories");
if (!r.ok) return;
const cats = await r.json();
const container = $("#category-filter");
Expand Down Expand Up @@ -95,7 +113,7 @@ function renderAvatars() {

async function loadAvatars() {
try {
const r = await fetch("/api/avatars");
const r = await fetchWithRetry("/api/avatars");
if (!r.ok) return;
allAvatars = await r.json();
allAvatars.forEach((a) => { avatarData[a.id] = a; });
Expand Down Expand Up @@ -183,7 +201,7 @@ async function generate() {
loadingText.textContent = "AI 思考中...";
const chatForm = new FormData();
chatForm.append("message", text);
const chatResp = await fetch("/api/chat", { method: "POST", body: chatForm });
const chatResp = await fetchWithRetry("/api/chat", { method: "POST", body: chatForm });
if (!chatResp.ok) throw new Error("AI 服务暂时不可用,请稍后重试");
const chatData = await chatResp.json();
const msg = chatData.message;
Expand All @@ -200,7 +218,7 @@ async function generate() {
form.append("engine", currentEngine);
form.append("text", spokenText);

const resp = await fetch("/api/generate", { method: "POST", body: form });
const resp = await fetchWithRetry("/api/generate", { method: "POST", body: form });
const elapsed = ((Date.now() - t0) / 1000).toFixed(1);

if (!resp.ok) {
Expand Down
4 changes: 2 additions & 2 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>数字人直播</title>
<link rel="icon" type="image/png" href="/favicon.png">
<link rel="stylesheet" href="style.css?v=14">
<link rel="stylesheet" href="style.css?v=15">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;500;600&display=swap" rel="stylesheet">
</head>
Expand Down Expand Up @@ -100,6 +100,6 @@

<div id="toast" class="toast hidden"></div>

<script src="app.js?v=14"></script>
<script src="app.js?v=15"></script>
</body>
</html>
Loading