Skip to content

Commit

Permalink
Added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Struki84 committed Oct 31, 2023
1 parent d508dcc commit 33174dc
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions callbacks/agent_final_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strings"
)

// DefaultKeywords is map of the agents final out prefix keywords.
//
//nolint:all
var DefaultKeywords = []string{"Final Answer:", "Final:", "AI:"}

Expand All @@ -21,6 +23,14 @@ type AgentFinalStreamHandler struct {

var _ Handler = &AgentFinalStreamHandler{}

// NewFinalStreamHandler creates a new instance of the AgentFinalStreamHandler struct.
//
// It accepts a variadic number of strings as keywords. If any keywords are provided,
// the DefaultKeywords variable is updated with the provided keywords.
//
// DefualtKeywords is map of the agents final out prefix keywords.
//
// The function returns a pointer to the created AgentFinalStreamHandler struct.
func NewFinalStreamHandler(keywords ...string) *AgentFinalStreamHandler {
if len(keywords) > 0 {
DefaultKeywords = keywords
Expand All @@ -32,10 +42,20 @@ func NewFinalStreamHandler(keywords ...string) *AgentFinalStreamHandler {
}
}

// GetEgress returns the egress channel of the AgentFinalStreamHandler.
//
// It does not take any parameters.
// It returns a channel of type []byte.
func (handler *AgentFinalStreamHandler) GetEgress() chan []byte {
return handler.egress
}

// ReadFromEgress reads data from the egress channel and invokes the provided
// callback function with each chunk of data.
//
// The callback function receives two parameters:
// - ctx: the context.Context object for the egress operation.
// - chunk: a byte slice representing a chunk of data from the egress channel.
func (handler *AgentFinalStreamHandler) ReadFromEgress(callback func(ctx context.Context, chunk []byte)) {
egressCtx, cancel := context.WithCancel(handler.ctx)
defer cancel()
Expand All @@ -48,6 +68,13 @@ func (handler *AgentFinalStreamHandler) ReadFromEgress(callback func(ctx context
}()
}

// HandleStreamingFunc implements the callback interface that handles the streaming
// of data in the AgentFinalStreamHandler. The handler reads the incoming data and checks for the
// agents final output keywords, ie, `Final Answer:`, `Final:`, `AI:`. Upon detection of
// the keyword, it starst to stream the agents final output to the egress channel.
//
// It takes in the context and a chunk of bytes as parameters.
// There is no return type for this function.
func (handler *AgentFinalStreamHandler) HandleStreamingFunc(ctx context.Context, chunk []byte) {
handler.ctx = ctx
chunkStr := string(chunk)
Expand Down

0 comments on commit 33174dc

Please sign in to comment.