functions.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. type: str
  44. name: str
  45. meta: FunctionMeta
  46. updated_at: int # timestamp in epoch
  47. created_at: int # timestamp in epoch
  48. class FunctionForm(BaseModel):
  49. id: str
  50. name: str
  51. content: str
  52. meta: FunctionMeta
  53. class FunctionsTable:
  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, type: str, form_data: FunctionForm
  59. ) -> Optional[FunctionModel]:
  60. function = FunctionModel(
  61. **{
  62. **form_data.model_dump(),
  63. "user_id": user_id,
  64. "type": type,
  65. "updated_at": int(time.time()),
  66. "created_at": int(time.time()),
  67. }
  68. )
  69. try:
  70. result = Function.create(**function.model_dump())
  71. if result:
  72. return function
  73. else:
  74. return None
  75. except Exception as e:
  76. print(f"Error creating tool: {e}")
  77. return None
  78. def get_function_by_id(self, id: str) -> Optional[FunctionModel]:
  79. try:
  80. function = Function.get(Function.id == id)
  81. return FunctionModel(**model_to_dict(function))
  82. except:
  83. return None
  84. def get_functions(self) -> List[FunctionModel]:
  85. return [
  86. FunctionModel(**model_to_dict(function)) for function in Function.select()
  87. ]
  88. def get_functions_by_type(self, type: str) -> List[FunctionModel]:
  89. return [
  90. FunctionModel(**model_to_dict(function))
  91. for function in Function.select().where(Function.type == type)
  92. ]
  93. def update_function_by_id(self, id: str, updated: dict) -> Optional[FunctionModel]:
  94. try:
  95. query = Function.update(
  96. **updated,
  97. updated_at=int(time.time()),
  98. ).where(Function.id == id)
  99. query.execute()
  100. function = Function.get(Function.id == id)
  101. return FunctionModel(**model_to_dict(function))
  102. except:
  103. return None
  104. def delete_function_by_id(self, id: str) -> bool:
  105. try:
  106. query = Function.delete().where((Function.id == id))
  107. query.execute() # Remove the rows, return number of rows removed.
  108. return True
  109. except:
  110. return False
  111. Functions = FunctionsTable(DB)