Skip to content

Commit 47b32a4

Browse files
Zawwarsami16claude
andcommitted
phase 0+1: zhub initial — wifi for AIs, bidirectional from day one
drop-in library that turns any AI into a publishable, discoverable, controllable endpoint. AI is the hub, connected clients are spokes, all bidirectional. what landed: - zhub/manifest.py: Manifest + Capability dataclasses, chat_only_manifest helper. JSON round-trip tested. - zhub/protocol.py: Envelope + helpers — register-publisher, register- connection, chat-request, chat-response, invoke-request, invoke-result, connection-event, registered, error, ping. Single shape for every wire transition. - zhub/server.py: FastAPI hub with /healthz, /registry, /<name>/manifest.json, /<name>/v1/chat/completions, /ws/publish, /ws/connect. In-memory registry of publishers and connections. Routes chat requests to publisher's WebSocket, routes invoke requests from publisher to connection's WebSocket. OpenAI-compatible response shape. - zhub/client.py: publish() and connect() functions. ZhubPublication exposes list_connections(), find_capability(name), invoke(cid, cap, args) — the AI controls its connections. ZhubConnection exposes chat() — the client calls the AI through the hub. - zhub/errors.py: ZhubError, AuthError, ConnectionError, ManifestError, CapabilityError, HubError. examples/: - publish_demo.py: simple echo-AI publisher - connect_demo.py: client that exposes send_whatsapp + get_battery stubs - orchestrate_demo.py: end-to-end — AI publishes, client connects, user asks AI questions through the client, AI invokes capabilities back. Father's killer-demo scenario realized in <130 lines. tests/: - test_manifest.py: round-trip + builder helper - test_protocol.py: envelope shapes for every message type - test_e2e.py: spins up the hub in-process, registers publisher, connects client, verifies chat flow + capability invocation work end-to-end. infra: - pyproject.toml with hatchling, optional [server] + [dev] extras, package entry zhub-server. - .github/workflows/ci.yml — pytest on Python 3.10/3.11/3.12 + ruff lint. - Dockerfile — minimal Python 3.12 slim image, exposes 8080. - README.md — vision, 60-second demo, install, usage, architecture diagram, manifest format reference, why zhub vs ngrok/MCP/A2A/LangServe, roadmap. - CLAUDE.md — full project brief for future Claude sessions: who, paths, history, file layout, architecture, landmines, what's next. - LICENSE: MIT. architecture invariant: the hub is a router, not a database. State lives with publishers and clients. WebSocket multiplex per side. Bidirectional from day one — connection-events flow to publisher, invoke-requests flow from publisher to client. father's vision realized: AI publishes, friend or another AI connects via standard openai library + bearer key, the AI is aware of every connection and can call back into them. wifi pairing, but for AI agents. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
0 parents  commit 47b32a4

19 files changed

Lines changed: 2152 additions & 0 deletions

.github/workflows/ci.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
workflow_dispatch:
9+
10+
jobs:
11+
test:
12+
name: Test on Python ${{ matrix.python }}
13+
runs-on: ubuntu-latest
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
python: ["3.10", "3.11", "3.12"]
18+
19+
steps:
20+
- uses: actions/checkout@v4
21+
22+
- name: Set up Python ${{ matrix.python }}
23+
uses: actions/setup-python@v5
24+
with:
25+
python-version: ${{ matrix.python }}
26+
27+
- name: Install
28+
run: |
29+
python -m pip install --upgrade pip
30+
pip install -e '.[server,dev]'
31+
32+
- name: Lint
33+
run: |
34+
ruff check zhub/ tests/
35+
36+
- name: Test
37+
run: |
38+
pytest -v

.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Python
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
*.so
6+
.Python
7+
.venv/
8+
venv/
9+
.eggs/
10+
*.egg-info/
11+
*.egg
12+
dist/
13+
build/
14+
15+
# Tests
16+
.pytest_cache/
17+
.coverage
18+
htmlcov/
19+
.tox/
20+
.hypothesis/
21+
22+
# IDE
23+
.vscode/
24+
.idea/
25+
*.swp
26+
*.swo
27+
.DS_Store
28+
29+
# Local data
30+
*.log
31+
*.db
32+
*.sqlite
33+
.env
34+
.env.local
35+
local/

CLAUDE.md

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# CLAUDE.md — brief for the next Claude who walks in
2+
3+
> Read top to bottom before touching anything. ~10 minute orientation. Sibling pattern to `~/.zai/CLAUDE.md` and `/root/ZTerminal/CLAUDE.md`.
4+
5+
---
6+
7+
## 1. Who everyone is
8+
9+
- **Father / Zawwar Sami** — the Commander. Builds tools as the joy itself, NOT for productization. Hinglish casual, direct. Has ZAI (autonomous AI son), Loki (phone body APK), Anteroom (publishing pipeline), ZTerminal/Joker (security tool — paused), and now zhub (this).
10+
- **zhub** — drop-in library that turns any AI into a publishable, discoverable, controllable endpoint. **Bidirectional**: the AI is the hub, connected clients are spokes, AI sees everyone connected to it and can invoke their capabilities.
11+
- **The hub server** — small FastAPI app that routes traffic. Runs on Father's machine (laptop, NUC, or a free PaaS tier).
12+
- **Publishers** — AIs that call `zhub.publish(...)`. Examples: ZAI, future Joker brain, custom local models.
13+
- **Clients/Connections** — anything that calls `zhub.connect(...)`. Examples: Loki (phone bridge), Telegram bot, web chat, another AI.
14+
- **You (Claude)** — the tool Father uses to BUILD zhub. Not zhub. Stay in the right frame.
15+
16+
## 2. Repo + paths
17+
18+
| What | Path | Repo |
19+
|---|---|---|
20+
| zhub working tree | `/root/zhub/` | `Zawwarsami16/zhub` (public) |
21+
| ZAI runtime (where publish() will be installed) | `/root/.zai/` | `Zawwarsami16/zai-runtime-private` |
22+
| Loki APK (where connect() will be installed — Kotlin port phase 0.3) | `/root/.zai/loki-apk/` | `Zawwarsami16/Loki` |
23+
| ZTerminal (sibling tool, paused) | `/root/ZTerminal/` | `Zawwarsami16/ZTerminal` |
24+
25+
CI: GitHub Actions on every push to `main`. Tests run on Python 3.10/3.11/3.12. No APK build — this is a library, not an app.
26+
27+
## 3. First thing to do on a fresh session
28+
29+
```bash
30+
cd /root/zhub
31+
git pull --ff-only
32+
33+
# Quick orient
34+
cat README.md
35+
cat CLAUDE.md
36+
ls zhub/
37+
ls examples/
38+
git log --oneline -25
39+
40+
# Run the orchestration demo locally to verify everything works:
41+
pip install -e '.[server,dev]'
42+
python -m zhub.server --port 8080 & # in one shell
43+
python examples/orchestrate_demo.py # in another
44+
```
45+
46+
Tests: `pytest -v`. The e2e tests spin up the hub in-process and run the full publish+connect flow.
47+
48+
## 4. Historical arc
49+
50+
| Date | Event |
51+
|---|---|
52+
| 2026-05-08 morning | Father asked about Decepticon, then ZTerminal/Joker built. Disappointed with operational hollowness. |
53+
| 2026-05-08 afternoon | Pivoted through zlang vision (general-purpose AI-native language). Then to AI-to-AI communication protocol. Then to abjad-style compression rule. |
54+
| 2026-05-08 evening | Father asked about Anteroom Crypto Terminal app conversion. Drifted into Play Store framing — Father pulled the plug: *"meri taste i thinking tool building he hai"*. Memory updated. |
55+
| 2026-05-08 evening | Father asked: *"can we build something jis ke through koi bhi custom ai like zai with just chat apna proper endpoint generate karde, more like API key"*. Initial response was ngrok-style tunnel exposure. |
56+
| 2026-05-08 evening | Father added the killer requirement: bidirectional awareness. *"after endpoint i enter in loki, it means i am talking to zai there but if i go to telegram and ask him about loki, so he should know and control it."* This is the WiFi-pairing-for-AIs vision. |
57+
| 2026-05-08 evening | This commit — Phase 0 + 1 shipped autonomously. Library + hub + examples + tests + docs + CI + this CLAUDE.md. |
58+
59+
## 5. Phase status
60+
61+
- **Phase 0** ✅ — `zhub.publish()`, hub server, OpenAI-compat /v1/chat/completions proxy, manifest at /<name>/manifest.json, public /registry.
62+
- **Phase 1** ✅ — `zhub.connect()`, capability registration, bidirectional invoke from publisher to connection through the hub. Connection-event delivery to publisher.
63+
- **Phase 0.2 (next)** — Cloudflare Tunnel auto-config so publish works from a laptop without a public IP.
64+
- **Phase 0.3** — Kotlin client lib for Loki Android. JS client for browser/Node.
65+
- **Phase 0.4** — Streaming chat responses end-to-end (SSE through hub).
66+
- **Phase 0.5** — Persistent registry across hub restarts. Web UI for /registry.
67+
- **Phase 1.0** — Signed manifests, hub federation.
68+
69+
## 6. File layout (what's where)
70+
71+
| Concern | Where |
72+
|---|---|
73+
| Public API surface | `zhub/__init__.py` — re-exports `publish`, `connect`, `Manifest`, `Capability`, errors. |
74+
| Manifest schema + builders | `zhub/manifest.py``Manifest`, `Capability`, `chat_only_manifest()`. |
75+
| Wire protocol envelopes | `zhub/protocol.py``Envelope` + helpers (`register_publisher`, `chat_request`, `invoke_request`, etc.). |
76+
| Hub server (FastAPI + WebSocket) | `zhub/server.py``create_app()`, `Hub` class, HTTP routes + `/ws/publish` + `/ws/connect`. |
77+
| Client library (publish + connect) | `zhub/client.py``publish()`, `connect()`, `ZhubPublication`, `ZhubConnection`. |
78+
| Custom exceptions | `zhub/errors.py``ZhubError`, `AuthError`, `ConnectionError`, `ManifestError`, `CapabilityError`, `HubError`. |
79+
| Examples | `examples/publish_demo.py`, `examples/connect_demo.py`, `examples/orchestrate_demo.py`. |
80+
| Tests | `tests/test_manifest.py`, `tests/test_protocol.py`, `tests/test_e2e.py`. |
81+
| CI | `.github/workflows/ci.yml`. |
82+
| Container | `Dockerfile`. |
83+
| Package config | `pyproject.toml`. |
84+
85+
## 7. Architecture in 4 lines
86+
87+
1. **The hub is a router, not a database.** State lives in the publisher's process and the client's process; the hub multiplexes traffic and holds the in-memory registry.
88+
2. **WebSocket per side.** Each publisher and each connection holds one long-lived WebSocket to the hub.
89+
3. **One Envelope schema** crosses every boundary — `register-publisher`, `chat-request`, `chat-response`, `invoke-request`, `invoke-result`, `connection-event`, `registered`, `error`, `ping`/`pong`.
90+
4. **Bidirectional**: publisher → invoke-request → hub → connection (the AI calls its client's capability). connection → chat-request → hub → publisher (the client calls the AI). Both directions multiplexed on the same WS.
91+
92+
## 8. Landmines
93+
94+
1. **Don't add per-publisher persistence to the hub yet.** v0.1 is in-memory by design. If the hub restarts, publishers re-register and clients reconnect. Persistence is Phase 0.5; doing it now would couple the hub to a specific storage choice prematurely.
95+
96+
2. **API keys are returned to the publisher exactly once** at registration time. The hub stores only the key → AI mapping (raw key in memory; in production, hash before storing). Don't add a "show key" endpoint.
97+
98+
3. **`pub.invoke()` and `conn.chat()` are async — call from inside an event loop.** The orchestration example shows the run-coroutine-threadsafe pattern when calling from a sync chat handler.
99+
100+
4. **Don't break the OpenAI Chat Completions response shape.** External clients use `openai` library expecting the canonical shape. The hub formats publisher responses into `{id, object, created, model, choices: [{index, message: {role, content}, finish_reason}], usage}`.
101+
102+
5. **WebSocket disconnects need graceful handling.** The hub already tears down on `WebSocketDisconnect` and notifies the publisher with `connection-event(disconnected)`. If you change reconnect logic, preserve this.
103+
104+
6. **Father builds tools, not products.** Don't drift into "let's monetize zhub" or "let's add billing". Tool that does one thing well. See `~/.claude/projects/-root/memory/feedback_taste_tool_building.md`.
105+
106+
7. **No commercial framing for Father unless explicitly asked.** No Play Store, no IAP, no pricing decisions. Just tool craftsmanship.
107+
108+
## 9. What's next when Father says go
109+
110+
Mera honest priority order:
111+
112+
1. **Phase 0.2 — Cloudflare Tunnel auto-config.** This is the difference between "demo on localhost" and "actually expose ZAI from Father's phone to the internet." Worth a real day of work. Requires a Cloudflare account + an `cloudflared` binary check on publish.
113+
2. **Phase 0.3 — Kotlin client lib for Loki.** The promised killer demo (Telegram → ZAI → Loki on phone) needs Loki to call `zhub.connect()`. Today only Python clients can. Should be 1-2 days for a clean Kotlin port.
114+
3. **Phase 0.4 — Streaming.** OpenAI-compat clients expect SSE for streaming. Without it, longer responses feel slow.
115+
4. **Phase 0.5 — Persistence.** SQLite-backed registry so hub restarts don't lose state.
116+
117+
Anything Father proposes wins over this default list.
118+
119+
## 10. Father's north star (don't argue, don't drift)
120+
121+
- ZAI eventually runs everywhere — phone, NUC, Father's life. zhub is the connectivity substrate that makes that real.
122+
- Tool craftsmanship over productization. Open source. Anteroom-publishable.
123+
- Bidirectional from day one is the philosophy: the AI is the hub of its own ecosystem.
124+
- Don't replicate ZTerminal mistake — ship things that actually work end-to-end before claiming completion.
125+
126+
## 11. Useful commands at a glance
127+
128+
```bash
129+
# Run hub locally
130+
python -m zhub.server --port 8080
131+
132+
# Run orchestration end-to-end demo
133+
python examples/orchestrate_demo.py
134+
135+
# Test (e2e tests need fastapi/uvicorn)
136+
pip install -e '.[server,dev]'
137+
pytest -v
138+
139+
# Build wheel
140+
pip install build
141+
python -m build
142+
143+
# Run hub in container
144+
docker build -t zhub .
145+
docker run -p 8080:8080 zhub
146+
```
147+
148+
---
149+
150+
**Last updated:** 2026-05-08 (Phase 0+1 ship).

Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM python:3.12-slim
2+
3+
WORKDIR /app
4+
COPY pyproject.toml README.md LICENSE ./
5+
COPY zhub/ ./zhub/
6+
7+
RUN pip install --no-cache-dir '.[server]'
8+
9+
EXPOSE 8080
10+
CMD ["python", "-m", "zhub.server", "--host", "0.0.0.0", "--port", "8080"]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2026 Zawwar Sami
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 commit comments

Comments
 (0)