@@ -215,6 +215,48 @@ export function useImageGen({
215
215
( existingDoc as unknown as ImageDocument ) . prompt ||
216
216
'' ;
217
217
218
+ // Check if we have a document with only an original file but no output files
219
+ // This happens when someone uploads an image but it hasn't been processed yet
220
+ const { versions } = getVersionsFromDocument ( existingDoc ) ;
221
+ const hasOutputFiles =
222
+ versions . length > 0 && versions . some ( ( v ) => existingDoc . _files ?. [ v . id ] ) ;
223
+ const hasOnlyOriginalFile = ! hasOutputFiles && existingDoc . _files ?. original ;
224
+
225
+ // If we have a prompt and only an original file, generate an image
226
+ if ( hasOnlyOriginalFile && currentPromptText && ! generationId ) {
227
+ console . log (
228
+ `Document ${ _id } has only original file and prompt, generating edited image`
229
+ ) ;
230
+ setLoading ( true ) ;
231
+
232
+ // Create options that include the document for access to the original file
233
+ const editOptions = {
234
+ ...options ,
235
+ document : existingDoc ,
236
+ _regenerationId : Date . now ( ) , // Ensure unique generation
237
+ } ;
238
+
239
+ // Generate a new image using the document's prompt and original file
240
+ data = await callImageGeneration ( currentPromptText , editOptions ) ;
241
+
242
+ if ( data ?. data ?. [ 0 ] ?. b64_json ) {
243
+ // Handle the generated image
244
+ const filename = generateSafeFilename ( currentPromptText ) ;
245
+ const newImageFile = base64ToFile ( data . data [ 0 ] . b64_json , filename ) ;
246
+
247
+ // Add as a new version to the document
248
+ const updatedDoc = addNewVersion ( existingDoc , newImageFile , currentPromptText ) ;
249
+ await db . put ( updatedDoc ) ;
250
+
251
+ // Update component state
252
+ const refreshedDoc = await db . get ( _id ) ;
253
+ setDocument ( refreshedDoc as unknown as ImageDocument ) ;
254
+ setImageData ( data . data [ 0 ] . b64_json ) ;
255
+ setProgress ( 100 ) ;
256
+ return ;
257
+ }
258
+ }
259
+
218
260
// If generationId is provided, we're creating a new version
219
261
// Only attempt if we have a document with a prompt
220
262
if ( generationId && currentPromptText ) {
0 commit comments