Metadata-Version: 2.5
Name: viola-agent
Version: 0.2.1
Summary: Self-hosted WhatsApp AI agent (Twilio) with an OpenAI-compatible API
Project-URL: Homepage, https://github.com/virpluzteam/viola-agent
Project-URL: Source, https://github.com/virpluzteam/viola-agent
Project-URL: Issues, https://github.com/virpluzteam/viola-agent/issues
Author: Xubin Ren, the viola contributors
License-Expression: MIT
License-File: LICENSE
License-File: THIRD_PARTY_NOTICES.md
Keywords: agent,ai,chatbot,llm,twilio,whatsapp
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: <3.14,>=3.11
Requires-Dist: aiohttp<4.0.0,>=3.9.0
Requires-Dist: anthropic<1.0.0,>=0.45.0
Requires-Dist: boto3>=1.43.0
Requires-Dist: chardet<6.0.0,>=3.0.2
Requires-Dist: croniter<7.0.0,>=6.0.0
Requires-Dist: ddgs<10.0.0,>=9.5.5
Requires-Dist: dingtalk-stream<1.0.0,>=0.24.0
Requires-Dist: dulwich<1.0.0,>=0.22.0
Requires-Dist: filelock>=3.25.2
Requires-Dist: httpx<1.0.0,>=0.28.0
Requires-Dist: jinja2<4.0.0,>=3.1.0
Requires-Dist: json-repair<1.0.0,>=0.57.0
Requires-Dist: lark-oapi<2.0.0,>=1.5.0
Requires-Dist: loguru<1.0.0,>=0.7.3
Requires-Dist: mcp<2.0.0,>=1.26.0
Requires-Dist: msgpack<2.0.0,>=1.1.0
Requires-Dist: oauth-cli-kit<1.0.0,>=0.1.3
Requires-Dist: openai>=2.8.0
Requires-Dist: openpyxl<4.0.0,>=3.1.0
Requires-Dist: prompt-toolkit<4.0.0,>=3.0.50
Requires-Dist: pydantic-settings<3.0.0,>=2.12.0
Requires-Dist: pydantic<3.0.0,>=2.12.0
Requires-Dist: pypdf<6.0.0,>=5.0.0
Requires-Dist: python-docx<2.0.0,>=1.1.0
Requires-Dist: python-dotenv<2.0.0,>=1.0.0
Requires-Dist: python-pptx<2.0.0,>=1.0.0
Requires-Dist: python-socketio<6.0.0,>=5.16.0
Requires-Dist: python-socks[asyncio]<3.0.0,>=2.8.0; sys_platform != 'win32'
Requires-Dist: python-telegram-bot[socks]<23.0,>=22.6
Requires-Dist: pyyaml<7.0.0,>=6.0
Requires-Dist: qq-botpy<2.0.0,>=1.2.0
Requires-Dist: questionary<3.0.0,>=2.0.0
Requires-Dist: readability-lxml<1.0.0,>=0.8.4
Requires-Dist: redis[hiredis]<6.0.0,>=5.0.0
Requires-Dist: rich<15.0.0,>=14.0.0
Requires-Dist: slack-sdk<4.0.0,>=3.39.0
Requires-Dist: slackify-markdown<1.0.0,>=0.2.0
Requires-Dist: socksio<2.0.0,>=1.0.0
Requires-Dist: tiktoken<1.0.0,>=0.12.0
Requires-Dist: typer<1.0.0,>=0.20.0
Requires-Dist: websocket-client<2.0.0,>=1.9.0
Requires-Dist: websockets<17.0,>=16.0
Provides-Extra: twilio
Requires-Dist: twilio<10.0.0,>=9.0.0; extra == 'twilio'
Description-Content-Type: text/markdown

# viola-agent

A self-hosted WhatsApp AI agent. A company deploys one copy, fills in its own Twilio,
LLM (OpenRouter or UniAPI) and optional billing credentials, and gets an assistant its
customers and staff can message on WhatsApp.

**→ Setup guide: [docs/getting-started.md](docs/getting-started.md)** covers what goes in
`.env`, starting it with Docker Compose, connecting Twilio, and customising the agent.

```bash
cp .env.example .env        # fill in Twilio + LLM (see the guide)
docker compose up -d        # image: ghcr.io/virpluzteam/viola-agent
```

Releases are published as a Docker image (`ghcr.io/virpluzteam/viola-agent`, amd64 and
arm64) and as a pip package (`pip install 'viola-agent[twilio]'`). Both are built from
the same wheel and read the same `.env`.

> **Status:** being generalised from an internal project. See [PLAN.md](PLAN.md) for the
> roadmap.

## Layout

- `viola/` — agent package. `viola serve` runs everything on one port: the Twilio
  WhatsApp webhook, an OpenAI-compatible API and `/health`
- `tests/` — pytest suite

## Run locally

```bash
python3.13 -m venv .venv && .venv/bin/pip install -e ".[twilio]" --group dev   # pip >= 25.1; or: uv sync --all-extras
cp .env.example .env   # fill in Twilio, LLM and (optionally) billing values,
                       # and set APP_ENV=development to run without Redis
.venv/bin/viola serve --port 8900
```

`viola serve` reads `.env` from the current directory (or `$VIOLA_ENV_FILE`) and stores
its data — `config.json`, the workspace with chat history, media, the billing outbox,
`viola.db` (notification idempotency), `content_sid_map.json` — in
`$VIOLA_DATA_DIR` (default `~/.viola`). Without `REDIS_URL` it uses in-memory dedup and rate
limits, which is fine for one local process; `APP_ENV=production` requires Redis.

Twilio must reach the webhook over public HTTPS. For a server, see
[Expose it over HTTPS](docs/getting-started.md#expose-it-over-https) (bundled Caddy profile
or a Cloudflare Tunnel; both publish only `/twilio/*`). For development, run `ngrok http 8900`,
set `PUBLIC_BASE_URL=https://<ngrok-host>` in `.env`, and set Twilio's "A message comes in"
URL to `https://<ngrok-host>/twilio/webhook`. Requests whose `X-Twilio-Signature` does not
match `PUBLIC_BASE_URL` + path are rejected with `403`.

Routes:

- `POST /twilio/webhook` — Twilio inbound; verifies the signature, ignores repeated
  `MessageSid`s, rate-limits per sender, replies `202` and answers in the background
- `GET /twilio/webhook` — plain-text probe
- `POST /twilio/status` — optional Twilio status callback (acknowledged)
- `POST /v1/chat/completions` — OpenAI-compatible API for the portal backend: needs the
  server key and a `user` in the body; the session is `portal:<user>:<session_id>`
- `POST /v1/notify` — send a proactive WhatsApp message (see below)

Every `/v1/*` route needs `Authorization: Bearer $VIOLA_API_SERVER_KEY` (`401` otherwise) and
returns `404` when no server key is configured.

Tools are off by default. Each channel profile (`whatsapp`, `portal`) gets only the tools
listed for it in `config.json` → `profiles`; see [docs/phase5-channels.md](docs/phase5-channels.md).
- `GET /health` — JSON health

## Proactive messages and templates

WhatsApp lets a business send free text only within **24 hours** of the customer's last
message; outside that window only Meta-approved templates may be sent. Every inbound
message opens the window (stored in Redis, or memory in development). `/v1/notify`, cron
reminders and heartbeat all go through `viola.notify.send`:

- window open and `fallback_text` given → the text is sent
- otherwise an approved `template_id` → the template is sent
- otherwise nothing is sent: `422 template_required` with the reason (a rule, not a failure)

```bash
curl -X POST https://<host>/v1/notify \
  -H "Authorization: Bearer $VIOLA_API_SERVER_KEY" -H "Content-Type: application/json" \
  -d '{"to": "+85212345678", "template_id": "reminder",
       "variables": {"reminder_text": "your appointment is tomorrow at 10:00"},
       "fallback_text": "Hi! Your appointment is tomorrow at 10:00.",
       "idempotency_key": "appt-123-reminder"}'
```

Responses: `200` sent (`mode` is `text` or `template`), `422 template_required`,
`502 twilio_error`, `409` same `idempotency_key` with a different body (or still sending),
`400` bad request or missing `idempotency_key`, `401` bad server key. A repeated
`idempotency_key` never sends twice: it returns the first result (kept for 7 days in
`$VIOLA_DATA_DIR/viola.db`). A key whose send failed may be retried.

Templates live in `viola/notify/templates.yaml` (welcome, reminder, follow-up, generic
notification; `en` and `zh_HK`). To use your own, copy it to
`$VIOLA_DATA_DIR/templates.yaml` and edit it. Then create them in your Twilio account:

```bash
viola templates sync      # create in Twilio + submit to Meta (--dry-run, --no-submit, --only)
viola templates status    # refresh approval status (approved / pending / rejected)
```

ContentSids and approval statuses are kept in `$VIOLA_DATA_DIR/content_sid_map.json`;
only `approved` templates are used. Cron reminders outside the window use the `reminder`
template.

## Development

```bash
.venv/bin/ruff check viola/
.venv/bin/pytest
```

CI (`.github/workflows/ci.yml`) runs ruff, pytest (Python 3.11–3.13, Linux and Windows),
a gitleaks scan of the full history (false positives go in `.gitleaksignore`), a wheel
build and an image build with a smoke test (`.github/scripts/smoke-image.sh`).

### Releasing

1. Set `version` in `pyproject.toml` (e.g. `0.2.1`) and merge to `main`.
2. Tag that commit and push the tag:

   ```bash
   git tag v0.2.1 && git push origin v0.2.1
   ```

`.github/workflows/release.yml` checks the tag matches the version, runs CI, then
publishes the wheel and sdist to PyPI, pushes the image to GHCR as `0.2.1`, `0.2` and
`latest`, and creates a GitHub release. One-time setup is described at the top of that
file (PyPI trusted publisher, the `pypi` environment, making the GHCR package public).

## License

MIT — see [LICENSE](LICENSE).
