From faca8c8b535b5d244d8f89967c43ad3ba15d32b9 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 28 Jul 2024 22:02:23 +0100 Subject: [PATCH] feat: multiple action support --- backend/main.py | 48 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/backend/main.py b/backend/main.py index d1d96b001..360f5f415 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1043,14 +1043,39 @@ async def get_all_models(): model["actions"] = [] for action_id in action_ids: action = Functions.get_function_by_id(action_id) - model["actions"].append( - { - "id": action_id, - "name": action.name, - "description": action.meta.description, - "icon_url": action.meta.manifest.get("icon_url", None), - } - ) + + if action_id in webui_app.state.FUNCTIONS: + function_module = webui_app.state.FUNCTIONS[action_id] + else: + function_module, _, _ = load_function_module_by_id(action_id) + webui_app.state.FUNCTIONS[action_id] = function_module + + if hasattr(function_module, "actions"): + actions = function_module.actions + model["actions"].extend( + [ + { + "id": f"{action_id}.{_action['id']}", + "name": _action.get( + "name", f"{action.name} ({_action['id']})" + ), + "description": action.meta.description, + "icon_url": _action.get( + "icon_url", action.meta.manifest.get("icon_url", None) + ), + } + for _action in actions + ] + ) + else: + model["actions"].append( + { + "id": action_id, + "name": action.name, + "description": action.meta.description, + "icon_url": action.meta.manifest.get("icon_url", None), + } + ) app.state.MODELS = {model["id"]: model for model in models} webui_app.state.MODELS = app.state.MODELS @@ -1288,6 +1313,11 @@ async def chat_completed(form_data: dict, user=Depends(get_verified_user)): async def chat_completed( action_id: str, form_data: dict, user=Depends(get_verified_user) ): + if "." in action_id: + action_id, sub_action_id = action_id.split(".") + else: + sub_action_id = None + action = Functions.get_function_by_id(action_id) if not action: raise HTTPException( @@ -1340,7 +1370,7 @@ async def chat_completed( # Extra parameters to be passed to the function extra_params = { "__model__": model, - "__id__": action_id, + "__id__": sub_action_id if sub_action_id is not None else action_id, "__event_emitter__": __event_emitter__, "__event_call__": __event_call__, }