Skip to content

Commit

Permalink
Merge pull request #1 from difizen/ximo_dev
Browse files Browse the repository at this point in the history
feat(api): add plugin database & api
  • Loading branch information
sunshinesmilelk authored Jul 3, 2024
2 parents ede594c + 9c4fed3 commit 71bb53f
Show file tree
Hide file tree
Showing 9 changed files with 562 additions and 2 deletions.
78 changes: 78 additions & 0 deletions api/alembic/versions/ac21c38c5e56_add_plugin_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""add plugin table
Revision ID: ac21c38c5e56
Revises: 5918599719a1
Create Date: 2024-07-02 09:56:29.437606
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'ac21c38c5e56'
down_revision: Union[str, None] = '5918599719a1'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('plugin',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('plugin_type', sa.Integer(), nullable=False),
sa.Column('created_by', sa.Integer(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_by', sa.Integer(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_table('plugin_config',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
sa.Column('avatar', sa.String(length=255), nullable=True),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('plugin_id', sa.Integer(), nullable=True),
sa.Column('is_draft', sa.Boolean(), nullable=False),
sa.Column('created_by', sa.Integer(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_by', sa.Integer(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['plugin_id'], ['plugin.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('plugin_api',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('plugin_config_id', sa.Integer(), nullable=True),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('openapi_desc', sa.Text(), nullable=True),
sa.Column('disabled', sa.Boolean(), nullable=False),
sa.Column('created_by', sa.Integer(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_by', sa.Integer(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['plugin_config_id'], [
'plugin_config.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.alter_column('messages', 'message_type',
type_=sa.VARCHAR(length=50),
existing_type=sa.Enum(
'MARKDOWN', 'TEXT', name='messagetype'),
existing_nullable=False)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('messages', 'message_type',
existing_type=sa.Enum(
'MARKDOWN', 'TEXT', name='messagetype'),
type_=sa.VARCHAR(length=50),
existing_nullable=False)
op.drop_table('plugin_api')
op.drop_table('plugin_config')
op.drop_table('plugin')
# ### end Alembic commands ###
8 changes: 7 additions & 1 deletion api/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from .agent_config import AgentConfigORM
from .account import AccountORM
from .chat import ChatORM, MessageORM
from .plugin import PluginORM
from .plugin_config import PluginConfigORM
from .plugin_api import PluginApiORM

__all__ = [
'Base',
Expand All @@ -14,5 +17,8 @@
'AgentBotORM',
'AgentConfigORM',
'ChatORM',
'MessageORM'
'MessageORM',
'PluginORM',
'PluginConfigORM',
'PluginApiORM'
]
60 changes: 60 additions & 0 deletions api/models/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from typing import Optional
from pydantic import BaseModel
from datetime import datetime

from sqlalchemy import (
Column,
Integer,
DateTime,
)

from db import Base
from models.plugin_config import PluginConfigModel


class PluginORM(Base):
'''
plugin database model
'''
__tablename__ = "plugin"
id = Column(Integer, primary_key=True)
plugin_type = Column(Integer, nullable=False) # 插件创建类型
created_by = Column(Integer)
created_at = Column(
DateTime(), nullable=False, default=datetime.now
)
updated_by = Column(Integer)
updated_at = Column(DateTime(),
nullable=False, onupdate=datetime.now)
# statistic_data = Column(JSON) # 保留字段,用于存储诸如 被多少个bot 引用了


class PluginCreate(BaseModel):
'''
plugin create
'''
plugin_type: int


class PluginUpdate(BaseModel):
'''
plugin update
'''
id: int
plugin_type: int


class PluginModel(PluginCreate):
'''
plugin
'''
id: int
plugin_type: int
created_by: int
created_at: datetime
updated_by: int
updated_at: datetime
draft: Optional[PluginConfigModel] = None

class Config:
from_attributes = True
84 changes: 84 additions & 0 deletions api/models/plugin_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from typing import Optional
from pydantic import BaseModel
from datetime import datetime

from sqlalchemy import (
Column,
ForeignKey,
Integer,
DateTime,
Text,
Boolean
)

from db import Base


class PluginApiORM(Base):
'''
plugin api model
'''
__tablename__ = "plugin_api"
id = Column(Integer, primary_key=True)
plugin_config_id = Column(Integer, ForeignKey(
"plugin_config.id"), nullable=True)
description = Column(Text) # API 描述
openapi_desc = Column(Text) # openapi 先调研
# request_params = Column(JSON) # 入参
# response_params = Column(JSON) # 出参
# debug_example = Column(JSON) # 调试示例
# debug_example_status = Column(Integer) # 调试示例状态
# debug_status = Column(Integer) # 调试状态
disabled = Column(Boolean, nullable=False, default=True)
# online_status = Column(Integer) # 服务状态
# path = Column(String(255)) #路径
created_by = Column(Integer)
created_at = Column(
DateTime(), nullable=False, default=datetime.now,
)
updated_by = Column(Integer)
updated_at = Column(DateTime(),
nullable=False, onupdate=datetime.now)
# statistic_data = Column(JSON) # 保留字段,用于存储诸如 被多少个bot 引用了


class PluginApiCreate(BaseModel):
'''
plugin api create
'''
plugin_config_id: int
description: str
openapi_desc: str
disabled: bool
created_by: int
created_at: datetime
updated_by: int
updated_at: datetime


class PluginApiUpdate(BaseModel):
'''
plugin api update
'''
id: int
description: str
openapi_desc: str
disabled: bool


class PluginApiModel(PluginApiCreate):
'''
plugin api
'''
id: int
plugin_config_id: int
description: str
openapi_desc: str
disabled: bool
created_by: int
created_at: datetime
updated_by: int
updated_at: datetime

class Config:
from_attributes = True
79 changes: 79 additions & 0 deletions api/models/plugin_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from typing import Optional
from datetime import datetime
from pydantic import BaseModel
from sqlalchemy import (
Boolean,
Column,
Integer,
DateTime,
ForeignKey,
String,
Text,
)

from db import Base


class PluginConfigORM(Base):
'''
plugin config database model
'''
__tablename__ = "plugin_config"
id = Column(Integer, primary_key=True)
name = Column(String(255), nullable=False) # 插件名字
avatar = Column(String(255)) # 插件图标
description = Column(Text) # 插件描述
# meta_info = Column(JSON) # 插件元信息,包含 api url,service_token,oauth_info 等
plugin_id = Column(Integer, ForeignKey("plugin.id"), nullable=True)
is_draft = Column(Boolean, nullable=False) # 是否为草稿
# openapi_desc = Column(Text) # openapi 先调研
# plugin_desc = Column(Text)
created_by = Column(Integer)
created_at = Column(
DateTime(), nullable=False, default=datetime.now,
)
updated_by = Column(Integer)
updated_at = Column(DateTime(),
nullable=False, onupdate=datetime.now)


class PluginConfigCreate(BaseModel):
'''
plugin config create
'''
plugin_id: int
name: str
avatar: str
description: str

is_draft: Optional[bool]


class PluginConfigUpdate(BaseModel):
'''
plugin config update
'''
id: int
is_draft: Optional[bool]
name: str
avatar: str
description: str


class PluginConfigModel(PluginConfigCreate):
'''
plugin config
'''
id: int
name: str
avatar: str
description: str

plugin_id: int
created_by: int
created_at: datetime
updated_by: int
updated_at: datetime

class Config:
from_attributes = True
3 changes: 2 additions & 1 deletion api/routers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
from .agent.router import agent_router
from .account.router import account_router
from .chat.router import chat_router
from .plugin.router import plugin_router

api_router = APIRouter()

api_router.include_router(agent_router, prefix="/agent", tags=["agent"])
api_router.include_router(account_router, prefix="/accounts", tags=["account"])
api_router.include_router(chat_router, prefix="/chats", tags=["chat"])

api_router.include_router(plugin_router, prefix="/plugins", tags=["plugin"])

COUNTER = 0

Expand Down
Empty file added api/routers/plugin/__init__.py
Empty file.
Loading

0 comments on commit 71bb53f

Please sign in to comment.