Files
company-ai-platform/app/modules/ai_agent/constants.py
JiuContinent fbd0aaa9e4 ```
refactor(api): 使用常量替代硬编码字符串

- 在health_check接口中使用ApiResponseKey.STATUS和ApiStatus.OK常量
- 替换硬编码的状态返回值为枚举常量

refactor(core): 配置模块错误信息统一使用常量

- 从constants模块导入ConfigErrorDetail并替换CORS_ORIGINS和LEGACY_ALLOWED_QUERIES的验证错误信息
- 配置类中的默认值使用constants中定义的常量

feat(constants): 添加API响应、安全错误和配置错误常量类

- 新增ApiResponseKey用于API状态键名
- 新增ApiStatus用于API状态值
- 新增SecurityErrorDetail用于安全认证错误详情
- 新增ConfigErrorDetail用于配置验证错误详情
- 添加DEFAULT_MODEL_PROVIDER和DEFAULT_OPENCLAW_ACTION_JSON常量

refactor(security): 安全认证模块使用错误常量

- 将硬编码的安全错误信息替换为SecurityErrorDetail常量
- 包括API密钥、审批密钥和审计密钥的相关错误信息

refactor(ai-agent): AI代理适配器改进错误处理

- 将HTTP状态码替换为FastAPI状态常量
- 添加OpenClaw工具和操作的错误常量
- 修复健康检查和工具调用中的状态码比较逻辑
- 添加AIToolAuditKey用于工具审计键名

feat(ai-agent): 扩展AI代理常量定义

- 新增AIToolAuditKey用于工具审计字段
- 添加OpenClaw相关的错误常量如OPENCLAW_CHAT_PROVIDER_REQUIRED等
- 添加UNSUPPORTED_AI_SKILL_TEMPLATE模板字符串

refactor(approvals): 审批模块常量化重构

- 新增ApprovalPayloadKey用于审批载荷字段
- 添加approval_action函数和APPROVAL_ACTION_SEPARATOR分隔符
- 使用常量替换字面量值

feat(audit): 审计模块新增飞书事件动作类型

- 添加FEISHU_WEBHOOK_EVENT和FEISHU_LONG_CONNECTION_EVENT审计动作

refactor(business): 业务模块全面常量化

- 新增BusinessDomain枚举包含所有业务域
- 添加BusinessResponseKey、BusinessPayloadKey等常量类
- 重构DOMAIN_MODELS为frozenset以提高性能
- 添加normalize_domain等辅助函数用于域标准化
- 使用常量替换路由和业务服务中的硬编码字符串
- 添加业务错误常量和字段验证模板

refactor(feishu): 飞书客户端错误处理优化

- 将HTTP状态码替换为FastAPI标准状态常量
- 改进错误处理的一致性

refactor(approvals): 审批服务使用新常量结构

- 使用ApprovalPayloadKey常量重构载荷字段
- 使用approval_action函数统一动作命名格式
- 优化高风险域判断逻辑
```
2026-07-06 15:56:43 +08:00

165 lines
4.1 KiB
Python

from enum import StrEnum
class AIProviderName(StrEnum):
NOOP = "noop"
OPENCLAW = "openclaw"
HERMES = "hermes"
OPENCLAW_HERMES = "openclaw_hermes"
OPENCLAW_HERMES_DASH = "openclaw-hermes"
HYBRID = "hybrid"
DIRECT_LLM = "direct_llm"
class AIResponseKey(StrEnum):
PROVIDER = "provider"
ANSWER = "answer"
RAW = "raw"
RESULT = "result"
OK = "ok"
ERROR = "error"
MESSAGE = "message"
TYPE = "type"
BASE_URL = "base_url"
HEALTH = "health"
HEALTHZ = "healthz"
READYZ = "readyz"
DATA = "data"
STATUS_CODE = "status_code"
TEXT = "text"
PIPELINE = "pipeline"
HERMES_RECALL = "hermes_recall"
HERMES_ANSWER = "hermes_answer"
HERMES_REMEMBER = "hermes_remember"
OPENCLAW = "openclaw"
TOOL = "tool"
TOOL_INVOKED = "tool_invoked"
TOOL_ERROR = "tool_error"
class AIRequestKey(StrEnum):
PROMPT = "prompt"
CONTEXT = "context"
class AIContextKey(StrEnum):
OPENCLAW_TOOL = "openclaw_tool"
OPENCLAW_ACTION = "openclaw_action"
OPENCLAW_ARGS = "openclaw_args"
OPENCLAW_SESSION_KEY = "openclaw_session_key"
AGENT_PIPELINE = "agent_pipeline"
HERMES_MEMORY = "hermes_memory"
OPENCLAW = "openclaw"
MODE = "mode"
USER_PROMPT = "user_prompt"
REQUEST_CONTEXT = "request_context"
ASSISTANT_ANSWER = "assistant_answer"
class AIMemoryMode(StrEnum):
RECALL = "memory_recall"
WRITE = "memory_write"
class AIHttpPath(StrEnum):
CHAT_COMPLETIONS = "/chat/completions"
HEALTH = "/health"
HEALTHZ = "/healthz"
READYZ = "/readyz"
TOOLS_INVOKE = "/tools/invoke"
V1 = "/v1"
class AIHttpHeader(StrEnum):
AUTHORIZATION = "Authorization"
HERMES_SESSION_ID = "X-Hermes-Session-Id"
class AIHttpPayloadKey(StrEnum):
MODEL = "model"
MESSAGES = "messages"
ROLE = "role"
CONTENT = "content"
STREAM = "stream"
TOOL = "tool"
ACTION = "action"
ARGS = "args"
SESSION_KEY = "sessionKey"
CHOICES = "choices"
MESSAGE = "message"
class AIToolAuditKey(StrEnum):
TOOL = "tool"
ACTION = "action"
ARGS = "args"
SESSION_KEY = "session_key"
class AIChatRole(StrEnum):
SYSTEM = "system"
USER = "user"
class AIDefault(StrEnum):
ACTION_JSON = "json"
SESSION_KEY_MAIN = "main"
class AIRiskPreference(StrEnum):
BALANCED = "balanced"
class AIErrorKey(StrEnum):
OPENCLAW = "openclaw_error"
HERMES = "hermes_error"
DIRECT_LLM = "llm_error"
OPENCLAW_HERMES_PIPELINE = (
"hermes_recall -> openclaw_gateway -> hermes_answer -> hermes_remember"
)
COMPANY_MANAGEMENT_SYSTEM_INSTRUCTIONS = (
"You are a company management AI. Be concise, cite data from context, "
"and never approve payments, performance changes, or trades automatically."
)
CHAT_USER_CONTENT_TEMPLATE = "Context:\n{context}\n\nTask:\n{task}"
AUTHORIZATION_BEARER_TEMPLATE = "Bearer {token}"
NOOP_PROVIDER_ANSWER = (
"AI provider is not configured yet. This is a deterministic placeholder. "
"Set MODEL_PROVIDER to openclaw_hermes, openclaw, hermes, or direct_llm "
"after credentials are ready."
)
OPENCLAW_TOOL_COMPLETED_ANSWER = "OpenClaw tool invocation completed."
DIRECT_LLM_API_KEY_MISSING = "DIRECT_LLM_API_KEY is not configured"
UNEXPECTED_HERMES_RESPONSE = "Unexpected chat completion response"
OPENCLAW_CHAT_PROVIDER_REQUIRED = (
"OpenClaw Gateway is not configured as a chat provider. "
"Provide context.openclaw_tool for /tools/invoke, or use "
"MODEL_PROVIDER=hermes/openclaw_hermes for AI answers."
)
OPENCLAW_TOOL_NOT_ALLOWED = "OpenClaw tool is not allowed"
OPENCLAW_ACTION_NOT_ALLOWED = "OpenClaw action is not allowed"
UNSUPPORTED_AI_SKILL_TEMPLATE = "Unsupported AI skill: {skill_id}"
AI_AUDIT_REDACTED_VALUE = "[REDACTED]"
AI_AUDIT_TRUNCATED_VALUE = "[TRUNCATED]"
AI_AUDIT_MAX_TEXT_LENGTH = 1000
AI_AUDIT_MAX_SEQUENCE_ITEMS = 20
AI_AUDIT_MAX_DEPTH = 4
AI_AUDIT_SENSITIVE_KEYS = frozenset(
{
"authorization",
"api_key",
"apikey",
"access_token",
"tenant_access_token",
"token",
"secret",
"password",
"openclaw_gateway_token",
"hermes_api_key",
"direct_llm_api_key",
}
)