use crate::channel::AgentRequestKind;
pub fn normalize_instruction_conversation_id(
provider_conversation_id: Option<&str>,
) -> Option<String> {
provider_conversation_id
.map(str::trim)
.filter(|value| !value.is_empty())
.map(ToString::to_string)
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum InstructionDeliveryMode {
BootstrapFull,
DeltaOnly,
BootstrapWithReplay,
}
pub fn plan_app_server_instruction_delivery(
request_kind: &AgentRequestKind,
current_provider_conversation_id: Option<&str>,
persisted_instruction_conversation_id: Option<&str>,
should_replay_session_output: bool,
) -> InstructionDeliveryMode {
if should_replay_session_output {
return InstructionDeliveryMode::BootstrapWithReplay;
}
if matches!(
request_kind,
AgentRequestKind::UtilityPrompt | AgentRequestKind::AccountRead
) {
return InstructionDeliveryMode::BootstrapFull;
}
if normalize_instruction_conversation_id(current_provider_conversation_id).as_deref()
== persisted_instruction_conversation_id
{
return InstructionDeliveryMode::DeltaOnly;
}
InstructionDeliveryMode::BootstrapFull
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_plan_app_server_instruction_delivery_uses_delta_only_for_matching_state() {
let persisted_instruction_conversation_id =
normalize_instruction_conversation_id(Some("thread-123"));
let mode = plan_app_server_instruction_delivery(
&AgentRequestKind::SessionResume {
session_output: None,
},
Some("thread-123"),
persisted_instruction_conversation_id.as_deref(),
false,
);
assert_eq!(mode, InstructionDeliveryMode::DeltaOnly);
}
#[test]
fn test_plan_app_server_instruction_delivery_uses_bootstrap_with_replay_after_reset() {
let persisted_instruction_conversation_id =
normalize_instruction_conversation_id(Some("thread-123"));
let mode = plan_app_server_instruction_delivery(
&AgentRequestKind::SessionResume {
session_output: Some("previous output".to_string()),
},
Some("thread-456"),
persisted_instruction_conversation_id.as_deref(),
true,
);
assert_eq!(mode, InstructionDeliveryMode::BootstrapWithReplay);
}
#[test]
fn test_plan_app_server_instruction_delivery_bootstraps_full_for_new_context() {
let persisted_instruction_conversation_id =
normalize_instruction_conversation_id(Some("thread-123"));
let mode = plan_app_server_instruction_delivery(
&AgentRequestKind::SessionResume {
session_output: None,
},
Some("thread-456"),
persisted_instruction_conversation_id.as_deref(),
false,
);
assert_eq!(mode, InstructionDeliveryMode::BootstrapFull);
}
#[test]
fn test_plan_app_server_instruction_delivery_bootstraps_full_for_utility_prompt() {
let persisted_instruction_conversation_id =
normalize_instruction_conversation_id(Some("thread-123"));
let mode = plan_app_server_instruction_delivery(
&AgentRequestKind::UtilityPrompt,
Some("thread-123"),
persisted_instruction_conversation_id.as_deref(),
false,
);
assert_eq!(mode, InstructionDeliveryMode::BootstrapFull);
}
}