diff --git a/.gitlab/issue_templates/Translation.md b/.gitlab/issue_templates/Translation.md new file mode 100644 index 0000000..3e5a28b --- /dev/null +++ b/.gitlab/issue_templates/Translation.md @@ -0,0 +1,33 @@ +## Language + +- English name: +- Native name (as it should appear in the language selector): +- Language/region code (for example `de`, `fr`, `pt-BR`): +- Text direction: left-to-right / right-to-left + +## Translation file + +Attach the translated `.json` file here, or a ZIP containing it. Start with the complete English catalog, not the short example. + +[Translation guide](https://gitlab.com/ArkHost/HelixNotes/-/blob/main/docs/translations/README.md) · [English catalog](https://gitlab.com/ArkHost/HelixNotes/-/blob/main/src/lib/i18n/locales/en.json) + +- English catalog version or Git commit used: +- Complete translation or partial? Which sections remain in English? + +## Checks + +- [ ] Message keys and argument names such as `{value0}` and `{count}` are unchanged. +- [ ] Plural messages keep an `other` branch and include the forms this language needs. +- [ ] The file is valid UTF-8 JSON, without comments or trailing commas. +- [ ] Untranslated values are left in English rather than removed. +- [ ] I wrote this translation or have permission to contribute it. + +No build or merge request is required. The maintainer will validate and integrate the translation. + +## Notes + +Terminology questions, regional differences, screenshots or clipped labels, if any: + +## Credit (optional) + +Name or handle to credit, or leave blank: diff --git a/README.md b/README.md index 20ae0fb..d481066 100644 --- a/README.md +++ b/README.md @@ -164,10 +164,13 @@ All releases: [gitlab.com/ArkHost/HelixNotes/-/releases](https://gitlab.com/ArkH - External `.md` viewer mode with import-to-vault flow - PDF preview, Obsidian import, "Show in File Manager" - AI writing tools (Ollama / OpenAI-compatible / Anthropic / OpenAI) +- [Provider model discovery and opt-in image input](docs/ai-models-and-images.md) (1.3.8, unreleased) - **Optional WebDAV sync** to your own server (Nextcloud, ownCloud, a NAS): manual or automatic, with keep-both conflict copies - Version history with main-area diffs and rendered read-only snapshots, automatic backups - Multi-window, file associations, focus mode, view mode - Themes (light, dark, and built-in palettes), accent colors, fonts including bundled Ubuntu and Ubuntu Mono, 80-200% interface scale +- [Daily-note templates](docs/daily-note-templates.md), with per-vault Markdown import/editing and date/title placeholders, applied only to new notes +- Interface languages: English, Russian, Dutch, German, Spanish, French, Italian, Portuguese (Portugal) and Portuguese (Brazil), with live switching - Local plain-text files, no company cloud ### Version history @@ -192,6 +195,12 @@ With **Close to tray** enabled and a tray icon available, closing the main windo Full documentation: [helixnotes.com/docs](https://helixnotes.com/docs.html) +## Contributing translations + +Want HelixNotes in your language? Copy the [English JSON catalog](src/lib/i18n/locales/en.json), translate its values, and attach the file to a GitLab issue using the **Translation** template. No build or programming experience is required. + +See the [translation guide and example file](docs/translations/README.md) for placeholders, plural forms and submission instructions. + ## Tech Stack - **Frontend**: SvelteKit (Svelte 5) + TailwindCSS v4 + TipTap v3 diff --git a/docs/ai-models-and-images.md b/docs/ai-models-and-images.md new file mode 100644 index 0000000..ca852e5 --- /dev/null +++ b/docs/ai-models-and-images.md @@ -0,0 +1,56 @@ +# AI models and image input + +These changes are planned for 1.3.8 and are not in the published 1.3.7 packages. + +## Choose a model + +In Settings → AI, select OpenAI or Anthropic and enter an API key. HelixNotes fetches the provider's model catalog after a short pause. **Refresh models** fetches it again. + +Catalog requests send the key to that provider's fixed HTTPS models endpoint. They do not send notes or generate a paid completion. Anthropic pagination is handled automatically. Catalog responses have size, count and timeout limits; redirects are not followed. Keys and provider error bodies are not included in catalog error messages. + +Fetching a catalog never changes the selected model. Choose an entry or enter its ID in **Model ID (manual fallback)**. A saved ID remains available even if it is absent from the catalog or discovery fails. Model choices are remembered separately for each provider. Turning AI off retains saved keys and choices but disables AI requests. + +OpenAI's catalog does not consistently describe chat or vision support. HelixNotes filters out recognizably non-chat model families, but a listed model can still be unavailable to your account or incompatible with the chat endpoint. Manual IDs remain available for models the filter misses. There are no automatic model substitutions or paid retries. + +Ollama and OpenAI-compatible servers keep their manual model-ID and base-URL settings. Requests go to that URL, which is not necessarily on this device. Use the final server URL; AI requests do not follow redirects. **Test connection** is a separate, explicit request that generates a short reply and may incur provider charges. + +## Include images + +Open AI Actions in a note, with or without a text selection. **Images from this note** lists eligible local PNG, JPEG and WebP file references, including managed attachments. All checkboxes start unchecked each time the menu opens. + +1. Check up to four images. The list covers the current note, even when only some text is selected. +2. Review the disclosure showing the destination, model and number of images. +3. Choose an AI action, **Describe selected images**, or a custom prompt. + +Only checked files are included. An image elsewhere in the note is not sent unless you check it. Selecting checkboxes alone does not start an AI request. Remote URLs, data-URI images, PDFs, GIFs and other unsupported files are not offered as image inputs. The AI backend never fetches image URLs. + +The chosen model and server must accept image input. OpenAI, Anthropic, and compatible vision models on Ollama/OpenAI-compatible servers receive actual image content blocks, not just Markdown links. Unsupported requests fail visibly; HelixNotes does not silently retry them without images. + +### File and request limits + +- Images must resolve inside the note's configured vault. Private hidden paths outside `.helixnotes/attachments` are rejected. +- Each source file is limited to 10 MiB and 20 megapixels. Reads, decoder allocation and encoded output are bounded. +- Images are oriented, reduced to at most 1,568 pixels on either side, and re-encoded in memory as PNG or JPEG. Small images are not enlarged. Encoded files are limited to 4 MiB each. +- Only a still-image rendition is sent, not an animation sequence. This is not PDF, audio or video analysis. +- Re-encoding omits source-file metadata. Source files, filenames and note references are not rewritten. Small text or fine detail can become less readable after resizing. +- Prompt text and the custom prompt together are limited to 2 MiB. Images can increase token usage and cost. + +Image requests check the provider, model and configured server against the settings shown in the requesting window. If those settings have changed, the request is rejected rather than sent to an unexpected destination. + +## Review before applying + +Rich-editor selections retain their Markdown structure, including image-only selections. Source-mode requests send the selected text, or the whole note when the selection is empty. + +Images, reference definitions and PDF embeds use protected placeholders. An AI response that removes or duplicates a placeholder cannot be applied. A result also cannot be applied after switching or editing the originating note. Applying to an existing note requires a successful save and history snapshot first. + +Only successfully completed text responses can be applied. OpenAI-compatible streams must report `finish_reason: "stop"` followed by `[DONE]`. Anthropic streams must report `end_turn` or `stop_sequence` followed by `message_stop`. Missing completion events, malformed payloads, output limits, refusals and tool requests block application, even if partial text is visible. Compatible servers must follow that completion contract; HelixNotes does not treat a dropped connection as success. + +Streaming errors use fixed application messages rather than provider error bodies. Connection errors omit server URLs, which may contain credentials or query secrets. + +Requests and attachment selection do not themselves edit notes or create history versions. Review the generated text before applying it. Closing the menu stops displaying the response; it does not promise cancellation of a request already sent to the provider. + +## Verification and release checks + +Automated tests cover provider payload shapes, catalog parsing/pagination, key-safe errors, bounded image preparation, vault/private-file checks, resizing, unchanged source bytes, media restoration, and Unicode split across streaming chunks. Loopback HTTP tests also exercise the production stream receiver for successful completion, malformed events, unsuccessful finish reasons, missing terminators, disconnections, output bounds and sanitized provider errors. Browser tests use a mocked native boundary for model settings and both editor modes. + +No paid provider inference calls were used for these checks. Before release, test with real provider accounts, supported vision models, rebuilt desktop/mobile apps and representative screenshots. Browser mocks do not prove provider acceptance, native webview behavior or model accuracy. diff --git a/docs/daily-note-templates.md b/docs/daily-note-templates.md new file mode 100644 index 0000000..23367a1 --- /dev/null +++ b/docs/daily-note-templates.md @@ -0,0 +1,65 @@ +# Daily-note templates + +Open **Settings → General → Daily note template** in the vault you want to configure. + +- **Create template** opens a Markdown editor. Save to enable it. +- **Import Markdown file…** imports a copy of a UTF-8 `.md` file. The source file is not changed. Later edits to the source are not imported automatically. +- **Edit** changes the saved copy. Saving enables the template. +- **Disable template** stops applying it without deleting its contents. **Enable template** uses that saved body again. +- **Reload from disk** reads changes made in another window, by sync or in an external editor. Cancel editing first; copy any unsaved draft you want to keep before cancelling. + +## Example + +```markdown +# {{title}} + +Date: {{date}} + +## Priorities +- [ ] + +## Notes + +## End-of-day review +``` + +Only two placeholders are supported: + +| Placeholder | Value | +| --- | --- | +| `{{date}}` | The selected calendar date as `YYYY-MM-DD`. This can be a past or future date, not necessarily today. | +| `{{title}}` | The daily-note title using the format selected in Settings. | + +Placeholders are case-sensitive. Unknown placeholders remain literal text. There are no scripts or executable expressions. + +The body layout is preserved, including headings, blank lines and Markdown hard breaks. No extra title heading is inserted. Links and attachment paths are copied as written; importing a template does not import its attachments or rewrite its links. + +## What stays unchanged + +The template applies only when creating a daily note. Opening an existing daily note never replaces or appends to its contents, even if the template has since changed or cannot be read. + +Imported frontmatter is not copied. Each new daily note receives its own ID, timestamps, configured title and `daily` tag. Changing the interface language does not translate or rewrite the saved template or its draft. + +The template must fit within 256 KiB, including its stored metadata. Invalid UTF-8, oversized files and disk errors are reported instead of silently creating a partial note. Concurrent creation of the same day publishes one complete note without overwriting an existing file. + +## Storage, sync and backups + +The saved template belongs to the vault, not the device-wide application settings: + +```text +.helixnotes/daily-template.md +``` + +It is excluded from normal note lists, search and Tasks. Its checklist items appear in Tasks only after they have been copied into an actual daily note. + +Disabling retains the body in the same file, with this frontmatter flag: + +```yaml +helixnotes_daily_template_enabled: false +``` + +The file is included in the next WebDAV sync. Conflicting copies retain their original template text and are kept under `.helixnotes`; they are not automatically chosen as the active template. + +New backups include the saved template even when attachments are excluded. Restoring a new backup restores its template state too, including whether it was disabled or absent. Older backups that do not record template state leave the current template alone. + +Saving checks that the file still matches the version loaded by Settings. If it changed elsewhere, the save is rejected and your draft remains in the editor. Reload the latest copy before deciding what to save. diff --git a/docs/translations/README.md b/docs/translations/README.md new file mode 100644 index 0000000..ff4ffcb --- /dev/null +++ b/docs/translations/README.md @@ -0,0 +1,68 @@ +# Translate HelixNotes + +You can contribute a translation without building the app or writing code. Translate a JSON file and attach it to a [GitLab issue](https://gitlab.com/ArkHost/HelixNotes/-/issues/new). + +## Files to use + +- **Full template:** [`en.json`](../../src/lib/i18n/locales/en.json). Copy this file to start a translation. Use GitLab's **Open raw** or download button to save the JSON, not the surrounding web page. +- **Short example:** [`example.json`](example.json). This shows the format, but is not a complete language file. +- **Existing translation:** [`ru.json`](../../src/lib/i18n/locales/ru.json), including Russian plural forms. + +The English file is the reference. Download the latest version before you start, and note the app version or Git commit you used. You can also improve an existing translation. See [`languages.json`](../../src/lib/i18n/languages.json) for the supported language codes and native names. + +Portuguese has separate [`pt-PT.json`](../../src/lib/i18n/locales/pt-PT.json) and [`pt-BR.json`](../../src/lib/i18n/locales/pt-BR.json) catalogs. Keep regional wording such as *ficheiro/arquivo*, *guardar/salvar* and *palavra-passe/senha* appropriate to each variant. + +## Translate the values + +Name your copy after the language code, for example `de.json` for German or `pt-BR.json` for Brazilian Portuguese. Save it as UTF-8. + +Keep the keys on the left unchanged. Translate the text on the right: + +```json +{ + "ui.save": "Speichern", + "ui.cancel": "Abbrechen", + "ui.create_note": "Notiz „{value0}“ erstellen", + "count.notes": "{count, plural, one {# Notiz} other {# Notizen}}" +} +``` + +Keep argument names such as `{value0}`, `{title}`, `{error}` and `{count}` unchanged. You can move them within the sentence. The app replaces them with actual values. + +For plural messages, translate every branch and keep `other`. Add any plural categories your language needs: `zero`, `one`, `two`, `few`, `many`, `other`. Russian, for example: + +```json +{ + "count.notes": "{count, plural, one {# заметка} few {# заметки} many {# заметок} other {# заметки}}" +} +``` + +`#` inserts the count. Do not replace it with a fixed number. + +### Keep these unchanged + +- Message keys and argument names. +- Shortcut codes such as `Ctrl`, `Shift`, `Enter` and `Alt`. +- Product names, URLs, file extensions and literal Markdown syntax where they identify an actual format or command. +- JSON punctuation. Use double quotes; escape a double quote inside a value as `\"`. JSON does not support comments or trailing commas. + +Use consistent terminology for notes, notebooks, vaults, tags and backups. Prefer clear wording over a literal translation. Do not add HTML to messages. ICU uses apostrophes for quoting; a literal apostrophe can be escaped as `''` when needed. + +An incomplete translation is welcome. Leave untranslated values in English instead of deleting their keys, and say which sections remain unfinished. + +## Submit through a GitLab issue + +1. Open a new issue and select the **Translation** description template. +2. Use a title such as `Translation: German (de)`. +3. Give the English language name, native name and language/region code. +4. Attach your translated JSON file. If GitLab refuses the extension, attach a ZIP containing it. +5. State which English catalog version you used, whether the translation is complete, and any wording or layout concerns. +6. Say how you would like to be credited, if at all. + +You do not need to make a merge request or edit the app's language selector. The maintainer reviews the file, runs catalog checks, registers the language and includes it in a future release. Attaching a file to an issue does not immediately install it in the app. + +If you can test a development build, screenshots of clipped labels are useful. If you cannot, submit the file anyway. Right-to-left languages also need layout testing before release. + +## Maintainer checks + +See the [implementation guide](../../src/lib/i18n/README.md) for registering a language and running validation. Check catalog keys, ICU syntax, argument names, plural forms, native labels and layout before shipping. Do not modify user notes as part of language integration. diff --git a/docs/translations/example.json b/docs/translations/example.json new file mode 100644 index 0000000..0372bc6 --- /dev/null +++ b/docs/translations/example.json @@ -0,0 +1,10 @@ +{ + "language.title": "Language", + "language.description": "Choose the interface language. Your notes are not translated.", + "language.system": "System default", + "ui.save": "Save", + "ui.cancel": "Cancel", + "ui.settings": "Settings", + "ui.create_note": "Create note \"{value0}\"", + "count.notes": "{count, plural, one {# note} other {# notes}}" +} diff --git a/package.json b/package.json index 8d4b63a..0a7f004 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,7 @@ "diff": "^9.0.0", "dompurify": "^3.4.13", "highlight.js": "^11.11.1", + "intl-messageformat": "^11.2.15", "katex": "^0.16.28", "lowlight": "^3.3.0", "markdown-it": "^14.3.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d733976..4d92aca 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -119,6 +119,9 @@ importers: highlight.js: specifier: ^11.11.1 version: 11.11.1 + intl-messageformat: + specifier: ^11.2.15 + version: 11.2.15 katex: specifier: ^0.16.28 version: 0.16.28 @@ -385,6 +388,15 @@ packages: cpu: [x64] os: [win32] + '@formatjs/fast-memoize@3.1.7': + resolution: {integrity: sha512-zXfhLpvA6T7+efdt9JLbBwZ00tT7NsBMDVnDu8rpHeNNv8KfRZAMo2gkG0k9lK/Nzc//3kJ9pImsfuJxk3KhUA==} + + '@formatjs/icu-messageformat-parser@3.5.18': + resolution: {integrity: sha512-wX1efcL8d7K5QapOTRsP/OLTA/kpwe4LwqWyNxDNg5iRs8i9jJbCyxB5P8jc4KOW5oiLzz2DJr+BRY5xMV9+bA==} + + '@formatjs/icu-skeleton-parser@2.1.11': + resolution: {integrity: sha512-j8cUmOJzVgkHuS0QiQ6ga76UIoLOFSAMWhs7aZJztH3aAdCOAE6vpC8KVvFB4cU10ON0y2/5oOVmPJ43s2lTwA==} + '@iconify/types@2.0.0': resolution: {integrity: sha512-+wluvCrRhXrhyOmRDJ3q8mux9JkKy5SJ/v8ol2tu4FVjyYvtEzkc/3pK15ET6RKg4b4w4BmTk1+gsCUhf21Ykg==} @@ -1455,6 +1467,9 @@ packages: resolution: {integrity: sha512-5Hh7Y1wQbvY5ooGgPbDaL5iYLAPzMTUrjMulskHLH6wnv/A+1q5rgEaiuqEjB+oxGXIVZs1FF+R/KPN3ZSQYYg==} engines: {node: '>=12'} + intl-messageformat@11.2.15: + resolution: {integrity: sha512-W4VNytsXyWgW5lvVchmsb9xgA5rSKqoflu3/IQ9UCtB0z4R+Bx9MoEkwFT/+30RCl/KzUoH0QGGe9lppF5SuvQ==} + is-reference@3.0.3: resolution: {integrity: sha512-ixkJoqQvAP88E6wLydLGGqCJsrFUnqoH6HnaczB8XmDH1oaWU+xxdptvikTgaEhtZ53Ky6YXiBuUI2WXLMCwjw==} @@ -2026,6 +2041,14 @@ snapshots: '@esbuild/win32-x64@0.28.2': optional: true + '@formatjs/fast-memoize@3.1.7': {} + + '@formatjs/icu-messageformat-parser@3.5.18': + dependencies: + '@formatjs/icu-skeleton-parser': 2.1.11 + + '@formatjs/icu-skeleton-parser@2.1.11': {} + '@iconify/types@2.0.0': {} '@iconify/utils@3.1.4': @@ -3031,6 +3054,11 @@ snapshots: internmap@2.0.3: {} + intl-messageformat@11.2.15: + dependencies: + '@formatjs/fast-memoize': 3.1.7 + '@formatjs/icu-messageformat-parser': 3.5.18 + is-reference@3.0.3: dependencies: '@types/estree': 1.0.9 diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 6128b1a..21459d4 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -1780,6 +1780,7 @@ version = "1.3.7" dependencies = [ "aho-corasick", "arboard", + "base64 0.22.1", "chrono", "dirs", "fs4", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index a45a6d9..2cedef7 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -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"] } diff --git a/src-tauri/src/ai.rs b/src-tauri/src/ai.rs index 53437c4..c9607a8 100644 --- a/src-tauri/src/ai.rs +++ b/src-tauri/src/ai.rs @@ -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::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, + images: Vec, ) { 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::().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::(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::(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 { - 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 { .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 { - 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, model: &str) -> Result 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 { + // 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 { + 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, 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(¬e, "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(), ¬e, &[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(), ¬e, &[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(), + ¬e, + &[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(), ¬e, &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(), + ¬e, + &[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(), + ¬e, + &[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(), + ¬e, + 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(), ¬e, &[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(), + ¬e, + 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(), + ¬e, + &[link.to_string_lossy().into_owned()] + ) + .is_err()); + } +} diff --git a/src-tauri/src/ai/models.rs b/src-tauri/src/ai/models.rs new file mode 100644 index 0000000..b5cce7b --- /dev/null +++ b/src-tauri/src/ai/models.rs @@ -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, +} + +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, 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, 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, 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 = 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::>(), + ["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(); + } +} diff --git a/src-tauri/src/ai/sse.rs b/src-tauri/src/ai/sse.rs new file mode 100644 index 0000000..0b1fc5e --- /dev/null +++ b/src-tauri/src/ai/sse.rs @@ -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, + 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, 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()); + } +} diff --git a/src-tauri/src/ai/stream.rs b/src-tauri/src/ai/stream.rs new file mode 100644 index 0000000..29c597b --- /dev/null +++ b/src-tauri/src/ai/stream.rs @@ -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, 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, 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, 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; diff --git a/src-tauri/src/ai/stream_tests.rs b/src-tauri/src/ai/stream_tests.rs new file mode 100644 index 0000000..7758f1a --- /dev/null +++ b/src-tauri/src/ai/stream_tests.rs @@ -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()); +} diff --git a/src-tauri/src/backup.rs b/src-tauri/src/backup.rs index d39f0de..b7d9014 100644 --- a/src-tauri/src/backup.rs +++ b/src-tauri/src/backup.rs @@ -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, } fn vault_id(vault: &Path) -> Result { @@ -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, ) -> 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")) diff --git a/src-tauri/src/backup/daily_template_tests.rs b/src-tauri/src/backup/daily_template_tests.rs new file mode 100644 index 0000000..bfb26e7 --- /dev/null +++ b/src-tauri/src/backup/daily_template_tests.rs @@ -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()); +} diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 8f1514f..3ab80f7 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -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( window.destroy().map_err(|error| error.to_string()) } +#[tauri::command] +pub fn set_language( + app: AppHandle, + 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 { + 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, + expected_template: Option, +) -> Result { + 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( + app: AppHandle, + state: State<'_, AppState>, + vault_path: String, + title: String, + expected_template: Option, +) -> Result, 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( app: AppHandle, @@ -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, 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, +} + +#[derive(serde::Deserialize)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub struct AiRequestConfig { + provider: Option, + model: String, + base_url: Option, +} + #[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, openai_compatible_api_key: Option, ) -> 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, request_id: String, + image_context: Option, + expected_config: Option, ) -> 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::(); 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::(); + 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(()) } diff --git a/src-tauri/src/i18n.rs b/src-tauri/src/i18n.rs new file mode 100644 index 0000000..1e5c2b7 --- /dev/null +++ b/src-tauri/src/i18n.rs @@ -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> = OnceLock::new(); + LANGUAGES.get_or_init(|| { + let registry: Vec = + 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>; + +#[cfg(any(desktop, test))] +fn catalogs() -> &'static Catalogs { + static CATALOGS: OnceLock = 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); + } +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 77a0e02..39bfe41 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -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( + app: &tauri::AppHandle, + language: &str, +) -> tauri::Result> { + 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> { - 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::() + .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( diff --git a/src-tauri/src/sync.rs b/src-tauri/src/sync.rs index 6c3bde7..9a9ce18 100644 --- a/src-tauri/src/sync.rs +++ b/src-tauri/src/sync.rs @@ -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, stamp: &str) -> Vec { 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") } diff --git a/src-tauri/src/sync/daily_template_tests.rs b/src-tauri/src/sync/daily_template_tests.rs new file mode 100644 index 0000000..0b02d28 --- /dev/null +++ b/src-tauri/src/sync/daily_template_tests.rs @@ -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 + ); +} diff --git a/src-tauri/src/types.rs b/src-tauri/src/types.rs index 76a5346..d3265ef 100644 --- a/src-tauri/src/types.rs +++ b/src-tauri/src/types.rs @@ -184,6 +184,8 @@ pub struct AppConfig { #[serde(default)] pub active_bookmark_id: Option, 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, #[serde(default = "default_ai_model")] pub ai_model: String, + pub anthropic_model: Option, + pub openai_model: Option, + pub ollama_model: Option, + pub openai_compatible_model: Option, #[serde(default)] pub ai_writing_style: Option, #[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(); diff --git a/src-tauri/src/vault/daily_template.rs b/src-tauri/src/vault/daily_template.rs new file mode 100644 index 0000000..ebba47f --- /dev/null +++ b/src-tauri/src/vault/daily_template.rs @@ -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, + pub content: String, + pub enabled: bool, +} + +impl DailyTemplate { + pub fn from_raw(raw: Option) -> 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::::new() + .parse(raw.strip_prefix('\u{feff}').unwrap_or(raw)); + parsed + .data + .and_then(|data| data.deserialize::().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, 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 { + 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, 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, 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" + ); + } +} diff --git a/src-tauri/src/vault/daily_template_tests.rs b/src-tauri/src/vault/daily_template_tests.rs new file mode 100644 index 0000000..635bcbe --- /dev/null +++ b/src-tauri/src/vault/daily_template_tests.rs @@ -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
keep
\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
keep
\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::>() + }); + assert_eq!( + entries + .iter() + .map(|entry| &entry.meta.id) + .collect::>() + .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" + ); +} diff --git a/src-tauri/src/vault/mod.rs b/src-tauri/src/vault/mod.rs index 25fd077..9baed28 100644 --- a/src-tauri/src/vault/mod.rs +++ b/src-tauri/src/vault/mod.rs @@ -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; diff --git a/src-tauri/src/vault/operations.rs b/src-tauri/src/vault/operations.rs index cec5d9f..0385636 100644 --- a/src-tauri/src/vault/operations.rs +++ b/src-tauri/src/vault/operations.rs @@ -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 diff --git a/src/lib/api.ts b/src/lib/api.ts index a5f3a29..9c6fb1e 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -1,4 +1,5 @@ import { invoke } from "@tauri-apps/api/core"; +import type { Language } from './i18n/core'; import type { ExternalNoteReference } from "./editor/externalNote"; import type { AppConfig, @@ -52,6 +53,10 @@ export async function cancelWindowClose(): Promise { return invoke("cancel_window_close"); } +export async function setLanguage(language: Language): Promise { + return invoke("set_language", { language }); +} + export async function setTheme(theme: string): Promise { return invoke("set_theme", { theme }); } @@ -191,6 +196,24 @@ export async function createDailyNote(vaultPath: string, date?: string): Promise return invoke("create_daily_note", { vaultPath, date: date ?? null }); } +export interface DailyNoteTemplate { + raw: string | null; + content: string; + enabled: boolean; +} + +export async function getDailyNoteTemplate(vaultPath: string): Promise { + return invoke("get_daily_note_template", { vaultPath }); +} + +export async function setDailyNoteTemplate(vaultPath: string, template: string | null, expectedTemplate: string | null): Promise { + return invoke("set_daily_note_template", { vaultPath, template, expectedTemplate }); +} + +export async function importDailyNoteTemplate(vaultPath: string, title: string, expectedTemplate: string | null): Promise { + return invoke("import_daily_note_template", { vaultPath, title, expectedTemplate }); +} + export async function renameNote( vaultPath: string, path: string, @@ -610,13 +633,23 @@ export async function testAiConnection(): Promise { return invoke("test_ai_connection"); } +export interface AiModel { id: string; name: string; vision: boolean | null } +export interface AiImageContext { vaultPath: string; notePath: string; paths: string[] } +export interface AiRequestConfig { provider: string | null; model: string; baseUrl: string | null } + +export async function fetchAiModels(provider: string, apiKey: string): Promise { + return invoke("fetch_ai_models", { provider, apiKey }); +} + export async function aiAsk( action: string, text: string, customPrompt: string | null, requestId: string, + imageContext: AiImageContext | null = null, + expectedConfig: AiRequestConfig | null = null, ): Promise { - return invoke("ai_ask", { action, text, customPrompt, requestId }); + return invoke("ai_ask", { action, text, customPrompt, requestId, imageContext, expectedConfig }); } export async function getInstallType(): Promise { diff --git a/src/lib/components/AiImageOptions.svelte b/src/lib/components/AiImageOptions.svelte new file mode 100644 index 0000000..e182d54 --- /dev/null +++ b/src/lib/components/AiImageOptions.svelte @@ -0,0 +1,36 @@ + + +
+

{$t('ai.vision.title')}

+

{$t('ai.vision.help')}

+ {#if images.length} +
+ {#each images as image (image.path)} + + {/each} +
+ {:else}

{$t('ai.vision.none')}

{/if} + {#if selected.length}

{$t('ai.vision.privacy', { provider: destination, count: selected.length })}

{/if} +
+ + diff --git a/src/lib/components/AiModelPicker.svelte b/src/lib/components/AiModelPicker.svelte new file mode 100644 index 0000000..2ba6684 --- /dev/null +++ b/src/lib/components/AiModelPicker.svelte @@ -0,0 +1,65 @@ + + +
+

{$t('ai.models.help')}

+ {#if options.length}{/if} + + {#if !key}

{$t('ai.models.needKey')}

{/if} + {#if fetched && !models.length}

{$t('ai.models.empty')}

{/if} + {#if error}{/if} + + { if (draft.trim() !== model) void choose(draft); }} onkeydown={(e) => { if (e.key === 'Enter') { e.preventDefault(); void choose(draft); } }} /> +
+ + diff --git a/src/lib/components/AppLayout.svelte b/src/lib/components/AppLayout.svelte index 655f834..8566302 100644 --- a/src/lib/components/AppLayout.svelte +++ b/src/lib/components/AppLayout.svelte @@ -1,4 +1,6 @@ + +
+

{$t('dailyTemplate.title')}

+

{$t('dailyTemplate.description')}

+

{$t('dailyTemplate.tokens', { dateToken: '{{date}}', titleToken: '{{title}}' })}

+ {#if loading} +

{$t('ui.loading')}

+ {:else if loaded} +

{$t(value.enabled ? 'dailyTemplate.enabled' : value.raw === null ? 'dailyTemplate.none' : 'dailyTemplate.disabled')}

+
+ + + {#if value.raw !== null} + + {/if} + +
+ {/if} + {#if editing} + + +
+ + +
+ {/if} + {#if error} + + {#if !loaded}{/if} + {/if} + {#if message}

{$t(message)}

{/if} +

{$t('dailyTemplate.storage')} .helixnotes/daily-template.md

+
+ + diff --git a/src/lib/components/Editor.svelte b/src/lib/components/Editor.svelte index 6b1fdd7..c781cb8 100644 --- a/src/lib/components/Editor.svelte +++ b/src/lib/components/Editor.svelte @@ -1,4 +1,7 @@ {#snippet tableSizePicker()} -