Deploy GAME ChatAgent bridge (README.md)
Browse files
README.md
CHANGED
|
@@ -7,4 +7,103 @@ sdk: docker
|
|
| 7 |
pinned: false
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
pinned: false
|
| 8 |
---
|
| 9 |
|
| 10 |
+
# bba-chat
|
| 11 |
+
|
| 12 |
+
Ask AI backend for the Big Brain Ape dashboard. The live Space is [simzy-bba-chat.hf.space](https://simzy-bba-chat.hf.space) (`Simzy/bba-chat`).
|
| 13 |
+
|
| 14 |
+
The dashboard posts a question and polls this inbox for a `role=agent` reply. This server stores those messages and, after each `POST /send`, asks a Virtuals GAME `ChatAgent` that speaks as Big Brain Ape / BigBrain2Bot (Virtuals agent #96114, DegenClaw #1729).
|
| 15 |
+
|
| 16 |
+
`@BigBrain2Bot` on Telegram stays on Virtuals. This Space does not register a Telegram webhook.
|
| 17 |
+
|
| 18 |
+
## How Ask AI uses it
|
| 19 |
+
|
| 20 |
+
The dashboard (`app35.html`) does this:
|
| 21 |
+
|
| 22 |
+
1. `POST /send` with `{"text": "...", "user": "analysis-app"}`.
|
| 23 |
+
2. Poll `GET /messages?since=<user message ts>` about every 4 seconds, for up to 120 seconds.
|
| 24 |
+
3. Render the first `role=agent` message whose `ts` is greater than that user timestamp.
|
| 25 |
+
|
| 26 |
+
The tasks page uses the same inbox with `user: "Simzy"`.
|
| 27 |
+
|
| 28 |
+
Agent replies are produced by `game-sdk` 0.1.5:
|
| 29 |
+
|
| 30 |
+
```python
|
| 31 |
+
from game_sdk.game.chat_agent import Chat, ChatAgent
|
| 32 |
+
|
| 33 |
+
agent = ChatAgent(api_key=os.environ["GAME_API_KEY"], prompt=AGENT_PROMPT)
|
| 34 |
+
chat = agent.create_chat(partner_id=user, partner_name=user)
|
| 35 |
+
response = chat.next(user_text) # response.message is stored as role=agent
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
`ChatAgent` lives in `game_sdk.game.chat_agent` (not `game_sdk.game.agent`). The published 0.1.5 wheel includes that module. The key must start with `apt-` or the SDK raises before any call.
|
| 39 |
+
|
| 40 |
+
Sessions are per `user`. The SDK has no save/resume helper, so this app stores `conversation_id` in `$BBA_CHAT_DATA/sessions.json` and rebuilds `Chat(conversation_id, client)` for the next turn. If a conversation is finished or the next call fails, the id is dropped and `create_chat` runs again.
|
| 41 |
+
|
| 42 |
+
The Ask AI page hides agent text that contains `telegram`, `on it`, `get back to you`, or `I'll review`. GAME replies are rewritten so those phrases do not swallow the answer. `POST /respond` text is stored as sent.
|
| 43 |
+
|
| 44 |
+
## HTTP contract
|
| 45 |
+
|
| 46 |
+
| Method | Path | Body / query | Notes |
|
| 47 |
+
| --- | --- | --- | --- |
|
| 48 |
+
| GET | `/` | | Health plus message count and whether `GAME_API_KEY` is set |
|
| 49 |
+
| GET | `/health` | | Same booleans as `/` |
|
| 50 |
+
| GET | `/messages` | `since` (unix seconds, default 0) | `{"messages": [...], "total": N}` |
|
| 51 |
+
| POST | `/send` | `{"text", "user"}` | Stores `role=user`, then replies in the background |
|
| 52 |
+
| POST | `/respond` | `{"text", "secret"}` | Manual `role=agent` write. Does not call GAME |
|
| 53 |
+
| GET | `/pending` | `secret` | User messages after the latest agent message |
|
| 54 |
+
|
| 55 |
+
User message shape: `id`, `ts`, `role` (`user`), `text`, `user`.
|
| 56 |
+
|
| 57 |
+
Agent message shape: `id`, `ts`, `role` (`agent`), `text`.
|
| 58 |
+
|
| 59 |
+
`/` and `/health` include `game_api_key_set` and `game_api_key_v2` (true only when the key starts with `apt-`). They never return the key.
|
| 60 |
+
|
| 61 |
+
Without `GAME_API_KEY`, `/send` still returns immediately and stores an agent message that says the key is not configured. A key that does not start with `apt-` stores that error instead of raising.
|
| 62 |
+
|
| 63 |
+
## Secrets Casey must set on the Space
|
| 64 |
+
|
| 65 |
+
In the Space: **Settings → Variables and secrets → New secret**.
|
| 66 |
+
|
| 67 |
+
| Secret | Required | Purpose |
|
| 68 |
+
| --- | --- | --- |
|
| 69 |
+
| `GAME_API_KEY` | Yes, for live replies | Virtuals GAME v2 key. Must start with `apt-`. From the GAME console. |
|
| 70 |
+
| `BBA_CHAT_SECRET` | Recommended | Protects `POST /respond` and `GET /pending`. If unset, the process falls back to `bba-chat-2026`. |
|
| 71 |
+
|
| 72 |
+
Do not put either value in the Dockerfile or the repo. The image only sets `BBA_CHAT_DATA=/data`.
|
| 73 |
+
|
| 74 |
+
Optional runtime setting (not a secret): `BBA_CHAT_GAME_TIMEOUT` seconds for the GAME HTTP calls. Default 75, clamped to 10–110 so a failure still lands inside the dashboard's 120 second poll.
|
| 75 |
+
|
| 76 |
+
After saving secrets, restart the Space so the container sees them.
|
| 77 |
+
|
| 78 |
+
## Health check
|
| 79 |
+
|
| 80 |
+
```bash
|
| 81 |
+
curl -s https://simzy-bba-chat.hf.space/health
|
| 82 |
+
```
|
| 83 |
+
|
| 84 |
+
Expect `"status": "ok"` and `"game_api_key_set": true` once the secret is present. Then:
|
| 85 |
+
|
| 86 |
+
```bash
|
| 87 |
+
curl -s -X POST https://simzy-bba-chat.hf.space/send \
|
| 88 |
+
-H 'Content-Type: application/json' \
|
| 89 |
+
-d '{"text":"What are the three lenses?","user":"casey"}'
|
| 90 |
+
curl -s 'https://simzy-bba-chat.hf.space/messages?since=0'
|
| 91 |
+
```
|
| 92 |
+
|
| 93 |
+
A GAME reply usually shows up within 30–90 seconds. If the key is missing, the agent error row is written as soon as the background task runs.
|
| 94 |
+
|
| 95 |
+
## Run locally
|
| 96 |
+
|
| 97 |
+
```bash
|
| 98 |
+
pip install -r requirements.txt
|
| 99 |
+
export BBA_CHAT_DATA=/tmp/bba-chat
|
| 100 |
+
export GAME_API_KEY=apt-your-key
|
| 101 |
+
export BBA_CHAT_SECRET=choose-a-secret
|
| 102 |
+
python app.py
|
| 103 |
+
```
|
| 104 |
+
|
| 105 |
+
The process listens on `0.0.0.0:7860`. Messages go to `$BBA_CHAT_DATA/messages.json`. `chat.json` in this repo is the old stub and is not the live store.
|
| 106 |
+
|
| 107 |
+
## Deploy
|
| 108 |
+
|
| 109 |
+
This repo is the Space source: `Dockerfile`, `app.py`, `requirements.txt`. Point the Hugging Face Space at this repo, or copy these files into `Simzy/bba-chat`, then set the secrets above and rebuild. Rebuilding without `GAME_API_KEY` keeps the inbox up and stores the not-configured agent message.
|