]> git.giacomolli.uk - onechat/blobdiff - whatsapp_state.go
Order model and chat declarations
[onechat] / whatsapp_state.go
index b1b5935c7536833728bea6a06230998b1ae605af..825fa189e307aafe2dafc6733cdc56e7eb34dbea 100644 (file)
@@ -31,32 +31,6 @@ const (
        whatsappSendRecords = 1000
 )
 
        whatsappSendRecords = 1000
 )
 
-// openWhatsAppState reads the batches and the send records of the state file at
-// 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{
-               path: path,
-       }
-
-       data, err := os.ReadFile(path)
-       if errors.Is(err, os.ErrNotExist) {
-               return s, nil
-       }
-
-       if err != nil {
-               return nil, fmt.Errorf("read WhatsApp state: %w", err)
-       }
-
-       err = json.Unmarshal(data, &s.file)
-       if err != nil {
-               return nil, fmt.Errorf("%s: %w", path, err)
-       }
-
-       return s, nil
-}
-
 // 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
@@ -83,6 +57,56 @@ type WhatsAppState struct {
        file  WhatsAppStateFile
 }
 
        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"`
+}
+
+// openWhatsAppState reads the batches and the send records of the state file at
+// 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{
+               path: path,
+       }
+
+       data, err := os.ReadFile(path)
+       if errors.Is(err, os.ErrNotExist) {
+               return s, nil
+       }
+
+       if err != nil {
+               return nil, fmt.Errorf("read WhatsApp state: %w", err)
+       }
+
+       err = json.Unmarshal(data, &s.file)
+       if err != nil {
+               return nil, fmt.Errorf("%s: %w", path, err)
+       }
+
+       return s, nil
+}
+
 // Add puts message of chat into the open batch of chat, or into a new batch,
 // with arrival as its time of arrival. A message that the batch holds already,
 // which the servers sent again, changes nothing. It returns true when a batch
 // Add puts message of chat into the open batch of chat, or into a new batch,
 // with arrival as its time of arrival. A message that the batch holds already,
 // which the servers sent again, changes nothing. It returns true when a batch
@@ -287,25 +311,3 @@ func (s *WhatsAppState) save() error {
 
        return nil
 }
 
        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"`
-}