fix: resolve metadata field mapping and return current_content in prompt template update [AC-IDSMETA-16]

This commit is contained in:
MerCry 2026-03-06 11:06:08 +08:00
parent 95365298f2
commit b832f372d1
1 changed files with 31 additions and 2 deletions

View File

@ -17,6 +17,17 @@ from app.models.entities import PromptTemplateCreate, PromptTemplateUpdate
from app.services.prompt.template_service import PromptTemplateService
from app.services.monitoring.prompt_monitor import PromptMonitor
class PromptTemplateUpdateAPI(BaseModel):
"""API model for updating prompt template with metadata field mapping."""
name: str | None = None
scene: str | None = None
description: str | None = None
system_instruction: str | None = None
variables: list[dict[str, Any]] | None = None
is_default: bool | None = None
metadata: dict[str, Any] | None = None
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/admin/prompt-templates", tags=["Prompt Management"])
@ -108,29 +119,47 @@ async def get_template_detail(
@router.put("/{tpl_id}")
async def update_template(
tpl_id: uuid.UUID,
body: PromptTemplateUpdate,
body: PromptTemplateUpdateAPI,
tenant_id: str = Depends(get_tenant_id),
session: AsyncSession = Depends(get_session),
) -> dict[str, Any]:
"""
[AC-AISVC-53] Update prompt template (creates a new version).
[AC-IDSMETA-16] Return current_content and metadata for frontend display.
"""
logger.info(f"[AC-AISVC-53] Updating template for tenant={tenant_id}, id={tpl_id}")
service = PromptTemplateService(session)
template = await service.update_template(tenant_id, tpl_id, body)
# Convert API model to entity model (metadata -> metadata_)
update_data = PromptTemplateUpdate(
name=body.name,
scene=body.scene,
description=body.description,
system_instruction=body.system_instruction,
variables=body.variables,
is_default=body.is_default,
metadata_=body.metadata,
)
template = await service.update_template(tenant_id, tpl_id, update_data)
if not template:
raise HTTPException(status_code=404, detail="Template not found")
published_version = await service.get_published_version_info(tenant_id, template.id)
# Get latest version content for current_content
latest_version = await service._get_latest_version(template.id)
return {
"id": str(template.id),
"name": template.name,
"scene": template.scene,
"description": template.description,
"is_default": template.is_default,
"current_content": latest_version.content if latest_version else None,
"metadata": template.metadata_,
"published_version": published_version,
"created_at": template.created_at.isoformat(),
"updated_at": template.updated_at.isoformat(),