mirror of https://github.com/kortix-ai/suna.git
fix: task list tool
This commit is contained in:
parent
a9cd2fbca6
commit
d6bc346bbc
|
@ -21,27 +21,20 @@ class Task(BaseModel):
|
||||||
updated_at: str = Field(default_factory=lambda: datetime.now(timezone.utc).isoformat())
|
updated_at: str = Field(default_factory=lambda: datetime.now(timezone.utc).isoformat())
|
||||||
completed_at: Optional[str] = None
|
completed_at: Optional[str] = None
|
||||||
|
|
||||||
def mark_completed(self):
|
def update(self, content: Optional[str] = None, status: Optional[TaskStatus] = None):
|
||||||
"""Mark task as completed"""
|
"""Update task content and/or status"""
|
||||||
self.status = TaskStatus.COMPLETED
|
if content is not None:
|
||||||
self.completed_at = datetime.now(timezone.utc).isoformat()
|
self.content = content
|
||||||
self.updated_at = datetime.now(timezone.utc).isoformat()
|
|
||||||
|
|
||||||
def mark_pending(self):
|
if status is not None:
|
||||||
"""Mark task as pending"""
|
self.status = status
|
||||||
self.status = TaskStatus.PENDING
|
if status == TaskStatus.COMPLETED:
|
||||||
self.completed_at = None
|
self.completed_at = datetime.now(timezone.utc).isoformat()
|
||||||
self.updated_at = datetime.now(timezone.utc).isoformat()
|
elif status == TaskStatus.PENDING:
|
||||||
|
self.completed_at = None
|
||||||
|
|
||||||
def update_content(self, content: str):
|
|
||||||
"""Update task content"""
|
|
||||||
self.content = content
|
|
||||||
self.updated_at = datetime.now(timezone.utc).isoformat()
|
self.updated_at = datetime.now(timezone.utc).isoformat()
|
||||||
|
|
||||||
class TaskCreateRequest(BaseModel):
|
|
||||||
content: str
|
|
||||||
status: TaskStatus = TaskStatus.PENDING
|
|
||||||
|
|
||||||
class TaskUpdateRequest(BaseModel):
|
class TaskUpdateRequest(BaseModel):
|
||||||
id: str
|
id: str
|
||||||
content: Optional[str] = None
|
content: Optional[str] = None
|
||||||
|
@ -51,7 +44,7 @@ class TaskListTool(SandboxToolsBase):
|
||||||
"""Tool for managing tasks stored in a single task_list message.
|
"""Tool for managing tasks stored in a single task_list message.
|
||||||
|
|
||||||
Provides simple CRUD operations with batch support for efficient task management.
|
Provides simple CRUD operations with batch support for efficient task management.
|
||||||
Tasks persist in a single message with type "task_list" following the KISS principle.
|
Tasks persist in a single message with type "task_list"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, project_id: str, thread_manager, thread_id: str):
|
def __init__(self, project_id: str, thread_manager, thread_id: str):
|
||||||
|
@ -179,22 +172,12 @@ class TaskListTool(SandboxToolsBase):
|
||||||
}, indent=2)
|
}, indent=2)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Format for display
|
|
||||||
formatted_tasks = []
|
|
||||||
for task in tasks:
|
|
||||||
formatted_tasks.append({
|
|
||||||
"id": task.id,
|
|
||||||
"content": task.content,
|
|
||||||
"status": task.status.value,
|
|
||||||
"created_at": task.created_at,
|
|
||||||
"completed_at": task.completed_at
|
|
||||||
})
|
|
||||||
|
|
||||||
return ToolResult(
|
return ToolResult(
|
||||||
success=True,
|
success=True,
|
||||||
output=json.dumps({
|
output=json.dumps({
|
||||||
"tasks": formatted_tasks,
|
"tasks": [task.model_dump() for task in tasks],
|
||||||
"total": len(formatted_tasks),
|
"total": len(tasks),
|
||||||
"filter": status_filter
|
"filter": status_filter
|
||||||
}, indent=2)
|
}, indent=2)
|
||||||
)
|
)
|
||||||
|
@ -263,17 +246,12 @@ class TaskListTool(SandboxToolsBase):
|
||||||
# Validate input and create task objects
|
# Validate input and create task objects
|
||||||
created_tasks = []
|
created_tasks = []
|
||||||
for task_data in tasks:
|
for task_data in tasks:
|
||||||
task_request = TaskCreateRequest(**task_data)
|
|
||||||
new_task = Task(
|
new_task = Task(
|
||||||
content=task_request.content,
|
content=task_data["content"],
|
||||||
status=task_request.status
|
status=TaskStatus(task_data.get("status", "pending"))
|
||||||
)
|
)
|
||||||
existing_tasks.append(new_task)
|
existing_tasks.append(new_task)
|
||||||
created_tasks.append({
|
created_tasks.append(new_task.model_dump())
|
||||||
"id": new_task.id,
|
|
||||||
"content": new_task.content,
|
|
||||||
"status": new_task.status.value
|
|
||||||
})
|
|
||||||
|
|
||||||
await self._save_tasks(existing_tasks)
|
await self._save_tasks(existing_tasks)
|
||||||
|
|
||||||
|
@ -360,16 +338,10 @@ class TaskListTool(SandboxToolsBase):
|
||||||
task = task_map[update_request.id]
|
task = task_map[update_request.id]
|
||||||
|
|
||||||
if update_request.content is not None:
|
if update_request.content is not None:
|
||||||
task.update_content(update_request.content)
|
task.update(content=update_request.content)
|
||||||
|
|
||||||
if update_request.status is not None:
|
if update_request.status is not None:
|
||||||
if update_request.status == TaskStatus.COMPLETED:
|
task.update(status=update_request.status)
|
||||||
task.mark_completed()
|
|
||||||
elif update_request.status == TaskStatus.PENDING:
|
|
||||||
task.mark_pending()
|
|
||||||
else:
|
|
||||||
task.status = update_request.status
|
|
||||||
task.updated_at = datetime.now(timezone.utc).isoformat()
|
|
||||||
|
|
||||||
updated_count += 1
|
updated_count += 1
|
||||||
|
|
||||||
|
@ -490,7 +462,7 @@ class TaskListTool(SandboxToolsBase):
|
||||||
success=True,
|
success=True,
|
||||||
output=json.dumps({
|
output=json.dumps({
|
||||||
"message": "All tasks have been cleared",
|
"message": "All tasks have been cleared",
|
||||||
"tasks_remaining": 0
|
"tasks": []
|
||||||
}, indent=2)
|
}, indent=2)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue