schema.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from __future__ import annotations
  2. from typing import Any, Literal, TypedDict
  3. Completeness = Literal["complete", "partial", "missing"]
  4. Resolution = Literal["resolved", "partial", "missing", "unsafe"]
  5. class EvidenceSpec(TypedDict):
  6. availability: Literal[
  7. "direct-read",
  8. "returned-to-agent",
  9. "available-upstream",
  10. "produced-by-run",
  11. "visualization-derived",
  12. "unavailable",
  13. ]
  14. adoption: Literal[
  15. "explicitly-adopted",
  16. "explicitly-rejected",
  17. "not-recorded",
  18. "not-applicable",
  19. ]
  20. confidence: Literal["exact", "safe-association", "deterministic", "unconfirmed"]
  21. class SourceBinding(TypedDict):
  22. id: str
  23. sourceId: str
  24. role: Literal["input", "basis", "constraint", "process", "output", "status"]
  25. selector: dict[str, Any]
  26. transform: dict[str, Any]
  27. evidence: EvidenceSpec
  28. resolution: Resolution
  29. class SourceRecord(TypedDict, total=False):
  30. id: str
  31. kind: Literal[
  32. "database",
  33. "runtime-event",
  34. "artifact",
  35. "log-anchor",
  36. "calculation",
  37. "historical-fallback",
  38. "missing",
  39. ]
  40. label: str
  41. locator: dict[str, Any]
  42. status: str
  43. durationMs: int | None
  44. completeness: Completeness
  45. resultState: Literal["present", "empty", "missing", "unavailable"]
  46. truncated: bool
  47. omittedCharacters: int
  48. selectedValues: list[dict[str, Any]]
  49. rawRecord: Any
  50. def unresolved_binding(
  51. binding_id: str,
  52. *,
  53. reason: str,
  54. role: str = "basis",
  55. source_id: str | None = None,
  56. resolution: Resolution = "missing",
  57. code: str | None = None,
  58. ) -> SourceBinding:
  59. selector = {"kind": "unresolved", "reason": reason}
  60. if code:
  61. selector["code"] = code
  62. return {
  63. "id": binding_id,
  64. "sourceId": source_id or f"missing:{binding_id}",
  65. "role": role, # type: ignore[typeddict-item]
  66. "selector": selector,
  67. "transform": {"kind": "direct"},
  68. "evidence": {
  69. "availability": "unavailable",
  70. "adoption": "not-recorded",
  71. "confidence": "unconfirmed",
  72. },
  73. "resolution": resolution,
  74. }