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.iri;
019
020import java.net.URI;
021import java.net.URISyntaxException;
022import java.util.Optional;
023
024/**
025 * Provider of an AI Thing based on the <a href="https://docs.enola.dev/specs/aiuri/">Enola.dev AI
026 * URI spec</a>.
027 */
028public interface Provider<T> {
029
030    String name();
031
032    String docURL();
033
034    Iterable<String> uriTemplates();
035
036    Iterable<URI> uriExamples();
037
038    // TODO Set<String> secrets();
039
040    /**
041     * Return AI Thing if it's available. Empty if its configuration like secrets etc. is missing.
042     */
043    Optional<T> optional(URI uri);
044
045    default T get(URI uri) throws IllegalArgumentException {
046        return optional(uri)
047                .orElseThrow(() -> new IllegalArgumentException(name() + " cannot provide " + uri));
048    }
049
050    default T get(String uri, Object context) throws IllegalArgumentException {
051        try {
052            return get(new URI(uri));
053        } catch (URISyntaxException e) {
054            throw new IllegalArgumentException(
055                    "Invalid Model URI (see https://docs.enola.dev/specs/aiuri/) '"
056                            + uri
057                            + "' (raw input: '"
058                            + e.getInput()
059                            + "') in: "
060                            + context,
061                    e);
062        }
063    }
064}