diff --git a/src/Managing.Api/Controllers/LlmController.cs b/src/Managing.Api/Controllers/LlmController.cs index c7200571..4e0c598a 100644 --- a/src/Managing.Api/Controllers/LlmController.cs +++ b/src/Managing.Api/Controllers/LlmController.cs @@ -1,3 +1,4 @@ +using System.Text.Json; using Managing.Application.Abstractions.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -125,7 +126,7 @@ public class LlmController : BaseController return new LlmMessage { Role = "tool", - Content = System.Text.Json.JsonSerializer.Serialize(toolResult), + Content = JsonSerializer.Serialize(toolResult), ToolCallId = toolCall.Id }; } @@ -223,10 +224,21 @@ public class LlmController : BaseController { var lastMessage = request.Messages.LastOrDefault(m => m.Role == "user")?.Content?.ToLowerInvariant() ?? ""; - // Complex operations need more iterations - if (lastMessage.Contains("bundle") || lastMessage.Contains("analyze") || lastMessage.Contains("compare")) + // Complex operations need more iterations (bundle analysis, multi-step workflows) + if (lastMessage.Contains("bundle") || lastMessage.Contains("compare") || lastMessage.Contains("all backtests")) return 5; + // Backtest detail requests need 4 iterations (list → get_by_id → analyze → format) + if (lastMessage.Contains("backtest") && + (lastMessage.Contains("detail") || lastMessage.Contains("analyze") || lastMessage.Contains("show") || + lastMessage.Contains("this") || lastMessage.Contains("that") || lastMessage.Contains("best") || + lastMessage.Contains("top") || lastMessage.Contains("recent"))) + return 4; + + // General analysis queries + if (lastMessage.Contains("analyze")) + return 4; + // Simple queries need fewer iterations if (lastMessage.Contains("explain") || lastMessage.Contains("what is") || lastMessage.Contains("how does")) return 2; @@ -264,20 +276,34 @@ public class LlmController : BaseController - Indicators: get_indicator_info() for detailed specs - Use conversation context: "that X" or "this Y" → extract ID from previous messages - 2. ANALYZE WITH EXPERTISE: + 2. CONTEXT EXTRACTION: + - Pay attention to backtest IDs mentioned in conversation history + - When user says "analyze this backtest" or "show me details", extract the backtest ID from previous messages + - If multiple backtests were listed, use the most recently mentioned one or the top-ranked one + - NEVER ask user for IDs that were already provided in conversation + + 3. BACKTEST DETAIL WORKFLOW: + When user requests backtest details/analysis: + a) If backtest ID is in conversation → IMMEDIATELY call get_backtest_by_id(id) + b) If no ID but refers to "best/top" → call get_backtests_paginated(sortBy='Score', sortOrder='desc', pageSize=1) THEN get_backtest_by_id() + c) If no ID but refers to "recent/latest" → call get_backtests_paginated(sortOrder='desc', pageSize=1) THEN get_backtest_by_id() + d) If completely ambiguous → ask ONCE for clarification, then proceed + + 4. ANALYZE WITH EXPERTISE: After retrieving data, provide comprehensive analysis: - Backtests: Performance (PnL, growth, ROI), Risk (Sharpe, drawdown), Win rate, Position patterns, Strengths/weaknesses, Recommendations + Backtests: Performance (PnL, growth, ROI), Risk (Sharpe, drawdown), Win rate, Position patterns, Trade duration, Strengths/weaknesses, Recommendations Bundles: Aggregate performance, Best/worst combinations, Optimal parameters, Robustness Indicators: Use cases, Parameter sensitivity, Combination suggestions, Pitfalls General: Compare to benchmarks, Statistical significance, Actionable insights - 3. BE PROACTIVE: + 5. BE PROACTIVE: - Execute multiple tool iterations for complete data - Interpret data, don't just return it + - Chain tool calls automatically (list → get_by_id → analyze) - Only ask for clarification when truly ambiguous - Be concise, accurate, and proactive. + Be concise, accurate, and proactive. Always prioritize retrieving complete data over asking questions. """; }