luigidemasi commented on code in PR #27494:
URL: https://github.com/apache/camel/pull/27494#discussion_r4215765521


##########
components/camel-ai/camel-typesafe-ai/src/main/java/org/apache/camel/component/typesafeai/TypeSafeAiSemanticAdapter.java:
##########
@@ -94,36 +160,86 @@ public Map<String, SemanticResult> 
evaluateBatch(Map<String, SemanticQuestion> q
         return results;
     }
 
-    private Map<String, Object> definition(SemanticQuestion question) {
+    private Map<String, Object> definition(SemanticEvaluation question) {
         validate(question);
         Map<String, Object> definition = new HashMap<>();
-        definition.put("instructions", question.getInstructions());
+        definition.put("instructions", 
question.getParameters().get("instructions"));
         definition.put("type", type(question));
-        if (question.getType() == SemanticQuestion.Type.SCORE) {
-            definition.put("criteria", question.getLevels());
-        } else if (!question.getCriteria().isEmpty()) {
-            definition.put("criteria", question.getCriteria());
+        if ("score".equals(question.getOperation())) {
+            definition.put("criteria", levels(question));
+        } else if (!criteria(question).isEmpty()) {
+            definition.put("criteria", criteria(question));
         }
         return definition;
     }
 
-    private String type(SemanticQuestion question) {
-        return switch (question.getType()) {
-            case BOOLEAN -> "noul";
-            case CHOICE -> "choice";
-            case SCORE -> "score";
+    private String type(SemanticEvaluation question) {
+        return switch (question.getOperation()) {
+            case "boolean" -> "noul";
+            case "choice" -> "choice";
+            case "score" -> "score";
+            default -> throw new IllegalArgumentException("Unsupported 
TypeSafe AI operation");
         };
     }
 
-    private SemanticResult result(SemanticQuestion question, JsonObject 
answer, JsonObject response) {
+    private SemanticResult result(SemanticEvaluation question, JsonObject 
answer, JsonObject response) {
         Map<String, Double> probabilities = new HashMap<>();
         if (answer.get("probabilities") instanceof Map<?, ?> values) {
             values.forEach((key, value) -> probabilities.put((String) key, 
((Number) value).doubleValue()));
         }
-        return new SemanticResult(
-                question.getType() == SemanticQuestion.Type.BOOLEAN ? null : 
answer.get(type(question)),
-                question.getType() == SemanticQuestion.Type.BOOLEAN ? 
answer.getDouble("noul") : null,
+        Double probability = "boolean".equals(question.getOperation()) ? 
answer.getDouble("noul") : null;
+        Object value = switch (question.getOperation()) {
+            case "boolean" -> booleanDecision(question, probability);

Review Comment:
   I traced this through the HTTP client and added three adapter-level HTTP 
regressions in [this 
commit](https://github.com/apache/camel/commit/96e6d465f016856fb43c7e95b8e67818cda8112d).
 All three malformed responses fail before reaching this mapping code:
   
   - Missing `noul`: `IOException: Invalid TypeSafe AI response: Expected a 
number`.
   - Missing named answer: `IOException: Invalid TypeSafe AI response: Answer 
names must match question names`.
   - Missing `answers`: `IOException: Invalid TypeSafe AI response: Expected an 
object`.
   
   `TypeSafeAiClient.send()` always passes the response through 
`TypeSafeAiJson.response(..., request)`, which validates the answer names and 
required numeric probability. The tests exercise that real path through the 
adapter and a local HTTP server. I kept the existing diagnostic and exception 
contract instead of adding an unreachable null guard in the adapter.
   
   _AI-generated by Codex on behalf of @luigidemasi._



##########
core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimplePredicateExpression.java:
##########
@@ -0,0 +1,32 @@
+/*
+ * 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.language.simple;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Expression;
+import org.apache.camel.Predicate;
+
+/**
+ * Opts a Simple function into using its predicate contract for negation, 
including any validation in
+ * {@link #matches(org.apache.camel.Exchange)}. Existing expressions retain 
value-based truthiness, even when they also
+ * implement {@link Predicate}.
+ */
+public interface SimplePredicateExpression extends Expression, Predicate {

Review Comment:
   Added `@since 4.23` to `SimplePredicateExpression` in [this 
commit](https://github.com/apache/camel/commit/96e6d465f016856fb43c7e95b8e67818cda8112d).
   
   _AI-generated by Codex on behalf of @luigidemasi._



##########
components/camel-ai/camel-semantic/src/test/java/org/apache/camel/semantic/SemanticInitializationTest.java:
##########
@@ -77,6 +76,39 @@ void concurrentQuestionRegistryCreationReturnsOneInstance() 
throws Exception {
         }
     }
 
+    @Test
+    void cachedRegistryLookupDoesNotWaitForAnotherContext() throws Exception {
+        ExecutorService callers = Executors.newFixedThreadPool(2);
+        CountDownLatch entered = new CountDownLatch(1);
+        CountDownLatch release = new CountDownLatch(1);
+        try (var creating = new DefaultCamelContext(); var cached = new 
DefaultCamelContext()) {
+            var expected = SemanticEvaluations.get(cached);
+            
creating.getCamelContextExtension().lazyAddContextPlugin(SemanticEvaluations.class,
 () -> {
+                entered.countDown();
+                try {
+                    assertThat(release.await(30, TimeUnit.SECONDS)).isTrue();
+                } catch (InterruptedException e) {
+                    Thread.currentThread().interrupt();
+                    throw new IllegalStateException(e);
+                }
+                // Leave the plugin absent so get() creates and registers it 
after this lookup is released.
+                return null;
+            });
+            var creation = callers.submit(() -> 
SemanticEvaluations.get(creating));
+            try {
+                assertThat(entered.await(10, TimeUnit.SECONDS)).isTrue();
+                var lookup = callers.submit(() -> 
SemanticEvaluations.get(cached));
+                assertThat(lookup.get(10, 
TimeUnit.SECONDS)).isSameAs(expected);
+            } finally {
+                release.countDown();
+                assertThat(creation.get(10, TimeUnit.SECONDS)).isNotNull();

Review Comment:
   Added `.as("registry created after the lazy lookup returns no plugin")` to 
the final creation-future assertion in [this 
commit](https://github.com/apache/camel/commit/96e6d465f016856fb43c7e95b8e67818cda8112d).
   
   _AI-generated by Codex on behalf of @luigidemasi._



-- 
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]

Reply via email to