Server SDK

A complete Go program that implements an agent, registers it with the conversation server and serves it over HTTP, WebSocket and gRPC.

This program uses the Go server SDK (protocol/server and protocol/server/serve). It registers two agents: the built-in deterministic mock agent and a small echo agent that you write. Clients from the Client SDK example or the terminal chat can talk to either.

The program

Save it as main.go in a Go module that depends on github.com/robotelos/robotelos:

package main

import (
	"context"
	"encoding/json"
	"log"
	"os"
	"os/signal"
	"strings"
	"time"

	"github.com/robotelos/robotelos/protocol"
	"github.com/robotelos/robotelos/protocol/mockagent"
	"github.com/robotelos/robotelos/protocol/server"
	"github.com/robotelos/robotelos/protocol/server/serve"
)

// echo replies with the text of the latest message.
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
}

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()
}

func main() {
	// 1. Register agents. "mock" is the built-in deterministic agent;
	//    "echo" is the one written above.
	srv, err := server.New(server.Config{
		Agents: []server.AgentSpec{
			{ID: "mock", Version: "1", Display: "Mock", Agent: mockagent.Agent{Delay: 300 * time.Millisecond}},
			{ID: "echo", Version: "1", Display: "Echo", Agent: echo{}},
		},
	})
	if err != nil {
		log.Fatal(err)
	}
	defer srv.Close()

	// 2. Serve HTTP (SSE and WebSocket) and gRPC. Tokens map to principals.
	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"},
			"demo-bob":   {ID: "par_bob", Account: "acct_bob", Display: "Bob"},
		},
	})
	if err != nil {
		log.Fatal(err)
	}
	defer l.Close()
	log.Printf("sse: %s  websocket: %s  grpc: %s", l.SSEURL, l.WebSocketURL, l.GRPCAddr)

	// 3. Run until Ctrl-C.
	ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
	defer stop()
	<-ctx.Done()
}

Run it

go mod tidy
go run .

The server logs the three addresses it listens on:

sse: http://127.0.0.1:8787  websocket: ws://127.0.0.1:8787/v1/ws  grpc: 127.0.0.1:8788

Test the echo agent from the terminal chat. The chat client opens the mock agent by default, so pass the agent ID:

go run ./protocol/cmd/robotelos-chat -transport sse -agent echo

What each part does

PartPurpose
ReplyCalled when a participant writes a message. Return the parts of the reply and an outcome.
ResumeCalled after an approval is answered. approved says which answer it was.
AgentTurn.ApprovalSet this instead of a reply when the agent needs approval. The server opens an obligation and calls Resume after the answer. The mock agent does this for run <command>.
server.AgentSpecRegisters the agent under an ID and version. Clients name the ID when they create a conversation.
server.ConfigOther fields: Workers (how many turns run at once; a negative number runs none, so the process only takes commands), TurnLease and MaxTurnAttempts (for crash recovery), and Store (a PostgreSQL store replaces the in-memory default).
serve.StartListens on HTTP, WebSocket and gRPC. An empty GRPCAddr disables gRPC.
serve.Config.TokensMaps bearer tokens to principals. Keep tokens secret and store them in configuration.

Rules for agents

  • Return from Reply promptly. Long work belongs behind an approval or an outcome, not in one blocking call.
  • Honor the context: the server cancels it when a turn is abandoned.
  • A disconnected client does not cancel the turn. The reply is in the journal, and the client reads it when it reconnects.
  • Keep no customer data in the agent’s memory across turns. The journal is the record.

The Write an agent guide explains the port in more detail.

Next steps