margin-api 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.
Files changed (27) hide show
  1. margin_api-0.1.0/.gitignore +80 -0
  2. margin_api-0.1.0/CHANGELOG.md +13 -0
  3. margin_api-0.1.0/LICENSE +21 -0
  4. margin_api-0.1.0/PKG-INFO +121 -0
  5. margin_api-0.1.0/README.md +91 -0
  6. margin_api-0.1.0/pyproject.toml +47 -0
  7. margin_api-0.1.0/src/margin_api/__init__.py +3 -0
  8. margin_api-0.1.0/src/margin_api/app.py +338 -0
  9. margin_api-0.1.0/src/margin_api/jobs.py +253 -0
  10. margin_api-0.1.0/src/margin_api/llm_settings.py +173 -0
  11. margin_api-0.1.0/src/margin_api/main.py +17 -0
  12. margin_api-0.1.0/src/margin_api/py.typed +1 -0
  13. margin_api-0.1.0/src/margin_api/settings.py +32 -0
  14. margin_api-0.1.0/src/margin_api/ui/assets/index-DIeVYF0g.js +11 -0
  15. margin_api-0.1.0/src/margin_api/ui/assets/index-Do_ARaDC.css +1 -0
  16. margin_api-0.1.0/src/margin_api/ui/fonts/OFL-fraunces.txt +93 -0
  17. margin_api-0.1.0/src/margin_api/ui/fonts/OFL-instrument-sans.txt +93 -0
  18. margin_api-0.1.0/src/margin_api/ui/fonts/OFL-jetbrains-mono.txt +93 -0
  19. margin_api-0.1.0/src/margin_api/ui/fonts/fraunces-latin-ext.woff2 +0 -0
  20. margin_api-0.1.0/src/margin_api/ui/fonts/fraunces-latin.woff2 +0 -0
  21. margin_api-0.1.0/src/margin_api/ui/fonts/instrument-sans-latin-ext.woff2 +0 -0
  22. margin_api-0.1.0/src/margin_api/ui/fonts/instrument-sans-latin.woff2 +0 -0
  23. margin_api-0.1.0/src/margin_api/ui/fonts/jetbrains-mono-latin-ext.woff2 +0 -0
  24. margin_api-0.1.0/src/margin_api/ui/fonts/jetbrains-mono-latin.woff2 +0 -0
  25. margin_api-0.1.0/src/margin_api/ui/index.html +22 -0
  26. margin_api-0.1.0/src/margin_api/ui/mark.svg +1 -0
  27. margin_api-0.1.0/tests/test_api.py +403 -0
@@ -0,0 +1,80 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ *.so
6
+ .Python
7
+ build/
8
+ develop-eggs/
9
+ dist/
10
+ downloads/
11
+ eggs/
12
+ .eggs/
13
+ /lib/
14
+ /lib64/
15
+ parts/
16
+ sdist/
17
+ var/
18
+ wheels/
19
+ share/python-wheels/
20
+ *.egg-info/
21
+ .installed.cfg
22
+ *.egg
23
+
24
+ # Virtual environments
25
+ .venv/
26
+ venv/
27
+ ENV/
28
+ env/
29
+
30
+ # uv
31
+ .uv/
32
+
33
+ # Environment variables & secrets
34
+ .env
35
+ .env.local
36
+ .env.*.local
37
+ *.key
38
+ *.pem
39
+
40
+ # Testing & Coverage
41
+ .pytest_cache/
42
+ .coverage
43
+ .coverage.*
44
+ htmlcov/
45
+ .tox/
46
+ .nox/
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+
51
+ # Type checking & linting
52
+ .mypy_cache/
53
+ .ruff_cache/
54
+ .dmypy.json
55
+ dmypy.json
56
+
57
+ # IDEs & Editors
58
+ .vscode/
59
+ .idea/
60
+ *.swp
61
+ *.swo
62
+ *~
63
+
64
+ # Operating System
65
+ .DS_Store
66
+ Thumbs.db
67
+ ehthumbs.db
68
+ desktop.ini
69
+
70
+ # Output / Reports
71
+ report.json
72
+ report.md
73
+ temp/
74
+ tmp/
75
+
76
+ # Private planning notes (kept locally, not published)
77
+ PLAN.md
78
+
79
+ # the web UI bundled into margin-api at release time (scripts/build_release.py)
80
+ packages/api/src/margin_api/ui/
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ All notable changes to this package are documented here. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and versions follow
5
+ [Semantic Versioning](https://semver.org/).
6
+
7
+ ## [0.1.0] - 2026-10-21
8
+
9
+ ### Added
10
+ - Background runs with live progress (server-sent events), queue positions and cancellation.
11
+ - Model settings over HTTP: provider, models, keys (never returned), live model lists and a connection test.
12
+ - The web UI, bundled in the package and served from the same address.
13
+ - Answers only to `localhost` by default (protects against DNS rebinding).
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 margin contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,121 @@
1
+ Metadata-Version: 2.5
2
+ Name: margin-api
3
+ Version: 0.1.0
4
+ Summary: margin's HTTP API and web UI: run agents in the background, follow them live, read their evidence.
5
+ Project-URL: Homepage, https://github.com/Waddah0-0/margin
6
+ Project-URL: Source, https://github.com/Waddah0-0/margin/tree/main/packages/api
7
+ Project-URL: Issues, https://github.com/Waddah0-0/margin/issues
8
+ Project-URL: Changelog, https://github.com/Waddah0-0/margin/blob/main/packages/api/CHANGELOG.md
9
+ Author: Waddah Ali
10
+ License-Expression: MIT
11
+ License-File: LICENSE
12
+ Keywords: agents,api,fastapi,llm
13
+ Classifier: Development Status :: 3 - Alpha
14
+ Classifier: Environment :: Web Environment
15
+ Classifier: Framework :: FastAPI
16
+ Classifier: Intended Audience :: Developers
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Typing :: Typed
22
+ Requires-Python: >=3.11
23
+ Requires-Dist: fastapi>=0.111.0
24
+ Requires-Dist: httpx>=0.27.0
25
+ Requires-Dist: margin-core<0.2,>=0.1.0
26
+ Requires-Dist: pydantic-settings>=2.2.0
27
+ Requires-Dist: sse-starlette>=2.1.0
28
+ Requires-Dist: uvicorn>=0.30.0
29
+ Description-Content-Type: text/markdown
30
+
31
+ # margin-api
32
+
33
+ The web UI and HTTP API for margin. It runs your installed agents in the background, so you
34
+ can start a run, watch its progress live and read the result with all its evidence, in the
35
+ browser or from any program that speaks HTTP.
36
+
37
+ ## Starting it
38
+
39
+ ```bash
40
+ pip install margin-api margin-ai-code-detector
41
+ margin-api # open http://127.0.0.1:8000 (API docs at /docs)
42
+ ```
43
+
44
+ The web UI comes inside the package, so that one address is the whole app. The server only
45
+ listens on `127.0.0.1`, which means nothing else on your network can reach it. You can change
46
+ that and a few other things with `MARGIN_API_` environment variables (or a `.env` file):
47
+
48
+ | Setting | Default | Meaning |
49
+ |---|---|---|
50
+ | `MARGIN_API_HOST` / `MARGIN_API_PORT` | `127.0.0.1` / `8000` | Where to listen. Change the host only if you mean to expose it. |
51
+ | `MARGIN_API_ALLOWED_HOSTS` | `["127.0.0.1", "localhost"]` | Host names the API answers to. This blocks DNS-rebinding attacks from web pages; add your host name if you expose the API. |
52
+ | `MARGIN_API_CORS_ORIGINS` | `["http://localhost:3000", "http://127.0.0.1:3000"]` | Web pages allowed to call it (JSON list). |
53
+ | `MARGIN_API_LLM_CONCURRENCY` | `1` | Runs of agents that use a model, at once (one local GPU). |
54
+ | `MARGIN_API_OTHER_CONCURRENCY` | `4` | Runs of other agents, at once. |
55
+ | `MARGIN_API_RESULT_TTL_S` | `3600` | Seconds a finished run is kept. |
56
+ | `MARGIN_API_MAX_BODY_BYTES` | `1000000` | Largest accepted request body. |
57
+ | `MARGIN_API_CANCEL_GRACE_S` | `30` | Time a cancelled run gets to stop on its own before it is stopped. |
58
+
59
+ Model settings (`DEFAULT_LLM_PROVIDER`, `LOCAL_LLM_BASE_URL`, ...) are the same as for the CLI.
60
+
61
+ ## Endpoints
62
+
63
+ | Method and path | What it does |
64
+ |---|---|
65
+ | `GET /health` | Liveness check. |
66
+ | `GET /agents` | Installed agents with their input and output JSON schemas, plus any that failed to load. |
67
+ | `GET /agents/{id}` | One agent. |
68
+ | `POST /agents/{id}/runs` | Start a run. The body is the agent's input as JSON; `?provider=` and `?model=` choose the model for this run only. Answers `202` with the `run_id`. |
69
+ | `GET /runs/{run_id}` | Status (`queued`, `running`, `done`, `cancelled`), queue position, latest progress and, when done, the result. |
70
+ | `GET /runs/{run_id}/events` | Server-sent events: `status`, `progress`, then one final `result`. |
71
+ | `DELETE /runs/{run_id}` | Cancel a run. |
72
+ | `GET /runs` | Recent runs. |
73
+ | `GET /settings/llm` | Model settings: the provider in use, each provider's model, whether a key is set (with its last four characters, never the key), and where each value comes from. |
74
+ | `PUT /settings/llm` | Save model settings (provider, models, keys, local server address, spending limit). Applies from the next run. |
75
+ | `GET /settings/llm/models?provider=` | The models the provider offers right now, asked live. |
76
+ | `POST /settings/llm/test` | Send one tiny prompt and report whether, and how fast, the model answered. |
77
+
78
+ ## Example with curl
79
+
80
+ ```bash
81
+ # start a run
82
+ curl -s -X POST http://127.0.0.1:8000/agents/ai-code-detector/runs \
83
+ -H 'content-type: application/json' -d '{"target": "https://github.com/psf/requests"}'
84
+ # -> {"run_id": "342e1c...", "status": "queued", "queue_position": 1, "links": {...}}
85
+
86
+ # follow it live (the stream ends with a "result" event)
87
+ curl -N http://127.0.0.1:8000/runs/342e1c.../events
88
+
89
+ # or poll it, or cancel it
90
+ curl -s http://127.0.0.1:8000/runs/342e1c...
91
+ curl -s -X DELETE http://127.0.0.1:8000/runs/342e1c...
92
+ ```
93
+
94
+ ## How runs behave
95
+
96
+ Starting a run returns straight away with a `run_id`; the work happens in the background.
97
+
98
+ Runs that use a local model wait in one queue and go one at a time, since a local model
99
+ usually has a single GPU to work with. Runs on Anthropic or OpenAI skip that queue, and agents
100
+ that use no model run up to four at once. While a run waits, it reports its place in line.
101
+
102
+ Each run keeps its last 200 events, and the event stream replays them before going live, so
103
+ you can connect late without missing anything. If the connection drops, reconnect with the
104
+ standard `Last-Event-ID` header and the stream picks up where it stopped.
105
+
106
+ When an agent fails, the run still finishes as `done`; the failure is in its result
107
+ (`status: "error"`, with the reason). HTTP errors are for bad requests: `404` for an unknown
108
+ agent or run, `422` for input that doesn't match the agent's schema, `413` for a body that's
109
+ too large, and `500` for an agent that failed to load.
110
+
111
+ Cancelling asks the agent to stop at its next checkpoint. A model call already under way is
112
+ allowed to finish, and an agent that still hasn't stopped after the grace period is stopped.
113
+
114
+ Runs live in memory. They're gone after an hour, or when the server restarts, so export a
115
+ result if you want to keep it.
116
+
117
+ ## Working on the UI
118
+
119
+ In a source checkout, `scripts/export_openapi.py` writes the OpenAPI schema to
120
+ `ui/openapi.json`, and the UI's typed client is generated from it. CI fails if the file is
121
+ out of date.
@@ -0,0 +1,91 @@
1
+ # margin-api
2
+
3
+ The web UI and HTTP API for margin. It runs your installed agents in the background, so you
4
+ can start a run, watch its progress live and read the result with all its evidence, in the
5
+ browser or from any program that speaks HTTP.
6
+
7
+ ## Starting it
8
+
9
+ ```bash
10
+ pip install margin-api margin-ai-code-detector
11
+ margin-api # open http://127.0.0.1:8000 (API docs at /docs)
12
+ ```
13
+
14
+ The web UI comes inside the package, so that one address is the whole app. The server only
15
+ listens on `127.0.0.1`, which means nothing else on your network can reach it. You can change
16
+ that and a few other things with `MARGIN_API_` environment variables (or a `.env` file):
17
+
18
+ | Setting | Default | Meaning |
19
+ |---|---|---|
20
+ | `MARGIN_API_HOST` / `MARGIN_API_PORT` | `127.0.0.1` / `8000` | Where to listen. Change the host only if you mean to expose it. |
21
+ | `MARGIN_API_ALLOWED_HOSTS` | `["127.0.0.1", "localhost"]` | Host names the API answers to. This blocks DNS-rebinding attacks from web pages; add your host name if you expose the API. |
22
+ | `MARGIN_API_CORS_ORIGINS` | `["http://localhost:3000", "http://127.0.0.1:3000"]` | Web pages allowed to call it (JSON list). |
23
+ | `MARGIN_API_LLM_CONCURRENCY` | `1` | Runs of agents that use a model, at once (one local GPU). |
24
+ | `MARGIN_API_OTHER_CONCURRENCY` | `4` | Runs of other agents, at once. |
25
+ | `MARGIN_API_RESULT_TTL_S` | `3600` | Seconds a finished run is kept. |
26
+ | `MARGIN_API_MAX_BODY_BYTES` | `1000000` | Largest accepted request body. |
27
+ | `MARGIN_API_CANCEL_GRACE_S` | `30` | Time a cancelled run gets to stop on its own before it is stopped. |
28
+
29
+ Model settings (`DEFAULT_LLM_PROVIDER`, `LOCAL_LLM_BASE_URL`, ...) are the same as for the CLI.
30
+
31
+ ## Endpoints
32
+
33
+ | Method and path | What it does |
34
+ |---|---|
35
+ | `GET /health` | Liveness check. |
36
+ | `GET /agents` | Installed agents with their input and output JSON schemas, plus any that failed to load. |
37
+ | `GET /agents/{id}` | One agent. |
38
+ | `POST /agents/{id}/runs` | Start a run. The body is the agent's input as JSON; `?provider=` and `?model=` choose the model for this run only. Answers `202` with the `run_id`. |
39
+ | `GET /runs/{run_id}` | Status (`queued`, `running`, `done`, `cancelled`), queue position, latest progress and, when done, the result. |
40
+ | `GET /runs/{run_id}/events` | Server-sent events: `status`, `progress`, then one final `result`. |
41
+ | `DELETE /runs/{run_id}` | Cancel a run. |
42
+ | `GET /runs` | Recent runs. |
43
+ | `GET /settings/llm` | Model settings: the provider in use, each provider's model, whether a key is set (with its last four characters, never the key), and where each value comes from. |
44
+ | `PUT /settings/llm` | Save model settings (provider, models, keys, local server address, spending limit). Applies from the next run. |
45
+ | `GET /settings/llm/models?provider=` | The models the provider offers right now, asked live. |
46
+ | `POST /settings/llm/test` | Send one tiny prompt and report whether, and how fast, the model answered. |
47
+
48
+ ## Example with curl
49
+
50
+ ```bash
51
+ # start a run
52
+ curl -s -X POST http://127.0.0.1:8000/agents/ai-code-detector/runs \
53
+ -H 'content-type: application/json' -d '{"target": "https://github.com/psf/requests"}'
54
+ # -> {"run_id": "342e1c...", "status": "queued", "queue_position": 1, "links": {...}}
55
+
56
+ # follow it live (the stream ends with a "result" event)
57
+ curl -N http://127.0.0.1:8000/runs/342e1c.../events
58
+
59
+ # or poll it, or cancel it
60
+ curl -s http://127.0.0.1:8000/runs/342e1c...
61
+ curl -s -X DELETE http://127.0.0.1:8000/runs/342e1c...
62
+ ```
63
+
64
+ ## How runs behave
65
+
66
+ Starting a run returns straight away with a `run_id`; the work happens in the background.
67
+
68
+ Runs that use a local model wait in one queue and go one at a time, since a local model
69
+ usually has a single GPU to work with. Runs on Anthropic or OpenAI skip that queue, and agents
70
+ that use no model run up to four at once. While a run waits, it reports its place in line.
71
+
72
+ Each run keeps its last 200 events, and the event stream replays them before going live, so
73
+ you can connect late without missing anything. If the connection drops, reconnect with the
74
+ standard `Last-Event-ID` header and the stream picks up where it stopped.
75
+
76
+ When an agent fails, the run still finishes as `done`; the failure is in its result
77
+ (`status: "error"`, with the reason). HTTP errors are for bad requests: `404` for an unknown
78
+ agent or run, `422` for input that doesn't match the agent's schema, `413` for a body that's
79
+ too large, and `500` for an agent that failed to load.
80
+
81
+ Cancelling asks the agent to stop at its next checkpoint. A model call already under way is
82
+ allowed to finish, and an agent that still hasn't stopped after the grace period is stopped.
83
+
84
+ Runs live in memory. They're gone after an hour, or when the server restarts, so export a
85
+ result if you want to keep it.
86
+
87
+ ## Working on the UI
88
+
89
+ In a source checkout, `scripts/export_openapi.py` writes the OpenAPI schema to
90
+ `ui/openapi.json`, and the UI's typed client is generated from it. CI fails if the file is
91
+ out of date.
@@ -0,0 +1,47 @@
1
+ [project]
2
+ name = "margin-api"
3
+ version = "0.1.0"
4
+ description = "margin's HTTP API and web UI: run agents in the background, follow them live, read their evidence."
5
+ readme = "README.md"
6
+ requires-python = ">=3.11"
7
+ license = "MIT"
8
+ license-files = ["LICENSE"]
9
+ authors = [{ name = "Waddah Ali" }]
10
+ keywords = ["agents", "llm", "api", "fastapi"]
11
+ classifiers = [
12
+ "Development Status :: 3 - Alpha",
13
+ "Intended Audience :: Developers",
14
+ "Operating System :: OS Independent",
15
+ "Programming Language :: Python :: 3",
16
+ "Programming Language :: Python :: 3.11",
17
+ "Programming Language :: Python :: 3.12",
18
+ "Typing :: Typed",
19
+ "Framework :: FastAPI",
20
+ "Environment :: Web Environment",
21
+ ]
22
+ dependencies = [
23
+ "margin-core>=0.1.0,<0.2",
24
+ "fastapi>=0.111.0",
25
+ "uvicorn>=0.30.0",
26
+ "sse-starlette>=2.1.0",
27
+ "httpx>=0.27.0",
28
+ "pydantic-settings>=2.2.0",
29
+ ]
30
+
31
+ [project.scripts]
32
+ margin-api = "margin_api.main:run"
33
+
34
+ [project.urls]
35
+ Homepage = "https://github.com/Waddah0-0/margin"
36
+ Source = "https://github.com/Waddah0-0/margin/tree/main/packages/api"
37
+ Issues = "https://github.com/Waddah0-0/margin/issues"
38
+ Changelog = "https://github.com/Waddah0-0/margin/blob/main/packages/api/CHANGELOG.md"
39
+
40
+ [build-system]
41
+ requires = ["hatchling"]
42
+ build-backend = "hatchling.build"
43
+
44
+ [tool.hatch.build.targets.wheel]
45
+ packages = ["src/margin_api"]
46
+ # the built web UI, copied in by scripts/build_release.py (git-ignored, so listed here)
47
+ artifacts = ["src/margin_api/ui/**"]
@@ -0,0 +1,3 @@
1
+ """HTTP API for margin."""
2
+
3
+ __version__ = "0.1.0"