使用示例
用于多模态模型「文生图」异步任务查询
const agent = this.getAgent();
if (!agent) return;
const taskInfo = await agent.taskInfo();
console.log("res", agentInfo);
this.pollingTimers[taskId] = setInterval(async () => {
this.pollingCounts[taskId]++;
console.log(
`轮询第${this.pollingCounts[taskId]}次,任务ID: ${taskId},图片索引: ${imageIndex}`
);
try {
const result = await agent.queryTasks({ taskId });
console.log(`taskId: ${taskId} 轮询结果:`, result);
// 根据任务状态处理结果
if (
this.handleTaskResult(result, taskId, imageIndex)
) {
// 任务完成,停止轮询
console.log(
`taskId: ${taskId} 任务完成,停止轮询`
);
this.clearPollingTimer(taskId);
return;
}
// 检查是否超过最大轮询次数
if (
this.pollingCounts[taskId] >= this.maxPollingCount
) {
console.warn(
`taskId: ${taskId} 轮询超时,停止轮询`
);
this.clearPollingTimer(taskId);
this.handlePollingTimeout(taskId, imageIndex);
return;
}
} catch (error) {
console.error(
`taskId: ${taskId} 轮询请求失败:`,
error
);
this.pollingCounts[taskId]++;
// 连续失败3次则停止轮询
if (this.pollingCounts[taskId] >= 3) {
console.error(
`taskId: ${taskId} 轮询连续失败,停止轮询`
);
this.clearPollingTimer(taskId);
this.handlePollingError(
error,
taskId,
imageIndex
);
return;
}
}
}, 3000); // 3秒轮询一次
const agentInfo = await agent.queryTasks();
属性名 | 类型 | 说明 | 最低支持版本 |
|---|---|---|---|
| taskId | string | 任务id | 文生图: 0.3.0, 其他任务: < 0.3.0 |
属性名 | 类型 | 说明 | 最低支持版本 |
|---|---|---|---|
| request_id | string | 请求id | 文生图: 0.3.0, 其他任务: < 0.3.0 |
| output | 见下方代码块 | 任务输出信息 | 文生图: 0.3.0, 其他任务: < 0.3.0 |
| usage | { image_count?: number } | 输出信息 | 文生图: 0.3.0, 其他任务: < 0.3.0 |
/** 任务输出信息 */
output: {
task_id: string;
task_status: "PENDING" | "RUNNING" | "SUCCEEDED" | "FAILED" | "CANCELED" | "UNKNOWN";
results?: Array<{
url?: string;
[key: string]: any;
}>;
task_metrics?: {
TOTAL: number;
SUCCEEDED: number;
FAILED: number;
};
};