Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(frontend): keep distinct reasoning visible beside final answers (#4267)
Rudy Mizrahi Celeklid7c836e6587e
diff --git a/frontend/src/utils/finalAnswer.test.ts b/frontend/src/utils/finalAnswer.test.tsnew file mode 100644index 000000000..ace661ad5--- /dev/null+++ b/frontend/src/utils/finalAnswer.test.ts@@ -0,0 +1,52 @@+import assert from 'node:assert/strict'+import test from 'node:test'++import { thinkingEqualsAnswer } from './finalAnswer.ts'++const context = 'The reviewed request includes the complete supporting documents and all of the information needed for a decision. '+const conclusion = ' The next step is to record the decision in the case history and notify the requester using the established process.'+const decision = (content: string) => `${context}${content}${conclusion}`++for (const [name, thinking, answer] of [+ ['opposite decisions', 'The request is approved.', 'The request is rejected.'],+ ['different amounts', 'The total is $1,000.', 'The total is $9,000.'],+ ['different dates', 'The deadline is 2026-10-13.', 'The deadline is 2026-10-14.'],+ ['a negated conclusion', 'The request can proceed.', 'The request cannot proceed.'],+] as const) {+ test(`keeps long reasoning with ${name} distinct from the final answer`, () => {+ assert.equal(thinkingEqualsAnswer(decision(thinking), decision(answer)), false)+ assert.equal(thinkingEqualsAnswer(decision(answer), decision(thinking)), false)+ })+}++test('deduplicates identical short and long answers', () => {+ assert.equal(thinkingEqualsAnswer('Approved.', 'Approved.'), true)+ const answer = decision('The request is approved.')+ assert.equal(thinkingEqualsAnswer(answer, answer), true)+})++test('deduplicates whitespace differences throughout a long answer', () => {+ const answer = decision('The request is approved.')+ const streamed = `\n ${answer.replaceAll(' ', '\n\t ')} \n`+ assert.equal(thinkingEqualsAnswer(streamed, answer), true)+})++test('deduplicates the existing final-answer wrappers and completed think blocks', () => {+ const answer = decision('The request is approved.')+ for (const thinking of [+ `<answer>${answer}</answer>`,+ `<final_answer><answer>${answer}</answer></final_answer>`,+ `\`\`\`final_answer\n${answer}\n\`\`\``,+ `Final Answer: ${answer}`,+ `<think>Review the evidence before deciding.</think><answer>${answer}</answer>`,+ ]) {+ assert.equal(thinkingEqualsAnswer(thinking, answer), true)+ }+})++test('does not treat empty content or different short answers as duplicates', () => {+ assert.equal(thinkingEqualsAnswer('', ''), false)+ assert.equal(thinkingEqualsAnswer(' \n\t ', 'Approved.'), false)+ assert.equal(thinkingEqualsAnswer('<think>Reviewing.</think>', ''), false)+ assert.equal(thinkingEqualsAnswer('Approved.', 'Rejected.'), false)+})diff --git a/frontend/src/utils/finalAnswer.ts b/frontend/src/utils/finalAnswer.tsindex 13117154e..abb3896be 100644--- a/frontend/src/utils/finalAnswer.ts+++ b/frontend/src/utils/finalAnswer.ts@@ -89,26 +89,12 @@ export function normaliseForComparison(content: string): string {/*** Returns true when `thinking` and `answer` represent the same final answer- * after normalisation. Tolerates small differences (≤2% length delta with- * matching head/tail) to absorb chunk-boundary whitespace noise.+ * after normalisation. Whitespace and answer wrappers are already handled+ * above; differences in the remaining content must keep the thinking visible.*/export function thinkingEqualsAnswer(thinking: string, answer: string): boolean {const a = normaliseForComparison(thinking);const b = normaliseForComparison(answer);if (!a || !b) return false;- if (a === b) return true;-- const maxLen = Math.max(a.length, b.length);- // Require at least ~80 chars of signal to avoid matching trivial prefixes.- if (maxLen < 80) return false;-- const lenDelta = Math.abs(a.length - b.length);- if (lenDelta > maxLen * 0.02) return false;-- const head = 50;- const tail = 50;- return (- a.slice(0, head) === b.slice(0, head) &&- a.slice(-tail) === b.slice(-tail)- );+ return a === b;}