Skip to main content

lash_sansio/
plugin.rs

1use std::sync::Arc;
2
3use crate::{MessageOrigin, MessageRole, Part};
4use serde::{Deserialize, Serialize};
5
6#[derive(Clone, Debug, Serialize, Deserialize)]
7pub struct PluginMessage {
8    pub role: MessageRole,
9    pub content: String,
10    #[serde(default, skip_serializing_if = "Option::is_none")]
11    pub origin: Option<MessageOrigin>,
12    #[serde(default, skip_serializing_if = "Vec::is_empty")]
13    pub parts: Vec<Part>,
14    #[serde(default, skip_serializing_if = "Vec::is_empty")]
15    pub images: Vec<Vec<u8>>,
16}
17
18impl PluginMessage {
19    pub fn text(role: MessageRole, content: impl Into<String>) -> Self {
20        Self {
21            role,
22            content: content.into(),
23            origin: None,
24            parts: Vec::new(),
25            images: Vec::new(),
26        }
27    }
28
29    pub fn with_origin(mut self, origin: MessageOrigin) -> Self {
30        self.origin = Some(origin);
31        self
32    }
33
34    pub fn first_text(&self) -> Option<&str> {
35        if !self.content.is_empty() {
36            return Some(self.content.as_str());
37        }
38        self.parts.iter().find_map(|part| {
39            matches!(part.kind, crate::PartKind::Text | crate::PartKind::Prose)
40                .then_some(part.content.as_str())
41        })
42    }
43}
44
45/// Gate on Tool Catalog membership: a contribution is kept when at least one
46/// of `tools` is a member of the catalog. There is no minimum-tier dimension.
47#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
48pub struct PromptContributionGate {
49    #[serde(default, skip_serializing_if = "Vec::is_empty")]
50    pub tools: Vec<String>,
51}
52
53impl PromptContributionGate {
54    pub fn is_empty(&self) -> bool {
55        self.tools.is_empty()
56    }
57}
58
59#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
60pub struct PromptContribution {
61    pub slot: crate::PromptSlot,
62    #[serde(default, skip_serializing_if = "Option::is_none")]
63    pub title: Option<Arc<str>>,
64    #[serde(default)]
65    pub priority: i32,
66    #[serde(default, skip_serializing_if = "PromptContributionGate::is_empty")]
67    pub gate: PromptContributionGate,
68    pub content: Arc<str>,
69}
70
71impl PromptContribution {
72    pub fn new(
73        slot: crate::PromptSlot,
74        title: impl Into<Arc<str>>,
75        content: impl Into<Arc<str>>,
76    ) -> Self {
77        let title: Arc<str> = title.into();
78        let title = (!title.trim().is_empty()).then_some(title);
79        Self {
80            slot,
81            title,
82            priority: 0,
83            gate: PromptContributionGate { tools: Vec::new() },
84            content: content.into(),
85        }
86    }
87
88    pub fn with_priority(mut self, priority: i32) -> Self {
89        self.priority = priority;
90        self
91    }
92
93    pub fn requires_tool(mut self, tool_name: impl Into<String>) -> Self {
94        self.gate = PromptContributionGate {
95            tools: vec![tool_name.into()],
96        };
97        self
98    }
99
100    pub fn requires_any_tool(
101        mut self,
102        tool_names: impl IntoIterator<Item = impl Into<String>>,
103    ) -> Self {
104        self.gate = PromptContributionGate {
105            tools: tool_names.into_iter().map(Into::into).collect(),
106        };
107        self
108    }
109
110    pub fn intro(title: impl Into<Arc<str>>, content: impl Into<Arc<str>>) -> Self {
111        Self::new(crate::PromptSlot::Intro, title, content)
112    }
113
114    pub fn execution(title: impl Into<Arc<str>>, content: impl Into<Arc<str>>) -> Self {
115        Self::new(crate::PromptSlot::Execution, title, content)
116    }
117
118    pub fn guidance(title: impl Into<Arc<str>>, content: impl Into<Arc<str>>) -> Self {
119        Self::new(crate::PromptSlot::Guidance, title, content)
120    }
121
122    pub fn project_instructions(content: impl Into<Arc<str>>) -> Self {
123        Self::new(
124            crate::PromptSlot::ProjectInstructions,
125            "Project Instructions",
126            content,
127        )
128    }
129
130    pub fn runtime_context(content: impl Into<Arc<str>>) -> Self {
131        Self::new(
132            crate::PromptSlot::RuntimeContext,
133            "Runtime Context",
134            content,
135        )
136    }
137
138    pub fn environment(title: impl Into<Arc<str>>, content: impl Into<Arc<str>>) -> Self {
139        Self::new(crate::PromptSlot::Environment, title, content)
140    }
141}
142
143#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
144#[serde(tag = "kind", rename_all = "snake_case")]
145pub enum PluginRuntimeEvent {
146    Status {
147        key: String,
148        label: String,
149        #[serde(default, skip_serializing_if = "Option::is_none")]
150        detail: Option<String>,
151    },
152    Custom {
153        name: String,
154        payload: serde_json::Value,
155    },
156}
157
158#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
159#[serde(rename_all = "snake_case")]
160pub enum CheckpointKind {
161    AfterWork,
162    BeforeCompletion,
163}