| 1 | package wire |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "unicode/utf8" |
| 6 | ) |
| 7 | |
| 8 | // Lookups (spec 2026-11-26-assistant-abilities §4.2): the instance asks |
| 9 | // the person's runner for two isolated model steps, the query and the |
| 10 | // answer, over a live-only channel. Neither frame is durable: nothing is |
| 11 | // written to runner_commands, acked or resent; an answer is accepted only |
| 12 | // for the lookup's current attempt and nonce. |
| 13 | const ( |
| 14 | // FrameLookupStep is instance to runner, live only. |
| 15 | FrameLookupStep = "lookup_step" |
| 16 | // FrameLookupResult is runner to instance, live only. |
| 17 | FrameLookupResult = "lookup_result" |
| 18 | ) |
| 19 | |
| 20 | // The two stages. |
| 21 | const ( |
| 22 | LookupStageQuery = "query" |
| 23 | LookupStageAnswer = "answer" |
| 24 | ) |
| 25 | |
| 26 | // Bounds on a lookup's frames and results. |
| 27 | const ( |
| 28 | MaxLookupPack = 4 << 10 |
| 29 | MaxLookupStepBytes = 56 << 10 |
| 30 | MaxLookupAnswer = 1200 |
| 31 | MaxLookupQueries = 2 |
| 32 | MaxLookupQuery = 200 |
| 33 | MaxLookupSources = 5 |
| 34 | MaxLookupsPerReply = 2 |
| 35 | ) |
| 36 | |
| 37 | // LookupPack is what the query writer sees (§4.2 step 1): the person's |
| 38 | // question now, the lookup questions it follows on from (newest first), |
| 39 | // their zone's city and today's date there. |
| 40 | type LookupPack struct { |
| 41 | Question string `json:"question"` |
| 42 | Earlier []string `json:"earlier,omitempty"` |
| 43 | City string `json:"city,omitempty"` |
| 44 | Date string `json:"date"` |
| 45 | } |
| 46 | |
| 47 | // SearchResult is one search result as the answer step reads it. |
| 48 | type SearchResult struct { |
| 49 | Title string `json:"title"` |
| 50 | URL string `json:"url"` |
| 51 | Snippet string `json:"snippet"` |
| 52 | Date string `json:"date"` |
| 53 | } |
| 54 | |
| 55 | // LookupPage is one page read, as readable text. |
| 56 | type LookupPage struct { |
| 57 | URL string `json:"url"` |
| 58 | Title string `json:"title"` |
| 59 | Text string `json:"text"` |
| 60 | } |
| 61 | |
| 62 | // LookupStep asks the runner for one stage: the query (from the pack) or |
| 63 | // the answer (from the pack, the results and the pages). The prompts are |
| 64 | // the runner's own constants (lookup_prompts.go), chosen by Stage. Person |
| 65 | // is whose model connection runs it. |
| 66 | type LookupStep struct { |
| 67 | Person string `json:"person"` |
| 68 | Lookup string `json:"lookup"` |
| 69 | Attempt int `json:"attempt"` |
| 70 | Nonce string `json:"nonce"` |
| 71 | Stage string `json:"stage"` |
| 72 | Pack LookupPack `json:"pack"` |
| 73 | Results []SearchResult `json:"results,omitempty"` |
| 74 | Pages []LookupPage `json:"pages,omitempty"` |
| 75 | Truncated bool `json:"truncated,omitempty"` |
| 76 | } |
| 77 | |
| 78 | // LookupResult is the runner's answer to one stage: the queries, or the |
| 79 | // answer and the indexes of the results it used, or a failure code. |
| 80 | type LookupResult struct { |
| 81 | Lookup string `json:"lookup"` |
| 82 | Attempt int `json:"attempt"` |
| 83 | Nonce string `json:"nonce"` |
| 84 | Stage string `json:"stage"` |
| 85 | Queries []string `json:"queries,omitempty"` |
| 86 | Answer string `json:"answer,omitempty"` |
| 87 | Used []int `json:"used,omitempty"` |
| 88 | Code string `json:"code,omitempty"` |
| 89 | } |
| 90 | |
| 91 | // AssistantLookup is one lookup shown under a reply: its answer and the |
| 92 | // pages it came from (spec §4.6). |
| 93 | type AssistantLookup struct { |
| 94 | Answer string `json:"answer"` |
| 95 | Sources []LookupSource `json:"sources"` |
| 96 | } |
| 97 | |
| 98 | // LookupSource is a page a lookup's answer came from. |
| 99 | type LookupSource struct { |
| 100 | URL string `json:"url"` |
| 101 | Title string `json:"title"` |
| 102 | } |
| 103 | |
| 104 | // The lookup codes: model-facing results and step failures. |
| 105 | const ( |
| 106 | CodeLookupShown = "shown" |
| 107 | CodeLookupFailed = "lookup_failed" |
| 108 | CodeNothingFound = "nothing_found" |
| 109 | CodeLookupLimit = "lookup_limit" |
| 110 | CodeLookupUnsupported = "lookup_unsupported" |
| 111 | ) |
| 112 | |
| 113 | var errLookupResult = errors.New("wire: lookup result out of bounds") |
| 114 | |
| 115 | // ValidQueryResult checks a query step's result: 1 or 2 queries, each 1 |
| 116 | // to 200 characters, and nothing else. |
| 117 | func ValidQueryResult(r LookupResult) error { |
| 118 | if r.Code != "" || r.Answer != "" || r.Used != nil || len(r.Queries) == 0 || len(r.Queries) > MaxLookupQueries { |
| 119 | return errLookupResult |
| 120 | } |
| 121 | for _, q := range r.Queries { |
| 122 | if n := utf8.RuneCountInString(q); n == 0 || n > MaxLookupQuery || !utf8.ValidString(q) { |
| 123 | return errLookupResult |
| 124 | } |
| 125 | } |
| 126 | return nil |
| 127 | } |
| 128 | |
| 129 | // ValidAnswerResult checks an answer step's result: an answer of 1 to |
| 130 | // 1,200 characters, the results it used among the results given, and no |
| 131 | // queries. |
| 132 | func ValidAnswerResult(r LookupResult, results int) error { |
| 133 | if r.Code != "" || r.Queries != nil || !utf8.ValidString(r.Answer) { |
| 134 | return errLookupResult |
| 135 | } |
| 136 | if n := utf8.RuneCountInString(r.Answer); n == 0 || n > MaxLookupAnswer { |
| 137 | return errLookupResult |
| 138 | } |
| 139 | seen := map[int]bool{} |
| 140 | for _, i := range r.Used { |
| 141 | if i < 0 || i >= results || seen[i] { |
| 142 | return errLookupResult |
| 143 | } |
| 144 | seen[i] = true |
| 145 | } |
| 146 | return nil |
| 147 | } |
| 148 | |