55 lines
1.9 KiB
JavaScript
55 lines
1.9 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
|
|
let dispatchType = task.dispatchType || 'runninghub';
|
|
|
|
const apikey = modelData[AIGC]?.[platform]?.apikey || '';
|
|
|
|
let response;
|
|
|
|
try {
|
|
const headers = await outside[platform].getGenerateHeader(apikey, dispatchType, jwtToken);
|
|
const url = outside[platform].getGenerateUrl(dispatchType);
|
|
const body = outside[platform].getGenerateBody({payload: task.taskData, apikey}, dispatchType, jwtToken);
|
|
|
|
response = await fetch(url, { method: 'POST', headers, body: body });
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`外部平台返回错误状态: ${response.status} ${response.statusText}`);
|
|
}
|
|
} catch (error) {
|
|
console.error(`[externalPostRequest] 请求失败: taskId=${task.taskId}, error=${error.message}`);
|
|
return {
|
|
taskId: task.taskId,
|
|
remoteTaskId: { type: 2, message: '算力维护中' },
|
|
platform,
|
|
AIGC
|
|
};
|
|
}
|
|
|
|
try {
|
|
const successResult = await outside[platform].getSuccessTasks(response, dispatchType);
|
|
|
|
let remoteTaskId;
|
|
if (successResult.type === 2) {
|
|
remoteTaskId = successResult;
|
|
console.error(`[externalPostRequest] 任务失败: taskId=${task.taskId}, result=`, successResult);
|
|
} else {
|
|
remoteTaskId = { type: 1, data: successResult };
|
|
}
|
|
|
|
return { taskId: task.taskId, remoteTaskId, platform, AIGC, workflowId: task.workflowId };
|
|
} catch (parseError) {
|
|
console.error(`[externalPostRequest] 解析响应失败: taskId=${task.taskId}, error=${parseError.message}`);
|
|
return {
|
|
taskId: task.taskId,
|
|
remoteTaskId: { type: 2, message: `解析响应失败: ${parseError.message}` },
|
|
platform,
|
|
AIGC
|
|
};
|
|
}
|
|
}
|