feat: fix glow for CDP page-based tool calls#376
Conversation
Greptile SummaryThis PR restores sidepanel glow functionality after the migration to CDP page-based tool calls by enriching streamed tool inputs with Key Changes:
Design Rationale: Issues Found:
Confidence Score: 3/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Client as Sidepanel UI
participant Service as ChatV2Service
participant Agent as AiSdkAgent
participant Browser as Browser
participant Hook as useNotifyActiveTab
Client->>Service: User message
Service->>Agent: createAgentUIStream
Agent->>Agent: Process tool call
Agent->>Agent: Emit tool-input-available(page: 5)
Agent->>Service: Stream chunk
Service->>Browser: resolvePageIdToTabId(5)
Browser->>Browser: listPages()
Browser-->>Service: tabId: 99
Service->>Service: enrichToolInputChunkForGlow
Service->>Client: Stream enriched chunk(page: 5, tabId: 99)
Client->>Hook: Update with enriched input
Hook->>Hook: Cache page→tab mapping
Hook->>Hook: Activate glow on tab 99
Hook->>Client: chrome.tabs.sendMessage(99)
Last reviewed commit: bedc904 |
| const enrichedStream = stream.pipeThrough( | ||
| new TransformStream<UIMessageChunk, UIMessageChunk>({ | ||
| transform: async (chunk, controller) => { | ||
| const enrichedChunk = await enrichToolInputChunkForGlow( |
There was a problem hiding this comment.
if CDP disconnects, enrichToolInputChunkForGlow will throw and break the stream
| const enrichedChunk = await enrichToolInputChunkForGlow( | |
| const enrichedChunk = await enrichToolInputChunkForGlow( | |
| chunk, | |
| this.deps.browser, | |
| ).catch(() => chunk) |
Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/server/src/agent/tool-loop/service.ts
Line: 161
Comment:
if CDP disconnects, `enrichToolInputChunkForGlow` will throw and break the stream
```suggestion
const enrichedChunk = await enrichToolInputChunkForGlow(
chunk,
this.deps.browser,
).catch(() => chunk)
```
How can I resolve this? If you propose a fix, please make it concise.| return args | ||
| } | ||
|
|
||
| const tabId = await browser.resolvePageIdToTabId(pageId) |
There was a problem hiding this comment.
if CDP disconnects, resolvePageIdToTabId will throw during listPages(). wrap in try-catch to gracefully handle
| const tabId = await browser.resolvePageIdToTabId(pageId) | |
| const tabId = await browser.resolvePageIdToTabId(pageId).catch(() => undefined) |
Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/server/src/tools/framework.ts
Line: 65
Comment:
if CDP disconnects, `resolvePageIdToTabId` will throw during `listPages()`. wrap in try-catch to gracefully handle
```suggestion
const tabId = await browser.resolvePageIdToTabId(pageId).catch(() => undefined)
```
How can I resolve this? If you propose a fix, please make it concise.
Summary
tabIdwhen tools providepage/pageId.Browserand wire it intoGeminiAgenttool-call streaming updates.pageId -> tabIdcache and add unit coverage for enrichment logic.Design
The fix keeps the new page-first CDP tool contract intact and adds compatibility metadata at stream time. Before each tool execution, the server resolves
pageIdtotabIdfrom authoritative Browser state and emits an updatedtool-input-availableevent for the sametoolCallId, allowing AI SDK to update the existing tool part in-place. The extension glow hook then reliably receivestabIdwhile still supporting page-centric workflows.Test plan
bun --env-file=.env.development test apps/server/tests/tools/framework.test.tsbun run --filter @browseros/server typecheckbunx biome check apps/agent/entrypoints/sidepanel/index/useNotifyActiveTab.tsx apps/server/src/tools/framework.ts apps/server/src/agent/gemini-agent.ts apps/server/src/agent/session.ts apps/server/src/api/services/chat-service.ts apps/server/src/browser/browser.ts apps/server/tests/tools/framework.test.tsbun run --filter @browseros/agent typecheck(fails in this environment due Node heap OOM; rerun with higher memory in CI/local)