agent.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from datetime import datetime
  2. from typing import TYPE_CHECKING
  3. from pydantic import BaseModel, ConfigDict, Field
  4. from core_domain import (
  5. AgentDefinitionContract,
  6. AgentMemoryPolicyContract,
  7. AgentModelConfigContract,
  8. AgentRunContract,
  9. AgentRunStatus,
  10. AgentSkillRefContract,
  11. AgentStatus,
  12. AgentToolRefContract,
  13. AgentVersionContract,
  14. AgentVersionStatus,
  15. )
  16. from core_shared import JSONValue
  17. if TYPE_CHECKING:
  18. from app.db.models import AgentDefinition, AgentRun, AgentVersion
  19. class AgentCreateRequest(BaseModel):
  20. tenant_id: str
  21. code: str
  22. name: str
  23. description: str | None = None
  24. agent_type: str = "assistant"
  25. owner_user_id: str | None = None
  26. metadata_json: dict[str, JSONValue] = Field(default_factory=dict)
  27. class AgentStatusUpdateRequest(BaseModel):
  28. tenant_id: str
  29. status: AgentStatus
  30. class AgentResponse(AgentDefinitionContract):
  31. @classmethod
  32. def from_entity(cls, entity: "AgentDefinition") -> "AgentResponse":
  33. return cls.model_validate(entity, from_attributes=True)
  34. class AgentVersionCreateRequest(BaseModel):
  35. model_config = ConfigDict(populate_by_name=True)
  36. tenant_id: str
  37. agent_id: str
  38. status: AgentVersionStatus = "draft"
  39. role: str = "assistant"
  40. goal: str | None = None
  41. system_prompt: str
  42. model_config_data: AgentModelConfigContract = Field(
  43. default_factory=AgentModelConfigContract,
  44. alias="model_config",
  45. )
  46. memory_policy: AgentMemoryPolicyContract = Field(default_factory=AgentMemoryPolicyContract)
  47. tool_refs: list[AgentToolRefContract] = Field(default_factory=list)
  48. skill_refs: list[AgentSkillRefContract] = Field(default_factory=list)
  49. class AgentVersionResponse(AgentVersionContract):
  50. @classmethod
  51. def from_entity(cls, entity: "AgentVersion") -> "AgentVersionResponse":
  52. return cls.model_validate(entity, from_attributes=True)
  53. class AgentRunCreateRequest(BaseModel):
  54. tenant_id: str
  55. agent_id: str
  56. agent_version_id: str | None = None
  57. session_id: str | None = None
  58. input_text: str | None = None
  59. input_json: dict[str, JSONValue] | None = None
  60. class AgentRunStatusUpdateRequest(BaseModel):
  61. tenant_id: str
  62. status: AgentRunStatus
  63. worker_key: str | None = None
  64. output_text: str | None = None
  65. output_json: dict[str, JSONValue] | None = None
  66. error_code: str | None = None
  67. error_message: str | None = None
  68. class AgentRunExecuteRequest(BaseModel):
  69. tenant_id: str
  70. worker_key: str | None = None
  71. dry_run: bool = False
  72. class AgentWorkerExecuteNextRequest(BaseModel):
  73. worker_key: str
  74. lease_seconds: int | None = Field(default=None, gt=0)
  75. dry_run: bool | None = None
  76. class AgentRunResponse(AgentRunContract):
  77. @classmethod
  78. def from_entity(cls, entity: "AgentRun") -> "AgentRunResponse":
  79. return cls.model_validate(entity, from_attributes=True)
  80. class AgentRunExecuteResponse(BaseModel):
  81. run: AgentRunResponse
  82. model: str | None = None
  83. dry_run: bool = False
  84. class AgentWorkerExecuteNextResponse(BaseModel):
  85. run: AgentRunResponse
  86. model: str | None = None
  87. dry_run: bool = False
  88. released_lease_count: int = 0
  89. class AgentHealthResponse(BaseModel):
  90. service: str
  91. status: str
  92. database: str
  93. checked_time: datetime