+// 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 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
+// whatsappSendRecords records.
+//
+// Each change writes the state file whole, so the batches and the records
+// survive a restart. The mutex protects them, because the event handler of the
+// library adds messages while Run closes and removes batches, and the turns
+// send.
+type WhatsAppState struct {
+ path string
+
+ mutex sync.Mutex
+ file WhatsAppStateFile
+}
+
+// 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, 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"`
+ Image string `json:"image,omitempty"`
+ MessageID string `json:"message_id"`
+ State string `json:"state"`
+ Answer string `json:"answer,omitempty"`
+ Time time.Time `json:"time"`
+}
+