Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(maker-shared): normalize malformed rich-answer drafts
Eapp1e892209f89405
diff --git a/packages/maker-shared/src/__tests__/richAnswer.test.ts b/packages/maker-shared/src/__tests__/richAnswer.test.tsindex 6227f2817c..c3a2f9f027 100644--- a/packages/maker-shared/src/__tests__/richAnswer.test.ts+++ b/packages/maker-shared/src/__tests__/richAnswer.test.ts@@ -299,3 +299,95 @@ it('lets the user retry a refused submission without consuming field values', asawait controller.submit('receipt', 'Park', async () => true);expect(controller.status('receipt', 'Park')).toBe('sent');});+++// #7627: two near-miss drafts models keep writing. Both used to drop the whole+// node (or all of its text) and leave only the "part of this content was hidden" line.+describe('malformed node drafts stay readable', () => {+ it('keeps the text of a node whose props is a plain string', () => {+ const doc = new RichAnswerStream().update(+ line(1, { op: 'create', id: 'b', kind: 'text', props: '这一行会消失' }),+ );+ expect(doc.issues).toBe(0);+ expect(doc.nodes.get('b')?.props.text).toBe('这一行会消失');+ });+ it('turns inline children of a details node into child nodes', () => {+ const doc = new RichAnswerStream().update(+ line(1, {+ op: 'create',+ id: 'd',+ kind: 'details',+ props: { label: '折叠块', children: [{ kind: 'markdown', text: '正文' }] },+ }),+ );+ const details = doc.nodes.get('d');+ expect(doc.issues).toBe(0);+ expect(details?.props.children).toBeUndefined();+ expect(details?.children).toHaveLength(1);+ const child = doc.nodes.get(details?.children[0] ?? '');+ expect(child?.kind).toBe('markdown');+ expect(child?.props.text).toBe('正文');+ });+});+describe('inline draft limits', () => {+ it('caps nested inline drafts at the depth limit', () => {+ let nested: Record<string, unknown> = { kind: 'markdown', text: 'deepest' };+ for (let level = 0; level < 40; level++) nested = { kind: 'stack', children: [nested] };+ const doc = new RichAnswerStream().update(+ line(1, { op: 'create', id: 'd', kind: 'details', props: { label: '折叠块', children: [nested] } }),+ );+ expect(doc.nodes.size).toBeLessThanOrEqual(32);+ expect(doc.nodes.get('d')?.children).toHaveLength(1);+ // 被深度上限截掉的子树属于「没能显示出来的内容」, 不能让 issues 保持 0。+ expect(doc.issues).toBeGreaterThan(0);+ });+ it('skips one unreadable inline draft without losing its siblings', () => {+ const doc = new RichAnswerStream().update(+ line(1, {+ op: 'create',+ id: 'd',+ kind: 'details',+ props: {+ label: '折叠块',+ children: [+ { kind: 'markdown', text: '第一段' },+ { kind: 'text', props: 123 },+ { kind: 'markdown', text: '第二段' },+ ],+ },+ }),+ );+ const details = doc.nodes.get('d');+ expect(details?.children).toHaveLength(2);+ const texts = (details?.children ?? []).map((id) => doc.nodes.get(id)?.props.text);+ expect(texts).toEqual(['第一段', '第二段']);+ expect(doc.issues).toBe(1);+ });+});+describe('inline draft ids', () => {+ it('limits string props by the size of the node it becomes', () => {+ const quoted = '"'.repeat(65_535);+ const doc = new RichAnswerStream().update(+ line(1, { op: 'create', id: 'q', kind: 'text', props: quoted }),+ );+ // 原始字符串没超限, 但存下来的是 {text: ...}, 序列化后翻倍越过上限。+ expect(doc.nodes.get('q')).toBeUndefined();+ expect(doc.issues).toBe(1);+ const plain = new RichAnswerStream().update(+ line(1, { op: 'create', id: 'p', kind: 'text', props: 'a'.repeat(30_000) }),+ );+ expect(plain.nodes.get('p')?.props.text).toHaveLength(30_000);+ });+ it('keeps generated ids inside the eighty character grammar', () => {+ const parentId = 'p'.repeat(79);+ const drafts = Array.from({ length: 1001 }, (_, i) => ({ kind: 'markdown', text: `第${i}段` }));+ const doc = new RichAnswerStream().update(+ line(1, { op: 'create', id: parentId, kind: 'details', props: { label: '折叠块', children: drafts } }),+ );+ const children = doc.nodes.get(parentId)?.children ?? [];+ expect(children).toHaveLength(1001);+ expect(new Set(children).size).toBe(1001);+ for (const id of children) expect(id.length).toBeLessThanOrEqual(80);+ expect(doc.issues).toBe(0);+ });+});diff --git a/packages/maker-shared/src/richAnswer.ts b/packages/maker-shared/src/richAnswer.tsindex ead65ca67c..48578fcdae 100644--- a/packages/maker-shared/src/richAnswer.ts+++ b/packages/maker-shared/src/richAnswer.ts@@ -55,6 +55,16 @@ const ID = /^[a-zA-Z0-9_-]{1,80}$/;const MAX_SOURCE = 2_000_000;const MAX_NODES = 2048;const MAX_DEPTH = 32;+/** Kinds whose children are separate `create` operations. */+const CONTAINER_KINDS = [+ "stack",+ "row",+ "grid",+ "group",+ "form",+ "details",+ "tabs",+];const record = (v: unknown): v is Record<string, unknown> =>!!v && typeof v === "object" && !Array.isArray(v);const value = (v: unknown): v is RichValue =>@@ -205,18 +215,7 @@ export class RichAnswerStream {? null: this.nodes.get(String(op.parent));if (op.parent && op.parent !== "root" && !parent) throw Error("parent");- if (- parent &&- ![- "stack",- "row",- "grid",- "group",- "form",- "details",- "tabs",- ].includes(parent.kind)- )+ if (parent && !CONTAINER_KINDS.includes(parent.kind))throw Error("leaf");const node: RichNode = {id: op.id,@@ -238,6 +237,11 @@ export class RichAnswerStream {this.roots = [...this.roots];this.roots.splice(index, 0, node.id);}+ const inline = node.props.children;+ if (Array.isArray(inline) && CONTAINER_KINDS.includes(node.kind)) {+ delete node.props.children;+ this.attachInlineChildren(node.id, inline, this.depth(node.id) + 1);+ }} else if (op.op === "append") {if (!current ||@@ -325,8 +329,77 @@ export class RichAnswerStream {this.issues++;}}+ /** #7627: children written inline in `props` become the child nodes they were meant to be. */+ private attachInlineChildren(+ parentId: string,+ drafts: unknown[],+ childDepth: number,+ ) {+ const parent = this.nodes.get(parentId);+ if (!parent) return;+ const children = [...parent.children];+ drafts.forEach((draft, offset) => {+ // Inline children are already part of the document the reader sees, so a+ // drop here is missing content: report it like a rejected operation.+ const dropped = () => {+ this.issues++;+ };+ if (+ !record(draft) ||+ !RICH_ANSWER_KINDS.includes(draft.kind as RichAnswerKind) ||+ childDepth > MAX_DEPTH ||+ this.nodes.size >= MAX_NODES+ ) {+ dropped();+ return;+ }+ // One unreadable draft must not take its siblings down with it.+ let props: Record<string, unknown>;+ try {+ props = this.props(+ draft.props ?? (typeof draft.text === "string" ? { text: draft.text } : {}),+ );+ } catch {+ dropped();+ return;+ }+ const id = this.inlineChildId(parentId, offset + 1);+ this.nodes.set(id, {+ id,+ kind: draft.kind as RichAnswerKind,+ props,+ children: [],+ });+ children.push(id);+ // `children` may sit on the draft itself (`{kind:"stack",children:[...]}`) or+ // inside its props; both mean the same thing and both keep nesting bounded.+ const nested = Array.isArray(draft.children) ? draft.children : props.children;+ if (Array.isArray(nested) && CONTAINER_KINDS.includes(draft.kind as RichAnswerKind)) {+ delete props.children;+ this.attachInlineChildren(id, nested, childDepth + 1);+ }+ });+ this.nodes.set(parentId, { ...parent, children });+ }+ private inlineChildId(parentId: string, ordinal: number) {+ // The id grammar caps ids at 80 characters, so the suffix gets the room first.+ for (let attempt = 1; ; attempt++) {+ const suffix = attempt === 1 ? `-${ordinal}` : `-${ordinal}-${attempt}`;+ const id = `${parentId.slice(0, 80 - suffix.length)}${suffix}`;+ if (!this.nodes.has(id)) return id;+ }+ }private props(v: unknown): Record<string, unknown> {if (v === undefined) return {};+ // Models write the text straight into props (`"props": "hello"`); the protocol+ // default keeps that block readable instead of dropping the whole node (#7627).+ if (typeof v === "string") {+ // The limit is on the node that gets stored and replayed, which is the+ // object below: escaping can make it longer than the incoming string.+ const text = { text: v };+ if (JSON.stringify(text).length > 65_536) throw Error("props");+ return text;+ }if (!record(v) || JSON.stringify(v).length > 65_536) throw Error("props");return Object.fromEntries(Object.entries(v).filter(@@ -966,3 +1039,4 @@ export function richMediaSource(node: RichNode): string | null {? allowed[1].toLowerCase() + src.slice(allowed[1].length): src;}+