/** * ============================================================ * axSearch.ts —— 无障碍树里的查找原语(纯函数) * ------------------------------------------------------------ * 三个消费方共用这一份: * · `manualSchema.ts::evaluatePrecondition` 的 element_exists / 文本谓词 * · `waitFor.ts` 的 wait_for_element / wait_for_text * · `computeruse.ts` 的 click-by-text(`textTarget` → elementId) * * 分出来的理由不是「代码复用」,是**判据必须一致**:说明书里写 * `precondition: element_exists AXButton:发送`,模型接着 `wait_for_element` * 等同一个控件,然后 `textTarget:"发送"` 去点它 —— 这三步要是各用一套匹配 * 规则,就会出现「前提说成立、等待等不到」或者「等到了、点的是另一个」。 * * ★ 匹配一律**大小写不敏感的子串**,而不是相等。AX 的 name 常带修饰 * (「发送 (⌘↩)」「Send Message」),要求相等等于要求调用方精确复述系统文案。 * role 例外:它是枚举(AXButton/AXTextField…),用相等。 * ============================================================ */ /** 查找只关心这几件事,不绑具体协议类型(便于合成测试)。 */ export interface AxSearchNode { elementId?: string; role?: string; name?: string; value?: string; subrole?: string; focused?: boolean; enabled?: boolean; actions?: string[]; bounds?: { x: number; y: number; width: number; height: number }; children?: AxSearchNode[]; } /** 大小写不敏感的子串包含。空 needle 视为不匹配(避免「搜空串命中一切」)。 */ export function containsFold(haystack: string | undefined, needle: string): boolean { if (!needle) return false; return (haystack ?? '').toLowerCase().includes(needle.toLowerCase()); } /** 去所有空白(空格/全角空格/制表符/换行)后再做大小写不敏感包含匹配。 * 用于中文场景:AX name 常带空格(如「发 送」「微信(3)」),模型输入可能不带空格。 */ export function containsFoldLoose(haystack: string | undefined, needle: string): boolean { if (!needle) return false; const strip = (s: string) => s.replace(/[\s\u3000\u00a0\u200b]/g, '').toLowerCase(); return strip(haystack ?? '').includes(strip(needle)); } /** 深度优先展平。顺序稳定 —— 多个候选时「第一个」必须可复现。 */ export function flattenAx(node: AxSearchNode | undefined | null): AxSearchNode[] { if (!node || typeof node !== 'object') return []; return [node, ...(node.children ?? []).flatMap(flattenAx)]; } /** role 相等(大小写不敏感)。缺 role 的节点永不匹配。 */ export function matchesRole(node: AxSearchNode, role: string): boolean { if (!role) return false; return (node.role ?? '').toLowerCase() === role.toLowerCase(); } /** 节点的可见文本:name 或 value 任一含给定片段。 */ export function matchesText(node: AxSearchNode, text: string): boolean { return containsFold(node.name, text) || containsFold(node.value, text); } /** 宽松文本匹配:先按普通 containsFold,再试去空白的 loose 匹配(中文场景友好)。 */ export function matchesTextLoose(node: AxSearchNode, text: string): boolean { if (matchesText(node, text)) return true; return containsFoldLoose(node.name, text) || containsFoldLoose(node.value, text); } /** * 按 role(+ 可选名字片段)找控件。 * * `spec` 用说明书里 `element_exists` 的同一语法:`AXButton` 或 `AXButton:发送`。 * 共用语法是有意的 —— 模型在注入的摘要里见到的就是这个形状。 */ export function findByElementSpec(nodes: AxSearchNode[], spec: string): AxSearchNode[] { const [rawRole, ...rest] = spec.split(':'); const role = (rawRole ?? '').trim(); const namePart = rest.join(':').trim(); return nodes.filter((node) => matchesRole(node, role) && (!namePart || containsFold(node.name, namePart))); } /** 按 role + 可选名字片段找控件(结构化入参版本)。 */ export function findByRole( nodes: AxSearchNode[], role: string, nameFragment?: string, ): AxSearchNode[] { return nodes.filter((node) => matchesRole(node, role) && (!nameFragment || containsFold(node.name, nameFragment))); } /** * 按文字找**可点**的控件。 * * 只返回带 `elementId` 且有 `bounds` 的节点:click-by-text 最终要走 elementId * 定位,一个说得出名字却给不出位置的节点点不了,把它算进候选只会让「多个匹配」 * 的拒绝变得莫名其妙。 * * 匹配策略:先精确包含匹配,命中为 0 时再试去空白的宽松匹配(中文场景下 AX name * 常夹杂空格/全角空格,模型输入不会带这些)。 */ export function findClickableByText(nodes: AxSearchNode[], text: string): AxSearchNode[] { const eligible = nodes.filter((node) => Boolean(node.elementId) && Boolean(node.bounds)); const exact = eligible.filter((node) => matchesText(node, text)); if (exact.length > 0) return exact; return eligible.filter((node) => matchesTextLoose(node, text)); } /** 人话描述一个候选,用于「多个匹配」时把选择权交回给模型。 */ export function describeNode(node: AxSearchNode): string { const name = node.name ? `「${node.name}」` : '(无名字)'; const where = node.bounds ? `位于 (${Math.round(node.bounds.x)},${Math.round(node.bounds.y)})` : '无位置'; return `${node.role ?? '未知角色'} ${name} ${where}${node.elementId ? ` · ${node.elementId}` : ''}`; }