summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-07-30 07:45:29 +0800
committerGitHub <noreply@github.com>2024-07-30 07:45:29 +0800
commitcc74be5d2163fbb1e9d57f85d5546857c6602446 (patch)
tree9897a88950ad6fc65dbd63f0cb9bbc9e7290d950 /src
parent4c18e91165269970747bd2734b9b5db013ade2c2 (diff)
downloadaichat-cc74be5d2163fbb1e9d57f85d5546857c6602446.tar.gz
refactor: several improvements (#765)
Diffstat (limited to 'src')
-rw-r--r--src/client/access_token.rs3
-rw-r--r--src/client/common.rs3
-rw-r--r--src/config/agent.rs2
-rw-r--r--src/config/input.rs3
-rw-r--r--src/config/role.rs17
-rw-r--r--src/main.rs10
-rw-r--r--src/render/markdown.rs3
-rw-r--r--src/repl/mod.rs8
-rw-r--r--src/utils/command.rs7
-rw-r--r--src/utils/mod.rs3
-rw-r--r--src/utils/request.rs3
11 files changed, 27 insertions, 35 deletions
diff --git a/src/client/access_token.rs b/src/client/access_token.rs
index 07640d5..0f69d9a 100644
--- a/src/client/access_token.rs
+++ b/src/client/access_token.rs
@@ -1,10 +1,9 @@
use anyhow::{anyhow, Result};
use chrono::Utc;
use indexmap::IndexMap;
-use lazy_static::lazy_static;
use parking_lot::RwLock;
-lazy_static! {
+lazy_static::lazy_static! {
static ref ACCESS_TOKENS: RwLock<IndexMap<String, (String, i64)>> =
RwLock::new(IndexMap::new());
}
diff --git a/src/client/common.rs b/src/client/common.rs
index ec1f37d..684d10e 100644
--- a/src/client/common.rs
+++ b/src/client/common.rs
@@ -10,7 +10,6 @@ use crate::{
use anyhow::{bail, Context, Result};
use fancy_regex::Regex;
use indexmap::IndexMap;
-use lazy_static::lazy_static;
use reqwest::{Client as ReqwestClient, RequestBuilder};
use serde::Deserialize;
use serde_json::{json, Value};
@@ -19,7 +18,7 @@ use tokio::sync::mpsc::unbounded_channel;
const MODELS_YAML: &str = include_str!("../../models.yaml");
-lazy_static! {
+lazy_static::lazy_static! {
pub static ref ALL_MODELS: Vec<BuiltinModels> = serde_yaml::from_str(MODELS_YAML).unwrap();
static ref ESCAPE_SLASH_RE: Regex = Regex::new(r"(?<!\\)/").unwrap();
}
diff --git a/src/config/agent.rs b/src/config/agent.rs
index 5f78e4b..0025095 100644
--- a/src/config/agent.rs
+++ b/src/config/agent.rs
@@ -409,7 +409,7 @@ fn builtin_variables() -> Vec<(&'static str, String)> {
("__os__", env::consts::OS.to_string()),
("__os_family__", env::consts::FAMILY.to_string()),
("__arch__", env::consts::ARCH.to_string()),
- ("__shell__", detect_shell().name),
+ ("__shell__", SHELL.name.clone()),
("__locale__", sys_locale::get_locale().unwrap_or_default()),
(
"__now__",
diff --git a/src/config/input.rs b/src/config/input.rs
index 6df3b71..fe1da51 100644
--- a/src/config/input.rs
+++ b/src/config/input.rs
@@ -9,13 +9,12 @@ use crate::utils::{base64_encode, sha256, AbortSignal};
use anyhow::{bail, Context, Result};
use fancy_regex::Regex;
-use lazy_static::lazy_static;
use std::{collections::HashMap, fs::File, io::Read, path::Path};
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
const IMAGE_EXTS: [&str; 5] = ["png", "jpeg", "jpg", "webp", "gif"];
-lazy_static! {
+lazy_static::lazy_static! {
static ref URL_RE: Regex = Regex::new(r"^[A-Za-z0-9_-]{2,}:/").unwrap();
}
diff --git a/src/config/role.rs b/src/config/role.rs
index 3fc7696..31e894b 100644
--- a/src/config/role.rs
+++ b/src/config/role.rs
@@ -1,9 +1,6 @@
use super::*;
-use crate::{
- client::{Message, MessageContent, MessageRole, Model},
- utils::{detect_os, detect_shell},
-};
+use crate::client::{Message, MessageContent, MessageRole, Model};
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
@@ -315,17 +312,17 @@ fn parse_structure_prompt(prompt: &str) -> (&str, Vec<(&str, &str)>) {
}
fn shell_prompt() -> String {
- let os = detect_os();
- let shell = detect_shell();
- let shell = shell.name.as_str();
+ let os = OS.as_str();
+ let shell = SHELL.name.as_str();
let combinator = if shell == "powershell" {
- "\nIf multiple steps required try to combine them together using ';'.\nIf it already combined with '&&' try to replace it with ';'.".to_string()
+ "If multiple steps required try to combine them together using ';'.\nIf it already combined with '&&' try to replace it with ';'.".to_string()
} else {
- "\nIf multiple steps required try to combine them together using '&&'.".to_string()
+ "If multiple steps required try to combine them together using '&&'.".to_string()
};
format!(
r#"Provide only {shell} commands for {os} without any description.
-Ensure the output is a valid {shell} command. {combinator}
+Ensure the output is a valid {shell} command.
+{combinator}
If there is a lack of details, provide most logical solution.
Output plain text only, without any markdown formatting."#
)
diff --git a/src/main.rs b/src/main.rs
index 04915ed..eefdc41 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -24,12 +24,11 @@ use crate::function::{eval_tool_calls, need_send_tool_results};
use crate::render::render_error;
use crate::repl::Repl;
use crate::utils::{
- create_abort_signal, create_spinner, detect_shell, extract_block, get_env_name, run_command,
- run_with_spinner, AbortSignal, Shell, CODE_BLOCK_RE, IS_STDOUT_TERMINAL,
+ create_abort_signal, create_spinner, extract_block, get_env_name, run_command,
+ run_with_spinner, AbortSignal, Shell, CODE_BLOCK_RE, IS_STDOUT_TERMINAL, SHELL,
};
use anyhow::{bail, Result};
-use async_recursion::async_recursion;
use clap::Parser;
use inquire::{Select, Text};
use is_terminal::IsTerminal;
@@ -161,8 +160,7 @@ async fn run(
let is_repl = config.read().working_mode.is_repl();
if cli.execute && !is_repl {
let input = create_input(&config, text, &cli.file).await?;
- let shell = detect_shell();
- shell_execute(&config, &shell, input).await?;
+ shell_execute(&config, &SHELL, input).await?;
return Ok(());
}
config.write().apply_prelude()?;
@@ -176,7 +174,7 @@ async fn run(
}
}
-#[async_recursion]
+#[async_recursion::async_recursion]
async fn start_directive(
config: &GlobalConfig,
input: Input,
diff --git a/src/render/markdown.rs b/src/render/markdown.rs
index 400cfe2..b8710b7 100644
--- a/src/render/markdown.rs
+++ b/src/render/markdown.rs
@@ -2,7 +2,6 @@ use ansi_colours::AsRGB;
use anyhow::{anyhow, Context, Result};
use crossterm::style::{Color, Stylize};
use crossterm::terminal;
-use lazy_static::lazy_static;
use std::collections::HashMap;
use syntect::highlighting::{Color as SyntectColor, FontStyle, Style, Theme};
use syntect::parsing::SyntaxSet;
@@ -11,7 +10,7 @@ use syntect::{easy::HighlightLines, parsing::SyntaxReference};
/// Comes from https://github.com/sharkdp/bat/raw/5e77ca37e89c873e4490b42ff556370dc5c6ba4f/assets/syntaxes.bin
const SYNTAXES: &[u8] = include_bytes!("../../assets/syntaxes.bin");
-lazy_static! {
+lazy_static::lazy_static! {
static ref LANG_MAPS: HashMap<String, String> = {
let mut m = HashMap::new();
m.insert("csharp".into(), "C#".into());
diff --git a/src/repl/mod.rs b/src/repl/mod.rs
index 0711a6d..18d00f9 100644
--- a/src/repl/mod.rs
+++ b/src/repl/mod.rs
@@ -13,9 +13,7 @@ use crate::render::render_error;
use crate::utils::{create_abort_signal, set_text, temp_file, AbortSignal};
use anyhow::{bail, Context, Result};
-use async_recursion::async_recursion;
use fancy_regex::Regex;
-use lazy_static::lazy_static;
use nu_ansi_term::Color;
use reedline::{
default_emacs_keybindings, default_vi_insert_keybindings, default_vi_normal_keybindings,
@@ -25,14 +23,14 @@ use reedline::{
use reedline::{MenuBuilder, Signal};
use std::{env, process};
-lazy_static! {
+lazy_static::lazy_static! {
static ref SPLIT_FILES_TEXT_ARGS_RE: Regex =
Regex::new(r"(?m) (-- |--\n|--\r\n|--\r|--$)").unwrap();
}
const MENU_NAME: &str = "completion_menu";
-lazy_static! {
+lazy_static::lazy_static! {
static ref REPL_COMMANDS: [ReplCommand; 28] = [
ReplCommand::new(".help", "Show this help message", AssertState::pass()),
ReplCommand::new(".info", "View system info", AssertState::pass()),
@@ -549,7 +547,7 @@ impl Validator for ReplValidator {
}
}
-#[async_recursion]
+#[async_recursion::async_recursion]
async fn ask(
config: &GlobalConfig,
abort_signal: AbortSignal,
diff --git a/src/utils/command.rs b/src/utils/command.rs
index 3876f3d..caf70c7 100644
--- a/src/utils/command.rs
+++ b/src/utils/command.rs
@@ -4,13 +4,18 @@ use std::{collections::HashMap, env, ffi::OsStr, path::Path, process::Command};
use anyhow::{anyhow, bail, Context, Result};
+lazy_static::lazy_static! {
+ pub static ref OS: String = detect_os();
+ pub static ref SHELL: Shell = detect_shell();
+}
+
pub fn detect_os() -> String {
let os = env::consts::OS;
if os == "linux" {
if let Ok(contents) = std::fs::read_to_string("/etc/os-release") {
for line in contents.lines() {
if let Some(id) = line.strip_prefix("ID=") {
- return format!("{os}/{id}");
+ return format!("{os} ({id})");
}
}
}
diff --git a/src/utils/mod.rs b/src/utils/mod.rs
index 0c1a0c4..a3aef22 100644
--- a/src/utils/mod.rs
+++ b/src/utils/mod.rs
@@ -21,11 +21,10 @@ pub use self::spinner::*;
use anyhow::{Context, Result};
use fancy_regex::Regex;
use is_terminal::IsTerminal;
-use lazy_static::lazy_static;
use std::{env, path::PathBuf, process};
use unicode_segmentation::UnicodeSegmentation;
-lazy_static! {
+lazy_static::lazy_static! {
pub static ref CODE_BLOCK_RE: Regex = Regex::new(r"(?ms)```\w*(.*)```").unwrap();
pub static ref IS_STDOUT_TERMINAL: bool = std::io::stdout().is_terminal();
}
diff --git a/src/utils/request.rs b/src/utils/request.rs
index 585018d..4a4e7c9 100644
--- a/src/utils/request.rs
+++ b/src/utils/request.rs
@@ -2,7 +2,6 @@ use super::*;
use anyhow::{bail, Result};
use http::header::CONTENT_TYPE;
-use lazy_static::lazy_static;
use std::{collections::HashMap, time::Duration};
use tokio::io::AsyncWriteExt;
@@ -11,7 +10,7 @@ pub const RECURSIVE_URL_LOADER: &str = "recursive_url";
pub const MEDIA_URL_EXTENSION: &str = "media_url";
pub const DEFAULT_EXTENSION: &str = "txt";
-lazy_static! {
+lazy_static::lazy_static! {
static ref CLIENT: Result<reqwest::Client> = {
let builder = reqwest::ClientBuilder::new().timeout(Duration::from_secs(30));
let builder = set_proxy(builder, None)?;