agent.py 3.5 KB

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