Skip to main content

Module sanitize

Module sanitize 

Source
Expand description

Strips the formatting a model wraps around its whole reply. Whether what is left is usable is the reply format’s question (reply), then the purpose’s own check (validate).

Constants§

CONVERSATIONAL_PREAMBLES 🔒
Conversational-filler openers a model reaches for despite being told to respond with a bare, direct instruction — e.g. “Yeah, let’s fix the bug” instead of “Fix the bug”. Matched case-insensitively against the very start of the string only (a “let’s” appearing mid-sentence is left alone — this strips openers, not arbitrary word choice). Longer/more specific entries first so e.g. “let’s go ahead and “ matches whole rather than the shorter “let’s “ eating only part of it.

Functions§

case_insensitive_prefix_byte_len 🔒
Byte length in s of a prefix that case-insensitively matches prefix (always plain ASCII lowercase), or None if s doesn’t start with it. Walks s’s own char boundaries rather than slicing by an offset computed against a separately-lowercased copy of s — a char’s lowercase mapping can change UTF-8 byte length (e.g. the Kelvin sign ‘K’ -> ‘k’) or even character count (e.g. Turkish ‘İ’ -> “i̇”), which would otherwise misalign the offset or land it off a char boundary and panic on slice.
sanitize_ambient_text
Removes, repeatedly, then trims: a code fence wrapping the whole text (optionally with a language tag), one matching pair of quotes wrapping it, and a conversational opener (“Yeah, let’s…”). A reply left with nothing but backticks (an empty fence) becomes “”, which callers already treat as no reply.
strip_conversational_preamble 🔒
Strip one leading conversational-filler opener (see CONVERSATIONAL_PREAMBLES) and re-capitalize the new first letter, so “Yeah, let’s debug the bug” becomes “Debug the bug” — matching the direct-instruction register every ambient-call prompt asks for. Returns None if no known opener matches (left alone rather than guessed at — this list is deliberately not exhaustive NLP-style filler detection, just the handful of openers a model actually reaches for here).
strip_wrapping_fence 🔒
Strip a wrapping code fence (triple-backtick, optionally with a language tag on the opening line, or a single inline backtick) if the entire string is wrapped — a fence-like substring embedded mid-sentence is left alone. Returns the un-fenced inner text, trimmed.
strip_wrapping_quotes 🔒
Strip ONE pair of quotes that wraps the whole string. A quote at only one end, or a pair that closes before the end ("a" and "b"), is part of the text.