ai-cut/app/composables/useAIStoryboard.ts

43 lines
1.2 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.

/**
* AI 分鏡表 Composable
* 統一處理錯誤UI 只呼叫此 composable
*/
import { useAIProvider } from './useAIProvider'
import { buildStoryboardPrompt, type StoryboardInput } from '~/utils/ai/prompts'
import { getTextModel } from '~/utils/storage'
import { DEFAULT_TEXT_MODEL as DEFAULT_MODEL } from '~/utils/clients/gemini-models'
export function useAIStoryboard() {
const isLoading = ref(false)
const error = ref<string | null>(null)
async function generate(input: StoryboardInput): Promise<string> {
isLoading.value = true
error.value = null
try {
// 根據選擇的模型自動選擇對應的 provider
const modelName = getTextModel() || DEFAULT_MODEL
const provider = useAIProvider(modelName)
const prompt = buildStoryboardPrompt(input)
const result = await provider.generateStoryboard(prompt)
return result
} catch (err) {
const errorMessage = err instanceof Error ? err.message : '未知錯誤'
error.value = errorMessage
throw err
} finally {
isLoading.value = false
}
}
return {
generate,
isLoading: readonly(isLoading),
error: readonly(error)
}
}