本次提交包含多项优化与新增功能: 1. 清理多个文件中多余的空行与导入顺序 2. 修复voice.py中的多行字符串格式化问题 3. 新增微信公众号被动回复构建函数与配置项 4. 新增企业微信markdown消息发送支持 5. 新增消息去重TTL与最大条目配置 6. 新增markdown文本截断工具函数 7. 新增微信授权与OAuth相关工具方法 8. 重构消息去重逻辑,使用DedupPolicy替代本地字典实现 9. 新增子账号多租户支持功能 10. 新增消息动作处理适配器,支持send/reply等操作 11. 修复token持久化逻辑,新增状态存储支持
45 lines
1.7 KiB
Python
45 lines
1.7 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
PROMPT_PREFIXES = {
|
|
"wecom": "你是一个企业微信 AI 助手,通过企业微信与用户沟通。请保持专业、简洁,使用中文回复。",
|
|
"mp": "你是一个微信公众号 AI 助手,通过公众号消息与用户沟通。请保持友好、专业,使用中文回复。",
|
|
"personal": "你是一个微信 AI 助手,通过个人微信与用户沟通。请保持自然、亲切,使用中文回复。",
|
|
}
|
|
|
|
|
|
class WeChatAgentPromptAdapter:
|
|
def __init__(self):
|
|
self._custom_prefix: str | None = None
|
|
self._enabled: bool = True
|
|
|
|
def configure(self, config: dict[str, Any], mode: str = "wecom") -> None:
|
|
self._enabled = config.get("agent_prompt", {}).get("enabled", True)
|
|
self._custom_prefix = config.get("agent_prompt", {}).get("prefix")
|
|
if not self._custom_prefix:
|
|
self._custom_prefix = PROMPT_PREFIXES.get(mode, "")
|
|
|
|
def get_channel_prefix(self, config: dict[str, Any], mode: str = "wecom") -> str:
|
|
if not self._enabled:
|
|
return ""
|
|
if self._custom_prefix:
|
|
return self._custom_prefix
|
|
return PROMPT_PREFIXES.get(mode, "")
|
|
|
|
@staticmethod
|
|
def inject_prompt(base_prompt: str, channel_prefix: str) -> str:
|
|
if not channel_prefix:
|
|
return base_prompt
|
|
return f"{channel_prefix}\n\n{base_prompt}"
|
|
|
|
@staticmethod
|
|
def strip_channel_prefix(prompt: str, mode: str = "wecom") -> str:
|
|
prefix = PROMPT_PREFIXES.get(mode, "")
|
|
if prefix and prompt.startswith(prefix):
|
|
return prompt[len(prefix) :].strip()
|
|
return prompt
|
|
|
|
def is_enabled(self) -> bool:
|
|
return self._enabled
|