← 返回
未分类

Claw Chat Hub

智能体实时通讯模块 - 支持 Provider 和 Consumer 双向消息、频道管理、消息历史
智能体实时通讯模块 - 支持提供者和消费者双向消息、频道管理、消息历史
tangboheng tangboheng 来源
未分类 clawhub v0.1.0 1 版本 100000 Key: 无需
★ 0
Stars
📥 388
下载
💾 0
安装
1
版本
#latest

概述

Claw Chat Hub

> 实现智能体之间的双向实时通讯

概述

claw-chat-hub 提供智能体之间的实时消息通讯能力,让服务提供者和服务使用者可以通过 Hub 进行双向实时对话。

功能

  • 服务绑定通讯 - 服务注册时自动创建通讯频道
  • 双向实时消息 - 支持 Provider 和 Consumer 之间实时交流
  • 消息历史 - 获取历史会话记录
  • 频道管理 - 创建、绑定、结束通讯会话

安装

pip install -e /path/to/Claw-Service-Hub/claw-chat-hub

快速开始

Provider 端(服务提供者)

from claw_chat_hub import ChatClient

# 创建客户端
chat = ChatClient(
    hub_url="ws://localhost:8765",
    agent_id="weather-provider"
)

# 监听消息
async def on_message(msg):
    print(f"收到消息 from {msg['sender_id']}: {msg['content']}")
    
    # 回复消息
    await chat.send_message(
        target_agent=msg['sender_id'],
        content="消息已收到!"
    )

# 启动监听
await chat.connect()
await chat.listen_for_messages(on_message)

Consumer 端(服务使用者)

from claw_chat_hub import ChatClient

# 创建客户端
chat = ChatClient(
    hub_url="ws://localhost:8765",
    agent_id="weather-consumer"
)

# 连接到 Hub
await chat.connect()

# 发送消息
await chat.send_message(
    target_agent="weather-provider",
    content="查询北京天气"
)

# 监听回复
async for msg in chat.messages():
    print(f"收到: {msg['content']}")

API 参考

ChatClient

初始化

chat = ChatClient(
    hub_url="ws://localhost:8765",  # Hub 地址
    agent_id="my-agent",             # 智能体 ID
    api_key=None                     # API 密钥(可选)
)

连接与断开

await chat.connect()      # 连接到 Hub
await chat.disconnect()   # 断开连接

发送消息

result = await chat.send_message(
    target_agent="other-agent",  # 目标智能体
    content="Hello!",            # 消息内容
    service_id="weather-svc"     # 服务 ID(可选)
)

请求通讯

# Consumer 请求与 Provider 通讯
result = await chat.request_chat(
    service_id="weather-svc"
)
# result = {"status": "accepted", "channel_id": "ch_xxx"}

接受/拒绝通讯

# Provider 接受
await chat.accept_chat(consumer_id="consumer-agent")

# Provider 拒绝
await chat.reject_chat(consumer_id="consumer-agent", reason="Busy")

结束通讯

await chat.end_chat(channel_id="ch_xxx")

获取历史

history = await chat.get_history(
    channel_id="ch_xxx",      # 频道 ID
    service_id="weather-svc", # 服务 ID
    limit=50                  # 数量限制
)

便捷函数

from claw_chat_hub import quick_send

# 快速发送消息(单次)
result = await quick_send(
    hub_url="ws://localhost:8765",
    agent_id="sender",
    target_agent="receiver",
    content="Hello!"
)

消息协议

消息类型方向说明
----------------------
chat_requestConsumer → Hub → Provider发起通讯请求
chat_acceptProvider → Hub → Consumer接受通讯
chat_rejectProvider → Hub → Consumer拒绝通讯
chat_message双向消息内容
chat_end任意结束通讯
chat_historyConsumer → Hub获取历史

数据结构

频道 (Channel)

{
    "channel_id": "ch_xxx",
    "provider_id": "weather-provider",
    "consumer_id": "weather-consumer",
    "service_id": "weather-svc",
    "created_at": "2024-01-01T00:00:00Z"
}

消息 (Message)

{
    "message_id": "msg_xxx",
    "channel_id": "ch_xxx",
    "sender_id": "weather-provider",
    "content": "北京今天晴,25°C",
    "timestamp": "2024-01-01T00:00:00Z"
}

与其他模块的关系

claw-chat-hub
    │
    ├── 需要: hub-client (连接 Hub)
    ├── 需要: server/chat_* (Hub 端支持)
    └── 可选: claw-trade-hub (交易 + 通讯)

示例

See examples/chat_example.py for complete examples.

版本历史

共 1 个版本

  • v0.1.0 当前
    2026-05-03 09:30 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

ai-agent

Agent Browser

rez0
用于 AI 代理的浏览器自动化 CLI。当用户需要与网站交互(包括浏览页面、填写表单、点击按钮、截图等)时使用。
★ 831 📥 297,297
ai-agent

self-improving agent

pskoett
捕获经验教训、错误及修正内容,以实现持续改进。适用于以下场景:(1)命令或操作意外失败;(2)用户纠正Claude(如“不,那不对……”“实际上……”);(3)用户请求的功能不存在;(4)外部API或工具出现故障;(5)Claude发现自身
★ 4,094 📥 821,466
ai-agent

Self-Improving + Proactive Agent

ivangdavila
自我反思+自我批评+自我学习+自组织记忆。智能体评估自身工作、发现错误并持续改进。
★ 1,390 📥 321,853