20 lines
1.1 KiB
Python
20 lines
1.1 KiB
Python
|
|
"""Tool: enforce raw-output-to-file policy and compact large outputs."""
|
||
|
|
from helpers.tool import Tool, Response
|
||
|
|
from usr.plugins.a0_software_orchestrator.helpers.context_budget import estimate_tokens, estimate_ratio, should_warn, should_hard_compact, compact_text
|
||
|
|
from usr.plugins.a0_software_orchestrator.helpers.safety_rules import redact_secrets
|
||
|
|
import os
|
||
|
|
|
||
|
|
class ContextCompactor(Tool):
|
||
|
|
async def execute(self, text: str = "", max_lines: int = 80, max_tokens: int = 4096, **kwargs):
|
||
|
|
if not text:
|
||
|
|
return Response(message="No text provided", break_loop=False)
|
||
|
|
text = redact_secrets(text)
|
||
|
|
tokens = estimate_tokens(text)
|
||
|
|
ratio = estimate_ratio(tokens, max_tokens)
|
||
|
|
if should_hard_compact(ratio):
|
||
|
|
compacted = compact_text(text, max_lines)
|
||
|
|
return Response(message=f"COMPACTED ({tokens} tokens, ratio {ratio:.2f}):\n{compacted}", break_loop=False)
|
||
|
|
elif should_warn(ratio):
|
||
|
|
return Response(message=f"WARNING: {tokens} tokens (ratio {ratio:.2f}). Consider compacting.", break_loop=False)
|
||
|
|
return Response(message=f"OK: {tokens} tokens (ratio {ratio:.2f})", break_loop=False)
|