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): align multi-card citation anchors with global superscript index

Akarshan Biswasde36aaa2de9a
diff --git a/web-app/src/components/Citations.tsx b/web-app/src/components/Citations.tsx
index db0f78351..71478f3c5 100644
--- a/web-app/src/components/Citations.tsx
+++ b/web-app/src/components/Citations.tsx
@@ -156,9 +156,13 @@ export const Citations = memo(
({
payload,
anchorPrefix,
+ indexOffset = 0,
}: {
payload: CitationsPayload
anchorPrefix?: string
+ // Number/anchor cards from this base so a turn's multiple retrieve cards
+ // share one continuous numbering that matches the inline superscripts.
+ indexOffset?: number
}) => {
useEnsureAttachmentNames(
payload.kind === 'rag' ? payload.scope : undefined,
@@ -171,19 +175,22 @@ export const Citations = memo(
const items = useMemo(() => {
if (payload.kind === 'rag') {
- return payload.citations.map((c, i) => (
- <RagCitationItem
- key={c.id}
- c={c}
- index={i}
- anchorId={anchorPrefix ? `${anchorPrefix}-${i + 1}` : undefined}
- />
- ))
+ return payload.citations.map((c, i) => {
+ const n = i + indexOffset
+ return (
+ <RagCitationItem
+ key={c.id}
+ c={c}
+ index={n}
+ anchorId={anchorPrefix ? `${anchorPrefix}-${n + 1}` : undefined}
+ />
+ )
+ })
}
return payload.citations.map((c, i) => (
<WebCitationItem key={`${c.url}-${i}`} c={c} />
))
- }, [payload, anchorPrefix])
+ }, [payload, anchorPrefix, indexOffset])
if (!items.length) return null
diff --git a/web-app/src/components/ai-elements/tool.tsx b/web-app/src/components/ai-elements/tool.tsx
index e221a6426..2aab7e597 100644
--- a/web-app/src/components/ai-elements/tool.tsx
+++ b/web-app/src/components/ai-elements/tool.tsx
@@ -316,10 +316,20 @@ export type ToolOutputProps = ComponentProps<'div'> & {
output: ToolUIPart['output']
errorText: ToolUIPart['errorText']
resolver: (input: string) => Promise<string>
+ // Running count of citations from earlier tool calls in this turn, so each
+ // card's numbering/anchors continue the global sequence the markers use.
+ citationOffset?: number
}
export const ToolOutput = memo(
- ({ className, output, errorText, resolver, ...props }: ToolOutputProps) => {
+ ({
+ className,
+ output,
+ errorText,
+ resolver,
+ citationOffset = 0,
+ ...props
+ }: ToolOutputProps) => {
const { messageId } = useTool()
const citationPayload = useMemo(
() => (output ? parseCitationsFromToolOutput(output) : null),
@@ -336,6 +346,7 @@ export const ToolOutput = memo(
<Citations
payload={citationPayload}
anchorPrefix={messageId ? `cite-${messageId}` : undefined}
+ indexOffset={citationOffset}
/>
)
}
@@ -455,7 +466,7 @@ export const ToolOutput = memo(
}
return <div>{output as ReactNode}</div>
- }, [output, errorText, resolver, citationPayload, messageId])
+ }, [output, errorText, resolver, citationPayload, messageId, citationOffset])
if (!(output || errorText)) {
return null
diff --git a/web-app/src/containers/MessageItem.tsx b/web-app/src/containers/MessageItem.tsx
index 675e98649..54dfca453 100644
--- a/web-app/src/containers/MessageItem.tsx
+++ b/web-app/src/containers/MessageItem.tsx
@@ -163,16 +163,26 @@ export const MessageItem = memo(
status === CHAT_STATUS.SUBMITTED)) ||
hasPendingToolCall
- const ragCitations = useMemo<RagCitation[]>(() => {
- if (message.role !== 'assistant') return []
+ // Aggregate RAG citations in part order and record each rag tool part's
+ // base offset, so its card numbers/anchors continue the same global
+ // sequence the inline superscript markers use.
+ const { ragCitations, citationOffsets } = useMemo(() => {
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)
+ const offsets = new Map<number, number>()
+ if (message.role === 'assistant') {
+ const parts = message.parts as any[]
+ for (let i = 0; i < parts.length; i++) {
+ const part = parts[i]
+ if (!part.type?.startsWith('tool-')) continue
+ if (part.state !== 'output-available') continue
+ const parsed = parseCitationsFromToolOutput(part.output)
+ if (parsed?.kind === 'rag') {
+ offsets.set(i, out.length)
+ out.push(...parsed.citations)
+ }
+ }
}
- return out
+ return { ragCitations: out, citationOffsets: offsets }
}, [message.parts, message.role])
const serviceHub = useServiceHub()
@@ -435,6 +445,7 @@ export const MessageItem = memo(
output={part.output}
resolver={(input) => Promise.resolve(input)}
errorText={undefined}
+ citationOffset={citationOffsets.get(partIndex) ?? 0}
/>
)}
{part.state === 'output-error' && (