| 1 | use crate::action::AgentAction; |
| 2 | use crate::template::{Values, render}; |
| 3 | |
| 4 | const THREAD_LIMIT: usize = 20; |
| 5 | |
| 6 | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 7 | pub struct ThreadMessage { |
| 8 | pub sender: String, |
| 9 | pub text: String, |
| 10 | pub time: String, |
| 11 | } |
| 12 | |
| 13 | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 14 | pub struct MessageContext { |
| 15 | pub sender: String, |
| 16 | pub chat: String, |
| 17 | pub conversation_id: String, |
| 18 | pub message_id: String, |
| 19 | pub message: String, |
| 20 | pub thread: Vec<ThreadMessage>, |
| 21 | } |
| 22 | |
| 23 | fn render_thread(thread: &[ThreadMessage]) -> String { |
| 24 | if thread.is_empty() { |
| 25 | return "(none)".to_string(); |
| 26 | } |
| 27 | let skipped = thread.len().saturating_sub(THREAD_LIMIT); |
| 28 | thread[skipped..] |
| 29 | .iter() |
| 30 | .map(|entry| format!("{} ({}): {}", entry.sender, entry.time, entry.text)) |
| 31 | .collect::<Vec<_>>() |
| 32 | .join("\n") |
| 33 | } |
| 34 | |
| 35 | pub fn build_prompt( |
| 36 | action: &AgentAction, |
| 37 | context: &MessageContext, |
| 38 | extra: &str, |
| 39 | session_id: &str, |
| 40 | agent_id: &str, |
| 41 | ) -> String { |
| 42 | let values = Values { |
| 43 | message: context.message.clone(), |
| 44 | sender: context.sender.clone(), |
| 45 | chat: context.chat.clone(), |
| 46 | conversation_id: context.conversation_id.clone(), |
| 47 | message_id: context.message_id.clone(), |
| 48 | thread: render_thread(&context.thread), |
| 49 | extra: extra.to_string(), |
| 50 | session_id: session_id.to_string(), |
| 51 | agent: agent_id.to_string(), |
| 52 | ..Values::default() |
| 53 | }; |
| 54 | let rendered = |
| 55 | render(&action.prompt_template, &values).unwrap_or_else(|_| action.prompt_template.clone()); |
| 56 | rendered.trim().to_string() |
| 57 | } |
| 58 | |
| 59 | pub fn build_session_prompt( |
| 60 | context: &MessageContext, |
| 61 | message: &str, |
| 62 | session_id: &str, |
| 63 | agent_id: &str, |
| 64 | ) -> String { |
| 65 | let quoted = context |
| 66 | .message |
| 67 | .lines() |
| 68 | .map(|line| format!("> {line}")) |
| 69 | .collect::<Vec<_>>() |
| 70 | .join("\n"); |
| 71 | format!( |
| 72 | "Context: {sender} wrote in the Teams chat \"{chat}\" (conversation_id \"{conversation_id}\", message_id \"{message_id}\"):\n\n{quoted}\n\nThe `teams` MCP server can show images of messages (`get_image`) and list the newest messages of other chats (`recent_messages`). To reply, use the `write_message` tool of the `teams` MCP server: conversation_id \"{conversation_id}\", reply_to_message_id \"{message_id}\", session_id \"{session_id}\", agent \"{agent_id}\". Never send anything any other way.\n\n{message}", |
| 73 | sender = context.sender, |
| 74 | chat = context.chat, |
| 75 | conversation_id = context.conversation_id, |
| 76 | message_id = context.message_id, |
| 77 | ) |
| 78 | } |
| 79 | |
| 80 | #[cfg(test)] |
| 81 | mod tests { |
| 82 | use super::*; |
| 83 | |
| 84 | fn context(thread_length: usize) -> MessageContext { |
| 85 | MessageContext { |
| 86 | sender: "Ada".to_string(), |
| 87 | chat: "Ops".to_string(), |
| 88 | conversation_id: "19:abc".to_string(), |
| 89 | message_id: "42".to_string(), |
| 90 | message: "Where is the report?".to_string(), |
| 91 | thread: (0..thread_length) |
| 92 | .map(|index| ThreadMessage { |
| 93 | sender: format!("User{index}"), |
| 94 | text: format!("text {index}"), |
| 95 | time: "10:00".to_string(), |
| 96 | }) |
| 97 | .collect(), |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | #[test] |
| 102 | fn a_session_prompt_carries_the_message_ids_and_the_typed_text_last() { |
| 103 | let prompt = build_session_prompt(&context(0), "and the tests?", "sid-9", "claude"); |
| 104 | assert!(prompt.starts_with("Context: Ada wrote in the Teams chat \"Ops\"")); |
| 105 | assert!(prompt.contains("conversation_id \"19:abc\", message_id \"42\"")); |
| 106 | assert!(prompt.contains("\n> Where is the report?\n")); |
| 107 | assert!(prompt.contains("write_message")); |
| 108 | assert!(prompt.contains("session_id \"sid-9\", agent \"claude\"")); |
| 109 | assert!(prompt.ends_with("\n\nand the tests?")); |
| 110 | } |
| 111 | |
| 112 | #[test] |
| 113 | fn answer_prompt_renders_ids() { |
| 114 | let prompt = build_prompt( |
| 115 | &AgentAction::answer_this("claude"), |
| 116 | &context(0), |
| 117 | "", |
| 118 | "sid-9", |
| 119 | "claude", |
| 120 | ); |
| 121 | assert!( |
| 122 | prompt.starts_with("Ada asked me in the Teams chat \"Ops\":\n\nWhere is the report?") |
| 123 | ); |
| 124 | assert!(prompt.contains("Earlier messages in this chat:\n(none)\n\n")); |
| 125 | assert!(prompt.contains("conversation_id \"19:abc\", reply_to_message_id \"42\", session_id \"sid-9\", agent \"claude\".")); |
| 126 | assert!(prompt.ends_with("Never send anything any other way.")); |
| 127 | } |
| 128 | |
| 129 | #[test] |
| 130 | fn build_prompt_is_trimmed_and_takes_extra() { |
| 131 | let prompt = build_prompt( |
| 132 | &AgentAction::build_this("claude"), |
| 133 | &context(1), |
| 134 | "", |
| 135 | "sid-9", |
| 136 | "claude", |
| 137 | ); |
| 138 | assert!(prompt.ends_with("also after a restart.")); |
| 139 | assert!( |
| 140 | prompt |
| 141 | .contains("`schedule_wakeup` of the `teams` MCP server with session_id \"sid-9\"") |
| 142 | ); |
| 143 | assert!(prompt.contains("write a short reply to Ada")); |
| 144 | assert!(prompt.contains("User0 (10:00): text 0")); |
| 145 | let with_extra = build_prompt( |
| 146 | &AgentAction::build_this("claude"), |
| 147 | &context(1), |
| 148 | "Use Rust", |
| 149 | "sid-9", |
| 150 | "claude", |
| 151 | ); |
| 152 | assert!(with_extra.ends_with("also after a restart. Use Rust")); |
| 153 | } |
| 154 | |
| 155 | #[test] |
| 156 | fn thread_keeps_last_twenty() { |
| 157 | let prompt = build_prompt( |
| 158 | &AgentAction::build_this("claude"), |
| 159 | &context(25), |
| 160 | "", |
| 161 | "sid-9", |
| 162 | "claude", |
| 163 | ); |
| 164 | assert!(!prompt.contains("User4 ")); |
| 165 | assert!(prompt.contains("User5 (10:00)")); |
| 166 | assert!(prompt.contains("User24 (10:00)")); |
| 167 | } |
| 168 | } |