tg-gemini 0.0.5__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,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 A.J.Zeller
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,0 +1,270 @@
1
+ Metadata-Version: 2.3
2
+ Name: tg-gemini
3
+ Version: 0.0.5
4
+ Summary: Telegram bot middleware bridging Gemini CLI headless mode
5
+ Author: A.J.Zeller
6
+ Author-email: A.J.Zeller <hello@atticux.me>
7
+ License: MIT License
8
+
9
+ Copyright (c) 2024 A.J.Zeller
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ SOFTWARE.
28
+ Classifier: License :: OSI Approved :: MIT License
29
+ Classifier: Programming Language :: Python
30
+ Classifier: Programming Language :: Python :: 3 :: Only
31
+ Classifier: Programming Language :: Python :: 3.11
32
+ Classifier: Programming Language :: Python :: 3.12
33
+ Classifier: Programming Language :: Python :: 3.13
34
+ Classifier: Programming Language :: Python :: 3.14
35
+ Requires-Dist: python-telegram-bot>=21.0
36
+ Requires-Dist: pydantic>=2.0
37
+ Requires-Dist: typer>=0.12
38
+ Requires-Dist: loguru>=0.7
39
+ Maintainer: A.J.Zeller
40
+ Maintainer-email: A.J.Zeller <hello@atticux.me>
41
+ Requires-Python: >=3.12
42
+ Project-URL: Changelog, https://github.com/atticuszeller/tg-gemini/blob/main/CHANGELOG.md
43
+ Project-URL: Homepage, https://github.com/atticuszeller/tg-gemini
44
+ Project-URL: Issues, https://github.com/atticuszeller/tg-gemini/issues
45
+ Project-URL: documentation, https://atticuszeller.github.io/tg-gemini/
46
+ Description-Content-Type: text/markdown
47
+
48
+ # tg-gemini
49
+
50
+ <div align="center">
51
+
52
+ [![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json)](https://github.com/astral-sh/uv)
53
+ [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
54
+ [![Python](https://img.shields.io/badge/python-3.12%2B-blue.svg)](https://github.com/atticuszeller/tg-gemini)
55
+ [![Test](https://github.com/atticuszeller/tg-gemini/actions/workflows/main.yml/badge.svg)](https://github.com/atticuszeller/tg-gemini/actions/workflows/main.yml)
56
+
57
+ **Telegram ↔ Gemini CLI 中间件**
58
+
59
+ 在 VPS 上持续运行,将 Telegram 消息转发给 Gemini CLI,实时流式返回响应。
60
+
61
+ </div>
62
+
63
+ ---
64
+
65
+ ```
66
+ 手机 Telegram ──→ tg-gemini (VPS) ──→ gemini CLI (本地)
67
+ ↑ │
68
+ └──────────── 流式响应 ←───────────────────┘
69
+ ```
70
+
71
+ ## 前置条件
72
+
73
+ - **Python 3.12+** 和 **[uv](https://docs.astral.sh/uv/)**
74
+ - **Gemini CLI** 已安装并完成登录:
75
+ ```bash
76
+ npm install -g @google/gemini-cli
77
+ gemini # 首次运行完成认证
78
+ ```
79
+ - **Telegram Bot Token**:通过 [@BotFather](https://t.me/botfather) 创建
80
+
81
+ ## 安装
82
+
83
+ ```bash
84
+ # pip
85
+ pip install tg-gemini
86
+
87
+ # uv(推荐,全局工具安装)
88
+ uv tool install tg-gemini
89
+
90
+ # 源码开发模式
91
+ git clone https://github.com/atticuszeller/tg-gemini
92
+ cd tg-gemini && uv sync --all-groups
93
+ ```
94
+
95
+ ## 快速开始
96
+
97
+ ```bash
98
+ # 1. 创建配置目录
99
+ mkdir -p ~/.tg-gemini
100
+
101
+ # 2. 复制并编辑配置(详细注释见 config.example.toml)
102
+ cp config.example.toml ~/.tg-gemini/config.toml
103
+
104
+ # 编辑:至少填写 telegram.token
105
+ # [telegram]
106
+ # token = "123456:ABC-DEF..."
107
+
108
+ # 3. 启动
109
+ tg-gemini start
110
+
111
+ # 或指定配置文件
112
+ tg-gemini start --config /path/to/config.toml
113
+ ```
114
+
115
+ **Ctrl+C** 优雅停止。
116
+
117
+ ## 配置
118
+
119
+ 配置文件为 TOML 格式,路径解析顺序:
120
+
121
+ 1. `--config` 参数
122
+ 2. 当前目录 `config.toml`
123
+ 3. `~/.tg-gemini/config.toml`(默认)
124
+
125
+ 最小配置:
126
+
127
+ ```toml
128
+ [telegram]
129
+ token = "YOUR_BOT_TOKEN"
130
+ ```
131
+
132
+ 完整选项见 [`config.example.toml`](config.example.toml) 和 [docs/configuration.md](docs/configuration.md)。
133
+
134
+ ### 常用配置片段
135
+
136
+ ```toml
137
+ # 限制只有自己能用(向 @userinfobot 查询 ID)
138
+ [telegram]
139
+ token = "..."
140
+ allow_from = "你的用户ID"
141
+
142
+ # 设置工作目录和模式
143
+ [gemini]
144
+ work_dir = "/path/to/your/project"
145
+ mode = "yolo" # 全自动,推荐服务端使用
146
+ ```
147
+
148
+ ## Bot 命令
149
+
150
+ | 命令 | 功能 |
151
+ |------|------|
152
+ | `/new` | 开启新 Gemini 会话 |
153
+ | `/list` | 查看所有会话 |
154
+ | `/switch <目标>` | 切换会话 |
155
+ | `/name <名称>` | 重命名会话 |
156
+ | `/delete` | 删除会话 |
157
+ | `/history` | 查看对话历史 |
158
+ | `/status` | 当前状态信息 |
159
+ | `/model [名称]` | 查看/切换模型 |
160
+ | `/mode [模式]` | 查看/切换工具模式 |
161
+ | `/lang [en\|zh]` | 切换界面语言 |
162
+ | `/quiet` | 切换静音(隐藏工具通知)|
163
+ | `/stop` | 终止当前 Agent |
164
+ | `/help` | 帮助 |
165
+
166
+ 完整说明见 [docs/commands.md](docs/commands.md)。
167
+
168
+ ## 群聊支持
169
+
170
+ ```toml
171
+ [telegram]
172
+ token = "..."
173
+ group_reply_all = false # 仅响应 @bot 或回复 bot(默认)
174
+ share_session_in_channel = false # 每人独立会话(默认)
175
+ ```
176
+
177
+ 默认行为:只有 @机器人、回复机器人的消息、以及 `/command` 会被处理。
178
+
179
+ ## 作为服务运行
180
+
181
+ 创建 `/etc/systemd/system/tg-gemini.service`:
182
+
183
+ ```ini
184
+ [Unit]
185
+ Description=tg-gemini Telegram Bot
186
+ After=network.target
187
+
188
+ [Service]
189
+ Type=simple
190
+ User=ubuntu
191
+ ExecStart=/home/ubuntu/.local/bin/tg-gemini start
192
+ Restart=on-failure
193
+ RestartSec=10
194
+
195
+ [Install]
196
+ WantedBy=multi-user.target
197
+ ```
198
+
199
+ ```bash
200
+ sudo systemctl daemon-reload
201
+ sudo systemctl enable --now tg-gemini
202
+ sudo journalctl -u tg-gemini -f
203
+ ```
204
+
205
+ ## 架构
206
+
207
+ tg-gemini 由 7 个核心模块组成:
208
+
209
+ ```
210
+ cli.py → engine.py → gemini.py 子进程 + JSONL 解析
211
+
212
+ telegram_platform.py 长轮询 + 消息收发
213
+ streaming.py 流式预览节流
214
+ session.py 多会话 + 历史持久化
215
+ card.py 卡片 UI(InlineKeyboard)
216
+ ```
217
+
218
+ 详细架构图和数据流见 [docs/architecture.md](docs/architecture.md)。
219
+
220
+ ## 常见问题
221
+
222
+ **Q: Gemini CLI 没有响应**
223
+
224
+ ```bash
225
+ gemini -p "hello" --output-format stream-json # 测试 CLI 是否正常
226
+ ```
227
+
228
+ **Q: 收不到 Telegram 消息**
229
+
230
+ 检查 `allow_from` 配置,查看日志:`tg-gemini start` 或 `journalctl -u tg-gemini -f`。
231
+
232
+ **Q: 流式预览触发限流**
233
+
234
+ 增大 `interval_ms`:
235
+ ```toml
236
+ [stream_preview]
237
+ interval_ms = 3000
238
+ ```
239
+
240
+ **Q: 如何让 Gemini 记住上下文**
241
+
242
+ 不要发 `/new`。服务重启后自动恢复到最近一次会话(通过 `--resume` 机制)。
243
+
244
+ ## 文档
245
+
246
+ | 文档 | 内容 |
247
+ |------|------|
248
+ | [docs/configuration.md](docs/configuration.md) | 完整配置项参考表 |
249
+ | [docs/commands.md](docs/commands.md) | 所有 Bot 命令详细说明 |
250
+ | [docs/architecture.md](docs/architecture.md) | 组件架构、数据流、设计决策 |
251
+ | [docs/development.md](docs/development.md) | 开发环境、测试、发布流程 |
252
+ | [docs/internals.md](docs/internals.md) | stream-json 协议、Markdown 转换、流式预览实现 |
253
+ | [config.example.toml](config.example.toml) | 带注释的完整配置示例 |
254
+
255
+ ## 开发
256
+
257
+ ```bash
258
+ git clone https://github.com/atticuszeller/tg-gemini
259
+ cd tg-gemini && uv sync --all-groups
260
+
261
+ bash dev.sh check # format → lint → test → pre-commit
262
+ bash dev.sh test # 运行测试(662 个,98.95% 覆盖率)
263
+ bash dev.sh lint # ty + ruff
264
+ ```
265
+
266
+ 详见 [docs/development.md](docs/development.md)。
267
+
268
+ ## License
269
+
270
+ MIT
@@ -0,0 +1,223 @@
1
+ # tg-gemini
2
+
3
+ <div align="center">
4
+
5
+ [![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json)](https://github.com/astral-sh/uv)
6
+ [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
7
+ [![Python](https://img.shields.io/badge/python-3.12%2B-blue.svg)](https://github.com/atticuszeller/tg-gemini)
8
+ [![Test](https://github.com/atticuszeller/tg-gemini/actions/workflows/main.yml/badge.svg)](https://github.com/atticuszeller/tg-gemini/actions/workflows/main.yml)
9
+
10
+ **Telegram ↔ Gemini CLI 中间件**
11
+
12
+ 在 VPS 上持续运行,将 Telegram 消息转发给 Gemini CLI,实时流式返回响应。
13
+
14
+ </div>
15
+
16
+ ---
17
+
18
+ ```
19
+ 手机 Telegram ──→ tg-gemini (VPS) ──→ gemini CLI (本地)
20
+ ↑ │
21
+ └──────────── 流式响应 ←───────────────────┘
22
+ ```
23
+
24
+ ## 前置条件
25
+
26
+ - **Python 3.12+** 和 **[uv](https://docs.astral.sh/uv/)**
27
+ - **Gemini CLI** 已安装并完成登录:
28
+ ```bash
29
+ npm install -g @google/gemini-cli
30
+ gemini # 首次运行完成认证
31
+ ```
32
+ - **Telegram Bot Token**:通过 [@BotFather](https://t.me/botfather) 创建
33
+
34
+ ## 安装
35
+
36
+ ```bash
37
+ # pip
38
+ pip install tg-gemini
39
+
40
+ # uv(推荐,全局工具安装)
41
+ uv tool install tg-gemini
42
+
43
+ # 源码开发模式
44
+ git clone https://github.com/atticuszeller/tg-gemini
45
+ cd tg-gemini && uv sync --all-groups
46
+ ```
47
+
48
+ ## 快速开始
49
+
50
+ ```bash
51
+ # 1. 创建配置目录
52
+ mkdir -p ~/.tg-gemini
53
+
54
+ # 2. 复制并编辑配置(详细注释见 config.example.toml)
55
+ cp config.example.toml ~/.tg-gemini/config.toml
56
+
57
+ # 编辑:至少填写 telegram.token
58
+ # [telegram]
59
+ # token = "123456:ABC-DEF..."
60
+
61
+ # 3. 启动
62
+ tg-gemini start
63
+
64
+ # 或指定配置文件
65
+ tg-gemini start --config /path/to/config.toml
66
+ ```
67
+
68
+ **Ctrl+C** 优雅停止。
69
+
70
+ ## 配置
71
+
72
+ 配置文件为 TOML 格式,路径解析顺序:
73
+
74
+ 1. `--config` 参数
75
+ 2. 当前目录 `config.toml`
76
+ 3. `~/.tg-gemini/config.toml`(默认)
77
+
78
+ 最小配置:
79
+
80
+ ```toml
81
+ [telegram]
82
+ token = "YOUR_BOT_TOKEN"
83
+ ```
84
+
85
+ 完整选项见 [`config.example.toml`](config.example.toml) 和 [docs/configuration.md](docs/configuration.md)。
86
+
87
+ ### 常用配置片段
88
+
89
+ ```toml
90
+ # 限制只有自己能用(向 @userinfobot 查询 ID)
91
+ [telegram]
92
+ token = "..."
93
+ allow_from = "你的用户ID"
94
+
95
+ # 设置工作目录和模式
96
+ [gemini]
97
+ work_dir = "/path/to/your/project"
98
+ mode = "yolo" # 全自动,推荐服务端使用
99
+ ```
100
+
101
+ ## Bot 命令
102
+
103
+ | 命令 | 功能 |
104
+ |------|------|
105
+ | `/new` | 开启新 Gemini 会话 |
106
+ | `/list` | 查看所有会话 |
107
+ | `/switch <目标>` | 切换会话 |
108
+ | `/name <名称>` | 重命名会话 |
109
+ | `/delete` | 删除会话 |
110
+ | `/history` | 查看对话历史 |
111
+ | `/status` | 当前状态信息 |
112
+ | `/model [名称]` | 查看/切换模型 |
113
+ | `/mode [模式]` | 查看/切换工具模式 |
114
+ | `/lang [en\|zh]` | 切换界面语言 |
115
+ | `/quiet` | 切换静音(隐藏工具通知)|
116
+ | `/stop` | 终止当前 Agent |
117
+ | `/help` | 帮助 |
118
+
119
+ 完整说明见 [docs/commands.md](docs/commands.md)。
120
+
121
+ ## 群聊支持
122
+
123
+ ```toml
124
+ [telegram]
125
+ token = "..."
126
+ group_reply_all = false # 仅响应 @bot 或回复 bot(默认)
127
+ share_session_in_channel = false # 每人独立会话(默认)
128
+ ```
129
+
130
+ 默认行为:只有 @机器人、回复机器人的消息、以及 `/command` 会被处理。
131
+
132
+ ## 作为服务运行
133
+
134
+ 创建 `/etc/systemd/system/tg-gemini.service`:
135
+
136
+ ```ini
137
+ [Unit]
138
+ Description=tg-gemini Telegram Bot
139
+ After=network.target
140
+
141
+ [Service]
142
+ Type=simple
143
+ User=ubuntu
144
+ ExecStart=/home/ubuntu/.local/bin/tg-gemini start
145
+ Restart=on-failure
146
+ RestartSec=10
147
+
148
+ [Install]
149
+ WantedBy=multi-user.target
150
+ ```
151
+
152
+ ```bash
153
+ sudo systemctl daemon-reload
154
+ sudo systemctl enable --now tg-gemini
155
+ sudo journalctl -u tg-gemini -f
156
+ ```
157
+
158
+ ## 架构
159
+
160
+ tg-gemini 由 7 个核心模块组成:
161
+
162
+ ```
163
+ cli.py → engine.py → gemini.py 子进程 + JSONL 解析
164
+
165
+ telegram_platform.py 长轮询 + 消息收发
166
+ streaming.py 流式预览节流
167
+ session.py 多会话 + 历史持久化
168
+ card.py 卡片 UI(InlineKeyboard)
169
+ ```
170
+
171
+ 详细架构图和数据流见 [docs/architecture.md](docs/architecture.md)。
172
+
173
+ ## 常见问题
174
+
175
+ **Q: Gemini CLI 没有响应**
176
+
177
+ ```bash
178
+ gemini -p "hello" --output-format stream-json # 测试 CLI 是否正常
179
+ ```
180
+
181
+ **Q: 收不到 Telegram 消息**
182
+
183
+ 检查 `allow_from` 配置,查看日志:`tg-gemini start` 或 `journalctl -u tg-gemini -f`。
184
+
185
+ **Q: 流式预览触发限流**
186
+
187
+ 增大 `interval_ms`:
188
+ ```toml
189
+ [stream_preview]
190
+ interval_ms = 3000
191
+ ```
192
+
193
+ **Q: 如何让 Gemini 记住上下文**
194
+
195
+ 不要发 `/new`。服务重启后自动恢复到最近一次会话(通过 `--resume` 机制)。
196
+
197
+ ## 文档
198
+
199
+ | 文档 | 内容 |
200
+ |------|------|
201
+ | [docs/configuration.md](docs/configuration.md) | 完整配置项参考表 |
202
+ | [docs/commands.md](docs/commands.md) | 所有 Bot 命令详细说明 |
203
+ | [docs/architecture.md](docs/architecture.md) | 组件架构、数据流、设计决策 |
204
+ | [docs/development.md](docs/development.md) | 开发环境、测试、发布流程 |
205
+ | [docs/internals.md](docs/internals.md) | stream-json 协议、Markdown 转换、流式预览实现 |
206
+ | [config.example.toml](config.example.toml) | 带注释的完整配置示例 |
207
+
208
+ ## 开发
209
+
210
+ ```bash
211
+ git clone https://github.com/atticuszeller/tg-gemini
212
+ cd tg-gemini && uv sync --all-groups
213
+
214
+ bash dev.sh check # format → lint → test → pre-commit
215
+ bash dev.sh test # 运行测试(662 个,98.95% 覆盖率)
216
+ bash dev.sh lint # ty + ruff
217
+ ```
218
+
219
+ 详见 [docs/development.md](docs/development.md)。
220
+
221
+ ## License
222
+
223
+ MIT