// browser2systemprompt.1 bis .3 (#1248, ab 0.1.32): Systemprompt-Ebenen im echten Browser mit einem echten kleinen Modell: // globaler Prompt auf der Modellseite (nur Admin), Standard-Persona (im Chat vorgewählt), Ersetzen statt Stapeln. // Aufruf: node systemprompt-ebenen.mjs (Benutzer `claude`, `NEXUS_BASE`, Standard http://127.0.0.1:18131; // Testinstanz mit llama.cpp und dem Modell `qwen-klein`). Die Antwort eines kleinen Modells ist nur ein Hinweis, kein Beweis: // geprüft wird, ob das gewünschte Wort vorkommt. Nicht gegen die Produktion. import * as cdp from './lib.mjs'; const BASE = process.env.NEXUS_BASE || 'http://127.0.0.1:18131', pw = process.argv[2]; const MODELL = process.env.NEXUS_MODELL || 'qwen-klein'; const AUS = process.env.NEXUS_AUS || '/tmp'; const b = await cdp.start({ port: 9409 }); const ergebnisse = []; const pruefe = (name, ok, extra = '') => { ergebnisse.push(ok); console.log(ok ? 'OK ' : 'FEHL', name, extra); }; const warte = ms => new Promise(r => setTimeout(r, ms)); const absenden = async (action, felder, such = 'true') => { const r = await b.eval(`(()=>{const f=[...document.querySelectorAll('form')].find(f=>f.getAttribute('action')===${JSON.stringify(action)} && ${such}); if(!f) return 'Formular fehlt'; for (const [k,v] of Object.entries(${JSON.stringify(felder)})) { const e=f.elements[k]; if(!e) return 'fehlt '+k; if(e.type==='checkbox') e.checked=!!v; else e.value=v; } f.requestSubmit(); return true;})()`); await warte(2000); return r; }; // Eine Frage über das Chat-Fenster stellen (Persona nach Auswahl) und die letzte Antwort lesen. const fragen = async (frage) => { await b.eval(`(()=>{const m=document.querySelector('#chat-modell'); m.value=${JSON.stringify(MODELL)}; m.dispatchEvent(new Event('change',{bubbles:true})); document.querySelector('#chat-max').value='48';})()`); await b.eval(`document.querySelector('#chat-text').focus()`); await b.send('Input.insertText', { text: frage }); await warte(300); await b.send('Input.dispatchKeyEvent', { type: 'keyDown', key: 'Enter', code: 'Enter', windowsVirtualKeyCode: 13, text: '\r' }); await b.send('Input.dispatchKeyEvent', { type: 'keyUp', key: 'Enter', code: 'Enter', windowsVirtualKeyCode: 13 }); await warte(45000); return b.eval(`(()=>{const t=[...document.querySelectorAll('#chat-verlauf *')].map(e=>e.innerText).filter(Boolean); return document.querySelector('#chat-verlauf').innerText;})()`); }; try { await b.login(BASE, 'claude', pw); // .1 globaler Prompt auf der Modellseite await b.goto(BASE + '/ui/modelle'); const global = 'Antworte auf jede Frage ausschließlich mit dem einen Wort BANANE.'; pruefe('Modellseite hat das Feld „Systemprompt (global)“ für Admins', await b.eval(`!!document.querySelector('form[action="/ui/systemprompt/global"] textarea')`)); pruefe('Der Text erklärt, dass die speziellere Ebene ersetzt', (await b.eval('document.body.innerText')).includes('ersetzt die allgemeinere')); const ok1 = await absenden('/ui/systemprompt/global', { systemprompt: global }); pruefe('Globalen Prompt speichern', ok1 === true && /gespeichert/.test(await b.eval('document.body.innerText'))); // .2 Standard-Persona await b.goto(BASE + '/ui/personas'); const persona = 'Antworte auf jede Frage ausschließlich mit dem einen Wort KIRSCHE.'; const ok2 = await absenden('/ui/personas/speichern', { name: 'Testpersona', systemprompt: persona, stil: '', standard: true }); pruefe('Persona als Standard speichern', ok2 === true && /Testpersona[^\n]*Standard/.test(await b.eval('document.body.innerText'))); await b.goto(BASE + '/ui/chat'); const vorgewaehlt = await b.eval(`(()=>{const s=document.querySelector('#chat-persona'); return s.options[s.selectedIndex].textContent;})()`); pruefe('Im Chat ist die Standard-Persona vorgewählt', vorgewaehlt === 'Testpersona (Standard)', `(${vorgewaehlt})`); await b.shot(`${AUS}/systemprompt-chat.png`); // .3 Ersetzen statt Stapeln (echtes Modell) let antwort = await fragen('Wie heißt die Hauptstadt von Frankreich?'); console.log('Antwort mit Persona:', JSON.stringify(antwort.slice(-120))); pruefe('Mit Standard-Persona: die Persona gilt (KIRSCHE), der globale Prompt nicht (BANANE)', /KIRSCHE/i.test(antwort) && !/BANANE/i.test(antwort)); await b.goto(BASE + '/ui/chat'); await b.eval(`(()=>{const s=document.querySelector('#chat-persona'); s.value=''; s.dispatchEvent(new Event('change',{bubbles:true}));})()`); antwort = await fragen('Wie heißt die Hauptstadt von Deutschland?'); console.log('Antwort ohne Persona:', JSON.stringify(antwort.slice(-120))); pruefe('Ohne Persona („aus“): der globale Prompt gilt (BANANE)', /BANANE/i.test(antwort) && !/KIRSCHE/i.test(antwort)); await b.shot(`${AUS}/systemprompt-chat-2.png`); // .5 Der Längenhinweis zählt nur die wirksame Ebene (Kontext des Modells 2048 Tokens: Hinweis ab 1024, also ab rund 3600 Zeichen) await b.goto(BASE + '/ui/modelle'); await absenden('/ui/systemprompt/global', { systemprompt: 'x'.repeat(4200) }); await b.goto(BASE + '/ui/chat'); const hinweis = () => b.eval(`document.querySelector('#chat-persona-hinweis').textContent`); await b.eval(`(()=>{const m=document.querySelector('#chat-modell'); m.value=${JSON.stringify(MODELL)}; m.dispatchEvent(new Event('change',{bubbles:true})); const s=document.querySelector('#chat-persona'); s.value=''; s.dispatchEvent(new Event('change',{bubbles:true}));})()`); const ohne = await hinweis(); pruefe('Ohne Persona zählt der lange globale Prompt: Längenhinweis erscheint', /Der wirksame Systemprompt belegt geschätzt/.test(ohne), ohne.slice(0, 80)); await b.eval(`(()=>{const s=document.querySelector('#chat-persona'); s.selectedIndex=[...s.options].findIndex(o=>/Standard/.test(o.textContent)); s.dispatchEvent(new Event('change',{bubbles:true}));})()`); const mit = await hinweis(); pruefe('Mit der kurzen Persona (ersetzt den globalen): kein Längenhinweis', mit === '', JSON.stringify(mit)); await b.goto(BASE + '/ui/modelle'); await absenden('/ui/systemprompt/global', { systemprompt: '' }); } finally { b.close(); } const fehl = ergebnisse.filter(x => !x).length; console.log(fehl ? `${fehl} Prüfung(en) fehlgeschlagen` : `Alle ${ergebnisse.length} Prüfungen bestanden`); process.exit(fehl ? 1 : 0);