okami.mom/web-extract-go
git clone https://git.nkp.pet/okami.mom/web-extract-go
5a90548
main
1import { useEffect , useRef , useState , type FormEvent } from "react" 2import { Play } from "lucide-react" 3import { Button } from "@/components/ui/button" 4import { Input } from "@/components/ui/input" 5import { Label } from "@/components/ui/label" 6import { Textarea } from "@/components/ui/textarea" 7import { Tabs , TabsContent , TabsList , TabsTrigger } from "@/components/ui/tabs" 8import { Empty , JSONView , Notice , SourceLink , Status } from "@/components/common" 9import { TraceView } from "@/components/trace" 10import { buildQuery , type QueryFields } from "@/lib/query" 11import { message , number } from "@/lib/api" 12import type { Endpoint , QueryResponse , Result } from "@/lib/types" 13 14function ResultItem ({ result} :{ result :Result }) { 15return < article className = "result-item" > 16< h3 >< SourceLink url = { result . url }>{ result . title || result . url }</ SourceLink ></ h3 > 17< p className = "muted break-all text-xs mt-1" >{ result . url }</ p > 18{ result . summary && < p className = "source-text" >{ result . summary }</ p >} 19{ !! result . highlights ?. length && < details className = "mt-4" open >< summary > selected highlights ( { result . highlights . length } ) </ summary > 20{ result . highlights . map (( text , i ) => < div className = "highlight" key = { i }> 21{ result . highlightScores ?.[ i ] != null && < span className = "muted text-xs" > evidence score {( result . highlightScores [ i ] * 100 ). toFixed ( 1 )} % </ span >} 22< p className = "source-text mt-1" >{ text }</ p > 23</ div >)} 24</ details >} 25{ result . text != null && < details className = "mt-4" >< summary > source text ( { number ( result . text . length )} characters) </ summary >< pre className = "source-text" >{ result . text }</ pre ></ details >} 26</ article > 27} 28 29export function Playground ({ onRun} :{ onRun :() => void }) { 30const [ fields , setFields ] = useState < QueryFields >({ endpoint :"search" , query :"" , urls :"" , domains :"" , count :3 , highlights :true , summary :true , text :false , characters :4000 }) 31const [ custom , setCustom ] = useState < string | null >( null ) 32const [ busy , setBusy ] = useState ( false ) 33const [ error , setError ] = useState ( "" ) 34const [ run , setRun ] = useState <{ body :QueryResponse ; request :unknown ; status :number ; ms :number ; endpoint :Endpoint } | null >( null ) 35const abort = useRef < AbortController | null >( null ) 36useEffect (() => () => abort . current ?. abort (), []) 37const change = < K extends keyof QueryFields >( key :K , value :QueryFields [ K ]) => setFields ( f => ({ ...f , [ key ] :value })) 38async function execute ( event :FormEvent ) { 39event . preventDefault () 40if ( busy ) return 41let request :unknown 42try { request = custom === null ?buildQuery ( fields ) :JSON . parse ( custom ) } catch { setError ( "request json is invalid. correct it and try again." ); return } 43setBusy ( true ); setError ( "" ); setRun ( null ) 44const started = performance . now () 45const controller = new AbortController (); abort . current = controller 46try { 47const response = await fetch ( `/admin/api/ ${ fields . endpoint } ` , { method :"POST" , credentials :"same-origin" , headers :{ "Content-Type" :"application/json" }, body :JSON . stringify ( request ), signal :controller . signal }) 48const body = await response . json () as QueryResponse 49if ( response . status === 401 ) { window . dispatchEvent ( new Event ( "session-expired" )); return } 50setRun ({ body, request, status :response . status , ms :Math . round ( performance . now () - started ), endpoint :fields . endpoint }) 51onRun () 52} catch ( error ) { setError ( controller . signal . aborted ?"request canceled. any completed model calls remain in the activity log." :message ( error )) } 53finally { setBusy ( false ); abort . current = null } 54} 55return < div className = "playground-grid" > 56< form className = "query-form" onSubmit = { execute } onKeyDown = { event => { if ( event . key === "Enter" && ( event . metaKey || event . ctrlKey )) { event . preventDefault (); event . currentTarget . requestSubmit () } }}> 57< div >< h2 > query playground </ h2 >< p className = "muted" > real requests, real provider usage. results are logged as < code > admin-dashboard </ code > . </ p ></ div > 58< fieldset disabled = { busy } className = "space-y-5" > 59< div className = "field" >< Label htmlFor = "endpoint" > endpoint </ Label >< select id = "endpoint" value = { fields . endpoint } onChange = { event => { change ( "endpoint" , event . target . value as Endpoint ); setCustom ( null ) }}>< option value = "search" > search </ option >< option value = "contents" > contents </ option >< option value = "answer" > answer </ option ></ select ></ div > 60< fieldset disabled = { custom !== null } className = "space-y-5" > 61{ fields . endpoint === "contents" && < div className = "field" >< Label htmlFor = "urls" > urls · one per line </ Label >< Textarea id = "urls" rows = { 3 } required = { custom === null } value = { fields . urls } onChange = { e => change ( "urls" , e . target . value )} placeholder = "https://sqlite.org/wal.html" /></ div >} 62< div className = "field" >< Label htmlFor = "query" >{ fields . endpoint === "contents" ?"focus query (optional)" :"query" }</ Label >< Textarea id = "query" rows = { 4 } maxLength = { 4000 } required = { fields . endpoint !== "contents" && custom === null } value = { fields . query } onChange = { e => change ( "query" , e . target . value )} placeholder = "can sqlite wal have multiple writers at once?" /></ div > 63{ fields . endpoint === "search" && <> 64< div className = "field" >< Label htmlFor = "domains" > include domains (optional) </ Label >< Input id = "domains" value = { fields . domains } onChange = { e => change ( "domains" , e . target . value )} placeholder = "sqlite.org, go.dev" /></ div > 65< div className = "field" >< Label htmlFor = "count" > number of results </ Label >< Input id = "count" type = "number" min = { 1 } max = { 20 } required value = { fields . count } onChange = { e => change ( "count" , Number ( e . target . value ))} /></ div > 66</>} 67< div className = "field" >< span className = "text-sm font-medium" > return </ span >< div className = "flex flex-wrap gap-4" > 68{ fields . endpoint !== "answer" && <> 69< label className = "inline-control" >< input type = "checkbox" checked = { fields . highlights } onChange = { e => change ( "highlights" , e . target . checked )} /> highlights </ label > 70< label className = "inline-control" >< input type = "checkbox" checked = { fields . summary } onChange = { e => change ( "summary" , e . target . checked )} /> summary </ label > 71</>} 72< label className = "inline-control" >< input type = "checkbox" checked = { fields . text } onChange = { e => change ( "text" , e . target . checked )} />{ fields . endpoint === "answer" ?"citation evidence" :"source text" }</ label > 73</ div ></ div > 74{ fields . endpoint !== "answer" && < div className = "field" >< Label htmlFor = "characters" > maximum text / highlight characters </ Label >< Input id = "characters" type = "number" min = { 1 } max = { 10000 } required value = { fields . characters } onChange = { e => change ( "characters" , Number ( e . target . value ))} /></ div >} 75</ fieldset > 76< details >< summary > request json </ summary >< div className = "space-y-3 mt-3" >< label className = "inline-control" >< input type = "checkbox" checked = { custom !== null } onChange = { e => setCustom ( e . target . checked ?JSON . stringify ( buildQuery ( fields ), null , 2 ) :null )} /> edit json instead of form </ label > 77{ custom !== null ?< Textarea aria-label = "request json" className = "font-mono text-xs min-h-64" spellCheck = { false } value = { custom } onChange = { e => setCustom ( e . target . value )} /> :< JSONView value = { buildQuery ( fields )} />} 78</ div ></ details > 79</ fieldset > 80{ error && < Notice >{ error }</ Notice >} 81< div className = "flex items-center gap-3" >< Button type = "submit" disabled = { busy }>< Play aria-hidden = "true" />{ busy ?"running…" :"run query" }</ Button > 82{ busy ?< Button type = "button" variant = "outline" onClick = {() => abort . current ?. abort ()}> cancel </ Button > :< span className = "muted text-xs" > ⌘ / ctrl + enter </ span >} 83</ div > 84</ form > 85< section className = "query-output" aria-label = "query output" aria-busy = { busy }> 86< div className = "section-toolbar" >< h2 > response </ h2 >{ run && < div className = "flex flex-wrap items-center gap-2" >< Status error = { run . status >= 400 }> http { run . status }</ Status >< span className = "muted numeric" >{( run . ms / 1000 ). toFixed ( 2 )} s </ span ></ div >}</ div > 87{ busy && < div className = "empty" role = "status" > running { fields . endpoint } … < p className = "muted mt-2" > fetching pages and judging evidence can take up to 90 seconds. </ p ></ div >} 88{ ! run && ! busy && < Empty > run a query to inspect results, selected passages, and raw model calls. </ Empty >} 89{ run && <> 90< p className = "muted font-mono text-xs my-4 break-all" >{ run . endpoint } · { run . body . requestId || "no request id" }</ p > 91< Tabs key = { run . body . requestId || run . ms } defaultValue = "results" >< TabsList aria-label = "query response" >< TabsTrigger value = "results" > results </ TabsTrigger >< TabsTrigger value = "trace" disabled = { ! run . body . requestId }> trace </ TabsTrigger >< TabsTrigger value = "json" > json </ TabsTrigger ></ TabsList > 92< TabsContent value = "results" > 93{ run . body . error && < Notice >{ run . body . tag ?` ${ run . body . tag } : ` :"" }{ run . body . error }</ Notice >} 94{ run . body . answer && < p className = "source-text text-base" >{ run . body . answer }</ p >} 95{ run . body . statuses && < div className = "url-statuses" >{ run . body . statuses . map (( status , i ) => < div key = { i }>< Status error = { !! status . error }>{ status . error ?. tag || status . status }</ Status >< span className = "break-all" >{ status . id }</ span >{ status . source && < span className = "muted" >{ status . source }</ span >}</ div >)}</ div >} 96{( run . body . results || run . body . citations || []). map (( result , i ) => < ResultItem key = { ` ${ result . id } - ${ i } ` } result = { result } />)} 97{ ! run . body . error && ! run . body . answer && ! run . body . results ?. length && < Empty > the request completed without results. try a broader query or inspect the raw response. </ Empty >} 98</ TabsContent > 99< TabsContent value = "trace" >{ run . body . requestId && < TraceView id = { run . body . requestId } />}</ TabsContent > 100< TabsContent value = "json" >< h3 className = "mt-5" > response </ h3 >< JSONView value = { run . body } />< h3 className = "mt-5" > sent request </ h3 >< JSONView value = { run . request } /></ TabsContent > 101</ Tabs > 102</>} 103</ section > 104</ div > 105}