feat: function toggle support

This commit is contained in:
Timothy J. Baek
2024-06-23 18:34:42 -07:00
parent 3034f3d310
commit d8c112d8b0
8 changed files with 338 additions and 209 deletions

View File

@@ -48,7 +48,7 @@ class FunctionModel(BaseModel):
type: str
content: str
meta: FunctionMeta
is_active: bool
is_active: bool = False
updated_at: int # timestamp in epoch
created_at: int # timestamp in epoch
@@ -115,16 +115,56 @@ class FunctionsTable:
except:
return None
def get_functions(self) -> List[FunctionModel]:
return [
FunctionModel(**model_to_dict(function)) for function in Function.select()
]
def get_functions(self, active_only=False) -> List[FunctionModel]:
if active_only:
return [
FunctionModel(**model_to_dict(function))
for function in Function.select().where(Function.is_active == True)
]
else:
return [
FunctionModel(**model_to_dict(function))
for function in Function.select()
]
def get_functions_by_type(self, type: str) -> List[FunctionModel]:
return [
FunctionModel(**model_to_dict(function))
for function in Function.select().where(Function.type == type)
]
def get_functions_by_type(
self, type: str, active_only=False
) -> List[FunctionModel]:
if active_only:
return [
FunctionModel(**model_to_dict(function))
for function in Function.select().where(
Function.type == type, Function.is_active == True
)
]
else:
return [
FunctionModel(**model_to_dict(function))
for function in Function.select().where(Function.type == type)
]
def get_function_valves_by_id(self, id: str) -> Optional[FunctionValves]:
try:
function = Function.get(Function.id == id)
return FunctionValves(**model_to_dict(function))
except Exception as e:
print(f"An error occurred: {e}")
return None
def update_function_valves_by_id(
self, id: str, valves: dict
) -> Optional[FunctionValves]:
try:
query = Function.update(
**{"valves": valves},
updated_at=int(time.time()),
).where(Function.id == id)
query.execute()
function = Function.get(Function.id == id)
return FunctionValves(**model_to_dict(function))
except:
return None
def get_user_valves_by_id_and_user_id(
self, id: str, user_id: str