Spaces:
Sleeping
Sleeping
File size: 4,349 Bytes
74e7808 | 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 | import apiClient from './index';
import { toCamelCase } from './utils';
import type {
AnalysisRequest,
AnalysisResult,
AnalyzeResponse,
AnalyzeAsyncResponse,
AnalysisReport,
TaskStatus,
TaskListResponse,
} from '../types/analysis';
// ============ API Interfaces ============
export const analysisApi = {
/**
* Trigger stock analysis.
* @param data Analysis request payload
* @returns Sync mode returns AnalysisResult; async mode returns accepted task payloads
*/
analyze: async (data: AnalysisRequest): Promise<AnalyzeResponse> => {
const requestData = {
stock_code: data.stockCode,
stock_codes: data.stockCodes,
report_type: data.reportType || 'detailed',
force_refresh: data.forceRefresh || false,
async_mode: data.asyncMode || false,
stock_name: data.stockName,
original_query: data.originalQuery,
selection_source: data.selectionSource,
};
const response = await apiClient.post<Record<string, unknown>>(
'/api/v1/analysis/analyze',
requestData
);
const result = toCamelCase<AnalyzeResponse>(response.data);
// Ensure the sync analysis report payload is converted recursively.
if ('report' in result && result.report) {
result.report = toCamelCase<AnalysisReport>(result.report);
}
return result;
},
/**
* Trigger analysis in async mode.
* @param data Analysis request payload
* @returns Accepted task payloads; throws DuplicateTaskError on 409
*/
analyzeAsync: async (data: AnalysisRequest): Promise<AnalyzeAsyncResponse> => {
const requestData = {
stock_code: data.stockCode,
stock_codes: data.stockCodes,
report_type: data.reportType || 'detailed',
force_refresh: data.forceRefresh || false,
async_mode: true,
stock_name: data.stockName,
original_query: data.originalQuery,
selection_source: data.selectionSource,
};
const response = await apiClient.post<Record<string, unknown>>(
'/api/v1/analysis/analyze',
requestData,
{
// Allow 202 accepted responses in addition to standard success codes.
validateStatus: (status) => status === 200 || status === 202 || status === 409,
}
);
// Handle duplicate submission compatibility.
if (response.status === 409) {
const errorData = toCamelCase<{
error: string;
message: string;
stockCode: string;
existingTaskId: string;
}>(response.data);
throw new DuplicateTaskError(errorData.stockCode, errorData.existingTaskId, errorData.message);
}
return toCamelCase<AnalyzeAsyncResponse>(response.data);
},
/**
* Get async task status.
* @param taskId Task ID
*/
getStatus: async (taskId: string): Promise<TaskStatus> => {
const response = await apiClient.get<Record<string, unknown>>(
`/api/v1/analysis/status/${taskId}`
);
const data = toCamelCase<TaskStatus>(response.data);
// Ensure nested result payloads are converted recursively.
if (data.result) {
data.result = toCamelCase<AnalysisResult>(data.result);
if (data.result.report) {
data.result.report = toCamelCase<AnalysisReport>(data.result.report);
}
}
return data;
},
/**
* Get task list.
* @param params Filter parameters
*/
getTasks: async (params?: {
status?: string;
limit?: number;
}): Promise<TaskListResponse> => {
const response = await apiClient.get<Record<string, unknown>>(
'/api/v1/analysis/tasks',
{ params }
);
const data = toCamelCase<TaskListResponse>(response.data);
return data;
},
/**
* Get the SSE stream URL.
*/
getTaskStreamUrl: (): string => {
// Read API base URL from the shared client.
const baseUrl = apiClient.defaults.baseURL || '';
return `${baseUrl}/api/v1/analysis/tasks/stream`;
},
};
// ============ Custom Error Classes ============
/**
* Duplicate task error.
*/
export class DuplicateTaskError extends Error {
stockCode: string;
existingTaskId: string;
constructor(stockCode: string, existingTaskId: string, message?: string) {
super(message || `股票 ${stockCode} 正在分析中`);
this.name = 'DuplicateTaskError';
this.stockCode = stockCode;
this.existingTaskId = existingTaskId;
}
}
|