返回 DeepSeek-Reasonix
model_settings.go
根目录 / desktop / model_settings.go
1 package main
2
3 import (
4 "errors"
5 "fmt"
6 "log/slog"
7 "strings"
8
9 "reasonix/internal/config"
10 "reasonix/internal/control"
11 "reasonix/internal/extension/providerext"
12 )
13
14 var errModelSettingsSuperseded = errors.New("model settings changed while building the session")
15
16 type tabModelApplicationState struct {
17 startupRetry bool
18 failure *modelSettingsApplyFailure
19 }
20
21 type modelSettingsApplyFailure struct {
22 controller control.SessionAPI
23 revision string
24 message string
25 }
26
27 type modelSettingsSnapshot interface {
28 ModelSettingsState() (applied, desired string, err error)
29 }
30
31 func modelSettingsNeedApply(ctrl control.SessionAPI) (bool, error) {
32 snapshot, ok := ctrl.(modelSettingsSnapshot)
33 if !ok {
34 return false, nil
35 }
36 applied, desired, err := snapshot.ModelSettingsState()
37 return applied != desired, err
38 }
39
40 // Model writes own only persisted configuration. Runtime admission applies the
41 // latest effective snapshot before accepting the next run, including when the
42 // write came from another process. Saving never requires a visible session.
43 func (a *App) applyModelConfigChange(mutate func(*config.Config) error) error {
44 _, err := a.applyModelConfigChangeWithWarning("model settings", mutate)
45 return err
46 }
47
48 func (a *App) applyModelConfigChangeWithWarning(setting string, mutate func(*config.Config) error) (string, error) {
49 return a.applyModelConfigChangeWithSave(setting, mutate, nil)
50 }
51
52 func (a *App) applyModelConfigChangeWithSave(setting string, mutate func(*config.Config) error, save func(*config.Config, string) error) (string, error) {
53 err := func() error {
54 unlock := config.LockUserConfigEdits()
55 defer unlock()
56 unlockCredentials, err := config.LockUserCredentialEdits()
57 if err != nil {
58 return err
59 }
60 defer unlockCredentials()
61 cfg, path, err := a.loadDesktopUserConfigForEdit()
62 if err != nil {
63 return err
64 }
65 baseline := cfg.ModelSettingsBaseline()
66 defer cfg.CleanupStagedModelCredentialsLocked(path)
67 if err := mutate(cfg); err != nil {
68 return err
69 }
70 if save == nil {
71 return cfg.SaveModelSettingsTo(path, baseline)
72 }
73 return save(cfg, path)
74 }()
75 if err != nil {
76 return "", err
77 }
78 a.modelSettingsSaved(setting)
79 return "", nil
80 }
81
82 func (a *App) modelSettingsSaved(setting string) {
83 a.invalidateAuxiliaryProviderOperations()
84 a.mu.RLock()
85 count, active := len(a.tabs), a.activeTabID != ""
86 var retry []*WorkspaceTab
87 for _, tab := range a.tabs {
88 if tab != nil && tab.Ctrl == nil && tab.modelApplication.startupRetry {
89 retry = append(retry, tab)
90 }
91 }
92 a.mu.RUnlock()
93 slog.Debug("model settings persisted", "setting", setting, "visibleSessions", count, "hasActiveSession", active)
94 a.refreshActiveTabMetaExtras()
95 if a.ctx != nil {
96 for _, tab := range retry {
97 a.scheduleDeferredStartupBuild(tab.ID)
98 }
99 }
100 }
101
102 // refreshTabModelSettings runs outside the turn admission read lock. The
103 // existing rebuild owner protects build/swap; no second scheduling authority.
104 func (a *App) refreshTabModelSettings(tab *WorkspaceTab) error {
105 a.runtimeRebuildMu.Lock()
106 defer a.runtimeRebuildMu.Unlock()
107 tab.turnStartMu.Lock()
108 defer tab.turnStartMu.Unlock()
109 for {
110 if err := a.reqCtx().Err(); err != nil {
111 return err
112 }
113 current := a.controllerForTab(tab)
114 snapshot, ok := current.(modelSettingsSnapshot)
115 if !ok {
116 return nil
117 }
118 applied, attempted, err := snapshot.ModelSettingsState()
119 if err != nil {
120 return fmt.Errorf("read saved model settings: %w", err)
121 }
122 if applied == attempted {
123 return nil
124 }
125 if err := a.rebuildSettingTurnLocked("saved model settings", tab, false, true); err != nil {
126 if errors.Is(err, errModelSettingsSuperseded) {
127 continue
128 }
129 if current != nil {
130 a.mu.Lock()
131 if a.ownsRuntimeTabLocked(tab) && tab.Ctrl == current {
132 tab.modelApplication.failure = &modelSettingsApplyFailure{current, attempted, modelSettingsIssue("apply_failed", err).Message}
133 }
134 a.mu.Unlock()
135 }
136 return fmt.Errorf("model settings were saved but this session could not apply them: %w", err)
137 }
138 return nil
139 }
140 }
141
142 // Preserve a selected model while it is still accessible; a removed selection
143 // may only fall back to an explicitly available and configured Desktop model.
144 func resolveModelSettingsRuntime(c *config.Config, model string) (string, error) {
145 if providerext.PluginRefOwner(model) != "" {
146 return model, nil
147 }
148 if p, ok := c.ResolveModel(model); ok && modelProviderAccessAllowed(c.Desktop.ProviderAccess, p.Name) && p.Configured() {
149 return p.Name + "/" + p.Model, nil
150 }
151 if ref := resolveNewSessionModel(c); strings.TrimSpace(ref) != "" {
152 if p, ok := c.ResolveModel(ref); ok && modelProviderAccessAllowed(c.Desktop.ProviderAccess, p.Name) && p.Configured() {
153 return p.Name + "/" + p.Model, nil
154 }
155 }
156 return "", fmt.Errorf("no configured model is available; choose a model in Settings before starting another run")
157 }
158
159 func (a *App) sessionPathForSettingsRebuild(tab *WorkspaceTab) string {
160 if path := a.reconciledSessionPathForTab(tab); path != "" {
161 return path
162 }
163 return a.currentSessionPathFor(tab)
164 }
165
166 func validateModelSettingsReplacement(ctrl, previous control.SessionAPI) error {
167 if stale, err := modelSettingsNeedApply(ctrl); err != nil || stale {
168 if ctrl != previous {
169 discardReplacementController(ctrl, previous)
170 }
171 if err != nil {
172 return err
173 }
174 return errModelSettingsSuperseded
175 }
176 return nil
177 }
178
178 lines GO