From 7e149a10c0db9fa7a80a230ec714d41edb061681 Mon Sep 17 00:00:00 2001 From: GJ Date: Sat, 7 Nov 2026 16:38:26 +0800 Subject: [PATCH] =?UTF-8?q?feat(short-drama-agent-front):=20=E6=8E=A5?= =?UTF-8?q?=E5=85=A5=E7=A9=BA=E9=97=B4=E6=AF=8D=E7=89=88=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E4=B8=8E=E4=BF=AE=E8=AE=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/f04-spatial-master-integration.md | 36 ++ src/features/spatial-layouts/api.ts | 144 +++++++ .../components/MasterGenerationForm.vue | 149 +++++++ .../components/MasterRecoveryImpact.vue | 192 +++++++++ .../components/MasterReviewForm.vue | 301 ++++++++++++++ .../components/SceneMasterManager.vue | 366 ++++++++++++++++++ src/features/spatial-layouts/model.ts | 76 ++++ src/features/spatial-layouts/types.ts | 232 +++++++++++ .../spatial-layouts/useMasterGeneration.ts | 316 +++++++++++++++ .../subject-images/SubjectImagesPage.vue | 32 +- .../components/FormSourceDiagnostics.vue | 9 +- .../components/ImageGalleryDialog.vue | 21 +- .../subject-images/image-request-guard.ts | 15 +- .../subject-images/source-diagnostics.ts | 17 +- .../spatial-layouts/generation.test.ts | 230 +++++++++++ .../spatial-layouts/manager-ui.test.ts | 108 ++++++ .../features/spatial-layouts/masters.test.ts | 151 ++++++++ tests/fixtures/spatial-masters.ts | 349 +++++++++++++++++ 18 files changed, 2732 insertions(+), 12 deletions(-) create mode 100644 docs/f04-spatial-master-integration.md create mode 100644 src/features/spatial-layouts/api.ts create mode 100644 src/features/spatial-layouts/components/MasterGenerationForm.vue create mode 100644 src/features/spatial-layouts/components/MasterRecoveryImpact.vue create mode 100644 src/features/spatial-layouts/components/MasterReviewForm.vue create mode 100644 src/features/spatial-layouts/components/SceneMasterManager.vue create mode 100644 src/features/spatial-layouts/model.ts create mode 100644 src/features/spatial-layouts/types.ts create mode 100644 src/features/spatial-layouts/useMasterGeneration.ts create mode 100644 tests/features/spatial-layouts/generation.test.ts create mode 100644 tests/features/spatial-layouts/manager-ui.test.ts create mode 100644 tests/features/spatial-layouts/masters.test.ts create mode 100644 tests/fixtures/spatial-masters.ts diff --git a/docs/f04-spatial-master-integration.md b/docs/f04-spatial-master-integration.md new file mode 100644 index 0000000..b0999ed --- /dev/null +++ b/docs/f04-spatial-master-integration.md @@ -0,0 +1,36 @@ +# F04 空间母版恢复与修订接入 + +## 基线与范围 + +- 前端从 `c808a58` 开始,根目录 `dev` 开发;既有 Vision 完整执行历史已在该基线接入,本轮保留并回归,不再建立本机执行历史缓存。 +- 本轮核对后端 `758b07e4` 的正式路由、Schema、服务、响应和 `docs/audits/b04-09-layout-master-recovery-2026-11-07.md`;未修改后端。 +- 沿用“形态图片 → 查看图片与记录 → 场景几何与机位参考”,增加空间母版管理入口。先关闭图库并等待离场,再打开母版抽屉。 + +## 已接入 + +- 布局及完整母版列表、正式当前全局确认母版、生成任务记录;完成任务不等于质量通过。 +- `GET .../masters/recovery-impact`:当前版本/母版、关联镜头、历史 Primary、Anchor、Form,以及独立的图片完整性与来源当前性。原审核通过不等于当前来源通过。 +- `POST .../masters/generation-preflight`:明确选择历史确认母版;当前草稿仅在显式修订模式及同版本下可选。展示原图、最终提示词、参数、参考摘要和指纹。 +- `POST .../masters/generate`:完整复用同次预检输入,强制传 `expectedPreviewFingerprint` 与独立费用确认。结果仅为草稿,不自动审核、确认或重生下游素材。 +- 原有 `review` / `validate` / `confirm`:资产、人工审核、机器审核及来源版本分别呈现;逐项人工证据和真实确认人必须显式填写。AI 审核另行确认费用,不能填成人工通过。 +- 原任务记录展示原模型、参数、提示词及 `referenceSnapshot`,不使用当前素材反推原输入。 + +## 切换与费用保护 + +- 关闭、项目、布局、母版或预检参数变化取消旧免费请求,并拒绝迟到响应。再次进入同一范围也有请求代次保护。 +- 同步母版任务通过正式只读 GET 观察;保留既有 Vision WorkflowExecution / SSE 实现,不为同步接口伪造 SSE 或进度。 +- 输入或原件变化撤销旧预检和费用勾选;提交中锁定重复点击、标签切换和关闭。普通查询允许关闭。 +- 网络中断、畸形回执、Provider 后保存冲突继续保留未知费用保护;不会自动重发。400/409 只有在正式任务列表证明未创建新任务后才能解除本次保护。 +- 不自动恢复 interrupted 任务,不触发 General 重跑,不改历史 Primary。 + +## 验证 + +- 新增隔离 Mock 契约、表单与组件测试:历史参考、草稿修订、独立人工/机器结论、来源影响、指纹、参数变更、范围切换、断线、400/409 的付费前后区分和畸形回执。 +- 回归既有 Vision 多执行、分页、Physical Retry、复用步骤计量和任务切换测试。 +- 全量检查通过:104 个测试文件、774 项测试;lint、格式检查、类型检查与生产构建通过。 +- 本地 Chrome 复用已有标签页、`localhost:4312`。隔离页面验收免费预检、Mock 生成及独立审核;深浅主题、375×667 窄屏、长内容滚动、固定底栏均已检查。临时验收文件已移除。 +- 真实项目仅只读核对图库到母版抽屉的交接、当前/历史母版、人工确认与机器结论、来源影响、原任务参考摘要。未提交正式数据,真实 Provider 调用为 0。 + +## 真实后端缺口 + +母版 generation-preflight 和模型目录目前没有预计金额及货币字段。页面显示“预检接口未提供报价”,保留明确收费确认,不硬编码金额;后端提供正式报价契约后才能展示数值预计费用。 diff --git a/src/features/spatial-layouts/api.ts b/src/features/spatial-layouts/api.ts new file mode 100644 index 0000000..07a42ab --- /dev/null +++ b/src/features/spatial-layouts/api.ts @@ -0,0 +1,144 @@ +import { request } from '../../lib/http' +import type { + MasterGenerationInput, + MasterGenerationReceipt, + MasterHumanResult, + MasterJob, + MasterPreflight, + MasterRecoveryImpact, + MasterReview, + MasterVisionReceipt, + SceneMaster, + SpatialLayout +} from './types' + +/** + * 布局路径统一编码,不用场景身份或 Form ID 代替布局。 + * @param projectId 正式项目 + * @param layoutId 正式布局 + * @returns 布局范围路径 + */ +function path(projectId: string, layoutId: string) { + return `/projects/${encodeURIComponent(projectId)}/shared-spatial-layouts/${encodeURIComponent(layoutId)}/masters` +} +/** 正式母版路由,免费读取与付费操作分开,不自动重试任何写入。 */ +export const spatialMasterApi = { + /** + * 读取项目布局供现有来源入口和管理页复用。 + * @param projectId 项目编号 + * @param signal 取消信号 + * @returns 正式布局列表 + */ + layouts: (projectId: string, signal?: AbortSignal) => + request(`/projects/${encodeURIComponent(projectId)}/shared-spatial-layouts`, { signal }), + /** + * 保留历史确认和草稿的完整母版列表。 + * @param projectId 项目编号 + * @param layoutId 布局编号 + * @param signal 取消信号 + * @returns 母版资产 + */ + masters: (projectId: string, layoutId: string, signal?: AbortSignal) => + request(path(projectId, layoutId), { signal }), + /** + * 读取真正当前已确认的全局母版,空值为尚无记录。 + * @param projectId 项目编号 + * @param layoutId 布局编号 + * @param signal 取消信号 + * @returns 当前母版或 null + */ + confirmed: (projectId: string, layoutId: string, signal?: AbortSignal) => + request(path(projectId, layoutId) + '/confirmed', { signal }), + /** + * 免费读取来源影响。 + * @param projectId 项目编号 + * @param layoutId 布局编号 + * @param signal 取消信号 + * @returns 原件与来源独立诊断 + */ + impact: (projectId: string, layoutId: string, signal?: AbortSignal) => + request(path(projectId, layoutId) + '/recovery-impact', { signal, timeoutMs: 0 }), + /** + * 免费预检最终修订输入,不创建任务。 + * @param projectId 项目编号 + * @param layoutId 布局编号 + * @param input 本次完整参数 + * @param signal 取消信号 + * @returns 正式最终预览 + */ + preflight: (projectId: string, layoutId: string, input: MasterGenerationInput, signal?: AbortSignal) => + request(path(projectId, layoutId) + '/generation-preflight', { + method: 'POST', + body: input, + signal, + timeoutMs: 0 + }), + /** + * 明确付费后只生成一次,必须绑定本次预检指纹。 + * @param projectId 项目编号 + * @param layoutId 布局编号 + * @param input 已确认的完整参数和指纹 + * @returns 仅为草稿的回执 + */ + generate: ( + projectId: string, + layoutId: string, + input: MasterGenerationInput & { expectedPreviewFingerprint: string; confirmPaidGeneration: true } + ) => + request(path(projectId, layoutId) + '/generate', { + method: 'POST', + body: input, + timeoutMs: 0 + }), + /** + * 只读任务状态,不自动恢复或再生成。 + * @param projectId 项目编号 + * @param layoutId 布局编号 + * @param signal 取消信号 + * @returns 服务保存的任务 + */ + jobs: (projectId: string, layoutId: string, signal?: AbortSignal) => + request(path(projectId, layoutId) + '/generation-jobs', { signal }), + /** + * 免费读取逐项人工与独立机器证据。 + * @param projectId 项目编号 + * @param layoutId 布局编号 + * @param masterId 母版编号 + * @param signal 取消信号 + * @returns 正式审核材料 + */ + review: (projectId: string, layoutId: string, masterId: string, signal?: AbortSignal) => + request(path(projectId, layoutId) + '/' + encodeURIComponent(masterId) + '/review', { signal }), + /** + * 仅在用户独立确认本次 Vision 费用后调用,不自动确认母版。 + * @param projectId 项目编号 + * @param layoutId 布局编号 + * @param masterId 母版编号 + * @returns 独立机器证据 + */ + validate: (projectId: string, layoutId: string, masterId: string) => + request(path(projectId, layoutId) + '/' + encodeURIComponent(masterId) + '/validate', { + method: 'POST', + body: { confirmPaidValidation: true }, + timeoutMs: 0 + }), + /** + * 正式人工确认,传真实身份和每项证据,不冒充 Vision 通过。 + * @param projectId 项目编号 + * @param layoutId 布局编号 + * @param masterId 母版编号 + * @param input 当前版本及人工依据 + * @returns 已确认母版 + */ + confirm: ( + projectId: string, + layoutId: string, + masterId: string, + input: { expectedVersion: string; evidence: string; confirmedBy: string; reviewResults: MasterHumanResult[] } + ) => + request(path(projectId, layoutId) + '/' + encodeURIComponent(masterId) + '/confirm', { + method: 'POST', + body: input, + timeoutMs: 0 + }) +} diff --git a/src/features/spatial-layouts/components/MasterGenerationForm.vue b/src/features/spatial-layouts/components/MasterGenerationForm.vue new file mode 100644 index 0000000..d3f7b24 --- /dev/null +++ b/src/features/spatial-layouts/components/MasterGenerationForm.vue @@ -0,0 +1,149 @@ + + + diff --git a/src/features/spatial-layouts/components/MasterRecoveryImpact.vue b/src/features/spatial-layouts/components/MasterRecoveryImpact.vue new file mode 100644 index 0000000..e009645 --- /dev/null +++ b/src/features/spatial-layouts/components/MasterRecoveryImpact.vue @@ -0,0 +1,192 @@ + + + diff --git a/src/features/spatial-layouts/components/MasterReviewForm.vue b/src/features/spatial-layouts/components/MasterReviewForm.vue new file mode 100644 index 0000000..4332470 --- /dev/null +++ b/src/features/spatial-layouts/components/MasterReviewForm.vue @@ -0,0 +1,301 @@ + + + diff --git a/src/features/spatial-layouts/components/SceneMasterManager.vue b/src/features/spatial-layouts/components/SceneMasterManager.vue new file mode 100644 index 0000000..a712c27 --- /dev/null +++ b/src/features/spatial-layouts/components/SceneMasterManager.vue @@ -0,0 +1,366 @@ + + + diff --git a/src/features/spatial-layouts/model.ts b/src/features/spatial-layouts/model.ts new file mode 100644 index 0000000..9d89ac6 --- /dev/null +++ b/src/features/spatial-layouts/model.ts @@ -0,0 +1,76 @@ +import type { MasterPreflight, MasterPreview, MasterReview, SceneMaster, ImpactCandidate } from './types' + +/** + * 历史确认母版可恢复;当前版草稿只有明确图片修订时可引用。 + * @param master 正式资产状态 + * @param version 当前几何版本 + * @param revision 是否有明确修订意图 + * @returns 本次是否可以选择为参考 + */ +export function canUseMasterReference( + master: Pick, + version: string, + revision: boolean +) { + return ( + master.status === 'confirmed' || + (revision && master.status === 'draft' && master.spatialLayoutVersion === version) + ) +} +/** + * 人工、机器及资产状态分别展示,不从 confirmed 推断 Vision。 + * @param review 正式审核摘要 + * @returns 独立状态标签 + */ +export function masterReviewLabels( + review: Pick +) { + return { + asset: review.status === 'confirmed' ? '已确认' : review.status === 'draft' ? '草稿' : '状态待核对', + human: review.reviewStatus === 'human_passed' ? '人工审核通过' : '人工审核待核对', + vision: review.currentVersion && review.automatedVisionPassed ? 'AI 视觉审核通过' : 'AI 视觉审核未通过或未运行' + } +} +/** + * 将正式预检适配现有图片输入组件,显示原图而不把 Base64 放进页面。 + * @param preflight 本次服务端完整预检 + * @returns 共用展示组件所需预览 + */ +export function toMasterPreview(preflight: MasterPreflight): MasterPreview { + return { + preflight, + provider: preflight.provider, + model: preflight.model, + previewFingerprint: preflight.previewFingerprint, + providerPrompt: preflight.prompt, + referenceImages: preflight.references.map(item => item.imageUrl), + generationConfig: preflight.generationOptions, + actualProviderReferenceCount: preflight.referenceCount, + references: preflight.references.map((item, index) => ({ + imageNumber: index + 1, + role: item.role ?? 'identity-anchor', + imageId: item.imageId, + imageUrl: item.imageUrl + })) + } +} + +/** + * 原任务执行完成与原质量结论分开,历史通过也不代表当前来源有效。 + * @param vision 正式影响分析保留的原审核摘要 + * @returns 只描述可核实的原始结论 + */ +export function impactVisionLabel(vision: NonNullable) { + if (vision.status !== 'completed') return '原审核未完成' + const labels: Record = { + failed: '原结论未通过', + needs_review: '原结论待人工复核', + stale: '原审核来源已失效', + blocked: '原质量受阻', + insufficient_evidence: '原审核证据不足' + } + if (vision.qualityDecision && labels[vision.qualityDecision]) return labels[vision.qualityDecision]! + if (vision.passed === true && vision.qualityDecision === 'passed') + return vision.evidenceComplete ? '原结论通过' : '原结论通过,完整证据待核验' + return vision.passed === false ? '原结论未通过' : '原质量结论待核验' +} diff --git a/src/features/spatial-layouts/types.ts b/src/features/spatial-layouts/types.ts new file mode 100644 index 0000000..3c909f5 --- /dev/null +++ b/src/features/spatial-layouts/types.ts @@ -0,0 +1,232 @@ +import type { ImageGenerationPreview } from '../subject-images/generation-preview' +import type { GenerationOptions } from '../generation-config/types' + +/** 空间布局的正式身份与当前几何版本。 */ +export interface SpatialLayout { + /** 布局编号。 */ id: string + /** 所属项目。 */ projectId: string + /** 用户定义的布局名称。 */ name: string + /** 当前布局版本。 */ version: string + /** 正式关联场景。 */ sceneSubjectIds: string[] +} +/** 草稿或已确认的母版原始资产;确认不等于 Vision 通过。 */ +export interface SceneMaster { + /** 母版编号。 */ id: string + /** 正式项目。 */ projectId: string + /** 正式布局。 */ spatialLayoutId: string + /** 本图绑定版本。 */ spatialLayoutVersion: string + /** 原始图片。 */ imageUrl: string + /** 数据库原状态。 */ status: string + /** 特定形态机位,空值为全局母版。 */ subjectFormId: string | null + /** 人工机位描述。 */ viewDescription: string | null + /** 原始确认依据。 */ evidence: string | null + /** 人工确认人。 */ confirmedBy: string | null + /** 人工确认时间。 */ confirmedAt: string | null + /** 机器审核时间,不表示通过。 */ visionReviewedAt: string | null + /** 创建时间。 */ createdAt: string + /** 最近更新。 */ updatedAt: string +} +/** 单次母版生成参数,原样复用预检到生成。 */ +export interface MasterGenerationInput { + /** 本次布局版本。 */ expectedVersion: string + /** 明确选择的历史或草稿母版。 */ referenceMasterId?: string + /** 仅有此字段时才允许当前版本 draft 作修订参考。 */ revisionInstruction?: string + /** 本次明确的模型供应方。 */ providerName?: string + /** 本次模型选项。 */ generationOptions?: GenerationOptions +} +/** 预检返回的实际参考职责和原件证据。 */ +export interface MasterReference { + /** 正式参考编号。 */ imageId: string + /** 持久化原图。 */ imageUrl: string + /** 真实送图地址,可能为大体积 Base64,不展示或存储。 */ providerImageUrl: string + /** 送图是否解析成功。 */ providerInputResolved: boolean + /** 布局母版具有独立角色。 */ role?: 'layout-master' + /** 共享主体编号。 */ subjectId?: string + /** 共享主体引用。 */ subjectRef?: string + /** 历史几何版本。 */ sourceLayoutVersion?: string + /** 历史确认或待修订草稿。 */ sourceMasterStatus?: 'draft' | 'confirmed' + /** 原件字节摘要。 */ sha256?: string + /** 像素摘要。 */ pixelFingerprint?: string + /** 原始宽度。 */ width?: number + /** 原始高度。 */ height?: number +} +/** 免费预检的正式 DTO;接口没有报价字段。 */ +export interface MasterPreflight { + /** 布局编号。 */ layoutId: string + /** 本次当前版本。 */ layoutVersion: string + /** 实际供应方。 */ provider: string + /** 实际模型。 */ model: string + /** 最终实际参数。 */ generationOptions: GenerationOptions + /** 最终画面描述。 */ prompt: string + /** 按真实顺序的参考。 */ references: MasterReference[] + /** 同次输入指纹。 */ previewFingerprint: string + /** 用户选择的参考。 */ referenceMasterId: string | null + /** 同次明确修订文字。 */ revisionInstruction: string | null + /** 实际参考数。 */ referenceCount: number + /** 已核对的场景数量。 */ verifiedSceneCount: number + /** 模型上限,空值表示无公开上限。 */ maxReferenceImages: number | null + /** 免费请求不会启动生成。 */ generationAllowed: false + /** 公开业务原因。 */ reason: string +} +/** 适配既有实际输入展示组件,不改变正式参数或推断报价。 */ +export interface MasterPreview extends ImageGenerationPreview { + /** 母版原始预检,保留数据层的真实返回值。 */ preflight: MasterPreflight +} +/** 正式单次生图回执,只说明草稿已登记。 */ +export interface MasterGenerationReceipt { + /** 原持久化任务。 */ jobId: string + /** 新生成的草稿。 */ masterId: string + /** 已保存原图。 */ imageUrl: string + /** 同步执行结束,不表示质量通过。 */ status: 'completed' +} +/** 母版持久化生成任务;中断可能已经计费,不能自动重发。 */ +export interface MasterJob { + /** 正式任务。 */ id: string + /** 原项目。 */ projectId: string + /** 原布局。 */ spatialLayoutId: string + /** 任务绑定版本。 */ spatialLayoutVersion: string + /** 原执行状态。 */ status: string + /** 本次模型供应方。 */ provider: string + /** 本次模型。 */ model: string + /** 本次实际提示词。 */ prompt: string + /** 原任务实际选项。 */ generationOptions: GenerationOptions + /** 原任务不可变参考摘要,不用当前素材反推。 */ referenceSnapshot: Array< + Pick< + MasterReference, + 'imageId' | 'subjectId' | 'role' | 'sourceLayoutVersion' | 'sourceMasterStatus' | 'sha256' + > + > + /** 已存远程或本地结果。 */ imageUrl: string | null + /** 原错误保留数据层。 */ errorMessage: string | null + /** 创建时间。 */ createdAt: string + /** 更新时间。 */ updatedAt: string +} +/** 必须由用户逐项核对的正式人工证据。 */ +export interface MasterHumanResult { + /** 一基审核项。 */ index: number + /** 用户明确选择,打开不默认通过。 */ result: 'passed' | 'failed' | 'needs_review' + /** 人工依据。 */ evidence: string +} +/** 人工审核预览,不消耗 Vision 费用。 */ +export interface MasterReview { + /** 原母版。 */ masterId: string + /** 当前图像。 */ imageUrl: string + /** 当前布局。 */ layoutId: string + /** 当前几何版本。 */ layoutVersion: string + /** 图像是否仍绑定当前版本。 */ currentVersion: boolean + /** 原资产状态。 */ status: string + /** 已保存的逐项人工审核状态。 */ reviewStatus: 'human_passed' | 'needs_review' + /** 独立机器结论,不能从 confirmed 推断。 */ automatedVisionPassed: boolean + /** 原始自动审核摘要,普通区域不直接展示内部对象。 */ automatedVisionReview: Record | null + /** 适用场景。 */ sceneSubjectIds: string[] + /** 机位形态。 */ subjectFormId: string | null + /** 机位描述。 */ viewDescription: string | null + /** 逐项真实规则。 */ checklist: Array<{ + /** 一基索引。 */ index: number + /** 规则类别。 */ kind: string + /** 关联主体。 */ subjectId: string + /** 参照主体。 */ referenceSubjectId: string | null + /** 要求。 */ requirement: string + /** 验收准则。 */ acceptance: string + /** 服务已保存结果。 */ result: MasterHumanResult['result'] + }> + /** 只说明版本和 draft 允许发起人工确认。 */ canConfirm: boolean + /** 已保存人工证据,不能在 draft 上伪造。 */ humanReviewEvidence: { + /** 人工总依据。 */ summary: string + /** 逐项人工结果。 */ compositionReviews: MasterHumanResult[] + } | null + /** 公开边界。 */ note: string +} +/** 自动 Vision 回执,顶层 passed=false 保留正式人工门禁。 */ +export interface MasterVisionReceipt { + /** 所属母版。 */ masterId: string + /** 所属布局。 */ layoutId: string + /** 本次版本。 */ layoutVersion: string + /** 推理后来源是否仍一致。 */ currentVersion: boolean + /** 本次结果是否已存证。 */ persisted: boolean + /** 机器独立通过,不代表人工确认。 */ automatedVisionPassed: boolean + /** 实际检查状态。 */ reviewStatus: string + /** 结果摘要。 */ summary: string +} +/** 来源影响中的历史候选,原件完整性和来源当前性独立。 */ +export interface ImpactCandidate { + /** 历史候选。 */ id: string + /** 保留的历史主图。 */ isPrimary: boolean + /** 原执行状态。 */ status: string + /** 原始图片。 */ imageUrl: string | null + /** 原件宽度。 */ width: number | null + /** 原件高度。 */ height: number | null + /** 原件与原审核的关系。 */ imageIntegrity: 'matches_review' | 'readable_unbound' | 'changed' | 'unavailable' + /** 来源当前性,不由原件可读推断。 */ sourceStatus: + | 'current' + | 'layout_version_changed' + | 'stale_or_unverified' + | 'unknown' + /** 来源说明。 */ sourceReason: string | null + /** 应保留或重新核验,不自动重生。 */ action: 'retain' | 'revalidate' + /** 原始不可变生成输入。 */ providerInputFingerprint: string | null + /** 历史审核,不等于当前通过。 */ latestVision: { + /** 原记录编号。 */ id: string + /** 原执行状态。 */ status: string + /** 原审核是否通过。 */ passed: boolean | null + /** 原质量结论。 */ qualityDecision: string | null + /** 原审核证据是否完整。 */ evidenceComplete: boolean + /** 是否为历史来源结论。 */ historical: boolean + } | null + /** 原参考证据,只在高级详情展开。 */ referenceEvidence: Array<{ + /** 参考图片序号。 */ imageNumber: number + /** 原参考内容校验值。 */ recordedSha256: string + /** 原参考绑定与完整性。 */ bindings: Array< + Record & { imageIntegrity: 'matches_input' | 'changed' | 'unavailable' } + > + }> +} +/** 正式布局范围的免费恢复影响,不授予生成或晋升资格。 */ +export interface MasterRecoveryImpact { + /** 当前项目。 */ projectId: string + /** 当前布局。 */ layoutId: string + /** 当前布局版本。 */ layoutVersion: string + /** 真正当前已确认母版。 */ currentMaster: SceneMaster | null + /** 真实汇总。 */ summary: { + /** 关联镜头数量。 */ affectedShots: number + /** 保留历史主图的镜头数。 */ primaryShots: number + /** 存在审核任务的镜头数。 */ shotsWithVision: number + /** 暂无首帧的镜头数。 */ shotsWithoutAssets: number + /** 历史候选数量。 */ candidateCount: number + /** 主图待重新核验的镜头数。 */ primaryRevalidationShots: number + } + /** 逐镜头历史及原主图。 */ shots: Array<{ + /** 正式镜头编号。 */ shotId: string + /** 剧集序号。 */ episodeNo: number + /** 节拍序号。 */ beatNo: number + /** 镜头序号。 */ shotNo: number + /** 镜头标题。 */ title: string + /** 历史主图数量。 */ primaryCount: number + /** 原主图记录。 */ primary: ImpactCandidate | null + /** 审核任务数量。 */ visionTaskCount: number + /** 全部历史候选。 */ candidates: ImpactCandidate[] + }> + /** 按正式场景关联的旧 Anchor 与 Form。 */ assets: Array<{ + /** 场景身份。 */ subjectId: string + /** 场景引用。 */ ref: string + /** 场景名称。 */ name: string + /** 原形态和主图。 */ forms: Array<{ + /** 原记录编号。 */ id: string + /** 正式名称。 */ name: string + /** 形态原主图。 */ primaryImages: Array<{ + /** 原记录编号。 */ id: string + /** 原图片地址。 */ imageUrl: string | null + /** 原件可读取状态。 */ fileStatus: 'readable' | 'unavailable' + /** 来源当前性及原因。 */ currentness: { current: boolean; reason?: string | null } + }> + }> + /** 原身份锚图,只给出待重验,不能假定来源已通过。 */ anchors: Array<{ + /** 原记录编号。 */ id: string + /** 原图片地址。 */ imageUrl: string | null + /** 原件可读取状态。 */ fileStatus: 'readable' | 'unavailable' + /** 需重新核验,不自动重生。 */ action: 'revalidate' + }> + }> + /** 不允许直接生成。 */ generationAllowed: false + /** 公开限制。 */ limitation: string +} diff --git a/src/features/spatial-layouts/useMasterGeneration.ts b/src/features/spatial-layouts/useMasterGeneration.ts new file mode 100644 index 0000000..5ae0f94 --- /dev/null +++ b/src/features/spatial-layouts/useMasterGeneration.ts @@ -0,0 +1,316 @@ +import { computed, onScopeDispose, ref, shallowRef, watch } from 'vue' +import { useQuery } from '../../composables/useQuery' +import { ApiError, errorMessage } from '../../lib/http' +import { useActionMessage, showActionFeedback } from '../../components/ui/useActionMessage' +import { generationConfigApi } from '../generation-config/api' +import type { GenerationOptions } from '../generation-config/types' +import { getOperation, runOperation } from '../workflows/operations' +import { imageRequestNotice, runGuardedImageRequest } from '../subject-images/image-request-guard' +import { spatialMasterApi } from './api' +import { canUseMasterReference, toMasterPreview } from './model' +import type { MasterGenerationReceipt, MasterPreview, SceneMaster, SpatialLayout } from './types' + +/** + * 免费预检和单次付费生成,版本或输入变化必须重新确认;断线不重发。 + * @param props 当前项目、布局、母版和活动锁 + * @param changed 原范围内成功生成后的刷新通知 + * @param stale 原范围来源冲突时的只读刷新通知 + * @returns 表单、实际预览和受费用保护的提交入口 + */ +export function useMasterGeneration( + props: { projectId: string; layout: SpatialLayout; masters: SceneMaster[]; disabled: boolean }, + changed: (receipt: MasterGenerationReceipt) => void, + stale: () => void +) { + /** 用户明确选择的参考,不默认选取旧图。 */ + const referenceId = ref('') + /** 显式图片修订模式,不修改正式布局。 */ + const revisionMode = ref(false) + /** 用户本次限定修订。 */ + const instruction = ref('') + /** 本次选择的模型。 */ + const provider = ref('') + /** 本次明确参数。 */ + const options = ref({}) + /** 原项目配置查询身份。 */ + const context = computed(() => JSON.stringify([props.projectId, props.layout.id])) + /** 复用现有目录和配置,切项目取消旧请求。 */ + const config = useQuery( + computed(() => props.projectId), + async (projectId, signal) => { + const [catalog, profile] = await Promise.all([ + generationConfigApi.catalog(signal), + generationConfigApi.profile(projectId, signal) + ]) + if (profile && profile.projectId !== projectId) throw new Error('生成配置不属于当前项目。') + return { catalog, profile } + } + ) + /** 已取得有效的启用图片模型,不猜测默认模型。 */ + const modelReady = computed( + () => + !!config.data.value?.profile && + !config.error.value && + !!config.data.value.catalog.images.find(item => item.provider === provider.value && item.enabled) + ) + /** 当前模式真正允许选择的正式参考。 */ + const references = computed(() => + props.masters.filter( + item => + item.projectId === props.projectId && + item.spatialLayoutId === props.layout.id && + canUseMasterReference(item, props.layout.version, revisionMode.value) + ) + ) + /** 用户选中的原始母版。 */ + const selectedReference = computed(() => + props.masters.find( + item => + item.id === referenceId.value && + item.projectId === props.projectId && + item.spatialLayoutId === props.layout.id + ) + ) + /** 同一参数对象贯穿预检和正式提交。 */ + const input = computed(() => ({ + expectedVersion: props.layout.version, + ...(referenceId.value ? { referenceMasterId: referenceId.value } : {}), + ...(revisionMode.value ? { revisionInstruction: instruction.value.trim() } : {}), + providerName: provider.value, + generationOptions: { ...options.value } + })) + /** 校验只控制操作,不在打开空表单时显示红色错误。 */ + const valid = computed( + () => + modelReady.value && + (!referenceId.value || references.value.some(item => item.id === referenceId.value)) && + (!revisionMode.value || + (!!referenceId.value && !!instruction.value.trim() && instruction.value.trim().length <= 2000)) + ) + /** 只有本次参数和原件状态完全相同时才保留预览。 */ + const identity = computed(() => + JSON.stringify([ + context.value, + input.value, + props.masters.map(item => [item.id, item.status, item.updatedAt, item.imageUrl, item.spatialLayoutVersion]) + ]) + ) + /** 只在内存保存本次免费预检,不持久化 Base64。 */ + const preview = shallowRef(null) + /** 独立确认一次生成费用和范围。 */ + const acknowledged = ref(false) + /** 免费预览加载不锁关闭。 */ + const loading = ref(false) + /** 同步长请求进行中不重复提交。 */ + const submitting = ref(false) + /** 原任务的未知费用保护不随选项变化解除。 */ + const recoveryTarget = computed(() => `scene-layout-master:${props.projectId}:${props.layout.id}`) + /** 同项目活动任务和来源限制沿用既有操作锁。 */ + const canSubmit = computed( + () => + valid.value && + !!preview.value && + acknowledged.value && + !loading.value && + !submitting.value && + !props.disabled && + !getOperation(props.projectId).pending && + !imageRequestNotice(recoveryTarget.value) + ) + /** 只在请求结束后显示未知结果提示。 */ + const recoveryNotice = computed(() => (submitting.value ? '' : imageRequestNotice(recoveryTarget.value))) + /** 消息层统一反馈。 */ + const message = useActionMessage() + /** 取消旧预检并拒绝相同参数的迟到结果。 */ + let revision = 0 + /** 免费请求可取消,付费提交保持观察。 */ + let controller: AbortController | undefined + /** 组件卸载后不回填。 */ + let disposed = false + /** 清除本次预览与费用勾选,保留未知费用标记。 */ + function invalidate() { + revision++ + controller?.abort() + preview.value = null + acknowledged.value = false + loading.value = false + } + watch(identity, invalidate, { flush: 'sync' }) + watch( + context, + () => { + referenceId.value = '' + revisionMode.value = false + instruction.value = '' + provider.value = '' + options.value = {} + }, + { flush: 'sync' } + ) + watch( + [() => config.data.value, context], + ([data]) => { + if (!data?.profile || data.profile.projectId !== props.projectId) return + const model = data.catalog.images.find( + item => item.enabled && item.provider === data.profile!.imageProvider + ) + if (model) { + provider.value = model.provider + options.value = { ...model.defaults, ...data.profile.imageOptions } + } + }, + { immediate: true } + ) + watch( + references, + items => { + if (referenceId.value && !items.some(item => item.id === referenceId.value)) referenceId.value = '' + }, + { flush: 'sync' } + ) + onScopeDispose(() => { + disposed = true + invalidate() + }) + /** + * 只读编译实际生成输入,并验证版本、参考和完整指纹。 + * @returns 当前预检结束 + */ + async function loadPreview() { + if (!valid.value || submitting.value || loading.value) return + invalidate() + const current = revision + const target = identity.value + const projectId = props.projectId + const layoutId = props.layout.id + const snapshot = JSON.parse(JSON.stringify(input.value)) as typeof input.value + controller = new AbortController() + loading.value = true + try { + const result = await spatialMasterApi.preflight(projectId, layoutId, snapshot, controller.signal) + if (disposed || target !== identity.value || current !== revision) return + if ( + !result || + result.layoutId !== layoutId || + result.layoutVersion !== snapshot.expectedVersion || + result.referenceMasterId !== (snapshot.referenceMasterId ?? null) || + result.revisionInstruction !== (snapshot.revisionInstruction ?? null) || + result.provider !== snapshot.providerName || + !result.model || + !result.prompt?.trim() || + !/^[a-f0-9]{64}$/.test(result.previewFingerprint) || + !Array.isArray(result.references) || + result.referenceCount !== result.references.length || + result.references.some(item => !item.providerInputResolved) + ) + throw new Error('母版预检与当前输入不一致,请重新核对。') + preview.value = toMasterPreview(result) + } catch (cause) { + if (!disposed && target === identity.value && current === revision) + showActionFeedback(message, errorMessage(cause), 'error') + } finally { + if (current === revision) loading.value = false + } + } + /** + * 固定同次免费输入,只提交一个生成请求,未知费用永不自动重发。 + * @returns 单次请求与必要只读核查完成 + */ + async function submit() { + if (!canSubmit.value || !preview.value) return + const target = identity.value + const requestRevision = revision + const projectId = props.projectId + const layoutId = props.layout.id + const version = props.layout.version + const guard = recoveryTarget.value + const snapshot = JSON.parse( + JSON.stringify({ + ...input.value, + expectedPreviewFingerprint: preview.value.previewFingerprint, + confirmPaidGeneration: true as const + }) + ) as typeof input.value & { expectedPreviewFingerprint: string; confirmPaidGeneration: true } + acknowledged.value = false + submitting.value = true + const ok = await runOperation(projectId, '生成一张空间母版草稿', async () => { + const before = await spatialMasterApi.jobs(projectId, layoutId) + if ( + before.some( + job => + job.projectId !== projectId || + job.spatialLayoutId !== layoutId || + !['completed', 'failed'].includes(job.status) + ) + ) + throw new Error('已有母版任务进行中或结果待核查,请刷新任务记录。') + if (disposed || target !== identity.value || requestRevision !== revision) + throw new Error('已离开原母版输入,本次未提交。') + return runGuardedImageRequest( + guard, + async () => { + const receipt = await spatialMasterApi.generate(projectId, layoutId, snapshot) + if ( + !receipt || + receipt.status !== 'completed' || + !receipt.jobId || + !receipt.masterId || + !receipt.imageUrl + ) + throw new Error('生成回执不完整,请先读取任务记录,勿再次生图。') + if (!disposed && target === identity.value && requestRevision === revision) { + preview.value = null + changed(receipt) + } + return receipt + }, + { + confirmRejection: async cause => { + if (!(cause instanceof ApiError) || ![400, 409].includes(cause.status)) return false + const after = await spatialMasterApi.jobs(projectId, layoutId) + return after.every( + job => + job.projectId === projectId && + job.spatialLayoutId === layoutId && + before.some(old => old.id === job.id) + ) + } + } + ) + }) + submitting.value = false + if (disposed || target !== identity.value || requestRevision !== revision) return + if (ok) showActionFeedback(message, '已生成母版草稿,尚未完成质量审核与人工确认。', 'info') + else { + const operation = getOperation(projectId) + showActionFeedback(message, operation.error, 'error') + preview.value = null + if ( + operation.cause instanceof ApiError && + operation.cause.status === 409 && + props.layout.version === version + ) + stale() + } + } + return { + referenceId, + revisionMode, + instruction, + provider, + options, + config, + modelReady, + references, + selectedReference, + valid, + preview, + acknowledged, + loading, + submitting, + canSubmit, + recoveryNotice, + loadPreview, + submit + } +} diff --git a/src/features/subject-images/SubjectImagesPage.vue b/src/features/subject-images/SubjectImagesPage.vue index e367bae..6bf4fa3 100644 --- a/src/features/subject-images/SubjectImagesPage.vue +++ b/src/features/subject-images/SubjectImagesPage.vue @@ -1,4 +1,5 @@