owui-deploy 0.1.2__py3-none-any.whl

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.
owui_deploy/config.py ADDED
@@ -0,0 +1,190 @@
1
+ """Configuration file (``owui-deploy.toml``) and the desired state built from it."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import tomllib
6
+ from dataclasses import dataclass, field
7
+ from pathlib import Path
8
+
9
+ from .errors import ConfigError
10
+ from .frontmatter import parse_frontmatter
11
+ from .i18n import t
12
+
13
+ DEFAULT_CONFIG_NAME = "owui-deploy.toml"
14
+ KINDS = ("tool", "function")
15
+ _TOP_KEYS = {"server", "paths", "defaults", "tools", "functions"}
16
+ _ITEM_KEYS = {"tools": {"name", "description", "active"}, "functions": {"name", "description", "active", "global"}}
17
+
18
+
19
+ @dataclass
20
+ class Config:
21
+ """Parsed configuration. Paths are absolute (relative to the config file)."""
22
+
23
+ path: Path | None
24
+ base_dir: Path
25
+ url: str | None = None
26
+ tools_dir: Path = Path("tools")
27
+ functions_dir: Path = Path("functions")
28
+ default_active: bool = True
29
+ default_global: bool = False
30
+ explicit_paths: set[str] = field(default_factory=set) # "tools"/"functions" keys set in [paths]
31
+ overrides: dict[str, dict[str, dict]] = field(default_factory=lambda: {"tools": {}, "functions": {}})
32
+ # lowercased id -> key exactly as written in the config (for messages)
33
+ written_keys: dict[str, dict[str, str]] = field(default_factory=lambda: {"tools": {}, "functions": {}})
34
+
35
+
36
+ @dataclass(frozen=True)
37
+ class DesiredItem:
38
+ """What one local file should look like on the server."""
39
+
40
+ kind: str # "tool" | "function"
41
+ id: str
42
+ path: Path
43
+ content: str
44
+ name: str
45
+ description: str
46
+ active: bool
47
+ is_global: bool # always False for tools
48
+
49
+
50
+ @dataclass(frozen=True)
51
+ class Skipped:
52
+ """A local file that cannot become a server item (bad id, id collision)."""
53
+
54
+ kind: str
55
+ path: Path
56
+ reason_key: str
57
+ params: dict
58
+
59
+
60
+ def _check_table(value: object, where: str) -> dict:
61
+ if not isinstance(value, dict):
62
+ raise ConfigError(t("config.not_table", where=where))
63
+ return value
64
+
65
+
66
+ def load_config(path: Path) -> Config:
67
+ """Read and validate the TOML file; raise ConfigError with a fix hint on any problem."""
68
+ if not path.is_file():
69
+ raise ConfigError(t("config.missing", path=str(path)))
70
+ try:
71
+ data = tomllib.loads(path.read_text(encoding="utf-8"))
72
+ except (tomllib.TOMLDecodeError, UnicodeDecodeError) as exc:
73
+ raise ConfigError(t("config.invalid_toml", path=str(path), detail=str(exc))) from exc
74
+
75
+ unknown = sorted(set(data) - _TOP_KEYS)
76
+ if unknown:
77
+ raise ConfigError(t("config.unknown_key", where=str(path), keys=", ".join(unknown)))
78
+
79
+ base = path.resolve().parent
80
+ cfg = Config(path=path, base_dir=base)
81
+ cfg.tools_dir = base / "tools"
82
+ cfg.functions_dir = base / "functions"
83
+
84
+ server = _check_table(data.get("server", {}), "[server]")
85
+ if "url" in server:
86
+ if not isinstance(server["url"], str):
87
+ raise ConfigError(t("config.bad_type", where="server.url", expected="string"))
88
+ cfg.url = server["url"]
89
+
90
+ paths = _check_table(data.get("paths", {}), "[paths]")
91
+ for key, attr in (("tools", "tools_dir"), ("functions", "functions_dir")):
92
+ if key in paths:
93
+ if not isinstance(paths[key], str):
94
+ raise ConfigError(t("config.bad_type", where=f"paths.{key}", expected="string"))
95
+ setattr(cfg, attr, (base / paths[key]))
96
+ cfg.explicit_paths.add(key)
97
+
98
+ defaults = _check_table(data.get("defaults", {}), "[defaults]")
99
+ for key, attr in (("active", "default_active"), ("global", "default_global")):
100
+ if key in defaults:
101
+ if not isinstance(defaults[key], bool):
102
+ raise ConfigError(t("config.bad_type", where=f"defaults.{key}", expected="true/false"))
103
+ setattr(cfg, attr, defaults[key])
104
+
105
+ for section in ("tools", "functions"):
106
+ table = _check_table(data.get(section, {}), f"[{section}]")
107
+ for item_id, values in table.items():
108
+ values = _check_table(values, f"[{section}.{item_id}]")
109
+ bad = sorted(set(values) - _ITEM_KEYS[section])
110
+ if bad:
111
+ raise ConfigError(t("config.unknown_key", where=f"[{section}.{item_id}]", keys=", ".join(bad)))
112
+ for key, value in values.items():
113
+ expected = str if key in ("name", "description") else bool
114
+ if not isinstance(value, expected):
115
+ kind_text = "string" if expected is str else "true/false"
116
+ raise ConfigError(t("config.bad_type", where=f"{section}.{item_id}.{key}", expected=kind_text))
117
+ norm_id = item_id.lower() # the server lowercases ids, so config keys must match
118
+ if norm_id in cfg.overrides[section]:
119
+ raise ConfigError(
120
+ t(
121
+ "config.duplicate_key",
122
+ where=f"[{section}]",
123
+ first=cfg.written_keys[section][norm_id],
124
+ second=item_id,
125
+ id=norm_id,
126
+ )
127
+ )
128
+ cfg.overrides[section][norm_id] = dict(values)
129
+ cfg.written_keys[section][norm_id] = item_id
130
+ return cfg
131
+
132
+
133
+ def default_config(base_dir: Path) -> Config:
134
+ """Configuration used when no file exists (``lint`` on a bare folder, ``pull`` into a new one)."""
135
+ return Config(path=None, base_dir=base_dir, tools_dir=base_dir / "tools", functions_dir=base_dir / "functions")
136
+
137
+
138
+ def derive_name(item_id: str, content: str, override: dict) -> str:
139
+ """name: config > frontmatter title > id."""
140
+ return override.get("name") or parse_frontmatter(content).get("title") or item_id
141
+
142
+
143
+ def derive_description(content: str, override: dict) -> str:
144
+ """description: config > frontmatter description > empty."""
145
+ return override.get("description") if "description" in override else parse_frontmatter(content).get("description", "")
146
+
147
+
148
+ def build_desired(cfg: Config) -> tuple[list[DesiredItem], list[Skipped]]:
149
+ """Read the local folders and return the desired items and the files that must be skipped."""
150
+ items: list[DesiredItem] = []
151
+ skipped: list[Skipped] = []
152
+ for kind, section, folder in (("tool", "tools", cfg.tools_dir), ("function", "functions", cfg.functions_dir)):
153
+ seen: dict[str, Path] = {}
154
+ if not folder.is_dir():
155
+ if section in cfg.explicit_paths: # a configured folder that does not exist is most likely a typo
156
+ skipped.append(Skipped(kind, folder, "skip.folder_missing", {"folder": str(folder)}))
157
+ for file in sorted(folder.glob("*.py") if folder.is_dir() else []):
158
+ if file.name.startswith("__"):
159
+ continue
160
+ stem = file.stem
161
+ if not stem.isidentifier():
162
+ skipped.append(Skipped(kind, file, "skip.bad_id", {"name": stem}))
163
+ continue
164
+ item_id = stem.lower() # the server lowercases ids
165
+ if item_id in seen:
166
+ skipped.append(Skipped(kind, file, "skip.duplicate", {"id": item_id, "other": seen[item_id].name}))
167
+ continue
168
+ seen[item_id] = file
169
+ try:
170
+ content = file.read_text(encoding="utf-8")
171
+ except UnicodeDecodeError as exc:
172
+ raise ConfigError(t("config.not_utf8", path=str(file))) from exc
173
+ override = cfg.overrides[section].get(item_id, {})
174
+ items.append(
175
+ DesiredItem(
176
+ kind=kind,
177
+ id=item_id,
178
+ path=file,
179
+ content=content,
180
+ name=derive_name(item_id, content, override),
181
+ description=derive_description(content, override),
182
+ active=override.get("active", cfg.default_active),
183
+ is_global=False if kind == "tool" else override.get("global", cfg.default_global),
184
+ )
185
+ )
186
+ stale = set(cfg.overrides[section]) - set(seen)
187
+ for item_id in sorted(stale):
188
+ written = cfg.written_keys[section].get(item_id, item_id)
189
+ skipped.append(Skipped(kind, folder / f"{item_id}.py", "skip.config_without_file", {"id": written}))
190
+ return items, skipped
owui_deploy/errors.py ADDED
@@ -0,0 +1,41 @@
1
+ """Exceptions that map to exit code 2 ("tool error") in the CLI."""
2
+
3
+ from __future__ import annotations
4
+
5
+
6
+ class ToolError(Exception):
7
+ """A problem with the tool setup, not with the user's files (exit code 2)."""
8
+
9
+ def __init__(self, message: str) -> None:
10
+ super().__init__(message)
11
+ self.message = message
12
+
13
+
14
+ class ConfigError(ToolError):
15
+ """The configuration file is missing or invalid."""
16
+
17
+
18
+ class OwuiConnectionError(ToolError):
19
+ """The server cannot be reached."""
20
+
21
+
22
+ class AuthError(ToolError):
23
+ """The server answered 401 or 403."""
24
+
25
+ def __init__(self, message: str, status: int) -> None:
26
+ super().__init__(message)
27
+ self.status = status
28
+
29
+
30
+ class ApiError(Exception):
31
+ """The server rejected one request (HTTP 4xx/5xx other than 401/403).
32
+
33
+ This is *not* a ToolError: the apply step collects these per item and keeps going.
34
+ """
35
+
36
+ def __init__(self, status: int, detail: str, method: str = "", path: str = "") -> None:
37
+ super().__init__(f"HTTP {status}: {detail}")
38
+ self.status = status
39
+ self.detail = detail
40
+ self.method = method
41
+ self.path = path
owui_deploy/fake.py ADDED
@@ -0,0 +1,353 @@
1
+ """A small practice mock of Open WebUI 0.13.3 (NOT a real Open WebUI).
2
+
3
+ It models the behaviours that matter for deploying Tools and Functions, each one
4
+ recorded from a real 0.13.3 instance (see ``tests/fixtures/owui-0.13.3``):
5
+
6
+ * the import rewrite on save (``from utils`` -> ``from open_webui.utils``)
7
+ * toggles flip the current value (they do not set it)
8
+ * a new Function is created inactive and non-global; a new Tool is active
9
+ * a file without a Pipe/Filter/Action/Event (Tools: ``Tools``) is answered with the
10
+ one-line 400 ``[ERROR: Error creating function]``
11
+ * bad id / duplicate id are 400
12
+ * ``POST /functions/sync`` deletes every Function that is not in the list
13
+ * ``export?include_valves=true`` returns valves in plain text
14
+
15
+ Difference from the real server: the code is never executed. Classes are detected with ``ast``.
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ import ast
21
+ import json
22
+ import uuid
23
+ from pathlib import Path
24
+ from urllib.parse import unquote
25
+
26
+ import httpx
27
+
28
+ from .frontmatter import parse_frontmatter
29
+ from .normalize import normalize
30
+
31
+ FUNCTION_CLASSES = ("Pipe", "Filter", "Action", "Event")
32
+ FAKE_VERSION = "0.13.3"
33
+ _NOW = 1794477106 # fixed clock keeps the mock deterministic
34
+
35
+ # Response key sets per (kind, operation), copied from the recorded 0.13.3 responses.
36
+ _BASE_FUNCTION = ["version_id", "id", "user_id", "type", "name", "meta", "is_active", "is_global", "updated_at", "created_at"]
37
+ _BASE_TOOL = ["version_id", "id", "user_id", "name", "meta", "is_active", "access_grants", "updated_at", "created_at"]
38
+ KEYS = {
39
+ ("function", "list"): _BASE_FUNCTION,
40
+ ("function", "create"): _BASE_FUNCTION,
41
+ ("function", "get"): [*_BASE_FUNCTION, "content"],
42
+ ("function", "toggle"): [*_BASE_FUNCTION, "content"],
43
+ ("function", "update"): [*_BASE_FUNCTION, "content"],
44
+ ("function", "export"): [*_BASE_FUNCTION, "content"],
45
+ ("function", "export_valves"): [*_BASE_FUNCTION, "content", "valves"],
46
+ ("function", "sync"): [*_BASE_FUNCTION, "content", "valves"],
47
+ ("tool", "list"): _BASE_TOOL,
48
+ ("tool", "create"): _BASE_TOOL,
49
+ ("tool", "toggle"): _BASE_TOOL,
50
+ ("tool", "get"): [*_BASE_TOOL, "content", "specs", "user", "write_access"],
51
+ ("tool", "update"): [*_BASE_TOOL, "content", "specs"],
52
+ ("tool", "export"): [*_BASE_TOOL, "content", "specs"],
53
+ }
54
+
55
+ # Seed used by ``--fake`` when no state file exists yet. Sources are created through the
56
+ # normal create path, so the import rewrite etc. apply to them as on a real server.
57
+ SEED_TOOL_CLOCK = '''"""
58
+ title: Clock
59
+ version: 0.1.0
60
+ """
61
+ from datetime import datetime
62
+
63
+
64
+ class Tools:
65
+ def now(self) -> str:
66
+ """Return the current time."""
67
+ return datetime.now().isoformat()
68
+ '''
69
+
70
+ SEED_PII_MASK = '''"""
71
+ title: PII Mask
72
+ version: 0.1.0
73
+ description: Mask e-mail addresses before the model sees them
74
+ """
75
+ import re
76
+
77
+ from pydantic import BaseModel
78
+ from utils.misc import get_last_user_message
79
+
80
+
81
+ class Filter:
82
+ class Valves(BaseModel):
83
+ api_key: str = ""
84
+ priority: int = 0
85
+
86
+ def __init__(self):
87
+ self.valves = self.Valves()
88
+
89
+ def inlet(self, body: dict, __user__: dict | None = None) -> dict:
90
+ message = get_last_user_message(body["messages"])
91
+ if message:
92
+ body["messages"][-1]["content"] = re.sub(r"[\\w.]+@[\\w.]+", "[email]", message)
93
+ return body
94
+ '''
95
+
96
+ SEED_LEGACY_FILTER = '''"""
97
+ title: Legacy Filter
98
+ version: 0.0.1
99
+ """
100
+
101
+
102
+ class Filter:
103
+ def inlet(self, body: dict) -> dict:
104
+ return body
105
+ '''
106
+
107
+
108
+ class FakeOpenWebUI:
109
+ """In-memory (optionally JSON-persisted) server model with an httpx MockTransport adapter."""
110
+
111
+ def __init__(self, state_path: Path | None = None, token: str | None = None) -> None:
112
+ self.state_path = state_path
113
+ self.token = token # None = accept any bearer token
114
+ self.log: list[tuple[str, str]] = []
115
+ self.bodies: list[tuple[str, str, dict]] = [] # JSON bodies of write requests (for tests)
116
+ self.items: dict[str, dict[str, dict]] = {"tool": {}, "function": {}}
117
+ self.valves: dict[str, dict] = {}
118
+ self.history: list[dict] = []
119
+ if state_path is not None and state_path.is_file():
120
+ self._load()
121
+ else:
122
+ self._seed()
123
+
124
+ # -- persistence -------------------------------------------------------------------------
125
+ def _load(self) -> None:
126
+ assert self.state_path is not None
127
+ data = json.loads(self.state_path.read_text(encoding="utf-8"))
128
+ self.items = data["items"]
129
+ self.valves = data.get("valves", {})
130
+ self.history = data.get("history", [])
131
+
132
+ def save(self) -> None:
133
+ if self.state_path is None:
134
+ return
135
+ payload = {"items": self.items, "valves": self.valves, "history": self.history}
136
+ self.state_path.write_text(json.dumps(payload, ensure_ascii=False, indent=1), encoding="utf-8")
137
+
138
+ def _seed(self) -> None:
139
+ self._create("tool", {"id": "clock", "name": "Clock", "content": SEED_TOOL_CLOCK, "meta": {"description": "Clock"}})
140
+ function = self._create(
141
+ "function",
142
+ {"id": "pii_mask", "name": "PII Mask", "content": SEED_PII_MASK, "meta": {"description": "Mask e-mail addresses before the model sees them"}},
143
+ )
144
+ self.valves["pii_mask"] = {"api_key": "dummy-not-secret", "priority": 3}
145
+ assert function["is_active"] is False
146
+ self._create("function", {"id": "legacy_filter", "name": "Legacy Filter", "content": SEED_LEGACY_FILTER, "meta": {"description": ""}})
147
+ self.items["function"]["legacy_filter"]["is_active"] = True
148
+ self.log.clear()
149
+
150
+ # -- transport ---------------------------------------------------------------------------
151
+ def transport(self) -> httpx.MockTransport:
152
+ return httpx.MockTransport(self.handle)
153
+
154
+ def writes(self) -> list[tuple[str, str]]:
155
+ """Logged write requests (everything except GET)."""
156
+ return [entry for entry in self.log if entry[0] != "GET"]
157
+
158
+ # -- domain helpers ----------------------------------------------------------------------
159
+ @staticmethod
160
+ def _detect_type(kind: str, content: str) -> str | None:
161
+ try:
162
+ tree = ast.parse(content)
163
+ except SyntaxError:
164
+ return None
165
+ names = set()
166
+ for node in tree.body:
167
+ if isinstance(node, ast.ClassDef):
168
+ names.add(node.name)
169
+ elif isinstance(node, (ast.Import, ast.ImportFrom)):
170
+ names.update((a.asname or a.name).split(".")[0] for a in node.names)
171
+ elif isinstance(node, ast.Assign):
172
+ names.update(t.id for t in node.targets if isinstance(t, ast.Name))
173
+ if kind == "tool":
174
+ return "tool" if "Tools" in names else None
175
+ for candidate in FUNCTION_CLASSES:
176
+ if candidate in names:
177
+ return candidate.lower()
178
+ return None
179
+
180
+ @staticmethod
181
+ def _specs(content: str) -> list[dict]:
182
+ specs: list[dict] = []
183
+ try:
184
+ tree = ast.parse(content)
185
+ except SyntaxError:
186
+ return specs
187
+ for node in tree.body:
188
+ if isinstance(node, ast.ClassDef) and node.name == "Tools":
189
+ for member in node.body:
190
+ if isinstance(member, (ast.FunctionDef, ast.AsyncFunctionDef)) and not member.name.startswith("_"):
191
+ specs.append(
192
+ {
193
+ "name": member.name,
194
+ "description": ast.get_docstring(member) or "",
195
+ "parameters": {"properties": {}, "type": "object"},
196
+ }
197
+ )
198
+ return specs
199
+
200
+ def _new_record(self, kind: str, form: dict, content: str, ftype: str | None) -> dict:
201
+ meta_in = form.get("meta") or {}
202
+ manifest = parse_frontmatter(content)
203
+ record = {
204
+ "version_id": str(uuid.uuid4()),
205
+ "id": form["id"],
206
+ "user_id": "REDACTED-user-id",
207
+ "name": form.get("name", form["id"]),
208
+ "content": content,
209
+ "created_at": _NOW,
210
+ "updated_at": _NOW,
211
+ }
212
+ if kind == "tool":
213
+ record.update(
214
+ is_active=True,
215
+ access_grants=[],
216
+ meta={"i18n": None, "description": meta_in.get("description", ""), "manifest": manifest, "has_user_valves": False},
217
+ )
218
+ else:
219
+ record.update(
220
+ type=ftype,
221
+ is_active=False,
222
+ is_global=False,
223
+ meta={"description": meta_in.get("description", ""), "manifest": manifest, "toggle": False},
224
+ )
225
+ return record
226
+
227
+ def _create(self, kind: str, form: dict) -> dict:
228
+ item_id = form.get("id", "")
229
+ if not isinstance(item_id, str) or not item_id.isidentifier():
230
+ raise _Reject(400, "Only alphanumeric characters and underscores are allowed in the id")
231
+ item_id = item_id.lower()
232
+ form = {**form, "id": item_id}
233
+ if item_id in self.items[kind]:
234
+ raise _Reject(400, "Uh-oh! This id is already registered. Please choose another id string.")
235
+ content = normalize(form.get("content", ""))
236
+ ftype = self._detect_type(kind, content)
237
+ if ftype is None:
238
+ raise _Reject(400, f"[ERROR: Error creating {kind}]")
239
+ record = self._new_record(kind, form, content, ftype)
240
+ self.items[kind][item_id] = record
241
+ self._add_history(kind, item_id, form.get("commit_message"))
242
+ return record
243
+
244
+ def _update(self, kind: str, item_id: str, form: dict) -> dict:
245
+ record = self.items[kind].get(item_id)
246
+ if record is None:
247
+ raise _Reject(404, "Not found")
248
+ content = normalize(form.get("content", ""))
249
+ ftype = self._detect_type(kind, content)
250
+ if ftype is None:
251
+ raise _Reject(400, f"[ERROR: Error updating {kind}]")
252
+ record["content"] = content
253
+ record["name"] = form.get("name", record["name"])
254
+ record["meta"]["description"] = (form.get("meta") or {}).get("description", "")
255
+ record["meta"]["manifest"] = parse_frontmatter(content)
256
+ if kind == "function":
257
+ record["type"] = ftype
258
+ record["version_id"] = str(uuid.uuid4())
259
+ self._add_history(kind, item_id, form.get("commit_message"))
260
+ return record
261
+
262
+ def _add_history(self, kind: str, item_id: str, message: str | None) -> None:
263
+ self.history.append({"kind": kind, "id": item_id, "commit_message": message})
264
+
265
+ def _shape(self, kind: str, op: str, record: dict) -> dict:
266
+ full = dict(record)
267
+ full.setdefault("specs", self._specs(record["content"]) if kind == "tool" else None)
268
+ full.setdefault("user", None)
269
+ full.setdefault("write_access", True)
270
+ full["valves"] = self.valves.get(record["id"], {})
271
+ return {key: full[key] for key in KEYS[(kind, op)] if key in full}
272
+
273
+ # -- HTTP --------------------------------------------------------------------------------
274
+ def handle(self, request: httpx.Request) -> httpx.Response:
275
+ path = request.url.path
276
+ self.log.append((request.method, path))
277
+ if request.method != "GET" and request.content:
278
+ self.bodies.append((request.method, path, json.loads(request.content)))
279
+ if self.token is not None and request.headers.get("authorization") != f"Bearer {self.token}":
280
+ return _json(401, {"detail": "Not authenticated"})
281
+ try:
282
+ response = self._route(request.method, path, request)
283
+ if request.method != "GET":
284
+ self.save()
285
+ return response
286
+ except _Reject as exc:
287
+ return _json(exc.status, {"detail": exc.detail})
288
+
289
+ def _route(self, method: str, path: str, request: httpx.Request) -> httpx.Response:
290
+ if method == "GET" and path == "/api/version":
291
+ return _json(200, {"version": FAKE_VERSION})
292
+ parts = [unquote(p) for p in path.strip("/").split("/")]
293
+ if len(parts) < 3 or parts[:2] != ["api", "v1"] or parts[2] not in ("tools", "functions"):
294
+ return _json(404, {"detail": "Not Found"})
295
+ kind = "tool" if parts[2] == "tools" else "function"
296
+ rest = parts[3:]
297
+ body = json.loads(request.content) if request.content else {}
298
+
299
+ if method == "GET" and rest == []:
300
+ return _json(200, [self._shape(kind, "list", r) for r in self.items[kind].values()])
301
+ if method == "GET" and rest == ["export"]:
302
+ with_valves = kind == "function" and request.url.params.get("include_valves") == "true"
303
+ op = "export_valves" if with_valves else "export"
304
+ return _json(200, [self._shape(kind, op, r) for r in self.items[kind].values()])
305
+ if method == "POST" and rest == ["create"]:
306
+ return _json(200, self._shape(kind, "create", self._create(kind, body)))
307
+ if method == "POST" and rest == ["sync"] and kind == "function":
308
+ keep = {f["id"] for f in body.get("functions", [])}
309
+ for item_id in list(self.items["function"]):
310
+ if item_id not in keep:
311
+ del self.items["function"][item_id]
312
+ for form in body.get("functions", []):
313
+ if form["id"] in self.items["function"]:
314
+ self._update("function", form["id"], form)
315
+ else:
316
+ self._create("function", form)
317
+ return _json(200, [self._shape("function", "sync", r) for r in self.items["function"].values()])
318
+ if len(rest) >= 2 and rest[0] == "id":
319
+ item_id = rest[1]
320
+ action = rest[2:]
321
+ record = self.items[kind].get(item_id)
322
+ if record is None:
323
+ return _json(404, {"detail": "Not found"})
324
+ if method == "GET" and action == []:
325
+ return _json(200, self._shape(kind, "get", record))
326
+ if method == "POST" and action == ["update"]:
327
+ return _json(200, self._shape(kind, "update", self._update(kind, item_id, body)))
328
+ if method == "POST" and action == ["toggle"]:
329
+ record["is_active"] = not record["is_active"]
330
+ return _json(200, self._shape(kind, "toggle", record))
331
+ if method == "POST" and action == ["toggle", "global"] and kind == "function":
332
+ record["is_global"] = not record["is_global"]
333
+ return _json(200, self._shape(kind, "toggle", record))
334
+ if method == "DELETE" and action == ["delete"]:
335
+ del self.items[kind][item_id]
336
+ self.valves.pop(item_id, None)
337
+ return _json(200, True)
338
+ if action in (["valves"], ["valves", "update"]):
339
+ if method == "POST":
340
+ self.valves[item_id] = body
341
+ return _json(200, self.valves.get(item_id, {}))
342
+ return _json(404, {"detail": "Not Found"})
343
+
344
+
345
+ class _Reject(Exception):
346
+ def __init__(self, status: int, detail: str) -> None:
347
+ super().__init__(detail)
348
+ self.status = status
349
+ self.detail = detail
350
+
351
+
352
+ def _json(status: int, body: object) -> httpx.Response:
353
+ return httpx.Response(status, json=body)
@@ -0,0 +1,28 @@
1
+ """Frontmatter parser with the same rules as Open WebUI 0.13.3 (`utils/plugin.py`)."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+
7
+ _PATTERN = re.compile(r"^\s*([a-z_]+):\s*(.*)\s*$", re.IGNORECASE)
8
+
9
+
10
+ def parse_frontmatter(content: str) -> dict[str, str]:
11
+ """Return the ``key: value`` pairs of the leading docstring.
12
+
13
+ The first line must be ``\"\"\"`` (surrounding spaces allowed); otherwise the
14
+ result is empty, exactly like the server. Parsing stops at the first line
15
+ that contains ``\"\"\"``.
16
+ """
17
+ lines = content.splitlines()
18
+ if len(lines) < 1 or lines[0].strip() != '"""':
19
+ return {}
20
+ result: dict[str, str] = {}
21
+ for line in lines[1:]:
22
+ if '"""' in line:
23
+ break
24
+ match = _PATTERN.match(line)
25
+ if match:
26
+ key, value = match.groups()
27
+ result[key.strip()] = value.strip()
28
+ return result
owui_deploy/i18n.py ADDED
@@ -0,0 +1,60 @@
1
+ """Language selection and message lookup (English / Korean).
2
+
3
+ Priority: ``--lang`` > ``OWUI_DEPLOY_LANG`` > ``LC_ALL`` > ``LC_MESSAGES`` > ``LANG``.
4
+ Anything that does not start with ``ko`` falls back to English.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import json
10
+ from functools import lru_cache
11
+ from importlib import resources
12
+ from typing import Mapping
13
+
14
+ SUPPORTED = ("en", "ko")
15
+ _current = "en"
16
+
17
+
18
+ def _normalize(value: str | None) -> str | None:
19
+ if not value:
20
+ return None
21
+ return "ko" if value.strip().lower().startswith("ko") else "en"
22
+
23
+
24
+ def resolve_lang(cli_value: str | None, env: Mapping[str, str]) -> str:
25
+ """Pick the output language following the documented priority."""
26
+ for candidate in (
27
+ cli_value,
28
+ env.get("OWUI_DEPLOY_LANG"),
29
+ env.get("LC_ALL"),
30
+ env.get("LC_MESSAGES"),
31
+ env.get("LANG"),
32
+ ):
33
+ picked = _normalize(candidate)
34
+ if picked:
35
+ return picked
36
+ return "en"
37
+
38
+
39
+ def set_lang(lang: str) -> None:
40
+ global _current
41
+ _current = lang if lang in SUPPORTED else "en"
42
+
43
+
44
+ def get_lang() -> str:
45
+ return _current
46
+
47
+
48
+ @lru_cache(maxsize=None)
49
+ def load_catalog(lang: str) -> dict[str, str]:
50
+ """Load ``locales/<lang>.json`` (the message table, one place for all text)."""
51
+ path = resources.files("owui_deploy") / "locales" / f"{lang}.json"
52
+ return json.loads(path.read_text(encoding="utf-8"))
53
+
54
+
55
+ def t(key: str, **params: object) -> str:
56
+ """Translate ``key`` into the current language (falls back to English, then the key)."""
57
+ text = load_catalog(_current).get(key)
58
+ if text is None:
59
+ text = load_catalog("en").get(key, key)
60
+ return text.format(**params) if params else text