Skip to content

Commit a74d2b6

Browse files
authored
refactor: change Query::[no_]stream from bool to StreamMode (#159)
Signed-off-by: Jean Mertz <git@jeanmertz.com>
1 parent 6d418b0 commit a74d2b6

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

crates/jp_cli/src/cmd/query.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use std::{
66
str::FromStr,
77
};
88

9-
use clap::{builder::TypedValueParser as _, ArgAction};
9+
use clap::{
10+
builder::{BoolishValueParser, TypedValueParser as _},
11+
ArgAction,
12+
};
1013
use crossterm::style::{Color, Stylize as _};
1114
use futures::StreamExt as _;
1215
use jp_config::{expand_tilde, style::code::LinkStyle};
@@ -114,15 +117,17 @@ pub struct Args {
114117
///
115118
/// This is the default behaviour for TTY sessions, but can be forced for
116119
/// non-TTY sessions by setting this flag.
117-
#[arg(short = 's', long = "stream", conflicts_with = "no_stream")]
118-
pub stream: bool,
120+
#[arg(short = 's', long = "stream", conflicts_with = "no_stream", value_parser = BoolishValueParser::new()
121+
.map(|b| if b { StreamMode::Forced(true) } else { StreamMode::Auto }))]
122+
pub stream: StreamMode,
119123

120124
/// Disable streaming the assistant's response.
121125
///
122126
/// This is the default behaviour for non-TTY sessions, or for structured
123127
/// responses, but can be forced by setting this flag.
124-
#[arg(short = 'S', long = "no-stream", conflicts_with = "stream")]
125-
pub no_stream: bool,
128+
#[arg(short = 'S', long = "no-stream", conflicts_with = "stream", value_parser = BoolishValueParser::new()
129+
.map(|b| if b { StreamMode::Forced(false) } else { StreamMode::Auto }))]
130+
pub no_stream: StreamMode,
126131

127132
/// The tool to use.
128133
///
@@ -141,7 +146,7 @@ pub struct Args {
141146

142147
/// The stream mode to use for the assistant's response.
143148
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
144-
enum StreamMode {
149+
pub(crate) enum StreamMode {
145150
/// Use the default stream mode, depending on whether the output is a TTY,
146151
/// and if a structured response is requested.
147152
#[default]
@@ -163,14 +168,6 @@ impl Args {
163168
let last_active_conversation_id = ctx.workspace.active_conversation_id();
164169
let conversation_id = self.get_conversation_id(ctx)?;
165170

166-
let stream_mode = if self.stream {
167-
StreamMode::Forced(true)
168-
} else if self.no_stream {
169-
StreamMode::Forced(false)
170-
} else {
171-
StreamMode::Auto
172-
};
173-
174171
self.update_context(ctx).await?;
175172

176173
// Ensure we start the MCP servers attached to the conversation.
@@ -253,7 +250,7 @@ impl Args {
253250
tools.clone(),
254251
tool_choice,
255252
&mut messages,
256-
stream_mode,
253+
self.stream,
257254
)
258255
.await?;
259256
}
@@ -285,7 +282,7 @@ impl Args {
285282
}
286283

287284
if self.schema.is_some() && !reply.is_empty() {
288-
if let StreamMode::Forced(true) = stream_mode {
285+
if let StreamMode::Forced(true) = self.stream {
289286
stdout::typewriter(&reply, ctx.config.style.typewriter.code_delay)?;
290287
} else {
291288
return Ok(Success::Json(serde_json::from_str(&reply)?));

0 commit comments

Comments
 (0)