From d737b1f7ea3c39aac0df2d05ea29d6ead4b94163 Mon Sep 17 00:00:00 2001 From: Paul Bailey Date: Tue, 9 Dec 2025 11:47:30 +0000 Subject: [PATCH] feat: implement script generation API client function - Add ScriptSegment, ScriptRequest, and ScriptResponse TypeScript interfaces - Implement generateScript() function to call /api/tts/script endpoint - Update GenerateRequest interface to support optional segments array - Add comprehensive error handling for script generation - Validates Requirements 15.1, 15.2 --- .kiro/specs/text-to-audiobook/tasks.md | 2 +- frontend/src/api/tts.ts | 48 +++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/.kiro/specs/text-to-audiobook/tasks.md b/.kiro/specs/text-to-audiobook/tasks.md index 14d8936..b1be0df 100644 --- a/.kiro/specs/text-to-audiobook/tasks.md +++ b/.kiro/specs/text-to-audiobook/tasks.md @@ -389,7 +389,7 @@ - Test segment speaker assignments are respected - _Requirements: 15.7_ -- [ ] 20. Implement script generation API client function +- [x] 20. Implement script generation API client function - Add generateScript() function to src/api/tts.ts - Define ScriptSegment, ScriptRequest, and ScriptResponse TypeScript interfaces - Implement error handling for script generation diff --git a/frontend/src/api/tts.ts b/frontend/src/api/tts.ts index 4562741..89199ec 100644 --- a/frontend/src/api/tts.ts +++ b/frontend/src/api/tts.ts @@ -29,11 +29,36 @@ export interface RolesResponse { roles: Role[]; } +/** + * Represents a script segment with speaker assignment + */ +export interface ScriptSegment { + id: string; + speaker: string; + text: string; +} + +/** + * Request payload for script generation + */ +export interface ScriptRequest { + text: string; + roles: string[]; +} + +/** + * Response from the script generation endpoint + */ +export interface ScriptResponse { + segments: ScriptSegment[]; +} + /** * Request payload for audiobook generation */ export interface GenerateRequest { - text: string; + segments?: ScriptSegment[]; + text?: string; lang: string; roles: { id: string; voice: string }[]; speed?: number; @@ -188,6 +213,27 @@ export async function detectRoles(text: string): Promise { return await response.json(); } +/** + * Generate a script preview with AI-segmented text and speaker assignments + * @param request - Script request with text and detected roles + * @returns Promise resolving to script segments + * @throws ApiError if the request fails + */ +export async function generateScript(request: ScriptRequest): Promise { + const baseUrl = getApiBaseUrl(); + const url = `${baseUrl}/api/tts/script`; + + const response = await fetchWithErrorHandling(url, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(request), + }); + + return await response.json(); +} + /** * Generate an audiobook from text with assigned voices * @param payload - Generation request with text, language, roles, and speed