feat(lpt-ai):异步任务队列 + 提交/轮询路由
lpt-ai 新增异步任务处理能力,LLM 调用改为后台 Worker 顺序执行:
新增 src/task-queue.ts:
- createTask(type, params) → taskId(randomUUID)
- getTask(taskId) → { status, result?, error? }
- 单线程 Worker(setImmediate 链),同一时刻只处理一个 LLM 调用
- 按类型分派到对应的 prompt 函数
- TTL 清理:5 分钟/次,移除完成超过 1 小时的记录
- 异常隔离 + logStore 集成
修改 src/routes/ai.ts:
- POST /ai/tasks:接收 { type, params },返回 201 { taskId }
- GET /ai/tasks/:taskId:返回任务状态与结果(pending/running/done/failed)
- 旧同步端点完全不动,向后兼容
This commit is contained in:
@@ -6,6 +6,7 @@ import type { FastifyInstance } from "fastify";
|
||||
import { chat, isAvailable, loadConfig, LlmError } from "../llm/client.js";
|
||||
import { aggregateReportPrompt, compareRecallPrompt, generateMindMapPrompt, type FragmentInput } from "../llm/prompts.js";
|
||||
import { logStore } from "../admin/store.js";
|
||||
import { createTask, getTask, type TaskType } from "../task-queue.js";
|
||||
|
||||
interface AggregateReportBody {
|
||||
taskName: string;
|
||||
@@ -173,4 +174,31 @@ export async function aiRoutes(app: FastifyInstance): Promise<void> {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
|
||||
// ============ 异步任务(提交 + 轮询)============
|
||||
|
||||
/** 提交 AI 任务,立即返回 taskId */
|
||||
app.post<{ Body: { type: string; params: Record<string, unknown> } }>("/ai/tasks", async (request, reply) => {
|
||||
const { type, params } = 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" });
|
||||
}
|
||||
if (!params || typeof params !== "object") {
|
||||
return reply.status(400).send({ error: "params 必须为非空对象" });
|
||||
}
|
||||
|
||||
const taskId = createTask(type as TaskType, params as Record<string, unknown>);
|
||||
return reply.status(201).send({ taskId });
|
||||
});
|
||||
|
||||
/** 查询任务状态与结果 */
|
||||
app.get<{ Params: { taskId: string } }>("/ai/tasks/:taskId", async (request, reply) => {
|
||||
const { taskId } = request.params;
|
||||
const task = getTask(taskId);
|
||||
if (!task) {
|
||||
return reply.status(404).send({ error: "任务不存在" });
|
||||
}
|
||||
return task;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user