From 8079dd8def710c32d05e6ca343c6bd58fc5fbc4e Mon Sep 17 00:00:00 2001 From: XZH Date: Tue, 29 Sep 2026 11:14:09 -0700 Subject: [PATCH] fix(chat): label sent-message KB references with catalog names The reference chips under a sent message printed the stored request-snapshot entry verbatim, so workspace-owned knowledge bases showed their qualified ref (workspace::kb:) instead of the readable name the composer shows for the same selection. Thread the catalogs' ref-to-name resolution into ChatMessageList/ UserMessage and use it for the Knowledge chip label, keeping the qualified ref as identity and fallback. Wired from the chat workspace catalog and Mastery study topic sources, with a regression test for both the resolved label and the fallback. Fixes #1647 Co-authored-by: multica-agent --- .../space/learning/MasteryStudy.tsx | 16 ++++++++ .../chat/components/ChatWorkspace.tsx | 9 +++++ .../chat/messages/ChatMessageList.tsx | 14 ++++++- web/tests/integration/chat-messages.spec.tsx | 39 +++++++++++++++++++ 4 files changed, 76 insertions(+), 2 deletions(-) diff --git a/web/components/space/learning/MasteryStudy.tsx b/web/components/space/learning/MasteryStudy.tsx index dbb035339..555f13395 100644 --- a/web/components/space/learning/MasteryStudy.tsx +++ b/web/components/space/learning/MasteryStudy.tsx @@ -242,6 +242,21 @@ export function MasteryStudy({ () => buildChatOutline(state.messages, state.selectedBranches), [state.messages, state.selectedBranches], ); + // The study composer lists KB sources by label, but the stored request + // snapshot keeps the qualified ref — resolve it so sent-message reference + // chips read the same way. + const kbDisplayNames = useMemo( + () => + Object.fromEntries( + (topic?.sources ?? []) + .filter( + (source) => + source.kind === "knowledge_base" && source.source_id && source.label, + ) + .map((source) => [source.source_id as string, source.label]), + ), + [topic], + ); const jumpToTurn = useCallback( (key: string) => { if ( @@ -807,6 +822,7 @@ export function MasteryStudy({ } }} availableKbNames={new Set(knowledgeBases)} + kbDisplayNames={kbDisplayNames} showModeBadge={false} /> diff --git a/web/features/chat/components/ChatWorkspace.tsx b/web/features/chat/components/ChatWorkspace.tsx index ff98cce8c..f82ee3efb 100644 --- a/web/features/chat/components/ChatWorkspace.tsx +++ b/web/features/chat/components/ChatWorkspace.tsx @@ -328,6 +328,12 @@ export default function ChatWorkspace({ () => new Set(knowledgeBases.map(knowledgeBaseRef)), [knowledgeBases], ); + // Sent-message reference chips show the readable KB name; the snapshot + // stores the qualified ref, so the label is resolved through this map. + const kbDisplayNames = useMemo( + () => Object.fromEntries(knowledgeBases.map((kb) => [knowledgeBaseRef(kb), kb.name])), + [knowledgeBases], + ); // A connected agent to preselect once it loads, from `?agent=` on the // URL (the partner list page links here to drop straight into a chat with a // partner). Captured once at first client render — the URL is rewritten to @@ -2652,6 +2658,9 @@ export default function ChatWorkspace({ availableKbNames={ knowledgeBasesLoaded ? availableKbNames : undefined } + kbDisplayNames={ + knowledgeBasesLoaded ? kbDisplayNames : undefined + } />
void; /** Names of KBs confirmed to exist. Omitted when the KB list is unavailable. */ availableKbNames?: Set; + /** Qualified KB ref -> display name from the selection catalog; the chip + * label falls back to the raw ref when no entry matches. */ + kbDisplayNames?: Record; /** Label the bubble with its capability. A single-capability surface * already names the mode in its own chrome. */ showModeBadge?: boolean; @@ -1636,7 +1640,7 @@ export const UserMessage = memo(function UserMessage({ key: `kb-${name}`, icon: Database, kind: t("Knowledge"), - label: name, + label: kbDisplayNames?.[name] ?? name, }; }), ...(snap?.bookReferences ?? []).map((ref): ContextTreeItem => ({ @@ -1881,6 +1885,7 @@ export const ChatMessageList = memo(function ChatMessageList({ onEditMessage, onSwitchBranch, availableKbNames, + kbDisplayNames, onSubmitUserReply, onAnswerMasteryQuestion, onSkipMasteryQuestion, @@ -1942,8 +1947,12 @@ export const ChatMessageList = memo(function ChatMessageList({ ) => void | boolean | Promise; /** Names of KBs confirmed to exist. Omitted when the KB list is unavailable. */ availableKbNames?: Set; + /** Qualified KB ref -> display name, from the same catalog the composer + * resolves against. Snapshots store the ref; only the chip label should + * show the human-readable name (falls back to the ref when unmapped). */ + kbDisplayNames?: Record; /** Label each user bubble with its capability. Off on surfaces that run a - * single capability and already name it in their own chrome. */ + * single capability and already name it in its own chrome. */ showModeBadge?: boolean; onLoadMessageTrace?: (messageId: number) => Promise; onReleaseMessageTrace?: (messageId: number) => void; @@ -2176,6 +2185,7 @@ export const ChatMessageList = memo(function ChatMessageList({ siblingInfo={sib} onSwitchBranch={onSwitchBranch} availableKbNames={availableKbNames} + kbDisplayNames={kbDisplayNames} showModeBadge={showModeBadge} onOpenConsultation={consultationEvents.length && onOpenConsultation ? () => onOpenConsultation(consultationEvents) diff --git a/web/tests/integration/chat-messages.spec.tsx b/web/tests/integration/chat-messages.spec.tsx index 3f6b90466..a05aee91e 100644 --- a/web/tests/integration/chat-messages.spec.tsx +++ b/web/tests/integration/chat-messages.spec.tsx @@ -38,6 +38,45 @@ describe('chat message feature', () => { expect(retry).toHaveBeenCalledOnce() }) + // The snapshot stores the qualified KB ref, but the reference chip under a + // sent message used to print that ref verbatim — while the composer chip + // for the same resource showed its readable name. The label now resolves + // through the selection catalog, falling back to the ref when unmapped. + it('labels sent-message KB references with the catalog name, not the raw ref', () => { + const renderWithKb = (kb: string[], kbDisplayNames?: Record) => + render( + undefined)} + onRegenerateMessage={() => undefined} + kbDisplayNames={kbDisplayNames} + />, + ) + // Identity is preserved: an unmapped ref still renders, as itself. + renderWithKb(['account:kb:legacy_kb']) + expect(screen.getByText('account:kb:legacy_kb')).toBeVisible() + + const { unmount } = renderWithKb( + ['workspace:ws-1:kb:example_kb'], + { 'workspace:ws-1:kb:example_kb': 'example_kb' }, + ) + expect(screen.getByText('example_kb')).toBeVisible() + expect(screen.queryByText('workspace:ws-1:kb:example_kb')).toBeNull() + unmount() + }) + it('renders a user row with keyboard-accessible message actions', async () => { const copy = vi.fn(async () => undefined) const user = userEvent.setup()