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:
+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)]
|
||||
|
||||
Reference in New Issue
Block a user