001/*
002 *  Copyright 2025 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */package org.ametys.plugins.ai.provider;
016
017import java.util.List;
018import java.util.Map;
019import java.util.stream.Collectors;
020
021import org.apache.avalon.framework.component.Component;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.commons.lang3.StringUtils;
025
026import org.ametys.plugins.ai.provider.impl.AbstractAIProvider;
027import org.ametys.runtime.i18n.I18nizableText;
028import org.ametys.runtime.model.Enumerator;
029import org.ametys.runtime.plugin.component.DeferredServiceable;
030
031/**
032 * Enumerates possible models depending on chosen aiprovider
033 */
034public class AIImageModelEnumerator implements Enumerator<String>, DeferredServiceable, Component
035{
036    /** Avalon role */
037    public static final String ROLE = AIImageModelEnumerator.class.getName();
038    
039    private AIProviderExtensionPoint _aiProviderExtensionPoint;
040    
041    public void deferredService(ServiceManager manager) throws ServiceException
042    {
043        _aiProviderExtensionPoint = (AIProviderExtensionPoint) manager.lookup(AIProviderExtensionPoint.ROLE);
044    }
045    
046    public Map<String, I18nizableText> getEntries() throws Exception
047    {
048        throw new UnsupportedOperationException(ROLE + " must be configured with one dependency");
049    }
050    
051    public Map<String, I18nizableText> getEntries(Map<String, Object> contextualParameters) throws Exception
052    {
053        @SuppressWarnings("unchecked")
054        List<String> dependencies = (List<String>) contextualParameters.get("dependencies");
055        
056        if (dependencies == null || dependencies.size() != 1)
057        {
058            throw new IllegalArgumentException(ROLE + " must be configured with one dependency");
059        }
060        
061        String aiProviderId = dependencies.get(0);
062        if (StringUtils.isNotBlank(aiProviderId)
063            && _aiProviderExtensionPoint.hasExtension(aiProviderId))
064        {
065            AIProvider aiProvider = _aiProviderExtensionPoint.getExtension(aiProviderId);
066            if (aiProvider instanceof AbstractAIProvider absAIProvider)
067            {
068                return absAIProvider.getKnownImageModels().stream().collect(Collectors.toMap(v -> v, v -> new I18nizableText(v)));
069            }
070        }
071        return Map.of();
072    }
073}