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.

【架构优化】从 savePlatformCodeBuddyKey 中拆出共享 Key 模型清单归一化与校验 sharedCodeBuddyModels (#813)

fx-kurumi6a1279701139
diff --git a/internal/httpapi/platform_settings.go b/internal/httpapi/platform_settings.go
index 76519a74..ee98693e 100644
--- a/internal/httpapi/platform_settings.go
+++ b/internal/httpapi/platform_settings.go
@@ -151,6 +151,33 @@ func (h *usersHandlers) resetPlatformAccountPassword(c *gin.Context) {
var sharedModelPattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._:/-]{0,127}$`)
+// sharedCodeBuddyModels normalizes the allow list of the shared CodeBuddy
+// key: blanks and duplicates are dropped in submission order, every ID must
+// match sharedModelPattern, and the list must hold 1..MaxSharedCodeBuddyModels
+// entries. The error text is shown to the administrator as is.
+func sharedCodeBuddyModels(raw []string) ([]string, error) {
+ var models []string
+ seen := map[string]bool{}
+ for _, value := range raw {
+ id := strings.TrimSpace(value)
+ if id == "" || seen[id] {
+ continue
+ }
+ if !sharedModelPattern.MatchString(id) {
+ return nil, errors.New("模型 ID 格式不正确:" + id)
+ }
+ seen[id] = true
+ models = append(models, id)
+ }
+ if len(models) == 0 {
+ return nil, errors.New("请至少选择一个允许使用的模型")
+ }
+ if len(models) > repo.MaxSharedCodeBuddyModels {
+ return nil, fmt.Errorf("最多选择 %d 个模型", repo.MaxSharedCodeBuddyModels)
+ }
+ return models, nil
+}
+
type platformCodeBuddyKeyResponse struct {
repo.PlatformCodeBuddyKeyStatus
// Catalog lists the models offered in the requested network environment.
@@ -223,33 +250,16 @@ func (h *usersHandlers) savePlatformCodeBuddyKey(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"error": "API Key 格式不正确"})
return
}
- var models []string
- seen := map[string]bool{}
- for _, raw := range input.Models {
- id := strings.TrimSpace(raw)
- if id == "" || seen[id] {
- continue
- }
- if !sharedModelPattern.MatchString(id) {
- c.JSON(http.StatusBadRequest, gin.H{"error": "模型 ID 格式不正确:" + id})
- return
- }
- seen[id] = true
- models = append(models, id)
- }
- if len(models) == 0 {
- c.JSON(http.StatusBadRequest, gin.H{"error": "请至少选择一个允许使用的模型"})
- return
- }
- if len(models) > repo.MaxSharedCodeBuddyModels {
- c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("最多选择 %d 个模型", repo.MaxSharedCodeBuddyModels)})
+ models, err := sharedCodeBuddyModels(input.Models)
+ if err != nil {
+ c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
var actorID int64
if current, ok := h.requestUser(c.Request, true); ok {
actorID = current.ID
}
- err := h.repo.SetPlatformCodeBuddyKey(repo.PlatformCodeBuddyKeyUpdate{APIKey: key, InternetEnvironment: environment, Models: models}, actorID)
+ err = h.repo.SetPlatformCodeBuddyKey(repo.PlatformCodeBuddyKeyUpdate{APIKey: key, InternetEnvironment: environment, Models: models}, actorID)
if errors.Is(err, repo.ErrSharedCodeBuddyKeyRequired) {
c.JSON(http.StatusBadRequest, gin.H{"error": "请填写 CodeBuddy API Key"})
return
diff --git a/internal/httpapi/platform_settings_models_test.go b/internal/httpapi/platform_settings_models_test.go
new file mode 100644
index 00000000..e84f9da8
--- /dev/null
+++ b/internal/httpapi/platform_settings_models_test.go
@@ -0,0 +1,53 @@
+package httpapi
+
+import (
+ "fmt"
+ "strings"
+ "testing"
+
+ "agentflow/internal/repo"
+)
+
+func TestSharedCodeBuddyModelsTrimsAndDeduplicatesInOrder(t *testing.T) {
+ got, err := sharedCodeBuddyModels([]string{" glm-5.1 ", "", "kimi-k2.6", "glm-5.1", " ", "org/model:v1"})
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ if strings.Join(got, ",") != "glm-5.1,kimi-k2.6,org/model:v1" {
+ t.Fatalf("models = %q", got)
+ }
+}
+
+func TestSharedCodeBuddyModelsRejectsInvalidID(t *testing.T) {
+ for _, id := range []string{"-leading-dash", "bad id", "中文模型", strings.Repeat("a", 129)} {
+ got, err := sharedCodeBuddyModels([]string{"glm-5.1", id})
+ want := "模型 ID 格式不正确:" + strings.TrimSpace(id)
+ if err == nil || err.Error() != want || got != nil {
+ t.Fatalf("id %q: models=%q err=%v, want %q", id, got, err, want)
+ }
+ }
+}
+
+func TestSharedCodeBuddyModelsRequiresAtLeastOne(t *testing.T) {
+ for _, raw := range [][]string{nil, {}, {"", " "}} {
+ if _, err := sharedCodeBuddyModels(raw); err == nil || err.Error() != "请至少选择一个允许使用的模型" {
+ t.Fatalf("raw %q: err = %v", raw, err)
+ }
+ }
+}
+
+func TestSharedCodeBuddyModelsEnforcesUpperBound(t *testing.T) {
+ raw := make([]string, 0, repo.MaxSharedCodeBuddyModels+1)
+ for i := 0; i < repo.MaxSharedCodeBuddyModels; i++ {
+ raw = append(raw, fmt.Sprintf("model-%d", i))
+ }
+ got, err := sharedCodeBuddyModels(append(raw, raw[0]))
+ if err != nil || len(got) != repo.MaxSharedCodeBuddyModels {
+ t.Fatalf("at limit (with duplicate): len=%d err=%v", len(got), err)
+ }
+ raw = append(raw, "one-too-many")
+ want := fmt.Sprintf("最多选择 %d 个模型", repo.MaxSharedCodeBuddyModels)
+ if _, err := sharedCodeBuddyModels(raw); err == nil || err.Error() != want {
+ t.Fatalf("over limit: err = %v, want %q", err, want)
+ }
+}