get_text_context

This commit is contained in:
2026-07-23 14:38:45 -07:00
parent 03e47bad42
commit 504794f9a8
4 changed files with 128 additions and 27 deletions
+1 -27
View File
@@ -6,36 +6,10 @@ import datetime
from pathlib import Path
from typing import Any, Dict, List, Tuple
from PIL import Image, ImageOps
from tools.utils import format_relative_time, ToolError, get_file_info_list, sort_file_list
from tools.utils import format_relative_time, ToolError, get_file_info_list, sort_file_list, parse_indices
logger = logging.getLogger("MattCP")
def parse_indices(indices_str: str) -> List[int]:
"""
Parses a string like '11, 31-33, 44' into a list of 1-based indices.
Preserves the order specified in the string.
"""
indices = []
parts = [p.strip() for p in indices_str.split(',')]
for part in parts:
if not part:
continue
if '-' in part:
try:
start, end = map(int, part.split('-'))
# Expand range in order. Handle reverse ranges as well.
step = 1 if start <= end else -1
for i in range(start, end + step, step):
indices.append(i)
except ValueError:
raise ToolError(f"Invalid range format: {part}")
else:
try:
indices.append(int(part))
except ValueError:
raise ToolError(f"Invalid index format: {part}")
return indices
def calculate_patch_dimensions(orig_w: int, orig_h: int, target_tokens: int = 70):
"""
Calculates optimal pixel dimensions to hit a token budget of at least target_tokens.