|
| 1 | +package taskname |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/anthropics/anthropic-sdk-go" |
| 9 | + anthropicoption "github.com/anthropics/anthropic-sdk-go/option" |
| 10 | + "golang.org/x/xerrors" |
| 11 | + |
| 12 | + "github.com/coder/aisdk-go" |
| 13 | + "github.com/coder/coder/v2/codersdk" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + defaultModel = anthropic.ModelClaude3_5HaikuLatest |
| 18 | + systemPrompt = `Generate a short workspace name from this AI task prompt. |
| 19 | +
|
| 20 | +Requirements: |
| 21 | +- Only lowercase letters, numbers, and hyphens |
| 22 | +- Start with "task-" |
| 23 | +- End with a random number between 0-99 |
| 24 | +- Maximum 32 characters total |
| 25 | +- Descriptive of the main task |
| 26 | +
|
| 27 | +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" |
| 32 | +
|
| 33 | +If you cannot create a suitable name: |
| 34 | +- Respond with "task-unnamed" |
| 35 | +- Do not end with a random number` |
| 36 | +) |
| 37 | + |
| 38 | +var ( |
| 39 | + ErrNoAPIKey = xerrors.New("no api key provided") |
| 40 | + ErrNoNameGenerated = xerrors.New("no task name generated") |
| 41 | +) |
| 42 | + |
| 43 | +type options struct { |
| 44 | + apiKey string |
| 45 | + model anthropic.Model |
| 46 | +} |
| 47 | + |
| 48 | +type Option func(o *options) |
| 49 | + |
| 50 | +func WithAPIKey(apiKey string) Option { |
| 51 | + return func(o *options) { |
| 52 | + o.apiKey = apiKey |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func WithModel(model anthropic.Model) Option { |
| 57 | + return func(o *options) { |
| 58 | + o.model = model |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func GetAnthropicAPIKeyFromEnv() string { |
| 63 | + return os.Getenv("ANTHROPIC_API_KEY") |
| 64 | +} |
| 65 | + |
| 66 | +func GetAnthropicModelFromEnv() anthropic.Model { |
| 67 | + return anthropic.Model(os.Getenv("ANTHROPIC_MODEL")) |
| 68 | +} |
| 69 | + |
| 70 | +func Generate(ctx context.Context, prompt string, opts ...Option) (string, error) { |
| 71 | + o := options{} |
| 72 | + for _, opt := range opts { |
| 73 | + opt(&o) |
| 74 | + } |
| 75 | + |
| 76 | + if o.model == "" { |
| 77 | + o.model = defaultModel |
| 78 | + } |
| 79 | + if o.apiKey == "" { |
| 80 | + return "", ErrNoAPIKey |
| 81 | + } |
| 82 | + |
| 83 | + conversation := []aisdk.Message{ |
| 84 | + { |
| 85 | + Role: "system", |
| 86 | + Parts: []aisdk.Part{{ |
| 87 | + Type: aisdk.PartTypeText, |
| 88 | + Text: systemPrompt, |
| 89 | + }}, |
| 90 | + }, |
| 91 | + { |
| 92 | + Role: "user", |
| 93 | + Parts: []aisdk.Part{{ |
| 94 | + Type: aisdk.PartTypeText, |
| 95 | + Text: prompt, |
| 96 | + }}, |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + anthropicOptions := anthropic.DefaultClientOptions() |
| 101 | + anthropicOptions = append(anthropicOptions, anthropicoption.WithAPIKey(o.apiKey)) |
| 102 | + anthropicClient := anthropic.NewClient(anthropicOptions...) |
| 103 | + |
| 104 | + stream, err := anthropicDataStream(ctx, anthropicClient, o.model, conversation) |
| 105 | + if err != nil { |
| 106 | + return "", xerrors.Errorf("create anthropic data stream: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + var acc aisdk.DataStreamAccumulator |
| 110 | + stream = stream.WithAccumulator(&acc) |
| 111 | + |
| 112 | + if err := stream.Pipe(io.Discard); err != nil { |
| 113 | + return "", xerrors.Errorf("pipe data stream") |
| 114 | + } |
| 115 | + |
| 116 | + if len(acc.Messages()) == 0 { |
| 117 | + return "", ErrNoNameGenerated |
| 118 | + } |
| 119 | + |
| 120 | + generatedName := acc.Messages()[0].Content |
| 121 | + |
| 122 | + if err := codersdk.NameValid(generatedName); err != nil { |
| 123 | + return "", xerrors.Errorf("generated name %v not valid: %w", generatedName, err) |
| 124 | + } |
| 125 | + |
| 126 | + if generatedName == "task-unnamed" { |
| 127 | + return "", ErrNoNameGenerated |
| 128 | + } |
| 129 | + |
| 130 | + return generatedName, nil |
| 131 | +} |
| 132 | + |
| 133 | +func anthropicDataStream(ctx context.Context, client anthropic.Client, model anthropic.Model, input []aisdk.Message) (aisdk.DataStream, error) { |
| 134 | + messages, system, err := aisdk.MessagesToAnthropic(input) |
| 135 | + if err != nil { |
| 136 | + return nil, xerrors.Errorf("convert messages to anthropic format: %w", err) |
| 137 | + } |
| 138 | + |
| 139 | + return aisdk.AnthropicToDataStream(client.Messages.NewStreaming(ctx, anthropic.MessageNewParams{ |
| 140 | + Model: model, |
| 141 | + MaxTokens: 24, |
| 142 | + System: system, |
| 143 | + Messages: messages, |
| 144 | + })), nil |
| 145 | +} |
0 commit comments