Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: slightly again
  • Loading branch information
DanielleMaywood committed Aug 13, 2025
commit 706c78915e4da9f52a9e54f8bd8229cf9db07b8f
10 changes: 10 additions & 0 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"sync/atomic"
"time"

"github.com/anthropics/anthropic-sdk-go"
anthropicoption "github.com/anthropics/anthropic-sdk-go/option"
"github.com/charmbracelet/lipgloss"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/coreos/go-systemd/daemon"
Expand Down Expand Up @@ -629,6 +631,13 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
vals.WorkspaceHostnameSuffix.String())
}

var anthropicClient atomic.Pointer[anthropic.Client]
if vals.AnthropicAPIKey.String() != "" {
client := anthropic.NewClient(anthropicoption.WithAPIKey(vals.AnthropicAPIKey.String()))

anthropicClient.Store(&client)
}

options := &coderd.Options{
AccessURL: vals.AccessURL.Value(),
AppHostname: appHostname,
Expand Down Expand Up @@ -666,6 +675,7 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
AllowWorkspaceRenames: vals.AllowWorkspaceRenames.Value(),
Entitlements: entitlements.New(),
NotificationsEnqueuer: notifications.NewNoopEnqueuer(), // Changed further down if notifications enabled.
AnthropicClient: &anthropicClient,
}
if httpServers.TLSConfig != nil {
options.TLSCertificates = httpServers.TLSConfig.Certificates
Expand Down
27 changes: 11 additions & 16 deletions coderd/aitasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import (
"fmt"
"io"
"net/http"
"os"
"slices"
"strings"

"github.com/anthropics/anthropic-sdk-go"
anthropicoption "github.com/anthropics/anthropic-sdk-go/option"
"github.com/google/uuid"
"golang.org/x/xerrors"

Expand Down Expand Up @@ -88,15 +86,15 @@ func (api *API) generateTaskName(ctx context.Context, prompt, fallback string) (
Parts: []aisdk.Part{{
Type: aisdk.PartTypeText,
Text: `
You are a task summarizer.
You summarize AI prompts into workspace names.
You will only respond with a workspace name.
The workspace name **MUST** follow this regex /^[a-z0-9]+(?:-[a-z0-9]+)*$/
The workspace name **MUST** be 32 characters or **LESS**.
The workspace name **MUST** be all lower case.
The workspace name **MUST** end in a number between 0 and 100.
The workspace name **MUST** be prefixed with "task".
`,
You are a task summarizer.
You summarize AI prompts into workspace names.
You will only respond with a workspace name.
The workspace name **MUST** follow this regex /^[a-z0-9]+(?:-[a-z0-9]+)*$/
The workspace name **MUST** be 32 characters or **LESS**.
The workspace name **MUST** be all lower case.
The workspace name **MUST** end in a number between 0 and 100.
The workspace name **MUST** be prefixed with "task".
`,
}},
},
{
Expand All @@ -108,11 +106,8 @@ func (api *API) generateTaskName(ctx context.Context, prompt, fallback string) (
},
}

if anthropicAPIKey := os.Getenv("ANTHROPIC_API_KEY"); anthropicAPIKey != "" {
anthropicAPIKey := os.Getenv("ANTHROPIC_API_KEY")
anthropicClient := anthropic.NewClient(anthropicoption.WithAPIKey(anthropicAPIKey))

stream, err = anthropicDataStream(ctx, anthropicClient, conversation)
if anthropicClient := api.anthropicClient.Load(); anthropicClient != nil {
stream, err = anthropicDataStream(ctx, *anthropicClient, conversation)
if err != nil {
return "", xerrors.Errorf("create anthropic data stream: %w", err)
}
Expand Down
12 changes: 11 additions & 1 deletion coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"sync/atomic"
"time"

"github.com/anthropics/anthropic-sdk-go"
"github.com/coder/coder/v2/coderd/oauth2provider"
"github.com/coder/coder/v2/coderd/pproflabel"
"github.com/coder/coder/v2/coderd/prebuilds"
Expand Down Expand Up @@ -276,6 +277,8 @@ type Options struct {

// WebPushDispatcher is a way to send notifications over Web Push.
WebPushDispatcher webpush.Dispatcher

AnthropicClient *atomic.Pointer[anthropic.Client]
}

// @title Coder API
Expand Down Expand Up @@ -475,6 +478,10 @@ func New(options *Options) *API {
options.NotificationsEnqueuer = notifications.NewNoopEnqueuer()
}

if options.AnthropicClient == nil {
options.AnthropicClient = &atomic.Pointer[anthropic.Client]{}
}

r := chi.NewRouter()
// We add this middleware early, to make sure that authorization checks made
// by other middleware get recorded.
Expand Down Expand Up @@ -600,7 +607,8 @@ func New(options *Options) *API {
options.Database,
options.Pubsub,
),
dbRolluper: options.DatabaseRolluper,
dbRolluper: options.DatabaseRolluper,
anthropicClient: options.AnthropicClient,
}
api.WorkspaceAppsProvider = workspaceapps.NewDBTokenProvider(
options.Logger.Named("workspaceapps"),
Expand Down Expand Up @@ -1723,6 +1731,8 @@ type API struct {
// dbRolluper rolls up template usage stats from raw agent and app
// stats. This is used to provide insights in the WebUI.
dbRolluper *dbrollup.Rolluper

anthropicClient *atomic.Pointer[anthropic.Client]
}

// Close waits for all WebSocket connections to drain before returning.
Expand Down
8 changes: 8 additions & 0 deletions codersdk/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ type DeploymentValues struct {
WorkspaceHostnameSuffix serpent.String `json:"workspace_hostname_suffix,omitempty" typescript:",notnull"`
Prebuilds PrebuildsConfig `json:"workspace_prebuilds,omitempty" typescript:",notnull"`
HideAITasks serpent.Bool `json:"hide_ai_tasks,omitempty" typescript:",notnull"`
AnthropicAPIKey serpent.String `json:"anthropic_api_key,omitempty" typescript:",notnull"`

Config serpent.YAMLConfigPath `json:"config,omitempty" typescript:",notnull"`
WriteConfig serpent.Bool `json:"write_config,omitempty" typescript:",notnull"`
Expand Down Expand Up @@ -3205,6 +3206,13 @@ Write out the current server config as YAML to stdout.`,
Group: &deploymentGroupClient,
YAML: "hideAITasks",
},
{
Name: "Anthropic API Key",
Description: "API Key for accessing Anthropic's API platform.",
Env: "ANTHROPIC_API_KEY",
Value: &c.AnthropicAPIKey,
Group: &deploymentGroupClient,
},
}

return opts
Expand Down