| 1 | package draftstate |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/hex" |
| 6 | "encoding/json" |
| 7 | ) |
| 8 | |
| 9 | // SnapshotDigest hashes canonical JSON, preserving array order and exact text. |
| 10 | func SnapshotDigest(content, settings string) (string, error) { |
| 11 | var body, profile any |
| 12 | if err := json.Unmarshal([]byte(content), &body); err != nil { |
| 13 | return "", err |
| 14 | } |
| 15 | if err := json.Unmarshal([]byte(settings), &profile); err != nil { |
| 16 | return "", err |
| 17 | } |
| 18 | if object, ok := body.(map[string]any); ok { |
| 19 | if attachments, ok := object["attachments"].([]any); ok { |
| 20 | for _, item := range attachments { |
| 21 | if attachment, ok := item.(map[string]any); ok { |
| 22 | delete(attachment, "previewUrl") |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | if object, ok := profile.(map[string]any); ok && object["modelSource"] == "default" { |
| 28 | // The concrete model is a compatibility mirror. Inherited defaults remain |
| 29 | // live until submission, so they are not part of the editable draft's |
| 30 | // semantic identity. |
| 31 | delete(object, "model") |
| 32 | } |
| 33 | canonical, err := json.Marshal([]any{body, profile}) |
| 34 | if err != nil { |
| 35 | return "", err |
| 36 | } |
| 37 | sum := sha256.Sum256(canonical) |
| 38 | return hex.EncodeToString(sum[:]), nil |
| 39 | } |
| 40 |