SendTo 发送消息给指定成员
(ctx context.Context, from string, to string, text string)
| 197 | |
| 198 | // SendTo 发送消息给指定成员 |
| 199 | func (r *Room) SendTo(ctx context.Context, from string, to string, text string) error { |
| 200 | r.mu.RLock() |
| 201 | |
| 202 | // 检查发送者 |
| 203 | if _, exists := r.members[from]; !exists && from != "system" { |
| 204 | r.mu.RUnlock() |
| 205 | return fmt.Errorf("sender is not a member: %s", from) |
| 206 | } |
| 207 | |
| 208 | // 检查接收者 |
| 209 | agentID, exists := r.members[to] |
| 210 | if !exists { |
| 211 | r.mu.RUnlock() |
| 212 | return fmt.Errorf("recipient not found: %s", to) |
| 213 | } |
| 214 | |
| 215 | r.mu.RUnlock() |
| 216 | |
| 217 | // 记录到历史 |
| 218 | msg := RoomMessage{ |
| 219 | From: from, |
| 220 | To: []string{to}, |
| 221 | Text: text, |
| 222 | Sent: nowTimestamp(), |
| 223 | } |
| 224 | |
| 225 | r.mu.Lock() |
| 226 | r.history = append(r.history, msg) |
| 227 | r.mu.Unlock() |
| 228 | |
| 229 | // 获取 Agent 并发送 |
| 230 | ag, exists := r.pool.Get(agentID) |
| 231 | if !exists { |
| 232 | return fmt.Errorf("agent not found for member %s", to) |
| 233 | } |
| 234 | |
| 235 | formattedText := fmt.Sprintf("[from:%s] %s", from, text) |
| 236 | return ag.Send(ctx, formattedText) |
| 237 | } |
| 238 | |
| 239 | // GetMembers 获取所有成员 |
| 240 | func (r *Room) GetMembers() []RoomMember { |