refactor(Dockerfile): 使用requirements.txt替代硬编码依赖 将Dockerfile中的硬编码pip包列表替换为通过requirements.txt文件安装, 提高依赖管理的灵活性和可维护性。 feat(scheduling): 移除内置APScheduler,采用独立调度系统 移除app/core/background/scheduler.py中原来的APScheduler实现, 改为使用新的应用级调度系统app.application.scheduling。 refactor(task_queue): 调整任务队列模块结构和导入路径 将任务队列相关常量从app.core.background.task_queue.constants迁移至 app.tasks.constants,并更新所有相关导入路径和引用。 refactor(events): 将事件服务重构为独立的应用层组件 将事件分发逻辑从核心层迁移到应用层,使用app.application.events.EventDispatchService 替代原有的app.modules.events.services.EventService。 feat(ai_memory): 增强AI记忆自动写入的安全策略 新增ai_memory_blocked_content_terms配置项用于阻止敏感内容, 添加TTL过期机制控制自动写入条目的生命周期。 fix(security): 强化生产环境安全验证机制 增加model_validator确保生产环境中数据库连接、API密钥、CORS设置等 关键安全配置符合要求。 feat(risks): 优化风险事件操作动作的外键约束 为RiskEventAction模型的风险事件ID字段添加外键约束, 防止孤立记录并增强数据完整性。 refactor(audit): 优化审计服务方法命名和事务处理 将AuditService的log方法重命名为record以反映其阶段行为, 并调整事务提交时机以提高性能。 feat(events): 增强领域事件并发处理和响应模型 添加事件锁定机制防止重复处理,更新API响应模型以提供 更准确的数据类型定义。 ```
142 lines
4.5 KiB
Python
142 lines
4.5 KiB
Python
import re
|
|
from typing import Any
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.application.feishu.delivery import send_text_if_configured
|
|
from app.application.feishu.results import command_result
|
|
from app.core.config import get_settings
|
|
from app.modules.ai_agent.constants import AIResponseKey
|
|
from app.modules.feishu.constants import FeishuCommandName, FeishuReplyType
|
|
from app.modules.feishu.service import FeishuService
|
|
from app.modules.reports.chart import lifecycle_chart_alt, render_lifecycle_chart
|
|
from app.modules.reports.constants import ReportResponseKey
|
|
from app.modules.reports.services import ReportService
|
|
|
|
PROJECT_FINANCE_PATTERN = re.compile(r"^项目资金\s+(.+)$")
|
|
FINANCE_COMMANDS = {"资金需求", "未来30天资金需求"}
|
|
|
|
|
|
def handle_finance_command(
|
|
db: Session,
|
|
feishu: FeishuService,
|
|
command_text: str,
|
|
chat_id: str | None,
|
|
actor: str,
|
|
auto_reply: bool,
|
|
) -> dict[str, Any] | None:
|
|
"""Handle project cash-needs commands."""
|
|
|
|
project_match = PROJECT_FINANCE_PATTERN.fullmatch(command_text)
|
|
if command_text not in FINANCE_COMMANDS and project_match is None:
|
|
return None
|
|
command = (
|
|
FeishuCommandName.PROJECT_FINANCE if project_match else FeishuCommandName.FINANCE_NEEDS
|
|
)
|
|
if not get_settings().finance_needs_enabled:
|
|
return _text_result(
|
|
feishu,
|
|
command,
|
|
"项目资金需求分析",
|
|
"项目资金需求分析尚未启用,请先配置并启用财务只读同步。",
|
|
chat_id,
|
|
actor,
|
|
auto_reply,
|
|
)
|
|
|
|
project_code = project_match.group(1).strip() if project_match else None
|
|
service = ReportService(db)
|
|
preview = service.project_finance_needs_report(
|
|
project_code=project_code,
|
|
include_ai=False,
|
|
actor=actor,
|
|
)
|
|
if project_code and not preview["items"]:
|
|
return _text_result(
|
|
feishu,
|
|
command,
|
|
"项目资金需求分析",
|
|
f"未找到项目“{project_code}”,请使用稳定项目编号或展示编号。",
|
|
chat_id,
|
|
actor,
|
|
auto_reply,
|
|
)
|
|
if not preview["summary"]["data_available"]:
|
|
return _text_result(
|
|
feishu,
|
|
command,
|
|
"项目资金需求分析",
|
|
"项目财务数据未接入或无有效记录,暂不生成资金分析报告。",
|
|
chat_id,
|
|
actor,
|
|
auto_reply,
|
|
)
|
|
|
|
report = service.project_finance_needs_report(
|
|
project_code=project_code,
|
|
include_ai=True,
|
|
actor=actor,
|
|
)
|
|
ai_analysis = report.get("ai_analysis") or {}
|
|
if not ai_analysis.get(AIResponseKey.OK):
|
|
return _text_result(
|
|
feishu,
|
|
command,
|
|
"AI 暂不可用",
|
|
"AI 当前不可用,本次项目资金分析报告未发送。请检查模型服务。",
|
|
chat_id,
|
|
actor,
|
|
auto_reply,
|
|
)
|
|
|
|
provider_response = None
|
|
if auto_reply:
|
|
provider_response = _send_finance_card(feishu, chat_id, report, actor)
|
|
return command_result(
|
|
command,
|
|
FeishuReplyType.CARD,
|
|
report[ReportResponseKey.TITLE],
|
|
report[ReportResponseKey.CONTENT],
|
|
provider_response,
|
|
report[ReportResponseKey.LINES],
|
|
)
|
|
|
|
|
|
def _text_result(
|
|
feishu: FeishuService,
|
|
command: FeishuCommandName,
|
|
title: str,
|
|
content: str,
|
|
chat_id: str | None,
|
|
actor: str,
|
|
auto_reply: bool,
|
|
) -> dict[str, Any]:
|
|
response = send_text_if_configured(feishu, chat_id, content, actor) if auto_reply else None
|
|
return command_result(command, FeishuReplyType.TEXT, title, content, response)
|
|
|
|
|
|
def _send_finance_card(
|
|
feishu: FeishuService,
|
|
chat_id: str | None,
|
|
report: dict[str, Any],
|
|
actor: str,
|
|
) -> dict[str, Any] | None:
|
|
settings = get_settings()
|
|
if not (settings.feishu_app_id and settings.feishu_app_secret):
|
|
return None
|
|
chart_data = {
|
|
"period": report.get("as_of"),
|
|
"finance": report.get("finance_chart_data"),
|
|
}
|
|
image_result = feishu.upload_image(render_lifecycle_chart(chart_data), actor)
|
|
image_key = (image_result.get("data") or {}).get("image_key")
|
|
if not image_key:
|
|
raise ValueError("Feishu image upload did not return image_key")
|
|
card = FeishuService.build_basic_card(
|
|
report[ReportResponseKey.TITLE],
|
|
report[ReportResponseKey.LINES],
|
|
image_key=image_key,
|
|
image_alt=lifecycle_chart_alt(chart_data),
|
|
)
|
|
return feishu.send_card(card, receive_id=chat_id, actor=actor)
|