kiln_ai.utils.formatting
1import re 2 3AGENT_TRUNCATION_SENTINEL = "[...truncated, load task for full instructions]" 4 5 6def snake_case(s: str) -> str: 7 return re.sub(r"(?<!^)(?=[A-Z])", "_", s).lower() 8 9 10def truncate_to_words_with_agent_sentinel( 11 text: str | None, max_words: int 12) -> str | None: 13 """Truncate to max_words and append AGENT_TRUNCATION_SENTINEL when truncation 14 occurred. Preserves None for None. Preserves original whitespace in the 15 retained prefix. Applied to task instructions surfaced to the LLM agent -- 16 see specs/projects/agent_info_trim.""" 17 if max_words < 0: 18 raise ValueError("max_words must be non-negative") 19 if text is None: 20 return None 21 words = text.split() 22 if len(words) <= max_words: 23 return text 24 # Find the end position of the max_words-th word in the original text, 25 # preserving original whitespace in the prefix. 26 pos = 0 27 for _ in range(max_words): 28 # Skip whitespace 29 while pos < len(text) and text[pos].isspace(): 30 pos += 1 31 # Skip word 32 while pos < len(text) and not text[pos].isspace(): 33 pos += 1 34 prefix = text[:pos].rstrip() 35 if not prefix: 36 return AGENT_TRUNCATION_SENTINEL 37 return f"{prefix} {AGENT_TRUNCATION_SENTINEL}"
AGENT_TRUNCATION_SENTINEL =
'[...truncated, load task for full instructions]'
def
snake_case(s: str) -> str:
def
truncate_to_words_with_agent_sentinel(text: str | None, max_words: int) -> str | None:
11def truncate_to_words_with_agent_sentinel( 12 text: str | None, max_words: int 13) -> str | None: 14 """Truncate to max_words and append AGENT_TRUNCATION_SENTINEL when truncation 15 occurred. Preserves None for None. Preserves original whitespace in the 16 retained prefix. Applied to task instructions surfaced to the LLM agent -- 17 see specs/projects/agent_info_trim.""" 18 if max_words < 0: 19 raise ValueError("max_words must be non-negative") 20 if text is None: 21 return None 22 words = text.split() 23 if len(words) <= max_words: 24 return text 25 # Find the end position of the max_words-th word in the original text, 26 # preserving original whitespace in the prefix. 27 pos = 0 28 for _ in range(max_words): 29 # Skip whitespace 30 while pos < len(text) and text[pos].isspace(): 31 pos += 1 32 # Skip word 33 while pos < len(text) and not text[pos].isspace(): 34 pos += 1 35 prefix = text[:pos].rstrip() 36 if not prefix: 37 return AGENT_TRUNCATION_SENTINEL 38 return f"{prefix} {AGENT_TRUNCATION_SENTINEL}"
Truncate to max_words and append AGENT_TRUNCATION_SENTINEL when truncation occurred. Preserves None for None. Preserves original whitespace in the retained prefix. Applied to task instructions surfaced to the LLM agent -- see specs/projects/agent_info_trim.