Documentation
¶
Index ¶
- Constants
- func ExtractInt64FromMetadata(msg *ChatMessage, key string) int64
- func ExtractStringFromMetadata(msg *ChatMessage, key string) string
- func ReadBody(r *http.Request) ([]byte, error)
- func RespondWithError(w http.ResponseWriter, code int, message string)
- func RespondWithJSON(w http.ResponseWriter, code int, data any) error
- func RespondWithText(w http.ResponseWriter, code int, text string)
- type Adapter
- func (a *Adapter) GetOrCreateSession(key, userID string) string
- func (a *Adapter) GetSession(key string) (*Session, bool)
- func (a *Adapter) HandleMessage(ctx context.Context, msg *ChatMessage) error
- func (a *Adapter) Handler() MessageHandler
- func (a *Adapter) Logger() *slog.Logger
- func (a *Adapter) Platform() string
- func (a *Adapter) SendMessage(ctx context.Context, sessionID string, msg *ChatMessage) error
- func (a *Adapter) SetHandler(handler MessageHandler)
- func (a *Adapter) SetLogger(logger *slog.Logger)
- func (a *Adapter) Start(ctx context.Context) error
- func (a *Adapter) Stop() error
- func (a *Adapter) SystemPrompt() string
- func (a *Adapter) WebhookHandler() http.Handler
- func (a *Adapter) WebhookPath() string
- type AdapterOption
- func WithHTTPHandler(path string, handler http.HandlerFunc) AdapterOption
- func WithMessageParser(parser MessageParser) AdapterOption
- func WithMessageSender(sender MessageSender) AdapterOption
- func WithMetadataExtractor(extractor MetadataExtractor) AdapterOption
- func WithSessionTimeout(timeout time.Duration) AdapterOption
- func WithoutServer() AdapterOption
- type Attachment
- type ChatAdapter
- type ChatMessage
- type Config
- type HTTPClient
- func (c *HTTPClient) Get(ctx context.Context, url string, headers map[string]string) ([]byte, error)
- func (c *HTTPClient) PostJSON(ctx context.Context, url string, payload any, headers map[string]string) ([]byte, error)
- func (c *HTTPClient) PostJSONWithResponse(ctx context.Context, url string, payload any, headers map[string]string, ...) error
- type MessageHandler
- type MessageParser
- type MessageSender
- type MetadataExtractor
- type ParseMode
- type RichContent
- type SenderFunc
- type SenderWithMutex
- type Session
- type WebhookProvider
- type WebhookRunner
Constants ¶
const ErrSenderNotConfigured = "sender not configured"
ErrSenderNotConfigured is returned when SendMessage is called but no sender function has been configured.
Variables ¶
This section is empty.
Functions ¶
func ExtractInt64FromMetadata ¶
func ExtractInt64FromMetadata(msg *ChatMessage, key string) int64
ExtractInt64FromMetadata extracts an int64 value from message metadata. Returns 0 if the key doesn't exist or the value is not a numeric type.
func ExtractStringFromMetadata ¶
func ExtractStringFromMetadata(msg *ChatMessage, key string) string
ExtractStringFromMetadata extracts a string value from message metadata. Returns empty string if the key doesn't exist or the value is not a string.
func RespondWithError ¶
func RespondWithError(w http.ResponseWriter, code int, message string)
func RespondWithJSON ¶
func RespondWithJSON(w http.ResponseWriter, code int, data any) error
func RespondWithText ¶
func RespondWithText(w http.ResponseWriter, code int, text string)
Types ¶
type Adapter ¶
type Adapter struct {
// contains filtered or unexported fields
}
Adapter is the base adapter implementing common functionality
func NewAdapter ¶
func NewAdapter( platform string, config Config, logger *slog.Logger, opts ...AdapterOption, ) *Adapter
NewAdapter creates a new base adapter
func (*Adapter) GetOrCreateSession ¶
GetOrCreateSession gets or creates a session
func (*Adapter) GetSession ¶
GetSession retrieves a session by key
func (*Adapter) HandleMessage ¶
func (a *Adapter) HandleMessage(ctx context.Context, msg *ChatMessage) error
HandleMessage handles incoming message (stub for interface compliance)
func (*Adapter) Handler ¶
func (a *Adapter) Handler() MessageHandler
Handler returns the message handler (thread-safe)
func (*Adapter) SendMessage ¶
SendMessage sends a message (requires messageSender to be set)
func (*Adapter) SetHandler ¶
func (a *Adapter) SetHandler(handler MessageHandler)
SetHandler sets the message handler (thread-safe)
func (*Adapter) SystemPrompt ¶
SystemPrompt returns the system prompt
func (*Adapter) WebhookHandler ¶
WebhookHandler returns an http.Handler with all webhook endpoints registered
func (*Adapter) WebhookPath ¶
WebhookPath returns the primary webhook path for this adapter
type AdapterOption ¶
type AdapterOption func(*Adapter)
AdapterOption configures the base adapter
func WithHTTPHandler ¶
func WithHTTPHandler(path string, handler http.HandlerFunc) AdapterOption
WithHTTPHandler adds an HTTP handler
func WithMessageParser ¶
func WithMessageParser(parser MessageParser) AdapterOption
WithMessageParser sets the message parser
func WithMessageSender ¶
func WithMessageSender(sender MessageSender) AdapterOption
WithMessageSender sets the message sender
func WithMetadataExtractor ¶
func WithMetadataExtractor(extractor MetadataExtractor) AdapterOption
WithMetadataExtractor sets the metadata extractor
func WithSessionTimeout ¶
func WithSessionTimeout(timeout time.Duration) AdapterOption
WithSessionTimeout sets the session timeout
func WithoutServer ¶
func WithoutServer() AdapterOption
WithoutServer disables the embedded HTTP server Use this when running adapters under a unified server
type Attachment ¶
type ChatAdapter ¶
type ChatMessage ¶
type HTTPClient ¶
type HTTPClient struct {
// contains filtered or unexported fields
}
HTTPClient provides common HTTP request patterns for chat adapters. This eliminates the duplicate HTTP request building code across adapters.
func NewHTTPClient ¶
func NewHTTPClient() *HTTPClient
NewHTTPClient creates a new HTTPClient with the default http.Client.
func NewHTTPClientWithConfig ¶
func NewHTTPClientWithConfig(timeout time.Duration, maxRetries int) *HTTPClient
NewHTTPClientWithConfig creates a new HTTPClient with custom configuration.
func (*HTTPClient) Get ¶
func (c *HTTPClient) Get(ctx context.Context, url string, headers map[string]string) ([]byte, error)
Get sends a GET request and returns the response body.
type MessageHandler ¶
type MessageHandler func(ctx context.Context, msg *ChatMessage) error
type MessageParser ¶
type MessageParser func(body []byte, metadata map[string]any) (*ChatMessage, error)
MessageParser parses incoming requests into ChatMessage
type MessageSender ¶
type MessageSender func(ctx context.Context, sessionID string, msg *ChatMessage) error
MessageSender sends messages to the platform
type MetadataExtractor ¶
MetadataExtractor extracts platform-specific metadata from incoming requests
type RichContent ¶
type RichContent struct {
ParseMode ParseMode
InlineKeyboard any
Blocks []any
Embeds []any
Attachments []Attachment
}
type SenderFunc ¶
type SenderFunc func(ctx context.Context, sessionID string, msg *ChatMessage) error
SenderFunc is the function signature for sending messages to a platform.
type SenderWithMutex ¶
type SenderWithMutex struct {
// contains filtered or unexported fields
}
SenderWithMutex provides thread-safe sender management for chat adapters. This eliminates the duplicate sender/senderMu pattern across all adapters.
func NewSenderWithMutex ¶
func NewSenderWithMutex() *SenderWithMutex
NewSenderWithMutex creates a new SenderWithMutex with no sender configured.
func NewSenderWithMutexFunc ¶
func NewSenderWithMutexFunc(sender SenderFunc) *SenderWithMutex
NewSenderWithMutexFunc creates a new SenderWithMutex with an initial sender.
func (*SenderWithMutex) HasSender ¶
func (s *SenderWithMutex) HasSender() bool
HasSender returns true if a sender has been configured.
func (*SenderWithMutex) SendMessage ¶
func (s *SenderWithMutex) SendMessage(ctx context.Context, sessionID string, msg *ChatMessage) error
SendMessage sends a message using the configured sender. Thread-safe. Returns ErrSenderNotConfigured if no sender has been set.
func (*SenderWithMutex) Sender ¶
func (s *SenderWithMutex) Sender() SenderFunc
Sender returns the current sender function (may be nil). Note: This does not acquire the lock, so the caller should ensure thread-safety if the sender might be modified concurrently.
func (*SenderWithMutex) SetSender ¶
func (s *SenderWithMutex) SetSender(fn SenderFunc)
SetSender sets the sender function. Thread-safe.
type WebhookProvider ¶
WebhookProvider exposes HTTP handlers for unified server integration
type WebhookRunner ¶
type WebhookRunner struct {
// contains filtered or unexported fields
}
WebhookRunner manages the lifecycle of webhook processing goroutines. This eliminates the duplicate webhookWg pattern across all adapters.
func NewWebhookRunner ¶
func NewWebhookRunner(logger *slog.Logger) *WebhookRunner
NewWebhookRunner creates a new WebhookRunner.
func (*WebhookRunner) Run ¶
func (r *WebhookRunner) Run(ctx context.Context, handler MessageHandler, msg *ChatMessage)
Run executes the handler in a goroutine and tracks its completion. If handler is nil, this is a no-op.
func (*WebhookRunner) Stop ¶
func (r *WebhookRunner) Stop() bool
Stop is an alias for WaitDefault for API consistency with adapters.
func (*WebhookRunner) Wait ¶
func (r *WebhookRunner) Wait(timeout time.Duration) bool
Wait blocks until all running goroutines complete or timeout occurs. Returns true if all goroutines completed, false if timeout occurred.
func (*WebhookRunner) WaitDefault ¶
func (r *WebhookRunner) WaitDefault() bool
WaitDefault blocks with the default 5 second timeout.