Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
3399f09
Refactor Gemini Service to use API middleware
google-labs-jules[bot] Sep 26, 2025
99f047a
feat: Add backend middleware for secure API calls
google-labs-jules[bot] Sep 26, 2025
84dd8b6
fix: Add production build process for server
google-labs-jules[bot] Sep 26, 2025
383bf83
fix: Convert server to CommonJS to resolve deployment error
google-labs-jules[bot] Sep 26, 2025
6d2057c
fix: Resolve deployment error using .mjs extension
google-labs-jules[bot] Sep 26, 2025
2f14b72
fix: Configure server to serve the frontend application
google-labs-jules[bot] Sep 26, 2025
0087829
fix: Correct catch-all route to resolve PathError
google-labs-jules[bot] Sep 26, 2025
1cbc85d
fix: Implement correct catch-all route syntax for Express v5+
google-labs-jules[bot] Sep 26, 2025
9ae2a65
fix: Use RegEx for catch-all route to resolve deployment crash
google-labs-jules[bot] Sep 26, 2025
635ddbe
fix: Implement robust full-stack server for production
google-labs-jules[bot] Sep 27, 2025
59fb6c8
fix: Correct image generation flow and add UI feedback
google-labs-jules[bot] Sep 27, 2025
fbe2304
fix: Implement stable full-stack configuration
google-labs-jules[bot] Sep 29, 2025
f452972
feat: Migrate backend to Vercel Serverless Functions
google-labs-jules[bot] Oct 1, 2025
1c7dbd0
fix: Implement robust Express server and add UI feedback
google-labs-jules[bot] Oct 1, 2025
ba1c983
feat: Migrate backend to Vercel Serverless Functions
google-labs-jules[bot] Oct 1, 2025
24c4b15
feat: Migrate to Vercel Serverless and Fix All Build Errors
google-labs-jules[bot] Oct 1, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 7 additions & 23 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
# Dependencies
node_modules/

node_modules
dist
dist-ssr
*.local
# Build artifacts
dist/
dist-server/

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
.env
# Jules scratch directory
jules-scratch/
29 changes: 29 additions & 0 deletions api/_lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { GoogleGenAI } from '@google/genai';

// Helper to get the Gemini API key from environment variables
export function getApiKey(): string {
const apiKey = process.env.GEMINI_API_KEY;
if (!apiKey) {
throw new Error('GEMINI_API_KEY is not set in environment variables.');
}
return apiKey;
}

// Helper to build the edit prompt
export function buildEditPrompt(instruction: string, hasMask: boolean): string {
const maskInstruction = hasMask
? "\n\nIMPORTANT: Apply changes ONLY where the mask image shows white pixels (value 255). Leave all other areas completely unchanged. Respect the mask boundaries precisely and maintain seamless blending at the edges."
: "";

return `Edit this image according to the following instruction: ${instruction}\n\nMaintain the original image's lighting, perspective, and overall composition. Make the changes look natural and seamlessly integrated.${maskInstruction}\n\nPreserve image quality and ensure the edit looks professional and realistic.`;
}

// Helper to build the segmentation prompt
export function buildSegmentationPrompt(query: string): string {
return `Analyze this image and create a segmentation mask for: ${query}\n\nReturn a JSON object with this exact structure:\n{\n "masks": [\n {\n "label": "description of the segmented object",\n "box_2d": [x, y, width, height],\n "mask": "base64-encoded binary mask image"\n }\n ]\n}\n\nOnly segment the specific object or region requested. The mask should be a binary PNG where white pixels (255) indicate the selected region and black pixels (0) indicate the background.`;
}

// Re-usable instance of the GenAI client
export function getGenAIInstance() {
return new GoogleGenAI({ apiKey: getApiKey() });
}
43 changes: 43 additions & 0 deletions api/edit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { VercelRequest, VercelResponse } from '@vercel/node';
import { getGenAIInstance, buildEditPrompt } from './_lib/helpers.js';

export default async function handler(req: VercelRequest, res: VercelResponse) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method Not Allowed' });
}

try {
const { instruction, originalImage, referenceImages, maskImage } = req.body;
const genAI = getGenAIInstance();

const contents: any[] = [
{ text: buildEditPrompt(instruction, !!maskImage) },
{ inlineData: { mimeType: 'image/png', data: originalImage } },
];

if (referenceImages && referenceImages.length > 0) {
referenceImages.forEach((image: string) => {
contents.push({ inlineData: { mimeType: 'image/png', data: image } });
});
}

if (maskImage) {
contents.push({ inlineData: { mimeType: 'image/png', data: maskImage } });
}

const result = await genAI.models.generateContent({
model: 'gemini-2.5-flash-image-preview',
contents,
});

// Correctly access the candidates from the result object
const images: string[] = result.candidates[0].content.parts
.filter((part: any) => part.inlineData)
.map((part: any) => part.inlineData.data);

return res.status(200).json({ images });
} catch (error: any) {
console.error('Error in /api/edit:', error);
return res.status(500).json({ error: error.message });
}
}
35 changes: 35 additions & 0 deletions api/generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { VercelRequest, VercelResponse } from '@vercel/node';
import { getGenAIInstance } from './_lib/helpers.js';

export default async function handler(req: VercelRequest, res: VercelResponse) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method Not Allowed' });
}

try {
const { prompt, referenceImages } = req.body;
const genAI = getGenAIInstance();

const contents: any[] = [{ text: prompt }];
if (referenceImages && referenceImages.length > 0) {
referenceImages.forEach((image: string) => {
contents.push({ inlineData: { mimeType: 'image/png', data: image } });
});
}

const result = await genAI.models.generateContent({
model: 'gemini-2.5-flash-image-preview',
contents,
});

// Correctly access the candidates from the result object
const images: string[] = result.candidates[0].content.parts
.filter((part: any) => part.inlineData)
.map((part: any) => part.inlineData.data);

return res.status(200).json({ images });
} catch (error: any) {
console.error('Error in /api/generate:', error);
return res.status(500).json({ error: error.message });
}
}
37 changes: 37 additions & 0 deletions api/segment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { VercelRequest, VercelResponse } from '@vercel/node';
import { getGenAIInstance, buildSegmentationPrompt } from './_lib/helpers.js';

export default async function handler(req: VercelRequest, res: VercelResponse) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method Not Allowed' });
}

try {
const { image, query } = req.body;
const genAI = getGenAIInstance();

const contents = [
{ text: buildSegmentationPrompt(query) },
{ inlineData: { mimeType: 'image/png', data: image } },
];

const result = await genAI.models.generateContent({
model: 'gemini-2.5-flash-image-preview',
contents,
});

// Correctly access the candidates from the result object
const responseText = result.candidates[0].content.parts[0].text;

try {
const jsonResponse = JSON.parse(responseText);
return res.status(200).json(jsonResponse);
} catch (e) {
return res.status(200).send(responseText);
}

} catch (error: any) {
console.error('Error in /api/segment:', error);
return res.status(500).json({ error: error.message });
}
}
Loading