mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-03 15:27:56 +01:00
* design: make images and read screenshots with Gemini when there is no OpenAI key (#990) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * design: test the live Gemini check; set fixture env keys without secret-shaped assignments Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: pre-landing review fixes (setup keeps piped keys and names the provider export; Gemini iterate fallback tests) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * design: setup doc comment matches --key-stdin behavior Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * fix: Gemini saves the final image of a thinking answer; iterate uploads only absolute-path PNGs under 15 MB; sessions store absolute output paths Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * changelog: fragment holds the entry text only Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * todos: Gemini design provider follow-ups Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * docs: the egress fail-open list names design Gemini calls Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --------- Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
79 lines
3.0 KiB
TypeScript
79 lines
3.0 KiB
TypeScript
/**
|
|
* Design-to-Code Prompt Generator.
|
|
* Extracts implementation instructions from an approved mockup via the active provider's vision model.
|
|
* Produces a structured prompt the agent can use to implement the design.
|
|
*/
|
|
|
|
import fs from "fs";
|
|
import { requireProvider } from "./auth";
|
|
import { receiptedFetch } from "./receipted-fetch";
|
|
import { visionErrorHint, visionRequest } from "./provider";
|
|
import { readDesignConstraints } from "./memory";
|
|
|
|
export interface DesignToCodeResult {
|
|
implementationPrompt: string;
|
|
colors: string[];
|
|
typography: string[];
|
|
layout: string[];
|
|
components: string[];
|
|
}
|
|
|
|
/**
|
|
* Generate a structured implementation prompt from an approved mockup.
|
|
*/
|
|
export async function generateDesignToCodePrompt(
|
|
imagePath: string,
|
|
repoRoot?: string,
|
|
): Promise<DesignToCodeResult> {
|
|
const provider = requireProvider();
|
|
const imageData = fs.readFileSync(imagePath).toString("base64");
|
|
|
|
// Read DESIGN.md if available for additional context
|
|
const designConstraints = repoRoot ? readDesignConstraints(repoRoot) : null;
|
|
|
|
const controller = new AbortController();
|
|
const timeout = setTimeout(() => controller.abort(), 60_000);
|
|
|
|
try {
|
|
const contextBlock = designConstraints
|
|
? `\n\nExisting DESIGN.md (use these as constraints):\n${designConstraints}`
|
|
: "";
|
|
|
|
const request = visionRequest(provider, [{
|
|
role: "user",
|
|
content: [
|
|
{
|
|
type: "image_url",
|
|
image_url: { url: `data:image/png;base64,${imageData}` },
|
|
},
|
|
{
|
|
type: "text",
|
|
text: `Analyze this approved UI mockup and generate a structured implementation prompt. Return valid JSON only:
|
|
|
|
{
|
|
"implementationPrompt": "A detailed paragraph telling a developer exactly how to build this UI. Include specific CSS values, layout approach (flex/grid), component structure, and interaction behaviors. Reference the specific elements visible in the mockup.",
|
|
"colors": ["#hex - usage", ...],
|
|
"typography": ["role: family, size, weight", ...],
|
|
"layout": ["description of layout pattern", ...],
|
|
"components": ["component name - description", ...]
|
|
}
|
|
|
|
Be specific about every visual detail: exact hex colors, font sizes in px, spacing values, border-radius, shadows. The developer should be able to implement this without looking at the mockup again.${contextBlock}`,
|
|
},
|
|
],
|
|
}], 1000, { response_format: { type: "json_object" } });
|
|
const response = await receiptedFetch("design-to-code-request", request.url, { ...request.init, signal: controller.signal }, globalThis.fetch, request.receipt);
|
|
|
|
if (!response.ok) {
|
|
const error = await response.text();
|
|
throw new Error(`API error (${response.status}): ${error.slice(0, 200)}${visionErrorHint(provider, response.status, error)}`);
|
|
}
|
|
|
|
const data = await response.json() as any;
|
|
const content = data.choices?.[0]?.message?.content?.trim() || "";
|
|
return JSON.parse(content) as DesignToCodeResult;
|
|
} finally {
|
|
clearTimeout(timeout);
|
|
}
|
|
}
|