fix(ai-chat): cross-mark text edits, partial batches, JSON-string node parity

edit_page_text (applyTextEdits) now matches at the inline-block level instead of
per text node, so a find/replace may cross bold/italic/link boundaries; the
replacement inherits marks from the unchanged common prefix/suffix via a diff
splice. Atom (non-text inline) slots can never be part of a match, making the
U+FFFC placeholder collision-safe, and inserted text never inherits an atom's
marks.

The edit batch is no longer all-or-nothing: applyTextEdits returns
{ doc, results, failed } and applies what it can; editPageText writes only on a
real change (no spurious history version for a no-op) and throws an aggregated,
actionable error only when nothing applied.

The AI-chat insert_node / patch_node / update_page_json tools now JSON.parse a
node/content argument that arrives as a string, matching the standalone MCP
server (this is what made insert_node fail under OpenAI tool calls).

Tool descriptions gain concrete ProseMirror examples and reflect the new
edit_page_text behavior. Adds/updates json-edit unit tests (183 pass).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
vvzvlad
2026-06-17 06:57:58 +03:00
parent 0a9788e89a
commit fc9088b74d
8 changed files with 1036 additions and 210 deletions

View File

@@ -32,18 +32,24 @@ test("single-match replace preserves ids/marks and reports replacements===1", ()
assert.equal(tnode.text, "Hello there");
});
test("zero match throws not found", () => {
test("zero match is reported via failed[], doc unchanged", () => {
const input = doc(paragraph(textNode("Hello world")));
const snapshot = JSON.parse(JSON.stringify(input));
assert.throws(
() => applyTextEdits(input, [{ find: "absent", replace: "x" }]),
/not found/,
);
const { doc: out, results, failed } = applyTextEdits(input, [
{ find: "absent", replace: "x" },
]);
assert.deepEqual(results, []);
assert.equal(failed.length, 1);
assert.match(failed[0].reason, /not found/);
// Doc is structurally unchanged (modulo deep-copy identity).
assert.deepEqual(out, snapshot);
});
test("text split across two text nodes (one bold) throws spans-multiple-runs", () => {
test("text split across two text nodes (one bold) now applies, marks preserved", () => {
// "Hello world" is split: "Hello " (plain) + "world" (bold). No single text
// node contains "Hello world", but the collected document text does.
// node contains "Hello world", but the block-level matcher spans them.
const input = doc(
paragraph(
textNode("Hello "),
@@ -51,20 +57,161 @@ test("text split across two text nodes (one bold) throws spans-multiple-runs", (
),
);
assert.throws(
() => applyTextEdits(input, [{ find: "Hello world", replace: "x" }]),
/spans/,
);
const { doc: out, results, failed } = applyTextEdits(input, [
{ find: "Hello world", replace: "Hello there" },
]);
assert.deepEqual(results, [{ find: "Hello world", replacements: 1 }]);
assert.deepEqual(failed, []);
// The unchanged prefix "Hello " stays plain; the changed region "world" was
// uniformly bold, so the replacement "there" stays bold.
const para = out.content[0];
assert.equal(para.content.length, 2);
assert.equal(para.content[0].text, "Hello ");
assert.equal(para.content[0].marks, undefined);
assert.equal(para.content[1].text, "there");
assert.deepEqual(para.content[1].marks, [{ type: "bold" }]);
});
test("multi-match without replaceAll throws matches", () => {
test("multi-match without replaceAll is reported via failed[], doc unchanged", () => {
// "ab" appears twice inside a single text node.
const input = doc(paragraph(textNode("ab cd ab")));
const snapshot = JSON.parse(JSON.stringify(input));
assert.throws(
() => applyTextEdits(input, [{ find: "ab", replace: "x" }]),
/matches/,
const { doc: out, results, failed } = applyTextEdits(input, [
{ find: "ab", replace: "x" },
]);
assert.deepEqual(results, []);
assert.equal(failed.length, 1);
assert.match(failed[0].reason, /matches/);
assert.deepEqual(out, snapshot);
});
test("cross-run replace with mixed marks inherits left-neighbor marks", () => {
// The matched region "BC" is split: "B" bold, "C" italic — non-uniform marks,
// and the replacement "X" shares no common prefix/suffix with "BC", so the
// inserted text inherits the left neighbor's marks. Here the left neighbor of
// the changed region is "A" (plain), so "X" must be plain.
const input = doc(
paragraph(
textNode("A"),
textNode("B", { marks: [{ type: "bold" }] }),
textNode("C", { marks: [{ type: "italic" }] }),
textNode("D"),
),
);
const { doc: out, results, failed } = applyTextEdits(input, [
{ find: "BC", replace: "X" },
]);
assert.deepEqual(results, [{ find: "BC", replacements: 1 }]);
assert.deepEqual(failed, []);
// "A" + "X"(plain) + "D" coalesce into a single plain text node "AXD".
const para = out.content[0];
assert.equal(para.content.length, 1);
assert.equal(para.content[0].text, "AXD");
assert.equal(para.content[0].marks, undefined);
});
test("cross-run replace at block start inherits [] marks", () => {
// The whole block content is the mixed-mark match "BC" with no left neighbor,
// so inserted text falls through to the right neighbor / [] (block start).
const input = doc(
paragraph(
textNode("B", { marks: [{ type: "bold" }] }),
textNode("C", { marks: [{ type: "italic" }] }),
),
);
const { doc: out, results } = applyTextEdits(input, [
{ find: "BC", replace: "X" },
]);
assert.deepEqual(results, [{ find: "BC", replacements: 1 }]);
const para = out.content[0];
assert.equal(para.content.length, 1);
assert.equal(para.content[0].text, "X");
assert.equal(para.content[0].marks, undefined);
});
test("partial batch: good edits apply, the bad one goes to failed[]", () => {
const input = doc(paragraph(textNode("alpha beta gamma")));
const { doc: out, results, failed } = applyTextEdits(input, [
{ find: "alpha", replace: "ALPHA" },
{ find: "absent", replace: "X" },
{ find: "gamma", replace: "GAMMA" },
]);
// The 2 matching edits applied; the missing one is reported.
assert.deepEqual(results, [
{ find: "alpha", replacements: 1 },
{ find: "gamma", replacements: 1 },
]);
assert.equal(failed.length, 1);
assert.equal(failed[0].find, "absent");
assert.match(failed[0].reason, /not found/);
assert.equal(out.content[0].content[0].text, "ALPHA beta GAMMA");
});
test("a match that crosses an atom is refused, doc unchanged", () => {
// paragraph: "a" <hardBreak> "b". A find of "ab" spans the hardBreak atom,
// so it is not a valid match: a match range may not contain an atom slot.
// The edit lands in failed[] (reason: atom-specific OR not-found) and the
// document is left unchanged.
const input = doc(
paragraph(
textNode("a"),
{ type: "hardBreak" },
textNode("b"),
),
);
const snapshot = JSON.parse(JSON.stringify(input));
const { doc: out, results, failed } = applyTextEdits(input, [
{ find: "ab", replace: "z" },
]);
assert.deepEqual(results, []);
assert.equal(failed.length, 1);
assert.match(failed[0].reason, /non-text inline node|not found/);
assert.deepEqual(out, snapshot);
});
test("a TEXT node containing a literal U+FFFC matches/replaces normally", () => {
// The U+FFFC OBJECT REPLACEMENT CHARACTER is the placeholder for atom slots,
// but a real text node may legitimately contain that code unit. Such a slot
// has no `.atom`, so it must match and replace like any other character —
// proving atoms and literal-U+FFFC text are distinguished.
const input = doc(paragraph(textNode("xy")));
const { doc: out, results, failed } = applyTextEdits(input, [
{ find: "xy", replace: "done" },
]);
assert.deepEqual(results, [{ find: "xy", replacements: 1 }]);
assert.deepEqual(failed, []);
assert.equal(out.content[0].content[0].text, "done");
});
test("a no-op edit (find === replace) produces a doc deep-equal to the input", () => {
// find === replace "applies" but changes nothing: the produced document must
// be structurally identical to the input (this is what lets the client skip
// the collaboration write and avoid a spurious history version).
const input = doc(paragraph(textNode("unchanged text")));
const snapshot = JSON.parse(JSON.stringify(input));
const { doc: out, results } = applyTextEdits(input, [
{ find: "unchanged", replace: "unchanged" },
]);
assert.deepEqual(results, [{ find: "unchanged", replacements: 1 }]);
// Deep-equal to the input despite the edit being reported as applied.
assert.deepEqual(out, snapshot);
});
test("replaceAll replaces all occurrences", () => {