88 "strings"
99 "time"
1010
11+ "github.com/chat2anyllm/code-agent-manager/internal/appstate"
1112 "github.com/chat2anyllm/code-agent-manager/internal/pathutil"
1213 "github.com/chat2anyllm/code-agent-manager/internal/providers"
1314)
@@ -67,6 +68,7 @@ type ProviderPatch struct {
6768// ProviderAPI contains provider workflows shared by CLI and desktop adapters.
6869type ProviderAPI struct {
6970 ProvidersPath string
71+ DBPath string
7072 CacheTTL time.Duration
7173 Env func (string ) string
7274}
@@ -78,6 +80,20 @@ func (api ProviderAPI) path() string {
7880 return providers .DefaultPath ()
7981}
8082
83+ func (api ProviderAPI ) dbPath () string {
84+ if api .DBPath != "" {
85+ return api .DBPath
86+ }
87+ if api .ProvidersPath != "" {
88+ return api .ProvidersPath + ".db"
89+ }
90+ return appstate .DefaultPath ()
91+ }
92+
93+ func (api ProviderAPI ) store () appstate.Store {
94+ return appstate .New (api .dbPath ())
95+ }
96+
8197func (api ProviderAPI ) getenv () func (string ) string {
8298 if api .Env != nil {
8399 return api .Env
@@ -92,27 +108,30 @@ func (api ProviderAPI) cacheTTL() time.Duration {
92108 return time .Hour
93109}
94110
95- // Init creates an empty providers file if it does not already exist .
111+ // Init creates the SQLite app state database and imports providers.json when present .
96112func (api ProviderAPI ) Init (ctx context.Context ) (OperationResult , error ) {
97- _ = ctx
98- path := api .path ()
99- file , created , err := providers .LoadOrInit (path )
100- if err != nil {
113+ store := api .store ()
114+ if err := store .Init (ctx ); err != nil {
101115 return OperationResult {}, err
102116 }
103- if ! created {
104- return OperationResult {OK : true , Message : fmt .Sprintf ("providers.json already exists at %s" , path ), Path : path }, nil
105- }
106- if err := providers .Save (path , file ); err != nil {
117+ if err := store .ImportProvidersJSON (ctx , api .path ()); err != nil {
107118 return OperationResult {}, err
108119 }
109- return OperationResult {OK : true , Message : fmt .Sprintf ("Created empty providers.json at %s" , path ), Path : path }, nil
120+ return OperationResult {OK : true , Message : fmt .Sprintf ("SQLite app state ready at %s" , store .Path ()), Path : store .Path ()}, nil
121+ }
122+
123+ // File returns providers in legacy providers.File shape for adapters that still need existing helpers.
124+ func (api ProviderAPI ) File (ctx context.Context ) (providers.File , error ) {
125+ store := api .store ()
126+ if err := store .ImportProvidersJSON (ctx , api .path ()); err != nil {
127+ return providers.File {}, err
128+ }
129+ return store .ListProviders (ctx )
110130}
111131
112132// List returns all configured providers.
113133func (api ProviderAPI ) List (ctx context.Context ) ([]Provider , error ) {
114- _ = ctx
115- file , _ , err := providers .LoadOrInit (api .path ())
134+ file , err := api .File (ctx )
116135 if err != nil {
117136 return nil , err
118137 }
@@ -125,8 +144,11 @@ func (api ProviderAPI) List(ctx context.Context) ([]Provider, error) {
125144
126145// Show returns one provider by name.
127146func (api ProviderAPI ) Show (ctx context.Context , name string ) (Provider , error ) {
128- _ = ctx
129- file , _ , err := providers .LoadOrInit (api .path ())
147+ store := api .store ()
148+ if err := store .ImportProvidersJSON (ctx , api .path ()); err != nil {
149+ return Provider {}, err
150+ }
151+ file , err := store .ListProviders (ctx )
130152 if err != nil {
131153 return Provider {}, err
132154 }
@@ -139,30 +161,15 @@ func (api ProviderAPI) Show(ctx context.Context, name string) (Provider, error)
139161
140162// Add inserts a new provider.
141163func (api ProviderAPI ) Add (ctx context.Context , input ProviderInput ) (Provider , error ) {
142- _ = ctx
143- path := api .path ()
144- file , _ , err := providers .LoadOrInit (path )
145- if err != nil {
146- return Provider {}, err
147- }
148164 endpoint := endpointFromInput (input )
149- if err := providers .Add (& file , input .Name , endpoint ); err != nil {
150- return Provider {}, err
151- }
152- if err := providers .Save (path , file ); err != nil {
165+ if err := api .store ().AddProvider (ctx , input .Name , endpoint ); err != nil {
153166 return Provider {}, err
154167 }
155168 return providerFromEndpoint (input .Name , endpoint , api .getenv ()), nil
156169}
157170
158171// Update applies a sparse provider patch.
159172func (api ProviderAPI ) Update (ctx context.Context , name string , patch ProviderPatch ) (Provider , error ) {
160- _ = ctx
161- path := api .path ()
162- file , _ , err := providers .LoadOrInit (path )
163- if err != nil {
164- return Provider {}, err
165- }
166173 providerPatch := providers.Patch {
167174 Endpoint : patch .Endpoint ,
168175 APIKeyEnv : patch .APIKeyEnv ,
@@ -177,70 +184,58 @@ func (api ProviderAPI) Update(ctx context.Context, name string, patch ProviderPa
177184 if patch .SupportedClient != nil {
178185 providerPatch .Clients = & providers.ListPatch {Op : providers .ListOpReplace , Items : splitClients (* patch .SupportedClient )}
179186 }
180- if err := providers .Update (& file , name , providerPatch ); err != nil {
187+ store := api .store ()
188+ if err := store .UpdateProvider (ctx , name , providerPatch ); err != nil {
181189 return Provider {}, err
182190 }
183- if err := providers .Save (path , file ); err != nil {
191+ file , err := store .ListProviders (ctx )
192+ if err != nil {
184193 return Provider {}, err
185194 }
186195 return providerFromEndpoint (name , file .Endpoints [name ], api .getenv ()), nil
187196}
188197
189198// Remove deletes a provider.
190199func (api ProviderAPI ) Remove (ctx context.Context , name string ) (OperationResult , error ) {
191- _ = ctx
192- path := api .path ()
193- file , _ , err := providers .LoadOrInit (path )
194- if err != nil {
195- return OperationResult {}, err
196- }
197- if ! providers .Remove (& file , name ) {
200+ if ! api .store ().RemoveProvider (ctx , name ) {
198201 return OperationResult {}, fmt .Errorf ("provider %q not found: %w" , name , providers .ErrNotFound )
199202 }
200- if err := providers .Save (path , file ); err != nil {
201- return OperationResult {}, err
202- }
203- return OperationResult {OK : true , Message : fmt .Sprintf ("Removed provider %q" , name ), Path : path }, nil
203+ return OperationResult {OK : true , Message : fmt .Sprintf ("Removed provider %q" , name ), Path : api .dbPath ()}, nil
204204}
205205
206206// SetEnabled toggles a provider's enabled state.
207207func (api ProviderAPI ) SetEnabled (ctx context.Context , name string , enabled bool ) (Provider , error ) {
208- _ = ctx
209- path := api .path ()
210- file , _ , err := providers .LoadOrInit (path )
211- if err != nil {
208+ store := api .store ()
209+ if err := store .SetProviderEnabled (ctx , name , enabled ); err != nil {
212210 return Provider {}, err
213211 }
214- if err := providers .SetEnabled (& file , name , enabled ); err != nil {
215- return Provider {}, err
216- }
217- if err := providers .Save (path , file ); err != nil {
212+ file , err := store .ListProviders (ctx )
213+ if err != nil {
218214 return Provider {}, err
219215 }
220216 return providerFromEndpoint (name , file .Endpoints [name ], api .getenv ()), nil
221217}
222218
223219// Rename changes a provider key.
224220func (api ProviderAPI ) Rename (ctx context.Context , oldName , newName string ) (Provider , error ) {
225- _ = ctx
226- path := api .path ()
227- file , _ , err := providers .LoadOrInit (path )
228- if err != nil {
229- return Provider {}, err
230- }
231- if err := providers .Rename (& file , oldName , newName ); err != nil {
221+ store := api .store ()
222+ if err := store .RenameProvider (ctx , oldName , newName ); err != nil {
232223 return Provider {}, err
233224 }
234- if err := providers .Save (path , file ); err != nil {
225+ file , err := store .ListProviders (ctx )
226+ if err != nil {
235227 return Provider {}, err
236228 }
237229 return providerFromEndpoint (newName , file .Endpoints [newName ], api .getenv ()), nil
238230}
239231
240232// ResolveModels resolves static or dynamically discovered models for a provider.
241233func (api ProviderAPI ) ResolveModels (ctx context.Context , name string ) ([]string , error ) {
242- _ = ctx
243- file , _ , err := providers .LoadOrInit (api .path ())
234+ store := api .store ()
235+ if err := store .ImportProvidersJSON (ctx , api .path ()); err != nil {
236+ return nil , err
237+ }
238+ file , err := store .ListProviders (ctx )
244239 if err != nil {
245240 return nil , err
246241 }
@@ -250,7 +245,6 @@ func (api ProviderAPI) ResolveModels(ctx context.Context, name string) ([]string
250245 }
251246 return providers .ResolveModels (endpoint , name , api .cacheTTL (), filepath .Join (pathutil .CacheDir (), "models" ), api .getenv ())
252247}
253-
254248func providerFromEndpoint (name string , endpoint providers.Endpoint , getenv func (string ) string ) Provider {
255249 return Provider {
256250 Name : name ,
0 commit comments