33import static io .quarkiverse .langchain4j .runtime .OptionalUtil .firstOrDefault ;
44
55import java .time .Duration ;
6- import java .util .function .Supplier ;
6+ import java .util .function .Function ;
7+ import java .util .stream .Collectors ;
8+
9+ import jakarta .enterprise .inject .Instance ;
10+ import jakarta .enterprise .util .TypeLiteral ;
711
812import org .jboss .logging .Logger ;
913
1317import dev .langchain4j .model .chat .DisabledChatModel ;
1418import dev .langchain4j .model .chat .DisabledStreamingChatModel ;
1519import dev .langchain4j .model .chat .StreamingChatModel ;
20+ import dev .langchain4j .model .chat .listener .ChatModelListener ;
1621import io .quarkiverse .langchain4j .anthropic .runtime .config .ChatModelConfig ;
1722import io .quarkiverse .langchain4j .anthropic .runtime .config .LangChain4jAnthropicConfig ;
1823import io .quarkiverse .langchain4j .runtime .NamedConfigUtil ;
24+ import io .quarkus .arc .SyntheticCreationalContext ;
1925import io .quarkus .runtime .RuntimeValue ;
2026import io .quarkus .runtime .annotations .Recorder ;
2127import io .smallrye .config .ConfigValidationException ;
2430public class AnthropicRecorder {
2531 private static final Logger LOG = Logger .getLogger (AnthropicRecorder .class );
2632
33+ private static final TypeLiteral <Instance <ChatModelListener >> CHAT_MODEL_LISTENER_TYPE_LITERAL = new TypeLiteral <>() {
34+ };
35+
2736 private static final String DUMMY_KEY = "dummy" ;
2837
2938 private final RuntimeValue <LangChain4jAnthropicConfig > runtimeConfig ;
@@ -32,7 +41,7 @@ public AnthropicRecorder(RuntimeValue<LangChain4jAnthropicConfig> runtimeConfig)
3241 this .runtimeConfig = runtimeConfig ;
3342 }
3443
35- public Supplier < ChatModel > chatModel (String configName ) {
44+ public Function < SyntheticCreationalContext < ChatModel >, ChatModel > chatModel (String configName ) {
3645 var anthropicConfig = correspondingAnthropicConfig (runtimeConfig .getValue (), configName );
3746
3847 if (anthropicConfig .enableIntegration ()) {
@@ -89,23 +98,26 @@ public Supplier<ChatModel> chatModel(String configName) {
8998 builder .sendThinking (thinkingConfig .sendThinking ().get ());
9099 }
91100
92- return new Supplier <>() {
101+ return new Function <>() {
93102 @ Override
94- public ChatModel get () {
103+ public ChatModel apply (SyntheticCreationalContext <ChatModel > context ) {
104+ builder .listeners (context .getInjectedReference (CHAT_MODEL_LISTENER_TYPE_LITERAL ).stream ()
105+ .collect (Collectors .toList ()));
95106 return builder .build ();
96107 }
97108 };
98109 } else {
99- return new Supplier <>() {
110+ return new Function <>() {
100111 @ Override
101- public ChatModel get ( ) {
112+ public ChatModel apply ( SyntheticCreationalContext < ChatModel > context ) {
102113 return new DisabledChatModel ();
103114 }
104115 };
105116 }
106117 }
107118
108- public Supplier <StreamingChatModel > streamingChatModel (String configName ) {
119+ public Function <SyntheticCreationalContext <StreamingChatModel >, StreamingChatModel > streamingChatModel (
120+ String configName ) {
109121 var anthropicConfig = correspondingAnthropicConfig (runtimeConfig .getValue (), configName );
110122
111123 if (anthropicConfig .enableIntegration ()) {
@@ -160,16 +172,18 @@ public Supplier<StreamingChatModel> streamingChatModel(String configName) {
160172 builder .sendThinking (thinkingConfig .sendThinking ().get ());
161173 }
162174
163- return new Supplier <>() {
175+ return new Function <>() {
164176 @ Override
165- public StreamingChatModel get () {
177+ public StreamingChatModel apply (SyntheticCreationalContext <StreamingChatModel > context ) {
178+ builder .listeners (context .getInjectedReference (CHAT_MODEL_LISTENER_TYPE_LITERAL ).stream ()
179+ .collect (Collectors .toList ()));
166180 return builder .build ();
167181 }
168182 };
169183 } else {
170- return new Supplier <>() {
184+ return new Function <>() {
171185 @ Override
172- public StreamingChatModel get ( ) {
186+ public StreamingChatModel apply ( SyntheticCreationalContext < StreamingChatModel > context ) {
173187 return new DisabledStreamingChatModel ();
174188 }
175189 };
0 commit comments