Skip to content

Commit 0742afb

Browse files
committed
ADd ability to chat to improve the logo image
1 parent b994c84 commit 0742afb

File tree

6 files changed

+120
-29
lines changed

6 files changed

+120
-29
lines changed

BACKLOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Backlog
22

33
---
4-
[x] Convert generated logo to multiple sizes and formats
4+
## Feature name
5+
Verify if there are any issues.
56

7+
### Description
8+
Verify if there are any issues and if you are using the latest way to call the api. I want to generate logos via chat in my CLI.
69
---

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"@std/fs": "jsr:@std/fs@^1.0.21",
1212
"@std/path": "jsr:@std/path@^1.1.4",
1313
"@std/fmt/colors": "jsr:@std/fmt@^1.0.8/colors",
14-
"@google/genai": "npm:@google/genai@^1.35.0"
14+
"@google/genai": "npm:@google/genai@^1.36.0"
1515
},
1616
"publish": {
1717
"exclude": [

deno.lock

Lines changed: 69 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ai.ts

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { join } from "@std/path";
33
import { ensureDir } from "@std/fs";
44
import { GoogleGenAI } from "@google/genai";
55
import { processLogo } from "./image.ts";
6+
import { promptInput } from "./prompt.ts";
67

78
/**
89
* Generates a logo using Google's Gemini AI (Imagen model)
@@ -12,41 +13,60 @@ export async function generateLogo(
1213
apiKey: string,
1314
destDir: string,
1415
): Promise<string | null> {
15-
const spinner = startSpinner("🎨 Generating logo with AI...");
16+
let spinner = startSpinner("🎨 Generating logo with AI...");
1617

1718
try {
1819
const ai = new GoogleGenAI({ apiKey });
1920

20-
const response = await ai.models.generateImages({
21+
// Initial message to start the chat
22+
const chat = ai.chats.create({
2123
model: "gemini-3-pro-image-preview",
22-
prompt: prompt,
2324
config: {
24-
numberOfImages: 1,
25-
aspectRatio: "1:1",
26-
outputMimeType: "image/png",
25+
responseModalities: ["TEXT", "IMAGE"],
2726
},
2827
});
2928

30-
const base64Image = response.generatedImages?.[0]?.image?.imageBytes;
29+
let currentPrompt = prompt;
3130

32-
if (!base64Image) {
33-
spinner.stop(false);
34-
console.log(" ⚠️ Failed to generate logo: No image data received from AI.");
35-
return null;
36-
}
31+
while (true) {
32+
const response = await chat.sendMessage({ message: currentPrompt });
33+
34+
let base64Image: string | undefined;
35+
for (const part of response.candidates?.[0]?.content?.parts || []) {
36+
if (part.inlineData && part.inlineData.mimeType === "image/png") {
37+
base64Image = part.inlineData.data;
38+
}
39+
}
40+
41+
if (!base64Image) {
42+
spinner.stop(false);
43+
console.log(" ⚠️ Failed to generate logo: No image data received from AI.");
44+
return null;
45+
}
3746

38-
const imageBuffer = Uint8Array.from(atob(base64Image), (c) => c.charCodeAt(0));
39-
const publicDir = join(destDir, "public");
40-
await ensureDir(publicDir);
41-
const logoPath = join(publicDir, "logo.png");
42-
await Deno.writeFile(logoPath, imageBuffer);
47+
const imageBuffer = Uint8Array.from(atob(base64Image), (c) => c.charCodeAt(0));
48+
const publicDir = join(destDir, "public");
49+
await ensureDir(publicDir);
50+
const logoPath = join(publicDir, "logo.png");
51+
await Deno.writeFile(logoPath, imageBuffer);
4352

44-
spinner.stop(true);
53+
spinner.stop(true);
4554

46-
// Process the logo into multiple sizes and formats
47-
await processLogo(logoPath, destDir);
55+
// Process the logo into multiple sizes and formats
56+
await processLogo(logoPath, destDir);
4857

49-
return logoPath;
58+
const improvement = promptInput(
59+
"✨ Would you like to improve this logo? (enter your instructions, or leave empty to finish)",
60+
"",
61+
);
62+
63+
if (!improvement) {
64+
return logoPath;
65+
}
66+
67+
currentPrompt = improvement;
68+
spinner = startSpinner("🎨 Improving logo with AI...");
69+
}
5070
} catch (err) {
5171
spinner.stop(false);
5272
console.log(` ⚠️ An unexpected error occurred: ${err instanceof Error ? err.message : String(err)}`);

lib/ui.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export function startSpinner(text: string) {
1111
update(newText: string) {
1212
currentText = newText;
1313
},
14+
start() {
15+
// No-op for now, as update already changes the text and the timer is running
16+
},
1417
stop(success: boolean) {
1518
clearInterval(timer);
1619
const icon = success ? "✅" : "❌";

new-logo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export async function newLogoCommand() {
4848
const logoPath = await generateLogo(logoPrompt, geminiApiKey, destDir);
4949

5050
if (logoPath) {
51-
console.log(blue(`\n ✅ Logo generated and processed successfully!`));
52-
console.log(` 📂 Files created in: ${bold(destDir + "/public")}`);
51+
console.log(blue(`\n ✅ All done!`));
52+
console.log(` 📂 Files updated in: ${bold(destDir + "/public")}`);
5353
} else {
5454
console.log(red(`\n ❌ Failed to generate logo.`));
5555
}

0 commit comments

Comments
 (0)