这是一个批量整理提交,包含以下主要改动: 1. 删除多处冗余的空行和未使用的导入 2. 修复文件末尾缺少换行符的问题 3. 调整部分模块的导入顺序与代码排版 4. 修复部分配置默认值与策略逻辑 5. 新增多个功能模块与辅助工具 6. 完善异常处理与日志记录 7. 修复速率限制、消息缓存、权限校验等逻辑bug 8. 废弃部分旧有API与配置项并添加警告提示
98 lines
2.9 KiB
Python
98 lines
2.9 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from .cards import (
|
|
TEXT_CHUNK_LIMIT,
|
|
CARD_CONTENT_LIMIT,
|
|
build_feishu_card,
|
|
build_feishu_post_content,
|
|
build_feishu_text_content,
|
|
is_post_format_requested,
|
|
)
|
|
|
|
|
|
def format_outbound_message(content: str, *, metadata: dict | None = None, **_kwargs: Any) -> dict[str, Any]:
|
|
if is_post_format_requested(metadata):
|
|
return _format_post(content)
|
|
return _format_text(content)
|
|
|
|
|
|
def format_outbound_card(
|
|
content: str,
|
|
*,
|
|
title: str = "AI 助手",
|
|
buttons: list[dict[str, str]] | None = None,
|
|
streaming: bool = False,
|
|
url_unfurl: list[str] | None = None,
|
|
images: list[str] | None = None,
|
|
files: list[dict[str, str]] | None = None,
|
|
note: str | None = None,
|
|
template: str | None = None,
|
|
tone: str | None = None,
|
|
context_text: str | None = None,
|
|
dividers: int = 0,
|
|
selectors: list[dict[str, Any]] | None = None,
|
|
) -> dict[str, Any]:
|
|
return build_feishu_card(
|
|
content,
|
|
title=title,
|
|
buttons=buttons,
|
|
streaming=streaming,
|
|
url_unfurl=url_unfurl,
|
|
images=images,
|
|
files=files,
|
|
note=note,
|
|
template=template,
|
|
tone=tone,
|
|
context_text=context_text,
|
|
dividers=dividers,
|
|
selectors=selectors,
|
|
)
|
|
|
|
|
|
def format_outbound_diagnostic_card(title: str, details: dict[str, Any]) -> dict[str, Any]:
|
|
lines = [f"**{title}**", ""]
|
|
for key, value in details.items():
|
|
lines.append(f"- **{key}**: {str(value)[:200]}")
|
|
content = "\n".join(lines)
|
|
return build_feishu_card(content, title="系统诊断", template="orange")
|
|
|
|
|
|
def _format_text(content: str) -> dict[str, Any]:
|
|
truncated = _truncate_text(content, limit=TEXT_CHUNK_LIMIT)
|
|
return build_feishu_text_content(truncated)
|
|
|
|
|
|
def _format_post(content: str) -> dict[str, Any]:
|
|
truncated = _truncate_text(content, limit=TEXT_CHUNK_LIMIT)
|
|
return build_feishu_post_content(truncated)
|
|
|
|
|
|
def _truncate_text(content: str, limit: int = TEXT_CHUNK_LIMIT) -> str:
|
|
if len(content) <= limit:
|
|
return content
|
|
suffix = "\n\n...(内容过长已截断,请查看完整的消息记录)"
|
|
return content[:limit - len(suffix)] + suffix
|
|
|
|
|
|
def make_error_card(title: str, detail: str) -> dict[str, Any]:
|
|
return build_feishu_card(
|
|
f"**{detail}**",
|
|
title=title,
|
|
template="red",
|
|
tone="danger",
|
|
)
|
|
|
|
|
|
def format_outbound(content: str, **_kwargs: Any) -> dict[str, Any]:
|
|
result = format_outbound_message(content, metadata=_kwargs.get("metadata"))
|
|
result["content"] = content
|
|
for key in ("chat_type", "buttons", "thread_id"):
|
|
if key in _kwargs:
|
|
result[key] = _kwargs[key]
|
|
metadata = _kwargs.get("metadata", {})
|
|
for key in ("buttons", "thread_id"):
|
|
if isinstance(metadata, dict) and key in metadata:
|
|
result[key] = metadata[key]
|
|
return result |