|
| 1 | +import type { ActionSpaceElement, HistoryEntry, JevCandidate, JevCandidateSpace, Observation, Operation } from "./types"; |
| 2 | + |
| 3 | +const MAX_GROUNDED_CANDIDATES = 250; |
| 4 | +const EXCLUDED_CONTROL = /\b(?:password|passphrase|choose file|upload file)\b/i; |
| 5 | +const CLICKABLE_ROLES = new Set([ |
| 6 | + "button", |
| 7 | + "link", |
| 8 | + "checkbox", |
| 9 | + "radio", |
| 10 | + "switch", |
| 11 | + "tab", |
| 12 | + "menuitem", |
| 13 | + "menuitemcheckbox", |
| 14 | + "menuitemradio", |
| 15 | + "treeitem", |
| 16 | +]); |
| 17 | +const EDITABLE_ROLES = new Set(["textbox", "searchbox", "spinbutton"]); |
| 18 | + |
| 19 | +export function buildCandidateSpace(observation: Observation, goal: string, history: readonly HistoryEntry[] = []): JevCandidateSpace { |
| 20 | + const candidates: JevCandidate[] = []; |
| 21 | + const navigationOnly = observation.url === "about:blank" || observation.url.startsWith("chrome://"); |
| 22 | + const pageElements = navigationOnly ? [] : observation.elements; |
| 23 | + const operationsByRef = new Map<string, Set<Operation>>(); |
| 24 | + const optionsByRef = new Map<string, ActionSpaceElement["options"]>(); |
| 25 | + const nativeOptions = new Set<string>(); |
| 26 | + let grounded = 0; |
| 27 | + |
| 28 | + const addGrounded = (candidate: JevCandidate): boolean => { |
| 29 | + if (grounded >= MAX_GROUNDED_CANDIDATES) return false; |
| 30 | + candidates.push(candidate); |
| 31 | + grounded += 1; |
| 32 | + if (candidate.ref) { |
| 33 | + const operations = operationsByRef.get(candidate.ref) ?? new Set<Operation>(); |
| 34 | + operations.add(candidate.operation); |
| 35 | + operationsByRef.set(candidate.ref, operations); |
| 36 | + } |
| 37 | + return true; |
| 38 | + }; |
| 39 | + |
| 40 | + for (let index = 0; index < pageElements.length && grounded < MAX_GROUNDED_CANDIDATES; index++) { |
| 41 | + const element = pageElements[index]!; |
| 42 | + if (element.disabled || EXCLUDED_CONTROL.test(element.name)) continue; |
| 43 | + |
| 44 | + if (element.role === "combobox") { |
| 45 | + const options = descendantOptions(pageElements, index); |
| 46 | + if (options.length > 0) { |
| 47 | + optionsByRef.set(element.ref, options.map((option) => ({ |
| 48 | + label: option.name, |
| 49 | + value: option.name, |
| 50 | + selected: option.selected === true, |
| 51 | + }))); |
| 52 | + if (element.expanded !== true) { |
| 53 | + for (const option of options) { |
| 54 | + nativeOptions.add(option.ref); |
| 55 | + if (option.selected) continue; |
| 56 | + if (!addGrounded({ |
| 57 | + id: `select:${element.ref}:${option.ref}`, |
| 58 | + kind: "browser-step", |
| 59 | + operation: "SELECT", |
| 60 | + label: `Select ${JSON.stringify(option.name)} in ${JSON.stringify(element.name)}`, |
| 61 | + ref: element.ref, |
| 62 | + value: option.name, |
| 63 | + step: { type: "fill", ref: element.ref, value: option.name }, |
| 64 | + })) break; |
| 65 | + } |
| 66 | + continue; |
| 67 | + } |
| 68 | + } |
| 69 | + addGrounded({ |
| 70 | + id: `type:${element.ref}`, |
| 71 | + kind: "browser-step", |
| 72 | + operation: "TYPE_TEXT", |
| 73 | + label: `Enter text in ${JSON.stringify(element.name)}; current value=${JSON.stringify(element.value ?? "")}`, |
| 74 | + ref: element.ref, |
| 75 | + value: element.value ?? "", |
| 76 | + textPurpose: "field", |
| 77 | + }); |
| 78 | + addGrounded({ |
| 79 | + id: `click:${element.ref}`, |
| 80 | + kind: "browser-step", |
| 81 | + operation: "CLICK", |
| 82 | + label: `Open ${JSON.stringify(element.name)}`, |
| 83 | + ref: element.ref, |
| 84 | + step: { type: "click", ref: element.ref }, |
| 85 | + }); |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + if (EDITABLE_ROLES.has(element.role)) { |
| 90 | + addGrounded({ |
| 91 | + id: `type:${element.ref}`, |
| 92 | + kind: "browser-step", |
| 93 | + operation: "TYPE_TEXT", |
| 94 | + label: `Enter text in ${JSON.stringify(element.name)}; current value=${JSON.stringify(element.value ?? "")}`, |
| 95 | + ref: element.ref, |
| 96 | + value: element.value ?? "", |
| 97 | + textPurpose: "field", |
| 98 | + }); |
| 99 | + addGrounded({ |
| 100 | + id: `click:${element.ref}`, |
| 101 | + kind: "browser-step", |
| 102 | + operation: "CLICK", |
| 103 | + label: `Open ${JSON.stringify(element.name)}`, |
| 104 | + ref: element.ref, |
| 105 | + step: { type: "click", ref: element.ref }, |
| 106 | + }); |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + if (CLICKABLE_ROLES.has(element.role) || (element.role === "option" && !nativeOptions.has(element.ref))) { |
| 111 | + addGrounded({ |
| 112 | + id: `click:${element.ref}`, |
| 113 | + kind: "browser-step", |
| 114 | + operation: "CLICK", |
| 115 | + label: `Click ${element.role} ${JSON.stringify(element.name)}${stateDescription(element)}`, |
| 116 | + ref: element.ref, |
| 117 | + step: { type: "click", ref: element.ref }, |
| 118 | + }); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + const scrollPoint = { |
| 123 | + x: Math.max(0, Math.floor(observation.scroll.width / 2)), |
| 124 | + y: Math.max(0, Math.floor(observation.scroll.viewport / 2)), |
| 125 | + }; |
| 126 | + const scrollAmount = Math.max(1, Math.ceil(observation.scroll.viewport / 120)); |
| 127 | + if (!navigationOnly && observation.scroll.y + observation.scroll.viewport < observation.scroll.height - 2) { |
| 128 | + candidates.push({ |
| 129 | + id: "scroll:down", |
| 130 | + kind: "browser-action", |
| 131 | + operation: "SCROLL", |
| 132 | + label: "Scroll down to reveal more page content", |
| 133 | + action: { type: "browser_scroll", ...scrollPoint, direction: "down", amount: scrollAmount }, |
| 134 | + }); |
| 135 | + } |
| 136 | + if (!navigationOnly && observation.scroll.y > 0) { |
| 137 | + candidates.push({ |
| 138 | + id: "scroll:up", |
| 139 | + kind: "browser-action", |
| 140 | + operation: "SCROLL", |
| 141 | + label: "Scroll up to reveal earlier page content", |
| 142 | + action: { type: "browser_scroll", ...scrollPoint, direction: "up", amount: scrollAmount }, |
| 143 | + }); |
| 144 | + } |
| 145 | + candidates.push({ id: "wait", kind: "browser-step", operation: "WAIT", label: "Wait briefly for the page to update", step: { type: "wait", ms: 100 } }); |
| 146 | + |
| 147 | + const literalUrls = extractLiteralUrls(goal); |
| 148 | + if (literalUrls.length > 0) { |
| 149 | + for (const [index, url] of literalUrls.entries()) { |
| 150 | + candidates.push({ id: `navigate:${index}`, kind: "navigate", operation: "NAVIGATE", label: `Navigate to ${url}`, value: url }); |
| 151 | + } |
| 152 | + } else { |
| 153 | + candidates.push({ |
| 154 | + id: "navigate:resolve", |
| 155 | + kind: "navigate", |
| 156 | + operation: "NAVIGATE", |
| 157 | + label: "Navigate to the website needed to advance the goal", |
| 158 | + textPurpose: "navigation", |
| 159 | + }); |
| 160 | + } |
| 161 | + if (!navigationOnly) { |
| 162 | + candidates.push({ id: "history:back", kind: "history", operation: "BACK", label: "Go back one page" }); |
| 163 | + if (history.at(-1)?.operation === "BACK") { |
| 164 | + candidates.push({ id: "history:forward", kind: "history", operation: "FORWARD", label: "Go forward one page" }); |
| 165 | + } |
| 166 | + candidates.push({ id: "history:reload", kind: "history", operation: "RELOAD", label: "Reload the current page" }); |
| 167 | + } |
| 168 | + candidates.push({ id: "done", kind: "terminal", operation: "DONE", label: "Every requirement is visibly satisfied" }); |
| 169 | + candidates.push({ id: "blocked", kind: "terminal", operation: "BLOCKED", label: "No supported operation can make progress safely" }); |
| 170 | + |
| 171 | + const byOperation = new Map<Operation, JevCandidate[]>(); |
| 172 | + for (const candidate of candidates) { |
| 173 | + const group = byOperation.get(candidate.operation) ?? []; |
| 174 | + group.push(candidate); |
| 175 | + byOperation.set(candidate.operation, group); |
| 176 | + } |
| 177 | + const elements: ActionSpaceElement[] = pageElements |
| 178 | + .filter((element) => !EXCLUDED_CONTROL.test(element.name)) |
| 179 | + .map((element) => ({ |
| 180 | + ...element, |
| 181 | + operations: [...(operationsByRef.get(element.ref) ?? [])], |
| 182 | + ...(optionsByRef.has(element.ref) ? { options: optionsByRef.get(element.ref) } : {}), |
| 183 | + })); |
| 184 | + return { candidates, byId: new Map(candidates.map((candidate) => [candidate.id, candidate])), byOperation, elements }; |
| 185 | +} |
| 186 | + |
| 187 | +export function extractLiteralUrls(goal: string): string[] { |
| 188 | + const urls = new Set<string>(); |
| 189 | + for (const match of goal.matchAll(/https?:\/\/[^\s<>"']+/gi)) { |
| 190 | + const value = match[0].replace(/[),.;!?]+$/, ""); |
| 191 | + try { |
| 192 | + const url = new URL(value); |
| 193 | + if (url.protocol === "http:" || url.protocol === "https:") urls.add(url.href); |
| 194 | + } catch { |
| 195 | + // Ignore malformed literals and let the text resolver handle navigation. |
| 196 | + } |
| 197 | + } |
| 198 | + return [...urls].slice(0, MAX_GROUNDED_CANDIDATES); |
| 199 | +} |
| 200 | + |
| 201 | +function descendantOptions(elements: readonly Observation["elements"][number][], parentIndex: number): Observation["elements"] { |
| 202 | + const parent = elements[parentIndex]!; |
| 203 | + const options: Observation["elements"] = []; |
| 204 | + for (let index = parentIndex + 1; index < elements.length; index++) { |
| 205 | + const candidate = elements[index]!; |
| 206 | + if (candidate.depth <= parent.depth) break; |
| 207 | + if (candidate.role === "option" && !candidate.disabled) options.push(candidate); |
| 208 | + } |
| 209 | + return options; |
| 210 | +} |
| 211 | + |
| 212 | +function stateDescription(element: Observation["elements"][number]): string { |
| 213 | + const states = [ |
| 214 | + element.value === undefined ? undefined : `value=${JSON.stringify(element.value)}`, |
| 215 | + element.checked === undefined ? undefined : `checked=${element.checked}`, |
| 216 | + element.selected === undefined ? undefined : `selected=${element.selected}`, |
| 217 | + element.expanded === undefined ? undefined : `expanded=${element.expanded}`, |
| 218 | + ].filter((state): state is string => state !== undefined); |
| 219 | + return states.length ? `; ${states.join(", ")}` : ""; |
| 220 | +} |
0 commit comments