d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
128 lines
4.1 KiB
TypeScript
128 lines
4.1 KiB
TypeScript
import { createLogger } from '@sim/logger'
|
|
import { getErrorMessage } from '@sim/utils/errors'
|
|
import { GenerateVideo } from '@/lib/copilot/generated/tool-catalog-v1'
|
|
import {
|
|
assertServerToolNotAborted,
|
|
type BaseServerTool,
|
|
type ServerToolContext,
|
|
} from '@/lib/copilot/tools/server/base-tool'
|
|
import { writeWorkspaceFileByPath } from '@/lib/copilot/vfs/resource-writer'
|
|
import { generateFalVideo } from '@/lib/media/falai-video'
|
|
import {
|
|
fetchWorkspaceFileBuffer,
|
|
resolveWorkspaceFileReference,
|
|
} from '@/lib/uploads/contexts/workspace/workspace-file-manager'
|
|
|
|
const logger = createLogger('GenerateVideoTool')
|
|
|
|
interface GenerateVideoArgs {
|
|
prompt: string
|
|
model?: string
|
|
aspectRatio?: string
|
|
resolution?: string
|
|
duration?: number
|
|
generateAudio?: boolean
|
|
negativePrompt?: string
|
|
promptOptimizer?: boolean
|
|
inputs?: { files?: Array<{ path: string }> }
|
|
outputs?: {
|
|
files?: Array<{ path: string; mode?: 'create' | 'overwrite'; mimeType?: string }>
|
|
}
|
|
}
|
|
|
|
interface GenerateVideoResult {
|
|
success: boolean
|
|
message: string
|
|
fileId?: string
|
|
fileName?: string
|
|
vfsPath?: string
|
|
downloadUrl?: string
|
|
_serviceCost?: { service: string; cost: number }
|
|
}
|
|
|
|
export const generateVideoServerTool: BaseServerTool<GenerateVideoArgs, GenerateVideoResult> = {
|
|
name: GenerateVideo.id,
|
|
|
|
async execute(
|
|
params: GenerateVideoArgs,
|
|
context?: ServerToolContext
|
|
): Promise<GenerateVideoResult> {
|
|
if (!context?.userId) {
|
|
throw new Error('Authentication required')
|
|
}
|
|
const workspaceId = context.workspaceId
|
|
if (!workspaceId) {
|
|
return { success: false, message: 'Workspace ID is required' }
|
|
}
|
|
if (!params.prompt) {
|
|
return { success: false, message: 'prompt is required' }
|
|
}
|
|
|
|
try {
|
|
let imageDataUri: string | undefined
|
|
const refPath = params.inputs?.files?.[0]?.path
|
|
if (refPath) {
|
|
const fileRecord = await resolveWorkspaceFileReference(workspaceId, refPath)
|
|
if (!fileRecord) {
|
|
return { success: false, message: `Reference image not found: ${refPath}` }
|
|
}
|
|
const buffer = await fetchWorkspaceFileBuffer(fileRecord)
|
|
const mime = fileRecord.type || 'image/png'
|
|
imageDataUri = `data:${mime};base64,${buffer.toString('base64')}`
|
|
}
|
|
|
|
logger.info('Generating video', {
|
|
model: params.model || 'veo-3.1-fast',
|
|
promptLength: params.prompt.length,
|
|
imageToVideo: Boolean(imageDataUri),
|
|
})
|
|
|
|
const result = await generateFalVideo({
|
|
prompt: params.prompt,
|
|
model: params.model,
|
|
aspectRatio: params.aspectRatio,
|
|
resolution: params.resolution,
|
|
duration: params.duration,
|
|
generateAudio: params.generateAudio,
|
|
negativePrompt: params.negativePrompt,
|
|
promptOptimizer: params.promptOptimizer,
|
|
imageDataUri,
|
|
})
|
|
|
|
const outputFile = params.outputs?.files?.[0]
|
|
const outputPath = outputFile?.path || 'files/generated-video.mp4'
|
|
const mode = outputFile?.mode ?? 'create'
|
|
|
|
assertServerToolNotAborted(context)
|
|
const written = await writeWorkspaceFileByPath({
|
|
workspaceId,
|
|
userId: context.userId,
|
|
target: { path: outputPath, mode, mimeType: outputFile?.mimeType },
|
|
buffer: result.buffer,
|
|
inferredMimeType: result.contentType,
|
|
})
|
|
|
|
logger.info('Generated video saved', {
|
|
fileId: written.id,
|
|
vfsPath: written.vfsPath,
|
|
size: result.buffer.length,
|
|
model: result.model,
|
|
})
|
|
|
|
return {
|
|
success: true,
|
|
message: `Video generated and ${written.mode === 'overwrite' ? 'updated' : 'saved'} at "${written.vfsPath}" (${result.buffer.length} bytes, model ${result.model})`,
|
|
fileId: written.id,
|
|
fileName: written.name,
|
|
vfsPath: written.vfsPath,
|
|
downloadUrl: written.downloadUrl,
|
|
_serviceCost: { service: 'falai_video', cost: result.cost.costDollars },
|
|
}
|
|
} catch (error) {
|
|
const msg = getErrorMessage(error, 'Unknown error')
|
|
logger.error('Video generation failed', { error: msg })
|
|
return { success: false, message: `Failed to generate video: ${msg}` }
|
|
}
|
|
},
|
|
}
|