app.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import uuid
  2. from flask_login import current_user
  3. from flask_restful import Resource, inputs, marshal, marshal_with, reqparse
  4. from werkzeug.exceptions import BadRequest, Forbidden, abort
  5. from controllers.console import api
  6. from controllers.console.app.wraps import get_app_model
  7. from controllers.console.setup import setup_required
  8. from controllers.console.wraps import account_initialization_required, cloud_edition_billing_resource_check
  9. from core.ops.ops_trace_manager import OpsTraceManager
  10. from fields.app_fields import (
  11. app_detail_fields,
  12. app_detail_fields_with_site,
  13. app_pagination_fields,
  14. )
  15. from libs.login import login_required
  16. from services.app_service import AppService
  17. ALLOW_CREATE_APP_MODES = ['chat', 'agent-chat', 'advanced-chat', 'workflow', 'completion']
  18. class AppListApi(Resource):
  19. @setup_required
  20. @login_required
  21. @account_initialization_required
  22. def get(self):
  23. """Get app list"""
  24. def uuid_list(value):
  25. try:
  26. return [str(uuid.UUID(v)) for v in value.split(',')]
  27. except ValueError:
  28. abort(400, message="Invalid UUID format in tag_ids.")
  29. parser = reqparse.RequestParser()
  30. parser.add_argument('page', type=inputs.int_range(1, 99999), required=False, default=1, location='args')
  31. parser.add_argument('limit', type=inputs.int_range(1, 100), required=False, default=20, location='args')
  32. parser.add_argument('mode', type=str, choices=['chat', 'workflow', 'agent-chat', 'channel', 'all'], default='all', location='args', required=False)
  33. parser.add_argument('name', type=str, location='args', required=False)
  34. parser.add_argument('tag_ids', type=uuid_list, location='args', required=False)
  35. args = parser.parse_args()
  36. # get app list
  37. app_service = AppService()
  38. app_pagination = app_service.get_paginate_apps(current_user.current_tenant_id, args)
  39. if not app_pagination:
  40. return {'data': [], 'total': 0, 'page': 1, 'limit': 20, 'has_more': False}
  41. return marshal(app_pagination, app_pagination_fields)
  42. @setup_required
  43. @login_required
  44. @account_initialization_required
  45. @marshal_with(app_detail_fields)
  46. @cloud_edition_billing_resource_check('apps')
  47. def post(self):
  48. """Create app"""
  49. parser = reqparse.RequestParser()
  50. parser.add_argument('name', type=str, required=True, location='json')
  51. parser.add_argument('description', type=str, location='json')
  52. parser.add_argument('mode', type=str, choices=ALLOW_CREATE_APP_MODES, location='json')
  53. parser.add_argument('icon', type=str, location='json')
  54. parser.add_argument('icon_background', type=str, location='json')
  55. args = parser.parse_args()
  56. # The role of the current user in the ta table must be admin, owner, or editor
  57. if not current_user.is_editor:
  58. raise Forbidden()
  59. if 'mode' not in args or args['mode'] is None:
  60. raise BadRequest("mode is required")
  61. app_service = AppService()
  62. app = app_service.create_app(current_user.current_tenant_id, args, current_user)
  63. return app, 201
  64. class AppImportApi(Resource):
  65. @setup_required
  66. @login_required
  67. @account_initialization_required
  68. @marshal_with(app_detail_fields_with_site)
  69. @cloud_edition_billing_resource_check('apps')
  70. def post(self):
  71. """Import app"""
  72. # The role of the current user in the ta table must be admin, owner, or editor
  73. if not current_user.is_editor:
  74. raise Forbidden()
  75. parser = reqparse.RequestParser()
  76. parser.add_argument('data', type=str, required=True, nullable=False, location='json')
  77. parser.add_argument('name', type=str, location='json')
  78. parser.add_argument('description', type=str, location='json')
  79. parser.add_argument('icon', type=str, location='json')
  80. parser.add_argument('icon_background', type=str, location='json')
  81. args = parser.parse_args()
  82. app_service = AppService()
  83. app = app_service.import_app(current_user.current_tenant_id, args['data'], args, current_user)
  84. return app, 201
  85. class AppApi(Resource):
  86. @setup_required
  87. @login_required
  88. @account_initialization_required
  89. @get_app_model
  90. @marshal_with(app_detail_fields_with_site)
  91. def get(self, app_model):
  92. """Get app detail"""
  93. app_service = AppService()
  94. app_model = app_service.get_app(app_model)
  95. return app_model
  96. @setup_required
  97. @login_required
  98. @account_initialization_required
  99. @get_app_model
  100. @marshal_with(app_detail_fields_with_site)
  101. def put(self, app_model):
  102. """Update app"""
  103. # The role of the current user in the ta table must be admin, owner, or editor
  104. if not current_user.is_editor:
  105. raise Forbidden()
  106. parser = reqparse.RequestParser()
  107. parser.add_argument('name', type=str, required=True, nullable=False, location='json')
  108. parser.add_argument('description', type=str, location='json')
  109. parser.add_argument('icon', type=str, location='json')
  110. parser.add_argument('icon_background', type=str, location='json')
  111. args = parser.parse_args()
  112. app_service = AppService()
  113. app_model = app_service.update_app(app_model, args)
  114. return app_model
  115. @setup_required
  116. @login_required
  117. @account_initialization_required
  118. @get_app_model
  119. def delete(self, app_model):
  120. """Delete app"""
  121. # The role of the current user in the ta table must be admin, owner, or editor
  122. if not current_user.is_editor:
  123. raise Forbidden()
  124. app_service = AppService()
  125. app_service.delete_app(app_model)
  126. return {'result': 'success'}, 204
  127. class AppCopyApi(Resource):
  128. @setup_required
  129. @login_required
  130. @account_initialization_required
  131. @get_app_model
  132. @marshal_with(app_detail_fields_with_site)
  133. def post(self, app_model):
  134. """Copy app"""
  135. # The role of the current user in the ta table must be admin, owner, or editor
  136. if not current_user.is_editor:
  137. raise Forbidden()
  138. parser = reqparse.RequestParser()
  139. parser.add_argument('name', type=str, location='json')
  140. parser.add_argument('description', type=str, location='json')
  141. parser.add_argument('icon', type=str, location='json')
  142. parser.add_argument('icon_background', type=str, location='json')
  143. args = parser.parse_args()
  144. app_service = AppService()
  145. data = app_service.export_app(app_model)
  146. app = app_service.import_app(current_user.current_tenant_id, data, args, current_user)
  147. return app, 201
  148. class AppExportApi(Resource):
  149. @setup_required
  150. @login_required
  151. @account_initialization_required
  152. @get_app_model
  153. def get(self, app_model):
  154. """Export app"""
  155. app_service = AppService()
  156. return {
  157. "data": app_service.export_app(app_model)
  158. }
  159. class AppNameApi(Resource):
  160. @setup_required
  161. @login_required
  162. @account_initialization_required
  163. @get_app_model
  164. @marshal_with(app_detail_fields)
  165. def post(self, app_model):
  166. # The role of the current user in the ta table must be admin, owner, or editor
  167. if not current_user.is_editor:
  168. raise Forbidden()
  169. parser = reqparse.RequestParser()
  170. parser.add_argument('name', type=str, required=True, location='json')
  171. args = parser.parse_args()
  172. app_service = AppService()
  173. app_model = app_service.update_app_name(app_model, args.get('name'))
  174. return app_model
  175. class AppIconApi(Resource):
  176. @setup_required
  177. @login_required
  178. @account_initialization_required
  179. @get_app_model
  180. @marshal_with(app_detail_fields)
  181. def post(self, app_model):
  182. # The role of the current user in the ta table must be admin, owner, or editor
  183. if not current_user.is_editor:
  184. raise Forbidden()
  185. parser = reqparse.RequestParser()
  186. parser.add_argument('icon', type=str, location='json')
  187. parser.add_argument('icon_background', type=str, location='json')
  188. args = parser.parse_args()
  189. app_service = AppService()
  190. app_model = app_service.update_app_icon(app_model, args.get('icon'), args.get('icon_background'))
  191. return app_model
  192. class AppSiteStatus(Resource):
  193. @setup_required
  194. @login_required
  195. @account_initialization_required
  196. @get_app_model
  197. @marshal_with(app_detail_fields)
  198. def post(self, app_model):
  199. # The role of the current user in the ta table must be admin, owner, or editor
  200. if not current_user.is_editor:
  201. raise Forbidden()
  202. parser = reqparse.RequestParser()
  203. parser.add_argument('enable_site', type=bool, required=True, location='json')
  204. args = parser.parse_args()
  205. app_service = AppService()
  206. app_model = app_service.update_app_site_status(app_model, args.get('enable_site'))
  207. return app_model
  208. class AppApiStatus(Resource):
  209. @setup_required
  210. @login_required
  211. @account_initialization_required
  212. @get_app_model
  213. @marshal_with(app_detail_fields)
  214. def post(self, app_model):
  215. # The role of the current user in the ta table must be admin or owner
  216. if not current_user.is_admin_or_owner:
  217. raise Forbidden()
  218. parser = reqparse.RequestParser()
  219. parser.add_argument('enable_api', type=bool, required=True, location='json')
  220. args = parser.parse_args()
  221. app_service = AppService()
  222. app_model = app_service.update_app_api_status(app_model, args.get('enable_api'))
  223. return app_model
  224. class AppTraceApi(Resource):
  225. @setup_required
  226. @login_required
  227. @account_initialization_required
  228. def get(self, app_id):
  229. """Get app trace"""
  230. app_trace_config = OpsTraceManager.get_app_tracing_config(
  231. app_id=app_id
  232. )
  233. return app_trace_config
  234. @setup_required
  235. @login_required
  236. @account_initialization_required
  237. def post(self, app_id):
  238. # add app trace
  239. if not current_user.is_admin_or_owner:
  240. raise Forbidden()
  241. parser = reqparse.RequestParser()
  242. parser.add_argument('enabled', type=bool, required=True, location='json')
  243. parser.add_argument('tracing_provider', type=str, required=True, location='json')
  244. args = parser.parse_args()
  245. OpsTraceManager.update_app_tracing_config(
  246. app_id=app_id,
  247. enabled=args['enabled'],
  248. tracing_provider=args['tracing_provider'],
  249. )
  250. return {"result": "success"}
  251. api.add_resource(AppListApi, '/apps')
  252. api.add_resource(AppImportApi, '/apps/import')
  253. api.add_resource(AppApi, '/apps/<uuid:app_id>')
  254. api.add_resource(AppCopyApi, '/apps/<uuid:app_id>/copy')
  255. api.add_resource(AppExportApi, '/apps/<uuid:app_id>/export')
  256. api.add_resource(AppNameApi, '/apps/<uuid:app_id>/name')
  257. api.add_resource(AppIconApi, '/apps/<uuid:app_id>/icon')
  258. api.add_resource(AppSiteStatus, '/apps/<uuid:app_id>/site-enable')
  259. api.add_resource(AppApiStatus, '/apps/<uuid:app_id>/api-enable')
  260. api.add_resource(AppTraceApi, '/apps/<uuid:app_id>/trace')