Skip to content

Commit

Permalink
add form schemas.
Browse files Browse the repository at this point in the history
  • Loading branch information
GarfieldDai committed Oct 24, 2023
1 parent 41e452d commit b42d096
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json → api/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "api/app.py",
"FLASK_APP": "app.py",
"FLASK_DEBUG": "1",
"GEVENT_SUPPORT": "True"
},
Expand All @@ -21,7 +21,7 @@
"--debug"
],
"jinja": true,
"justMyCode": true
"justMyCode": false
}
]
}
2 changes: 1 addition & 1 deletion api/controllers/console/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
api = ExternalApi(bp)

# Import other controllers
from . import setup, version, apikey, admin
from . import extension, setup, version, apikey, admin

# Import app controllers
from .app import advanced_prompt_template, app, site, completion, model_config, statistic, conversation, message, generator, audio
Expand Down
23 changes: 23 additions & 0 deletions api/controllers/console/extension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from flask_restful import Resource, reqparse

from controllers.console import api
from controllers.console.setup import setup_required
from controllers.console.wraps import account_initialization_required
from libs.login import login_required
from services.extension_service import ExtensionService


class Extension(Resource):

@setup_required
@login_required
@account_initialization_required
def get(self):
parser = reqparse.RequestParser()
parser.add_argument('module', type=str, required=True, location='args')
args = parser.parse_args()

return ExtensionService.get_code_based_extensions(args['module'])


api.add_resource(Extension, '/code-based-extensions')
1 change: 1 addition & 0 deletions api/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from core.moderation.cloud_service.cloud_service import CloudService
27 changes: 27 additions & 0 deletions api/core/helper/extensible.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import json
import os

extensions = {}

class Extensible:

def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
cls.register()

@classmethod
def register(cls):
subclass_path = os.path.abspath(cls.__module__.replace(".", os.path.sep) + '.py')
subclass_dir_path = os.path.dirname(subclass_path)
parent_folder_name = os.path.basename(os.path.dirname(subclass_dir_path))

json_path = os.path.join(subclass_dir_path, 'schema.json')
json_data = {}
if os.path.exists(json_path):
with open(json_path, 'r') as f:
json_data = json.load(f)

if parent_folder_name not in extensions:
extensions[parent_folder_name] = []

extensions[parent_folder_name].append(json_data)
Empty file added api/core/moderation/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions api/core/moderation/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from core.helper.extensible import Extensible

class BaseModeration(Extensible):
pass
Empty file.
4 changes: 4 additions & 0 deletions api/core/moderation/cloud_service/cloud_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from core.moderation.base import BaseModeration

class CloudService(BaseModeration):
pass
54 changes: 54 additions & 0 deletions api/core/moderation/cloud_service/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "cloud_service",
"label": {
"en-US": "Cloud Service",
"zh-Hans": "云服务"
},
"form_schema": [
{
"select": {
"label": {
"en-US": "Cloud Provider",
"zh-Hans": "云计算厂商"
},
"variable": "cloud_provider",
"required": true,
"options": [
"腾讯云",
"阿里云",
"AWS"
],
"default": "",
"placeholder": ""
}
},
{
"text-input": {
"label": {
"en-US": "API Endpoint",
"zh-Hans": "API Endpoint"
},
"variable": "api_endpoint",
"required": true,
"max-length": 100,
"default": "",
"placeholder": ""
}
},
{
"paragraph": {
"label": {
"en-US": "API Key",
"zh-Hans": "API Key"
},
"variable": "api_keys",
"required": true,
"default": "",
"placeholder": ""
}
}
],
"extract-config": {
"input_d": true
}
}
7 changes: 7 additions & 0 deletions api/services/extension_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from core.helper.extensible import extensions

class ExtensionService:

@classmethod
def get_code_based_extensions(cls, module: str) -> list[dict]:
return extensions.get(module, [])

0 comments on commit b42d096

Please sign in to comment.