@@ -88,7 +88,7 @@ func (r *LocalRuntime) SetAgentModel(ctx context.Context, agentName, modelRef st
8888 modelConfig .Name = modelRef
8989 // Check if this is an alloy model (no provider, comma-separated models)
9090 if isAlloyModelConfig (modelConfig ) {
91- providers , err := r .createProvidersFromAlloyConfig (ctx , modelConfig )
91+ providers , err := r .resolveModelRefs (ctx , modelConfig . Model )
9292 if err != nil {
9393 return fmt .Errorf ("failed to create alloy model from config: %w" , err )
9494 }
@@ -109,7 +109,7 @@ func (r *LocalRuntime) SetAgentModel(ctx context.Context, agentName, modelRef st
109109 // Check if this is an inline alloy spec (comma-separated provider/model specs)
110110 // e.g., "openai/gpt-4o,anthropic/claude-sonnet-4-0"
111111 if isInlineAlloySpec (modelRef ) {
112- providers , err := r .createProvidersFromInlineAlloy (ctx , modelRef )
112+ providers , err := r .resolveModelRefs (ctx , modelRef )
113113 if err != nil {
114114 return fmt .Errorf ("failed to create inline alloy model: %w" , err )
115115 }
@@ -146,17 +146,12 @@ func (r *LocalRuntime) resolveModelRef(ctx context.Context, modelRef string) (pr
146146 }
147147
148148 // Try inline "provider/model" format.
149- providerName , modelName , ok := strings . Cut (modelRef , "/" )
150- if ! ok || providerName == "" || modelName == "" {
149+ inlineCfg , err := latest . ParseModelRef (modelRef )
150+ if err != nil {
151151 return nil , fmt .Errorf ("invalid model reference %q: expected a model name from config or 'provider/model' format" , modelRef )
152152 }
153153
154- inlineCfg := & latest.ModelConfig {
155- Provider : providerName ,
156- Model : modelName ,
157- }
158-
159- return r .createProviderFromConfig (ctx , inlineCfg )
154+ return r .createProviderFromConfig (ctx , & inlineCfg )
160155}
161156
162157// isAlloyModelConfig checks if a model config is an alloy model (multiple models).
@@ -186,92 +181,44 @@ func isInlineAlloySpec(modelRef string) bool {
186181 return validParts >= 2
187182}
188183
189- // createProvidersFromInlineAlloy creates providers from an inline alloy spec.
190- // An inline alloy is comma-separated provider/model specs like "openai/gpt-4o,anthropic/claude-sonnet-4-0".
191- func (r * LocalRuntime ) createProvidersFromInlineAlloy (ctx context.Context , modelRef string ) ([]provider.Provider , error ) {
184+ // resolveModelRefs resolves a comma-separated list of model references into
185+ // providers. Each reference is first looked up in the config by name; if not
186+ // found it is parsed as an inline "provider/model" spec.
187+ func (r * LocalRuntime ) resolveModelRefs (ctx context.Context , commaSeparatedRefs string ) ([]provider.Provider , error ) {
192188 var providers []provider.Provider
193189
194- for part := range strings .SplitSeq (modelRef , "," ) {
195- part = strings .TrimSpace (part )
196- if part == "" {
190+ for ref := range strings .SplitSeq (commaSeparatedRefs , "," ) {
191+ ref = strings .TrimSpace (ref )
192+ if ref == "" {
197193 continue
198194 }
199195
200- // Check if this part exists as a named model in config
201- if modelCfg , exists := r .modelSwitcherCfg .Models [part ]; exists {
202- modelCfg .Name = part
196+ // Check if this ref exists as a named model in config
197+ if modelCfg , exists := r .modelSwitcherCfg .Models [ref ]; exists {
198+ modelCfg .Name = ref
203199 prov , err := r .createProviderFromConfig (ctx , & modelCfg )
204200 if err != nil {
205- return nil , fmt .Errorf ("failed to create provider for %q: %w" , part , err )
201+ return nil , fmt .Errorf ("failed to create provider for %q: %w" , ref , err )
206202 }
207203 providers = append (providers , prov )
208204 continue
209205 }
210206
211207 // Parse as provider/model
212- providerName , modelName , ok := strings .Cut (part , "/" )
213- if ! ok {
214- return nil , fmt .Errorf ("invalid model reference %q in inline alloy: expected 'provider/model' format" , part )
215- }
216-
217- inlineCfg := & latest.ModelConfig {
218- Provider : providerName ,
219- Model : modelName ,
220- }
221- prov , err := r .createProviderFromConfig (ctx , inlineCfg )
222- if err != nil {
223- return nil , fmt .Errorf ("failed to create provider for %q: %w" , part , err )
224- }
225- providers = append (providers , prov )
226- }
227-
228- if len (providers ) == 0 {
229- return nil , errors .New ("inline alloy spec has no valid models" )
230- }
231-
232- return providers , nil
233- }
234-
235- // createProvidersFromAlloyConfig creates providers for each model in an alloy configuration.
236- func (r * LocalRuntime ) createProvidersFromAlloyConfig (ctx context.Context , alloyCfg latest.ModelConfig ) ([]provider.Provider , error ) {
237- var providers []provider.Provider
238-
239- for modelRef := range strings .SplitSeq (alloyCfg .Model , "," ) {
240- modelRef = strings .TrimSpace (modelRef )
241- if modelRef == "" {
242- continue
208+ inlineCfg , parseErr := latest .ParseModelRef (ref )
209+ if parseErr != nil {
210+ return nil , fmt .Errorf ("invalid model reference %q: expected 'provider/model' format or a named model from config" , ref )
243211 }
244212
245- // Check if this model reference exists in the config
246- if modelCfg , exists := r .modelSwitcherCfg .Models [modelRef ]; exists {
247- modelCfg .Name = modelRef
248- prov , err := r .createProviderFromConfig (ctx , & modelCfg )
249- if err != nil {
250- return nil , fmt .Errorf ("failed to create provider for %q: %w" , modelRef , err )
251- }
252- providers = append (providers , prov )
253- continue
254- }
255-
256- // Try parsing as inline spec (provider/model)
257- providerName , modelName , ok := strings .Cut (modelRef , "/" )
258- if ! ok {
259- return nil , fmt .Errorf ("invalid model reference %q in alloy config: expected 'provider/model' format" , modelRef )
260- }
261-
262- inlineCfg := & latest.ModelConfig {
263- Provider : providerName ,
264- Model : modelName ,
265- }
266- prov , err := r .createProviderFromConfig (ctx , inlineCfg )
213+ prov , err := r .createProviderFromConfig (ctx , & inlineCfg )
267214 if err != nil {
268- return nil , fmt .Errorf ("failed to create provider for %q: %w" , modelRef , err )
215+ return nil , fmt .Errorf ("failed to create provider for %q: %w" , ref , err )
269216 }
270217 providers = append (providers , prov )
271218 }
272219
273220 if len (providers ) == 0 {
274- return nil , errors .New ("alloy model config has no valid models " )
221+ return nil , errors .New ("no valid models found in model reference list " )
275222 }
276223
277224 return providers , nil
0 commit comments