Skip to content

LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。

TypeScript

feat: add local HTTP clip server for browser extension integration

nash_sucee9c87c7ee7
diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock
index b8cd2eac..962c6562 100644
--- a/src-tauri/Cargo.lock
+++ b/src-tauri/Cargo.lock
@@ -76,6 +76,12 @@ dependencies = [
"derive_arbitrary",
]
+[[package]]
+name = "ascii"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16"
+
[[package]]
name = "async-broadcast"
version = "0.7.2"
@@ -570,6 +576,12 @@ dependencies = [
"windows-link 0.2.1",
]
+[[package]]
+name = "chunked_transfer"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6e4de3bc4ea267985becf712dc6d9eed8b04c953b3fcfb339ebc87acd9804901"
+
[[package]]
name = "cipher"
version = "0.4.4"
@@ -1830,6 +1842,12 @@ version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87"
+[[package]]
+name = "httpdate"
+version = "1.0.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
+
[[package]]
name = "hyper"
version = "1.9.0"
@@ -2339,6 +2357,7 @@ dependencies = [
"tauri-plugin-dialog",
"tauri-plugin-opener",
"tauri-plugin-store",
+ "tiny_http",
"zip 2.4.2",
]
@@ -4651,6 +4670,18 @@ dependencies = [
"time-core",
]
+[[package]]
+name = "tiny_http"
+version = "0.12.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "389915df6413a2e74fb181895f933386023c71110878cd0825588928e64cdc82"
+dependencies = [
+ "ascii",
+ "chunked_transfer",
+ "httpdate",
+ "log",
+]
+
[[package]]
name = "tinystr"
version = "0.8.3"
diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml
index 4ac78f8a..bc916584 100644
--- a/src-tauri/Cargo.toml
+++ b/src-tauri/Cargo.toml
@@ -27,6 +27,7 @@ chrono = { version = "0.4", features = ["clock"] }
tauri-plugin-dialog = "2.7.0"
pdf-extract = "0.10.0"
tauri-plugin-store = "2.4.2"
+tiny_http = "0.12"
zip = "2"
calamine = "0.34.0"
docx-rs = "0.4.20"
diff --git a/src-tauri/src/clip_server.rs b/src-tauri/src/clip_server.rs
new file mode 100644
index 00000000..be0d49cf
--- /dev/null
+++ b/src-tauri/src/clip_server.rs
@@ -0,0 +1,232 @@
+use std::sync::Mutex;
+use std::thread;
+use tiny_http::{Header, Method, Response, Server};
+
+static CURRENT_PROJECT: Mutex<String> = Mutex::new(String::new());
+
+pub fn start_clip_server() {
+ thread::spawn(|| {
+ let server = match Server::http("127.0.0.1:19827") {
+ Ok(s) => s,
+ Err(e) => {
+ eprintln!("Failed to start clip server: {}", e);
+ return;
+ }
+ };
+
+ println!("Clip server listening on http://127.0.0.1:19827");
+
+ for mut request in server.incoming_requests() {
+ let cors_headers = vec![
+ Header::from_bytes("Access-Control-Allow-Origin", "*").unwrap(),
+ Header::from_bytes("Access-Control-Allow-Methods", "GET, POST, OPTIONS").unwrap(),
+ Header::from_bytes("Access-Control-Allow-Headers", "Content-Type").unwrap(),
+ Header::from_bytes("Content-Type", "application/json").unwrap(),
+ ];
+
+ // Handle CORS preflight
+ if request.method() == &Method::Options {
+ let mut response = Response::from_string("").with_status_code(204);
+ for h in &cors_headers {
+ response.add_header(h.clone());
+ }
+ let _ = request.respond(response);
+ continue;
+ }
+
+ let url = request.url().to_string();
+
+ match (request.method(), url.as_str()) {
+ (&Method::Get, "/status") => {
+ let body = r#"{"ok":true,"version":"0.1.0"}"#;
+ let mut response = Response::from_string(body);
+ for h in &cors_headers {
+ response.add_header(h.clone());
+ }
+ let _ = request.respond(response);
+ }
+ (&Method::Get, "/project") => {
+ let path = CURRENT_PROJECT.lock().unwrap().clone();
+ let body = format!(r#"{{"ok":true,"path":"{}"}}"#, path);
+ let mut response = Response::from_string(body);
+ for h in &cors_headers {
+ response.add_header(h.clone());
+ }
+ let _ = request.respond(response);
+ }
+ (&Method::Post, "/project") => {
+ let mut body = String::new();
+ if let Err(e) = request.as_reader().read_to_string(&mut body) {
+ let err =
+ format!(r#"{{"ok":false,"error":"Failed to read body: {}"}}"#, e);
+ let mut response = Response::from_string(err).with_status_code(400);
+ for h in &cors_headers {
+ response.add_header(h.clone());
+ }
+ let _ = request.respond(response);
+ continue;
+ }
+
+ let result = handle_set_project(&body);
+ let status = if result.contains(r#""ok":true"#) {
+ 200
+ } else {
+ 400
+ };
+ let mut response = Response::from_string(result).with_status_code(status);
+ for h in &cors_headers {
+ response.add_header(h.clone());
+ }
+ let _ = request.respond(response);
+ }
+ (&Method::Post, "/clip") => {
+ let mut body = String::new();
+ if let Err(e) = request.as_reader().read_to_string(&mut body) {
+ let err =
+ format!(r#"{{"ok":false,"error":"Failed to read body: {}"}}"#, e);
+ let mut response = Response::from_string(err).with_status_code(400);
+ for h in &cors_headers {
+ response.add_header(h.clone());
+ }
+ let _ = request.respond(response);
+ continue;
+ }
+
+ let result = handle_clip(&body);
+ let status = if result.contains(r#""ok":true"#) {
+ 200
+ } else {
+ 500
+ };
+ let mut response = Response::from_string(result).with_status_code(status);
+ for h in &cors_headers {
+ response.add_header(h.clone());
+ }
+ let _ = request.respond(response);
+ }
+ _ => {
+ let body = r#"{"ok":false,"error":"Not found"}"#;
+ let mut response = Response::from_string(body).with_status_code(404);
+ for h in &cors_headers {
+ response.add_header(h.clone());
+ }
+ let _ = request.respond(response);
+ }
+ }
+ }
+ });
+}
+
+fn handle_set_project(body: &str) -> String {
+ let parsed: serde_json::Value = match serde_json::from_str(body) {
+ Ok(v) => v,
+ Err(e) => return format!(r#"{{"ok":false,"error":"Invalid JSON: {}"}}"#, e),
+ };
+
+ let path = match parsed["path"].as_str() {
+ Some(p) => p.to_string(),
+ None => return r#"{"ok":false,"error":"path field is required"}"#.to_string(),
+ };
+
+ match CURRENT_PROJECT.lock() {
+ Ok(mut guard) => {
+ *guard = path;
+ r#"{"ok":true}"#.to_string()
+ }
+ Err(e) => format!(r#"{{"ok":false,"error":"Lock error: {}"}}"#, e),
+ }
+}
+
+fn handle_clip(body: &str) -> String {
+ let parsed: serde_json::Value = match serde_json::from_str(body) {
+ Ok(v) => v,
+ Err(e) => return format!(r#"{{"ok":false,"error":"Invalid JSON: {}"}}"#, e),
+ };
+
+ let title = parsed["title"].as_str().unwrap_or("Untitled");
+ let url = parsed["url"].as_str().unwrap_or("");
+ let content = parsed["content"].as_str().unwrap_or("");
+
+ // Use projectPath from request body, or fall back to globally-set project path
+ let project_path_from_body = parsed["projectPath"].as_str().unwrap_or("").to_string();
+ let project_path = if project_path_from_body.is_empty() {
+ match CURRENT_PROJECT.lock() {
+ Ok(guard) => guard.clone(),
+ Err(e) => return format!(r#"{{"ok":false,"error":"Lock error: {}"}}"#, e),
+ }
+ } else {
+ project_path_from_body
+ };
+
+ if project_path.is_empty() {
+ return r#"{"ok":false,"error":"projectPath is required (set via POST /project or include in request body)"}"#
+ .to_string();
+ }
+
+ if content.is_empty() {
+ return r#"{"ok":false,"error":"content is required"}"#.to_string();
+ }
+
+ let date = chrono::Local::now().format("%Y-%m-%d").to_string();
+ let date_compact = chrono::Local::now().format("%Y%m%d").to_string();
+
+ // Generate slug from title
+ let slug_raw: String = title
+ .chars()
+ .map(|c| {
+ if c.is_alphanumeric() || c == ' ' || c == '-' {
+ c
+ } else {
+ ' '
+ }
+ })
+ .collect::<String>()
+ .split_whitespace()
+ .collect::<Vec<_>>()
+ .join("-")
+ .to_lowercase();
+ let slug = if slug_raw.len() > 50 {
+ &slug_raw[..50]
+ } else {
+ &slug_raw
+ };
+
+ let base_name = format!("{}-{}", slug, date_compact);
+ let dir = format!("{}/raw/sources", project_path);
+
+ // Ensure directory exists
+ if let Err(e) = std::fs::create_dir_all(&dir) {
+ return format!(
+ r#"{{"ok":false,"error":"Failed to create directory: {}"}}"#,
+ e
+ );
+ }
+
+ // Find unique filename
+ let mut file_path = format!("{}/{}.md", dir, base_name);
+ let mut counter = 2u32;
+ while std::path::Path::new(&file_path).exists() {
+ file_path = format!("{}/{}-{}.md", dir, base_name, counter);
+ counter += 1;
+ }
+
+ // Build markdown content
+ let markdown = format!(
+ "---\ntype: clip\ntitle: \"{}\"\nurl: \"{}\"\nclipped: {}\nsources: []\ntags: [web-clip]\n---\n\n# {}\n\n{}\n",
+ title.replace('"', r#"\""#),
+ url.replace('"', r#"\""#),
+ date,
+ title,
+ content,
+ );
+
+ if let Err(e) = std::fs::write(&file_path, &markdown) {
+ return format!(
+ r#"{{"ok":false,"error":"Failed to write file: {}"}}"#,
+ e
+ );
+ }
+
+ let relative_path = file_path.replace(&format!("{}/", project_path), "");
+ format!(r#"{{"ok":true,"path":"{}"}}"#, relative_path)
+}
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index a8d8eb53..a1113063 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -1,8 +1,11 @@
+mod clip_server;
mod commands;
mod types;
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
+ clip_server::start_clip_server();
+
tauri::Builder::default()
.plugin(tauri_plugin_opener::init())
.plugin(tauri_plugin_dialog::init())
diff --git a/src/App.tsx b/src/App.tsx
index d4d9ff11..afdf9c20 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -66,6 +66,12 @@ function App() {
setSelectedFile(null)
setActiveView("wiki")
await saveLastProject(proj)
+ // Notify local clip server of the current project path (fire-and-forget)
+ fetch("http://127.0.0.1:19827/project", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ path: proj.path }),
+ }).catch(() => {})
try {
const tree = await listDirectory(proj.path)
setFileTree(tree)