mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-04-15 03:57:30 +08:00
add role/from_me filters and trigger error aggregation
This commit is contained in:
@@ -43,6 +43,8 @@ func (t *SessionsTool) Parameters() map[string]interface{} {
|
||||
"kinds": map[string]interface{}{"type": "array", "items": map[string]interface{}{"type": "string"}, "description": "optional session kinds filter for list"},
|
||||
"query": map[string]interface{}{"type": "string", "description": "optional text query for list or history"},
|
||||
"include_tools": map[string]interface{}{"type": "boolean", "description": "include tool role messages in history", "default": false},
|
||||
"from_me": map[string]interface{}{"type": "boolean", "description": "history only: filter assistant messages when true, user messages when false"},
|
||||
"role": map[string]interface{}{"type": "string", "description": "history only: filter by role, e.g. user|assistant|tool|system"},
|
||||
"around": map[string]interface{}{"type": "integer", "description": "1-indexed message index center for history window"},
|
||||
"before": map[string]interface{}{"type": "integer", "description": "1-indexed message index upper bound (exclusive)"},
|
||||
"after": map[string]interface{}{"type": "integer", "description": "1-indexed message index lower bound (exclusive)"},
|
||||
@@ -81,6 +83,14 @@ func (t *SessionsTool) Execute(ctx context.Context, args map[string]interface{})
|
||||
}
|
||||
query, _ := args["query"].(string)
|
||||
query = strings.ToLower(strings.TrimSpace(query))
|
||||
roleFilter, _ := args["role"].(string)
|
||||
roleFilter = strings.ToLower(strings.TrimSpace(roleFilter))
|
||||
fromMeSet := false
|
||||
fromMe := false
|
||||
if v, ok := args["from_me"].(bool); ok {
|
||||
fromMeSet = true
|
||||
fromMe = v
|
||||
}
|
||||
kindFilter := map[string]struct{}{}
|
||||
if rawKinds, ok := args["kinds"].([]interface{}); ok {
|
||||
for _, it := range rawKinds {
|
||||
@@ -215,6 +225,28 @@ func (t *SessionsTool) Execute(ctx context.Context, args map[string]interface{})
|
||||
}
|
||||
h = filtered
|
||||
}
|
||||
if roleFilter != "" {
|
||||
filtered := make([]providers.Message, 0, len(h))
|
||||
for _, m := range h {
|
||||
if strings.ToLower(strings.TrimSpace(m.Role)) == roleFilter {
|
||||
filtered = append(filtered, m)
|
||||
}
|
||||
}
|
||||
h = filtered
|
||||
}
|
||||
if fromMeSet {
|
||||
targetRole := "user"
|
||||
if fromMe {
|
||||
targetRole = "assistant"
|
||||
}
|
||||
filtered := make([]providers.Message, 0, len(h))
|
||||
for _, m := range h {
|
||||
if strings.ToLower(strings.TrimSpace(m.Role)) == targetRole {
|
||||
filtered = append(filtered, m)
|
||||
}
|
||||
}
|
||||
h = filtered
|
||||
}
|
||||
if query != "" {
|
||||
filtered := make([]providers.Message, 0, len(h))
|
||||
for _, m := range h {
|
||||
|
||||
@@ -50,3 +50,25 @@ func TestSessionsToolHistoryWithoutTools(t *testing.T) {
|
||||
t.Fatalf("tool message should be filtered: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionsToolHistoryFromMe(t *testing.T) {
|
||||
tool := NewSessionsTool(nil, func(key string, limit int) []providers.Message {
|
||||
return []providers.Message{
|
||||
{Role: "user", Content: "u1"},
|
||||
{Role: "assistant", Content: "a1"},
|
||||
{Role: "assistant", Content: "a2"},
|
||||
}
|
||||
})
|
||||
|
||||
out, err := tool.Execute(context.Background(), map[string]interface{}{
|
||||
"action": "history",
|
||||
"key": "telegram:1",
|
||||
"from_me": true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(out, "u1") || !strings.Contains(out, "a1") {
|
||||
t.Fatalf("unexpected filtered output: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SubagentsTool struct {
|
||||
@@ -26,9 +27,10 @@ func (t *SubagentsTool) Parameters() map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{
|
||||
"action": map[string]interface{}{"type": "string", "description": "list|info|kill|steer|send|log"},
|
||||
"id": map[string]interface{}{"type": "string", "description": "subagent id/#index/all for info/kill/steer/send/log"},
|
||||
"message": map[string]interface{}{"type": "string", "description": "steering message for steer/send action"},
|
||||
"action": map[string]interface{}{"type": "string", "description": "list|info|kill|steer|send|log"},
|
||||
"id": map[string]interface{}{"type": "string", "description": "subagent id/#index/all for info/kill/steer/send/log"},
|
||||
"message": map[string]interface{}{"type": "string", "description": "steering message for steer/send action"},
|
||||
"recent_minutes": map[string]interface{}{"type": "integer", "description": "optional list/info all filter by recent updated minutes"},
|
||||
},
|
||||
"required": []string{"action"},
|
||||
}
|
||||
@@ -45,10 +47,14 @@ func (t *SubagentsTool) Execute(ctx context.Context, args map[string]interface{}
|
||||
id = strings.TrimSpace(id)
|
||||
message, _ := args["message"].(string)
|
||||
message = strings.TrimSpace(message)
|
||||
recentMinutes := 0
|
||||
if v, ok := args["recent_minutes"].(float64); ok && int(v) > 0 {
|
||||
recentMinutes = int(v)
|
||||
}
|
||||
|
||||
switch action {
|
||||
case "list":
|
||||
tasks := t.manager.ListTasks()
|
||||
tasks := t.filterRecent(t.manager.ListTasks(), recentMinutes)
|
||||
if len(tasks) == 0 {
|
||||
return "No subagents.", nil
|
||||
}
|
||||
@@ -61,7 +67,7 @@ func (t *SubagentsTool) Execute(ctx context.Context, args map[string]interface{}
|
||||
return strings.TrimSpace(sb.String()), nil
|
||||
case "info":
|
||||
if strings.EqualFold(strings.TrimSpace(id), "all") {
|
||||
tasks := t.manager.ListTasks()
|
||||
tasks := t.filterRecent(t.manager.ListTasks(), recentMinutes)
|
||||
if len(tasks) == 0 {
|
||||
return "No subagents.", nil
|
||||
}
|
||||
@@ -84,7 +90,7 @@ func (t *SubagentsTool) Execute(ctx context.Context, args map[string]interface{}
|
||||
return fmt.Sprintf("ID: %s\nStatus: %s\nLabel: %s\nCreated: %d\nUpdated: %d\nSteering Count: %d\nTask: %s\nResult:\n%s", task.ID, task.Status, task.Label, task.Created, task.Updated, len(task.Steering), task.Task, task.Result), nil
|
||||
case "kill":
|
||||
if strings.EqualFold(strings.TrimSpace(id), "all") {
|
||||
tasks := t.manager.ListTasks()
|
||||
tasks := t.filterRecent(t.manager.ListTasks(), recentMinutes)
|
||||
if len(tasks) == 0 {
|
||||
return "No subagents.", nil
|
||||
}
|
||||
@@ -169,3 +175,17 @@ func (t *SubagentsTool) resolveTaskID(idOrIndex string) (string, error) {
|
||||
}
|
||||
return idOrIndex, nil
|
||||
}
|
||||
|
||||
func (t *SubagentsTool) filterRecent(tasks []*SubagentTask, recentMinutes int) []*SubagentTask {
|
||||
if recentMinutes <= 0 {
|
||||
return tasks
|
||||
}
|
||||
cutoff := time.Now().Add(-time.Duration(recentMinutes) * time.Minute).UnixMilli()
|
||||
out := make([]*SubagentTask, 0, len(tasks))
|
||||
for _, task := range tasks {
|
||||
if task.Updated >= cutoff {
|
||||
out = append(out, task)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -20,3 +20,18 @@ func TestSubagentsInfoAll(t *testing.T) {
|
||||
t.Fatalf("unexpected output: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubagentsKillAll(t *testing.T) {
|
||||
m := NewSubagentManager(nil, ".", nil, nil)
|
||||
m.tasks["subagent-1"] = &SubagentTask{ID: "subagent-1", Status: "running", Label: "a", Created: 2}
|
||||
m.tasks["subagent-2"] = &SubagentTask{ID: "subagent-2", Status: "running", Label: "b", Created: 3}
|
||||
|
||||
tool := NewSubagentsTool(m)
|
||||
out, err := tool.Execute(context.Background(), map[string]interface{}{"action": "kill", "id": "all"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(out, "2") {
|
||||
t.Fatalf("unexpected kill output: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user