cubepi.utils
emit_eventâ
function
emit_event(emit_fn: Callable, event: object) -> None
parse_streaming_jsonâ
function
parse_streaming_json(text: str | None) -> dict
Parse potentially incomplete or malformed JSON from streaming.
Uses a 3-tier fallback:
json.loads(text)json.loads(repair_json(text))- Partial-parse: repair + close open braces/brackets/strings
\{\}as last resort
Always returns a dict (or at minimum \{\}) so callers never need
to handle parse failures.
repair_jsonâ
function
repair_json(text: str) -> str
Repair malformed JSON string literals.
Operates character-by-character, tracking whether the cursor is inside a JSON string value. Inside strings it:
- Escapes raw control characters (0x00-0x1F) that are not already
escaped (
\t,\n,\rare kept when already escaped). - Doubles a backslash before an invalid escape character
(e.g.
\xbecomes\\x), making the output parseable. - Handles truncated backslash at end-of-string.