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 */ 016package org.ametys.plugins.ai.provider; 017 018import java.util.List; 019import java.util.Map; 020import java.util.stream.Collectors; 021 022import org.apache.avalon.framework.component.Component; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.commons.lang3.StringUtils; 026 027import org.ametys.plugins.ai.provider.impl.AbstractAIProvider; 028import org.ametys.runtime.i18n.I18nizableText; 029import org.ametys.runtime.model.Enumerator; 030import org.ametys.runtime.plugin.component.DeferredServiceable; 031 032/** 033 * Enumerates possible models depending on chosen aiprovider 034 */ 035public class AITextModelEnumerator implements Enumerator<String>, DeferredServiceable, Component 036{ 037 /** Avalon role */ 038 public static final String ROLE = AITextModelEnumerator.class.getName(); 039 040 private AIProviderExtensionPoint _aiProviderExtensionPoint; 041 042 public void deferredService(ServiceManager manager) throws ServiceException 043 { 044 _aiProviderExtensionPoint = (AIProviderExtensionPoint) manager.lookup(AIProviderExtensionPoint.ROLE); 045 } 046 047 public Map<String, I18nizableText> getEntries() throws Exception 048 { 049 throw new UnsupportedOperationException(ROLE + " must be configured with one dependency"); 050 } 051 052 public Map<String, I18nizableText> getEntries(Map<String, Object> contextualParameters) throws Exception 053 { 054 @SuppressWarnings("unchecked") 055 List<String> dependencies = (List<String>) contextualParameters.get("dependencies"); 056 057 if (dependencies.size() != 1) 058 { 059 throw new IllegalArgumentException(ROLE + " must be configured with one dependency"); 060 } 061 062 String aiProviderId = dependencies.get(0); 063 if (StringUtils.isNotBlank(aiProviderId) 064 && _aiProviderExtensionPoint.hasExtension(aiProviderId)) 065 { 066 AIProvider aiProvider = _aiProviderExtensionPoint.getExtension(aiProviderId); 067 if (aiProvider instanceof AbstractAIProvider absAIProvider) 068 { 069 return absAIProvider.getKnownTextModels().stream().collect(Collectors.toMap(v -> v, v -> new I18nizableText(v))); 070 } 071 } 072 return Map.of(); 073 } 074}