repositories.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 get_by_id(self, *, tool_version_id: str) -> ToolVersion | None:
  70. stmt = (
  71. select(ToolVersion)
  72. .where(ToolVersion.id == tool_version_id)
  73. )
  74. return self.db.scalar(stmt)
  75. def _next_version_no(self, tool_id: str) -> int:
  76. stmt = select(func.max(ToolVersion.version_no)).where(ToolVersion.tool_id == tool_id)
  77. current_max = self.db.scalar(stmt)
  78. return (current_max or 0) + 1
  79. class ToolBindingRepository:
  80. def __init__(self, db: Session) -> None:
  81. self.db = db
  82. def create(
  83. self,
  84. *,
  85. app_id: str,
  86. tool_version_id: str,
  87. credential_id: str | None,
  88. binding_scope: str,
  89. enabled: bool,
  90. config_json: dict[str, JSONValue] | None) -> ToolBinding:
  91. entity = ToolBinding(
  92. app_id=app_id,
  93. tool_version_id=tool_version_id,
  94. credential_id=credential_id,
  95. binding_scope=binding_scope,
  96. enabled=enabled,
  97. config_json=config_json)
  98. self.db.add(entity)
  99. self.db.commit()
  100. self.db.refresh(entity)
  101. return entity
  102. def list_by_scope(self, *, app_id: str | None = None) -> list[ToolBinding]:
  103. stmt = select(ToolBinding)
  104. if app_id is not None:
  105. stmt = stmt.where(ToolBinding.app_id == app_id)
  106. stmt = stmt.order_by(ToolBinding.created_time.desc())
  107. return list(self.db.scalars(stmt))
  108. def get_by_id(self, *, binding_id: str) -> ToolBinding | None:
  109. stmt = (
  110. select(ToolBinding)
  111. .where(ToolBinding.id == binding_id)
  112. )
  113. return self.db.scalar(stmt)
  114. class ToolCredentialRepository:
  115. def __init__(self, db: Session) -> None:
  116. self.db = db
  117. def create(
  118. self,
  119. *,
  120. name: str,
  121. credential_type: str,
  122. encrypted_payload_text: str,
  123. secret_fingerprint: str,
  124. encryption_algorithm: str,
  125. metadata_json: dict[str, JSONValue]) -> ToolCredential:
  126. entity = ToolCredential(
  127. name=name,
  128. credential_type=credential_type,
  129. encrypted_payload_text=encrypted_payload_text,
  130. secret_fingerprint=secret_fingerprint,
  131. encryption_algorithm=encryption_algorithm,
  132. metadata_json=metadata_json)
  133. self.db.add(entity)
  134. self.db.commit()
  135. self.db.refresh(entity)
  136. return entity
  137. def list_all(self) -> list[ToolCredential]:
  138. stmt = (
  139. select(ToolCredential)
  140. .order_by(ToolCredential.created_time.desc())
  141. )
  142. return list(self.db.scalars(stmt))
  143. def get_by_id(self, *, credential_id: str) -> ToolCredential | None:
  144. stmt = (
  145. select(ToolCredential)
  146. .where(ToolCredential.id == credential_id)
  147. )
  148. return self.db.scalar(stmt)