video-flow-b/app/service/usecase/RoleEditUseCase.ts

192 lines
5.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { RoleEntity } from '../domain/Entities';
import { TagValueObject } from '../domain/valueObject';
import {
applyRoleToShots,
getRoleList,
getUserRoleLibrary,
getRoleData,
regenerateRole,
getRoleShots,
replaceRole
} from '@/api/video_flow';
import { TagEditUseCase } from './TagEditUseCase';
/**
* 角色图编辑用例
* 负责角色图内容的初始化、修改和优化
*/
export class RoleEditUseCase {
roleList: RoleEntity[] = [];
selectedRole: RoleEntity | null = null;
selectedRoleTags:TagEditUseCase = new TagEditUseCase([]);
roleLibraryList: RoleEntity[] = [];
constructor() {
}
/**
* 获取当前项目角色列表
* @param projectId 项目ID
* @returns Promise<RoleEntity[]> 角色列表
*/
async getRoleList(projectId: string): Promise<RoleEntity[]> {
try {
const response = await getRoleList({ projectId });
if (response.successful) {
return response.data;
} else {
throw new Error(response.message || '获取角色列表失败');
}
} catch (error) {
console.error('获取角色列表失败:', error);
throw error;
}
}
/**
* 获取角色库的角色列表
* @returns Promise<RoleEntity[]> 角色库列表
*/
async getRoleLibraryList(): Promise<RoleEntity[]> {
try {
const response = await getUserRoleLibrary();
if (response.successful) {
return response.data;
} else {
throw new Error(response.message || '获取角色库失败');
}
} catch (error) {
console.error('获取角色库失败:', error);
throw error;
}
}
/**
* 修改某个标签内容
* @param tagName 标签名称
* @param newContent 新内容
*/
async updateTag(tagName: string, newContent: string | number): Promise<void> {
await this.selectedRoleTags.updateTag(tagName, newContent);
}
/**
* 选中某个角色作为当前活跃角色
* @param roleId 角色ID
*/
async selectRole(roleId: string): Promise<void> {
try {
// 从已获取的角色列表中查找对应的角色实体
const roleEntity = this.roleList.find(role => role.id === roleId);
if (roleEntity) {
this.selectedRole = roleEntity;
// 获取角色数据以获取标签信息
const response = await getRoleData({ roleId });
if (response.successful) {
this.selectedRoleTags = new TagEditUseCase(response.data.tags);
} else {
throw new Error(response.message || '获取角色标签数据失败');
}
} else {
throw new Error('未找到对应的角色实体,请先获取角色列表');
}
} catch (error) {
console.error('选择角色失败:', error);
throw error;
}
}
/**
* 重新生成角色
* @param prompt 角色提示词
* @param tags 标签列表
* @returns Promise<RoleEntity> 重新生成的角色
*/
async AIgenerateRole(prompt: string, tags: TagValueObject[]): Promise<RoleEntity> {
try {
// 直接使用当前角色的ID不做任何处理
const response = await regenerateRole({
prompt,
tagTypes: tags, // 直接传递完整的标签列表给后端,让后端处理
roleId: this.selectedRole?.id
});
if (response.successful) {
return response.data;
} else {
throw new Error(response.message || '重新生成角色失败');
}
} catch (error) {
console.error('重新生成角色失败:', error);
throw error;
}
}
/**
* 应用此角色到指定分镜
* @param shotIds 分镜ID列表
* @param roleId 角色ID
* @returns 应用结果
*/
async applyRole(shotIds: string[], roleId: string) {
try {
const response = await applyRoleToShots({
roleId,
shotIds,
});
if (response.successful) {
return response.data;
} else {
throw new Error(response.message || '应用角色到分镜失败');
}
} catch (error) {
console.error('应用角色到分镜失败:', error);
throw error;
}
}
/**
* 获取角色应用到的分镜列表
* @param roleId 角色ID
* @returns 分镜列表和应用状态
*/
async getRoleShotsList(roleId: string) {
try {
const response = await getRoleShots({ roleId });
if (response.successful) {
return response.data;
} else {
throw new Error(response.message || '获取角色分镜列表失败');
}
} catch (error) {
console.error('获取角色分镜列表失败:', error);
throw error;
}
}
/**
* 替换角色
* @param currentRoleId 当前角色ID
* @param replaceRoleId 替换的角色ID
* @returns 替换结果
*/
async replaceRoleById(currentRoleId: string, replaceRoleId: string) {
try {
const response = await replaceRole({
currentRoleId,
replaceRoleId,
});
if (response.successful) {
return response.data;
} else {
throw new Error(response.message || '替换角色失败');
}
} catch (error) {
console.error('替换角色失败:', error);
throw error;
}
}
}