fix(agent): hide trailing thinking event when it duplicates final answer
The natural-stop path on the backend (observe.go Case 1) emits the same
content twice: once as streaming thinking chunks (from
streamThinkingToEventBus's per-chunk callback) which the frontend merges
into a single thinking event, and once as one big EventAgentFinalAnswer.
The result is that the intermediate-steps tree contains a thinking card
whose content is identical to the answer card rendered below it.
Detect this duplication and hide the offending thinking event from the
tree:
- New utils `normaliseForComparison` and `thinkingEqualsAnswer` perform
a tolerant comparison: strip <think>…</think> blocks (which leak into
raw thinking chunks but not into the post-StripThinkBlocks answer),
strip final-answer wrappers, collapse whitespace, then exact-equal or
±2% length with matching head/tail.
- `hiddenThinkingEventIds` (replacing the previous single-id
`promotedThinkingEventId`) now collects two cases of duplicate
thinking: the legacy promotion fallback (no answer event present) and
the natural-stop content-match case.
The final_answer-tool path is unaffected because reasoning thinking
events ("我找到了…让我整理…") and the answer payload have different
content and don't match the equivalence check.
This commit is contained in:
@@ -69,3 +69,45 @@ export function unwrapFinalAnswerWrappers(content: string): string {
|
||||
|
||||
return changed ? result.trim() : result;
|
||||
}
|
||||
|
||||
const THINK_BLOCK_RE = /<think\b[^>]*>[\s\S]*?<\/think>/gi;
|
||||
|
||||
/**
|
||||
* Normalise content for cross-event comparison. Strips <think>…</think>
|
||||
* blocks (which appear in raw thinking chunks but not in the final answer
|
||||
* event), strips final-answer wrappers, and collapses whitespace. Used to
|
||||
* detect when the natural-stop path emits the same content twice (once as
|
||||
* streaming thinking chunks and once as a final answer event).
|
||||
*/
|
||||
export function normaliseForComparison(content: string): string {
|
||||
if (!content || typeof content !== 'string') return '';
|
||||
const stripped = content.replace(THINK_BLOCK_RE, '');
|
||||
const unwrapped = unwrapFinalAnswerWrappers(stripped);
|
||||
return unwrapped.replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
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)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user