okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ea2d565e6
main
1/** 2* Search through a service the person has their own key for: Exa or Perplexity (the Harness ships both as separate packages the app's 3* bundle leaves out, so these are the same two small calls written here; DeepSeek's provider is in the bundle and is used as it is). 4* Shapes follow the Harness's providers (packages/web/web-search-exa and web-search-perplexity at the pinned commit). 5*/ 6import { SearchUnavailable } from './web-search-ddg.mjs' ; 7 8const abortError = e => e ?. name === 'AbortError' ; 9 10async function call ( service , url , options , signal ) { 11let res ; 12try { res = await fetch ( url , { ...options , redirect :'error' , ...( signal ?{ signal} :{}) }); } 13catch ( error ) { 14if ( abortError ( error )) throw new SearchUnavailable ( 'The search was cancelled.' , 'WEB_ABORTED' , { cause :error }); 15throw new SearchUnavailable ( `The search could not reach ${ service } . Check the internet connection and try again.` , 'WEB_PROVIDER_ERROR' , { cause :error }); 16} 17if ( ! res . ok ) { 18let detail = '' ; 19try { const body = await res . json (); detail = typeof body . error === 'string' ?body . error :body . error ?. message ?? body . message ?? '' ; } catch { /* not JSON */ } 20if ( res . status === 401 || res . status === 403 ) throw new SearchUnavailable ( ` ${ service } did not accept the key. Check it in Settings, Tools.` , 'WEB_PROVIDER_ERROR' ); 21if ( res . status === 429 ) throw new SearchUnavailable ( ` ${ service } says there have been too many searches. Try again in a minute.` , 'WEB_RATE_LIMITED' ); 22throw new SearchUnavailable ( ` ${ service } could not search (error ${ res . status } ). ${ detail ? ' ' + String ( detail ). slice ( 0 , 200 ) : '' } ` , 'WEB_PROVIDER_ERROR' ); 23} 24try { return await res . json (); } 25catch ( error ) { throw new SearchUnavailable ( ` ${ service } sent an answer that could not be read.` , 'WEB_PROVIDER_ERROR' , { cause :error }); } 26} 27 28export function createExaSearch ({ key}) { 29return { 30id :'exa' , 31available :() => typeof key === 'function' ?!! key () :!! key , 32async search ( request , signal ) { 33const apiKey = typeof key === 'function' ?key () :key ; 34const body = await call ( 'Exa' , 'https://api.exa.ai/search' , { 35method :'POST' , 36headers :{ authorization :`Bearer ${ apiKey } ` , 'content-type' :'application/json' , accept :'application/json' }, 37body :JSON . stringify ({ query :request . query , type :'auto' , contents :{ highlights :{ highlightsPerUrl :1 } }, ...( request . maxResults ?{ numResults :request . maxResults } :{}) }), 38}, signal ); 39const sources = ( body . results ?? []). flatMap ( r => { 40const snippet = ( r . highlights ?? []). find ( h => String ( h ). trim ()); 41return snippet === undefined ?[] :[{ url :r . url , ...( r . title ?{ title :r . title } :{}), snippet, ...( r . publishedDate ?{ publishedAt :r . publishedDate } :{}) }]; 42}); 43return { sources, truncated :false }; 44}, 45}; 46} 47 48export function createPerplexitySearch ({ key}) { 49return { 50id :'perplexity' , 51available :() => typeof key === 'function' ?!! key () :!! key , 52async search ( request , signal ) { 53const apiKey = typeof key === 'function' ?key () :key ; 54const body = await call ( 'Perplexity' , 'https://api.perplexity.ai/chat/completions' , { 55method :'POST' , 56headers :{ authorization :`Bearer ${ apiKey } ` , 'content-type' :'application/json' , accept :'application/json' }, 57body :JSON . stringify ({ model :'sonar' , max_tokens :1024 , messages :[{ role :'user' , content :request . query }] }), 58}, signal ); 59const content = body . choices ?.[ 0 ]?. message ?. content ; 60const sources = body . search_results !== undefined 61 ?body . search_results . map ( r => ({ url :r . url , ...( r . title ?{ title :r . title } :{}), ...( r . snippet ?{ snippet :r . snippet } :{}), ...( r . date ?{ publishedAt :r . date } :{}) })) 62 :( body . citations ?? []). map ( url => ({ url})); 63return { ...( content ?{ content} :{}), sources, truncated :false }; 64}, 65}; 66}