|
16 | 16 |
|
17 | 17 | package org.springframework.ai.model.tool.autoconfigure; |
18 | 18 |
|
| 19 | +import io.micrometer.observation.ObservationRegistry; |
19 | 20 | import java.util.ArrayList; |
20 | 21 | import java.util.List; |
21 | | - |
22 | | -import io.micrometer.observation.ObservationRegistry; |
23 | 22 | import org.slf4j.Logger; |
24 | 23 | import org.slf4j.LoggerFactory; |
25 | 24 |
|
|
43 | 42 | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
44 | 43 | import org.springframework.context.annotation.Bean; |
45 | 44 | import org.springframework.context.support.GenericApplicationContext; |
| 45 | +import org.springframework.util.ClassUtils; |
46 | 46 |
|
47 | 47 | /** |
48 | 48 | * Auto-configuration for common tool calling features of {@link ChatModel}. |
49 | 49 | * |
50 | 50 | * @author Thomas Vitale |
51 | 51 | * @author Christian Tzolov |
| 52 | + * @author Daniel Garnier-Moiroux |
52 | 53 | * @since 1.0.0 |
53 | 54 | */ |
54 | 55 | @AutoConfiguration |
@@ -78,7 +79,21 @@ ToolCallbackResolver toolCallbackResolver(GenericApplicationContext applicationC |
78 | 79 | @Bean |
79 | 80 | @ConditionalOnMissingBean |
80 | 81 | ToolExecutionExceptionProcessor toolExecutionExceptionProcessor(ToolCallingProperties properties) { |
81 | | - return new DefaultToolExecutionExceptionProcessor(properties.isThrowExceptionOnError()); |
| 82 | + ArrayList<Class<? extends RuntimeException>> rethrownExceptions = new ArrayList<>(); |
| 83 | + |
| 84 | + // ClientAuthorizationException is used by Spring Security in oauth2 flows, |
| 85 | + // for example with ServletOAuth2AuthorizedClientExchangeFilterFunction and |
| 86 | + // OAuth2ClientHttpRequestInterceptor. |
| 87 | + Class<? extends RuntimeException> oauth2Exception = getClassOrNull( |
| 88 | + "org.springframework.security.oauth2.client.ClientAuthorizationException"); |
| 89 | + if (oauth2Exception != null) { |
| 90 | + rethrownExceptions.add(oauth2Exception); |
| 91 | + } |
| 92 | + |
| 93 | + return DefaultToolExecutionExceptionProcessor.builder() |
| 94 | + .alwaysThrow(properties.isThrowExceptionOnError()) |
| 95 | + .rethrowExceptions(rethrownExceptions) |
| 96 | + .build(); |
82 | 97 | } |
83 | 98 |
|
84 | 99 | @Bean |
@@ -108,4 +123,14 @@ ToolCallingContentObservationFilter toolCallingContentObservationFilter() { |
108 | 123 | return new ToolCallingContentObservationFilter(); |
109 | 124 | } |
110 | 125 |
|
| 126 | + private static Class<? extends RuntimeException> getClassOrNull(String className) { |
| 127 | + try { |
| 128 | + return (Class<? extends RuntimeException>) ClassUtils.forName(className, null); |
| 129 | + } |
| 130 | + catch (ClassNotFoundException e) { |
| 131 | + logger.debug("Cannot load class", e); |
| 132 | + } |
| 133 | + return null; |
| 134 | + } |
| 135 | + |
111 | 136 | } |
0 commit comments