BlueGit

ict rusty-teams

Files

rusty-teams / crates / agents / src / context.rs

context.rs

context.rs 3.9 KiB Download Raw
1use crate::action::AgentAction;
2use crate::template::{Values, render};
3
4const THREAD_LIMIT: usize = 20;
5
6#[derive(Clone, Debug, Default, PartialEq, Eq)]
7pub struct ThreadMessage {
8 pub sender: String,
9 pub text: String,
10 pub time: String,
11}
12
13#[derive(Clone, Debug, Default, PartialEq, Eq)]
14pub 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
23fn 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
35pub 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#[cfg(test)]
60mod tests {
61 use super::*;
62
63 fn context(thread_length: usize) -> MessageContext {
64 MessageContext {
65 sender: "Ada".to_string(),
66 chat: "Ops".to_string(),
67 conversation_id: "19:abc".to_string(),
68 message_id: "42".to_string(),
69 message: "Where is the report?".to_string(),
70 thread: (0..thread_length)
71 .map(|index| ThreadMessage {
72 sender: format!("User{index}"),
73 text: format!("text {index}"),
74 time: "10:00".to_string(),
75 })
76 .collect(),
77 }
78 }
79
80 #[test]
81 fn answer_prompt_renders_ids() {
82 let prompt = build_prompt(
83 &AgentAction::answer_this("claude"),
84 &context(0),
85 "",
86 "sid-9",
87 "claude",
88 );
89 assert!(
90 prompt.starts_with("Ada asked me in the Teams chat \"Ops\":\n\nWhere is the report?")
91 );
92 assert!(prompt.contains("Earlier messages in this chat:\n(none)\n\n"));
93 assert!(prompt.contains("conversation_id \"19:abc\", reply_to_message_id \"42\", session_id \"sid-9\", agent \"claude\"."));
94 assert!(prompt.ends_with("Never send anything yourself."));
95 }
96
97 #[test]
98 fn build_prompt_is_trimmed_and_takes_extra() {
99 let prompt = build_prompt(
100 &AgentAction::build_this("claude"),
101 &context(1),
102 "",
103 "sid-9",
104 "claude",
105 );
106 assert!(prompt.ends_with("agent \"claude\"."));
107 assert!(prompt.contains("propose a short reply to Ada"));
108 assert!(prompt.contains("User0 (10:00): text 0"));
109 let with_extra = build_prompt(
110 &AgentAction::build_this("claude"),
111 &context(1),
112 "Use Rust",
113 "sid-9",
114 "claude",
115 );
116 assert!(with_extra.ends_with("agent \"claude\". Use Rust"));
117 }
118
119 #[test]
120 fn thread_keeps_last_twenty() {
121 let prompt = build_prompt(
122 &AgentAction::build_this("claude"),
123 &context(25),
124 "",
125 "sid-9",
126 "claude",
127 );
128 assert!(!prompt.contains("User4 "));
129 assert!(prompt.contains("User5 (10:00)"));
130 assert!(prompt.contains("User24 (10:00)"));
131 }
132}