mirror of
https://github.com/danswer-ai/danswer.git
synced 2025-04-08 11:58:34 +02:00
fail gracefully on provider fetch (#2981)
This commit is contained in:
parent
1201ed5ac0
commit
aafcf7af55
@ -1,5 +1,4 @@
|
||||
import { fetchSS } from "@/lib/utilsSS";
|
||||
import { CCPairBasicInfo } from "@/lib/types";
|
||||
import { Persona } from "@/app/admin/assistants/interfaces";
|
||||
import { fetchLLMProvidersSS } from "@/lib/llm/fetchLLMs";
|
||||
import { personaComparator } from "@/app/admin/assistants/lib";
|
||||
@ -12,58 +11,77 @@ interface AssistantData {
|
||||
hasImageCompatibleModel: boolean;
|
||||
}
|
||||
export async function fetchAssistantData(): Promise<AssistantData> {
|
||||
const [assistants, assistantsFetchError] = await fetchAssistantsSS();
|
||||
const ccPairsResponse = await fetchSS("/manage/indexing-status");
|
||||
|
||||
let ccPairs: CCPairBasicInfo[] = [];
|
||||
if (ccPairsResponse?.ok) {
|
||||
ccPairs = await ccPairsResponse.json();
|
||||
} else {
|
||||
console.log(`Failed to fetch connectors - ${ccPairsResponse?.status}`);
|
||||
}
|
||||
|
||||
const hasAnyConnectors = ccPairs.length > 0;
|
||||
|
||||
// if no connectors are setup, only show personas that are pure
|
||||
// passthrough and don't do any retrieval
|
||||
let filteredAssistants = assistants;
|
||||
if (assistantsFetchError) {
|
||||
console.log(`Failed to fetch assistants - ${assistantsFetchError}`);
|
||||
}
|
||||
|
||||
// remove those marked as hidden by an admin
|
||||
filteredAssistants = filteredAssistants.filter(
|
||||
(assistant) => assistant.is_visible
|
||||
);
|
||||
|
||||
if (!hasAnyConnectors) {
|
||||
filteredAssistants = filteredAssistants.filter(
|
||||
(assistant) => assistant.num_chunks === 0
|
||||
);
|
||||
}
|
||||
|
||||
// sort them in priority order
|
||||
filteredAssistants.sort(personaComparator);
|
||||
|
||||
const llmProviders = await fetchLLMProvidersSS();
|
||||
const hasImageCompatibleModel = llmProviders.some(
|
||||
(provider) =>
|
||||
provider.provider === "openai" ||
|
||||
provider.model_names.some((model) => checkLLMSupportsImageInput(model))
|
||||
);
|
||||
|
||||
if (!hasImageCompatibleModel) {
|
||||
filteredAssistants = filteredAssistants.filter(
|
||||
(assistant) =>
|
||||
!assistant.tools.some(
|
||||
(tool) => tool.in_code_tool_id === "ImageGenerationTool"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
assistants: filteredAssistants,
|
||||
hasAnyConnectors,
|
||||
hasImageCompatibleModel,
|
||||
// Default state if anything fails
|
||||
const defaultState: AssistantData = {
|
||||
assistants: [],
|
||||
hasAnyConnectors: false,
|
||||
hasImageCompatibleModel: false,
|
||||
};
|
||||
|
||||
try {
|
||||
// Fetch core assistants data first
|
||||
const [assistants, assistantsFetchError] = await fetchAssistantsSS();
|
||||
if (assistantsFetchError) {
|
||||
console.error(`Failed to fetch assistants - ${assistantsFetchError}`);
|
||||
return defaultState;
|
||||
}
|
||||
|
||||
// Parallel fetch of additional data
|
||||
const [ccPairsResponse, llmProviders] = await Promise.all([
|
||||
fetchSS("/manage/indexing-status").catch((error) => {
|
||||
console.error("Failed to fetch connectors:", error);
|
||||
return null;
|
||||
}),
|
||||
fetchLLMProvidersSS().catch((error) => {
|
||||
console.error("Failed to fetch LLM providers:", error);
|
||||
return [];
|
||||
}),
|
||||
]);
|
||||
|
||||
// Process visible assistants
|
||||
let filteredAssistants = assistants.filter(
|
||||
(assistant) => assistant.is_visible
|
||||
);
|
||||
|
||||
// Process connector status
|
||||
const hasAnyConnectors = ccPairsResponse?.ok
|
||||
? (await ccPairsResponse.json()).length > 0
|
||||
: false;
|
||||
|
||||
// Filter assistants based on connector status
|
||||
if (!hasAnyConnectors) {
|
||||
filteredAssistants = filteredAssistants.filter(
|
||||
(assistant) => assistant.num_chunks === 0
|
||||
);
|
||||
}
|
||||
|
||||
// Sort assistants
|
||||
filteredAssistants.sort(personaComparator);
|
||||
|
||||
// Check for image-compatible models
|
||||
const hasImageCompatibleModel = llmProviders.some(
|
||||
(provider) =>
|
||||
provider.provider === "openai" ||
|
||||
provider.model_names.some((model) => checkLLMSupportsImageInput(model))
|
||||
);
|
||||
|
||||
// Filter out image generation tools if no compatible model
|
||||
if (!hasImageCompatibleModel) {
|
||||
filteredAssistants = filteredAssistants.filter(
|
||||
(assistant) =>
|
||||
!assistant.tools.some(
|
||||
(tool) => tool.in_code_tool_id === "ImageGenerationTool"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
assistants: filteredAssistants,
|
||||
hasAnyConnectors,
|
||||
hasImageCompatibleModel,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Unexpected error in fetchAssistantData:", error);
|
||||
return defaultState;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user