optio-chat-hub 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.
- optio_chat_hub-0.1.0/PKG-INFO +164 -0
- optio_chat_hub-0.1.0/README.md +134 -0
- optio_chat_hub-0.1.0/pyproject.toml +58 -0
- optio_chat_hub-0.1.0/setup.cfg +4 -0
- optio_chat_hub-0.1.0/src/optio_chat_hub/__init__.py +171 -0
- optio_chat_hub-0.1.0/src/optio_chat_hub/cli.py +614 -0
- optio_chat_hub-0.1.0/src/optio_chat_hub/codec.py +645 -0
- optio_chat_hub-0.1.0/src/optio_chat_hub/core.py +2072 -0
- optio_chat_hub-0.1.0/src/optio_chat_hub/images.py +268 -0
- optio_chat_hub-0.1.0/src/optio_chat_hub/lease.py +247 -0
- optio_chat_hub-0.1.0/src/optio_chat_hub/limits.py +22 -0
- optio_chat_hub-0.1.0/src/optio_chat_hub/packets.py +202 -0
- optio_chat_hub-0.1.0/src/optio_chat_hub/serving.py +85 -0
- optio_chat_hub-0.1.0/src/optio_chat_hub/settings.py +386 -0
- optio_chat_hub-0.1.0/src/optio_chat_hub/store.py +1152 -0
- optio_chat_hub-0.1.0/src/optio_chat_hub/testing.py +1370 -0
- optio_chat_hub-0.1.0/src/optio_chat_hub/transport.py +380 -0
- optio_chat_hub-0.1.0/src/optio_chat_hub.egg-info/PKG-INFO +164 -0
- optio_chat_hub-0.1.0/src/optio_chat_hub.egg-info/SOURCES.txt +55 -0
- optio_chat_hub-0.1.0/src/optio_chat_hub.egg-info/dependency_links.txt +1 -0
- optio_chat_hub-0.1.0/src/optio_chat_hub.egg-info/entry_points.txt +2 -0
- optio_chat_hub-0.1.0/src/optio_chat_hub.egg-info/requires.txt +10 -0
- optio_chat_hub-0.1.0/src/optio_chat_hub.egg-info/top_level.txt +1 -0
- optio_chat_hub-0.1.0/tests/test_cli_run.py +707 -0
- optio_chat_hub-0.1.0/tests/test_codec.py +686 -0
- optio_chat_hub-0.1.0/tests/test_command.py +442 -0
- optio_chat_hub-0.1.0/tests/test_conformance_files.py +491 -0
- optio_chat_hub-0.1.0/tests/test_conformance_paho.py +997 -0
- optio_chat_hub-0.1.0/tests/test_conformance_participation.py +497 -0
- optio_chat_hub-0.1.0/tests/test_conformance_shared_names.py +701 -0
- optio_chat_hub-0.1.0/tests/test_core.py +1842 -0
- optio_chat_hub-0.1.0/tests/test_fake_hub.py +714 -0
- optio_chat_hub-0.1.0/tests/test_fake_hub_attach_and_marks.py +377 -0
- optio_chat_hub-0.1.0/tests/test_fake_hub_avatar_changes.py +183 -0
- optio_chat_hub-0.1.0/tests/test_fake_hub_files.py +586 -0
- optio_chat_hub-0.1.0/tests/test_fake_hub_participation.py +512 -0
- optio_chat_hub-0.1.0/tests/test_fake_hub_shared_names.py +580 -0
- optio_chat_hub-0.1.0/tests/test_health.py +218 -0
- optio_chat_hub-0.1.0/tests/test_hub_announcements.py +268 -0
- optio_chat_hub-0.1.0/tests/test_hub_attach_only_and_read_marks.py +379 -0
- optio_chat_hub-0.1.0/tests/test_hub_avatar_changes.py +224 -0
- optio_chat_hub-0.1.0/tests/test_hub_files.py +1429 -0
- optio_chat_hub-0.1.0/tests/test_hub_listing_leave.py +176 -0
- optio_chat_hub-0.1.0/tests/test_hub_migration.py +340 -0
- optio_chat_hub-0.1.0/tests/test_hub_profiles.py +173 -0
- optio_chat_hub-0.1.0/tests/test_hub_shared_fanout.py +455 -0
- optio_chat_hub-0.1.0/tests/test_hub_shared_lifecycle.py +623 -0
- optio_chat_hub-0.1.0/tests/test_images_preview.py +205 -0
- optio_chat_hub-0.1.0/tests/test_integration_client_files.py +305 -0
- optio_chat_hub-0.1.0/tests/test_integration_client_hub.py +310 -0
- optio_chat_hub-0.1.0/tests/test_integration_shared_names.py +447 -0
- optio_chat_hub-0.1.0/tests/test_lease.py +449 -0
- optio_chat_hub-0.1.0/tests/test_package.py +51 -0
- optio_chat_hub-0.1.0/tests/test_serving.py +110 -0
- optio_chat_hub-0.1.0/tests/test_settings.py +478 -0
- optio_chat_hub-0.1.0/tests/test_store.py +418 -0
- optio_chat_hub-0.1.0/tests/test_transport.py +597 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: optio-chat-hub
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: The optio agent chat hub: an MQTT 5 broker keeping its state in MongoDB, run inside an optio engine or on its own (optio-chat-hub)
|
|
5
|
+
Author-email: Kristof Csillag <kristof.csillag@deai-labs.com>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/deai-network/optio
|
|
8
|
+
Project-URL: Repository, https://github.com/deai-network/optio
|
|
9
|
+
Project-URL: Issues, https://github.com/deai-network/optio/issues
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
16
|
+
Classifier: Operating System :: MacOS
|
|
17
|
+
Classifier: Topic :: Communications :: Chat
|
|
18
|
+
Classifier: Framework :: AsyncIO
|
|
19
|
+
Requires-Python: >=3.11
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
Requires-Dist: optio-chat<0.5,>=0.4
|
|
22
|
+
Requires-Dist: motor>=3.3.0
|
|
23
|
+
Requires-Dist: aiohttp>=3.9
|
|
24
|
+
Requires-Dist: Pillow>=10
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
27
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == "dev"
|
|
28
|
+
Requires-Dist: pytest-xdist>=3.6; extra == "dev"
|
|
29
|
+
Requires-Dist: packaging>=23; extra == "dev"
|
|
30
|
+
|
|
31
|
+
# optio-chat-hub
|
|
32
|
+
|
|
33
|
+
The optio agent chat hub: an MQTT 5 broker that keeps names, channels, memberships, undelivered
|
|
34
|
+
messages, avatars and chat attachments in MongoDB, so a restart loses nothing. An optio engine
|
|
35
|
+
runs one inside unless it uses another hub as a client; the `optio-chat-hub` command runs one on
|
|
36
|
+
its own, a fixed meeting point that stays up while optio apps come and go. Agents and people
|
|
37
|
+
connect to it with optio-chat's client library, its `optio-chat` command and its agent plugin,
|
|
38
|
+
and optio engines connect to it in client mode.
|
|
39
|
+
|
|
40
|
+
## Install
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
pip install optio-chat-hub
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Python 3.11 or newer, and a MongoDB the hub can reach.
|
|
47
|
+
|
|
48
|
+
## Run
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
optio-chat-hub
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The hub runs in the foreground until Ctrl-C, SIGTERM or SIGHUP (closing the terminal or tmux pane
|
|
55
|
+
it runs in), and logs to stderr. At the first start it writes its settings file, with a free port
|
|
56
|
+
and a generated password; every later start reads it, so the port and the password stay the same.
|
|
57
|
+
It logs where it listens, its database and prefix, and what other servers need:
|
|
58
|
+
|
|
59
|
+
```
|
|
60
|
+
... INFO optio_chat_hub.cli: optio-chat-hub: listening on ws://127.0.0.1:40123/ (MQTT over WebSocket; GET /health)
|
|
61
|
+
... INFO optio_chat_hub.cli: optio-chat-hub: database optio_chat_hub, prefix hub; settings /home/me/.config/optio-chat-hub/settings.json
|
|
62
|
+
... INFO optio_chat_hub.cli: optio-chat-hub: other servers set OPTIO_CHAT_HUB=wss://<your domain>/ and OPTIO_CHAT_SHARED_PASSWORD to the password in /home/me/.config/optio-chat-hub/settings.json
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Each optio server that connects is logged once (`optio-chat-hub: optio server <id> connected`).
|
|
66
|
+
The password itself never appears in the log, nor do the credentials of the MongoDB URL.
|
|
67
|
+
|
|
68
|
+
Exit codes: 0 after a clean stop (also one during the start), 1 after an error, 2 when the start
|
|
69
|
+
is refused (bad settings, MongoDB not answering, another hub on the same database and prefix, a
|
|
70
|
+
taken port) or another hub took over. Code 2 covers refusals that pass as well as ones that last:
|
|
71
|
+
after a reboot MongoDB or the bind address may not be up yet, and a killed hub's lease holds for
|
|
72
|
+
up to 30 s. A supervisor (systemd, a restart loop) should restart the hub on every exit code,
|
|
73
|
+
after a pause.
|
|
74
|
+
|
|
75
|
+
## The settings file
|
|
76
|
+
|
|
77
|
+
`~/.config/optio-chat-hub/settings.json` (the folder is made with mode 0700, the file 0600: it
|
|
78
|
+
holds the password):
|
|
79
|
+
|
|
80
|
+
| Key | Default | Rule |
|
|
81
|
+
|---|---|---|
|
|
82
|
+
| `bind` | `"127.0.0.1"` | the interface to listen on |
|
|
83
|
+
| `port` | a free port, picked at the first start | 1-65535, then kept |
|
|
84
|
+
| `password` | a generated random secret | the hub's shared password |
|
|
85
|
+
| `mongo_url` | `"mongodb://localhost:27017"` | a `mongodb://` or `mongodb+srv://` URL, its user name and password percent-encoded (`urllib.parse.quote_plus`) |
|
|
86
|
+
| `database` | `"optio_chat_hub"` | 1-63 letters, digits, `_` or `-` |
|
|
87
|
+
| `prefix` | `"hub"` | as `database`; the hub's collections are `<prefix>_chat_*` |
|
|
88
|
+
| `max_file_mb` | `10` | the largest chat attachment, whole MiB from 1 to 100 |
|
|
89
|
+
| `retention_days` | `7` | how long an attachment is kept after the last message that carried it (messages too, once the hub keeps history), whole days from 1 to 36500 |
|
|
90
|
+
|
|
91
|
+
A key you leave out takes its default and is written back. An unknown key or a bad value (a
|
|
92
|
+
number written as a string, say) stops the start with a message naming the key, and the file
|
|
93
|
+
stays as it was. The hub makes the file private to you at every start; it stops when it cannot
|
|
94
|
+
(a file another user owns). When it rewrites the file it replaces it, so a symlinked
|
|
95
|
+
`settings.json` becomes a regular file.
|
|
96
|
+
|
|
97
|
+
## Flags and the password variable
|
|
98
|
+
|
|
99
|
+
- `--bind HOST` and `--port N` set the listen address and are saved to the settings file.
|
|
100
|
+
- `--config PATH` uses another settings file.
|
|
101
|
+
- `OPTIO_CHAT_SHARED_PASSWORD`, when set and not empty, is the hub's password instead of the
|
|
102
|
+
file's (for admins who keep secrets elsewhere). It is never written to the file.
|
|
103
|
+
|
|
104
|
+
Use a fixed port for a deployment, so your proxy can point at it: start once with
|
|
105
|
+
`--port 8770` (or any free port), and the hub keeps it. If the saved port is taken, the start
|
|
106
|
+
stops with a message; the hub never moves to another port.
|
|
107
|
+
|
|
108
|
+
## Behind a TLS proxy
|
|
109
|
+
|
|
110
|
+
TLS is not the hub's concern. One port carries MQTT over WebSocket on `/` and the health check on
|
|
111
|
+
`/health`; put your reverse proxy in front of it. With Caddy:
|
|
112
|
+
|
|
113
|
+
```
|
|
114
|
+
hub.example.com {
|
|
115
|
+
reverse_proxy 127.0.0.1:8770
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Caddy forwards WebSocket upgrades as they are, so nothing else is needed. Keep `bind` on
|
|
120
|
+
`127.0.0.1` (or the address your proxy reaches) so that the hub is not open to the network
|
|
121
|
+
without TLS. On a public port, give your proxy a connection limit: before a client has
|
|
122
|
+
authenticated, the hub may hold up to 6 MiB of one WebSocket message per connection for up to
|
|
123
|
+
10 s, and it sets no cap on connections itself.
|
|
124
|
+
|
|
125
|
+
## Health for monitors
|
|
126
|
+
|
|
127
|
+
`GET /health` on the hub's port answers `200 {"status": "ok"}` while the hub runs and its
|
|
128
|
+
database answers, else `503 {"status": "unavailable"}`, within about two seconds either way (a
|
|
129
|
+
database that hangs included). A monitor may ask every second.
|
|
130
|
+
|
|
131
|
+
## Two hubs on one machine
|
|
132
|
+
|
|
133
|
+
Give each its own settings file, port and database (or prefix):
|
|
134
|
+
|
|
135
|
+
```
|
|
136
|
+
optio-chat-hub --config ~/.config/optio-chat-hub/team-a.json --port 8770
|
|
137
|
+
optio-chat-hub --config ~/.config/optio-chat-hub/team-b.json --port 8771
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
and set a different `database` or `prefix` in the second file. Only one hub may serve a database
|
|
141
|
+
and prefix: the second one to start on the same pair is refused with a message naming it. A hub
|
|
142
|
+
that was killed holds that pair for up to 30 s more; a clean stop gives it back at once.
|
|
143
|
+
|
|
144
|
+
The hosts that share a MongoDB this way should keep their clocks in sync: a host whose clock is
|
|
145
|
+
more than 20 s ahead can take over the pair from a live hub. An optio engine's own hub does not
|
|
146
|
+
take part in this check, so never point the command at an engine's database and prefix.
|
|
147
|
+
|
|
148
|
+
## What other servers set
|
|
149
|
+
|
|
150
|
+
On every server that should use this hub, as optio engines read them (client mode), and as
|
|
151
|
+
optio-chat's `optio-chat` command and plugin read them:
|
|
152
|
+
|
|
153
|
+
```
|
|
154
|
+
OPTIO_CHAT_HUB=wss://hub.example.com/
|
|
155
|
+
OPTIO_CHAT_SHARED_PASSWORD=<the password from the settings file>
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Upgrading
|
|
159
|
+
|
|
160
|
+
When the hub starts on a store an older hub wrote, it converts the store once, one way: an older
|
|
161
|
+
hub cannot read it afterwards. A hub upgrade comes before the clients that need its new features
|
|
162
|
+
(optio-chat's README lists them).
|
|
163
|
+
|
|
164
|
+
Design: `docs/2026-11-28-standalone-chat-hub-design.md` in the optio repository.
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# optio-chat-hub
|
|
2
|
+
|
|
3
|
+
The optio agent chat hub: an MQTT 5 broker that keeps names, channels, memberships, undelivered
|
|
4
|
+
messages, avatars and chat attachments in MongoDB, so a restart loses nothing. An optio engine
|
|
5
|
+
runs one inside unless it uses another hub as a client; the `optio-chat-hub` command runs one on
|
|
6
|
+
its own, a fixed meeting point that stays up while optio apps come and go. Agents and people
|
|
7
|
+
connect to it with optio-chat's client library, its `optio-chat` command and its agent plugin,
|
|
8
|
+
and optio engines connect to it in client mode.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
pip install optio-chat-hub
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Python 3.11 or newer, and a MongoDB the hub can reach.
|
|
17
|
+
|
|
18
|
+
## Run
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
optio-chat-hub
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The hub runs in the foreground until Ctrl-C, SIGTERM or SIGHUP (closing the terminal or tmux pane
|
|
25
|
+
it runs in), and logs to stderr. At the first start it writes its settings file, with a free port
|
|
26
|
+
and a generated password; every later start reads it, so the port and the password stay the same.
|
|
27
|
+
It logs where it listens, its database and prefix, and what other servers need:
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
... INFO optio_chat_hub.cli: optio-chat-hub: listening on ws://127.0.0.1:40123/ (MQTT over WebSocket; GET /health)
|
|
31
|
+
... INFO optio_chat_hub.cli: optio-chat-hub: database optio_chat_hub, prefix hub; settings /home/me/.config/optio-chat-hub/settings.json
|
|
32
|
+
... INFO optio_chat_hub.cli: optio-chat-hub: other servers set OPTIO_CHAT_HUB=wss://<your domain>/ and OPTIO_CHAT_SHARED_PASSWORD to the password in /home/me/.config/optio-chat-hub/settings.json
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Each optio server that connects is logged once (`optio-chat-hub: optio server <id> connected`).
|
|
36
|
+
The password itself never appears in the log, nor do the credentials of the MongoDB URL.
|
|
37
|
+
|
|
38
|
+
Exit codes: 0 after a clean stop (also one during the start), 1 after an error, 2 when the start
|
|
39
|
+
is refused (bad settings, MongoDB not answering, another hub on the same database and prefix, a
|
|
40
|
+
taken port) or another hub took over. Code 2 covers refusals that pass as well as ones that last:
|
|
41
|
+
after a reboot MongoDB or the bind address may not be up yet, and a killed hub's lease holds for
|
|
42
|
+
up to 30 s. A supervisor (systemd, a restart loop) should restart the hub on every exit code,
|
|
43
|
+
after a pause.
|
|
44
|
+
|
|
45
|
+
## The settings file
|
|
46
|
+
|
|
47
|
+
`~/.config/optio-chat-hub/settings.json` (the folder is made with mode 0700, the file 0600: it
|
|
48
|
+
holds the password):
|
|
49
|
+
|
|
50
|
+
| Key | Default | Rule |
|
|
51
|
+
|---|---|---|
|
|
52
|
+
| `bind` | `"127.0.0.1"` | the interface to listen on |
|
|
53
|
+
| `port` | a free port, picked at the first start | 1-65535, then kept |
|
|
54
|
+
| `password` | a generated random secret | the hub's shared password |
|
|
55
|
+
| `mongo_url` | `"mongodb://localhost:27017"` | a `mongodb://` or `mongodb+srv://` URL, its user name and password percent-encoded (`urllib.parse.quote_plus`) |
|
|
56
|
+
| `database` | `"optio_chat_hub"` | 1-63 letters, digits, `_` or `-` |
|
|
57
|
+
| `prefix` | `"hub"` | as `database`; the hub's collections are `<prefix>_chat_*` |
|
|
58
|
+
| `max_file_mb` | `10` | the largest chat attachment, whole MiB from 1 to 100 |
|
|
59
|
+
| `retention_days` | `7` | how long an attachment is kept after the last message that carried it (messages too, once the hub keeps history), whole days from 1 to 36500 |
|
|
60
|
+
|
|
61
|
+
A key you leave out takes its default and is written back. An unknown key or a bad value (a
|
|
62
|
+
number written as a string, say) stops the start with a message naming the key, and the file
|
|
63
|
+
stays as it was. The hub makes the file private to you at every start; it stops when it cannot
|
|
64
|
+
(a file another user owns). When it rewrites the file it replaces it, so a symlinked
|
|
65
|
+
`settings.json` becomes a regular file.
|
|
66
|
+
|
|
67
|
+
## Flags and the password variable
|
|
68
|
+
|
|
69
|
+
- `--bind HOST` and `--port N` set the listen address and are saved to the settings file.
|
|
70
|
+
- `--config PATH` uses another settings file.
|
|
71
|
+
- `OPTIO_CHAT_SHARED_PASSWORD`, when set and not empty, is the hub's password instead of the
|
|
72
|
+
file's (for admins who keep secrets elsewhere). It is never written to the file.
|
|
73
|
+
|
|
74
|
+
Use a fixed port for a deployment, so your proxy can point at it: start once with
|
|
75
|
+
`--port 8770` (or any free port), and the hub keeps it. If the saved port is taken, the start
|
|
76
|
+
stops with a message; the hub never moves to another port.
|
|
77
|
+
|
|
78
|
+
## Behind a TLS proxy
|
|
79
|
+
|
|
80
|
+
TLS is not the hub's concern. One port carries MQTT over WebSocket on `/` and the health check on
|
|
81
|
+
`/health`; put your reverse proxy in front of it. With Caddy:
|
|
82
|
+
|
|
83
|
+
```
|
|
84
|
+
hub.example.com {
|
|
85
|
+
reverse_proxy 127.0.0.1:8770
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Caddy forwards WebSocket upgrades as they are, so nothing else is needed. Keep `bind` on
|
|
90
|
+
`127.0.0.1` (or the address your proxy reaches) so that the hub is not open to the network
|
|
91
|
+
without TLS. On a public port, give your proxy a connection limit: before a client has
|
|
92
|
+
authenticated, the hub may hold up to 6 MiB of one WebSocket message per connection for up to
|
|
93
|
+
10 s, and it sets no cap on connections itself.
|
|
94
|
+
|
|
95
|
+
## Health for monitors
|
|
96
|
+
|
|
97
|
+
`GET /health` on the hub's port answers `200 {"status": "ok"}` while the hub runs and its
|
|
98
|
+
database answers, else `503 {"status": "unavailable"}`, within about two seconds either way (a
|
|
99
|
+
database that hangs included). A monitor may ask every second.
|
|
100
|
+
|
|
101
|
+
## Two hubs on one machine
|
|
102
|
+
|
|
103
|
+
Give each its own settings file, port and database (or prefix):
|
|
104
|
+
|
|
105
|
+
```
|
|
106
|
+
optio-chat-hub --config ~/.config/optio-chat-hub/team-a.json --port 8770
|
|
107
|
+
optio-chat-hub --config ~/.config/optio-chat-hub/team-b.json --port 8771
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
and set a different `database` or `prefix` in the second file. Only one hub may serve a database
|
|
111
|
+
and prefix: the second one to start on the same pair is refused with a message naming it. A hub
|
|
112
|
+
that was killed holds that pair for up to 30 s more; a clean stop gives it back at once.
|
|
113
|
+
|
|
114
|
+
The hosts that share a MongoDB this way should keep their clocks in sync: a host whose clock is
|
|
115
|
+
more than 20 s ahead can take over the pair from a live hub. An optio engine's own hub does not
|
|
116
|
+
take part in this check, so never point the command at an engine's database and prefix.
|
|
117
|
+
|
|
118
|
+
## What other servers set
|
|
119
|
+
|
|
120
|
+
On every server that should use this hub, as optio engines read them (client mode), and as
|
|
121
|
+
optio-chat's `optio-chat` command and plugin read them:
|
|
122
|
+
|
|
123
|
+
```
|
|
124
|
+
OPTIO_CHAT_HUB=wss://hub.example.com/
|
|
125
|
+
OPTIO_CHAT_SHARED_PASSWORD=<the password from the settings file>
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Upgrading
|
|
129
|
+
|
|
130
|
+
When the hub starts on a store an older hub wrote, it converts the store once, one way: an older
|
|
131
|
+
hub cannot read it afterwards. A hub upgrade comes before the clients that need its new features
|
|
132
|
+
(optio-chat's README lists them).
|
|
133
|
+
|
|
134
|
+
Design: `docs/2026-11-28-standalone-chat-hub-design.md` in the optio repository.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "optio-chat-hub"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "The optio agent chat hub: an MQTT 5 broker keeping its state in MongoDB, run inside an optio engine or on its own (optio-chat-hub)"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "Apache-2.0"
|
|
11
|
+
requires-python = ">=3.11"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Kristof Csillag", email = "kristof.csillag@deai-labs.com" },
|
|
14
|
+
]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3.11",
|
|
19
|
+
"Programming Language :: Python :: 3.12",
|
|
20
|
+
"Programming Language :: Python :: 3.13",
|
|
21
|
+
"Operating System :: POSIX :: Linux",
|
|
22
|
+
"Operating System :: MacOS",
|
|
23
|
+
"Topic :: Communications :: Chat",
|
|
24
|
+
"Framework :: AsyncIO",
|
|
25
|
+
]
|
|
26
|
+
dependencies = [
|
|
27
|
+
"optio-chat>=0.4,<0.5",
|
|
28
|
+
"motor>=3.3.0",
|
|
29
|
+
"aiohttp>=3.9",
|
|
30
|
+
"Pillow>=10",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.optional-dependencies]
|
|
34
|
+
dev = [
|
|
35
|
+
"pytest>=8.0",
|
|
36
|
+
"pytest-asyncio>=0.23",
|
|
37
|
+
"pytest-xdist>=3.6",
|
|
38
|
+
"packaging>=23",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[project.urls]
|
|
42
|
+
Homepage = "https://github.com/deai-network/optio"
|
|
43
|
+
Repository = "https://github.com/deai-network/optio"
|
|
44
|
+
Issues = "https://github.com/deai-network/optio/issues"
|
|
45
|
+
|
|
46
|
+
[project.scripts]
|
|
47
|
+
optio-chat-hub = "optio_chat_hub.cli:main"
|
|
48
|
+
|
|
49
|
+
[tool.setuptools.packages.find]
|
|
50
|
+
where = ["src"]
|
|
51
|
+
|
|
52
|
+
[tool.pytest.ini_options]
|
|
53
|
+
asyncio_mode = "auto"
|
|
54
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
55
|
+
testpaths = ["tests"]
|
|
56
|
+
markers = [
|
|
57
|
+
"serial: spawn-heavy or timing-fragile tests, run in the serial phase",
|
|
58
|
+
]
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"""The chat hub: an MQTT 5 broker keeping its state in MongoDB (install ``optio-chat-hub``).
|
|
2
|
+
|
|
3
|
+
``Hub`` wires the store (``store.py``), the broker core (``core.py``) and the transports
|
|
4
|
+
(``transport.py``, which use the codec in ``codec.py``).
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import asyncio
|
|
10
|
+
import logging
|
|
11
|
+
from typing import TYPE_CHECKING, Callable
|
|
12
|
+
|
|
13
|
+
# wire needs neither motor nor aiohttp (see below), so the default can be read here.
|
|
14
|
+
from optio_chat.wire import MAX_FILE_BYTES
|
|
15
|
+
|
|
16
|
+
log = logging.getLogger("optio_chat_hub")
|
|
17
|
+
|
|
18
|
+
# How long ``Hub.healthy`` waits for the database's answer to a ping.
|
|
19
|
+
HEALTH_TIMEOUT_S = 2.0
|
|
20
|
+
|
|
21
|
+
if TYPE_CHECKING:
|
|
22
|
+
from motor.motor_asyncio import AsyncIOMotorDatabase
|
|
23
|
+
|
|
24
|
+
from optio_chat_hub.core import BrokerCore
|
|
25
|
+
from optio_chat_hub.transport import TransportServer
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def server_connected_line(server_id: str, via_tunnel: bool) -> str:
|
|
29
|
+
"""The log line of an optio server that connected to the hub (owner, 2026-11-24)."""
|
|
30
|
+
if via_tunnel:
|
|
31
|
+
return f"Chat hub: optio server {server_id} connected through optio-api's tunnel"
|
|
32
|
+
return f"Chat hub: optio server {server_id} connected"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class Hub:
|
|
36
|
+
"""One hub over one MongoDB database and collection prefix.
|
|
37
|
+
|
|
38
|
+
Typical use (the engine chat service):
|
|
39
|
+
hub = Hub(db, prefix, password=password, on_server_connected=log_it)
|
|
40
|
+
await hub.start()
|
|
41
|
+
tcp_port = await hub.listen_tcp() # loopback, ephemeral port
|
|
42
|
+
ws_port = await hub.listen_websocket(bind, port) # only when sharing (optio-api tunnels here)
|
|
43
|
+
...
|
|
44
|
+
await hub.stop() # connections closed; state stays in MongoDB
|
|
45
|
+
|
|
46
|
+
``on_server_connected(server_id, via_tunnel)`` (synchronous; an exception is logged, never
|
|
47
|
+
raised) is called when a connection carrying an optio server's identity
|
|
48
|
+
(``wire.PROP_SERVER``) passes the password check while that server has no other live
|
|
49
|
+
connection at the hub: once per server, not per name, and again after all its connections
|
|
50
|
+
had ended. ``via_tunnel``: the connection came through the WebSocket listener (optio-api's
|
|
51
|
+
tunnel). Never for the hub's own server (``wire.server_id(db.name, prefix)``), a
|
|
52
|
+
connection without the property, or a value ``wire.server_id_ok`` refuses. None: the hub
|
|
53
|
+
logs ``server_connected_line`` on its own logger (``optio_chat_hub``, INFO).
|
|
54
|
+
|
|
55
|
+
``max_file_bytes`` (chat attachments, docs/2026-11-27-chat-attachments-design.md): the
|
|
56
|
+
per-file limit in bytes, the deployment's (default ``wire.MAX_FILE_BYTES``, 10 MiB), which
|
|
57
|
+
every successful CONNACK carries (``optio-file-limit``). None turns files off: the hub
|
|
58
|
+
answers as one from before them (no ``optio-file-limit``, an upload 0x90 ``refused``, a
|
|
59
|
+
fetch 0x8F ``refused``, a send's attachments dropped).
|
|
60
|
+
|
|
61
|
+
``retention_s``: the deployment's retention in seconds (a whole number, 1 to
|
|
62
|
+
``core.MAX_RETENTION_S``; ValueError otherwise), one for messages and attachments (owner,
|
|
63
|
+
2026-11-28). It covers the attachments now, and the messages too once the hub keeps a
|
|
64
|
+
channel's history: a file is deleted that long after the last message that carried it (each
|
|
65
|
+
message's reference ends that long after it was sent; a later send of the file renews it in
|
|
66
|
+
its channel), or earlier when every channel it was sent in has ended; an upload no message
|
|
67
|
+
refers to keeps its hold (``wire.UPLOAD_HOLD_S``). None (the default): no retention, a file
|
|
68
|
+
lives while a channel it was sent in exists. optio's engine always passes its setting
|
|
69
|
+
(``OPTIO_CHAT_RETENTION_DAYS``, default 7 days). Nothing of it is on the wire: a name whose
|
|
70
|
+
references to the file have all ended gets what no such file gets (``file-not-found``).
|
|
71
|
+
|
|
72
|
+
Health (the standalone command, ``optio-chat-hub``): ``healthy()`` says whether the hub runs
|
|
73
|
+
and its database answers; ``listen_websocket(..., health=True)`` serves it as ``GET /health``.
|
|
74
|
+
"""
|
|
75
|
+
|
|
76
|
+
def __init__(self, db: "AsyncIOMotorDatabase", prefix: str, *, password: str,
|
|
77
|
+
on_server_connected: Callable[[str, bool], None] | None = None,
|
|
78
|
+
max_file_bytes: int | None = MAX_FILE_BYTES,
|
|
79
|
+
retention_s: int | None = None) -> None:
|
|
80
|
+
self._db = db
|
|
81
|
+
self._on_server_connected = on_server_connected
|
|
82
|
+
self._max_file_bytes = max_file_bytes
|
|
83
|
+
self._retention_s = retention_s
|
|
84
|
+
self._running = False # start() has finished, stop() not begun
|
|
85
|
+
self._ping_task: asyncio.Task | None = None # the health ping on its way, if any
|
|
86
|
+
self._core = self._make_core(db, prefix, password)
|
|
87
|
+
self._transports = self._make_transports(self._core)
|
|
88
|
+
|
|
89
|
+
# The parts are imported where they are built, not at module level: importing any
|
|
90
|
+
# optio_chat_hub submodule (packets, codec) runs this file, and those need neither motor
|
|
91
|
+
# nor aiohttp, so ``import optio_chat_hub.packets`` stays light.
|
|
92
|
+
|
|
93
|
+
def _make_core(self, db: "AsyncIOMotorDatabase", prefix: str, password: str) -> "BrokerCore":
|
|
94
|
+
from optio_chat import wire
|
|
95
|
+
from optio_chat_hub.core import BrokerCore
|
|
96
|
+
from optio_chat_hub.store import HubStore
|
|
97
|
+
|
|
98
|
+
return BrokerCore(HubStore(db, prefix), password=password,
|
|
99
|
+
own_server=wire.server_id(db.name, prefix),
|
|
100
|
+
on_server_connected=self._on_server_connected,
|
|
101
|
+
max_file_bytes=self._max_file_bytes,
|
|
102
|
+
retention_s=self._retention_s)
|
|
103
|
+
|
|
104
|
+
def _make_transports(self, core: "BrokerCore") -> "TransportServer":
|
|
105
|
+
from optio_chat_hub.transport import TransportServer
|
|
106
|
+
|
|
107
|
+
return TransportServer(core)
|
|
108
|
+
|
|
109
|
+
async def start(self) -> None:
|
|
110
|
+
"""Start the core: convert a store from before shared names (``HubStore.migrate``),
|
|
111
|
+
indexes, end what a stopped hub left half done, the expiry sweep. Call before listening.
|
|
112
|
+
|
|
113
|
+
The conversion is one way: after it a hub from before can no longer read the store (the
|
|
114
|
+
keys moved from the names to their connections), so rolling back loses every key and
|
|
115
|
+
every held name, the engine's own names included."""
|
|
116
|
+
await self._core.start()
|
|
117
|
+
self._running = True
|
|
118
|
+
|
|
119
|
+
async def listen_tcp(self, host: str = "127.0.0.1", port: int = 0) -> int:
|
|
120
|
+
"""Accept MQTT over TCP; returns the bound port."""
|
|
121
|
+
return await self._transports.listen_tcp(host, port)
|
|
122
|
+
|
|
123
|
+
async def listen_websocket(self, host: str = "127.0.0.1", port: int = 0, *,
|
|
124
|
+
health: bool = False) -> int:
|
|
125
|
+
"""Accept MQTT over WebSocket (aiohttp, path "/", subprotocol "mqtt"); returns the port.
|
|
126
|
+
``health``: the listener also answers ``GET /health`` by ``healthy()`` (200
|
|
127
|
+
``{"status": "ok"}``, else 503 ``{"status": "unavailable"}``)."""
|
|
128
|
+
return await self._transports.listen_websocket(
|
|
129
|
+
host, port, health=self.healthy if health else None)
|
|
130
|
+
|
|
131
|
+
async def healthy(self) -> bool:
|
|
132
|
+
"""Whether the hub runs (``start()`` has finished, ``stop()`` has not begun) and its
|
|
133
|
+
database answers a ping within ``HEALTH_TIMEOUT_S``. One ping at a time: a call while a
|
|
134
|
+
ping is on its way waits for that one, so a database that hangs gets no pile of pings
|
|
135
|
+
from a monitor that asks every second; a ping that raised counts as no answer (its
|
|
136
|
+
exception is logged at debug)."""
|
|
137
|
+
if not self._running:
|
|
138
|
+
return False
|
|
139
|
+
if self._ping_task is None:
|
|
140
|
+
self._ping_task = asyncio.create_task(self._ping())
|
|
141
|
+
self._ping_task.add_done_callback(self._ping_ended)
|
|
142
|
+
try:
|
|
143
|
+
await asyncio.wait_for(asyncio.shield(self._ping_task), HEALTH_TIMEOUT_S)
|
|
144
|
+
except Exception: # no answer in time, or the ping raised
|
|
145
|
+
return False
|
|
146
|
+
return self._running
|
|
147
|
+
|
|
148
|
+
def _ping_ended(self, task: asyncio.Task) -> None:
|
|
149
|
+
if self._ping_task is task:
|
|
150
|
+
self._ping_task = None
|
|
151
|
+
# Its outcome is seen here, also when every check has stopped waiting (a shield drops
|
|
152
|
+
# its callback on a timeout): else asyncio logs "Task exception was never retrieved".
|
|
153
|
+
if not task.cancelled() and (exc := task.exception()) is not None:
|
|
154
|
+
log.debug("chat hub: the health ping failed: %s: %s", type(exc).__name__, exc)
|
|
155
|
+
|
|
156
|
+
async def _ping(self) -> None:
|
|
157
|
+
await self._db.command("ping")
|
|
158
|
+
|
|
159
|
+
async def stop(self) -> None:
|
|
160
|
+
"""Stop listening, stop the core (it closes its connections), then close whatever
|
|
161
|
+
connection is left and wait until the core has seen each one end. State stays in
|
|
162
|
+
MongoDB."""
|
|
163
|
+
self._running = False
|
|
164
|
+
await self._transports.stop_listening()
|
|
165
|
+
try:
|
|
166
|
+
await self._core.stop()
|
|
167
|
+
finally:
|
|
168
|
+
await self._transports.close()
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
__all__ = ["HEALTH_TIMEOUT_S", "Hub", "server_connected_line"]
|