Create 创建新 Agent 并加入池
(ctx context.Context, config *types.AgentConfig)
| 41 | |
| 42 | // Create 创建新 Agent 并加入池 |
| 43 | func (p *Pool) Create(ctx context.Context, config *types.AgentConfig) (*agent.Agent, error) { |
| 44 | p.mu.Lock() |
| 45 | defer p.mu.Unlock() |
| 46 | |
| 47 | // 检查是否已存在 |
| 48 | if _, exists := p.agents[config.AgentID]; exists { |
| 49 | return nil, fmt.Errorf("agent already exists: %s", config.AgentID) |
| 50 | } |
| 51 | |
| 52 | // 检查池容量 |
| 53 | if len(p.agents) >= p.maxAgents { |
| 54 | return nil, fmt.Errorf("pool is full (max %d agents)", p.maxAgents) |
| 55 | } |
| 56 | |
| 57 | // 创建 Agent |
| 58 | ag, err := agent.Create(ctx, config, p.deps) |
| 59 | if err != nil { |
| 60 | return nil, fmt.Errorf("create agent: %w", err) |
| 61 | } |
| 62 | |
| 63 | // 加入池 |
| 64 | p.agents[config.AgentID] = ag |
| 65 | return ag, nil |
| 66 | } |
| 67 | |
| 68 | // Get 获取指定 Agent |
| 69 | func (p *Pool) Get(agentID string) (*agent.Agent, bool) { |