前端开发
在 React/Next.js 中把结构化 agent…
适用于已有 ui.inference.sh 的 widgets registry,并需要从 JSON 声明式渲染富交互 UI 的 React/Next.js 项目。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 38/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 60/100 ×0.15
适用场景
在 React/Next.js 中把结构特定
解决思路
- 运行 `npx skills add belt-sh/cli` 安装 belt CLI 技能。
- 运行 `npx shadcn@latest add https://ui.inference.sh/r/widgets.json` 将 Widget 组件添加到项目。
- 从 `@/registry/blocks/widgets/widget-renderer` 导入 `WidgetRenderer`。
- 从 `@/registry/blocks/widgets/types` 导入 `Widget` 和 `WidgetAction` 类型。
- 构造 `Widget` 对象:设置 `type: 'ui'`,可选设置 `title`,并在 `children` 中放置子组件数组。
- 用 `<WidgetRenderer widget={widget} onAction={(action, formData) => console.log(action, formData)} />` 渲染 widget,并传入 `onAction` 回调处理动作与表单数据。
- 布局:使用 `type: 'row'`,配置 `gap` 和 `children` 渲染行布局。
- 布局:使用 `type: 'col'`,配置 `gap` 和 `children` 渲染列布局。
- 布局:使用 `type: 'box'`,配置 `background`、`padding`、`radius`、`children` 渲染带背景、内边距和圆角的盒子。
- 排版:使用 `type: 'title'`,配置 `value`、`size`、`weight` 渲染标题。
- 排版:使用 `type: 'text'`,配置 `value`、`variant` 渲染正文。
- 排版:使用 `type: 'caption'`,配置 `value` 渲染小字说明。
- 排版:使用 `type: 'label'`,配置 `value`、`fieldName` 渲染字段标签。
- 交互:使用 `type: 'button'`,配置 `label`、`variant`、`onClickAction`,例如 `{ type: 'submit' }`,渲染按钮并触发动作。
- 交互:使用 `type: 'input'`,配置 `name`、`placeholder` 渲染输入框。
- 交互:使用 `type: 'textarea'`,配置 `name`、`placeholder` 渲染多行文本输入框。
- 交互:使用 `type: 'select'`,配置 `name`、`options`,每个选项包含 `value` 和 `label`,渲染下拉选择。
- 交互:使用 `type: 'checkbox'`,配置 `name`、`label`、`defaultChecked` 渲染复选框。
- 展示:使用 `type: 'badge'`,配置 `label`、`variant` 渲染徽章。
- 展示:使用 `type: 'icon'`,配置 `iconName`、`size` 渲染图标。
- 展示:使用 `type: 'image'`,配置 `src`、`alt`、`width`、`height` 渲染图片。
- 展示:使用 `type: 'divider'` 渲染分隔线。
- 航班卡片示例:外层用 `box`,设置 `background: 'gradient-ocean'`、`padding: 4`、`radius: 'lg'`;内部用 `row` 并设置 `justify: 'between'`;左 `col` 放 `caption`(值为“出发地”)和 `title`(值为 `SFO`,`size: '2xl'`、`weight: 'bold'`);中间放 `icon`(`iconName: 'plane'`、`size: 'lg'`);右 `col` 设置 `align: 'end'`,放 `caption`(值为“到达地”)和 `title`(值为 `JFK`,`size: '2xl'`、`weight: 'bold'`)。
- 表单示例:设置 `type: 'ui'`、`title: 'Contact Form'`、`asForm: true`;在 `col`(`gap: 3`)中依次放置 `input`(`name: 'name'`、`placeholder: 'Your name'`)、`input`(`name: 'email'`、`placeholder: 'Email address'`)、`textarea`(`name: 'message'`、`placeholder: 'Message'`)、`button`(`label: 'Send'`、`variant: 'default'`、`onClickAction: { type: 'submit' }`)。
- 使用渐变背景 `gradient-ocean`:蓝青渐变。
- 使用渐变背景 `gradient-sunset`:橙粉渐变。
- 使用渐变背景 `gradient-purple`:紫色渐变。
- 使用渐变背景 `gradient-cool`:冷蓝渐变。
- 使用渐变背景 `gradient-midnight`:深蓝渐变。
- 定义 `handleAction`:`(action: WidgetAction, formData?: WidgetFormData) => { ... }`,并用 `switch (action.type)` 处理动作。
- 当 `action.type` 为 `submit` 时,读取并处理 `formData`。
- 当 `action.type` 为 `click` 时,处理按钮点击。
- 运行 `npx skills add inference-sh/skills@agent-ui` 添加完整 agent 组件相关技能。
- 运行 `npx skills add inference-sh/skills@chat-ui` 添加聊天 UI blocks 相关技能。
- 运行 `npx skills add inference-sh/skills@tools-ui` 添加工具 UI 相关技能。
- 查阅 `https://inference.sh/docs/agents/widgets/overview` 了解 Widgets Overview。
- 查阅 `https://inference.sh/docs/agents/widgets/schema` 了解 Widget Schema 与 Widget JSON 结构。
- 查阅 `https://inference.sh/blog/ux/generative-ui` 了解 Agents That Generate UI 与生成式 UI。
- 查阅组件文档 `https://ui.inference.sh/blocks/widgets`。
适用边界 · 注意事项
- 没有使用 ui.inference.sh 的 registry/云服务,或无法运行 `npx shadcn@latest add https://ui.inference.sh/r/widgets.json` 时不适用。
- 不是 React/Next.js 项目,或不愿引入 shadcn 组件与 `@/registry/blocks/widgets/*` 依赖时不适用。
- 仅需静态页面且不需要 JSON 驱动、表单交互或动作回传时,直接写 JSX 更简单。