repositories.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. plugin_id: str | None,
  12. code: str,
  13. name: str,
  14. tool_type: str,
  15. description: str | None) -> ToolDefinition:
  16. entity = ToolDefinition(
  17. plugin_id=plugin_id,
  18. code=code,
  19. name=name,
  20. tool_type=tool_type,
  21. description=description)
  22. self.db.add(entity)
  23. self.db.commit()
  24. self.db.refresh(entity)
  25. return entity
  26. def list_all(self) -> list[ToolDefinition]:
  27. stmt = (
  28. select(ToolDefinition)
  29. .order_by(ToolDefinition.created_time.desc())
  30. )
  31. return list(self.db.scalars(stmt))
  32. def get_by_id(self, *, tool_id: str) -> ToolDefinition | None:
  33. stmt = (
  34. select(ToolDefinition)
  35. .where(ToolDefinition.id == tool_id)
  36. )
  37. return self.db.scalar(stmt)
  38. class ToolVersionRepository:
  39. def __init__(self, db: Session) -> None:
  40. self.db = db
  41. def create(
  42. self,
  43. *,
  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) -> ToolVersion:
  50. entity = ToolVersion(
  51. tool_id=tool_id,
  52. version_no=self._next_version_no(tool_id),
  53. input_schema_json=input_schema_json,
  54. output_schema_json=output_schema_json,
  55. invoke_config_json=invoke_config_json,
  56. timeout_ms=timeout_ms,
  57. retry_policy_json=retry_policy_json)
  58. self.db.add(entity)
  59. self.db.commit()
  60. self.db.refresh(entity)
  61. return entity
  62. def list_by_tool(self, *, tool_id: str) -> list[ToolVersion]:
  63. stmt = (
  64. select(ToolVersion)
  65. .where(ToolVersion.tool_id == tool_id)
  66. .order_by(ToolVersion.version_no.desc())
  67. )
  68. return list(self.db.scalars(stmt))
  69. def list_all(self) -> list[ToolVersion]:
  70. stmt = (
  71. select(ToolVersion)
  72. .order_by(ToolVersion.created_time.desc())
  73. )
  74. return list(self.db.scalars(stmt))
  75. def get_by_id(self, *, tool_version_id: str) -> ToolVersion | None:
  76. stmt = (
  77. select(ToolVersion)
  78. .where(ToolVersion.id == tool_version_id)
  79. )
  80. return self.db.scalar(stmt)
  81. def _next_version_no(self, tool_id: str) -> int:
  82. stmt = select(func.max(ToolVersion.version_no)).where(ToolVersion.tool_id == tool_id)
  83. current_max = self.db.scalar(stmt)
  84. return (current_max or 0) + 1
  85. class ToolBindingRepository:
  86. def __init__(self, db: Session) -> None:
  87. self.db = db
  88. def create(
  89. self,
  90. *,
  91. app_id: str,
  92. tool_version_id: str,
  93. credential_id: str | None,
  94. binding_scope: str,
  95. enabled: bool,
  96. config_json: dict[str, JSONValue] | None) -> ToolBinding:
  97. entity = ToolBinding(
  98. app_id=app_id,
  99. tool_version_id=tool_version_id,
  100. credential_id=credential_id,
  101. binding_scope=binding_scope,
  102. enabled=enabled,
  103. config_json=config_json)
  104. self.db.add(entity)
  105. self.db.commit()
  106. self.db.refresh(entity)
  107. return entity
  108. def list_by_scope(self, *, app_id: str | None = None) -> list[ToolBinding]:
  109. stmt = select(ToolBinding)
  110. if app_id is not None:
  111. stmt = stmt.where(ToolBinding.app_id == app_id)
  112. stmt = stmt.order_by(ToolBinding.created_time.desc())
  113. return list(self.db.scalars(stmt))
  114. def get_by_id(self, *, binding_id: str) -> ToolBinding | None:
  115. stmt = (
  116. select(ToolBinding)
  117. .where(ToolBinding.id == binding_id)
  118. )
  119. return self.db.scalar(stmt)
  120. class ToolCredentialRepository:
  121. def __init__(self, db: Session) -> None:
  122. self.db = db
  123. def create(
  124. self,
  125. *,
  126. name: str,
  127. credential_type: str,
  128. encrypted_payload_text: str,
  129. secret_fingerprint: str,
  130. encryption_algorithm: str,
  131. metadata_json: dict[str, JSONValue]) -> ToolCredential:
  132. entity = ToolCredential(
  133. name=name,
  134. credential_type=credential_type,
  135. encrypted_payload_text=encrypted_payload_text,
  136. secret_fingerprint=secret_fingerprint,
  137. encryption_algorithm=encryption_algorithm,
  138. metadata_json=metadata_json)
  139. self.db.add(entity)
  140. self.db.commit()
  141. self.db.refresh(entity)
  142. return entity
  143. def list_all(self) -> list[ToolCredential]:
  144. stmt = (
  145. select(ToolCredential)
  146. .order_by(ToolCredential.created_time.desc())
  147. )
  148. return list(self.db.scalars(stmt))
  149. def get_by_id(self, *, credential_id: str) -> ToolCredential | None:
  150. stmt = (
  151. select(ToolCredential)
  152. .where(ToolCredential.id == credential_id)
  153. )
  154. return self.db.scalar(stmt)