feat(provider,web,asr): enhance model management with explicit provider metadata (#2701)
* feat(provider,web): enhance model management with provider options * fix(asr): enhance compatibility for ElevenLabs transcription model * fix(provider,web): align provider availability predicates and add flow gating * fix(web,asr): preserve legacy elevenlabs transcription configs * fix(provider,web,asr): normalize elevenlabs configs and gate default chat models * fix: tighten provider catalog and elevenlabs compatibility
This commit is contained in:
26 files changed
+2341
-193
No files matched your search
@@ -82,7 +82,8 @@ Notes:
|
||||
"model_list": [
|
||||
{
|
||||
"model_name": "elevenlabs-asr",
|
||||
"model": "elevenlabs/scribe_v1"
|
||||
"provider": "elevenlabs",
|
||||
"model": "scribe_v1"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -130,7 +131,7 @@ PicoClaw currently supports three main ASR routes:
|
||||
|
||||
| Route | Example models | Behavior |
|
||||
| --- | --- | --- |
|
||||
| ElevenLabs ASR | `elevenlabs/scribe_v1` | Uses the ElevenLabs transcription API. |
|
||||
| ElevenLabs ASR | `provider: elevenlabs`, `model: scribe_v1` | Uses the ElevenLabs transcription API. |
|
||||
| Whisper endpoint models | `openai/whisper-1`, `groq/whisper-large-v3` | Uses an OpenAI-compatible `/audio/transcriptions` endpoint. |
|
||||
| Audio-capable chat models **(Under construction)** | `openai/gpt-4o-audio-preview`, `gemini/gemini-2.5-flash` | Sends audio to a multimodal chat model and asks it to transcribe. |
|
||||
|
||||
@@ -142,7 +143,7 @@ If you are unsure which one to pick, choose Groq Whisper or ElevenLabs first.
|
||||
|
||||
1. **Preferred path**: resolve `voice.model_name` against `model_list`.
|
||||
2. If that resolved model is:
|
||||
- `elevenlabs/...`, PicoClaw uses the ElevenLabs transcriber.
|
||||
- an `elevenlabs` provider model, PicoClaw uses the ElevenLabs transcriber.
|
||||
- an OpenAI-compatible Whisper model, PicoClaw uses the Whisper transcriber.
|
||||
- an audio-capable chat model, PicoClaw uses `AudioModelTranscriber`.
|
||||
3. **Fallback path**: if `voice.model_name` is not set, PicoClaw performs a compatibility scan through `model_list` for legacy auto-detected ASR entries.
|
||||
|
||||
@@ -82,7 +82,8 @@ model_list:
|
||||
"model_list": [
|
||||
{
|
||||
"model_name": "elevenlabs-asr",
|
||||
"model": "elevenlabs/scribe_v1"
|
||||
"provider": "elevenlabs",
|
||||
"model": "scribe_v1"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -130,7 +131,7 @@ PicoClaw 目前主要支持三种 ASR 路径:
|
||||
|
||||
| 路径 | 示例模型 | 行为说明 |
|
||||
| --- | --- | --- |
|
||||
| ElevenLabs ASR | `elevenlabs/scribe_v1` | 使用 ElevenLabs 的语音转录接口。 |
|
||||
| ElevenLabs ASR | `provider: elevenlabs`,`model: scribe_v1` | 使用 ElevenLabs 的语音转录接口。 |
|
||||
| Whisper 接口模型 | `openai/whisper-1`、`groq/whisper-large-v3` | 使用 OpenAI 兼容的 `/audio/transcriptions` 接口。 |
|
||||
| 支持音频的聊天模型 **(重构中)** | `openai/gpt-4o-audio-preview`、`gemini/gemini-2.5-flash` | 把音频发给多模态聊天模型,并要求它返回转录结果。 |
|
||||
|
||||
@@ -142,7 +143,7 @@ PicoClaw 目前主要支持三种 ASR 路径:
|
||||
|
||||
1. **首选路径**:根据 `voice.model_name` 在 `model_list` 中找到对应模型。
|
||||
2. 如果找到的模型属于以下类型:
|
||||
- `elevenlabs/...`,则使用 ElevenLabs transcriber。
|
||||
- `provider=elevenlabs` 的模型,则使用 ElevenLabs transcriber。
|
||||
- OpenAI 兼容的 Whisper 模型,则使用 Whisper transcriber。
|
||||
- 支持音频输入的聊天模型,则使用 `AudioModelTranscriber`。
|
||||
3. **回退路径**:如果没有设置 `voice.model_name`,PicoClaw 会为了兼容旧配置,扫描 `model_list` 中可自动识别的 ASR 条目。
|
||||
|
||||
+21
-6
@@ -8,6 +8,12 @@ import (
|
||||
"github.com/sipeed/picoclaw/pkg/providers"
|
||||
)
|
||||
|
||||
const elevenLabsSupportedModelID = "scribe_v1"
|
||||
|
||||
func ElevenLabsSupportedModelID() string {
|
||||
return elevenLabsSupportedModelID
|
||||
}
|
||||
|
||||
type Transcriber interface {
|
||||
Name() string
|
||||
Transcribe(ctx context.Context, audioFilePath string) (*TranscriptionResponse, error)
|
||||
@@ -72,14 +78,23 @@ func whisperModelID(modelCfg *config.ModelConfig) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func isElevenLabsTranscriptionModel(modelCfg *config.ModelConfig) bool {
|
||||
if modelCfg == nil || modelCfg.APIKey() == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
protocol, _ := providers.ExtractProtocol(modelCfg)
|
||||
return protocol == "elevenlabs"
|
||||
}
|
||||
|
||||
func transcriberFromModelConfig(modelCfg *config.ModelConfig) Transcriber {
|
||||
if modelCfg == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
protocol, _ := providers.ExtractProtocol(modelCfg)
|
||||
if protocol == "elevenlabs" && modelCfg.APIKey() != "" {
|
||||
return NewElevenLabsTranscriber(modelCfg.APIKey(), modelCfg.APIBase)
|
||||
if isElevenLabsTranscriptionModel(modelCfg) {
|
||||
_, modelID := providers.ExtractProtocol(modelCfg)
|
||||
return NewElevenLabsTranscriber(modelCfg.APIKey(), modelCfg.APIBase, modelID)
|
||||
}
|
||||
if modelID := whisperModelID(modelCfg); modelID != "" {
|
||||
return NewWhisperTranscriber(modelCfg)
|
||||
@@ -95,9 +110,9 @@ func fallbackTranscriberFromModelConfig(modelCfg *config.ModelConfig) Transcribe
|
||||
return nil
|
||||
}
|
||||
|
||||
protocol, _ := providers.ExtractProtocol(modelCfg)
|
||||
if protocol == "elevenlabs" && modelCfg.APIKey() != "" {
|
||||
return NewElevenLabsTranscriber(modelCfg.APIKey(), modelCfg.APIBase)
|
||||
if isElevenLabsTranscriptionModel(modelCfg) {
|
||||
_, modelID := providers.ExtractProtocol(modelCfg)
|
||||
return NewElevenLabsTranscriber(modelCfg.APIKey(), modelCfg.APIBase, modelID)
|
||||
}
|
||||
if modelID := whisperModelID(modelCfg); modelID != "" {
|
||||
return NewWhisperTranscriber(modelCfg)
|
||||
|
||||
@@ -46,6 +46,21 @@ func TestDetectTranscriber(t *testing.T) {
|
||||
},
|
||||
wantName: "elevenlabs",
|
||||
},
|
||||
{
|
||||
name: "explicit elevenlabs provider selects elevenlabs transcriber",
|
||||
cfg: &config.Config{
|
||||
Voice: config.VoiceConfig{ModelName: "my-asr-model"},
|
||||
ModelList: []*config.ModelConfig{
|
||||
{
|
||||
ModelName: "my-asr-model",
|
||||
Provider: "elevenlabs",
|
||||
Model: "scribe_v1",
|
||||
APIKeys: config.SimpleSecureStrings("sk_elevenlabs_test"),
|
||||
},
|
||||
},
|
||||
},
|
||||
wantName: "elevenlabs",
|
||||
},
|
||||
{
|
||||
name: "voice model name alias selects whisper transcriber for groq",
|
||||
cfg: &config.Config{
|
||||
|
||||
@@ -20,19 +20,24 @@ import (
|
||||
type ElevenLabsTranscriber struct {
|
||||
apiKey string
|
||||
apiBase string
|
||||
modelID string
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
func NewElevenLabsTranscriber(apiKey, apiBase string) *ElevenLabsTranscriber {
|
||||
func NewElevenLabsTranscriber(apiKey, apiBase, modelID string) *ElevenLabsTranscriber {
|
||||
logger.DebugCF("voice", "Creating ElevenLabs transcriber", map[string]any{"has_api_key": apiKey != ""})
|
||||
|
||||
if apiBase == "" {
|
||||
apiBase = "https://api.elevenlabs.io"
|
||||
}
|
||||
if modelID == "" || modelID != ElevenLabsSupportedModelID() {
|
||||
modelID = ElevenLabsSupportedModelID()
|
||||
}
|
||||
|
||||
return &ElevenLabsTranscriber{
|
||||
apiKey: apiKey,
|
||||
apiBase: apiBase,
|
||||
modelID: modelID,
|
||||
httpClient: &http.Client{
|
||||
Timeout: 120 * time.Second,
|
||||
},
|
||||
@@ -74,7 +79,7 @@ func (t *ElevenLabsTranscriber) Transcribe(ctx context.Context, audioFilePath st
|
||||
return nil, fmt.Errorf("failed to copy file content: %w", err)
|
||||
}
|
||||
|
||||
if err = writer.WriteField("model_id", "scribe_v1"); err != nil {
|
||||
if err = writer.WriteField("model_id", t.modelID); err != nil {
|
||||
return nil, fmt.Errorf("failed to write model_id field: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -3,10 +3,14 @@ package asr
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"mime"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -14,7 +18,7 @@ import (
|
||||
var _ Transcriber = (*ElevenLabsTranscriber)(nil)
|
||||
|
||||
func TestElevenLabsTranscriberName(t *testing.T) {
|
||||
tr := NewElevenLabsTranscriber("sk_test", "")
|
||||
tr := NewElevenLabsTranscriber("sk_test", "", "scribe_v1")
|
||||
if got := tr.Name(); got != "elevenlabs" {
|
||||
t.Errorf("Name() = %q, want %q", got, "elevenlabs")
|
||||
}
|
||||
@@ -35,6 +39,35 @@ func TestElevenLabsTranscribe(t *testing.T) {
|
||||
if r.Header.Get("Xi-Api-Key") != "sk_test" {
|
||||
t.Errorf("unexpected xi-api-key header: %s", r.Header.Get("Xi-Api-Key"))
|
||||
}
|
||||
mediaType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||
if err != nil {
|
||||
t.Fatalf("ParseMediaType() error = %v", err)
|
||||
}
|
||||
if mediaType != "multipart/form-data" {
|
||||
t.Fatalf("content-type = %q, want multipart/form-data", mediaType)
|
||||
}
|
||||
reader := multipart.NewReader(r.Body, params["boundary"])
|
||||
var gotModelID string
|
||||
for {
|
||||
part, err := reader.NextPart()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("NextPart() error = %v", err)
|
||||
}
|
||||
if part.FormName() != "model_id" {
|
||||
continue
|
||||
}
|
||||
body, err := io.ReadAll(part)
|
||||
if err != nil {
|
||||
t.Fatalf("ReadAll(part) error = %v", err)
|
||||
}
|
||||
gotModelID = strings.TrimSpace(string(body))
|
||||
}
|
||||
if gotModelID != "scribe_v1" {
|
||||
t.Fatalf("model_id = %q, want %q", gotModelID, "scribe_v1")
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(TranscriptionResponse{
|
||||
Text: "hello from elevenlabs",
|
||||
@@ -43,7 +76,7 @@ func TestElevenLabsTranscribe(t *testing.T) {
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
tr := NewElevenLabsTranscriber("sk_test", "")
|
||||
tr := NewElevenLabsTranscriber("sk_test", "", "scribe_v1")
|
||||
tr.apiBase = srv.URL
|
||||
|
||||
resp, err := tr.Transcribe(context.Background(), audioPath)
|
||||
@@ -64,7 +97,7 @@ func TestElevenLabsTranscribe(t *testing.T) {
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
tr := NewElevenLabsTranscriber("sk_bad", "")
|
||||
tr := NewElevenLabsTranscriber("sk_bad", "", "scribe_v1")
|
||||
tr.apiBase = srv.URL
|
||||
|
||||
_, err := tr.Transcribe(context.Background(), audioPath)
|
||||
@@ -74,10 +107,54 @@ func TestElevenLabsTranscribe(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("missing file", func(t *testing.T) {
|
||||
tr := NewElevenLabsTranscriber("sk_test", "")
|
||||
tr := NewElevenLabsTranscriber("sk_test", "", "scribe_v1")
|
||||
_, err := tr.Transcribe(context.Background(), filepath.Join(tmpDir, "nonexistent.ogg"))
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing file, got nil")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("unsupported model falls back to scribe_v1", func(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
mediaType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||
if err != nil {
|
||||
t.Fatalf("ParseMediaType() error = %v", err)
|
||||
}
|
||||
if mediaType != "multipart/form-data" {
|
||||
t.Fatalf("content-type = %q, want multipart/form-data", mediaType)
|
||||
}
|
||||
reader := multipart.NewReader(r.Body, params["boundary"])
|
||||
var gotModelID string
|
||||
for {
|
||||
part, err := reader.NextPart()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("NextPart() error = %v", err)
|
||||
}
|
||||
if part.FormName() != "model_id" {
|
||||
continue
|
||||
}
|
||||
body, err := io.ReadAll(part)
|
||||
if err != nil {
|
||||
t.Fatalf("ReadAll(part) error = %v", err)
|
||||
}
|
||||
gotModelID = strings.TrimSpace(string(body))
|
||||
}
|
||||
if gotModelID != "scribe_v1" {
|
||||
t.Fatalf("model_id = %q, want runtime fallback to %q", gotModelID, "scribe_v1")
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(TranscriptionResponse{Text: "ok"})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
tr := NewElevenLabsTranscriber("sk_test", "", "unsupported-model")
|
||||
tr.apiBase = srv.URL
|
||||
|
||||
if _, err := tr.Transcribe(context.Background(), audioPath); err != nil {
|
||||
t.Fatalf("Transcribe() error: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -110,19 +110,7 @@ func ExtractProtocol(cfg *config.ModelConfig) (protocol, modelID string) {
|
||||
if provider := strings.TrimSpace(cfg.Provider); provider != "" {
|
||||
return NormalizeProvider(provider), model
|
||||
}
|
||||
if model == "" {
|
||||
return "", ""
|
||||
}
|
||||
|
||||
protocol, rest, found := strings.Cut(model, "/")
|
||||
if !found {
|
||||
return "openai", model
|
||||
}
|
||||
protocol = strings.TrimSpace(protocol)
|
||||
if protocol == "" {
|
||||
return "", strings.TrimSpace(rest)
|
||||
}
|
||||
return NormalizeProvider(protocol), strings.TrimSpace(rest)
|
||||
return SplitModelProviderAndID(model, "openai")
|
||||
}
|
||||
|
||||
// ResolveAPIBase returns the configured API base, or the protocol default when
|
||||
@@ -154,6 +142,7 @@ func CreateProviderFromConfig(cfg *config.ModelConfig) (LLMProvider, string, err
|
||||
}
|
||||
|
||||
protocol, modelID := ExtractProtocol(cfg)
|
||||
authMethod := strings.ToLower(strings.TrimSpace(cfg.AuthMethod))
|
||||
|
||||
userAgent := cfg.UserAgent
|
||||
if userAgent == "" {
|
||||
@@ -163,7 +152,7 @@ func CreateProviderFromConfig(cfg *config.ModelConfig) (LLMProvider, string, err
|
||||
switch protocol {
|
||||
case "openai":
|
||||
// OpenAI with OAuth/token auth (Codex-style)
|
||||
if cfg.AuthMethod == "oauth" || cfg.AuthMethod == "token" {
|
||||
if authMethod == "oauth" || authMethod == "token" {
|
||||
provider, err := createCodexAuthProvider()
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
@@ -320,7 +309,7 @@ func CreateProviderFromConfig(cfg *config.ModelConfig) (LLMProvider, string, err
|
||||
return finalizeProviderFromConfig(provider, modelID, cfg)
|
||||
|
||||
case "anthropic":
|
||||
if cfg.AuthMethod == "oauth" || cfg.AuthMethod == "token" {
|
||||
if authMethod == "oauth" || authMethod == "token" {
|
||||
// Use OAuth credentials from auth store
|
||||
provider, err := createClaudeAuthProvider()
|
||||
if err != nil {
|
||||
@@ -431,7 +420,7 @@ func finalizeProviderFromConfig(
|
||||
}
|
||||
|
||||
func isEmptyAPIKeyAllowed(protocol string) bool {
|
||||
meta, ok := protocolMetaByName[protocol]
|
||||
meta, ok := protocolMetaForName(protocol)
|
||||
return ok && meta.emptyAPIKeyAllowed
|
||||
}
|
||||
|
||||
@@ -451,9 +440,19 @@ func DefaultAPIBaseForProtocol(protocol string) string {
|
||||
|
||||
// getDefaultAPIBase returns the default API base URL for a given protocol.
|
||||
func getDefaultAPIBase(protocol string) string {
|
||||
meta, ok := protocolMetaByName[protocol]
|
||||
meta, ok := protocolMetaForName(protocol)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
return meta.defaultAPIBase
|
||||
}
|
||||
|
||||
func protocolMetaForName(protocol string) (protocolMeta, bool) {
|
||||
if meta, ok := protocolMetaByName[protocol]; ok {
|
||||
return meta, true
|
||||
}
|
||||
if meta, ok := attachedModelProviderMetaByName[protocol]; ok {
|
||||
return meta.protocolMeta, true
|
||||
}
|
||||
return protocolMeta{}, false
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/sipeed/picoclaw/pkg/auth"
|
||||
"github.com/sipeed/picoclaw/pkg/config"
|
||||
)
|
||||
|
||||
@@ -101,6 +102,12 @@ func TestExtractProtocol(t *testing.T) {
|
||||
wantProtocol: "",
|
||||
wantModelID: "gpt-4o",
|
||||
},
|
||||
{
|
||||
name: "unknown prefix falls back to openai",
|
||||
config: &config.ModelConfig{Model: "meta-llama/Llama-3.1-8B-Instruct"},
|
||||
wantProtocol: "openai",
|
||||
wantModelID: "meta-llama/Llama-3.1-8B-Instruct",
|
||||
},
|
||||
{
|
||||
name: "nil config",
|
||||
wantProtocol: "",
|
||||
@@ -605,6 +612,41 @@ func TestCreateProviderFromConfig_CodexCLI(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateProviderFromConfig_OpenAIMixedCaseAuthMethodUsesOAuthBranch(t *testing.T) {
|
||||
origGetCredential := getCredential
|
||||
getCredential = func(provider string) (*auth.AuthCredential, error) {
|
||||
if provider != "openai" {
|
||||
t.Fatalf("provider = %q, want %q", provider, "openai")
|
||||
}
|
||||
return &auth.AuthCredential{
|
||||
AccessToken: "test-token",
|
||||
AccountID: "acct-test",
|
||||
Provider: "openai",
|
||||
AuthMethod: "oauth",
|
||||
}, nil
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
getCredential = origGetCredential
|
||||
})
|
||||
|
||||
cfg := &config.ModelConfig{
|
||||
ModelName: "test-openai-oauth",
|
||||
Model: "openai/gpt-5.4",
|
||||
AuthMethod: "OAuth",
|
||||
}
|
||||
|
||||
provider, modelID, err := CreateProviderFromConfig(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateProviderFromConfig() error = %v", err)
|
||||
}
|
||||
if provider == nil {
|
||||
t.Fatal("CreateProviderFromConfig() returned nil provider")
|
||||
}
|
||||
if modelID != "gpt-5.4" {
|
||||
t.Errorf("modelID = %q, want %q", modelID, "gpt-5.4")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateProviderFromConfig_MissingAPIKey(t *testing.T) {
|
||||
cfg := &config.ModelConfig{
|
||||
ModelName: "test-no-key",
|
||||
@@ -619,8 +661,9 @@ func TestCreateProviderFromConfig_MissingAPIKey(t *testing.T) {
|
||||
|
||||
func TestCreateProviderFromConfig_UnknownProtocol(t *testing.T) {
|
||||
cfg := &config.ModelConfig{
|
||||
ModelName: "test-unknown",
|
||||
Model: "unknown-protocol/model",
|
||||
ModelName: "test-unknown-provider",
|
||||
Provider: "unknown-protocol",
|
||||
Model: "model",
|
||||
}
|
||||
cfg.SetAPIKey("test-key")
|
||||
|
||||
@@ -630,6 +673,26 @@ func TestCreateProviderFromConfig_UnknownProtocol(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateProviderFromConfig_UnknownModelPrefixDefaultsToOpenAI(t *testing.T) {
|
||||
cfg := &config.ModelConfig{
|
||||
ModelName: "test-unknown-model-prefix",
|
||||
Model: "meta-llama/Llama-3.1-8B-Instruct",
|
||||
APIBase: "https://api.example.com/v1",
|
||||
}
|
||||
cfg.SetAPIKey("test-key")
|
||||
|
||||
provider, modelID, err := CreateProviderFromConfig(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateProviderFromConfig() error = %v", err)
|
||||
}
|
||||
if provider == nil {
|
||||
t.Fatal("CreateProviderFromConfig() returned nil provider")
|
||||
}
|
||||
if modelID != "meta-llama/Llama-3.1-8B-Instruct" {
|
||||
t.Fatalf("modelID = %q, want full model ID", modelID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateProviderFromConfig_NilConfig(t *testing.T) {
|
||||
_, _, err := CreateProviderFromConfig(nil)
|
||||
if err == nil {
|
||||
@@ -889,6 +952,71 @@ func TestGetDefaultAPIBase_QwenUSAliases(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestModelProviderOptions(t *testing.T) {
|
||||
options := ModelProviderOptions()
|
||||
if len(options) == 0 {
|
||||
t.Fatal("ModelProviderOptions() returned no options")
|
||||
}
|
||||
|
||||
seen := make(map[string]ModelProviderOption, len(options))
|
||||
for _, option := range options {
|
||||
seen[option.ID] = option
|
||||
}
|
||||
|
||||
if _, ok := seen["openai"]; !ok {
|
||||
t.Fatal("openai option missing")
|
||||
}
|
||||
if option, ok := seen["openai"]; ok && !option.CreateAllowed {
|
||||
t.Fatal("openai should be creatable")
|
||||
}
|
||||
if option, ok := seen["lmstudio"]; !ok {
|
||||
t.Fatal("lmstudio option missing")
|
||||
} else if !option.EmptyAPIKeyAllowed {
|
||||
t.Fatal("lmstudio should allow empty API keys")
|
||||
}
|
||||
if option, ok := seen["anthropic"]; !ok {
|
||||
t.Fatal("anthropic option missing")
|
||||
} else if option.DefaultAPIBase != "https://api.anthropic.com/v1" {
|
||||
t.Fatalf("anthropic default_api_base = %q, want %q", option.DefaultAPIBase, "https://api.anthropic.com/v1")
|
||||
}
|
||||
if _, ok := seen["azure"]; !ok {
|
||||
t.Fatal("azure option missing")
|
||||
}
|
||||
if option, ok := seen["bedrock"]; !ok {
|
||||
t.Fatal("bedrock option missing")
|
||||
} else if !option.CreateAllowed {
|
||||
t.Fatal("bedrock should be creatable and defer credential/build errors to runtime")
|
||||
}
|
||||
if option, ok := seen["elevenlabs"]; !ok {
|
||||
t.Fatal("elevenlabs option missing")
|
||||
} else {
|
||||
if option.DefaultAPIBase != "https://api.elevenlabs.io" {
|
||||
t.Fatalf("elevenlabs default_api_base = %q, want %q", option.DefaultAPIBase, "https://api.elevenlabs.io")
|
||||
}
|
||||
if option.DefaultModelAllowed {
|
||||
t.Fatal("elevenlabs should be ASR-only and therefore not allowed as a default chat model")
|
||||
}
|
||||
}
|
||||
if option, ok := seen["antigravity"]; !ok {
|
||||
t.Fatal("antigravity option missing")
|
||||
} else {
|
||||
if !option.CreateAllowed {
|
||||
t.Fatal("antigravity should be creatable")
|
||||
}
|
||||
if option.DefaultAuthMethod != "oauth" {
|
||||
t.Fatalf("antigravity default_auth_method = %q, want %q", option.DefaultAuthMethod, "oauth")
|
||||
}
|
||||
if !option.AuthMethodLocked {
|
||||
t.Fatal("antigravity auth method should be locked")
|
||||
}
|
||||
}
|
||||
if option, ok := seen["github-copilot"]; !ok {
|
||||
t.Fatal("github-copilot option missing")
|
||||
} else if option.DefaultAPIBase != "localhost:4321" {
|
||||
t.Fatalf("github-copilot default_api_base = %q, want %q", option.DefaultAPIBase, "localhost:4321")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateProviderFromConfig_MinimaxInjectsReasoningSplit(t *testing.T) {
|
||||
var requestBody map[string]any
|
||||
|
||||
|
||||
+15
-10
@@ -17,18 +17,13 @@ func ParseModelRef(raw string, defaultProvider string) *ModelRef {
|
||||
return nil
|
||||
}
|
||||
|
||||
if idx := strings.Index(raw, "/"); idx > 0 {
|
||||
provider := NormalizeProvider(raw[:idx])
|
||||
model := strings.TrimSpace(raw[idx+1:])
|
||||
if model == "" {
|
||||
return nil
|
||||
}
|
||||
return &ModelRef{Provider: provider, Model: model}
|
||||
provider, model := SplitModelProviderAndID(raw, defaultProvider)
|
||||
if model == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &ModelRef{
|
||||
Provider: NormalizeProvider(defaultProvider),
|
||||
Model: raw,
|
||||
Provider: provider,
|
||||
Model: model,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,6 +48,8 @@ func NormalizeProvider(provider string) string {
|
||||
return "zhipu"
|
||||
case "google":
|
||||
return "gemini"
|
||||
case "google-antigravity":
|
||||
return "antigravity"
|
||||
case "alibaba-coding", "qwen-coding":
|
||||
return "coding-plan"
|
||||
case "alibaba-coding-anthropic":
|
||||
@@ -61,6 +58,14 @@ func NormalizeProvider(provider string) string {
|
||||
return "qwen-intl"
|
||||
case "dashscope-us":
|
||||
return "qwen-us"
|
||||
case "azure-openai":
|
||||
return "azure"
|
||||
case "claudecli":
|
||||
return "claude-cli"
|
||||
case "codexcli":
|
||||
return "codex-cli"
|
||||
case "copilot":
|
||||
return "github-copilot"
|
||||
}
|
||||
|
||||
return p
|
||||
|
||||
@@ -72,7 +72,12 @@ func TestNormalizeProvider(t *testing.T) {
|
||||
{"claude", "anthropic"},
|
||||
{"glm", "zhipu"},
|
||||
{"google", "gemini"},
|
||||
{"google-antigravity", "antigravity"},
|
||||
{"groq", "groq"},
|
||||
{"azure-openai", "azure"},
|
||||
{"claudecli", "claude-cli"},
|
||||
{"codexcli", "codex-cli"},
|
||||
{"copilot", "github-copilot"},
|
||||
// Alibaba Coding Plan aliases
|
||||
{"alibaba-coding", "coding-plan"},
|
||||
{"qwen-coding", "coding-plan"},
|
||||
@@ -131,3 +136,42 @@ func TestParseModelRef_DefaultProviderNormalization(t *testing.T) {
|
||||
t.Errorf("provider = %q, want openai (normalized from GPT)", ref.Provider)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseModelRef_UnknownPrefixFallsBackToDefaultProvider(t *testing.T) {
|
||||
ref := ParseModelRef("meta-llama/Llama-3.1-8B-Instruct", "openai")
|
||||
if ref == nil {
|
||||
t.Fatal("expected non-nil ref")
|
||||
}
|
||||
if ref.Provider != "openai" {
|
||||
t.Fatalf("provider = %q, want openai", ref.Provider)
|
||||
}
|
||||
if ref.Model != "meta-llama/Llama-3.1-8B-Instruct" {
|
||||
t.Fatalf("model = %q, want full original model ID", ref.Model)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseModelRef_UnknownPrefixPreservesEmptyDefaultProvider(t *testing.T) {
|
||||
ref := ParseModelRef("meta-llama/Llama-3.1-8B-Instruct", "")
|
||||
if ref == nil {
|
||||
t.Fatal("expected non-nil ref")
|
||||
}
|
||||
if ref.Provider != "" {
|
||||
t.Fatalf("provider = %q, want empty", ref.Provider)
|
||||
}
|
||||
if ref.Model != "meta-llama/Llama-3.1-8B-Instruct" {
|
||||
t.Fatalf("model = %q, want full original model ID", ref.Model)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseModelRef_KnownNonSelectableProvider(t *testing.T) {
|
||||
ref := ParseModelRef("bedrock/us.anthropic.claude-sonnet-4-20250514-v1:0", "openai")
|
||||
if ref == nil {
|
||||
t.Fatal("expected non-nil ref")
|
||||
}
|
||||
if ref.Provider != "bedrock" {
|
||||
t.Fatalf("provider = %q, want bedrock", ref.Provider)
|
||||
}
|
||||
if ref.Model != "us.anthropic.claude-sonnet-4-20250514-v1:0" {
|
||||
t.Fatalf("model = %q, want preserved bedrock model ID", ref.Model)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
package providers
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ModelProviderOption describes a canonical provider entry exposed to the Web UI.
|
||||
type ModelProviderOption struct {
|
||||
ID string `json:"id"`
|
||||
DefaultAPIBase string `json:"default_api_base"`
|
||||
EmptyAPIKeyAllowed bool `json:"empty_api_key_allowed"`
|
||||
CreateAllowed bool `json:"create_allowed"`
|
||||
DefaultModelAllowed bool `json:"default_model_allowed"`
|
||||
DefaultAuthMethod string `json:"default_auth_method,omitempty"`
|
||||
AuthMethodLocked bool `json:"auth_method_locked,omitempty"`
|
||||
}
|
||||
|
||||
type attachedModelProviderMeta struct {
|
||||
protocolMeta
|
||||
createAllowed bool
|
||||
defaultModelAllowed bool
|
||||
defaultAuthMethod string
|
||||
authMethodLocked bool
|
||||
}
|
||||
|
||||
// attachedModelProviderMetaByName augments protocolMetaByName for provider
|
||||
// families that are implemented in CreateProviderFromConfig but intentionally
|
||||
// kept out of the core HTTP metadata map because they have special auth/runtime
|
||||
// semantics.
|
||||
var attachedModelProviderMetaByName = map[string]attachedModelProviderMeta{
|
||||
"azure": {createAllowed: true, defaultModelAllowed: true},
|
||||
"anthropic": {
|
||||
protocolMeta: protocolMeta{defaultAPIBase: "https://api.anthropic.com/v1"},
|
||||
createAllowed: true,
|
||||
defaultModelAllowed: true,
|
||||
},
|
||||
"anthropic-messages": {
|
||||
protocolMeta: protocolMeta{defaultAPIBase: "https://api.anthropic.com/v1"},
|
||||
createAllowed: true,
|
||||
defaultModelAllowed: true,
|
||||
},
|
||||
"bedrock": {createAllowed: true, defaultModelAllowed: true},
|
||||
"antigravity": {
|
||||
createAllowed: true,
|
||||
defaultModelAllowed: true,
|
||||
defaultAuthMethod: "oauth",
|
||||
authMethodLocked: true,
|
||||
},
|
||||
"claude-cli": {createAllowed: true, defaultModelAllowed: true},
|
||||
"codex-cli": {createAllowed: true, defaultModelAllowed: true},
|
||||
"github-copilot": {
|
||||
protocolMeta: protocolMeta{defaultAPIBase: "localhost:4321"},
|
||||
createAllowed: true,
|
||||
defaultModelAllowed: true,
|
||||
},
|
||||
// ElevenLabs is intentionally exposed only as an ASR-capable provider. It
|
||||
// belongs in the shared model catalog because ASR is configured via
|
||||
// model_list, but it must not be selectable as the default chat model.
|
||||
"elevenlabs": {
|
||||
protocolMeta: protocolMeta{defaultAPIBase: "https://api.elevenlabs.io"},
|
||||
createAllowed: true,
|
||||
defaultModelAllowed: false,
|
||||
},
|
||||
}
|
||||
|
||||
// ModelProviderOptions returns the canonical provider catalog exposed to the Web UI.
|
||||
func ModelProviderOptions() []ModelProviderOption {
|
||||
optionsByID := make(map[string]ModelProviderOption, len(protocolMetaByName)+len(attachedModelProviderMetaByName))
|
||||
for provider := range protocolMetaByName {
|
||||
if NormalizeProvider(provider) != provider {
|
||||
continue
|
||||
}
|
||||
optionsByID[provider] = ModelProviderOption{
|
||||
ID: provider,
|
||||
DefaultAPIBase: DefaultAPIBaseForProtocol(provider),
|
||||
EmptyAPIKeyAllowed: IsEmptyAPIKeyAllowedForProtocol(provider),
|
||||
CreateAllowed: true,
|
||||
DefaultModelAllowed: true,
|
||||
}
|
||||
}
|
||||
for provider, meta := range attachedModelProviderMetaByName {
|
||||
if NormalizeProvider(provider) != provider {
|
||||
continue
|
||||
}
|
||||
optionsByID[provider] = ModelProviderOption{
|
||||
ID: provider,
|
||||
DefaultAPIBase: meta.defaultAPIBase,
|
||||
EmptyAPIKeyAllowed: meta.emptyAPIKeyAllowed,
|
||||
CreateAllowed: meta.createAllowed,
|
||||
DefaultModelAllowed: meta.defaultModelAllowed,
|
||||
DefaultAuthMethod: meta.defaultAuthMethod,
|
||||
AuthMethodLocked: meta.authMethodLocked,
|
||||
}
|
||||
}
|
||||
|
||||
options := make([]ModelProviderOption, 0, len(optionsByID))
|
||||
for _, option := range optionsByID {
|
||||
options = append(options, option)
|
||||
}
|
||||
sort.Slice(options, func(i, j int) bool {
|
||||
return options[i].ID < options[j].ID
|
||||
})
|
||||
return options
|
||||
}
|
||||
|
||||
// IsSupportedModelProvider reports whether provider resolves to a provider ID
|
||||
// returned by ModelProviderOptions.
|
||||
func IsSupportedModelProvider(provider string) bool {
|
||||
normalized := NormalizeProvider(provider)
|
||||
if normalized == "" {
|
||||
return false
|
||||
}
|
||||
if _, ok := protocolMetaByName[normalized]; ok {
|
||||
return true
|
||||
}
|
||||
_, ok := attachedModelProviderMetaByName[normalized]
|
||||
return ok
|
||||
}
|
||||
|
||||
// IsCreatableModelProvider reports whether provider can be selected for a new
|
||||
// model entry from the Web UI.
|
||||
func IsCreatableModelProvider(provider string) bool {
|
||||
normalized := NormalizeProvider(provider)
|
||||
if normalized == "" {
|
||||
return false
|
||||
}
|
||||
if _, ok := protocolMetaByName[normalized]; ok {
|
||||
return true
|
||||
}
|
||||
meta, ok := attachedModelProviderMetaByName[normalized]
|
||||
return ok && meta.createAllowed
|
||||
}
|
||||
|
||||
// IsDefaultModelProvider reports whether provider can be used as the default
|
||||
// chat model. Some providers such as ASR-only entries are intentionally
|
||||
// exposed in model_list management but cannot drive the gateway default model.
|
||||
func IsDefaultModelProvider(provider string) bool {
|
||||
normalized := NormalizeProvider(provider)
|
||||
if normalized == "" {
|
||||
return false
|
||||
}
|
||||
if _, ok := protocolMetaByName[normalized]; ok {
|
||||
return true
|
||||
}
|
||||
meta, ok := attachedModelProviderMetaByName[normalized]
|
||||
return ok && meta.defaultModelAllowed
|
||||
}
|
||||
|
||||
// SplitModelProviderAndID separates a legacy "provider/model" string into its
|
||||
// effective provider and canonical model ID. Unknown prefixes are treated as
|
||||
// part of the model ID and fall back to defaultProvider.
|
||||
func SplitModelProviderAndID(model, defaultProvider string) (provider, modelID string) {
|
||||
model = strings.TrimSpace(model)
|
||||
if model == "" {
|
||||
return "", ""
|
||||
}
|
||||
|
||||
provider, modelID = splitKnownProviderModel(model)
|
||||
if provider != "" || modelID != "" {
|
||||
return provider, modelID
|
||||
}
|
||||
|
||||
return NormalizeProvider(defaultProvider), model
|
||||
}
|
||||
|
||||
func splitKnownProviderModel(model string) (provider, modelID string) {
|
||||
provider, modelID, found := strings.Cut(strings.TrimSpace(model), "/")
|
||||
if !found {
|
||||
return "", ""
|
||||
}
|
||||
provider = strings.TrimSpace(provider)
|
||||
modelID = strings.TrimSpace(modelID)
|
||||
if provider == "" {
|
||||
return "", modelID
|
||||
}
|
||||
if !IsSupportedModelProvider(provider) {
|
||||
return "", ""
|
||||
}
|
||||
return NormalizeProvider(provider), modelID
|
||||
}
|
||||
Reference in new issue
Block a user