75 lines
3.2 KiB
Markdown
75 lines
3.2 KiB
Markdown
# Gateway (viktor_gateway.py) — Viktor web-chat as an OpenAI-compatible LLM
|
|
|
|
`tools/viktor_gateway.py` turns a pool of Viktor JWTs (harvested by the Slack
|
|
reger) into a local OpenAI-compatible endpoint. Viktor's web-chat is an agent
|
|
product with no public API — this bridge reverse-engineers the SPA's own
|
|
web-chat protocol.
|
|
|
|
## Protocol (extracted from `hooks-BoSLX5O_.js`)
|
|
|
|
REST base `https://api.viktor.com`, prefix `/api/coworker/app-chat`,
|
|
header `Authorization: Bearer <jwt>`:
|
|
|
|
| Call | Shape |
|
|
|------|-------|
|
|
| `POST /threads` | `{"title": str}` -> `{"id": thread_id}` |
|
|
| `POST /threads/{id}/messages` | `{"content": str, "model_option"?: str}` -> `{"id": msg_id}` |
|
|
| `POST /threads/{id}/stop` | stop generation |
|
|
| `GET /projects` | sanity check (also `chief_of_staff` state) |
|
|
| `GET /api/coworker/workspace-info` | refreshes JWT via `sliding-session` response header |
|
|
|
|
WebSocket `wss://api.viktor.com/api/coworker/app-chat/ws`:
|
|
|
|
```jsonc
|
|
// client -> server
|
|
{"a": "auth", "token": "<jwt>"}
|
|
{"a": "watch", "thread_id": "<id>"} // or with "after": ts for tail
|
|
// server -> client
|
|
{"e": "hb", "interval_s": N} // heartbeat
|
|
{"e": "history", "scope": "thread", ...} // catch-up frames
|
|
{"e": "row", "row": {"thread_id","seq","kind","content",...}} // message rows
|
|
{"e": "tok_snapshot"|"tok_delta", "thread_id","message_id","kind","text","done"}
|
|
{"e": "live", "scope": "thread", "thread_id": "..."} // history complete
|
|
```
|
|
|
|
Streaming = accumulate `tok_delta.text` per `message_id` (skip `kind=thinking`),
|
|
final text also lands as a `row` event.
|
|
|
|
## Gateway endpoints
|
|
|
|
- `GET /v1/models` — lists `viktor-coworker`
|
|
- `POST /v1/chat/completions` — non-stream + `stream:true` (SSE `data:` chunks)
|
|
- `GET /health` — pool status (`pool_total` / `pool_live` / `live_keys`)
|
|
|
|
System prompt is prepended to the user message as a `[system]...[/system]`
|
|
block; the last user message is the payload.
|
|
|
|
## Pool
|
|
|
|
`accounts.jsonl` (same dir) — one JSON per line; needs `"jwt"` (or
|
|
`"exchange_token"`, which the gateway converts via `/api/auth/jwt_token`).
|
|
Round-robin rotation; a token that gets 401/403 is disabled in `gw_pool.json`.
|
|
Pool hot-reloads on every request, so a running reger feeding `accounts.jsonl`
|
|
grows the gateway live.
|
|
|
|
JWTs carry `exp` (~hours); re-run the reger or implement sliding-session
|
|
refresh (`workspace-info` response header) to extend.
|
|
|
|
## Run
|
|
|
|
```bash
|
|
pip install websockets # only external dep (stdlib http.server otherwise)
|
|
export VIKTOR_GW_PORT=8700 # default
|
|
python viktor_gateway.py
|
|
|
|
curl -s localhost:8700/health
|
|
curl -s localhost:8700/v1/chat/completions -H 'Content-Type: application/json' \
|
|
-d '{"model":"viktor-coworker","messages":[{"role":"user","content":"hi"}]}'
|
|
```
|
|
|
|
Env: `VIKTOR_GW_PORT`, `VIKTOR_GW_MODEL`, `VIKTOR_GW_TIMEOUT` (reply deadline, default 240s).
|
|
|
|
## Credits note (2026-10-09)
|
|
|
|
Fresh Slack-workspace accounts start with 0 credits: message send returns HTTP 402 `This workspace is out of credits`. Auth itself works (workspace-info 200). The gateway surfaces 402 verbatim in `content`. Farming value depends on whether Viktor grants starter credits via UI onboarding (chief_of_staff setup) — see IDE-GUIDE.md.
|