Skip to content
WebJs
Menu

Displays the steps the agent takes, as it takes them.

Add Open Graph metadata to the home page.
Found the project files
Searched for generateMetadata Read app/layout.tsx Read app/page.tsx
Updated the page metadata
Edited app/layout.tsx Edited app/page.tsx

Added Open Graph defaults to app/layout.tsx and a title, description and image for the home page in app/page.tsx.

import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import { bubbleClass, bubbleContentClass } from '#components/ui/bubble.ts';
import { buttonClass } from '#components/ui/button.ts';
import { messageClass, messageContentClass } from '#components/ui/message.ts';
import {
  traceClass, traceLineClass, traceIconClass, traceTitleClass,
  traceCollapsibleClass, traceCollapsibleTriggerClass, traceCollapsibleContentClass,
} from '#components/ui/trace.ts';
import { ChevronRightIcon, CircleCheckIcon } from '#lib/ui/lucide.ts';

const question = 'Add Open Graph metadata to the home page.';
const answer =
  'Added Open Graph defaults to app/layout.tsx and a title, description and image for the home page in app/page.tsx.';

const tasks = [
  {
    title: 'Found the project files',
    steps: ['Searched for generateMetadata', 'Read app/layout.tsx', 'Read app/page.tsx'],
  },
  {
    title: 'Updated the page metadata',
    steps: ['Edited app/layout.tsx', 'Edited app/page.tsx'],
  },
];

// A finished task: past tense, closed, its steps a trace under the line.
function TaskRow(task: { title: string; steps: string[] }) {
  return html`
    <details data-slot="trace-collapsible" class=${traceCollapsibleClass()}>
      <summary data-slot="trace-collapsible-trigger" class=${traceCollapsibleTriggerClass()}>
        <span data-slot="trace-line" class=${traceLineClass()}>
          <span data-slot="trace-icon" aria-hidden="true" class=${traceIconClass()}>${CircleCheckIcon()}</span>
          <span data-slot="trace-title" class=${traceTitleClass()}>${task.title}</span>
        </span>
        <span data-slot="trace-icon" aria-hidden="true" class=${cn(traceIconClass(), 'transition-transform [[open]>summary>&]:rotate-90')}>${ChevronRightIcon()}</span>
      </summary>
      <div data-slot="trace-collapsible-content" class=${traceCollapsibleContentClass()}>
        <div data-slot="trace" class=${traceClass()}>
          ${task.steps.map((step) => html`
            <span data-slot="trace-line" class=${traceLineClass()}>
              <span data-slot="trace-title" class=${traceTitleClass()}>${step}</span>
            </span>
          `)}
        </div>
      </div>
    </details>
  `;
}

// The conversation once both tasks are done, so both are closed.
export default function TaskDemo() {
  return html`
    <div class="flex min-h-72 w-full max-w-sm flex-col gap-4 py-6 text-sm">
      <div data-slot="message" data-align="end" class=${messageClass()}>
        <div data-slot="message-content" class=${messageContentClass()}>
          <div data-slot="bubble" data-variant="muted" data-align="end" class=${bubbleClass({ variant: 'muted' })}>
            <div data-slot="bubble-content" class=${bubbleContentClass()}>${question}</div>
          </div>
        </div>
      </div>
      <div data-slot="message" data-align="start" class=${messageClass()}>
        <div data-slot="message-content" class=${cn(messageContentClass(), 'gap-4')}>
          <div data-slot="trace" class=${traceClass()}>
            ${tasks.map((task) => TaskRow(task))}
          </div>
          <p>${answer}</p>
        </div>
      </div>
      <button type="button" data-slot="button" data-variant="outline" data-size="sm" class=${cn(buttonClass({ variant: 'outline', size: 'sm' }), 'mt-auto self-start')}>
        Replay
      </button>
    </div>
  `;
}
View Code

Task is a piece of work the agent is doing, such as finding the project files or updating a page. Each step, like a search, a read or an edit, appears under the task when the agent takes it.

Task is built with a trace, with a trace collapsible for each task.

Task or Plan

Both are a list of steps under a collapsible line. The difference is when the steps are known.

Use Plan when the agent decides the steps before it starts. Use Task when the steps come from the work itself, and the agent only knows the next one once it has taken the last.

PlanTask
StepsEvery step, from the startEach step, once the agent takes it
Pending stepsYes, waiting their turnNone
TitleThe goal, with progress: "2 of 4 done"What the agent is doing, then what it did
Per replyOne planOne or more tasks, each closing when it is done

Installation

Task is built with the Trace component.

pnpm dlx @webjsdev/ui add trace

Usage

import {
  traceClass,
  traceCollapsibleClass,
  traceCollapsibleContentClass,
  traceCollapsibleTriggerClass,
  traceIconClass,
  traceLineClass,
  traceTitleClass,
} from '#components/ui/trace.ts';
<div data-slot="trace" class=${traceClass()}>
  <details data-slot="trace-collapsible" open class=${cn(traceCollapsibleClass(), 'group/trace-collapsible')}>
    <summary data-slot="trace-collapsible-trigger" class=${traceCollapsibleTriggerClass()}>
      <span data-slot="trace-line" class=${traceLineClass()}>
        <span data-slot="trace-title" class=${traceTitleClass()}>Found the project files</span>
      </span>
      <span data-slot="trace-icon" aria-hidden="true" class=${cn(traceIconClass(), 'transition-transform group-open/trace-collapsible:rotate-90')}>
        ${ChevronRightIcon()}
      </span>
    </summary>
    <div data-slot="trace-collapsible-content" class=${traceCollapsibleContentClass()}>
      <div data-slot="trace" class=${traceClass()}>
        <span data-slot="trace-line" class=${traceLineClass()}>
          <span data-slot="trace-icon" aria-hidden="true" class=${traceIconClass()}>${SearchIcon()}</span>
          <span data-slot="trace-title" class=${traceTitleClass()}>Searched for app/page.tsx</span>
        </span>
        <a href="/files/app/page.tsx">
          <span data-slot="trace-line" class=${traceLineClass()}>
            <span data-slot="trace-icon" aria-hidden="true" class=${traceIconClass()}>${FileTextIcon()}</span>
            <span data-slot="trace-title" class=${traceTitleClass()}>Read app/page.tsx</span>
          </span>
        </a>
      </div>
    </div>
  </details>
</div>

Status

Set the task's status from its steps. Write the title in the present tense while the task runs, and in the past tense once it is done.

Stepsdata-statusTitle
A step is still running"running"Updating the page metadata
A step failed"error"Failed to update the page metadata
Every step is donenoneUpdated the page metadata

Put several tasks in one trace. Keep the running task open with ?open=${running}, and it renders closed once it is done.

AI SDK

Stream each task as a data part with its own id, writing the task again with each new step. Where your component collects the parts, keep the latest part for each id, so a task updates in place instead of repeating. See AI SDK for streaming the parts from a 'use server' action.

modules/chat/actions/chat.server.ts
yield { type: 'data-task', id: 'update', data: { title, steps } };
const tasks = message.parts.filter((part) => part.type === 'data-task');

Migrating from AI SDK Elements

If you used Task from AI SDK Elements, its wrapper lives in components/ai-elements/task.tsx. In a WebJs app it becomes this pattern, built from WebJs UI components. Each export of the wrapper maps onto a part:

AI SDK ElementsWebJs UI
TaskA trace with a trace collapsible.
TaskTriggerThe collapsible's <summary>, holding a trace line and a trace title with the title.
TaskContentThe trace collapsible content, holding a trace for the steps.
TaskItemA trace line with a trace title.
TaskItemFileNo kit part. Keep it as markup of your own inside the trace title.

API Reference

See Trace.