Shared filter utility for clarity (#3270)

* shared filter util

* clearer comment
This commit is contained in:
pablodanswer
2024-12-03 11:30:42 -08:00
committed by GitHub
parent 0a685bda7d
commit 66f47d294c
3 changed files with 42 additions and 41 deletions

View File

@@ -11,6 +11,7 @@ import {
classifyAssistants,
orderAssistantsForUser,
getUserCreatedAssistants,
filterAssistants,
} from "@/lib/assistants/utils";
import { useUser } from "../user/UserProvider";
@@ -145,22 +146,13 @@ export const AssistantsProvider: React.FC<{
if (!response.ok) throw new Error("Failed to fetch assistants");
let assistants: Persona[] = await response.json();
if (!hasImageCompatibleModel) {
assistants = assistants.filter(
(assistant) =>
!assistant.tools.some(
(tool) => tool.in_code_tool_id === "ImageGenerationTool"
)
);
}
let filteredAssistants = filterAssistants(
assistants,
hasAnyConnectors,
hasImageCompatibleModel
);
if (!hasAnyConnectors) {
assistants = assistants.filter(
(assistant) => assistant.num_chunks === 0
);
}
setAssistants(assistants);
setAssistants(filteredAssistants);
// Fetch and update allAssistants for admins and curators
await fetchPersonas();

View File

@@ -1,6 +1,7 @@
import { Persona } from "@/app/admin/assistants/interfaces";
import { User } from "../types";
import { checkUserIsNoAuthUser } from "../user";
import { personaComparator } from "@/app/admin/assistants/lib";
export function checkUserOwnsAssistant(user: User | null, assistant: Persona) {
return checkUserIdOwnsAssistant(user?.id, assistant);
@@ -117,3 +118,31 @@ export function getUserCreatedAssistants(
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);
}

View File

@@ -4,6 +4,7 @@ import { fetchLLMProvidersSS } from "@/lib/llm/fetchLLMs";
import { personaComparator } from "@/app/admin/assistants/lib";
import { fetchAssistantsSS } from "../assistants/fetchAssistantsSS";
import { checkLLMSupportsImageInput } from "../llm/utils";
import { filterAssistants } from "../assistants/utils";
interface AssistantData {
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
? (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"
)
);
}
let filteredAssistants = filterAssistants(
assistants,
hasAnyConnectors,
hasImageCompatibleModel
);
return {
assistants: filteredAssistants,