feat: support Lark (larksuite.com) alongside Feishu

Add larkx/region.py with per-region endpoints, gateway app id and
locale. The region is chosen at login (lark auth qr/import --region,
default LARKX_REGION) and persisted in credentials; every request derives
its URLs from it. Credentials saved before this change are treated as
Feishu.

Fixes found while testing with real Lark and Feishu accounts:
- search: locale belongs on the request header, not searchContext
- storage: drop inline thumbnail bytes so content_data stays valid JSON
- send --root: set parentId, plus isReplyInThread for regular groups;
  topic-mode groups reject that flag, so chat modes are looked up and
  cached per chat
- topic groups (GROUP + chatMode THREAD/THREAD_V2): root messages anchor
  their own thread, except system notices; push handling and
  pull_history share decoders.resolve_scope
- show media/audio durations in seconds (they are milliseconds)
- render @mentions with the mentioned name via AtProperty
- lark send stores the server message id so the WS echo fills in the
  same row instead of inserting a duplicate

proto.proto gains isReplyInThread, AtProperty and PutMessageResponse;
proto_pb2.py is regenerated with grpcio-tools 1.71 (protoc 5.29.0).
This commit is contained in:
thxzzzzz
2026-09-30 15:55:43 +08:00
parent 2cb4f90fcf
commit b9c2d50d62
13 changed files with 542 additions and 358 deletions

View File

@ -9,6 +9,8 @@ MSG_TYPE_NAMES = {
22: 'COMMERCIALIZED_HONGBAO', 23: 'SHARE_USER_CARD', 24: 'TODO', 25: 'FOLDER',
}
CHAT_TYPE_NAMES = {0: 'UNKNOWN', 1: 'P2P', 2: 'GROUP', 3: 'TOPIC_GROUP'}
# Chat.ChatMode 的 THREAD / THREAD_V2;新建的话题群是 GROUP + 这两种模式,旧版话题群才是 TOPIC_GROUP
THREAD_CHAT_MODES = (2, 3)
def enum_to_int(value):
@ -25,6 +27,23 @@ def enum_to_int(value):
return 0
def is_thread_chat(chat_info: dict) -> bool:
"""话题群里每条根消息自成一个话题。chat_info 为 chats.PullChatsByIdsRequest 返回的单个会话。"""
return enum_to_int(chat_info.get('chatMode', 0)) in THREAD_CHAT_MODES or enum_to_int(chat_info.get('type', 0)) == 3
def resolve_scope(msg_id: str, msg_type: int, chat_type: int, root_id: str, thread_id: str, thread_chat: bool=False) -> tuple:
"""返回 (scope, anchor):anchor 是回复时的 --root,也是 agent 的会话边界。"""
if chat_type == 1:
return ('chat', '')
if root_id and root_id != '0':
return ('topic', thread_id or root_id)
# 话题群的根消息本身就开启一个话题(建群等系统提示除外);普通群每条消息也带 threadId=自身 id,不能据此判断
if (thread_chat or chat_type == 3) and msg_type != 6:
return ('topic', msg_id or '')
return ('chat', '')
def extract_rich_text(rich_text_dict):
TAG_PLACEHOLDERS = {2: '[图片]', 5: '[@]', 10: '[表情]', 18: '[视频]', 22: '[文档]'}
text = ''
@ -54,6 +73,15 @@ def extract_rich_text(rich_text_dict):
continue
except Exception:
pass
if tag == 5:
ap = P.AtProperty()
try:
ap.ParseFromString(prop)
if ap.content:
text += ap.content
continue
except Exception:
pass
text += TAG_PLACEHOLDERS.get(tag, '')
except Exception:
pass
@ -102,12 +130,12 @@ def decode_message_content(message_type, content_bytes):
mc = P.MediaContent()
mc.ParseFromString(content_bytes)
d = protobuf_to_dict(mc)
return (f"[视频] {d.get('name', '')} (时长{d.get('duration', 0)}s, {d.get('size', 0)}字节, key={d.get('key', '')})", d)
return (f"[视频] {d.get('name', '')} (时长{d.get('duration', 0) / 1000:.1f}s, {d.get('size', 0)}字节, key={d.get('key', '')})", d)
if message_type == 7:
ac = P.AudioContent()
ac.ParseFromString(content_bytes)
d = protobuf_to_dict(ac)
return (f"[语音] 时长{d.get('duration', 0)}s, key={d.get('key', '')}", d)
return (f"[语音] 时长{d.get('duration', 0) / 1000:.1f}s, key={d.get('key', '')}", d)
if message_type == 10:
sc = P.StickerContent()
sc.ParseFromString(content_bytes)
@ -225,15 +253,7 @@ def decode_push_messages(raw: bytes):
message_type = enum_to_int(v.get('type', 0))
chat_type = enum_to_int(v.get('chatType', 0))
summary, data = decode_message_content(message_type, v.get('content', b''))
thread_id = v.get('threadId') or ''
root_id = v.get('rootId') or ''
if chat_type == 1:
scope, anchor = ('chat', '')
elif root_id and root_id != '0':
scope, anchor = ('topic', thread_id or root_id)
elif chat_type == 3:
scope, anchor = ('chat', v.get('id') or '')
else:
scope, anchor = ('chat', '')
scope, anchor = resolve_scope(v.get('id'), message_type, chat_type, root_id, v.get('threadId') or '')
out.append({'msg_id': v.get('id'), 'msg_type': message_type, 'msg_type_name': MSG_TYPE_NAMES.get(message_type, str(message_type)), 'from_id': v.get('fromId'), 'chat_id': v.get('chatId') or v.get('channelId'), 'chat_type': chat_type, 'chat_type_name': CHAT_TYPE_NAMES.get(chat_type, str(chat_type)), 'scope': scope, 'anchor': anchor, 'at_me': bool(at_me_map.get(v.get('id')) or at_me_map.get(k)), 'root_id': root_id, 'parent_id': v.get('parentId') or '', 'cid': v.get('cid') or '', 'position': v.get('position'), 'create_time': v.get('createTime'), 'content': summary, 'content_data': data})
return out