This commit is contained in:
tanglong 2026-07-13 17:50:48 +08:00
parent b72d166adb
commit d78c278a52
2 changed files with 20 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry; import reactor.util.retry.Retry;
import java.time.Duration; import java.time.Duration;
@ -26,10 +27,26 @@ public class CozeWorkflowClient {
return webClient.post() return webClient.post()
.uri(properties.getCoze().getChatPath()) .uri(properties.getCoze().getChatPath())
.header(HttpHeaders.AUTHORIZATION, "Bearer " + properties.getCoze().getApiKey()) .header(HttpHeaders.AUTHORIZATION, "Bearer " + properties.getCoze().getApiKey())
.accept(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_JSON)
.bodyValue(request) .bodyValue(request)
.retrieve() .exchangeToMono(response -> response.bodyToMono(String.class)
.bodyToMono(String.class) .defaultIfEmpty("")
.flatMap(body -> {
if (response.statusCode().isError()) {
return Mono.error(new IllegalStateException(
"Coze workflow HTTP " + response.statusCode().value() + " body=" + preview(body)
));
}
return Mono.just(body);
}))
.retryWhen(Retry.backoff(properties.getCoze().getMaxRetries(), Duration.ofMillis(300))) .retryWhen(Retry.backoff(properties.getCoze().getMaxRetries(), Duration.ofMillis(300)))
.block(Duration.ofMillis(properties.getCoze().getReadTimeoutMs())); .block(Duration.ofMillis(properties.getCoze().getReadTimeoutMs()));
} }
private String preview(String text) {
if (text == null || text.isBlank()) {
return "";
}
return text.length() <= 300 ? text : text.substring(0, 300) + "...";
}
} }

View File

@ -38,7 +38,7 @@ foundation:
coze: coze:
base-url: https://api.coze.cn base-url: https://api.coze.cn
chat-path: /v1/workflows/chat chat-path: /v1/workflows/chat
api-key: ${COZE_API_KEY:cztei_hTd3jrof7F2fpRM4rVvNiK1ipxaQl4rlvQNHKBDK1RlTgnyFO9UqwShkr8IXcuVrk} api-key: ${COZE_API_KEY:cztei_l0qEUEjq0o8KUTatjnvaZkKdaOwvhBMzu3cmkI0qLmjQwz4bAhGrHrs4l46I61xbY}
default-workflow: wecom-default default-workflow: wecom-default
workflows: workflows:
wecom-default: wecom-default: