repositories.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. from datetime import datetime
  2. from core_shared import JSONValue
  3. from sqlalchemy import select
  4. from sqlalchemy.orm import Session
  5. from app.db.models import ToolBinding, ToolCredential, ToolDefinition, ToolConnection
  6. class ToolDefinitionRepository:
  7. def __init__(self, db: Session) -> None:
  8. self.db = db
  9. def create(
  10. self,
  11. *,
  12. plugin_id: str | None,
  13. code: str,
  14. name: str,
  15. tool_type: str,
  16. description: str | None) -> ToolDefinition:
  17. entity = ToolDefinition(
  18. plugin_id=plugin_id,
  19. code=code,
  20. name=name,
  21. tool_type=tool_type,
  22. description=description)
  23. self.db.add(entity)
  24. self.db.commit()
  25. self.db.refresh(entity)
  26. return entity
  27. def list_all(self) -> list[ToolDefinition]:
  28. stmt = (
  29. select(ToolDefinition)
  30. .order_by(ToolDefinition.created_time.desc())
  31. )
  32. return list(self.db.scalars(stmt))
  33. def get_by_id(self, *, tool_id: str) -> ToolDefinition | None:
  34. stmt = (
  35. select(ToolDefinition)
  36. .where(ToolDefinition.id == tool_id)
  37. )
  38. return self.db.scalar(stmt)
  39. def save(self, entity: ToolDefinition) -> ToolDefinition:
  40. self.db.add(entity)
  41. self.db.commit()
  42. self.db.refresh(entity)
  43. return entity
  44. def delete(self, entity: ToolDefinition) -> None:
  45. self.db.delete(entity)
  46. self.db.commit()
  47. class ToolConnectionRepository:
  48. def __init__(self, db: Session) -> None:
  49. self.db = db
  50. def create(
  51. self,
  52. *,
  53. tool_id: str,
  54. input_schema_json: dict[str, JSONValue] | None,
  55. output_schema_json: dict[str, JSONValue] | None,
  56. invoke_config_json: dict[str, JSONValue] | None,
  57. timeout_ms: int | None,
  58. retry_policy_json: dict[str, JSONValue] | None) -> ToolConnection:
  59. entity = ToolConnection(
  60. tool_id=tool_id,
  61. input_schema_json=input_schema_json,
  62. output_schema_json=output_schema_json,
  63. invoke_config_json=invoke_config_json,
  64. timeout_ms=timeout_ms,
  65. retry_policy_json=retry_policy_json)
  66. self.db.add(entity)
  67. self.db.commit()
  68. self.db.refresh(entity)
  69. return entity
  70. def list_by_tool(self, *, tool_id: str) -> list[ToolConnection]:
  71. stmt = (
  72. select(ToolConnection)
  73. .where(ToolConnection.tool_id == tool_id)
  74. .order_by(ToolConnection.created_time.desc())
  75. )
  76. return list(self.db.scalars(stmt))
  77. def list_all(self) -> list[ToolConnection]:
  78. stmt = (
  79. select(ToolConnection)
  80. .order_by(ToolConnection.created_time.desc())
  81. )
  82. return list(self.db.scalars(stmt))
  83. def list_pending_mcp_discovery(
  84. self,
  85. *,
  86. stale_before: datetime,
  87. limit: int = 20) -> list[ToolConnection]:
  88. pending: list[ToolConnection] = []
  89. for connection in self.list_all():
  90. config = connection.invoke_config_json or {}
  91. status_payload = config.get("mcp_status")
  92. if not isinstance(status_payload, dict):
  93. continue
  94. status = status_payload.get("status")
  95. if status == "queued" or (status == "running" and connection.updated_time < stale_before):
  96. pending.append(connection)
  97. pending.sort(key=lambda item: item.updated_time)
  98. return pending[:limit]
  99. def get_by_id(self, *, tool_connection_id: str) -> ToolConnection | None:
  100. stmt = (
  101. select(ToolConnection)
  102. .where(ToolConnection.id == tool_connection_id)
  103. )
  104. return self.db.scalar(stmt)
  105. def save(self, entity: ToolConnection) -> ToolConnection:
  106. self.db.add(entity)
  107. self.db.commit()
  108. self.db.refresh(entity)
  109. return entity
  110. def delete(self, entity: ToolConnection) -> None:
  111. self.db.delete(entity)
  112. self.db.commit()
  113. class ToolBindingRepository:
  114. def __init__(self, db: Session) -> None:
  115. self.db = db
  116. def create(
  117. self,
  118. *,
  119. app_id: str,
  120. tool_connection_id: str,
  121. credential_id: str | None,
  122. binding_scope: str,
  123. enabled: bool,
  124. config_json: dict[str, JSONValue] | None) -> ToolBinding:
  125. entity = ToolBinding(
  126. app_id=app_id,
  127. tool_connection_id=tool_connection_id,
  128. credential_id=credential_id,
  129. binding_scope=binding_scope,
  130. enabled=enabled,
  131. config_json=config_json)
  132. self.db.add(entity)
  133. self.db.commit()
  134. self.db.refresh(entity)
  135. return entity
  136. def list_by_scope(self, *, app_id: str | None = None) -> list[ToolBinding]:
  137. stmt = select(ToolBinding)
  138. if app_id is not None:
  139. stmt = stmt.where(ToolBinding.app_id == app_id)
  140. stmt = stmt.order_by(ToolBinding.created_time.desc())
  141. return list(self.db.scalars(stmt))
  142. def get_by_id(self, *, binding_id: str) -> ToolBinding | None:
  143. stmt = (
  144. select(ToolBinding)
  145. .where(ToolBinding.id == binding_id)
  146. )
  147. return self.db.scalar(stmt)
  148. def save(self, entity: ToolBinding) -> ToolBinding:
  149. self.db.add(entity)
  150. self.db.commit()
  151. self.db.refresh(entity)
  152. return entity
  153. def delete(self, entity: ToolBinding) -> None:
  154. self.db.delete(entity)
  155. self.db.commit()
  156. class ToolCredentialRepository:
  157. def __init__(self, db: Session) -> None:
  158. self.db = db
  159. def create(
  160. self,
  161. *,
  162. name: str,
  163. credential_type: str,
  164. encrypted_payload_text: str,
  165. secret_fingerprint: str,
  166. encryption_algorithm: str,
  167. metadata_json: dict[str, JSONValue]) -> ToolCredential:
  168. entity = ToolCredential(
  169. name=name,
  170. credential_type=credential_type,
  171. encrypted_payload_text=encrypted_payload_text,
  172. secret_fingerprint=secret_fingerprint,
  173. encryption_algorithm=encryption_algorithm,
  174. metadata_json=metadata_json)
  175. self.db.add(entity)
  176. self.db.commit()
  177. self.db.refresh(entity)
  178. return entity
  179. def list_all(self) -> list[ToolCredential]:
  180. stmt = (
  181. select(ToolCredential)
  182. .order_by(ToolCredential.created_time.desc())
  183. )
  184. return list(self.db.scalars(stmt))
  185. def get_by_id(self, *, credential_id: str) -> ToolCredential | None:
  186. stmt = (
  187. select(ToolCredential)
  188. .where(ToolCredential.id == credential_id)
  189. )
  190. return self.db.scalar(stmt)
  191. def save(self, entity: ToolCredential) -> ToolCredential:
  192. self.db.add(entity)
  193. self.db.commit()
  194. self.db.refresh(entity)
  195. return entity
  196. def delete(self, entity: ToolCredential) -> None:
  197. self.db.delete(entity)
  198. self.db.commit()