This commit is contained in:
141
app/presentation.py
Normal file
141
app/presentation.py
Normal file
@@ -0,0 +1,141 @@
|
||||
"""Presenting an Execution to callers: prompt text and the /generate response shape."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
from collections.abc import Mapping, Sequence
|
||||
from typing import Any
|
||||
|
||||
from app.adk.runtime import RoleSpec
|
||||
from app.agent import Interaction
|
||||
from app.workflows.deliverables import DEPENDENCY_GRAPH, DeliverableKind
|
||||
|
||||
|
||||
def describe_prompt(prompt: Mapping[str, Any]) -> str:
|
||||
"""Plain-text rendering of what the user is asked, including how to answer."""
|
||||
if "approval" in prompt:
|
||||
approval = prompt["approval"]
|
||||
reply = {"approval": {"phase": approval["phase"], "decision": "approve"}}
|
||||
changes = {
|
||||
"approval": {
|
||||
"phase": approval["phase"],
|
||||
"decision": "changes",
|
||||
"feedback": "<what to change>",
|
||||
}
|
||||
}
|
||||
return (
|
||||
f"Please review the {approval['phase']} Phase: {approval['summary']}\n\n"
|
||||
f"Deliverables: {', '.join(approval['deliverables'])}.\n\n"
|
||||
f"To approve, reply with the data {json.dumps(reply)}. "
|
||||
f"To request changes, reply with {json.dumps(changes)}. "
|
||||
"A text reply is recorded as conversation and never approves."
|
||||
)
|
||||
if "permission" in prompt:
|
||||
permission = prompt["permission"]
|
||||
grant = {"permission": {permission["tier"]: True}}
|
||||
return (
|
||||
"May I read live resources in your cloud environment to discover the current "
|
||||
f"state? Requested by: {', '.join(permission['requested_by'])}.\n\n"
|
||||
f"To allow, reply with the data {json.dumps(grant)}; to refuse, use false."
|
||||
)
|
||||
lines = [
|
||||
f"{index}. ({item['from']}) {item['question']}"
|
||||
for index, item in enumerate(prompt.get("questions", []), start=1)
|
||||
]
|
||||
return "\n".join(lines) + "\n\nReply in text; answer every question you can."
|
||||
|
||||
|
||||
def to_generate_response(
|
||||
interaction: Interaction, roles: Mapping[str, RoleSpec]
|
||||
) -> dict[str, Any]:
|
||||
"""Map an Execution onto the legacy /generate response keys."""
|
||||
state = interaction.state
|
||||
deliverables: Mapping[str, Mapping[str, Any]] = state.get("deliverables", {})
|
||||
ledger = state.get("ledger", {})
|
||||
|
||||
def content(kind: DeliverableKind) -> str | None:
|
||||
item = deliverables.get(kind.value)
|
||||
return item["content"] if item else None
|
||||
|
||||
def attachment(kind: DeliverableKind, name: str) -> str | None:
|
||||
item = deliverables.get(kind.value)
|
||||
return item.get("attachments", {}).get(name) if item else None
|
||||
|
||||
requirements = content(DeliverableKind.REQUIREMENTS)
|
||||
revisions = sum(
|
||||
d.get("revisions", 0) for d in ledger.get("deliverables", {}).values()
|
||||
)
|
||||
submitted = [kind for kind in DeliverableKind if kind.value in deliverables]
|
||||
skills = _skills_used(submitted, roles)
|
||||
status = {
|
||||
"completed": "success",
|
||||
"completed_with_warnings": "completed_with_warnings",
|
||||
}.get(interaction.status, interaction.status)
|
||||
|
||||
return {
|
||||
"execution_id": interaction.execution_id,
|
||||
"session_id": state.get("session_id"),
|
||||
"status": status,
|
||||
"validation_passed": interaction.status == "completed",
|
||||
"total_loop_iterations": revisions + 1,
|
||||
"current_phase": _current_phase(ledger),
|
||||
"artifacts": {
|
||||
"source_discovery_doc": _section(requirements, "Current state"),
|
||||
"source_mermaid_diagram": None,
|
||||
"requirements_doc": requirements,
|
||||
"architecture_doc": content(DeliverableKind.ARCHITECTURE_DESCRIPTION),
|
||||
"mermaid_diagram": attachment(
|
||||
DeliverableKind.ARCHITECTURE_DIAGRAM, "architecture.mmd"
|
||||
),
|
||||
"terraform_code": attachment(
|
||||
DeliverableKind.DEPLOYMENT_GUIDANCE, "terraform/main.tf"
|
||||
),
|
||||
"validation_results": content(DeliverableKind.VALIDATION_RESULTS),
|
||||
"solution_guide": content(DeliverableKind.SOLUTION_GUIDE),
|
||||
},
|
||||
"active_skills": skills,
|
||||
"warnings": list(state.get("warnings", [])),
|
||||
}
|
||||
|
||||
|
||||
def _skills_used(
|
||||
submitted: Sequence[DeliverableKind], roles: Mapping[str, RoleSpec]
|
||||
) -> list[str]:
|
||||
names: list[str] = []
|
||||
for kind in submitted:
|
||||
role = roles.get(DEPENDENCY_GRAPH[kind].specialist)
|
||||
for skill in role.skills if role else ():
|
||||
if skill not in names:
|
||||
names.append(skill)
|
||||
return names
|
||||
|
||||
|
||||
def _current_phase(ledger: Mapping[str, Any]) -> str | None:
|
||||
phases: Mapping[str, str] = ledger.get("phases", {})
|
||||
for phase, status in phases.items():
|
||||
if status != "approved":
|
||||
return phase
|
||||
return list(phases)[-1] if phases else None
|
||||
|
||||
|
||||
def _section(markdown: str | None, heading: str) -> str | None:
|
||||
"""The body under a heading (any level, numbering ignored), up to the next heading."""
|
||||
if not markdown:
|
||||
return None
|
||||
lines = markdown.splitlines()
|
||||
pattern = re.compile(
|
||||
rf"^#{{1,6}}\s+(?:\d+(?:\.\d+)*\.?\s+)?{re.escape(heading)}\s*$", re.IGNORECASE
|
||||
)
|
||||
for index, line in enumerate(lines):
|
||||
if pattern.match(line.strip()):
|
||||
level = len(line.strip()) - len(line.strip().lstrip("#"))
|
||||
body = []
|
||||
for following in lines[index + 1 :]:
|
||||
stripped = following.strip()
|
||||
hashes = len(stripped) - len(stripped.lstrip("#"))
|
||||
if stripped.startswith("#") and hashes <= level:
|
||||
break
|
||||
body.append(following)
|
||||
return "\n".join(body).strip() or None
|
||||
return None
|
||||
Reference in New Issue
Block a user