owncast-plugin-sdk 0.8.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.
@@ -0,0 +1,176 @@
1
+ Metadata-Version: 2.4
2
+ Name: owncast-plugin-sdk
3
+ Version: 0.8.0
4
+ Summary: SDK and CLI for authoring Owncast plugins in Python
5
+ Author: Owncast
6
+ License: MIT
7
+ Project-URL: Homepage, https://owncast.online
8
+ Project-URL: Repository, https://github.com/owncast/plugin-sdk
9
+ Keywords: owncast,plugin,wasm,extism
10
+ Requires-Python: >=3.8
11
+ Description-Content-Type: text/markdown
12
+
13
+ # owncast-plugin-sdk (Python)
14
+
15
+ SDK for authoring [Owncast](https://owncast.online) plugins in **Python**. Plugins ship their Python source and run sandboxed inside the Owncast server on an embedded Python engine. They use the same runtime, wire protocol, and `.ocpkg` format as the [JavaScript SDK](../js), so a Python plugin is a first-class peer of a JS one.
16
+
17
+ You write ordinary Python with decorators. There is no compile step and no PDK to install: the Owncast host embeds one Python engine and runs every Python plugin on it, so your plugin ships as plain source.
18
+
19
+ > ### Plugins are source, not a compiled binary
20
+ >
21
+ > Because the host supplies the Python engine, a plugin package contains only your `plugin.py`, its manifest, and any assets, with no bundled interpreter. That keeps packages small and means an author never installs or runs a wasm toolchain (`extism-py`, binaryen) at all. Those are a maintainer-only dependency of building the engine itself.
22
+
23
+ ## Quick start
24
+
25
+ Install the SDK to get the `owncast-plugin-py` CLI. It fetches and caches the host test/serve binaries on first use, so there's nothing else to install by hand. There's no wasm compiler to fetch, because plugins run on the engine the host embeds.
26
+
27
+ ```sh
28
+ pip install owncast-plugin-sdk # or: uv tool install owncast-plugin-sdk
29
+ ```
30
+
31
+ > Not on PyPI yet (publishing is the last roadmap item). Until then, install from this repo with `uv tool install ./sdks/python` (or `pip install ./sdks/python`). That puts `owncast-plugin-py` on your PATH so every command below works as written.
32
+ >
33
+ > You *can* run it without installing via `uvx --from ./sdks/python owncast-plugin-py …`, but `uvx` is one-shot: the command isn't added to your PATH, so you must repeat the `uvx --from ./sdks/python` prefix on **every** command (`new`, `test`, `package`, …), not just the first. Installing once is simpler.
34
+
35
+ Scaffold a new plugin project (the Python peer of `npm create owncast-plugin`):
36
+
37
+ ```sh
38
+ owncast-plugin-py new my-plugin # drops a working starter into ./my-plugin
39
+ ```
40
+
41
+ This writes a `my-plugin/` directory with `plugin.manifest.json`, `src/plugin.py`, a sample `__tests__/plugin.test.json`, and docs (README, INSTRUCTIONS.md, AGENTS.md + a `create-owncast-plugin-py` skill) already wired up. A plugin is just a directory:
42
+
43
+ ```
44
+ my-plugin/
45
+ ├── plugin.manifest.json # name, slug, version, permissions
46
+ ├── src/plugin.py # your code
47
+ └── __tests__/*.test.json # optional scenario tests
48
+ ```
49
+
50
+ Build, test, serve, and package it:
51
+
52
+ ```sh
53
+ owncast-plugin-py build my-plugin # emit src/plugin.py -> <slug>.py
54
+ owncast-plugin-py test my-plugin # run the __tests__/ scenarios
55
+ owncast-plugin-py serve my-plugin # local dev server (POST /_dev/chat to drive it)
56
+ owncast-plugin-py package my-plugin # build + bundle -> <slug>.ocpkg (the only file you ship)
57
+ ```
58
+
59
+ Install the `.ocpkg` in Owncast from the admin **Plugins** page (**Upload plugin**) or by copying it to the server's `data/plugins/` directory, then toggle **Enabled**.
60
+
61
+ > **CI / no-install:** the build/package step is also runnable directly with `python3 sdks/python/owncast_plugin_build.py <dir> [--package]`, with no toolchain on `PATH` (it just emits source). `OWNCAST_PLUGIN_HOST_BIN_DIR` points `test`/`serve` at locally-built host binaries, and `OWNCAST_PLUGIN_HOST_BINARIES_VERSION` pins the release they're fetched from.
62
+
63
+ ## Writing a plugin
64
+
65
+ Import `plugin`, `owncast`, and `filter`, and register handlers with decorators:
66
+
67
+ ```python
68
+ from owncast_plugin import plugin, owncast, filter
69
+
70
+
71
+ @plugin.on_chat_message
72
+ def greet(msg):
73
+ owncast.chat.send(f"echo: {msg.body}")
74
+
75
+
76
+ @plugin.filter_chat_message
77
+ def block_spam(msg):
78
+ return filter.drop("spam") if "spam" in msg.body else filter.pass_()
79
+ ```
80
+
81
+ Declare the permissions you use (`chat.send` above) in `plugin.manifest.json`. The build only wires up the host functions your permissions grant.
82
+
83
+ ### Event handlers
84
+
85
+ Each decorator subscribes to one event, and the SDK derives the manifest subscriptions from which handlers you define.
86
+
87
+ | Decorator | Fires on |
88
+ |---|---|
89
+ | `@plugin.on_chat_message` | a chat message (notify) |
90
+ | `@plugin.filter_chat_message` | a chat message, **before broadcast** (return a `filter` result, requires `chat.filter`) |
91
+ | `@plugin.on_chat_user_joined` / `_parted` / `_renamed` | chat presence |
92
+ | `@plugin.on_message_moderated` | a message hidden/restored |
93
+ | `@plugin.on_stream_started` / `_stopped` / `_title_changed` | stream lifecycle |
94
+ | `@plugin.on_sse_connect` / `_disconnect` | a viewer's SSE stream opened/closed |
95
+ | `@plugin.on_tick` | ~once per second |
96
+ | `@plugin.on_fediverse_follow` / `_like` / `_repost` / `_mention` / `_reply` | fediverse activity |
97
+ | `@plugin.on("custom.event")` | a plugin-emitted custom event |
98
+ | `@plugin.on_tab_content("slug")` / `@plugin.on_page_content("slug")` | render dynamic viewer-page HTML |
99
+ | `@plugin.on_page_styles` / `@plugin.on_page_scripts` | inject viewer-page CSS / JS computed at request time |
100
+
101
+ Payloads are attribute objects with `snake_case` accessors over the wire JSON (`msg.body`, `msg.user.display_name`, `msg.client_id`). Use `msg.raw` for the underlying dict.
102
+
103
+ ### HTTP routing
104
+
105
+ Plugins with `http.serve` can answer requests under `/plugins/<slug>/…`. Route by path and method declaratively:
106
+
107
+ ```python
108
+ @plugin.get("/api/messages")
109
+ def list_messages(req):
110
+ return {"status": 200, "body": "...", "headers": {"Content-Type": "application/json"}}
111
+
112
+ @plugin.post("/api/messages")
113
+ def add_message(req):
114
+ body = req.body
115
+ return {"status": 201}
116
+
117
+ @plugin.on_http_request("/health") # any method, exact path
118
+ def health(req):
119
+ return "ok" # a plain string → 200 with that body
120
+
121
+ @plugin.on_http_request # bare: catch-all fallback
122
+ def fallback(req):
123
+ return {"status": 404}
124
+ ```
125
+
126
+ - `@plugin.get/post/put/delete/patch(path)` and `@plugin.route(path, methods=[...])` for method-specific routes, and `@plugin.on_http_request(path)` for any method.
127
+ - Paths are exact and **plugin-relative** (e.g. `/api/messages`), excluding the query string. Read query params from `req.query`.
128
+ - A request whose path matches a route but not its method gets an automatic **405**. An unmatched path falls through to the bare catch-all, else **404**.
129
+ - A handler returns a `dict` (`{status, body, headers}`), a `str` (→ 200), or `None` (→ 204).
130
+
131
+ ### The `owncast` host API
132
+
133
+ `owncast.<group>.<method>(...)`, and each group is gated by the matching manifest permission.
134
+
135
+ | Group | Methods |
136
+ |---|---|
137
+ | `chat` | `send`, `send_action`, `system`, `send_to`, `reply_to`, `history`, `clients`, `delete_message`, `kick` |
138
+ | `kv` | `get`, `set`, `get_json`, `set_json`, `delete` |
139
+ | `storage` / `fs` | `storage.upload`, `fs.read_text`, `fs.write`, `fs.list`, `fs.delete`, `fs.exists` |
140
+ | `server` / `stream` | `server.info/socials/emotes/federation/tags`, `stream.current/broadcaster` |
141
+ | `video_config` | `read`, `write` |
142
+ | `notifications` | `discord`, `browser_push`, `fediverse` |
143
+ | `users` | `list`, `get`, `set_enabled`, `ban_ip` |
144
+ | `events` / `fediverse` / `sse` | `events.emit`, `fediverse.post`, `sse.send` |
145
+ | `actions` | `add`, `clear` |
146
+ | `timer` | `set_timeout`, `set_interval`, `clear` |
147
+ | `config` / `assets` / `http` | `config.get`, `assets.read_text`, `http.fetch` (needs `network.fetch` + `network.allowedHosts`) |
148
+
149
+ Return values that are JSON objects come back as the same attribute objects (`owncast.server.info().name`), and lists come back as Python lists.
150
+
151
+ The concepts (events, permissions, the `.ocpkg` format, the manifest) are shared with the JS SDK, so the **[Owncast Plugin Author Guide](https://github.com/owncast/plugin-sdk/blob/main/docs/PLUGIN_AUTHOR_GUIDE.md)** applies. Just read the API names as their Pythonic `snake_case` forms.
152
+
153
+ ## How it works (and how it differs from the JS SDK)
154
+
155
+ Plugins run on a Python engine the Owncast host embeds and shares across every Python plugin, so there's no per-plugin compile. `build` writes your `src/plugin.py` out as `<slug>.py`, and `package` zips that with the manifest and assets into the `.ocpkg`. A single-file plugin is emitted with the `from owncast_plugin import …` line stripped (the SDK names are already globals in the engine). A plugin that imports other local modules has them inlined into the one shipped `plugin.py`. You still `from owncast_plugin import …` for editor support and unit tests.
156
+
157
+ Consequences worth knowing:
158
+
159
+ - **The entry can't use relative imports.** In `src/plugin.py` import your own modules absolutely (`from helpers import …`), not `from . import helpers`. Relative imports inside a package's own modules are fine.
160
+ - **Pure-Python only, no `pip`.** The embedded engine runs pure Python with no filesystem, so there's no `pip install` and C extensions (numpy, pandas, etc.) won't load. You add a third-party library by copying its pure-Python source into `src/` and importing it like any local module. For outbound HTTP use `owncast.http.fetch`, not `requests`.
161
+ - **Don't shadow stdlib names at module top level.** Your code runs in the same global scope as the runtime (which does `import json`), so a top-level `def json(...)` in your plugin shadows it and breaks things. Name helpers like `json_response` instead.
162
+ - **`snake_case` everywhere**, vs the JS SDK's camelCase (`send_action`, `get_json`, `msg.user.display_name`, `filter.pass_()`, where the trailing underscore avoids the Python keyword `pass`).
163
+
164
+ ## Testing
165
+
166
+ `__tests__/*.test.json` scenario files are **identical in format to the JS SDK's** and run through the same `owncast-plugin-test` binary, so a Python port of a plugin can reuse the JS version's test scenarios verbatim. Each scenario dispatches events / HTTP requests and asserts on observed side effects (`chatSends`, kv writes, HTTP responses, …).
167
+
168
+ ## Status
169
+
170
+ Working today: the runtime (`owncast_plugin/`), the `owncast-plugin-py` CLI (`new`/`build`/`test`/`serve`/`package`) with lazy host-binary download, the full host API, HTTP routing, `.ocpkg` packaging, a pip/uv-installable package (`pyproject.toml`), and CI that builds + tests every example. All 27 of the JS example plugins have Python counterparts under [`examples/python/`](../../examples/python).
171
+
172
+ Not yet (roadmap): publishing to PyPI and type stubs.
173
+
174
+ ## License
175
+
176
+ MIT
@@ -0,0 +1,164 @@
1
+ # owncast-plugin-sdk (Python)
2
+
3
+ SDK for authoring [Owncast](https://owncast.online) plugins in **Python**. Plugins ship their Python source and run sandboxed inside the Owncast server on an embedded Python engine. They use the same runtime, wire protocol, and `.ocpkg` format as the [JavaScript SDK](../js), so a Python plugin is a first-class peer of a JS one.
4
+
5
+ You write ordinary Python with decorators. There is no compile step and no PDK to install: the Owncast host embeds one Python engine and runs every Python plugin on it, so your plugin ships as plain source.
6
+
7
+ > ### Plugins are source, not a compiled binary
8
+ >
9
+ > Because the host supplies the Python engine, a plugin package contains only your `plugin.py`, its manifest, and any assets, with no bundled interpreter. That keeps packages small and means an author never installs or runs a wasm toolchain (`extism-py`, binaryen) at all. Those are a maintainer-only dependency of building the engine itself.
10
+
11
+ ## Quick start
12
+
13
+ Install the SDK to get the `owncast-plugin-py` CLI. It fetches and caches the host test/serve binaries on first use, so there's nothing else to install by hand. There's no wasm compiler to fetch, because plugins run on the engine the host embeds.
14
+
15
+ ```sh
16
+ pip install owncast-plugin-sdk # or: uv tool install owncast-plugin-sdk
17
+ ```
18
+
19
+ > Not on PyPI yet (publishing is the last roadmap item). Until then, install from this repo with `uv tool install ./sdks/python` (or `pip install ./sdks/python`). That puts `owncast-plugin-py` on your PATH so every command below works as written.
20
+ >
21
+ > You *can* run it without installing via `uvx --from ./sdks/python owncast-plugin-py …`, but `uvx` is one-shot: the command isn't added to your PATH, so you must repeat the `uvx --from ./sdks/python` prefix on **every** command (`new`, `test`, `package`, …), not just the first. Installing once is simpler.
22
+
23
+ Scaffold a new plugin project (the Python peer of `npm create owncast-plugin`):
24
+
25
+ ```sh
26
+ owncast-plugin-py new my-plugin # drops a working starter into ./my-plugin
27
+ ```
28
+
29
+ This writes a `my-plugin/` directory with `plugin.manifest.json`, `src/plugin.py`, a sample `__tests__/plugin.test.json`, and docs (README, INSTRUCTIONS.md, AGENTS.md + a `create-owncast-plugin-py` skill) already wired up. A plugin is just a directory:
30
+
31
+ ```
32
+ my-plugin/
33
+ ├── plugin.manifest.json # name, slug, version, permissions
34
+ ├── src/plugin.py # your code
35
+ └── __tests__/*.test.json # optional scenario tests
36
+ ```
37
+
38
+ Build, test, serve, and package it:
39
+
40
+ ```sh
41
+ owncast-plugin-py build my-plugin # emit src/plugin.py -> <slug>.py
42
+ owncast-plugin-py test my-plugin # run the __tests__/ scenarios
43
+ owncast-plugin-py serve my-plugin # local dev server (POST /_dev/chat to drive it)
44
+ owncast-plugin-py package my-plugin # build + bundle -> <slug>.ocpkg (the only file you ship)
45
+ ```
46
+
47
+ Install the `.ocpkg` in Owncast from the admin **Plugins** page (**Upload plugin**) or by copying it to the server's `data/plugins/` directory, then toggle **Enabled**.
48
+
49
+ > **CI / no-install:** the build/package step is also runnable directly with `python3 sdks/python/owncast_plugin_build.py <dir> [--package]`, with no toolchain on `PATH` (it just emits source). `OWNCAST_PLUGIN_HOST_BIN_DIR` points `test`/`serve` at locally-built host binaries, and `OWNCAST_PLUGIN_HOST_BINARIES_VERSION` pins the release they're fetched from.
50
+
51
+ ## Writing a plugin
52
+
53
+ Import `plugin`, `owncast`, and `filter`, and register handlers with decorators:
54
+
55
+ ```python
56
+ from owncast_plugin import plugin, owncast, filter
57
+
58
+
59
+ @plugin.on_chat_message
60
+ def greet(msg):
61
+ owncast.chat.send(f"echo: {msg.body}")
62
+
63
+
64
+ @plugin.filter_chat_message
65
+ def block_spam(msg):
66
+ return filter.drop("spam") if "spam" in msg.body else filter.pass_()
67
+ ```
68
+
69
+ Declare the permissions you use (`chat.send` above) in `plugin.manifest.json`. The build only wires up the host functions your permissions grant.
70
+
71
+ ### Event handlers
72
+
73
+ Each decorator subscribes to one event, and the SDK derives the manifest subscriptions from which handlers you define.
74
+
75
+ | Decorator | Fires on |
76
+ |---|---|
77
+ | `@plugin.on_chat_message` | a chat message (notify) |
78
+ | `@plugin.filter_chat_message` | a chat message, **before broadcast** (return a `filter` result, requires `chat.filter`) |
79
+ | `@plugin.on_chat_user_joined` / `_parted` / `_renamed` | chat presence |
80
+ | `@plugin.on_message_moderated` | a message hidden/restored |
81
+ | `@plugin.on_stream_started` / `_stopped` / `_title_changed` | stream lifecycle |
82
+ | `@plugin.on_sse_connect` / `_disconnect` | a viewer's SSE stream opened/closed |
83
+ | `@plugin.on_tick` | ~once per second |
84
+ | `@plugin.on_fediverse_follow` / `_like` / `_repost` / `_mention` / `_reply` | fediverse activity |
85
+ | `@plugin.on("custom.event")` | a plugin-emitted custom event |
86
+ | `@plugin.on_tab_content("slug")` / `@plugin.on_page_content("slug")` | render dynamic viewer-page HTML |
87
+ | `@plugin.on_page_styles` / `@plugin.on_page_scripts` | inject viewer-page CSS / JS computed at request time |
88
+
89
+ Payloads are attribute objects with `snake_case` accessors over the wire JSON (`msg.body`, `msg.user.display_name`, `msg.client_id`). Use `msg.raw` for the underlying dict.
90
+
91
+ ### HTTP routing
92
+
93
+ Plugins with `http.serve` can answer requests under `/plugins/<slug>/…`. Route by path and method declaratively:
94
+
95
+ ```python
96
+ @plugin.get("/api/messages")
97
+ def list_messages(req):
98
+ return {"status": 200, "body": "...", "headers": {"Content-Type": "application/json"}}
99
+
100
+ @plugin.post("/api/messages")
101
+ def add_message(req):
102
+ body = req.body
103
+ return {"status": 201}
104
+
105
+ @plugin.on_http_request("/health") # any method, exact path
106
+ def health(req):
107
+ return "ok" # a plain string → 200 with that body
108
+
109
+ @plugin.on_http_request # bare: catch-all fallback
110
+ def fallback(req):
111
+ return {"status": 404}
112
+ ```
113
+
114
+ - `@plugin.get/post/put/delete/patch(path)` and `@plugin.route(path, methods=[...])` for method-specific routes, and `@plugin.on_http_request(path)` for any method.
115
+ - Paths are exact and **plugin-relative** (e.g. `/api/messages`), excluding the query string. Read query params from `req.query`.
116
+ - A request whose path matches a route but not its method gets an automatic **405**. An unmatched path falls through to the bare catch-all, else **404**.
117
+ - A handler returns a `dict` (`{status, body, headers}`), a `str` (→ 200), or `None` (→ 204).
118
+
119
+ ### The `owncast` host API
120
+
121
+ `owncast.<group>.<method>(...)`, and each group is gated by the matching manifest permission.
122
+
123
+ | Group | Methods |
124
+ |---|---|
125
+ | `chat` | `send`, `send_action`, `system`, `send_to`, `reply_to`, `history`, `clients`, `delete_message`, `kick` |
126
+ | `kv` | `get`, `set`, `get_json`, `set_json`, `delete` |
127
+ | `storage` / `fs` | `storage.upload`, `fs.read_text`, `fs.write`, `fs.list`, `fs.delete`, `fs.exists` |
128
+ | `server` / `stream` | `server.info/socials/emotes/federation/tags`, `stream.current/broadcaster` |
129
+ | `video_config` | `read`, `write` |
130
+ | `notifications` | `discord`, `browser_push`, `fediverse` |
131
+ | `users` | `list`, `get`, `set_enabled`, `ban_ip` |
132
+ | `events` / `fediverse` / `sse` | `events.emit`, `fediverse.post`, `sse.send` |
133
+ | `actions` | `add`, `clear` |
134
+ | `timer` | `set_timeout`, `set_interval`, `clear` |
135
+ | `config` / `assets` / `http` | `config.get`, `assets.read_text`, `http.fetch` (needs `network.fetch` + `network.allowedHosts`) |
136
+
137
+ Return values that are JSON objects come back as the same attribute objects (`owncast.server.info().name`), and lists come back as Python lists.
138
+
139
+ The concepts (events, permissions, the `.ocpkg` format, the manifest) are shared with the JS SDK, so the **[Owncast Plugin Author Guide](https://github.com/owncast/plugin-sdk/blob/main/docs/PLUGIN_AUTHOR_GUIDE.md)** applies. Just read the API names as their Pythonic `snake_case` forms.
140
+
141
+ ## How it works (and how it differs from the JS SDK)
142
+
143
+ Plugins run on a Python engine the Owncast host embeds and shares across every Python plugin, so there's no per-plugin compile. `build` writes your `src/plugin.py` out as `<slug>.py`, and `package` zips that with the manifest and assets into the `.ocpkg`. A single-file plugin is emitted with the `from owncast_plugin import …` line stripped (the SDK names are already globals in the engine). A plugin that imports other local modules has them inlined into the one shipped `plugin.py`. You still `from owncast_plugin import …` for editor support and unit tests.
144
+
145
+ Consequences worth knowing:
146
+
147
+ - **The entry can't use relative imports.** In `src/plugin.py` import your own modules absolutely (`from helpers import …`), not `from . import helpers`. Relative imports inside a package's own modules are fine.
148
+ - **Pure-Python only, no `pip`.** The embedded engine runs pure Python with no filesystem, so there's no `pip install` and C extensions (numpy, pandas, etc.) won't load. You add a third-party library by copying its pure-Python source into `src/` and importing it like any local module. For outbound HTTP use `owncast.http.fetch`, not `requests`.
149
+ - **Don't shadow stdlib names at module top level.** Your code runs in the same global scope as the runtime (which does `import json`), so a top-level `def json(...)` in your plugin shadows it and breaks things. Name helpers like `json_response` instead.
150
+ - **`snake_case` everywhere**, vs the JS SDK's camelCase (`send_action`, `get_json`, `msg.user.display_name`, `filter.pass_()`, where the trailing underscore avoids the Python keyword `pass`).
151
+
152
+ ## Testing
153
+
154
+ `__tests__/*.test.json` scenario files are **identical in format to the JS SDK's** and run through the same `owncast-plugin-test` binary, so a Python port of a plugin can reuse the JS version's test scenarios verbatim. Each scenario dispatches events / HTTP requests and asserts on observed side effects (`chatSends`, kv writes, HTTP responses, …).
155
+
156
+ ## Status
157
+
158
+ Working today: the runtime (`owncast_plugin/`), the `owncast-plugin-py` CLI (`new`/`build`/`test`/`serve`/`package`) with lazy host-binary download, the full host API, HTTP routing, `.ocpkg` packaging, a pip/uv-installable package (`pyproject.toml`), and CI that builds + tests every example. All 27 of the JS example plugins have Python counterparts under [`examples/python/`](../../examples/python).
159
+
160
+ Not yet (roadmap): publishing to PyPI and type stubs.
161
+
162
+ ## License
163
+
164
+ MIT