mirror of
https://github.com/laoxong/nofx.git
synced 2026-06-04 01:48:22 +08:00
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.
This commit is contained in:
+42
-21
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user