pi-gateway 0.1.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,27 @@
1
+ name: Publish Python package
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+
7
+ permissions:
8
+ contents: read
9
+ id-token: write
10
+
11
+ jobs:
12
+ publish:
13
+ name: Publish to PyPI
14
+ runs-on: ubuntu-latest
15
+ environment: pypi
16
+ steps:
17
+ - name: Check out repository
18
+ uses: actions/checkout@v4
19
+
20
+ - name: Set up uv
21
+ uses: astral-sh/setup-uv@v5
22
+
23
+ - name: Build package
24
+ run: uv build
25
+
26
+ - name: Publish package
27
+ run: uv publish
@@ -0,0 +1,16 @@
1
+ # Python-generated files
2
+ __pycache__/
3
+ *.py[oc]
4
+ build/
5
+ dist/
6
+ wheels/
7
+ *.egg-info
8
+
9
+ # Virtual environments
10
+ .venv
11
+
12
+ # Runtime state
13
+ *.sqlite3
14
+ *.sqlite3-*
15
+ *.log
16
+ /config.yaml
@@ -0,0 +1 @@
1
+ 3.14
@@ -0,0 +1,147 @@
1
+ # AGENTS.md
2
+
3
+ Guidance for AI agents working on this repository.
4
+
5
+ ## Project Overview
6
+
7
+ Pi Gateway is a Python CLI/daemon that connects Telegram to persistent Pi coding-agent sessions.
8
+
9
+ Core idea:
10
+
11
+ ```text
12
+ Telegram conversation identity -> SQLite mapping -> Pi JSONL session file -> pi --mode rpc
13
+ ```
14
+
15
+ Pi owns conversation history. The gateway owns Telegram routing, authorization, process management, and session metadata.
16
+
17
+ ## Read These First
18
+
19
+ Before making changes, read:
20
+
21
+ 1. [`README.md`](README.md) - user-facing install/run instructions
22
+ 2. [`docs/README.md`](docs/README.md) - documentation index
23
+ 3. [`docs/01-architecture-overview.md`](docs/01-architecture-overview.md) - architecture and design constraints
24
+ 4. The specific doc for the area you are modifying:
25
+ - CLI/startup: [`docs/02-startup-and-cli-flow.md`](docs/02-startup-and-cli-flow.md)
26
+ - Telegram: [`docs/03-telegram-gateway.md`](docs/03-telegram-gateway.md)
27
+ - Pi RPC: [`docs/04-pi-rpc-integration.md`](docs/04-pi-rpc-integration.md)
28
+ - SQLite/session mapping: [`docs/05-session-mapping-and-sqlite.md`](docs/05-session-mapping-and-sqlite.md)
29
+ - deployment/config: [`docs/06-configuration-and-deployment.md`](docs/06-configuration-and-deployment.md)
30
+ - debugging: [`docs/07-troubleshooting.md`](docs/07-troubleshooting.md)
31
+
32
+ ## Important Commands
33
+
34
+ Syntax check:
35
+
36
+ ```bash
37
+ python3 -m compileall pi_gateway main.py
38
+ ```
39
+
40
+ CLI smoke tests:
41
+
42
+ ```bash
43
+ python3 main.py --help
44
+ python3 main.py configure --help
45
+ python3 main.py configure telegram --help
46
+ python3 main.py status
47
+ ```
48
+
49
+ uv development:
50
+
51
+ ```bash
52
+ uv sync
53
+ uv run pi-gateway --help
54
+ ```
55
+
56
+ Install/update as uv tool from checkout:
57
+
58
+ ```bash
59
+ uv tool install --force .
60
+ ```
61
+
62
+ ## Runtime Commands
63
+
64
+ Foreground daemon:
65
+
66
+ ```bash
67
+ pi-gateway run
68
+ ```
69
+
70
+ Background daemon:
71
+
72
+ ```bash
73
+ pi-gateway start
74
+ pi-gateway status
75
+ pi-gateway logs -f
76
+ pi-gateway stop
77
+ ```
78
+
79
+ Interactive Telegram configuration:
80
+
81
+ ```bash
82
+ pi-gateway configure telegram
83
+ ```
84
+
85
+ ## Repository Structure
86
+
87
+ ```text
88
+ pi_gateway/
89
+ ├── cli.py # CLI, config wizard, foreground/background process commands
90
+ ├── config.py # YAML/env config loader and dataclasses
91
+ ├── db.py # SQLite schema and gateway persistence
92
+ ├── pi_rpc.py # JSONL RPC subprocess client for `pi --mode rpc`
93
+ ├── session_manager.py # per-conversation Pi client cache/locks
94
+ └── telegram_bot.py # Telegram adapter, auth, commands, lifecycle notifications
95
+ ```
96
+
97
+ ## Design Rules
98
+
99
+ - Do not duplicate Pi conversation history in SQLite.
100
+ - Store gateway metadata in SQLite: Telegram identity, Pi session file/id/name, audit messages.
101
+ - Prefer `pi_session_file` over only `pi_session_id` when resuming sessions.
102
+ - Keep Telegram user allowlisting secure by default.
103
+ - Group chats should remain disabled by default.
104
+ - Keep `pi-gateway run` as foreground mode; `start/stop/status/logs` are convenience wrappers.
105
+ - For production VPS deployment, continue to recommend systemd.
106
+ - If changing behavior, update `README.md` and relevant files in `docs/`.
107
+
108
+ ## Security Notes
109
+
110
+ This gateway can expose a coding agent with filesystem and shell tools. Be careful.
111
+
112
+ - Maintain `telegram.allowedUserIds` checks.
113
+ - Do not add broad unauthenticated webhooks or APIs.
114
+ - Do not log secrets such as Telegram bot tokens.
115
+ - If adding new platforms, implement explicit allowlists.
116
+ - If adding group support, consider session-key and authorization implications.
117
+
118
+ ## Pi Integration Notes
119
+
120
+ The gateway uses Pi RPC, not the Pi SDK.
121
+
122
+ Important Pi RPC assumptions:
123
+
124
+ - Start command is `pi --mode rpc`.
125
+ - Existing sessions can resume with `--session <session-file>`.
126
+ - JSONL records are newline-delimited.
127
+ - Prompt completion is detected by consuming events until `agent_end`.
128
+
129
+ If Pi RPC protocol changes, update:
130
+
131
+ - `pi_gateway/pi_rpc.py`
132
+ - [`docs/04-pi-rpc-integration.md`](docs/04-pi-rpc-integration.md)
133
+
134
+ ## Git Hygiene
135
+
136
+ - Run syntax checks before committing.
137
+ - Keep commits focused and descriptive.
138
+ - Do not commit local `config.yaml`, SQLite DBs, logs, PID files, virtualenvs, or `__pycache__`.
139
+ - `.gitignore` already excludes common runtime state.
140
+
141
+ ## Documentation Requirement
142
+
143
+ When changing code, update relevant docs:
144
+
145
+ - User-facing behavior: `README.md`
146
+ - Architecture or internals: `docs/*.md`
147
+ - Agent handoff instructions: this `AGENTS.md`
@@ -0,0 +1,180 @@
1
+ Metadata-Version: 2.4
2
+ Name: pi-gateway
3
+ Version: 0.1.0
4
+ Summary: Telegram gateway for controlling persistent Pi coding-agent sessions over RPC
5
+ Requires-Python: >=3.11
6
+ Requires-Dist: python-telegram-bot<23.0,>=21.0
7
+ Requires-Dist: pyyaml<7.0,>=6.0.1
8
+ Description-Content-Type: text/markdown
9
+
10
+ # pi-gateway
11
+
12
+ Telegram gateway for persistent [Pi](https://pi.dev) coding-agent sessions.
13
+
14
+ The gateway is a long-running process. Telegram conversations are mapped to Pi JSONL session files in SQLite, while Pi remains the source of truth for agent history.
15
+
16
+ ## Documentation
17
+
18
+ See [`docs/`](docs/README.md) for architecture, startup flow, Telegram gateway internals, Pi RPC integration, session mapping, deployment, and troubleshooting notes.
19
+
20
+ ## Install with uv
21
+
22
+ Directly from GitHub (no clone needed):
23
+
24
+ ```bash
25
+ uv tool install git+https://github.com/YOUR_USERNAME/pi-gateway.git
26
+ ```
27
+
28
+ Install a specific tag or branch:
29
+
30
+ ```bash
31
+ uv tool install git+https://github.com/YOUR_USERNAME/pi-gateway.git@v0.1.0
32
+ ```
33
+
34
+ Upgrade later:
35
+
36
+ ```bash
37
+ uv tool install --force git+https://github.com/YOUR_USERNAME/pi-gateway.git
38
+ # or
39
+ uv tool upgrade pi-gateway
40
+ ```
41
+
42
+ From a local checkout:
43
+
44
+ ```bash
45
+ uv tool install .
46
+ ```
47
+
48
+ Or for development:
49
+
50
+ ```bash
51
+ uv sync
52
+ uv run pi-gateway --help
53
+ ```
54
+
55
+ Pi must already be installed and authenticated on the machine as the same user that runs the gateway.
56
+
57
+ ## Configure Telegram
58
+
59
+ Create/update the default config at `~/.config/pi-gateway/config.yaml` interactively:
60
+
61
+ ```bash
62
+ pi-gateway configure telegram
63
+ ```
64
+
65
+ It will ask for your BotFather token, your allowed Telegram user id, and the Pi working directory.
66
+
67
+ You can also configure non-interactively:
68
+
69
+ ```bash
70
+ pi-gateway configure telegram \
71
+ --allowed-user-id YOUR_TELEGRAM_USER_ID \
72
+ --pi-cwd /home/agent/pi-workspace
73
+ ```
74
+
75
+ By default the bot token can be read from `TELEGRAM_BOT_TOKEN`. You can also write it into the config:
76
+
77
+ ```bash
78
+ pi-gateway configure telegram \
79
+ --bot-token '123:abc' \
80
+ --allowed-user-id YOUR_TELEGRAM_USER_ID \
81
+ --pi-cwd /home/agent/pi-workspace
82
+ ```
83
+
84
+ Security note: `--allowed-user-id` writes a single allowlisted Telegram user id. Messages from other users are ignored. Group chats are disabled unless you pass `--allow-groups`.
85
+
86
+ Print the default config path:
87
+
88
+ ```bash
89
+ pi-gateway config-path
90
+ ```
91
+
92
+ You can still maintain config manually; see `examples/config.yaml`.
93
+
94
+ ## Run
95
+
96
+ Foreground mode, useful for debugging or systemd:
97
+
98
+ ```bash
99
+ export TELEGRAM_BOT_TOKEN=123:abc
100
+ pi-gateway run
101
+ ```
102
+
103
+ Background mode, useful for a simple VPS setup without systemd:
104
+
105
+ ```bash
106
+ pi-gateway start
107
+ pi-gateway status
108
+ pi-gateway logs -f
109
+ pi-gateway stop
110
+ ```
111
+
112
+ `start` writes logs to:
113
+
114
+ ```text
115
+ ~/.local/state/pi-gateway/pi-gateway.log
116
+ ```
117
+
118
+ With an explicit config:
119
+
120
+ ```bash
121
+ pi-gateway -c config.yaml start
122
+ pi-gateway -c config.yaml run
123
+ # or
124
+ pi-gateway run -c config.yaml
125
+ ```
126
+
127
+ Development checkout:
128
+
129
+ ```bash
130
+ uv run pi-gateway run
131
+ ```
132
+
133
+ ## Telegram commands
134
+
135
+ - `/status` current Pi session/model/stats
136
+ - `/new` fresh Pi session for this Telegram chat
137
+ - `/name <name>` name current Pi session
138
+ - `/compact [instructions]` compact current Pi context
139
+ - `/stop` abort current Pi operation
140
+ - `/last` resend last assistant response
141
+ - `/export` export current session to HTML
142
+ - `/sessions` list known sessions
143
+ - `/switch <id>` point this chat at another known Pi session
144
+ - `/clone` clone current branch into a new session
145
+ - `/models` list available models
146
+ - `/model <provider/model-id>` switch model
147
+ - `/thinking <level>` set thinking level
148
+ - `/queue <text>` queue follow-up
149
+ - `/steer <text>` steer current/next turn
150
+ - `/pi <text>` send raw text to Pi, including Pi slash commands
151
+
152
+ Normal Telegram messages are sent to Pi as prompts.
153
+
154
+ ## Session mapping
155
+
156
+ Gateway key:
157
+
158
+ ```text
159
+ telegram:<chat_id>:<thread_id?>:<user_id?>
160
+ ```
161
+
162
+ SQLite stores that key plus Pi's `sessionId` and `sessionFile`. On restart the gateway resumes with:
163
+
164
+ ```bash
165
+ pi --mode rpc --session <stored-session-file>
166
+ ```
167
+
168
+ ## systemd
169
+
170
+ See `systemd/pi-gateway.service` and adjust paths/user/env.
171
+
172
+ Example with uv tool install:
173
+
174
+ ```ini
175
+ [Service]
176
+ User=agent
177
+ Environment=TELEGRAM_BOT_TOKEN=123:abc
178
+ ExecStart=/home/agent/.local/bin/pi-gateway run
179
+ Restart=always
180
+ ```
@@ -0,0 +1,171 @@
1
+ # pi-gateway
2
+
3
+ Telegram gateway for persistent [Pi](https://pi.dev) coding-agent sessions.
4
+
5
+ The gateway is a long-running process. Telegram conversations are mapped to Pi JSONL session files in SQLite, while Pi remains the source of truth for agent history.
6
+
7
+ ## Documentation
8
+
9
+ See [`docs/`](docs/README.md) for architecture, startup flow, Telegram gateway internals, Pi RPC integration, session mapping, deployment, and troubleshooting notes.
10
+
11
+ ## Install with uv
12
+
13
+ Directly from GitHub (no clone needed):
14
+
15
+ ```bash
16
+ uv tool install git+https://github.com/YOUR_USERNAME/pi-gateway.git
17
+ ```
18
+
19
+ Install a specific tag or branch:
20
+
21
+ ```bash
22
+ uv tool install git+https://github.com/YOUR_USERNAME/pi-gateway.git@v0.1.0
23
+ ```
24
+
25
+ Upgrade later:
26
+
27
+ ```bash
28
+ uv tool install --force git+https://github.com/YOUR_USERNAME/pi-gateway.git
29
+ # or
30
+ uv tool upgrade pi-gateway
31
+ ```
32
+
33
+ From a local checkout:
34
+
35
+ ```bash
36
+ uv tool install .
37
+ ```
38
+
39
+ Or for development:
40
+
41
+ ```bash
42
+ uv sync
43
+ uv run pi-gateway --help
44
+ ```
45
+
46
+ Pi must already be installed and authenticated on the machine as the same user that runs the gateway.
47
+
48
+ ## Configure Telegram
49
+
50
+ Create/update the default config at `~/.config/pi-gateway/config.yaml` interactively:
51
+
52
+ ```bash
53
+ pi-gateway configure telegram
54
+ ```
55
+
56
+ It will ask for your BotFather token, your allowed Telegram user id, and the Pi working directory.
57
+
58
+ You can also configure non-interactively:
59
+
60
+ ```bash
61
+ pi-gateway configure telegram \
62
+ --allowed-user-id YOUR_TELEGRAM_USER_ID \
63
+ --pi-cwd /home/agent/pi-workspace
64
+ ```
65
+
66
+ By default the bot token can be read from `TELEGRAM_BOT_TOKEN`. You can also write it into the config:
67
+
68
+ ```bash
69
+ pi-gateway configure telegram \
70
+ --bot-token '123:abc' \
71
+ --allowed-user-id YOUR_TELEGRAM_USER_ID \
72
+ --pi-cwd /home/agent/pi-workspace
73
+ ```
74
+
75
+ Security note: `--allowed-user-id` writes a single allowlisted Telegram user id. Messages from other users are ignored. Group chats are disabled unless you pass `--allow-groups`.
76
+
77
+ Print the default config path:
78
+
79
+ ```bash
80
+ pi-gateway config-path
81
+ ```
82
+
83
+ You can still maintain config manually; see `examples/config.yaml`.
84
+
85
+ ## Run
86
+
87
+ Foreground mode, useful for debugging or systemd:
88
+
89
+ ```bash
90
+ export TELEGRAM_BOT_TOKEN=123:abc
91
+ pi-gateway run
92
+ ```
93
+
94
+ Background mode, useful for a simple VPS setup without systemd:
95
+
96
+ ```bash
97
+ pi-gateway start
98
+ pi-gateway status
99
+ pi-gateway logs -f
100
+ pi-gateway stop
101
+ ```
102
+
103
+ `start` writes logs to:
104
+
105
+ ```text
106
+ ~/.local/state/pi-gateway/pi-gateway.log
107
+ ```
108
+
109
+ With an explicit config:
110
+
111
+ ```bash
112
+ pi-gateway -c config.yaml start
113
+ pi-gateway -c config.yaml run
114
+ # or
115
+ pi-gateway run -c config.yaml
116
+ ```
117
+
118
+ Development checkout:
119
+
120
+ ```bash
121
+ uv run pi-gateway run
122
+ ```
123
+
124
+ ## Telegram commands
125
+
126
+ - `/status` current Pi session/model/stats
127
+ - `/new` fresh Pi session for this Telegram chat
128
+ - `/name <name>` name current Pi session
129
+ - `/compact [instructions]` compact current Pi context
130
+ - `/stop` abort current Pi operation
131
+ - `/last` resend last assistant response
132
+ - `/export` export current session to HTML
133
+ - `/sessions` list known sessions
134
+ - `/switch <id>` point this chat at another known Pi session
135
+ - `/clone` clone current branch into a new session
136
+ - `/models` list available models
137
+ - `/model <provider/model-id>` switch model
138
+ - `/thinking <level>` set thinking level
139
+ - `/queue <text>` queue follow-up
140
+ - `/steer <text>` steer current/next turn
141
+ - `/pi <text>` send raw text to Pi, including Pi slash commands
142
+
143
+ Normal Telegram messages are sent to Pi as prompts.
144
+
145
+ ## Session mapping
146
+
147
+ Gateway key:
148
+
149
+ ```text
150
+ telegram:<chat_id>:<thread_id?>:<user_id?>
151
+ ```
152
+
153
+ SQLite stores that key plus Pi's `sessionId` and `sessionFile`. On restart the gateway resumes with:
154
+
155
+ ```bash
156
+ pi --mode rpc --session <stored-session-file>
157
+ ```
158
+
159
+ ## systemd
160
+
161
+ See `systemd/pi-gateway.service` and adjust paths/user/env.
162
+
163
+ Example with uv tool install:
164
+
165
+ ```ini
166
+ [Service]
167
+ User=agent
168
+ Environment=TELEGRAM_BOT_TOKEN=123:abc
169
+ ExecStart=/home/agent/.local/bin/pi-gateway run
170
+ Restart=always
171
+ ```
@@ -0,0 +1,104 @@
1
+ # Architecture Overview
2
+
3
+ Pi Gateway is a personal gateway daemon that lets a Telegram user communicate with Pi from a VPS or always-on machine.
4
+
5
+ The central design decision is:
6
+
7
+ > The gateway owns platform routing and metadata. Pi owns the agent conversation history.
8
+
9
+ Pi sessions are still normal Pi JSONL sessions. The gateway stores only enough SQLite metadata to reconnect a Telegram conversation to the right Pi session file.
10
+
11
+ ## Main Components
12
+
13
+ ```text
14
+ pi-gateway CLI
15
+ ├── configure telegram
16
+ ├── run / start / stop / status / logs
17
+ │
18
+ ▼
19
+ Gateway runtime
20
+ ├── Config loader
21
+ ├── SQLite GatewayDB
22
+ ├── TelegramGateway adapter
23
+ └── PiSessionManager
24
+ └── PiRpcClient subprocesses
25
+ └── pi --mode rpc
26
+ ```
27
+
28
+ ## Runtime Flow
29
+
30
+ ```text
31
+ Telegram message arrives
32
+ ↓
33
+ TelegramGateway authorizes sender
34
+ ↓
35
+ Build gateway session key
36
+ ↓
37
+ GatewayDB finds/creates conversation row
38
+ ↓
39
+ Command router handles gateway commands, or forwards normal text
40
+ ↓
41
+ PiSessionManager gets per-conversation PiRpcClient
42
+ ↓
43
+ PiRpcClient sends JSONL command to `pi --mode rpc`
44
+ ↓
45
+ Assistant response is extracted from Pi RPC events
46
+ ↓
47
+ TelegramGateway sends response back to Telegram
48
+ ```
49
+
50
+ ## Directory Structure
51
+
52
+ ```text
53
+ pi-gateway/
54
+ ├── pi_gateway/
55
+ │ ├── cli.py # command-line interface and daemon startup
56
+ │ ├── config.py # config dataclasses and YAML/env loading
57
+ │ ├── db.py # SQLite schema and database access
58
+ │ ├── pi_rpc.py # Pi RPC subprocess client
59
+ │ ├── session_manager.py # per-conversation Pi client cache and locks
60
+ │ └── telegram_bot.py # Telegram adapter and command router
61
+ ├── examples/config.yaml
62
+ ├── systemd/pi-gateway.service
63
+ ├── docs/
64
+ └── pyproject.toml
65
+ ```
66
+
67
+ ## Why RPC Instead of SDK?
68
+
69
+ The gateway uses Pi RPC mode instead of importing the Pi SDK directly.
70
+
71
+ Reasons:
72
+
73
+ 1. **Process isolation**: Pi runs as a child process. Gateway and Pi failures are more isolated.
74
+ 2. **Stable boundary**: The gateway talks to the documented JSONL RPC protocol.
75
+ 3. **VPS-friendly recovery**: On restart, the gateway can spawn `pi --mode rpc --session <file>`.
76
+ 4. **Hermes-like architecture**: Messaging gateway and agent runtime are cleanly separated.
77
+
78
+ The tradeoff is that we must manage subprocesses, JSONL framing, and stdout/stderr readers.
79
+
80
+ ## State Ownership
81
+
82
+ | State | Owner | Storage |
83
+ |-------|-------|---------|
84
+ | Pi conversation history | Pi | JSONL session files |
85
+ | Active branch/session tree | Pi | JSONL session files |
86
+ | Telegram to Pi mapping | Gateway | SQLite |
87
+ | Inbound/outbound audit log | Gateway | SQLite |
88
+ | Running child processes | Gateway | In memory + PID file for background daemon |
89
+
90
+ ## Design Constraints
91
+
92
+ - One Telegram conversation should map to one Pi session file.
93
+ - Normal text goes to Pi as a prompt.
94
+ - Gateway slash commands are handled before Pi sees the message.
95
+ - Pi slash commands can still be sent with `/pi <text>`.
96
+ - Messages from non-allowlisted Telegram users are ignored.
97
+ - Pi runs from a configured working directory so session storage and filesystem tools are predictable.
98
+
99
+ ## Related Documents
100
+
101
+ - [Startup and CLI Flow](02-startup-and-cli-flow.md)
102
+ - [Telegram Gateway](03-telegram-gateway.md)
103
+ - [Pi RPC Integration](04-pi-rpc-integration.md)
104
+ - [Session Mapping and SQLite](05-session-mapping-and-sqlite.md)