@llblab/pi-telegram 0.2.10 → 0.4.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.
- package/README.md +52 -19
- package/docs/README.md +2 -3
- package/docs/architecture.md +62 -31
- package/docs/locks.md +136 -0
- package/index.ts +323 -1880
- package/lib/api.ts +396 -60
- package/lib/attachments.ts +128 -16
- package/lib/commands.ts +648 -14
- package/lib/config.ts +169 -0
- package/lib/handlers.ts +474 -0
- package/lib/locks.ts +306 -0
- package/lib/media.ts +196 -46
- package/lib/menu.ts +920 -338
- package/lib/model.ts +647 -0
- package/lib/pi.ts +90 -0
- package/lib/polling.ts +240 -14
- package/lib/preview.ts +420 -25
- package/lib/queue.ts +1137 -110
- package/lib/registration.ts +214 -31
- package/lib/rendering.ts +560 -366
- package/lib/replies.ts +198 -8
- package/lib/routing.ts +217 -0
- package/lib/runtime.ts +475 -0
- package/lib/setup.ts +143 -1
- package/lib/status.ts +432 -13
- package/lib/turns.ts +217 -36
- package/lib/updates.ts +340 -109
- package/package.json +18 -3
- package/AGENTS.md +0 -91
- package/BACKLOG.md +0 -5
- package/CHANGELOG.md +0 -34
- package/lib/model-switch.ts +0 -62
- package/lib/types.ts +0 -137
- package/tests/api.test.ts +0 -331
- package/tests/attachments.test.ts +0 -132
- package/tests/commands.test.ts +0 -85
- package/tests/config.test.ts +0 -80
- package/tests/media.test.ts +0 -166
- package/tests/menu.test.ts +0 -676
- package/tests/polling.test.ts +0 -202
- package/tests/preview.test.ts +0 -480
- package/tests/queue.test.ts +0 -3245
- package/tests/registration.test.ts +0 -268
- package/tests/rendering.test.ts +0 -526
- package/tests/replies.test.ts +0 -142
- package/tests/turns.test.ts +0 -247
- package/tests/updates.test.ts +0 -416
package/README.md
CHANGED
|
@@ -17,8 +17,8 @@ This repository is an actively maintained fork of [`badlogic/pi-telegram`](https
|
|
|
17
17
|
- **Interactive UI**: Manage your session directly from Telegram. Inline buttons allow you to switch models and adjust reasoning (thinking) levels on the fly.
|
|
18
18
|
- **In-flight Model Switching**: Change the active model mid-generation. The agent gracefully pauses, applies the new model, and restarts its response without losing context.
|
|
19
19
|
- **Smart Message Queue**: Messages sent while the agent is busy are queued and previewed in the pi status bar, and queued turns can be reprioritized or removed with Telegram reactions.
|
|
20
|
-
- **Mobile-Optimized Rendering**: Tables and lists are formatted for narrow screens. Markdown is correctly parsed and split to fit Telegram's limits without breaking HTML structures or code blocks, block spacing stays faithful to the original Markdown with readable heading separation, supported absolute links stay clickable, and unsupported link forms degrade safely.
|
|
21
|
-
- **File Handling & Attachments**: Send images and files to the agent, or ask
|
|
20
|
+
- **Mobile-Optimized Rendering**: Tables and lists are formatted for narrow screens, table padding accounts for emoji grapheme and wide Unicode display width, and Telegram-originated runs prompt the assistant to prefer narrow table columns for phone readability. Markdown is correctly parsed and split to fit Telegram's limits without breaking HTML structures or code blocks, block spacing stays faithful to the original Markdown with readable heading separation, supported absolute links stay clickable, and unsupported link forms degrade safely.
|
|
21
|
+
- **File Handling & Attachments**: Send images and files to the agent, transcribe or transform inbound files with configured attachment handlers, or ask pi to generate and return artifacts. Inbound downloads and outbound attachments are size-limited by default, and outbound files are delivered automatically via the `telegram_attach` tool.
|
|
22
22
|
- **Streaming Responses**: Closed Markdown blocks stream back as rich Telegram HTML while pi is generating, and the still-growing tail stays readable until the final fully rendered reply lands.
|
|
23
23
|
|
|
24
24
|
## Install
|
|
@@ -44,7 +44,7 @@ pi install git:github.com/llblab/pi-telegram
|
|
|
44
44
|
3. Pick a name and username
|
|
45
45
|
4. Copy the bot token
|
|
46
46
|
|
|
47
|
-
### 2.
|
|
47
|
+
### 2. Configure the extension in pi
|
|
48
48
|
|
|
49
49
|
Start pi, then run:
|
|
50
50
|
|
|
@@ -52,17 +52,17 @@ Start pi, then run:
|
|
|
52
52
|
/telegram-setup
|
|
53
53
|
```
|
|
54
54
|
|
|
55
|
-
Paste your bot token when prompted. If a bot token is already saved in `~/.pi/agent/telegram.json`,
|
|
55
|
+
Paste your bot token when prompted. If a bot token is already saved in `~/.pi/agent/telegram.json`, the setup prompt shows that stored value by default. Otherwise it prefills from the first configured environment variable in `TELEGRAM_BOT_TOKEN`, `TELEGRAM_BOT_KEY`, `TELEGRAM_TOKEN`, or `TELEGRAM_KEY`. The saved config file is written with private `0600` permissions.
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
### 3. Connect this pi session
|
|
58
58
|
|
|
59
59
|
```bash
|
|
60
60
|
/telegram-connect
|
|
61
61
|
```
|
|
62
62
|
|
|
63
|
-
|
|
63
|
+
The bridge is session-local: only one pi instance polls Telegram at a time. `/telegram-connect` records polling ownership in `~/.pi/agent/locks.json`; live ownership moves require confirmation, while `/new` and same-`cwd` process restarts resume automatically.
|
|
64
64
|
|
|
65
|
-
###
|
|
65
|
+
### 4. Pair your account from Telegram
|
|
66
66
|
|
|
67
67
|
1. Open the DM with your bot in Telegram
|
|
68
68
|
2. Send `/start`
|
|
@@ -73,34 +73,69 @@ The first user to message the bot becomes the exclusive owner of the bridge. The
|
|
|
73
73
|
|
|
74
74
|
Once paired, simply chat with your bot in Telegram. All text, images, and files are forwarded to pi.
|
|
75
75
|
|
|
76
|
-
### Commands & Controls
|
|
76
|
+
### Telegram Commands & Controls
|
|
77
77
|
|
|
78
|
+
Use these inside the Telegram DM with your bot:
|
|
79
|
+
|
|
80
|
+
- **`/start`**: Pair the first Telegram user when needed, register the bot command menu, and show help.
|
|
81
|
+
- **`/help`**: Show the Telegram help text.
|
|
78
82
|
- **`/status`**: View session stats, cost, and use inline buttons to change models.
|
|
79
83
|
- **`/model`**: Open the interactive model selector.
|
|
80
84
|
- **`/compact`**: Start session compaction (only works when the session is idle).
|
|
81
85
|
- **`/stop`**: Abort the active run.
|
|
82
|
-
|
|
83
|
-
|
|
86
|
+
|
|
87
|
+
Telegram command admission is explicit: `/compact`, `/stop`, `/help`, and `/start` execute immediately; `/status` and `/model` enter the high-priority control lane so they can run before normal queued prompts when pi becomes safe to dispatch.
|
|
88
|
+
|
|
89
|
+
### Pi Commands
|
|
90
|
+
|
|
91
|
+
Run these inside pi, not Telegram:
|
|
92
|
+
|
|
93
|
+
- **`/telegram-setup`**: Configure or update the Telegram bot token.
|
|
94
|
+
- **`/telegram-status`**: Check bridge status, connection, polling, execution, queue, and recent redacted runtime/API failure events.
|
|
95
|
+
- **`/telegram-connect`**: Start polling Telegram updates in the current pi session, acquire the singleton lock, or interactively move ownership here from another live instance.
|
|
96
|
+
- **`/telegram-disconnect`**: Stop polling in the current pi session and release the singleton lock.
|
|
84
97
|
|
|
85
98
|
### Queue, Reactions, and Media
|
|
86
99
|
|
|
87
|
-
- If you send more Telegram messages while pi is busy, they
|
|
88
|
-
- `👍` moves a waiting
|
|
100
|
+
- If you send more Telegram messages while pi is busy, they enter the default prompt queue and are processed in order.
|
|
101
|
+
- `👍` moves a waiting prompt into the priority prompt queue, behind control actions but ahead of default prompts. Removing `👍` sends it back to its normal queue position, and adding `👍` again gives it a fresh priority position.
|
|
89
102
|
- `👎` removes a waiting turn from the queue. Telegram Bot API does not expose ordinary DM message-deletion events through the polling path used here, so queue removal is bound to the dislike reaction.
|
|
90
103
|
- For media groups, a reaction on any message in the group applies to the whole queued turn.
|
|
91
104
|
- If you edit a Telegram message while it is still waiting in the queue, the queued turn is updated instead of creating a duplicate prompt. Edits after a turn has already started may not affect the active run.
|
|
92
|
-
- Inbound images, albums, and files are saved to `~/.pi/agent/tmp/telegram
|
|
105
|
+
- Inbound images, albums, and files are saved to `~/.pi/agent/tmp/telegram`. Unhandled local file paths are included in the prompt, handled attachment output is injected into the prompt text, and inbound images are forwarded to pi as image inputs. Inbound downloads default to a 50 MiB limit and can be adjusted with `PI_TELEGRAM_INBOUND_FILE_MAX_BYTES` or `TELEGRAM_MAX_FILE_SIZE_BYTES`.
|
|
93
106
|
- Queue reactions depend on Telegram delivering `message_reaction` updates for your bot and chat type.
|
|
94
107
|
|
|
108
|
+
### Inbound Attachment Handlers
|
|
109
|
+
|
|
110
|
+
`telegram.json` can define `attachmentHandlers` for common preprocessing such as voice transcription. The first matching handler runs after download and before the Telegram turn enters the pi queue.
|
|
111
|
+
|
|
112
|
+
```json
|
|
113
|
+
{
|
|
114
|
+
"attachmentHandlers": [
|
|
115
|
+
{
|
|
116
|
+
"mime": "audio/*",
|
|
117
|
+
"command": "/home/me/bin/transcribe {filename} ru"
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
"type": "voice",
|
|
121
|
+
"tool": "transcribe_groq",
|
|
122
|
+
"args": { "lang": "ru" }
|
|
123
|
+
}
|
|
124
|
+
]
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Matching supports `mime`, `type`, or `match`; wildcards like `audio/*` are accepted. Command placeholders are substituted as argv, not shell text: `{filename}` and `{path}` are the downloaded file path, `{basename}` is the display filename, `{mime}` is the MIME type, and `{type}` is the Telegram attachment type. Tool handlers invoke a locally available tool by name, for example `transcribe_groq`; how that tool is provided is outside pi-telegram. Local attachments stay in the prompt under `[attachments] <directory>` with relative file entries; successful handler stdout is added under `[outputs]`; failed or empty handlers simply produce no output block.
|
|
129
|
+
|
|
95
130
|
### Requesting Files
|
|
96
131
|
|
|
97
|
-
If you ask pi for a file or generated artifact (e.g., _"generate a shell script and attach it"_), pi will call the `telegram_attach` tool, and the extension will send the file alongside its next Telegram reply.
|
|
132
|
+
If you ask pi for a file or generated artifact (e.g., _"generate a shell script and attach it"_), pi will call the `telegram_attach` tool, and the extension will send the file alongside its next Telegram reply. Outbound attachments default to a 50 MiB limit and can be adjusted with `PI_TELEGRAM_OUTBOUND_ATTACHMENT_MAX_BYTES` or `TELEGRAM_MAX_ATTACHMENT_SIZE_BYTES`.
|
|
98
133
|
|
|
99
134
|
## Streaming
|
|
100
135
|
|
|
101
136
|
The extension streams assistant previews back to Telegram while pi is generating.
|
|
102
137
|
|
|
103
|
-
Rich previews are sent through editable messages because Telegram drafts are text-only. Closed top-level Markdown blocks can appear with formatting before the answer finishes, while the still-growing tail remains conservative and readable until the preview is replaced with the fully rendered Telegram HTML reply.
|
|
138
|
+
Rich previews are sent through editable messages because Telegram drafts are text-only. Closed top-level Markdown blocks can appear with formatting before the answer finishes, while the still-growing tail remains conservative and readable until the preview is replaced with the fully rendered Telegram HTML reply. Editable preview messages are also attached as replies to the source Telegram prompt when possible.
|
|
104
139
|
|
|
105
140
|
## Status bar
|
|
106
141
|
|
|
@@ -112,10 +147,8 @@ The pi status bar shows queued Telegram turns as compact previews, for example:
|
|
|
112
147
|
|
|
113
148
|
## Notes
|
|
114
149
|
|
|
115
|
-
-
|
|
116
|
-
-
|
|
117
|
-
- Long replies are split below Telegram's 4096 character limit without intentionally breaking Telegram HTML formatting
|
|
118
|
-
- Outbound files are sent via `telegram_attach`
|
|
150
|
+
- Replies to Telegram prompts are sent as Telegram replies to the source message when possible; if the source message is unavailable, delivery falls back to a normal message
|
|
151
|
+
- Long replies are split below Telegram's 4096 character limit without intentionally breaking Telegram HTML formatting; only the first split message is attached as a Telegram reply to the source prompt
|
|
119
152
|
- Temporary inbound Telegram files are cleaned up on later session starts
|
|
120
153
|
|
|
121
154
|
## License
|
package/docs/README.md
CHANGED
|
@@ -4,6 +4,5 @@ Living index of project documentation in `/docs`.
|
|
|
4
4
|
|
|
5
5
|
## Documents
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
| [architecture.md](./architecture.md) | Overview of the Telegram bridge runtime, queueing model, rendering pipeline, and interactive controls |
|
|
7
|
+
- [architecture.md](./architecture.md) — Overview of the Telegram bridge runtime, queueing model, rendering pipeline, and interactive controls
|
|
8
|
+
- [locks.md](./locks.md) — Shared `locks.json` standard for singleton extension ownership
|
package/docs/architecture.md
CHANGED
|
@@ -7,39 +7,51 @@
|
|
|
7
7
|
- Poll Telegram updates and enforce single-user pairing
|
|
8
8
|
- Translate Telegram messages and media into pi inputs
|
|
9
9
|
- Stream and deliver pi responses back to Telegram
|
|
10
|
-
- Manage Telegram-specific controls such as queue reactions, `/status`, `/model`, and `/
|
|
10
|
+
- Manage Telegram-specific controls such as queue reactions, `/status`, `/model`, `/compact`, and `/stop`
|
|
11
11
|
|
|
12
12
|
## Runtime Structure
|
|
13
13
|
|
|
14
|
-
`index.ts` remains the extension entrypoint and composition
|
|
14
|
+
`index.ts` remains the extension entrypoint and composition root. Reusable runtime logic is split into flat domain files under `/lib` rather than into a deep local module tree.
|
|
15
|
+
|
|
16
|
+
Architecture shorthand: this repository uses a `Flat Domain DAG`: cohesive bridge domains live as flat `/lib/*.ts` modules, local imports must form a directed acyclic graph, shared buckets are avoided, and `index.ts` wires live pi/Telegram ports plus session state.
|
|
15
17
|
|
|
16
18
|
Domain grouping rule: prefer cohesive domain files over atomizing every helper into its own file. A `shared` domain is allowed only for types or constants that genuinely span multiple bridge domains.
|
|
17
19
|
|
|
20
|
+
Interface consistency rule: when two modules mean the same runtime entity, they should converge on the owning domain's exported contract. Local structural `*Like` or view contracts are appropriate only when a domain intentionally needs a narrow projection to avoid unnecessary coupling; they should not become duplicate source-of-truth shapes for the same entity.
|
|
21
|
+
|
|
18
22
|
Naming rule: because the repository already scopes this codebase to Telegram, extracted module and test filenames use bare domain names such as `api.ts`, `queue.ts`, `updates.ts`, and `queue.test.ts` rather than repeating `telegram-*` in every filename.
|
|
19
23
|
|
|
20
24
|
Current runtime areas include:
|
|
21
25
|
|
|
22
|
-
- Telegram Bot API transport
|
|
23
|
-
-
|
|
24
|
-
-
|
|
25
|
-
-
|
|
26
|
+
- Telegram Bot API concrete transport shapes live with Telegram API helpers in `/lib/api.ts`, while persisted bot/session pairing state lives in `/lib/config.ts`; domain-owned runtime state types stay with their owners, such as queued/active turn state in `/lib/queue.ts` and preview state in `/lib/preview.ts`, while domain helpers prefer local structural `*Like` contracts instead of importing concrete wire DTOs
|
|
27
|
+
- Direct pi SDK imports are centralized in `/lib/pi.ts`, which exposes concrete pi SDK type exports, bound extension API runtime ports, and narrow bridge-facing helpers such as settings-manager creation plus context model/idle/pending-message/compaction adapters; `index.ts` uses this adapter namespace instead of importing `@mariozechner/pi-coding-agent` directly
|
|
28
|
+
- Session-local runtime primitives such as queue/control/priority ordering counters, lifecycle/dispatch flags, setup guard state, abort-handler storage/binding, typing-loop timer lifecycle, typing-loop starter binding, prompt-dispatch lifecycle/runtime adapters, and agent-end reset sequencing in `/lib/runtime.ts`; the runtime domain's essence is mutable cross-domain session coordination rather than business behavior. It exposes a grouped bridge runtime facade with named queue/lifecycle/setup/abort/typing ports that bind those primitives to one session state while remaining a cohesive state/runtime boundary, and `index.ts` still wires live Telegram API calls and status updates into those helpers. Preview-specific state, draft-support detection, and draft-id allocation live in `/lib/preview.ts`.
|
|
29
|
+
- Constants live in their owning domains instead of a shared constants module: API paths/inbound limits and inbound file-size env parsing in `/lib/api.ts`, outbound attachment limits and outbound attachment-size env parsing in `/lib/attachments.ts`, media-group debounce in `/lib/media.ts`, attachment-handler timeout and local tool lookup defaults in `/lib/handlers.ts`, menu cache/state bounds in `/lib/menu.ts`, preview throttle/draft bounds in `/lib/preview.ts`, typing cadence in `/lib/runtime.ts`, diagnostic ring limits in `/lib/status.ts`, Telegram prompt prefix in `/lib/turns.ts`, and system-prompt guidance in `/lib/registration.ts`.
|
|
30
|
+
- Queueing, narrow Telegram prompt content contracts, queue-store contracts/state helpers, active-turn state helpers, dispatch-readiness adapters, queue append/mutation runtime/controller adapters, control enqueue controllers, queue dispatch readiness/controller/runtime adapters, prompt enqueue/history planning/runtime/controllers, queue-runtime, session state appliers plus lifecycle/runtime sequencing, session start/shutdown sequencing plus hook binding, agent-start/agent-end lifecycle handling plus hook/runtime binding, and tool lifecycle handling plus tool-execution hook/runtime binding in `/lib/queue.ts`
|
|
31
|
+
- Model identity/thinking-level contracts, scoped model-pattern parsing/resolution/sorting, current-model store/update/runtime helpers, in-flight model-switch state helpers, restart eligibility, delayed abort decisions, Telegram-prefix defaulted continuation prompt construction, continuation queue adapters, and model-switch controller/runtime binding over queue-owned turns in `/lib/model.ts`
|
|
32
|
+
- Preview transport-selection, assistant-message preview lifecycle hook binding/handling, preview-finalization, preview controller state/reset helpers, preview Bot API message/rendered-chunk transport adapters, preview-controller/assistant-preview runtime binding, reply-metadata defaulting through the replies-domain helper, and preview-runtime helpers in `/lib/preview.ts`
|
|
33
|
+
- Reply-transport, rendered-message delivery runtime/binding, structural assistant-message extraction, reply-parameter construction over API-owned transport shapes, and plain/Markdown final-reply helpers in `/lib/replies.ts`
|
|
26
34
|
- Preview appearance and snapshot derivation stay in `/lib/rendering.ts`, while `/lib/preview.ts` owns transport and lifecycle decisions, so richer preview strategies can evolve without entangling Markdown formatting with Telegram delivery state
|
|
27
|
-
- Polling request, stop-condition,
|
|
28
|
-
- Telegram
|
|
29
|
-
- Telegram
|
|
30
|
-
- Telegram media
|
|
31
|
-
- Telegram
|
|
32
|
-
- Telegram
|
|
33
|
-
- Telegram
|
|
34
|
-
- Telegram
|
|
35
|
-
-
|
|
36
|
-
-
|
|
37
|
-
-
|
|
38
|
-
-
|
|
39
|
-
-
|
|
40
|
-
-
|
|
35
|
+
- Polling request, start/stop controller state orchestration, polling activity readers, stop-condition, structural config contract, long-poll loop helpers, and poll-loop/controller runtime wiring over Telegram transport ports in `/lib/polling.ts`
|
|
36
|
+
- Telegram persisted config shape, config-path defaults, config file read/write helpers, mutable config-store accessors, single-user authorization, and first-user pairing side effects/runtime adapters in `/lib/config.ts`
|
|
37
|
+
- Telegram API helpers, concrete Bot API transport shapes including reply parameters and send/edit message bodies, typed/default Bot API runtime helpers, bot identity fetch transport, chat-action sender adapters/runtime-bound typing action, lazy bot-token client wrappers, API runtime error-recording wrappers, temp paths, inbound file-size limits, and runtime-bound temp-directory preparation/default cleanup in `/lib/api.ts`
|
|
38
|
+
- Telegram turn-building helpers, runtime turn-builder wiring over media download ports and media-owned downloaded-file metadata contracts, inbound attachment handler processing, queued-prompt edit runtime binding, and Node-backed image-file reads for pi image inputs in `/lib/turns.ts`
|
|
39
|
+
- Telegram media/text extraction, file-info normalization, downloaded-message-file metadata contracts, inbound file download assembly, media-group debounce helpers, media-group controller state, and media-group-aware authorized-message dispatch adapter wiring in `/lib/media.ts`
|
|
40
|
+
- Telegram inbound attachment handler matching, command placeholder substitution, local tool invocation, handler-output collection, and quiet failure handling in `/lib/handlers.ts`
|
|
41
|
+
- Telegram slash-command parsing, command-message target helpers/adapters, command control-enqueue adapters/runtime binding, command-action routing, command-handler/target-runtime and command-or-prompt dispatch binding, command runtime port orchestration, shared command-runtime reply/status/control adapter closures, stop/compact/status/model/help command side-effect branching, bound Bot API command registration, and Bot API command metadata helpers in `/lib/commands.ts`
|
|
42
|
+
- Telegram updates extraction, authorization classification, execution-planning, authorized reaction priority/removal handling, direct execute-from-update routing, and runtime helpers in `/lib/updates.ts`
|
|
43
|
+
- Inbound route composition across paired update execution, callback menus, command-or-prompt dispatch, media grouping, prompt enqueueing, queued edits, and attachment-handler turn building in `/lib/routing.ts`
|
|
44
|
+
- Telegram attachment queueing, narrow structural attachment turn targets, queued-attachment sender runtime binding, delivery helpers, Node-backed file stat checks, outbound photo-vs-document classification, and outbound attachment limits/env parsing in `/lib/attachments.ts`
|
|
45
|
+
- Telegram tool, command, before-agent prompt, and lifecycle-hook registration helpers in `/lib/registration.ts`
|
|
46
|
+
- Setup/token prompt, environment fallback, guarded setup runtime adapter wiring, structural setup config contract, token validation, config persistence orchestration, and setup notification helpers in `/lib/setup.ts`
|
|
47
|
+
- Markdown block scanning/rendering, inline-token/style rendering, text-piece rendering, stable-preview block scanning, final rendered-block chunk balancing, preview-snapshot derivation, HTML escaping, raw HTML tag-preserving chunking, and Telegram message rendering helpers in `/lib/rendering.ts`
|
|
48
|
+
- Status-bar rendering/runtime adapters, bridge status state adapters, status-message rendering and status-HTML binding, structural queue-lane status view contracts, structured redacted runtime-event recording, recent-event recorder state, recent-event line formatting, and grouped pi-side diagnostics helpers in `/lib/status.ts`
|
|
49
|
+
- Menu settings/model-registry access through structural ports, menu-state construction, menu runtime state/cache controller, menu-state storage pruning/refresh helpers, command open-flow branching, action runtime/state-builder adapters, menu callback handler adapters, stored callback entry/runtime routing, model-menu input-cache/state-building resolution, pure menu-page derivation, pure menu render-payload builders, menu-message runtime, callback parsing, callback mutation helpers, full model-callback planning and execution, interface-polished callback effect ports, status-thinking callback handling, and UI helpers in `/lib/menu.ts`
|
|
50
|
+
- Telegram API-bound transport adapters and top-level runtime registration stay in `index.ts`; broader inbound event-side orchestration lives in `/lib/routing.ts`; direct Node file-operation imports stay in the owning domains rather than the entrypoint
|
|
51
|
+
- Remaining `index.ts` wiring is intentionally cross-domain adapter code that closes over live extension state, pi callbacks, Telegram API ports, and status updates; keep repeated wiring DRY through small local adapter helpers or owning-domain contracts when that reduces duplication without obscuring live state, and extract more only when a boundary can move cohesive behavior into an owning domain instead of relocating one-off closures
|
|
41
52
|
- Additional domains can be extracted into `/lib/*.ts` as the bridge grows, while keeping `index.ts` as the single entrypoint
|
|
42
|
-
-
|
|
53
|
+
- `index.ts` uses namespace imports for local bridge domains so orchestration reads as domain-scoped calls such as `Queue.*`, `Turns.*`, and `Rendering.*` instead of long flat import lists
|
|
54
|
+
- Mirrored domain regression coverage lives in `/tests/*.test.ts` using the same bare domain naming scheme, and architecture-invariant coverage in `/tests/invariants.test.ts` checks that the local `index.ts` plus `/lib/*.ts` import graph stays acyclic, shared bucket domains such as `lib/constants.ts` or `lib/types.ts` are not reintroduced, empty interface-extension shells stay collapsed into clearer type aliases, direct pi SDK imports stay centralized, `index.ts` source code stays free of direct Node runtime imports, local helper declarations, local arrow adapters, direct `process.env`, and direct `pi.*` receiver access, `/lib/runtime.ts` stays free of local domain imports, structural leaf domains stay free of local nominal imports, the menu domain stays on structural ports without re-exporting model, API transport stays decoupled from persisted config defaults, structural update/media domains stay decoupled from concrete API transport shapes, and attachment delivery stays decoupled from queue/inbound media/API helpers
|
|
43
55
|
|
|
44
56
|
## Configuration UX
|
|
45
57
|
|
|
@@ -49,7 +61,11 @@ Current runtime areas include:
|
|
|
49
61
|
2. Otherwise use the first configured environment variable from the supported Telegram token list
|
|
50
62
|
3. Fall back to the example placeholder when no real value exists
|
|
51
63
|
|
|
52
|
-
Because `ctx.ui.input()` only exposes placeholder text, the bridge uses `ctx.ui.editor()` whenever a real default value must appear already filled in.
|
|
64
|
+
Because `ctx.ui.input()` only exposes placeholder text, the bridge uses `ctx.ui.editor()` whenever a real default value must appear already filled in. The persisted `telegram.json` config is written with private `0600` permissions because it contains the bot token.
|
|
65
|
+
|
|
66
|
+
## Runtime Ownership
|
|
67
|
+
|
|
68
|
+
Telegram bot configuration stays in `~/.pi/agent/telegram.json`; singleton runtime ownership lives separately in `~/.pi/agent/locks.json` under `@llblab/pi-telegram`. `/telegram-connect` acquires or moves that lock before polling starts, and `/telegram-disconnect` stops polling and releases it. Session start may read the existing lock and resume polling when the lock already points at the current `pid`/`cwd`; after a full pi process restart, it may also replace a stale lock from the same `cwd` and resume polling automatically. Session start does not create new ownership from an inactive lock, a live external lock, or a stale lock from another directory. Session replacement suspends polling and ownership watchers without releasing the lock, allowing the next session-start hook in the same `pid`/`cwd` to resume from the existing explicit ownership. When a live external owner exists, `/telegram-connect` asks whether to move singleton ownership to the current pi instance. Active owners poll the lock while running through a snapshotted ownership context, so long-lived timers do not touch stale pi contexts after `/new`; they stop local polling when `locks.json` no longer points at their own `pid`/`cwd`, without deleting the new owner lock. Deleting `locks.json` resets runtime ownership without deleting Telegram configuration.
|
|
53
69
|
|
|
54
70
|
## Message And Queue Flow
|
|
55
71
|
|
|
@@ -59,10 +75,12 @@ Because `ctx.ui.input()` only exposes placeholder text, the bridge uses `ctx.ui.
|
|
|
59
75
|
2. Each update offset is persisted only after the update handler succeeds; repeated handler failures are bounded so one poisoned update cannot stall polling forever
|
|
60
76
|
3. The bridge filters to the paired private user
|
|
61
77
|
4. Media groups are coalesced into a single Telegram turn when needed
|
|
62
|
-
5. Files are streamed into `~/.pi/agent/tmp/telegram` with size
|
|
63
|
-
6.
|
|
64
|
-
7.
|
|
65
|
-
8.
|
|
78
|
+
5. Files are streamed into `~/.pi/agent/tmp/telegram` with a default 50 MiB size limit, partial-download cleanup on failures, and stale temp cleanup on session start; operators can tune the limit with `PI_TELEGRAM_INBOUND_FILE_MAX_BYTES` or `TELEGRAM_MAX_FILE_SIZE_BYTES`
|
|
79
|
+
6. Configured inbound attachment handlers may run on downloaded files by MIME wildcard, Telegram attachment type, or generic match selector; command handlers receive safe argv substitution for `{filename}`/`{path}`/`{basename}`/`{mime}`/`{type}`, and tool handlers invoke locally available tools by name
|
|
80
|
+
7. Local attachments stay visible under `[attachments] <directory>` with relative file entries, and handler stdout is appended under `[outputs]` before the agent sees the turn; failed or empty handlers simply omit handler output while keeping the attachment entry
|
|
81
|
+
8. A `PendingTelegramTurn` is created and queued locally
|
|
82
|
+
9. Telegram `edited_message` updates are routed separately and update a matching queued turn when the original message has not been dispatched yet
|
|
83
|
+
10. The queue dispatcher sends the turn into pi only when dispatch is safe
|
|
66
84
|
|
|
67
85
|
### Queue Safety Model
|
|
68
86
|
|
|
@@ -73,7 +91,16 @@ Queued items now use two explicit dimensions:
|
|
|
73
91
|
- `kind`: prompt vs control
|
|
74
92
|
- `queueLane`: control vs priority vs default
|
|
75
93
|
|
|
76
|
-
|
|
94
|
+
Admission contract:
|
|
95
|
+
|
|
96
|
+
| Admission | Examples | Queue shape | Dispatch rank |
|
|
97
|
+
| --------------------- | ---------------------------------------------------- | -------------------------------------------------------------------- | ------------- |
|
|
98
|
+
| Immediate execution | `/compact`, `/stop`, `/help`, `/start` | Does not enter the Telegram queue | N/A |
|
|
99
|
+
| Control queue | `/status`, `/model`, model-switch continuation turns | `queueLane: control`; accepts control items and continuation prompts | 0 |
|
|
100
|
+
| Priority prompt queue | A waiting prompt promoted by `👍` | `kind: prompt`, `queueLane: priority` | 1 |
|
|
101
|
+
| Default prompt queue | Normal Telegram text/media turns | `kind: prompt`, `queueLane: default` | 2 |
|
|
102
|
+
|
|
103
|
+
The command action itself carries its execution mode, and the queue domain exposes lane contracts for admission mode, dispatch rank, and allowed item kinds. Queue append and planning paths validate lane admission so a malformed control/default or other invalid lane pairing fails predictably instead of silently changing priority. This lets synthetic control actions and Telegram prompts share one stable ordering model while still rendering distinctly in status output. In the pi status bar queue preview, priority prompts are marked with `⬆` while control items keep their own control-specific summary markers such as `⚡`.
|
|
77
104
|
|
|
78
105
|
A dispatched prompt remains in the queue until `agent_start` consumes it. That keeps the active Telegram turn bound correctly for previews, attachments, abort handling, and final reply delivery.
|
|
79
106
|
|
|
@@ -85,7 +112,7 @@ Dispatch is gated by:
|
|
|
85
112
|
- `ctx.isIdle()` being true
|
|
86
113
|
- `ctx.hasPendingMessages()` being false
|
|
87
114
|
|
|
88
|
-
This prevents queue races around rapid follow-ups, `/compact`, and mixed local plus Telegram activity.
|
|
115
|
+
This prevents queue races around rapid follow-ups, `/compact`, and mixed local plus Telegram activity. The dispatch controller also serializes asynchronous control items, so a queued `/status` or `/model` action must settle before the next queued action can dispatch.
|
|
89
116
|
|
|
90
117
|
### Abort Behavior
|
|
91
118
|
|
|
@@ -100,13 +127,13 @@ Key rules:
|
|
|
100
127
|
- Rich text should render cleanly in Telegram chats
|
|
101
128
|
- Real code blocks must remain literal and escaped
|
|
102
129
|
- Supported absolute HTTP(S) and mailto links should stay clickable, with generated HTML attributes escaped separately from text content, while unsupported link forms such as unresolved references, footnotes, or relative links without a known base should degrade safely instead of producing broken Telegram anchors
|
|
103
|
-
- Markdown tables should keep their internal separators but drop the outer left and right borders when rendered as monospace blocks so narrow Telegram clients keep more usable width
|
|
130
|
+
- Markdown tables should keep their internal separators but drop the outer left and right borders when rendered as monospace blocks so narrow Telegram clients keep more usable width; table padding should count grapheme/display width for multi-codepoint emoji, combining marks, and wide Unicode where possible, and the Telegram before-agent prompt suffix also asks the assistant to prefer narrow table columns because many chats are read on phone-width screens
|
|
104
131
|
- Unordered Markdown lists should render with a monospace `-` marker and ordered Markdown lists should render with monospace numeric markers so list indentation stays more predictable on narrow Telegram clients
|
|
105
132
|
- Real Markdown task-list items should render with checkbox markers, while standalone `[x]` and `[ ]` prose should stay literal instead of being reinterpreted as checklists
|
|
106
133
|
- Nested Markdown quotes should flatten into one Telegram blockquote with added non-breaking-space indentation because Telegram does not render nested blockquotes reliably
|
|
107
134
|
- Original blank-line spacing between Markdown blocks should stay intact in both preview and final rendering instead of being collapsed to one generic block separator, while headings should still keep readable separation from following blocks such as code fences even when source Markdown omits a blank line
|
|
108
135
|
- Long replies, including raw HTML-mode replies used by interactive/status flows, must be split below Telegram's 4096-character limit
|
|
109
|
-
-
|
|
136
|
+
- Raw HTML chunking lives with the rendering helpers in `/lib/rendering.ts` and should preserve/reopen active tags across chunk boundaries where possible
|
|
110
137
|
- Preview rendering uses stable top-level Markdown blocks for rich Telegram HTML and appends the still-growing tail conservatively as readable plain text so the preview stays valid even when the answer is incomplete
|
|
111
138
|
|
|
112
139
|
The renderer is a Telegram-specific formatter, not a general Markdown engine, so rendering changes should be treated as regression-prone.
|
|
@@ -124,7 +151,9 @@ Preferred order:
|
|
|
124
151
|
|
|
125
152
|
Draft streaming can remain as a plain-text fallback path, but rich Telegram previews are driven through editable messages and stable-block snapshot selection.
|
|
126
153
|
|
|
127
|
-
|
|
154
|
+
Telegram prompt responses use explicit delivery context to attach outbound text, rich previews, errors, attachment notices, and uploads as Telegram replies to the source prompt when possible. Reply metadata is opt-in per delivery path, uses `reply_parameters` with `allow_sending_without_reply: true`, and is applied only to the first chunk of split long responses; continuation chunks are sent as normal adjacent messages. Media-group turns reply to the turn's representative `replyToMessageId`, not to every source message in the group.
|
|
155
|
+
|
|
156
|
+
Outbound files are sent only after the active Telegram turn completes, must be staged through the `telegram_attach` tool, are staged atomically per tool call, are checked against a default 50 MiB limit configurable through `PI_TELEGRAM_OUTBOUND_ATTACHMENT_MAX_BYTES` or `TELEGRAM_MAX_ATTACHMENT_SIZE_BYTES`, and use file-backed multipart blobs so large sends do not require preloading whole files into memory.
|
|
128
157
|
|
|
129
158
|
## Interactive Controls
|
|
130
159
|
|
|
@@ -136,6 +165,8 @@ Current operator controls include:
|
|
|
136
165
|
- Inline status buttons for model and thinking adjustments, applying idle selections immediately while still respecting busy-run restart rules; model-menu inputs are cached briefly and stored inline-menu states are pruned by TTL/LRU so old keyboards expire predictably
|
|
137
166
|
- `/model` for interactive model selection, queued as a high-priority control item when needed and supporting in-flight restart of the active Telegram-owned run on a newly selected model
|
|
138
167
|
- `/compact` for Telegram-triggered pi session compaction when the bridge is idle
|
|
168
|
+
- `/stop` for aborting the active Telegram-owned run
|
|
169
|
+
- `/telegram-status` for pi-side diagnostics as grouped line-by-line sections separated by blank lines: connection, polling, execution, queue, and the recent redacted runtime/API event ring. These sections include polling state, last update id, active turn source ids, pending dispatch, compaction state, active tool count, pending model-switch state, total queue depth, and queue-lane counts. The event ring records transport/API, polling/update, prompt-dispatch, control-action, typing, compaction, setup, session-lifecycle, and attachment queue/delivery failures; benign unchanged edit responses and unsupported empty draft-clear attempts are filtered out so expected preview transport noise does not obscure real failures
|
|
139
170
|
- Queue reactions using `👍` and `👎`, with `👎` acting as the canonical queue-removal path because ordinary Telegram DM message deletions are not exposed through the Bot API polling path this bridge uses
|
|
140
171
|
|
|
141
172
|
## In-Flight Model Switching
|
package/docs/locks.md
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# Extension Locks Standard
|
|
2
|
+
|
|
3
|
+
`locks.json` is a shared registry for singleton pi extensions.
|
|
4
|
+
|
|
5
|
+
Path:
|
|
6
|
+
|
|
7
|
+
```text
|
|
8
|
+
~/.pi/agent/locks.json
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Shape
|
|
12
|
+
|
|
13
|
+
```json
|
|
14
|
+
{
|
|
15
|
+
"@llblab/pi-telegram": {
|
|
16
|
+
"pid": 2590864,
|
|
17
|
+
"cwd": "/home/user/project"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Top-level keys are extension identities. Values are JSON objects owned by that extension.
|
|
23
|
+
|
|
24
|
+
## Identity key
|
|
25
|
+
|
|
26
|
+
Use the most stable available identity:
|
|
27
|
+
|
|
28
|
+
1. `package.json/name` for npm-style pi packages
|
|
29
|
+
2. Directory name when the extension entrypoint is `index.ts` but there is no package name
|
|
30
|
+
3. File basename when the extension is a single file
|
|
31
|
+
|
|
32
|
+
For npm-style package extensions, the canonical value is the `package.json` `name`. Implementations may keep that value as a small local constant when it is clearer than runtime package introspection. The fallback rules are only for unpackaged extensions.
|
|
33
|
+
|
|
34
|
+
Examples:
|
|
35
|
+
|
|
36
|
+
```text
|
|
37
|
+
extensions/pi-telegram/package.json name=@llblab/pi-telegram -> @llblab/pi-telegram
|
|
38
|
+
extensions/pi-telegram/index.ts without package.json -> pi-telegram
|
|
39
|
+
extensions/pi-telegram.ts -> pi-telegram
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Required fields
|
|
43
|
+
|
|
44
|
+
```json
|
|
45
|
+
{
|
|
46
|
+
"pid": 2590864
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
`pid` is the process that currently owns the singleton runtime. `cwd` should be stored when ownership is tied to a pi session directory.
|
|
51
|
+
|
|
52
|
+
During a user-initiated start/connect event, an extension should:
|
|
53
|
+
|
|
54
|
+
1. Read its lock entry
|
|
55
|
+
2. If `pid` is stale, replace the entry
|
|
56
|
+
3. If `pid` and `cwd` match the current pi instance, refresh or keep the entry
|
|
57
|
+
4. If a live external owner exists, ask interactively whether to move singleton ownership here
|
|
58
|
+
|
|
59
|
+
## Acquisition timing
|
|
60
|
+
|
|
61
|
+
Lock writes must be caused by an explicit user-initiated runtime event, such as `/wakeup-start`, `/telegram-connect`, or a confirmed takeover prompt.
|
|
62
|
+
|
|
63
|
+
Extension initialization and session-start hooks may read `locks.json`, update local status, install ownership watchers, and resume local work when the existing lock already points at the current `pid`/`cwd`. After a full process restart, a session-start hook may replace a stale lock from the same `cwd` to restore explicitly requested ownership. They must not create ownership from an inactive lock, take over a live external owner, or replace a stale lock from another directory by themselves. Such locks should stay visible as state until the user runs the start/connect command. Session replacement should suspend local runtime work and ownership watchers without releasing the lock, so the next session in the same `pid`/`cwd` can resume from explicit ownership.
|
|
64
|
+
|
|
65
|
+
## Optional fields
|
|
66
|
+
|
|
67
|
+
Extensions may add compact fields when useful:
|
|
68
|
+
|
|
69
|
+
```json
|
|
70
|
+
{
|
|
71
|
+
"pid": 2590864,
|
|
72
|
+
"cwd": "/repo/project",
|
|
73
|
+
"mode": "connected",
|
|
74
|
+
"updatedAt": "2026-04-28T00:00:00.000Z"
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Do not print optional fields in normal UI unless they help the user act.
|
|
79
|
+
|
|
80
|
+
## Ownership rules
|
|
81
|
+
|
|
82
|
+
- One top-level key per singleton extension
|
|
83
|
+
- An extension may only mutate its own key
|
|
84
|
+
- Other keys must be preserved exactly
|
|
85
|
+
- If `cwd` is present, active-here ownership means both `pid` and `cwd` match the current pi instance
|
|
86
|
+
- Human-readable diagnostics should say `active here`, `active elsewhere`, or `stale`
|
|
87
|
+
- Debug data belongs in `locks.json`, not in normal status output
|
|
88
|
+
|
|
89
|
+
## Runtime status
|
|
90
|
+
|
|
91
|
+
Singleton extensions with footer/status presence should expose quiet but explicit local state. For example, pi-wakeup uses:
|
|
92
|
+
|
|
93
|
+
- `wakeup off` when this pi instance does not own the singleton runtime
|
|
94
|
+
- `wakeup on` when this pi instance owns the runtime but has no pending wake-up detail to show
|
|
95
|
+
- `wakeup [16:32:39]` when the runtime owns scheduled work and can show the next countdown
|
|
96
|
+
|
|
97
|
+
## Interactive takeover
|
|
98
|
+
|
|
99
|
+
Start/connect commands should make singleton moves easy:
|
|
100
|
+
|
|
101
|
+
1. If no live owner exists, take ownership without an extra prompt
|
|
102
|
+
2. If a live external owner exists, ask whether to move singleton ownership to this pi instance
|
|
103
|
+
3. On confirmation, write the current `{ "pid": ..., "cwd": ... }` to this extension's key in `locks.json`
|
|
104
|
+
4. The previous owner must notice that `locks.json` no longer points at its own `pid`/`cwd` and stop local runtime work without deleting the new lock
|
|
105
|
+
|
|
106
|
+
Takeover prompts should use the extension name as the dialog title, then the question, a blank line, and source/target lines:
|
|
107
|
+
|
|
108
|
+
```text
|
|
109
|
+
pi-telegram
|
|
110
|
+
move singleton lock here?
|
|
111
|
+
|
|
112
|
+
from: pid 2590864, cwd /old
|
|
113
|
+
to: /new
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Avoid repeating the extension name in the body. Color is encouraged: extension title/name accent, question warning, `from:`/`to:` muted.
|
|
117
|
+
|
|
118
|
+
The previous owner may use `fs.watch`, mtime polling, or an existing status/timer tick. Long-lived watchers should compare against a snapshotted `pid`/`cwd` identity rather than a live pi context object, because session replacement such as `/new` makes captured contexts stale. The important contract is graceful local shutdown after ownership mismatch.
|
|
119
|
+
|
|
120
|
+
## Reset
|
|
121
|
+
|
|
122
|
+
Delete `~/.pi/agent/locks.json` to reset singleton runtime ownership for all participating extensions without deleting their configuration files such as `telegram.json`.
|
|
123
|
+
|
|
124
|
+
## Atomicity
|
|
125
|
+
|
|
126
|
+
Current baseline is read-modify-write JSON. This is enough for interactive pi singleton starts.
|
|
127
|
+
|
|
128
|
+
If multiple instances may start concurrently, use an atomic helper later:
|
|
129
|
+
|
|
130
|
+
- Lock file around `locks.json`, or
|
|
131
|
+
- Temp file + rename with conflict checks, or
|
|
132
|
+
- OS-level exclusive open for a short critical section
|
|
133
|
+
|
|
134
|
+
## Migration
|
|
135
|
+
|
|
136
|
+
Migrations from legacy lock files or legacy keys should be one-off cleanup work. Runtime ownership should read and write only `locks.json` under the canonical identity key.
|