Skip to content

Commit 8f51a4c

Browse files
chore: remove excesss configuration
1 parent 7bd118e commit 8f51a4c

File tree

5 files changed

+91
-109
lines changed

5 files changed

+91
-109
lines changed

cli/server.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ import (
3131
"sync/atomic"
3232
"time"
3333

34-
"github.com/anthropics/anthropic-sdk-go"
35-
anthropicoption "github.com/anthropics/anthropic-sdk-go/option"
3634
"github.com/charmbracelet/lipgloss"
3735
"github.com/coreos/go-oidc/v3/oidc"
3836
"github.com/coreos/go-systemd/daemon"
@@ -631,13 +629,6 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
631629
vals.WorkspaceHostnameSuffix.String())
632630
}
633631

634-
var anthropicClient atomic.Pointer[anthropic.Client]
635-
if vals.AnthropicAPIKey.String() != "" {
636-
client := anthropic.NewClient(anthropicoption.WithAPIKey(vals.AnthropicAPIKey.String()))
637-
638-
anthropicClient.Store(&client)
639-
}
640-
641632
options := &coderd.Options{
642633
AccessURL: vals.AccessURL.Value(),
643634
AppHostname: appHostname,
@@ -675,7 +666,6 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
675666
AllowWorkspaceRenames: vals.AllowWorkspaceRenames.Value(),
676667
Entitlements: entitlements.New(),
677668
NotificationsEnqueuer: notifications.NewNoopEnqueuer(), // Changed further down if notifications enabled.
678-
AnthropicClient: &anthropicClient,
679669
}
680670
if httpServers.TLSConfig != nil {
681671
options.TLSCertificates = httpServers.TLSConfig.Certificates

coderd/aitasks.go

Lines changed: 4 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
package coderd
22

33
import (
4-
"context"
54
"database/sql"
65
"errors"
76
"fmt"
8-
"io"
97
"net/http"
108
"slices"
119
"strings"
1210

13-
"github.com/anthropics/anthropic-sdk-go"
11+
"cdr.dev/slog"
1412
"github.com/google/uuid"
15-
"golang.org/x/xerrors"
1613

17-
"github.com/coder/aisdk-go"
1814
"github.com/coder/coder/v2/coderd/audit"
1915
"github.com/coder/coder/v2/coderd/database"
2016
"github.com/coder/coder/v2/coderd/httpapi"
2117
"github.com/coder/coder/v2/coderd/httpmw"
2218
"github.com/coder/coder/v2/coderd/rbac"
19+
"github.com/coder/coder/v2/coderd/taskname"
2320
"github.com/coder/coder/v2/codersdk"
2421
)
2522

@@ -74,74 +71,6 @@ func (api *API) aiTasksPrompts(rw http.ResponseWriter, r *http.Request) {
7471
})
7572
}
7673

77-
func (api *API) generateTaskName(ctx context.Context, prompt, fallback string) (string, error) {
78-
var (
79-
stream aisdk.DataStream
80-
err error
81-
)
82-
83-
conversation := []aisdk.Message{
84-
{
85-
Role: "system",
86-
Parts: []aisdk.Part{{
87-
Type: aisdk.PartTypeText,
88-
Text: `You are a task summarizer.
89-
You summarize AI prompts into workspace names.
90-
You will only respond with a workspace name.
91-
The workspace name **MUST** follow this regex /^[a-z0-9]+(?:-[a-z0-9]+)*$/
92-
The workspace name **MUST** be 32 characters or **LESS**.
93-
The workspace name **MUST** be all lower case.
94-
The workspace name **MUST** end in a number between 0 and 100.
95-
The workspace name **MUST** be prefixed with "task".`,
96-
}},
97-
},
98-
{
99-
Role: "user",
100-
Parts: []aisdk.Part{{
101-
Type: aisdk.PartTypeText,
102-
Text: prompt,
103-
}},
104-
},
105-
}
106-
107-
anthropicClient := api.anthropicClient.Load()
108-
if anthropicClient == nil {
109-
return fallback, nil
110-
}
111-
112-
stream, err = anthropicDataStream(ctx, *anthropicClient, conversation)
113-
if err != nil {
114-
return "", xerrors.Errorf("create anthropic data stream: %w", err)
115-
}
116-
117-
var acc aisdk.DataStreamAccumulator
118-
stream = stream.WithAccumulator(&acc)
119-
120-
if err := stream.Pipe(io.Discard); err != nil {
121-
return "", err
122-
}
123-
124-
if len(acc.Messages()) == 0 {
125-
return fallback, nil
126-
}
127-
128-
return acc.Messages()[0].Content, nil
129-
}
130-
131-
func anthropicDataStream(ctx context.Context, client anthropic.Client, input []aisdk.Message) (aisdk.DataStream, error) {
132-
messages, system, err := aisdk.MessagesToAnthropic(input)
133-
if err != nil {
134-
return nil, xerrors.Errorf("convert messages to anthropic format: %w", err)
135-
}
136-
137-
return aisdk.AnthropicToDataStream(client.Messages.NewStreaming(ctx, anthropic.MessageNewParams{
138-
Model: anthropic.ModelClaude3_5HaikuLatest,
139-
MaxTokens: 24,
140-
System: system,
141-
Messages: messages,
142-
})), nil
143-
}
144-
14574
// This endpoint is experimental and not guaranteed to be stable, so we're not
14675
// generating public-facing documentation for it.
14776
func (api *API) tasksCreate(rw http.ResponseWriter, r *http.Request) {
@@ -177,13 +106,9 @@ func (api *API) tasksCreate(rw http.ResponseWriter, r *http.Request) {
177106
return
178107
}
179108

180-
taskName, err := api.generateTaskName(ctx, req.Prompt, req.Name)
109+
taskName, err := taskname.Generate(ctx, req.Prompt, req.Name)
181110
if err != nil {
182-
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
183-
Message: "Internal error generating name for task.",
184-
Detail: err.Error(),
185-
})
186-
return
111+
api.Logger.Error(ctx, "unable to generate task name", slog.Error(err))
187112
}
188113

189114
if taskName == "" {

coderd/coderd.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"sync/atomic"
2121
"time"
2222

23-
"github.com/anthropics/anthropic-sdk-go"
24-
2523
"github.com/coder/coder/v2/coderd/oauth2provider"
2624
"github.com/coder/coder/v2/coderd/pproflabel"
2725
"github.com/coder/coder/v2/coderd/prebuilds"
@@ -278,8 +276,6 @@ type Options struct {
278276

279277
// WebPushDispatcher is a way to send notifications over Web Push.
280278
WebPushDispatcher webpush.Dispatcher
281-
282-
AnthropicClient *atomic.Pointer[anthropic.Client]
283279
}
284280

285281
// @title Coder API
@@ -479,10 +475,6 @@ func New(options *Options) *API {
479475
options.NotificationsEnqueuer = notifications.NewNoopEnqueuer()
480476
}
481477

482-
if options.AnthropicClient == nil {
483-
options.AnthropicClient = &atomic.Pointer[anthropic.Client]{}
484-
}
485-
486478
r := chi.NewRouter()
487479
// We add this middleware early, to make sure that authorization checks made
488480
// by other middleware get recorded.
@@ -608,8 +600,7 @@ func New(options *Options) *API {
608600
options.Database,
609601
options.Pubsub,
610602
),
611-
dbRolluper: options.DatabaseRolluper,
612-
anthropicClient: options.AnthropicClient,
603+
dbRolluper: options.DatabaseRolluper,
613604
}
614605
api.WorkspaceAppsProvider = workspaceapps.NewDBTokenProvider(
615606
options.Logger.Named("workspaceapps"),
@@ -1732,8 +1723,6 @@ type API struct {
17321723
// dbRolluper rolls up template usage stats from raw agent and app
17331724
// stats. This is used to provide insights in the WebUI.
17341725
dbRolluper *dbrollup.Rolluper
1735-
1736-
anthropicClient *atomic.Pointer[anthropic.Client]
17371726
}
17381727

17391728
// Close waits for all WebSocket connections to drain before returning.

coderd/taskname/taskname.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package taskname
2+
3+
import (
4+
"context"
5+
"io"
6+
7+
"github.com/anthropics/anthropic-sdk-go"
8+
"github.com/coder/aisdk-go"
9+
"github.com/coder/coder/v2/codersdk"
10+
"golang.org/x/xerrors"
11+
)
12+
13+
const systemPrompt = `Generate a short workspace name from this AI task prompt.
14+
15+
Requirements:
16+
- Only lowercase letters, numbers, and hyphens
17+
- Start with "task-"
18+
- Maximum 32 characters total
19+
- Descriptive of the main task
20+
21+
Examples:
22+
- "Help me debug a Python script" → "task-python-debug"
23+
- "Create a React dashboard component" → "task-react-dashboard"
24+
- "Analyze sales data from Q3" → "task-analyze-q3-sales"
25+
- "Set up CI/CD pipeline" → "task-setup-cicd"
26+
27+
If you cannot create a suitable name, respond with just "task-workspace".`
28+
29+
func Generate(ctx context.Context, prompt, fallback string) (string, error) {
30+
conversation := []aisdk.Message{
31+
{
32+
Role: "system",
33+
Parts: []aisdk.Part{{
34+
Type: aisdk.PartTypeText,
35+
Text: systemPrompt,
36+
}},
37+
},
38+
{
39+
Role: "user",
40+
Parts: []aisdk.Part{{
41+
Type: aisdk.PartTypeText,
42+
Text: prompt,
43+
}},
44+
},
45+
}
46+
47+
anthropicClient := anthropic.NewClient(anthropic.DefaultClientOptions()...)
48+
49+
stream, err := anthropicDataStream(ctx, anthropicClient, conversation)
50+
if err != nil {
51+
return fallback, xerrors.Errorf("create anthropic data stream: %w", err)
52+
}
53+
54+
var acc aisdk.DataStreamAccumulator
55+
stream = stream.WithAccumulator(&acc)
56+
57+
if err := stream.Pipe(io.Discard); err != nil {
58+
return fallback, xerrors.Errorf("pipe data stream")
59+
}
60+
61+
if len(acc.Messages()) == 0 {
62+
return fallback, nil
63+
}
64+
65+
generatedName := acc.Messages()[0].Content
66+
67+
if err := codersdk.NameValid(generatedName); err != nil {
68+
return fallback, xerrors.Errorf("generated name %p not valid: %w", generatedName, err)
69+
}
70+
71+
return generatedName, nil
72+
}
73+
74+
func anthropicDataStream(ctx context.Context, client anthropic.Client, input []aisdk.Message) (aisdk.DataStream, error) {
75+
messages, system, err := aisdk.MessagesToAnthropic(input)
76+
if err != nil {
77+
return nil, xerrors.Errorf("convert messages to anthropic format: %w", err)
78+
}
79+
80+
return aisdk.AnthropicToDataStream(client.Messages.NewStreaming(ctx, anthropic.MessageNewParams{
81+
Model: anthropic.ModelClaude3_5HaikuLatest,
82+
MaxTokens: 24,
83+
System: system,
84+
Messages: messages,
85+
})), nil
86+
}

codersdk/deployment.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,6 @@ type DeploymentValues struct {
497497
WorkspaceHostnameSuffix serpent.String `json:"workspace_hostname_suffix,omitempty" typescript:",notnull"`
498498
Prebuilds PrebuildsConfig `json:"workspace_prebuilds,omitempty" typescript:",notnull"`
499499
HideAITasks serpent.Bool `json:"hide_ai_tasks,omitempty" typescript:",notnull"`
500-
AnthropicAPIKey serpent.String `json:"anthropic_api_key,omitempty" typescript:",notnull"`
501500

502501
Config serpent.YAMLConfigPath `json:"config,omitempty" typescript:",notnull"`
503502
WriteConfig serpent.Bool `json:"write_config,omitempty" typescript:",notnull"`
@@ -3206,13 +3205,6 @@ Write out the current server config as YAML to stdout.`,
32063205
Group: &deploymentGroupClient,
32073206
YAML: "hideAITasks",
32083207
},
3209-
{
3210-
Name: "Anthropic API Key",
3211-
Description: "API Key for accessing Anthropic's API platform.",
3212-
Env: "ANTHROPIC_API_KEY",
3213-
Value: &c.AnthropicAPIKey,
3214-
Group: &deploymentGroupClient,
3215-
},
32163208
}
32173209

32183210
return opts

0 commit comments

Comments
 (0)