use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PromptBlock {
pub role: BlockRole,
pub content: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cache_strategy: Option<CacheStrategy>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(default, skip_serializing_if = "BlockOrigin::is_kernel")]
pub origin: BlockOrigin,
}
pub mod names {
pub const SCENE_SKELETON: &str = "scene.skeleton";
pub const SCENE_PREFIX: &str = "scene.";
pub const SKILLS_CATALOG: &str = "skills.catalog";
pub const MEMORY_SESSION: &str = "memory.session";
pub const RULES: &str = "rules";
pub const MCP_INSTRUCTIONS: &str = "mcp.instructions";
pub const CONFIG_PROMPT_APPEND: &str = "config.prompt_append";
pub const CONFIG_PREFIX: &str = "config.";
pub const HOST_PREFIX: &str = "host.";
pub const CONFIG_PROMPT_OVERRIDE: &str = "config.prompt_override";
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum BlockOrigin {
#[default]
Kernel,
Config,
Plugin(String),
Script(String),
Host(String),
#[serde(untagged)]
ConfigEntry { config: String },
}
impl BlockOrigin {
pub fn is_kernel(&self) -> bool {
matches!(self, Self::Kernel)
}
pub fn is_local(&self) -> bool {
matches!(
self,
Self::Kernel
| Self::Config
| Self::ConfigEntry { .. }
| Self::Script(_)
| Self::Host(_)
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum BlockRole {
System,
User,
Assistant,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CacheStrategy {
Ephemeral,
Global,
}
impl PromptBlock {
pub fn system(content: impl Into<String>) -> Self {
Self {
role: BlockRole::System,
content: content.into(),
cache_strategy: None,
name: None,
origin: BlockOrigin::Kernel,
}
}
pub fn system_cached(content: impl Into<String>) -> Self {
Self {
role: BlockRole::System,
content: content.into(),
cache_strategy: Some(CacheStrategy::Ephemeral),
name: None,
origin: BlockOrigin::Kernel,
}
}
pub fn user(content: impl Into<String>) -> Self {
Self {
role: BlockRole::User,
content: content.into(),
cache_strategy: None,
name: None,
origin: BlockOrigin::Kernel,
}
}
pub fn named(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn from_origin(mut self, origin: BlockOrigin) -> Self {
self.origin = origin;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_kernel_block_names_are_what_they_are_published_as() {
assert_eq!(names::SCENE_SKELETON, "scene.skeleton");
assert_eq!(names::SKILLS_CATALOG, "skills.catalog");
assert_eq!(names::MEMORY_SESSION, "memory.session");
assert_eq!(names::RULES, "rules");
assert_eq!(names::MCP_INSTRUCTIONS, "mcp.instructions");
assert_eq!(names::CONFIG_PROMPT_APPEND, "config.prompt_append");
assert_eq!(names::CONFIG_PROMPT_OVERRIDE, "config.prompt_override");
assert_eq!(names::HOST_PREFIX, "host.");
assert_eq!(names::CONFIG_PREFIX, "config.");
}
#[test]
fn a_settings_file_entry_reads_and_writes_as_config_with_its_name() {
let entry = BlockOrigin::ConfigEntry {
config: "review".into(),
};
let json = serde_json::json!({"config": "review"});
assert_eq!(serde_json::to_value(&entry).unwrap(), json);
assert_eq!(serde_json::from_value::<BlockOrigin>(json).unwrap(), entry);
assert_eq!(
serde_json::from_value::<BlockOrigin>(serde_json::json!("config")).unwrap(),
BlockOrigin::Config
);
assert_eq!(
serde_json::from_value::<BlockOrigin>(serde_json::json!({"host": "a"})).unwrap(),
BlockOrigin::Host("a".into())
);
}
}