opencode-telegram-monitor 0.1.0

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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +129 -0
  3. package/monitor.ts +3537 -0
  4. package/package.json +41 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Hipc
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.
package/README.md ADDED
@@ -0,0 +1,129 @@
1
+ # opencode-telegram-monitor
2
+
3
+ A read-only [opencode](https://opencode.ai) plugin that keeps you in the loop on your opencode sessions from **Telegram**.
4
+
5
+ It watches opencode sessions in real time and reports their lifecycle — started, busy, idle, retried, completed, failed or cancelled — plus token usage and cost, to a Telegram bot chat of your choice. The bot is fully read-only: approvals, permission prompts and answers are always handled in opencode itself.
6
+
7
+ ## Features
8
+
9
+ - **Session lifecycle notifications** — track sessions through `idle` / `busy` / `retry` states and `completed` / `failed` / `cancelled` outcomes, delivered straight to Telegram.
10
+ - **Token usage & cost** — aggregated input / output / reasoning / cache tokens with estimated cost per session.
11
+ - **Todo projection** — see the current session's todo list from Telegram.
12
+ - **Project registry & inline menu** — a registry of monitored projects (`~/.otg/projects.json`) with an inline-keyboard menu (`/menu`) to manage them from the chat.
13
+ - **Read-only bot** — intentional design; approvals and answers stay in opencode.
14
+ - **Cross-process poller lock** — when several opencode windows are open on the same machine, a file-based lock (`PollerLock`) guarantees only one instance polls Telegram at a time.
15
+ - **Proxy support** — optional HTTP/HTTPS proxy (with auth and CONNECT tunneling) for reaching the Telegram Bot API.
16
+ - **Resilient messaging** — long polling (`getUpdates`, 25 s interval), retries with backoff, message length clamping, and bot-token redaction in all logs.
17
+
18
+ ## Requirements
19
+
20
+ - Node.js (runtime for opencode plugins)
21
+ - opencode `>= 1.18` (plugin targets `1.18.23`)
22
+ - A Telegram bot token (create one with [@BotFather](https://t.me/BotFather)) and your Telegram `chatId`
23
+
24
+ ## Installation
25
+
26
+ ### From npm (recommended)
27
+
28
+ The plugin is published as [`opencode-telegram-monitor`](https://www.npmjs.com/package/opencode-telegram-monitor) and can be loaded straight from the opencode config:
29
+
30
+ 1. Add the plugin to `~/.config/opencode/opencode.json`. **Pin a concrete version** (not `@latest`) — a pinned version makes opencode load the cached copy directly, so the plugin's own self-update is the only thing that ever replaces the files:
31
+
32
+ ```json
33
+ {
34
+ "$schema": "https://opencode.ai/config.json",
35
+ "plugin": ["opencode-telegram-monitor@0.1.0"]
36
+ }
37
+ ```
38
+
39
+ 2. Restart opencode. The plugin is installed automatically into the opencode cache (`~/.cache/opencode/`) on first start.
40
+
41
+ 3. If you previously installed a local copy, **remove it** to avoid double-loading the plugin (npm and local copies with the same name load side by side, which would run two pollers):
42
+
43
+ ```bash
44
+ rm -f ~/.config/opencode/plugins/telegram-session-monitor.ts
45
+ ```
46
+
47
+ The version in the config never needs manual bumping — see [Automatic updates](#automatic-updates) below.
48
+
49
+ ### From source (local file)
50
+
51
+ 1. Copy `monitor.ts` into your opencode plugins directory:
52
+
53
+ ```bash
54
+ mkdir -p ~/.config/opencode/plugins
55
+ cp monitor.ts ~/.config/opencode/plugins/telegram-session-monitor.ts
56
+ ```
57
+
58
+ 2. Restart opencode. The plugin loads automatically from the plugins directory.
59
+
60
+ ## Automatic updates
61
+
62
+ When installed from npm, the plugin checks for a newer release on the npm registry **once per opencode start** (5 seconds after startup, non-blocking). If a newer version is found, it:
63
+
64
+ 1. Downloads the new tarball into a **staging directory** (`~/.otg/update-staging/`).
65
+ 2. Verifies the staged `monitor.ts` reports the expected version.
66
+ 3. Atomically swaps the cached plugin directory (old directory is renamed as a backup, then replaced), re-verifies, and only then removes the backup.
67
+ 4. Sends a Telegram notification; **restart opencode** to load the new version.
68
+
69
+ Any failure along the way — including being **offline** — leaves the previously installed version completely untouched, so opencode always loads a working plugin. A local-file installation (see above) is never auto-updated.
70
+
71
+ ## Configuration
72
+
73
+ The plugin reads its configuration from `~/.otg/telegram.json`:
74
+
75
+ ```json
76
+ {
77
+ "botToken": "123456789:ABCdef...",
78
+ "chatId": "987654321",
79
+ "proxy": "http://user:pass@proxy.example.com:8080"
80
+ }
81
+ ```
82
+
83
+ | Field | Required | Description |
84
+ | ---------- | :------: | ------------------------------------------------------------------------ |
85
+ | `botToken` | yes | Your Telegram bot token (validated on load). |
86
+ | `chatId` | yes | The chat the bot is allowed to talk to / listen from. |
87
+ | `proxy` | no | Optional `http://` or `https://` proxy URL (may include auth). |
88
+
89
+ > If the config is missing or invalid, the plugin logs an error and disables itself instead of crashing opencode.
90
+
91
+ ## Telegram commands
92
+
93
+ | Command | Description |
94
+ | -------------------- | -------------------------------------------------- |
95
+ | `/start` | Check the plugin connection and bot health. |
96
+ | `/sessions` | List active sessions. |
97
+ | `/use <short-id>` | Select a session to inspect. |
98
+ | `/status` | Show the selected session's status. |
99
+ | `/todo` | Show the selected session's todo list. |
100
+ | `/usage` | Show the selected session's token usage and cost. |
101
+ | `/menu` | Manage monitored projects (inline keyboard). |
102
+ | `/help` | Show this help. |
103
+
104
+ ## How it works
105
+
106
+ - The plugin subscribes to opencode's event stream through the `@opencode-ai/sdk` client and maintains an in-memory projection of every session: state, outcome, tools, todos, waiting prompts and token totals.
107
+ - A background poller talks to the Telegram Bot API (`getUpdates` long polling) so you can send commands from the chat; replies are sent back through the same channel with retries.
108
+ - When several opencode processes share one machine, `PollerLock` (`~/.otg/`) elects a single poller to avoid duplicate `getUpdates` consumers.
109
+ - A self-healing registrar re-asserts the current project into the registry every 5 minutes, so a project removed via `/menu` comes back as disabled while its window stays open.
110
+
111
+ ### State & data files (`~/.otg/`)
112
+
113
+ | File | Purpose |
114
+ | ------------------ | ---------------------------------------------- |
115
+ | `telegram.json` | Plugin configuration (bot token, chat id). |
116
+ | `projects.json` | Registry of monitored projects. |
117
+ | `tgdiag.log` | Diagnostics log (token-redacted). |
118
+ | `*.lock` | Cross-process poller lock files. |
119
+
120
+ ## Security notes
121
+
122
+ - The bot is **read-only** — it never acts on your behalf inside opencode.
123
+ - Messages are limited to the originating `chatId`; updates from any other chat are ignored.
124
+ - The bot token is redacted (`[REDACTED]`) in all log output and diagnostics.
125
+ - The plugin runs locally and talks to the public Telegram Bot API only.
126
+
127
+ ## License
128
+
129
+ [MIT](LICENSE)