Overview
Theread tool provides flexible file reading capabilities for agents. It handles text files with line numbering, images as base64-encoded content, and PDFs as base64-encoded documents. The tool includes intelligent truncation and binary detection.
Tool Definition
Absolute path to the file to read. Relative paths are not supported.
0-based line number to start reading from. Only applies to text files. Useful for reading large files in chunks.
Maximum number of lines to read from text files. Default is 2000 lines.
Return Value
Text Files
Returns a string context with:- XML-wrapped file content with metadata
- Line numbers (1-based display)
- Truncation indicators if applicable
Images
Returns an array containing anImageContentPart:
type: “image”mediaType: MIME type (image/png, image/jpeg, etc.)data: Base64-encoded image data
PDFs
Returns an array containing aDocumentContentPart:
type: “document”mediaType: “application/pdf”data: Base64-encoded PDF data
Behavior
File Type Detection
The tool determines file type based on extension: Supported Image Extensions:.png→ image/png.jpg,.jpeg→ image/jpeg.gif→ image/gif.webp→ image/webp
.pdf→ application/pdf
Size Limits
- Text files: 50KB of output (after formatting with line numbers)
- Images and PDFs: 5MB maximum file size
Binary Detection
For non-image, non-PDF files:- Reads the file into a buffer
- Checks the first 8KB for null bytes (0x00)
- If null bytes found, rejects as binary
- Otherwise, processes as UTF-8 text
Line Truncation
For text files:- Lines longer than 2000 characters are truncated with
[truncated]suffix - Output stops if total formatted content exceeds 50KB
- Truncation message indicates use of offset/limit for continuation
Line Number Formatting
Text output uses 1-based line numbers with consistent formatting:FileTime Tracking
The tool records read timestamps viafileTime.read(filePath) for conflict detection with the patch tool.
Usage Examples
Reading a Complete Text File
Reading a File in Chunks
Reading an Image
Reading a PDF
Handling Errors
Implementation Details
The tool is defined inpackages/ai/tools/read.ts.
Schema Definition
Constants
Execute Function Flow
- Extract file extension
- Check file existence and size via
stat - If image: Read, encode base64, return ImageContentPart
- If PDF: Read, encode base64, return DocumentContentPart
- If text:
- Read file buffer
- Check for binary content (null bytes)
- Convert to UTF-8 string
- Split into lines
- Apply offset and limit
- Truncate long lines
- Format with line numbers
- Check 50KB output limit
- Wrap in XML with metadata
- Record read timestamp via
fileTime.read() - Return formatted context
Related Tools
- patch - Apply file modifications (uses FileTime for conflict detection)
- bash - Execute commands to locate or process files
- agent - Delegate complex file analysis to subagents
Related Types
ContentPart- Union type for text, image, and document contentImageContentPart- Structure for image dataDocumentContentPart- Structure for PDF dataFileTime- Timestamp tracker for read/write conflict detection
