Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion harness/adk/availability.harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ describe('adk availability', () => {
const info = await adk.getModelInfo()
expect(info.availability).toBe('available')
expect(info.contextSize).toBeGreaterThan(0)
expect(await adk.isAvailable()).toBe(true)
expect(adk.isAvailable()).toBe(true)
})

it('checks availability synchronously', () => {
expect(typeof adk.isAvailable()).toBe('boolean')
expect(adk().isAvailable()).toBe(adk.isAvailable())
})

it('counts tokens with the Gemini Nano tokenizer', async () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/adk/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.callstack.ai.adk">
<!-- Lets isAvailable() check whether AICore, which runs Gemini Nano, is installed -->
<queries>
<package android:name="com.google.android.aicore" />
</queries>
</manifest>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.callstack.ai.adk

import android.content.Context
import android.content.pm.PackageManager
import android.net.Uri
import android.util.Base64
import com.facebook.react.bridge.ReadableArray
Expand Down Expand Up @@ -39,6 +40,8 @@ import kotlinx.coroutines.sync.withLock
import java.util.UUID
import java.util.concurrent.ConcurrentHashMap

private const val AICORE_PACKAGE = "com.google.android.aicore"

data class AdkRunResult(
val content: String,
val reasoning: String?,
Expand Down Expand Up @@ -82,6 +85,26 @@ class AdkAgentRunner(
) {
private val nanoModels = ConcurrentHashMap<NanoConfig, GenerativeModel>()
private val nanoPrepareMutex = Mutex()
// The last Gemini Nano status seen per model selection, for the synchronous check
private val nanoStatuses = ConcurrentHashMap<NanoConfig, Int>()

/**
* Whether Gemini Nano is available or downloadable, answered without async work: from the last
* status this process saw, or else from whether AICore is installed.
*/
fun isNanoAvailable(config: NanoConfig): Boolean {
// The latest status wins over a model prepared earlier, which may have gone away since
nanoStatuses[config]?.let {
return it == FeatureStatus.AVAILABLE || it == FeatureStatus.DOWNLOADABLE
}
if (nanoModels.containsKey(config)) return true
return try {
context.packageManager.getPackageInfo(AICORE_PACKAGE, 0)
true
} catch (_: PackageManager.NameNotFoundException) {
false
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

private fun nanoClient(config: NanoConfig): GenerativeModel {
if (config.releaseStage == null && config.preference == null) {
Expand Down Expand Up @@ -109,7 +132,9 @@ class AdkAgentRunner(
if (nanoModels.containsKey(config)) return

val model = nanoClient(config)
when (val status = model.checkStatus()) {
val status = model.checkStatus()
nanoStatuses[config] = status
when (status) {
FeatureStatus.UNAVAILABLE ->
throw AdkErrors.AdkException(
AdkErrors.MODEL_UNAVAILABLE,
Expand All @@ -119,6 +144,7 @@ class AdkAgentRunner(
}
model.warmup()
nanoModels[config] = model
nanoStatuses[config] = FeatureStatus.AVAILABLE
}
}

Expand Down Expand Up @@ -165,6 +191,7 @@ class AdkAgentRunner(
} catch (_: Exception) {
return AdkModelInfo(availability = "unknown")
}
nanoStatuses[config] = status

return when (status) {
FeatureStatus.UNAVAILABLE -> AdkModelInfo(availability = "unavailable")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class NativeAdkEngineModule(reactContext: ReactApplicationContext) :
reject(AdkErrors.codeFor(error), error.message ?: "Unknown ADK error", error)
}

override fun isAvailable(nano: ReadableMap?): Boolean =
runner.isNanoAvailable(NanoConfig.from(nano))

override fun getModelInfo(nano: ReadableMap?, promise: Promise) {
scope.launch {
try {
Expand Down
2 changes: 2 additions & 0 deletions packages/adk/src/NativeAdkEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ export interface NanoDownloadProgressEvent {
}

export interface Spec extends TurboModule {
/** Quick synchronous check, see `AdkProvider.isAvailable` */
isAvailable(nano?: AdkNanoModelConfig): boolean
getModelInfo(nano?: AdkNanoModelConfig): Promise<AdkNativeModelInfo>
prepareNano(nano?: AdkNanoModelConfig): Promise<void>
countTokens(text: string, nano?: AdkNanoModelConfig): Promise<number>
Expand Down
16 changes: 9 additions & 7 deletions packages/adk/src/__tests__/ai-sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const streamText = mock(
)
const prepareNano = mock(async (_nano?: unknown) => {})
const cancelStream = mock(async (_id: string) => {})
const isAvailable = mock((_nano?: unknown) => true)
const getModelInfo = mock(async (_nano?: unknown) => ({
availability: 'downloadable',
}))
Expand All @@ -59,6 +60,7 @@ mock.module('react-native', () => ({
prepareNano,
cancelStream,
getModelInfo,
isAvailable,
onStreamUpdate,
onStreamComplete,
onStreamError,
Expand Down Expand Up @@ -145,14 +147,14 @@ describe('provider', () => {
expect(getModelInfo.mock.calls.at(-1)![0]).toEqual({ preference: 'fast' })
})

test('derives isAvailable from the model info', async () => {
const provider = createAdkProvider()
expect(await provider.isAvailable()).toBe(true)
test('checks availability synchronously', () => {
const provider = createAdkProvider({ nano: { preference: 'fast' } })

getModelInfo.mockImplementationOnce(async () => ({
availability: 'downloading',
}))
expect(await provider.isAvailable()).toBe(false)
expect(provider.isAvailable()).toBe(true)
expect(isAvailable.mock.calls.at(-1)).toEqual([{ preference: 'fast' }])

isAvailable.mockImplementationOnce(() => false)
expect(provider().isAvailable()).toBe(false)
})
})

Expand Down
23 changes: 17 additions & 6 deletions packages/adk/src/ai-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export interface AdkPrepareOptions {
}

export interface AdkLanguageModel extends LanguageModelV4 {
/** Quick synchronous check, see `AdkProvider.isAvailable` */
isAvailable: () => boolean
/** Downloads Gemini Nano when needed and loads it, reporting download progress */
prepare: (options?: AdkPrepareOptions) => Promise<void>
/** Counts tokens with the Gemini Nano tokenizer */
Expand All @@ -79,8 +81,12 @@ export interface AdkProvider {
(options?: AdkLanguageModelOptions): AdkLanguageModel
readonly specificationVersion: 'v4'
languageModel: (options?: AdkLanguageModelOptions) => AdkLanguageModel
/** Whether Gemini Nano can be used now: it is `available` or `downloadable` */
isAvailable: () => Promise<boolean>
/**
* 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
*/
isAvailable: () => boolean
/** Gemini Nano's availability, for the provider's model selection or `nano` */
getModelInfo: (options?: {
nano?: AdkNanoModelConfig
Expand Down Expand Up @@ -114,6 +120,10 @@ const listenForDownloadProgress = (
)
: null

function isAvailable(nano: AdkNanoModelConfig | undefined) {
return Platform.OS === 'android' && getNativeAdkEngine().isAvailable(nano)
}

async function getModelInfo(
nano: AdkNanoModelConfig | undefined
): Promise<AdkModelInfo> {
Expand Down Expand Up @@ -144,10 +154,7 @@ export function createAdkProvider(
}
provider.specificationVersion = 'v4' as const
provider.languageModel = createLanguageModel
provider.isAvailable = async () => {
const { availability } = await getModelInfo(defaults.nano)
return availability === 'available' || availability === 'downloadable'
}
provider.isAvailable = () => isAvailable(defaults.nano)
provider.getModelInfo = (options: { nano?: AdkNanoModelConfig } = {}) =>
getModelInfo(options.nano ?? defaults.nano)
return provider
Expand Down Expand Up @@ -317,6 +324,10 @@ class AdkChatLanguageModel implements AdkLanguageModel {
}
}

isAvailable(): boolean {
return isAvailable(this.agentConfig.nano)
}

async prepare(options: AdkPrepareOptions = {}): Promise<void> {
const listener = listenForDownloadProgress(options.onProgress)
try {
Expand Down
2 changes: 1 addition & 1 deletion website/src/content/docs/docs/adk/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Pass `adk()` to the AI SDK. Check that Gemini Nano can run on the device first:
import { adk } from '@react-native-ai/adk'
import { generateText } from 'ai'

if (await adk.isAvailable()) {
if (adk.isAvailable()) {
const { text } = await generateText({
model: adk(),
prompt: 'Summarize on-device AI in one sentence.',
Expand Down
18 changes: 15 additions & 3 deletions website/src/content/docs/docs/adk/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,21 @@ switch (info.availability) {
When Gemini Nano is `available`, `getModelInfo()` also returns `baseModelName`
and `contextSize`, its input token limit.

`adk.isAvailable()` is the yes/no version: it's `true` when Gemini Nano is
`available` or `downloadable`. Both are cheap, never download anything, and are
safe to call at startup and again when the app resumes. See the
`adk.isAvailable()` and `model.isAvailable()` are the yes/no version. Like
`apple.isAvailable()`, they return a boolean synchronously, so UI code can call
them directly:

```typescript
if (adk.isAvailable()) {
// Show Gemini Nano in the model picker
}
```

ML Kit only reports Gemini Nano's status asynchronously, so the synchronous
check uses the last status `getModelInfo()` or `prepare()` saw, or else whether
AICore is installed. Call `getModelInfo()` for the exact state, such as whether
Gemini Nano still needs a download. Neither call downloads anything, and both
are safe at startup and again when the app resumes. See the
[ML Kit GenAI documentation](https://developers.google.com/ml-kit/genai#feature-device)
for the supported devices.

Expand Down