diff --git a/frontend/src/composables/useStreamingHeightFloor.test.ts b/frontend/src/composables/useStreamingHeightFloor.test.ts new file mode 100644 index 00000000..90a44c7e --- /dev/null +++ b/frontend/src/composables/useStreamingHeightFloor.test.ts @@ -0,0 +1,17 @@ +import assert from 'node:assert/strict' +import { test } from 'node:test' +import { nextHeightFloor } from './useStreamingHeightFloor' + +test('streaming height floor grows at once and absorbs shrinks up to the cap', () => { + let floor = 0 + floor = nextHeightFloor(floor, 400, 200) + assert.equal(floor, 400) + floor = nextHeightFloor(floor, 300, 200) + assert.equal(floor, 400, 'a folded step leaves slack instead of shrinking') + floor = nextHeightFloor(floor, 350, 200) + assert.equal(floor, 400, 'the next step fills the slack before the reply grows') + floor = nextHeightFloor(floor, 450, 200) + assert.equal(floor, 450) + floor = nextHeightFloor(floor, 100, 200) + assert.equal(floor, 300, 'slack never exceeds the cap, keeping new content in view') +}) diff --git a/frontend/src/composables/useStreamingHeightFloor.ts b/frontend/src/composables/useStreamingHeightFloor.ts new file mode 100644 index 00000000..3ca04b3e --- /dev/null +++ b/frontend/src/composables/useStreamingHeightFloor.ts @@ -0,0 +1,65 @@ +import { onBeforeUnmount, onMounted, onUpdated, watch, type Ref } from 'vue' + +/** + * Next minimum height for a streaming message. Growth raises the floor at once; + * a shrink keeps up to `cap` pixels of slack so the following step can fill it. + */ +export function nextHeightFloor(floor: number, content: number, cap: number): number { + return Math.max(content, Math.min(floor, content + cap)) +} + +function contentHeight(node: HTMLElement): number { + const top = node.getBoundingClientRect().top + let bottom = top + for (const child of Array.from(node.children)) { + bottom = Math.max(bottom, child.getBoundingClientRect().bottom) + } + return bottom - top +} + +/** + * While a reply streams, steps fold and previews disappear. A follow-the-bottom + * viewport turns every such shrink into the whole conversation dropping, then + * climbing again as the next step grows. Holding a height floor keeps scroll + * motion one-directional; the slack is capped so the latest content stays in + * view, and released before the completed timeline folds. + */ +export function useStreamingHeightFloor( + el: Ref, + active: Readonly>, + capRatio = 0.3, +): void { + let floor = 0 + let observer: ResizeObserver | null = null + + const apply = () => { + const node = el.value + if (!node || !active.value) return + const next = nextHeightFloor(floor, contentHeight(node), window.innerHeight * capRatio) + if (next === floor) return + floor = next + node.style.minHeight = `${Math.ceil(next)}px` + } + + const release = () => { + floor = 0 + if (el.value) el.value.style.minHeight = '' + } + + // Pre-flush: drop the floor before the render that folds the finished timeline. + watch(active, on => (on ? apply() : release())) + + onMounted(() => { + if (!el.value || typeof ResizeObserver === 'undefined') return + observer = new ResizeObserver(apply) + observer.observe(el.value) + apply() + }) + // A shrink held by the floor does not resize the element, so re-check the + // slack cap after each render. + onUpdated(apply) + onBeforeUnmount(() => { + observer?.disconnect() + observer = null + }) +} diff --git a/frontend/src/i18n/embed.ts b/frontend/src/i18n/embed.ts index eb18a407..bea16fe0 100644 --- a/frontend/src/i18n/embed.ts +++ b/frontend/src/i18n/embed.ts @@ -296,6 +296,8 @@ const messages = { "stepsCompleted": "已完成 {steps} 个步骤", "stepsCompletedWithDuration": "已完成 {steps} 个步骤,耗时 {duration}", "reasoningRounds": "思考 {rounds} 轮", + "earlierSteps": "已完成 {count} 个步骤", + "viewReasoning": "查看思路", "toolCalls": "调用 {tools} 次工具", "durationSuffix": "耗时 {duration}", "stepSummarySeparator": " · " @@ -882,6 +884,8 @@ const messages = { "stepsCompleted": "已完成 {steps} 個步驟", "stepsCompletedWithDuration": "已完成 {steps} 個步驟,耗時 {duration}", "reasoningRounds": "思考 {rounds} 輪", + "earlierSteps": "已完成 {count} 個步驟", + "viewReasoning": "檢視思路", "toolCalls": "呼叫 {tools} 次工具", "durationSuffix": "耗時 {duration}", "stepSummarySeparator": " · " @@ -1468,6 +1472,8 @@ const messages = { "stepsCompleted": "{steps}ステップ完了", "stepsCompletedWithDuration": "{steps}ステップ完了(所要時間{duration})", "reasoningRounds": "推論{rounds}回", + "earlierSteps": "完了したステップ {count} 件", + "viewReasoning": "思考を表示", "toolCalls": "ツール呼び出し{tools}回", "durationSuffix": "所要時間{duration}", "stepSummarySeparator": " · " @@ -2054,6 +2060,8 @@ const messages = { "stepsCompleted": "Completed {steps} step(s)", "stepsCompletedWithDuration": "Completed {steps} step(s) in {duration}", "reasoningRounds": "{rounds} reasoning round(s)", + "earlierSteps": "{count} earlier steps", + "viewReasoning": "View reasoning", "toolCalls": "{tools} tool call(s)", "durationSuffix": "{duration}", "stepSummarySeparator": " · " diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.ts index cf6b0ef0..4d0ab099 100755 --- a/frontend/src/i18n/locales/en-US.ts +++ b/frontend/src/i18n/locales/en-US.ts @@ -1226,6 +1226,8 @@ export default { toolFallback: 'Tool', stepsCompleted: 'Completed {steps} step(s)', reasoningRounds: '{rounds} reasoning round(s)', + earlierSteps: '{count} earlier steps', + viewReasoning: 'View reasoning', toolCalls: '{tools} tool call(s)', durationSuffix: '{duration}', stepSummarySeparator: ' · ', diff --git a/frontend/src/i18n/locales/ja-JP.ts b/frontend/src/i18n/locales/ja-JP.ts index 44e0a409..d172a256 100644 --- a/frontend/src/i18n/locales/ja-JP.ts +++ b/frontend/src/i18n/locales/ja-JP.ts @@ -1226,6 +1226,8 @@ export default { toolFallback: 'ツール', stepsCompleted: '{steps}ステップ完了', reasoningRounds: '推論{rounds}回', + earlierSteps: '完了したステップ {count} 件', + viewReasoning: '思考を表示', toolCalls: 'ツール呼び出し{tools}回', durationSuffix: '{duration}', stepSummarySeparator: ' · ', diff --git a/frontend/src/i18n/locales/ko-KR.ts b/frontend/src/i18n/locales/ko-KR.ts index 869578fd..29109297 100755 --- a/frontend/src/i18n/locales/ko-KR.ts +++ b/frontend/src/i18n/locales/ko-KR.ts @@ -7238,6 +7238,8 @@ export default { toolFallback: '도구', stepsCompleted: '{steps}개 단계 완료', reasoningRounds: '사고 {rounds}회', + earlierSteps: '이전 단계 {count}개', + viewReasoning: '사고 과정 보기', toolCalls: '도구 {tools}회 호출', durationSuffix: '소요 시간 {duration}', stepSummarySeparator: ' · ', diff --git a/frontend/src/i18n/locales/ru-RU.ts b/frontend/src/i18n/locales/ru-RU.ts index 4aa8e18e..d385fa0b 100755 --- a/frontend/src/i18n/locales/ru-RU.ts +++ b/frontend/src/i18n/locales/ru-RU.ts @@ -7238,6 +7238,8 @@ export default { toolFallback: 'Инструмент', stepsCompleted: 'Выполнено {steps} шаг(ов)', reasoningRounds: '{rounds} раунд(ов) рассуждений', + earlierSteps: 'Предыдущие шаги: {count}', + viewReasoning: 'Показать рассуждения', toolCalls: '{tools} вызов(ов) инструментов', durationSuffix: '{duration}', stepSummarySeparator: ' · ', diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts index 91bb0fc1..91be9218 100755 --- a/frontend/src/i18n/locales/zh-CN.ts +++ b/frontend/src/i18n/locales/zh-CN.ts @@ -7239,6 +7239,8 @@ export default { toolFallback: '工具', stepsCompleted: '已完成 {steps} 个步骤', reasoningRounds: '思考 {rounds} 轮', + earlierSteps: '已完成 {count} 个步骤', + viewReasoning: '查看思路', toolCalls: '调用 {tools} 次工具', durationSuffix: '耗时 {duration}', stepSummarySeparator: ' · ', diff --git a/frontend/src/i18n/locales/zh-TW.ts b/frontend/src/i18n/locales/zh-TW.ts index bc9841a2..7a345234 100644 --- a/frontend/src/i18n/locales/zh-TW.ts +++ b/frontend/src/i18n/locales/zh-TW.ts @@ -7239,6 +7239,8 @@ export default { toolFallback: '工具', stepsCompleted: '已完成 {steps} 個步驟', reasoningRounds: '思考 {rounds} 輪', + earlierSteps: '已完成 {count} 個步驟', + viewReasoning: '檢視思路', toolCalls: '呼叫 {tools} 次工具', durationSuffix: '耗時 {duration}', stepSummarySeparator: ' · ', diff --git a/frontend/src/utils/agentTimelinePresentation.test.ts b/frontend/src/utils/agentTimelinePresentation.test.ts index 5b4a50d1..64199514 100644 --- a/frontend/src/utils/agentTimelinePresentation.test.ts +++ b/frontend/src/utils/agentTimelinePresentation.test.ts @@ -79,3 +79,34 @@ test('progress grouping preserves approval, reasoning, tool, user input and fina assert.deepEqual(groupProgressUpdates(events), events) } }) + +test('finished untitled reasoning rides on the tool call it led to', async () => { + const { attachReasoningToTools } = await import('./agentTimelinePresentation') + const tool = Object.freeze({ type: 'tool_call', tool_call_id: 't1', tool_name: 'knowledge_search' }) + const events = [ + { type: 'thinking', event_id: 'r1', content: 'Search first' }, tool, + { type: 'thinking', event_id: 'r2', title: '我先读一下文档', content: 'Read it' }, + { type: 'tool_call', tool_call_id: 't2', tool_name: 'read_document' }, + { type: 'thinking', event_id: 'r3', content: 'Still thinking' }, + { type: 'tool_call', tool_call_id: 't3', tool_name: 'web_search' }, + { type: 'thinking', event_id: 'r4', content: 'Wrap up' }, + ] + const output = attachReasoningToTools(events, (e: any) => e.event_id !== 'r3') + assert.deepEqual(output.map((e: any) => e.event_id || e.tool_call_id), ['t1', 'r2', 't2', 'r3', 't3', 'r4']) + assert.deepEqual(output[0].reasoning, { id: 'reasoning-r1', content: 'Search first' }) + assert.equal('reasoning' in tool, false) + assert.equal(output[2].reasoning, undefined, 'titled narration keeps its own row') + assert.equal(output[4].reasoning, undefined, 'reasoning that cannot attach keeps its own row') +}) + +test('earlier steps fold behind recent and attention-demanding rows', async () => { + const { earlierStepKeys } = await import('./agentTimelinePresentation') + const rows = (n: number, sticky: string[] = []) => Array.from({ length: n }, (_, i) => ({ + key: `k${i}`, step: true, sticky: sticky.includes(`k${i}`), + })) + assert.deepEqual(earlierStepKeys(rows(4)), [], 'folding a single row saves nothing') + assert.deepEqual(earlierStepKeys(rows(6)), ['k0', 'k1', 'k2']) + assert.deepEqual(earlierStepKeys(rows(5, ['k1'])), [], 'sticky rows stay out of the fold') + assert.deepEqual(earlierStepKeys(rows(6, ['k1'])), ['k0', 'k2']) + assert.deepEqual(earlierStepKeys([...rows(6), { key: 'answer', step: false, sticky: false }]), ['k0', 'k1', 'k2']) +}) diff --git a/frontend/src/utils/agentTimelinePresentation.ts b/frontend/src/utils/agentTimelinePresentation.ts index 18856024..dbacdf49 100644 --- a/frontend/src/utils/agentTimelinePresentation.ts +++ b/frontend/src/utils/agentTimelinePresentation.ts @@ -21,6 +21,46 @@ export function foldProgressIntoThinking(events: any[]): any[] { return result } +/** + * Move finished, untitled reasoning onto the tool call it led to, so the + * timeline shows one row per action instead of alternating thought/tool rows. + * Titled reasoning (a relocated preamble) is user-facing narration and stays. + */ +export function attachReasoningToTools(events: any[], canAttach: (event: any) => boolean): any[] { + const result: any[] = [] + for (let i = 0; i < events.length; i++) { + const event = events[i] + const next = events[i + 1] + const content = typeof event?.content === 'string' ? event.content.trim() : '' + if (event?.type === 'thinking' && !event.title && content && event.event_id + && next?.type === 'tool_call' && next.tool_name !== 'thinking' && !next.reasoning + && canAttach(event)) { + result.push({ ...next, reasoning: { id: `reasoning-${event.event_id}`, content: event.content } }) + i++ + continue + } + result.push(event) + } + return result +} + +/** + * Keys of early timeline rows to fold while a run streams. The most recent + * rows and anything still demanding attention stay visible; folding fewer + * than `minFolded` rows would cost a row without saving one. + */ +export function earlierStepKeys( + rows: { key: string, step: boolean, sticky: boolean }[], + keepRecent = 3, + minFolded = 2, +): string[] { + const steps = rows.filter(row => row.step) + const keys = steps.slice(0, Math.max(0, steps.length - keepRecent)) + .filter(row => !row.sticky) + .map(row => row.key) + return keys.length >= minFolded ? keys : [] +} + /** Compact adjacent visible updates without crossing tools, prompts or answers. */ export function groupProgressUpdates(events: any[]): any[] { const result: any[] = [] diff --git a/frontend/src/views/chat/components/AgentStreamDisplay.style.test.mjs b/frontend/src/views/chat/components/AgentStreamDisplay.style.test.mjs index 3eb44da9..4cf94169 100644 --- a/frontend/src/views/chat/components/AgentStreamDisplay.style.test.mjs +++ b/frontend/src/views/chat/components/AgentStreamDisplay.style.test.mjs @@ -52,7 +52,9 @@ test('agent steps use compact muted timeline styling', () => { }) test('expanded agent step log keeps model thinking in the tool timeline', () => { - assert.match(source, /visibleIntermediateEvents\s*=\s*computed\(\(\) => intermediateEvents\.value\)/) + assert.match(source, /visibleIntermediateEvents\s*=\s*computed\(\(\) => attachReasoningToTools\(intermediateEvents\.value, canAttachReasoning\)\)/) + assert.match(source, /v-for="\(event, index\) in timelineEvents"/) + assert.match(source, /class="action-reasoning thinking-detail-content markdown-content"/) assert.match(source, /v-for="\(event, index\) in visibleIntermediateEvents"/) }) diff --git a/frontend/src/views/chat/components/AgentStreamDisplay.vue b/frontend/src/views/chat/components/AgentStreamDisplay.vue index e7b69635..f0d1c0d9 100644 --- a/frontend/src/views/chat/components/AgentStreamDisplay.vue +++ b/frontend/src/views/chat/components/AgentStreamDisplay.vue @@ -74,7 +74,7 @@
-
+
@@ -82,7 +82,8 @@ aria-hidden="true" /> {{ event.title }}
+ class="thinking-inline-content markdown-content" + :class="{ 'thinking-live-window': isLiveThinking(event.event_id) }">
{{ getThinkingSummary(event) @@ -90,7 +91,8 @@
-
+
@@ -182,9 +184,21 @@ +{{ getSandboxDiffStat(event)?.added }} -{{ getSandboxDiffStat(event)?.removed }} +
+
+
+
+
{{ getSandboxFilePreview(event) }}
@@ -298,8 +312,27 @@ @toggle="toggleMemory" @forget="forgetMemory" /> -