返回 CodeWhale
README.md
1 # Telegram Bridge
2
3 This bridge lets a Telegram chat control a local `codewhale serve --http`
4 runtime from a phone. It uses Telegram Bot API long polling, so the first
5 version does not need a public webhook URL or inbound port.
6
7 Security model:
8
9 - `codewhale serve --http` stays bound to `127.0.0.1`.
10 - `/v1/*` runtime calls use `CODEWHALE_RUNTIME_TOKEN`. Legacy
11 `DEEPSEEK_RUNTIME_TOKEN` is accepted only as a compatibility fallback.
12 - Telegram chats must be allowlisted unless `TELEGRAM_ALLOW_UNLISTED=true` is
13 set for first pairing.
14 - Direct messages are the intended MVP control surface. Group chat control is
15 disabled unless `TELEGRAM_ALLOW_GROUPS=true`.
16 - Tool approvals are text commands: `/allow <approval_id>` or `/deny <approval_id>`.
17 - The bridge also sends inline button controls for common actions. Text
18 commands remain the fallback.
19
20 ## Setup
21
22 Create a bot with Telegram's `@BotFather`, then configure the bridge:
23
24 ```bash
25 cd /opt/codewhale/telegram-bridge
26 npm install --omit=dev
27 cp .env.example /etc/codewhale/telegram-bridge.env
28 sudoedit /etc/codewhale/telegram-bridge.env
29 node src/index.mjs
30 ```
31
32 Validate env files before starting the service:
33
34 ```bash
35 npm run validate:config -- \
36 --env /etc/codewhale/telegram-bridge.env \
37 --runtime-env /etc/codewhale/runtime.env \
38 --workspace-root /opt/whalebro \
39 --check-filesystem
40 ```
41
42 For first pairing, temporarily set `TELEGRAM_ALLOW_UNLISTED=true`, send the bot
43 `/status`, copy the returned `chat_id` or `user_id` into
44 `TELEGRAM_CHAT_ALLOWLIST`, then turn `TELEGRAM_ALLOW_UNLISTED=false`.
45
46 ## Commands
47
48 - `/menu`
49 - `/status`
50 - `/threads`
51 - `/new`
52 - `/resume <thread_id>`
53 - `/model <name|default>`
54 - `/interrupt`
55 - `/answer <response>`
56 - `/compact`
57 - `/allow <approval_id> [remember]`
58 - `/deny <approval_id>`
59
60 Anything else is sent as a prompt. If group control is explicitly enabled,
61 messages must start with `/cw` by default, for example:
62
63 ```text
64 /cw check git status and tell me what is dirty
65 ```
66
67 The `/menu`, `/status`, `/threads`, active-turn, and approval messages include
68 tap targets for common actions. Approval buttons map to the same runtime API as
69 `/allow` and `/deny`; they do not enable blanket auto-approval unless you tap
70 the explicit "Allow + remember" button.
71
72
73 ## Thread ownership
74
75 A thread belongs to the chat that created it. The bridge records the owner in
76 the thread map when it creates a thread. `/threads` lists only the threads this
77 chat created, and `/resume` accepts only an ID this chat created. Any other ID,
78 including a thread started from the desktop app, the TUI or another chat, gets
79 one fixed reply and is never sent to the Runtime.
80
81 When an existing thread map is first loaded, each chat keeps only the thread it
82 is bound to, and only after the bridge asks the Runtime once whether that thread
83 was started by this bridge in the configured workspace. A thread started from
84 the desktop app, the TUI or another client is dropped from the chat, and so is a
85 thread that several chats point at. Those chats send `/new` to start a new
86 thread. If the Runtime cannot be reached at start, the bridge retries every 30
87 seconds and meanwhile gives the fixed reply. An existing thread map records only
88 the thread each chat is bound to, so threads a chat created earlier and has
89 since left with `/new` are no longer listed or resumable. Review the thread map
90 after upgrading.
91
92 ## Restart access checks
93
94 Recovered deliveries require a saved, previously admitted sender identity that
95 still matches the current allowlist and group policy. Removing access and
96 restarting the bridge also stops recovery messages to that chat. Legacy state
97 without this identity stays detached; it does not cancel the runtime turn.
98 Use the Runtime to control a turn without a recorded initiating human. A fresh admitted
99 message records identity for subsequent restart recovery. Keep allow-unlisted
100 mode off when operating with private workspace or runtime data.
101
102
103 ## Durable admission and questions
104
105 The bridge saves each prompt, its Telegram message identity, initiating human,
106 and frozen Engine request in the existing thread map before advancing the
107 Telegram polling cursor. It uses Engine operation keys only when the Runtime
108 advertises both turn-operation idempotency and lookup. After an uncertain
109 submission, it looks up that original operation; a missing operation permits
110 retrying the same request and key, while an incomplete or unavailable receipt
111 stays retained. An Engine without these capabilities never receives an automatic
112 retry of an uncertain submission. `/status` reports retained admission state.
113 Changing the bot account, Runtime URL, allowlist, or group policy does not move
114 an existing pending request to another identity or Runtime.
115
116 A disconnected event stream reconnects to the accepted turn with its saved
117 cursor and accumulated reply. `/new`, `/resume`, and `/compact` cannot replace
118 a pending turn or retained final reply. Final replies are saved before sending.
119 An uncertain send is not automatically retried: the initiating human can retrieve
120 the saved reply with `/status`, which warns that the earlier delivery may have
121 succeeded. Telegram rate-limit refusals permit bounded retries. Progress updates
122 remain best effort; no send failure authorizes another Engine turn. This bridge does not provide a
123 cross-process lease for multiple instances sharing a state file. Run one bridge
124 process per bot token and state file.
125
126 When the Engine asks a question, reply with `/answer` followed by an option
127 number or text, or send the answer as a normal message. Answer multiple questions
128 on separate lines. The bridge checks the current Runtime question, turn, and
129 recorded initiating human before submitting an answer. Other group members
130 cannot answer, approve, or interrupt that person's turn. Group thread lists and
131 resume controls stay within that group's current conversation and, like private
132 chats, only cover threads that chat created.
133
133 lines MARKDOWN