functions.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. from pydantic import BaseModel
  2. from peewee import *
  3. from playhouse.shortcuts import model_to_dict
  4. from typing import List, Union, Optional
  5. import time
  6. import logging
  7. from apps.webui.internal.db import DB, JSONField
  8. import json
  9. from config import SRC_LOG_LEVELS
  10. log = logging.getLogger(__name__)
  11. log.setLevel(SRC_LOG_LEVELS["MODELS"])
  12. ####################
  13. # Functions DB Schema
  14. ####################
  15. class Function(Model):
  16. id = CharField(unique=True)
  17. user_id = CharField()
  18. name = TextField()
  19. type = TextField()
  20. content = TextField()
  21. meta = JSONField()
  22. updated_at = BigIntegerField()
  23. created_at = BigIntegerField()
  24. class Meta:
  25. database = DB
  26. class FunctionMeta(BaseModel):
  27. description: Optional[str] = None
  28. class FunctionModel(BaseModel):
  29. id: str
  30. user_id: str
  31. name: str
  32. type: str
  33. content: str
  34. meta: FunctionMeta
  35. updated_at: int # timestamp in epoch
  36. created_at: int # timestamp in epoch
  37. ####################
  38. # Forms
  39. ####################
  40. class FunctionResponse(BaseModel):
  41. id: str
  42. user_id: str
  43. name: str
  44. meta: FunctionMeta
  45. updated_at: int # timestamp in epoch
  46. created_at: int # timestamp in epoch
  47. class FunctionForm(BaseModel):
  48. id: str
  49. name: str
  50. type: str
  51. content: str
  52. meta: FunctionMeta
  53. class ToolsTable:
  54. def __init__(self, db):
  55. self.db = db
  56. self.db.create_tables([Function])
  57. def insert_new_function(
  58. self, user_id: str, form_data: FunctionForm
  59. ) -> Optional[FunctionModel]:
  60. function = FunctionModel(
  61. **{
  62. **form_data.model_dump(),
  63. "user_id": user_id,
  64. "updated_at": int(time.time()),
  65. "created_at": int(time.time()),
  66. }
  67. )
  68. try:
  69. result = Function.create(**function.model_dump())
  70. if result:
  71. return function
  72. else:
  73. return None
  74. except Exception as e:
  75. print(f"Error creating tool: {e}")
  76. return None
  77. def get_function_by_id(self, id: str) -> Optional[FunctionModel]:
  78. try:
  79. function = Function.get(Function.id == id)
  80. return FunctionModel(**model_to_dict(function))
  81. except:
  82. return None
  83. def get_functions(self) -> List[FunctionModel]:
  84. return [
  85. FunctionModel(**model_to_dict(function)) for function in Function.select()
  86. ]
  87. def get_functions_by_type(self, type: str) -> List[FunctionModel]:
  88. return [
  89. FunctionModel(**model_to_dict(function))
  90. for function in Function.select().where(Function.type == type)
  91. ]
  92. def update_function_by_id(self, id: str, updated: dict) -> Optional[FunctionModel]:
  93. try:
  94. query = Function.update(
  95. **updated,
  96. updated_at=int(time.time()),
  97. ).where(Function.id == id)
  98. query.execute()
  99. function = Function.get(Function.id == id)
  100. return FunctionModel(**model_to_dict(function))
  101. except:
  102. return None
  103. def delete_function_by_id(self, id: str) -> bool:
  104. try:
  105. query = Function.delete().where((Function.id == id))
  106. query.execute() # Remove the rows, return number of rows removed.
  107. return True
  108. except:
  109. return False
  110. Tools = ToolsTable(DB)