This commit is contained in:
luke 2025-06-23 18:27:28 +08:00
parent 69a71dfa43
commit 09b2db0df3
2 changed files with 2 additions and 14 deletions

View File

@ -29,7 +29,6 @@ public class LLMAppServiceImpl implements LLMAppService {
try {
FilteredSseOutputAdapter adapter = new FilteredSseOutputAdapter(emitter);
llmServiceFactory.current().streamAnswer(llmToken, question, params, adapter);
adapter.flushLastChunk(); // 收尾发一次 sources
emitter.complete();
} catch (Exception e) {
log.error("LLM调用异常", e);

View File

@ -51,27 +51,16 @@ public class FilteredSseOutputAdapter implements WriterAdapter {
// 合并缓存中的 sources
filtered.putAll(lastChunk);
lastChunk.clear(); // 清空缓存
String payload = mapper.writeValueAsString(filtered);
emitter.send("data: " + payload + "\n\n");
emitter.send(SseEmitter.event().data(payload));
} else if (!filtered.isEmpty()) {
// 普通中间片段
String payload = mapper.writeValueAsString(filtered);
emitter.send("data: " + payload + "\n\n");
emitter.send(SseEmitter.event().data(payload));
}
} catch (Exception e) {
log.warn("SSE数据解析失败{}", line, e);
}
}
public void flushLastChunk() {
if (lastChunk.isEmpty()) return;
try {
String finalJson = mapper.writeValueAsString(lastChunk);
emitter.send("data: " + finalJson + "\n\n");
} catch (Exception e) {
log.warn("flush 最后一条 SSE 失败", e);
}
}
}