@@ -18,13 +18,10 @@ export const ReadTool = Tool.define("read", {
1818 description : DESCRIPTION ,
1919 parameters : z . object ( {
2020 filePath : z . string ( ) . describe ( "The absolute path to the file or directory to read" ) ,
21- offset : z . coerce . number ( ) . describe ( "The line number to start reading from (1-indexed) " ) . optional ( ) ,
21+ offset : z . coerce . number ( ) . describe ( "The 0-based line offset to start reading from" ) . optional ( ) ,
2222 limit : z . coerce . number ( ) . describe ( "The maximum number of lines to read (defaults to 2000)" ) . optional ( ) ,
2323 } ) ,
2424 async execute ( params , ctx ) {
25- if ( params . offset !== undefined && params . offset < 1 ) {
26- throw new Error ( "offset must be greater than or equal to 1" )
27- }
2825 let filepath = params . filePath
2926 if ( ! path . isAbsolute ( filepath ) ) {
3027 filepath = path . resolve ( Instance . directory , filepath )
@@ -81,10 +78,9 @@ export const ReadTool = Tool.define("read", {
8178 entries . sort ( ( a , b ) => a . localeCompare ( b ) )
8279
8380 const limit = params . limit ?? DEFAULT_READ_LIMIT
84- const offset = params . offset ?? 1
85- const start = offset - 1
86- const sliced = entries . slice ( start , start + limit )
87- const truncated = start + sliced . length < entries . length
81+ const offset = params . offset || 0
82+ const sliced = entries . slice ( offset , offset + limit )
83+ const truncated = offset + sliced . length < entries . length
8884
8985 const output = [
9086 `<path>${ filepath } </path>` ,
@@ -142,15 +138,13 @@ export const ReadTool = Tool.define("read", {
142138 if ( isBinary ) throw new Error ( `Cannot read binary file: ${ filepath } ` )
143139
144140 const limit = params . limit ?? DEFAULT_READ_LIMIT
145- const offset = params . offset ?? 1
146- const start = offset - 1
141+ const offset = params . offset || 0
147142 const lines = await file . text ( ) . then ( ( text ) => text . split ( "\n" ) )
148- if ( start >= lines . length ) throw new Error ( `Offset ${ offset } is out of range for this file (${ lines . length } lines)` )
149143
150144 const raw : string [ ] = [ ]
151145 let bytes = 0
152146 let truncatedByBytes = false
153- for ( let i = start ; i < Math . min ( lines . length , start + limit ) ; i ++ ) {
147+ for ( let i = offset ; i < Math . min ( lines . length , offset + limit ) ; i ++ ) {
154148 const line = lines [ i ] . length > MAX_LINE_LENGTH ? lines [ i ] . substring ( 0 , MAX_LINE_LENGTH ) + "..." : lines [ i ]
155149 const size = Buffer . byteLength ( line , "utf-8" ) + ( raw . length > 0 ? 1 : 0 )
156150 if ( bytes + size > MAX_BYTES ) {
@@ -162,15 +156,15 @@ export const ReadTool = Tool.define("read", {
162156 }
163157
164158 const content = raw . map ( ( line , index ) => {
165- return `${ index + offset } : ${ line } `
159+ return `${ index + offset + 1 } : ${ line } `
166160 } )
167161 const preview = raw . slice ( 0 , 20 ) . join ( "\n" )
168162
169163 let output = [ `<path>${ filepath } </path>` , `<type>file</type>` , "<content>" ] . join ( "\n" )
170164 output += content . join ( "\n" )
171165
172166 const totalLines = lines . length
173- const lastReadLine = offset + raw . length - 1
167+ const lastReadLine = offset + raw . length
174168 const hasMoreLines = totalLines > lastReadLine
175169 const truncated = hasMoreLines || truncatedByBytes
176170
0 commit comments