@@ -72,11 +72,6 @@ func Save(LLMConfig LLMProvider) error {
7272
7373 cfg .Default = LLMConfig .LLM
7474
75- file , err := os .OpenFile (configPath , os .O_CREATE | os .O_WRONLY | os .O_TRUNC , 0600 )
76- if err != nil {
77- fmt .Println ("error opening file" )
78- }
79- defer file .Close ()
8075
8176 data , err = json .MarshalIndent (cfg , "" , " " )
8277 if err != nil {
@@ -149,7 +144,6 @@ func getConfigPath() (string, error) {
149144
150145}
151146
152-
153147func DefaultLLMKey () (* LLMProvider , error ) {
154148
155149 var cfg Config
@@ -192,4 +186,177 @@ func DefaultLLMKey() (*LLMProvider, error) {
192186 }
193187 }
194188 return nil , errors .New ("not found default model in config" )
189+ }
190+
191+
192+ func ListSavedModels () (* Config , error ){
193+
194+ var cfg Config
195+
196+ configPath , err := getConfigPath ()
197+ if err != nil {
198+ log .Fatal (err )
199+ }
200+
201+ isConfigExists := checkConfig (configPath )
202+ if ! isConfigExists {
203+ log .Fatal ("config file not exists" )
204+ }
205+
206+ data , err := os .ReadFile (configPath )
207+ if err != nil {
208+ fmt .Println ("Error" , err )
209+ log .Fatal (err )
210+ }
211+
212+ if len (data ) > 0 {
213+ err = json .Unmarshal (data , & cfg )
214+ if err != nil {
215+ fmt .Println ("unmarshal error" , err )
216+ log .Fatal (err )
217+ }
218+ } else {
219+ fmt .Println ("Config file is empty, Please add atlead one LLM Key" )
220+ return nil , errors .New ("config file is empty" )
221+ }
222+
223+
224+ return & cfg , nil
225+
226+ }
227+
228+
229+ func ChangeDefault (Model string ) error {
230+
231+ var cfg Config
232+
233+ configPath , err := getConfigPath ()
234+ if err != nil {
235+ return err
236+ }
237+
238+ isConfigExists := checkConfig (configPath )
239+ if ! isConfigExists {
240+ return errors .New ("config file not exists" )
241+ }
242+
243+ data , err := os .ReadFile (configPath )
244+ if err != nil {
245+ return err
246+ }
247+
248+ if len (data ) > 0 {
249+ err = json .Unmarshal (data , & cfg )
250+ if err != nil {
251+ return err
252+ }
253+ }
254+
255+ cfg .Default = Model
256+
257+ data , err = json .MarshalIndent (cfg , "" , " " )
258+ if err != nil {
259+ return err
260+ }
261+
262+ return os .WriteFile (configPath , data , 0600 )
263+ }
264+
265+
266+ func DeleteModel (Model string ) error {
267+
268+ var cfg Config
269+ var newCfg Config
270+
271+ configPath , err := getConfigPath ()
272+ if err != nil {
273+ return err
274+ }
275+
276+ isConfigExists := checkConfig (configPath )
277+ if ! isConfigExists {
278+ return errors .New ("config file not exists" )
279+ }
280+
281+ data , err := os .ReadFile (configPath )
282+ if err != nil {
283+ return err
284+ }
285+
286+ if len (data ) > 0 {
287+ err = json .Unmarshal (data , & cfg )
288+ if err != nil {
289+ return err
290+ }
291+ }
292+
293+
294+ if Model == cfg .Default {
295+ if len (cfg .LLMProviders ) > 1 {
296+ fmt .Println ("Please set other model as default, Cant delete default model" )
297+ return nil
298+ } else {
299+ return os .WriteFile (configPath , []byte ("{}" ), 0600 )
300+ }
301+ } else {
302+
303+ for _ ,p := range cfg .LLMProviders {
304+
305+ if p .LLM != Model {
306+ newCfg .LLMProviders = append (newCfg .LLMProviders , p )
307+ }
308+ }
309+
310+ newCfg .Default = cfg .Default
311+
312+ data , err = json .MarshalIndent (newCfg , "" , " " )
313+ if err != nil {
314+ return err
315+ }
316+ return os .WriteFile (configPath , data , 0600 )
317+
318+ }
319+ }
320+
321+
322+ func UpdateAPIKey (Model , APIKey string ) error {
323+
324+ var cfg Config
325+
326+
327+ configPath , err := getConfigPath ()
328+ if err != nil {
329+ return err
330+ }
331+
332+ isConfigExists := checkConfig (configPath )
333+ if ! isConfigExists {
334+ return errors .New ("config file not exists" )
335+ }
336+
337+ data , err := os .ReadFile (configPath )
338+ if err != nil {
339+ return err
340+ }
341+
342+ if len (data ) > 0 {
343+ err = json .Unmarshal (data , & cfg )
344+ if err != nil {
345+ return err
346+ }
347+ }
348+
349+ for i , p := range cfg .LLMProviders {
350+ if p .LLM == Model {
351+ cfg .LLMProviders [i ].APIKey = APIKey
352+ }
353+ }
354+
355+ data , err = json .MarshalIndent (cfg , "" , " " )
356+ if err != nil {
357+ return err
358+ }
359+
360+ return os .WriteFile (configPath , data , 0600 )
361+
195362}
0 commit comments