video-flow-b/app/model/sse_response.ts

177 lines
3.9 KiB
TypeScript

/**
* SSE 响应数据结构
* 对应 Python 后端的 sse_response.py
*/
import { TaskStatusEnum, FlowStageEnum } from './enums';
// 重新导出枚举以供其他模块使用
export { TaskStatusEnum, FlowStageEnum };
// 完成状态对象
export interface CompletionStatus {
total: number;
completed: number;
all_completed: boolean;
}
// 预览图对象
export interface PreviewObj {
scene_id: number;
prompt: string;
order: number;
preview_pic_url?: string;
status: string;
updated_at?: string;
}
// 角色对象
export interface CharacterObj {
name: string;
desc: string;
image_url?: string;
voice_desc: string;
voice_url?: string;
}
// 分镜视频对象
export interface ShotObj {
scene_id: number;
prompt: string;
order: number;
transition: string;
volume: number;
video_url?: string;
status: string;
updated_at?: string;
}
// 背景音乐对象
export interface BgmObj {
bgm_id?: number;
name?: string;
url?: string;
status: string;
}
// 后期制作对象
export interface PostProductionObj {
episode_status: number;
is_processing: boolean;
all_completed: boolean;
}
// 最终视频对象
export interface FinalVideoObj {
video_url?: string;
is_completed: boolean;
}
// SSE 响应数据联合类型
export type SseResponseData =
| PreviewObj
| CharacterObj
| ShotObj
| BgmObj
| PostProductionObj
| FinalVideoObj
| null;
// SSE 响应对象
export interface SseResponse {
code: number;
message: string;
data?: SseResponseData;
stage: number;
stage_index: number;
status: string;
completion_status?: CompletionStatus;
}
// 使用统一的枚举
export const WorkflowStageEnum = FlowStageEnum;
export const WorkflowStatusEnum = TaskStatusEnum;
// 类型守卫函数
export function isPreviewObj(data: SseResponseData): data is PreviewObj {
return data !== null && typeof data === 'object' && 'scene_id' in data && 'preview_pic_url' in data;
}
export function isCharacterObj(data: SseResponseData): data is CharacterObj {
return data !== null && typeof data === 'object' && 'name' in data && 'voice_desc' in data;
}
export function isShotObj(data: SseResponseData): data is ShotObj {
return data !== null && typeof data === 'object' && 'scene_id' in data && 'video_url' in data;
}
export function isBgmObj(data: SseResponseData): data is BgmObj {
return data !== null && typeof data === 'object' && 'bgm_id' in data;
}
export function isPostProductionObj(data: SseResponseData): data is PostProductionObj {
return data !== null && typeof data === 'object' && 'episode_status' in data && 'is_processing' in data;
}
export function isFinalVideoObj(data: SseResponseData): data is FinalVideoObj {
return data !== null && typeof data === 'object' && 'video_url' in data && 'is_completed' in data;
}
// 工厂函数
export class SseResponseFactory {
static success(
stage: number = 0,
stage_index: number = 0,
status: string = '',
message: string = '操作成功',
data?: SseResponseData,
completion_status?: CompletionStatus
): SseResponse {
return {
code: 0,
stage: stage,
stage_index: stage_index,
status: status,
message: message,
data: data,
completion_status: completion_status
};
}
static fail(
stage: number = 0,
stage_index: number = 0,
status: string = '',
code: number = 1,
message: string = '操作失败',
data?: SseResponseData
): SseResponse {
return {
code: code,
stage: stage,
stage_index: stage_index,
status: status,
message: message,
data: data
};
}
static withCompletionStatus(
stage: number,
stage_index: number,
status: string,
message: string,
completion_status: CompletionStatus
): SseResponse {
return {
code: 0,
stage: stage,
stage_index: stage_index,
status: status,
message: message,
data: null,
completion_status: completion_status
};
}
}