Files
company-ai-platform/app/modules/feishu/commands.py
JiuContinent d82116d637 ```
feat(ai_agent): 新增openclaw_hermes混合AI适配器

新增OpenClawHermesAdapter适配器,结合Hermes记忆功能和OpenClaw执行能力,
实现AI问答流程中的记忆召回、执行操作和记忆存储的完整闭环。
同时更新NoopAdapter提示信息,添加新的模型提供商选项。

feat(business): 新增考勤、工作报告和风险事件业务模型

新增AttendanceRecord、WorkReport、RiskEvent和LegacySyncRun四个业务模型,
扩展业务领域注册表,支持考勤管理、工作报告生成和风险事件跟踪等核心业务功能。

feat(reports): 实现考勤汇总和工作日报周报生成功能

新增attendance_summary方法用于统计每日考勤情况,
新增generate_work_report方法用于生成日/周经营报告,
包含任务完成情况、待处理事项和风险指标等综合信息。

feat(risk): 扩展风险管理API端点和供应商风险检测

新增供应商风险查询端点和风险事件管理端点,
提供风险事件列表查询和自动生成功能,
增强供应商风险评估能力。

feat(feishu): 添加考勤查询命令和风险摘要增强

集成考勤汇总查询功能到飞书命令系统,
在风险摘要中添加供应商风险和开放风险事件统计,
丰富日常经营管理信息展示。

refactor(service): 优化业务服务数据验证和类型转换

重构_model_payload函数实现数据验证和类型转换,
添加列值类型强制转换逻辑,提高API数据处理的准确性和安全性。

build(deps): 添加postgresql数据库驱动依赖

在Dockerfile中添加psycopg[binary]==3.2.3依赖包,
支持PostgreSQL数据库连接和操作。

chore(config): 更新.gitignore文件排除备份和迁移目录

在.gitignore中添加AGENTS.md.bak-*和migration/目录排除规则,
避免备份文件和本地迁移工作区被提交到版本控制系统。
```
2026-06-24 10:30:59 +08:00

198 lines
7.2 KiB
Python

import json
import re
from typing import Any
from sqlalchemy.orm import Session
from app.core.config import get_settings
from app.modules.ai_agent.service import AIService
from app.modules.feishu.service import FeishuService
from app.modules.reports.service import ReportService
from app.modules.risk.service import RiskService
def _parse_content_text(content: Any) -> str:
"""Extract plain command text from a Feishu message content payload."""
if isinstance(content, dict):
return str(content.get("text") or content.get("content") or "")
if not isinstance(content, str):
return ""
try:
data = json.loads(content)
except json.JSONDecodeError:
return content
if isinstance(data, dict):
return str(data.get("text") or data.get("content") or "")
return content
def _clean_command_text(text: str) -> str:
"""Remove mentions and invisible characters from Feishu command text."""
text = re.sub(r"@\S+", "", text or "")
text = text.replace("\u200b", "")
return text.strip()
class FeishuCommandService:
"""Route Feishu text commands to reports, risk summaries, or AI replies."""
def __init__(self, db: Session):
self.db = db
self.feishu = FeishuService(db)
def extract_event_command(self, payload: dict[str, Any]) -> dict[str, Any] | None:
event = payload.get("event") or {}
message = event.get("message") or {}
if not message:
return None
text = _clean_command_text(_parse_content_text(message.get("content")))
if not text:
return None
sender = event.get("sender") or {}
sender_id = sender.get("sender_id") or {}
actor = sender_id.get("open_id") or sender_id.get("user_id") or "feishu"
return {
"text": text,
"chat_id": message.get("chat_id"),
"actor": actor,
}
def handle_text(
self,
text: str,
chat_id: str | None = None,
actor: str = "feishu",
auto_reply: bool = True,
) -> dict[str, Any]:
command_text = _clean_command_text(text)
lowered = command_text.lower()
provider_response: dict[str, Any] | None = None
if any(keyword in command_text for keyword in ["日报", "晨报", "经营日报", "经营晨报"]):
report = ReportService(self.db).daily_brief()
result = {
"command": "daily_brief",
"reply_type": "card",
"title": report["title"],
"content": report["content"],
"lines": report["lines"],
}
if auto_reply:
provider_response = self._send_card_if_configured(
chat_id,
report["title"],
report["lines"],
actor,
)
result["provider_response"] = provider_response
return result
if any(keyword in command_text for keyword in ["周报", "项目周报"]):
report = ReportService(self.db).project_weekly()
result = {
"command": "project_weekly",
"reply_type": "card",
"title": report["title"],
"content": report["content"],
"lines": report["lines"],
}
if auto_reply:
provider_response = self._send_card_if_configured(
chat_id,
report["title"],
report["lines"],
actor,
)
result["provider_response"] = provider_response
return result
if any(keyword in command_text for keyword in ["打卡", "考勤", "attendance"]):
report = ReportService(self.db).attendance_summary()
result = {
"command": "attendance_summary",
"reply_type": "card",
"title": report["title"],
"content": report["content"],
"lines": report["lines"],
}
if auto_reply:
provider_response = self._send_card_if_configured(
chat_id,
report["title"],
report["lines"],
actor,
)
result["provider_response"] = provider_response
return result
if any(keyword in command_text for keyword in ["风险", "预警", "risk"]):
summary = RiskService(self.db).summary()
lines = [
f"- 综合风险等级:{summary['risk_level']}",
f"- 风险分:{summary['risk_score']}",
f"- 逾期任务:{len(summary['overdue_tasks'])}",
f"- 延期项目:{len(summary['delayed_projects'])}",
f"- 超预算项目:{len(summary['over_budget_projects'])}",
f"- 资金风险账户:{len(summary['fund_risks'])}",
f"- 供应商风险:{len(summary['supplier_risks'])}",
f"- 打开风险事件:{len(summary['open_events'])}",
]
result = {
"command": "risk_summary",
"reply_type": "card",
"title": "风险预警",
"content": "\n".join(lines),
"lines": lines,
}
if auto_reply:
provider_response = self._send_card_if_configured(chat_id, "风险预警", lines, actor)
result["provider_response"] = provider_response
return result
prompt = command_text
for prefix in ["", "ai ", "AI ", "/ask "]:
if command_text.startswith(prefix):
prompt = command_text[len(prefix) :].strip()
break
if not prompt:
prompt = "请说明你能做什么。"
ai_result = AIService(self.db).ask(prompt, context={}, actor=actor, source="feishu")
content = ai_result["answer"]
is_explicit_ai = lowered.startswith(("ai ", "/ask")) or command_text.startswith("")
result = {
"command": "ai_ask" if is_explicit_ai else "fallback_ai",
"reply_type": "text",
"title": "AI 回复",
"content": content,
}
if auto_reply:
provider_response = self._send_text_if_configured(chat_id, content, actor)
result["provider_response"] = provider_response
return result
def _send_card_if_configured(
self,
chat_id: str | None,
title: str,
lines: list[str],
actor: str,
) -> dict[str, Any] | None:
settings = get_settings()
if not (settings.feishu_app_id and settings.feishu_app_secret):
return None
card = FeishuService.build_basic_card(title, lines)
return self.feishu.send_card(card, receive_id=chat_id, actor=actor)
def _send_text_if_configured(
self,
chat_id: str | None,
text: str,
actor: str,
) -> dict[str, Any] | None:
settings = get_settings()
if not (settings.feishu_app_id and settings.feishu_app_secret):
return None
return self.feishu.send_text(text, receive_id=chat_id, actor=actor)