Elffuss BraidHub

utopia-ia/elffuss-code public

88d1ced5b041 · 2.2 KiBRawPermalink
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.
4import { chromium } from 'playwright';
5const b = await chromium.launch({ channel: 'chrome', headless: true });
6const p = await (await b.newContext()).newPage();
7// Por defecto contra producción, como siempre; BASE=… la corre contra otro
8// servidor (un árbol local), que es lo que hace falta antes de desplegar.
9await p.goto((process.env.BASE || 'https://code.elffuss.com') + '/', { waitUntil: 'domcontentloaded' });
10await p.waitForTimeout(1200);
11const r = await p.evaluate(async () => {
12 const { Agent } = await import('/js/agent.js?v=' + Date.now());
13 const code = await import('/js/tools/code.js');
14 const raiz = await navigator.storage.getDirectory();
15 for await (const [n] of raiz.entries()) await raiz.removeEntry(n, { recursive: true }).catch(()=>{});
16 const w = await (await raiz.getFileHandle('bueno.js', { create: true })).createWritable();
17 await w.write('export const x = 42;'); await w.close();
18 await code.openProject(raiz);
19 // proveedor: falla la 1ª, y si ve ERROR en el historial reintenta con el bueno
20 let llamadas = 0; const vistos = [];
21 const guion = { chat: async (history) => {
22 llamadas++;
23 const huboError = history.some(m => /ERROR|Reanaliza/.test(m.content || ''));
24 vistos.push(huboError ? 'vio-error' : 'primera');
25 if (!huboError) return '```tool\n{"tool":"code.read","args":{"path":"noexiste.js"}}\n```';
26 if (llamadas <= 3) return '```tool\n{"tool":"code.read","args":{"path":"bueno.js"}}\n```';
27 return 'Ya está, leído bueno.js.';
28 }};
29 const ev = [];
30 await new Agent(guion).handle('lee el fichero', e => ev.push(e.type + (e.tool ? ':' + e.tool : '')));
31 return { llamadas, vistos, tipos: [...new Set(ev)], reintentó: vistos.includes('vio-error') };
32});
33console.log(' llamadas al modelo:', r.llamadas, '·', r.vistos.join(' → '));
34console.log(' eventos:', r.tipos.join(', '));
35console.log(r.reintentó ? '\n✅ reintenta: tras el ERROR, el modelo recibe la pista y corrige (no se rinde)'
36 : '\n❌ no reintentó');
37await b.close();
38process.exit(r.reintentó ? 0 : 1);