@@ -10,9 +10,10 @@ import {zod} from '../third_party/index.js';
1010import type { ElementHandle , KeyInput } from '../third_party/index.js' ;
1111import type { TextSnapshotNode } from '../types.js' ;
1212import { parseKey } from '../utils/keyboard.js' ;
13+ import type { WaitForEventsResult } from '../WaitForHelper.js' ;
1314
1415import { ToolCategory } from './categories.js' ;
15- import type { ContextPage } from './ToolDefinition.js' ;
16+ import type { ContextPage , Response } from './ToolDefinition.js' ;
1617import { definePageTool } from './ToolDefinition.js' ;
1718
1819const dblClickSchema = zod
@@ -42,6 +43,16 @@ function handleActionError(error: unknown, uid: string) {
4243 ) ;
4344}
4445
46+ function appendNavigationIfAny (
47+ page : ContextPage ,
48+ response : Response ,
49+ result : WaitForEventsResult ,
50+ ) {
51+ if ( result . navigated ) {
52+ response . appendResponseLine ( `Page navigated to ${ page . pptrPage . url ( ) } .` ) ;
53+ }
54+ }
55+
4556export const click = definePageTool ( {
4657 name : 'click' ,
4758 description : `Clicks on the provided element` ,
@@ -62,7 +73,7 @@ export const click = definePageTool({
6273 const uid = request . params . uid ;
6374 const handle = await request . page . getElementByUid ( uid ) ;
6475 try {
65- await request . page . waitForEventsAfterAction ( async ( ) => {
76+ const result = await request . page . waitForEventsAfterAction ( async ( ) => {
6677 await handle . asLocator ( ) . click ( {
6778 count : request . params . dblClick ? 2 : 1 ,
6879 } ) ;
@@ -72,6 +83,7 @@ export const click = definePageTool({
7283 ? `Successfully double clicked on the element`
7384 : `Successfully clicked on the element` ,
7485 ) ;
86+ appendNavigationIfAny ( request . page , response , result ) ;
7587 if ( request . params . includeSnapshot ) {
7688 response . includeSnapshot ( ) ;
7789 }
@@ -99,7 +111,7 @@ export const clickAt = definePageTool({
99111 } ,
100112 handler : async ( request , response ) => {
101113 const page = request . page ;
102- await page . waitForEventsAfterAction ( async ( ) => {
114+ const result = await page . waitForEventsAfterAction ( async ( ) => {
103115 await page . pptrPage . mouse . click ( request . params . x , request . params . y , {
104116 clickCount : request . params . dblClick ? 2 : 1 ,
105117 } ) ;
@@ -109,6 +121,7 @@ export const clickAt = definePageTool({
109121 ? `Successfully double clicked at the coordinates`
110122 : `Successfully clicked at the coordinates` ,
111123 ) ;
124+ appendNavigationIfAny ( page , response , result ) ;
112125 if ( request . params . includeSnapshot ) {
113126 response . includeSnapshot ( ) ;
114127 }
@@ -134,10 +147,11 @@ export const hover = definePageTool({
134147 const uid = request . params . uid ;
135148 const handle = await request . page . getElementByUid ( uid ) ;
136149 try {
137- await request . page . waitForEventsAfterAction ( async ( ) => {
150+ const result = await request . page . waitForEventsAfterAction ( async ( ) => {
138151 await handle . asLocator ( ) . hover ( ) ;
139152 } ) ;
140153 response . appendResponseLine ( `Successfully hovered over the element` ) ;
154+ appendNavigationIfAny ( request . page , response , result ) ;
141155 if ( request . params . includeSnapshot ) {
142156 response . includeSnapshot ( ) ;
143157 }
@@ -235,7 +249,7 @@ export const fill = definePageTool({
235249 } ,
236250 handler : async ( request , response , context ) => {
237251 const page = request . page ;
238- await page . waitForEventsAfterAction ( async ( ) => {
252+ const result = await page . waitForEventsAfterAction ( async ( ) => {
239253 await fillFormElement (
240254 request . params . uid ,
241255 request . params . value ,
@@ -244,6 +258,7 @@ export const fill = definePageTool({
244258 ) ;
245259 } ) ;
246260 response . appendResponseLine ( `Successfully filled out the element` ) ;
261+ appendNavigationIfAny ( page , response , result ) ;
247262 if ( request . params . includeSnapshot ) {
248263 response . includeSnapshot ( ) ;
249264 }
@@ -263,7 +278,7 @@ export const typeText = definePageTool({
263278 } ,
264279 handler : async ( request , response ) => {
265280 const page = request . page ;
266- await page . waitForEventsAfterAction ( async ( ) => {
281+ const result = await page . waitForEventsAfterAction ( async ( ) => {
267282 await page . pptrPage . keyboard . type ( request . params . text ) ;
268283 if ( request . params . submitKey ) {
269284 await page . pptrPage . keyboard . press (
@@ -274,6 +289,7 @@ export const typeText = definePageTool({
274289 response . appendResponseLine (
275290 `Typed text "${ request . params . text } ${ request . params . submitKey ? ` + ${ request . params . submitKey } ` : '' } "` ,
276291 ) ;
292+ appendNavigationIfAny ( page , response , result ) ;
277293 } ,
278294} ) ;
279295
@@ -295,12 +311,13 @@ export const drag = definePageTool({
295311 ) ;
296312 const toHandle = await request . page . getElementByUid ( request . params . to_uid ) ;
297313 try {
298- await request . page . waitForEventsAfterAction ( async ( ) => {
314+ const result = await request . page . waitForEventsAfterAction ( async ( ) => {
299315 await fromHandle . drag ( toHandle ) ;
300316 await new Promise ( resolve => setTimeout ( resolve , 50 ) ) ;
301317 await toHandle . drop ( fromHandle ) ;
302318 } ) ;
303319 response . appendResponseLine ( `Successfully dragged an element` ) ;
320+ appendNavigationIfAny ( request . page , response , result ) ;
304321 if ( request . params . includeSnapshot ) {
305322 response . includeSnapshot ( ) ;
306323 }
@@ -332,8 +349,9 @@ export const fillForm = definePageTool({
332349 } ,
333350 handler : async ( request , response , context ) => {
334351 const page = request . page ;
352+ let lastResult : WaitForEventsResult = { navigated : false } ;
335353 for ( const element of request . params . elements ) {
336- await page . waitForEventsAfterAction ( async ( ) => {
354+ lastResult = await page . waitForEventsAfterAction ( async ( ) => {
337355 await fillFormElement (
338356 element . uid ,
339357 element . value ,
@@ -343,6 +361,7 @@ export const fillForm = definePageTool({
343361 } ) ;
344362 }
345363 response . appendResponseLine ( `Successfully filled out the form` ) ;
364+ appendNavigationIfAny ( page , response , lastResult ) ;
346365 if ( request . params . includeSnapshot ) {
347366 response . includeSnapshot ( ) ;
348367 }
@@ -419,7 +438,7 @@ export const pressKey = definePageTool({
419438 const tokens = parseKey ( request . params . key ) ;
420439 const [ key , ...modifiers ] = tokens ;
421440
422- await page . waitForEventsAfterAction ( async ( ) => {
441+ const result = await page . waitForEventsAfterAction ( async ( ) => {
423442 for ( const modifier of modifiers ) {
424443 await page . pptrPage . keyboard . down ( modifier ) ;
425444 }
@@ -432,6 +451,7 @@ export const pressKey = definePageTool({
432451 response . appendResponseLine (
433452 `Successfully pressed key: ${ request . params . key } ` ,
434453 ) ;
454+ appendNavigationIfAny ( page , response , result ) ;
435455 if ( request . params . includeSnapshot ) {
436456 response . includeSnapshot ( ) ;
437457 }
0 commit comments