gnodet-bot commented on code in PR #29722: URL: https://github.com/apache/camel/pull/29722#discussion_r4552995323
########## components/camel-ai/camel-ai-resource/src/main/java/org/apache/camel/component/ai/resource/AiResourceTemplateSpec.java: ########## @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.ai.resource; + +import java.util.Map; +import java.util.Optional; + +import org.apache.camel.support.DefaultConsumer; + +/** + * Framework-agnostic description of a Camel route registered as a templated AI resource. + * + * @since 4.24 + */ +public final class AiResourceTemplateSpec { + + private final String name; + private final String uriTemplate; + private final AiResourceUriTemplate matcher; + private final String description; + private final String mimeType; + private final String title; + private final DefaultConsumer consumer; + + public AiResourceTemplateSpec(String name, String uriTemplate, String description, String mimeType, String title, + DefaultConsumer consumer) { + this.name = name; + this.uriTemplate = uriTemplate; + this.matcher = new AiResourceUriTemplate(uriTemplate); + this.description = description; + this.mimeType = mimeType; + this.title = title; + this.consumer = consumer; + } + + public String getName() { + return name; + } + + public String getUriTemplate() { + return uriTemplate; + } + + public String getDescription() { + return description; + } + + public String getMimeType() { + return mimeType; + } + + public String getTitle() { + return title; + } + + public DefaultConsumer getConsumer() { + return consumer; + } + + public Optional<Map<String, String>> match(String resourceUri) { + return matcher.match(resourceUri); + } + + public boolean overlaps(AiResourceTemplateSpec other) { + return other != null && matcher.overlaps(other.matcher); + } + + /** + * Materializes this template as the fixed resource specification for one matched URI. This lets adapters reuse + * {@link AiResourceExecutor}'s existing MIME conversion and route execution path without introducing MCP types into + * this component. + */ + public AiResourceSpec asResourceSpec(String resourceUri) { + if (match(resourceUri).isEmpty()) { + throw new IllegalArgumentException("URI does not match resource template: " + resourceUri); + } + return new AiResourceSpec(name, resourceUri, description, mimeType, title, consumer); + } + + @Override Review Comment: Add the missing `equals`/`hashCode` before `toString`: ```suggestion @Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof AiResourceTemplateSpec that)) { return false; } return Objects.equals(name, that.name) && Objects.equals(uriTemplate, that.uriTemplate) && Objects.equals(description, that.description) && Objects.equals(mimeType, that.mimeType) && Objects.equals(title, that.title) && Objects.equals(consumer, that.consumer); } @Override public int hashCode() { return Objects.hash(name, uriTemplate, description, mimeType, title, consumer); } @Override ``` ########## components/camel-ai/camel-ai-resource/src/main/java/org/apache/camel/component/ai/resource/AiResourceRegistry.java: ########## @@ -237,4 +343,59 @@ public Set<AiResourceSpec> getDefaultResources() { lock.unlock(); } } + + /** + * Returns templates registered for a tag, merged with templates in the default pool. + */ + public Set<AiResourceTemplateSpec> getResourceTemplatesByTag(String tag) { + lock.lock(); + try { + Set<AiResourceTemplateSpec> result = new LinkedHashSet<>(defaultResourceTemplates); + Set<AiResourceTemplateSpec> tagged = resourceTemplates.get(tag); + if (tagged != null) { + result.addAll(tagged); + } + return result; + } finally { + lock.unlock(); + } + } + + /** + * Returns all templates across all tags and the default pool. + */ + public Set<AiResourceTemplateSpec> getAllResourceTemplates() { + lock.lock(); + try { + Set<AiResourceTemplateSpec> result = new LinkedHashSet<>(defaultResourceTemplates); + for (Set<AiResourceTemplateSpec> tagged : resourceTemplates.values()) { + result.addAll(tagged); + } + return result; + } finally { + lock.unlock(); + } + } + + public Map<String, Set<AiResourceTemplateSpec>> getResourceTemplates() { + lock.lock(); + try { + Map<String, Set<AiResourceTemplateSpec>> snapshot = new LinkedHashMap<>(); + for (Map.Entry<String, Set<AiResourceTemplateSpec>> entry : resourceTemplates.entrySet()) { + snapshot.put(entry.getKey(), new LinkedHashSet<>(entry.getValue())); + } + return Map.copyOf(snapshot); + } finally { + lock.unlock(); + } + } + + public Set<AiResourceTemplateSpec> getDefaultResourceTemplates() { Review Comment: 💡 Missing Javadoc: ```suggestion /** * Returns a snapshot of the default (untagged) resource templates. * * @since 4.24 */ public Set<AiResourceTemplateSpec> getDefaultResourceTemplates() { ``` ########## components/camel-ai/camel-ai-resource/src/main/java/org/apache/camel/component/ai/resource/AiResourceTemplateSpec.java: ########## @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.ai.resource; + +import java.util.Map; +import java.util.Optional; Review Comment: ⚠️ **Missing `equals`/`hashCode`:** `AiResourceTemplateSpec` is stored in `LinkedHashSet` collections in `AiResourceRegistry`, but relies on identity-based `Object.equals`. Its sibling `AiResourceSpec` explicitly defines `equals`/`hashCode` on all fields — the same contract is needed here for consistent Set semantics. Also add `import java.util.Objects;` (needed by the equals/hashCode implementation below): ```suggestion import java.util.Map; import java.util.Objects; import java.util.Optional; ``` ########## components/camel-ai/camel-ai-resource/src/main/java/org/apache/camel/component/ai/resource/AiResourceRegistry.java: ########## @@ -237,4 +343,59 @@ public Set<AiResourceSpec> getDefaultResources() { lock.unlock(); } } + + /** + * Returns templates registered for a tag, merged with templates in the default pool. + */ + public Set<AiResourceTemplateSpec> getResourceTemplatesByTag(String tag) { + lock.lock(); + try { + Set<AiResourceTemplateSpec> result = new LinkedHashSet<>(defaultResourceTemplates); + Set<AiResourceTemplateSpec> tagged = resourceTemplates.get(tag); + if (tagged != null) { + result.addAll(tagged); + } + return result; + } finally { + lock.unlock(); + } + } + + /** + * Returns all templates across all tags and the default pool. + */ + public Set<AiResourceTemplateSpec> getAllResourceTemplates() { + lock.lock(); + try { + Set<AiResourceTemplateSpec> result = new LinkedHashSet<>(defaultResourceTemplates); + for (Set<AiResourceTemplateSpec> tagged : resourceTemplates.values()) { + result.addAll(tagged); + } + return result; + } finally { + lock.unlock(); + } + } + + public Map<String, Set<AiResourceTemplateSpec>> getResourceTemplates() { Review Comment: 💡 Missing Javadoc — every other public method in this class is documented. This is public API and callers need to understand the return value semantics (defensive copy, tag-to-specs mapping): ```suggestion /** * Returns a snapshot of the tag-to-template-spec mapping. Each value set is a defensive copy. * * @since 4.24 */ public Map<String, Set<AiResourceTemplateSpec>> getResourceTemplates() { ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
