ui improvements and feature flags for AI integration
Continuous integration / build (push) Failing after 4m9s
Continuous integration / build (push) Failing after 4m9s
This commit is contained in:
@@ -1,34 +1,42 @@
|
||||
use egui::TextEdit;
|
||||
use egui_commonmark::{CommonMarkCache, CommonMarkViewer};
|
||||
use serde::{self, Deserialize, Serialize};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crate::{
|
||||
PROJECT_FOLDER,
|
||||
editors::{settings_editor::ProjectSettings, tags::Tag},
|
||||
llm_integration::content_llm::{ContentAI, ReadyState, ReasoningEffort},
|
||||
util,
|
||||
};
|
||||
|
||||
#[cfg(feature = "llm")]
|
||||
use crate::llm_integration::content_llm::{ContentAI, ReadyState};
|
||||
|
||||
pub struct MainEditor {
|
||||
pub content: ContentSection,
|
||||
pub show_editor: bool,
|
||||
pub editor_separate_window: bool,
|
||||
pub show_preview: bool,
|
||||
preview_cache: CommonMarkCache,
|
||||
dialog: Option<ContentAI>,
|
||||
|
||||
#[cfg(feature = "llm")]
|
||||
dialog: ContentAI,
|
||||
#[cfg(feature = "llm")]
|
||||
pub show_ai: bool,
|
||||
}
|
||||
|
||||
impl Clone for MainEditor {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
content: self.content.clone(),
|
||||
|
||||
show_editor: self.show_editor,
|
||||
editor_separate_window: self.editor_separate_window,
|
||||
show_preview: self.show_preview,
|
||||
preview_cache: CommonMarkCache::default(),
|
||||
|
||||
#[cfg(feature = "llm")]
|
||||
dialog: self.dialog.clone(),
|
||||
#[cfg(feature = "llm")]
|
||||
show_ai: self.show_ai,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -108,7 +116,11 @@ impl MainEditor {
|
||||
show_preview: false,
|
||||
editor_separate_window: false,
|
||||
preview_cache: CommonMarkCache::default(),
|
||||
dialog: None,
|
||||
|
||||
#[cfg(feature = "llm")]
|
||||
show_ai: false,
|
||||
#[cfg(feature = "llm")]
|
||||
dialog: ContentAI::new(String::new()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,7 +131,11 @@ impl MainEditor {
|
||||
show_preview: false,
|
||||
editor_separate_window: false,
|
||||
preview_cache: CommonMarkCache::default(),
|
||||
dialog: None,
|
||||
|
||||
#[cfg(feature = "llm")]
|
||||
show_ai: false,
|
||||
#[cfg(feature = "llm")]
|
||||
dialog: ContentAI::new(String::new()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,6 +193,10 @@ impl MainEditor {
|
||||
// preview toggle
|
||||
ui.checkbox(&mut self.show_preview, "Preview");
|
||||
|
||||
// assistant toggle
|
||||
#[cfg(feature = "llm")]
|
||||
ui.checkbox(&mut self.show_ai, "AI Assistant");
|
||||
|
||||
// editor toggle
|
||||
ui.checkbox(&mut self.editor_separate_window, "Pop out editor");
|
||||
});
|
||||
@@ -222,10 +242,13 @@ impl MainEditor {
|
||||
|
||||
ui.separator();
|
||||
|
||||
if let Some(dialog) = &mut self.dialog {
|
||||
dialog.ui(ui, project);
|
||||
#[cfg(feature = "llm")]
|
||||
if self.show_ai {
|
||||
let dialog = &mut self.dialog;
|
||||
|
||||
dialog.content = self.content.content.clone();
|
||||
dialog.ui(ui, project);
|
||||
|
||||
if *dialog.ready.lock().unwrap() == ReadyState::Ready {
|
||||
self.content
|
||||
.content
|
||||
@@ -319,50 +342,16 @@ impl MainEditor {
|
||||
|
||||
ui.set_min_width(max_width as f32);
|
||||
|
||||
let text_edit = TextEdit::multiline(&mut self.content.content)
|
||||
.id_source("MainEditor_editor")
|
||||
.font(egui::TextStyle::Monospace)
|
||||
.interactive(true)
|
||||
.frame(false)
|
||||
.lock_focus(true)
|
||||
.hint_text("Type here...")
|
||||
.desired_width(max_width as f32);
|
||||
|
||||
let mut ctx_menu = false;
|
||||
let response = ui
|
||||
.add_sized(
|
||||
egui::vec2(max_width as f32 - 30.0, ui.available_height()),
|
||||
text_edit,
|
||||
)
|
||||
.on_hover_text("Right click to open context menu")
|
||||
.context_menu(|ui| {
|
||||
ctx_menu = true;
|
||||
|
||||
ui.menu_button("AI Actions", |ui| {
|
||||
ui.add_enabled_ui(project.ai_enabled(), |ui| {
|
||||
if ui.button("AI Assistant").clicked() {
|
||||
self.dialog = Some(ContentAI {
|
||||
content: self.content.content.clone(),
|
||||
instruction: String::new(),
|
||||
max_tokens: 1024,
|
||||
reasoning_effort: ReasoningEffort::default(),
|
||||
context_override: "".to_string(),
|
||||
result: Arc::new(Mutex::new(String::new())),
|
||||
open: true,
|
||||
ready: Arc::new(Mutex::new(ReadyState::Idle)),
|
||||
temperature: 0.7,
|
||||
model_override: "".to_string(),
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
if let Some(response) = response {
|
||||
if response.response.changed() || ctx_menu {
|
||||
self.content.saved = false;
|
||||
}
|
||||
}
|
||||
ui.add(
|
||||
TextEdit::multiline(&mut self.content.content)
|
||||
.id_source("MainEditor_editor")
|
||||
.font(egui::TextStyle::Monospace)
|
||||
.interactive(true)
|
||||
.frame(false)
|
||||
.lock_focus(true)
|
||||
.hint_text("Type here...")
|
||||
.desired_width(max_width as f32),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+17
-19
@@ -1,4 +1,7 @@
|
||||
use walkdir::{DirEntry, WalkDir};
|
||||
use itertools::Itertools;
|
||||
use std::fs::{self, DirEntry};
|
||||
|
||||
// use walkdir::{DirEntry, WalkDir};
|
||||
|
||||
use crate::{
|
||||
PROJECT_FOLDER, RightPanelContent,
|
||||
@@ -249,24 +252,22 @@ impl Explorer {
|
||||
});
|
||||
})
|
||||
.body(|ui| {
|
||||
let entries: Vec<_> = WalkDir::new(PROJECT_FOLDER.join("assets"))
|
||||
.min_depth(1)
|
||||
.max_depth(1) // Only immediate children
|
||||
.sort_by(|a, b| {
|
||||
let entries = fs::read_dir(PROJECT_FOLDER.join("assets"))
|
||||
.unwrap()
|
||||
.filter_map(Result::ok)
|
||||
.sorted_by(|a, b| {
|
||||
// Directories first, then files
|
||||
let a_is_dir = a.file_type().is_dir();
|
||||
let b_is_dir = b.file_type().is_dir();
|
||||
let a_is_dir = a.file_type().unwrap().is_dir();
|
||||
let b_is_dir = b.file_type().unwrap().is_dir();
|
||||
if a_is_dir == b_is_dir {
|
||||
a.file_name().cmp(b.file_name())
|
||||
a.file_name().cmp(&b.file_name())
|
||||
} else if a_is_dir {
|
||||
std::cmp::Ordering::Less
|
||||
} else {
|
||||
std::cmp::Ordering::Greater
|
||||
}
|
||||
})
|
||||
.into_iter()
|
||||
.filter_map(Result::ok)
|
||||
.collect();
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
for entry in entries {
|
||||
Self::render_entry(ui, to_load, &entry);
|
||||
@@ -275,19 +276,16 @@ impl Explorer {
|
||||
}
|
||||
|
||||
fn render_entry(ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>, entry: &DirEntry) {
|
||||
let file_type = entry.file_type();
|
||||
let file_type = entry.file_type().unwrap();
|
||||
let is_dir = file_type.is_dir();
|
||||
let file_name = entry.file_name().to_string_lossy();
|
||||
let file_name = entry.file_name().to_str().unwrap().to_string();
|
||||
let path = entry.path();
|
||||
|
||||
if is_dir {
|
||||
let entries: Vec<_> = WalkDir::new(path)
|
||||
.min_depth(1)
|
||||
.max_depth(1)
|
||||
.sort_by(|a, b| a.file_name().cmp(b.file_name()))
|
||||
.into_iter()
|
||||
let entries = fs::read_dir(path)
|
||||
.unwrap()
|
||||
.filter_map(Result::ok)
|
||||
.collect();
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
egui::collapsing_header::CollapsingState::load_with_default_open(
|
||||
ui.ctx(),
|
||||
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
use std::io::{self, Read, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// Platform-agnostic file system operations
|
||||
trait FileSystem {
|
||||
fn read_file(&self, path: &Path) -> io::Result<Vec<u8>>;
|
||||
fn write_file(&self, path: &Path, contents: &[u8]) -> io::Result<()>;
|
||||
fn create_dir_all(&self, path: &Path) -> io::Result<()>;
|
||||
fn read_dir(&self, path: &Path) -> io::Result<Vec<PathBuf>>;
|
||||
fn exists(&self, path: &Path) -> bool;
|
||||
}
|
||||
|
||||
/// Native filesystem implementation
|
||||
#[cfg(feature = "native")]
|
||||
struct NativeFileSystem;
|
||||
|
||||
#[cfg(feature = "native")]
|
||||
impl FileSystem for NativeFileSystem {
|
||||
fn read_file(&self, path: &Path) -> io::Result<Vec<u8>> {
|
||||
std::fs::read(path)
|
||||
}
|
||||
|
||||
fn write_file(&self, path: &Path, contents: &[u8]) -> io::Result<()> {
|
||||
if let Some(parent) = path.parent() {
|
||||
std::fs::create_dir_all(parent)?;
|
||||
}
|
||||
std::fs::write(path, contents)
|
||||
}
|
||||
|
||||
fn create_dir_all(&self, path: &Path) -> io::Result<()> {
|
||||
std::fs::create_dir_all(path)
|
||||
}
|
||||
|
||||
fn read_dir(&self, path: &Path) -> io::Result<Vec<PathBuf>> {
|
||||
Ok(std::fs::read_dir(path)?
|
||||
.filter_map(Result::ok)
|
||||
.map(|entry| entry.path())
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn exists(&self, path: &Path) -> bool {
|
||||
path.exists()
|
||||
}
|
||||
}
|
||||
|
||||
/// Web filesystem implementation
|
||||
#[cfg(feature = "web")]
|
||||
struct WebFileSystem;
|
||||
|
||||
#[cfg(feature = "web")]
|
||||
impl WebFileSystem {
|
||||
fn new() -> Self {
|
||||
// Initialize web-specific storage if needed
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "web")]
|
||||
impl FileSystem for WebFileSystem {
|
||||
fn read_file(&self, path: &Path) -> io::Result<Vec<u8>> {
|
||||
// In a real implementation, this would use web_sys and IndexedDB
|
||||
// This is a simplified version that won't actually work
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"Web filesystem not implemented",
|
||||
))
|
||||
}
|
||||
|
||||
fn write_file(&self, path: &Path, contents: &[u8]) -> io::Result<()> {
|
||||
// In a real implementation, this would use web_sys and IndexedDB
|
||||
// This is a simplified version that won't actually work
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"Web filesystem not implemented",
|
||||
))
|
||||
}
|
||||
|
||||
fn create_dir_all(&self, _path: &Path) -> io::Result<()> {
|
||||
// In web, directories are virtual and created automatically
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_dir(&self, _path: &Path) -> io::Result<Vec<PathBuf>> {
|
||||
// In a real implementation, this would list files from IndexedDB
|
||||
// This is a simplified version that returns an empty list
|
||||
Ok(Vec::new())
|
||||
}
|
||||
|
||||
fn exists(&self, _path: &Path) -> bool {
|
||||
// In a real implementation, this would check IndexedDB
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "web")]
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs::File;
|
||||
use tempfile::tempdir;
|
||||
|
||||
#[test]
|
||||
fn test_native_fs() {
|
||||
let temp_dir = tempdir().unwrap();
|
||||
let file_path = temp_dir.path().join("test.txt");
|
||||
|
||||
let fs = NativeFileSystem;
|
||||
|
||||
// Test write and read
|
||||
let test_data = b"Hello, world!";
|
||||
fs.write_file(&file_path, test_data).unwrap();
|
||||
let read_data = fs.read_file(&file_path).unwrap();
|
||||
assert_eq!(read_data, test_data);
|
||||
|
||||
// Test exists
|
||||
assert!(fs.exists(&file_path));
|
||||
assert!(!fs.exists(&temp_dir.path().join("nonexistent")));
|
||||
|
||||
// Test create_dir_all and read_dir
|
||||
let dir_path = temp_dir.path().join("subdir");
|
||||
fs.create_dir_all(&dir_path).unwrap();
|
||||
let entries = fs.read_dir(temp_dir.path()).unwrap();
|
||||
assert_eq!(entries.len(), 2); // Should contain both the file and the subdirectory
|
||||
}
|
||||
}
|
||||
+297
-198
@@ -10,18 +10,50 @@ use crate::editors::settings_editor::ProjectSettings;
|
||||
#[derive(Clone)]
|
||||
pub struct ContentAI {
|
||||
pub open: bool,
|
||||
|
||||
// model input
|
||||
pub content: String,
|
||||
pub instruction: String,
|
||||
pub max_tokens: usize,
|
||||
pub context_override: String,
|
||||
pub result: Arc<Mutex<String>>,
|
||||
pub ready: Arc<Mutex<ReadyState>>,
|
||||
pub system_prompt: String,
|
||||
|
||||
// model settings
|
||||
pub max_tokens: usize,
|
||||
pub temperature: f32,
|
||||
pub reasoning_effort: ReasoningEffort,
|
||||
pub model_override: String,
|
||||
|
||||
// model output
|
||||
pub reasoning: Arc<Mutex<String>>,
|
||||
pub result: Arc<Mutex<String>>,
|
||||
pub ready: Arc<Mutex<ReadyState>>,
|
||||
}
|
||||
|
||||
impl ContentAI {
|
||||
pub fn new(content: String) -> Self {
|
||||
Self {
|
||||
// model input
|
||||
content,
|
||||
instruction: String::new(),
|
||||
context_override: String::new(),
|
||||
system_prompt: String::new(),
|
||||
|
||||
// model settings
|
||||
max_tokens: 2048,
|
||||
reasoning_effort: ReasoningEffort::default(),
|
||||
temperature: 0.7,
|
||||
model_override: String::new(),
|
||||
reasoning: Arc::new(Mutex::new(String::new())),
|
||||
|
||||
// output
|
||||
result: Arc::new(Mutex::new(String::new())),
|
||||
ready: Arc::new(Mutex::new(ReadyState::Idle)),
|
||||
|
||||
// ui
|
||||
open: true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ui(&mut self, ui: &mut egui::Ui, project: &mut ProjectSettings) {
|
||||
let is_open = self.open;
|
||||
|
||||
@@ -34,203 +66,266 @@ impl ContentAI {
|
||||
}
|
||||
|
||||
fn ui_output_box(&mut self, ui: &mut egui::Ui, project: &mut ProjectSettings) {
|
||||
egui::TopBottomPanel::bottom("llm_output")
|
||||
.resizable(true)
|
||||
.show_inside(ui, |ui| {
|
||||
let mut ready_lock = self.ready.lock().unwrap();
|
||||
let mut ready_lock = self.ready.lock().unwrap();
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
if *ready_lock == ReadyState::Generating {
|
||||
if ui.button("Cancel").clicked() {
|
||||
*ready_lock = ReadyState::Halted;
|
||||
ui.horizontal(|ui| {
|
||||
if *ready_lock == ReadyState::Generating {
|
||||
if ui.button("Cancel").clicked() {
|
||||
*ready_lock = ReadyState::Halted;
|
||||
}
|
||||
if ui.button("Stop").clicked() {
|
||||
*ready_lock = ReadyState::Idle;
|
||||
}
|
||||
ui.spinner();
|
||||
ui.label("Generating...");
|
||||
}
|
||||
|
||||
if *ready_lock == ReadyState::Idle {
|
||||
let continue_content = || {
|
||||
let content = self.content.clone();
|
||||
let project = project.clone();
|
||||
let result = self.result.clone();
|
||||
let reasoning = self.reasoning.clone();
|
||||
let ready = self.ready.clone();
|
||||
|
||||
let options = AIOptions {
|
||||
max_completion_tokens: self.max_tokens,
|
||||
reasoning_effort: self.reasoning_effort,
|
||||
temperature: self.temperature,
|
||||
model_override: if !self.model_override.is_empty() {
|
||||
Some(self.model_override.clone())
|
||||
} else {
|
||||
None
|
||||
},
|
||||
};
|
||||
|
||||
let ai_input = AIInput {
|
||||
system_prompt: self.system_prompt.clone(),
|
||||
user_prompt: format!(
|
||||
"{}\n\n{} {}",
|
||||
self.instruction.clone(),
|
||||
project.ai_context.clone(),
|
||||
self.context_override.clone()
|
||||
),
|
||||
previous_content: content.clone(),
|
||||
structure: None,
|
||||
};
|
||||
|
||||
result.lock().unwrap().clear();
|
||||
|
||||
std::thread::spawn(move || {
|
||||
let result = crate::llm_integration::content_llm::continue_content(
|
||||
ai_input,
|
||||
options,
|
||||
project,
|
||||
result,
|
||||
reasoning,
|
||||
ready.clone(),
|
||||
);
|
||||
if let Err(e) = result {
|
||||
eprintln!("Error in content generation: {e}");
|
||||
}
|
||||
if ui.button("Stop").clicked() {
|
||||
*ready_lock = ReadyState::Idle;
|
||||
}
|
||||
ui.spinner();
|
||||
ui.label("Generating...");
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
if *ready_lock == ReadyState::Idle {
|
||||
let continue_content = || {
|
||||
let context_override = self.context_override.clone();
|
||||
let content = self.content.clone();
|
||||
let instruction = self.instruction.clone();
|
||||
let project = project.clone();
|
||||
let ai_context = project.ai_context.clone();
|
||||
let result = self.result.clone();
|
||||
let ready = self.ready.clone();
|
||||
if ui.button("Generate ").clicked() {
|
||||
continue_content();
|
||||
}
|
||||
|
||||
let options = AIOptions {
|
||||
max_completion_tokens: self.max_tokens,
|
||||
reasoning_effort: self.reasoning_effort,
|
||||
temperature: self.temperature,
|
||||
model_override: if !self.model_override.is_empty() {
|
||||
Some(self.model_override.clone())
|
||||
} else {
|
||||
None
|
||||
},
|
||||
};
|
||||
ui.label("Idle");
|
||||
}
|
||||
|
||||
result.lock().unwrap().clear();
|
||||
// show regardless of state
|
||||
if ui.button("Insert").clicked() {
|
||||
*ready_lock = ReadyState::Ready;
|
||||
}
|
||||
|
||||
std::thread::spawn(move || {
|
||||
let result = crate::llm_integration::content_llm::continue_content(
|
||||
ai_context + "\n" + &context_override,
|
||||
content,
|
||||
instruction,
|
||||
options,
|
||||
project,
|
||||
result,
|
||||
ready.clone(),
|
||||
);
|
||||
if let Err(e) = result {
|
||||
eprintln!("Error in content generation: {e}");
|
||||
}
|
||||
});
|
||||
};
|
||||
if ui.button("Clear").clicked() {
|
||||
self.result.lock().unwrap().clear();
|
||||
self.reasoning.lock().unwrap().clear();
|
||||
}
|
||||
});
|
||||
|
||||
if ui.button("Generate ").clicked() {
|
||||
continue_content();
|
||||
}
|
||||
ui.spacing();
|
||||
|
||||
ui.label("Idle");
|
||||
}
|
||||
|
||||
// show regardless of state
|
||||
if ui.button("Insert").clicked() {
|
||||
*self.ready.lock().unwrap() = ReadyState::Ready;
|
||||
}
|
||||
|
||||
if ui.button("Clear").clicked() {
|
||||
self.result.lock().unwrap().clear();
|
||||
}
|
||||
ui.vertical(|ui| {
|
||||
egui::TopBottomPanel::top("reasoning_output")
|
||||
.resizable(true)
|
||||
.show_inside(ui, |ui| {
|
||||
egui::ScrollArea::both()
|
||||
.auto_shrink([false, true])
|
||||
.id_salt("reasoning_output")
|
||||
.max_width(ui.available_width())
|
||||
// .max_height(ui.available_height() / 3.0)
|
||||
.show(ui, |ui| {
|
||||
ui.add(
|
||||
egui::TextEdit::multiline(&mut *self.reasoning.lock().unwrap())
|
||||
.font(egui::TextStyle::Monospace)
|
||||
.interactive(false)
|
||||
.desired_rows(5)
|
||||
.frame(false)
|
||||
.desired_width(ui.available_width())
|
||||
.lock_focus(true)
|
||||
.hint_text("Reasoning will appear here..."),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
ui.separator();
|
||||
|
||||
egui::ScrollArea::both()
|
||||
.auto_shrink([false, false])
|
||||
.id_salt("llm_output")
|
||||
.max_width(ui.available_width())
|
||||
// .max_height(ui.available_height() / 3.0)
|
||||
.show(ui, |ui| {
|
||||
ui.add(
|
||||
egui::TextEdit::multiline(&mut *self.result.lock().unwrap())
|
||||
.font(egui::TextStyle::Monospace)
|
||||
.interactive(false)
|
||||
.desired_rows(0)
|
||||
.frame(false)
|
||||
.desired_width(ui.available_width())
|
||||
.lock_focus(true)
|
||||
.hint_text("Content will appear here..."),
|
||||
);
|
||||
});
|
||||
});
|
||||
egui::ScrollArea::both()
|
||||
.auto_shrink([false, false])
|
||||
.id_salt("llm_output")
|
||||
.max_width(ui.available_width())
|
||||
.show(ui, |ui| {
|
||||
ui.add(
|
||||
egui::TextEdit::multiline(&mut *self.result.lock().unwrap())
|
||||
.font(egui::TextStyle::Monospace)
|
||||
.interactive(false)
|
||||
.desired_rows(0)
|
||||
.frame(false)
|
||||
.desired_width(ui.available_width())
|
||||
.lock_focus(true)
|
||||
.hint_text("Content will appear here..."),
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn ui_main(&mut self, ui: &mut egui::Ui, project: &mut ProjectSettings) {
|
||||
{
|
||||
ui.weak("(The model will see current file content)");
|
||||
|
||||
egui::Grid::new("continue_grid")
|
||||
.num_columns(2)
|
||||
.striped(true)
|
||||
egui::CollapsingHeader::new("Settings")
|
||||
.default_open(true)
|
||||
.show(ui, |ui| {
|
||||
ui.label("Max Tokens");
|
||||
ui.add(
|
||||
egui::DragValue::new(&mut self.max_tokens)
|
||||
.range(128..=u32::MAX)
|
||||
.speed(128),
|
||||
);
|
||||
ui.end_row();
|
||||
egui::Grid::new("continue_grid")
|
||||
.num_columns(2)
|
||||
.striped(true)
|
||||
.show(ui, |ui| {
|
||||
ui.label("Max Tokens");
|
||||
ui.add(
|
||||
egui::DragValue::new(&mut self.max_tokens)
|
||||
.range(128..=u32::MAX)
|
||||
.speed(128),
|
||||
);
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Temperature");
|
||||
ui.add(
|
||||
egui::DragValue::new(&mut self.temperature)
|
||||
.range(0.0..=2.0)
|
||||
.speed(0.1),
|
||||
);
|
||||
ui.label("Temperature");
|
||||
ui.add(
|
||||
egui::DragValue::new(&mut self.temperature)
|
||||
.range(0.0..=2.0)
|
||||
.speed(0.1),
|
||||
);
|
||||
|
||||
ui.label("Reasoning effort");
|
||||
ui.label("Reasoning effort");
|
||||
|
||||
egui::ComboBox::from_id_salt("reasoning_effort")
|
||||
.selected_text(self.reasoning_effort.to_string())
|
||||
.show_ui(ui, |ui| {
|
||||
ui.selectable_value(
|
||||
&mut self.reasoning_effort,
|
||||
ReasoningEffort::Minimal,
|
||||
"Minimal",
|
||||
);
|
||||
ui.selectable_value(
|
||||
&mut self.reasoning_effort,
|
||||
ReasoningEffort::Low,
|
||||
"Low",
|
||||
);
|
||||
ui.selectable_value(
|
||||
&mut self.reasoning_effort,
|
||||
ReasoningEffort::Medium,
|
||||
"Medium",
|
||||
);
|
||||
ui.selectable_value(
|
||||
&mut self.reasoning_effort,
|
||||
ReasoningEffort::High,
|
||||
"High",
|
||||
);
|
||||
egui::ComboBox::from_id_salt("reasoning_effort")
|
||||
.selected_text(self.reasoning_effort.to_string())
|
||||
.show_ui(ui, |ui| {
|
||||
ui.selectable_value(
|
||||
&mut self.reasoning_effort,
|
||||
ReasoningEffort::Minimal,
|
||||
"Minimal",
|
||||
);
|
||||
ui.selectable_value(
|
||||
&mut self.reasoning_effort,
|
||||
ReasoningEffort::Low,
|
||||
"Low",
|
||||
);
|
||||
ui.selectable_value(
|
||||
&mut self.reasoning_effort,
|
||||
ReasoningEffort::Medium,
|
||||
"Medium",
|
||||
);
|
||||
ui.selectable_value(
|
||||
&mut self.reasoning_effort,
|
||||
ReasoningEffort::High,
|
||||
"High",
|
||||
);
|
||||
});
|
||||
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Model override");
|
||||
ui.add(egui::TextEdit::singleline(&mut self.model_override));
|
||||
ui.end_row();
|
||||
});
|
||||
});
|
||||
|
||||
ui.end_row();
|
||||
egui::TopBottomPanel::top("continue_instruction")
|
||||
.resizable(true)
|
||||
.show_inside(ui, |ui| {
|
||||
egui::CollapsingHeader::new("Instructions")
|
||||
.default_open(true)
|
||||
.show(ui, |ui| {
|
||||
egui::ScrollArea::vertical()
|
||||
.auto_shrink([false, false])
|
||||
.max_height(ui.available_height())
|
||||
.show(ui, |ui| {
|
||||
ui.add(
|
||||
egui::TextEdit::multiline(&mut self.instruction)
|
||||
.frame(false)
|
||||
.desired_width(ui.available_width())
|
||||
.hint_text("Writing Instructions"),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
ui.label("Model override");
|
||||
ui.add(egui::TextEdit::singleline(&mut self.model_override));
|
||||
ui.end_row();
|
||||
egui::TopBottomPanel::top("continue_context")
|
||||
.resizable(true)
|
||||
.show_inside(ui, |ui| {
|
||||
egui::CollapsingHeader::new("Context")
|
||||
.default_open(true)
|
||||
.show(ui, |ui| {
|
||||
egui::ScrollArea::vertical()
|
||||
.auto_shrink([false, false])
|
||||
.max_height(ui.available_height())
|
||||
.show(ui, |ui| {
|
||||
ui.add(
|
||||
egui::TextEdit::multiline(&mut self.context_override)
|
||||
.frame(false)
|
||||
.desired_width(ui.available_width())
|
||||
.hint_text("Any additional context?"),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
egui::TopBottomPanel::top("continue_system_prompt")
|
||||
.resizable(true)
|
||||
.show_inside(ui, |ui| {
|
||||
egui::CollapsingHeader::new("System prompt")
|
||||
.default_open(true)
|
||||
.show(ui, |ui| {
|
||||
egui::ScrollArea::vertical()
|
||||
.auto_shrink([false, false])
|
||||
.max_height(ui.available_height())
|
||||
.show(ui, |ui| {
|
||||
ui.add(
|
||||
egui::TextEdit::multiline(&mut self.system_prompt)
|
||||
.frame(false)
|
||||
.desired_width(ui.available_width())
|
||||
.hint_text("System prompt"),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
self.ui_output_box(ui, project);
|
||||
|
||||
ui.separator();
|
||||
|
||||
// Instructions
|
||||
egui::ScrollArea::both()
|
||||
.id_salt("continue_instruction")
|
||||
.auto_shrink([true, false])
|
||||
.max_height(ui.available_height() / 2.0)
|
||||
.max_width(ui.available_width())
|
||||
.show(ui, |ui| {
|
||||
ui.add(
|
||||
egui::TextEdit::multiline(&mut self.instruction)
|
||||
.frame(false)
|
||||
.desired_width(ui.available_width())
|
||||
.hint_text("Writing Instructions"),
|
||||
);
|
||||
});
|
||||
ui.separator();
|
||||
|
||||
// Context
|
||||
egui::ScrollArea::both()
|
||||
.id_salt("continue_context")
|
||||
.auto_shrink([true, false])
|
||||
.max_height(ui.available_height())
|
||||
.max_width(ui.available_width())
|
||||
.show(ui, |ui| {
|
||||
ui.add(
|
||||
egui::TextEdit::multiline(&mut self.context_override)
|
||||
.frame(false)
|
||||
.desired_width(ui.available_width())
|
||||
.hint_text("Any additional context?"),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn continue_content(
|
||||
context: String,
|
||||
previous_content: String,
|
||||
instruction: String,
|
||||
ai_input: AIInput,
|
||||
// context: String,
|
||||
// previous_content: String,
|
||||
// instruction: String,
|
||||
options: AIOptions,
|
||||
project: ProjectSettings,
|
||||
result: Arc<Mutex<String>>,
|
||||
reasoning: Arc<Mutex<String>>,
|
||||
ready: Arc<Mutex<ReadyState>>,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
*ready.lock().unwrap() = ReadyState::Generating;
|
||||
@@ -240,27 +335,15 @@ pub fn continue_content(
|
||||
let messages = vec![
|
||||
Message {
|
||||
role: "system".to_string(),
|
||||
content: "
|
||||
Please generate content that is a continuation of the given text.
|
||||
Your response should be a logical next step in the content and should not repeat any of the text from the instruction or the content.
|
||||
Do not generate any text that is not a direct continuation of the content.
|
||||
if extra instructions are provided, follow them exactly, otherwise continue the text in a logical way.
|
||||
your output should NEVER be a repeat of any previous content
|
||||
".to_string(),
|
||||
content: ai_input.system_prompt,
|
||||
},
|
||||
Message {
|
||||
role: "user".to_string(),
|
||||
content: format!("Context / General instructions: {context}"),
|
||||
content: format!(
|
||||
"<Instructions> {}\n\n<Previous content> {}\n\n",
|
||||
ai_input.user_prompt, ai_input.previous_content
|
||||
),
|
||||
},
|
||||
Message {
|
||||
role: "user".to_string(),
|
||||
content: format!("Content to continue: {previous_content}"),
|
||||
},
|
||||
Message {
|
||||
role: "user".to_string(),
|
||||
content: format!("Specific instructions: {instruction}"),
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
let request = ChatRequest {
|
||||
@@ -288,19 +371,20 @@ pub fn continue_content(
|
||||
|
||||
let response = if let Some(k) = api_key {
|
||||
client
|
||||
.post(llm_api_uri + "/v1/chat/completions")
|
||||
.post(llm_api_uri + "/api/v0/chat/completions")
|
||||
.json(&request)
|
||||
.bearer_auth(k)
|
||||
.send()?
|
||||
} else {
|
||||
client
|
||||
.post(llm_api_uri + "/v1/chat/completions")
|
||||
.post(llm_api_uri + "/api/v0/chat/completions")
|
||||
.json(&request)
|
||||
.send()?
|
||||
};
|
||||
|
||||
println!("success!");
|
||||
|
||||
// println!("response: {}", response.text().unwrap());
|
||||
let reader = BufReader::new(response);
|
||||
for line in reader.lines() {
|
||||
// initial loop to check if the user has terminated the generation
|
||||
@@ -309,6 +393,7 @@ pub fn continue_content(
|
||||
|
||||
if *ready == ReadyState::Halted {
|
||||
result.lock().unwrap().clear();
|
||||
reasoning.lock().unwrap().clear();
|
||||
}
|
||||
|
||||
if *ready != ReadyState::Generating {
|
||||
@@ -324,9 +409,17 @@ pub fn continue_content(
|
||||
|
||||
if let Some(json) = line.strip_prefix("data: ") {
|
||||
if let Ok(chunk) = serde_json::from_str::<StreamingChatResponse>(json) {
|
||||
println!("chunk: {chunk:?}");
|
||||
|
||||
if let Some(content) = chunk.choices[0].delta.content.as_ref() {
|
||||
println!("content: {content}");
|
||||
result.lock().unwrap().push_str(content);
|
||||
}
|
||||
|
||||
if let Some(reasoning_content) = chunk.choices[0].delta.reasoning_content.as_ref() {
|
||||
println!("reasoning_content: {reasoning_content}");
|
||||
reasoning.lock().unwrap().push_str(reasoning_content);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -343,6 +436,13 @@ pub struct AIOptions {
|
||||
pub model_override: Option<String>,
|
||||
}
|
||||
|
||||
pub struct AIInput {
|
||||
pub system_prompt: String,
|
||||
pub user_prompt: String,
|
||||
pub previous_content: String,
|
||||
pub structure: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
pub enum ReadyState {
|
||||
Idle,
|
||||
@@ -351,10 +451,12 @@ pub enum ReadyState {
|
||||
Halted,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Copy, Clone, PartialEq)]
|
||||
#[derive(Serialize, Copy, Clone, PartialEq, Default)]
|
||||
pub enum ReasoningEffort {
|
||||
#[serde(rename = "minimal")]
|
||||
Minimal,
|
||||
|
||||
#[default]
|
||||
#[serde(rename = "low")]
|
||||
Low,
|
||||
#[serde(rename = "medium")]
|
||||
@@ -363,19 +465,13 @@ pub enum ReasoningEffort {
|
||||
High,
|
||||
}
|
||||
|
||||
impl Default for ReasoningEffort {
|
||||
fn default() -> Self {
|
||||
ReasoningEffort::Low
|
||||
}
|
||||
}
|
||||
|
||||
impl ToString for ReasoningEffort {
|
||||
fn to_string(&self) -> String {
|
||||
impl std::fmt::Display for ReasoningEffort {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
ReasoningEffort::Minimal => "Minimal".to_string(),
|
||||
ReasoningEffort::Low => "Low".to_string(),
|
||||
ReasoningEffort::Medium => "Medium".to_string(),
|
||||
ReasoningEffort::High => "High".to_string(),
|
||||
ReasoningEffort::Minimal => write!(f, "Minimal"),
|
||||
ReasoningEffort::Low => write!(f, "Low"),
|
||||
ReasoningEffort::Medium => write!(f, "Medium"),
|
||||
ReasoningEffort::High => write!(f, "High"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -413,8 +509,11 @@ struct Delta {
|
||||
#[serde(default)]
|
||||
#[allow(unused)]
|
||||
role: Option<String>,
|
||||
|
||||
#[serde(default)]
|
||||
content: Option<String>,
|
||||
#[serde(default)]
|
||||
reasoning_content: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
|
||||
@@ -6,8 +6,11 @@ use egui::ScrollArea;
|
||||
|
||||
mod editors;
|
||||
mod explorer;
|
||||
|
||||
#[cfg(feature = "llm")]
|
||||
mod llm_integration;
|
||||
|
||||
mod index;
|
||||
mod util;
|
||||
|
||||
use crate::{
|
||||
|
||||
Reference in New Issue
Block a user