fix(chat): prevent duplicate terminal answer events
parent
4e3b512af6
commit
0e1e5a63ba
|
|
@ -112,6 +112,7 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context,
|
|||
thinkingID := fmt.Sprintf("%s-thinking", uuid.New().String()[:8])
|
||||
answerID := fmt.Sprintf("%s-answer", uuid.New().String()[:8])
|
||||
thinkingOpen := false
|
||||
answerCompleted := false
|
||||
|
||||
closeThinking := func() {
|
||||
if !thinkingOpen {
|
||||
|
|
@ -216,9 +217,17 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context,
|
|||
}
|
||||
|
||||
if response.ResponseType == types.ResponseTypeAnswer {
|
||||
// Providers can emit a completion once for finish_reason and again
|
||||
// for their EOF sentinel. A final answer is a terminal event for a
|
||||
// single stream, so forwarding a later duplicate would put an answer
|
||||
// after the session's complete event.
|
||||
if answerCompleted {
|
||||
continue
|
||||
}
|
||||
response.Content = answerDecoder.Feed(response.Content)
|
||||
if response.Done {
|
||||
response.Content += answerDecoder.Flush()
|
||||
answerCompleted = true
|
||||
}
|
||||
closeThinking()
|
||||
eventBus.Emit(ctx, types.Event{
|
||||
|
|
|
|||
|
|
@ -48,7 +48,8 @@ func (b *syncEventBus) finalAnswerContents() []string {
|
|||
// closes it, so the stream plugin blocks on the channel until ctx is cancelled
|
||||
// — deterministically exercising the ctx.Done() branch.
|
||||
type openStreamChat struct {
|
||||
chunks []types.StreamResponse
|
||||
chunks []types.StreamResponse
|
||||
closeStream bool
|
||||
}
|
||||
|
||||
func (m *openStreamChat) Chat(context.Context, []chat.Message, *chat.ChatOptions) (*types.ChatResponse, error) {
|
||||
|
|
@ -62,7 +63,10 @@ func (m *openStreamChat) ChatStream(
|
|||
for _, c := range m.chunks {
|
||||
ch <- c
|
||||
}
|
||||
return ch, nil // intentionally left open
|
||||
if m.closeStream {
|
||||
close(ch)
|
||||
}
|
||||
return ch, nil
|
||||
}
|
||||
|
||||
func (m *openStreamChat) GetModelName() string { return "mock" }
|
||||
|
|
@ -116,3 +120,36 @@ func TestStreamDropsIncompleteHandleOnCancel(t *testing.T) {
|
|||
time.Sleep(20 * time.Millisecond)
|
||||
require.Equal(t, []string{"hello "}, bus.finalAnswerContents())
|
||||
}
|
||||
|
||||
func TestStreamIgnoresDuplicateTerminalAnswer(t *testing.T) {
|
||||
bus := &syncEventBus{}
|
||||
model := &openStreamChat{closeStream: true, chunks: []types.StreamResponse{
|
||||
{ResponseType: types.ResponseTypeAnswer, Content: "hello"},
|
||||
{ResponseType: types.ResponseTypeAnswer, Done: true},
|
||||
// Some providers repeat the same terminal response when their EOF
|
||||
// sentinel is received after finish_reason.
|
||||
{ResponseType: types.ResponseTypeAnswer, Done: true},
|
||||
}}
|
||||
|
||||
chatManage := &types.ChatManage{}
|
||||
chatManage.SessionID = "sess-duplicate-done"
|
||||
chatManage.EventBus = bus
|
||||
plugin := &PluginChatCompletionStream{modelService: &stubModelService{model: model}}
|
||||
require.Nil(t, plugin.OnEvent(context.Background(), types.CHAT_COMPLETION_STREAM, chatManage, func() *PluginError { return nil }))
|
||||
|
||||
require.Eventually(t, func() bool {
|
||||
bus.mu.Lock()
|
||||
defer bus.mu.Unlock()
|
||||
return len(bus.events) == 2
|
||||
}, 2*time.Second, 5*time.Millisecond)
|
||||
|
||||
bus.mu.Lock()
|
||||
defer bus.mu.Unlock()
|
||||
var answerEvents []event.AgentFinalAnswerData
|
||||
for _, evt := range bus.events {
|
||||
if evt.Type == types.EventType(event.EventAgentFinalAnswer) {
|
||||
answerEvents = append(answerEvents, evt.Data.(event.AgentFinalAnswerData))
|
||||
}
|
||||
}
|
||||
require.Equal(t, []event.AgentFinalAnswerData{{Content: "hello"}, {Done: true}}, answerEvents)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue