Skip to content
Open
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
8 changes: 6 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,9 @@ LLM_PROXY_URL=
# Keep false unless the backend can actually reach that proxy endpoint.
LLM_USE_SYSTEM_PROXY=false

# Optional JEV pre-trade decision filter. Configure this from System Settings
# or set the values here. QuantDinger tries JEV first, then the configured LLM.
# Optional JEV or Drex pre-trade decision filter. Configure this from System
# Settings or set the values here. QuantDinger tries the System One provider
# selected by AI_DECISION_PROVIDER (jev or drex) first, then the configured LLM.
# If neither provider is configured or both are unavailable, entries are
# allowed and the fail-open outcome is written to the AI decision audit log.
JEV_ENABLED=true
Expand All @@ -325,6 +326,9 @@ JEV_BASE_URL=https://api.typesafe.ai/v1
JEV_MODEL=jev-latest
JEV_TIMEOUT_SECONDS=8
JEV_MIN_CONFIDENCE=0.55
AI_DECISION_PROVIDER=jev
NACE_API_KEY=
NACE_DEFAULT_DECISION_MODEL=drex-max-finance
AI_DECISION_TIMEOUT_SECONDS=10

OPENROUTER_API_KEY=
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,12 @@ Configure `JEV_ENABLED`, `JEV_API_KEY`, `JEV_BASE_URL`, `JEV_MODEL`, and
the decision filter uses the configured LLM directly. TypeSafe documents the
HTTP contract at [`POST /v1/systemone`](https://docs.typesafe.ai/introduction/quickstart).

To use [Nace Drex](https://console.nace.ai/docs/drex) instead, set `AI_DECISION_PROVIDER` to
`drex` and add `NACE_API_KEY` (model `NACE_DEFAULT_DECISION_MODEL`, default
`drex-max-finance`). Drex serves the same System One contract and runs the same
checks, `JEV_TIMEOUT_SECONDS`, `JEV_MIN_CONFIDENCE`, and fallbacks; `JEV_ENABLED`
applies to Jev only. The decision timeline records it as provider `drex`.

## AI agents and MCP

The Agent Gateway is exposed under `/api/agent/v1`. The included MCP server lets
Expand Down
4 changes: 4 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,10 @@ flowchart TD
大模型。TypeSafe 在
[`POST /v1/systemone`](https://docs.typesafe.ai/introduction/quickstart) 中说明了 HTTP 合同。

如需改用 [Nace Drex](https://console.nace.ai/docs/drex),将 `AI_DECISION_PROVIDER` 设为 `drex` 并配置
`NACE_API_KEY`(模型为 `NACE_DEFAULT_DECISION_MODEL`,默认 `drex-max-finance`)。Drex 使用相同的
System One 合同、检查项、`JEV_TIMEOUT_SECONDS`、`JEV_MIN_CONFIDENCE` 和回退流程(`JEV_ENABLED` 仅作用于 Jev),决策时间线中的提供商记录为 `drex`。

## AI Agent 与 MCP

Agent Gateway 通过 `/api/agent/v1` 暴露。内置 MCP Server 允许 Cursor、Claude Code 和
Expand Down
30 changes: 30 additions & 0 deletions backend_api_python/app/routes/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
'ALPHA_VANTAGE_API_KEY', 'ALPHA_VANTAGE_BASE_URL', 'ALPHA_VANTAGE_TIMEOUT', 'ALPHA_VANTAGE_NEWS_LIMIT',
'AI_CODE_GEN_MODEL', 'LLM_PROXY_URL', 'LLM_USE_SYSTEM_PROXY',
'JEV_BASE_URL', 'JEV_MODEL', 'JEV_TIMEOUT_SECONDS', 'JEV_MIN_CONFIDENCE', 'AI_DECISION_TIMEOUT_SECONDS',
'NACE_DEFAULT_DECISION_MODEL',
'OPENAI_BASE_URL', 'DEEPSEEK_BASE_URL', 'GROK_BASE_URL', 'ATLASCLOUD_BASE_URL', 'MINIMAX_BASE_URL',
# Data source internals
'FINNHUB_API_KEY', 'FINNHUB_FREE_ONLY',
Expand Down Expand Up @@ -473,6 +474,35 @@
'default': 0.55,
'description': 'settings.desc.JEV_MIN_CONFIDENCE'
},
{
'key': 'AI_DECISION_PROVIDER',
'label': 'AI Decision Provider',
'type': 'select',
'default': 'jev',
'options': [
{'value': 'jev', 'label': 'TypeSafe Jev'},
{'value': 'drex', 'label': 'Nace Drex'},
],
'description': 'System One model the AI decision filter calls before the LLM fallback'
},
{
'key': 'NACE_API_KEY',
'label': 'Nace API Key',
'type': 'password',
'required': False,
'link': 'https://console.nace.ai/',
'link_text': 'settings.link.getApiKey',
'description': 'Drex API key, used when AI Decision Provider is Nace Drex'
},
{
'key': 'NACE_DEFAULT_DECISION_MODEL',
'label': 'Drex Model',
'type': 'text',
'default': 'drex-max-finance',
'link': 'https://console.nace.ai/docs/drex/reference/models',
'link_text': 'settings.link.viewModels',
'description': 'Drex model name, e.g. drex-max-finance'
},
{
'key': 'LLM_PROVIDER',
'label': 'LLM Provider',
Expand Down
80 changes: 50 additions & 30 deletions backend_api_python/app/services/ai_decision_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import requests

from app._version import APP_VERSION
from app.services.llm import LLMService
from app.utils.db import get_db_connection
from app.utils.logger import get_logger
Expand Down Expand Up @@ -131,7 +132,7 @@ def public_dict(self) -> dict[str, Any]:


class AIDecisionFilter:
"""Evaluate entry orders with Jev first and an LLM fallback."""
"""Evaluate entry orders with a System One model (Jev or Drex) first and an LLM fallback."""

def evaluate(self, request: AIDecisionRequest, *, enabled: bool) -> AIDecisionResult:
decision_id = str(uuid.uuid4())
Expand All @@ -150,13 +151,13 @@ def evaluate(self, request: AIDecisionRequest, *, enabled: bool) -> AIDecisionRe
return result

failures: list[str] = []
jev_config = self._jev_config()
jev_enabled = str(jev_config.get("enabled", True)).strip().lower() in {
system_one = self._system_one_config()
system_one_enabled = str(system_one.get("enabled", True)).strip().lower() in {
"1", "true", "yes", "on",
}
llm: LLMService | None = None
llm_configured = False
if not jev_enabled or not jev_config["api_key"]:
if not system_one_enabled or not system_one["api_key"]:
try:
llm = LLMService()
llm_configured = llm.is_configured()
Expand Down Expand Up @@ -194,17 +195,17 @@ def evaluate(self, request: AIDecisionRequest, *, enabled: bool) -> AIDecisionRe
self._persist(request, result)
return result

if jev_enabled and jev_config["api_key"]:
if system_one_enabled and system_one["api_key"]:
try:
result = replace(
self._evaluate_jev(request, decision_id, started, jev_config),
self._evaluate_system_one(request, decision_id, started, system_one),
billing=billing,
)
self._persist(request, result)
return result
except Exception as exc:
failures.append(f"jev:{self._safe_error(exc)}")
logger.warning("Jev decision failed open: %s", exc)
failures.append(f"{system_one['provider']}:{self._safe_error(exc)}")
logger.warning("%s decision failed open: %s", system_one["provider"].capitalize(), exc)

if llm is None:
try:
Expand Down Expand Up @@ -240,7 +241,7 @@ def evaluate(self, request: AIDecisionRequest, *, enabled: bool) -> AIDecisionRe
self._persist(request, result)
return result

def _evaluate_jev(
def _evaluate_system_one(
self,
request: AIDecisionRequest,
decision_id: str,
Expand All @@ -249,14 +250,21 @@ def _evaluate_jev(
) -> AIDecisionResult:
base_url = config["base_url"].strip().rstrip("/")
url = base_url if base_url.endswith("/systemone") else f"{base_url}/systemone"
provider = config["provider"]
label = provider.capitalize()
model = config["model"].strip() or "jev-latest"
timeout = max(1.0, min(float(config["timeout_seconds"] or 8), 30.0))
headers = {
"Authorization": f"Bearer {config['api_key']}",
"Content-Type": "application/json",
}
if provider == "drex":
# Nace attributes Drex traffic per client integration from these headers.
client = f"quantdinger/{APP_VERSION}"
headers.update({"X-Nace-Client": client, "User-Agent": client})
response = requests.post(
url,
headers={
"Authorization": f"Bearer {config['api_key']}",
"Content-Type": "application/json",
},
headers=headers,
json={
"model": model,
"state": self._state_payload(request),
Expand All @@ -275,6 +283,7 @@ def _evaluate_jev(
answer,
question=name,
options=options,
label=label,
)
confidence = self._confidence(answer, probabilities, choice)
results[name] = (choice, probabilities, confidence)
Expand All @@ -291,7 +300,7 @@ def _evaluate_jev(
if confidence is None or confidence < min_confidence:
confidence_text = "missing" if confidence is None else f"{confidence:.3f}"
raise ValueError(
f"Jev confidence below threshold for {name} "
f"{label} confidence below threshold for {name} "
f"(confidence={confidence_text}, threshold={min_confidence:.3f})"
)

Expand All @@ -312,15 +321,15 @@ def _evaluate_jev(
)
final_choice = "pass" if allowed else "reject"
if allowed:
reason = "jev_entry_approved"
reason = f"{provider}_entry_approved"
elif risk_choice == "block":
reason = "jev_entry_rejected:risk_block"
reason = f"{provider}_entry_rejected:risk_block"
elif execution_choice == "block":
reason = "jev_entry_rejected:execution_block"
reason = f"{provider}_entry_rejected:execution_block"
elif directional_block:
reason = "jev_entry_rejected:signal_conflict"
reason = f"{provider}_entry_rejected:signal_conflict"
else:
reason = "jev_entry_rejected"
reason = f"{provider}_entry_rejected"
if risk_choice == "block":
confidence = risk_confidence
elif execution_choice == "block":
Expand All @@ -332,7 +341,7 @@ def _evaluate_jev(
return self._result(
allowed,
final_choice,
"jev",
provider,
reason,
decision_id,
started,
Expand Down Expand Up @@ -443,30 +452,31 @@ def _validate_choice_answer(
*,
question: str,
options: set[str],
label: str,
) -> tuple[str, dict[str, float]]:
choice = str(answer.get("choice") or answer.get("selected") or "").strip().lower()
if choice not in options:
raise ValueError(f"Jev response did not contain a valid {question}")
raise ValueError(f"{label} response did not contain a valid {question}")
raw_probabilities = cls._probabilities(answer)
if set(raw_probabilities) != options:
raise ValueError(f"Jev response probabilities were incomplete for {question}")
raise ValueError(f"{label} response probabilities were incomplete for {question}")
probabilities: dict[str, float] = {}
for option, value in raw_probabilities.items():
try:
probability = float(value)
except (TypeError, ValueError) as exc:
raise ValueError(f"Jev response probability was invalid for {question}") from exc
raise ValueError(f"{label} response probability was invalid for {question}") from exc
if probability < 0 or probability > 1:
raise ValueError(f"Jev response probability was out of range for {question}")
raise ValueError(f"{label} response probability was out of range for {question}")
probabilities[str(option)] = probability
if abs(sum(probabilities.values()) - 1.0) > 0.001:
raise ValueError(f"Jev response probabilities did not sum to one for {question}")
raise ValueError(f"{label} response probabilities did not sum to one for {question}")
maximum = max(probabilities.values())
if probabilities.get(choice) != maximum:
raise ValueError(f"Jev response choice was not the highest probability for {question}")
raise ValueError(f"{label} response choice was not the highest probability for {question}")
confidence = cls._bounded_float(answer.get("confidence"))
if confidence is None:
raise ValueError(f"Jev response confidence was invalid for {question}")
raise ValueError(f"{label} response confidence was invalid for {question}")
return choice, probabilities

@classmethod
Expand Down Expand Up @@ -498,28 +508,38 @@ def _json_object(value: Any) -> dict[str, Any]:
return parsed

@staticmethod
def _jev_config() -> dict[str, Any]:
def _system_one_config() -> dict[str, Any]:
values: dict[str, str] = {}
try:
from app.services.settings.env_file import read_env_file

values = read_env_file()
except Exception as exc:
logger.debug("Jev settings file refresh skipped: %s", exc)
logger.debug("AI decision settings file refresh skipped: %s", exc)

def setting(key: str, default: str = "") -> str:
if key in values:
return str(values.get(key) or "").strip()
return str(os.getenv(key, default) or default).strip()

return {
config = {
"provider": "jev",
"enabled": setting("JEV_ENABLED", "true").lower() in {"1", "true", "yes", "on"},
"api_key": setting("JEV_API_KEY"),
"base_url": setting("JEV_BASE_URL", "https://api.typesafe.ai/v1"),
"model": setting("JEV_MODEL", "jev-latest"),
"timeout_seconds": setting("JEV_TIMEOUT_SECONDS", "8"),
"min_confidence": setting("JEV_MIN_CONFIDENCE", "0.55"),
}
if setting("AI_DECISION_PROVIDER", "jev").lower() == "drex":
config.update(
provider="drex",
enabled=True,
api_key=setting("NACE_API_KEY"),
base_url="https://console.nace.ai/v1",
model=setting("NACE_DEFAULT_DECISION_MODEL") or "drex-max-finance",
)
return config

@staticmethod
def _safe_error(exc: Exception) -> str:
Expand Down
8 changes: 6 additions & 2 deletions backend_api_python/env.example
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ LLM_PROXY_URL=
# Keep false unless the backend can actually reach that proxy endpoint.
LLM_USE_SYSTEM_PROXY=false

# Optional JEV pre-trade decision filter. Configure this from System Settings
# or set the values here. QuantDinger tries JEV first, then the configured LLM.
# Optional JEV or Drex pre-trade decision filter. Configure this from System
# Settings or set the values here. QuantDinger tries the System One provider
# selected by AI_DECISION_PROVIDER (jev or drex) first, then the configured LLM.
# If neither provider is configured or both are unavailable, entries are
# allowed and the fail-open outcome is written to the AI decision audit log.
JEV_ENABLED=true
Expand All @@ -202,6 +203,9 @@ JEV_BASE_URL=https://api.typesafe.ai/v1
JEV_MODEL=jev-latest
JEV_TIMEOUT_SECONDS=8
JEV_MIN_CONFIDENCE=0.55
AI_DECISION_PROVIDER=jev
NACE_API_KEY=
NACE_DEFAULT_DECISION_MODEL=drex-max-finance
AI_DECISION_TIMEOUT_SECONDS=10

OPENROUTER_API_KEY=
Expand Down
Loading