|
| 1 | +package io.quarkiverse.langchain4j.runtime.tool; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | +import java.util.concurrent.ConcurrentHashMap; |
| 5 | + |
| 6 | +import dev.langchain4j.model.chat.request.json.JsonArraySchema; |
| 7 | +import dev.langchain4j.model.chat.request.json.JsonBooleanSchema; |
| 8 | +import dev.langchain4j.model.chat.request.json.JsonEnumSchema; |
| 9 | +import dev.langchain4j.model.chat.request.json.JsonIntegerSchema; |
| 10 | +import dev.langchain4j.model.chat.request.json.JsonNumberSchema; |
| 11 | +import dev.langchain4j.model.chat.request.json.JsonObjectSchema; |
| 12 | +import dev.langchain4j.model.chat.request.json.JsonReferenceSchema; |
| 13 | +import dev.langchain4j.model.chat.request.json.JsonSchemaElement; |
| 14 | +import dev.langchain4j.model.chat.request.json.JsonStringSchema; |
| 15 | +import io.quarkus.runtime.ObjectSubstitution; |
| 16 | + |
| 17 | +public sealed class JsonSchemaElementObjectSubstitution |
| 18 | + implements ObjectSubstitution<JsonSchemaElement, JsonSchemaElementObjectSubstitution.Serialized> |
| 19 | + permits JsonArraySchemaObjectSubstitution, |
| 20 | + JsonBooleanSchemaObjectSubstitution, |
| 21 | + JsonEnumSchemaObjectSubstitution, |
| 22 | + JsonIntegerSchemaObjectSubstitution, |
| 23 | + JsonNumberSchemaObjectSubstitution, |
| 24 | + JsonObjectSchemaObjectSubstitution, |
| 25 | + JsonReferenceSchemaObjectSubstitution, |
| 26 | + JsonStringSchemaObjectSubstitution { |
| 27 | + |
| 28 | + // Using ConcurrentHashMap in case multiple threads are using this class at the same time |
| 29 | + // Not sure if this will ever happen |
| 30 | + private final Map<Class<?>, JsonSchemaElementObjectSubstitution> substitutions = new ConcurrentHashMap<>(8); |
| 31 | + |
| 32 | + @Override |
| 33 | + public JsonSchemaElementObjectSubstitution.Serialized serialize(JsonSchemaElement obj) { |
| 34 | + return getSubstitution(obj.getClass()).serialize(obj); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public JsonSchemaElement deserialize(JsonSchemaElementObjectSubstitution.Serialized obj) { |
| 39 | + return getSubstitution(obj.getClass()).deserialize(obj); |
| 40 | + } |
| 41 | + |
| 42 | + private JsonSchemaElementObjectSubstitution getSubstitution(Class<?> clazz) { |
| 43 | + return this.substitutions.computeIfAbsent(clazz, c -> { |
| 44 | + if (JsonArraySchema.class.isAssignableFrom(c) |
| 45 | + || JsonArraySchemaObjectSubstitution.Serialized.class.isAssignableFrom(c)) { |
| 46 | + return new JsonArraySchemaObjectSubstitution(); |
| 47 | + } else if (JsonBooleanSchema.class.isAssignableFrom(c) |
| 48 | + || JsonBooleanSchemaObjectSubstitution.Serialized.class.isAssignableFrom(c)) { |
| 49 | + return new JsonBooleanSchemaObjectSubstitution(); |
| 50 | + } else if (JsonEnumSchema.class.isAssignableFrom(c) |
| 51 | + || JsonEnumSchemaObjectSubstitution.Serialized.class.isAssignableFrom(c)) { |
| 52 | + return new JsonEnumSchemaObjectSubstitution(); |
| 53 | + } else if (JsonIntegerSchema.class.isAssignableFrom(c) |
| 54 | + || JsonIntegerSchemaObjectSubstitution.Serialized.class.isAssignableFrom(c)) { |
| 55 | + return new JsonIntegerSchemaObjectSubstitution(); |
| 56 | + } else if (JsonNumberSchema.class.isAssignableFrom(c) |
| 57 | + || JsonNumberSchemaObjectSubstitution.Serialized.class.isAssignableFrom(c)) { |
| 58 | + return new JsonNumberSchemaObjectSubstitution(); |
| 59 | + } else if (JsonObjectSchema.class.isAssignableFrom(c) |
| 60 | + || JsonObjectSchemaObjectSubstitution.Serialized.class.isAssignableFrom(c)) { |
| 61 | + return new JsonObjectSchemaObjectSubstitution(); |
| 62 | + } else if (JsonReferenceSchema.class.isAssignableFrom(c) |
| 63 | + || JsonReferenceSchemaObjectSubstitution.Serialized.class.isAssignableFrom(c)) { |
| 64 | + return new JsonReferenceSchemaObjectSubstitution(); |
| 65 | + } else if (JsonStringSchema.class.isAssignableFrom(c) |
| 66 | + || JsonStringSchemaObjectSubstitution.Serialized.class.isAssignableFrom(c)) { |
| 67 | + return new JsonStringSchemaObjectSubstitution(); |
| 68 | + } |
| 69 | + |
| 70 | + // Handle unsupported types |
| 71 | + throw new IllegalArgumentException("Unsupported type: %s".formatted(c.getName())); |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + public static sealed class Serialized |
| 76 | + permits JsonArraySchemaObjectSubstitution.Serialized, |
| 77 | + JsonBooleanSchemaObjectSubstitution.Serialized, |
| 78 | + JsonEnumSchemaObjectSubstitution.Serialized, |
| 79 | + JsonIntegerSchemaObjectSubstitution.Serialized, |
| 80 | + JsonNumberSchemaObjectSubstitution.Serialized, |
| 81 | + JsonObjectSchemaObjectSubstitution.Serialized, |
| 82 | + JsonReferenceSchemaObjectSubstitution.Serialized, |
| 83 | + JsonStringSchemaObjectSubstitution.Serialized { |
| 84 | + |
| 85 | + } |
| 86 | +} |
0 commit comments