saveCheckpoint creates a checkpoint of the current conversation state. Called automatically before each user message is sent to the LLM.
()
| 26 | // saveCheckpoint creates a checkpoint of the current conversation state. |
| 27 | // Called automatically before each user message is sent to the LLM. |
| 28 | func (cli *ChatCLI) saveCheckpoint() { |
| 29 | // Build a short label from the last user message |
| 30 | label := "" |
| 31 | for i := len(cli.history) - 1; i >= 0; i-- { |
| 32 | if cli.history[i].Role == "user" { |
| 33 | label = cli.history[i].Content |
| 34 | break |
| 35 | } |
| 36 | } |
| 37 | if label == "" { |
| 38 | label = "(start)" |
| 39 | } |
| 40 | // Truncate label |
| 41 | label = strings.ReplaceAll(label, "\n", " ") |
| 42 | if len(label) > 60 { |
| 43 | label = label[:57] + "..." |
| 44 | } |
| 45 | |
| 46 | // Deep copy history |
| 47 | snapshot := make([]models.Message, len(cli.history)) |
| 48 | copy(snapshot, cli.history) |
| 49 | |
| 50 | cp := conversationCheckpoint{ |
| 51 | Timestamp: time.Now(), |
| 52 | Label: label, |
| 53 | History: snapshot, |
| 54 | MsgCount: len(cli.history), |
| 55 | } |
| 56 | |
| 57 | cli.checkpoints = append(cli.checkpoints, cp) |
| 58 | |
| 59 | // Keep only last N checkpoints |
| 60 | if len(cli.checkpoints) > maxCheckpoints { |
| 61 | cli.checkpoints = cli.checkpoints[len(cli.checkpoints)-maxCheckpoints:] |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // showRewindMenu displays the rewind menu and handles user selection. |
| 66 | // Returns true if a rewind was performed. |
no outgoing calls