- Notifications
You must be signed in to change notification settings - Fork1k
refactor: generate task name fallback on coderd#19447
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
7cf1815
620a01b
a9ccd72
fb2b8f2
74bfabc
a0e41c1
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -2,11 +2,15 @@ package taskname | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"math/rand/v2" | ||
"os" | ||
"strings" | ||
"github.com/anthropics/anthropic-sdk-go" | ||
anthropicoption "github.com/anthropics/anthropic-sdk-go/option" | ||
"github.com/moby/moby/pkg/namesgenerator" | ||
"golang.org/x/xerrors" | ||
"github.com/coder/aisdk-go" | ||
@@ -20,19 +24,17 @@ const ( | ||
Requirements: | ||
- Only lowercase letters, numbers, and hyphens | ||
- Start with "task-" | ||
- Maximum 28 characters total | ||
- Descriptive of the main task | ||
Examples: | ||
- "Help me debug a Python script" → "task-python-debug" | ||
- "Create a React dashboard component" → "task-react-dashboard" | ||
- "Analyze sales data from Q3" → "task-analyze-q3-sales" | ||
- "Set up CI/CD pipeline" → "task-setup-cicd" | ||
If you cannot create a suitable name: | ||
Comment on lines -23 to -35 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. We previously asked the LLM to generate a random number at the end to help reduce the odds of collision, but we'll move this to be under our control. | ||
- Respond with "task-unnamed"` | ||
) | ||
var ( | ||
@@ -67,6 +69,32 @@ func GetAnthropicModelFromEnv() anthropic.Model { | ||
return anthropic.Model(os.Getenv("ANTHROPIC_MODEL")) | ||
} | ||
// generateSuffix generates a random hex string between `0000` and `ffff`. | ||
func generateSuffix() string { | ||
numMin := 0x00000 | ||
numMax := 0x10000 | ||
//nolint:gosec // We don't need a cryptographically secure random number generator for generating a task name suffix. | ||
num := rand.IntN(numMax-numMin) + numMin | ||
return fmt.Sprintf("%04x", num) | ||
} | ||
func GenerateFallback() string { | ||
// We have a 32 character limit for the name. | ||
// We have a 5 character prefix `task-`. | ||
// We have a 5 character suffix `-ffff`. | ||
// This leaves us with 22 characters for the middle. | ||
// | ||
// Unfortunately, `namesgenerator.GetRandomName(0)` will | ||
// generate names that are longer than 22 characters, so | ||
// we just trim these down to length. | ||
name := strings.ReplaceAll(namesgenerator.GetRandomName(0), "_", "-") | ||
name = name[:min(len(name), 22)] | ||
name = strings.TrimSuffix(name, "-") | ||
return fmt.Sprintf("task-%s-%s", name, generateSuffix()) | ||
} | ||
func Generate(ctx context.Context, prompt string, opts ...Option) (string, error) { | ||
o := options{} | ||
for _, opt := range opts { | ||
@@ -127,7 +155,7 @@ func Generate(ctx context.Context, prompt string, opts ...Option) (string, error | ||
return "", ErrNoNameGenerated | ||
} | ||
returnfmt.Sprintf("%s-%s",generatedName, generateSuffix()), nil | ||
} | ||
func anthropicDataStream(ctx context.Context, client anthropic.Client, model anthropic.Model, input []aisdk.Message) (aisdk.DataStream, error) { | ||
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.