mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-10-25 17:54:34 +08:00
fix(ai): harden validation and db handle reuse
This commit is contained in:
1 parent
eea1dad907
commit
40d7d9e840
3 files changed
+6
-19
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()
|
||||
}
|
||||
return filterMessagesWithContext(db, { keywords, timeFilter, senderIds, contextSize, page, pageSize })
|
||||
})
|
||||
|
||||
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()
|
||||
}
|
||||
return getMultipleSessionsMessages(db, chatSessionIds, page, pageSize)
|
||||
})
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user