refactor: UI

This commit is contained in:
leookun
2026-08-07 01:50:20 +08:00
parent 5bd39ed317
commit 225de8bb85
39 changed files with 1734 additions and 1604 deletions
@@ -16,6 +16,7 @@ func (store *Store) LegacyRuntimeSnapshot(ctx context.Context) (legacyruntime.Ru
for _, item := range cfg.ModelAdapters {
adapters = append(adapters, legacyruntime.ModelAdapterConfig{
ID: item.ID,
Sort: item.Sort,
DisplayName: item.DisplayName,
Type: item.Type,
BaseURL: item.BaseURL,
@@ -147,6 +147,7 @@ func (manager *Manager) LegacyRuntimeSnapshot(_ context.Context) (legacyruntime.
for _, item := range cfg.ModelAdapters {
adapters = append(adapters, legacyruntime.ModelAdapterConfig{
ID: item.ID,
Sort: item.Sort,
DisplayName: item.DisplayName,
Type: item.Type,
BaseURL: item.BaseURL,
+24
View File
@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net"
"sort"
"strconv"
"strings"
@@ -21,6 +22,7 @@ const (
type ModelAdapterConfig struct {
ID string `json:"id,omitempty" yaml:"-"`
Sort int `json:"sort" yaml:"sort"`
DisplayName string `json:"displayName" yaml:"displayName"`
Type string `json:"type" yaml:"type"`
BaseURL string `json:"baseURL" yaml:"baseURL"`
@@ -104,6 +106,7 @@ func NormalizeModelAdapterConfigs(input []ModelAdapterConfig) ([]ModelAdapterCon
}
nextType := normalizeModelAdapterType(item.Type)
next := ModelAdapterConfig{
Sort: item.Sort,
DisplayName: strings.TrimSpace(item.DisplayName),
Type: nextType,
BaseURL: baseURL,
@@ -164,9 +167,30 @@ func NormalizeModelAdapterConfigs(input []ModelAdapterConfig) ([]ModelAdapterCon
seenChannelIDs[next.ID] = struct{}{}
normalized = append(normalized, next)
}
normalizeModelAdapterSorts(normalized)
return normalized, nil
}
func normalizeModelAdapterSorts(adapters []ModelAdapterConfig) {
sort.SliceStable(adapters, func(leftIndex, rightIndex int) bool {
left := adapters[leftIndex].Sort
right := adapters[rightIndex].Sort
switch {
case left <= 0 && right <= 0:
return false
case left <= 0:
return false
case right <= 0:
return true
default:
return left < right
}
})
for index := range adapters {
adapters[index].Sort = index + 1
}
}
func validateJSONMap(value string, fieldName string) error {
text := strings.TrimSpace(value)
if text == "" {
@@ -0,0 +1,60 @@
package config
import "testing"
func testModelAdapter(displayName string, sortValue int) ModelAdapterConfig {
return ModelAdapterConfig{
Sort: sortValue,
DisplayName: displayName,
Type: "openai",
BaseURL: "https://api.example.com/v1",
APIKey: "test-key",
TooltipData: displayName,
ModelID: displayName,
ReasoningEffort: "medium",
OpenAIEndpoint: "/v1/responses",
}
}
func TestNormalizeModelAdapterConfigsPreservesLegacyArrayOrder(t *testing.T) {
adapters, err := NormalizeModelAdapterConfigs([]ModelAdapterConfig{
testModelAdapter("first", 0),
testModelAdapter("second", 0),
testModelAdapter("third", 0),
})
if err != nil {
t.Fatalf("NormalizeModelAdapterConfigs returned error: %v", err)
}
for index, expectedName := range []string{"first", "second", "third"} {
if adapters[index].DisplayName != expectedName {
t.Fatalf("adapter %d = %q, want %q", index, adapters[index].DisplayName, expectedName)
}
if adapters[index].Sort != index+1 {
t.Fatalf("adapter %d sort = %d, want %d", index, adapters[index].Sort, index+1)
}
}
}
func TestNormalizeModelAdapterConfigsUsesStableExplicitSort(t *testing.T) {
adapters, err := NormalizeModelAdapterConfigs([]ModelAdapterConfig{
testModelAdapter("legacy", 0),
testModelAdapter("third", 30),
testModelAdapter("first", 10),
testModelAdapter("second-a", 20),
testModelAdapter("second-b", 20),
})
if err != nil {
t.Fatalf("NormalizeModelAdapterConfigs returned error: %v", err)
}
expectedNames := []string{"first", "second-a", "second-b", "third", "legacy"}
for index, expectedName := range expectedNames {
if adapters[index].DisplayName != expectedName {
t.Fatalf("adapter %d = %q, want %q", index, adapters[index].DisplayName, expectedName)
}
if adapters[index].Sort != index+1 {
t.Fatalf("adapter %d sort = %d, want %d", index, adapters[index].Sort, index+1)
}
}
}