bot contesta con e2ee
This commit is contained in:
+44
-6
@@ -4,7 +4,9 @@ package agents
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"path/filepath"
|
||||
|
||||
"maunium.net/go/mautrix/event"
|
||||
|
||||
@@ -28,6 +30,7 @@ type Agent struct {
|
||||
runner *effects.Runner
|
||||
listener *matrix.Listener
|
||||
logger *slog.Logger
|
||||
cryptoStore io.Closer // non-nil when E2EE is enabled; closed on shutdown
|
||||
}
|
||||
|
||||
// New assembles an Agent from its config, rules, and logger.
|
||||
@@ -38,6 +41,18 @@ func New(cfg *config.AgentConfig, rules []decision.Rule, logger *slog.Logger) (*
|
||||
return nil, fmt.Errorf("matrix client: %w", err)
|
||||
}
|
||||
|
||||
// E2EE — initialize before the sync loop starts
|
||||
var cryptoStore io.Closer
|
||||
if cfg.Matrix.Encryption.Enabled {
|
||||
storePath := filepath.Join(cfg.Matrix.Encryption.StorePath, "crypto.db")
|
||||
logger.Info("initializing e2ee", "store", storePath)
|
||||
cryptoStore, err = matrixClient.InitCrypto(context.Background(), storePath, cfg.Agent.ID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("e2ee init: %w", err)
|
||||
}
|
||||
logger.Info("e2ee ready")
|
||||
}
|
||||
|
||||
// SSH executor
|
||||
sshExec := ssh.NewExecutor(cfg.SSH)
|
||||
|
||||
@@ -61,12 +76,13 @@ func New(cfg *config.AgentConfig, rules []decision.Rule, logger *slog.Logger) (*
|
||||
runner := effects.NewRunner(matrixClient, sshExec, logger)
|
||||
|
||||
a := &Agent{
|
||||
cfg: cfg,
|
||||
rules: rules,
|
||||
llm: llmFunc,
|
||||
matrix: matrixClient,
|
||||
runner: runner,
|
||||
logger: logger,
|
||||
cfg: cfg,
|
||||
rules: rules,
|
||||
llm: llmFunc,
|
||||
matrix: matrixClient,
|
||||
runner: runner,
|
||||
logger: logger,
|
||||
cryptoStore: cryptoStore,
|
||||
}
|
||||
|
||||
// Matrix event listener
|
||||
@@ -77,21 +93,33 @@ func New(cfg *config.AgentConfig, rules []decision.Rule, logger *slog.Logger) (*
|
||||
|
||||
// Run starts the agent sync loop. Blocks until ctx is cancelled.
|
||||
func (a *Agent) Run(ctx context.Context) error {
|
||||
if a.cryptoStore != nil {
|
||||
defer a.cryptoStore.Close()
|
||||
}
|
||||
a.logger.Info("agent starting", "id", a.cfg.Agent.ID, "name", a.cfg.Agent.Name)
|
||||
return a.listener.Run(ctx)
|
||||
}
|
||||
|
||||
// handleEvent is called by the matrix Listener for each filtered incoming event.
|
||||
func (a *Agent) handleEvent(ctx context.Context, msgCtx decision.MessageContext, evt *event.Event) {
|
||||
a.logger.Debug("handling event",
|
||||
"sender", msgCtx.SenderID,
|
||||
"is_dm", msgCtx.IsDirectMsg,
|
||||
"is_mention", msgCtx.IsMention,
|
||||
"command", msgCtx.Command,
|
||||
)
|
||||
|
||||
if a.cfg.Personality.Behavior.TypingIndicator {
|
||||
_ = a.matrix.SendTyping(ctx, evt.RoomID.String(), true)
|
||||
defer a.matrix.SendTyping(ctx, evt.RoomID.String(), false)
|
||||
}
|
||||
|
||||
actions := decision.Evaluate(msgCtx, a.rules)
|
||||
a.logger.Debug("rules evaluated", "matched_actions", len(actions))
|
||||
|
||||
// If no rules matched and the message mentions the bot or is a DM, use LLM.
|
||||
if len(actions) == 0 && (msgCtx.IsMention || msgCtx.IsDirectMsg) {
|
||||
a.logger.Debug("no rules matched, falling back to LLM")
|
||||
actions = []decision.Action{{
|
||||
Kind: decision.ActionKindLLM,
|
||||
LLM: &decision.LLMAction{ContextKey: msgCtx.RoomID},
|
||||
@@ -99,6 +127,10 @@ func (a *Agent) handleEvent(ctx context.Context, msgCtx decision.MessageContext,
|
||||
}
|
||||
|
||||
if len(actions) == 0 {
|
||||
a.logger.Debug("no actions, ignoring message",
|
||||
"is_dm", msgCtx.IsDirectMsg,
|
||||
"is_mention", msgCtx.IsMention,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -128,6 +160,10 @@ func (a *Agent) handleEvent(ctx context.Context, msgCtx decision.MessageContext,
|
||||
}
|
||||
|
||||
func (a *Agent) runLLM(ctx context.Context, msgCtx decision.MessageContext) (string, error) {
|
||||
a.logger.Debug("calling LLM",
|
||||
"model", a.cfg.LLM.Primary.Model,
|
||||
"provider", a.cfg.LLM.Primary.Provider,
|
||||
)
|
||||
req := coretypes.CompletionRequest{
|
||||
Model: a.cfg.LLM.Primary.Model,
|
||||
MaxTokens: a.cfg.LLM.Primary.MaxTokens,
|
||||
@@ -139,7 +175,9 @@ func (a *Agent) runLLM(ctx context.Context, msgCtx decision.MessageContext) (str
|
||||
}
|
||||
resp, err := a.llm(ctx, req)
|
||||
if err != nil {
|
||||
a.logger.Error("LLM call failed", "model", req.Model, "err", err)
|
||||
return "", err
|
||||
}
|
||||
a.logger.Debug("LLM responded", "content_len", len(resp.Content))
|
||||
return resp.Content, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user