package wire import ( "errors" "unicode/utf8" ) // Lookups (spec 2026-11-26-assistant-abilities §4.2): the instance asks // the person's runner for two isolated model steps, the query and the // answer, over a live-only channel. Neither frame is durable: nothing is // written to runner_commands, acked or resent; an answer is accepted only // for the lookup's current attempt and nonce. const ( // FrameLookupStep is instance to runner, live only. FrameLookupStep = "lookup_step" // FrameLookupResult is runner to instance, live only. FrameLookupResult = "lookup_result" ) // The two stages. const ( LookupStageQuery = "query" LookupStageAnswer = "answer" ) // Bounds on a lookup's frames and results. const ( MaxLookupPack = 4 << 10 MaxLookupStepBytes = 56 << 10 MaxLookupAnswer = 1200 MaxLookupQueries = 2 MaxLookupQuery = 200 MaxLookupSources = 5 MaxLookupsPerReply = 2 ) // LookupPack is what the query writer sees (§4.2 step 1): the person's // question now, the lookup questions it follows on from (newest first), // their zone's city and today's date there. type LookupPack struct { Question string `json:"question"` Earlier []string `json:"earlier,omitempty"` City string `json:"city,omitempty"` Date string `json:"date"` } // SearchResult is one search result as the answer step reads it. type SearchResult struct { Title string `json:"title"` URL string `json:"url"` Snippet string `json:"snippet"` Date string `json:"date"` } // LookupPage is one page read, as readable text. type LookupPage struct { URL string `json:"url"` Title string `json:"title"` Text string `json:"text"` } // LookupStep asks the runner for one stage: the query (from the pack) or // the answer (from the pack, the results and the pages). The prompts are // the runner's own constants (lookup_prompts.go), chosen by Stage. Person // is whose model connection runs it. type LookupStep struct { Person string `json:"person"` Lookup string `json:"lookup"` Attempt int `json:"attempt"` Nonce string `json:"nonce"` Stage string `json:"stage"` Pack LookupPack `json:"pack"` Results []SearchResult `json:"results,omitempty"` Pages []LookupPage `json:"pages,omitempty"` Truncated bool `json:"truncated,omitempty"` } // LookupResult is the runner's answer to one stage: the queries, or the // answer and the indexes of the results it used, or a failure code. type LookupResult struct { Lookup string `json:"lookup"` Attempt int `json:"attempt"` Nonce string `json:"nonce"` Stage string `json:"stage"` Queries []string `json:"queries,omitempty"` Answer string `json:"answer,omitempty"` Used []int `json:"used,omitempty"` Code string `json:"code,omitempty"` } // AssistantLookup is one lookup shown under a reply: its answer and the // pages it came from (spec §4.6). type AssistantLookup struct { Answer string `json:"answer"` Sources []LookupSource `json:"sources"` } // LookupSource is a page a lookup's answer came from. type LookupSource struct { URL string `json:"url"` Title string `json:"title"` } // The lookup codes: model-facing results and step failures. const ( CodeLookupShown = "shown" CodeLookupFailed = "lookup_failed" CodeNothingFound = "nothing_found" CodeLookupLimit = "lookup_limit" CodeLookupUnsupported = "lookup_unsupported" ) var errLookupResult = errors.New("wire: lookup result out of bounds") // ValidQueryResult checks a query step's result: 1 or 2 queries, each 1 // to 200 characters, and nothing else. func ValidQueryResult(r LookupResult) error { if r.Code != "" || r.Answer != "" || r.Used != nil || len(r.Queries) == 0 || len(r.Queries) > MaxLookupQueries { return errLookupResult } for _, q := range r.Queries { if n := utf8.RuneCountInString(q); n == 0 || n > MaxLookupQuery || !utf8.ValidString(q) { return errLookupResult } } return nil } // ValidAnswerResult checks an answer step's result: an answer of 1 to // 1,200 characters, the results it used among the results given, and no // queries. func ValidAnswerResult(r LookupResult, results int) error { if r.Code != "" || r.Queries != nil || !utf8.ValidString(r.Answer) { return errLookupResult } if n := utf8.RuneCountInString(r.Answer); n == 0 || n > MaxLookupAnswer { return errLookupResult } seen := map[int]bool{} for _, i := range r.Used { if i < 0 || i >= results || seen[i] { return errLookupResult } seen[i] = true } return nil }