repositories.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. from sqlalchemy import func, select
  2. from sqlalchemy.orm import Session
  3. from app.db.models import ToolBinding, ToolCredential, 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. def get_by_id(self, *, tenant_id: str, tool_id: str) -> ToolDefinition | None:
  38. stmt = (
  39. select(ToolDefinition)
  40. .where(ToolDefinition.tenant_id == tenant_id)
  41. .where(ToolDefinition.id == tool_id)
  42. )
  43. return self.db.scalar(stmt)
  44. class ToolVersionRepository:
  45. def __init__(self, db: Session) -> None:
  46. self.db = db
  47. def create(
  48. self,
  49. *,
  50. tenant_id: str,
  51. tool_id: str,
  52. input_schema_json: dict[str, JSONValue] | None,
  53. output_schema_json: dict[str, JSONValue] | None,
  54. invoke_config_json: dict[str, JSONValue] | None,
  55. timeout_ms: int | None,
  56. retry_policy_json: dict[str, JSONValue] | None,
  57. ) -> ToolVersion:
  58. entity = ToolVersion(
  59. tenant_id=tenant_id,
  60. tool_id=tool_id,
  61. version_no=self._next_version_no(tool_id),
  62. input_schema_json=input_schema_json,
  63. output_schema_json=output_schema_json,
  64. invoke_config_json=invoke_config_json,
  65. timeout_ms=timeout_ms,
  66. retry_policy_json=retry_policy_json,
  67. )
  68. self.db.add(entity)
  69. self.db.commit()
  70. self.db.refresh(entity)
  71. return entity
  72. def list_by_tool(self, *, tenant_id: str, tool_id: str) -> list[ToolVersion]:
  73. stmt = (
  74. select(ToolVersion)
  75. .where(ToolVersion.tenant_id == tenant_id)
  76. .where(ToolVersion.tool_id == tool_id)
  77. .order_by(ToolVersion.version_no.desc())
  78. )
  79. return list(self.db.scalars(stmt))
  80. def get_by_id(self, *, tenant_id: str, tool_version_id: str) -> ToolVersion | None:
  81. stmt = (
  82. select(ToolVersion)
  83. .where(ToolVersion.tenant_id == tenant_id)
  84. .where(ToolVersion.id == tool_version_id)
  85. )
  86. return self.db.scalar(stmt)
  87. def _next_version_no(self, tool_id: str) -> int:
  88. stmt = select(func.max(ToolVersion.version_no)).where(ToolVersion.tool_id == tool_id)
  89. current_max = self.db.scalar(stmt)
  90. return (current_max or 0) + 1
  91. class ToolBindingRepository:
  92. def __init__(self, db: Session) -> None:
  93. self.db = db
  94. def create(
  95. self,
  96. *,
  97. tenant_id: str,
  98. app_id: str,
  99. tool_version_id: str,
  100. credential_id: str | None,
  101. binding_scope: str,
  102. enabled: bool,
  103. config_json: dict[str, JSONValue] | None,
  104. ) -> ToolBinding:
  105. entity = ToolBinding(
  106. tenant_id=tenant_id,
  107. app_id=app_id,
  108. tool_version_id=tool_version_id,
  109. credential_id=credential_id,
  110. binding_scope=binding_scope,
  111. enabled=enabled,
  112. config_json=config_json,
  113. )
  114. self.db.add(entity)
  115. self.db.commit()
  116. self.db.refresh(entity)
  117. return entity
  118. def list_by_scope(self, *, tenant_id: str, app_id: str | None = None) -> list[ToolBinding]:
  119. stmt = select(ToolBinding).where(ToolBinding.tenant_id == tenant_id)
  120. if app_id is not None:
  121. stmt = stmt.where(ToolBinding.app_id == app_id)
  122. stmt = stmt.order_by(ToolBinding.created_time.desc())
  123. return list(self.db.scalars(stmt))
  124. def get_by_id(self, *, tenant_id: str, binding_id: str) -> ToolBinding | None:
  125. stmt = (
  126. select(ToolBinding)
  127. .where(ToolBinding.tenant_id == tenant_id)
  128. .where(ToolBinding.id == binding_id)
  129. )
  130. return self.db.scalar(stmt)
  131. class ToolCredentialRepository:
  132. def __init__(self, db: Session) -> None:
  133. self.db = db
  134. def create(
  135. self,
  136. *,
  137. tenant_id: str,
  138. name: str,
  139. credential_type: str,
  140. encrypted_payload_text: str,
  141. secret_fingerprint: str,
  142. encryption_algorithm: str,
  143. metadata_json: dict[str, JSONValue],
  144. ) -> ToolCredential:
  145. entity = ToolCredential(
  146. tenant_id=tenant_id,
  147. name=name,
  148. credential_type=credential_type,
  149. encrypted_payload_text=encrypted_payload_text,
  150. secret_fingerprint=secret_fingerprint,
  151. encryption_algorithm=encryption_algorithm,
  152. metadata_json=metadata_json,
  153. )
  154. self.db.add(entity)
  155. self.db.commit()
  156. self.db.refresh(entity)
  157. return entity
  158. def list_by_tenant(self, *, tenant_id: str) -> list[ToolCredential]:
  159. stmt = (
  160. select(ToolCredential)
  161. .where(ToolCredential.tenant_id == tenant_id)
  162. .order_by(ToolCredential.created_time.desc())
  163. )
  164. return list(self.db.scalars(stmt))
  165. def get_by_id(self, *, tenant_id: str, credential_id: str) -> ToolCredential | None:
  166. stmt = (
  167. select(ToolCredential)
  168. .where(ToolCredential.tenant_id == tenant_id)
  169. .where(ToolCredential.id == credential_id)
  170. )
  171. return self.db.scalar(stmt)