first commit

This commit is contained in:
2026-04-07 16:05:05 +08:00
commit 9d9bdbb1ce
136 changed files with 5103 additions and 0 deletions
+12
View File
@@ -0,0 +1,12 @@
from .auth import AuthService
from .user import UserService
from .workspace import WorkspaceService
from .agent import AgentService
from .conversation import ConversationService
from .message import MessageService
from .tool import ToolService
from .skill import SkillService
from .memory import MemoryService
from .model import ModelService
from .cron_job import CronJobService
from .channel import ChannelService
+47
View File
@@ -0,0 +1,47 @@
from ..models import Agent
class AgentService:
@staticmethod
def create_agent(workspace_id, data):
agent = Agent(
workspace_id=workspace_id,
name=data["name"],
description=data.get("description"),
system_prompt=data.get("system_prompt"),
model_id=data.get("model_id"),
temperature=data.get("temperature", 0.70),
max_tokens=data.get("max_tokens", 2000),
)
agent.save()
return agent
@staticmethod
def update_agent(agent, data):
if "name" in data:
agent.name = data["name"]
if "description" in data:
agent.description = data["description"]
if "system_prompt" in data:
agent.system_prompt = data["system_prompt"]
if "model_id" in data:
agent.model_id = data["model_id"]
if "temperature" in data:
agent.temperature = data["temperature"]
if "max_tokens" in data:
agent.max_tokens = data["max_tokens"]
if "is_active" in data:
agent.is_active = data["is_active"]
agent.save()
return agent
@staticmethod
def delete_agent(agent):
agent.delete()
+34
View File
@@ -0,0 +1,34 @@
import bcrypt
from ..models import User
class AuthService:
@staticmethod
def register(username, password, email=None):
if User.query.filter_by(username=username).first():
raise ValueError("用户名已存在")
if email and User.query.filter_by(email=email).first():
raise ValueError("邮箱已被使用")
hashed_password = bcrypt.hashpw(
password.encode("utf-8"), bcrypt.gensalt()
).decode("utf-8")
user = User(username=username, password=hashed_password, email=email)
user.save()
return user
@staticmethod
def login(username, password):
user = User.query.filter_by(username=username).first()
if not user:
raise ValueError("用户名或密码错误")
if not bcrypt.checkpw(password.encode("utf-8"), user.password.encode("utf-8")):
raise ValueError("用户名或密码错误")
if not user.is_active:
raise ValueError("用户已被禁用")
return user
+35
View File
@@ -0,0 +1,35 @@
from ..models import Channel
class ChannelService:
@staticmethod
def create_channel(data):
channel = Channel(
type=data["type"],
name=data["name"],
config=data["config"],
is_active=data.get("is_active", True),
)
channel.save()
return channel
@staticmethod
def update_channel(channel, data):
if "name" in data:
channel.name = data["name"]
if "config" in data:
channel.config = data["config"]
if "is_active" in data:
channel.is_active = data["is_active"]
channel.save()
return channel
@staticmethod
def delete_channel(channel_id):
channel = Channel.query.get(channel_id)
if not channel:
raise ValueError("渠道不存在")
channel.delete()
+24
View File
@@ -0,0 +1,24 @@
from ..models import Conversation
class ConversationService:
@staticmethod
def create_conversation(workspace_id, data):
conversation = Conversation(
workspace_id=workspace_id,
agent_id=data["agent_id"],
title=data.get("title"),
)
conversation.save()
return conversation
@staticmethod
def update_conversation(conversation, data):
if "title" in data:
conversation.title = data["title"]
if "status" in data:
conversation.status = data["status"]
conversation.save()
return conversation
+39
View File
@@ -0,0 +1,39 @@
from ..models import CronJob
class CronJobService:
@staticmethod
def create_cron_job(agent_id, data):
cron_job = CronJob(
agent_id=agent_id,
name=data["name"],
cron_expression=data["cron_expression"],
prompt=data["prompt"],
is_active=data.get("is_active", True),
)
cron_job.save()
return cron_job
@staticmethod
def update_cron_job(cron_job, data):
if "name" in data:
cron_job.name = data["name"]
if "cron_expression" in data:
cron_job.cron_expression = data["cron_expression"]
if "prompt" in data:
cron_job.prompt = data["prompt"]
if "is_active" in data:
cron_job.is_active = data["is_active"]
cron_job.save()
return cron_job
@staticmethod
def delete_cron_job(cron_job_id):
cron_job = CronJob.query.get(cron_job_id)
if not cron_job:
raise ValueError("定时任务不存在")
cron_job.delete()
+34
View File
@@ -0,0 +1,34 @@
from ..models import Memory
class MemoryService:
@staticmethod
def create_memory(workspace_id, data):
memory = Memory(
workspace_id=workspace_id,
agent_id=data.get("agent_id"),
type=data["type"],
content=data["content"],
tags=data.get("tags"),
importance=data.get("importance", 5),
)
memory.save()
return memory
@staticmethod
def update_memory(memory, data):
if "content" in data:
memory.content = data["content"]
if "tags" in data:
memory.tags = data["tags"]
if "importance" in data:
memory.importance = data["importance"]
memory.save()
return memory
@staticmethod
def delete_memory(memory):
memory.delete()
+15
View File
@@ -0,0 +1,15 @@
from ..models import Message
class MessageService:
@staticmethod
def create_message(conversation_id, data):
message = Message(
conversation_id=conversation_id,
role=data["role"],
content=data["content"],
tool_calls=data.get("tool_calls"),
metadata=data.get("metadata"),
)
message.save()
return message
+51
View File
@@ -0,0 +1,51 @@
from ..models import Model
class ModelService:
@staticmethod
def create_model(data):
model = Model(
provider=data["provider"],
name=data["name"],
model_id=data["model_id"],
api_key=data.get("api_key"),
base_url=data.get("base_url"),
is_default=data.get("is_default", False),
is_active=data.get("is_active", True),
)
if model.is_default:
Model.query.filter_by(is_default=True).update({"is_default": False})
model.save()
return model
@staticmethod
def update_model(model, data):
if "name" in data:
model.name = data["name"]
if "api_key" in data:
model.api_key = data["api_key"]
if "base_url" in data:
model.base_url = data["base_url"]
if "is_default" in data and data["is_default"]:
Model.query.filter(Model.id != model.id, Model.is_default == True).update(
{"is_default": False}
)
model.is_default = True
if "is_active" in data:
model.is_active = data["is_active"]
model.save()
return model
@staticmethod
def delete_model(model_id):
model = Model.query.get(model_id)
if model.is_default:
raise ValueError("默认模型不能删除")
model.delete()
+29
View File
@@ -0,0 +1,29 @@
from ..models import Skill
class SkillService:
@staticmethod
def install_skill(skill_id):
skill = Skill.query.get(skill_id)
if not skill:
raise ValueError("技能不存在")
if skill.is_installed:
raise ValueError("技能已安装")
skill.is_installed = True
skill.save()
return skill
@staticmethod
def uninstall_skill(skill_id):
skill = Skill.query.get(skill_id)
if not skill:
raise ValueError("技能不存在")
if not skill.is_installed:
raise ValueError("技能未安装")
skill.is_installed = False
skill.save()
return skill
+35
View File
@@ -0,0 +1,35 @@
from ..models import Tool, AgentTool
class ToolService:
@staticmethod
def create_tool(data):
if Tool.query.filter_by(name=data["name"]).first():
raise ValueError("工具名称已存在")
tool = Tool(
name=data["name"],
type=data["type"],
description=data.get("description"),
config=data.get("config"),
)
tool.save()
return tool
@staticmethod
def add_tool_to_agent(agent_id, tool_id):
if AgentTool.query.filter_by(agent_id=agent_id, tool_id=tool_id).first():
raise ValueError("工具已添加到此 Agent")
agent_tool = AgentTool(agent_id=agent_id, tool_id=tool_id)
agent_tool.save()
return agent_tool
@staticmethod
def remove_tool_from_agent(agent_id, tool_id):
agent_tool = AgentTool.query.filter_by(
agent_id=agent_id, tool_id=tool_id
).first()
if not agent_tool:
raise ValueError("工具关联不存在")
agent_tool.delete()
+21
View File
@@ -0,0 +1,21 @@
from ..models import User
class UserService:
@staticmethod
def update_user(user, data):
if "email" in data:
if (
data["email"]
and User.query.filter(
User.email == data["email"], User.id != user.id
).first()
):
raise ValueError("邮箱已被使用")
user.email = data["email"]
if "avatar" in data:
user.avatar = data["avatar"]
user.save()
return user
+26
View File
@@ -0,0 +1,26 @@
from ..models import Workspace
class WorkspaceService:
@staticmethod
def create_workspace(user_id, name, description=None):
workspace = Workspace(user_id=user_id, name=name, description=description)
workspace.save()
return workspace
@staticmethod
def update_workspace(workspace, data):
if "name" in data:
workspace.name = data["name"]
if "description" in data:
workspace.description = data["description"]
workspace.save()
return workspace
@staticmethod
def delete_workspace(workspace):
if workspace.is_default:
raise ValueError("默认工作空间不能删除")
workspace.delete()