( apiKey: string, endpointId: string )
| 119 | } |
| 120 | |
| 121 | async function estimateFalAICallCost( |
| 122 | apiKey: string, |
| 123 | endpointId: string |
| 124 | ): Promise<{ costDollars?: number; error?: string }> { |
| 125 | let response: Response |
| 126 | try { |
| 127 | response = await fetch('https://api.fal.ai/v1/models/pricing/estimate', { |
| 128 | method: 'POST', |
| 129 | headers: { |
| 130 | Authorization: `Key ${apiKey}`, |
| 131 | 'Content-Type': 'application/json', |
| 132 | }, |
| 133 | body: JSON.stringify({ |
| 134 | estimate_type: 'historical_api_price', |
| 135 | endpoints: { |
| 136 | [endpointId]: { |
| 137 | call_quantity: 1, |
| 138 | }, |
| 139 | }, |
| 140 | }), |
| 141 | }) |
| 142 | } catch (error) { |
| 143 | return { error: getErrorMessage(error, 'Unknown error') } |
| 144 | } |
| 145 | |
| 146 | if (!response.ok) { |
| 147 | const error = await response.text().catch(() => '') |
| 148 | return { error: `Fal.ai pricing estimate failed: ${response.status} ${error}` } |
| 149 | } |
| 150 | |
| 151 | const data = (await response.json()) as unknown |
| 152 | const totalCost = isRecordLike(data) ? getNumber(data.total_cost) : undefined |
| 153 | if (totalCost === undefined) { |
| 154 | return { error: 'Fal.ai pricing estimate missing total_cost' } |
| 155 | } |
| 156 | |
| 157 | return { costDollars: totalCost } |
| 158 | } |
| 159 | |
| 160 | export async function getFalAICostMetadata({ |
| 161 | apiKey, |
no test coverage detected