SetCurrentAgentModel sets the model for the current agent and persists the override in the session. Returns an error if model switching is not supported by the runtime (e.g., remote runtimes). Pass an empty modelRef to clear the override and use the agent's default model.
(ctx context.Context, modelRef string)
| 952 | // supported by the runtime (e.g., remote runtimes). |
| 953 | // Pass an empty modelRef to clear the override and use the agent's default model. |
| 954 | func (a *App) SetCurrentAgentModel(ctx context.Context, modelRef string) error { |
| 955 | agentName := a.runtime.CurrentAgentName(ctx) |
| 956 | |
| 957 | // Set the model override on the runtime (empty modelRef clears the override) |
| 958 | if err := a.runtime.SetAgentModel(ctx, agentName, modelRef); err != nil { |
| 959 | if errors.Is(err, runtime.ErrUnsupported) { |
| 960 | return errors.New("model switching not supported by this runtime") |
| 961 | } |
| 962 | return err |
| 963 | } |
| 964 | |
| 965 | // Update the session's model overrides |
| 966 | if modelRef == "" { |
| 967 | // Clear the override - remove from map |
| 968 | delete(a.session.AgentModelOverrides, agentName) |
| 969 | slog.DebugContext(ctx, "Cleared model override from session", "session_id", a.session.ID, "agent", agentName) |
| 970 | } else { |
| 971 | // Set the override |
| 972 | if a.session.AgentModelOverrides == nil { |
| 973 | a.session.AgentModelOverrides = make(map[string]string) |
| 974 | } |
| 975 | a.session.AgentModelOverrides[agentName] = modelRef |
| 976 | slog.DebugContext(ctx, "Set model override in session", "session_id", a.session.ID, "agent", agentName, "model", modelRef) |
| 977 | |
| 978 | // Track custom models (inline provider/model format) in the session |
| 979 | if strings.Contains(modelRef, "/") { |
| 980 | a.trackCustomModel(modelRef) |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | // Persist the session |
| 985 | if store := a.runtime.SessionStore(); store != nil { |
| 986 | if err := store.UpdateSession(ctx, a.session); err != nil { |
| 987 | return fmt.Errorf("failed to persist model override: %w", err) |
| 988 | } |
| 989 | slog.DebugContext(ctx, "Persisted session with model override", "session_id", a.session.ID, "overrides", a.session.AgentModelOverrides) |
| 990 | } |
| 991 | |
| 992 | // Re-emit startup info so the sidebar updates with the new model |
| 993 | a.reEmitStartupInfo(ctx) |
| 994 | |
| 995 | return nil |
| 996 | } |
| 997 | |
| 998 | // CycleAgentThinkingLevel advances the current agent's thinking-effort level |
| 999 | // to the next value in its provider's cycle and returns the newly selected |
no test coverage detected