fix(feishu): support file message and use absolute workspace path

This commit is contained in:
zhayujie
2026-05-08 11:31:22 +08:00
parent 55aaf60a57
commit 3e28659528
2 changed files with 39 additions and 2 deletions

View File

@@ -542,6 +542,32 @@ class FeiShuChanel(ChatChannel):
# 单张图片不直接处理,等待用户提问
return
# 如果是文件消息,触发实际下载并缓存,等待用户后续提问时一并带上。
# 与 wecom_bot 行为对齐:发文件后静默缓存(飞书客户端会显示"已读"
# 用户下一条文本消息会自动 attach 上文件路径给 agent。
if feishu_msg.ctype == ContextType.FILE:
try:
feishu_msg.prepare()
# prepare 通过 _prepared 标记保证幂等,重复调用安全
if not os.path.exists(feishu_msg.content):
raise FileNotFoundError(feishu_msg.content)
except Exception as e:
logger.warning(f"[FeiShu] prepare file failed: {e}")
# 文件下载失败时主动通知用户,避免静默丢失
try:
err_reply = Reply(ReplyType.TEXT, f"⚠️ 文件下载失败,请重新发送:{e}")
self._send(err_reply, self._compose_context(
ContextType.TEXT, "",
isgroup=is_group, msg=feishu_msg,
receive_id_type=receive_id_type, no_need_at=True,
))
except Exception:
pass
return
file_cache.add(session_id, feishu_msg.content, file_type='file')
logger.info(f"[FeiShu] File cached for session {session_id}: {feishu_msg.content}")
return
# 如果是文本消息,检查是否有缓存的文件
if feishu_msg.ctype == ContextType.TEXT:
cached_files = file_cache.get(session_id)

View File

@@ -144,7 +144,14 @@ class FeishuMessage(ChatMessage):
file_key = content.get("file_key")
file_name = content.get("file_name")
self.content = TmpDir().path() + file_key + "." + utils.get_path_suffix(file_name)
# 落到 agent_workspace/tmp 下(绝对路径),与图片处理一致;
# 否则相对路径 ./tmp 在 agent 工作区里 read 时会找不到。
workspace_root = expand_path(conf().get("agent_workspace", "~/cow"))
tmp_dir = os.path.join(workspace_root, "tmp")
os.makedirs(tmp_dir, exist_ok=True)
self.content = os.path.join(
tmp_dir, f"{file_key}.{utils.get_path_suffix(file_name)}"
)
def _download_file():
# 如果响应状态码是200则将响应内容写入本地文件
@@ -170,7 +177,11 @@ class FeishuMessage(ChatMessage):
content = json.loads(msg.get("content"))
file_key = content.get("file_key")
self.content = TmpDir().path() + file_key + ".opus"
# 落到 agent_workspace/tmp 下(绝对路径),保证语音 STT 流程可读到
workspace_root = expand_path(conf().get("agent_workspace", "~/cow"))
tmp_dir = os.path.join(workspace_root, "tmp")
os.makedirs(tmp_dir, exist_ok=True)
self.content = os.path.join(tmp_dir, f"{file_key}.opus")
logger.info(f"[FeiShu] audio message: file_key={file_key}, save_path={self.content}")
def _download_audio():