feat: add thinking content to chat response
This commit is contained in:
@@ -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),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,7 +113,8 @@
|
|||||||
bot={$currentBotStore}
|
bot={$currentBotStore}
|
||||||
messages={$chatStore}
|
messages={$chatStore}
|
||||||
isThinking={isSending}
|
isThinking={isSending}
|
||||||
onSendMessage={handleSendMessage}
|
{thinkingContent}
|
||||||
|
onSendMessage={handleSendMessage}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user