MCPcopy Index your code
hub / github.com/OpenBMB/ChatDev / read_text_file_snippet

Function read_text_file_snippet

functions/function_calling/file.py:412–444  ·  view source on GitHub ↗

Read a snippet of a workspace text file without loading entire content.

(
    path: str,
    *,
    offset: int = 0,
    limit: int = 4000,
    encoding: str = "utf-8",
    _context: Dict[str, Any] | None = None,
)

Source from the content-addressed store, hash-verified

410
411
412def read_text_file_snippet(
413 path: str,
414 *,
415 offset: int = 0,
416 limit: int = 4000,
417 encoding: str = "utf-8",
418 _context: Dict[str, Any] | None = None,
419) -> Dict[str, Any]:
420 """Read a snippet of a workspace text file without loading entire content."""
421
422 ctx = FileToolContext(_context)
423 target: Path | None = None
424 try:
425 candidate = ctx.resolve_under_workspace(path)
426 except ValueError:
427 candidate = None
428
429 if candidate and candidate.exists() and candidate.is_file():
430 target = candidate
431
432 if target is None:
433 target = ctx.resolve_under_session(path)
434 if not target.exists() or not target.is_file():
435 raise ValueError(f"File not found in session attachments/workspace: {path}")
436
437 data = target.read_text(encoding=encoding, errors="replace")
438 snippet = data[offset : offset + limit]
439 return {
440 "snippet": snippet,
441 "truncated": offset + limit < len(data),
442 "length": len(data),
443 "offset": offset,
444 }
445
446
447def read_file_segment(

Callers

nothing calls this directly

Calls 3

resolve_under_sessionMethod · 0.95
FileToolContextClass · 0.85

Tested by

no test coverage detected