resolveInteractiveFileReference function
- String text, {
- InputPromptFileFactory fileOf = _defaultFileOf,
Resolves an interactive prompt that starts with a pasted file path.
The first whitespace-delimited token is treated as a file reference
when it looks path-like (/…, ~/, ./, ../) AND names an
existing file. It becomes an explicit [attached file: …] path
reference — the marker headless mode uses too — annotated with locally
measured stats so the model can judge the cost BEFORE reading:
[attached file: /logs/build.log (97.7 KB · 3412 lines · ~8500
tokens est.) — read it with your tools]
Size formats as human-readable B/KB/MB; decodable text additionally
reports line count and the repo's standard ~4-chars-per-token estimate
(the compaction heuristic in token_estimation.dart). Content itself
is deliberately NEVER inlined — pasting happens before anyone looked
at the size, so the model decides whether and how much to read with
its tools (read takes line-range selectors). For files over
_attachStatsReadCap the content is not read even for statistics;
the size speaks for itself. Everything typed after the path rides
along as the instruction.
Conservative by design: a token without a path-like prefix is never converted (plain sentences and bare words stay untouched), and input with no such leading reference returns unchanged.
Implementation
String resolveInteractiveFileReference(
String text, {
InputPromptFileFactory fileOf = _defaultFileOf,
}) {
final token = _leadingPathLikeToken(text);
if (token == null) return text;
final file = fileOf(token);
try {
if (!file.existsSync()) return text;
} on Object {
return text;
}
var length = 0;
try {
length = file.lengthSync();
} on Object {
// Stats are best-effort; an unreadable length still attaches.
}
final textStats = _attachTextStats(file, length);
final stats = [_formatAttachBytes(length), ?textStats].join(' · ');
final trailing = text.trimLeft().substring(token.length).trim();
final body =
'[attached file: ${file.absolute.path} ($stats)'
' — read it with your tools]';
return trailing.isEmpty ? body : '$body\n\n$trailing';
}