-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add user profile query support for IM adapters
- Introduced UserProfile data model with comprehensive user information - Created UserProfileAdapter protocol for querying user profiles - Implemented QueryUserProfileBlock for workflow-based user profile retrieval - Added user profile query method to Telegram adapter with caching - Supported flexible profile information extraction with fallback mechanisms
- Loading branch information
Showing
6 changed files
with
156 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import Optional, Any | ||
from enum import Enum, auto | ||
from pydantic import BaseModel, Field | ||
|
||
class Gender(Enum): | ||
MALE = auto() | ||
FEMALE = auto() | ||
UNKNOWN = auto() | ||
OTHER = auto() | ||
|
||
class UserProfile(BaseModel): | ||
""" | ||
通用的用户资料结构 | ||
""" | ||
user_id: str = Field(..., description="用户唯一标识") | ||
username: Optional[str] = Field(None, description="用户名") | ||
display_name: Optional[str] = Field(None, description="显示名称") | ||
full_name: Optional[str] = Field(None, description="完整名称") | ||
gender: Optional[Gender] = Field(None, description="性别") | ||
age: Optional[int] = Field(None, description="年龄") | ||
avatar_url: Optional[str] = Field(None, description="头像URL") | ||
level: Optional[int] = Field(None, description="用户等级") | ||
language: Optional[str] = Field(None, description="语言") | ||
extra_info: Optional[dict] = Field(None, description="平台特定的额外信息") |
33 changes: 33 additions & 0 deletions
33
framework/workflow/implementations/blocks/im/user_profile.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from typing import Dict, Any, Optional | ||
from framework.ioc.container import DependencyContainer | ||
from framework.workflow.core.block import Block | ||
from framework.workflow.core.workflow.input_output import Input, Output | ||
from framework.im.sender import ChatSender | ||
from framework.im.adapter import IMAdapter, UserProfileAdapter | ||
from framework.im.profile import UserProfile | ||
|
||
class QueryUserProfileBlock(Block): | ||
def __init__(self, container: DependencyContainer): | ||
inputs = { | ||
"chat_sender": Input("chat_sender", ChatSender, "Chat sender to query profile for"), | ||
"im_adapter": Input("im_adapter", IMAdapter, "IM Adapter to use for profile query", optional=True) | ||
} | ||
outputs = { | ||
"profile": Output("profile", UserProfile, "User profile information") | ||
} | ||
super().__init__("query_user_profile", inputs, outputs) | ||
self.container = container | ||
|
||
def execute(self, chat_sender: ChatSender, im_adapter: Optional[IMAdapter] = None) -> Dict[str, Any]: | ||
# 如果没有提供 im_adapter,则从容器中获取默认的 | ||
if im_adapter is None: | ||
im_adapter = self.container.resolve(IMAdapter) | ||
|
||
# 检查 im_adapter 是否实现了 UserProfileAdapter 协议 | ||
if not isinstance(im_adapter, UserProfileAdapter): | ||
raise TypeError(f"IM Adapter {type(im_adapter)} does not support user profile querying") | ||
|
||
# 同步调用异步方法(在工作流执行器中会被正确处理) | ||
profile = im_adapter.query_user_profile(chat_sender) | ||
|
||
return {"profile": profile} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters