Skip to content

Commit

Permalink
Backend: auto-configuration. Fixes #436
Browse files Browse the repository at this point in the history
  • Loading branch information
enricoros committed Mar 29, 2024
1 parent 15678cd commit 65c7df7
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions src/common/logic/autoconf.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { createStore } from 'zustand/vanilla';
import { persist } from 'zustand/middleware';

import { DModelSource, useModelsStore } from '~/modules/llms/store-llms';
import { createModelSourceForVendor, findAccessForSourceOrThrow, findAllVendors } from '~/modules/llms/vendors/vendors.registry';
import { getBackendCapabilities } from '~/modules/backend/store-backend-capabilities';
import { updateModelsForSource } from '~/modules/llms/vendors/useLlmUpdateModels';


interface AutoConfStore {
Expand All @@ -15,6 +19,7 @@ interface AutoConfStore {

}


const autoConfVanillaStore = createStore<AutoConfStore>()(persist((_set, _get) => ({

// init state
Expand All @@ -32,16 +37,50 @@ const autoConfVanillaStore = createStore<AutoConfStore>()(persist((_set, _get) =
// skip if no change is detected / no config needed
const backendCaps = getBackendCapabilities();
const backendHash = backendCaps.llmConfigHash;
if (!backendHash || backendHash === lastSeenBackendEnvHash) {
console.log('No backend configuration hash found or no change detected. Skipping...');
if (!backendHash || backendHash === lastSeenBackendEnvHash)
return _set({ isConfiguring: false, isConfigurationDone: true });
}

// begin configuration
_set({ isConfiguring: true, lastSeenBackendEnvHash: backendHash });



// find
let configurableVendors = findAllVendors()
.filter(vendor => vendor.hasBackendCapKey && backendCaps[vendor.hasBackendCapKey]);

// Sequentially auto-configure each vendor
await configurableVendors.reduce(async (promiseChain, vendor) => {
return promiseChain
.then(async () => {

// find the first source for this vendor
const { sources, addSource } = useModelsStore.getState();
let source: DModelSource;
const fistSourceForVendor = sources.find(source => source.vId === vendor.id);
if (fistSourceForVendor)
source = fistSourceForVendor;
else {
// create and append the model source, assuming the backend configuration will be successful
source = createModelSourceForVendor(vendor.id, sources);
addSource(source);
source = useModelsStore.getState().sources.find(_s => _s.id === source.id)!;
}

// get the access, assuming there's no client config and the server will do all
const transportAcess = findAccessForSourceOrThrow(source.id);

// fetch models
const data = await vendor.rpcUpdateModelsOrThrow(transportAcess);
return updateModelsForSource(data, source, true);
})
.catch(error => {
// catches errors and logs them, but does not stop the chain
console.error('Auto-configuration failed for vendor:', vendor.name, error);
})
.then(() => {
// short delay between vendors
return new Promise(resolve => setTimeout(resolve, 50));
});
}, Promise.resolve());

// end configuration
_set({ isConfiguring: false, isConfigurationDone: true });
Expand Down

0 comments on commit 65c7df7

Please sign in to comment.