@@ -185,7 +185,7 @@ func NewViewTool(
187 // Read the file content
187 // Read the file content
188− content, lineCount, err := readTextFile(filePath, params.Offset, params.Limit)
188+ content, hasMore, err := readTextFile(filePath, params.Offset, params.Limit)
189 if err != nil {
189 if err != nil {
190 return fantasy.ToolResponse{}, fmt.Errorf("error reading file: %w", err)
190 return fantasy.ToolResponse{}, fmt.Errorf("error reading file: %w", err)
@@ -195,11 +195,9 @@ func NewViewTool(
196 notifyLSPs(ctx, lspManager, filePath)
196 notifyLSPs(ctx, lspManager, filePath)
197 output := "<file>\n"
197 output := "<file>\n"
198− // Format the output with line numbers
199 output += addLineNumbers(content, params.Offset+1)
198 output += addLineNumbers(content, params.Offset+1)
201− // Add a note if the content was truncated
200+ if hasMore {
202− if lineCount > params.Offset+len(strings.Split(content, "\n")) {
203 output += fmt.Sprintf("\n\n(File has more lines. Use 'offset' parameter to read beyond line %d)",
201 output += fmt.Sprintf("\n\n(File has more lines. Use 'offset' parameter to read beyond line %d)",
204 params.Offset+len(strings.Split(content, "\n")))
202 params.Offset+len(strings.Split(content, "\n")))
@@ -251,38 +249,28 @@ func addLineNumbers(content string, startLine int) string {
251 return strings.Join(result, "\n")
249 return strings.Join(result, "\n")
254−func readTextFile(filePath string, offset, limit int) (string, int, error) {
252+func readTextFile(filePath string, offset, limit int) (string, bool, error) {
255 file, err := os.Open(filePath)
253 file, err := os.Open(filePath)
256 if err != nil {
254 if err != nil {
257− return "", 0, err
255+ return "", false, err
259 defer file.Close()
257 defer file.Close()
263 scanner := NewLineScanner(file)
259 scanner := NewLineScanner(file)
264 if offset > 0 {
260 if offset > 0 {
265− for lineCount < offset && scanner.Scan() {
262+ for skipped < offset && scanner.Scan() {
266− lineCount++
263+ skipped++
268 if err = scanner.Err(); err != nil {
265 if err = scanner.Err(); err != nil {
269− return "", 0, err
266+ return "", false, err
274− _, err = file.Seek(0, io.SeekStart)
280− // Pre-allocate slice with expected capacity
270+ // Pre-allocate slice with expected capacity.
281 lines := make([]string, 0, limit)
271 lines := make([]string, 0, limit)
284 for scanner.Scan() && len(lines) < limit {
273 for scanner.Scan() && len(lines) < limit {
286 lineText := scanner.Text()
274 lineText := scanner.Text()
287 if len(lineText) > MaxLineLength {
275 if len(lineText) > MaxLineLength {
288 lineText = lineText[:MaxLineLength] + "..."
276 lineText = lineText[:MaxLineLength] + "..."
@@ -290,16 +278,14 @@ func readTextFile(filePath string, offset, limit int) (string, int, error) {
290 lines = append(lines, lineText)
278 lines = append(lines, lineText)
293− // Continue scanning to get total line count
281+ // Peek one more line to determine if the file has more content.
294− for scanner.Scan() {
282+ hasMore := scanner.Scan()
298 if err := scanner.Err(); err != nil {
284 if err := scanner.Err(); err != nil {
299− return "", 0, err
285+ return "", false, err
302− return strings.Join(lines, "\n"), lineCount, nil
288+ return strings.Join(lines, "\n"), hasMore, nil
305func getImageMimeType(filePath string) (bool, string) {
291func getImageMimeType(filePath string) (bool, string) {