refactor(backend): 迁移基础设施,core,拆分models
This commit is contained in:
1 parent
b89d93d738
commit
053ea392f2
61 files changed
+3329
-3268
No files matched your search
@@ -34,8 +34,9 @@ from typing import Any, List, Optional
|
||||
import requests
|
||||
from flask import current_app
|
||||
|
||||
from app.config import Config
|
||||
from app.models import ChatLog, db
|
||||
from app.core.config.settings import Config
|
||||
from app import db
|
||||
from app.models import ChatLog
|
||||
from app.services.chat_perf import ChatPerfTracker, chat_perf_enabled
|
||||
from app.services.integration_api import IntegrationApiService
|
||||
|
||||
|
||||
@@ -12,7 +12,8 @@ from typing import Any, Optional
|
||||
|
||||
from flask import current_app
|
||||
|
||||
from app.models import ChatLog, QuickQuestion, db
|
||||
from app import db
|
||||
from app.models import ChatLog, QuickQuestion
|
||||
from app.services.faq_text_utils import normalize_faq_query
|
||||
|
||||
|
||||
|
||||
@@ -12,7 +12,8 @@ from typing import Any, Optional
|
||||
|
||||
from flask import current_app
|
||||
|
||||
from app.models import KbDifySyncRecord, QuickQuestion, db
|
||||
from app import db
|
||||
from app.models import KbDifySyncRecord, QuickQuestion
|
||||
from app.services.dify import DifyService
|
||||
from app.services.integration_api import IntegrationApiService
|
||||
|
||||
|
||||
@@ -1,93 +1,93 @@
|
||||
"""␍
|
||||
services/file_check_tools/dify_inputs.py␍
|
||||
␍
|
||||
用途:构建文件检核 Chatflow 的 Dify chat inputs,并在 task_id 缺失时快速失败。␍
|
||||
依赖:task_resolver、agent_resolver、errors.ValidationError。␍
|
||||
逻辑:chat.py 在调 Dify 前调用本模块;缺失 schedule_task_id 时抛 ValidationError,避免 Dify 长连接挂死。␍
|
||||
"""␍
|
||||
from __future__ import annotations␍
|
||||
␍
|
||||
from app.errors import ValidationError␍
|
||||
from app.services.file_check_tools.task_resolver import resolve_schedule_task_id_from_agent␍
|
||||
␍
|
||||
# Dify Chatflow「用户输入」变量名 — 须与画布 Start 节点配置一致␍
|
||||
FILE_CHECK_TASK_ID_INPUT = "task_id"␍
|
||||
FILE_CHECK_TASK_ID_MISSING_CODE = "file_check_task_id_required"␍
|
||||
␍
|
||||
␍
|
||||
def _missing_task_id_error(agent) -> ValidationError:␍
|
||||
"""␍
|
||||
构造 task_id 未配置时的业务异常。␍
|
||||
␍
|
||||
核心逻辑:返回可操作的配置提示,供管理后台与前端直接展示。␍
|
||||
"""␍
|
||||
return ValidationError(␍
|
||||
"文件检核 Agent 未配置有效的 schedule_task_id,无法调用 Dify Chatflow。"␍
|
||||
"请在管理后台编辑该 Agent,于 extra_config 中设置 "␍
|
||||
"tool_run_progress.schedule_task_id(绑定排程任务 ID)。",␍
|
||||
details={␍
|
||||
"error_code": FILE_CHECK_TASK_ID_MISSING_CODE,␍
|
||||
"agent_id": getattr(agent, "id", None),␍
|
||||
"agent_name": getattr(agent, "name", None) or "",␍
|
||||
"config_hint": {␍
|
||||
"capabilities": ["tool_run_progress"],␍
|
||||
"tool_run_progress": {␍
|
||||
"type": "file_check",␍
|
||||
"schedule_task_id": "<ScheduleTask.id>",␍
|
||||
},␍
|
||||
},␍
|
||||
},␍
|
||||
)␍
|
||||
␍
|
||||
␍
|
||||
def ensure_file_check_task_id_configured(agent) -> int:␍
|
||||
"""␍
|
||||
解析并校验文件检核 Agent 绑定的 schedule_task_id。␍
|
||||
␍
|
||||
Args:␍
|
||||
agent: 当前对话 Agent。␍
|
||||
␍
|
||||
Returns:␍
|
||||
有效的 ScheduleTask.id。␍
|
||||
␍
|
||||
Raises:␍
|
||||
ValidationError: extra_config 未配置或 schedule_task_id 非法。␍
|
||||
"""␍
|
||||
task_id = resolve_schedule_task_id_from_agent(agent)␍
|
||||
if not task_id:␍
|
||||
raise _missing_task_id_error(agent)␍
|
||||
return task_id␍
|
||||
␍
|
||||
␍
|
||||
def build_file_check_dify_inputs(agent, *, required: bool = False) -> dict[str, str]:␍
|
||||
"""␍
|
||||
构建文件检核 Chatflow 的 Dify chat inputs。␍
|
||||
␍
|
||||
仅注入 task_id。hxops_session_id / hxops_turn_id 由 chat.py 登记 turn 注册表,␍
|
||||
Tool API 启动时通过 user_message 推断,无需传入 Dify 画布。␍
|
||||
␍
|
||||
Args:␍
|
||||
agent: 当前对话 Agent;task_id 从其 extra_config 解析。␍
|
||||
required: 为 True 时若 task_id 缺失立即抛 ValidationError,避免 Dify 流式挂死。␍
|
||||
␍
|
||||
Returns:␍
|
||||
含 task_id 的 inputs 字典;required=False 且未配置时返回空 dict。␍
|
||||
␍
|
||||
Raises:␍
|
||||
ValidationError: required=True 且 schedule_task_id 未配置。␍
|
||||
"""␍
|
||||
task_id = resolve_schedule_task_id_from_agent(agent)␍
|
||||
if not task_id:␍
|
||||
if required:␍
|
||||
raise _missing_task_id_error(agent)␍
|
||||
return {}␍
|
||||
return {FILE_CHECK_TASK_ID_INPUT: str(task_id)}␍
|
||||
␍
|
||||
␍
|
||||
__all__ = [␍
|
||||
"FILE_CHECK_TASK_ID_INPUT",␍
|
||||
"FILE_CHECK_TASK_ID_MISSING_CODE",␍
|
||||
"build_file_check_dify_inputs",␍
|
||||
"ensure_file_check_task_id_configured",␍
|
||||
]␍
|
||||
"""
|
||||
services/file_check_tools/dify_inputs.py
|
||||
|
||||
用途:构建文件检核 Chatflow 的 Dify chat inputs,并在 task_id 缺失时快速失败。
|
||||
依赖:task_resolver、agent_resolver、errors.ValidationError。
|
||||
逻辑:chat.py 在调 Dify 前调用本模块;缺失 schedule_task_id 时抛 ValidationError,避免 Dify 长连接挂死。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from app.core.errors import ValidationError
|
||||
from app.services.file_check_tools.task_resolver import resolve_schedule_task_id_from_agent
|
||||
|
||||
# Dify Chatflow「用户输入」变量名 — 须与画布 Start 节点配置一致
|
||||
FILE_CHECK_TASK_ID_INPUT = "task_id"
|
||||
FILE_CHECK_TASK_ID_MISSING_CODE = "file_check_task_id_required"
|
||||
|
||||
|
||||
def _missing_task_id_error(agent) -> ValidationError:
|
||||
"""
|
||||
构造 task_id 未配置时的业务异常。
|
||||
|
||||
核心逻辑:返回可操作的配置提示,供管理后台与前端直接展示。
|
||||
"""
|
||||
return ValidationError(
|
||||
"文件检核 Agent 未配置有效的 schedule_task_id,无法调用 Dify Chatflow。"
|
||||
"请在管理后台编辑该 Agent,于 extra_config 中设置 "
|
||||
"tool_run_progress.schedule_task_id(绑定排程任务 ID)。",
|
||||
details={
|
||||
"error_code": FILE_CHECK_TASK_ID_MISSING_CODE,
|
||||
"agent_id": getattr(agent, "id", None),
|
||||
"agent_name": getattr(agent, "name", None) or "",
|
||||
"config_hint": {
|
||||
"capabilities": ["tool_run_progress"],
|
||||
"tool_run_progress": {
|
||||
"type": "file_check",
|
||||
"schedule_task_id": "<ScheduleTask.id>",
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def ensure_file_check_task_id_configured(agent) -> int:
|
||||
"""
|
||||
解析并校验文件检核 Agent 绑定的 schedule_task_id。
|
||||
|
||||
Args:
|
||||
agent: 当前对话 Agent。
|
||||
|
||||
Returns:
|
||||
有效的 ScheduleTask.id。
|
||||
|
||||
Raises:
|
||||
ValidationError: extra_config 未配置或 schedule_task_id 非法。
|
||||
"""
|
||||
task_id = resolve_schedule_task_id_from_agent(agent)
|
||||
if not task_id:
|
||||
raise _missing_task_id_error(agent)
|
||||
return task_id
|
||||
|
||||
|
||||
def build_file_check_dify_inputs(agent, *, required: bool = False) -> dict[str, str]:
|
||||
"""
|
||||
构建文件检核 Chatflow 的 Dify chat inputs。
|
||||
|
||||
仅注入 task_id。hxops_session_id / hxops_turn_id 由 chat.py 登记 turn 注册表,
|
||||
Tool API 启动时通过 user_message 推断,无需传入 Dify 画布。
|
||||
|
||||
Args:
|
||||
agent: 当前对话 Agent;task_id 从其 extra_config 解析。
|
||||
required: 为 True 时若 task_id 缺失立即抛 ValidationError,避免 Dify 流式挂死。
|
||||
|
||||
Returns:
|
||||
含 task_id 的 inputs 字典;required=False 且未配置时返回空 dict。
|
||||
|
||||
Raises:
|
||||
ValidationError: required=True 且 schedule_task_id 未配置。
|
||||
"""
|
||||
task_id = resolve_schedule_task_id_from_agent(agent)
|
||||
if not task_id:
|
||||
if required:
|
||||
raise _missing_task_id_error(agent)
|
||||
return {}
|
||||
return {FILE_CHECK_TASK_ID_INPUT: str(task_id)}
|
||||
|
||||
|
||||
__all__ = [
|
||||
"FILE_CHECK_TASK_ID_INPUT",
|
||||
"FILE_CHECK_TASK_ID_MISSING_CODE",
|
||||
"build_file_check_dify_inputs",
|
||||
"ensure_file_check_task_id_configured",
|
||||
]
|
||||
␍
|
||||
@@ -11,7 +11,7 @@ import re
|
||||
from typing import Any
|
||||
|
||||
from app import db
|
||||
from app.errors import NotFoundError, ServiceUnavailableError, ValidationError
|
||||
from app.core.errors import NotFoundError, ServiceUnavailableError, ValidationError
|
||||
from app.models import KbGroup, ScheduleTask
|
||||
from app.services.knowledge_cleaning.dify_sync_service import KbDifySyncError, KbDifySyncService
|
||||
|
||||
|
||||
@@ -40,7 +40,8 @@ from typing import Any, Optional
|
||||
from flask import current_app
|
||||
from sqlalchemy.orm import joinedload
|
||||
|
||||
from app.models import Agent, ChatLog, QuickQuestion, db
|
||||
from app import db
|
||||
from app.models import Agent, ChatLog, QuickQuestion
|
||||
from app.services.faq_hot_settings import read_faq_hot_env
|
||||
from app.services.faq_text_utils import normalize_faq_query
|
||||
from app.services.hot_faq_candidate_store import HotFaqCandidateStore
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
backend/app/services/hot_faq_candidate_store.py
|
||||
|
||||
用途:热点 FAQ 候选池 PostgreSQL 读写(离线聚合结果,不含在线命中)。
|
||||
依赖:models.FaqHotCandidate、Agent、faq_hot_settings、SQLAlchemy db。
|
||||
依赖:models.FaqHotCandidate、Agent、faq_hot_settings、app.extensions.db。
|
||||
逻辑:HotFaqAggregator 写入;管理端 API 读取 / 晋升 / 忽略。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
@@ -13,7 +13,8 @@ from typing import Any, Optional
|
||||
from flask import current_app
|
||||
from sqlalchemy import desc, or_
|
||||
|
||||
from app.models import Agent, FaqHotCandidate, db
|
||||
from app import db
|
||||
from app.models import Agent, FaqHotCandidate
|
||||
from app.services.faq_hot_settings import read_faq_hot_env
|
||||
|
||||
|
||||
|
||||
@@ -445,7 +445,8 @@ WHERE c002001 = ?
|
||||
latency,
|
||||
)
|
||||
|
||||
from app.models import ChatLog, db
|
||||
from app import db
|
||||
from app.models import ChatLog
|
||||
|
||||
chat_log_id = None
|
||||
try:
|
||||
|
||||
@@ -8,7 +8,7 @@ from urllib.parse import urlparse
|
||||
import requests
|
||||
from flask import current_app, has_app_context, has_request_context, request
|
||||
|
||||
from app.config import Config
|
||||
from app.core.config.settings import Config
|
||||
|
||||
|
||||
class IntegrationApiService:
|
||||
|
||||
@@ -9,7 +9,7 @@ import os
|
||||
from flask import current_app
|
||||
|
||||
from app import db
|
||||
from app.env_persistence import persist_env_settings
|
||||
from app.core.config.env_persistence import persist_env_settings
|
||||
from app.models import AppSetting, KbDifySyncRecord, KbGroup, KbJob, KbDocumentVersion
|
||||
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ from typing import BinaryIO
|
||||
from minio import Minio
|
||||
from minio.error import S3Error
|
||||
from werkzeug.utils import secure_filename
|
||||
from app.config import config, ACTIVE_ENV_NAME
|
||||
from app.core.config.settings import config, ACTIVE_ENV_NAME
|
||||
|
||||
cfg = config[ACTIVE_ENV_NAME]
|
||||
|
||||
|
||||
@@ -8,7 +8,8 @@ from urllib.parse import quote
|
||||
|
||||
from flask import current_app
|
||||
|
||||
from app.models import ChatLog, db
|
||||
from app import db
|
||||
from app.models import ChatLog
|
||||
from app.services.text2sql import Text2SQLService
|
||||
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ from sqlalchemy.orm import joinedload
|
||||
|
||||
from app import db
|
||||
from app.models import ScheduleRun, ScheduleTask
|
||||
from app.response_utils import utc_iso
|
||||
from app.core.response import utc_iso
|
||||
from app.services.schedule.timezone_util import (
|
||||
DEFAULT_SCHEDULE_TIMEZONE,
|
||||
compute_next_run_at,
|
||||
|
||||
@@ -2164,7 +2164,8 @@ ORDER BY faj.faj02
|
||||
return
|
||||
|
||||
# Create ChatLog
|
||||
from app.models import ChatLog, db
|
||||
from app import db
|
||||
from app.models import ChatLog
|
||||
chat_log_id = None
|
||||
try:
|
||||
chat_log = ChatLog(
|
||||
|
||||
Reference in new issue
Block a user