Bladeren bron

评估:评估AI助手修复

Alex 1 week geleden
bovenliggende
commit
fc572bfe1d

+ 1 - 2
docs/sql/addsql/20261118.sql

@@ -13,5 +13,4 @@ ADD INDEX `idx_mbpr_mode_year_version_time` (`del_flag`,`mode_code`,`particular_
 ADD INDEX `idx_mbpr_mode_province_time` (`del_flag`,`mode_code`,`province`,`create_time`,`id`,`material_name`),
 ADD INDEX `idx_mbpr_mode_province_city_time` (`del_flag`,`mode_code`,`province`,`city`,`create_time`,`id`,`material_name`),
 ADD INDEX `idx_mbpr_mode_fir_time` (`del_flag`,`mode_code`,`fir_catalog`,`create_time`,`id`,`material_name`),
-ADD INDEX `idx_mbpr_mode_fir_seco_time` (`del_flag`,`mode_code`,`fir_catalog`,`seco_catalog`,`create_time`,`id`,`material_name`)
-, ALGORITHM=INPLACE, LOCK=NONE;
+ADD INDEX `idx_mbpr_mode_fir_seco_time` (`del_flag`,`mode_code`,`fir_catalog`,`seco_catalog`,`create_time`,`id`,`material_name`), ALGORITHM=INPLACE, LOCK=NONE;

+ 43 - 20
jeeplus-modules/jeeplus-assess/src/main/java/com/jeeplus/assess/aiAssistant/client/WeKnoraEmbedClient.java

@@ -1,16 +1,22 @@
 package com.jeeplus.assess.aiAssistant.client;
 
 import cn.hutool.core.util.StrUtil;
-import cn.hutool.http.HttpRequest;
-import cn.hutool.http.HttpResponse;
 import com.alibaba.fastjson.JSON;
 import com.jeeplus.assess.aiAssistant.client.dto.EmbedExchangeResp;
 import com.jeeplus.assess.aiAssistant.config.WeKnoraProperties;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
+import java.nio.charset.StandardCharsets;
 
 /**
  * WeKnora Embed 客户端:只做「用发布令牌 em_ 换短期令牌 ems_」这一件事。
@@ -60,36 +66,53 @@ public class WeKnoraEmbedClient {
         if (StringUtils.isBlank(publishToken)) {
             throw new IllegalStateException("知识助手渠道未配置发布令牌(weknora.channels[].publish-token)");
         }
+        String origin = StringUtils.trimToNull(weKnoraProperties.getAllowedOrigin());
+        if (origin == null) {
+            throw new IllegalStateException("知识助手未配置 weknora.allowed-origin(须与前端页面 Origin 逐字一致,如 http://localhost:2800)");
+        }
         String url = StrUtil.removeSuffix(weKnoraProperties.getBaseUrl(), "/")
                 + "/api/v1/embed/" + channelId + "/exchange";
         long begin = System.currentTimeMillis();
-        HttpResponse response;
-        try {
-            response = HttpRequest.post(url)
-                    .header("Authorization", "Embed " + publishToken)
-                    .header("Origin", weKnoraProperties.getAllowedOrigin())
-                    .header("Content-Type", "application/json")
-                    .body("{}", "application/json")
-                    .setConnectionTimeout(CONNECT_TIMEOUT_MS)
-                    .setReadTimeout(READ_TIMEOUT_MS)
-                    .execute();
+        int status;
+        String body;
+        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
+            HttpPost post = new HttpPost(url);
+            post.setHeader("Authorization", "Embed " + publishToken);
+            // 这一行是整件事的成败点:HttpClient 会真的把它发出去,HttpURLConnection 不会
+            post.setHeader("Origin", origin);
+            post.setHeader("Accept", "application/json");
+            post.setHeader("Content-Type", "application/json");
+            post.setEntity(new StringEntity("{}", StandardCharsets.UTF_8));
+            post.setConfig(RequestConfig.custom()
+                    .setConnectTimeout(CONNECT_TIMEOUT_MS)
+                    .setSocketTimeout(READ_TIMEOUT_MS)
+                    .build());
+            log.info("知识助手换令牌请求 channelId={} origin={}", channelId, origin);
+            try (CloseableHttpResponse response = httpClient.execute(post)) {
+                status = response.getStatusLine().getStatusCode();
+                body = response.getEntity() == null ? "" : EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
+            }
         } catch (Exception e) {
-            log.error("知识助手换令牌异常 channelId={} elapsedMs={}", channelId, System.currentTimeMillis() - begin, e);
+            log.error("知识助手换令牌异常 channelId={} origin={} elapsedMs={}",
+                    channelId, origin, System.currentTimeMillis() - begin, e);
             throw new IllegalStateException("知识助手服务暂时不可用,请稍后重试");
         }
 
         long elapsed = System.currentTimeMillis() - begin;
-        if (!response.isOk()) {
-            // 只记状态码与截断后的错误体;403 基本都是 Origin 没命中渠道白名单(方案 §12 第 7 步)
-            log.error("知识助手换令牌失败 channelId={} status={} elapsedMs={} body={}",
-                    channelId, response.getStatus(), elapsed,
-                    StrUtil.maxLength(response.body(), ERROR_BODY_LOG_LIMIT));
-            throw new IllegalStateException("知识助手服务返回 HTTP " + response.getStatus());
+        if (status != 200) {
+            // 只记状态码与截断后的错误体;403 基本都是 Origin 没命中渠道白名单
+            log.error("知识助手换令牌失败 channelId={} origin={} status={} elapsedMs={} body={}",
+                    channelId, origin, status, elapsed, StrUtil.maxLength(body, ERROR_BODY_LOG_LIMIT));
+            if (status == 403) {
+                throw new IllegalStateException("知识助手拒绝访问:渠道白名单未包含 Origin="
+                        + origin + "(请核对 WeKnora 渠道 allowed_origins 与此值逐字一致)");
+            }
+            throw new IllegalStateException("知识助手服务返回 HTTP " + status);
         }
 
         EmbedExchangeResp resp;
         try {
-            resp = JSON.parseObject(response.body(), EmbedExchangeResp.class);
+            resp = JSON.parseObject(body, EmbedExchangeResp.class);
         } catch (Exception e) {
             log.error("知识助手换令牌响应无法解析 channelId={} elapsedMs={}", channelId, elapsed, e);
             throw new IllegalStateException("知识助手服务响应格式异常");