Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(maker-shared): append 到容器的正文落入隐式 markdown 子节点

Zhang Yunjin4441b3370878
diff --git a/docs/rich-answers.md b/docs/rich-answers.md
index dcbb76ee18..c5c6023635 100644
--- a/docs/rich-answers.md
+++ b/docs/rich-answers.md
@@ -6,7 +6,7 @@
文本型 Harness 复用现有累积文本事件,以 `cindy-ui` 围栏承载逐行操作。每一完整操作行即可提交,无需等围栏或回答完成。普通 Markdown 仍用现有正文组件。用户消息、引用内嵌示例与文件预览不启用组件解释。
-共享解析器保存稳定节点:create、append、patch、move、remove。节点按 id 更新;输入、勾选和选择值与模型默认值分开。Markdown、图片、视频、公式和 Mermaid 沿用现有文件来源与预览边界。布局仅接受组件属性,不执行模型 HTML、CSS 或 JavaScript。数值表达式只支持有界的算术/选择树;按钮默认修改本地状态,也可声明显式提交一条用户消息。
+共享解析器保存稳定节点:create、append、patch、move、remove。节点按 id 更新;输入、勾选和选择值与模型默认值分开。模型的常见笔误就近挽救成可读节点:打到容器上的 append 落入该容器的隐式 markdown 子节点,裸字符串 props 与内联 children 转成正式子节点;无法挽救的内容才计入未能显示。Markdown、图片、视频、公式和 Mermaid 沿用现有文件来源与预览边界。布局仅接受组件属性,不执行模型 HTML、CSS 或 JavaScript。数值表达式只支持有界的算术/选择树;按钮默认修改本地状态,也可声明显式提交一条用户消息。
Desktop 使用原生 React 组件;Mobile 使用原生 View/Text 与既有媒体组件,不新增原生依赖或 runtime fingerprint。浅深主题取各端语义 token,窄屏按可用宽度换行。
diff --git a/packages/maker-shared/src/__tests__/richAnswer.test.ts b/packages/maker-shared/src/__tests__/richAnswer.test.ts
index 8333776a28..0d9e966b8c 100644
--- a/packages/maker-shared/src/__tests__/richAnswer.test.ts
+++ b/packages/maker-shared/src/__tests__/richAnswer.test.ts
@@ -404,6 +404,78 @@ describe('malformed node drafts stay readable', () => {
expect(child?.props.text).toBe('正文');
});
});
+
+// 测试价值
+// - 生产入口与可观察结果:RichAnswerStream 重放 cindy-ui 围栏;用户看到容器卡片里的
+// 正文,而不是空卡片加「部分内容未能显示」。
+// - Bug/规则依据:会话 66065272 实录(details 写 children:["kind:stack"],另有 5 条
+// append 直接打到 stack/details 容器,正文全部丢失);#7627 同族的第三类笔误。
+// - 现有覆盖缺口:#7627 用例只盖裸字符串 props 与对象型内联 children;
+// append→容器与字符串 children draft 均无覆盖。
+// - 最小失败输入:create details(children:["kind:stack"]) + append 到容器。
+// - tier:unit(与同文件流归约测试同级,复用同一模块加载)。
+// - 实测耗时:pnpm --filter @cindy/maker-shared exec vitest run src/__tests__/richAnswer.test.ts
+// (修复前新增用例红、修复后全绿)。
+describe('append to a container stays readable', () => {
+ it('routes prose appended to a container into one implicit markdown child', () => {
+ const parser = new RichAnswerStream();
+ const source =
+ line(1, { op: 'create', id: 'card', kind: 'stack' }) +
+ line(2, { op: 'append', id: 'card', text: '**提交表**\n' });
+ const doc = parser.update(source + line(3, { op: 'append', id: 'card', text: '| a | b |' }));
+ const children = doc.nodes.get('card')?.children ?? [];
+ expect(children).toEqual(['card-1']);
+ expect(doc.nodes.get('card-1')?.kind).toBe('markdown');
+ expect(doc.nodes.get('card-1')?.props.text).toBe('**提交表**\n| a | b |');
+ expect(doc.issues).toBe(0);
+ // 历史消息从原文重放,容错结果必须逐节点一致。
+ expect(new RichAnswerStream().update(source + line(3, { op: 'append', id: 'card', text: '| a | b |' })).nodes).toEqual(doc.nodes);
+ });
+
+ it('keeps a details label and its appended body when children only name kinds', () => {
+ const doc = new RichAnswerStream().update(
+ line(1, { op: 'create', id: 'd', kind: 'details', props: { label: '折叠块', children: ['kind:stack'] } }) +
+ line(2, { op: 'append', id: 'd', text: '正文' }),
+ );
+ const details = doc.nodes.get('d');
+ expect(details?.props.label).toBe('折叠块');
+ expect(details?.children).toHaveLength(1);
+ expect(doc.nodes.get(details?.children[0] ?? '')?.props.text).toBe('正文');
+ // 只有救不回的字符串 draft 计一次,append 本身算成功。
+ expect(doc.issues).toBe(1);
+ });
+
+ it('grows a fresh implicit child after the old subtree is removed or moved away', () => {
+ const parser = new RichAnswerStream();
+ const base =
+ line(1, { op: 'create', id: 'card', kind: 'stack' }) +
+ line(2, { op: 'append', id: 'card', text: 'one' });
+ const removed = parser.update(
+ base + line(3, { op: 'remove', id: 'card' }) +
+ line(4, { op: 'create', id: 'card', kind: 'stack' }) +
+ line(5, { op: 'append', id: 'card', text: 'two' }),
+ );
+ expect(removed.nodes.get('card')?.children).toEqual(['card-1']);
+ expect(removed.nodes.get('card-1')?.props.text).toBe('two');
+ const moved = parser.update(
+ base + line(3, { op: 'move', id: 'card-1', parent: 'root' }) +
+ line(4, { op: 'append', id: 'card', text: 'two' }),
+ );
+ expect(moved.roots).toContain('card-1');
+ expect(moved.nodes.get('card-1')?.props.text).toBe('one');
+ expect(moved.nodes.get('card')?.children).toEqual(['card-1-2']);
+ expect(moved.nodes.get('card-1-2')?.props.text).toBe('two');
+ });
+
+ it('still rejects appends to leaves that render no text', () => {
+ const doc = new RichAnswerStream().update(
+ line(1, { op: 'create', id: 'stat', kind: 'stat', props: { value: 1 } }) +
+ line(2, { op: 'append', id: 'stat', text: 'x' }),
+ );
+ expect(doc.nodes.get('stat')?.props.text).toBeUndefined();
+ expect(doc.issues).toBe(1);
+ });
+});
describe('inline draft limits', () => {
it('caps nested inline drafts at the depth limit', () => {
let nested: Record<string, unknown> = { kind: 'markdown', text: 'deepest' };
diff --git a/packages/maker-shared/src/richAnswer.ts b/packages/maker-shared/src/richAnswer.ts
index 7dadfd28b5..65d5890c53 100644
--- a/packages/maker-shared/src/richAnswer.ts
+++ b/packages/maker-shared/src/richAnswer.ts
@@ -150,6 +150,8 @@ export class RichAnswerStream {
private seq = 0;
private nodes = new Map<string, RichNode>();
private roots: string[] = [];
+ /** Container id → implicit markdown child that received appended prose. */
+ private implicitText = new Map<string, string>();
private revision = 0;
private issues = 0;
private failedFinalLine: { offset: number; seq: number; issues: number } | null = null;
@@ -161,6 +163,7 @@ export class RichAnswerStream {
this.seq = 0;
this.nodes = new Map();
this.roots = [];
+ this.implicitText.clear();
this.issues = 0;
this.failedFinalLine = null;
this.revision++;
@@ -262,21 +265,51 @@ export class RichAnswerStream {
this.attachInlineChildren(node.id, inline, this.depth(node.id) + 1);
}
} else if (op.op === "append") {
- if (
- !current ||
- !["text", "markdown", "heading", "code", "math", "diagram"].includes(
- current.kind,
- ) ||
- typeof op.text !== "string"
- )
- throw Error("text");
- this.nodes.set(current.id, {
- ...current,
- props: {
- ...current.props,
- text: richString(current, "text") + op.text,
- },
- });
+ if (!current || typeof op.text !== "string") throw Error("text");
+ if (TEXT_PROP_KINDS.includes(current.kind)) {
+ this.nodes.set(current.id, {
+ ...current,
+ props: {
+ ...current.props,
+ text: richString(current, "text") + op.text,
+ },
+ });
+ } else if (CONTAINER_KINDS.includes(current.kind)) {
+ // Models append prose straight to containers (session 66065272: five
+ // appends onto stack/details lost their whole body). Containers render
+ // no text, so the prose lands in one implicit markdown child per
+ // container instead of being dropped, like the #7627 salvages.
+ if (this.nodes.size >= MAX_NODES || this.depth(current.id) >= MAX_DEPTH)
+ throw Error("depth");
+ const existing = this.implicitText.get(current.id);
+ if (
+ existing &&
+ this.nodes.has(existing) &&
+ current.children.includes(existing)
+ ) {
+ const child = this.nodes.get(existing)!;
+ this.nodes.set(existing, {
+ ...child,
+ props: {
+ ...child.props,
+ text: richString(child, "text") + op.text,
+ },
+ });
+ } else {
+ const id = this.inlineChildId(current.id, 1);
+ this.nodes.set(id, {
+ id,
+ kind: "markdown",
+ props: this.props({ text: op.text }, "markdown"),
+ children: [],
+ });
+ this.nodes.set(current.id, {
+ ...current,
+ children: [...current.children, id],
+ });
+ this.implicitText.set(current.id, id);
+ }
+ } else throw Error("text");
} else if (op.op === "patch") {
if (!current) throw Error("target");
this.nodes.set(current.id, {