From be4813eb0b06af9cc72d5575e0dd5ff6bb50289b Mon Sep 17 00:00:00 2001 From: Donnie Adams Date: Sun, 22 Dec 2024 15:49:27 -0500 Subject: [PATCH] feat: add the ability to explicitly deconfigure a model provider Signed-off-by: Donnie Adams --- pkg/api/handlers/modelprovider.go | 20 ++++++++++++++++++++ pkg/api/router/router.go | 1 + 2 files changed, 21 insertions(+) diff --git a/pkg/api/handlers/modelprovider.go b/pkg/api/handlers/modelprovider.go index 11b7d065..5ba57850 100644 --- a/pkg/api/handlers/modelprovider.go +++ b/pkg/api/handlers/modelprovider.go @@ -138,6 +138,26 @@ func (mp *ModelProviderHandler) Configure(req api.Context) error { return req.Update(&ref) } +func (mp *ModelProviderHandler) Deconfigure(req api.Context) error { + var ref v1.ToolReference + if err := req.Get(&ref, req.PathValue("id")); err != nil { + return err + } + + if ref.Spec.Type != types.ToolReferenceTypeModelProvider { + return types.NewErrBadRequest("%q is not a model provider", ref.Name) + } + + if err := mp.gptscript.DeleteCredential(req.Context(), string(ref.UID), ref.Name); err != nil && !strings.HasSuffix(err.Error(), "credential not found") { + return fmt.Errorf("failed to update credential: %w", err) + } + + // Stop the model provider so that the credential is completely removed from the system. + mp.dispatcher.StopModelProvider(ref.Namespace, ref.Name) + + return nil +} + func (mp *ModelProviderHandler) Reveal(req api.Context) error { var ref v1.ToolReference if err := req.Get(&ref, req.PathValue("id")); err != nil { diff --git a/pkg/api/router/router.go b/pkg/api/router/router.go index 609c8d24..f74703a2 100644 --- a/pkg/api/router/router.go +++ b/pkg/api/router/router.go @@ -255,6 +255,7 @@ func Router(services *services.Services) (http.Handler, error) { mux.HandleFunc("GET /api/model-providers", modelProviders.List) mux.HandleFunc("GET /api/model-providers/{id}", modelProviders.ByID) mux.HandleFunc("POST /api/model-providers/{id}/configure", modelProviders.Configure) + mux.HandleFunc("POST /api/model-providers/{id}/deconfigure", modelProviders.Deconfigure) mux.HandleFunc("POST /api/model-providers/{id}/reveal", modelProviders.Reveal) mux.HandleFunc("POST /api/model-providers/{id}/refresh-models", modelProviders.RefreshModels)