realitydiff 0.4.0__py3-none-any.whl
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.
- realitydiff/__init__.py +8 -0
- realitydiff/__main__.py +4 -0
- realitydiff/api.py +270 -0
- realitydiff/blame.py +85 -0
- realitydiff/claim.py +104 -0
- realitydiff/cli.py +168 -0
- realitydiff/compress.py +94 -0
- realitydiff/config.py +66 -0
- realitydiff/diffing.py +210 -0
- realitydiff/hashing.py +24 -0
- realitydiff/models.py +239 -0
- realitydiff/reasoner.py +440 -0
- realitydiff/schedule.py +217 -0
- realitydiff/service.py +173 -0
- realitydiff/static/app.js +814 -0
- realitydiff/static/favicon.svg +4 -0
- realitydiff/static/index.html +175 -0
- realitydiff/static/styles.css +477 -0
- realitydiff/store.py +296 -0
- realitydiff/watcher.py +42 -0
- realitydiff/xai.py +506 -0
- realitydiff-0.4.0.dist-info/METADATA +115 -0
- realitydiff-0.4.0.dist-info/RECORD +26 -0
- realitydiff-0.4.0.dist-info/WHEEL +4 -0
- realitydiff-0.4.0.dist-info/entry_points.txt +2 -0
- realitydiff-0.4.0.dist-info/licenses/LICENSE +21 -0
realitydiff/xai.py
ADDED
|
@@ -0,0 +1,506 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import re
|
|
6
|
+
import time
|
|
7
|
+
from typing import Any, Protocol
|
|
8
|
+
|
|
9
|
+
import httpx
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
DEFAULT_MODELS = (
|
|
13
|
+
"grok-4.3", # 1M context window; cheapest cached-input path among current Grok 4.x
|
|
14
|
+
"grok-4.5",
|
|
15
|
+
"grok-4.20-0309-non-reasoning",
|
|
16
|
+
"grok-4.6",
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
JSON_FENCE = re.compile(r"```(?:json)?\s*(.*?)```", re.DOTALL)
|
|
20
|
+
TRAILING_COMMA = re.compile(r",(\s*[}\]])")
|
|
21
|
+
COMPACT_AFTER_TOKENS = 6_000
|
|
22
|
+
MAX_TOOL_ROUNDS = 3
|
|
23
|
+
|
|
24
|
+
CLOSE_CLAIM_TOOL: dict[str, Any] = {
|
|
25
|
+
"type": "function",
|
|
26
|
+
"name": "close_claim",
|
|
27
|
+
"description": (
|
|
28
|
+
"Close this living claim because the real-world outcome is now known. "
|
|
29
|
+
"Call only when the claim itself is resolved by primary evidence: official "
|
|
30
|
+
"confirmation, a shipping product, an election or court result, or an equivalent "
|
|
31
|
+
"recorded fact. Do not call for rumors, high confidence, stale due dates, or "
|
|
32
|
+
"ordinary watch updates. After calling, still return the final belief JSON."
|
|
33
|
+
),
|
|
34
|
+
"parameters": {
|
|
35
|
+
"type": "object",
|
|
36
|
+
"properties": {
|
|
37
|
+
"verdict": {
|
|
38
|
+
"type": "string",
|
|
39
|
+
"enum": ["confirmed", "falsified", "settled"],
|
|
40
|
+
"description": (
|
|
41
|
+
"confirmed = the claim happened / is true; falsified = it did not / is false; "
|
|
42
|
+
"settled = the outcome is known but is not a binary yes/no"
|
|
43
|
+
),
|
|
44
|
+
},
|
|
45
|
+
"outcome": {
|
|
46
|
+
"type": "string",
|
|
47
|
+
"description": "One paragraph: what happened, with the strongest URLs.",
|
|
48
|
+
},
|
|
49
|
+
"confidence": {
|
|
50
|
+
"type": "number",
|
|
51
|
+
"description": "Final confidence 0-100 after the outcome.",
|
|
52
|
+
},
|
|
53
|
+
"source_url": {
|
|
54
|
+
"type": "string",
|
|
55
|
+
"description": "Primary https URL proving the outcome.",
|
|
56
|
+
},
|
|
57
|
+
"source_title": {
|
|
58
|
+
"type": "string",
|
|
59
|
+
"description": "Title of the primary source.",
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
"required": ["verdict", "outcome"],
|
|
63
|
+
},
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
SEARCH_TOOLS: list[dict[str, Any]] = [{"type": "web_search"}, {"type": "x_search"}]
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class XAIError(RuntimeError):
|
|
70
|
+
pass
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class LanguageModel(Protocol):
|
|
74
|
+
def complete(
|
|
75
|
+
self,
|
|
76
|
+
prompt: str,
|
|
77
|
+
*,
|
|
78
|
+
search: bool = False,
|
|
79
|
+
system: str | None = None,
|
|
80
|
+
conv_id: str | None = None,
|
|
81
|
+
cache_key: str | None = None,
|
|
82
|
+
previous_response_id: str | None = None,
|
|
83
|
+
compaction: dict[str, Any] | None = None,
|
|
84
|
+
) -> dict[str, Any]: ...
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class XAIClient:
|
|
88
|
+
"""xAI Responses API with live web_search / x_search, prompt cache, then fallback."""
|
|
89
|
+
|
|
90
|
+
def __init__(
|
|
91
|
+
self,
|
|
92
|
+
api_key: str | None = None,
|
|
93
|
+
*,
|
|
94
|
+
base_url: str = "https://api.x.ai/v1",
|
|
95
|
+
models: tuple[str, ...] = DEFAULT_MODELS,
|
|
96
|
+
timeout: float = 180.0,
|
|
97
|
+
) -> None:
|
|
98
|
+
self.api_key = api_key or os.environ.get("XAI_API_KEY", "")
|
|
99
|
+
if not self.api_key:
|
|
100
|
+
raise XAIError("XAI_API_KEY is not set")
|
|
101
|
+
self.base_url = base_url.rstrip("/")
|
|
102
|
+
self.models = models
|
|
103
|
+
self.timeout = timeout
|
|
104
|
+
self.last_model: str | None = None
|
|
105
|
+
|
|
106
|
+
def complete(
|
|
107
|
+
self,
|
|
108
|
+
prompt: str,
|
|
109
|
+
*,
|
|
110
|
+
search: bool = False,
|
|
111
|
+
system: str | None = None,
|
|
112
|
+
conv_id: str | None = None,
|
|
113
|
+
cache_key: str | None = None,
|
|
114
|
+
previous_response_id: str | None = None,
|
|
115
|
+
compaction: dict[str, Any] | None = None,
|
|
116
|
+
) -> dict[str, Any]:
|
|
117
|
+
last_error: Exception | None = None
|
|
118
|
+
sticky = conv_id or cache_key or "realitydiff"
|
|
119
|
+
attempts = _request_plans(previous_response_id=previous_response_id, compaction=compaction)
|
|
120
|
+
for model in self.models:
|
|
121
|
+
for plan in attempts:
|
|
122
|
+
for retry in range(3):
|
|
123
|
+
try:
|
|
124
|
+
payload = _build_payload(
|
|
125
|
+
model=model,
|
|
126
|
+
prompt=prompt,
|
|
127
|
+
system=system,
|
|
128
|
+
search=search,
|
|
129
|
+
cache_key=sticky,
|
|
130
|
+
previous_response_id=plan.get("previous_response_id"),
|
|
131
|
+
compaction=plan.get("compaction"),
|
|
132
|
+
store=plan["store"],
|
|
133
|
+
)
|
|
134
|
+
headers = {
|
|
135
|
+
"Authorization": f"Bearer {self.api_key}",
|
|
136
|
+
"Content-Type": "application/json",
|
|
137
|
+
"x-grok-conv-id": sticky,
|
|
138
|
+
}
|
|
139
|
+
response = httpx.post(
|
|
140
|
+
f"{self.base_url}/responses",
|
|
141
|
+
headers=headers,
|
|
142
|
+
json=payload,
|
|
143
|
+
timeout=self.timeout,
|
|
144
|
+
)
|
|
145
|
+
if response.status_code == 429:
|
|
146
|
+
last_error = XAIError(response.text)
|
|
147
|
+
time.sleep(2**retry)
|
|
148
|
+
continue
|
|
149
|
+
if response.status_code >= 400:
|
|
150
|
+
last_error = XAIError(f"{model} HTTP {response.status_code}: {response.text[:500]}")
|
|
151
|
+
break
|
|
152
|
+
body = response.json()
|
|
153
|
+
if body.get("error"):
|
|
154
|
+
last_error = XAIError(str(body.get("error")))
|
|
155
|
+
break
|
|
156
|
+
if body.get("status") == "failed":
|
|
157
|
+
last_error = XAIError(str(body.get("incomplete_details") or body))
|
|
158
|
+
time.sleep(2**retry)
|
|
159
|
+
continue
|
|
160
|
+
text, close_claim, body = self._resolve_tools(
|
|
161
|
+
model=model,
|
|
162
|
+
sticky=sticky,
|
|
163
|
+
search=search,
|
|
164
|
+
store=plan["store"],
|
|
165
|
+
body=body,
|
|
166
|
+
)
|
|
167
|
+
if not text.strip() and not close_claim:
|
|
168
|
+
last_error = XAIError(f"{model} returned empty output")
|
|
169
|
+
time.sleep(2**retry)
|
|
170
|
+
continue
|
|
171
|
+
self.last_model = model
|
|
172
|
+
result = {
|
|
173
|
+
"model": model,
|
|
174
|
+
"text": text,
|
|
175
|
+
"citations": extract_citations(body),
|
|
176
|
+
"response_id": body.get("id"),
|
|
177
|
+
"cached_tokens": _cached_tokens(body),
|
|
178
|
+
"input_tokens": _input_tokens(body),
|
|
179
|
+
"compaction": None,
|
|
180
|
+
"close_claim": close_claim,
|
|
181
|
+
"raw": body,
|
|
182
|
+
}
|
|
183
|
+
if result["input_tokens"] >= COMPACT_AFTER_TOKENS:
|
|
184
|
+
result["compaction"] = self._compact(model, payload["input"], sticky)
|
|
185
|
+
return result
|
|
186
|
+
except httpx.HTTPError as exc:
|
|
187
|
+
last_error = exc
|
|
188
|
+
time.sleep(2**retry)
|
|
189
|
+
raise XAIError(f"xAI request failed: {last_error}")
|
|
190
|
+
|
|
191
|
+
def _resolve_tools(
|
|
192
|
+
self,
|
|
193
|
+
*,
|
|
194
|
+
model: str,
|
|
195
|
+
sticky: str,
|
|
196
|
+
search: bool,
|
|
197
|
+
store: bool,
|
|
198
|
+
body: dict[str, Any],
|
|
199
|
+
) -> tuple[str, dict[str, Any] | None, dict[str, Any]]:
|
|
200
|
+
close_claim: dict[str, Any] | None = None
|
|
201
|
+
text = extract_output_text(body)
|
|
202
|
+
for _ in range(MAX_TOOL_ROUNDS):
|
|
203
|
+
calls = extract_function_calls(body)
|
|
204
|
+
if not calls:
|
|
205
|
+
break
|
|
206
|
+
outputs: list[dict[str, Any]] = []
|
|
207
|
+
for call in calls:
|
|
208
|
+
executed = execute_agent_tool(call["name"], call["arguments"])
|
|
209
|
+
if call["name"] == "close_claim" and executed.get("ok"):
|
|
210
|
+
close_claim = executed
|
|
211
|
+
outputs.append(
|
|
212
|
+
{
|
|
213
|
+
"type": "function_call_output",
|
|
214
|
+
"call_id": call["call_id"],
|
|
215
|
+
"output": json.dumps(executed),
|
|
216
|
+
}
|
|
217
|
+
)
|
|
218
|
+
if text.strip() or not body.get("id"):
|
|
219
|
+
break
|
|
220
|
+
follow = {
|
|
221
|
+
"model": model,
|
|
222
|
+
"store": store,
|
|
223
|
+
"input": outputs,
|
|
224
|
+
"prompt_cache_key": sticky,
|
|
225
|
+
"previous_response_id": body.get("id"),
|
|
226
|
+
"tools": agent_tools(search=search),
|
|
227
|
+
}
|
|
228
|
+
try:
|
|
229
|
+
response = httpx.post(
|
|
230
|
+
f"{self.base_url}/responses",
|
|
231
|
+
headers={
|
|
232
|
+
"Authorization": f"Bearer {self.api_key}",
|
|
233
|
+
"Content-Type": "application/json",
|
|
234
|
+
"x-grok-conv-id": sticky,
|
|
235
|
+
},
|
|
236
|
+
json=follow,
|
|
237
|
+
timeout=self.timeout,
|
|
238
|
+
)
|
|
239
|
+
except httpx.HTTPError:
|
|
240
|
+
break
|
|
241
|
+
if response.status_code >= 400:
|
|
242
|
+
break
|
|
243
|
+
body = response.json()
|
|
244
|
+
if body.get("error") or body.get("status") == "failed":
|
|
245
|
+
break
|
|
246
|
+
text = extract_output_text(body)
|
|
247
|
+
return text, close_claim, body
|
|
248
|
+
|
|
249
|
+
def _compact(self, model: str, messages: list[dict[str, Any]], conv_id: str) -> dict[str, Any] | None:
|
|
250
|
+
try:
|
|
251
|
+
response = httpx.post(
|
|
252
|
+
f"{self.base_url}/responses/compact",
|
|
253
|
+
headers={
|
|
254
|
+
"Authorization": f"Bearer {self.api_key}",
|
|
255
|
+
"Content-Type": "application/json",
|
|
256
|
+
"x-grok-conv-id": conv_id,
|
|
257
|
+
},
|
|
258
|
+
json={"model": model, "input": messages},
|
|
259
|
+
timeout=self.timeout,
|
|
260
|
+
)
|
|
261
|
+
if response.status_code >= 400:
|
|
262
|
+
return None
|
|
263
|
+
body = response.json()
|
|
264
|
+
items = body.get("output") or []
|
|
265
|
+
if items and items[0].get("type") == "compaction" and items[0].get("encrypted_content"):
|
|
266
|
+
return items[0]
|
|
267
|
+
except httpx.HTTPError:
|
|
268
|
+
return None
|
|
269
|
+
return None
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def _request_plans(
|
|
273
|
+
*,
|
|
274
|
+
previous_response_id: str | None,
|
|
275
|
+
compaction: dict[str, Any] | None,
|
|
276
|
+
) -> list[dict[str, Any]]:
|
|
277
|
+
plans: list[dict[str, Any]] = []
|
|
278
|
+
if previous_response_id:
|
|
279
|
+
plans.append({"store": True, "previous_response_id": previous_response_id, "compaction": None})
|
|
280
|
+
if compaction and compaction.get("encrypted_content"):
|
|
281
|
+
plans.append({"store": True, "previous_response_id": None, "compaction": compaction})
|
|
282
|
+
plans.append({"store": True, "previous_response_id": None, "compaction": None})
|
|
283
|
+
plans.append({"store": False, "previous_response_id": None, "compaction": None})
|
|
284
|
+
# Deduplicate identical plans while preserving order.
|
|
285
|
+
seen: set[str] = set()
|
|
286
|
+
unique: list[dict[str, Any]] = []
|
|
287
|
+
for plan in plans:
|
|
288
|
+
key = json.dumps(
|
|
289
|
+
{
|
|
290
|
+
"store": plan["store"],
|
|
291
|
+
"prev": plan.get("previous_response_id"),
|
|
292
|
+
"cmp": bool(plan.get("compaction")),
|
|
293
|
+
},
|
|
294
|
+
sort_keys=True,
|
|
295
|
+
)
|
|
296
|
+
if key not in seen:
|
|
297
|
+
seen.add(key)
|
|
298
|
+
unique.append(plan)
|
|
299
|
+
return unique
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
def _build_payload(
|
|
303
|
+
*,
|
|
304
|
+
model: str,
|
|
305
|
+
prompt: str,
|
|
306
|
+
system: str | None,
|
|
307
|
+
search: bool,
|
|
308
|
+
cache_key: str,
|
|
309
|
+
previous_response_id: str | None,
|
|
310
|
+
compaction: dict[str, Any] | None,
|
|
311
|
+
store: bool,
|
|
312
|
+
) -> dict[str, Any]:
|
|
313
|
+
messages: list[dict[str, Any]] = []
|
|
314
|
+
if previous_response_id:
|
|
315
|
+
messages.append({"role": "user", "content": prompt})
|
|
316
|
+
elif compaction:
|
|
317
|
+
messages.append(compaction)
|
|
318
|
+
messages.append({"role": "user", "content": prompt})
|
|
319
|
+
else:
|
|
320
|
+
if system:
|
|
321
|
+
messages.append({"role": "system", "content": system})
|
|
322
|
+
messages.append({"role": "user", "content": prompt})
|
|
323
|
+
payload: dict[str, Any] = {
|
|
324
|
+
"model": model,
|
|
325
|
+
"store": store,
|
|
326
|
+
"input": messages,
|
|
327
|
+
"prompt_cache_key": cache_key,
|
|
328
|
+
}
|
|
329
|
+
if previous_response_id:
|
|
330
|
+
payload["previous_response_id"] = previous_response_id
|
|
331
|
+
if search:
|
|
332
|
+
payload["tools"] = agent_tools(search=True)
|
|
333
|
+
return payload
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def agent_tools(*, search: bool = False) -> list[dict[str, Any]]:
|
|
337
|
+
tools: list[dict[str, Any]] = [CLOSE_CLAIM_TOOL]
|
|
338
|
+
if search:
|
|
339
|
+
tools.extend(SEARCH_TOOLS)
|
|
340
|
+
return tools
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
def execute_agent_tool(name: str, arguments: Any) -> dict[str, Any]:
|
|
344
|
+
if name == "close_claim":
|
|
345
|
+
return parse_close_claim(arguments)
|
|
346
|
+
return {"ok": False, "error": f"unknown tool {name}"}
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
def parse_close_claim(arguments: Any) -> dict[str, Any]:
|
|
350
|
+
if isinstance(arguments, str):
|
|
351
|
+
try:
|
|
352
|
+
arguments = json.loads(arguments) if arguments.strip() else {}
|
|
353
|
+
except json.JSONDecodeError:
|
|
354
|
+
return {"ok": False, "error": "arguments are not JSON"}
|
|
355
|
+
if not isinstance(arguments, dict):
|
|
356
|
+
return {"ok": False, "error": "arguments must be an object"}
|
|
357
|
+
verdict = str(arguments.get("verdict") or "").strip().lower()
|
|
358
|
+
if verdict not in {"confirmed", "falsified", "settled"}:
|
|
359
|
+
return {"ok": False, "error": "verdict must be confirmed, falsified, or settled"}
|
|
360
|
+
outcome = str(arguments.get("outcome") or "").strip()
|
|
361
|
+
if len(outcome) < 8:
|
|
362
|
+
return {"ok": False, "error": "outcome must describe what happened"}
|
|
363
|
+
source_url = str(arguments.get("source_url") or "").strip() or None
|
|
364
|
+
if source_url and not source_url.startswith(("http://", "https://")):
|
|
365
|
+
source_url = None
|
|
366
|
+
confidence = arguments.get("confidence")
|
|
367
|
+
if isinstance(confidence, bool) or confidence == "":
|
|
368
|
+
confidence = None
|
|
369
|
+
elif isinstance(confidence, (int, float)):
|
|
370
|
+
confidence = max(0.0, min(100.0, float(confidence)))
|
|
371
|
+
else:
|
|
372
|
+
try:
|
|
373
|
+
confidence = max(0.0, min(100.0, float(str(confidence).replace("%", ""))))
|
|
374
|
+
except (TypeError, ValueError):
|
|
375
|
+
confidence = None
|
|
376
|
+
return {
|
|
377
|
+
"ok": True,
|
|
378
|
+
"closed": True,
|
|
379
|
+
"verdict": verdict,
|
|
380
|
+
"outcome": outcome,
|
|
381
|
+
"confidence": confidence,
|
|
382
|
+
"source_url": source_url,
|
|
383
|
+
"source_title": str(arguments.get("source_title") or "").strip() or None,
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
def _cached_tokens(body: dict[str, Any]) -> int:
|
|
388
|
+
usage = body.get("usage") or {}
|
|
389
|
+
details = usage.get("input_tokens_details") or usage.get("prompt_tokens_details") or {}
|
|
390
|
+
return int(details.get("cached_tokens") or 0)
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
def _input_tokens(body: dict[str, Any]) -> int:
|
|
394
|
+
usage = body.get("usage") or {}
|
|
395
|
+
return int(usage.get("input_tokens") or usage.get("prompt_tokens") or 0)
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
def extract_output_text(body: dict[str, Any]) -> str:
|
|
399
|
+
chunks: list[str] = []
|
|
400
|
+
for item in body.get("output") or []:
|
|
401
|
+
if item.get("type") != "message":
|
|
402
|
+
continue
|
|
403
|
+
for part in item.get("content") or []:
|
|
404
|
+
if part.get("type") in {"output_text", "text"} and part.get("text"):
|
|
405
|
+
chunks.append(part["text"])
|
|
406
|
+
if chunks:
|
|
407
|
+
return "\n".join(chunks)
|
|
408
|
+
if body.get("output_text"):
|
|
409
|
+
return str(body["output_text"])
|
|
410
|
+
return ""
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
def extract_function_calls(body: dict[str, Any]) -> list[dict[str, Any]]:
|
|
414
|
+
found: list[dict[str, Any]] = []
|
|
415
|
+
for item in body.get("output") or []:
|
|
416
|
+
if item.get("type") != "function_call":
|
|
417
|
+
continue
|
|
418
|
+
found.append(
|
|
419
|
+
{
|
|
420
|
+
"name": str(item.get("name") or ""),
|
|
421
|
+
"call_id": str(item.get("call_id") or item.get("id") or ""),
|
|
422
|
+
"arguments": item.get("arguments") or "{}",
|
|
423
|
+
}
|
|
424
|
+
)
|
|
425
|
+
return found
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
def extract_citations(body: dict[str, Any]) -> list[dict[str, str]]:
|
|
429
|
+
found: list[dict[str, str]] = []
|
|
430
|
+
seen: set[str] = set()
|
|
431
|
+
|
|
432
|
+
def add(url: str, title: str = "") -> None:
|
|
433
|
+
if url and url not in seen:
|
|
434
|
+
seen.add(url)
|
|
435
|
+
found.append({"url": url, "title": title})
|
|
436
|
+
|
|
437
|
+
for item in body.get("output") or []:
|
|
438
|
+
for part in item.get("content") or []:
|
|
439
|
+
for annotation in part.get("annotations") or []:
|
|
440
|
+
url = annotation.get("url") or annotation.get("href") or ""
|
|
441
|
+
title = annotation.get("title") or annotation.get("text") or ""
|
|
442
|
+
add(url, title)
|
|
443
|
+
if part.get("type") in {"citation", "url_citation"}:
|
|
444
|
+
add(part.get("url") or "", part.get("title") or "")
|
|
445
|
+
citations = item.get("citations") or []
|
|
446
|
+
if isinstance(citations, list):
|
|
447
|
+
for citation in citations:
|
|
448
|
+
if isinstance(citation, str):
|
|
449
|
+
add(citation)
|
|
450
|
+
elif isinstance(citation, dict):
|
|
451
|
+
add(citation.get("url") or citation.get("href") or "", citation.get("title") or "")
|
|
452
|
+
|
|
453
|
+
for citation in body.get("citations") or []:
|
|
454
|
+
if isinstance(citation, str):
|
|
455
|
+
add(citation)
|
|
456
|
+
elif isinstance(citation, dict):
|
|
457
|
+
add(citation.get("url") or "", citation.get("title") or "")
|
|
458
|
+
return found
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
def parse_json_object(text: str) -> dict[str, Any]:
|
|
462
|
+
if not text or not str(text).strip():
|
|
463
|
+
raise XAIError("Model returned empty output")
|
|
464
|
+
cleaned = (
|
|
465
|
+
str(text)
|
|
466
|
+
.strip()
|
|
467
|
+
.replace("\u201c", '"')
|
|
468
|
+
.replace("\u201d", '"')
|
|
469
|
+
.replace("\u2018", "'")
|
|
470
|
+
.replace("\u2019", "'")
|
|
471
|
+
)
|
|
472
|
+
blobs: list[str] = [m.group(1).strip() for m in JSON_FENCE.finditer(cleaned)]
|
|
473
|
+
blobs.extend(_json_blobs(cleaned))
|
|
474
|
+
blobs.append(cleaned)
|
|
475
|
+
seen: set[str] = set()
|
|
476
|
+
last_error: Exception | None = None
|
|
477
|
+
for blob in blobs:
|
|
478
|
+
if not blob or blob in seen:
|
|
479
|
+
continue
|
|
480
|
+
seen.add(blob)
|
|
481
|
+
for variant in (blob, TRAILING_COMMA.sub(r"\1", blob)):
|
|
482
|
+
try:
|
|
483
|
+
data = json.loads(variant)
|
|
484
|
+
except json.JSONDecodeError as exc:
|
|
485
|
+
last_error = exc
|
|
486
|
+
continue
|
|
487
|
+
if isinstance(data, dict):
|
|
488
|
+
return data
|
|
489
|
+
raise XAIError(f"Model did not return JSON: {cleaned[:400]}") from last_error
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
def _json_blobs(text: str) -> list[str]:
|
|
493
|
+
decoder = json.JSONDecoder()
|
|
494
|
+
blobs: list[str] = []
|
|
495
|
+
idx = 0
|
|
496
|
+
while idx < len(text):
|
|
497
|
+
start = text.find("{", idx)
|
|
498
|
+
if start < 0:
|
|
499
|
+
break
|
|
500
|
+
try:
|
|
501
|
+
_, end = decoder.raw_decode(text, start)
|
|
502
|
+
blobs.append(text[start:end])
|
|
503
|
+
idx = end
|
|
504
|
+
except json.JSONDecodeError:
|
|
505
|
+
idx = start + 1
|
|
506
|
+
return blobs
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: realitydiff
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: Git for beliefs — living claims that watch the internet and version confidence.
|
|
5
|
+
License-File: LICENSE
|
|
6
|
+
Requires-Python: >=3.11
|
|
7
|
+
Requires-Dist: fastapi>=0.115.0
|
|
8
|
+
Requires-Dist: httpx>=0.28.0
|
|
9
|
+
Requires-Dist: pydantic>=2.10.0
|
|
10
|
+
Requires-Dist: uvicorn[standard]>=0.34.0
|
|
11
|
+
Provides-Extra: dev
|
|
12
|
+
Requires-Dist: pytest-asyncio>=0.25.0; extra == 'dev'
|
|
13
|
+
Requires-Dist: pytest>=8.3.0; extra == 'dev'
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# Reality Diff
|
|
17
|
+
|
|
18
|
+
Git for beliefs — a version history of a claim: evidence, confidence, and why that number moved. For people tracking a story that is still unfolding (products, policy, markets) who want a trail, not a vibe.
|
|
19
|
+
|
|
20
|
+
## Why I built this
|
|
21
|
+
|
|
22
|
+
News feeds overwrite your memory. Last week’s confidence on a rumor disappears under today’s take. I wanted git-style operations on a belief: commit when evidence actually moves, diff two weeks, blame a confidence jump, browse the claim as of a SHA, or revert a panic. So I built Reality Diff.
|
|
23
|
+
|
|
24
|
+
## News feed vs this
|
|
25
|
+
|
|
26
|
+
| | Typical news / search | Reality Diff |
|
|
27
|
+
| --- | --- | --- |
|
|
28
|
+
| Memory | Overwritten | Versioned history per claim |
|
|
29
|
+
| Confidence | Vibes / headlines | Explicit score + reason when it moves |
|
|
30
|
+
| Compare | Manual screenshots | `diff` between commits |
|
|
31
|
+
| Undo | None | `revert` to an earlier belief state |
|
|
32
|
+
|
|
33
|
+
## Features
|
|
34
|
+
|
|
35
|
+
**Claims**
|
|
36
|
+
|
|
37
|
+
- Open a claim (e.g. “Apple is going to replace Siri with an LLM”)
|
|
38
|
+
- Living object: evidence for/against, unknowns, predictions, confidence
|
|
39
|
+
- Commits only when **meaningful** new evidence appears (cosmetic rewrites dropped)
|
|
40
|
+
|
|
41
|
+
**Git-style ops**
|
|
42
|
+
|
|
43
|
+
- `commit` · `diff` · `blame` · `revert` · `watch` · `log`
|
|
44
|
+
- Browse the belief as of any SHA
|
|
45
|
+
- Background watch via OS timer (macOS LaunchAgent / Linux systemd / Windows task) or the UI interval picker
|
|
46
|
+
|
|
47
|
+
**Surfaces**
|
|
48
|
+
|
|
49
|
+
- Local web UI (`realitydiff serve` → <http://localhost:8000>)
|
|
50
|
+
- CLI for agents and scripts
|
|
51
|
+
- REST API for the same actions
|
|
52
|
+
|
|
53
|
+
## Install
|
|
54
|
+
|
|
55
|
+
One line (macOS or Linux, no sudo):
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
curl -fsSL https://raw.githubusercontent.com/kavin0x/RealityDiff/main/install.sh | sh
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
It installs [uv](https://docs.astral.sh/uv/) into `~/.local/bin` if you don't have it, installs `realitydiff` as an isolated uv tool, and can save your `XAI_API_KEY` to `~/.config/realitydiff/env` (permissions 600). It never uses sudo and only touches your shell rc if you say yes.
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
# options go after `sh -s --`
|
|
65
|
+
curl -fsSL https://raw.githubusercontent.com/kavin0x/RealityDiff/main/install.sh | sh -s -- --yes # no prompts
|
|
66
|
+
curl -fsSL https://raw.githubusercontent.com/kavin0x/RealityDiff/main/install.sh | sh -s -- --ref <tag-or-commit>
|
|
67
|
+
curl -fsSL https://raw.githubusercontent.com/kavin0x/RealityDiff/main/install.sh | sh -s -- --uninstall
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Prefer to read it first? Download `install.sh`, look it over, then run `sh install.sh --help`.
|
|
71
|
+
|
|
72
|
+
Or install by hand:
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
uv tool install "realitydiff @ https://github.com/kavin0x/RealityDiff/archive/main.tar.gz"
|
|
76
|
+
# or
|
|
77
|
+
pipx install "realitydiff @ https://github.com/kavin0x/RealityDiff/archive/main.tar.gz"
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Then:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
realitydiff serve --host 127.0.0.1 # open http://127.0.0.1:8000
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
The installed CLI keeps claims in `~/.local/share/realitydiff/` (set `REALITYDIFF_DB` to change it). Inside a checkout it still uses `data/`.
|
|
87
|
+
|
|
88
|
+
## Try it (from a checkout)
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
export XAI_API_KEY=...
|
|
92
|
+
uv sync --extra dev
|
|
93
|
+
uv run realitydiff serve
|
|
94
|
+
# open http://localhost:8000
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
uv run realitydiff open "Apple is going to replace Siri with an LLM."
|
|
99
|
+
uv run realitydiff watch <claim_id>
|
|
100
|
+
uv run realitydiff log <claim_id>
|
|
101
|
+
uv run realitydiff diff <claim_id> <sha_a> <sha_b>
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
uv run realitydiff service install --interval 300
|
|
106
|
+
uv run pytest
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Stack
|
|
110
|
+
|
|
111
|
+
Python 3.11+ · uv · local web UI + REST · xAI Responses API (web + X search) · MIT
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
realitydiff/__init__.py,sha256=rqA8Il3aj7M5sKo6lE56zdLX7x-W9u0tgN3s-TVuYiU,253
|
|
2
|
+
realitydiff/__main__.py,sha256=6Gctit3yuonClXgwB3M4iE9iQUTxN3Y-OFItJKjxgqY,90
|
|
3
|
+
realitydiff/api.py,sha256=WluVZ5l2vlVNxoM5uMg_3tG0VTytAcHssFiO12PGtjc,9971
|
|
4
|
+
realitydiff/blame.py,sha256=__uhdtyJwdQl2u-SFNPXYc_a3KVCtlgkAHHkeqZSZP0,4080
|
|
5
|
+
realitydiff/claim.py,sha256=goTkcnGlQURVnaGdo4we01Xa3QtXENWQ835NcMxQsQ8,3913
|
|
6
|
+
realitydiff/cli.py,sha256=F6PAIQbdu3IHxfZMxHFQQHO2_GonWrwQ9gKvCwDvoLA,5696
|
|
7
|
+
realitydiff/compress.py,sha256=3WiS9L7Ls3sak4z6May5Lvdkj0yzkLCA1cfj6nfayTA,3500
|
|
8
|
+
realitydiff/config.py,sha256=si-PdboKGkylqbOHZ7X4_LOXcW2EGl5UaKyc_sXW0jA,2330
|
|
9
|
+
realitydiff/diffing.py,sha256=OXhaysMgB4Ipz8am90Dr9A6kCzfHjXyRltTf7M3NAPE,7499
|
|
10
|
+
realitydiff/hashing.py,sha256=zJM6YA8U9SkxtpZcjswclBkioHWyFxQ_aFK7K6jPUVs,678
|
|
11
|
+
realitydiff/models.py,sha256=3SYf5bKi8cu36NIdckPP2-bHpXK39lDN64_dAFmHYjs,6338
|
|
12
|
+
realitydiff/reasoner.py,sha256=AqHx2hOI9bEuTcFz9z5Djqw94PHyqMFb18T6_ry5fHc,17042
|
|
13
|
+
realitydiff/schedule.py,sha256=UQ-IZDELKGWmnK70IzvbUxP3Ze3KBEqLrT0iMVFzsFc,7303
|
|
14
|
+
realitydiff/service.py,sha256=IGqcM7Z76eFczzwLO_ysDmDZfY91o1mLtweY-ZrnldE,6712
|
|
15
|
+
realitydiff/store.py,sha256=tAwcdzPxH6EhYpmZhuqDuMg8dvO3cWQTSbjLMsXa3iw,11559
|
|
16
|
+
realitydiff/watcher.py,sha256=PplYjf5hj7Le0ejn7xlWUCHGSEwqHX70fRl7FQSHa5k,1255
|
|
17
|
+
realitydiff/xai.py,sha256=K8gDBaDsCqCjh8RfE9rvV8GKfU9MDYaH2I2b0Tb4dAU,18636
|
|
18
|
+
realitydiff/static/app.js,sha256=CVwwgZyxsFvYJUWIbNx6XDbWEn5PoA_fsgWZLIAFY6Y,29338
|
|
19
|
+
realitydiff/static/favicon.svg,sha256=YFU_cjAIA8DBdzloeVbPfmtUHOGj1YrkZXV8PigVRh4,227
|
|
20
|
+
realitydiff/static/index.html,sha256=X2uLukL3K8h9ULKmTNtLw88KMKePEDBcTAaVYyO7gYQ,8598
|
|
21
|
+
realitydiff/static/styles.css,sha256=PUuEveM3ljNOI8MSUmDxs2UPeL7YchEEufz-RFqIbyg,15493
|
|
22
|
+
realitydiff-0.4.0.dist-info/METADATA,sha256=WgeXeDSS9F5DAGCPGltAE1hpVEY0EoS3Y8v1LuZEEf0,3882
|
|
23
|
+
realitydiff-0.4.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
24
|
+
realitydiff-0.4.0.dist-info/entry_points.txt,sha256=mi_TnqLZVq0NKU_IPLisEyk7MsD7uuBon2tbYq7FMyk,53
|
|
25
|
+
realitydiff-0.4.0.dist-info/licenses/LICENSE,sha256=-g4ponRIZ8t9IoZaCz3ghxWn0fI0YCpkyjiV9VEu5Dc,1082
|
|
26
|
+
realitydiff-0.4.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Reality Diff contributors
|
|
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.
|