fix: enable AI subtitles after material save

This commit is contained in:
TTlaji committed 2026-06-03 13:51:13 +08:00
1 parent 72072c884c
commit 5ff72bf1b6
3 files changed
+77 -6

No files matched your search

+1 -1
View File
@@ -1,3 +1,3 @@
DEEPSEEK_API_KEY=
DEEPSEEK_BASE_URL=https://api.deepseek.com
DEEPSEEK_BASE_URL=https://coding.simgreek.com/v1
DEEPSEEK_MODEL=deepseek-chat
+52 -5
View File
@@ -153,9 +153,10 @@ async function loadAiStatus() {
if (!els.aiStatus) return;
try {
const status = await apiRequest("/api/ai/status");
els.aiStatus.textContent = status.message || "AI 状态未知。";
const buttonState = getAiSubtitleButtonState(status, editingVideo);
els.aiStatus.textContent = buttonState.hint || status.message || "AI 状态未知。";
if (els.processAiSubtitlesBtn) {
els.processAiSubtitlesBtn.disabled = !status.configured || !editingVideo;
els.processAiSubtitlesBtn.disabled = buttonState.disabled;
}
} catch (error) {
els.aiStatus.textContent = error.message || "AI 状态读取失败。";
@@ -163,6 +164,27 @@ async function loadAiStatus() {
}
}
function getAiSubtitleButtonState(status, selectedVideo) {
if (!status?.configured) {
return {
disabled: true,
hint: "请先配置 DeepSeek API。",
};
}
if (!selectedVideo) {
return {
disabled: true,
hint: "请先在右侧素材列表点“编辑”。",
};
}
return {
disabled: false,
hint: "",
};
}
async function testAiConnection() {
try {
const result = await apiRequest("/api/ai/test", { method: "POST" });
@@ -315,6 +337,19 @@ function resetFormMode() {
loadAiStatus();
}
function pickVideoAfterSave(videoList, response, previousEditingVideo) {
const savedVideo = response?.video || response;
if (savedVideo?.id) {
return videoList.find((video) => String(video.id) === String(savedVideo.id)) || savedVideo;
}
if (previousEditingVideo?.id) {
return videoList.find((video) => String(video.id) === String(previousEditingVideo.id)) || previousEditingVideo;
}
return null;
}
async function addVideo(event) {
event.preventDefault();
clearMessage();
@@ -342,18 +377,24 @@ async function addVideo(event) {
if (els.subtitleEn.files?.[0]) formData.append("subtitle_en", els.subtitleEn.files[0]);
if (els.subtitleZh.files?.[0]) formData.append("subtitle_zh", els.subtitleZh.files[0]);
if (editingVideo) appendEditedSubtitleContent(formData);
const previousEditingVideo = editingVideo;
try {
const path = editingVideo ? `/api/videos/${editingVideo.id}` : "/api/videos";
const method = editingVideo ? "PATCH" : "POST";
await apiRequest(path, {
const response = await apiRequest(path, {
method,
body: formData,
});
const successMessage = editingVideo ? "素材已更新。" : "素材已添加。";
requestSucceeded = true;
resetFormMode();
await loadVideos();
const savedVideo = pickVideoAfterSave(videos, response, previousEditingVideo);
if (savedVideo) {
startEditingVideo(savedVideo);
} else {
resetFormMode();
}
showMessage(successMessage);
} catch (error) {
showMessage(error.message || (editingVideo ? "更新素材失败。" : "添加素材失败。"), true);
@@ -364,7 +405,13 @@ async function addVideo(event) {
}
if (typeof module !== "undefined" && module.exports) {
module.exports = { getFileInputState, parseSubtitleBlocks, buildWebVtt };
module.exports = {
getFileInputState,
parseSubtitleBlocks,
buildWebVtt,
getAiSubtitleButtonState,
pickVideoAfterSave,
};
}
function createEmptySubtitleEditState() {
+24
View File
@@ -0,0 +1,24 @@
import assert from "node:assert/strict";
import adminHelpers from "../admin.js";
const { getAiSubtitleButtonState, pickVideoAfterSave } = adminHelpers;
assert.deepEqual(getAiSubtitleButtonState({ configured: false }, { id: 1 }), {
disabled: true,
hint: "请先配置 DeepSeek API。",
});
assert.deepEqual(getAiSubtitleButtonState({ configured: true }, null), {
disabled: true,
hint: "请先在右侧素材列表点“编辑”。",
});
assert.deepEqual(getAiSubtitleButtonState({ configured: true }, { id: 1 }), {
disabled: false,
hint: "",
});
const created = { id: 9, title: "New material" };
const previous = { id: 7, title: "Old material" };
assert.equal(pickVideoAfterSave([previous, created], { video: created }, null), created);
assert.equal(pickVideoAfterSave([previous, created], { ok: true }, previous), previous);