@fickydev/pigent 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +22 -0
- package/AGENTS.md +242 -0
- package/CHANGELOG.md +35 -0
- package/LICENSE +21 -0
- package/PLAN.md +369 -0
- package/README.md +369 -0
- package/TODO.md +183 -0
- package/agents/coder/SYSTEM.md +3 -0
- package/agents/coder/agent.yaml +20 -0
- package/drizzle/migrations/0000_great_daredevil.sql +78 -0
- package/drizzle/migrations/meta/0000_snapshot.json +505 -0
- package/drizzle/migrations/meta/_journal.json +13 -0
- package/drizzle.config.ts +13 -0
- package/package.json +66 -0
- package/pigent.yaml +12 -0
- package/profiles/software-engineer.yaml +11 -0
- package/src/agents/AgentRunner.ts +112 -0
- package/src/agents/BotCommandHandler.ts +65 -0
- package/src/agents/MessageRouter.ts +106 -0
- package/src/channels/telegram/TelegramApi.ts +67 -0
- package/src/channels/telegram/TelegramPollingAdapter.ts +123 -0
- package/src/channels/telegram/types.ts +50 -0
- package/src/channels/types.ts +29 -0
- package/src/cli/run.ts +77 -0
- package/src/cli/setup.ts +261 -0
- package/src/config/loadConfig.ts +115 -0
- package/src/config/schemas.ts +92 -0
- package/src/daemon/AgentDaemon.ts +161 -0
- package/src/db/client.ts +23 -0
- package/src/db/repositories/AgentRepository.ts +45 -0
- package/src/db/repositories/MessageRepository.ts +45 -0
- package/src/db/repositories/RuntimeKvRepository.ts +27 -0
- package/src/db/repositories/SessionRepository.ts +65 -0
- package/src/db/repositories/TelegramRepository.ts +98 -0
- package/src/db/repositories/index.ts +18 -0
- package/src/db/schema.ts +106 -0
- package/src/logging/logger.ts +45 -0
- package/src/main.ts +37 -0
- package/src/pi/PiAgentRunner.ts +73 -0
- package/tsconfig.json +17 -0
package/.env.example
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Runtime
|
|
2
|
+
NODE_ENV=development
|
|
3
|
+
LOG_LEVEL=info
|
|
4
|
+
|
|
5
|
+
# Database
|
|
6
|
+
DATABASE_URL=file:./pigent.db
|
|
7
|
+
|
|
8
|
+
# Telegram
|
|
9
|
+
TELEGRAM_BOT_TOKEN=
|
|
10
|
+
TELEGRAM_POLL_TIMEOUT_SECONDS=30
|
|
11
|
+
TELEGRAM_POLL_INTERVAL_MS=1000
|
|
12
|
+
|
|
13
|
+
# Pi
|
|
14
|
+
PIGENT_WORKSPACE_ROOT=~/.pigent
|
|
15
|
+
# Set to 1 to bypass Pi SDK and use deterministic fake responses.
|
|
16
|
+
PIGENT_FAKE_AGENT=0
|
|
17
|
+
# Set to 1 to use fake responses if Pi SDK execution fails.
|
|
18
|
+
PIGENT_FALLBACK_FAKE_AGENT=1
|
|
19
|
+
# Set to 1 to auto-create DB chat routing for new Telegram chats.
|
|
20
|
+
PIGENT_AUTO_SETUP_CHATS=0
|
|
21
|
+
# Agent used when auto-creating chat routing. Defaults to first loaded agent.
|
|
22
|
+
PIGENT_AUTO_SETUP_DEFAULT_AGENT=coder
|
package/AGENTS.md
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
# AGENTS.md
|
|
2
|
+
|
|
3
|
+
## Project
|
|
4
|
+
|
|
5
|
+
Pigent is an autonomous multi-agent daemon using Pi as core execution engine. It will route messages from external channels to Pi-backed agents with per-agent profiles, skills, extensions, tools, permissions, heartbeat behavior, and per-chat sessions.
|
|
6
|
+
|
|
7
|
+
## Current Direction
|
|
8
|
+
|
|
9
|
+
Use smallest working daemon first.
|
|
10
|
+
|
|
11
|
+
- Bun runtime
|
|
12
|
+
- TypeScript
|
|
13
|
+
- Drizzle ORM
|
|
14
|
+
- SQLite first, PostgreSQL later
|
|
15
|
+
- Telegram polling first
|
|
16
|
+
- Pi SDK for agent execution
|
|
17
|
+
- No Hono server in MVP
|
|
18
|
+
|
|
19
|
+
Add Hono later only for webhooks, Slack, WhatsApp, admin API, health endpoints, or dashboard backend.
|
|
20
|
+
|
|
21
|
+
## Coding Rules
|
|
22
|
+
|
|
23
|
+
- Default workspace root is `~/.pigent`.
|
|
24
|
+
- Keep changes small and scoped.
|
|
25
|
+
- Prefer working code over abstractions.
|
|
26
|
+
- Always update `CHANGELOG.md` after working.
|
|
27
|
+
- Always update `TODO.md` after working.
|
|
28
|
+
- Whenever user says `remember this`, update `AGENTS.md` accordingly.
|
|
29
|
+
- Do not add Hono until explicitly requested.
|
|
30
|
+
- Do not let channel adapters call Pi directly.
|
|
31
|
+
- Do not let Pi runtime know channel secrets.
|
|
32
|
+
- Use repositories for database access; avoid raw Drizzle queries spread across runtime code.
|
|
33
|
+
- Keep SQLite/PostgreSQL portability in mind.
|
|
34
|
+
- Avoid hard-coded Telegram-specific logic outside `channels/telegram` and routing config.
|
|
35
|
+
- Do not inject secrets or env values into model prompts.
|
|
36
|
+
- Default safety policy should be conservative.
|
|
37
|
+
|
|
38
|
+
## Architecture Boundaries
|
|
39
|
+
|
|
40
|
+
```text
|
|
41
|
+
Channel Adapter -> Message Router -> Agent Orchestrator -> Pi Runner
|
|
42
|
+
-> State Store
|
|
43
|
+
-> Policy Engine
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Channel Adapter
|
|
47
|
+
|
|
48
|
+
Responsibilities:
|
|
49
|
+
|
|
50
|
+
- receive external messages
|
|
51
|
+
- normalize channel payloads
|
|
52
|
+
- send outbound messages
|
|
53
|
+
- manage channel-specific offsets/tokens/retries
|
|
54
|
+
|
|
55
|
+
Must not:
|
|
56
|
+
|
|
57
|
+
- create Pi sessions
|
|
58
|
+
- inspect agent internals
|
|
59
|
+
- bypass router
|
|
60
|
+
|
|
61
|
+
### Message Router
|
|
62
|
+
|
|
63
|
+
Responsibilities:
|
|
64
|
+
|
|
65
|
+
- select agent from message
|
|
66
|
+
- parse explicit agent commands
|
|
67
|
+
- apply chat default agent
|
|
68
|
+
- enforce allowed agents
|
|
69
|
+
- return route result or user-facing routing error
|
|
70
|
+
|
|
71
|
+
### Agent Orchestrator
|
|
72
|
+
|
|
73
|
+
Responsibilities:
|
|
74
|
+
|
|
75
|
+
- load agent config
|
|
76
|
+
- get/create session
|
|
77
|
+
- compose prompt context
|
|
78
|
+
- call Pi runner
|
|
79
|
+
- persist messages/events
|
|
80
|
+
- coordinate locks
|
|
81
|
+
|
|
82
|
+
### Pi Runner
|
|
83
|
+
|
|
84
|
+
Responsibilities:
|
|
85
|
+
|
|
86
|
+
- integrate with Pi SDK
|
|
87
|
+
- create/reuse Pi sessions
|
|
88
|
+
- send prompt
|
|
89
|
+
- return response/error
|
|
90
|
+
|
|
91
|
+
Must not:
|
|
92
|
+
|
|
93
|
+
- know Telegram/Slack/WhatsApp token details
|
|
94
|
+
- send messages to external channels directly
|
|
95
|
+
|
|
96
|
+
### State Store
|
|
97
|
+
|
|
98
|
+
Responsibilities:
|
|
99
|
+
|
|
100
|
+
- Drizzle client
|
|
101
|
+
- schema
|
|
102
|
+
- repositories
|
|
103
|
+
- migrations
|
|
104
|
+
- runtime state persistence
|
|
105
|
+
|
|
106
|
+
### Policy Engine
|
|
107
|
+
|
|
108
|
+
Responsibilities:
|
|
109
|
+
|
|
110
|
+
- gate tools/actions
|
|
111
|
+
- enforce path policy
|
|
112
|
+
- require approval for risky operations
|
|
113
|
+
- rate-limit autonomous notifications
|
|
114
|
+
|
|
115
|
+
## Planned Layout
|
|
116
|
+
|
|
117
|
+
```text
|
|
118
|
+
src/
|
|
119
|
+
main.ts
|
|
120
|
+
|
|
121
|
+
daemon/
|
|
122
|
+
AgentDaemon.ts
|
|
123
|
+
HeartbeatScheduler.ts
|
|
124
|
+
|
|
125
|
+
channels/
|
|
126
|
+
types.ts
|
|
127
|
+
telegram/
|
|
128
|
+
TelegramPollingAdapter.ts
|
|
129
|
+
TelegramApi.ts
|
|
130
|
+
types.ts
|
|
131
|
+
|
|
132
|
+
agents/
|
|
133
|
+
AgentConfig.ts
|
|
134
|
+
AgentRegistry.ts
|
|
135
|
+
AgentRunner.ts
|
|
136
|
+
MessageRouter.ts
|
|
137
|
+
|
|
138
|
+
pi/
|
|
139
|
+
PiAgentRunner.ts
|
|
140
|
+
PiSessionFactory.ts
|
|
141
|
+
|
|
142
|
+
db/
|
|
143
|
+
client.ts
|
|
144
|
+
schema.ts
|
|
145
|
+
repositories/
|
|
146
|
+
|
|
147
|
+
config/
|
|
148
|
+
loadConfig.ts
|
|
149
|
+
schemas.ts
|
|
150
|
+
|
|
151
|
+
policy/
|
|
152
|
+
PolicyEngine.ts
|
|
153
|
+
|
|
154
|
+
logging/
|
|
155
|
+
logger.ts
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Routing Rules
|
|
159
|
+
|
|
160
|
+
One Telegram bot can serve many agents.
|
|
161
|
+
|
|
162
|
+
Priority:
|
|
163
|
+
|
|
164
|
+
1. explicit `@agentId` prefix
|
|
165
|
+
2. explicit `/agent <agentId>` command
|
|
166
|
+
3. chat default agent
|
|
167
|
+
4. no route error
|
|
168
|
+
|
|
169
|
+
Session key:
|
|
170
|
+
|
|
171
|
+
```text
|
|
172
|
+
agentId + channel + chatId + threadId
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Group chats share session by default. Private chats use chat id. Telegram forum topics use thread id.
|
|
176
|
+
|
|
177
|
+
## Prompt Context Rules
|
|
178
|
+
|
|
179
|
+
Prompt composition may include:
|
|
180
|
+
|
|
181
|
+
- agent profile instructions
|
|
182
|
+
- agent system prompt
|
|
183
|
+
- chat instructions
|
|
184
|
+
- thread/session metadata
|
|
185
|
+
- current user message
|
|
186
|
+
|
|
187
|
+
Prompt composition must not include:
|
|
188
|
+
|
|
189
|
+
- bot tokens
|
|
190
|
+
- API keys
|
|
191
|
+
- raw `.env`
|
|
192
|
+
- unrelated chat history from other sessions
|
|
193
|
+
- other agents' private config unless explicitly allowed
|
|
194
|
+
|
|
195
|
+
## Database Rules
|
|
196
|
+
|
|
197
|
+
Use Drizzle repositories.
|
|
198
|
+
|
|
199
|
+
Initial tables expected:
|
|
200
|
+
|
|
201
|
+
- `agents`
|
|
202
|
+
- `agent_sessions`
|
|
203
|
+
- `telegram_chats`
|
|
204
|
+
- `telegram_chat_agents`
|
|
205
|
+
- `messages`
|
|
206
|
+
- `heartbeats`
|
|
207
|
+
- `runtime_kv`
|
|
208
|
+
|
|
209
|
+
SQLite now. PostgreSQL later. Avoid SQLite-only behavior in business logic.
|
|
210
|
+
|
|
211
|
+
## Safety Rules
|
|
212
|
+
|
|
213
|
+
Autonomous behavior needs guardrails.
|
|
214
|
+
|
|
215
|
+
- Heartbeat must support `NOOP`.
|
|
216
|
+
- Heartbeat must have cooldown/rate limits.
|
|
217
|
+
- Dangerous actions need policy checks.
|
|
218
|
+
- File writes and shell commands should be restricted by path/action policy.
|
|
219
|
+
- External sends should be auditable.
|
|
220
|
+
- Dynamic agent creation should require approval.
|
|
221
|
+
|
|
222
|
+
## Implementation Order
|
|
223
|
+
|
|
224
|
+
Follow `TODO.md`.
|
|
225
|
+
|
|
226
|
+
Recommended order:
|
|
227
|
+
|
|
228
|
+
1. project skeleton
|
|
229
|
+
2. config schemas
|
|
230
|
+
3. database schema/repositories
|
|
231
|
+
4. Telegram polling adapter
|
|
232
|
+
5. router
|
|
233
|
+
6. Pi runner
|
|
234
|
+
7. heartbeat
|
|
235
|
+
8. policy gates
|
|
236
|
+
9. Hono/webhooks later
|
|
237
|
+
|
|
238
|
+
## Docs
|
|
239
|
+
|
|
240
|
+
- Product/architecture plan: `PLAN.md`
|
|
241
|
+
- Task checklist: `TODO.md`
|
|
242
|
+
- Agent working instructions: `AGENTS.md`
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## Unreleased
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- Added `bunx @fickydev/pigent <dir>` scaffold/setup/run CLI path.
|
|
8
|
+
- Added one-command setup-and-run CLI at `src/cli/run.ts` and `bun run run`.
|
|
9
|
+
- Added interactive setup CLI at `src/cli/setup.ts` and `bun run setup`.
|
|
10
|
+
- Expanded setup CLI with quick/custom modes, environment prompts, chat routing prompts, and optional install/migrate/typecheck steps.
|
|
11
|
+
- Added `README.md` with setup, architecture, Telegram testing, routing, and development notes.
|
|
12
|
+
- Added project planning docs: `PLAN.md`, `TODO.md`, and `AGENTS.md`.
|
|
13
|
+
- Added Bun TypeScript project skeleton with startup/shutdown flow.
|
|
14
|
+
- Added config schemas and filesystem config loader.
|
|
15
|
+
- Added example coder agent and software engineer profile.
|
|
16
|
+
- Added Drizzle SQLite schema, migration config, and repositories.
|
|
17
|
+
- Added channel abstraction and Telegram polling adapter skeleton.
|
|
18
|
+
- Added Telegram Bot API wrapper for `getUpdates` and `sendMessage`.
|
|
19
|
+
- Wired Telegram polling startup when `TELEGRAM_BOT_TOKEN` is configured.
|
|
20
|
+
- Added message router for `@agentId`, `/agent <id>`, and chat default routing.
|
|
21
|
+
- Added fake agent runner that creates sessions and persists inbound/outbound messages.
|
|
22
|
+
- Wired routed Telegram messages to fake agent responses.
|
|
23
|
+
- Added Telegram `getMe` support and bot self-message filtering.
|
|
24
|
+
- Added `/help`, `/start`, and `/agents` bot commands.
|
|
25
|
+
- Added `PiAgentRunner` using Pi SDK sessions, read-only tools, and per-agent system prompts.
|
|
26
|
+
- Added fake-runner controls via `PIGENT_FAKE_AGENT` and `PIGENT_FALLBACK_FAKE_AGENT`.
|
|
27
|
+
- Added optional automatic Telegram chat setup via `PIGENT_AUTO_SETUP_CHATS`.
|
|
28
|
+
- Set default workspace root to `~/.pigent` and added tilde expansion for agent workspaces.
|
|
29
|
+
- Added npm package metadata and MIT license for publishing.
|
|
30
|
+
|
|
31
|
+
### Changed
|
|
32
|
+
|
|
33
|
+
- Updated `AGENTS.md` with rule to update `CHANGELOG.md` after work.
|
|
34
|
+
- Updated `AGENTS.md` with rule to update `TODO.md` after work.
|
|
35
|
+
- Updated `AGENTS.md` with rule to persist future `remember this` instructions.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ficky Dev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/PLAN.md
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
# Pigent Plan
|
|
2
|
+
|
|
3
|
+
## Vision
|
|
4
|
+
|
|
5
|
+
Build an autonomous multi-agent daemon with Pi as core execution engine. One transport, such as a Telegram bot, can route messages to many Pi-backed agents. Each agent can have its own profile, skills, extensions, tools, heartbeat policy, permissions, and per-channel session memory.
|
|
6
|
+
|
|
7
|
+
## Current Product Direction
|
|
8
|
+
|
|
9
|
+
Start with a local Bun daemon, not an HTTP server. Defer Hono until webhook-based channels, admin API, health endpoints, or dashboard backend become necessary.
|
|
10
|
+
|
|
11
|
+
Initial stack:
|
|
12
|
+
|
|
13
|
+
- Bun runtime and package manager
|
|
14
|
+
- TypeScript
|
|
15
|
+
- Pi SDK for agent sessions
|
|
16
|
+
- Drizzle ORM
|
|
17
|
+
- SQLite first, PostgreSQL later
|
|
18
|
+
- Telegram polling first
|
|
19
|
+
- File-based agent/profile config plus database-backed runtime state
|
|
20
|
+
|
|
21
|
+
## Core Concepts
|
|
22
|
+
|
|
23
|
+
### Transport
|
|
24
|
+
|
|
25
|
+
Transport connects external channels to the daemon. A transport receives inbound messages and sends outbound replies. Telegram bot is a transport, not an agent.
|
|
26
|
+
|
|
27
|
+
Examples:
|
|
28
|
+
|
|
29
|
+
- Telegram polling transport
|
|
30
|
+
- Telegram webhook transport later
|
|
31
|
+
- Slack webhook transport later
|
|
32
|
+
- WhatsApp Cloud API transport later
|
|
33
|
+
|
|
34
|
+
### Agent
|
|
35
|
+
|
|
36
|
+
Agent is a named Pi-backed worker with its own identity, profile, system prompt, skills, extensions, tools, and policy.
|
|
37
|
+
|
|
38
|
+
Examples:
|
|
39
|
+
|
|
40
|
+
- `coder`
|
|
41
|
+
- `reviewer`
|
|
42
|
+
- `devops`
|
|
43
|
+
- `researcher`
|
|
44
|
+
- `personal-assistant`
|
|
45
|
+
|
|
46
|
+
### Session
|
|
47
|
+
|
|
48
|
+
Session is conversation memory for one agent in one channel context.
|
|
49
|
+
|
|
50
|
+
Recommended Telegram session key:
|
|
51
|
+
|
|
52
|
+
```text
|
|
53
|
+
agentId + channel + chatId + threadId
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Private chat can omit `threadId`. Group forum topic should include `threadId`.
|
|
57
|
+
|
|
58
|
+
### Profile
|
|
59
|
+
|
|
60
|
+
Profile defines reusable behavior shared by agents.
|
|
61
|
+
|
|
62
|
+
Profile may include:
|
|
63
|
+
|
|
64
|
+
- model preference
|
|
65
|
+
- base system prompt
|
|
66
|
+
- response style
|
|
67
|
+
- default skills
|
|
68
|
+
- default extensions
|
|
69
|
+
- default permission policy
|
|
70
|
+
|
|
71
|
+
### Skill
|
|
72
|
+
|
|
73
|
+
Skill is Pi-compatible instruction bundle, usually a `SKILL.md` folder. Agents can load different skills.
|
|
74
|
+
|
|
75
|
+
### Extension
|
|
76
|
+
|
|
77
|
+
Extension is Pi-compatible TypeScript/JavaScript module that adds tools, hooks, commands, or runtime behavior. Agents can load different extensions.
|
|
78
|
+
|
|
79
|
+
### Heartbeat
|
|
80
|
+
|
|
81
|
+
Heartbeat is scheduled self-activation. It prompts an agent periodically to check tasks, channels, external state, or pending work.
|
|
82
|
+
|
|
83
|
+
Heartbeat must avoid spam via:
|
|
84
|
+
|
|
85
|
+
- `NOOP` convention
|
|
86
|
+
- cooldowns
|
|
87
|
+
- dedupe
|
|
88
|
+
- max messages per hour
|
|
89
|
+
- lock per agent/session
|
|
90
|
+
|
|
91
|
+
## Target Architecture
|
|
92
|
+
|
|
93
|
+
```text
|
|
94
|
+
Bun Daemon
|
|
95
|
+
├─ Config Loader
|
|
96
|
+
├─ Agent Registry
|
|
97
|
+
├─ Channel Transports
|
|
98
|
+
│ └─ Telegram Polling Transport
|
|
99
|
+
├─ Message Router
|
|
100
|
+
├─ Agent Orchestrator
|
|
101
|
+
│ ├─ Pi Session Factory
|
|
102
|
+
│ ├─ Pi Agent Runner
|
|
103
|
+
│ └─ Policy Engine
|
|
104
|
+
├─ Heartbeat Scheduler
|
|
105
|
+
└─ Drizzle State Store
|
|
106
|
+
└─ SQLite now, PostgreSQL later
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Message Flow
|
|
110
|
+
|
|
111
|
+
```text
|
|
112
|
+
Telegram update
|
|
113
|
+
↓
|
|
114
|
+
TelegramPollingTransport normalizes update
|
|
115
|
+
↓
|
|
116
|
+
MessageRouter selects target agent
|
|
117
|
+
↓
|
|
118
|
+
AgentRegistry loads agent config and chat config
|
|
119
|
+
↓
|
|
120
|
+
StateStore gets or creates agent session
|
|
121
|
+
↓
|
|
122
|
+
PiAgentRunner sends prompt to Pi SDK session
|
|
123
|
+
↓
|
|
124
|
+
Response returned to router
|
|
125
|
+
↓
|
|
126
|
+
Telegram transport sends reply
|
|
127
|
+
↓
|
|
128
|
+
Messages and session metadata persisted
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Routing Strategy
|
|
132
|
+
|
|
133
|
+
One Telegram bot can serve many agents.
|
|
134
|
+
|
|
135
|
+
Routing modes:
|
|
136
|
+
|
|
137
|
+
1. Default agent per Telegram chat
|
|
138
|
+
2. Explicit agent mention, such as `@coder review this`
|
|
139
|
+
3. Slash command, such as `/agent coder review this`
|
|
140
|
+
4. Auto-dispatcher agent later
|
|
141
|
+
|
|
142
|
+
Recommended MVP:
|
|
143
|
+
|
|
144
|
+
- one default agent per chat
|
|
145
|
+
- explicit `@agentId` override
|
|
146
|
+
- allowed agent list per chat
|
|
147
|
+
|
|
148
|
+
## Telegram Chat Configuration
|
|
149
|
+
|
|
150
|
+
Each Telegram group/private chat can define:
|
|
151
|
+
|
|
152
|
+
- default agent
|
|
153
|
+
- allowed agents
|
|
154
|
+
- custom instructions
|
|
155
|
+
- heartbeat settings
|
|
156
|
+
- enabled/disabled status
|
|
157
|
+
|
|
158
|
+
Example:
|
|
159
|
+
|
|
160
|
+
```yaml
|
|
161
|
+
telegramChats:
|
|
162
|
+
- chatId: "-100111"
|
|
163
|
+
title: Engineering
|
|
164
|
+
defaultAgent: coder
|
|
165
|
+
allowedAgents:
|
|
166
|
+
- coder
|
|
167
|
+
- reviewer
|
|
168
|
+
- devops
|
|
169
|
+
instructions: |
|
|
170
|
+
This is engineering group. Be concise. Prefer TypeScript.
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## Prompt Composition
|
|
174
|
+
|
|
175
|
+
For each request, compose context from:
|
|
176
|
+
|
|
177
|
+
1. base Pi system prompt
|
|
178
|
+
2. agent profile prompt
|
|
179
|
+
3. agent system prompt
|
|
180
|
+
4. channel/chat instructions
|
|
181
|
+
5. thread/session metadata
|
|
182
|
+
6. current user message
|
|
183
|
+
|
|
184
|
+
Never inject secrets into model context.
|
|
185
|
+
|
|
186
|
+
## State Model
|
|
187
|
+
|
|
188
|
+
Use Drizzle repositories. Do not spread raw Drizzle queries across runtime code.
|
|
189
|
+
|
|
190
|
+
Initial entities:
|
|
191
|
+
|
|
192
|
+
- agents
|
|
193
|
+
- agent sessions
|
|
194
|
+
- telegram chats
|
|
195
|
+
- telegram chat agents
|
|
196
|
+
- messages
|
|
197
|
+
- heartbeats
|
|
198
|
+
- tasks/events later
|
|
199
|
+
|
|
200
|
+
SQLite first. Keep schema types portable so PostgreSQL migration is straightforward.
|
|
201
|
+
|
|
202
|
+
## Safety Model
|
|
203
|
+
|
|
204
|
+
Autonomous agents need strict policy gates.
|
|
205
|
+
|
|
206
|
+
Policy controls:
|
|
207
|
+
|
|
208
|
+
- allowed filesystem paths
|
|
209
|
+
- blocked filesystem paths
|
|
210
|
+
- shell command allow/deny
|
|
211
|
+
- tool allow/deny
|
|
212
|
+
- external send approval
|
|
213
|
+
- destructive action approval
|
|
214
|
+
- heartbeat notification limits
|
|
215
|
+
|
|
216
|
+
Default policy should be conservative.
|
|
217
|
+
|
|
218
|
+
## File Layout
|
|
219
|
+
|
|
220
|
+
```text
|
|
221
|
+
src/
|
|
222
|
+
main.ts
|
|
223
|
+
|
|
224
|
+
daemon/
|
|
225
|
+
AgentDaemon.ts
|
|
226
|
+
HeartbeatScheduler.ts
|
|
227
|
+
|
|
228
|
+
channels/
|
|
229
|
+
types.ts
|
|
230
|
+
telegram/
|
|
231
|
+
TelegramPollingAdapter.ts
|
|
232
|
+
TelegramApi.ts
|
|
233
|
+
types.ts
|
|
234
|
+
|
|
235
|
+
agents/
|
|
236
|
+
AgentConfig.ts
|
|
237
|
+
AgentRegistry.ts
|
|
238
|
+
AgentRunner.ts
|
|
239
|
+
MessageRouter.ts
|
|
240
|
+
|
|
241
|
+
pi/
|
|
242
|
+
PiAgentRunner.ts
|
|
243
|
+
PiSessionFactory.ts
|
|
244
|
+
|
|
245
|
+
db/
|
|
246
|
+
client.ts
|
|
247
|
+
schema.ts
|
|
248
|
+
repositories/
|
|
249
|
+
AgentRepository.ts
|
|
250
|
+
MessageRepository.ts
|
|
251
|
+
SessionRepository.ts
|
|
252
|
+
TelegramRepository.ts
|
|
253
|
+
|
|
254
|
+
config/
|
|
255
|
+
loadConfig.ts
|
|
256
|
+
schemas.ts
|
|
257
|
+
|
|
258
|
+
policy/
|
|
259
|
+
PolicyEngine.ts
|
|
260
|
+
|
|
261
|
+
logging/
|
|
262
|
+
logger.ts
|
|
263
|
+
|
|
264
|
+
agents/
|
|
265
|
+
coder/
|
|
266
|
+
agent.yaml
|
|
267
|
+
SYSTEM.md
|
|
268
|
+
skills/
|
|
269
|
+
extensions/
|
|
270
|
+
|
|
271
|
+
profiles/
|
|
272
|
+
software-engineer.yaml
|
|
273
|
+
|
|
274
|
+
drizzle/
|
|
275
|
+
migrations/
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
## MVP Milestones
|
|
279
|
+
|
|
280
|
+
### Milestone 1: Skeleton
|
|
281
|
+
|
|
282
|
+
- Bun TypeScript project
|
|
283
|
+
- env loading
|
|
284
|
+
- structured logging
|
|
285
|
+
- graceful shutdown
|
|
286
|
+
- config loader
|
|
287
|
+
- initial docs
|
|
288
|
+
|
|
289
|
+
### Milestone 2: Database
|
|
290
|
+
|
|
291
|
+
- Drizzle SQLite client
|
|
292
|
+
- schema
|
|
293
|
+
- migrations
|
|
294
|
+
- repositories
|
|
295
|
+
- basic integration test
|
|
296
|
+
|
|
297
|
+
### Milestone 3: Agent Registry
|
|
298
|
+
|
|
299
|
+
- load agents from filesystem
|
|
300
|
+
- load profiles
|
|
301
|
+
- validate config with Zod
|
|
302
|
+
- expose list/get methods
|
|
303
|
+
|
|
304
|
+
### Milestone 4: Telegram Polling
|
|
305
|
+
|
|
306
|
+
- bot API wrapper
|
|
307
|
+
- polling loop
|
|
308
|
+
- update normalization
|
|
309
|
+
- outbound send
|
|
310
|
+
- offset persistence
|
|
311
|
+
|
|
312
|
+
### Milestone 5: Routing and Sessions
|
|
313
|
+
|
|
314
|
+
- default agent per chat
|
|
315
|
+
- `@agentId` route override
|
|
316
|
+
- allowed-agent enforcement
|
|
317
|
+
- per `agentId + chatId + threadId` session
|
|
318
|
+
|
|
319
|
+
### Milestone 6: Pi Runner
|
|
320
|
+
|
|
321
|
+
- create Pi SDK sessions
|
|
322
|
+
- send prompts
|
|
323
|
+
- return responses
|
|
324
|
+
- persist inbound/outbound messages
|
|
325
|
+
|
|
326
|
+
### Milestone 7: Heartbeat
|
|
327
|
+
|
|
328
|
+
- scheduler
|
|
329
|
+
- per-agent lock
|
|
330
|
+
- `NOOP` handling
|
|
331
|
+
- notification cooldown
|
|
332
|
+
- heartbeat history
|
|
333
|
+
|
|
334
|
+
### Milestone 8: Safety
|
|
335
|
+
|
|
336
|
+
- policy engine
|
|
337
|
+
- path/tool restrictions
|
|
338
|
+
- approval hooks design
|
|
339
|
+
- default conservative config
|
|
340
|
+
|
|
341
|
+
### Milestone 9: Webhooks/API Later
|
|
342
|
+
|
|
343
|
+
Add Hono only after daemon works. Use it for:
|
|
344
|
+
|
|
345
|
+
- Telegram webhooks
|
|
346
|
+
- Slack Events API
|
|
347
|
+
- WhatsApp Cloud API
|
|
348
|
+
- admin API
|
|
349
|
+
- health/readiness endpoints
|
|
350
|
+
- dashboard backend
|
|
351
|
+
|
|
352
|
+
## Non-Goals For MVP
|
|
353
|
+
|
|
354
|
+
- no Hono server
|
|
355
|
+
- no web UI
|
|
356
|
+
- no WhatsApp
|
|
357
|
+
- no Slack
|
|
358
|
+
- no vector database
|
|
359
|
+
- no dynamic npm package install
|
|
360
|
+
- no auto-created agents without approval
|
|
361
|
+
- no production deployment automation
|
|
362
|
+
|
|
363
|
+
## Open Questions
|
|
364
|
+
|
|
365
|
+
- Which Pi SDK session manager should be used for persistent sessions?
|
|
366
|
+
- How exactly should per-agent skills/extensions be loaded into Pi runtime?
|
|
367
|
+
- Should group instructions live in YAML, DB, or both?
|
|
368
|
+
- Should each chat have one default agent or multiple active agents?
|
|
369
|
+
- What minimum approval flow is needed before shell/file-write tools?
|