Skip to content

Commit 008e833

Browse files
committed
fix: prevent browser tool image accumulation to avoid AWS Bedrock 20-image limit
- Add addBrowserActionToApiHistory method to Task class that removes previous browser screenshots before adding new ones - Modify browserActionTool to use new method instead of direct addToApiConversationHistory - Add resetBrowserScreenshotTracking method to clear tracking when browser is closed - Fixes issue #6348 where continuous browser tool usage would accumulate screenshots and hit AWS Bedrock's 20-image limit This implements @daniel-lxs's recommendation to only send the most recent screenshot instead of accumulating all screenshots in conversation history.
1 parent 181993f commit 008e833

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

src/core/task/Task.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ export class Task extends EventEmitter<ClineEvents> {
214214

215215
// Computer User
216216
browserSession: BrowserSession
217+
private lastBrowserScreenshotMessageId?: string // Track the last message with browser screenshot
217218

218219
// Editing
219220
diffViewProvider: DiffViewProvider
@@ -508,6 +509,57 @@ export class Task extends EventEmitter<ClineEvents> {
508509
await this.saveApiConversationHistory()
509510
}
510511

512+
/**
513+
* Add a browser action result to conversation history, removing previous browser screenshots
514+
* to prevent hitting provider image limits (e.g., AWS Bedrock's 20-image limit).
515+
*/
516+
async addBrowserActionToApiHistory(
517+
toolResult: string | Array<Anthropic.TextBlockParam | Anthropic.ImageBlockParam>,
518+
) {
519+
// Remove previous browser screenshot from conversation history
520+
if (this.lastBrowserScreenshotMessageId) {
521+
// Find and remove images from the last browser action message
522+
for (let i = this.apiConversationHistory.length - 1; i >= 0; i--) {
523+
const message = this.apiConversationHistory[i]
524+
if (message.role === "user" && Array.isArray(message.content)) {
525+
// Check if this message contains the last browser screenshot
526+
const hasToolResult = message.content.some(
527+
(block) => block.type === "text" && block.text.includes("[browser_action Result]"),
528+
)
529+
if (hasToolResult) {
530+
// Remove image blocks from this message, keep only text blocks
531+
message.content = message.content.filter((block) => block.type === "text")
532+
break
533+
}
534+
}
535+
}
536+
}
537+
538+
// Add the new browser action result
539+
const content = Array.isArray(toolResult) ? toolResult : [{ type: "text" as const, text: toolResult }]
540+
const messageWithTs = {
541+
role: "user" as const,
542+
content,
543+
ts: Date.now(),
544+
}
545+
546+
// Track this message if it contains images
547+
const hasImages = Array.isArray(toolResult) && toolResult.some((block) => block.type === "image")
548+
if (hasImages) {
549+
this.lastBrowserScreenshotMessageId = messageWithTs.ts.toString()
550+
}
551+
552+
this.apiConversationHistory.push(messageWithTs)
553+
await this.saveApiConversationHistory()
554+
}
555+
556+
/**
557+
* Reset browser screenshot tracking when browser is closed
558+
*/
559+
resetBrowserScreenshotTracking() {
560+
this.lastBrowserScreenshotMessageId = undefined
561+
}
562+
511563
async overwriteApiConversationHistory(newHistory: ApiMessage[]) {
512564
this.apiConversationHistory = newHistory
513565
await this.saveApiConversationHistory()

src/core/tools/browserActionTool.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,25 @@ export async function browserActionTool(
158158
case "resize":
159159
await cline.say("browser_action_result", JSON.stringify(browserActionResult))
160160

161-
pushToolResult(
162-
formatResponse.toolResult(
161+
{
162+
const toolResult = formatResponse.toolResult(
163163
`The browser action has been executed. The console logs and screenshot have been captured for your analysis.\n\nConsole logs:\n${
164164
browserActionResult?.logs || "(No new logs)"
165165
}\n\n(REMEMBER: if you need to proceed to using non-\`browser_action\` tools or launch a new browser, you MUST first close cline browser. For example, if after analyzing the logs and screenshot you need to edit a file, you must first close the browser before you can use the write_to_file tool.)`,
166166
browserActionResult?.screenshot ? [browserActionResult.screenshot] : [],
167-
),
168-
)
167+
)
168+
169+
// Use the new method to manage browser screenshot history
170+
await cline.addBrowserActionToApiHistory(toolResult)
171+
172+
pushToolResult(toolResult)
173+
}
169174

170175
break
171176
case "close":
177+
// Reset browser screenshot tracking when browser is closed
178+
cline.resetBrowserScreenshotTracking()
179+
172180
pushToolResult(
173181
formatResponse.toolResult(
174182
`The browser has been closed. You may now proceed to using other tools.`,

0 commit comments

Comments
 (0)