-
Notifications
You must be signed in to change notification settings - Fork 3.2k
refs(#2264): Phase 4 — replace Session.messages: Vec<Message> with AppendLog #2579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -195,16 +195,15 @@ impl std::fmt::Display for PrefixDrift { | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // ── AppendLog ────────────────────────────────────────────────────────── | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /// Append-only conversation history. Only exposes `push`-style mutations. | ||||||||||||||||||||||||
| /// Append-only conversation history. Derefs to [`Vec<Message>`] for | ||||||||||||||||||||||||
| /// transparent read access; mutations go through `push()` / `From`. | ||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||
| /// **Phase 1 scaffolding** — not yet wired into the engine request path. | ||||||||||||||||||||||||
| #[allow(dead_code)] | ||||||||||||||||||||||||
| /// Phase 4: backing store for `Session.messages` (#2264). | ||||||||||||||||||||||||
| #[derive(Debug, Clone)] | ||||||||||||||||||||||||
| pub struct AppendLog { | ||||||||||||||||||||||||
| messages: Vec<Message>, | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| #[allow(dead_code)] | ||||||||||||||||||||||||
| impl AppendLog { | ||||||||||||||||||||||||
| pub fn new() -> Self { | ||||||||||||||||||||||||
| Self { | ||||||||||||||||||||||||
|
|
@@ -216,33 +215,47 @@ impl AppendLog { | |||||||||||||||||||||||
| Self { messages } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /// Append a message to the log. | ||||||||||||||||||||||||
| pub fn push(&mut self, message: Message) { | ||||||||||||||||||||||||
| self.messages.push(message); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /// Consume and return the inner `Vec<Message>`. | ||||||||||||||||||||||||
| #[must_use] | ||||||||||||||||||||||||
| pub fn len(&self) -> usize { | ||||||||||||||||||||||||
| self.messages.len() | ||||||||||||||||||||||||
| pub fn into_inner(self) -> Vec<Message> { | ||||||||||||||||||||||||
| self.messages | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| #[must_use] | ||||||||||||||||||||||||
| pub fn is_empty(&self) -> bool { | ||||||||||||||||||||||||
| self.messages.is_empty() | ||||||||||||||||||||||||
| impl Default for AppendLog { | ||||||||||||||||||||||||
| fn default() -> Self { | ||||||||||||||||||||||||
| Self::new() | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| impl From<Vec<Message>> for AppendLog { | ||||||||||||||||||||||||
| fn from(messages: Vec<Message>) -> Self { | ||||||||||||||||||||||||
| Self { messages } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| pub fn iter(&self) -> impl Iterator<Item = &Message> { | ||||||||||||||||||||||||
| self.messages.iter() | ||||||||||||||||||||||||
| impl From<AppendLog> for Vec<Message> { | ||||||||||||||||||||||||
| fn from(log: AppendLog) -> Self { | ||||||||||||||||||||||||
| log.messages | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| #[must_use] | ||||||||||||||||||||||||
| pub fn as_slice(&self) -> &[Message] { | ||||||||||||||||||||||||
| impl std::ops::Deref for AppendLog { | ||||||||||||||||||||||||
| type Target = Vec<Message>; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| fn deref(&self) -> &Self::Target { | ||||||||||||||||||||||||
| &self.messages | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| impl Default for AppendLog { | ||||||||||||||||||||||||
| fn default() -> Self { | ||||||||||||||||||||||||
| Self::new() | ||||||||||||||||||||||||
| impl std::ops::DerefMut for AppendLog { | ||||||||||||||||||||||||
| fn deref_mut(&mut self) -> &mut Self::Target { | ||||||||||||||||||||||||
| &mut self.messages | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
Comment on lines
+256
to
260
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
@@ -525,7 +538,6 @@ mod tests { | |||||||||||||||||||||||
| let msgs = vec![make_message("user", "a"), make_message("assistant", "b")]; | ||||||||||||||||||||||||
| let log = AppendLog::from_messages(msgs); | ||||||||||||||||||||||||
| assert_eq!(log.len(), 2); | ||||||||||||||||||||||||
| assert_eq!(log.as_slice().len(), 2); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // ── TurnScratch ─────────────────────────────────────────────── | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clone().into_inner()allocates a fullAppendLogclone just to immediately unwrap it intoVec<Message>. SinceDeref<Target = Vec<Message>>is now implemented, the innerVeccan be cloned directly without constructing the intermediateAppendLog.