feat: add thinking content to chat response
This commit is contained in:
@@ -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),
|
||||
)
|
||||
|
||||
@@ -145,6 +145,7 @@ class BotChatRequest(BaseModel):
|
||||
|
||||
class BotChatResponse(BaseModel):
|
||||
response: str
|
||||
thinking: Optional[str] = None
|
||||
strategy_config: Optional[dict] = None
|
||||
success: bool = False
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -123,6 +123,7 @@ export interface BotChatRequest {
|
||||
|
||||
export interface BotChatResponse {
|
||||
response: string;
|
||||
thinking: string | null;
|
||||
strategy_config: StrategyConfig | null;
|
||||
success: boolean;
|
||||
}
|
||||
|
||||
@@ -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,6 +113,7 @@
|
||||
bot={$currentBotStore}
|
||||
messages={$chatStore}
|
||||
isThinking={isSending}
|
||||
{thinkingContent}
|
||||
onSendMessage={handleSendMessage}
|
||||
/>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user