← back to Visual Factory

src/stages/iterate.js

41 lines

// Stage 7 — iterate. qwen3:14b applies critic.fix_instructions to the HTML, single retry.

const fs = require('node:fs/promises');
const { ollama } = require('../llm');

const SYSTEM = `You revise a single HTML+CSS document in place per the listed fixes.
Output ONLY the revised raw HTML. No code fences, no prose. Same self-contained structure as before.`;

async function runIterate({ htmlPath, html, spec, review, signal = null }) {
  if (review.approved) return { changed: false, html };

  const userPrompt = `Spec:
${JSON.stringify(spec, null, 2)}

Current HTML:
---BEGIN HTML---
${html}
---END HTML---

Issues found:
${review.issues.map((s, i) => `${i + 1}. ${s}`).join('\n')}

Fixes to apply:
${review.fix_instructions.map((s, i) => `${i + 1}. ${s}`).join('\n')}

Output the revised raw HTML now.`;

  const revised = (await ollama({
    model: process.env.COMPOSE_MODEL || 'qwen3:14b',
    system: SYSTEM,
    prompt: userPrompt,
    temperature: 0.3,
    signal
  })).replace(/^```(?:html)?\s*\n/i, '').replace(/\n```\s*$/, '').trim();

  await fs.writeFile(htmlPath, revised, 'utf8');
  return { changed: true, html: revised };
}

module.exports = { runIterate };