fix(chat): settle authoritative turns and make capabilities explicit
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
@@ -729,10 +730,71 @@ func (c *Chat) Stream(ctx context.Context, id string, ready func(), emit func(pr
|
||||
return err
|
||||
}
|
||||
defer body.Close()
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
var delivery sync.Mutex
|
||||
state.mu.Lock()
|
||||
busy := state.Active
|
||||
state.mu.Unlock()
|
||||
setBusy := func(value bool) {
|
||||
busy = value
|
||||
state.mu.Lock()
|
||||
state.Active = value
|
||||
state.mu.Unlock()
|
||||
}
|
||||
deliver := func(frame protocol.ChatEventFrame) error {
|
||||
delivery.Lock()
|
||||
defer delivery.Unlock()
|
||||
if frame.Kind == protocol.ChatEventStep || frame.Kind == protocol.ChatEventText || frame.Kind == protocol.ChatEventTool || frame.Kind == protocol.ChatEventReasoning {
|
||||
setBusy(true)
|
||||
}
|
||||
return emit(frame)
|
||||
}
|
||||
// V2 /api/session/active describes foreground drains, not individual
|
||||
// provider steps. Poll it even after reconnect: a busy drain may have
|
||||
// started before this SSE subscription. Failed readbacks never imply idle.
|
||||
watchDone := make(chan struct{})
|
||||
go func() {
|
||||
defer close(watchDone)
|
||||
ticker := time.NewTicker(500 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
}
|
||||
delivery.Lock()
|
||||
readCtx, readCancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
data, err := c.call(readCtx, "get", "/api/session/active", nil)
|
||||
readCancel()
|
||||
var emitErr error
|
||||
var response struct {
|
||||
Data *map[string]json.RawMessage `json:"data"`
|
||||
}
|
||||
if err == nil && json.Unmarshal(data, &response) == nil && response.Data != nil {
|
||||
_, active := (*response.Data)[id]
|
||||
if active && !busy {
|
||||
setBusy(true)
|
||||
emitErr = emit(protocol.ChatEventFrame{SessionID: id, Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepStarted}})
|
||||
} else if !active && busy {
|
||||
setBusy(false)
|
||||
emitErr = emit(protocol.ChatEventFrame{SessionID: id, Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded}})
|
||||
}
|
||||
}
|
||||
delivery.Unlock()
|
||||
if emitErr != nil {
|
||||
cancel()
|
||||
_ = body.Close()
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
defer func() { cancel(); <-watchDone }()
|
||||
if ready != nil {
|
||||
ready()
|
||||
}
|
||||
return c.scanChatEventsWithState(ctx, body, id, state, emit)
|
||||
return c.scanChatEventsWithState(ctx, body, id, state, deliver)
|
||||
}
|
||||
|
||||
// scanChatEvents turns an SSE body into protocol frames, one bounded frame at a
|
||||
@@ -905,6 +967,7 @@ func (i *chatShellInfo) shellSessionID() string {
|
||||
// (which carry no session) can be attributed to the right chat.
|
||||
type ChatStreamState struct {
|
||||
mu sync.Mutex
|
||||
Active bool // last observed foreground drain, retained across SSE reconnects
|
||||
LastMessageID string
|
||||
Shells map[string]struct{}
|
||||
Forms map[string]string
|
||||
@@ -1056,7 +1119,7 @@ func NormalizeChatEvent(payload []byte, sessionID string, stream *ChatStreamStat
|
||||
frame.Part = &protocol.ChatPart{Type: protocol.ChatPartReasoning, ID: protocol.ChatDeltaPartID(messageID, protocol.ChatPartReasoning, data.Ordinal)}
|
||||
case outer.Type == "session.step.started" || outer.Type == "session.step.streamed" || outer.Type == "session.step.ended":
|
||||
frame.Kind = protocol.ChatEventStep
|
||||
frame.Step = &protocol.ChatStep{Status: strings.TrimPrefix(outer.Type, "session.step."), Index: stepIndex(data)}
|
||||
frame.Step = &protocol.ChatStep{Status: strings.TrimPrefix(outer.Type, "session.step."), Index: stepIndex(data), Intermediate: outer.Type == "session.step.ended"}
|
||||
case strings.HasPrefix(outer.Type, "session.tool."):
|
||||
status, ok := toolStatus(outer.Type)
|
||||
if !ok {
|
||||
|
||||
Reference in New Issue
Block a user