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
5 changes: 4 additions & 1 deletion apps/example/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
"expo-build-properties",
{
"android": {
"minSdkVersion": 26
"minSdkVersion": 26,
"packagingOptions": {
"exclude": ["META-INF/INDEX.LIST", "META-INF/DEPENDENCIES"]
}
}
}
],
Expand Down
17 changes: 12 additions & 5 deletions apps/example/jest.harness.config.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
const ignored = [
// Tests that need a physical device are left out on simulators
...(process.env.HARNESS_SIMULATOR === 'true'
? ['\\.device\\.harness\\.ts$']
: []),
// Each platform leaves out the providers that only run on the other one
...(process.env.HARNESS_PLATFORM === 'android'
? ['/harness/apple/', '/harness/core-ai/', '/harness/json-ui/apple\\.']
: ['/harness/adk/']),
]

// The tests live in the harness folder at the monorepo root
export default {
preset: 'react-native-harness',
roots: ['<rootDir>/../../harness'],
testMatch: ['**/*.harness.ts'],
// Tests that need a physical device are left out on simulators
testPathIgnorePatterns:
process.env.HARNESS_SIMULATOR === 'true'
? ['\\.device\\.harness\\.ts$']
: [],
testPathIgnorePatterns: ignored,
setupFiles: ['<rootDir>/../../harness/setup.ts'],
// The test files are few, so a crawl is fast and does not depend on Watchman
watchman: false,
Expand Down
5 changes: 4 additions & 1 deletion apps/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
"ios": "expo run:ios",
"typecheck": "tsc --noEmit",
"test:harness:simulator": "scripts/test-harness.sh simulator",
"test:harness:device": "scripts/test-harness.sh device"
"test:harness:device": "scripts/test-harness.sh device",
"test:harness:android": "scripts/test-harness.sh android"
},
"dependencies": {
"@ai-sdk/provider": "^4.0.33",
"@azure/core-asynciterator-polyfill": "^1.0.2",
"@expo/ui": "~58.0.23",
"@expo/vector-icons": "^15.0.3",
"@legendapp/list": "^3.7.0",
"@react-native-ai/adk": "workspace:*",
"@react-native-ai/apple": "workspace:*",
"@react-native-ai/core-ai": "workspace:*",
"@react-native-ai/json-ui": "workspace:*",
Expand Down Expand Up @@ -55,6 +57,7 @@
},
"devDependencies": {
"@babel/core": "^7.20.0",
"@react-native-harness/platform-android": "1.6.0",
"@react-native-harness/platform-apple": "1.6.0",
"@types/react": "~19.3.0",
"@types/ungap__structured-clone": "^1.2.0",
Expand Down
20 changes: 19 additions & 1 deletion apps/example/rn-harness.config.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
androidPlatform,
physicalAndroidDevice,
} from '@react-native-harness/platform-android'
import {
applePhysicalDevice,
applePlatform,
Expand All @@ -8,9 +12,14 @@ const deviceName = process.env.HARNESS_IOS_DEVICE
// Builds signed with entitlements the default App ID lacks use another one
const bundleId = process.env.HARNESS_IOS_BUNDLE_ID ?? 'com.callstack.ai.example'

const androidDevice = process.env.HARNESS_ANDROID_DEVICE
const androidManufacturer = process.env.HARNESS_ANDROID_MANUFACTURER ?? 'Google'

// Tests run in a debug build of the app. The ios runner uses the physical
// device named with HARNESS_IOS_DEVICE (as `xcrun devicectl list devices`
// shows it), and ios-simulator a simulator
// shows it), and ios-simulator a simulator. The android runner uses the
// physical device whose model is HARNESS_ANDROID_DEVICE (as
// `adb shell getprop ro.product.model` shows it, such as Pixel 9 Pro)
export default {
entryPoint: './index.js',
appRegistryComponentName: 'main',
Expand All @@ -29,6 +38,15 @@ export default {
device: appleSimulator('iPhone 18 Pro', '27.0'),
bundleId: 'com.callstack.ai.example',
}),
...(androidDevice
? [
androidPlatform({
name: 'android',
device: physicalAndroidDevice(androidManufacturer, androidDevice),
bundleId: 'com.callstack.ai.example',
}),
]
: []),
],
defaultRunner: deviceName ? 'ios' : 'ios-simulator',
// On-device models load and generate slowly, but a test that hangs should
Expand Down
27 changes: 26 additions & 1 deletion apps/example/scripts/test-harness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#
# bun run test:harness:simulator [test path]
# HARNESS_IOS_DEVICE="My iPhone" bun run test:harness:device [test path]
# HARNESS_ANDROID_DEVICE="Pixel 9 Pro" bun run test:harness:android [test path]

set -euo pipefail

Expand Down Expand Up @@ -35,8 +36,32 @@ case "$TARGET" in
npx expo run:ios --device "$HARNESS_IOS_DEVICE" --no-bundler
npx react-native-harness --harnessRunner ios "$@"
;;
android)
if [[ -z "${HARNESS_ANDROID_DEVICE:-}" ]]; then
echo 'Set HARNESS_ANDROID_DEVICE to the device model, as `adb shell getprop ro.product.model` shows it' >&2
exit 1
fi
# Harness runs the tests on the first connected device of that model and
# ignores ANDROID_SERIAL, so exactly one may be connected, and the build is
# installed on it
matching=()
for serial in $(adb devices | awk 'NR > 1 && $2 == "device" { print $1 }'); do
model="$(adb -s "$serial" shell getprop ro.product.model | tr -d '\r')"
if [[ "$model" == "$HARNESS_ANDROID_DEVICE" ]]; then
matching+=("$serial")
fi
done
if [[ "${#matching[@]}" != 1 ]]; then
echo "Connect exactly one $HARNESS_ANDROID_DEVICE, found ${#matching[@]}" >&2
exit 1
fi
export ANDROID_SERIAL="${matching[0]}"
npx expo prebuild --clean --platform android
npx expo run:android --no-bundler --device "$ANDROID_SERIAL"
HARNESS_PLATFORM=android npx react-native-harness --harnessRunner android "$@"
;;
*)
echo "Usage: $0 simulator|device [test path]" >&2
echo "Usage: $0 simulator|device|android [test path]" >&2
exit 1
;;
esac
3 changes: 3 additions & 0 deletions apps/example/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
""
],
"paths": {
"@react-native-ai/adk": [
"../../packages/adk/src"
],
"@react-native-ai/apple": [
"../../packages/apple-llm/src"
],
Expand Down
6 changes: 6 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions harness/adk/availability.harness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { adk } from '@react-native-ai/adk'
import { describe, expect, it } from 'react-native-harness'

// Gemini Nano runs only on physical devices with AICore, such as a Pixel 9
describe('adk availability', () => {
it('reports Gemini Nano details', async () => {
const info = await adk.getModelInfo()

expect(['available', 'downloadable', 'downloading']).toContain(
info.availability
)
})

// On a fresh device this downloads Gemini Nano, which takes minutes
it('prepares Gemini Nano with download progress', async () => {
await adk().prepare({ onProgress: () => {} })

const info = await adk.getModelInfo()
expect(info.availability).toBe('available')
expect(info.contextSize).toBeGreaterThan(0)
expect(await adk.isAvailable()).toBe(true)
})

it('counts tokens with the Gemini Nano tokenizer', async () => {
expect(await adk().countTokens('How many tokens is this?')).toBeGreaterThan(
0
)
})
})
111 changes: 111 additions & 0 deletions harness/adk/generate.harness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { adk, createAdkProvider } from '@react-native-ai/adk'
import { generateText, jsonSchema, streamText, tool } from 'ai'
import {
beforeAll,
beforeEach,
describe,
expect,
it,
} from 'react-native-harness'

import { cooldown, prompt } from './helpers'

describe('adk gemini nano', () => {
beforeAll(async () => {
await adk().prepare()
})
beforeEach(cooldown)

it('answers a prompt', async () => {
const result = await adk().doGenerate({
prompt: prompt('Reply with exactly the word BANANA and nothing else.'),
})

expect(result.content.at(-1)).toMatchObject({
type: 'text',
text: expect.stringMatching(/banana/i),
})
expect(result.finishReason.unified).toBe('stop')
})

it('follows the system message', async () => {
const { text } = await generateText({
model: adk(),
system: 'Whatever the user says, reply with only the word PINEAPPLE.',
prompt: 'How are you?',
})

expect(text).toMatch(/pineapple/i)
})

it('follows the provider instruction', async () => {
const model = createAdkProvider({
instruction: 'Whatever the user says, reply with only the word MANGO.',
})()

const { text } = await generateText({ model, prompt: 'How are you?' })

expect(text).toMatch(/mango/i)
})

it('remembers earlier turns', async () => {
const { text } = await generateText({
model: adk(),
messages: [
{ role: 'user', content: 'My favourite colour is teal. Just say OK.' },
{ role: 'assistant', content: 'OK' },
{
role: 'user',
content: 'What is my favourite colour? Answer with one word.',
},
],
})

expect(text).toMatch(/teal/i)
})

it('streams text', async () => {
const result = streamText({
model: adk(),
prompt: 'Count from 1 to 5, separated by spaces.',
})

let deltas = 0
for await (const delta of result.textStream) {
if (delta) deltas++
}
expect(await result.text).toMatch(/1.*2.*3.*4.*5/s)
expect(deltas).toBeGreaterThan(0)
})

it('warns that tools are unsupported', async () => {
const { warnings } = await generateText({
model: adk(),
prompt: 'What is the weather in Paris?',
tools: {
getWeather: tool({
inputSchema: jsonSchema<{ city: string }>({ type: 'object' }),
execute: async () => 'Sunny',
}),
},
})

expect(warnings).toContainEqual(
expect.objectContaining({ type: 'unsupported', feature: 'tools' })
)
})

it('warns that JSON output is unsupported', async () => {
const { warnings } = await adk().doGenerate({
prompt: prompt('Describe a cat.'),
responseFormat: { type: 'json' },
})

expect(warnings).toContainEqual(
expect.objectContaining({
type: 'unsupported',
feature: 'responseFormat',
})
)
})
})
24 changes: 24 additions & 0 deletions harness/adk/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type {
LanguageModelV4CallOptions,
LanguageModelV4StreamPart,
} from '@ai-sdk/provider'

export function prompt(text: string): LanguageModelV4CallOptions['prompt'] {
return [{ role: 'user', content: [{ type: 'text', text }] }]
}

export async function readAll(
stream: ReadableStream<LanguageModelV4StreamPart>
) {
const parts: LanguageModelV4StreamPart[] = []
const reader = stream.getReader()
while (true) {
const { done, value } = await reader.read()
if (done) return parts
parts.push(value)
}
}

// AICore rejects requests an app sends in quick succession with a quota error
export const cooldown = () =>
new Promise<void>((resolve) => setTimeout(resolve, 3000))