From 0bde01a85f4aa39b04ff1ea4dc9a47346c02cb51 Mon Sep 17 00:00:00 2001 From: Enrico Ros Date: Wed, 12 Nov 2025 10:56:42 -0800 Subject: [PATCH] Llms: vector ops --- src/common/stores/llms/store-llms.ts | 16 +++++++++++++++- .../openrouter/OpenRouterServiceSetup.tsx | 19 ++++++++++--------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/common/stores/llms/store-llms.ts b/src/common/stores/llms/store-llms.ts index f4302e249..30ed3c35f 100644 --- a/src/common/stores/llms/store-llms.ts +++ b/src/common/stores/llms/store-llms.ts @@ -37,6 +37,7 @@ interface LlmsRootActions { removeLLM: (id: DLLMId) => void; rerankLLMsByServices: (serviceIdOrder: DModelsServiceId[]) => void; updateLLM: (id: DLLMId, partial: Partial) => void; + updateLLMs: (updates: Array<{ id: DLLMId; partial: Partial }>) => void; updateLLMUserParameters: (id: DLLMId, partial: Partial) => void; deleteLLMUserParameter: (id: DLLMId, parameterId: DModelParameterId) => void; resetLLMUserParameters: (id: DLLMId) => void; @@ -141,6 +142,19 @@ export const useModelsStore = create()(persist( ), })), + updateLLMs: (updates: Array<{ id: DLLMId; partial: Partial }>) => + set(state => { + // Create a map of updates for efficient lookup + const updatesMap = new Map(updates.map(u => [u.id, u.partial])); + + return { + llms: state.llms.map((llm: DLLM): DLLM => { + const partial = updatesMap.get(llm.id); + return partial ? { ...llm, ...partial } : llm; + }), + }; + }), + updateLLMUserParameters: (id: DLLMId, partialUserParameters: Partial) => set(({ llms }) => ({ llms: llms.map((llm: DLLM): DLLM => @@ -252,7 +266,7 @@ export const useModelsStore = create()(persist( * 2: large changes on all LLMs, and reset chat/fast/func LLMs * 3: big-AGI v2.x upgrade * 4: migrate .options to .initialParameters/.userParameters - * 4B: we changed from .chatLLMId/.fastLLMId to modelAssignments: {}, without expicit migration (done on rehydrate, and for no particular reason) + * 4B: we changed from .chatLLMId/.fastLLMId to modelAssignments: {}, without explicit migration (done on rehydrate, and for no particular reason) */ version: 4, migrate: (_state: any, fromVersion: number): LlmsStore => { diff --git a/src/modules/llms/vendors/openrouter/OpenRouterServiceSetup.tsx b/src/modules/llms/vendors/openrouter/OpenRouterServiceSetup.tsx index df839d2a7..952cd8316 100644 --- a/src/modules/llms/vendors/openrouter/OpenRouterServiceSetup.tsx +++ b/src/modules/llms/vendors/openrouter/OpenRouterServiceSetup.tsx @@ -48,22 +48,23 @@ export function OpenRouterServiceSetup(props: { serviceId: DModelsServiceId }) { // ...bye / see you soon at the callback location... }; - const handleRemoveNonFreeLLMs = () => { - // A bit of a hack + const handleHIdeNonFreeLLMs = () => { const { llms } = llmsStoreState(); - const { updateLLM } = llmsStoreActions(); - llms + const { updateLLMs } = llmsStoreActions(); + const updates = llms .filter(llm => llm.sId === props.serviceId) .filter(llm => getLLMPricing(llm)?.chat?._isFree === false) - .forEach(llm => updateLLM(llm.id, { userHidden: true })); + .map(llm => ({ id: llm.id, partial: { userHidden: true } })); + updateLLMs(updates); }; const handleSetVisibilityAll = React.useCallback((visible: boolean) => { const { llms } = llmsStoreState(); - const { updateLLM } = llmsStoreActions(); - llms + const { updateLLMs } = llmsStoreActions(); + const updates = llms .filter(llm => llm.sId === props.serviceId) - .forEach(llm => updateLLM(llm.id, { userHidden: !visible })); + .map(llm => ({ id: llm.id, partial: { userHidden: !visible } })); + updateLLMs(updates); }, [props.serviceId]); return <> @@ -112,7 +113,7 @@ export function OpenRouterServiceSetup(props: { serviceId: DModelsServiceId }) {