agent.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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
  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. status: AgentStatus
  29. class AgentResponse(AgentDefinitionContract):
  30. @classmethod
  31. def from_entity(cls, entity: "AgentDefinition") -> "AgentResponse":
  32. return cls.model_validate(entity, from_attributes=True)
  33. class AgentVersionCreateRequest(BaseModel):
  34. model_config = ConfigDict(populate_by_name=True)
  35. agent_id: str
  36. status: AgentVersionStatus = "draft"
  37. role: str = "assistant"
  38. goal: str | None = None
  39. system_prompt: str
  40. model_config_data: AgentModelConfigContract = Field(
  41. default_factory=AgentModelConfigContract,
  42. alias="model_config")
  43. memory_policy: AgentMemoryPolicyContract = Field(default_factory=AgentMemoryPolicyContract)
  44. tool_refs: list[AgentToolRefContract] = Field(default_factory=list)
  45. skill_refs: list[AgentSkillRefContract] = Field(default_factory=list)
  46. class AgentVersionResponse(AgentVersionContract):
  47. @classmethod
  48. def from_entity(cls, entity: "AgentVersion") -> "AgentVersionResponse":
  49. return cls.model_validate(entity, from_attributes=True)
  50. class AgentRunCreateRequest(BaseModel):
  51. agent_id: str
  52. agent_version_id: str | None = None
  53. session_id: str | None = None
  54. input_text: str | None = None
  55. input_json: dict[str, JSONValue] | None = None
  56. class AgentRunStatusUpdateRequest(BaseModel):
  57. status: AgentRunStatus
  58. worker_key: str | None = None
  59. output_text: str | None = None
  60. output_json: dict[str, JSONValue] | None = None
  61. error_code: str | None = None
  62. error_message: str | None = None
  63. class AgentRunExecuteRequest(BaseModel):
  64. worker_key: str | None = None
  65. dry_run: bool = False
  66. class AgentWorkerExecuteNextRequest(BaseModel):
  67. worker_key: str
  68. lease_seconds: int | None = Field(default=None, gt=0)
  69. dry_run: bool | None = None
  70. class AgentRunResponse(AgentRunContract):
  71. @classmethod
  72. def from_entity(cls, entity: "AgentRun") -> "AgentRunResponse":
  73. return cls.model_validate(entity, from_attributes=True)
  74. class AgentToolInvocationResponse(AgentToolInvocationContract):
  75. @classmethod
  76. def from_entity(cls, entity: "AgentToolInvocation") -> "AgentToolInvocationResponse":
  77. return cls.model_validate(entity, from_attributes=True)
  78. class AgentRunExecuteResponse(BaseModel):
  79. run: AgentRunResponse
  80. model: str | None = None
  81. dry_run: bool = False
  82. class AgentWorkerExecuteNextResponse(BaseModel):
  83. run: AgentRunResponse
  84. model: str | None = None
  85. dry_run: bool = False
  86. released_lease_count: int = 0
  87. class AgentHealthResponse(BaseModel):
  88. service: str
  89. status: str
  90. database: str
  91. checked_time: datetime