fix(frontend): 修复占位行样式并把记忆行统一收进时间轴

「正在准备回答」占位节点缺少 .tool-event 包装,图标定位与行高规则都挂在该
层下,shimmer 文案因行高不足被裁切。补上包装,与同组件其他步骤结构一致。

Agent 模式的记忆行原先是悬在时间轴上方的独立卡片,现在作为时间轴首个节点
渲染,折叠时随中间步骤一起收起。召回列表、展开状态与「忘记」调用抽成
useChatMemoryRow,两条时间轴共用,避免各写一套后行为分叉。
main
wizardchen 2026-08-13 20:57:08 +08:00 committed by lyingbug
parent 1d68b4dcd7
commit 9ee9fa0b52
6 changed files with 153 additions and 47 deletions

View File

@ -0,0 +1,50 @@
import { computed, ref } from 'vue'
import { MessagePlugin } from 'tdesign-vue-next'
import { useI18n } from 'vue-i18n'
import { deleteMemoryItem } from '@/api/memory'
export type UsedMemory = { id: string; kind: string; content: string }
/**
* State for the "memories used by this turn" timeline row.
*
* Both timelines (the quick-answer pipeline and the agent stream) show the same
* row, so the recall list, the expand state and the forget call live here rather
* than being written twice and drifting apart.
*/
export function useChatMemoryRow(getUsedMemories: () => UsedMemory[] | undefined) {
const { t } = useI18n()
const expanded = ref(false)
const forgettingId = ref('')
const forgottenIds = ref<string[]>([])
const memoryItems = computed(() => {
const used = getUsedMemories()
if (!Array.isArray(used)) return []
return used.filter((memory) => memory?.id && !forgottenIds.value.includes(memory.id))
})
const hasMemory = computed(() => memoryItems.value.length > 0)
const toggle = () => {
expanded.value = !expanded.value
}
// Forgetting from the answer is the shortest path from noticing a wrong memory
// to it being gone, which is where users actually notice one.
const forget = async (memory: { id: string }) => {
forgettingId.value = memory.id
try {
await deleteMemoryItem(memory.id)
forgottenIds.value = [...forgottenIds.value, memory.id]
MessagePlugin.success(t('chat.memoryForgotten'))
} catch (error: any) {
MessagePlugin.error(error?.message || t('chat.memoryForgetFailed'))
} finally {
forgettingId.value = ''
}
}
return { memoryItems, hasMemory, expanded, forgettingId, toggle, forget }
}

View File

@ -92,6 +92,22 @@ test('only the collapsed root summary shows an expand chevron', () => {
assert.doesNotMatch(source, /isEventExpanded\(event\.event_id\) \? 'chevron/)
})
// Recalled memory is one more thing the turn did before answering, so it rides
// the same timeline as the steps instead of sitting in a card above them. It has
// to travel into the collapsed tree with them, and appear exactly once.
test('recalled memory rides the agent timeline as its leading row', () => {
const template = source.split('<script')[0]
assert.equal((template.match(/<ChatMemoryStep/g) || []).length, 2)
assert.match(template, /<div v-if="showIntermediateSteps" class="tree-children">\s*\n\s*<ChatMemoryStep/)
assert.match(template, /<ChatMemoryStep\s*\n\s*v-if="showMemoryRow"/)
assert.match(template, /<ChatMemoryStep[\s\S]*?:is-last="memoryIsLast"/)
assert.match(source, /!props\.ragMode && hasMemory\.value && !shouldShowCollapsedSteps\.value/)
assert.match(
source,
/const memoryIsLast = computed\([\s\S]*lastStreamingTimelineEventIndex\.value === -1/,
)
})
test('pending tool rows do not render an extra axis dot', () => {
assert.doesNotMatch(source, /&\.action-pending\s*\{[\s\S]*&::after/)
})

View File

@ -18,6 +18,14 @@
</div>
<!-- Tree children (intermediate steps) -->
<div v-if="showIntermediateSteps" class="tree-children">
<ChatMemoryStep
v-if="hasMemory"
:memories="memoryItems"
:expanded="memoryExpanded"
:forgetting-id="memoryForgettingId"
@toggle="toggleMemory"
@forget="forgetMemory"
/>
<template v-for="(event, index) in visibleIntermediateEvents" :key="getEventKey(event, index)">
<div v-if="event && event.type" class="tree-child"
:class="{ 'tree-child-last': !isConversationDone && index === visibleIntermediateEvents.length - 1 }">
@ -213,6 +221,19 @@
'streaming-steps-constrained': !answerEverStarted && !isConversationDone,
'is-streaming-timeline': showStreamingTimeline
}">
<!-- Recalled memory leads the timeline: it is what the turn knew before it
started, so it belongs on the same line as the steps that follow it
rather than in a card of its own above them. -->
<ChatMemoryStep
v-if="showMemoryRow"
class="event-item"
:memories="memoryItems"
:expanded="memoryExpanded"
:is-last="memoryIsLast"
:forgetting-id="memoryForgettingId"
@toggle="toggleMemory"
@forget="forgetMemory"
/>
<template v-for="(event, index) in displayEvents" :key="getEventKey(event, index)">
<div v-if="event && event.type" class="event-item" :class="{
'event-answer': event.type === 'answer',
@ -517,6 +538,8 @@ import ChatRequestInfoButton from '@/components/ChatRequestInfoButton.vue';
import ChatCitationFloat from '@/components/ChatCitationFloat.vue';
import picturePreview from '@/components/picture-preview.vue';
import ChatArtifactsDrawer from './ChatArtifactsDrawer.vue';
import ChatMemoryStep from './ChatMemoryStep.vue';
import { useChatMemoryRow, type UsedMemory } from '@/composables/useChatMemoryRow';
import { countGrepDocuments, groupGrepChunkResults } from '@/utils/grepResultsGroup';
import { getKnowledgeChunksSummaryHtml } from '@/utils/knowledgeChunksDisplay';
import { getAttachmentParsingSummaryHtml } from '@/utils/attachmentParsingDisplay';
@ -844,6 +867,15 @@ const showRequestInfo = computed(
() => !props.embeddedMode && !!(props.session?.request_id || props.session?.id),
);
const {
memoryItems,
hasMemory,
expanded: memoryExpanded,
forgettingId: memoryForgettingId,
toggle: toggleMemory,
forget: forgetMemory,
} = useChatMemoryRow(() => props.session?.used_memories as UsedMemory[] | undefined);
const resolveAssistantMessageId = (session?: SessionData) =>
String(session?.assistant_message_id || session?.id || '').trim();
@ -1665,6 +1697,19 @@ const shouldShowCollapsedSteps = computed(() => {
return hasSteps && isConversationDone.value;
});
// Once the steps collapse, the memory row travels with them into the tree —
// showing it here as well would leave two rows saying the same thing. In
// quick-answer mode the pipeline component owns the timeline and its memory row.
const showMemoryRow = computed(
() => !props.ragMode && hasMemory.value && !shouldShowCollapsedSteps.value,
);
// Memory leads the timeline, so it is only the last node while nothing has
// followed it yet — and a lone node has no trunk line to draw below it.
const memoryIsLast = computed(
() => lastStreamingTimelineEventIndex.value === -1 && !showAgentActivityIndicator.value,
);
// Check if event is a "deep thinking" type (either streaming thinking or thinking tool call)
const isThinkingLikeEvent = (event: any): boolean => {
if (event.type === 'thinking') return true;

View File

@ -49,6 +49,7 @@ test('rag pipeline opens references from search steps and the drawer composable'
test('rag pipeline uses a native pending step and lets the thinking title shimmer while pending', () => {
assert.match(source, /showPrePipelineWait/)
assert.match(source, /v-else-if="showPrePipelineWait"[\s\S]*class="tool-event"/)
assert.match(source, /class="action-card action-pending"/)
assert.match(source, /t\('chat\.preparingAnswer'\)/)
assert.match(source, /showThinkingStep/)
@ -128,7 +129,23 @@ test('memory-only hosts get the memory row and nothing else', () => {
assert.match(source, /v-if="memoryOnly"\s*\n\s*variant="root"/)
assert.match(source, /<div v-else-if="showPrePipelineWait"/)
assert.match(source, /if \(props\.memoryOnly\) return hasMemory\.value/)
const botmsg = readFileSync(join(here, 'botmsg.vue'), 'utf8')
assert.match(botmsg, /<RagPipelineProgress v-if="session\.used_memories\?\.length"[\s\S]*?memory-only/)
})
// The standalone row is for answers with no timeline at all. Agent turns keep
// their memory row inside the agent timeline, so rendering it here too would
// show the same recall twice, in two different visual languages.
test('only timeline-less answers get the standalone memory row', () => {
const botmsg = readFileSync(join(here, 'botmsg.vue'), 'utf8')
assert.match(
botmsg,
/<RagPipelineProgress v-if="!session\.isAgentMode && session\.used_memories\?\.length"[\s\S]*?memory-only/,
)
})
// Both timelines show the same row from the same state; duplicating the recall
// list, expand state and forget call is how the two drift apart.
test('memory row state is shared by both timelines', () => {
assert.match(source, /useChatMemoryRow\(\(\) => props\.session\?\.used_memories\)/)
const agent = readFileSync(join(here, 'AgentStreamDisplay.vue'), 'utf8')
assert.match(agent, /useChatMemoryRow\(/)
})

View File

@ -22,11 +22,13 @@
<div class="tree-child tree-child-last streaming-loading-node">
<div class="tree-branch" />
<div class="tree-child-content">
<div class="action-card action-pending">
<div class="action-header no-results">
<div class="action-title">
<t-icon class="action-title-icon" name="lightbulb" />
<span class="action-name">{{ t('chat.preparingAnswer') }}</span>
<div class="tool-event">
<div class="action-card action-pending">
<div class="action-header no-results">
<div class="action-title">
<t-icon class="action-title-icon" name="lightbulb" />
<span class="action-name">{{ t('chat.preparingAnswer') }}</span>
</div>
</div>
</div>
</div>
@ -247,10 +249,9 @@
<script setup lang="ts">
import { computed, nextTick, onBeforeUnmount, ref, watch } from 'vue'
import { MessagePlugin } from 'tdesign-vue-next'
import { useI18n } from 'vue-i18n'
import { deleteMemoryItem } from '@/api/memory'
import ChatMemoryStep from './ChatMemoryStep.vue'
import { useChatMemoryRow } from '@/composables/useChatMemoryRow'
import { getAgentToolIconName } from '@/utils/agent-tool-icons'
import {
getKnowledgeSearchSummaryHtml,
@ -295,36 +296,14 @@ const waitController = createRagWaitController((view) => {
// Long-term memory is shown as a timeline row rather than as a card of its
// own: it is one more thing the turn did before answering, and giving it a
// separate visual language would make it read as unrelated to the pipeline.
const memoryExpanded = ref(false)
const forgettingId = ref('')
const forgottenIds = ref<string[]>([])
const memoryItems = computed(() => {
const used = props.session?.used_memories
if (!Array.isArray(used)) return []
return used.filter((memory) => memory?.id && !forgottenIds.value.includes(memory.id))
})
const hasMemory = computed(() => memoryItems.value.length > 0)
const toggleMemory = () => {
memoryExpanded.value = !memoryExpanded.value
}
// Forgetting from the answer is the shortest path from noticing a wrong memory
// to it being gone, which is where users actually notice one.
const forgetMemory = async (memory: { id: string }) => {
forgettingId.value = memory.id
try {
await deleteMemoryItem(memory.id)
forgottenIds.value = [...forgottenIds.value, memory.id]
MessagePlugin.success(t('chat.memoryForgotten'))
} catch (error: any) {
MessagePlugin.error(error?.message || t('chat.memoryForgetFailed'))
} finally {
forgettingId.value = ''
}
}
const {
memoryItems,
hasMemory,
expanded: memoryExpanded,
forgettingId,
toggle: toggleMemory,
forget: forgetMemory,
} = useChatMemoryRow(() => props.session?.used_memories)
const thinkingContent = computed(() => {
const stream = props.session?.agentEventStream

View File

@ -21,13 +21,12 @@
@render-complete-change="emit('render-complete-change', $event)" />
</div>
<template v-else>
<!-- No pipeline here, but a turn that used long-term memory
still has something to report. Only the memory row is
wanted: agent mode already draws its own timeline, and
letting this component re-derive one from the same event
stream would show every step twice. -->
<RagPipelineProgress v-if="session.used_memories?.length" :session="session"
:embedded-mode="embeddedMode" memory-only />
<!-- A plain answer has no timeline to put the memory row on, so
it gets the standalone row. Agent turns render theirs inside
the agent timeline instead, next to the steps it belongs
with. -->
<RagPipelineProgress v-if="!session.isAgentMode && session.used_memories?.length"
:session="session" :embedded-mode="embeddedMode" memory-only />
<docInfo v-if="session.knowledge_references?.length" :session="session"></docInfo>
<AgentStreamDisplay :session="session" :session-id="sessionId" :user-query="userQuery"
v-if="session.isAgentMode" :follow-up-loading="followUpLoading"