pi-ai stream idle timeout after 300000ms

2026年10月05日 19:17 9.12 秒 success

错误信息

pi-ai stream idle timeout after 300000ms

AI 解决方案

根据你的报错信息,以下是我的分析:

🌐 错误翻译

原文:pi-ai stream idle timeout after 300000ms
译文:pi-ai 流在300000毫秒后空闲超时

🎯 原因分析


这是API连接的流式响应(stream)在300秒(300000ms)内没有接收到任何数据,导致连接因空闲超时被中断。通常发生在SSE(Server-Sent Events)长连接场景中,原因可能是:
  • 服务端长时间未返回数据或响应过慢

  • 网络不稳定导致数据包丢失

  • 客户端未及时处理或消费流数据
  • 💡 解决方案

    // 方案1:增加超时时间
    const client = new PiAIClient({
      timeout: 600000, // 将超时时间延长到600秒(10分钟)
    });
    
    // 方案2:添加心跳保活机制
    const eventSource = new EventSource(url, {
      // 定期发送心跳,防止空闲超时
    });
    
    // 方案3:使用重试逻辑
    async function fetchWithRetry(url: string, retries = 3) {
      for (let i = 0; i < retries; i++) {
        try {
          const response = await fetch(url);
          return response;
        } catch (error) {
          if (i === retries - 1) throw error;
          await new Promise(r => setTimeout(r, 2000 * (i + 1))); // 指数退避
        }
      }
    }
    

    🛡️ 预防措施

    • 添加心跳机制:在长连接场景中,定期发送心跳包或轻量请求维持连接活性

    • 合理设置超时:根据业务场景调整超时阈值,避免设置过短导致正常请求被中断

    • 使用开发工具监控:推荐使用Charles或Fiddler等工具监控网络请求,观察流式响应的实际响应时间