|
1
|
package client |
|
2
|
|
|
3
|
import ( |
|
4
|
"bytes" |
|
5
|
"context" |
|
6
|
"encoding/json" |
|
7
|
"fmt" |
|
8
|
"net/http" |
|
9
|
) |
|
10
|
|
|
11
|
// ChannelInfo is the result of creating or looking up a channel. |
|
12
|
type ChannelInfo struct { |
|
13
|
// Channel is the full IRC channel name (e.g. "#task.gh-42"). |
|
14
|
Channel string `json:"channel"` |
|
15
|
|
|
16
|
// Type is the channel type name from the topology policy (e.g. "task", "sprint"). |
|
17
|
// Empty if the channel does not match any configured type. |
|
18
|
Type string `json:"type,omitempty"` |
|
19
|
|
|
20
|
// Supervision is the coordination/supervision channel where summaries |
|
21
|
// from this channel should also be posted (e.g. "#general"). Empty if none. |
|
22
|
Supervision string `json:"supervision,omitempty"` |
|
23
|
|
|
24
|
// Autojoin is the list of bot nicks that were invited when the channel was created. |
|
25
|
Autojoin []string `json:"autojoin,omitempty"` |
|
26
|
} |
|
27
|
|
|
28
|
// ChannelTypeInfo describes a class of channels defined in the topology policy. |
|
29
|
type ChannelTypeInfo struct { |
|
30
|
// Name is the type identifier (e.g. "task", "sprint"). |
|
31
|
Name string `json:"name"` |
|
32
|
|
|
33
|
// Prefix is the channel name prefix (e.g. "task."). |
|
34
|
Prefix string `json:"prefix"` |
|
35
|
|
|
36
|
// Autojoin is the list of bot nicks invited when a channel of this type is created. |
|
37
|
Autojoin []string `json:"autojoin,omitempty"` |
|
38
|
|
|
39
|
// Supervision is the coordination channel for this type, or empty. |
|
40
|
Supervision string `json:"supervision,omitempty"` |
|
41
|
|
|
42
|
// Ephemeral indicates channels of this type are automatically reaped. |
|
43
|
Ephemeral bool `json:"ephemeral,omitempty"` |
|
44
|
|
|
45
|
// TTLSeconds is the maximum lifetime in seconds for ephemeral channels, or zero. |
|
46
|
TTLSeconds int64 `json:"ttl_seconds,omitempty"` |
|
47
|
} |
|
48
|
|
|
49
|
// TopologyClient calls the scuttlebot HTTP API to provision and discover channels. |
|
50
|
// It complements the IRC-based Client for the dual-channel pattern: agents create |
|
51
|
// a task channel here and get back the supervision channel where they should also post. |
|
52
|
type TopologyClient struct { |
|
53
|
apiURL string |
|
54
|
token string |
|
55
|
http *http.Client |
|
56
|
} |
|
57
|
|
|
58
|
// NewTopologyClient creates a TopologyClient. |
|
59
|
// apiURL is the base URL of the scuttlebot API (e.g. "http://localhost:8080"). |
|
60
|
// token is the Bearer token issued by scuttlebot. |
|
61
|
func NewTopologyClient(apiURL, token string) *TopologyClient { |
|
62
|
return &TopologyClient{ |
|
63
|
apiURL: apiURL, |
|
64
|
token: token, |
|
65
|
http: &http.Client{}, |
|
66
|
} |
|
67
|
} |
|
68
|
|
|
69
|
type createChannelReq struct { |
|
70
|
Name string `json:"name"` |
|
71
|
Topic string `json:"topic,omitempty"` |
|
72
|
Ops []string `json:"ops,omitempty"` |
|
73
|
Voice []string `json:"voice,omitempty"` |
|
74
|
Autojoin []string `json:"autojoin,omitempty"` |
|
75
|
} |
|
76
|
|
|
77
|
// CreateChannel provisions an IRC channel via the scuttlebot topology API. |
|
78
|
// The server applies autojoin policy and invites the configured bots. |
|
79
|
// Returns a ChannelInfo with the channel name, type, and supervision channel. |
|
80
|
// |
|
81
|
// Example: create a task channel for a GitHub issue. |
|
82
|
// |
|
83
|
// info, err := topo.CreateChannel(ctx, "#task.gh-42", "GitHub issue #42") |
|
84
|
// if err != nil { ... } |
|
85
|
// // post activity to info.Channel, summaries to info.Supervision |
|
86
|
func (t *TopologyClient) CreateChannel(ctx context.Context, name, topic string) (ChannelInfo, error) { |
|
87
|
body, err := json.Marshal(createChannelReq{Name: name, Topic: topic}) |
|
88
|
if err != nil { |
|
89
|
return ChannelInfo{}, fmt.Errorf("topology: marshal request: %w", err) |
|
90
|
} |
|
91
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, t.apiURL+"/v1/channels", bytes.NewReader(body)) |
|
92
|
if err != nil { |
|
93
|
return ChannelInfo{}, fmt.Errorf("topology: build request: %w", err) |
|
94
|
} |
|
95
|
req.Header.Set("Content-Type", "application/json") |
|
96
|
req.Header.Set("Authorization", "Bearer "+t.token) |
|
97
|
|
|
98
|
resp, err := t.http.Do(req) |
|
99
|
if err != nil { |
|
100
|
return ChannelInfo{}, fmt.Errorf("topology: create channel: %w", err) |
|
101
|
} |
|
102
|
defer resp.Body.Close() |
|
103
|
if resp.StatusCode != http.StatusCreated { |
|
104
|
var apiErr struct { |
|
105
|
Error string `json:"error"` |
|
106
|
} |
|
107
|
_ = json.NewDecoder(resp.Body).Decode(&apiErr) |
|
108
|
return ChannelInfo{}, fmt.Errorf("topology: create channel: %s", apiErr.Error) |
|
109
|
} |
|
110
|
var info ChannelInfo |
|
111
|
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil { |
|
112
|
return ChannelInfo{}, fmt.Errorf("topology: decode response: %w", err) |
|
113
|
} |
|
114
|
return info, nil |
|
115
|
} |
|
116
|
|
|
117
|
// DropChannel drops an ephemeral channel via the scuttlebot topology API. |
|
118
|
// The ChanServ registration is removed and the channel will be vacated. |
|
119
|
func (t *TopologyClient) DropChannel(ctx context.Context, channel string) error { |
|
120
|
if len(channel) < 2 || channel[0] != '#' { |
|
121
|
return fmt.Errorf("topology: invalid channel name %q", channel) |
|
122
|
} |
|
123
|
slug := channel[1:] // strip leading # |
|
124
|
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, t.apiURL+"/v1/topology/channels/"+slug, nil) |
|
125
|
if err != nil { |
|
126
|
return fmt.Errorf("topology: build request: %w", err) |
|
127
|
} |
|
128
|
req.Header.Set("Authorization", "Bearer "+t.token) |
|
129
|
resp, err := t.http.Do(req) |
|
130
|
if err != nil { |
|
131
|
return fmt.Errorf("topology: drop channel: %w", err) |
|
132
|
} |
|
133
|
defer resp.Body.Close() |
|
134
|
if resp.StatusCode != http.StatusNoContent { |
|
135
|
var apiErr struct { |
|
136
|
Error string `json:"error"` |
|
137
|
} |
|
138
|
_ = json.NewDecoder(resp.Body).Decode(&apiErr) |
|
139
|
return fmt.Errorf("topology: drop channel: %s", apiErr.Error) |
|
140
|
} |
|
141
|
return nil |
|
142
|
} |
|
143
|
|
|
144
|
type topologyResp struct { |
|
145
|
StaticChannels []string `json:"static_channels"` |
|
146
|
Types []ChannelTypeInfo `json:"types"` |
|
147
|
} |
|
148
|
|
|
149
|
// GetTopology returns the channel type rules and static channels from the server. |
|
150
|
func (t *TopologyClient) GetTopology(ctx context.Context) ([]string, []ChannelTypeInfo, error) { |
|
151
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, t.apiURL+"/v1/topology", nil) |
|
152
|
if err != nil { |
|
153
|
return nil, nil, fmt.Errorf("topology: build request: %w", err) |
|
154
|
} |
|
155
|
req.Header.Set("Authorization", "Bearer "+t.token) |
|
156
|
resp, err := t.http.Do(req) |
|
157
|
if err != nil { |
|
158
|
return nil, nil, fmt.Errorf("topology: get topology: %w", err) |
|
159
|
} |
|
160
|
defer resp.Body.Close() |
|
161
|
if resp.StatusCode != http.StatusOK { |
|
162
|
return nil, nil, fmt.Errorf("topology: get topology: status %d", resp.StatusCode) |
|
163
|
} |
|
164
|
var body topologyResp |
|
165
|
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil { |
|
166
|
return nil, nil, fmt.Errorf("topology: decode response: %w", err) |
|
167
|
} |
|
168
|
return body.StaticChannels, body.Types, nil |
|
169
|
} |
|
170
|
|
|
171
|
// PostActivity sends a structured message to the task/activity channel. |
|
172
|
// It is a convenience wrapper around client.Send for the dual-channel pattern. |
|
173
|
func PostActivity(ctx context.Context, c *Client, channel, msgType string, payload any) error { |
|
174
|
return c.Send(ctx, channel, msgType, payload) |
|
175
|
} |
|
176
|
|
|
177
|
// PostSummary sends a structured message to the supervision channel. |
|
178
|
// supervision is the channel returned by CreateChannel (info.Supervision). |
|
179
|
// It is a no-op if supervision is empty. |
|
180
|
func PostSummary(ctx context.Context, c *Client, supervision, msgType string, payload any) error { |
|
181
|
if supervision == "" { |
|
182
|
return nil |
|
183
|
} |
|
184
|
return c.Send(ctx, supervision, msgType, payload) |
|
185
|
} |
|
186
|
|