-
Notifications
You must be signed in to change notification settings - Fork 31
feat: fix glow for CDP page-based tool calls #376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import type { UIMessageChunk } from 'ai' | ||
| import type { Browser } from '../../browser/browser' | ||
| import { enrichToolInputWithTabId } from '../../tools/framework' | ||
|
|
||
| type ToolInputAvailableChunk = UIMessageChunk & { | ||
| type: 'tool-input-available' | ||
| input?: unknown | ||
| } | ||
|
|
||
| function isToolInputAvailableChunk( | ||
| chunk: UIMessageChunk, | ||
| ): chunk is ToolInputAvailableChunk { | ||
| return chunk.type === 'tool-input-available' | ||
| } | ||
|
|
||
| export async function enrichToolInputChunkForGlow( | ||
| chunk: UIMessageChunk, | ||
| browser: Browser, | ||
| ): Promise<UIMessageChunk> { | ||
| if (!isToolInputAvailableChunk(chunk)) { | ||
| return chunk | ||
| } | ||
|
|
||
| const enrichedInput = await enrichToolInputWithTabId(chunk.input, browser) | ||
| if (enrichedInput === chunk.input) { | ||
| return chunk | ||
| } | ||
|
|
||
| return { ...chunk, input: enrichedInput } as UIMessageChunk | ||
| } |
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
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
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
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -32,6 +32,44 @@ export function defineTool<T extends z.ZodType>(config: { | |||||
| return config as ToolDefinition | ||||||
| } | ||||||
|
|
||||||
| function getNumberField( | ||||||
| value: Record<string, unknown>, | ||||||
| key: string, | ||||||
| ): number | undefined { | ||||||
| const candidate = value[key] | ||||||
| if (typeof candidate !== 'number' || !Number.isFinite(candidate)) { | ||||||
| return undefined | ||||||
| } | ||||||
| return candidate | ||||||
| } | ||||||
|
|
||||||
| export async function enrichToolInputWithTabId( | ||||||
| args: unknown, | ||||||
| browser: Browser, | ||||||
| ): Promise<unknown> { | ||||||
| if (!args || typeof args !== 'object' || Array.isArray(args)) { | ||||||
| return args | ||||||
| } | ||||||
|
|
||||||
| const input = args as Record<string, unknown> | ||||||
| if (getNumberField(input, 'tabId') !== undefined) { | ||||||
| return args | ||||||
| } | ||||||
|
|
||||||
| const pageId = | ||||||
| getNumberField(input, 'pageId') ?? getNumberField(input, 'page') | ||||||
| if (pageId === undefined) { | ||||||
| return args | ||||||
| } | ||||||
|
|
||||||
| const tabId = await browser.resolvePageIdToTabId(pageId) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if CDP disconnects,
Suggested change
Prompt To Fix With AIThis 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. |
||||||
| if (tabId === undefined) { | ||||||
| return args | ||||||
| } | ||||||
|
|
||||||
| return { ...input, tabId } | ||||||
| } | ||||||
|
|
||||||
| export async function executeTool( | ||||||
| tool: ToolDefinition, | ||||||
| args: unknown, | ||||||
|
|
||||||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { describe, expect, it } from 'bun:test' | ||
| import type { UIMessageChunk } from 'ai' | ||
| import { enrichToolInputChunkForGlow } from '../../../src/agent/tool-loop/glow-enrichment' | ||
| import type { Browser } from '../../../src/browser/browser' | ||
|
|
||
| describe('enrichToolInputChunkForGlow', () => { | ||
| const browser = { | ||
| resolvePageIdToTabId: async (pageId: number) => { | ||
| if (pageId === 5) return 99 | ||
| return undefined | ||
| }, | ||
| } as unknown as Browser | ||
|
|
||
| it('returns non-tool-input chunk unchanged', async () => { | ||
| const chunk = { | ||
| type: 'text-delta', | ||
| id: '0', | ||
| delta: 'hello', | ||
| } as UIMessageChunk | ||
|
|
||
| const result = await enrichToolInputChunkForGlow(chunk, browser) | ||
| expect(result).toBe(chunk) | ||
| }) | ||
|
|
||
| it('enriches tool-input-available chunk with tabId from page', async () => { | ||
| const chunk = { | ||
| type: 'tool-input-available', | ||
| toolCallId: 'call_1', | ||
| toolName: 'click', | ||
| input: { page: 5, element: 12 }, | ||
| } as UIMessageChunk | ||
|
|
||
| const result = await enrichToolInputChunkForGlow(chunk, browser) | ||
| expect(result).toEqual({ | ||
| type: 'tool-input-available', | ||
| toolCallId: 'call_1', | ||
| toolName: 'click', | ||
| input: { page: 5, element: 12, tabId: 99 }, | ||
| }) | ||
| }) | ||
|
|
||
| it('keeps chunk unchanged when tabId already exists', async () => { | ||
| const chunk = { | ||
| type: 'tool-input-available', | ||
| toolCallId: 'call_2', | ||
| toolName: 'click', | ||
| input: { tabId: 7, page: 5 }, | ||
| } as UIMessageChunk | ||
|
|
||
| const result = await enrichToolInputChunkForGlow(chunk, browser) | ||
| expect(result).toBe(chunk) | ||
| }) | ||
|
|
||
| it('keeps chunk unchanged when page cannot be resolved', async () => { | ||
| const chunk = { | ||
| type: 'tool-input-available', | ||
| toolCallId: 'call_3', | ||
| toolName: 'click', | ||
| input: { page: 404 }, | ||
| } as UIMessageChunk | ||
|
|
||
| const result = await enrichToolInputChunkForGlow(chunk, browser) | ||
| expect(result).toBe(chunk) | ||
| }) | ||
| }) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { describe, expect, it } from 'bun:test' | ||
| import type { Browser } from '../../src/browser/browser' | ||
| import { enrichToolInputWithTabId } from '../../src/tools/framework' | ||
|
|
||
| describe('enrichToolInputWithTabId', () => { | ||
| const browser = { | ||
| resolvePageIdToTabId: async (pageId: number) => { | ||
| if (pageId === 7) return 42 | ||
| if (pageId === 8) return 84 | ||
| return undefined | ||
| }, | ||
| } as unknown as Browser | ||
|
|
||
| it('returns non-object input unchanged', async () => { | ||
| const input = 'not-an-object' | ||
| const result = await enrichToolInputWithTabId(input, browser) | ||
| expect(result).toBe(input) | ||
| }) | ||
|
|
||
| it('returns input unchanged when tabId is already present', async () => { | ||
| const input = { tabId: 11, page: 7 } | ||
| const result = await enrichToolInputWithTabId(input, browser) | ||
| expect(result).toBe(input) | ||
| }) | ||
|
|
||
| it('adds tabId when page is present', async () => { | ||
| const input = { page: 7, element: 13 } | ||
| const result = await enrichToolInputWithTabId(input, browser) | ||
| expect(result).toEqual({ page: 7, element: 13, tabId: 42 }) | ||
| }) | ||
|
|
||
| it('adds tabId when pageId is present', async () => { | ||
| const input = { pageId: 8 } | ||
| const result = await enrichToolInputWithTabId(input, browser) | ||
| expect(result).toEqual({ pageId: 8, tabId: 84 }) | ||
| }) | ||
|
|
||
| it('returns input unchanged when page cannot be resolved', async () => { | ||
| const input = { page: 999 } | ||
| const result = await enrichToolInputWithTabId(input, browser) | ||
| expect(result).toBe(input) | ||
| }) | ||
| }) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if CDP disconnects,
enrichToolInputChunkForGlowwill throw and break the streamPrompt To Fix With AI