Skip to content

SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.

5.2 KB
Raw Blame
import { Request } from 'express';
import {
  DEFAULT_SMYTH_LLM_PROVIDERS_SETTINGS,
  SMYTH_LLM_PROVIDERS_SETTINGS_KEY,
} from '../../constants';
import { getTeamSettingsObj } from '../../services/team-data.service';
import { vault } from '../SmythVault.class';
import { LLMService } from './LLMService.class';

// TODO: Refactor this function to be more readable and maintainable.
async function getUserLLMModels(req: Request) {
  const models = {};
  try {
    const team = req._team?.id;

    const llmProvider = new LLMService();
    const LLMModels = await llmProvider.getModels(req);

    const keys: any = (await vault.get({ team, scope: ['global'] }, req)) || {};
    const enabledSmythOSProviders = await _getEnabledSmythOSProviders(req);

    for (let modelEntryId in LLMModels) {
      let modelTpl = JSON.parse(JSON.stringify(LLMModels[modelEntryId]));

      if (modelTpl.alias) {
        const aliasOptions = JSON.parse(JSON.stringify(LLMModels[modelTpl.alias] || {}));
        delete aliasOptions.hidden; //do not override hide option because we may want to hide the original model but keep the alias

        // We keep some of the original model information in the alias model to clearly show which model uses which alias.
        // In the list, we display the original model label with the alias as a tag.
        // However, when running the component, we properly use the alias model information.
        const aliasOverrides = {
          label: modelTpl.label,
          modelId: modelTpl.modelId,
          tags: modelTpl.tags,
        };

        modelTpl = { ...modelTpl, ...aliasOptions, ...aliasOverrides }; //override the model config with the alias config
        modelTpl.tags.push('alias: ' + modelTpl.alias);
      }

      // #region: Legacy keyOptions handling
      // TODO: We will clean up `keyOptions` in the future but keep it for legacy users.

      const modelKeyOptions = modelTpl.keyOptions || {};
      delete modelTpl.keyOptions;

      const provider = modelTpl?.provider?.toLowerCase();
      const hasAPIKey = !!keys[provider?.toLowerCase()];

      if (hasAPIKey) {
        modelTpl = { ...modelTpl, ...modelKeyOptions }; //override the model config with the key config
        modelTpl.hasKey = true;
      }
      // #endregion

      let enabled =
        typeof LLMModels[modelEntryId].enabled === 'function'
          ? LLMModels[modelEntryId].enabled({ user: req.user, team: req._team })
          : modelTpl.enabled;

      // If the credentials are only 'vault' and the team doesn't have their own API key, disable the model.
      if (modelTpl.credentials === 'vault' && !hasAPIKey) {
        enabled = false;
      }

      // Skip legacy models for users with built-in model access to avoid confusion
      if (_hasBuiltInModels(req) && modelEntryId.startsWith('legacy/')) {
        enabled = false;
      }

      // V2: only applicable for GenAI LLM and SmythOS models
      if (modelEntryId.startsWith('smythos/')) {
        enabled = enabled && enabledSmythOSProviders.includes(modelTpl?.provider?.toLowerCase());
      }

      // Only show Runware models to users with proper plan (indicated by having builtin models enabled)
      if (provider === 'runware') {
        enabled = enabled && _hasBuiltInModels(req);
      }

      // Hide DALL-E models from users with builtin models
      if (_hasBuiltInModels(req) && modelEntryId.startsWith('dall-e')) {
        enabled = false;
      }

      if (!enabled || (!modelTpl?.components && !modelTpl?.features)) {
        continue;
      }

      // Add token tag to the model
      if (modelTpl?.tokens) {
        modelTpl?.tags?.push(_getTokenTag(modelTpl?.tokens));
      }

      delete modelTpl.enabled;
      delete modelTpl.alias;

      models[modelEntryId] = modelTpl;
    }
  } catch (error) {
    console.error('Error getting User LLMs', error?.message);
  }

  return models;
}

/**
 * Gets the list of enabled SmythOS LLM providers for the team
 * @param req - Express Request object containing team information
 * @returns Array of enabled provider names in lowercase
 */
async function _getEnabledSmythOSProviders(req: Request): Promise<string[]> {
  let smythosLLMProviders = (await getTeamSettingsObj(req, SMYTH_LLM_PROVIDERS_SETTINGS_KEY)) || {};

  // Allow builtin model for smyth staff
  const hasBuiltinModels = _hasBuiltInModels(req);

  if (!hasBuiltinModels) return [];

  smythosLLMProviders = { ...DEFAULT_SMYTH_LLM_PROVIDERS_SETTINGS, ...smythosLLMProviders };

  // Filter and map enabled providers
  const enabledProviders = Object.entries(smythosLLMProviders || {})
    .filter(([_, config]) => config?.enabled === true)
    .map(([provider]) => provider.toLowerCase());

  // Always include 'runware' provider since it's not configurable through the UI but should be enabled
  return [...enabledProviders, 'runware'];
}

function _getTokenTag(contextWindow) {
  if (!contextWindow) return '';

  if (contextWindow >= 1000000) {
    return `${Math.floor(contextWindow / 1000000)}M`;
  } else {
    return `${Math.floor(contextWindow / 1000)}K`;
  }
}

function _hasBuiltInModels(req: Request) {
  return (
    req._team.subscription.plan?.properties?.flags?.hasBuiltinModels ||
    req._team.subscription.plan?.isDefaultPlan ||
    false
  );
}

export default {
  getUserLLMModels,
};