Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

refactor(runtime): 拆分 FoldCursorCLIModels 的扫描、普通 ID 并入家族与家族行构造 (#521) (#522)

fx-kurumif29682b8f94c
diff --git a/internal/runtime/cursor_models.go b/internal/runtime/cursor_models.go
index 3e0af88d..2a7cfa46 100644
--- a/internal/runtime/cursor_models.go
+++ b/internal/runtime/cursor_models.go
@@ -310,18 +310,46 @@ func inferredCursorDefault(variants map[string]string) string {
// effort→ID mapping (Efforts, DefaultEffort); it is saved with the owner's
// Cursor catalog, which CursorModelID and CursorModelEfforts resolve from.
func FoldCursorCLIModels(models []domain.CLIModel) []domain.CLIModel {
- present := map[string]bool{}
- type group struct {
- name string
- variants map[string]string
+ present, groups, tail := scanCursorCLIModels(models)
+ dynamic := mergePlainCursorFamilies(tail, groups)
+ out := make([]domain.CLIModel, 0, len(present)+len(dynamic))
+ for _, family := range cursorModels {
+ if present[family.ID] {
+ out = append(out, domain.CLIModel{ID: family.ID, Name: family.Name})
+ }
}
- groups := map[string]*group{}
- type slot struct {
- family bool
- id string
- plain domain.CLIModel
+ for _, item := range dynamic {
+ if !item.family {
+ out = append(out, item.plain)
+ continue
+ }
+ out = append(out, cursorFamilyRow(item.id, groups[item.id]))
}
- var tail []slot
+ return out
+}
+
+// cursorFamilyGroup collects the effort variants of one family that is not
+// in the built-in table.
+type cursorFamilyGroup struct {
+ name string
+ variants map[string]string
+}
+
+// cursorModelSlot keeps the CLI's listing order of a dynamic family or of a
+// plain model ID.
+type cursorModelSlot struct {
+ family bool
+ id string
+ plain domain.CLIModel
+}
+
+// scanCursorCLIModels sorts the listed IDs into built-in families (present),
+// dynamic families folded from effort suffixes (groups), and the listing
+// order of dynamic families and plain IDs (tail).
+func scanCursorCLIModels(models []domain.CLIModel) (map[string]bool, map[string]*cursorFamilyGroup, []cursorModelSlot) {
+ present := map[string]bool{}
+ groups := map[string]*cursorFamilyGroup{}
+ var tail []cursorModelSlot
seen := map[string]bool{}
for _, model := range models {
id := strings.TrimSpace(model.ID)
@@ -339,11 +367,11 @@ func FoldCursorCLIModels(models []domain.CLIModel) []domain.CLIModel {
}
g := groups[familyID]
if g == nil {
- g = &group{variants: map[string]string{}}
+ g = &cursorFamilyGroup{variants: map[string]string{}}
groups[familyID] = g
if !seen["family:"+familyID] {
seen["family:"+familyID] = true
- tail = append(tail, slot{family: true, id: familyID})
+ tail = append(tail, cursorModelSlot{family: true, id: familyID})
}
}
if _, exists := g.variants[effort]; !exists {
@@ -358,9 +386,16 @@ func FoldCursorCLIModels(models []domain.CLIModel) []domain.CLIModel {
continue
}
seen["plain:"+id] = true
- tail = append(tail, slot{id: id, plain: domain.CLIModel{ID: id, Name: strings.TrimSpace(model.Name)}})
+ tail = append(tail, cursorModelSlot{id: id, plain: domain.CLIModel{ID: id, Name: strings.TrimSpace(model.Name)}})
}
- var dynamic []slot
+ return present, groups, tail
+}
+
+// mergePlainCursorFamilies drops a plain ID that names a dynamic family and
+// records it as that family's medium variant unless one was listed; its name
+// then becomes the family name.
+func mergePlainCursorFamilies(tail []cursorModelSlot, groups map[string]*cursorFamilyGroup) []cursorModelSlot {
+ var dynamic []cursorModelSlot
for _, item := range tail {
if item.family {
dynamic = append(dynamic, item)
@@ -378,27 +413,19 @@ func FoldCursorCLIModels(models []domain.CLIModel) []domain.CLIModel {
}
}
}
- out := make([]domain.CLIModel, 0, len(present)+len(dynamic))
- for _, family := range cursorModels {
- if present[family.ID] {
- out = append(out, domain.CLIModel{ID: family.ID, Name: family.Name})
- }
+ return dynamic
+}
+
+// cursorFamilyRow builds the saved row of a dynamic family, falling back to
+// the family ID when no name was found.
+func cursorFamilyRow(id string, g *cursorFamilyGroup) domain.CLIModel {
+ name := g.name
+ if name == "" {
+ name = id
}
- for _, item := range dynamic {
- if !item.family {
- out = append(out, item.plain)
- continue
- }
- g := groups[item.id]
- name := g.name
- if name == "" {
- name = item.id
- }
- variants := make(map[string]string, len(g.variants))
- for effort, raw := range g.variants {
- variants[effort] = raw
- }
- out = append(out, domain.CLIModel{ID: item.id, Name: name, Efforts: variants, DefaultEffort: inferredCursorDefault(variants)})
+ variants := make(map[string]string, len(g.variants))
+ for effort, raw := range g.variants {
+ variants[effort] = raw
}
- return out
+ return domain.CLIModel{ID: id, Name: name, Efforts: variants, DefaultEffort: inferredCursorDefault(variants)}
}
diff --git a/internal/runtime/cursor_models_fold_test.go b/internal/runtime/cursor_models_fold_test.go
new file mode 100644
index 00000000..975b9b9a
--- /dev/null
+++ b/internal/runtime/cursor_models_fold_test.go
@@ -0,0 +1,75 @@
+package runtime
+
+import (
+ "reflect"
+ "testing"
+
+ "agentflow/internal/domain"
+)
+
+func TestScanCursorCLIModelsSortsIDs(t *testing.T) {
+ present, groups, tail := scanCursorCLIModels([]domain.CLIModel{
+ {ID: "gemini-4-carbon-high"},
+ {ID: " "},
+ {ID: "widget-low", Name: "Widget Low"},
+ {ID: "widget-medium", Name: "Widget Medium"},
+ {ID: "widget-low", Name: "Widget Low Duplicate"},
+ {ID: "plain", Name: " Plain "},
+ {ID: "plain", Name: "Plain Again"},
+ })
+ if !present["gemini-4-carbon"] || len(present) != 1 {
+ t.Fatalf("present = %v", present)
+ }
+ g := groups["widget"]
+ if g == nil || len(groups) != 1 {
+ t.Fatalf("groups = %v", groups)
+ }
+ if g.name != "Widget" || !reflect.DeepEqual(g.variants, map[string]string{"low": "widget-low", "medium": "widget-medium"}) {
+ t.Fatalf("widget group = %+v", *g)
+ }
+ want := []cursorModelSlot{
+ {family: true, id: "widget"},
+ {id: "plain", plain: domain.CLIModel{ID: "plain", Name: "Plain"}},
+ }
+ if !reflect.DeepEqual(tail, want) {
+ t.Fatalf("tail = %+v, want %+v", tail, want)
+ }
+}
+
+func TestMergePlainCursorFamilies(t *testing.T) {
+ groups := map[string]*cursorFamilyGroup{
+ "widget": {name: "Widget Stripped", variants: map[string]string{"low": "widget-low"}},
+ "gadget": {name: "Gadget", variants: map[string]string{"medium": "gadget-medium"}},
+ }
+ tail := []cursorModelSlot{
+ {family: true, id: "widget"},
+ {id: "widget", plain: domain.CLIModel{ID: "widget", Name: "Widget"}},
+ {family: true, id: "gadget"},
+ {id: "gadget", plain: domain.CLIModel{ID: "gadget", Name: "Gadget Plain"}},
+ {id: "other", plain: domain.CLIModel{ID: "other"}},
+ }
+ got := mergePlainCursorFamilies(tail, groups)
+ want := []cursorModelSlot{tail[0], tail[2], tail[4]}
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("dynamic = %+v, want %+v", got, want)
+ }
+ if w := groups["widget"]; w.name != "Widget" || w.variants["medium"] != "widget" {
+ t.Errorf("widget should take the plain ID as medium: %+v", *w)
+ }
+ if g := groups["gadget"]; g.name != "Gadget" || g.variants["medium"] != "gadget-medium" {
+ t.Errorf("gadget already had medium and must stay: %+v", *g)
+ }
+}
+
+func TestCursorFamilyRow(t *testing.T) {
+ g := &cursorFamilyGroup{variants: map[string]string{"low": "solo-low", "high": "solo-high"}}
+ got := cursorFamilyRow("solo", g)
+ want := domain.CLIModel{ID: "solo", Name: "solo", DefaultEffort: inferredCursorDefault(g.variants), Efforts: map[string]string{"low": "solo-low", "high": "solo-high"}}
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("row = %+v, want %+v", got, want)
+ }
+ got.Efforts["low"] = "changed"
+ if g.variants["low"] != "solo-low" {
+ t.Error("row efforts must be a copy of the group variants")
+ }
+}