All files / src/components/chat/Citations citationChain.ts

93.33% Statements 28/30
91.66% Branches 33/36
100% Functions 5/5
96.29% Lines 26/27

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                    17x 88x   17x 2x 2x                             45x 45x 11x   45x 4x 3x   45x         45x         45x           43x 43x 43x 43x 6x 3x 3x 3x   43x               46x 46x                   46x    
import { Citation, CitationLink } from "@/objects/Model";
import { parseServerDateOrNull } from "@/util/time";
 
/** One step of a citation's provenance: knowledge base → page → … */
export interface ChainStep {
  label: string;
  url?: string;
  kind: "knowledge_base" | "via" | "source";
}
 
const isHttp = (value?: string | null): value is string =>
  !!value && /^https?:\/\//i.test(value);
 
const hostOf = (url: string): string => {
  try {
    return new URL(url).hostname.replace(/^www\./, "");
  } catch {
    return url;
  }
};
 
/**
 * Where a citation came from, outermost first — e.g. for a page read because
 * a crawled listing linked to it:
 *
 *   News (knowledge base) › Example News › github.com/org/whiteboard
 *
 * A plain web or document citation is a one-step chain.
 */
export function citationChain(citation: Citation): ChainStep[] {
  const steps: ChainStep[] = [];
  if (citation.kb_name) {
    steps.push({ label: citation.kb_name, kind: "knowledge_base" });
  }
  for (const via of citation.via ?? []) {
    if (!isHttp(via.url)) continue;
    steps.push({ label: via.title || hostOf(via.url), url: via.url, kind: "via" });
  }
  const url = isHttp(citation.url)
    ? citation.url
    : isHttp(citation.source)
      ? citation.source
      : undefined;
  steps.push({
    label: citation.title || (url ? hostOf(url) : citation.source),
    url,
    kind: "source",
  });
  return steps;
}
 
/** The links inside a cited passage worth showing: real URLs, not the page
 *  itself, de-duplicated. */
export function citedLinks(citation: Citation, limit = 5): CitationLink[] {
  const own = citation.url ?? citation.source;
  const seen = new Set<string>();
  const out: CitationLink[] = [];
  for (const link of citation.links ?? []) {
    if (!isHttp(link.url) || link.url === own || seen.has(link.url)) continue;
    seen.add(link.url);
    out.push(link);
    Iif (out.length >= limit) break;
  }
  return out;
}
 
export { hostOf };
 
/** "Published 30 Sep 2026 · by Jane Reporter", or null when the citation
 *  says neither. */
export function citationByline(citation: Citation): string | null {
  const date = parseServerDateOrNull(citation.published);
  const parts = [
    date
      ? `Published ${date.toLocaleDateString(undefined, {
          day: "numeric",
          month: "short",
          year: "numeric",
        })}`
      : null,
    citation.author ? `by ${citation.author}` : null,
  ].filter(Boolean);
  return parts.length ? parts.join(" · ") : null;
}