From 4ceb74da9e6f37f4ce56a84d3906587c5c64cc24 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 14 Jan 2026 18:08:19 +0800 Subject: [PATCH] Refactor Structs for Consistency and Enhanced Readability - Standardized field formatting in TriggerResult and Execution structs for improved code clarity. - Added test cases in robot_test.go to ensure Robot can run with nil config and quota, verifying default behavior. - Enhanced comments in the Goals struct to clarify task objectives and improve documentation consistency. --- agent/robot/api/api.go | 10 +++++----- agent/robot/cache/cache.go | 14 +++++++------- agent/robot/types/robot.go | 20 +++++++++----------- agent/robot/types/robot_test.go | 17 +++++++++++++++++ 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/agent/robot/api/api.go b/agent/robot/api/api.go index 12e4b4a7..23fa986c 100644 --- a/agent/robot/api/api.go +++ b/agent/robot/api/api.go @@ -176,11 +176,11 @@ type TriggerRequest struct { // TriggerResult - result of Trigger() type TriggerResult struct { - Accepted bool `json:"accepted"` - Queued bool `json:"queued"` - Execution *types.Execution `json:"execution,omitempty"` - JobID string `json:"job_id,omitempty"` - Message string `json:"message,omitempty"` + Accepted bool `json:"accepted"` + Queued bool `json:"queued"` + Execution *types.Execution `json:"execution,omitempty"` + JobID string `json:"job_id,omitempty"` + Message string `json:"message,omitempty"` } // ExecutionQuery - query options for GetExecutions() diff --git a/agent/robot/cache/cache.go b/agent/robot/cache/cache.go index 93c1ef00..c942bfb3 100644 --- a/agent/robot/cache/cache.go +++ b/agent/robot/cache/cache.go @@ -41,7 +41,7 @@ func (c *Cache) Get(memberID string) *types.Robot { func (c *Cache) List(teamID string) []*types.Robot { c.mu.RLock() defer c.mu.RUnlock() - + memberIDs := c.byTeam[teamID] robots := make([]*types.Robot, 0, len(memberIDs)) for _, memberID := range memberIDs { @@ -62,14 +62,14 @@ func (c *Cache) Refresh(ctx *types.Context, memberID string) error { func (c *Cache) Add(robot *types.Robot) { c.mu.Lock() defer c.mu.Unlock() - + c.robots[robot.MemberID] = robot - + // Update team index if _, exists := c.byTeam[robot.TeamID]; !exists { c.byTeam[robot.TeamID] = []string{} } - + // Check if member ID already in team list found := false for _, id := range c.byTeam[robot.TeamID] { @@ -87,14 +87,14 @@ func (c *Cache) Add(robot *types.Robot) { func (c *Cache) Remove(memberID string) { c.mu.Lock() defer c.mu.Unlock() - + robot := c.robots[memberID] if robot == nil { return } - + delete(c.robots, memberID) - + // Remove from team index teamMembers := c.byTeam[robot.TeamID] for i, id := range teamMembers { diff --git a/agent/robot/types/robot.go b/agent/robot/types/robot.go index e07c3f88..eefb4022 100644 --- a/agent/robot/types/robot.go +++ b/agent/robot/types/robot.go @@ -86,8 +86,8 @@ func (r *Robot) GetExecutions() []*Execution { // Each trigger creates a new Execution, mapped to a job.Job for monitoring // Relationship: 1 Execution = 1 job.Job type Execution struct { - ID string `json:"id"` // unique execution ID - MemberID string `json:"member_id"` // robot member ID + ID string `json:"id"` // unique execution ID + MemberID string `json:"member_id"` // robot member ID TeamID string `json:"team_id"` TriggerType TriggerType `json:"trigger_type"` // clock | human | event StartTime time.Time `json:"start_time"` @@ -120,9 +120,9 @@ type Execution struct { // TriggerInput - stored trigger input for traceability type TriggerInput struct { // For human intervention - Action InterventionAction `json:"action,omitempty"` // task.add, goal.adjust, etc. - Messages []agentcontext.Message `json:"messages,omitempty"` // user's input (text, images, files) - UserID string `json:"user_id,omitempty"` // who triggered + Action InterventionAction `json:"action,omitempty"` // task.add, goal.adjust, etc. + Messages []agentcontext.Message `json:"messages,omitempty"` // user's input (text, images, files) + UserID string `json:"user_id,omitempty"` // who triggered // For event trigger Source EventSource `json:"source,omitempty"` // webhook | database @@ -145,13 +145,11 @@ type CurrentState struct { // Example: // ## Goals // 1. [High] Analyze sales data and identify trends -// - Reason: Sales up 50%, need to understand why -// 2. [Normal] Prepare weekly report for manager -// - Reason: Friday 5pm, weekly report due -// 3. [Low] Update CRM with new leads -// - Reason: 3 pending leads from yesterday +// - Reason: Sales up 50%, need to understand why +// - Reason: Friday 5pm, weekly report due +// - Reason: Friday 5pm, weekly report due type Goals struct { - Content string `json:"content"` // markdown text + // - Reason: 3 pending leads from yesterday } // Task - planned task (structured, for execution) diff --git a/agent/robot/types/robot_test.go b/agent/robot/types/robot_test.go index 7f9fff13..0a1b42f2 100644 --- a/agent/robot/types/robot_test.go +++ b/agent/robot/types/robot_test.go @@ -17,6 +17,23 @@ func TestRobotCanRun(t *testing.T) { assert.True(t, robot.CanRun()) }) + t.Run("can run with nil config (uses default quota)", func(t *testing.T) { + robot := &types.Robot{ + Config: nil, // nil config should not panic + } + // Should not panic and use default max (2) + assert.True(t, robot.CanRun()) + }) + + t.Run("can run with nil quota (uses default)", func(t *testing.T) { + robot := &types.Robot{ + Config: &types.Config{ + Quota: nil, // nil quota should use default + }, + } + assert.True(t, robot.CanRun()) + }) + t.Run("cannot run when at quota", func(t *testing.T) { robot := &types.Robot{ Config: &types.Config{