|
| 1 | +# Show progress for long-running tools |
| 2 | + |
| 3 | +Long-running tools can feel stalled without feedback. Use progress updates for lightweight status pings and workflows for structured, persisted task checklists. |
| 4 | + |
| 5 | +| | Progress updates (`ProgressUpdateEvent`) | Workflow items (`Workflow`, `WorkflowTask*`) | |
| 6 | +|---------------------|------------------------------------------|----------------------------------------------| |
| 7 | +| Purpose | Quick, ephemeral status text | Structured list of tasks with statuses | |
| 8 | +| Persistence | Not saved to the thread | Persisted as thread items | |
| 9 | +| UI | Inline, transient shimmer text | Collapsible checklist widget | |
| 10 | +| When new content streams | Automatically cleared and replaced by streamed content | Remains visible above the streamed content | |
| 11 | +| Best for | Reporting current phase ("Indexing…") | Multi-step plans users may revisit later | |
| 12 | +| How to emit | `ctx.context.stream(ProgressUpdateEvent(...))` | `start_workflow`, `add_workflow_task`, `update_workflow_task`, `end_workflow` | |
| 13 | + |
| 14 | +## Progress updates |
| 15 | + |
| 16 | +Emit `ProgressUpdateEvent` when you need lightweight, real-time status. They stream immediately to the client and disappear after the turn—they are not stored in the thread. |
| 17 | + |
| 18 | +### From tools |
| 19 | + |
| 20 | +Inside a tool, use `AgentContext.stream` to enqueue progress events. They are delivered to the client immediately and are not persisted as thread items. |
| 21 | + |
| 22 | +```python |
| 23 | +from agents import RunContextWrapper, function_tool |
| 24 | +from chatkit.agents import AgentContext |
| 25 | +from chatkit.types import ProgressUpdateEvent |
| 26 | + |
| 27 | +@function_tool() |
| 28 | +async def ingest_files(ctx: RunContextWrapper[AgentContext], paths: list[str]): |
| 29 | + await ctx.context.stream(ProgressUpdateEvent(icon="upload", text="Uploading...")) |
| 30 | + await upload(paths) |
| 31 | + |
| 32 | + await ctx.context.stream( |
| 33 | + ProgressUpdateEvent(icon="search", text="Indexing and chunking...") |
| 34 | + ) |
| 35 | + await index_files(paths) |
| 36 | + |
| 37 | + await ctx.context.stream(ProgressUpdateEvent(icon="check", text="Done")) |
| 38 | +``` |
| 39 | + |
| 40 | +`stream_agent_response` will forward these events for you alongside any assistant text or tool call updates. |
| 41 | + |
| 42 | +### From custom pipelines |
| 43 | + |
| 44 | +If you are not using the Agents SDK, yield `ProgressUpdateEvent` directly from the `respond` or `action` methods while your backend works: |
| 45 | + |
| 46 | +```python |
| 47 | +async def respond(...): |
| 48 | + yield ProgressUpdateEvent(icon="search", text="Searching tickets...") |
| 49 | + results = await search_tickets() |
| 50 | + |
| 51 | + yield ProgressUpdateEvent(icon="code", text="Generating summary...") |
| 52 | + yield from await stream_summary(results) |
| 53 | +``` |
| 54 | + |
| 55 | +Use short, action-oriented messages and throttle updates to meaningful stages instead of every percent to avoid noisy streams. |
| 56 | + |
| 57 | +## Workflow items |
| 58 | + |
| 59 | +Use workflows when you want a persisted, user-visible checklist of tasks. They render as a widget in the transcript and survive after the turn. Combine with progress updates if you need both a checklist and lightweight status text. |
| 60 | + |
| 61 | +Workflows support multiple task variants (custom, search, thought, file, image); see [`Task`](../../../api/chatkit/types/#chatkit.types.Task). Summaries shown when closing a workflow use [`WorkflowSummary`](../../../api/chatkit/types/#chatkit.types.WorkflowSummary) (for example, `CustomSummary` in the snippet below). |
| 62 | + |
| 63 | +Example streaming workflow updates using `AgentContext` helpers: |
| 64 | + |
| 65 | +```python |
| 66 | +from agents import RunContextWrapper, function_tool |
| 67 | +from chatkit.agents import AgentContext |
| 68 | +from chatkit.types import CustomSummary, CustomTask, Workflow |
| 69 | + |
| 70 | +@function_tool() |
| 71 | +async def long_running_tool_with_steps(ctx: RunContextWrapper[AgentContext]): |
| 72 | + # Create an empty workflow container |
| 73 | + await ctx.context.start_workflow(Workflow(type="custom", tasks=[])) |
| 74 | + |
| 75 | + # Add and update the first task |
| 76 | + discovery = CustomTask(title="Search data sources", status_indicator="loading") |
| 77 | + await ctx.context.add_workflow_task(discovery) |
| 78 | + |
| 79 | + # Run the first task |
| 80 | + await search_my_data_sources() |
| 81 | + |
| 82 | + await ctx.context.update_workflow_task( |
| 83 | + discovery.model_copy(update={"status_indicator": "complete"}), task_index=0 |
| 84 | + ) |
| 85 | + |
| 86 | + # Add a follow-up task |
| 87 | + summary = CustomTask(title="Summarize findings", status_indicator="loading") |
| 88 | + await ctx.context.add_workflow_task(summary) |
| 89 | + |
| 90 | + # Run the second task |
| 91 | + await summarize_my_findings() |
| 92 | + |
| 93 | + await ctx.context.update_workflow_task( |
| 94 | + summary.model_copy(update={"status_indicator": "complete"}), task_index=1 |
| 95 | + ) |
| 96 | + |
| 97 | + # Close the workflow and collapse it in the UI |
| 98 | + await ctx.context.end_workflow( |
| 99 | + summary=CustomSummary(title="Analysis complete"), |
| 100 | + expanded=False, |
| 101 | + ) |
| 102 | +``` |
| 103 | + |
| 104 | +Workflows are saved as thread items by `stream_agent_response` when you yield the associated events; they show up for all participants and remain visible in history. |
0 commit comments