mirror of
https://github.com/coder/coder.git
synced 2026-06-03 04:58:23 +00:00
3452ab3166
Add a `chat_client_type` enum (`ui` | `api`) and `client_type` column to the `chats` table. The column defaults to `api` for new rows so API callers don't need to set it explicitly. Existing rows are backfilled to `ui`. The field flows through `CreateChatRequest`, `chatd.CreateOptions`, `InsertChat`, and is returned in the `Chat` response via `db2sdk`. <details> <summary>Implementation notes (Coder Agents generated)</summary> ### Changes **Database migration (000469)** - New enum `chat_client_type` with values `ui`, `api`. - New `client_type` column, `NOT NULL DEFAULT 'api'`. - Backfill: `UPDATE chats SET client_type = 'ui'`. **SQL query** — `InsertChat` now includes `client_type`. **SDK** — `ChatClientType` type added; `ClientType` field added to both `CreateChatRequest` (optional, defaults server-side to `api`) and `Chat` response. **Handler** — `postChats` maps the request field (defaulting to `api`) and passes it through `chatd.CreateOptions`. **Sub-agent** — Child chats inherit their parent's `client_type`. **db2sdk** — Maps the database value to the SDK type. ### Decision log - Default is `api` (not `ui`) so existing API integrations get the correct value without code changes. - Backfill sets existing rows to `ui` per requirement. - Child chats inherit `client_type` from parent rather than defaulting. </details>
79 lines
2.1 KiB
Go
79 lines
2.1 KiB
Go
package coderd
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
|
"github.com/coder/coder/v2/coderd/database/dbfake"
|
|
"github.com/coder/coder/v2/coderd/database/dbgen"
|
|
"github.com/coder/coder/v2/coderd/database/dbtestutil"
|
|
"github.com/coder/coder/v2/testutil"
|
|
)
|
|
|
|
func TestActiveAgentChatDefinitionsAgree(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitMedium))
|
|
db, _ := dbtestutil.NewDB(t)
|
|
|
|
org, err := db.GetDefaultOrganization(ctx)
|
|
require.NoError(t, err)
|
|
|
|
owner := dbgen.User(t, db, database.User{})
|
|
workspace := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
|
|
OrganizationID: org.ID,
|
|
OwnerID: owner.ID,
|
|
}).WithAgent().Do()
|
|
modelConfig := insertAgentChatTestModelConfig(ctx, t, db, owner.ID)
|
|
|
|
insertedChats := make([]database.Chat, 0, len(database.AllChatStatusValues())*2)
|
|
for _, archived := range []bool{false, true} {
|
|
for _, status := range database.AllChatStatusValues() {
|
|
chat, err := db.InsertChat(ctx, database.InsertChatParams{
|
|
OrganizationID: org.ID,
|
|
Status: status,
|
|
ClientType: database.ChatClientTypeUi,
|
|
OwnerID: owner.ID,
|
|
LastModelConfigID: modelConfig.ID,
|
|
Title: fmt.Sprintf("%s-archived-%t", status, archived),
|
|
AgentID: uuid.NullUUID{UUID: workspace.Agents[0].ID, Valid: true},
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
if archived {
|
|
_, err = db.ArchiveChatByID(ctx, chat.ID)
|
|
require.NoError(t, err)
|
|
|
|
chat, err = db.GetChatByID(ctx, chat.ID)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
insertedChats = append(insertedChats, chat)
|
|
}
|
|
}
|
|
|
|
activeChats, err := db.GetActiveChatsByAgentID(ctx, workspace.Agents[0].ID)
|
|
require.NoError(t, err)
|
|
|
|
activeByID := make(map[uuid.UUID]bool, len(activeChats))
|
|
for _, chat := range activeChats {
|
|
activeByID[chat.ID] = true
|
|
}
|
|
|
|
for _, chat := range insertedChats {
|
|
require.Equalf(
|
|
t,
|
|
isActiveAgentChat(chat),
|
|
activeByID[chat.ID],
|
|
"status=%s archived=%t",
|
|
chat.Status,
|
|
chat.Archived,
|
|
)
|
|
}
|
|
}
|