-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_aiconfig.go
More file actions
340 lines (312 loc) · 9.77 KB
/
Copy pathapp_aiconfig.go
File metadata and controls
340 lines (312 loc) · 9.77 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package main
import (
"context"
"errors"
"strings"
"futrixdata/platform/internal/aiconfig"
)
// --- Embedding config bindings ---
func (a *App) ListEmbeddingConfigs() ([]aiconfig.AIConfig, error) {
configs := a.aiConfigStore.ListByPurpose(aiconfig.PurposeEmbedding)
masked := make([]aiconfig.AIConfig, len(configs))
for i, cfg := range configs {
masked[i] = maskAIKey(cfg)
}
return masked, nil
}
func (a *App) CreateEmbeddingConfig(payload AIConfigPayload) (aiconfig.AIConfig, error) {
if strings.TrimSpace(payload.Name) == "" {
return aiconfig.AIConfig{}, errors.New("name is required")
}
if strings.TrimSpace(payload.Model) == "" {
return aiconfig.AIConfig{}, errors.New("model is required")
}
cfg := payload.toAIConfig("")
cfg.Purpose = aiconfig.PurposeEmbedding
created, err := a.aiConfigStore.Create(cfg)
if err != nil {
return aiconfig.AIConfig{}, err
}
return maskAIKey(created), nil
}
func (a *App) UpdateEmbeddingConfig(id string, payload AIConfigPayload) (aiconfig.AIConfig, error) {
existing, ok := a.aiConfigStore.Get(id)
if !ok {
return aiconfig.AIConfig{}, errors.New("configuration not found")
}
if strings.TrimSpace(payload.APIKey) == "" || isMaskedAIKey(payload.APIKey, existing.APIKey) {
payload.APIKey = ""
nextProvider := existing.Provider
if strings.TrimSpace(string(payload.Provider)) != "" {
nextProvider = payload.Provider
}
nextBaseURL := existing.BaseURL
if strings.TrimSpace(payload.BaseURL) != "" {
nextBaseURL = strings.TrimSpace(payload.BaseURL)
}
if nextProvider == existing.Provider && strings.TrimSpace(nextBaseURL) == strings.TrimSpace(existing.BaseURL) {
payload.APIKey = existing.APIKey
}
}
cfg := payload.toAIConfig(id)
cfg.Purpose = aiconfig.PurposeEmbedding
updated, err := a.aiConfigStore.Update(id, cfg)
if err != nil {
return aiconfig.AIConfig{}, err
}
return maskAIKey(updated), nil
}
func (a *App) DeleteEmbeddingConfig(id string) (bool, error) {
return a.DeleteAIConfig(id)
}
func (a *App) ListEmbeddingProviders() (map[string]aiconfig.ProviderInfo, error) {
providers := make(map[string]aiconfig.ProviderInfo)
for key, value := range aiconfig.EmbeddingProviderDefaults {
providers[string(key)] = value
}
return providers, nil
}
func (a *App) TestEmbeddingConfig(id string) (aiconfig.TestResult, error) {
cfg, ok := a.aiConfigStore.Get(id)
if !ok {
return aiconfig.TestResult{}, errors.New("configuration not found")
}
result := aiconfig.TestEmbeddingConnection(context.Background(), cfg)
if !result.Connected {
msg := strings.TrimSpace(result.Error)
if msg == "" {
msg = "connection failed"
}
return result, errors.New(msg)
}
_, _ = a.aiConfigStore.UpdateStatus(id, result)
return result, nil
}
func (a *App) TestEmbeddingConfigPayload(payload AIConfigPayload) (aiconfig.TestResult, error) {
if strings.TrimSpace(payload.Model) == "" {
return aiconfig.TestResult{}, errors.New("model is required")
}
cfg := payload.toAIConfig("")
cfg.Purpose = aiconfig.PurposeEmbedding
result := aiconfig.TestEmbeddingConnection(context.Background(), cfg)
if !result.Connected {
msg := strings.TrimSpace(result.Error)
if msg == "" {
msg = "connection failed"
}
return result, errors.New(msg)
}
return result, nil
}
// ComputeEmbeddingForSearch converts text to a vector using a configured
// embedding provider. Called by the ChromaDB console for text-based search.
// The dimensions parameter controls the output vector size (0 = model default).
func (a *App) ComputeEmbeddingForSearch(embeddingConfigID, text string, dimensions int) ([]float64, error) {
cfg, ok := a.aiConfigStore.Get(embeddingConfigID)
if !ok {
return nil, errors.New("embedding configuration not found")
}
return aiconfig.ComputeEmbedding(context.Background(), cfg, text, dimensions)
}
type AIConfigPayload struct {
Name string `json:"name"`
Provider aiconfig.ProviderType `json:"provider"`
BaseURL string `json:"baseUrl"`
APIKey string `json:"apiKey"`
Model string `json:"model"`
Options map[string]any `json:"options"`
}
func (p AIConfigPayload) toAIConfig(id string) aiconfig.AIConfig {
return aiconfig.AIConfig{
ID: id,
Name: strings.TrimSpace(p.Name),
Provider: p.Provider,
BaseURL: strings.TrimSpace(p.BaseURL),
APIKey: p.APIKey,
Model: strings.TrimSpace(p.Model),
Options: p.Options,
}
}
func validateAIConfigPayload(p AIConfigPayload) error {
if strings.TrimSpace(p.Name) == "" {
return errors.New("name is required")
}
if p.Provider == "" {
return errors.New("provider is required")
}
switch p.Provider {
case aiconfig.ProviderOpenAI, aiconfig.ProviderAnthropic, aiconfig.ProviderGemini,
aiconfig.ProviderQwen, aiconfig.ProviderZhipu, aiconfig.ProviderDeepSeek,
aiconfig.ProviderOpenRouter, aiconfig.ProviderOllama, aiconfig.ProviderLMStudio,
aiconfig.ProviderCustom:
default:
return errors.New("unsupported provider")
}
if strings.TrimSpace(p.APIKey) == "" {
return errors.New("apiKey is required")
}
if p.Provider == aiconfig.ProviderCustom && strings.TrimSpace(p.BaseURL) == "" {
return errors.New("baseUrl is required for custom provider")
}
return nil
}
func validateAITestPayload(p AIConfigPayload) error {
if err := validateAIConfigPayload(p); err != nil {
return err
}
if strings.TrimSpace(p.Model) == "" {
return errors.New("model is required")
}
return nil
}
func maskAIKey(cfg aiconfig.AIConfig) aiconfig.AIConfig {
if len(cfg.APIKey) > 8 {
cfg.APIKey = cfg.APIKey[:4] + "***" + cfg.APIKey[len(cfg.APIKey)-4:]
} else if len(cfg.APIKey) > 0 {
cfg.APIKey = "***"
}
return cfg
}
func maskAIKeyString(apiKey string) string {
if len(apiKey) > 8 {
return apiKey[:4] + "***" + apiKey[len(apiKey)-4:]
}
if len(apiKey) > 0 {
return "***"
}
return ""
}
func isMaskedAIKey(apiKey, existingKey string) bool {
trimmed := strings.TrimSpace(apiKey)
if trimmed == "" {
return false
}
if strings.HasPrefix(trimmed, "***") {
return true
}
return trimmed == maskAIKeyString(existingKey)
}
func (a *App) ListAIConfigs() ([]aiconfig.AIConfig, error) {
configs := a.aiConfigStore.ListByPurpose(aiconfig.PurposeChat)
masked := make([]aiconfig.AIConfig, len(configs))
for i, cfg := range configs {
masked[i] = maskAIKey(cfg)
}
return masked, nil
}
func (a *App) CreateAIConfig(payload AIConfigPayload) (aiconfig.AIConfig, error) {
if err := validateAIConfigPayload(payload); err != nil {
return aiconfig.AIConfig{}, err
}
created, err := a.aiConfigStore.Create(payload.toAIConfig(""))
if err != nil {
return aiconfig.AIConfig{}, err
}
return maskAIKey(created), nil
}
func (a *App) UpdateAIConfig(id string, payload AIConfigPayload) (aiconfig.AIConfig, error) {
existing, ok := a.aiConfigStore.Get(id)
if !ok {
return aiconfig.AIConfig{}, errors.New("configuration not found")
}
if strings.TrimSpace(string(payload.Provider)) == "" {
payload.Provider = existing.Provider
}
if strings.TrimSpace(payload.APIKey) == "" || isMaskedAIKey(payload.APIKey, existing.APIKey) {
if payload.Provider == existing.Provider {
payload.APIKey = existing.APIKey
}
}
if err := validateAIConfigPayload(payload); err != nil {
return aiconfig.AIConfig{}, err
}
updated, err := a.aiConfigStore.Update(id, payload.toAIConfig(id))
if err != nil {
return aiconfig.AIConfig{}, err
}
return maskAIKey(updated), nil
}
func (a *App) DeleteAIConfig(id string) (bool, error) {
if err := a.aiConfigStore.Delete(id); err != nil {
return false, err
}
return true, nil
}
func (a *App) GetAIConfigAPIKey(id string) (string, error) {
cfg, ok := a.aiConfigStore.Get(id)
if !ok {
return "", errors.New("configuration not found")
}
return cfg.APIKey, nil
}
func (a *App) ListAIProviders() (map[string]aiconfig.ProviderInfo, error) {
providers := make(map[string]aiconfig.ProviderInfo)
for key, value := range aiconfig.ProviderDefaults {
providers[string(key)] = value
}
return providers, nil
}
func (a *App) TestAIConfig(id string) (aiconfig.TestResult, error) {
cfg, ok := a.aiConfigStore.Get(id)
if !ok {
return aiconfig.TestResult{}, errors.New("configuration not found")
}
result := aiconfig.TestConnection(context.Background(), cfg)
if !result.Connected {
message := strings.TrimSpace(result.Error)
if message == "" {
message = "connection failed"
}
return result, errors.New(message)
}
updated, err := a.aiConfigStore.UpdateStatus(id, result)
if err == nil {
_, _ = a.store.AssignAIConfigIfUnset(updated.ID)
}
return result, nil
}
func (a *App) TestAIConfigPayload(payload AIConfigPayload) (aiconfig.TestResult, error) {
if err := validateAITestPayload(payload); err != nil {
return aiconfig.TestResult{}, err
}
result := aiconfig.TestConnection(context.Background(), payload.toAIConfig(""))
if !result.Connected {
message := strings.TrimSpace(result.Error)
if message == "" {
message = "connection failed"
}
return result, errors.New(message)
}
return result, nil
}
func (a *App) TestAIConfigPreview(id string, payload AIConfigPayload) (aiconfig.TestResult, error) {
cfg, ok := a.aiConfigStore.Get(id)
if !ok {
return aiconfig.TestResult{}, errors.New("configuration not found")
}
if payload.Provider != "" {
cfg.Provider = payload.Provider
if strings.TrimSpace(payload.BaseURL) == "" {
cfg.BaseURL = ""
}
}
if strings.TrimSpace(payload.BaseURL) != "" {
cfg.BaseURL = strings.TrimSpace(payload.BaseURL)
}
if strings.TrimSpace(payload.Model) != "" {
cfg.Model = strings.TrimSpace(payload.Model)
}
apiKey := strings.TrimSpace(payload.APIKey)
if apiKey != "" && !isMaskedAIKey(apiKey, cfg.APIKey) {
cfg.APIKey = payload.APIKey
}
result := aiconfig.TestConnection(context.Background(), cfg)
if !result.Connected {
message := strings.TrimSpace(result.Error)
if message == "" {
message = "connection failed"
}
return result, errors.New(message)
}
return result, nil
}