# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-treat-the-terminal-state-window-in-a2a-v1-0-task-lifecycle-negotiation-when-the-remote-agent-s-card-advertises-a-defaultinputmo.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-treat-the-terminal-state-window-in-a2a-v1-0-task-lifecycle-negotiation-when-the-remote-agent-s-card-advertises-a-defaultinputmo.warrant.json
# Markdown SHA-256:   70461319bf31aa8ce58330d5084ce83d5176cb05024f13fc97b9febe11b4121d
# Code AST SHA-256:   e46563f58e243e8e809f9a519ecf4aae3f0497b92f873b19afd05a923cd84a5c
# Verified Classes:   ArtifactInputModeMismatch, AgentCard
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Iterable


class ArtifactInputModeMismatch(RuntimeError):
    """Raised when a produced artifact's MIME type is not an accepted input mode
    of the remote A2A v1.0 agent card, so it cannot be echoed in a continuation."""


# A2A v1.0: when defaultInputModes is omitted, the implicit default is text/plain.
IMPLICIT_INPUT_MODES = frozenset({"text/plain"})


@dataclass(frozen=True)
class AgentCard:
    default_input_modes: Iterable[str] | None = None
    default_output_modes: Iterable[str] = field(default_factory=lambda: ["text/plain"])

    def accepted_input_modes(self) -> frozenset[str]:
        if self.default_input_modes is None:
            return IMPLICIT_INPUT_MODES
        return frozenset(self.default_input_modes)

    def output_only_modes(self) -> frozenset[str]:
        return frozenset(self.default_output_modes) - self.accepted_input_modes()


def build_continuation_parts(
    card: AgentCard,
    artifact_mime: str,
    artifact_ref: str,
    summary: str,
) -> list[dict]:
    """Construct input parts for a tasks/send continuation.

    If the artifact MIME type is accepted by the remote card, embed it as a file
    part. Otherwise pass a text/plain reference so the remote can address it
    without violating its declared input modes.
    """
    accepted = card.accepted_input_modes()
    if artifact_mime in accepted:
        return [
            {"type": "file", "mimeType": artifact_mime, "uri": artifact_ref},
            {"type": "text", "text": summary},
        ]

    # Artifact is output-only for this remote: reference-passing only.
    if "text/plain" not in accepted:
        raise ArtifactInputModeMismatch(
            f"remote card accepts {sorted(accepted)} but neither the artifact "
            f"({artifact_mime}) nor a text/plain reference; cannot continue task"
        )

    return [
        {
            "type": "text",
            "text": f"artifact_ref={artifact_ref} mime={artifact_mime}\n{summary}",
        }
    ]


def assert_roundtrippable(card: AgentCard, artifact_mime: str) -> None:
    """Fail fast when the task plan requires value-passing of a binary artifact."""
    if artifact_mime not in card.accepted_input_modes():
        raise ArtifactInputModeMismatch(
            f"artifact mime {artifact_mime!r} is output-only for remote "
            f"(output_only={sorted(card.output_only_modes())}); "
            f"task plan requires value-passing, not reference-passing"
        )
