hydro-ai-helper 1.8.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +31 -0
- package/.github/workflows/sync-releases-to-gitee.yml +119 -0
- package/.github/workflows/sync-to-gitee.yml +53 -0
- package/.vercelignore +31 -0
- package/CONTRIBUTING.md +165 -0
- package/README.md +273 -0
- package/TROUBLESHOOTING.md +355 -0
- package/api/badge-active.ts +88 -0
- package/api/badge-installs.ts +75 -0
- package/api/report.ts +242 -0
- package/assets/screenshots/1.png +0 -0
- package/assets/screenshots/2.png +0 -0
- package/assets/screenshots/3.png +0 -0
- package/assets/screenshots/4.png +0 -0
- package/assets/screenshots/5.png +0 -0
- package/assets/screenshots/6.png +0 -0
- package/assets/screenshots/7.png +0 -0
- package/dist/__tests__/__mocks__/hydrooj.js +29 -0
- package/dist/__tests__/__mocks__/hydrooj.js.map +1 -0
- package/dist/constants/jailbreakRules.js +29 -0
- package/dist/constants/jailbreakRules.js.map +1 -0
- package/dist/constants/permissions.js +42 -0
- package/dist/constants/permissions.js.map +1 -0
- package/dist/handlers/adminConfigHandler.js +347 -0
- package/dist/handlers/adminConfigHandler.js.map +1 -0
- package/dist/handlers/adminHandler.js +378 -0
- package/dist/handlers/adminHandler.js.map +1 -0
- package/dist/handlers/analyticsHandler.js +433 -0
- package/dist/handlers/analyticsHandler.js.map +1 -0
- package/dist/handlers/dashboardHandler.js +22 -0
- package/dist/handlers/dashboardHandler.js.map +1 -0
- package/dist/handlers/exportHandler.js +118 -0
- package/dist/handlers/exportHandler.js.map +1 -0
- package/dist/handlers/studentHandler.js +430 -0
- package/dist/handlers/studentHandler.js.map +1 -0
- package/dist/handlers/teacherHandler.js +246 -0
- package/dist/handlers/teacherHandler.js.map +1 -0
- package/dist/handlers/testHandler.js +27 -0
- package/dist/handlers/testHandler.js.map +1 -0
- package/dist/handlers/updateHandler.js +84 -0
- package/dist/handlers/updateHandler.js.map +1 -0
- package/dist/handlers/versionHandler.js +61 -0
- package/dist/handlers/versionHandler.js.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +151 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/crypto.js +110 -0
- package/dist/lib/crypto.js.map +1 -0
- package/dist/lib/httpHelpers.js +61 -0
- package/dist/lib/httpHelpers.js.map +1 -0
- package/dist/lib/queryHelpers.js +81 -0
- package/dist/lib/queryHelpers.js.map +1 -0
- package/dist/models/aiConfig.js +255 -0
- package/dist/models/aiConfig.js.map +1 -0
- package/dist/models/conversation.js +209 -0
- package/dist/models/conversation.js.map +1 -0
- package/dist/models/jailbreakLog.js +68 -0
- package/dist/models/jailbreakLog.js.map +1 -0
- package/dist/models/message.js +174 -0
- package/dist/models/message.js.map +1 -0
- package/dist/models/pluginInstall.js +95 -0
- package/dist/models/pluginInstall.js.map +1 -0
- package/dist/models/rateLimitRecord.js +54 -0
- package/dist/models/rateLimitRecord.js.map +1 -0
- package/dist/models/versionCache.js +89 -0
- package/dist/models/versionCache.js.map +1 -0
- package/dist/services/effectivenessService.js +131 -0
- package/dist/services/effectivenessService.js.map +1 -0
- package/dist/services/exportService.js +152 -0
- package/dist/services/exportService.js.map +1 -0
- package/dist/services/migrationService.js +90 -0
- package/dist/services/migrationService.js.map +1 -0
- package/dist/services/openaiClient.js +412 -0
- package/dist/services/openaiClient.js.map +1 -0
- package/dist/services/promptService.js +467 -0
- package/dist/services/promptService.js.map +1 -0
- package/dist/services/rateLimitService.js +108 -0
- package/dist/services/rateLimitService.js.map +1 -0
- package/dist/services/telemetryService.js +173 -0
- package/dist/services/telemetryService.js.map +1 -0
- package/dist/services/updateService.js +388 -0
- package/dist/services/updateService.js.map +1 -0
- package/dist/services/versionService.js +289 -0
- package/dist/services/versionService.js.map +1 -0
- package/dist/types/hydrooj.js +7 -0
- package/dist/types/hydrooj.js.map +1 -0
- package/dist/types/mongo.js +7 -0
- package/dist/types/mongo.js.map +1 -0
- package/dist/utils/domainHelper.js +16 -0
- package/dist/utils/domainHelper.js.map +1 -0
- package/dist/utils/mongo.js +16 -0
- package/dist/utils/mongo.js.map +1 -0
- package/frontend/admin/ConfigPanel.tsx +1379 -0
- package/frontend/admin/VersionBadge.tsx +655 -0
- package/frontend/ai_helper.page.tsx +52 -0
- package/frontend/ai_helper_admin_config.page.tsx +47 -0
- package/frontend/ai_helper_analytics.page.tsx +52 -0
- package/frontend/ai_helper_conversation_detail.page.tsx +55 -0
- package/frontend/ai_helper_conversations.page.tsx +56 -0
- package/frontend/components/AIHelperDashboard.tsx +125 -0
- package/frontend/problem_detail.page.tsx +395 -0
- package/frontend/student/AIAssistantPanel.tsx +1839 -0
- package/frontend/student/AIChatPanel.tsx +944 -0
- package/frontend/student/ThreeColumnLayout.tsx +240 -0
- package/frontend/teacher/AnalyticsPage.tsx +614 -0
- package/frontend/teacher/ConversationDetail.tsx +504 -0
- package/frontend/teacher/ConversationList.tsx +568 -0
- package/frontend/teacher/ExportDialog.tsx +291 -0
- package/frontend/test.page.ts +88 -0
- package/frontend/utils/domainUtils.ts +37 -0
- package/index.js +6 -0
- package/jest.config.js +17 -0
- package/lib/mongodb.ts +60 -0
- package/locales/en.yaml +4 -0
- package/locales/zh.yaml +77 -0
- package/package.json +48 -0
- package/public/.gitkeep +0 -0
- package/public/README.md +1 -0
- package/src/__tests__/__mocks__/hydrooj.ts +27 -0
- package/src/__tests__/services/exportService.test.ts +156 -0
- package/src/__tests__/services/promptService.test.ts +141 -0
- package/src/__tests__/services/rateLimitService.test.ts +145 -0
- package/src/__tests__/utils/domainHelper.test.ts +47 -0
- package/src/constants/jailbreakRules.ts +28 -0
- package/src/constants/permissions.ts +43 -0
- package/src/handlers/adminConfigHandler.ts +363 -0
- package/src/handlers/adminHandler.ts +427 -0
- package/src/handlers/analyticsHandler.ts +517 -0
- package/src/handlers/dashboardHandler.ts +20 -0
- package/src/handlers/exportHandler.ts +134 -0
- package/src/handlers/studentHandler.ts +483 -0
- package/src/handlers/teacherHandler.ts +317 -0
- package/src/handlers/testHandler.ts +25 -0
- package/src/handlers/updateHandler.ts +85 -0
- package/src/handlers/versionHandler.ts +62 -0
- package/src/index.ts +198 -0
- package/src/lib/crypto.ts +125 -0
- package/src/lib/httpHelpers.ts +75 -0
- package/src/lib/queryHelpers.ts +108 -0
- package/src/models/aiConfig.ts +375 -0
- package/src/models/conversation.ts +328 -0
- package/src/models/jailbreakLog.ts +108 -0
- package/src/models/message.ts +246 -0
- package/src/models/pluginInstall.ts +138 -0
- package/src/models/rateLimitRecord.ts +81 -0
- package/src/models/versionCache.ts +127 -0
- package/src/services/effectivenessService.ts +161 -0
- package/src/services/exportService.ts +191 -0
- package/src/services/migrationService.ts +111 -0
- package/src/services/openaiClient.ts +548 -0
- package/src/services/promptService.ts +573 -0
- package/src/services/rateLimitService.ts +122 -0
- package/src/services/telemetryService.ts +218 -0
- package/src/services/updateService.ts +441 -0
- package/src/services/versionService.ts +351 -0
- package/src/types/hydrooj-extensions.d.ts +55 -0
- package/src/types/hydrooj.ts +38 -0
- package/src/types/mongo.ts +23 -0
- package/src/utils/domainHelper.ts +16 -0
- package/src/utils/mongo.ts +15 -0
- package/templates/ai-helper/admin_config.html +8 -0
- package/templates/ai-helper/analytics.html +8 -0
- package/templates/ai-helper/dashboard.html +8 -0
- package/templates/ai-helper/teacher_conversation_detail.html +8 -0
- package/templates/ai-helper/teacher_conversations.html +8 -0
- package/tests/integration/README.md +86 -0
- package/tests/integration/curl/01-admin-config-set.sh +56 -0
- package/tests/integration/curl/02-student-chat.sh +67 -0
- package/tests/integration/curl/03-teacher-list.sh +52 -0
- package/tests/integration/curl/04-teacher-detail.sh +37 -0
- package/tests/integration/curl/05-export.sh +49 -0
- package/tests/integration/curl/06-analytics.sh +43 -0
- package/tests/performance/README.md +85 -0
- package/tests/performance/data/prepare-sample-data.md +57 -0
- package/tests/performance/scripts/export-csv-k6.js +41 -0
- package/tests/performance/scripts/student-chat-k6.js +44 -0
- package/tests/performance/scripts/teacher-conversations-k6.js +33 -0
- package/tsconfig.json +42 -0
- package/tsconfig.vercel.json +27 -0
- package/types/hydrooj/index.d.ts +73 -0
- package/vercel.json +3 -0
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AI 配置页面 Handler
|
|
3
|
+
* 处理管理员配置页面的渲染和配置请求
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Handler, PRIV } from 'hydrooj';
|
|
7
|
+
import { AIConfig, AIConfigModel, APIEndpoint, SelectedModel } from '../models/aiConfig';
|
|
8
|
+
import { decrypt, encrypt, maskApiKey } from '../lib/crypto';
|
|
9
|
+
import { builtinJailbreakPatternSources } from '../constants/jailbreakRules';
|
|
10
|
+
import { JailbreakLogModel } from '../models/jailbreakLog';
|
|
11
|
+
import type { JailbreakLog } from '../models/jailbreakLog';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 更新配置请求接口(兼容旧版 + 新版多端点)
|
|
15
|
+
*/
|
|
16
|
+
interface UpdateConfigRequest {
|
|
17
|
+
// 旧版单端点字段(向后兼容)
|
|
18
|
+
apiBaseUrl?: string;
|
|
19
|
+
modelName?: string;
|
|
20
|
+
apiKey?: string;
|
|
21
|
+
// 新版多端点字段
|
|
22
|
+
endpoints?: Array<{
|
|
23
|
+
id?: string;
|
|
24
|
+
name: string;
|
|
25
|
+
apiBaseUrl: string;
|
|
26
|
+
apiKey?: string; // 明文 API Key,仅新建或更新时传入
|
|
27
|
+
models?: string[];
|
|
28
|
+
enabled?: boolean;
|
|
29
|
+
}>;
|
|
30
|
+
selectedModels?: SelectedModel[];
|
|
31
|
+
// 通用字段
|
|
32
|
+
rateLimitPerMinute?: number;
|
|
33
|
+
timeoutSeconds?: number;
|
|
34
|
+
systemPromptTemplate?: string;
|
|
35
|
+
extraJailbreakPatternsText?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* AdminConfigHandler - AI 配置页面
|
|
40
|
+
* GET /ai-helper/admin/config
|
|
41
|
+
*/
|
|
42
|
+
export class AdminConfigHandler extends Handler {
|
|
43
|
+
async get() {
|
|
44
|
+
try {
|
|
45
|
+
// 当 Accept 包含 text/html 时才渲染页面;其他情况返回 JSON 配置
|
|
46
|
+
const accept = this.request.headers.accept || '';
|
|
47
|
+
const prefersHtml = accept.includes('text/html');
|
|
48
|
+
|
|
49
|
+
if (prefersHtml) {
|
|
50
|
+
this.response.template = 'ai-helper/admin_config.html';
|
|
51
|
+
this.response.body = {};
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const aiConfigModel: AIConfigModel = this.ctx.get('aiConfigModel');
|
|
56
|
+
const jailbreakLogModel: JailbreakLogModel = this.ctx.get('jailbreakLogModel');
|
|
57
|
+
|
|
58
|
+
// 解析分页参数
|
|
59
|
+
const page = parseInt(String(this.request.query.page || '1'), 10) || 1;
|
|
60
|
+
const limit = parseInt(String(this.request.query.limit || '20'), 10) || 20;
|
|
61
|
+
|
|
62
|
+
const config = await aiConfigModel.getConfig();
|
|
63
|
+
const logResult = await jailbreakLogModel.listWithPagination(page, limit);
|
|
64
|
+
|
|
65
|
+
if (!config) {
|
|
66
|
+
this.response.body = {
|
|
67
|
+
config: null,
|
|
68
|
+
builtinJailbreakPatterns: builtinJailbreakPatternSources,
|
|
69
|
+
jailbreakLogs: {
|
|
70
|
+
logs: logResult.logs.map(formatJailbreakLog),
|
|
71
|
+
total: logResult.total,
|
|
72
|
+
page: logResult.page,
|
|
73
|
+
totalPages: logResult.totalPages
|
|
74
|
+
},
|
|
75
|
+
// 兼容旧前端
|
|
76
|
+
recentJailbreakLogs: logResult.logs.map(formatJailbreakLog)
|
|
77
|
+
};
|
|
78
|
+
this.response.type = 'application/json';
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// 处理端点的 API Key 脱敏
|
|
83
|
+
const endpointsWithMaskedKeys = (config.endpoints || []).map(ep => {
|
|
84
|
+
let apiKeyMasked = '';
|
|
85
|
+
let hasApiKey = false;
|
|
86
|
+
try {
|
|
87
|
+
if (ep.apiKeyEncrypted) {
|
|
88
|
+
const apiKeyPlain = decrypt(ep.apiKeyEncrypted);
|
|
89
|
+
apiKeyMasked = maskApiKey(apiKeyPlain);
|
|
90
|
+
hasApiKey = true;
|
|
91
|
+
}
|
|
92
|
+
} catch {
|
|
93
|
+
hasApiKey = false;
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
id: ep.id,
|
|
97
|
+
name: ep.name,
|
|
98
|
+
apiBaseUrl: ep.apiBaseUrl,
|
|
99
|
+
models: ep.models || [],
|
|
100
|
+
modelsLastFetched: ep.modelsLastFetched?.toISOString(),
|
|
101
|
+
enabled: ep.enabled,
|
|
102
|
+
apiKeyMasked,
|
|
103
|
+
hasApiKey,
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// 兼容旧版:处理旧版单 API Key
|
|
108
|
+
let apiKeyMasked = '';
|
|
109
|
+
let hasApiKey = false;
|
|
110
|
+
try {
|
|
111
|
+
if (config.apiKeyEncrypted) {
|
|
112
|
+
const apiKeyPlain = decrypt(config.apiKeyEncrypted);
|
|
113
|
+
apiKeyMasked = maskApiKey(apiKeyPlain);
|
|
114
|
+
hasApiKey = true;
|
|
115
|
+
}
|
|
116
|
+
} catch (err) {
|
|
117
|
+
console.error('[AdminConfigHandler] API Key 解密失败:', err);
|
|
118
|
+
hasApiKey = false;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
this.response.body = {
|
|
122
|
+
config: {
|
|
123
|
+
// 新版多端点字段
|
|
124
|
+
endpoints: endpointsWithMaskedKeys,
|
|
125
|
+
selectedModels: config.selectedModels || [],
|
|
126
|
+
// 旧版字段(向后兼容)
|
|
127
|
+
apiBaseUrl: config.apiBaseUrl,
|
|
128
|
+
modelName: config.modelName,
|
|
129
|
+
rateLimitPerMinute: config.rateLimitPerMinute,
|
|
130
|
+
timeoutSeconds: config.timeoutSeconds,
|
|
131
|
+
systemPromptTemplate: config.systemPromptTemplate,
|
|
132
|
+
extraJailbreakPatternsText: config.extraJailbreakPatternsText || '',
|
|
133
|
+
apiKeyMasked,
|
|
134
|
+
hasApiKey,
|
|
135
|
+
updatedAt: config.updatedAt.toISOString()
|
|
136
|
+
},
|
|
137
|
+
builtinJailbreakPatterns: builtinJailbreakPatternSources,
|
|
138
|
+
jailbreakLogs: {
|
|
139
|
+
logs: logResult.logs.map(formatJailbreakLog),
|
|
140
|
+
total: logResult.total,
|
|
141
|
+
page: logResult.page,
|
|
142
|
+
totalPages: logResult.totalPages
|
|
143
|
+
},
|
|
144
|
+
// 兼容旧前端
|
|
145
|
+
recentJailbreakLogs: logResult.logs.map(formatJailbreakLog)
|
|
146
|
+
};
|
|
147
|
+
this.response.type = 'application/json';
|
|
148
|
+
} catch (err) {
|
|
149
|
+
console.error('[AI Helper] AdminConfigHandler error:', err);
|
|
150
|
+
this.response.status = 500;
|
|
151
|
+
this.response.body = { error: err instanceof Error ? err.message : '服务器内部错误' };
|
|
152
|
+
this.response.type = 'application/json';
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* PUT /ai-helper/admin/config
|
|
158
|
+
* 更新配置(支持旧版单端点和新版多端点)
|
|
159
|
+
*/
|
|
160
|
+
async put() {
|
|
161
|
+
try {
|
|
162
|
+
const aiConfigModel: AIConfigModel = this.ctx.get('aiConfigModel');
|
|
163
|
+
const body = this.request.body as UpdateConfigRequest;
|
|
164
|
+
|
|
165
|
+
const partial: Partial<Omit<AIConfig, '_id' | 'updatedAt'>> = {};
|
|
166
|
+
|
|
167
|
+
// 处理新版多端点配置
|
|
168
|
+
if (body.endpoints !== undefined) {
|
|
169
|
+
const existingConfig = await aiConfigModel.getConfig();
|
|
170
|
+
const existingEndpoints = existingConfig?.endpoints || [];
|
|
171
|
+
|
|
172
|
+
const newEndpoints: APIEndpoint[] = [];
|
|
173
|
+
for (const ep of body.endpoints) {
|
|
174
|
+
// 查找是否有现有端点
|
|
175
|
+
const existing = ep.id ? existingEndpoints.find(e => e.id === ep.id) : null;
|
|
176
|
+
|
|
177
|
+
let apiKeyEncrypted = existing?.apiKeyEncrypted || '';
|
|
178
|
+
|
|
179
|
+
// 如果提供了新的 API Key,加密它
|
|
180
|
+
if (ep.apiKey && ep.apiKey.trim()) {
|
|
181
|
+
try {
|
|
182
|
+
apiKeyEncrypted = encrypt(ep.apiKey.trim());
|
|
183
|
+
} catch (err) {
|
|
184
|
+
this.response.status = 500;
|
|
185
|
+
this.response.body = {
|
|
186
|
+
error: `端点 "${ep.name}" 的 API Key 加密失败`
|
|
187
|
+
};
|
|
188
|
+
this.response.type = 'application/json';
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
newEndpoints.push({
|
|
194
|
+
id: ep.id || (await import('crypto')).randomUUID(),
|
|
195
|
+
name: ep.name,
|
|
196
|
+
apiBaseUrl: ep.apiBaseUrl,
|
|
197
|
+
apiKeyEncrypted,
|
|
198
|
+
models: ep.models || existing?.models || [],
|
|
199
|
+
modelsLastFetched: existing?.modelsLastFetched,
|
|
200
|
+
enabled: ep.enabled !== undefined ? ep.enabled : true,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
partial.endpoints = newEndpoints;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// 处理选中的模型
|
|
207
|
+
if (body.selectedModels !== undefined) {
|
|
208
|
+
partial.selectedModels = body.selectedModels;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// 旧版单端点字段(向后兼容)
|
|
212
|
+
if (body.apiBaseUrl !== undefined) {
|
|
213
|
+
partial.apiBaseUrl = body.apiBaseUrl.trim();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (body.modelName !== undefined) {
|
|
217
|
+
partial.modelName = body.modelName.trim();
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (body.rateLimitPerMinute !== undefined) {
|
|
221
|
+
const rate = parseInt(String(body.rateLimitPerMinute), 10);
|
|
222
|
+
if (rate <= 0) {
|
|
223
|
+
this.response.status = 400;
|
|
224
|
+
this.response.body = { error: 'rateLimitPerMinute 必须大于 0' };
|
|
225
|
+
this.response.type = 'application/json';
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
partial.rateLimitPerMinute = rate;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (body.timeoutSeconds !== undefined) {
|
|
232
|
+
const timeout = parseInt(String(body.timeoutSeconds), 10);
|
|
233
|
+
if (timeout <= 0) {
|
|
234
|
+
this.response.status = 400;
|
|
235
|
+
this.response.body = { error: 'timeoutSeconds 必须大于 0' };
|
|
236
|
+
this.response.type = 'application/json';
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
partial.timeoutSeconds = timeout;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (body.systemPromptTemplate !== undefined) {
|
|
243
|
+
partial.systemPromptTemplate = body.systemPromptTemplate;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (body.extraJailbreakPatternsText !== undefined) {
|
|
247
|
+
partial.extraJailbreakPatternsText = body.extraJailbreakPatternsText;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// 旧版单 API Key(向后兼容)
|
|
251
|
+
if (body.apiKey !== undefined && body.apiKey !== '') {
|
|
252
|
+
try {
|
|
253
|
+
partial.apiKeyEncrypted = encrypt(body.apiKey.trim());
|
|
254
|
+
} catch (err) {
|
|
255
|
+
this.response.status = 500;
|
|
256
|
+
this.response.body = {
|
|
257
|
+
error: `API Key 加密失败: ${err instanceof Error ? err.message : String(err)}`
|
|
258
|
+
};
|
|
259
|
+
this.response.type = 'application/json';
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const jailbreakLogModel: JailbreakLogModel = this.ctx.get('jailbreakLogModel');
|
|
265
|
+
|
|
266
|
+
await aiConfigModel.updateConfig(partial);
|
|
267
|
+
|
|
268
|
+
const updatedConfig = await aiConfigModel.getConfig();
|
|
269
|
+
if (!updatedConfig) {
|
|
270
|
+
throw new Error('配置更新后读取失败');
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// 处理端点的 API Key 脱敏
|
|
274
|
+
const endpointsWithMaskedKeys = (updatedConfig.endpoints || []).map(ep => {
|
|
275
|
+
let apiKeyMasked = '';
|
|
276
|
+
let hasApiKey = false;
|
|
277
|
+
try {
|
|
278
|
+
if (ep.apiKeyEncrypted) {
|
|
279
|
+
const apiKeyPlain = decrypt(ep.apiKeyEncrypted);
|
|
280
|
+
apiKeyMasked = maskApiKey(apiKeyPlain);
|
|
281
|
+
hasApiKey = true;
|
|
282
|
+
}
|
|
283
|
+
} catch {
|
|
284
|
+
hasApiKey = false;
|
|
285
|
+
}
|
|
286
|
+
return {
|
|
287
|
+
id: ep.id,
|
|
288
|
+
name: ep.name,
|
|
289
|
+
apiBaseUrl: ep.apiBaseUrl,
|
|
290
|
+
models: ep.models || [],
|
|
291
|
+
modelsLastFetched: ep.modelsLastFetched?.toISOString(),
|
|
292
|
+
enabled: ep.enabled,
|
|
293
|
+
apiKeyMasked,
|
|
294
|
+
hasApiKey,
|
|
295
|
+
};
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
// 兼容旧版:处理单 API Key
|
|
299
|
+
let apiKeyMasked = '';
|
|
300
|
+
let hasApiKey = false;
|
|
301
|
+
try {
|
|
302
|
+
if (updatedConfig.apiKeyEncrypted) {
|
|
303
|
+
const apiKeyPlain = decrypt(updatedConfig.apiKeyEncrypted);
|
|
304
|
+
apiKeyMasked = maskApiKey(apiKeyPlain);
|
|
305
|
+
hasApiKey = true;
|
|
306
|
+
}
|
|
307
|
+
} catch (err) {
|
|
308
|
+
console.error('[AdminConfigHandler] API Key 解密失败:', err);
|
|
309
|
+
hasApiKey = false;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const logResult = await jailbreakLogModel.listWithPagination(1, 20);
|
|
313
|
+
|
|
314
|
+
this.response.body = {
|
|
315
|
+
config: {
|
|
316
|
+
endpoints: endpointsWithMaskedKeys,
|
|
317
|
+
selectedModels: updatedConfig.selectedModels || [],
|
|
318
|
+
apiBaseUrl: updatedConfig.apiBaseUrl,
|
|
319
|
+
modelName: updatedConfig.modelName,
|
|
320
|
+
rateLimitPerMinute: updatedConfig.rateLimitPerMinute,
|
|
321
|
+
timeoutSeconds: updatedConfig.timeoutSeconds,
|
|
322
|
+
systemPromptTemplate: updatedConfig.systemPromptTemplate,
|
|
323
|
+
extraJailbreakPatternsText: updatedConfig.extraJailbreakPatternsText || '',
|
|
324
|
+
apiKeyMasked,
|
|
325
|
+
hasApiKey,
|
|
326
|
+
updatedAt: updatedConfig.updatedAt.toISOString()
|
|
327
|
+
},
|
|
328
|
+
builtinJailbreakPatterns: builtinJailbreakPatternSources,
|
|
329
|
+
jailbreakLogs: {
|
|
330
|
+
logs: logResult.logs.map(formatJailbreakLog),
|
|
331
|
+
total: logResult.total,
|
|
332
|
+
page: logResult.page,
|
|
333
|
+
totalPages: logResult.totalPages
|
|
334
|
+
},
|
|
335
|
+
recentJailbreakLogs: logResult.logs.map(formatJailbreakLog)
|
|
336
|
+
};
|
|
337
|
+
this.response.type = 'application/json';
|
|
338
|
+
} catch (err) {
|
|
339
|
+
console.error('[AdminConfigHandler] 更新配置失败:', err);
|
|
340
|
+
this.response.status = 500;
|
|
341
|
+
this.response.body = {
|
|
342
|
+
error: err instanceof Error ? err.message : '更新配置失败'
|
|
343
|
+
};
|
|
344
|
+
this.response.type = 'application/json';
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// 导出路由权限配置(使用系统管理员权限)
|
|
350
|
+
export const AdminConfigHandlerPriv = PRIV.PRIV_EDIT_SYSTEM;
|
|
351
|
+
|
|
352
|
+
function formatJailbreakLog(log: JailbreakLog) {
|
|
353
|
+
return {
|
|
354
|
+
id: log._id.toHexString(),
|
|
355
|
+
userId: log.userId,
|
|
356
|
+
problemId: log.problemId,
|
|
357
|
+
conversationId: log.conversationId ? log.conversationId.toHexString() : undefined,
|
|
358
|
+
questionType: log.questionType,
|
|
359
|
+
matchedPattern: log.matchedPattern,
|
|
360
|
+
matchedText: log.matchedText,
|
|
361
|
+
createdAt: log.createdAt.toISOString()
|
|
362
|
+
};
|
|
363
|
+
}
|