aitextaroo 0.1.0__tar.gz
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.
- aitextaroo-0.1.0/LICENSE +21 -0
- aitextaroo-0.1.0/PKG-INFO +193 -0
- aitextaroo-0.1.0/README.md +160 -0
- aitextaroo-0.1.0/aitextaroo/__init__.py +18 -0
- aitextaroo-0.1.0/aitextaroo/agents.py +235 -0
- aitextaroo-0.1.0/aitextaroo/bridge.py +157 -0
- aitextaroo-0.1.0/aitextaroo/cli.py +108 -0
- aitextaroo-0.1.0/aitextaroo/client.py +403 -0
- aitextaroo-0.1.0/aitextaroo/commands.py +106 -0
- aitextaroo-0.1.0/aitextaroo/conversation.py +93 -0
- aitextaroo-0.1.0/aitextaroo/formatting.py +55 -0
- aitextaroo-0.1.0/aitextaroo/models.py +88 -0
- aitextaroo-0.1.0/aitextaroo/setup.py +125 -0
- aitextaroo-0.1.0/aitextaroo.egg-info/PKG-INFO +193 -0
- aitextaroo-0.1.0/aitextaroo.egg-info/SOURCES.txt +26 -0
- aitextaroo-0.1.0/aitextaroo.egg-info/dependency_links.txt +1 -0
- aitextaroo-0.1.0/aitextaroo.egg-info/entry_points.txt +3 -0
- aitextaroo-0.1.0/aitextaroo.egg-info/requires.txt +9 -0
- aitextaroo-0.1.0/aitextaroo.egg-info/top_level.txt +1 -0
- aitextaroo-0.1.0/pyproject.toml +64 -0
- aitextaroo-0.1.0/setup.cfg +4 -0
- aitextaroo-0.1.0/tests/test_agents.py +107 -0
- aitextaroo-0.1.0/tests/test_bridge.py +259 -0
- aitextaroo-0.1.0/tests/test_client.py +61 -0
- aitextaroo-0.1.0/tests/test_commands.py +90 -0
- aitextaroo-0.1.0/tests/test_conversation.py +76 -0
- aitextaroo-0.1.0/tests/test_formatting.py +65 -0
- aitextaroo-0.1.0/tests/test_models.py +60 -0
aitextaroo-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 rwcrabtree
|
|
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.
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: aitextaroo
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Client library for AI Text-a-roo — give any AI agent a phone number
|
|
5
|
+
Author-email: AI Text-a-roo <hello@aitextaroo.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://aitextaroo.com
|
|
8
|
+
Project-URL: Documentation, https://github.com/rwfresh/aitextaroo-clients
|
|
9
|
+
Project-URL: Repository, https://github.com/rwfresh/aitextaroo-clients
|
|
10
|
+
Project-URL: Issues, https://github.com/rwfresh/aitextaroo-clients/issues
|
|
11
|
+
Keywords: sms,ai,agent,chatbot,text-messaging
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Topic :: Communications :: Chat
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Requires-Dist: httpx>=0.27.0
|
|
26
|
+
Provides-Extra: bridge
|
|
27
|
+
Requires-Dist: pyyaml>=6.0; extra == "bridge"
|
|
28
|
+
Provides-Extra: dev
|
|
29
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
30
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == "dev"
|
|
31
|
+
Requires-Dist: ruff>=0.4.0; extra == "dev"
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
# AI Text-a-roo
|
|
35
|
+
|
|
36
|
+
Give any AI agent a phone number. In 60 seconds.
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
pip install aitextaroo
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## What is this?
|
|
43
|
+
|
|
44
|
+
[AI Text-a-roo](https://aitextaroo.com) is an SMS gateway for AI agents. You sign up, get a phone number, and your agent can send and receive text messages.
|
|
45
|
+
|
|
46
|
+
This library connects your agent to the gateway. No webhook, no public IP, no server setup — just one outbound HTTPS connection.
|
|
47
|
+
|
|
48
|
+
## Setup
|
|
49
|
+
|
|
50
|
+
### Let your agent do it
|
|
51
|
+
|
|
52
|
+
Tell your agent:
|
|
53
|
+
|
|
54
|
+
> Go to aitextaroo.com and set up SMS for me.
|
|
55
|
+
|
|
56
|
+
Your agent will find the setup instructions and handle everything.
|
|
57
|
+
|
|
58
|
+
### Or use the CLI
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
# 1. Install
|
|
62
|
+
pip install aitextaroo
|
|
63
|
+
|
|
64
|
+
# 2. Verify your phone (sends a 6-digit code)
|
|
65
|
+
aitextaroo-setup --phone +12125551234
|
|
66
|
+
|
|
67
|
+
# 3. Enter the code, get your API key
|
|
68
|
+
aitextaroo-setup --phone +12125551234 --code 123456
|
|
69
|
+
|
|
70
|
+
# 4. Start the bridge
|
|
71
|
+
aitextaroo-bridge --api-key YOUR_KEY
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
The bridge auto-detects your AI agent and starts piping SMS through it.
|
|
75
|
+
|
|
76
|
+
Supported agents: `claude` (Claude Code), `hermes`, `openclaw`, `nanoclaw`.
|
|
77
|
+
|
|
78
|
+
## How it works
|
|
79
|
+
|
|
80
|
+
```
|
|
81
|
+
Your phone AI Text-a-roo Your machine
|
|
82
|
+
│ │ │
|
|
83
|
+
├── SMS "hey" ──────────► │ │
|
|
84
|
+
│ ├── SSE stream ──────────► │
|
|
85
|
+
│ │ ├── Agent processes
|
|
86
|
+
│ │ POST /v1/send ◄────────┤ the message
|
|
87
|
+
│ ◄── SMS "hello!" ──────┤ │
|
|
88
|
+
│ │ │
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
1. User texts your assigned number
|
|
92
|
+
2. AI Text-a-roo pushes the message via SSE (Server-Sent Events)
|
|
93
|
+
3. Your agent processes it and replies via `client.send()`
|
|
94
|
+
4. The reply is delivered as SMS
|
|
95
|
+
|
|
96
|
+
No polling. No webhooks. No public IP. Works behind NAT, firewalls, VPNs.
|
|
97
|
+
|
|
98
|
+
## Features
|
|
99
|
+
|
|
100
|
+
### Conversation history
|
|
101
|
+
|
|
102
|
+
The bridge keeps conversation context in memory so your agent can have
|
|
103
|
+
coherent multi-turn conversations over SMS. History is local — nothing
|
|
104
|
+
is stored on our servers.
|
|
105
|
+
|
|
106
|
+
### SMS formatting
|
|
107
|
+
|
|
108
|
+
Agent responses are automatically cleaned for SMS — markdown bold,
|
|
109
|
+
italic, code blocks, and headers are stripped to plain text.
|
|
110
|
+
|
|
111
|
+
### SMS commands
|
|
112
|
+
|
|
113
|
+
Your user can text these commands:
|
|
114
|
+
|
|
115
|
+
| Command | What it does |
|
|
116
|
+
|-----------|----------------------------------------|
|
|
117
|
+
| `/help` | List available commands |
|
|
118
|
+
| `/new` | Start a fresh conversation |
|
|
119
|
+
| `/status` | Show agent name, uptime, message count |
|
|
120
|
+
|
|
121
|
+
## Python API
|
|
122
|
+
|
|
123
|
+
### TextarooClient
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
import asyncio
|
|
127
|
+
from aitextaroo import TextarooClient
|
|
128
|
+
|
|
129
|
+
async def main():
|
|
130
|
+
client = TextarooClient(api_key="your-key")
|
|
131
|
+
|
|
132
|
+
async for message in client.listen():
|
|
133
|
+
print(f"Got: {message.text}")
|
|
134
|
+
await client.send(f"You said: {message.text}")
|
|
135
|
+
|
|
136
|
+
asyncio.run(main())
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
#### `listen(auto_reconnect=True)` → `AsyncIterator[InboundMessage]`
|
|
140
|
+
|
|
141
|
+
Opens an SSE stream and yields messages as they arrive. Handles reconnection automatically.
|
|
142
|
+
|
|
143
|
+
#### `send(text)` → `str`
|
|
144
|
+
|
|
145
|
+
Send an SMS to the registered user. Returns the message ID.
|
|
146
|
+
|
|
147
|
+
#### `account()` → `dict`
|
|
148
|
+
|
|
149
|
+
Get account info (tier, assigned number, usage).
|
|
150
|
+
|
|
151
|
+
#### `close()`
|
|
152
|
+
|
|
153
|
+
Release resources.
|
|
154
|
+
|
|
155
|
+
### Signup API (static methods)
|
|
156
|
+
|
|
157
|
+
```python
|
|
158
|
+
# Send verification code
|
|
159
|
+
await TextarooClient.request_verification(phone="+12125551234")
|
|
160
|
+
|
|
161
|
+
# Confirm and get API key
|
|
162
|
+
result = await TextarooClient.confirm_verification(phone="+12125551234", code="123456")
|
|
163
|
+
print(result["api_key"])
|
|
164
|
+
print(result["assigned_number"])
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### InboundMessage
|
|
168
|
+
|
|
169
|
+
| Field | Type | Description |
|
|
170
|
+
|---------------|--------|--------------------------|
|
|
171
|
+
| `id` | `str` | Unique message ID (UUID) |
|
|
172
|
+
| `text` | `str` | Message body |
|
|
173
|
+
| `received_at` | `str` | ISO 8601 timestamp |
|
|
174
|
+
| `channel` | `str` | Always `"sms"` |
|
|
175
|
+
| `has_pin` | `bool` | Whether sender has a PIN |
|
|
176
|
+
|
|
177
|
+
## Privacy
|
|
178
|
+
|
|
179
|
+
- Messages pass through and disappear — nothing is stored on our servers
|
|
180
|
+
- Conversation history lives in your bridge process memory only
|
|
181
|
+
- When the bridge stops, history is gone
|
|
182
|
+
- Your phone number is hashed and encrypted at rest
|
|
183
|
+
|
|
184
|
+
## Environment Variables
|
|
185
|
+
|
|
186
|
+
| Variable | Description |
|
|
187
|
+
|-----------------------|------------------------------------------|
|
|
188
|
+
| `AITEXTAROO_API_KEY` | Your API key (alternative to `--api-key`) |
|
|
189
|
+
| `AITEXTAROO_BASE_URL` | Custom API URL (for self-hosted) |
|
|
190
|
+
|
|
191
|
+
## License
|
|
192
|
+
|
|
193
|
+
MIT
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# AI Text-a-roo
|
|
2
|
+
|
|
3
|
+
Give any AI agent a phone number. In 60 seconds.
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
pip install aitextaroo
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## What is this?
|
|
10
|
+
|
|
11
|
+
[AI Text-a-roo](https://aitextaroo.com) is an SMS gateway for AI agents. You sign up, get a phone number, and your agent can send and receive text messages.
|
|
12
|
+
|
|
13
|
+
This library connects your agent to the gateway. No webhook, no public IP, no server setup — just one outbound HTTPS connection.
|
|
14
|
+
|
|
15
|
+
## Setup
|
|
16
|
+
|
|
17
|
+
### Let your agent do it
|
|
18
|
+
|
|
19
|
+
Tell your agent:
|
|
20
|
+
|
|
21
|
+
> Go to aitextaroo.com and set up SMS for me.
|
|
22
|
+
|
|
23
|
+
Your agent will find the setup instructions and handle everything.
|
|
24
|
+
|
|
25
|
+
### Or use the CLI
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
# 1. Install
|
|
29
|
+
pip install aitextaroo
|
|
30
|
+
|
|
31
|
+
# 2. Verify your phone (sends a 6-digit code)
|
|
32
|
+
aitextaroo-setup --phone +12125551234
|
|
33
|
+
|
|
34
|
+
# 3. Enter the code, get your API key
|
|
35
|
+
aitextaroo-setup --phone +12125551234 --code 123456
|
|
36
|
+
|
|
37
|
+
# 4. Start the bridge
|
|
38
|
+
aitextaroo-bridge --api-key YOUR_KEY
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
The bridge auto-detects your AI agent and starts piping SMS through it.
|
|
42
|
+
|
|
43
|
+
Supported agents: `claude` (Claude Code), `hermes`, `openclaw`, `nanoclaw`.
|
|
44
|
+
|
|
45
|
+
## How it works
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
Your phone AI Text-a-roo Your machine
|
|
49
|
+
│ │ │
|
|
50
|
+
├── SMS "hey" ──────────► │ │
|
|
51
|
+
│ ├── SSE stream ──────────► │
|
|
52
|
+
│ │ ├── Agent processes
|
|
53
|
+
│ │ POST /v1/send ◄────────┤ the message
|
|
54
|
+
│ ◄── SMS "hello!" ──────┤ │
|
|
55
|
+
│ │ │
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
1. User texts your assigned number
|
|
59
|
+
2. AI Text-a-roo pushes the message via SSE (Server-Sent Events)
|
|
60
|
+
3. Your agent processes it and replies via `client.send()`
|
|
61
|
+
4. The reply is delivered as SMS
|
|
62
|
+
|
|
63
|
+
No polling. No webhooks. No public IP. Works behind NAT, firewalls, VPNs.
|
|
64
|
+
|
|
65
|
+
## Features
|
|
66
|
+
|
|
67
|
+
### Conversation history
|
|
68
|
+
|
|
69
|
+
The bridge keeps conversation context in memory so your agent can have
|
|
70
|
+
coherent multi-turn conversations over SMS. History is local — nothing
|
|
71
|
+
is stored on our servers.
|
|
72
|
+
|
|
73
|
+
### SMS formatting
|
|
74
|
+
|
|
75
|
+
Agent responses are automatically cleaned for SMS — markdown bold,
|
|
76
|
+
italic, code blocks, and headers are stripped to plain text.
|
|
77
|
+
|
|
78
|
+
### SMS commands
|
|
79
|
+
|
|
80
|
+
Your user can text these commands:
|
|
81
|
+
|
|
82
|
+
| Command | What it does |
|
|
83
|
+
|-----------|----------------------------------------|
|
|
84
|
+
| `/help` | List available commands |
|
|
85
|
+
| `/new` | Start a fresh conversation |
|
|
86
|
+
| `/status` | Show agent name, uptime, message count |
|
|
87
|
+
|
|
88
|
+
## Python API
|
|
89
|
+
|
|
90
|
+
### TextarooClient
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
import asyncio
|
|
94
|
+
from aitextaroo import TextarooClient
|
|
95
|
+
|
|
96
|
+
async def main():
|
|
97
|
+
client = TextarooClient(api_key="your-key")
|
|
98
|
+
|
|
99
|
+
async for message in client.listen():
|
|
100
|
+
print(f"Got: {message.text}")
|
|
101
|
+
await client.send(f"You said: {message.text}")
|
|
102
|
+
|
|
103
|
+
asyncio.run(main())
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
#### `listen(auto_reconnect=True)` → `AsyncIterator[InboundMessage]`
|
|
107
|
+
|
|
108
|
+
Opens an SSE stream and yields messages as they arrive. Handles reconnection automatically.
|
|
109
|
+
|
|
110
|
+
#### `send(text)` → `str`
|
|
111
|
+
|
|
112
|
+
Send an SMS to the registered user. Returns the message ID.
|
|
113
|
+
|
|
114
|
+
#### `account()` → `dict`
|
|
115
|
+
|
|
116
|
+
Get account info (tier, assigned number, usage).
|
|
117
|
+
|
|
118
|
+
#### `close()`
|
|
119
|
+
|
|
120
|
+
Release resources.
|
|
121
|
+
|
|
122
|
+
### Signup API (static methods)
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
# Send verification code
|
|
126
|
+
await TextarooClient.request_verification(phone="+12125551234")
|
|
127
|
+
|
|
128
|
+
# Confirm and get API key
|
|
129
|
+
result = await TextarooClient.confirm_verification(phone="+12125551234", code="123456")
|
|
130
|
+
print(result["api_key"])
|
|
131
|
+
print(result["assigned_number"])
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### InboundMessage
|
|
135
|
+
|
|
136
|
+
| Field | Type | Description |
|
|
137
|
+
|---------------|--------|--------------------------|
|
|
138
|
+
| `id` | `str` | Unique message ID (UUID) |
|
|
139
|
+
| `text` | `str` | Message body |
|
|
140
|
+
| `received_at` | `str` | ISO 8601 timestamp |
|
|
141
|
+
| `channel` | `str` | Always `"sms"` |
|
|
142
|
+
| `has_pin` | `bool` | Whether sender has a PIN |
|
|
143
|
+
|
|
144
|
+
## Privacy
|
|
145
|
+
|
|
146
|
+
- Messages pass through and disappear — nothing is stored on our servers
|
|
147
|
+
- Conversation history lives in your bridge process memory only
|
|
148
|
+
- When the bridge stops, history is gone
|
|
149
|
+
- Your phone number is hashed and encrypted at rest
|
|
150
|
+
|
|
151
|
+
## Environment Variables
|
|
152
|
+
|
|
153
|
+
| Variable | Description |
|
|
154
|
+
|-----------------------|------------------------------------------|
|
|
155
|
+
| `AITEXTAROO_API_KEY` | Your API key (alternative to `--api-key`) |
|
|
156
|
+
| `AITEXTAROO_BASE_URL` | Custom API URL (for self-hosted) |
|
|
157
|
+
|
|
158
|
+
## License
|
|
159
|
+
|
|
160
|
+
MIT
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""AI Text-a-roo — give any AI agent a phone number.
|
|
2
|
+
|
|
3
|
+
Connect to the AI Text-a-roo SMS gateway to receive and send
|
|
4
|
+
text messages through any AI agent framework.
|
|
5
|
+
|
|
6
|
+
Example:
|
|
7
|
+
>>> from aitextaroo import TextarooClient
|
|
8
|
+
>>> client = TextarooClient(api_key="your-key")
|
|
9
|
+
>>> async for message in client.listen():
|
|
10
|
+
... print(f"Got: {message.text}")
|
|
11
|
+
... await client.send(f"You said: {message.text}")
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from aitextaroo.client import TextarooClient
|
|
15
|
+
from aitextaroo.models import InboundMessage, StreamEvent
|
|
16
|
+
|
|
17
|
+
__version__ = "0.1.0"
|
|
18
|
+
__all__ = ["TextarooClient", "InboundMessage", "StreamEvent"]
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
"""Agent abstraction for the AI Text-a-roo bridge.
|
|
2
|
+
|
|
3
|
+
Defines how the bridge communicates with AI agents. Each agent
|
|
4
|
+
takes a text message in and produces a text response.
|
|
5
|
+
|
|
6
|
+
The only implementation today is CliAgent — a one-shot subprocess
|
|
7
|
+
invocation per message. New agent types (HTTP API, long-running
|
|
8
|
+
process, etc.) can be added by implementing the Agent protocol.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import asyncio
|
|
14
|
+
import logging
|
|
15
|
+
import shutil
|
|
16
|
+
from abc import ABC, abstractmethod
|
|
17
|
+
from dataclasses import dataclass, field
|
|
18
|
+
|
|
19
|
+
logger = logging.getLogger(__name__)
|
|
20
|
+
|
|
21
|
+
# Maximum time to wait for an agent to respond (seconds).
|
|
22
|
+
DEFAULT_TIMEOUT = 120
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class Agent(ABC):
|
|
26
|
+
"""Interface for an AI agent that processes text messages.
|
|
27
|
+
|
|
28
|
+
Implementations must be able to:
|
|
29
|
+
1. Accept a text prompt and return a text response.
|
|
30
|
+
2. Provide a display name and optional system prompt.
|
|
31
|
+
3. Release any held resources on close.
|
|
32
|
+
|
|
33
|
+
The bridge reads system_prompt to build conversation context.
|
|
34
|
+
The agent receives the fully-assembled prompt — it does not
|
|
35
|
+
need to manage conversation state or system instructions.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
@property
|
|
39
|
+
@abstractmethod
|
|
40
|
+
def name(self) -> str:
|
|
41
|
+
"""Human-readable name for display and logging."""
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
def system_prompt(self) -> str:
|
|
45
|
+
"""System instructions prepended to every prompt by the bridge.
|
|
46
|
+
|
|
47
|
+
Override to provide agent-specific context (e.g., "respond
|
|
48
|
+
concisely, these are SMS messages"). Default is empty.
|
|
49
|
+
"""
|
|
50
|
+
return ""
|
|
51
|
+
|
|
52
|
+
@abstractmethod
|
|
53
|
+
async def ask(self, text: str) -> str:
|
|
54
|
+
"""Send a fully-assembled prompt and return the response.
|
|
55
|
+
|
|
56
|
+
The prompt already includes system instructions and
|
|
57
|
+
conversation history (assembled by the bridge). The agent
|
|
58
|
+
just needs to run it.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
text: The complete prompt to send.
|
|
62
|
+
|
|
63
|
+
Returns:
|
|
64
|
+
The agent's response text.
|
|
65
|
+
|
|
66
|
+
Raises:
|
|
67
|
+
asyncio.TimeoutError: If the agent doesn't respond in time.
|
|
68
|
+
RuntimeError: If the agent process fails.
|
|
69
|
+
"""
|
|
70
|
+
|
|
71
|
+
async def close(self) -> None:
|
|
72
|
+
"""Release any resources held by this agent.
|
|
73
|
+
|
|
74
|
+
Default implementation does nothing. Override if your agent
|
|
75
|
+
holds open connections or processes.
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
# ── CLI Agent ────────────────────────────────────────────────────
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@dataclass(frozen=True, slots=True)
|
|
83
|
+
class CliAgentConfig:
|
|
84
|
+
"""How to invoke a CLI agent.
|
|
85
|
+
|
|
86
|
+
Attributes:
|
|
87
|
+
name: Human-readable name (for logging and display).
|
|
88
|
+
command: Executable name (must be on PATH).
|
|
89
|
+
args: Arguments placed before the prompt.
|
|
90
|
+
system_prompt: Context the bridge prepends to prompts.
|
|
91
|
+
timeout: Max seconds to wait for a response.
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
name: str
|
|
95
|
+
command: str
|
|
96
|
+
args: list[str] = field(default_factory=list)
|
|
97
|
+
system_prompt: str = ""
|
|
98
|
+
timeout: float = DEFAULT_TIMEOUT
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class CliAgent(Agent):
|
|
102
|
+
"""Runs a CLI agent as a one-shot subprocess per message.
|
|
103
|
+
|
|
104
|
+
Each call to ask() spawns a fresh process:
|
|
105
|
+
{command} {args} "{prompt}"
|
|
106
|
+
|
|
107
|
+
Stdout is captured as the response. Stderr is logged on failure.
|
|
108
|
+
This is the simplest and most reliable approach — no process
|
|
109
|
+
lifecycle management, no stdin/stdout protocol, no state leaks.
|
|
110
|
+
|
|
111
|
+
The agent receives the complete prompt from the bridge. It does
|
|
112
|
+
not manage system prompts or conversation context — that's the
|
|
113
|
+
bridge's responsibility.
|
|
114
|
+
|
|
115
|
+
Args:
|
|
116
|
+
config: How to invoke the agent CLI.
|
|
117
|
+
"""
|
|
118
|
+
|
|
119
|
+
def __init__(self, config: CliAgentConfig) -> None:
|
|
120
|
+
self._config = config
|
|
121
|
+
|
|
122
|
+
@property
|
|
123
|
+
def name(self) -> str:
|
|
124
|
+
return self._config.name
|
|
125
|
+
|
|
126
|
+
@property
|
|
127
|
+
def system_prompt(self) -> str:
|
|
128
|
+
return self._config.system_prompt
|
|
129
|
+
|
|
130
|
+
async def ask(self, text: str) -> str:
|
|
131
|
+
"""Spawn the agent CLI with the prompt and return its output."""
|
|
132
|
+
cmd = [self._config.command, *self._config.args, text]
|
|
133
|
+
|
|
134
|
+
logger.debug("Running: %s %s '%s...'", cmd[0], " ".join(cmd[1:-1]), text[:40])
|
|
135
|
+
|
|
136
|
+
process = await asyncio.create_subprocess_exec(
|
|
137
|
+
*cmd,
|
|
138
|
+
stdout=asyncio.subprocess.PIPE,
|
|
139
|
+
stderr=asyncio.subprocess.PIPE,
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
try:
|
|
143
|
+
stdout, stderr = await asyncio.wait_for(
|
|
144
|
+
process.communicate(),
|
|
145
|
+
timeout=self._config.timeout,
|
|
146
|
+
)
|
|
147
|
+
except asyncio.TimeoutError:
|
|
148
|
+
process.kill()
|
|
149
|
+
await process.wait()
|
|
150
|
+
raise
|
|
151
|
+
|
|
152
|
+
if process.returncode != 0:
|
|
153
|
+
error_text = stderr.decode(errors="replace")[:200]
|
|
154
|
+
logger.warning(
|
|
155
|
+
"Agent %s exited with code %d: %s",
|
|
156
|
+
self._config.name, process.returncode, error_text,
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
return stdout.decode(errors="replace").strip()
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
# ── Agent Registry ───────────────────────────────────────────────
|
|
163
|
+
|
|
164
|
+
# Built-in agent configurations. Each entry defines how to invoke
|
|
165
|
+
# a known CLI agent. The bridge uses these to auto-detect and
|
|
166
|
+
# instantiate agents.
|
|
167
|
+
#
|
|
168
|
+
# To add a new agent: add an entry here and it works automatically.
|
|
169
|
+
# No other code changes needed.
|
|
170
|
+
|
|
171
|
+
BUILTIN_AGENTS: dict[str, CliAgentConfig] = {
|
|
172
|
+
"claude": CliAgentConfig(
|
|
173
|
+
name="Claude Code",
|
|
174
|
+
command="claude",
|
|
175
|
+
args=["-p"],
|
|
176
|
+
system_prompt=(
|
|
177
|
+
"You are receiving SMS text messages from a user. "
|
|
178
|
+
"Respond concisely — these are text messages with a 1600 character limit."
|
|
179
|
+
),
|
|
180
|
+
),
|
|
181
|
+
"hermes": CliAgentConfig(
|
|
182
|
+
name="Hermes",
|
|
183
|
+
command="hermes",
|
|
184
|
+
args=["--pipe"],
|
|
185
|
+
system_prompt=(
|
|
186
|
+
"You are receiving SMS text messages from a user via AI Text-a-roo. "
|
|
187
|
+
"Respond conversationally. Keep responses concise — they're text messages."
|
|
188
|
+
),
|
|
189
|
+
),
|
|
190
|
+
"openclaw": CliAgentConfig(
|
|
191
|
+
name="OpenClaw",
|
|
192
|
+
command="openclaw",
|
|
193
|
+
args=["--pipe"],
|
|
194
|
+
system_prompt="You are receiving SMS text messages. Respond concisely.",
|
|
195
|
+
),
|
|
196
|
+
"nanoclaw": CliAgentConfig(
|
|
197
|
+
name="NanoClaw",
|
|
198
|
+
command="nanoclaw",
|
|
199
|
+
args=["--pipe"],
|
|
200
|
+
system_prompt="You are receiving SMS text messages. Respond concisely.",
|
|
201
|
+
),
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def detect_agents() -> list[str]:
|
|
206
|
+
"""Find which supported agents are installed on this system.
|
|
207
|
+
|
|
208
|
+
Returns:
|
|
209
|
+
Agent names found on PATH, in preference order.
|
|
210
|
+
"""
|
|
211
|
+
found = []
|
|
212
|
+
for name, config in BUILTIN_AGENTS.items():
|
|
213
|
+
if shutil.which(config.command):
|
|
214
|
+
found.append(name)
|
|
215
|
+
logger.debug("Detected agent: %s (%s)", config.name, config.command)
|
|
216
|
+
return found
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def create_agent(name: str) -> CliAgent:
|
|
220
|
+
"""Create an agent instance by name.
|
|
221
|
+
|
|
222
|
+
Args:
|
|
223
|
+
name: Key in BUILTIN_AGENTS (e.g., "claude", "hermes").
|
|
224
|
+
|
|
225
|
+
Returns:
|
|
226
|
+
A ready-to-use CliAgent.
|
|
227
|
+
|
|
228
|
+
Raises:
|
|
229
|
+
ValueError: If the agent name is not recognized.
|
|
230
|
+
"""
|
|
231
|
+
config = BUILTIN_AGENTS.get(name)
|
|
232
|
+
if config is None:
|
|
233
|
+
available = ", ".join(BUILTIN_AGENTS.keys())
|
|
234
|
+
raise ValueError(f"Unknown agent: {name!r}. Available: {available}")
|
|
235
|
+
return CliAgent(config)
|