新增飞书机器人适配器全套功能,包括: - 基础适配器入口与工具导出 - 消息格式化、卡片渲染、回复调度逻辑 - 会话ID生成、模型覆盖策略 - 消息发送缓存、顺序队列管理 - 飞书签名验证、加解密webhook请求 - 审批权限校验、机器人菜单事件处理 - 文档评论、钉消息、语音转码处理 - 静态/动态目录管理、子代理生命周期管理 - 各类工具集:聊天、云盘、文档、知识库API封装
50 lines
1.2 KiB
Python
50 lines
1.2 KiB
Python
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
_CODE_BLOCK_PATTERN = re.compile(r"```[\s\S]*?```", re.DOTALL)
|
|
_TABLE_PATTERN = re.compile(r"\|[^\n]+\|", re.DOTALL)
|
|
_LARGE_OUTPUT_THRESHOLD = 1500
|
|
_URL_PATTERN = re.compile(r"https?://[^\s<>\"{}|\\^`\[\]]+")
|
|
|
|
|
|
def should_use_card(content: str, buttons: list | None = None) -> bool:
|
|
if buttons:
|
|
return True
|
|
|
|
if len(content) > _LARGE_OUTPUT_THRESHOLD:
|
|
return True
|
|
|
|
if _CODE_BLOCK_PATTERN.search(content):
|
|
return True
|
|
|
|
if _TABLE_PATTERN.search(content):
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
def dispatch_render(content: str, *, buttons: list | None = None, render_mode: str = "auto") -> str:
|
|
if render_mode == "card":
|
|
return "card"
|
|
if render_mode == "raw":
|
|
return "text"
|
|
|
|
if should_use_card(content, buttons):
|
|
return "card"
|
|
return "text"
|
|
|
|
|
|
def extract_urls(content: str, max_urls: int = 5) -> list[str]:
|
|
urls = _URL_PATTERN.findall(content)
|
|
seen: set[str] = set()
|
|
result: list[str] = []
|
|
for url in urls:
|
|
url = url.rstrip(".,;:)!?\"'")
|
|
if url not in seen:
|
|
seen.add(url)
|
|
result.append(url)
|
|
if len(result) >= max_urls:
|
|
break
|
|
return result
|