reworked settings, general interface improvements, more AI configuration, bugfixes and QOL.
Continuous integration / build (push) Has been cancelled
Continuous integration / build (push) Has been cancelled
This commit is contained in:
+159
-286
@@ -6,14 +6,15 @@ use serde::{self, Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
PROJECT_FOLDER,
|
||||
editors::{context_editor::ProjectContext, tags::Tag},
|
||||
llm_integration::content_llm::ReadyState,
|
||||
editors::{settings_editor::ProjectSettings, tags::Tag},
|
||||
llm_integration::content_llm::{ContentAI, ReadyState},
|
||||
util,
|
||||
};
|
||||
|
||||
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>,
|
||||
@@ -25,6 +26,7 @@ impl Clone for MainEditor {
|
||||
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(),
|
||||
dialog: self.dialog.clone(),
|
||||
@@ -57,158 +59,6 @@ pub struct ContentSection {
|
||||
pub saved: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum ContentAI {
|
||||
Summarise {
|
||||
open: bool,
|
||||
content: String,
|
||||
result: Arc<Mutex<String>>,
|
||||
ready: Arc<Mutex<ReadyState>>,
|
||||
},
|
||||
Continue {
|
||||
open: bool,
|
||||
content: String,
|
||||
instruction: String,
|
||||
max_tokens: usize,
|
||||
context_override: String,
|
||||
result: Arc<Mutex<String>>,
|
||||
ready: Arc<Mutex<ReadyState>>,
|
||||
},
|
||||
}
|
||||
|
||||
impl ContentAI {
|
||||
pub fn ui(&mut self, ui: &mut egui::Ui, project: &mut ProjectContext) {
|
||||
let mut is_open = *match self {
|
||||
ContentAI::Summarise { open, .. } => open,
|
||||
ContentAI::Continue { open, .. } => open,
|
||||
};
|
||||
|
||||
if is_open {
|
||||
egui::Window::new("AI Assistant")
|
||||
.open(&mut is_open)
|
||||
.show(ui.ctx(), |ui| match self {
|
||||
ContentAI::Summarise {
|
||||
content,
|
||||
result,
|
||||
ready,
|
||||
..
|
||||
} => {
|
||||
egui::ScrollArea::vertical()
|
||||
.auto_shrink([false, false])
|
||||
.max_width(ui.available_width())
|
||||
.show(ui, |ui| {
|
||||
ui.add(
|
||||
egui::TextEdit::multiline(content)
|
||||
.frame(false)
|
||||
.interactive(false),
|
||||
);
|
||||
});
|
||||
ui.add(
|
||||
egui::TextEdit::multiline(&mut *result.lock().unwrap())
|
||||
.font(egui::TextStyle::Monospace)
|
||||
.interactive(false)
|
||||
.frame(false)
|
||||
.lock_focus(true)
|
||||
.hint_text("Summary will appear here..."),
|
||||
);
|
||||
if ui.button("Summarise").clicked() {
|
||||
// Self::summarise(content, result.clone());
|
||||
*ready.lock().unwrap() = ReadyState::Generating;
|
||||
}
|
||||
}
|
||||
ContentAI::Continue {
|
||||
content,
|
||||
instruction,
|
||||
max_tokens,
|
||||
context_override,
|
||||
result,
|
||||
ready,
|
||||
..
|
||||
} => {
|
||||
ui.weak("(The model will see current file content)");
|
||||
ui.separator();
|
||||
ui.add(
|
||||
egui::TextEdit::multiline(instruction)
|
||||
.frame(false)
|
||||
.hint_text("Writing Instructions"),
|
||||
);
|
||||
ui.separator();
|
||||
ui.add(
|
||||
egui::TextEdit::multiline(context_override)
|
||||
.frame(false)
|
||||
.hint_text("Any additional context?"),
|
||||
);
|
||||
ui.separator();
|
||||
ui.label("Max Tokens");
|
||||
ui.add(egui::Slider::new(max_tokens, 1000..=1000000));
|
||||
ui.separator();
|
||||
if ui.button("Continue").clicked() {
|
||||
let context_override = context_override.clone();
|
||||
let content = content.clone();
|
||||
let instruction = instruction.clone();
|
||||
let max_tokens = *max_tokens;
|
||||
let project = project.clone();
|
||||
let ai_context = project.ai_context_prompt.clone();
|
||||
let result = result.clone();
|
||||
let ready = ready.clone();
|
||||
|
||||
std::thread::spawn(move || {
|
||||
*ready.lock().unwrap() = ReadyState::Generating;
|
||||
|
||||
let result = crate::llm_integration::content_llm::continue_content(
|
||||
if context_override.is_empty() {
|
||||
ai_context
|
||||
} else {
|
||||
context_override
|
||||
},
|
||||
content,
|
||||
instruction,
|
||||
max_tokens,
|
||||
project,
|
||||
result,
|
||||
);
|
||||
if let Err(e) = result {
|
||||
eprintln!("Error in content generation: {e}");
|
||||
}
|
||||
|
||||
*ready.lock().unwrap() = ReadyState::Ready;
|
||||
});
|
||||
}
|
||||
|
||||
if *ready.lock().unwrap() == ReadyState::Generating {
|
||||
ui.horizontal(|ui| {
|
||||
ui.spinner();
|
||||
ui.label("Generating...");
|
||||
});
|
||||
|
||||
egui::ScrollArea::both()
|
||||
.auto_shrink([false, false])
|
||||
.max_width(ui.available_width())
|
||||
.show(ui, |ui| {
|
||||
ui.add(
|
||||
egui::TextEdit::multiline(&mut *result.lock().unwrap())
|
||||
.font(egui::TextStyle::Monospace)
|
||||
.interactive(false)
|
||||
.frame(false)
|
||||
.desired_width(ui.available_width())
|
||||
.lock_focus(true)
|
||||
.hint_text("Content will appear here..."),
|
||||
);
|
||||
});
|
||||
} else if *ready.lock().unwrap() == ReadyState::Idle {
|
||||
ui.label("Idle");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
match self {
|
||||
ContentAI::Summarise { open, .. } => *open = is_open,
|
||||
ContentAI::Continue { open, .. } => *open = is_open,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl ContentSection {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
@@ -257,6 +107,7 @@ impl MainEditor {
|
||||
content: ContentSection::new(),
|
||||
show_editor: false, // Start with editor hidden
|
||||
show_preview: false,
|
||||
editor_separate_window: false,
|
||||
preview_cache: CommonMarkCache::default(),
|
||||
dialog: None,
|
||||
}
|
||||
@@ -267,147 +118,167 @@ impl MainEditor {
|
||||
content,
|
||||
show_editor: true,
|
||||
show_preview: false,
|
||||
editor_separate_window: false,
|
||||
preview_cache: CommonMarkCache::default(),
|
||||
dialog: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ui(&mut self, ctx: &egui::Context, project: &mut ProjectContext) {
|
||||
pub fn render_ui(&mut self, project: &mut ProjectSettings, ui: &mut egui::Ui) {
|
||||
if let Some(dialog) = &mut self.dialog {
|
||||
dialog.ui(ui, project);
|
||||
|
||||
match dialog {
|
||||
ContentAI::Summarise { ready, result, .. } => {
|
||||
if *ready.lock().unwrap() == ReadyState::Ready {
|
||||
self.content.content.push_str(&result.lock().unwrap());
|
||||
self.content.saved = false;
|
||||
*ready.lock().unwrap() = ReadyState::Idle;
|
||||
} else if *ready.lock().unwrap() == ReadyState::Halted {
|
||||
*ready.lock().unwrap() = ReadyState::Idle;
|
||||
}
|
||||
}
|
||||
ContentAI::Continue {
|
||||
ready,
|
||||
result,
|
||||
content,
|
||||
..
|
||||
} => {
|
||||
*content = self.content.content.clone();
|
||||
if *ready.lock().unwrap() == ReadyState::Ready {
|
||||
self.content.content.push_str(&result.lock().unwrap());
|
||||
self.content.saved = false;
|
||||
*ready.lock().unwrap() = ReadyState::Idle;
|
||||
} else if *ready.lock().unwrap() == ReadyState::Halted {
|
||||
*ready.lock().unwrap() = ReadyState::Idle;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ui.vertical(|ui| {
|
||||
// check for Ctrl+S to save
|
||||
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
|
||||
if let Err(e) = self.content.save() {
|
||||
eprintln!("Failed to save: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
// display save state
|
||||
util::saved_status(
|
||||
ui,
|
||||
self.content.saved,
|
||||
&self.content.id,
|
||||
&self.content.title,
|
||||
);
|
||||
|
||||
// Save/Cancel buttons
|
||||
ui.horizontal(|ui| {
|
||||
// save button
|
||||
if ui.button("Save").clicked() {
|
||||
if let Err(e) = self.content.save() {
|
||||
eprintln!("Failed to save: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
// create copy button
|
||||
if ui.button("Create Copy").clicked() {
|
||||
let mut copy = self.clone();
|
||||
copy.content.id = uuid::Uuid::new_v4().to_string();
|
||||
copy.content.title = format!("{} (Copy)", self.content.title);
|
||||
copy.content.save().unwrap();
|
||||
}
|
||||
|
||||
// delete button
|
||||
if ui.button("Delete").clicked() {
|
||||
std::fs::remove_file(
|
||||
PROJECT_FOLDER
|
||||
.join("documents")
|
||||
.join(format!("{}.json", self.content.id)),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
*self = Self::new();
|
||||
}
|
||||
|
||||
// revert changes button
|
||||
if ui.button("Revert changes").clicked() {
|
||||
self.content = ContentSection::load(&self.content.id).unwrap();
|
||||
}
|
||||
|
||||
// preview toggle
|
||||
ui.checkbox(&mut self.show_preview, "Preview");
|
||||
|
||||
// editor toggle
|
||||
ui.checkbox(&mut self.editor_separate_window, "Pop out editor");
|
||||
});
|
||||
});
|
||||
|
||||
ui.separator();
|
||||
|
||||
// Name and description grid
|
||||
egui::Grid::new("top_grid")
|
||||
.striped(true)
|
||||
.num_columns(2)
|
||||
.show(ui, |ui| {
|
||||
ui.strong("Name");
|
||||
if ui
|
||||
.add(
|
||||
TextEdit::singleline(&mut self.content.title)
|
||||
.desired_width(f32::INFINITY)
|
||||
.frame(false),
|
||||
)
|
||||
.changed()
|
||||
{
|
||||
self.content.saved = false;
|
||||
}
|
||||
ui.end_row();
|
||||
|
||||
ui.strong("Description");
|
||||
if ui
|
||||
.add(
|
||||
TextEdit::singleline(&mut self.content.description)
|
||||
.desired_width(f32::INFINITY)
|
||||
.frame(false),
|
||||
)
|
||||
.changed()
|
||||
{
|
||||
self.content.saved = false;
|
||||
}
|
||||
ui.end_row();
|
||||
|
||||
ui.strong("Tags");
|
||||
Tag::selector_ui(&mut self.content.tags, ui, Some(&mut self.content.saved));
|
||||
ui.end_row();
|
||||
});
|
||||
|
||||
ui.separator();
|
||||
|
||||
if self.show_preview {
|
||||
self.preview_ui(ui);
|
||||
}
|
||||
|
||||
self.editor_ui(ui, project);
|
||||
}
|
||||
|
||||
pub fn ui(&mut self, ctx: &egui::Context, project: &mut ProjectSettings) {
|
||||
// Show the editor window if enabled
|
||||
let mut show = self.show_editor;
|
||||
if show {
|
||||
egui::Window::new("Markdown Editor")
|
||||
.resizable(true)
|
||||
.default_width(1000.0)
|
||||
.default_height(800.0)
|
||||
.open(&mut show)
|
||||
.show(ctx, |ui| {
|
||||
if let Some(dialog) = &mut self.dialog {
|
||||
dialog.ui(ui, project);
|
||||
|
||||
match dialog {
|
||||
ContentAI::Summarise { ready, result, .. } => {
|
||||
if *ready.lock().unwrap() == ReadyState::Ready {
|
||||
self.content.content.push_str(&result.lock().unwrap());
|
||||
self.content.saved = false;
|
||||
*ready.lock().unwrap() = ReadyState::Idle;
|
||||
}
|
||||
}
|
||||
ContentAI::Continue { ready, result, .. } => {
|
||||
if *ready.lock().unwrap() == ReadyState::Ready {
|
||||
self.content.content.push_str(&result.lock().unwrap());
|
||||
self.content.saved = false;
|
||||
*ready.lock().unwrap() = ReadyState::Idle;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ui.vertical(|ui| {
|
||||
// check for Ctrl+S to save
|
||||
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
|
||||
if let Err(e) = self.content.save() {
|
||||
eprintln!("Failed to save: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
// display save state
|
||||
util::saved_status(
|
||||
ui,
|
||||
self.content.saved,
|
||||
&self.content.id,
|
||||
&self.content.title,
|
||||
);
|
||||
|
||||
// Save/Cancel buttons
|
||||
ui.horizontal(|ui| {
|
||||
// save button
|
||||
if ui.button("Save").clicked() {
|
||||
if let Err(e) = self.content.save() {
|
||||
eprintln!("Failed to save: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
// create copy button
|
||||
if ui.button("Create Copy").clicked() {
|
||||
let mut copy = self.clone();
|
||||
copy.content.id = uuid::Uuid::new_v4().to_string();
|
||||
copy.content.title = format!("{} (Copy)", self.content.title);
|
||||
copy.content.save().unwrap();
|
||||
}
|
||||
|
||||
// delete button
|
||||
if ui.button("Delete").clicked() {
|
||||
std::fs::remove_file(
|
||||
PROJECT_FOLDER
|
||||
.join("documents")
|
||||
.join(format!("{}.json", self.content.id)),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
*self = Self::new();
|
||||
}
|
||||
|
||||
// revert changes button
|
||||
if ui.button("Revert changes").clicked() {
|
||||
self.content = ContentSection::load(&self.content.id).unwrap();
|
||||
}
|
||||
|
||||
// preview toggle
|
||||
ui.checkbox(&mut self.show_preview, "Preview");
|
||||
});
|
||||
if self.editor_separate_window {
|
||||
egui::Window::new("Editor")
|
||||
.resizable(true)
|
||||
.default_width(1000.0)
|
||||
.default_height(800.0)
|
||||
.open(&mut show)
|
||||
.show(ctx, |ui| {
|
||||
self.render_ui(project, ui);
|
||||
});
|
||||
|
||||
ui.separator();
|
||||
|
||||
// Name and description grid
|
||||
egui::Grid::new("top_grid")
|
||||
.striped(true)
|
||||
.num_columns(2)
|
||||
.show(ui, |ui| {
|
||||
ui.strong("Name");
|
||||
if ui
|
||||
.add(
|
||||
TextEdit::singleline(&mut self.content.title)
|
||||
.desired_width(f32::INFINITY)
|
||||
.frame(false),
|
||||
)
|
||||
.changed()
|
||||
{
|
||||
self.content.saved = false;
|
||||
}
|
||||
ui.end_row();
|
||||
|
||||
ui.strong("Description");
|
||||
if ui
|
||||
.add(
|
||||
TextEdit::singleline(&mut self.content.description)
|
||||
.desired_width(f32::INFINITY)
|
||||
.frame(false),
|
||||
)
|
||||
.changed()
|
||||
{
|
||||
self.content.saved = false;
|
||||
}
|
||||
ui.end_row();
|
||||
|
||||
ui.strong("Tags");
|
||||
Tag::selector_ui(
|
||||
&mut self.content.tags,
|
||||
ui,
|
||||
Some(&mut self.content.saved),
|
||||
);
|
||||
ui.end_row();
|
||||
});
|
||||
|
||||
ui.separator();
|
||||
|
||||
if self.show_preview {
|
||||
self.preview_ui(ui);
|
||||
}
|
||||
|
||||
self.editor_ui(ui, project);
|
||||
} else {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
self.render_ui(project, ui);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
self.show_editor = show;
|
||||
@@ -447,7 +318,7 @@ impl MainEditor {
|
||||
});
|
||||
}
|
||||
|
||||
fn editor_ui(&mut self, ui: &mut egui::Ui, project: &mut ProjectContext) {
|
||||
fn editor_ui(&mut self, ui: &mut egui::Ui, project: &mut ProjectSettings) {
|
||||
let _response = egui::ScrollArea::both()
|
||||
.auto_shrink([false, false])
|
||||
.id_salt("editor_scroll")
|
||||
@@ -504,6 +375,8 @@ impl MainEditor {
|
||||
result: Arc::new(Mutex::new(String::new())),
|
||||
open: true,
|
||||
ready: Arc::new(Mutex::new(ReadyState::Idle)),
|
||||
temperature: 0.7,
|
||||
model_override: "".to_string(),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,148 +0,0 @@
|
||||
use std::io::Read;
|
||||
|
||||
use chrono::NaiveDate;
|
||||
use egui::TextEdit;
|
||||
use egui_extras::DatePickerButton;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::PROJECT_FOLDER;
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct ProjectContext {
|
||||
date: NaiveDate,
|
||||
project_name: String,
|
||||
project_author: String,
|
||||
project_description: String,
|
||||
|
||||
// settings
|
||||
enable_ai: bool,
|
||||
pub llm_api_uri: String,
|
||||
pub llm_api_key: String,
|
||||
pub ai_context_prompt: String,
|
||||
|
||||
#[serde(skip)]
|
||||
pub open: bool,
|
||||
}
|
||||
|
||||
impl ProjectContext {
|
||||
#[allow(dead_code)]
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn load() -> Self {
|
||||
let path = PROJECT_FOLDER.join("context.json");
|
||||
if let Ok(mut file) = std::fs::File::open(path) {
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents).unwrap();
|
||||
if let Ok(proj) = serde_json::from_str(&contents) {
|
||||
return proj;
|
||||
}
|
||||
}
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn save(&self) {
|
||||
let path = PROJECT_FOLDER.join("context.json");
|
||||
let content = serde_json::to_string_pretty(self).unwrap();
|
||||
std::fs::write(path, content).unwrap();
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn ui(&mut self, ui: &mut egui::Ui) {
|
||||
// table
|
||||
egui::Grid::new("context_editor")
|
||||
.striped(true)
|
||||
.num_columns(2)
|
||||
.show(ui, |ui| {
|
||||
ui.label("Project Name");
|
||||
ui.text_edit_singleline(&mut self.project_name);
|
||||
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Project Author");
|
||||
ui.text_edit_singleline(&mut self.project_author);
|
||||
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Project Description");
|
||||
ui.text_edit_singleline(&mut self.project_description);
|
||||
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Date");
|
||||
ui.add(DatePickerButton::new(&mut self.date));
|
||||
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Enable AI");
|
||||
ui.checkbox(&mut self.enable_ai, "Enable AI");
|
||||
|
||||
ui.end_row();
|
||||
|
||||
ui.label("LLM API URI");
|
||||
ui.text_edit_singleline(&mut self.llm_api_uri);
|
||||
|
||||
ui.end_row();
|
||||
|
||||
ui.label("LLM API Key");
|
||||
ui.text_edit_singleline(&mut self.llm_api_key);
|
||||
|
||||
ui.end_row();
|
||||
|
||||
ui.label("AI Context Prompt");
|
||||
ui.add(TextEdit::multiline(&mut self.ai_context_prompt)
|
||||
.font(egui::TextStyle::Monospace)
|
||||
.interactive(true)
|
||||
.frame(false)
|
||||
.lock_focus(true)
|
||||
.hint_text("What is this project about? what should the LLM know when generating content for this project?"));
|
||||
ui.end_row();
|
||||
});
|
||||
}
|
||||
|
||||
pub fn ai_enabled(&mut self) -> bool {
|
||||
let client = reqwest::blocking::Client::new();
|
||||
|
||||
if self.enable_ai {
|
||||
return true;
|
||||
}
|
||||
|
||||
if client
|
||||
.get(self.llm_api_uri.clone() + "/v1/models")
|
||||
.send()
|
||||
.is_ok()
|
||||
{
|
||||
self.enable_ai = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
pub fn open(&mut self) {
|
||||
self.open = true;
|
||||
}
|
||||
|
||||
pub fn close(&mut self) {
|
||||
self.open = false;
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ProjectContext {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
date: chrono::Local::now().naive_local().into(),
|
||||
project_name: "New Project".to_string(),
|
||||
project_author: "Your Name".to_string(),
|
||||
project_description: "Description of your project".to_string(),
|
||||
|
||||
enable_ai: true,
|
||||
llm_api_uri: "http://localhost:1234".to_string(),
|
||||
llm_api_key: "".to_string(),
|
||||
ai_context_prompt: "".to_string(),
|
||||
|
||||
open: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
pub mod asset_editor;
|
||||
pub mod content_editor;
|
||||
pub mod context_editor;
|
||||
pub mod note_editor;
|
||||
pub mod object_editor;
|
||||
pub mod settings_editor;
|
||||
pub mod tags;
|
||||
pub mod template_editor;
|
||||
|
||||
@@ -0,0 +1,333 @@
|
||||
use std::{
|
||||
cell::OnceCell,
|
||||
io::Read,
|
||||
path::{Path, PathBuf},
|
||||
sync::LazyLock,
|
||||
};
|
||||
|
||||
use chrono::NaiveDate;
|
||||
use egui::TextEdit;
|
||||
use egui_extras::DatePickerButton;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{PROJECT_FOLDER, util::saved_status};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct ProjectSettings {
|
||||
date: NaiveDate,
|
||||
project_name: String,
|
||||
project_author: String,
|
||||
project_description: String,
|
||||
|
||||
// AI settings
|
||||
pub ai_context: String,
|
||||
|
||||
// settings
|
||||
#[serde(skip)]
|
||||
pub global_settings: EditorSettings,
|
||||
#[serde(skip)]
|
||||
pub local_overrides: EditorSettings,
|
||||
|
||||
#[serde(skip)]
|
||||
pub open: bool,
|
||||
|
||||
#[serde(skip)]
|
||||
pub saved: bool,
|
||||
}
|
||||
|
||||
static GLOBAL_SETTINGS_PATH: LazyLock<String> =
|
||||
LazyLock::new(|| match std::env::var("XDG_CONFIG_HOME") {
|
||||
Ok(path) => path + "/worldcoder/settings.json",
|
||||
Err(_) => {
|
||||
eprintln!(
|
||||
"XDG_CONFIG_HOME not set, using default path of ~/.config/worldcoder/settings.json"
|
||||
);
|
||||
"~/.config/worldcoder/settings.json".to_string()
|
||||
}
|
||||
});
|
||||
|
||||
impl ProjectSettings {
|
||||
#[allow(dead_code)]
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn load() -> Self {
|
||||
let project_path = PROJECT_FOLDER.join("project.json");
|
||||
|
||||
let mut file = if let Ok(file) = std::fs::File::open(project_path) {
|
||||
file
|
||||
} else {
|
||||
return Self::default();
|
||||
};
|
||||
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents).unwrap();
|
||||
if let Ok(mut proj) = serde_json::from_str::<Self>(&contents) {
|
||||
proj.saved = true;
|
||||
|
||||
// load global settings
|
||||
proj.global_settings = EditorSettings::load_global();
|
||||
|
||||
// load local overrides
|
||||
proj.local_overrides = EditorSettings::load();
|
||||
|
||||
proj
|
||||
} else {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(&mut self) {
|
||||
let project_path = PROJECT_FOLDER.join("project.json");
|
||||
let content = serde_json::to_string_pretty(self).unwrap();
|
||||
std::fs::write(project_path, content).unwrap();
|
||||
|
||||
self.global_settings.save();
|
||||
self.local_overrides.save();
|
||||
|
||||
self.saved = true;
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn ui(&mut self, ui: &mut egui::Ui) {
|
||||
// save state
|
||||
saved_status(ui, self.saved, "N/A", "Project Settings");
|
||||
if ui.button("Save").clicked() {
|
||||
self.save();
|
||||
}
|
||||
|
||||
ui.separator();
|
||||
|
||||
// project settings
|
||||
ui.heading("Project Settings");
|
||||
egui::Grid::new("project settings")
|
||||
.striped(true)
|
||||
.num_columns(2)
|
||||
.show(ui, |ui| {
|
||||
ui.label("Project Name");
|
||||
ui.text_edit_singleline(&mut self.project_name);
|
||||
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Project Author");
|
||||
ui.text_edit_singleline(&mut self.project_author);
|
||||
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Project Description");
|
||||
ui.text_edit_singleline(&mut self.project_description);
|
||||
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Date");
|
||||
ui.add(DatePickerButton::new(&mut self.date));
|
||||
|
||||
ui.end_row();
|
||||
|
||||
ui.label("AI Context Prompt");
|
||||
ui.add(TextEdit::multiline(&mut self.ai_context)
|
||||
.font(egui::TextStyle::Monospace)
|
||||
.interactive(true)
|
||||
.frame(false)
|
||||
.lock_focus(true)
|
||||
.hint_text("What is this project about? what should the LLM know when generating content for this project?"));
|
||||
|
||||
ui.end_row();
|
||||
});
|
||||
|
||||
ui.separator();
|
||||
|
||||
// local settings overrides for editor
|
||||
ui.heading("Local Overrides");
|
||||
egui::Grid::new("local overrides")
|
||||
.striped(true)
|
||||
.num_columns(2)
|
||||
.show(ui, |ui| {
|
||||
ui.label("Enable AI");
|
||||
if let Some(ai_enabled) = &mut self.local_overrides.ai_enabled {
|
||||
ui.checkbox(ai_enabled, "Enable AI");
|
||||
if ui.button("Remove Override").clicked() {
|
||||
self.local_overrides.ai_enabled = None;
|
||||
}
|
||||
} else if ui.button("Override").clicked() {
|
||||
self.local_overrides.ai_enabled = Some(true);
|
||||
}
|
||||
|
||||
ui.end_row();
|
||||
|
||||
ui.label("LLM API URI");
|
||||
if let Some(llm_api_uri) = &mut self.local_overrides.llm_api_uri {
|
||||
ui.text_edit_singleline(llm_api_uri);
|
||||
if ui.button("Remove Override").clicked() {
|
||||
self.local_overrides.llm_api_uri = None;
|
||||
}
|
||||
} else if ui.button("Override").clicked() {
|
||||
self.local_overrides.llm_api_uri = Some("http://localhost:1234".to_string());
|
||||
}
|
||||
|
||||
ui.end_row();
|
||||
|
||||
ui.label("LLM API Key");
|
||||
if let Some(llm_api_key) = &mut self.local_overrides.llm_api_key {
|
||||
ui.text_edit_singleline(llm_api_key);
|
||||
if ui.button("Remove Override").clicked() {
|
||||
self.local_overrides.llm_api_key = None;
|
||||
}
|
||||
} else if ui.button("Override").clicked() {
|
||||
self.local_overrides.llm_api_key = Some("1234".to_string());
|
||||
}
|
||||
|
||||
ui.end_row();
|
||||
});
|
||||
|
||||
ui.separator();
|
||||
|
||||
// global editor settings
|
||||
ui.heading("Global Editor Settings");
|
||||
egui::Grid::new("global settings")
|
||||
.striped(true)
|
||||
.num_columns(2)
|
||||
.show(ui, |ui| {
|
||||
ui.label("Enable AI");
|
||||
ui.checkbox(&mut self.global_settings.ai_enabled.unwrap(), "Enable AI");
|
||||
|
||||
ui.end_row();
|
||||
|
||||
ui.label("LLM API URI");
|
||||
ui.text_edit_singleline(self.global_settings.llm_api_uri.as_mut().unwrap());
|
||||
|
||||
ui.end_row();
|
||||
|
||||
ui.label("LLM API Key");
|
||||
ui.text_edit_singleline(self.global_settings.llm_api_key.as_mut().unwrap());
|
||||
|
||||
ui.end_row();
|
||||
});
|
||||
}
|
||||
|
||||
pub fn ai_enabled(&mut self) -> bool {
|
||||
let client = reqwest::blocking::Client::new();
|
||||
|
||||
if self.global_settings.ai_enabled.unwrap() {
|
||||
return true;
|
||||
}
|
||||
|
||||
if client
|
||||
.get(self.global_settings.llm_api_uri.clone().unwrap() + "/v1/models")
|
||||
.send()
|
||||
.is_ok()
|
||||
{
|
||||
self.global_settings.ai_enabled = Some(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
pub fn open(&mut self) {
|
||||
self.open = true;
|
||||
}
|
||||
|
||||
pub fn close(&mut self) {
|
||||
self.open = false;
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ProjectSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
date: chrono::Local::now().naive_local().into(),
|
||||
project_name: "New Project".to_string(),
|
||||
project_author: "Your Name".to_string(),
|
||||
project_description: "Description of your project".to_string(),
|
||||
|
||||
ai_context: "".to_string(),
|
||||
global_settings: EditorSettings::new(),
|
||||
local_overrides: EditorSettings::new(),
|
||||
|
||||
// window state
|
||||
open: false,
|
||||
saved: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct EditorSettings {
|
||||
pub llm_api_uri: Option<String>,
|
||||
pub llm_api_key: Option<String>,
|
||||
pub ai_enabled: Option<bool>,
|
||||
pub dark_theme: Option<bool>,
|
||||
|
||||
#[serde(skip)]
|
||||
is_global: bool,
|
||||
}
|
||||
|
||||
impl Default for EditorSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
llm_api_uri: Some("http://localhost:1234".to_string()),
|
||||
llm_api_key: Some("".to_string()),
|
||||
ai_enabled: Some(true),
|
||||
dark_theme: Some(true),
|
||||
|
||||
// window state
|
||||
is_global: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl EditorSettings {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
llm_api_uri: None,
|
||||
llm_api_key: None,
|
||||
ai_enabled: None,
|
||||
dark_theme: None,
|
||||
|
||||
is_global: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load() -> Self {
|
||||
let path = PROJECT_FOLDER.join("settings.json");
|
||||
let mut file = if let Ok(file) = std::fs::File::open(path) {
|
||||
file
|
||||
} else {
|
||||
return Self::default();
|
||||
};
|
||||
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents).unwrap();
|
||||
serde_json::from_str(&contents).unwrap()
|
||||
}
|
||||
|
||||
pub fn save(&self) {
|
||||
let content = serde_json::to_string_pretty(self).unwrap();
|
||||
|
||||
let path = if self.is_global {
|
||||
PathBuf::from(GLOBAL_SETTINGS_PATH.clone())
|
||||
} else {
|
||||
PROJECT_FOLDER.join("settings.json")
|
||||
};
|
||||
|
||||
std::fs::write(path, content).unwrap();
|
||||
}
|
||||
|
||||
pub fn load_global() -> Self {
|
||||
let path = PathBuf::from(GLOBAL_SETTINGS_PATH.clone());
|
||||
|
||||
if !path.exists() {
|
||||
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
|
||||
let content = serde_json::to_string_pretty(&Self::default()).unwrap();
|
||||
std::fs::write(&path, content).unwrap();
|
||||
|
||||
// return a default config
|
||||
return Self::default();
|
||||
}
|
||||
|
||||
let content = std::fs::read_to_string(path).unwrap();
|
||||
serde_json::from_str(&content).unwrap()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user