-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.go
More file actions
248 lines (208 loc) · 7 KB
/
update.go
File metadata and controls
248 lines (208 loc) · 7 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
package starmap
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"slices"
"time"
"github.com/agentstation/starmap/internal/sources/local"
"github.com/agentstation/starmap/internal/sources/modelsdev"
"github.com/agentstation/starmap/internal/sources/providers"
"github.com/agentstation/starmap/pkg/catalogs"
"github.com/agentstation/starmap/pkg/errors"
"github.com/agentstation/starmap/pkg/logging"
"github.com/agentstation/starmap/pkg/sources"
"github.com/agentstation/starmap/pkg/sync"
)
// Updater handles catalog synchronization operations.
type Updater interface {
// Sync synchronizes the catalog with provider APIs
Sync(ctx context.Context, opts ...sync.Option) (*sync.Result, error)
// Update manually triggers a catalog update
Update(ctx context.Context) error
}
// Compile-time interface check to ensure proper implementation.
var _ Updater = (*client)(nil)
// Update manually triggers a catalog update.
func (c *client) Update(ctx context.Context) error {
if c.options.remoteServerURL != nil {
return c.updateFromServer(ctx)
}
if c.options.autoUpdateFunc != nil {
c.mu.RLock()
currentCatalog := c.catalog
c.mu.RUnlock()
newCatalog, err := c.options.autoUpdateFunc(currentCatalog)
if err != nil {
return err
}
c.setCatalog(newCatalog)
} else {
// Use pipeline-based update as default
return c.updateWithPipeline(ctx)
}
return nil
}
// updateWithPipeline performs a pipeline-based update for all providers.
func (c *client) updateWithPipeline(ctx context.Context) error {
// Use default options for auto-updates
opts := []sync.Option{
sync.WithDryRun(false),
sync.WithAutoApprove(true),
}
// Perform a sync operation with default options
_, err := c.Sync(ctx, opts...)
return err
}
// updateFromServer fetches catalog updates from the remote server.
func (c *client) updateFromServer(ctx context.Context) error {
if c.options.remoteServerURL == nil {
return &errors.ConfigError{
Component: "starmap",
Message: "remote server URL is not set",
}
}
logger := logging.FromContext(ctx)
logger.Debug().
Str("url", *c.options.remoteServerURL).
Msg("Fetching catalog from remote server")
req, err := http.NewRequestWithContext(ctx, "GET", *c.options.remoteServerURL+"/catalog", nil)
if err != nil {
return errors.WrapResource("create", "request", "", err)
}
if c.options.remoteServerAPIKey != nil {
req.Header.Set("Authorization", "Bearer "+*c.options.remoteServerAPIKey)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return &errors.APIError{
Provider: "starmap-server",
Endpoint: *c.options.remoteServerURL,
Message: "failed to make request",
Err: err,
}
}
defer func() {
// Drain and close body to allow connection reuse
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
}()
// Check response status
if resp.StatusCode != http.StatusOK {
logger.Error().
Int("status_code", resp.StatusCode).
Str("url", *c.options.remoteServerURL).
Msg("Remote server returned error status")
return &errors.APIError{
Provider: "starmap-server",
Endpoint: *c.options.remoteServerURL,
StatusCode: resp.StatusCode,
Message: fmt.Sprintf("server returned status %d", resp.StatusCode),
}
}
logger.Trace().Msg("Parsing catalog response")
// Read response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return errors.WrapIO("read", "response body", err)
}
// Parse remote catalog response
type RemoteCatalogResponse struct {
Version string `json:"version"`
Catalog json.RawMessage `json:"catalog"`
Checksum string `json:"checksum,omitempty"`
Timestamp time.Time `json:"timestamp"`
}
var response RemoteCatalogResponse
if err := json.Unmarshal(body, &response); err != nil {
return errors.WrapParse("json", "remote catalog response", err)
}
// Create a new memory catalog and populate it
newCatalog := catalogs.NewEmpty()
// Parse catalog data structure
type CatalogData struct {
Providers []catalogs.Provider `json:"providers,omitempty"`
Authors []catalogs.Author `json:"authors,omitempty"`
Models []catalogs.Model `json:"models,omitempty"`
Endpoints []catalogs.Endpoint `json:"endpoints,omitempty"`
}
var catalogData CatalogData
if err := json.Unmarshal(response.Catalog, &catalogData); err != nil {
return errors.WrapParse("json", "catalog data", err)
}
// Populate the catalog
for _, provider := range catalogData.Providers {
if err := newCatalog.SetProvider(provider); err != nil {
logger.Warn().Err(err).Str("provider", string(provider.ID)).Msg("Failed to set provider")
}
}
for _, author := range catalogData.Authors {
if err := newCatalog.SetAuthor(author); err != nil {
logger.Warn().Err(err).Str("author", string(author.ID)).Msg("Failed to set author")
}
}
// Models are now associated with providers and authors, not set directly
// They should already be included in the provider/author data structures
for _, endpoint := range catalogData.Endpoints {
if err := newCatalog.SetEndpoint(endpoint); err != nil {
logger.Warn().Err(err).Str("endpoint", endpoint.ID).Msg("Failed to set endpoint")
}
}
// Update the catalog
c.setCatalog(newCatalog)
logger.Info().
Str("version", response.Version).
Time("timestamp", response.Timestamp).
Int("providers", len(catalogData.Providers)).
Int("models", len(catalogData.Models)).
Msg("Successfully updated catalog from remote server")
return nil
}
// setCatalog updates the catalog and triggers appropriate event hooks.
func (c *client) setCatalog(newCatalog catalogs.Catalog) {
c.mu.Lock()
oldCatalog := c.catalog
c.catalog = newCatalog
c.mu.Unlock()
// Trigger hooks for catalog changes
c.hooks.triggerUpdate(oldCatalog, newCatalog)
}
// Sources returns the sources to use based on configuration.
func (c *client) filterSources(options *sync.Options, localCatalog catalogs.Catalog) []sources.Source {
// Create sources with configuration (especially SourcesDir)
configuredSources := createSourcesWithConfig(options, localCatalog)
// If specific sources are requested, filter to those
if len(options.Sources) > 0 {
var filtered []sources.Source
for _, src := range configuredSources {
if slices.Contains(options.Sources, src.ID()) {
filtered = append(filtered, src)
}
}
return filtered
}
// Otherwise return all configured sources
return configuredSources
}
// createSourcesWithConfig creates sources configured with sync options.
func createSourcesWithConfig(options *sync.Options, localCatalog catalogs.Catalog) []sources.Source {
sources := []sources.Source{
local.New(local.WithCatalog(localCatalog)),
providers.New(localCatalog.Providers()),
}
// Configure models.dev sources if SourcesDir is specified
if options.SourcesDir != "" {
sources = append(sources,
modelsdev.NewGitSource(modelsdev.WithSourcesDir(options.SourcesDir)),
modelsdev.NewHTTPSource(modelsdev.WithHTTPSourcesDir(options.SourcesDir)),
)
} else {
sources = append(sources,
modelsdev.NewGitSource(),
modelsdev.NewHTTPSource(),
)
}
return sources
}