feat: import agents_and_robots platform as unibots (Matrix-out, unibus transport)
Reemplaza el scaffold del echobot por la plataforma completa de bots traida desde ~/DataProyects/Github/agents_and_robots tras la operacion Matrix-out: los bots ya no hablan por Matrix sino por el bus unibus (modelo todo-rooms + E2E via shell/transportunibus sobre github.com/enmanuel/unibus/pkg/client). - go.mod: replace de unibus -> ../unibus y de fn-registry -> ../../../.. (paths relativos reajustados a la nueva ubicacion dentro de fn_registry). - app.md: bump a 0.2.0, descripcion + arquitectura + comandos + gotchas reales. - modulo Go conservado como github.com/enmanuel/agents (sin reescribir imports). agents_and_robots queda archivado como museo de la era Matrix.
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
// Package effects interprets pure []decision.Action values into real side effects.
|
||||
package effects
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/enmanuel/agents/pkg/decision"
|
||||
"github.com/enmanuel/agents/shell/logger"
|
||||
"github.com/enmanuel/agents/shell/ssh"
|
||||
)
|
||||
|
||||
// Result holds the outcome of executing a single action.
|
||||
type Result struct {
|
||||
Action decision.Action
|
||||
Output string
|
||||
Err error
|
||||
}
|
||||
|
||||
// Sender is the transport-neutral message-sending capability the runner depends
|
||||
// on. It is satisfied by the unibus bus sender (and was satisfied by the Matrix
|
||||
// client before the bus migration). SendTyping is a no-op on transports without
|
||||
// typing indicators.
|
||||
type Sender interface {
|
||||
SendText(ctx context.Context, roomID, text string) error
|
||||
SendMarkdown(ctx context.Context, roomID, markdown string) error
|
||||
SendReplyMarkdown(ctx context.Context, roomID, inReplyTo, markdown string) error
|
||||
SendThreadMarkdown(ctx context.Context, roomID, threadRootID, inReplyTo, markdown string) error
|
||||
SendTyping(ctx context.Context, roomID string, typing bool) error
|
||||
}
|
||||
|
||||
// Runner interprets actions and executes them.
|
||||
type Runner struct {
|
||||
sender Sender
|
||||
ssh *ssh.Executor
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
// NewRunner creates a Runner with the provided dependencies.
|
||||
func NewRunner(sender Sender, ssh *ssh.Executor, logger *slog.Logger) *Runner {
|
||||
return &Runner{sender: sender, ssh: ssh, logger: logger}
|
||||
}
|
||||
|
||||
// Execute runs each action sequentially and returns results.
|
||||
func (r *Runner) Execute(ctx context.Context, roomID string, actions []decision.Action) []Result {
|
||||
r.logger.Debug("effects_batch", "room", roomID, "count", len(actions))
|
||||
results := make([]Result, 0, len(actions))
|
||||
for _, a := range actions {
|
||||
start := time.Now()
|
||||
res := r.executeOne(ctx, roomID, a)
|
||||
ms := time.Since(start).Milliseconds()
|
||||
results = append(results, res)
|
||||
if res.Err != nil {
|
||||
r.logger.Error("action_failed", logger.FieldAction, a.Kind, logger.FieldDurationMS, ms, "err", res.Err)
|
||||
} else {
|
||||
r.logger.Info("action_done", logger.FieldAction, a.Kind, logger.FieldDurationMS, ms)
|
||||
}
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
func (r *Runner) executeOne(ctx context.Context, roomID string, a decision.Action) Result {
|
||||
switch a.Kind {
|
||||
case decision.ActionKindReply:
|
||||
if a.Reply == nil {
|
||||
return Result{Action: a, Err: fmt.Errorf("nil reply action")}
|
||||
}
|
||||
var err error
|
||||
switch {
|
||||
case a.Reply.ThreadID != "":
|
||||
// Thread reply: send as part of the thread with fallback in_reply_to
|
||||
err = r.sender.SendThreadMarkdown(ctx, roomID, a.Reply.ThreadID, a.Reply.InReplyTo, a.Reply.Content)
|
||||
case a.Reply.InReplyTo != "":
|
||||
err = r.sender.SendReplyMarkdown(ctx, roomID, a.Reply.InReplyTo, a.Reply.Content)
|
||||
default:
|
||||
err = r.sender.SendMarkdown(ctx, roomID, a.Reply.Content)
|
||||
}
|
||||
return Result{Action: a, Output: a.Reply.Content, Err: err}
|
||||
|
||||
case decision.ActionKindSSH:
|
||||
if a.SSH == nil {
|
||||
return Result{Action: a, Err: fmt.Errorf("nil ssh action")}
|
||||
}
|
||||
res := r.ssh.Execute(ctx, *a.SSH)
|
||||
output := res.Stdout
|
||||
if res.Stderr != "" {
|
||||
output += "\nstderr: " + res.Stderr
|
||||
}
|
||||
return Result{Action: a, Output: output, Err: res.Err}
|
||||
|
||||
default:
|
||||
return Result{Action: a, Err: fmt.Errorf("unhandled action kind: %s", a.Kind)}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
package effects
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"testing"
|
||||
|
||||
"github.com/enmanuel/agents/pkg/decision"
|
||||
)
|
||||
|
||||
// fakeSender records calls for assertions.
|
||||
type fakeSender struct {
|
||||
calls []senderCall
|
||||
}
|
||||
|
||||
type senderCall struct {
|
||||
method string
|
||||
roomID string
|
||||
threadRootID string
|
||||
inReplyTo string
|
||||
markdown string
|
||||
}
|
||||
|
||||
func (f *fakeSender) SendText(ctx context.Context, roomID, text string) error {
|
||||
f.calls = append(f.calls, senderCall{method: "SendText", roomID: roomID, markdown: text})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeSender) SendMarkdown(ctx context.Context, roomID, markdown string) error {
|
||||
f.calls = append(f.calls, senderCall{method: "SendMarkdown", roomID: roomID, markdown: markdown})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeSender) SendReplyMarkdown(ctx context.Context, roomID, inReplyTo, markdown string) error {
|
||||
f.calls = append(f.calls, senderCall{method: "SendReplyMarkdown", roomID: roomID, inReplyTo: inReplyTo, markdown: markdown})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeSender) SendThreadMarkdown(ctx context.Context, roomID, threadRootID, inReplyTo, markdown string) error {
|
||||
f.calls = append(f.calls, senderCall{method: "SendThreadMarkdown", roomID: roomID, threadRootID: threadRootID, inReplyTo: inReplyTo, markdown: markdown})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeSender) SendTyping(ctx context.Context, roomID string, typing bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestExecuteReply_PlainMarkdown(t *testing.T) {
|
||||
sender := &fakeSender{}
|
||||
runner := NewRunner(sender, nil, slog.Default())
|
||||
|
||||
actions := []decision.Action{{
|
||||
Kind: decision.ActionKindReply,
|
||||
Reply: &decision.ReplyAction{Content: "hello"},
|
||||
}}
|
||||
|
||||
results := runner.Execute(context.Background(), "!room:test", actions)
|
||||
if len(results) != 1 || results[0].Err != nil {
|
||||
t.Fatalf("unexpected results: %+v", results)
|
||||
}
|
||||
if len(sender.calls) != 1 {
|
||||
t.Fatalf("expected 1 call, got %d", len(sender.calls))
|
||||
}
|
||||
c := sender.calls[0]
|
||||
if c.method != "SendMarkdown" {
|
||||
t.Errorf("expected SendMarkdown, got %s", c.method)
|
||||
}
|
||||
if c.roomID != "!room:test" {
|
||||
t.Errorf("expected room !room:test, got %s", c.roomID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteReply_WithInReplyTo(t *testing.T) {
|
||||
sender := &fakeSender{}
|
||||
runner := NewRunner(sender, nil, slog.Default())
|
||||
|
||||
actions := []decision.Action{{
|
||||
Kind: decision.ActionKindReply,
|
||||
Reply: &decision.ReplyAction{Content: "hello", InReplyTo: "$evt1"},
|
||||
}}
|
||||
|
||||
runner.Execute(context.Background(), "!room:test", actions)
|
||||
|
||||
if len(sender.calls) != 1 {
|
||||
t.Fatalf("expected 1 call, got %d", len(sender.calls))
|
||||
}
|
||||
c := sender.calls[0]
|
||||
if c.method != "SendReplyMarkdown" {
|
||||
t.Errorf("expected SendReplyMarkdown, got %s", c.method)
|
||||
}
|
||||
if c.inReplyTo != "$evt1" {
|
||||
t.Errorf("expected inReplyTo=$evt1, got %s", c.inReplyTo)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteReply_WithThread(t *testing.T) {
|
||||
sender := &fakeSender{}
|
||||
runner := NewRunner(sender, nil, slog.Default())
|
||||
|
||||
actions := []decision.Action{{
|
||||
Kind: decision.ActionKindReply,
|
||||
Reply: &decision.ReplyAction{
|
||||
Content: "thread reply",
|
||||
ThreadID: "$root",
|
||||
InReplyTo: "$evt2",
|
||||
},
|
||||
}}
|
||||
|
||||
runner.Execute(context.Background(), "!room:test", actions)
|
||||
|
||||
if len(sender.calls) != 1 {
|
||||
t.Fatalf("expected 1 call, got %d", len(sender.calls))
|
||||
}
|
||||
c := sender.calls[0]
|
||||
if c.method != "SendThreadMarkdown" {
|
||||
t.Errorf("expected SendThreadMarkdown, got %s", c.method)
|
||||
}
|
||||
if c.threadRootID != "$root" {
|
||||
t.Errorf("expected threadRootID=$root, got %s", c.threadRootID)
|
||||
}
|
||||
if c.inReplyTo != "$evt2" {
|
||||
t.Errorf("expected inReplyTo=$evt2, got %s", c.inReplyTo)
|
||||
}
|
||||
if c.roomID != "!room:test" {
|
||||
t.Errorf("expected room !room:test, got %s", c.roomID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteReply_ThreadWithoutInReplyTo(t *testing.T) {
|
||||
sender := &fakeSender{}
|
||||
runner := NewRunner(sender, nil, slog.Default())
|
||||
|
||||
actions := []decision.Action{{
|
||||
Kind: decision.ActionKindReply,
|
||||
Reply: &decision.ReplyAction{
|
||||
Content: "thread reply no fallback",
|
||||
ThreadID: "$root",
|
||||
},
|
||||
}}
|
||||
|
||||
runner.Execute(context.Background(), "!room:test", actions)
|
||||
|
||||
if len(sender.calls) != 1 {
|
||||
t.Fatalf("expected 1 call, got %d", len(sender.calls))
|
||||
}
|
||||
c := sender.calls[0]
|
||||
if c.method != "SendThreadMarkdown" {
|
||||
t.Errorf("expected SendThreadMarkdown, got %s", c.method)
|
||||
}
|
||||
// inReplyTo should be empty; SendThreadMarkdown will default to threadRootID
|
||||
if c.inReplyTo != "" {
|
||||
t.Errorf("expected empty inReplyTo, got %s", c.inReplyTo)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteReply_NilReply(t *testing.T) {
|
||||
sender := &fakeSender{}
|
||||
runner := NewRunner(sender, nil, slog.Default())
|
||||
|
||||
actions := []decision.Action{{
|
||||
Kind: decision.ActionKindReply,
|
||||
Reply: nil,
|
||||
}}
|
||||
|
||||
results := runner.Execute(context.Background(), "!room:test", actions)
|
||||
if len(results) != 1 {
|
||||
t.Fatalf("expected 1 result, got %d", len(results))
|
||||
}
|
||||
if results[0].Err == nil {
|
||||
t.Error("expected error for nil reply")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user