Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(chat): preserve committed content during rich-answer recovery
xuzini-xzn652b8e274c81
diff --git a/apps/mobile/src/__tests__/richAnswer.test.tsx b/apps/mobile/src/__tests__/richAnswer.test.tsxindex 918ec9967e..76669c73ea 100644--- a/apps/mobile/src/__tests__/richAnswer.test.tsx+++ b/apps/mobile/src/__tests__/richAnswer.test.tsx@@ -83,3 +83,14 @@ it('replaces the incomplete notice with the late full answer without remounting'expect(container.textContent).toContain('Complete findings');expect(container.textContent).not.toContain('message.richAnswer.incomplete');});++// Tier 1: malformed later suffixes must not erase text already on screen.+it('keeps committed text visible while malformed suffixes grow without remounting', async () => {+ const full = JSON.stringify({ version: 1, seq: 1, op: 'create', id: 'answer', kind: 'markdown', props: { text: 'Readable findings' } });+ for (const suffix of ['', '{', '{"seq":', '{"seq":2']) {+ await act(async () => root.render(createElement(SessionQuickSendProvider, { value: null },+ createElement(RichAnswer, { source: full + suffix, stateKey: 'malformed-tail', complete: true, renderMarkdown: text => text }))));+ expect(container.textContent).toContain('Readable findings');+ if (suffix) expect(container.textContent).toContain('message.richAnswer.incomplete');+ }+});diff --git a/packages/maker-shared/src/__tests__/richAnswer.test.ts b/packages/maker-shared/src/__tests__/richAnswer.test.tsindex 826d47b9d8..e276c92211 100644--- a/packages/maker-shared/src/__tests__/richAnswer.test.ts+++ b/packages/maker-shared/src/__tests__/richAnswer.test.ts@@ -62,6 +62,22 @@ describe('streaming answer documents', () => {expect(parser.update(full, true).nodes.get('answer')?.props.text).toBe('one two');expect(first.nodes.get('answer')?.props.text).toBe('one');});+ it.each(['create', 'append', 'patch'])('preserves a committed %s tail when malformed suffixes keep growing', (operation) => {+ const create = line(1, { op: 'create', id: 'answer', kind: 'text', props: { text: 'one' } });+ const update = operation === 'create' ? create.trimEnd() : create + line(2, operation === 'append'+ ? { op: 'append', id: 'answer', text: ' two' }+ : { op: 'patch', id: 'answer', props: { text: 'one two' } }).trimEnd();+ const expected = operation === 'create' ? 'one' : 'one two';+ const parser = new RichAnswerStream();+ const committed = parser.update(update, true);+ for (const suffix of ['{', '{"seq":', '{"seq":3']) {+ const next = parser.update(update + suffix, true);+ expect(next.nodes.get('answer')?.props.text).toBe(expected);+ expect(next.roots).toEqual(['answer']);+ expect(next.issues).toBeGreaterThan(0);+ }+ expect(committed.nodes.get('answer')?.props.text).toBe(expected);+ });it('preserves committed content when an extended finalized tail exceeds the source limit', () => {const create = line(1, { op: 'create', id: 'answer', kind: 'text', props: { text: 'Readable' } }).trimEnd();const parser = new RichAnswerStream();@@ -78,6 +94,7 @@ describe('streaming answer documents', () => {const answer = line(1, { op: 'create', id: 'answer', kind: 'text', props: { text: 'Readable' } });const parser = new RichAnswerStream();expect(parser.update(prefix + answer.slice(0, -8), true).issues).toBe(2);+ expect(parser.update(prefix + answer.slice(0, -8) + ' '.repeat(2_000_001), true).issues).toBe(2);const recovered = parser.update(prefix + answer, true);expect(recovered.nodes.get('answer')?.props.text).toBe('Readable');expect(recovered.issues).toBe(1);diff --git a/packages/maker-shared/src/richAnswer.ts b/packages/maker-shared/src/richAnswer.tsindex 5df3e0e56d..993b048fec 100644--- a/packages/maker-shared/src/richAnswer.ts+++ b/packages/maker-shared/src/richAnswer.ts@@ -139,11 +139,13 @@ export class RichAnswerStream {private roots: string[] = [];private revision = 0;private issues = 0;+ private failedFinalLine = false;+ private sourceTooLong = false;update(source: string, complete = false): RichDocument {- // A terminal snapshot can precede the remaining text. If its unterminated- // last line was already consumed, replay the corrected snapshot rather than- // parsing just the suffix and retaining the earlier partial-line error.- const extendsFinalLine = source.length > this.source.length+ // Replay only a failed final line. Extending a committed operation with a+ // malformed suffix must not discard the content it already made readable.+ const extendsFinalLine = this.failedFinalLine+ && source.length > this.source.length&& source.length <= MAX_SOURCE&& this.offset === this.source.length&& this.offset > 0@@ -155,21 +157,27 @@ export class RichAnswerStream {this.nodes = new Map();this.roots = [];this.issues = 0;+ this.failedFinalLine = false;this.revision++;}if (source.length > MAX_SOURCE) {- this.issues = Math.max(1, this.issues);+ this.sourceTooLong = true;return this.snapshot();}+ this.sourceTooLong = false;this.source = source;this.nodes = new Map(this.nodes);let end: number;while ((end = source.indexOf("\n", this.offset)) !== -1) {this.line(source.slice(this.offset, end));+ this.failedFinalLine = false;this.offset = end + 1;}if (complete && this.offset < source.length) {- this.line(source.slice(this.offset));+ const failed = !this.line(source.slice(this.offset));+ // A suffix parsed after an earlier committed tail is not a whole line:+ // replaying it would erase that earlier operation on the next snapshot.+ this.failedFinalLine = failed && (this.offset === 0 || source[this.offset - 1] === "\n");this.offset = source.length;}return this.snapshot();@@ -179,17 +187,17 @@ export class RichAnswerStream {nodes: this.nodes,roots: this.roots,revision: this.revision,- issues: this.issues,+ issues: this.sourceTooLong ? Math.max(1, this.issues) : this.issues,};}- private line(line: string) {- if (!line.trim()) return;+ private line(line: string): boolean {+ if (!line.trim()) return true;try {const op: unknown = JSON.parse(line);if (!record(op) || !Number.isSafeInteger(op.seq) || Number(op.seq) < 1)throw Error("sequence");const seq = Number(op.seq);- if (seq <= this.seq) return;+ if (seq <= this.seq) return true;// A malformed or absent line leaves a numbering gap: committed content stays,// the missing operations are reported, and later lines still apply.this.issues += Math.min(seq - this.seq - 1, MAX_NODES);@@ -329,8 +337,10 @@ export class RichAnswerStream {}} else throw Error("operation");this.revision++;+ return true;} catch {this.issues++;+ return false;}}private props(v: unknown): Record<string, unknown> {