feat(templates): nom d'agent par défaut + permissions système et MCP outils sur AgentTemplate — #242
AgentTemplate étendu (defaultAgentName, systemPermissions, mcpToolPermissions), CreateTemplate/UpdateTemplate/CreateAgentFromTemplate projetés vers l'agent créé, DTO/MCP tools et persistance store complétés. Correctif modèle par défaut: UpdateTemplate ne persistait pas defaultProfileId (profil stale à la création d'agent). Backfill best-effort des templates existants depuis les agents du projet au open_project, overrides explicites uniquement.
This commit is contained in:
@ -13,10 +13,14 @@
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use domain::ports::{AgentContextStore, EventBus, IdGenerator, StoreError, TemplateStore};
|
||||
use domain::ports::{
|
||||
AgentContextStore, EventBus, IdGenerator, McpToolPermissionStore, StoreError,
|
||||
SystemPermissionStore, TemplateStore,
|
||||
};
|
||||
use domain::{
|
||||
Agent, AgentId, AgentManifest, AgentOrigin, AgentTemplate, DomainEvent, ManifestEntry,
|
||||
MarkdownDoc, ProfileId, Project, TemplateId, TemplateVersion,
|
||||
MarkdownDoc, McpToolPolicy, ProfileId, Project, SystemPermissionSet, TemplateId,
|
||||
TemplateVersion,
|
||||
};
|
||||
|
||||
use crate::agent::unique_md_path;
|
||||
@ -35,6 +39,12 @@ pub struct CreateTemplateInput {
|
||||
pub content: String,
|
||||
/// Default runtime profile for agents created from this template.
|
||||
pub default_profile_id: ProfileId,
|
||||
/// Optional default agent name for agents created from this template.
|
||||
pub default_agent_name: Option<String>,
|
||||
/// Optional agent-level system permission override applied at creation time.
|
||||
pub default_system_permissions: Option<SystemPermissionSet>,
|
||||
/// Optional agent-level MCP tool permission override applied at creation time.
|
||||
pub default_mcp_tool_policy: Option<McpToolPolicy>,
|
||||
}
|
||||
|
||||
/// Output of [`CreateTemplate::execute`].
|
||||
@ -69,10 +79,19 @@ impl CreateTemplate {
|
||||
let id = TemplateId::from_uuid(self.ids.new_uuid());
|
||||
let template = AgentTemplate::new(
|
||||
id,
|
||||
input.name,
|
||||
input.name.clone(),
|
||||
MarkdownDoc::new(input.content),
|
||||
input.default_profile_id,
|
||||
)
|
||||
.and_then(|template| {
|
||||
template.with_metadata(
|
||||
input.name,
|
||||
input.default_profile_id,
|
||||
input.default_agent_name,
|
||||
input.default_system_permissions,
|
||||
input.default_mcp_tool_policy,
|
||||
)
|
||||
})
|
||||
.map_err(|e| AppError::Invalid(e.to_string()))?;
|
||||
self.templates.save(&template).await?;
|
||||
Ok(CreateTemplateOutput { template })
|
||||
@ -88,8 +107,21 @@ impl CreateTemplate {
|
||||
pub struct UpdateTemplateInput {
|
||||
/// Template to update.
|
||||
pub template_id: TemplateId,
|
||||
/// New Markdown content.
|
||||
pub content: String,
|
||||
/// New Markdown content. `None` leaves content and version untouched.
|
||||
pub content: Option<String>,
|
||||
/// New display name. `None` leaves the current value.
|
||||
pub name: Option<String>,
|
||||
/// New default runtime profile. `None` leaves the current value.
|
||||
pub default_profile_id: Option<ProfileId>,
|
||||
/// New default agent name. Outer `None` leaves the current value; inner
|
||||
/// `None` clears the default so agent creation falls back to template name.
|
||||
pub default_agent_name: Option<Option<String>>,
|
||||
/// New default system permissions. Outer `None` leaves the current value;
|
||||
/// inner `None` preserves project inheritance for future agents.
|
||||
pub default_system_permissions: Option<Option<SystemPermissionSet>>,
|
||||
/// New default MCP tool policy. Outer `None` leaves the current value; inner
|
||||
/// `None` preserves project inheritance for future agents.
|
||||
pub default_mcp_tool_policy: Option<Option<McpToolPolicy>>,
|
||||
}
|
||||
|
||||
/// Output of [`UpdateTemplate::execute`].
|
||||
@ -123,7 +155,28 @@ impl UpdateTemplate {
|
||||
input: UpdateTemplateInput,
|
||||
) -> Result<UpdateTemplateOutput, AppError> {
|
||||
let current = self.templates.get(input.template_id).await?;
|
||||
let updated = current.with_updated_content(MarkdownDoc::new(input.content));
|
||||
let mut updated = if let Some(content) = input.content {
|
||||
current.with_updated_content(MarkdownDoc::new(content))
|
||||
} else {
|
||||
current
|
||||
};
|
||||
updated = updated
|
||||
.with_metadata(
|
||||
input.name.unwrap_or_else(|| updated.name.clone()),
|
||||
input
|
||||
.default_profile_id
|
||||
.unwrap_or(updated.default_profile_id),
|
||||
input
|
||||
.default_agent_name
|
||||
.unwrap_or_else(|| updated.default_agent_name.clone()),
|
||||
input
|
||||
.default_system_permissions
|
||||
.unwrap_or_else(|| updated.default_system_permissions.clone()),
|
||||
input
|
||||
.default_mcp_tool_policy
|
||||
.unwrap_or_else(|| updated.default_mcp_tool_policy.clone()),
|
||||
)
|
||||
.map_err(|e| AppError::Invalid(e.to_string()))?;
|
||||
self.templates.save(&updated).await?;
|
||||
self.events.publish(DomainEvent::TemplateUpdated {
|
||||
template_id: updated.id,
|
||||
@ -271,6 +324,8 @@ pub struct CreateAgentFromTemplate {
|
||||
contexts: Arc<dyn AgentContextStore>,
|
||||
ids: Arc<dyn IdGenerator>,
|
||||
events: Arc<dyn EventBus>,
|
||||
system_permissions: Option<Arc<dyn SystemPermissionStore>>,
|
||||
mcp_tool_permissions: Option<Arc<dyn McpToolPermissionStore>>,
|
||||
}
|
||||
|
||||
impl CreateAgentFromTemplate {
|
||||
@ -287,9 +342,30 @@ impl CreateAgentFromTemplate {
|
||||
contexts,
|
||||
ids,
|
||||
events,
|
||||
system_permissions: None,
|
||||
mcp_tool_permissions: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Injects the project system permission store used to project template
|
||||
/// agent overrides at creation time.
|
||||
#[must_use]
|
||||
pub fn with_system_permission_store(mut self, store: Arc<dyn SystemPermissionStore>) -> Self {
|
||||
self.system_permissions = Some(store);
|
||||
self
|
||||
}
|
||||
|
||||
/// Injects the project MCP tool permission store used to project template
|
||||
/// agent overrides at creation time.
|
||||
#[must_use]
|
||||
pub fn with_mcp_tool_permission_store(
|
||||
mut self,
|
||||
store: Arc<dyn McpToolPermissionStore>,
|
||||
) -> Self {
|
||||
self.mcp_tool_permissions = Some(store);
|
||||
self
|
||||
}
|
||||
|
||||
/// Executes creation from a template.
|
||||
///
|
||||
/// # Errors
|
||||
@ -303,7 +379,10 @@ impl CreateAgentFromTemplate {
|
||||
let template = self.templates.get(input.template_id).await?;
|
||||
let manifest = self.contexts.load_manifest(&input.project).await?;
|
||||
|
||||
let name = input.name.unwrap_or_else(|| template.name.clone());
|
||||
let name = input
|
||||
.name
|
||||
.or_else(|| template.default_agent_name.clone())
|
||||
.unwrap_or_else(|| template.name.clone());
|
||||
let id = AgentId::from_uuid(self.ids.new_uuid());
|
||||
let md_path = unique_md_path(&name, &manifest);
|
||||
let origin = AgentOrigin::FromTemplate {
|
||||
@ -333,6 +412,27 @@ impl CreateAgentFromTemplate {
|
||||
.write_context(&input.project, &agent.id, &template.content_md)
|
||||
.await?;
|
||||
|
||||
if let Some(permissions) = template.default_system_permissions.clone() {
|
||||
if let Some(store) = &self.system_permissions {
|
||||
let mut doc = store.load_system_permissions(&input.project).await?;
|
||||
doc.set_agent_permissions(agent.id, Some(permissions));
|
||||
store.save_system_permissions(&input.project, &doc).await?;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(policy) = template.default_mcp_tool_policy.clone() {
|
||||
if let Some(store) = &self.mcp_tool_permissions {
|
||||
let mut doc = store.load_mcp_tool_permissions(&input.project).await?;
|
||||
doc.agents
|
||||
.retain(|override_| override_.agent_id != agent.id);
|
||||
doc.agents
|
||||
.push(domain::AgentMcpToolPolicyOverride::new(agent.id, policy));
|
||||
store
|
||||
.save_mcp_tool_permissions(&input.project, &doc)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
self.events.publish(DomainEvent::LayoutChanged {
|
||||
project_id: input.project.id,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user