整文件 JSON 保存。 对应源码: session.rs:88-91 pub fn save_to_path(&self, path) -> Result<(), SessionError> { fs::write(path, self.to_json().render())?; Ok(()) }
(session: Session, path: str)
| 235 | # 这是 Rust 源码中实际实现的方式(简单但有局限) |
| 236 | |
| 237 | def save_session_json(session: Session, path: str): |
| 238 | """ |
| 239 | 整文件 JSON 保存。 |
| 240 | |
| 241 | 对应源码: session.rs:88-91 |
| 242 | pub fn save_to_path(&self, path) -> Result<(), SessionError> { |
| 243 | fs::write(path, self.to_json().render())?; |
| 244 | Ok(()) |
| 245 | } |
| 246 | """ |
| 247 | data = { |
| 248 | "version": session.version, |
| 249 | "session_id": session.session_id, |
| 250 | "messages": [m.to_dict() for m in session.messages], |
| 251 | } |
| 252 | with open(path, "w") as f: |
| 253 | json.dump(data, f, indent=2, ensure_ascii=False) |
| 254 | |
| 255 | |
| 256 | def load_session_json(path: str) -> Session: |