feed-protocol 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Aniku Gul IEng, MIET, IMechE, VCAT II, CAA
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,200 @@
1
+ Metadata-Version: 2.4
2
+ Name: feed-protocol
3
+ Version: 0.2.0
4
+ Summary: FEED — Format for Enforced Evidence-based Digestion. A self-bootstrapping document protocol that forces downstream LLMs to ground answers in cited evidence.
5
+ Author: Aniku Gul IEng, MIET, IMechE, VCAT II, CAA
6
+ License: MIT License
7
+
8
+ Copyright (c) 2026 Aniku Gul IEng, MIET, IMechE, VCAT II, CAA
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Homepage, https://github.com/mrjesters/feed-protocol
29
+ Project-URL: Spec, https://github.com/mrjesters/feed-protocol/blob/main/spec/feed-spec-v0.2.md
30
+ Keywords: llm,rag,grounding,documents,protocol,feed,ai
31
+ Classifier: Development Status :: 4 - Beta
32
+ Classifier: Intended Audience :: Developers
33
+ Classifier: License :: OSI Approved :: MIT License
34
+ Classifier: Programming Language :: Python :: 3
35
+ Classifier: Topic :: Text Processing :: Markup
36
+ Requires-Python: >=3.10
37
+ Description-Content-Type: text/markdown
38
+ License-File: LICENSE
39
+ Provides-Extra: tagger
40
+ Requires-Dist: anthropic>=0.40; extra == "tagger"
41
+ Provides-Extra: dev
42
+ Requires-Dist: pytest>=7; extra == "dev"
43
+ Requires-Dist: anthropic>=0.40; extra == "dev"
44
+ Dynamic: license-file
45
+
46
+ # FEED — Format for Enforced Evidence-based Digestion
47
+
48
+ > Make any document tell the AI reading it: **here's what matters, here's the
49
+ > evidence, cite it or say you can't.**
50
+
51
+ FEED is a plain-text convention you embed in a document so that any LLM — Copilot,
52
+ ChatGPT, Claude, Gemini, a local model — grounds its answers in your evidence and
53
+ cites it by ID. No install, no plugin, no provider support, no prior knowledge of
54
+ FEED required on either side. It works on every model **today** because the
55
+ document teaches the rules inline.
56
+
57
+ It also has **teeth**: because evidence carries stable IDs and answers must cite
58
+ them, you can *mechanically verify* that an answer is actually grounded.
59
+
60
+ ```
61
+ ┌─ Header ──────────────────────────────────────────────┐
62
+ │ <!-- FEED:DOC version="0.2" grounding="strict" --> │
63
+ │ > AI INGESTION NOTICE … ground answers, cite [E###] … │ ← teaches any LLM the rules
64
+ ├─ Tier 0: Claims & Decisions (front-loaded) ───────────┤ ← small-context-safe
65
+ ├─ Tier 1: Findings (narrative, references [E001]) ─────┤
66
+ ├─ Tier 2: Evidence (atomic key/value facts, IDs) ──────┤ ← the source of truth
67
+ └────────────────────────────────────────────────────────┘
68
+ ```
69
+
70
+ ## Why it exists
71
+
72
+ You generate AI reports and send them to people who paste them into their own AI.
73
+ Every hop loses fidelity — the reader's AI skims headings and riffs. FEED fixes
74
+ the **author** side of that loop: the document constrains how the downstream AI
75
+ reads and answers. Nothing else does this at the document level (`llms.txt` is
76
+ website-level and has no grounding contract; RAG/citation systems are all
77
+ retrieval-side, controlled by the AI, not the author).
78
+
79
+ ## FEED is AI-to-AI (the library never needs its own API key)
80
+
81
+ The AI **already in your loop** — the one that wrote the report, your assistant, a
82
+ pipeline step — is what produces FEED. The library just renders, validates, and
83
+ verifies; it never calls an LLM of its own. Authoring is self-bootstrapping, the
84
+ same way ingestion is:
85
+
86
+ ```
87
+ Reading side: the document carries a notice that teaches any AI to ground & cite
88
+ Authoring side: an authoring prompt + schema teach any AI to emit FEED, then
89
+ feed.build() renders it deterministically — no key, no network
90
+ ```
91
+
92
+ ### Primary flow: the AI emits FEED, you render it
93
+
94
+ ```python
95
+ from feed import AUTHORING_PROMPT, FEED_JSON_SCHEMA, build
96
+
97
+ # 1. Hand AUTHORING_PROMPT + FEED_JSON_SCHEMA to whatever AI is already in your loop.
98
+ # It returns structured JSON (no FEED tooling needed on the AI's side).
99
+ # 2. Render that JSON into a validated FEED document — pure Python, no API key:
100
+ doc = build(ai_json, grounding="strict", author="N. Gul")
101
+ doc.write("report.md")
102
+ ```
103
+
104
+ Or entirely from the shell:
105
+
106
+ ```bash
107
+ feed prompt > authoring-kit.txt # the prompt + schema to give any AI
108
+ feed build ai_output.json -o report.md # render the AI's JSON into FEED (no key)
109
+ ```
110
+
111
+ Manual additions are just edits: open `report.md` and add/adjust evidence and
112
+ claims by hand — it's plain markdown. `feed validate` checks it's still conformant.
113
+
114
+ > `feed tag draft.md` is an **optional** convenience that calls Claude directly,
115
+ > for when you have a plain document and *no* AI already in the loop. It is not the
116
+ > primary path and is the only thing that needs an API key.
117
+
118
+ ## Quick start
119
+
120
+ ### Build one in Python (manual / programmatic)
121
+
122
+ ```python
123
+ from feed import FeedDocument
124
+
125
+ doc = FeedDocument("Q2 Pump Health Assessment", grounding="strict")
126
+ doc.add_evidence("E001", asset="XYZ-003", metric="vibration_rms",
127
+ value="12.4 mm/s",
128
+ threshold="11.2 mm/s (ISO 10816-3 Zone C)", confidence="high")
129
+ doc.add_claim("C1", "XYZ-003 needs intervention", evidence=["E001"],
130
+ decision="Approve bearing replacement work order")
131
+
132
+ doc.write("report.md") # clean markdown, FEED in HTML comments
133
+ doc.write("report.html") # styled, opens in any browser
134
+ ```
135
+
136
+ Your team opens `report.md` as a normal report. They upload it to whatever AI
137
+ they use → it reads the notice, sees the evidence, and answers grounded.
138
+
139
+ ### Verify an answer was grounded (the teeth)
140
+
141
+ ```python
142
+ from feed import FeedDocument, verify
143
+
144
+ doc = FeedDocument.read("report.md")
145
+ report = verify(ai_answer_text, doc)
146
+ print(report.passed) # False if it cited evidence that doesn't exist,
147
+ # or (strict mode) didn't cite anything
148
+ ```
149
+
150
+ ### From the command line
151
+
152
+ ```bash
153
+ feed prompt # authoring kit for any AI (no key)
154
+ feed build ai_output.json -o report.md # render an AI's JSON into FEED (no key)
155
+ feed validate report.md # is it well-formed FEED?
156
+ feed verify --doc report.md --answer answer.txt # is this answer grounded?
157
+ feed render report.md --to html -o report.html # styled HTML
158
+ feed tag draft.md --grounding strict -o report.md # OPTIONAL: auto-tag via Claude (needs key)
159
+ ```
160
+
161
+ ## What's in this repo
162
+
163
+ | Path | What it is |
164
+ |------|------------|
165
+ | `spec/feed-spec-v0.2.md` | The protocol definition (the constitution) |
166
+ | `feed/` | The reference library — build, render, validate, verify, auto-tag |
167
+ | `feed/verify.py` | The citation verifier — FEED's defensible edge, ~40 lines |
168
+ | `feed/cli.py` | The `feed` command-line tool |
169
+ | `examples/` | A complete worked example: a pump condition report (`.md` + `.html`) and the script that builds it |
170
+ | `templates/blank.feed.md` | A hand-authoring starter |
171
+ | `tests/` | Round-trip, validation, and verification tests |
172
+
173
+ ## Install
174
+
175
+ ```bash
176
+ pip install -e . # library + CLI, zero dependencies
177
+ pip install -e ".[tagger]" # adds the auto-tagger (needs anthropic + ANTHROPIC_API_KEY)
178
+ ```
179
+
180
+ The core — authoring kit, build, render, validate, verify — is **pure Python with
181
+ no dependencies and never calls an LLM**. Only the optional `tag` convenience
182
+ calls Claude directly; it defaults to Claude Opus 4.8.
183
+
184
+ ## The three primitives
185
+
186
+ - **Evidence** — atomic, ID'd, key/value facts. Never prose. The source of truth.
187
+ - **Claim** — a short statement grounded in evidence IDs, optionally a decision.
188
+ - **Header** — declares the grounding mode and carries the self-teaching notice.
189
+
190
+ Plus **grounding modes** (`strict` / `standard` / `open`) — the author's dial for
191
+ how strict the reading AI must be. In `strict`, no evidence means "Not supported
192
+ by this document."
193
+
194
+ ## Status
195
+
196
+ v0.2 — spec + reference library + verifier + CLI + auto-tagger + worked example.
197
+ Deliberately out of scope for now: PDF/DOCX embedding, a hosted validator, and any
198
+ provider-native "FEED mode".
199
+
200
+ MIT licensed. Spec and tooling are open — adoption is the point.
@@ -0,0 +1,155 @@
1
+ # FEED — Format for Enforced Evidence-based Digestion
2
+
3
+ > Make any document tell the AI reading it: **here's what matters, here's the
4
+ > evidence, cite it or say you can't.**
5
+
6
+ FEED is a plain-text convention you embed in a document so that any LLM — Copilot,
7
+ ChatGPT, Claude, Gemini, a local model — grounds its answers in your evidence and
8
+ cites it by ID. No install, no plugin, no provider support, no prior knowledge of
9
+ FEED required on either side. It works on every model **today** because the
10
+ document teaches the rules inline.
11
+
12
+ It also has **teeth**: because evidence carries stable IDs and answers must cite
13
+ them, you can *mechanically verify* that an answer is actually grounded.
14
+
15
+ ```
16
+ ┌─ Header ──────────────────────────────────────────────┐
17
+ │ <!-- FEED:DOC version="0.2" grounding="strict" --> │
18
+ │ > AI INGESTION NOTICE … ground answers, cite [E###] … │ ← teaches any LLM the rules
19
+ ├─ Tier 0: Claims & Decisions (front-loaded) ───────────┤ ← small-context-safe
20
+ ├─ Tier 1: Findings (narrative, references [E001]) ─────┤
21
+ ├─ Tier 2: Evidence (atomic key/value facts, IDs) ──────┤ ← the source of truth
22
+ └────────────────────────────────────────────────────────┘
23
+ ```
24
+
25
+ ## Why it exists
26
+
27
+ You generate AI reports and send them to people who paste them into their own AI.
28
+ Every hop loses fidelity — the reader's AI skims headings and riffs. FEED fixes
29
+ the **author** side of that loop: the document constrains how the downstream AI
30
+ reads and answers. Nothing else does this at the document level (`llms.txt` is
31
+ website-level and has no grounding contract; RAG/citation systems are all
32
+ retrieval-side, controlled by the AI, not the author).
33
+
34
+ ## FEED is AI-to-AI (the library never needs its own API key)
35
+
36
+ The AI **already in your loop** — the one that wrote the report, your assistant, a
37
+ pipeline step — is what produces FEED. The library just renders, validates, and
38
+ verifies; it never calls an LLM of its own. Authoring is self-bootstrapping, the
39
+ same way ingestion is:
40
+
41
+ ```
42
+ Reading side: the document carries a notice that teaches any AI to ground & cite
43
+ Authoring side: an authoring prompt + schema teach any AI to emit FEED, then
44
+ feed.build() renders it deterministically — no key, no network
45
+ ```
46
+
47
+ ### Primary flow: the AI emits FEED, you render it
48
+
49
+ ```python
50
+ from feed import AUTHORING_PROMPT, FEED_JSON_SCHEMA, build
51
+
52
+ # 1. Hand AUTHORING_PROMPT + FEED_JSON_SCHEMA to whatever AI is already in your loop.
53
+ # It returns structured JSON (no FEED tooling needed on the AI's side).
54
+ # 2. Render that JSON into a validated FEED document — pure Python, no API key:
55
+ doc = build(ai_json, grounding="strict", author="N. Gul")
56
+ doc.write("report.md")
57
+ ```
58
+
59
+ Or entirely from the shell:
60
+
61
+ ```bash
62
+ feed prompt > authoring-kit.txt # the prompt + schema to give any AI
63
+ feed build ai_output.json -o report.md # render the AI's JSON into FEED (no key)
64
+ ```
65
+
66
+ Manual additions are just edits: open `report.md` and add/adjust evidence and
67
+ claims by hand — it's plain markdown. `feed validate` checks it's still conformant.
68
+
69
+ > `feed tag draft.md` is an **optional** convenience that calls Claude directly,
70
+ > for when you have a plain document and *no* AI already in the loop. It is not the
71
+ > primary path and is the only thing that needs an API key.
72
+
73
+ ## Quick start
74
+
75
+ ### Build one in Python (manual / programmatic)
76
+
77
+ ```python
78
+ from feed import FeedDocument
79
+
80
+ doc = FeedDocument("Q2 Pump Health Assessment", grounding="strict")
81
+ doc.add_evidence("E001", asset="XYZ-003", metric="vibration_rms",
82
+ value="12.4 mm/s",
83
+ threshold="11.2 mm/s (ISO 10816-3 Zone C)", confidence="high")
84
+ doc.add_claim("C1", "XYZ-003 needs intervention", evidence=["E001"],
85
+ decision="Approve bearing replacement work order")
86
+
87
+ doc.write("report.md") # clean markdown, FEED in HTML comments
88
+ doc.write("report.html") # styled, opens in any browser
89
+ ```
90
+
91
+ Your team opens `report.md` as a normal report. They upload it to whatever AI
92
+ they use → it reads the notice, sees the evidence, and answers grounded.
93
+
94
+ ### Verify an answer was grounded (the teeth)
95
+
96
+ ```python
97
+ from feed import FeedDocument, verify
98
+
99
+ doc = FeedDocument.read("report.md")
100
+ report = verify(ai_answer_text, doc)
101
+ print(report.passed) # False if it cited evidence that doesn't exist,
102
+ # or (strict mode) didn't cite anything
103
+ ```
104
+
105
+ ### From the command line
106
+
107
+ ```bash
108
+ feed prompt # authoring kit for any AI (no key)
109
+ feed build ai_output.json -o report.md # render an AI's JSON into FEED (no key)
110
+ feed validate report.md # is it well-formed FEED?
111
+ feed verify --doc report.md --answer answer.txt # is this answer grounded?
112
+ feed render report.md --to html -o report.html # styled HTML
113
+ feed tag draft.md --grounding strict -o report.md # OPTIONAL: auto-tag via Claude (needs key)
114
+ ```
115
+
116
+ ## What's in this repo
117
+
118
+ | Path | What it is |
119
+ |------|------------|
120
+ | `spec/feed-spec-v0.2.md` | The protocol definition (the constitution) |
121
+ | `feed/` | The reference library — build, render, validate, verify, auto-tag |
122
+ | `feed/verify.py` | The citation verifier — FEED's defensible edge, ~40 lines |
123
+ | `feed/cli.py` | The `feed` command-line tool |
124
+ | `examples/` | A complete worked example: a pump condition report (`.md` + `.html`) and the script that builds it |
125
+ | `templates/blank.feed.md` | A hand-authoring starter |
126
+ | `tests/` | Round-trip, validation, and verification tests |
127
+
128
+ ## Install
129
+
130
+ ```bash
131
+ pip install -e . # library + CLI, zero dependencies
132
+ pip install -e ".[tagger]" # adds the auto-tagger (needs anthropic + ANTHROPIC_API_KEY)
133
+ ```
134
+
135
+ The core — authoring kit, build, render, validate, verify — is **pure Python with
136
+ no dependencies and never calls an LLM**. Only the optional `tag` convenience
137
+ calls Claude directly; it defaults to Claude Opus 4.8.
138
+
139
+ ## The three primitives
140
+
141
+ - **Evidence** — atomic, ID'd, key/value facts. Never prose. The source of truth.
142
+ - **Claim** — a short statement grounded in evidence IDs, optionally a decision.
143
+ - **Header** — declares the grounding mode and carries the self-teaching notice.
144
+
145
+ Plus **grounding modes** (`strict` / `standard` / `open`) — the author's dial for
146
+ how strict the reading AI must be. In `strict`, no evidence means "Not supported
147
+ by this document."
148
+
149
+ ## Status
150
+
151
+ v0.2 — spec + reference library + verifier + CLI + auto-tagger + worked example.
152
+ Deliberately out of scope for now: PDF/DOCX embedding, a hosted validator, and any
153
+ provider-native "FEED mode".
154
+
155
+ MIT licensed. Spec and tooling are open — adoption is the point.
@@ -0,0 +1,38 @@
1
+ """FEED — Format for Enforced Evidence-based Digestion.
2
+
3
+ A self-bootstrapping document protocol that makes downstream LLMs ground their
4
+ answers in cited evidence — and lets you mechanically verify they did.
5
+
6
+ from feed import FeedDocument
7
+
8
+ doc = FeedDocument("Q2 Pump Health Assessment", grounding="strict")
9
+ doc.add_evidence("E001", asset="XYZ-003", metric="vibration_rms",
10
+ value="12.4 mm/s", threshold="11.2 mm/s (ISO 10816-3 Zone C)",
11
+ confidence="high")
12
+ doc.add_claim("C1", "XYZ-003 needs intervention", evidence=["E001"],
13
+ decision="Approve bearing replacement work order")
14
+ print(doc.render("md"))
15
+ """
16
+
17
+ from .authoring import AUTHORING_PROMPT, FEED_JSON_SCHEMA, build
18
+ from .constants import GROUNDING_MODES, VERSION
19
+ from .document import Claim, Evidence, FeedDocument
20
+ from .validate import ValidationReport, validate
21
+ from .verify import VerificationReport, verify
22
+
23
+ __version__ = VERSION
24
+
25
+ __all__ = [
26
+ "FeedDocument",
27
+ "Evidence",
28
+ "Claim",
29
+ "build",
30
+ "AUTHORING_PROMPT",
31
+ "FEED_JSON_SCHEMA",
32
+ "validate",
33
+ "ValidationReport",
34
+ "verify",
35
+ "VerificationReport",
36
+ "GROUNDING_MODES",
37
+ "VERSION",
38
+ ]
@@ -0,0 +1,157 @@
1
+ """The authoring side of FEED — self-bootstrapping, no API key required.
2
+
3
+ FEED is an AI-to-AI protocol. The AI that is *already in the loop* (the one that
4
+ wrote the report, or the user's assistant, or a pipeline step) is what produces
5
+ FEED — the library never needs its own LLM credentials.
6
+
7
+ That works because the authoring rules are portable, exactly like the ingestion
8
+ notice is portable on the reading side:
9
+
10
+ 1. `AUTHORING_PROMPT` + `FEED_JSON_SCHEMA` — hand these to *any* AI and it emits
11
+ conformant FEED data. No FEED-specific tooling on the AI's side.
12
+ 2. `build(data)` — a pure-Python, dependency-free renderer that turns that data
13
+ into a validated FEED document. No network, no key.
14
+
15
+ The optional `feed.tagger` module is a convenience wrapper that calls Claude for
16
+ people who don't already have an AI in the loop — it is not the primary path.
17
+ """
18
+
19
+ from __future__ import annotations
20
+
21
+ from .document import FeedDocument
22
+
23
+ # The instruction block to give any AI so it authors FEED natively.
24
+ AUTHORING_PROMPT = """\
25
+ You are producing a FEED document (Format for Enforced Evidence-based Digestion).
26
+ FEED separates a document so a downstream AI can answer questions grounded in cited
27
+ evidence. Return ONLY JSON matching the provided schema. Structure the content as:
28
+
29
+ - evidence: every concrete fact in the source, as an atomic key/value block. Never
30
+ prose. Each gets an id E001, E002, ... in document order. Normalise values: ISO
31
+ dates (YYYY-MM-DD), explicit units, consistent names. Include thresholds and
32
+ baselines as their own fields when present. `type` is one of data | quote | calc |
33
+ observation | reference | image; `confidence` is high | medium | low; `note` is an
34
+ optional one-line free-text aside ("" if none).
35
+ - claims: short narrative statements (ids C1, C2, ...), each grounded in one or more
36
+ evidence ids. If a claim implies an action, put it in `decision` ("" if none).
37
+ - findings: brief narrative paragraphs (1-3 sentences) that reference evidence inline
38
+ as [E001]. Say each fact once and reference it by id rather than repeating it.
39
+ - title and summary: the document title and a one-sentence bottom line.
40
+
41
+ Rules: extract every concrete fact as evidence; never invent facts; be dense (no
42
+ filler, no repetition); keep ids sequential and in document order.
43
+ """
44
+
45
+ # JSON Schema the AI should emit. Compatible with Anthropic structured outputs
46
+ # (additionalProperties:false everywhere) but usable with any model — paste it
47
+ # alongside AUTHORING_PROMPT.
48
+ FEED_JSON_SCHEMA = {
49
+ "type": "object",
50
+ "properties": {
51
+ "title": {"type": "string"},
52
+ "summary": {"type": "string"},
53
+ "evidence": {
54
+ "type": "array",
55
+ "items": {
56
+ "type": "object",
57
+ "properties": {
58
+ "id": {"type": "string"},
59
+ "type": {
60
+ "type": "string",
61
+ "enum": ["data", "quote", "calc", "observation", "reference", "image"],
62
+ },
63
+ "confidence": {"type": "string", "enum": ["high", "medium", "low"]},
64
+ "fields": {
65
+ "type": "array",
66
+ "items": {
67
+ "type": "object",
68
+ "properties": {
69
+ "key": {"type": "string"},
70
+ "value": {"type": "string"},
71
+ },
72
+ "required": ["key", "value"],
73
+ "additionalProperties": False,
74
+ },
75
+ },
76
+ "note": {"type": "string"},
77
+ },
78
+ "required": ["id", "type", "confidence", "fields", "note"],
79
+ "additionalProperties": False,
80
+ },
81
+ },
82
+ "claims": {
83
+ "type": "array",
84
+ "items": {
85
+ "type": "object",
86
+ "properties": {
87
+ "id": {"type": "string"},
88
+ "text": {"type": "string"},
89
+ "evidence": {"type": "array", "items": {"type": "string"}},
90
+ "decision": {"type": "string"},
91
+ },
92
+ "required": ["id", "text", "evidence", "decision"],
93
+ "additionalProperties": False,
94
+ },
95
+ },
96
+ "findings": {"type": "array", "items": {"type": "string"}},
97
+ },
98
+ "required": ["title", "summary", "evidence", "claims", "findings"],
99
+ "additionalProperties": False,
100
+ }
101
+
102
+
103
+ def build(
104
+ data: dict,
105
+ title: str | None = None,
106
+ author: str | None = None,
107
+ grounding: str = "strict",
108
+ created: str | None = None,
109
+ ) -> FeedDocument:
110
+ """Render a FeedDocument from the structured data an AI produced. Pure Python,
111
+ no LLM call. `grounding`, `author`, `created` are author-policy overrides — they
112
+ are not the AI's to decide, so they come from the caller, not the data.
113
+
114
+ Resilient to imperfect AI output: claim references to non-existent evidence are
115
+ dropped, and evidence with no fields is skipped, so a slightly-off model
116
+ response still yields a valid document.
117
+ """
118
+ doc = FeedDocument(
119
+ title=title or data.get("title") or "Untitled",
120
+ author=author or data.get("author"),
121
+ grounding=grounding,
122
+ created=created or data.get("created"),
123
+ summary=data.get("summary") or None,
124
+ )
125
+ for ev in data.get("evidence", []):
126
+ fields = _fields(ev)
127
+ if not fields:
128
+ continue
129
+ doc.add_evidence(
130
+ ev["id"],
131
+ type=ev.get("type", "data"),
132
+ confidence=ev.get("confidence", "medium"),
133
+ note=(ev.get("note") or None),
134
+ **fields,
135
+ )
136
+ valid_ev = {e.id for e in doc.evidence}
137
+ for c in data.get("claims", []):
138
+ evidence = [e for e in c.get("evidence", []) if e in valid_ev]
139
+ doc.add_claim(
140
+ c["id"],
141
+ text=c["text"],
142
+ evidence=evidence,
143
+ decision=(c.get("decision") or None),
144
+ )
145
+ for f in data.get("findings", []):
146
+ if f and f.strip():
147
+ doc.add_finding(f)
148
+ return doc
149
+
150
+
151
+ def _fields(ev: dict) -> dict[str, str]:
152
+ """Accept either the schema's [{key,value},...] form or a plain {key: value}
153
+ mapping, so a hand-authored or differently-shaped AI payload still works."""
154
+ raw = ev.get("fields", [])
155
+ if isinstance(raw, dict):
156
+ return {k: str(v) for k, v in raw.items() if k}
157
+ return {f["key"]: f["value"] for f in raw if isinstance(f, dict) and f.get("key")}