bazaar-contrib-server-magpie 0.3.0rc1__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 (45) hide show
  1. bazaar_contrib_server_magpie-0.3.0rc1/PKG-INFO +58 -0
  2. bazaar_contrib_server_magpie-0.3.0rc1/README.md +40 -0
  3. bazaar_contrib_server_magpie-0.3.0rc1/pyproject.toml +51 -0
  4. bazaar_contrib_server_magpie-0.3.0rc1/pyproject.toml.orig +51 -0
  5. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/__init__.py +68 -0
  6. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/admin.py +243 -0
  7. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/api.py +69 -0
  8. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/data.py +130 -0
  9. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/gateways.py +130 -0
  10. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/pages.py +505 -0
  11. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/locales/en.toml +218 -0
  12. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/locales/zh-CN.toml +218 -0
  13. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/magpie-license.txt +21 -0
  14. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/static/icons/anthropic.svg +1 -0
  15. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/static/icons/deepseek-color.svg +1 -0
  16. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/static/icons/kimi.svg +1 -0
  17. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/static/icons/magpie.svg +1 -0
  18. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/static/icons/minimax-color.svg +1 -0
  19. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/static/icons/opencode.svg +1 -0
  20. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/static/icons/openrouter.svg +1 -0
  21. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/static/icons/qwen-color.svg +1 -0
  22. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/static/icons/workbuddy-color.svg +1 -0
  23. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/static/icons/zhipu-color.svg +1 -0
  24. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/static/magpie.css +333 -0
  25. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/static/magpie.js +179 -0
  26. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/static/mark.png +0 -0
  27. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/templates/magpie/gateway.html +20 -0
  28. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/templates/magpie/icons/activity.svg +1 -0
  29. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/templates/magpie/icons/close.svg +1 -0
  30. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/templates/magpie/icons/coins.svg +1 -0
  31. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/templates/magpie/icons/file-text.svg +1 -0
  32. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/templates/magpie/icons/list.svg +1 -0
  33. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/templates/magpie/icons/plus.svg +1 -0
  34. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/templates/magpie/icons/search.svg +1 -0
  35. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/templates/magpie/icons/triangle-alert.svg +1 -0
  36. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/templates/magpie/index.html +47 -0
  37. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/templates/magpie/logos/claudecode.svg +1 -0
  38. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/templates/magpie/logos/codex.svg +1 -0
  39. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/templates/magpie/manage.html +156 -0
  40. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/templates/magpie/operation.html +24 -0
  41. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/templates/magpie/overview.html +21 -0
  42. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/templates/magpie/parts.html +40 -0
  43. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/templates/magpie/requests.html +91 -0
  44. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/templates/magpie/routing.html +76 -0
  45. bazaar_contrib_server_magpie-0.3.0rc1/src/bazaar_contrib_server_magpie/resources/templates/magpie/usage.html +55 -0
@@ -0,0 +1,58 @@
1
+ Metadata-Version: 2.3
2
+ Name: bazaar-contrib-server-magpie
3
+ Version: 0.3.0rc1
4
+ Summary: Magpie routing, usage and gateway administration in Bazaar Server
5
+ Requires-Dist: aiohttp>=3.13.0,<4
6
+ Requires-Dist: bazaar-core~=0.3.0rc32
7
+ Requires-Dist: bazaar-contrib-schema~=0.3.0rc30
8
+ Requires-Dist: bazaar-contrib-server~=0.3.0rc15
9
+ Requires-Dist: bazaar-contrib-server-database~=0.3.0rc1
10
+ Requires-Dist: bazaar-contrib-server-overview~=0.3.0rc5
11
+ Requires-Dist: bazaar-contrib-storage~=0.3.0rc30
12
+ Requires-Dist: pydantic>=2.12,<3
13
+ Requires-Dist: python-multipart>=0.0.32
14
+ Requires-Dist: starlette>=0.48
15
+ Requires-Python: >=3.15
16
+ Project-URL: Repository, https://github.com/open-bazaar/contrib-server-magpie
17
+ Description-Content-Type: text/markdown
18
+
19
+ # Magpie for Bazaar Server
20
+
21
+ `server-magpie` is a standalone Bazaar Server contrib. It registers a Magpie
22
+ sidebar page and an Overview card using the Server and Overview services.
23
+ Install the wheel on the computer running Bazaar Server and add an entry to
24
+ that server's existing configuration:
25
+
26
+ ```toml
27
+ [[contribs]]
28
+ package = "bazaar-contrib-server-magpie==0.3.0rc1"
29
+ [[contribs.enabled]]
30
+ entry = "server-magpie"
31
+ ```
32
+
33
+ Open **Magpie → Add gateway** in a Server account with `magpie.manage` and
34
+ enter the gateway's name, base URL, and one Gateway Key. For remote
35
+ administration choose a Key with **From any computer** administration in
36
+ Magpie. The same Key is used for readings and writes. A read-only Key can be
37
+ used for monitoring; a denied admin operation shows the error at its point
38
+ of use. `magpie.view` grants access to readings without granting Server-side
39
+ management. The integration credential is stored with the Server database
40
+ and is used only on the Server, never in a link, page, or event stream.
41
+
42
+ The API contract targets Magpie `v0.1.4530`. The plugin uses the official
43
+ route feed, concurrency, request/usage ledger, cached quotas and management
44
+ API. It does not use private GUI endpoints or the local-only Trace API.
45
+ Routing and billing views do not show message bodies.
46
+
47
+ In **Gateway settings → Models**, refresh a provider's list, enter one or
48
+ more model IDs, or test models individually or together. Adding to an
49
+ automatic list keeps the currently served models as explicit picks.
50
+ Connectivity tests are the only actions here that send sample model
51
+ requests. They run in Magpie with the provider's existing credentials.
52
+
53
+ Usage periods follow Magpie's server clock. Request timestamps are shown in
54
+ the viewer's time zone.
55
+
56
+ The Magpie and supported provider marks are from Magpie `v0.1.4530`. Its
57
+ upstream MIT license is reproduced in
58
+ `src/bazaar_contrib_server_magpie/resources/magpie-license.txt`.
@@ -0,0 +1,40 @@
1
+ # Magpie for Bazaar Server
2
+
3
+ `server-magpie` is a standalone Bazaar Server contrib. It registers a Magpie
4
+ sidebar page and an Overview card using the Server and Overview services.
5
+ Install the wheel on the computer running Bazaar Server and add an entry to
6
+ that server's existing configuration:
7
+
8
+ ```toml
9
+ [[contribs]]
10
+ package = "bazaar-contrib-server-magpie==0.3.0rc1"
11
+ [[contribs.enabled]]
12
+ entry = "server-magpie"
13
+ ```
14
+
15
+ Open **Magpie → Add gateway** in a Server account with `magpie.manage` and
16
+ enter the gateway's name, base URL, and one Gateway Key. For remote
17
+ administration choose a Key with **From any computer** administration in
18
+ Magpie. The same Key is used for readings and writes. A read-only Key can be
19
+ used for monitoring; a denied admin operation shows the error at its point
20
+ of use. `magpie.view` grants access to readings without granting Server-side
21
+ management. The integration credential is stored with the Server database
22
+ and is used only on the Server, never in a link, page, or event stream.
23
+
24
+ The API contract targets Magpie `v0.1.4530`. The plugin uses the official
25
+ route feed, concurrency, request/usage ledger, cached quotas and management
26
+ API. It does not use private GUI endpoints or the local-only Trace API.
27
+ Routing and billing views do not show message bodies.
28
+
29
+ In **Gateway settings → Models**, refresh a provider's list, enter one or
30
+ more model IDs, or test models individually or together. Adding to an
31
+ automatic list keeps the currently served models as explicit picks.
32
+ Connectivity tests are the only actions here that send sample model
33
+ requests. They run in Magpie with the provider's existing credentials.
34
+
35
+ Usage periods follow Magpie's server clock. Request timestamps are shown in
36
+ the viewer's time zone.
37
+
38
+ The Magpie and supported provider marks are from Magpie `v0.1.4530`. Its
39
+ upstream MIT license is reproduced in
40
+ `src/bazaar_contrib_server_magpie/resources/magpie-license.txt`.
@@ -0,0 +1,51 @@
1
+ [project]
2
+ name = "bazaar-contrib-server-magpie"
3
+ version = "0.3.0rc1"
4
+ description = "Magpie routing, usage and gateway administration in Bazaar Server"
5
+ readme = "README.md"
6
+ requires-python = ">=3.15"
7
+ dependencies = [
8
+ "aiohttp>=3.13.0,<4",
9
+ "bazaar-core~=0.3.0rc32",
10
+ "bazaar-contrib-schema~=0.3.0rc30",
11
+ "bazaar-contrib-server~=0.3.0rc15",
12
+ "bazaar-contrib-server-database~=0.3.0rc1",
13
+ "bazaar-contrib-server-overview~=0.3.0rc5",
14
+ "bazaar-contrib-storage~=0.3.0rc30",
15
+ "pydantic>=2.12,<3",
16
+ "python-multipart>=0.0.32",
17
+ "starlette>=0.48",
18
+ ]
19
+
20
+ [project.entry-points."bazaar_compute_node.contribs"]
21
+ server-magpie = "bazaar_contrib_server_magpie:magpie"
22
+
23
+ [project.urls]
24
+ Repository = "https://github.com/open-bazaar/contrib-server-magpie"
25
+
26
+ [build-system]
27
+ requires = ["uv_build>=0.13.0,<0.14.0"]
28
+ build-backend = "uv_build"
29
+
30
+ [tool.pytest.ini_options]
31
+ testpaths = ["tests"]
32
+ addopts = "-p no:anyio"
33
+ asyncio_mode = "auto"
34
+ asyncio_default_fixture_loop_scope = "function"
35
+
36
+ [tool.ruff.lint]
37
+ extend-select = ["ASYNC"]
38
+
39
+ [dependency-groups]
40
+ dev = [
41
+ "bazaar-contrib-http~=0.3.0rc1",
42
+ "bazaar-contrib-server-access~=0.3.0rc8",
43
+ "bazaar-contrib-server-migrations~=0.3.0rc2",
44
+ "bazaar-contrib-sqlite~=0.3.0rc10",
45
+ "bazaar-contrib-timer~=0.3.0rc32",
46
+ "httpx>=0.28,<1",
47
+ "playwright>=1.56,<2",
48
+ "pytest>=9,<10",
49
+ "pytest-asyncio>=1.3,<2",
50
+ "ruff>=0.9",
51
+ ]
@@ -0,0 +1,51 @@
1
+ [project]
2
+ name = "bazaar-contrib-server-magpie"
3
+ version = "0.3.0rc1"
4
+ description = "Magpie routing, usage and gateway administration in Bazaar Server"
5
+ readme = "README.md"
6
+ requires-python = ">=3.15"
7
+ dependencies = [
8
+ "aiohttp>=3.13.0,<4",
9
+ "bazaar-core~=0.3.0rc32",
10
+ "bazaar-contrib-schema~=0.3.0rc30",
11
+ "bazaar-contrib-server~=0.3.0rc15",
12
+ "bazaar-contrib-server-database~=0.3.0rc1",
13
+ "bazaar-contrib-server-overview~=0.3.0rc5",
14
+ "bazaar-contrib-storage~=0.3.0rc30",
15
+ "pydantic>=2.12,<3",
16
+ "python-multipart>=0.0.32",
17
+ "starlette>=0.48",
18
+ ]
19
+
20
+ [project.entry-points."bazaar_compute_node.contribs"]
21
+ server-magpie = "bazaar_contrib_server_magpie:magpie"
22
+
23
+ [project.urls]
24
+ Repository = "https://github.com/open-bazaar/contrib-server-magpie"
25
+
26
+ [build-system]
27
+ requires = ["uv_build>=0.13.0,<0.14.0"]
28
+ build-backend = "uv_build"
29
+
30
+ [tool.pytest.ini_options]
31
+ testpaths = ["tests"]
32
+ addopts = "-p no:anyio"
33
+ asyncio_mode = "auto"
34
+ asyncio_default_fixture_loop_scope = "function"
35
+
36
+ [tool.ruff.lint]
37
+ extend-select = ["ASYNC"]
38
+
39
+ [dependency-groups]
40
+ dev = [
41
+ "bazaar-contrib-http~=0.3.0rc1",
42
+ "bazaar-contrib-server-access~=0.3.0rc8",
43
+ "bazaar-contrib-server-migrations~=0.3.0rc2",
44
+ "bazaar-contrib-sqlite~=0.3.0rc10",
45
+ "bazaar-contrib-timer~=0.3.0rc32",
46
+ "httpx>=0.28,<1",
47
+ "playwright>=1.56,<2",
48
+ "pytest>=9,<10",
49
+ "pytest-asyncio>=1.3,<2",
50
+ "ruff>=0.9",
51
+ ]
@@ -0,0 +1,68 @@
1
+ """Magpie gateways in Bazaar Server."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from contextlib import asynccontextmanager
6
+ from importlib.resources import files
7
+ from pathlib import Path
8
+
9
+ from aiohttp import ClientSession
10
+ from bazaar_contrib_server import SERVER, PermissionItem, SidebarItem
11
+ from bazaar_contrib_server_database import SERVER_DB, SERVER_SCHEMA
12
+ from bazaar_contrib_server_overview import OVERVIEW, OverviewItem
13
+ from bazaar_core import Context, Contrib
14
+
15
+ from .api import Magpie
16
+ from .gateways import MODULE, Gateways
17
+ from .pages import MANAGE, VIEW, Views
18
+
19
+ __all__ = ["magpie"]
20
+ ICON = '<img src="/static/magpie/icons/magpie.svg" width="18" height="18" alt="">'
21
+
22
+
23
+ @asynccontextmanager
24
+ async def lifespan(ctx: Context):
25
+ server = ctx.accept(SERVER)
26
+ resources = Path(str(files(__name__))) / "resources"
27
+ async with ctx.accept(SERVER_SCHEMA).register(MODULE), ClientSession() as session:
28
+ pages = await server.pages("magpie", __name__)
29
+ views = Views(pages, Gateways(ctx.accept(SERVER_DB)), Magpie(session))
30
+ async with (
31
+ server.module(
32
+ "magpie",
33
+ ui=pages.application(views.routes),
34
+ locales=resources / "locales",
35
+ static=resources / "static",
36
+ ),
37
+ server.permission(
38
+ PermissionItem(point=VIEW, description="magpie.permission.view")
39
+ ),
40
+ server.permission(
41
+ PermissionItem(point=MANAGE, description="magpie.permission.manage")
42
+ ),
43
+ server.sidebar(
44
+ SidebarItem(
45
+ text="magpie.title",
46
+ icon=ICON,
47
+ path="/magpie",
48
+ group="magpie.sidebar",
49
+ permission=VIEW,
50
+ order=70,
51
+ )
52
+ ),
53
+ ctx.accept(OVERVIEW).register(
54
+ OverviewItem(
55
+ key="magpie",
56
+ title="magpie.title",
57
+ permission=VIEW,
58
+ src="/ui/magpie/overview",
59
+ order=60,
60
+ )
61
+ ),
62
+ ):
63
+ yield
64
+
65
+
66
+ magpie = Contrib(
67
+ accepts=[SERVER, SERVER_DB, SERVER_SCHEMA, OVERVIEW], lifespan=lifespan
68
+ )
@@ -0,0 +1,243 @@
1
+ """Named forms backed by the public Magpie management API."""
2
+
3
+ import asyncio
4
+ from collections.abc import Awaitable, Callable
5
+
6
+ from starlette.datastructures import FormData
7
+
8
+ from .api import Data
9
+
10
+ SECTIONS = (
11
+ "routing-groups",
12
+ "gateway-keys",
13
+ "provider-keys",
14
+ "account-groups",
15
+ "models",
16
+ )
17
+ PATHS = {
18
+ "gateway-keys": "admin/gateway-keys",
19
+ "provider-keys": "admin/providers",
20
+ "account-groups": "admin/account-groups",
21
+ "routing-groups": "admin/routing-groups",
22
+ "models": "admin/providers",
23
+ }
24
+ CATALOG = "admin/gateway-keys/models"
25
+ MODEL_SAVE_FIELDS = (
26
+ "id",
27
+ "name",
28
+ "preset",
29
+ "icon",
30
+ "iconFrom",
31
+ "chat",
32
+ "responses",
33
+ "anthropic",
34
+ "gemini",
35
+ "decide",
36
+ "catalog",
37
+ "headers",
38
+ "fallback",
39
+ "balanceURL",
40
+ "balancePath",
41
+ "balanceMethod",
42
+ "modelsURL",
43
+ "searches",
44
+ "unredacted",
45
+ "pinUpstream",
46
+ "countFree",
47
+ "billing",
48
+ "unlisted",
49
+ )
50
+ type Read = Callable[..., Awaitable[Data]]
51
+
52
+
53
+ async def load(section: str, read: Read) -> Data:
54
+ path = PATHS[section]
55
+ if section in ("gateway-keys", "account-groups", "routing-groups"):
56
+ config, catalog = await asyncio.gather(read(path), read(CATALOG))
57
+ else:
58
+ config, catalog = await read(path), {}
59
+ values = {
60
+ "section": section,
61
+ "sections": SECTIONS,
62
+ "path": path,
63
+ "config": config,
64
+ "catalog": catalog,
65
+ }
66
+ if section == "models":
67
+ chips = {
68
+ provider["id"]: provider_models(provider)
69
+ for provider in config.get("providers") or ()
70
+ }
71
+ values.update(
72
+ model_chips=chips,
73
+ model_batch={
74
+ provider_id: bool(test_models(models))
75
+ for provider_id, models in chips.items()
76
+ },
77
+ )
78
+ return values
79
+
80
+
81
+ def group_members(group: Data) -> list[str]:
82
+ matched = set(group.get("matched") or [])
83
+ return [member for member in (group.get("members") or []) if member not in matched]
84
+
85
+
86
+ def provider_models(provider: Data) -> list[Data]:
87
+ can_test = not provider.get("modelTest")
88
+ deciders = set(provider.get("deciders") or ())
89
+ chips: dict[str, Data] = {}
90
+ for model in provider.get("models") or ():
91
+ model_id = model["id"]
92
+ chips[model_id] = {
93
+ "id": model_id,
94
+ "name": model.get("name") or model_id,
95
+ "active": bool(model.get("on")),
96
+ "testable": not (model.get("disabled") or model.get("noAPI"))
97
+ and (
98
+ bool(provider.get("decideTest")) if model_id in deciders else can_test
99
+ ),
100
+ }
101
+ for model_id in provider.get("chosen") or ():
102
+ chips.setdefault(
103
+ model_id,
104
+ {"id": model_id, "name": model_id, "active": False, "testable": can_test},
105
+ )
106
+ hidden = set(provider.get("hiddenDrawers") or ())
107
+ for model_id in provider.get("drawIds") or ():
108
+ chips.setdefault(
109
+ model_id,
110
+ {
111
+ "id": model_id,
112
+ "name": (provider.get("drawNames") or {}).get(model_id, model_id),
113
+ "active": model_id not in hidden,
114
+ "testable": can_test,
115
+ "image": True,
116
+ },
117
+ )
118
+ return list(chips.values())
119
+
120
+
121
+ def test_models(models: list[Data], model: str = "") -> list[str]:
122
+ if model:
123
+ return [
124
+ entry["id"]
125
+ for entry in models
126
+ if entry["id"] == model and entry["testable"]
127
+ ]
128
+ active = [entry for entry in models if entry["active"] and entry["testable"]]
129
+ return [entry["id"] for entry in active if not entry.get("image")] or [
130
+ entry["id"] for entry in active
131
+ ]
132
+
133
+
134
+ def add_models(provider: Data, additions: list[str]) -> Data:
135
+ picks = provider.get("chosen") or []
136
+ if not picks and not provider.get("noModels"):
137
+ picks = [
138
+ model["id"] for model in provider.get("models") or () if model.get("on")
139
+ ]
140
+ selected = list(dict.fromkeys([*picks, *additions]))
141
+ if selected == picks:
142
+ return {}
143
+ return {
144
+ **{field: provider[field] for field in MODEL_SAVE_FIELDS if field in provider},
145
+ "models": selected,
146
+ }
147
+
148
+
149
+ def model_results(answer: Data, models: list[str]) -> list[Data]:
150
+ return [
151
+ {
152
+ "model": model,
153
+ "state": (
154
+ "queued"
155
+ if result.get("queued")
156
+ else "answered"
157
+ if result.get("ok")
158
+ else "failed"
159
+ ),
160
+ "status": result.get("status"),
161
+ "protocol": result.get("protocol"),
162
+ "ms": result.get("ms"),
163
+ }
164
+ for model, result in zip(models, answer["results"], strict=True)
165
+ ]
166
+
167
+
168
+ def command(section: str, action: str, form: FormData) -> tuple[str, Data]:
169
+ def value(name: str, *, required: bool = False) -> str:
170
+ text = str(form.get(name, "")).strip()
171
+ if required and not text:
172
+ raise ValueError
173
+ return text
174
+
175
+ def lines(name: str) -> list[str]:
176
+ return list(
177
+ dict.fromkeys(
178
+ text.strip() for text in value(name).splitlines() if text.strip()
179
+ )
180
+ )
181
+
182
+ if section == "gateway-keys":
183
+ if action == "add-key":
184
+ return f"gateway-keys/{action}", {
185
+ "name": value("name", required=True),
186
+ "readOnly": value("readOnly") == "1",
187
+ }
188
+ if action in (
189
+ "rename-key",
190
+ "rotate-key",
191
+ "on-key",
192
+ "off-key",
193
+ "remove-key",
194
+ "models-key",
195
+ ):
196
+ body: Data = {"key": value("id", required=True)}
197
+ if action == "rename-key":
198
+ body["name"] = value("name", required=True)
199
+ if action == "models-key":
200
+ body["models"] = lines("models")
201
+ return f"gateway-keys/{action}", body
202
+ elif section == "provider-keys":
203
+ if action in ("add", "rename", "replace", "on", "off", "remove"):
204
+ body = {"id": value("provider", required=True)}
205
+ if action != "add":
206
+ body["ref"] = value("id", required=True)
207
+ if action in ("rename", "add"):
208
+ body["name"] = value("name")
209
+ if action in ("add", "replace"):
210
+ body["key"] = value("key", required=True)
211
+ return f"keys/{action}", body
212
+ elif section == "account-groups":
213
+ if action in ("save", "remove"):
214
+ body = {"id": value("id", required=action == "remove")}
215
+ if action == "save":
216
+ body.update(
217
+ name=value("name", required=True), accounts=lines("accounts")
218
+ )
219
+ return f"account-groups/{action}", body
220
+ elif section == "routing-groups":
221
+ if action in ("create", "update", "switch", "delete"):
222
+ body = {"id": value("id", required=True)}
223
+ if action in ("create", "update"):
224
+ body.update(name=value("name", required=True), note=value("note"))
225
+ if action == "create" or value("members") != value("original_members"):
226
+ body["members"] = lines("members")
227
+ if action == "update":
228
+ body["from"] = value("original_id", required=True)
229
+ elif action == "switch":
230
+ body["on"] = value("on") == "1"
231
+ return f"routing-groups/{action}", body
232
+ elif section == "models":
233
+ body = {"id": value("provider", required=True)}
234
+ if action == "refresh":
235
+ return "provider/models", body
236
+ if action == "add-model":
237
+ typed = value("models", required=True).replace(",", "\n")
238
+ models = [model.strip() for model in typed.splitlines() if model.strip()]
239
+ if models:
240
+ return "provider/save", {**body, "models": models}
241
+ if action == "test-models":
242
+ return "provider/test", {**body, "model": value("model")}
243
+ raise ValueError
@@ -0,0 +1,69 @@
1
+ """Calls made with the selected gateway's one Key, on the Server only."""
2
+
3
+ from collections.abc import AsyncIterator, Mapping
4
+ from contextlib import asynccontextmanager
5
+ from typing import Any
6
+
7
+ from aiohttp import ClientError, ClientResponse, ClientSession
8
+
9
+ from .gateways import Connection
10
+
11
+ PREFIX = "/v1/magpie/"
12
+ SNAPSHOT = "route-events/snapshot"
13
+ EVENTS = "route-events"
14
+ USAGE = "usage"
15
+ REQUESTS = "usage/requests"
16
+ CONCURRENCY = "concurrency"
17
+ QUOTAS = "quotas"
18
+
19
+ type Data = dict[str, Any]
20
+
21
+
22
+ class Rejected(Exception):
23
+ def __init__(self, status: int, code: str = "") -> None:
24
+ super().__init__(status, code)
25
+ self.status = status
26
+ self.code = code
27
+
28
+
29
+ class Magpie:
30
+ def __init__(self, session: ClientSession) -> None:
31
+ self.session = session
32
+
33
+ @asynccontextmanager
34
+ async def request(
35
+ self,
36
+ connection: Connection,
37
+ path: str,
38
+ *,
39
+ params: Mapping[str, str | int] | None = None,
40
+ body: Data | None = None,
41
+ cursor: str = "",
42
+ ) -> AsyncIterator[ClientResponse]:
43
+ headers = {"Authorization": f"Bearer {connection.key}"}
44
+ if cursor:
45
+ headers["Last-Event-ID"] = cursor
46
+ try:
47
+ async with self.session.request(
48
+ "GET" if body is None else "POST",
49
+ connection.gateway.endpoint + PREFIX + path,
50
+ headers=headers,
51
+ params=params,
52
+ json=body,
53
+ allow_redirects=False,
54
+ ) as response:
55
+ if not 200 <= response.status < 300:
56
+ raise Rejected(response.status)
57
+ yield response
58
+ except (ClientError, ValueError, TimeoutError) as error:
59
+ raise Rejected(502) from error
60
+
61
+ async def read(
62
+ self, connection: Connection, path: str, **params: str | int
63
+ ) -> Data:
64
+ async with self.request(connection, path, params=params or None) as response:
65
+ return await response.json()
66
+
67
+ async def write(self, connection: Connection, path: str, body: Data) -> Data:
68
+ async with self.request(connection, "admin/" + path, body=body) as response:
69
+ return await response.json()