mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-04-13 01:57:28 +08:00
add role/from_me filters and trigger error aggregation
This commit is contained in:
@@ -93,12 +93,24 @@ func statusCmd() {
|
||||
if data, err := os.ReadFile(triggerStats); err == nil {
|
||||
fmt.Printf("Trigger Stats: %s\n", strings.TrimSpace(string(data)))
|
||||
}
|
||||
if errs, err := collectRecentTriggerErrors(filepath.Join(workspace, "memory", "trigger-audit.jsonl"), 5); err == nil && len(errs) > 0 {
|
||||
auditPath := filepath.Join(workspace, "memory", "trigger-audit.jsonl")
|
||||
if errs, err := collectRecentTriggerErrors(auditPath, 5); err == nil && len(errs) > 0 {
|
||||
fmt.Println("Recent Trigger Errors:")
|
||||
for _, e := range errs {
|
||||
fmt.Printf(" - %s\n", e)
|
||||
}
|
||||
}
|
||||
if agg, err := collectTriggerErrorCounts(auditPath); err == nil && len(agg) > 0 {
|
||||
fmt.Println("Trigger Error Counts:")
|
||||
keys := make([]string, 0, len(agg))
|
||||
for k := range agg {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, trigger := range keys {
|
||||
fmt.Printf(" %s: %d\n", trigger, agg[trigger])
|
||||
}
|
||||
}
|
||||
|
||||
sessionsDir := filepath.Join(filepath.Dir(configPath), "sessions")
|
||||
if kinds, err := collectSessionKindCounts(sessionsDir); err == nil && len(kinds) > 0 {
|
||||
@@ -182,6 +194,37 @@ func collectRecentTriggerErrors(path string, limit int) ([]string, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func collectTriggerErrorCounts(path string) (map[string]int, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
lines := strings.Split(strings.TrimSpace(string(data)), "\n")
|
||||
counts := map[string]int{}
|
||||
for _, line := range lines {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
var row struct {
|
||||
Trigger string `json:"trigger"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(line), &row); err != nil {
|
||||
continue
|
||||
}
|
||||
if strings.TrimSpace(row.Error) == "" {
|
||||
continue
|
||||
}
|
||||
trigger := strings.ToLower(strings.TrimSpace(row.Trigger))
|
||||
if trigger == "" {
|
||||
trigger = "unknown"
|
||||
}
|
||||
counts[trigger]++
|
||||
}
|
||||
return counts, nil
|
||||
}
|
||||
|
||||
func collectRecentSubagentSessions(sessionsDir string, limit int) ([]string, error) {
|
||||
entries, err := os.ReadDir(sessionsDir)
|
||||
if err != nil {
|
||||
|
||||
@@ -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