fix: bound AI analysis provider latency

This commit is contained in:
2026-07-09 01:11:29 -07:00
parent a8776f9d79
commit 153c501cf7
6 changed files with 173 additions and 7 deletions

View File

@@ -26,13 +26,22 @@ import type { AuthContext } from "@/modules/core/types";
type ServiceResult<T> = { success: true; data: T } | { success: false; reason: string; status: number };
type AnalysisRow = typeof elderAiAnalyses.$inferSelect;
function createChatModel(config: { baseUrl: string; apiKey: string; chatModel: string }) {
function createChatModel(config: {
baseUrl: string;
apiKey: string;
chatModel: string;
requestTimeoutMs: number;
maxTokens: number;
maxRetries: number;
}) {
const configuration = config.baseUrl ? { baseURL: config.baseUrl } : undefined;
return new ChatOpenAI({
apiKey: config.apiKey,
model: config.chatModel,
maxTokens: 1800,
maxTokens: config.maxTokens,
maxRetries: config.maxRetries,
temperature: 0.2,
timeout: config.requestTimeoutMs,
configuration,
});
}
@@ -42,9 +51,22 @@ function toIsoString(value: Date): string {
}
function mapErrorCategory(error: unknown): AiErrorCategory {
if (error instanceof Error && error.name.toLowerCase().includes("timeout")) {
if (!(error instanceof Error)) {
return "provider_error";
}
const name = error.name.toLowerCase();
const message = error.message.toLowerCase();
if (
name.includes("abort") ||
name.includes("timeout") ||
message.includes("abort") ||
message.includes("timeout") ||
message.includes("timed out")
) {
return "timeout";
}
return "provider_error";
}