(&mut self, visibility: Visibility)
| 255 | } |
| 256 | |
| 257 | fn agent_declaration(&mut self, visibility: Visibility) -> Option<Stmt<'gc>> { |
| 258 | self.consume(TokenType::Identifier, "Expect agent name."); |
| 259 | let name = self.previous; |
| 260 | self.scopes.push(name.lexeme.to_owned()); |
| 261 | self.consume(TokenType::OpenBrace, "Expect '{' before agent body."); |
| 262 | let mut fields = HashMap::new(); |
| 263 | while !self.check(TokenType::CloseBrace) && !self.check(TokenType::Fn) && !self.is_at_end() |
| 264 | { |
| 265 | let (key, value) = self.field_declaration()?; |
| 266 | match key.lexeme { |
| 267 | "instructions" | "model" | "tool_choice" => { |
| 268 | if !matches!( |
| 269 | value, |
| 270 | Expr::Literal { |
| 271 | value: Literal::String { .. }, |
| 272 | .. |
| 273 | } |
| 274 | ) { |
| 275 | self.error(&format!( |
| 276 | "Field '{}' in agent declaration should be a string.", |
| 277 | key.lexeme |
| 278 | )); |
| 279 | continue; |
| 280 | } |
| 281 | } |
| 282 | "tools" => { |
| 283 | if !matches!(value, Expr::List { .. }) { |
| 284 | self.error("Field 'tools' in agent declaration should be an array."); |
| 285 | continue; |
| 286 | } |
| 287 | } |
| 288 | invalid => self.error_at( |
| 289 | key, |
| 290 | &format!("Invalid field '{}' in agent declaration.", invalid), |
| 291 | ), |
| 292 | } |
| 293 | |
| 294 | if fields.contains_key(key.lexeme) { |
| 295 | self.error_at( |
| 296 | key, |
| 297 | &format!("Duplicate field '{}' in agent declaration.", key.lexeme), |
| 298 | ); |
| 299 | continue; |
| 300 | } |
| 301 | fields.insert(key.lexeme, value); |
| 302 | // Consume comma after field declaration |
| 303 | if !self.check(TokenType::CloseBrace) { |
| 304 | self.consume(TokenType::Comma, "Expect ',' after field declaration."); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | // Check for required fields |
| 309 | let required_fields = ["instructions"]; |
| 310 | for field in required_fields { |
| 311 | if !fields.contains_key(field) { |
| 312 | self.error(&format!( |
| 313 | "Missing required field '{}' in agent declaration.", |
| 314 | field |
no test coverage detected