Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/agent/channel_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,25 @@ fn extract_reply_content_from_cancelled_history(
if let Some(content_value) = tool_call.function.arguments.get("content")
&& let Some(text) = content_value.as_str()
{
return Some(text.to_string());
// Also extract card descriptions so the full response
// (not just the short content text) is preserved in
// conversation history for subsequent LLM turns.
let cards = match tool_call.function.arguments.get("cards") {
Some(v) => {
serde_json::from_value::<Vec<crate::Card>>(v.clone())
.unwrap_or_else(|e| {
tracing::warn!(
error = %e,
"failed to deserialize cards from cancelled reply tool call; \
card content will be omitted from history"
);
Vec::new()
})
}
None => Vec::new(),
};

return Some(crate::OutboundResponse::text_with_cards(text, &cards));
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,22 @@ impl OutboundResponse {
}
sections.join("\n\n")
}

/// Merge `text` with a plaintext representation of `cards`.
///
/// Returns `text` unchanged when cards produce no text content,
/// the card text alone when `text` is whitespace-only, or both
/// joined with a blank line.
pub fn text_with_cards(text: &str, cards: &[Card]) -> String {
let card_text = Self::text_from_cards(cards);
if card_text.is_empty() {
text.to_string()
} else if text.trim().is_empty() {
card_text
} else {
format!("{}\n\n{}", text, card_text)
}
}
}

/// A generic rich-formatted card (maps to Embeds in Discord).
Expand Down
13 changes: 12 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ fn forward_sse_event(
) {
match response {
spacebot::OutboundResponse::Text(text)
| spacebot::OutboundResponse::RichMessage { text, .. }
| spacebot::OutboundResponse::ThreadReply { text, .. } => {
api_event_tx
.send(spacebot::api::ApiEvent::OutboundMessage {
Expand All @@ -285,6 +284,18 @@ fn forward_sse_event(
})
.ok();
}
spacebot::OutboundResponse::RichMessage { text, cards, .. } => {
// Flatten card content so SSE consumers (dashboard and webchat),
// which don't render rich embeds, see the full response.
let full_text = spacebot::OutboundResponse::text_with_cards(text, cards);
api_event_tx
.send(spacebot::api::ApiEvent::OutboundMessage {
agent_id: agent_id.to_string(),
channel_id: channel_id.to_string(),
text: full_text,
})
.ok();
}
spacebot::OutboundResponse::Status(spacebot::StatusUpdate::Thinking) => {
api_event_tx
.send(spacebot::api::ApiEvent::TypingState {
Expand Down
4 changes: 3 additions & 1 deletion src/messaging/webchat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ impl Messaging for WebChatAdapter {
async fn broadcast(&self, target: &str, response: OutboundResponse) -> crate::Result<()> {
let text = match &response {
OutboundResponse::Text(text) => text.clone(),
OutboundResponse::RichMessage { text, .. } => text.clone(),
OutboundResponse::RichMessage { text, cards, .. } => {
OutboundResponse::text_with_cards(text, cards)
}
_ => return Ok(()),
};

Expand Down
16 changes: 13 additions & 3 deletions src/tools/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,25 +460,35 @@ impl Tool for ReplyTool {
text: converted_content.clone(),
}
} else if args.cards.is_some() || args.interactive_elements.is_some() || poll.is_some() {
let cards = args.cards.unwrap_or_default();
let interactive_elements = args.interactive_elements.unwrap_or_default();
OutboundResponse::RichMessage {
text: converted_content.clone(),
blocks: vec![],
cards: args.cards.unwrap_or_default(),
interactive_elements: args.interactive_elements.unwrap_or_default(),
cards,
interactive_elements,
poll,
}
} else {
OutboundResponse::Text(converted_content.clone())
};

// For the conversation log, flatten card content so consumers of
// stored history (including webchat) see the full response.
let logged_content = if let OutboundResponse::RichMessage { cards, .. } = &response {
OutboundResponse::text_with_cards(&converted_content, cards)
} else {
converted_content.clone()
};

self.response_tx
.send(response)
.await
.map_err(|e| ReplyError(format!("failed to send reply: {e}")))?;

self.conversation_logger.log_bot_message_with_name(
&self.channel_id,
&converted_content,
&logged_content,
Some(&self.agent_display_name),
);

Expand Down
Loading