fix: 完善AI流式后台任务与模型配置

This commit is contained in:
LoveChengke committed 2026-11-07 22:25:39 +08:00
1 parent 0b35a58f07
commit cdb64c5499
30 files changed
+1732 -150

No files matched your search

+130 -1
View File
@@ -6,7 +6,7 @@ import { prisma } from '../../lib/db.js';
import { ApiError } from '../../lib/http.js';
import { getPlan, preparePlan, confirmPlan, discardPlan } from './plans.js';
import { planIdSchema, preparePlanSchema, confirmPlanSchema } from './ai.schemas.js';
import { API_PATHS } from '@classhelper/shared';
import { API_PATHS, type AiAgentEvent } from '@classhelper/shared';
import { sendOk } from '../../lib/http.js';
import { authenticate, getAuthUser, requirePermission } from '../../middleware/auth.js';
import { validate, validatedBody, validatedQuery } from '../../middleware/validate.js';
@@ -20,10 +20,139 @@ import {
type UpdateAiSettingsInput,
} from './ai.schemas.js';
import * as aiService from './ai.service.js';
import { discoverModelsSchema, taskAttachmentSchema } from './ai.schemas.js';
import { discoverModels } from './model-transport.js';
import * as tasks from './tasks.js';
import { setTimeout as delay } from 'node:timers/promises';
const router = Router();
router.use(authenticate());
router.get(
'/tasks/:id/events',
requirePermission('ai:use'),
validate({ params: planIdSchema }),
async (req, res) => {
const user = getAuthUser(req),
id = String(req.params.id);
await tasks.task(user, id);
res.status(200).set({
'content-type': 'text/event-stream; charset=utf-8',
'cache-control': 'no-cache, no-transform',
'x-accel-buffering': 'no',
});
res.flushHeaders();
const connection = new AbortController();
res.on('close', () => connection.abort());
const send = (event: AiAgentEvent) => {
if (!res.destroyed) res.write(`data: ${JSON.stringify(event)}\n\n`);
};
const sent = new Set<string>();
let lastText = '';
try {
while (!connection.signal.aborted) {
const current = await tasks.task(user, id);
for (const event of current.events) {
if (event.type === 'message') {
if (event.text !== lastText) {
send(event);
lastText = event.text;
}
} else if (!sent.has(JSON.stringify(event))) {
send(event);
sent.add(JSON.stringify(event));
}
}
if (['ready', 'succeeded'].includes(current.state) && current.result) {
send({ type: 'result', result: current.result });
break;
}
if (['failed', 'stopped'].includes(current.state)) {
send({
type: 'error',
code: 'AI_TASK_INCOMPLETE',
message: current.error || '任务已暂停,可继续处理。',
retryable: true,
});
break;
}
res.write(': heartbeat\n\n');
await delay(350, undefined, { signal: connection.signal });
}
} catch (error) {
if (!connection.signal.aborted)
send({
type: 'error',
code: 'AI_READ_FAILED',
message: error instanceof ApiError ? error.message : '连接暂时中断,后台任务继续运行。',
retryable: true,
});
} finally {
res.end();
}
},
);
router.get('/tasks', requirePermission('ai:use'), async (req, res) =>
sendOk(res, await tasks.listTasks(getAuthUser(req))),
);
router.post('/tasks', requirePermission('ai:use'), validate({ body: aiInterpretSchema }), async (req, res) =>
sendOk(res, await tasks.createTask(getAuthUser(req), req.body)),
);
router.get('/tasks/:id', requirePermission('ai:use'), validate({ params: planIdSchema }), async (req, res) =>
sendOk(res, await tasks.task(getAuthUser(req), String(req.params.id))),
);
router.put(
'/tasks/:id/files/:attachmentId',
requirePermission('ai:use'),
validate({ params: taskAttachmentSchema }),
async (req, res) => {
await tasks.stageAttachment(
getAuthUser(req),
String(req.params.id),
String(req.params.attachmentId),
req,
);
sendOk(res, null);
},
);
router.post(
'/tasks/:id/start',
requirePermission('ai:use'),
validate({ params: planIdSchema }),
async (req, res) => {
await tasks.startTask(getAuthUser(req), String(req.params.id));
sendOk(res, null);
},
);
router.post(
'/tasks/:id/execute',
requirePermission('ai:use'),
validate({ params: planIdSchema, body: confirmPlanSchema }),
async (req, res) => {
await tasks.executeTask(getAuthUser(req), String(req.params.id), req.body, req.socket.localPort!);
sendOk(res, null);
},
);
router.post(
'/tasks/:id/stop',
requirePermission('ai:use'),
validate({ params: planIdSchema }),
async (req, res) => {
await tasks.stopTask(getAuthUser(req), String(req.params.id));
sendOk(res, null);
},
);
router.post(
'/models',
requirePermission('ai:manage'),
validate({ body: discoverModelsSchema }),
async (req, res) => {
sendOk(res, await discoverModels(getAuthUser(req), req.body));
},
);
router.get('/settings', requirePermission('ai:manage'), async (_req, res) => {
sendOk(res, await aiService.getAiSettings(), '获取大模型设置成功');
});
@@ -2,6 +2,7 @@ import { z } from 'zod';
/** 大模型服务端实现:openai=真接口 / mock=离线确定性结果(自动化验收用,不联网) */
export const AI_PROVIDERS = ['openai', 'mock'] as const;
const reasoningEffortSchema = z.enum(['auto', 'none', 'low', 'medium', 'high', 'max']);
/** `PUT /api/ai/settings`:字段都可选;`apiKey` 传字符串=换新、null=清除、不传=保持 */
export const updateAiSettingsSchema = z.object({
@@ -9,10 +10,13 @@ export const updateAiSettingsSchema = z.object({
provider: z.enum(AI_PROVIDERS).optional(),
baseUrl: z.string().trim().max(300).optional(),
model: z.string().trim().max(120).optional(),
reasoningEffort: reasoningEffortSchema.optional(),
apiKey: z.string().trim().max(300).nullable().optional(),
systemPromptExtra: z.string().trim().max(1000).optional(),
});
export const discoverModelsSchema = updateAiSettingsSchema.pick({ baseUrl: true, apiKey: true });
/** `POST /api/ai/interpret`:一句中文原话 */
export const aiInterpretSchema = z.object({
text: z.string().trim().min(1, '请说明要做的事').max(8000),
@@ -50,6 +54,10 @@ export type AiInterpretInput = z.infer<typeof aiInterpretSchema>;
export type AiAuditQuery = z.infer<typeof aiAuditQuerySchema>;
export const planIdSchema = z.object({ id: z.string().min(1).max(100) });
export const taskAttachmentSchema = z.object({
id: z.string().min(1).max(100),
attachmentId: z.string().min(1).max(100),
});
export const preparePlanSchema = z
.object({
files: z.record(z.string(), z.object({ contentBase64: z.string().max(12 * 1024 * 1024) }).strict()),
@@ -13,6 +13,7 @@ export async function getAiSettings(): Promise<AiSettingsDto> {
SETTING_KEYS.aiProvider,
SETTING_KEYS.aiBaseUrl,
SETTING_KEYS.aiModel,
SETTING_KEYS.aiReasoningEffort,
SETTING_KEYS.aiApiKey,
SETTING_KEYS.aiSystemPromptExtra,
]);
@@ -22,6 +23,11 @@ export async function getAiSettings(): Promise<AiSettingsDto> {
provider: values[SETTING_KEYS.aiProvider] === 'mock' ? 'mock' : 'openai',
baseUrl: values[SETTING_KEYS.aiBaseUrl] ?? '',
model: values[SETTING_KEYS.aiModel] ?? '',
reasoningEffort: (['none', 'low', 'medium', 'high', 'max'].includes(
values[SETTING_KEYS.aiReasoningEffort] ?? '',
)
? values[SETTING_KEYS.aiReasoningEffort]
: 'auto') as AiSettingsDto['reasoningEffort'],
hasApiKey: apiKey.length > 0,
keyHint: apiKey.length >= 4 ? apiKey.slice(-4) : '',
systemPromptExtra: values[SETTING_KEYS.aiSystemPromptExtra] ?? '',
@@ -33,6 +39,8 @@ export async function updateAiSettings(
input: UpdateAiSettingsInput,
): Promise<AiSettingsDto> {
assertAdmin(user);
if (input.reasoningEffort !== undefined)
await setSetting(SETTING_KEYS.aiReasoningEffort, input.reasoningEffort);
if (input.enabled !== undefined) await setSetting(SETTING_KEYS.aiEnabled, input.enabled ? 'true' : 'false');
if (input.provider !== undefined) await setSetting(SETTING_KEYS.aiProvider, input.provider);
if (input.baseUrl !== undefined) await setSetting(SETTING_KEYS.aiBaseUrl, input.baseUrl.trim());
+50 -6
View File
@@ -1,13 +1,19 @@
import { z } from 'zod';
import { dayKeyLocal, type AiInterpretResult, type AiConversationResult } from '@classhelper/shared';
import {
dayKeyLocal,
type AiInterpretResult,
type AiConversationResult,
type AiPlanStep,
} from '@classhelper/shared';
import { assertPermission, assertPublish } from '../../lib/authz/index.js';
import { prisma } from '../../lib/db.js';
import { ApiError } from '../../lib/http.js';
import type { TokenPayload } from '../../lib/jwt.js';
import { loadSessionUser } from '../../lib/authz/session.js';
import { activeStudentWhere } from '../../lib/students.js';
import { capabilities, runRead } from './actions.js';
import { createPlan } from './plans.js';
import { planWithModel, sanitizePrompt } from './model-planner.js';
import { planWithModel, sanitizePrompt, type AgentRun } from './model-planner.js';
import { getAiSettings } from './ai.service.js';
import type { AiInterpretInput } from './ai.schemas.js';
@@ -113,8 +119,10 @@ function localReply(
export async function interpretConversation(
user: TokenPayload,
input: AiInterpretInput,
run: AgentRun = {},
): Promise<AiInterpretResult & AiConversationResult> {
await assertPermission(user, 'ai:use');
run.emit?.({ type: 'status', message: '正在检查权限与可用功能' });
const settings = await getAiSettings();
if (!settings.enabled) throw new ApiError(400, 'AI_NOT_CONFIGURED', '请管理员先开启 AI 助手');
const tools = await capabilities(user);
@@ -131,7 +139,7 @@ export async function interpretConversation(
}
let modelResults: AiConversationResult['results'] = [];
if (settings.provider === 'openai') {
const planned = await planWithModel(user, input, tools, classes);
const planned = await planWithModel(user, input, tools, classes, run);
reply = planned.reply;
modelResults = planned.results;
} else reply ??= localReply(input, classes);
@@ -174,6 +182,7 @@ export async function interpretConversation(
action: query.action,
label: tools.find((item) => item.action === query.action)?.label ?? query.action,
data: await runRead(user, query),
operation: query,
});
for (const operation of reply.operations)
if (operation.action === 'calls.create' && operation.params?.studentName && !operation.params.studentId) {
@@ -188,9 +197,44 @@ export async function interpretConversation(
operation.params.studentId = students[0]!.id;
delete operation.params.studentName;
}
const plan = reply.operations.length
? await createPlan(user, sanitizePrompt(input.text), reply.operations, input.attachments ?? [])
: { planId: null, steps: [] };
run.signal?.throwIfAborted();
await run.authorize?.();
user = await loadSessionUser({ ...user });
await assertPermission(user, 'ai:use');
run.emit?.({
type: 'status',
message: reply.operations.length ? '正在校验参数并生成变更预览' : '正在整理查询结果',
});
let plan: { planId: string | null; steps: AiPlanStep[] };
try {
plan = reply.operations.length
? await createPlan(user, sanitizePrompt(input.text), reply.operations, input.attachments ?? [])
: { planId: null, steps: [] };
} catch (error) {
if (
settings.provider !== 'openai' ||
!(error instanceof ApiError) ||
![400, 422].includes(error.status) ||
(run.corrections ?? 0) >= 2
)
throw error;
run.emit?.({ type: 'status', message: '参数需要调整,正在自动核对并修正预览' });
return interpretConversation(
user,
{
...input,
messages: [
...(input.messages ?? []),
{ role: 'assistant' as const, content: JSON.stringify(reply).slice(0, 8000) },
{
role: 'user' as const,
content: `预览校验未通过:${sanitizePrompt(error.message)}。请查询真实数据后修正参数,不能省略用户要求。信息不足时提出具体问题。`,
},
].slice(-20),
},
{ ...run, corrections: (run.corrections ?? 0) + 1 },
);
}
const message =
reply.message ||
(plan.steps.length
+142 -34
View File
@@ -1,6 +1,14 @@
import { z } from 'zod';
import { dayKeyLocal, type AiCapability, type AiConversationResult } from '@classhelper/shared';
import { setTimeout as delay } from 'node:timers/promises';
import {
dayKeyLocal,
type AiAgentEvent,
type AiCapability,
type AiConversationResult,
} from '@classhelper/shared';
import type { TokenPayload } from '../../lib/jwt.js';
import { loadSessionUser } from '../../lib/authz/session.js';
import { assertPermission } from '../../lib/authz/index.js';
import { ApiError } from '../../lib/http.js';
import { decryptSecret } from '../../lib/secrets.js';
import { getSetting, SETTING_KEYS } from '../../lib/settings.js';
@@ -8,6 +16,7 @@ import { getAiSettings } from './ai.service.js';
import { runRead } from './actions.js';
import { redact } from './business-read.js';
import type { AiInterpretInput } from './ai.schemas.js';
import { modelEndpoint, readCompletion } from './model-transport.js';
const operationSchema = z
.object({
@@ -24,7 +33,37 @@ const replySchema = z
queries: z.array(operationSchema).max(8).default([]),
operations: z.array(operationSchema).max(16).default([]),
})
.strict();
.refine(
(value) => !!value.message?.trim() || value.queries.length > 0 || value.operations.length > 0,
'回复不能是空对象',
);
export function parseModelReply(content: string) {
const trimmed = content
.trim()
.replace(/^```(?:json)?\s*/i, '')
.replace(/\s*```$/, '');
// 只修正展示包装,不猜测 ID、不补写业务参数,不执行任意文本。
const start = trimmed.indexOf('{'),
end = trimmed.lastIndexOf('}');
return replySchema.parse(JSON.parse(start >= 0 && end >= start ? trimmed.slice(start, end + 1) : trimmed));
}
function visibleMessage(content: string): string | undefined {
const match = content.match(/"message"\s*:\s*"((?:[^"\\]|\\.)*)/);
if (!match) return;
try {
return sanitizePrompt(JSON.parse(`"${match[1]}"`)).slice(0, 4000);
} catch {
return;
}
}
export interface AgentRun {
authorize?: () => Promise<void>;
corrections?: number;
signal?: AbortSignal;
emit?: (event: AiAgentEvent) => void;
}
export function sanitizePrompt(text: string): string {
return text
.replace(
@@ -43,23 +82,19 @@ export async function planWithModel(
input: AiInterpretInput,
tools: AiCapability[],
classes: { id: string; name: string }[],
run: AgentRun = {},
) {
const settings = await getAiSettings();
if (!settings.enabled || !settings.baseUrl || !settings.hasApiKey)
throw new ApiError(400, 'AI_NOT_CONFIGURED', '请管理员先配置模型服务');
let url: URL;
try {
url = new URL(settings.baseUrl);
} catch {
throw ApiError.badRequest('模型服务地址无效');
}
if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password)
throw ApiError.badRequest('模型服务地址无效');
const url = modelEndpoint(settings.baseUrl);
const signal = AbortSignal.any([...(run.signal ? [run.signal] : []), AbortSignal.timeout(300000)]);
const messages = [
{
role: 'system',
content: [
'你是 ClassHelper 助手。只输出严格 JSON:{message,queries:[{action,target?,query?}],operations:[{action,target?,params?,attachmentId?}]}。',
'例如普通回复:{"message":"你好,需要处理什么?","queries":[],"operations":[]};查询:{"message":"正在查询班级","queries":[{"action":"classes.list"}],"operations":[]}。禁止 Markdown 围栏,message 只写简短进度或给用户的回复。',
'queries 只能查询,operations 是待确认写操作,不能声称已执行。先查询真实 id 和现状,再规划修改;不猜测班级、账号、学生、课程和时间点 id。信息不足时追问。',
'仅可选择工具目录中的 action。工具 schema.target 列出路径参数,schema.params 是 params 的 JSON Schema。查询条件放 query。所有数据和动作遵循操作者的实时权限。',
'数据库备份/恢复/清空/切换/SQL、服务器配置/启停/升级和密钥维护禁止执行。密码字段留空,由用户在专门表单输入。',
@@ -77,40 +112,109 @@ export async function planWithModel(
];
const results: AiConversationResult['results'] = [];
const apiKey = decryptSecret(await getSetting(SETTING_KEYS.aiApiKey));
for (let round = 0; round < 5; round++) {
let corrections = 0;
for (let round = 0; round < 12; round++) {
signal.throwIfAborted();
await run.authorize?.();
const actor = await loadSessionUser({ ...user });
await assertPermission(actor, 'ai:use');
for (const result of results)
if (result.operation) {
const current = await runRead(actor, result.operation);
if (JSON.stringify(current) !== JSON.stringify(result.data))
throw ApiError.conflict('记录或权限已变化,请重新处理任务以读取当前结果');
}
run.emit?.({ type: 'status', message: round ? '继续处理任务' : '正在分析任务' });
let response: Response;
try {
response = await fetch(`${settings.baseUrl.replace(/\/+$/, '')}/chat/completions`, {
method: 'POST',
headers: { 'content-type': 'application/json', authorization: `Bearer ${apiKey}` },
body: JSON.stringify({
model: settings.model || 'gpt-4o-mini',
temperature: 0.2,
response_format: { type: 'json_object' },
messages,
}),
signal: AbortSignal.timeout(20_000),
});
} catch {
throw new ApiError(504, 'AI_TIMEOUT', '无法连接模型服务,请稍后重试');
for (let attempt = 0; ; attempt++) {
try {
response = await fetch(url, {
method: 'POST',
headers: { 'content-type': 'application/json', authorization: `Bearer ${apiKey}` },
body: JSON.stringify({
model: settings.model || 'gpt-4o-mini',
stream: true,
max_tokens: 16384 + corrections * 16384,
...(settings.reasoningEffort !== 'auto' ? { reasoning_effort: settings.reasoningEffort } : {}),
temperature: 0.2,
response_format: { type: 'json_object' },
messages,
}),
signal: AbortSignal.any([signal, AbortSignal.timeout(120000)]),
});
if (!response.ok && [429, 500, 502, 503, 504].includes(response.status) && attempt < 2) {
await response.body?.cancel();
run.emit?.({ type: 'status', message: '模型服务繁忙,正在自动重连' });
await delay(500 * (attempt + 1), undefined, { signal });
continue;
}
break;
} catch {
signal.throwIfAborted();
if (attempt >= 2) throw new ApiError(504, 'AI_TIMEOUT', '连接暂时中断,任务已保留。可重试继续处理。');
run.emit?.({ type: 'status', message: '连接暂时中断,正在自动重连' });
await delay(500 * (attempt + 1), undefined, { signal });
}
}
if (!response.ok) throw new ApiError(502, 'AI_PROVIDER_ERROR', `模型服务返回 HTTP ${response.status}`);
const body = (await response.json()) as { choices?: { message?: { content?: string } }[] };
const content = body.choices?.[0]?.message?.content;
if (!response.ok)
throw new ApiError(
502,
'AI_PROVIDER_ERROR',
`模型服务暂不可用(HTTP ${response.status}),请检查服务地址、模型与密钥。任务尚未执行。`,
);
let reply: z.infer<typeof replySchema>;
try {
reply = replySchema.parse(JSON.parse(content ?? ''));
let published = '';
let lastReasoningUpdate = 0;
const completion = await readCompletion(
response,
(content) => {
const text = visibleMessage(content);
if (text && text !== published) {
run.emit?.({ type: 'message', text });
published = text;
}
},
(characters) => {
if (Date.now() - lastReasoningUpdate < 1000) return;
lastReasoningUpdate = Date.now();
run.emit?.({ type: 'status', message: `模型正在推理 · 已接收 ${characters} 字符进度` });
},
);
if (completion.truncated) throw new Error('输出被截断');
reply = parseModelReply(completion.content);
} catch {
throw new ApiError(502, 'AI_INVALID_OUTPUT', '模型返回的计划格式无效,请换一种说法');
signal.throwIfAborted();
if (++corrections >= 3)
throw new ApiError(
502,
'AI_TASK_INCOMPLETE',
'任务已保留,但模型暂未给出可执行方案。自动校正已尝试三次,可点击继续重试;尚未执行任何更改。',
);
run.emit?.({ type: 'status', message: `正在自动校正回复(${corrections}/3)` });
messages.push({
role: 'user',
content:
'上一轮输出未通过校验或被截断。请重新输出完整 JSON 对象,包含 message 字符串、queries 数组和 operations 数组;不使用 Markdown,不添加解释。不确定的业务参数请在 message 中追问,不猜测。',
});
continue;
}
corrections = 0;
if (!reply.queries.length) return { reply, results };
if (reply.operations.length) throw ApiError.badRequest('模型需要先完成查询再生成操作计划,请重试');
for (const query of reply.queries)
for (const query of reply.queries) {
signal.throwIfAborted();
run.emit?.({
type: 'status',
message: `正在${tools.find((item) => item.action === query.action)?.label ?? '查询记录'}`,
});
results.push({
action: query.action,
label: tools.find((item) => item.action === query.action)?.label ?? query.action,
data: await runRead(user, query),
data: await runRead(actor, query),
operation: query,
});
run.emit?.({ type: 'status', message: '查询完成,正在核对下一步' });
}
messages.push(
{ role: 'assistant', content: JSON.stringify(reply) },
{
@@ -119,5 +223,9 @@ export async function planWithModel(
},
);
}
throw ApiError.badRequest('任务查询过多,请缩小范围');
throw new ApiError(
502,
'AI_TASK_INCOMPLETE',
'本轮已完成多次查询,任务尚未完成。请继续重试,已有业务更改不会重复执行。',
);
}
@@ -0,0 +1,141 @@
import { z } from 'zod';
import { EventStreamDecoder, type AiModelInfo, type AiReasoningEffort } from '@classhelper/shared';
import { ApiError } from '../../lib/http.js';
import { assertAdmin } from '../../lib/authz/index.js';
import type { TokenPayload } from '../../lib/jwt.js';
import { decryptSecret } from '../../lib/secrets.js';
import { getSetting, SETTING_KEYS } from '../../lib/settings.js';
import type { discoverModelsSchema } from './ai.schemas.js';
import { getAiSettings } from './ai.service.js';
export function modelEndpoint(baseUrl: string, resource = 'chat/completions'): string {
let url: URL;
try {
url = new URL(baseUrl.trim());
} catch {
throw ApiError.badRequest('请填写有效的模型服务地址');
}
if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password)
throw ApiError.badRequest('请填写有效的模型服务地址');
const base = url.pathname.replace(/\/+$/, '').replace(/\/chat\/completions$/, '');
url.pathname = `${base}/${resource}`;
url.hash = '';
return url.toString();
}
export async function discoverModels(
user: TokenPayload,
input: z.infer<typeof discoverModelsSchema>,
): Promise<AiModelInfo[]> {
assertAdmin(user);
const settings = await getAiSettings();
const apiKey = input.apiKey?.trim() || decryptSecret(await getSetting(SETTING_KEYS.aiApiKey));
if (!apiKey) throw ApiError.badRequest('请先填写模型密钥');
let response: Response;
try {
response = await fetch(modelEndpoint(input.baseUrl ?? settings.baseUrl, 'models'), {
headers: { authorization: `Bearer ${apiKey}` },
redirect: 'error',
signal: AbortSignal.timeout(20000),
});
} catch {
throw new ApiError(502, 'AI_MODELS_UNAVAILABLE', '暂时无法获取模型列表,可重试或手动填写模型名');
}
if (!response.ok)
throw new ApiError(
502,
'AI_MODELS_UNAVAILABLE',
`模型列表请求失败(HTTP ${response.status}),请检查地址与密钥`,
);
const parsed = z
.object({
data: z
.array(
z.object({
id: z.string().min(1).max(120),
name: z.string().max(200).optional(),
effort: z.object({ supported_levels: z.array(z.string()) }).optional(),
}),
)
.max(2000),
})
.safeParse(await response.json());
if (!parsed.success)
throw new ApiError(502, 'AI_MODELS_UNAVAILABLE', '此服务未提供兼容的模型列表,可手动填写模型名');
const levels: AiReasoningEffort[] = ['none', 'low', 'medium', 'high', 'max'];
return [
...new Map(
parsed.data.data.map((item) => [
item.id,
{
id: item.id,
name: item.name || item.id,
efforts: item.effort
? [
'auto' as const,
'none' as const,
...levels.filter((level) => item.effort!.supported_levels.includes(level)),
]
: ['auto' as const, ...levels.filter((level) => level !== 'max')],
},
]),
).values(),
].sort((a, b) => a.id.localeCompare(b.id));
}
interface Completion {
choices?: {
message?: { content?: string };
delta?: { content?: string; reasoning_content?: string };
finish_reason?: string;
}[];
error?: unknown;
}
/** 只转发可展示的回复和阶段信息,不把模型推理原文、未校验参数或凭据发给浏览器。 */
export async function readCompletion(
response: Response,
onText: (content: string) => void,
onReasoning?: (characters: number) => void,
): Promise<{ content: string; truncated: boolean }> {
if (!response.headers.get('content-type')?.includes('text/event-stream')) {
const body = (await response.json()) as Completion;
return {
content: body.choices?.[0]?.message?.content ?? '',
truncated: body.choices?.[0]?.finish_reason === 'length',
};
}
if (!response.body) throw new Error('模型未返回内容');
const reader = response.body.getReader(),
decoder = new TextDecoder(),
stream = new EventStreamDecoder();
let reasoningCharacters = 0;
let content = '',
truncated = false,
completed = false;
try {
while (true) {
const chunk = await reader.read();
if (chunk.done) break;
for (const event of stream.push(decoder.decode(chunk.value, { stream: true }))) {
if (event === '[DONE]') {
completed = true;
continue;
}
const data = JSON.parse(event) as Completion;
if (data.error) throw new Error('模型流中断');
const choice = data.choices?.[0];
reasoningCharacters += choice?.delta?.reasoning_content?.length ?? 0;
if (choice?.delta?.reasoning_content) onReasoning?.(reasoningCharacters);
content += choice?.delta?.content ?? '';
if (content.length > 256000) throw new Error('模型回复过长');
if (choice?.finish_reason) completed = true;
if (choice?.finish_reason === 'length') truncated = true;
onText(content);
}
}
} finally {
await reader.cancel().catch(() => undefined);
reader.releaseLock();
}
return { content, truncated: truncated || !completed };
}
+366
View File
@@ -0,0 +1,366 @@
import fs from 'node:fs';
import fsp from 'node:fs/promises';
import path from 'node:path';
import { createHash, randomUUID } from 'node:crypto';
import { Transform } from 'node:stream';
import { pipeline } from 'node:stream/promises';
import type { Request } from 'express';
import type { AiAgentEvent, AiConversationResult, AiTaskDto } from '@classhelper/shared';
import { prisma } from '../../lib/db.js';
import { ApiError } from '../../lib/http.js';
import { assertPermission, loadAuthPrincipal } from '../../lib/authz/index.js';
import { loadSessionUser } from '../../lib/authz/session.js';
import { encryptSecret, decryptSecret } from '../../lib/secrets.js';
import { signToken, type TokenPayload } from '../../lib/jwt.js';
import { coursewareRoot } from '../courseware/courseware.service.js';
import { interpretConversation } from './conversation.js';
import { sanitizePrompt } from './model-planner.js';
import { runRead } from './actions.js';
import { confirmPlan, getPlan, preparePlan } from './plans.js';
import type { AiInterpretInput } from './ai.schemas.js';
const active = new Map<string, AbortController>();
const stagingRoot = path.resolve(coursewareRoot, '.ai-tasks');
const decode = <T>(value: string): T => JSON.parse(decryptSecret(value));
async function accessStamp(user: TokenPayload): Promise<string> {
const current = await loadSessionUser({ ...user });
const principal = await loadAuthPrincipal(current);
const relations =
current.role === 'ADMIN'
? []
: await Promise.all([
prisma.class.findMany({
select: { id: true, grade: true, teacherId: true },
orderBy: { id: 'asc' },
}),
prisma.course.findMany({
where: { teacherId: current.sub },
select: { id: true, classId: true, name: true },
orderBy: { id: 'asc' },
}),
]);
return createHash('sha256')
.update(
JSON.stringify({
role: current.role,
keys: [...principal.permissions].sort(),
grants: [...principal.grants].sort(),
relations,
}),
)
.digest('hex');
}
function attachmentPath(id: string, attachmentId: string): string {
return path.join(
stagingRoot,
createHash('sha256').update(id).digest('hex'),
createHash('sha256').update(attachmentId).digest('hex'),
);
}
async function owned(user: TokenPayload, id: string) {
await assertPermission(user, 'ai:use');
const row = await prisma.aiTask.findFirst({ where: { id, userId: user.sub } });
if (!row) throw ApiError.forbidden('无法访问此任务');
const saved = decode<AiInterpretInput & { accessStamp?: string }>(row.input).accessStamp;
if (saved && saved !== (await accessStamp(user)))
throw ApiError.forbidden('权限或任课关系已变化,请重新提交任务,原计划不会自动执行');
return row;
}
export async function createTask(user: TokenPayload, input: AiInterpretInput) {
await assertPermission(user, 'ai:use');
const expired = await prisma.aiTask.findMany({
where: {
userId: user.sub,
state: { in: ['preparing', 'ready', 'failed', 'stopped', 'succeeded'] },
updatedAt: { lt: new Date(Date.now() - 24 * 60 * 60 * 1000) },
},
take: 20,
});
for (const row of expired) await cleanup(row.id, decode<AiInterpretInput>(row.input));
if (
(await prisma.aiTask.count({ where: { userId: user.sub, state: { in: ['planning', 'executing'] } } })) >=
3
)
throw ApiError.conflict('请等待现有后台任务完成');
const safe = {
...input,
text: sanitizePrompt(input.text),
messages: input.messages?.map((item) => ({ ...item, content: sanitizePrompt(item.content) })),
};
Object.assign(safe, { accessStamp: await accessStamp(user) });
const row = await prisma.aiTask.create({
data: { userId: user.sub, input: encryptSecret(JSON.stringify(safe)) },
});
return { id: row.id };
}
export async function stageAttachment(
user: TokenPayload,
id: string,
attachmentId: string,
request: Request,
) {
const row = await owned(user, id);
if (row.state !== 'preparing' || active.has(id)) throw ApiError.conflict('任务已开始,不能更换附件');
const attachment = decode<AiInterpretInput>(row.input).attachments?.find(
(item) => item.id === attachmentId,
);
if (!attachment) throw ApiError.forbidden('附件不属于本次任务');
const file = attachmentPath(id, attachmentId),
partial = file + '.' + randomUUID() + '.partial';
await fsp.mkdir(path.dirname(file), { recursive: true });
let size = 0;
const digest = createHash('sha256');
try {
await pipeline(
request,
new Transform({
transform(chunk: Buffer, _encoding, callback) {
size += chunk.length;
if (size > attachment.size) {
callback(ApiError.badRequest('附件大小与预览不一致'));
return;
}
digest.update(chunk);
callback(null, chunk);
},
}),
fs.createWriteStream(partial, { flags: 'wx' }),
);
if (size !== attachment.size || digest.digest('hex') !== attachment.sha256)
throw ApiError.badRequest('附件校验失败,请重新上传');
await fsp.rename(partial, file);
} finally {
await fsp.rm(partial, { force: true });
}
}
async function cleanup(id: string, input: AiInterpretInput) {
for (const attachment of input.attachments ?? [])
await fsp.rm(attachmentPath(id, attachment.id), { force: true });
await fsp.rmdir(path.dirname(attachmentPath(id, ''))).catch(() => undefined);
}
export async function task(user: TokenPayload, id: string): Promise<AiTaskDto> {
const row = await owned(user, id);
if (
['planning', 'executing'].includes(row.state) &&
!active.has(id) &&
Date.now() - row.updatedAt.getTime() > 2000
) {
await prisma.aiTask.updateMany({
where: { id, state: row.state },
data: {
state: 'failed',
error:
row.state === 'executing'
? '服务已重启,请核对步骤状态后继续;成功步骤不会重复执行。'
: '服务已重启,任务已保留,可继续处理。',
},
});
return task(user, id);
}
let result = row.result ? decode<AiConversationResult>(row.result) : undefined;
if (result) {
if (result.planId) result.steps = await getPlan(user, result.planId);
// 重进页面时重新查询授权范围,不能用历史快照泄漏撤权后的记录。
const results: AiConversationResult['results'] = [];
for (const item of result.results)
if (item.operation) {
const data = await runRead(user, item.operation);
if (JSON.stringify(data) !== JSON.stringify(item.data))
throw ApiError.forbidden('记录或权限已变化,请重新查询当前结果');
results.push({ ...item, data });
}
result = { ...result, results };
}
return {
id,
state: row.state,
text: decode<AiInterpretInput>(row.input).text,
result,
events: row.events ? decode<AiAgentEvent[]>(row.events) : [],
...(row.error ? { error: row.error } : {}),
};
}
export async function listTasks(user: TokenPayload) {
await assertPermission(user, 'ai:use');
const rows = await prisma.aiTask.findMany({
where: { userId: user.sub },
orderBy: { createdAt: 'desc' },
take: 20,
});
return rows.map((row) => ({
id: row.id,
state: row.state,
text: decode<AiInterpretInput>(row.input).text,
events: [],
}));
}
function launch(
id: string,
user: TokenPayload,
work: (signal: AbortSignal, emit: (event: AiAgentEvent) => void) => Promise<void>,
) {
const controller = new AbortController();
active.set(id, controller);
let events: AiAgentEvent[] = [],
writes = Promise.resolve(),
lastMessage = '';
const emit = (event: AiAgentEvent) => {
if (event.type === 'status' && event.message.startsWith('模型正在推理'))
events = events.filter((item) => item.type !== 'status' || !item.message.startsWith('模型正在推理'));
if (event.type === 'message') {
if (lastMessage === event.text) return;
lastMessage = event.text;
events = events.filter((item) => item.type !== 'message');
}
events = [...events, event].slice(-80);
const snapshot = encryptSecret(JSON.stringify(events));
writes = writes
.then(async () => {
await prisma.aiTask.update({ where: { id }, data: { events: snapshot } });
})
.catch(() => undefined);
};
void work(controller.signal, emit)
.catch(async (error) => {
const message = controller.signal.aborted
? '任务已按你的要求停止。'
: error instanceof ApiError
? error.message
: '任务暂未完成,已保留当前进度。请重试继续。';
await prisma.aiTask.update({
where: { id },
data: { state: controller.signal.aborted ? 'stopped' : 'failed', error: message },
});
await prisma.aiAudit.create({
data: { userId: user.sub, prompt: '后台任务', intent: id, status: 'failed', message },
});
})
.finally(async () => {
await writes.catch(() => undefined);
active.delete(id);
})
.catch(() => {
active.delete(id);
});
}
export async function startTask(user: TokenPayload, id: string) {
const row = await owned(user, id);
if (active.has(id)) return;
if (!['preparing', 'failed', 'stopped'].includes(row.state) || row.result)
throw ApiError.conflict('任务已有结果,请核对现有计划');
const input = decode<AiInterpretInput>(row.input);
for (const attachment of input.attachments ?? [])
if (!fs.existsSync(attachmentPath(id, attachment.id)))
throw ApiError.badRequest('附件尚未上传完成,请重新选择原文件');
const claimed = await prisma.aiTask.updateMany({
where: { id, state: row.state },
data: { state: 'planning', error: '' },
});
if (!claimed.count) return;
launch(id, user, async (signal, emit) => {
const result = await interpretConversation(user, input, {
signal,
emit,
authorize: async () => {
await owned(user, id);
},
});
if (result.planId && result.steps.some((step) => step.status === 'draft')) {
for (const step of result.steps.filter((step) => step.status === 'draft')) {
signal.throwIfAborted();
const file = attachmentPath(id, step.attachmentId!);
if ((await fsp.stat(file)).size > 8 * 1024 * 1024)
throw ApiError.badRequest('结构化导入文件不可超过 8 MB');
result.steps = await preparePlan(user, result.planId, {
[step.attachmentId!]: { contentBase64: (await fsp.readFile(file)).toString('base64') },
});
}
}
signal.throwIfAborted();
await prisma.aiTask.update({
where: { id },
data: {
state: result.steps.length ? 'ready' : 'succeeded',
result: encryptSecret(JSON.stringify(result)),
},
});
emit({ type: 'status', message: result.steps.length ? '变更预览已准备,等待确认' : '处理完成' });
if (!result.steps.length) await cleanup(id, input);
});
}
export async function executeTask(
user: TokenPayload,
id: string,
confirmation: Parameters<typeof confirmPlan>[2],
localPort: number,
) {
const row = await owned(user, id);
if (active.has(id)) return;
const result = row.result ? decode<AiConversationResult>(row.result) : undefined;
if (!result?.planId) throw ApiError.badRequest('请先生成操作预览');
const steps = await confirmPlan(user, result.planId, confirmation);
const input = decode<AiInterpretInput>(row.input);
for (const step of steps)
if (
step.status !== 'succeeded' &&
step.request?.attachmentId &&
!fs.existsSync(attachmentPath(id, step.request.attachmentId))
)
throw ApiError.badRequest('临时附件已过期,请重新上传原文件');
const claimed = await prisma.aiTask.updateMany({
where: { id, state: row.state },
data: { state: 'executing', error: '' },
});
if (!claimed.count) return;
launch(id, user, async (signal, emit) => {
for (const step of steps) {
signal.throwIfAborted();
if (step.status === 'succeeded') continue;
const request = step.request;
if (!request) throw ApiError.conflict('此步骤未确认,请重新核对');
emit({ type: 'status', message: `正在执行:${step.label}` });
// 地址只由本进程监听端口和确认后的动作目录生成,不接受模型 URL。
const url = new URL(`/api${request.path}`, `http://127.0.0.1:${localPort}`);
for (const [key, value] of Object.entries(request.query)) url.searchParams.set(key, String(value));
const file = request.attachmentId ? attachmentPath(id, request.attachmentId) : undefined;
const response = await fetch(url, {
method: request.method,
headers: {
authorization: `Bearer ${signToken(user)}`,
'x-classhelper-ai-step': step.id,
'content-type': file ? 'application/octet-stream' : 'application/json',
...(file ? { 'x-classhelper-ai-sha256': request.sha256! } : {}),
},
body: file
? (fs.createReadStream(file) as unknown as NonNullable<Parameters<typeof fetch>[1]>['body'])
: JSON.stringify(request.body),
...(file ? { duplex: 'half' } : {}),
signal: AbortSignal.timeout(600000),
});
const body = (await response.json()) as { message?: string };
if (!response.ok)
throw new ApiError(response.status, 'AI_READ_FAILED', body.message || '此步骤未完成,后续步骤已暂停');
emit({ type: 'status', message: `已完成:${step.label}` });
}
result.steps = await getPlan(user, result.planId!);
await prisma.aiTask.update({
where: { id },
data: { state: 'succeeded', result: encryptSecret(JSON.stringify(result)) },
});
await cleanup(id, input);
});
}
export async function stopTask(user: TokenPayload, id: string) {
await owned(user, id);
const controller = active.get(id);
if (controller) controller.abort();
else
await prisma.aiTask.update({
where: { id },
data: { state: 'stopped', error: '任务已按你的要求停止。' },
});
}