Skip to content

react

实现 ReAct 模式:推理 → 行动 → 观察 → 再推理

适用于需要自我修正的场景,如 LLM 决策错误后根据反馈调整。

基本用法

typescript
import { react } from '@multi-agent/agent-kit'

const result = await react({
  // 1. 推理:根据输入做出决策
  reason: async (input, feedback) => {
    if (feedback) {
      // 根据上次错误调整决策
      return adjustedDecision
    }
    return initialDecision
  },

  // 2. 行动:执行决策
  act: async (decision) => {
    return await execute(decision)
  },

  // 3. 观察:分析结果,判断成功或失败
  observe: (result, decision) => {
    if (result.error) {
      return { success: false, error: result.error }
    }
    return { success: true, data: result.data }
  },

  // 判断是否可重试
  isRetryable: (error) => error.type === 'INVALID_SELECTION',

  // 可选:重试回调
  onRetry: (attempt, error) => console.log(`重试 ${attempt}...`),

  maxRetries: 2,
}, input)

执行流程

ReAct 执行流程

实际场景:Agent 选择

typescript
import { react } from '@multi-agent/agent-kit'

const result = await react({
  // 1. Reasoning: LLM 选择 Agent
  reason: async (requirement, lastError) => {
    if (lastError) {
      return await agentSelector.selectWithFeedback(requirement, lastError)
    }
    return await agentSelector.select(requirement)
  },

  // 2. Acting: 调用选中的 Agent
  act: async (selection) => {
    return await callAgent(selection.primaryAgent)
  },

  // 3. Observing: 分析调用结果
  observe: (result, selection) => {
    if (result.errorMessage) {
      return {
        success: false,
        error: {
          selectedAgent: selection.primaryAgent,
          errorMessage: result.errorMessage,
        }
      }
    }
    return { success: true, data: result }
  },

  isRetryable: (error) => error.type === 'agentError',

  onRetry: (attempt, error) => {
    console.log(`Agent 选择失败,重试 ${attempt}...`)
  },

  maxRetries: 2,
}, userRequirement)

配置选项

typescript
interface ReactConfig<TInput, TDecision, TActResult, TSuccess, TError> {
  reason: (input: TInput, feedback?: TError) => Promise<TDecision>
  act: (decision: TDecision) => Promise<TActResult>
  observe: (result: TActResult, decision: TDecision) => ObserveResult<TSuccess, TError>
  isRetryable: (error: TError) => boolean
  onRetry?: (attempt: number, error: TError, decision: TDecision) => void
  maxRetries?: number  // 默认 2
}

type ObserveResult<TSuccess, TError> =
  | { success: true; data: TSuccess }
  | { success: false; error: TError }

返回值

typescript
interface ReactResult<TSuccess, TDecision> {
  data: TSuccess       // 最终数据
  decision: TDecision  // 最终使用的决策
  attempts: number     // 总尝试次数
}

错误处理

当所有重试都失败时,抛出 ReactError

typescript
import { react, ReactError } from '@multi-agent/agent-kit'

try {
  const result = await react(config, input)
} catch (error) {
  if (error instanceof ReactError) {
    console.log(error.error)        // 最后一个错误
    console.log(error.lastDecision) // 最后的决策
    console.log(error.attempts)     // 尝试次数
  }
}

react vs retry

特性retryreact
适用场景临时故障(网络超时等)需要自我修正的场景(LLM 决策错误等)
操作方式相同操作重复执行根据错误反馈调整决策
反馈机制简单的字符串反馈结构化的错误对象传递给 reason
决策控制完整的 reason → act → observe 循环

选择建议

  • 调用外部 API、网络请求 → 使用 retry
  • LLM 决策、Agent 选择 → 使用 react

MIT Licensed