@@ -30,6 +30,8 @@ export const getPageContent = defineTool<z.ZodRawShape, Context, Response>({
3030 schema : {
3131 tabId : z . coerce . number ( ) . describe ( 'Tab ID to extract content from' ) ,
3232 type : z . enum ( [ 'text' , 'text-with-links' ] ) . describe ( 'Type of content to extract: text or text-with-links' ) ,
33+ page : z . string ( ) . optional ( ) . default ( 'all' ) . describe ( 'Page number to retrieve: "all", "1", "2", etc. (default: "all")' ) ,
34+ contextWindow : z . string ( ) . optional ( ) . default ( '20k' ) . describe ( 'Context window size for pagination: "20k", "30k", "50k", "100k" (default: "20k")' ) ,
3335 options : z
3436 . object ( {
3537 context : z . enum ( [ 'visible' , 'full' ] ) . optional ( ) . describe ( 'Extract from visible viewport or full page (default: visible)' ) ,
@@ -47,11 +49,24 @@ export const getPageContent = defineTool<z.ZodRawShape, Context, Response>({
4749 const params = request . params as {
4850 tabId : number ;
4951 type : 'text' | 'text-with-links' ;
52+ page ?: string ;
53+ contextWindow ?: string ;
5054 options ?: { context ?: 'visible' | 'full' ; includeSections ?: string [ ] } ;
5155 } ;
5256
5357 try {
5458 const includeLinks = params . type === 'text-with-links' ;
59+ const requestedPage = params . page || 'all' ;
60+ const contextWindowStr = params . contextWindow || '20k' ;
61+
62+ // Parse context window size
63+ const parseContextWindow = ( cw : string ) : number => {
64+ const match = cw . match ( / ^ ( \d + ) k $ / i) ;
65+ if ( ! match ) return 20000 ; // default 20k
66+ return parseInt ( match [ 1 ] ) * 1000 ;
67+ } ;
68+
69+ const contextWindowSize = parseContextWindow ( contextWindowStr ) ;
5570
5671 const snapshotResult = await context . executeAction ( 'getSnapshot' , {
5772 tabId : params . tabId ,
@@ -63,25 +78,62 @@ export const getPageContent = defineTool<z.ZodRawShape, Context, Response>({
6378 return ;
6479 }
6580
66- let pageContent = '' ;
67-
81+ // Build full content
82+ let fullContent = '' ;
6883 snapshot . items . forEach ( ( item ) => {
6984 if ( item . type === 'heading' ) {
7085 const prefix = '#' . repeat ( item . level || 1 ) ;
71- pageContent += `${ prefix } ${ item . text } \n` ;
86+ fullContent += `${ prefix } ${ item . text } \n` ;
7287 } else if ( item . type === 'text' ) {
73- pageContent += `${ item . text } \n` ;
88+ fullContent += `${ item . text } \n` ;
7489 } else if ( item . type === 'link' && includeLinks ) {
75- pageContent += `[${ item . text } ](${ item . url } )\n` ;
90+ fullContent += `[${ item . text } ](${ item . url } )\n` ;
7691 }
7792 } ) ;
7893
79- if ( pageContent ) {
80- response . appendResponseLine ( pageContent . trim ( ) ) ;
94+ if ( ! fullContent ) {
95+ response . appendResponseLine ( 'No content extracted.' ) ;
96+ return ;
97+ }
98+
99+ // Split content into pages
100+ const pages : string [ ] = [ ] ;
101+ let currentPage = '' ;
102+ const lines = fullContent . split ( '\n' ) ;
103+
104+ for ( const line of lines ) {
105+ if ( ( currentPage + line + '\n' ) . length > contextWindowSize && currentPage . length > 0 ) {
106+ pages . push ( currentPage . trim ( ) ) ;
107+ currentPage = '' ;
108+ }
109+ currentPage += line + '\n' ;
110+ }
111+ if ( currentPage . trim ( ) ) {
112+ pages . push ( currentPage . trim ( ) ) ;
113+ }
114+
115+ const totalPages = pages . length ;
116+
117+ // Return requested page(s)
118+ if ( requestedPage === 'all' ) {
119+ response . appendResponseLine ( `Total pages: ${ totalPages } (${ contextWindowStr } per page)` ) ;
81120 response . appendResponseLine ( '' ) ;
82- response . appendResponseLine ( `(${ pageContent . length } characters)` ) ;
121+ response . appendResponseLine ( fullContent . trim ( ) ) ;
122+ response . appendResponseLine ( '' ) ;
123+ response . appendResponseLine ( `(${ fullContent . length } characters total)` ) ;
83124 } else {
84- response . appendResponseLine ( 'No content extracted.' ) ;
125+ const pageNum = parseInt ( requestedPage ) ;
126+ if ( isNaN ( pageNum ) || pageNum < 1 || pageNum > totalPages ) {
127+ response . appendResponseLine ( `Error: Invalid page number "${ requestedPage } ". Valid pages: 1-${ totalPages } or "all"` ) ;
128+ return ;
129+ }
130+
131+ const pageIndex = pageNum - 1 ;
132+ response . appendResponseLine ( `Page ${ pageNum } of ${ totalPages } (${ contextWindowStr } limit per page)` ) ;
133+ response . appendResponseLine ( '' ) ;
134+ response . appendResponseLine ( pages [ pageIndex ] ) ;
135+ response . appendResponseLine ( '' ) ;
136+ response . appendResponseLine ( `(${ pages [ pageIndex ] . length } characters)` ) ;
85137 }
86138
87139 response . appendResponseLine ( '' ) ;
0 commit comments