mirror of
https://ghfast.top/https://github.com/aeroxw/tick-stock-panel.git
synced 2026-09-12 21:24:16 +08:00
- 新增 /api/signals/ai/generate 接口:自然语言描述 → 结构化信号条件 - 新增 custom_signals_ai 模块:组装提示词 + 解析校验 AI 返回的 JSON - 复用 custom_signals.validate() 白名单安全闸门 - 新增输出 token 上限和上下文窗口设置 - 实现 AI 请求 max_tokens 钳制和输入预算检查 - 自定义信号右值字段名容错处理 - 增强 JSON 解析容错(尾随逗号、垃圾字符) - 前端自定义信号对话框接入 AI 生成
346 lines
13 KiB
Python
346 lines
13 KiB
Python
from __future__ import annotations
|
|
|
|
import tomllib
|
|
|
|
import httpx
|
|
import openai
|
|
import pytest
|
|
|
|
from app.services import ai_provider
|
|
from app.services.ai_provider import (
|
|
_format_openai_error,
|
|
_is_temperature_rejected,
|
|
normalize_openai_base_url,
|
|
)
|
|
|
|
|
|
def test_normalize_openai_base_url_adds_v1_for_root_gateway():
|
|
assert normalize_openai_base_url("http://ai.zedbox.cn:8080") == "http://ai.zedbox.cn:8080/v1"
|
|
|
|
|
|
def test_normalize_openai_base_url_preserves_v1_base():
|
|
assert normalize_openai_base_url("http://ai.zedbox.cn:8080/v1") == "http://ai.zedbox.cn:8080/v1"
|
|
|
|
|
|
def test_normalize_openai_base_url_strips_chat_completions_path():
|
|
assert normalize_openai_base_url("http://ai.zedbox.cn:8080/v1/chat/completions") == "http://ai.zedbox.cn:8080/v1"
|
|
|
|
|
|
def test_normalize_openai_base_url_preserves_glm_v4():
|
|
"""智谱 GLM 用 /api/paas/v4, 不能强制补成 /v4/v1 (会 404)。"""
|
|
assert normalize_openai_base_url("https://open.bigmodel.cn/api/paas/v4") == "https://open.bigmodel.cn/api/paas/v4"
|
|
|
|
|
|
def test_normalize_openai_base_url_strips_chat_completions_from_glm_v4():
|
|
"""用户填完整 /v4/chat/completions 时, 去掉后缀归一化为 /v4。"""
|
|
assert normalize_openai_base_url("https://open.bigmodel.cn/api/paas/v4/chat/completions") == "https://open.bigmodel.cn/api/paas/v4"
|
|
|
|
|
|
def test_normalize_openai_base_url_preserves_other_version_segments():
|
|
"""其它非 v1 版本号 (/v2 等) 也应保持原样。"""
|
|
assert normalize_openai_base_url("https://example.com/api/v2") == "https://example.com/api/v2"
|
|
|
|
|
|
def test_normalize_openai_base_url_strips_trailing_slash():
|
|
assert normalize_openai_base_url("https://open.bigmodel.cn/api/paas/v4/") == "https://open.bigmodel.cn/api/paas/v4"
|
|
|
|
|
|
def test_format_openai_error_hides_html_gateway_body():
|
|
response = httpx.Response(
|
|
504,
|
|
headers={"content-type": "text/html; charset=utf-8"},
|
|
text="<!DOCTYPE html><html><body><h1>Gateway Timeout</h1></body></html>",
|
|
request=httpx.Request("POST", "https://example.com/v1/chat/completions"),
|
|
)
|
|
exc = openai.InternalServerError("gateway timeout", response=response, body=response.text)
|
|
|
|
message = _format_openai_error(exc)
|
|
|
|
assert message == "AI 服务请求失败(504): AI 上游服务超时, 请稍后重试或检查 AI Base URL / 网络"
|
|
assert "html" not in message.lower()
|
|
assert "Gateway Timeout" not in message
|
|
|
|
|
|
def test_format_openai_error_prefers_upstream_detail_when_available():
|
|
"""有可读的上游 detail 时优先透出, 而不是用 400 通用文案吞掉。"""
|
|
response = httpx.Response(
|
|
400,
|
|
json={"error": {"message": "model context length exceeded"}},
|
|
request=httpx.Request("POST", "https://example.com/v1/chat/completions"),
|
|
)
|
|
exc = openai.BadRequestError(
|
|
"bad request",
|
|
response=response,
|
|
body={"error": {"message": "model context length exceeded"}},
|
|
)
|
|
|
|
message = _format_openai_error(exc)
|
|
|
|
assert message == "AI 服务请求失败(400): model context length exceeded"
|
|
|
|
|
|
def test_format_openai_error_falls_back_to_status_message_without_detail():
|
|
"""上游无可读 detail (如 HTML 网关页) 时, 才回落到 400 通用文案。"""
|
|
response = httpx.Response(
|
|
400,
|
|
headers={"content-type": "text/html; charset=utf-8"},
|
|
text="<!DOCTYPE html><html></html>",
|
|
request=httpx.Request("POST", "https://example.com/v1/chat/completions"),
|
|
)
|
|
exc = openai.BadRequestError("bad request", response=response, body=None)
|
|
|
|
message = _format_openai_error(exc)
|
|
|
|
assert message == "AI 服务请求失败(400): 请求参数无效, 请检查模型名称和上下文长度"
|
|
|
|
|
|
def test_is_temperature_rejected_matches_moonshot_message():
|
|
"""Moonshot 对 reasoning 模型报 'only 1 is allowed for this model'。"""
|
|
response = httpx.Response(
|
|
400,
|
|
json={"error": {"message": "invalid temperature: only 1 is allowed for this model"}},
|
|
request=httpx.Request("POST", "https://api.moonshot.cn/v1/chat/completions"),
|
|
)
|
|
exc = openai.BadRequestError(
|
|
"bad request",
|
|
response=response,
|
|
body={"error": {"message": "invalid temperature: only 1 is allowed for this model"}},
|
|
)
|
|
assert _is_temperature_rejected(exc) is True
|
|
|
|
|
|
def test_is_temperature_rejected_matches_generic_temperature_hint():
|
|
response = httpx.Response(
|
|
400,
|
|
json={"error": {"message": "unsupported parameter: temperature"}},
|
|
request=httpx.Request("POST", "https://example.com/v1/chat/completions"),
|
|
)
|
|
exc = openai.BadRequestError(
|
|
"bad request", response=response,
|
|
body={"error": {"message": "unsupported parameter: temperature"}},
|
|
)
|
|
assert _is_temperature_rejected(exc) is True
|
|
|
|
|
|
def test_is_temperature_rejected_false_for_other_400():
|
|
"""非 temperature 相关的 400 (如 model not found) 不应触发去 temperature 重试。"""
|
|
response = httpx.Response(
|
|
400,
|
|
json={"error": {"message": "model not found"}},
|
|
request=httpx.Request("POST", "https://example.com/v1/chat/completions"),
|
|
)
|
|
exc = openai.BadRequestError(
|
|
"bad request", response=response,
|
|
body={"error": {"message": "model not found"}},
|
|
)
|
|
assert _is_temperature_rejected(exc) is False
|
|
|
|
|
|
def test_is_temperature_rejected_false_for_non_400():
|
|
response = httpx.Response(
|
|
401,
|
|
json={"error": {"message": "invalid api key"}},
|
|
request=httpx.Request("POST", "https://example.com/v1/chat/completions"),
|
|
)
|
|
exc = openai.AuthenticationError("unauthorized", response=response, body=None)
|
|
assert _is_temperature_rejected(exc) is False
|
|
|
|
|
|
# ── 输出上限 / 上下文窗口配置 ─────────────────────────────────
|
|
|
|
|
|
def test_resolve_max_tokens_defaults_to_config_cap(monkeypatch):
|
|
monkeypatch.setattr(ai_provider, "current_ai_max_output_tokens", lambda: 8192)
|
|
assert ai_provider._resolve_max_tokens(None) == 8192
|
|
|
|
|
|
def test_resolve_max_tokens_clamps_above_cap(monkeypatch):
|
|
monkeypatch.setattr(ai_provider, "current_ai_max_output_tokens", lambda: 3000)
|
|
assert ai_provider._resolve_max_tokens(9000) == 3000
|
|
|
|
|
|
def test_resolve_max_tokens_keeps_below_cap(monkeypatch):
|
|
monkeypatch.setattr(ai_provider, "current_ai_max_output_tokens", lambda: 8192)
|
|
assert ai_provider._resolve_max_tokens(2000) == 2000
|
|
|
|
|
|
def test_estimate_input_tokens_counts_cjk_and_ascii():
|
|
# 中文按 1 字 1 token
|
|
cjk = [{"role": "user", "content": "中文" * 100}] # 200 字
|
|
assert ai_provider._estimate_input_tokens(cjk) >= 200
|
|
# 英文按 ~4 字符 1 token
|
|
ascii_msg = [{"role": "user", "content": "a" * 400}]
|
|
assert ai_provider._estimate_input_tokens(ascii_msg) <= 200
|
|
|
|
|
|
def test_check_input_budget_raises_when_over_window(monkeypatch):
|
|
monkeypatch.setattr(ai_provider, "current_ai_context_window", lambda: 100)
|
|
big = [{"role": "user", "content": "中" * 200}] # 估算输入 ~200 tokens
|
|
with pytest.raises(ValueError, match="上下文窗口"):
|
|
ai_provider._check_input_budget(big, max_tokens=3000)
|
|
|
|
|
|
def test_check_input_budget_passes_within_window(monkeypatch):
|
|
monkeypatch.setattr(ai_provider, "current_ai_context_window", lambda: 64000)
|
|
small = [{"role": "user", "content": "中" * 100}]
|
|
# 不抛异常
|
|
ai_provider._check_input_budget(small, max_tokens=2000)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_generate_ai_text_clamps_max_tokens_to_config_cap(monkeypatch):
|
|
captured: dict = {}
|
|
monkeypatch.setattr(ai_provider, "is_codex_cli_provider", lambda: False)
|
|
monkeypatch.setattr(ai_provider, "current_ai_max_output_tokens", lambda: 3000)
|
|
monkeypatch.setattr(ai_provider, "current_ai_context_window", lambda: 64000)
|
|
|
|
async def fake_run(messages, *, temperature, max_tokens, timeout):
|
|
captured["max_tokens"] = max_tokens
|
|
return "ok"
|
|
|
|
monkeypatch.setattr(ai_provider, "_run_openai_once", fake_run)
|
|
text = await ai_provider.generate_ai_text(
|
|
[{"role": "user", "content": "hi"}], max_tokens=9000
|
|
)
|
|
assert text == "ok"
|
|
assert captured["max_tokens"] == 3000
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_generate_ai_text_defaults_to_config_cap(monkeypatch):
|
|
captured: dict = {}
|
|
monkeypatch.setattr(ai_provider, "is_codex_cli_provider", lambda: False)
|
|
monkeypatch.setattr(ai_provider, "current_ai_max_output_tokens", lambda: 4000)
|
|
monkeypatch.setattr(ai_provider, "current_ai_context_window", lambda: 64000)
|
|
|
|
async def fake_run(messages, *, temperature, max_tokens, timeout):
|
|
captured["max_tokens"] = max_tokens
|
|
return "ok"
|
|
|
|
monkeypatch.setattr(ai_provider, "_run_openai_once", fake_run)
|
|
await ai_provider.generate_ai_text([{"role": "user", "content": "hi"}])
|
|
assert captured["max_tokens"] == 4000
|
|
|
|
|
|
def test_save_ai_settings_persists_token_sizes(monkeypatch):
|
|
from app.api import settings as settings_api
|
|
from app.config import settings as app_settings
|
|
|
|
saved: dict = {}
|
|
monkeypatch.setattr(settings_api.secrets_store, "save", lambda updates: saved.update(updates))
|
|
monkeypatch.setattr(settings_api.secrets_store, "load", lambda: saved)
|
|
original_output = app_settings.ai_max_output_tokens
|
|
original_window = app_settings.ai_context_window
|
|
try:
|
|
req = settings_api.AiSettingsIn(
|
|
provider="openai_compat",
|
|
base_url="https://example.com/v1",
|
|
api_key="sk-test",
|
|
model="gpt-x",
|
|
max_output_tokens=5000,
|
|
context_window=128000,
|
|
)
|
|
result = settings_api.save_ai_settings(req)
|
|
assert saved["ai_max_output_tokens"] == 5000
|
|
assert saved["ai_context_window"] == 128000
|
|
assert result["ai_max_output_tokens"] == 5000
|
|
assert result["ai_context_window"] == 128000
|
|
finally:
|
|
app_settings.ai_max_output_tokens = original_output
|
|
app_settings.ai_context_window = original_window
|
|
|
|
|
|
def test_save_ai_settings_rejects_non_positive(monkeypatch):
|
|
from app.api import settings as settings_api
|
|
from fastapi import HTTPException
|
|
|
|
req = settings_api.AiSettingsIn(provider="openai_compat", max_output_tokens=-1)
|
|
with pytest.raises(HTTPException):
|
|
settings_api.save_ai_settings(req)
|
|
req2 = settings_api.AiSettingsIn(provider="openai_compat", context_window=0)
|
|
with pytest.raises(HTTPException):
|
|
settings_api.save_ai_settings(req2)
|
|
|
|
|
|
def test_codex_process_env_excludes_application_secrets(monkeypatch, tmp_path):
|
|
monkeypatch.setenv("PATH", "test-path")
|
|
monkeypatch.setenv("HTTPS_PROXY", "http://proxy.example")
|
|
monkeypatch.setenv("TICKFLOW_API_KEY", "tickflow-secret")
|
|
monkeypatch.setenv("AI_API_KEY", "ai-secret")
|
|
monkeypatch.setenv("OPENAI_API_KEY", "openai-secret")
|
|
monkeypatch.setenv("AUTH_PASSWORD", "password-secret")
|
|
|
|
env = ai_provider._codex_process_env(tmp_path / "codex-home")
|
|
|
|
assert env["PATH"] == "test-path"
|
|
assert env["HTTPS_PROXY"] == "http://proxy.example"
|
|
assert env["NO_COLOR"] == "1"
|
|
assert env["CODEX_HOME"] == str(tmp_path / "codex-home")
|
|
assert "TICKFLOW_API_KEY" not in env
|
|
assert "AI_API_KEY" not in env
|
|
assert "OPENAI_API_KEY" not in env
|
|
assert "AUTH_PASSWORD" not in env
|
|
|
|
|
|
def test_codex_config_adapts_local_access_provider_for_docker(monkeypatch, tmp_path):
|
|
monkeypatch.setenv("CODEX_DOCKER_HOST", "host.docker.internal")
|
|
monkeypatch.setattr(ai_provider, "current_ai_model", lambda: "")
|
|
monkeypatch.setattr(ai_provider, "current_codex_reasoning_effort", lambda: "")
|
|
monkeypatch.setattr(
|
|
ai_provider,
|
|
"_read_codex_config",
|
|
lambda: {
|
|
"model_provider": "codex_local_access",
|
|
"model": "gpt-5.6-sol",
|
|
"model_providers": {
|
|
"codex_local_access": {
|
|
"name": "Codex API Service",
|
|
"base_url": "http://localhost:62678/v1",
|
|
"wire_api": "responses",
|
|
"requires_openai_auth": True,
|
|
"supports_websockets": False,
|
|
"experimental_bearer_token": "local-secret",
|
|
}
|
|
},
|
|
},
|
|
)
|
|
path = tmp_path / "config.toml"
|
|
|
|
ai_provider._write_compatible_codex_config(path)
|
|
|
|
with path.open("rb") as f:
|
|
config = tomllib.load(f)
|
|
assert config["model_provider"] == "codex_local_access"
|
|
provider = config["model_providers"]["codex_local_access"]
|
|
assert provider["base_url"] == "http://host.docker.internal:62678/v1"
|
|
assert provider["experimental_bearer_token"] == "local-secret"
|
|
assert provider["requires_openai_auth"] is True
|
|
assert provider["supports_websockets"] is False
|
|
|
|
|
|
def test_codex_config_does_not_copy_provider_without_docker_opt_in(monkeypatch, tmp_path):
|
|
monkeypatch.delenv("CODEX_DOCKER_HOST", raising=False)
|
|
monkeypatch.setattr(ai_provider, "current_ai_model", lambda: "")
|
|
monkeypatch.setattr(ai_provider, "current_codex_reasoning_effort", lambda: "")
|
|
monkeypatch.setattr(
|
|
ai_provider,
|
|
"_read_codex_config",
|
|
lambda: {
|
|
"model_provider": "codex_local_access",
|
|
"model_providers": {
|
|
"codex_local_access": {
|
|
"base_url": "http://localhost:62678/v1",
|
|
"experimental_bearer_token": "must-not-leak",
|
|
}
|
|
},
|
|
},
|
|
)
|
|
path = tmp_path / "config.toml"
|
|
|
|
ai_provider._write_compatible_codex_config(path)
|
|
|
|
text = path.read_text(encoding="utf-8")
|
|
assert "model_provider" not in text
|
|
assert "model_providers" not in text
|
|
assert "must-not-leak" not in text
|