@@ -98,9 +98,8 @@ export function createBrowserToolExecutor(params: {
9898} ) : NonNullable < AnyAgentTool [ "execute" ] > {
9999 const { opts, binding, capabilities } = params ;
100100 return async ( _toolCallId , args , signal ) => {
101- const publicParams = binding
102- ? applyBrowserTabToolBinding ( args as Record < string , unknown > , binding )
103- : ( args as Record < string , unknown > ) ;
101+ const inputParams = asNullableRecord ( args ) ?? { } ;
102+ const publicParams = binding ? applyBrowserTabToolBinding ( inputParams , binding ) : inputParams ;
104103 const approved = isBrowserStewardRuntimeApproved ( publicParams , opts ?. approvalAuthority ) ;
105104 const browserStewardGatewayApproval = approved
106105 ? ( request : Parameters < typeof createBrowserStewardGatewayApprovalClaim > [ 0 ] ) =>
@@ -140,11 +139,11 @@ export function createBrowserToolExecutor(params: {
140139 const requestedProfile = readStringParam ( effectiveParams , "profile" ) ;
141140 const requestedNode = readStringParam ( effectiveParams , "node" ) ;
142141 const requestedTimeoutMs = readToolTimeoutMs ( effectiveParams ) ;
143- let target = readStringParam ( effectiveParams , "target" ) as
144- | "sandbox"
145- | "host"
146- | "node"
147- | undefined ;
142+ const requestedTarget = readStringParam ( effectiveParams , "target" ) ;
143+ let target : "sandbox" | "host" | "node" | undefined ;
144+ if ( requestedTarget === "sandbox" || requestedTarget === "host" || requestedTarget === "node" ) {
145+ target = requestedTarget ;
146+ }
148147 const runtimeConfig = getRuntimeConfig ( ) ;
149148 const resolvedBrowser = resolveBrowserConfig ( runtimeConfig . browser , runtimeConfig ) ;
150149 const effectiveProfile = requestedProfile ?? resolvedBrowser . defaultProfile ;
@@ -317,8 +316,7 @@ export function createBrowserToolExecutor(params: {
317316 ? await proxyRequest ( { method : "POST" , path, profile, body } )
318317 : await runLocal ( ) ;
319318 sessionTabs . touch (
320- readStringValue ( ( result as { targetId ?: unknown } ) . targetId ) ??
321- readStringValue ( body . targetId ) ,
319+ readStringValue ( asNullableRecord ( result ) ?. targetId ) ?? readStringValue ( body . targetId ) ,
322320 ) ;
323321 return jsonResult ( result ) ;
324322 } ;
@@ -400,7 +398,7 @@ export function createBrowserToolExecutor(params: {
400398 timeoutMs : toolTimeoutMs ,
401399 signal,
402400 } ) ;
403- sessionTabs . touch ( readStringValue ( ( result as { targetId ?: unknown } ) . targetId ) ?? targetId ) ;
401+ sessionTabs . touch ( readStringValue ( asNullableRecord ( result ) ? .targetId ) ?? targetId ) ;
404402 return jsonResult ( result ) ;
405403 }
406404 case "close" : {
@@ -420,9 +418,7 @@ export function createBrowserToolExecutor(params: {
420418 body : { kind : "close" } ,
421419 timeoutMs : toolTimeoutMs ,
422420 } ) ;
423- sessionTabs . untrack (
424- readStringValue ( ( result as { targetId ?: unknown } ) . targetId ) ?? targetId ,
425- ) ;
421+ sessionTabs . untrack ( readStringValue ( asNullableRecord ( result ) ?. targetId ) ?? targetId ) ;
426422 return jsonResult ( result ) ;
427423 }
428424 const result = targetId
@@ -489,16 +485,15 @@ export function createBrowserToolExecutor(params: {
489485 profile,
490486 signal,
491487 } ) ;
492- const navigatedTargetId =
493- readStringValue ( ( result as { targetId ?: unknown } ) . targetId ) ?? targetId ;
488+ const navigatedTargetId = readStringValue ( asNullableRecord ( result ) ?. targetId ) ?? targetId ;
494489 sessionTabs . touch ( navigatedTargetId ) ;
495490 const formatted = formatBrowserExternalToolResult ( {
496- kind : ( result as { download ?: unknown } ) . download ? "download" : "act" ,
491+ kind : asNullableRecord ( result ) ? .download ? "download" : "act" ,
497492 payload : result ,
498493 } ) ;
499494 // A navigation that resolved to a download leaves the document
500495 // unchanged, so inline page state would describe the wrong thing.
501- if ( ( result as { download ?: unknown } ) . download ) {
496+ if ( asNullableRecord ( result ) ? .download ) {
502497 return formatted ;
503498 }
504499 return await appendNavigatedPageState ( {
@@ -519,9 +514,7 @@ export function createBrowserToolExecutor(params: {
519514 signal,
520515 } ) ;
521516 const targetId = readStringParam ( effectiveParams , "targetId" ) ;
522- const canonicalTargetId = readStringValue (
523- ( result . details as { targetId ?: unknown } | undefined ) ?. targetId ,
524- ) ;
517+ const canonicalTargetId = readStringValue ( asNullableRecord ( result . details ) ?. targetId ) ;
525518 sessionTabs . touch ( canonicalTargetId ?? targetId ) ;
526519 return result ;
527520 }
@@ -551,16 +544,21 @@ export function createBrowserToolExecutor(params: {
551544 case "pdf" : {
552545 const targetId = normalizeOptionalString ( effectiveParams . targetId ) ;
553546 const result = proxyRequest
554- ? ( ( await proxyRequest ( {
547+ ? await proxyRequest ( {
555548 method : "POST" ,
556549 path : "/pdf" ,
557550 profile,
558551 body : { targetId } ,
559- } ) ) as Awaited < ReturnType < typeof browserPdfSave > > )
552+ } )
560553 : await browserPdfSave ( baseUrl , { targetId, profile, signal } ) ;
561- sessionTabs . touch ( readStringValue ( result . targetId ) ?? targetId ) ;
554+ const resultRecord = asNullableRecord ( result ) ;
555+ const resultPath = readStringValue ( resultRecord ?. path ) ;
556+ if ( ! resultPath ) {
557+ throw new Error ( "browser PDF response missing path" ) ;
558+ }
559+ sessionTabs . touch ( readStringValue ( resultRecord ?. targetId ) ?? targetId ) ;
562560 return {
563- content : [ { type : "text" as const , text : `FILE:${ result . path } ` } ] ,
561+ content : [ { type : "text" as const , text : `FILE:${ resultPath } ` } ] ,
564562 details : result ,
565563 } ;
566564 }
0 commit comments