fix: auto-update profile name when model changes (#325)
* fix: auto-update profile name when model changes When updating an agent profile's model without explicitly setting a name, automatically update the profile name to match the new model's display name. Uses the existing fetchModelsWithCache pattern to avoid expensive ListModels() calls. * test: add tests for profile name auto-update on model change
This commit is contained in:
@@ -24,6 +24,7 @@ type testAgent struct {
|
||||
runtime *agents.RuntimeConfig
|
||||
permissionSettings map[string]agents.PermissionSetting
|
||||
logoData []byte
|
||||
modelList *agents.ModelList
|
||||
}
|
||||
|
||||
func (a *testAgent) ID() string { return a.id }
|
||||
@@ -42,6 +43,9 @@ func (a *testAgent) IsInstalled(ctx context.Context) (*agents.DiscoveryResult, e
|
||||
func (a *testAgent) DefaultModel() string { return "" }
|
||||
|
||||
func (a *testAgent) ListModels(ctx context.Context) (*agents.ModelList, error) {
|
||||
if a.modelList != nil {
|
||||
return a.modelList, nil
|
||||
}
|
||||
return &agents.ModelList{}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -7,13 +7,20 @@ import (
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
|
||||
"github.com/kandev/kandev/internal/agent/agents"
|
||||
"github.com/kandev/kandev/internal/agent/registry"
|
||||
"github.com/kandev/kandev/internal/agent/settings/modelfetcher"
|
||||
"github.com/kandev/kandev/internal/agent/settings/models"
|
||||
"github.com/kandev/kandev/internal/agent/settings/store"
|
||||
"github.com/kandev/kandev/internal/common/logger"
|
||||
)
|
||||
|
||||
func newTestControllerWithRepo(t *testing.T) (*Controller, *registry.Registry) {
|
||||
ctrl, reg, _ := newTestControllerWithRepoAndStore(t)
|
||||
return ctrl, reg
|
||||
}
|
||||
|
||||
func newTestControllerWithRepoAndStore(t *testing.T) (*Controller, *registry.Registry, store.Repository) {
|
||||
t.Helper()
|
||||
log, _ := logger.NewLogger(logger.LoggingConfig{Level: "error", Format: "json"})
|
||||
db, err := sqlx.Open("sqlite3", ":memory:")
|
||||
@@ -34,7 +41,7 @@ func newTestControllerWithRepo(t *testing.T) (*Controller, *registry.Registry) {
|
||||
modelCache: modelfetcher.NewCache(),
|
||||
logger: log,
|
||||
}
|
||||
return ctrl, reg
|
||||
return ctrl, reg, repo
|
||||
}
|
||||
|
||||
func TestSlugify(t *testing.T) {
|
||||
@@ -219,3 +226,94 @@ func TestDeleteAgent_UnregistersFromRegistry(t *testing.T) {
|
||||
t.Error("expected agent to be unregistered from registry after delete")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProfile_AutoUpdatesNameWhenModelChanges(t *testing.T) {
|
||||
ctrl, repo := setupProfileUpdateTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
agent := &models.Agent{Name: "test-agent"}
|
||||
if err := repo.CreateAgent(ctx, agent); err != nil {
|
||||
t.Fatalf("create agent: %v", err)
|
||||
}
|
||||
|
||||
profile, err := ctrl.CreateProfile(ctx, CreateProfileRequest{
|
||||
AgentID: agent.ID,
|
||||
Name: "Model A Display",
|
||||
Model: "model-a",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create profile: %v", err)
|
||||
}
|
||||
|
||||
// Update only the model (not the name)
|
||||
newModel := "model-b"
|
||||
updated, err := ctrl.UpdateProfile(ctx, UpdateProfileRequest{
|
||||
ID: profile.ID,
|
||||
Model: &newModel,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("update profile: %v", err)
|
||||
}
|
||||
|
||||
if updated.Name != "Model B Display" {
|
||||
t.Errorf("profile name = %q, want %q", updated.Name, "Model B Display")
|
||||
}
|
||||
if updated.Model != "model-b" {
|
||||
t.Errorf("profile model = %q, want %q", updated.Model, "model-b")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProfile_KeepsExplicitNameWhenProvided(t *testing.T) {
|
||||
ctrl, repo := setupProfileUpdateTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
agent := &models.Agent{Name: "test-agent"}
|
||||
if err := repo.CreateAgent(ctx, agent); err != nil {
|
||||
t.Fatalf("create agent: %v", err)
|
||||
}
|
||||
|
||||
profile, err := ctrl.CreateProfile(ctx, CreateProfileRequest{
|
||||
AgentID: agent.ID,
|
||||
Name: "Original Name",
|
||||
Model: "model-a",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create profile: %v", err)
|
||||
}
|
||||
|
||||
// Update both model and name explicitly
|
||||
newModel := "model-b"
|
||||
newName := "My Custom Name"
|
||||
updated, err := ctrl.UpdateProfile(ctx, UpdateProfileRequest{
|
||||
ID: profile.ID,
|
||||
Model: &newModel,
|
||||
Name: &newName,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("update profile: %v", err)
|
||||
}
|
||||
|
||||
if updated.Name != "My Custom Name" {
|
||||
t.Errorf("profile name = %q, want %q", updated.Name, "My Custom Name")
|
||||
}
|
||||
}
|
||||
|
||||
// setupProfileUpdateTest creates a controller with a test agent that has multiple models.
|
||||
func setupProfileUpdateTest(t *testing.T) (*Controller, store.Repository) {
|
||||
t.Helper()
|
||||
ctrl, reg, repo := newTestControllerWithRepoAndStore(t)
|
||||
ta := &testAgent{
|
||||
id: "test-agent",
|
||||
name: "test-agent",
|
||||
modelList: &agents.ModelList{
|
||||
Models: []agents.Model{
|
||||
{ID: "model-a", Name: "Model A Display"},
|
||||
{ID: "model-b", Name: "Model B Display"},
|
||||
},
|
||||
},
|
||||
}
|
||||
if err := reg.Register(ta); err != nil {
|
||||
t.Fatalf("register agent: %v", err)
|
||||
}
|
||||
return ctrl, repo
|
||||
}
|
||||
|
||||
@@ -78,6 +78,12 @@ func (c *Controller) UpdateProfile(ctx context.Context, req UpdateProfileRequest
|
||||
return nil, ErrModelRequired
|
||||
}
|
||||
profile.Model = *req.Model
|
||||
// Auto-update profile name when model changes and name wasn't explicitly provided
|
||||
if req.Name == nil {
|
||||
if newName := c.resolveProfileNameForModel(ctx, profile.AgentID, *req.Model); newName != "" {
|
||||
profile.Name = newName
|
||||
}
|
||||
}
|
||||
}
|
||||
if req.AutoApprove != nil {
|
||||
profile.AutoApprove = *req.AutoApprove
|
||||
@@ -170,3 +176,24 @@ func toProfileDTO(profile *models.AgentProfile) dto.AgentProfileDTO {
|
||||
UpdatedAt: profile.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
// resolveProfileNameForModel looks up the agent by ID, fetches its model list (using cache),
|
||||
// and returns the display name for the given model ID. Returns empty string on failure.
|
||||
func (c *Controller) resolveProfileNameForModel(ctx context.Context, agentID, modelID string) string {
|
||||
agent, err := c.repo.GetAgent(ctx, agentID)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
agentConfig, ok := c.agentRegistry.Get(agent.Name)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
|
||||
modelEntries, _ := c.fetchModelsWithCache(ctx, agentConfig)
|
||||
for _, m := range modelEntries {
|
||||
if m.ID == modelID {
|
||||
return m.Name
|
||||
}
|
||||
}
|
||||
return modelID // fallback to model ID if not found
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user