@@ -2,11 +2,15 @@ package taskname
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"io"
7
+ "math/rand/v2"
6
8
"os"
9
+ "strings"
7
10
8
11
"github.com/anthropics/anthropic-sdk-go"
9
12
anthropicoption "github.com/anthropics/anthropic-sdk-go/option"
13
+ "github.com/moby/moby/pkg/namesgenerator"
10
14
"golang.org/x/xerrors"
11
15
12
16
"github.com/coder/aisdk-go"
@@ -20,19 +24,17 @@ const (
20
24
Requirements:
21
25
- Only lowercase letters, numbers, and hyphens
22
26
- Start with "task-"
23
- - End with a random number between 0-99
24
- - Maximum 32 characters total
27
+ - Maximum 28 characters total
25
28
- Descriptive of the main task
26
29
27
30
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 "
31
+ - "Help me debug a Python script" → "task-python-debug"
32
+ - "Create a React dashboard component" → "task-react-dashboard"
33
+ - "Analyze sales data from Q3" → "task-analyze-q3-sales"
34
+ - "Set up CI/CD pipeline" → "task-setup-cicd"
32
35
33
36
If you cannot create a suitable name:
34
- - Respond with "task-unnamed"
35
- - Do not end with a random number`
37
+ - Respond with "task-unnamed"`
36
38
)
37
39
38
40
var (
@@ -67,6 +69,32 @@ func GetAnthropicModelFromEnv() anthropic.Model {
67
69
return anthropic .Model (os .Getenv ("ANTHROPIC_MODEL" ))
68
70
}
69
71
72
+ // generateSuffix generates a random hex string between `0000` and `ffff`.
73
+ func generateSuffix () string {
74
+ numMin := 0x00000
75
+ numMax := 0x10000
76
+ //nolint:gosec // We don't need a cryptographically secure random number generator for generating a task name suffix.
77
+ num := rand .IntN (numMax - numMin ) + numMin
78
+
79
+ return fmt .Sprintf ("%04x" , num )
80
+ }
81
+
82
+ func GenerateFallback () string {
83
+ // We have a 32 character limit for the name.
84
+ // We have a 5 character prefix `task-`.
85
+ // We have a 5 character suffix `-ffff`.
86
+ // This leaves us with 22 characters for the middle.
87
+ //
88
+ // Unfortunately, `namesgenerator.GetRandomName(0)` will
89
+ // generate names that are longer than 22 characters, so
90
+ // we just trim these down to length.
91
+ name := strings .ReplaceAll (namesgenerator .GetRandomName (0 ), "_" , "-" )
92
+ name = name [:min (len (name ), 22 )]
93
+ name = strings .TrimSuffix (name , "-" )
94
+
95
+ return fmt .Sprintf ("task-%s-%s" , name , generateSuffix ())
96
+ }
97
+
70
98
func Generate (ctx context.Context , prompt string , opts ... Option ) (string , error ) {
71
99
o := options {}
72
100
for _ , opt := range opts {
@@ -127,7 +155,7 @@ func Generate(ctx context.Context, prompt string, opts ...Option) (string, error
127
155
return "" , ErrNoNameGenerated
128
156
}
129
157
130
- return generatedName , nil
158
+ return fmt . Sprintf ( "%s-%s" , generatedName , generateSuffix ()) , nil
131
159
}
132
160
133
161
func anthropicDataStream (ctx context.Context , client anthropic.Client , model anthropic.Model , input []aisdk.Message ) (aisdk.DataStream , error ) {
0 commit comments