Write an agent

Implement the agent port, register an agent with the server and serve it over HTTP, WebSocket and gRPC.

An agent plugs into the conversation server through a two-method port. The server calls it when a participant sends a message, and again when an approval is answered.

The port

type Agent interface {
    Reply(ctx context.Context, history []protocol.Event) (protocol.AgentTurn, error)
    Resume(ctx context.Context, history []protocol.Event, req protocol.ApprovalRequest, approved bool) (protocol.AgentTurn, error)
}
  • Reply receives the conversation’s events so far and returns one turn.
  • Resume runs after an approval was answered. approved tells you the answer. The req is the request you returned earlier.

A turn has three possible shapes:

ReturnMeaning
PartsThe agent’s reply. Parts carry text, structured data, or a URL with a media type
ApprovalThe agent needs approval before it continues. The server creates an approval obligation
Outcome"succeeded" or "failed". The server records an outcome event

An agent’s output is a proposal. The server records it; it does not execute anything on the agent’s behalf.

A minimal agent

package main

import (
    "context"
    "encoding/json"
    "strings"

    "github.com/robotelos/robotelos/protocol"
)

type echo struct{}

func (echo) Reply(ctx context.Context, history []protocol.Event) (protocol.AgentTurn, error) {
    return protocol.AgentTurn{
        Parts:   []protocol.Part{protocol.TextPart("you said: " + lastText(history))},
        Outcome: "succeeded",
    }, nil
}

func (echo) Resume(ctx context.Context, history []protocol.Event, req protocol.ApprovalRequest, approved bool) (protocol.AgentTurn, error) {
    if !approved {
        return protocol.AgentTurn{Parts: []protocol.Part{protocol.TextPart("Understood, I did not run it.")}}, nil
    }
    return protocol.AgentTurn{Parts: []protocol.Part{protocol.TextPart("Approved.")}}, nil
}

// lastText returns the text of the most recent event, when it is a message.
func lastText(history []protocol.Event) string {
    if len(history) == 0 {
        return ""
    }
    var mc protocol.MessageContent
    if err := json.Unmarshal(history[len(history)-1].Content, &mc); err != nil {
        return ""
    }
    var b strings.Builder
    for _, p := range mc.Parts {
        if p.Text != nil {
            b.WriteString(*p.Text)
        }
    }
    return b.String()
}

protocol/mockagent is a complete example: it asks for approval when a message starts with run .

Serve it

Register the agent and start the listeners. server.New takes the agents; serve.Start takes the bearer tokens:

srv, err := server.New(server.Config{
    Agents: []server.AgentSpec{{ID: "echo", Version: "1", Display: "Echo", Agent: echo{}}},
})
if err != nil {
    return err
}

l, err := serve.Start(srv, serve.Config{
    HTTPAddr: "127.0.0.1:8787",
    GRPCAddr: "127.0.0.1:8788",
    Tokens: map[string]server.Principal{
        "demo-alice": {ID: "par_alice", Account: "acct_alice", Display: "Alice"},
    },
})
if err != nil {
    return err
}
defer l.Close()

Clients then create conversations with agent = "echo". Keep tokens secret and store them in configuration.

Rules for agents

  • Return promptly from Reply. Long work belongs behind the approval and outcome events, not in one blocking call.
  • Honor the context. The server cancels it when the turn is abandoned.
  • Do not assume a disconnected client receives your reply. Disconnects cancel nothing; the event is in the journal.
  • Do not hold credentials or customer data in the agent’s memory across turns. The journal is the record.

Next steps