summaryrefslogtreecommitdiffstats
path: root/src/config/input.rs
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-06-28 06:24:20 +0800
committerGitHub <noreply@github.com>2024-06-28 06:24:20 +0800
commit4fbbbd2d991b37ac04b77151ef862de9649bbfec (patch)
tree7e4343fb19b8d105b39ad6137d23944d8bffce60 /src/config/input.rs
parent10bd71297db11c163f95625080d956469a1d8689 (diff)
downloadaichat-4fbbbd2d991b37ac04b77151ef862de9649bbfec.tar.gz
feat: `.file`/`--file` support URLs (#665)
Diffstat (limited to 'src/config/input.rs')
-rw-r--r--src/config/input.rs23
1 files changed, 18 insertions, 5 deletions
diff --git a/src/config/input.rs b/src/config/input.rs
index 3b15d1f..bfc3ea9 100644
--- a/src/config/input.rs
+++ b/src/config/input.rs
@@ -59,20 +59,25 @@ impl Input {
}
}
- pub fn new(
+ pub async fn from_files(
config: &GlobalConfig,
text: &str,
files: Vec<String>,
role: Option<Role>,
) -> Result<Self> {
- let mut texts = vec![text.to_string()];
+ let mut texts = vec![];
+ if !text.is_empty() {
+ texts.push(text.to_string());
+ };
let mut medias = vec![];
let mut data_urls = HashMap::new();
let files: Vec<_> = files
.iter()
.map(|f| (f, is_image_ext(Path::new(f))))
.collect();
- let include_filepath = files.iter().filter(|(_, is_image)| !*is_image).count() > 1;
+ let multi_files = files.iter().filter(|(_, is_image)| !*is_image).count() > 1;
+ let loaders = config.read().document_loaders.clone();
+ let spinner = create_spinner("Loading files").await;
for (file_item, is_image) in files {
match resolve_local_file(file_item) {
Some(file_path) => {
@@ -84,7 +89,7 @@ impl Input {
} else {
let text = read_file(&file_path)
.with_context(|| format!("Unable to read file '{file_item}'"))?;
- if include_filepath {
+ if multi_files {
texts.push(format!("`{file_item}`:\n~~~~~~\n{text}\n~~~~~~"));
} else {
texts.push(text);
@@ -95,11 +100,19 @@ impl Input {
if is_image {
medias.push(file_item.to_string())
} else {
- bail!("Unable to use remote file '{file_item}");
+ let (text, _) = fetch(&loaders, file_item)
+ .await
+ .with_context(|| format!("Failed to load '{file_item}'"))?;
+ if multi_files {
+ texts.push(format!("`{file_item}`:\n~~~~~~\n{text}\n~~~~~~"));
+ } else {
+ texts.push(text);
+ }
}
}
}
}
+ spinner.stop();
let (role, with_session, with_agent) = resolve_role(&config.read(), role);
Ok(Self {