Skip to content

Commit 1b1d85f

Browse files
committed
refac
1 parent edb8971 commit 1b1d85f

2 files changed

Lines changed: 31 additions & 21 deletions

File tree

backend/open_webui/tools/builtin.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,8 @@ async def execute_code(
393393
if CODE_INTERPRETER_BLOCKED_MODULES:
394394
import textwrap
395395

396-
blocking_code = textwrap.dedent(f"""
396+
blocking_code = textwrap.dedent(
397+
f"""
397398
import builtins
398399
399400
BLOCKED_MODULES = {CODE_INTERPRETER_BLOCKED_MODULES}
@@ -409,7 +410,8 @@ def restricted_import(name, globals=None, locals=None, fromlist=(), level=0):
409410
return _real_import(name, globals, locals, fromlist, level)
410411
411412
builtins.__import__ = restricted_import
412-
""")
413+
"""
414+
)
413415
code = blocking_code + '\n' + code
414416

415417
engine = getattr(__request__.app.state.config, 'CODE_INTERPRETER_ENGINE', 'pyodide')
@@ -2342,7 +2344,7 @@ class TaskItem(BaseModel):
23422344
status: Literal['pending', 'in_progress', 'completed', 'cancelled'] = Field('pending', description="Task status.")
23432345

23442346

2345-
async def update_tasks(
2347+
async def tasks(
23462348
tasks: list[TaskItem],
23472349
overwrite: bool = True,
23482350
__chat_id__: str = None,
@@ -2352,21 +2354,29 @@ async def update_tasks(
23522354
__user__: dict = None,
23532355
) -> str:
23542356
"""
2355-
Create or update tasks for the current chat. By default replaces the
2356-
entire task list. Set overwrite=false to update individual tasks by id
2357-
while preserving the rest.
2357+
Create or update a checklist of tasks tied to this chat.
2358+
Useful whenever a request involves multiple pieces of work that
2359+
should be tracked individually.
2360+
2361+
By default the entire list is replaced (overwrite=true). Set
2362+
overwrite=false to patch specific items by id without discarding
2363+
the rest.
23582364
2359-
Only ONE task should be in_progress at a time. Mark tasks completed
2360-
immediately when done.
2365+
Each item carries an id, content string, and a status field
2366+
(pending, in_progress, completed, or cancelled). Order reflects
2367+
priority. Only one item should be in_progress at a time; mark
2368+
it completed before moving on, or cancel and replace it if the
2369+
approach changes.
23612370
2362-
: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).
2371+
: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).
23632372
:param overwrite: If true (default), replaces the entire task list. If false, updates/adds tasks by id while keeping existing ones.
23642373
:return: JSON with the full task list and summary counts
23652374
"""
23662375
if __chat_id__ is None:
23672376
return json.dumps({'error': 'Chat context not available'})
23682377

23692378
try:
2379+
23702380
def _to_dict(task) -> dict:
23712381
"""Convert TaskItem or dict to plain dict."""
23722382
if hasattr(task, 'model_dump'):
@@ -2391,7 +2401,7 @@ def _resolve_id(d: dict, idx: int) -> str:
23912401
return item_id if item_id else str(idx + 1)
23922402

23932403
if overwrite:
2394-
# Full replacement validate and write
2404+
# Full replacement - validate and write
23952405
all_tasks = []
23962406
for idx, task in enumerate(tasks):
23972407
d = _to_dict(task)
@@ -2404,13 +2414,15 @@ def _resolve_id(d: dict, idx: int) -> str:
24042414
if status not in VALID_TASK_STATUSES:
24052415
status = 'pending'
24062416

2407-
all_tasks.append({
2408-
'id': item_id,
2409-
'content': content,
2410-
'status': status,
2411-
})
2417+
all_tasks.append(
2418+
{
2419+
'id': item_id,
2420+
'content': content,
2421+
'status': status,
2422+
}
2423+
)
24122424
else:
2413-
# Partial update merge by id
2425+
# Partial update - merge by id
24142426
existing_tasks = Chats.get_chat_tasks_by_id(__chat_id__)
24152427
existing_by_id = {t['id']: t for t in existing_tasks}
24162428

@@ -2486,7 +2498,5 @@ def _resolve_id(d: dict, idx: int) -> str:
24862498
ensure_ascii=False,
24872499
)
24882500
except Exception as e:
2489-
log.exception(f'update_tasks error: {e}')
2501+
log.exception(f'tasks error: {e}')
24902502
return json.dumps({'error': str(e)})
2491-
2492-

backend/open_webui/utils/tools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
view_file,
8686
view_knowledge_file,
8787
view_skill,
88-
update_tasks,
88+
tasks,
8989
)
9090

9191
import copy
@@ -506,7 +506,7 @@ def is_builtin_tool_enabled(category: str) -> bool:
506506

507507
# Task management - break down complex work into trackable steps
508508
if is_builtin_tool_enabled('tasks'):
509-
builtin_functions.append(update_tasks)
509+
builtin_functions.append(tasks)
510510

511511
for func in builtin_functions:
512512
callable = get_async_tool_function_and_apply_extra_params(

0 commit comments

Comments
 (0)