feat: add thinking content to chat response

This commit is contained in:
shokollm
2026-04-10 09:16:08 +00:00
parent db4fb83243
commit 57fa200ba9
5 changed files with 26 additions and 5 deletions

View File

@@ -220,6 +220,7 @@ def chat(
return BotChatResponse( return BotChatResponse(
response=assistant_content, response=assistant_content,
thinking=result.get("thinking"),
strategy_config=bot.strategy_config if result.get("strategy_updated") else None, strategy_config=bot.strategy_config if result.get("strategy_updated") else None,
success=result.get("success", False), success=result.get("success", False),
) )

View File

@@ -145,6 +145,7 @@ class BotChatRequest(BaseModel):
class BotChatResponse(BaseModel): class BotChatResponse(BaseModel):
response: str response: str
thinking: Optional[str] = None
strategy_config: Optional[dict] = None strategy_config: Optional[dict] = None
success: bool = False success: bool = False

View File

@@ -131,25 +131,38 @@ class ConversationalAgent:
conversation_history: Optional list of previous messages conversation_history: Optional list of previous messages
Returns: 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: 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 # Check if strategy was updated
result_str = str(result)
strategy_updated = "update_trading_strategy" in result_str or \ strategy_updated = "update_trading_strategy" in result_str or \
"Successfully updated" in result_str "Successfully updated" in result_str
return { return {
"response": result_str, "response": result_str,
"thinking": thinking,
"strategy_updated": strategy_updated, "strategy_updated": strategy_updated,
"success": True "success": True
} }
except Exception as e: except Exception as e:
return { return {
"response": f"I encountered an error: {str(e)}. Please try again.", "response": f"I encountered an error: {str(e)}. Please try again.",
"thinking": None,
"strategy_updated": False, "strategy_updated": False,
"success": False "success": False
} }

View File

@@ -123,6 +123,7 @@ export interface BotChatRequest {
export interface BotChatResponse { export interface BotChatResponse {
response: string; response: string;
thinking: string | null;
strategy_config: StrategyConfig | null; strategy_config: StrategyConfig | null;
success: boolean; success: boolean;
} }

View File

@@ -9,6 +9,7 @@
let botId = $derived($page.params.id); let botId = $derived($page.params.id);
let isSending = $state(false); let isSending = $state(false);
let showStrategy = $state(false); let showStrategy = $state(false);
let thinkingContent = $state('');
onMount(async () => { onMount(async () => {
if (!$isAuthenticated && !$isLoading) { if (!$isAuthenticated && !$isLoading) {
@@ -43,6 +44,7 @@
if (isSending) return; if (isSending) return;
isSending = true; isSending = true;
thinkingContent = '';
// Add user's message immediately so it shows even before API response // Add user's message immediately so it shows even before API response
addMessage({ role: 'user', content: message }); addMessage({ role: 'user', content: message });
@@ -55,6 +57,8 @@
const response = await api.bots.chat(botId, message, controller.signal); const response = await api.bots.chat(botId, message, controller.signal);
clearTimeout(timeoutId); clearTimeout(timeoutId);
// Set thinking content for display
thinkingContent = response.thinking || '';
addMessage({ role: 'assistant', content: response.response }); addMessage({ role: 'assistant', content: response.response });
if (response.strategy_config) { if (response.strategy_config) {
@@ -109,6 +113,7 @@
bot={$currentBotStore} bot={$currentBotStore}
messages={$chatStore} messages={$chatStore}
isThinking={isSending} isThinking={isSending}
{thinkingContent}
onSendMessage={handleSendMessage} onSendMessage={handleSendMessage}
/> />
</div> </div>