99 lines
3.6 KiB
JavaScript
99 lines
3.6 KiB
JavaScript
import {modelData} from '../config/Config.js';
|
|
import outside from './outPlatforms/outside.js'
|
|
|
|
// 发送请求
|
|
export async function externalPostRequest(task, jwtToken = null) {
|
|
const platform = task.platformName
|
|
const AIGC = process.env.PROJECT_PREFIX
|
|
|
|
// 获取分发标识,默认为 'runninghub'
|
|
const dispatchType = task.dispatchType || 'runninghub';
|
|
|
|
console.log(`[externalPostRequest] 任务分发 - 平台: ${platform}, 分发标识: ${dispatchType}`);
|
|
|
|
const apikey = modelData[AIGC]?.[platform]?.apikey || '';
|
|
|
|
let response;
|
|
let success = false;
|
|
|
|
try {
|
|
// 对于 comfyui 平台,使用分发标识来调用相应的接口
|
|
const headers = outside[platform].getGenerateHeader(apikey, dispatchType, jwtToken);
|
|
const url = outside[platform].getGenerateUrl(dispatchType);
|
|
const body = outside[platform].getGenerateBody({payload: task.taskData, apikey}, dispatchType, jwtToken);
|
|
|
|
console.log(`[externalPostRequest] 发送请求到 ${platform} (${dispatchType}): ${url}`);
|
|
|
|
response = await fetch(url, { method: 'POST', headers, body: body });
|
|
|
|
// 检查响应状态
|
|
if (!response.ok) {
|
|
throw new Error(`外部平台返回错误状态: ${response.status} ${response.statusText}`);
|
|
}
|
|
|
|
success = true;
|
|
} catch (error) {
|
|
// 如果是 comfyui 平台且使用了 messageDispatcher 分发,尝试降级到 runninghub
|
|
if (platform === 'comfyui' && dispatchType === 'messageDispatcher') {
|
|
console.warn('[externalPostRequest] messageDispatcher 分发失败,降级使用 runninghub:', error.message);
|
|
|
|
const fallbackDispatchType = 'runninghub';
|
|
|
|
try {
|
|
const headers = outside[platform].getGenerateHeader(apikey, fallbackDispatchType, jwtToken);
|
|
const url = outside[platform].getGenerateUrl(fallbackDispatchType);
|
|
const body = outside[platform].getGenerateBody({payload: task.taskData, apikey}, fallbackDispatchType, jwtToken);
|
|
|
|
console.log(`[externalPostRequest] 降级发送请求到 ${platform} (${fallbackDispatchType}): ${url}`);
|
|
|
|
response = await fetch(url, { method: 'POST', headers, body: body });
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`降级平台返回错误状态: ${response.status} ${response.statusText}`);
|
|
}
|
|
|
|
success = true;
|
|
} catch (fallbackError) {
|
|
console.error('[externalPostRequest] 降级也失败:', fallbackError.message);
|
|
return {
|
|
taskId: task.taskId,
|
|
remoteTaskId: { type: 2, message: `请求失败: ${fallbackError.message}` },
|
|
platform,
|
|
AIGC
|
|
};
|
|
}
|
|
} else {
|
|
console.error('[externalPostRequest] 外部请求失败:', error);
|
|
return {
|
|
taskId: task.taskId,
|
|
remoteTaskId: { type: 2, message: `外部请求失败: ${error.message}` },
|
|
platform,
|
|
AIGC
|
|
};
|
|
}
|
|
}
|
|
|
|
// 处理成功响应
|
|
try {
|
|
const successResult = await outside[platform].getSuccessTasks(response, dispatchType);
|
|
console.log(`[externalPostRequest] ${platform} 响应:`, successResult);
|
|
|
|
let remoteTaskId;
|
|
if (successResult.type === 2) {
|
|
remoteTaskId = successResult;
|
|
} else {
|
|
remoteTaskId = { type: 1, data: successResult };
|
|
}
|
|
|
|
return { taskId: task.taskId, remoteTaskId, platform, AIGC, workflowId: task.workflowId };
|
|
} catch (parseError) {
|
|
console.error('[externalPostRequest] 解析响应失败:', parseError);
|
|
return {
|
|
taskId: task.taskId,
|
|
remoteTaskId: { type: 2, message: `解析响应失败: ${parseError.message}` },
|
|
platform,
|
|
AIGC
|
|
};
|
|
}
|
|
}
|