-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlib.ts
More file actions
338 lines (285 loc) · 10.9 KB
/
lib.ts
File metadata and controls
338 lines (285 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import { GoogleGenAI } from "@google/genai";
import { exec } from "child_process";
import { promisify } from "util";
import * as fs from "fs";
import * as path from "path";
const execAsync = promisify(exec);
export const OUTPUT_DIR = "nanobanana-output";
export const DEFAULT_MODEL = "gemini-3-pro-image-preview";
// ============ Utilities ============
export function getApiKey(): string {
const key =
process.env.NANOBANANA_GEMINI_API_KEY ||
process.env.GEMINI_API_KEY ||
process.env.GOOGLE_API_KEY;
if (!key) {
throw new Error("No API key found. Set GEMINI_API_KEY environment variable.");
}
return key;
}
export function ensureOutputDir(baseDir?: string): string {
const outputPath = path.join(baseDir || process.cwd(), OUTPUT_DIR);
if (!fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath, { recursive: true });
}
return outputPath;
}
export function findFile(filename: string): string | null {
const paths = [
process.cwd(),
path.join(process.cwd(), OUTPUT_DIR),
path.join(process.env.HOME || "~", "Downloads"),
path.join(process.env.HOME || "~", "Desktop"),
];
if (path.isAbsolute(filename) && fs.existsSync(filename)) return filename;
for (const p of paths) {
const full = path.join(p, filename);
if (fs.existsSync(full)) return full;
}
return null;
}
export function genFilename(prompt: string, idx = 0, outputDir?: string): string {
let base = prompt.toLowerCase().replace(/[^a-z0-9\s]/g, "").replace(/\s+/g, "_").slice(0, 32) || "image";
const out = outputDir || ensureOutputDir();
let name = `${base}.png`;
let c = idx > 0 ? idx : 1;
while (fs.existsSync(path.join(out, name))) {
name = `${base}_${c++}.png`;
}
return name;
}
export async function saveImage(b64: string, filename: string, outputDir?: string): Promise<string> {
const out = outputDir || ensureOutputDir();
const full = path.join(out, filename);
await fs.promises.writeFile(full, Buffer.from(b64, "base64"));
return full;
}
export async function openFile(f: string) {
try {
const cmd = process.platform === "darwin" ? `open "${f}"` : process.platform === "win32" ? `start "" "${f}"` : `xdg-open "${f}"`;
await execAsync(cmd);
} catch {}
}
export function isBase64(d: string): boolean {
return !!d && d.length > 1000 && /^[A-Za-z0-9+/]*={0,2}$/.test(d);
}
export function parseArgs(args: string[]): { pos: string[]; flags: Record<string, string> } {
const pos: string[] = [];
const flags: Record<string, string> = {};
for (const a of args) {
if (a.startsWith("--")) {
const [k, v] = a.slice(2).split("=");
flags[k] = v || "true";
} else if (a.startsWith("-") && a.length === 2) {
flags[a[1]] = "true";
} else {
pos.push(a);
}
}
return { pos, flags };
}
// ============ Core Functions ============
export interface GenerateOptions {
count?: number;
styles?: string[];
variations?: string[];
preview?: boolean;
outputDir?: string;
}
export async function generate(prompt: string, opts: GenerateOptions = {}): Promise<string[]> {
const ai = new GoogleGenAI({ apiKey: getApiKey() });
const model = process.env.NANOBANANA_MODEL || DEFAULT_MODEL;
const files: string[] = [];
const outputDir = opts.outputDir || ensureOutputDir();
let prompts = [prompt];
if (opts.styles?.length) prompts = opts.styles.map(s => `${prompt}, ${s} style`);
if (opts.variations?.length) {
const base = prompts;
prompts = [];
for (const b of base) {
for (const v of opts.variations) {
if (v === "lighting") { prompts.push(`${b}, dramatic lighting`); prompts.push(`${b}, soft lighting`); }
else if (v === "mood") { prompts.push(`${b}, cheerful mood`); prompts.push(`${b}, dramatic mood`); }
else prompts.push(`${b}, ${v}`);
}
}
}
if (opts.count && opts.count > 1 && prompts.length === 1) prompts = Array(opts.count).fill(prompt);
if (opts.count && prompts.length > opts.count) prompts = prompts.slice(0, opts.count);
console.log(`Generating ${prompts.length} image(s)...`);
for (let i = 0; i < prompts.length; i++) {
try {
const res = await ai.models.generateContent({ model, contents: [{ role: "user", parts: [{ text: prompts[i] }] }] });
for (const part of res.candidates?.[0]?.content?.parts || []) {
const b64 = part.inlineData?.data || (part.text && isBase64(part.text) ? part.text : null);
if (b64) {
const filename = genFilename(prompts[i], i, outputDir);
const f = await saveImage(b64, filename, outputDir);
files.push(f);
console.log(` ${f}`);
break;
}
}
} catch (e: any) { console.error(` Error: ${e.message || e}`); }
}
if (opts.preview) for (const f of files) await openFile(f);
return files;
}
export interface EditOptions {
preview?: boolean;
mode?: string;
outputDir?: string;
}
export async function edit(file: string, prompt: string, opts: EditOptions = {}): Promise<string[]> {
const fp = findFile(file);
if (!fp) {
throw new Error(`File not found: ${file}`);
}
console.log(`${opts.mode === "restore" ? "Restoring" : "Editing"} ${fp}...`);
const ai = new GoogleGenAI({ apiKey: getApiKey() });
const model = process.env.NANOBANANA_MODEL || DEFAULT_MODEL;
const imgB64 = (await fs.promises.readFile(fp)).toString("base64");
const outputDir = opts.outputDir || ensureOutputDir();
try {
const res = await ai.models.generateContent({
model,
contents: [{ role: "user", parts: [{ text: prompt }, { inlineData: { data: imgB64, mimeType: "image/png" } }] }],
});
for (const part of res.candidates?.[0]?.content?.parts || []) {
const b64 = part.inlineData?.data || (part.text && isBase64(part.text) ? part.text : null);
if (b64) {
const filename = genFilename(`${opts.mode || "edit"}_${prompt}`, 0, outputDir);
const f = await saveImage(b64, filename, outputDir);
console.log(` ${f}`);
if (opts.preview) await openFile(f);
return [f];
}
}
} catch (e: any) { console.error(`Error: ${e.message || e}`); }
return [];
}
export interface StoryOptions {
steps?: number;
type?: string;
style?: string;
transition?: string;
preview?: boolean;
outputDir?: string;
}
export async function story(prompt: string, opts: StoryOptions = {}): Promise<string[]> {
const ai = new GoogleGenAI({ apiKey: getApiKey() });
const model = process.env.NANOBANANA_MODEL || DEFAULT_MODEL;
const files: string[] = [];
const steps = opts.steps || 4;
const type = opts.type || "story";
const outputDir = opts.outputDir || ensureOutputDir();
console.log(`Generating ${steps}-step ${type}...`);
for (let i = 0; i < steps; i++) {
let p = `${prompt}, step ${i + 1} of ${steps}`;
if (type === "process") p += ", instructional illustration";
else if (type === "tutorial") p += ", educational diagram";
else if (type === "timeline") p += ", chronological";
else p += `, ${opts.style || "consistent"} style`;
if (i > 0) p += `, ${opts.transition || "smooth"} transition`;
try {
const res = await ai.models.generateContent({ model, contents: [{ role: "user", parts: [{ text: p }] }] });
for (const part of res.candidates?.[0]?.content?.parts || []) {
const b64 = part.inlineData?.data || (part.text && isBase64(part.text) ? part.text : null);
if (b64) {
const filename = genFilename(`${type}_step${i + 1}_${prompt}`, 0, outputDir);
const f = await saveImage(b64, filename, outputDir);
files.push(f);
console.log(` Step ${i + 1}: ${f}`);
break;
}
}
} catch (e: any) { console.error(` Error step ${i + 1}: ${e.message || e}`); }
}
if (opts.preview) for (const f of files) await openFile(f);
return files;
}
// ============ Tips ============
export const TIPS: Record<string, string> = {
generate: `
generate: Create images from text
nanobanana generate "sunset over mountains"
nanobanana generate "logo" --count=4 --styles=modern,minimal
nanobanana generate "cat" --variations=lighting,mood --preview
Options: --count, --styles, --variations, --preview
Styles: photorealistic, watercolor, oil-painting, sketch, pixel-art, anime, vintage, modern, abstract, minimalist
Variations: lighting, angle, color-palette, composition, mood, season, time-of-day
`,
edit: `
edit: Modify an existing image
nanobanana edit photo.png "add sunglasses"
nanobanana edit landscape.jpg "change sky to sunset" --preview
Be specific about what to change and where.
`,
restore: `
restore: Enhance old or damaged photos
nanobanana restore old_photo.jpg
nanobanana restore damaged.png "remove scratches, enhance clarity"
`,
icon: `
icon: Generate app icons
nanobanana icon "settings gear" --sizes=64,128,256
nanobanana icon "logo" --type=favicon --style=minimal
Options: --type (app-icon,favicon,ui-element), --style (flat,minimal,modern), --sizes, --background, --corners
`,
pattern: `
pattern: Create seamless patterns
nanobanana pattern "hexagons" --style=geometric --colors=duotone
nanobanana pattern "wood grain" --type=texture
Options: --type, --style, --density, --colors, --size
`,
diagram: `
diagram: Generate technical diagrams
nanobanana diagram "login flow" --type=flowchart
nanobanana diagram "microservices" --type=architecture
Options: --type (flowchart,architecture,network,database,wireframe,mindmap,sequence), --style, --layout, --complexity, --colors
`,
story: `
story: Create sequential images
nanobanana story "seed growing into tree" --steps=5
nanobanana story "making coffee" --type=tutorial --steps=6
Options: --steps (2-8), --type (story,process,tutorial,timeline), --style, --transition
`,
};
export function showTips(cmd?: string): string {
if (!cmd) {
return `
Tips - run "nanobanana tips <command>" for details
Commands: generate, edit, restore, icon, pattern, diagram, story
General tips:
- Be specific: "golden retriever puppy" beats "dog"
- Include style: "watercolor", "photorealistic"
- Add context: "for a children's book"
`;
} else {
return TIPS[cmd] || `Unknown: ${cmd}\nAvailable: generate, edit, restore, icon, pattern, diagram, story`;
}
}
// ============ Help ============
export function showHelp(): string {
return `
nanobanana - Gemini image generation CLI
Commands:
generate <prompt> Generate images from text
edit <file> <prompt> Modify an existing image
restore <file> [prompt] Restore old/damaged photos
icon <prompt> Generate app icons
pattern <prompt> Create seamless patterns
diagram <prompt> Generate technical diagrams
story <prompt> Create image sequences
tips [command] Show prompting tips
Options:
--count=N Number of variations (1-8)
--styles=a,b Comma-separated styles
--preview, -p Open images after generation
--type=TYPE Type for icons/patterns/diagrams
--steps=N Steps for stories (2-8)
Environment:
GEMINI_API_KEY Your Gemini API key (required)
Output saved to: ./nanobanana-output/
`;
}