-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection.go
More file actions
242 lines (227 loc) · 8.54 KB
/
Copy pathselection.go
File metadata and controls
242 lines (227 loc) · 8.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package engine
import (
"context"
"fmt"
"strings"
"github.com/GrayCodeAI/eyrie/catalog"
"github.com/GrayCodeAI/eyrie/config"
"github.com/GrayCodeAI/eyrie/runtime"
)
// NormalizeProviderID resolves provider aliases to Eyrie's runtime identifier.
func NormalizeProviderID(providerID string) string {
return runtime.NormalizeProviderID(providerID)
}
// EffectiveSelection resolves persisted state plus optional host overrides
// using this Engine's injected catalog, provider config, and credential store.
func (e *Engine) EffectiveSelection(ctx context.Context, opts SelectionOptions) Selection {
ctx = nonNilContext(ctx)
active := e.ActiveSelection(ctx)
provider := NormalizeProviderID(active.Provider)
model := strings.TrimSpace(active.Model)
hasProviderOverride := false
if override := NormalizeProviderID(opts.ProviderOverride); override != "" {
if override != provider && strings.TrimSpace(opts.ModelOverride) == "" {
model = ""
}
provider = override
hasProviderOverride = true
}
if override := strings.TrimSpace(opts.ModelOverride); override != "" {
model = override
}
compiled, _ := catalog.LoadCatalog(ctx, catalog.LoadCatalogOptions{CachePath: e.catalogPath})
if provider == "" && model != "" && compiled != nil {
provider = NormalizeProviderID(catalog.GatewayForModel(compiled, model))
if provider == "" {
provider = NormalizeProviderID(catalog.ProviderForModel(compiled, model))
}
}
gateways := e.Gateways(ctx)
hasConfigured := false
configured := make(map[string]bool, len(gateways))
for _, gateway := range gateways {
ready := gateway.DeploymentConfigured
configured[NormalizeProviderID(gateway.ID)] = ready
if ready {
hasConfigured = true
}
}
if provider == "" && hasConfigured {
provider = preferredConfiguredGateway(gateways, configured)
}
if provider != "" && hasConfigured && !configured[provider] && !hasProviderOverride {
provider = preferredConfiguredGateway(gateways, configured)
model = ""
}
if provider != "" && model == "" {
if models, err := e.ListModels(ctx, provider, false); err == nil && len(models) > 0 {
model = models[0].ID
}
}
if _, custom := e.customGateway(provider); !custom && compiled != nil && model != "" {
if canonical, ok := catalog.CanonicalModelForProviderNative(compiled, provider, model); ok {
model = canonical
} else if canonical, ok := compiled.CanonicalModelForAliasOrID(model); ok {
model = canonical
}
}
routing := active.DeploymentRouting
if opts.DeploymentRoutingOverride != nil {
routing = *opts.DeploymentRoutingOverride
}
return Selection{
Provider: provider, Model: model,
HasConfiguredDeployment: hasConfigured,
DeploymentRouting: routing,
}
}
func preferredConfiguredGateway(gateways []Gateway, configured map[string]bool) string {
bestID, bestRank := "", int(^uint(0)>>1)
for _, gateway := range gateways {
providerID := NormalizeProviderID(gateway.ID)
if !configured[providerID] {
continue
}
rank := gateway.ChatPreference
if rank <= 0 {
rank = gateway.SortOrder + 10_000
}
if rank < bestRank || (rank == bestRank && (bestID == "" || providerID < bestID)) {
bestID, bestRank = providerID, rank
}
}
return bestID
}
// ActiveSelection reads the persisted host selection from this Engine's state.
func (e *Engine) ActiveSelection(ctx context.Context) Route {
_ = nonNilContext(ctx)
cfg := config.LoadProviderConfig(e.providerConfigPath)
return Route{
Provider: NormalizeProviderID(config.ActiveProvider(cfg)),
Model: config.ActiveModel(cfg),
DeploymentRouting: cfg != nil && (cfg.ConfigVersion >= 2 || len(cfg.Deployments) > 0 || cfg.Routing != nil),
}
}
// SetActiveProvider persists a provider choice without requiring a model.
func (e *Engine) SetActiveProvider(ctx context.Context, providerID string) error {
ctx = nonNilContext(ctx)
providerID = NormalizeProviderID(providerID)
if providerID == "" {
return invalid("set_active_provider", "eyrie engine: provider id is required")
}
unlock := lockProviderStatePath(e.providerConfigPath)
defer unlock()
cfg, err := e.loadProviderConfigStrict()
if err != nil {
return &Error{Code: ErrorInternal, Operation: "set_active_provider", Provider: providerID, Message: err.Error(), Cause: err}
}
config.SetActiveProvider(cfg, providerID)
if err := e.saveProviderConfig(ctx, cfg); err != nil {
return &Error{Code: ErrorInternal, Operation: "set_active_provider", Provider: providerID, Message: err.Error(), Cause: err}
}
return nil
}
// SetActiveModel persists a model choice and infers its serving provider from
// the configured catalog.
func (e *Engine) SetActiveModel(ctx context.Context, modelID string) error {
return e.SetSelection(ctx, "", modelID)
}
// SetSelection persists the host/user's provider and model choice in this
// Engine's configured state path. It does not persist credentials.
func (e *Engine) SetSelection(ctx context.Context, providerID, modelID string) error {
ctx = nonNilContext(ctx)
providerID = NormalizeProviderID(providerID)
modelID = strings.TrimSpace(modelID)
if modelID == "" {
return invalid("set_selection", "eyrie engine: model id is required")
}
unlock := lockProviderStatePath(e.providerConfigPath)
defer unlock()
cfg, err := e.loadProviderConfigStrict()
if err != nil {
return &Error{Code: ErrorInternal, Operation: "set_selection", Provider: providerID, Model: modelID, Message: err.Error(), Cause: err}
}
activeProvider := NormalizeProviderID(config.ActiveProvider(cfg))
_, custom := e.customGateway(providerID)
if providerID == "" {
if gateway, ok := e.customGatewayForModel(modelID); ok {
providerID, custom = gateway.ID, true
} else if _, ok := e.customGateway(activeProvider); ok {
providerID, custom = activeProvider, true
}
}
if !custom {
compiled, catalogErr := catalog.LoadCatalog(ctx, catalog.LoadCatalogOptions{CachePath: e.catalogPath, RequireCache: true})
if catalogErr != nil {
return &Error{Code: ErrorCatalogUnavailable, Operation: "set_selection", Provider: providerID, Model: modelID, Message: catalogErr.Error(), Cause: catalogErr}
}
if providerID != "" {
canonical, ok := canonicalSelectionModel(compiled, providerID, modelID)
if !ok {
return selectionModelUnavailable(providerID, modelID)
}
modelID = canonical
} else {
if activeProvider != "" {
if canonical, ok := canonicalSelectionModel(compiled, activeProvider, modelID); ok {
providerID, modelID = activeProvider, canonical
}
}
if providerID == "" {
canonical, ok := compiled.CanonicalModelForAliasOrID(modelID)
if !ok {
return selectionModelUnavailable("", modelID)
}
modelID = canonical
providerID = NormalizeProviderID(catalog.GatewayForModel(compiled, modelID))
if providerID == "" {
providerID = NormalizeProviderID(catalog.ProviderForModel(compiled, modelID))
}
if providerID == "" {
return selectionModelUnavailable("", modelID)
}
}
}
}
config.SetProviderModel(cfg, providerID, modelID)
if err := e.saveProviderConfig(ctx, cfg); err != nil {
return &Error{Code: ErrorInternal, Operation: "set_selection", Provider: providerID, Model: modelID, Message: err.Error(), Cause: err}
}
return nil
}
func canonicalSelectionModel(compiled *catalog.CompiledCatalog, providerID, modelID string) (string, bool) {
if canonical, ok := catalog.CanonicalModelForProviderNative(compiled, providerID, modelID); ok {
return canonical, true
}
canonical, ok := compiled.CanonicalModelForAliasOrID(modelID)
if !ok {
return "", false
}
if !providerModelAvailable(compiled, providerID, canonical) {
return "", false
}
return canonical, true
}
func selectionModelUnavailable(providerID, modelID string) error {
message := fmt.Sprintf("eyrie engine: model %q is not available", modelID)
if providerID != "" {
message = fmt.Sprintf("eyrie engine: model %q is not available through %q", modelID, providerID)
}
return &Error{Code: ErrorModelUnavailable, Operation: "set_selection", Provider: providerID, Model: modelID, Message: message}
}
// ClearSelection removes active provider/model state without modifying
// credentials, deployments, or routing.
func (e *Engine) ClearSelection(ctx context.Context) error {
ctx = nonNilContext(ctx)
unlock := lockProviderStatePath(e.providerConfigPath)
defer unlock()
cfg, err := e.loadProviderConfigStrict()
if err != nil {
return &Error{Code: ErrorInternal, Operation: "clear_selection", Message: err.Error(), Cause: err}
}
config.ClearActiveSelection(cfg)
if err := e.saveProviderConfig(ctx, cfg); err != nil {
return &Error{Code: ErrorInternal, Operation: "clear_selection", Message: err.Error(), Cause: err}
}
return nil
}