Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
4e1543c
feat(coderd): add the manage_automations tool and the chat switch
ThomasK33 Nov 12, 2026
7e113b2
test(coderd): cover the manage_automations tool, its availability and…
ThomasK33 Nov 12, 2026
b74a015
docs(coderd/x/chatd): describe the manage_automations tool and its sw…
ThomasK33 Nov 12, 2026
ebb8756
test(coderd/x/chatd): export the availability check without a confusi…
ThomasK33 Nov 12, 2026
4388d3e
fix(coderd/x/chatd/chatautomation): keep get in the chat's organizati…
ThomasK33 Nov 12, 2026
b56b0f4
docs(coderd/x/chatd): state the organization and byte bounds of the tool
ThomasK33 Nov 12, 2026
68fc44d
refactor(coderd/x/chatd/chatautomation): drop a helper that the tool …
ThomasK33 Nov 12, 2026
bf79a9d
test(coderd/x/chatd/chatautomation): cover the owner's roles per call…
ThomasK33 Nov 12, 2026
64ce454
test(coderd/x/chatd): read the experiment again for each step of a turn
ThomasK33 Nov 12, 2026
12e2df1
test(coderd): cover a partial switch request and the omitted field of…
ThomasK33 Nov 12, 2026
f339d30
refactor(coderd/database): set the chat automations switch without re…
ThomasK33 Nov 12, 2026
81d04f8
test(coderd/x/chatd): cover the switch of a sub-agent child chat
ThomasK33 Nov 12, 2026
607d66e
test(coderd/x/chatd): name the sub-agent switch test with the other m…
ThomasK33 Nov 12, 2026
4038ba7
fix(coderd): answer 404 when the switch write finds no chat and cover…
ThomasK33 Nov 13, 2026
496388c
Merge remote-tracking branch 'origin/tk/automations-api-create' into …
ThomasK33 Nov 13, 2026
719eef1
test: cut tests that repeat or cannot fail
ThomasK33 Nov 13, 2026
eeab8fc
test: keep the guards a verifier named
ThomasK33 Nov 13, 2026
dfa79bf
Merge remote-tracking branch 'origin/tk/automations-api-create' into …
ThomasK33 Nov 13, 2026
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
8 changes: 8 additions & 0 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 3 additions & 66 deletions coderd/chatautomations.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/coder/coder/v2/coderd/audit"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/db2sdk"
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/database/dbtime"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/httpmw"
Expand Down Expand Up @@ -116,7 +115,7 @@ func (api *API) postChatAutomation(rw http.ResponseWriter, r *http.Request) {
// The row is committed and the generated secret exists only in this
// response, so a failed read of the owner and chats must not fail it.
automations := []database.ChatAutomation{result.Automation}
refs, err := api.loadChatAutomationRefs(ctx, automations)
refs, err := chatautomation.LoadRefs(ctx, api.Database, automations)
if err != nil {
api.Logger.Warn(ctx, "load chat automation references for the create response", slog.Error(err))
refs = db2sdk.ChatAutomationRefs{}
Expand Down Expand Up @@ -161,7 +160,7 @@ func (api *API) listChatAutomations(rw http.ResponseWriter, r *http.Request) {
return
}
automations := slice.List(rows, db2sdk.ChatAutomationFromListRow)
refs, err := api.loadChatAutomationRefs(ctx, automations)
refs, err := chatautomation.LoadRefs(ctx, api.Database, automations)
if err != nil {
api.writeChatAutomationError(ctx, rw, "Failed to list chat automations.", err)
return
Expand Down Expand Up @@ -231,7 +230,7 @@ func (api *API) getChatAutomation(rw http.ResponseWriter, r *http.Request) {
api.writeChatAutomationError(ctx, rw, "Failed to get chat automation.", err)
return
}
refs, err := api.loadChatAutomationRefs(ctx, []database.ChatAutomation{automation})
refs, err := chatautomation.LoadRefs(ctx, api.Database, []database.ChatAutomation{automation})
if err != nil {
api.writeChatAutomationError(ctx, rw, "Failed to get chat automation.", err)
return
Expand Down Expand Up @@ -292,65 +291,3 @@ func parseChatAutomationID(rw http.ResponseWriter, r *http.Request) (uuid.UUID,
}
return id, true
}

// loadChatAutomationRefs reads the owners and chats of the rows. It reads the
// chats in one query. It reads the owners in one query too, and one by one when
// the caller cannot read every owner at once.
func (api *API) loadChatAutomationRefs(ctx context.Context, automations []database.ChatAutomation) (db2sdk.ChatAutomationRefs, error) {
refs := db2sdk.ChatAutomationRefs{
Owners: make(map[uuid.UUID]database.User),
Chats: make(map[uuid.UUID]database.Chat),
}
ownerIDs := make(map[uuid.UUID]struct{})
chatIDs := make(map[uuid.UUID]struct{})
for _, a := range automations {
ownerIDs[a.OwnerID] = struct{}{}
for _, id := range []uuid.NullUUID{a.TargetChatID, a.CreatedByChatID} {
if id.Valid {
chatIDs[id.UUID] = struct{}{}
}
}
}
if len(ownerIDs) > 0 {
users, err := api.Database.GetUsersByIDs(ctx, mapKeys(ownerIDs))
switch {
case dbauthz.IsNotAuthorizedError(err):
// The batch read is all or nothing. Read the owners one by one and
// leave out each one that the caller cannot read.
for id := range ownerIDs {
user, err := api.Database.GetUserByID(ctx, id)
if httpapi.Is404Error(err) {
continue
}
if err != nil {
return refs, xerrors.Errorf("get owner: %w", err)
}
refs.Owners[id] = user
}
case err != nil:
return refs, xerrors.Errorf("get owners: %w", err)
default:
for _, u := range users {
refs.Owners[u.ID] = u
}
}
}
if len(chatIDs) > 0 {
chats, err := api.Database.GetReadableChatsByIDs(ctx, mapKeys(chatIDs))
if err != nil {
return refs, xerrors.Errorf("get chats: %w", err)
}
for _, c := range chats {
refs.Chats[c.ID] = c
}
}
return refs, nil
}

func mapKeys(set map[uuid.UUID]struct{}) []uuid.UUID {
keys := make([]uuid.UUID, 0, len(set))
for k := range set {
keys = append(keys, k)
}
return keys
}
1 change: 1 addition & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,7 @@ func New(options *Options) *API {
StreamSilenceTimeout: streamSilenceTimeout,
DisableCallerSuppliedTools: options.DeploymentValues.DisableChatCallerSuppliedTools.Value(),
Experiments: experiments,
ExperimentEvaluator: api.ExperimentEvaluator,
AgentConn: api.agentProvider.AgentConn,
AgentInactiveDisconnectTimeout: api.AgentInactiveDisconnectTimeout,
CreateWorkspace: api.chatCreateWorkspace,
Expand Down
2 changes: 2 additions & 0 deletions coderd/database/db2sdk/db2sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2059,6 +2059,8 @@ func ChatWithDiffStatuses(
Labels: labels,
ClientType: codersdk.ChatClientType(c.ClientType),
LastError: lastError,

ManageAutomationsEnabled: c.ManageAutomationsEnabled,
}
if c.LastTurnSummary.Valid {
chat.LastTurnSummary = &c.LastTurnSummary.String
Expand Down
2 changes: 2 additions & 0 deletions coderd/database/db2sdk/db2sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,8 @@ func TestChat_AllFieldsPopulated(t *testing.T) {
ContextAggregateHash: []byte{0x01, 0x02, 0x03},
ContextDirtySince: sql.NullTime{Time: now, Valid: true},
ContextError: "context boom",

ManageAutomationsEnabled: true,
}
// Only ChatID is needed here. This test checks that
// Chat.DiffStatus is non-nil, not that every DiffStatus
Expand Down
11 changes: 11 additions & 0 deletions coderd/database/dbauthz/dbauthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -8161,6 +8161,17 @@ func (q *querier) UpdateChatMCPServerIDs(ctx context.Context, arg database.Updat
return q.db.UpdateChatMCPServerIDs(ctx, arg)
}

func (q *querier) UpdateChatManageAutomationsEnabledByID(ctx context.Context, arg database.UpdateChatManageAutomationsEnabledByIDParams) (int64, error) {
chat, err := q.db.GetChatByID(ctx, arg.ID)
if err != nil {
return 0, err
}
if err := q.authorizeContext(ctx, policy.ActionUpdate, chat); err != nil {
return 0, err
}
return q.db.UpdateChatManageAutomationsEnabledByID(ctx, arg)
}

func (q *querier) UpdateChatModelConfig(ctx context.Context, arg database.UpdateChatModelConfigParams) (database.ChatModelConfig, error) {
existing, err := q.db.GetChatModelConfigByID(ctx, arg.ID)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions coderd/database/dbauthz/dbauthz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2027,6 +2027,13 @@ func (s *MethodTestSuite) TestChats() {
dbm.EXPECT().UpdateChatPlanModeByID(gomock.Any(), arg).Return(chat, nil).AnyTimes()
check.Args(arg).Asserts(chat, policy.ActionUpdate).Returns(chat)
}))
s.Run("UpdateChatManageAutomationsEnabledByID", s.Mocked(func(dbm *dbmock.MockStore, faker *gofakeit.Faker, check *expects) {
chat := testutil.Fake(s.T(), faker, database.Chat{})
arg := database.UpdateChatManageAutomationsEnabledByIDParams{ID: chat.ID, ManageAutomationsEnabled: true}
dbm.EXPECT().GetChatByID(gomock.Any(), chat.ID).Return(chat, nil).AnyTimes()
dbm.EXPECT().UpdateChatManageAutomationsEnabledByID(gomock.Any(), arg).Return(int64(1), nil).AnyTimes()
check.Args(arg).Asserts(chat, policy.ActionUpdate).Returns(int64(1))
}))
s.Run("UpdateChatHeartbeats", s.Mocked(func(dbm *dbmock.MockStore, faker *gofakeit.Faker, check *expects) {
resultID := uuid.New()
arg := database.UpdateChatHeartbeatsParams{
Expand Down
2 changes: 2 additions & 0 deletions coderd/database/dbgen/dbgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ func Chat(t testing.TB, db database.Store, seed database.Chat) database.Chat {
Labels: labels,
DynamicTools: seed.DynamicTools,
ClientType: takeFirst(seed.ClientType, database.ChatClientTypeUi),

ManageAutomationsEnabled: seed.ManageAutomationsEnabled,
})
require.NoError(t, err, "insert chat")
return chat
Expand Down
8 changes: 8 additions & 0 deletions coderd/database/dbmetrics/querymetrics.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions coderd/database/dbmock/dbmock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions coderd/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 50 additions & 21 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading