| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | |
| 6 | "reasonix/internal/extension/protocol" |
| 7 | ) |
| 8 | |
| 9 | // RemovePluginModelRefs moves every model ref owned by pluginID to the first |
| 10 | // remaining configured provider, or clears it when none is left, and reports |
| 11 | // whether anything changed. Plugin refs resolve outside the config catalog, so |
| 12 | // a removed plugin would otherwise strand them and fail the next boot. |
| 13 | func (c *Config) RemovePluginModelRefs(pluginID string) bool { |
| 14 | pluginID = strings.TrimSpace(pluginID) |
| 15 | if pluginID == "" { |
| 16 | return false |
| 17 | } |
| 18 | ownedByPlugin := func(ref string) bool { |
| 19 | return protocol.PluginRefOwner(strings.TrimSpace(ref)) == pluginID |
| 20 | } |
| 21 | |
| 22 | defaultOwned := ownedByPlugin(c.DefaultModel) |
| 23 | plannerOwned := ownedByPlugin(c.Agent.PlannerModel) |
| 24 | subagentOwned := ownedByPlugin(c.Agent.SubagentModel) |
| 25 | subagentModelsOwned := map[string]bool{} |
| 26 | for skill, ref := range c.Agent.SubagentModels { |
| 27 | if ownedByPlugin(ref) { |
| 28 | subagentModelsOwned[skill] = true |
| 29 | } |
| 30 | } |
| 31 | if !defaultOwned && !plannerOwned && !subagentOwned && len(subagentModelsOwned) == 0 { |
| 32 | return false |
| 33 | } |
| 34 | |
| 35 | fallback := c.providerRemovalFallback("") |
| 36 | if defaultOwned { |
| 37 | c.DefaultModel = fallback |
| 38 | } |
| 39 | if plannerOwned { |
| 40 | c.Agent.PlannerModel = fallback |
| 41 | } |
| 42 | if subagentOwned { |
| 43 | c.Agent.SubagentModel = fallback |
| 44 | } |
| 45 | for skill := range subagentModelsOwned { |
| 46 | if fallback != "" { |
| 47 | c.Agent.SubagentModels[skill] = fallback |
| 48 | } else { |
| 49 | delete(c.Agent.SubagentModels, skill) |
| 50 | } |
| 51 | } |
| 52 | return true |
| 53 | } |
| 54 |