1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
use anyhow::{Context, Result};
use clap::Parser;
use is_terminal::IsTerminal;
use std::io::{stdin, Read};
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
/// Select a LLM model
#[clap(short, long)]
pub model: Option<String>,
/// Use the system prompt
#[clap(long)]
pub prompt: Option<String>,
/// Select a role
#[clap(short, long)]
pub role: Option<String>,
/// Start or join a session
#[clap(short = 's', long)]
pub session: Option<Option<String>>,
/// Ensure the session is empty
#[clap(long)]
pub empty_session: bool,
/// Ensure the new conversation is saved to the session
#[clap(long)]
pub save_session: bool,
/// Start a agent
#[clap(short = 'a', long)]
pub agent: Option<String>,
/// Set agent variables
#[clap(long, value_names = ["NAME", "VALUE"], num_args = 2)]
pub agent_variable: Vec<String>,
/// Start a RAG
#[clap(long)]
pub rag: Option<String>,
/// Rebuild the RAG to sync document changes
#[clap(long)]
pub rebuild_rag: bool,
/// Execute a macro
#[clap(long = "macro", value_name = "MACRO")]
pub macro_name: Option<String>,
/// Serve the LLM API and WebAPP
#[clap(long, value_name = "ADDRESS")]
pub serve: Option<Option<String>>,
/// Execute commands in natural language
#[clap(short = 'e', long)]
pub execute: bool,
/// Output code only
#[clap(short = 'c', long)]
pub code: bool,
/// Include files, directories, or URLs
#[clap(short = 'f', long, value_name = "FILE")]
pub file: Vec<String>,
/// Turn off stream mode
#[clap(short = 'S', long)]
pub no_stream: bool,
/// Display the message without sending it
#[clap(long)]
pub dry_run: bool,
/// Display information
#[clap(long)]
pub info: bool,
/// Sync models updates
#[clap(long)]
pub sync_models: bool,
/// List all available chat models
#[clap(long)]
pub list_models: bool,
/// List all roles
#[clap(long)]
pub list_roles: bool,
/// List all sessions
#[clap(long)]
pub list_sessions: bool,
/// List all agents
#[clap(long)]
pub list_agents: bool,
/// List all RAGs
#[clap(long)]
pub list_rags: bool,
/// List all macros
#[clap(long)]
pub list_macros: bool,
/// Input text
#[clap(trailing_var_arg = true)]
text: Vec<String>,
}
impl Cli {
pub fn text(&self) -> Result<Option<String>> {
let mut stdin_text = String::new();
if !stdin().is_terminal() {
let _ = stdin()
.read_to_string(&mut stdin_text)
.context("Invalid stdin pipe")?;
};
match self.text.is_empty() {
true => {
if stdin_text.is_empty() {
Ok(None)
} else {
Ok(Some(stdin_text))
}
}
false => {
if self.macro_name.is_some() {
let text = self
.text
.iter()
.map(|v| shell_words::quote(v))
.collect::<Vec<_>>()
.join(" ");
if stdin_text.is_empty() {
Ok(Some(text))
} else {
Ok(Some(format!("{} -- {}", text, stdin_text)))
}
} else {
let text = self.text.join(" ");
if stdin_text.is_empty() {
Ok(Some(text))
} else {
Ok(Some(format!("{}\n{}", text, stdin_text)))
}
}
}
}
}
}
|