feat: 任务队列改为并发池并支持幂等复用与退避重试

This commit is contained in:
cat
2026-10-08 17:32:34 +08:00
parent 7c751bfc02
commit bf56c2bd46
10 changed files with 1296 additions and 123 deletions
+21 -4
View File
@@ -17,8 +17,17 @@ export async function aiRoutes(app: FastifyInstance): Promise<void> {
}));
/** 提交 AI 任务,立即返回 taskId */
app.post<{ Body: { type: string; params: Record<string, unknown> } }>("/ai/tasks", async (request, reply) => {
const { type, params } = request.body ?? {};
app.post<{
Body: {
type: string;
params: Record<string, unknown>;
/** 可选:显式幂等键,用于跨请求去重 */
idempotencyKey?: string;
/** 可选:跳过结果复用,强制重新生成 */
force?: boolean;
};
}>("/ai/tasks", async (request, reply) => {
const { type, params, idempotencyKey, force } = request.body ?? {};
if (!type || !["aggregate-report", "generate-mind-map", "compare-recall"].includes(type)) {
return reply.status(400).send({ error: "无效的 type,允许: aggregate-report, generate-mind-map, compare-recall" });
@@ -27,8 +36,16 @@ export async function aiRoutes(app: FastifyInstance): Promise<void> {
return reply.status(400).send({ error: "params 必须为非空对象" });
}
const taskId = createTask(type as TaskType, params as Record<string, unknown>);
return reply.status(201).send({ taskId });
const created = createTask(type as TaskType, params as Record<string, unknown>, {
...(typeof idempotencyKey === "string" && idempotencyKey.trim().length > 0
? { idempotencyKey }
: {}),
...(force === true ? { force: true } : {}),
});
// 固定返回 201:Java 端 AiServiceClient 把非 201 当作提交失败,
// 所以不能改用 200 表达「复用」。复用信息放在响应体里。
return reply.status(201).send(created);
});
/** 查询任务状态与结果 */