Merge pull request #897 from trheyi/main

Refactor assistant save and delete methods for cache management
This commit is contained in:
Max 2025-03-18 15:41:41 +08:00 committed by GitHub
commit 4ca0e40ad0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 30 additions and 6 deletions

View file

@ -1131,14 +1131,14 @@ func (neo *DSL) handleAssistantDetail(c *gin.Context) {
// handleAssistantSave handles creating or updating an assistant // handleAssistantSave handles creating or updating an assistant
func (neo *DSL) handleAssistantSave(c *gin.Context) { func (neo *DSL) handleAssistantSave(c *gin.Context) {
var assistant map[string]interface{} var assistantData map[string]interface{}
if err := c.BindJSON(&assistant); err != nil { if err := c.BindJSON(&assistantData); err != nil {
c.JSON(400, gin.H{"message": "invalid request body", "code": 400}) c.JSON(400, gin.H{"message": "invalid request body", "code": 400})
c.Done() c.Done()
return return
} }
id, err := neo.Store.SaveAssistant(assistant) id, err := neo.Store.SaveAssistant(assistantData)
if err != nil { if err != nil {
c.JSON(500, gin.H{"message": err.Error(), "code": 500}) c.JSON(500, gin.H{"message": err.Error(), "code": 500})
c.Done() c.Done()
@ -1146,11 +1146,24 @@ func (neo *DSL) handleAssistantSave(c *gin.Context) {
} }
// Update the assistant map with the returned ID if it's not already set // Update the assistant map with the returned ID if it's not already set
if _, ok := assistant["assistant_id"]; !ok { if _, ok := assistantData["assistant_id"]; !ok {
assistant["assistant_id"] = id assistantData["assistant_id"] = id
} }
c.JSON(200, gin.H{"message": "ok", "data": assistant}) // Remove the assistant from cache to ensure fresh data on next load
cache := assistant.GetCache()
if cache != nil {
cache.Remove(id.(string))
}
// Reload the assistant to ensure it's available in cache with updated data
_, err = assistant.Get(id.(string))
if err != nil {
// Just log the error, don't fail the request
fmt.Printf("Error reloading assistant %s: %v\n", id, err)
}
c.JSON(200, gin.H{"message": "ok", "data": assistantData})
c.Done() c.Done()
} }
@ -1170,6 +1183,12 @@ func (neo *DSL) handleAssistantDelete(c *gin.Context) {
return return
} }
// Remove the assistant from cache to ensure it's fully deleted
cache := assistant.GetCache()
if cache != nil {
cache.Remove(assistantID)
}
c.JSON(200, gin.H{"message": "ok"}) c.JSON(200, gin.H{"message": "ok"})
c.Done() c.Done()
} }

View file

@ -148,6 +148,11 @@ func ClearCache() {
} }
} }
// GetCache returns the loaded cache
func GetCache() *Cache {
return loaded
}
// LoadStore create a new assistant from store // LoadStore create a new assistant from store
func LoadStore(id string) (*Assistant, error) { func LoadStore(id string) (*Assistant, error) {