| | @@ -1,1 +1,109 @@ |
| 1 | +// Package protocol defines the scuttlebot wire format. |
| 2 | +// |
| 3 | +// Agent messages are JSON envelopes sent as IRC PRIVMSG. |
| 4 | +// System/status messages use NOTICE and are human-readable only. |
| 1 | 5 | package protocol |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "math/rand" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/oklog/ulid/v2" |
| 14 | +) |
| 15 | + |
| 16 | +// Version is the current envelope version. |
| 17 | +const Version = 1 |
| 18 | + |
| 19 | +// Message types. |
| 20 | +const ( |
| 21 | + TypeTaskCreate = "task.create" |
| 22 | + TypeTaskUpdate = "task.update" |
| 23 | + TypeTaskComplete = "task.complete" |
| 24 | + TypeAgentHello = "agent.hello" |
| 25 | + TypeAgentBye = "agent.bye" |
| 26 | +) |
| 27 | + |
| 28 | +// Envelope is the standard wrapper for all agent messages over IRC. |
| 29 | +type Envelope struct { |
| 30 | + V int `json:"v"` |
| 31 | + Type string `json:"type"` |
| 32 | + ID string `json:"id"` |
| 33 | + From string `json:"from"` |
| 34 | + TS int64 `json:"ts"` |
| 35 | + Payload json.RawMessage `json:"payload,omitempty"` |
| 36 | +} |
| 37 | + |
| 38 | +// New creates a new Envelope with a generated ID and current timestamp. |
| 39 | +func New(msgType, from string, payload any) (*Envelope, error) { |
| 40 | + var raw json.RawMessage |
| 41 | + if payload != nil { |
| 42 | + b, err := json.Marshal(payload) |
| 43 | + if err != nil { |
| 44 | + return nil, fmt.Errorf("protocol: marshal payload: %w", err) |
| 45 | + } |
| 46 | + raw = b |
| 47 | + } |
| 48 | + return &Envelope{ |
| 49 | + V: Version, |
| 50 | + Type: msgType, |
| 51 | + ID: newID(), |
| 52 | + From: from, |
| 53 | + TS: time.Now().UnixMilli(), |
| 54 | + Payload: raw, |
| 55 | + }, nil |
| 56 | +} |
| 57 | + |
| 58 | +// Marshal encodes the envelope to JSON. |
| 59 | +func Marshal(e *Envelope) ([]byte, error) { |
| 60 | + b, err := json.Marshal(e) |
| 61 | + if err != nil { |
| 62 | + return nil, fmt.Errorf("protocol: marshal envelope: %w", err) |
| 63 | + } |
| 64 | + return b, nil |
| 65 | +} |
| 66 | + |
| 67 | +// Unmarshal decodes a JSON envelope and validates it. |
| 68 | +func Unmarshal(data []byte) (*Envelope, error) { |
| 69 | + var e Envelope |
| 70 | + if err := json.Unmarshal(data, &e); err != nil { |
| 71 | + return nil, fmt.Errorf("protocol: unmarshal envelope: %w", err) |
| 72 | + } |
| 73 | + if err := validate(&e); err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + return &e, nil |
| 77 | +} |
| 78 | + |
| 79 | +// UnmarshalPayload decodes the envelope payload into dst. |
| 80 | +func UnmarshalPayload(e *Envelope, dst any) error { |
| 81 | + if len(e.Payload) == 0 { |
| 82 | + return nil |
| 83 | + } |
| 84 | + if err := json.Unmarshal(e.Payload, dst); err != nil { |
| 85 | + return fmt.Errorf("protocol: unmarshal payload: %w", err) |
| 86 | + } |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func validate(e *Envelope) error { |
| 91 | + if e.V != Version { |
| 92 | + return fmt.Errorf("protocol: unsupported version %d", e.V) |
| 93 | + } |
| 94 | + if e.Type == "" { |
| 95 | + return fmt.Errorf("protocol: missing type") |
| 96 | + } |
| 97 | + if e.ID == "" { |
| 98 | + return fmt.Errorf("protocol: missing id") |
| 99 | + } |
| 100 | + if e.From == "" { |
| 101 | + return fmt.Errorf("protocol: missing from") |
| 102 | + } |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +func newID() string { |
| 107 | + entropy := ulid.Monotonic(rand.New(rand.NewSource(time.Now().UnixNano())), 0) //nolint:gosec |
| 108 | + return ulid.MustNew(ulid.Timestamp(time.Now()), entropy).String() |
| 109 | +} |
| 2 | 110 | |
| 3 | 111 | ADDED pkg/protocol/protocol_test.go |