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:<id>:kb:<name>) 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 <github@multica.ai>
This commit is contained in:
XZH
2026-09-29 11:14:09 -07:00
parent ef2d9e5c3c
commit 8079dd8def
4 changed files with 76 additions and 2 deletions

View File

@@ -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}
/>
</div>

View File

@@ -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=<name>` 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
}
/>
<div
ref={messagesEndRef}

View File

@@ -1533,6 +1533,7 @@ export const UserMessage = memo(function UserMessage({
siblingInfo,
onSwitchBranch,
availableKbNames,
kbDisplayNames,
showModeBadge,
onOpenConsultation,
}: {
@@ -1546,6 +1547,9 @@ export const UserMessage = memo(function UserMessage({
onSwitchBranch?: (parentMessageId: number | null, childId: number) => void;
/** Names of KBs confirmed to exist. Omitted when the KB list is unavailable. */
availableKbNames?: Set<string>;
/** Qualified KB ref -> display name from the selection catalog; the chip
* label falls back to the raw ref when no entry matches. */
kbDisplayNames?: Record<string, string>;
/** 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<void | boolean>;
/** Names of KBs confirmed to exist. Omitted when the KB list is unavailable. */
availableKbNames?: Set<string>;
/** 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<string, string>;
/** 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<void>;
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)

View File

@@ -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<string, string>) =>
render(
<ChatMessageList
messages={[{
id: 7,
role: 'user',
content: 'Summarize the papers',
parentMessageId: null,
requestSnapshot: {
content: 'Summarize the papers',
enabledTools: [],
knowledgeBases: kb,
language: 'en',
},
}]}
isStreaming={false}
onCopyAssistantMessage={vi.fn(async () => 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()