feat(streaming): token-level delta events + interrupt for CLI/WebUI/GUI

Expose the LLM token-level streaming deltas (EventReasoningDelta /
EventContentDelta) to every client channel and add user-initiated
interrupt (cancel generation / send interrupt message) to all three
frontends, preserving the existing interrupt-injection semantics.

SDK/events:
  - EventReasoningDelta, EventContentDelta constants exported in the
    public/internal SDK event alias tables.

CLI plugin:
  - handleChat subscribes to both delta events and forwards
    reasoning_delta / content_delta JSON frames (channel-filtered);
    aggregated reasoning/tool_call/response frames still fire as before.
  - New /stop (alias /interrupt) builtin injects an interrupt via
    InjectInterrupt(cliSource, cliChannel) - matches interceptLoop
    semantics: cancels an active stream and re-injects the message as
    a [中断消息] for a restarted turn; with no active LLM it behaves
    as a plain input.

Waiter client (line mode + TUI):
  - streamRender accumulates delta chunks and redraws the current line;
    a reset frame (stream abandoned, e.g. user interrupt) flushes the
    partial buffer so the next turn does not concatenate onto stale
    content. Aggregated frames terminate the delta line and render the
    final text (old servers without deltas behave exactly as before).
  - TUI merges content_delta into the in-flight agent message and seals
    it (final flag) on response/tool_call/error so subsequent deltas
    never append to a finished message.

WebUI:
  - SSE handler subscribes to the two delta events but does NOT record
    them into the replay ring - reconnection replays only aggregated
    events (the final truth), avoiding duplicate delta accumulation.
  - POST /api/v1/chat/interrupt calls InjectInterrupt(webui, webui)
    with optional message; fronted by a Stop button shown only while
    a generation is in flight.

dashboard.html / GUI app.js:
  - Stop button next to Send (hidden until chatLoading); interruptChat
    POSTs /chat/interrupt. Delta listeners append incrementally;
    agent_output (aggregated) now replaces (not appends) the in-flight
    content and marks _final; reset frames finalize the partial message.

process.go:
  - chatStreamWithFallback preserves the context.Canceled/
    DeadlineExceeded contract: a user interrupt returns the canceled
    error (never a partial-content success) so the existing continue
    branch restarts the turn with the [中断消息]. A reset
    EventContentDelta is published so connected clients drop stale
    partial renderings before the new turn begins.

Verified: /stop 'msg' via waiter triggers 'interrupt from cli/cli' in
interceptLoop; unit TestChatStreamCancelPreservesInterrupt confirms the
canceled error propagates instead of being swallowed.
This commit is contained in:
JianFeeeee
2026-08-25 10:50:37 +08:00
parent 28a6d3f09c
commit 061d2ae320
11 changed files with 423 additions and 7 deletions

View File

@ -1166,6 +1166,9 @@ function buildChatLayout() {
'<input id="chat-input" placeholder="' +
__("输入消息...", "Type a message...") +
'" onkeydown="if(event.key==\'Enter\')sendChat()">' +
'<button class="btn" onclick="interruptChat()" id="chat-stop-btn" style="display:none;background:#d1383d;color:#fff">' +
__("停止", "Stop") +
"</button>" +
'<button class="btn btn-primary" onclick="sendChat()" id="chat-send-btn">' +
__("发送", "Send") +
"</button>" +
@ -2036,6 +2039,7 @@ function buildChatStarmapGraph() {
async function sendChat() {
var inp = document.getElementById("chat-input");
var btn = document.getElementById("chat-send-btn");
var stopBtn = document.getElementById("chat-stop-btn");
var text = inp.value.trim();
if (!text || state.chatLoading) return;
if (state.currentConn && state.currentConn.type === "cli") {
@ -2057,6 +2061,7 @@ async function sendChat() {
state.chatStage = __("等待AI回复...", "Waiting for AI...");
btn.disabled = true;
btn.textContent = "";
if (stopBtn) stopBtn.style.display = ""; // 生成期间可停止
rerenderChat();
try {
// webui 连接:同步 POST 等完整回复(服务端 X-Trigger-Only 也返回 responseSSE 公网不稳时靠同步兜底)
@ -2124,10 +2129,26 @@ async function sendChat() {
state.chatStage = "";
btn.disabled = false;
btn.textContent = __("发送", "Send");
var sb2 = document.getElementById("chat-stop-btn");
if (sb2) sb2.style.display = "none"; // 回复完成/失败,隐藏停止按钮
rerenderChat();
}
}
// 停止生成 / 发送中断消息。核心拦截语义:有 LLM 在跑则取消当前请求并以
// [中断消息] 重启轮次;无则在跑则作为普通消息处理。
async function interruptChat() {
try {
await api("/chat/interrupt", {
method: "POST",
body: JSON.stringify({}),
});
toast(__("已发送中断信号", "Interrupt signal sent"));
} catch (e) {
toast(__("中断失败: ", "Interrupt failed: ") + e.message, true);
}
}
async function queryMemoryChat() {
var q = document.getElementById("mem-query")?.value;
var r = document.getElementById("mem-result-chat");

View File

@ -12,6 +12,7 @@ func handleBuiltin(cmd string, cfg *Config, state *State, reconnect func(), out
case cmd == "/help":
fmt.Fprintln(out, `Built-in commands:
/help show this help
/stop [msg] stop generation / send interrupt (alias /interrupt)
/exit, /quit exit waiter
/clear clear screen
/reconnect force reconnection

View File

@ -202,10 +202,15 @@ func runInteractive(state *State, cfg *Config) {
}
func oneshot(state *State, msg string) {
var sr streamRender
stop := startSpinner("thinking...")
resp, err := state.SendChatStream(msg, func(rl respLine) {
// 第一个过程帧到达即停转,后续帧直接渲染
stop()
if sr.handleDelta(rl) {
return // delta 已增量渲染
}
sr.reset() // 聚合帧/工具帧:结束 delta 流,换行输出
printServerEvent(rl)
})
stop()
@ -347,6 +352,56 @@ loop:
}
}
// streamRender 累积 token 级 delta 帧并增量重绘当前行。
// 聚合帧reasoning/tool_call/response到达时清空累积状态该轮已结束
// 旧服务器不发 delta此结构始终为空行为与原来完全一致。
type streamRender struct {
reasoning strings.Builder
content strings.Builder
}
// handleDelta 处理 delta 帧;返回是否消费了该帧。
// reset=true 的空帧表示服务端轮次作废(用户中断):清空累积并定格已显示内容。
func (sr *streamRender) handleDelta(rl respLine) bool {
switch rl.Type {
case "reasoning_delta":
if rl.Reset {
sr.reasoning.Reset()
fmt.Print(clearLine)
return true
}
sr.reasoning.WriteString(rl.Content)
if colors {
fmt.Printf("%s%s· %s%s", clearLine, colorDim, sr.reasoning.String(), colorReset)
} else {
fmt.Printf("%s[思考] %s", clearLine, sr.reasoning.String())
}
return true
case "content_delta":
if rl.Reset {
sr.content.Reset()
fmt.Print(clearLine + "\n") // 定格已显示的部分内容,换行
return true
}
sr.content.WriteString(rl.Content)
if colors {
fmt.Printf("%s%s%s%s", clearLine, colorGreen, sr.content.String(), colorReset)
} else {
fmt.Printf("%s%s", clearLine, sr.content.String())
}
return true
}
return false
}
// reset 在收到聚合帧/工具帧时调用delta 流被打断或结束,
// 下一行输出不再覆盖 delta 内容。
func (sr *streamRender) reset() {
sr.reasoning.Reset()
sr.content.Reset()
fmt.Print(clearLine + "\n")
}
// printServerOutput 渲染一行服务器输出JSON 帧)。
func printServerOutput(content string) {
rl := parseRespLineStruct(content)
@ -369,8 +424,10 @@ func printServerEvent(rl respLine) {
// renderPlain 无色模式下的纯文本渲染。
func renderPlain(rl respLine, raw string) string {
switch rl.Type {
case "reasoning":
case "reasoning", "reasoning_delta":
return "[思考] " + rl.Content
case "content_delta":
return rl.Content
case "tool_call":
return fmt.Sprintf("[工具] %s (%s) %s", rl.Tool, rl.Status, rl.Result)
case "response":

View File

@ -126,6 +126,7 @@ type respLine struct {
Tool string `json:"tool"`
Status string `json:"status"`
Result string `json:"result"`
Reset bool `json:"reset,omitempty"` // delta 帧:服务端轮次作废,清空累积
}
// parseRespLineStruct 解析一行 JSON 响应帧,解析失败时将原文放入 Content。

View File

@ -69,6 +69,7 @@ type chatMsg struct {
tool string // msgTool: 工具名
status string // msgTool: ok/denied/interrupted/error/running
result string // msgTool: 结果预览
final bool // msgAgent: 流式消息已完成(后续 delta 不再追加)
}
// ---- tea.Msg ----
@ -375,18 +376,61 @@ func (m *tuiModel) handleServerLine(line string) {
} else {
m.append(cm)
}
case "reasoning_delta":
// token 级增量:与 reasoning 同样合并到最后一条 reasoning 消息
if rl.Reset {
m.messages = []chatMsg{}
break
}
if n := len(m.messages); n > 0 && m.messages[n-1].kind == msgReasoning {
m.messages[n-1].text += rl.Content
} else if rl.Content != "" {
m.append(chatMsg{kind: msgReasoning, text: rl.Content})
}
case "content_delta":
// token 级增量:追加到最后一条 agent 消息(流式生成中的回复)
if rl.Reset {
m.sealLastAgent()
break
}
if n := len(m.messages); n > 0 && m.messages[n-1].kind == msgAgent && !m.messages[n-1].final {
m.messages[n-1].text += rl.Content
} else if rl.Content != "" {
m.append(chatMsg{kind: msgAgent, text: rl.Content})
}
case "tool_call":
// 工具调用打断内容流:置 final 防止后续 delta 误追加到旧消息
m.sealLastAgent()
m.append(chatMsg{kind: msgTool, tool: rl.Tool, status: rl.Status, result: rl.Result})
case "response":
// 聚合最终响应:覆盖/替换 delta 累积的最后一条 agent 消息(内容相同),
// 或在无 delta 时新建。置 final 标记本轮完成。
m.busy = false
m.append(chatMsg{kind: msgAgent, text: rl.Content})
if n := len(m.messages); n > 0 && m.messages[n-1].kind == msgAgent && !m.messages[n-1].final {
if rl.Content != "" {
m.messages[n-1].text = rl.Content // 以聚合为准(含 stage 插件改写后的最终文本)
}
m.messages[n-1].final = true
} else {
cm := chatMsg{kind: msgAgent, text: rl.Content, final: true}
m.append(cm)
}
case "error":
m.busy = false
m.sealLastAgent()
m.append(chatMsg{kind: msgError, text: rl.Error})
default:
// 非 JSON 旧行(旧服务器):当最终输出
m.busy = false
m.append(chatMsg{kind: msgAgent, text: line})
m.append(chatMsg{kind: msgAgent, text: line, final: true})
}
m.refreshViewport()
}
// sealLastAgent 将最后一条未完成的 agent 流式消息标记为完成。
func (m *tuiModel) sealLastAgent() {
if n := len(m.messages); n > 0 && m.messages[n-1].kind == msgAgent {
m.messages[n-1].final = true
}
}