%%{init: {"theme": "base", "themeVariables": {"background": "#fffdf7", "primaryTextColor": "#222222", "fontFamily": "Arial, sans-serif", "lineColor": "#565656"}}}%%
flowchart TD
M["Selected backend mode"] --> API["HF API
Qwen/Qwen3-4B"]
M --> Local["ZeroGPU or local
OpenBMB"]
M --> D["Deterministic mode
no token required"]
API --> V["Validate structured output"]
Local --> V
D --> V
API -->|"missing token
timeout
invalid JSON"| F["Deterministic fallback"]
Local -->|"GPU unavailable
load failure
invalid output"| F
V -->|"invalid after repair"| F
F --> V
V -->|"valid"| R["Stage beat"]
R --> T["Sanitized trace export"]
classDef stage fill:#fff2b8,stroke:#2c2c2c,color:#222222,stroke-width:2px;
classDef model fill:#dcecff,stroke:#2c2c2c,color:#222222,stroke-width:2px;
classDef runtime fill:#ece8ff,stroke:#2c2c2c,color:#222222,stroke-width:2px;
classDef fallback fill:#fbdada,stroke:#2c2c2c,color:#222222,stroke-width:2px;
classDef validate fill:#d9f3df,stroke:#2c2c2c,color:#222222,stroke-width:2px;
classDef trace fill:#f9e1ee,stroke:#2c2c2c,color:#222222,stroke-width:2px;
class M runtime;
class API,Local model;
class D,F fallback;
class V validate;
class R stage;
class T trace;