|
1 | 1 | import { GoogleGenAI, Type } from "@google/genai"; |
2 | 2 | import { type NextRequest, NextResponse } from "next/server"; |
3 | 3 | import { getGoogleAiApiKey } from "@/lib/api-keys"; |
4 | | -import { callGeminiWithRetry } from "@/lib/gemini-helper"; |
| 4 | +import { type ApiResponse, callGeminiWithRetry } from "@/lib/gemini-helper"; |
5 | 5 | import { parseGeminiJSON } from "@/lib/json-parser"; |
6 | 6 | import { |
7 | 7 | logApiRequest, |
@@ -29,7 +29,7 @@ interface AnalysisData { |
29 | 29 | } |
30 | 30 |
|
31 | 31 | const genAI = new GoogleGenAI({ apiKey: getGoogleAiApiKey(true) }); |
32 | | -const model = "gemini-2.5-flash"; |
| 32 | +// Model will be determined dynamically by callGeminiWithRetry |
33 | 33 |
|
34 | 34 | export async function POST(request: NextRequest) { |
35 | 35 | const startTime = Date.now(); |
@@ -109,81 +109,107 @@ Please provide: |
109 | 109 |
|
110 | 110 | let text: string; |
111 | 111 | try { |
112 | | - text = await callGeminiWithRetry( |
113 | | - async () => { |
114 | | - const result = await genAI.models.generateContent({ |
115 | | - model: model, |
116 | | - contents: prompt, |
117 | | - config: { |
118 | | - responseMimeType: "application/json", |
119 | | - responseSchema: { |
| 112 | + // Prepare Bedrock fallback parameters |
| 113 | + |
| 114 | + const response = await callGeminiWithRetry( |
| 115 | + genAI, |
| 116 | + prompt, |
| 117 | + { |
| 118 | + responseMimeType: "application/json", |
| 119 | + responseSchema: { |
| 120 | + type: Type.OBJECT, |
| 121 | + properties: { |
| 122 | + title: { |
| 123 | + type: Type.STRING, |
| 124 | + }, |
| 125 | + characters: { |
| 126 | + type: Type.ARRAY, |
| 127 | + items: { |
| 128 | + type: Type.OBJECT, |
| 129 | + properties: { |
| 130 | + name: { |
| 131 | + type: Type.STRING, |
| 132 | + }, |
| 133 | + physicalDescription: { |
| 134 | + type: Type.STRING, |
| 135 | + }, |
| 136 | + personality: { |
| 137 | + type: Type.STRING, |
| 138 | + }, |
| 139 | + role: { |
| 140 | + type: Type.STRING, |
| 141 | + }, |
| 142 | + }, |
| 143 | + propertyOrdering: [ |
| 144 | + "name", |
| 145 | + "physicalDescription", |
| 146 | + "personality", |
| 147 | + "role", |
| 148 | + ], |
| 149 | + }, |
| 150 | + }, |
| 151 | + setting: { |
120 | 152 | type: Type.OBJECT, |
121 | 153 | properties: { |
122 | | - title: { |
| 154 | + timePeriod: { |
123 | 155 | type: Type.STRING, |
124 | 156 | }, |
125 | | - characters: { |
126 | | - type: Type.ARRAY, |
127 | | - items: { |
128 | | - type: Type.OBJECT, |
129 | | - properties: { |
130 | | - name: { |
131 | | - type: Type.STRING, |
132 | | - }, |
133 | | - physicalDescription: { |
134 | | - type: Type.STRING, |
135 | | - }, |
136 | | - personality: { |
137 | | - type: Type.STRING, |
138 | | - }, |
139 | | - role: { |
140 | | - type: Type.STRING, |
141 | | - }, |
142 | | - }, |
143 | | - propertyOrdering: [ |
144 | | - "name", |
145 | | - "physicalDescription", |
146 | | - "personality", |
147 | | - "role", |
148 | | - ], |
149 | | - }, |
| 157 | + location: { |
| 158 | + type: Type.STRING, |
150 | 159 | }, |
151 | | - setting: { |
152 | | - type: Type.OBJECT, |
153 | | - properties: { |
154 | | - timePeriod: { |
155 | | - type: Type.STRING, |
156 | | - }, |
157 | | - location: { |
158 | | - type: Type.STRING, |
159 | | - }, |
160 | | - mood: { |
161 | | - type: Type.STRING, |
162 | | - }, |
163 | | - }, |
164 | | - propertyOrdering: ["timePeriod", "location", "mood"], |
| 160 | + mood: { |
| 161 | + type: Type.STRING, |
165 | 162 | }, |
166 | 163 | }, |
167 | | - propertyOrdering: ["title", "characters", "setting"], |
| 164 | + propertyOrdering: ["timePeriod", "location", "mood"], |
168 | 165 | }, |
169 | 166 | }, |
170 | | - }); |
171 | | - |
| 167 | + propertyOrdering: ["title", "characters", "setting"], |
| 168 | + }, |
| 169 | + }, |
| 170 | + (result) => { |
172 | 171 | storyAnalysisLogger.debug( |
173 | 172 | { |
174 | | - response_length: result.text?.length || 0, |
| 173 | + response_length: (result as { text?: string })?.text?.length || 0, |
175 | 174 | }, |
176 | 175 | "Received response from Gemini API", |
177 | 176 | ); |
178 | | - |
179 | | - return result.text || ""; |
| 177 | + return (result as { text?: string })?.text || ""; |
180 | 178 | }, |
181 | 179 | storyAnalysisLogger, |
| 180 | + "text", |
182 | 181 | { |
183 | | - model: model, |
184 | 182 | prompt_length: prompt.length, |
185 | 183 | }, |
| 184 | + // Bedrock fallback parameters |
| 185 | + { |
| 186 | + prompt: prompt, |
| 187 | + maxTokens: 4096, |
| 188 | + temperature: 0.7, |
| 189 | + }, |
186 | 190 | ); |
| 191 | + |
| 192 | + // Handle response based on source |
| 193 | + if (typeof response === "object" && "source" in response) { |
| 194 | + // Response from Bedrock or Gemini with source info |
| 195 | + const apiResponse = response as ApiResponse<string>; |
| 196 | + storyAnalysisLogger.info( |
| 197 | + { |
| 198 | + source: apiResponse.source, |
| 199 | + }, |
| 200 | + `Story analysis completed using ${apiResponse.source}`, |
| 201 | + ); |
| 202 | + text = apiResponse.result; |
| 203 | + } else { |
| 204 | + // Direct response from Gemini (backward compatibility) |
| 205 | + text = response as string; |
| 206 | + storyAnalysisLogger.info( |
| 207 | + { |
| 208 | + source: "gemini", |
| 209 | + }, |
| 210 | + "Story analysis completed using gemini", |
| 211 | + ); |
| 212 | + } |
187 | 213 | } catch (error) { |
188 | 214 | logError(storyAnalysisLogger, error, "story analysis"); |
189 | 215 | logApiResponse( |
|
0 commit comments