返回 DeepSeek-Reasonix
effort.go
根目录 / internal / serve / effort.go
1 package serve
2
3 import (
4 "context"
5 "fmt"
6 "reasonix/internal/config"
7 )
8
9 func (s *Server) switchEffortExpected(ctx context.Context, level, expectedPath string) error {
10 s.bindMu.Lock()
11 defer s.bindMu.Unlock()
12 if err := s.expectedSessionPathErrorLocked(expectedPath); err != nil {
13 return err
14 }
15 cur := s.ctl()
16 if controllerHasActiveRuntimeWork(cur) {
17 return fmt.Errorf("cannot change effort while active work or background jobs are running")
18 }
19 cfg, err := config.Load()
20 if err != nil {
21 return fmt.Errorf("load config: %w", err)
22 }
23 if s.managedModels != nil {
24 if err := s.managedModels.Apply(cfg, cur.WorkspaceRoot()); err != nil {
25 return err
26 }
27 }
28 ref := currentModelRef(cur)
29 entry, ok := cfg.ResolveModel(ref)
30 if !ok {
31 return fmt.Errorf("cannot resolve current provider %q", ref)
32 }
33 if !config.EffortCapabilityForEntry(entry).Supported && level != "auto" {
34 return fmt.Errorf("effort is not configurable for %s", entry.Name)
35 }
36 effort, err := config.NormalizeEffort(entry, level)
37 if err != nil {
38 return err
39 }
40 if s.managedModels != nil {
41 // Managed providers are transient tunnel identities. Keep an explicit
42 // session effort override in memory instead of persisting virtual keys.
43 previous := s.buildOptions.EffortOverride
44 s.buildOptions.EffortOverride = &effort
45 if err := s.switchModelLocked(ctx, ref); err != nil {
46 s.buildOptions.EffortOverride = previous
47 return err
48 }
49 return nil
50 }
51 editPath := config.UserConfigPath()
52 if editPath == "" {
53 return fmt.Errorf("no config file found")
54 }
55 // Lock only the load-modify-save cycle; switchModel below rebuilds the
56 // controller and must not hold the config edit lock.
57 if err := func() error {
58 unlock := config.LockUserConfigEdits()
59 defer unlock()
60 edit := config.LoadForEdit(editPath)
61 if err := applyEffortEdit(edit, entry, effort); err != nil {
62 return err
63 }
64 if err := edit.SaveTo(editPath); err != nil {
65 return fmt.Errorf("save config: %w", err)
66 }
67 return nil
68 }(); err != nil {
69 return err
70 }
71 previous := s.buildOptions.EffortOverride
72 s.buildOptions.EffortOverride = &effort
73 if err := s.switchModelLocked(ctx, entry.Name+"/"+entry.Model); err != nil {
74 s.buildOptions.EffortOverride = previous
75 return err
76 }
77 return nil
78 }
79
79 lines GO