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

+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',
'本轮已完成多次查询,任务尚未完成。请继续重试,已有业务更改不会重复执行。',
);
}