All files / src/util platformWebSources.ts

100% Statements 26/26
100% Branches 19/19
100% Functions 15/15
100% Lines 21/21

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                                                      3x 101x   213x             16x 16x 13x   36x 36x 13x 12x   31x 31x 16x                       29x 11x 29x 31x   11x 30x   88x   30x    
/**
 * The web sources CHAINR Bot's searches put first, as its page shows them —
 * read from the agent's own config (`config.web_sources`, svc-core
 * `system_resources/web_sources.py`), which a release changes and its edit
 * history records. Kind labels and criteria come with it, so this page names
 * a kind exactly as the platform does and holds no copy of the vocabulary.
 */
 
export interface PlatformSource {
  domain: string;
  publisher: string;
  kind: string;
}
 
export interface PlatformSourceKind {
  kind: string;
  label: string;
  criteria: string;
}
 
export interface PlatformWebSources {
  preferred: PlatformSource[];
  kinds: PlatformSourceKind[];
  /** Built-in Tools rule #13: the same list, as a template to start from. */
  rule_template_id: string | null;
}
 
const isRecord = (value: unknown): value is Record<string, unknown> =>
  typeof value === "object" && value !== null && !Array.isArray(value);
 
const text = (value: unknown): string => (typeof value === "string" ? value : "");
 
/** `config.web_sources`, or null when the agent has none — every agent but
 *  the built-in one. Entries that are not what they should be are left out. */
export function platformWebSources(
  config: Record<string, unknown> | null | undefined,
): PlatformWebSources | null {
  const web = isRecord(config) ? config.web_sources : null;
  if (!isRecord(web) || !Array.isArray(web.preferred)) return null;
  const preferred = web.preferred
    .filter(isRecord)
    .map((e) => ({ domain: text(e.domain), publisher: text(e.publisher), kind: text(e.kind) }))
    .filter((e) => e.domain);
  if (preferred.length === 0) return null;
  const kinds = (Array.isArray(web.kinds) ? web.kinds : [])
    .filter(isRecord)
    .map((k) => ({ kind: text(k.kind), label: text(k.label), criteria: text(k.criteria) }))
    .filter((k) => k.kind);
  return {
    preferred,
    kinds,
    rule_template_id: text(web.rule_template_id) || null,
  };
}
 
/** The sources grouped by kind, in the kinds' order; a kind with no source is
 *  left out, and a source of a kind not described goes under its own key. */
export function groupedByKind(
  web: PlatformWebSources,
): { kind: PlatformSourceKind; sources: PlatformSource[] }[] {
  const known = new Map(web.kinds.map((k) => [k.kind, k]));
  const order = [
    ...web.kinds.map((k) => k.kind),
    ...web.preferred.map((s) => s.kind).filter((k) => !known.has(k)),
  ];
  return [...new Set(order)]
    .map((kind) => ({
      kind: known.get(kind) ?? { kind, label: kind, criteria: "" },
      sources: web.preferred.filter((s) => s.kind === kind),
    }))
    .filter((group) => group.sources.length > 0);
}