@@ -7,10 +7,13 @@ import { Effect, Schema } from "effect"
77import { BrowserExecute } from "@browser-use/bcode-browser/browser-execute"
88import { Global } from "@opencode-ai/core/global"
99import { InstanceState } from "@/effect/instance-state"
10+ import { Truncate } from "./truncate"
1011import * as Tool from "./tool"
1112import DESCRIPTION from "./browser-execute.txt"
1213
1314const MAX_METADATA_LENGTH = 30_000
15+ const MAX_CONTEXT_BYTES = 8 * 1024
16+ const MAX_CONTEXT_LINES = 400
1417const preview = ( text : string ) =>
1518 text . length <= MAX_METADATA_LENGTH ? text : "...\n\n" + text . slice ( - MAX_METADATA_LENGTH )
1619
@@ -24,6 +27,7 @@ export const BrowserExecuteTool = Tool.define(
2427 "browser_execute" ,
2528 Effect . gen ( function * ( ) {
2629 const impl = yield * BrowserExecute . make ( Global . Path . data )
30+ const truncate = yield * Truncate . Service
2731 return {
2832 // Substitute the resolved skills path so `{{SKILLS_DIR}}` references in
2933 // the description point at a concrete location. Workspace is
@@ -66,18 +70,28 @@ export const BrowserExecuteTool = Tool.define(
6670 mime : s . mime ,
6771 url : `data:${ s . mime } ;base64,${ s . base64 } ` ,
6872 } ) )
73+ const fullOutput = [
74+ result . output . trimEnd ( ) ,
75+ result . result === "null" ? "" : `=> ${ result . result } ` ,
76+ attachments . length > 0
77+ ? `(${ attachments . length } screenshot${ attachments . length === 1 ? "" : "s" } attached)`
78+ : "" ,
79+ ]
80+ . filter ( Boolean )
81+ . join ( "\n\n" )
82+ const contextualOutput = yield * truncate . output ( fullOutput , {
83+ maxBytes : MAX_CONTEXT_BYTES ,
84+ maxLines : MAX_CONTEXT_LINES ,
85+ } )
6986 return {
7087 title : "browser_execute" ,
71- output : [
72- result . output . trimEnd ( ) ,
73- result . result === "null" ? "" : `=> ${ result . result } ` ,
74- attachments . length > 0
75- ? `(${ attachments . length } screenshot${ attachments . length === 1 ? "" : "s" } attached)`
76- : "" ,
77- ]
78- . filter ( Boolean )
79- . join ( "\n\n" ) ,
80- metadata : { result : result . result , output : preview ( result . output ) } ,
88+ output : contextualOutput . content ,
89+ metadata : {
90+ result : result . result ,
91+ output : preview ( result . output ) ,
92+ truncated : contextualOutput . truncated ,
93+ ...( contextualOutput . truncated && { outputPath : contextualOutput . outputPath } ) ,
94+ } ,
8195 attachments,
8296 }
8397 } ) . pipe ( Effect . orDie ) ,
0 commit comments