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
153 lines
5.1 KiB
TypeScript
153 lines
5.1 KiB
TypeScript
import { createLogger } from '@sim/logger'
|
|
import { getErrorMessage } from '@sim/utils/errors'
|
|
import { GenerateAudio } 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 { type AudioType, generateFalAudio } from '@/lib/media/falai-audio'
|
|
import {
|
|
fetchWorkspaceFileBuffer,
|
|
resolveWorkspaceFileReference,
|
|
} from '@/lib/uploads/contexts/workspace/workspace-file-manager'
|
|
|
|
const logger = createLogger('GenerateAudioTool')
|
|
|
|
const VALID_TYPES: AudioType[] = ['speech', 'music', 'sfx']
|
|
|
|
interface GenerateAudioArgs {
|
|
prompt: string
|
|
type?: string
|
|
model?: string
|
|
voice?: string
|
|
duration?: number
|
|
/** For music: explicit lyrics for a vocal track. */
|
|
lyrics?: string
|
|
/** For music: true = instrumental (default), false = vocal track. */
|
|
instrumental?: boolean
|
|
/** Optional reference voice sample (workspace audio file) for zero-shot voice cloning. */
|
|
inputs?: { files?: Array<{ path: string }> }
|
|
outputs?: {
|
|
files?: Array<{ path: string; mode?: 'create' | 'overwrite'; mimeType?: string }>
|
|
}
|
|
}
|
|
|
|
interface GenerateAudioResult {
|
|
success: boolean
|
|
message: string
|
|
fileId?: string
|
|
fileName?: string
|
|
vfsPath?: string
|
|
downloadUrl?: string
|
|
_serviceCost?: { service: string; cost: number }
|
|
}
|
|
|
|
function audioExtFromContentType(contentType: string): string {
|
|
if (contentType.includes('wav')) return 'wav'
|
|
if (contentType.includes('mp4') || contentType.includes('m4a')) return 'm4a'
|
|
if (contentType.includes('ogg')) return 'ogg'
|
|
if (contentType.includes('flac')) return 'flac'
|
|
if (contentType.includes('aac')) return 'aac'
|
|
if (contentType.includes('opus')) return 'opus'
|
|
return 'mp3'
|
|
}
|
|
|
|
export const generateAudioServerTool: BaseServerTool<GenerateAudioArgs, GenerateAudioResult> = {
|
|
name: GenerateAudio.id,
|
|
|
|
async execute(
|
|
params: GenerateAudioArgs,
|
|
context?: ServerToolContext
|
|
): Promise<GenerateAudioResult> {
|
|
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' }
|
|
}
|
|
|
|
const type = (params.type || 'speech') as AudioType
|
|
if (!VALID_TYPES.includes(type)) {
|
|
return {
|
|
success: false,
|
|
message: `Invalid type "${params.type}". Must be one of: ${VALID_TYPES.join(', ')}`,
|
|
}
|
|
}
|
|
|
|
// Voice cloning: a reference sample clones that voice into the generated speech.
|
|
let voiceSampleDataUri: string | undefined
|
|
const samplePath = params.inputs?.files?.[0]?.path
|
|
if (samplePath) {
|
|
const sample = await resolveWorkspaceFileReference(workspaceId, samplePath)
|
|
if (!sample) {
|
|
return { success: false, message: `Voice sample not found: ${samplePath}` }
|
|
}
|
|
const sampleBuffer = await fetchWorkspaceFileBuffer(sample)
|
|
const sampleMime = sample.type || 'audio/mpeg'
|
|
voiceSampleDataUri = `data:${sampleMime};base64,${sampleBuffer.toString('base64')}`
|
|
}
|
|
|
|
try {
|
|
logger.info('Generating audio', {
|
|
type,
|
|
model: params.model,
|
|
promptLength: params.prompt.length,
|
|
voiceClone: Boolean(voiceSampleDataUri),
|
|
})
|
|
|
|
const result = await generateFalAudio({
|
|
prompt: params.prompt,
|
|
type,
|
|
model: params.model,
|
|
voice: params.voice,
|
|
duration: params.duration,
|
|
lyrics: params.lyrics,
|
|
instrumental: params.instrumental,
|
|
voiceSampleDataUri,
|
|
})
|
|
|
|
const outputFile = params.outputs?.files?.[0]
|
|
const ext = audioExtFromContentType(result.contentType)
|
|
const outputPath = outputFile?.path || `files/generated-audio.${ext}`
|
|
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 audio saved', {
|
|
fileId: written.id,
|
|
vfsPath: written.vfsPath,
|
|
size: result.buffer.length,
|
|
type,
|
|
model: result.model,
|
|
})
|
|
|
|
return {
|
|
success: true,
|
|
message: `${type === 'speech' ? 'Speech' : type === 'music' ? 'Music' : 'Sound effect'} 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_audio', cost: result.cost.costDollars },
|
|
}
|
|
} catch (error) {
|
|
const msg = getErrorMessage(error, 'Unknown error')
|
|
logger.error('Audio generation failed', { error: msg })
|
|
return { success: false, message: `Failed to generate audio: ${msg}` }
|
|
}
|
|
},
|
|
}
|