From 0d74c27be27ea39536d73515be88fc89783dd8d4 Mon Sep 17 00:00:00 2001 From: Dean Date: Wed, 15 Apr 2026 18:34:20 +0800 Subject: [PATCH] refactor(api): streamline claw402 wallet key retrieval and error handling Refactored the strategy handling logic to encapsulate claw402 wallet key retrieval in a new method, `resolveStrategyDataWalletKey`. This improves code readability and maintains consistent error handling for missing or invalid wallet keys during strategy test runs. The changes enhance the overall robustness of the AI model integration. --- api/strategy.go | 63 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/api/strategy.go b/api/strategy.go index 07e20987..f2813efb 100644 --- a/api/strategy.go +++ b/api/strategy.go @@ -516,27 +516,13 @@ func (s *Server) handleStrategyTestRun(c *gin.Context) { req.PromptVariant = "balanced" } - claw402WalletKey := "" - if req.AIModelID != "" { - model, err := s.store.AIModel().Get(userID, req.AIModelID) - if err != nil { - c.JSON(http.StatusBadRequest, gin.H{ - "error": "Failed to load selected AI model", - "ai_response": "", - }) - return - } - - if model.Provider == "claw402" { - claw402WalletKey = string(model.APIKey) - if claw402WalletKey == "" { - c.JSON(http.StatusBadRequest, gin.H{ - "error": "Selected claw402 model is missing wallet private key", - "ai_response": "", - }) - return - } - } + claw402WalletKey, err := s.resolveStrategyDataWalletKey(userID, req.AIModelID) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "error": err.Error(), + "ai_response": "", + }) + return } // Create strategy engine to build prompt @@ -720,3 +706,38 @@ func (s *Server) runRealAITest(userID, modelID, systemPrompt, userPrompt string) return response, nil } + +func (s *Server) resolveStrategyDataWalletKey(userID, selectedModelID string) (string, error) { + if selectedModelID != "" { + model, err := s.store.AIModel().Get(userID, selectedModelID) + if err != nil { + return "", fmt.Errorf("failed to load selected AI model") + } + + if model.Provider == "claw402" { + walletKey := string(model.APIKey) + if walletKey == "" { + return "", fmt.Errorf("selected claw402 model is missing wallet private key") + } + return walletKey, nil + } + } + + models, err := s.store.AIModel().List(userID) + if err != nil { + return "", fmt.Errorf("failed to load AI models") + } + + for _, model := range models { + if model == nil || model.Provider != "claw402" { + continue + } + + walletKey := string(model.APIKey) + if walletKey != "" { + return walletKey, nil + } + } + + return "", nil +}