- Notifications
You must be signed in to change notification settings - Fork1k
feat(coderd): generate task names based on their prompt#19335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
DanielleMaywood merged 13 commits intomainfromdanielle/tasks/generate-task-name-on-coderdAug 19, 2025
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
13 commits Select commitHold shift + click to select a range
afd1c70
feat(coderd): generate task name based on prompt using llm
DanielleMaywood1ed234e
refactor: slightly
DanielleMaywood706c789
refactor: slightly again
DanielleMaywood29f446a
refactor: remove space from prompt
DanielleMaywood7bd118e
chore: appease linter and formatter
DanielleMaywood8f51a4c
chore: remove excesss configuration
DanielleMaywood76b494a
test: add
DanielleMaywood8bdea7e
chore: appease linter and formatter
DanielleMaywood38bc49f
chore: some feedback
DanielleMaywood72595d6
chore: slightly logic oopsie
DanielleMaywood9562664
chore: replace `errors` with `xerrors`
DanielleMaywoodf5b43a9
Merge branch 'main' into danielle/tasks/generate-task-name-on-coderd
DanielleMaywood0eed2ae
chore: export option type
DanielleMaywoodFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
17 changes: 16 additions & 1 deletioncoderd/aitasks.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletionscoderd/taskname/taskname.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package taskname | ||
import ( | ||
"context" | ||
"io" | ||
"os" | ||
"github.com/anthropics/anthropic-sdk-go" | ||
anthropicoption "github.com/anthropics/anthropic-sdk-go/option" | ||
"golang.org/x/xerrors" | ||
"github.com/coder/aisdk-go" | ||
"github.com/coder/coder/v2/codersdk" | ||
) | ||
const ( | ||
defaultModel = anthropic.ModelClaude3_5HaikuLatest | ||
systemPrompt = `Generate a short workspace name from this AI task prompt. | ||
Requirements: | ||
- Only lowercase letters, numbers, and hyphens | ||
- Start with "task-" | ||
- End with a random number between 0-99 | ||
- Maximum 32 characters total | ||
- Descriptive of the main task | ||
Examples: | ||
- "Help me debug a Python script" → "task-python-debug-12" | ||
- "Create a React dashboard component" → "task-react-dashboard-93" | ||
- "Analyze sales data from Q3" → "task-analyze-q3-sales-37" | ||
- "Set up CI/CD pipeline" → "task-setup-cicd-44" | ||
If you cannot create a suitable name: | ||
- Respond with "task-unnamed" | ||
- Do not end with a random number` | ||
) | ||
var ( | ||
ErrNoAPIKey = xerrors.New("no api key provided") | ||
ErrNoNameGenerated = xerrors.New("no task name generated") | ||
) | ||
type options struct { | ||
apiKey string | ||
model anthropic.Model | ||
} | ||
type Option func(o *options) | ||
func WithAPIKey(apiKey string) Option { | ||
return func(o *options) { | ||
o.apiKey = apiKey | ||
} | ||
} | ||
func WithModel(model anthropic.Model) Option { | ||
return func(o *options) { | ||
o.model = model | ||
} | ||
} | ||
func GetAnthropicAPIKeyFromEnv() string { | ||
return os.Getenv("ANTHROPIC_API_KEY") | ||
} | ||
func GetAnthropicModelFromEnv() anthropic.Model { | ||
return anthropic.Model(os.Getenv("ANTHROPIC_MODEL")) | ||
} | ||
func Generate(ctx context.Context, prompt string, opts ...Option) (string, error) { | ||
o := options{} | ||
for _, opt := range opts { | ||
opt(&o) | ||
} | ||
if o.model == "" { | ||
o.model = defaultModel | ||
} | ||
if o.apiKey == "" { | ||
return "", ErrNoAPIKey | ||
} | ||
conversation := []aisdk.Message{ | ||
{ | ||
Role: "system", | ||
Parts: []aisdk.Part{{ | ||
Type: aisdk.PartTypeText, | ||
Text: systemPrompt, | ||
}}, | ||
}, | ||
{ | ||
Role: "user", | ||
Parts: []aisdk.Part{{ | ||
Type: aisdk.PartTypeText, | ||
Text: prompt, | ||
}}, | ||
}, | ||
} | ||
anthropicOptions := anthropic.DefaultClientOptions() | ||
anthropicOptions = append(anthropicOptions, anthropicoption.WithAPIKey(o.apiKey)) | ||
anthropicClient := anthropic.NewClient(anthropicOptions...) | ||
stream, err := anthropicDataStream(ctx, anthropicClient, o.model, conversation) | ||
if err != nil { | ||
return "", xerrors.Errorf("create anthropic data stream: %w", err) | ||
} | ||
var acc aisdk.DataStreamAccumulator | ||
stream = stream.WithAccumulator(&acc) | ||
if err := stream.Pipe(io.Discard); err != nil { | ||
return "", xerrors.Errorf("pipe data stream") | ||
} | ||
if len(acc.Messages()) == 0 { | ||
return "", ErrNoNameGenerated | ||
} | ||
generatedName := acc.Messages()[0].Content | ||
if err := codersdk.NameValid(generatedName); err != nil { | ||
return "", xerrors.Errorf("generated name %v not valid: %w", generatedName, err) | ||
} | ||
if generatedName == "task-unnamed" { | ||
return "", ErrNoNameGenerated | ||
} | ||
return generatedName, nil | ||
} | ||
func anthropicDataStream(ctx context.Context, client anthropic.Client, model anthropic.Model, input []aisdk.Message) (aisdk.DataStream, error) { | ||
messages, system, err := aisdk.MessagesToAnthropic(input) | ||
if err != nil { | ||
return nil, xerrors.Errorf("convert messages to anthropic format: %w", err) | ||
} | ||
return aisdk.AnthropicToDataStream(client.Messages.NewStreaming(ctx, anthropic.MessageNewParams{ | ||
Model: model, | ||
MaxTokens: 24, | ||
System: system, | ||
Messages: messages, | ||
})), nil | ||
} |
48 changes: 48 additions & 0 deletionscoderd/taskname/taskname_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package taskname_test | ||
import ( | ||
"os" | ||
"testing" | ||
"github.com/stretchr/testify/require" | ||
"github.com/coder/coder/v2/coderd/taskname" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/coder/v2/testutil" | ||
) | ||
const ( | ||
anthropicEnvVar = "ANTHROPIC_API_KEY" | ||
) | ||
func TestGenerateTaskName(t *testing.T) { | ||
t.Parallel() | ||
t.Run("Fallback", func(t *testing.T) { | ||
t.Parallel() | ||
ctx := testutil.Context(t, testutil.WaitShort) | ||
name, err := taskname.Generate(ctx, "Some random prompt") | ||
require.ErrorIs(t, err, taskname.ErrNoAPIKey) | ||
require.Equal(t, "", name) | ||
}) | ||
t.Run("Anthropic", func(t *testing.T) { | ||
t.Parallel() | ||
apiKey := os.Getenv(anthropicEnvVar) | ||
if apiKey == "" { | ||
t.Skipf("Skipping test as %s not set", anthropicEnvVar) | ||
} | ||
ctx := testutil.Context(t, testutil.WaitShort) | ||
name, err := taskname.Generate(ctx, "Create a finance planning app", taskname.WithAPIKey(apiKey)) | ||
require.NoError(t, err) | ||
require.NotEqual(t, "", name) | ||
err = codersdk.NameValid(name) | ||
require.NoError(t, err, "name should be valid") | ||
}) | ||
} |
2 changes: 1 addition & 1 deletiongo.mod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.