MCPcopy Index your code
hub / github.com/coder/coder / generateFromPrompt

Function generateFromPrompt

coderd/taskname/taskname.go:166–206  ·  view source on GitHub ↗

generateFromPrompt creates a task name directly from the prompt by sanitizing it. This is used as a fallback when Claude fails to generate a name.

(prompt string)

Source from the content-addressed store, hash-verified

164// generateFromPrompt creates a task name directly from the prompt by sanitizing it.
165// This is used as a fallback when Claude fails to generate a name.
166func generateFromPrompt(prompt string) (TaskName, error) {
167 // Normalize newlines and tabs to spaces
168 prompt = regexp.MustCompile(`[\n\r\t]+`).ReplaceAllString(prompt, " ")
169
170 // Truncate prompt to 27 chars with full words for task name generation
171 truncatedForName := prompt
172 if len(prompt) > 27 {
173 truncatedForName = strutil.Truncate(prompt, 27, strutil.TruncateWithFullWords)
174 }
175
176 // Generate task name from truncated prompt
177 name := strings.ToLower(truncatedForName)
178 // Replace whitespace (\t \r \n and spaces) sequences with hyphens
179 name = regexp.MustCompile(`\s+`).ReplaceAllString(name, "-")
180 // Remove all characters except lowercase letters, numbers, and hyphens
181 name = regexp.MustCompile(`[^a-z0-9-]+`).ReplaceAllString(name, "")
182 // Collapse multiple consecutive hyphens into a single hyphen
183 name = regexp.MustCompile(`-+`).ReplaceAllString(name, "-")
184 // Remove leading and trailing hyphens
185 name = strings.Trim(name, "-")
186
187 if len(name) == 0 {
188 return TaskName{}, ErrNoNameGenerated
189 }
190
191 taskName := fmt.Sprintf("%s-%s", name, generateSuffix())
192
193 // Use the initial prompt as display name, truncated to 64 chars with full words
194 displayName := strutil.Truncate(prompt, 64, strutil.TruncateWithFullWords, strutil.TruncateWithEllipsis)
195 displayName = strings.TrimSpace(displayName)
196 if len(displayName) == 0 {
197 // Ensure display name is never empty
198 displayName = strings.ReplaceAll(name, "-", " ")
199 }
200 displayName = strutil.Capitalize(displayName)
201
202 return TaskName{
203 Name: taskName,
204 DisplayName: displayName,
205 }, nil
206}
207
208// generateFromAnthropic uses Claude (Anthropic) to generate semantic task and display names from a user prompt.
209// It sends the prompt to Claude with a structured system prompt requesting JSON output containing both names.

Callers 2

GenerateFunction · 0.85
TestGenerateFromPromptFunction · 0.85

Calls 2

generateSuffixFunction · 0.85
MustCompileMethod · 0.80

Tested by 1

TestGenerateFromPromptFunction · 0.68