-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathgithub_store.py
More file actions
395 lines (323 loc) · 13.3 KB
/
Copy pathgithub_store.py
File metadata and controls
395 lines (323 loc) · 13.3 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
"""
cortex/github_store.py
----------------------
Push extracted context to GitHub repo via the REST API.
Repo structure (v2):
sources/src_*.md — immutable source manifests
records/YYYY-MM-DD/mem_*.md — dated atomic claims and decisions
entities/people/person_*.md — canonical person pages (append-only)
entities/projects/proj_*.md — canonical project pages
tasks/open/task_*.md — open actions and questions
tasks/resolved/task_*.md — completed tasks
knowledge/decisions/ — high-importance curated decisions
views/ — generated summaries (person dossiers, project briefs)
indexes/entity-registry.json — canonical entity ID → name mapping
Every file has YAML front matter with stable IDs, kind, status,
confidence, importance, source_ids, entity_ids, topics.
"""
import base64
import hashlib
import json
import os
import re
import requests
from datetime import datetime
from typing import Optional
GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN", "")
CORTEX_REPO = os.environ.get("CORTEX_REPO", "")
GITHUB_API = "https://api.github.com"
BRANCH = os.environ.get("CORTEX_BRANCH", "main")
# ── GitHub helpers ─────────────────────────────────────────────────────────────
def _headers() -> dict:
return {
"Authorization": f"Bearer {GITHUB_TOKEN}",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
def _get_existing_sha(filepath: str) -> Optional[str]:
url = f"{GITHUB_API}/repos/{CORTEX_REPO}/contents/{filepath}"
resp = requests.get(url, headers=_headers(), params={"ref": BRANCH})
if resp.status_code == 200:
return resp.json().get("sha")
return None
def _get_existing_content(filepath: str) -> Optional[str]:
url = f"{GITHUB_API}/repos/{CORTEX_REPO}/contents/{filepath}"
resp = requests.get(url, headers=_headers(), params={"ref": BRANCH})
if resp.status_code == 200:
data = resp.json()
return base64.b64decode(data["content"]).decode("utf-8")
return None
def push_file(filepath: str, content: str, commit_message: str) -> bool:
if not GITHUB_TOKEN or not CORTEX_REPO:
raise EnvironmentError("GITHUB_TOKEN and CORTEX_REPO must be set")
url = f"{GITHUB_API}/repos/{CORTEX_REPO}/contents/{filepath}"
sha = _get_existing_sha(filepath)
payload = {
"message": commit_message,
"content": base64.b64encode(content.encode("utf-8")).decode("ascii"),
"branch": BRANCH,
}
if sha:
payload["sha"] = sha
resp = requests.put(url, headers=_headers(), json=payload)
return resp.status_code in (200, 201)
def _slug(text: str, max_len: int = 50) -> str:
s = re.sub(r"[^\w\s-]", "", text.lower())
s = re.sub(r"[\s_]+", "-", s).strip("-")
return s[:max_len]
# ── Front matter builder ───────────────────────────────────────────────────────
def _frontmatter(fields: dict) -> str:
"""Build YAML front matter from a dict."""
lines = ["---"]
for k, v in fields.items():
if isinstance(v, list):
if v:
lines.append(f"{k}:")
for item in v:
lines.append(f" - {item}")
else:
lines.append(f"{k}: []")
elif v is None:
lines.append(f"{k}: null")
else:
# Quote strings that contain special chars
sv = str(v)
if any(c in sv for c in [":", "#", "[", "]", "{", "}"]):
lines.append(f'{k}: "{sv}"')
else:
lines.append(f"{k}: {sv}")
lines.append("---")
return "\n".join(lines)
# ── Main save function ─────────────────────────────────────────────────────────
def save_extracted_context(extracted: dict, raw_text: str = "") -> list[str]:
"""
Save all extracted context items to the GitHub repo (new v2 structure).
Returns list of file paths successfully saved.
"""
source = extracted.get("_source", "unknown")
source_id = extracted.get("_source_id", "src_unknown")
timestamp = extracted.get("_timestamp", datetime.now().isoformat())
date_str = timestamp[:10]
captured_at = timestamp
saved = []
# ── 1. Source manifest ─────────────────────────────────────────────────────
source_path = f"sources/{source_id}.md"
source_fm = _frontmatter({
"id": source_id,
"kind": "source",
"source": source,
"captured_at": captured_at,
"raw_length": extracted.get("_raw_length", 0),
"scope": "private",
})
source_content = f"""{source_fm}
# Source: {source} · {date_str}
**Captured:** {captured_at}
**From:** `{source}`
## Summary
{extracted.get('summary', '_No summary extracted._')}
## Raw Excerpt
> {raw_text[:400].strip()}{"..." if len(raw_text) > 400 else ""}
"""
if push_file(source_path, source_content, f"source({source}): {source_id} @ {date_str}"):
saved.append(source_path)
# ── 2. Records (claims, decisions, observations) ───────────────────────────
for record in extracted.get("records", []):
rid = record.get("id", "mem_unknown")
kind = record.get("kind", "claim")
content = record.get("content", "")
confidence = record.get("confidence", "confirmed")
importance = record.get("importance", 3)
entity_ids = record.get("entity_ids", [])
topics = record.get("topics", [])
occurred_at = record.get("occurred_at")
if not content.strip():
continue
filepath = f"records/{date_str}/{rid}.md"
fm = _frontmatter({
"id": rid,
"kind": kind,
"status": "active",
"occurred_at": occurred_at or captured_at,
"captured_at": captured_at,
"source_ids": [source_id],
"entity_ids": entity_ids,
"topics": topics,
"confidence": confidence,
"importance": importance,
"scope": "private",
})
header = "Decision" if kind == "decision" else kind.title()
doc = f"""{fm}
# {header}: {content[:80]}{"..." if len(content) > 80 else ""}
{content}
**Source:** `{source}` · {date_str}
**Confidence:** {confidence} · **Importance:** {importance}/5
"""
if push_file(filepath, doc, f"record({kind}): {rid}"):
saved.append(filepath)
# Also copy high-importance decisions to knowledge/decisions/
if kind == "decision" and importance >= 4:
knowledge_path = f"knowledge/decisions/{rid}.md"
if push_file(knowledge_path, doc, f"knowledge(decision): {rid}"):
saved.append(knowledge_path)
# ── 3. Tasks (actions + questions) ────────────────────────────────────────
for task in extracted.get("tasks", []):
tid = task.get("id", "task_unknown")
kind = task.get("kind", "action")
content = task.get("content", "")
importance = task.get("importance", 3)
entity_ids = task.get("entity_ids", [])
topics = task.get("topics", [])
status = task.get("status", "open")
if not content.strip():
continue
filepath = f"tasks/{status}/{tid}.md"
fm = _frontmatter({
"id": tid,
"kind": kind,
"status": status,
"captured_at": captured_at,
"source_ids": [source_id],
"entity_ids": entity_ids,
"topics": topics,
"importance": importance,
"scope": "private",
})
icon = "❓" if kind == "question" else "☐"
doc = f"""{fm}
# {icon} {content}
**Kind:** {kind} · **Status:** {status} · **Importance:** {importance}/5
**Source:** `{source}` · {date_str}
"""
if push_file(filepath, doc, f"task({kind}): {tid}"):
saved.append(filepath)
# File questions as GitHub Issues too
if kind == "question":
open_github_issue(
title=f"❓ {content[:100]}",
body=f"**Source:** {source}\n**Captured:** {captured_at}\n\n> Auto-filed by Cortex",
labels=["open-question", "cortex"]
)
# ── 4. Entities (people, projects, orgs) ──────────────────────────────────
for entity in extracted.get("entities", []):
eid = entity.get("id", "")
ekind = entity.get("kind", "person")
name = entity.get("name", "")
context_str = entity.get("context", "")
aliases = entity.get("aliases", [])
if not eid or not name:
continue
folder = f"entities/{ekind}s" # people, projects, orgs
filepath = f"{folder}/{eid}.md"
# Append to existing entity page, or create new
existing = _get_existing_content(filepath)
if existing:
new_entry = f"\n## {date_str} · {source}\n{context_str}\n"
doc = existing + new_entry
commit_msg = f"entity({ekind}): update {eid}"
else:
fm = _frontmatter({
"id": eid,
"kind": ekind,
"name": name,
"aliases": aliases,
"status": "active",
"first_seen": captured_at,
"scope": "private",
})
doc = f"""{fm}
# {name}
{context_str}
## {date_str} · {source}
{context_str}
"""
commit_msg = f"entity({ekind}): create {eid}"
if push_file(filepath, doc, commit_msg):
saved.append(filepath)
# ── 5. Update entity registry index ───────────────────────────────────────
_update_entity_registry(extracted.get("entities", []), captured_at)
return saved
def _update_entity_registry(entities: list[dict], captured_at: str):
"""Update indexes/entity-registry.json with new entities."""
if not entities:
return
registry_path = "indexes/entity-registry.json"
existing_content = _get_existing_content(registry_path)
if existing_content:
try:
registry = json.loads(existing_content)
except Exception:
registry = {}
else:
registry = {}
changed = False
for entity in entities:
eid = entity.get("id", "")
if not eid:
continue
if eid not in registry:
registry[eid] = {
"id": eid,
"kind": entity.get("kind", ""),
"name": entity.get("name", ""),
"aliases": entity.get("aliases", []),
"first_seen": captured_at,
}
changed = True
else:
# Add new aliases
existing_aliases = set(registry[eid].get("aliases", []))
new_aliases = set(entity.get("aliases", []))
if new_aliases - existing_aliases:
registry[eid]["aliases"] = list(existing_aliases | new_aliases)
changed = True
if changed:
push_file(
registry_path,
json.dumps(registry, indent=2, ensure_ascii=False),
f"index: update entity-registry ({len(entities)} entities)"
)
# ── GitHub Issues for open questions ──────────────────────────────────────────
def open_github_issue(title: str, body: str, labels: list[str] = None) -> Optional[str]:
url = f"{GITHUB_API}/repos/{CORTEX_REPO}/issues"
payload = {
"title": title,
"body": body,
"labels": labels or ["open-question"],
}
resp = requests.post(url, headers=_headers(), json=payload)
if resp.status_code == 201:
return resp.json().get("html_url")
return None
# ── Repo initialisation ───────────────────────────────────────────────────────
def init_repo_structure() -> list[str]:
"""Create the v2 folder structure with .gitkeep files."""
folders = [
"sources",
"records",
"entities/people",
"entities/projects",
"entities/orgs",
"knowledge/decisions",
"knowledge/facts",
"tasks/open",
"tasks/resolved",
"views",
"indexes",
"governance",
"archive",
]
created = []
for folder in folders:
filepath = f"{folder}/.gitkeep"
if push_file(filepath, "", f"init: create {folder}/"):
created.append(folder)
print(f" ✅ {folder}/")
else:
print(f" ⚠️ {folder}/ (already exists or error)")
return created
if __name__ == "__main__":
print("Initialising Cortex repo structure (v2)...")
init_repo_structure()
print("Done. Check your GitHub repo.")