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.

feat(agent): native Claude (Anthropic) engine kind

mrbizarroea263076d905
diff --git a/agent/engine.py b/agent/engine.py
index 3c63fbd..2bca730 100644
--- a/agent/engine.py
+++ b/agent/engine.py
@@ -32,7 +32,7 @@ class EngineConfig:
promises a panel-spawned mlx-lm.server on `base_url`.
"""
- kind: str = "phosphene_local" # "phosphene_local" | "ollama" | "custom"
+ kind: str = "phosphene_local" # "phosphene_local" | "ollama" | "custom" | "anthropic"
base_url: str = "http://127.0.0.1:8200/v1"
model: str = "mlx-community/gemma-3-12b-it-4bit"
api_key: str = ""
@@ -42,6 +42,9 @@ class EngineConfig:
# `local_model_path` may be a Hugging Face id ("Qwen/Qwen3-30B-A3B-Instruct-4bit")
# OR an absolute path to a local model dir (the bundled Gemma path).
local_model_path: str = ""
+ # Anthropic-only: API version pinned in the `anthropic-version` header.
+ # Anthropic adds breaking changes per dated version; pin to a known-good.
+ anthropic_version: str = "2023-06-01"
# Operating mode for the agent.
# "plan_sleep" — agent plans, queues all shots, calls finish; the panel
# auto-stops the local engine the moment finish lands so
@@ -111,6 +114,9 @@ def chat(messages: list[dict], config: EngineConfig,
sees what the engine actually said (helpful for debugging API key
/ model name typos in the Settings drawer).
"""
+ if config.kind == "anthropic":
+ return _chat_anthropic(messages, config, timeout=timeout)
+
url = config.base_url.rstrip("/") + "/chat/completions"
# mlx-lm.server identifies models by their LOAD PATH, not by short
# name. Its /v1/models endpoint returns the absolute path (e.g.
@@ -183,6 +189,94 @@ def chat(messages: list[dict], config: EngineConfig,
model=data.get("model", config.model))
+def _chat_anthropic(messages: list[dict], config: EngineConfig,
+ *, timeout: int) -> ChatResult:
+ """Anthropic Messages API — `/v1/messages` shape.
+
+ Differences from the OpenAI shape we already handle:
+ - System prompt is a top-level `system` STRING, not a {role:"system"}
+ item in `messages`. We pop it out (assumes only one — see
+ _normalize_for_wire which never adds a second).
+ - `messages` must strictly alternate user/assistant (same as Gemma).
+ _normalize_for_wire already collapses adjacent roles so this works.
+ - Auth is via `x-api-key` header, NOT `Authorization: Bearer`.
+ - Response is `{content: [{type:"text", text:"..."}], stop_reason}`
+ — no `choices` array. Concatenate text blocks for the result.
+ - `max_tokens` is REQUIRED.
+ """
+ base = config.base_url.rstrip("/") or "https://api.anthropic.com/v1"
+ url = base + "/messages"
+ # Normalize first (collapse adjacent same-role messages so the
+ # Anthropic "strict alternation" rule passes), THEN extract the
+ # leading system message.
+ norm = _normalize_for_wire(messages)
+ system_text = ""
+ if norm and norm[0].get("role") == "system":
+ system_text = (norm[0].get("content") or "").strip()
+ norm = norm[1:]
+ body: dict = {
+ "model": config.model or "claude-sonnet-4-5",
+ "messages": norm,
+ "max_tokens": config.max_tokens,
+ "temperature": config.temperature,
+ }
+ if system_text:
+ body["system"] = system_text
+
+ headers = {
+ "Content-Type": "application/json",
+ "anthropic-version": config.anthropic_version or "2023-06-01",
+ }
+ if config.api_key:
+ headers["x-api-key"] = config.api_key
+
+ req = urllib.request.Request(
+ url, data=json.dumps(body).encode("utf-8"), headers=headers, method="POST",
+ )
+ try:
+ with urllib.request.urlopen(req, timeout=timeout) as resp:
+ raw = resp.read()
+ except urllib.error.HTTPError as e:
+ detail = ""
+ try:
+ detail = e.read().decode("utf-8", errors="replace")
+ except Exception:
+ pass
+ # Anthropic ships errors as {"type":"error","error":{"type","message"}}.
+ nice = detail
+ try:
+ j = json.loads(detail)
+ if isinstance(j, dict) and isinstance(j.get("error"), dict):
+ err = j["error"]
+ nice = f"{err.get('type','?')}: {err.get('message','?')}"
+ except Exception:
+ pass
+ raise RuntimeError(
+ f"Anthropic API returned HTTP {e.code} ({e.reason}). "
+ f"Detail: {nice[:600]}"
+ ) from e
+ except urllib.error.URLError as e:
+ raise RuntimeError(
+ f"Anthropic API unreachable at {url}: {e.reason}."
+ ) from e
+
+ try:
+ data = json.loads(raw)
+ except json.JSONDecodeError as e:
+ raise RuntimeError(
+ f"Anthropic returned non-JSON ({len(raw)} bytes). "
+ f"First 200 chars: {raw[:200]!r}"
+ ) from e
+
+ blocks = data.get("content") or []
+ text_parts = [b.get("text") or "" for b in blocks if b.get("type") == "text"]
+ content = "".join(text_parts)
+ finish = data.get("stop_reason") or "end_turn"
+ usage = data.get("usage") or {}
+ return ChatResult(content=content, finish_reason=finish, usage=usage,
+ model=data.get("model", config.model))
+
+
def health_check(config: EngineConfig, *, timeout: int = 5) -> tuple[bool, str]:
"""Light probe of the engine. Returns (ok, message).
@@ -190,9 +284,17 @@ def health_check(config: EngineConfig, *, timeout: int = 5) -> tuple[bool, str]:
all expose the same. We don't insist on a specific shape — any 200
response means the endpoint is alive and accepts auth (if provided).
"""
- url = config.base_url.rstrip("/") + "/models"
+ base = config.base_url.rstrip("/") or (
+ "https://api.anthropic.com/v1" if config.kind == "anthropic" else ""
+ )
+ url = base + "/models"
headers = {}
- if config.api_key:
+ if config.kind == "anthropic":
+ # Anthropic auth is x-api-key + anthropic-version, not Bearer.
+ headers["anthropic-version"] = config.anthropic_version or "2023-06-01"
+ if config.api_key:
+ headers["x-api-key"] = config.api_key
+ elif config.api_key:
headers["Authorization"] = f"Bearer {config.api_key}"
req = urllib.request.Request(url, headers=headers)
try:
diff --git a/mlx_ltx_panel.py b/mlx_ltx_panel.py
index 68655e0..cd8658f 100644
--- a/mlx_ltx_panel.py
+++ b/mlx_ltx_panel.py
@@ -10438,8 +10438,9 @@ HTML = r"""<!doctype html>
<label>Engine kind</label>
<select id="agentKind" onchange="agentKindChanged()">
<option value="phosphene_local">Phosphene Local — bundled mlx-lm.server</option>
+ <option value="anthropic">Claude (Anthropic) — claude.ai API key, billed per token</option>
<option value="ollama">Ollama bridge — talks to your `ollama serve` on port 11434</option>
- <option value="custom">Custom — any OpenAI-compatible URL (Anthropic compat, OpenAI, OpenRouter, LM Studio…)</option>
+ <option value="custom">Custom — any OpenAI-compatible URL (OpenRouter, LM Studio…)</option>
</select>
</div>
@@ -10465,19 +10466,42 @@ HTML = r"""<!doctype html>
</div>
</div>
+ <!-- Anthropic: shown only when engine kind == 'anthropic'. Curated
+ model dropdown (covers the common picks) plus a free-form
+ override field for whatever Anthropic ships next. The base URL
+ is implicit (https://api.anthropic.com/v1) so we don't expose
+ it in the UI. The api_key field below this is shared with the
+ custom-OpenAI branch — same input, different transport. -->
+ <div class="field" id="agentAnthropicModelField" style="display:none">
+ <label>Claude model</label>
+ <select id="agentAnthropicModel" onchange="agentAnthropicModelChanged()">
+ <option value="claude-sonnet-4-5">Sonnet 4.5 — balanced reasoning, recommended</option>
+ <option value="claude-opus-4-5">Opus 4.5 — strongest reasoning, slower + pricier</option>
+ <option value="claude-haiku-4-5">Haiku 4.5 — fastest, cheapest, lighter reasoning</option>
+ <option value="__custom__">Custom model id…</option>
+ </select>
+ <input type="text" id="agentAnthropicCustomModel"
+ placeholder="claude-…-YYYYMMDD"
+ style="margin-top:8px; display:none">
+ <div style="margin-top:6px;font-size:11px;color:var(--muted)">
+ API charges per token. Get a key at <code>console.anthropic.com</code>. Phosphene
+ sends your key directly to <code>api.anthropic.com</code> — never to a third party.
+ </div>
+ </div>
+
<div class="field" id="agentBaseUrlField" style="display:none">
<label>Base URL</label>
<input type="text" id="agentBaseUrl" placeholder="https://api.openai.com/v1">
</div>
<div class="field" id="agentApiKeyField" style="display:none">
- <label>API key (saved locally; only sent as Authorization header)</label>
+ <label>API key (saved locally; sent only to the configured endpoint)</label>
<input type="password" id="agentApiKey" placeholder="(leave blank to keep saved key)">
</div>
<div class="field" id="agentRemoteModelField" style="display:none">
<label>Model identifier</label>
- <input type="text" id="agentRemoteModel" placeholder="e.g. claude-sonnet-4-6 or gpt-5">
+ <input type="text" id="agentRemoteModel" placeholder="e.g. gpt-5 or qwen2.5-coder:32b">
</div>
<div class="row">
@@ -15158,6 +15182,25 @@ function openAgentSettings() {
document.getElementById('agentApiKey').value = '';
document.getElementById('agentApiKey').placeholder =
cfg.has_api_key ? '(saved key — leave blank to keep)' : 'Paste API key';
+ // Anthropic: pick the curated dropdown if cfg.model matches, else fall
+ // through to the custom-id input so power users can pick e.g. a
+ // dated alias Anthropic shipped after this UI was last edited.
+ const anthropicModels = ['claude-sonnet-4-5', 'claude-opus-4-5', 'claude-haiku-4-5'];
+ if (cfg.kind === 'anthropic') {
+ if (anthropicModels.includes(cfg.model || '')) {
+ document.getElementById('agentAnthropicModel').value = cfg.model;
+ document.getElementById('agentAnthropicCustomModel').value = '';
+ document.getElementById('agentAnthropicCustomModel').style.display = 'none';
+ } else {
+ document.getElementById('agentAnthropicModel').value = '__custom__';
+ document.getElementById('agentAnthropicCustomModel').value = cfg.model || '';
+ document.getElementById('agentAnthropicCustomModel').style.display = '';
+ }
+ } else {
+ document.getElementById('agentAnthropicModel').value = 'claude-sonnet-4-5';
+ document.getElementById('agentAnthropicCustomModel').value = '';
+ document.getElementById('agentAnthropicCustomModel').style.display = 'none';
+ }
document.getElementById('agentTemp').value = cfg.temperature ?? 0.4;
document.getElementById('agentMaxTokens').value = cfg.max_tokens ?? 3072;
@@ -15269,12 +15312,30 @@ function agentKindChanged() {
document.getElementById('agentLocalModelField').style.display = kind === 'phosphene_local' ? '' : 'none';
document.getElementById('agentLocalRow').style.display = kind === 'phosphene_local' ? '' : 'none';
document.getElementById('agentOllamaField').style.display = kind === 'ollama' ? '' : 'none';
+ document.getElementById('agentAnthropicModelField').style.display = kind === 'anthropic' ? '' : 'none';
+ // Base URL is implicit for Anthropic — hide the field.
document.getElementById('agentBaseUrlField').style.display = kind === 'custom' ? '' : 'none';
- document.getElementById('agentApiKeyField').style.display = kind === 'custom' ? '' : 'none';
+ // API key field is shared between Anthropic + custom (transport differs:
+ // Anthropic uses x-api-key, custom uses Authorization Bearer — that's
+ // engine.chat()'s problem, not the UI's).
+ document.getElementById('agentApiKeyField').style.display =
+ (kind === 'custom' || kind === 'anthropic') ? '' : 'none';
document.getElementById('agentRemoteModelField').style.display = kind === 'custom' ? '' : 'none';
if (kind === 'ollama') agentOllamaRefresh();
}
+function agentAnthropicModelChanged() {
+ const sel = document.getElementById('agentAnthropicModel');
+ const custom = document.getElementById('agentAnthropicCustomModel');
+ if (!sel || !custom) return;
+ if (sel.value === '__custom__') {
+ custom.style.display = '';
+ custom.focus();
+ } else {
+ custom.style.display = 'none';
+ }
+}
+
async function agentOllamaRefresh() {
const sel = document.getElementById('agentOllamaModel');
const hint = document.getElementById('agentOllamaHint');
@@ -15401,6 +15462,19 @@ async function agentSaveSettings() {
payload.base_url = 'http://127.0.0.1:11434/v1';
payload.model = sel ? sel.value : '';
payload.local_model_path = '';
+ } else if (kind === 'anthropic') {
+ // Anthropic Messages API: implicit base URL + x-api-key auth (handled
+ // inside engine.chat). Model is either the curated select or a custom
+ // dated id.
+ payload.base_url = 'https://api.anthropic.com/v1';
+ const sel = document.getElementById('agentAnthropicModel');
+ const custom = (document.getElementById('agentAnthropicCustomModel').value || '').trim();
+ payload.model = (sel && sel.value === '__custom__')
+ ? (custom || 'claude-sonnet-4-5')
+ : (sel ? sel.value : 'claude-sonnet-4-5');
+ payload.local_model_path = '';
+ const ak = (document.getElementById('agentApiKey').value || '').trim();
+ if (ak) payload.api_key = ak;
} else {
payload.base_url = (document.getElementById('agentBaseUrl').value || '').trim();
payload.model = (document.getElementById('agentRemoteModel').value || '').trim();