mcpField
T
tool-ui

tool-ui

github.com/assistant-ui开发779762 次安装8 周前更新
代码仓库

关于 tool-ui

Tool UI 是一项帮助项目集成工具交互界面组件的技能,涵盖组件安装及接入 assistant-ui Toolkit,并说明前端与后端工具的实现方式。

主要功能

  1. 01包含 25 种界面组件的目录
  2. 02AI 辅助或 shadcn 安装
  3. 03assistant-ui Toolkit 配置
  4. 04前端与后端工具模式

适用场景

  1. 01为工具工作流添加进度和计划视图
  2. 02展示引用、代码或数据可视化
  3. 03收集用户选择或审批
  4. 04在助手界面中呈现媒体内容

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/assistant-ui/tool-ui --skill tool-ui

安装后在 Claude Code 中输入/tool-ui确认已加载。

详细介绍

工具 UI

使用此技能,可快速从需求推进到可用的工具 UI 集成。

如果项目没有现有的聊天 UI/运行时,优先使用 assistant-ui。如果应用已有可用的运行时,则 assistant-ui 为可选项。

第 1 步:兼容性与 Doctor

读取用户项目中的 components.json 并验证:

  • components.json 存在。

第 2 步:安装组件

从项目根目录运行安装命令

首选(AI 辅助集成):

npx tool-agent "integrate the <component> component"

使用下方目录中的组件专属提示词(例如:integrate the plan component for step-by-step task workflows with status tracking)。

替代方案(直接使用注册表):

npx shadcn@latest add @tool-ui/<component-id>

多个组件:

npx tool-agent "integrate the plan, progress-tracker, and approval-card components for planning flows"

或通过 shadcn 安装:

npx shadcn@latest add @tool-ui/plan @tool-ui/progress-tracker @tool-ui/approval-card
完整组件目录

全部 25 个 Tool UI 组件及其工具 Agent 提示词和 shadcn 安装命令:

进度

组件描述工具 Agent 提示词shadcn
plan带状态跟踪的分步任务工作流integrate the plan component for step-by-step task workflows with status trackingnpx shadcn@latest add @tool-ui/plan
progress-tracker为多步骤操作提供实时状态反馈integrate the progress tracker component for real-time status feedback on multi-step operationsnpx shadcn@latest add @tool-ui/progress-tracker

输入

组件描述工具 Agent 提示词shadcn
option-list让用户从多个选项中进行选择integrate the option list component to let users select from multiple choicesnpx shadcn@latest add @tool-ui/option-list
parameter-slider数值参数调节控件integrate the parameter slider component for numeric parameter adjustment controlsnpx shadcn@latest add @tool-ui/parameter-slider
preferences-panel用户偏好设置紧凑面板integrate the preferences panel component for compact user settingsnpx shadcn@latest add @tool-ui/preferences-panel
question-flow带分支的多步骤引导式问题integrate the question flow component for multi-step guided questions with branchingnpx shadcn@latest add @tool-ui/question-flow

展示

ComponentDescriptiontool-agent promptshadcn
citation显示带有来源归属信息的引用integrate the citation component to display source references with attributionnpx shadcn@latest add @tool-ui/citation
item-carousel用于浏览集合的水平轮播integrate the item carousel component for horizontal browsing of collectionsnpx shadcn@latest add @tool-ui/item-carousel
link-preview带有 Open Graph 数据的丰富链接预览integrate the link preview component for rich link previews with Open Graph datanpx shadcn@latest add @tool-ui/link-preview
stats-display以可视化网格展示关键指标和 KPIintegrate the stats display component for key metrics and KPIs in a visual gridnpx shadcn@latest add @tool-ui/stats-display
terminal显示命令行输出和日志integrate the terminal component to show command-line output and logsnpx shadcn@latest add @tool-ui/terminal
weather-widget显示天气预报和天气状况integrate the weather widget component for weather display with forecasts and conditionsnpx shadcn@latest add @tool-ui/weather-widget

制品

ComponentDescriptiontool-agent promptshadcn
chart使用交互式图表可视化数据(需要 recharts)integrate the chart component to visualize data with interactive chartsnpx shadcn@latest add @tool-ui/chart
code-block显示带语法高亮的代码片段integrate the code block component for syntax-highlighted code snippetsnpx shadcn@latest add @tool-ui/code-block
code-diff使用带语法高亮的差异比较代码变更(需要 @pierre/diffs)integrate the code diff component to compare code changes with syntax-highlighted diffsnpx shadcn@latest add @tool-ui/code-diff
data-table在可排序表格中展示结构化数据integrate the data table component to present structured data in sortable tablesnpx shadcn@latest add @tool-ui/data-table
message-draft在发送前审核并批准消息integrate the message draft component to review and approve messages before sendingnpx shadcn@latest add @tool-ui/message-draft
instagram-post呈现 Instagram 帖子预览integrate the instagram post component to render Instagram post previewsnpx shadcn@latest add @tool-ui/instagram-post
linkedin-post呈现 LinkedIn 帖子预览integrate the linkedin post component to render LinkedIn post previewsnpx shadcn@latest add @tool-ui/linkedin-post
x-post呈现 X 帖子预览integrate the x post component to render X/Twitter post previewsnpx shadcn@latest add @tool-ui/x-post

确认

ComponentDescriptiontool-agent promptshadcn
approval-card对 Agent 操作进行二元确认integrate the approval card component for binary confirmation of agent actionsnpx shadcn@latest add @tool-ui/approval-card
order-summary显示包含逐项定价的购买信息integrate the order summary component to display purchases with itemized pricingnpx shadcn@latest add @tool-ui/order-summary

媒体

Component描述tool-agent promptshadcn
audio带封面图和元数据的音频播放integrate the audio component for audio playback with artwork and metadatanpx shadcn@latest add @tool-ui/audio
image显示带元数据和署名的图像integrate the image component to display images with metadata and attributionnpx shadcn@latest add @tool-ui/image
image-gallery带全屏灯箱查看器的瀑布流网格integrate the image gallery component with masonry grid and lightbox viewernpx shadcn@latest add @tool-ui/image-gallery
video带控件和海报图的视频播放integrate the video component for video playback with controls and posternpx shadcn@latest add @tool-ui/video

展示(地理地图)

Component描述tool-agent promptshadcn
geo-map显示地理定位实体和车队位置integrate the geo map component to display geolocated entities and fleet positionsnpx shadcn@latest add @tool-ui/geo-map
按使用场景安装示例

tool-agent(推荐):

npx tool-agent "integrate the plan, progress-tracker, and approval-card components for planning flows"
npx tool-agent "integrate citation, link-preview, code-block, and code-diff for research output"
npx tool-agent "integrate data-table, chart, and stats-display for data visualization"
npx tool-agent "integrate image, image-gallery, video, and audio for media display"

shadcn(直接安装):

npx shadcn@latest add @tool-ui/plan @tool-ui/progress-tracker @tool-ui/approval-card
npx shadcn@latest add @tool-ui/citation @tool-ui/link-preview @tool-ui/code-block @tool-ui/code-diff
npx shadcn@latest add @tool-ui/data-table @tool-ui/chart @tool-ui/stats-display
# npm i recharts  # peer for chart
npx shadcn@latest add @tool-ui/image @tool-ui/image-gallery @tool-ui/video @tool-ui/audio
在代码库中设置工具包

安装组件后,通过 Toolkit 将它们接入 assistant-ui。本节介绍完整的设置流程:提供方、运行时、工具包文件和 ID 处理。

1. 提供方和运行时

创建一个助手封装组件,提供运行时、传输层和工具:

"use client";

import { lastAssistantMessageIsCompleteWithToolCalls } from "ai";
import { AssistantRuntimeProvider, Tools, useAui } from "@assistant-ui/react";
import {
  AssistantChatTransport,
  useChatRuntime,
} from "@assistant-ui/react-ai-sdk";
import { Thread } from "@/components/assistant-ui/thread";
import { toolkit } from "@/components/toolkit";

export const Assistant = () => {
  const runtime = useChatRuntime({
    transport: new AssistantChatTransport({ api: "/api/chat" }),
    sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls,
  });
  const aui = useAui({ tools: Tools({ toolkit }) });

  return (
    <AssistantRuntimeProvider runtime={runtime} aui={aui}>
      <div className="h-dvh">
        <Thread />
      </div>
    </AssistantRuntimeProvider>
  );
};

要点:

  • useChatRuntime + AssistantChatTransport:连接到聊天 API。
  • Tools({ toolkit }):将工具定义和渲染器转发给模型。
  • sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls:工具调用后自动继续(可选,但常用于工具密集型流程)。
2. 工具包文件结构

创建一个 toolkit.ts(或 toolkit.tsx),导出一个 Toolkit 对象。每个键都是一个工具名称;每个值都包含 type、description、parameters 和 render。

工具描述 — 始终为每个工具添加 description。描述何时调用该工具以及它承担什么角色,而不是描述可见内容。Tool UI 组件已经会向用户呈现有效负载(选项、计划、图表等);重复描述这些内容会显得多余。优先提供面向模型的指导(例如“提供一个供用户遵循的计划”或“让用户选择一个选项”),而不是复述内容(例如“显示带有标签和描述的选项列表”)。

-前端工具与后端工具

-前端后端
-实现方式在浏览器中运行;用户交互通过 addResult 提交工具实现在服务端;模型返回结果
-execute必需 — 在客户端运行工具 UI 工作流不需要
-parameters必需(模型参数的架构)不使用;如果后端 LLM 通过 aisdk 实现,则改用 inputSchema
-render必需(用于参数、状态、结果和 addResult 的 UI)必需(用于 result 的 UI)

后端工具(模型返回结果;不需要用户输入):

import { type Toolkit } from "@assistant-ui/react";
import { Plan } from "@/components/tool-ui/plan";
import { safeParseSerializablePlan } from "@/components/tool-ui/plan/schema";

export const toolkit: Toolkit = {
  showPlan: {
    type: "backend",
    render: ({ result }) => {
      const parsed = safeParseSerializablePlan(result);
      if (!parsed) return null;
      return <Plan {...parsed} />;
    },
  },
};

前端工具(模型发送参数;用户交互通过 addResult 提交):

import { type Toolkit } from "@assistant-ui/react";
import { OptionList } from "@/components/tool-ui/option-list";
import {
  SerializableOptionListSchema,
  safeParseSerializableOptionList,
} from "@/components/tool-ui/option-list/schema";

const optionListTool: Toolkit[string] = {
  description: "Render selectable options with confirm and clear actions.",
  parameters: SerializableOptionListSchema,
  render: ({ args, toolCallId, result, addResult }) => {
    const parsed = safeParseSerializableOptionList({
      ...args,
      id: args?.id ?? `option-list-${toolCallId}`,
    });
    if (!parsed) return null;

    if (result) {
      return <OptionList {...parsed} choice={result} />;
    }
    return (
      <OptionList
        {...parsed}
        onAction={async (actionId, selection) => {
          if (actionId === "confirm" || actionId === "cancel") {
            await addResult?.(selection);
          }
        }}
      />
    );
  },
};

export const toolkit: Toolkit = {
  option_list: optionListTool,
  approval_card: {
    /* ... */
  },
};
3. API 路由(AI SDK)

当聊天 API 使用 AI SDK(streamText)时,使用 ai 中的 tool() 定义后端工具:

  • 使用 inputSchema
  • 后端工具在服务端使用 execute;结果会流式传输,并通过工具包的 render 函数渲染
import { streamText, tool, convertToModelMessages } from "ai";
import { openai } from "@ai-sdk/openai";
import { z } from "zod";

// With frontend tools: ...frontendTools(clientTools) — clientTools come from the request body via AssistantChatTransport
const result = streamText({
  model: openai("gpt-4o"),
  messages: await convertToModelMessages(messages),
  tools: {
    get_weather: tool({
      description:
        "Get the current weather and forecast for a location. Returns data to display in a weather widget.",
      inputSchema: z.object({
        location: z.string().describe("City name, e.g. 'San Francisco'"),
        units: z
          .enum(["celsius", "fahrenheit"])
          .default("fahrenheit")
          .describe("Temperature unit"),
      }),
      execute: async ({ location, units }) => {
        // Fetch weather data, return shape matching your widget schema
        return { location, units /* ... */ };
      },
    }),
  },
});
4. 以操作为中心与复合组件
模式组件用法
以操作为中心OptionList、ParameterSlider、PreferencesPanel、ApprovalCard直接连接 onAction 或 onConfirm/onCancel;无需 ToolUI 包装器。传入 choice={result} 以设置回执状态。
复合式OrderSummary、DataTable 等使用 ToolUI + ToolUI.Surface + ToolUI.Actions 包装;使用 DecisionActions 或 LocalActions。

以操作为中心的示例(OptionList):

return (
  <OptionList
    {...parsed}
    onAction={async (actionId, selection) => {
      if (actionId === "confirm" || actionId === "cancel") {
        await addResult?.(selection);
      }
    }}
  />
);

复合式示例(搭配 DecisionActions 的 OrderSummary):

return (
  <ToolUI id={parsed.id}>
    <ToolUI.Surface>
      <OrderSummary {...parsed} />
    </ToolUI.Surface>
    <ToolUI.Actions>
      <ToolUI.DecisionActions
        actions={[
          { id: "cancel", label: "Cancel", variant: "outline" },
          { id: "confirm", label: "Purchase" },
        ]}
        onAction={(action) =>
          createDecisionResult({ decisionId: parsed.id, action })
        }
        onCommit={(decision) => addResult?.(decision)}
      />
    </ToolUI.Actions>
  </ToolUI>
);

ApprovalCard 使用内嵌操作;直接连接 onConfirm/onCancel:

return (
  <ApprovalCard
    {...parsed}
    choice={
      result === "approved" || result === "denied" ? result : parsed.choice
    }
    onConfirm={async () => addResult?.("approved")}
    onCancel={async () => addResult?.("denied")}
  />
);

操作模型

工具 UI 使用两个操作界面,它们作为复合兄弟组件渲染在显示组件之外:

  • ToolUI.LocalActions:非关键副作用(导出、复制、打开链接)。处理函数不得调用 addResult(...)。
  • ToolUI.DecisionActions:会产生后果的选择,通过 createDecisionResult(...) 生成 DecisionResult 封装。提交回调会调用 addResult(...)。

包含操作的显示组件的复合包装模式:

<ToolUI id={surfaceId}>
  <ToolUI.Surface>
    <DataTable {...props} />
  </ToolUI.Surface>
  <ToolUI.Actions>
    <ToolUI.LocalActions
      actions={[{ id: "export-csv", label: "Export CSV" }]}
      onAction={(actionId) => {
        /* side effects only */
      }}
    />
  </ToolUI.Actions>
</ToolUI>

有三个组件是以操作为中心的例外 — 它们保留内嵌操作属性,而不是使用兄弟界面。这三个组件共用一个统一接口:

  • actions:由组件渲染的操作按钮。
  • onAction(actionId, state):操作执行后运行,并接收操作后的状态。
  • onBeforeAction(actionId, state):在操作运行前进行评估的守卫函数。
组件传递给处理函数的状态类型
OptionListOptionListSelection
ParameterSliderSliderValue[]
PreferencesPanelPreferencesValue

使用复合模式的组件:CodeBlock、CodeDiff、Terminal、ProgressTracker。

上下文通过 createContext + use() 共享(React 19)。如果在 Root 之外使用子组件,它们会抛出异常。

回执和选择约定

具有结果的组件使用 choice 属性来呈现已确认/已完成状态:

组件choice 类型值/形状
ApprovalCard"approved" | "denied"字符串字面量
OptionListstring | string[]所选选项 ID
OrderSummaryOrderDecision{ action: "confirm", orderId?, confirmedAt? }
ProgressTrackerToolUIReceipt{ outcome, summary, identifiers?, at }(共享类型)

存在 choice 时,组件会以回执模式呈现——只读且不包含操作。

操作规则

  • 安装能够满足需求的最少组件。
  • 每个工具都必须有 description;应针对模型编写,说明何时调用以及承担什么角色,避免重复组件将要呈现的内容。

注意: 前端工具需要 execute 函数 后端工具的实现位于服务端。 后端工具两者都不需要 忽略生成的文件 设置完成后:

  • 确保已安装所需的包依赖,让更改完成后的首次运行体验顺畅无阻
  • 如果未设置成功运行刚实现功能所需的环境变量,请通知用户;这些通常主要是 API 聊天端点所需的变量。

评论

支持 Markdown