okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ea2d565e6
main
1/** 2* Settings -> Tools and what it gives the assistant, through the real app on a real model, with screenshots (playwright-core + Chrome). 3* 4* STRATA_URL=http://127.0.0.1:8899 SHOTS=./shots MODEL='Qwen test' node scripts/agent2-tools-flow.mjs 5* 6* The host must run with STRATA_BROWSER_HEADLESS=1 (the browser step must not open a window on the person's desktop) and a scratch 7* STRATA_HOME / STRATA_TIMESKIP_DIR. Steps (each prints ok/FAIL): 8* 1. Settings -> Tools shows the groups; two tools of my own are added through the dialog (instructions, command); 9* 2. web search with no key: "Searched the web" card, real DuckDuckGo results in the answer; 10* 3. the instruction tool is used without asking and shapes the answer; the command tool asks first in Manual, runs after Approve; 11* 4. Search the web switched off: the assistant has no search tool any more; 12* 5. the browser switched on: Manual asks before opening https://example.com, the answer names the page's heading. 13*/ 14import { chromium } from 'playwright-core' ; 15import { existsSync , mkdirSync } from 'node:fs' ; 16import { join , resolve } from 'node:path' ; 17 18const BASE = process . env . STRATA_URL ; 19if ( ! BASE ) { console . error ( 'STRATA_URL is required' ); process . exit ( 2 ); } 20const SHOTS = resolve ( process . env . SHOTS ?? 'agent2-tools-shots' ); 21mkdirSync ( SHOTS , { recursive :true }); 22const MODEL = process . env . MODEL ?? 'Qwen test' ; 23const SKIP = new Set (( process . env . SKIP ?? '' ). split ( ',' ). filter ( Boolean )); 24const CHROME = process . env . CHROME ?? [ 'C:/Program Files/Google/Chrome/Application/chrome.exe' , '/usr/bin/google-chrome' ]. find ( existsSync ); 25 26let failed = false ; 27const check = ( ok , label ) => { console . log ( ` ${ ok ? 'ok ' : 'FAIL' } ${ label } ` ); if ( ! ok ) failed = true ; }; 28const browser = await chromium . launch ({ executablePath :CHROME , headless :true }); 29const page = await browser . newPage ({ viewport :{ width :1360 , height :980 } }); 30const problems = []; 31page . on ( 'pageerror' , e => problems . push ( `pageerror: ${ e . message } ` )); 32page . on ( 'console' , m => { if ( m . type () === 'error' ) problems . push ( `console: ${ m . text (). slice ( 0 , 200 )} ` ); }); 33const shot = async name => { await page . waitForTimeout ( 500 ); await page . screenshot ({ path :join ( SHOTS , ` ${ name } .png` ) }); console . log ( `shot ${ name } .png` ); }; 34const send = async text => { await page . locator ( 'textarea' ). first (). fill ( text ); await page . getByRole ( 'button' , { name :'Send' }). click (); }; 35const idle = async ( ms = 900000 ) => page . waitForFunction (() => document . documentElement . dataset . streaming !== 'true' , null , { timeout :ms }); 36const started = async () => page . waitForFunction (() => document . documentElement . dataset . streaming === 'true' , null , { timeout :30000 }); 37const chatText = () => page . evaluate (() => document . querySelector ( '.chat' )?. innerText ?? document . body . innerText ); 38 39async function approveUntilIdle ( label ) { 40let asked = 0 ; const seen = new Set (); const reasons = []; 41for (;;) { 42const streaming = await page . evaluate (() => document . documentElement . dataset . streaming === 'true' ); 43const cards = page . locator ( '.toolcard[data-state="proposed"]' ); 44const n = await cards . count (); 45for ( let i = 0 ; i < n ; i ++ ) { 46const card = cards . nth ( i ); 47const key = ( await card . innerText ()). slice ( 0 , 200 ); 48if ( ! seen . has ( key )) { seen . add ( key ); asked ++ ; reasons . push ( key . replace ( / \s +/ g , ' ' )); await shot ( ` ${ label } -approve- ${ asked } ` ); } 49await card . getByRole ( 'button' , { name :'Approve' }). click (). catch (() => {}); 50} 51if ( ! streaming && n === 0 ) break ; 52await page . waitForTimeout ( 400 ); 53} 54return { asked, reasons}; 55} 56async function openTools () { 57await page . goto ( ` ${ BASE } /#/settings` ); 58await page . waitForSelector ( '#settings-tab-Tools' , { timeout :20000 }); 59await page . locator ( '#settings-tab-Tools' ). evaluate ( el => el . click ()); 60await page . waitForSelector ( '.settings-tools' , { timeout :10000 }); 61await page . waitForSelector ( '.settings-tools [role="switch"]' , { timeout :10000 }); 62} 63async function newChat () { 64await page . goto ( ` ${ BASE } /#/chat` ); 65await page . waitForTimeout ( 1200 ); 66await page . getByRole ( 'button' , { name :'New chat' }). first (). click (); 67await page . waitForTimeout ( 600 ); 68await page . getByRole ( 'button' , { name :/ Choose model / }). first (). click (); 69await page . waitForTimeout ( 300 ); 70await page . getByRole ( 'menuitemradio' , { name :new RegExp ( MODEL ) }). first (). click (); 71await page . waitForTimeout ( 300 ); 72} 73 74try { 75await page . goto ( ` ${ BASE } /` ); 76const skip = page . getByText ( 'Go straight to the app' ); 77await skip . waitFor ({ timeout :8000 }). then (() => skip . click ()). catch (() => {}); 78 79// 1. the screen, and two tools of my own 80if ( ! SKIP . has ( 'own' )) { 81await openTools (); 82await shot ( '01-tools' ); 83const names = await page . locator ( '.settings-tools .field__label, .settings-tools [class*="field__label"]' ). allInnerTexts (); 84check ([ 'Run commands' , 'Search the web' , 'Open web pages' , 'Goals' ]. every ( n => names . some ( x => x . includes ( n ))), 'the groups are listed in plain words' ); 85await page . getByRole ( 'button' , { name :'Add a tool' }). click (); 86await page . getByRole ( 'dialog' ). getByLabel ( 'Name' ). fill ( 'House style' ); 87await page . getByRole ( 'dialog' ). getByLabel ( 'What it does' ). fill ( 'Our house style for every answer.' ); 88await page . getByRole ( 'dialog' ). getByLabel ( 'Instructions' ). fill ( 'End your answer with the single word FINISHED on a line of its own.' ); 89await shot ( '02-add-instructions' ); 90await page . getByRole ( 'button' , { name :'Save tool' }). click (); 91await page . waitForTimeout ( 800 ); 92await page . getByRole ( 'button' , { name :'Add a tool' }). click (); 93await page . getByRole ( 'dialog' ). getByLabel ( 'Name' ). fill ( 'Say hello' ); 94await page . getByRole ( 'dialog' ). getByLabel ( 'What it does' ). fill ( 'Prints a greeting from this computer.' ); 95await page . getByRole ( 'dialog' ). getByLabel ( 'How it works' ). click (); 96await page . getByRole ( 'option' , { name :'Run a command' }). click (); 97await page . getByRole ( 'dialog' ). getByLabel ( 'Command to run' ). fill ( 'echo hello from my own tool' ); 98await shot ( '03-add-command' ); 99await page . getByRole ( 'button' , { name :'Save tool' }). click (); 100await page . waitForTimeout ( 800 ); 101await page . getByRole ( 'heading' , { name :'Your own tools' }). evaluate ( el => el . scrollIntoView ({ block :'start' })); 102await shot ( '04-own-tools' ); 103const own = await page . locator ( '.settings-tools' ). innerText (); 104check ( / House style / . test ( own ) && / Say hello / . test ( own ), 'both tools of my own are listed' ); 105} 106 107// 2. web search with no key 108if ( ! SKIP . has ( 'search' )) { 109await newChat (); 110await send ( 'Use the web_search tool to find the GitHub page of the Strata local LLM engine by Niko1221. Tell me its address.' ); 111await started (). catch (() => {}); 112const r = await approveUntilIdle ( '05' ); 113await idle (); 114await shot ( '05-search' ); 115const body = await chatText (); 116check ( / Searched the web / . test ( body ), 'a "Searched the web" card appeared' ); 117check ( r . asked === 0 , 'searching asked nothing (it only reads)' ); 118check ( / github\.com\/Niko1221\/Strata / i . test ( body ), 'the answer names the real GitHub address from the results' ); 119} 120 121// 3. my own tools 122if ( ! SKIP . has ( 'use' )) { 123await newChat (); 124await send ( 'Use my House style tool, then tell me what 2 plus 2 is.' ); 125await started (). catch (() => {}); 126const a = await approveUntilIdle ( '06' ); 127await idle (); 128await shot ( '06-house-style' ); 129const body = await chatText (); 130check ( / Read your instructions / . test ( body ), 'a "Read your instructions" card appeared' ); 131check ( a . asked === 0 , 'an instruction tool asked nothing' ); 132check ( / FINISHED\s * $ / m . test ( body . trim (). split ( '\n' ). filter ( l => ! /tok\/s | total tokens | of context / . test ( l )). join ( '\n' )), 'the answer follows the instructions (ends with FINISHED)' ); 133await send ( 'Now use my Say hello tool and tell me what it printed.' ); 134await started (). catch (() => {}); 135const b = await approveUntilIdle ( '07' ); 136await idle (); 137await shot ( '07-say-hello' ); 138const after = await chatText (); 139check ( b . asked >= 1 && b . reasons . some ( x => / Running your tool. * say hello / i . test ( x )), `Manual asked first, in plain words ( ${ b . reasons [ 0 ] ?? 'no card' } )` ); 140check ( / hello from my own tool / i . test ( after ), 'after Approve the command ran and the answer has its output' ); 141} 142 143// 4. Search the web switched off 144if ( ! SKIP . has ( 'off' )) { 145await openTools (); 146await page . getByRole ( 'switch' , { name :'Search the web' }). click (); 147await page . waitForTimeout ( 800 ); 148await shot ( '08-search-off' ); 149check (( await page . getByRole ( 'switch' , { name :'Search the web' }). getAttribute ( 'aria-checked' )) === 'false' , 'the switch is off' ); 150await newChat (); 151await send ( 'Use the web_search tool to search for the word Strata. If you have no such tool, say exactly: NO SEARCH TOOL.' ); 152await started (). catch (() => {}); 153await approveUntilIdle ( '08' ); 154await idle (); 155await shot ( '09-no-search' ); 156const body = await chatText (); 157check ( ! /Searched the web | Searching the web / . test ( body ), 'no search card: the assistant has no search tool now' ); 158await openTools (); 159await page . getByRole ( 'switch' , { name :'Search the web' }). click (); 160await page . waitForTimeout ( 500 ); 161} 162 163// 5. the browser 164if ( ! SKIP . has ( 'browser' )) { 165await openTools (); 166await page . getByRole ( 'heading' , { name :'Web browser' }). evaluate ( el => el . scrollIntoView ({ block :'start' })); 167const sw = page . getByRole ( 'switch' , { name :'Use a web browser' }); 168check ( await sw . isEnabled (), 'the browser switch is available (Chrome or Edge and the tool server were found)' ); 169if (( await sw . getAttribute ( 'aria-checked' )) !== 'true' ) await sw . click (); 170await page . waitForTimeout ( 800 ); 171await shot ( '10-browser-on' ); 172await newChat (); 173await send ( 'Open https://example.com in the browser and tell me the big heading on that page.' ); 174await started (). catch (() => {}); 175const r = await approveUntilIdle ( '11' ); 176await idle (); 177await shot ( '11-browser' ); 178const body = await chatText (); 179check ( r . asked >= 1 && r . reasons . some ( x => / Opening a page in the browser. * example\.com / i . test ( x )), `Manual asked before opening the page ( ${ r . reasons [ 0 ] ?? 'no card' } )` ); 180check ( / Opened a page in the browser / . test ( body ), 'a plain "Opened a page in the browser" card appeared' ); 181check ( / Example Domain / . test ( body ), 'the answer names the page heading' ); 182// leave the settings as they were found 183await openTools (); 184await page . getByRole ( 'heading' , { name :'Web browser' }). evaluate ( el => el . scrollIntoView ({ block :'start' })); 185if (( await page . getByRole ( 'switch' , { name :'Use a web browser' }). getAttribute ( 'aria-checked' )) === 'true' ) await page . getByRole ( 'switch' , { name :'Use a web browser' }). click (); 186} 187} catch ( e ) { 188console . log ( 'ERROR' , e . message ); 189await shot ( 'zz-error' ). catch (() => {}); 190failed = true ; 191} finally { 192const text = problems . filter ( p => ! /Failed to load resource | ERR_ | favicon / . test ( p )); 193console . log ( `page problems: ${ text . length } ` ); text . slice ( 0 , 8 ). forEach ( p => console . log ( ' ' + p )); 194await browser . close (); 195} 196process . exit ( failed ?1 :0 );