前端开发
React/Next.js 中展示 Agent…
在 React/Next.js 应用里需要展示 Agent/MCP/函数调用的工具状态、进度、人工审批与结果时,使用 ui.inference.sh 的 Tool UI 组件。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 38/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 60/100 ×0.15
适用场景
React/Next.js 中展示 Ag特定
解决思路
- 先安装 belt CLI skill:运行 `npx skills add belt-sh/cli`。
- 用 shadcn 添加 Tool UI 组件:运行 `npx shadcn@latest add https://ui.inference.sh/r/tools.json`。
- 用 5 种状态驱动工具 UI:`pending`=工具调用已请求、等待执行;`running`=正在执行;`approval`=执行前需人工批准;`success`=成功完成;`error`=执行失败。
- 展示工具调用:从 `@/registry/blocks/tools/tool-call` 引入 `ToolCall`,传入 `name`、`args`、`status`,例如 `<ToolCall name="search_web" args={{ query: "latest AI news" }} status="running" />`。
- 展示工具结果:从 `@/registry/blocks/tools/tool-result` 引入 `ToolResult`,传入 `name`、`result`、`status`,例如 `<ToolResult name="search_web" result={{ results: [...] }} status="success" />`。
- 实现人工审批:从 `@/registry/blocks/tools/tool-approval` 引入 `ToolApproval`,传入 `name`、`args`,并实现 `onApprove`(执行工具)和 `onDeny`(取消工具),例如 `<ToolApproval name="send_email" args={{ to: "user@example.com", subject: "Hello" }} onApprove={() => executeTool()} onDeny={() => cancelTool()} />`。
- 渲染完整工具生命周期:若 `tool.status === 'approval'`,渲染 `ToolApproval` 并传入 `name`、`args`、`approve`、`deny`;否则若存在 `tool.result`,渲染 `ToolResult` 并传入 `name`、`result`、`status`;否则渲染 `ToolCall` 并传入 `name`、`args`、`status`。
- 自定义工具卡片样式:给 `ToolCall` 等组件传 `className`,例如 `<ToolCall name="read_file" args={{ path: "/src/index.ts" }} status="running" className="border-blue-500" />`。
- 按工具名自动匹配图标:`search*`、`find*` 用 Search;`read*`、`get*` 用 File;`write*`、`create*` 用 Pencil;`delete*`、`remove*` 用 Trash;`send*`、`email*` 用 Mail;默认用 Wrench。
- 与 Agent 组件集成:从 `@/registry/blocks/agent/agent` 引入 `Agent`,传入 `proxyUrl="/api/inference/proxy"` 和 `config`;在 `config` 中设置 `core_app: { ref: 'openrouter/claude-sonnet-45@0fkg6xwb' }`,并在 `tools` 中定义工具(`name`、`description`、`parameters`),需要审批时设置 `requiresApproval: true`;Agent 组件会自动处理工具生命周期。
- 按需安装相关 skills:完整 Agent 组件运行 `npx skills add inference-sh/skills@agent-ui`;聊天 UI blocks 运行 `npx skills add inference-sh/skills@chat-ui`;工具结果 widgets 运行 `npx skills add inference-sh/skills@widgets-ui`。
- 查阅文档:Adding Tools to Agents(https://inference.sh/docs/agents/adding-tools)、Human-in-the-Loop(https://inference.sh/docs/runtime/human-in-the-loop)、Tool Approval Gates(https://inference.sh/blog/tools/approval-gates),以及组件文档 https://ui.inference.sh/blocks/tools。
适用边界 · 注意事项
- 不适用于非 React/Next.js 项目,或不想使用 shadcn registry 的项目。
- 不适用于无法访问、不愿依赖 ui.inference.sh 组件与 registry 的场景。
- 如果只需要通用 UI 而不涉及 Agent/MCP/函数调用的工具生命周期展示,不必使用。