Client SDK
A complete Go program that connects to a conversation server, sends a message, answers an approval and prints the reply.
This program uses the Go client SDK (protocol/client) to do the whole round trip against a running server. It needs the server from the Server SDK example, or the local stack from Chat from a terminal.
The program
Save it as main.go in any Go module that depends on github.com/robotelos/robotelos:
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/robotelos/robotelos/protocol"
"github.com/robotelos/robotelos/protocol/client"
"github.com/robotelos/robotelos/protocol/client/dial"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
// 1. Connect. The server from the server example listens on these addresses.
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 {
log.Fatal(err)
}
defer c.Close()
if _, err := c.Initialize(ctx, protocol.InitializeRequest{
Client: protocol.ClientInfo{Name: "client-example", Kind: "adapter"},
}); err != nil {
log.Fatal(err)
}
// 2. Create a conversation with the mock agent and follow it.
rcp, err := c.CreateConversation(ctx, protocol.NewID("cmd"), "mock", "hello from the client SDK")
if err != nil {
log.Fatal(err)
}
if rcp.Disposition != protocol.Accepted {
log.Fatalf("create conversation: %s %v", rcp.Disposition, rcp.Error)
}
sub, err := c.Subscribe(ctx, rcp.ConversationID, nil)
if err != nil {
log.Fatal(err)
}
// 3. Send a message. "run" asks the mock agent for an approval.
if _, err := c.SendText(ctx, rcp.ConversationID, protocol.NewID("cmd"), "run ls -la"); err != nil {
log.Fatal(err)
}
// 4. Read frames. Print each message once, answer the approval when it is
// open, and stop when the agent replies after the answer.
printed := 0
answered := false
for {
if _, err := sub.Next(ctx); err != nil {
log.Fatal(err)
}
conv := client.Project(rcp.ConversationID, "par_alice", sub.View())
for _, m := range conv.Messages[printed:] {
fmt.Printf("%s: %s\n", m.SenderName, m.Text)
}
printed = len(conv.Messages)
if !answered {
for _, ob := range conv.Obligations {
if ob.Status != "open" {
continue
}
receipt, err := c.AnswerObligation(ctx, sub, ob.ID, protocol.AnswerApprove)
if err != nil {
log.Fatal(err)
}
fmt.Println("answer:", receipt.Disposition)
answered = true
break
}
continue
}
if n := len(conv.Messages); n > 0 && !conv.Messages[n-1].Mine {
return
}
}
}
Run it
With the server running, in a second terminal:
go run .
The output looks like this. The approval is accepted and the agent reports its result:
Alice: run ls -la
answer: ACCEPTED
Mock: outcome (succeeded): Done. Ran: ls -la
What each step does
| Step | Call | Why it matters |
|---|---|---|
| Connect | dial.Client | Picks the binding (dial.WebSocket, dial.SSE or dial.GRPC) and sends the bearer token. Every binding carries the same messages. |
| Initialize | c.Initialize | Identifies the client. Call it once per connection. |
| Create | c.CreateConversation | Sends a command with a new command_id. The receipt says whether it was accepted. |
| Follow | c.Subscribe, sub.Next | Each frame updates the view that the SDK keeps for you. sub.View() is the current state. |
| Project | client.Project | Turns the view into messages, participants and open obligations for one reader. The second argument is your own participant ID. |
| Send | c.SendText | Sends a message as a command. Retrying with the same command_id and text is safe. |
| Approve | c.AnswerObligation | Takes the obligation’s version and digest from the view, so the answer applies only to what was shown. The receipt is ACCEPTED, STALE or REJECTED. |
Make it resilient
To survive transient failures and reconnects, wrap the client in client.Reliable and resume with the last view. The Go client guide explains both.
Next steps
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.