Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2aefc7ef5a | ||
|
|
52c9fbeaf3 | ||
|
|
a46fb916a0 | ||
|
|
a9364a344a | ||
|
|
0852274102 | ||
|
|
7a54e2dbeb | ||
|
|
897c69006e |
No files matched your search
+14
-1
@@ -2,7 +2,20 @@
|
||||
|
||||
## Unreleased
|
||||
|
||||
### 0.22.0-evandro.1 — Evandro Fonseca Junior fork (oh-my-pi-telegram)
|
||||
- `omp Agent Directory`: Resolve the agent directory from the host's own `getAgentDir()` (registered by the composition root via `setHostAgentDirResolver`) instead of sniffing `execPath`/`argv[1]` for an `omp` prefix. omp installed through Bun runs as `bun …/@oh-my-pi/pi-coding-agent/dist/cli.js`, which matched neither, so with `telegram.json` in `~/.omp/agent` the bridge never connected and wrote its log and bus socket under `~/.pi/agent`. `PI_CODING_AGENT_DIR` still wins; with no host registered the fallback stays `~/.pi/agent`. Impact: a bot token saved by `/telegram-setup` lives in omp's own agent directory and is found again. `lib/paths.ts` stays free of Pi SDK imports, which cost about a second of startup per importing process.
|
||||
- `omp Settled Boundary`: omp never emits `agent_settled`, the only place the activity runtime finalized a turn. On omp the first terminal `agent_end` (no `willContinue: true`, which omp sets for retries, compaction continuations, todo/plan reminders and async wakes) now drives `onAgentSettled`; hosts that emit `agent_settled` natively are unchanged. Impact: Activity API consumers receive `agent-settled` and the activity id no longer leaks across turns.
|
||||
- `omp Current Model`: omp never emits `model_select`, so the stored current model went stale after a local `/model` switch, and omp builds command contexts by spreading a base context, which freezes `ctx.model` at the moment of `/telegram-connect`. On omp the live `ctx.models.current()` now wins over both, and the stored value no longer overrides it. Impact: the Telegram model menu marks the model omp is actually using.
|
||||
- `omp Settings Manager`: omp's `SettingsManager.create()` returns its own `Settings`, which has no `reload()` or `getEnabledModels()`; rendering the model menu and `/status` failed with `settingsManager.reload is not a function`, so no menu reached Telegram. The SDK boundary now adapts it (`reloadFromDisk()`, global `enabledModels` read) and scoped-model persistence is not offered on omp. Impact: the operator menus render on omp.
|
||||
- `Time Injection`: Reject non-finite configured intervals and resume time annotations after the wall clock moves backwards.
|
||||
- `Paths`: Resolve diagnostic paths from the current agent directory and profile instead of reporting hard-coded `~/.pi` locations.
|
||||
- `Security`: Apply the `brace-expansion` `5.0.12` override at both root and coding-agent scopes to address the latest recursion and expansion denial-of-service advisories.
|
||||
|
||||
- **Security overrides refreshed** — `protobufjs` `7.6.4` → `7.6.6`, `undici` `8.5.0` → `8.10.2`, and a new `brace-expansion` `5.0.9` override keep `npm audit` at 0 vulnerabilities; the `validate` gate (`typecheck` → `test` → `audit` → `pack:check`) passes again.
|
||||
- **README rewritten** to the repository audit standard: verified install/build/test commands, the real `omp install` syntax, omp agent-directory resolution, and an explicit limitations section.
|
||||
- **Release notes extraction fixed** — the `0.22.0-evandro.1` fork section promoted from `###` to `##` so `.github/workflows/release.yml` can build release notes for tag `v0.22.0-evandro.1`.
|
||||
- **Bus ledger test made timing-independent** — `tests/bus.test.ts` no longer assumes the first bus envelope reaches the server within a fixed 5 ms, which failed on a slower CI runner (`0 !== 1`). The `Validate` workflow is green again on `main`.
|
||||
|
||||
## 0.22.0-evandro.1 — Evandro Fonseca Junior fork (oh-my-pi-telegram)
|
||||
|
||||
- **Repackaged as `@evandrodevbr/oh-my-pi-telegram`** — version suffix `-evandro.1` keeps the 0.22.0 source line (SHA `afe09c5`) traceable to upstream `llblab/pi-telegram` while making the fork unambiguous in the `omp` extension registry.
|
||||
- **Oh My Pi (omp) compatibility metadata** — added an `omp` field in `package.json` (displayName, homepage, `compatibleWith: { "@oh-my-pi/pi-coding-agent": ">=16.5.2" }`) so the fork is discoverable as a first-class extension for `can1357/oh-my-pi` (`omp >= 16.5.2`).
|
||||
|
||||
@@ -1,339 +1,259 @@
|
||||
# oh-my-pi-telegram
|
||||
|
||||

|
||||
**Telegram runtime adapter for live [Oh My Pi](https://github.com/can1357/oh-my-pi) (`omp`) sessions: send prompts, queue work, watch previews and collect artifacts from a private Telegram DM while the agent keeps running in the terminal.**
|
||||
|
||||
**Telegram companion hub for live [Oh My Pi](https://github.com/can1357/oh-my-pi) (omp) sessions.**
|
||||
[](https://github.com/evandrodevbr/oh-my-pi-telegram/actions/workflows/validate.yml)
|
||||

|
||||

|
||||

|
||||

|
||||
|
||||
> **This is the Evandro Fonseca Junior fork** of [`@llblab/[email protected]`](https://github.com/llblab/pi-telegram/tree/v0.22.0) (SHA `afe09c5`), repackaged and validated as a drop-in extension for [`can1357/oh-my-pi`](https://github.com/can1357/oh-my-pi) — the AI coding agent for the terminal.
|
||||
>
|
||||
> Upstream lineage: `badlogic/pi-telegram` → `llblab/pi-telegram` (0.22.0) → **`evandrodevbr/oh-my-pi-telegram`** (this fork).
|
||||

|
||||
|
||||
## Oh My Pi (omp) Compatibility
|
||||
> Fork of [`@llblab/[email protected]`](https://github.com/llblab/pi-telegram/tree/v0.22.0) (SHA `afe09c5`), republished as `@evandrodevbr/oh-my-pi-telegram` for `omp`.
|
||||
> Lineage: `badlogic/pi-telegram` → `llblab/pi-telegram` (0.22.0) → **this fork**.
|
||||
> The fork is based on upstream 0.22.0, with `omp` compatibility metadata and local runtime fixes recorded in `CHANGELOG.md`.
|
||||
|
||||
This fork is built and tested against:
|
||||
## About
|
||||
|
||||
| Component | Version | Status |
|
||||
| --- | --- | --- |
|
||||
| `can1357/oh-my-pi` (CLI: `omp`) | `>=16.5.2` (`@oh-my-pi/pi-coding-agent`) | ✅ verified |
|
||||
| `@earendil-works/pi-{ai,agent-core,coding-agent}` | `>=0.80.6` | ✅ verified (peerDeps preserved from upstream) |
|
||||
| `node` | `>=22.19.0` | ✅ verified on Node 24.15.0 |
|
||||
| Telegram Bot API | current | ✅ unchanged from upstream |
|
||||
Supervising a long agent run means staying at the keyboard, because the work lives in the terminal session. This extension removes that constraint for the part that does not need a terminal: it binds a private Telegram DM to a running `omp` instance, so text, replies, images, files and voice notes sent from the phone become ordinary turns in that instance's active session, and answers, artifacts, buttons and voice come back to the chat.
|
||||
|
||||
### Install into omp
|
||||
What it does:
|
||||
|
||||
- Accepts prompts from Telegram (text, replies, edits, images, files, albums, voice) and injects them into the active `omp` session.
|
||||
- Queues turns while the agent is busy instead of interrupting the running turn, with menu controls to inspect, promote, delete or force the next item.
|
||||
- Streams native "typing"/draft previews and delivers final answers as Telegram Rich Markdown, plus files through `telegram_attach`.
|
||||
- Exposes an operator menu (`/start`) for status, model, thinking level, settings, queue, prompt templates and diagnostics.
|
||||
- Pairs exactly one Telegram owner; other users are ignored.
|
||||
|
||||
What it deliberately is not: a remote terminal, a PTY, a shell, a process launcher or a session browser. It does not spawn hidden `omp` processes and does not forward arbitrary Telegram slash commands into the TUI.
|
||||
|
||||
## How it works
|
||||
|
||||
```
|
||||
Telegram DM ──▶ getUpdates (single polling owner)
|
||||
│
|
||||
▼
|
||||
inbound layer ──▶ Telegram turn ──▶ queue / active dispatch
|
||||
│ │
|
||||
│ ▼
|
||||
│ omp active session (model turn)
|
||||
▼ │
|
||||
inbound files to <agent-dir>/tmp/telegram ▼
|
||||
streaming preview / native active status
|
||||
│
|
||||
▼
|
||||
final Rich Markdown reply ──▶ Telegram DM
|
||||
(+ files, voice, inline buttons)
|
||||
```
|
||||
|
||||
- One live `omp` instance owns polling for a profile; each Telegram destination follows that instance and sends prompts into its currently active session (it is not bound to one session file).
|
||||
- A second `omp` instance started while Telegram private-chat Threaded Mode is available registers as a follower through a local leader/follower bus, each one behind its own Telegram thread.
|
||||
- Companion extensions can register commands, menu sections, status rows, update/callback handlers and voice providers without owning a second bot loop.
|
||||
|
||||
## Stack
|
||||
|
||||
| Layer | Choice |
|
||||
| --- | --- |
|
||||
| Language | TypeScript 7, strict, run directly by the `omp`/Pi runtime (no build step) |
|
||||
| Runtime | Node.js >= 22.19.0 (`node:test` + `--experimental-strip-types` for the suite) |
|
||||
| Host agent | `omp` (`@oh-my-pi/pi-coding-agent` >= 16.5.2); peer deps `@earendil-works/pi-{ai,agent-core,coding-agent}` >= 0.80.6, `@sinclair/typebox` |
|
||||
| Telegram transport | Direct Telegram Bot API over `undici` HTTP calls (long polling, Rich Messages, files, voice) |
|
||||
| Transport hardening | Dependency `overrides` pin security-patched `protobufjs`, `undici`, `ws`, `brace-expansion` for the transitive tree |
|
||||
| Package manager | npm (`package-lock.json` is the lockfile CI uses); `bun.lock` is a leftover from upstream |
|
||||
| CI | GitHub Actions: `Validate` (npm ci + `npm run validate`), `Release` (tag `v*.*.*` → GitHub release from `CHANGELOG.md`) |
|
||||
|
||||
## Requirements
|
||||
|
||||
- Node.js `>= 22.19.0` (declared in `engines`; verified here on 24.20.0)
|
||||
- npm `>= 10` for the validation gates (verified on 11.19.0). `bun` also works for running the extension, but the gates are npm-based.
|
||||
- `omp` `>= 16.5.2` (verified with `omp` 18.4.8) or a Pi runtime exposing the same `@earendil-works/pi-*` extension API
|
||||
- A Telegram bot token from [@BotFather](https://t.me/BotFather)
|
||||
|
||||
## Quick start
|
||||
|
||||
The extension is **not published to npm yet** (`npm view @evandrodevbr/oh-my-pi-telegram` returns 404), so clone the repository and install from the local checkout.
|
||||
|
||||
```bash
|
||||
# From npm (after this fork is published):
|
||||
omp install npm:@evandrodevbr/oh-my-pi-telegram
|
||||
# Local development checkout, run the gates first
|
||||
git clone https://github.com/evandrodevbr/oh-my-pi-telegram
|
||||
cd oh-my-pi-telegram
|
||||
npm ci
|
||||
npm run validate
|
||||
|
||||
# From git (always works):
|
||||
omp install git:github.com/evandrodevbr/oh-my-pi-telegram
|
||||
# Install into omp from the checkout (verified working; --link is NOT a valid flag)
|
||||
omp install /absolute/path/to/oh-my-pi-telegram
|
||||
|
||||
# From a local checkout (development):
|
||||
omp install --link /path/to/oh-my-pi-telegram
|
||||
# Confirm omp sees the extension and reads its `omp` manifest field
|
||||
omp plugin list --json
|
||||
omp plugin doctor
|
||||
```
|
||||
|
||||
`omp plugin --help` documents these plugin sources: a local path (`./path`, `../path`, `/abs`, `~/path`, symlinked), `github:user/repo[#ref]` (also `gitlab:`, `bitbucket:`, `codeberg:`, `sourcehut:`), a full git URL (`https://github.com/user/repo`), `name@marketplace`, and npm specs (`pkg`, `[email protected]`). Prefer the local-path form: the package is not on npm, and omp's plugin source resolver currently rejects `npm` sources with `npm plugin sources are not yet supported. Use git-based sources instead.`
|
||||
|
||||
Then inside `omp`:
|
||||
|
||||
```bash
|
||||
/telegram-setup # paste bot token
|
||||
/telegram-connect # activate polling for this omp instance
|
||||
```
|
||||
|
||||
In your Telegram bot DM:
|
||||
|
||||
```text
|
||||
/start
|
||||
/telegram-setup # paste the bot token (offers a saved token as default)
|
||||
/telegram-connect # start polling for this instance
|
||||
```
|
||||
|
||||
The first user becomes the allowed owner.
|
||||
Finally, open the bot DM and send `/start`. The first Telegram user to write becomes the allowed owner.
|
||||
|
||||
### Why this fork exists
|
||||
Config is written to `<agent-dir>/telegram.json`. The agent directory is `PI_CODING_AGENT_DIR` when set, otherwise the directory the host reports through `getAgentDir()`: `~/.pi/agent` on Pi, and on `omp` its profile-aware directory (`~/.omp/agent` by default). The executable name and `argv` are not inspected.
|
||||
|
||||
`llblab/[email protected]` targets the **Pi extension platform** (`@earendil-works/pi-*`). `omp` ships the same platform but republishes it under `@oh-my-pi/pi-coding-agent` with the additional harness tooling (LSP, DAP, hashline, subagents, advisor, TTSR, …). This fork:
|
||||
## Usage
|
||||
|
||||
1. **Renames the npm package** to `@evandrodevbr/oh-my-pi-telegram` so it is unambiguous in the `omp` extension registry.
|
||||
2. **Keeps the upstream `peerDependencies`** on `@earendil-works/*` (these are the canonical runtime packages that both Pi and omp consume).
|
||||
3. **Adds an `omp` field** in `package.json` with explicit compatibility metadata for downstream extension discovery.
|
||||
4. **Re-runs every upstream gate** (`typecheck` → `test` → `audit` → `pack:check`) on the exact `v0.22.0` source — 1229 tests pass, 0 vulnerabilities, pack clean.
|
||||
5. **Adds this README section** so the fork is discoverable as the Telegram bridge for omp.
|
||||
|
||||
## What `pi-telegram` (and this fork) does
|
||||
|
||||
`pi-telegram` turns a private Telegram DM into a mobile operator surface for Pi/omp. It accepts prompts, queues work, streams readable previews, delivers final replies and files, exposes safe controls, and lets companion extensions add Telegram-native capabilities without owning a second bot loop.
|
||||
|
||||
It is a **runtime adapter**, not a remote terminal. Start or supervise work in the `omp` TUI, then continue from Telegram while away from the keyboard. Each Telegram destination follows a running `omp` instance and sends prompts into that instance's currently active session; it is not permanently bound to one session file or session identity. The bridge preserves Pi session semantics instead of pretending Telegram is a PTY, shell, process launcher, or session browser. That boundary is the product: Telegram gets safe runtime handles, not raw terminal power.
|
||||
|
||||
This fork is derived from [`llblab/[email protected]`](https://github.com/llblab/pi-telegram/tree/v0.22.0) (SHA `afe09c5`), which itself is an actively maintained fork of [`badlogic/pi-telegram`](https://github.com/badlogic/pi-telegram).
|
||||
|
||||
## Install (npm/git, same as upstream)
|
||||
|
||||
From npm:
|
||||
|
||||
```bash
|
||||
pi install npm:@evandrodevbr/oh-my-pi-telegram
|
||||
```
|
||||
|
||||
From git:
|
||||
|
||||
```bash
|
||||
pi install git:github.com/evandrodevbr/oh-my-pi-telegram
|
||||
```
|
||||
|
||||
The 0.21 extension platform requires Pi `0.80.6` or newer. Its Activity API uses the public `agent_settled` lifecycle event to keep retries/continuations under one activity identity and release that identity only after the run fully settles.
|
||||
|
||||
## Quick Start
|
||||
|
||||
### 1. Create a Telegram bot
|
||||
|
||||
1. Open [@BotFather](https://t.me/BotFather).
|
||||
2. Run `/newbot`.
|
||||
3. Pick a name and username.
|
||||
4. Copy the bot token.
|
||||
|
||||
### 2. Configure Pi/omp
|
||||
|
||||
Run this inside Pi/omp:
|
||||
|
||||
```bash
|
||||
/telegram-setup
|
||||
```
|
||||
|
||||
Paste the bot token. If `~/.pi/agent/telegram.json` already contains a saved token, setup offers it as the default. If no saved token exists, setup can prefill from `TELEGRAM_BOT_TOKEN`, `TELEGRAM_BOT_KEY`, `TELEGRAM_TOKEN`, or `TELEGRAM_KEY`. Named profiles are optional; the ordinary `/telegram-setup` and `/telegram-connect` flow keeps using the default profile. Use `/telegram-setup <name>` only when you want an additional bot profile. Cancelling or failing named-profile token validation leaves the currently active profile and polling runtime unchanged; setup reports the profile as saved and connected only after polling startup succeeds.
|
||||
|
||||
### 3. Connect this Pi/omp instance and its active session
|
||||
|
||||
```bash
|
||||
/telegram-connect
|
||||
```
|
||||
|
||||
The connected instance owns Telegram polling. Use `/telegram-connect <name>` to activate a named profile. Each profile is a parallel bot runtime with isolated polling, diagnostics, Threaded Mode state, and local bus transport; the unnamed default profile keeps legacy paths. In classic mode each profile uses a singleton lock. When Telegram private-chat Threaded Mode is available, one live instance becomes the profile's leader and later visible Pi instances register as followers.
|
||||
|
||||
### 4. Pair your Telegram account
|
||||
|
||||
Open the bot DM and send:
|
||||
|
||||
```text
|
||||
/start
|
||||
```
|
||||
|
||||
The first Telegram user to message the bot becomes the allowed owner. Other users are ignored.
|
||||
|
||||
## What It Feels Like
|
||||
|
||||
- Start a task in the terminal, walk away, and keep supervising it from your phone.
|
||||
- Send another prompt while Pi is busy; it becomes a queued Telegram turn instead of interrupting the active run.
|
||||
- Open `/start` to inspect status, model, thinking, settings, prompt templates, and queue controls.
|
||||
- Send voice, images, files, replies, edits, or media groups; the bridge turns them into Pi context.
|
||||
- Ask for an artifact; `telegram_attach` returns it through the active reply or direct Telegram delivery.
|
||||
- In Threaded Mode, run multiple visible Pi instances through one bot, each with its own Telegram thread.
|
||||
- Configure named profiles to run independent Telegram bots from the same Pi agent directory without sharing transport or routing state.
|
||||
|
||||
## Product Model
|
||||
|
||||
| Lens | What `pi-telegram` owns |
|
||||
| --- | --- |
|
||||
| Operator companion | A phone-width control surface for the active session of a running Pi instance |
|
||||
| Runtime adapter | Telegram targets mapped to Pi instances, then into each instance's current session lifecycle, queueing, previews, final replies, and artifacts |
|
||||
| Telegram UI harness | Menus, settings, callbacks, Rich Markdown, drafts, active status, buttons, voice, and files |
|
||||
| Multi-instance organism | One leader plus explicit visible followers routed through Telegram private-chat threads |
|
||||
| Extension platform | Commands, sections, status rows, update handlers, inbound/outbound handlers, and voice providers |
|
||||
| Safety boundary | No hidden Pi processes, no fake terminal, no PTY tricks, no arbitrary TUI slash-command forwarding |
|
||||
|
||||
## Feature Showcase
|
||||
|
||||
`pi-telegram` is intentionally broad: it is a Telegram-shaped runtime surface, not only a message relay. This catalogue keeps the practical feature surface visible while detailed contracts stay in `/docs`.
|
||||
|
||||
| Surface | What you can do | Why it matters |
|
||||
| --- | --- | --- |
|
||||
| Prompt intake | Send text, replies, edits, images, files, albums, voice notes, and handler output into Pi. | Telegram becomes a real mobile input surface with file/context references, not just a text tunnel. |
|
||||
| Queue control | Inspect waiting turns, delete stale work, promote important prompts, continue, abort, stop, or force the next queued item. | Long Pi tasks keep running while new mobile prompts stay visible and controllable instead of interrupting or disappearing. |
|
||||
| Operator menu | Use `/start` for status, prompt templates, model, thinking, settings, queue, extension sections, and diagnostics. | The bot is an operator panel, not a command cheat sheet. |
|
||||
| Prompt templates | Run Pi prompt templates as Telegram-safe commands such as `/fix_tests`. | Reusable local workflows become phone-accessible without exposing arbitrary terminal commands. |
|
||||
| Model and thinking | Switch model or reasoning level from Telegram through safe continuation flows. | Mobile control can adjust execution strategy without tearing down the current session. |
|
||||
| Compaction | Confirm `/compact`, show native active status during compaction, and preserve Telegram-owned turn semantics. | Context maintenance is visible and safe from the phone. |
|
||||
| Draft previews | Show Telegram's native `…typing` indicator whenever the connected instance is doing agent work, or enable Rich Draft previews for streamed answer text. | Local prompts, Telegram turns, and autonomous continuations remain visibly active while draft visibility stays independent from final rendering. |
|
||||
| Assistant rendering | Choose Native Rich Markdown or legacy Markdown-to-HTML for final assistant replies. | Renderer compatibility is explicit instead of being conflated with draft previews. |
|
||||
| Bridge UI rendering | Render tool rows, reasoning/technical steps, menus, queue controls, status, settings, diagnostics, and sections through explicit Telegram HTML/plain UI. | Harness-owned surfaces remain operationally predictable and visually distinct from model-authored answers. |
|
||||
| Inbound files | Download inbound files to the Pi agent temp directory with size limits. | Screenshots, PDFs, datasets, and artifacts enter Pi as inspectable local files. |
|
||||
| Outbound artifacts | Return generated files through `telegram_attach` during active turns or explicit direct delivery. | Agents send real artifacts as files, not pasted blobs. |
|
||||
| Voice input | Route audio through configured command-template handlers, programmatic handlers, or STT providers. | Voice notes become usable prompt context. |
|
||||
| Voice output | Use `telegram_voice`, reply modes, configured voice handlers, and TTS providers. | Replies can become Telegram voice messages when the workflow calls for it. |
|
||||
| Buttons | Turn top-level `telegram_button` comments into inline buttons. | Assistant-authored choices become native Telegram interactions. |
|
||||
| Callback routing | Route known callbacks to the owner extension and unknown callbacks back into Pi. | Companion extensions can build UI without polling Telegram themselves. |
|
||||
| Threaded Mode | Run one leader plus visible follower Pi instances through named private-chat threads. | One bot can host a local multi-instance Pi organism without hidden process spawning. |
|
||||
| Reroute and restore | Preserve unknown threads and offer explicit target choices. | Telegram client state can be repaired without silently deleting or hijacking prompts. |
|
||||
| Extension sections | Add menu sections, commands, status rows, settings, callbacks, and delivery helpers from companion extensions. | `pi-telegram` becomes a platform surface for other Pi extensions. |
|
||||
| Runtime diagnostics | Use `/telegram-status` and recent runtime events for connection, role, queue, transport, and failure evidence. | Debugging lives in the operator surface instead of hidden logs only. |
|
||||
| Safety and ownership | Pair one owner, lock transport, scope targets, and reject fake terminal behavior. | Remote access remains explicit, bounded, and understandable. |
|
||||
|
||||
## Core Loop
|
||||
|
||||
```text
|
||||
Telegram message
|
||||
-> Telegram turn
|
||||
-> queue or active dispatch
|
||||
-> Pi agent lifecycle
|
||||
-> streaming preview / native active status
|
||||
-> final Rich Markdown reply
|
||||
-> optional files, voice, buttons, or callback actions
|
||||
```
|
||||
|
||||
The bridge keeps Telegram responsive without stealing Pi's runtime model. Queueing, model changes, compaction, aborts, final delivery, and direct artifact sends all stay scoped to the Pi instance that accepted the work.
|
||||
|
||||
## Telegram Controls
|
||||
|
||||
Use these in the bot DM.
|
||||
### Telegram commands
|
||||
|
||||
| Command | Purpose |
|
||||
| --- | --- |
|
||||
| `/start` | Pair when needed and open the main operator menu |
|
||||
| `/compact` | Confirm and run session compaction when safe |
|
||||
| `/start` | Pair when needed and open the operator menu (status, templates, model, thinking, settings, queue, sections) |
|
||||
| `/compact` | Confirm and run session compaction |
|
||||
| `/next` | Dispatch the next queued turn, aborting first if needed |
|
||||
| `/continue` | Enqueue a priority continuation prompt |
|
||||
| `/abort` | Abort the active run while preserving the queue |
|
||||
| `/abort` | Abort the active run, keep the queue |
|
||||
| `/stop` | Abort the active run and clear waiting Telegram turns |
|
||||
|
||||
Hidden compatibility shortcuts: `/help`, `/status`, `/model`, `/thinking`, `/queue`, and `/settings` jump into the same menu system.
|
||||
`/help`, `/status`, `/model`, `/thinking`, `/queue` and `/settings` are accepted shortcuts into the same menus. Prompt templates registered in `omp` are reachable as `/template_name` commands.
|
||||
|
||||
## Pi Commands
|
||||
|
||||
Run these inside Pi.
|
||||
### omp commands
|
||||
|
||||
| Command | Purpose |
|
||||
| --- | --- |
|
||||
| `/telegram-setup` | Save or update the default bot token |
|
||||
| `/telegram-setup <profile>` | Save or update a named-profile bot token |
|
||||
| `/telegram-connect` | Activate the default profile and acquire its transport ownership |
|
||||
| `/telegram-connect <profile>` | Activate a named profile and acquire its transport ownership |
|
||||
| `/telegram-setup <profile>` | Save or update a named profile's bot token |
|
||||
| `/telegram-connect` | Start polling on the default profile and take transport ownership |
|
||||
| `/telegram-connect <profile>` | Activate a named profile |
|
||||
| `/telegram-disconnect` | Stop polling and release ownership |
|
||||
| `/telegram-status` | Inspect connection, mode, queue, transport, and recent diagnostics |
|
||||
| `/telegram-status` | Connection, mode, queue, transport and recent diagnostics |
|
||||
|
||||
Named profile identifiers contain only lowercase ASCII letters and digits (maximum 32 characters); `default`, `main`, and `active` remain reserved.
|
||||
Profile names: lowercase ASCII letters and digits, up to 32 characters; `default`, `main` and `active` are reserved. Profiles keep isolated polling, diagnostics, Threaded Mode state and local bus transport.
|
||||
|
||||
## Main Surfaces
|
||||
### Tools available to the agent
|
||||
|
||||
### Operator Menu
|
||||
| Tool | Purpose |
|
||||
| --- | --- |
|
||||
| `telegram_help` | Fetch the bridge contract on demand instead of keeping it in every prompt |
|
||||
| `telegram_attach` | Attach a generated file to the active Telegram reply, or send it via direct delivery |
|
||||
| `telegram_message` | Send Markdown text directly to the paired chat or an explicit `chat_id` (`/thread_id`) |
|
||||
|
||||
`/start` opens the Telegram-native control panel: status, prompt-template commands, model selection, thinking level, settings, queue controls, and extension sections. It is the primary Telegram UI; reaction shortcuts are secondary queue affordances.
|
||||
Assistant replies can also carry top-level hidden HTML comments: `<!-- telegram_voice: ... -->` for voice output and `<!-- telegram_button: ... -->` for inline prompt buttons. They are stripped from the visible text.
|
||||
|
||||
### Queue Runtime
|
||||
### Environment configuration
|
||||
|
||||
Messages sent while Pi is busy become queued turns. Priority lanes support control actions and model-switch continuations. Queue controls let you inspect, delete, promote, and dispatch work from Telegram without touching the terminal.
|
||||
|
||||
### Native Rich Markdown
|
||||
|
||||
Rich Markdown is the default model-answer membrane. Complete assistant and guest model replies use Telegram's native Rich Message APIs, while tool-call rows, reasoning/thinking blocks, menus, status rows, queue controls, settings, diagnostics, and other harness-owned surfaces use explicit Telegram HTML/plain rendering. This keeps meaningful model-authored answers visually distinct from bridge-owned operational UI. Two Settings controls keep the layers separate: `Draft previews` toggles live `sendRichMessageDraft` frames, while `Assistant rendering` chooses final-answer delivery (`rich` Native Rich Markdown or `html` legacy Markdown-to-HTML).
|
||||
|
||||
### Files And Artifacts
|
||||
|
||||
Inbound files land under `<agent-dir>/tmp/telegram` and default to a 50 MiB limit. `telegram_attach` is the canonical outbound file path. During Telegram-originated turns it attaches to the active reply; during explicit local/TUI delivery it can send to the paired/default chat or routed Threaded Mode target.
|
||||
|
||||
### Voice And Media
|
||||
|
||||
Voice notes, audio, images, PDFs, and other media can pass through configured inbound handlers, programmatic handlers, or registered STT providers. Outbound voice can use configured `outboundHandlers` or registered TTS providers; `pi-telegram` owns reply policy and Telegram transport, while providers own synthesis.
|
||||
|
||||
### Buttons And Callbacks
|
||||
|
||||
Assistant replies can include top-level hidden `telegram_button` comments. The bridge strips the comments from visible text, renders inline buttons, and routes callbacks back into Pi as queued prompts or extension-owned callback actions.
|
||||
|
||||
### Threaded Mode And Multi-Instance Bus
|
||||
|
||||
Classic private DM mode is the base product mode. When Telegram private-chat Threaded Mode is available, the bridge enables a local leader/follower bus automatically:
|
||||
|
||||
- One live leader owns `getUpdates`.
|
||||
- Followers are visible Pi processes started by the operator.
|
||||
- Each connected instance gets a Telegram thread target.
|
||||
- Follower session replacement automatically reconnects the new session context to the same thread instead of requiring another manual connect.
|
||||
- Unknown threads are preserved and offered explicit reroute/restore choices.
|
||||
- Telegram never launches hidden Pi processes.
|
||||
|
||||
| Mode | Best for | Runtime shape |
|
||||
| --- | --- | --- |
|
||||
| Classic DM | One running Pi instance and its active session controlled from one private bot chat | One polling owner, one queue/runtime surface |
|
||||
| Threaded Mode | Several visible Pi instances sharing one bot | One leader owns transport; each named private-chat thread follows its assigned instance and current session |
|
||||
|
||||
## Environment Configuration
|
||||
|
||||
Most controls live in Pi commands or the Telegram menu. Environment variables remain for bootstrap and transport boundaries:
|
||||
Most controls live in the Telegram menu or in `/telegram-*` commands. Environment variables cover bootstrap and transport boundaries:
|
||||
|
||||
| Area | Variables |
|
||||
| --- | --- |
|
||||
| Bot token bootstrap | `TELEGRAM_BOT_TOKEN`, `TELEGRAM_BOT_KEY`, `TELEGRAM_TOKEN`, `TELEGRAM_KEY` |
|
||||
| HTTP proxy | `HTTP_PROXY`, `HTTPS_PROXY`, `NO_PROXY`, plus `NODE_USE_ENV_PROXY=1` or Node `--use-env-proxy` |
|
||||
| Telegram network family | `PI_TELEGRAM_NETWORK_FAMILY=auto`, `ipv4`, `ipv6`, or `ipv4-fallback` |
|
||||
| Bot token bootstrap (used as `/telegram-setup` prefill) | `TELEGRAM_BOT_TOKEN`, `TELEGRAM_BOT_KEY`, `TELEGRAM_TOKEN`, `TELEGRAM_KEY` |
|
||||
| Agent data root | `PI_CODING_AGENT_DIR` |
|
||||
| Inbound file limit | `PI_TELEGRAM_INBOUND_FILE_MAX_BYTES`, `TELEGRAM_MAX_FILE_SIZE_BYTES` |
|
||||
| Outbound attachment limit | `PI_TELEGRAM_OUTBOUND_ATTACHMENT_MAX_BYTES`, `TELEGRAM_MAX_ATTACHMENT_SIZE_BYTES` |
|
||||
| Telegram network family | `PI_TELEGRAM_NETWORK_FAMILY` = `auto` (default), `ipv4`, `ipv6`, `ipv4-fallback` |
|
||||
| Inbound file limit | `PI_TELEGRAM_INBOUND_FILE_MAX_BYTES`, `TELEGRAM_MAX_FILE_SIZE_BYTES` (default 50 MiB) |
|
||||
| Outbound attachment limit | `PI_TELEGRAM_OUTBOUND_ATTACHMENT_MAX_BYTES`, `TELEGRAM_MAX_ATTACHMENT_SIZE_BYTES` (default 50 MiB) |
|
||||
| HTTP proxy | Not implemented by the bridge: proxies come from the Node runtime itself (`node --use-env-proxy`, or `NODE_USE_ENV_PROXY=1`, honouring `HTTP_PROXY` / `HTTPS_PROXY` / `NO_PROXY`) |
|
||||
|
||||
Defaults are chosen for ordinary private-bot use: saved config in `~/.pi/agent`, inbound temp files in `~/.pi/agent/tmp/telegram`, `assistant: { rendering: "rich", draftPreviews: false }` for assistant answer output, and native Telegram active status for long-running turns.
|
||||
Defaults: config in `<agent-dir>/telegram.json`, inbound temp files in `<agent-dir>/tmp/telegram`, `assistant.rendering = "rich"` (native Rich Markdown) with `assistant.draftPreviews = false`, and native Telegram active status for long turns.
|
||||
|
||||
## Extension Platform
|
||||
### Extension API
|
||||
|
||||
Companion extensions can integrate with Telegram without owning polling or transport:
|
||||
Stable package entrypoints (see `exports` in `package.json`):
|
||||
|
||||
- Register Telegram slash commands.
|
||||
- Add menu sections and settings surfaces.
|
||||
- Add compact status rows.
|
||||
- Deliver target-aware operational views and chat actions from companion code.
|
||||
- Observe normalized assistant, reasoning, tool, compaction, and settlement activity without blocking Pi.
|
||||
- Handle update/callback namespaces.
|
||||
- Provide inbound preprocessing handlers.
|
||||
- Provide outbound voice synthesis.
|
||||
- Use direct delivery helpers for explicit local/TUI sends.
|
||||
| Subpath | Content |
|
||||
| --- | --- |
|
||||
| `.` | Extension entrypoint (`index.ts`) |
|
||||
| `./inbound`, `./outbound`, `./delivery`, `./activity`, `./updates`, `./commands`, `./sections`, `./status`, `./voice`, `./keyboard` | Public companion-extension APIs |
|
||||
|
||||
Stable public entrypoints are documented in [Public API](./docs/public-api.md), [Telegram Delivery API](./docs/delivery.md), [Telegram Activity API](./docs/activity.md), [Extension Sections](./docs/sections.md), [Inbound Handlers](./docs/inbound.md), [Outbound Handlers](./docs/outbound.md), [Updates](./docs/updates.md), and [Voice Integration](./docs/voice.md).
|
||||
Deep `lib/*` imports are intentionally not exported. Contract details are in [`docs/public-api.md`](docs/public-api.md).
|
||||
|
||||
## Safety Boundaries
|
||||
## Production and release
|
||||
|
||||
`pi-telegram` intentionally does not:
|
||||
|
||||
- Spawn hidden Pi follower processes.
|
||||
- Pretend Telegram is a terminal or PTY.
|
||||
- Forward arbitrary Telegram slash commands into the Pi TUI.
|
||||
- Inject raw TTY input or terminal-control sequences.
|
||||
- Replace Pi session lifecycle without an official Pi API.
|
||||
- Let non-owner Telegram users control the bridge.
|
||||
|
||||
Telegram is a companion surface around a live Pi runtime, not a second runtime. It can compact the current session, but it cannot create, resume, fork, browse, or switch sessions until Pi exposes safe public extension APIs for those operations.
|
||||
|
||||
A Telegram prompt is a normal model turn in the active Pi session and therefore inherits that session's active post-compaction context; the bridge does not make token cost proportional only to the new mobile message. Current releases keep per-turn guidance small and transient, with detailed bridge instructions available on demand through `telegram_help` instead of persisted in every user turn. Pi session JSONL contains model history; profile-scoped pi-telegram `logs*.jsonl` contains redacted operational events and is never model context.
|
||||
|
||||
## Documentation Map
|
||||
|
||||
- [Architecture](./docs/architecture.md) — runtime, domains, queue, transport, and Threaded Mode overview.
|
||||
- [Public API](./docs/public-api.md) — package entrypoints and stable companion-extension contracts.
|
||||
- [Telegram Delivery API](./docs/delivery.md) — target-aware operational views, logical message handles, and lifecycle-safe transport.
|
||||
- [Telegram Activity API](./docs/activity.md) — normalized lifecycle events, source identity, non-blocking delivery contexts, and consumer policy examples.
|
||||
- [Inbound Handlers](./docs/inbound.md) — Telegram-to-Pi preprocessing pipelines.
|
||||
- [Outbound Handlers](./docs/outbound.md) — final text/voice/file transformation and delivery.
|
||||
- [Voice Integration](./docs/voice.md) — STT/TTS provider model and reply policies.
|
||||
- [Extension Sections](./docs/sections.md) — Telegram-native companion UI surfaces.
|
||||
- [Updates](./docs/updates.md) — update handler registry and callback interop.
|
||||
- [Multi-Instance Bus](./docs/multi-instance-bus.md) — leader/follower routing in Threaded Mode.
|
||||
- [Locks](./docs/locks.md) — singleton ownership and shared lock conventions.
|
||||
- [UI Style](./docs/ui-style.md) — menu, emoji, labels, dialogs, and inline keyboard standards.
|
||||
- [Callback Namespaces](./docs/callback-namespaces.md) — callback ownership and routing.
|
||||
- [Command Templates](./docs/command-templates.md) — handler command-template conventions.
|
||||
|
||||
The docs index lives at [docs/README.md](./docs/README.md).
|
||||
|
||||
## Development
|
||||
There is no build step: `omp` loads the TypeScript sources directly, and `npm pack` ships them.
|
||||
|
||||
```bash
|
||||
npm run typecheck
|
||||
npm test
|
||||
npm run audit
|
||||
npm run pack:check
|
||||
npm run validate # the gate: typecheck + tests + audit + pack:check
|
||||
npm run pack:check # npm pack --dry-run, 87 files, no secrets
|
||||
```
|
||||
|
||||
Full validation:
|
||||
Release flow: bump `package.json` version, add a matching `## <version>` section to `CHANGELOG.md`, then push the tag.
|
||||
|
||||
```bash
|
||||
npm run validate
|
||||
# after bumping the version and adding the CHANGELOG section
|
||||
git tag v0.22.0-evandro.2 && git push origin v0.22.0-evandro.2
|
||||
```
|
||||
|
||||
Project context:
|
||||
`.github/workflows/release.yml` fails fast unless the tag equals the `package.json` version and `CHANGELOG.md` has a non-empty `## <version>` section; it then runs `gh release create` with those notes. Publishing to npm (`npm publish`, `publishConfig.access = public`) has not been done yet.
|
||||
|
||||
- [AGENTS.md](./AGENTS.md) — engineering and runtime conventions.
|
||||
- [BACKLOG.md](./BACKLOG.md) — release-relevant open work.
|
||||
- [CHANGELOG.md](./CHANGELOG.md) — completed delivery history.
|
||||
## Project structure
|
||||
|
||||
```
|
||||
index.ts extension entrypoint: composition root and runtime wiring
|
||||
api/ public package subpaths (inbound, outbound, delivery, ...)
|
||||
lib/ 54 domain modules: config, paths, polling, queue, routing,
|
||||
rendering, outbound, bus leader/follower, locks, voice, menus
|
||||
tests/ 55 node:test suites (unit + integration, no network)
|
||||
docs/ architecture and per-surface contracts
|
||||
.agents/skills/ project-local agent skills (Telegram Bot API reference, domain DAG)
|
||||
AGENTS.md engineering and runtime conventions (project context, not docs)
|
||||
BACKLOG.md open, evidence-gated work items
|
||||
CHANGELOG.md release history used to build GitHub release notes
|
||||
```
|
||||
|
||||
The doc index lives at [`docs/README.md`](docs/README.md).
|
||||
|
||||
## Verification
|
||||
|
||||
Everything below was executed in this repository on Node 24.20.0 / npm 11.19.0 (Linux, Manjaro), plus the GitHub Actions history of `main`:
|
||||
|
||||
| Command | Result |
|
||||
| --- | --- |
|
||||
| `npm ci` | exit 0, lockfile consistent, `found 0 vulnerabilities` |
|
||||
| `npm run typecheck` | exit 0, `tsc --noEmit` |
|
||||
| `npm test` | exit 0, `tests 1230 / pass 1229 / fail 0 / skipped 1` (the skip is a Windows-only case) |
|
||||
| `npm run audit` | exit 0, `found 0 vulnerabilities` |
|
||||
| `npm run pack:check` | exit 0, tarball `evandrodevbr-oh-my-pi-telegram-0.22.0-evandro.1.tgz` |
|
||||
| `npm run validate` | exit 0 (the four gates above, in order, exactly as CI runs them) |
|
||||
| `omp plugin list --json` | exit 0, `omp` 18.1.20 reads the package `omp` manifest (`displayName`, `extensions: ["./index.ts"]`, `compatibleWith`) |
|
||||
| `omp plugin doctor` | `3 ok, 1 warnings, 0 errors` (warning: no plugin package manifest) |
|
||||
| `omp install <local path>` | exit 0, `Linked @evandrodevbr/oh-my-pi-telegram from .` (the link was reverted afterwards to leave the machine as it was) |
|
||||
| `omp install --link <path>` | fails: `Unknown option '--link'` (kept out of this README) |
|
||||
| ad hoc e2e on `omp` 18.4.8 | throwaway harness, not kept in the repo: real `omp --mode rpc` with a sandboxed `HOME`, a fake Telegram Bot API and a fake OpenAI-compatible model. `/telegram-connect` finds `~/.omp/agent/telegram.json` and nothing is written under `~/.pi`; a Telegram prompt runs a model turn and the reply is delivered; Activity API handlers receive `agent-settled`; `/model` renders and marks the model that was switched locally in `omp` |
|
||||
|
||||
The test suite is deterministic and offline: transport calls are injected, so no live Telegram bot is required. Live Telegram paths (long polling, Rich Messages, Threaded Mode threading) are covered by the operator's own smoke sessions, not by this suite; the `Validate` workflow (npm ci + `npm run validate` on Node 24) is green on `main` (run 34838605928, 2026-09-14).
|
||||
|
||||
## Current state and limitations
|
||||
|
||||
- **Not published to npm**: `@evandrodevbr/oh-my-pi-telegram` returns 404 on the registry. Install from a local checkout; on top of that, `omp` 18.1.20's plugin source resolver rejects `npm` sources (`npm plugin sources are not yet supported. Use git-based sources instead.`).
|
||||
- **No GitHub release yet**: the only tag, `v0.22.0-evandro.1`, produced a failed `Release` run (`CHANGELOG.md has no section for 0.22.0-evandro.1`, because the fork section was a level-3 heading). The heading is fixed in this commit, but the existing tag still points at the pre-fix commit, so the release must be created manually or the tag moved.
|
||||
- **Fork lags upstream**: this fork stays on upstream `0.22.0` (SHA `afe09c5`), while `@llblab/pi-telegram` has since published 0.51.6. Upstream changes after 0.22.0 are not included. Upstream 0.51.6 loads in `omp` 18.4.8 but subscribes to five events `omp` never emits (`agent_settled`, `model_select`, `session_compact_failed`, `ui_prompt_start`, `ui_prompt_end`), so it is not a drop-in replacement.
|
||||
- **Verified on one platform only**: Linux with Node 24.20.0. The suite contains Windows-specific cases (and the single skipped test is one of them), but Windows/macOS were not run here.
|
||||
- **Requires a live `omp` instance and a real bot token** for end-to-end use; the repository suite does not cover the Telegram API surface, and the `omp` 18.4.8 checks above used a fake Bot API, so live Telegram behavior (Rich Messages, Threaded Mode, voice, files) is unverified on `omp`.
|
||||
- **LLM/token cost applies**: a Telegram prompt is a normal model turn in the active session, so it inherits that session's post-compaction context.
|
||||
- **Single-owner by design**: the first Telegram user to message the bot is the owner; other users are ignored. No multi-user or group-chat support.
|
||||
- **omp host differences**: `omp` emits neither `agent_settled` nor `model_select`. The bridge derives the settled boundary from the first terminal `agent_end` (`willContinue` not `true`) and reads the live model through `ctx.models.current()`. Scoped-model persistence from the Telegram model menu is not offered on `omp`, and its scoped list is read only from a global `enabledModels` array.
|
||||
- `bun.lock` is still present from upstream while CI validates with `package-lock.json`; the two are not kept in sync.
|
||||
|
||||
## Documentation
|
||||
|
||||
| Document | Content |
|
||||
| --- | --- |
|
||||
| [`docs/README.md`](docs/README.md) | Documentation index |
|
||||
| [`docs/architecture.md`](docs/architecture.md) | Runtime, domains, queue, transport and Threaded Mode overview |
|
||||
| [`docs/public-api.md`](docs/public-api.md) | Package entrypoints and stable extension contracts |
|
||||
| [`docs/delivery.md`](docs/delivery.md) | Target-aware delivery, logical message handles, leader/follower transport |
|
||||
| [`docs/activity.md`](docs/activity.md) | Normalized agent lifecycle events for companion extensions |
|
||||
| [`docs/inbound.md`](docs/inbound.md), [`docs/outbound.md`](docs/outbound.md), [`docs/voice.md`](docs/voice.md) | Inbound pipelines, outbound transforms and STT/TTS providers |
|
||||
| [`docs/sections.md`](docs/sections.md), [`docs/updates.md`](docs/updates.md), [`docs/callback-namespaces.md`](docs/callback-namespaces.md) | Companion UI sections, update routing and callback ownership |
|
||||
| [`docs/multi-instance-bus.md`](docs/multi-instance-bus.md), [`docs/locks.md`](docs/locks.md) | Leader/follower routing and singleton lock conventions |
|
||||
| [`docs/ui-style.md`](docs/ui-style.md), [`docs/command-templates.md`](docs/command-templates.md) | Inline UI standards and command-template conventions |
|
||||
| [`BACKLOG.md`](BACKLOG.md), [`CHANGELOG.md`](CHANGELOG.md) | Open work and delivery history |
|
||||
|
||||
## License
|
||||
|
||||
MIT. See [`LICENSE`](LICENSE), which keeps the copyright notices of the upstream chain (`badlogic/pi-telegram`, `llblab/pi-telegram`) alongside this fork's.
|
||||
|
||||
## Review checks (2026-09-30)
|
||||
|
||||
Diagnostics report the resolved agent directory, including `omp` and `PI_CODING_AGENT_DIR` overrides, with isolated profile suffixes. Interval time injection recovers after a backward wall-clock adjustment and rejects non-finite intervals. The `brace-expansion` security override is updated to 5.0.12.
|
||||
|
||||
Focused offline checks: `node --experimental-strip-types --test tests/time-injection.test.ts tests/paths.test.ts tests/config.test.ts tests/status.test.ts`, followed by `npm run typecheck`. The Domain DAG check remains part of architecture validation. Run `npm run validate` in an environment that permits Unix-domain sockets; this review's restricted environment blocks the bus socket tests. No live bot, Telegram client or Windows IPC session was used.
|
||||
@@ -51,6 +51,7 @@ type ActivePiModel = NonNullable<Pi.ExtensionContext["model"]>;
|
||||
// --- Extension Runtime ---
|
||||
|
||||
export default function (pi: Pi.ExtensionAPI) {
|
||||
Paths.setHostAgentDirResolver(Pi.getHostAgentDir);
|
||||
const piRuntime = Pi.createExtensionApiRuntimePorts(pi);
|
||||
const {
|
||||
getCommands,
|
||||
@@ -397,6 +398,7 @@ export default function (pi: Pi.ExtensionAPI) {
|
||||
const currentModelRuntime = Model.createCurrentModelRuntime({
|
||||
getContextModel,
|
||||
updateStatus,
|
||||
preferContextModel: Pi.isOmpHost(pi),
|
||||
});
|
||||
const queueMutationRuntime = Queue.createTelegramQueueMutationController({
|
||||
...telegramQueueStore,
|
||||
@@ -698,10 +700,12 @@ export default function (pi: Pi.ExtensionAPI) {
|
||||
resolveTimeLine: timeInjectionRuntime.resolveLine,
|
||||
getThinkingLevel,
|
||||
setThinkingLevel,
|
||||
persistScopedModelPatterns: Pi.createScopedModelPatternPersister({
|
||||
createSettingsManager: Pi.createSettingsManager,
|
||||
clearCachedModelMenuInputs: modelMenuRuntime.clearCachedInputs,
|
||||
}),
|
||||
persistScopedModelPatterns: Pi.isOmpHost(pi)
|
||||
? undefined
|
||||
: Pi.createScopedModelPatternPersister({
|
||||
createSettingsManager: Pi.createSettingsManager,
|
||||
clearCachedModelMenuInputs: modelMenuRuntime.clearCachedInputs,
|
||||
}),
|
||||
setModel,
|
||||
sendUserMessage,
|
||||
isIdle,
|
||||
|
||||
@@ -639,6 +639,7 @@ export function registerTelegramLifecycleRuntimeHooks({
|
||||
if (!isSessionContextActive(ctx)) return;
|
||||
activityRuntime.onAgentSettled();
|
||||
},
|
||||
settleOnTerminalAgentEnd: Pi.isOmpHost(pi),
|
||||
onBeforeAgentStart: Prompts.createTelegramProactiveBeforeAgentStartHook({
|
||||
isConfigured: configStore.hasBotToken,
|
||||
isProactivePushEnabled,
|
||||
|
||||
+1
-1
@@ -669,7 +669,7 @@ export function resolveTelegramTimeConfig(
|
||||
? raw.injectionMode
|
||||
: "hidden";
|
||||
const interval =
|
||||
typeof raw?.interval === "number" && raw.interval > 0
|
||||
typeof raw?.interval === "number" && Number.isFinite(raw.interval) && raw.interval > 0
|
||||
? raw.interval
|
||||
: 60 * 60 * 1000;
|
||||
const timezone = getSystemTimezone();
|
||||
|
||||
@@ -23,6 +23,7 @@ import type {
|
||||
ToolExecutionStartEvent,
|
||||
ToolExecutionUpdateEvent,
|
||||
} from "./pi.ts";
|
||||
import { isTerminalAgentEnd } from "./pi.ts";
|
||||
|
||||
let resetTransportReplyDedupFn: (() => void) | undefined;
|
||||
|
||||
@@ -110,6 +111,12 @@ export interface TelegramLifecycleRegistrationDeps {
|
||||
event: AgentSettledEvent,
|
||||
ctx: ExtensionContext,
|
||||
) => Promise<void> | void;
|
||||
/**
|
||||
* Hosts that never emit `agent_settled` (omp) get it synthesized from the
|
||||
* first terminal `agent_end`. Leave unset on hosts that emit it natively so
|
||||
* the settle point is not announced early.
|
||||
*/
|
||||
settleOnTerminalAgentEnd?: boolean;
|
||||
}
|
||||
|
||||
export interface TelegramSessionLifecycleHooks {
|
||||
@@ -562,6 +569,9 @@ export function registerTelegramLifecycleHooks(
|
||||
pi.on("agent_end", async (event, ctx) => {
|
||||
if (!isActive(ctx)) return;
|
||||
await deps.onAgentEnd(event, ctx);
|
||||
if (deps.settleOnTerminalAgentEnd && isTerminalAgentEnd(event)) {
|
||||
await deps.onAgentSettled?.({ type: "agent_settled" }, ctx);
|
||||
}
|
||||
});
|
||||
pi.on("agent_settled", async (event, ctx) => {
|
||||
if (!isActive(ctx)) return;
|
||||
|
||||
+16
-2
@@ -55,15 +55,25 @@ export type CurrentModelRuntime<
|
||||
> = CurrentModelStore<TContext, TModel> &
|
||||
CurrentModelUpdateRuntime<TContext, TModel>;
|
||||
|
||||
/**
|
||||
* `preferContextModel` makes the host's live context model win over the stored
|
||||
* one. Pi pushes model changes through `model_select`, so its stored value is
|
||||
* current; omp has no such event, so its stored value would go stale after a
|
||||
* local `/model` switch.
|
||||
*/
|
||||
export function createCurrentModelStore<
|
||||
TContext,
|
||||
TModel extends MenuModel = MenuModel,
|
||||
>(
|
||||
getContextModel: (ctx: TContext) => TModel | undefined,
|
||||
preferContextModel = false,
|
||||
): CurrentModelStore<TContext, TModel> {
|
||||
let currentModel: TModel | undefined;
|
||||
return {
|
||||
get: (ctx) => currentModel ?? getContextModel(ctx),
|
||||
get: (ctx) =>
|
||||
preferContextModel
|
||||
? (getContextModel(ctx) ?? currentModel)
|
||||
: (currentModel ?? getContextModel(ctx)),
|
||||
getStored: () => currentModel,
|
||||
set: (model) => {
|
||||
currentModel = model;
|
||||
@@ -96,8 +106,12 @@ export function createCurrentModelRuntime<
|
||||
>(deps: {
|
||||
getContextModel: (ctx: TContext) => TModel | undefined;
|
||||
updateStatus: (ctx: TContext) => void;
|
||||
preferContextModel?: boolean;
|
||||
}): CurrentModelRuntime<TContext, TModel> {
|
||||
const store = createCurrentModelStore(deps.getContextModel);
|
||||
const store = createCurrentModelStore(
|
||||
deps.getContextModel,
|
||||
deps.preferContextModel,
|
||||
);
|
||||
return {
|
||||
...store,
|
||||
...createCurrentModelUpdateRuntime({
|
||||
|
||||
+28
-21
@@ -1,19 +1,32 @@
|
||||
/**
|
||||
* Telegram bridge path resolution for Pi-compatible runtimes
|
||||
* Zones: telemetry paths, filesystem, runtime identity
|
||||
* Owns agent-dir detection and extension-local path derivation
|
||||
* Owns agent-dir resolution and extension-local path derivation
|
||||
*
|
||||
* This domain is pure/path-only: it resolves directories and file paths
|
||||
* from environment and runtime identity. It does not read config, manage
|
||||
* state, or import broader Telegram domains.
|
||||
* This domain is pure/path-only: it resolves directories and file paths from
|
||||
* the environment and the host runtime's own agent directory. It does not read
|
||||
* config, manage state, or import broader Telegram domains or the Pi SDK.
|
||||
*/
|
||||
import { homedir } from "node:os";
|
||||
import { join, resolve } from "node:path";
|
||||
|
||||
export interface TelegramAgentDirResolutionInput {
|
||||
env?: Partial<Pick<NodeJS.ProcessEnv, "PI_CODING_AGENT_DIR">>;
|
||||
execPath?: string;
|
||||
argv?: readonly string[];
|
||||
}
|
||||
|
||||
let hostAgentDirResolver: (() => string) | undefined;
|
||||
|
||||
/**
|
||||
* Register the host runtime's own agent-directory lookup (the Pi SDK
|
||||
* `getAgentDir()`). It is registered by the composition root instead of
|
||||
* imported here: importing the Pi SDK from this leaf made every domain that
|
||||
* touches a path, and every child process the test suite spawns, pay about a
|
||||
* second of SDK startup.
|
||||
*/
|
||||
export function setHostAgentDirResolver(
|
||||
resolver: (() => string) | undefined,
|
||||
): void {
|
||||
hostAgentDirResolver = resolver;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -21,23 +34,18 @@ export interface TelegramAgentDirResolutionInput {
|
||||
*
|
||||
* Precedence:
|
||||
* 1. `PI_CODING_AGENT_DIR` env variable, when explicitly set.
|
||||
* 2. Detect Pi-compatible runtime identity from the executable or argv[1]
|
||||
* (e.g. OMP vs standard Pi agent).
|
||||
* 3. Fallback: `~/.pi/agent`.
|
||||
* 2. The registered host agent directory. Pi returns `~/.pi/agent`; omp's Pi
|
||||
* shim returns its profile-aware directory (default `~/.omp/agent`). The
|
||||
* executable and argv are not inspected: omp installed through Bun runs as
|
||||
* `bun .../dist/cli.js`, which neither contains nor starts with `omp`.
|
||||
* 3. Fallback when no host is registered: `~/.pi/agent`.
|
||||
*/
|
||||
export function resolveAgentDir(
|
||||
input: TelegramAgentDirResolutionInput = {},
|
||||
): string {
|
||||
const env = input.env ?? process.env;
|
||||
if (env.PI_CODING_AGENT_DIR) return resolve(env.PI_CODING_AGENT_DIR);
|
||||
const execPath = input.execPath ?? process.execPath;
|
||||
const argv = input.argv ?? process.argv;
|
||||
const execBasename = execPath.toLowerCase().split(/[\\/]/u).pop() ?? "";
|
||||
const argv1Last = (argv[1] ?? "").toLowerCase().split(/[\\/]/u).pop() ?? "";
|
||||
if (execBasename.startsWith("omp") || argv1Last.startsWith("omp")) {
|
||||
return join(homedir(), ".omp", "agent");
|
||||
}
|
||||
return join(homedir(), ".pi", "agent");
|
||||
return hostAgentDirResolver?.() ?? join(homedir(), ".pi", "agent");
|
||||
}
|
||||
|
||||
/** Telegram bridge configuration file (<agentDir>/telegram.json). */
|
||||
@@ -75,11 +83,10 @@ export function getTelegramDiagnosticsDisplayPaths(profileName?: string): {
|
||||
state: string;
|
||||
logs: string;
|
||||
} {
|
||||
const suffix = getTelegramProfilePathSuffix(profileName);
|
||||
const profileSlug = suffix.slice(1);
|
||||
const agentDir = resolveAgentDir();
|
||||
return {
|
||||
state: `~/.pi/agent/tmp/telegram/state${suffix}.json`,
|
||||
logs: `~/.pi/agent/tmp/telegram/logs${profileSlug ? `.${profileSlug}` : ""}.jsonl`,
|
||||
state: resolveTelegramProfileTempFilePath("state", "json", agentDir, profileName),
|
||||
logs: resolveTelegramProfileTempFilePath("logs", "jsonl", agentDir, profileName),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
type SessionStartEvent,
|
||||
type SlashCommandInfo,
|
||||
SettingsManager,
|
||||
getAgentDir,
|
||||
} from "@earendil-works/pi-coding-agent";
|
||||
|
||||
export type {
|
||||
@@ -39,6 +40,13 @@ export type {
|
||||
SlashCommandInfo,
|
||||
};
|
||||
|
||||
/**
|
||||
* Agent data directory owned by the host runtime. Pi returns its own root and
|
||||
* omp's legacy Pi shim forwards omp's profile-aware directory, so no argv or
|
||||
* executable sniffing is needed.
|
||||
*/
|
||||
export { getAgentDir as getHostAgentDir };
|
||||
|
||||
export interface ToolExecutionStartEvent {
|
||||
type: "tool_execution_start";
|
||||
toolCallId: string;
|
||||
@@ -69,6 +77,26 @@ export interface PiSettingsManager {
|
||||
setEnabledModels: (patterns: string[] | undefined) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* omp's `Settings` is what its legacy shim returns from
|
||||
* `SettingsManager.create()`. It has no `reload()` or `get/setEnabledModels()`:
|
||||
* it reloads through `reloadFromDisk()` and keeps `enabledModels` as a
|
||||
* path-scoped typed setting.
|
||||
*/
|
||||
interface OmpSettingsSurface {
|
||||
reloadFromDisk: () => Promise<void>;
|
||||
flush: () => Promise<void>;
|
||||
getGlobalSettings: () => { enabledModels?: unknown };
|
||||
}
|
||||
|
||||
function isOmpSettings(value: object): value is OmpSettingsSurface {
|
||||
return (
|
||||
"reloadFromDisk" in value &&
|
||||
"flush" in value &&
|
||||
"getGlobalSettings" in value
|
||||
);
|
||||
}
|
||||
|
||||
export type PiSlashCommandInfo = SlashCommandInfo;
|
||||
export type PiRunMode = "tui" | "rpc" | "json" | "print";
|
||||
|
||||
@@ -150,7 +178,22 @@ export function createExtensionApiRuntimePorts(
|
||||
}
|
||||
|
||||
export function createSettingsManager(cwd: string): PiSettingsManager {
|
||||
return SettingsManager.create(cwd);
|
||||
const manager: PiSettingsManager = SettingsManager.create(cwd);
|
||||
if (!isOmpSettings(manager)) return manager;
|
||||
return {
|
||||
reload: () => manager.reloadFromDisk(),
|
||||
flush: () => manager.flush(),
|
||||
getEnabledModels: () => {
|
||||
const enabled = manager.getGlobalSettings().enabledModels;
|
||||
return Array.isArray(enabled) &&
|
||||
enabled.every((pattern) => typeof pattern === "string")
|
||||
? enabled
|
||||
: undefined;
|
||||
},
|
||||
setEnabledModels: () => {
|
||||
throw new Error("Scoped model persistence is not supported on omp.");
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function createScopedModelPatternPersister(deps: {
|
||||
@@ -167,16 +210,56 @@ export function createScopedModelPatternPersister(deps: {
|
||||
};
|
||||
}
|
||||
|
||||
interface OmpModelQuery {
|
||||
current: () => ExtensionContext["model"];
|
||||
}
|
||||
|
||||
function hasOmpModelQuery(
|
||||
ctx: ExtensionContext,
|
||||
): ctx is ExtensionContext & { models: OmpModelQuery } {
|
||||
return (
|
||||
"models" in ctx &&
|
||||
typeof ctx.models === "object" &&
|
||||
ctx.models !== null &&
|
||||
"current" in ctx.models &&
|
||||
typeof ctx.models.current === "function"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* omp builds command contexts by spreading a base context, which evaluates the
|
||||
* `model` getter once: a context stored from `/telegram-connect` would report
|
||||
* the model of that moment forever. omp's `ctx.models.current()` closes over
|
||||
* the live session model, so prefer it when present.
|
||||
*/
|
||||
export function getExtensionContextModel(
|
||||
ctx: ExtensionContext,
|
||||
): ExtensionContext["model"] {
|
||||
return ctx.model;
|
||||
return hasOmpModelQuery(ctx) ? ctx.models.current() : ctx.model;
|
||||
}
|
||||
|
||||
export function getExtensionContextCwd(ctx: ExtensionContext): string {
|
||||
return ctx.cwd;
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the extension API belongs to omp. omp's `ExtensionAPI` carries
|
||||
* `typebox` and `pi` module facades that upstream Pi's API does not have.
|
||||
*/
|
||||
export function isOmpHost(api: ExtensionAPI): boolean {
|
||||
return "typebox" in api && "pi" in api;
|
||||
}
|
||||
|
||||
/**
|
||||
* omp marks an `agent_end` with `willContinue: true` whenever it already
|
||||
* scheduled a retry, compaction continuation, todo/plan reminder, or async
|
||||
* wake. Any other `agent_end` is terminal, which is what Pi reports through
|
||||
* `agent_settled`.
|
||||
*/
|
||||
export function isTerminalAgentEnd(event: AgentEndEvent): boolean {
|
||||
return !("willContinue" in event && event.willContinue === true);
|
||||
}
|
||||
|
||||
export function isExtensionContextIdle(ctx: ExtensionContext): boolean {
|
||||
return ctx.isIdle();
|
||||
}
|
||||
|
||||
@@ -67,6 +67,7 @@ export function createTimeInjectionRuntime(
|
||||
const nowMs = now.getTime();
|
||||
if (
|
||||
previous !== undefined &&
|
||||
nowMs >= previous &&
|
||||
nowMs - previous < config.interval
|
||||
) {
|
||||
return null;
|
||||
|
||||
Generated
+10
-19
@@ -963,7 +963,6 @@
|
||||
"version": "26.1.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-26.1.1.tgz",
|
||||
"integrity": "sha512-nxAkRSVkN1Y0JC1W8ky/fTfkGsMmcrRsbx+3XoZE+rMOX71kLYTV7fLXpqud1GpbpP5TuffXFqfX7fH2GgZREw==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"undici-types": "~8.3.0"
|
||||
@@ -1363,14 +1362,16 @@
|
||||
"peer": true
|
||||
},
|
||||
"node_modules/brace-expansion": {
|
||||
"version": "5.0.6",
|
||||
"version": "5.0.12",
|
||||
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-5.0.12.tgz",
|
||||
"integrity": "sha512-YovQ3rzhaLMIrDjNDMkNS01tea93qhEhG5xy8f6+R0l+dw3Ki+5sCoIoI942iuLZTHWogWktgwVDhU09iNEimQ==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"balanced-match": "^4.0.2"
|
||||
},
|
||||
"engines": {
|
||||
"node": "18 || 20 || >=22"
|
||||
"node": "20 || >=22"
|
||||
}
|
||||
},
|
||||
"node_modules/buffer-equal-constant-time": {
|
||||
@@ -1879,7 +1880,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/protobufjs": {
|
||||
"version": "7.6.4",
|
||||
"version": "7.6.6",
|
||||
"resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-7.6.6.tgz",
|
||||
"integrity": "sha512-dYDWdjSl5RNb7SgPxGQcRU+GtvP7s2fpkrY0r432PcOIaZ0/rBcxEZnQN67iJhFuQiVw754JDoPruPCNdGsbjg==",
|
||||
"hasInstallScript": true,
|
||||
"license": "BSD-3-Clause",
|
||||
"peer": true,
|
||||
@@ -1900,19 +1903,6 @@
|
||||
"node": ">=12.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/protobufjs/node_modules/@types/node": {
|
||||
"version": "22.19.19",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"undici-types": "~6.21.0"
|
||||
}
|
||||
},
|
||||
"node_modules/protobufjs/node_modules/@types/node/node_modules/undici-types": {
|
||||
"version": "6.21.0",
|
||||
"license": "MIT",
|
||||
"peer": true
|
||||
},
|
||||
"node_modules/retry": {
|
||||
"version": "0.12.0",
|
||||
"license": "MIT",
|
||||
@@ -2037,7 +2027,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/undici": {
|
||||
"version": "8.5.0",
|
||||
"version": "8.10.2",
|
||||
"resolved": "https://registry.npmjs.org/undici/-/undici-8.10.2.tgz",
|
||||
"integrity": "sha512-/y4/bH9YNU5hi9NIrpOuvGXFcxrj3CMrV+/AYpowAYTpHn8gX/XPFjNy766FPoYY0miQhdW977JFWKGNhBdwyQ==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"engines": {
|
||||
@@ -2048,7 +2040,6 @@
|
||||
"version": "8.3.0",
|
||||
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-8.3.0.tgz",
|
||||
"integrity": "sha512-j375ScV60dom+YkPFIfTLcOiPxkN/buHz5GobjLhixFuANaNs3C9l4GmrWqejgXWJ7BbJcFYpTEUkS1Ge8bpZQ==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/web-streams-polyfill": {
|
||||
|
||||
+10
-8
@@ -81,25 +81,27 @@
|
||||
"typescript": "latest"
|
||||
},
|
||||
"overrides": {
|
||||
"protobufjs": "7.6.4",
|
||||
"undici": "8.5.0",
|
||||
"protobufjs": "7.6.6",
|
||||
"undici": "8.10.2",
|
||||
"brace-expansion": "5.0.12",
|
||||
"ws": "8.21.0",
|
||||
"@earendil-works/pi-coding-agent": {
|
||||
"protobufjs": "7.6.4",
|
||||
"undici": "8.5.0",
|
||||
"protobufjs": "7.6.6",
|
||||
"undici": "8.10.2",
|
||||
"brace-expansion": "5.0.12",
|
||||
"ws": "8.21.0",
|
||||
"@google/genai": {
|
||||
"protobufjs": "7.6.4",
|
||||
"protobufjs": "7.6.6",
|
||||
"ws": "8.21.0"
|
||||
},
|
||||
"@earendil-works/pi-ai": {
|
||||
"protobufjs": "7.6.4",
|
||||
"protobufjs": "7.6.6",
|
||||
"ws": "8.21.0",
|
||||
"@google/genai": {
|
||||
"protobufjs": "7.6.4",
|
||||
"protobufjs": "7.6.6",
|
||||
"ws": "8.21.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
-1
@@ -1060,7 +1060,12 @@ test("Bus local server memoizes completed and in-flight request results", async
|
||||
};
|
||||
const first = sendTelegramBusLocalEnvelope({ socketPath, envelope });
|
||||
const duplicate = sendTelegramBusLocalEnvelope({ socketPath, envelope });
|
||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
||||
// Wait for the first envelope to reach the handler instead of assuming a
|
||||
// fixed delivery time; the duplicate must still be memoized while in flight.
|
||||
const deliveryDeadlineMs = Date.now() + 5_000;
|
||||
while (executions === 0 && Date.now() < deliveryDeadlineMs) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
||||
}
|
||||
assert.equal(executions, 1);
|
||||
release?.();
|
||||
const [firstResult, duplicateResult] = await Promise.all([first, duplicate]);
|
||||
|
||||
@@ -560,3 +560,38 @@ test("Lifecycle helpers register pi hooks and delegate to handlers", async () =>
|
||||
"agent-end",
|
||||
]);
|
||||
});
|
||||
|
||||
test("Terminal agent_end synthesizes agent_settled only for hosts that never emit it", async () => {
|
||||
const cases = [
|
||||
{ settle: true, event: { messages: [] }, expected: ["agent-end", "agent-settled"] },
|
||||
{ settle: true, event: { messages: [], willContinue: true }, expected: ["agent-end"] },
|
||||
{ settle: false, event: { messages: [] }, expected: ["agent-end"] },
|
||||
];
|
||||
for (const { settle, event, expected } of cases) {
|
||||
const harness = createLifecycleApiHarness();
|
||||
const events: string[] = [];
|
||||
registerTelegramLifecycleHooks(harness.api, {
|
||||
settleOnTerminalAgentEnd: settle,
|
||||
onSessionStart: async () => {},
|
||||
onSessionShutdown: async () => {},
|
||||
onBeforeAgentStart: () => undefined,
|
||||
onModelSelect: () => {},
|
||||
onAgentStart: async () => {},
|
||||
onToolExecutionStart: () => {},
|
||||
onToolExecutionEnd: () => {},
|
||||
onMessageStart: async () => {},
|
||||
onMessageUpdate: async () => {},
|
||||
onAgentEnd: async () => {
|
||||
events.push("agent-end");
|
||||
},
|
||||
onAgentSettled: () => {
|
||||
events.push("agent-settled");
|
||||
},
|
||||
});
|
||||
await getRequiredLifecycleHandler(harness.handlers, "agent_end")(
|
||||
event,
|
||||
createLifecycleContext(),
|
||||
);
|
||||
assert.deepEqual(events, expected, JSON.stringify({ settle, event }));
|
||||
}
|
||||
});
|
||||
@@ -83,6 +83,18 @@ test("Current model store prefers explicit Telegram model over context fallback"
|
||||
assert.equal(store.get({ model: fallback }), fallback);
|
||||
});
|
||||
|
||||
test("Current model store follows the live context model on hosts without model_select", () => {
|
||||
const stale = { provider: "stale", id: "a", name: "Stale" };
|
||||
const live = { provider: "live", id: "b", name: "Live" };
|
||||
const store = createCurrentModelStore<{ model?: typeof live }, typeof live>(
|
||||
(ctx) => ctx.model,
|
||||
true,
|
||||
);
|
||||
store.set(stale);
|
||||
assert.equal(store.get({ model: live }), live);
|
||||
assert.equal(store.get({ model: undefined }), stale);
|
||||
});
|
||||
|
||||
test("Current model runtime combines store fallback and status updates", () => {
|
||||
const fallback = { provider: "fallback", id: "f", name: "Fallback" };
|
||||
const selected = { provider: "selected", id: "s", name: "Selected" };
|
||||
|
||||
+30
-26
@@ -10,48 +10,52 @@ import test from "node:test";
|
||||
|
||||
import {
|
||||
resolveAgentDir,
|
||||
setHostAgentDirResolver,
|
||||
resolveTelegramConfigPath,
|
||||
resolveTelegramLocksPath,
|
||||
resolveTelegramTempDir,
|
||||
resolveTelegramRuntimeLogPath,
|
||||
getTelegramDiagnosticsDisplayPaths,
|
||||
resolveTelegramProfileTempFilePath,
|
||||
} from "../lib/paths.ts";
|
||||
|
||||
test("Diagnostics use the resolved agent root and profile suffix", () => {
|
||||
const agentDir = resolveAgentDir();
|
||||
for (const profile of [undefined, "work"]) {
|
||||
assert.deepEqual(getTelegramDiagnosticsDisplayPaths(profile), {
|
||||
state: resolveTelegramProfileTempFilePath("state", "json", agentDir, profile),
|
||||
logs: resolveTelegramProfileTempFilePath("logs", "jsonl", agentDir, profile),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
await test("resolveAgentDir", async (t) => {
|
||||
await t.test("returns PI_CODING_AGENT_DIR when env is set", () => {
|
||||
t.afterEach(() => setHostAgentDirResolver(undefined));
|
||||
|
||||
await t.test("PI_CODING_AGENT_DIR wins over the host directory", () => {
|
||||
setHostAgentDirResolver(() => {
|
||||
throw new Error("host directory must not be consulted");
|
||||
});
|
||||
assert.equal(
|
||||
resolveAgentDir({
|
||||
env: { PI_CODING_AGENT_DIR: "/custom/agent/dir" },
|
||||
execPath: "/usr/bin/omp",
|
||||
argv: ["omp"],
|
||||
}),
|
||||
resolveAgentDir({ env: { PI_CODING_AGENT_DIR: "/custom/agent/dir" } }),
|
||||
resolve("/custom/agent/dir"),
|
||||
);
|
||||
});
|
||||
|
||||
await t.test("returns ~/.omp/agent for OMP-compatible runtimes", () => {
|
||||
assert.equal(
|
||||
resolveAgentDir({ env: {}, execPath: "/home/user/.local/bin/omp" }),
|
||||
join(homedir(), ".omp", "agent"),
|
||||
);
|
||||
assert.equal(
|
||||
resolveAgentDir({
|
||||
env: {},
|
||||
execPath: "/usr/bin/node",
|
||||
argv: ["node", "omp"],
|
||||
}),
|
||||
join(homedir(), ".omp", "agent"),
|
||||
);
|
||||
});
|
||||
|
||||
await t.test(
|
||||
"returns ~/.pi/agent as fallback when no env and no OMP runtime",
|
||||
"uses the registered host runtime directory when env is unset",
|
||||
() => {
|
||||
assert.equal(
|
||||
resolveAgentDir({ env: {}, execPath: "/usr/bin/node", argv: ["node"] }),
|
||||
join(homedir(), ".pi", "agent"),
|
||||
);
|
||||
setHostAgentDirResolver(() => "/home/user/.omp/agent");
|
||||
assert.equal(resolveAgentDir({ env: {} }), "/home/user/.omp/agent");
|
||||
},
|
||||
);
|
||||
|
||||
await t.test("falls back to ~/.pi/agent when no host is registered", () => {
|
||||
assert.equal(
|
||||
resolveAgentDir({ env: {} }),
|
||||
join(homedir(), ".pi", "agent"),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
await test("resolveTelegramConfigPath", () => {
|
||||
|
||||
@@ -157,3 +157,15 @@ test("Pi context helpers expose model, idle, pending-message, and compact adapte
|
||||
assert.equal(hasExtensionContextPendingMessages(ctx), false);
|
||||
assert.deepEqual(events, ["compact", "complete"]);
|
||||
});
|
||||
|
||||
test("Pi context model prefers omp's live models facade over a frozen ctx.model", () => {
|
||||
const frozen = { provider: "fake", id: "fake-1" };
|
||||
const live = { provider: "fake", id: "fake-2" };
|
||||
const ompCtx = {
|
||||
model: frozen,
|
||||
models: { current: () => live },
|
||||
} as unknown as ExtensionContext;
|
||||
const piCtx = { model: frozen } as unknown as ExtensionContext;
|
||||
assert.equal(getExtensionContextModel(ompCtx), live);
|
||||
assert.equal(getExtensionContextModel(piCtx), frozen);
|
||||
});
|
||||
@@ -47,6 +47,20 @@ test("Time config rejects non-positive interval", () => {
|
||||
assert.equal(resolved.interval, 60 * 60 * 1000);
|
||||
});
|
||||
|
||||
test("Time config rejects non-finite intervals", () => {
|
||||
for (const interval of [NaN, Infinity, -Infinity]) {
|
||||
assert.equal(resolveTelegramTimeConfig({ injectionMode: "interval", interval }).interval, 3_600_000);
|
||||
}
|
||||
});
|
||||
|
||||
test("Time injection recovers when the wall clock moves backwards", () => {
|
||||
const runtime = makeRuntime({ injectionMode: "interval", interval: 60_000, timezone: "UTC" });
|
||||
assert.ok(runtime.resolveLine(1, new Date("2026-05-16T15:00:00Z")));
|
||||
assert.ok(runtime.resolveLine(1, new Date("2026-05-16T14:00:00Z")));
|
||||
assert.equal(runtime.resolveLine(1, new Date("2026-05-16T14:00:30Z")), null);
|
||||
assert.ok(runtime.resolveLine(1, new Date("2026-05-16T14:01:00Z")));
|
||||
});
|
||||
|
||||
test("Time injection runtime returns null for every call when injectionMode is hidden", () => {
|
||||
const runtime = makeRuntime({ injectionMode: "hidden", timezone: "UTC" });
|
||||
for (let i = 0; i < 5; i++) {
|
||||
|
||||
Reference in new issue
Block a user