chatbot

package
v1.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 1, 2026 License: Apache-2.0 Imports: 33 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultApprovalTimeout = 5 * time.Minute

Default approval timeout

Variables

This section is empty.

Functions

func NewCleanupRegistry

func NewCleanupRegistry() *cleanupRegistry

NewCleanupRegistry creates a new cleanup registry for the session

func TrimLeadingWhitespace added in v1.5.3

func TrimLeadingWhitespace(s string) string

TrimLeadingWhitespace strips leading whitespace characters (space, tab, newline, carriage return)

func TruncateToTermWidth

func TruncateToTermWidth(s string) (string, bool)

Types

type ApprovalRequest

type ApprovalRequest struct {
	ApprovalID string
	ResultChan chan ApprovalResultMap
}

ApprovalRequest holds the approval ID and result channel

type ApprovalResultMap

type ApprovalResultMap map[string]*mcp.ApprovalResult

ApprovalResultMap holds approval results for multiple targets

type ApprovalTarget

type ApprovalTarget struct {
	ID            string
	ToolName      string
	ArgumentsInfo string
}

ApprovalTarget represents a single approval request target

type ChatBot

type ChatBot struct {
	// contains filtered or unexported fields
}

ChatBot struct for the chatbot

func NewChatBot

func NewChatBot(ctx context.Context, agent *adk.ChatModelAgent, manager *manager.Manager, scanner *readline.Instance, persistence *store.PersistenceStore) ChatBot

func (*ChatBot) ClearContext

func (cb *ChatBot) ClearContext()

ClearContext clears the context

func (*ChatBot) GetContextSummary

func (cb *ChatBot) GetContextSummary() string

GetContextSummary retrieves context summary

func (*ChatBot) SetHandler

func (cb *ChatBot) SetHandler(handler Handler)

SetHandler sets the output handler for the chatbot

func (*ChatBot) StreamChat

func (cb *ChatBot) StreamChat(ctx context.Context, userInput string) error

StreamChat performs streaming chat conversation with CLI output

func (*ChatBot) StreamChatWithHandler

func (cb *ChatBot) StreamChatWithHandler(ctx context.Context, userInput string, files []FileData) error

StreamChatWithHandler performs streaming chat with a custom handler

type ChatSession

type ChatSession struct {
	ID        string
	Name      string
	Preset    config.Chat
	Agent     *adk.ChatModelAgent
	Manager   *manager.Manager
	Tools     []tool.BaseTool
	MCPClient *mcp.Client
	// contains filtered or unexported fields
}

ChatSession represents a chat session with its configuration

func InitChatSession

func InitChatSession(ctx context.Context, cfg *config.Config, chatName string, sessionID string, debug bool) (*ChatSession, error)

InitChatSession initializes a new chat session with the given chat name and session ID

func (*ChatSession) Clear

func (s *ChatSession) Clear() error

Clear clear the current context

func (*ChatSession) Close

func (s *ChatSession) Close() error

Close closes the chat session and releases all resources

func (*ChatSession) GetLastUserMessage

func (s *ChatSession) GetLastUserMessage() string

GetLastUserMessage returns the last user message from the conversation, if any. Used for redo/regenerate functionality.

func (*ChatSession) GetMessageCount

func (s *ChatSession) GetMessageCount() int

GetMessageCount returns the number of messages in the session

func (*ChatSession) OnGenModelInput

func (s *ChatSession) OnGenModelInput(ctx context.Context, instruction string, inputMessages []*schema.Message) ([]*schema.Message, error)

OnGenModelInput executes the genmodelinput hook if configured This hook is called before sending messages to the model and can modify the message list

func (*ChatSession) OnKeep

func (s *ChatSession) OnKeep() error

func (*ChatSession) PersistenceStore

func (s *ChatSession) PersistenceStore() *store.PersistenceStore

PersistenceStore returns the persistence store for this session

func (*ChatSession) RemoveLastRound

func (s *ChatSession) RemoveLastRound()

RemoveLastRound removes the last round of messages (used for regenerate)

type FileData

type FileData struct {
	URL      string
	Type     string
	Name     string
	FileSize int64
}

FileData represents file data for multimodal messages

type Handler

type Handler interface {
	// SendChunk sends a content chunk with position markers
	// contentType: "response" or "thinking"
	SendChunk(content string, first, last bool, contentType string)

	// SendToolCall sends a tool call notification with name, arguments, index and streaming status
	// index: the tool call index
	// streaming: true if this is a streaming update (arguments may be partial), false when complete
	SendToolCall(name string, arguments string, id string, streaming bool)

	// SendThinking sends a thinking indicator
	SendThinking(status bool)

	// SendComplete sends a completion signal
	SendComplete(message string)

	// SendError sends an error message
	SendError(err string)

	// SendApprovalRequest sends an approval request to the client and waits for the result
	// targets: list of approval targets requiring user authorization
	// Returns a map of target IDs to their approval results
	SendApprovalRequest(targets []ApprovalTarget) (ApprovalResultMap, error)

	// SendMessageCount sends the current message count to the client
	SendMessageCount()
}

Handler interface for handling chat output events This allows the same streaming logic to be used in different contexts (CLI with readline, WebSocket, etc.)

type StreamFilter

type StreamFilter struct {
	// contains filtered or unexported fields
}

func NewStreamFilter

func NewStreamFilter() *StreamFilter

func (*StreamFilter) Finish

func (f *StreamFilter) Finish() *string

func (*StreamFilter) Process

func (f *StreamFilter) Process(chunk string) *string

type WSChatHandler

type WSChatHandler struct {
	// contains filtered or unexported fields
}

WSChatHandler implements Handler for WebSocket output

func NewWSChatHandler

func NewWSChatHandler(session *WSSession) *WSChatHandler

func (*WSChatHandler) SendApprovalRequest

func (h *WSChatHandler) SendApprovalRequest(targets []ApprovalTarget) (ApprovalResultMap, error)

func (*WSChatHandler) SendChunk

func (h *WSChatHandler) SendChunk(content string, first, last bool, contentType string)

func (*WSChatHandler) SendComplete

func (h *WSChatHandler) SendComplete(message string)

func (*WSChatHandler) SendError

func (h *WSChatHandler) SendError(err string)

func (*WSChatHandler) SendMessageCount

func (h *WSChatHandler) SendMessageCount()

SendApprovalRequest sends an approval request to the client and waits for the result

func (*WSChatHandler) SendThinking

func (h *WSChatHandler) SendThinking(status bool)

func (*WSChatHandler) SendToolCall

func (h *WSChatHandler) SendToolCall(name string, arguments string, id string, streaming bool)

type WSMessage

type WSMessage struct {
	Type    string          `json:"type"`
	Payload json.RawMessage `json:"payload"`
}

WebSocket message types

type WSSession

type WSSession struct {
	SessionID   string
	ChatName    string
	ChatSession *ChatSession
	ChatBot     *ChatBot
	WSHandler   *WSChatHandler
	// contains filtered or unexported fields
}

WSSession represents a WebSocket session with its connection

func NewWSSession

func NewWSSession(conn *websocket.Conn, sessionID string, cfg *config.Config) *WSSession

func (*WSSession) HandleApprovalResponse

func (s *WSSession) HandleApprovalResponse(approvalID string, results ApprovalResultMap)

HandleApprovalResponse processes an approval response from the client This method is called from the main read loop when an approval_response message is received

func (*WSSession) IsCancelled

func (s *WSSession) IsCancelled() bool

IsCancelled returns true if the session is cancelled

func (*WSSession) IsClosed

func (s *WSSession) IsClosed() bool

IsClosed returns true if the session has been marked as closed.

func (*WSSession) MarkClosed

func (s *WSSession) MarkClosed()

MarkClosed marks the session as closed so that subsequent SendMessage/SendPing calls are silently dropped instead of writing to a closed connection.

func (*WSSession) ResetCancel

func (s *WSSession) ResetCancel()

ResetCancel resets the cancel state for a new request

func (*WSSession) SendChunk

func (s *WSSession) SendChunk(content string, isFirst, isLast bool, contentType string)

func (*WSSession) SendError

func (s *WSSession) SendError(errMsg string)

func (*WSSession) SendMessage

func (s *WSSession) SendMessage(msgType string, content interface{})

func (*WSSession) SendMessageCount

func (s *WSSession) SendMessageCount()

SendMessageCount sends the current message count to the client

func (*WSSession) SendPing

func (s *WSSession) SendPing()

SendPing sends a WebSocket ping frame to the client. Used for keepalive to detect dead connections (e.g., mobile network loss). The write deadline ensures we don't block forever if the connection is dead. The deadline is cleared after the write to avoid affecting subsequent writes.

func (*WSSession) SetApprovalTimeout

func (s *WSSession) SetApprovalTimeout(timeout time.Duration)

SetApprovalTimeout sets the timeout for approval requests

func (*WSSession) SetCancelFunc

func (s *WSSession) SetCancelFunc(cancelFunc context.CancelFunc)

SetCancelFunc sets the cancel function for the current request

func (*WSSession) SetCancelled

func (s *WSSession) SetCancelled()

SetCancelled marks the session as cancelled

func (*WSSession) SetReadTimeout

func (s *WSSession) SetReadTimeout(d time.Duration)

SetReadTimeout sets the read timeout used to reset the read deadline after writes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL