diff options
Diffstat (limited to '')
| -rw-r--r-- | src/session.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/session.rs b/src/session.rs index 881b1ed..8fc6d29 100644 --- a/src/session.rs +++ b/src/session.rs | |||
| @@ -252,6 +252,31 @@ impl Session { | |||
| 252 | &self.path | 252 | &self.path |
| 253 | } | 253 | } |
| 254 | 254 | ||
| 255 | /// Rewrites the transcript from scratch: a fresh header plus the given | ||
| 256 | /// messages. This is the one sanctioned rewrite, called only by `/compact` | ||
| 257 | /// (see ADR 0003); everything else appends. | ||
| 258 | pub fn rewrite(&mut self, system: &Value, messages: &[Value]) -> Result<(), String> { | ||
| 259 | use std::io::{Seek, SeekFrom, Write}; | ||
| 260 | self.file.set_len(0).map_err(|e| e.to_string())?; | ||
| 261 | self.file.seek(SeekFrom::Start(0)).map_err(|e| e.to_string())?; | ||
| 262 | let header = json!({ | ||
| 263 | "t": "header", | ||
| 264 | "session": self.id, | ||
| 265 | "model": self.model, | ||
| 266 | "cwd": self.cwd, | ||
| 267 | "version": self.version, | ||
| 268 | "created": now_ms(), | ||
| 269 | "system": system, | ||
| 270 | }); | ||
| 271 | writeln!(self.file, "{header}").map_err(|e| e.to_string())?; | ||
| 272 | for message in messages { | ||
| 273 | let mut line = message.as_object().cloned().unwrap_or_default(); | ||
| 274 | line.insert("t".into(), json!("msg")); | ||
| 275 | writeln!(self.file, "{}", Value::Object(line)).map_err(|e| e.to_string())?; | ||
| 276 | } | ||
| 277 | self.file.flush().map_err(|e| e.to_string()) | ||
| 278 | } | ||
| 279 | |||
| 255 | /// Records a model switch so `--list` shows what the last turn used. Appending | 280 | /// Records a model switch so `--list` shows what the last turn used. Appending |
| 256 | /// is legal; the header itself is never rewritten. | 281 | /// is legal; the header itself is never rewritten. |
| 257 | pub fn record_model(&mut self, model: &str) -> Result<(), String> { | 282 | pub fn record_model(&mut self, model: &str) -> Result<(), String> { |
