Go client

Connect to a conversation server, create a conversation, follow it and answer an approval with the Go client SDK.

The protocol/client package is the Go client SDK. It works over any transport the server offers, and the same code runs in tests and in applications.

Connect

dial.Client opens a connection over ws, sse or grpc:

import (
    "github.com/robotelos/robotelos/protocol"
    "github.com/robotelos/robotelos/protocol/client/dial"
)

c, err := dial.Client(ctx, dial.Options{
    Transport: dial.WebSocket,
    URL:       "ws://127.0.0.1:8787/v1/ws",
    Token:     "demo-alice",
})
if err != nil {
    return err
}
defer c.Close()

_, err = c.Initialize(ctx, protocol.InitializeRequest{
    Client: protocol.ClientInfo{Name: "my-app"},
})

Use dial.SSE with http://127.0.0.1:8787 or dial.GRPC with 127.0.0.1:8788 for the other bindings.

Create a conversation and send a message

rcp, err := c.CreateConversation(ctx, protocol.NewID("cmd"), "mock", "hello")
if err != nil {
    return err
}
if rcp.Disposition != protocol.Accepted {
    return fmt.Errorf("create conversation: %s %v", rcp.Disposition, rcp.Error)
}

_, err = c.SendText(ctx, rcp.ConversationID, protocol.NewID("cmd"), "run ls -la")

protocol.NewID("cmd") generates a command ID. Keep it if you retry the same command: a retry with the same ID and payload is safe (Commands and receipts).

Follow the conversation

Subscribe and read frames in order. The subscription keeps a view that the reducer updates for each frame:

sub, err := c.Subscribe(ctx, rcp.ConversationID, nil) // or pass a saved *protocol.View to resume
if err != nil {
    return err
}
for {
    if _, err := sub.Next(ctx); err != nil {
        return err
    }
    ui := client.Project(rcp.ConversationID, "par_alice", sub.View())
    // ui holds messages, surfaces and open obligations ready to display
}

sub.WaitUpToDate(ctx) waits until the snapshot has caught up with the end of the journal. sub.AwaitEcho(ctx, commandID) waits until your own command has come back through the stream; a consumer goroutine must be running for it.

To keep a conversation flowing across reconnects, use client.Follow. It resubscribes from the last cursor and asks for a resnapshot when the server requires one. If the consumer falls behind, the subscription returns ErrSlowConsumer; WithQueueLimit changes the bound.

Answer an approval

When the view shows an open obligation, answer it with its ID and the answer. The client takes the expected version and the request digest from the obligation in the view, not from the surface, so a button cannot change what is approved:

receipt, err := c.AnswerObligation(ctx, sub, obligationID, protocol.AnswerApprove)
if err != nil {
    return err
}
_ = receipt // ACCEPTED, STALE or REJECTED; see Commands and receipts

protocol.AnswerApprove is "approve"; protocol/obligation.go lists the other answer kinds.

Retries

Wrap the client in client.Reliable to retry commands on transient failures with backoff and jitter. The wrapper reuses the command ID. It never retries an answer to an obligation.

Next steps