okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221merge f-split-agent2 c7b65c7f7 (/loop, /goal, /time-skip, keyless search, system-browser tool, Settings Tools, Plan refuses connected tools) into f-split-intea2d565e6

main
10.8 KiB196 linesraw
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) {
40  let asked = 0; const seen = new Set(); const reasons = [];
41  for (;;) {
42    const streaming = await page.evaluate(() => document.documentElement.dataset.streaming === 'true');
43    const cards = page.locator('.toolcard[data-state="proposed"]');
44    const n = await cards.count();
45    for (let i = 0; i < n; i++) {
46      const card = cards.nth(i);
47      const key = (await card.innerText()).slice(0, 200);
48      if (!seen.has(key)) { seen.add(key); asked++; reasons.push(key.replace(/\s+/g, ' ')); await shot(`${label}-approve-${asked}`); }
49      await card.getByRole('button', { name: 'Approve' }).click().catch(() => {});
50    }
51    if (!streaming && n === 0) break;
52    await page.waitForTimeout(400);
53  }
54  return { asked, reasons };
55}
56async function openTools() {
57  await page.goto(`${BASE}/#/settings`);
58  await page.waitForSelector('#settings-tab-Tools', { timeout: 20000 });
59  await page.locator('#settings-tab-Tools').evaluate(el => el.click());
60  await page.waitForSelector('.settings-tools', { timeout: 10000 });
61  await page.waitForSelector('.settings-tools [role="switch"]', { timeout: 10000 });
62}
63async function newChat() {
64  await page.goto(`${BASE}/#/chat`);
65  await page.waitForTimeout(1200);
66  await page.getByRole('button', { name: 'New chat' }).first().click();
67  await page.waitForTimeout(600);
68  await page.getByRole('button', { name: /Choose model/ }).first().click();
69  await page.waitForTimeout(300);
70  await page.getByRole('menuitemradio', { name: new RegExp(MODEL) }).first().click();
71  await page.waitForTimeout(300);
72}
73
74try {
75  await page.goto(`${BASE}/`);
76  const skip = page.getByText('Go straight to the app');
77  await skip.waitFor({ timeout: 8000 }).then(() => skip.click()).catch(() => {});
78
79  // 1. the screen, and two tools of my own
80  if (!SKIP.has('own')) {
81    await openTools();
82    await shot('01-tools');
83    const names = await page.locator('.settings-tools .field__label, .settings-tools [class*="field__label"]').allInnerTexts();
84    check(['Run commands', 'Search the web', 'Open web pages', 'Goals'].every(n => names.some(x => x.includes(n))), 'the groups are listed in plain words');
85    await page.getByRole('button', { name: 'Add a tool' }).click();
86    await page.getByRole('dialog').getByLabel('Name').fill('House style');
87    await page.getByRole('dialog').getByLabel('What it does').fill('Our house style for every answer.');
88    await page.getByRole('dialog').getByLabel('Instructions').fill('End your answer with the single word FINISHED on a line of its own.');
89    await shot('02-add-instructions');
90    await page.getByRole('button', { name: 'Save tool' }).click();
91    await page.waitForTimeout(800);
92    await page.getByRole('button', { name: 'Add a tool' }).click();
93    await page.getByRole('dialog').getByLabel('Name').fill('Say hello');
94    await page.getByRole('dialog').getByLabel('What it does').fill('Prints a greeting from this computer.');
95    await page.getByRole('dialog').getByLabel('How it works').click();
96    await page.getByRole('option', { name: 'Run a command' }).click();
97    await page.getByRole('dialog').getByLabel('Command to run').fill('echo hello from my own tool');
98    await shot('03-add-command');
99    await page.getByRole('button', { name: 'Save tool' }).click();
100    await page.waitForTimeout(800);
101    await page.getByRole('heading', { name: 'Your own tools' }).evaluate(el => el.scrollIntoView({ block: 'start' }));
102    await shot('04-own-tools');
103    const own = await page.locator('.settings-tools').innerText();
104    check(/House style/.test(own) && /Say hello/.test(own), 'both tools of my own are listed');
105  }
106
107  // 2. web search with no key
108  if (!SKIP.has('search')) {
109    await newChat();
110    await send('Use the web_search tool to find the GitHub page of the Strata local LLM engine by Niko1221. Tell me its address.');
111    await started().catch(() => {});
112    const r = await approveUntilIdle('05');
113    await idle();
114    await shot('05-search');
115    const body = await chatText();
116    check(/Searched the web/.test(body), 'a "Searched the web" card appeared');
117    check(r.asked === 0, 'searching asked nothing (it only reads)');
118    check(/github\.com\/Niko1221\/Strata/i.test(body), 'the answer names the real GitHub address from the results');
119  }
120
121  // 3. my own tools
122  if (!SKIP.has('use')) {
123    await newChat();
124    await send('Use my House style tool, then tell me what 2 plus 2 is.');
125    await started().catch(() => {});
126    const a = await approveUntilIdle('06');
127    await idle();
128    await shot('06-house-style');
129    const body = await chatText();
130    check(/Read your instructions/.test(body), 'a "Read your instructions" card appeared');
131    check(a.asked === 0, 'an instruction tool asked nothing');
132    check(/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)');
133    await send('Now use my Say hello tool and tell me what it printed.');
134    await started().catch(() => {});
135    const b = await approveUntilIdle('07');
136    await idle();
137    await shot('07-say-hello');
138    const after = await chatText();
139    check(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'})`);
140    check(/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
144  if (!SKIP.has('off')) {
145    await openTools();
146    await page.getByRole('switch', { name: 'Search the web' }).click();
147    await page.waitForTimeout(800);
148    await shot('08-search-off');
149    check((await page.getByRole('switch', { name: 'Search the web' }).getAttribute('aria-checked')) === 'false', 'the switch is off');
150    await newChat();
151    await send('Use the web_search tool to search for the word Strata. If you have no such tool, say exactly: NO SEARCH TOOL.');
152    await started().catch(() => {});
153    await approveUntilIdle('08');
154    await idle();
155    await shot('09-no-search');
156    const body = await chatText();
157    check(!/Searched the web|Searching the web/.test(body), 'no search card: the assistant has no search tool now');
158    await openTools();
159    await page.getByRole('switch', { name: 'Search the web' }).click();
160    await page.waitForTimeout(500);
161  }
162
163  // 5. the browser
164  if (!SKIP.has('browser')) {
165    await openTools();
166    await page.getByRole('heading', { name: 'Web browser' }).evaluate(el => el.scrollIntoView({ block: 'start' }));
167    const sw = page.getByRole('switch', { name: 'Use a web browser' });
168    check(await sw.isEnabled(), 'the browser switch is available (Chrome or Edge and the tool server were found)');
169    if ((await sw.getAttribute('aria-checked')) !== 'true') await sw.click();
170    await page.waitForTimeout(800);
171    await shot('10-browser-on');
172    await newChat();
173    await send('Open https://example.com in the browser and tell me the big heading on that page.');
174    await started().catch(() => {});
175    const r = await approveUntilIdle('11');
176    await idle();
177    await shot('11-browser');
178    const body = await chatText();
179    check(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'})`);
180    check(/Opened a page in the browser/.test(body), 'a plain "Opened a page in the browser" card appeared');
181    check(/Example Domain/.test(body), 'the answer names the page heading');
182    // leave the settings as they were found
183    await openTools();
184    await page.getByRole('heading', { name: 'Web browser' }).evaluate(el => el.scrollIntoView({ block: 'start' }));
185    if ((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) {
188  console.log('ERROR', e.message);
189  await shot('zz-error').catch(() => {});
190  failed = true;
191} finally {
192  const text = problems.filter(p => !/Failed to load resource|ERR_|favicon/.test(p));
193  console.log(`page problems: ${text.length}`); text.slice(0, 8).forEach(p => console.log('  ' + p));
194  await browser.close();
195}
196process.exit(failed ? 1 : 0);