app.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. parser.add_argument('max_active_requests', type=int, location='json')
  112. args = parser.parse_args()
  113. app_service = AppService()
  114. app_model = app_service.update_app(app_model, args)
  115. return app_model
  116. @setup_required
  117. @login_required
  118. @account_initialization_required
  119. @get_app_model
  120. def delete(self, app_model):
  121. """Delete app"""
  122. # The role of the current user in the ta table must be admin, owner, or editor
  123. if not current_user.is_editor:
  124. raise Forbidden()
  125. app_service = AppService()
  126. app_service.delete_app(app_model)
  127. return {'result': 'success'}, 204
  128. class AppCopyApi(Resource):
  129. @setup_required
  130. @login_required
  131. @account_initialization_required
  132. @get_app_model
  133. @marshal_with(app_detail_fields_with_site)
  134. def post(self, app_model):
  135. """Copy app"""
  136. # The role of the current user in the ta table must be admin, owner, or editor
  137. if not current_user.is_editor:
  138. raise Forbidden()
  139. parser = reqparse.RequestParser()
  140. parser.add_argument('name', type=str, location='json')
  141. parser.add_argument('description', type=str, location='json')
  142. parser.add_argument('icon', type=str, location='json')
  143. parser.add_argument('icon_background', type=str, location='json')
  144. args = parser.parse_args()
  145. app_service = AppService()
  146. data = app_service.export_app(app_model)
  147. app = app_service.import_app(current_user.current_tenant_id, data, args, current_user)
  148. return app, 201
  149. class AppExportApi(Resource):
  150. @setup_required
  151. @login_required
  152. @account_initialization_required
  153. @get_app_model
  154. def get(self, app_model):
  155. """Export app"""
  156. # The role of the current user in the ta table must be admin, owner, or editor
  157. if not current_user.is_editor:
  158. raise Forbidden()
  159. app_service = AppService()
  160. return {
  161. "data": app_service.export_app(app_model)
  162. }
  163. class AppNameApi(Resource):
  164. @setup_required
  165. @login_required
  166. @account_initialization_required
  167. @get_app_model
  168. @marshal_with(app_detail_fields)
  169. def post(self, app_model):
  170. # The role of the current user in the ta table must be admin, owner, or editor
  171. if not current_user.is_editor:
  172. raise Forbidden()
  173. parser = reqparse.RequestParser()
  174. parser.add_argument('name', type=str, required=True, location='json')
  175. args = parser.parse_args()
  176. app_service = AppService()
  177. app_model = app_service.update_app_name(app_model, args.get('name'))
  178. return app_model
  179. class AppIconApi(Resource):
  180. @setup_required
  181. @login_required
  182. @account_initialization_required
  183. @get_app_model
  184. @marshal_with(app_detail_fields)
  185. def post(self, app_model):
  186. # The role of the current user in the ta table must be admin, owner, or editor
  187. if not current_user.is_editor:
  188. raise Forbidden()
  189. parser = reqparse.RequestParser()
  190. parser.add_argument('icon', type=str, location='json')
  191. parser.add_argument('icon_background', type=str, location='json')
  192. args = parser.parse_args()
  193. app_service = AppService()
  194. app_model = app_service.update_app_icon(app_model, args.get('icon'), args.get('icon_background'))
  195. return app_model
  196. class AppSiteStatus(Resource):
  197. @setup_required
  198. @login_required
  199. @account_initialization_required
  200. @get_app_model
  201. @marshal_with(app_detail_fields)
  202. def post(self, app_model):
  203. # The role of the current user in the ta table must be admin, owner, or editor
  204. if not current_user.is_editor:
  205. raise Forbidden()
  206. parser = reqparse.RequestParser()
  207. parser.add_argument('enable_site', type=bool, required=True, location='json')
  208. args = parser.parse_args()
  209. app_service = AppService()
  210. app_model = app_service.update_app_site_status(app_model, args.get('enable_site'))
  211. return app_model
  212. class AppApiStatus(Resource):
  213. @setup_required
  214. @login_required
  215. @account_initialization_required
  216. @get_app_model
  217. @marshal_with(app_detail_fields)
  218. def post(self, app_model):
  219. # The role of the current user in the ta table must be admin or owner
  220. if not current_user.is_admin_or_owner:
  221. raise Forbidden()
  222. parser = reqparse.RequestParser()
  223. parser.add_argument('enable_api', type=bool, required=True, location='json')
  224. args = parser.parse_args()
  225. app_service = AppService()
  226. app_model = app_service.update_app_api_status(app_model, args.get('enable_api'))
  227. return app_model
  228. class AppTraceApi(Resource):
  229. @setup_required
  230. @login_required
  231. @account_initialization_required
  232. def get(self, app_id):
  233. """Get app trace"""
  234. app_trace_config = OpsTraceManager.get_app_tracing_config(
  235. app_id=app_id
  236. )
  237. return app_trace_config
  238. @setup_required
  239. @login_required
  240. @account_initialization_required
  241. def post(self, app_id):
  242. # add app trace
  243. if not current_user.is_admin_or_owner:
  244. raise Forbidden()
  245. parser = reqparse.RequestParser()
  246. parser.add_argument('enabled', type=bool, required=True, location='json')
  247. parser.add_argument('tracing_provider', type=str, required=True, location='json')
  248. args = parser.parse_args()
  249. OpsTraceManager.update_app_tracing_config(
  250. app_id=app_id,
  251. enabled=args['enabled'],
  252. tracing_provider=args['tracing_provider'],
  253. )
  254. return {"result": "success"}
  255. api.add_resource(AppListApi, '/apps')
  256. api.add_resource(AppImportApi, '/apps/import')
  257. api.add_resource(AppApi, '/apps/<uuid:app_id>')
  258. api.add_resource(AppCopyApi, '/apps/<uuid:app_id>/copy')
  259. api.add_resource(AppExportApi, '/apps/<uuid:app_id>/export')
  260. api.add_resource(AppNameApi, '/apps/<uuid:app_id>/name')
  261. api.add_resource(AppIconApi, '/apps/<uuid:app_id>/icon')
  262. api.add_resource(AppSiteStatus, '/apps/<uuid:app_id>/site-enable')
  263. api.add_resource(AppApiStatus, '/apps/<uuid:app_id>/api-enable')
  264. api.add_resource(AppTraceApi, '/apps/<uuid:app_id>/trace')