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.
JavaScript agent-orchestrationagentic-workflowai-agentsai-codingclaude-codecode-reviewcodebuddycodexcoding-agentscursordeveloper-toolshuman-on-the-loopllmmcpmulti-agentsdlcself-hostedsopworkflow-automationworkflow-engine
refactor(httpapi): share SSE event cursor helpers in assistant_live.go (#366)
fx-kurumida3d290e6b4f
diff --git a/internal/httpapi/assistant_live.go b/internal/httpapi/assistant_live.goindex 0e5b88fd..b2148fd4 100644--- a/internal/httpapi/assistant_live.go+++ b/internal/httpapi/assistant_live.go@@ -149,19 +149,9 @@ func serveAssistantGenerationStream(c *gin.Context, persisted domain.AssistantGefirst = false} else {g.Stream = nil- events := make([]domain.AssistantGenerationEvent, 0)- for _, e := range g.Events {- if e.Sequence > cursor {- events = append(events, e)- }- }- g.Events = events- }- for _, e := range g.Events {- if e.Sequence > cursor {- cursor = e.Sequence- }+ g.Events = assistantEventsAfter(g.Events, cursor)}+ cursor = latestAssistantEventSequence(g.Events, cursor)writeAssistantSSE(c, g)if g.Status != "running" {return@@ -182,11 +172,7 @@ func streamPersistedAssistantGeneration(c *gin.Context, g domain.AssistantGeneraif g.Stream != nil {cursor = g.Stream.Sequence}- for _, e := range g.Events {- if e.Sequence > cursor {- cursor = e.Sequence- }- }+ cursor = latestAssistantEventSequence(g.Events, cursor)writeAssistantSSE(c, g)id := g.IDpoll := time.NewTicker(assistantPersistedPollInterval)@@ -218,13 +204,8 @@ func streamPersistedAssistantGeneration(c *gin.Context, g domain.AssistantGenerag.Events = nil} else {g.Stream = nil- events := make([]domain.AssistantGenerationEvent, 0)- for _, e := range g.Events {- if e.Sequence > cursor {- events = append(events, e)- cursor = e.Sequence- }- }+ events := assistantEventsAfter(g.Events, cursor)+ cursor = latestAssistantEventSequence(events, cursor)g.Events = eventsif len(events) == 0 && g.Status == "running" {continue@@ -236,3 +217,23 @@ func streamPersistedAssistantGeneration(c *gin.Context, g domain.AssistantGenera}}}++// assistantEventsAfter returns the events whose sequence is past cursor. The+// result is never nil so an empty batch still serializes as [].+func assistantEventsAfter(events []domain.AssistantGenerationEvent, cursor int) []domain.AssistantGenerationEvent {+ out := make([]domain.AssistantGenerationEvent, 0)+ for _, e := range events {+ if e.Sequence > cursor {+ out = append(out, e)+ }+ }+ return out+}++// latestAssistantEventSequence advances cursor to the highest event sequence.+func latestAssistantEventSequence(events []domain.AssistantGenerationEvent, cursor int) int {+ for _, e := range events {+ cursor = max(cursor, e.Sequence)+ }+ return cursor+}diff --git a/internal/httpapi/assistant_live_cursor_test.go b/internal/httpapi/assistant_live_cursor_test.gonew file mode 100644index 00000000..e1e39155--- /dev/null+++ b/internal/httpapi/assistant_live_cursor_test.go@@ -0,0 +1,50 @@+package httpapi++import (+ "agentflow/internal/domain"+ "encoding/json"+ "testing"+)++func cursorEvents(seqs ...int) []domain.AssistantGenerationEvent {+ out := make([]domain.AssistantGenerationEvent, 0, len(seqs))+ for _, s := range seqs {+ out = append(out, domain.AssistantGenerationEvent{Sequence: s})+ }+ return out+}++func TestAssistantEventsAfterFiltersByCursor(t *testing.T) {+ got := assistantEventsAfter(cursorEvents(3, 4, 5, 6), 4)+ if len(got) != 2 || got[0].Sequence != 5 || got[1].Sequence != 6 {+ t.Fatalf("events after 4 = %+v", got)+ }+ for _, events := range [][]domain.AssistantGenerationEvent{nil, cursorEvents(1, 2)} {+ empty := assistantEventsAfter(events, 2)+ if empty == nil || len(empty) != 0 {+ t.Fatalf("empty batch must be non-nil empty slice, got %#v", empty)+ }+ raw, _ := json.Marshal(empty)+ if string(raw) != "[]" {+ t.Fatalf("empty batch json = %s", raw)+ }+ }+}++func TestLatestAssistantEventSequenceNeverMovesBackwards(t *testing.T) {+ cases := []struct {+ events []domain.AssistantGenerationEvent+ cursor int+ want int+ }{+ {nil, 7, 7},+ {cursorEvents(1, 2, 3), 7, 7},+ {cursorEvents(5, 8, 9), 7, 9},+ {cursorEvents(10), 0, 10},+ }+ for _, tc := range cases {+ if got := latestAssistantEventSequence(tc.events, tc.cursor); got != tc.want {+ t.Fatalf("latest(%+v, %d) = %d, want %d", tc.events, tc.cursor, got, tc.want)+ }+ }+}