From 1f15f79e1e4f9e1171399ae6e5e3ada2c7bf6029 Mon Sep 17 00:00:00 2001 From: Mike Grabowski Date: Sat, 31 Oct 2026 12:07:23 +0100 Subject: [PATCH 1/3] refactor(android): drop isAvailable in favor of getModelInfo ML Kit has no synchronous status check, so isAvailable() answered from a cached status or a guess, and it counted a downloading model as available where apple and ADK don't. getModelInfo() reports the exact state. Builds on #367. Co-Authored-By: Claude Opus 5.5 --- harness/android/availability.harness.ts | 1 - .../android/src/main/AndroidManifest.xml | 4 --- .../callstack/ai/android/AndroidAIModule.kt | 5 ---- .../com/callstack/ai/android/GeminiNano.kt | 30 +------------------ .../com/callstack/ai/android/Transcription.kt | 27 ++--------------- packages/android/src/NativeAndroidAI.ts | 4 --- packages/android/src/__tests__/ai-sdk.test.ts | 23 -------------- packages/android/src/ai-sdk.ts | 29 ------------------ .../docs/docs/android/getting-started.mdx | 3 +- .../src/content/docs/docs/android/models.md | 13 ++------ .../docs/docs/android/transcription.md | 8 ----- 11 files changed, 8 insertions(+), 139 deletions(-) diff --git a/harness/android/availability.harness.ts b/harness/android/availability.harness.ts index 75aba9b4..32d5e5d0 100644 --- a/harness/android/availability.harness.ts +++ b/harness/android/availability.harness.ts @@ -22,7 +22,6 @@ describe('android availability', () => { expect(info.availability).toBe('available') expect(info.contextSize).toBeGreaterThan(0) expect(info.features).toBeDefined() - expect(android.isAvailable()).toBe(true) }) it('reports Gemini Nano 4 preview details', async () => { diff --git a/packages/android/android/src/main/AndroidManifest.xml b/packages/android/android/src/main/AndroidManifest.xml index d5ee5eee..470a319d 100644 --- a/packages/android/android/src/main/AndroidManifest.xml +++ b/packages/android/android/src/main/AndroidManifest.xml @@ -1,7 +1,3 @@ - - - - diff --git a/packages/android/android/src/main/java/com/callstack/ai/android/AndroidAIModule.kt b/packages/android/android/src/main/java/com/callstack/ai/android/AndroidAIModule.kt index 8de499dd..27d130fc 100644 --- a/packages/android/android/src/main/java/com/callstack/ai/android/AndroidAIModule.kt +++ b/packages/android/android/src/main/java/com/callstack/ai/android/AndroidAIModule.kt @@ -91,8 +91,6 @@ class AndroidAIModule(reactContext: ReactApplicationContext) : NativeAndroidAISp putString("arguments", FunctionJson.argumentsToJson(arguments).toString()) } - override fun isAvailable(model: ReadableMap?): Boolean = nano.isAvailable(NanoConfig.from(model)) - override fun getModelInfo(model: ReadableMap?, promise: Promise) = launch(promise) { nano.getModelInfo(NanoConfig.from(model)).toWritableMap() } @@ -162,9 +160,6 @@ class AndroidAIModule(reactContext: ReactApplicationContext) : NativeAndroidAISp activeRuns.remove(runId)?.cancel() } - override fun isTranscriptionAvailable(options: ReadableMap): Boolean = - transcription.isAvailable(TranscriptionConfig.from(options)) - override fun getTranscriptionModelInfo(options: ReadableMap, promise: Promise) = launch(promise) { transcription.getModelInfo(TranscriptionConfig.from(options)).toWritableMap() } diff --git a/packages/android/android/src/main/java/com/callstack/ai/android/GeminiNano.kt b/packages/android/android/src/main/java/com/callstack/ai/android/GeminiNano.kt index 08424c10..56e8ad91 100644 --- a/packages/android/android/src/main/java/com/callstack/ai/android/GeminiNano.kt +++ b/packages/android/android/src/main/java/com/callstack/ai/android/GeminiNano.kt @@ -1,7 +1,6 @@ package com.callstack.ai.android import android.content.Context -import android.content.pm.PackageManager import android.net.Uri import android.util.Base64 import com.facebook.react.bridge.ReadableArray @@ -31,16 +30,6 @@ import kotlinx.coroutines.sync.withLock import org.json.JSONObject import java.util.concurrent.ConcurrentHashMap -private const val AICORE_PACKAGE = "com.google.android.aicore" - -fun isAICoreInstalled(context: Context): Boolean = - try { - context.packageManager.getPackageInfo(AICORE_PACKAGE, 0) - true - } catch (_: PackageManager.NameNotFoundException) { - false - } - /** Gemini Nano model selection, see ML Kit `ModelConfig`. */ data class NanoConfig( val releaseStage: String? = null, @@ -99,8 +88,6 @@ class GeminiNano( private val prepared = ConcurrentHashMap.newKeySet() // One lock per model selection, so one download does not hold up another selection private val prepareMutexes = ConcurrentHashMap() - // The last status seen per model selection, for the synchronous check - private val statuses = ConcurrentHashMap() private val systemPromptSupport = ConcurrentHashMap() private fun client(config: NanoConfig): GenerativeModel = @@ -123,17 +110,6 @@ class GeminiNano( } } - /** - * Whether Gemini Nano is available, downloadable or downloading, answered without async work: from the last - * status this process saw, or else from whether AICore is installed. - */ - fun isAvailable(config: NanoConfig): Boolean { - statuses[config]?.let { - return it == FeatureStatus.AVAILABLE || it == FeatureStatus.DOWNLOADABLE || it == FeatureStatus.DOWNLOADING - } - return isAICoreInstalled(context) - } - suspend fun getModelInfo(config: NanoConfig): ModelInfo { val model = client(config) val status = @@ -142,7 +118,6 @@ class GeminiNano( } catch (_: Exception) { return ModelInfo(availability = "unknown") } - statuses[config] = status if (status != FeatureStatus.AVAILABLE) { return ModelInfo(availability = availabilityOf(status)) } @@ -172,9 +147,7 @@ class GeminiNano( if (config in prepared) return val model = client(config) - val status = model.checkStatus() - statuses[config] = status - when (status) { + when (val status = model.checkStatus()) { FeatureStatus.UNAVAILABLE -> throw AndroidAIErrors.CodedException( AndroidAIErrors.MODEL_UNAVAILABLE, @@ -185,7 +158,6 @@ class GeminiNano( } model.warmup() prepared.add(config) - statuses[config] = FeatureStatus.AVAILABLE } } diff --git a/packages/android/android/src/main/java/com/callstack/ai/android/Transcription.kt b/packages/android/android/src/main/java/com/callstack/ai/android/Transcription.kt index 230c6f4e..9f5ba38c 100644 --- a/packages/android/android/src/main/java/com/callstack/ai/android/Transcription.kt +++ b/packages/android/android/src/main/java/com/callstack/ai/android/Transcription.kt @@ -1,7 +1,6 @@ package com.callstack.ai.android import android.content.Context -import android.os.Build import android.util.Base64 import com.facebook.react.bridge.ReadableMap import com.google.mlkit.genai.common.FeatureStatus @@ -32,8 +31,6 @@ class Transcription( ) { private val clients = ConcurrentHashMap() private val prepared = ConcurrentHashMap.newKeySet() - // The last status seen per language and mode, for the synchronous check - private val statuses = ConcurrentHashMap() // One lock per language and mode, so one download does not hold up another private val prepareMutexes = ConcurrentHashMap() @@ -58,23 +55,8 @@ class Transcription( "Not a BCP 47 language tag: $tag. Use a tag such as en-US", ) - /** - * Whether the recognizer is available, downloadable or downloading, answered without async work: from the last - * status this process saw, or else from what the mode needs (Android 12 for basic, AICore for advanced). - */ - fun isAvailable(config: TranscriptionConfig): Boolean { - statuses[config]?.let { - return it == FeatureStatus.AVAILABLE || it == FeatureStatus.DOWNLOADABLE || it == FeatureStatus.DOWNLOADING - } - return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && - (config.mode != "advanced" || isAICoreInstalled(context)) - } - - suspend fun getModelInfo(config: TranscriptionConfig): ModelInfo { - val status = client(config).checkStatus() - statuses[config] = status - return ModelInfo(availability = availabilityOf(status)) - } + suspend fun getModelInfo(config: TranscriptionConfig): ModelInfo = + ModelInfo(availability = availabilityOf(client(config).checkStatus())) /** Downloads the recognizer's model when needed, reporting progress, then warms it up. */ suspend fun prepare(config: TranscriptionConfig) { @@ -83,9 +65,7 @@ class Transcription( if (config in prepared) return val recognizer = client(config) - val status = recognizer.checkStatus() - statuses[config] = status - when (status) { + when (val status = recognizer.checkStatus()) { FeatureStatus.UNAVAILABLE -> throw AndroidAIErrors.CodedException( AndroidAIErrors.MODEL_UNAVAILABLE, @@ -96,7 +76,6 @@ class Transcription( } recognizer.warmup() prepared.add(config) - statuses[config] = FeatureStatus.AVAILABLE } } diff --git a/packages/android/src/NativeAndroidAI.ts b/packages/android/src/NativeAndroidAI.ts index 6d0c4d7a..9fa31d36 100644 --- a/packages/android/src/NativeAndroidAI.ts +++ b/packages/android/src/NativeAndroidAI.ts @@ -112,8 +112,6 @@ export interface NativeEmbeddingModelInfo { } export interface Spec extends TurboModule { - /** Quick synchronous check, see `AndroidProvider.isAvailable` */ - isAvailable(model?: NanoModelConfig): boolean getModelInfo(model?: NanoModelConfig): Promise prepare(model?: NanoModelConfig): Promise countTokens(text: string, model?: NanoModelConfig): Promise @@ -126,8 +124,6 @@ export interface Spec extends TurboModule { /** Cancels a `generateText`, `streamText` or `transcribe` run by its id */ cancel(runId: string): void - /** Quick synchronous check, see `AndroidTranscriptionModel.isAvailable` */ - isTranscriptionAvailable(options: NativeTranscriptionOptions): boolean getTranscriptionModelInfo( options: NativeTranscriptionOptions ): Promise diff --git a/packages/android/src/__tests__/ai-sdk.test.ts b/packages/android/src/__tests__/ai-sdk.test.ts index a1433e1c..c78aaad8 100644 --- a/packages/android/src/__tests__/ai-sdk.test.ts +++ b/packages/android/src/__tests__/ai-sdk.test.ts @@ -34,8 +34,6 @@ const generateText = mock( const streamText = mock(async (_streamId: string, _request: any) => {}) const prepare = mock(async (_model?: unknown) => {}) const cancel = mock((_id: string) => {}) -const isAvailable = mock((_model?: unknown) => true) -const isTranscriptionAvailable = mock((_options?: unknown) => true) const prepareTranscription = mock(async (_options?: unknown) => {}) const prepareEmbedding = mock(async () => {}) const getModelInfo = mock( @@ -64,10 +62,8 @@ mock.module('react-native', () => ({ streamText, prepare, cancel, - isAvailable, getModelInfo, transcribe, - isTranscriptionAvailable, getTranscriptionModelInfo, prepareTranscription, prepareEmbedding, @@ -157,12 +153,6 @@ describe('provider', () => { }) }) - test('checks availability synchronously', () => { - expect(createAndroidProvider().isAvailable()).toBe(true) - isAvailable.mockImplementationOnce(() => false) - expect(nano.isAvailable()).toBe(false) - }) - test('reports download progress while preparing', async () => { const model = createAndroidProvider()() prepare.mockImplementationOnce(async () => { @@ -610,19 +600,6 @@ describe('other models', () => { expect(result.language).toBe('en') }) - test('checks transcription availability synchronously', () => { - const model = createAndroidProvider().transcriptionModel({ - language: 'en-US', - }) - expect(model.isAvailable()).toBe(true) - isTranscriptionAvailable.mockImplementationOnce(() => false) - expect(model.isAvailable()).toBe(false) - expect(isTranscriptionAvailable.mock.calls.at(-1)![0]).toEqual({ - locale: 'en-US', - mode: undefined, - }) - }) - test('warns and prepares when a model is used before prepare', async () => { const warn = spyOn(console, 'warn').mockImplementation(() => {}) const model = createAndroidProvider().embeddingModel() diff --git a/packages/android/src/ai-sdk.ts b/packages/android/src/ai-sdk.ts index fdc6d2d4..0241a620 100644 --- a/packages/android/src/ai-sdk.ts +++ b/packages/android/src/ai-sdk.ts @@ -100,8 +100,6 @@ export interface AndroidPrepareOptions { } export interface AndroidLanguageModel extends LanguageModelV4 { - /** Quick synchronous check, see `AndroidProvider.isAvailable` */ - isAvailable: () => boolean /** Gemini Nano's availability and features for this model selection */ getModelInfo: () => Promise /** Downloads Gemini Nano when needed and loads it, reporting download progress */ @@ -198,10 +196,6 @@ async function withProgress( } } -function isAvailable(model: NanoModelConfig | undefined) { - return Platform.OS === 'android' && getNativeAndroidAI().isAvailable(model) -} - async function getModelInfo( model: NanoModelConfig | undefined ): Promise { @@ -229,12 +223,6 @@ export function createAndroidProvider( } provider.specificationVersion = 'v4' as const provider.languageModel = createLanguageModel - /** - * Quick synchronous check whether Gemini Nano can be used. It reflects the - * last status `getModelInfo()` or `prepare()` saw, or else whether AICore is - * installed. Use `getModelInfo()` for the exact availability - */ - provider.isAvailable = () => isAvailable(defaults.model) /** Gemini Nano's availability, for the provider's model selection or `model` */ provider.getModelInfo = (options: { model?: AndroidNanoModel } = {}) => getModelInfo(options.model ?? defaults.model) @@ -628,10 +616,6 @@ class AndroidChatLanguageModel implements AndroidLanguageModel { this.model = options.model } - isAvailable(): boolean { - return isAvailable(this.model) - } - getModelInfo(): Promise { return getModelInfo(this.model) } @@ -893,19 +877,6 @@ class AndroidTranscriptionModel implements TranscriptionModelV4 { } } - /** - * Whether the recognizer for this language and mode is available, - * downloadable or downloading, answered synchronously from the last status - * `getModelInfo()` or `prepare()` saw. Before either runs, it checks what the - * mode needs: Android 12 for basic, AICore for advanced - */ - isAvailable(): boolean { - return ( - Platform.OS === 'android' && - getNativeAndroidAI().isTranscriptionAvailable(this.options) - ) - } - /** Downloads the recognizer's model when needed, reporting download progress */ async prepare(options: AndroidPrepareOptions = {}): Promise { await withProgress('transcription', options, () => diff --git a/website/src/content/docs/docs/android/getting-started.mdx b/website/src/content/docs/docs/android/getting-started.mdx index 7d4eea03..54468b36 100644 --- a/website/src/content/docs/docs/android/getting-started.mdx +++ b/website/src/content/docs/docs/android/getting-started.mdx @@ -83,7 +83,8 @@ Pass `android()` to the AI SDK. Check that Gemini Nano can run on the device fir import { android } from '@react-native-ai/android' import { generateText } from 'ai' -if (android.isAvailable()) { +const { availability } = await android.getModelInfo() +if (availability === 'available' || availability === 'downloadable') { const { text } = await generateText({ model: android(), prompt: 'Summarize on-device AI in one sentence.', diff --git a/website/src/content/docs/docs/android/models.md b/website/src/content/docs/docs/android/models.md index 0b02e75e..95f07ce9 100644 --- a/website/src/content/docs/docs/android/models.md +++ b/website/src/content/docs/docs/android/models.md @@ -7,20 +7,11 @@ AICore downloads it on first use and updates it on its own. ## Checking Availability -`isAvailable()` answers synchronously, from the last status `getModelInfo()` or -`prepare()` saw, or else from whether AICore is installed: +`getModelInfo()` checks with AICore: ```typescript import { android } from '@react-native-ai/android' -if (android.isAvailable()) { - // Offer on-device features -} -``` - -`getModelInfo()` checks with AICore: - -```typescript const info = await android.getModelInfo() // { // availability: 'available', @@ -73,4 +64,4 @@ const preview = createAndroidProvider({ [Tool calling](/docs/android/tools) needs Gemini Nano 4: the preview release with `fast`, or the default model on devices that ship it, such as the Galaxy Z Flip 8 and Fold 8. -`getModelInfo` and `isAvailable` take the same selection. +`getModelInfo` takes the same selection. diff --git a/website/src/content/docs/docs/android/transcription.md b/website/src/content/docs/docs/android/transcription.md index f2021309..84094890 100644 --- a/website/src/content/docs/docs/android/transcription.md +++ b/website/src/content/docs/docs/android/transcription.md @@ -31,15 +31,7 @@ empty. ## Availability and Download -`isAvailable()` answers synchronously, from the last status `getModelInfo()` -or `prepare()` saw. Before either has run, it checks what the mode needs: -Android 12 for `basic`, AICore for `advanced`. - ```typescript -if (model.isAvailable()) { - // available, downloadable or downloading -} - const info = await model.getModelInfo() // { availability: 'available' | 'downloadable' | 'downloading' | 'unavailable' } From c97bb4bb227912330136a9962e3d52c6e4137803 Mon Sep 17 00:00:00 2001 From: Mike Grabowski Date: Sat, 31 Oct 2026 23:03:12 +0100 Subject: [PATCH 2/3] docs(android): note that a downloadable gemini nano downloads on first use Co-Authored-By: Claude Opus 5.5 --- website/src/content/docs/docs/android/getting-started.mdx | 1 + 1 file changed, 1 insertion(+) diff --git a/website/src/content/docs/docs/android/getting-started.mdx b/website/src/content/docs/docs/android/getting-started.mdx index 54468b36..39cb61e4 100644 --- a/website/src/content/docs/docs/android/getting-started.mdx +++ b/website/src/content/docs/docs/android/getting-started.mdx @@ -84,6 +84,7 @@ import { android } from '@react-native-ai/android' import { generateText } from 'ai' const { availability } = await android.getModelInfo() +// When downloadable, the first request downloads Gemini Nano on its own if (availability === 'available' || availability === 'downloadable') { const { text } = await generateText({ model: android(), From a905da66b0e7ede9898762a56efbcad1a04e3f71 Mon Sep 17 00:00:00 2001 From: Mike Grabowski Date: Sat, 31 Oct 2026 23:03:15 +0100 Subject: [PATCH 3/3] refactor(apple)!: drop isAvailable in favor of getModelInfo Removes isAvailable() from the provider, the transcription model and the AppleTranscription module, matching the Android provider. getModelInfo() reports availability and why the model is unavailable. Co-Authored-By: Claude Opus 5.5 --- apps/example/src/adapters/apple.ts | 10 +++++++-- apps/example/src/adapters/llama.ts | 2 +- apps/example/src/adapters/types.ts | 2 +- .../src/components/FeatureRequirements.tsx | 7 +++--- apps/example/src/store/features.ts | 22 ++++++++++++++----- apps/example/src/store/models.ts | 19 ++++++++++++---- apps/example/src/store/readiness.ts | 5 +++-- harness/apple/availability.harness.ts | 4 ---- harness/apple/transcription.device.harness.ts | 6 ----- packages/apple-llm/ios/AppleLLM.mm | 4 ---- packages/apple-llm/ios/AppleLLMImpl.swift | 13 ----------- .../ios/transcription/AppleTranscription.mm | 4 ---- .../AppleTranscriptionImpl.swift | 11 ---------- packages/apple-llm/src/NativeAppleLLM.ts | 1 - .../apple-llm/src/NativeAppleTranscription.ts | 3 --- packages/apple-llm/src/ai-sdk.ts | 8 +------ .../references/apple-provider.md | 6 ++--- .../docs/docs/apple/getting-started.mdx | 10 ++++----- website/src/content/docs/docs/apple/models.md | 14 +++++------- .../content/docs/docs/apple/transcription.md | 19 ++++++++-------- website/src/site/content.ts | 2 +- 21 files changed, 74 insertions(+), 98 deletions(-) diff --git a/apps/example/src/adapters/apple.ts b/apps/example/src/adapters/apple.ts index da4717d5..dee0da43 100644 --- a/apps/example/src/adapters/apple.ts +++ b/apps/example/src/adapters/apple.ts @@ -17,8 +17,14 @@ export function createAppleLanguageSetupAdapter(): SetupAdapter // Image prompts need iOS 27 imageInput: Platform.OS === 'ios' && parseInt(String(Platform.Version), 10) >= 27, - isAvailable() { - return Platform.OS === 'ios' && apple.isAvailable() ? 'yes' : 'no' + async isAvailable() { + if (Platform.OS !== 'ios') return 'no' + try { + const { availability } = await apple.getModelInfo() + return availability === 'available' ? 'yes' : 'no' + } catch { + return 'no' + } }, async download() {}, async delete() {}, diff --git a/apps/example/src/adapters/llama.ts b/apps/example/src/adapters/llama.ts index 65f9ad48..b711d441 100644 --- a/apps/example/src/adapters/llama.ts +++ b/apps/example/src/adapters/llama.ts @@ -57,7 +57,7 @@ export function createLlamaLanguageSetupAdapter({ sizeBytes, builtIn: false, imageInput: Boolean(projectorId), - isAvailable(): Availability { + async isAvailable(): Promise { return files.every(isDownloaded) ? 'yes' : 'availableForDownload' }, async download(onProgress) { diff --git a/apps/example/src/adapters/types.ts b/apps/example/src/adapters/types.ts index a01e1fd8..a9cd7d42 100644 --- a/apps/example/src/adapters/types.ts +++ b/apps/example/src/adapters/types.ts @@ -24,7 +24,7 @@ export interface SetupAdapter { // Whether the model accepts images, omitted means text only imageInput?: boolean // Check if model is ready, unavailable, or downloadable - isAvailable: () => Availability + isAvailable: () => Promise // Download the model with progress callback download: (onProgress: (percentage: number) => void) => Promise // Remove the downloaded model from storage diff --git a/apps/example/src/components/FeatureRequirements.tsx b/apps/example/src/components/FeatureRequirements.tsx index 5995485f..e29dfacd 100644 --- a/apps/example/src/components/FeatureRequirements.tsx +++ b/apps/example/src/components/FeatureRequirements.tsx @@ -5,9 +5,9 @@ import type { Capability, Runtime } from '../adapters/types' import { isSimulator } from '../simulator' import type { FeatureId } from '../store/features' import { - isTranscriptionAvailable, useEmbeddingsStatus, useEnabledFeatures, + useTranscriptionAvailable, } from '../store/features' import { chatModels, @@ -128,6 +128,7 @@ function EmbeddingsRow() { // What a feature needs, with a way to get each missing piece export function FeatureRequirements({ feature }: { feature: FeatureId }) { const { isEnabled } = useEnabledFeatures() + const transcriptionAvailable = useTranscriptionAvailable() switch (feature) { case 'chat': return ( @@ -145,11 +146,11 @@ export function FeatureRequirements({ feature }: { feature: FeatureId }) { subtitle={ isSimulator ? 'Simulator preview: hears a scripted question' - : isTranscriptionAvailable() + : transcriptionAvailable ? 'Transcribes on device as you speak' : 'Not available for your language' } - state={isTranscriptionAvailable() ? 'ready' : 'unavailable'} + state={transcriptionAvailable ? 'ready' : 'unavailable'} /> { + if (isSimulator) return true + if (Platform.OS !== 'ios') return false + try { + return (await apple.transcriptionModel().getModelInfo()).isAvailable + } catch { + return false + } + }), + () => false +) + +export function useTranscriptionAvailable() { + return useAtomValue(transcriptionAvailableAtom) } // Apple's embedding assets ship separately and download on first prepare diff --git a/apps/example/src/store/models.ts b/apps/example/src/store/models.ts index 1167d99b..3d36e310 100644 --- a/apps/example/src/store/models.ts +++ b/apps/example/src/store/models.ts @@ -1,6 +1,6 @@ import type { LanguageModelV4 } from '@ai-sdk/provider' import { atom, useAtom, useAtomValue, useSetAtom } from 'jotai' -import { atomWithRefresh } from 'jotai/utils' +import { atomWithRefresh, unwrap } from 'jotai/utils' import { createAppleLanguageSetupAdapter } from '../adapters/apple' import { createLlamaLanguageSetupAdapter } from '../adapters/llama' @@ -44,12 +44,23 @@ export function formatSize(bytes: number) { } const availabilityAtom = atomWithRefresh( - () => + async () => new Map( - chatModels.map((adapter) => [adapter.modelId, adapter.isAvailable()]) + await Promise.all( + chatModels.map( + async (adapter) => + [adapter.modelId, await adapter.isAvailable()] as const + ) + ) ) ) +// Keeps the last result while a refresh is in flight +const availabilityValueAtom = unwrap( + availabilityAtom, + (prev) => prev ?? new Map() +) + const downloadProgressAtom = atom>({}) const selectedModelIdAtom = atomWithPersistence( @@ -63,7 +74,7 @@ let modelTransition = Promise.resolve() let activeModel: SetupAdapter | undefined export function useModels() { - const availability = useAtomValue(availabilityAtom) + const availability = useAtomValue(availabilityValueAtom) const refreshAvailability = useSetAtom(availabilityAtom) const setDownloadProgress = useSetAtom(downloadProgressAtom) const [selectedModelId, setSelectedModelId] = useAtom(selectedModelIdAtom) diff --git a/apps/example/src/store/readiness.ts b/apps/example/src/store/readiness.ts index 373b7ed3..1589e8ee 100644 --- a/apps/example/src/store/readiness.ts +++ b/apps/example/src/store/readiness.ts @@ -1,17 +1,18 @@ import type { FeatureId } from './features' -import { isTranscriptionAvailable, useEmbeddingsStatus } from './features' +import { useEmbeddingsStatus, useTranscriptionAvailable } from './features' import { useModels } from './models' // Whether each feature has what it needs on this device right now export function useFeatureReadiness(): Record { const { readyModels } = useModels() const { status: embeddingsStatus } = useEmbeddingsStatus() + const transcriptionAvailable = useTranscriptionAvailable() const hasChatModel = readyModels.length > 0 return { chat: hasChatModel, // Voice answers with the chat model - voice: hasChatModel && isTranscriptionAvailable(), + voice: hasChatModel && transcriptionAvailable, search: embeddingsStatus === 'ready', } } diff --git a/harness/apple/availability.harness.ts b/harness/apple/availability.harness.ts index 8540bcc0..880907dc 100644 --- a/harness/apple/availability.harness.ts +++ b/harness/apple/availability.harness.ts @@ -2,10 +2,6 @@ import { apple, AppleUtils } from '@react-native-ai/apple' import { describe, expect, it } from 'react-native-harness' describe('apple availability', () => { - it('is available', () => { - expect(apple.isAvailable()).toBe(true) - }) - it('describes the on-device model', async () => { const info = await apple.getModelInfo() diff --git a/harness/apple/transcription.device.harness.ts b/harness/apple/transcription.device.harness.ts index 2d3a0e6f..1a15dce4 100644 --- a/harness/apple/transcription.device.harness.ts +++ b/harness/apple/transcription.device.harness.ts @@ -15,12 +15,6 @@ beforeAll(async () => { }) describe('apple transcription', () => { - it('is available for english', () => { - expect(apple.transcriptionModel({ language: 'en-US' }).isAvailable()).toBe( - true - ) - }) - it('describes the locale a language resolves to', async () => { const model = apple.transcriptionModel({ language: 'en' }) await model.prepare() diff --git a/packages/apple-llm/ios/AppleLLM.mm b/packages/apple-llm/ios/AppleLLM.mm index 06cbe883..bc1219bb 100644 --- a/packages/apple-llm/ios/AppleLLM.mm +++ b/packages/apple-llm/ios/AppleLLM.mm @@ -239,9 +239,5 @@ - (void)generateStream:(nonnull NSString *)streamId messages:(nonnull NSArray *) } -- (nonnull NSNumber *)isAvailable { - return @([_llm isAvailable]); -} - @end diff --git a/packages/apple-llm/ios/AppleLLMImpl.swift b/packages/apple-llm/ios/AppleLLMImpl.swift index b30f7e15..171ddd02 100644 --- a/packages/apple-llm/ios/AppleLLMImpl.swift +++ b/packages/apple-llm/ios/AppleLLMImpl.swift @@ -54,19 +54,6 @@ public class AppleLLMImpl: NSObject { streamTasksLock.withLock { streamTasks.removeValue(forKey: id) } } - @objc - public func isAvailable() -> Bool { -#if canImport(FoundationModels) - if #available(iOS 26, *) { - return SystemLanguageModel.default.availability == .available - } else { - return false - } -#else - return false -#endif - } - @objc public func countTokens( _ text: String, diff --git a/packages/apple-llm/ios/transcription/AppleTranscription.mm b/packages/apple-llm/ios/transcription/AppleTranscription.mm index a179fe68..bc69f727 100644 --- a/packages/apple-llm/ios/transcription/AppleTranscription.mm +++ b/packages/apple-llm/ios/transcription/AppleTranscription.mm @@ -134,10 +134,6 @@ - (void)installJSIBindingsWithRuntime:(facebook::jsi::Runtime &)rt callInvoker:( return std::make_shared(params); } -- (nonnull NSNumber *)isAvailable:(nonnull NSString *)language { - return @([_transcription isAvailable:language]); -} - - (void)getModelInfo:(nonnull NSString *)language resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject { [_transcription getModelInfo:language resolve:resolve reject:reject]; } diff --git a/packages/apple-llm/ios/transcription/AppleTranscriptionImpl.swift b/packages/apple-llm/ios/transcription/AppleTranscriptionImpl.swift index 3f5d47f3..f360a16f 100644 --- a/packages/apple-llm/ios/transcription/AppleTranscriptionImpl.swift +++ b/packages/apple-llm/ios/transcription/AppleTranscriptionImpl.swift @@ -48,17 +48,6 @@ public class AppleTranscriptionImpl: NSObject { } } - /// Whether the device can transcribe. Locale support is only known - /// asynchronously, so getModelInfo reports it - @objc - public func isAvailable(_ language: String) -> Bool { - if #available(iOS 26, *) { - return SpeechTranscriber.isAvailable - } else { - return false - } - } - @objc public func getModelInfo(_ language: String, resolve: @escaping (Any?) -> Void, reject: @escaping (String, String, Error?) -> Void) { guard #available(iOS 26, *) else { diff --git a/packages/apple-llm/src/NativeAppleLLM.ts b/packages/apple-llm/src/NativeAppleLLM.ts index 1e916e6e..d05622ea 100644 --- a/packages/apple-llm/src/NativeAppleLLM.ts +++ b/packages/apple-llm/src/NativeAppleLLM.ts @@ -50,7 +50,6 @@ export type StreamErrorEvent = { } export interface Spec extends TurboModule { - isAvailable(): boolean getModelInfo( locale?: string, model?: string diff --git a/packages/apple-llm/src/NativeAppleTranscription.ts b/packages/apple-llm/src/NativeAppleTranscription.ts index e30d47d0..a83529ad 100644 --- a/packages/apple-llm/src/NativeAppleTranscription.ts +++ b/packages/apple-llm/src/NativeAppleTranscription.ts @@ -44,7 +44,6 @@ export interface AppleTranscriptionModelInfo { } export interface Spec extends TurboModule { - isAvailable(language: string): boolean getModelInfo(language: string): Promise prepare(language: string): Promise startLiveTranscription(sessionId: string, language: string): Promise @@ -205,8 +204,6 @@ export default { }, } }, - // Whether the device can transcribe. getModelInfo reports locale support - isAvailable: NativeAppleTranscription.isAvailable, getModelInfo: (language: string) => NativeAppleTranscription.getModelInfo( language diff --git a/packages/apple-llm/src/ai-sdk.ts b/packages/apple-llm/src/ai-sdk.ts index ab97a6ff..5c90aafc 100644 --- a/packages/apple-llm/src/ai-sdk.ts +++ b/packages/apple-llm/src/ai-sdk.ts @@ -958,7 +958,6 @@ export function createAppleProvider({ return createLanguageModel(options) } provider.specificationVersion = 'v4' as const - provider.isAvailable = () => NativeAppleLLM.isAvailable() provider.getModelInfo = ( options: { locale?: string; model?: AppleLanguageModelId } = {} ) => @@ -1065,12 +1064,7 @@ class AppleTranscriptionModel implements TranscriptionModelV4 { this.language = options.language ?? NativeAppleUtils.getCurrentLocale() } - /** Whether the device can transcribe. See getModelInfo for locale support */ - isAvailable(): boolean { - return NativeAppleTranscription.isAvailable(this.language) - } - - /** Locale support and asset status for the model's language */ + /** Whether the device can transcribe, locale support and asset status for the model's language */ getModelInfo() { return NativeAppleTranscription.getModelInfo(this.language) } diff --git a/skills/react-native-ai/references/apple-provider.md b/skills/react-native-ai/references/apple-provider.md index 844a5aa6..9f5fad94 100644 --- a/skills/react-native-ai/references/apple-provider.md +++ b/skills/react-native-ai/references/apple-provider.md @@ -42,13 +42,13 @@ npm add @react-native-ai/apple ```ts import { apple } from '@react-native-ai/apple' -if (apple.isAvailable()) { +const { availability } = await apple.getModelInfo() +if (availability === 'available') { // Use Apple provider } ``` -When it is unavailable, `(await apple.getModelInfo()).availability` gives the -reason: `'deviceNotEligible' | 'appleIntelligenceNotEnabled' | 'modelNotReady' +Otherwise `availability` gives the reason: `'deviceNotEligible' | 'appleIntelligenceNotEnabled' | 'modelNotReady' | 'systemNotReady' | 'unknown'`. `'appleIntelligenceNotEnabled'` is fixable by the user in Settings. diff --git a/website/src/content/docs/docs/apple/getting-started.mdx b/website/src/content/docs/docs/apple/getting-started.mdx index d7ac01fc..68030dae 100644 --- a/website/src/content/docs/docs/apple/getting-started.mdx +++ b/website/src/content/docs/docs/apple/getting-started.mdx @@ -37,7 +37,8 @@ Examples with schemas also use `zod`. import { apple } from '@react-native-ai/apple' import { generateText } from 'ai' -if (apple.isAvailable()) { +const { availability } = await apple.getModelInfo() +if (availability === 'available') { const { text } = await generateText({ model: apple(), prompt: 'Explain quantum computing in simple terms.', @@ -45,10 +46,9 @@ if (apple.isAvailable()) { } ``` -`isAvailable()` is `false` when the device is not eligible, Apple Intelligence -is off, or the model is still downloading. [`getModelInfo()`](/docs/apple/models#availability-and-model-info) -tells you which. Before iOS 26, `isAvailable()` is `false` and `getModelInfo()` -rejects with `UNSUPPORTED_OS`. +When the device is not eligible, Apple Intelligence is off, or the model is +still downloading, [`availability`](/docs/apple/models#availability-and-model-info) +tells you which. Before iOS 26, `getModelInfo()` rejects with `UNSUPPORTED_OS`. To test in the iOS Simulator, turn on Apple Intelligence on your Mac first, see [Running on Simulator](/docs/apple/running-on-simulator). diff --git a/website/src/content/docs/docs/apple/models.md b/website/src/content/docs/docs/apple/models.md index d075dc70..5a6efae9 100644 --- a/website/src/content/docs/docs/apple/models.md +++ b/website/src/content/docs/docs/apple/models.md @@ -8,22 +8,20 @@ Private Cloud Compute. ## Availability And Model Info -`apple.isAvailable()` is a quick synchronous check that Apple Intelligence can -run on the device: +`getModelInfo()` reports whether Apple Intelligence can run on the device, and +what it supports: ```typescript import { apple } from '@react-native-ai/apple' -if (!apple.isAvailable()) { +const info = await apple.getModelInfo({ locale: 'en-US' }) + +if (info.availability !== 'available') { // Show fallback UI or use another provider. } ``` -`getModelInfo()` tells you more: - -```typescript -const info = await apple.getModelInfo({ locale: 'en-US' }) -``` +Before iOS 26, it rejects with `UNSUPPORTED_OS`. It returns `availability`, locale support (`supportsLocale`, `supportedLanguages`), `contextSize` when Apple exposes it, the Private Cloud diff --git a/website/src/content/docs/docs/apple/transcription.md b/website/src/content/docs/docs/apple/transcription.md index c79fe201..33e70ee4 100644 --- a/website/src/content/docs/docs/apple/transcription.md +++ b/website/src/content/docs/docs/apple/transcription.md @@ -117,19 +117,19 @@ such as `fr` resolve to a supported locale: const model = apple.transcriptionModel({ language: 'fr' }) ``` -`isAvailable()` is a quick synchronous check that the device can transcribe at -all. `getModelInfo()` reports whether it supports the language, which locale it -resolves to, and whether its assets are on the device: +`getModelInfo()` reports whether the device can transcribe at all, whether it +supports the language, which locale it resolves to, and whether its assets are +on the device: ```tsx -if (model.isAvailable()) { - const info = await model.getModelInfo() - // { isAvailable: true, supportsLocale: true, locale: 'fr-FR', - // assets: 'installed' | 'downloading' | 'supported' | 'unsupported', - // supportedLocales: [...] } -} +const info = await model.getModelInfo() +// { isAvailable: true, supportsLocale: true, locale: 'fr-FR', +// assets: 'installed' | 'downloading' | 'supported' | 'unsupported', +// supportedLocales: [...] } ``` +Before iOS 26, it rejects with `UNSUPPORTED_OS`. + `assets` is `supported` when the locale can be used once `prepare()` downloads it. @@ -157,7 +157,6 @@ import { AppleTranscription } from '@react-native-ai/apple' const language = 'fr' -const isAvailable = AppleTranscription.isAvailable(language) const info = await AppleTranscription.getModelInfo(language) await AppleTranscription.prepare(language) diff --git a/website/src/site/content.ts b/website/src/site/content.ts index 5e2d0944..b290be57 100644 --- a/website/src/site/content.ts +++ b/website/src/site/content.ts @@ -140,7 +140,7 @@ export const faq = [ }, { q: 'Can I mix on-device and cloud models?', - a: 'Yes. They are all AI SDK models, so you can pick one per call, and fall back to a cloud provider when `apple.isAvailable()` is false.', + a: 'Yes. They are all AI SDK models, so you can pick one per call, and fall back to a cloud provider when `apple.getModelInfo()` reports the model is not available.', }, ];