koishi-plugin-aka-ai-generator 0.7.3 → 0.7.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +58 -54
- package/lib/providers/yunwu-video.d.ts +1 -1
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -954,12 +954,12 @@ var YunwuVideoProvider = class {
|
|
|
954
954
|
logger?.error("API 返回错误状态", { status: response.status, error: errorMsg, response: response.data });
|
|
955
955
|
throw new Error(sanitizeString(errorMsg));
|
|
956
956
|
}
|
|
957
|
-
const taskId = response.id
|
|
957
|
+
const taskId = response.id;
|
|
958
958
|
if (!taskId) {
|
|
959
959
|
logger?.error("未能获取任务ID", { response });
|
|
960
960
|
throw new Error("未能获取任务ID,请检查 API 响应格式");
|
|
961
961
|
}
|
|
962
|
-
logger?.info("视频任务已创建", { taskId });
|
|
962
|
+
logger?.info("视频任务已创建", { taskId, status: response.status });
|
|
963
963
|
return taskId;
|
|
964
964
|
} catch (error) {
|
|
965
965
|
logger?.error("创建视频任务失败", { error: sanitizeError(error) });
|
|
@@ -979,54 +979,31 @@ var YunwuVideoProvider = class {
|
|
|
979
979
|
}
|
|
980
980
|
/**
|
|
981
981
|
* 查询任务状态
|
|
982
|
-
* API: GET /v1/video/{taskId}
|
|
982
|
+
* API: GET /v1/video/query?id={taskId}
|
|
983
983
|
*/
|
|
984
984
|
async queryTaskStatus(taskId) {
|
|
985
985
|
const { logger, ctx } = this.config;
|
|
986
986
|
try {
|
|
987
|
-
const
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
`${this.config.apiBase}${endpoint}`,
|
|
998
|
-
{
|
|
999
|
-
headers: {
|
|
1000
|
-
"Authorization": `Bearer ${this.config.apiKey}`,
|
|
1001
|
-
"Accept": "application/json"
|
|
1002
|
-
},
|
|
1003
|
-
timeout: this.config.apiTimeout * 1e3
|
|
1004
|
-
}
|
|
1005
|
-
);
|
|
1006
|
-
if (response && !response.error) {
|
|
1007
|
-
logger?.debug("查询任务状态成功", { taskId, endpoint });
|
|
1008
|
-
break;
|
|
1009
|
-
}
|
|
1010
|
-
} catch (err) {
|
|
1011
|
-
lastError = err;
|
|
1012
|
-
if (err.response?.status === 404 && possibleEndpoints.indexOf(endpoint) < possibleEndpoints.length - 1) {
|
|
1013
|
-
logger?.debug("尝试下一个查询端点", { taskId, endpoint, nextEndpoint: possibleEndpoints[possibleEndpoints.indexOf(endpoint) + 1] });
|
|
1014
|
-
continue;
|
|
1015
|
-
}
|
|
1016
|
-
throw err;
|
|
987
|
+
const endpoint = `/v1/video/query?id=${encodeURIComponent(taskId)}`;
|
|
988
|
+
logger?.debug("查询任务状态", { taskId, endpoint });
|
|
989
|
+
const response = await ctx.http.get(
|
|
990
|
+
`${this.config.apiBase}${endpoint}`,
|
|
991
|
+
{
|
|
992
|
+
headers: {
|
|
993
|
+
"Authorization": `Bearer ${this.config.apiKey}`,
|
|
994
|
+
"Accept": "application/json"
|
|
995
|
+
},
|
|
996
|
+
timeout: this.config.apiTimeout * 1e3
|
|
1017
997
|
}
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
}
|
|
1022
|
-
const status = response.status || response.data?.status || "pending";
|
|
1023
|
-
const videoUrl = response.video_url || response.url || response.data?.video_url || response.data?.url;
|
|
998
|
+
);
|
|
999
|
+
const status = response.status || "pending";
|
|
1000
|
+
const videoUrl = response.video_url || null;
|
|
1024
1001
|
return {
|
|
1025
1002
|
status,
|
|
1026
|
-
taskId,
|
|
1027
|
-
videoUrl,
|
|
1028
|
-
error: response.error
|
|
1029
|
-
progress: response.progress
|
|
1003
|
+
taskId: response.id || taskId,
|
|
1004
|
+
videoUrl: videoUrl || void 0,
|
|
1005
|
+
error: response.error,
|
|
1006
|
+
progress: response.progress
|
|
1030
1007
|
};
|
|
1031
1008
|
} catch (error) {
|
|
1032
1009
|
logger?.error("查询任务状态失败", { taskId, error: sanitizeError(error) });
|
|
@@ -1043,6 +1020,8 @@ var YunwuVideoProvider = class {
|
|
|
1043
1020
|
async pollTaskCompletion(taskId, maxWaitTime = 300, pollInterval = 3, onProgress) {
|
|
1044
1021
|
const { logger } = this.config;
|
|
1045
1022
|
const startTime = Date.now();
|
|
1023
|
+
let consecutiveFailures = 0;
|
|
1024
|
+
const maxConsecutiveFailures = 5;
|
|
1046
1025
|
while (true) {
|
|
1047
1026
|
const elapsed = (Date.now() - startTime) / 1e3;
|
|
1048
1027
|
if (elapsed > maxWaitTime) {
|
|
@@ -1050,17 +1029,40 @@ var YunwuVideoProvider = class {
|
|
|
1050
1029
|
throw new Error(`视频生成超时(已等待${Math.floor(elapsed)}秒),任务ID: ${taskId}
|
|
1051
1030
|
请使用"查询视频 ${taskId}"命令稍后查询结果`);
|
|
1052
1031
|
}
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1032
|
+
try {
|
|
1033
|
+
const status = await this.queryTaskStatus(taskId);
|
|
1034
|
+
consecutiveFailures = 0;
|
|
1035
|
+
logger?.debug("任务状态", { taskId, status: status.status, elapsed: Math.floor(elapsed) });
|
|
1036
|
+
if (onProgress) {
|
|
1037
|
+
await onProgress(status);
|
|
1038
|
+
}
|
|
1039
|
+
if (status.status === "completed" && status.videoUrl) {
|
|
1040
|
+
logger?.info("视频生成完成", { taskId, elapsed: Math.floor(elapsed) });
|
|
1041
|
+
return status.videoUrl;
|
|
1042
|
+
}
|
|
1043
|
+
if (status.status === "failed") {
|
|
1044
|
+
throw new Error(status.error || "视频生成失败");
|
|
1045
|
+
}
|
|
1046
|
+
} catch (error) {
|
|
1047
|
+
consecutiveFailures++;
|
|
1048
|
+
logger?.warn("查询任务状态失败,继续重试", {
|
|
1049
|
+
taskId,
|
|
1050
|
+
consecutiveFailures,
|
|
1051
|
+
maxConsecutiveFailures,
|
|
1052
|
+
error: sanitizeError(error),
|
|
1053
|
+
elapsed: Math.floor(elapsed)
|
|
1054
|
+
});
|
|
1055
|
+
if (consecutiveFailures >= maxConsecutiveFailures) {
|
|
1056
|
+
logger?.error("连续查询失败次数过多,终止轮询", {
|
|
1057
|
+
taskId,
|
|
1058
|
+
consecutiveFailures,
|
|
1059
|
+
elapsed: Math.floor(elapsed)
|
|
1060
|
+
});
|
|
1061
|
+
throw new Error(`查询任务状态连续失败 ${consecutiveFailures} 次,任务ID: ${taskId}
|
|
1062
|
+
请稍后使用"查询视频 ${taskId}"命令手动查询结果`);
|
|
1063
|
+
}
|
|
1064
|
+
await new Promise((resolve) => setTimeout(resolve, pollInterval * 1e3 * 2));
|
|
1065
|
+
continue;
|
|
1064
1066
|
}
|
|
1065
1067
|
await new Promise((resolve) => setTimeout(resolve, pollInterval * 1e3));
|
|
1066
1068
|
}
|
|
@@ -1073,6 +1075,8 @@ var YunwuVideoProvider = class {
|
|
|
1073
1075
|
try {
|
|
1074
1076
|
logger?.info("开始生成视频", { prompt, imageUrl, options });
|
|
1075
1077
|
const taskId = await this.createVideoTask(prompt, imageUrl, options);
|
|
1078
|
+
logger?.debug("任务已创建,等待 3 秒后开始查询", { taskId });
|
|
1079
|
+
await new Promise((resolve) => setTimeout(resolve, 3e3));
|
|
1076
1080
|
const videoUrl = await this.pollTaskCompletion(taskId, maxWaitTime);
|
|
1077
1081
|
logger?.info("视频生成完成", { taskId, videoUrl });
|
|
1078
1082
|
return videoUrl;
|
|
@@ -14,7 +14,7 @@ export declare class YunwuVideoProvider implements VideoProvider {
|
|
|
14
14
|
createVideoTask(prompt: string, imageUrl: string, options?: VideoGenerationOptions): Promise<string>;
|
|
15
15
|
/**
|
|
16
16
|
* 查询任务状态
|
|
17
|
-
* API: GET /v1/video/{taskId}
|
|
17
|
+
* API: GET /v1/video/query?id={taskId}
|
|
18
18
|
*/
|
|
19
19
|
queryTaskStatus(taskId: string): Promise<VideoTaskStatus>;
|
|
20
20
|
/**
|