Centralize JavaScript asset management:

- Introduce `ui_assets.json` to define script asset groups for core, workflow, and page scripts.
- Update templates to dynamically load scripts from `ui_assets.json`.
- Enhance maintainability by centralizing script paths.
This commit is contained in:
2026-01-09 21:03:18 +00:00
parent 6beb4d895a
commit adcf8f2e3b
4 changed files with 55 additions and 35 deletions

View File

@@ -162,6 +162,19 @@ def get_metadata():
with open(metadata_path, "r", encoding="utf-8") as f:
return json.load(f)
def get_ui_assets():
assets_path = os.path.join(os.path.dirname(__file__), "ui_assets.json")
if not os.path.exists(assets_path):
return {"core_scripts": [], "workflow_scripts": [], "page_scripts": []}
try:
with open(assets_path, "r", encoding="utf-8") as f:
data = json.load(f)
except json.JSONDecodeError:
return {"core_scripts": [], "workflow_scripts": [], "page_scripts": []}
if not isinstance(data, dict):
return {"core_scripts": [], "workflow_scripts": [], "page_scripts": []}
return data
def load_translation_file(messages_map, lang):
pkg_dir = os.path.dirname(os.path.dirname(__file__))
messages_file = messages_map.get(lang, f"messages_{lang}.json")
@@ -285,6 +298,7 @@ async def read_item(request: Request, username: str = Depends(get_current_user))
env_vars = get_env_vars()
translations = get_translations()
metadata = get_metadata()
ui_assets = get_ui_assets()
prompt_content = get_prompt_content()
workflow_content = get_workflow_content()
is_running = bot_process is not None or mock_running
@@ -300,6 +314,7 @@ async def read_item(request: Request, username: str = Depends(get_current_user))
"env_vars": env_vars,
"translations": translations,
"metadata": metadata,
"ui_assets": ui_assets,
"prompt_content": prompt_content,
"workflow_content": workflow_content,
"is_running": is_running,

View File

@@ -34,17 +34,7 @@
<!-- Choices.js -->
<script src="https://cdn.jsdelivr.net/npm/choices.js@10.2.0/public/assets/scripts/choices.min.js"></script>
{% set core_scripts = [
'/static/js/app_context.js',
'/static/js/plugin_registry.js',
'/static/js/services/toast.js',
'/static/js/plugins/theme_manager.js',
'/static/js/plugins/navigation_manager.js',
'/static/js/plugins/choices_manager.js',
'/static/js/plugins/workflow_toggle.js',
'/static/js/plugins/form_validator.js',
'/static/js/plugins/status_poller.js'
] %}
{% set core_scripts = ui_assets.get('core_scripts', []) if ui_assets else [] %}
{% for script in core_scripts %}
<script src="{{ script }}"></script>
{% endfor %}

View File

@@ -14,33 +14,11 @@
{% endblock %}
{% block scripts %}
{% set workflow_scripts = [
'/static/js/workflow/workflow_utils.js',
'/static/js/workflow/workflow_state.js',
'/static/js/workflow/workflow_mutations.js',
'/static/js/workflow/workflow_plugin_options.js',
'/static/js/workflow/workflow_field_renderer.js',
'/static/js/workflow/workflow_node_template.js',
'/static/js/workflow/workflow_node_events.js',
'/static/js/workflow/workflow_loop_renderer.js',
'/static/js/workflow/workflow_node_renderer.js',
'/static/js/workflow/workflow_canvas_renderer.js',
'/static/js/workflow/workflow_builder.js'
] %}
{% set workflow_scripts = ui_assets.get('workflow_scripts', []) if ui_assets else [] %}
{% for script in workflow_scripts %}
<script src="{{ script }}"></script>
{% endfor %}
{% set page_scripts = [
'/static/js/plugins/navigation_loader.js',
'/static/js/plugins/workflow_builder.js',
'/static/js/plugins/workflow_templates.js',
'/static/js/plugins/run_mode_toggle.js',
'/static/js/plugins/prompt_builder.js',
'/static/js/plugins/translation_editor_base.js',
'/static/js/plugins/translation_editor_render.js',
'/static/js/plugins/translation_editor_actions.js',
'/static/js/plugins/translation_editor_network.js'
] %}
{% set page_scripts = ui_assets.get('page_scripts', []) if ui_assets else [] %}
{% for script in page_scripts %}
<script src="{{ script }}"></script>
{% endfor %}

View File

@@ -0,0 +1,37 @@
{
"core_scripts": [
"/static/js/app_context.js",
"/static/js/plugin_registry.js",
"/static/js/services/toast.js",
"/static/js/plugins/theme_manager.js",
"/static/js/plugins/navigation_manager.js",
"/static/js/plugins/choices_manager.js",
"/static/js/plugins/workflow_toggle.js",
"/static/js/plugins/form_validator.js",
"/static/js/plugins/status_poller.js"
],
"workflow_scripts": [
"/static/js/workflow/workflow_utils.js",
"/static/js/workflow/workflow_state.js",
"/static/js/workflow/workflow_mutations.js",
"/static/js/workflow/workflow_plugin_options.js",
"/static/js/workflow/workflow_field_renderer.js",
"/static/js/workflow/workflow_node_template.js",
"/static/js/workflow/workflow_node_events.js",
"/static/js/workflow/workflow_loop_renderer.js",
"/static/js/workflow/workflow_node_renderer.js",
"/static/js/workflow/workflow_canvas_renderer.js",
"/static/js/workflow/workflow_builder.js"
],
"page_scripts": [
"/static/js/plugins/navigation_loader.js",
"/static/js/plugins/workflow_builder.js",
"/static/js/plugins/workflow_templates.js",
"/static/js/plugins/run_mode_toggle.js",
"/static/js/plugins/prompt_builder.js",
"/static/js/plugins/translation_editor_base.js",
"/static/js/plugins/translation_editor_render.js",
"/static/js/plugins/translation_editor_actions.js",
"/static/js/plugins/translation_editor_network.js"
]
}