HouYunFei Codex commited on
Commit ·
2dd0d2e
1
Parent(s): 1ff7d54
Add request content filtering
Browse filesCo-Authored-By: Codex <noreply@openai.com>
- api/ai.py +15 -1
- api/image_tasks.py +12 -0
- config.json +11 -1
- services/config.py +12 -0
- services/content_filter.py +57 -0
- web/src/app/settings/components/config-card.tsx +40 -0
- web/src/app/settings/store.ts +26 -0
- web/src/lib/api.ts +8 -0
api/ai.py
CHANGED
|
@@ -5,6 +5,7 @@ from fastapi.concurrency import run_in_threadpool
|
|
| 5 |
from pydantic import BaseModel, ConfigDict, Field
|
| 6 |
|
| 7 |
from api.support import require_identity, resolve_image_base_url
|
|
|
|
| 8 |
from services.log_service import LoggedCall
|
| 9 |
from services.protocol import (
|
| 10 |
anthropic_v1_messages,
|
|
@@ -53,6 +54,14 @@ class AnthropicMessageRequest(BaseModel):
|
|
| 53 |
stream: bool | None = None
|
| 54 |
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
def create_router() -> APIRouter:
|
| 57 |
router = APIRouter()
|
| 58 |
|
|
@@ -74,6 +83,7 @@ def create_router() -> APIRouter:
|
|
| 74 |
payload = body.model_dump(mode="python")
|
| 75 |
payload["base_url"] = resolve_image_base_url(request)
|
| 76 |
call = LoggedCall(identity, "/v1/images/generations", body.model, "文生图")
|
|
|
|
| 77 |
return await call.run(openai_v1_image_generations.handle, payload)
|
| 78 |
|
| 79 |
@router.post("/v1/images/edits")
|
|
@@ -90,8 +100,10 @@ def create_router() -> APIRouter:
|
|
| 90 |
stream: bool | None = Form(default=None),
|
| 91 |
):
|
| 92 |
identity = require_identity(authorization)
|
|
|
|
| 93 |
if n < 1 or n > 4:
|
| 94 |
raise HTTPException(status_code=400, detail={"error": "n must be between 1 and 4"})
|
|
|
|
| 95 |
uploads = [*(image or []), *(image_list or [])]
|
| 96 |
if not uploads:
|
| 97 |
raise HTTPException(status_code=400, detail={"error": "image file is required"})
|
|
@@ -111,7 +123,6 @@ def create_router() -> APIRouter:
|
|
| 111 |
"stream": stream,
|
| 112 |
"base_url": resolve_image_base_url(request),
|
| 113 |
}
|
| 114 |
-
call = LoggedCall(identity, "/v1/images/edits", model, "图生图")
|
| 115 |
return await call.run(openai_v1_image_edit.handle, payload)
|
| 116 |
|
| 117 |
@router.post("/v1/chat/completions")
|
|
@@ -120,6 +131,7 @@ def create_router() -> APIRouter:
|
|
| 120 |
payload = body.model_dump(mode="python")
|
| 121 |
model = str(payload.get("model") or "auto")
|
| 122 |
call = LoggedCall(identity, "/v1/chat/completions", model, "文本生成")
|
|
|
|
| 123 |
return await call.run(openai_v1_chat_complete.handle, payload)
|
| 124 |
|
| 125 |
@router.post("/v1/responses")
|
|
@@ -128,6 +140,7 @@ def create_router() -> APIRouter:
|
|
| 128 |
payload = body.model_dump(mode="python")
|
| 129 |
model = str(payload.get("model") or "auto")
|
| 130 |
call = LoggedCall(identity, "/v1/responses", model, "Responses")
|
|
|
|
| 131 |
return await call.run(openai_v1_response.handle, payload)
|
| 132 |
|
| 133 |
@router.post("/v1/messages")
|
|
@@ -141,6 +154,7 @@ def create_router() -> APIRouter:
|
|
| 141 |
payload = body.model_dump(mode="python")
|
| 142 |
model = str(payload.get("model") or "auto")
|
| 143 |
call = LoggedCall(identity, "/v1/messages", model, "Messages")
|
|
|
|
| 144 |
return await call.run(anthropic_v1_messages.handle, payload, sse="anthropic")
|
| 145 |
|
| 146 |
return router
|
|
|
|
| 5 |
from pydantic import BaseModel, ConfigDict, Field
|
| 6 |
|
| 7 |
from api.support import require_identity, resolve_image_base_url
|
| 8 |
+
from services.content_filter import check_request, request_text
|
| 9 |
from services.log_service import LoggedCall
|
| 10 |
from services.protocol import (
|
| 11 |
anthropic_v1_messages,
|
|
|
|
| 54 |
stream: bool | None = None
|
| 55 |
|
| 56 |
|
| 57 |
+
async def filter_or_log(call: LoggedCall, text: str) -> None:
|
| 58 |
+
try:
|
| 59 |
+
await run_in_threadpool(check_request, text)
|
| 60 |
+
except HTTPException as exc:
|
| 61 |
+
call.log("调用失败", status="failed", error=str(exc.detail))
|
| 62 |
+
raise
|
| 63 |
+
|
| 64 |
+
|
| 65 |
def create_router() -> APIRouter:
|
| 66 |
router = APIRouter()
|
| 67 |
|
|
|
|
| 83 |
payload = body.model_dump(mode="python")
|
| 84 |
payload["base_url"] = resolve_image_base_url(request)
|
| 85 |
call = LoggedCall(identity, "/v1/images/generations", body.model, "文生图")
|
| 86 |
+
await filter_or_log(call, body.prompt)
|
| 87 |
return await call.run(openai_v1_image_generations.handle, payload)
|
| 88 |
|
| 89 |
@router.post("/v1/images/edits")
|
|
|
|
| 100 |
stream: bool | None = Form(default=None),
|
| 101 |
):
|
| 102 |
identity = require_identity(authorization)
|
| 103 |
+
call = LoggedCall(identity, "/v1/images/edits", model, "图生图")
|
| 104 |
if n < 1 or n > 4:
|
| 105 |
raise HTTPException(status_code=400, detail={"error": "n must be between 1 and 4"})
|
| 106 |
+
await filter_or_log(call, prompt)
|
| 107 |
uploads = [*(image or []), *(image_list or [])]
|
| 108 |
if not uploads:
|
| 109 |
raise HTTPException(status_code=400, detail={"error": "image file is required"})
|
|
|
|
| 123 |
"stream": stream,
|
| 124 |
"base_url": resolve_image_base_url(request),
|
| 125 |
}
|
|
|
|
| 126 |
return await call.run(openai_v1_image_edit.handle, payload)
|
| 127 |
|
| 128 |
@router.post("/v1/chat/completions")
|
|
|
|
| 131 |
payload = body.model_dump(mode="python")
|
| 132 |
model = str(payload.get("model") or "auto")
|
| 133 |
call = LoggedCall(identity, "/v1/chat/completions", model, "文本生成")
|
| 134 |
+
await filter_or_log(call, request_text(payload.get("prompt"), payload.get("messages")))
|
| 135 |
return await call.run(openai_v1_chat_complete.handle, payload)
|
| 136 |
|
| 137 |
@router.post("/v1/responses")
|
|
|
|
| 140 |
payload = body.model_dump(mode="python")
|
| 141 |
model = str(payload.get("model") or "auto")
|
| 142 |
call = LoggedCall(identity, "/v1/responses", model, "Responses")
|
| 143 |
+
await filter_or_log(call, request_text(payload.get("input"), payload.get("instructions")))
|
| 144 |
return await call.run(openai_v1_response.handle, payload)
|
| 145 |
|
| 146 |
@router.post("/v1/messages")
|
|
|
|
| 154 |
payload = body.model_dump(mode="python")
|
| 155 |
model = str(payload.get("model") or "auto")
|
| 156 |
call = LoggedCall(identity, "/v1/messages", model, "Messages")
|
| 157 |
+
await filter_or_log(call, request_text(payload.get("system"), payload.get("messages"), payload.get("tools")))
|
| 158 |
return await call.run(anthropic_v1_messages.handle, payload, sse="anthropic")
|
| 159 |
|
| 160 |
return router
|
api/image_tasks.py
CHANGED
|
@@ -5,7 +5,9 @@ from fastapi.concurrency import run_in_threadpool
|
|
| 5 |
from pydantic import BaseModel, Field
|
| 6 |
|
| 7 |
from api.support import require_identity, resolve_image_base_url
|
|
|
|
| 8 |
from services.image_task_service import image_task_service
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
class ImageGenerationTaskRequest(BaseModel):
|
|
@@ -19,6 +21,14 @@ def _parse_task_ids(value: str) -> list[str]:
|
|
| 19 |
return [item.strip() for item in value.split(",") if item.strip()]
|
| 20 |
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def create_router() -> APIRouter:
|
| 23 |
router = APIRouter()
|
| 24 |
|
|
@@ -37,6 +47,7 @@ def create_router() -> APIRouter:
|
|
| 37 |
authorization: str | None = Header(default=None),
|
| 38 |
):
|
| 39 |
identity = require_identity(authorization)
|
|
|
|
| 40 |
try:
|
| 41 |
return await run_in_threadpool(
|
| 42 |
image_task_service.submit_generation,
|
|
@@ -62,6 +73,7 @@ def create_router() -> APIRouter:
|
|
| 62 |
size: str | None = Form(default=None),
|
| 63 |
):
|
| 64 |
identity = require_identity(authorization)
|
|
|
|
| 65 |
uploads = [*(image or []), *(image_list or [])]
|
| 66 |
if not uploads:
|
| 67 |
raise HTTPException(status_code=400, detail={"error": "image file is required"})
|
|
|
|
| 5 |
from pydantic import BaseModel, Field
|
| 6 |
|
| 7 |
from api.support import require_identity, resolve_image_base_url
|
| 8 |
+
from services.content_filter import check_request
|
| 9 |
from services.image_task_service import image_task_service
|
| 10 |
+
from services.log_service import LoggedCall
|
| 11 |
|
| 12 |
|
| 13 |
class ImageGenerationTaskRequest(BaseModel):
|
|
|
|
| 21 |
return [item.strip() for item in value.split(",") if item.strip()]
|
| 22 |
|
| 23 |
|
| 24 |
+
async def filter_or_log(call: LoggedCall, text: str) -> None:
|
| 25 |
+
try:
|
| 26 |
+
await run_in_threadpool(check_request, text)
|
| 27 |
+
except HTTPException as exc:
|
| 28 |
+
call.log("调用失败", status="failed", error=str(exc.detail))
|
| 29 |
+
raise
|
| 30 |
+
|
| 31 |
+
|
| 32 |
def create_router() -> APIRouter:
|
| 33 |
router = APIRouter()
|
| 34 |
|
|
|
|
| 47 |
authorization: str | None = Header(default=None),
|
| 48 |
):
|
| 49 |
identity = require_identity(authorization)
|
| 50 |
+
await filter_or_log(LoggedCall(identity, "/api/image-tasks/generations", body.model, "文生图任务"), body.prompt)
|
| 51 |
try:
|
| 52 |
return await run_in_threadpool(
|
| 53 |
image_task_service.submit_generation,
|
|
|
|
| 73 |
size: str | None = Form(default=None),
|
| 74 |
):
|
| 75 |
identity = require_identity(authorization)
|
| 76 |
+
await filter_or_log(LoggedCall(identity, "/api/image-tasks/edits", model, "图生图任务"), prompt)
|
| 77 |
uploads = [*(image or []), *(image_list or [])]
|
| 78 |
if not uploads:
|
| 79 |
raise HTTPException(status_code=400, detail={"error": "image file is required"})
|
config.json
CHANGED
|
@@ -11,5 +11,15 @@
|
|
| 11 |
"warning"
|
| 12 |
],
|
| 13 |
"proxy": "",
|
| 14 |
-
"base_url": ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
}
|
|
|
|
| 11 |
"warning"
|
| 12 |
],
|
| 13 |
"proxy": "",
|
| 14 |
+
"base_url": "",
|
| 15 |
+
"sensitive_words": [
|
| 16 |
+
"色情"
|
| 17 |
+
],
|
| 18 |
+
"ai_review": {
|
| 19 |
+
"enabled": false,
|
| 20 |
+
"base_url": "",
|
| 21 |
+
"api_key": "",
|
| 22 |
+
"model": "gpt-5.4-mini",
|
| 23 |
+
"prompt": "判断用户请求是否允许。只回答 ALLOW 或 REJECT。"
|
| 24 |
+
}
|
| 25 |
}
|
services/config.py
CHANGED
|
@@ -132,6 +132,16 @@ class ConfigStore:
|
|
| 132 |
allowed = {"debug", "info", "warning", "error"}
|
| 133 |
return [level for item in levels if (level := str(item or "").strip().lower()) in allowed]
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
@property
|
| 136 |
def images_dir(self) -> Path:
|
| 137 |
path = DATA_DIR / "images"
|
|
@@ -181,6 +191,8 @@ class ConfigStore:
|
|
| 181 |
data["auto_remove_invalid_accounts"] = self.auto_remove_invalid_accounts
|
| 182 |
data["auto_remove_rate_limited_accounts"] = self.auto_remove_rate_limited_accounts
|
| 183 |
data["log_levels"] = self.log_levels
|
|
|
|
|
|
|
| 184 |
data.pop("auth-key", None)
|
| 185 |
return data
|
| 186 |
|
|
|
|
| 132 |
allowed = {"debug", "info", "warning", "error"}
|
| 133 |
return [level for item in levels if (level := str(item or "").strip().lower()) in allowed]
|
| 134 |
|
| 135 |
+
@property
|
| 136 |
+
def sensitive_words(self) -> list[str]:
|
| 137 |
+
words = self.data.get("sensitive_words")
|
| 138 |
+
return [word for item in words if (word := str(item or "").strip())] if isinstance(words, list) else []
|
| 139 |
+
|
| 140 |
+
@property
|
| 141 |
+
def ai_review(self) -> dict[str, object]:
|
| 142 |
+
value = self.data.get("ai_review")
|
| 143 |
+
return value if isinstance(value, dict) else {}
|
| 144 |
+
|
| 145 |
@property
|
| 146 |
def images_dir(self) -> Path:
|
| 147 |
path = DATA_DIR / "images"
|
|
|
|
| 191 |
data["auto_remove_invalid_accounts"] = self.auto_remove_invalid_accounts
|
| 192 |
data["auto_remove_rate_limited_accounts"] = self.auto_remove_rate_limited_accounts
|
| 193 |
data["log_levels"] = self.log_levels
|
| 194 |
+
data["sensitive_words"] = self.sensitive_words
|
| 195 |
+
data["ai_review"] = self.ai_review
|
| 196 |
data.pop("auth-key", None)
|
| 197 |
return data
|
| 198 |
|
services/content_filter.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from curl_cffi import requests
|
| 4 |
+
from fastapi import HTTPException
|
| 5 |
+
|
| 6 |
+
from services.config import config
|
| 7 |
+
from services.proxy_service import proxy_settings
|
| 8 |
+
|
| 9 |
+
DEFAULT_REVIEW_PROMPT = "判断用户请求是否允许。只回答 ALLOW 或 REJECT。"
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def _text(value: object) -> str:
|
| 13 |
+
if isinstance(value, str):
|
| 14 |
+
return value
|
| 15 |
+
if isinstance(value, list):
|
| 16 |
+
return "\n".join(_text(item) for item in value)
|
| 17 |
+
if isinstance(value, dict):
|
| 18 |
+
return "\n".join(_text(value.get(key)) for key in ("text", "input_text", "content", "input", "instructions", "system", "prompt"))
|
| 19 |
+
return ""
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def request_text(*values: object) -> str:
|
| 23 |
+
return "\n".join(part for value in values if (part := _text(value).strip()))
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def check_request(text: str) -> None:
|
| 27 |
+
text = str(text or "")
|
| 28 |
+
if not text:
|
| 29 |
+
return
|
| 30 |
+
for word in config.sensitive_words:
|
| 31 |
+
if word in text:
|
| 32 |
+
raise HTTPException(status_code=400, detail={"error": "检测到敏感词,拒绝本次任务"})
|
| 33 |
+
review = config.ai_review
|
| 34 |
+
if not review.get("enabled"):
|
| 35 |
+
return
|
| 36 |
+
base_url = str(review.get("base_url") or "").strip().rstrip("/")
|
| 37 |
+
api_key = str(review.get("api_key") or "").strip()
|
| 38 |
+
model = str(review.get("model") or "").strip()
|
| 39 |
+
if not base_url or not api_key or not model:
|
| 40 |
+
raise HTTPException(status_code=400, detail={"error": "ai review config is incomplete"})
|
| 41 |
+
prompt = str(review.get("prompt") or DEFAULT_REVIEW_PROMPT).strip()
|
| 42 |
+
content = f"{prompt}\n\n用户请求:\n{text}\n\n只回答 ALLOW 或 REJECT。"
|
| 43 |
+
try:
|
| 44 |
+
response = requests.post(
|
| 45 |
+
f"{base_url}/v1/chat/completions",
|
| 46 |
+
headers={"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"},
|
| 47 |
+
json={"model": model, "messages": [{"role": "user", "content": content}], "temperature": 0},
|
| 48 |
+
timeout=60,
|
| 49 |
+
**proxy_settings.build_session_kwargs(),
|
| 50 |
+
)
|
| 51 |
+
data = response.json()
|
| 52 |
+
result = str(data["choices"][0]["message"]["content"]).strip().lower()
|
| 53 |
+
except Exception as exc:
|
| 54 |
+
raise HTTPException(status_code=502, detail={"error": f"ai review failed: {exc}"}) from exc
|
| 55 |
+
if result.startswith(("allow", "pass", "true", "yes", "通过", "允许", "安全")):
|
| 56 |
+
return
|
| 57 |
+
raise HTTPException(status_code=400, detail={"error": "AI 审核未通过,拒绝本次任务"})
|
web/src/app/settings/components/config-card.tsx
CHANGED
|
@@ -8,6 +8,7 @@ import { Button } from "@/components/ui/button";
|
|
| 8 |
import { Card, CardContent } from "@/components/ui/card";
|
| 9 |
import { Checkbox } from "@/components/ui/checkbox";
|
| 10 |
import { Input } from "@/components/ui/input";
|
|
|
|
| 11 |
import { testProxy, type ProxyTestResult } from "@/lib/api";
|
| 12 |
|
| 13 |
import { useSettingsStore } from "../store";
|
|
@@ -26,6 +27,8 @@ export function ConfigCard() {
|
|
| 26 |
const setLogLevel = useSettingsStore((state) => state.setLogLevel);
|
| 27 |
const setProxy = useSettingsStore((state) => state.setProxy);
|
| 28 |
const setBaseUrl = useSettingsStore((state) => state.setBaseUrl);
|
|
|
|
|
|
|
| 29 |
const saveConfig = useSettingsStore((state) => state.saveConfig);
|
| 30 |
|
| 31 |
const handleTestProxy = async () => {
|
|
@@ -167,6 +170,43 @@ export function ConfigCard() {
|
|
| 167 |
))}
|
| 168 |
</div>
|
| 169 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
</div>
|
| 171 |
|
| 172 |
<div className="flex justify-end">
|
|
|
|
| 8 |
import { Card, CardContent } from "@/components/ui/card";
|
| 9 |
import { Checkbox } from "@/components/ui/checkbox";
|
| 10 |
import { Input } from "@/components/ui/input";
|
| 11 |
+
import { Textarea } from "@/components/ui/textarea";
|
| 12 |
import { testProxy, type ProxyTestResult } from "@/lib/api";
|
| 13 |
|
| 14 |
import { useSettingsStore } from "../store";
|
|
|
|
| 27 |
const setLogLevel = useSettingsStore((state) => state.setLogLevel);
|
| 28 |
const setProxy = useSettingsStore((state) => state.setProxy);
|
| 29 |
const setBaseUrl = useSettingsStore((state) => state.setBaseUrl);
|
| 30 |
+
const setSensitiveWordsText = useSettingsStore((state) => state.setSensitiveWordsText);
|
| 31 |
+
const setAIReviewField = useSettingsStore((state) => state.setAIReviewField);
|
| 32 |
const saveConfig = useSettingsStore((state) => state.saveConfig);
|
| 33 |
|
| 34 |
const handleTestProxy = async () => {
|
|
|
|
| 170 |
))}
|
| 171 |
</div>
|
| 172 |
</div>
|
| 173 |
+
<div className="space-y-2 md:col-span-2">
|
| 174 |
+
<label className="text-sm text-stone-700">敏感词</label>
|
| 175 |
+
<Textarea
|
| 176 |
+
value={(config?.sensitive_words || []).join("\n")}
|
| 177 |
+
onChange={(event) => setSensitiveWordsText(event.target.value)}
|
| 178 |
+
placeholder="一行一个,命中即拒绝"
|
| 179 |
+
className="min-h-28 rounded-xl border-stone-200 bg-white font-mono text-xs shadow-none"
|
| 180 |
+
/>
|
| 181 |
+
<p className="text-xs text-stone-500">只要用户请求包含任意敏感词,就直接返回拒绝。</p>
|
| 182 |
+
</div>
|
| 183 |
+
<div className="space-y-4 rounded-xl border border-stone-200 bg-white px-4 py-3 md:col-span-2">
|
| 184 |
+
<label className="flex items-center gap-3 text-sm text-stone-700">
|
| 185 |
+
<Checkbox
|
| 186 |
+
checked={Boolean(config?.ai_review?.enabled)}
|
| 187 |
+
onCheckedChange={(checked) => setAIReviewField("enabled", Boolean(checked))}
|
| 188 |
+
/>
|
| 189 |
+
启用 AI 审核
|
| 190 |
+
</label>
|
| 191 |
+
<div className="grid gap-4 md:grid-cols-3">
|
| 192 |
+
<div className="space-y-2">
|
| 193 |
+
<label className="text-sm text-stone-700">Base URL</label>
|
| 194 |
+
<Input value={String(config?.ai_review?.base_url || "")} onChange={(event) => setAIReviewField("base_url", event.target.value)} placeholder="https://api.openai.com" className="h-10 rounded-xl border-stone-200 bg-white" />
|
| 195 |
+
</div>
|
| 196 |
+
<div className="space-y-2">
|
| 197 |
+
<label className="text-sm text-stone-700">API Key</label>
|
| 198 |
+
<Input value={String(config?.ai_review?.api_key || "")} onChange={(event) => setAIReviewField("api_key", event.target.value)} placeholder="sk-..." className="h-10 rounded-xl border-stone-200 bg-white" />
|
| 199 |
+
</div>
|
| 200 |
+
<div className="space-y-2">
|
| 201 |
+
<label className="text-sm text-stone-700">Model</label>
|
| 202 |
+
<Input value={String(config?.ai_review?.model || "")} onChange={(event) => setAIReviewField("model", event.target.value)} placeholder="gpt-4.1-mini" className="h-10 rounded-xl border-stone-200 bg-white" />
|
| 203 |
+
</div>
|
| 204 |
+
</div>
|
| 205 |
+
<div className="space-y-2">
|
| 206 |
+
<label className="text-sm text-stone-700">审核提示词</label>
|
| 207 |
+
<Textarea value={String(config?.ai_review?.prompt || "")} onChange={(event) => setAIReviewField("prompt", event.target.value)} placeholder="判断用户请求是否允许。只回答 ALLOW 或 REJECT。" className="min-h-24 rounded-xl border-stone-200 bg-white text-xs shadow-none" />
|
| 208 |
+
</div>
|
| 209 |
+
</div>
|
| 210 |
</div>
|
| 211 |
|
| 212 |
<div className="flex justify-end">
|
web/src/app/settings/store.ts
CHANGED
|
@@ -37,6 +37,14 @@ function normalizeConfig(config: SettingsConfig): SettingsConfig {
|
|
| 37 |
log_levels: Array.isArray(config.log_levels) ? config.log_levels : [],
|
| 38 |
proxy: typeof config.proxy === "string" ? config.proxy : "",
|
| 39 |
base_url: typeof config.base_url === "string" ? config.base_url : "",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
};
|
| 41 |
}
|
| 42 |
|
|
@@ -98,6 +106,8 @@ type SettingsStore = {
|
|
| 98 |
setLogLevel: (level: string, enabled: boolean) => void;
|
| 99 |
setProxy: (value: string) => void;
|
| 100 |
setBaseUrl: (value: string) => void;
|
|
|
|
|
|
|
| 101 |
|
| 102 |
loadRegister: (silent?: boolean) => Promise<void>;
|
| 103 |
setRegisterConfig: (config: RegisterConfig) => void;
|
|
@@ -202,6 +212,14 @@ export const useSettingsStore = create<SettingsStore>((set, get) => ({
|
|
| 202 |
auto_remove_rate_limited_accounts: Boolean(config.auto_remove_rate_limited_accounts),
|
| 203 |
proxy: config.proxy.trim(),
|
| 204 |
base_url: String(config.base_url || "").trim(),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
});
|
| 206 |
set({
|
| 207 |
config: normalizeConfig(data.config),
|
|
@@ -278,6 +296,14 @@ export const useSettingsStore = create<SettingsStore>((set, get) => ({
|
|
| 278 |
});
|
| 279 |
},
|
| 280 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
loadRegister: async (silent = false) => {
|
| 282 |
if (!silent) set({ isLoadingRegister: true });
|
| 283 |
try {
|
|
|
|
| 37 |
log_levels: Array.isArray(config.log_levels) ? config.log_levels : [],
|
| 38 |
proxy: typeof config.proxy === "string" ? config.proxy : "",
|
| 39 |
base_url: typeof config.base_url === "string" ? config.base_url : "",
|
| 40 |
+
sensitive_words: Array.isArray(config.sensitive_words) ? config.sensitive_words : [],
|
| 41 |
+
ai_review: {
|
| 42 |
+
enabled: Boolean(config.ai_review?.enabled),
|
| 43 |
+
base_url: String(config.ai_review?.base_url || ""),
|
| 44 |
+
api_key: String(config.ai_review?.api_key || ""),
|
| 45 |
+
model: String(config.ai_review?.model || ""),
|
| 46 |
+
prompt: String(config.ai_review?.prompt || ""),
|
| 47 |
+
},
|
| 48 |
};
|
| 49 |
}
|
| 50 |
|
|
|
|
| 106 |
setLogLevel: (level: string, enabled: boolean) => void;
|
| 107 |
setProxy: (value: string) => void;
|
| 108 |
setBaseUrl: (value: string) => void;
|
| 109 |
+
setSensitiveWordsText: (value: string) => void;
|
| 110 |
+
setAIReviewField: (key: "enabled" | "base_url" | "api_key" | "model" | "prompt", value: string | boolean) => void;
|
| 111 |
|
| 112 |
loadRegister: (silent?: boolean) => Promise<void>;
|
| 113 |
setRegisterConfig: (config: RegisterConfig) => void;
|
|
|
|
| 212 |
auto_remove_rate_limited_accounts: Boolean(config.auto_remove_rate_limited_accounts),
|
| 213 |
proxy: config.proxy.trim(),
|
| 214 |
base_url: String(config.base_url || "").trim(),
|
| 215 |
+
sensitive_words: (config.sensitive_words || []).map((item) => String(item).trim()).filter(Boolean),
|
| 216 |
+
ai_review: {
|
| 217 |
+
enabled: Boolean(config.ai_review?.enabled),
|
| 218 |
+
base_url: String(config.ai_review?.base_url || "").trim(),
|
| 219 |
+
api_key: String(config.ai_review?.api_key || "").trim(),
|
| 220 |
+
model: String(config.ai_review?.model || "").trim(),
|
| 221 |
+
prompt: String(config.ai_review?.prompt || "").trim(),
|
| 222 |
+
},
|
| 223 |
});
|
| 224 |
set({
|
| 225 |
config: normalizeConfig(data.config),
|
|
|
|
| 296 |
});
|
| 297 |
},
|
| 298 |
|
| 299 |
+
setSensitiveWordsText: (value) => {
|
| 300 |
+
set((state) => state.config ? { config: { ...state.config, sensitive_words: value.split("\n") } } : {});
|
| 301 |
+
},
|
| 302 |
+
|
| 303 |
+
setAIReviewField: (key, value) => {
|
| 304 |
+
set((state) => state.config ? { config: { ...state.config, ai_review: { ...(state.config.ai_review || {}), [key]: value } } } : {});
|
| 305 |
+
},
|
| 306 |
+
|
| 307 |
loadRegister: async (silent = false) => {
|
| 308 |
if (!silent) set({ isLoadingRegister: true });
|
| 309 |
try {
|
web/src/lib/api.ts
CHANGED
|
@@ -53,6 +53,14 @@ type AccountUpdateResponse = {
|
|
| 53 |
export type SettingsConfig = {
|
| 54 |
proxy: string;
|
| 55 |
base_url?: string;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
refresh_account_interval_minute?: number | string;
|
| 57 |
image_retention_days?: number | string;
|
| 58 |
auto_remove_invalid_accounts?: boolean;
|
|
|
|
| 53 |
export type SettingsConfig = {
|
| 54 |
proxy: string;
|
| 55 |
base_url?: string;
|
| 56 |
+
sensitive_words?: string[];
|
| 57 |
+
ai_review?: {
|
| 58 |
+
enabled?: boolean;
|
| 59 |
+
base_url?: string;
|
| 60 |
+
api_key?: string;
|
| 61 |
+
model?: string;
|
| 62 |
+
prompt?: string;
|
| 63 |
+
};
|
| 64 |
refresh_account_interval_minute?: number | string;
|
| 65 |
image_retention_days?: number | string;
|
| 66 |
auto_remove_invalid_accounts?: boolean;
|