diff --git a/dungeonGenerator.js b/dungeonGenerator.js index 2b72952..b3b192a 100644 --- a/dungeonGenerator.js +++ b/dungeonGenerator.js @@ -5,9 +5,8 @@ async function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } -async function callOllama(prompt, model = "gemma3:4b", retries = 3) { - let attempt = 0; - while (attempt < retries) { +async function callOllama(prompt, model = "gemma3:4b", retries = 6) { + for (let attempt = 1; attempt <= retries; attempt++) { try { const response = await fetch(OLLAMA_API_URL, { method: "POST", @@ -31,14 +30,18 @@ async function callOllama(prompt, model = "gemma3:4b", retries = 3) { return text; } catch (err) { - attempt++; console.warn(`⚠️ Ollama call failed (attempt ${attempt}/${retries}): ${err.message}`); - if (attempt >= retries) throw err; - await sleep(1000 * attempt); // exponential backoff + if (attempt === retries) throw err; + + // Exponential backoff with jitter + const delay = Math.pow(2, attempt) * 1000; // 2^attempt seconds + const jitter = Math.random() * 1000; // up to 1 second extra + await sleep(delay + jitter); } } } + /** * Three-pass dungeon generation with full resiliency and JSON retry */ @@ -105,11 +108,11 @@ Dungeon description: ${refined} `; - const maxJsonRetries = 3; + const maxJsonRetries = 5; for (let attempt = 1; attempt <= maxJsonRetries; attempt++) { try { console.log(`📦 JSON pass (attempt ${attempt}/${maxJsonRetries})...`); - jsonText = await callOllama(jsonPrompt); + jsonText = await callOllama(jsonPrompt, "gemma3:4b", 6); const cleaned = jsonText.replace(/```json|```/g, "").trim(); const result = JSON.parse(cleaned); console.log("🎉 Dungeon generation complete!");