@@ -5,11 +5,6 @@ import (
55 "strings"
66)
77
8- type OpenClawPatch struct {
9- PrimaryModel string
10- Models map [string ]interface {}
11- }
12-
138type OpenClawProviderPatch struct {
149 PrimaryModel string
1510 ProviderKey string
@@ -20,54 +15,37 @@ type OpenClawProviderPatch struct {
2015 AuthHeader bool
2116}
2217
23- func BuildOpenClawPatch (provider , modelName , apiType string , reasoning bool , maxTokens , contextWindow int , baseURL , apiKey string ) (* OpenClawPatch , error ) {
24- providerPatch , err := BuildOpenClawProviderPatch (provider , modelName , apiType , baseURL , apiKey )
25- if err != nil {
26- return nil , err
27- }
28- _ , maxTokens , contextWindow = ResolveRuntimeParams (provider , apiType , maxTokens , contextWindow )
29- return newOpenClawPatch (
30- providerPatch ,
31- resolveOpenClawModelName (provider , providerPatch .ModelID ),
32- resolveOpenClawModelInput (provider , providerPatch .ModelID ),
33- reasoning ,
34- contextWindow ,
35- maxTokens ,
36- ), nil
37- }
38-
3918func BuildOpenClawProviderPatch (provider , modelName , apiType , baseURL , apiKey string ) (* OpenClawProviderPatch , error ) {
40- modelName = strings .TrimSpace (modelName )
4119 if modelName == "" {
4220 return nil , fmt .Errorf ("model is required" )
4321 }
4422 resolvedAPIType , _ , _ := ResolveRuntimeParams (provider , apiType , 0 , 0 )
4523 modelID := resolveOpenClawModelID (provider , modelName )
4624 switch provider {
4725 case "deepseek" :
48- return newOpenClawProviderPatch (modelName , "deepseek" , modelID , strings . TrimSpace ( apiKey ) , baseURL , "openai-completions" , false ), nil
26+ return newOpenClawProviderPatch (modelName , "deepseek" , modelID , apiKey , baseURL , "openai-completions" , false ), nil
4927 case "gemini" :
50- return newOpenClawProviderPatch ("google/" + modelID , "google" , modelID , strings . TrimSpace ( apiKey ) , baseURL , resolvedAPIType , false ), nil
28+ return newOpenClawProviderPatch ("google/" + modelID , "google" , modelID , apiKey , baseURL , resolvedAPIType , false ), nil
5129 case "moonshot" , "kimi" :
5230 return buildMoonshotProviderPatch (provider , modelName , modelID , baseURL , apiKey ), nil
5331 case "bailian-coding-plan" :
54- return newOpenClawProviderPatch ("bailian-coding-plan/" + modelID , "bailian-coding-plan" , modelID , strings . TrimSpace ( apiKey ) , baseURL , "openai-completions" , false ), nil
32+ return newOpenClawProviderPatch ("bailian-coding-plan/" + modelID , "bailian-coding-plan" , modelID , apiKey , baseURL , "openai-completions" , false ), nil
5533 case "ark-coding-plan" :
56- return newOpenClawProviderPatch ("ark-coding-plan/" + modelID , "ark-coding-plan" , modelID , strings . TrimSpace ( apiKey ) , baseURL , "openai-completions" , false ), nil
34+ return newOpenClawProviderPatch ("ark-coding-plan/" + modelID , "ark-coding-plan" , modelID , apiKey , baseURL , "openai-completions" , false ), nil
5735 case "minimax" :
58- return newOpenClawProviderPatch ("minimax/" + modelID , "minimax" , modelID , strings . TrimSpace ( apiKey ) , baseURL , "anthropic-messages" , true ), nil
36+ return newOpenClawProviderPatch ("minimax/" + modelID , "minimax" , modelID , apiKey , baseURL , "anthropic-messages" , true ), nil
5937 case "xiaomi" :
60- return newOpenClawProviderPatch ("xiaomi/" + modelID , "xiaomi" , modelID , strings . TrimSpace ( apiKey ) , baseURL , "openai-completions" , false ), nil
38+ return newOpenClawProviderPatch ("xiaomi/" + modelID , "xiaomi" , modelID , apiKey , baseURL , "openai-completions" , false ), nil
6139 case "custom" , "vllm" :
62- return newOpenClawProviderPatch (provider + "/" + modelID , provider , modelID , strings . TrimSpace ( apiKey ), strings . TrimSpace ( baseURL ) , resolvedAPIType , false ), nil
40+ return newOpenClawProviderPatch (provider + "/" + modelID , provider , modelID , apiKey , baseURL , resolvedAPIType , false ), nil
6341 case "ollama" :
64- return newOpenClawProviderPatch (modelName , "ollama" , modelID , "ollama" , strings . TrimSpace ( baseURL ) , resolvedAPIType , false ), nil
42+ return newOpenClawProviderPatch (modelName , "ollama" , modelID , "ollama" , baseURL , resolvedAPIType , false ), nil
6543 case "kimi-coding" :
66- return newOpenClawProviderPatch (modelName , "kimi-coding" , modelID , strings . TrimSpace ( apiKey ) , baseURL , "anthropic-messages" , false ), nil
44+ return newOpenClawProviderPatch (modelName , "kimi-coding" , modelID , apiKey , baseURL , "anthropic-messages" , false ), nil
6745 case "zai" :
68- return newOpenClawProviderPatch ("zai/" + modelID , "zai" , modelID , strings . TrimSpace ( apiKey ) , baseURL , "openai-completions" , false ), nil
46+ return newOpenClawProviderPatch ("zai/" + modelID , "zai" , modelID , apiKey , baseURL , "openai-completions" , false ), nil
6947 default :
70- return newOpenClawProviderPatch (modelName , provider , modelID , strings . TrimSpace ( apiKey ) , baseURL , resolvedAPIType , false ), nil
48+ return newOpenClawProviderPatch (modelName , provider , modelID , apiKey , baseURL , resolvedAPIType , false ), nil
7149 }
7250}
7351
@@ -78,7 +56,7 @@ func buildMoonshotProviderPatch(provider, modelName, modelID, baseURL, apiKey st
7856 providerKey = "moonshot"
7957 primaryModel = "moonshot/" + modelID
8058 }
81- return newOpenClawProviderPatch (primaryModel , providerKey , modelID , strings . TrimSpace ( apiKey ) , baseURL , "openai-completions" , false )
59+ return newOpenClawProviderPatch (primaryModel , providerKey , modelID , apiKey , baseURL , "openai-completions" , false )
8260}
8361
8462func newOpenClawProviderPatch (primaryModel , providerKey , modelID , apiKey , baseURL , apiType string , authHeader bool ) * OpenClawProviderPatch {
@@ -93,84 +71,16 @@ func newOpenClawProviderPatch(primaryModel, providerKey, modelID, apiKey, baseUR
9371 }
9472}
9573
96- func newOpenClawPatch (providerPatch * OpenClawProviderPatch , modelName string , input []string , reasoning bool , contextWindow , maxTokens int ) * OpenClawPatch {
97- model := map [string ]interface {}{
98- "id" : providerPatch .ModelID ,
99- "name" : modelName ,
100- "reasoning" : reasoning ,
101- "input" : input ,
102- "contextWindow" : contextWindow ,
103- "maxTokens" : maxTokens ,
104- "cost" : map [string ]interface {}{},
105- }
106- providerConfig := map [string ]interface {}{
107- "apiKey" : providerPatch .APIKey ,
108- "baseUrl" : providerPatch .BaseURL ,
109- "api" : providerPatch .APIType ,
110- "models" : []map [string ]interface {}{model },
111- }
112- if providerPatch .AuthHeader {
113- providerConfig ["authHeader" ] = true
114- }
115- return & OpenClawPatch {
116- PrimaryModel : providerPatch .PrimaryModel ,
117- Models : map [string ]interface {}{
118- "mode" : "merge" ,
119- "providers" : map [string ]interface {}{
120- providerPatch .ProviderKey : providerConfig ,
121- },
122- },
123- }
124- }
125-
12674func resolveOpenClawModelID (provider , modelName string ) string {
12775 if provider == "custom" || provider == "vllm" {
128- return normalizeCustomModel (modelName )
76+ target := strings .TrimLeft (modelName , "/" )
77+ if parts := strings .SplitN (target , "/" , 2 ); len (parts ) == 2 && parts [0 ] == "custom" {
78+ return strings .TrimLeft (parts [1 ], "/" )
79+ }
80+ return target
12981 }
13082 if parts := strings .SplitN (modelName , "/" , 2 ); len (parts ) == 2 {
13183 return parts [1 ]
13284 }
13385 return modelName
13486}
135-
136- func resolveOpenClawModelName (provider , modelID string ) string {
137- if item , ok := resolveOpenClawCatalogModel (provider , modelID ); ok {
138- return item .Name
139- }
140- if provider == "kimi-coding" {
141- return "Kimi for Coding"
142- }
143- return modelID
144- }
145-
146- func resolveOpenClawModelInput (provider , modelID string ) []string {
147- if item , ok := resolveOpenClawCatalogModel (provider , modelID ); ok && len (item .Input ) > 0 {
148- return item .Input
149- }
150- if provider == "kimi-coding" {
151- return []string {"text" , "image" }
152- }
153- return []string {"text" }
154- }
155-
156- func resolveOpenClawCatalogModel (provider , modelID string ) (Model , bool ) {
157- candidates := []string {provider + "/" + modelID }
158- if provider == "gemini" {
159- candidates = append (candidates , "google/" + modelID )
160- }
161- for _ , candidate := range candidates {
162- if item , ok := FindModel (provider , candidate ); ok {
163- return item , true
164- }
165- }
166- return Model {}, false
167- }
168-
169- func normalizeCustomModel (modelName string ) string {
170- trim := strings .TrimSpace (modelName )
171- trim = strings .TrimLeft (trim , "/" )
172- if parts := strings .SplitN (trim , "/" , 2 ); len (parts ) == 2 && strings .EqualFold (parts [0 ], "custom" ) {
173- return strings .TrimLeft (strings .TrimSpace (parts [1 ]), "/" )
174- }
175- return trim
176- }
0 commit comments