Skip to content

Commit f8b0b84

Browse files
refactor: generate task name fallback on coderd
Instead of generating the fallback task name on the website, we instead generate it on coderd.
1 parent 560cf84 commit f8b0b84

File tree

6 files changed

+25
-19
lines changed

6 files changed

+25
-19
lines changed

coderd/aitasks.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (api *API) tasksCreate(rw http.ResponseWriter, r *http.Request) {
107107
return
108108
}
109109

110-
taskName := req.Name
110+
taskName := taskname.GenerateFallback()
111111
if anthropicAPIKey := taskname.GetAnthropicAPIKeyFromEnv(); anthropicAPIKey != "" {
112112
anthropicModel := taskname.GetAnthropicModelFromEnv()
113113

@@ -118,6 +118,7 @@ func (api *API) tasksCreate(rw http.ResponseWriter, r *http.Request) {
118118
taskName = generatedName
119119
}
120120
}
121+
taskName += "-" + taskname.GenerateSuffix()
121122

122123
createReq := codersdk.CreateWorkspaceRequest{
123124
Name: taskName,

coderd/aitasks_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ func TestTaskCreate(t *testing.T) {
151151
var (
152152
ctx = testutil.Context(t, testutil.WaitShort)
153153

154-
taskName = "task-foo-bar-baz"
155154
taskPrompt = "Some task prompt"
156155
)
157156

@@ -176,15 +175,14 @@ func TestTaskCreate(t *testing.T) {
176175

177176
// When: We attempt to create a Task.
178177
workspace, err := expClient.CreateTask(ctx, "me", codersdk.CreateTaskRequest{
179-
Name: taskName,
180178
TemplateVersionID: template.ActiveVersionID,
181179
Prompt: taskPrompt,
182180
})
183181
require.NoError(t, err)
184182
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
185183

186184
// Then: We expect a workspace to have been created.
187-
assert.Equal(t, taskName, workspace.Name)
185+
assert.NotEmpty(t, workspace.Name)
188186
assert.Equal(t, template.ID, workspace.TemplateID)
189187

190188
// And: We expect it to have the "AI Prompt" parameter correctly set.
@@ -201,7 +199,6 @@ func TestTaskCreate(t *testing.T) {
201199
var (
202200
ctx = testutil.Context(t, testutil.WaitShort)
203201

204-
taskName = "task-foo-bar-baz"
205202
taskPrompt = "Some task prompt"
206203
)
207204

@@ -217,7 +214,6 @@ func TestTaskCreate(t *testing.T) {
217214

218215
// When: We attempt to create a Task.
219216
_, err := expClient.CreateTask(ctx, "me", codersdk.CreateTaskRequest{
220-
Name: taskName,
221217
TemplateVersionID: template.ActiveVersionID,
222218
Prompt: taskPrompt,
223219
})
@@ -235,7 +231,6 @@ func TestTaskCreate(t *testing.T) {
235231
var (
236232
ctx = testutil.Context(t, testutil.WaitShort)
237233

238-
taskName = "task-foo-bar-baz"
239234
taskPrompt = "Some task prompt"
240235
)
241236

@@ -251,7 +246,6 @@ func TestTaskCreate(t *testing.T) {
251246

252247
// When: We attempt to create a Task with an invalid template version ID.
253248
_, err := expClient.CreateTask(ctx, "me", codersdk.CreateTaskRequest{
254-
Name: taskName,
255249
TemplateVersionID: uuid.New(),
256250
Prompt: taskPrompt,
257251
})

coderd/taskname/taskname.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package taskname
22

33
import (
44
"context"
5+
"fmt"
56
"io"
7+
"math/rand/v2"
68
"os"
79

810
"github.com/anthropics/anthropic-sdk-go"
911
anthropicoption "github.com/anthropics/anthropic-sdk-go/option"
12+
"github.com/moby/moby/pkg/namesgenerator"
1013
"golang.org/x/xerrors"
1114

1215
"github.com/coder/aisdk-go"
@@ -20,19 +23,17 @@ const (
2023
Requirements:
2124
- Only lowercase letters, numbers, and hyphens
2225
- Start with "task-"
23-
- End with a random number between 0-99
24-
- Maximum 32 characters total
26+
- Maximum 28 characters total
2527
- Descriptive of the main task
2628
2729
Examples:
28-
- "Help me debug a Python script" → "task-python-debug-12"
29-
- "Create a React dashboard component" → "task-react-dashboard-93"
30-
- "Analyze sales data from Q3" → "task-analyze-q3-sales-37"
31-
- "Set up CI/CD pipeline" → "task-setup-cicd-44"
30+
- "Help me debug a Python script" → "task-python-debug"
31+
- "Create a React dashboard component" → "task-react-dashboard"
32+
- "Analyze sales data from Q3" → "task-analyze-q3-sales"
33+
- "Set up CI/CD pipeline" → "task-setup-cicd"
3234
3335
If you cannot create a suitable name:
34-
- Respond with "task-unnamed"
35-
- Do not end with a random number`
36+
- Respond with "task-unnamed"`
3637
)
3738

3839
var (
@@ -67,6 +68,19 @@ func GetAnthropicModelFromEnv() anthropic.Model {
6768
return anthropic.Model(os.Getenv("ANTHROPIC_MODEL"))
6869
}
6970

71+
// GenerateSuffix generates a random hex string between `100` and `fff`.
72+
func GenerateSuffix() string {
73+
min := 0x100
74+
max := 0x1000
75+
num := rand.IntN(max-min) + min
76+
77+
return fmt.Sprintf("%x", num)
78+
}
79+
80+
func GenerateFallback() string {
81+
return namesgenerator.GetRandomName(0)
82+
}
83+
7084
func Generate(ctx context.Context, prompt string, opts ...Option) (string, error) {
7185
o := options{}
7286
for _, opt := range opts {

codersdk/aitasks.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ func (c *ExperimentalClient) AITaskPrompts(ctx context.Context, buildIDs []uuid.
4747
}
4848

4949
type CreateTaskRequest struct {
50-
Name string `json:"name"`
5150
TemplateVersionID uuid.UUID `json:"template_version_id" format:"uuid"`
5251
TemplateVersionPresetID uuid.UUID `json:"template_version_preset_id,omitempty" format:"uuid"`
5352
Prompt string `json:"prompt"`

site/src/api/typesGenerated.ts

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

site/src/pages/TasksPage/TasksPage.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,6 @@ export const data = {
741741
}
742742

743743
const workspace = await API.experimental.createTask(userId, {
744-
name: `task-${generateWorkspaceName()}`,
745744
template_version_id: templateVersionId,
746745
template_version_preset_id: preset_id || undefined,
747746
prompt,

0 commit comments

Comments
 (0)