All files / app/detail/hooks useStreamingReport.ts

89.85% Statements 62/69
84.37% Branches 27/32
81.81% Functions 9/11
91.93% Lines 57/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263                                                                                      1x                 1x         15x 15x 15x         15x   6x 6x                 6x   6x   6x 6x 1x     5x     5x                   4x 1x       3x 3x       3x     3x 6x   6x   1x           1x 1x       5x     5x 5x 8x 5x   5x   1x           1x     4x 4x   3x   2x     2x         2x             3x         1x             2x                                     1x       1x                       3x 3x                   3x 3x                         15x 1x 1x                 15x 1x 1x 1x     15x              
/**
 * useStreamingReport Hook
 *
 * 处理 AI 报告的流式生成
 * 支持实时显示、取消请求、错误处理
 */
 
import { useCallback, useRef, useState } from 'react';
 
import { API_BASE_URL } from '@/src/config/env';
import { useUserStore } from '@/src/store/userStore';
import { logger } from '@/src/utils/logger';
 
// ==================== 类型定义 ====================
 
/** 流式状态 */
export interface StreamingState {
  /** 当前流式内容 */
  content: string;
  /** 是否正在流式传输 */
  isStreaming: boolean;
  /** 是否已完成 */
  isComplete: boolean;
  /** 错误信息 */
  error: string | null;
  /** 进度估算 (0-100) */
  progress: number;
}
 
/** Hook 返回值 */
export interface UseStreamingReportReturn {
  /** 流式状态 */
  state: StreamingState;
  /** 开始流式生成 */
  startStreaming: (catfoodId: number, ingredients: string) => Promise<void>;
  /** 停止流式传输 */
  stopStreaming: () => void;
  /** 重置状态 */
  reset: () => void;
}
 
// ==================== 常量 ====================
 
const INITIAL_STATE: StreamingState = {
  content: '',
  isStreaming: false,
  isComplete: false,
  error: null,
  progress: 0,
};
 
// 估计的总字符数(用于进度计算)
const ESTIMATED_TOTAL_CHARS = 1500;
 
// ==================== Hook 实现 ====================
 
export function useStreamingReport(): UseStreamingReportReturn {
  const [state, setState] = useState<StreamingState>(INITIAL_STATE);
  const abortControllerRef = useRef<AbortController | null>(null);
  const contentRef = useRef<string>('');
 
  /**
   * 开始流式生成
   */
  const startStreaming = useCallback(async (catfoodId: number, ingredients: string) => {
    // 重置状态
    contentRef.current = '';
    setState({
      content: '',
      isStreaming: true,
      isComplete: false,
      error: null,
      progress: 0,
    });
 
    // 创建 AbortController
    abortControllerRef.current = new AbortController();
 
    try {
      // 获取 token
      const token = useUserStore.getState().accessToken;
      if (!token) {
        throw new Error('未登录');
      }
 
      logger.info('开始流式生成 AI 报告', { catfoodId });
 
      // 发起流式请求
      const response = await fetch(`${API_BASE_URL}/api/ai/${catfoodId}/stream/`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          Authorization: `Bearer ${token}`,
        },
        body: JSON.stringify({ ingredients }),
        signal: abortControllerRef.current.signal,
      });
 
      if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`);
      }
 
      // 获取 ReadableStream
      const reader = response.body?.getReader();
      Iif (!reader) {
        throw new Error('无法获取响应流');
      }
 
      const decoder = new TextDecoder();
 
      // 读取流
      while (true) {
        const { done, value } = await reader.read();
 
        if (done) {
          // 流正常结束
          setState((prev) => ({
            ...prev,
            isStreaming: false,
            isComplete: true,
            progress: 100,
          }));
          logger.info('流式生成完成', { catfoodId });
          break;
        }
 
        // 解码数据
        const chunk = decoder.decode(value, { stream: true });
 
        // 解析 SSE 格式
        const lines = chunk.split('\n');
        for (const line of lines) {
          if (line.startsWith('data: ')) {
            const data = line.slice(6); // 去掉 "data: " 前缀
 
            if (data === '[DONE]') {
              // 流结束标记
              setState((prev) => ({
                ...prev,
                isStreaming: false,
                isComplete: true,
                progress: 100,
              }));
              return;
            }
 
            try {
              const parsed = JSON.parse(data);
 
              if (parsed.content) {
                // 追加内容
                contentRef.current += parsed.content;
 
                // 计算进度
                const progress = Math.min(
                  (contentRef.current.length / ESTIMATED_TOTAL_CHARS) * 100,
                  95
                );
 
                setState((prev) => ({
                  ...prev,
                  content: contentRef.current,
                  progress,
                }));
              }
 
              if (parsed.error) {
                // If we throw here, it is caught by the inner catch (parseError)
                // We need to rethrow or handle it.
                // But wait, the inner catch is for JSON.parse errors or logic errors inside try block.
                // If we throw here, it goes to catch (parseError).
                throw new Error(parsed.error);
              }
            } catch (parseError) {
              // If it was our thrown error, we should probably rethrow it to the outer catch?
              // Or handle it here.
              // The current code swallows it if it's an Error object, unless we check.
 
              if (
                parseError instanceof Error &&
                parseError.message &&
                !parseError.message.includes('JSON')
              ) {
                // It might be the error we threw above.
                // But wait, if JSON.parse fails, it throws SyntaxError.
                // If we throw Error(parsed.error), it is an Error.
 
                // The issue is that the catch block below:
                // if (data && data !== '[DONE]' && !data.startsWith('{'))
                // This logic is for when JSON.parse fails (non-JSON data).
 
                // If we throw an error inside the try block, it is caught here.
                // And then ignored because of the if condition (data starts with '{' for json).
 
                // So the error is swallowed.
 
                // We should rethrow if it is a "real" error we want to propagate.
                throw parseError;
              }
 
              // 忽略非 JSON 数据
              Iif (data && data !== '[DONE]' && !data.startsWith('{')) {
                contentRef.current += data;
                setState((prev) => ({
                  ...prev,
                  content: contentRef.current,
                }));
              }
            }
          }
        }
      }
    } catch (error) {
      Eif (error instanceof Error) {
        Iif (error.name === 'AbortError') {
          // 用户主动取消
          logger.info('用户取消流式生成');
          setState((prev) => ({
            ...prev,
            isStreaming: false,
            error: null,
          }));
        } else {
          // 其他错误
          logger.error('流式生成失败', error);
          setState((prev) => ({
            ...prev,
            isStreaming: false,
            error: error.message || '生成报告失败',
          }));
        }
      }
    }
  }, []);
 
  /**
   * 停止流式传输
   */
  const stopStreaming = useCallback(() => {
    abortControllerRef.current?.abort();
    setState((prev) => ({
      ...prev,
      isStreaming: false,
    }));
  }, []);
 
  /**
   * 重置状态
   */
  const reset = useCallback(() => {
    abortControllerRef.current?.abort();
    contentRef.current = '';
    setState(INITIAL_STATE);
  }, []);
 
  return {
    state,
    startStreaming,
    stopStreaming,
    reset,
  };
}