Coverage for app / services / system_resources / web_sources.py: 100%
38 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
1"""The web sources CHAINR Bot's searches put first — chosen by CHAIN-R, by
2published criteria, and shown on the bot's page.
4**Prefer, never restrict.** The bot ranks these publishers first, runs one
5more news search scoped to them when the first page had none, and is told to
6treat them as a starting point; every other source stays usable. A
7workspace's own Tools rules come first: a list they put first or allow
8replaces this one, what they block comes off it, and no source left means
9nothing is put first (svc-ai ``app/tools/web/platform_sources.py``).
11**Who is on it.** Original newsgathering or official publication; a
12published impartiality obligation; mostly free to read; in English; no
13partisan editorial line. That keeps to wire services, public-service
14broadcasters and an official statistics office, and leaves newspapers out on
15purpose — however good, each has an editorial line, and a list a platform
16puts first for every user should not have one. Each kind's criteria are
17:data:`CRITERIA`, shown on the bot's page and in its guide.
19How it travels: ``CHAINR_BOT.config["web_sources"]`` (a change is a CHAIN-R
20release of the bot, in its edit history), sent by ``agent_spec`` on the
21built-in agent's requests only (``platform_sources``). Built-in Tools rule
22#13 is the same list (:func:`domains`), for a workspace that wants it for
23its own agents.
24"""
26from __future__ import annotations
28from dataclasses import dataclass
29from enum import StrEnum
30from uuid import UUID
32from app.util import vocab, web_domains
35#: Built-in Tools rule #13 (``rules.TRUSTED_NEWS``), the same list as a
36#: template: the bot's page opens the builder on it, pinned to the bot, for a
37#: workspace that wants other sources. Here so the bot's config can name it
38#: without importing the rules (which import this module).
39TEMPLATE_RULE_ID = UUID("c4a1b0e7-0000-4000-8000-000000000413")
42class SourceKind(StrEnum):
43 """What a platform source is — and so which criteria put it on the list."""
45 wire = "wire"
46 public_broadcaster = "public_broadcaster"
47 official_statistics = "official_statistics"
50#: Each kind's label, as the bot's page and its guide name it.
51LABELS: dict[SourceKind, str] = {
52 SourceKind.wire: "Wire services",
53 SourceKind.public_broadcaster: "Public-service broadcasters",
54 SourceKind.official_statistics: "Official statistics",
55}
56vocab.check(LABELS, SourceKind)
58#: Why a source of each kind is on the list.
59CRITERIA: dict[SourceKind, str] = {
60 SourceKind.wire: (
61 "Original newsgathering sold to other newsrooms, under published "
62 "standards of accuracy and impartiality; no editorial line of its own."
63 ),
64 SourceKind.public_broadcaster: (
65 "Publicly funded newsgathering with a published duty of impartiality, "
66 "mostly free to read, in English."
67 ),
68 SourceKind.official_statistics: (
69 "Official figures, published by a statistics office independent of "
70 "government under a code of practice."
71 ),
72}
73vocab.check(CRITERIA, SourceKind)
76@dataclass(frozen=True)
77class PlatformSource:
78 domain: str
79 publisher: str
80 kind: SourceKind
82 def config(self) -> dict[str, str]:
83 return {
84 "domain": self.domain,
85 "publisher": self.publisher,
86 "kind": self.kind.value,
87 }
90#: In the order the bot's page lists them, by kind.
91PLATFORM_SOURCES: tuple[PlatformSource, ...] = (
92 PlatformSource("reuters.com", "Reuters", SourceKind.wire),
93 PlatformSource("apnews.com", "Associated Press", SourceKind.wire),
94 PlatformSource("bbc.co.uk", "BBC", SourceKind.public_broadcaster),
95 PlatformSource("bbc.com", "BBC", SourceKind.public_broadcaster),
96 PlatformSource("dw.com", "Deutsche Welle", SourceKind.public_broadcaster),
97 PlatformSource("cbc.ca", "CBC", SourceKind.public_broadcaster),
98 PlatformSource("abc.net.au", "ABC (Australia)", SourceKind.public_broadcaster),
99 PlatformSource("rte.ie", "RTÉ", SourceKind.public_broadcaster),
100 PlatformSource("france24.com", "France 24", SourceKind.public_broadcaster),
101 PlatformSource(
102 "ons.gov.uk", "Office for National Statistics", SourceKind.official_statistics
103 ),
104)
107def domains() -> list[str]:
108 """The list's domains, as a Tools rule stores them."""
109 return list(web_domains.normalise_domains(s.domain for s in PLATFORM_SOURCES))
112def bot_config() -> dict[str, object]:
113 """``CHAINR_BOT.config["web_sources"]``: what the bot's page shows and
114 ``agent_spec`` sends — every source with its publisher and kind, the
115 criteria per kind, and the template rule its page offers."""
116 return {
117 "preferred": [s.config() for s in PLATFORM_SOURCES],
118 "rule_template_id": str(TEMPLATE_RULE_ID),
119 "kinds": [
120 {"kind": kind.value, "label": LABELS[kind], "criteria": CRITERIA[kind]}
121 for kind in SourceKind
122 ],
123 }
126def preferred_of(config: object) -> list[str]:
127 """The domains a stored agent config's ``web_sources`` names — none when
128 it names none. Read only for the built-in agent (``agent_spec.load``)."""
129 if not isinstance(config, dict):
130 return []
131 web = config.get("web_sources")
132 if not isinstance(web, dict):
133 return []
134 entries = web.get("preferred")
135 if not isinstance(entries, list):
136 return []
137 named = (e.get("domain") if isinstance(e, dict) else e for e in entries)
138 return list(web_domains.normalise_domains(d for d in named if isinstance(d, str)))
141__all__ = [
142 "CRITERIA",
143 "LABELS",
144 "PLATFORM_SOURCES",
145 "PlatformSource",
146 "SourceKind",
147 "TEMPLATE_RULE_ID",
148 "bot_config",
149 "domains",
150 "preferred_of",
151]