]> git.giacomolli.uk - onechat/blobdiff - whatsapp_state.go
Keep test daemon sockets under the path limit
[onechat] / whatsapp_state.go
index c09423d83d6e73b97ede0feb152f8f701006a391..3d21b1680e3d0c25e58bb1860317fcf46613a32a 100644 (file)
@@ -16,12 +16,12 @@ const (
        // the batches that are not in the log yet and the records of the sends.
        whatsappStateFileName = "state.json"
 
        // the batches that are not in the log yet and the records of the sends.
        whatsappStateFileName = "state.json"
 
-       // whatsappBatchQuiet is how long a chat is quiet before its batch
+       // whatsappBatchIdleTimeout is how long a chat is quiet before its batch
        // closes.
        // closes.
-       whatsappBatchQuiet = 20 * time.Second
+       whatsappBatchIdleTimeout = 20 * time.Second
 
 
-       // whatsappBatchWindow is the longest time of a batch.
-       whatsappBatchWindow = 2 * time.Minute
+       // whatsappBatchTimeout is the longest time of a batch.
+       whatsappBatchTimeout = 2 * time.Minute
 
        // whatsappBatchMessages is the most messages of a batch.
        whatsappBatchMessages = 50
 
        // whatsappBatchMessages is the most messages of a batch.
        whatsappBatchMessages = 50
@@ -32,13 +32,12 @@ const (
 )
 
 // openWhatsAppState reads the batches and the send records of the state file at
 )
 
 // openWhatsAppState reads the batches and the send records of the state file at
-// path. A missing file holds none. A batch closes after quiet with no message,
-// or after window at the latest.
-func openWhatsAppState(path string, quiet, window time.Duration) (*WhatsAppState, error) {
+// path. A missing file holds none. A batch closes after
+// whatsappBatchIdleTimeout with no message, or after whatsappBatchTimeout at
+// the latest.
+func openWhatsAppState(path string) (*WhatsAppState, error) {
        s := &WhatsAppState{
        s := &WhatsAppState{
-               path:   path,
-               quiet:  quiet,
-               window: window,
+               path: path,
        }
 
        data, err := os.ReadFile(path)
        }
 
        data, err := os.ReadFile(path)
@@ -61,11 +60,13 @@ func openWhatsAppState(path string, quiet, window time.Duration) (*WhatsAppState
 // WhatsAppState holds the state file: the send records, and the batches of the
 // WhatsApp messages that are not in the log yet, open and closed, in the order
 // of their first message. The messages of one chat form one batch while the
 // WhatsAppState holds the state file: the send records, and the batches of the
 // WhatsApp messages that are not in the log yet, open and closed, in the order
 // of their first message. The messages of one chat form one batch while the
-// times of the messages are at most quiet apart, at most window after the
-// first, and the batch has fewer than whatsappBatchMessages messages. The times
-// are the WhatsApp times, so a catch-up after a stop gives the same batches as
-// live messages. A batch also closes when quiet passes after the arrival of its
-// last message, or window after the arrival of its first message, in real time.
+// times of the messages are at most whatsappBatchIdleTimeout apart, at most
+// whatsappBatchTimeout after the first, and the batch has fewer than
+// whatsappBatchMessages messages. The times are the WhatsApp times, so a
+// catch-up after a stop gives the same batches as live messages. A batch also
+// closes when whatsappBatchIdleTimeout passes after the arrival of its last
+// message, or whatsappBatchTimeout after the arrival of its first message, in
+// real time.
 //
 // A send record keeps the result of a send by its ID, so that a repeated call
 // of the send tool does not send again. The state file keeps the last
 //
 // A send record keeps the result of a send by its ID, so that a repeated call
 // of the send tool does not send again. The state file keeps the last
@@ -76,9 +77,7 @@ func openWhatsAppState(path string, quiet, window time.Duration) (*WhatsAppState
 // library adds messages while Run closes and removes batches, and the turns
 // send.
 type WhatsAppState struct {
 // library adds messages while Run closes and removes batches, and the turns
 // send.
 type WhatsAppState struct {
-       path   string
-       quiet  time.Duration
-       window time.Duration
+       path string
 
        mutex sync.Mutex
        file  WhatsAppStateFile
 
        mutex sync.Mutex
        file  WhatsAppStateFile
@@ -102,7 +101,7 @@ func (s *WhatsAppState) Add(chat WhatsAppChat, message WhatsAppMessage, arrival
                        return false, nil
                }
 
                        return false, nil
                }
 
-               if message.Time.Sub(batch.Messages[len(batch.Messages)-1].Time) > s.quiet || message.Time.Sub(batch.Messages[0].Time) > s.window {
+               if message.Time.Sub(batch.Messages[len(batch.Messages)-1].Time) > whatsappBatchIdleTimeout || message.Time.Sub(batch.Messages[0].Time) > whatsappBatchTimeout {
                        batch.Closed = true
                        closed = true
                        index = -1
                        batch.Closed = true
                        closed = true
                        index = -1
@@ -261,8 +260,8 @@ func (s *WhatsAppState) open(chat string) int {
 
 // deadline returns when batch closes in real time.
 func (s *WhatsAppState) deadline(batch WhatsAppBatch) time.Time {
 
 // deadline returns when batch closes in real time.
 func (s *WhatsAppState) deadline(batch WhatsAppBatch) time.Time {
-       quiet := batch.Last.Add(s.quiet)
-       window := batch.First.Add(s.window)
+       quiet := batch.Last.Add(whatsappBatchIdleTimeout)
+       window := batch.First.Add(whatsappBatchTimeout)
        if window.Before(quiet) {
                return window
        }
        if window.Before(quiet) {
                return window
        }
@@ -299,12 +298,14 @@ type WhatsAppStateFile struct {
 }
 
 // WhatsAppSend is the record of one send: the ID that the agent gave, the chat,
 }
 
 // WhatsAppSend is the record of one send: the ID that the agent gave, the chat,
-// the SHA-256 of the text, the WhatsApp message ID, the state, which is
-// sending, sent, or failed, the answer of the tool, and the time of its start.
+// the SHA-256 of the text, an optional stored image name, the WhatsApp message
+// ID, the state, which is sending, sent, or failed, the answer of the tool, and
+// the time of its start.
 type WhatsAppSend struct {
        ID        string    `json:"id"`
        Chat      string    `json:"chat"`
        Digest    string    `json:"digest"`
 type WhatsAppSend struct {
        ID        string    `json:"id"`
        Chat      string    `json:"chat"`
        Digest    string    `json:"digest"`
+       Image     string    `json:"image,omitempty"`
        MessageID string    `json:"message_id"`
        State     string    `json:"state"`
        Answer    string    `json:"answer,omitempty"`
        MessageID string    `json:"message_id"`
        State     string    `json:"state"`
        Answer    string    `json:"answer,omitempty"`