unaiverse.uai.fence¶
A message carries a block as a fenced code block whose info string is uai. This module is the only one of the package that implements the grammar of that fence. It splits a message into prose segments and raw block bodies in their original order and it wraps a body into a fence. It parses no JSON. parse_message reads the bodies that this module extracts. Import the re-exported names from unaiverse.uai. The protocol is described in the package page.
The grammar follows CommonMark with one restriction on the tag.
- An opening fence is a line with at most three leading spaces, three or more backticks, optional spaces or tabs, the literal
uaiand optional trailing spaces or tabs. A line of tildes never opens a block. - A closing fence is a line with at most three leading spaces, at least as many backticks as the opening fence and optional trailing spaces or tabs.
- The body is every line between the two fences joined with
\n. One carriage return at the end of a fence line is ignored. The carriage returns of the body stay in it. - An opening fence without a closing fence is prose.
- A body larger than
MAX_BLOCK_BYTES(8192 bytes in UTF-8) is markedtoo_largeand must never be parsed.
| Object | Re-exported by unaiverse.uai |
Role |
|---|---|---|
has_fence |
yes | A fast test for a candidate opening fence. |
wrap_fence |
yes | Puts a JSON body inside a fence. |
extract_fences |
yes | Splits a message into prose and raw blocks. |
fence_spans |
yes | The character spans of the blocks. |
cut_to_bytes |
no | Cuts a string to a byte budget on a character boundary. |
OPEN_RE, HAS_FENCE_RE, CR_RE |
no | The compiled regular expressions of the grammar. |
The compiled pattern of an opening fence: {0,3}(`{3,})[ \t]*uai[ \t]* (it starts with a space). extract_fences matches it against a whole line with fullmatch. Group 1 captures the backticks and its length gives the minimum number of backticks of the closing fence.
HAS_FENCE_RE
module-attribute
¶
HAS_FENCE_RE = re.compile('```[ \\t]*' + FENCE_TAG + '[ \\t]*(\\r?\\n|$)')
The compiled pattern behind has_fence: three backticks, optional spaces or tabs, uai, optional spaces or tabs and then a line end or the end of the text. The pattern is searched anywhere in the text and is not anchored to the start of a line.
The compiled pattern \r\Z that removes one trailing carriage return from a line before the fence patterns are tested. A message with CRLF line ends therefore splits like the same message with LF line ends.
has_fence
¶
Returns True when a text contains at least one candidate opening fence. The node, the agent, the logger and the processor wrapper call it on every incoming or outgoing text as a fast path before any parsing.
| Argument | Type | Default | Meaning |
|---|---|---|---|
text |
any | required | The text to test. A value that is not a str gives False. |
It returns a bool. The test is cheap and permissive and matches ```uai even after other characters on the same line. A True result means only that the message may hold a block. parse_message gives the final answer.
has_fence("Hi\n```uai\n{}\n```") # True
has_fence("see ```uai\n") # True (a candidate that extract_fences does not open)
has_fence("```uaix\n") # False
has_fence(None) # False
wrap_fence
¶
Wraps a JSON body in a uai fence. It adds no trailing newline after the closing fence.
| Argument | Type | Default | Meaning |
|---|---|---|---|
body |
str |
required | The canonical JSON body. The function does not check it. |
serialize_block calls it on the output of canonical_json.
cut_to_bytes
¶
Source code in unaiverse/uai/fence.py
Cuts a string to the longest prefix whose UTF-8 encoding fits in max_bytes. The cut never splits a character.
| Argument | Type | Default | Meaning |
|---|---|---|---|
s |
str |
required | The string to cut. |
max_bytes |
int |
required | The byte budget. |
The package uses it for the body of an oversized block, for the model view of to_model_text and for the raw texts that encode_reply trims.
extract_fences
¶
Source code in unaiverse/uai/fence.py
Splits a message into an ordered list of prose and block segments.
| Argument | Type | Default | Meaning |
|---|---|---|---|
text |
str |
required | The message. A value that is not a str or an empty string gives []. |
It returns a list[dict] with two shapes of segment.
| Key | Prose segment | Block segment |
|---|---|---|
kind |
"prose" |
"block" |
text |
The prose without the blank lines that only separate it from a fence. A prose segment is never empty or blank. | absent |
raw |
absent | The body between the fences, cut to 8192 bytes when too_large is True. |
bytes |
absent | The size of the full body in UTF-8 bytes. |
too_large |
absent | True when the body exceeds MAX_BLOCK_BYTES. |
start, end |
Character offsets of the segment in text (end is exclusive and the range includes the separating blank lines). |
Character offsets of the whole fence from the start of the opening line to one past the last character of the closing line. |
The function never raises. A body over the limit comes back cut. Its caller must not parse it.
m = 'Intro\n\n```uai\n{"v":1,"type":"x-a","alt":"z"}\n```\nOutro\n```uai\nnot closed'
extract_fences(m)
# [{'kind': 'prose', 'text': 'Intro', 'start': 0, 'end': 7},
# {'kind': 'block', 'raw': '{"v":1,"type":"x-a","alt":"z"}', 'bytes': 30, 'too_large': False,
# 'start': 7, 'end': 48},
# {'kind': 'prose', 'text': 'Outro\n```uai\nnot closed', 'start': 49, 'end': 72}]
The second opening fence has no closing fence and stays prose. A block opened by four backticks needs at least four backticks to close. A line of three backticks inside it belongs to the body.
fence_spans
¶
Returns the (start, end) character spans of the blocks of a message in order. It runs extract_fences and keeps the block segments. Blocks marked too_large are included.
| Argument | Type | Default | Meaning |
|---|---|---|---|
text |
str |
required | The message. |
truncate_outside_fences uses these spans to avoid cutting inside a block.