|
| 1 | +//! Egui web application |
| 2 | +
|
| 3 | +use eframe::egui; |
| 4 | + |
| 5 | +/// The main web application |
| 6 | +#[derive(Default)] |
| 7 | +pub struct WebApp { |
| 8 | + message_input: String, |
| 9 | + messages: Vec<Message>, |
| 10 | + session_id: Option<String>, |
| 11 | + status: Status, |
| 12 | +} |
| 13 | + |
| 14 | +#[derive(Clone)] |
| 15 | +struct Message { |
| 16 | + role: String, |
| 17 | + content: String, |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Default)] |
| 21 | +struct Status { |
| 22 | + connected: bool, |
| 23 | + model: String, |
| 24 | + version: String, |
| 25 | +} |
| 26 | + |
| 27 | +impl WebApp { |
| 28 | + /// Create a new web app |
| 29 | + pub fn new(_cc: &eframe::CreationContext<'_>) -> Self { |
| 30 | + Self { |
| 31 | + message_input: String::new(), |
| 32 | + messages: Vec::new(), |
| 33 | + session_id: None, |
| 34 | + status: Status { |
| 35 | + connected: true, |
| 36 | + model: "claude-cli/opus".to_string(), |
| 37 | + version: "0.3.0".to_string(), |
| 38 | + }, |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl eframe::App for WebApp { |
| 44 | + fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { |
| 45 | + // Top panel with toolbar |
| 46 | + egui::TopBottomPanel::top("toolbar").show(ctx, |ui| { |
| 47 | + ui.horizontal(|ui| { |
| 48 | + ui.heading("LocalGPT"); |
| 49 | + ui.separator(); |
| 50 | + |
| 51 | + // Status indicator |
| 52 | + let status_color = if self.status.connected { |
| 53 | + egui::Color32::GREEN |
| 54 | + } else { |
| 55 | + egui::Color32::RED |
| 56 | + }; |
| 57 | + ui.colored_label(status_color, "●"); |
| 58 | + |
| 59 | + ui.label(format!("Model: {}", self.status.model)); |
| 60 | + |
| 61 | + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { |
| 62 | + if ui.button("New Session").clicked() { |
| 63 | + self.messages.clear(); |
| 64 | + self.session_id = None; |
| 65 | + } |
| 66 | + |
| 67 | + if let Some(ref id) = self.session_id { |
| 68 | + // Safe truncation that respects UTF-8 character boundaries |
| 69 | + let truncated: String = id.chars().take(8).collect(); |
| 70 | + ui.label(format!("Session: {}...", truncated)); |
| 71 | + } |
| 72 | + }); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + // Main chat area |
| 77 | + egui::CentralPanel::default().show(ctx, |ui| { |
| 78 | + egui::ScrollArea::vertical() |
| 79 | + .auto_shrink([false, false]) |
| 80 | + .stick_to_bottom(true) |
| 81 | + .show(ui, |ui| { |
| 82 | + if self.messages.is_empty() { |
| 83 | + ui.vertical_centered(|ui| { |
| 84 | + ui.add_space(100.0); |
| 85 | + ui.heading("Welcome to LocalGPT"); |
| 86 | + ui.label("This is a Proof of Concept egui web UI"); |
| 87 | + ui.add_space(10.0); |
| 88 | + ui.label("Type a message below to start chatting"); |
| 89 | + ui.add_space(10.0); |
| 90 | + ui.label("🚧 Note: This is a static demo without backend connection"); |
| 91 | + }); |
| 92 | + } else { |
| 93 | + for msg in &self.messages { |
| 94 | + self.render_message(ui, msg); |
| 95 | + } |
| 96 | + } |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + // Bottom input panel |
| 101 | + egui::TopBottomPanel::bottom("input").show(ctx, |ui| { |
| 102 | + ui.horizontal(|ui| { |
| 103 | + let input_response = ui.add( |
| 104 | + egui::TextEdit::multiline(&mut self.message_input) |
| 105 | + .desired_width(f32::INFINITY) |
| 106 | + .desired_rows(1) |
| 107 | + .hint_text("Type a message..."), |
| 108 | + ); |
| 109 | + |
| 110 | + let enter_without_shift = input_response.has_focus() |
| 111 | + && ui.input(|i| i.key_pressed(egui::Key::Enter) && !i.modifiers.shift); |
| 112 | + |
| 113 | + if ui.button("Send").clicked() || enter_without_shift { |
| 114 | + self.send_message(); |
| 115 | + input_response.request_focus(); |
| 116 | + } |
| 117 | + }); |
| 118 | + }); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +impl WebApp { |
| 123 | + fn render_message(&self, ui: &mut egui::Ui, msg: &Message) { |
| 124 | + let bg_color = if msg.role == "user" { |
| 125 | + egui::Color32::from_rgb(40, 40, 60) |
| 126 | + } else { |
| 127 | + egui::Color32::from_rgb(30, 50, 40) |
| 128 | + }; |
| 129 | + |
| 130 | + egui::Frame::none() |
| 131 | + .fill(bg_color) |
| 132 | + .inner_margin(egui::Margin::same(10.0)) |
| 133 | + .corner_radius(egui::CornerRadius::same(5.0)) |
| 134 | + .show(ui, |ui| { |
| 135 | + ui.horizontal(|ui| { |
| 136 | + let role_color = if msg.role == "user" { |
| 137 | + egui::Color32::LIGHT_BLUE |
| 138 | + } else { |
| 139 | + egui::Color32::LIGHT_GREEN |
| 140 | + }; |
| 141 | + ui.colored_label(role_color, &msg.role); |
| 142 | + }); |
| 143 | + ui.label(&msg.content); |
| 144 | + }); |
| 145 | + |
| 146 | + ui.add_space(8.0); |
| 147 | + } |
| 148 | + |
| 149 | + fn send_message(&mut self) { |
| 150 | + if self.message_input.trim().is_empty() { |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + // Add user message |
| 155 | + self.messages.push(Message { |
| 156 | + role: "user".to_string(), |
| 157 | + content: self.message_input.clone(), |
| 158 | + }); |
| 159 | + |
| 160 | + // Add a mock response |
| 161 | + self.messages.push(Message { |
| 162 | + role: "assistant".to_string(), |
| 163 | + content: format!( |
| 164 | + "This is a PoC demo. Your message was: \"{}\"\n\n\ |
| 165 | + In the full implementation, this would connect to the LocalGPT backend \ |
| 166 | + via WebSocket or HTTP API to send your message and stream the response.", |
| 167 | + self.message_input |
| 168 | + ), |
| 169 | + }); |
| 170 | + |
| 171 | + self.message_input.clear(); |
| 172 | + } |
| 173 | +} |
0 commit comments