task_manager
Fire-and-forget task manager for tool execution.
Wraps tool handler coroutines with a configurable timeout. If a tool
completes within the timeout its result is returned inline. Otherwise
the coroutine continues as a background asyncio.Task and a
JSON envelope containing a task ID is returned so the LLM can poll for
results later via the check_task tool.
Output redirect
Any backgrounded task can have its result automatically delivered to a
channel on any platform when it finishes. Call
TaskManager.set_output_redirect() (or use the redirect_task
tool) to configure this.
- class task_manager.TaskStatus(*values)[source]
-
TaskStatus (inherits from str, Enum).
- RUNNING = 'running'
- COMPLETED = 'completed'
- FAILED = 'failed'
- class task_manager.TaskRecord(task_id, tool_name, status, created_at=<factory>, result=None, error=None, user_id='', channel_id='', platform='', asyncio_task=None, redirect_channel_id='', redirect_platform='', redirect_adapter=None, redirect_max_chars=0)[source]
Bases:
objectIn-memory record for a tracked background task.
- Parameters:
- status: TaskStatus
- class task_manager.TaskManager(timeout=10.0, redis=None)[source]
Bases:
objectManage fire-and-forget tool execution with timeout.
- Parameters:
- async execute(coro, tool_name='', user_id='', channel_id='', platform='')[source]
Run coro with a timeout; background it if it takes too long.
Returns the tool result string directly when the coroutine finishes within
timeout, or a JSON envelope with atask_idwhen it does not.
- async get_result(task_id, user_id=None)[source]
Return the result for task_id, or a status update.
If user_id is set, only tasks owned by that user are returned.
- async await_result(task_id, timeout=300.0)[source]
Block until task_id completes and return its result.
Unlike
get_result()which returns immediately with a status update, this method awaits the underlyingasyncio.Taskso the caller’s coroutine is suspended until the work finishes.
- async list_tasks(user_id=None)[source]
Return a JSON summary of tracked tasks.
If user_id is provided, only tasks belonging to that user are returned. Pass
Noneto list all tasks.