AIClient-2-API/src/providers/openai/openai-responses-core.js
hex2077 de3f6a5f21 feat: 添加监控请求ID临时存储功能到各AI服务提供商
在各AI服务提供商的核心API方法中添加monitorRequestId临时存储逻辑,用于跟踪请求链路。当请求体中包含_monitorRequestId字段时,将其存储到配置对象中并从请求体中删除,避免干扰API调用。

修改涉及以下提供商:
- Claude (claude-core.js, claude-kiro.js)
- OpenAI (openai-core.js, openai-responses-core.js, iflow-core.js, codex-core.js, qwen-core.js)
- Forward (forward-core.js)
- Gemini (gemini-core.js, antigravity-core.js)

统一处理模式:检查请求体中的_monitorRequestId字段,存储到this.config._monitorRequestId,然后从请求体中删除该字段。
2026-02-05 15:28:43 +08:00

190 lines
7.9 KiB
JavaScript

import axios from 'axios';
import logger from '../../utils/logger.js';
import * as http from 'http';
import * as https from 'https';
import { configureAxiosProxy } from '../../utils/proxy-utils.js';
// OpenAI Responses API specification service for interacting with third-party models
export class OpenAIResponsesApiService {
constructor(config) {
if (!config.OPENAI_API_KEY) {
throw new Error("OpenAI API Key is required for OpenAIResponsesApiService.");
}
this.config = config;
this.apiKey = config.OPENAI_API_KEY;
this.baseUrl = config.OPENAI_BASE_URL || 'https://api.openai.com/v1';
this.useSystemProxy = config?.USE_SYSTEM_PROXY_OPENAI ?? false;
logger.info(`[OpenAIResponses] System proxy ${this.useSystemProxy ? 'enabled' : 'disabled'}`);
// 配置 HTTP/HTTPS agent 限制连接池大小,避免资源泄漏
const httpAgent = new http.Agent({
keepAlive: true,
maxSockets: 100,
maxFreeSockets: 5,
timeout: 120000,
});
const httpsAgent = new https.Agent({
keepAlive: true,
maxSockets: 100,
maxFreeSockets: 5,
timeout: 120000,
});
const axiosConfig = {
baseURL: this.baseUrl,
httpAgent,
httpsAgent,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiKey}`
}
};
// 禁用系统代理以避免HTTPS代理错误
if (!this.useSystemProxy) {
axiosConfig.proxy = false;
}
// 配置自定义代理 (使用 openai-custom 的代理配置)
configureAxiosProxy(axiosConfig, config, 'openai-custom');
this.axiosInstance = axios.create(axiosConfig);
}
async callApi(endpoint, body, isRetry = false, retryCount = 0) {
const maxRetries = this.config.REQUEST_MAX_RETRIES || 3;
const baseDelay = this.config.REQUEST_BASE_DELAY || 1000; // 1 second base delay
try {
const response = await this.axiosInstance.post(endpoint, body);
return response.data;
} catch (error) {
const status = error.response?.status;
const data = error.response?.data;
if (status === 401 || status === 403) {
logger.error(`[API] Received ${status}. API Key might be invalid or expired.`);
throw error;
}
// Handle 429 (Too Many Requests) with exponential backoff
if (status === 429 && retryCount < maxRetries) {
const delay = baseDelay * Math.pow(2, retryCount);
logger.info(`[API] Received 429 (Too Many Requests). Retrying in ${delay}ms... (attempt ${retryCount + 1}/${maxRetries})`);
await new Promise(resolve => setTimeout(resolve, delay));
return this.callApi(endpoint, body, isRetry, retryCount + 1);
}
// Handle other retryable errors (5xx server errors)
if (status >= 500 && status < 600 && retryCount < maxRetries) {
const delay = baseDelay * Math.pow(2, retryCount);
logger.info(`[API] Received ${status} server error. Retrying in ${delay}ms... (attempt ${retryCount + 1}/${maxRetries})`);
await new Promise(resolve => setTimeout(resolve, delay));
return this.callApi(endpoint, body, isRetry, retryCount + 1);
}
logger.error(`Error calling OpenAI Responses API (Status: ${status}):`, data || error.message);
throw error;
}
}
async *streamApi(endpoint, body, isRetry = false, retryCount = 0) {
const maxRetries = this.config.REQUEST_MAX_RETRIES || 3;
const baseDelay = this.config.REQUEST_BASE_DELAY || 1000; // 1 second base delay
// OpenAI 的流式请求需要将 stream 设置为 true
const streamRequestBody = { ...body, stream: true };
try {
const response = await this.axiosInstance.post(endpoint, streamRequestBody, {
responseType: 'stream'
});
const stream = response.data;
let buffer = '';
for await (const chunk of stream) {
buffer += chunk.toString();
let newlineIndex;
while ((newlineIndex = buffer.indexOf('\n')) !== -1) {
const line = buffer.substring(0, newlineIndex).trim();
buffer = buffer.substring(newlineIndex + 1);
if (line.startsWith('data: ')) {
const jsonData = line.substring(6).trim();
if (jsonData === '[DONE]') {
return; // Stream finished
}
try {
const parsedChunk = JSON.parse(jsonData);
yield parsedChunk;
} catch (e) {
logger.warn("[OpenAIResponsesApiService] Failed to parse stream chunk JSON:", e.message, "Data:", jsonData);
}
} else if (line === '') {
// Empty line, end of an event
}
}
}
} catch (error) {
const status = error.response?.status;
const data = error.response?.data;
if (status === 401 || status === 403) {
logger.error(`[API] Received ${status} during stream. API Key might be invalid or expired.`);
throw error;
}
// Handle 429 (Too Many Requests) with exponential backoff
if (status === 429 && retryCount < maxRetries) {
const delay = baseDelay * Math.pow(2, retryCount);
logger.info(`[API] Received 429 (Too Many Requests) during stream. Retrying in ${delay}ms... (attempt ${retryCount + 1}/${maxRetries})`);
await new Promise(resolve => setTimeout(resolve, delay));
yield* this.streamApi(endpoint, body, isRetry, retryCount + 1);
return;
}
// Handle other retryable errors (5xx server errors)
if (status >= 500 && status < 600 && retryCount < maxRetries) {
const delay = baseDelay * Math.pow(2, retryCount);
logger.info(`[API] Received ${status} server error during stream. Retrying in ${delay}ms... (attempt ${retryCount + 1}/${maxRetries})`);
await new Promise(resolve => setTimeout(resolve, delay));
yield* this.streamApi(endpoint, body, isRetry, retryCount + 1);
return;
}
logger.error(`Error calling OpenAI Responses streaming API (Status: ${status}):`, data || error.message);
throw error;
}
}
async generateContent(model, requestBody) {
// 临时存储 monitorRequestId
if (requestBody._monitorRequestId) {
this.config._monitorRequestId = requestBody._monitorRequestId;
delete requestBody._monitorRequestId;
}
return this.callApi('/responses', requestBody);
}
async *generateContentStream(model, requestBody) {
// 临时存储 monitorRequestId
if (requestBody._monitorRequestId) {
this.config._monitorRequestId = requestBody._monitorRequestId;
delete requestBody._monitorRequestId;
}
yield* this.streamApi('/responses', requestBody);
}
async listModels() {
try {
const response = await this.axiosInstance.get('/models');
return response.data;
} catch (error) {
const status = error.response?.status;
const data = error.response?.data;
logger.error(`Error listing OpenAI Responses models (Status: ${status}):`, data || error.message);
throw error;
}
}
}