Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(chat): keep RAG/web citations visible after trace collapses

Akarshan Biswase347bb6f2359
diff --git a/web-app/src/components/ai-elements/tool.tsx b/web-app/src/components/ai-elements/tool.tsx
index e221a6426..8334cd577 100644
--- a/web-app/src/components/ai-elements/tool.tsx
+++ b/web-app/src/components/ai-elements/tool.tsx
@@ -24,7 +24,6 @@ import { useToolApproval } from '@/hooks/useToolApproval'
import { useTranslation } from '@/i18n/react-i18next-compat'
import { Button } from '@/components/ui/button'
import { ShieldAlertIcon } from 'lucide-react'
-import { Citations } from '@/components/Citations'
import { parseCitationsFromToolOutput } from '@/lib/citation-parser'
type ToolContextValue = {
@@ -320,7 +319,6 @@ export type ToolOutputProps = ComponentProps<'div'> & {
export const ToolOutput = memo(
({ className, output, errorText, resolver, ...props }: ToolOutputProps) => {
- const { messageId } = useTool()
const citationPayload = useMemo(
() => (output ? parseCitationsFromToolOutput(output) : null),
[output]
@@ -331,13 +329,11 @@ export const ToolOutput = memo(
return null
}
+ // Citation payloads are surfaced as an always-visible card in the message
+ // body (see MessageItem); rendering them here too would duplicate the
+ // anchor ids and hide the sources inside the collapsible trace.
if (citationPayload) {
- return (
- <Citations
- payload={citationPayload}
- anchorPrefix={messageId ? `cite-${messageId}` : undefined}
- />
- )
+ return null
}
// Handle string output
@@ -455,7 +451,7 @@ export const ToolOutput = memo(
}
return <div>{output as ReactNode}</div>
- }, [output, errorText, resolver, citationPayload, messageId])
+ }, [output, errorText, resolver, citationPayload])
if (!(output || errorText)) {
return null
diff --git a/web-app/src/containers/MessageItem.tsx b/web-app/src/containers/MessageItem.tsx
index 675e98649..fbc813ec7 100644
--- a/web-app/src/containers/MessageItem.tsx
+++ b/web-app/src/containers/MessageItem.tsx
@@ -40,7 +40,8 @@ import { PromptProgress } from '@/components/PromptProgress'
import { useServiceHub } from '@/hooks/useServiceHub'
import { useToolApproval } from '@/hooks/useToolApproval'
import { parseCitationsFromToolOutput } from '@/lib/citation-parser'
-import type { RagCitation } from '@/components/Citations'
+import { Citations } from '@/components/Citations'
+import type { CitationsPayload, RagCitation } from '@/components/Citations'
import { useGroundingStore } from '@/stores/grounding-store'
import { injectCitationMarkers } from '@/lib/grounding'
@@ -163,18 +164,32 @@ export const MessageItem = memo(
status === CHAT_STATUS.SUBMITTED)) ||
hasPendingToolCall
- const ragCitations = useMemo<RagCitation[]>(() => {
- if (message.role !== 'assistant') return []
- const out: RagCitation[] = []
- for (const part of message.parts as any[]) {
- if (!part.type?.startsWith('tool-')) continue
- if (part.state !== 'output-available') continue
- const parsed = parseCitationsFromToolOutput(part.output)
- if (parsed?.kind === 'rag') out.push(...parsed.citations)
+ // Aggregate citation payloads from every retrieve/search tool call. The RAG
+ // payloads are merged into one (so grounding anchors `cite-<id>-<n>` line up
+ // with a single ordered list); web payloads stay per-call.
+ const { ragPayload, webPayloads } = useMemo(() => {
+ const rags: Extract<CitationsPayload, { kind: 'rag' }>[] = []
+ const webs: Extract<CitationsPayload, { kind: 'web' }>[] = []
+ if (message.role === 'assistant') {
+ for (const part of message.parts as any[]) {
+ if (!part.type?.startsWith('tool-')) continue
+ if (part.state !== 'output-available') continue
+ const parsed = parseCitationsFromToolOutput(part.output)
+ if (parsed?.kind === 'rag') rags.push(parsed)
+ else if (parsed?.kind === 'web') webs.push(parsed)
+ }
}
- return out
+ const merged: CitationsPayload | null = rags.length
+ ? { ...rags[0], citations: rags.flatMap((r) => r.citations) }
+ : null
+ return { ragPayload: merged, webPayloads: webs }
}, [message.parts, message.role])
+ const ragCitations = useMemo<RagCitation[]>(
+ () => (ragPayload?.kind === 'rag' ? ragPayload.citations : []),
+ [ragPayload]
+ )
+
const serviceHub = useServiceHub()
const grounding = useGroundingStore((s) => s.byMessageId[message.id])
const ensureGrounding = useGroundingStore((s) => s.ensure)
@@ -664,6 +679,15 @@ export const MessageItem = memo(
{/* Render message parts */}
{renderedParts}
+ {/* Sources stay in the message body so they remain visible after the
+ working trace auto-collapses, and so inline citation anchors resolve. */}
+ {ragPayload && (
+ <Citations payload={ragPayload} anchorPrefix={`cite-${message.id}`} />
+ )}
+ {webPayloads.map((payload, i) => (
+ <Citations key={`web-cite-${message.id}-${i}`} payload={payload} />
+ ))}
+
{isLastMessage &&
message.role === 'assistant' &&
!awaitingApproval &&