2024-06-18 13:03:31 +00:00
|
|
|
from pydantic import BaseModel, ConfigDict
|
2024-06-18 17:36:06 +00:00
|
|
|
from typing import List, Union, Optional
|
|
|
|
import time
|
|
|
|
import logging
|
2024-06-18 13:03:31 +00:00
|
|
|
|
|
|
|
from sqlalchemy import Column, String, Text, BigInteger, Boolean
|
|
|
|
|
2024-06-24 11:06:15 +00:00
|
|
|
from apps.webui.internal.db import JSONField, Base, Session
|
2024-06-22 18:26:33 +00:00
|
|
|
from apps.webui.models.users import Users
|
2024-06-18 17:36:06 +00:00
|
|
|
|
|
|
|
import json
|
2024-06-22 19:08:32 +00:00
|
|
|
import copy
|
|
|
|
|
2024-06-18 17:36:06 +00:00
|
|
|
|
|
|
|
from config import SRC_LOG_LEVELS
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
log.setLevel(SRC_LOG_LEVELS["MODELS"])
|
|
|
|
|
|
|
|
####################
|
|
|
|
# Functions DB Schema
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
class Function(Base):
|
|
|
|
__tablename__ = "function"
|
2024-06-18 17:36:06 +00:00
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
id = Column(String, primary_key=True)
|
|
|
|
user_id = Column(String)
|
|
|
|
name = Column(Text)
|
|
|
|
type = Column(Text)
|
|
|
|
content = Column(Text)
|
|
|
|
meta = Column(JSONField)
|
|
|
|
valves = Column(JSONField)
|
|
|
|
is_active = Column(Boolean)
|
|
|
|
updated_at = Column(BigInteger)
|
|
|
|
created_at = Column(BigInteger)
|
2024-06-18 17:36:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FunctionMeta(BaseModel):
|
|
|
|
description: Optional[str] = None
|
2024-06-24 03:31:40 +00:00
|
|
|
manifest: Optional[dict] = {}
|
2024-06-18 17:36:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FunctionModel(BaseModel):
|
|
|
|
id: str
|
|
|
|
user_id: str
|
|
|
|
name: str
|
|
|
|
type: str
|
|
|
|
content: str
|
|
|
|
meta: FunctionMeta
|
2024-06-24 01:34:42 +00:00
|
|
|
is_active: bool = False
|
2024-06-18 17:36:06 +00:00
|
|
|
updated_at: int # timestamp in epoch
|
|
|
|
created_at: int # timestamp in epoch
|
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
|
2024-06-18 17:36:06 +00:00
|
|
|
|
|
|
|
####################
|
|
|
|
# Forms
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
|
|
|
class FunctionResponse(BaseModel):
|
|
|
|
id: str
|
|
|
|
user_id: str
|
2024-06-20 08:16:31 +00:00
|
|
|
type: str
|
2024-06-18 17:36:06 +00:00
|
|
|
name: str
|
|
|
|
meta: FunctionMeta
|
2024-06-24 01:05:33 +00:00
|
|
|
is_active: bool
|
2024-06-18 17:36:06 +00:00
|
|
|
updated_at: int # timestamp in epoch
|
|
|
|
created_at: int # timestamp in epoch
|
|
|
|
|
|
|
|
|
|
|
|
class FunctionForm(BaseModel):
|
|
|
|
id: str
|
|
|
|
name: str
|
|
|
|
content: str
|
|
|
|
meta: FunctionMeta
|
|
|
|
|
|
|
|
|
2024-06-24 01:05:33 +00:00
|
|
|
class FunctionValves(BaseModel):
|
|
|
|
valves: Optional[dict] = None
|
|
|
|
|
|
|
|
|
2024-06-20 07:37:02 +00:00
|
|
|
class FunctionsTable:
|
2024-06-18 17:36:06 +00:00
|
|
|
|
|
|
|
def insert_new_function(
|
2024-06-21 12:58:57 +00:00
|
|
|
self, user_id: str, type: str, form_data: FunctionForm
|
2024-06-18 17:36:06 +00:00
|
|
|
) -> Optional[FunctionModel]:
|
|
|
|
function = FunctionModel(
|
|
|
|
**{
|
|
|
|
**form_data.model_dump(),
|
|
|
|
"user_id": user_id,
|
2024-06-20 07:54:58 +00:00
|
|
|
"type": type,
|
2024-06-18 17:36:06 +00:00
|
|
|
"updated_at": int(time.time()),
|
|
|
|
"created_at": int(time.time()),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
2024-06-24 11:06:15 +00:00
|
|
|
result = Function(**function.model_dump())
|
|
|
|
Session.add(result)
|
|
|
|
Session.commit()
|
|
|
|
Session.refresh(result)
|
|
|
|
if result:
|
|
|
|
return FunctionModel.model_validate(result)
|
|
|
|
else:
|
|
|
|
return None
|
2024-06-18 17:36:06 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(f"Error creating tool: {e}")
|
|
|
|
return None
|
|
|
|
|
2024-06-21 12:58:57 +00:00
|
|
|
def get_function_by_id(self, id: str) -> Optional[FunctionModel]:
|
2024-06-18 17:36:06 +00:00
|
|
|
try:
|
2024-06-24 11:06:15 +00:00
|
|
|
function = Session.get(Function, id)
|
|
|
|
return FunctionModel.model_validate(function)
|
2024-06-18 17:36:06 +00:00
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
2024-06-24 01:34:42 +00:00
|
|
|
def get_functions(self, active_only=False) -> List[FunctionModel]:
|
|
|
|
if active_only:
|
2024-06-24 11:06:15 +00:00
|
|
|
return [
|
|
|
|
FunctionModel.model_validate(function)
|
|
|
|
for function in Session.query(Function).filter_by(is_active=True).all()
|
|
|
|
]
|
2024-06-24 01:34:42 +00:00
|
|
|
else:
|
2024-06-24 11:06:15 +00:00
|
|
|
return [
|
|
|
|
FunctionModel.model_validate(function)
|
|
|
|
for function in Session.query(Function).all()
|
|
|
|
]
|
2024-06-24 01:34:42 +00:00
|
|
|
|
|
|
|
def get_functions_by_type(
|
|
|
|
self, type: str, active_only=False
|
|
|
|
) -> List[FunctionModel]:
|
|
|
|
if active_only:
|
2024-06-24 11:06:15 +00:00
|
|
|
return [
|
|
|
|
FunctionModel.model_validate(function)
|
|
|
|
for function in Session.query(Function)
|
|
|
|
.filter_by(type=type, is_active=True)
|
|
|
|
.all()
|
|
|
|
]
|
2024-06-24 01:34:42 +00:00
|
|
|
else:
|
2024-06-24 11:06:15 +00:00
|
|
|
return [
|
|
|
|
FunctionModel.model_validate(function)
|
|
|
|
for function in Session.query(Function).filter_by(type=type).all()
|
|
|
|
]
|
2024-06-24 01:34:42 +00:00
|
|
|
|
2024-06-24 02:18:13 +00:00
|
|
|
def get_function_valves_by_id(self, id: str) -> Optional[dict]:
|
2024-06-24 01:34:42 +00:00
|
|
|
try:
|
2024-06-24 11:06:15 +00:00
|
|
|
function = Session.get(Function, id)
|
|
|
|
return function.valves if function.valves else {}
|
2024-06-24 01:34:42 +00:00
|
|
|
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:
|
2024-06-24 11:06:15 +00:00
|
|
|
function = Session.get(Function, id)
|
|
|
|
function.valves = valves
|
|
|
|
function.updated_at = int(time.time())
|
|
|
|
Session.commit()
|
|
|
|
Session.refresh(function)
|
|
|
|
return self.get_function_by_id(id)
|
2024-06-24 01:34:42 +00:00
|
|
|
except:
|
|
|
|
return None
|
2024-06-18 17:36:06 +00:00
|
|
|
|
2024-06-22 18:26:33 +00:00
|
|
|
def get_user_valves_by_id_and_user_id(
|
|
|
|
self, id: str, user_id: str
|
|
|
|
) -> Optional[dict]:
|
|
|
|
try:
|
|
|
|
user = Users.get_user_by_id(user_id)
|
2024-06-22 19:08:32 +00:00
|
|
|
user_settings = user.settings.model_dump()
|
2024-06-22 18:26:33 +00:00
|
|
|
|
|
|
|
# Check if user has "functions" and "valves" settings
|
2024-06-22 19:08:32 +00:00
|
|
|
if "functions" not in user_settings:
|
|
|
|
user_settings["functions"] = {}
|
|
|
|
if "valves" not in user_settings["functions"]:
|
|
|
|
user_settings["functions"]["valves"] = {}
|
2024-06-22 18:26:33 +00:00
|
|
|
|
2024-06-22 19:08:32 +00:00
|
|
|
return user_settings["functions"]["valves"].get(id, {})
|
2024-06-22 18:26:33 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(f"An error occurred: {e}")
|
|
|
|
return None
|
|
|
|
|
|
|
|
def update_user_valves_by_id_and_user_id(
|
|
|
|
self, id: str, user_id: str, valves: dict
|
|
|
|
) -> Optional[dict]:
|
|
|
|
try:
|
|
|
|
user = Users.get_user_by_id(user_id)
|
2024-06-22 19:08:32 +00:00
|
|
|
user_settings = user.settings.model_dump()
|
2024-06-22 18:26:33 +00:00
|
|
|
|
|
|
|
# Check if user has "functions" and "valves" settings
|
2024-06-22 19:08:32 +00:00
|
|
|
if "functions" not in user_settings:
|
|
|
|
user_settings["functions"] = {}
|
|
|
|
if "valves" not in user_settings["functions"]:
|
|
|
|
user_settings["functions"]["valves"] = {}
|
2024-06-22 18:26:33 +00:00
|
|
|
|
2024-06-22 19:08:32 +00:00
|
|
|
user_settings["functions"]["valves"][id] = valves
|
2024-06-22 18:26:33 +00:00
|
|
|
|
|
|
|
# Update the user settings in the database
|
2024-06-22 19:08:32 +00:00
|
|
|
query = Users.update_user_by_id(user_id, {"settings": user_settings})
|
2024-06-22 18:26:33 +00:00
|
|
|
query.execute()
|
|
|
|
|
2024-06-22 19:08:32 +00:00
|
|
|
return user_settings["functions"]["valves"][id]
|
2024-06-22 18:26:33 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(f"An error occurred: {e}")
|
|
|
|
return None
|
|
|
|
|
2024-06-18 17:36:06 +00:00
|
|
|
def update_function_by_id(self, id: str, updated: dict) -> Optional[FunctionModel]:
|
|
|
|
try:
|
2024-06-24 11:06:15 +00:00
|
|
|
Session.query(Function).filter_by(id=id).update(
|
|
|
|
{
|
|
|
|
**updated,
|
|
|
|
"updated_at": int(time.time()),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
Session.commit()
|
|
|
|
return self.get_function_by_id(id)
|
2024-06-18 17:36:06 +00:00
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
2024-06-24 02:28:33 +00:00
|
|
|
def deactivate_all_functions(self) -> Optional[bool]:
|
|
|
|
try:
|
2024-06-24 11:06:15 +00:00
|
|
|
Session.query(Function).update(
|
|
|
|
{
|
|
|
|
"is_active": False,
|
|
|
|
"updated_at": int(time.time()),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
Session.commit()
|
2024-06-24 02:28:33 +00:00
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
2024-06-21 12:58:57 +00:00
|
|
|
def delete_function_by_id(self, id: str) -> bool:
|
2024-06-18 17:36:06 +00:00
|
|
|
try:
|
2024-06-24 11:06:15 +00:00
|
|
|
Session.query(Function).filter_by(id=id).delete()
|
2024-06-18 17:36:06 +00:00
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
Functions = FunctionsTable()
|