import{Command,UiSchema}from"@effect/ui"import{Html}from"@effect/ui/html"import*as stylex from"@stylexjs/stylex"import{Effect,Schema}from"effect"import{Search,SearchAnswer,typeSearchResult}from"./ui/uicontext/components/kit/Search.ts"import{ sx }from"./ui/uicontext/components/Sx.ts"import{ color }from"./ui/uicontext/components/tokens.stylex.ts"// A pretend workspace: results arrive after 300 ms (standing in for the app's search request),// sources filter them, recent searches are kept, and the AI answer streams in with its sources.const corpus:ReadonlyArray<SearchResult>=[{id:"p1",title:"Onboarding checklist",href:"#p1",source:"Pages",path:"Handbook › People",snippet:"Every new hire gets a laptop, a buddy, and an onboarding plan for the first two weeks.",meta:"Edited 2 days ago",},{id:"p2",title:"Security review process",href:"#p2",source:"Pages",path:"Engineering › Specs",snippet:"Changes to sign-in, payments or permissions need a security review before release.",meta:"Edited last week",},{id:"i1",title:"Fix the sign-in redirect loop",href:"#i1",source:"Issues",path:"ENG-12",snippet:"After sign-in the redirect returns to the sign-in page when the session cookie is missing.",meta:"In Progress",},{id:"i2",title:"Onboarding emails go out twice",href:"#i2",source:"Issues",path:"GROW-88",snippet:"The onboarding sequence sends the welcome email twice for invited members.",meta:"Todo",},{id:"s1",title:"#eng: sign-in incident follow-up",href:"#s1",source:"Messages",snippet:"Ana: the sign-in fix needs a security review before Friday's release.",meta:"Yesterday",},]const sourceIds =newMap([["Pages","pages"],["Issues","issues"],["Messages","messages"],])constMessage=UiSchema.TaggedEnum({RequestedSearch:{query:Schema.String,sources:Schema.Array(Schema.String)},CompletedSearch:{query:Schema.String},OpenedResult:{id:Schema.String},ClickedAsk:{query:Schema.String},CompletedAnswer:{},ClickedStop:{},})constWait=Command.make("WaitForSearch",{args:{query:Schema.String}},({ query })=>Effect.sleep("300 millis").pipe(Effect.as(Message.CompletedSearch({ query }))),)constThink=Command.make("WaitForAnswer",{},()=>Effect.sleep("1400 millis").pipe(Effect.as(Message.CompletedAnswer())),)const matches =(query: string,sources:ReadonlyArray<string>)=>{const words = query.toLowerCase().split(/\s+/u).filter((w)=> w !=="")return corpus.filter((r)=> words.length>0&& words.every((w)=>`${r.title} ${r.snippet ?? ""}`.toLowerCase().includes(w))&&(sources.length===0|| sources.includes(sourceIds.get(r.source??"")??"")),)}const citations =[{id:"p2",label:"1",title:"Security review process",source:"Pages",href:"#p2"},{id:"s1",label:"2",title:"#eng: sign-in incident follow-up",source:"Messages",href:"#s1"},]const fullAnswer ="Sign-in changes need a **security review** before release [1]. The fix for the redirect loop is waiting on that review, which Ana asked for before Friday [2]."const styles = stylex.create({box:{display:"grid",gap:12,inlineSize:"min(100%, 680px)"},opened:{margin:0,minBlockSize:"1.4em",fontSize:13,color: color.muted},})exportconstWorkspaceSearch=Html.component({name:"WorkspaceSearch",model:Schema.Struct({query:Schema.String,sources:Schema.Array(Schema.String),status:Schema.Literals(["idle","loading","ready"]),answer:Schema.NullOr(SearchAnswer),opened:Schema.NullOr(Schema.String),recent:Schema.Array(Schema.String),}),message:Message,}).make({init:()=>({query:"",sources:[],status:"idle",answer:null,opened:null,recent:["sign-in","onboarding emails"]}),update:{RequestedSearch:($,{ query, sources })=> $.evolve({ query, sources,status: query.trim()===""?"idle":"loading",answer:null}).run(Wait.make({ query }),),// a search that finished becomes the latest recent oneCompletedSearch:($,{ query })=> query === $.model.query&& query.trim()!==""? $.evolve({status:"ready",recent:(recent)=>[query.trim(),...recent.filter((r)=> r !== query.trim())].slice(0,5)}): query === $.model.query? $.evolve({status:"ready"}): $.model,OpenedResult:($,{ id })=> $.evolve({opened: id }),ClickedAsk:($)=> $.evolve({answer:{status:"streaming",text: fullAnswer.slice(0,60),citations:[]}}).run(Think.make({})),CompletedAnswer:($)=> $.model.answer?.status==="streaming"? $.evolve({answer:{status:"succeeded",text: fullAnswer, citations }}): $.model,ClickedStop:($)=> $.model.answer===null? $.model: $.evolve({answer:{...$.model.answer,status:"succeeded",notice:"Stopped before the end."}}),},view:(h, model)=>{const results =matches(model.query, model.sources)const count =(source: string)=>matches(model.query,[source]).lengthreturn h.div.attrs(sx(styles.box)).children(Search(h,{props:{query: model.query,results: model.status==="loading"?[]: results,status: model.status,sources:[{id:"pages",label:"Pages",count:count("pages")},{id:"issues",label:"Issues",count:count("issues")},{id:"messages",label:"Messages",count:count("messages")},],selected: model.sources,answer: model.answer,can:["ask"],hint:"Search pages, issues and messages.",recent: model.recent,placeholder:"Search the workspace…",agentId:"search-demo",},onEmit:{RequestedSearch:({ query, sources })=>Message.RequestedSearch({ query, sources }),RequestedOpen:({ id })=>Message.OpenedResult({ id }),RequestedAsk:({ query })=>Message.ClickedAsk({ query }),RequestedStop:()=>Message.ClickedStop(),},}), h.p.attrs({role:"status",...sx(styles.opened)}).children( model.opened===null?"":`Opened “${corpus.find((r) => r.id === model.opened)?.title ?? model.opened}”`,),)},})
constMessage=UiSchema.TaggedEnum({RequestedSearch:{query:Schema.String},RequestedOpenResult:{id:Schema.String},})exportconstWorkspaceSearch=(h:Html.H<typeofMessage.Type>,query: string,results:ReadonlyArray<SearchResult>)=>Search(h,{props:{ query, results,status:"ready",recent:["sign-in","onboarding"]},onEmit:{RequestedSearch:({ query })=>Message.RequestedSearch({ query }),RequestedOpen:({ id })=>Message.RequestedOpenResult({ id }),RequestedAsk:()=>undefined,RequestedStop:()=>undefined,},})
API reference
Props
Search props
Name
Type
Default
Description
queryrequired
string
—
The query the results are for
resultsrequired
SearchResult[]
—
The results for query, best first
statusrequired
"idle" | "loading" | "ready"
—
Whether the results are being fetched (loading shows placeholders) or are in (ready)
total
number
—
Every match, when there are more than shown
sources
SearchSource[]
—
Where results can come from, shown as filters: Issues, Docs, People
selected
string[]
—
The sources filtered to; none means all
answer
SearchAnswer | null
—
An AI answer to the query, streamed in, with its citations
can
Can(["ask"])
—
What the asker may do: "ask" asks for an AI answer (plan, role and setting allowing)
label
string
"Search"
The search field's accessible name; "Search" by default
placeholder
string
—
The field's placeholder; "Search…" by default
hint
string
—
Shown before anything is typed
recent
string[]
—
The person's recent searches, latest first: listed before anything is typed, and picking one searches it again. The app keeps them, from RequestedSearch.
locale
string
—
The locale counts and dates are written in, such as "en-US"; the browser's by default
agentId
string
—
This search's id for agents, unique on the page
scopes
SearchScope[]
—
Tokens that narrow the search: from:, in:, has:
grouped
boolean
—
Give each run of results from one source a heading (send them sorted by source)
Events
Search events
Event
Payload
Description
RequestedSearch
{ query: string; sources: string[] }
The query or the source filter changed: the app fetches results
RequestedOpen
{ id: string }
A result was opened, by Enter or a click: the app goes there
RequestedAsk
{ query: string }
The person asked for an AI answer to the query
RequestedStop
{}
The person stopped the answer being written
Agent actions
What agents can do with Search
Action
Description
SubmittedQuery
Search for a query; returns once its results are in
ToggledSource
Filter to a source by id, or "all"
OpenedResult
Open a result by its id
ClickedAsk
Ask for an AI answer to the current query, with sources
ClickedStop
Stop the answer being written
Accessibility
The field is a combobox inside a search landmark: Up and Down move through the results and Enter opens one, while focus stays in the field.
The source filters are toggle buttons (aria-pressed), the result count is a status, and the AI answer is a live region that says when it's still being written.
Agents search, filter, open a result, and ask for or stop an answer.