44 * SPDX-License-Identifier: Apache-2.0
55 */
66
7- import { zod , ajv , type JSONSchema7 } from '../third_party/index.js' ;
7+ import {
8+ zod ,
9+ ajv ,
10+ type JSONSchema7 ,
11+ type ElementHandle ,
12+ } from '../third_party/index.js' ;
813
914import { ToolCategory } from './categories.js' ;
1015import { definePageTool } from './ToolDefinition.js' ;
@@ -87,6 +92,22 @@ export const executeInPageTool = definePageTool({
8792 }
8893 }
8994
95+ // Creates array of ElementHandles from the UIDs in the params.
96+ // We do not replace the uids with the ElementsHandles yet, because
97+ // the `evaluate` function only turns them into DOM elements if they
98+ // are passed as non-nested arguments.
99+ const handles : ElementHandle [ ] = [ ] ;
100+ for ( const value of Object . values ( params ) ) {
101+ if (
102+ value instanceof Object &&
103+ 'uid' in value &&
104+ typeof value . uid === 'string' &&
105+ Object . keys ( value ) . length === 1
106+ ) {
107+ handles . push ( await request . page . getElementByUid ( value . uid ) ) ;
108+ }
109+ }
110+
90111 const toolGroup = request . page . getInPageTools ( ) ;
91112 const tool = toolGroup ?. tools . find ( t => t . name === toolName ) ;
92113 if ( ! tool ) {
@@ -102,7 +123,19 @@ export const executeInPageTool = definePageTool({
102123 }
103124
104125 const result = await request . page . pptrPage . evaluate (
105- async ( name , args ) => {
126+ async ( name , args , ...elements ) => {
127+ // Replace the UIDs with DOM elements.
128+ for ( const [ key , value ] of Object . entries ( args ) ) {
129+ if (
130+ value instanceof Object &&
131+ 'uid' in value &&
132+ typeof value . uid === 'string' &&
133+ Object . keys ( value ) . length === 1
134+ ) {
135+ args [ key ] = elements . shift ( ) ;
136+ }
137+ }
138+
106139 if ( ! window . __dtmcp ?. executeTool ) {
107140 throw new Error ( 'No tools found on the page' ) ;
108141 }
@@ -114,6 +147,7 @@ export const executeInPageTool = definePageTool({
114147 } ,
115148 toolName ,
116149 params ,
150+ ...handles ,
117151 ) ;
118152 response . appendResponseLine ( JSON . stringify ( result , null , 2 ) ) ;
119153 } ,
0 commit comments