Skip to content

Commit 80a172f

Browse files
authored
chore: move chatd and related packages to /x/ subpackage (#23445)
- Moves `coderd/chatd/`, `coderd/gitsync/`, `enterprise/coderd/chatd/` under `x/` parent directories to signal instability - Adds `Experimental:` glue code comments in `coderd/coderd.go` > 🤖 This PR was created with the help of Coder Agents, and was reviewed by my human. 🧑‍💻
1 parent 86d8b6d commit 80a172f

64 files changed

Lines changed: 93 additions & 91 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

coderd/coderd.go

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import (
5151
"github.com/coder/coder/v2/coderd/audit"
5252
"github.com/coder/coder/v2/coderd/awsidentity"
5353
"github.com/coder/coder/v2/coderd/boundaryusage"
54-
"github.com/coder/coder/v2/coderd/chatd"
5554
"github.com/coder/coder/v2/coderd/connectionlog"
5655
"github.com/coder/coder/v2/coderd/cryptokeys"
5756
"github.com/coder/coder/v2/coderd/database"
@@ -63,7 +62,6 @@ import (
6362
"github.com/coder/coder/v2/coderd/externalauth"
6463
"github.com/coder/coder/v2/coderd/files"
6564
"github.com/coder/coder/v2/coderd/gitsshkey"
66-
"github.com/coder/coder/v2/coderd/gitsync"
6765
"github.com/coder/coder/v2/coderd/healthcheck"
6866
"github.com/coder/coder/v2/coderd/healthcheck/derphealth"
6967
"github.com/coder/coder/v2/coderd/httpapi"
@@ -94,6 +92,8 @@ import (
9492
"github.com/coder/coder/v2/coderd/workspaceapps/appurl"
9593
"github.com/coder/coder/v2/coderd/workspacestats"
9694
"github.com/coder/coder/v2/coderd/wsbuilder"
95+
"github.com/coder/coder/v2/coderd/x/chatd"
96+
"github.com/coder/coder/v2/coderd/x/gitsync"
9797
"github.com/coder/coder/v2/codersdk"
9898
"github.com/coder/coder/v2/codersdk/drpcsdk"
9999
"github.com/coder/coder/v2/codersdk/healthsdk"
@@ -767,43 +767,45 @@ func New(options *Options) *API {
767767
}
768768
api.agentProvider = stn
769769

770-
maxChatsPerAcquire := options.DeploymentValues.AI.Chat.AcquireBatchSize.Value()
771-
if maxChatsPerAcquire > math.MaxInt32 {
772-
maxChatsPerAcquire = math.MaxInt32
773-
}
774-
if maxChatsPerAcquire < math.MinInt32 {
775-
maxChatsPerAcquire = math.MinInt32
776-
}
777-
778-
api.chatDaemon = chatd.New(chatd.Config{
779-
Logger: options.Logger.Named("chatd"),
780-
Database: options.Database,
781-
ReplicaID: api.ID,
782-
SubscribeFn: options.ChatSubscribeFn,
783-
MaxChatsPerAcquire: int32(maxChatsPerAcquire), //nolint:gosec // maxChatsPerAcquire is clamped to int32 range above.
784-
ProviderAPIKeys: chatProviderAPIKeysFromDeploymentValues(options.DeploymentValues),
785-
AgentConn: api.agentProvider.AgentConn,
786-
CreateWorkspace: api.chatCreateWorkspace,
787-
StartWorkspace: api.chatStartWorkspace,
788-
Pubsub: options.Pubsub,
789-
WebpushDispatcher: options.WebPushDispatcher,
790-
UsageTracker: options.WorkspaceUsageTracker,
791-
})
792-
gitSyncLogger := options.Logger.Named("gitsync")
793-
refresher := gitsync.NewRefresher(
794-
api.resolveGitProvider,
795-
api.resolveChatGitAccessToken,
796-
gitSyncLogger.Named("refresher"),
797-
quartz.NewReal(),
798-
)
799-
api.gitSyncWorker = gitsync.NewWorker(options.Database,
800-
refresher,
801-
api.chatDaemon.PublishDiffStatusChange,
802-
quartz.NewReal(),
803-
gitSyncLogger,
804-
)
805-
// nolint:gocritic // chat diff worker needs to be able to CRUD chats.
806-
go api.gitSyncWorker.Start(dbauthz.AsChatd(api.ctx))
770+
{ // Experimental: agents — chat daemon and git sync worker initialization.
771+
maxChatsPerAcquire := options.DeploymentValues.AI.Chat.AcquireBatchSize.Value()
772+
if maxChatsPerAcquire > math.MaxInt32 {
773+
maxChatsPerAcquire = math.MaxInt32
774+
}
775+
if maxChatsPerAcquire < math.MinInt32 {
776+
maxChatsPerAcquire = math.MinInt32
777+
}
778+
779+
api.chatDaemon = chatd.New(chatd.Config{
780+
Logger: options.Logger.Named("chatd"),
781+
Database: options.Database,
782+
ReplicaID: api.ID,
783+
SubscribeFn: options.ChatSubscribeFn,
784+
MaxChatsPerAcquire: int32(maxChatsPerAcquire), //nolint:gosec // maxChatsPerAcquire is clamped to int32 range above.
785+
ProviderAPIKeys: chatProviderAPIKeysFromDeploymentValues(options.DeploymentValues),
786+
AgentConn: api.agentProvider.AgentConn,
787+
CreateWorkspace: api.chatCreateWorkspace,
788+
StartWorkspace: api.chatStartWorkspace,
789+
Pubsub: options.Pubsub,
790+
WebpushDispatcher: options.WebPushDispatcher,
791+
UsageTracker: options.WorkspaceUsageTracker,
792+
})
793+
gitSyncLogger := options.Logger.Named("gitsync")
794+
refresher := gitsync.NewRefresher(
795+
api.resolveGitProvider,
796+
api.resolveChatGitAccessToken,
797+
gitSyncLogger.Named("refresher"),
798+
quartz.NewReal(),
799+
)
800+
api.gitSyncWorker = gitsync.NewWorker(options.Database,
801+
refresher,
802+
api.chatDaemon.PublishDiffStatusChange,
803+
quartz.NewReal(),
804+
gitSyncLogger,
805+
)
806+
// nolint:gocritic // chat diff worker needs to be able to CRUD chats.
807+
go api.gitSyncWorker.Start(dbauthz.AsChatd(api.ctx))
808+
}
807809
if options.DeploymentValues.Prometheus.Enable {
808810
options.PrometheusRegistry.MustRegister(stn)
809811
api.lifecycleMetrics = agentapi.NewLifecycleMetrics(options.PrometheusRegistry)
@@ -1146,6 +1148,7 @@ func New(options *Options) *API {
11461148
})
11471149
})
11481150
})
1151+
// Experimental(agents): chat API routes gated by ExperimentAgents.
11491152
r.Route("/chats", func(r chi.Router) {
11501153
r.Use(
11511154
apiKeyMiddleware,
@@ -2086,13 +2089,12 @@ type API struct {
20862089
// dbRolluper rolls up template usage stats from raw agent and app
20872090
// stats. This is used to provide insights in the WebUI.
20882091
dbRolluper *dbrollup.Rolluper
2089-
// chatDaemon handles background processing of pending chats.
2092+
// Experimental(agents): chatDaemon handles background processing of pending chats.
20902093
chatDaemon *chatd.Server
2094+
// Experimental(agents): gitSyncWorker refreshes stale chat diff statuses in the background.
2095+
gitSyncWorker *gitsync.Worker
20912096
// AISeatTracker records AI seat usage.
20922097
AISeatTracker aiseats.SeatTracker
2093-
// gitSyncWorker refreshes stale chat diff statuses in the
2094-
// background.
2095-
gitSyncWorker *gitsync.Worker
20962098

20972099
// ProfileCollector abstracts the runtime/pprof and runtime/trace
20982100
// calls used by the /debug/profile endpoint. Tests override this

coderd/database/db2sdk/db2sdk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"tailscale.com/tailcfg"
2020

2121
agentproto "github.com/coder/coder/v2/agent/proto"
22-
"github.com/coder/coder/v2/coderd/chatd/chatprompt"
2322
"github.com/coder/coder/v2/coderd/database"
2423
"github.com/coder/coder/v2/coderd/externalauth/gitprovider"
2524
"github.com/coder/coder/v2/coderd/rbac"
@@ -28,6 +27,7 @@ import (
2827
"github.com/coder/coder/v2/coderd/util/ptr"
2928
"github.com/coder/coder/v2/coderd/util/slice"
3029
"github.com/coder/coder/v2/coderd/workspaceapps/appurl"
30+
"github.com/coder/coder/v2/coderd/x/chatd/chatprompt"
3131
"github.com/coder/coder/v2/codersdk"
3232
"github.com/coder/coder/v2/provisionersdk/proto"
3333
"github.com/coder/coder/v2/tailnet"

coderd/database/querier_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/stretchr/testify/require"
2222

2323
"cdr.dev/slog/v3/sloggers/slogtest"
24-
"github.com/coder/coder/v2/coderd/chatd/chatprompt"
2524
"github.com/coder/coder/v2/coderd/coderdtest"
2625
"github.com/coder/coder/v2/coderd/database"
2726
"github.com/coder/coder/v2/coderd/database/dbauthz"
@@ -35,6 +34,7 @@ import (
3534
"github.com/coder/coder/v2/coderd/rbac"
3635
"github.com/coder/coder/v2/coderd/rbac/policy"
3736
"github.com/coder/coder/v2/coderd/util/slice"
37+
"github.com/coder/coder/v2/coderd/x/chatd/chatprompt"
3838
"github.com/coder/coder/v2/codersdk"
3939
"github.com/coder/coder/v2/provisionersdk"
4040
"github.com/coder/coder/v2/testutil"

coderd/chats.go renamed to coderd/exp_chats.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,11 @@ import (
2828
"cdr.dev/slog/v3"
2929
"github.com/coder/coder/v2/agent/agentssh"
3030
"github.com/coder/coder/v2/coderd/audit"
31-
"github.com/coder/coder/v2/coderd/chatd"
32-
"github.com/coder/coder/v2/coderd/chatd/chatprovider"
3331
"github.com/coder/coder/v2/coderd/database"
3432
"github.com/coder/coder/v2/coderd/database/db2sdk"
3533
"github.com/coder/coder/v2/coderd/database/dbauthz"
3634
"github.com/coder/coder/v2/coderd/externalauth"
3735
"github.com/coder/coder/v2/coderd/externalauth/gitprovider"
38-
"github.com/coder/coder/v2/coderd/gitsync"
3936
"github.com/coder/coder/v2/coderd/httpapi"
4037
"github.com/coder/coder/v2/coderd/httpapi/httperror"
4138
"github.com/coder/coder/v2/coderd/httpmw"
@@ -46,6 +43,9 @@ import (
4643
"github.com/coder/coder/v2/coderd/tracing"
4744
"github.com/coder/coder/v2/coderd/util/ptr"
4845
"github.com/coder/coder/v2/coderd/workspaceapps"
46+
"github.com/coder/coder/v2/coderd/x/chatd"
47+
"github.com/coder/coder/v2/coderd/x/chatd/chatprovider"
48+
"github.com/coder/coder/v2/coderd/x/gitsync"
4949
"github.com/coder/coder/v2/codersdk"
5050
"github.com/coder/coder/v2/codersdk/wsjson"
5151
"github.com/coder/websocket"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import (
1818
"github.com/shopspring/decimal"
1919
"github.com/stretchr/testify/require"
2020

21-
"github.com/coder/coder/v2/coderd/chatd"
22-
"github.com/coder/coder/v2/coderd/chatd/chatprompt"
2321
"github.com/coder/coder/v2/coderd/coderdtest"
2422
"github.com/coder/coder/v2/coderd/coderdtest/oidctest"
2523
"github.com/coder/coder/v2/coderd/database"
@@ -31,6 +29,8 @@ import (
3129
coderdpubsub "github.com/coder/coder/v2/coderd/pubsub"
3230
"github.com/coder/coder/v2/coderd/rbac"
3331
"github.com/coder/coder/v2/coderd/util/ptr"
32+
"github.com/coder/coder/v2/coderd/x/chatd"
33+
"github.com/coder/coder/v2/coderd/x/chatd/chatprompt"
3434
"github.com/coder/coder/v2/codersdk"
3535
"github.com/coder/coder/v2/testutil"
3636
"github.com/coder/websocket"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"github.com/shopspring/decimal"
77
"github.com/stretchr/testify/require"
88

9-
"github.com/coder/coder/v2/coderd/chatd/chatcost"
109
"github.com/coder/coder/v2/coderd/util/ptr"
10+
"github.com/coder/coder/v2/coderd/x/chatd/chatcost"
1111
"github.com/coder/coder/v2/codersdk"
1212
)
1313

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ import (
2020
"golang.org/x/xerrors"
2121

2222
"cdr.dev/slog/v3"
23-
"github.com/coder/coder/v2/coderd/chatd/chatcost"
24-
"github.com/coder/coder/v2/coderd/chatd/chatloop"
25-
"github.com/coder/coder/v2/coderd/chatd/chatprompt"
26-
"github.com/coder/coder/v2/coderd/chatd/chatprovider"
27-
"github.com/coder/coder/v2/coderd/chatd/chattool"
28-
"github.com/coder/coder/v2/coderd/chatd/mcpclient"
2923
"github.com/coder/coder/v2/coderd/database"
3024
"github.com/coder/coder/v2/coderd/database/db2sdk"
3125
"github.com/coder/coder/v2/coderd/database/dbauthz"
@@ -34,6 +28,12 @@ import (
3428
"github.com/coder/coder/v2/coderd/util/ptr"
3529
"github.com/coder/coder/v2/coderd/webpush"
3630
"github.com/coder/coder/v2/coderd/workspacestats"
31+
"github.com/coder/coder/v2/coderd/x/chatd/chatcost"
32+
"github.com/coder/coder/v2/coderd/x/chatd/chatloop"
33+
"github.com/coder/coder/v2/coderd/x/chatd/chatprompt"
34+
"github.com/coder/coder/v2/coderd/x/chatd/chatprovider"
35+
"github.com/coder/coder/v2/coderd/x/chatd/chattool"
36+
"github.com/coder/coder/v2/coderd/x/chatd/mcpclient"
3737
"github.com/coder/coder/v2/codersdk"
3838
"github.com/coder/coder/v2/codersdk/workspacesdk"
3939
"github.com/coder/quartz"
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ import (
2626

2727
"cdr.dev/slog/v3/sloggers/slogtest"
2828
"github.com/coder/coder/v2/agent/agenttest"
29-
"github.com/coder/coder/v2/coderd/chatd"
30-
"github.com/coder/coder/v2/coderd/chatd/chatprompt"
31-
"github.com/coder/coder/v2/coderd/chatd/chattest"
32-
"github.com/coder/coder/v2/coderd/chatd/chattool"
3329
"github.com/coder/coder/v2/coderd/coderdtest"
3430
"github.com/coder/coder/v2/coderd/database"
3531
"github.com/coder/coder/v2/coderd/database/db2sdk"
@@ -41,6 +37,10 @@ import (
4137
"github.com/coder/coder/v2/coderd/rbac"
4238
"github.com/coder/coder/v2/coderd/util/slice"
4339
"github.com/coder/coder/v2/coderd/workspacestats"
40+
"github.com/coder/coder/v2/coderd/x/chatd"
41+
"github.com/coder/coder/v2/coderd/x/chatd/chatprompt"
42+
"github.com/coder/coder/v2/coderd/x/chatd/chattest"
43+
"github.com/coder/coder/v2/coderd/x/chatd/chattool"
4444
"github.com/coder/coder/v2/codersdk"
4545
"github.com/coder/coder/v2/codersdk/workspacesdk"
4646
"github.com/coder/coder/v2/codersdk/workspacesdk/agentconnmock"

0 commit comments

Comments
 (0)