mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-05-06 14:07:29 +08:00
fix bug
This commit is contained in:
@@ -14,6 +14,10 @@ type SubagentTask struct {
|
||||
ID string
|
||||
Task string
|
||||
Label string
|
||||
Role string
|
||||
PipelineID string
|
||||
PipelineTask string
|
||||
SharedState map[string]interface{}
|
||||
OriginChannel string
|
||||
OriginChatID string
|
||||
Status string
|
||||
@@ -26,22 +30,24 @@ type SubagentManager struct {
|
||||
mu sync.RWMutex
|
||||
provider providers.LLMProvider
|
||||
bus *bus.MessageBus
|
||||
orc *Orchestrator
|
||||
workspace string
|
||||
nextID int
|
||||
runFunc SubagentRunFunc
|
||||
}
|
||||
|
||||
func NewSubagentManager(provider providers.LLMProvider, workspace string, bus *bus.MessageBus) *SubagentManager {
|
||||
func NewSubagentManager(provider providers.LLMProvider, workspace string, bus *bus.MessageBus, orc *Orchestrator) *SubagentManager {
|
||||
return &SubagentManager{
|
||||
tasks: make(map[string]*SubagentTask),
|
||||
provider: provider,
|
||||
bus: bus,
|
||||
orc: orc,
|
||||
workspace: workspace,
|
||||
nextID: 1,
|
||||
}
|
||||
}
|
||||
|
||||
func (sm *SubagentManager) Spawn(ctx context.Context, task, label, originChannel, originChatID string) (string, error) {
|
||||
func (sm *SubagentManager) Spawn(ctx context.Context, task, label, originChannel, originChatID, pipelineID, pipelineTask string) (string, error) {
|
||||
sm.mu.Lock()
|
||||
defer sm.mu.Unlock()
|
||||
|
||||
@@ -52,6 +58,8 @@ func (sm *SubagentManager) Spawn(ctx context.Context, task, label, originChannel
|
||||
ID: taskID,
|
||||
Task: task,
|
||||
Label: label,
|
||||
PipelineID: pipelineID,
|
||||
PipelineTask: pipelineTask,
|
||||
OriginChannel: originChannel,
|
||||
OriginChatID: originChatID,
|
||||
Status: "running",
|
||||
@@ -61,20 +69,30 @@ func (sm *SubagentManager) Spawn(ctx context.Context, task, label, originChannel
|
||||
|
||||
go sm.runTask(ctx, subagentTask)
|
||||
|
||||
desc := fmt.Sprintf("Spawned subagent for task: %s", task)
|
||||
if label != "" {
|
||||
return fmt.Sprintf("Spawned subagent '%s' for task: %s", label, task), nil
|
||||
desc = fmt.Sprintf("Spawned subagent '%s' for task: %s", label, task)
|
||||
}
|
||||
return fmt.Sprintf("Spawned subagent for task: %s", task), nil
|
||||
if pipelineID != "" && pipelineTask != "" {
|
||||
desc += fmt.Sprintf(" (pipeline=%s task=%s)", pipelineID, pipelineTask)
|
||||
}
|
||||
return desc, nil
|
||||
}
|
||||
|
||||
func (sm *SubagentManager) runTask(ctx context.Context, task *SubagentTask) {
|
||||
sm.mu.Lock()
|
||||
task.Status = "running"
|
||||
task.Created = time.Now().UnixMilli()
|
||||
sm.mu.Unlock()
|
||||
|
||||
if sm.orc != nil && task.PipelineID != "" && task.PipelineTask != "" {
|
||||
_ = sm.orc.MarkTaskRunning(task.PipelineID, task.PipelineTask)
|
||||
}
|
||||
|
||||
// 1. 独立 Agent 逻辑:支持递归工具调用
|
||||
// 这里简单实现:通过共享 AgentLoop 的逻辑来实现 full subagent 能力
|
||||
// 但目前 subagent.go 不方便反向依赖 agent 包,我们暂时通过 Inject 方式解决
|
||||
|
||||
|
||||
// 如果没有注入 RunFunc,则退化为简单的一步 Chat
|
||||
if sm.runFunc != nil {
|
||||
result, err := sm.runFunc(ctx, task.Task, task.OriginChannel, task.OriginChatID)
|
||||
@@ -82,9 +100,15 @@ func (sm *SubagentManager) runTask(ctx context.Context, task *SubagentTask) {
|
||||
if err != nil {
|
||||
task.Status = "failed"
|
||||
task.Result = fmt.Sprintf("Error: %v", err)
|
||||
if sm.orc != nil && task.PipelineID != "" && task.PipelineTask != "" {
|
||||
_ = sm.orc.MarkTaskDone(task.PipelineID, task.PipelineTask, task.Result, err)
|
||||
}
|
||||
} else {
|
||||
task.Status = "completed"
|
||||
task.Result = result
|
||||
if sm.orc != nil && task.PipelineID != "" && task.PipelineTask != "" {
|
||||
_ = sm.orc.MarkTaskDone(task.PipelineID, task.PipelineTask, task.Result, nil)
|
||||
}
|
||||
}
|
||||
sm.mu.Unlock()
|
||||
} else {
|
||||
@@ -108,9 +132,15 @@ func (sm *SubagentManager) runTask(ctx context.Context, task *SubagentTask) {
|
||||
if err != nil {
|
||||
task.Status = "failed"
|
||||
task.Result = fmt.Sprintf("Error: %v", err)
|
||||
if sm.orc != nil && task.PipelineID != "" && task.PipelineTask != "" {
|
||||
_ = sm.orc.MarkTaskDone(task.PipelineID, task.PipelineTask, task.Result, err)
|
||||
}
|
||||
} else {
|
||||
task.Status = "completed"
|
||||
task.Result = response.Content
|
||||
if sm.orc != nil && task.PipelineID != "" && task.PipelineTask != "" {
|
||||
_ = sm.orc.MarkTaskDone(task.PipelineID, task.PipelineTask, task.Result, nil)
|
||||
}
|
||||
}
|
||||
sm.mu.Unlock()
|
||||
}
|
||||
@@ -122,6 +152,9 @@ func (sm *SubagentManager) runTask(ctx context.Context, task *SubagentTask) {
|
||||
prefix = fmt.Sprintf("Task '%s' completed", task.Label)
|
||||
}
|
||||
announceContent := fmt.Sprintf("%s.\n\nResult:\n%s", prefix, task.Result)
|
||||
if task.PipelineID != "" && task.PipelineTask != "" {
|
||||
announceContent += fmt.Sprintf("\n\nPipeline: %s\nPipeline Task: %s", task.PipelineID, task.PipelineTask)
|
||||
}
|
||||
sm.bus.PublishInbound(bus.InboundMessage{
|
||||
Channel: "system",
|
||||
SenderID: fmt.Sprintf("subagent:%s", task.ID),
|
||||
|
||||
Reference in New Issue
Block a user