Agent UX设计:如何设计AI产品的交互体验
AI · 前端

Agent UX设计:如何设计AI产品的交互体验

模块 14:Agent UX 设计

让用户愿意用、喜欢用 —— Agent 产品的交互设计原则

1. Agent UX 的独特挑战

传统软件 UX:
  用户操作 → 系统响应 → 确定性结果

Agent UX:
  用户表达意图 → Agent 思考 → 不确定性结果
  │                              │
  │  可能对,也可能错              │
  │  可能快,也可能慢              │
  │  可能一步完成,也可能多步       │
  └──────────────────────────────┘

核心挑战:

  • 输出不确定(需要展示推理过程)
  • 响应时间长(需要进度反馈)
  • 可能出错(需要纠错机制)
  • 用户信任(需要透明度)

2. 思考过程展示(Show Thinking)

2.1 为什么要展示

不展示思考过程:
  用户问 → 等待 30 秒 → 突然出现答案
  用户心理:"它在干嘛?卡死了?这个答案靠谱吗?"

展示思考过程:
  用户问 → "正在分析问题..." → "搜索相关信息..." → "整理答案..."
  用户心理:"它在一步步处理,过程看起来合理。"

2.2 设计模式

// 思考过程展示组件
interface ThinkingStep {
  status: 'pending' | 'in_progress' | 'completed' | 'error';
  label: string;
  detail?: string;
  timestamp: number;
}

function ThinkingProcess({ steps }: { steps: ThinkingStep[] }) {
  return (
    <div className="thinking-process">
      {steps.map((step, i) => (
        <div key={i} className={`step ${step.status}`}>
          <span className="icon">
            {step.status === 'completed' ? '✓' :
             step.status === 'in_progress' ? '⟳' :
             step.status === 'error' ? '✗' : '○'}
          </span>
          <span className="label">{step.label}</span>
          {step.detail && <span className="detail">{step.detail}</span>}
        </div>
      ))}
    </div>
  );
}

2.3 展示层级

Level 1:简单状态(适合简单场景)
  "正在思考..."

Level 2:步骤展示(适合多步骤)
  ✓ 理解问题
  ⟳ 搜索信息
  ○ 生成回答

Level 3:详细日志(适合开发者/高级用户)
  ✓ 理解问题 → 识别为"天气查询"意图
  ⟳ 搜索信息 → 调用 get_weather("北京")
  ○ 生成回答 → 等待工具返回

3. 流式输出

3.1 为什么流式

非流式:
  等待 5 秒 → 一次性显示 200 字
  用户体验:焦虑等待

流式:
  逐字/逐句显示
  用户体验:实时反馈,感觉更快

3.2 实现

// 流式输出组件
function StreamingMessage({ stream }: { stream: ReadableStream }) {
  const [text, setText] = useState('');

  useEffect(() => {
    const reader = stream.getReader();
    const decoder = new TextDecoder();

    async function read() {
      while (true) {
        const { done, value } = await reader.read();
        if (done) break;
        setText(prev => prev + decoder.decode(value));
      }
    }
    read();
  }, [stream]);

  return <div className="message">{text}</div>;
}

3.3 工具调用状态

// 工具调用过程展示
function ToolCallStatus({ toolCalls }: { toolCalls: ToolCall[] }) {
  return (
    <div className="tool-calls">
      {toolCalls.map((call, i) => (
        <div key={i} className="tool-call">
          <span className="tool-icon">🔧</span>
          <span className="tool-name">{call.name}</span>
          <span className={`status ${call.status}`}>
            {call.status === 'running' ? '执行中...' :
             call.status === 'done' ? '完成' : '失败'}
          </span>
        </div>
      ))}
    </div>
  );
}

4. 错误处理 UX

4.1 错误类型与处理

// 错误状态设计
interface ErrorState {
  type: 'timeout' | 'tool_error' | 'llm_error' | 'rate_limit' | 'unknown';
  message: string;
  recoverable: boolean;
  retryAction?: () => void;
}

function ErrorMessage({ error }: { error: ErrorState }) {
  return (
    <div className="error-message">
      <div className="error-icon">⚠️</div>
      <div className="error-text">
        {error.type === 'timeout' && '响应超时,请稍后重试'}
        {error.type === 'tool_error' && '工具调用失败,正在尝试其他方法'}
        {error.type === 'rate_limit' && '请求过于频繁,请稍后重试'}
        {error.type === 'llm_error' && 'AI 服务暂时不可用'}
        {error.type === 'unknown' && '遇到了未知错误'}
      </div>
      {error.recoverable && (
        <button onClick={error.retryAction}>重试</button>
      )}
    </div>
  );
}

4.2 优雅降级

完整功能 → 部分失败 → 降级策略

示例:
  Agent 调用搜索工具失败
  → 降级:"搜索服务暂时不可用,我将基于已有知识回答(可能不是最新信息)"
  → 用缓存/知识库回答
  → 标注信息来源和时效性

5. 人机协作模式

5.1 Human-in-the-Loop

// 确认对话框
function ConfirmationDialog({
  action,
  onConfirm,
  onCancel
}: {
  action: { name: string; details: string; risk: 'low' | 'medium' | 'high' };
  onConfirm: () => void;
  onCancel: () => void;
}) {
  return (
    <div className="confirmation-dialog">
      <div className="warning-icon">
        {action.risk === 'high' ? '🔴' : action.risk === 'medium' ? '🟡' : '🔵'}
      </div>
      <h3>确认操作</h3>
      <p>Agent 请求执行以下操作:</p>
      <div className="action-details">{action.details}</div>
      <div className="buttons">
        <button onClick={onCancel} className="cancel">取消</button>
        <button onClick={onConfirm} className="confirm">确认执行</button>
      </div>
    </div>
  );
}

5.2 介入时机

需要确认的操作:
  ✓ 发送邮件/消息
  ✓ 创建/删除资源
  ✓ 花费金钱
  ✓ 不可逆操作
  ✓ 超出权限的操作

不需要确认的操作:
  ✗ 搜索查询
  ✗ 读取数据
  ✗ 计算分析
  ✗ 草稿生成

6. 对话式 UI 设计

6.1 消息类型

type MessageType =
  | { type: 'text'; content: string }
  | { type: 'code'; language: string; code: string }
  | { type: 'image'; url: string; alt: string }
  | { type: 'table'; headers: string[]; rows: string[][] }
  | { type: 'chart'; config: ChartConfig }
  | { type: 'file'; name: string; url: string }
  | { type: 'thinking'; steps: ThinkingStep[] }
  | { type: 'tool_call'; tool: string; status: string }
  | { type: 'error'; error: ErrorState };

6.2 输入设计

// 智能输入框
function SmartInput({
  onSubmit,
  suggestions
}: {
  onSubmit: (text: string) => void;
  suggestions: string[];
}) {
  return (
    <div className="smart-input">
      {/* 快捷建议 */}
      <div className="suggestions">
        {suggestions.map((s, i) => (
          <button key={i} onClick={() => onSubmit(s)}>{s}</button>
        ))}
      </div>
      {/* 输入框 */}
      <textarea
        placeholder="输入消息... (Shift+Enter 换行)"
        onKeyDown={(e) => {
          if (e.key === 'Enter' && !e.shiftKey) {
            onSubmit(e.currentTarget.value);
            e.currentTarget.value = '';
          }
        }}
      />
      {/* 附件按钮 */}
      <div className="actions">
        <button className="attach">📎</button>
        <button className="send">发送</button>
      </div>
    </div>
  );
}

7. 响应时间优化 UX

策略 1:即时反馈
  用户发送 → 立即显示"已收到" → 逐步展示处理过程

策略 2:渐进式加载
  先显示简短回答 → 逐步补充细节

策略 3:预测性加载
  用户输入时就开始预处理 → 减少感知延迟

策略 4:骨架屏
  显示内容占位符 → 替换为实际内容

8. 信任建设

1. 透明度
   - 展示信息来源
   - 标注置信度
   - 说明推理过程

2. 可控性
   - 用户可以中断
   - 用户可以纠正
   - 用户可以回退

3. 一致性
   - 相同输入相同输出(低 Temperature)
   - 遵循用户偏好
   - 行为可预测

4. 安全感
   - 敏感操作确认
   - 隐私保护说明
   - 数据使用透明

9. 延伸阅读


小码提示: Agent UX 的核心是「信任」。用户看不到 LLM 的推理过程,所以你需要用 UI 把它"翻译"出来。流式输出 + 思考过程展示 + 错误优雅降级 = 好的 Agent 体验。