agent.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. from datetime import datetime
  2. from typing import TYPE_CHECKING
  3. from core_domain import (
  4. AgentDefinitionContract,
  5. AgentMemoryPolicyContract,
  6. AgentModelConfigContract,
  7. AgentRunContract,
  8. AgentRunStatus,
  9. AgentSkillRefContract,
  10. AgentStatus,
  11. AgentToolInvocationContract,
  12. AgentToolRefContract,
  13. AgentVersionContract,
  14. AgentVersionStatus,
  15. )
  16. from core_shared import JSONValue
  17. from pydantic import BaseModel, ConfigDict, Field
  18. if TYPE_CHECKING:
  19. from app.db.models import AgentDefinition, AgentRun, AgentToolInvocation, AgentVersion
  20. class AgentCreateRequest(BaseModel):
  21. code: str | None = None
  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 AgentUpdateRequest(BaseModel):
  28. agent_id: str
  29. name: str | None = None
  30. description: str | None = None
  31. metadata_json: dict[str, JSONValue] | None = None
  32. class AgentStatusUpdateRequest(BaseModel):
  33. status: AgentStatus
  34. class AgentResponse(AgentDefinitionContract):
  35. @classmethod
  36. def from_entity(cls, entity: "AgentDefinition") -> "AgentResponse":
  37. return cls.model_validate(entity, from_attributes=True)
  38. class AgentConfigListRequest(BaseModel):
  39. agent_id: str
  40. class AgentConfigCreateRequest(BaseModel):
  41. model_config = ConfigDict(populate_by_name=True)
  42. agent_id: str
  43. role: str = "assistant"
  44. goal: str | None = None
  45. system_prompt: str
  46. model_config_data: AgentModelConfigContract = Field(
  47. default_factory=AgentModelConfigContract,
  48. alias="model_config")
  49. memory_policy: AgentMemoryPolicyContract = Field(default_factory=AgentMemoryPolicyContract)
  50. tool_refs: list[AgentToolRefContract] = Field(default_factory=list)
  51. skill_refs: list[AgentSkillRefContract] = Field(default_factory=list)
  52. class AgentVersionCreateRequest(AgentConfigCreateRequest):
  53. status: AgentVersionStatus = "draft"
  54. class AgentVersionResponse(AgentVersionContract):
  55. @classmethod
  56. def from_entity(cls, entity: "AgentVersion") -> "AgentVersionResponse":
  57. return cls.model_validate(entity, from_attributes=True)
  58. class AgentConfigResponse(BaseModel):
  59. id: str
  60. agent_id: str
  61. role: str
  62. goal: str | None = None
  63. system_prompt: str
  64. model_config_json: dict[str, JSONValue]
  65. memory_policy_json: dict[str, JSONValue]
  66. tool_refs_json: list[dict[str, JSONValue]]
  67. skill_refs_json: list[dict[str, JSONValue]]
  68. created_time: datetime
  69. @classmethod
  70. def from_entity(cls, entity: "AgentVersion") -> "AgentConfigResponse":
  71. return cls(
  72. id=entity.id,
  73. agent_id=entity.agent_id,
  74. role=entity.role,
  75. goal=entity.goal,
  76. system_prompt=entity.system_prompt,
  77. model_config_json=entity.model_config_json,
  78. memory_policy_json=entity.memory_policy_json,
  79. tool_refs_json=entity.tool_refs_json,
  80. skill_refs_json=entity.skill_refs_json,
  81. created_time=entity.created_time)
  82. class AgentRunCreateRequest(BaseModel):
  83. agent_id: str
  84. agent_version_id: str | None = None
  85. agent_config_id: str | None = None
  86. session_id: str | None = None
  87. input_text: str | None = None
  88. input_json: dict[str, JSONValue] | None = None
  89. class AgentRunDetailRequest(BaseModel):
  90. agent_run_id: str
  91. class AgentRunStatusUpdateRequest(BaseModel):
  92. status: AgentRunStatus
  93. worker_key: str | None = None
  94. output_text: str | None = None
  95. output_json: dict[str, JSONValue] | None = None
  96. error_code: str | None = None
  97. error_message: str | None = None
  98. class AgentRunExecuteRequest(BaseModel):
  99. worker_key: str | None = None
  100. dry_run: bool = False
  101. class AgentWorkerExecuteNextRequest(BaseModel):
  102. worker_key: str
  103. lease_seconds: int | None = Field(default=None, gt=0)
  104. dry_run: bool | None = None
  105. class AgentRunResponse(AgentRunContract):
  106. @classmethod
  107. def from_entity(cls, entity: "AgentRun") -> "AgentRunResponse":
  108. return cls.model_validate(entity, from_attributes=True)
  109. class AgentToolInvocationResponse(AgentToolInvocationContract):
  110. @classmethod
  111. def from_entity(cls, entity: "AgentToolInvocation") -> "AgentToolInvocationResponse":
  112. return cls.model_validate(entity, from_attributes=True)
  113. class AgentRunExecuteResponse(BaseModel):
  114. run: AgentRunResponse
  115. model: str | None = None
  116. dry_run: bool = False
  117. class AgentWorkerExecuteNextResponse(BaseModel):
  118. run: AgentRunResponse
  119. model: str | None = None
  120. dry_run: bool = False
  121. released_lease_count: int = 0
  122. class AgentHealthResponse(BaseModel):
  123. service: str
  124. status: str
  125. database: str
  126. checked_time: datetime