2025-04-04 15:22:00 -07:00
/ * *
* Copyright ( c ) Microsoft Corporation .
*
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* /
import { z } from 'zod' ;
2025-07-22 07:53:33 -07:00
import { defineTabTool } from './tool.js' ;
2025-07-16 16:40:00 -07:00
import { elementSchema } from './snapshot.js' ;
import { generateLocator } from './utils.js' ;
import * as javascript from '../javascript.js' ;
2025-07-22 07:53:33 -07:00
const pressKey = defineTabTool ( {
2025-04-04 17:14:30 -07:00
capability : 'core' ,
2025-04-16 19:36:48 -07:00
2025-04-04 15:22:00 -07:00
schema : {
name : 'browser_press_key' ,
2025-05-05 17:38:22 -07:00
title : 'Press a key' ,
2025-04-04 15:22:00 -07:00
description : 'Press a key on the keyboard' ,
2025-04-22 13:24:38 +02:00
inputSchema : z.object ( {
key : z.string ( ) . describe ( 'Name of the key to press or a character to generate, such as `ArrowLeft` or `a`' ) ,
} ) ,
2025-05-05 17:38:22 -07:00
type : 'destructive' ,
2025-04-04 15:22:00 -07:00
} ,
2025-04-16 19:36:48 -07:00
2025-07-22 07:53:33 -07:00
handle : async ( tab , params ) = > {
2025-04-16 19:36:48 -07:00
const code = [
2025-04-22 13:24:38 +02:00
` // Press ${ params . key } ` ,
` await page.keyboard.press(' ${ params . key } '); ` ,
2025-04-16 19:36:48 -07:00
] ;
2025-04-22 13:24:38 +02:00
const action = ( ) = > tab . page . keyboard . press ( params . key ) ;
2025-04-16 19:36:48 -07:00
return {
code ,
action ,
2025-07-16 16:40:00 -07:00
captureSnapshot : true ,
2025-04-16 19:36:48 -07:00
waitForNetwork : true
} ;
2025-04-04 15:22:00 -07:00
} ,
} ) ;
2025-07-16 16:40:00 -07:00
const typeSchema = elementSchema . extend ( {
text : z.string ( ) . describe ( 'Text to type into the element' ) ,
submit : z.boolean ( ) . optional ( ) . describe ( 'Whether to submit entered text (press Enter after)' ) ,
slowly : z.boolean ( ) . optional ( ) . describe ( 'Whether to type one character at a time. Useful for triggering key handlers in the page. By default entire text is filled in at once.' ) ,
} ) ;
2025-07-22 07:53:33 -07:00
const type = defineTabTool ( {
2025-07-16 16:40:00 -07:00
capability : 'core' ,
schema : {
name : 'browser_type' ,
title : 'Type text' ,
description : 'Type text into editable element' ,
inputSchema : typeSchema ,
type : 'destructive' ,
} ,
2025-07-22 07:53:33 -07:00
handle : async ( tab , params ) = > {
const locator = await tab . refLocator ( params ) ;
2025-07-16 16:40:00 -07:00
const code : string [ ] = [ ] ;
const steps : ( ( ) = > Promise < void > ) [ ] = [ ] ;
if ( params . slowly ) {
code . push ( ` // Press " ${ params . text } " sequentially into " ${ params . element } " ` ) ;
code . push ( ` await page. ${ await generateLocator ( locator ) } .pressSequentially( ${ javascript . quote ( params . text ) } ); ` ) ;
steps . push ( ( ) = > locator . pressSequentially ( params . text ) ) ;
} else {
code . push ( ` // Fill " ${ params . text } " into " ${ params . element } " ` ) ;
code . push ( ` await page. ${ await generateLocator ( locator ) } .fill( ${ javascript . quote ( params . text ) } ); ` ) ;
steps . push ( ( ) = > locator . fill ( params . text ) ) ;
}
if ( params . submit ) {
code . push ( ` // Submit text ` ) ;
code . push ( ` await page. ${ await generateLocator ( locator ) } .press('Enter'); ` ) ;
steps . push ( ( ) = > locator . press ( 'Enter' ) ) ;
}
return {
code ,
action : ( ) = > steps . reduce ( ( acc , step ) = > acc . then ( step ) , Promise . resolve ( ) ) ,
captureSnapshot : true ,
waitForNetwork : true ,
} ;
} ,
} ) ;
export default [
pressKey ,
type ,
2025-04-04 15:22:00 -07:00
] ;