feat: add deterministic Markdown endpoint for tabs#3278
Open
linouxis9 wants to merge 5 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
GET /tabs/:tabId/markdownfor deterministic Markdown rendering of the current tab, backed by Playwright accessibility snapshots.view=document(default): readable Markdown without refs/control noise.view=agent: agent-friendly Markdown preserving actionable refs, controls, states, and values.GET /tabs/:tabId/snapshotbehavior intact for backward compatibility.playwright-coreto^1.59.1, forpage.ariaSnapshot({ mode: 'ai' })support.Why
Raw accessibility snapshots are useful for automation but noisy for reading/extraction. This endpoint gives callers a stable Markdown surface without forcing consumers to post-process snapshot YAML themselves, while
view=agentpreserves actionability for future agent workflows.API
Response shape mirrors snapshot pagination fields:
{ "url": "https://example.com", "view": "document", "markdown": "# Example\n\n...", "refsCount": 12, "truncated": false, "totalChars": 1234, "offset": 0, "hasMore": false, "nextOffset": null }view=document: clean, no refsview=agent— refs and controls preserved/snapshot: unchanged (raw aria YAML)Implementation
Renderer (
lib/markdown.js, ~1,400 lines):view.Design choices:
viewis an enum, not a toggle.agentmode renders inline controls with states/values, keeps interactive chrome visible, and marks un-annotated elementsno ref.buildRefs→annotateAriaSnapshotWithRefs).tabState.lastMarkdownis a per-viewMap; snapshot and markdown pagination never cross-contaminate.## Sidebar: [link1], [link2]lines.Route (
server.js):parseSnapshotRoleLine,annotateAriaSnapshotWithRefs,buildAnnotatedAriaSnapshot) used by both/snapshotand/markdown.getAriaSnapshotaccepts{ mode };mode='ai'triespage.ariaSnapshot({ mode: 'ai' })with body-locator fallback.clearRenderedContentCaches(tabState)replaces scatteredlastSnapshot = nullassignments across navigate, click, type, scroll, press, back, forward, refresh, and viewport.extractGoogleSerpdirectly.Testing
Unit tests (
tests/unit/markdown.test.js: 22 tests): empty input, common shapes (headings, paragraphs, links, images, lists, tables, code), document ref/control stripping, agent ref/control preservation, YAML-quoted keys, legacy@erefs, safe code fences, inferred table headers, layout table flattening, GFM task lists, private-use glyph filtering, duplicate line collapsing.E2E tests (
tests/e2e/markdown.test.js: 7 tests): document default, agent view, invalid view → 400,/snapshotbackcompat, offset pagination with Markdown markers, per-view cache isolation, cache invalidation on navigation.AI snapshot capability (
tests/e2e/playwrightAiSnapshotCapabilities.test.js): verifiespage.ariaSnapshot({ mode: 'ai' })is available and produces[ref=eN]annotations.Backwards compatibility
/tabs/:tabId/snapshot: unchanged. Response shape, query params, and pagination behavior preserved.