Skip to content

feat(browser): send design mode annotations to the composer (S3) - #154

Merged
bubbleptr merged 14 commits into
mainfrom
feat/embedded-browser-payload
Sep 2, 2026
Merged

bubbleptr merged 14 commits into
mainfrom
feat/embedded-browser-payload

Conversation

@bubbleptr

Copy link
Copy Markdown
Owner

Closes #151. Refs #86. PRD: .scratch/embedded-browser/PRD.md(决策 5、「已拍板」、「S2 / S3 实现约束」第 4、5 条)。

摘要

design mode 的标注现在能回到 Chat:工具条新增 Send to composer,把固定模板的 markdown 与一张带序号标记的截图落到当前 Session 的 composer 草稿(只落草稿,不直发)。

  • packages/core/src/browser-annotation.ts:BrowserAnnotationElement / BrowserAnnotationViewport / BrowserAnnotationPayload(从 browser-protocol.ts 搬入,protocol 只做 type 再导出)与 formatBrowserAnnotationPrompt。两种模板由 payload.screenshot 决定:有截图时靠序号定位;无截图时每条附 rect。
  • 截图握手:browser_capture_annotation → 主进程向页面下发 prepare-capture → overlay 收起评论气泡(提交评论)、隐藏 hover 高亮、重测视口,回 capture-ready { annotations, viewport } → 主进程 capturePage 并按面板 CSS 宽度 nativeImage.resize 降采样 → 返回 { image, annotations, viewport, url }。渲染层用这份答复组装 payload,不用闭包状态。页面 500ms 不应答则按主进程最后收到的标注直接截图。
  • composer 注入口:entities/session/composer-injections.ts(window CustomEvent,与 follow-up-drafts 同范式),FullChatComposer 订阅本 Session 的注入:文本追加到草稿并持久化,截图经 attachments.addFiles 造 File 走既有附件路径(抽屉预览 + 8 MiB 校验)。注入返回是否被消费,未消费(只读投影 / draft 视图)时 surface 显示一行 role="status" 提示,标注保留。
  • 防护:发送期间按钮禁用与 in-flight 守卫;data-testid 选择器改用 CSS.escape;主进程与 core 都把换行折成空格,页面内容无法伪造 prompt 行。
  • E2E:严格 CSP 页标注 → Send → composer 草稿出现模板文本与一张 PNG 附件;顺带修了 S1 一条偶发(tooltip 快照用例的 hover 改为每次轮询重发)。
  • /design 页与 docs/self-built-ui.md 已更新。

与 PRD / issue 的偏差

  • title 永远为空:协议没有 title-updated 事件(S1 砍掉的),不在 S3 补。
  • 有截图时 prompt 不打印 rect:标记时刻的 rect 滚动后即失效,截图上的序号才是定位依据;无截图时才退回 rect。
  • 8 MiB 校验留在 composer 附件路径,主进程只负责降采样;超限会在提交时按既有路径报错,不会静默丢图。
  • browser_capture_annotation 与 browser_capture 保持两条命令:前者带握手与载荷,后者是弹层快照,语义不同(审查建议合并,未采纳)。

已知限制(S4 写入 ADR-0029)

  • 握手 500ms 超时后按最后收到的标注截图,此时进行中的评论可能缺失、打开的气泡可能入镜。
  • 截图降采样到面板 CSS 宽度,HiDPI 下 Pi 看到的是 1x 图。
  • 滚出视口的标记在文本里列出,但不在图上。
  • 无截图时的 rect 是标记时刻的值。
  • 注入只到达当前 Session 已挂载的 composer,不排队。
  • 注入文本作为 follow-up 草稿持久化,截图不持久化;重载后只剩文本。
  • no screenshot / no composer 只用一行纯文本提示,不用弹层(弹层会触发快照冻结)。

审查

/code-review medium 8 条:7 条修复(其中截图入镜、视口过期、评论漏发三条由握手机制一并解决),1 条合并命令的建议未采纳(理由见上)。

验证

  • bunx tsc --noEmit -p tsconfig.json 通过
  • bun run test:97 files / 895 tests,连跑 2 次通过
  • bun run test:e2e:18 passed,连跑 2 次(coder 侧 6 次)
  • out/preload/:两个自包含文件

…hem for Pi

The annotation types outgrew the wire contract: S3 assembles a payload out of
them and hands Pi a markdown block, so both belong on the shared kernel's
public surface. `browser-protocol.ts` re-exports them type-only, which keeps
the annotation preload self-contained (PRD S2 constraint 6).

The template is fixed on purpose — it is what Pi reads — so the tests assert
whole strings rather than fragments.

Refs #151.
…ght on it

Two things stand between `capturePage()` and a prompt attachment. On a HiDPI
display the capture is twice the page's size, and a wide panel then lands near
the 8 MiB prompt-image ceiling for pixels a model cannot use — so
`browser_capture_annotation` downsamples to the panel's own CSS width, while
the overlay still keeps every device pixel it is displayed at.

And the hover highlight is `position: fixed` over the last element the pointer
touched. Reaching the toolbar takes the pointer out of the page, so the overlay
drops the highlight there — `relatedTarget` is null only when the pointer
really left, which is what keeps it following the pointer inside the page.

Refs #151.
The draft is local state inside `FullChatComposer` and its attachments only
ever live in memory, so a surface outside the chat column had no way in. This
adds one, following the CustomEvent paradigm the follow-up drafts next door
already use: text is appended to the draft as its own block, files go through
the existing attachment path, and only a composer mounted for that Session
takes it.

Nothing is queued for a composer that is not mounted, and no image is written
to localStorage (PRD S3 implementation constraint 5).

Refs #151.
The toolbar gets the action design mode exists for. It assembles the payload
from what the page reported — the marks and the viewport they were measured in,
which travels with them so resizing the panel before sending cannot rewrite it
— renders it with core's template, and drops it into this Session's composer
with a screenshot as a normal image attachment.

The URL comes from the last navigation rather than the address bar, which is an
input the user can type in without going anywhere.

Refs #151.
…draft

Extends the design-mode case: after marking the strict-CSP page, Send to
composer has to leave the template text in the Session's composer with the
screenshot attached. No comment is typed — the bubble is inside the closed
shadow root, out of reach of any driver — so this also covers the row for a
mark the user said nothing about.

Refs #151.
…edger

The gallery already carried the surface in both marked and unmarked states, so
the new control shows up in both — the captions now say what it does, and a
test holds the two states there. `docs/self-built-ui.md` gets the S3 half of
the browser-surface entry and drops S3 from the roadmap row.

Refs #151.
The address bar is an editable input: typing in it without pressing Enter goes
nowhere, and the prompt must not claim the marks are on a page that was never
loaded.

Refs #151.
Two ways a page could reach past the fields it is describing. A `data-testid`
holding a newline made an unusable selector — a parse error inside a CSS
string, which Chromium throws on and jsdom silently matches nothing with — so
it goes through `CSS.escape` like the id branch already did. And every string
main accepts is folded to one line before it is clamped, because the prompt is
one row per mark and a newline in a comment is a row the page wrote.

Review findings 6.
The template promised an attached screenshot unconditionally, so a failed
capture left Pi looking for numbered markers on an image that is not there.
`screenshot` now says which of the two templates applies, and the one without
carries each mark's viewport rect — the only locator left once the picture is
gone.

Core also folds its own fields to one line. Main does it at the trust boundary;
the template's promise of one row per mark is core's to keep for every caller.

Review findings 5, 6.
Three of the review findings were one bug: the payload and the picture came
from different moments. The comment being typed reached main only on blur and
after two IPC hops, the hover box could still be framing an element, and the
viewport in the prompt was whatever the last mark was measured in.

So `browser_capture_annotation` is a handshake now. Main asks the page to
prepare — the overlay commits and closes the bubble, hides the highlight, and
answers with what it then holds, measured at the size the shot is about to be
taken at — and only then does it photograph. The answer travels with the image,
so one moment describes the whole payload. A page that does not answer within
half a second is photographed anyway against the marks main last heard, rather
than leaving the toolbar waiting on a document that may have no overlay at all.

The HiDPI/8 MiB reasoning now lives once, where the cap is decided.

Review findings 2, 3, 7.
An archived Session shows a read-only projection with no composer, and the
browser panel outlives a switch to one. The injection was dispatched into that
silence and the user was left believing their marks had been sent.

Dispatch is synchronous, so the subscriber can answer on the way through:
`injectIntoComposer` now returns whether a composer took it.

Review finding 1.
… happened

The panel builds the payload out of the capture's own answer now, so the
comment committed by the handshake and the viewport measured for the shot are
the ones Pi reads; what this side was holding only stands in when the whole
exchange fails.

Two ways a send could go wrong quietly, both now visible in a line of plain
text under the toolbar — a layer there would swap the live page for a still:
no composer took it (the marks stay on the page, so it can be sent again), or
it went without its screenshot (the prompt says so and carries rects instead).
And the button is out of reach while a send is in flight: the handshake is a
round trip, and a second click during it pasted the block into the draft twice.

Design page carries both new states; `docs/self-built-ui.md` reconciled.

Review findings 1, 4, 5.
The handshake keeps the highlight out of the screenshot, including on the
keyboard route this rule never covered. What is left of it is what the user
sees: a highlight that follows the pointer must stop framing an element once
the pointer is off the page.
…shot

Flaked on the first run after a build: a single hover dispatched while the
panel was still settling lands where the button is about to be, and no layer
ever opens for the waiting assertion to see. Re-hovering each poll makes the
wait mean what it says.

Seen once in four full runs before, none in six after.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Embedded browser S3:标注载荷回传 composer(core 类型 + 格式化 + 截图 + Send to composer)

1 participant