fix: 透传 lpt-ai 的失败原因,避免误导成「AI 没有回应」
This commit is contained in:
@@ -22,6 +22,10 @@ import java.util.Optional;
|
|||||||
/**
|
/**
|
||||||
* lpt-ai 独立 AI 服务客户端。
|
* lpt-ai 独立 AI 服务客户端。
|
||||||
* <p>通过 {@code lpt.ai-service.url} 配置服务地址;未配置或调用失败时返回 empty,由调用方降级。</p>
|
* <p>通过 {@code lpt.ai-service.url} 配置服务地址;未配置或调用失败时返回 empty,由调用方降级。</p>
|
||||||
|
* <p>唯一例外是 lpt-ai <b>明确回报任务失败</b>({@code status=failed}):此时抛出
|
||||||
|
* {@link AiServiceTaskFailedException},把服务商给出的真实原因(鉴权、额度等)透传给用户,
|
||||||
|
* 而不是退化成一句无从下手的「AI 没有回应」。有降级形态的调用方(报告草稿、导图、回忆对比)
|
||||||
|
* 仍在各自 catch 里降级,行为不变。</p>
|
||||||
* <p>
|
* <p>
|
||||||
* 内部使用异步任务模式:提交任务到 lpt-ai 后轮询等待结果,
|
* 内部使用异步任务模式:提交任务到 lpt-ai 后轮询等待结果,
|
||||||
* 避免同步等待 LLM 响应时阻塞 HTTP 连接。
|
* 避免同步等待 LLM 响应时阻塞 HTTP 连接。
|
||||||
@@ -67,6 +71,21 @@ public class AiServiceClient {
|
|||||||
return token == null ? "" : token.trim();
|
return token == null ? "" : token.trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* lpt-ai 明确回报任务失败时抛出,{@code message} 是它(通常是 LLM 服务商)给出的真实原因。
|
||||||
|
*
|
||||||
|
* <p>与「服务不可达 / 超时 / 响应缺字段」区分开:那些属于临时故障,调用方降级或提示重试即可;
|
||||||
|
* 而这里是确定性原因(例如 {@code LLM 请求失败 (402): 账户余额不足}),只有把它透传出去才能定位。</p>
|
||||||
|
*
|
||||||
|
* <p>继承 {@link AiTaskFailedException},因此从 AI 任务处理器抛出后,
|
||||||
|
* 执行器会按既有逻辑把 message 写进任务表并展示给用户。</p>
|
||||||
|
*/
|
||||||
|
public static class AiServiceTaskFailedException extends AiTaskFailedException {
|
||||||
|
public AiServiceTaskFailedException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ============ 公开 API(签名不变,内部改为 submit + poll)============
|
// ============ 公开 API(签名不变,内部改为 submit + poll)============
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -132,8 +151,9 @@ public class AiServiceClient {
|
|||||||
/**
|
/**
|
||||||
* 调用 lpt-ai 进行学习引导的一轮对话(复习-06)。
|
* 调用 lpt-ai 进行学习引导的一轮对话(复习-06)。
|
||||||
*
|
*
|
||||||
* <p>学习引导允许完全依赖 AI:这条链路没有降级形态,AI 不可用时返回 empty,
|
* <p>学习引导允许完全依赖 AI:这条链路没有降级形态,拿不到回复即失败。
|
||||||
* 由调用方把任务标记为失败,前端据此把入口置为不可用。</p>
|
* 若失败原因是 lpt-ai 明确回报的(如额度、鉴权),异常会向上抛出让用户看到真实原因;
|
||||||
|
* 其余情况返回 empty,由调用方以可重试的提示文案标记失败。</p>
|
||||||
*
|
*
|
||||||
* @param history 之前的轮次(role/content),作为多轮上下文
|
* @param history 之前的轮次(role/content),作为多轮上下文
|
||||||
*/
|
*/
|
||||||
@@ -149,6 +169,9 @@ public class AiServiceClient {
|
|||||||
);
|
);
|
||||||
Optional<JsonNode> result = submitAndWait("guidance", params);
|
Optional<JsonNode> result = submitAndWait("guidance", params);
|
||||||
return result.map(r -> r.path("reply").asText(null)).filter(s -> s != null && !s.isBlank());
|
return result.map(r -> r.path("reply").asText(null)).filter(s -> s != null && !s.isBlank());
|
||||||
|
} catch (AiServiceTaskFailedException e) {
|
||||||
|
// 服务商给了确定原因:把原因交给用户,不要退化成「AI 没有回应」
|
||||||
|
throw e;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("lpt-ai 学习引导异常: {}", e.getMessage());
|
log.warn("lpt-ai 学习引导异常: {}", e.getMessage());
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
@@ -261,7 +284,8 @@ public class AiServiceClient {
|
|||||||
case "failed":
|
case "failed":
|
||||||
String err = json.path("error").asText("未知错误");
|
String err = json.path("error").asText("未知错误");
|
||||||
log.warn("lpt-ai 任务失败: taskId={}, error={}", taskId, err);
|
log.warn("lpt-ai 任务失败: taskId={}, error={}", taskId, err);
|
||||||
return Optional.empty();
|
// 带上真实原因向上抛:调用方要么降级,要么把它展示给用户
|
||||||
|
throw new AiServiceTaskFailedException(err);
|
||||||
case "pending":
|
case "pending":
|
||||||
case "running":
|
case "running":
|
||||||
// 还在处理,继续轮询
|
// 还在处理,继续轮询
|
||||||
@@ -270,6 +294,9 @@ public class AiServiceClient {
|
|||||||
log.warn("lpt-ai 未知任务状态: taskId={}, status={}", taskId, status);
|
log.warn("lpt-ai 未知任务状态: taskId={}, status={}", taskId, status);
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
} catch (AiServiceTaskFailedException e) {
|
||||||
|
// 明确的失败原因:直接向上抛,不要当成「轮询异常」重试到超时
|
||||||
|
throw e;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("lpt-ai 轮询异常 (第{}次): {}", pollCount, e.getMessage());
|
log.warn("lpt-ai 轮询异常 (第{}次): {}", pollCount, e.getMessage());
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-2
@@ -27,7 +27,9 @@ import java.util.Optional;
|
|||||||
* <p>每一轮对话是一个 AI 任务:提交用户的这一轮,执行器在请求线程之外调用 AI 并把回复写回对话。
|
* <p>每一轮对话是一个 AI 任务:提交用户的这一轮,执行器在请求线程之外调用 AI 并把回复写回对话。
|
||||||
* 这样多轮对话不需要前端在请求线程里干等,也不受 AI-01 的事务约束。</p>
|
* 这样多轮对话不需要前端在请求线程里干等,也不受 AI-01 的事务约束。</p>
|
||||||
*
|
*
|
||||||
* <p>这条链路没有降级形态:AI 不可用时直接失败,前端把入口置为不可用。</p>
|
* <p>这条链路没有降级形态:AI 拿不到回复就直接失败。若 lpt-ai 回报了确定原因
|
||||||
|
* (例如 LLM 额度不足、鉴权失败),该原因会原样写进任务表并展示给用户;
|
||||||
|
* 其余临时故障才用可重试的提示文案。</p>
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@@ -63,7 +65,7 @@ public class GuidanceAiTaskHandler implements AiTaskHandler {
|
|||||||
List<Map<String, String>> history = readHistory(conversation);
|
List<Map<String, String>> history = readHistory(conversation);
|
||||||
|
|
||||||
Optional<String> reply = aiServiceClient.guidance(taskName, conversation.getConcept(), history);
|
Optional<String> reply = aiServiceClient.guidance(taskName, conversation.getConcept(), history);
|
||||||
String text = reply.orElseThrow(() -> new AiTaskFailedException("AI 这次没有给出回应,请重试"));
|
String text = reply.orElseThrow(() -> new AiTaskFailedException("AI 暂时没有回应,请稍后再试"));
|
||||||
|
|
||||||
// 把 AI 的回复写回对话,供下一轮作为上下文
|
// 把 AI 的回复写回对话,供下一轮作为上下文
|
||||||
guidanceService.appendAssistantTurn(guidanceNum, text);
|
guidanceService.appendAssistantTurn(guidanceNum, text);
|
||||||
|
|||||||
@@ -229,4 +229,46 @@ class AiServiceClientTest extends TestHttpServer {
|
|||||||
// 503 → 退避 2s → continue → deadline 过期退出
|
// 503 → 退避 2s → continue → deadline 过期退出
|
||||||
assertTrue(elapsed >= 2000, "应等待退避后超时退出,实际 " + elapsed + "ms");
|
assertTrue(elapsed >= 2000, "应等待退避后超时退出,实际 " + elapsed + "ms");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ============ 失败原因透传(额度/鉴权等问题不再被吞掉)============
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void guidance_pollFailed_throwsWithProviderReason() {
|
||||||
|
route("POST", "/ai/tasks", 201, "{\"taskId\":\"tg1\"}");
|
||||||
|
route("GET", "/ai/tasks/tg1", 200,
|
||||||
|
"{\"status\":\"failed\",\"error\":\"LLM 请求失败 (402): 账户余额不足\"}");
|
||||||
|
AiServiceClient client = newClient(baseUrl(), 2);
|
||||||
|
|
||||||
|
AiServiceClient.AiServiceTaskFailedException ex = assertThrows(
|
||||||
|
AiServiceClient.AiServiceTaskFailedException.class,
|
||||||
|
() -> client.guidance("任务A", "组合式 API", List.of()));
|
||||||
|
|
||||||
|
assertEquals("LLM 请求失败 (402): 账户余额不足", ex.getMessage(),
|
||||||
|
"学习引导必须把服务商的原话带出来,而不是退化成通用文案");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void guidance_pollFailedWithoutErrorField_throws() {
|
||||||
|
route("POST", "/ai/tasks", 201, "{\"taskId\":\"tg2\"}");
|
||||||
|
route("GET", "/ai/tasks/tg2", 200, "{\"status\":\"failed\"}");
|
||||||
|
AiServiceClient client = newClient(baseUrl(), 2);
|
||||||
|
|
||||||
|
AiServiceClient.AiServiceTaskFailedException ex = assertThrows(
|
||||||
|
AiServiceClient.AiServiceTaskFailedException.class,
|
||||||
|
() -> client.guidance("任务A", "概念", List.of()));
|
||||||
|
|
||||||
|
assertEquals("未知错误", ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void degradingApis_pollFailed_stillReturnEmpty() {
|
||||||
|
// 有降级形态的三个入口行为不变:失败原因只写日志,仍然返回 empty 交由调用方降级
|
||||||
|
route("POST", "/ai/tasks", 201, "{\"taskId\":\"tg3\"}");
|
||||||
|
route("GET", "/ai/tasks/tg3", 200, "{\"status\":\"failed\",\"error\":\"模型超时\"}");
|
||||||
|
AiServiceClient client = newClient(baseUrl(), 2);
|
||||||
|
|
||||||
|
assertTrue(client.aggregateReport("任务A", List.of("片段"), null).isEmpty());
|
||||||
|
assertTrue(client.generateMindMap("任务A", "描述", List.of("报告内容")).isEmpty());
|
||||||
|
assertTrue(client.compareRecall("任务A", "标准大纲", "回忆大纲").isEmpty());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+33
@@ -229,4 +229,37 @@ class GuidanceIntegrationTest {
|
|||||||
assertEquals(AiTaskStatusEnum.FAILED.name(), done.getStatus(),
|
assertEquals(AiTaskStatusEnum.FAILED.name(), done.getStatus(),
|
||||||
"学习引导没有降级形态:AI 不可用时该链路直接失败");
|
"学习引导没有降级形态:AI 不可用时该链路直接失败");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 回归:lpt-ai 明确回报失败(如 LLM 额度/鉴权问题)时,真实原因必须落到任务表。
|
||||||
|
* 之前它在 AiServiceClient 里被丢掉,用户只能看到「AI 没有给出回应」,无从下手。
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void guidance_providerReasonIsRecordedInTaskError() throws Exception {
|
||||||
|
String reason = "LLM 请求失败 (402): 账户余额不足";
|
||||||
|
when(aiServiceClient.guidance(anyString(), anyString(), any()))
|
||||||
|
.thenThrow(new AiServiceClient.AiServiceTaskFailedException(reason));
|
||||||
|
String taskNum = saveTask();
|
||||||
|
ReviewGuidanceConversationEntity conversation = guidanceService.start(taskNum, "某概念");
|
||||||
|
|
||||||
|
AiTaskSubmitRequest request = new AiTaskSubmitRequest();
|
||||||
|
request.setType("GUIDANCE");
|
||||||
|
request.setTargetNum(conversation.getGuidanceNum());
|
||||||
|
AiTaskResponse submitted = aiTaskService.submit(request);
|
||||||
|
|
||||||
|
AiTaskResponse done = null;
|
||||||
|
for (int i = 0; i < 150; i++) {
|
||||||
|
Thread.sleep(50);
|
||||||
|
done = aiTaskService.getByTaskNum(submitted.getTaskNum());
|
||||||
|
if (AiTaskStatusEnum.SUCCEEDED.name().equals(done.getStatus())
|
||||||
|
|| AiTaskStatusEnum.FAILED.name().equals(done.getStatus())) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertNotNull(done);
|
||||||
|
assertEquals(AiTaskStatusEnum.FAILED.name(), done.getStatus());
|
||||||
|
assertEquals(reason, done.getErrorMessage(),
|
||||||
|
"服务商给出的真实原因应原样写进任务表,不能被替换成通用文案");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user