aboutsummaryrefslogtreecommitdiff
path: root/app/backends/common.py
diff options
context:
space:
mode:
Diffstat (limited to 'app/backends/common.py')
-rw-r--r--app/backends/common.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/app/backends/common.py b/app/backends/common.py
index 25d4f31..b9448e0 100644
--- a/app/backends/common.py
+++ b/app/backends/common.py
@@ -230,6 +230,28 @@ def normalize_remote_url(value: str) -> str:
(parts.scheme or "http", parts.netloc, parts.path, "", ""))
+def parse_request_options(text: str) -> Dict[str, str]:
+ """Parse a user-supplied ``KEY=VALUE`` option string into a dict.
+
+ Items are separated by commas or whitespace; each must contain an
+ ``=`` with a non-empty key. Values are kept verbatim (only the key is
+ stripped), so e.g. ``speed=1.1`` yields ``{"speed": "1.1"}`` — the
+ audio.cpp server coerces per-model option values itself. A blank
+ string yields {}. Raises ValueError with a user-facing message when
+ an item lacks ``=`` or has an empty key; later duplicates of a key
+ override earlier ones.
+ """
+ options: Dict[str, str] = {}
+ for item in text.replace(",", " ").split():
+ key, sep, value = item.partition("=")
+ if not sep or not key.strip():
+ raise ValueError(
+ f"Request options expect KEY=VALUE items "
+ f"(e.g. emotion=neutral); got {item!r}")
+ options[key.strip()] = value
+ return options
+
+
def server_running(url: str, timeout: float = 0.3) -> bool:
"""True when something accepts TCP connections at URL's host:port.