Skip to content

Commit 4777f4f

Browse files
committed
refac
1 parent 1b1d85f commit 4777f4f

1 file changed

Lines changed: 20 additions & 16 deletions

File tree

backend/open_webui/tools/builtin.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2345,7 +2345,7 @@ class TaskItem(BaseModel):
23452345

23462346

23472347
async def tasks(
2348-
tasks: list[TaskItem],
2348+
tasks: Optional[list[TaskItem]] = None,
23492349
overwrite: bool = True,
23502350
__chat_id__: str = None,
23512351
__message_id__: str = None,
@@ -2356,7 +2356,8 @@ async def tasks(
23562356
"""
23572357
Create or update a checklist of tasks tied to this chat.
23582358
Useful whenever a request involves multiple pieces of work that
2359-
should be tracked individually.
2359+
should be tracked individually. Call without arguments to retrieve
2360+
the existing list.
23602361
23612362
By default the entire list is replaced (overwrite=true). Set
23622363
overwrite=false to patch specific items by id without discarding
@@ -2368,7 +2369,7 @@ async def tasks(
23682369
it completed before moving on, or cancel and replace it if the
23692370
approach changes.
23702371
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).
2372+
:param tasks: Optional list of task items. Each item: id (string), content (string, required for new tasks), status (pending|in_progress|completed|cancelled). Leave empty to fetch without modifying.
23722373
:param overwrite: If true (default), replaces the entire task list. If false, updates/adds tasks by id while keeping existing ones.
23732374
:return: JSON with the full task list and summary counts
23742375
"""
@@ -2400,7 +2401,10 @@ def _resolve_id(d: dict, idx: int) -> str:
24002401
item_id = str(d.get('id', '') or '').strip()
24012402
return item_id if item_id else str(idx + 1)
24022403

2403-
if overwrite:
2404+
if tasks is None:
2405+
# Read-only - return current list
2406+
all_tasks = Chats.get_chat_tasks_by_id(__chat_id__)
2407+
elif overwrite:
24042408
# Full replacement - validate and write
24052409
all_tasks = []
24062410
for idx, task in enumerate(tasks):
@@ -2464,19 +2468,19 @@ def _resolve_id(d: dict, idx: int) -> str:
24642468
if not any(t['id'] == item_id for t in existing_tasks):
24652469
all_tasks.append(existing_by_id[item_id])
24662470

2467-
# Persist to DB
2468-
Chats.update_chat_tasks_by_id(__chat_id__, all_tasks)
2471+
# Persist to DB and emit (skip for read-only)
2472+
if tasks is not None:
2473+
Chats.update_chat_tasks_by_id(__chat_id__, all_tasks)
24692474

2470-
# Emit to frontend for real-time UI update
2471-
if __event_emitter__:
2472-
await __event_emitter__(
2473-
{
2474-
'type': 'chat:message:tasks',
2475-
'data': {
2476-
'tasks': all_tasks,
2477-
},
2478-
}
2479-
)
2475+
if __event_emitter__:
2476+
await __event_emitter__(
2477+
{
2478+
'type': 'chat:message:tasks',
2479+
'data': {
2480+
'tasks': all_tasks,
2481+
},
2482+
}
2483+
)
24802484

24812485
# Build summary counts
24822486
pending = sum(1 for t in all_tasks if t['status'] == 'pending')

0 commit comments

Comments
 (0)