( pages: Page[], taskId: string | null, fullOutline: string, onProgress: (event: ProgressEvent) => void, onComplete: (event: ProgressEvent) => void, onError: (event: ProgressEvent) => void, onFinish: (event: FinishEvent) => void, onStreamError: (error: unknown) => void, userImages?: File[], userTopic?: string, recordId?: string | null, force: boolean = false )
| 76 | } |
| 77 | |
| 78 | export async function generateImagesPost( |
| 79 | pages: Page[], |
| 80 | taskId: string | null, |
| 81 | fullOutline: string, |
| 82 | onProgress: (event: ProgressEvent) => void, |
| 83 | onComplete: (event: ProgressEvent) => void, |
| 84 | onError: (event: ProgressEvent) => void, |
| 85 | onFinish: (event: FinishEvent) => void, |
| 86 | onStreamError: (error: unknown) => void, |
| 87 | userImages?: File[], |
| 88 | userTopic?: string, |
| 89 | recordId?: string | null, |
| 90 | force: boolean = false |
| 91 | ) { |
| 92 | try { |
| 93 | const userImagesBase64 = userImages && userImages.length > 0 |
| 94 | ? await Promise.all(userImages.map(readFileAsDataUrl)) |
| 95 | : [] |
| 96 | |
| 97 | const response = await fetch(`${API_BASE_URL}/generate`, { |
| 98 | method: 'POST', |
| 99 | headers: { |
| 100 | 'Content-Type': 'application/json', |
| 101 | }, |
| 102 | body: JSON.stringify({ |
| 103 | pages, |
| 104 | task_id: taskId, |
| 105 | full_outline: fullOutline, |
| 106 | user_images: userImagesBase64.length > 0 ? userImagesBase64 : undefined, |
| 107 | user_topic: userTopic || '', |
| 108 | record_id: recordId || undefined, |
| 109 | force |
| 110 | }) |
| 111 | }) |
| 112 | |
| 113 | if (!response.ok) { |
| 114 | throw await readErrorResponse(response, `请求失败:HTTP ${response.status}`) |
| 115 | } |
| 116 | |
| 117 | await readSseResponse(response, { |
| 118 | progress: onProgress, |
| 119 | complete: onComplete, |
| 120 | error: onError, |
| 121 | finish: onFinish |
| 122 | }) |
| 123 | } catch (error) { |
| 124 | onStreamError(error) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | function readFileAsDataUrl(file: File): Promise<string> { |
| 129 | return new Promise<string>((resolve, reject) => { |
no test coverage detected