File size: 5,510 Bytes
d3a7520
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
757f620
d3a7520
 
 
 
757f620
 
 
d3a7520
 
 
 
 
 
 
757f620
d3a7520
 
 
 
 
b2d8381
 
 
d3a7520
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2d8381
d3a7520
 
 
b2d8381
 
 
 
 
 
d3a7520
 
 
 
 
 
 
 
 
 
 
 
 
 
b2d8381
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3a7520
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2d8381
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3a7520
b2d8381
d3a7520
b2d8381
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
async function apiJson(url, options = undefined) {
  const resp = await fetch(url, options);
  if (!resp.ok) {
    const data = await resp.text();
    throw new Error(data || `HTTP ${resp.status}`);
  }
  return resp.json();
}

function esc(s) {
  return String(s || "").replace(/[&<>"']/g, (c) => ({
    "&": "&amp;",
    "<": "&lt;",
    ">": "&gt;",
    '"': "&quot;",
    "'": "&#39;",
  })[c]);
}

async function refreshBilling() {
  const summary = await apiJson("/api/billing/me");
  const rows = await apiJson("/api/billing/me/records?limit=20");

  document.getElementById("billingSummary").textContent =
    `总 tokens=${summary.total_tokens} | 总费用(USD)=${Number(
      summary.total_cost_usd,
    ).toFixed(6)}(仅统计计费模型,不含 SiliconFlowFree 等免费模型)`;

  const body = document.getElementById("billingBody");
  body.innerHTML = "";
  for (const r of rows.records) {
    const cost = Number(r.cost_usd).toFixed(6);
    const costLabel = cost === "0.000000" ? `${cost}(不计费模型)` : cost;

    const tr = document.createElement("tr");
    tr.innerHTML = `
      <td>${esc(r.created_at)}</td>
      <td class="mono">${esc(r.model)}</td>
      <td>${r.prompt_tokens}</td>
      <td>${r.completion_tokens}</td>
      <td>${r.total_tokens}</td>
      <td>${costLabel}</td>
    `;
    body.appendChild(tr);
  }
}

// 任务状态缓存:在前端维护一个简单的内存表,方便 SSE/轮询统一渲染
const jobsState = new Map();

function actionButtons(job) {
  const actions = [];
  if (job.status === "queued" || job.status === "running") {
    actions.push(
      `<button class="danger" onclick="cancelJob('${job.id}')">取消</button>`,
    );
  }
  if (job.artifact_urls?.mono) {
    actions.push(
      `<a href="${job.artifact_urls.mono}"><button class="muted">单语版</button></a>`,
    );
  }
  if (job.artifact_urls?.dual) {
    actions.push(
      `<a href="${job.artifact_urls.dual}"><button class="muted">双语版</button></a>`,
    );
  }
  if (job.artifact_urls?.glossary) {
    actions.push(
      `<a href="${job.artifact_urls.glossary}"><button class="muted">术语表</button></a>`,
    );
  }
  return actions.join(" ");
}

function statusText(status) {
  const statusMap = {
    queued: "排队中",
    running: "进行中",
    succeeded: "成功",
    failed: "失败",
    cancelled: "已取消",
  };
  return statusMap[status] || status;
}

function renderJobsFromState() {
  const body = document.getElementById("jobsBody");
  body.innerHTML = "";

  const jobs = Array.from(jobsState.values());
  jobs.sort((a, b) =>
    (b.created_at || "").localeCompare(a.created_at || ""),
  );

  for (const job of jobs) {
    const tr = document.createElement("tr");
    tr.innerHTML = `
      <td class="mono">${esc(job.id)}</td>
      <td>${esc(job.filename)}</td>
      <td>${esc(statusText(job.status))}${job.error ? " / " + esc(job.error) : ""}</td>
      <td>${Number(job.progress).toFixed(1)}%</td>
      <td class="mono">${esc(job.model)}</td>
      <td class="mono">${esc(job.updated_at)}</td>
      <td class="actions">${actionButtons(job)}</td>
    `;
    body.appendChild(tr);
  }
}

function upsertJob(jobPatch) {
  const existing = jobsState.get(jobPatch.id) || {};
  jobsState.set(jobPatch.id, { ...existing, ...jobPatch });
  renderJobsFromState();
}

async function refreshJobs() {
  const data = await apiJson("/api/jobs?limit=50");
  jobsState.clear();
  for (const job of data.jobs) {
    jobsState.set(job.id, job);
  }
  renderJobsFromState();
}

async function cancelJob(jobId) {
  try {
    await apiJson(`/api/jobs/${jobId}/cancel`, { method: "POST" });
    await refreshJobs();
  } catch (err) {
    alert(`取消失败: ${err.message}`);
  }
}

document.getElementById("jobForm").addEventListener("submit", async (event) => {
  event.preventDefault();
  const status = document.getElementById("jobStatus");
  status.textContent = "提交中...";

  const formData = new FormData(event.target);
  try {
    const created = await apiJson("/api/jobs", { method: "POST", body: formData });
    status.textContent = `任务已入队: ${created.job.id}`;
    event.target.reset();
    await refreshJobs();
  } catch (err) {
    status.textContent = `提交失败: ${err.message}`;
  }
});

async function refreshAll() {
  await Promise.all([refreshJobs(), refreshBilling()]);
}

let jobEventSource = null;
let pollingEnabled = true;
const POLL_INTERVAL_MS = 10000;

function setupJobStream() {
  if (!("EventSource" in window)) {
    console.warn("EventSource not supported, fallback to polling");
    pollingEnabled = true;
    return;
  }

  jobEventSource = new EventSource("/api/jobs/stream");

  jobEventSource.onmessage = (event) => {
    try {
      const payload = JSON.parse(event.data);
      if (!payload || !payload.id) {
        return;
      }
      upsertJob(payload);
    } catch (err) {
      console.error("Failed to parse job SSE payload:", err);
    }
  };

  jobEventSource.onerror = () => {
    console.error("Job SSE error, switching back to polling");
    if (jobEventSource) {
      jobEventSource.close();
      jobEventSource = null;
    }
    pollingEnabled = true;
  };

  pollingEnabled = false;
}

refreshAll();
setupJobStream();

setInterval(async () => {
  if (document.hidden) {
    // 页面不可见时降低刷新频率:完全跳过本轮
    return;
  }

  if (pollingEnabled) {
    await refreshAll();
  } else {
    await refreshBilling();
  }
}, POLL_INTERVAL_MS);