Repository navigation
extra: Move Speech into nsh-extra and recognize speech on Android, Windows and the web - #2947
Merged
Merged
Conversation
…, Windows and the web
- A session another engine displaces ends with `audio-capture`; the same engine's new session ends it without an error (`element::engine_id()`). - A refused session ends nothing; a stale platform start changes nothing. - No recognizer in terminal apps on any OS. - Headless apps with different fakes take turns in one process; fake names are unique across sets; `Registry::fakes` is hidden. - `nsh/test` drops `app.hear`, `app.endSpeech` and `app.speechRequests`: tests call the speech fake through `app.fake`. - Windows asks WinRT whether speech is available off the engine thread. - CI lints `nsh-extra` for wasm32; docs follow.
madcodelife
enabled auto-merge (squash)
November 23, 2026 14:09
madcodelife
disabled auto-merge
November 23, 2026 14:13
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
What
Speech(speech recognition) leaves NSH's runtime and becomes thespeechmodule ofnsh-extra, and it now recognizes speech on Android, Windows and the web as well as macOS and iOS.The JS API keeps its shape:
Speech.available(),Speech.start(options),SpeechSession's events,stop/abort, and the error names. Only the specifier changes.nsh-platform-apple)android.speech.SpeechRecognizer,RECORD_AUDIOasked when a session starts;available()is false on devices without a recognition serviceWindows.Media.SpeechRecognition's continuous dictation; it uses the user's online speech recognition setting, and a session ends withnot-allowedwhile it is offSpeechRecognition/webkitSpeechRecognition), through wasm-bindgen;available()is false where the browser has noneavailable()is false and a session ends withunavailableAn app embedded with GPUI uses the platform's recognizer on macOS and Windows.
One session runs at a time in the process: the microphone is one, so starting another ends the one running. A new session of the same app ends it without an error, as
abort()does; a session another app of the process starts (an embedding app's otherAppView) ends it witherror: "audio-capture", the microphone could not be used. A session an app refuses (speechnot granted) ends nothing.Packaging
The CLI no longer adds
NSMicrophoneUsageDescriptionandNSSpeechRecognitionUsageDescriptionto every iOS and macOS app.nsh-extradeclares what the module needs under[package.metadata.nsh.features.speech], so only apps that turnspeechon get them:com.apple.security.device.audio-input.RECORD_AUDIOand a<queries>entry forandroid.speech.RecognitionService.The
speechcapability stays: the module checks it in every function of its JS face (asPushchecksnotifications) and for sessions Rust code starts.Generic extension points
The module is built only on hooks any third-party nshm can use:
nsh_core::element::access(): the running engine's capabilities, for checks in Rust.nsh_core::element::engine_id(): which engine the calling code runs in, so a module whose state is the process's (one microphone) tells its apps' requests apart.nsh_core::fake::FakeandRegistry::fake: a module's stand-in for headless runs.nsh testinstalls the fakes of the app's sets before the app starts and routesapp.fake(name, method, args)to them; the speech fake's methods (hear,end,requests) are documented with the module. A fake may stand in process-wide, so headless apps with different fakes take turns in one process (two#[test]s); the apps onensh testfile opens share theirs.nsh/testkeeps no speech-specific calls.nsh-platform-android'sproxy::implement: implements Java interfaces (listeners,Runnable) in Rust throughjava.lang.reflect.Proxy, so an nshm needs no Java classes of its own.Also
nshreads the version a workspace member inherits (version.workspace = true), so an nshm developed in a Cargo workspace works as a"path"dependency.examples/extra: a small app that turns onnsh-extraand holds the speech tests (moved fromexamples/gallery, which stays TSX-only). Its tests run inrust-app.yml.stop/abortnever reaches another's, nor does a late callback a later session); a session another app displaces ends withaudio-capture; a session whose app stops is aborted by its own drop, which reaches the platform without going through the engine, so the microphone turns off (a_session_ends_when_its_app_stops).nsh-platform-apple's per-surfacespeech::Listenergoes with the rest of the runtime's speech code.mod-speechis removed: compat apps link only NSH's runtime, not nshm.nsh-extra's API is covered by its crate tests andexamples/extra.Why
docs/dev/js-modules.mdputs a system feature only some apps use, which would add permissions or store declarations to every app, innsh-extra, with the runtime keeping only generic hooks. Speech is such a feature: every iOS and macOS app without declared capabilities carried the microphone and speech recognition usage descriptions, whether it dictated or not. Moving it also lets each platform's recognizer live in one place, so the module now covers Android, Windows and the web.Public API
nsh-extra(featurespeech), TS:Speech.available(): Promise<boolean>,Speech.start(options?: SpeechOptions): SpeechSession,SpeechSession(on,text,ended,stop,abort): moved fromnsh, unchanged.nsh_extra::speech, Rust:async fn available() -> bool: whether speech can be recognized here.fn recognize(SpeechRequest) -> SpeechRecognition: starts a session;SpeechRecognition(next().await/poll_next,stop,abort; dropping it aborts).fn set_backend(Option<Arc<dyn SpeechBackend>>): an embedding app's own recognizer, process-wide.SpeechRequest { lang, interim }(non-exhaustive,Default),SpeechEvent,SpeechError(ALL: &'static [SpeechError],as_str,from_name,Display),SpeechSink(send,start,result,end,is_ended),trait SpeechControl,trait SpeechBackend(available,start).speech;[package.metadata.nsh.features.speech](see Packaging).nsh-core:element::access() -> Option<Arc<access::Access>>: the running engine's capabilities;Noneon no engine's thread.element::engine_id() -> Option<EngineId>,element::EngineId(opaque,Copy,Eq,Hash, never reused in the process): which engine the calling code runs in.fake::Fake(install(&self),call(&self, method: &str, args: &[JsonValue]) -> Result<JsonValue, String>): a module's stand-in for headless runs.Registry::fake(&mut self, name: &'static str, fake: Arc<dyn Fake>) -> &mut Self; fake names are unique across the sets an app is served (Registry::mergedpanics otherwise).Registry::fakes(what the headless driver installs) is#[doc(hidden)].nsh_extra::speech):speechmodule,speech_available,recognize_speech,SpeechRecognition,Core::set_speech,Environment::speech(inruntimeandelement),Command::Speech, the rootSpeechevent.nsh-platform-apple:speech(command(control, command)), now insidensh-extra.nsh-engine:EngineConfig::speech(nownsh_extra::speech::set_backend).nsh-testing/nsh/test:Headless::call_fake(name, method, args) -> Result<JsonValue, String>; TSapp.fake(name: string, method: string, args?: unknown[]): Promise<unknown>.Headless::startwaits while apps with other fakes run in the process (above).SpeechRequest,Headless's speech methods, TSapp.hear(text),app.endSpeech(error?),app.speechRequests(); tests call the fake.nsh-platform-android(nsh):proxy::implement(env, interfaces: &[&str], handler) -> jni::errors::Result<Proxy>,proxy::Proxy(#[must_use],as_obj): Java interfaces implemented in Rust. Only interfaces; a method returning a primitive gets a boxed value.CLI:
nsh-extra'sspeechfeature declares them."path"nshm whoseCargo.tomlinherits its version from its workspace is read.Breaking Changes
Nothing here is in a recorded release (
docs/dev/compatibility.md), soSpeechmoves without an alias.A session another app displaces now ends with
error: "audio-capture"instead of no error.How to Test
Ran on macOS:
cargo test -p nsh-extra --features nsh-extra/camera,nsh-extra/photos,nsh-extra/push,nsh-extra/speech: 71 unit tests, and theextratest binary:a_session_ends_when_its_app_stops,an_app_with_other_fakes_waits_for_the_one_running(two headless apps with registries of their own take turns; it failed before the fix, app A's test reaching app B's fake),apps_with_the_same_fakes_run_togethera_new_session_ends_the_one_running,another_engines_session_ends_the_one_running_with_audio_capture,a_stale_start_changes_nothing;fake_names_are_unique_across_setsinnsh-corecargo test -p nsh-core -p nsh-engine -p nsh-testing,cargo test -p nsh-testing --features bundle --test testing,cargo test -p nsh-engine --features websocket websocketnsh test examples/extra: 4 passed, throughapp.fake("nsh-extra/speech", ...)cargo clippy -- -D warnings: the workspace;nsh-extrawith every feature; the macOS and Catalyst lines withnsh-extra/speech;nsh-extraforx86_64-pc-windows-msvcandwasm32-unknown-unknowncargo xtask clippy-android: clean fornsh-platform-androidandnsh-extraapart frommissing_const_for_thread_localonthread_local!s that already useconst { }, which this toolchain (1.100) also reports innsh-coreonmainjavac --release 17 -Xlint:allofnsh-platform-android's Java sources againstandroid.jar: compiles, no warning inNativeProxy.javacargo +nightly fmt --all --check: only files this PR does not touch differ, as onmainNot verified here (devices and browsers):
AppViews on one device: the displaced app'saudio-capture, and the microphone indicator going off when an app stops.nsh packageof an app withspeechfor iOS, macOS and Android: the usage descriptions, the macOS entitlement,RECORD_AUDIOand the<queries>entry in the packaged app.available()off the engine thread: compiled forx86_64-pc-windows-msvconly.wasm-bindgenCLI (docs/dev/web.md), so an app withspeechneeds it installed to build for the web.Checklist