From d78c278a529af5a34c3d63f884690fbdda3c7673 Mon Sep 17 00:00:00 2001 From: tanglong <842690096@qq.com> Date: Mon, 13 Jul 2026 17:50:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=88=E6=9D=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coze/client/CozeWorkflowClient.java | 21 +++++++++++++++++-- src/main/resources/application.yml | 2 +- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/company/agentfoundation/integration/coze/client/CozeWorkflowClient.java b/src/main/java/com/company/agentfoundation/integration/coze/client/CozeWorkflowClient.java index 43ee28a..e00f937 100644 --- a/src/main/java/com/company/agentfoundation/integration/coze/client/CozeWorkflowClient.java +++ b/src/main/java/com/company/agentfoundation/integration/coze/client/CozeWorkflowClient.java @@ -6,6 +6,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Mono; import reactor.util.retry.Retry; import java.time.Duration; @@ -26,10 +27,26 @@ public class CozeWorkflowClient { return webClient.post() .uri(properties.getCoze().getChatPath()) .header(HttpHeaders.AUTHORIZATION, "Bearer " + properties.getCoze().getApiKey()) + .accept(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_JSON) .bodyValue(request) - .retrieve() - .bodyToMono(String.class) + .exchangeToMono(response -> response.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))) .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) + "..."; + } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index df2ee1e..b8744f8 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -38,7 +38,7 @@ foundation: coze: base-url: https://api.coze.cn chat-path: /v1/workflows/chat - api-key: ${COZE_API_KEY:cztei_hTd3jrof7F2fpRM4rVvNiK1ipxaQl4rlvQNHKBDK1RlTgnyFO9UqwShkr8IXcuVrk} + api-key: ${COZE_API_KEY:cztei_l0qEUEjq0o8KUTatjnvaZkKdaOwvhBMzu3cmkI0qLmjQwz4bAhGrHrs4l46I61xbY} default-workflow: wecom-default workflows: wecom-default: