Skip to content

Commit ddfc8ba

Browse files
authored
revives conversation load (#2250)
1 parent a566a2a commit ddfc8ba

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

crates/chat-cli/src/cli/chat/cli/persist.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crossterm::style::{
66
Color,
77
};
88

9+
use crate::cli::ConversationState;
910
use crate::cli::chat::{
1011
ChatError,
1112
ChatSession,
@@ -74,17 +75,37 @@ impl PersistSubcommand {
7475
style::SetAttribute(Attribute::Reset)
7576
)?;
7677
},
77-
Self::Load { path: _ } => {
78-
// For profile operations that need a profile name, show profile selector
79-
// As part of the agent implementation, we are disabling the ability to
80-
// switch profile after a session has started.
81-
// TODO: perhaps revive this after we have a decision on profile switching
78+
Self::Load { path } => {
79+
// Try the original path first
80+
let original_result = os.fs.read_to_string(&path).await;
81+
82+
// If the original path fails and doesn't end with .json, try with .json appended
83+
let contents = if original_result.is_err() && !path.ends_with(".json") {
84+
let json_path = format!("{}.json", path);
85+
match os.fs.read_to_string(&json_path).await {
86+
Ok(content) => content,
87+
Err(_) => {
88+
// If both paths fail, return the original error for better user experience
89+
tri!(original_result, "import from", &path)
90+
},
91+
}
92+
} else {
93+
tri!(original_result, "import from", &path)
94+
};
95+
96+
let mut new_state: ConversationState = tri!(serde_json::from_str(&contents), "import from", &path);
97+
std::mem::swap(&mut new_state.tool_manager, &mut session.conversation.tool_manager);
98+
std::mem::swap(
99+
&mut new_state.context_manager,
100+
&mut session.conversation.context_manager,
101+
);
102+
std::mem::swap(&mut new_state.agents, &mut session.conversation.agents);
103+
session.conversation = new_state;
104+
82105
execute!(
83106
session.stderr,
84-
style::SetForegroundColor(Color::Yellow),
85-
style::Print(
86-
"Conversation loading has been disabled. To load a conversation. Quit and restart q chat."
87-
),
107+
style::SetForegroundColor(Color::Green),
108+
style::Print(format!("\n✔ Imported conversation state from {}\n\n", &path)),
88109
style::SetAttribute(Attribute::Reset)
89110
)?;
90111
},

0 commit comments

Comments
 (0)