Skip to content

update:合并群聊模式下userid的生成#1

Merged
bjhx2003 merged 1 commit intobjhx2003:feat/wecomfrom
Saiyuekin:feat/wecom
Apr 2, 2026
Merged

update:合并群聊模式下userid的生成#1
bjhx2003 merged 1 commit intobjhx2003:feat/wecomfrom
Saiyuekin:feat/wecom

Conversation

@Saiyuekin
Copy link
Copy Markdown

好的,我来帮你准备一份符合 GitHub PR 格式的提交内容。

PR 提交内容

## 概述 / Overview

> 请在此部分填写你实现/解决/优化的内容:  
> Summary of what you implemented/solved/optimized:
> 

**修复企业微信群聊场景下 Dify sys.user_id 组装不完整的问题**

**Fix incomplete sys.user_id construction in WeCom group chat scenarios**

### 问题描述 / Problem Description

在企业微信 AI Bot 适配器中,群聊场景下传递给 Dify 的 `sys.user_id` 只包含群 ID,缺少用户 ID,导致:
- Dify 无法区分同一群内不同用户的会话
- 群聊用户共享同一个会话上下文,影响多轮对话准确性

**Before:**
- 私聊:`sys.user_id = person_huaming`- 群聊:`sys.user_id = group_CHATBOTID` ❌(缺少用户 ID)

**After:**
- 私聊:`sys.user_id = person_huaming`- 群聊:`sys.user_id = group_CHATBOTID_448823` ✅(包含群 ID 和用户 ID)

### 实现方案 / Implementation

#### 1. 企业微信 AI Bot 适配器添加 `get_launcher_id` 方法

**文件**: `src/langbot/pkg/platform/sources/wecombot.py``WecomBotAdapter` 类中新增 `get_launcher_id` 方法,在群聊场景下返回 `chatid_userid` 格式的组合 ID:

```python
def get_launcher_id(self, event: platform_events.MessageEvent) -> str | None:
    """获取启动器 ID,用于区分不同的会话场景"""
    if isinstance(event, platform_events.GroupMessage):
        wecom_event = event.source_platform_object
        if isinstance(wecom_event, WecomBotEvent):
            chatid = wecom_event.chatid
            userid = wecom_event.userid
            if chatid and userid:
                return f'{chatid}_{userid}'
    return None

2. 同步日志记录与 launcher_id 逻辑

文件: src/langbot/pkg/platform/botmgr.py

  • 将日志记录中的 message_session_id 与实际处理的 launcher_id 保持一致
  • 优化代码结构,避免重复计算 launcher_id
  • 确保日志只在实际处理消息时记录(在 if not skip_pipeline: 条件块内)

修改前:

# 日志总是记录,且使用原始的 group.id
await self.logger.info(
    f'{event.message_chain}',
    images=image_components,
    message_session_id=f'group_{event.group.id}',  # ❌ 仅群 ID
)

# 后面才计算实际的 launcher_id
if not skip_pipeline:
    launcher_id = event.group.id
    if hasattr(adapter, 'get_launcher_id'):
        custom_launcher_id = adapter.get_launcher_id(event)
        if custom_launcher_id:
            launcher_id = custom_launcher_id

修改后:

if not skip_pipeline:
    # 构建启动器 ID(支持适配器自定义)
    launcher_id = event.group.id
    if hasattr(adapter, 'get_launcher_id'):
        custom_launcher_id = adapter.get_launcher_id(event)
        if custom_launcher_id:
            launcher_id = custom_launcher_id

    # 日志使用计算后的 launcher_id,保证一致性
    await self.logger.info(
        f'{event.message_chain}',
        images=image_components,
        message_session_id=f'group_{launcher_id}',  # ✅ 完整的组合 ID
    )

更改前后对比截图 / Screenshots

请在此部分粘贴更改前后对比截图(可以是界面截图、控制台输出、对话截图等):
Please paste the screenshots of changes before and after here (can be interface screenshots, console output, conversation screenshots, etc.):

修改前 / Before:

Dify 收到的 sys.user_id:
- 私聊: person_huaming
- 群聊: group_CHATBOTID  # ❌ 所有群用户共用同一个 ID

修改后 / After:

Dify 收到的 sys.user_id:
- 私聊: person_huaming
- 群聊: group_CHATBOTID_448823  # ✅ 每个用户有独立的 ID
- 群聊: group_CHATBOTID_123456  # ✅ 不同用户可以区分

影响范围 / Impact

  • 向后兼容: 使用 hasattr 检测,不影响未实现 get_launcher_id 的其他适配器
  • 设计模式: 遵循开闭原则,通过可选方法扩展适配器功能
  • 日志优化: 减少无效日志记录,提升日志准确性
  • 数据一致性: 日志 message_session_id 与实际 launcher_id 完全一致

测试建议 / Testing Suggestions

  1. 企业微信群聊发送消息,验证 Dify 收到的 sys.user_id 格式为 group_chatid_userid
  2. 企业微信私聊发送消息,验证 sys.user_id 仍为 person_userid
  3. 同一群内多个用户发送消息,确认每个用户有独立的会话 ID
  4. 检查日志系统是否正确记录会话 ID
  5. 验证 webhook 插件跳过 pipeline 时日志记录是否正确

检查清单 / Checklist

PR 作者完成 / For PR author

请在方括号间写x以打勾 / Please tick the box with x

  • 阅读仓库贡献指引了吗? / Have you read the contribution guide?
  • 与项目所有者沟通过了吗? / Have you communicated with the project maintainer?
  • 我确定已自行测试所作的更改,确保功能符合预期。 / I have tested the changes and ensured they work as expected.

项目维护者完成 / For project maintainer

  • 相关 issues 链接了吗? / Have you linked the related issues?
  • 配置项写好了吗?迁移写好了吗?生效了吗? / Have you written the configuration items? Have you written the migration? Has it taken effect?
  • 依赖加到 pyproject.toml 和 core/bootutils/deps.py 了吗 / Have you added the dependencies to pyproject.toml and core/bootutils/deps.py?
  • 文档编写了吗? / Have you written the documentation?

修改文件列表 / Modified Files

  • src/langbot/pkg/platform/sources/wecombot.py - 添加 get_launcher_id 方法
  • src/langbot/pkg/platform/botmgr.py - 优化 launcher_id 构建逻辑和日志记录

Commit Message 建议

fix(wecombot): 修复企业微信群聊 sys.user_id 缺少用户 ID 的问题

- 在 WecomBotAdapter 中添加 get_launcher_id 方法
- 群聊场景下返回 chatid_userid 格式的组合 ID
- 同步 botmgr.py 中的日志 message_session_id 与 launcher_id 保持一致
- 优化代码结构,避免重复计算 launcher_id

Fixes: 企业微信群聊中 Dify 无法区分不同用户会话的问题

## 💡 使用建议

1. **填写截图**: 在提交 PR 时,请补充实际的 Dify 控制台截图或日志输出,展示修复前后的 `sys.user_id` 对比

2. **关联 Issue**: 如果有相关的 Issue,记得在 Checklist 中勾选并添加链接

3. **测试验证**: 建议在实际的企业微信环境中测试:
   ```bash
   # 启动服务后,观察日志输出
   # 群聊消息应该显示: message_session_id=group_CHATBOTID_448823
   # Dify 控制台应该显示: sys.user_id = group_CHATBOTID_448823
  1. 补充测试用例 (可选): 如果项目有单元测试,可以考虑添加测试用例验证 get_launcher_id 方法的逻辑

这份 PR 模板已经符合项目的贡献指引要求,清晰地说明了问题、解决方案和影响范围。👍

@bjhx2003 bjhx2003 merged commit 117dd6f into bjhx2003:feat/wecom Apr 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants