feat: add localization, daily templates and safer multimodal AI

Prepare unreleased 1.3.8 features while retaining version 1.3.7.

Add nine interface languages, themed selectors and a community translation workflow. Add vault-scoped daily-note templates with sync and backup support. Add provider model discovery, opt-in image input and validated streaming completion with sanitized errors.

Validation: 419 frontend tests, 311 Rust tests, Svelte/TypeScript checks, formatting, Clippy, production frontend build and Android ARM64 Rust compile check. Real-provider and rebuilt native-app verification remain pending.
This commit is contained in:
Yuri Karamian committed 2026-11-13 22:56:10 +04:00
1 parent 0007726f02
commit 622cb1f397
81 files changed
+15984 -1469

No files matched your search

+1
View File
@@ -1780,6 +1780,7 @@ version = "1.3.7"
dependencies = [
"aho-corasick",
"arboard",
"base64 0.22.1",
"chrono",
"dirs",
"fs4",
+3 -2
View File
@@ -51,12 +51,14 @@ rayon = "1"
quick-xml = "0.41"
urlencoding = "2"
sha2 = "0.10"
base64 = "0.22"
image = { version = "0.25", default-features = false, features = ["png", "jpeg", "webp", "gif"] }
fs4 = { version = "0.8", default-features = false }
rustls = { version = "0.23", default-features = false, features = ["ring", "logging", "std", "tls12"] }
[target.'cfg(target_os = "macos")'.dependencies]
objc2-app-kit = "0.3.2"
[target.'cfg(target_os = "android")'.dependencies]
[target.'cfg(unix)'.dependencies]
libc = "0.2"
[target.'cfg(target_os = "ios")'.dependencies]
@@ -68,4 +70,3 @@ tauri-plugin-single-instance = "2"
tauri-plugin-window-state = "2"
arboard = { version = "3", features = ["image-data", "wayland-data-control"] }
png = "0.17"
image = { version = "0.25", default-features = false, features = ["png", "jpeg", "webp", "gif"] }
+158 -212
View File
@@ -3,11 +3,72 @@ use serde_json::json;
use tauri::{AppHandle, Emitter};
use crate::types::AiStreamEvent;
pub mod images;
pub mod models;
use images::AiImage;
mod sse;
mod stream;
async fn receive_stream(
app: &AppHandle,
response: reqwest::Response,
request_id: &str,
protocol: stream::Protocol,
) -> Result<(), String> {
stream::receive(response, protocol, |text| {
let _ = app.emit(
"ai-stream",
AiStreamEvent {
request_id: request_id.to_owned(),
event_type: "text".into(),
text: Some(text),
error: None,
},
);
})
.await?;
// This is the only success event; errors are emitted once by ai_request.
let _ = app.emit(
"ai-stream",
AiStreamEvent {
request_id: request_id.to_owned(),
event_type: "done".into(),
text: None,
error: None,
},
);
Ok(())
}
const ANTHROPIC_API_URL: &str = "https://api.anthropic.com/v1/messages";
const OPENAI_API_URL: &str = "https://api.openai.com/v1/chat/completions";
const OLLAMA_DEFAULT_URL: &str = "http://localhost:11434";
fn request_client() -> Result<Client, String> {
Client::builder()
.redirect(reqwest::redirect::Policy::none())
.connect_timeout(std::time::Duration::from_secs(10))
.timeout(std::time::Duration::from_secs(180))
.build()
.map_err(|_| "Could not initialize AI connection".into())
}
fn connection_error(_: reqwest::Error) -> String {
// reqwest errors may contain configured URLs, including credentials or query secrets.
"AI connection failed. Check the connection and configured server.".into()
}
fn http_error(status: reqwest::StatusCode) -> String {
let hint = match status.as_u16() {
401 | 403 => "Check the API key and account permissions.",
429 => "Check rate limits and account quota.",
400 | 404 => "Check the model ID and its support for this request, including images.",
_ => "The provider or configured server could not complete the request.",
};
// Error bodies can echo credentials or image data. Do not broadcast them.
format!("AI API returned HTTP {}. {hint}", status.as_u16())
}
#[allow(clippy::too_many_arguments)]
pub fn ai_request(
app: AppHandle,
@@ -18,6 +79,7 @@ pub fn ai_request(
user_message: String,
request_id: String,
base_url: Option<String>,
images: Vec<AiImage>,
) {
std::thread::spawn(move || {
let rt = tokio::runtime::Runtime::new().unwrap();
@@ -38,6 +100,7 @@ pub fn ai_request(
&system_prompt,
&user_message,
&request_id,
&images,
)
.await
}
@@ -52,6 +115,7 @@ pub fn ai_request(
&system_prompt,
&user_message,
&request_id,
&images,
)
.await
}
@@ -69,6 +133,7 @@ pub fn ai_request(
&system_prompt,
&user_message,
&request_id,
&images,
)
.await
}
@@ -81,6 +146,7 @@ pub fn ai_request(
&system_prompt,
&user_message,
&request_id,
&images,
)
.await
}
@@ -118,7 +184,14 @@ fn openai_request_body(
max_tokens: u32,
) -> serde_json::Value {
// GPT-5/6 reject legacy output limits and sampling with their default reasoning.
let reasoning = model.starts_with("gpt-5") || model.starts_with("gpt-6");
let family = model.strip_prefix("ft:").unwrap_or(model);
let reasoning = family
.strip_prefix("gpt-")
.and_then(|s| s.split(['.', '-']).next()?.parse::<u32>().ok())
.is_some_and(|version| version >= 5)
|| family
.strip_prefix('o')
.is_some_and(|s| s.starts_with(|c: char| c.is_ascii_digit()));
let token_key = if reasoning {
"max_completion_tokens"
} else {
@@ -137,6 +210,22 @@ fn openai_request_body(
body
}
fn user_content(provider: &str, text: &str, images: &[AiImage]) -> serde_json::Value {
if images.is_empty() {
return json!(text);
}
let mut blocks = vec![json!({"type":"text","text":text})];
for image in images {
if provider == "anthropic" {
blocks.push(json!({"type":"image","source":{"type":"base64","media_type":image.media_type,"data":image.data}}));
} else {
blocks.push(json!({"type":"image_url","image_url":{"url":format!("data:{};base64,{}",image.media_type,image.data)}}));
}
}
json!(blocks)
}
#[allow(clippy::too_many_arguments)]
async fn stream_anthropic(
app: &AppHandle,
api_key: &str,
@@ -144,8 +233,9 @@ async fn stream_anthropic(
system_prompt: &str,
user_message: &str,
request_id: &str,
images: &[AiImage],
) -> Result<(), String> {
let client = Client::new();
let client = request_client()?;
let body = json!({
"model": model,
@@ -155,7 +245,7 @@ async fn stream_anthropic(
"messages": [
{
"role": "user",
"content": user_message
"content": user_content("anthropic", user_message, images)
}
]
});
@@ -168,108 +258,16 @@ async fn stream_anthropic(
.json(&body)
.send()
.await
.map_err(|e| format!("Request failed: {}", e))?;
.map_err(connection_error)?;
if !response.status().is_success() {
let status = response.status();
let body_text = response.text().await.unwrap_or_default();
return Err(format!("API error {}: {}", status, body_text));
return Err(http_error(response.status()));
}
// Parse SSE stream
use futures::StreamExt;
let mut stream = response.bytes_stream();
let mut buffer = String::new();
while let Some(chunk) = stream.next().await {
let chunk = chunk.map_err(|e| format!("Stream error: {}", e))?;
buffer.push_str(&String::from_utf8_lossy(&chunk));
// Process complete SSE events from buffer
while let Some(event_end) = buffer.find("\n\n") {
let event_str = buffer[..event_end].to_string();
buffer = buffer[event_end + 2..].to_string();
for line in event_str.lines() {
if let Some(data) = line.strip_prefix("data: ") {
if data == "[DONE]" {
let _ = app.emit(
"ai-stream",
AiStreamEvent {
request_id: request_id.to_owned(),
event_type: "done".to_string(),
text: None,
error: None,
},
);
return Ok(());
}
if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(data) {
let event_type = parsed["type"].as_str().unwrap_or("");
match event_type {
"content_block_delta" => {
if let Some(text) = parsed["delta"]["text"].as_str() {
let _ = app.emit(
"ai-stream",
AiStreamEvent {
request_id: request_id.to_owned(),
event_type: "text".to_string(),
text: Some(text.to_string()),
error: None,
},
);
}
}
"message_stop" => {
let _ = app.emit(
"ai-stream",
AiStreamEvent {
request_id: request_id.to_owned(),
event_type: "done".to_string(),
text: None,
error: None,
},
);
return Ok(());
}
"error" => {
let msg = parsed["error"]["message"]
.as_str()
.unwrap_or("Unknown API error");
let _ = app.emit(
"ai-stream",
AiStreamEvent {
request_id: request_id.to_owned(),
event_type: "error".to_string(),
text: None,
error: Some(msg.to_string()),
},
);
return Err(msg.to_string());
}
_ => {}
}
}
}
}
}
}
let _ = app.emit(
"ai-stream",
AiStreamEvent {
request_id: request_id.to_owned(),
event_type: "done".to_string(),
text: None,
error: None,
},
);
Ok(())
receive_stream(app, response, request_id, stream::Protocol::Anthropic).await
}
#[allow(clippy::too_many_arguments)]
async fn stream_openai(
app: &AppHandle,
url: &str,
@@ -278,10 +276,12 @@ async fn stream_openai(
system_prompt: &str,
user_message: &str,
request_id: &str,
images: &[AiImage],
) -> Result<(), String> {
let client = Client::new();
let client = request_client()?;
let body = openai_request_body(model, Some(system_prompt), user_message, true, 4096);
let mut body = openai_request_body(model, Some(system_prompt), user_message, true, 4096);
body["messages"][1]["content"] = user_content("openai", user_message, images);
let mut req = client.post(url).header("content-type", "application/json");
@@ -289,107 +289,13 @@ async fn stream_openai(
req = req.header("Authorization", format!("Bearer {}", key));
}
let response = req
.json(&body)
.send()
.await
.map_err(|e| format!("Request failed: {}", e))?;
let response = req.json(&body).send().await.map_err(connection_error)?;
if !response.status().is_success() {
let status = response.status();
let body_text = response.text().await.unwrap_or_default();
return Err(format!("API error {}: {}", status, body_text));
return Err(http_error(response.status()));
}
use futures::StreamExt;
let mut stream = response.bytes_stream();
let mut buffer = String::new();
while let Some(chunk) = stream.next().await {
let chunk = chunk.map_err(|e| format!("Stream error: {}", e))?;
buffer.push_str(&String::from_utf8_lossy(&chunk));
while let Some(event_end) = buffer.find("\n\n") {
let event_str = buffer[..event_end].to_string();
buffer = buffer[event_end + 2..].to_string();
for line in event_str.lines() {
if let Some(data) = line.strip_prefix("data: ") {
if data == "[DONE]" {
let _ = app.emit(
"ai-stream",
AiStreamEvent {
request_id: request_id.to_owned(),
event_type: "done".to_string(),
text: None,
error: None,
},
);
return Ok(());
}
if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(data) {
// OpenAI streaming: choices[0].delta.content
if let Some(content) = parsed["choices"][0]["delta"]["content"].as_str() {
if !content.is_empty() {
let _ = app.emit(
"ai-stream",
AiStreamEvent {
request_id: request_id.to_owned(),
event_type: "text".to_string(),
text: Some(content.to_string()),
error: None,
},
);
}
}
// Check finish_reason
if let Some(reason) = parsed["choices"][0]["finish_reason"].as_str() {
if reason == "stop" || reason == "length" {
let _ = app.emit(
"ai-stream",
AiStreamEvent {
request_id: request_id.to_owned(),
event_type: "done".to_string(),
text: None,
error: None,
},
);
return Ok(());
}
}
// Check for error in stream
if let Some(err) = parsed["error"]["message"].as_str() {
let _ = app.emit(
"ai-stream",
AiStreamEvent {
request_id: request_id.to_owned(),
event_type: "error".to_string(),
text: None,
error: Some(err.to_string()),
},
);
return Err(err.to_string());
}
}
}
}
}
}
let _ = app.emit(
"ai-stream",
AiStreamEvent {
request_id: request_id.to_owned(),
event_type: "done".to_string(),
text: None,
error: None,
},
);
Ok(())
receive_stream(app, response, request_id, stream::Protocol::OpenAi).await
}
pub async fn test_connection(
@@ -423,7 +329,7 @@ pub async fn test_connection(
}
async fn test_anthropic(api_key: &str, model: &str) -> Result<String, String> {
let client = Client::new();
let client = request_client()?;
let body = json!({
"model": model,
@@ -444,19 +350,17 @@ async fn test_anthropic(api_key: &str, model: &str) -> Result<String, String> {
.json(&body)
.send()
.await
.map_err(|e| format!("Connection failed: {}", e))?;
.map_err(connection_error)?;
if response.status().is_success() {
Ok("Connection successful".to_string())
} else {
let status = response.status();
let body_text = response.text().await.unwrap_or_default();
Err(format!("API error {}: {}", status, body_text))
Err(http_error(response.status()))
}
}
async fn test_openai(url: &str, api_key: Option<&str>, model: &str) -> Result<String, String> {
let client = Client::new();
let client = request_client()?;
let body = openai_request_body(model, None, "Hi", false, 20);
let mut req = client.post(url).header("content-type", "application/json");
@@ -465,18 +369,12 @@ async fn test_openai(url: &str, api_key: Option<&str>, model: &str) -> Result<St
req = req.header("Authorization", format!("Bearer {}", key));
}
let response = req
.json(&body)
.send()
.await
.map_err(|e| format!("Connection failed: {}", e))?;
let response = req.json(&body).send().await.map_err(connection_error)?;
if response.status().is_success() {
Ok("Connection successful".to_string())
} else {
let status = response.status();
let body_text = response.text().await.unwrap_or_default();
Err(format!("API error {}: {}", status, body_text))
Err(http_error(response.status()))
}
}
@@ -484,6 +382,54 @@ async fn test_openai(url: &str, api_key: Option<&str>, model: &str) -> Result<St
mod request_tests {
use super::*;
#[test]
fn vision_blocks_are_provider_specific_and_text_only_stays_a_string() {
assert_eq!(user_content("openai", "Text only", &[]), json!("Text only"));
let images = vec![AiImage {
media_type: "image/png",
data: "PIXELS".into(),
}];
let openai = user_content("openai", "Describe", &images);
assert_eq!(
openai[1]["image_url"]["url"],
"data:image/png;base64,PIXELS"
);
let anthropic = user_content("anthropic", "Describe", &images);
assert_eq!(anthropic[1]["source"]["type"], "base64");
assert_eq!(anthropic[1]["source"]["media_type"], "image/png");
assert_eq!(anthropic[1]["source"]["data"], "PIXELS");
}
#[test]
fn connection_and_http_errors_never_include_urls_or_provider_payloads() {
let url =
reqwest::Url::parse("https://user:private-password@example.invalid/?key=private-key")
.unwrap();
let error = Client::new()
.get("not a URL")
.build()
.unwrap_err()
.with_url(url);
assert!(error.to_string().contains("private-key"));
let safe = connection_error(error);
assert!(!safe.contains("private"));
assert!(!safe.contains("example.invalid"));
for code in [400, 401, 403, 404, 429, 500] {
let safe = http_error(reqwest::StatusCode::from_u16(code).unwrap());
assert!(safe.contains(&code.to_string()));
assert!(safe.len() < 256);
}
}
#[test]
fn future_gpt_and_reasoning_ids_use_modern_output_limits() {
for model in ["gpt-99-next", "o3", "o12-mini"] {
let body = openai_request_body(model, None, "Hi", true, 4096);
assert!(body.get("max_completion_tokens").is_some());
assert!(body.get("temperature").is_none());
}
}
#[test]
fn gpt6_streaming_requests_use_supported_parameters() {
let body = openai_request_body(
+327
View File
@@ -0,0 +1,327 @@
//! Explicitly selected local images. No URL fetching, data-URI passthrough or metadata uploads.
use base64::{engine::general_purpose::STANDARD, Engine};
use image::{ImageDecoder, ImageFormat, ImageReader};
use std::{
fs,
io::{Cursor, Read},
path::{Component, Path},
};
pub const MAX_IMAGES: usize = 4;
const MAX_INPUT: u64 = 10 * 1024 * 1024;
const MAX_OUTPUT: usize = 4 * 1024 * 1024;
const MAX_PIXELS: u64 = 20_000_000;
const MAX_SIDE: u32 = 1568;
pub struct AiImage {
pub media_type: &'static str,
pub data: String,
}
fn check_lexical_scope(root: &Path, configured: &Path, requested: &Path) -> Result<(), String> {
if !requested.is_absolute() {
return Err("AI context paths must be absolute local paths".into());
}
#[cfg(windows)]
let (root, configured, requested) = (
std::path::PathBuf::from(root.to_string_lossy().to_lowercase()),
std::path::PathBuf::from(configured.to_string_lossy().to_lowercase()),
std::path::PathBuf::from(requested.to_string_lossy().to_lowercase()),
);
#[cfg(windows)]
let (root, configured, requested) = (root.as_path(), configured.as_path(), requested.as_path());
let relative = requested
.strip_prefix(root)
.or_else(|_| requested.strip_prefix(configured))
.map_err(|_| "AI context paths must stay inside the configured vault")?;
if relative
.components()
.any(|part| matches!(part, Component::ParentDir))
{
return Err("AI context paths must not traverse outside their approved scope".into());
}
Ok(())
}
fn allowed_target(
vault: &Path,
configured: &Path,
requested: &Path,
) -> Result<std::path::PathBuf, String> {
// Reject other roots (including UNC shares) before filesystem resolution.
check_lexical_scope(vault, configured, requested)?;
let target = fs::canonicalize(requested).map_err(|_| "AI image could not be resolved")?;
let relative = target
.strip_prefix(vault)
.map_err(|_| "AI images must stay inside the note's configured vault")?;
if relative.starts_with(".helixnotes/attachments") {
for folder in [
vault.join(".helixnotes"),
vault.join(".helixnotes/attachments"),
] {
if !fs::symlink_metadata(folder).is_ok_and(|meta| meta.file_type().is_dir()) {
return Err("AI attachments must not use linked metadata directories".into());
}
}
} else if relative.components().any(|part| match part {
Component::Normal(name) => name.to_string_lossy().starts_with('.'),
_ => true,
}) {
return Err("Private vault metadata cannot be sent to AI".into());
}
if !target.is_file() {
return Err("AI image must be a regular file".into());
}
Ok(target)
}
fn encode(bytes: &[u8]) -> Result<AiImage, String> {
let format =
image::guess_format(bytes).map_err(|_| "AI attachment is not a supported image")?;
if !matches!(
format,
ImageFormat::Png | ImageFormat::Jpeg | ImageFormat::WebP
) {
return Err("AI images must be PNG, JPEG or WebP".into());
}
let mut limits = image::Limits::default();
limits.max_alloc = Some(96 * 1024 * 1024);
let mut header = ImageReader::with_format(Cursor::new(bytes), format);
header.limits(limits.clone());
let (width, height) = header
.into_dimensions()
.map_err(|_| "Could not read AI image dimensions within the memory limit")?;
if width == 0 || height == 0 || u64::from(width) * u64::from(height) > MAX_PIXELS {
return Err("AI image exceeds the 20 megapixel limit".into());
}
let mut reader = ImageReader::with_format(Cursor::new(bytes), format);
reader.limits(limits);
let mut decoder = reader
.into_decoder()
.map_err(|_| "Could not decode AI image within the memory limit")?;
let orientation = decoder
.orientation()
.map_err(|_| "Could not read AI image orientation")?;
let mut decoded = image::DynamicImage::from_decoder(decoder)
.map_err(|_| "Could not decode AI image within the memory limit")?;
decoded.apply_orientation(orientation);
let resized = if width > MAX_SIDE || height > MAX_SIDE {
decoded.thumbnail(MAX_SIDE, MAX_SIDE)
} else {
decoded
};
let mut encoded = Cursor::new(Vec::new());
resized
.write_to(&mut encoded, ImageFormat::Png)
.map_err(|_| "Could not encode AI image")?;
let mut data = encoded.into_inner();
let mut media_type = "image/png";
if data.len() > MAX_OUTPUT {
data.clear();
image::codecs::jpeg::JpegEncoder::new_with_quality(&mut data, 85)
.encode_image(&image::DynamicImage::ImageRgb8(resized.to_rgb8()))
.map_err(|_| "Could not encode AI image")?;
media_type = "image/jpeg";
}
if data.len() > MAX_OUTPUT {
return Err("AI image exceeds the encoded size limit".into());
}
Ok(AiImage {
media_type,
data: STANDARD.encode(data),
})
}
pub fn prepare(vault: &str, note: &str, paths: &[String]) -> Result<Vec<AiImage>, String> {
if paths.len() > MAX_IMAGES {
return Err("Select at most four AI images".into());
}
let root = fs::canonicalize(vault).map_err(|_| "Invalid AI note vault")?;
check_lexical_scope(&root, Path::new(vault), Path::new(note))?;
let note = fs::canonicalize(note).map_err(|_| "Save the note before including AI images")?;
if !note.starts_with(&root)
|| note.starts_with(root.join(".helixnotes"))
|| !note.is_file()
|| note.extension().and_then(|s| s.to_str()) != Some("md")
{
return Err("AI image context must be a note in the configured vault".into());
}
let mut seen = std::collections::HashSet::new();
let mut result = Vec::new();
for requested in paths {
let target = allowed_target(&root, Path::new(vault), Path::new(requested))?;
if !seen.insert(target.clone()) {
continue;
}
let mut options = fs::OpenOptions::new();
options.read(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
options.custom_flags(libc::O_NOFOLLOW);
}
let file = options
.open(target)
.map_err(|_| "Could not open AI image")?;
if !file
.metadata()
.is_ok_and(|meta| meta.is_file() && meta.len() <= MAX_INPUT)
{
return Err("AI image exceeds the 10 MiB source limit or is not a file".into());
}
let mut bytes = Vec::new();
file.take(MAX_INPUT + 1)
.read_to_end(&mut bytes)
.map_err(|_| "Could not read AI image")?;
if bytes.len() as u64 > MAX_INPUT {
return Err("AI image exceeds the 10 MiB source limit".into());
}
result.push(encode(&bytes)?);
}
Ok(result)
}
#[cfg(test)]
mod tests {
use super::*;
fn fixture() -> (tempfile::TempDir, String, String) {
let root = tempfile::tempdir().unwrap();
let note = root.path().join("Note.md");
fs::write(&note, "Note").unwrap();
let image = root.path().join("screen.png");
image::RgbaImage::from_pixel(32, 16, image::Rgba([10, 20, 30, 255]))
.save(&image)
.unwrap();
(
root,
note.to_string_lossy().into_owned(),
image.to_string_lossy().into_owned(),
)
}
#[test]
fn prepares_pixels_not_file_paths_and_deduplicates_images() {
let (root, note, path) = fixture();
let result = prepare(root.path().to_str().unwrap(), &note, &[path.clone(), path]).unwrap();
assert_eq!(result.len(), 1);
assert_eq!(result[0].media_type, "image/png");
let bytes = STANDARD.decode(&result[0].data).unwrap();
let decoded = image::load_from_memory(&bytes).unwrap();
assert_eq!((decoded.width(), decoded.height()), (32, 16));
}
#[test]
fn rejects_external_files_urls_data_uris_and_private_metadata() {
let (root, note, path) = fixture();
let outside = tempfile::tempdir().unwrap();
let outside_file = outside.path().join("screen.png");
fs::copy(&path, &outside_file).unwrap();
let hidden = root.path().join(".helixnotes/private.png");
fs::create_dir_all(hidden.parent().unwrap()).unwrap();
fs::copy(&path, &hidden).unwrap();
for p in [
outside_file.to_string_lossy().into_owned(),
hidden.to_string_lossy().into_owned(),
"https://example.com/a.png".into(),
"data:image/png;base64,abcd".into(),
] {
assert!(prepare(root.path().to_str().unwrap(), &note, &[p]).is_err());
}
}
#[test]
fn allows_managed_attachments_but_rejects_missing_context_and_excess_images() {
let (root, note, path) = fixture();
let managed = root.path().join(".helixnotes/attachments/screen.png");
fs::create_dir_all(managed.parent().unwrap()).unwrap();
fs::copy(&path, &managed).unwrap();
assert!(prepare(
root.path().to_str().unwrap(),
&note,
&[managed.to_string_lossy().into_owned()]
)
.is_ok());
assert!(prepare(
root.path().to_str().unwrap(),
"missing.md",
std::slice::from_ref(&path)
)
.is_err());
assert!(prepare(root.path().to_str().unwrap(), &note, &vec![path; 5]).is_err());
}
#[test]
fn rejects_other_roots_and_traversal_before_probing_files() {
let (root, note, _) = fixture();
let missing_outside = root
.path()
.parent()
.unwrap()
.join("missing-outside/image.png");
let error = prepare(
root.path().to_str().unwrap(),
&note,
&[missing_outside.to_string_lossy().into_owned()],
)
.err()
.unwrap();
assert!(error.contains("inside the configured vault"));
let traversal = root.path().join("../missing-outside/image.png");
let error = prepare(
root.path().to_str().unwrap(),
&note,
&[traversal.to_string_lossy().into_owned()],
)
.err()
.unwrap();
assert!(error.contains("must not traverse"));
}
#[test]
fn rejects_oversized_and_disguised_files() {
let (root, note, path) = fixture();
fs::write(&path, b"not image bytes").unwrap();
assert!(prepare(
root.path().to_str().unwrap(),
&note,
std::slice::from_ref(&path)
)
.is_err());
fs::File::create(&path)
.unwrap()
.set_len(MAX_INPUT + 1)
.unwrap();
assert!(prepare(root.path().to_str().unwrap(), &note, &[path]).is_err());
}
#[test]
fn downsizes_large_images_without_modifying_source_files() {
let (root, note, path) = fixture();
image::RgbaImage::from_pixel(2000, 1000, image::Rgba([10, 20, 30, 255]))
.save(&path)
.unwrap();
let original = fs::read(&path).unwrap();
let result = prepare(
root.path().to_str().unwrap(),
&note,
std::slice::from_ref(&path),
)
.unwrap();
let decoded = image::load_from_memory(&STANDARD.decode(&result[0].data).unwrap()).unwrap();
assert_eq!((decoded.width(), decoded.height()), (1568, 784));
assert_eq!(fs::read(path).unwrap(), original);
}
#[cfg(unix)]
#[test]
fn symlinks_cannot_send_images_from_outside_the_vault() {
use std::os::unix::fs::symlink;
let (root, note, _) = fixture();
let outside = tempfile::tempdir().unwrap();
let target = outside.path().join("image.png");
fs::write(&target, "secret").unwrap();
let link = root.path().join("link.png");
symlink(target, &link).unwrap();
assert!(prepare(
root.path().to_str().unwrap(),
&note,
&[link.to_string_lossy().into_owned()]
)
.is_err());
}
}
+264
View File
@@ -0,0 +1,264 @@
//! Provider catalogs, fetched without sending prompts or note data.
use futures::StreamExt;
use reqwest::{redirect::Policy, Client};
use serde::Serialize;
use serde_json::Value;
use std::{
collections::{BTreeMap, HashSet},
time::Duration,
};
const MAX_RESPONSE: usize = 1024 * 1024;
const MAX_MODELS: usize = 2000;
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct AiModel {
pub id: String,
pub name: String,
/// None means the provider did not advertise the capability, not lack of support.
pub vision: Option<bool>,
}
fn chat_candidate(id: &str) -> bool {
let id = id.strip_prefix("ft:").unwrap_or(id);
let family = id.starts_with("gpt-")
|| id.starts_with("chatgpt-")
|| id
.strip_prefix('o')
.is_some_and(|s| s.starts_with(|c: char| c.is_ascii_digit()));
family
&& ![
"realtime",
"audio",
"transcribe",
"tts",
"embedding",
"image",
"codex",
"search",
"moderation",
]
.iter()
.any(|kind| id.contains(kind))
}
fn parse_models(provider: &str, page: &Value) -> Result<Vec<AiModel>, String> {
let data = page["data"]
.as_array()
.ok_or("Invalid model-list response")?;
if data.len() > MAX_MODELS {
return Err("Provider model list exceeds the limit".into());
}
let mut result = Vec::new();
for item in data {
let Some(id) = item["id"]
.as_str()
.filter(|id| !id.is_empty() && id.len() <= 200 && !id.chars().any(char::is_control))
else {
continue;
};
if item["lifecycle"] == "retired" || (provider == "openai" && !chat_candidate(id)) {
continue;
}
let name = item["display_name"]
.as_str()
.filter(|s| !s.is_empty() && s.len() <= 200 && !s.chars().any(char::is_control))
.unwrap_or(id);
result.push(AiModel {
id: id.into(),
name: name.into(),
vision: item["capabilities"]["image_input"]["supported"].as_bool(),
});
}
Ok(result)
}
pub async fn fetch_models(provider: &str, key: &str) -> Result<Vec<AiModel>, String> {
let url = match provider {
"openai" => "https://api.openai.com/v1/models",
"anthropic" => "https://api.anthropic.com/v1/models",
_ => return Err("Model discovery supports OpenAI and Anthropic".into()),
};
let client = Client::builder()
.redirect(Policy::none())
.connect_timeout(Duration::from_secs(5))
.timeout(Duration::from_secs(20))
.build()
.map_err(|_| "Could not initialize model discovery")?;
fetch_at(&client, provider, key.trim(), url).await
}
async fn fetch_at(
client: &Client,
provider: &str,
key: &str,
url: &str,
) -> Result<Vec<AiModel>, String> {
if key.is_empty() {
return Err("Enter an API key before fetching models".into());
}
if key.len() > 8192 {
return Err("API key exceeds the length limit".into());
}
let mut cursor: Option<String> = None;
let mut cursors = HashSet::new();
let mut models = BTreeMap::new();
for _ in 0..20 {
let mut request = client.get(url);
if provider == "anthropic" {
request = request
.header("x-api-key", key)
.header("anthropic-version", "2023-06-01")
.query(&[("limit", "100")]);
if let Some(after) = &cursor {
request = request.query(&[("after_id", after)]);
}
} else {
request = request.bearer_auth(key);
}
// Never return provider bodies or reqwest's debug representation: invalid-key
// responses can echo credentials. Fixed endpoints and no redirects keep keys scoped.
let response = request
.send()
.await
.map_err(|_| "Could not reach the provider model catalog")?;
if !response.status().is_success() {
return Err(format!(
"Model catalog returned HTTP {}. Check the API key and account permissions.",
response.status().as_u16()
));
}
let mut bytes = Vec::new();
let mut stream = response.bytes_stream();
while let Some(chunk) = stream.next().await {
let chunk = chunk.map_err(|_| "Could not read the provider model catalog")?;
if bytes.len() + chunk.len() > MAX_RESPONSE {
return Err("Provider model list exceeds the size limit".into());
}
bytes.extend_from_slice(&chunk);
}
let page: Value =
serde_json::from_slice(&bytes).map_err(|_| "Invalid model-list response")?;
for model in parse_models(provider, &page)? {
models.insert(model.id.clone(), model);
}
if models.len() > MAX_MODELS {
return Err("Provider model list exceeds the limit".into());
}
if provider != "anthropic" || page["has_more"] != true {
let mut result: Vec<_> = models.into_values().collect();
result.sort_by_cached_key(|m| (m.name.to_lowercase(), m.id.clone()));
return Ok(result);
}
let next = page["last_id"]
.as_str()
.filter(|id| !id.is_empty() && id.len() <= 200)
.ok_or("Provider pagination cursor is missing")?
.to_string();
if !cursors.insert(next.clone()) {
return Err("Provider repeated its pagination cursor".into());
}
cursor = Some(next);
}
Err("Provider model catalog has too many pages".into())
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
use std::io::{Read, Write};
#[test]
fn openai_filters_non_chat_families_without_freezing_model_versions() {
let result = parse_models("openai", &json!({"data":[
{"id":"gpt-99-next"},{"id":"o12"},{"id":"ft:gpt-4o:org:custom"},
{"id":"gpt-image-1"},{"id":"gpt-4o-realtime-preview"},{"id":"text-embedding-3-small"},
{"id":"gpt-5-codex"},{"id":"gpt-audio"},{"id":"whisper-1"}
]})).unwrap();
assert_eq!(
result.iter().map(|m| m.id.as_str()).collect::<Vec<_>>(),
["gpt-99-next", "o12", "ft:gpt-4o:org:custom"]
);
assert!(result.iter().all(|m| m.vision.is_none()));
}
#[test]
fn anthropic_preserves_reported_capability_and_excludes_retired_models() {
let result = parse_models("anthropic", &json!({"data":[
{"id":"claude-future","display_name":"Future Claude","capabilities":{"image_input":{"supported":true}}},
{"id":"claude-old","lifecycle":"retired"},{"id":"claude-legacy"}
]})).unwrap();
assert_eq!(result.len(), 2);
assert_eq!(result[0].vision, Some(true));
assert_eq!(result[1].vision, None);
assert!(parse_models("openai", &json!({"unexpected":[]})).is_err());
}
#[tokio::test]
async fn paginates_with_scoped_headers_and_deduplicates_ids() {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let url = format!("http://{}/models", listener.local_addr().unwrap());
let server = std::thread::spawn(move || {
for (i, body) in [
r#"{"data":[{"id":"claude-a"}],"has_more":true,"last_id":"claude-a"}"#,
r#"{"data":[{"id":"claude-a"},{"id":"claude-b"}],"has_more":false}"#,
]
.into_iter()
.enumerate()
{
let (mut socket, _) = listener.accept().unwrap();
let mut bytes = [0; 8192];
let count = socket.read(&mut bytes).unwrap();
let request = String::from_utf8_lossy(&bytes[..count]);
assert!(request.contains("x-api-key: fake-key"));
assert!(!request.contains("authorization:"));
if i == 1 {
assert!(request.contains("after_id=claude-a"));
}
write!(
socket,
"HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
body.len(),
body
)
.unwrap();
}
});
let result = fetch_at(&Client::new(), "anthropic", "fake-key", &url)
.await
.unwrap();
assert_eq!(result.len(), 2);
server.join().unwrap();
}
#[tokio::test]
async fn authentication_failures_do_not_echo_keys() {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let url = format!("http://{}/models", listener.local_addr().unwrap());
let server = std::thread::spawn(move || {
let (mut socket, _) = listener.accept().unwrap();
let mut bytes = [0; 4096];
let _ = socket.read(&mut bytes);
let body = "secret-key-should-not-appear";
write!(
socket,
"HTTP/1.1 401 Unauthorized\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
body.len(),
body
)
.unwrap();
});
let error = fetch_at(
&Client::builder().redirect(Policy::none()).build().unwrap(),
"openai",
"secret-key-should-not-appear",
&url,
)
.await
.unwrap_err();
assert!(error.contains("401"));
assert!(!error.contains("secret-key"));
server.join().unwrap();
}
}
+74
View File
@@ -0,0 +1,74 @@
//! Decode UTF-8 after a complete SSE event, never at arbitrary network chunk boundaries.
#[derive(Default)]
pub struct Events {
bytes: Vec<u8>,
output: usize,
}
impl Events {
pub fn record_text(&mut self, text: &str) -> Result<(), String> {
self.output += text.len();
if self.output > 2 * 1024 * 1024 {
return Err("AI response exceeds the text limit".into());
}
Ok(())
}
pub fn push(&mut self, chunk: &[u8]) -> Result<Vec<String>, String> {
if self.bytes.len() + chunk.len() > 8 * 1024 * 1024 {
return Err("AI stream buffer exceeds the size limit".into());
}
self.bytes.extend_from_slice(chunk);
let mut result = Vec::new();
loop {
let lf = self
.bytes
.windows(2)
.position(|b| b == b"\n\n")
.map(|p| (p, 2));
let crlf = self
.bytes
.windows(4)
.position(|b| b == b"\r\n\r\n")
.map(|p| (p, 4));
let boundary = lf.into_iter().chain(crlf).min_by_key(|(offset, _)| *offset);
let Some((end, delimiter)) = boundary else {
break;
};
if end > 4 * 1024 * 1024 {
return Err("AI stream event exceeds the size limit".into());
}
let event = std::str::from_utf8(&self.bytes[..end])
.map_err(|_| "AI stream contains invalid UTF-8")?
.to_string();
self.bytes.drain(..end + delimiter);
result.push(event);
}
if self.bytes.len() > 4 * 1024 * 1024 {
return Err("AI stream event exceeds the size limit".into());
}
Ok(result)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_byte_boundary_preserves_unicode_and_crlf() {
let input = "data: {\"text\":\"Привет café 👋\"}\r\n\r\n";
for split in 0..input.len() {
let mut decoder = Events::default();
let mut events = decoder.push(&input.as_bytes()[..split]).unwrap();
events.extend(decoder.push(&input.as_bytes()[split..]).unwrap());
assert_eq!(events, ["data: {\"text\":\"Привет café 👋\"}"]);
}
}
#[test]
fn handles_multiple_events_and_refuses_unbounded_partial_data() {
let mut decoder = Events::default();
assert_eq!(
decoder.push(b"data: a\n\ndata: b\n\n").unwrap(),
["data: a", "data: b"]
);
assert!(decoder.push(&vec![b'x'; 4 * 1024 * 1024 + 1]).is_err());
}
}
+196
View File
@@ -0,0 +1,196 @@
//! Validate provider completion before a streamed draft becomes eligible for application.
//! All errors are app-owned strings: never forward provider payloads or transport URLs.
use futures::StreamExt;
use serde_json::Value;
use super::sse;
const INVALID: &str = "AI returned an invalid streaming response. The result cannot be applied.";
const INCOMPLETE: &str =
"AI stream ended before successful completion. The result cannot be applied.";
const PROVIDER_ERROR: &str = "The AI provider reported a streaming error. Check the model, account and request, then try again.";
const OUTPUT_LIMIT: &str = "AI response reached its output limit. Use a smaller selection.";
#[derive(Clone, Copy)]
pub(super) enum Protocol {
OpenAi,
Anthropic,
}
struct Completion {
protocol: Protocol,
stopped: bool,
complete: bool,
has_text: bool,
}
impl Completion {
fn new(protocol: Protocol) -> Self {
Self {
protocol,
stopped: false,
complete: false,
has_text: false,
}
}
fn event(&mut self, event: &str) -> Result<Option<String>, String> {
// SSE joins multiple data fields with a newline. Comments, event names, IDs
// and retry fields are not JSON payloads. A space after ':' is optional.
let fields: Vec<_> = event
.lines()
.filter_map(|line| {
line.strip_prefix("data:")
.map(|s| s.strip_prefix(' ').unwrap_or(s))
})
.collect();
if fields.is_empty() {
return Ok(None);
}
if self.complete {
return Err(INVALID.into());
}
let data = fields.join("\n");
if data == "[DONE]" {
if !matches!(self.protocol, Protocol::OpenAi) || !self.stopped || !self.has_text {
return Err(INCOMPLETE.into());
}
self.complete = true;
return Ok(None);
}
let value: Value = serde_json::from_str(&data).map_err(|_| INVALID)?;
if !value.is_object() {
return Err(INVALID.into());
}
// Do not include the error's type, message or other provider-controlled data.
if value.get("error").is_some_and(|error| !error.is_null()) || value["type"] == "error" {
return Err(PROVIDER_ERROR.into());
}
let text = match self.protocol {
Protocol::OpenAi => self.openai(&value)?,
Protocol::Anthropic => self.anthropic(&value)?,
};
if let Some(text) = &text {
self.has_text |= !text.trim().is_empty();
}
Ok(text)
}
fn openai(&mut self, value: &Value) -> Result<Option<String>, String> {
let choices = value["choices"].as_array().ok_or(INVALID)?;
// A final usage-only event is permitted, including between stop and [DONE].
if choices.is_empty() && value["usage"].is_object() {
return Ok(None);
}
if choices.len() != 1 || self.stopped {
return Err(INVALID.into());
}
let choice = &choices[0];
if choice.get("index").is_some_and(|index| index != 0) {
return Err(INVALID.into());
}
let delta = choice["delta"].as_object().ok_or(INVALID)?;
if delta.get("refusal").is_some_and(|v| !v.is_null()) {
return Err(
"The AI provider refused this request. The result cannot be applied.".into(),
);
}
if ["tool_calls", "function_call"]
.iter()
.any(|key| delta.get(*key).is_some_and(|v| !v.is_null()))
{
return Err("AI returned a tool request instead of replacement text. The result cannot be applied.".into());
}
let text = match delta.get("content") {
None | Some(Value::Null) => None,
Some(Value::String(text)) => Some(text.clone()),
_ => return Err(INVALID.into()),
};
match choice.get("finish_reason") {
None | Some(Value::Null) => {}
Some(Value::String(reason)) if reason == "stop" => self.stopped = true,
Some(Value::String(reason)) if reason == "length" => return Err(OUTPUT_LIMIT.into()),
Some(Value::String(_)) => {
return Err(
"AI response did not finish successfully. The result cannot be applied.".into(),
)
}
_ => return Err(INVALID.into()),
}
Ok(text)
}
fn anthropic(&mut self, value: &Value) -> Result<Option<String>, String> {
let kind = value["type"].as_str().ok_or(INVALID)?;
if self.stopped && !matches!(kind, "message_stop" | "ping") {
return Err(INVALID.into());
}
match kind {
"content_block_start" => {
let block = &value["content_block"];
match block["type"].as_str().ok_or(INVALID)? {
"text" => Ok(Some(block["text"].as_str().ok_or(INVALID)?.into())),
"thinking" | "redacted_thinking" => Ok(None),
_ => Err("AI returned unsupported content instead of replacement text. The result cannot be applied.".into()),
}
}
"content_block_delta" => {
let delta = &value["delta"];
match delta["type"].as_str().ok_or(INVALID)? {
"text_delta" => Ok(Some(delta["text"].as_str().ok_or(INVALID)?.into())),
"thinking_delta" | "signature_delta" => Ok(None),
_ => Err(INVALID.into()),
}
}
"message_delta" => {
match value["delta"]["stop_reason"].as_str().ok_or(INVALID)? {
"end_turn" | "stop_sequence" => self.stopped = true,
"max_tokens" => return Err(OUTPUT_LIMIT.into()),
_ => return Err(
"AI response did not finish successfully. The result cannot be applied."
.into(),
),
}
Ok(None)
}
"message_stop" => {
if !self.stopped || !self.has_text {
return Err(INCOMPLETE.into());
}
self.complete = true;
Ok(None)
}
// Anthropic may add informational events; they cannot mark completion.
_ => Ok(None),
}
}
}
pub(super) async fn receive(
response: reqwest::Response,
protocol: Protocol,
mut on_text: impl FnMut(String),
) -> Result<(), String> {
let mut stream = response.bytes_stream();
let mut decoder = sse::Events::default();
let mut completion = Completion::new(protocol);
while let Some(chunk) = stream.next().await {
let chunk = chunk.map_err(|_| "AI stream connection failed")?;
for event in decoder.push(&chunk)? {
if let Some(text) = completion.event(&event)? {
decoder.record_text(&text)?;
if !text.is_empty() {
on_text(text);
}
}
}
if completion.complete {
return Ok(());
}
}
Err(INCOMPLETE.into())
}
#[cfg(test)]
#[path = "stream_tests.rs"]
mod tests;
+319
View File
@@ -0,0 +1,319 @@
use super::*;
use serde_json::json;
use std::io::{Read, Write};
fn data(value: Value) -> String {
format!("data: {value}\n\n")
}
fn openai_text(text: &str) -> String {
data(json!({"choices":[{"index":0,"delta":{"content":text},"finish_reason":null}]}))
}
fn openai_stop(reason: &str) -> String {
data(json!({"choices":[{"index":0,"delta":{},"finish_reason":reason}]}))
}
fn anthropic_text(text: &str) -> String {
data(json!({"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":text}}))
}
fn anthropic_stop(reason: &str) -> String {
data(
json!({"type":"message_delta","delta":{"stop_reason":reason},"usage":{"output_tokens":10}}),
)
}
fn anthropic_done() -> String {
data(json!({"type":"message_stop"}))
}
async fn request(body: &str, protocol: Protocol) -> (Result<(), String>, String) {
mock_response(body.as_bytes(), body.len(), protocol).await
}
async fn mock_response(
body: &[u8],
declared_length: usize,
protocol: Protocol,
) -> (Result<(), String>, String) {
// Loopback only: exercise the production HTTP-body/SSE/completion path without
// provider credentials, paid calls, or a native UI mock that invents 'done'.
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let url = format!("http://{}/stream", listener.local_addr().unwrap());
let body = body.to_vec();
let server = std::thread::spawn(move || {
let (mut socket, _) = listener.accept().unwrap();
socket
.set_read_timeout(Some(std::time::Duration::from_secs(5)))
.unwrap();
let mut request = Vec::new();
while !request.ends_with(b"\r\n\r\n") {
let mut bytes = [0; 1024];
let count = socket.read(&mut bytes).unwrap();
assert!(count > 0, "client closed before sending request headers");
request.extend_from_slice(&bytes[..count]);
assert!(request.len() <= 8192);
}
write!(socket, "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {declared_length}\r\nConnection: close\r\n\r\n").unwrap();
// The receiver can reject an invalid stream before the server finishes writing.
let _ = socket.write_all(&body);
});
let response = reqwest::Client::builder()
.no_proxy()
.timeout(std::time::Duration::from_secs(5))
.build()
.unwrap()
.get(url)
.send()
.await
.unwrap();
let mut text = String::new();
let result = receive(response, protocol, |chunk| text.push_str(&chunk)).await;
server.join().unwrap();
(result, text)
}
#[tokio::test]
async fn openai_requires_successful_stop_and_done_and_allows_usage_metadata() {
let wire = data(json!({"choices":[{"index":0,"delta":{"role":"assistant"}}]}))
+ &openai_text("Привет café 👋")
+ &openai_stop("stop")
+ &data(json!({"choices":[],"usage":{"completion_tokens":10}}))
+ "data: [DONE]\n\n";
let (result, text) = request(&wire, Protocol::OpenAi).await;
assert_eq!(result, Ok(()));
assert_eq!(text, "Привет café 👋");
}
#[tokio::test]
async fn openai_rejects_all_unsuccessful_finish_reasons() {
for reason in [
"length",
"content_filter",
"tool_calls",
"function_call",
"unknown-secret",
] {
let wire = openai_text("Partial replacement") + &openai_stop(reason) + "data: [DONE]\n\n";
let (result, _) = request(&wire, Protocol::OpenAi).await;
let error = result.unwrap_err();
assert!(!error.contains("unknown-secret"));
if reason == "length" {
assert_eq!(error, OUTPUT_LIMIT);
}
}
}
#[tokio::test]
async fn openai_rejects_missing_terminal_events_and_empty_replacements() {
for wire in [
openai_text("Partial") + "data: [DONE]\n\n",
openai_text("Partial") + &openai_stop("stop"),
openai_text("Partial"),
"data: [DONE]\n\n".into(),
openai_text(" \n") + &openai_stop("stop") + "data: [DONE]\n\n",
] {
assert!(request(&wire, Protocol::OpenAi).await.0.is_err());
}
}
#[tokio::test]
async fn malformed_json_and_invalid_shapes_cannot_be_skipped_before_done() {
for invalid in [
"{broken secret",
"[]",
"null",
"{}",
r#"{"choices":"secret"}"#,
r#"{"choices":[{"delta":{"content":42}}]}"#,
] {
for protocol in [Protocol::OpenAi, Protocol::Anthropic] {
let (prefix, suffix) = match protocol {
Protocol::OpenAi => (
openai_text("Partial"),
openai_stop("stop") + "data: [DONE]\n\n",
),
Protocol::Anthropic => (
anthropic_text("Partial"),
anthropic_stop("end_turn") + &anthropic_done(),
),
};
let wire = format!("{prefix}data: {invalid}\n\n{suffix}");
let error = request(&wire, protocol).await.0.unwrap_err();
assert_eq!(error, INVALID);
}
}
}
#[tokio::test]
async fn errors_after_stop_but_before_done_still_block_application() {
let wire = openai_text("Draft")
+ &openai_stop("stop")
+ &data(json!({"error":{"message":"secret key and private prompt"}}))
+ "data: [DONE]\n\n";
assert_eq!(
request(&wire, Protocol::OpenAi).await.0.unwrap_err(),
PROVIDER_ERROR
);
}
#[tokio::test]
async fn anthropic_requires_successful_stop_reason_and_message_stop() {
for reason in ["end_turn", "stop_sequence"] {
let wire = data(json!({"type":"message_start","message":{"role":"assistant"}}))
+ &data(
json!({"type":"content_block_start","content_block":{"type":"text","text":""}}),
)
+ &anthropic_text("Привет café 👋")
+ &data(json!({"type":"content_block_stop","index":0}))
+ &anthropic_stop(reason)
+ &data(json!({"type":"ping"}))
+ &anthropic_done();
let (result, text) = request(&wire, Protocol::Anthropic).await;
assert_eq!(result, Ok(()));
assert_eq!(text, "Привет café 👋");
}
}
#[tokio::test]
async fn anthropic_rejects_refusals_limits_tools_pauses_and_unknown_reasons() {
for reason in [
"max_tokens",
"refusal",
"tool_use",
"pause_turn",
"model_context_window_exceeded",
"unknown-secret",
] {
let wire = anthropic_text("Partial") + &anthropic_stop(reason) + &anthropic_done();
let error = request(&wire, Protocol::Anthropic).await.0.unwrap_err();
assert!(!error.contains("unknown-secret"));
if reason == "max_tokens" {
assert_eq!(error, OUTPUT_LIMIT);
}
}
}
#[tokio::test]
async fn anthropic_rejects_missing_terminal_events_empty_text_and_openai_done() {
for wire in [
anthropic_text("Partial") + &anthropic_done(),
anthropic_text("Partial") + &anthropic_stop("end_turn"),
anthropic_text("Partial") + "data: [DONE]\n\n",
anthropic_stop("end_turn") + &anthropic_done(),
anthropic_text(" \n") + &anthropic_stop("end_turn") + &anthropic_done(),
] {
assert!(request(&wire, Protocol::Anthropic).await.0.is_err());
}
}
#[tokio::test]
async fn refusals_and_tool_payloads_are_never_replacement_text() {
for delta in [
json!({"refusal":"secret refusal"}),
json!({"tool_calls":[]}),
json!({"function_call":{"name":"secret"}}),
] {
let wire = openai_text("Partial")
+ &data(json!({"choices":[{"delta":delta,"finish_reason":"stop"}]}))
+ "data: [DONE]\n\n";
let error = request(&wire, Protocol::OpenAi).await.0.unwrap_err();
assert!(!error.contains("secret"));
}
let wire = anthropic_text("Partial")
+ &data(
json!({"type":"content_block_start","content_block":{"type":"tool_use","input":{"secret":"private"}}}),
)
+ &anthropic_stop("end_turn")
+ &anthropic_done();
assert!(request(&wire, Protocol::Anthropic).await.0.is_err());
}
#[tokio::test]
async fn provider_error_payloads_are_replaced_with_a_fixed_safe_message() {
for payload in [
json!({"error":{"type":"secret-key","message":"private prompt data:image/png;base64,PRIVATE"}}),
json!({"error":"secret-key"}),
json!({"type":"error","message":"secret-key"}),
] {
for protocol in [Protocol::OpenAi, Protocol::Anthropic] {
let error = request(&data(payload.clone()), protocol)
.await
.0
.unwrap_err();
assert_eq!(error, PROVIDER_ERROR);
assert!(!error.contains("PRIVATE"));
assert!(!error.contains("secret-key"));
}
}
}
#[tokio::test]
async fn supports_optional_sse_spacing_comments_and_multiline_data() {
let wire = ": keepalive\r\n\r\nevent: message\r\nid: 1\r\ndata:{\r\ndata: \"choices\": [{\"delta\":{\"content\":\"OK\"}}]}\r\n\r\n".to_string()
+ &openai_stop("stop").replace('\n', "\r\n") + "data:[DONE]\r\n\r\n";
let (result, text) = request(&wire, Protocol::OpenAi).await;
assert_eq!(result, Ok(()));
assert_eq!(text, "OK");
}
#[test]
fn completion_validation_survives_every_utf8_chunk_boundary_for_both_providers() {
for (protocol, wire) in [
(
Protocol::OpenAi,
openai_text("Привет café 👋") + &openai_stop("stop") + "data: [DONE]\n\n",
),
(
Protocol::Anthropic,
anthropic_text("Привет café 👋") + &anthropic_stop("end_turn") + &anthropic_done(),
),
] {
for split in 0..wire.len() {
let mut decoder = sse::Events::default();
let mut completion = Completion::new(protocol);
let mut text = String::new();
for bytes in [&wire.as_bytes()[..split], &wire.as_bytes()[split..]] {
for event in decoder.push(bytes).unwrap() {
if let Some(chunk) = completion.event(&event).unwrap() {
text.push_str(&chunk);
}
}
}
assert!(completion.complete);
assert_eq!(text, "Привет café 👋");
}
}
}
#[tokio::test]
async fn disconnected_http_body_and_invalid_utf8_fail_closed() {
let body = openai_text("Partial");
assert!(
mock_response(body.as_bytes(), body.len() + 100, Protocol::OpenAi)
.await
.0
.is_err()
);
assert!(
request(&(body.clone() + "data: {partial"), Protocol::OpenAi)
.await
.0
.is_err()
);
let bytes = [body.as_bytes(), b"data: \xff\n\n"].concat();
assert!(mock_response(&bytes, bytes.len(), Protocol::OpenAi)
.await
.0
.is_err());
}
#[tokio::test]
async fn output_size_limits_remain_enforced_before_completion() {
let wire =
openai_text(&"x".repeat(2 * 1024 * 1024 + 1)) + &openai_stop("stop") + "data: [DONE]\n\n";
let (result, text) = request(&wire, Protocol::OpenAi).await;
assert!(result.unwrap_err().contains("text limit"));
assert!(text.is_empty());
}
+77 -3
View File
@@ -21,12 +21,18 @@ const MAX_NAME_BYTES: usize = 256 * 1024 * 1024;
const MAX_PATH_COMPONENTS: usize = 64;
const MANIFEST: &str = ".helixnotes-backup.json";
#[cfg(test)]
mod daily_template_tests;
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct BackupManifest {
version: u32,
vault: String,
attachments: bool,
/// None is a pre-template backup: leave the current template alone on restore.
#[serde(default, skip_serializing_if = "Option::is_none")]
daily_template: Option<bool>,
}
fn vault_id(vault: &Path) -> Result<String, String> {
@@ -69,7 +75,10 @@ fn excluded_path(path: &Path, vault: &Path, backup_dir: &Path, attachments: bool
let first = parts.next().expect("non-root entry").as_os_str();
if first == ".helixnotes" {
return match parts.next() {
None => !attachments,
None => false,
Some(part) if part.as_os_str() == "daily-template.md" && parts.next().is_none() => {
false
}
Some(part) if part.as_os_str() == "attachments" && attachments => {
parts.any(|part| part.as_os_str().to_string_lossy().starts_with('.'))
}
@@ -122,6 +131,7 @@ pub fn create_backup(
return Err("Backup directory must not be the vault root".to_string());
}
let id = vault_id(&vault)?;
let has_template = crate::vault::daily_template::read(vault_path)?.is_some();
let now = Utc::now();
let timestamp = now.format("%Y-%m-%dT%H-%M-%S").to_string();
let filename = format!("helixnotes-backup-{timestamp}-{id}-{}.zip", Uuid::new_v4());
@@ -142,6 +152,7 @@ pub fn create_backup(
version: 1,
vault: id,
attachments: include_attachments,
daily_template: Some(has_template),
},
)
.map_err(|e| e.to_string())?;
@@ -372,6 +383,7 @@ fn install_staged(
previous: &Path,
backup_dir: &Path,
attachments: bool,
template: Option<bool>,
) -> Result<(), String> {
let nested = backup_dir
.strip_prefix(vault)
@@ -396,6 +408,10 @@ fn install_staged(
let attachments_path = vault.join(".helixnotes/attachments");
let mut displaced_attachments = false;
let mut installed_attachments = false;
let template_path = vault.join(crate::vault::daily_template::RELATIVE_PATH);
let previous_template = previous.join("daily-template.md");
let mut displaced_template = false;
let mut installed_template = false;
let operation = (|| -> io::Result<()> {
fs::create_dir(&previous_entries)?;
fs::create_dir(&held_history)?;
@@ -447,6 +463,20 @@ fn install_staged(
fs::rename(contents.join(".helixnotes/attachments"), &attachments_path)?;
installed_attachments = true;
}
if let Some(enabled) = template {
if fs::symlink_metadata(&template_path).is_ok() {
fs::rename(&template_path, &previous_template)?;
displaced_template = true;
}
if enabled {
fs::create_dir_all(vault.join(".helixnotes"))?;
fs::rename(
contents.join(crate::vault::daily_template::RELATIVE_PATH),
&template_path,
)?;
installed_template = true;
}
}
if let Some(relative) = nested {
if held_output {
let destination = vault.join(relative);
@@ -489,6 +519,11 @@ fn install_staged(
failures.join("; ")
));
}
if installed_template {
if let Err(e) = remove_entry(&template_path) {
failures.push(format!("remove new daily template: {e}"));
}
}
if installed_attachments {
if let Err(e) = remove_entry(&attachments_path) {
failures.push(format!("remove new attachments: {e}"));
@@ -514,6 +549,11 @@ fn install_staged(
failures.push(format!("restore original {:?}: {e}", name));
}
}
if displaced_template {
if let Err(e) = fs::rename(&previous_template, &template_path) {
failures.push(format!("restore daily template: {e}"));
}
}
if displaced_attachments {
if let Err(e) = fs::rename(&previous_attachments, &attachments_path) {
failures.push(format!("restore attachments: {e}"));
@@ -634,6 +674,15 @@ pub fn restore_backup(
Some(component) if component.as_os_str() == "attachments" => {
legacy_attachments = true;
}
Some(component)
if component.as_os_str() == "daily-template.md"
&& parts.next().is_none()
&& !directory =>
{
if item.size() > crate::vault::daily_template::MAX_BYTES as u64 {
return Err("Daily template exceeds the 256 KiB limit".into());
}
}
_ => return Err(format!("Archive contains unsupported app metadata: {name}")),
}
} else if first.as_os_str().to_string_lossy().starts_with('.')
@@ -710,11 +759,28 @@ pub fn restore_backup(
}
}
// An archive holding only its manifest would replace the vault with nothing.
if seen.keys().all(|entry| entry == Path::new(MANIFEST)) {
if seen.keys().all(|entry| {
entry == Path::new(MANIFEST)
|| entry == Path::new(".helixnotes")
|| entry == Path::new(crate::vault::daily_template::RELATIVE_PATH)
}) {
return Err(
"This backup contains no notes, so restoring it would empty the vault".to_string(),
);
}
let has_template = seen.contains_key(Path::new(crate::vault::daily_template::RELATIVE_PATH));
let template = match manifest.as_ref().and_then(|meta| meta.daily_template) {
Some(enabled) if enabled != has_template => {
return Err("Daily template does not match the backup manifest".into())
}
Some(enabled) => Some(enabled),
None => has_template.then_some(true),
};
if has_template {
let file = File::open(contents.join(crate::vault::daily_template::RELATIVE_PATH))
.map_err(|e| e.to_string())?;
crate::vault::daily_template::read_from(file)?;
}
let attachments = if let Some(meta) = manifest {
if meta.version != 1
|| meta.vault.len() != 64
@@ -736,13 +802,21 @@ pub fn restore_backup(
return Err("Cannot replace attachments containing the backup output".to_string());
}
}
}
if attachments || template.is_some() {
let internals = vault.join(".helixnotes");
if fs::symlink_metadata(&internals).is_ok_and(|meta| !meta.file_type().is_dir()) {
return Err("Vault app metadata directory is not a real directory".to_string());
}
}
if template.is_some()
&& fs::symlink_metadata(vault.join(crate::vault::daily_template::RELATIVE_PATH))
.is_ok_and(|meta| meta.file_type().is_dir())
{
return Err("Daily template destination must not be a directory".into());
}
drop(archive);
let result = install_staged(&vault, &contents, &previous, &output, attachments);
let result = install_staged(&vault, &contents, &previous, &output, attachments, template);
if result
.as_ref()
.is_err_and(|message| message.contains("rollback incomplete"))
@@ -0,0 +1,192 @@
use super::{create_backup, restore_backup};
use crate::vault::daily_template as template;
use std::{fs, io::Write};
use zip::write::SimpleFileOptions;
fn archive(path: &std::path::Path, entries: &[(&str, &[u8])]) {
let file = fs::File::create(path).unwrap();
let mut writer = zip::ZipWriter::new(file);
for (name, bytes) in entries {
writer
.start_file(*name, SimpleFileOptions::default())
.unwrap();
writer.write_all(bytes).unwrap();
}
writer.finish().unwrap();
}
#[test]
fn templates_are_backed_up_without_attachments_and_restore_enabled_and_disabled_state() {
let dir = tempfile::tempdir().unwrap();
let output = tempfile::tempdir().unwrap();
let vault = dir.path().to_str().unwrap();
fs::write(dir.path().join("Note.md"), "Original note\n").unwrap();
template::set(vault, Some("# {{title}}\n- [ ] Task\n"), None).unwrap();
let snapshot = create_backup(vault, output.path(), false).unwrap();
let current = template::read(vault).unwrap();
template::set(vault, Some("Later template"), current.as_deref()).unwrap();
fs::write(dir.path().join("Note.md"), "Edited note\n").unwrap();
restore_backup(vault, output.path(), &snapshot.path).unwrap();
let restored = template::DailyTemplate::from_raw(template::read(vault).unwrap());
assert!(restored.enabled);
assert_eq!(restored.content, "# {{title}}\n- [ ] Task\n");
assert_eq!(
fs::read_to_string(dir.path().join("Note.md")).unwrap(),
"Original note\n"
);
let disabled = template::set(vault, None, restored.raw.as_deref()).unwrap();
let snapshot = create_backup(vault, output.path(), false).unwrap();
template::set(vault, Some("Enabled later"), disabled.as_deref()).unwrap();
restore_backup(vault, output.path(), &snapshot.path).unwrap();
let restored = template::DailyTemplate::from_raw(template::read(vault).unwrap());
assert!(!restored.enabled);
assert_eq!(restored.content, "# {{title}}\n- [ ] Task\n");
}
#[test]
fn restoring_a_new_snapshot_without_a_template_restores_that_absence() {
let dir = tempfile::tempdir().unwrap();
let output = tempfile::tempdir().unwrap();
let vault = dir.path().to_str().unwrap();
fs::write(dir.path().join("Note.md"), "Note").unwrap();
let snapshot = create_backup(vault, output.path(), false).unwrap();
template::set(vault, Some("Added after backup"), None).unwrap();
restore_backup(vault, output.path(), &snapshot.path).unwrap();
assert_eq!(template::read(vault).unwrap(), None);
}
#[test]
fn legacy_backups_leave_the_existing_template_untouched() {
let dir = tempfile::tempdir().unwrap();
let output = tempfile::tempdir().unwrap();
let vault = dir.path().to_str().unwrap();
template::set(vault, Some("Keep this template"), None).unwrap();
let path = output.path().join("legacy.zip");
let manifest = serde_json::to_vec(&serde_json::json!({
"version": 1, "vault": super::vault_id(dir.path()).unwrap(), "attachments": false,
}))
.unwrap();
for with_manifest in [false, true] {
let mut entries: Vec<(&str, &[u8])> = vec![("Note.md", b"Legacy note")];
if with_manifest {
entries.push((super::MANIFEST, &manifest));
}
archive(&path, &entries);
restore_backup(vault, output.path(), path.to_str().unwrap()).unwrap();
assert_eq!(
template::read(vault).unwrap().as_deref(),
Some("Keep this template")
);
}
assert_eq!(
template::read(vault).unwrap().as_deref(),
Some("Keep this template")
);
assert_eq!(
fs::read_to_string(dir.path().join("Note.md")).unwrap(),
"Legacy note"
);
}
#[test]
fn template_only_backups_cannot_erase_the_current_notes() {
let dir = tempfile::tempdir().unwrap();
let output = tempfile::tempdir().unwrap();
let vault = dir.path().to_str().unwrap();
template::set(vault, Some("Only a template"), None).unwrap();
let snapshot = create_backup(vault, output.path(), false).unwrap();
fs::write(dir.path().join("Keep.md"), "Keep me").unwrap();
assert!(restore_backup(vault, output.path(), &snapshot.path).is_err());
assert_eq!(
fs::read_to_string(dir.path().join("Keep.md")).unwrap(),
"Keep me"
);
}
#[test]
fn oversized_and_binary_templates_are_rejected_before_the_live_vault_changes() {
let dir = tempfile::tempdir().unwrap();
let output = tempfile::tempdir().unwrap();
let vault = dir.path().to_str().unwrap();
fs::write(dir.path().join("Keep.md"), "Keep me").unwrap();
template::set(vault, Some("Original template"), None).unwrap();
for bytes in [vec![b'x'; template::MAX_BYTES + 1], vec![0xff]] {
let path = output.path().join("bad.zip");
archive(
&path,
&[
("Note.md", b"Replacement"),
(template::RELATIVE_PATH, &bytes),
],
);
assert!(restore_backup(vault, output.path(), path.to_str().unwrap()).is_err());
assert_eq!(
fs::read_to_string(dir.path().join("Keep.md")).unwrap(),
"Keep me"
);
assert_eq!(
template::read(vault).unwrap().as_deref(),
Some("Original template")
);
}
}
#[test]
fn failed_install_rolls_back_the_template_notes_and_historical_backups() {
let dir = tempfile::tempdir().unwrap();
let output = tempfile::tempdir().unwrap();
let vault = dir.path().to_str().unwrap();
fs::write(dir.path().join("Keep.md"), "Keep me").unwrap();
fs::create_dir(dir.path().join("History")).unwrap();
let historical = dir.path().join("History/helixnotes-backup-kept.zip");
fs::write(&historical, "Historical backup").unwrap();
template::set(vault, Some("Original template"), None).unwrap();
let path = output.path().join("rollback.zip");
// Installation reaches the template swap, then cannot reinsert the historical ZIP
// because the archive replaced its containing directory with a regular file.
archive(
&path,
&[
("History", b"not a directory"),
("Replacement.md", b"Replacement"),
(template::RELATIVE_PATH, b"New template"),
],
);
assert!(restore_backup(vault, output.path(), path.to_str().unwrap()).is_err());
assert_eq!(
fs::read_to_string(dir.path().join("Keep.md")).unwrap(),
"Keep me"
);
assert_eq!(
template::read(vault).unwrap().as_deref(),
Some("Original template")
);
assert_eq!(fs::read_to_string(historical).unwrap(), "Historical backup");
assert!(!dir.path().join("Replacement.md").exists());
}
#[cfg(unix)]
#[test]
fn template_restore_cannot_write_through_a_metadata_directory_symlink() {
use std::os::unix::fs::symlink;
let dir = tempfile::tempdir().unwrap();
let output = tempfile::tempdir().unwrap();
let outside = tempfile::tempdir().unwrap();
let vault = dir.path().to_str().unwrap();
fs::write(dir.path().join("Keep.md"), "Keep me").unwrap();
let path = output.path().join("template.zip");
archive(
&path,
&[
("Note.md", b"Replacement"),
(template::RELATIVE_PATH, b"Template"),
],
);
symlink(outside.path(), dir.path().join(".helixnotes")).unwrap();
assert!(restore_backup(vault, output.path(), path.to_str().unwrap()).is_err());
assert_eq!(
fs::read_to_string(dir.path().join("Keep.md")).unwrap(),
"Keep me"
);
assert!(fs::read_dir(outside.path()).unwrap().next().is_none());
}
+232 -8
View File
@@ -2,6 +2,7 @@ use crate::asset_scope;
use crate::search::SearchIndex;
use crate::state::AppState;
use crate::types::*;
use crate::vault::daily_template::DailyTemplate;
use crate::vault::operations;
use std::path::{Path, PathBuf};
use std::sync::Arc;
@@ -490,6 +491,36 @@ pub fn complete_window_close<R: Runtime>(
window.destroy().map_err(|error| error.to_string())
}
#[tauri::command]
pub fn set_language<R: Runtime>(
app: AppHandle<R>,
state: State<'_, AppState>,
language: String,
) -> Result<(), String> {
if language != "system" && !crate::i18n::supported_languages().contains(&language) {
return Err("Unsupported interface language".to_string());
}
let mut config = state.config.lock().map_err(|e| e.to_string())?;
let previous = std::mem::replace(&mut config.language, language.clone());
if let Err(error) = save_app_config(&config) {
config.language = previous;
return Err(error);
}
drop(config);
#[cfg(desktop)]
if let Some(tray) = app.tray_by_id("helixnotes") {
match crate::localized_tray_menu(&app, &language) {
Ok(menu) => {
let _ = tray.set_menu(Some(menu));
}
Err(error) => log::error!("Could not update tray language: {error}"),
}
}
// Detached note windows share this device's language, without touching vault state.
let _ = app.emit("language-changed", &language);
Ok(())
}
#[tauri::command]
pub fn set_theme(state: State<'_, AppState>, theme: String) -> Result<(), String> {
let mut config = state.config.lock().map_err(|e| e.to_string())?;
@@ -996,14 +1027,99 @@ pub async fn create_daily_note(
.map_err(|error| error.to_string())?
.daily_title_format
.clone();
let _write_guard = state.note_save.lock().await;
let entry = operations::create_daily_note(&vault_path, date.as_deref(), &title_format)?;
let write_guard = state.note_save.clone().lock_owned().await;
let entry = tauri::async_runtime::spawn_blocking(move || {
let _write_guard = write_guard;
operations::create_daily_note(&vault_path, date.as_deref(), &title_format)
})
.await
.map_err(|e| e.to_string())??;
index_note_bg(search, &entry.path);
Ok(entry)
}
#[tauri::command]
pub async fn get_daily_note_template(
state: State<'_, AppState>,
vault_path: String,
) -> Result<DailyTemplate, String> {
state.validate_vault(&vault_path)?;
let guard = state.note_save.clone().lock_owned().await;
tauri::async_runtime::spawn_blocking(move || {
let _guard = guard;
crate::vault::daily_template::read(&vault_path).map(DailyTemplate::from_raw)
})
.await
.map_err(|e| e.to_string())?
}
#[tauri::command]
pub async fn set_daily_note_template(
state: State<'_, AppState>,
vault_path: String,
template: Option<String>,
expected_template: Option<String>,
) -> Result<DailyTemplate, String> {
let guard = state.note_save.clone().lock_owned().await;
state.validate_vault(&vault_path)?;
tauri::async_runtime::spawn_blocking(move || {
let _guard = guard;
crate::vault::daily_template::set(
&vault_path,
template.as_deref(),
expected_template.as_deref(),
)
.map(DailyTemplate::from_raw)
})
.await
.map_err(|e| e.to_string())?
}
#[tauri::command]
pub async fn import_daily_note_template<R: Runtime>(
app: AppHandle<R>,
state: State<'_, AppState>,
vault_path: String,
title: String,
expected_template: Option<String>,
) -> Result<Option<DailyTemplate>, String> {
state.validate_vault(&vault_path)?;
// Pick inside this command: callers cannot nominate an arbitrary external path.
// Do not hold the note-save lock while a user is choosing a file.
let text = tauri::async_runtime::spawn_blocking(move || {
use tauri_plugin_dialog::DialogExt;
let Some(source) = app
.dialog()
.file()
.set_title(title)
.add_filter("Markdown", &["md"])
.blocking_pick_file()
else {
return Ok::<_, String>(None);
};
let mut options = tauri_plugin_fs::OpenOptions::new();
options.read(true);
let file = app.fs().open(source, options).map_err(|e| e.to_string())?;
crate::vault::daily_template::read_from(file).map(Some)
})
.await
.map_err(|e| e.to_string())??;
let Some(text) = text else {
return Ok(None);
};
let guard = state.note_save.clone().lock_owned().await;
state.validate_vault(&vault_path)?;
tauri::async_runtime::spawn_blocking(move || {
let _guard = guard;
crate::vault::daily_template::set(&vault_path, Some(&text), expected_template.as_deref())
.map(|raw| Some(DailyTemplate::from_raw(raw)))
})
.await
.map_err(|e| e.to_string())?
}
#[tauri::command]
pub async fn rename_note<R: Runtime>(
app: AppHandle<R>,
@@ -3961,6 +4077,30 @@ pub async fn get_note_version_content(
// ── AI ──
#[tauri::command]
pub async fn fetch_ai_models(
provider: String,
api_key: String,
) -> Result<Vec<crate::ai::models::AiModel>, String> {
crate::ai::models::fetch_models(&provider, &api_key).await
}
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct AiImageContext {
vault_path: String,
note_path: String,
paths: Vec<String>,
}
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct AiRequestConfig {
provider: Option<String>,
model: String,
base_url: Option<String>,
}
#[tauri::command]
#[allow(clippy::too_many_arguments)]
pub fn set_ai_settings(
@@ -3974,8 +4114,20 @@ pub fn set_ai_settings(
openai_compatible_base_url: Option<String>,
openai_compatible_api_key: Option<String>,
) -> Result<(), String> {
let mut config = state.config.lock().map_err(|e| e.to_string())?;
let key = api_key.filter(|k| !k.is_empty());
let mut current = state.config.lock().map_err(|e| e.to_string())?;
let mut config = current.clone();
let key = api_key
.map(|key| key.trim().to_string())
.filter(|key| !key.is_empty());
let model = model.trim().to_string();
if model.len() > 512
|| key.as_ref().is_some_and(|key| key.len() > 8192)
|| writing_style
.as_ref()
.is_some_and(|style| style.len() > 256 * 1024)
{
return Err("AI settings exceed the size limit".into());
}
match provider.as_deref() {
Some("openai") => config.openai_api_key = key,
Some("ollama") => {
@@ -3987,12 +4139,23 @@ pub fn set_ai_settings(
openai_compatible_base_url.filter(|u| !u.trim().is_empty());
config.openai_compatible_api_key = openai_compatible_api_key.filter(|k| !k.is_empty());
}
_ => config.ai_api_key = key,
Some("anthropic") => config.ai_api_key = key,
None => {} // Turning AI off must not erase saved credentials.
_ => return Err("Unsupported AI provider".into()),
}
let remembered = (!model.is_empty()).then(|| model.clone());
match provider.as_deref() {
Some("openai") => config.openai_model = remembered,
Some("anthropic") => config.anthropic_model = remembered,
Some("ollama") => config.ollama_model = remembered,
Some("openai_compatible") => config.openai_compatible_model = remembered,
_ => {}
}
config.ai_provider = provider;
config.ai_model = model;
config.ai_writing_style = writing_style.filter(|s| !s.trim().is_empty());
save_app_config(&config)?;
*current = config;
Ok(())
}
@@ -4004,7 +4167,7 @@ pub fn test_ai_connection(app: AppHandle) -> Result<(), String> {
let provider = config
.ai_provider
.clone()
.unwrap_or_else(|| "anthropic".to_string());
.ok_or("AI is disabled. Choose a provider in Settings.")?;
let key = match provider.as_str() {
"ollama" => Some(config.ollama_api_key.clone().unwrap_or_default()),
"openai_compatible" => {
@@ -4021,6 +4184,14 @@ pub fn test_ai_connection(app: AppHandle) -> Result<(), String> {
};
(provider, key, model, base_url)
};
if model.trim().is_empty()
|| !matches!(
provider.as_str(),
"openai" | "anthropic" | "ollama" | "openai_compatible"
)
{
return Err("Choose an AI provider and model in Settings".into());
}
std::thread::spawn(move || {
use tauri::Emitter;
@@ -4267,20 +4438,25 @@ pub fn sync_now(app: AppHandle) -> Result<(), String> {
}
#[tauri::command]
pub fn ai_ask(
pub async fn ai_ask(
app: AppHandle,
action: String,
text: String,
custom_prompt: Option<String>,
request_id: String,
image_context: Option<AiImageContext>,
expected_config: Option<AiRequestConfig>,
) -> Result<(), String> {
if text.len() + custom_prompt.as_ref().map_or(0, String::len) > 2 * 1024 * 1024 {
return Err("AI text exceeds the 2 MiB request limit".into());
}
let (provider, api_key, model, writing_style, base_url) = {
let state = app.state::<AppState>();
let config = state.config.lock().map_err(|e| e.to_string())?;
let provider = config
.ai_provider
.clone()
.unwrap_or_else(|| "anthropic".to_string());
.ok_or("AI is disabled. Choose a provider in Settings.")?;
let key = match provider.as_str() {
"ollama" => Some(config.ollama_api_key.clone().unwrap_or_default()),
"openai_compatible" => {
@@ -4299,6 +4475,49 @@ pub fn ai_ask(
(provider, key, model, style, base_url)
};
if let Some(expected) = expected_config {
let relevant_base = matches!(provider.as_str(), "ollama" | "openai_compatible");
if expected.provider.as_deref() != Some(provider.as_str())
|| expected.model != model
|| (relevant_base && expected.base_url != base_url)
{
return Err(
"AI settings changed. Reopen AI actions before sending this request.".into(),
);
}
} else if image_context.is_some() {
return Err("Confirm the AI destination before including images".into());
}
if model.trim().is_empty() {
return Err("Choose an AI model in Settings before starting".into());
}
if !matches!(
provider.as_str(),
"openai" | "anthropic" | "ollama" | "openai_compatible"
) {
return Err("Unsupported AI provider".into());
}
let images = if let Some(context) = image_context {
let state = app.state::<AppState>();
state.validate_vault(&context.vault_path)?;
let guard = state.note_save.clone().lock_owned().await;
tauri::async_runtime::spawn_blocking(move || {
let _guard = guard;
crate::ai::images::prepare(&context.vault_path, &context.note_path, &context.paths)
})
.await
.map_err(|_| "AI image preparation failed")??
} else {
Vec::new()
};
if writing_style
.as_ref()
.is_some_and(|style| style.len() > 256 * 1024)
|| model.len() > 512
{
return Err("AI settings exceed the size limit".into());
}
let mut system_prompt = "You are a helpful writing assistant inside a note-taking app called HelixNotes. \
You help users improve, rewrite, summarize, and transform their text. \
Return ONLY the resulting text - no explanations, no markdown code fences, no preamble. \
@@ -4313,7 +4532,11 @@ pub fn ai_ask(
));
}
if action == "describe_images" && images.is_empty() {
return Err("Select images before requesting a description".into());
}
let user_message = match action.as_str() {
"describe_images" => format!("Describe the attached images, including readable text and relevant details. Preserve the existing text and every media placeholder; add descriptions alongside them:\n\n{}", text),
"improve" => format!("Improve the writing quality of this text while keeping the same meaning and tone:\n\n{}", text),
"fix_grammar" => format!("Fix all grammar, spelling, and punctuation errors in this text. Keep the original style:\n\n{}", text),
"shorter" => format!("Make this text more concise while keeping the key points:\n\n{}", text),
@@ -4343,6 +4566,7 @@ pub fn ai_ask(
user_message,
request_id,
base_url,
images,
);
Ok(())
}
+151
View File
@@ -0,0 +1,151 @@
//! Native labels and supported preferences share the frontend's bundled locale registry.
use std::sync::OnceLock;
pub fn supported_languages() -> &'static [String] {
static LANGUAGES: OnceLock<Vec<String>> = OnceLock::new();
LANGUAGES.get_or_init(|| {
let registry: Vec<serde_json::Value> =
serde_json::from_str(include_str!("../../src/lib/i18n/languages.json"))
.expect("invalid bundled language registry");
registry
.into_iter()
.map(|entry| {
entry["code"]
.as_str()
.expect("missing language code")
.to_string()
})
.collect()
})
}
#[cfg(any(desktop, test))]
fn resolve_tag(tag: &str) -> Option<&'static str> {
let tag = tag
.trim()
.split(['.', '@'])
.next()
.unwrap_or_default()
.replace('_', "-")
.to_ascii_lowercase();
if let Some(exact) = supported_languages()
.iter()
.find(|code| code.to_ascii_lowercase() == tag)
{
return Some(exact);
}
let mut parts = tag.split('-');
let base = parts.next()?;
if base == "pt" {
return Some(if parts.any(|part| part == "br") {
"pt-BR"
} else {
"pt-PT"
});
}
supported_languages()
.iter()
.find(|code| code.as_str() == base)
.map(String::as_str)
}
#[cfg(any(desktop, test))]
type Catalogs = std::collections::HashMap<&'static str, std::collections::HashMap<String, String>>;
#[cfg(any(desktop, test))]
fn catalogs() -> &'static Catalogs {
static CATALOGS: OnceLock<Catalogs> = OnceLock::new();
CATALOGS.get_or_init(|| {
[
("en", include_str!("../../src/lib/i18n/locales/en.json")),
("ru", include_str!("../../src/lib/i18n/locales/ru.json")),
("nl", include_str!("../../src/lib/i18n/locales/nl.json")),
("de", include_str!("../../src/lib/i18n/locales/de.json")),
("es", include_str!("../../src/lib/i18n/locales/es.json")),
("fr", include_str!("../../src/lib/i18n/locales/fr.json")),
("it", include_str!("../../src/lib/i18n/locales/it.json")),
(
"pt-PT",
include_str!("../../src/lib/i18n/locales/pt-PT.json"),
),
(
"pt-BR",
include_str!("../../src/lib/i18n/locales/pt-BR.json"),
),
]
.into_iter()
.map(|(code, source)| {
(
code,
serde_json::from_str(source).expect("invalid bundled language catalog"),
)
})
.collect()
})
}
#[cfg(any(desktop, test))]
pub fn text(language: &str, key: &'static str) -> &'static str {
let locale =
if language == "system" || !supported_languages().iter().any(|code| code == language) {
sys_locale::get_locale()
.and_then(|tag| resolve_tag(&tag))
.unwrap_or("en")
} else {
resolve_tag(language).unwrap_or("en")
};
catalogs()
.get(locale)
.and_then(|catalog| catalog.get(key))
.or_else(|| catalogs().get("en").and_then(|catalog| catalog.get(key)))
.map(String::as_str)
.unwrap_or(key)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn native_labels_share_the_frontend_catalogs() {
assert_eq!(text("en", "native.quit"), "Quit");
assert_eq!(text("ru", "native.quit"), "Выйти");
assert_eq!(text("ru", "native.show"), "Показать HelixNotes");
assert_eq!(text("ru", "unknown.key"), "unknown.key");
for (code, quit) in [
("nl", "Afsluiten"),
("de", "Beenden"),
("es", "Salir"),
("fr", "Quitter"),
("it", "Esci"),
("pt-PT", "Sair"),
("pt-BR", "Sair"),
] {
assert_eq!(text(code, "native.quit"), quit, "{code}");
}
for code in supported_languages() {
assert!(catalogs().contains_key(code.as_str()), "missing {code}");
}
assert_eq!(supported_languages().len(), catalogs().len());
}
#[test]
fn native_locale_matching_preserves_portuguese_regions() {
for (tag, expected) in [
("nl-BE", "nl"),
("de_AT.UTF-8", "de"),
("es-MX", "es"),
("fr-CA", "fr"),
("it-CH", "it"),
("ru_RU", "ru"),
("pt", "pt-PT"),
("pt-PT", "pt-PT"),
("pt-AO", "pt-PT"),
("pt_BR.UTF-8", "pt-BR"),
("pt-Latn-BR", "pt-BR"),
] {
assert_eq!(resolve_tag(tag), Some(expected), "{tag}");
}
assert_eq!(resolve_tag("ja-JP"), None);
}
}
+23 -3
View File
@@ -3,6 +3,7 @@ mod asset_scope;
mod backup;
mod commands;
mod history;
mod i18n;
mod image_proxy;
mod search;
mod state;
@@ -165,6 +166,7 @@ pub fn run() {
#[cfg(desktop)]
commands::complete_window_close,
commands::set_theme,
commands::set_language,
commands::set_system_themes,
commands::set_accent_color,
commands::save_custom_theme,
@@ -191,6 +193,9 @@ pub fn run() {
commands::create_note,
commands::duplicate_note,
commands::create_daily_note,
commands::get_daily_note_template,
commands::set_daily_note_template,
commands::import_daily_note_template,
commands::rename_note,
commands::delete_note,
commands::move_note,
@@ -248,6 +253,7 @@ pub fn run() {
commands::create_version,
commands::set_ai_settings,
commands::test_ai_connection,
commands::fetch_ai_models,
commands::ai_ask,
commands::set_sync_settings,
commands::test_sync_connection,
@@ -392,11 +398,25 @@ pub fn run() {
});
}
#[cfg(desktop)]
fn localized_tray_menu<R: tauri::Runtime>(
app: &tauri::AppHandle<R>,
language: &str,
) -> tauri::Result<tauri::menu::Menu<R>> {
let show = MenuItemBuilder::with_id("show", i18n::text(language, "native.show")).build(app)?;
let quit = MenuItemBuilder::with_id("quit", i18n::text(language, "native.quit")).build(app)?;
MenuBuilder::new(app).items(&[&show, &quit]).build()
}
#[cfg(desktop)]
fn setup_tray(app: &mut tauri::App) -> Result<(), Box<dyn std::error::Error>> {
let show = MenuItemBuilder::with_id("show", "Show HelixNotes").build(app)?;
let quit = MenuItemBuilder::with_id("quit", "Quit").build(app)?;
let menu = MenuBuilder::new(app).items(&[&show, &quit]).build()?;
let language = app
.state::<AppState>()
.config
.lock()
.map(|config| config.language.clone())
.unwrap_or_default();
let menu = localized_tray_menu(app.handle(), &language)?;
let _tray = TrayIconBuilder::with_id("helixnotes")
.icon(
+7 -3
View File
@@ -7,7 +7,7 @@
// conflict copies so nothing is ever lost.
//
// Synced set: every `*.md` in the vault tree, `.helixnotes/attachments/`,
// `.helixnotes/notebook_icons.json`, and `.helixnotes/tag_styles.json`. Search indexes,
// `.helixnotes/notebook_icons.json`, `.helixnotes/tag_styles.json` and the daily template. Search indexes,
// trash, history, other metadata, and the manifest itself remain local-only.
use crate::state::AppState;
@@ -22,6 +22,9 @@ use std::path::{Path, PathBuf};
use tauri::{Emitter, Manager};
use walkdir::WalkDir;
#[cfg(test)]
mod daily_template_tests;
/// Folder listings requested at once unless the settings say otherwise. A handful hides most of
/// the round-trip time and stays polite to a small server.
pub const DEFAULT_LISTING_PARALLELISM: usize = 6;
@@ -252,7 +255,7 @@ fn conflict_relpath(relpath: &str, stamp: &str) -> String {
// title. A note without a title of its own already takes it from the file name.
fn conflict_copy(relpath: &str, bytes: Vec<u8>, stamp: &str) -> Vec<u8> {
use crate::vault::{frontmatter, operations};
if !relpath.ends_with(".md") {
if !relpath.ends_with(".md") || relpath.starts_with(".helixnotes/") {
return bytes;
}
let Ok(raw) = std::str::from_utf8(&bytes) else {
@@ -335,7 +338,7 @@ struct LocalFile {
}
/// The synced set: `*.md` anywhere outside `.helixnotes/`, everything under
/// `.helixnotes/attachments/`, plus notebook icon and tag style mappings. Applied to BOTH local
/// `.helixnotes/attachments/`, plus the daily template, notebook icon and tag style mappings. Applied to BOTH local
/// and remote so pointing at a folder with unrelated files never imports them.
fn is_synced_relpath(rel: &str) -> bool {
// On a case-insensitive disk `.HelixNotes/history/x.md` lands in the app's own folder, so any
@@ -350,6 +353,7 @@ fn is_synced_relpath(rel: &str) -> bool {
rel.starts_with(".helixnotes/attachments/")
|| rel == ".helixnotes/notebook_icons.json"
|| rel == ".helixnotes/tag_styles.json"
|| rel == crate::vault::daily_template::RELATIVE_PATH
} else {
rel.ends_with(".md")
}
@@ -0,0 +1,38 @@
use super::{conflict_copy, is_synced_relpath, scan_local};
use crate::vault::daily_template as template;
use std::fs;
#[test]
fn sync_includes_only_the_designated_template_metadata_file() {
assert!(is_synced_relpath(template::RELATIVE_PATH));
for path in [
".helixnotes/history/daily-template.md",
".helixnotes/daily-template.json",
".helixnotes/other.md",
".HelixNotes/daily-template.md",
] {
assert!(!is_synced_relpath(path), "{path}");
}
let dir = tempfile::tempdir().unwrap();
let vault = dir.path().to_str().unwrap();
template::set(vault, Some("{{date}}\n- [ ] Task"), None).unwrap();
fs::write(dir.path().join(".helixnotes/private.md"), "Private").unwrap();
fs::write(dir.path().join("Note.md"), "Note").unwrap();
let files = scan_local(vault).unwrap();
assert_eq!(files.len(), 2);
assert!(files.contains_key(template::RELATIVE_PATH));
assert!(files.contains_key("Note.md"));
}
#[test]
fn metadata_conflict_copies_preserve_template_bytes_instead_of_rewriting_note_titles() {
let raw = b"---\nhelixnotes_daily_template_enabled: false\ntitle: Not a note\n---\n# {{title}}\n{{date}}\n".to_vec();
assert_eq!(
conflict_copy(
".helixnotes/daily-template (conflict x).md",
raw.clone(),
"x"
),
raw
);
}
+30
View File
@@ -184,6 +184,8 @@ pub struct AppConfig {
#[serde(default)]
pub active_bookmark_id: Option<String>,
pub theme: String,
/// Device-local interface preference; never written into vault data.
pub language: String,
/// Themes used when `theme` is "system": the frontend picks one by the OS color scheme.
/// Default to the plain "light"/"dark" schemes so existing configs keep their behavior.
#[serde(default = "default_system_light_theme")]
@@ -276,6 +278,10 @@ pub struct AppConfig {
pub openai_compatible_api_key: Option<String>,
#[serde(default = "default_ai_model")]
pub ai_model: String,
pub anthropic_model: Option<String>,
pub openai_model: Option<String>,
pub ollama_model: Option<String>,
pub openai_compatible_model: Option<String>,
#[serde(default)]
pub ai_writing_style: Option<String>,
#[serde(default)]
@@ -368,6 +374,7 @@ impl Default for AppConfig {
active_vault: None,
active_bookmark_id: None,
theme: "system".to_string(),
language: "system".to_string(),
system_light_theme: default_system_light_theme(),
system_dark_theme: default_system_dark_theme(),
accent_color: None,
@@ -412,6 +419,10 @@ impl Default for AppConfig {
openai_compatible_base_url: None,
openai_compatible_api_key: None,
ai_model: default_ai_model(),
anthropic_model: None,
openai_model: None,
ollama_model: None,
openai_compatible_model: None,
ai_writing_style: None,
default_view_mode: false,
new_notes_in_source_mode: false,
@@ -651,6 +662,25 @@ mod startup_view_tests {
);
}
#[test]
fn existing_configs_default_system_language() {
let mut value = serde_json::to_value(AppConfig::default()).unwrap();
value.as_object_mut().unwrap().remove("language");
let config: AppConfig = serde_json::from_value(value).unwrap();
assert_eq!(config.language, "system");
for language in crate::i18n::supported_languages()
.iter()
.map(String::as_str)
.chain(["system"])
{
let mut config = config.clone();
config.language = language.to_string();
let roundtrip: AppConfig =
serde_json::from_str(&serde_json::to_string(&config).unwrap()).unwrap();
assert_eq!(roundtrip.language, language);
}
}
#[test]
fn existing_configs_default_system_theme_pair() {
let mut value = serde_json::to_value(AppConfig::default()).unwrap();
+295
View File
@@ -0,0 +1,295 @@
//! A vault-owned Markdown template, deliberately outside the note/task/search tree.
use std::fs;
use std::io::Read;
use std::path::PathBuf;
pub const RELATIVE_PATH: &str = ".helixnotes/daily-template.md";
pub const MAX_BYTES: usize = 256 * 1024;
const DISABLED_HEADER: &str = "---\nhelixnotes_daily_template_enabled: false\n---\n";
#[derive(Debug, serde::Serialize)]
pub struct DailyTemplate {
/// The exact stored bytes, used as a compare-and-swap revision.
pub raw: Option<String>,
pub content: String,
pub enabled: bool,
}
impl DailyTemplate {
pub fn from_raw(raw: Option<String>) -> Self {
let enabled = raw.as_deref().is_some_and(is_enabled);
let content = raw.as_deref().map(body).unwrap_or_default().to_string();
Self {
raw,
content,
enabled,
}
}
}
pub fn is_enabled(raw: &str) -> bool {
let parsed = gray_matter::Matter::<super::frontmatter::BoundedYaml>::new()
.parse(raw.strip_prefix('\u{feff}').unwrap_or(raw));
parsed
.data
.and_then(|data| data.deserialize::<serde_json::Value>().ok())
.and_then(|data| {
data.get("helixnotes_daily_template_enabled")
.and_then(serde_json::Value::as_bool)
})
.unwrap_or(true)
}
fn path(vault: &str, create_parent: bool) -> Result<Option<PathBuf>, String> {
let root = fs::canonicalize(vault).map_err(|e| format!("Invalid vault: {e}"))?;
if !root.is_dir() {
return Err("Vault is not a directory".into());
}
let parent = root.join(".helixnotes");
if create_parent {
match fs::create_dir(&parent) {
Ok(()) => {}
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {}
Err(e) => return Err(e.to_string()),
}
}
match fs::symlink_metadata(&parent) {
Ok(meta) if meta.file_type().is_dir() => {}
Ok(_) => return Err("Vault metadata directory must not be a symlink or file".into()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(e) => return Err(e.to_string()),
}
let target = root.join(RELATIVE_PATH);
match fs::symlink_metadata(&target) {
Ok(meta) if !meta.file_type().is_file() => {
return Err("Daily template must be a regular file, not a symlink".into())
}
Err(e) if e.kind() != std::io::ErrorKind::NotFound => return Err(e.to_string()),
_ => {}
}
Ok(Some(target))
}
/// Bound the read itself, including Android document-provider streams and growing files.
pub fn read_from(reader: impl Read) -> Result<String, String> {
let mut bytes = Vec::new();
reader
.take((MAX_BYTES + 1) as u64)
.read_to_end(&mut bytes)
.map_err(|e| e.to_string())?;
if bytes.len() > MAX_BYTES {
return Err("Daily template exceeds the 256 KiB limit".into());
}
String::from_utf8(bytes).map_err(|_| "Daily template must be UTF-8 text".into())
}
pub fn read(vault: &str) -> Result<Option<String>, String> {
let Some(target) = path(vault, false)? else {
return Ok(None);
};
let mut options = fs::OpenOptions::new();
options.read(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
options.custom_flags(libc::O_NOFOLLOW);
}
match options.open(target) {
Ok(file) => read_from(file).map(Some),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(format!("Could not read daily template: {e}")),
}
}
/// Ignore a leading UTF-8 BOM and complete YAML frontmatter, without trimming the body.
/// An unmatched Markdown horizontal rule is ordinary body text, not metadata.
pub fn body(raw: &str) -> &str {
let raw = raw.strip_prefix('\u{feff}').unwrap_or(raw);
let mut lines = raw.split_inclusive('\n');
let Some(first) = lines.next() else {
return raw;
};
if first.trim_end_matches(['\r', '\n']) != "---" {
return raw;
}
let mut offset = first.len();
for line in lines {
offset += line.len();
if matches!(line.trim_end_matches(['\r', '\n']), "---" | "...") {
return &raw[offset..];
}
}
raw
}
/// Compare-and-swap prevents a stale Settings window from replacing another window's edit.
pub fn set(
vault: &str,
template: Option<&str>,
expected: Option<&str>,
) -> Result<Option<String>, String> {
if template.is_some_and(|text| text.len() > MAX_BYTES - DISABLED_HEADER.len()) {
return Err("Daily template exceeds the 256 KiB limit".into());
}
let current = read(vault)?;
if current.as_deref() != expected {
return Err(
"Daily template changed on disk. Reload it before saving; your draft was not written."
.into(),
);
}
let Some(target) = path(vault, template.is_some())? else {
return Ok(None);
};
if let Some(raw) = template {
let text = body(raw);
super::operations::replace_file_atomically(&target, text.as_bytes())?;
Ok(Some(text.to_string()))
} else if let Some(raw) = current {
// Disabling must not destroy the only copy of a template created in Settings.
let disabled = format!("{DISABLED_HEADER}{}", body(&raw));
if disabled.len() > MAX_BYTES {
return Err("Daily template exceeds the 256 KiB limit".into());
}
super::operations::replace_file_atomically(&target, disabled.as_bytes())?;
Ok(Some(disabled))
} else {
Ok(None)
}
}
pub fn expand(raw: &str, date: &str, title: &str) -> String {
// Values come from the selected calendar date and configured title format, not note text.
body(raw)
.replace("{{date}}", date)
.replace("{{title}}", title)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn vault_local_template_can_be_saved_replaced_and_disabled() {
let a = tempfile::tempdir().unwrap();
let b = tempfile::tempdir().unwrap();
let vault = a.path().to_str().unwrap();
assert_eq!(read(vault).unwrap(), None);
assert_eq!(
set(vault, Some("# {{title}}\n\n- [ ] Plan \n"), None)
.unwrap()
.as_deref(),
Some("# {{title}}\n\n- [ ] Plan \n")
);
assert_eq!(read(b.path().to_str().unwrap()).unwrap(), None);
let old = read(vault).unwrap();
assert_eq!(
set(vault, Some(""), old.as_deref()).unwrap(),
Some(String::new())
);
let disabled = set(vault, None, Some("")).unwrap().unwrap();
assert!(!is_enabled(&disabled));
assert_eq!(body(&disabled), "");
assert!(a.path().join(RELATIVE_PATH).exists());
assert_eq!(
set(vault, Some("enabled again"), Some(&disabled))
.unwrap()
.as_deref(),
Some("enabled again")
);
}
#[test]
fn disabling_keeps_the_saved_body_and_reenabling_restores_it() {
let dir = tempfile::tempdir().unwrap();
let vault = dir.path().to_str().unwrap();
let original = "# {{title}}\n\n- [ ] My plan \n";
set(vault, Some(original), None).unwrap();
let disabled = set(vault, None, Some(original)).unwrap().unwrap();
let state = DailyTemplate::from_raw(Some(disabled.clone()));
assert!(!state.enabled);
assert_eq!(state.content, original);
assert_eq!(
set(vault, Some(&state.content), Some(&disabled))
.unwrap()
.as_deref(),
Some(original)
);
}
#[test]
fn stale_updates_and_disables_leave_the_latest_template_untouched() {
let dir = tempfile::tempdir().unwrap();
let vault = dir.path().to_str().unwrap();
set(vault, Some("original"), None).unwrap();
set(vault, Some("newer"), Some("original")).unwrap();
assert!(set(vault, Some("stale"), Some("original")).is_err());
assert!(set(vault, None, Some("original")).is_err());
assert_eq!(read(vault).unwrap().as_deref(), Some("newer"));
}
#[test]
fn strips_only_leading_complete_frontmatter_and_preserves_body_bytes() {
let raw =
"\u{feff}---\r\nid: source\r\ntitle: Source\r\n---\r\n\r\n## Plan\r\n- [ ] Task \r\n";
assert_eq!(body(raw), "\r\n## Plan\r\n- [ ] Task \r\n");
assert_eq!(body("---\nA horizontal rule\n"), "---\nA horizontal rule\n");
assert_eq!(
body("Text\n---\nid: not metadata\n---\n"),
"Text\n---\nid: not metadata\n---\n"
);
assert_eq!(body("---\n---\n"), "");
}
#[test]
fn substitutes_only_the_two_documented_tokens() {
assert_eq!(
expand(
"# {{title}}\n{{date}} / {{date}} / {{unknown}}\n",
"2026-02-03",
"February 3, 2026"
),
"# February 3, 2026\n2026-02-03 / 2026-02-03 / {{unknown}}\n"
);
}
#[test]
fn bounds_reads_and_writes_and_requires_utf8() {
assert_eq!(
read_from(vec![b'x'; MAX_BYTES].as_slice()).unwrap().len(),
MAX_BYTES
);
assert!(read_from(vec![b'x'; MAX_BYTES + 1].as_slice()).is_err());
assert!(read_from([0xff].as_slice()).is_err());
let dir = tempfile::tempdir().unwrap();
let vault = dir.path().to_str().unwrap();
assert!(set(vault, Some(&"x".repeat(MAX_BYTES + 1)), None).is_err());
assert!(!dir.path().join(RELATIVE_PATH).exists());
}
#[cfg(unix)]
#[test]
fn refuses_linked_metadata_and_linked_templates_without_touching_the_target() {
use std::os::unix::fs::symlink;
let dir = tempfile::tempdir().unwrap();
let outside = tempfile::tempdir().unwrap();
let vault = dir.path().to_str().unwrap();
fs::write(outside.path().join("daily-template.md"), "outside").unwrap();
symlink(outside.path(), dir.path().join(".helixnotes")).unwrap();
assert!(read(vault).is_err());
assert!(set(vault, Some("bad"), None).is_err());
fs::remove_file(dir.path().join(".helixnotes")).unwrap();
fs::create_dir(dir.path().join(".helixnotes")).unwrap();
symlink(
outside.path().join("daily-template.md"),
dir.path().join(RELATIVE_PATH),
)
.unwrap();
assert!(read(vault).is_err());
assert!(set(vault, Some("bad"), None).is_err());
assert_eq!(
fs::read_to_string(outside.path().join("daily-template.md")).unwrap(),
"outside"
);
}
}
+118
View File
@@ -0,0 +1,118 @@
use super::{daily_template as template, operations};
use std::collections::HashSet;
use std::fs;
#[test]
fn daily_template_uses_the_selected_date_fresh_metadata_and_exact_body_layout() {
let root = tempfile::tempdir().unwrap();
let vault = root.path().to_str().unwrap();
let source = "---\r\nid: template-id\r\ntitle: Do not copy\r\ntags: [private]\r\npinned: true\r\ncreated: 2000-01-01\r\n---\r\n## {{title}}\r\n\r\nDate: {{date}} \r\n- [ ] Plan\r\n<table><tr><td>keep</td></tr></table>\r\n";
template::set(vault, Some(source), None).unwrap();
let entry = operations::create_daily_note(vault, Some("2020-02-29"), "long").unwrap();
assert_eq!(
entry.relative_path.replace('\\', "/"),
"Daily/2020-02-29.md"
);
assert_eq!(entry.meta.title, "February 29, 2020");
assert_ne!(entry.meta.id, "template-id");
uuid::Uuid::parse_str(&entry.meta.id).unwrap();
assert_eq!(entry.meta.tags, ["daily"]);
assert!(!entry.meta.pinned);
assert!(
entry.meta.created > chrono::DateTime::parse_from_rfc3339("2020-01-01T00:00:00Z").unwrap()
);
let raw = fs::read_to_string(&entry.path).unwrap();
assert_eq!(template::body(&raw), "## February 29, 2020\r\n\r\nDate: 2020-02-29 \r\n- [ ] Plan\r\n<table><tr><td>keep</td></tr></table>\r\n");
assert!(!raw.contains("template-id"));
assert_eq!(
template::read(vault).unwrap().as_deref(),
Some(template::body(source))
);
}
#[test]
fn existing_daily_notes_are_never_retemplated_even_if_the_template_is_broken() {
let root = tempfile::tempdir().unwrap();
let vault = root.path().to_str().unwrap();
template::set(vault, Some("First {{date}}\n"), None).unwrap();
let first = operations::create_daily_note(vault, Some("2026-06-01"), "iso").unwrap();
let original = fs::read(&first.path).unwrap();
fs::write(root.path().join(template::RELATIVE_PATH), [0xff]).unwrap();
let reopened = operations::create_daily_note(vault, Some("2026-06-01"), "eu").unwrap();
assert_eq!(reopened.meta.id, first.meta.id);
assert_eq!(fs::read(&first.path).unwrap(), original);
assert!(operations::create_daily_note(vault, Some("2026-06-02"), "iso").is_err());
assert!(!root.path().join("Daily/2026-06-02.md").exists());
}
#[test]
fn disable_and_reenable_preserve_the_template_without_touching_existing_notes() {
let root = tempfile::tempdir().unwrap();
let vault = root.path().to_str().unwrap();
let saved = template::set(vault, Some("{{title}} — {{date}}\n"), None).unwrap();
let first = operations::create_daily_note(vault, Some("2026-07-01"), "iso").unwrap();
let before = fs::read(&first.path).unwrap();
let disabled = template::set(vault, None, saved.as_deref()).unwrap();
let blank = operations::create_daily_note(vault, Some("2026-07-02"), "iso").unwrap();
assert_eq!(
template::body(&fs::read_to_string(blank.path).unwrap()),
"\n"
);
let state = template::DailyTemplate::from_raw(disabled.clone());
assert!(!state.enabled);
template::set(vault, Some(&state.content), disabled.as_deref()).unwrap();
let next = operations::create_daily_note(vault, Some("2026-07-03"), "iso").unwrap();
assert_eq!(
template::body(&fs::read_to_string(next.path).unwrap()),
"2026-07-03 — 2026-07-03\n"
);
assert_eq!(fs::read(&first.path).unwrap(), before);
}
#[test]
fn concurrent_daily_creation_publishes_one_complete_note_and_cleans_temporary_files() {
let root = tempfile::tempdir().unwrap();
let vault = root.path().to_str().unwrap();
template::set(vault, Some("# {{title}}\n\n{{date}}\n"), None).unwrap();
let entries = std::thread::scope(|scope| {
let workers: Vec<_> = (0..6)
.map(|_| {
scope.spawn(|| {
operations::create_daily_note(vault, Some("2026-08-01"), "iso").unwrap()
})
})
.collect();
workers
.into_iter()
.map(|worker| worker.join().unwrap())
.collect::<Vec<_>>()
});
assert_eq!(
entries
.iter()
.map(|entry| &entry.meta.id)
.collect::<HashSet<_>>()
.len(),
1
);
assert_eq!(fs::read_dir(root.path().join("Daily")).unwrap().count(), 1);
assert_eq!(
template::body(&fs::read_to_string(&entries[0].path).unwrap()),
"# 2026-08-01\n\n2026-08-01\n"
);
}
#[test]
fn template_checkboxes_do_not_become_notes_or_tasks_and_vaults_are_independent() {
let a = tempfile::tempdir().unwrap();
let b = tempfile::tempdir().unwrap();
let vault = a.path().to_str().unwrap();
template::set(vault, Some("- [ ] Template-only task\n"), None).unwrap();
assert!(operations::scan_notes(vault, None).unwrap().is_empty());
let note = operations::create_daily_note(b.path().to_str().unwrap(), Some("2026-01-01"), "iso")
.unwrap();
assert_eq!(
template::body(&fs::read_to_string(note.path).unwrap()),
"\n"
);
}
+3
View File
@@ -1,3 +1,4 @@
pub mod daily_template;
pub mod frontmatter;
pub mod import;
pub mod operations;
@@ -5,5 +6,7 @@ pub mod operations;
mod portal_watcher;
pub mod watcher;
#[cfg(test)]
mod daily_template_tests;
#[cfg(test)]
mod note_import_tests;
+22 -11
View File
@@ -1132,8 +1132,10 @@ pub fn create_daily_note(
};
let dir = Path::new(vault_path).join("Daily");
if fs::symlink_metadata(&dir).is_err() {
fs::create_dir(&dir).map_err(|e| e.to_string())?;
match fs::create_dir(&dir) {
Ok(()) => {}
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {}
Err(error) => return Err(error.to_string()),
}
let dir = ensure_vault_content_dir(vault_path, &dir)?;
let file_path = dir.join(format!("{}.md", date_str));
@@ -1162,14 +1164,23 @@ pub fn create_daily_note(
modified: now,
};
let raw = frontmatter::update_note_raw(&meta, "\n");
match fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&file_path)
{
Ok(mut file) => file.write_all(raw.as_bytes()).map_err(|e| e.to_string())?,
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
let body = super::daily_template::read(vault_path)?
.filter(|raw| super::daily_template::is_enabled(raw))
.map(|raw| super::daily_template::expand(&raw, &date_str, &meta.title))
.unwrap_or_else(|| "\n".to_string());
// A template owns its body layout: do not insert another heading or copy its metadata.
let raw = format!("{}{}", frontmatter::serialize_frontmatter(&meta), body);
let mut temporary = tempfile::Builder::new()
.prefix(".helixnotes-daily-")
.tempfile_in(&dir)
.map_err(|e| e.to_string())?;
temporary
.write_all(raw.as_bytes())
.map_err(|e| e.to_string())?;
temporary.as_file().sync_all().map_err(|e| e.to_string())?;
match temporary.persist_noclobber(&file_path) {
Ok(_) => {}
Err(error) if error.error.kind() == std::io::ErrorKind::AlreadyExists => {
if fs::symlink_metadata(&file_path)
.map_err(|e| e.to_string())?
.file_type()
@@ -1180,7 +1191,7 @@ pub fn create_daily_note(
ensure_note_path(vault_path, &file_path)?;
return read_note_entry(&file_path, vault_root);
}
Err(error) => return Err(error.to_string()),
Err(error) => return Err(error.error.to_string()),
}
let relative = file_path