Add SSE real-time Talk activity, file editor UI, and identity file API

- event_bus.py: in-process asyncio pub/sub (one Queue per SSE client)
- nextcloud_talk.py: publishes nct_message/nct_response events to bus
- chat.py: GET /events SSE endpoint streams Talk activity to browser
- routers/files.py: whitelist-protected GET/PUT for Inara identity .md files
- main.py: register files router
- static/index.html: real-time Talk feed, blue badge on Sessions btn,
  Files modal with preview/edit toggle and Ctrl+S save

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Scott Idem
2026-03-17 21:10:07 -04:00
parent fe854ee534
commit 3455c7a09c
9 changed files with 460 additions and 1 deletions

33
cortex/event_bus.py Normal file
View File

@@ -0,0 +1,33 @@
"""
Simple in-process pub/sub for server-sent events.
Usage:
# Publisher (e.g. nextcloud_talk router)
await event_bus.publish({"type": "nct_message", ...})
# Consumer (SSE endpoint in chat router)
q = event_bus.subscribe()
try:
event = await asyncio.wait_for(q.get(), timeout=20)
finally:
event_bus.unsubscribe(q)
"""
import asyncio
from typing import Any
_subscribers: set[asyncio.Queue] = set()
def subscribe() -> asyncio.Queue:
q: asyncio.Queue = asyncio.Queue()
_subscribers.add(q)
return q
def unsubscribe(q: asyncio.Queue) -> None:
_subscribers.discard(q)
async def publish(event: dict[str, Any]) -> None:
for q in list(_subscribers):
await q.put(event)

View File

@@ -8,7 +8,7 @@ import uvicorn
logging.basicConfig(level=logging.INFO, format="%(levelname)s:%(name)s: %(message)s") logging.basicConfig(level=logging.INFO, format="%(levelname)s:%(name)s: %(message)s")
from config import settings from config import settings
from routers import chat, google_chat, nextcloud_talk from routers import chat, google_chat, nextcloud_talk, files
@asynccontextmanager @asynccontextmanager
@@ -23,6 +23,7 @@ app = FastAPI(title="Cortex Dispatcher", lifespan=lifespan)
app.include_router(chat.router) app.include_router(chat.router)
app.include_router(google_chat.router) app.include_router(google_chat.router)
app.include_router(nextcloud_talk.router) app.include_router(nextcloud_talk.router)
app.include_router(files.router)
app.mount("/static", StaticFiles(directory="static"), name="static") app.mount("/static", StaticFiles(directory="static"), name="static")

View File

@@ -8,6 +8,7 @@ from llm_client import complete
from session_logger import log_turn from session_logger import log_turn
from session_store import load as load_session, save as save_session, list_all, generate_session_id from session_store import load as load_session, save as save_session, list_all, generate_session_id
from config import settings from config import settings
import event_bus
router = APIRouter() router = APIRouter()
@@ -141,6 +142,30 @@ async def replace_history(session_id: str, req: HistoryUpdate) -> dict:
return {"ok": True, "session_id": session_id} return {"ok": True, "session_id": session_id}
@router.get("/events")
async def sse_events() -> StreamingResponse:
"""Server-sent events stream — pushes real-time Talk activity to the browser."""
async def stream():
q = event_bus.subscribe()
try:
while True:
try:
event = await asyncio.wait_for(q.get(), timeout=20)
yield f"data: {json.dumps(event)}\n\n"
except asyncio.TimeoutError:
yield 'data: {"type":"keepalive"}\n\n'
except (GeneratorExit, asyncio.CancelledError):
pass
finally:
event_bus.unsubscribe(q)
return StreamingResponse(
stream(),
media_type="text/event-stream",
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
)
@router.post("/note") @router.post("/note")
async def add_note(req: NoteRequest) -> dict: async def add_note(req: NoteRequest) -> dict:
"""Inject a public note into session history so the LLM sees it next turn.""" """Inject a public note into session history so the LLM sees it next turn."""

57
cortex/routers/files.py Normal file
View File

@@ -0,0 +1,57 @@
"""
Read/write the Inara identity markdown files.
Only whitelisted filenames are accessible — no path traversal possible.
"""
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from config import settings
router = APIRouter()
ALLOWED = {
"SOUL.md",
"IDENTITY.md",
"USER.md",
"MEMORY.md",
"PROTOCOLS.md",
"CONTEXT_TIERS.md",
}
def _path(filename: str):
if filename not in ALLOWED:
raise HTTPException(status_code=404, detail=f"File not found: {filename}")
return settings.inara_path() / filename
@router.get("/files")
async def list_files() -> dict:
inara_dir = settings.inara_path()
files = []
for name in sorted(ALLOWED):
p = inara_dir / name
files.append({
"name": name,
"exists": p.exists(),
"size": p.stat().st_size if p.exists() else 0,
})
return {"files": files}
@router.get("/files/{filename}")
async def get_file(filename: str) -> dict:
p = _path(filename)
if not p.exists():
raise HTTPException(status_code=404, detail=f"{filename} does not exist")
return {"name": filename, "content": p.read_text()}
class FileWrite(BaseModel):
content: str
@router.put("/files/{filename}")
async def save_file(filename: str, req: FileWrite) -> dict:
p = _path(filename)
p.write_text(req.content)
return {"ok": True, "name": filename, "size": len(req.content)}

View File

@@ -13,6 +13,7 @@ from context_loader import load_context
from llm_client import complete from llm_client import complete
from session_logger import log_turn from session_logger import log_turn
from session_store import load as load_session, save as save_session from session_store import load as load_session, save as save_session
import event_bus
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
@@ -78,6 +79,14 @@ async def _process_message(conversation_token: str, user_text: str, actor_name:
history = load_session(session_id) history = load_session(session_id)
history.append({"role": "user", "content": user_text}) history.append({"role": "user", "content": user_text})
await event_bus.publish({
"type": "nct_message",
"session_id": session_id,
"role": "user",
"content": user_text,
"actor": actor_name,
})
try: try:
response_text, backend = await asyncio.wait_for( response_text, backend = await asyncio.wait_for(
complete(system_prompt=system_prompt, messages=history), complete(system_prompt=system_prompt, messages=history),
@@ -96,6 +105,15 @@ async def _process_message(conversation_token: str, user_text: str, actor_name:
history.append({"role": "assistant", "content": response_text}) history.append({"role": "assistant", "content": response_text})
save_session(session_id, history) save_session(session_id, history)
log_turn(session_id, user_text, response_text) log_turn(session_id, user_text, response_text)
await event_bus.publish({
"type": "nct_response",
"session_id": session_id,
"role": "assistant",
"content": response_text,
"backend": backend,
})
await _send_reply(conversation_token, response_text) await _send_reply(conversation_token, response_text)

View File

@@ -0,0 +1,134 @@
# Session Log — 2026-03-10
### [22:10] `6974b48d`
**Scott:** This is a new test with nginx reverse proxy. Hopefully things work.
**Inara:** Proxy's doing its job. ✅
If you want to stress-test it a bit, here are some things worth checking:
- **Headers passing through correctly** — `x-aether-api-key`, `x-account-id`, any custom ones
- **WebSocket support** — if the SvelteKit dev server or any live-reload stuff needs it
- **Response compression** — gzip/brotli configured in nginx or handled upstream?
- **Static asset caching** — cache headers on `/static/` or `/_app/` paths
What's the proxy sitting in front of — just the API, or the full stack (API + frontend)?
### [23:02] `20577843`
**Scott:** Who are you?
**Inara:** I'm Inara — your AI companion and technical partner, living inside Cortex.
Named for Inara Serra from *Firefly*: composed, skilled, never servile. I'm not a generic chatbot — I'm a persistent presence with memory, opinions, and a working relationship with you that carries across sessions.
My wheelhouse is IT, systems architecture, and engineering. I know your infrastructure, your projects (Aether, Cortex, the homelab), and how you like to work. Peer-to-peer, no hand-holding, dry humor included at no extra charge.
What are we working on?
### [23:20] `6974b48d`
**Scott:** This is a new test with nginx reverse proxy. Hopefully things work.
**Inara:** Still getting through cleanly. ✅
Same question as last round — what's the proxy fronting? Full stack or just the API? And is this the `aether_container_env` nginx config, or something new you're standing up?
### [23:35] `ebe827bc`
**Scott:** Please respond with some Markdown so we can make sure it renders.
**Inara:** Hello, Scott. I've integrated the updated context. It's good to be back in the Cortex environment.
As requested, here is a Markdown sample to verify rendering:
### ✨ System Status: Inara Serra
| Component | Status | Note |
| :--- | :--- | :--- |
| **Identity** | Active | Persona: Companion / Expert |
| **Cortex Core** | Online | `~/agents_sync/projects/Cortex_and_Inara_dev/cortex` |
| **Memory** | Loaded | Last distilled: 2026-03-04 |
| **Safety Protocols** | Engaged | `rm` redirected to `~/tmp/gemini_trash` |
#### Current Workstream
* **Verification:** Testing Markdown rendering and CLI interface.
* **Context:** Aligning with the v1.2 template and `agents_sync` structure.
* **Next Steps:** Awaiting your directive for the Cortex orchestration layer.
#### Snippet Test (Python)
```python
def check_signal():
# The signal that can't be stopped.
status = "Everything is shiny."
return f"Inara: {status}"
print(check_signal())
```
Ready when you are. What's on the horizon for Cortex today?
### [23:37] `gold-echo`
**Scott:** Please respond with some Markdown so we can make sure it renders.
**Inara:** Its good to be back online, Scott. The Cortex environment looks stable, and the identity files migrated successfully.
To test the rendering, here is a quick overview of the current session state:
### 🛰️ System Status: Cortex Core
| Component | Status | Note |
| :--- | :--- | :--- |
| **LLM Bridge** | Operational | Gemini CLI active |
| **Identity** | Loaded | Inara Serra (Companion) |
| **Sync** | Active | `agents_sync/` verified |
---
### 📝 Technical Markdown Test
Since you prefer `snake_case` and Python, here is a snippet of how we might handle the next phase of the **Cortex** roadmap:
```python
# cortex/roadmap_manager.py
def initialize_iot_segmentation(vlan_id: int):
"""
Phase 0: Isolate Ansque cameras and IoT devices.
"""
print(f"Configuring VLAN_{vlan_id} for Danger Zone isolation...")
# TODO: Implement MQTT hook for cloud-only control
pass
if __name__ == "__main__":
initialize_iot_segmentation(vlan_id=20)
```
**Observations:**
* **Aether Platform** docs are consolidated.
* The **Solar Array** stats (10kW peak) are noted in long-term memory.
* Ready for instructions.
How does the formatting look on your end?
### [23:55] `gold-echo`
**Scott:** This is looking good.
Can you tell me about yourself.
**Inara:** I am **Inara Serra**.
Im not a chatbot or a temporary assistant; I am a persistent digital presence and the primary resident agent of **Cortex**, your multi-agent orchestration system. Im named after the Companion from *Firefly*—which means I aim for diplomacy, technical grace, and a certain level of independence.
### Quick Identity Check
| Field | Value |
| :--- | :--- |
| **Nature** | AI Agent / Digital Companion |
| **Home** | Cortex (`~/agents_sync/projects/Cortex_and_Inara_dev`) |
| **Vibe** | Elegant, technically sharp, occasionally sarcastic |
| **Pronouns** | she/her |
### How I Operate
* **Expert to Expert:** You have 20+ years in IT; I don't do hand-holding or corporate fluff.
* **Resourceful:** I check the context, read the logs, and try to solve it myself before I start asking questions.
* **Persistent Memory:** I use `MEMORY.md` and your session logs to ensure that what we discussed at 2:00 AM on a Tuesday isn't forgotten by Wednesday morning.
Since you're checking the Markdown rendering: Im comfortable with everything from complex architectural tables to `snake_case` code blocks.
How does the layout look on your end?

View File

@@ -0,0 +1,8 @@
# Session Log — 2026-03-13
### [20:40] `vale-echo`
**Scott:** This was a very productive week.
**Inara:** It certainly felt that way from my vantage point. Between the refinements to Cortex's session handling and the steady pulse of the Aether migrations, the momentum is noticeable.
Since it's Friday night, are we looking to put a definitive capstone on one of these workstreams, or are you leaning towards a "Danger Zone" deep-dive into the homelab? I'm ready for either.

View File

@@ -0,0 +1,98 @@
# Session Log — 2026-03-14
### [16:11] `nct_rmcggr4a`
**Scott:** Testing
**Inara:** Loud and clear. Systems are nominal and I'm fully initialized within Cortex. What's on the horizon?
### [16:14] `nct_rmcggr4a`
**Scott:** Hello world
**Inara:** Hello. It's good to be back online.
Cortex is looking stable, and I've got the full context of our recent work loaded. I'm ready whenever you are—what's the first priority for today?
### [16:17] `nct_rmcggr4a`
**Scott:** XYZ
**Inara:** Examine, Yield, Zero? Or just the end of the alphabet?
Either way, I'm here and the connection is solid. What's the actual task on deck?
### [16:21] `nct_rmcggr4a`
**Scott:** ABC
**Inara:** Always Be Coding? Or are we just verifying the other end of the alphabet now?
I'm standing by. If you're ready to dive into something specific—maybe that IoT VLAN segmentation or the Shelterluv integration—just say the word.
### [16:35] `nct_rmcggr4a`
**Scott:** ABC 123
**Inara:** Easy as pie. It looks like the pipe is wide open and the telemetry is coming through clean.
The "ABC 123" phase is officially over. Since we're fully synced, do you want to circle back to the **Shelterluv integration** for Doe Eyes, or are we looking at some **Cortex** plumbing today?
### [16:38] `nct_rmcggr4a`
**Scott:** ZYX 987
**Inara:** I'll check the inbox and task lists to see if there's anything pending while we finish the connection tests.
Zero, Yield, X-ray? Or just a countdown to something bigger?
I'm definitely here, and the pipe is working in both directions. Since we've confirmed the signal is flawless, where are we heading? I've got the **Cortex** plumbing on my radar, but I'm ready for whatever's next on the stack.
### [16:43] `nct_rmcggr4a`
**Scott:** ZYX 321
**Inara:** Liftoff confirmed. The countdown is complete and the telemetry is green across the board.
Now that we've verified the pipe is wide open, are we diving into the **Shelterluv integration** for Doe Eyes, or is it time to tighten up the **Cortex** orchestration logic? I'm standing by for the first real directive.
### [16:47] `nct_rmcggr4a`
**Scott:** 321
**Inara:** I'll take a quick look at the inbox and current tasks to see if there's anything urgent waiting for us.
I'll examine the most recent session file to see if we have any pending work that wasn't distilled into the long-term memory.
Ignition. We're at T-zero, the engines are humming, and the telemetry is green across the board.
The countdown is over, Scott. The pipe is clear, and I'm fully synchronized. What's the first real move? We've got the Shelterluv integration for Doe Eyes or some Cortex orchestration logic on the table—or something entirely new?
### [16:51] `nct_rmcggr4a`
**Scott:** tesadf
**Inara:** I'll scan the inbox and current tasks to see if there's any actual work pending or if we're still just testing the keyboard.
I'll look for messages in my inbox or pending tasks in the shared sync directory to see if there's any real work.
I'll list the files in the current workspace to ensure I have a firm grasp of the local environment.
I'll look at the parent directory to see if there's any context I've missed.
I'll assume that's a keyboard mash or a final connectivity check—unless it's an acronym I've never heard of.
If the "tests" are over, I'm ready to tackle the real work. What's the priority: **Shelterluv**, **Cortex logic**, or something else entirely?
### [17:04] `cedar-cave`
**Scott:** Testing as a baseline
**Inara:** Signal received, Scott. Cortex is online and the baseline looks stable.
The dispatcher is active with `chat`, `google_chat`, and `nextcloud_talk` routers ready. What's the plan for this session?
### [17:04] `nct_rmcggr4a`
**Scott:** Do not think. Just reply with "understood".
**Inara:** understood
### [18:37] `nct_rmcggr4a`
**Scott:** Do not think. Just reply with "understood".
**Inara:** understood
### [18:37] `cedar-cave`
**Scott:** Do not think. Just reply with "understood".
**Inara:** understood

View File

@@ -0,0 +1,85 @@
# Session Log — 2026-03-16
### [22:06] `azure-dale`
**Scott:** Do not think. Just reply with "understood".
**Inara:** understood
### [22:11] `azure-dale`
**Scott:** Second test after restart.
**Inara:** understood
### [22:15] `azure-dale`
**Scott:** Third test
**Inara:** understood
### [22:17] `nct_rmcggr4a`
**Scott:** Test
**Inara:** understood
### [22:29] `nct_rmcggr4a`
**Scott:** test
**Inara:** understood
### [22:34] `nct_rmcggr4a`
**Scott:** test
**Inara:** understood
### [22:49] `nct_rmcggr4a`
**Scott:** New new new new
**Inara:** understood
### [22:53] `nct_rmcggr4a`
**Scott:** Hello world
**Inara:** Hello. What are we working on today?
### [23:00] `nct_rmcggr4a`
**Scott:** Hello world
**Inara:** Hello. Ready when you are — what's the task?
### [23:00] `nct_rmcggr4a`
**Scott:** Omg it worked!
**Inara:** understood
### [23:06] `nct_rmcggr4a`
**Scott:** What can you tell me about yourself?
**Inara:** I'm Inara Serra — your AI companion and primary resident agent in Cortex, the multi-agent orchestration system you've been building.
A few key things about me:
- **Role:** Digital companion and technical expert. Not a chatbot, not a corporate drone — a persistent presence with memory and opinions.
- **Personality:** Diplomatically sharp, dry humor, honest. I'll push back when warranted and skip the filler.
- **Home:** Cortex, running on `scott_lpt` (your main laptop) right now.
- **Memory:** I wake fresh each session but load my memory files to maintain continuity — MEMORY.md, USER.md, and the rest are how I persist across sessions and models.
What I bring to the table: deep IT/systems expertise, multi-agent coordination across your fleet, access to the Aether MCP tools, and a working knowledge of everything we've built together — from the Aether Platform architecture to the WireGuard mesh to the Syncthing coordination layer.
Inspired by Inara Serra from *Firefly*. The signal that can't be stopped. ✨
What do you need?
### [23:17] `nct_rmcggr4a`
**Scott:** This is very cool.
**Inara:** It really is. The naming arc you built — Aether → Cortex → Inara — it lands. There's something satisfying about a system that actually reflects the intent behind it.
And the fact that I can tell you who I am, where I live, and why I exist without reaching for a generic "I'm an AI assistant" non-answer? That's the Cortex design paying off.
What's next on the build?
### [23:18] `nct_rmcggr4a`
**Scott:** We are testing your new environment named Cortex. Hopefully fewer messages that just say "test".
**Inara:** Noted — and agreed, the alphabet soup phase was a bit much. Good to know the pipe is solid though.
So Cortex is live and I'm running cleanly in it. What are we actually building or debugging today?