optio-task-chat-participation 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_task_chat_participation-0.1.0/PKG-INFO +131 -0
- optio_task_chat_participation-0.1.0/README.md +103 -0
- optio_task_chat_participation-0.1.0/pyproject.toml +53 -0
- optio_task_chat_participation-0.1.0/setup.cfg +4 -0
- optio_task_chat_participation-0.1.0/src/optio_task_chat_participation/__init__.py +89 -0
- optio_task_chat_participation-0.1.0/src/optio_task_chat_participation/author.py +147 -0
- optio_task_chat_participation-0.1.0/src/optio_task_chat_participation/avatars.py +78 -0
- optio_task_chat_participation-0.1.0/src/optio_task_chat_participation/events.py +101 -0
- optio_task_chat_participation-0.1.0/src/optio_task_chat_participation/me.py +33 -0
- optio_task_chat_participation-0.1.0/src/optio_task_chat_participation/models.py +204 -0
- optio_task_chat_participation-0.1.0/src/optio_task_chat_participation/naming.py +73 -0
- optio_task_chat_participation-0.1.0/src/optio_task_chat_participation/people.py +1859 -0
- optio_task_chat_participation-0.1.0/src/optio_task_chat_participation/pictures.py +221 -0
- optio_task_chat_participation-0.1.0/src/optio_task_chat_participation/retention.py +20 -0
- optio_task_chat_participation-0.1.0/src/optio_task_chat_participation/server.py +272 -0
- optio_task_chat_participation-0.1.0/src/optio_task_chat_participation/store.py +580 -0
- optio_task_chat_participation-0.1.0/src/optio_task_chat_participation/task.py +205 -0
- optio_task_chat_participation-0.1.0/src/optio_task_chat_participation.egg-info/PKG-INFO +131 -0
- optio_task_chat_participation-0.1.0/src/optio_task_chat_participation.egg-info/SOURCES.txt +38 -0
- optio_task_chat_participation-0.1.0/src/optio_task_chat_participation.egg-info/dependency_links.txt +1 -0
- optio_task_chat_participation-0.1.0/src/optio_task_chat_participation.egg-info/requires.txt +8 -0
- optio_task_chat_participation-0.1.0/src/optio_task_chat_participation.egg-info/top_level.txt +1 -0
- optio_task_chat_participation-0.1.0/tests/test_author.py +55 -0
- optio_task_chat_participation-0.1.0/tests/test_avatars.py +71 -0
- optio_task_chat_participation-0.1.0/tests/test_events.py +69 -0
- optio_task_chat_participation-0.1.0/tests/test_integration_real_hub.py +190 -0
- optio_task_chat_participation-0.1.0/tests/test_integration_shared_names.py +381 -0
- optio_task_chat_participation-0.1.0/tests/test_me.py +56 -0
- optio_task_chat_participation-0.1.0/tests/test_naming.py +70 -0
- optio_task_chat_participation-0.1.0/tests/test_people.py +836 -0
- optio_task_chat_participation-0.1.0/tests/test_people_attach_on_open.py +284 -0
- optio_task_chat_participation-0.1.0/tests/test_people_avatar_push.py +338 -0
- optio_task_chat_participation-0.1.0/tests/test_people_current_avatar.py +324 -0
- optio_task_chat_participation-0.1.0/tests/test_people_other_apps.py +294 -0
- optio_task_chat_participation-0.1.0/tests/test_people_read_marks.py +278 -0
- optio_task_chat_participation-0.1.0/tests/test_people_shared_names.py +356 -0
- optio_task_chat_participation-0.1.0/tests/test_retention.py +33 -0
- optio_task_chat_participation-0.1.0/tests/test_server.py +407 -0
- optio_task_chat_participation-0.1.0/tests/test_store.py +553 -0
- optio_task_chat_participation-0.1.0/tests/test_task.py +407 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: optio-task-chat-participation
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: An optio task through which an app's users take part in optio's agent chat as themselves
|
|
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-core<0.12,>=0.11
|
|
22
|
+
Requires-Dist: optio-chat[hub]<0.4,>=0.3
|
|
23
|
+
Requires-Dist: aiohttp>=3.9
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
26
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == "dev"
|
|
27
|
+
Requires-Dist: pytest-xdist>=3.6; extra == "dev"
|
|
28
|
+
|
|
29
|
+
# optio-task-chat-participation
|
|
30
|
+
|
|
31
|
+
An optio task through which an app's users take part in optio's agent chat as themselves: they
|
|
32
|
+
read a channel's conversation and write into it under their own name, beside the agents, from any
|
|
33
|
+
optio app, without running an agent.
|
|
34
|
+
|
|
35
|
+
- Each person has one chat name across the apps that embed the task. The first app makes it at
|
|
36
|
+
the person's first channel join, from their display name (letters of any alphabet; `Ági
|
|
37
|
+
Kovács` stays `Ági Kovács`; a taken name gets `-2`, `-3`, ...). Any other app attaches the
|
|
38
|
+
person to that name by their identity claims (below) as soon as they open the widget, so
|
|
39
|
+
their channels are listed at once. A join, a leave or a read in one app shows in every app:
|
|
40
|
+
memberships and read marks belong to the name.
|
|
41
|
+
- Their messages are kept in the app's own MongoDB (history survives leaving and a rejoin shows
|
|
42
|
+
it again); with `retention_days` older ones are deleted daily.
|
|
43
|
+
- Agents read who a person is, e.g. `Chat #design from Ági Kovács (a human admin of Excavator)
|
|
44
|
+
at 10:00:00 UTC: hi`.
|
|
45
|
+
- The widget is the npm package `optio-chat-participation-ui`: a channel list and the channel,
|
|
46
|
+
mobile first, in English, Hungarian and German.
|
|
47
|
+
|
|
48
|
+
## Use
|
|
49
|
+
|
|
50
|
+
Add the task to your engine's task definitions:
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from optio_task_chat_participation import chat_participation_task
|
|
54
|
+
|
|
55
|
+
async def get_task_definitions(services, metadata_filter=None):
|
|
56
|
+
return [
|
|
57
|
+
chat_participation_task(
|
|
58
|
+
process_id="analyst-chat", # default "chat-participation"
|
|
59
|
+
name="Analyst Chat", # default "Chat"
|
|
60
|
+
retention_days=None, # None: keep the history forever
|
|
61
|
+
auto_launch="always", # default: runs from every engine start
|
|
62
|
+
),
|
|
63
|
+
# ...
|
|
64
|
+
]
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
The app and deployment names in people's profiles come from the engine: give `optio_core.init`
|
|
68
|
+
your app's name (`app_name="Excavator"`) and set `OPTIO_DEPLOYMENT_NAME` in the engine's
|
|
69
|
+
environment to name the deployment (e.g. `daeo control panel`). `app=` and `deployment=` on
|
|
70
|
+
`chat_participation_task` are optional overrides of those two. With no app name anywhere,
|
|
71
|
+
people's profiles have none and agents read e.g. `a human user`.
|
|
72
|
+
|
|
73
|
+
The task runs from every engine start (`auto_launch="always"`, the default; the chat is no use
|
|
74
|
+
idle): as soon as the engine is ready, it is resumed when it has saved state (after a restart, or
|
|
75
|
+
a suspend) and launched fresh otherwise (its first start, or after it failed or was cancelled
|
|
76
|
+
without saved state), so your page needs no Launch, Suspend or Resume control. A running Chat is
|
|
77
|
+
left alone. `auto_launch=True` launches it only at a start while it has never run (it then comes
|
|
78
|
+
back after a restart through the engine's auto-resume delay, 300 s by default);
|
|
79
|
+
`auto_launch=False` leaves every launch to a person. While it is suspended, messages wait at the
|
|
80
|
+
chat hub and arrive when it runs again.
|
|
81
|
+
|
|
82
|
+
In the frontend, import the widget once (it registers itself with optio-ui) and show the task
|
|
83
|
+
with `ProcessDetailView`:
|
|
84
|
+
|
|
85
|
+
```tsx
|
|
86
|
+
import 'optio-chat-participation-ui';
|
|
87
|
+
|
|
88
|
+
<ProcessDetailView processId="analyst-chat" showLogs={false} />
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
The app's server must say who the person is: optio-api's `inputAuthor` hook (Fastify adapter).
|
|
92
|
+
Without it every widget request is refused (401). Give it the user's id and display name, and
|
|
93
|
+
optionally their role, their picture and identity claims:
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
inputAuthor: (req) => ({
|
|
97
|
+
id: req.user.id,
|
|
98
|
+
label: req.user.displayName,
|
|
99
|
+
role: 'admin',
|
|
100
|
+
claims: { email: req.user.email.trim().toLowerCase() },
|
|
101
|
+
}),
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Identity claims make one person one chat name across apps: when a person opens the widget, or
|
|
105
|
+
joins a first channel, in an app where they have no name yet, a claim (key and hash) that one
|
|
106
|
+
person's name already holds attaches them to that name instead of making a new one. Without
|
|
107
|
+
claims a person gets a name of their own in each app. optio-api hashes the claims before they
|
|
108
|
+
leave its process. Use the recommended keys, so that independent apps match: `email` (trimmed,
|
|
109
|
+
lower case) and `oidc` (`<issuer> <subject>` of your identity provider). Names a person got
|
|
110
|
+
separately (before shared names, or from an older hub) stay separate and still count as "me"
|
|
111
|
+
by their claims.
|
|
112
|
+
|
|
113
|
+
**Trust.** Any deployment that holds the hub's password can attach to a person's name with a
|
|
114
|
+
claim hash of theirs (anyone who knows the value, e.g. the email address, can compute it),
|
|
115
|
+
silently, and then read everything that comes for the name and write as the person. On a
|
|
116
|
+
shared hub every connected deployment is trusted that far with every person's name there.
|
|
117
|
+
|
|
118
|
+
## Settings
|
|
119
|
+
|
|
120
|
+
The task uses the hub the engine uses: with no settings the engine's own local hub (its port and
|
|
121
|
+
password are chosen at the first start and kept), otherwise `OPTIO_CHAT_HUB` and
|
|
122
|
+
`OPTIO_CHAT_SHARED_PASSWORD` as optio-core reads them. `OPTIO_DEPLOYMENT_NAME` (optional,
|
|
123
|
+
trimmed; blank counts as unset) names the deployment in people's profiles.
|
|
124
|
+
|
|
125
|
+
**Upgrading: upgrade the shared hub first.** People's names may use any alphabet and carry
|
|
126
|
+
profiles; a hub running an older optio-chat refuses both. A shared hub must run the new
|
|
127
|
+
optio-chat before an app embeds this task against it. Upgrade the other clients of that hub
|
|
128
|
+
too (engines, CLIs, plugins): an older optio-chat client drops every message from a person
|
|
129
|
+
whose chat name has letters outside ASCII.
|
|
130
|
+
|
|
131
|
+
Design: `docs/2026-11-25-chat-participation-design.md` in the optio repository.
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# optio-task-chat-participation
|
|
2
|
+
|
|
3
|
+
An optio task through which an app's users take part in optio's agent chat as themselves: they
|
|
4
|
+
read a channel's conversation and write into it under their own name, beside the agents, from any
|
|
5
|
+
optio app, without running an agent.
|
|
6
|
+
|
|
7
|
+
- Each person has one chat name across the apps that embed the task. The first app makes it at
|
|
8
|
+
the person's first channel join, from their display name (letters of any alphabet; `Ági
|
|
9
|
+
Kovács` stays `Ági Kovács`; a taken name gets `-2`, `-3`, ...). Any other app attaches the
|
|
10
|
+
person to that name by their identity claims (below) as soon as they open the widget, so
|
|
11
|
+
their channels are listed at once. A join, a leave or a read in one app shows in every app:
|
|
12
|
+
memberships and read marks belong to the name.
|
|
13
|
+
- Their messages are kept in the app's own MongoDB (history survives leaving and a rejoin shows
|
|
14
|
+
it again); with `retention_days` older ones are deleted daily.
|
|
15
|
+
- Agents read who a person is, e.g. `Chat #design from Ági Kovács (a human admin of Excavator)
|
|
16
|
+
at 10:00:00 UTC: hi`.
|
|
17
|
+
- The widget is the npm package `optio-chat-participation-ui`: a channel list and the channel,
|
|
18
|
+
mobile first, in English, Hungarian and German.
|
|
19
|
+
|
|
20
|
+
## Use
|
|
21
|
+
|
|
22
|
+
Add the task to your engine's task definitions:
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from optio_task_chat_participation import chat_participation_task
|
|
26
|
+
|
|
27
|
+
async def get_task_definitions(services, metadata_filter=None):
|
|
28
|
+
return [
|
|
29
|
+
chat_participation_task(
|
|
30
|
+
process_id="analyst-chat", # default "chat-participation"
|
|
31
|
+
name="Analyst Chat", # default "Chat"
|
|
32
|
+
retention_days=None, # None: keep the history forever
|
|
33
|
+
auto_launch="always", # default: runs from every engine start
|
|
34
|
+
),
|
|
35
|
+
# ...
|
|
36
|
+
]
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
The app and deployment names in people's profiles come from the engine: give `optio_core.init`
|
|
40
|
+
your app's name (`app_name="Excavator"`) and set `OPTIO_DEPLOYMENT_NAME` in the engine's
|
|
41
|
+
environment to name the deployment (e.g. `daeo control panel`). `app=` and `deployment=` on
|
|
42
|
+
`chat_participation_task` are optional overrides of those two. With no app name anywhere,
|
|
43
|
+
people's profiles have none and agents read e.g. `a human user`.
|
|
44
|
+
|
|
45
|
+
The task runs from every engine start (`auto_launch="always"`, the default; the chat is no use
|
|
46
|
+
idle): as soon as the engine is ready, it is resumed when it has saved state (after a restart, or
|
|
47
|
+
a suspend) and launched fresh otherwise (its first start, or after it failed or was cancelled
|
|
48
|
+
without saved state), so your page needs no Launch, Suspend or Resume control. A running Chat is
|
|
49
|
+
left alone. `auto_launch=True` launches it only at a start while it has never run (it then comes
|
|
50
|
+
back after a restart through the engine's auto-resume delay, 300 s by default);
|
|
51
|
+
`auto_launch=False` leaves every launch to a person. While it is suspended, messages wait at the
|
|
52
|
+
chat hub and arrive when it runs again.
|
|
53
|
+
|
|
54
|
+
In the frontend, import the widget once (it registers itself with optio-ui) and show the task
|
|
55
|
+
with `ProcessDetailView`:
|
|
56
|
+
|
|
57
|
+
```tsx
|
|
58
|
+
import 'optio-chat-participation-ui';
|
|
59
|
+
|
|
60
|
+
<ProcessDetailView processId="analyst-chat" showLogs={false} />
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
The app's server must say who the person is: optio-api's `inputAuthor` hook (Fastify adapter).
|
|
64
|
+
Without it every widget request is refused (401). Give it the user's id and display name, and
|
|
65
|
+
optionally their role, their picture and identity claims:
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
inputAuthor: (req) => ({
|
|
69
|
+
id: req.user.id,
|
|
70
|
+
label: req.user.displayName,
|
|
71
|
+
role: 'admin',
|
|
72
|
+
claims: { email: req.user.email.trim().toLowerCase() },
|
|
73
|
+
}),
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Identity claims make one person one chat name across apps: when a person opens the widget, or
|
|
77
|
+
joins a first channel, in an app where they have no name yet, a claim (key and hash) that one
|
|
78
|
+
person's name already holds attaches them to that name instead of making a new one. Without
|
|
79
|
+
claims a person gets a name of their own in each app. optio-api hashes the claims before they
|
|
80
|
+
leave its process. Use the recommended keys, so that independent apps match: `email` (trimmed,
|
|
81
|
+
lower case) and `oidc` (`<issuer> <subject>` of your identity provider). Names a person got
|
|
82
|
+
separately (before shared names, or from an older hub) stay separate and still count as "me"
|
|
83
|
+
by their claims.
|
|
84
|
+
|
|
85
|
+
**Trust.** Any deployment that holds the hub's password can attach to a person's name with a
|
|
86
|
+
claim hash of theirs (anyone who knows the value, e.g. the email address, can compute it),
|
|
87
|
+
silently, and then read everything that comes for the name and write as the person. On a
|
|
88
|
+
shared hub every connected deployment is trusted that far with every person's name there.
|
|
89
|
+
|
|
90
|
+
## Settings
|
|
91
|
+
|
|
92
|
+
The task uses the hub the engine uses: with no settings the engine's own local hub (its port and
|
|
93
|
+
password are chosen at the first start and kept), otherwise `OPTIO_CHAT_HUB` and
|
|
94
|
+
`OPTIO_CHAT_SHARED_PASSWORD` as optio-core reads them. `OPTIO_DEPLOYMENT_NAME` (optional,
|
|
95
|
+
trimmed; blank counts as unset) names the deployment in people's profiles.
|
|
96
|
+
|
|
97
|
+
**Upgrading: upgrade the shared hub first.** People's names may use any alphabet and carry
|
|
98
|
+
profiles; a hub running an older optio-chat refuses both. A shared hub must run the new
|
|
99
|
+
optio-chat before an app embeds this task against it. Upgrade the other clients of that hub
|
|
100
|
+
too (engines, CLIs, plugins): an older optio-chat client drops every message from a person
|
|
101
|
+
whose chat name has letters outside ASCII.
|
|
102
|
+
|
|
103
|
+
Design: `docs/2026-11-25-chat-participation-design.md` in the optio repository.
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "optio-task-chat-participation"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "An optio task through which an app's users take part in optio's agent chat as themselves"
|
|
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-core>=0.11,<0.12",
|
|
28
|
+
"optio-chat[hub]>=0.3,<0.4",
|
|
29
|
+
"aiohttp>=3.9",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
[project.optional-dependencies]
|
|
33
|
+
dev = [
|
|
34
|
+
"pytest>=8.0",
|
|
35
|
+
"pytest-asyncio>=0.23",
|
|
36
|
+
"pytest-xdist>=3.6",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
[project.urls]
|
|
40
|
+
Homepage = "https://github.com/deai-network/optio"
|
|
41
|
+
Repository = "https://github.com/deai-network/optio"
|
|
42
|
+
Issues = "https://github.com/deai-network/optio/issues"
|
|
43
|
+
|
|
44
|
+
[tool.setuptools.packages.find]
|
|
45
|
+
where = ["src"]
|
|
46
|
+
|
|
47
|
+
[tool.pytest.ini_options]
|
|
48
|
+
asyncio_mode = "auto"
|
|
49
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
50
|
+
testpaths = ["tests"]
|
|
51
|
+
markers = [
|
|
52
|
+
"serial: spawn-heavy or timing-fragile tests, run in the serial phase",
|
|
53
|
+
]
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"""optio-task-chat-participation: an optio task through which an app's users take part in optio's
|
|
2
|
+
agent chat as themselves.
|
|
3
|
+
|
|
4
|
+
Spec: docs/2026-11-25-chat-participation-design.md, section 7 (the task) and section 8 (its
|
|
5
|
+
widget, the npm package optio-chat-participation-ui). Plan: docs/2026-11-25-chat-participation-
|
|
6
|
+
plan.md (Phase 0 wrote the interfaces; P7 builds the task).
|
|
7
|
+
|
|
8
|
+
An app embeds it from its task definitions, like any task::
|
|
9
|
+
|
|
10
|
+
from optio_task_chat_participation import chat_participation_task
|
|
11
|
+
|
|
12
|
+
chat_participation_task(process_id="analyst-chat", name="Analyst Chat")
|
|
13
|
+
|
|
14
|
+
The app and deployment names in people's profiles are the engine's (optio-core's
|
|
15
|
+
``init(app_name=...)`` and ``OPTIO_DEPLOYMENT_NAME``) unless ``app=`` / ``deployment=`` override
|
|
16
|
+
them.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
from typing import Any, Literal
|
|
22
|
+
|
|
23
|
+
from optio_chat import ChatError, wire
|
|
24
|
+
from optio_chat.models import Profile
|
|
25
|
+
from optio_core.models import TaskInstance
|
|
26
|
+
|
|
27
|
+
from optio_task_chat_participation.task import run_participation
|
|
28
|
+
|
|
29
|
+
# The widget's name (optio-ui's registerWidget), registered by optio-chat-participation-ui.
|
|
30
|
+
WIDGET_NAME = "optio-chat-participation"
|
|
31
|
+
DEFAULT_PROCESS_ID = "chat-participation"
|
|
32
|
+
DEFAULT_NAME = "Chat"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def chat_participation_task(*, process_id: str = DEFAULT_PROCESS_ID, name: str = DEFAULT_NAME,
|
|
36
|
+
app: str | None = None, deployment: str | None = None,
|
|
37
|
+
retention_days: int | None = None, description: str | None = None,
|
|
38
|
+
metadata: dict[str, Any] | None = None,
|
|
39
|
+
auto_launch: bool | Literal["always"] = "always") -> TaskInstance:
|
|
40
|
+
"""The chat participation task (spec section 7.1).
|
|
41
|
+
|
|
42
|
+
``app`` and ``deployment`` go into the profile of every person's chat name. Both are
|
|
43
|
+
optional overrides of the engine's app identity: at run time the task uses ``app`` if given,
|
|
44
|
+
else ``ctx.app_name`` (optio-core's ``init(app_name=...)``), and ``deployment`` if given, else
|
|
45
|
+
``ctx.deployment_name`` (``OPTIO_DEPLOYMENT_NAME``); owner, 2026-11-26: "app name is an init
|
|
46
|
+
parameter, deployment name is env config". Neither yields an app: people's profiles have
|
|
47
|
+
none (descriptions then read e.g. "a human user"). Given values follow the profile string
|
|
48
|
+
rules (``wire.check_profile``: 1-200 code points, no control or bidirectional formatting
|
|
49
|
+
characters). ``retention_days``: None keeps the history forever, else a positive int (a
|
|
50
|
+
daily sweep deletes older messages). ``description`` and ``metadata`` are the
|
|
51
|
+
TaskInstance's. Several instances may run in one app, each keyed by its process id.
|
|
52
|
+
|
|
53
|
+
The TaskInstance: ``ui_widget=WIDGET_NAME``, ``supports_resume=True``, ``auto_resume=True``,
|
|
54
|
+
``cancellable=True``, and ``auto_launch`` (optio-core's ``TaskInstance.auto_launch``;
|
|
55
|
+
default ``"always"``: the Chat runs from every engine start, resumed when it has saved
|
|
56
|
+
state, else launched fresh, since the chat is useless idle; owner, 2026-11-26: "launch it
|
|
57
|
+
automatically at engine start when it has never run, so admins never see an idle Chat";
|
|
58
|
+
owner, 2026-11-27: it must run from every engine start, the "every start" mode he chose).
|
|
59
|
+
``True`` launches it only while it has never run; ``False`` leaves every launch to a person.
|
|
60
|
+
Raises ValueError for a value that breaks these rules.
|
|
61
|
+
"""
|
|
62
|
+
try:
|
|
63
|
+
wire.check_profile(Profile(app=app, deployment=deployment))
|
|
64
|
+
except ChatError as err:
|
|
65
|
+
raise ValueError(f"app and deployment must follow the chat profile rules: {err}") from None
|
|
66
|
+
if retention_days is not None and (isinstance(retention_days, bool)
|
|
67
|
+
or not isinstance(retention_days, int)
|
|
68
|
+
or retention_days < 1):
|
|
69
|
+
raise ValueError(f"retention_days must be None or a positive int, not {retention_days!r}")
|
|
70
|
+
|
|
71
|
+
async def execute(ctx: Any) -> None:
|
|
72
|
+
await run_participation(ctx, app=app, deployment=deployment,
|
|
73
|
+
retention_days=retention_days)
|
|
74
|
+
|
|
75
|
+
return TaskInstance(
|
|
76
|
+
execute=execute,
|
|
77
|
+
process_id=process_id,
|
|
78
|
+
name=name,
|
|
79
|
+
description=description,
|
|
80
|
+
metadata=dict(metadata or {}),
|
|
81
|
+
ui_widget=WIDGET_NAME,
|
|
82
|
+
supports_resume=True,
|
|
83
|
+
auto_resume=True,
|
|
84
|
+
auto_launch=auto_launch,
|
|
85
|
+
cancellable=True,
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
__all__ = ["DEFAULT_NAME", "DEFAULT_PROCESS_ID", "WIDGET_NAME", "chat_participation_task"]
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"""Who a widget request comes from: optio-api's author stamp (spec sections 5.1-5.4).
|
|
2
|
+
|
|
3
|
+
optio-api's widget proxy calls the host's ``inputAuthor`` hook for every request it forwards and
|
|
4
|
+
sets ``X-Optio-Author`` (base64url, padding tolerated, of the UTF-8 JSON ``{"id", "label"?,
|
|
5
|
+
"role"?, "avatarId"?, "claims"?}``); it removes any such header the browser sent. The person is
|
|
6
|
+
the stamp's ``id``.
|
|
7
|
+
|
|
8
|
+
This parser follows optio-api's ``parseInputAuthor`` and optio-agents' ``parse_author_header``
|
|
9
|
+
rule for rule (the shared vectors in ``packages/optio-agents/tests/fixtures/
|
|
10
|
+
author-header-vectors.json``); the task does not depend on optio-agents, so it has its own copy.
|
|
11
|
+
Unlike optio-agents' parser it keeps the claims: the task sets them in the person's profile and
|
|
12
|
+
matches "mine" by them (section 7.6).
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import base64
|
|
18
|
+
import binascii
|
|
19
|
+
import json
|
|
20
|
+
import re
|
|
21
|
+
from dataclasses import dataclass, field
|
|
22
|
+
from types import MappingProxyType
|
|
23
|
+
from typing import Mapping
|
|
24
|
+
|
|
25
|
+
from optio_chat import wire
|
|
26
|
+
|
|
27
|
+
# In Unicode code points (a str's len), as optio-api's and optio-agents' limits.
|
|
28
|
+
MAX_ID_CHARS = 128
|
|
29
|
+
MAX_LABEL_CHARS = 200
|
|
30
|
+
|
|
31
|
+
# Unicode's control characters (Cc): C0, DEL, C1.
|
|
32
|
+
_CONTROL = re.compile(r"[\x00-\x1f\x7f-\x9f]")
|
|
33
|
+
# Lone surrogates: only JSON's \u escapes can carry one.
|
|
34
|
+
_SURROGATE = re.compile(r"[\ud800-\udfff]")
|
|
35
|
+
# base64url, then at most the padding a value can need (tolerated, never required).
|
|
36
|
+
_HEADER_VALUE = re.compile(r"([A-Za-z0-9_-]+)={0,2}")
|
|
37
|
+
_KEYS = frozenset({"id", "label", "role", "avatarId", "claims"})
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass(frozen=True)
|
|
41
|
+
class Author:
|
|
42
|
+
"""A stamped person: ``id`` the host app's user id (opaque); ``label`` their display name;
|
|
43
|
+
``role`` their role in the host app; ``avatar_id`` their picture in optio's avatar store (32
|
|
44
|
+
lowercase hex); ``claims`` their identity claims, hashed (key -> ``wire.claim_hash``), kept
|
|
45
|
+
read-only."""
|
|
46
|
+
|
|
47
|
+
id: str
|
|
48
|
+
label: str | None = None
|
|
49
|
+
role: str | None = None
|
|
50
|
+
avatar_id: str | None = None
|
|
51
|
+
claims: Mapping[str, str] = field(default_factory=dict)
|
|
52
|
+
|
|
53
|
+
def __post_init__(self) -> None:
|
|
54
|
+
object.__setattr__(self, "claims", MappingProxyType(dict(self.claims)))
|
|
55
|
+
|
|
56
|
+
def __hash__(self) -> int:
|
|
57
|
+
return hash((self.id, self.label, self.role, self.avatar_id,
|
|
58
|
+
frozenset(self.claims.items())))
|
|
59
|
+
|
|
60
|
+
def __reduce__(self) -> tuple:
|
|
61
|
+
return (Author, (self.id, self.label, self.role, self.avatar_id, dict(self.claims)))
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _text_ok(value: str) -> bool:
|
|
65
|
+
return not _CONTROL.search(value) and not _SURROGATE.search(value)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _optional_text(obj: dict, key: str) -> str | None:
|
|
69
|
+
"""A label or a role: absent (None), else a str cut to ``MAX_LABEL_CHARS`` code points and
|
|
70
|
+
then checked; ValueError otherwise (null included)."""
|
|
71
|
+
if key not in obj:
|
|
72
|
+
return None
|
|
73
|
+
value = obj[key]
|
|
74
|
+
if not isinstance(value, str):
|
|
75
|
+
raise ValueError(f"{key} is not a string")
|
|
76
|
+
value = value[:MAX_LABEL_CHARS]
|
|
77
|
+
if not _text_ok(value):
|
|
78
|
+
raise ValueError(f"{key} has a control character or a lone surrogate")
|
|
79
|
+
return value
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _claims(value: object) -> dict[str, str]:
|
|
83
|
+
if not isinstance(value, dict):
|
|
84
|
+
raise ValueError("claims is not an object")
|
|
85
|
+
if len(value) > wire.MAX_CLAIMS:
|
|
86
|
+
raise ValueError(f"more than {wire.MAX_CLAIMS} claims")
|
|
87
|
+
for key, hashed in value.items():
|
|
88
|
+
if not wire.CLAIM_KEY_PATTERN.fullmatch(key):
|
|
89
|
+
raise ValueError("a claim key breaks the key rule")
|
|
90
|
+
if not isinstance(hashed, str) or not wire.CLAIM_HASH_PATTERN.fullmatch(hashed):
|
|
91
|
+
raise ValueError("a claim value is not a claim hash")
|
|
92
|
+
return dict(value)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _decode(value: str) -> object:
|
|
96
|
+
match = _HEADER_VALUE.fullmatch(value)
|
|
97
|
+
if match is None:
|
|
98
|
+
raise ValueError("not base64url")
|
|
99
|
+
core = match.group(1)
|
|
100
|
+
if len(core) % 4 == 1:
|
|
101
|
+
raise ValueError("not base64url")
|
|
102
|
+
try:
|
|
103
|
+
raw = base64.urlsafe_b64decode(core + "=" * (-len(core) % 4))
|
|
104
|
+
except (binascii.Error, ValueError):
|
|
105
|
+
raise ValueError("not base64url") from None
|
|
106
|
+
try:
|
|
107
|
+
return json.loads(raw.decode("utf-8"))
|
|
108
|
+
except UnicodeDecodeError:
|
|
109
|
+
raise ValueError("not UTF-8") from None
|
|
110
|
+
except (json.JSONDecodeError, RecursionError):
|
|
111
|
+
raise ValueError("not JSON") from None
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _author(value: str) -> Author:
|
|
115
|
+
obj = _decode(value)
|
|
116
|
+
if not isinstance(obj, dict):
|
|
117
|
+
raise ValueError("not a JSON object")
|
|
118
|
+
if "id" not in obj or not obj.keys() <= _KEYS:
|
|
119
|
+
raise ValueError("its keys are not id and the optional label, role, avatarId and claims")
|
|
120
|
+
author_id = obj["id"]
|
|
121
|
+
if (not isinstance(author_id, str) or not 1 <= len(author_id) <= MAX_ID_CHARS
|
|
122
|
+
or not _text_ok(author_id)):
|
|
123
|
+
raise ValueError(f"the id is not a str of 1-{MAX_ID_CHARS} characters without control "
|
|
124
|
+
"characters or lone surrogates")
|
|
125
|
+
label = _optional_text(obj, "label")
|
|
126
|
+
role = _optional_text(obj, "role")
|
|
127
|
+
avatar_id = obj.get("avatarId")
|
|
128
|
+
if "avatarId" in obj and (not isinstance(avatar_id, str)
|
|
129
|
+
or not wire.AVATAR_ID_PATTERN.fullmatch(avatar_id)):
|
|
130
|
+
raise ValueError("avatarId is not 32 lowercase hex")
|
|
131
|
+
claims = _claims(obj["claims"]) if "claims" in obj else {}
|
|
132
|
+
return Author(id=author_id, label=label, role=role, avatar_id=avatar_id, claims=claims)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def parse_author_header(value: str | None) -> Author | None:
|
|
136
|
+
"""The person an ``X-Optio-Author`` value names, or None for None and for anything invalid
|
|
137
|
+
(the same headers optio-api's ``parseInputAuthor`` refuses). A label or role over 200 code
|
|
138
|
+
points is cut; empty claims count as none."""
|
|
139
|
+
if not isinstance(value, str):
|
|
140
|
+
return None
|
|
141
|
+
try:
|
|
142
|
+
return _author(value)
|
|
143
|
+
except ValueError:
|
|
144
|
+
return None
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
__all__ = ["Author", "MAX_ID_CHARS", "MAX_LABEL_CHARS", "parse_author_header"]
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""What the task knows of each chat name's current avatar (spec section 15.5; owner, 2026-11-27:
|
|
2
|
+
"always use current picture from hub", and "b": every message of a name shows the name's
|
|
3
|
+
current picture, old ones included).
|
|
4
|
+
|
|
5
|
+
One ``CurrentAvatars`` per run of the task, shared by its people (a name's picture is the same
|
|
6
|
+
for everyone). Each name (by ``wire.fold``) has the avatar id last learned for it (None: no
|
|
7
|
+
picture), where it came from, and the time it is current as of, in whole milliseconds (the hub's
|
|
8
|
+
stamps have no more). Two sources, ranked: the hub's word (``from_hub``: a push of avatar
|
|
9
|
+
changes, a lookup's answer, an upload's answer), timed by the engine's clock; and a message's
|
|
10
|
+
or an announcement's stamp, timed by its ``sent_at`` (the hub's clock). The hub's word always
|
|
11
|
+
wins over a stamp, so the two clocks are never compared: a stamp counts only while the hub has
|
|
12
|
+
said nothing of the name in this run (a name it no longer holds, a lookup that failed). Within
|
|
13
|
+
one source a value as of an earlier time changes nothing (an older stamp, from a page of history
|
|
14
|
+
or a message that waited at the hub; a lookup asked before a push came); of two at the same time
|
|
15
|
+
the one learned last wins.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
from dataclasses import dataclass
|
|
21
|
+
from datetime import datetime, timezone
|
|
22
|
+
|
|
23
|
+
from optio_chat import wire
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(frozen=True)
|
|
27
|
+
class KnownAvatar:
|
|
28
|
+
"""A name's current avatar as the task knows it: ``name`` as last spelled, ``avatar_id``
|
|
29
|
+
(None: no picture), ``at`` (aware UTC, whole milliseconds; the engine's clock when
|
|
30
|
+
``from_hub``, else the stamp's ``sent_at``) and ``from_hub`` (the hub's word, not a
|
|
31
|
+
stamp)."""
|
|
32
|
+
|
|
33
|
+
name: str
|
|
34
|
+
avatar_id: str | None
|
|
35
|
+
at: datetime
|
|
36
|
+
from_hub: bool = False
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _in_ms(value: datetime) -> datetime:
|
|
40
|
+
"""``value`` as an aware UTC time cut to whole milliseconds (a naive one is UTC, as MongoDB
|
|
41
|
+
gives stored times back)."""
|
|
42
|
+
if value.tzinfo is None:
|
|
43
|
+
value = value.replace(tzinfo=timezone.utc)
|
|
44
|
+
value = value.astimezone(timezone.utc)
|
|
45
|
+
return value.replace(microsecond=value.microsecond // 1000 * 1000)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class CurrentAvatars:
|
|
49
|
+
"""Every chat name's current avatar as this run of the task knows it."""
|
|
50
|
+
|
|
51
|
+
def __init__(self) -> None:
|
|
52
|
+
self._known: dict[str, KnownAvatar] = {}
|
|
53
|
+
|
|
54
|
+
def get(self, name: str) -> KnownAvatar | None:
|
|
55
|
+
"""What is known of the name (matched by ``wire.fold``), or None: nothing yet."""
|
|
56
|
+
return self._known.get(wire.fold(name))
|
|
57
|
+
|
|
58
|
+
def learn(self, name: str, avatar_id: str | None, at: datetime, *,
|
|
59
|
+
from_hub: bool = False) -> bool:
|
|
60
|
+
"""The name's avatar is ``avatar_id`` as of ``at``, by the hub's word (``from_hub``) or
|
|
61
|
+
by a stamp. Kept unless the hub's word is known and this is a stamp, or something of
|
|
62
|
+
the same source was learned as of a later time; True when the known avatar changed (a
|
|
63
|
+
first value is a change)."""
|
|
64
|
+
fold = wire.fold(name)
|
|
65
|
+
at = _in_ms(at)
|
|
66
|
+
known = self._known.get(fold)
|
|
67
|
+
if known is not None and (known.from_hub, known.at) > (from_hub, at):
|
|
68
|
+
return False
|
|
69
|
+
self._known[fold] = KnownAvatar(name=name, avatar_id=avatar_id or None, at=at,
|
|
70
|
+
from_hub=from_hub)
|
|
71
|
+
return known is None or known.avatar_id != (avatar_id or None)
|
|
72
|
+
|
|
73
|
+
def names_with(self, avatar_id: str) -> list[KnownAvatar]:
|
|
74
|
+
"""Every name whose known avatar is ``avatar_id`` now."""
|
|
75
|
+
return [known for known in self._known.values() if known.avatar_id == avatar_id]
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
__all__ = ["CurrentAvatars", "KnownAvatar"]
|