fixed ai integration
Continuous integration / build (push) Failing after 6m27s

This commit is contained in:
2025-07-27 19:15:30 +01:00
parent 6a25c6c05c
commit bc71a30bfa
7 changed files with 292 additions and 43 deletions
+186 -13
View File
@@ -2,13 +2,18 @@ use egui::TextEdit;
use egui_commonmark::{CommonMarkCache, CommonMarkViewer};
use serde::{self, Deserialize, Serialize};
use crate::{PROJECT_FOLDER, editors::tags::Tag, llm_integration::content_llm::ai_enabled, util};
use crate::{
PROJECT_FOLDER,
editors::{context_editor::ProjectContext, tags::Tag},
util,
};
pub struct MainEditor {
pub content: ContentSection,
pub show_editor: bool,
pub show_preview: bool,
preview_cache: CommonMarkCache,
dialog: Option<ContentAI>,
}
impl Clone for MainEditor {
@@ -19,6 +24,7 @@ impl Clone for MainEditor {
show_editor: self.show_editor,
show_preview: self.show_preview,
preview_cache: CommonMarkCache::default(),
dialog: self.dialog.clone(),
}
}
}
@@ -48,6 +54,143 @@ pub struct ContentSection {
pub saved: bool,
}
#[derive(Clone)]
pub enum ContentAI {
Summarise {
open: bool,
content: String,
result: String,
ready: bool,
},
Continue {
open: bool,
content: String,
instruction: String,
max_tokens: usize,
context_override: String,
result: String,
ready: bool,
},
}
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("ContentAI")
.open(&mut is_open)
.show(ui.ctx(), |ui| match self {
ContentAI::Summarise {
content,
result,
ready,
..
} => {
egui::ScrollArea::vertical()
.auto_shrink([false, false])
.max_height(200.0)
.max_width(ui.available_width())
.show(ui, |ui| {
ui.add(
egui::TextEdit::multiline(content)
.frame(false)
.interactive(false),
);
});
ui.add(
egui::TextEdit::multiline(result)
.font(egui::TextStyle::Monospace)
.interactive(false)
.frame(false)
.lock_focus(true)
.hint_text("Summary will appear here..."),
);
if ui.button("Summarise").clicked() {
*result = Self::summarise(content).unwrap();
*ready = true;
}
}
ContentAI::Continue {
content,
instruction,
max_tokens,
context_override,
result,
ready,
..
} => {
ui.label("Continue");
ui.add(
egui::TextEdit::multiline(instruction)
.frame(false)
.hint_text("Instruction"),
);
ui.add(
egui::TextEdit::multiline(context_override)
.frame(false)
.hint_text("Any additional context?"),
);
ui.label("Max Tokens");
ui.add(egui::Slider::new(max_tokens, 1000..=1000000));
if ui.button("Continue").clicked() {
match Self::continue_content(
instruction,
*max_tokens,
context_override,
project,
content,
) {
Ok(str) => {
*result = str;
*ready = true;
}
Err(err) => {
*result = format!("Error: {err}");
*ready = true;
}
}
}
}
});
}
match self {
ContentAI::Summarise { open, .. } => *open = is_open,
ContentAI::Continue { open, .. } => *open = is_open,
};
}
fn summarise(_content: &str) -> Result<String, Box<dyn std::error::Error>> {
// crate::llm_integration::content_llm::summarise_content(content, result)
Ok(String::new())
}
fn continue_content(
instruction: &str,
max_tokens: usize,
context_override: &str,
project: &mut ProjectContext,
content: &mut String,
) -> Result<String, Box<dyn std::error::Error>> {
crate::llm_integration::content_llm::continue_content(
if context_override.is_empty() {
&project.ai_context_prompt
} else {
context_override
},
content,
instruction,
max_tokens,
)
}
}
impl ContentSection {
pub fn new() -> Self {
Self {
@@ -97,6 +240,7 @@ impl MainEditor {
show_editor: false, // Start with editor hidden
show_preview: false,
preview_cache: CommonMarkCache::default(),
dialog: None,
}
}
@@ -106,10 +250,11 @@ impl MainEditor {
show_editor: true,
show_preview: false,
preview_cache: CommonMarkCache::default(),
dialog: None,
}
}
pub fn ui(&mut self, ctx: &egui::Context) {
pub fn ui(&mut self, ctx: &egui::Context, project: &mut ProjectContext) {
// Show the editor window if enabled
let mut show = self.show_editor;
if show {
@@ -119,6 +264,27 @@ impl MainEditor {
.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 {
self.content.content.push_str(result.as_str());
self.content.saved = false;
*ready = false;
}
}
ContentAI::Continue { ready, result, .. } => {
if *ready {
self.content.content.push_str(result.as_str());
self.content.saved = false;
*ready = false;
}
}
}
}
ui.vertical(|ui| {
// check for Ctrl+S to save
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
@@ -222,7 +388,7 @@ impl MainEditor {
self.preview_ui(ui);
}
self.editor_ui(ui);
self.editor_ui(ui, project);
});
}
@@ -263,7 +429,7 @@ impl MainEditor {
});
}
fn editor_ui(&mut self, ui: &mut egui::Ui) {
fn editor_ui(&mut self, ui: &mut egui::Ui, project: &mut ProjectContext) {
let _response = egui::ScrollArea::both()
.auto_shrink([false, false])
.id_salt("editor_scroll")
@@ -301,19 +467,26 @@ impl MainEditor {
ctx_menu = true;
ui.menu_button("AI Actions", |ui| {
ui.add_enabled_ui(ai_enabled(), |ui| {
ui.add_enabled_ui(project.ai_enabled(), |ui| {
if ui.button("Summarise").clicked() {
println!("Summarise");
self.dialog = Some(ContentAI::Summarise {
result: String::new(),
content: self.content.content.clone(),
open: true,
ready: false,
});
}
if ui.button("Continue").clicked() {
let content = self.content.content.clone();
let response =
crate::llm_integration::content_llm::continue_content(
&content, "", 1024,
)
.unwrap();
self.content.content.push_str(&response);
self.dialog = Some(ContentAI::Continue {
content: self.content.content.clone(),
instruction: String::new(),
max_tokens: 1024,
context_override: "".to_string(),
result: String::new(),
open: true,
ready: false,
});
}
});
});