-
-// open returns the index of the open batch of chat, or -1. The caller holds the
-// mutex.
-func (s *WhatsAppState) open(chat string) int {
- return slices.IndexFunc(s.file.Batches, func(batch WhatsAppBatch) bool {
- return !batch.Closed && batch.Chat.ID == chat
- })
-}
-
-// deadline returns when batch closes in real time.
-func (s *WhatsAppState) deadline(batch WhatsAppBatch) time.Time {
- quiet := batch.Last.Add(whatsappBatchIdleTimeout)
- window := batch.First.Add(whatsappBatchTimeout)
- if window.Before(quiet) {
- return window
- }
-
- return quiet
-}
-
-// save writes the state file whole. The caller holds the mutex.
-func (s *WhatsAppState) save() error {
- data, err := json.Marshal(s.file)
- if err != nil {
- return fmt.Errorf("encode WhatsApp state: %w", err)
- }
-
- err = os.MkdirAll(filepath.Dir(s.path), 0o700)
- if err == nil {
- err = writeFileAtomic(s.path, append(data, '\n'))
- }
-
- if err != nil {
- return fmt.Errorf("save WhatsApp state: %w", err)
- }
-
- return nil
-}
-
-// WhatsAppStateFile is the content of the state file: the number of the last
-// batch, the batches that are not in the log yet, and the send records, oldest
-// first.
-type WhatsAppStateFile struct {
- Next int `json:"next"`
- Batches []WhatsAppBatch `json:"batches"`
- Sends []WhatsAppSend `json:"sends"`
-}
-
-// 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.
-type WhatsAppSend struct {
- ID string `json:"id"`
- Chat string `json:"chat"`
- Digest string `json:"digest"`
- MessageID string `json:"message_id"`
- State string `json:"state"`
- Answer string `json:"answer,omitempty"`
- Time time.Time `json:"time"`
-}