diff --git a/frontend/src/composables/useChatMemoryRow.ts b/frontend/src/composables/useChatMemoryRow.ts new file mode 100644 index 00000000..37457ab1 --- /dev/null +++ b/frontend/src/composables/useChatMemoryRow.ts @@ -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([]) + + 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 } +} diff --git a/frontend/src/views/chat/components/AgentStreamDisplay.style.test.mjs b/frontend/src/views/chat/components/AgentStreamDisplay.style.test.mjs index ddc17c7a..0b335949 100644 --- a/frontend/src/views/chat/components/AgentStreamDisplay.style.test.mjs +++ b/frontend/src/views/chat/components/AgentStreamDisplay.style.test.mjs @@ -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('\s*\n\s* { assert.doesNotMatch(source, /&\.action-pending\s*\{[\s\S]*&::after/) }) diff --git a/frontend/src/views/chat/components/AgentStreamDisplay.vue b/frontend/src/views/chat/components/AgentStreamDisplay.vue index 92397ed3..913a8bbd 100644 --- a/frontend/src/views/chat/components/AgentStreamDisplay.vue +++ b/frontend/src/views/chat/components/AgentStreamDisplay.vue @@ -18,6 +18,14 @@
+