Skip to content

Spot it. Save it. Free, local-first Chrome extension for images, videos and prompts. Explore 2,000+ curated cases and connect your library to AI Agents via MCP. 提示词导演

完善 GLM 视频分析与单一任务路由

MigenAI37a0feec5503
diff --git a/.github/workflows/release-safety.yml b/.github/workflows/release-safety.yml
index 972f0e9..103e960 100644
--- a/.github/workflows/release-safety.yml
+++ b/.github/workflows/release-safety.yml
@@ -54,9 +54,8 @@ jobs:
run: |
python -m pip install playwright==1.58.0
python -m playwright install --with-deps chromium
- - run: npm run test:e2e
+ - run: npm run test:local-extension
- run: npm run package:release
- - run: npm run package
- run: npm run test:upgrade
- name: Verify both Chrome archives
run: |
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b29e8ac..3557650 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,12 @@
# Changelog
+## 1.19.5
+
+- AI 服务保持一次连接,任务路由直接展示文字、Skill、创作规划、图片与视频实际调用的服务和模型;新连接只补齐空缺或仍由连接管理的路由,不覆盖用户手动分配。
+- 案例详情右侧统一提供视频逆推、创意拆解、广告审片和自定义问题;每个入口直接编辑本次指令并一次发送,逆推结果保存在对应视频的 AI 提示词中,可选在同一次请求生成视觉标签且不覆盖原始提示词。
+- 图片与视频批量分析保持独立入口;视频批量默认并发 2,汇总展示并确认缺失文件、不兼容来源和正在分析的排除数量与原因,后台恢复不会自动重发状态未知的付费请求。
+- 创作台和 Skill 按具体素材分别使用原始提示词、当前 AI 视觉逆推与人工时间笔记;原始内容优先且不会被 AI 结果或其他视频的内容覆盖。
+
## 1.19.4
- 新增智谱 GLM 服务连接和按任务模型分配,动态读取账号模型目录;GLM-5.3-Flash 可用于文字标签、Skill 提炼、创作规划、图片分析与视频分析,未确认能力的模型不会被静默代替。
diff --git a/README.md b/README.md
index f44d346..e332545 100644
--- a/README.md
+++ b/README.md
@@ -40,7 +40,7 @@ npm install
npm run verify
```
-`npm run verify` 是唯一正式发布门槛:先完成源码、资源、历史数据兼容与单元测试,再跑完整浏览器 E2E;全部通过后生成商店包和固定 ID 本地包,并用上一正式版资料执行同一 Chrome Profile 的最终包升级演练。任一阶段失败都不会通过门禁。只需快速检查源码时可运行 `npm run verify:source`,只检查历史数据合同可运行 `npm run check:compat`。`npm run package` 生成保留固定身份的本地安装包;`npm run package:release` 生成移除 `key` 的 Chrome Web Store 上传包。两者包含相同的运行代码,只有安装身份用途不同。正式身份缺失时命令会明确失败,避免用户资料被错误扩展 ID 隔离。
+`npm run verify` 是唯一正式发布门槛:先完成源码、资源、历史数据兼容与单元测试,再把固定 ID 本地包加载到隔离的 Chrome for Testing 中跑完整浏览器 E2E;全部通过后生成商店包,并用上一正式版资料执行同一 Chrome Profile 的最终包升级演练。任一阶段失败都不会通过门禁。只需快速检查源码时可运行 `npm run verify:source`,只检查历史数据合同可运行 `npm run check:compat`。`npm run test:local-extension` 是打包后自动化主验收;`npm run package` 生成保留固定身份的本地安装包;`npm run package:release` 生成移除 `key` 的 Chrome Web Store 上传包。两者包含相同的运行代码,只有安装身份用途不同。正式身份缺失时命令会明确失败,避免用户资料被错误扩展 ID 隔离。
已发布数据的长期支持规则、权威夹具和升级门禁见[数据兼容说明](docs/DATA_COMPATIBILITY.md)。当前浏览器兼容边界和已确认但延期的增强记录在[已知限制](docs/KNOWN_LIMITATIONS.md)。
diff --git a/ai-model-capabilities.js b/ai-model-capabilities.js
index 702f8b8..f5bd83e 100644
--- a/ai-model-capabilities.js
+++ b/ai-model-capabilities.js
@@ -83,7 +83,7 @@ export const AI_MODEL_CAPABILITIES = Object.freeze([
supportedAspectRatios: Object.freeze([]),
contextLength: 1_000_000,
structuredOutput: "json_object",
- structuredOutputTokenBudget: 4096,
+ structuredOutputTokenBudget: 8192,
mediaInput: Object.freeze({
imageBase64: "data_url",
localVideo: "base64",
diff --git a/ai-provider-registry.js b/ai-provider-registry.js
index 785e6fe..ee8d95f 100644
--- a/ai-provider-registry.js
+++ b/ai-provider-registry.js
@@ -23,6 +23,7 @@ const MANUAL_ASSIGNMENT_TASKS = new Set([
const TEXT_ANALYSIS_TASKS = new Set(["textTags", "skillExtraction", "creativePlanning"]);
export const DEFAULT_TEXT_ANALYSIS_CONCURRENCY = 20;
export const DEFAULT_MEDIA_ANALYSIS_CONCURRENCY = 10;
+export const DEFAULT_VIDEO_ANALYSIS_CONCURRENCY = 2;
export function normalizeAiProviderRegistry(value = {}) {
const source = value?.providers && typeof value.providers === "object" ? value.providers : {};
@@ -52,6 +53,9 @@ export function normalizeAiTaskAssignments(value = {}, registryValue = {}) {
? source.evidence
: inferredEvidence === "manual_unverified" ? inferredEvidence : "";
if (evidence) assignment.evidence = evidence;
+ if (assignment.providerId && assignment.model) {
+ assignment.managedBy = source.managedBy === "connection" ? "connection" : "task";
+ }
assignment.concurrency = normalizeTaskConcurrency(
task.id,
source.concurrency,
@@ -61,6 +65,37 @@ export function normalizeAiTaskAssignments(value = {}, registryValue = {}) {
}));
}
+export function applyConnectionModelAssignments(assignmentsValue = {}, selectionValue = {}, registryValue = {}) {
+ const registry = normalizeAiProviderRegistry(registryValue);
+ const current = normalizeAiTaskAssignments(assignmentsValue, registry);
+ const providerId = clean(selectionValue?.providerId);
+ const model = clean(selectionValue?.model);
+ const taskIds = new Set((Array.isArray(selectionValue?.taskIds) ? selectionValue.taskIds : [])
+ .filter((taskId) => AI_ASSIGNMENT_TASKS.some((task) => task.id === taskId)));
+ if (!providerId || !model || taskIds.size === 0) return current;
+ if (registry.providers[providerId]?.consent !== true) return current;
+ const allowManualUnverifiedTasks = new Set((Array.isArray(selectionValue?.allowManualUnverifiedTasks)
+ ? selectionValue.allowManualUnverifiedTasks : [])
+ .filter((taskId) => taskIds.has(taskId)));
+ const next = structuredClone(current);
+ for (const taskId of taskIds) {
+ const existing = current[taskId] ?? {};
+ const empty = !existing.providerId || !existing.model;
+ const managedByThisConnection = existing.managedBy === "connection" && existing.providerId === providerId;
+ if (!empty && !managedByThisConnection) continue;
+ const assignment = createAiTaskAssignment(taskId, providerId, model, registry);
+ if (assignment.evidence === "manual_unverified" && !allowManualUnverifiedTasks.has(taskId)) continue;
+ next[taskId] = normalizeAiTaskAssignments({
+ [taskId]: {
+ ...assignment,
+ managedBy: "connection",
+ concurrency: existing.concurrency
+ }
+ }, registry)[taskId];
+ }
+ return next;
+}
+
export function modelConcurrencyLimit(providerIdValue, modelValue, registryValue = {}) {
const providerId = clean(providerIdValue);
const model = clean(modelValue).replace(/^models\//, "");
@@ -277,7 +312,9 @@ function normalizeConcurrencyLimit(value) {
}
function normalizeTaskConcurrency(taskId, value, documentedLimit) {
- const fallback = TEXT_ANALYSIS_TASKS.has(taskId)
+ const fallback = taskId === "videoAnalysis"
+ ? DEFAULT_VIDEO_ANALYSIS_CONCURRENCY
+ : TEXT_ANALYSIS_TASKS.has(taskId)
? DEFAULT_TEXT_ANALYSIS_CONCURRENCY
: DEFAULT_MEDIA_ANALYSIS_CONCURRENCY;
const requested = Number(value);
diff --git a/ai-runtime.js b/ai-runtime.js
index dfc06a7..21ccbf5 100644
--- a/ai-runtime.js
+++ b/ai-runtime.js
@@ -235,12 +235,31 @@ export function resolveVideoAnalysisTask(configurationValue = {}) {
protocol: profile.protocol,
apiKey: profile.apiKey,
endpoint: profile.endpoint,
+ ...(modelRuntime.structuredOutputTokenBudget ? { maxOutputTokens: modelRuntime.structuredOutputTokenBudget } : {}),
...(["unsupported", "base64"].includes(modelRuntime.mediaInput.localVideo) ? { localVideo: modelRuntime.mediaInput.localVideo } : {}),
...(modelRuntime.mediaInput.preferPublicVideoUrl === true ? { preferPublicVideoUrl: true } : {}),
...(modelRuntime.mediaInput.publicVideoUrl === "direct" ? { publicVideoUrl: "direct" } : {})
};
}
+export function videoAnalysisRouteSnapshot(value = {}) {
+ return {
+ providerId: clean(value.providerId),
+ model: clean(value.model),
+ protocol: clean(value.protocol),
+ endpoint: clean(value.endpoint),
+ localVideo: ["unsupported", "base64"].includes(value.localVideo) ? value.localVideo : "",
+ preferPublicVideoUrl: value.preferPublicVideoUrl === true,
+ publicVideoUrl: value.publicVideoUrl === "direct" ? "direct" : ""
+ };
+}
+
+export function videoAnalysisRouteMatches(left, right) {
+ const expected = videoAnalysisRouteSnapshot(left);
+ const actual = videoAnalysisRouteSnapshot(right);
+ return Object.keys(expected).every((key) => expected[key] === actual[key]);
+}
+
function settingsForTextAssignment(assignmentValue, providers, preferences, strict) {
const providerId = clean(assignmentValue?.providerId);
if (!providerId) {
diff --git a/analysis-batch.js b/analysis-batch.js
index ccbb8ca..3bd3cfb 100644
--- a/analysis-batch.js
+++ b/analysis-batch.js
@@ -8,12 +8,13 @@ import {
textAnalysisReason
} from "./analysis-revision.js";
import { prepareFacetRebuild, validateAnalysisTagResponse } from "./tag-taxonomy.js";
-import { entryMediaAssets, primaryImageAsset } from "./media.js";
+import { currentVideoReconstruction, entryMediaAssets, primaryImageAsset } from "./media.js";
import { ANALYSIS_RETRY_POLICY } from "./analysis-retry-policy.js";
export const ANALYSIS_BATCH_VERSION = 2;
export const ANALYSIS_BATCH_CONCURRENCY = 20;
export const VISION_BATCH_CONCURRENCY = 10;
+export const VIDEO_BATCH_CONCURRENCY = 2;
export async function previewAnalysisBatch(entries = [], options = {}) {
const eligible = [];
@@ -200,13 +201,127 @@ export function createVisionBatchJob(entries = [], options = {}) {
};
}
+export function previewVideoBatch(entries = [], options = {}) {
+ const selected = new Set((Array.isArray(options.entryIds) ? options.entryIds : []).map(String));
+ const includeAllVideos = options.includeAllVideos === true;
+ const reanalyze = options.reanalyze === true;
+ const sendable = new Set((Array.isArray(options.sendableAssetIds) ? options.sendableAssetIds : []).map(String));
+ const snapshots = new Map((Array.isArray(options.assetSnapshots) ? options.assetSnapshots : []).map((item) => [
+ `${String(item?.entryId ?? "")}\u0000${String(item?.assetId ?? "")}`,
+ item
+ ]));
+ const items = [];
+ let skippedAnalyzedCount = 0;
+ let excludedCount = 0;
+ for (const entry of Array.isArray(entries) ? entries : []) {
+ if (!selected.has(String(entry?.id ?? ""))) continue;
+ const videos = entryMediaAssets(entry).filter((asset) => asset.kind === "video" && asset.usage !== "poster");
+ const primary = videos.find((asset) => asset.id === entry.primaryMediaId) || videos[0];
+ const candidates = includeAllVideos ? videos : [primary].filter(Boolean);
+ for (const asset of candidates) {
+ if (!asset?.id || asset.storageMode !== "managed" || !sendable.has(asset.id)) {
+ excludedCount += 1;
+ continue;
+ }
+ const alreadyAnalyzed = Boolean(currentVideoReconstruction(entry, asset.id));
+ if (!reanalyze && alreadyAnalyzed) {
+ skippedAnalyzedCount += 1;
+ continue;
+ }
+ items.push({
+ entryId: String(entry.id),
+ assetId: String(asset.id),
+ title: String(entry.title ?? ""),
+ byteSize: Math.max(0, Number(snapshots.get(`${entry.id}\u0000${asset.id}`)?.byteSize) || Number(asset.byteSize) || 0),
+ durationMs: Math.max(0, Number(snapshots.get(`${entry.id}\u0000${asset.id}`)?.durationMs) || Number(asset.durationMs) || 0),
+ fingerprint: String(snapshots.get(`${entry.id}\u0000${asset.id}`)?.fingerprint ?? ""),
+ sourcePlan: String(snapshots.get(`${entry.id}\u0000${asset.id}`)?.sourcePlan ?? options.sourcePlan ?? "").trim(),
+ alreadyAnalyzed
+ });
+ }
+ }
+ return {
+ kind: "video",
+ caseCount: new Set(items.map((item) => item.entryId)).size,
+ requestCount: items.length,
+ skippedAnalyzedCount,
+ excludedCount,
+ includeAllVideos,
+ reanalyze,
+ includeTags: options.includeTags !== false,
+ providerId: String(options.providerId ?? "").trim(),
+ model: String(options.model ?? "").trim(),
+ protocol: String(options.protocol ?? "").trim(),
+ sourcePlan: String(options.sourcePlan ?? "").trim(),
+ endpoint: String(options.endpoint ?? "").trim(),
+ localVideo: String(options.localVideo ?? "").trim(),
+ preferPublicVideoUrl: options.preferPublicVideoUrl === true,
+ publicVideoUrl: String(options.publicVideoUrl ?? "").trim(),
+ concurrency: normalizeConcurrency(options.concurrency, VIDEO_BATCH_CONCURRENCY),
+ totalBytes: items.reduce((sum, item) => sum + item.byteSize, 0),
+ knownDurationMs: items.reduce((sum, item) => sum + item.durationMs, 0),
+ unknownDurationCount: items.filter((item) => !item.durationMs).length,
+ items
+ };
+}
+
+export function createVideoBatchJob(entries = [], options = {}) {
+ const preview = previewVideoBatch(entries, options);
+ if (!preview.requestCount) throw new Error("没有需要批量逆推的本地视频");
+ const now = String(options.now ?? new Date().toISOString());
+ return {
+ version: ANALYSIS_BATCH_VERSION,
+ kind: "video",
+ id: String(options.id ?? "").trim() || `analysis:${globalThis.crypto.randomUUID()}`,
+ status: "running",
+ createdAt: now,
+ updatedAt: now,
+ outputLocale: options.outputLocale === "en" ? "en" : "zh-CN",
+ providerId: preview.providerId,
+ model: preview.model,
+ protocol: preview.protocol,
+ sourcePlan: preview.sourcePlan,
+ endpoint: preview.endpoint,
+ localVideo: preview.localVideo,
+ preferPublicVideoUrl: preview.preferPublicVideoUrl,
+ publicVideoUrl: preview.publicVideoUrl,
+ concurrency: preview.concurrency,
+ includeAllVideos: preview.includeAllVideos,
+ reanalyze: preview.reanalyze,
+ includeTags: preview.includeTags,
+ instruction: String(options.instruction ?? "").replace(/\r\n?/gu, "\n").trim(),
+ contractVersion: String(options.contractVersion ?? "").trim(),
+ requestCount: preview.requestCount,
+ skippedAnalyzedCount: preview.skippedAnalyzedCount,
+ excludedCount: preview.excludedCount,
+ totalBytes: preview.totalBytes,
+ knownDurationMs: preview.knownDurationMs,
+ unknownDurationCount: preview.unknownDurationCount,
+ items: preview.items.map((item) => ({
+ entryId: item.entryId,
+ assetId: item.assetId,
+ fingerprint: item.fingerprint,
+ sourcePlan: item.sourcePlan,
+ taskId: "",
+ attemptId: "",
+ requestId: "",
+ status: "pending",
+ attempts: 0,
+ claimId: "",
+ error: "",
+ statusCode: 0
+ })),
+ usage: emptyUsage()
+ };
+}
+
export function normalizeAnalysisBatchJob(value) {
if (!value || ![1, ANALYSIS_BATCH_VERSION].includes(value.version) || !value.id || !Array.isArray(value.items)) return null;
const statuses = new Set(["pending", "running", "succeeded", "failed"]);
const jobStatuses = new Set(["running", "paused", "completed", "partial", "failed", "canceled"]);
return {
version: ANALYSIS_BATCH_VERSION,
- kind: value.kind === "vision" ? "vision" : "text_tags",
+ kind: ["vision", "video"].includes(value.kind) ? value.kind : "text_tags",
mode: ["reanalyze", "rebuild"].includes(value.mode) ? "rebuild" : "incremental",
id: String(value.id),
status: jobStatuses.has(value.status) ? value.status : "paused",
@@ -214,16 +329,30 @@ export function normalizeAnalysisBatchJob(value) {
updatedAt: String(value.updatedAt ?? ""),
analysisModel: String(value.analysisModel ?? "") || DEFAULT_ANALYSIS_MODEL,
providerId: String(value.providerId ?? ""),
+ protocol: String(value.protocol ?? ""),
+ sourcePlan: String(value.sourcePlan ?? ""),
+ endpoint: String(value.endpoint ?? ""),
+ localVideo: String(value.localVideo ?? ""),
+ preferPublicVideoUrl: value.preferPublicVideoUrl === true,
+ publicVideoUrl: String(value.publicVideoUrl ?? ""),
outputProtocol: String(value.outputProtocol ?? "json_object"),
retryPolicy: normalizeRetryPolicy(value.retryPolicy),
outputLocale: value.outputLocale === "en" ? "en" : "zh-CN",
providerType: value.providerType === "compatible" ? "compatible" : "openai",
model: String(value.model ?? ""),
includeAllImages: value.includeAllImages === true,
+ includeAllVideos: value.includeAllVideos === true,
reanalyze: value.reanalyze === true,
- concurrency: normalizeConcurrency(value.concurrency, value.kind === "vision" ? VISION_BATCH_CONCURRENCY : ANALYSIS_BATCH_CONCURRENCY),
+ includeTags: value.includeTags !== false,
+ instruction: String(value.instruction ?? "").replace(/\r\n?/gu, "\n").trim(),
+ contractVersion: String(value.contractVersion ?? "").trim(),
+ concurrency: normalizeConcurrency(value.concurrency, value.kind === "video" ? VIDEO_BATCH_CONCURRENCY : value.kind === "vision" ? VISION_BATCH_CONCURRENCY : ANALYSIS_BATCH_CONCURRENCY),
requestCount: Math.max(0, Number(value.requestCount) || value.items.length),
skippedAnalyzedCount: Math.max(0, Number(value.skippedAnalyzedCount) || 0),
+ excludedCount: Math.max(0, Number(value.excludedCount) || 0),
+ totalBytes: Math.max(0, Number(value.totalBytes) || 0),
+ knownDurationMs: Math.max(0, Number(value.knownDurationMs) || 0),
+ unknownDurationCount: Math.max(0, Number(value.unknownDurationCount) || 0),
promptVersion: Number(value.promptVersion) || ANALYSIS_PROMPT_VERSION,
profileFingerprint: String(value.profileFingerprint ?? "").trim(),
catalogRevision: Number(value.catalogRevision) || 0,
@@ -237,6 +366,10 @@ export function normalizeAnalysisBatchJob(value) {
...(String(item.visualId ?? "").trim() ? { visualId: String(item.visualId).trim() } : {}),
...(String(item.assetId ?? "").trim() ? { assetId: String(item.assetId).trim() } : {}),
fingerprint: String(item.fingerprint ?? ""),
+ sourcePlan: String(item.sourcePlan ?? value.sourcePlan ?? ""),
+ taskId: String(item.taskId ?? ""),
+ attemptId: String(item.attemptId ?? ""),
+ requestId: String(item.requestId ?? ""),
textRevision: Math.max(1, Math.floor(Number(item.textRevision) || 1)),
status: item.status === "partial"
? "failed"
@@ -249,7 +382,9 @@ export function normalizeAnalysisBatchJob(value) {
statusCode: Math.max(0, Number(item.statusCode) || 0),
serviceRequests: Math.max(0, Number(item.serviceRequests) || 0),
outputCorrectionRequests: Math.max(0, Number(item.outputCorrectionRequests) || 0),
- cacheHit: item.cacheHit === true
+ cacheHit: item.cacheHit === true,
+ costReturned: item.costReturned === true,
+ cost: item.costReturned === true && Number.isFinite(Number(item.cost)) ? Number(item.cost) : null
}] : []),
usage: normalizeUsage(value.usage)
};
@@ -269,11 +404,15 @@ export function claimAnalysisItems(value, limit, idFactory = () => globalThis.cr
item.claimId = String(idFactory());
item.error = "";
item.statusCode = 0;
+ item.taskId = "";
+ item.attemptId = "";
+ item.requestId = "";
claims.push({
entryId: item.entryId,
...(item.visualId ? { visualId: item.visualId } : {}),
...(item.assetId ? { assetId: item.assetId } : {}),
fingerprint: item.fingerprint,
+ sourcePlan: item.sourcePlan,
textRevision: item.textRevision,
claimId: item.claimId,
attempts: item.attempts
@@ -283,6 +422,22 @@ export function claimAnalysisItems(value, limit, idFactory = () => globalThis.cr
return { job, claims };
}
+export function bindAnalysisItemAttempt(value, entryId, claimId, identity = {}) {
+ const job = requireJob(value);
+ const item = claimedItem(job, entryId, claimId);
+ const taskId = String(identity.taskId ?? "").trim();
+ const attemptId = String(identity.attemptId ?? "").trim();
+ const requestId = String(identity.requestId ?? attemptId).trim();
+ if (!taskId) throw new Error("批量视频任务缺少精确任务编号");
+ if (item.taskId && item.taskId !== taskId) throw new Error("批量视频任务编号已经变化");
+ if (item.attemptId && attemptId && item.attemptId !== attemptId) throw new Error("批量视频执行编号已经变化");
+ item.taskId = taskId;
+ if (attemptId) item.attemptId = attemptId;
+ if (requestId) item.requestId = requestId;
+ touch(job);
+ return job;
+}
+
export function succeedAnalysisItem(value, entryId, claimId, usage, catalogRevision, metadata = {}) {
const job = requireJob(value);
const item = claimedItem(job, entryId, claimId);
@@ -441,14 +596,26 @@ export function recoverInterruptedAnalysisBatch(value) {
return job;
}
-export function retryFailedAnalysisItems(value) {
+export function retryFailedAnalysisItems(value, options = {}) {
const job = requireJob(value);
+ const selected = new Set((Array.isArray(options.itemKeys) ? options.itemKeys : []).map(String));
+ const requireSelection = job.kind === "video" && options.requireSelection === true;
+ if (requireSelection && !selected.size) throw new Error("请选择要重新发送的视频");
let count = 0;
for (const item of job.items) {
if (!["failed", "partial"].includes(item.status)) continue;
+ const itemKey = `${item.entryId}\u0000${item.assetId || item.visualId || ""}`;
+ if (selected.size && !selected.has(itemKey)) continue;
+ if (requireSelection && /状态未知|可能已收到/.test(item.error) && options.confirmDuplicateCharge !== true) {
+ throw new Error("状态未知的视频重新发送前必须确认可能重复计费");
+ }
item.status = "pending";
item.error = "";
item.statusCode = 0;
+ item.claimId = "";
+ item.taskId = "";
+ item.attemptId = "";
+ item.requestId = "";
count += 1;
}
if (!count) throw new Error("没有可重试的失败案例");
@@ -518,6 +685,8 @@ export function analysisBatchSummary(value) {
return {
...job,
counts,
+ unknownCostCount: job.items.filter((item) => item.status === "succeeded" && item.costReturned !== true).length,
+ totalCost: job.items.reduce((sum, item) => sum + (item.costReturned === true ? Number(item.cost) || 0 : 0), 0),
total: job.items.length,
requestAttempts: job.items.reduce((sum, item) => sum + item.serviceRequests, 0),
outputCorrectionRequests: job.items.reduce((sum, item) => sum + item.outputCorrectionRequests, 0),
@@ -621,6 +790,10 @@ function recordItemExecution(item, metadata = {}) {
item.serviceRequests += Math.max(0, Number(metadata.attempts?.serviceRequests ?? metadata.serviceRequests) || 0);
item.outputCorrectionRequests += Math.max(0, Number(metadata.attempts?.outputCorrectionRequests ?? metadata.outputCorrectionRequests) || 0);
item.cacheHit = item.cacheHit || metadata.cacheHit === true;
+ if (Object.hasOwn(metadata, "cost")) {
+ item.costReturned = metadata.cost !== null && Number.isFinite(Number(metadata.cost));
+ item.cost = item.costReturned ? Number(metadata.cost) : null;
+ }
}
function failureCategory(status) {
diff --git a/analysis-task-registry.js b/analysis-task-registry.js
index 6aff7bc..cb24e49 100644
--- a/analysis-task-registry.js
+++ b/analysis-task-registry.js
@@ -11,12 +11,24 @@ const MAX_PERSISTED_ANALYSIS_TASKS = 50;
export function createOrJoinAnalysisTask(value, requestValue = {}, options = {}) {
const state = normalizeAnalysisTaskRegistry(value);
const request = normalizeRequest(requestValue);
- if (!request.sessionId || !request.tempReferenceIds.length || !request.clientRequestId) {
- throw new Error("图片分析任务缺少会话、参考图片或请求编号");
+ if (!request.clientRequestId) throw new Error("分析任务缺少请求编号");
+ if (request.kind === "entry_video") {
+ if (!request.entryId || !request.assetId || !request.mode) throw new Error("视频分析任务缺少案例、视频或分析方式");
+ } else if (!request.sessionId || !request.tempReferenceIds.length) {
+ throw new Error("图片分析任务缺少会话或参考图片");
}
- const existing = state.items.find((task) => task.clientRequestIds.includes(request.clientRequestId));
+ const activeVideoTask = request.kind === "entry_video"
+ ? state.items.find((task) => ["queued", "running"].includes(task.status)
+ && task.request.kind === "entry_video"
+ && task.request.entryId === request.entryId
+ && task.request.assetId === request.assetId)
+ : null;
+ const existing = state.items.find((task) => task.clientRequestIds.includes(request.clientRequestId))
+ ?? (activeVideoTask && sameEntryVideoRequest(activeVideoTask.request, request) ? activeVideoTask : null);
+ if (!existing && activeVideoTask) throw new Error("当前视频已有分析在运行,请等待或停止后再发起");
if (existing) {
if (request.consumerId && !existing.consumerIds.includes(request.consumerId)) existing.consumerIds.push(request.consumerId);
+ if (!existing.clientRequestIds.includes(request.clientRequestId)) existing.clientRequestIds.push(request.clientRequestId);
return { state, task: structuredClone(existing), created: false };
}
const task = createAnalysisTask({
@@ -26,8 +38,26 @@ export function createOrJoinAnalysisTask(value, requestValue = {}, options = {})
});
Object.assign(task, {
request: {
+ kind: request.kind,
sessionId: request.sessionId,
tempReferenceIds: request.tempReferenceIds,
+ entryId: request.entryId,
+ assetId: request.assetId,
+ mode: request.mode,
+ instruction: request.instruction,
+ includeTags: request.includeTags,
+ batchJobId: request.batchJobId,
+ batchClaimId: request.batchClaimId,
+ sourceFingerprint: request.sourceFingerprint,
+ protocol: request.protocol,
+ sourceKind: request.sourceKind,
+ routeEndpoint: request.routeEndpoint,
+ routeProviderId: request.routeProviderId,
+ routeModel: request.routeModel,
+ localVideo: request.localVideo,
+ preferPublicVideoUrl: request.preferPublicVideoUrl,
+ publicVideoUrl: request.publicVideoUrl,
+ hasRouteSnapshot: request.hasRouteSnapshot,
outputLocale: request.outputLocale,
priority: request.priority
},
@@ -96,9 +126,30 @@ function normalizeRegistryTask(value) {
}
function normalizeRequest(value = {}) {
+ const kind = value.kind === "entry_video" ? "entry_video" : "temp_references";
return {
+ kind,
sessionId: clean(value.sessionId),
tempReferenceIds: uniqueIds(value.tempReferenceIds),
+ entryId: kind === "entry_video" ? clean(value.entryId) : "",
+ assetId: kind === "entry_video" ? clean(value.assetId) : "",
+ mode: kind === "entry_video" ? clean(value.mode) : "",
+ instruction: kind === "entry_video" ? cleanMultiline(value.instruction) : "",
+ includeTags: kind === "entry_video" ? value.includeTags !== false : false,
+ batchJobId: kind === "entry_video" ? clean(value.batchJobId) : "",
+ batchClaimId: kind === "entry_video" ? clean(value.batchClaimId) : "",
+ sourceFingerprint: kind === "entry_video" ? clean(value.sourceFingerprint) : "",
+ protocol: kind === "entry_video" ? clean(value.protocol) : "",
+ sourceKind: kind === "entry_video" ? clean(value.sourceKind) : "",
+ routeEndpoint: kind === "entry_video" ? clean(value.routeEndpoint) : "",
+ routeProviderId: kind === "entry_video" ? clean(value.routeProviderId) : "",
+ routeModel: kind === "entry_video" ? clean(value.routeModel) : "",
+ localVideo: kind === "entry_video" ? clean(value.localVideo) : "",
+ preferPublicVideoUrl: kind === "entry_video" && typeof value.preferPublicVideoUrl === "boolean"
+ ? value.preferPublicVideoUrl
+ : null,
+ publicVideoUrl: kind === "entry_video" ? clean(value.publicVideoUrl) : "",
+ hasRouteSnapshot: kind === "entry_video" ? value.hasRouteSnapshot === true : false,
outputLocale: value.outputLocale === "en" ? "en" : "zh-CN",
priority: normalizeAnalysisPriority(value.priority),
consumerId: clean(value.consumerId),
@@ -106,6 +157,27 @@ function normalizeRequest(value = {}) {
};
}
+function sameEntryVideoRequest(left, right) {
+ return left.entryId === right.entryId
+ && left.assetId === right.assetId
+ && left.mode === right.mode
+ && left.instruction === right.instruction
+ && left.includeTags === right.includeTags
+ && left.batchJobId === right.batchJobId
+ && left.batchClaimId === right.batchClaimId
+ && left.sourceFingerprint === right.sourceFingerprint
+ && left.protocol === right.protocol
+ && left.sourceKind === right.sourceKind
+ && left.routeEndpoint === right.routeEndpoint
+ && left.routeProviderId === right.routeProviderId
+ && left.routeModel === right.routeModel
+ && left.localVideo === right.localVideo
+ && left.preferPublicVideoUrl === right.preferPublicVideoUrl
+ && left.publicVideoUrl === right.publicVideoUrl
+ && left.hasRouteSnapshot === right.hasRouteSnapshot
+ && left.outputLocale === right.outputLocale;
+}
+
function uniqueIds(values) {
return [...new Set((Array.isArray(values) ? values : []).map(clean).filter(Boolean))];
}
@@ -113,3 +185,7 @@ function uniqueIds(values) {
function clean(value) {
return String(value ?? "").trim();
}
+
+function cleanMultiline(value) {
+ return String(value ?? "").replace(/\r\n?/gu, "\n").trim();
+}
diff --git a/background.js b/background.js
index 9b80a46..dcbf62a 100644
--- a/background.js
+++ b/background.js
@@ -59,10 +59,12 @@ import {
analysisBatchSummary,
analysisRebuildRecovery,
backfillLegacyAnalysisMeta,
+ bindAnalysisItemAttempt,
cancelAnalysisBatch,
claimAnalysisItems,
createAnalysisBatchUndo,
createAnalysisBatchJob,
+ createVideoBatchJob,
createVisionBatchJob,
failAnalysisItem,
failUnfinishedAnalysisItems,
@@ -71,6 +73,7 @@ import {
normalizeAnalysisBatchJob,
pauseAnalysisBatch,
previewAnalysisBatch,
+ previewVideoBatch,
previewVisionBatch,
reconcileVisionBatchResults,
recoverInterruptedAnalysisBatch,
@@ -83,7 +86,7 @@ import {
} from "./analysis-batch.js";
import { canonicalTextAnalysisInput } from "./analysis-input.js";
import { buildAutomaticVisionJob } from "./automatic-vision.js";
-import { coalesceAnalysisRequest, runScheduledAnalysisWithRetries } from "./analysis-scheduler.js";
+import { coalesceAnalysisRequest, runScheduledAnalysisWithRetries, scheduleAnalysis } from "./analysis-scheduler.js";
import {
completeAnalysisAttempt,
failAnalysisAttempt,
@@ -106,7 +109,7 @@ import {
markEntryTextChanged,
updateEntryText
} from "./analysis-revision.js";
-import { applyDetailOrganizationMappings } from "./tag-taxonomy.js";
+import { applyDetailOrganizationMappings, applyFixedAnalysisTags } from "./tag-taxonomy.js";
import {
buildEntry,
defaultSettingsForLocale,
@@ -134,6 +137,7 @@ import {
import { deleteLocalAssetHandle } from "./local-asset-store.js";
import {
LOCAL_ASSET_REFERENCE_RECORD_TYPE,
+ chunkedBlobFingerprint,
findExactMediaDuplicate,
sha256Blob
} from "./local-media.js";
@@ -167,6 +171,7 @@ import {
import {
addEntryMedia,
addTimeNote,
+ editCurrentVideoReconstruction,
entryMediaAssets,
normalizeEntryMedia,
removeTimeNote,
@@ -302,6 +307,7 @@ import {
import { PAGE_CAPTURE_LIMITS, PAGE_CAPTURE_QUALITY_LIMITS, PORTABLE_LIBRARY_LIMITS } from "./resource-limits.js";
import { publicAiServiceProfiles } from "./ai-service-profiles.js";
import {
+ applyConnectionModelAssignments,
availableAiModelsForTask,
availableAiProvidersForTask,
mergeAiProviderRegistry,
@@ -316,13 +322,15 @@ import {
projectAiRuntime,
resolveTextTaskSettings,
resolveVideoAnalysisTask,
- resolveVisionTaskSettings
+ resolveVisionTaskSettings,
+ videoAnalysisRouteMatches
} from "./ai-runtime.js";
import {
analyzeVideoWithChatCompletions,
analyzeVideoWithGemini,
analyzeVideoWithOpenRouter,
- requireVideoAnalysisConfirmation
+ chatCompletionsVideoSourcePlan,
+ VIDEO_RECONSTRUCTION_CONTRACT_VERSION
} from "./video-analysis.js";
import { detectMediaReferenceProvider } from "./media-reference-resolver.js";
import {
@@ -538,10 +546,13 @@ chrome.alarms.onAlarm.addListener((alarm) => {
});
scheduleLibraryMaintenanceRunner();
scheduleAutomaticVisionRunner();
-scheduleAnalysisBatchRunner();
recoverCreativeJobs().catch((error) => console.error("PromptDirector creative job recovery failed", error));
enqueue(recoverImportJobs).catch((error) => console.error("PromptDirector local import recovery failed", error));
-enqueue(recoverAnalysisTasks).catch((error) => console.error("PromptDirector analysis task recovery failed", error));
+enqueue(async () => {
+ await recoverAnalysisTasks();
+ await recoverVideoBatchExecutionState();
+ scheduleAnalysisBatchRunner();
+}).catch((error) => console.error("PromptDirector analysis task recovery failed", error));
chrome.contextMenus.onClicked.addListener((info, tab) => {
handleContextMenuCapture(info, tab, {
@@ -786,6 +797,8 @@ async function handleMessage(message, interaction = {}) {
return enqueue(async () => verifyAiImageGenerationCredential(message));
case "DISCOVER_AI_PROVIDER_MODELS":
return discoverAiProviderModels(message.providerId, message.force === true);
+ case "PREVIEW_AI_PROVIDER_MODELS":
+ return previewAiProviderModels(message);
case "GET_AI_TASK_RUNTIME":
return enqueue(async () => getAiTaskRuntime(message.taskId, message.allowUnconfigured === true, message.assignment));
case "GET_COMPOSER_AI_RUNTIME":
@@ -828,6 +841,8 @@ async function handleMessage(message, interaction = {}) {
return startOrJoinAnalysisTaskAction(message);
case "GET_ANALYSIS_TASK":
return enqueue(async () => getAnalysisTaskAction(message.taskId));
+ case "GET_ENTRY_VIDEO_ANALYSIS_TASK":
+ return enqueue(async () => getEntryVideoAnalysisTaskAction(message.entryId, message.assetId));
case "DETACH_ANALYSIS_CONSUMER":
return enqueue(async () => detachAnalysisConsumerAction(message));
case "STOP_ANALYSIS_TASK":
@@ -895,8 +910,8 @@ async function handleMessage(message, interaction = {}) {
}
case "ANALYZE_ENTRY_VISUAL_SET":
return analyzeEntryVisualSet(message);
- case "ANALYZE_ENTRY_VIDEO":
- return analyzeEntryVideo(message);
+ case "UPDATE_VIDEO_RECONSTRUCTION_PROMPT":
+ return enqueue(async () => updateVideoReconstructionPrompt(message));
case "UPDATE_VISION_RECONSTRUCTION_PROMPT":
return enqueue(async () => updateVisionReconstructionPrompt(message.entryId, message.visualId, message.reconstructionPrompt));
case "UPDATE_ENTRY_TEXT":
@@ -997,6 +1012,10 @@ async function handleMessage(message, interaction = {}) {
return enqueue(async () => previewVisionBatchTask(message));
case "CREATE_VISION_BATCH":
return enqueue(async () => createVisionBatchTask(message));
+ case "PREVIEW_VIDEO_BATCH":
+ return enqueue(async () => previewVideoBatchTask(message));
+ case "CREATE_VIDEO_BATCH":
+ return enqueue(async () => createVideoBatchTask(message));
case "CLAIM_VISION_BATCH_ITEM":
return enqueue(async () => claimVisionBatchItem(message.jobId));
case "COMPLETE_VISION_BATCH_ITEM":
@@ -1010,7 +1029,10 @@ async function handleMessage(message, interaction = {}) {
case "CANCEL_VISION_BATCH":
return enqueue(async () => updateVisionBatch("cancel", message.jobId));
case "RETRY_VISION_BATCH_FAILURES":
- return enqueue(async () => updateVisionBatch("retry", message.jobId));
+ return enqueue(async () => updateVisionBatch("retry", message.jobId, {
+ itemKeys: message.itemKeys,
+ confirmDuplicateCharge: message.confirmDuplicateCharge
+ }));
case "IMPORT_ANALYSIS_CANDIDATES":
return enqueue(async () => importAnalysisCandidates(message.payload));
case "PREVIEW_FACET_CHANGE":
@@ -2994,7 +3016,7 @@ async function readState() {
restoredArchivedFacetCount: recoveredVocabulary.restoredFacetIds.length,
analysisBatchJob: textBatchSummary,
maintenanceJob: libraryMaintenanceSummary(stored[STORAGE_KEYS.libraryMaintenanceJob]),
- visionBatchJob: analysisBatchSummary(stored[STORAGE_KEYS.batchJob])?.kind === "vision"
+ visionBatchJob: ["vision", "video"].includes(analysisBatchSummary(stored[STORAGE_KEYS.batchJob])?.kind)
? analysisBatchSummary(stored[STORAGE_KEYS.batchJob])
: null,
canUndoAnalysisBatch: Boolean(textBatchSummary && analysisUndo?.jobId === textBatchSummary.id),
@@ -3746,11 +3768,30 @@ async function saveTempReferenceAsCaseAction(message) {
async function startOrJoinAnalysisTaskAction(message) {
const created = await enqueue(async () => {
- const stored = await chrome.storage.local.get(STORAGE_KEYS.analysisTasks);
+ const stored = await chrome.storage.local.get([STORAGE_KEYS.analysisTasks, STORAGE_KEYS.batchJob]);
+ if (message.kind === "entry_video" && String(message.batchJobId ?? "").trim()) {
+ const busy = normalizeAnalysisTaskRegistry(stored[STORAGE_KEYS.analysisTasks]).items.find((task) =>
+ task.request?.kind === "entry_video"
+ && ["queued", "running"].includes(task.status)
+ && task.request.entryId === String(message.entryId ?? "").trim()
+ && task.request.assetId === String(message.assetId ?? "").trim()
+ && task.request.batchJobId !== String(message.batchJobId).trim());
+ if (busy) return { state: stored[STORAGE_KEYS.analysisTasks], task: busy, created: false };
+ }
const result = createOrJoinAnalysisTask(stored[STORAGE_KEYS.analysisTasks], message, {
taskId: `analysis-task:${crypto.randomUUID()}`
});
- await commitLocalChanges({ [STORAGE_KEYS.analysisTasks]: result.state }, { markSyncDirty: false });
+ const changes = { [STORAGE_KEYS.analysisTasks]: result.state };
+ if (message.kind === "entry_video" && String(message.batchJobId ?? "").trim()) {
+ const batchJob = requireAnalysisBatch(stored[STORAGE_KEYS.batchJob], message.batchJobId, "video");
+ changes[STORAGE_KEYS.batchJob] = bindAnalysisItemAttempt(
+ batchJob,
+ message.entryId,
+ message.batchClaimId,
+ { taskId: result.task.id }
+ );
+ }
+ await commitLocalChanges(changes, { markSyncDirty: false });
return result;
});
if (created.created) scheduleAnalysisTaskRun(created.task.id);
@@ -3760,10 +3801,20 @@ async function startOrJoinAnalysisTaskAction(message) {
async function getAnalysisTaskAction(taskId) {
const stored = await chrome.storage.local.get(STORAGE_KEYS.analysisTasks);
const task = analysisTaskById(stored[STORAGE_KEYS.analysisTasks], taskId);
- if (!task) return { ok: false, message: "没有找到图片分析任务" };
+ if (!task) return { ok: false, message: "没有找到分析任务" };
return analysisTaskResponse(task);
}
+async function getEntryVideoAnalysisTaskAction(entryIdValue, assetIdValue) {
+ const entryId = String(entryIdValue ?? "").trim();
+ const assetId = String(assetIdValue ?? "").trim();
+ const stored = await chrome.storage.local.get(STORAGE_KEYS.analysisTasks);
+ const state = normalizeAnalysisTaskRegistry(stored[STORAGE_KEYS.analysisTasks]);
+ const task = state.items.slice().reverse().find((item) => item.request?.kind === "entry_video"
+ && item.request.entryId === entryId && item.request.assetId === assetId);
+ return task ? analysisTaskResponse(task) : { ok: true, task: null };
+}
+
async function detachAnalysisConsumerAction(message) {
const stored = await chrome.storage.local.get(STORAGE_KEYS.analysisTasks);
const detached = detachAnalysisTaskConsumer(
@@ -3781,7 +3832,7 @@ async function stopAnalysisTaskAction(message) {
const stored = await chrome.storage.local.get(STORAGE_KEYS.analysisTasks);
const state = normalizeAnalysisTaskRegistry(stored[STORAGE_KEYS.analysisTasks]);
const current = analysisTaskById(state, message.taskId);
- if (!current) throw new Error("没有找到图片分析任务");
+ if (!current) throw new Error("没有找到分析任务");
const task = stopAnalysisTask(current);
const next = replaceAnalysisTask(state, task);
await commitLocalChanges({ [STORAGE_KEYS.analysisTasks]: next }, { markSyncDirty: false });
@@ -3798,10 +3849,10 @@ async function retryAnalysisTaskAction(message) {
const stored = await chrome.storage.local.get(STORAGE_KEYS.analysisTasks);
const state = normalizeAnalysisTaskRegistry(stored[STORAGE_KEYS.analysisTasks]);
const current = analysisTaskById(state, message.taskId);
- if (!current) throw new Error("没有找到图片分析任务");
+ if (!current) throw new Error("没有找到分析任务");
const previousAttemptId = current.attempts.at(-1)?.id ?? "";
if (String(message.previousAttemptId ?? "").trim() !== previousAttemptId) {
- throw new Error("图片分析任务已经变化,请刷新后再重试");
+ throw new Error("分析任务已经变化,请刷新后再重试");
}
const task = retryAnalysisAttempt(current, {
attemptId: `analysis-attempt:${crypto.randomUUID()}`,
@@ -3832,7 +3883,7 @@ async function runQueuedAnalysisTasks() {
async function runAnalysisTask(taskId, preparedAttemptId = "") {
if (analysisTaskRunners.has(taskId)) return;
const claimed = await enqueue(async () => {
- const stored = await chrome.storage.local.get(STORAGE_KEYS.analysisTasks);
+ const stored = await chrome.storage.local.get([STORAGE_KEYS.analysisTasks, STORAGE_KEYS.batchJob]);
const state = normalizeAnalysisTaskRegistry(stored[STORAGE_KEYS.analysisTasks]);
const current = analysisTaskById(state, taskId);
if (!current) return null;
@@ -3843,7 +3894,20 @@ async function runAnalysisTask(taskId, preparedAttemptId = "") {
return null;
}
const next = replaceAnalysisTask(state, task);
- await commitLocalChanges({ [STORAGE_KEYS.analysisTasks]: next }, { markSyncDirty: false });
+ const changes = { [STORAGE_KEYS.analysisTasks]: next };
+ if (task.request.kind === "entry_video" && task.request.batchJobId && task.request.batchClaimId) {
+ const batchJob = normalizeAnalysisBatchJob(stored[STORAGE_KEYS.batchJob]);
+ if (!batchJob || batchJob.id !== task.request.batchJobId || batchJob.kind !== "video") {
+ throw new Error("批量视频任务已经变化,未发送请求");
+ }
+ changes[STORAGE_KEYS.batchJob] = bindAnalysisItemAttempt(
+ batchJob,
+ task.request.entryId,
+ task.request.batchClaimId,
+ { taskId: task.id, attemptId: task.activeAttemptId, requestId: task.activeAttemptId }
+ );
+ }
+ await commitLocalChanges(changes, { markSyncDirty: false });
return task;
});
if (!claimed) return;
@@ -3851,7 +3915,10 @@ async function runAnalysisTask(taskId, preparedAttemptId = "") {
analysisTaskRunners.set(taskId, { attemptId: claimed.activeAttemptId, controller });
let actionResult;
try {
- actionResult = await analyzeTempReferencesAction({
+ const execute = claimed.request.kind === "entry_video"
+ ? analyzeEntryVideoTaskAction
+ : analyzeTempReferencesAction;
+ actionResult = await execute({
...claimed.request,
taskId: claimed.id,
attemptId: claimed.activeAttemptId,
@@ -3859,7 +3926,7 @@ async function runAnalysisTask(taskId, preparedAttemptId = "") {
signal: controller.signal
});
} catch (error) {
- actionResult = { ok: false, message: userMessage(error) };
+ actionResult = { ok: false, message: userMessage(error), status: Number(error?.status) || 0, usage: error?.usage };
}
let settled;
try {
@@ -3879,6 +3946,10 @@ async function runAnalysisTask(taskId, preparedAttemptId = "") {
analysisTaskRunners.delete(taskId);
}
if (settled) await notifyAnalysisTaskUpdated(settled);
+ if (claimed.request.kind === "entry_video" && claimed.request.batchJobId) {
+ await enqueue(() => settleVideoBatchTask(claimed, actionResult)).catch((error) =>
+ console.error("PromptDirector video batch settlement failed", error));
+ }
}
async function recoverAnalysisTasks() {
@@ -3894,6 +3965,55 @@ async function recoverAnalysisTasks() {
for (const task of recovered.items.filter((item) => item.status === "queued")) scheduleAnalysisTaskRun(task.id);
}
+async function recoverVideoBatchExecutionState() {
+ const [stored, state] = await Promise.all([
+ chrome.storage.local.get([STORAGE_KEYS.batchJob, STORAGE_KEYS.analysisTasks]),
+ readState()
+ ]);
+ let job = normalizeAnalysisBatchJob(stored[STORAGE_KEYS.batchJob]);
+ if (!job || job.kind !== "video" || !job.items.some((item) => item.status === "running")) return;
+ const tasks = normalizeAnalysisTaskRegistry(stored[STORAGE_KEYS.analysisTasks]).items;
+ const entryById = new Map(state.entries.map((entry) => [entry.id, normalizeEntryMedia(entry)]));
+ let changed = false;
+ let hasUnknownExecution = false;
+ for (const item of job.items.filter((candidate) => candidate.status === "running")) {
+ const task = item.taskId ? tasks.find((candidate) => candidate.id === item.taskId) : null;
+ const identityMatches = Boolean(task
+ && task.request?.kind === "entry_video"
+ && task.request.batchJobId === job.id
+ && task.request.batchClaimId === item.claimId
+ && task.request.entryId === item.entryId
+ && task.request.assetId === item.assetId
+ && item.requestId
+ && task.attempts.some((attempt) => attempt.id === item.requestId));
+ const record = identityMatches
+ ? entryById.get(item.entryId)?.videoAnalyses?.find((analysis) =>
+ analysis.batchJobId === job.id && analysis.assetId === item.assetId && analysis.requestId === item.requestId)
+ : null;
+ if (record) {
+ job = succeedAnalysisItem(job, item.entryId, item.claimId, {
+ promptTokens: record.usage?.inputTokens,
+ completionTokens: record.usage?.outputTokens,
+ totalTokens: record.usage?.totalTokens
+ }, undefined, { serviceRequests: 1, cost: record.cost ?? null });
+ changed = true;
+ continue;
+ }
+ if (identityMatches && ["queued", "running"].includes(task.status)) continue;
+ job = failAnalysisItem(job, item.entryId, item.claimId, {
+ message: "上次执行状态未知,未自动重试;服务商可能已收到请求",
+ status: 0,
+ attempts: { serviceRequests: identityMatches ? 1 : 0 }
+ });
+ changed = true;
+ hasUnknownExecution = true;
+ }
+ if (!changed) return;
+ if (hasUnknownExecution) job = { ...job, status: "paused", updatedAt: new Date().toISOString() };
+ await commitLocalChanges({ [STORAGE_KEYS.batchJob]: job }, { markSyncDirty: false });
+ await ensureAnalysisBatchAlarm(job.status === "running");
+}
+
async function analysisTaskAttemptIsActive(taskId, attemptId) {
const stored = await chrome.storage.local.get(STORAGE_KEYS.analysisTasks);
const task = analysisTaskById(stored[STORAGE_KEYS.analysisTasks], taskId);
@@ -4686,21 +4806,24 @@ async function applyDetailTagOrganization(message) {
async function updateAiProviderConfiguration(message = {}) {
const current = await loadAiConfiguration();
const registry = mergeAiProviderRegistry(current.registry, message.registry);
- for (const [taskId, assignment] of Object.entries(message.assignments ?? {})) {
+ let assignments = normalizeAiTaskAssignments(message.assignments ?? current.assignments, registry);
+ if (message.connectionSelection) {
+ assignments = applyConnectionModelAssignments(assignments, message.connectionSelection, registry);
+ }
+ for (const [taskId, assignment] of Object.entries(assignments)) {
const requested = Number(assignment?.concurrency);
const limit = modelConcurrencyLimit(assignment?.providerId, assignment?.model, registry);
if (Number.isInteger(requested) && Number.isInteger(limit) && requested > limit) {
throw new Error(`${taskId} 的并发数 ${requested} 超过所选模型官方上限 ${limit}`);
}
}
- const assignments = normalizeAiTaskAssignments(message.assignments ?? current.assignments, registry);
const configuration = {
registry,
assignments,
preferences: normalizeAiPreferences(message.preferences ?? current.preferences)
};
await persistAiConfiguration(configuration);
- return aiConfigurationResponse(configuration, "AI 服务与七项默认任务已统一保存");
+ return aiConfigurationResponse(configuration, "AI 服务配置已保存");
}
async function verifyAiImageGenerationCredential(message = {}) {
@@ -4729,6 +4852,43 @@ async function verifyAiImageGenerationCredential(message = {}) {
};
}
+async function previewAiProviderModels(message = {}) {
+ const providerId = String(message.providerId ?? "").trim();
+ const current = await loadAiConfiguration();
+ const registry = mergeAiProviderRegistry(current.registry, message.registry);
+ const profile = registry.providers[providerId];
+ if (!profile) throw new Error("所选 AI 服务不存在");
+ let result;
+ try {
+ result = await aiProviderModule.discoverModels({
+ ...profile,
+ models: {
+ ...profile.models,
+ ...assignedModelsForProvider(current.assignments, providerId)
+ }
+ }, { etag: "" });
+ } catch (error) {
+ throw new Error(discoveryErrorMessage(error, profile.apiKey), { cause: error });
+ }
+ const previewRegistry = mergeAiProviderRegistry(registry, { providers: {
+ [providerId]: {
+ discoveredModels: result.models,
+ discovery: {
+ discoveredAt: result.discoveredAt,
+ source: result.source,
+ etag: result.cache?.etag,
+ cacheControl: result.cache?.cacheControl,
+ error: ""
+ }
+ }
+ } });
+ return {
+ ok: true,
+ message: `${profile.label} 已读取 ${result.models.length} 个模型;保存前请选择分析模型`,
+ provider: publicAiProviderRegistry(previewRegistry).providers[providerId]
+ };
+}
+
async function discoverAiProviderModels(providerIdValue, force = false) {
const providerId = String(providerIdValue ?? "").trim();
const configuration = await loadAiConfiguration();
@@ -5139,49 +5299,139 @@ async function analyzeEntryVisualSet(message) {
return { ok: true, message: "整组图片关系分析已保存", entry: updated, analysis };
}
-async function analyzeEntryVideo(message) {
- requireVideoAnalysisConfirmation(message.singleConfirmation);
+async function analyzeEntryVideoTaskAction(message) {
const state = await readState();
const entry = normalizeEntryMedia(findEntry(state, message.entryId));
const assetId = String(message.assetId ?? "").trim() || entry.primaryMediaId;
const asset = entry.mediaAssets.find((item) => item.id === assetId && item.kind === "video" && item.usage !== "poster");
if (!asset) return { ok: false, message: "没有找到要分析的视频" };
- const route = resolveVideoAnalysisTask(await loadAiConfiguration());
- const videoBlob = asset.storageMode === "managed" ? await getMediaBlob(asset.id) : null;
- if (asset.storageMode === "managed" && !videoBlob) return { ok: false, message: "本地视频文件缺失,无法分析" };
- const sourceUrl = asset.reference?.url || asset.sourceUrl;
+ const configuration = await loadAiConfiguration();
+ const route = resolveVideoAnalysisTask(configuration);
+ if (message.protocol && message.protocol !== route.protocol) {
+ return { ok: false, message: "视频分析请求协议已经变化,未发送" };
+ }
+ const requestRouteSnapshot = {
+ providerId: message.routeProviderId,
+ model: message.routeModel,
+ protocol: message.protocol,
+ endpoint: message.routeEndpoint,
+ localVideo: message.localVideo,
+ preferPublicVideoUrl: message.preferPublicVideoUrl,
+ publicVideoUrl: message.publicVideoUrl
+ };
+ if (message.hasRouteSnapshot === true && !videoAnalysisRouteMatches(requestRouteSnapshot, route)) {
+ return { ok: false, message: "视频分析服务的发送路径已经变化,未发送" };
+ }
+ const concurrency = configuration.assignments?.videoAnalysis?.concurrency;
const analyzeVideo = VIDEO_ANALYSIS_ADAPTERS[route.protocol];
if (!analyzeVideo) throw new Error(`${route.provider} 的视频理解请求协议当前版本尚未适配`);
- const analysis = await analyzeVideo({
- apiKey: route.apiKey,
- endpoint: route.endpoint,
- providerLabel: route.providerLabel,
- model: route.model,
- mode: message.mode,
- customQuestion: message.customQuestion,
- videoBlob,
- videoUrl: sourceUrl,
- referenceProvider: asset.reference?.provider,
- referencePlaybackMode: asset.reference?.playbackMode,
- localVideo: route.localVideo,
- preferPublicVideoUrl: route.preferPublicVideoUrl,
- publicVideoUrl: route.publicVideoUrl,
- onStage: (phase) => chrome.runtime.sendMessage({
- type: "VIDEO_ANALYSIS_CHANGED", entryId: entry.id, assetId: asset.id, phase,
- provider: route.provider, model: route.model
- }).catch(() => undefined)
- });
+ const sourceUrl = message.sourceKind === "local-video" ? "" : asset.reference?.url || asset.sourceUrl;
+ const preferPublicVideoUrl = message.sourceKind === "local-video" ? false : route.preferPublicVideoUrl;
+ const requestId = String(message.attemptId ?? "").trim();
+ if (!requestId || !await analysisTaskAttemptIsActive(message.taskId, requestId)) {
+ return { ok: false, message: "本次视频分析已经停止,未发送" };
+ }
+ const analysis = await scheduleAnalysis(
+ `${route.providerId}:${route.model}:videoAnalysis`,
+ concurrency,
+ async () => {
+ if (!await analysisTaskAttemptIsActive(message.taskId, requestId)) {
+ throw new DOMException("The operation was aborted", "AbortError");
+ }
+ const sendConfiguration = await loadAiConfiguration();
+ const sendRoute = resolveVideoAnalysisTask(sendConfiguration);
+ if (!videoAnalysisRouteMatches(route, sendRoute)
+ || (message.hasRouteSnapshot === true && !videoAnalysisRouteMatches(requestRouteSnapshot, sendRoute))) {
+ throw new Error("视频分析服务在排队期间已经变化,未发送");
+ }
+ if (!await analysisTaskAttemptIsActive(message.taskId, requestId)) {
+ throw new DOMException("The operation was aborted", "AbortError");
+ }
+ const videoBlob = asset.storageMode === "managed" ? await getMediaBlob(asset.id) : null;
+ if (asset.storageMode === "managed" && !videoBlob) throw new Error("本地视频文件缺失,无法分析");
+ const sourcePlan = sendRoute.protocol === "chat_completions"
+ ? chatCompletionsVideoSourcePlan({
+ providerLabel: sendRoute.providerLabel,
+ videoBlob,
+ videoUrl: sourceUrl,
+ videoMimeType: videoBlob?.type || asset.mimeType,
+ referenceProvider: asset.reference?.provider,
+ referencePlaybackMode: asset.reference?.playbackMode,
+ localVideo: sendRoute.localVideo,
+ preferPublicVideoUrl,
+ publicVideoUrl: sendRoute.publicVideoUrl
+ }).sourceKind
+ : videoBlob ? "local-video" : "public-video-url";
+ if (message.sourceKind && message.sourceKind !== sourcePlan) {
+ throw new Error("视频来源发送方式已经变化,未发送");
+ }
+ if (message.sourceFingerprint && (!videoBlob || await videoAssetFingerprint(videoBlob) !== message.sourceFingerprint)) {
+ throw new Error("视频文件在发送前已经变化,未发送");
+ }
+ if (!await analysisTaskAttemptIsActive(message.taskId, requestId)) {
+ throw new DOMException("The operation was aborted", "AbortError");
+ }
+ return analyzeVideo({
+ apiKey: sendRoute.apiKey,
+ endpoint: sendRoute.endpoint,
+ providerLabel: sendRoute.providerLabel,
+ model: sendRoute.model,
+ mode: message.mode,
+ customQuestion: message.mode === "custom" ? message.instruction : "",
+ instruction: message.instruction,
+ includeTags: message.includeTags,
+ requestId,
+ maxOutputTokens: sendRoute.maxOutputTokens,
+ catalog: state.facetCatalog,
+ locale: message.outputLocale,
+ durationMs: asset.durationMs,
+ width: asset.width,
+ height: asset.height,
+ videoBlob,
+ videoUrl: sourceUrl,
+ videoMimeType: asset.mimeType,
+ referenceProvider: asset.reference?.provider,
+ referencePlaybackMode: asset.reference?.playbackMode,
+ localVideo: sendRoute.localVideo,
+ preferPublicVideoUrl,
+ publicVideoUrl: sendRoute.publicVideoUrl,
+ signal: message.signal,
+ onStage: (phase) => chrome.runtime.sendMessage({
+ type: "VIDEO_ANALYSIS_CHANGED",
+ taskId: message.taskId,
+ attemptId: requestId,
+ entryId: entry.id,
+ assetId: asset.id,
+ phase,
+ provider: sendRoute.provider,
+ model: sendRoute.model
+ }).catch(() => undefined)
+ });
+ },
+ { priority: message.priority }
+ );
return await enqueue(async () => {
- const currentState = await readState();
+ if (!await analysisTaskAttemptIsActive(message.taskId, requestId)) {
+ return { ok: false, message: "本次视频分析已经停止,迟到结果未保存" };
+ }
+ let currentState = await readState();
const current = normalizeEntryMedia(findEntry(currentState, entry.id));
- if (!current.mediaAssets.some((item) => item.id === asset.id)) {
+ const currentAsset = current.mediaAssets.find((item) => item.id === asset.id && item.kind === "video");
+ if (!currentAsset) {
return { ok: false, message: "分析期间视频已被移除,本次结果没有写入" };
}
+ if (message.sourceFingerprint) {
+ const currentBlob = currentAsset.storageMode === "managed" ? await getMediaBlob(currentAsset.id) : null;
+ if (!currentBlob || await videoAssetFingerprint(currentBlob) !== message.sourceFingerprint) {
+ return { ok: false, message: "分析期间视频文件已经变化,本次结果没有写入" };
+ }
+ }
+ const mode = String(message.mode || "creative-breakdown");
+ const createdAt = new Date().toISOString();
const record = {
id: `video-analysis:${crypto.randomUUID()}`,
assetId: asset.id,
- text: analysis.text,
- mode: String(message.mode || "creative-breakdown"),
+ mode,
prompt: analysis.prompt,
sourceKind: analysis.sourceKind,
provider: analysis.provider,
@@ -5189,17 +5439,66 @@ async function analyzeEntryVideo(message) {
usage: analysis.usage,
cost: analysis.cost ?? null,
routing: analysis.routing ?? null,
- version: 1,
- createdAt: new Date().toISOString()
+ requestId,
+ ...(message.batchJobId ? { batchJobId: message.batchJobId } : {}),
+ version: current.videoAnalyses.filter((item) => item.assetId === asset.id && item.mode === mode).length + 1,
+ createdAt,
+ ...(mode === "visual-reconstruction" ? {
+ contractVersion: analysis.contractVersion,
+ reconstructionPrompt: analysis.reconstructionPrompt,
+ tags: analysis.tags,
+ uncertainties: analysis.uncertainties,
+ includeTags: analysis.includeTags,
+ analysisScope: analysis.analysisScope,
+ finishReason: analysis.finishReason,
+ userEdited: false
+ } : { text: analysis.text })
};
- const updated = normalizeEntryMedia({ ...current, videoAnalyses: [...current.videoAnalyses, record] });
+ let updated = normalizeEntryMedia({ ...current, videoAnalyses: [...current.videoAnalyses, record] });
+ if (mode === "visual-reconstruction" && analysis.includeTags) {
+ const entryIndex = currentState.entries.findIndex((item) => item.id === current.id);
+ const preservedAssignments = (updated.facetAssignments ?? []).filter((item) =>
+ item.source !== "vision_model" || item.visualId !== asset.id
+ );
+ currentState.entries[entryIndex] = { ...updated, facetAssignments: preservedAssignments };
+ const assignmentStart = preservedAssignments.length;
+ const applied = applyFixedAnalysisTags(currentState, current.id, analysis.tags, {
+ source: "vision_model",
+ maxTags: 8,
+ replaceExisting: false
+ });
+ currentState = applied.state;
+ updated = normalizeEntryMedia(currentState.entries[entryIndex]);
+ updated.facetAssignments = updated.facetAssignments.map((item, index) =>
+ index >= assignmentStart && item.source === "vision_model" ? { ...item, visualId: asset.id } : item
+ );
+ currentState.entries[entryIndex] = updated;
+ } else {
+ currentState.entries = currentState.entries.map((item) => item.id === current.id ? updated : item);
+ }
await commitLocalChanges({
- [STORAGE_KEYS.entries]: currentState.entries.map((item) => item.id === current.id ? updated : item)
+ [STORAGE_KEYS.entries]: currentState.entries,
+ [STORAGE_KEYS.facetCatalog]: currentState.facetCatalog
});
- return { ok: true, message: "视频分析已保存为新版本", entry: updated, analysis: record };
+ return {
+ ok: true,
+ message: mode === "visual-reconstruction" ? "AI 视觉逆推已保存" : "视频分析已保存为新版本",
+ entry: updated,
+ analysis: record,
+ result: { entryId: current.id, assetId: asset.id, analysisId: record.id }
+ };
});
}
+async function updateVideoReconstructionPrompt(message) {
+ const state = await readState();
+ const current = findEntry(state, message.entryId);
+ const updated = editCurrentVideoReconstruction(current, message.assetId, message.reconstructionPrompt);
+ const entries = state.entries.map((entry) => entry.id === current.id ? updated : entry);
+ await commitLocalChanges({ [STORAGE_KEYS.entries]: entries });
+ return { ok: true, message: "AI 视觉逆推提示词已保存", entry: updated, entries };
+}
+
async function updateVisionReconstructionPrompt(entryId, visualIdValue, reconstructionPrompt) {
const state = await readState();
const current = findEntry(state, entryId);
@@ -5839,6 +6138,21 @@ async function recoverDeepSeekBatch() {
if (!job || !job.items.some((item) => item.status === "running")) {
return { ok: true, analysisBatchJob: analysisBatchSummary(job) };
}
+ if (job.kind === "video") {
+ const recovered = structuredClone(job);
+ for (const item of recovered.items) {
+ if (item.status !== "running") continue;
+ item.status = "failed";
+ item.claimId = "";
+ item.error = "上次执行状态未知,未自动重试;服务商可能已收到请求";
+ item.statusCode = 0;
+ }
+ recovered.status = "paused";
+ recovered.updatedAt = new Date().toISOString();
+ await commitLocalChanges({ [STORAGE_KEYS.batchJob]: recovered }, { markSyncDirty: false });
+ await ensureAnalysisBatchAlarm(false);
+ return { ok: true, message: "视频批量任务已暂停,未知项未自动重发", visionBatchJob: analysisBatchSummary(recovered) };
+ }
const recovered = recoverInterruptedAnalysisBatch(job);
await commitLocalChanges({ [STORAGE_KEYS.batchJob]: recovered });
if (recovered.status === "running") {
@@ -5892,6 +6206,148 @@ async function previewVisionBatchTask(message) {
return { ok: true, preview };
}
+async function previewVideoBatchTask(message) {
+ const [state, configuration, stored] = await Promise.all([
+ readState(),
+ loadAiConfiguration(),
+ chrome.storage.local.get(STORAGE_KEYS.analysisTasks)
+ ]);
+ const route = resolveVideoAnalysisTask(configuration);
+ const selected = new Set((Array.isArray(message.entryIds) ? message.entryIds : []).map(String));
+ const busy = new Set(normalizeAnalysisTaskRegistry(stored[STORAGE_KEYS.analysisTasks]).items
+ .filter((task) => task.request?.kind === "entry_video" && ["queued", "running"].includes(task.status))
+ .map((task) => `${task.request.entryId}\u0000${task.request.assetId}`));
+ const sendableAssetIds = [];
+ const assetSnapshots = [];
+ const exclusions = [];
+ for (const entry of state.entries.filter((item) => selected.has(String(item.id)))) {
+ const videos = entryMediaAssets(entry).filter((item) => item.kind === "video" && item.usage !== "poster");
+ const primary = videos.find((asset) => asset.id === entry.primaryMediaId) || videos[0];
+ const candidates = message.includeAllVideos ? videos : [primary].filter(Boolean);
+ for (const asset of candidates) {
+ const exclude = (category, reason) => exclusions.push({ entryId: entry.id, assetId: asset.id, title: entry.title, category, reason });
+ if (asset.storageMode !== "managed") {
+ exclude("non_local", "批量视频分析只发送案例库中的本地视频");
+ continue;
+ }
+ if (busy.has(`${entry.id}\u0000${asset.id}`)) {
+ exclude("busy", "这支视频已有单项或批量分析任务在运行");
+ continue;
+ }
+ const blob = await getMediaBlob(asset.id);
+ if (!blob) {
+ exclude("missing", "本地视频文件缺失");
+ continue;
+ }
+ let sourcePlan = "local-video";
+ if (route.protocol === "chat_completions") {
+ try {
+ sourcePlan = chatCompletionsVideoSourcePlan({
+ providerLabel: route.providerLabel,
+ hasLocalVideo: true,
+ videoMimeType: blob.type || asset.mimeType,
+ localVideo: route.localVideo,
+ preferPublicVideoUrl: route.preferPublicVideoUrl,
+ publicVideoUrl: route.publicVideoUrl
+ }).sourceKind;
+ } catch (error) {
+ exclude("incompatible", userMessage(error));
+ continue;
+ }
+ }
+ sendableAssetIds.push(asset.id);
+ assetSnapshots.push({
+ entryId: entry.id,
+ assetId: asset.id,
+ byteSize: blob.size,
+ durationMs: asset.durationMs,
+ fingerprint: await videoAssetFingerprint(blob),
+ sourcePlan
+ });
+ }
+ }
+ const preview = previewVideoBatch(state.entries, {
+ entryIds: message.entryIds,
+ includeAllVideos: message.includeAllVideos,
+ reanalyze: message.reanalyze,
+ includeTags: message.includeTags,
+ providerId: route.providerId,
+ model: route.model,
+ protocol: route.protocol,
+ sourcePlan: new Set(assetSnapshots.map((item) => item.sourcePlan)).size === 1 ? assetSnapshots[0]?.sourcePlan || "" : "per-asset",
+ endpoint: route.endpoint,
+ localVideo: route.localVideo,
+ preferPublicVideoUrl: route.preferPublicVideoUrl,
+ publicVideoUrl: route.publicVideoUrl,
+ concurrency: configuration.assignments.videoAnalysis.concurrency,
+ sendableAssetIds,
+ assetSnapshots
+ });
+ preview.exclusions = exclusions;
+ preview.exclusionCounts = exclusions.reduce((counts, item) => {
+ counts[item.category] = (counts[item.category] || 0) + 1;
+ return counts;
+ }, { busy: 0, missing: 0, incompatible: 0, non_local: 0 });
+ return { ok: true, preview };
+}
+
+async function videoAssetFingerprint(blob) {
+ if (!(blob instanceof Blob) || !blob.size) throw new Error("本地视频文件缺失");
+ return await chunkedBlobFingerprint(blob);
+}
+
+async function createVideoBatchTask(message) {
+ const state = await readState();
+ const stored = await chrome.storage.local.get(STORAGE_KEYS.batchJob);
+ const previous = normalizeAnalysisBatchJob(stored[STORAGE_KEYS.batchJob]);
+ if (previous && ["running", "paused"].includes(previous.status)
+ && previous.items.some((item) => ["pending", "running"].includes(item.status))) {
+ return { ok: false, message: "已有未完成的批量任务,请继续或取消后再新建" };
+ }
+ const previewResponse = await previewVideoBatchTask(message);
+ const preview = previewResponse.preview;
+ if (preview.excludedCount && message.confirmExclusions !== true) {
+ return { ok: false, requiresExclusionConfirmation: true, preview, message: "部分视频不会发送,请确认排除项后继续" };
+ }
+ if (!String(message.instruction ?? "").trim()) return { ok: false, message: "批量视频逆推指令不能为空" };
+ const job = createVideoBatchJob(state.entries, {
+ entryIds: message.entryIds,
+ includeAllVideos: message.includeAllVideos,
+ reanalyze: message.reanalyze,
+ includeTags: message.includeTags,
+ providerId: preview.providerId,
+ model: preview.model,
+ protocol: preview.protocol,
+ sourcePlan: preview.sourcePlan,
+ endpoint: preview.endpoint,
+ localVideo: preview.localVideo,
+ preferPublicVideoUrl: preview.preferPublicVideoUrl,
+ publicVideoUrl: preview.publicVideoUrl,
+ concurrency: preview.concurrency,
+ sendableAssetIds: preview.items.map((item) => item.assetId),
+ assetSnapshots: preview.items.map((item) => ({
+ entryId: item.entryId,
+ assetId: item.assetId,
+ byteSize: item.byteSize,
+ durationMs: item.durationMs,
+ fingerprint: item.fingerprint,
+ sourcePlan: item.sourcePlan
+ })),
+ instruction: message.instruction,
+ contractVersion: VIDEO_RECONSTRUCTION_CONTRACT_VERSION,
+ outputLocale: message.outputLocale
+ });
+ await commitLocalChanges({ [STORAGE_KEYS.batchJob]: job });
+ await chrome.storage.local.remove(STORAGE_KEYS.analysisBatchUndo);
+ await ensureAnalysisBatchAlarm(true);
+ scheduleAnalysisBatchRunner();
+ return {
+ ok: true,
+ message: `已创建 ${job.requestCount} 支视频逆推任务`,
+ visionBatchJob: analysisBatchSummary(job)
+ };
+}
+
async function createVisionBatchTask(message) {
const state = await readState();
const stored = await chrome.storage.local.get(STORAGE_KEYS.batchJob);
@@ -5991,17 +6447,40 @@ async function failVisionBatchItem(message) {
return { ok: true, visionBatchJob: analysisBatchSummary(failed) };
}
-async function updateVisionBatch(action, jobId) {
- const stored = await chrome.storage.local.get(STORAGE_KEYS.batchJob);
- const current = requireAnalysisBatch(stored[STORAGE_KEYS.batchJob], jobId, "vision");
+async function updateVisionBatch(action, jobId, options = {}) {
+ const stored = await chrome.storage.local.get([STORAGE_KEYS.batchJob, STORAGE_KEYS.analysisTasks]);
+ const current = normalizeAnalysisBatchJob(stored[STORAGE_KEYS.batchJob]);
+ if (!current || current.id !== jobId || !["vision", "video"].includes(current.kind)) throw new Error("批量任务已经变化,请刷新后重试");
const actions = {
pause: pauseAnalysisBatch,
resume: resumeAnalysisBatch,
cancel: cancelAnalysisBatch,
retry: retryFailedAnalysisItems
};
- const next = actions[action](current);
- await commitLocalChanges({ [STORAGE_KEYS.batchJob]: next });
+ const next = action === "retry" && current.kind === "video"
+ ? retryFailedAnalysisItems(current, {
+ itemKeys: options.itemKeys,
+ requireSelection: true,
+ confirmDuplicateCharge: options.confirmDuplicateCharge
+ })
+ : actions[action](current);
+ const changes = { [STORAGE_KEYS.batchJob]: next };
+ if (current.kind === "video" && action === "cancel") {
+ const taskState = normalizeAnalysisTaskRegistry(stored[STORAGE_KEYS.analysisTasks]);
+ taskState.items = taskState.items.map((task) => task.request?.kind === "entry_video"
+ && task.request.batchJobId === current.id && ["queued", "running"].includes(task.status)
+ ? stopAnalysisTask(task)
+ : task);
+ changes[STORAGE_KEYS.analysisTasks] = taskState;
+ }
+ await commitLocalChanges(changes, { markSyncDirty: false });
+ if (current.kind === "video" && action === "cancel") {
+ for (const task of normalizeAnalysisTaskRegistry(changes[STORAGE_KEYS.analysisTasks]).items.filter((item) =>
+ item.request?.batchJobId === current.id && item.status === "stopped")) {
+ analysisTaskRunners.get(task.id)?.controller.abort();
+ await notifyAnalysisTaskUpdated(task);
+ }
+ }
if (next.status === "running") {
await ensureAnalysisBatchAlarm(true);
scheduleAnalysisBatchRunner();
@@ -6010,7 +6489,9 @@ async function updateVisionBatch(action, jobId) {
}
return {
ok: true,
- message: ({ pause: "批量画面分析已暂停", resume: "批量画面分析已继续", cancel: "批量画面分析已取消", retry: "失败图片已重新加入队列" })[action],
+ message: current.kind === "video"
+ ? ({ pause: "批量视频逆推已暂停", resume: "批量视频逆推已继续", cancel: "批量视频逆推已取消;已完成结果仍保留", retry: "失败视频已重新加入队列" })[action]
+ : ({ pause: "批量画面分析已暂停", resume: "批量画面分析已继续", cancel: "批量画面分析已取消", retry: "失败图片已重新加入队列" })[action],
visionBatchJob: analysisBatchSummary(next)
};
}
@@ -6036,6 +6517,8 @@ async function runPersistedAnalysisBatch() {
}
if (initial.kind === "vision") {
continueRunning = await runPersistedVisionBatchSlice(initial.id);
+ } else if (initial.kind === "video") {
+ continueRunning = await runPersistedVideoBatchSlice(initial.id);
} else {
continueRunning = await runPersistedTextBatchSlice(initial.id);
}
@@ -6045,7 +6528,7 @@ async function runPersistedAnalysisBatch() {
const stored = await chrome.storage.local.get(STORAGE_KEYS.batchJob).catch(() => ({}));
const current = normalizeAnalysisBatchJob(stored[STORAGE_KEYS.batchJob]);
if (current?.status === "running") {
- const taskLabel = current.kind === "vision" ? "图片服务" : "文字服务";
+ const taskLabel = current.kind === "vision" ? "图片服务" : current.kind === "video" ? "视频服务" : "文字服务";
const failed = failUnfinishedAnalysisItems(current, {
message: `任务启动失败:${taskLabel}配置或后台运行状态无效,请检查 AI 服务后重试`,
status: 500
@@ -6233,6 +6716,117 @@ async function runPersistedVisionBatchSlice(jobId) {
return normalizeAnalysisBatchJob(stored[STORAGE_KEYS.batchJob])?.status === "running";
}
+async function runPersistedVideoBatchSlice(jobId) {
+ const configuration = await loadAiConfiguration();
+ const prepared = await enqueue(async () => {
+ const stored = await chrome.storage.local.get(STORAGE_KEYS.batchJob);
+ let job = requireAnalysisBatch(stored[STORAGE_KEYS.batchJob], jobId, "video");
+ if (job.status !== "running") return { job, claims: [] };
+ let route;
+ try {
+ route = resolveVideoAnalysisTask(configuration);
+ } catch {
+ job = pauseAnalysisBatch(job);
+ await commitLocalChanges({ [STORAGE_KEYS.batchJob]: job });
+ return { job, claims: [] };
+ }
+ if (!videoAnalysisRouteMatches(job, route)) {
+ job = pauseAnalysisBatch(job);
+ await commitLocalChanges({ [STORAGE_KEYS.batchJob]: job });
+ return { job, claims: [] };
+ }
+ const available = Math.max(0, job.concurrency - job.items.filter((item) => item.status === "running").length);
+ if (!available) return { job, claims: [] };
+ const claimed = claimAnalysisItems(job, available);
+ await commitLocalChanges({ [STORAGE_KEYS.batchJob]: claimed.job });
+ return { job: claimed.job, claims: claimed.claims };
+ });
+ for (const claim of prepared.claims) {
+ try {
+ const response = await startOrJoinAnalysisTaskAction({
+ kind: "entry_video",
+ entryId: claim.entryId,
+ assetId: claim.assetId,
+ mode: "visual-reconstruction",
+ instruction: prepared.job.instruction,
+ includeTags: prepared.job.includeTags,
+ batchJobId: prepared.job.id,
+ batchClaimId: claim.claimId,
+ sourceFingerprint: claim.fingerprint,
+ protocol: prepared.job.protocol,
+ sourceKind: claim.sourcePlan,
+ routeEndpoint: prepared.job.endpoint,
+ routeProviderId: prepared.job.providerId,
+ routeModel: prepared.job.model,
+ localVideo: prepared.job.localVideo,
+ preferPublicVideoUrl: prepared.job.preferPublicVideoUrl,
+ publicVideoUrl: prepared.job.publicVideoUrl,
+ hasRouteSnapshot: true,
+ outputLocale: prepared.job.outputLocale,
+ priority: "user_batch",
+ consumerId: `video-batch:${prepared.job.id}`,
+ clientRequestId: `video-batch:${prepared.job.id}:${claim.entryId}:${claim.assetId}:${claim.claimId}`
+ });
+ if (response.task?.request?.batchJobId !== prepared.job.id) {
+ const error = new Error("这支视频已有单项分析任务,未重复发送;完成后可重新预览批量分析");
+ error.status = 409;
+ throw error;
+ }
+ } catch (error) {
+ await failVideoBatchClaim(prepared.job.id, claim, error);
+ }
+ }
+ const stored = await chrome.storage.local.get(STORAGE_KEYS.batchJob);
+ const current = normalizeAnalysisBatchJob(stored[STORAGE_KEYS.batchJob]);
+ return Boolean(current?.status === "running" && current.items.some((item) => item.status === "pending"));
+}
+
+async function settleVideoBatchTask(task, actionResult) {
+ const stored = await chrome.storage.local.get(STORAGE_KEYS.batchJob);
+ const job = normalizeAnalysisBatchJob(stored[STORAGE_KEYS.batchJob]);
+ if (!job || job.kind !== "video" || job.id !== task.request.batchJobId || job.status === "canceled") return;
+ const item = job.items.find((candidate) => candidate.entryId === task.request.entryId
+ && candidate.assetId === task.request.assetId
+ && candidate.claimId === task.request.batchClaimId
+ && candidate.taskId === task.id
+ && candidate.requestId === task.activeAttemptId
+ && candidate.status === "running");
+ if (!item) return;
+ let next;
+ if (actionResult?.ok) {
+ const usage = actionResult.analysis?.usage ?? actionResult.usage ?? {};
+ next = succeedAnalysisItem(job, item.entryId, item.claimId, {
+ promptTokens: usage.inputTokens,
+ completionTokens: usage.outputTokens,
+ totalTokens: usage.totalTokens
+ }, undefined, { serviceRequests: 1, cost: actionResult.analysis?.cost ?? null });
+ } else {
+ next = failAnalysisItem(job, item.entryId, item.claimId, {
+ message: actionResult?.message || "视频分析失败",
+ status: actionResult?.status,
+ usage: actionResult?.usage,
+ attempts: { serviceRequests: 1 }
+ });
+ if ([429, 500, 502, 503, 504].includes(Number(actionResult?.status))) next = pauseAnalysisBatch(next);
+ }
+ await commitLocalChanges({ [STORAGE_KEYS.batchJob]: next });
+ if (next.status === "running") {
+ await ensureAnalysisBatchAlarm(true);
+ scheduleAnalysisBatchRunner();
+ } else await ensureAnalysisBatchAlarm(false);
+}
+
+async function failVideoBatchClaim(jobId, claim, error) {
+ const stored = await chrome.storage.local.get(STORAGE_KEYS.batchJob);
+ const job = requireAnalysisBatch(stored[STORAGE_KEYS.batchJob], jobId, "video");
+ const next = failAnalysisItem(job, claim.entryId, claim.claimId, {
+ message: userMessage(error),
+ status: Number(error?.status) || 0,
+ attempts: { serviceRequests: 0 }
+ });
+ await commitLocalChanges({ [STORAGE_KEYS.batchJob]: next });
+}
+
async function ensureAnalysisBatchAlarm(running) {
if (running) await chrome.alarms.create(ANALYSIS_BATCH_ALARM, { periodInMinutes: 1 });
else await chrome.alarms.clear(ANALYSIS_BATCH_ALARM);
diff --git a/composer-page.js b/composer-page.js
index 625bcfc..ede3f0f 100644
--- a/composer-page.js
+++ b/composer-page.js
@@ -7,6 +7,7 @@ import {
composerProfileForTaskAssignment,
createComposerSession,
createReferenceSnapshots,
+ referenceSourcePartsForAsset,
imageReferenceModeAvailability,
isMeaningfulComposerSession,
isComposerEligibleEntry,
@@ -140,6 +141,7 @@ const composerAnalysisTaskBridge = createComposerAnalysisTaskBridge({
});
let referenceDraftSelections = new Map();
let referenceDraftOrder = [];
+let referenceDraftSourceSelections = new Map();
let referencePreviewAssetIds = new Map();
let workspaceMode = "references";
let feedbackTimer = 0;
@@ -388,7 +390,7 @@ async function initializeComposer() {
? [{ entryId: requestedIds[0], assetIds: [requestedAssetId] }]
: requestedIds.map((entryId) => {
const entry = entries.find((item) => item.id === entryId);
- const assets = selectableReferenceImages(entry);
+ const assets = selectableReferenceAssets(entry, requestedType);
const primary = assets.find((asset) => asset.id === entry?.primaryMediaId) ?? assets[0];
return { entryId, assetIds: primary ? [primary.id] : [] };
});
@@ -1927,6 +1929,7 @@ function openReferenceWorkspace() {
clearComposerFeedback();
referenceDraftSelections = new Map();
referenceDraftOrder = [];
+ referenceDraftSourceSelections = new Map();
referencePreviewAssetIds = new Map();
for (const item of (composerSession?.referenceSnapshots ?? []).filter((reference) => reference.sourceType !== TEMP_REFERENCE_SOURCE_TYPES.temporary)) {
const assetIds = [item.assetId, ...(item.imageRefs ?? []).map((imageRef) => imageRef.visualId)].filter(Boolean);
@@ -1936,6 +1939,9 @@ function openReferenceWorkspace() {
if (assetIds.length) assetIds.forEach((assetId) => addDraftOrder(item.entryId, assetId));
else addDraftOrder(item.entryId, "");
if (assetIds[0]) referencePreviewAssetIds.set(item.entryId, assetIds[0]);
+ if (item.assetId && item.referenceSources?.length) {
+ referenceDraftSourceSelections.set(draftOrderKey(item.entryId, item.assetId), new Set(item.referenceSources.map((source) => source.id)));
+ }
}
elements.composerReferenceWorkspace.hidden = false;
elements.composerReferenceWorkspace.inert = false;
@@ -2056,38 +2062,43 @@ function renderCasePicker() {
function createCaseOption(entry) {
const option = el("article", "composer-case-option");
option.dataset.entryId = entry.id;
- const imageAssets = selectableReferenceImages(entry);
- const primaryAsset = imageAssets.find((asset) => asset.id === entry.primaryMediaId) ?? imageAssets[0];
- const previewAsset = imageAssets.find((asset) => asset.id === referencePreviewAssetIds.get(entry.id)) ?? primaryAsset;
+ const referenceAssets = selectableReferenceAssets(entry, composerSession?.targetType);
+ const primaryAsset = referenceAssets.find((asset) => asset.id === entry.primaryMediaId) ?? referenceAssets[0];
+ const previewAsset = referenceAssets.find((asset) => asset.id === referencePreviewAssetIds.get(entry.id)) ?? primaryAsset;
if (previewAsset) referencePreviewAssetIds.set(entry.id, previewAsset.id);
option.dataset.selected = String(referenceDraftSelections.has(entry.id));
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.className = "composer-case-preview-checkbox";
- checkbox.setAttribute("aria-label", `选择当前预览图片:${entry.title || t("未命名案例")}`);
+ checkbox.setAttribute("aria-label", `选择当前预览素材:${entry.title || t("未命名案例")}`);
checkbox.checked = Boolean(previewAsset && referenceDraftSelections.get(entry.id)?.has(previewAsset.id));
checkbox.addEventListener("change", () => {
const assetId = referencePreviewAssetIds.get(entry.id);
if (!assetId) return;
setDraftAssetSelected(entry.id, assetId, checkbox.checked);
- syncCaseOptionSelection(option, entry);
+ const asset = referenceAssets.find((item) => item.id === assetId);
+ if (asset?.kind === "video") renderCasePicker();
+ else syncCaseOptionSelection(option, entry);
renderReferenceSelection();
});
const visual = el("div", "composer-case-visual");
const selectPreview = el("button", "composer-case-select-preview");
selectPreview.type = "button";
- selectPreview.setAttribute("aria-label", "选择或取消当前预览图片");
+ selectPreview.setAttribute("aria-label", "选择或取消当前预览素材");
selectPreview.addEventListener("click", () => {
const assetId = referencePreviewAssetIds.get(entry.id);
if (!assetId) return;
const selected = referenceDraftSelections.get(entry.id)?.has(assetId) === true;
setDraftAssetSelected(entry.id, assetId, !selected);
- syncCaseOptionSelection(option, entry);
+ const asset = referenceAssets.find((item) => item.id === assetId);
+ if (asset?.kind === "video") renderCasePicker();
+ else syncCaseOptionSelection(option, entry);
renderReferenceSelection();
});
- const inspect = textEl("button", "composer-case-inspect", "查看原图");
+ const inspect = textEl("button", "composer-case-inspect", previewAsset?.kind === "video" ? "查看封面" : "查看原图");
inspect.type = "button";
- inspect.addEventListener("click", () => openReferenceImagePreview(entry, referencePreviewAssetIds.get(entry.id)));
+ inspect.disabled = previewAsset?.kind === "video" && !posterAssetForVideo(entry, previewAsset);
+ inspect.addEventListener("click", () => openReferenceAssetPreview(entry, referencePreviewAssetIds.get(entry.id)));
visual.append(selectPreview, inspect);
option.append(checkbox, visual);
renderCasePreviewImage(option, entry, previewAsset?.id || "");
@@ -2099,39 +2110,90 @@ function createCaseOption(entry) {
const preview = entry.text || primaryVisionDescription(entry);
copy.append(rawTextEl("strong", "", entry.title || t("未命名案例")), rawTextEl("small", "", type), rawTextEl("p", "", excerpt(preview, 240)));
option.append(copy);
- if (imageAssets.length > 1) {
+ if (referenceAssets.length > 1) {
const assetPicker = el("div", "composer-case-assets");
- assetPicker.setAttribute("aria-label", `${entry.title || t("未命名案例")}的图片`);
- for (const [index, asset] of imageAssets.entries()) {
+ assetPicker.setAttribute("aria-label", `${entry.title || t("未命名案例")}的素材`);
+ for (const [index, asset] of referenceAssets.entries()) {
const assetOption = el("label", "composer-case-asset");
assetOption.dataset.assetId = asset.id;
const assetCheckbox = document.createElement("input");
assetCheckbox.type = "checkbox";
assetCheckbox.checked = referenceDraftSelections.get(entry.id)?.has(asset.id) === true;
- assetCheckbox.setAttribute("aria-label", `选择第 ${index + 1} 张图片`);
- const image = document.createElement("img");
- image.alt = "";
- image.loading = "lazy";
- image.decoding = "async";
- image.dataset.visualId = asset.id;
- const cached = thumbnailUrls.get(asset.id);
- if (cached) image.src = cached;
- else imageObserver.observe(image);
+ assetCheckbox.setAttribute("aria-label", asset.kind === "image"
+ ? `选择第 ${index + 1} 张图片`
+ : `选择第 ${index + 1} 支视频`);
+ const displayAsset = asset.kind === "video" ? posterAssetForVideo(entry, asset) : asset;
+ const thumbnail = displayAsset ? document.createElement("img") : rawTextEl("span", "", t("视频"));
+ if (displayAsset) {
+ thumbnail.alt = "";
+ thumbnail.loading = "lazy";
+ thumbnail.decoding = "async";
+ thumbnail.dataset.visualId = displayAsset.id;
+ const cached = thumbnailUrls.get(displayAsset.id);
+ if (cached) thumbnail.src = cached;
+ else imageObserver.observe(thumbnail);
+ }
assetCheckbox.addEventListener("change", () => {
referencePreviewAssetIds.set(entry.id, asset.id);
setDraftAssetSelected(entry.id, asset.id, assetCheckbox.checked);
- renderCasePreviewImage(option, entry, asset.id);
- syncCaseOptionSelection(option, entry);
+ if (asset.kind === "video") renderCasePicker();
+ else {
+ renderCasePreviewImage(option, entry, asset.id);
+ syncCaseOptionSelection(option, entry);
+ }
renderReferenceSelection();
});
- assetOption.append(assetCheckbox, image, rawTextEl("span", "", String(index + 1)));
+ assetOption.append(assetCheckbox, thumbnail, rawTextEl("span", "", asset.kind === "video" ? t("视频") : String(index + 1)));
assetPicker.append(assetOption);
}
option.append(assetPicker);
}
+ const sourceOptions = referenceAssets.flatMap((asset) => asset.kind === "video" && referenceDraftSelections.get(entry.id)?.has(asset.id)
+ ? composerVideoSourceOptions(entry, asset) : []);
+ if (sourceOptions.length) {
+ const sourcePicker = el("div", "composer-case-assets");
+ sourcePicker.setAttribute("aria-label", `${entry.title || t("未命名案例")}的视频文字来源`);
+ sourcePicker.append(...sourceOptions);
+ option.append(sourcePicker);
+ }
return option;
}
+function composerVideoSourceOptions(entry, asset) {
+ const key = draftOrderKey(entry.id, asset.id);
+ const analysisIds = (entry.videoAnalyses ?? [])
+ .filter((analysis) => String(analysis?.assetId ?? "") === asset.id)
+ .map((analysis) => String(analysis?.id ?? "")).filter(Boolean);
+ const parts = referenceSourcePartsForAsset(entry, asset.id, { analysisIds });
+ const defaultPartIds = new Set(referenceSourcePartsForAsset(entry, asset.id).map((source) => source.id));
+ let selected = referenceDraftSourceSelections.get(key);
+ if (!selected) {
+ selected = defaultPartIds;
+ referenceDraftSourceSelections.set(key, selected);
+ }
+ return parts.map((part) => {
+ const option = el("label", "composer-case-asset");
+ const input = document.createElement("input");
+ input.type = "checkbox";
+ input.checked = selected.has(part.id);
+ const short = part.kind === "original_prompt" && [...part.text.replace(/\s+/g, "")].length <= 3;
+ const labelText = part.kind === "other_analysis"
+ ? `${part.label} · ${excerpt(part.text, 24)}`
+ : short ? `${part.label} · ${t("短内容")}` : part.label;
+ const label = rawTextEl("span", "", labelText);
+ input.addEventListener("change", () => {
+ input.checked ? selected.add(part.id) : selected.delete(part.id);
+ if (!selected.size) setDraftAssetSelected(entry.id, asset.id, false);
+ renderCasePicker();
+ renderReferenceSelection();
+ });
+ option.dataset.assetId = asset.id;
+ option.title = part.text;
+ option.append(input, label);
+ return option;
+ });
+}
+
function setDraftAssetSelected(entryId, assetId, selected) {
if (!entryId || !assetId) return;
const selection = referenceDraftSelections.get(entryId) ?? new Set();
@@ -2139,25 +2201,33 @@ function setDraftAssetSelected(entryId, assetId, selected) {
selection.add(assetId);
referenceDraftSelections.set(entryId, selection);
addDraftOrder(entryId, assetId);
+ const entry = entries.find((item) => item.id === entryId);
+ const asset = selectableReferenceAssets(entry, composerSession?.targetType).find((item) => item.id === assetId);
+ if (asset?.kind === "video") {
+ referenceDraftSourceSelections.set(draftOrderKey(entryId, assetId), new Set(referenceSourcePartsForAsset(entry, assetId).map((source) => source.id)));
+ }
return;
}
selection.delete(assetId);
removeDraftOrder(entryId, assetId);
+ referenceDraftSourceSelections.delete(draftOrderKey(entryId, assetId));
if (!selection.size) referenceDraftSelections.delete(entryId);
}
function renderCasePreviewImage(option, entry, assetId) {
const surface = option.querySelector(".composer-case-select-preview");
if (!surface) return;
- const asset = selectableReferenceImages(entry).find((item) => item.id === assetId);
+ const asset = selectableReferenceAssets(entry, composerSession?.targetType).find((item) => item.id === assetId);
if (!asset) return surface.replaceChildren(rawTextEl("span", "", t("这条案例没有截图")));
+ const displayAsset = asset.kind === "video" ? posterAssetForVideo(entry, asset) : asset;
+ if (!displayAsset) return surface.replaceChildren(rawTextEl("span", "", t("视频")));
const image = document.createElement("img");
image.className = "composer-case-image";
image.alt = translateUiMessage(`${entry.title || t("未命名案例")} 对应画面`);
image.loading = "lazy";
image.decoding = "async";
- image.dataset.visualId = asset.id;
- const cached = thumbnailUrls.get(asset.id);
+ image.dataset.visualId = displayAsset.id;
+ const cached = thumbnailUrls.get(displayAsset.id);
if (cached) image.src = cached;
else imageObserver.observe(image);
surface.replaceChildren(image);
@@ -2181,10 +2251,12 @@ function syncRenderedReferenceCards() {
}
}
-async function openReferenceImagePreview(entry, assetId) {
+async function openReferenceAssetPreview(entry, assetId) {
if (!assetId) return;
try {
- const blob = await getScreenshotBlob(assetId);
+ const asset = selectableReferenceAssets(entry, composerSession?.targetType).find((item) => item.id === assetId);
+ const displayAsset = asset?.kind === "video" ? posterAssetForVideo(entry, asset) : asset;
+ const blob = displayAsset ? await getScreenshotBlob(displayAsset.id) : null;
if (!blob) throw new Error("无法读取原图");
const url = URL.createObjectURL(blob);
const dialog = el("dialog", "composer-reference-preview-dialog");
@@ -2208,8 +2280,9 @@ async function openReferenceImagePreview(entry, assetId) {
}
}
-function selectableReferenceImages(entry) {
- return entryMediaAssets(entry).filter((asset) => asset.kind === "image" && asset.usage !== "poster");
+function selectableReferenceAssets(entry, targetType = "") {
+ return entryMediaAssets(entry).filter((asset) => asset.usage !== "poster" &&
+ (asset.kind === "image" || targetType === "video" && asset.kind === "video"));
}
function draftOrderKey(entryId, assetId = "") {
@@ -2229,6 +2302,9 @@ function removeDraftOrder(entryId, assetId = "") {
function removeDraftEntry(entryId) {
referenceDraftSelections.delete(entryId);
referenceDraftOrder = referenceDraftOrder.filter((item) => !item.startsWith(`${entryId}\u0000`));
+ for (const key of referenceDraftSourceSelections.keys()) {
+ if (key.startsWith(`${entryId}\u0000`)) referenceDraftSourceSelections.delete(key);
+ }
}
function orderedDraftItems() {
@@ -2271,10 +2347,11 @@ function renderReferenceSelection() {
}
elements.composerSelectionStrip.replaceChildren(...selectedItems.slice(0, 8).map((item, index) => {
const entry = entries.find((candidate) => candidate.id === item.entryId);
- const assets = selectableReferenceImages(entry);
+ const assets = selectableReferenceAssets(entry, composerSession?.targetType);
const assetIndex = item.assetId ? assets.findIndex((asset) => asset.id === item.assetId) : -1;
+ const asset = assetIndex >= 0 ? assets[assetIndex] : null;
const chip = el("span", "composer-selection-chip");
- chip.append(rawTextEl("b", "", `${entry?.title || t("未命名案例")}${assetIndex >= 0 ? ` · 图${assetIndex + 1}` : ""}`));
+ chip.append(rawTextEl("b", "", `${entry?.title || t("未命名案例")}${assetIndex >= 0 ? asset?.kind === "video" ? " · 视频" : ` · 图${assetIndex + 1}` : ""}`));
const moveLeft = textEl("button", "", "←");
moveLeft.type = "button";
moveLeft.disabled = index === 0;
@@ -2294,6 +2371,7 @@ function renderReferenceSelection() {
else referenceDraftSelections.delete(item.entryId);
if (selection && !selection.size) referenceDraftSelections.delete(item.entryId);
removeDraftOrder(item.entryId, item.assetId);
+ referenceDraftSourceSelections.delete(item.key);
syncRenderedReferenceCards();
renderReferenceSelection();
});
@@ -2304,7 +2382,10 @@ function renderReferenceSelection() {
}
function draftReferenceLimitState(items = orderedDraftItems()) {
- const imageCount = items.filter((item) => item.assetId).length;
+ const imageCount = items.filter((item) => {
+ const entry = entries.find((candidate) => candidate.id === item.entryId);
+ return selectableReferenceAssets(entry, composerSession?.targetType).some((asset) => asset.id === item.assetId && asset.kind === "image");
+ }).length;
if (composerSession?.outputMode !== "create_image" || composerSession.imageReferenceMode !== "conditioned") {
return { imageCount, maximum: null, exceeded: false };
}
@@ -2341,7 +2422,16 @@ async function applySelectedReferences() {
composerSession = createComposerSession({
...composerSession,
referenceSnapshots: [
- ...createReferenceSnapshots(entries, selected.map((item) => ({ entryId: item.entryId, assetIds: item.assetId ? [item.assetId] : [] })), currentLocale(), composerSession.targetType),
+ ...createReferenceSnapshots(entries, selected.map((item) => ({
+ entryId: item.entryId,
+ assetIds: item.assetId ? [item.assetId] : [],
+ sourceIds: item.assetId ? [...(referenceDraftSourceSelections.get(item.key) ?? [])] : null,
+ analysisIds: item.assetId
+ ? (entries.find((entry) => entry.id === item.entryId)?.videoAnalyses ?? [])
+ .filter((analysis) => referenceDraftSourceSelections.get(item.key)?.has(`analysis:${String(analysis?.id ?? "")}`))
+ .map((analysis) => String(analysis.id))
+ : []
+ })), currentLocale(), composerSession.targetType),
...temporaryReferences
],
currentInstruction: "",
diff --git a/composer-service.js b/composer-service.js
index 188ac32..abe7649 100644
--- a/composer-service.js
+++ b/composer-service.js
@@ -150,6 +150,7 @@ export function composerServiceCatalog(aiSettingsValue = {}, visionSettingsValue
export function composerServiceCapabilities(profileValue, visionSettingsValue = {}) {
const profile = normalizeComposerAiProfile(profileValue);
+ if (profile.serviceId === "unassigned") return { serviceId: "unassigned", model: "", image: null, video: null };
const settings = normalizeVisionSettings(visionSettingsValue);
const xai = normalizeXaiComposerSettings(visionSettingsValue?.xai);
if (profile.serviceId === "deepseek") return { serviceId: "deepseek", model: profile.model, image: null, video: null };
@@ -419,6 +420,20 @@ export function composerImageEditCapabilities(profileValue, visionSettingsValue
export function selectedComposerService(profileValue, aiSettingsValue, visionSettingsValue) {
const profile = normalizeComposerAiProfile(profileValue);
+ if (profile.serviceId === "unassigned") {
+ return {
+ serviceId: "unassigned",
+ model: "",
+ label: "未选择模型",
+ shortLabel: "未选择模型",
+ configured: false,
+ vision: false,
+ planning: false,
+ reasoning: false,
+ imageGeneration: false,
+ videoGeneration: false
+ };
+ }
const catalog = composerServiceCatalog(aiSettingsValue, visionSettingsValue);
return catalog.find((item) => item.serviceId === profile.serviceId && item.model === profile.model)
?? catalog.find((item) => item.serviceId === profile.serviceId)
@@ -427,6 +442,9 @@ export function selectedComposerService(profileValue, aiSettingsValue, visionSet
export async function planComposerTurnWithService(input, settingsValue, options = {}) {
const profile = normalizeComposerAiProfile(input.session?.aiProfile);
+ if (profile.serviceId === "unassigned") {
+ throw new ComposerServiceError("创作规划未配置,请在 AI 服务的任务路由中选择模型", 422, { retryable: false });
+ }
if (profile.serviceId === "deepseek") return planDeepSeekTurn(input, settingsValue.ai, options);
const service = requireVisualService(profile, settingsValue.vision, "规划");
if (service.planning === false) {
@@ -1096,7 +1114,10 @@ function executionRequest(input) {
references: (input.session?.referenceSnapshots ?? []).map((item) => ({
alias: item.alias,
referenceKind: item.referenceKind,
- referenceText: item.originalText || "",
+ referenceText: item.referenceKind === "video_sources" ? item.referenceText || "" : item.originalText || "",
+ sources: item.referenceKind === "video_sources"
+ ? (item.referenceSources ?? []).map(({ kind, label }) => ({ kind, label }))
+ : [],
imageCount: item.imageRefs?.length || 0
}))
};
@@ -1106,6 +1127,7 @@ function multimodalContent(session, request, preparedImages, protocol) {
const content = [{ type: "text", text: JSON.stringify(request) }];
const images = new Map((Array.isArray(preparedImages) ? preparedImages : []).map((item) => [item.visualId, item]));
for (const reference of session?.referenceSnapshots ?? []) {
+ if (reference.referenceKind === "video_sources") continue;
const textOnly = session?.imageReferenceMode === "text_only";
const savedReconstructions = (reference.assets ?? []).map((asset, index) => String(asset?.reconstructionPrompt ?? "").trim()
? `[图片${index + 1}重建提示词]\n${String(asset.reconstructionPrompt).trim()}` : "").filter(Boolean).join("\n\n");
diff --git a/composer.js b/composer.js
index fed1f13..f142d9f 100644
--- a/composer.js
+++ b/composer.js
@@ -1,7 +1,7 @@
import { CONTENT_ROLES, contentRoleForEntry } from "./taxonomy.js";
import { normalizeAppliedSkillSnapshots } from "./creative-skills.js";
import { primaryVisionDescription } from "./visuals.js";
-import { entryMediaAssets } from "./media.js";
+import { currentVideoReconstruction, entryMediaAssets } from "./media.js";
import { TEMP_REFERENCE_SOURCE_TYPES } from "./temp-references.js";
import { normalizeLocalRelativePath } from "./local-media.js";
import {
@@ -37,6 +37,7 @@ export const COMPOSER_SERVICE_IDS = Object.freeze([
"compatible"
]);
export const DEFAULT_COMPOSER_AI_PROFILE = Object.freeze({ serviceId: "deepseek", model: "deepseek-v4-flash", thinking: false });
+export const UNASSIGNED_COMPOSER_AI_PROFILE = Object.freeze({ serviceId: "unassigned", model: "", thinking: false });
export const COMPOSER_INPUT_MAX_CHARACTERS = 750_000;
const COMPOSER_REQUEST_MAX_CHARACTERS = 775_000;
export { DEFAULT_AGENT_INSTRUCTION, DEFAULT_TASK_METHODS, normalizePlannerResult };
@@ -55,6 +56,7 @@ export function normalizeComposerSettings(value = {}) {
export function normalizeComposerAiProfile(value = {}) {
const requestedServiceId = String(value?.serviceId ?? "").trim();
+ if (requestedServiceId === UNASSIGNED_COMPOSER_AI_PROFILE.serviceId) return { ...UNASSIGNED_COMPOSER_AI_PROFILE };
const serviceId = requestedServiceId && COMPOSER_SERVICE_IDS.includes(requestedServiceId)
? requestedServiceId
: "deepseek";
@@ -70,12 +72,13 @@ export function normalizeComposerAiProfile(value = {}) {
export function composerProfileForTaskAssignment(assignmentValue = {}, fallbackValue = {}) {
const providerId = String(assignmentValue?.providerId ?? "").trim();
- if (!providerId) return normalizeComposerAiProfile(fallbackValue);
+ const model = String(assignmentValue?.model ?? "").trim();
+ if (!providerId || !model) return { ...UNASSIGNED_COMPOSER_AI_PROFILE };
const serviceId = providerId === "custom-media" ? "compatible" : providerId;
- if (!COMPOSER_SERVICE_IDS.includes(serviceId)) return normalizeComposerAiProfile(fallbackValue);
+ if (!COMPOSER_SERVICE_IDS.includes(serviceId)) return { ...UNASSIGNED_COMPOSER_AI_PROFILE };
return normalizeComposerAiProfile({
serviceId,
- model: assignmentValue?.model,
+ model,
thinking: fallbackValue?.thinking === true
});
}
@@ -112,7 +115,9 @@ export function isComposerEligibleEntry(entry, targetType = "") {
if (Array.isArray(entry?.memberEntries)) {
return Boolean(referenceTextForEntry(entry, targetType) || imageRefsForEntry(entry).length);
}
- if (targetType === "video" && videoNoteReferenceText(entry)) return true;
+ if (targetType === "video" && entryMediaAssets(entry).some((asset) =>
+ asset.kind === "video" && referenceSourcePartsForAsset(entry, asset.id).length
+ )) return true;
const contentRole = contentRoleForEntry(entry);
if (![CONTENT_ROLES.promptImage, CONTENT_ROLES.promptVideo, CONTENT_ROLES.imageCase, CONTENT_ROLES.reference].includes(contentRole)) return false;
return Boolean(String(entry?.text ?? "").trim() || imageRefsForEntry(entry).length);
@@ -125,7 +130,7 @@ export function createReferenceSnapshots(entries, entryIds, locale = "zh-CN", ta
const selection = normalizeReferenceSelection(selectionValue);
const entry = byId.get(selection.entryId);
if (!Array.isArray(entry?.memberEntries) && selection.assetIds.length > 1) {
- return selection.assetIds.map((assetId) => ({ entryId: selection.entryId, assetIds: [assetId] }));
+ return selection.assetIds.map((assetId) => ({ ...selection, assetIds: [assetId] }));
}
return [selection];
});
@@ -152,10 +157,33 @@ export function createReferenceSnapshots(entries, entryIds, locale = "zh-CN", ta
continue;
}
const contentRole = contentRoleForEntry(entry);
- const videoNotes = targetType === "video" ? videoNoteReferenceText(entry) : "";
- const selectedAssets = selectedImageAssets(entry, selection.assetIds);
+ const selectedAssets = selectedReferenceAssets(entry, selection.assetIds, targetType);
if (selection.assetIds.length && selectedAssets.length !== selection.assetIds.length) {
- throw new Error("选择的参考图片已经失效,请重新选择");
+ throw new Error("选择的参考素材已经失效,请重新选择");
+ }
+ if (selectedAssets.length === 1 && selectedAssets[0].kind === "video") {
+ const asset = selectedAssets[0];
+ const referenceSources = referenceSourcePartsForAsset(entry, asset.id, {
+ sourceIds: selection.sourceIds,
+ analysisIds: selection.analysisIds
+ });
+ if (!referenceSources.length) continue;
+ const referenceText = formatReferenceSourceParts(referenceSources, locale);
+ snapshots.push({
+ referenceId: referenceSnapshotId(entry.id, asset.id),
+ entryId: entry.id,
+ assetId: asset.id,
+ alias: locale === "en" ? `@Reference${snapshots.length + 1}` : `@参考${snapshots.length + 1}`,
+ title: String(entry.title ?? "").trim(),
+ referenceKind: "video_sources",
+ referenceText,
+ originalText: referenceSources.find((source) => source.kind === "original_prompt")?.text ?? "",
+ scope: "asset",
+ imageRefs: [],
+ assets: [],
+ referenceSources
+ });
+ continue;
}
const mediaPrompts = new Map((entry.mediaPrompts ?? []).map((item) => [item.assetId, String(item.text ?? "").trim()]));
const selectedPrompts = selectedAssets.map((asset) => mediaPrompts.get(asset.id)).filter(Boolean);
@@ -165,9 +193,7 @@ export function createReferenceSnapshots(entries, entryIds, locale = "zh-CN", ta
? selectedPrompts.map((text, index) => `[图片${index + 1}独立提示词]\n${text}`).join("\n\n")
: String(entry.text ?? "").trim();
const visualFacts = imageVisualFacts(entry, selection.assetIds);
- const referenceKind = videoNotes && !originalPrompt
- ? "video_notes"
- : originalPrompt && visualFacts.length ? "prompt_vision"
+ const referenceKind = originalPrompt && visualFacts.length ? "prompt_vision"
: contentRole === CONTENT_ROLES.imageCase ? "vision"
: contentRole === CONTENT_ROLES.reference ? "reference" : "prompt";
const baseText = referenceKind === "prompt_vision"
@@ -175,8 +201,7 @@ export function createReferenceSnapshots(entries, entryIds, locale = "zh-CN", ta
: referenceKind === "vision"
? visualFacts.map((fact, index) => `[图片${index + 1}可见事实]\n${fact}`).join("\n\n") || primaryVisionDescription(entry)
: originalPrompt;
- const referenceText = [baseText, videoNotes]
- .map((value) => String(value ?? "").trim()).filter(Boolean).join("\n\n");
+ const referenceText = String(baseText ?? "").trim();
snapshots.push({
referenceId: referenceSnapshotId(entry.id, selectedAssets.length === 1 ? selectedAssets[0].id : ""),
entryId: entry.id,
@@ -236,14 +261,29 @@ function imageVisualFacts(entry, assetIds = []) {
}
function normalizeReferenceSelection(value) {
- if (typeof value === "string") return { entryId: value.trim(), assetIds: [] };
+ if (typeof value === "string") return { entryId: value.trim(), assetIds: [], sourceIds: null, analysisIds: [] };
return {
entryId: String(value?.entryId ?? "").trim(),
assetIds: [...new Set((Array.isArray(value?.assetIds) ? value.assetIds : [])
+ .map((item) => String(item ?? "").trim()).filter(Boolean))],
+ sourceIds: Array.isArray(value?.sourceIds)
+ ? [...new Set(value.sourceIds.map((item) => String(item ?? "").trim()).filter(Boolean))]
+ : null,
+ analysisIds: [...new Set((Array.isArray(value?.analysisIds) ? value.analysisIds : [])
.map((item) => String(item ?? "").trim()).filter(Boolean))]
};
}
+function selectedReferenceAssets(entry, assetIds = [], targetType = "") {
+ const selected = new Set(Array.isArray(assetIds) ? assetIds : []);
+ const assets = entryMediaAssets(entry).filter((asset) => asset.usage !== "poster" &&
+ (asset.kind === "image" || targetType === "video" && asset.kind === "video"));
+ if (selected.size) return assets.filter((asset) => selected.has(asset.id));
+ if (targetType !== "video") return assets;
+ const primary = assets.find((asset) => asset.id === entry?.primaryMediaId);
+ return primary ? [primary] : assets.slice(0, 1);
+}
+
function selectedImageAssets(entry, assetIds = []) {
const selected = new Set(Array.isArray(assetIds) ? assetIds : []);
return entryMediaAssets(entry).filter((asset) =>
@@ -251,6 +291,132 @@ function selectedImageAssets(entry, assetIds = []) {
);
}
+export function referenceSourcePartsForAsset(entry, assetIdValue, options = {}) {
+ const assetId = String(assetIdValue ?? "").trim();
+ const asset = entryMediaAssets(entry).find((item) => item.id === assetId && item.usage !== "poster");
+ if (!asset) return [];
+ const allowedSourceIds = Array.isArray(options.sourceIds) ? new Set(options.sourceIds.map(String)) : null;
+ const selectedAnalysisIds = new Set((Array.isArray(options.analysisIds) ? options.analysisIds : []).map(String));
+ const mediaPrompt = (Array.isArray(entry?.mediaPrompts) ? entry.mediaPrompts : [])
+ .find((item) => String(item?.assetId ?? "") === assetId && String(item?.text ?? "").trim());
+ const originalText = String(mediaPrompt?.text ?? entry?.text ?? "").trim();
+ const current = asset.kind === "video" ? currentVideoReconstruction(entry, assetId) : null;
+ const notes = asset.kind === "video" ? videoNotesForAsset(entry, assetId) : [];
+ const defaultParts = [
+ originalText ? {
+ id: `original:${assetId}`,
+ kind: "original_prompt",
+ label: "原始提示词",
+ text: originalText,
+ provenance: [{
+ type: mediaPrompt ? "asset_prompt" : "case_text",
+ ...(mediaPrompt?.textRevision ? { version: Math.max(1, Number(mediaPrompt.textRevision) || 1) } : {})
+ }]
+ } : null,
+ current?.reconstructionPrompt ? {
+ id: `reconstruction:${String(current.id ?? "current")}`,
+ kind: "video_reconstruction",
+ label: "AI 视觉逆推",
+ text: String(current.reconstructionPrompt).trim(),
+ analysisId: String(current.id ?? "").trim(),
+ analysisVersion: Math.max(1, Number(current.version) || 1),
+ provenance: [{ type: "video_analysis", recordId: String(current.id ?? "").trim(), version: Math.max(1, Number(current.version) || 1) }]
+ } : null,
+ notes.length ? {
+ id: `notes:${assetId}`,
+ kind: "time_notes",
+ label: "人工时间点笔记",
+ text: notes.map((note) => `[${formatReferenceNoteRange(note)}] ${String(note.text ?? "").trim()}`).join("\n"),
+ recordIds: notes.map((note) => String(note.id ?? "").trim()).filter(Boolean),
+ provenance: notes.map((note) => ({ type: "time_note", recordId: String(note.id ?? "").trim() })).filter((item) => item.recordId)
+ } : null
+ ].filter(Boolean);
+ const otherParts = (Array.isArray(entry?.videoAnalyses) ? entry.videoAnalyses : []).flatMap((analysis) => {
+ const analysisId = String(analysis?.id ?? "").trim();
+ if (!analysisId || analysisId === String(current?.id ?? "") || String(analysis?.assetId ?? "") !== assetId || !selectedAnalysisIds.has(analysisId)) return [];
+ const text = String(analysis?.reconstructionPrompt ?? analysis?.text ?? analysis?.description ?? analysis?.summary ?? "").trim();
+ if (!text) return [];
+ return [{
+ id: `analysis:${analysisId}`,
+ kind: "other_analysis",
+ label: "其他分析",
+ text,
+ analysisId,
+ analysisVersion: Math.max(1, Number(analysis.version) || 1),
+ provenance: [{ type: "video_analysis", recordId: analysisId, version: Math.max(1, Number(analysis.version) || 1) }]
+ }];
+ });
+ return [
+ ...defaultParts.filter((part) => !allowedSourceIds || allowedSourceIds.has(part.id)),
+ ...otherParts
+ ]
+ .map(normalizeReferenceSourcePart).filter(Boolean);
+}
+
+export function formatReferenceSourceParts(partsValue = [], locale = "zh-CN") {
+ const groups = [];
+ const byText = new Map();
+ for (const part of (Array.isArray(partsValue) ? partsValue : []).map(normalizeReferenceSourcePart).filter(Boolean)) {
+ const key = part.text.replace(/\r\n?/g, "\n").trim();
+ let group = byText.get(key);
+ if (!group) {
+ group = { text: part.text, labels: [] };
+ byText.set(key, group);
+ groups.push(group);
+ }
+ const label = locale === "en" ? referenceSourceLabelEn(part.kind) : part.label;
+ if (!group.labels.includes(label)) group.labels.push(label);
+ }
+ return groups.map((group) => `[${group.labels.join(" + ")}]\n${group.text}`).join("\n\n");
+}
+
+function videoNotesForAsset(entry, assetId) {
+ return (Array.isArray(entry?.timeNotes) ? entry.timeNotes : [])
+ .filter((note) => String(note?.assetId ?? "") === assetId && String(note?.text ?? "").trim())
+ .toSorted((left, right) => Number(left.startMs) - Number(right.startMs) || String(left.createdAt ?? "").localeCompare(String(right.createdAt ?? "")));
+}
+
+function formatReferenceNoteRange(note) {
+ return Number(note?.endMs) > Number(note?.startMs)
+ ? `${formatReferenceTime(note.startMs)}-${formatReferenceTime(note.endMs)}`
+ : formatReferenceTime(note?.startMs);
+}
+
+function referenceSourceLabelEn(kind) {
+ return ({
+ original_prompt: "Original prompt",
+ video_reconstruction: "AI visual reconstruction",
+ time_notes: "Human time notes",
+ other_analysis: "Other analysis"
+ })[kind] || "Source";
+}
+
+function normalizeReferenceSourcePart(value) {
+ const id = String(value?.id ?? "").trim();
+ const text = String(value?.text ?? "").replace(/\r\n?/g, "\n").trim();
+ const kind = ["original_prompt", "video_reconstruction", "time_notes", "other_analysis"].includes(value?.kind)
+ ? value.kind : "";
+ if (!id || !kind || !text) return null;
+ return {
+ id,
+ kind,
+ label: String(value?.label ?? "").trim() || referenceSourceLabelEn(kind),
+ text,
+ ...(String(value?.analysisId ?? "").trim() ? { analysisId: String(value.analysisId).trim() } : {}),
+ ...(Number(value?.analysisVersion) > 0 ? { analysisVersion: Math.max(1, Number(value.analysisVersion) || 1) } : {}),
+ ...(Array.isArray(value?.recordIds) ? { recordIds: uniqueStrings(value.recordIds) } : {}),
+ provenance: (Array.isArray(value?.provenance) ? value.provenance : []).flatMap((item) => {
+ const type = String(item?.type ?? "").trim();
+ if (!type) return [];
+ return [{
+ type,
+ ...(String(item?.recordId ?? "").trim() ? { recordId: String(item.recordId).trim() } : {}),
+ ...(Number(item?.version) > 0 ? { version: Math.max(1, Number(item.version) || 1) } : {})
+ }];
+ })
+ };
+}
+
function referenceAssetSnapshot(asset) {
const analysis = asset.visionAnalysis && !asset.visionAnalysis.invalidated ? asset.visionAnalysis : null;
const analysisImageFingerprint = String(analysis?.imageFingerprint ?? "").trim();
@@ -467,10 +633,11 @@ export function plannerRequestPayload(sessionValue, userMessage, settingsValue)
sourceType: skill.source,
textMode: skill.textMode
})),
- references: session.referenceSnapshots.map(({ alias, referenceKind, referenceText, imageRefs }) => ({
+ references: session.referenceSnapshots.map(({ alias, referenceKind, referenceText, imageRefs, referenceSources }) => ({
alias,
referenceKind,
referenceText,
+ ...(referenceSources.length ? { sources: referenceSources.map(({ kind, label }) => ({ kind, label })) } : {}),
imageCount: imageRefs.length
})),
retrievedSources: session.retrievedSources.map(({ alias, role, referenceKind, text }) => ({ alias, role, referenceKind, text })),
@@ -611,7 +778,7 @@ function normalizeReferenceSnapshots(values) {
const assets = normalizeReferenceAssets(item?.assets);
if (!entryId || !referenceId || !alias || (!referenceText && !imageRefs.length && !assetRefs.length) || seen.has(referenceId)) return [];
seen.add(referenceId);
- const referenceKind = ["vision", "video_notes", "prompt_vision", "reference"].includes(item?.referenceKind)
+ const referenceKind = ["vision", "video_notes", "video_sources", "prompt_vision", "reference"].includes(item?.referenceKind)
? item.referenceKind
: "prompt";
return [{
@@ -629,7 +796,9 @@ function normalizeReferenceSnapshots(values) {
scope: item?.scope === "asset" ? "asset" : "case",
imageRefs,
assetRefs,
- assets
+ assets,
+ referenceSources: (Array.isArray(item?.referenceSources) ? item.referenceSources : [])
+ .map(normalizeReferenceSourcePart).filter(Boolean)
}];
});
}
diff --git a/creative-skill-service.js b/creative-skill-service.js
index 24758c6..571e42c 100644
--- a/creative-skill-service.js
+++ b/creative-skill-service.js
@@ -1,4 +1,9 @@
-import { COMPOSER_INPUT_MAX_CHARACTERS, createComposerSession, normalizeComposerSettings } from "./composer.js";
+import {
+ COMPOSER_INPUT_MAX_CHARACTERS,
+ createComposerSession,
+ normalizeComposerSettings,
+ referenceSourcePartsForAsset
+} from "./composer.js";
import { executeComposerTurnWithService } from "./composer-service.js";
import { entryMediaAssets } from "./media.js";
@@ -21,14 +26,19 @@ export function buildSkillExtractionRequest(input = {}) {
const sources = (Array.isArray(input.sources) ? input.sources : []).flatMap((source, index) => {
const prompt = multiline(source?.prompt);
const analysis = multiline(source?.analysis);
- return prompt || analysis ? [{ number: index + 1, prompt, analysis }] : [];
+ const parts = normalizeAnonymousSourceParts(source?.parts);
+ return prompt || analysis || parts.length ? [{ number: index + 1, prompt, analysis, parts }] : [];
});
if (!sources.length) throw new Error(locale === "en" ? "The selected cases have no usable text" : "所选案例没有可用于提炼的文字资料");
const vision = (Array.isArray(input.visualAnalyses) ? input.visualAnalyses : []).map(multiline).filter(Boolean);
const sourceText = sources.map((source) => [
locale === "en" ? `Source ${source.number}` : `匿名来源 ${source.number}`,
- source.prompt ? `${locale === "en" ? "Original prompt" : "原提示词"}:\n${source.prompt}` : "",
- source.analysis ? `${locale === "en" ? "Existing visual analysis" : "已有画面分析"}:\n${source.analysis}` : ""
+ ...(source.parts.length
+ ? exactAnonymousSourceGroups(source.parts).map((group) => `[${group.labels.map((kind) => skillSourceLabel(kind, locale)).join(" + ")}]\n${group.text}`)
+ : [
+ source.prompt ? `${locale === "en" ? "Original prompt" : "原提示词"}:\n${source.prompt}` : "",
+ source.analysis ? `${locale === "en" ? "Existing visual analysis" : "已有画面分析"}:\n${source.analysis}` : ""
+ ])
].filter(Boolean).join("\n")).join("\n\n");
const visualText = vision.length ? `\n\n${locale === "en" ? "Confirmed visual contact-sheet analyses" : "已确认的视觉联系表分析"}:\n${vision.map((item, index) => `${index + 1}. ${item}`).join("\n")}` : "";
const extractionInstruction = multiline(input.instructionOverride) || defaultSkillExtractionInstruction(locale);
@@ -53,7 +63,7 @@ export const SKILL_EXTRACTION_BATCH_TARGET_CHARACTERS = Math.floor(COMPOSER_INPU
export function skillExtractionWorkload(input = {}) {
const sources = normalizeSkillSources(input.sources);
const batches = partitionSkillSources(sources, input.maxBatchCharacters);
- const textCharacters = sources.reduce((sum, source) => sum + source.prompt.length + source.analysis.length, 0);
+ const textCharacters = sources.reduce((sum, source) => sum + skillSourceCharacters(source), 0);
return {
sourceCount: sources.length,
textCharacters,
@@ -65,7 +75,7 @@ export function skillExtractionWorkload(input = {}) {
tokenEstimate: estimateSkillTokens([
clean(input.goal),
multiline(input.instructionOverride) || defaultSkillExtractionInstruction(input.locale),
- ...sources.flatMap((source) => [source.prompt, source.analysis])
+ ...sources.flatMap((source) => source.parts?.length ? source.parts.map((part) => part.text) : [source.prompt, source.analysis])
].join("\n"))
};
}
@@ -177,24 +187,39 @@ export function anonymousSkillSources(entriesValue = [], selectionsValue = [], o
return (Array.isArray(entriesValue) ? entriesValue : []).flatMap((entry) => {
const selection = selections.get(String(entry?.id ?? ""));
if (!selection) return [];
- const selectedAssets = entryMediaAssets(entry).filter((asset) => selection.assetIds === null || selection.assetIds.has(String(asset.id)));
+ const selectedAssets = entryMediaAssets(entry).filter((asset) => asset.usage !== "poster" && (selection.assetIds === null || selection.assetIds.has(String(asset.id))));
const mediaPrompts = new Map((Array.isArray(entry?.mediaPrompts) ? entry.mediaPrompts : [])
.map((item) => [String(item?.assetId ?? ""), multiline(item?.text)]));
- const prompt = [
- selection.includeEntryText ? multiline(entry?.text) : "",
- ...selectedAssets.map((asset) => mediaPrompts.get(String(asset.id)) || ""),
- ...selectedAssets.filter((asset) => asset.kind === "document").map((asset) => multiline(documents.get(String(asset.id))))
- ].filter(Boolean).join("\n\n");
- const selectedIds = new Set(selectedAssets.map((asset) => String(asset.id)));
- const analysis = [
- ...selectedAssets.filter((asset) => !asset?.visionAnalysis?.invalidated)
- .map((asset) => multiline(asset?.visionAnalysis?.reconstructionPrompt || asset?.visionAnalysis?.description)),
- ...(Array.isArray(entry?.timeNotes) ? entry.timeNotes : [])
- .filter((note) => selectedIds.has(String(note?.assetId ?? ""))).map((note) => multiline(note?.text)),
- ...(Array.isArray(entry?.videoAnalyses) ? entry.videoAnalyses : [])
- .filter((item) => selectedIds.has(String(item?.assetId ?? ""))).map((item) => multiline(item?.description || item?.summary || item?.text))
- ].filter(Boolean).join("\n");
- return prompt || analysis ? [{ prompt, analysis }] : [];
+ const hasSelectedVideo = selectedAssets.some((asset) => asset.kind === "video");
+ const parts = [
+ selection.includeEntryText && multiline(entry?.text)
+ ? { kind: "original_prompt", text: multiline(entry.text) } : null,
+ ...selectedAssets.flatMap((asset) => {
+ if (asset.kind === "video") {
+ return referenceSourcePartsForAsset(entry, asset.id, {
+ sourceIds: selection.sourceIds,
+ analysisIds: selection.analysisIds
+ }).map(({ kind, text }) => ({ kind, text }));
+ }
+ const promptText = mediaPrompts.get(String(asset.id));
+ const documentText = asset.kind === "document" ? multiline(documents.get(String(asset.id))) : "";
+ const visualText = !asset?.visionAnalysis?.invalidated
+ ? multiline(asset?.visionAnalysis?.reconstructionPrompt || asset?.visionAnalysis?.description) : "";
+ return [
+ promptText ? { kind: "original_prompt", text: promptText } : null,
+ documentText ? { kind: "document_text", text: documentText } : null,
+ visualText ? { kind: "image_analysis", text: visualText } : null
+ ].filter(Boolean);
+ })
+ ].filter(Boolean);
+ const normalizedParts = normalizeAnonymousSourceParts(parts);
+ const prompt = normalizedParts.filter((part) => ["original_prompt", "document_text"].includes(part.kind)).map((part) => part.text).join("\n\n");
+ const analysis = normalizedParts.filter((part) => !["original_prompt", "document_text"].includes(part.kind)).map((part) => part.text).join("\n");
+ return normalizedParts.length ? [{
+ prompt,
+ analysis,
+ ...(hasSelectedVideo ? { parts: normalizedParts } : {})
+ }] : [];
});
}
@@ -208,7 +233,9 @@ function normalizeSourceSelections(values) {
if (!entryId) return [];
return [[entryId, {
includeEntryText: selection?.includeEntryText !== false,
- assetIds: new Set((Array.isArray(selection?.assetIds) ? selection.assetIds : []).map(String))
+ assetIds: new Set((Array.isArray(selection?.assetIds) ? selection.assetIds : []).map(String)),
+ sourceIds: Array.isArray(selection?.sourceIds) ? selection.sourceIds.map(String) : null,
+ analysisIds: (Array.isArray(selection?.analysisIds) ? selection.analysisIds : []).map(String)
}]];
}));
}
@@ -267,7 +294,8 @@ function normalizeSkillSources(values) {
return (Array.isArray(values) ? values : []).flatMap((source) => {
const prompt = multiline(source?.prompt);
const analysis = multiline(source?.analysis);
- return prompt || analysis ? [{ prompt, analysis }] : [];
+ const parts = normalizeAnonymousSourceParts(source?.parts);
+ return prompt || analysis || parts.length ? [{ prompt, analysis, parts }] : [];
});
}
@@ -278,7 +306,7 @@ function partitionSkillSources(values, maxCharactersValue) {
let current = [];
let characters = 0;
for (const source of fragments) {
- const size = source.prompt.length + source.analysis.length;
+ const size = skillSourceCharacters(source);
if (current.length && characters + size > maxCharacters) {
batches.push(current);
current = [];
@@ -292,17 +320,72 @@ function partitionSkillSources(values, maxCharactersValue) {
}
function splitSkillSource(source, maxCharacters) {
- if (source.prompt.length + source.analysis.length <= maxCharacters) return [source];
+ if (skillSourceCharacters(source) <= maxCharacters) return [source];
const chunks = [];
+ for (const part of source.parts ?? []) {
+ for (let offset = 0; offset < part.text.length; offset += maxCharacters) {
+ chunks.push({ prompt: "", analysis: "", parts: [{ ...part, text: part.text.slice(offset, offset + maxCharacters) }] });
+ }
+ }
+ if (source.parts?.length) return chunks;
for (let offset = 0; offset < source.prompt.length; offset += maxCharacters) {
- chunks.push({ prompt: source.prompt.slice(offset, offset + maxCharacters), analysis: "" });
+ chunks.push({ prompt: source.prompt.slice(offset, offset + maxCharacters), analysis: "", parts: [] });
}
for (let offset = 0; offset < source.analysis.length; offset += maxCharacters) {
- chunks.push({ prompt: "", analysis: source.analysis.slice(offset, offset + maxCharacters) });
+ chunks.push({ prompt: "", analysis: source.analysis.slice(offset, offset + maxCharacters), parts: [] });
}
return chunks;
}
+function skillSourceCharacters(source) {
+ return (source.parts ?? []).reduce((sum, part) => sum + part.text.length, 0)
+ || source.prompt.length + source.analysis.length;
+}
+
+function normalizeAnonymousSourceParts(values) {
+ const allowed = new Set(["original_prompt", "video_reconstruction", "time_notes", "other_analysis", "image_analysis", "document_text"]);
+ return (Array.isArray(values) ? values : []).flatMap((part) => {
+ const kind = String(part?.kind ?? "").trim();
+ const text = multiline(part?.text);
+ return allowed.has(kind) && text ? [{ kind, text }] : [];
+ });
+}
+
+function exactAnonymousSourceGroups(parts) {
+ const groups = [];
+ const byText = new Map();
+ for (const part of parts) {
+ const key = multiline(part.text);
+ let group = byText.get(key);
+ if (!group) {
+ group = { text: part.text, labels: [] };
+ byText.set(key, group);
+ groups.push(group);
+ }
+ if (!group.labels.includes(part.kind)) group.labels.push(part.kind);
+ }
+ return groups;
+}
+
+function skillSourceLabel(kind, locale) {
+ const labels = locale === "en" ? {
+ original_prompt: "Original prompt",
+ video_reconstruction: "AI visual reconstruction",
+ time_notes: "Human time notes",
+ other_analysis: "Other analysis",
+ image_analysis: "Image analysis",
+ document_text: "Selected document"
+ } : {
+ original_prompt: "原始提示词",
+ video_reconstruction: "AI 视觉逆推",
+ time_notes: "人工时间点笔记",
+ other_analysis: "其他分析",
+ image_analysis: "图片分析",
+ document_text: "所选文档"
+ };
+ return labels[kind] || (locale === "en" ? "Source" : "来源");
+}
+
function addUsage(left = {}, right = {}) {
const keys = new Set([...Object.keys(left || {}), ...Object.keys(right || {})]);
return Object.fromEntries([...keys].map((key) => [key, (Number(left?.[key]) || 0) + (Number(right?.[key]) || 0)]));
diff --git a/i18n.js b/i18n.js
index fd1d087..7cedc24 100644
--- a/i18n.js
+++ b/i18n.js
@@ -51,6 +51,93 @@ const EN = Object.freeze({
"当前图片 · 使用共享提示词": "Current image · Using shared prompt",
"完善分析": "Complete analysis",
"批量图片分析": "Analyze image set",
+ "批量分析图片": "Batch analyze images",
+ "批量分析视频": "Batch analyze videos",
+ "同时生成 AI 标签": "Also generate AI tags",
+ "主要媒体": "Primary media",
+ "创意拆解": "Creative breakdown",
+ "原始提示词": "Original prompt",
+ "尚未分配视频分析服务": "No video analysis service assigned",
+ "广告审片": "Ad review",
+ "自定义问题": "Custom question",
+ "逆推提示词": "Reconstruct prompt",
+ "附加本地视频": "Attach local video",
+ "授权并在案例库播放": "Allow and play in the library",
+ "首次在案例库播放 YouTube,需要授权播放器域名。只影响当前扩展发起的播放器,不读取网页内容。": "Playing YouTube in the library for the first time requires access to the player domain. This only affects players opened by this extension and does not read page content.",
+ "本批视频逆推指令": "Video reconstruction instructions for this batch",
+ "所选案例中没有可分析的本地视频": "The selected cases do not contain analyzable local videos",
+ "请先选择至少一个带本地视频的案例": "Select at least one case with a local video",
+ "请先选择至少一个带图片的案例": "Select at least one case with an image",
+ "包含案例中的全部视频": "Include every video in each case",
+ "包含已有 AI 视觉逆推的视频": "Include videos that already have an AI reconstruction",
+ "不生成 AI 标签": "Do not generate AI tags",
+ "每次点击发送 1 次请求,费用由服务商账户产生": "Each click sends one request. Charges are billed by the provider",
+ "输入只针对这支视频的具体问题": "Enter a question specific to this video",
+ "本次分析指令": "Instructions for this analysis",
+ "{mode}指令": "{mode} instructions",
+ "视频分析任务创建失败": "The video analysis task could not be created",
+ "正在排队…": "Queued…",
+ "正在编码本地视频…": "Encoding the local video…",
+ "正在上传…": "Uploading…",
+ "服务正在处理…": "The service is processing the video…",
+ "正在分析画面…": "Analyzing the video…",
+ "分析完成,正在保存…": "Analysis complete. Saving…",
+ "停止失败": "The task could not be stopped",
+ "重新发送失败": "The task could not be sent again",
+ "已人工修改": "Manually edited",
+ "仅分析画面,未分析声音": "Visual analysis only; audio was not analyzed",
+ "编辑 AI 视觉逆推提示词": "Edit the AI visual reconstruction prompt",
+ "AI 视觉逆推提示词保存失败": "The AI visual reconstruction prompt could not be saved",
+ "不确定项({count})": "Uncertainties ({count})",
+ "历史分析({count})": "Analysis history ({count})",
+ "{mode} · 版本 {version}": "{mode} · Version {version}",
+ "未关联旧记录": "Unlinked legacy record",
+ "本次用量:{tokens} tokens": "Usage: {tokens} tokens",
+ "请先填写自定义问题": "Enter a custom question first",
+ "本次分析指令不能为空": "The analysis instructions cannot be empty",
+ "没有获得所选 AI 服务的访问权限,未发送视频": "Access to the selected AI service was not granted, so the video was not sent",
+ "批量视频分析只发送案例库中的本地视频": "Batch video analysis sends only local videos stored in the library",
+ "这支视频已有单项或批量分析任务在运行": "This video already has a single-item or batch analysis in progress",
+ "本地视频文件缺失": "The local video file is missing",
+ " · 排除 {count} 支:{reasons}": " · {count} excluded: {reasons}",
+ "来源或当前状态不符合本次批量条件": "The source or current state is not eligible for this batch",
+ "有 {count} 支视频不会发送": "{count} videos will not be sent",
+ "只分析其余视频": "Analyze the remaining videos only",
+ "模型目录已读取 · {count} 个模型{unavailable}": "Model catalog loaded · {count} models{unavailable}",
+ "AI 服务只连接一次;任务路由显示每类功能实际调用哪个服务与模型。": "Connect each AI service once. Task routes show the service and model actually used by each feature.",
+ "任务路由": "Task routes",
+ "各任务实际路由": "Actual route for each task",
+ "未选择模型": "No model selected",
+ "创作规划未配置,请在 AI 服务的任务路由中选择模型": "Creative planning is not configured. Choose a model in AI Services task routing.",
+ "未连接可用服务": "No usable service connected",
+ "连接需修复:服务已移除": "Connection needs attention: the service was removed",
+ "连接需修复:API Key 或发送授权无效": "Connection needs attention: the API key or sending permission is invalid",
+ "已分配但模型不可用": "Assigned model unavailable",
+ "这里更改该任务实际使用的服务与模型;创作台本轮切换只影响当前创作。": "Change the service and model actually used by this task. A Composer switch affects only the current creation.",
+ "保存任务路由": "Save task route",
+ "文字任务默认 20,图片任务默认 10,视频任务初始为 2;调整只对新任务生效。": "Text tasks default to 20, image tasks to 10, and video tasks start at 2. Changes apply only to new tasks.",
+ "分析模型": "Analysis model",
+ "读取模型后选择": "Load models, then choose",
+ "填写这个兼容接口实际调用的模型": "Enter the model actually used by this compatible endpoint",
+ "保存后联动:{tasks};已单独更换的任务保持不变": "Saving updates: {tasks}. Tasks changed separately remain unchanged",
+ "填写兼容接口实际调用的模型;保存后任务路由会同步更新": "Enter the model actually used by the compatible endpoint. Saving also updates its task routes",
+ "先读取当前账号模型,再选择本次连接使用的分析模型": "Load the models available to this account, then choose the analysis model for this connection",
+ "请先填写 API Key": "Enter the API key first",
+ "请先确认发送授权": "Confirm permission to send task content first",
+ "没有获得所选 AI 服务的访问权限,未读取模型": "Access to the selected AI service was not granted, so models were not loaded",
+ "请选择分析模型": "Choose an analysis model",
+ "已读取 {count} 个可选分析模型": "Loaded {count} available analysis models",
+ "所选 AI 服务不存在": "The selected AI service does not exist",
+ "请填写分析模型": "Enter an analysis model",
+ "请先读取模型并选择分析模型": "Load models and choose an analysis model first",
+ "当前模型没有可确认的分析能力,请重新读取并选择": "This model has no confirmed analysis capability. Load the catalog again and choose another model",
+ "重新发送": "Send again",
+ "AI 视觉逆推提示词": "AI visual reconstruction prompt",
+ "编辑原始提示词": "Edit original prompt",
+ "读取模型": "Load models",
+ "短内容": "Short content",
+ "查看封面": "View cover",
+ "短内容 · 请确认是否保留": "Short content · Confirm whether to keep it",
"选择项目": "Choose projects",
"已加入 {count} 个项目": "Added to {count} projects",
"未选择模型": "No model selected",
@@ -1915,13 +2002,12 @@ const EN = Object.freeze({
"AI 任务配置": "AI task configuration",
"已连接 {count} 个服务;文字、图片、视频与生成任务各自明确分配,不会静默切换。": "{count} services connected. Text, image, video, and generation tasks are assigned explicitly and never switched silently.",
"尚未连接 AI 服务;本地资料整理仍可使用。": "No AI service connected. Local library organization remains available.",
- "各任务默认模型": "Default model for each task",
"连接 AI 服务": "Connect AI service",
"分析规则与创作方法(高级)": "Analysis rules and creation methods (Advanced)",
"只管理分析规则和创作方法;服务连接与模型分配均在上方完成。": "Manage only analysis rules and creation methods here. Connect services and assign models above.",
"保存分析规则": "Save analysis rules",
"图片视觉分析规则": "Image-analysis rules",
- "这里只管理图片分析规则;服务连接与默认模型在上方配置。": "Manage only image-analysis rules here. Configure the service connection and default model above.",
+ "这里只管理图片分析规则;服务连接与任务路由均在上方完成。": "Manage only image-analysis rules here. Connect services and assign task routes above.",
"规则保存在本机,只在对应任务运行时使用。": "Rules are stored locally and used only when the corresponding task runs.",
"分析规则已保存": "Analysis rules saved",
"分析规则保存失败": "Analysis rules could not be saved",
@@ -1957,14 +2043,15 @@ const EN = Object.freeze({
"(官方声明支持,账号可用性待实测)": " (officially documented; account access needs a live test)",
"官方声明该模型支持此能力,但账号目录未列出;以真实执行结果为准": "Official documentation supports this capability, but the account catalog does not list the model; rely on a live execution result.",
"(当前目录不可用)": " (not available in the current catalog)",
+ "已分配但模型不可用": "Assigned model unavailable",
"尚无已连接且模型目录明确支持“{task}”的服务,请先连接服务并刷新模型": "No connected service has a catalog model explicitly supporting {task}. Connect a service and refresh its models first.",
"尚无已连接且可用于“{task}”的模型,请先连接服务并刷新模型目录": "No connected model can be assigned to {task}. Connect a service and refresh its model catalog first.",
"这里设置全局默认;创作台中的本轮切换不会改动此处。": "Set the global default here. Switching a model for one Composer run does not change this setting.",
"模型": "Model",
"保存任务默认": "Save task default",
"请选择或填写模型": "Select or enter a model",
- "任务默认保存失败": "The task default could not be saved",
- "{task}默认模型已保存": "Default model saved for {task}",
+ "任务路由保存失败": "The task route could not be saved",
+ "{task}任务路由已保存": "Task route saved for {task}",
"编辑服务": "Edit service",
"图片分析 API Key": "Image-analysis API key",
"图片分析 API Key(米醋 vip_2,只用于看图)": "Image-analysis API key (Micu vip_2; vision only)",
diff --git a/library-package.js b/library-package.js
index 69a8302..53290a4 100644
--- a/library-package.js
+++ b/library-package.js
@@ -778,6 +778,10 @@ export function mergeLibraryPackage(current = {}, importedValue = {}, options =
assetId: visualIdMap[note.assetId] ?? note.assetId,
...(note.frameAssetId ? { frameAssetId: visualIdMap[note.frameAssetId] ?? note.frameAssetId } : {})
}));
+ entry.videoAnalyses = (entry.videoAnalyses ?? []).map((analysis) => ({
+ ...analysis,
+ ...(analysis.assetId ? { assetId: visualIdMap[analysis.assetId] ?? analysis.assetId } : {})
+ }));
entry.facetAssignments = (entry.facetAssignments ?? []).map((item) => {
const facetId = facetIds.get(item.facetId);
const nodeId = nodeIds.get(item.nodeId);
@@ -1219,6 +1223,10 @@ function remapTrashedEntrySnapshot(entryValue, targetEntryId, usedVisualIds, pre
...prompt,
assetId: visualIdMap[prompt.assetId] ?? prompt.assetId
}));
+ entry.videoAnalyses = (entry.videoAnalyses ?? []).map((analysis) => ({
+ ...analysis,
+ ...(analysis.assetId ? { assetId: visualIdMap[analysis.assetId] ?? analysis.assetId } : {})
+ }));
entry.facetAssignments = (entry.facetAssignments ?? []).map((assignment) => ({
...assignment,
...(assignment.visualId ? { visualId: visualIdMap[assignment.visualId] ?? assignment.visualId } : {})
diff --git a/library.css b/library.css
index 2c0857c..d78c83d 100644
--- a/library.css
+++ b/library.css
@@ -1227,10 +1227,31 @@ details:not([open]) > .selection-menu-panel { display: none; }
.detail-section h2 { font-size: 16px; }
.detail-visual-item, .detail-image { background: var(--viewer-bg); }
.detail-visual-caption { padding: 10px 12px; background: rgba(9, 11, 10, .92); }
-.video-analysis-history { display: grid; gap: 12px; padding: 16px; border-top: 1px solid var(--line); background: var(--viewer-bg); }
+.video-analysis-workspace { display: grid; gap: 12px; margin-top: 14px; border-top: 1px solid var(--line); padding-top: 14px; }
+.video-analysis-history { display: grid; gap: 10px; }
.video-analysis-history:empty { display: none; }
.video-analysis-header, .video-analysis-record > header { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
-.video-analysis-header h3 { margin: 0; font-size: 14px; }
+.video-analysis-header h3 { margin: 0; font-size: 11px; }
+.video-analysis-header small { color: var(--muted); font-size: 10px; text-align: right; }
+.video-analysis-actions { display: grid; gap: 8px; }
+.video-analysis-action { display: grid; grid-template-columns: minmax(118px, .32fr) minmax(0, 1fr); align-items: start; gap: 8px; }
+.video-analysis-action-controls { display: grid; gap: 6px; }
+.video-analysis-action-controls > button { width: 100%; min-height: 32px; padding: 5px 8px; font-size: 11px; }
+.video-analysis-instruction { width: 100%; min-height: 32px; max-height: 260px; resize: vertical; padding: 7px 9px; font: 11px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; }
+.video-analysis-tag-toggle { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 10px; line-height: 1.3; }
+.video-analysis-tag-toggle input { width: 14px; min-height: 14px; flex: 0 0 auto; accent-color: var(--accent-emphasis); }
+.video-analysis-task-status { display: flex; min-height: 34px; align-items: center; justify-content: space-between; gap: 10px; border: 1px solid var(--line); border-radius: var(--ui-control-radius); padding: 7px 9px; color: var(--muted); background: var(--raised); font-size: 11px; }
+.video-analysis-task-status button { min-height: 28px; padding: 4px 8px; font-size: 10px; }
+.video-reconstruction-current { display: grid; gap: 9px; border: 1px solid var(--line); border-radius: var(--ui-container-radius); padding: 12px; background: var(--raised); }
+.video-analysis-record-heading { display: grid; gap: 3px; }
+.video-analysis-record-heading h4 { margin: 0; color: var(--ink); }
+.video-analysis-record-heading small { color: var(--muted); font-size: 10px; line-height: 1.45; }
+.video-reconstruction-editor { width: 100%; min-height: 150px; resize: vertical; padding: 10px; font: 11px/1.65 ui-monospace, SFMono-Regular, Menlo, monospace; }
+.video-reconstruction-current > button { justify-self: end; min-height: 30px; padding: 5px 9px; font-size: 11px; }
+.video-analysis-tags { margin-top: 2px; }
+.video-analysis-uncertainties > summary, .video-analysis-history > summary { color: var(--muted); cursor: pointer; font-size: 11px; }
+.video-analysis-uncertainties ul { margin: 8px 0 0; padding-left: 18px; color: var(--muted); font-size: 11px; line-height: 1.55; }
+.video-analysis-history > .video-analysis-list { padding-top: 8px; }
.video-analysis-list { display: grid; gap: 10px; }
.video-analysis-record { display: grid; gap: 10px; padding: 12px; border: 1px solid var(--line); border-radius: var(--ui-container-radius); background: var(--raised); }
.video-analysis-record > header span, .video-analysis-record small, .video-analysis-empty { color: var(--muted); font-size: 11px; }
@@ -1286,6 +1307,8 @@ details:not([open]) > .selection-menu-panel { display: none; }
.trash-item-cover { width: 58px; }
.trash-item-actions { grid-column: 1 / -1; justify-content: flex-end; }
.ai-routing-card, .video-analysis-header, .video-analysis-record > header { align-items: stretch; flex-direction: column; }
+ .video-analysis-header small { text-align: left; }
+ .video-analysis-action { grid-template-columns: 1fr; }
.import-dialog { width: 100vw; height: 100dvh; max-height: none; border-radius: 0; }
.import-dialog-body { max-height: none; }
.import-summary { grid-template-columns: 1fr 1fr; }
diff --git a/library.html b/library.html
index 368e796..2bc0b31 100644
--- a/library.html
+++ b/library.html
@@ -109,7 +109,8 @@
<summary aria-label="更多" data-i18n-aria-label="更多"><svg class="ui-icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><use href="assets/ui-icons.svg#icon-ellipsis"></use></svg><span class="selection-action-label" data-i18n="更多">更多</span></summary>
<div class="project-menu-panel selection-menu-panel selection-more-panel">
<button id="selection-combine" class="button-secondary" type="button" disabled data-i18n="组合案例">组合案例</button>
- <button id="selection-analyze" class="button-secondary" type="button" disabled data-i18n="批量分析画面">批量分析画面</button>
+ <button id="selection-analyze" class="button-secondary" type="button" disabled data-i18n="批量分析图片">批量分析图片</button>
+ <button id="selection-video-analyze" class="button-secondary" type="button" disabled data-i18n="批量分析视频">批量分析视频</button>
<button id="selection-trash" class="quiet-danger" type="button" disabled data-i18n="移入回收站">移入回收站</button>
</div>
</details>
@@ -379,13 +380,13 @@
</section>
<section id="settings-ai-panel" class="settings-panel settings-ai-panel" data-settings-panel="ai" hidden>
<section class="ai-routing-card">
- <div class="ai-routing-heading"><h3 data-i18n="AI 配置">AI 配置</h3><p id="ai-routing-summary" data-i18n="先按任务选择默认服务;API Key 只需在连接服务时填写一次。">先按任务选择默认服务;API Key 只需在连接服务时填写一次。</p></div>
+ <div class="ai-routing-heading"><h3 data-i18n="AI 配置">AI 配置</h3><p id="ai-routing-summary" data-i18n="AI 服务只连接一次;任务路由显示每类功能实际调用哪个服务与模型。">AI 服务只连接一次;任务路由显示每类功能实际调用哪个服务与模型。</p></div>
<nav class="ai-routing-tabs" role="tablist" aria-label="AI 配置视图" data-i18n-aria-label="AI 配置视图">
- <button type="button" data-ai-routing-tab="tasks" aria-selected="true" data-i18n="任务默认">任务默认</button>
+ <button type="button" data-ai-routing-tab="tasks" aria-selected="true" data-i18n="任务路由">任务路由</button>
<button type="button" data-ai-routing-tab="providers" aria-selected="false" data-i18n="服务连接">服务连接</button>
</nav>
<div class="ai-routing-regions">
- <section class="ai-task-region" data-ai-routing-panel="tasks"><h4 data-i18n="各任务默认模型">各任务默认模型</h4><div id="ai-assignment-list" class="ai-assignment-list"></div></section>
+ <section class="ai-task-region" data-ai-routing-panel="tasks"><h4 data-i18n="各任务实际路由">各任务实际路由</h4><div id="ai-assignment-list" class="ai-assignment-list"></div></section>
<section data-ai-routing-panel="providers" hidden><div class="ai-provider-region-heading"><h4 data-i18n="服务连接">服务连接</h4><button id="open-ai-routing" type="button" data-i18n="连接 AI 服务">连接 AI 服务</button></div><div id="ai-provider-list" class="ai-provider-list"></div></section>
</div>
</section>
@@ -439,7 +440,7 @@
</details>
</form>
<form id="vision-settings-form" class="analysis-settings-form" data-analysis-kind-panel="vision" hidden>
- <div class="section-heading"><div><h3 data-i18n="图片视觉分析规则">图片视觉分析规则</h3><p data-i18n="这里只管理图片分析规则;服务连接与默认模型在上方配置。">这里只管理图片分析规则;服务连接与默认模型在上方配置。</p></div></div>
+ <div class="section-heading"><div><h3 data-i18n="图片视觉分析规则">图片视觉分析规则</h3><p data-i18n="这里只管理图片分析规则;服务连接与任务路由均在上方完成。">这里只管理图片分析规则;服务连接与任务路由均在上方完成。</p></div></div>
<section data-analysis-locale-panel="zh-CN">
<label><span data-i18n="中文图片分析规则">中文图片分析规则</span><textarea id="vision-instructions-zh" rows="10"></textarea></label>
</section>
@@ -459,7 +460,7 @@
<dialog id="vision-batch-dialog" class="data-safety-dialog vision-batch-dialog">
<header class="data-safety-header">
- <div><p class="eyebrow">VISION TASK</p><h2 data-i18n="批量分析画面">批量分析画面</h2></div>
+ <div><p class="eyebrow">VISION TASK</p><h2 id="vision-batch-title" data-i18n="批量分析图片">批量分析图片</h2></div>
<button id="vision-batch-close" class="icon-button" type="button" aria-label="关闭批量画面分析" data-i18n-aria-label="关闭批量画面分析" title="关闭批量画面分析" data-i18n-title="关闭批量画面分析"><svg class="ui-icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><use href="assets/ui-icons.svg#icon-x"></use></svg></button>
</header>
<div class="data-safety-body">
@@ -469,6 +470,8 @@
</section>
<label class="task-option"><input id="vision-batch-all-images" type="checkbox" /><span data-i18n="分析所选案例中的全部未分析图片">分析所选案例中的全部未分析图片</span></label>
<label class="task-option"><input id="vision-batch-reanalyze" type="checkbox" /><span data-i18n="重新分析已有反推提示词">重新分析已有反推提示词</span></label>
+ <label id="vision-batch-tags-option" class="task-option" hidden><input id="vision-batch-tags" type="checkbox" checked /><span data-i18n="同时生成 AI 标签">同时生成 AI 标签</span></label>
+ <label id="vision-batch-instruction-field" hidden><span data-i18n="本批视频逆推指令">本批视频逆推指令</span><textarea id="vision-batch-instruction" rows="3"></textarea></label>
<p class="data-safety-note" data-i18n="每次分析一张图片;失败项需手动重试。">每次分析一张图片;失败项需手动重试。</p>
<div id="vision-batch-progress" class="maintenance-progress" hidden><progress id="vision-batch-progress-bar" max="1" value="0"></progress></div>
<div class="data-safety-primary-actions">
diff --git a/library.js b/library.js
index fbedd5e..00fb4f9 100644
--- a/library.js
+++ b/library.js
@@ -63,7 +63,7 @@ import {
organizeDetailTagsWithDeepSeek
} from "./deepseek.js";
import { runScheduledAnalysisWithRetries } from "./analysis-scheduler.js";
-import { chatCompletionsVideoSourcePlan } from "./video-analysis.js";
+import { chatCompletionsVideoSourcePlan, videoAnalysisPrompt } from "./video-analysis.js";
import { getAiModelCapability } from "./ai-model-capabilities.js";
import {
COMPOSER_METHOD_VERSION,
@@ -102,6 +102,7 @@ import {
import {
entryMediaAssets,
entryHasMedia,
+ currentVideoReconstruction,
normalizeEntryMedia,
normalizeMediaAsset,
posterAssetForVideo,
@@ -210,12 +211,12 @@ const elements = Object.fromEntries([
"add-quick-note", "organize-detail-tags", "organize-detail-status", "pause-analysis-batch", "pause-library-maintenance", "preview-analysis-batch", "preview-analysis-reanalyze", "preview-reanalyze", "reanalyze-preview", "result-count", "resume-analysis-batch", "resume-library-maintenance", "retry-analysis-failures", "retry-library-maintenance", "start-analysis-reanalyze",
"project-selection-actions", "project-selection-cancel", "project-selection-clear", "project-selection-count", "project-selection-save", "project-selection-select-all", "project-selection-select-filtered", "project-selection-title", "project-order-status", "restore-analysis-default", "search-input", "share-bar", "share-cancel", "share-count", "share-export", "start-analysis-batch", "start-compose", "toggle-filters", "undo-analysis-batch", "undo-facet", "vocabulary-facet", "workspace-library",
"share-dialog", "share-dialog-close", "share-dialog-title", "share-dialog-meta", "share-dialog-options", "share-dialog-export", "share-dialog-submit", "share-dialog-disclosure", "share-dialog-result", "share-dialog-result-text", "share-dialog-show-files", "share-dialog-open-form",
- "add-menu", "export-path-setting", "media-file", "media-folder", "library-name-setting", "save-library-settings", "select-cases", "selection-select-filtered", "selection-clear", "selection-label-input", "selection-add-labels", "selection-add-project", "selection-remove-project", "selection-new-project", "selection-combine", "selection-analyze", "selection-project-target", "selection-trash", "open-settings", "settings-dialog", "settings-close", "settings-update-badge", "update-channel", "update-status", "update-checked-at", "update-available-version", "update-explanation", "check-extension-update", "apply-extension-update", "update-release-link", "update-feedback",
+ "add-menu", "export-path-setting", "media-file", "media-folder", "library-name-setting", "save-library-settings", "select-cases", "selection-select-filtered", "selection-clear", "selection-label-input", "selection-add-labels", "selection-add-project", "selection-remove-project", "selection-new-project", "selection-combine", "selection-analyze", "selection-video-analyze", "selection-project-target", "selection-trash", "open-settings", "settings-dialog", "settings-close", "settings-update-badge", "update-channel", "update-status", "update-checked-at", "update-available-version", "update-explanation", "check-extension-update", "apply-extension-update", "update-release-link", "update-feedback",
"project-section", "project-root-drop", "manage-project-order", "selection-simple-actions", "selection-selected-actions", "show-analysis-diagnostics", "ui-locale", "ui-theme", "ui-motion", "vocabulary-tree",
"vision-instructions-en", "vision-instructions-zh", "vision-protocol", "vision-settings-form", "vision-settings-status", "restore-vision-default",
"open-curated", "open-skills", "open-trash", "trash-count", "trash-dialog", "trash-close", "trash-list", "trash-feedback", "trash-restore-all", "trash-empty", "data-safety-dialog", "data-safety-count", "data-safety-status", "data-safety-feedback",
"sync-settings", "data-safety-password", "sync-password", "connect-sync-folder", "unlock-sync-vault", "sync-now", "cancel-sync", "create-folder-backup", "restore-folder-backup", "restore-library-replacement-point", "import-library-package", "library-package-file", "disconnect-sync-folder", "capture-web-permission-status", "capture-clipboard-permission-status", "revoke-capture-web-permission", "revoke-capture-clipboard-permission", "capture-permission-settings-feedback",
- "vision-batch-dialog", "vision-batch-close", "vision-batch-summary", "vision-batch-service", "vision-batch-all-images", "vision-batch-reanalyze",
+ "vision-batch-dialog", "vision-batch-close", "vision-batch-title", "vision-batch-summary", "vision-batch-service", "vision-batch-all-images", "vision-batch-reanalyze", "vision-batch-tags-option", "vision-batch-tags", "vision-batch-instruction-field", "vision-batch-instruction",
"vision-batch-progress", "vision-batch-progress-bar", "vision-batch-start", "vision-batch-pause", "vision-batch-resume", "vision-batch-retry", "vision-batch-cancel", "vision-batch-feedback",
"library-drop-target", "import-dialog", "import-dialog-title", "import-close", "import-source", "import-choose-files", "import-last-job", "import-actions", "import-preparing", "import-confirmation", "import-supported-count", "import-skipped-count", "import-duplicate-count", "import-byte-size", "import-project", "import-project-hint", "import-auto-analyze", "import-label-editor", "import-file-list", "import-feedback", "import-job-panel", "import-job-title", "import-job-count", "import-job-progress", "import-job-feedback", "import-cancel", "import-retry", "import-undo", "import-view-project", "import-start"
].map((id) => [camel(id), document.querySelector(`#${id}`)]));
@@ -308,7 +309,6 @@ let activeAnalysisLocale = currentLocale() === "en" ? "en" : "zh-CN";
let activeAnalysisKind = "text";
let activeSettingsTab = "general";
let activeAiRoutingTab = "tasks";
-let activeVideoAnalysisUi = null;
let selectionMode = "";
let projectSelectionId = "";
const selectedCaseIds = new Set();
@@ -318,6 +318,7 @@ let shareLocalAssetRecords = [];
let submissionDownloadIds = [];
let analysisBatchJob = null;
let visionBatchJob = null;
+let activeMediaBatchKind = "image";
let canUndoAnalysisBatch = false;
let analysisBatchPreview = null;
let analysisDiagnostics = [];
@@ -344,6 +345,10 @@ let syncStatus = {};
let dataSafetyOperationActive = false;
let dataSafetyOperationType = "";
const activeDetailMediaIdByEntry = new Map();
+const videoAnalysisTaskStateByAsset = new Map();
+const videoAnalysisTaskQueries = new Set();
+const videoAnalysisDrafts = new Map();
+const videoAnalysisIncludeTagsByAsset = new Map();
let promptEditState = null;
const visionStatusByEntry = new Map();
let activeFilterCount = 0;
@@ -672,8 +677,17 @@ function bindEvents() {
elements.visionBatchDialog.addEventListener("click", (event) => {
if (event.target === elements.visionBatchDialog) elements.visionBatchDialog.close();
});
- for (const checkbox of [elements.visionBatchAllImages, elements.visionBatchReanalyze]) {
- checkbox.addEventListener("change", previewSelectedVisionBatch);
+ for (const checkbox of [elements.visionBatchAllImages, elements.visionBatchReanalyze, elements.visionBatchTags]) {
+ checkbox.addEventListener("change", () => {
+ if (checkbox === elements.visionBatchTags && activeMediaBatchKind === "video") {
+ elements.visionBatchInstruction.value = videoAnalysisPrompt("visual-reconstruction", "", {
+ includeTags: checkbox.checked,
+ catalog: facetCatalog,
+ locale: currentLocale()
+ });
+ }
+ void previewSelectedVisionBatch();
+ });
}
elements.visionBatchStart.addEventListener("click", startSelectedVisionBatch);
elements.visionBatchPause.addEventListener("click", () => updateVisionBatchAction("PAUSE_VISION_BATCH"));
@@ -712,6 +726,7 @@ function bindEvents() {
elements.selectionRemoveProject.addEventListener("click", removeSelectionFromProject);
elements.selectionNewProject.addEventListener("click", createProjectFromSelection);
elements.selectionAnalyze.addEventListener("click", analyzeSelectedCases);
+ elements.selectionVideoAnalyze.addEventListener("click", analyzeSelectedVideos);
elements.selectionTrash.addEventListener("click", moveSelectionToTrash);
elements.selectionProjectTarget.addEventListener("change", () => {
selectionProjectTargetTouched = true;
@@ -2164,8 +2179,14 @@ function updateSelectionBar() {
elements.selectionNewProject.disabled = !selectedCaseIds.size;
const selectionHasAnalyzableImage = [...selectedCaseIds]
.some((id) => isVisionSelectableEntry(logicalCases.find((entry) => entry.id === id)));
+ const selectionHasAnalyzableVideo = [...selectedCaseIds].some((id) => {
+ const entry = logicalCases.find((item) => item.id === id);
+ return entry && entryMediaAssets(entry).some((asset) => asset.kind === "video" && asset.usage !== "poster" && asset.storageMode === "managed");
+ });
elements.selectionAnalyze.disabled = !selectionHasAnalyzableImage;
elements.selectionAnalyze.title = selectionHasAnalyzableImage ? "" : t("所选案例中没有可分析的图片");
+ elements.selectionVideoAnalyze.disabled = !selectionHasAnalyzableVideo;
+ elements.selectionVideoAnalyze.title = selectionHasAnalyzableVideo ? "" : t("所选案例中没有可分析的本地视频");
elements.shareExport.textContent = t("分享");
elements.shareExport.disabled = !selectedCaseIds.size;
elements.selectionTrash.disabled = !selectedCaseIds.size;
@@ -2302,6 +2323,7 @@ async function createProjectFromSelection() {
}
async function analyzeSelectedCases() {
+ activeMediaBatchKind = "image";
const eligibleIds = [...selectedCaseIds].filter((id) => {
const entry = logicalCases.find((item) => item.id === id);
return entry && isVisionSelectableEntry(entry);
@@ -2312,6 +2334,20 @@ async function analyzeSelectedCases() {
await openVisionBatchConfirmation();
}
+async function analyzeSelectedVideos() {
+ activeMediaBatchKind = "video";
+ const eligibleIds = [...selectedCaseIds].filter((id) => {
+ const entry = logicalCases.find((item) => item.id === id);
+ return entry && entryMediaAssets(entry).some((asset) =>
+ asset.kind === "video" && asset.usage !== "poster" && asset.storageMode === "managed"
+ );
+ });
+ if (!eligibleIds.length) return showFeedback("所选案例中没有可分析的本地视频", true);
+ selectedCaseIds.clear();
+ eligibleIds.forEach((id) => selectedCaseIds.add(id));
+ await openVisionBatchConfirmation();
+}
+
function completeTaskSelection() {
if (selectionMode === "vision") return openVisionBatchConfirmation();
if (selectionMode === "combine") return saveCompoundSelection();
@@ -2651,18 +2687,54 @@ async function openRequestedLibraryTarget() {
}
async function openVisionBatchConfirmation() {
- if (!selectedCaseIds.size) return showFeedback("请先选择至少一个带图片的案例", true);
+ if (!selectedCaseIds.size) return showFeedback(t(activeMediaBatchKind === "video" ? "请先选择至少一个带本地视频的案例" : "请先选择至少一个带图片的案例"), true);
elements.visionBatchFeedback.textContent = "";
elements.visionBatchAllImages.checked = false;
elements.visionBatchReanalyze.checked = false;
+ elements.visionBatchTags.checked = true;
+ const videoMode = activeMediaBatchKind === "video";
+ elements.visionBatchTitle.textContent = t(videoMode ? "批量分析视频" : "批量分析图片");
+ elements.visionBatchAllImages.nextElementSibling.textContent = t(videoMode ? "包含案例中的全部视频" : "分析所选案例中的全部未分析图片");
+ elements.visionBatchReanalyze.nextElementSibling.textContent = t(videoMode ? "包含已有 AI 视觉逆推的视频" : "重新分析已有反推提示词");
+ elements.visionBatchTagsOption.hidden = !videoMode;
+ elements.visionBatchInstructionField.hidden = !videoMode;
+ if (videoMode) {
+ elements.visionBatchInstruction.value = videoAnalysisPrompt("visual-reconstruction", "", {
+ includeTags: true,
+ catalog: facetCatalog,
+ locale: currentLocale()
+ });
+ }
await previewSelectedVisionBatch();
if (!elements.visionBatchDialog.open) elements.visionBatchDialog.showModal();
}
async function previewSelectedVisionBatch() {
if (visionBatchJob && ["running", "paused"].includes(visionBatchJob.status)) {
- const currentModel = visionSettings.activeProvider === "openai" ? visionSettings.openai.model : visionSettings.compatible.model;
- if (visionBatchJob.providerType !== visionSettings.activeProvider || visionBatchJob.model !== currentModel) {
+ const requestedKind = activeMediaBatchKind === "video" ? "video" : "vision";
+ if (visionBatchJob.kind !== requestedKind) {
+ activeMediaBatchKind = visionBatchJob.kind === "video" ? "video" : "image";
+ const videoMode = activeMediaBatchKind === "video";
+ elements.visionBatchTitle.textContent = t(videoMode ? "批量分析视频" : "批量分析图片");
+ elements.visionBatchTagsOption.hidden = !videoMode;
+ elements.visionBatchInstructionField.hidden = !videoMode;
+ showVisionBatchFeedback("已有未完成的批量任务,请继续或取消后再创建另一类任务");
+ renderVisionBatchDialog();
+ return;
+ }
+ const assignment = activeMediaBatchKind === "video" ? aiTaskAssignments.videoAnalysis : aiTaskAssignments.imageAnalysis;
+ const currentModel = activeMediaBatchKind === "video"
+ ? assignment?.model || ""
+ : visionSettings.activeProvider === "openai" ? visionSettings.openai.model : visionSettings.compatible.model;
+ const providerChanged = activeMediaBatchKind === "video"
+ ? visionBatchJob.kind !== "video" || visionBatchJob.providerId !== assignment?.providerId
+ : visionBatchJob.kind !== "vision" || visionBatchJob.providerType !== visionSettings.activeProvider;
+ if (providerChanged || visionBatchJob.model !== currentModel) {
+ if (activeMediaBatchKind === "video") {
+ showVisionBatchFeedback("视频分析路由已经变化;旧任务不会切换模型,请先取消后重新预览", true);
+ renderVisionBatchDialog();
+ return;
+ }
try {
const response = await chrome.runtime.sendMessage({ type: "CANCEL_VISION_BATCH", jobId: visionBatchJob.id });
if (!response?.ok) throw new Error(response?.message || "无法取消旧批量任务");
@@ -2677,12 +2749,14 @@ async function previewSelectedVisionBatch() {
return;
}
const response = await chrome.runtime.sendMessage({
- type: "PREVIEW_VISION_BATCH",
+ type: activeMediaBatchKind === "video" ? "PREVIEW_VIDEO_BATCH" : "PREVIEW_VISION_BATCH",
entryIds: expandLogicalCaseIds([...selectedCaseIds], compoundCases),
- includeAllImages: elements.visionBatchAllImages.checked,
- reanalyze: elements.visionBatchReanalyze.checked
+ includeAllImages: activeMediaBatchKind !== "video" && elements.visionBatchAllImages.checked,
+ includeAllVideos: activeMediaBatchKind === "video" && elements.visionBatchAllImages.checked,
+ reanalyze: elements.visionBatchReanalyze.checked,
+ includeTags: elements.visionBatchTags.checked
});
- if (!response?.ok) return showVisionBatchFeedback(response?.message || "无法生成图片分析预览", true);
+ if (!response?.ok) return showVisionBatchFeedback(response?.message || (activeMediaBatchKind === "video" ? "无法生成视频分析预览" : "无法生成图片分析预览"), true);
renderVisionBatchDialog(response.preview);
}
@@ -2691,6 +2765,7 @@ function renderVisionBatchDialog(preview = null) {
const job = activeJob ?? (preview ? null : visionBatchJob);
const active = Boolean(activeJob);
const source = activeJob ?? preview ?? visionBatchJob ?? {};
+ const videoMode = source.kind === "video" || activeMediaBatchKind === "video";
const counts = job?.counts ?? {};
const requestCount = job?.requestCount ?? preview?.requestCount ?? 0;
const caseCount = preview?.caseCount ?? new Set(job?.items?.map((item) => item.entryId) ?? []).size;
@@ -2706,12 +2781,32 @@ function renderVisionBatchDialog(preview = null) {
: currentLocale() === "en"
? `${counts.succeeded ?? 0}/${requestCount} completed · ${counts.failed ?? 0} failed · ${job.requestAttempts || 0} requests · ${job.cacheHitCount || 0} cache hits${analysisFailureCategorySummary(job, "en") ? ` · ${analysisFailureCategorySummary(job, "en")}` : ""}`
: `${counts.succeeded ?? 0}/${requestCount} 已完成 · ${counts.failed ?? 0} 失败 · 请求 ${job.requestAttempts || 0} 次 · 缓存 ${job.cacheHitCount || 0} 次${analysisFailureCategorySummary(job, "zh-CN") ? ` · ${analysisFailureCategorySummary(job, "zh-CN")}` : ""}`
- : currentLocale() === "en"
- ? `${caseCount} cases · ${requestCount} image requests`
- : `${caseCount} 个案例 · ${requestCount} 次图片请求`;
- elements.visionBatchService.textContent = [source.providerType, source.model].filter(Boolean).join(" · ");
+ : videoMode
+ ? `${caseCount} 个案例 · ${requestCount} 支视频 / ${requestCount} 次请求 · ${formatBytes(source.totalBytes || 0)} · 已知时长 ${formatMediaTime(source.knownDurationMs || 0)}${source.unknownDurationCount ? ` · ${source.unknownDurationCount} 支时长未知` : ""} · 并发 ${source.concurrency || 2}`
+ : currentLocale() === "en"
+ ? `${caseCount} cases · ${requestCount} image requests`
+ : `${caseCount} 个案例 · ${requestCount} 次图片请求`;
+ if (videoMode && source.excludedCount) {
+ const reasons = [...new Set((source.exclusions ?? []).map((item) => translateUiMessage(item.reason)).filter(Boolean))];
+ const categories = source.exclusionCounts ?? {};
+ const categorySummary = currentLocale() === "en"
+ ? [`${categories.busy || 0} busy`, `${categories.missing || 0} missing`, `${categories.incompatible || 0} incompatible`, `${categories.non_local || 0} non-local`].filter((item) => !item.startsWith("0 ")).join(" · ")
+ : [`处理中 ${categories.busy || 0}`, `文件缺失 ${categories.missing || 0}`, `格式不兼容 ${categories.incompatible || 0}`, `非本地视频 ${categories.non_local || 0}`].filter((item) => !item.endsWith(" 0")).join(" · ");
+ elements.visionBatchSummary.textContent += t(" · 排除 {count} 支:{reasons}", {
+ count: source.excludedCount,
+ reasons: categorySummary || reasons.join(";") || t("来源或当前状态不符合本次批量条件")
+ });
+ }
+ if (videoMode && job?.counts?.succeeded) {
+ elements.visionBatchSummary.textContent += job.unknownCostCount
+ ? ` · 服务商未返回 ${job.unknownCostCount} 项费用`
+ : ` · 服务返回费用 ${job.totalCost}`;
+ }
+ elements.visionBatchService.textContent = [source.providerId || source.providerType, source.model, videoMode ? t(source.includeTags === false ? "不生成 AI 标签" : "同时生成 AI 标签") : ""].filter(Boolean).join(" · ");
elements.visionBatchAllImages.disabled = Boolean(active);
elements.visionBatchReanalyze.disabled = Boolean(active);
+ elements.visionBatchTags.disabled = Boolean(active);
+ elements.visionBatchInstruction.disabled = Boolean(active);
elements.visionBatchProgress.hidden = !job;
elements.visionBatchProgressBar.max = Math.max(1, requestCount || job?.requestCount || 1);
elements.visionBatchProgressBar.value = Math.min(processedCount, elements.visionBatchProgressBar.max);
@@ -2722,7 +2817,7 @@ function renderVisionBatchDialog(preview = null) {
elements.visionBatchRetry.hidden = !job || !counts.failed || job.status === "running";
elements.visionBatchRetry.textContent = currentLocale() === "en"
? `Retry failed (${counts.failed || 0} expected items)`
- : `重试失败项(预计新增 ${counts.failed || 0} 项)`;
+ : `重试失败项(预计新增 ${counts.failed || 0} 次请求)`;
elements.visionBatchCancel.hidden = !active;
if (job && !active) {
const total = job.usage?.totalTokens ?? 0;
@@ -2738,14 +2833,33 @@ function renderVisionBatchDialog(preview = null) {
async function startSelectedVisionBatch() {
elements.visionBatchStart.disabled = true;
try {
- const response = await chrome.runtime.sendMessage({
- type: "CREATE_VISION_BATCH",
+ const payload = {
+ type: activeMediaBatchKind === "video" ? "CREATE_VIDEO_BATCH" : "CREATE_VISION_BATCH",
entryIds: expandLogicalCaseIds([...selectedCaseIds], compoundCases),
- includeAllImages: elements.visionBatchAllImages.checked,
+ includeAllImages: activeMediaBatchKind !== "video" && elements.visionBatchAllImages.checked,
+ includeAllVideos: activeMediaBatchKind === "video" && elements.visionBatchAllImages.checked,
reanalyze: elements.visionBatchReanalyze.checked,
+ includeTags: elements.visionBatchTags.checked,
+ instruction: elements.visionBatchInstruction.value,
outputLocale: currentLocale()
- });
- if (!response?.ok) throw new Error(response?.message || "无法创建批量画面分析");
+ };
+ let response = await chrome.runtime.sendMessage(payload);
+ if (response?.requiresExclusionConfirmation) {
+ renderVisionBatchDialog(response.preview);
+ const reasons = [...new Set((response.preview?.exclusions ?? []).map((item) => translateUiMessage(item.reason)).filter(Boolean))];
+ const categories = response.preview?.exclusionCounts ?? {};
+ const categorySummary = [`处理中 ${categories.busy || 0}`, `文件缺失 ${categories.missing || 0}`, `格式不兼容 ${categories.incompatible || 0}`, `非本地视频 ${categories.non_local || 0}`]
+ .filter((item) => !item.endsWith(" 0"))
+ .join(";");
+ const confirmed = await confirmAppAction({
+ title: t("有 {count} 支视频不会发送", { count: response.preview?.excludedCount || 0 }),
+ description: categorySummary || reasons.join(";") || t("来源或当前状态不符合本次批量条件"),
+ confirmLabel: t("只分析其余视频")
+ });
+ if (!confirmed) return;
+ response = await chrome.runtime.sendMessage({ ...payload, confirmExclusions: true });
+ }
+ if (!response?.ok) throw new Error(response?.message || (activeMediaBatchKind === "video" ? "无法创建批量视频分析" : "无法创建批量画面分析"));
visionBatchJob = response.visionBatchJob;
renderVisionBatchDialog();
} catch (error) {
@@ -2758,7 +2872,37 @@ async function startSelectedVisionBatch() {
async function updateVisionBatchAction(type) {
if (!visionBatchJob) return;
try {
- const response = await chrome.runtime.sendMessage({ type, jobId: visionBatchJob.id });
+ let retryOptions = {};
+ if (type === "RETRY_VISION_BATCH_FAILURES" && visionBatchJob.kind === "video") {
+ const entryById = new Map(entries.map((entry) => [entry.id, entry]));
+ const failedItems = (visionBatchJob.items ?? []).filter((item) => item.status === "failed");
+ const choice = await showAppDialog({
+ title: `选择要重新发送的视频`,
+ description: "每个勾选项会创建一次新的模型请求;已成功结果不会重跑。状态未知项可能已经被服务商接收。",
+ confirmLabel: "重新发送所选项",
+ fields: failedItems.map((item, index) => ({
+ id: `retry_${index}`,
+ type: "checkbox",
+ value: true,
+ label: (() => {
+ const entry = entryById.get(item.entryId);
+ const asset = entry ? entryMediaAssets(entry).find((candidate) => candidate.id === item.assetId) : null;
+ return `${entry?.title || item.entryId} · ${asset?.sourceTitle || item.assetId}`;
+ })(),
+ help: /状态未知|可能已收到/.test(item.error)
+ ? `${item.error};重新发送可能重复计费`
+ : item.error
+ }))
+ });
+ if (choice === null) return;
+ const selectedItems = failedItems.filter((_, index) => choice[`retry_${index}`] === true);
+ if (!selectedItems.length) return showVisionBatchFeedback("请至少选择一支要重新发送的视频", true);
+ retryOptions = {
+ itemKeys: selectedItems.map((item) => `${item.entryId}\u0000${item.assetId}`),
+ confirmDuplicateCharge: selectedItems.some((item) => /状态未知|可能已收到/.test(item.error))
+ };
+ }
+ const response = await chrome.runtime.sendMessage({ type, jobId: visionBatchJob.id, ...retryOptions });
if (!response?.ok) throw new Error(response?.message || "无法更新批量任务");
visionBatchJob = response.visionBatchJob;
showVisionBatchFeedback(response.message);
@@ -4744,7 +4888,6 @@ async function createDetailMediaGallery(entryValue, { immersive = false } = {})
const stage = el("div", "detail-visual-stage");
const rail = el("div", "detail-visual-rail");
const notes = el("section", "time-notes");
- const videoAnalysis = el("section", "video-analysis-history");
rail.setAttribute("aria-label", "案例媒体");
const imageUrls = await Promise.all(entry.mediaAssets.map((asset) =>
asset.kind === "image" && asset.storageMode === "managed" ? originalScreenshotUrl(asset.id) : ""
@@ -4838,7 +4981,7 @@ async function createDetailMediaGallery(entryValue, { immersive = false } = {})
}
activeController = body.mediaController || (localVideo ? localVideoController(localVideo) : null);
const caption = el("figcaption", "detail-visual-caption");
- const position = asset.id === entry.primaryMediaId ? `主要媒体 · ${activeIndex + 1}/${entry.mediaAssets.length}` : `${activeIndex + 1}/${entry.mediaAssets.length}`;
+ const position = asset.id === entry.primaryMediaId ? `${t("主要媒体")} · ${activeIndex + 1}/${entry.mediaAssets.length}` : `${activeIndex + 1}/${entry.mediaAssets.length}`;
caption.append(rawTextEl("span", "", [position, mediaMetadataText(asset)].filter(Boolean).join(" · ")));
const actions = el("span", "detail-visual-actions");
if (!entry.compoundCase && asset.id !== entry.primaryMediaId) {
@@ -4876,7 +5019,6 @@ async function createDetailMediaGallery(entryValue, { immersive = false } = {})
else body.addEventListener("load", () => syncImageStageSize(asset, body), { once: true });
}
renderTimeNotes(notes, entry, asset, () => activeController);
- renderVideoAnalysisPanel(videoAnalysis, entry, asset, () => activeController);
for (const [index, button] of [...rail.children].entries()) {
button.classList.toggle("is-active", index === activeIndex);
button.setAttribute("aria-pressed", String(index === activeIndex));
@@ -4918,7 +5060,7 @@ async function createDetailMediaGallery(entryValue, { immersive = false } = {})
}
gallery.append(stage);
if (entry.mediaAssets.length > 1) gallery.append(rail);
- gallery.append(notes, videoAnalysis);
+ gallery.append(notes);
await renderActive();
return gallery;
}
@@ -4936,34 +5078,129 @@ function refreshActiveDetailAssetSections(entry) {
}
function handleVideoAnalysisProgress(message) {
- if (message?.type !== "VIDEO_ANALYSIS_CHANGED" || !activeVideoAnalysisUi) return;
- if (message.entryId !== activeVideoAnalysisUi.entryId || message.assetId !== activeVideoAnalysisUi.assetId) return;
- const label = ({ uploading: "正在上传本地视频…", processing: "服务正在处理视频…", analyzing: "正在理解画面与声音…", completed: "分析完成,正在保存版本…" })[message.phase];
- if (label) activeVideoAnalysisUi.status.textContent = `${label} · ${message.provider} · ${message.model}`;
+ if (message?.type === "VIDEO_ANALYSIS_CHANGED") {
+ const key = videoAnalysisAssetKey(message.entryId, message.assetId);
+ const current = videoAnalysisTaskStateByAsset.get(key);
+ if (current) videoAnalysisTaskStateByAsset.set(key, { ...current, phase: message.phase });
+ if (currentDetailId === message.entryId) void renderDetail();
+ return;
+ }
+ if (message?.type !== "ANALYSIS_TASK_UPDATED" || message.task?.request?.kind !== "entry_video") return;
+ const task = message.task;
+ videoAnalysisTaskStateByAsset.set(videoAnalysisAssetKey(task.request.entryId, task.request.assetId), task);
+ if (task.status === "completed") void refreshLibrary();
+ else if (currentDetailId === task.request.entryId) void renderDetail();
}
-function renderVideoAnalysisPanel(container, entry, asset, getController) {
- if (asset.kind !== "video" || entry.compoundCase) return container.replaceChildren();
+function videoAnalysisAssetKey(entryId, assetId) {
+ return `${entryId}:${assetId}`;
+}
+
+function videoAnalysisDraftKey(entry, asset, mode, includeTags = false) {
+ return `${videoAnalysisAssetKey(entry.id, asset.id)}:${mode}:${includeTags ? "tags" : "plain"}`;
+}
+
+function defaultVideoAnalysisInstruction(entry, asset, mode, includeTags) {
+ if (mode === "custom") return "";
+ return videoAnalysisPrompt(mode, "", {
+ includeTags,
+ catalog: facetCatalog,
+ locale: currentLocale(),
+ durationMs: asset.durationMs,
+ width: asset.width,
+ height: asset.height
+ });
+}
+
+function requestVideoAnalysisTaskState(entry, asset) {
+ const key = videoAnalysisAssetKey(entry.id, asset.id);
+ if (videoAnalysisTaskQueries.has(key)) return;
+ videoAnalysisTaskQueries.add(key);
+ chrome.runtime.sendMessage({
+ type: "GET_ENTRY_VIDEO_ANALYSIS_TASK",
+ entryId: entry.id,
+ assetId: asset.id
+ }).then((response) => {
+ if (response?.ok && response.task) videoAnalysisTaskStateByAsset.set(key, response.task);
+ if (currentDetailId === entry.id) return renderDetail();
+ }).catch(() => undefined);
+}
+
+function createVideoAnalysisWorkspace(entry, asset) {
+ requestVideoAnalysisTaskState(entry, asset);
+ const workspace = el("div", "video-analysis-workspace");
+ const assignment = aiTaskAssignments.videoAnalysis ?? {};
+ const provider = aiProviderRegistry.providers?.[assignment.providerId];
+ const taskKey = videoAnalysisAssetKey(entry.id, asset.id);
+ const task = videoAnalysisTaskStateByAsset.get(taskKey);
+ const running = ["queued", "running"].includes(task?.status);
+ const includeTags = videoAnalysisIncludeTagsByAsset.get(taskKey) !== false;
const header = el("div", "video-analysis-header");
- const title = textEl("h3", "", "视频分析");
- const analyze = textEl("button", "", "分析视频");
- analyze.addEventListener("click", () => startVideoAnalysis(entry, asset));
- header.append(title, analyze);
- const records = (entry.videoAnalyses ?? []).filter((item) => !item.assetId || item.assetId === asset.id).slice().reverse();
- const list = el("div", "video-analysis-list");
- for (const [index, record] of records.entries()) {
- const item = el("article", "video-analysis-record");
- const heading = el("header", "");
- heading.append(
- rawTextEl("strong", "", `${videoAnalysisModeLabel(record.mode)} · 版本 ${records.length - index}`),
- rawTextEl("span", "", `${record.provider || t("未知服务")} · ${record.model || t("未知模型")} · ${formatDate(record.createdAt)}`)
- );
- item.append(heading, renderTimestampedAnalysis(record.text, entry, asset, getController));
- if (record.usage?.totalTokens) item.append(rawTextEl("small", "", `本次用量:${record.usage.totalTokens} tokens`));
- list.append(item);
+ header.append(
+ textEl("h3", "", "视频分析"),
+ rawTextEl("small", "", provider && assignment.model
+ ? `${providerDisplayLabel(provider)} · ${assignment.model} · ${t("每次点击发送 1 次请求,费用由服务商账户产生")}`
+ : t("尚未分配视频分析服务"))
+ );
+ workspace.append(header);
+
+ const actions = el("div", "video-analysis-actions");
+ for (const mode of ["visual-reconstruction", "creative-breakdown", "ad-review", "custom"]) {
+ const row = el("div", "video-analysis-action");
+ const modeIncludeTags = mode === "visual-reconstruction" && includeTags;
+ const draftKey = videoAnalysisDraftKey(entry, asset, mode, modeIncludeTags);
+ const textarea = document.createElement("textarea");
+ textarea.rows = mode === "visual-reconstruction" ? 2 : 1;
+ textarea.className = "video-analysis-instruction";
+ textarea.value = videoAnalysisDrafts.has(draftKey)
+ ? videoAnalysisDrafts.get(draftKey)
+ : defaultVideoAnalysisInstruction(entry, asset, mode, modeIncludeTags);
+ textarea.placeholder = t(mode === "custom" ? "输入只针对这支视频的具体问题" : "本次分析指令");
+ textarea.setAttribute("aria-label", t("{mode}指令", { mode: videoAnalysisModeLabel(mode) }));
+ textarea.addEventListener("input", () => videoAnalysisDrafts.set(draftKey, textarea.value));
+ const run = textEl("button", mode === "visual-reconstruction" ? "" : "button-secondary",
+ mode === "visual-reconstruction" && currentVideoReconstruction(entry, asset.id) ? "重新逆推" : videoAnalysisModeLabel(mode));
+ run.disabled = running;
+ run.addEventListener("click", async () => {
+ try {
+ const selectedTags = mode === "visual-reconstruction" && videoAnalysisIncludeTagsByAsset.get(taskKey) !== false;
+ await startVideoAnalysis(entry, asset, mode, textarea.value, selectedTags, run);
+ } catch (error) {
+ run.disabled = false;
+ showFeedback(translateUiMessage(error?.message) || t("视频分析任务创建失败"), true);
+ }
+ });
+ const controls = el("div", "video-analysis-action-controls");
+ controls.append(run);
+ if (mode === "visual-reconstruction") {
+ const tagToggle = document.createElement("label");
+ tagToggle.className = "video-analysis-tag-toggle";
+ const checkbox = document.createElement("input");
+ checkbox.type = "checkbox";
+ checkbox.checked = includeTags;
+ checkbox.disabled = running;
+ checkbox.addEventListener("change", () => {
+ videoAnalysisDrafts.set(draftKey, textarea.value);
+ videoAnalysisIncludeTagsByAsset.set(taskKey, checkbox.checked);
+ const nextKey = videoAnalysisDraftKey(entry, asset, mode, checkbox.checked);
+ textarea.value = videoAnalysisDrafts.has(nextKey)
+ ? videoAnalysisDrafts.get(nextKey)
+ : defaultVideoAnalysisInstruction(entry, asset, mode, checkbox.checked);
+ });
+ tagToggle.append(checkbox, document.createTextNode(t("同时生成 AI 标签")));
+ controls.append(tagToggle);
+ }
+ row.append(controls, textarea);
+ actions.append(row);
}
- if (!records.length) list.append(textEl("p", "video-analysis-empty", "尚无分析记录。每次重跑都会保存为新版本。"));
- container.replaceChildren(header, list);
+ workspace.append(actions);
+
+ if (task) workspace.append(createVideoAnalysisTaskStatus(task));
+ const current = currentVideoReconstruction(entry, asset.id);
+ if (current) workspace.append(createCurrentVideoReconstruction(entry, asset, current));
+ const history = createVideoAnalysisHistory(entry, asset, current?.id || "", task);
+ if (history) workspace.append(history);
+ return workspace;
}
function renderTimestampedAnalysis(text, entry, asset, getController) {
@@ -4990,29 +5227,167 @@ function renderTimestampedAnalysis(text, entry, asset, getController) {
return body;
}
-async function startVideoAnalysis(entry, asset) {
+function activeDetailVideoController() {
+ const video = elements.detailContent.querySelector(".detail-visual-stage video");
+ return video ? { seekToMs: async (milliseconds) => { video.currentTime = milliseconds / 1000; } } : null;
+}
+
+function createVideoAnalysisTaskStatus(task) {
+ const status = el("div", "video-analysis-task-status");
+ const attempt = task.attempts?.find((item) => item.id === task.activeAttemptId) || task.attempts?.at(-1);
+ if (["queued", "running"].includes(task.status)) {
+ const phase = task.phase || (task.status === "queued" ? "queued" : "analyzing");
+ const label = ({
+ queued: "正在排队…",
+ encoding: "正在编码本地视频…",
+ uploading: "正在上传…",
+ processing: "服务正在处理…",
+ analyzing: "正在分析画面…",
+ completed: "分析完成,正在保存…"
+ })[phase] || "正在分析画面…";
+ status.append(rawTextEl("span", "", t(label)));
+ const stop = textEl("button", "button-secondary", "停止");
+ stop.addEventListener("click", async () => {
+ stop.disabled = true;
+ const response = await chrome.runtime.sendMessage({ type: "STOP_ANALYSIS_TASK", taskId: task.id });
+ if (!response?.ok) showFeedback(translateUiMessage(response?.message) || t("停止失败"), true);
+ else {
+ videoAnalysisTaskStateByAsset.set(videoAnalysisAssetKey(task.request.entryId, task.request.assetId), response.task);
+ await renderDetail();
+ }
+ });
+ status.append(stop);
+ return status;
+ }
+ if (["failed", "stopped"].includes(task.status)) {
+ const unknown = task.executionState === "execution_state_unknown";
+ const message = unknown
+ ? "上次执行状态未知,未自动重试;服务商可能已收到请求"
+ : attempt?.error || (task.providerMayHaveAccepted ? "已停止等待,未保存结果;服务商可能已收到请求" : "已取消,未发送");
+ status.append(rawTextEl("span", "", t(message)));
+ const retry = textEl("button", "button-secondary", "重新发送");
+ retry.addEventListener("click", async () => {
+ if (!await confirmAppAction({
+ title: "重新发送这次视频分析?",
+ description: unknown || task.providerMayHaveAccepted
+ ? "服务商可能已收到上一次请求,重新发送可能再次计费。"
+ : "将创建一次新的模型请求,不会覆盖已经保存的结果。",
+ confirmLabel: "重新发送"
+ })) return;
+ retry.disabled = true;
+ const response = await chrome.runtime.sendMessage({
+ type: "RETRY_ANALYSIS_TASK",
+ taskId: task.id,
+ previousAttemptId: attempt?.id || "",
+ confirmDuplicateCharge: true,
+ consumerId: `library-detail:${task.request.entryId}:${task.request.assetId}`,
+ clientRequestId: `video-retry:${crypto.randomUUID()}`
+ });
+ if (!response?.ok) showFeedback(translateUiMessage(response?.message) || t("重新发送失败"), true);
+ else {
+ videoAnalysisTaskStateByAsset.set(videoAnalysisAssetKey(task.request.entryId, task.request.assetId), response.task);
+ await renderDetail();
+ }
+ });
+ status.append(retry);
+ return status;
+ }
+ return document.createDocumentFragment();
+}
+
+function createCurrentVideoReconstruction(entry, asset, record) {
+ const card = el("article", "video-reconstruction-current");
+ const heading = el("div", "video-analysis-record-heading");
+ heading.append(
+ textEl("h4", "", "AI 视觉逆推提示词"),
+ rawTextEl("small", "", [record.provider, record.model, formatDate(record.createdAt), record.userEdited ? t("已人工修改") : "", t("仅分析画面,未分析声音")].filter(Boolean).join(" · "))
+ );
+ const textarea = document.createElement("textarea");
+ textarea.className = "video-reconstruction-editor";
+ textarea.rows = 7;
+ textarea.value = record.reconstructionPrompt;
+ textarea.setAttribute("aria-label", t("编辑 AI 视觉逆推提示词"));
+ const save = textEl("button", "button-secondary", "保存修改");
+ save.disabled = true;
+ textarea.addEventListener("input", () => { save.disabled = textarea.value.trim() === record.reconstructionPrompt; });
+ save.addEventListener("click", async () => {
+ save.disabled = true;
+ const response = await chrome.runtime.sendMessage({
+ type: "UPDATE_VIDEO_RECONSTRUCTION_PROMPT",
+ entryId: entry.id,
+ assetId: asset.id,
+ reconstructionPrompt: textarea.value
+ });
+ if (!response?.ok) {
+ showFeedback(translateUiMessage(response?.message) || t("AI 视觉逆推提示词保存失败"), true);
+ save.disabled = false;
+ return;
+ }
+ showFeedback(response.message);
+ await refreshLibrary();
+ });
+ card.append(heading, textarea, save);
+ if (record.tags?.length) {
+ const tags = el("div", "detail-tags video-analysis-tags");
+ for (const tag of record.tags) {
+ const pill = rawTextEl("span", "attribute-pill", tag.t || tag.g);
+ pill.style.setProperty("--facet-color", "var(--accent)");
+ tags.append(pill);
+ }
+ card.append(tags);
+ }
+ if (record.uncertainties?.length) {
+ const uncertainties = el("details", "video-analysis-uncertainties");
+ uncertainties.append(rawTextEl("summary", "", t("不确定项({count})", { count: record.uncertainties.length })));
+ const list = document.createElement("ul");
+ for (const item of record.uncertainties) list.append(rawTextEl("li", "", item));
+ uncertainties.append(list);
+ card.append(uncertainties);
+ }
+ return card;
+}
+
+function createVideoAnalysisHistory(entry, asset, currentId, task) {
+ const records = (entry.videoAnalyses ?? [])
+ .filter((item) => (!item.assetId || item.assetId === asset.id) && item.id !== currentId)
+ .toReversed();
+ if (!records.length) return null;
+ const details = el("details", "video-analysis-history");
+ details.open = task?.status === "completed" && task.request?.mode !== "visual-reconstruction";
+ details.append(rawTextEl("summary", "", t("历史分析({count})", { count: records.length })));
+ const list = el("div", "video-analysis-list");
+ for (const record of records) {
+ const item = el("article", "video-analysis-record");
+ const heading = el("header", "");
+ heading.append(
+ rawTextEl("strong", "", t("{mode} · 版本 {version}", { mode: videoAnalysisModeLabel(record.mode), version: record.version || 1 })),
+ rawTextEl("span", "", `${record.provider || t("未知服务")} · ${record.model || t("未知模型")} · ${formatDate(record.createdAt)}${record.assetId ? "" : ` · ${t("未关联旧记录")}`}`)
+ );
+ item.append(heading, renderTimestampedAnalysis(record.reconstructionPrompt || record.text, entry, asset, activeDetailVideoController));
+ if (record.usage?.totalTokens) item.append(rawTextEl("small", "", t("本次用量:{tokens} tokens", { tokens: record.usage.totalTokens })));
+ list.append(item);
+ }
+ details.append(list);
+ return details;
+}
+
+async function startVideoAnalysis(entry, asset, mode, instructionValue, includeTags, button) {
const assignment = aiTaskAssignments.videoAnalysis ?? {};
const provider = aiProviderRegistry.providers?.[assignment.providerId];
- const model = assignment.model || provider?.models?.videoAnalysis || "";
- if (!provider?.configured || !provider.capabilities?.includes("videoAnalysis") || !model) {
- openSettingsDialog("ai");
+ const model = assignment.model || "";
+ if (!providerConnectionReady(provider) || !provider.capabilities?.includes("videoAnalysis") || !model) {
showFeedback("请先在 AI 服务与任务分工中连接并分配视频分析服务", true);
+ void openAiTaskAssignmentDialog("videoAnalysis");
return;
}
- if (provider.consent !== true) {
- openSettingsDialog("ai");
- showFeedback("请先允许所选 AI 服务接收本次明确提交的资料", true);
- return;
- }
+ const instruction = String(instructionValue ?? "").trim();
+ if (!instruction) return showFeedback(t(mode === "custom" ? "请先填写自定义问题" : "本次分析指令不能为空"), true);
const sourceUrl = asset.reference?.url || asset.sourceUrl;
const modelMediaInput = getAiModelCapability(provider.id, model)?.mediaInput ?? {};
const mediaInput = { ...(provider.mediaInput ?? {}), ...modelMediaInput };
- let sourceKind = asset.storageMode === "managed"
- ? "本地视频文件(将上传)"
- : asset.reference?.provider === "youtube" ? "公共 YouTube URL" : "当前视频链接";
if (provider.protocol === "chat_completions") {
try {
- const sourcePlan = chatCompletionsVideoSourcePlan({
+ chatCompletionsVideoSourcePlan({
providerLabel: provider.label,
videoUrl: sourceUrl,
hasLocalVideo: asset.storageMode === "managed",
@@ -5024,51 +5399,39 @@ async function startVideoAnalysis(entry, asset) {
preferPublicVideoUrl: mediaInput.preferPublicVideoUrl,
publicVideoUrl: mediaInput.publicVideoUrl
});
- sourceKind = sourcePlan.sourceKind === "local-video"
- ? "本地视频文件(将编码发送)"
- : mediaInput.publicVideoUrl === "direct" ? "公网视频文件直链" : "公共视频 URL";
} catch (error) {
showFeedback(error.message || "所选模型不能分析这个视频来源", true);
return;
}
}
- await showAppDialog({
- title: "分析视频",
- description: `来源:${sourceKind} · 服务:${provider.label} · 模型:${model}。将发送上述视频来源与本次问题;费用由服务商账户产生。`,
- fields: [
- { id: "mode", label: "分析方式", type: "select", value: "creative-breakdown", options: [
- { value: "creative-breakdown", label: "创意拆解" },
- { value: "content-summary", label: "内容总结" },
- { value: "ad-review", label: "广告评价" },
- { value: "custom", label: "自定义问题" }
- ] },
- { id: "customQuestion", label: "自定义问题(仅自定义时必填)", type: "textarea", rows: 4, placeholder: "例如:比较前后两段节奏,找出转化弱点" }
- ],
- confirmLabel: "开始分析",
- pendingLabel: "正在准备视频分析…",
- onSubmit: async (values, context) => {
- const permission = permissionPatternForProvider(provider.endpoint);
- if (!await chrome.permissions.request({ origins: [permission] })) throw new Error("没有获得所选 AI 服务的访问权限,未发送视频");
- const status = context.status();
- activeVideoAnalysisUi = { entryId: entry.id, assetId: asset.id, status };
- try {
- const response = await chrome.runtime.sendMessage({
- type: "ANALYZE_ENTRY_VIDEO", entryId: entry.id, assetId: asset.id,
- mode: values.mode, customQuestion: values.customQuestion, singleConfirmation: true
- });
- if (!response?.ok) throw new Error(response?.message || "视频分析失败");
- showFeedback(response.message);
- await refreshLibrary();
- return response;
- } finally {
- activeVideoAnalysisUi = null;
- }
- }
+ const permission = permissionPatternForProvider(provider.endpoint);
+ if (!await chrome.permissions.request({ origins: [permission] })) return showFeedback(t("没有获得所选 AI 服务的访问权限,未发送视频"), true);
+ button.disabled = true;
+ const fullInstruction = mode === "custom" ? videoAnalysisPrompt("custom", instruction) : instruction;
+ const response = await chrome.runtime.sendMessage({
+ type: "START_OR_JOIN_ANALYSIS_TASK",
+ kind: "entry_video",
+ entryId: entry.id,
+ assetId: asset.id,
+ mode,
+ instruction: fullInstruction,
+ includeTags: mode === "visual-reconstruction" && includeTags,
+ outputLocale: currentLocale(),
+ priority: "interactive",
+ consumerId: `library-detail:${entry.id}:${asset.id}`,
+ clientRequestId: `video-analysis:${crypto.randomUUID()}`
});
+ if (!response?.ok) {
+ button.disabled = false;
+ showFeedback(translateUiMessage(response?.message) || t("视频分析任务创建失败"), true);
+ return;
+ }
+ videoAnalysisTaskStateByAsset.set(videoAnalysisAssetKey(entry.id, asset.id), response.task);
+ await renderDetail();
}
function videoAnalysisModeLabel(value) {
- return ({ "creative-breakdown": "创意拆解", "content-summary": "内容总结", "ad-review": "广告评价", custom: "自定义问题" })[value] || "视频分析";
+ return t(({ "visual-reconstruction": "逆推提示词", "creative-breakdown": "创意拆解", "content-summary": "内容总结", "ad-review": "广告审片", custom: "自定义问题" })[value] || "视频分析");
}
async function createMediaViewer(asset, imageUrl, entry) {
@@ -5385,7 +5748,7 @@ async function createPlaybackFallback(entry, asset, { providerLabel, reason, act
label.append(
textEl("small", "", providerLabel.toLocaleUpperCase("en-US")),
rawTextEl("strong", "", entry.title || asset.sourceTitle || `${providerLabel} 视频`),
- rawTextEl("span", "", reason)
+ rawTextEl("span", "", t(reason))
);
const actions = el("span", "platform-link-actions");
if (actionLabel && onAction) {
@@ -7191,6 +7554,7 @@ function createPromptSection(entry, options = {}) {
const section = el("section", "detail-section prompt-section");
const activeAssetId = activeDetailMediaIdByEntry.get(entry.id) || entry.primaryMediaId;
const activeImage = entryMediaAssets(entry).find((asset) => asset.id === activeAssetId && asset.kind === "image" && asset.usage !== "poster");
+ const activeVideo = entryMediaAssets(entry).find((asset) => asset.id === activeAssetId && asset.kind === "video" && asset.usage !== "poster");
const imageAssets = entryMediaAssets(entry).filter((asset) => asset.kind === "image" && asset.usage !== "poster");
const managedImageAssets = imageAssets.filter((asset) => asset.storageMode === "managed");
const separatesCurrentAndShared = Boolean(activeImage && (imageAssets.length > 1 || options.compoundMember));
@@ -7243,7 +7607,9 @@ function createPromptSection(entry, options = {}) {
return section;
}
const heading = el("div", "prompt-section-heading");
- heading.append(textEl("h3", "", activeImage
+ heading.append(textEl("h3", "", activeVideo
+ ? "原始提示词"
+ : activeImage
? separatesCurrentAndShared ? (mediaPrompt ? "当前图片提示词" : entry.text ? "当前图片 · 使用共享提示词" : "当前图片提示词") : "提示词"
: "提示词"));
const copy = textEl("button", "button-secondary", "复制提示词");
@@ -7253,7 +7619,7 @@ function createPromptSection(entry, options = {}) {
? textEl("button", "button-secondary", vision ? "重新分析主图" : "分析主图")
: null;
const analyze = textEl("button", "button-secondary", "分析检索标签");
- const edit = textEl("button", "button-secondary", separatesCurrentAndShared ? "编辑当前图片" : "编辑");
+ const edit = textEl("button", "button-secondary", activeVideo ? "编辑原始提示词" : separatesCurrentAndShared ? "编辑当前图片" : "编辑");
const editShared = separatesCurrentAndShared && entry.text ? textEl("button", "button-secondary", "编辑共享提示词") : null;
const promptReplacement = activeImage ? visualAnalysisPromptReplacement(entry, activeImage.id) : null;
const replaceAnalyzedPrompt = promptReplacement ? textEl("button", "button-secondary", "更新为 V2 提示词") : null;
@@ -7305,9 +7671,10 @@ function createPromptSection(entry, options = {}) {
section.append(
heading,
rawTextEl("pre", `prompt-text${displayedPrompt ? "" : " is-empty"}`, displayedPrompt || t("暂无提示词")),
- coreActions,
- analysisMenu
+ coreActions
);
+ if (activeVideo && !entry.compoundCase) section.append(createVideoAnalysisWorkspace(entry, activeVideo));
+ section.append(analysisMenu);
if (textAnalysisReason(entry) === "text_changed") section.append(textEl("small", "prompt-analysis-stale", "提示词已修改,需要重新分析标签"));
return section;
}
@@ -7960,15 +8327,26 @@ function renderAiRoutingSummary() {
const assignment = aiTaskAssignments[task.id] ?? {};
const profile = aiProviderRegistry.providers?.[assignment.providerId];
const row = el("div", "ai-assignment-row");
- const modelState = assignedModelState(profile, assignment.model);
+ const modelUnavailable = assignedModelUnavailable(profile, assignment.model);
+ const hasReadyProvider = Object.values(aiProviderRegistry.providers ?? {}).some((candidate) =>
+ providerConnectionReady(candidate) && taskModelOptions(candidate, task.id).length);
+ const routeState = !assignment.providerId
+ ? t(hasReadyProvider ? "未选择模型" : "未连接可用服务")
+ : !profile
+ ? t("连接需修复:服务已移除")
+ : !providerConnectionReady(profile)
+ ? `${providerDisplayLabel(profile)} · ${t("连接需修复:API Key 或发送授权无效")}`
+ : !assignment.model
+ ? t("未选择模型")
+ : modelUnavailable
+ ? `${providerDisplayLabel(profile)} · ${assignment.model} · ${t("已分配但模型不可用")}`
+ : `${providerDisplayLabel(profile)} · ${assignment.model}`;
const change = textEl("button", "button-secondary", assignment.providerId ? "更换" : "配置");
change.type = "button";
change.addEventListener("click", () => openAiTaskAssignmentDialog(task.id));
row.append(
rawTextEl("strong", "", t(task.label)),
- rawTextEl("span", "", !assignment.providerId
- ? t("尚未分配")
- : profile ? `${providerDisplayLabel(profile)} · ${assignment.model || t("模型未填写")}${modelState}` : t("已分配服务不存在")),
+ rawTextEl("span", "", routeState),
change
);
return row;
@@ -7994,7 +8372,6 @@ function providerConnectionLabel(profile = {}) {
function providerCatalogLabel(profile = {}) {
const models = profile.discoveredModels ?? [];
const unavailable = models.filter((model) => model.status === "unavailable").length;
- const unverified = models.filter((model) => model.status === "unverified").length;
if (profile.discovery?.error) {
return t("模型目录读取失败:{error};保留 {count} 个模型{unavailable}", {
error: translateUiMessage(profile.discovery.error),
@@ -8003,20 +8380,17 @@ function providerCatalogLabel(profile = {}) {
});
}
if (!profile.discovery?.discoveredAt) return t("模型目录未读取");
- return t("模型目录已读取 · {count} 个模型 · 尚未执行模型调用验证{unavailable}", {
+ return t("模型目录已读取 · {count} 个模型{unavailable}", {
count: models.length,
- unavailable: `${unavailable ? t(" · {count} 个下架或当前不可用", { count: unavailable }) : ""}${unverified ? t(" · {count} 个来自官方能力说明,账号可用性待实测", { count: unverified }) : ""}`
+ unavailable: unavailable ? t(" · {count} 个下架或当前不可用", { count: unavailable }) : ""
});
}
-function assignedModelState(profile, modelValue) {
+function assignedModelUnavailable(profile, modelValue) {
const model = String(modelValue ?? "").trim();
- if (!profile || !model) return "";
+ if (!profile || !model) return false;
const discovered = (profile.discoveredModels ?? []).find((item) => item.id === model);
- if (discovered?.status === "unavailable") return t("(已下架或当前不可用)");
- if (discovered?.status === "unverified") return t("(官方声明支持,账号可用性待实测)");
- if (profile.discovery?.discoveredAt && !discovered) return t("(当前目录不可用)");
- return "";
+ return discovered?.status === "unavailable" || Boolean(profile.discovery?.discoveredAt && !discovered);
}
async function openAiTaskAssignmentDialog(taskId) {
@@ -8034,27 +8408,29 @@ async function openAiTaskAssignmentDialog(taskId) {
? current.providerId : capable[0].id;
const modelOptions = (providerId) => taskModelOptions(aiProviderRegistry.providers?.[providerId], taskId);
const initialModels = modelOptions(selectedProviderId);
- const supportsBatchConcurrency = ["textTags", "imageAnalysis"].includes(taskId);
- const defaultConcurrency = ["textTags", "skillExtraction", "creativePlanning"].includes(taskId) ? 20 : 10;
+ const supportsBatchConcurrency = ["textTags", "imageAnalysis", "videoAnalysis"].includes(taskId);
+ const defaultConcurrency = taskId === "videoAnalysis"
+ ? 2
+ : ["textTags", "skillExtraction", "creativePlanning"].includes(taskId) ? 20 : 10;
const initialConcurrency = Number.isInteger(Number(current.concurrency)) ? Number(current.concurrency) : defaultConcurrency;
const result = await showAppDialog({
title: t(task.label),
- description: t("这里设置全局默认;创作台中的本轮切换不会改动此处。"),
+ description: t("这里更改该任务实际使用的服务与模型;创作台本轮切换只影响当前创作。"),
fields: [
{ id: "providerId", label: t("AI 服务"), type: "select", value: selectedProviderId, options: capable.map((profile) => ({ value: profile.id, label: providerDisplayLabel(profile) })) },
{ id: "model", label: t("模型"), type: "select", value: initialModels.some((item) => item.value === current.model) ? current.model : initialModels[0]?.value || "", options: initialModels },
...(supportsBatchConcurrency ? [
- { id: "concurrency", label: t("批量分析并发数"), type: "number", min: 2, step: 1, value: initialConcurrency, help: t("文字任务默认 20,图片和视频任务默认 10;调整只对新任务生效。") },
+ { id: "concurrency", label: t("批量分析并发数"), type: "number", min: 2, step: 1, value: initialConcurrency, help: t("文字任务默认 20,图片任务默认 10,视频任务初始为 2;调整只对新任务生效。") },
{ id: "highConcurrencyConfirmed", label: t("我确认高于 20 的并发可能明显增加瞬时费用、内存占用和错误数量"), type: "checkbox", value: false }
] : [])
],
- confirmLabel: t("保存任务默认"),
+ confirmLabel: t("保存任务路由"),
cancelLabel: t("取消"),
dismissOnBackdrop: false,
confirmDismissWhenDirty: true,
dirtyDismissMessage: t("有未保存的更改,再次关闭或取消将放弃这些更改"),
discardLabel: t("确认放弃"),
- onReady: ({ dialog, controls }) => {
+ onReady: ({ dialog, controls, status }) => {
localizeAiDialogClose(dialog);
const provider = controls.get("providerId");
const model = controls.get("model");
@@ -8107,14 +8483,14 @@ async function openAiTaskAssignmentDialog(taskId) {
}
const response = await chrome.runtime.sendMessage({
type: "UPDATE_AI_PROVIDER_CONFIGURATION",
- assignments: { ...aiTaskAssignments, [taskId]: { providerId: values.providerId, model: values.model, concurrency } }
+ assignments: { ...aiTaskAssignments, [taskId]: { providerId: values.providerId, model: values.model, concurrency, managedBy: "task" } }
});
- if (!response?.ok) throw new Error(translateUiMessage(response?.message) || t("任务默认保存失败"));
+ if (!response?.ok) throw new Error(translateUiMessage(response?.message) || t("任务路由保存失败"));
applyAiConfigurationResponse(response);
return response;
}
});
- if (result) showFeedback(t("{task}默认模型已保存", { task: t(task.label) }));
+ if (result) showFeedback(t("{task}任务路由已保存", { task: t(task.label) }));
}
async function openAiProviderDialog(initialProviderId = "") {
@@ -8167,7 +8543,21 @@ async function openAiProviderDialog(initialProviderId = "") {
{ value: "chat_completions", label: "OpenAI Chat Completions" }
] }
);
- fields.push(...profile.capabilities.map((taskId) => {
+ const analysisTaskIds = connectionAnalysisTaskIds(profile);
+ if (analysisTaskIds.length) {
+ const analysisOptions = connectionAnalysisModelOptions(profile);
+ const currentModel = connectionManagedModel(profile);
+ fields.push({
+ id: `provider_${key}_analysisModel`,
+ label: t("分析模型"),
+ type: custom ? "text" : "select",
+ value: custom ? currentModel || analysisTaskIds.map((taskId) => profile.models?.[taskId]).find(Boolean) || ""
+ : analysisOptions.some((option) => option.value === currentModel) ? currentModel : "",
+ options: [{ value: "", label: t("读取模型后选择") }, ...analysisOptions],
+ placeholder: custom ? t("填写这个兼容接口实际调用的模型") : ""
+ });
+ }
+ for (const taskId of ["imageGeneration", "videoGeneration"].filter((taskId) => profile.capabilities.includes(taskId))) {
const catalogRequired = profile.catalogRequiredTasks?.includes(taskId) === true;
const assignedModel = aiTaskAssignments?.[taskId]?.providerId === profile.id
? aiTaskAssignments[taskId].model
@@ -8176,15 +8566,15 @@ async function openAiProviderDialog(initialProviderId = "") {
|| (profile.id === "custom-media" && taskId === "imageGeneration" ? profile.imageGeneration?.model : "")
|| assignedModel;
const options = discoveredModelOptions(profile, taskId, catalogRequired ? "" : configuredModel);
- return {
+ fields.push({
id: `provider_${key}_model_${taskId}`,
label: t("{task}模型(高级)", { task: taskDisplayLabel(taskId) }),
type: catalogRequired || options.length ? "select" : "text",
value: options.some((option) => option.value === configuredModel) ? configuredModel : "",
options,
advanced: true
- };
- }));
+ });
+ }
if (profile.id === "custom-media") fields.push(
{ id: `provider_${key}_imageProtocol`, label: t("图片生成协议(高级)"), type: "select", value: profile.imageGeneration?.protocol || "none", advanced: true, options: [
{ value: "none", label: t("不启用图片生成") },
@@ -8234,7 +8624,7 @@ async function openAiProviderDialog(initialProviderId = "") {
details.append(summary, advancedBody);
body.append(details);
},
- onReady: ({ dialog, controls }) => {
+ onReady: ({ dialog, controls, status }) => {
localizeAiDialogClose(dialog);
const editor = controls.get("providerEditor");
const advanced = dialog.querySelector(".app-dialog-advanced-settings");
@@ -8256,6 +8646,93 @@ async function openAiProviderDialog(initialProviderId = "") {
controls.get(`${prefix}imageSizes`).value = imagePreset.models[imagePreset.defaultModel].sizes.join(", ");
controls.get(`${prefix}apiKey`).focus();
});
+ const syncConnectionHelp = () => {
+ for (const profile of profiles) {
+ const key = keyFor(profile.id);
+ const input = controls.get(`provider_${key}_analysisModel`);
+ if (!input) continue;
+ const wrapper = input.closest(".app-dialog-field");
+ let help = wrapper.querySelector(":scope > .model-capability-help");
+ if (!help) {
+ help = document.createElement("small");
+ help.className = "model-capability-help";
+ wrapper.append(help);
+ input.addEventListener("input", syncConnectionHelp);
+ input.addEventListener("change", syncConnectionHelp);
+ }
+ const taskIds = profile.category === "custom"
+ ? connectionAnalysisTaskIds(profile)
+ : connectionModelTaskIds(profile, input.value);
+ help.textContent = input.value && taskIds.length
+ ? t("保存后联动:{tasks};已单独更换的任务保持不变", { tasks: taskIds.map(taskDisplayLabel).join("、") })
+ : profile.category === "custom"
+ ? t("填写兼容接口实际调用的模型;保存后任务路由会同步更新")
+ : t("先读取当前账号模型,再选择本次连接使用的分析模型");
+ }
+ };
+ for (const profile of profiles) {
+ if (profile.category === "custom" || !connectionAnalysisTaskIds(profile).length) continue;
+ const key = keyFor(profile.id);
+ const input = controls.get(`provider_${key}_analysisModel`);
+ const wrapper = input?.closest(".app-dialog-field");
+ if (!wrapper) continue;
+ const readModels = textEl("button", "button-secondary", "读取模型");
+ readModels.type = "button";
+ readModels.dataset.providerModelPreview = profile.id;
+ readModels.addEventListener("click", async () => {
+ const statusLine = status();
+ const apiKeyInput = controls.get(`provider_${key}_apiKey`);
+ const consentInput = controls.get(`provider_${key}_consent`);
+ const previousText = readModels.textContent;
+ try {
+ statusLine.classList.remove("error");
+ statusLine.textContent = "";
+ if (!apiKeyInput.value && !profile.credentialConfigured) {
+ apiKeyInput.focus();
+ throw new Error(t("请先填写 API Key"));
+ }
+ if (!consentInput.checked) {
+ consentInput.focus();
+ throw new Error(t("请先确认发送授权"));
+ }
+ const endpoint = profile.endpoint;
+ const permission = permissionPatternForProvider(endpoint);
+ if (!await chrome.permissions.request({ origins: [permission] })) {
+ throw new Error(t("没有获得所选 AI 服务的访问权限,未读取模型"));
+ }
+ readModels.disabled = true;
+ readModels.textContent = t("正在读取…");
+ const response = await chrome.runtime.sendMessage({
+ type: "PREVIEW_AI_PROVIDER_MODELS",
+ providerId: profile.id,
+ registry: { providers: { [profile.id]: {
+ endpoint,
+ protocol: profile.protocol,
+ apiKey: apiKeyInput.value,
+ consent: true
+ } } }
+ });
+ if (!response?.ok || !response.provider) {
+ throw new Error(translateUiMessage(response?.message) || t("模型目录读取失败"));
+ }
+ Object.assign(profile, response.provider);
+ const options = connectionAnalysisModelOptions(profile);
+ input.replaceChildren(option("", t("请选择分析模型")), ...options.map((item) => option(item.value, item.label)));
+ input.value = options.some((item) => item.value === connectionManagedModel(profile))
+ ? connectionManagedModel(profile) : "";
+ syncConnectionHelp();
+ statusLine.textContent = t("已读取 {count} 个可选分析模型", { count: options.length });
+ input.focus();
+ } catch (error) {
+ statusLine.classList.add("error");
+ statusLine.textContent = translateUiMessage(error?.message) || t("模型目录读取失败");
+ } finally {
+ readModels.disabled = false;
+ readModels.textContent = previousText;
+ }
+ });
+ wrapper.append(readModels);
+ }
const syncProviderFields = () => {
for (const profile of profiles) {
const prefix = `provider_${keyFor(profile.id)}_`;
@@ -8264,42 +8741,42 @@ async function openAiProviderDialog(initialProviderId = "") {
}
}
if (micuPreset) micuPreset.hidden = editor.value !== "custom-media";
- advanced.hidden = ![...advanced.querySelectorAll("[data-field-id]")].some((wrapper) => !wrapper.hidden);
- };
- const syncModelCapabilityHelp = () => {
- for (const profile of profiles) {
- const key = keyFor(profile.id);
- for (const taskId of profile.capabilities) {
- const input = controls.get(`provider_${key}_model_${taskId}`);
- if (!input) continue;
- const wrapper = input.closest(".app-dialog-field");
- let help = wrapper.querySelector(":scope > .model-capability-help");
- if (!help) {
- help = document.createElement("small");
- help.className = "model-capability-help";
- wrapper.append(help);
- input.addEventListener("change", syncModelCapabilityHelp);
- }
- help.textContent = modelAssignmentHelp(profile, taskId, input.value);
- help.hidden = !help.textContent;
- }
+ for (const button of dialog.querySelectorAll("[data-provider-model-preview]")) {
+ button.hidden = button.dataset.providerModelPreview !== editor.value;
}
+ advanced.hidden = ![...advanced.querySelectorAll("[data-field-id]")].some((wrapper) => !wrapper.hidden);
};
editor.addEventListener("change", syncProviderFields);
syncProviderFields();
- syncModelCapabilityHelp();
+ syncConnectionHelp();
},
onSubmit: async (values) => {
const registryUpdate = { providers: {} };
const origins = new Set();
- const profile = aiProviderRegistry.providers?.[values.providerEditor];
+ const profile = profiles.find((item) => item.id === values.providerEditor);
+ if (!profile) throw new Error(t("所选 AI 服务不存在"));
const key = keyFor(profile.id);
const apiKey = values[`provider_${key}_apiKey`];
const endpoint = profile.id.startsWith("custom") ? values[`provider_${key}_endpoint`] : profile.endpoint;
- const models = Object.fromEntries(profile.capabilities.flatMap((taskId) => {
- const model = String(values[`provider_${key}_model_${taskId}`] ?? "").trim();
- return model ? [[taskId, model]] : [];
- }));
+ const consent = values[`provider_${key}_consent`] === true;
+ const analysisTaskIds = connectionAnalysisTaskIds(profile);
+ const analysisModel = String(values[`provider_${key}_analysisModel`] ?? "").trim();
+ const hasAnalysisConnection = Boolean(apiKey || profile.credentialConfigured);
+ if (analysisTaskIds.length && hasAnalysisConnection && !analysisModel) {
+ throw new Error(profile.category === "custom" ? t("请填写分析模型") : t("请先读取模型并选择分析模型"));
+ }
+ if (analysisModel && !consent) throw new Error(t("请先确认发送授权"));
+ const selectedTaskIds = profile.category === "custom"
+ ? analysisTaskIds
+ : connectionModelTaskIds(profile, analysisModel);
+ if (analysisModel && !selectedTaskIds.length) throw new Error(t("当前模型没有可确认的分析能力,请重新读取并选择"));
+ const models = {};
+ for (const taskId of analysisTaskIds) {
+ models[taskId] = profile.category === "custom" ? analysisModel : "";
+ }
+ for (const taskId of ["imageGeneration", "videoGeneration"].filter((taskId) => profile.capabilities.includes(taskId))) {
+ models[taskId] = String(values[`provider_${key}_model_${taskId}`] ?? "").trim();
+ }
const imageGeneration = profile.id === "custom-media" ? {
protocol: values[`provider_${key}_imageProtocol`],
endpoint: values[`provider_${key}_imageEndpoint`],
@@ -8319,7 +8796,9 @@ async function openAiProviderDialog(initialProviderId = "") {
protocol: profile.id.startsWith("custom") ? values[`provider_${key}_protocol`] : profile.protocol,
apiKey,
models,
- consent: values[`provider_${key}_consent`],
+ consent,
+ discoveredModels: profile.discoveredModels,
+ discovery: profile.discovery,
...(imageGeneration ? { imageGeneration } : {})
};
if ((apiKey || profile.credentialConfigured) && endpoint) origins.add(permissionPatternForProvider(endpoint));
@@ -8344,30 +8823,20 @@ async function openAiProviderDialog(initialProviderId = "") {
const response = await chrome.runtime.sendMessage({
type: "UPDATE_AI_PROVIDER_CONFIGURATION",
registry: registryUpdate,
- assignments: aiTaskAssignments
+ assignments: aiTaskAssignments,
+ connectionSelection: {
+ providerId: profile.id,
+ model: analysisModel,
+ taskIds: selectedTaskIds,
+ allowManualUnverifiedTasks: profile.category === "custom" ? selectedTaskIds : []
+ }
});
if (!response?.ok) throw new Error(translateUiMessage(response?.message) || t("AI 服务配置保存失败"));
applyAiConfigurationResponse(response);
const savedMessage = imageCredentialVerification?.message
? `${t("配置已保存")};${t("模型目录中可见 {model};这只证明目录可见,不代表米醋已授权该 Key 进入生图分组", { model: imageGeneration.model })}`
: t("配置已保存");
- if (!(apiKey || profile.credentialConfigured)) return { ...response, message: savedMessage };
- try {
- const discovery = await chrome.runtime.sendMessage({
- type: "DISCOVER_AI_PROVIDER_MODELS",
- providerId: profile.id,
- force: true
- });
- if (!discovery?.ok) throw new Error(translateUiMessage(discovery?.message) || t("模型目录读取失败"));
- applyAiConfigurationResponse(discovery);
- return { ...discovery, message: `${savedMessage};${t("{provider} 已发现 {count} 个模型", { provider: providerDisplayLabel(profile), count: discovery.aiProviderRegistry?.providers?.[profile.id]?.discoveredModels?.length || 0 })}` };
- } catch (error) {
- return {
- ...response,
- message: `${savedMessage};${t("模型目录暂时读取失败,可稍后手动刷新")}`,
- discoveryWarning: translateUiMessage(error.message) || t("模型目录读取失败")
- };
- }
+ return { ...response, message: savedMessage };
}
});
if (result) {
@@ -8394,6 +8863,46 @@ function taskDisplayLabel(taskId) {
return t(AI_ASSIGNMENT_TASKS.find((task) => task.id === taskId)?.label || taskId);
}
+const CONNECTION_ANALYSIS_TASK_IDS = Object.freeze([
+ "textTags", "skillExtraction", "creativePlanning", "imageAnalysis", "videoAnalysis"
+]);
+
+function connectionAnalysisTaskIds(profile = {}) {
+ return CONNECTION_ANALYSIS_TASK_IDS.filter((taskId) => profile.capabilities?.includes(taskId));
+}
+
+function connectionAnalysisModelOptions(profile = {}) {
+ const byId = new Map();
+ for (const taskId of connectionAnalysisTaskIds(profile)) {
+ for (const model of availableAiModelsForTask(taskId, profile)) {
+ if (model.status !== "available") continue;
+ if (!["declared", "protocol_inferred"].includes(model.assignmentEvidence)) continue;
+ if (!byId.has(model.id)) byId.set(model.id, model);
+ }
+ }
+ return [...byId.values()].map((model) => ({
+ value: model.id,
+ label: `${model.name || model.id}${modelPriceLabel(model.pricing)}`
+ }));
+}
+
+function connectionModelTaskIds(profile = {}, modelValue = "") {
+ const model = String(modelValue ?? "").trim();
+ if (!model) return [];
+ return connectionAnalysisTaskIds(profile).filter((taskId) => availableAiModelsForTask(taskId, profile)
+ .some((item) => item.id === model && item.status === "available"
+ && ["declared", "protocol_inferred"].includes(item.assignmentEvidence)));
+}
+
+function connectionManagedModel(profile = {}) {
+ const models = new Set(connectionAnalysisTaskIds(profile).flatMap((taskId) => {
+ const assignment = aiTaskAssignments?.[taskId];
+ return assignment?.managedBy === "connection" && assignment.providerId === profile.id && assignment.model
+ ? [assignment.model] : [];
+ }));
+ return models.size === 1 ? [...models][0] : "";
+}
+
function providerUsesMicuImageGroup(profile = {}) {
if (profile.imageGeneration?.protocol !== "images_generations") return false;
try {
diff --git a/local-media.js b/local-media.js
index 36e9229..665a21a 100644
--- a/local-media.js
+++ b/local-media.js
@@ -1,6 +1,7 @@
import { readImageDimensions as readStoredImageDimensions } from "./image-metadata.js";
import {
ASSET_IMPORT_FAILURE_CODES,
+ MEDIA_FINGERPRINT_CHUNK_BYTES,
assetImportError,
assertImageDimensions,
formatBytes,
@@ -256,6 +257,19 @@ export async function sha256Blob(blob) {
return [...new Uint8Array(digest)].map((value) => value.toString(16).padStart(2, "0")).join("");
}
+export async function chunkedBlobFingerprint(blob, options = {}) {
+ if (!(blob instanceof Blob) || !blob.size) throw new Error("无法计算空媒体的内容指纹");
+ const chunkBytes = Math.max(1, Number(options.chunkBytes) || MEDIA_FINGERPRINT_CHUNK_BYTES);
+ const chunkDigests = [];
+ for (let offset = 0; offset < blob.size; offset += chunkBytes) {
+ const chunk = blob.slice(offset, Math.min(blob.size, offset + chunkBytes));
+ chunkDigests.push(`${chunk.size}:${await sha256Blob(chunk)}`);
+ }
+ const descriptor = `${blob.size}:${blob.type}:${chunkDigests.join(":")}`;
+ const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(descriptor));
+ return [...new Uint8Array(digest)].map((value) => value.toString(16).padStart(2, "0")).join("");
+}
+
async function readLocalImageDimensions(blob) {
if (blob.type !== "image/gif") return readStoredImageDimensions(blob);
const bytes = new Uint8Array(await blob.slice(0, 10).arrayBuffer());
diff --git a/manifest.json b/manifest.json
index bc7b6fa..3be06ed 100644
--- a/manifest.json
+++ b/manifest.json
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "__MSG_extensionName__",
- "version": "1.19.4",
+ "version": "1.19.5",
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAq2m8W9ACXJTkJqPAd36sPrtzonmHhOYU34uS7c+D7DXX6ggWNMwB4xZapGFJ4ScBQJv71PeG9KfsEnhcXFZ9RoX/X3wY+57evL19hf6woNX1xxhKOe1rtx5QmzWCEYIr99TzW50k/N9U6tS9Ri+2DHKYA85ssXTC8B2yRQ5zUXhsAWZe/HDU18UFuvUKXYt3ZdIUi31c2vIlgFFBfsyKbIUMny9LlxL/smb4vJzU/+A3LwJDjjvOtS/lmQ2vHoQGjHJItO18bYI6h67fIEvmWk+HJKtO08Ca+5RLS8TYhI+K7l7S0RyDVNujyVWtvhAULot1H0BvteYL87fKu3SNywIDAQAB",
"homepage_url": "https://github.com/wchao6891/PromptDirector",
"default_locale": "zh_CN",
diff --git a/media.js b/media.js
index e428df0..2956164 100644
--- a/media.js
+++ b/media.js
@@ -175,7 +175,7 @@ export function normalizeEntryMedia(entryValue = {}) {
entry.timeNotes = normalizeTimeNotes(entry.timeNotes, ids);
entry.mediaPrompts = normalizeMediaPrompts(entry.mediaPrompts, ids);
entry.visualSetAnalyses = normalizeVersionedAnalyses(entry.visualSetAnalyses, "visual-set");
- entry.videoAnalyses = normalizeVersionedAnalyses(entry.videoAnalyses, "video");
+ entry.videoAnalyses = normalizeVideoAnalyses(entry.videoAnalyses);
for (const field of [
"visuals", "primaryVisualId", "hasScreenshot", "screenshotWidth", "screenshotHeight",
"screenshotMimeType", "screenshotByteSize", "screenshotReviewStatus", "screenshotUpdatedAt",
@@ -331,6 +331,37 @@ export function setEntryMediaPrompt(entryValue, assetIdValue, textValue, source
return entry;
}
+export function currentVideoReconstruction(entryValue = {}, assetIdValue = "") {
+ const assetId = clean(assetIdValue);
+ if (!assetId) return null;
+ if (!entryMediaAssets(entryValue).some((asset) => asset.id === assetId && asset.kind === "video" && asset.usage !== "poster")) return null;
+ let current = null;
+ let currentTime = Number.NEGATIVE_INFINITY;
+ for (const analysis of normalizeVideoAnalyses(entryValue?.videoAnalyses)) {
+ if (analysis.assetId !== assetId || !completeVideoReconstruction(analysis)) continue;
+ const createdTime = Date.parse(analysis.createdAt);
+ if (createdTime < currentTime) continue;
+ current = analysis;
+ currentTime = createdTime;
+ }
+ return current ? structuredClone(current) : null;
+}
+
+export function editCurrentVideoReconstruction(entryValue, assetIdValue, reconstructionPromptValue) {
+ const entry = normalizeEntryMedia(entryValue);
+ const assetId = clean(assetIdValue);
+ const reconstructionPrompt = cleanMultiline(reconstructionPromptValue);
+ if (!assetId) throw new Error("没有指定要编辑的视频");
+ if (!reconstructionPrompt) throw new Error("AI 视觉逆推提示词不能为空");
+ const current = currentVideoReconstruction(entry, assetId);
+ if (!current) throw new Error("这个视频还没有可编辑的 AI 视觉逆推提示词");
+ const editedAt = new Date().toISOString();
+ entry.videoAnalyses = entry.videoAnalyses.map((analysis) => analysis.id === current.id
+ ? { ...analysis, reconstructionPrompt, userEdited: true, editedAt }
+ : analysis);
+ return entry;
+}
+
export function mediaDescriptions(entryValue = {}) {
return entryMediaAssets(entryValue)
.map((asset) => asset.visionAnalysis?.invalidated || asset.visionAnalysis?.quality === "partial"
@@ -398,6 +429,84 @@ function normalizeVersionedAnalyses(values, kind) {
});
}
+function normalizeVideoAnalyses(values) {
+ return (Array.isArray(values) ? values : []).flatMap((value) => {
+ if (!value || typeof value !== "object" || Array.isArray(value)) return [];
+ const id = clean(value.id);
+ const text = cleanMultiline(value.text);
+ const reconstructionPrompt = cleanMultiline(value.reconstructionPrompt);
+ const mode = clean(value.mode);
+ if (!id || (!text && !(mode === "visual-reconstruction" && reconstructionPrompt))) return [];
+ const tags = normalizeVideoReconstructionTags(value.tags);
+ const uncertainties = normalizeVideoReconstructionUncertainties(value.uncertainties);
+ const createdAt = validIso(value.createdAt) || new Date().toISOString();
+ return [{
+ id,
+ kind: "video",
+ ...(text ? { text } : {}),
+ assetId: clean(value.assetId),
+ mode,
+ prompt: cleanMultiline(value.prompt),
+ sourceKind: clean(value.sourceKind),
+ version: Math.max(1, positiveInteger(value.version)),
+ batchIndex: nonNegativeInteger(value.batchIndex),
+ batchCount: Math.max(1, positiveInteger(value.batchCount)),
+ model: clean(value.model),
+ provider: clean(value.provider),
+ usage: structuredClone(value.usage || {}),
+ cost: Number.isFinite(Number(value.cost)) && Number(value.cost) >= 0 ? Number(value.cost) : null,
+ routing: value.routing && typeof value.routing === "object" && !Array.isArray(value.routing)
+ ? structuredClone(value.routing)
+ : null,
+ createdAt,
+ ...(reconstructionPrompt ? { reconstructionPrompt } : {}),
+ ...(clean(value.requestId) ? { requestId: clean(value.requestId) } : {}),
+ ...(clean(value.contractVersion) ? { contractVersion: clean(value.contractVersion) } : {}),
+ ...(value.batchJobId && clean(value.batchJobId) ? { batchJobId: clean(value.batchJobId) } : {}),
+ ...(Array.isArray(value.tags) ? { tags } : {}),
+ ...(Array.isArray(value.uncertainties) ? { uncertainties } : {}),
+ ...(typeof value.includeTags === "boolean" ? { includeTags: value.includeTags } : {}),
+ ...(clean(value.analysisScope) ? { analysisScope: clean(value.analysisScope) } : {}),
+ ...(clean(value.finishReason) ? { finishReason: clean(value.finishReason) } : {}),
+ ...(typeof value.userEdited === "boolean" ? { userEdited: value.userEdited } : {}),
+ ...(validIso(value.editedAt) ? { editedAt: validIso(value.editedAt) } : {})
+ }];
+ });
+}
+
+function completeVideoReconstruction(value) {
+ if (value?.mode !== "visual-reconstruction" || !clean(value.assetId) || !cleanMultiline(value.reconstructionPrompt)) return false;
+ if (!clean(value.requestId) || !clean(value.contractVersion) || value.analysisScope !== "visual") return false;
+ if (typeof value.includeTags !== "boolean" || !Array.isArray(value.tags) || !Array.isArray(value.uncertainties)) return false;
+ const finishReason = clean(value.finishReason).toLocaleLowerCase("en-US");
+ if (!finishReason || ["length", "content_filter", "error", "cancelled", "canceled"].includes(finishReason)) return false;
+ return value.includeTags ? value.tags.length > 0 : value.tags.length === 0;
+}
+
+function normalizeVideoReconstructionTags(values) {
+ const seen = new Set();
+ return (Array.isArray(values) ? values : []).flatMap((value) => {
+ if (!value || typeof value !== "object" || Array.isArray(value)) return [];
+ const groupId = clean(value.g);
+ const detail = clean(value.t);
+ if (!groupId) return [];
+ const key = `${groupId}:${detail}`;
+ if (seen.has(key)) return [];
+ seen.add(key);
+ return [{ g: groupId, ...(detail ? { t: detail } : {}) }];
+ });
+}
+
+function normalizeVideoReconstructionUncertainties(values) {
+ const seen = new Set();
+ return (Array.isArray(values) ? values : []).flatMap((value) => {
+ const text = cleanMultiline(value);
+ if (!text || seen.has(text)) return [];
+ seen.add(text);
+ return [text];
+ });
+}
+
function normalizeTimeNote(value, assetIds) {
const id = clean(value?.id);
const assetId = clean(value?.assetId);
diff --git a/package-lock.json b/package-lock.json
index 29a3b86..456f4dc 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "prompt-case-collector",
- "version": "1.19.4",
+ "version": "1.19.5",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "prompt-case-collector",
- "version": "1.19.4",
+ "version": "1.19.5",
"license": "Apache-2.0",
"dependencies": {
"@jonahschulte/rtf-toolkit": "0.5.0",
diff --git a/package.json b/package.json
index bddba9f..11a2ea9 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "prompt-case-collector",
- "version": "1.19.4",
+ "version": "1.19.5",
"private": false,
"license": "Apache-2.0",
"repository": "https://github.com/wchao6891/PromptDirector",
@@ -27,7 +27,7 @@
"test:upgrade": "node tools/run-release-upgrade-e2e.mjs",
"test:smart-picker": "python3 test/smart_visual_picker_e2e.py",
"verify:source": "npm run check:public && npm run check && npm run check:brand && npm run check:documents && npm run check:pdfjs && npm run check:store && npm run check:compat && npm test",
- "verify:release": "npm run verify:source && npm run test:e2e && npm run package:release && npm run package && npm run test:upgrade",
+ "verify:release": "npm run verify:source && npm run test:local-extension && npm run package:release && npm run test:upgrade",
"verify": "npm run verify:release"
},
"dependencies": {
diff --git a/resource-limits.js b/resource-limits.js
index e482d35..dbd5035 100644
--- a/resource-limits.js
+++ b/resource-limits.js
@@ -62,6 +62,7 @@ export const PORTABLE_LIBRARY_LIMITS = Object.freeze({
export const SMART_VISUAL_SELECTION_LIMIT = 12;
export const SMART_VISUAL_MINIMUM_EDGE = 64;
+export const MEDIA_FINGERPRINT_CHUNK_BYTES = 8 * MEBIBYTE;
export const PAGE_CAPTURE_LIMITS = Object.freeze({
maxCandidates: 100,
maxMediaPerCandidate: 24,
diff --git a/skill-source-picker.js b/skill-source-picker.js
index b65d619..4edb096 100644
--- a/skill-source-picker.js
+++ b/skill-source-picker.js
@@ -26,7 +26,9 @@ export function cloneSkillSourceSelection(selection = {}) {
return {
entryId: String(selection.entryId ?? ""),
includeEntryText: Boolean(selection.includeEntryText),
- assetIds: new Set(selection.assetIds ?? [])
+ assetIds: new Set(selection.assetIds ?? []),
+ sourceIds: selection.sourceIds === null ? null : new Set(selection.sourceIds ?? []),
+ analysisIds: new Set(selection.analysisIds ?? [])
};
}
diff --git a/skills-page.js b/skills-page.js
index 3c34a8e..2530834 100644
--- a/skills-page.js
+++ b/skills-page.js
@@ -1,5 +1,5 @@
import { DEFAULT_COMPOSER_REQUEST_TIMEOUT_MS, normalizeAiSettings } from "./deepseek.js";
-import { normalizeComposerSettings } from "./composer.js";
+import { normalizeComposerSettings, referenceSourcePartsForAsset } from "./composer.js";
import { requireAiRuntimeProtocolVersion } from "./ai-runtime.js";
import {
currentCreativeSkillVersion,
@@ -528,7 +528,7 @@ function caseCard(entry) {
});
card.append(button);
const assets = availableSkillAssets(entry);
- if (assets.length > 1 || String(entry.text ?? "").trim()) {
+ if (assets.length > 1 || String(entry.text ?? "").trim() || hasSelectableVideoSources(entry, assets)) {
const details = textEl("button", sourceSelectionCountLabel(entry));
details.type = "button";
details.className = "skill-case-detail";
@@ -540,6 +540,13 @@ function caseCard(entry) {
return card;
}
+function hasSelectableVideoSources(entry, assets = availableSkillAssets(entry)) {
+ return assets.some((asset) => asset.kind === "video" && (
+ referenceSourcePartsForAsset(entry, asset.id).length > 1 ||
+ (entry.videoAnalyses ?? []).some((analysis) => String(analysis?.assetId ?? "") === asset.id)
+ ));
+}
+
function renderCaseState(card, entry) {
card.dataset.selected = String(selectedEntryIds.has(entry.id));
const state = card.querySelector(".skill-case-state");
@@ -561,10 +568,13 @@ function selectSourceDefaults(entry) {
function defaultSourceSelection(entry) {
const assets = availableSkillAssets(entry);
const primary = assets.find((asset) => asset.id === entry.primaryMediaId) ?? assets[0];
+ const selectedAssetIds = primary ? new Set([primary.id]) : new Set();
return {
entryId: entry.id,
- includeEntryText: Boolean(String(entry.text ?? "").trim()),
- assetIds: primary ? new Set([primary.id]) : new Set()
+ includeEntryText: primary?.kind !== "video" && Boolean(String(entry.text ?? "").trim()),
+ assetIds: selectedAssetIds,
+ sourceIds: defaultVideoSourceIds(entry, selectedAssetIds),
+ analysisIds: new Set()
};
}
@@ -578,7 +588,9 @@ function sourceSelectionSnapshots() {
return [...sourceSelections.values()].map((selection) => ({
entryId: selection.entryId,
includeEntryText: selection.includeEntryText,
- assetIds: [...selection.assetIds]
+ assetIds: [...selection.assetIds],
+ sourceIds: selection.sourceIds === null ? null : [...selection.sourceIds],
+ analysisIds: [...(selection.analysisIds ?? [])]
}));
}
@@ -668,7 +680,10 @@ function renderSourceInspector() {
const hasText = Boolean(String(entry.text ?? "").trim());
elements.skillSourceTextOption.hidden = !hasText;
elements.skillSourceIncludeText.checked = hasText && selection.includeEntryText;
- elements.skillSourceAssetList.replaceChildren(...availableSkillAssets(entry).map((asset, index) => sourceAssetOption(entry, asset, index)));
+ elements.skillSourceAssetList.replaceChildren(...availableSkillAssets(entry).flatMap((asset, index) => [
+ sourceAssetOption(entry, asset, index),
+ ...(asset.kind === "video" && selection.assetIds.has(asset.id) ? videoSourceOptions(entry, asset) : [])
+ ]));
}
function sourceAssetOption(entry, asset, index) {
@@ -688,13 +703,76 @@ function sourceAssetOption(entry, asset, index) {
input.type = "checkbox";
input.checked = selection.assetIds.has(asset.id);
input.addEventListener("change", () => {
- input.checked ? selection.assetIds.add(asset.id) : selection.assetIds.delete(asset.id);
+ if (input.checked) {
+ selection.assetIds.add(asset.id);
+ if (asset.kind === "video") {
+ if (selection.sourceIds === null) selection.sourceIds = new Set();
+ referenceSourcePartsForAsset(entry, asset.id).forEach((source) => selection.sourceIds.add(source.id));
+ }
+ } else {
+ selection.assetIds.delete(asset.id);
+ if (asset.kind === "video") {
+ const availableIds = new Set(referenceSourcePartsForAsset(entry, asset.id, {
+ analysisIds: (entry.videoAnalyses ?? []).map((analysis) => analysis.id)
+ }).map((source) => source.id));
+ if (selection.sourceIds !== null) {
+ for (const sourceId of availableIds) selection.sourceIds.delete(sourceId);
+ }
+ for (const analysis of entry.videoAnalyses ?? []) {
+ if (String(analysis?.assetId ?? "") === asset.id) selection.analysisIds?.delete(String(analysis.id));
+ }
+ }
+ }
renderSourceInspector();
});
row.append(preview, copy, input);
return row;
}
+function videoSourceOptions(entry, asset) {
+ const selection = inspectorDraftSelection;
+ const allAnalysisIds = (Array.isArray(entry?.videoAnalyses) ? entry.videoAnalyses : [])
+ .filter((analysis) => String(analysis?.assetId ?? "") === asset.id)
+ .map((analysis) => String(analysis?.id ?? "")).filter(Boolean);
+ const currentParts = referenceSourcePartsForAsset(entry, asset.id);
+ const allParts = referenceSourcePartsForAsset(entry, asset.id, { analysisIds: allAnalysisIds });
+ const defaultIds = new Set(currentParts.map((part) => part.id));
+ return allParts.map((part) => {
+ const row = el("label", "skill-source-asset");
+ const preview = el("span", "skill-source-asset-preview");
+ preview.textContent = t("来源");
+ const short = part.kind === "original_prompt" && [...part.text.replace(/\s+/g, "")].length <= 3;
+ const copy = el("span", "skill-source-asset-copy");
+ copy.append(
+ textEl("strong", part.label),
+ textEl("small", short ? t("短内容 · 请确认是否保留") : excerpt(part.text, 80))
+ );
+ const input = document.createElement("input");
+ input.type = "checkbox";
+ input.checked = part.kind === "other_analysis"
+ ? selection.analysisIds?.has(part.analysisId) === true
+ : selection.sourceIds === null || selection.sourceIds.has(part.id);
+ input.addEventListener("change", () => {
+ if (part.kind === "other_analysis") {
+ input.checked ? selection.analysisIds.add(part.analysisId) : selection.analysisIds.delete(part.analysisId);
+ } else {
+ if (selection.sourceIds === null) selection.sourceIds = new Set(defaultIds);
+ input.checked ? selection.sourceIds.add(part.id) : selection.sourceIds.delete(part.id);
+ }
+ renderSourceInspector();
+ });
+ row.dataset.selected = String(input.checked);
+ row.append(preview, copy, input);
+ return row;
+ });
+}
+
+function defaultVideoSourceIds(entry, assetIds) {
+ const selected = assetIds instanceof Set ? assetIds : new Set(assetIds ?? []);
+ return new Set(availableSkillAssets(entry).filter((asset) => asset.kind === "video" && selected.has(asset.id))
+ .flatMap((asset) => referenceSourcePartsForAsset(entry, asset.id).map((source) => source.id)));
+}
+
function updateInspectedTextSelection() {
if (!inspectorDraftSelection) return;
inspectorDraftSelection.includeEntryText = elements.skillSourceIncludeText.checked;
@@ -705,6 +783,8 @@ function selectAllInspectedSource() {
const selection = inspectorDraftSelection;
if (!entry || !selection) return;
selection.assetIds = new Set(availableSkillAssets(entry).map((asset) => asset.id));
+ selection.sourceIds = defaultVideoSourceIds(entry, selection.assetIds);
+ selection.analysisIds = new Set();
if (String(entry.text ?? "").trim()) selection.includeEntryText = true;
renderSourceInspector();
}
@@ -713,6 +793,8 @@ function clearInspectedSource() {
const selection = inspectorDraftSelection;
if (!selection) return;
selection.assetIds.clear();
+ selection.sourceIds = new Set();
+ selection.analysisIds = new Set();
selection.includeEntryText = false;
renderSourceInspector();
}
diff --git a/store/CHROME_WEB_STORE_SUBMISSION.md b/store/CHROME_WEB_STORE_SUBMISSION.md
index e17e53f..26023cb 100644
--- a/store/CHROME_WEB_STORE_SUBMISSION.md
+++ b/store/CHROME_WEB_STORE_SUBMISSION.md
@@ -6,9 +6,9 @@ This file is the source-of-truth worksheet for the current PromptDirector manife
- Chrome Web Store item public key: stored in the source `manifest.json` for local identity verification only; `package:release` removes `key` from the upload manifest.
- Expected extension ID: `iahakaahijddcjjldidbclicedibgpjm`.
-- Current release candidate: `dist/PromptDirector-1.19.4.zip`.
-- Candidate size: `8,839,874` bytes.
-- Candidate SHA-256: `5bfc4a2b230811c8d698df6208aff887afdb257f7036d9d4affe75de7fbbcf09`.
+- Current release candidate: `dist/PromptDirector-1.19.5.zip`.
+- Candidate size: `8,959,149` bytes.
+- Candidate SHA-256: `7ea497929cdbac7f1704951fb0cbabe5b92c2464f6fae8f31b69887af456d1cd`.
- Generate a candidate only with `npm run package:release`; the command verifies the source public key against the expected identity, then removes `key` from the Web Store upload manifest.
## Store listing
diff --git a/test/ai-background-v4.test.js b/test/ai-background-v4.test.js
index 88d9909..a33c557 100644
--- a/test/ai-background-v4.test.js
+++ b/test/ai-background-v4.test.js
@@ -38,6 +38,18 @@ test("background keeps service profiles for job execution but stops publishing o
assert.doesNotMatch(source, /aiTaskRoutes:/);
});
+test("connection model preview reads the account catalog without persisting credentials or routes", () => {
+ const preview = source.slice(
+ source.indexOf("async function previewAiProviderModels"),
+ source.indexOf("async function discoverAiProviderModels")
+ );
+ assert.match(source, /case "PREVIEW_AI_PROVIDER_MODELS"/);
+ assert.match(preview, /aiProviderModule\.discoverModels/);
+ assert.match(preview, /publicAiProviderRegistry\(previewRegistry\)/);
+ assert.doesNotMatch(preview, /persistAiConfiguration/);
+ assert.doesNotMatch(preview, /chrome\.storage/);
+});
+
test("persisted batch recovery wakes the runner while runner exceptions become visible failures", () => {
const recovery = source.slice(
source.indexOf("async function recoverDeepSeekBatch"),
diff --git a/test/ai-provider-registry.test.js b/test/ai-provider-registry.test.js
index 67f1d6a..b2aef4b 100644
--- a/test/ai-provider-registry.test.js
+++ b/test/ai-provider-registry.test.js
@@ -3,6 +3,7 @@ import assert from "node:assert/strict";
import {
AI_ASSIGNMENT_TASKS,
+ applyConnectionModelAssignments,
availableAiModelsForTask,
availableAiProvidersForTask,
createAiTaskAssignment,
@@ -136,6 +137,7 @@ test("task model candidates separate declared catalog capability from explicit m
providerId: "deepseek",
model: "opaque-account-model",
evidence: "manual_unverified",
+ managedBy: "task",
concurrency: 10
});
assert.throws(() => createAiTaskAssignment(
@@ -200,7 +202,7 @@ test("registry v5 exposes task concurrency defaults and official model limits",
assert.equal(publicAiProviderRegistry(registry).providers.kimi.category, "official");
assert.equal(assignments.textTags.concurrency, 20);
assert.equal(assignments.imageAnalysis.concurrency, 10);
- assert.equal(assignments.videoAnalysis.concurrency, 10);
+ assert.equal(assignments.videoAnalysis.concurrency, 2);
const deepSeekVision = getAiModelCapability("deepseek", "deepseek-v4-flash-vision-exp");
assert.equal(deepSeekVision.inputModalities.includes("image"), true);
assert.equal(deepSeekVision.concurrencyLimit.value, 2500);
@@ -212,6 +214,94 @@ test("registry v5 exposes task concurrency defaults and official model limits",
assert.deepEqual(bounded.providers.kimi.discoveredModels[0].tasks, ["videoAnalysis"]);
});
+test("assignment ownership protects existing task routes while one confirmed connection fills compatible gaps", () => {
+ const registry = normalizeAiProviderRegistry({ providers: {
+ zhipu: {
+ apiKey: "zhipu-secret",
+ consent: true,
+ discoveredModels: [{
+ id: "glm-5.3-flash",
+ status: "available",
+ confidence: "declared",
+ tasks: ["textTags", "skillExtraction", "creativePlanning", "imageAnalysis", "videoAnalysis"],
+ inputModalities: ["text", "image", "video"],
+ outputModalities: ["text"]
+ }]
+ },
+ deepseek: {
+ apiKey: "deepseek-secret",
+ consent: true,
+ discoveredModels: [{
+ id: "deepseek-text",
+ status: "available",
+ confidence: "declared",
+ tasks: ["textTags", "skillExtraction", "creativePlanning"],
+ inputModalities: ["text"],
+ outputModalities: ["text"]
+ }]
+ }
+ } });
+ const existing = normalizeAiTaskAssignments({
+ textTags: { providerId: "deepseek", model: "deepseek-text" },
+ imageAnalysis: {
+ providerId: "zhipu", model: "legacy-connection-model", managedBy: "connection"
+ }
+ }, registry);
+
+ assert.equal(existing.textTags.managedBy, "task");
+ assert.equal(existing.imageAnalysis.managedBy, "connection");
+ assert.equal(Object.hasOwn(existing.videoAnalysis, "managedBy"), false);
+
+ const next = applyConnectionModelAssignments(existing, {
+ providerId: "zhipu",
+ model: "glm-5.3-flash",
+ taskIds: ["textTags", "skillExtraction", "creativePlanning", "imageAnalysis", "videoAnalysis"]
+ }, registry);
+
+ assert.deepEqual(next.textTags, existing.textTags);
+ for (const taskId of ["skillExtraction", "creativePlanning", "imageAnalysis", "videoAnalysis"]) {
+ assert.equal(next[taskId].providerId, "zhipu");
+ assert.equal(next[taskId].model, "glm-5.3-flash");
+ assert.equal(next[taskId].managedBy, "connection");
+ }
+
+ const anotherProvider = applyConnectionModelAssignments(next, {
+ providerId: "deepseek",
+ model: "deepseek-text",
+ taskIds: ["textTags", "skillExtraction", "creativePlanning"]
+ }, registry);
+ assert.deepEqual(anotherProvider, next);
+});
+
+test("connection linking ignores unverified catalog guesses unless one explicit custom protocol role allows it", () => {
+ const registry = normalizeAiProviderRegistry({ providers: {
+ "custom-text": {
+ endpoint: "https://compatible.example/v1/chat/completions",
+ protocol: "chat_completions",
+ apiKey: "compatible-secret",
+ consent: true,
+ discoveredModels: [{ id: "opaque-model", confidence: "manual_unverified", tasks: [] }]
+ }
+ } });
+ const ignored = applyConnectionModelAssignments({}, {
+ providerId: "custom-text",
+ model: "opaque-model",
+ taskIds: ["textTags", "creativePlanning"]
+ }, registry);
+ assert.equal(ignored.textTags.providerId, "");
+ assert.equal(ignored.creativePlanning.providerId, "");
+
+ const explicit = applyConnectionModelAssignments({}, {
+ providerId: "custom-text",
+ model: "opaque-model",
+ taskIds: ["creativePlanning"],
+ allowManualUnverifiedTasks: ["creativePlanning"]
+ }, registry);
+ assert.equal(explicit.creativePlanning.providerId, "custom-text");
+ assert.equal(explicit.creativePlanning.managedBy, "connection");
+ assert.equal(explicit.textTags.providerId, "");
+});
+
test("new installs retain both custom connection entries without inventing a saved generic profile", () => {
const providers = publicAiProviderRegistry(normalizeAiProviderRegistry({})).providers;
assert.equal(providers["custom-text"].category, "custom");
diff --git a/test/ai-runtime.test.js b/test/ai-runtime.test.js
index a402c54..233f93e 100644
--- a/test/ai-runtime.test.js
+++ b/test/ai-runtime.test.js
@@ -8,7 +8,9 @@ import {
requireAiRuntimeProtocolVersion,
resolveTextTaskSettings,
resolveVideoAnalysisTask,
- resolveVisionTaskSettings
+ resolveVisionTaskSettings,
+ videoAnalysisRouteMatches,
+ videoAnalysisRouteSnapshot
} from "../ai-runtime.js";
test("Composer and background share one AI runtime protocol version", () => {
@@ -18,6 +20,31 @@ test("Composer and background share one AI runtime protocol version", () => {
assert.throws(() => requireAiRuntimeProtocolVersion(1), /没有发起付费请求/);
});
+test("video route snapshots normalize omitted media policy before comparison", () => {
+ const route = {
+ providerId: "gemini",
+ model: "gemini-video",
+ protocol: "gemini",
+ endpoint: "https://generativelanguage.googleapis.com"
+ };
+ assert.deepEqual(videoAnalysisRouteSnapshot(route), {
+ providerId: "gemini",
+ model: "gemini-video",
+ protocol: "gemini",
+ endpoint: "https://generativelanguage.googleapis.com",
+ localVideo: "",
+ preferPublicVideoUrl: false,
+ publicVideoUrl: ""
+ });
+ assert.equal(videoAnalysisRouteMatches(route, {
+ ...route,
+ localVideo: "",
+ preferPublicVideoUrl: false,
+ publicVideoUrl: ""
+ }), true);
+ assert.equal(videoAnalysisRouteMatches(route, { ...route, model: "gemini-video-next" }), false);
+});
+
const storedConfiguration = {
aiProviderRegistry: { version: 4, providers: {
deepseek: {
@@ -182,13 +209,14 @@ test("GLM-5.3-Flash overrides older provider defaults with its model-specific mu
const text = resolveTextTaskSettings("textTags", configuration);
assert.equal(text.compatible.structuredOutput, "json_object");
- assert.equal(text.compatible.structuredOutputTokenBudget, 4096);
+ assert.equal(text.compatible.structuredOutputTokenBudget, 8192);
const vision = resolveVisionTaskSettings("imageAnalysis", configuration);
assert.equal(vision.compatible.structuredOutput, "json_object");
assert.equal(vision.compatible.imageBase64, "data_url");
const video = resolveVideoAnalysisTask(configuration);
assert.equal(video.preferPublicVideoUrl, true);
assert.equal(video.localVideo, "base64");
+ assert.equal(video.maxOutputTokens, 8192);
});
test("changing the image-analysis assignment does not erase generation services", () => {
diff --git a/test/ai_provider_registry_e2e.py b/test/ai_provider_registry_e2e.py
index fa4a59c..719867d 100644
--- a/test/ai_provider_registry_e2e.py
+++ b/test/ai_provider_registry_e2e.py
@@ -195,7 +195,7 @@ def assert_deepseek_dynamic_image_analysis() -> None:
expect(dialog.locator(".model-capability-help")).to_have_count(1)
expect(dialog.locator(".model-capability-help")).to_contain_text("当前模型未声明这项能力;是否可用以真实执行结果为准")
expect(dialog).not_to_contain_text("实验")
- dialog.get_by_role("button", name="保存任务默认").click()
+ dialog.get_by_role("button", name="保存任务路由").click()
expect(dialog).to_be_hidden()
expect(image_task).to_contain_text(f"DeepSeek · {model_id}")
@@ -206,6 +206,7 @@ def assert_deepseek_dynamic_image_analysis() -> None:
"providerId": "deepseek",
"model": model_id,
"evidence": "manual_unverified",
+ "managedBy": "task",
"concurrency": 10,
}, stored_assignment
runtime = library.evaluate(
@@ -242,7 +243,7 @@ def assert_deepseek_dynamic_image_analysis() -> None:
expect(deepseek_row).to_contain_text("1 个下架或当前不可用")
library.locator('[data-ai-routing-tab="tasks"]').click()
expect(image_task).to_contain_text(f"DeepSeek · {model_id}")
- expect(image_task).to_contain_text("已下架或当前不可用")
+ expect(image_task).to_contain_text("已分配但模型不可用")
disappeared = library.evaluate(
"""async () => {
@@ -295,7 +296,7 @@ def assert_new_install_defaults() -> None:
expect(library.locator('#ai-provider-list [data-provider-category="custom"]')).to_contain_text("自定义兼容服务")
expect(library.locator('[data-provider-id="kimi"]')).to_contain_text("Kimi")
expect(library.locator("#ai-assignment-list .ai-assignment-row")).to_have_count(7)
- expect(library.locator("#ai-assignment-list .ai-assignment-row", has_text="尚未分配")).to_have_count(7)
+ expect(library.locator("#ai-assignment-list .ai-assignment-row", has_text="未连接可用服务")).to_have_count(7)
expect(library.locator("#ai-provider-list")).to_contain_text("模型目录未读取")
stored = library.evaluate("() => chrome.storage.local.get(['aiProviderRegistry', 'aiTaskAssignments'])")
@@ -309,7 +310,7 @@ def assert_new_install_defaults() -> None:
"skillExtraction": 20,
"creativePlanning": 20,
"imageAnalysis": 10,
- "videoAnalysis": 10,
+ "videoAnalysis": 2,
"imageGeneration": 10,
"videoGeneration": 10,
}, stored
@@ -351,8 +352,9 @@ def assert_new_install_defaults() -> None:
library.locator("#open-settings").click()
library.locator('[data-settings-tab="ai"]').click()
expect(library.locator('[data-provider-id="deepseek"]')).to_contain_text("模型目录读取失败:目录服务暂时不可用;保留 1 个模型")
- expect(library.locator('[data-provider-id="kimi"]')).to_contain_text("模型目录已读取 · 1 个模型 · 尚未执行模型调用验证")
- expect(library.locator("#ai-assignment-list .ai-assignment-row", has_text="文字标签")).to_contain_text("已下架或当前不可用")
+ expect(library.locator('[data-provider-id="kimi"]')).to_contain_text("模型目录已读取 · 1 个模型")
+ expect(library.locator('[data-provider-id="kimi"]')).not_to_contain_text("尚未执行模型调用验证")
+ expect(library.locator("#ai-assignment-list .ai-assignment-row", has_text="文字标签")).to_contain_text("连接需修复:API Key 或发送授权无效")
def assert_english_ai_settings() -> None:
@@ -368,7 +370,7 @@ def assert_english_ai_settings() -> None:
expect(library.locator("html")).to_have_attribute("lang", "en")
expect(library.locator("#settings-ai-panel")).not_to_contain_text(chinese, use_inner_text=True)
- expect(library.locator("#ai-assignment-list .ai-assignment-row", has_text="Not assigned")).to_have_count(7)
+ expect(library.locator("#ai-assignment-list .ai-assignment-row", has_text="No usable service connected")).to_have_count(7)
expect(library.locator('[data-provider-id="volcengine"]')).to_contain_text("Volcengine")
library.locator("#ai-assignment-list .ai-assignment-row").first.get_by_role("button", name="Configure").click()
@@ -411,7 +413,10 @@ def main() -> None:
route.fulfill(
status=200,
content_type="application/json",
- body='{"data":[{"id":"gpt-text-catalog-only"}]}',
+ body=(
+ '{"data":[{"id":"gpt-text-catalog-only",'
+ '"input_modalities":["text"],"output_modalities":["text"]}]}'
+ ),
)
def mock_gemini_models(route) -> None:
@@ -574,7 +579,8 @@ def main() -> None:
expect(dialog.locator("#promptdirector-app-dialog-providerEditor option", has_text="官方服务 · Kimi")).to_have_count(1)
expect(dialog.locator("#promptdirector-app-dialog-providerEditor option", has_text="聚合平台 · OpenRouter")).to_have_count(1)
expect(dialog.locator("#promptdirector-app-dialog-providerEditor option", has_text="自定义兼容服务 · 自定义兼容服务(图片与生成)")).to_have_count(1)
- expect(dialog.locator('[data-field-id^="provider_"]:visible')).to_have_count(2)
+ expect(dialog.locator('[data-field-id^="provider_"]:visible')).to_have_count(3)
+ expect(dialog.locator('[data-field-id="provider_openrouter_analysisModel"]')).to_be_visible()
expect(dialog.locator('[data-field-id^="assignment_"]')).to_have_count(0)
password_values = dialog.locator('input[type="password"]').evaluate_all("nodes => nodes.map(node => node.value)")
assert all(value == "" for value in password_values), password_values
@@ -633,10 +639,7 @@ def main() -> None:
expect(dialog).to_be_hidden()
expect(library.locator("#feedback")).to_contain_text("模型目录中可见 gpt-image-2")
expect(library.locator("#feedback")).to_contain_text("不代表米醋已授权")
- assert model_authorizations[-2:] == [
- "Bearer replacement-image-secret",
- "Bearer replacement-analysis-secret",
- ], model_authorizations
+ assert model_authorizations[-1:] == ["Bearer replacement-image-secret"], model_authorizations
saved_keys = library.evaluate("() => chrome.storage.local.get(['aiProviderRegistry', 'aiTaskAssignments'])")
custom_media = saved_keys["aiProviderRegistry"]["providers"]["custom-media"]
assert custom_media["apiKey"] == "replacement-analysis-secret"
@@ -647,6 +650,10 @@ def main() -> None:
openai_dialog = library.locator("#promptdirector-app-dialog")
expect(openai_dialog).to_be_visible()
expect(openai_dialog.locator("#promptdirector-app-dialog-providerEditor")).to_have_value("openai")
+ openai_dialog.get_by_role("button", name="读取模型").click()
+ openai_analysis_model = openai_dialog.locator("#promptdirector-app-dialog-provider_openai_analysisModel")
+ expect(openai_analysis_model.locator("option")).to_have_count(2)
+ openai_analysis_model.select_option("gpt-text-catalog-only")
openai_image_model = openai_dialog.locator("#promptdirector-app-dialog-provider_openai_model_imageGeneration")
expect(openai_image_model).to_be_hidden()
openai_dialog.locator(".app-dialog-advanced-settings > summary").click()
@@ -665,7 +672,7 @@ def main() -> None:
}
assert openai_catalog == {
"gpt-text-catalog-only": "available",
- "openai-account-image-model": "unavailable",
+ "openai-vision": "unavailable",
}, openai_catalog
assert assignment_routes(openai_saved["aiTaskAssignments"]) == assignment_routes(openai_assignments_before_save)
@@ -726,12 +733,13 @@ def main() -> None:
expect(nano_model.locator("option")).to_have_count(1)
expect(nano_model).to_have_value("gemini-3.1-flash-image")
expect(nano_dialog).to_contain_text("Nano Banana 2")
- nano_dialog.get_by_role("button", name="保存任务默认").click()
+ nano_dialog.get_by_role("button", name="保存任务路由").click()
expect(nano_dialog).to_be_hidden()
nano_saved = library.evaluate("() => chrome.storage.local.get(['aiProviderRegistry', 'aiTaskAssignments'])")
assert nano_saved["aiTaskAssignments"]["imageGeneration"] == {
- "providerId": "gemini", "model": "gemini-3.1-flash-image", "concurrency": 10
+ "providerId": "gemini", "model": "gemini-3.1-flash-image", "concurrency": 10,
+ "managedBy": "task",
}
assert assignment_routes({
task: value for task, value in nano_saved["aiTaskAssignments"].items() if task != "imageGeneration"
diff --git a/test/analysis-batch.test.js b/test/analysis-batch.test.js
index 0361970..11cc83f 100644
--- a/test/analysis-batch.test.js
+++ b/test/analysis-batch.test.js
@@ -5,6 +5,7 @@ import { readFile } from "node:fs/promises";
import {
analysisBatchSummary,
analysisRebuildRecovery,
+ bindAnalysisItemAttempt,
cancelAnalysisBatch,
claimAnalysisItems,
createAnalysisBatchUndo,
@@ -23,6 +24,8 @@ import {
textFingerprint,
previewVisionBatch,
createVisionBatchJob,
+ previewVideoBatch,
+ createVideoBatchJob,
normalizeAnalysisBatchJob,
reconcileVisionBatchResults,
recoverInterruptedAnalysisBatch
@@ -50,9 +53,148 @@ test("manual text and vision batches run from the persistent background queue",
assert.match(backgroundSource, /scheduleAnalysisBatchRunner\(\)/);
assert.match(backgroundSource, /runPersistedTextBatchSlice\(initial\.id\)/);
assert.match(backgroundSource, /runPersistedVisionBatchSlice\(initial\.id\)/);
+ assert.match(backgroundSource, /runPersistedVideoBatchSlice\(initial\.id\)/);
assert.match(backgroundSource, /chrome\.alarms\.create\(ANALYSIS_BATCH_ALARM/);
});
+test("video batch preview selects primary local videos and excludes current reconstructions by default", () => {
+ const entries = [{
+ id: "case:video",
+ primaryMediaId: "video:primary",
+ mediaAssets: [
+ { id: "video:primary", kind: "video", storageMode: "managed", mimeType: "video/mp4", byteSize: 12, durationMs: 1000 },
+ { id: "video:second", kind: "video", storageMode: "managed", mimeType: "video/mp4", byteSize: 20 },
+ { id: "image:one", kind: "image", storageMode: "managed" }
+ ],
+ videoAnalyses: [{
+ id: "analysis:old",
+ assetId: "video:second",
+ mode: "visual-reconstruction",
+ contractVersion: "visual-reconstruction-tags-json-v3-1-evidence-guard",
+ reconstructionPrompt: "第二支视频已有逆推",
+ tags: [{ g: "visual.style", t: "写实" }],
+ uncertainties: [],
+ analysisScope: "visual",
+ finishReason: "stop",
+ createdAt: "2026-08-30T00:00:00.000Z"
+ }]
+ }];
+ const primary = previewVideoBatch(entries, {
+ entryIds: ["case:video"],
+ sendableAssetIds: ["video:primary", "video:second"],
+ providerId: "zhipu",
+ model: "glm-5.3-flash",
+ concurrency: 2
+ });
+ assert.deepEqual(primary.items.map((item) => item.assetId), ["video:primary"]);
+ assert.equal(primary.totalBytes, 12);
+ assert.equal(primary.knownDurationMs, 1000);
+
+ const all = previewVideoBatch(entries, {
+ entryIds: ["case:video"],
+ sendableAssetIds: ["video:primary", "video:second"],
+ includeAllVideos: true,
+ reanalyze: true,
+ providerId: "zhipu",
+ model: "glm-5.3-flash",
+ protocol: "chat_completions",
+ sourcePlan: "local-video",
+ endpoint: "https://open.bigmodel.cn/api/paas/v4/chat/completions",
+ localVideo: "base64",
+ preferPublicVideoUrl: false,
+ publicVideoUrl: "direct",
+ assetSnapshots: [
+ { entryId: "case:video", assetId: "video:primary", byteSize: 120, durationMs: 1100, fingerprint: "hash-primary" },
+ { entryId: "case:video", assetId: "video:second", byteSize: 240, durationMs: 2200, fingerprint: "hash-second" }
+ ]
+ });
+ assert.deepEqual(all.items.map((item) => item.assetId), ["video:primary", "video:second"]);
+ const job = createVideoBatchJob(entries, {
+ ...all,
+ entryIds: ["case:video"],
+ sendableAssetIds: ["video:primary", "video:second"],
+ includeAllVideos: true,
+ reanalyze: true,
+ protocol: all.protocol,
+ sourcePlan: all.sourcePlan,
+ assetSnapshots: all.items,
+ instruction: "批量逆推可见画面"
+ });
+ assert.equal(job.kind, "video");
+ assert.equal(job.concurrency, 2);
+ assert.equal(job.items.length, 2);
+ assert.equal(job.protocol, "chat_completions");
+ assert.equal(job.sourcePlan, "local-video");
+ assert.equal(job.endpoint, "https://open.bigmodel.cn/api/paas/v4/chat/completions");
+ assert.equal(job.localVideo, "base64");
+ assert.equal(job.preferPublicVideoUrl, false);
+ assert.equal(job.publicVideoUrl, "direct");
+ assert.deepEqual(job.items.map((item) => item.sourcePlan), ["local-video", "local-video"]);
+ assert.deepEqual(job.items.map((item) => item.fingerprint), ["hash-primary", "hash-second"]);
+ const claimed = claimAnalysisItems(job, 2, () => "claim:video");
+ const first = succeedAnalysisItem(claimed.job, "case:video", "claim:video", {
+ promptTokens: 10,
+ completionTokens: 20,
+ totalTokens: 30
+ }, undefined, { serviceRequests: 1, cost: 0.25 });
+ const secondItem = first.items.find((item) => item.assetId === "video:second");
+ const completed = succeedAnalysisItem(first, "case:video", secondItem.claimId, {}, undefined, {
+ serviceRequests: 1,
+ cost: null
+ });
+ const summary = analysisBatchSummary(completed);
+ assert.equal(summary.totalCost, 0.25);
+ assert.equal(summary.unknownCostCount, 1);
+ assert.equal(summary.requestAttempts, 2);
+});
+
+test("video batch binds the exact task and attempt and retries only selected failures", () => {
+ const entries = [{
+ id: "case:video",
+ primaryMediaId: "video:a",
+ mediaAssets: [
+ { id: "video:a", kind: "video", usage: "content", storageMode: "managed" },
+ { id: "video:b", kind: "video", usage: "content", storageMode: "managed" }
+ ]
+ }];
+ let job = createVideoBatchJob(entries, {
+ id: "video-selective-retry",
+ entryIds: ["case:video"],
+ includeAllVideos: true,
+ reanalyze: true,
+ sendableAssetIds: ["video:a", "video:b"],
+ assetSnapshots: [
+ { entryId: "case:video", assetId: "video:a", fingerprint: "hash-a" },
+ { entryId: "case:video", assetId: "video:b", fingerprint: "hash-b" }
+ ],
+ instruction: "逆推"
+ });
+ let claimed = claimAnalysisItems(job, 2, (() => { let index = 0; return () => `claim-${++index}`; })());
+ job = bindAnalysisItemAttempt(claimed.job, "case:video", "claim-1", {
+ taskId: "task-a",
+ attemptId: "attempt-a",
+ requestId: "attempt-a"
+ });
+ assert.deepEqual(
+ Object.fromEntries(["taskId", "attemptId", "requestId"].map((key) => [key, job.items[0][key]])),
+ { taskId: "task-a", attemptId: "attempt-a", requestId: "attempt-a" }
+ );
+ job = failAnalysisItem(job, "case:video", "claim-1", { message: "状态未知,服务商可能已收到", status: 0 });
+ job = failAnalysisItem(job, "case:video", "claim-2", { message: "格式错误", status: 422 });
+ assert.throws(() => retryFailedAnalysisItems(job, {
+ itemKeys: ["case:video\u0000video:a"],
+ requireSelection: true
+ }), /确认可能重复计费/);
+ job = retryFailedAnalysisItems(job, {
+ itemKeys: ["case:video\u0000video:b"],
+ requireSelection: true,
+ confirmDuplicateCharge: false
+ });
+ assert.equal(job.items[0].status, "failed");
+ assert.equal(job.items[1].status, "pending");
+ assert.equal(job.items[1].taskId, "");
+});
+
test("batch preview selects only text cases that are new or changed", async () => {
const unchanged = await textFingerprint("same prompt");
const preview = await previewAnalysisBatch([
diff --git a/test/analysis-task-background.test.js b/test/analysis-task-background.test.js
index d406c80..a58af45 100644
--- a/test/analysis-task-background.test.js
+++ b/test/analysis-task-background.test.js
@@ -27,6 +27,25 @@ test("temporary-reference analysis checks the active Attempt again before writin
assert.match(action, /priority/);
});
+test("video analysis uses the persisted Attempt, shared scheduler, and one no-retry execution path", () => {
+ const runner = background.slice(
+ background.indexOf("async function runAnalysisTask"),
+ background.indexOf("async function recoverAnalysisTasks")
+ );
+ const video = background.slice(
+ background.indexOf("async function analyzeEntryVideoTaskAction"),
+ background.indexOf("async function updateVideoReconstructionPrompt")
+ );
+ assert.match(runner, /claimed\.request\.kind === "entry_video"/);
+ assert.match(video, /const requestId = String\(message\.attemptId/);
+ assert.match(video, /scheduleAnalysis\(/);
+ assert.match(video, /analysisTaskAttemptIsActive/);
+ assert.match(video, /requestId,/);
+ assert.match(video, /maxOutputTokens:\s*sendRoute\.maxOutputTokens/);
+ assert.doesNotMatch(video, /runScheduledAnalysisWithRetries/);
+ assert.doesNotMatch(background, /case "ANALYZE_ENTRY_VIDEO"/);
+});
+
test("analysis runner ownership is always released when settlement persistence fails", () => {
const runner = background.slice(
background.indexOf("async function runAnalysisTask"),
@@ -34,3 +53,64 @@ test("analysis runner ownership is always released when settlement persistence f
);
assert.match(runner, /finally\s*\{\s*analysisTaskRunners\.delete\(taskId\)/);
});
+
+test("video batch settlement is serialized and service-worker recovery never resends unknown paid attempts", () => {
+ const runner = background.slice(
+ background.indexOf("async function runAnalysisTask"),
+ background.indexOf("async function analysisTaskAttemptIsActive")
+ );
+ const startup = background.slice(
+ background.indexOf("scheduleLibraryMaintenanceRunner()"),
+ background.indexOf("chrome.contextMenus.onClicked")
+ );
+ const recovery = background.slice(
+ background.indexOf("async function recoverVideoBatchExecutionState"),
+ background.indexOf("async function analysisTaskAttemptIsActive")
+ );
+ assert.match(runner, /enqueue\(\(\) => settleVideoBatchTask/);
+ assert.match(startup, /await recoverAnalysisTasks\(\);\s*await recoverVideoBatchExecutionState\(\);\s*scheduleAnalysisBatchRunner\(\)/);
+ assert.match(recovery, /上次执行状态未知,未自动重试/);
+ assert.match(recovery, /candidate\.id === item\.taskId/);
+ assert.match(recovery, /analysis\.requestId === item\.requestId/);
+ assert.match(recovery, /task\.request\.batchClaimId === item\.claimId/);
+ assert.doesNotMatch(recovery, /retryFailedAnalysisItems/);
+});
+
+test("video batches exclude busy assets and never join an unrelated single-item task", () => {
+ const preview = background.slice(
+ background.indexOf("async function previewVideoBatchTask"),
+ background.indexOf("async function createVideoBatchTask")
+ );
+ const start = background.slice(
+ background.indexOf("async function startOrJoinAnalysisTaskAction"),
+ background.indexOf("async function getAnalysisTaskAction")
+ );
+ const runner = background.slice(
+ background.indexOf("async function runPersistedVideoBatchSlice"),
+ background.indexOf("async function settleVideoBatchTask")
+ );
+ assert.match(preview, /\["queued", "running"\]\.includes\(task\.status\)/);
+ assert.match(preview, /preview\.exclusions = exclusions/);
+ assert.match(preview, /fingerprint: await videoAssetFingerprint\(blob\)/);
+ assert.match(preview, /exclusionCounts/);
+ assert.match(start, /task\.request\.batchJobId !== String\(message\.batchJobId\)\.trim\(\)/);
+ assert.match(runner, /response\.task\?\.request\?\.batchJobId !== prepared\.job\.id/);
+ assert.match(runner, /未重复发送/);
+ assert.match(runner, /sourceFingerprint: claim\.fingerprint/);
+ assert.match(runner, /batchClaimId: claim\.claimId/);
+ assert.match(runner, /videoAnalysisRouteMatches\(job, route\)/);
+ assert.match(runner, /routeEndpoint: prepared\.job\.endpoint/);
+ assert.match(runner, /routeProviderId: prepared\.job\.providerId/);
+ assert.match(runner, /routeModel: prepared\.job\.model/);
+ assert.match(runner, /sourceKind: claim\.sourcePlan/);
+ const video = background.slice(
+ background.indexOf("async function analyzeEntryVideoTaskAction"),
+ background.indexOf("async function updateVideoReconstructionPrompt")
+ );
+ assert.match(video, /videoAnalysisRouteMatches\(requestRouteSnapshot, route\)/);
+ assert.match(video, /message\.sourceKind === "local-video" \? ""/);
+ assert.match(video, /message\.sourceKind === "local-video" \? false/);
+ assert.match(video, /const sendConfiguration = await loadAiConfiguration\(\)/);
+ assert.match(video, /videoAnalysisRouteMatches\(route, sendRoute\)/);
+ assert.ok(video.indexOf("const sendConfiguration = await loadAiConfiguration()") < video.indexOf("return analyzeVideo({"));
+});
diff --git a/test/analysis-task-registry.test.js b/test/analysis-task-registry.test.js
index ab0fb38..ef736a2 100644
--- a/test/analysis-task-registry.test.js
+++ b/test/analysis-task-registry.test.js
@@ -35,6 +35,119 @@ test("the same client request joins one persisted task instead of creating dupli
assert.deepEqual(second.task.request.tempReferenceIds, ["reference:one"]);
});
+test("the same video joins one active task even when a second click has a new client request id", () => {
+ const videoRequest = {
+ kind: "entry_video",
+ entryId: "entry:one",
+ assetId: "video:one",
+ mode: "visual-reconstruction",
+ instruction: "只分析可见画面",
+ includeTags: true,
+ priority: "interactive",
+ consumerId: "detail:one",
+ clientRequestId: "video-request:one"
+ };
+ const first = createOrJoinAnalysisTask({}, videoRequest, { taskId: "task:video" });
+ const second = createOrJoinAnalysisTask(first.state, {
+ ...videoRequest,
+ clientRequestId: "video-request:two"
+ }, { taskId: "task:duplicate" });
+
+ assert.equal(first.created, true);
+ assert.equal(second.created, false);
+ assert.equal(second.task.id, "task:video");
+ assert.equal(second.state.items.length, 1);
+ assert.equal(second.task.request.instruction, "只分析可见画面");
+ assert.equal(second.task.request.includeTags, true);
+ assert.deepEqual(second.task.clientRequestIds, ["video-request:one", "video-request:two"]);
+});
+
+test("different video analysis semantics are blocked while the same asset is busy", () => {
+ const first = createOrJoinAnalysisTask({}, {
+ kind: "entry_video",
+ entryId: "entry:one",
+ assetId: "video:one",
+ mode: "visual-reconstruction",
+ instruction: "逆推可见画面",
+ includeTags: true,
+ outputLocale: "zh-CN",
+ clientRequestId: "video-request:one"
+ }, { taskId: "task:reconstruction" });
+ assert.throws(() => createOrJoinAnalysisTask(first.state, {
+ kind: "entry_video",
+ entryId: "entry:one",
+ assetId: "video:one",
+ mode: "ad-review",
+ instruction: "分析广告效果",
+ includeTags: false,
+ outputLocale: "zh-CN",
+ clientRequestId: "video-request:two"
+ }, { taskId: "task:review" }),
+ /当前视频已有分析在运行/);
+ assert.equal(first.state.items.length, 1);
+});
+
+test("a completed video allows an explicit new task and append-only analysis version", () => {
+ const videoRequest = {
+ kind: "entry_video",
+ entryId: "entry:one",
+ assetId: "video:one",
+ mode: "visual-reconstruction",
+ clientRequestId: "video-request:one"
+ };
+ const first = createOrJoinAnalysisTask({}, videoRequest, { taskId: "task:video" });
+ first.state.items[0].status = "completed";
+ first.state.items[0].executionState = "completed";
+ const second = createOrJoinAnalysisTask(first.state, {
+ ...videoRequest,
+ clientRequestId: "video-request:two"
+ }, { taskId: "task:next-video" });
+
+ assert.equal(second.created, true);
+ assert.equal(second.task.id, "task:next-video");
+ assert.equal(second.state.items.length, 2);
+});
+
+test("batch video tasks preserve the exact endpoint and transport snapshot", () => {
+ const created = createOrJoinAnalysisTask({}, {
+ kind: "entry_video",
+ entryId: "entry:one",
+ assetId: "video:one",
+ mode: "visual-reconstruction",
+ batchJobId: "batch:one",
+ batchClaimId: "claim:one",
+ sourceFingerprint: "fingerprint:one",
+ protocol: "chat_completions",
+ sourceKind: "local-video",
+ routeEndpoint: "https://open.bigmodel.cn/api/paas/v4/chat/completions",
+ routeProviderId: "zhipu",
+ routeModel: "glm-5.3-flash",
+ localVideo: "base64",
+ preferPublicVideoUrl: false,
+ publicVideoUrl: "direct",
+ hasRouteSnapshot: true,
+ clientRequestId: "video-request:batch"
+ }, { taskId: "task:batch-video" });
+
+ assert.deepEqual({
+ routeEndpoint: created.task.request.routeEndpoint,
+ routeProviderId: created.task.request.routeProviderId,
+ routeModel: created.task.request.routeModel,
+ localVideo: created.task.request.localVideo,
+ preferPublicVideoUrl: created.task.request.preferPublicVideoUrl,
+ publicVideoUrl: created.task.request.publicVideoUrl,
+ hasRouteSnapshot: created.task.request.hasRouteSnapshot
+ }, {
+ routeEndpoint: "https://open.bigmodel.cn/api/paas/v4/chat/completions",
+ routeProviderId: "zhipu",
+ routeModel: "glm-5.3-flash",
+ localVideo: "base64",
+ preferPublicVideoUrl: false,
+ publicVideoUrl: "direct",
+ hasRouteSnapshot: true
+ });
+});
+
test("detaching removes only the consumer and leaves the running task intact", () => {
const created = createOrJoinAnalysisTask({}, request, { taskId: "task:detach" });
const running = startAnalysisAttempt(created.task, { attemptId: "attempt:detach" });
diff --git a/test/composer-service.test.js b/test/composer-service.test.js
index 636054d..5013e51 100644
--- a/test/composer-service.test.js
+++ b/test/composer-service.test.js
@@ -398,6 +398,21 @@ test("an assigned provider without creative-planning capability fails instead of
}), /未声明创作规划能力/);
});
+test("an unassigned creative-planning route fails before any provider request", async () => {
+ let calls = 0;
+ const session = createComposerSession({ aiProfile: { serviceId: "unassigned", model: "" } });
+
+ await assert.rejects(() => planComposerTurnWithService({
+ session, userMessage: "", composerSettings: settings
+ }, visualSettings(), {
+ fetchImpl: async () => {
+ calls += 1;
+ throw new Error("不应发起请求");
+ }
+ }), /创作规划未配置/);
+ assert.equal(calls, 0);
+});
+
test("xAI is a real per-turn composer service and image generation uses its configured endpoints", async () => {
const xaiSettings = visualSettings({
xai: { apiKey: "xai-secret", textModel: "grok-text", imageModel: "grok-image", mediaConsent: true }
@@ -612,6 +627,51 @@ test("visual planning stays text-only while execution sends every selected image
assert.match(result.finalPrompt, /三人.*中间女性清晰聚焦/);
});
+test("saved video sources stay text-only during Composer execution and never trigger a video upload", async () => {
+ let request;
+ const session = createComposerSession({
+ targetType: "video",
+ aiProfile: { serviceId: "compatible", model: "gpt-5.4-mini" },
+ messages: [{ role: "user", type: "request", content: "基于所选视频方法继续创作" }],
+ referenceSnapshots: [{
+ entryId: "private-case",
+ assetId: "private-video",
+ alias: "@参考1",
+ referenceKind: "video_sources",
+ referenceText: "[原始提示词]\n人物推门进入\n\n[AI 视觉逆推]\n镜头缓慢前推",
+ originalText: "人物推门进入",
+ referenceSources: [
+ { id: "original:private-video", kind: "original_prompt", label: "原始提示词", text: "人物推门进入" },
+ { id: "reconstruction:private-analysis", kind: "video_reconstruction", label: "AI 视觉逆推", text: "镜头缓慢前推" }
+ ]
+ }]
+ });
+ const result = await executeComposerTurnWithService({
+ session,
+ userMessage: "",
+ composerSettings: settings,
+ route: "compose",
+ instruction: "整理为新的视频提示词"
+ }, visualSettings(), [], {
+ stream: false,
+ fetchImpl: async (_url, options) => {
+ request = JSON.parse(options.body);
+ return response({ output_text: "人物推门进入,镜头缓慢前推。" });
+ }
+ });
+ const serialized = JSON.stringify(request);
+ assert.match(serialized, /原始提示词/);
+ assert.match(serialized, /AI 视觉逆推/);
+ assert.equal(serialized.match(/人物推门进入/g)?.length, 1);
+ assert.equal(serialized.includes("input_video"), false);
+ assert.equal(serialized.includes("video_url"), false);
+ assert.equal(serialized.includes("data:video"), false);
+ assert.equal(serialized.includes("private-case"), false);
+ assert.equal(serialized.includes("private-video"), false);
+ assert.equal(serialized.includes("private-analysis"), false);
+ assert.equal(result.finalPrompt, "人物推门进入,镜头缓慢前推。");
+});
+
test("DeepSeek refuses a pure-image reference instead of silently composing without seeing it", async () => {
const session = createComposerSession({
aiProfile: { serviceId: "deepseek", model: "deepseek-v4-flash" },
diff --git a/test/composer.test.js b/test/composer.test.js
index 8dc9152..e4f84fa 100644
--- a/test/composer.test.js
+++ b/test/composer.test.js
@@ -6,6 +6,7 @@ import {
COMPOSER_METHOD_VERSION,
DEFAULT_AGENT_INSTRUCTION,
DEFAULT_COMPOSER_AI_PROFILE,
+ UNASSIGNED_COMPOSER_AI_PROFILE,
DEFAULT_TASK_METHODS,
appendComposerMessage,
appendDiagnosticEvent,
@@ -21,6 +22,7 @@ import {
normalizeComposerSessions,
normalizeComposerSettings,
normalizePlannerResult,
+ formatReferenceSourceParts,
plannerRequestPayload,
resetComposerAgentInstruction,
resetComposerTaskMethod,
@@ -77,6 +79,14 @@ test("composer AI profile preserves dynamically assigned planning models across
{ providerId: "custom-text", model: "local-planner" },
DEFAULT_COMPOSER_AI_PROFILE
), { serviceId: "custom-text", model: "local-planner", thinking: false });
+ assert.deepEqual(composerProfileForTaskAssignment(
+ {},
+ { serviceId: "zhipu", model: "glm-5.3-flash", thinking: true }
+ ), UNASSIGNED_COMPOSER_AI_PROFILE);
+ assert.deepEqual(createComposerSession({
+ aiProfile: UNASSIGNED_COMPOSER_AI_PROFILE,
+ generationAiProfile: UNASSIGNED_COMPOSER_AI_PROFILE
+ }).aiProfile, UNASSIGNED_COMPOSER_AI_PROFILE);
assert.deepEqual(createComposerSession({ generationParameters: { size: "1536x1024", quality: "high", aspectRatio: "16:9", imageSize: "2K", secret: "drop" } }).generationParameters, {
size: "1536x1024",
quality: "high",
@@ -249,6 +259,99 @@ test("multiple selected images from one case become independent reference snapsh
assert.deepEqual(references.map((item) => item.alias), ["@参考1", "@参考2"]);
});
+test("video reference snapshots freeze only the selected asset original prompt current reconstruction and human notes", () => {
+ const entry = {
+ id: "multi-video",
+ title: "private title",
+ text: "案例级回退提示词",
+ classification: { pathIds: [CONTENT_IDS.promptVideo] },
+ primaryMediaId: "video-one",
+ mediaAssets: [
+ { id: "video-one", kind: "video", usage: "content", mimeType: "video/mp4" },
+ { id: "video-two", kind: "video", usage: "content", mimeType: "video/mp4" }
+ ],
+ mediaPrompts: [
+ { assetId: "video-one", text: "第一条视频原提示词", textRevision: 3 },
+ { assetId: "video-two", text: "第二条视频原提示词" }
+ ],
+ timeNotes: [
+ { id: "note-one", assetId: "video-one", startMs: 1200, text: "第一条视频人工笔记", createdAt: "2026-08-30T00:00:00.000Z" },
+ { id: "note-two", assetId: "video-two", startMs: 2300, text: "第二条视频人工笔记", createdAt: "2026-08-30T00:00:01.000Z" }
+ ],
+ videoAnalyses: [
+ completeReconstruction("reverse-old", "video-one", "第一条旧逆推", "2026-08-30T00:00:01.000Z"),
+ { id: "breakdown", assetId: "video-one", mode: "creative-breakdown", text: "默认不应进入的创作拆解", version: 4, createdAt: "2026-08-30T00:00:04.000Z" },
+ completeReconstruction("reverse-current", "video-one", "第一条当前逆推", "2026-08-30T00:00:03.000Z"),
+ completeReconstruction("reverse-other", "video-two", "第二条视频逆推", "2026-08-30T00:00:05.000Z")
+ ],
+ facetAssignments: [{ source: "vision_model", facetId: "private-ai-tag" }]
+ };
+
+ const [reference] = createReferenceSnapshots([entry], [{ entryId: entry.id, assetIds: ["video-one"] }], "zh-CN", "video");
+ assert.equal(reference.referenceId, "multi-video:video-one");
+ assert.equal(reference.assetId, "video-one");
+ assert.equal(reference.referenceKind, "video_sources");
+ assert.deepEqual(reference.referenceSources.map((source) => source.kind), ["original_prompt", "video_reconstruction", "time_notes"]);
+ assert.deepEqual(reference.referenceSources.map((source) => source.text), ["第一条视频原提示词", "第一条当前逆推", "[0:01.200] 第一条视频人工笔记"]);
+ assert.match(reference.referenceText, /\[原始提示词\][\s\S]*\[AI 视觉逆推\][\s\S]*\[人工时间点笔记\]/);
+ assert.doesNotMatch(JSON.stringify(reference), /第二条视频|第一条旧逆推|创作拆解|private-ai-tag/);
+
+ entry.mediaPrompts[0].text = "编辑后的原文";
+ entry.videoAnalyses.push(completeReconstruction("reverse-later", "video-one", "后来新增的逆推", "2026-08-30T00:00:06.000Z"));
+ const restored = createComposerSession({ referenceSnapshots: [reference] }).referenceSnapshots[0];
+ assert.equal(restored.referenceText, reference.referenceText);
+ assert.equal(restored.referenceSources[1].analysisId, "reverse-current");
+});
+
+test("video references allow one source to be cancelled and selected other analysis to be frozen explicitly", () => {
+ const entry = {
+ id: "video-explicit-sources",
+ text: "短",
+ classification: { pathIds: [CONTENT_IDS.promptVideo] },
+ mediaAssets: [{ id: "video", kind: "video", usage: "content", mimeType: "video/mp4" }],
+ videoAnalyses: [
+ completeReconstruction("reverse", "video", "可见画面逆推", "2026-08-30T00:00:01.000Z"),
+ { id: "review", assetId: "video", mode: "ad-review", text: "显式广告审片", version: 2, createdAt: "2026-08-30T00:00:02.000Z" }
+ ]
+ };
+ const [reference] = createReferenceSnapshots([entry], [{
+ entryId: entry.id,
+ assetIds: ["video"],
+ sourceIds: ["reconstruction:reverse"],
+ analysisIds: ["review"]
+ }], "zh-CN", "video");
+ assert.deepEqual(reference.referenceSources.map((source) => source.kind), ["video_reconstruction", "other_analysis"]);
+ assert.doesNotMatch(reference.referenceText, /\[原始提示词\]|短/);
+ assert.match(reference.referenceText, /可见画面逆推[\s\S]*显式广告审片/);
+});
+
+test("exact duplicate video source bodies are sent once with both provenance labels", () => {
+ const formatted = formatReferenceSourceParts([
+ { id: "original", kind: "original_prompt", label: "原始提示词", text: "完全相同的正文" },
+ { id: "reverse", kind: "video_reconstruction", label: "AI 视觉逆推", text: "完全相同的正文" }
+ ]);
+ assert.equal(formatted, "[原始提示词 + AI 视觉逆推]\n完全相同的正文");
+ assert.equal(formatted.match(/完全相同的正文/g)?.length, 1);
+});
+
+function completeReconstruction(id, assetId, reconstructionPrompt, createdAt) {
+ return {
+ id,
+ assetId,
+ mode: "visual-reconstruction",
+ requestId: `request:${id}`,
+ contractVersion: "visual-v3-1",
+ reconstructionPrompt,
+ tags: [],
+ uncertainties: [],
+ includeTags: false,
+ analysisScope: "visual",
+ finishReason: "stop",
+ version: 2,
+ createdAt
+ };
+}
+
test("image reference modes default to conditioned and unlock text-only modes for prompt-backed or independently analyzed assets", () => {
const analyzedReference = {
entryId: "case-a",
diff --git a/test/dialog-layout.test.js b/test/dialog-layout.test.js
index e380d2d..6299b37 100644
--- a/test/dialog-layout.test.js
+++ b/test/dialog-layout.test.js
@@ -33,6 +33,21 @@ test("AI configuration dialogs keep dirty credentials open until the user explic
);
assert.match(providerDialog, /dismissOnBackdrop:\s*false/);
assert.match(providerDialog, /confirmDismissWhenDirty:\s*true/);
+ assert.match(providerDialog, /provider_\$\{key\}_analysisModel/);
+ assert.match(providerDialog, /type:\s*"PREVIEW_AI_PROVIDER_MODELS"/);
+ assert.match(providerDialog, /connectionSelection:/);
+ assert.doesNotMatch(providerDialog, /profile\.capabilities\.flatMap/);
+ assert.doesNotMatch(providerDialog, /type:\s*"DISCOVER_AI_PROVIDER_MODELS"/);
+});
+
+test("connected service summaries omit internal verification chatter", async () => {
+ const source = await readFile(new URL("../library.js", import.meta.url), "utf8");
+ const summary = source.slice(
+ source.indexOf("function providerCatalogLabel"),
+ source.indexOf("async function openAiTaskAssignmentDialog")
+ );
+ assert.doesNotMatch(summary, /尚未执行模型调用验证/);
+ assert.doesNotMatch(summary, /账号可用性待实测/);
});
test("every model catalog refresh applies the complete AI configuration response", async () => {
diff --git a/test/library-detail-layout.test.js b/test/library-detail-layout.test.js
index 3ec022c..2d29fbf 100644
--- a/test/library-detail-layout.test.js
+++ b/test/library-detail-layout.test.js
@@ -202,6 +202,9 @@ test("detail editing and core prompt actions stay beside the content they change
assert.match(prompt, /createComposerAction\(entry\)/);
assert.match(prompt, /detail-analysis-menu/);
assert.match(prompt, /完善分析/);
- assert.match(promptDisplay, /section\.append\(\s*heading,\s*rawTextEl\("pre",[\s\S]*?coreActions,\s*analysisMenu\s*\)/);
+ assert.match(promptDisplay, /section\.append\(\s*heading,\s*rawTextEl\("pre",[\s\S]*?coreActions\s*\)/);
+ assert.match(promptDisplay, /if \(activeVideo && !entry\.compoundCase\) section\.append\(createVideoAnalysisWorkspace\(entry, activeVideo\)\)/);
+ assert.match(promptDisplay, /section\.append\(analysisMenu\)/);
+ assert.ok(promptDisplay.indexOf("createVideoAnalysisWorkspace") < promptDisplay.indexOf("section.append(analysisMenu)"));
assert.match(rule(styles, ".detail-core-actions"), /grid-template-columns:\s*repeat\(2,\s*minmax\(0,\s*1fr\)\)/);
});
diff --git a/test/library-interaction-regressions.test.js b/test/library-interaction-regressions.test.js
index a12204a..173783e 100644
--- a/test/library-interaction-regressions.test.js
+++ b/test/library-interaction-regressions.test.js
@@ -74,7 +74,8 @@ test("vision batch dialog only revives running or paused jobs and otherwise show
assert.match(renderVisionBatchDialog, /elements\.visionBatchProgress\.hidden = !job/);
assert.match(renderVisionBatchDialog, /elements\.visionBatchProgressBar\.max = Math\.max\(1, requestCount \|\| job\?\.requestCount \|\| 1\)/);
assert.match(renderVisionBatchDialog, /elements\.visionBatchProgressBar\.value = Math\.min\(processedCount, elements\.visionBatchProgressBar\.max\)/);
- assert.match(previewSelectedVisionBatch, /visionBatchJob\.providerType !== visionSettings\.activeProvider \|\| visionBatchJob\.model !== currentModel/);
+ assert.match(previewSelectedVisionBatch, /visionBatchJob\.kind !== "vision" \|\| visionBatchJob\.providerType !== visionSettings\.activeProvider/);
+ assert.match(previewSelectedVisionBatch, /if \(providerChanged \|\| visionBatchJob\.model !== currentModel\)/);
assert.match(previewSelectedVisionBatch, /chrome\.runtime\.sendMessage\(\{ type: "CANCEL_VISION_BATCH", jobId: visionBatchJob\.id \}\)/);
});
diff --git a/test/library-package.test.js b/test/library-package.test.js
index 37fefb2..b02519e 100644
--- a/test/library-package.test.js
+++ b/test/library-package.test.js
@@ -368,7 +368,13 @@ test("imports remap both sides of a video poster relationship", () => {
{ id: "video-shared", kind: "video", storageMode: "managed", mimeType: "video/mp4", assetPath: `videos/${id}.mp4`, posterAssetId: "poster-shared" },
{ id: "poster-shared", kind: "image", usage: "poster", storageMode: "managed", mimeType: "image/webp", assetPath: `images/${id}.webp`, derivedFromAssetId: "video-shared" }
],
- primaryMediaId: "video-shared"
+ primaryMediaId: "video-shared",
+ videoAnalyses: [{
+ id: `analysis:${id}`, assetId: "video-shared", mode: "visual-reconstruction",
+ requestId: `attempt:${id}`, contractVersion: "visual-reconstruction-tags-json-v3-1-evidence-guard",
+ reconstructionPrompt: `${id} 视觉逆推`, tags: [], uncertainties: [], includeTags: false,
+ analysisScope: "visual", finishReason: "stop", createdAt: "2026-08-30T00:00:00.000Z"
+ }]
});
const current = {
entries: [videoEntry("local")], taxonomy: createDefaultTaxonomy(), facetCatalog: catalog(),
@@ -383,6 +389,8 @@ test("imports remap both sides of a video poster relationship", () => {
assert.notEqual(video.id, "video-shared");
assert.equal(video.posterAssetId, poster.id);
assert.equal(poster.derivedFromAssetId, video.id);
+ assert.equal(remote.videoAnalyses[0].assetId, video.id);
+ assert.equal(remote.videoAnalyses[0].reconstructionPrompt, "remote 视觉逆推");
});
test("parseLibraryPackage enforces case and image byte limits before storage", () => {
@@ -546,6 +554,46 @@ test("non-empty restore remaps trashed case and project identities instead of dr
assert.equal(restoredEntry.relationships.collections[0].id, restoredProject.targetId);
});
+test("trash import remaps structured video analysis to the restored video asset", () => {
+ const videoSnapshot = {
+ ...entry("deleted-video"), hasScreenshot: undefined, screenshotPath: undefined,
+ mediaAssets: [{
+ id: "video:trash-shared", kind: "video", storageMode: "managed", mimeType: "video/mp4",
+ assetPath: "videos/deleted-video.mp4"
+ }],
+ primaryMediaId: "video:trash-shared",
+ videoAnalyses: [{
+ id: "analysis:trash", assetId: "video:trash-shared", mode: "visual-reconstruction",
+ requestId: "attempt:trash", contractVersion: "visual-reconstruction-tags-json-v3-1-evidence-guard",
+ reconstructionPrompt: "回收站视频逆推", tags: [], uncertainties: [], includeTags: false,
+ analysisScope: "visual", finishReason: "stop", createdAt: "2026-08-30T00:00:00.000Z"
+ }]
+ };
+ const data = packageData([], catalog());
+ data.trashState = { version: 1, items: [{
+ id: "trash:entry:deleted-video", kind: "entry", targetId: "deleted-video",
+ deletedAt: "2026-08-30T00:01:00.000Z", snapshot: videoSnapshot, relationships: {}
+ }] };
+ const current = {
+ entries: [{
+ ...entry("local-video"), hasScreenshot: undefined, screenshotPath: undefined,
+ mediaAssets: [{
+ id: "video:trash-shared", kind: "video", storageMode: "managed", mimeType: "video/mp4",
+ assetPath: "videos/local-video.mp4"
+ }],
+ primaryMediaId: "video:trash-shared"
+ }],
+ trashState: { version: 1, items: [] }, taxonomy: createDefaultTaxonomy(), facetCatalog: catalog(),
+ classificationRules: [], settings: {}, organizerState: { collections: [] }, compoundCases: []
+ };
+
+ const result = mergeLibraryPackage(current, data);
+ const restored = result.state.trashState.items.find((item) => item.kind === "entry").snapshot;
+ assert.notEqual(restored.primaryMediaId, "video:trash-shared");
+ assert.equal(restored.videoAnalyses[0].assetId, restored.primaryMediaId);
+ assert.equal(restored.videoAnalyses[0].reconstructionPrompt, "回收站视频逆推");
+});
+
test("restored trash relationships follow the imported project into its actual merged destination", () => {
const data = packageData([entry("remote")], catalog());
data.organizerState = {
diff --git a/test/local-media.test.js b/test/local-media.test.js
index ad6e74a..a181604 100644
--- a/test/local-media.test.js
+++ b/test/local-media.test.js
@@ -2,6 +2,7 @@ import test from "node:test";
import assert from "node:assert/strict";
import {
+ chunkedBlobFingerprint,
createUnsupportedLocalAssetReference,
detectLocalMediaFile,
findExactMediaDuplicate,
@@ -39,6 +40,16 @@ test("local media detection keeps only supported formats and safe relative paths
assert.throws(() => normalizeLocalRelativePath("../private/frame.gif", "frame.gif"), /相对路径/);
});
+test("media fingerprints read current blob bytes even when metadata could retain an old hash", async () => {
+ const original = new Blob(["AAAA", "BBBB"], { type: "video/mp4" });
+ const replaced = new Blob(["AAAA", "BBBC"], { type: "video/mp4" });
+ assert.equal(original.size, replaced.size);
+ assert.notEqual(
+ await chunkedBlobFingerprint(original, { chunkBytes: 4 }),
+ await chunkedBlobFingerprint(replaced, { chunkBytes: 4 })
+ );
+});
+
test("subtitle preparation extracts safe plain text as a document", async () => {
const subtitle = new File(["1\r\n00:00:01,000 --> 00:00:02,000\r\nOpening\u0000\r\n"], "captions.srt", {
type: "text/plain"
diff --git a/test/media.test.js b/test/media.test.js
index ae387de..7614a9e 100644
--- a/test/media.test.js
+++ b/test/media.test.js
@@ -4,6 +4,8 @@ import assert from "node:assert/strict";
import {
addEntryMedia,
addTimeNote,
+ currentVideoReconstruction,
+ editCurrentVideoReconstruction,
mediaDescriptions,
mediaKindFromFile,
normalizeEntryMedia,
@@ -134,6 +136,73 @@ test("video analysis keeps actual provider cost and routing metadata", () => {
assert.deepEqual(entry.videoAnalyses[0].routing, { provider: "declared-provider" });
});
+test("structured video reconstruction survives normalization and current selection stays asset-specific", () => {
+ const entry = normalizeEntryMedia({
+ id: "case:video-reconstruction",
+ mediaAssets: [
+ { id: "video:one", kind: "video", storageMode: "managed", mimeType: "video/mp4" },
+ { id: "video:two", kind: "video", storageMode: "managed", mimeType: "video/mp4" }
+ ],
+ videoAnalyses: [
+ {
+ id: "legacy:unlinked", text: "旧版无资产记录", mode: "visual-reconstruction",
+ createdAt: "2026-08-30T00:00:00.000Z"
+ },
+ {
+ id: "reconstruction:old", assetId: "video:one", mode: "visual-reconstruction",
+ requestId: "attempt:old", contractVersion: "visual-v3",
+ reconstructionPrompt: "旧版逆推", tags: [{ g: "style.render", t: "电影写实" }],
+ uncertainties: ["转场方式不确定"], includeTags: true, analysisScope: "visual",
+ finishReason: "stop", provider: "智谱 GLM", model: "glm-5.3-flash",
+ createdAt: "2026-08-30T00:01:00.000Z"
+ },
+ {
+ id: "temporary:newer", assetId: "video:one", mode: "creative-breakdown",
+ text: "临时创作拆解", createdAt: "2026-08-30T00:03:00.000Z"
+ },
+ {
+ id: "reconstruction:current", assetId: "video:one", mode: "visual-reconstruction",
+ requestId: "attempt:current", contractVersion: "visual-v3-1", batchJobId: "batch:one",
+ reconstructionPrompt: "当前逆推", tags: [{ g: "composition.shot", t: "近景" }],
+ uncertainties: [], includeTags: true, analysisScope: "visual", finishReason: "stop",
+ userEdited: false, provider: "智谱 GLM", model: "glm-5.3-flash",
+ createdAt: "2026-08-30T00:02:00.000Z"
+ }
+ ]
+ });
+
+ const current = currentVideoReconstruction(entry, "video:one");
+ assert.equal(current.id, "reconstruction:current");
+ assert.equal(current.reconstructionPrompt, "当前逆推");
+ assert.equal(current.requestId, "attempt:current");
+ assert.equal(current.batchJobId, "batch:one");
+ assert.deepEqual(current.tags, [{ g: "composition.shot", t: "近景" }]);
+ assert.equal(currentVideoReconstruction(entry, "video:two"), null);
+ assert.equal(currentVideoReconstruction(entry, ""), null);
+});
+
+test("editing current video reconstruction changes only its reusable prompt and keeps request evidence", () => {
+ const source = normalizeEntryMedia({
+ id: "case:edit-video",
+ mediaAssets: [{ id: "video:one", kind: "video", storageMode: "managed", mimeType: "video/mp4" }],
+ text: "ORIGINAL_PROMPT_SENTINEL",
+ videoAnalyses: [{
+ id: "reconstruction:one", assetId: "video:one", mode: "visual-reconstruction",
+ requestId: "attempt:one", contractVersion: "visual-v3-1", prompt: "ACTUAL_REQUEST_PROMPT",
+ reconstructionPrompt: "模型原结果", tags: [], uncertainties: [], includeTags: false,
+ analysisScope: "visual", finishReason: "stop", createdAt: "2026-08-30T00:00:00.000Z"
+ }]
+ });
+ const edited = editCurrentVideoReconstruction(source, "video:one", "人工调整后的逆推提示词");
+ const current = currentVideoReconstruction(edited, "video:one");
+ assert.equal(current.reconstructionPrompt, "人工调整后的逆推提示词");
+ assert.equal(current.prompt, "ACTUAL_REQUEST_PROMPT");
+ assert.equal(current.requestId, "attempt:one");
+ assert.equal(current.userEdited, true);
+ assert.match(current.editedAt, /^\d{4}-\d{2}-\d{2}T/);
+ assert.equal(edited.text, "ORIGINAL_PROMPT_SENTINEL");
+});
+
test("external video references keep only supported playback modes", () => {
const youtube = normalizeMediaAsset({
id: "video:youtube", kind: "video", storageMode: "reference",
diff --git a/test/release-gate.test.js b/test/release-gate.test.js
index 3feebcf..c13ffd3 100644
--- a/test/release-gate.test.js
+++ b/test/release-gate.test.js
@@ -5,14 +5,14 @@ import { readFile } from "node:fs/promises";
const packageJson = JSON.parse(await readFile(new URL("../package.json", import.meta.url), "utf8"));
const e2eSupport = await readFile(new URL("./e2e_support.py", import.meta.url), "utf8");
-test("the only release verification gate runs source tests and browser E2E before packaging", () => {
+test("the only release verification gate runs source tests and the packaged fixed-id lab before the store package", () => {
const scripts = packageJson.scripts;
assert.equal(scripts.verify, "npm run verify:release");
assert.match(scripts["verify:source"], /npm test/);
assert.match(scripts["verify:release"], /npm run verify:source/);
- assert.match(scripts["verify:release"], /npm run test:e2e/);
+ assert.match(scripts["verify:release"], /npm run test:local-extension/);
assert.match(scripts["verify:release"], /npm run package:release/);
- assert.ok(scripts["verify:release"].indexOf("npm run test:e2e") < scripts["verify:release"].indexOf("npm run package:release"));
+ assert.ok(scripts["verify:release"].indexOf("npm run test:local-extension") < scripts["verify:release"].indexOf("npm run package:release"));
assert.equal((scripts["verify:release"].match(/package:release/g) ?? []).length, 1);
});
@@ -33,9 +33,9 @@ test("release verification cannot bypass the historical data compatibility gate"
test("release verification rehearses an installed-profile upgrade with the final fixed-id package", () => {
const scripts = packageJson.scripts;
assert.equal(scripts["test:upgrade"], "node tools/run-release-upgrade-e2e.mjs");
- assert.match(scripts["verify:release"], /npm run package/);
+ assert.match(scripts["test:local-extension"], /npm run package/);
assert.match(scripts["verify:release"], /npm run test:upgrade/);
- assert.ok(scripts["verify:release"].indexOf("npm run package") < scripts["verify:release"].indexOf("npm run test:upgrade"));
+ assert.ok(scripts["verify:release"].indexOf("npm run test:local-extension") < scripts["verify:release"].indexOf("npm run test:upgrade"));
});
test("GitHub runs source contracts and packaged Chromium journeys before main can advance", async () => {
@@ -44,7 +44,7 @@ test("GitHub runs source contracts and packaged Chromium journeys before main ca
assert.match(workflow, /push:[\s\S]*branches:[\s\S]*main/);
assert.match(workflow, /fetch-depth:\s*0/);
assert.match(workflow, /npm run verify:source/);
- assert.match(workflow, /npm run test:e2e/);
+ assert.match(workflow, /npm run test:local-extension/);
assert.match(workflow, /npm run package:release/);
assert.match(workflow, /npm run package/);
assert.match(workflow, /npm run test:upgrade/);
diff --git a/test/skill-contact-sheet.test.js b/test/skill-contact-sheet.test.js
index 59c0c38..a4a0640 100644
--- a/test/skill-contact-sheet.test.js
+++ b/test/skill-contact-sheet.test.js
@@ -52,3 +52,81 @@ test("extraction payload contains anonymous selected text and no local identity
assert.match(request, /已有画面分析[\s\S]*visible composition/);
assert.doesNotMatch(request, /local:secret|Secret Project|private\.example/);
});
+
+test("selected video Skill sources stay asset-specific anonymous and exclude history tags and binary media", () => {
+ const entries = [{
+ id: "private:case-id",
+ title: "PRIVATE_TITLE_SENTINEL",
+ url: "https://private.example/video",
+ text: "案例级回退",
+ mediaAssets: [
+ { id: "private:video-one", kind: "video", usage: "content", mimeType: "video/mp4", assetPath: "private/video.mp4" },
+ { id: "private:video-two", kind: "video", usage: "content", mimeType: "video/mp4" }
+ ],
+ mediaPrompts: [
+ { assetId: "private:video-one", text: "所选视频原始提示词" },
+ { assetId: "private:video-two", text: "另一视频提示词" }
+ ],
+ timeNotes: [
+ { id: "private:note-one", assetId: "private:video-one", startMs: 0, text: "所选视频人工笔记", createdAt: "2026-08-30T00:00:00.000Z" },
+ { id: "private:note-two", assetId: "private:video-two", startMs: 0, text: "另一视频笔记", createdAt: "2026-08-30T00:00:00.000Z" }
+ ],
+ videoAnalyses: [
+ completeSkillReconstruction("private:old", "private:video-one", "旧逆推", "2026-08-30T00:00:01.000Z"),
+ completeSkillReconstruction("private:current", "private:video-one", "当前视觉逆推", "2026-08-30T00:00:03.000Z"),
+ { id: "private:review", assetId: "private:video-one", mode: "ad-review", text: "默认不进入的审片", createdAt: "2026-08-30T00:00:04.000Z" },
+ completeSkillReconstruction("private:other", "private:video-two", "另一视频逆推", "2026-08-30T00:00:05.000Z")
+ ],
+ facetAssignments: [{ source: "vision_model", facetId: "PRIVATE_TAG_SENTINEL" }]
+ }];
+ const selections = [{ entryId: "private:case-id", includeEntryText: false, assetIds: ["private:video-one"] }];
+ assert.deepEqual(selectedSkillContentImages(entries, selections), []);
+ const [source] = anonymousSkillSources(entries, selections);
+ assert.deepEqual(source.parts.map((part) => part.kind), ["original_prompt", "video_reconstruction", "time_notes"]);
+ const request = buildSkillExtractionRequest({ goal: "提炼视频方法", sources: [source], locale: "zh-CN" });
+ assert.match(request, /\[原始提示词\][\s\S]*所选视频原始提示词/);
+ assert.match(request, /\[AI 视觉逆推\][\s\S]*当前视觉逆推/);
+ assert.match(request, /\[人工时间点笔记\][\s\S]*所选视频人工笔记/);
+ assert.doesNotMatch(request, /旧逆推|审片|另一视频|PRIVATE_TAG_SENTINEL|PRIVATE_TITLE_SENTINEL|private:|private\.example|video\.mp4/);
+
+ const [customized] = anonymousSkillSources(entries, [{
+ ...selections[0],
+ sourceIds: ["reconstruction:private:current", "notes:private:video-one"],
+ analysisIds: ["private:review"]
+ }]);
+ assert.deepEqual(customized.parts.map((part) => part.kind), ["video_reconstruction", "time_notes", "other_analysis"]);
+ assert.doesNotMatch(customized.prompt, /所选视频原始提示词/);
+ assert.match(customized.analysis, /当前视觉逆推[\s\S]*所选视频人工笔记[\s\S]*默认不进入的审片/);
+});
+
+test("Skill source exact dedupe keeps both labels without repeating the body", () => {
+ const request = buildSkillExtractionRequest({
+ goal: "提炼共同方法",
+ locale: "zh-CN",
+ sources: [{
+ parts: [
+ { kind: "original_prompt", text: "相同正文" },
+ { kind: "video_reconstruction", text: "相同正文" }
+ ]
+ }]
+ });
+ assert.match(request, /\[原始提示词 \+ AI 视觉逆推\]\n相同正文/);
+ assert.equal(request.match(/相同正文/g)?.length, 1);
+});
+
+function completeSkillReconstruction(id, assetId, reconstructionPrompt, createdAt) {
+ return {
+ id,
+ assetId,
+ mode: "visual-reconstruction",
+ requestId: `request:${id}`,
+ contractVersion: "visual-v3-1",
+ reconstructionPrompt,
+ tags: [],
+ uncertainties: [],
+ includeTags: false,
+ analysisScope: "visual",
+ finishReason: "stop",
+ createdAt
+ };
+}
diff --git a/test/skill-source-picker.test.js b/test/skill-source-picker.test.js
index 3b8311a..f709c02 100644
--- a/test/skill-source-picker.test.js
+++ b/test/skill-source-picker.test.js
@@ -64,14 +64,26 @@ test("Skill source summary separates images videos documents and case text", ()
});
test("Skill source inspector edits an isolated draft until the user applies it", () => {
- const source = { entryId: "compound", includeEntryText: true, assetIds: new Set(["compound:image:0"]) };
+ const source = {
+ entryId: "compound",
+ includeEntryText: true,
+ assetIds: new Set(["compound:image:0"]),
+ sourceIds: new Set(["original:compound:video:2"]),
+ analysisIds: new Set()
+ };
const draft = cloneSkillSourceSelection(source);
draft.assetIds.add("compound:video:2");
+ draft.sourceIds.delete("original:compound:video:2");
+ draft.analysisIds.add("analysis:one");
draft.includeEntryText = false;
assert.deepEqual([...source.assetIds], ["compound:image:0"]);
+ assert.deepEqual([...source.sourceIds], ["original:compound:video:2"]);
+ assert.deepEqual([...source.analysisIds], []);
assert.equal(source.includeEntryText, true);
assert.deepEqual([...draft.assetIds], ["compound:image:0", "compound:video:2"]);
+ assert.deepEqual([...draft.sourceIds], []);
+ assert.deepEqual([...draft.analysisIds], ["analysis:one"]);
assert.equal(draft.includeEntryText, false);
});
diff --git a/test/video-analysis.test.js b/test/video-analysis.test.js
index 9df3f83..30524d6 100644
--- a/test/video-analysis.test.js
+++ b/test/video-analysis.test.js
@@ -7,6 +7,7 @@ import {
chatCompletionsVideoSourcePlan,
publicYouTubeUrl,
requireVideoAnalysisConfirmation,
+ VIDEO_RECONSTRUCTION_CONTRACT_VERSION,
videoAnalysisPrompt
} from "../video-analysis.js";
@@ -27,6 +28,21 @@ test("custom video analysis requires an explicit question", () => {
assert.match(videoAnalysisPrompt("custom", "比较前后节奏"), /比较前后节奏/);
});
+test("the production reconstruction prompt exposes the visual-only v3.1 evidence contract", () => {
+ const prompt = videoAnalysisPrompt("visual-reconstruction", "", {
+ includeTags: true,
+ durationMs: 12_345,
+ width: 1920,
+ height: 1080
+ });
+ assert.match(prompt, /12\.345 秒/);
+ assert.match(prompt, /1920×1080/);
+ assert.match(prompt, /开始状态、变化过程和结束状态/);
+ assert.match(prompt, /完全没有音轨证据/);
+ assert.match(prompt, /fixedPaths=/);
+ assert.doesNotMatch(prompt, /\["sound"/);
+});
+
test("Gemini YouTube analysis reports the real source, model and usage", async () => {
const calls = [];
const result = await analyzeVideoWithGemini({
@@ -48,6 +64,39 @@ test("Gemini YouTube analysis reports the real source, model and usage", async (
assert.equal(result.usage.totalTokens, 14);
});
+test("Gemini visual reconstruction uses the same editable structured contract as other video routes", async () => {
+ let body;
+ const result = await analyzeVideoWithGemini({
+ apiKey: "key",
+ model: "video-model",
+ mode: "visual-reconstruction",
+ includeTags: false,
+ instruction: "RETURN_EDITABLE_RECONSTRUCTION",
+ youtubeUrl: "https://www.youtube.com/watch?v=abc"
+ }, {
+ fetchImpl: async (_url, options) => {
+ body = JSON.parse(options.body);
+ return new Response(JSON.stringify({
+ modelVersion: "video-model-001",
+ candidates: [{
+ finishReason: "STOP",
+ content: { parts: [{ text: JSON.stringify({
+ reconstructionPrompt: "一支可编辑的视频生成提示词。",
+ tags: [],
+ uncertainties: ["镜头运动方向无法确认"]
+ }) }] }
+ }]
+ }), { status: 200, headers: { "content-type": "application/json" } });
+ }
+ });
+
+ assert.equal(body.generationConfig.responseMimeType, "application/json");
+ assert.equal(body.contents[0].parts[1].text, "RETURN_EDITABLE_RECONSTRUCTION");
+ assert.equal(result.contractVersion, VIDEO_RECONSTRUCTION_CONTRACT_VERSION);
+ assert.equal(result.reconstructionPrompt, "一支可编辑的视频生成提示词。");
+ assert.deepEqual(result.tags, []);
+});
+
test("non-YouTube social links are not disguised as full video analysis", async () => {
await assert.rejects(analyzeVideoWithGemini({ apiKey: "key", model: "model", mode: "creative-breakdown", youtubeUrl: "https://x.com/user/status/1" }), /附加本地视频/);
});
@@ -191,6 +240,129 @@ test("GLM-5.3-Flash sends an eligible local video as raw Base64", async () => {
assert.equal(result.sourceKind, "local-video");
});
+test("GLM visual reconstruction performs one structured visual-only call with the active Attempt request id", async () => {
+ const calls = [];
+ const result = await analyzeVideoWithChatCompletions({
+ apiKey: "zhipu-key",
+ endpoint: "https://open.bigmodel.cn/api/paas/v4/chat/completions",
+ providerLabel: "智谱 GLM",
+ model: "glm-5.3-flash",
+ mode: "visual-reconstruction",
+ includeTags: true,
+ requestId: "attempt:visual-one",
+ maxOutputTokens: 4096,
+ durationMs: 20_000,
+ width: 1280,
+ height: 720,
+ originalPrompt: "ORIGINAL_PROMPT_MUST_NOT_BE_SENT",
+ localVideo: "base64",
+ videoBlob: new Blob([new Uint8Array([1, 2, 3])], { type: "video/mp4" })
+ }, { fetchImpl: async (url, options) => {
+ calls.push({ url, options, body: JSON.parse(options.body) });
+ return new Response(JSON.stringify({
+ request_id: "provider-request-one",
+ model: "glm-5.3-flash",
+ choices: [{
+ finish_reason: "stop",
+ message: { content: JSON.stringify({
+ reconstructionPrompt: "一支只描述可见画面的逐镜头生成提示词。",
+ tags: [
+ { g: "style.render", t: "电影写实" },
+ { g: "camera.shot", t: "近景" },
+ { g: "light.palette", t: "冷暖对比" },
+ { g: "action.change", t: "渐变显现" }
+ ],
+ uncertainties: ["相对运动来源无法仅凭画面确认"]
+ }) }
+ }],
+ usage: { prompt_tokens: 100, completion_tokens: 50, total_tokens: 150 }
+ }), { status: 200, headers: { "content-type": "application/json" } });
+ } });
+
+ assert.equal(calls.length, 1);
+ assert.equal(calls[0].body.request_id, "attempt:visual-one");
+ assert.equal(calls[0].body.max_tokens, 4096);
+ assert.deepEqual(calls[0].body.response_format, { type: "json_object" });
+ assert.equal(calls[0].body.messages[0].content[1].video_url.url, "AQID");
+ assert.doesNotMatch(calls[0].options.body, /ORIGINAL_PROMPT_MUST_NOT_BE_SENT/);
+ assert.equal(result.contractVersion, VIDEO_RECONSTRUCTION_CONTRACT_VERSION);
+ assert.equal(result.analysisScope, "visual");
+ assert.equal(result.requestId, "attempt:visual-one");
+ assert.equal(result.finishReason, "stop");
+ assert.equal(result.reconstructionPrompt, "一支只描述可见画面的逐镜头生成提示词。");
+ assert.equal(result.tags.length, 4);
+ assert.deepEqual(result.uncertainties, ["相对运动来源无法仅凭画面确认"]);
+});
+
+test("visual reconstruction without AI tags still uses one call and requires an empty tags field", async () => {
+ let calls = 0;
+ const result = await analyzeVideoWithChatCompletions({
+ apiKey: "zhipu-key",
+ endpoint: "https://open.bigmodel.cn/api/paas/v4/chat/completions",
+ providerLabel: "智谱 GLM",
+ model: "glm-5.3-flash",
+ mode: "visual-reconstruction",
+ includeTags: false,
+ requestId: "attempt:no-tags",
+ localVideo: "base64",
+ videoBlob: new Blob([new Uint8Array([7, 8, 9])], { type: "video/mp4" })
+ }, { fetchImpl: async () => {
+ calls += 1;
+ return new Response(JSON.stringify({
+ choices: [{ finish_reason: "stop", message: { content: JSON.stringify({
+ reconstructionPrompt: "完整视觉重建提示词",
+ tags: [],
+ uncertainties: []
+ }) } }]
+ }), { status: 200, headers: { "content-type": "application/json" } });
+ } });
+ assert.equal(calls, 1);
+ assert.deepEqual(result.tags, []);
+ assert.equal(result.includeTags, false);
+});
+
+test("truncated or malformed visual reconstruction responses fail instead of returning partial records", async () => {
+ const input = {
+ apiKey: "zhipu-key",
+ endpoint: "https://open.bigmodel.cn/api/paas/v4/chat/completions",
+ providerLabel: "智谱 GLM",
+ model: "glm-5.3-flash",
+ mode: "visual-reconstruction",
+ includeTags: false,
+ requestId: "attempt:invalid",
+ localVideo: "base64",
+ videoBlob: new Blob([new Uint8Array([1])], { type: "video/mp4" })
+ };
+ await assert.rejects(analyzeVideoWithChatCompletions(input, {
+ fetchImpl: async () => new Response(JSON.stringify({
+ choices: [{ finish_reason: "length", message: { content: "{\"reconstructionPrompt\":\"截断" } }]
+ }), { status: 200, headers: { "content-type": "application/json" } })
+ }), /截断/);
+ await assert.rejects(analyzeVideoWithChatCompletions(input, {
+ fetchImpl: async () => new Response(JSON.stringify({
+ choices: [{ finish_reason: "stop", message: { content: JSON.stringify({ reconstructionPrompt: "缺字段" }) } }]
+ }), { status: 200, headers: { "content-type": "application/json" } })
+ }), /字段不完整/);
+});
+
+test("an aborted visual reconstruction does not reach the paid request", async () => {
+ const controller = new AbortController();
+ controller.abort();
+ let calls = 0;
+ await assert.rejects(analyzeVideoWithChatCompletions({
+ apiKey: "zhipu-key",
+ endpoint: "https://open.bigmodel.cn/api/paas/v4/chat/completions",
+ providerLabel: "智谱 GLM",
+ model: "glm-5.3-flash",
+ mode: "visual-reconstruction",
+ requestId: "attempt:aborted",
+ signal: controller.signal,
+ localVideo: "base64",
+ videoBlob: new Blob([new Uint8Array([1])], { type: "video/mp4" })
+ }, { fetchImpl: async () => { calls += 1; } }), /abort/i);
+ assert.equal(calls, 0);
+});
+
test("GLM local video preflight does not invent an unpublished 8 MB API limit", () => {
assert.deepEqual(chatCompletionsVideoSourcePlan({
providerLabel: "智谱 GLM",
@@ -229,6 +401,7 @@ test("generic Chat Completions video errors name the selected provider without e
}), (error) => {
assert.match(error.message, /兼容视频服务.*quota unavailable/);
assert.doesNotMatch(error.message, /never-print-this-key/);
+ assert.equal(error.status, 429);
return true;
});
});
diff --git a/test/zhipu_glm_analysis_e2e.py b/test/zhipu_glm_analysis_e2e.py
index 257d8f9..02f19ae 100644
--- a/test/zhipu_glm_analysis_e2e.py
+++ b/test/zhipu_glm_analysis_e2e.py
@@ -83,12 +83,27 @@ def main() -> None:
)
return
if isinstance(content, list) and any(item.get("type") == "video_url" for item in content):
+ result = {
+ "reconstructionPrompt": "00:01 黑场出现主体,00:03 镜头稳定推进。",
+ "tags": [
+ {"g": "style.render", "t": "电影写实"},
+ {"g": "camera.shot", "t": "近景主体"},
+ {"g": "camera.motion", "t": "稳定推进"},
+ {"g": "light.palette", "t": "深色背景"},
+ ],
+ "uncertainties": [],
+ }
route.fulfill(
status=200,
content_type="application/json",
body=json.dumps({
"model": "glm-5.3-flash",
- "choices": [{"message": {"content": "00:01 黑场出现主体,00:03 镜头推进。"}}],
+ "choices": [{
+ "message": {"content": json.dumps(result, ensure_ascii=False)
+ if payload.get("response_format") == {"type": "json_object"}
+ else "00:01 黑场出现主体,00:03 镜头推进。"},
+ "finish_reason": "stop",
+ }],
"usage": {"prompt_tokens": 12, "completion_tokens": 6, "total_tokens": 18},
}, ensure_ascii=False),
)
@@ -184,12 +199,25 @@ def main() -> None:
assert image_request["response_format"] == {"type": "json_object"}, image_request
video_result = setup.evaluate(
- """() => chrome.runtime.sendMessage({
- type: 'ANALYZE_ENTRY_VIDEO', entryId: 'zhipu-video-entry', assetId: 'zhipu-video',
- mode: 'content-summary', customQuestion: '', singleConfirmation: true
- })"""
+ """async () => {
+ const started = await chrome.runtime.sendMessage({
+ type: 'START_OR_JOIN_ANALYSIS_TASK', kind: 'entry_video',
+ entryId: 'zhipu-video-entry', assetId: 'zhipu-video', mode: 'content-summary',
+ instruction: '只用一句中文概括视频可见内容。', includeTags: false,
+ consumerId: 'zhipu-e2e', clientRequestId: 'zhipu-e2e-public-video',
+ priority: 'interactive', outputLocale: 'zh-CN'
+ });
+ if (!started?.ok) return started;
+ for (let index = 0; index < 100; index += 1) {
+ const current = await chrome.runtime.sendMessage({type: 'GET_ANALYSIS_TASK', taskId: started.task.id});
+ if (!['queued', 'running'].includes(current?.task?.status)) return current;
+ await new Promise(resolve => setTimeout(resolve, 20));
+ }
+ return {ok: false, message: '视频分析任务等待超时'};
+ }"""
)
assert video_result["ok"] is True, video_result
+ assert video_result["task"]["status"] == "completed", video_result
video_request = next(item["payload"] for item in requests if any(
part.get("type") == "video_url"
for part in item["payload"].get("messages", [{}])[-1].get("content", [])
@@ -201,11 +229,10 @@ def main() -> None:
library = run.open_page("library.html", wait_until="networkidle")
library.evaluate("() => { chrome.permissions.request = async () => true; }")
library.locator('.case-card[data-entry-id="zhipu-local-video-entry"]').click()
- library.get_by_role("button", name="分析视频", exact=True).click()
- expect(library.locator("#promptdirector-app-dialog")).to_be_visible()
- expect(library.locator("#promptdirector-app-dialog")).to_contain_text("本地视频文件(将编码发送)")
- library.get_by_role("button", name="开始分析", exact=True).click()
- expect(library.locator(".video-analysis-record")).to_contain_text("00:01 黑场出现主体")
+ library.get_by_role("button", name="逆推提示词", exact=True).click()
+ expect(library.locator(".video-reconstruction-current .video-reconstruction-editor")).to_have_value(
+ "00:01 黑场出现主体,00:03 镜头稳定推进。"
+ )
assert len(requests) == requests_before_local_preflight + 1, requests
local_video_request = requests[-1]["payload"]
local_video_part = next(part for part in local_video_request["messages"][-1]["content"] if part["type"] == "video_url")
diff --git a/video-analysis.js b/video-analysis.js
index 58c5a12..f5984a0 100644
--- a/video-analysis.js
+++ b/video-analysis.js
@@ -1,6 +1,10 @@
+import { analysisTaxonomyPayload, validateAnalysisTagResponse } from "./tag-taxonomy.js";
+
+export const VIDEO_RECONSTRUCTION_CONTRACT_VERSION = "visual-reconstruction-tags-json-v3-1-evidence-guard";
+
export const VIDEO_ANALYSIS_MODES = Object.freeze([
+ { id: "visual-reconstruction", label: "逆推提示词" },
{ id: "creative-breakdown", label: "创意拆解" },
- { id: "content-summary", label: "内容总结" },
{ id: "ad-review", label: "广告评价" },
{ id: "custom", label: "自定义问题" }
]);
@@ -18,15 +22,16 @@ export function requireVideoAnalysisConfirmation(value) {
return true;
}
-export function videoAnalysisPrompt(mode, customQuestion = "") {
- if (mode === "content-summary") return "完整理解视频的画面与声音,给出结构化内容总结,并为关键段落标注可点击的时间戳。只陈述视频中可验证的内容。";
- if (mode === "ad-review") return "从广告创意角度分析视频:前 3 秒钩子、叙事结构、画面节奏、声音、卖点、受众、转化动作与主要问题;为关键判断标注时间戳。";
+export function videoAnalysisPrompt(mode, customQuestion = "", options = {}) {
+ if (mode === "visual-reconstruction") return visualReconstructionPrompt(options);
+ if (mode === "content-summary") return "只根据视频中可见画面给出结构化内容总结,并为关键段落标注时间戳;无法由画面确认的内容明确说明。";
+ if (mode === "ad-review") return "只根据视频中可见画面,从广告创意角度分析前 3 秒钩子、叙事结构、画面节奏、卖点、受众、转化动作与主要问题;为关键判断标注时间戳,不推断未提供的音轨内容。";
if (mode === "custom") {
- const question = clean(customQuestion);
+ const question = cleanMultiline(customQuestion);
if (!question) throw new Error("请填写本次要分析的问题");
- return `${question}\n\n回答时请同时参考视频画面和声音;关键结论标注时间戳,不确定内容明确说明。`;
+ return `${question}\n\n只根据视频中可见画面回答;关键结论标注时间戳,不确定内容明确说明,不推断未提供的音轨内容。`;
}
- return "从创意导演视角完整拆解视频:分段叙事、镜头与构图、运动与剪辑、光色与美术、角色/产品呈现、声音、情绪曲线和可复用方法;为关键段落标注时间戳。";
+ return "只根据视频中可见画面,从创意导演视角完整拆解分段叙事、镜头与构图、运动与剪辑、光色与美术、角色或产品呈现、视觉情绪曲线和可复用方法;为关键段落标注时间戳,不推断未提供的音轨内容。";
}
export async function analyzeVideoWithGemini(input = {}, dependencies = {}) {
@@ -35,7 +40,18 @@ export async function analyzeVideoWithGemini(input = {}, dependencies = {}) {
const apiKey = clean(input.apiKey);
const model = clean(input.model);
if (!apiKey || !model) throw new Error("Gemini 视频分析尚未完成配置");
- const prompt = videoAnalysisPrompt(input.mode, input.customQuestion);
+ const mode = clean(input.mode) || "creative-breakdown";
+ const structuredReconstruction = mode === "visual-reconstruction";
+ const includeTags = input.includeTags !== false;
+ const prompt = cleanMultiline(input.instruction) || videoAnalysisPrompt(mode, input.customQuestion, {
+ includeTags,
+ catalog: input.catalog,
+ locale: input.locale,
+ durationMs: input.durationMs,
+ durationSeconds: input.durationSeconds,
+ width: input.width,
+ height: input.height
+ });
const onStage = typeof input.onStage === "function" ? input.onStage : () => {};
let filePart;
let sourceKind;
@@ -58,11 +74,19 @@ export async function analyzeVideoWithGemini(input = {}, dependencies = {}) {
headers: { "content-type": "application/json", "x-goog-api-key": apiKey },
credentials: "omit",
redirect: "error",
- body: JSON.stringify({ contents: [{ role: "user", parts: [filePart, { text: prompt }] }] })
+ signal: input.signal,
+ body: JSON.stringify({
+ contents: [{ role: "user", parts: [filePart, { text: prompt }] }],
+ ...(structuredReconstruction ? { generationConfig: { responseMimeType: "application/json" } } : {})
+ })
});
const payload = await readJsonResponse(response, "Gemini 视频分析");
const text = (payload.candidates ?? []).flatMap((candidate) => candidate?.content?.parts ?? []).map((part) => clean(part?.text)).filter(Boolean).join("\n\n");
if (!text) throw new Error("Gemini 没有返回可保存的视频分析文字");
+ const finishReason = clean(payload.candidates?.[0]?.finishReason);
+ const reconstruction = structuredReconstruction
+ ? parseVideoReconstruction(text, { includeTags, catalog: input.catalog, finishReason })
+ : null;
onStage("completed");
return {
text,
@@ -70,7 +94,16 @@ export async function analyzeVideoWithGemini(input = {}, dependencies = {}) {
model: clean(payload.modelVersion) || model,
sourceKind,
prompt,
- usage: normalizeGeminiUsage(payload.usageMetadata)
+ usage: normalizeGeminiUsage(payload.usageMetadata),
+ finishReason,
+ ...(reconstruction ? {
+ contractVersion: VIDEO_RECONSTRUCTION_CONTRACT_VERSION,
+ analysisScope: "visual",
+ includeTags,
+ reconstructionPrompt: reconstruction.reconstructionPrompt,
+ tags: reconstruction.tags,
+ uncertainties: reconstruction.uncertainties
+ } : {})
};
}
@@ -87,8 +120,22 @@ export async function analyzeVideoWithChatCompletions(input = {}, dependencies =
const model = clean(input.model);
const providerLabel = clean(input.providerLabel) || "兼容视频服务";
if (!apiKey || !model) throw new Error(`${providerLabel} 视频分析尚未完成配置`);
- const prompt = videoAnalysisPrompt(input.mode, input.customQuestion);
+ const mode = clean(input.mode) || "creative-breakdown";
+ const structuredReconstruction = mode === "visual-reconstruction";
+ const includeTags = input.includeTags !== false;
+ const requestId = clean(input.requestId);
+ if (structuredReconstruction && !requestId) throw new Error("AI 视觉逆推缺少本次唯一请求标识,尚未发送");
+ const prompt = cleanMultiline(input.instruction) || videoAnalysisPrompt(mode, input.customQuestion, {
+ includeTags,
+ catalog: input.catalog,
+ locale: input.locale,
+ durationMs: input.durationMs,
+ durationSeconds: input.durationSeconds,
+ width: input.width,
+ height: input.height
+ });
const onStage = typeof input.onStage === "function" ? input.onStage : () => {};
+ throwIfAborted(input.signal);
const sourcePlan = chatCompletionsVideoSourcePlan(input);
let videoUrl = sourcePlan.videoUrl;
const sourceKind = sourcePlan.sourceKind;
@@ -97,20 +144,27 @@ export async function analyzeVideoWithChatCompletions(input = {}, dependencies =
throw new Error(`${providerLabel} 当前不支持 ${input.videoBlob.type || "未知格式"} 视频;请先转换为 MP4、WebM、MOV 或 MPEG`);
}
onStage("encoding");
+ throwIfAborted(input.signal);
videoUrl = input.localVideo === "base64"
? await blobBase64(input.videoBlob)
: await blobDataUrl(input.videoBlob);
+ throwIfAborted(input.signal);
}
onStage("analyzing");
const endpoint = chatCompletionsEndpoint(input.endpoint, providerLabel);
+ const maxOutputTokens = positiveInteger(input.maxOutputTokens);
const response = await fetchImpl(endpoint, {
method: "POST",
headers: { "content-type": "application/json", authorization: `Bearer ${apiKey}` },
credentials: "omit",
redirect: "error",
+ signal: input.signal,
body: JSON.stringify({
model,
stream: false,
+ ...(requestId ? { request_id: requestId } : {}),
+ ...(maxOutputTokens ? { max_tokens: maxOutputTokens } : {}),
+ ...(structuredReconstruction ? { response_format: { type: "json_object" } } : {}),
messages: [{
role: "user",
content: [
@@ -124,10 +178,18 @@ export async function analyzeVideoWithChatCompletions(input = {}, dependencies =
try {
payload = await readJsonResponse(response, `${providerLabel} 视频分析`);
} catch (error) {
- throw new Error(`${providerLabel} 视频分析失败:${redactSecret(error?.message, apiKey)}`, { cause: error });
+ const wrapped = new Error(`${providerLabel} 视频分析失败:${redactSecret(error?.message, apiKey)}`, { cause: error });
+ wrapped.status = Number(error?.status) || 0;
+ throw wrapped;
}
- const text = chatCompletionsMessageText(payload?.choices?.[0]?.message?.content);
+ throwIfAborted(input.signal);
+ const choice = payload?.choices?.[0];
+ const finishReason = clean(choice?.finish_reason);
+ const text = chatCompletionsMessageText(choice?.message?.content);
if (!text) throw new Error(`${providerLabel} 没有返回可保存的视频分析文字`);
+ const reconstruction = structuredReconstruction
+ ? parseVideoReconstruction(text, { includeTags, catalog: input.catalog, finishReason })
+ : null;
onStage("completed");
return {
text,
@@ -137,7 +199,18 @@ export async function analyzeVideoWithChatCompletions(input = {}, dependencies =
prompt,
usage: normalizeChatCompletionsUsage(payload.usage),
cost: Number.isFinite(Number(payload.usage?.cost)) ? Number(payload.usage.cost) : null,
- routing: clean(payload.provider) ? { provider: clean(payload.provider) } : null
+ routing: clean(payload.provider) ? { provider: clean(payload.provider) } : null,
+ finishReason,
+ ...(requestId ? { requestId } : {}),
+ ...(clean(payload.request_id) ? { responseRequestId: clean(payload.request_id) } : {}),
+ ...(reconstruction ? {
+ contractVersion: VIDEO_RECONSTRUCTION_CONTRACT_VERSION,
+ analysisScope: "visual",
+ includeTags,
+ reconstructionPrompt: reconstruction.reconstructionPrompt,
+ tags: reconstruction.tags,
+ uncertainties: reconstruction.uncertainties
+ } : {})
};
}
@@ -284,6 +357,82 @@ function chatCompletionsMessageText(value) {
.join("\n\n");
}
+function visualReconstructionPrompt(options = {}) {
+ const includeTags = options.includeTags !== false;
+ const durationSeconds = positiveNumber(options.durationSeconds) || positiveNumber(options.durationMs) / 1000;
+ const width = positiveInteger(options.width);
+ const height = positiveInteger(options.height);
+ const metadata = [
+ durationSeconds > 0 ? `时长 ${durationSeconds.toFixed(3)} 秒` : "",
+ width && height ? `画面 ${width}×${height}` : ""
+ ].filter(Boolean);
+ const tagInstruction = includeTags
+ ? [
+ "tags 必须为 4–8 个对象;每个对象只含 g、t,g 必须来自 fixedPaths 的视觉分类路径,t 是稳定、具体、互不重复的中文检索短标签。",
+ `fixedPaths=${visualAnalysisTaxonomyPayload(options.catalog, options.locale)}`
+ ].join("\n")
+ : "本次不生成标签,tags 必须是空数组。";
+ return [
+ "你是视频生成提示词逆向工程师。目标是重建这支成片中实际可见的视觉结果,不猜测原作者未实现的意图。返回一个 JSON 对象。",
+ metadata.length ? `媒体元数据由客户端直接读取:${metadata.join(",")}。这些值高于模型对画幅与时长的猜测。` : "",
+ "JSON 只能有 reconstructionPrompt、tags、uncertainties 三个字段。reconstructionPrompt 必须是完整、可编辑、可直接用于视频生成的中文提示词;uncertainties 只列会显著影响视觉重建、但无法从画面确认的项目。",
+ tagInstruction,
+ "reconstructionPrompt 依次包含:整体视觉目标与媒介形式;稳定主体和场景;按真实时长划分的逐镜头时间线;风格、光色和材质;主体连续性、动作物理与生成约束;需要复现的片内叙事文字。每个真实镜头或连续段只写一次,并在同一条中写时间、景别、机位、有证据的运镜、主体动作和切换方式。",
+ "只描述画面证据。平台或模型附加的 AI 角标、水印、播放器控件、网页边框不属于创意内容,不得写进任何字段;只有教程标题、剧情字幕、产品信息等片内叙事文字才保留。",
+ "当前请求只验证视觉画面,完全没有音轨证据。三个输出字段都不得描述、推断或限制任何声音信息,也不得用声音是否存在来补全画面;音频能力由产品界面在结果之外另行说明。",
+ "时间线先保留每段最显著的可见变化及其开始状态、变化过程和结束状态,再补材质等次要细节。每个动作、运镜、变形、出现或消失都必须在视频中看到相应的连续变化;只看到起点或终点时,只描述已见状态,不补写未完整发生的动作,也不把相对运动武断归因给主体或摄影机。不得续写最后一帧之后可能发生的事。",
+ "先识别会影响视觉重建但证据不足的判断。此类判断只能进入 uncertainties;reconstructionPrompt 必须改写为所有可能解释都成立的可见共同事实,不能断言其中任一解释,也不能以互斥选项补成生成约束。确定项不得在 uncertainties 中重复。",
+ "返回前逐项核对时间线中的动作主语、起止状态和转场判断:没有直接画面证据的动作删除;与 uncertainties 冲突的断言降级为可见共同事实。不要输出创作评价、方法论、品牌猜测、幕后意图、Markdown 或 JSON 以外文字。先完整写完 reconstructionPrompt;如果输出空间不足,先减少 uncertainties,再减少 tags,禁止截断主提示词。"
+ ].filter(Boolean).join("\n");
+}
+
+function visualAnalysisTaxonomyPayload(catalog, locale) {
+ const payload = JSON.parse(analysisTaxonomyPayload(catalog, locale));
+ payload.f = (Array.isArray(payload.f) ? payload.f : []).filter((facet) => facet?.[0] !== "sound");
+ return JSON.stringify(payload);
+}
+
+function parseVideoReconstruction(text, { includeTags, catalog, finishReason }) {
+ const normalizedFinishReason = clean(finishReason).toLocaleLowerCase("en-US");
+ if (normalizedFinishReason === "length") throw new Error("视频逆推结果被模型截断,本次没有保存");
+ if (normalizedFinishReason !== "stop") throw new Error("视频逆推没有完整结束,本次没有保存");
+ let value;
+ try {
+ value = JSON.parse(text);
+ } catch {
+ throw new Error("视频逆推没有返回有效 JSON,本次没有保存");
+ }
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
+ throw new Error("视频逆推返回结构无效,本次没有保存");
+ }
+ const expectedFields = ["reconstructionPrompt", "tags", "uncertainties"];
+ const fields = Object.keys(value).toSorted();
+ if (fields.length !== expectedFields.length || fields.some((field, index) => field !== expectedFields.toSorted()[index])) {
+ throw new Error("视频逆推返回字段不完整,本次没有保存");
+ }
+ const reconstructionPrompt = cleanMultiline(value.reconstructionPrompt);
+ if (!reconstructionPrompt) throw new Error("视频逆推没有返回可独立使用的提示词,本次没有保存");
+ if (!Array.isArray(value.uncertainties) || value.uncertainties.some((item) => typeof item !== "string" || !cleanMultiline(item))) {
+ throw new Error("视频逆推的不确定项格式无效,本次没有保存");
+ }
+ const uncertainties = [...new Set(value.uncertainties.map(cleanMultiline))];
+ if (!Array.isArray(value.tags)) throw new Error("视频逆推的标签格式无效,本次没有保存");
+ if (!includeTags) {
+ if (value.tags.length) throw new Error("本次未请求 AI 标签,但服务返回了标签;结果未保存");
+ return { reconstructionPrompt, tags: [], uncertainties };
+ }
+ if (value.tags.length < 4 || value.tags.length > 8 || value.tags.some((tag) => {
+ if (!tag || typeof tag !== "object" || Array.isArray(tag)) return true;
+ const keys = Object.keys(tag).toSorted();
+ return keys.length !== 2 || keys[0] !== "g" || keys[1] !== "t" || !clean(tag.g) || !clean(tag.t) || clean(tag.g).startsWith("sound.");
+ })) {
+ throw new Error("视频逆推必须返回 4–8 个有效视觉标签,本次没有保存");
+ }
+ const tags = validateAnalysisTagResponse({ tags: value.tags }, catalog, { maxTags: 8 });
+ if (tags.length !== value.tags.length) throw new Error("视频逆推标签包含重复或无效分类,本次没有保存");
+ return { reconstructionPrompt, tags, uncertainties };
+}
+
function normalizeChatCompletionsUsage(value = {}) {
return {
inputTokens: Math.max(0, Number(value.prompt_tokens ?? value.input_tokens) || 0),
@@ -301,7 +450,11 @@ function redactSecret(value, secretValue) {
async function readJsonResponse(response, label) {
let payload = {};
try { payload = await response.json(); } catch {}
- if (!response.ok) throw new Error(clean(payload?.error?.message) || `${label}失败(HTTP ${response.status})`);
+ if (!response.ok) {
+ const error = new Error(clean(payload?.error?.message) || `${label}失败(HTTP ${response.status})`);
+ error.status = Math.max(0, Number(response.status) || 0);
+ throw error;
+ }
return payload;
}
@@ -313,6 +466,26 @@ function normalizeGeminiUsage(value = {}) {
};
}
+function positiveInteger(value) {
+ const number = Number(value);
+ return Number.isSafeInteger(number) && number > 0 ? number : 0;
+}
+
+function positiveNumber(value) {
+ const number = Number(value);
+ return Number.isFinite(number) && number > 0 ? number : 0;
+}
+
+function throwIfAborted(signal) {
+ if (!signal?.aborted) return;
+ if (typeof signal.throwIfAborted === "function") signal.throwIfAborted();
+ throw new DOMException("The operation was aborted", "AbortError");
+}
+
+function cleanMultiline(value) {
+ return String(value ?? "").replace(/\r\n?/g, "\n").replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/g, "").trim();
+}
+
function clean(value) {
return String(value ?? "").replace(/[\u0000-\u001f\u007f]/g, " ").trim();
}