feat: mensajes progresivos en Matrix con ProgressReporter
Implementa la Fase 2 del issue 0036: mensajes de progreso en tiempo real que muestran al usuario que herramientas esta usando el agente claude-code. - SendMarkdownGetID en shell/matrix/client.go: envia mensaje y retorna el event ID para editarlo despues - EditMessage en shell/matrix/client.go: edita un mensaje existente usando m.replace (m.relates_to con rel_type=m.replace) - ProgressReporter en shell/effects/progress.go (NEW): recibe streaming events y actualiza un mensaje unico en Matrix mostrando el progreso (e.g. "🔧 Bash: ls -la" → "🔧 Read: file.go" → "✅ Completado") - Rate limiter integrado: max 1 edit/segundo para no saturar el homeserver - Conectado en devagents/handler.go: cuando provider=claude-code y streaming+show_tool_progress habilitados, crea ProgressReporter y pasa StreamFunc al CompletionRequest - MatrixSender interface actualizada con los nuevos metodos - 10 tests nuevos para ProgressReporter, todos los existentes pasan Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
+25
-2
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/enmanuel/agents/pkg/sanitize"
|
||||
"github.com/enmanuel/agents/shell/audit"
|
||||
"github.com/enmanuel/agents/shell/bus"
|
||||
"github.com/enmanuel/agents/shell/effects"
|
||||
)
|
||||
|
||||
// handleEvent is called by the matrix Listener for each filtered incoming event.
|
||||
@@ -184,14 +185,28 @@ func (a *Agent) executeActions(ctx context.Context, roomID string, msgCtx decisi
|
||||
})
|
||||
a.persistMessage(ctx, memKey, coretypes.RoleUser, msgCtx.Content)
|
||||
|
||||
reply, err := a.runLLM(ctx, msgCtx, memKey)
|
||||
// Create ProgressReporter for claude-code streaming if enabled
|
||||
var progress *effects.ProgressReporter
|
||||
if a.isStreamingEnabled() {
|
||||
progress = effects.NewProgressReporter(a.sender, roomID, a.logger)
|
||||
}
|
||||
|
||||
reply, err := a.runLLM(ctx, msgCtx, memKey, progress)
|
||||
if err != nil {
|
||||
a.logger.Error("llm error", "err", err)
|
||||
if progress != nil {
|
||||
progress.Finalize("\u274c Error al procesar la solicitud.")
|
||||
}
|
||||
expanded = append(expanded, decision.Action{
|
||||
Kind: decision.ActionKindReply,
|
||||
Reply: &decision.ReplyAction{Content: "Sorry, I encountered an error.", InReplyTo: msgCtx.EventID, ThreadID: msgCtx.ThreadID},
|
||||
})
|
||||
} else {
|
||||
// If progress reporter was used, finalize it with a done indicator
|
||||
if progress != nil && progress.EventID() != "" {
|
||||
progress.Finalize("\u2705 *Completado*")
|
||||
}
|
||||
|
||||
expanded = append(expanded, decision.Action{
|
||||
Kind: decision.ActionKindReply,
|
||||
Reply: &decision.ReplyAction{Content: reply, InReplyTo: msgCtx.EventID, ThreadID: msgCtx.ThreadID},
|
||||
@@ -295,7 +310,7 @@ func (a *Agent) handleTaskEvent(ctx context.Context, msg bus.AgentMessage) {
|
||||
Role: coretypes.RoleUser, Content: msgCtx.Content,
|
||||
})
|
||||
|
||||
reply, err := a.runLLM(ctx, msgCtx, roomID)
|
||||
reply, err := a.runLLM(ctx, msgCtx, roomID, nil)
|
||||
|
||||
// Build the result to send back via bus
|
||||
result := orchestration.TaskResult{
|
||||
@@ -368,6 +383,14 @@ func (a *Agent) emitAudit(evt audit.Event) {
|
||||
}
|
||||
}
|
||||
|
||||
// isStreamingEnabled returns true when the agent uses claude-code provider
|
||||
// with streaming and show_tool_progress both enabled.
|
||||
func (a *Agent) isStreamingEnabled() bool {
|
||||
return a.cfg.LLM.Primary.Provider == "claude-code" &&
|
||||
a.cfg.LLM.Primary.ClaudeCode.Streaming &&
|
||||
a.cfg.LLM.Primary.ClaudeCode.ShowToolProgress
|
||||
}
|
||||
|
||||
// sanitizeInput runs prompt injection detection on the message content.
|
||||
// Returns the (possibly modified) content and true if the message should be rejected.
|
||||
func (a *Agent) sanitizeInput(content, roomID, senderID string) (string, bool) {
|
||||
|
||||
+11
-1
@@ -13,11 +13,14 @@ import (
|
||||
coretypes "github.com/enmanuel/agents/pkg/llm"
|
||||
"github.com/enmanuel/agents/pkg/personality"
|
||||
"github.com/enmanuel/agents/shell/audit"
|
||||
"github.com/enmanuel/agents/shell/effects"
|
||||
shelllm "github.com/enmanuel/agents/shell/llm"
|
||||
)
|
||||
|
||||
// runLLM executes the LLM completion loop, including iterative tool-use.
|
||||
func (a *Agent) runLLM(ctx context.Context, msgCtx decision.MessageContext, memKey string) (string, error) {
|
||||
// progress may be nil; when non-nil, its StreamFunc is attached to the request
|
||||
// for providers that support streaming (claude-code).
|
||||
func (a *Agent) runLLM(ctx context.Context, msgCtx decision.MessageContext, memKey string, progress *effects.ProgressReporter) (string, error) {
|
||||
a.logger.Debug("calling LLM",
|
||||
"model", a.cfg.LLM.Primary.Model,
|
||||
"provider", a.cfg.LLM.Primary.Provider,
|
||||
@@ -62,6 +65,12 @@ func (a *Agent) runLLM(ctx context.Context, msgCtx decision.MessageContext, memK
|
||||
maxIter = defaultMaxToolIterations
|
||||
}
|
||||
|
||||
// Resolve StreamFunc for providers that support streaming
|
||||
var streamFn coretypes.StreamFunc
|
||||
if progress != nil {
|
||||
streamFn = progress.StreamFunc()
|
||||
}
|
||||
|
||||
// Tool-use loop: call LLM → execute tools → feed results back → repeat
|
||||
for i := 0; i < maxIter; i++ {
|
||||
req := coretypes.CompletionRequest{
|
||||
@@ -71,6 +80,7 @@ func (a *Agent) runLLM(ctx context.Context, msgCtx decision.MessageContext, memK
|
||||
SystemPrompt: systemPrompt,
|
||||
Messages: messages,
|
||||
Tools: llmTools,
|
||||
StreamFunc: streamFn,
|
||||
}
|
||||
|
||||
resp, err := a.llm(ctx, req)
|
||||
|
||||
@@ -82,6 +82,20 @@ func (s *spyMatrixSender) SendMarkdown(_ context.Context, roomID, markdown strin
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *spyMatrixSender) SendMarkdownGetID(_ context.Context, roomID, markdown string) (string, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.messages = append(s.messages, sentMessage{roomID: roomID, text: markdown})
|
||||
return "$spy_event_id", nil
|
||||
}
|
||||
|
||||
func (s *spyMatrixSender) EditMessage(_ context.Context, roomID, originalEventID, markdown string) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.messages = append(s.messages, sentMessage{roomID: roomID, text: markdown, inReplyTo: originalEventID})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *spyMatrixSender) SendReplyMarkdown(_ context.Context, roomID, inReplyTo, markdown string) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
@@ -590,7 +604,7 @@ func TestRunLLM_ToolCallExecutesAndReturns(t *testing.T) {
|
||||
IsDirectMsg: true,
|
||||
}
|
||||
|
||||
reply, err := a.runLLM(context.Background(), msgCtx, "!room:example.com")
|
||||
reply, err := a.runLLM(context.Background(), msgCtx, "!room:example.com", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("runLLM error: %v", err)
|
||||
}
|
||||
@@ -655,7 +669,7 @@ func TestRunLLM_ToolCallFailsPassesErrorToLLM(t *testing.T) {
|
||||
Content: "do something",
|
||||
}
|
||||
|
||||
reply, err := a.runLLM(context.Background(), msgCtx, "!room:example.com")
|
||||
reply, err := a.runLLM(context.Background(), msgCtx, "!room:example.com", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("runLLM error: %v", err)
|
||||
}
|
||||
@@ -716,7 +730,7 @@ func TestRunLLM_MaxIterationsRespected(t *testing.T) {
|
||||
Content: "loop please",
|
||||
}
|
||||
|
||||
reply, err := a.runLLM(context.Background(), msgCtx, "!room:example.com")
|
||||
reply, err := a.runLLM(context.Background(), msgCtx, "!room:example.com", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("runLLM error: %v", err)
|
||||
}
|
||||
@@ -776,7 +790,7 @@ func TestRunLLM_RBACDeniesToolCall(t *testing.T) {
|
||||
Content: "use restricted tool",
|
||||
}
|
||||
|
||||
reply, err := a.runLLM(context.Background(), msgCtx, "!room:example.com")
|
||||
reply, err := a.runLLM(context.Background(), msgCtx, "!room:example.com", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("runLLM error: %v", err)
|
||||
}
|
||||
@@ -819,7 +833,7 @@ func TestRunLLM_LLMError(t *testing.T) {
|
||||
Content: "hello",
|
||||
}
|
||||
|
||||
_, err := a.runLLM(context.Background(), msgCtx, "!room:example.com")
|
||||
_, err := a.runLLM(context.Background(), msgCtx, "!room:example.com", nil)
|
||||
if err == nil {
|
||||
t.Fatal("expected error from LLM, got nil")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user