|
| 1 | +package agent |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log/slog" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/furisto/construct/backend/memory" |
| 10 | + memory_message "github.com/furisto/construct/backend/memory/message" |
| 11 | + "github.com/furisto/construct/backend/memory/schema/types" |
| 12 | + memory_task "github.com/furisto/construct/backend/memory/task" |
| 13 | + "github.com/furisto/construct/backend/model" |
| 14 | + "github.com/google/uuid" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + MaxTitleLength = 80 // characters |
| 19 | + MaxRetries = 2 // attempts to get shorter title |
| 20 | +) |
| 21 | + |
| 22 | +type TitleGenerator struct { |
| 23 | + memory *memory.Client |
| 24 | + providerFactory *ModelProviderFactory |
| 25 | +} |
| 26 | + |
| 27 | +func NewTitleGenerator(memory *memory.Client, providerFactory *ModelProviderFactory) *TitleGenerator { |
| 28 | + return &TitleGenerator{ |
| 29 | + memory: memory, |
| 30 | + providerFactory: providerFactory, |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func (g *TitleGenerator) GenerateTitle(ctx context.Context, taskID uuid.UUID) error { |
| 35 | + _, agent, err := g.fetchTaskWithAgent(ctx, taskID) |
| 36 | + if err != nil { |
| 37 | + return fmt.Errorf("failed to fetch task: %w", err) |
| 38 | + } |
| 39 | + |
| 40 | + messages, err := g.memory.Message.Query(). |
| 41 | + Where(memory_message.TaskIDEQ(taskID)). |
| 42 | + Order(memory_message.ByCreateTime()). |
| 43 | + Limit(5). |
| 44 | + All(ctx) |
| 45 | + if err != nil { |
| 46 | + return fmt.Errorf("failed to fetch messages: %w", err) |
| 47 | + } |
| 48 | + |
| 49 | + if !hasUserMessage(messages) { |
| 50 | + slog.DebugContext(ctx, "no user messages, skipping title generation", "task_id", taskID) |
| 51 | + return nil |
| 52 | + } |
| 53 | + |
| 54 | + modelProvider, err := g.providerFactory.CreateClient(ctx, agent.Edges.Model.ModelProviderID) |
| 55 | + if err != nil { |
| 56 | + return fmt.Errorf("failed to create model provider: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + modelMessages, err := g.buildMessageHistory(messages) |
| 60 | + if err != nil { |
| 61 | + return fmt.Errorf("failed to build message history: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + systemPrompt := `You are a title generator for development tasks and conversations. Generate a concise, descriptive title based on the user's request and conversation context. |
| 65 | +
|
| 66 | +CRITICAL RULES: |
| 67 | +- Maximum 8 words |
| 68 | +- Maximum 80 characters |
| 69 | +- Start immediately with the title (no preamble) |
| 70 | +- Use action verbs when describing tasks |
| 71 | +- Be specific about technology/domain when relevant |
| 72 | +- NO quotes, markdown, or extra punctuation |
| 73 | +- NO meta-commentary or explanations |
| 74 | +
|
| 75 | +GOOD EXAMPLES: |
| 76 | +- "Implement JWT authentication for API" |
| 77 | +- "Fix memory leak in worker pool" |
| 78 | +- "Add TypeScript support to build" |
| 79 | +- "Debug race condition in cache" |
| 80 | +- "Refactor database connection pooling" |
| 81 | +
|
| 82 | +BAD EXAMPLES (too vague): |
| 83 | +- "Help with code" |
| 84 | +- "Fix issue" |
| 85 | +- "Question about app" |
| 86 | +
|
| 87 | +BAD EXAMPLES (too long): |
| 88 | +- "Implement a comprehensive user authentication system with JWT tokens and refresh functionality" |
| 89 | +
|
| 90 | +For the given conversation, generate ONLY the title starting with a quote:` |
| 91 | + |
| 92 | + title, err := g.generateTitleWithRetry(ctx, modelProvider, systemPrompt, modelMessages, 0) |
| 93 | + if err != nil { |
| 94 | + return fmt.Errorf("failed to generate title: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + _, err = memory.Transaction(ctx, g.memory, func(tx *memory.Client) (*memory.Task, error) { |
| 98 | + return tx.Task.UpdateOneID(taskID).SetDescription(title).Save(ctx) |
| 99 | + }) |
| 100 | + if err != nil { |
| 101 | + return fmt.Errorf("failed to save title: %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + slog.InfoContext(ctx, "generated title for task", "task_id", taskID, "title", title) |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +func (g *TitleGenerator) generateTitleWithRetry( |
| 109 | + ctx context.Context, |
| 110 | + provider model.ModelProvider, |
| 111 | + systemPrompt string, |
| 112 | + messages []*model.Message, |
| 113 | + attempt int, |
| 114 | +) (string, error) { |
| 115 | + messagesWithPrefill := append(messages, &model.Message{ |
| 116 | + Source: model.MessageSourceModel, |
| 117 | + Content: []model.ContentBlock{ |
| 118 | + &model.TextBlock{Text: "\""}, |
| 119 | + }, |
| 120 | + }) |
| 121 | + |
| 122 | + var anthropicProvider *model.AnthropicProvider |
| 123 | + anthropicProvider, ok := provider.(*model.AnthropicProvider) |
| 124 | + if !ok { |
| 125 | + return "", fmt.Errorf("provider is not an Anthropic provider") |
| 126 | + } |
| 127 | + |
| 128 | + budgetModel := anthropicProvider.BudgetModel() |
| 129 | + |
| 130 | + response, err := provider.InvokeModel( |
| 131 | + ctx, |
| 132 | + budgetModel, |
| 133 | + systemPrompt, |
| 134 | + messagesWithPrefill, |
| 135 | + ) |
| 136 | + if err != nil { |
| 137 | + return "", err |
| 138 | + } |
| 139 | + |
| 140 | + title := extractTitle(response.Content) |
| 141 | + if title == "" { |
| 142 | + return "", fmt.Errorf("model returned empty title") |
| 143 | + } |
| 144 | + |
| 145 | + if len(title) > MaxTitleLength && attempt < MaxRetries { |
| 146 | + slog.DebugContext(ctx, "title too long, retrying", "attempt", attempt+1, "length", len(title), "title", title) |
| 147 | + |
| 148 | + messages = append(messages, |
| 149 | + &model.Message{ |
| 150 | + Source: model.MessageSourceModel, |
| 151 | + Content: []model.ContentBlock{ |
| 152 | + &model.TextBlock{Text: title}, |
| 153 | + }, |
| 154 | + }, |
| 155 | + &model.Message{ |
| 156 | + Source: model.MessageSourceUser, |
| 157 | + Content: []model.ContentBlock{ |
| 158 | + &model.TextBlock{ |
| 159 | + Text: "That title is too long. Please provide a shorter version (max 8 words, ~80 characters).", |
| 160 | + }, |
| 161 | + }, |
| 162 | + }, |
| 163 | + ) |
| 164 | + |
| 165 | + return g.generateTitleWithRetry(ctx, provider, systemPrompt, messages, attempt+1) |
| 166 | + } |
| 167 | + |
| 168 | + if len(title) > MaxTitleLength { |
| 169 | + slog.WarnContext(ctx, "title still too long after retries, truncating", "original_length", len(title)) |
| 170 | + title = title[:MaxTitleLength-3] + "..." |
| 171 | + } |
| 172 | + |
| 173 | + return title, nil |
| 174 | +} |
| 175 | + |
| 176 | +func (g *TitleGenerator) fetchTaskWithAgent(ctx context.Context, taskID uuid.UUID) (*memory.Task, *memory.Agent, error) { |
| 177 | + task, err := g.memory.Task.Query(). |
| 178 | + Where(memory_task.IDEQ(taskID)). |
| 179 | + WithAgent(func(query *memory.AgentQuery) { |
| 180 | + query.WithModel() |
| 181 | + }). |
| 182 | + Only(ctx) |
| 183 | + |
| 184 | + if err != nil { |
| 185 | + return nil, nil, err |
| 186 | + } |
| 187 | + |
| 188 | + if task.Edges.Agent == nil { |
| 189 | + return nil, nil, fmt.Errorf("no agent associated with task: %s", taskID) |
| 190 | + } |
| 191 | + |
| 192 | + return task, task.Edges.Agent, nil |
| 193 | +} |
| 194 | + |
| 195 | +func (g *TitleGenerator) buildMessageHistory(messages []*memory.Message) ([]*model.Message, error) { |
| 196 | + modelMessages := make([]*model.Message, 0, len(messages)) |
| 197 | + |
| 198 | + for _, msg := range messages { |
| 199 | + if msg.Source == types.MessageSourceUser || msg.Source == types.MessageSourceAssistant { |
| 200 | + modelMsg, err := ConvertMemoryMessageToModel(msg) |
| 201 | + if err != nil { |
| 202 | + return nil, err |
| 203 | + } |
| 204 | + modelMessages = append(modelMessages, modelMsg) |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + return modelMessages, nil |
| 209 | +} |
| 210 | + |
| 211 | +func extractTitle(content []model.ContentBlock) string { |
| 212 | + var title string |
| 213 | + for _, block := range content { |
| 214 | + if textBlock, ok := block.(*model.TextBlock); ok { |
| 215 | + title = textBlock.Text |
| 216 | + break |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + title = strings.Trim(title, "\" \n\t\r'`") |
| 221 | + title = strings.TrimSpace(title) |
| 222 | + |
| 223 | + return title |
| 224 | +} |
| 225 | + |
| 226 | +func hasUserMessage(messages []*memory.Message) bool { |
| 227 | + for _, msg := range messages { |
| 228 | + if msg.Source == types.MessageSourceUser { |
| 229 | + return true |
| 230 | + } |
| 231 | + } |
| 232 | + return false |
| 233 | +} |
0 commit comments