use crate::core::agent::events::ExecEventRecorder;
use crate::core::agent::session::AgentSessionState;
use crate::core::agent::task::TaskOutcome;
use crate::llm::provider::{FinishReason, Message, MessageRole};
use vtcode_exec_events::Usage;
pub(super) fn record_terminal_turn_event(event_recorder: &mut ExecEventRecorder, outcome: &TaskOutcome, usage: Usage) {
if outcome.is_success() {
event_recorder.turn_completed(usage);
} else {
event_recorder.turn_failed(&outcome.description(), Some(usage));
}
}
pub(super) fn discard_refused_assistant_message(state: &mut AgentSessionState) {
if state
.messages
.last()
.is_none_or(|message| message.role != MessageRole::Assistant)
{
return;
}
if let Some(message) = state.messages_mut().pop() {
state.adjust_token_count(-(message.estimate_tokens() as isize));
}
if state.conversation.last().is_some_and(|content| content.role == "model") {
state.conversation.pop();
state.last_processed_message_idx = state.last_processed_message_idx.min(state.conversation.len());
}
}
pub(super) fn emit_blocked_handoff_events(
event_recorder: &mut ExecEventRecorder,
blocked_message: &str,
current_path: &std::path::Path,
archive_path: &std::path::Path,
) {
event_recorder.turn_blocked(vtcode_exec_events::TurnBlockedEvent {
completed_at: None,
message: blocked_message.to_string(),
last_tool: None,
blocked_streak: 0,
blocked_total: 0,
consecutive_cap: 0,
total_cap: 0,
recovery_active: false,
usage: None,
});
event_recorder.harness_event(
crate::exec::events::HarnessEventKind::TurnBlocked,
Some(blocked_message.to_string()),
None,
None,
None,
None,
None,
);
for path in [current_path, archive_path] {
event_recorder.harness_event(
crate::exec::events::HarnessEventKind::BlockedHandoffWritten,
Some("Blocked handoff written".to_string()),
None,
Some(path.display().to_string()),
None,
None,
None,
);
}
}
pub(super) fn summarize_verification_output(result: &serde_json::Value) -> String {
result
.get("output")
.and_then(serde_json::Value::as_str)
.or_else(|| result.get("stderr").and_then(serde_json::Value::as_str))
.or_else(|| result.get("stdout").and_then(serde_json::Value::as_str))
.map(str::trim)
.filter(|text| !text.is_empty())
.map(|text| {
let truncated = text.lines().take(20).collect::<Vec<_>>().join("\n").trim().to_string();
if truncated.len() < text.len() {
format!("{truncated}\n...")
} else {
truncated
}
})
.unwrap_or_default()
}
pub(super) fn stop_reason_from_finish_reason(finish_reason: &FinishReason) -> String {
match finish_reason {
FinishReason::Stop => "end_turn".to_string(),
FinishReason::Length => "max_tokens".to_string(),
FinishReason::ToolCalls => "tool_calls".to_string(),
FinishReason::ContentFilter => "content_filter".to_string(),
FinishReason::Pause => "pause_turn".to_string(),
FinishReason::Refusal => "refusal".to_string(),
FinishReason::Error(message) => message.clone(),
}
}
pub(super) fn prepare_responses_request_messages<'a>(
previous_chains: &mut crate::llm::provider::ResponsesContinuationStore,
provider_name: &str,
provider_supports_responses_compaction: bool,
model: &str,
messages: &'a [Message],
) -> (std::borrow::Cow<'a, [Message]>, Option<String>) {
previous_chains.prepare_request_messages(provider_name, provider_supports_responses_compaction, model, messages)
}