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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
mod config;
use std::io::{stdout, Write};
use std::path::Path;
use std::path::PathBuf;
use std::process::exit;
use std::time::Duration;
use config::{Config, Role};
use anyhow::{anyhow, Result};
use clap::{Arg, ArgAction, Command};
use eventsource_stream::{EventStream, Eventsource};
use futures_util::Stream;
use futures_util::StreamExt;
use inquire::{Confirm, Editor, Text};
use reedline::{
default_emacs_keybindings, ColumnarMenu, DefaultCompleter, DefaultPrompt, DefaultPromptSegment,
Emacs, FileBackedHistory, KeyCode, KeyModifiers, Reedline, ReedlineEvent, ReedlineMenu, Signal,
};
use reqwest::{Client, Proxy};
use serde_json::{json, Value};
use tokio::runtime::Runtime;
const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
const API_URL: &str = "https://api.openai.com/v1/chat/completions";
const MODEL: &str = "gpt-3.5-turbo";
const REPL_COMMANDS: [(&str, &str); 7] = [
(".clear", "Clear the screen"),
(".clear-history", "Clear the history"),
(".exit", "Exit the REPL"),
(".help", "Print this help message"),
(".history", "Print the history"),
(".role", "Specify the role that the AI will play"),
(".view", "Use an external editor to view the AI reply"),
];
fn main() {
if let Err(err) = start() {
eprintln!("error: {err}");
exit(1);
}
}
fn start() -> Result<()> {
let matches = Command::new(env!("CARGO_CRATE_NAME"))
.version(env!("CARGO_PKG_VERSION"))
.author(env!("CARGO_PKG_AUTHORS"))
.about(concat!(
env!("CARGO_PKG_DESCRIPTION"),
" - ",
env!("CARGO_PKG_REPOSITORY")
))
.arg(
Arg::new("list-roles")
.short('L')
.long("list-roles")
.action(ArgAction::SetTrue)
.help("List all roles"),
)
.arg(
Arg::new("role")
.short('r')
.long("role")
.action(ArgAction::Set)
.help("Specify the role that the AI will play"),
)
.arg(
Arg::new("text")
.action(ArgAction::Append)
.help("Input text"),
)
.get_matches();
let mut text = matches.get_many::<String>("text").map(|v| {
v.map(|x| x.trim().to_string())
.collect::<Vec<String>>()
.join(" ")
});
let config_path = get_config_path()?;
if !config_path.exists() && text.is_none() {
create_config_file(&config_path)?;
}
let config = Config::init(&config_path)?;
let role_name = matches.get_one::<String>("role").cloned();
if let (Some(name), Some(text_)) = (role_name.as_ref(), text.as_ref()) {
let role = config
.roles
.iter()
.find(|v| &v.name == name)
.ok_or_else(|| anyhow!("Unknown role \"{name}\" "))?;
text = Some(role.generate(text_));
};
if matches.get_flag("list-roles") {
config.roles.iter().for_each(|v| println!("{}", v.name));
exit(1);
}
let client = init_client(&config)?;
let runtime = init_runtime()?;
match text {
Some(text) => {
let output = runtime.block_on(async move { acquire(&client, &config, &text).await })?;
println!("{}", output.trim());
}
None => run_repl(runtime, client, config, role_name)?,
}
Ok(())
}
fn run_repl(
runtime: Runtime,
client: Client,
config: Config,
role_name: Option<String>,
) -> Result<()> {
print_repl_title();
let mut commands: Vec<String> = REPL_COMMANDS
.into_iter()
.map(|(v, _)| v.to_string())
.collect();
commands.extend(config.roles.iter().map(|v| format!(".role {}", v.name)));
let mut completer = DefaultCompleter::with_inclusions(&['.', '-']).set_min_word_len(2);
completer.insert(commands.clone());
let completer = Box::new(completer);
let completion_menu = Box::new(ColumnarMenu::default().with_name("completion_menu"));
let mut keybindings = default_emacs_keybindings();
keybindings.add_binding(
KeyModifiers::NONE,
KeyCode::Tab,
ReedlineEvent::UntilFound(vec![
ReedlineEvent::Menu("completion_menu".to_string()),
ReedlineEvent::MenuNext,
]),
);
let history = Box::new(
FileBackedHistory::with_file(1000, get_history_path()?)
.map_err(|err| anyhow!("Failed to setup history file, {err}"))?,
);
let edit_mode = Box::new(Emacs::new(keybindings));
let mut line_editor = Reedline::create()
.with_completer(completer)
.with_history(history)
.with_menu(ReedlineMenu::EngineCompleter(completion_menu))
.with_edit_mode(edit_mode);
let prompt = DefaultPrompt::new(DefaultPromptSegment::Empty, DefaultPromptSegment::Empty);
let mut trigged_ctrlc = false;
let mut output = String::new();
let mut role: Option<Role> = None;
let handle_line = |line: String,
line_editor: &mut Reedline,
trigged_ctrlc: &mut bool,
role: &mut Option<Role>,
output: &mut String|
-> Result<bool> {
if line.starts_with('.') {
let (name, args) = match line.split_once(' ') {
Some((head, tail)) => (head, Some(tail.trim())),
None => (line.as_str(), None),
};
match name {
".view" => {
if output.is_empty() {
return Ok(false);
}
let _ = Editor::new("view ai reply with an external editor")
.with_file_extension(".md")
.with_predefined_text(output)
.prompt()?;
dump("", 1);
}
".exit" => {
return Ok(true);
}
".help" => {
dump(get_repl_help(), 2);
}
".clear" => {
line_editor.clear_scrollback()?;
}
".clear-history" => {
let history = Box::new(line_editor.history_mut());
history
.clear()
.map_err(|err| anyhow!("Failed to clear history, {err}"))?;
}
".history" => {
line_editor.print_history()?;
dump("", 1);
}
".role" => match args {
Some(name) => match config.roles.iter().find(|v| v.name == name) {
Some(role_) => {
*role = Some(role_.clone());
}
None => dump("Unknown role.", 2),
},
None => dump("Usage: .role <name>.", 2),
},
_ => {
dump("Unknown command. Type \".help\" for more information.", 2);
}
}
} else {
let input = if let Some(role) = role.take() {
role.generate(&line)
} else {
line
};
output.clear();
*trigged_ctrlc = false;
if input.is_empty() {
return Ok(false);
}
runtime.block_on(async {
tokio::select! {
ret = handle_input(&client, &config, &input, output) => {
if let Err(err) = ret {
dump(format!("error: {err}"), 2);
}
}
_ = tokio::signal::ctrl_c() => {
*trigged_ctrlc = true;
dump(" Abort current session.", 2)
}
}
});
}
Ok(false)
};
if let Some(name) = role_name {
handle_line(
format!(".role {name}"),
&mut line_editor,
&mut trigged_ctrlc,
&mut role,
&mut output,
)?;
}
loop {
let sig = line_editor.read_line(&prompt);
match sig {
Ok(Signal::Success(line)) => {
let quit = handle_line(
line,
&mut line_editor,
&mut trigged_ctrlc,
&mut role,
&mut output,
)?;
if quit {
break;
}
}
Ok(Signal::CtrlC) => {
if !trigged_ctrlc {
trigged_ctrlc = true;
dump("(To exit, press Ctrl+C again or Ctrl+D or type .exit)", 2);
} else {
break;
}
}
Ok(Signal::CtrlD) => {
break;
}
Err(err) => {
eprintln!("{err:?}");
break;
}
}
}
Ok(())
}
async fn handle_input(
client: &Client,
config: &Config,
input: &str,
output: &mut String,
) -> Result<()> {
if config.dry_run {
output.push_str(input);
dump(input, 2);
return Ok(());
}
let mut stream = acquire_stream(client, config, input).await?;
let mut virgin = true;
while let Some(part) = stream.next().await {
let chunk = part?.data;
if chunk == "[DONE]" {
output.push('\n');
dump("", 2);
break;
} else {
let data: Value = serde_json::from_str(&chunk)?;
let text = data["choices"][0]["delta"]["content"]
.as_str()
.unwrap_or_default();
if text.is_empty() {
continue;
}
if virgin {
virgin = false;
if text == "\n\n" {
continue;
}
}
output.push_str(text);
dump(text, 0);
}
}
Ok(())
}
fn init_client(config: &Config) -> Result<Client> {
let mut builder = Client::builder();
if let Some(proxy) = config.proxy.as_ref() {
builder =
builder.proxy(Proxy::all(proxy).map_err(|err| anyhow!("Invalid config.proxy, {err}"))?);
}
let client = builder
.connect_timeout(CONNECT_TIMEOUT)
.build()
.map_err(|err| anyhow!("Failed to init http client, {err}"))?;
Ok(client)
}
fn init_runtime() -> Result<Runtime> {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.map_err(|err| anyhow!("Failed to init tokio, {err}"))
}
fn create_config_file(config_path: &Path) -> Result<()> {
let ans = Confirm::new("No config file, create a new one?")
.with_default(true)
.prompt()
.map_err(|_| anyhow!("Error with questionnaire, try again later"))?;
if !ans {
exit(0);
}
let api_key = Text::new("Openai API Key:")
.prompt()
.map_err(|_| anyhow!("An error happened when asking for your key, try again later."))?;
std::fs::write(config_path, format!("api_key = \"{api_key}\"\n"))
.map_err(|err| anyhow!("Failed to write to config file, {err}"))?;
Ok(())
}
async fn acquire(client: &Client, config: &Config, content: &str) -> Result<String> {
if config.dry_run {
return Ok(content.to_string());
}
let mut body = json!({
"model": MODEL,
"messages": [{"role": "user", "content": content}]
});
if let Some(v) = config.temperature {
body.as_object_mut()
.and_then(|m| m.insert("temperature".into(), json!(v)));
}
let data: Value = client
.post(API_URL)
.bearer_auth(&config.api_key)
.json(&body)
.send()
.await?
.json()
.await?;
let output = data["choices"][0]["message"]["content"]
.as_str()
.ok_or_else(|| anyhow!("Unexpected response {data}"))?;
Ok(output.to_string())
}
async fn acquire_stream(
client: &Client,
config: &Config,
content: &str,
) -> Result<EventStream<impl Stream<Item = reqwest::Result<bytes::Bytes>>>> {
let mut body = json!({
"model": MODEL,
"messages": [{"role": "user", "content": content}],
"stream": true,
});
if let Some(v) = config.temperature {
body.as_object_mut()
.and_then(|m| m.insert("temperature".into(), json!(v)));
}
let stream = client
.post(API_URL)
.bearer_auth(&config.api_key)
.json(&body)
.send()
.await?
.bytes_stream()
.eventsource();
Ok(stream)
}
fn dump<T: ToString>(text: T, newlines: usize) {
print!("{}{}", text.to_string(), "\n".repeat(newlines));
stdout().flush().unwrap();
}
fn get_config_path() -> Result<PathBuf> {
let config_dir = dirs::home_dir().ok_or_else(|| anyhow!("No home dir"))?;
Ok(config_dir.join(format!(".{}.toml", env!("CARGO_CRATE_NAME"))))
}
fn get_history_path() -> Result<PathBuf> {
let config_dir = dirs::home_dir().ok_or_else(|| anyhow!("No home dir"))?;
Ok(config_dir.join(format!(".{}_history", env!("CARGO_CRATE_NAME"))))
}
fn print_repl_title() {
println!("Welcome to aichat {}", env!("CARGO_PKG_VERSION"));
println!("Type \".help\" for more information.");
}
fn get_repl_help() -> String {
let head = REPL_COMMANDS
.iter()
.map(|(name, desc)| format!("{name:<15} {desc}"))
.collect::<Vec<String>>()
.join("\n");
format!("{head}\n\nPress Ctrl+C to abort session, Ctrl+D to exit the REPL")
}
|