From 1b1d85fe2e3a5505e371848105bf70c140d77f5e Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sun, 29 Mar 2026 19:42:10 -0500 Subject: [PATCH] refac --- backend/open_webui/tools/builtin.py | 48 +++++++++++++++++------------ backend/open_webui/utils/tools.py | 4 +-- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/backend/open_webui/tools/builtin.py b/backend/open_webui/tools/builtin.py index d9c93eb93..2c79be907 100644 --- a/backend/open_webui/tools/builtin.py +++ b/backend/open_webui/tools/builtin.py @@ -393,7 +393,8 @@ async def execute_code( if CODE_INTERPRETER_BLOCKED_MODULES: import textwrap - blocking_code = textwrap.dedent(f""" + blocking_code = textwrap.dedent( + f""" import builtins BLOCKED_MODULES = {CODE_INTERPRETER_BLOCKED_MODULES} @@ -409,7 +410,8 @@ async def execute_code( return _real_import(name, globals, locals, fromlist, level) builtins.__import__ = restricted_import - """) + """ + ) code = blocking_code + '\n' + code engine = getattr(__request__.app.state.config, 'CODE_INTERPRETER_ENGINE', 'pyodide') @@ -2342,7 +2344,7 @@ class TaskItem(BaseModel): status: Literal['pending', 'in_progress', 'completed', 'cancelled'] = Field('pending', description="Task status.") -async def update_tasks( +async def tasks( tasks: list[TaskItem], overwrite: bool = True, __chat_id__: str = None, @@ -2352,14 +2354,21 @@ async def update_tasks( __user__: dict = None, ) -> str: """ - Create or update tasks for the current chat. By default replaces the - entire task list. Set overwrite=false to update individual tasks by id - while preserving the rest. + Create or update a checklist of tasks tied to this chat. + Useful whenever a request involves multiple pieces of work that + should be tracked individually. - Only ONE task should be in_progress at a time. Mark tasks completed - immediately when done. + By default the entire list is replaced (overwrite=true). Set + overwrite=false to patch specific items by id without discarding + the rest. - :param tasks: List of task items. Each must have: id (string, unique identifier), content (string, task description — required for new tasks), status (one of: pending, in_progress, completed, cancelled). + Each item carries an id, content string, and a status field + (pending, in_progress, completed, or cancelled). Order reflects + priority. Only one item should be in_progress at a time; mark + it completed before moving on, or cancel and replace it if the + approach changes. + + :param tasks: List of task items. Each must have: id (string, unique identifier), content (string, task description, required for new tasks), status (one of: pending, in_progress, completed, cancelled). :param overwrite: If true (default), replaces the entire task list. If false, updates/adds tasks by id while keeping existing ones. :return: JSON with the full task list and summary counts """ @@ -2367,6 +2376,7 @@ async def update_tasks( return json.dumps({'error': 'Chat context not available'}) try: + def _to_dict(task) -> dict: """Convert TaskItem or dict to plain dict.""" if hasattr(task, 'model_dump'): @@ -2391,7 +2401,7 @@ async def update_tasks( return item_id if item_id else str(idx + 1) if overwrite: - # Full replacement — validate and write + # Full replacement - validate and write all_tasks = [] for idx, task in enumerate(tasks): d = _to_dict(task) @@ -2404,13 +2414,15 @@ async def update_tasks( if status not in VALID_TASK_STATUSES: status = 'pending' - all_tasks.append({ - 'id': item_id, - 'content': content, - 'status': status, - }) + all_tasks.append( + { + 'id': item_id, + 'content': content, + 'status': status, + } + ) else: - # Partial update — merge by id + # Partial update - merge by id existing_tasks = Chats.get_chat_tasks_by_id(__chat_id__) existing_by_id = {t['id']: t for t in existing_tasks} @@ -2486,7 +2498,5 @@ async def update_tasks( ensure_ascii=False, ) except Exception as e: - log.exception(f'update_tasks error: {e}') + log.exception(f'tasks error: {e}') return json.dumps({'error': str(e)}) - - diff --git a/backend/open_webui/utils/tools.py b/backend/open_webui/utils/tools.py index 391c45dcb..e266e9d9c 100644 --- a/backend/open_webui/utils/tools.py +++ b/backend/open_webui/utils/tools.py @@ -85,7 +85,7 @@ from open_webui.tools.builtin import ( view_file, view_knowledge_file, view_skill, - update_tasks, + tasks, ) import copy @@ -506,7 +506,7 @@ def get_builtin_tools( # Task management - break down complex work into trackable steps if is_builtin_tool_enabled('tasks'): - builtin_functions.append(update_tasks) + builtin_functions.append(tasks) for func in builtin_functions: callable = get_async_tool_function_and_apply_extra_params(