commit 884e275b0e1160642a11a66918cbfc1a2e26336e Author: flemming-it Date: Mon May 25 23:02:44 2026 +0200 feat: studio.translate.ollama v0.1.0 — Ollama-backed translate hook First plugin to exercise the Studio-plugin translate hook end-to-end. Routes through an Ollama-shaped /api/chat endpoint configured via OLLAMA_ENDPOINT (default http://localhost:11434/api/chat). System prompt constrains the model to ONLY the translation — no commentary, no markdown frames — so the translate hook contract (plain string in, plain string out) stays honest. Declared permissions: net: localhost + net: 127.0.0.1 + net: ollama. Operator policy extends the ceiling for remote / cloud LLM endpoints. v0.1 limits: no glossary, no batching, no caching. Same SDK v0.1 single-world contract — declares Declined stubs for the theme and output-view hooks. Built artefact: ~108 KiB stripped wasm. Uploaded to releases.fai.flemming.ai (sha256 0101ed6e…) and reachable via `fai install studio.translate.ollama`. Signed-off-by: flemming-it diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fcc55a5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +target/ +**/*.rs.bk +.DS_Store +module.wasm diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..71a885d --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,602 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "ahash" +version = "0.8.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75" +dependencies = [ + "cfg-if", + "once_cell", + "version_check", + "zerocopy", +] + +[[package]] +name = "anyhow" +version = "1.0.102" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c" + +[[package]] +name = "bitflags" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3" + +[[package]] +name = "bytes" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33" + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "fai-studio-plugin-sdk" +version = "0.1.0" +dependencies = [ + "wit-bindgen 0.36.0", +] + +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +dependencies = [ + "ahash", +] + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "http" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8be7462df143984c4598a256ef469b251d7d7f9e271135073e78fc535414f3d0" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "id-arena" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" + +[[package]] +name = "indexmap" +version = "2.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9" +dependencies = [ + "equivalent", + "hashbrown 0.17.1", + "serde", + "serde_core", +] + +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "leb128" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6cc46bac87ef8093eed6f272babb833b6443374399985ac8ed28471ee0918545" + +[[package]] +name = "log" +version = "0.4.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "616ec5685824bcc94416c6d4a7a446eea774a31efd7062c8480ba6fd06d7a6e5" + +[[package]] +name = "memchr" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "prettyplease" +version = "0.2.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" +dependencies = [ + "proc-macro2", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "semver" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.150" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + +[[package]] +name = "spdx" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3e17e880bafaeb362a7b751ec46bdc5b61445a188f80e0606e68167cd540fa3" +dependencies = [ + "smallvec", +] + +[[package]] +name = "studio_translate_ollama" +version = "0.1.0" +dependencies = [ + "fai-studio-plugin-sdk", + "serde", + "serde_json", + "waki", +] + +[[package]] +name = "syn" +version = "2.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "waki" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e2db2daf1dfbadf228fd8b3c22b96a359135fd673b3d2c203274ee6a0df9c77" +dependencies = [ + "anyhow", + "form_urlencoded", + "http", + "serde", + "waki-macros", + "wit-bindgen 0.34.0", +] + +[[package]] +name = "waki-macros" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a061143f321cc5eeb523f60bdbcd45cfc3ee8851f8cf24f7a4b963bddc5642eb" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "wasm-encoder" +version = "0.219.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8aa79bcd666a043b58f5fa62b221b0b914dd901e6f620e8ab7371057a797f3e1" +dependencies = [ + "leb128", + "wasmparser 0.219.2", +] + +[[package]] +name = "wasm-encoder" +version = "0.220.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e913f9242315ca39eff82aee0e19ee7a372155717ff0eb082c741e435ce25ed1" +dependencies = [ + "leb128", + "wasmparser 0.220.1", +] + +[[package]] +name = "wasm-metadata" +version = "0.219.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1ef51bd442042a2a7b562dddb6016ead52c4abab254c376dcffc83add2c9c34" +dependencies = [ + "anyhow", + "indexmap", + "serde", + "serde_derive", + "serde_json", + "spdx", + "wasm-encoder 0.219.2", + "wasmparser 0.219.2", +] + +[[package]] +name = "wasm-metadata" +version = "0.220.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "185dfcd27fa5db2e6a23906b54c28199935f71d9a27a1a27b3a88d6fee2afae7" +dependencies = [ + "anyhow", + "indexmap", + "serde", + "serde_derive", + "serde_json", + "spdx", + "wasm-encoder 0.220.1", + "wasmparser 0.220.1", +] + +[[package]] +name = "wasmparser" +version = "0.219.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5220ee4c6ffcc0cb9d7c47398052203bc902c8ef3985b0c8134118440c0b2921" +dependencies = [ + "ahash", + "bitflags", + "hashbrown 0.14.5", + "indexmap", + "semver", +] + +[[package]] +name = "wasmparser" +version = "0.220.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d07b6a3b550fefa1a914b6d54fc175dd11c3392da11eee604e6ffc759805d25" +dependencies = [ + "ahash", + "bitflags", + "hashbrown 0.14.5", + "indexmap", + "semver", +] + +[[package]] +name = "wit-bindgen" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e11ad55616555605a60a8b2d1d89e006c2076f46c465c892cc2c153b20d4b30" +dependencies = [ + "wit-bindgen-rt 0.34.0", + "wit-bindgen-rust-macro 0.34.0", +] + +[[package]] +name = "wit-bindgen" +version = "0.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a2b3e15cd6068f233926e7d8c7c588b2ec4fb7cc7bf3824115e7c7e2a8485a3" +dependencies = [ + "wit-bindgen-rt 0.36.0", + "wit-bindgen-rust-macro 0.36.0", +] + +[[package]] +name = "wit-bindgen-core" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "163cee59d3d5ceec0b256735f3ab0dccac434afb0ec38c406276de9c5a11e906" +dependencies = [ + "anyhow", + "heck", + "wit-parser 0.219.2", +] + +[[package]] +name = "wit-bindgen-core" +version = "0.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b632a5a0fa2409489bd49c9e6d99fcc61bb3d4ce9d1907d44662e75a28c71172" +dependencies = [ + "anyhow", + "heck", + "wit-parser 0.220.1", +] + +[[package]] +name = "wit-bindgen-rt" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "744845cde309b8fa32408d6fb67456449278c66ea4dcd96de29797b302721f02" +dependencies = [ + "bitflags", +] + +[[package]] +name = "wit-bindgen-rt" +version = "0.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7947d0131c7c9da3f01dfde0ab8bd4c4cf3c5bd49b6dba0ae640f1fa752572ea" +dependencies = [ + "bitflags", +] + +[[package]] +name = "wit-bindgen-rust" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6919521fc7807f927a739181db93100ca7ed03c29509b84d5f96b27b2e49a9a" +dependencies = [ + "anyhow", + "heck", + "indexmap", + "prettyplease", + "syn", + "wasm-metadata 0.219.2", + "wit-bindgen-core 0.34.0", + "wit-component 0.219.2", +] + +[[package]] +name = "wit-bindgen-rust" +version = "0.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4329de4186ee30e2ef30a0533f9b3c123c019a237a7c82d692807bf1b3ee2697" +dependencies = [ + "anyhow", + "heck", + "indexmap", + "prettyplease", + "syn", + "wasm-metadata 0.220.1", + "wit-bindgen-core 0.36.0", + "wit-component 0.220.1", +] + +[[package]] +name = "wit-bindgen-rust-macro" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c967731fc5d50244d7241ecfc9302a8929db508eea3c601fbc5371b196ba38a5" +dependencies = [ + "anyhow", + "prettyplease", + "proc-macro2", + "quote", + "syn", + "wit-bindgen-core 0.34.0", + "wit-bindgen-rust 0.34.0", +] + +[[package]] +name = "wit-bindgen-rust-macro" +version = "0.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "177fb7ee1484d113b4792cc480b1ba57664bbc951b42a4beebe573502135b1fc" +dependencies = [ + "anyhow", + "prettyplease", + "proc-macro2", + "quote", + "syn", + "wit-bindgen-core 0.36.0", + "wit-bindgen-rust 0.36.0", +] + +[[package]] +name = "wit-component" +version = "0.219.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b8479a29d81c063264c3ab89d496787ef78f8345317a2dcf6dece0f129e5fcd" +dependencies = [ + "anyhow", + "bitflags", + "indexmap", + "log", + "serde", + "serde_derive", + "serde_json", + "wasm-encoder 0.219.2", + "wasm-metadata 0.219.2", + "wasmparser 0.219.2", + "wit-parser 0.219.2", +] + +[[package]] +name = "wit-component" +version = "0.220.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b505603761ed400c90ed30261f44a768317348e49f1864e82ecdc3b2744e5627" +dependencies = [ + "anyhow", + "bitflags", + "indexmap", + "log", + "serde", + "serde_derive", + "serde_json", + "wasm-encoder 0.220.1", + "wasm-metadata 0.220.1", + "wasmparser 0.220.1", + "wit-parser 0.220.1", +] + +[[package]] +name = "wit-parser" +version = "0.219.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca004bb251010fe956f4a5b9d4bf86b4e415064160dd6669569939e8cbf2504f" +dependencies = [ + "anyhow", + "id-arena", + "indexmap", + "log", + "semver", + "serde", + "serde_derive", + "serde_json", + "unicode-xid", + "wasmparser 0.219.2", +] + +[[package]] +name = "wit-parser" +version = "0.220.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae2a7999ed18efe59be8de2db9cb2b7f84d88b27818c79353dfc53131840fe1a" +dependencies = [ + "anyhow", + "id-arena", + "indexmap", + "log", + "semver", + "serde", + "serde_derive", + "serde_json", + "unicode-xid", + "wasmparser 0.220.1", +] + +[[package]] +name = "zerocopy" +version = "0.8.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eed437bf9d6692032087e337407a86f04cd8d6a16a37199ed57949d415bd68e9" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70e3cd084b1788766f53af483dd21f93881ff30d7320490ec3ef7526d203bad4" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zmij" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..ba22404 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,32 @@ +[package] +name = "studio_translate_ollama" +version = "0.1.0" +edition = "2021" +authors = ["Dr. Stefan Flemming "] +license = "Apache-2.0" +publish = false +description = "F∆I Studio plugin — Ollama-backed translate hook" +repository = "https://git.flemming.ai/fai-modules/studio-translate-ollama" +rust-version = "1.85" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +fai-studio-plugin-sdk = { path = "../../fai_studio_plugin_sdk" } +serde = { version = "1", features = ["derive"] } +serde_json = "1" + +# WASI-0.2 HTTP client. Only compiled into the wasm32-wasip2 +# target; the rlib used by host-side tests doesn't need it. +[target.'cfg(target_arch = "wasm32")'.dependencies] +waki = "0.5" + +[package.metadata.component] +package = "fai:studio-plugin" + +[profile.release] +opt-level = "s" +lto = true +codegen-units = 1 +strip = true diff --git a/MODULE.de.md b/MODULE.de.md new file mode 100644 index 0000000..a3cbca4 --- /dev/null +++ b/MODULE.de.md @@ -0,0 +1,72 @@ +# studio.translate.ollama + +Translate-Hook-Plugin für F∆I Studio. Routet `translate`- +Aufrufe durch einen konfigurierten Ollama-`/api/chat`-Endpunkt +— so wird Server-gelieferter englischer Text (MCP-Tool- +Beschreibungen, n8n-Endpunkt-Namen, `[EN]`-gebadgete Strings) +in die aktive Studio-Sprache übersetzt, just-in-time. + +## Capability + +- `studio.translate.ollama@0.1.0` + +## Implementierter Hook + +- `translate(text, from_locale, to_locale) -> string` + +Die anderen beiden Studio-Plugin-Hooks (`theme`, `output-view`) +liefern `PluginError::Declined` — dieses Plugin übersetzt nur. + +## Konfiguration + +Liest zwei Umgebungsvariablen, die der Operator dem Plugin +über die `env:`-Berechtigung verfügbar macht: + +| Variable | Default | +| ----------------- | ------------------------------------ | +| `OLLAMA_ENDPOINT` | `http://localhost:11434/api/chat` | +| `OLLAMA_MODEL` | `qwen2.5:14b` | + +Für andere Endpunkte (z.B. remote Inferenz-Node): +`OLLAMA_ENDPOINT` vor `fai serve` setzen und den Host in den +`net:`-Berechtigungen unten ergänzen. + +## Berechtigungen + +```yaml +permissions: + - "net: localhost" + - "net: 127.0.0.1" + - "net: ollama" +``` + +Operator:innen mit nicht-Loopback-Endpunkt erweitern ihre +Operator-Policy-Ceiling um den Upstream-Host. + +## Wie der System-Prompt arbeitet + +Das Plugin sendet zwei Nachrichten an das Modell: einen festen +System-Prompt, der die Ausgabe auf NUR die Übersetzung +einschränkt (keine Anführungszeichen, kein Kommentar, keine +Erklärung), und eine User-Message mit dem Quelltext. Der +Sprach-Hinweis steht im System-Prompt — das Modell behandelt +ihn als Anweisung, nicht als zu übersetzenden Inhalt. + +So bleibt der Translate-Hook-Vertrag ehrlich: der Caller +erwartet einen reinen String zurück, keine markdown-umrahmte +LLM-Antwort. + +## Grenzen in v0.1.0 + +- Kein Glossar / keine Terminologie-Liste — das Modell stützt + sich auf sein Allgemeinwissen. +- Kein Batching. Jeder Aufruf ist ein HTTP-Request. +- Kein Caching. Studios Plugin-Host könnte in einer späteren + Iteration einen pro-Aufruf-Cache ergänzen. + +## Build + +```bash +cargo build --release --target wasm32-wasip2 +# Ausgabe: target/wasm32-wasip2/release/studio_translate_ollama.wasm +``` diff --git a/MODULE.md b/MODULE.md new file mode 100644 index 0000000..95bb52a --- /dev/null +++ b/MODULE.md @@ -0,0 +1,72 @@ +# studio.translate.ollama + +Translate-hook plugin for F∆I Studio. Routes the `translate` +call through a configured Ollama-shaped `/api/chat` endpoint +so server-supplied English text (MCP tool descriptions, n8n +endpoint names, `[EN]`-badged strings) gets translated into +the active Studio locale on the fly. + +## Capability + +- `studio.translate.ollama@0.1.0` + +## Hook implemented + +- `translate(text, from_locale, to_locale) -> string` + +The other two Studio-plugin hooks (`theme`, `output-view`) +return `PluginError::Declined` — this plugin only translates. + +## Configuration + +Reads two environment variables that the operator exposes via +the plugin's `env:` permission: + +| Variable | Default | +| ----------------- | ------------------------------------ | +| `OLLAMA_ENDPOINT` | `http://localhost:11434/api/chat` | +| `OLLAMA_MODEL` | `qwen2.5:14b` | + +To point at a different endpoint (e.g. a remote inference +node), set `OLLAMA_ENDPOINT` before starting `fai serve` and +make sure the host appears in the `net:` permissions below. + +## Permissions + +```yaml +permissions: + - "net: localhost" + - "net: 127.0.0.1" + - "net: ollama" +``` + +Operators that route through a non-loopback endpoint extend +their operator-policy ceiling to include the upstream host. + +## How the system prompt works + +The plugin sends two messages to the model: a fixed system +prompt that constrains the output to ONLY the translation +(no quotation marks, no commentary, no explanation) and a +user message with the source text. The locale hint goes in +the system prompt so the model treats it as a directive, not +as translatable content. + +This keeps the translate hook's contract honest: caller +expects a plain string back, not a markdown-wrapped LLM +response. + +## Limits in v0.1.0 + +- No glossary / terminology list — the model relies on its + general training. +- No batching. Each call is one HTTP request. +- No caching. Studio's plugin-host may add a per-call cache + in a future iteration. + +## Build + +```bash +cargo build --release --target wasm32-wasip2 +# Output: target/wasm32-wasip2/release/studio_translate_ollama.wasm +``` diff --git a/module.yaml b/module.yaml new file mode 100644 index 0000000..7bd1b0d --- /dev/null +++ b/module.yaml @@ -0,0 +1,29 @@ +schema_version: 1 +name: studio-translate-ollama +version: 0.1.0 + +provides: + - capability: studio.translate.ollama + version: 0.1.0 + +# Studio plugins are called via WIT exports, not the flow-engine +# `invoke` path. Inputs / outputs stay empty so the hub's +# manifest validator doesn't trip. +inputs: {} +outputs: {} + +# Reaches out to an Ollama-shaped HTTP endpoint. The default +# permissions cover loopback (local Ollama via host services +# or `ollama serve`) plus the two common LLM endpoints +# operators might tunnel through. +permissions: + - "net: localhost" + - "net: 127.0.0.1" + - "net: ollama" + +# Plugin-host declaration: which hooks this component +# implements. Studio reads this at plugin-discovery time so it +# knows which RPCs to call against this capability. +studio_extension: + hooks: + - translate diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..32add0c --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,4 @@ +[toolchain] +channel = "1.86" +targets = ["wasm32-wasip2"] +components = ["rustfmt", "clippy"] diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..9d1163a --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,161 @@ +//! `studio.translate.ollama` — second Studio plugin. +//! +//! Implements the `translate` hook by routing through an +//! Ollama-shaped `/api/chat` endpoint. Reads its endpoint URL +//! from the `OLLAMA_ENDPOINT` environment variable (operator +//! sets it in the hub's operator config, exposed to plugins +//! via the `env:` permission). Default falls back to +//! `http://localhost:11434/api/chat`. +//! +//! Same SDK v0.1 shape as `studio.theme.solarized`: declares +//! all three Guest impls; theme + output-view return +//! `PluginError::Declined`, only translate does real work. +//! +//! Wire format: standard Ollama `/api/chat` non-streaming +//! request with a system prompt that instructs the model to +//! return ONLY the translation, no commentary. + +#![allow(clippy::result_large_err)] + +use fai_studio_plugin_sdk::{bindings, export_plugin}; + +type ColorScheme = bindings::fai::studio_plugin::plugin_types::ColorScheme; +type PluginError = bindings::fai::studio_plugin::plugin_types::PluginError; +type RenderedOutput = bindings::fai::studio_plugin::plugin_types::RenderedOutput; + +const DEFAULT_ENDPOINT: &str = "http://localhost:11434/api/chat"; +const DEFAULT_MODEL: &str = "qwen2.5:14b"; + +struct OllamaTranslatePlugin; + +impl bindings::exports::fai::studio_plugin::translate::Guest for OllamaTranslatePlugin { + fn translate( + text: String, + from_locale: String, + to_locale: String, + ) -> Result { + if to_locale.trim().is_empty() { + return Err(PluginError::Declined( + "translate: to_locale is empty (host must supply it)".to_string(), + )); + } + if text.trim().is_empty() { + // No work to do; return verbatim so the calling UI + // can keep the original placeholder. + return Ok(text); + } + do_translate(&text, &from_locale, &to_locale) + } +} + +// ─── theme + output-view: declined stubs ──────────────────── +// v0.1 of the SDK declares all three hooks in a single WIT +// world. Plugins that only serve one hook return Declined for +// the others; the hub falls back to its built-in renderer or +// the [EN] badge. + +impl bindings::exports::fai::studio_plugin::theme::Guest for OllamaTranslatePlugin { + fn theme_for(_brightness: String) -> Result { + Err(PluginError::Declined( + "studio.translate.ollama is a translate plugin, not a theme plugin".to_string(), + )) + } +} + +impl bindings::exports::fai::studio_plugin::output_view::Guest for OllamaTranslatePlugin { + fn render(_mime_type: String, _data: Vec) -> Result { + Err(PluginError::Declined( + "studio.translate.ollama is a translate plugin, not an output viewer".to_string(), + )) + } +} + +export_plugin!(OllamaTranslatePlugin); + +// ─── HTTP path ────────────────────────────────────────────── +// Compiled only for wasm32-wasip2; the host-side test fixture +// (none yet) gets the stub. + +#[cfg(target_arch = "wasm32")] +fn do_translate(text: &str, from_locale: &str, to_locale: &str) -> Result { + let endpoint = read_env("OLLAMA_ENDPOINT", DEFAULT_ENDPOINT); + let model = read_env("OLLAMA_MODEL", DEFAULT_MODEL); + + // Two-message chat: one system prompt that constrains the + // model to return ONLY the translation, one user message + // with the source text. The locale hint goes in the system + // prompt so the model treats it as a directive, not as + // translatable content. + let from_clause = if from_locale.trim().is_empty() { + "auto-detected from the input".to_string() + } else { + from_locale.to_string() + }; + let system_prompt = format!( + "You are a translation engine. Translate the user's text \ + from {from_clause} to {to_locale}. Output ONLY the \ + translated text. Do not add commentary, explanations, \ + quotation marks, or any framing. Preserve formatting \ + (newlines, punctuation, capitalisation) of the source." + ); + + let body = serde_json::json!({ + "model": model, + "messages": [ + { "role": "system", "content": system_prompt }, + { "role": "user", "content": text }, + ], + "stream": false, + }) + .to_string(); + + let request = waki::Client::new() + .post(&endpoint) + .header( + waki::header::HeaderName::from_static("content-type"), + waki::header::HeaderValue::from_static("application/json"), + ) + .body(body); + + let response = request + .send() + .map_err(|e| PluginError::Misconfigured(format!("ollama unreachable: {e}")))?; + let status = response.status_code(); + if !(200..300).contains(&status) { + return Err(PluginError::Misconfigured(format!( + "ollama returned HTTP {status}" + ))); + } + let bytes = response + .body() + .map_err(|e| PluginError::Internal(format!("body read: {e}")))?; + let parsed: serde_json::Value = serde_json::from_slice(&bytes) + .map_err(|e| PluginError::Internal(format!("response is not JSON: {e}")))?; + let translated = parsed + .get("message") + .and_then(|m| m.get("content")) + .and_then(|c| c.as_str()) + .ok_or_else(|| { + PluginError::Internal( + "ollama response missing `message.content` field".to_string(), + ) + })?; + Ok(translated.trim().to_string()) +} + +#[cfg(not(target_arch = "wasm32"))] +fn do_translate(_text: &str, _from: &str, _to: &str) -> Result { + Err(PluginError::Internal( + "translate hook can only execute under the WASM runtime".to_string(), + )) +} + +/// Read an env-var if the plugin has the `env: VAR` +/// permission; fall back to a default otherwise. waki + std's +/// `std::env::var` work the same way under wasm32-wasip2 — +/// `var()` returns `Err(NotPresent)` for both unset variables +/// AND variables the WASI policy did not expose. +#[cfg(target_arch = "wasm32")] +fn read_env(name: &str, default: &str) -> String { + std::env::var(name).unwrap_or_else(|_| default.to_string()) +}