From 211e523759ab286582701c1d8cad783dc53fe61e Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 21 Mar 2025 19:57:46 +0800 Subject: [PATCH] Implement built-in assistant deletion and loading improvements - Retrieve existing built-in assistants before deletion to ensure accurate removal. - Update LoadBuiltIn function to track and delete assistants that are no longer present. - Enhance error handling during the deletion process for better reliability. --- neo/assistant/load.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/neo/assistant/load.go b/neo/assistant/load.go index 052b8de6..2c90726d 100644 --- a/neo/assistant/load.go +++ b/neo/assistant/load.go @@ -41,13 +41,23 @@ func LoadBuiltIn() error { return err } + // Get all existing built-in assistants + deletedBuiltIn := map[string]bool{} + // Remove the built-in assistants if storage != nil { + builtIn := true - _, err := storage.DeleteAssistants(store.AssistantFilter{BuiltIn: &builtIn}) + res, err := storage.GetAssistants(store.AssistantFilter{BuiltIn: &builtIn, Select: []string{"assistant_id", "id"}}) if err != nil { return err } + + // Get all existing built-in assistants + for _, assistant := range res.Data { + assistantID := assistant["assistant_id"].(string) + deletedBuiltIn[assistantID] = true + } } // Check if the assistant is built-in @@ -96,6 +106,22 @@ func LoadBuiltIn() error { sort++ loaded.Put(assistant) + // Remove the built-in assistant from the store + if _, ok := deletedBuiltIn[assistant.ID]; ok { + delete(deletedBuiltIn, assistant.ID) + } + } + + // Remove deleted built-in assistants + if len(deletedBuiltIn) > 0 { + assistantIDs := []string{} + for assistantID := range deletedBuiltIn { + assistantIDs = append(assistantIDs, assistantID) + } + _, err := storage.DeleteAssistants(store.AssistantFilter{AssistantIDs: assistantIDs}) + if err != nil { + return err + } } return nil