1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//! Slot definitions for code injection points.
//!
//! Slots are placeholders in templates where AI-generated code will be injected.
use serde::{Deserialize, Serialize};
/// Represents a slot in a template where code can be injected.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Slot {
/// Unique identifier for this slot.
pub name: String,
/// The prompt or instruction for AI to generate code.
pub prompt: String,
/// Type of slot (determines generation behavior).
pub kind: SlotKind,
/// Optional constraints on generated code.
pub constraints: Option<SlotConstraints>,
/// Whether this slot is required.
pub required: bool,
/// Default value if AI generation fails.
pub default: Option<String>,
/// Specific temperature override for this slot (0.0 - 2.0).
pub temperature: Option<f32>,
/// Specific model override for this slot (e.g., "gpt-4o").
pub model: Option<String>,
/// Maximum tokens to generate for this slot.
pub max_tokens: Option<u32>,
}
/// The kind of slot determines how code is generated.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum SlotKind {
/// Raw code injection (no wrapper).
#[default]
Raw,
/// Function definition.
Function,
/// Class/struct definition.
Class,
/// HTML element.
Html,
/// CSS styles.
Css,
/// JavaScript code.
JavaScript,
/// Complete component (HTML + CSS + JS).
Component,
/// Custom kind with user-defined wrapper.
Custom(String),
}
/// Constraints on generated code.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct SlotConstraints {
/// Maximum lines of code.
pub max_lines: Option<usize>,
/// Maximum characters.
pub max_chars: Option<usize>,
/// Required imports or dependencies.
pub required_imports: Vec<String>,
/// Forbidden patterns (regex).
pub forbidden_patterns: Vec<String>,
/// Language hint for code generation.
pub language: Option<String>,
/// TDD Test harness. This is the code that will be used to test the generated output.
/// It should contain a placeholder like `{{CODE}}` where the generated code will be injected.
pub test_harness: Option<String>,
/// Command to execute the test harness (e.g., "cargo test", "node test.js").
pub test_command: Option<String>,
}
impl Eq for Slot {}
impl std::hash::Hash for Slot {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.name.hash(state);
self.prompt.hash(state);
self.kind.hash(state);
self.constraints.hash(state);
self.required.hash(state);
self.default.hash(state);
if let Some(temp) = self.temperature {
temp.to_bits().hash(state);
}
self.model.hash(state);
self.max_tokens.hash(state);
}
}
impl Slot {
/// Create a new slot with the given name and prompt.
///
/// # Arguments
///
/// * `name` - Unique identifier for this slot
/// * `prompt` - The instruction for AI to generate code
///
/// # Example
///
/// ```
/// use aether_core::Slot;
///
/// let slot = Slot::new("button", "Create a submit button with hover effects");
/// assert_eq!(slot.name, "button");
/// ```
pub fn new(name: impl Into<String>, prompt: impl Into<String>) -> Self {
Self {
name: name.into(),
prompt: prompt.into(),
kind: SlotKind::default(),
constraints: None,
required: true,
default: None,
temperature: None,
model: None,
max_tokens: None,
}
}
/// Set a specific temperature for this slot.
pub fn with_temperature(mut self, temp: f32) -> Self {
self.temperature = Some(temp.clamp(0.0, 2.0));
self
}
/// Set model override for this slot.
pub fn with_model(mut self, model: impl Into<String>) -> Self {
self.model = Some(model.into());
self
}
/// Set maximum tokens for this slot.
pub fn with_max_tokens(mut self, max_tokens: u32) -> Self {
self.max_tokens = Some(max_tokens);
self
}
/// Set the slot kind.
pub fn with_kind(mut self, kind: SlotKind) -> Self {
self.kind = kind;
self
}
/// Set constraints on the generated code.
pub fn with_constraints(mut self, constraints: SlotConstraints) -> Self {
self.constraints = Some(constraints);
self
}
/// Mark this slot as optional with a default value.
pub fn optional(mut self, default: impl Into<String>) -> Self {
self.required = false;
self.default = Some(default.into());
self
}
/// Validate the generated code against constraints.
pub fn validate(&self, code: &str) -> Result<(), Vec<String>> {
let mut errors = Vec::new();
if let Some(ref constraints) = self.constraints {
// Check max lines
if let Some(max) = constraints.max_lines {
let lines = code.lines().count();
if lines > max {
errors.push(format!("Code exceeds max lines: {} > {}", lines, max));
}
}
// Check max chars
if let Some(max) = constraints.max_chars {
if code.len() > max {
errors.push(format!("Code exceeds max chars: {} > {}", code.len(), max));
}
}
// Check forbidden patterns
for pattern in &constraints.forbidden_patterns {
if let Ok(re) = regex::Regex::new(pattern) {
if re.is_match(code) {
errors.push(format!("Code contains forbidden pattern: {}", pattern));
}
}
}
}
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}
}
impl SlotConstraints {
/// Create new empty constraints.
pub fn new() -> Self {
Self::default()
}
/// Set maximum lines.
pub fn max_lines(mut self, lines: usize) -> Self {
self.max_lines = Some(lines);
self
}
/// Set maximum characters.
pub fn max_chars(mut self, chars: usize) -> Self {
self.max_chars = Some(chars);
self
}
/// Set the target language.
pub fn language(mut self, lang: impl Into<String>) -> Self {
self.language = Some(lang.into());
self
}
/// Add a required import.
pub fn require_import(mut self, import: impl Into<String>) -> Self {
self.required_imports.push(import.into());
self
}
/// Add a forbidden pattern.
pub fn forbid_pattern(mut self, pattern: impl Into<String>) -> Self {
self.forbidden_patterns.push(pattern.into());
self
}
/// Set a TDD test harness.
pub fn test_harness(mut self, harness: impl Into<String>) -> Self {
self.test_harness = Some(harness.into());
self
}
/// Set a TDD test command.
pub fn test_command(mut self, command: impl Into<String>) -> Self {
self.test_command = Some(command.into());
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_slot_creation() {
let slot = Slot::new("test", "Generate a test");
assert_eq!(slot.name, "test");
assert_eq!(slot.prompt, "Generate a test");
assert!(slot.required);
}
#[test]
fn test_slot_validation() {
let slot = Slot::new("test", "")
.with_constraints(SlotConstraints::new().max_lines(5));
assert!(slot.validate("line1\nline2\nline3").is_ok());
assert!(slot.validate("1\n2\n3\n4\n5\n6").is_err());
}
}