repositories.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from sqlalchemy import func, select
  2. from sqlalchemy.orm import Session
  3. from app.db.models import ToolBinding, ToolDefinition, ToolVersion
  4. from core_shared import JSONValue
  5. class ToolDefinitionRepository:
  6. def __init__(self, db: Session) -> None:
  7. self.db = db
  8. def create(
  9. self,
  10. *,
  11. tenant_id: str,
  12. plugin_id: str | None,
  13. code: str,
  14. name: str,
  15. tool_type: str,
  16. description: str | None,
  17. ) -> ToolDefinition:
  18. entity = ToolDefinition(
  19. tenant_id=tenant_id,
  20. plugin_id=plugin_id,
  21. code=code,
  22. name=name,
  23. tool_type=tool_type,
  24. description=description,
  25. )
  26. self.db.add(entity)
  27. self.db.commit()
  28. self.db.refresh(entity)
  29. return entity
  30. def list_by_tenant(self, tenant_id: str) -> list[ToolDefinition]:
  31. stmt = (
  32. select(ToolDefinition)
  33. .where(ToolDefinition.tenant_id == tenant_id)
  34. .order_by(ToolDefinition.created_time.desc())
  35. )
  36. return list(self.db.scalars(stmt))
  37. class ToolVersionRepository:
  38. def __init__(self, db: Session) -> None:
  39. self.db = db
  40. def create(
  41. self,
  42. *,
  43. tenant_id: str,
  44. tool_id: str,
  45. input_schema_json: dict[str, JSONValue] | None,
  46. output_schema_json: dict[str, JSONValue] | None,
  47. invoke_config_json: dict[str, JSONValue] | None,
  48. timeout_ms: int | None,
  49. retry_policy_json: dict[str, JSONValue] | None,
  50. ) -> ToolVersion:
  51. entity = ToolVersion(
  52. tenant_id=tenant_id,
  53. tool_id=tool_id,
  54. version_no=self._next_version_no(tool_id),
  55. input_schema_json=input_schema_json,
  56. output_schema_json=output_schema_json,
  57. invoke_config_json=invoke_config_json,
  58. timeout_ms=timeout_ms,
  59. retry_policy_json=retry_policy_json,
  60. )
  61. self.db.add(entity)
  62. self.db.commit()
  63. self.db.refresh(entity)
  64. return entity
  65. def list_by_tool(self, *, tenant_id: str, tool_id: str) -> list[ToolVersion]:
  66. stmt = (
  67. select(ToolVersion)
  68. .where(ToolVersion.tenant_id == tenant_id)
  69. .where(ToolVersion.tool_id == tool_id)
  70. .order_by(ToolVersion.version_no.desc())
  71. )
  72. return list(self.db.scalars(stmt))
  73. def _next_version_no(self, tool_id: str) -> int:
  74. stmt = select(func.max(ToolVersion.version_no)).where(ToolVersion.tool_id == tool_id)
  75. current_max = self.db.scalar(stmt)
  76. return (current_max or 0) + 1
  77. class ToolBindingRepository:
  78. def __init__(self, db: Session) -> None:
  79. self.db = db
  80. def create(
  81. self,
  82. *,
  83. tenant_id: str,
  84. app_id: str,
  85. tool_version_id: str,
  86. credential_id: str | None,
  87. binding_scope: str,
  88. enabled: bool,
  89. config_json: dict[str, JSONValue] | None,
  90. ) -> ToolBinding:
  91. entity = ToolBinding(
  92. tenant_id=tenant_id,
  93. app_id=app_id,
  94. tool_version_id=tool_version_id,
  95. credential_id=credential_id,
  96. binding_scope=binding_scope,
  97. enabled=enabled,
  98. config_json=config_json,
  99. )
  100. self.db.add(entity)
  101. self.db.commit()
  102. self.db.refresh(entity)
  103. return entity
  104. def list_by_scope(self, *, tenant_id: str, app_id: str | None = None) -> list[ToolBinding]:
  105. stmt = select(ToolBinding).where(ToolBinding.tenant_id == tenant_id)
  106. if app_id is not None:
  107. stmt = stmt.where(ToolBinding.app_id == app_id)
  108. stmt = stmt.order_by(ToolBinding.created_time.desc())
  109. return list(self.db.scalars(stmt))