Skip to content

Commit fc44c82

Browse files
committed
重构 AI 模型和翻译处理程序,使其使用安全的 API 请求
- 更新了 `loadAiModelRegistry` 和 `loadTranslationModelRegistry` 函数,使其使用 `secureApiRequest` 获取 AI 模型和翻译模型。 - 为 AI 模型和翻译模型引入了新的处理程序函数,确保请求和响应的安全处理。 - 实现了从 JSON 文件加载 AI 模型和翻译模型的配置。 - 为请求处理添加了全面的日志记录,包括 Redis 连接和无效请求的错误处理。 - 通过改进提示和重试机制,增强了翻译和摘要功能。 - 建立了用于提取响应和解析翻译的实用函数。
1 parent 9c59922 commit fc44c82

15 files changed

Lines changed: 754 additions & 934 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ npm-debug.log*
1212
yarn-debug.log*
1313
yarn-error.log*
1414
pnpm-debug.log*
15-
15+
/server/logs
1616
# environment variables
1717
.env
1818
.env.production

server/index.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ const routes = {
129129
'POST:/api/ai/summary': handleSummary,
130130
'POST:/api/ai/secure': handleSecureAi,
131131
'POST:/api/ai/translate': handleTranslate,
132-
'GET:/api/ai/translation-models': handleTranslationModels,
133-
'GET:/api/ai/models': handleAiModels,
132+
'POST:/api/ai/translation-models': handleTranslationModels,
133+
'POST:/api/ai/models': handleAiModels,
134134
}
135135

136136
const server = createServer(async (req, res) => {

server/src/core/redis.mjs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,10 @@ export function createRedisCache({ log, url, prefix = 'mahiro:', enabled = true
136136
// 尝试连接 Redis
137137
try {
138138
client = new Redis(redisUrl, {
139-
maxRetriesPerRequest: 2,
139+
maxRetriesPerRequest: 1,
140140
retryStrategy(times) {
141-
// 重连退避策略:500ms → 1s → 2s → 最大 10s
142-
const delay = Math.min(times * 500, 10_000)
143-
log('WARN', t('redis_reconnecting'), { attempt: times, delayMs: delay })
144-
return delay
141+
log('WARN', '无法连接到 Redis,放弃重连并降级为纯内存缓存。')
142+
return null // 连不上直接不启用,不进行无限重连
145143
},
146144
lazyConnect: false,
147145
enableReadyCheck: true,

0 commit comments

Comments
 (0)