Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

refactor(httpapi): merge assistantStreamFilter answer/thinking text accumulation and extract acceptTool (#624) (#626)

fx-kurumi56cf1c93a26c
diff --git a/internal/httpapi/assistant_stream.go b/internal/httpapi/assistant_stream.go
index 10def382..41ffe0a4 100644
--- a/internal/httpapi/assistant_stream.go
+++ b/internal/httpapi/assistant_stream.go
@@ -35,70 +35,68 @@ func (f *assistantStreamFilter) accept(ev assistantStreamEvent) bool {
f.lastStatus = ev.Text
return true
case "answer":
- if ev.Text == "" {
- return false
- }
- if ev.Full {
- // 增量已经流过同样的文本时丢弃整段重复块。
- if f.answer != "" && strings.Contains(f.answer, strings.TrimSpace(ev.Text)) {
- return false
- }
- f.answer += ev.Text
- return true
- }
- f.answer += ev.Text
- return true
+ return appendStreamText(&f.answer, ev)
case "tool":
f.inReply = false
- if assistantProtocolTool(ev.Tool) {
- return false
- }
- if ev.ID != "" {
- if f.seenTools == nil {
- f.seenTools = map[string]string{}
- }
- // The CLI announces a call with empty input and repeats it once
- // the input is complete. Keep the repeat only when it adds
- // something: the detail, or an Agent call's type and task.
- seen := ev.Detail + "\x00" + ev.Subagent + "\x00" + ev.Input + "\x00" + ev.AgentID
- if previous, ok := f.seenTools[ev.ID]; ok {
- if seen == previous || (ev.Detail == "" && ev.Subagent == "" && ev.Input == "") {
- return false
- }
- }
- f.seenTools[ev.ID] = seen
- f.lastTool, f.lastDetail = ev.Tool, ev.Detail
- return true
- }
- if ev.Tool == "" {
- return false
- }
- if ev.Tool == f.lastTool && ev.Detail == f.lastDetail {
- return false
- }
- f.lastTool, f.lastDetail = ev.Tool, ev.Detail
- return true
+ return f.acceptTool(ev)
case "thinkingStart":
f.inReply = false
return true
case "toolDone":
return ev.ID != ""
case "thinking":
- if ev.Text == "" {
- return false
+ return appendStreamText(&f.thinking, ev)
+ default:
+ return ev.Text != "" || ev.Tool != ""
+ }
+}
+
+// appendStreamText accumulates an answer or thinking event into acc and
+// reports whether the event should be forwarded. Empty text is dropped, and a
+// Full block whose text the deltas already streamed is dropped as a repeat.
+func appendStreamText(acc *string, ev assistantStreamEvent) bool {
+ if ev.Text == "" {
+ return false
+ }
+ // 增量已经流过同样的文本时丢弃整段重复块。
+ if ev.Full && *acc != "" && strings.Contains(*acc, strings.TrimSpace(ev.Text)) {
+ return false
+ }
+ *acc += ev.Text
+ return true
+}
+
+// acceptTool dedupes tool announcements. Protocol tools are hidden; calls with
+// an ID are deduped per ID, and calls without one against the previous tool.
+func (f *assistantStreamFilter) acceptTool(ev assistantStreamEvent) bool {
+ if assistantProtocolTool(ev.Tool) {
+ return false
+ }
+ if ev.ID != "" {
+ if f.seenTools == nil {
+ f.seenTools = map[string]string{}
}
- if ev.Full {
- if f.thinking != "" && strings.Contains(f.thinking, strings.TrimSpace(ev.Text)) {
+ // The CLI announces a call with empty input and repeats it once
+ // the input is complete. Keep the repeat only when it adds
+ // something: the detail, or an Agent call's type and task.
+ seen := ev.Detail + "\x00" + ev.Subagent + "\x00" + ev.Input + "\x00" + ev.AgentID
+ if previous, ok := f.seenTools[ev.ID]; ok {
+ if seen == previous || (ev.Detail == "" && ev.Subagent == "" && ev.Input == "") {
return false
}
- f.thinking += ev.Text
- return true
}
- f.thinking += ev.Text
+ f.seenTools[ev.ID] = seen
+ f.lastTool, f.lastDetail = ev.Tool, ev.Detail
return true
- default:
- return ev.Text != "" || ev.Tool != ""
}
+ if ev.Tool == "" {
+ return false
+ }
+ if ev.Tool == f.lastTool && ev.Detail == f.lastDetail {
+ return false
+ }
+ f.lastTool, f.lastDetail = ev.Tool, ev.Detail
+ return true
}
// assistantSessionToolPrefix namespaces the platform's own session MCP tools.
diff --git a/internal/httpapi/assistant_stream_filter_test.go b/internal/httpapi/assistant_stream_filter_test.go
new file mode 100644
index 00000000..aafdb7e9
--- /dev/null
+++ b/internal/httpapi/assistant_stream_filter_test.go
@@ -0,0 +1,87 @@
+package httpapi
+
+import "testing"
+
+func TestAppendStreamTextDropsEmptyAndRepeatedFullBlocks(t *testing.T) {
+ var acc string
+ steps := []struct {
+ ev assistantStreamEvent
+ want bool
+ acc string
+ }{
+ {assistantStreamEvent{Text: ""}, false, ""},
+ {assistantStreamEvent{Text: "", Full: true}, false, ""},
+ {assistantStreamEvent{Text: "hello ", Full: true}, true, "hello "},
+ {assistantStreamEvent{Text: "world"}, true, "hello world"},
+ {assistantStreamEvent{Text: " hello world\n", Full: true}, false, "hello world"},
+ {assistantStreamEvent{Text: "!", Full: true}, true, "hello world!"},
+ }
+ for i, step := range steps {
+ if got := appendStreamText(&acc, step.ev); got != step.want {
+ t.Fatalf("step %d: appendStreamText = %v, want %v", i, got, step.want)
+ }
+ if acc != step.acc {
+ t.Fatalf("step %d: acc = %q, want %q", i, acc, step.acc)
+ }
+ }
+}
+
+func TestAssistantStreamFilterKeepsAnswerAndThinkingSeparate(t *testing.T) {
+ var f assistantStreamFilter
+ if !f.accept(assistantStreamEvent{Kind: "answer", Text: "plan"}) {
+ t.Fatal("answer delta dropped")
+ }
+ if !f.accept(assistantStreamEvent{Kind: "thinking", Text: "plan", Full: true}) {
+ t.Fatal("thinking full block deduped against answer text")
+ }
+ if f.accept(assistantStreamEvent{Kind: "answer", Text: "plan", Full: true}) {
+ t.Fatal("repeated answer full block kept")
+ }
+ if f.answer != "plan" || f.thinking != "plan" {
+ t.Fatalf("answer=%q thinking=%q", f.answer, f.thinking)
+ }
+}
+
+func TestAssistantStreamFilterAcceptToolDedupes(t *testing.T) {
+ var f assistantStreamFilter
+ steps := []struct {
+ name string
+ ev assistantStreamEvent
+ want bool
+ }{
+ {"protocol tool hidden", assistantStreamEvent{Tool: "mcp__agentflow_session__submit_assistant_reply", ID: "p1"}, false},
+ {"first announce", assistantStreamEvent{Tool: "Bash", ID: "t1"}, true},
+ {"empty repeat", assistantStreamEvent{Tool: "Bash", ID: "t1"}, false},
+ {"repeat adds detail", assistantStreamEvent{Tool: "Bash", ID: "t1", Detail: "ls"}, true},
+ {"identical repeat", assistantStreamEvent{Tool: "Bash", ID: "t1", Detail: "ls"}, false},
+ {"repeat with empty detail and input", assistantStreamEvent{Tool: "Bash", ID: "t1", AgentID: "a1"}, false},
+ {"agent call adds task", assistantStreamEvent{Tool: "Agent", ID: "t2", Subagent: "Explore", Input: "find x"}, true},
+ {"no id empty tool", assistantStreamEvent{}, false},
+ {"no id same as last", assistantStreamEvent{Tool: "Agent", Detail: ""}, false},
+ {"no id new detail", assistantStreamEvent{Tool: "Agent", Detail: "y"}, true},
+ {"no id consecutive repeat", assistantStreamEvent{Tool: "Agent", Detail: "y"}, false},
+ {"no id other tool", assistantStreamEvent{Tool: "Read", Detail: "y"}, true},
+ }
+ for _, step := range steps {
+ if got := f.acceptTool(step.ev); got != step.want {
+ t.Fatalf("%s: acceptTool = %v, want %v", step.name, got, step.want)
+ }
+ }
+ if f.lastTool != "Read" || f.lastDetail != "y" {
+ t.Fatalf("last tool = %q/%q", f.lastTool, f.lastDetail)
+ }
+}
+
+func TestAssistantStreamFilterToolEndsReplyDraft(t *testing.T) {
+ var f assistantStreamFilter
+ f.accept(assistantStreamEvent{Kind: "draftStart"})
+ if !f.accept(assistantStreamEvent{Kind: "draftDelta", Detail: "x"}) {
+ t.Fatal("draft delta dropped while in reply")
+ }
+ if f.accept(assistantStreamEvent{Kind: "tool", Tool: "submit_clarification"}) {
+ t.Fatal("protocol tool forwarded")
+ }
+ if f.accept(assistantStreamEvent{Kind: "draftDelta", Detail: "x"}) {
+ t.Fatal("draft delta kept after a tool event")
+ }
+}