Skip to content

Commit 194d0fe

Browse files
committed
feat: auto-generate edited image when document has original file and prompt
1 parent 0956f1f commit 194d0fe

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "use-vibes",
3-
"version": "0.5.2",
3+
"version": "0.5.3",
44
"type": "module",
55
"description": "Transform any DOM element into an AI-powered micro-app",
66
"main": "dist/index.js",

src/hooks/image-gen/use-image-gen.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,48 @@ export function useImageGen({
215215
(existingDoc as unknown as ImageDocument).prompt ||
216216
'';
217217

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+
218260
// If generationId is provided, we're creating a new version
219261
// Only attempt if we have a document with a prompt
220262
if (generationId && currentPromptText) {

0 commit comments

Comments
 (0)