mirror of
https://github.com/wassname/pi-telegram.git
synced 2026-06-27 16:46:21 +08:00
5.7 KiB
5.7 KiB
Project Context
0. Meta-Protocol Principles
Constraint-Driven Evolution: Add structure when the bridge gains real operator or runtime constraintsSingle Source of Truth: Keep durable rules inAGENTS.md, open work inBACKLOG.md, completed delivery inCHANGELOG.md, and deeper technical detail in/docsBoundary Clarity: Separate Telegram transport concerns, pi integration concerns, rendering behavior, and release/documentation stateRuntime Safety: Prefer queue and rendering behavior that fails predictably over clever behavior that can desynchronize the Telegram bridge from pi session state
1. Concept
pi-telegram is a pi extension that turns a Telegram DM into a session-local frontend for pi, including text/file forwarding, streaming previews, queued follow-ups, model controls, and outbound attachment delivery.
2. Identity & Naming Contract
Telegram turn: One unit of Telegram input processed by pi; this may represent one message or a coalesced media groupQueued Telegram turn: A Telegram turn accepted by the bridge but not yet active in piActive Telegram turn: The Telegram turn currently bound to the running pi agent loopPreview: The transient streamed response shown through Telegram drafts or editable messages before the final reply landsScoped models: The subset of models exposed to Telegram model selection when pi settings or CLI flags limit the available list
3. Project Topology
/index.ts: Main extension runtime, Telegram API integration, queueing, rendering, previews, menus, and tool wiring/docs/README.md: Documentation index for technical project docs/docs/architecture.md: Runtime and subsystem overview for the bridge/README.md: User-facing project entry point, install guide, and fork summary/AGENTS.md: Durable engineering and runtime conventions/BACKLOG.md: Canonical open work/CHANGELOG.md: Completed delivery history
4. Core Entities
TelegramConfig: Persisted bot/session pairing statePendingTelegramTurn/ActiveTelegramTurn: Queue and active-turn state for Telegram-originated workTelegramPreviewState: Streaming preview state for drafts or editable Telegram messagesTelegramModelMenuState: Inline menu state for status/model/thinking controlsQueuedAttachment: Outbound files staged for delivery throughtelegram_attach
5. Architectural Decisions
- The extension ships as a single runtime file for simple pi packaging, so logical sections inside
index.tsare the primary module boundaries - The bridge is session-local and intentionally pairs with a single allowed Telegram user per config
- Telegram queue state is tracked locally and must stay aligned with pi agent lifecycle hooks; dispatch must respect active turns, pending dispatch, compaction, and pi pending-message state
- In-flight
/modelswitching is supported only for Telegram-owned active turns and is implemented as set-model plus synthetic continuation turn plus abort; if a tool call is active, the abort is delayed until that tool finishes instead of interrupting the tool mid-flight - Telegram replies render through Telegram HTML, not raw Markdown; real code blocks must stay literal and escaped
telegram_attachis the canonical outbound file-delivery path for Telegram-originated requests
6. Engineering Conventions
- Treat queue handling, compaction interaction, and lifecycle-hook state transitions as regression-prone areas; validate them after changing dispatch logic
- Treat Markdown rendering as Telegram-specific output work, not generic Markdown rendering; preserve literal code content and avoid HTML chunk splits that break tags
- Keep comments and user-facing docs in English unless the surrounding file already follows another convention
- Prefer targeted edits inside
index.tsover broad rewrites unless a section boundary is being intentionally restructured
7. Operational Conventions
- When Telegram-visible behavior changes, sync
README.mdand the relevant/docsentry in the same pass - When durable runtime constraints or repeat bug patterns emerge, record them here instead of burying them in changelog prose
- When fork identity changes, keep
README.md, package metadata, and docs aligned so the published package does not point back at stale upstream coordinates
8. Integration Protocols
- Telegram API methods currently used include polling, message editing, draft streaming, callback queries, reactions, file download, and media upload endpoints
- pi integration depends on lifecycle hooks such as
before_agent_start,agent_start,message_start,message_update, andagent_end - Status/model/thinking controls are driven through Telegram inline keyboards and callback queries
- Inbound files may become pi image inputs; outbound files must flow through
telegram_attach
9. Pre-Task Preparation Protocol
- Read
README.mdfor current user-facing behavior and fork positioning - Read
BACKLOG.mdbefore changing runtime behavior or documentation so open work stays truthful - Read
/docs/architecture.mdbefore restructuring queue, preview, rendering, or command-handling logic - Inspect the relevant
index.tssection before editing because most bridge behavior is stateful and cross-linked
10. Task Completion Protocol
- Run the smallest meaningful validation for the touched area;
npm testis the default regression suite once rendering or queue logic changes - For rendering changes, ensure regressions still cover nested lists, code blocks, underscore-heavy text, and long-message chunking
- For queue/dispatch changes, validate abort, compaction, pending-dispatch, and pi pending-message guard behavior
- Sync
README.md,CHANGELOG.md,BACKLOG.md, and/docswhenever user-visible behavior or real open-work state changes