// browser2personas.4: Die Antwort folgt der gewählten Persona; Wechsel wirkt nur auf neue Nachrichten, der Verlauf nennt je Antwort die Persona. // Aufruf: node persona-folgt.mjs [Modell] (Testinstanz mit einem Chat-Modell, das Anweisungen gut befolgt; NEXUS_BASE) import * as cdp from './lib.mjs'; const BASE=process.env.NEXUS_BASE||'http://127.0.0.1:18098', pw=process.argv[2], modell=process.argv[3]||'darkidol'; const b=await cdp.start({port:9382}); const ok=(n,c,d='')=>{console.log(`${c?'ok ':'FEHLER'} ${n}${d?' – '+d:''}`); if(!c) process.exitCode=1;}; const warte=ms=>new Promise(r=>setTimeout(r,ms)); const ev=x=>b.eval(x); const frage=async t=>{ const n0=await ev(`document.querySelectorAll('.blase.assistant').length`); await ev(`document.getElementById('chat-text').value=${JSON.stringify(t)}`); await ev(`document.getElementById('chat-senden').click()`); for(let i=0;i<400;i++){ await warte(500); if(await ev(`!document.getElementById('chat-senden').disabled && document.querySelectorAll('.blase.assistant').length>${n0}`)) break; } await warte(1000); }; const antworten=()=>ev(`[...document.querySelectorAll('.blase.assistant')].map(x=>({text:x.innerText.split(String.fromCharCode(10))[0].slice(0,140), meta:(x.querySelector('.meta')||{}).textContent||''}))`); try{ await b.login(BASE,'claude',pw); await b.goto(BASE+'/ui/personas'); await warte(800); const haben=await ev('document.body.innerText'); for(const [n,sp] of [['Pirat','Du bist ein Pirat. Beginne jede Antwort mit dem Wort „Arr!“ und sprich wie ein Pirat.'],['Roboter','Du bist ein Roboter. Beginne jede Antwort mit „BEEP.“ und antworte in höchstens zwei kurzen Sätzen.']]){ if(!new RegExp(n).test(haben)) await ev(`(async()=>{const csrf=document.body.getAttribute('data-csrf'); await fetch('/ui/personas/speichern',{method:'POST',headers:{'content-type':'application/x-www-form-urlencoded'},body:new URLSearchParams({csrf,id:'',name:${JSON.stringify(n)},systemprompt:${JSON.stringify(sp)},stil:'',stimme:''})});})()`); await warte(500); } await b.goto(BASE+'/ui/chat'); await warte(1200); await ev(`document.getElementById('chat-max').value='80'`); const waehlP=async n=>{ await ev(`(()=>{const s=document.getElementById('chat-persona'); const o=[...s.options].find(o=>o.textContent.trim()===${JSON.stringify(n)}); s.value=o.value; s.dispatchEvent(new Event('change',{bubbles:true}));})()`); await warte(300); }; await ev(`(()=>{const s=document.getElementById('chat-modell'); s.value=${JSON.stringify(modell)}; s.dispatchEvent(new Event('change',{bubbles:true}));})()`); await waehlP('Pirat'); await frage('Wie wird das Wetter morgen?'); await frage('Und was empfiehlst du zum Mittagessen?'); await waehlP('Roboter'); await frage('Erkläre kurz, was ein Scheduler ist.'); await frage('Und was ist eine Warteschlange?'); const a=await antworten(); console.log(JSON.stringify(a,null,1).slice(0,1500)); ok('Pirat: beide Antworten beginnen mit „Arr“', /^Arr/i.test(a[0].text) && /^Arr/i.test(a[1].text), a[0].text.slice(0,40)+' | '+a[1].text.slice(0,40)); ok('Roboter: beide Antworten beginnen mit „BEEP“', /^BEEP/i.test(a[2].text) && /^BEEP/i.test(a[3].text), a[2].text.slice(0,40)+' | '+a[3].text.slice(0,40)); ok('Verlauf nennt je Antwort die Persona mit Version (alte unverändert)', /Persona Pirat · Version 1/.test(a[0].meta)&&/Persona Pirat · Version 1/.test(a[1].meta)&&/Persona Roboter · Version 1/.test(a[2].meta)&&/Persona Roboter · Version 1/.test(a[3].meta), a.map(x=>x.meta.slice(0,45)).join(' || ')); }catch(e){console.log('Fehler im Lauf:',e.message); process.exitCode=1;} finally{b.close();}