forked from Michealshodipo56/Ted
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_reader.py
More file actions
63 lines (50 loc) · 2.16 KB
/
Copy pathfile_reader.py
File metadata and controls
63 lines (50 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
============================================================================
FILE READER MODULE — Headless Buffer Ingestion
============================================================================
Allows Ted to read local files directly into the conversation context
window, bypassing terminal copy-paste limits. Handles token counting
and automatic truncation for massive files.
============================================================================
"""
import os
from pathlib import Path
from typing import Tuple
class FileReader:
"""Reads files and formats them for context injection."""
def __init__(self, max_tokens: int = 8000):
self.max_tokens = max_tokens
def read_file(self, filepath: str) -> Tuple[bool, str, int, int]:
"""
Reads a file from disk.
Returns (success, content, line_count, estimated_tokens).
"""
path = Path(filepath).expanduser().resolve()
if not path.exists():
return False, f"File not found: {path}", 0, 0
if not path.is_file():
return False, f"Path is a directory, not a file: {path}", 0, 0
try:
content = path.read_text(encoding="utf-8")
except UnicodeDecodeError:
return False, f"Cannot read binary file: {path}", 0, 0
except Exception as e:
return False, f"Error reading file {path}: {str(e)}", 0, 0
lines = content.splitlines()
line_count = len(lines)
# Rough token estimation (1 token ≈ 4 chars)
estimated_tokens = len(content) // 4
# Truncate if too massive
if estimated_tokens > self.max_tokens:
allowed_chars = self.max_tokens * 4
content = content[:allowed_chars]
content += f"\n\n[... FILE TRUNCATED: Exceeded {self.max_tokens} tokens ...]"
estimated_tokens = self.max_tokens
formatted_content = (
f"--- FILE CONTENT: {path.name} ---\n"
f"Path: {path}\n"
f"Lines: {line_count}\n"
f"```\n{content}\n```\n"
f"--- END OF FILE ---\n"
)
return True, formatted_content, line_count, estimated_tokens