assistant.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from sqlmodel import select
  2. from sqlalchemy.ext.asyncio import AsyncSession
  3. from app.exceptions.exception import ResourceNotFoundError
  4. from app.models.assistant import Assistant, AssistantUpdate, AssistantCreate
  5. from app.models.token_relation import RelationType
  6. from app.providers.auth_provider import auth_policy
  7. from app.schemas.common import DeleteResponse
  8. from app.utils import revise_tool_names
  9. class AssistantService:
  10. @staticmethod
  11. async def create_assistant(
  12. *, session: AsyncSession, body: AssistantCreate, token_id: str = None
  13. ) -> Assistant:
  14. revise_tool_names(body.tools)
  15. db_assistant = Assistant.model_validate(body.model_dump(by_alias=True))
  16. session.add(db_assistant)
  17. auth_policy.insert_token_rel(
  18. session=session,
  19. token_id=token_id,
  20. relation_type=RelationType.Assistant,
  21. relation_id=db_assistant.id,
  22. )
  23. await session.commit()
  24. await session.refresh(db_assistant)
  25. return db_assistant
  26. @staticmethod
  27. async def modify_assistant(
  28. *, session: AsyncSession, assistant_id: str, body: AssistantUpdate
  29. ) -> Assistant:
  30. revise_tool_names(body.tools)
  31. db_assistant = await AssistantService.get_assistant(
  32. session=session, assistant_id=assistant_id
  33. )
  34. update_data = body.dict(exclude_unset=True)
  35. for key, value in update_data.items():
  36. setattr(db_assistant, key, value)
  37. session.add(db_assistant)
  38. await session.commit()
  39. await session.refresh(db_assistant)
  40. return db_assistant
  41. @staticmethod
  42. async def delete_assistant(
  43. *,
  44. session: AsyncSession,
  45. assistant_id: str,
  46. ) -> DeleteResponse:
  47. db_ass = await AssistantService.get_assistant(
  48. session=session, assistant_id=assistant_id
  49. )
  50. await session.delete(db_ass)
  51. await auth_policy.delete_token_rel(
  52. session=session,
  53. relation_type=RelationType.Assistant,
  54. relation_id=assistant_id,
  55. )
  56. await session.commit()
  57. return DeleteResponse(id=assistant_id, object="assistant.deleted", deleted=True)
  58. @staticmethod
  59. async def get_assistant(*, session: AsyncSession, assistant_id: str) -> Assistant:
  60. statement = select(Assistant).where(Assistant.id == assistant_id)
  61. result = await session.execute(statement)
  62. assistant = result.scalars().one_or_none()
  63. if assistant is None:
  64. raise ResourceNotFoundError(message="Assistant not found")
  65. return assistant
  66. @staticmethod
  67. def get_assistant_sync(*, session: AsyncSession, assistant_id: str) -> Assistant:
  68. statement = select(Assistant).where(Assistant.id == assistant_id)
  69. result = session.execute(statement)
  70. assistant = result.scalars().one_or_none()
  71. if assistant is None:
  72. raise ResourceNotFoundError(message="Assistant not found")
  73. return assistant
  74. @staticmethod
  75. def get_assistant_sync(*, session: AsyncSession, assistant_id: str) -> Assistant:
  76. statement = select(Assistant).where(Assistant.id == assistant_id)
  77. result = session.execute(statement)
  78. assistant = result.scalars().one_or_none()
  79. if assistant is None:
  80. raise ResourceNotFoundError(message="Assistant not found")
  81. return assistant