"""Local inference engine.
Two interchangeable backends, both running entirely on this server:
* llamacpp — loads a GGUF file from storage/models in-process via
llama-cpp-python. The weights are plain files the owner can back up,
fine-tune, and redistribute per each model's open license.
* ollama — talks to a local Ollama instance (http://127.0.0.1:11434).
No OpenAI, no Anthropic, no hosted AI API of any kind. The engine also ships
a curated catalog of open-weight models (Apache-2.0 Qwen family and friends)
that can be downloaded straight into storage/models from the dashboard.
"""
from __future__ import annotations
import json
import re
import threading
import time
from pathlib import Path
from typing import Iterator
import httpx
from .config import cfg
from .db import setting_get, setting_set
from .imagin import EDITOR_PROMPT, IMAGIN_SCHEMA, STUDIO_PROMPT # noqa: F401
# --------------------------------------------------------------------------- #
# Modes
# --------------------------------------------------------------------------- #
FILE_RULES = (
"When you produce project code, split it into real files using fenced code "
"blocks tagged `language:filename`, e.g. ```html:index.html```, "
"```css:style.css```, ```js:script.js```. Always use correct, conventional "
"file names — never placeholders like `file1`. Reference sibling files by "
"those exact names (,