Files

65 lines
2.5 KiB
Python

"""Presenting an Execution to callers: prompt text and the one-shot response."""
from __future__ import annotations
import json
from collections.abc import Callable, Mapping
from typing import Any
def describe_prompt(
prompt: Mapping[str, Any],
describe_approval: Callable[[Mapping[str, Any]], str] | None = None,
) -> str:
"""Plain-text rendering of what the user is asked, including how to answer.
`describe_approval` is the Workflow's account of what an Approval commits to,
from the details it added.
"""
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"{describe_approval(approval) if describe_approval else ''}"
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 generate_response(interaction: Any) -> dict[str, Any]:
"""The reply to a one-shot request, for a Workflow that gives none of its own."""
state = interaction.state
return {
"execution_id": interaction.execution_id,
"session_id": state.get("session_id"),
"status": interaction.status,
"deliverables": {
kind: deliverable.get("content")
for kind, deliverable in state.get("deliverables", {}).items()
},
"warnings": list(state.get("warnings", [])),
}