backend: let a model put a figure in the paper, and set a formula's alignment
Two things the editor gained and MCP could not do, both because the tool layer
had been told about the domain's columns by hand.
**A figure.** Bytes go to MinIO and the sentence keeps a Markdown address, so a
picture is ordinary text — which means the writing tools already carry it, and
the only half a model cannot do is the upload: it has bytes and no way to send
multipart. `image_upload` takes base64 and answers with the URL, the Markdown
and the *sentence object* the editor produces when a picture is dropped in, so a
figure is written by putting that object into `sentences` unchanged. It does not
fetch a URL the caller names: a service inside the cluster that will GET any
address it is handed is an SSRF primitive aimed at everything beside it.
The width argument is the reader's rule or it is refused. `{width=70%}` is
parsed by `frontend/src/utils/rich.ts`, and a value it cannot read is not an
error there — the picture just prints at its natural size. A size that silently
does not apply is worse than a refusal, so the syntax and the range (pixels
1–4000, per cent 1–100) are mirrored in `app/mcp/tools/images.py`, with the
usual comment naming the other side.
`paragraph_text` also had to learn what a figure is: a sentence holding nothing
but an image is a 插图 — the view prints it on its own line with the brackets'
text as its caption — and the text a tool hands back broke lines around a
display formula but not around a picture. Same rule as `figureOf`, same pair of
implementations, same warning about changing both.
**A formula's alignment.** `paper_sentence.formula_align` reached the editor and
the REST API and stopped there: `SentenceSpec` had no such field, so a model that
sent one got a successful call and a centred formula. The column is now declared,
carried through every write path and reported by every read path.
That is the second column lost the same way, so the fix is structural rather
than another two edits. `SENTENCE_FIELDS` is read from
`app.schemas.paper.SentenceInput` instead of written out, the payload is built
by one `sentence_payload()`, and the three append paths spread
`SentenceCreate(**item.model_dump())` rather than copying fields. One edit
remains — declaring the field in `specs.py` — and smoke_mcp.py's first step
compares the two field sets, so forgetting it now fails the suite instead of
losing the field. `sort` is declared too, which it never was: a model that sent
one had it dropped in silence.
smoke_mcp.py: 99 checks to 119. The figure step uploads a real 1x1 GIF (the same
bytes twice, and the key is the same object), writes the returned sentence as it
came back, checks the paragraph prints it alone on its line and that an image
inside a sentence does not break the line, and takes the four refusals — not a
picture, an unparseable size, an out-of-range one, not base64. The alignment step
writes a right-aligned formula, moves the sentence to left on its own, and takes
the paragraph to another position to prove the setting travels with it.
Deployed as v6, and the pod now carries MINIO_ENDPOINT (the in-cluster Service)
plus the two credential keys from the Secret; without them the image tool answers
with the missing variable names and every writing tool still works.
This commit is contained in:
+99
-16
@@ -260,6 +260,9 @@ def sentence_row(sentence: Any, *, separator: str | None = None) -> dict:
|
||||
# move one: a paragraph read back without this flag looks like prose
|
||||
# that happens to have no spaces in it.
|
||||
"line_break_before": sentence.line_break_before,
|
||||
# Only meaningful for a sentence holding a display formula, and reported
|
||||
# anyway: a row that hides it looks like a row where it is not set.
|
||||
"formula_align": sentence.formula_align,
|
||||
"citations": [citation_row(item) for item in sentence.citations],
|
||||
}
|
||||
if separator is not None:
|
||||
@@ -283,17 +286,46 @@ def paragraph_row(paragraph: ParagraphRead, *, preview: int = 80) -> dict:
|
||||
|
||||
#: A formula the paper view sets on a line of its own: ``$$…$$`` or ``\[…\]``.
|
||||
#:
|
||||
#: This is the same rule as ``hasDisplayMath`` in ``frontend/src/utils/math.ts``,
|
||||
#: This is the same rule as ``hasDisplayMath`` in ``frontend/src/utils/rich.ts``,
|
||||
#: written twice because the view is TypeScript and this is Python. The two must
|
||||
#: agree, or a tool result and the page a writer is looking at would disagree
|
||||
#: about where the lines are — which is the one thing this module exists to
|
||||
#: prevent. Change one, change the other.
|
||||
_DISPLAY_MATH = re.compile(r"\$\$[\s\S]+?\$\$|\\\[[\s\S]+?\\\]")
|
||||
|
||||
#: One image, as a sentence writes it: ```` with pandoc's optional
|
||||
#: ``{width=…}``. The counterpart of the image alternative in ``RICH_PATTERN``
|
||||
#: (same file as above), and the same warning applies: the two are one rule.
|
||||
_IMAGE = re.compile(r"!\[(?P<alt>[^\]]*)\]\([^()\s]+(?:\s+\"[^\"]*\")?\)(?:\{[^}]*\})?")
|
||||
|
||||
def _is_display(sentence: Any) -> bool:
|
||||
"""Whether this sentence is a display formula — a block, not a run of text."""
|
||||
return bool(_DISPLAY_MATH.search(sentence.content))
|
||||
|
||||
def _figure_caption(content: str) -> str | None:
|
||||
"""The 图注 of a figure sentence — a sentence that is nothing but one image.
|
||||
|
||||
The paper view's ``figureOf``: a sentence holding only a picture is a 插图,
|
||||
printed on a line of its own with the brackets' text underneath as its
|
||||
caption; a picture inside a sentence that says something stays in the line.
|
||||
Anything else — two images, or an image with words around it — is not a
|
||||
figure, and returns ``None``.
|
||||
"""
|
||||
match = _IMAGE.search(content)
|
||||
if match is None:
|
||||
return None
|
||||
if content[: match.start()].strip() or content[match.end() :].strip():
|
||||
return None
|
||||
return match.group("alt")
|
||||
|
||||
|
||||
def _is_block(sentence: Any) -> bool:
|
||||
"""Whether this sentence takes a line of its own, whether or not it asked.
|
||||
|
||||
Two kinds do, and both are rules the paper view already applies: a display
|
||||
formula, and a figure. Nothing else is inferred — an ordinary sentence that
|
||||
merely follows a full stop runs on, which is what a paragraph is.
|
||||
"""
|
||||
return bool(_DISPLAY_MATH.search(sentence.content)) or _figure_caption(
|
||||
sentence.content
|
||||
) is not None
|
||||
|
||||
|
||||
def paragraph_text(
|
||||
@@ -308,14 +340,20 @@ def paragraph_text(
|
||||
belongs at each seam, so a tool result and the paper view agree on where
|
||||
the spaces go. Nothing here adds spacing of its own.
|
||||
|
||||
A line break is printed where the model asked for one, and around a display
|
||||
formula — the paper view renders ``$$…$$`` as a block, so the lines on
|
||||
either side of it are line boundaries there too. A break *replaces* the
|
||||
A line break is printed where the model asked for one, and on both sides of
|
||||
a block — a display formula, or a sentence that is one figure — because the
|
||||
paper view sets those on a line of their own. A break *replaces* the
|
||||
separator rather than joining it: the new line already separates the two
|
||||
sentences, and a space as well would indent the line below by one blank.
|
||||
The first sentence of a paragraph is never preceded by a break, because
|
||||
there is nothing above it to break from.
|
||||
|
||||
A figure prints as the Markdown it is stored as — ```` on its
|
||||
own line — rather than as its caption alone. The text form cannot draw the
|
||||
picture, and the address is what says *which* picture: a model that reads a
|
||||
paragraph back sees the same string the writer put there, and can paste it
|
||||
somewhere else unchanged.
|
||||
|
||||
A marker's number comes from ``index_by_citation`` — the citation's *work*
|
||||
number as the whole document assigned it, not its position in this sentence —
|
||||
so what is printed in the text matches the 参考文献 list it points at. Read
|
||||
@@ -325,7 +363,7 @@ def paragraph_text(
|
||||
parts: list[str] = []
|
||||
previous_block = False
|
||||
for sentence in paragraph.sentences:
|
||||
block = _is_display(sentence)
|
||||
block = _is_block(sentence)
|
||||
if parts and (sentence.line_break_before or block or previous_block):
|
||||
parts.append("\n")
|
||||
else:
|
||||
@@ -619,6 +657,50 @@ def _is_latin_break(text: str, index: int) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
#: Every field a sentence payload carries, read from the payload schema itself
|
||||
#: rather than listed by hand here.
|
||||
#:
|
||||
#: The hand-written list was wrong twice. A column added to the domain —
|
||||
#: ``line_break_before``, and then ``formula_align`` — reached the REST API, the
|
||||
#: editor and the paper view, and was silently dropped by every MCP write path
|
||||
#: that had spelled the fields out: the call succeeded and the field was simply
|
||||
#: gone. Reading the names from :class:`~app.schemas.paper.SentenceInput` means a
|
||||
#: new column flows through :func:`sentence_payload` and through the tools that
|
||||
#: spread it, and the only edit left is declaring it in
|
||||
#: :class:`~app.mcp.specs.SentenceSpec` — which ``smoke_mcp.py`` checks for, so
|
||||
#: forgetting *that* fails the suite instead of losing the field.
|
||||
SENTENCE_FIELDS: tuple[str, ...] = tuple(SentenceInput.model_fields)
|
||||
|
||||
|
||||
def sentence_payload(values: Mapping[str, Any], **overrides: Any) -> SentenceInput:
|
||||
"""One sentence payload, built from whatever the caller sent.
|
||||
|
||||
Fields the caller did not mention keep the schema's own defaults, and keys
|
||||
this layer does not know are dropped rather than handed to a validator that
|
||||
would refuse them: a model that copies a row out of ``paragraph_get`` and
|
||||
edits it sends back ``id``, ``position`` and ``separator_before`` as well,
|
||||
and read-modify-write is a reasonable way to work.
|
||||
"""
|
||||
known = {name: values[name] for name in SENTENCE_FIELDS if name in values}
|
||||
known.update(overrides)
|
||||
return SentenceInput(**known)
|
||||
|
||||
|
||||
def sentence_values(row: Any) -> dict[str, Any]:
|
||||
"""One *stored* sentence as payload fields again — for a move or a copy.
|
||||
|
||||
``sort`` is deliberately absent, and the caller decides about it: a sentence
|
||||
moved onto another paragraph must not carry its old number there, or the two
|
||||
paragraphs interleave by sort instead of the moved block landing after the
|
||||
target's own sentences.
|
||||
"""
|
||||
return {
|
||||
name: getattr(row, name)
|
||||
for name in SENTENCE_FIELDS
|
||||
if name not in {"sort", "citations"} and hasattr(row, name)
|
||||
}
|
||||
|
||||
|
||||
def sentence_inputs(items: Sequence[Any], *, default_split: str = "line") -> list[SentenceInput]:
|
||||
"""Normalise what a model sent into the project's own sentence payload.
|
||||
|
||||
@@ -660,13 +742,7 @@ def sentence_inputs(items: Sequence[Any], *, default_split: str = "line") -> lis
|
||||
line_break = bool(item.get("line_break_before", False))
|
||||
|
||||
if item.get("content") is not None:
|
||||
out.append(
|
||||
SentenceInput(
|
||||
content=str(item["content"]),
|
||||
line_break_before=line_break,
|
||||
citations=citations,
|
||||
)
|
||||
)
|
||||
out.append(sentence_payload(item, citations=citations))
|
||||
continue
|
||||
|
||||
if item.get("text") is not None:
|
||||
@@ -679,8 +755,12 @@ def sentence_inputs(items: Sequence[Any], *, default_split: str = "line") -> lis
|
||||
# would turn a paragraph the writer wrapped across three lines
|
||||
# into three lines of the paper.
|
||||
out.append(
|
||||
SentenceInput(
|
||||
sentence_payload(
|
||||
item,
|
||||
content=piece,
|
||||
# A block has no ``sort`` of its own: the pieces are one
|
||||
# after another, and the CRUD layer numbers them.
|
||||
sort=None,
|
||||
line_break_before=line_break and offset == 0,
|
||||
citations=citations,
|
||||
)
|
||||
@@ -728,6 +808,7 @@ def paragraph_inputs(spec: Any) -> list[SentenceInput]:
|
||||
|
||||
__all__ = [
|
||||
"MAX_PAGE_SIZE",
|
||||
"SENTENCE_FIELDS",
|
||||
"SPLIT_MODES",
|
||||
"clean",
|
||||
"citation_row",
|
||||
@@ -747,7 +828,9 @@ __all__ = [
|
||||
"resolve_position",
|
||||
"sentence_inputs",
|
||||
"sentence_or_fail",
|
||||
"sentence_payload",
|
||||
"sentence_row",
|
||||
"sentence_values",
|
||||
"session",
|
||||
"split_text",
|
||||
"template_or_fail",
|
||||
|
||||
Reference in New Issue
Block a user