🎬 火宝短剧 - 基于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
/**
* 剧本改写 Agent 工具
* 模块级单例 — episodeId 通过 RequestContext 按请求注入
*/
import { createTool } from '@mastra/core/tools'
import { z } from 'zod'
import { db, schema } from '../../db/index.js'
import { eq } from 'drizzle-orm'
import { now } from '../../utils/response.js'
import { getEpisodeId } from '../context.js'
const readEpisodeScript = createTool({
id: 'read_episode_script',
description: 'Read the script content of the current episode.',
inputSchema: z.object({}),
execute: async (_input, context) => {
const episodeId = getEpisodeId(context?.requestContext)
if (!episodeId) return { error: 'Missing episodeId in request context' }
const [ep] = await db.select().from(schema.episodes)
.where(eq(schema.episodes.id, episodeId))
if (!ep) return { error: `Episode not found (id=${episodeId})` }
const content = ep.content || ep.scriptContent
if (!content) return { error: `Episode has no content (id=${episodeId})` }
return { content, word_count: content.length, episode_id: episodeId }
},
})
const rewriteToScreenplay = createTool({
id: 'rewrite_to_screenplay',
description: 'Read the original content for AI rewriting. Returns the source text with formatting instructions.',
inputSchema: z.object({
instructions: z.string().optional().describe('Additional rewrite instructions'),
}),
execute: async ({ instructions }, context) => {
const episodeId = getEpisodeId(context?.requestContext)
if (!episodeId) return { error: 'Missing episodeId in request context' }
const [ep] = await db.select().from(schema.episodes)
.where(eq(schema.episodes.id, episodeId))
if (!ep) return { error: `Episode not found` }
const source = ep.content || ep.scriptContent
if (!source) return { error: `Episode has no content to rewrite` }
return {
source_content: source,
instruction: `请将以下内容改写为格式化剧本。
格式规范:
- 场景头:## S编号 | 内景/外景 · 地点 | 时间段
- 动作描写:自然段落,不包含镜头语言
- 对白:角色名:(状态/表情)台词内容
- 每个场景 30-60 秒内容
${instructions || ''}
【原始内容】
${source}`,
}
},
})
const saveScript = createTool({
id: 'save_script',
description: 'Save the rewritten screenplay content to the current episode.',
inputSchema: z.object({
content: z.string().describe('The formatted screenplay content to save'),
}),
execute: async ({ content }, context) => {
const episodeId = getEpisodeId(context?.requestContext)
if (!episodeId) return { error: 'Missing episodeId in request context' }
await db.update(schema.episodes)
.set({ scriptContent: content, updatedAt: now() })
.where(eq(schema.episodes.id, episodeId))
return { message: `Script saved`, word_count: content.length }
},
})
export const scriptTools = { readEpisodeScript, rewriteToScreenplay, saveScript }