Skip to content

Commit

Permalink
Update blox.go
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan6sha committed Feb 3, 2025
1 parent 4b0d728 commit 5ca550c
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions blockchain/blox.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"net/http"
"strings"
"sync"

"github.com/functionland/go-fula/wap/pkg/wifi"
Expand Down Expand Up @@ -594,6 +595,8 @@ func (bl *FxBlockchain) handleChatWithAI(ctx context.Context, from peer.ID, w ht
log.Debugw("Streaming AI response started", "ai_model", req.AIModel, "user_message", req.UserMessage)
defer log.Debugw("Streaming AI response ended", "ai_model", req.AIModel, "user_message", req.UserMessage)

var lastChunk string // Keep track of the last chunk sent

// Stream chunks to the client
for {
select {
Expand All @@ -605,6 +608,14 @@ func (bl *FxBlockchain) handleChatWithAI(ctx context.Context, from peer.ID, w ht
return // Channel closed
}

chunk = strings.TrimSpace(chunk) // Remove leading/trailing whitespace

// Skip empty or duplicate chunks
if chunk == "" || chunk == lastChunk {
continue
}
lastChunk = chunk

response := wifi.ChatWithAIResponse{
Status: true,
Msg: chunk,
Expand Down

0 comments on commit 5ca550c

Please sign in to comment.