fix: CLI poll loop used UUID instead of queue cursor (last_id)

The poll loop was storing msg.ID (UUID string) as afterID, but the server
expects the integer queue cursor from last_id. This caused the CLI to
re-fetch ALL messages on every poll cycle.

- Change PollMessages to accept int64 afterID and return PollResult with LastID
- Track lastQID (queue cursor) instead of lastMsgID (UUID)
- Parse the wrapped MessagesResponse properly
This commit is contained in:
clawbot
2026-02-10 18:22:08 -08:00
parent ae1c67050e
commit 5c95d7cdf6
3 changed files with 30 additions and 28 deletions

View File

@@ -97,21 +97,18 @@ func (c *Client) SendMessage(msg *Message) error {
return err
}
// PollMessages long-polls for new messages.
func (c *Client) PollMessages(afterID string, timeout int) ([]Message, error) {
// PollMessages long-polls for new messages. afterID is the queue cursor (last_id).
func (c *Client) PollMessages(afterID int64, timeout int) (*PollResult, error) {
// Use a longer HTTP timeout than the server long-poll timeout.
client := &http.Client{Timeout: time.Duration(timeout+5) * time.Second}
params := url.Values{}
if afterID != "" {
params.Set("after", afterID)
if afterID > 0 {
params.Set("after", fmt.Sprintf("%d", afterID))
}
params.Set("timeout", fmt.Sprintf("%d", timeout))
path := "/api/v1/messages"
if len(params) > 0 {
path += "?" + params.Encode()
}
path := "/api/v1/messages?" + params.Encode()
req, err := http.NewRequest("GET", c.BaseURL+path, nil)
if err != nil {
@@ -134,18 +131,15 @@ func (c *Client) PollMessages(afterID string, timeout int) ([]Message, error) {
return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, string(data))
}
// The server may return an array directly or wrapped.
var msgs []Message
if err := json.Unmarshal(data, &msgs); err != nil {
// Try wrapped format.
var wrapped MessagesResponse
if err2 := json.Unmarshal(data, &wrapped); err2 != nil {
return nil, fmt.Errorf("decode messages: %w (raw: %s)", err, string(data))
}
msgs = wrapped.Messages
var wrapped MessagesResponse
if err := json.Unmarshal(data, &wrapped); err != nil {
return nil, fmt.Errorf("decode messages: %w (raw: %s)", err, string(data))
}
return msgs, nil
return &PollResult{
Messages: wrapped.Messages,
LastID: wrapped.LastID,
}, nil
}
// JoinChannel joins a channel via the unified command endpoint.