| 1 | // ¿El agente REINTENTA tras un error en vez de rendirse? |
| 2 | // Proveedor de guion: 1ª vez pide code.read de un fichero que no existe (falla); |
| 3 | // 2ª vez, viendo el ERROR + la pista en el historial, corrige y lee el bueno. |
| 4 | import { chromium } from 'playwright'; |
| 5 | const b = await chromium.launch({ channel: 'chrome', headless: true }); |
| 6 | // Modo Básico: esta prueba pone su propio proveedor y no necesita modelo. Sin |
| 7 | // esto, al abrir la página main.js se pone a cargar el modelo local por defecto |
| 8 | // (con GPU, Gemma; sin ella, el ONNX ligero) mientras la prueba corre. |
| 9 | const ctxPrueba = await b.newContext(); |
| 10 | await ctxPrueba.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 11 | const p = await ctxPrueba.newPage(); |
| 12 | // Por defecto contra producción, como siempre; BASE=… la corre contra otro |
| 13 | // servidor (un árbol local), que es lo que hace falta antes de desplegar. |
| 14 | await p.goto((process.env.BASE || 'https://code.elffuss.com') + '/', { waitUntil: 'domcontentloaded' }); |
| 15 | await p.waitForTimeout(1200); |
| 16 | const r = await p.evaluate(async () => { |
| 17 | const { Agent } = await import('/js/agent.js?v=' + Date.now()); |
| 18 | const code = await import('/js/tools/code.js'); |
| 19 | const raiz = await navigator.storage.getDirectory(); |
| 20 | for await (const [n] of raiz.entries()) await raiz.removeEntry(n, { recursive: true }).catch(()=>{}); |
| 21 | const w = await (await raiz.getFileHandle('bueno.js', { create: true })).createWritable(); |
| 22 | await w.write('export const x = 42;'); await w.close(); |
| 23 | await code.openProject(raiz); |
| 24 | // proveedor: falla la 1ª, y si ve ERROR en el historial reintenta con el bueno |
| 25 | let llamadas = 0; const vistos = []; |
| 26 | const guion = { chat: async (history) => { |
| 27 | llamadas++; |
| 28 | const huboError = history.some(m => /ERROR|Reanaliza/.test(m.content || '')); |
| 29 | vistos.push(huboError ? 'vio-error' : 'primera'); |
| 30 | if (!huboError) return '```tool\n{"tool":"code.read","args":{"path":"noexiste.js"}}\n```'; |
| 31 | if (llamadas <= 3) return '```tool\n{"tool":"code.read","args":{"path":"bueno.js"}}\n```'; |
| 32 | return 'Ya está, leído bueno.js.'; |
| 33 | }}; |
| 34 | const ev = []; |
| 35 | await new Agent(guion).handle('lee el fichero', e => ev.push(e.type + (e.tool ? ':' + e.tool : ''))); |
| 36 | return { llamadas, vistos, tipos: [...new Set(ev)], reintentó: vistos.includes('vio-error') }; |
| 37 | }); |
| 38 | console.log(' llamadas al modelo:', r.llamadas, '·', r.vistos.join(' → ')); |
| 39 | console.log(' eventos:', r.tipos.join(', ')); |
| 40 | console.log(r.reintentó ? '\n✅ reintenta: tras el ERROR, el modelo recibe la pista y corrige (no se rinde)' |
| 41 | : '\n❌ no reintentó'); |
| 42 | await b.close(); |
| 43 | process.exit(r.reintentó ? 0 : 1); |