ports.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """Application-owned service ports used by the framework runtime."""
  2. from __future__ import annotations
  3. from typing import Any, Literal, Protocol, Sequence
  4. from pydantic import Field
  5. from cyber_agent.application.models import ApplicationModel, ApplicationRef
  6. from cyber_agent.core.task_protocol import ContextRef, TaskBrief
  7. class ContextRequest(ApplicationModel):
  8. application_ref: ApplicationRef
  9. root_trace_id: str = Field(min_length=1)
  10. trace_id: str = Field(min_length=1)
  11. parent_trace_id: str | None = None
  12. uid: str | None = None
  13. role_id: str = Field(min_length=1)
  14. task_brief: TaskBrief | None = None
  15. task_brief_revision: int = Field(default=0, ge=0)
  16. authorized_context_refs: tuple[ContextRef, ...] = ()
  17. class ContextResourceDescriptor(ApplicationModel):
  18. application_ref: ApplicationRef
  19. root_trace_id: str = Field(min_length=1)
  20. trace_id: str = Field(min_length=1)
  21. uid: str | None = None
  22. role_id: str = Field(min_length=1)
  23. source_id: str = Field(min_length=1, max_length=500)
  24. source_version: str = Field(min_length=1, max_length=200)
  25. content_hash: str = Field(pattern=r"^[0-9a-f]{64}$")
  26. kind: str = Field(min_length=1, max_length=200)
  27. summary: str = Field(min_length=1, max_length=500)
  28. inheritance: Literal["task_only", "inheritable"] = "task_only"
  29. priority: int = 0
  30. estimated_chars: int = Field(default=0, ge=0)
  31. authorization: dict[str, Any] = Field(default_factory=dict)
  32. class ContextMaterial(ApplicationModel):
  33. descriptor: ContextResourceDescriptor
  34. content: dict[str, Any]
  35. class TaskContextProvider(Protocol):
  36. async def list_context(
  37. self,
  38. request: ContextRequest,
  39. ) -> Sequence[ContextResourceDescriptor]: ...
  40. async def resolve_context(
  41. self,
  42. request: ContextRequest,
  43. descriptor: ContextResourceDescriptor,
  44. ) -> ContextMaterial: ...
  45. class RunEventProjector(Protocol):
  46. """Application projection port; cursor movement is part of its transaction."""
  47. async def load_cursor(
  48. self,
  49. application_ref: ApplicationRef,
  50. root_trace_id: str,
  51. ) -> int: ...
  52. async def project(self, event: Any, expected_cursor: int) -> None: ...
  53. async def advance_cursor(
  54. self,
  55. application_ref: ApplicationRef,
  56. root_trace_id: str,
  57. event_id: int,
  58. expected_cursor: int,
  59. ) -> None:
  60. """Atomically skip an unsupported event without updating business views."""
  61. ...