From 3c852d44463be24222577fdfbd2751b96cd641f3 Mon Sep 17 00:00:00 2001 From: liuzh Date: Thu, 2 Apr 2026 00:53:17 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B7=A5=E5=85=B7=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E5=90=8E=E7=BB=AD=E6=96=87=E6=9C=AC=E7=BC=A9=E8=BF=9B=20-=20st?= =?UTF-8?q?reamNewLine=E5=AD=97=E6=AE=B5=E5=85=B1=E4=BA=AB=E7=8A=B6?= =?UTF-8?q?=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 isNewLine 局部变量提升为 streamNewLine 实例字段 - 工具 START/END 事件回调中同步标记换行状态 - 确保工具调用后的 AI 续文也有 4 空格缩进对齐 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../java/com/claudecode/repl/ReplSession.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/claudecode/repl/ReplSession.java b/src/main/java/com/claudecode/repl/ReplSession.java index 56f10a4..5ba7df3 100644 --- a/src/main/java/com/claudecode/repl/ReplSession.java +++ b/src/main/java/com/claudecode/repl/ReplSession.java @@ -57,6 +57,9 @@ public class ReplSession { private String conversationSummary = ""; private volatile boolean running = true; + /** 流式输出换行跟踪:工具渲染和流式回调共享,保证缩进一致 */ + private volatile boolean streamNewLine = false; + /** 当前活跃的 LineReader(JLine 模式下用于 AskUser 和权限确认) */ private volatile LineReader activeReader; /** 当前活跃的 Scanner(Scanner 模式下用于 AskUser 和权限确认) */ @@ -100,8 +103,12 @@ public class ReplSession { case START -> { spinner.stop(); toolStatusRenderer.renderStart(event.toolName(), event.arguments()); + streamNewLine = true; // 工具渲染输出以 println 结尾 + } + case END -> { + toolStatusRenderer.renderEnd(event.toolName(), event.result()); + streamNewLine = true; // 工具渲染输出以 println 结尾,标记下一个流式 token 需要缩进 } - case END -> toolStatusRenderer.renderEnd(event.toolName(), event.result()); } }); @@ -330,19 +337,19 @@ public class ReplSession { // AI 回复前的 ● 标识(不换行,后续流式文本紧跟其后) out.print(AnsiStyle.BRIGHT_CYAN + " ● " + AnsiStyle.RESET); + streamNewLine = false; // 重置换行跟踪 // 流式回调:逐 token 输出到终端,自动在每行开头加缩进 - final boolean[] isNewLine = {false}; // 跟踪是否刚输出换行符 String response = agentLoop.runStreaming(input, token -> { for (int i = 0; i < token.length(); i++) { char c = token.charAt(i); if (c == '\n') { out.println(); - isNewLine[0] = true; + streamNewLine = true; } else { - if (isNewLine[0]) { + if (streamNewLine) { out.print(" "); // 续行缩进(与 ● 后文本对齐) - isNewLine[0] = false; + streamNewLine = false; } out.print(c); }