netget/llm/
event_instructions.rs1use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct EventInstructions {
12 pub instructions: String,
14
15 #[serde(default)]
17 pub examples: Vec<Example>,
18}
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
22pub struct Example {
23 pub input: String,
25
26 pub output: String,
28
29 #[serde(skip_serializing_if = "Option::is_none")]
31 pub description: Option<String>,
32}
33
34#[derive(Debug, Clone, Default)]
36pub struct ServerInstructionConfig {
37 pub global_instructions: Option<String>,
39
40 pub event_overrides: HashMap<String, EventInstructions>,
42
43 pub scripts: HashMap<String, crate::scripting::types::ScriptConfig>,
45}
46
47impl ServerInstructionConfig {
48 pub fn new() -> Self {
50 Self::default()
51 }
52
53 pub fn with_global_instructions(mut self, instructions: String) -> Self {
55 self.global_instructions = Some(instructions);
56 self
57 }
58
59 pub fn with_event_override(
61 mut self,
62 event_type: String,
63 instructions: EventInstructions,
64 ) -> Self {
65 self.event_overrides.insert(event_type, instructions);
66 self
67 }
68
69 pub fn get_instructions_for_event(&self, event_type: &str) -> Option<&EventInstructions> {
71 self.event_overrides.get(event_type)
72 }
73
74 pub fn has_script_for_event(&self, event_type: &str) -> bool {
76 self.scripts
77 .iter()
78 .any(|(_, config)| config.handles_context(event_type))
79 }
80}
81
82#[derive(Debug, Clone)]
84pub enum InstructionSource {
85 Script(String),
87 EventOverride(EventInstructions),
89 Default(EventInstructions),
91 GlobalOnly(String),
93 None,
95}
96
97impl EventInstructions {
98 pub fn new(instructions: impl Into<String>) -> Self {
100 Self {
101 instructions: instructions.into(),
102 examples: Vec::new(),
103 }
104 }
105
106 pub fn with_example(mut self, input: impl Into<String>, output: impl Into<String>) -> Self {
108 self.examples.push(Example {
109 input: input.into(),
110 output: output.into(),
111 description: None,
112 });
113 self
114 }
115
116 pub fn with_described_example(
118 mut self,
119 input: impl Into<String>,
120 output: impl Into<String>,
121 description: impl Into<String>,
122 ) -> Self {
123 self.examples.push(Example {
124 input: input.into(),
125 output: output.into(),
126 description: Some(description.into()),
127 });
128 self
129 }
130}