Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | 16x 1x 16x 45x 9x 9x 16x 5x 5x 5x 1x 4x 2x 2x 2x 1x 1x 1x 16x 16x 42x 41x 18x 16x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 26x 26x 42x 8x 8x 8x 8x 8x 42x 38x 8x 8x 8x 8x 8x 8x 42x 2x 5x 1x 1x 33x 36x 4x 36x 16x 32x 32x 33x | import { Citation, CitationKind, isCitationKind } from "@/objects/Model";
import { useApiHandler } from "@/services/useApiHandler";
import { openAttachment } from "@/util/chatAttachments";
import {
ChevronRight,
Database,
ExternalLink,
FileText,
Link2,
Loader2,
} from "lucide-react";
import React, { useEffect, useRef, useState } from "react";
import { createPortal } from "react-dom";
import SiteIcon from "./SiteIcon";
import {
citationByline,
citationChain,
citedLinks,
hostOf,
} from "./citationChain";
/** What a citation with no knowledge base and no URL is, by its kind โ so a
* document or a tool's result is not subtitled "Knowledge Base". */
const KIND_SUBTITLES: Partial<Record<CitationKind, string>> = {
graph: "Knowledge graph",
document: "Document",
other: "Tool result",
};
function kindSubtitle(kind: Citation["kind"]): string {
return (kind && KIND_SUBTITLES[kind]) || "Knowledge Base";
}
/** The tool a document the person attached is cited under (svc-ai
* `attachments.TOOL`). */
export const ATTACHMENT_TOOL = "attachment";
/** The library id an attached document's citation can open it by โ none for
* one pasted into a message before attachments were kept as documents
* (svc-ai's `legacy:` ids), which has no file to open. */
export function attachedDocumentId(citation: Citation): string | null {
if (citation.tool !== ATTACHMENT_TOOL) return null;
const id = citation.document_id;
return id && !id.startsWith("legacy:") ? id : null;
}
/** Open the person's attached document as it was uploaded
* (`openAttachment`), and say when it can no longer be. */
const OpenAttachment: React.FC<{
id: string;
name: string;
onOpened: () => void;
}> = ({ id, name, onOpened }) => {
const { getApiHandler } = useApiHandler();
const [state, setState] = useState<"idle" | "opening" | "gone">("idle");
if (state === "gone") {
return (
<span className="text-xs text-gray-400 dark:text-gray-500 italic">
No longer available
</span>
);
}
return (
<button
type="button"
disabled={state === "opening"}
onClick={async () => {
setState("opening");
const opened = await openAttachment(getApiHandler().file_router, {
id,
name,
});
if (opened) {
setState("idle");
onOpened();
} else {
setState("gone");
}
}}
className="flex items-center gap-1 text-xs font-medium text-indigo-600 dark:text-indigo-400 hover:text-indigo-700 dark:hover:text-indigo-300 transition-colors disabled:opacity-60"
>
{state === "opening" ? (
<Loader2 className="w-3 h-3 animate-spin" />
) : (
<ExternalLink className="w-3 h-3" />
)}
Open
</button>
);
};
/** A chip's label: what the citation is a source of (svc-ai stamps a `kind`
* on every citation). */
const KIND_LABELS: Record<CitationKind, string> = {
web: "Web",
knowledge: "KB",
graph: "Graph",
document: "Doc",
other: "Tool",
};
/** Citations from before kinds, labelled by the tool that made them. */
const TOOL_LABELS: Record<string, string> = {
web_search: "Web",
url_fetch: "Page",
search_knowledge: "KB",
query_knowledge_base: "KB",
retrieval: "KB",
read_document: "Doc",
explore_graph: "Graph",
retrieval_graph: "Graph",
};
function citationLabel(citation: Citation): string {
// A page read whole is still a web source, but "Page" says which kind.
if (citation.tool === "url_fetch") return TOOL_LABELS.url_fetch;
if (isCitationKind(citation.kind)) return KIND_LABELS[citation.kind];
return TOOL_LABELS[citation.tool] ?? citation.tool;
}
interface PopoverPos {
x: number;
y: number;
openUp: boolean;
}
const CitationCard: React.FC<{ citation: Citation }> = ({ citation }) => {
const [pos, setPos] = useState<PopoverPos | null>(null);
const btnRef = useRef<HTMLButtonElement>(null);
// A knowledge base's web page cites the page (``url``); ``source`` is the
// same URL, or a filename for an uploaded document.
const href = citation.url || citation.source;
const isUrl = href.startsWith("http");
const label = citationLabel(citation);
const chain = citationChain(citation);
const links = citedLinks(citation);
const byline = citationByline(citation);
const attached = citation.tool === ATTACHMENT_TOOL;
const openable = attachedDocumentId(citation);
let hostname = "";
if (isUrl) {
try {
hostname = new URL(href).hostname;
} catch {
/* ignore */
}
}
const open = (e: React.MouseEvent) => {
e.stopPropagation();
Iif (pos) {
setPos(null);
return;
}
const rect = (e.currentTarget as HTMLElement).getBoundingClientRect();
const spaceBelow = window.innerHeight - rect.bottom;
setPos({
x: Math.min(rect.left, window.innerWidth - 296),
y: rect.top,
openUp: spaceBelow < 180,
});
};
useEffect(() => {
if (!pos) return;
const close = () => setPos(null);
window.addEventListener("click", close);
window.addEventListener("scroll", close, { capture: true });
return () => {
window.removeEventListener("click", close);
window.removeEventListener("scroll", close, { capture: true });
};
}, [pos]);
return (
<>
<button
ref={btnRef}
onClick={open}
className={`inline-flex items-center gap-1.5 px-2 py-1 rounded-full text-xs font-medium border transition-all
${
pos
? "bg-indigo-100 dark:bg-indigo-900/50 border-indigo-200 dark:border-indigo-700 text-indigo-700 dark:text-indigo-300"
: "bg-gray-100 dark:bg-gray-750 border-gray-200 dark:border-gray-600 text-gray-600 dark:text-gray-300 hover:bg-gray-200 dark:hover:bg-gray-700 hover:border-gray-300 dark:hover:border-gray-500"
}`}
>
{citation.marker != null && (
<span className="shrink-0 min-w-[1.1rem] h-[1.1rem] inline-flex items-center justify-center rounded-full text-[10px] font-semibold bg-indigo-600 text-white">
{citation.marker}
</span>
)}
<span className="shrink-0 px-1 py-0.5 rounded text-[10px] font-semibold bg-indigo-100 dark:bg-indigo-900/50 text-indigo-600 dark:text-indigo-300">
{label}
</span>
<span className="max-w-[180px] truncate">
{citation.title || (isUrl ? hostname : citation.source)}
</span>
</button>
{pos &&
createPortal(
<div
className="fixed z-[9999]"
style={{
left: pos.x,
top: pos.openUp ? pos.y - 8 : pos.y + 28,
transform: pos.openUp ? "translateY(-100%)" : "translateY(0)",
width: 288,
}}
onClick={(e) => e.stopPropagation()}
>
<div className="bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-xl shadow-xl shadow-black/10 dark:shadow-black/40 overflow-hidden">
{/* Header */}
<div className="px-3.5 py-3 flex items-start gap-2.5">
{isUrl && hostname ? (
<SiteIcon hostname={hostname} className="w-5 h-5 mt-0.5" />
) : attached ? (
<div className="w-5 h-5 rounded bg-indigo-100 dark:bg-indigo-900/50 flex items-center justify-center shrink-0 mt-0.5">
<FileText className="w-3 h-3 text-indigo-500" />
</div>
) : (
<div className="w-5 h-5 rounded bg-indigo-100 dark:bg-indigo-900/50 flex items-center justify-center shrink-0 mt-0.5">
<Database className="w-3 h-3 text-indigo-500" />
</div>
)}
<div className="min-w-0 flex-1">
<p className="text-sm font-semibold text-gray-800 dark:text-gray-100 leading-tight line-clamp-2">
{citation.title || (isUrl ? hostname : citation.source)}
</p>
<p className="text-xs text-gray-500 dark:text-gray-400 mt-0.5 truncate">
{attached
? "Attached document"
: citation.kb_name
? `${citation.kb_name} ยท ${isUrl ? hostname : "knowledge base"}`
: isUrl
? hostname
: (citation.collection ??
kindSubtitle(citation.kind))}
</p>
</div>
</div>
{/* When it was published and by whom โ what dates a claim. */}
{byline && (
<p className="px-3.5 -mt-1.5 pb-2.5 text-[11px] text-gray-500 dark:text-gray-400">
{byline}
</p>
)}
{/* Deep citation: how this source was reached, outermost first. */}
{chain.length > 1 && (
<ol
aria-label="Where this source came from"
className="px-3.5 pb-2.5 flex flex-wrap items-center gap-x-1 gap-y-0.5 text-[11px] text-gray-500 dark:text-gray-400"
>
{chain.map((step, i) => (
<li
key={`${step.kind}-${i}`}
className="flex items-center gap-1 min-w-0"
>
{i > 0 && (
<ChevronRight className="w-3 h-3 shrink-0 text-gray-300 dark:text-gray-600" />
)}
{step.url && step.kind !== "source" ? (
<a
href={step.url}
target="_blank"
rel="noopener noreferrer"
className="truncate max-w-[140px] hover:text-indigo-600 dark:hover:text-indigo-400 hover:underline"
>
{step.label}
</a>
) : (
<span
className={`truncate max-w-[140px] ${
step.kind === "source"
? "font-medium text-gray-700 dark:text-gray-200"
: ""
}`}
>
{step.label}
</span>
)}
</li>
))}
</ol>
)}
{/* Deep citation: the originals this passage points at. */}
{links.length > 0 && (
<div className="px-3.5 pb-2.5">
<p className="text-[10px] font-semibold uppercase tracking-wide text-gray-400 dark:text-gray-500 mb-1">
Links in this passage
</p>
<ul className="space-y-0.5">
{links.map((link) => (
<li key={link.url} className="min-w-0">
<a
href={link.url}
target="_blank"
rel="noopener noreferrer"
className="flex items-center gap-1 text-xs text-indigo-600 dark:text-indigo-400 hover:underline min-w-0"
>
<Link2 className="w-3 h-3 shrink-0" />
<span className="truncate">
{link.text || hostOf(link.url)}
</span>
<span className="shrink-0 text-gray-400 dark:text-gray-500">
{hostOf(link.url)}
</span>
</a>
</li>
))}
</ul>
</div>
)}
{/* The credit the source's licence asks for. */}
{citation.attribution && (
<p className="px-3.5 pb-2.5 text-[11px] leading-snug text-gray-500 dark:text-gray-400">
{citation.attribution}
{citation.licence_url && (
<>
{" "}
<a
href={citation.licence_url}
target="_blank"
rel="noopener noreferrer"
className="text-indigo-600 dark:text-indigo-400 hover:underline"
>
Licence
</a>
</>
)}
</p>
)}
{/* Footer */}
<div className="px-3.5 py-2 border-t border-gray-100 dark:border-gray-700 flex items-center justify-between">
<span className="text-xs text-gray-400 dark:text-gray-500">
Source #{citation.marker ?? citation.index}
</span>
{isUrl ? (
<a
href={href}
target="_blank"
rel="noopener noreferrer"
onClick={() => setPos(null)}
className="flex items-center gap-1 text-xs font-medium text-indigo-600 dark:text-indigo-400 hover:text-indigo-700 dark:hover:text-indigo-300 transition-colors"
>
<ExternalLink className="w-3 h-3" />
Visit source
</a>
) : openable ? (
<OpenAttachment
id={openable}
name={citation.title || citation.source}
onOpened={() => setPos(null)}
/>
) : (
<span className="text-xs text-gray-400 dark:text-gray-500 italic">
Internal
</span>
)}
</div>
</div>
</div>,
document.body,
)}
</>
);
};
/** Cards in the order the answer cites them: by the number its Sources
* block gives each (``marker``), then the rest as they came. */
export function citationOrder(citations: Citation[]): Citation[] {
return citations
.map((c, at) => ({ c, at }))
.sort(
(a, b) =>
(a.c.marker ?? Number.MAX_SAFE_INTEGER) -
(b.c.marker ?? Number.MAX_SAFE_INTEGER) || a.at - b.at,
)
.map(({ c }) => c);
}
const CitationRow: React.FC<{ citations: Citation[] }> = ({ citations }) => {
Iif (citations.length === 0) return null;
return (
<div className="flex flex-wrap gap-1.5">
{citationOrder(citations).map((c) => (
<CitationCard key={c.citation_id ?? c.index} citation={c} />
))}
</div>
);
};
export default CitationRow;
export { CitationCard };
|