feat(api): manifest formatter with token-budget truncation
This commit is contained in:
@@ -60,6 +60,41 @@ def _language_instruction(context: dict[str, Any]) -> str:
|
||||
f"All your output text must be written in {lang}."
|
||||
)
|
||||
|
||||
MANIFEST_TOKEN_BUDGET = 3000 # rough budget for <linked_folder> block
|
||||
|
||||
|
||||
def format_folder_manifest(manifest: dict | None) -> str:
|
||||
"""Format a folder manifest into the <linked_folder> block.
|
||||
|
||||
Truncates by mtime DESC if estimated tokens exceed MANIFEST_TOKEN_BUDGET.
|
||||
Returns empty string if manifest is None or has no files.
|
||||
"""
|
||||
if not manifest or not manifest.get("files"):
|
||||
return ""
|
||||
files = list(manifest["files"])
|
||||
files.sort(key=lambda f: f.get("mtimeMs", 0), reverse=True)
|
||||
|
||||
header = (
|
||||
f"<linked_folder>\npath: {manifest.get('folderPath', '?')} "
|
||||
f"({len(files)} files, scanned {manifest.get('lastScannedAt', '?')})\nfiles:\n"
|
||||
)
|
||||
footer_template = "… {} more files omitted, use read_project_folder_file to access by path\n</linked_folder>"
|
||||
|
||||
char_budget = MANIFEST_TOKEN_BUDGET * 4 # ~4 chars/token
|
||||
body = ""
|
||||
included = 0
|
||||
for f in files:
|
||||
line = f"- /{f['relPath']} [{f.get('kind','text')}] {f.get('summary','')}\n"
|
||||
if len(header) + len(body) + len(line) + len(footer_template.format(0)) > char_budget:
|
||||
break
|
||||
body += line
|
||||
included += 1
|
||||
omitted = len(files) - included
|
||||
if omitted > 0:
|
||||
return header + body + footer_template.format(omitted)
|
||||
return header + body + "</linked_folder>"
|
||||
|
||||
|
||||
def _datetime_context_injection(context: dict[str, Any]) -> str:
|
||||
"""Build a comprehensive DATE CONTEXT block with pre-computed ms-epoch boundaries for common ranges."""
|
||||
fp = context.get("format_prefs")
|
||||
|
||||
Reference in New Issue
Block a user