From 57fa200ba9b81b236d48b375f52e1e908436efde Mon Sep 17 00:00:00 2001 From: shokollm <270575765+shokollm@users.noreply.github.com> Date: Fri, 10 Apr 2026 09:16:08 +0000 Subject: [PATCH] feat: add thinking content to chat response --- src/backend/app/api/bots.py | 1 + src/backend/app/db/schemas.py | 1 + .../app/services/ai_agent/conversational.py | 21 +++++++++++++++---- src/frontend/src/lib/api/types.ts | 1 + src/frontend/src/routes/bot/[id]/+page.svelte | 7 ++++++- 5 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/backend/app/api/bots.py b/src/backend/app/api/bots.py index e18ff8e..4755e95 100644 --- a/src/backend/app/api/bots.py +++ b/src/backend/app/api/bots.py @@ -220,6 +220,7 @@ def chat( return BotChatResponse( response=assistant_content, + thinking=result.get("thinking"), strategy_config=bot.strategy_config if result.get("strategy_updated") else None, success=result.get("success", False), ) diff --git a/src/backend/app/db/schemas.py b/src/backend/app/db/schemas.py index 12f9b98..c89f963 100644 --- a/src/backend/app/db/schemas.py +++ b/src/backend/app/db/schemas.py @@ -145,6 +145,7 @@ class BotChatRequest(BaseModel): class BotChatResponse(BaseModel): response: str + thinking: Optional[str] = None strategy_config: Optional[dict] = None success: bool = False diff --git a/src/backend/app/services/ai_agent/conversational.py b/src/backend/app/services/ai_agent/conversational.py index d8b3b63..152be2b 100644 --- a/src/backend/app/services/ai_agent/conversational.py +++ b/src/backend/app/services/ai_agent/conversational.py @@ -131,25 +131,38 @@ class ConversationalAgent: conversation_history: Optional list of previous messages Returns: - Dict with 'response' (the assistant's reply) and 'strategy_updated' (bool) + Dict with 'response' (the assistant's reply), 'thinking' (reasoning), and 'strategy_updated' (bool) """ - # Execute agent using kickoff + # Execute agent using stream to capture both thinking and output try: - result = self.agent.kickoff(user_message) + # Use stream instead of kickoff to get thinking content + result = self.agent.stream(user_message) + + # In stream mode, result contains both output and thinking + # Extract thinking from the result if available + thinking = None + if hasattr(result, 'thinking') and result.thinking: + thinking = result.thinking + elif isinstance(result, dict) and 'thinking' in result: + thinking = result.get('thinking') + + # The actual response + result_str = str(result) if not isinstance(result, str) else result # Check if strategy was updated - result_str = str(result) strategy_updated = "update_trading_strategy" in result_str or \ "Successfully updated" in result_str return { "response": result_str, + "thinking": thinking, "strategy_updated": strategy_updated, "success": True } except Exception as e: return { "response": f"I encountered an error: {str(e)}. Please try again.", + "thinking": None, "strategy_updated": False, "success": False } diff --git a/src/frontend/src/lib/api/types.ts b/src/frontend/src/lib/api/types.ts index c6b9847..f43db6b 100644 --- a/src/frontend/src/lib/api/types.ts +++ b/src/frontend/src/lib/api/types.ts @@ -123,6 +123,7 @@ export interface BotChatRequest { export interface BotChatResponse { response: string; + thinking: string | null; strategy_config: StrategyConfig | null; success: boolean; } diff --git a/src/frontend/src/routes/bot/[id]/+page.svelte b/src/frontend/src/routes/bot/[id]/+page.svelte index bc759ad..7f04f5e 100644 --- a/src/frontend/src/routes/bot/[id]/+page.svelte +++ b/src/frontend/src/routes/bot/[id]/+page.svelte @@ -9,6 +9,7 @@ let botId = $derived($page.params.id); let isSending = $state(false); let showStrategy = $state(false); + let thinkingContent = $state(''); onMount(async () => { if (!$isAuthenticated && !$isLoading) { @@ -43,6 +44,7 @@ if (isSending) return; isSending = true; + thinkingContent = ''; // Add user's message immediately so it shows even before API response addMessage({ role: 'user', content: message }); @@ -55,6 +57,8 @@ const response = await api.bots.chat(botId, message, controller.signal); clearTimeout(timeoutId); + // Set thinking content for display + thinkingContent = response.thinking || ''; addMessage({ role: 'assistant', content: response.response }); if (response.strategy_config) { @@ -109,7 +113,8 @@ bot={$currentBotStore} messages={$chatStore} isThinking={isSending} - onSendMessage={handleSendMessage} + {thinkingContent} + onSendMessage={handleSendMessage} />