2024-06-18 13:03:31 +00:00
|
|
|
from pydantic import BaseModel, ConfigDict
|
|
|
|
from typing import List, Optional
|
2024-01-03 04:41:37 +00:00
|
|
|
import time
|
|
|
|
|
2024-06-24 11:21:51 +00:00
|
|
|
from sqlalchemy import String, Column, BigInteger, Text
|
2024-01-03 04:41:37 +00:00
|
|
|
|
2024-07-04 06:32:39 +00:00
|
|
|
from apps.webui.internal.db import Base, get_db
|
2024-01-03 04:41:37 +00:00
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
####################
|
|
|
|
# Prompts DB Schema
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
class Prompt(Base):
|
|
|
|
__tablename__ = "prompt"
|
2024-01-03 04:41:37 +00:00
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
command = Column(String, primary_key=True)
|
|
|
|
user_id = Column(String)
|
2024-06-24 11:21:51 +00:00
|
|
|
title = Column(Text)
|
|
|
|
content = Column(Text)
|
2024-06-18 13:03:31 +00:00
|
|
|
timestamp = Column(BigInteger)
|
2024-01-03 04:41:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PromptModel(BaseModel):
|
|
|
|
command: str
|
|
|
|
user_id: str
|
|
|
|
title: str
|
|
|
|
content: str
|
|
|
|
timestamp: int # timestamp in epoch
|
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
|
2024-01-03 04:41:37 +00:00
|
|
|
|
|
|
|
####################
|
|
|
|
# Forms
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
|
|
|
class PromptForm(BaseModel):
|
|
|
|
command: str
|
|
|
|
title: str
|
|
|
|
content: str
|
|
|
|
|
|
|
|
|
|
|
|
class PromptsTable:
|
2024-01-03 22:33:57 +00:00
|
|
|
|
2024-03-31 08:13:39 +00:00
|
|
|
def insert_new_prompt(
|
2024-06-21 12:58:57 +00:00
|
|
|
self, user_id: str, form_data: PromptForm
|
2024-03-31 08:13:39 +00:00
|
|
|
) -> Optional[PromptModel]:
|
2024-06-24 11:06:15 +00:00
|
|
|
prompt = PromptModel(
|
|
|
|
**{
|
|
|
|
"user_id": user_id,
|
|
|
|
"command": form_data.command,
|
|
|
|
"title": form_data.title,
|
|
|
|
"content": form_data.content,
|
|
|
|
"timestamp": int(time.time()),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
|
|
|
|
result = Prompt(**prompt.dict())
|
|
|
|
db.add(result)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(result)
|
|
|
|
if result:
|
|
|
|
return PromptModel.model_validate(result)
|
|
|
|
else:
|
|
|
|
return None
|
2024-06-24 11:06:15 +00:00
|
|
|
except Exception as e:
|
|
|
|
return None
|
2024-01-03 04:41:37 +00:00
|
|
|
|
2024-06-21 12:58:57 +00:00
|
|
|
def get_prompt_by_command(self, command: str) -> Optional[PromptModel]:
|
2024-06-24 11:06:15 +00:00
|
|
|
try:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
|
|
|
|
prompt = db.query(Prompt).filter_by(command=command).first()
|
|
|
|
return PromptModel.model_validate(prompt)
|
2024-06-24 11:06:15 +00:00
|
|
|
except:
|
|
|
|
return None
|
2024-01-03 04:41:37 +00:00
|
|
|
|
2024-06-21 12:58:57 +00:00
|
|
|
def get_prompts(self) -> List[PromptModel]:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
|
|
|
|
return [
|
|
|
|
PromptModel.model_validate(prompt) for prompt in db.query(Prompt).all()
|
|
|
|
]
|
2024-01-03 04:41:37 +00:00
|
|
|
|
|
|
|
def update_prompt_by_command(
|
2024-06-21 12:58:57 +00:00
|
|
|
self, command: str, form_data: PromptForm
|
2024-03-31 08:13:39 +00:00
|
|
|
) -> Optional[PromptModel]:
|
2024-06-24 11:06:15 +00:00
|
|
|
try:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
|
|
|
|
prompt = db.query(Prompt).filter_by(command=command).first()
|
|
|
|
prompt.title = form_data.title
|
|
|
|
prompt.content = form_data.content
|
|
|
|
prompt.timestamp = int(time.time())
|
|
|
|
db.commit()
|
|
|
|
return PromptModel.model_validate(prompt)
|
2024-06-24 11:06:15 +00:00
|
|
|
except:
|
|
|
|
return None
|
2024-06-21 12:58:57 +00:00
|
|
|
|
|
|
|
def delete_prompt_by_command(self, command: str) -> bool:
|
2024-06-24 11:06:15 +00:00
|
|
|
try:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
|
|
|
|
db.query(Prompt).filter_by(command=command).delete()
|
2024-07-06 15:10:58 +00:00
|
|
|
db.commit()
|
|
|
|
|
2024-07-04 06:32:39 +00:00
|
|
|
return True
|
2024-06-24 11:06:15 +00:00
|
|
|
except:
|
|
|
|
return False
|
2024-01-03 04:41:37 +00:00
|
|
|
|
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
Prompts = PromptsTable()
|