Skip to content

Free, open-source AI video generator for Mac. Run LTX-2.5 and MiniMax H3 (Hailuo) locally on Apple Silicon: text-to-video and image-to-video with sound, lip-sync, character LoRA training, songs from lyrics (YuE2) and image editing (Qwen-Image-Edit). One-click install via Pinokio. Offline: no cloud, no API key, no NVIDIA GPU.

fix(agent): alternate roles for Gemma + disable mlx-lm batching

mrbizarro06486527645c
diff --git a/agent/engine.py b/agent/engine.py
index b788f4a..7c01cb2 100644
--- a/agent/engine.py
+++ b/agent/engine.py
@@ -62,6 +62,36 @@ class ChatResult:
model: str = ""
+def _normalize_for_wire(messages: list[dict]) -> list[dict]:
+ """Collapse consecutive same-role messages into one for transport.
+
+ Gemma (and several other open chat models) ship with chat templates
+ that require strict user/assistant alternation. Our runtime stores
+ the raw conversation — tool results land as a user-role wrapper
+ after an assistant tool-call, and a new user turn after a tool-
+ result-tool-result-finish chain produces user→user adjacency. We
+ keep the raw shape on disk (the UI renders each tool result as its
+ own chip), and merge here so the wire form is always valid.
+
+ The system message is preserved as-is; only user/assistant runs are
+ coalesced. Joins are by double newline so the model can see the
+ boundary.
+ """
+ if not messages:
+ return messages
+ out: list[dict] = []
+ for m in messages:
+ if not out:
+ out.append(dict(m))
+ continue
+ prev = out[-1]
+ if prev.get("role") == m.get("role") and m.get("role") in ("user", "assistant"):
+ prev["content"] = (prev.get("content") or "") + "\n\n" + (m.get("content") or "")
+ else:
+ out.append(dict(m))
+ return out
+
+
def chat(messages: list[dict], config: EngineConfig,
*, timeout: int = DEFAULT_TIMEOUT_S) -> ChatResult:
"""Send a chat completion request. Returns the assistant text.
@@ -74,7 +104,7 @@ def chat(messages: list[dict], config: EngineConfig,
url = config.base_url.rstrip("/") + "/chat/completions"
body = {
"model": config.model,
- "messages": messages,
+ "messages": _normalize_for_wire(messages),
"temperature": config.temperature,
"max_tokens": config.max_tokens,
"stream": False,
diff --git a/agent/local_server.py b/agent/local_server.py
index cfb078a..0156250 100644
--- a/agent/local_server.py
+++ b/agent/local_server.py
@@ -78,12 +78,22 @@ def start(model_path: str, *, venv_python: str, log_sink=None) -> dict:
_LAST_ERROR = f"venv python not found: {venv_python}"
return _status_locked()
+ # Conservative concurrency. mlx-lm 0.31.1 has a known KV-cache merge
+ # bug in `_merge_caches` (BatchRotatingKVCache.merge) that fires
+ # under multi-prompt batching, manifesting as
+ # `ValueError: [broadcast_shapes] Shapes (1,8,N+1,256) and
+ # (1,8,N,256) cannot be broadcast.` mid-generation, hanging the
+ # request until the client times out. Setting prompt/decode
+ # concurrency to 1 takes that code path out of play. The agent
+ # is a single-user feature anyway — there's nothing to batch.
cmd = [
venv_python, "-m", "mlx_lm.server",
"--model", model_path,
"--host", "127.0.0.1",
"--port", str(_PORT),
"--log-level", "WARNING",
+ "--prompt-concurrency", "1",
+ "--decode-concurrency", "1",
]
env = os.environ.copy()
# mlx-lm honors HF_HOME for model cache lookup. The bundled