okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
74e25e165
main
1/** With no internet the assistant gets plain words from the web tools, not "fetch failed", and can go on without them. */ 2import test from 'node:test' ; 3import assert from 'node:assert/strict' ; 4import { mkdtempSync , mkdirSync , rmSync } from 'node:fs' ; 5import { tmpdir } from 'node:os' ; 6import { join } from 'node:path' ; 7import { createDuckDuckGoSearch , UNREACHABLE_MESSAGE } from '../server/web-search-ddg.mjs' ; 8import { plainToolError , withPlain } from '../server/agent/events.mjs' ; 9import { startScriptedWorker } from './helpers/scripted-worker.mjs' ; 10import { createAgentBridge } from '../server/agent/agent-bridge.mjs' ; 11import { resolveHarness } from '../server/agent/harness-runtime.mjs' ; 12 13const HARNESS = process . env . STRATA_HARNESS_DIR ; 14const skip = ! HARNESS || ! resolveHarness ({ env :process . env }). ok ?'STRATA_HARNESS_DIR is not set to a built Harness' :false ; 15 16test ( 'search with no internet: one readable sentence that tells the model to go on without it' , async () => { 17let t = 5_000_000 ; // a pretend clock that moves when the search waits, as in web-search.test.mjs 18const offline = createDuckDuckGoSearch ({ fetch :async () => { throw new TypeError ( 'fetch failed' ); }, now :() => t , sleep :async ms => { t += ms ; }, random :() => 0.5 }); 19const error = await offline . search ({ query :'heat pumps' }). catch ( e => e ); 20assert . equal ( error . message , UNREACHABLE_MESSAGE ); 21assert . match ( error . message , / could not reach the internet / ); 22assert . match ( error . message , / go on with what you know / ); 23assert . doesNotMatch ( error . message , / fetch failed | ENOTFOUND | TypeError / ); 24}); 25 26test ( 'opening a page with no internet: the card says so in plain words and keeps the technical text behind Details' , () => { 27for ( const raw of [ 'Error: web fetch failed: fetch failed' , 'Error: getaddrinfo ENOTFOUND example.com' , 'Error: connect ECONNREFUSED 93.184.216.34:443' ]) { 28const text = withPlain ( raw ); 29assert . match ( text , / ^That web page could not be opened\. Check the address and your internet connection\. / , raw ); 30assert . match ( text , / Details: / ); 31} 32assert . equal ( plainToolError ( UNREACHABLE_MESSAGE ), null , 'the search sentence is already plain and is left as it is' ); 33}); 34 35test ( 'real Harness: the model gets the plain sentence when the search cannot reach the internet' , { skip}, async t => { 36const worker = await startScriptedWorker ({ chunkDelayMs :5 }); 37const dir = mkdtempSync ( join ( tmpdir (), 'strata-weboff-' )); 38const ws = join ( dir , 'ws' ); mkdirSync ( ws , { recursive :true }); 39const bridge = createAgentBridge ({ dataDir :dir , localInstances :() => [{ id :'i' , label :'S' , baseUrl :worker . url , modelId :'scripted-model' , context :32768 }], connections :() => [], env :process . env , startTimeoutMs :90_000 , searchMock :'offline' }); 40t . after ( async () => { await bridge . close (); await worker . close (); try { rmSync ( dir , { recursive :true , force :true , maxRetries :3 }); } catch { /* best effort */ } }); 41const frames = []; 42await bridge . runTurn ({ chatId :'off1' , message :'SCRIPT:call web_search {"queries":["heat pumps"]}' , workspacePath :ws , mode :'bypass' , onFrame :f => frames . push ( f ) }); 43const card = frames . filter ( f => f . type === 'agent-tool' && f . tool . name === 'web_search' ). at ( - 1 ). tool ; 44assert . equal ( card . state , 'error' ); 45assert . match ( card . result , / could not reach the internet / ); 46assert . doesNotMatch ( card . result , / fetch failed / ); 47assert . equal ( frames . at ( - 1 ). type , 'done' , 'the turn ends normally' ); 48});