(
&self,
input: GenerateInput,
provider_registry: &opencode_provider::ProviderRegistry,
)
| 685 | } |
| 686 | |
| 687 | pub async fn generate( |
| 688 | &self, |
| 689 | input: GenerateInput, |
| 690 | provider_registry: &opencode_provider::ProviderRegistry, |
| 691 | ) -> Result<GeneratedAgentConfig, AgentError> { |
| 692 | let model_ref = input.model.clone().ok_or(AgentError::NoDefaultModel)?; |
| 693 | |
| 694 | let provider = provider_registry |
| 695 | .get(&model_ref.provider_id) |
| 696 | .ok_or_else(|| AgentError::NoDefaultModel)?; |
| 697 | |
| 698 | let existing_names: Vec<&str> = self.agents.keys().map(|s| s.as_str()).collect(); |
| 699 | let existing_list = existing_names.join(", "); |
| 700 | |
| 701 | let user_content = format!( |
| 702 | "Create an agent configuration based on this request: \"{}\".\n\n\ |
| 703 | IMPORTANT: The following identifiers already exist and must NOT be used: {}\n\ |
| 704 | Return ONLY the JSON object, no other text, do not wrap in backticks", |
| 705 | input.description, existing_list |
| 706 | ); |
| 707 | |
| 708 | let messages = vec![ |
| 709 | opencode_provider::Message::system(PROMPT_GENERATE), |
| 710 | opencode_provider::Message::user(&user_content), |
| 711 | ]; |
| 712 | |
| 713 | let request = opencode_provider::ChatRequest::new(&model_ref.model_id, messages) |
| 714 | .with_temperature(0.3) |
| 715 | .with_stream(false); |
| 716 | |
| 717 | let response = provider.chat(request).await?; |
| 718 | |
| 719 | let content = response |
| 720 | .choices |
| 721 | .first() |
| 722 | .and_then(|c| match &c.message.content { |
| 723 | opencode_provider::Content::Text(text) => Some(text.clone()), |
| 724 | opencode_provider::Content::Parts(parts) => { |
| 725 | parts.first().and_then(|p| p.text.clone()) |
| 726 | } |
| 727 | }) |
| 728 | .unwrap_or_default(); |
| 729 | |
| 730 | let cleaned = content |
| 731 | .trim() |
| 732 | .trim_start_matches("```json") |
| 733 | .trim_start_matches("```") |
| 734 | .trim_end_matches("```") |
| 735 | .trim(); |
| 736 | |
| 737 | serde_json::from_str(cleaned) |
| 738 | .map_err(|e| AgentError::ParseError(format!("{}: {}", e, cleaned))) |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | impl Default for AgentRegistry { |
nothing calls this directly
no test coverage detected