|
| 1 | +package project |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/helixml/helix/api/pkg/agent" |
| 9 | + "github.com/helixml/helix/api/pkg/store" |
| 10 | + "github.com/helixml/helix/api/pkg/system" |
| 11 | + "github.com/helixml/helix/api/pkg/types" |
| 12 | + "github.com/helixml/helix/api/pkg/util/jsonschema" |
| 13 | + "github.com/rs/zerolog/log" |
| 14 | + "github.com/sashabaranov/go-openai" |
| 15 | +) |
| 16 | + |
| 17 | +// CreateSpecTaskTool - creates a new spec task |
| 18 | + |
| 19 | +var createSpecTaskParameters = jsonschema.Definition{ |
| 20 | + Type: jsonschema.Object, |
| 21 | + Properties: map[string]jsonschema.Definition{ |
| 22 | + "name": { |
| 23 | + Type: jsonschema.String, |
| 24 | + Description: "Short, descriptive name for the task", |
| 25 | + }, |
| 26 | + "description": { |
| 27 | + Type: jsonschema.String, |
| 28 | + Description: "Detailed description of what needs to be done", |
| 29 | + }, |
| 30 | + "type": { |
| 31 | + Type: jsonschema.String, |
| 32 | + Description: "Task type: feature, bug, or refactor", |
| 33 | + Enum: []string{"feature", "bug", "refactor"}, |
| 34 | + }, |
| 35 | + "priority": { |
| 36 | + Type: jsonschema.String, |
| 37 | + Description: "Task priority: low, medium, high, or critical", |
| 38 | + Enum: []string{"low", "medium", "high", "critical"}, |
| 39 | + }, |
| 40 | + "original_prompt": { |
| 41 | + Type: jsonschema.String, |
| 42 | + Description: "The original user request or prompt that led to this task", |
| 43 | + }, |
| 44 | + }, |
| 45 | + Required: []string{"name", "description"}, |
| 46 | +} |
| 47 | + |
| 48 | +type CreateSpecTaskTool struct { |
| 49 | + store store.Store |
| 50 | + projectID string |
| 51 | +} |
| 52 | + |
| 53 | +func NewCreateSpecTaskTool(projectID string, store store.Store) *CreateSpecTaskTool { |
| 54 | + return &CreateSpecTaskTool{ |
| 55 | + store: store, |
| 56 | + projectID: projectID, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +var _ agent.Tool = &CreateSpecTaskTool{} |
| 61 | + |
| 62 | +func (t *CreateSpecTaskTool) Name() string { |
| 63 | + return "CreateSpecTask" |
| 64 | +} |
| 65 | + |
| 66 | +func (t *CreateSpecTaskTool) Description() string { |
| 67 | + return "Create a new spec-driven task in the project" |
| 68 | +} |
| 69 | + |
| 70 | +func (t *CreateSpecTaskTool) String() string { |
| 71 | + return "CreateSpecTask" |
| 72 | +} |
| 73 | + |
| 74 | +func (t *CreateSpecTaskTool) StatusMessage() string { |
| 75 | + return "Creating spec task" |
| 76 | +} |
| 77 | + |
| 78 | +func (t *CreateSpecTaskTool) Icon() string { |
| 79 | + return "AddIcon" |
| 80 | +} |
| 81 | + |
| 82 | +func (t *CreateSpecTaskTool) OpenAI() []openai.Tool { |
| 83 | + return []openai.Tool{ |
| 84 | + { |
| 85 | + Type: openai.ToolTypeFunction, |
| 86 | + Function: &openai.FunctionDefinition{ |
| 87 | + Name: "CreateSpecTask", |
| 88 | + Description: "Create a new spec-driven task in the project", |
| 89 | + Parameters: createSpecTaskParameters, |
| 90 | + }, |
| 91 | + }, |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +type CreateSpecTaskResult struct { |
| 96 | + ID string `json:"id"` |
| 97 | + Name string `json:"name"` |
| 98 | + Description string `json:"description"` |
| 99 | + Status string `json:"status"` |
| 100 | + Priority string `json:"priority"` |
| 101 | + Type string `json:"type"` |
| 102 | + Message string `json:"message"` |
| 103 | +} |
| 104 | + |
| 105 | +func (r *CreateSpecTaskResult) ToString() string { |
| 106 | + return fmt.Sprintf("ID: %s\nTask: %s\nDescription: %s\nStatus: %s\nPriority: %s\nType: %s\nMessage: %s", |
| 107 | + r.ID, r.Name, r.Description, r.Status, r.Priority, r.Type, r.Message) |
| 108 | +} |
| 109 | + |
| 110 | +func (t *CreateSpecTaskTool) Execute(ctx context.Context, meta agent.Meta, args map[string]interface{}) (string, error) { |
| 111 | + projectID := t.projectID |
| 112 | + if projectID == "" { |
| 113 | + projectContext, ok := types.GetHelixProjectContext(ctx) |
| 114 | + if !ok { |
| 115 | + return "", fmt.Errorf("helix project context not found") |
| 116 | + } |
| 117 | + projectID = projectContext.ProjectID |
| 118 | + } |
| 119 | + |
| 120 | + log.Info(). |
| 121 | + Str("project_id", projectID). |
| 122 | + Str("user_id", meta.UserID). |
| 123 | + Str("session_id", meta.SessionID). |
| 124 | + Interface("args", args). |
| 125 | + Msg("Executing CreateSpecTask tool") |
| 126 | + |
| 127 | + name, ok := args["name"].(string) |
| 128 | + if !ok || name == "" { |
| 129 | + return "", fmt.Errorf("name is required") |
| 130 | + } |
| 131 | + |
| 132 | + description, ok := args["description"].(string) |
| 133 | + if !ok || description == "" { |
| 134 | + return "", fmt.Errorf("description is required") |
| 135 | + } |
| 136 | + |
| 137 | + taskType := "feature" |
| 138 | + if t, ok := args["type"].(string); ok && t != "" { |
| 139 | + taskType = t |
| 140 | + } |
| 141 | + |
| 142 | + priority := types.SpecTaskPriorityMedium |
| 143 | + if p, ok := args["priority"].(string); ok && p != "" { |
| 144 | + priority = types.SpecTaskPriority(p) |
| 145 | + } |
| 146 | + |
| 147 | + originalPrompt := "" |
| 148 | + if op, ok := args["original_prompt"].(string); ok { |
| 149 | + originalPrompt = op |
| 150 | + } |
| 151 | + |
| 152 | + task := &types.SpecTask{ |
| 153 | + ID: system.GenerateSpecTaskID(), |
| 154 | + ProjectID: projectID, |
| 155 | + Name: name, |
| 156 | + Description: description, |
| 157 | + Type: taskType, |
| 158 | + Priority: priority, |
| 159 | + Status: types.TaskStatusBacklog, |
| 160 | + OriginalPrompt: originalPrompt, |
| 161 | + CreatedBy: meta.UserID, |
| 162 | + CreatedAt: time.Now(), |
| 163 | + UpdatedAt: time.Now(), |
| 164 | + } |
| 165 | + |
| 166 | + err := t.store.CreateSpecTask(ctx, task) |
| 167 | + if err != nil { |
| 168 | + log.Error().Err(err).Str("project_id", projectID).Msg("Failed to create spec task") |
| 169 | + return "", fmt.Errorf("failed to create spec task: %w", err) |
| 170 | + } |
| 171 | + |
| 172 | + result := CreateSpecTaskResult{ |
| 173 | + ID: task.ID, |
| 174 | + Name: task.Name, |
| 175 | + Description: task.Description, |
| 176 | + Status: string(task.Status), |
| 177 | + Priority: string(task.Priority), |
| 178 | + Type: task.Type, |
| 179 | + Message: "Task created successfully", |
| 180 | + } |
| 181 | + |
| 182 | + return result.ToString(), nil |
| 183 | +} |
0 commit comments