(args: Record<string, any>)
| 537 | } |
| 538 | |
| 539 | async function agentQuery(args: Record<string, any>): Promise<{ content: string }> { |
| 540 | const selector = typeof args.selector === "string" ? args.selector : undefined; |
| 541 | const mode = typeof args.mode === "string" && args.mode ? args.mode : "text"; |
| 542 | const indexValue = Number.isFinite(args.index) ? args.index : 0; |
| 543 | const limitValue = Number.isFinite(args.limit) |
| 544 | ? args.limit |
| 545 | : mode === "page_text" |
| 546 | ? DEFAULT_PAGE_TEXT_LIMIT |
| 547 | : DEFAULT_LIST_LIMIT; |
| 548 | const timeoutValue = Number.isFinite(args.timeoutMs) ? args.timeoutMs : 0; |
| 549 | const pollValue = Number.isFinite(args.pollMs) ? args.pollMs : DEFAULT_POLL_MS; |
| 550 | const pattern = typeof args.pattern === "string" ? args.pattern : null; |
| 551 | const flags = typeof args.flags === "string" ? args.flags : "i"; |
| 552 | |
| 553 | if (mode === "page_text") { |
| 554 | if (selector && timeoutValue > 0) { |
| 555 | await waitForCount(selector, 1, timeoutValue, pollValue); |
| 556 | } |
| 557 | const pageText = await agentEvaluate(buildAgentPageTextScript(limitValue, pattern, flags)); |
| 558 | return { content: JSON.stringify({ ok: true, value: pageText }, null, 2) }; |
| 559 | } |
| 560 | |
| 561 | if (!selector) throw new Error("selector is required"); |
| 562 | |
| 563 | if (mode === "exists") { |
| 564 | const count = await waitForCount(selector, 1, timeoutValue, pollValue); |
| 565 | return { |
| 566 | content: JSON.stringify({ ok: true, value: { exists: count > 0, count } }, null, 2), |
| 567 | }; |
| 568 | } |
| 569 | |
| 570 | const count = await waitForCount(selector, indexValue + 1, timeoutValue, pollValue); |
| 571 | if (count <= indexValue) { |
| 572 | throw new Error(`No matches for selector: ${selector}`); |
| 573 | } |
| 574 | |
| 575 | if (mode === "text") { |
| 576 | const data = |
| 577 | indexValue > 0 |
| 578 | ? await agentCommand("nth", { selector, index: indexValue, subaction: "text" }) |
| 579 | : await agentCommand("innertext", { selector }); |
| 580 | return { content: typeof data?.text === "string" ? data.text : "" }; |
| 581 | } |
| 582 | |
| 583 | if (mode === "value") { |
| 584 | if (indexValue > 0) { |
| 585 | const result = ensureEvalResult( |
| 586 | await agentEvaluate(buildAgentNthValueScript(selector, indexValue)), |
| 587 | "Value lookup failed" |
| 588 | ); |
| 589 | return { content: typeof result === "string" ? result : JSON.stringify(result) }; |
| 590 | } |
| 591 | const data = await agentCommand("inputvalue", { selector }); |
| 592 | return { content: typeof data?.value === "string" ? data.value : "" }; |
| 593 | } |
| 594 | |
| 595 | if (mode === "attribute") { |
| 596 | if (!args.attribute) throw new Error("attribute is required"); |
no test coverage detected