use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventInstructions {
pub instructions: String,
#[serde(default)]
pub examples: Vec<Example>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Example {
pub input: String,
pub output: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
}
#[derive(Debug, Clone, Default)]
pub struct ServerInstructionConfig {
pub global_instructions: Option<String>,
pub event_overrides: HashMap<String, EventInstructions>,
pub scripts: HashMap<String, crate::scripting::types::ScriptConfig>,
}
impl ServerInstructionConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_global_instructions(mut self, instructions: String) -> Self {
self.global_instructions = Some(instructions);
self
}
pub fn with_event_override(
mut self,
event_type: String,
instructions: EventInstructions,
) -> Self {
self.event_overrides.insert(event_type, instructions);
self
}
pub fn get_instructions_for_event(&self, event_type: &str) -> Option<&EventInstructions> {
self.event_overrides.get(event_type)
}
pub fn has_script_for_event(&self, event_type: &str) -> bool {
self.scripts
.iter()
.any(|(_, config)| config.handles_context(event_type))
}
}
#[derive(Debug, Clone)]
pub enum InstructionSource {
Script(String),
EventOverride(EventInstructions),
Default(EventInstructions),
GlobalOnly(String),
None,
}
impl EventInstructions {
pub fn new(instructions: impl Into<String>) -> Self {
Self {
instructions: instructions.into(),
examples: Vec::new(),
}
}
pub fn with_example(mut self, input: impl Into<String>, output: impl Into<String>) -> Self {
self.examples.push(Example {
input: input.into(),
output: output.into(),
description: None,
});
self
}
pub fn with_described_example(
mut self,
input: impl Into<String>,
output: impl Into<String>,
description: impl Into<String>,
) -> Self {
self.examples.push(Example {
input: input.into(),
output: output.into(),
description: Some(description.into()),
});
self
}
}