-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat(channels): add xiaoyi channel support #875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ystyle
wants to merge
2
commits into
sipeed:refactor/channel-system
Choose a base branch
from
ystyle:feat/xiaoyi-channel-refactor
base: refactor/channel-system
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+328
−1
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,49 @@ | ||
| # XiaoYi (小艺) | ||
|
|
||
| XiaoYi Channel enables AI agent communication via Huawei's A2A (Agent-to-Agent) protocol, supporting text messages, file attachments, streaming responses, and status updates. | ||
|
|
||
| ## Configuration | ||
|
|
||
| ```json | ||
| { | ||
| "channels": { | ||
| "xiaoyi": { | ||
| "enabled": true, | ||
| "ak": "your-access-key", | ||
| "sk": "your-secret-key", | ||
| "agent_id": "your-agent-id", | ||
| "ws_url1": "", | ||
| "ws_url2": "", | ||
| "allow_from": [] | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| | Field | Type | Required | Description | | ||
| | ---------- | ------ | -------- | ---------------------------------------------- | | ||
| | enabled | bool | Yes | Enable XiaoYi channel | | ||
| | ak | string | Yes | Access Key | | ||
| | sk | string | Yes | Secret Key | | ||
| | agent_id | string | Yes | Agent identifier | | ||
| | ws_url1 | string | No | Server 1 URL, defaults to official server | | ||
| | ws_url2 | string | No | Server 2 URL, defaults to backup server | | ||
| | allow_from | array | No | User ID whitelist, empty allows all users | | ||
|
|
||
| ## Setup | ||
|
|
||
| For detailed setup instructions, please refer to the official Huawei documentation: [OpenClaw Integration Guide](https://developer.huawei.com/consumer/cn/doc/service/openclaw-0000002518410344) | ||
|
|
||
| 1. Register and create an OpenClaw type agent on Huawei Developer Platform | ||
| 2. Obtain AK (Access Key) and SK (Secret Key) | ||
| 3. Get Agent ID | ||
| 4. Add configuration to config file | ||
| 5. Start PicoClaw, XiaoYi Channel will automatically connect to XiaoYi servers | ||
|
|
||
| ## Features | ||
|
|
||
| - **WebSocket long connection**: Dual server hot-standby support | ||
| - **Auto-reconnect**: Exponential backoff strategy, max 50 retries | ||
| - **Heartbeat**: Protocol-level + application-level dual heartbeat | ||
| - **Streaming response**: Support progressive result return | ||
| - **Status update**: Send "processing" status immediately upon receiving message |
This file contains hidden or 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,49 @@ | ||
| # 小艺 (XiaoYi) | ||
|
|
||
| 小艺 Channel 通过华为小艺 A2A (Agent-to-Agent) 协议实现智能体通信,支持文本消息、文件附件、流式响应和状态更新。 | ||
|
|
||
| ## 配置 | ||
|
|
||
| ```json | ||
| { | ||
| "channels": { | ||
| "xiaoyi": { | ||
| "enabled": true, | ||
| "ak": "your-access-key", | ||
| "sk": "your-secret-key", | ||
| "agent_id": "your-agent-id", | ||
| "ws_url1": "", | ||
| "ws_url2": "", | ||
| "allow_from": [] | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| | 字段 | 类型 | 必填 | 描述 | | ||
| | ---------- | ------ | ---- | -------------------------------------------- | | ||
| | enabled | bool | 是 | 是否启用小艺频道 | | ||
| | ak | string | 是 | Access Key | | ||
| | sk | string | 是 | Secret Key | | ||
| | agent_id | string | 是 | Agent 标识 | | ||
| | ws_url1 | string | 否 | 服务器1 URL,默认为小艺官方服务器 | | ||
| | ws_url2 | string | 否 | 服务器2 URL,默认为小艺备用服务器 | | ||
| | allow_from | array | 否 | 用户ID白名单,空表示允许所有用户 | | ||
|
|
||
| ## 设置流程 | ||
|
|
||
| 详细教程请参考华为官方文档:[OpenClaw 接入指南](https://developer.huawei.com/consumer/cn/doc/service/openclaw-0000002518410344) | ||
|
|
||
| 1. 在华为开发者平台注册并创建 OpenClaw 类型的智能体 | ||
| 2. 获取 AK (Access Key) 和 SK (Secret Key) | ||
| 3. 获取 Agent ID | ||
| 4. 将配置填入配置文件中 | ||
| 5. 启动 PicoClaw,小艺 Channel 将自动连接到小艺服务器 | ||
|
|
||
| ## 特性 | ||
|
|
||
| - **WebSocket 长连接**:支持双服务器热备 | ||
| - **自动重连**:指数退避策略,最大重试 50 次 | ||
| - **心跳机制**:协议层 + 应用层双重心跳 | ||
| - **流式响应**:支持逐步返回结果 | ||
| - **状态更新**:收到消息后立即发送"处理中"状态 |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,13 @@ | ||
| package xiaoyi | ||
|
|
||
| import ( | ||
| "github.com/sipeed/picoclaw/pkg/bus" | ||
| "github.com/sipeed/picoclaw/pkg/channels" | ||
| "github.com/sipeed/picoclaw/pkg/config" | ||
| ) | ||
|
|
||
| func init() { | ||
| channels.RegisterFactory("xiaoyi", func(cfg *config.Config, b *bus.MessageBus) (channels.Channel, error) { | ||
| return NewXiaoYiChannel(cfg, b) | ||
| }) | ||
| } |
This file contains hidden or 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,191 @@ | ||
| package xiaoyi | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| xiaoyi "github.com/ystyle/xiaoyi-agent-sdk/pkg/client" | ||
| "github.com/ystyle/xiaoyi-agent-sdk/pkg/types" | ||
|
|
||
| "github.com/sipeed/picoclaw/pkg/bus" | ||
| "github.com/sipeed/picoclaw/pkg/channels" | ||
| "github.com/sipeed/picoclaw/pkg/config" | ||
| "github.com/sipeed/picoclaw/pkg/identity" | ||
| "github.com/sipeed/picoclaw/pkg/logger" | ||
| ) | ||
|
|
||
| type XiaoYiChannel struct { | ||
| *channels.BaseChannel | ||
| config *config.Config | ||
| client xiaoyi.Client | ||
| ctx context.Context | ||
| cancel context.CancelFunc | ||
| } | ||
|
|
||
| func NewXiaoYiChannel(cfg *config.Config, messageBus *bus.MessageBus) (*XiaoYiChannel, error) { | ||
| xiaoyiCfg := cfg.Channels.XiaoYi | ||
|
|
||
| base := channels.NewBaseChannel( | ||
| "xiaoyi", | ||
| xiaoyiCfg, | ||
| messageBus, | ||
| xiaoyiCfg.AllowFrom, | ||
| ) | ||
|
|
||
| return &XiaoYiChannel{ | ||
| BaseChannel: base, | ||
| config: cfg, | ||
| }, nil | ||
| } | ||
|
|
||
| func (c *XiaoYiChannel) Start(ctx context.Context) error { | ||
| xiaoyiCfg := c.config.Channels.XiaoYi | ||
|
|
||
| if xiaoyiCfg.AK == "" || xiaoyiCfg.SK == "" || xiaoyiCfg.AgentID == "" { | ||
| return fmt.Errorf("xiaoyi ak, sk and agent_id are required") | ||
| } | ||
|
|
||
| logger.InfoC("xiaoyi", "Starting XiaoYi channel") | ||
|
|
||
| cfg := &types.Config{ | ||
| AK: xiaoyiCfg.AK, | ||
| SK: xiaoyiCfg.SK, | ||
| AgentID: xiaoyiCfg.AgentID, | ||
| WSUrl1: xiaoyiCfg.WSUrl1, | ||
| WSUrl2: xiaoyiCfg.WSUrl2, | ||
| SingleServer: true, | ||
| } | ||
|
|
||
| c.client = xiaoyi.New(cfg) | ||
|
|
||
| c.client.OnMessage(func(ctx context.Context, msg types.Message) error { | ||
| return c.handleMessage(ctx, msg) | ||
| }) | ||
|
|
||
| c.client.OnClear(func(sessionID string) { | ||
| logger.InfoCF("xiaoyi", "Session cleared", map[string]any{ | ||
| "session": sessionID, | ||
| }) | ||
| }) | ||
|
|
||
| c.client.OnCancel(func(sessionID, taskID string) { | ||
| logger.InfoCF("xiaoyi", "Task canceled", map[string]any{ | ||
| "session": sessionID, | ||
| "task": taskID, | ||
| }) | ||
| }) | ||
|
|
||
| c.client.OnError(func(serverID string, err error) { | ||
| logger.ErrorCF("xiaoyi", "Server error", map[string]any{ | ||
| "server": serverID, | ||
| "error": err.Error(), | ||
| }) | ||
| }) | ||
|
|
||
| c.ctx, c.cancel = context.WithCancel(ctx) | ||
|
|
||
| if err := c.client.Connect(c.ctx); err != nil { | ||
| return fmt.Errorf("failed to connect xiaoyi: %w", err) | ||
| } | ||
|
|
||
| c.SetRunning(true) | ||
| logger.InfoC("xiaoyi", "XiaoYi channel started successfully") | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (c *XiaoYiChannel) Stop(ctx context.Context) error { | ||
| logger.InfoC("xiaoyi", "Stopping XiaoYi channel") | ||
| c.SetRunning(false) | ||
|
|
||
| if c.cancel != nil { | ||
| c.cancel() | ||
| } | ||
|
|
||
| if c.client != nil { | ||
| c.client.Close() | ||
| } | ||
|
|
||
| logger.InfoC("xiaoyi", "XiaoYi channel stopped") | ||
| return nil | ||
| } | ||
|
|
||
| func (c *XiaoYiChannel) Send(ctx context.Context, msg bus.OutboundMessage) error { | ||
| if !c.IsRunning() { | ||
| return channels.ErrNotRunning | ||
| } | ||
|
|
||
| parts := strings.SplitN(msg.ChatID, ":", 2) | ||
| if len(parts) != 2 { | ||
| return fmt.Errorf("invalid chat_id format: %w", channels.ErrSendFailed) | ||
| } | ||
|
|
||
| sessionID := parts[0] | ||
| taskID := parts[1] | ||
|
|
||
| logger.DebugCF("xiaoyi", "Sending message", map[string]any{ | ||
| "session": sessionID, | ||
| "task": taskID, | ||
| "length": len(msg.Content), | ||
| }) | ||
|
|
||
| if err := c.client.ReplyStream(ctx, taskID, sessionID, msg.Content, false, false); err != nil { | ||
| return fmt.Errorf("xiaoyi reply: %w", channels.ErrTemporary) | ||
| } | ||
|
|
||
| if err := c.client.SendStatus(ctx, taskID, sessionID, "Completed", "completed"); err != nil { | ||
| logger.WarnCF("xiaoyi", "Failed to send completed status", map[string]any{"error": err.Error()}) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (c *XiaoYiChannel) handleMessage(ctx context.Context, msg types.Message) error { | ||
| sessionID := msg.SessionID() | ||
| taskID := msg.TaskID() | ||
| text := strings.TrimSpace(msg.Text()) | ||
|
|
||
| logger.InfoCF("xiaoyi", "Received message", map[string]any{ | ||
| "session": sessionID, | ||
| "task": taskID, | ||
| "text": text, | ||
| }) | ||
|
|
||
| sender := bus.SenderInfo{ | ||
| Platform: "xiaoyi", | ||
| PlatformID: sessionID, | ||
| CanonicalID: identity.BuildCanonicalID("xiaoyi", sessionID), | ||
| } | ||
|
|
||
| if !c.IsAllowedSender(sender) { | ||
| logger.DebugCF("xiaoyi", "Message rejected by allowlist", map[string]any{ | ||
| "session": sessionID, | ||
| }) | ||
| return nil | ||
| } | ||
|
|
||
| chatID := fmt.Sprintf("%s:%s", sessionID, taskID) | ||
|
||
| peer := bus.Peer{Kind: "direct", ID: sessionID} | ||
|
|
||
| if err := c.client.SendStatus(ctx, taskID, sessionID, "Processing...", "working"); err != nil { | ||
| logger.WarnCF("xiaoyi", "Failed to send status", map[string]any{"error": err.Error()}) | ||
| } | ||
|
|
||
| metadata := map[string]string{ | ||
| "task_id": taskID, | ||
| } | ||
|
|
||
| c.HandleMessage(c.ctx, | ||
| peer, | ||
| "", | ||
| sessionID, | ||
| chatID, | ||
| text, | ||
| nil, | ||
| metadata, | ||
| sender, | ||
| ) | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will the completed status published here conflict with long message fragmentation?