@@ -10,6 +10,8 @@ import {
1010 readNonNegativeIntegerParam ,
1111 readPositiveIntegerParam ,
1212} from "openclaw/plugin-sdk/param-readers" ;
13+ import { truncateSanitizedExternalContent } from "openclaw/plugin-sdk/security-runtime" ;
14+ import { DEFAULT_MAX_LIVE_TOOL_RESULT_CHARS } from "openclaw/plugin-sdk/text-utility-runtime" ;
1315import {
1416 DEFAULT_AI_SNAPSHOT_MAX_CHARS ,
1517 browserSnapshot ,
@@ -24,6 +26,53 @@ import { DEFAULT_BROWSER_SNAPSHOT_TIMEOUT_MS } from "./browser/constants.js";
2426import { neutralizeMediaDirectives } from "./browser/vision.js" ;
2527import { formatErrorMessage } from "./infra/errors.js" ;
2628
29+ type BrowserExternalJsonKind = "snapshot" | "console" | "tabs" | "act" | "download" ;
30+
31+ const BROWSER_EXTERNAL_JSON_TRUNCATION_MARKERS = {
32+ snapshot : "\n[truncated — retry with a smaller maxChars or limit]" ,
33+ console : "\n[truncated — retry with a stricter level or targetId]" ,
34+ tabs : "\n[truncated — retry with action=snapshot and a specific targetId]" ,
35+ act : "\n[truncated — inspect the affected targetId with action=snapshot]" ,
36+ download : "\n[truncated — retry with a specific targetId and download ref]" ,
37+ } satisfies Record < BrowserExternalJsonKind , string > ;
38+
39+ function truncateBrowserToolText ( value : string , marker : string , maxChars : number ) {
40+ const bounded = truncateSanitizedExternalContent ( value , maxChars ) ;
41+ if ( ! bounded . truncated ) {
42+ return bounded ;
43+ }
44+ const marked = truncateSanitizedExternalContent ( value , Math . max ( 0 , maxChars - marker . length ) ) ;
45+ return {
46+ text : `${ marked . text } ${ marker } ` ,
47+ truncated : true ,
48+ } ;
49+ }
50+
51+ function wrapBoundedBrowserToolText ( params : {
52+ value : string ;
53+ marker : string ;
54+ includeWarning : boolean ;
55+ } ) {
56+ const wrap = ( value : string ) =>
57+ wrapExternalContent ( value , {
58+ source : "browser" ,
59+ includeWarning : params . includeWarning ,
60+ } ) ;
61+ const wrapperOverhead = wrap ( "" ) . length ;
62+ let maxInnerChars = Math . max ( 0 , DEFAULT_MAX_LIVE_TOOL_RESULT_CHARS - wrapperOverhead ) ;
63+ let bounded = truncateBrowserToolText ( params . value , params . marker , maxInnerChars ) ;
64+ let wrappedText = wrap ( bounded . text ) ;
65+ if ( wrappedText . length > DEFAULT_MAX_LIVE_TOOL_RESULT_CHARS ) {
66+ maxInnerChars = Math . max (
67+ 0 ,
68+ maxInnerChars - ( wrappedText . length - DEFAULT_MAX_LIVE_TOOL_RESULT_CHARS ) ,
69+ ) ;
70+ bounded = truncateBrowserToolText ( params . value , params . marker , maxInnerChars ) ;
71+ wrappedText = wrap ( bounded . text ) ;
72+ }
73+ return { text : wrappedText , truncated : bounded . truncated } ;
74+ }
75+
2776export type BrowserProxyRequest = ( ( opts : {
2877 method : string ;
2978 path : string ;
@@ -40,22 +89,24 @@ export type BrowserProxyRequest = ((opts: {
4089
4190/** Wrap page-controlled JSON payloads as untrusted browser content. */
4291export function wrapBrowserExternalJson ( params : {
43- kind : "snapshot" | "console" | "tabs" | "act" | "download" ;
92+ kind : BrowserExternalJsonKind ;
4493 payload : unknown ;
4594 includeWarning ?: boolean ;
4695} ) : { wrappedText : string ; safeDetails : Record < string , unknown > } {
47- const extractedText = JSON . stringify (
48- params . payload ,
49- ( _key : string , value : unknown ) =>
50- typeof value === "string" ? neutralizeMediaDirectives ( value ) : value ,
51- 2 ,
52- ) ;
96+ const serialized =
97+ JSON . stringify (
98+ params . payload ,
99+ ( _key : string , value : unknown ) =>
100+ typeof value === "string" ? neutralizeMediaDirectives ( value ) : value ,
101+ 2 ,
102+ ) ?? "null" ;
53103 // Browser tabs, snapshots, and console output are page-controlled data. Keep
54104 // text wrapped even when details carry the structured fields for callers.
55- const wrappedText = wrapExternalContent ( extractedText , {
56- source : "browser" ,
105+ const wrappedText = wrapBoundedBrowserToolText ( {
106+ value : serialized ,
107+ marker : BROWSER_EXTERNAL_JSON_TRUNCATION_MARKERS [ params . kind ] ,
57108 includeWarning : params . includeWarning ?? true ,
58- } ) ;
109+ } ) . text ;
59110 return {
60111 wrappedText,
61112 safeDetails : {
@@ -208,17 +259,18 @@ export async function executeSnapshotAction(params: {
208259 } ,
209260 } ;
210261 }
211- const extractedText = snapshot . snapshot ?? "" ;
212- const wrappedSnapshot = wrapExternalContent ( neutralizeMediaDirectives ( extractedText ) , {
213- source : "browser" ,
262+ const extractedText = neutralizeMediaDirectives ( snapshot . snapshot ?? "" ) ;
263+ const boundedSnapshot = wrapBoundedBrowserToolText ( {
264+ value : extractedText ,
265+ marker : BROWSER_EXTERNAL_JSON_TRUNCATION_MARKERS . snapshot ,
214266 includeWarning : true ,
215267 } ) ;
216268 const safeDetails = {
217269 ok : true ,
218270 format : snapshot . format ,
219271 targetId : snapshot . targetId ,
220272 url : snapshot . url ,
221- truncated : snapshot . truncated ,
273+ truncated : snapshot . truncated || boundedSnapshot . truncated ? true : undefined ,
222274 newElements : snapshot . newElements ,
223275 stats : snapshot . stats ,
224276 refs : snapshot . refs ? Object . keys ( snapshot . refs ) . length : undefined ,
@@ -242,14 +294,14 @@ export async function executeSnapshotAction(params: {
242294 return await imageResultFromFile ( {
243295 label : "browser:snapshot" ,
244296 path : snapshot . imagePath ,
245- extraText : wrappedSnapshot ,
297+ extraText : boundedSnapshot . text ,
246298 // Keep model-only screenshots out of automatic channel delivery.
247299 details : { ...safeDetails , media : { outbound : false } } ,
248300 imageSanitization : resolveRuntimeImageSanitization ( ) ,
249301 } ) ;
250302 }
251303 return {
252- content : [ { type : "text" as const , text : wrappedSnapshot } ] ,
304+ content : [ { type : "text" as const , text : boundedSnapshot . text } ] ,
253305 details : safeDetails ,
254306 } ;
255307 }
0 commit comments