fix(ai): harden validation and db handle reuse

This commit is contained in:
digua committed 2026-06-02 15:41:21 +08:00
1 parent eea1dad907
commit 40d7d9e840
3 files changed
+4 -17

No files matched your search

@@ -18,11 +18,7 @@ export function registerAiFilterRoutes(server: FastifyInstance, ctx: HttpRouteCo
const db = ctx.dbManager.open(sessionId)
if (!db) return reply.code(404).send({ error: `Session not found: ${sessionId}` })
try {
return filterMessagesWithContext(db, { keywords, timeFilter, senderIds, contextSize, page, pageSize })
} finally {
db.close()
}
})
server.post<{
@@ -37,10 +33,6 @@ export function registerAiFilterRoutes(server: FastifyInstance, ctx: HttpRouteCo
const db = ctx.dbManager.open(sessionId)
if (!db) return reply.code(404).send({ error: `Session not found: ${sessionId}` })
try {
return getMultipleSessionsMessages(db, chatSessionIds, page, pageSize)
} finally {
db.close()
}
})
}
@@ -82,11 +82,6 @@ export function registerAiToolRoutes(server: FastifyInstance, ctx: HttpRouteCont
return { success: false, error: String(error) }
} finally {
activeToolTests.delete(testId)
try {
db.close()
} catch {
/* ignore close errors */
}
}
})
@@ -357,8 +357,7 @@ export function useAIConfigForm(props: {
const canFetchModels = computed(() => {
if (effectiveApiFormat.value === 'anthropic-messages') return false
const baseUrl = formData.value.baseUrl || currentProviderDef.value?.defaultBaseUrl || ''
const hasKey =
formData.value.apiKey.trim() || isLocalMode.value || canReuseStoredKey.value
const hasKey = formData.value.apiKey.trim() || isLocalMode.value || canReuseStoredKey.value
return !!(baseUrl.trim() && hasKey)
})
@@ -569,7 +568,8 @@ export function useAIConfigForm(props: {
// ============ 验证 ============
async function validateKey() {
const { provider, apiKey, baseUrl } = formData.value
const { provider, baseUrl } = formData.value
const apiKey = formData.value.apiKey.trim()
const canReuse = canReuseStoredKey.value
if (!isPresetMode.value) {
@@ -589,7 +589,7 @@ export function useAIConfigForm(props: {
const configId = !apiKey && canReuse ? props.config.value?.id : undefined
try {
const testApiKey = apiKey || 'sk-no-key-required'
const testApiKey = !apiKey && canReuse ? '' : apiKey || 'sk-no-key-required'
const result = await useLLMService().validateApiKey(
provider || 'openai-compatible',
testApiKey,