feat(chat): implement orphan alias detection and logging in chat completion and streaming

Add functionality to detect unresolvable resource aliases emitted by the model during chat completion and streaming. Introduce logging for these orphan aliases to aid in debugging and improve the handling of resource references. Update the stream processing to ensure held aliases are flushed upon cancellation, preventing data loss. Include tests to verify the correct reporting of orphan aliases.
This commit is contained in:
wizardchen
2026-07-15 16:49:43 +08:00
committed by lyingbug
parent 6d14cc3536
commit 38c594aec3
6 changed files with 210 additions and 16 deletions
@@ -129,9 +129,34 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context,
thinkingOpen = false
}
// flushDecoders drains any alias suffix the stream decoders held back to
// bridge references split across provider chunks. Both the normal close
// and the cancellation path must call this, otherwise a resource
// reference in flight at teardown is silently dropped (and never
// persisted, since the assistant message is saved from these events).
flushDecoders := func() {
if tail := thinkingDecoder.Flush(); tail != "" {
_ = eventBus.Emit(ctx, types.Event{
ID: thinkingID,
Type: types.EventType(event.EventAgentThought),
SessionID: chatManage.SessionID,
Data: event.AgentThoughtData{Content: tail},
})
}
if tail := answerDecoder.Flush(); tail != "" {
_ = eventBus.Emit(ctx, types.Event{
ID: answerID,
Type: types.EventType(event.EventAgentFinalAnswer),
SessionID: chatManage.SessionID,
Data: event.AgentFinalAnswerData{Content: tail},
})
}
}
for {
select {
case <-ctx.Done():
flushDecoders()
closeThinking()
pipelineInfo(ctx, "Stream", "context_cancelled", map[string]interface{}{
"session_id": chatManage.SessionID,
@@ -140,22 +165,7 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context,
case response, ok := <-responseChan:
if !ok {
if tail := thinkingDecoder.Flush(); tail != "" {
_ = eventBus.Emit(ctx, types.Event{
ID: thinkingID,
Type: types.EventType(event.EventAgentThought),
SessionID: chatManage.SessionID,
Data: event.AgentThoughtData{Content: tail},
})
}
if tail := answerDecoder.Flush(); tail != "" {
_ = eventBus.Emit(ctx, types.Event{
ID: answerID,
Type: types.EventType(event.EventAgentFinalAnswer),
SessionID: chatManage.SessionID,
Data: event.AgentFinalAnswerData{Content: tail},
})
}
flushDecoders()
closeThinking()
pipelineInfo(ctx, "Stream", "channel_close", map[string]interface{}{
"session_id": chatManage.SessionID,