mirror of
https://github.com/danswer-ai/danswer.git
synced 2025-10-10 05:05:34 +02:00
Shared filter utility for clarity (#3270)
* shared filter util * clearer comment
This commit is contained in:
@@ -11,6 +11,7 @@ import {
|
|||||||
classifyAssistants,
|
classifyAssistants,
|
||||||
orderAssistantsForUser,
|
orderAssistantsForUser,
|
||||||
getUserCreatedAssistants,
|
getUserCreatedAssistants,
|
||||||
|
filterAssistants,
|
||||||
} from "@/lib/assistants/utils";
|
} from "@/lib/assistants/utils";
|
||||||
import { useUser } from "../user/UserProvider";
|
import { useUser } from "../user/UserProvider";
|
||||||
|
|
||||||
@@ -145,22 +146,13 @@ export const AssistantsProvider: React.FC<{
|
|||||||
if (!response.ok) throw new Error("Failed to fetch assistants");
|
if (!response.ok) throw new Error("Failed to fetch assistants");
|
||||||
let assistants: Persona[] = await response.json();
|
let assistants: Persona[] = await response.json();
|
||||||
|
|
||||||
if (!hasImageCompatibleModel) {
|
let filteredAssistants = filterAssistants(
|
||||||
assistants = assistants.filter(
|
assistants,
|
||||||
(assistant) =>
|
hasAnyConnectors,
|
||||||
!assistant.tools.some(
|
hasImageCompatibleModel
|
||||||
(tool) => tool.in_code_tool_id === "ImageGenerationTool"
|
);
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!hasAnyConnectors) {
|
setAssistants(filteredAssistants);
|
||||||
assistants = assistants.filter(
|
|
||||||
(assistant) => assistant.num_chunks === 0
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
setAssistants(assistants);
|
|
||||||
|
|
||||||
// Fetch and update allAssistants for admins and curators
|
// Fetch and update allAssistants for admins and curators
|
||||||
await fetchPersonas();
|
await fetchPersonas();
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
import { Persona } from "@/app/admin/assistants/interfaces";
|
import { Persona } from "@/app/admin/assistants/interfaces";
|
||||||
import { User } from "../types";
|
import { User } from "../types";
|
||||||
import { checkUserIsNoAuthUser } from "../user";
|
import { checkUserIsNoAuthUser } from "../user";
|
||||||
|
import { personaComparator } from "@/app/admin/assistants/lib";
|
||||||
|
|
||||||
export function checkUserOwnsAssistant(user: User | null, assistant: Persona) {
|
export function checkUserOwnsAssistant(user: User | null, assistant: Persona) {
|
||||||
return checkUserIdOwnsAssistant(user?.id, assistant);
|
return checkUserIdOwnsAssistant(user?.id, assistant);
|
||||||
@@ -117,3 +118,31 @@ export function getUserCreatedAssistants(
|
|||||||
checkUserOwnsAssistant(user, assistant)
|
checkUserOwnsAssistant(user, assistant)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Filter assistants based on connector status, image compatibility and visibility
|
||||||
|
export function filterAssistants(
|
||||||
|
assistants: Persona[],
|
||||||
|
hasAnyConnectors: boolean,
|
||||||
|
hasImageCompatibleModel: boolean
|
||||||
|
): Persona[] {
|
||||||
|
let filteredAssistants = assistants.filter(
|
||||||
|
(assistant) => assistant.is_visible
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!hasAnyConnectors) {
|
||||||
|
filteredAssistants = filteredAssistants.filter(
|
||||||
|
(assistant) => assistant.num_chunks === 0
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasImageCompatibleModel) {
|
||||||
|
filteredAssistants = filteredAssistants.filter(
|
||||||
|
(assistant) =>
|
||||||
|
!assistant.tools.some(
|
||||||
|
(tool) => tool.in_code_tool_id === "ImageGenerationTool"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return filteredAssistants.sort(personaComparator);
|
||||||
|
}
|
||||||
|
@@ -4,6 +4,7 @@ import { fetchLLMProvidersSS } from "@/lib/llm/fetchLLMs";
|
|||||||
import { personaComparator } from "@/app/admin/assistants/lib";
|
import { personaComparator } from "@/app/admin/assistants/lib";
|
||||||
import { fetchAssistantsSS } from "../assistants/fetchAssistantsSS";
|
import { fetchAssistantsSS } from "../assistants/fetchAssistantsSS";
|
||||||
import { checkLLMSupportsImageInput } from "../llm/utils";
|
import { checkLLMSupportsImageInput } from "../llm/utils";
|
||||||
|
import { filterAssistants } from "../assistants/utils";
|
||||||
|
|
||||||
interface AssistantData {
|
interface AssistantData {
|
||||||
assistants: Persona[];
|
assistants: Persona[];
|
||||||
@@ -39,42 +40,21 @@ export async function fetchAssistantData(): Promise<AssistantData> {
|
|||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Process visible assistants
|
|
||||||
let filteredAssistants = assistants.filter(
|
|
||||||
(assistant) => assistant.is_visible
|
|
||||||
);
|
|
||||||
|
|
||||||
// Process connector status
|
|
||||||
const hasAnyConnectors = ccPairsResponse?.ok
|
const hasAnyConnectors = ccPairsResponse?.ok
|
||||||
? (await ccPairsResponse.json()).length > 0
|
? (await ccPairsResponse.json()).length > 0
|
||||||
: false;
|
: 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(
|
const hasImageCompatibleModel = llmProviders.some(
|
||||||
(provider) =>
|
(provider) =>
|
||||||
provider.provider === "openai" ||
|
provider.provider === "openai" ||
|
||||||
provider.model_names.some((model) => checkLLMSupportsImageInput(model))
|
provider.model_names.some((model) => checkLLMSupportsImageInput(model))
|
||||||
);
|
);
|
||||||
|
|
||||||
// Filter out image generation tools if no compatible model
|
let filteredAssistants = filterAssistants(
|
||||||
if (!hasImageCompatibleModel) {
|
assistants,
|
||||||
filteredAssistants = filteredAssistants.filter(
|
hasAnyConnectors,
|
||||||
(assistant) =>
|
hasImageCompatibleModel
|
||||||
!assistant.tools.some(
|
);
|
||||||
(tool) => tool.in_code_tool_id === "ImageGenerationTool"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
assistants: filteredAssistants,
|
assistants: filteredAssistants,
|
||||||
|
Reference in New Issue
Block a user