🎬 火宝短剧 - 基于AI的一站式短剧生成平台 《一句话生成完整短剧,从剧本到成片全自动化》 Huobao Drama - An AI-Powered End-to-End Short Drama Generator "One Sentence to Complete Drama: Fully Automated from Script to Final Video"
TypeScript
/**
* 批量视频提示词任务 — 异步为缺少 video_prompt 的分镜逐个运行 prompt_generator Agent
* 进程内内存态:按集跟踪一份任务,运行中不重复启动;重启后状态丢失
*/
import { eq } from 'drizzle-orm'
import { db, schema } from '../db/index.js'
import { mastra } from '../mastra/index.js'
import { buildAgentRequestContext } from '../agents/context.js'
import { logTaskError, logTaskProgress, logTaskStart, logTaskSuccess } from '../utils/task-logger.js'
export interface VideoPromptBatchStatus {
status: 'running' | 'done' | 'error'
total: number
completed: number
failed: number
current_storyboard_id?: number
started_at: string
finished_at?: string
error?: string
}
const tasks = new Map<number, VideoPromptBatchStatus>()
/** 启动批量生成(立即返回);运行中返回 started:false,total:-1;无待生成分镜返回 started:false,total:0;
* 传入 storyboardIds 时只处理所选分镜(即使已有提示词也重新生成),否则处理全部缺失提示词的分镜 */
export async function startVideoPromptBatch(
episodeId: number,
dramaId: number,
opts: { model?: string; configId?: number } = {},
storyboardIds?: number[],
): Promise<{ started: boolean; total: number }> {
if (tasks.get(episodeId)?.status === 'running') return { started: false, total: -1 }
const sbs = await db.select().from(schema.storyboards)
.where(eq(schema.storyboards.episodeId, episodeId))
.orderBy(schema.storyboards.storyboardNumber)
const pending = storyboardIds?.length
? sbs.filter(sb => storyboardIds.includes(sb.id))
: sbs.filter(sb => !(sb.videoPrompt || '').trim())
if (!pending.length) return { started: false, total: 0 }
// 视频模型标签:跟随该集锁定的视频配置,供 Agent 按模型特性生成
const [ep] = await db.select().from(schema.episodes).where(eq(schema.episodes.id, episodeId))
let videoLabel = '默认'
if (ep?.videoConfigId) {
const [cfg] = await db.select().from(schema.aiServiceConfigs).where(eq(schema.aiServiceConfigs.id, ep.videoConfigId))
if (cfg) videoLabel = `${cfg.name} (${cfg.provider})`
}
const task: VideoPromptBatchStatus = {
status: 'running',
total: pending.length,
completed: 0,
failed: 0,
started_at: new Date().toISOString(),
}
tasks.set(episodeId, task)
logTaskStart('VideoPrompt', 'batch', { episodeId, dramaId, total: pending.length, model: opts.model || undefined })
;(async () => {
const agent = mastra.getAgent('prompt_generator')
if (!agent) throw new Error('视频提示词 Agent 不可用')
const requestContext = buildAgentRequestContext({
episodeId,
dramaId,
modelOverride: opts.model || undefined,
textConfigId: opts.configId || undefined,
})
for (const sb of pending) {
task.current_storyboard_id = sb.id
logTaskProgress('VideoPrompt', 'batch-shot', { episodeId, storyboardId: sb.id, index: task.completed + task.failed + 1, total: task.total })
try {
await agent.generate([{
role: 'user',
content: `请为分镜 #${sb.storyboardNumber}(ID:${sb.id})生成视频提示词(video_prompt)。视频模型:${videoLabel},请根据该模型的特性和时长限制生成。
请先调用 read_storyboard_context 获取该分镜的画面描述(含【镜头N】子镜头与台词/旁白)、氛围及时长,据此生成 video_prompt(第一行是信息头:列出该分镜出场的人物与场景,用 @角色名/@场景名 引用;之后按 3 秒分段换行、用 @角色名/@场景名/@道具名 引用参考素材;段落内允许多镜头切镜,段与段可以是不同景别/角度/对象,但不跨场景,切镜点对齐分镜 description 的【镜头N】结构),然后调用 update_storyboard 保存到分镜 ID:${sb.id}。update_storyboard 参数只传 storyboard_id 和 video_prompt 两个键,不要回传该分镜的其他任何字段,不要重新拆分整集。`,
}], { maxSteps: 8, requestContext })
// 以实际落库为准判定成败
const [fresh] = await db.select().from(schema.storyboards).where(eq(schema.storyboards.id, sb.id))
if ((fresh?.videoPrompt || '').trim()) task.completed++
else {
task.failed++
logTaskError('VideoPrompt', 'batch-shot', { storyboardId: sb.id, error: 'agent finished but video_prompt is empty' })
}
} catch (err: any) {
task.failed++
logTaskError('VideoPrompt', 'batch-shot', { storyboardId: sb.id, error: err?.message })
}
}
})()
.then(() => {
task.status = 'done'
task.finished_at = new Date().toISOString()
task.current_storyboard_id = undefined
logTaskSuccess('VideoPrompt', 'batch', { episodeId, total: task.total, completed: task.completed, failed: task.failed })
})
.catch((err: any) => {
task.status = 'error'
task.finished_at = new Date().toISOString()
task.error = err?.message || '批量生成失败'
logTaskError('VideoPrompt', 'batch', { episodeId, error: err?.message })
})
return { started: true, total: pending.length }
}
export function getVideoPromptBatchStatus(episodeId: number): VideoPromptBatchStatus | null {
return tasks.get(episodeId) || null
}