001/*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2025-2026 The Enola <https://enola.dev> Authors
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *     https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package dev.enola.ai.adk.web;
019
020import com.google.adk.agents.BaseAgent;
021import com.google.adk.web.AgentLoader;
022import com.google.common.collect.ImmutableList;
023import com.google.common.collect.ImmutableMap;
024
025import java.util.Arrays;
026import java.util.NoSuchElementException;
027import java.util.function.Function;
028import java.util.stream.Stream;
029import java.util.stream.StreamSupport;
030
031// TODO Upstream this (it's package-local there)
032public class AgentStaticLoader implements AgentLoader {
033    private final ImmutableMap<String, BaseAgent> agents;
034
035    public AgentStaticLoader(BaseAgent... agents) {
036        this(Arrays.stream(agents));
037    }
038
039    public AgentStaticLoader(Iterable<BaseAgent> agents) {
040        this(StreamSupport.stream(agents.spliterator(), false));
041    }
042
043    public AgentStaticLoader(Stream<BaseAgent> agents) {
044        this.agents =
045                agents.collect(ImmutableMap.toImmutableMap(BaseAgent::name, Function.identity()));
046    }
047
048    public ImmutableList<String> listAgents() {
049        return this.agents.keySet().stream().collect(ImmutableList.toImmutableList());
050    }
051
052    public BaseAgent loadAgent(String name) {
053        if (name != null && !name.trim().isEmpty()) {
054            BaseAgent agent = this.agents.get(name);
055            if (agent == null) {
056                throw new NoSuchElementException("Agent not found: " + name);
057            } else {
058                return agent;
059            }
060        } else {
061            throw new IllegalArgumentException("Agent name cannot be null or empty");
062        }
063    }
064}