Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions api/agents/shield/explainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def _timeout() -> float:

def llm_available() -> bool:
"""Whether a better explanation than the template could be generated."""
return bool(ai_clients.anthropic(_timeout()) or ai_clients.openai(_timeout()))
return bool(ai_clients.huawei(_timeout()) or ai_clients.anthropic(_timeout()) or ai_clients.openai(_timeout()))


# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -167,7 +167,7 @@ def generate_llm_explanation(signals: list[str], amount: float, action: str) ->
"Write the explanation now."
)

text = _try_anthropic(user_prompt) or _try_openai(user_prompt)
text = _try_huawei(user_prompt) or _try_anthropic(user_prompt) or _try_openai(user_prompt)
elapsed_ms = round((time.perf_counter() - started) * 1000, 2)

if text:
Expand Down Expand Up @@ -214,6 +214,29 @@ def model_facts(signals: list[str]) -> list[str]:
return out[:12]


def _try_huawei(user_prompt: str) -> str | None:
"""Huawei MaaS on the Forge cluster. Thinking is off, as the platform
guide asks: GLM reasons by default and max_tokens does not cap it."""
client = ai_clients.huawei(_timeout())
if not client:
return None
try:
resp = client.chat.completions.create(
model=ai_clients.huawei_model(),
temperature=0.3,
max_tokens=160,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_prompt},
],
extra_body={"chat_template_kwargs": {"thinking": False}},
)
text = (resp.choices[0].message.content or "").strip()
return text or None
except Exception:
return None # the template stays; a model error never touches the decision


def _try_anthropic(user_prompt: str) -> str | None:
client = ai_clients.anthropic(_timeout())
if not client:
Expand Down
24 changes: 23 additions & 1 deletion api/core/ai_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""
from __future__ import annotations

import os
import threading

from core import secrets
Expand Down Expand Up @@ -53,6 +54,26 @@ def build(key, t):
return _cached("anthropic", secrets.get("ANTHROPIC_API_KEY"), timeout, build)


def huawei(timeout: float = 30):
"""Huawei MaaS through the Forge gateway: OpenAI-compatible, with the team
key in an `apikey` header. Only set up where FORGE_MAAS_BASE_URL is."""
base = os.getenv("FORGE_MAAS_BASE_URL", "").strip()
key = os.getenv("GATEWAY_KEY", "").strip()
if not base:
return None

def build(k, t):
from openai import OpenAI

return OpenAI(base_url=base, api_key="unused", default_headers={"apikey": k}, timeout=t)

return _cached("huawei", key, timeout, build)


def huawei_model() -> str:
return os.getenv("FORGE_MAAS_MODEL", "").strip() or "glm-5.2"


def openai_model() -> str:
return secrets.get("OPENAI_MODEL")

Expand All @@ -64,6 +85,7 @@ def anthropic_model() -> str:
def configured() -> bool:
"""Whether any AI provider key is in force (for /health). Never raises."""
try:
return bool(secrets.get("ANTHROPIC_API_KEY") or secrets.get("OPENAI_API_KEY"))
return bool(secrets.get("ANTHROPIC_API_KEY") or secrets.get("OPENAI_API_KEY")
or (os.getenv("FORGE_MAAS_BASE_URL") and os.getenv("GATEWAY_KEY")))
except Exception: # noqa: BLE001
return False
44 changes: 44 additions & 0 deletions api/tests/test_huawei_maas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Huawei MaaS writes the explanation wording on the Forge cluster, never the decision."""
from types import SimpleNamespace


def test_huawei_client_sends_the_team_key_in_the_apikey_header(monkeypatch):
from core import ai_clients

monkeypatch.setenv("FORGE_MAAS_BASE_URL", "http://maas.test/openai/v1")
monkeypatch.setenv("GATEWAY_KEY", "team-key-for-test")
client = ai_clients.huawei(5)
assert client is not None
assert str(client.base_url).startswith("http://maas.test/openai/v1")
assert client.default_headers.get("apikey") == "team-key-for-test"
assert ai_clients.huawei_model() == "glm-5.2"


def test_no_huawei_client_off_the_cluster(monkeypatch):
from core import ai_clients

monkeypatch.delenv("FORGE_MAAS_BASE_URL", raising=False)
assert ai_clients.huawei(5) is None


def test_explanation_turns_thinking_off_and_falls_back_on_error(monkeypatch):
from agents.shield import explainer
from core import ai_clients

seen = {}

def create(**kwargs):
seen.update(kwargs)
return SimpleNamespace(choices=[SimpleNamespace(message=SimpleNamespace(content="Paused: new payee, unusual amount."))])

fake = SimpleNamespace(chat=SimpleNamespace(completions=SimpleNamespace(create=create)))
monkeypatch.setattr(ai_clients, "huawei", lambda _t=30: fake)
assert explainer._try_huawei("decision") == "Paused: new payee, unusual amount."
assert seen["extra_body"] == {"chat_template_kwargs": {"thinking": False}}
assert seen["model"] == "glm-5.2"

def boom(**_kw):
raise RuntimeError("gateway down")

fake.chat.completions.create = boom
assert explainer._try_huawei("decision") is None
Loading