Skip to content

The platform for continuously improving AI agents.

1.6 KB
Raw Blame
from datetime import datetime
from typing import Literal
from uuid import UUID

from pydantic import Field, model_validator

from agent_runtime.contracts import Contract


class SDKProjection(Contract):
    version: Literal[1] = 1
    id: UUID
    parent_id: UUID | None
    timestamp: datetime
    kind: Literal["system", "message", "action", "observation", "context", "error", "tool_error"]
    text: str = ""
    tool: str | None = Field(default=None, max_length=100)
    tool_call_id: str | None = Field(default=None, max_length=256)
    classification: dict | None = None
    arguments: dict | None = None
    action_id: UUID | None = None
    observation: dict | None = None
    sender: str | None = Field(default=None, max_length=128)
    role: Literal["user", "assistant", "system", "tool"] | None = None

    @model_validator(mode="after")
    def event_shape(self):
        if self.timestamp.tzinfo is None:
            raise ValueError("An SDK event timestamp requires a timezone")
        if self.kind == "action" and (not self.tool or self.arguments is None):
            raise ValueError("An action requires its tool and original arguments")
        if self.kind == "observation" and (
            not self.tool or self.action_id is None or self.observation is None
        ):
            raise ValueError("An observation requires its action identity and result")
        if self.kind == "message" and self.role is None:
            raise ValueError("A message requires its role")
        if self.kind == "tool_error" and (not self.tool or not self.tool_call_id):
            raise ValueError("A tool error requires its tool call identity")
        return self