使用统一httpclient,复用axios配置逻辑
Browse files- src/api/client.js +25 -16
- src/server/index.js +5 -5
- src/utils/httpClient.js +7 -0
src/api/client.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import axios from 'axios';
|
| 2 |
import tokenManager from '../auth/token_manager.js';
|
| 3 |
import config from '../config/config.js';
|
| 4 |
import { generateToolCallId } from '../utils/idGenerator.js';
|
|
@@ -6,7 +5,7 @@ import AntigravityRequester from '../AntigravityRequester.js';
|
|
| 6 |
import { saveBase64Image } from '../utils/imageStorage.js';
|
| 7 |
import logger from '../utils/logger.js';
|
| 8 |
import memoryManager, { MemoryPressure, registerMemoryPoolCleanup } from '../utils/memoryManager.js';
|
| 9 |
-
import { buildAxiosRequestConfig } from '../utils/httpClient.js';
|
| 10 |
import { setReasoningSignature, setToolSignature } from '../utils/thoughtSignatureCache.js';
|
| 11 |
import { getOriginalToolName } from '../utils/toolNameCache.js';
|
| 12 |
|
|
@@ -178,15 +177,6 @@ function buildHeaders(token) {
|
|
| 178 |
};
|
| 179 |
}
|
| 180 |
|
| 181 |
-
function buildAxiosConfig(url, headers, body = null) {
|
| 182 |
-
return buildAxiosRequestConfig({
|
| 183 |
-
method: 'POST',
|
| 184 |
-
url,
|
| 185 |
-
headers,
|
| 186 |
-
data: body
|
| 187 |
-
});
|
| 188 |
-
}
|
| 189 |
-
|
| 190 |
function buildRequesterConfig(headers, body = null) {
|
| 191 |
const reqConfig = {
|
| 192 |
method: 'POST',
|
|
@@ -341,8 +331,12 @@ export async function generateAssistantResponse(requestBody, token, callback) {
|
|
| 341 |
|
| 342 |
try {
|
| 343 |
if (useAxios) {
|
| 344 |
-
const
|
| 345 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
|
| 347 |
// 使用 Buffer 直接处理,避免 toString 的内存分配
|
| 348 |
response.data.on('data', chunk => {
|
|
@@ -392,7 +386,12 @@ export async function generateAssistantResponse(requestBody, token, callback) {
|
|
| 392 |
async function fetchRawModels(headers, token) {
|
| 393 |
try {
|
| 394 |
if (useAxios) {
|
| 395 |
-
const response = await
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
return response.data;
|
| 397 |
}
|
| 398 |
const response = await requester.antigravity_fetch(config.api.modelsUrl, buildRequesterConfig(headers, {}));
|
|
@@ -495,7 +494,12 @@ export async function generateAssistantResponseNoStream(requestBody, token) {
|
|
| 495 |
|
| 496 |
try {
|
| 497 |
if (useAxios) {
|
| 498 |
-
data = (await
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 499 |
} else {
|
| 500 |
const response = await requester.antigravity_fetch(config.api.noStreamUrl, buildRequesterConfig(headers, requestBody));
|
| 501 |
if (response.status !== 200) {
|
|
@@ -577,7 +581,12 @@ export async function generateImageForSD(requestBody, token) {
|
|
| 577 |
|
| 578 |
try {
|
| 579 |
if (useAxios) {
|
| 580 |
-
data = (await
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 581 |
} else {
|
| 582 |
const response = await requester.antigravity_fetch(config.api.noStreamUrl, buildRequesterConfig(headers, requestBody));
|
| 583 |
if (response.status !== 200) {
|
|
|
|
|
|
|
| 1 |
import tokenManager from '../auth/token_manager.js';
|
| 2 |
import config from '../config/config.js';
|
| 3 |
import { generateToolCallId } from '../utils/idGenerator.js';
|
|
|
|
| 5 |
import { saveBase64Image } from '../utils/imageStorage.js';
|
| 6 |
import logger from '../utils/logger.js';
|
| 7 |
import memoryManager, { MemoryPressure, registerMemoryPoolCleanup } from '../utils/memoryManager.js';
|
| 8 |
+
import { buildAxiosRequestConfig, httpRequest, httpStreamRequest } from '../utils/httpClient.js';
|
| 9 |
import { setReasoningSignature, setToolSignature } from '../utils/thoughtSignatureCache.js';
|
| 10 |
import { getOriginalToolName } from '../utils/toolNameCache.js';
|
| 11 |
|
|
|
|
| 177 |
};
|
| 178 |
}
|
| 179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
function buildRequesterConfig(headers, body = null) {
|
| 181 |
const reqConfig = {
|
| 182 |
method: 'POST',
|
|
|
|
| 331 |
|
| 332 |
try {
|
| 333 |
if (useAxios) {
|
| 334 |
+
const response = await httpStreamRequest({
|
| 335 |
+
method: 'POST',
|
| 336 |
+
url: config.api.url,
|
| 337 |
+
headers,
|
| 338 |
+
data: requestBody
|
| 339 |
+
});
|
| 340 |
|
| 341 |
// 使用 Buffer 直接处理,避免 toString 的内存分配
|
| 342 |
response.data.on('data', chunk => {
|
|
|
|
| 386 |
async function fetchRawModels(headers, token) {
|
| 387 |
try {
|
| 388 |
if (useAxios) {
|
| 389 |
+
const response = await httpRequest({
|
| 390 |
+
method: 'POST',
|
| 391 |
+
url: config.api.modelsUrl,
|
| 392 |
+
headers,
|
| 393 |
+
data: {}
|
| 394 |
+
});
|
| 395 |
return response.data;
|
| 396 |
}
|
| 397 |
const response = await requester.antigravity_fetch(config.api.modelsUrl, buildRequesterConfig(headers, {}));
|
|
|
|
| 494 |
|
| 495 |
try {
|
| 496 |
if (useAxios) {
|
| 497 |
+
data = (await httpRequest({
|
| 498 |
+
method: 'POST',
|
| 499 |
+
url: config.api.noStreamUrl,
|
| 500 |
+
headers,
|
| 501 |
+
data: requestBody
|
| 502 |
+
})).data;
|
| 503 |
} else {
|
| 504 |
const response = await requester.antigravity_fetch(config.api.noStreamUrl, buildRequesterConfig(headers, requestBody));
|
| 505 |
if (response.status !== 200) {
|
|
|
|
| 581 |
|
| 582 |
try {
|
| 583 |
if (useAxios) {
|
| 584 |
+
data = (await httpRequest({
|
| 585 |
+
method: 'POST',
|
| 586 |
+
url: config.api.noStreamUrl,
|
| 587 |
+
headers,
|
| 588 |
+
data: requestBody
|
| 589 |
+
})).data;
|
| 590 |
} else {
|
| 591 |
const response = await requester.antigravity_fetch(config.api.noStreamUrl, buildRequesterConfig(headers, requestBody));
|
| 592 |
if (response.status !== 200) {
|
src/server/index.js
CHANGED
|
@@ -10,7 +10,7 @@ import config from '../config/config.js';
|
|
| 10 |
import tokenManager from '../auth/token_manager.js';
|
| 11 |
import adminRouter from '../routes/admin.js';
|
| 12 |
import sdRouter from '../routes/sd.js';
|
| 13 |
-
import memoryManager, {
|
| 14 |
|
| 15 |
const __filename = fileURLToPath(import.meta.url);
|
| 16 |
const __dirname = path.dirname(__filename);
|
|
@@ -153,10 +153,10 @@ const createStreamChunk = (id, created, model, delta, finish_reason = null) => {
|
|
| 153 |
const writeStreamData = (res, data) => {
|
| 154 |
const json = JSON.stringify(data);
|
| 155 |
// 释放对象回池
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
res.write(SSE_PREFIX);
|
| 161 |
res.write(json);
|
| 162 |
res.write(SSE_SUFFIX);
|
|
|
|
| 10 |
import tokenManager from '../auth/token_manager.js';
|
| 11 |
import adminRouter from '../routes/admin.js';
|
| 12 |
import sdRouter from '../routes/sd.js';
|
| 13 |
+
import memoryManager, { registerMemoryPoolCleanup } from '../utils/memoryManager.js';
|
| 14 |
|
| 15 |
const __filename = fileURLToPath(import.meta.url);
|
| 16 |
const __dirname = path.dirname(__filename);
|
|
|
|
| 153 |
const writeStreamData = (res, data) => {
|
| 154 |
const json = JSON.stringify(data);
|
| 155 |
// 释放对象回池
|
| 156 |
+
const delta = { reasoning_content: data.reasoning_content };
|
| 157 |
+
if (data.thoughtSignature) {
|
| 158 |
+
delta.thoughtSignature = data.thoughtSignature;
|
| 159 |
+
}
|
| 160 |
res.write(SSE_PREFIX);
|
| 161 |
res.write(json);
|
| 162 |
res.write(SSE_SUFFIX);
|
src/utils/httpClient.js
CHANGED
|
@@ -68,3 +68,10 @@ export async function httpRequest(configOverrides) {
|
|
| 68 |
const axiosConfig = buildAxiosRequestConfig(configOverrides);
|
| 69 |
return axios(axiosConfig);
|
| 70 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
const axiosConfig = buildAxiosRequestConfig(configOverrides);
|
| 69 |
return axios(axiosConfig);
|
| 70 |
}
|
| 71 |
+
|
| 72 |
+
// 流式请求封装
|
| 73 |
+
export async function httpStreamRequest(configOverrides) {
|
| 74 |
+
const axiosConfig = buildAxiosRequestConfig(configOverrides);
|
| 75 |
+
axiosConfig.responseType = 'stream';
|
| 76 |
+
return axios(axiosConfig);
|
| 77 |
+
}
|