Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/chat-cli/src/cli/chat/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ pub struct Chat {
/// prompt requests permissions to use a tool, unless --trust-all-tools is also used.
#[arg(long)]
pub no_interactive: bool,
/// Start a new conversation and overwrites any previous conversation from this directory.
#[arg(long)]
pub new: bool,
/// The first question to ask
pub input: Option<String>,
/// Context profile to use
Expand Down
55 changes: 40 additions & 15 deletions crates/chat-cli/src/cli/chat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ pub async fn launch_chat(database: &mut Database, telemetry: &TelemetryThread, a
telemetry,
args.input,
args.no_interactive,
args.new,
args.accept_all,
args.profile,
args.trust_all_tools,
Expand All @@ -306,12 +307,13 @@ pub async fn launch_chat(database: &mut Database, telemetry: &TelemetryThread, a
.await
}

#[allow(clippy::too_many_arguments)]
#[allow(clippy::too_many_arguments, clippy::fn_params_excessive_bools)]
pub async fn chat(
database: &mut Database,
telemetry: &TelemetryThread,
input: Option<String>,
no_interactive: bool,
new_conversation: bool,
accept_all: bool,
profile: Option<String>,
trust_all_tools: bool,
Expand Down Expand Up @@ -440,6 +442,7 @@ pub async fn chat(
input,
InputSource::new(database, prompt_request_sender, prompt_response_receiver)?,
interactive,
new_conversation,
client,
|| terminal::window_size().map(|s| s.columns.into()).ok(),
tool_manager,
Expand Down Expand Up @@ -526,6 +529,7 @@ impl ChatContext {
mut input: Option<String>,
input_source: InputSource,
interactive: bool,
new_conversation: bool,
client: StreamingClient,
terminal_width_provider: fn() -> Option<usize>,
tool_manager: ToolManager,
Expand All @@ -537,21 +541,38 @@ impl ChatContext {
let output_clone = output.clone();

let mut existing_conversation = false;
let conversation_state = match std::env::current_dir()
.ok()
.and_then(|cwd| database.get_conversation_by_path(cwd).ok())
.flatten()
{
Some(mut prior) => {
let conversation_state = if new_conversation {
let new_state = ConversationState::new(
ctx_clone,
conversation_id,
tool_config,
profile,
Some(output_clone),
tool_manager,
)
.await;

std::env::current_dir()
.ok()
.and_then(|cwd| database.set_conversation_by_path(cwd, &new_state).ok());
new_state
} else {
let prior = std::env::current_dir()
.ok()
.and_then(|cwd| database.get_conversation_by_path(cwd).ok())
.flatten();

// Only restore conversations where there were actual messages.
// Prevents edge case where user clears conversation with --new, then exits without chatting.
if prior.as_ref().is_some_and(|cs| !cs.history().is_empty()) {
let mut cs = prior.unwrap();

existing_conversation = true;
prior
.reload_serialized_state(Arc::clone(&ctx), Some(output.clone()))
.await;
cs.reload_serialized_state(Arc::clone(&ctx), Some(output.clone())).await;
input = Some(input.unwrap_or("In a few words, summarize our conversation so far.".to_owned()));
prior.tool_manager = tool_manager;
prior
},
None => {
cs.tool_manager = tool_manager;
cs
} else {
ConversationState::new(
ctx_clone,
conversation_id,
Expand All @@ -561,7 +582,7 @@ impl ChatContext {
tool_manager,
)
.await
},
}
};

Ok(Self {
Expand Down Expand Up @@ -3692,6 +3713,7 @@ mod tests {
"exit".to_string(),
]),
true,
false,
test_client,
|| Some(80),
tool_manager,
Expand Down Expand Up @@ -3837,6 +3859,7 @@ mod tests {
"exit".to_string(),
]),
true,
false,
test_client,
|| Some(80),
tool_manager,
Expand Down Expand Up @@ -3935,6 +3958,7 @@ mod tests {
"exit".to_string(),
]),
true,
false,
test_client,
|| Some(80),
tool_manager,
Expand Down Expand Up @@ -4012,6 +4036,7 @@ mod tests {
"exit".to_string(),
]),
true,
false,
test_client,
|| Some(80),
tool_manager,
Expand Down
12 changes: 10 additions & 2 deletions crates/chat-cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ mod test {
subcommand: Some(CliRootCommands::Chat(Chat {
accept_all: false,
no_interactive: false,
new: false,
input: None,
profile: None,
trust_all_tools: false,
Expand Down Expand Up @@ -407,6 +408,7 @@ mod test {
CliRootCommands::Chat(Chat {
accept_all: false,
no_interactive: false,
new: false,
input: None,
profile: Some("my-profile".to_string()),
trust_all_tools: false,
Expand All @@ -422,6 +424,7 @@ mod test {
CliRootCommands::Chat(Chat {
accept_all: false,
no_interactive: false,
new: false,
input: Some("Hello".to_string()),
profile: Some("my-profile".to_string()),
trust_all_tools: false,
Expand All @@ -437,6 +440,7 @@ mod test {
CliRootCommands::Chat(Chat {
accept_all: true,
no_interactive: false,
new: false,
input: None,
profile: Some("my-profile".to_string()),
trust_all_tools: false,
Expand All @@ -446,12 +450,13 @@ mod test {
}

#[test]
fn test_chat_with_no_interactive() {
fn test_chat_with_no_interactive_new() {
assert_parse!(
["chat", "--no-interactive"],
["chat", "--no-interactive", "--new"],
CliRootCommands::Chat(Chat {
accept_all: false,
no_interactive: true,
new: true,
input: None,
profile: None,
trust_all_tools: false,
Expand All @@ -467,6 +472,7 @@ mod test {
CliRootCommands::Chat(Chat {
accept_all: false,
no_interactive: false,
new: false,
input: None,
profile: None,
trust_all_tools: true,
Expand All @@ -482,6 +488,7 @@ mod test {
CliRootCommands::Chat(Chat {
accept_all: false,
no_interactive: false,
new: false,
input: None,
profile: None,
trust_all_tools: false,
Expand All @@ -497,6 +504,7 @@ mod test {
CliRootCommands::Chat(Chat {
accept_all: false,
no_interactive: false,
new: false,
input: None,
profile: None,
trust_all_tools: false,
Expand Down
Loading