rs_adk/plugin/
global_instruction.rs1use async_trait::async_trait;
7
8use super::{Plugin, PluginResult};
9use crate::context::InvocationContext;
10
11pub struct GlobalInstructionPlugin {
16 prepend: Option<String>,
18 append: Option<String>,
20}
21
22impl GlobalInstructionPlugin {
23 pub fn new() -> Self {
25 Self {
26 prepend: None,
27 append: None,
28 }
29 }
30
31 pub fn with_prepend(mut self, instruction: impl Into<String>) -> Self {
33 self.prepend = Some(instruction.into());
34 self
35 }
36
37 pub fn with_append(mut self, instruction: impl Into<String>) -> Self {
39 self.append = Some(instruction.into());
40 self
41 }
42
43 pub fn prepend(&self) -> Option<&str> {
45 self.prepend.as_deref()
46 }
47
48 pub fn append(&self) -> Option<&str> {
50 self.append.as_deref()
51 }
52}
53
54impl Default for GlobalInstructionPlugin {
55 fn default() -> Self {
56 Self::new()
57 }
58}
59
60#[async_trait]
61impl Plugin for GlobalInstructionPlugin {
62 fn name(&self) -> &str {
63 "global_instruction"
64 }
65
66 async fn before_model(
67 &self,
68 _request: &crate::llm::LlmRequest,
69 _ctx: &InvocationContext,
70 ) -> PluginResult {
71 PluginResult::Continue
75 }
76}
77
78#[cfg(test)]
79mod tests {
80 use super::*;
81
82 #[test]
83 fn default_empty() {
84 let plugin = GlobalInstructionPlugin::new();
85 assert!(plugin.prepend().is_none());
86 assert!(plugin.append().is_none());
87 }
88
89 #[test]
90 fn with_instructions() {
91 let plugin = GlobalInstructionPlugin::new()
92 .with_prepend("Safety first.")
93 .with_append("Always be helpful.");
94 assert_eq!(plugin.prepend(), Some("Safety first."));
95 assert_eq!(plugin.append(), Some("Always be helpful."));
96 }
97
98 #[test]
99 fn plugin_name() {
100 let plugin = GlobalInstructionPlugin::new();
101 assert_eq!(plugin.name(), "global_instruction");
102 }
103}