diff --git a/README.md b/README.md index 27dec4df2..83f314e59 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ keep the panel mounted without receiving telemetry. | Provider | Built-in | Platforms | Runtime | Description | | ------------------- | -------- | ------------ | ------------------------------------------------------------------- | ---------------------------------------------------------- | | [Apple](#apple) | ✅ Yes | iOS | [Apple](https://developer.apple.com/documentation/FoundationModels) | Apple Foundation Models, embeddings, transcription, speech | -| [ADK](#google-adk) | ✅ Yes | Android | [Google ADK](https://developer.android.com/ai/adk) | On-device Gemini Nano, with optional cloud Gemini | +| [Android](#android) | ✅ Yes | Android | [ML Kit GenAI](https://developers.google.com/ml-kit/genai) | On-device Gemini Nano, transcription and embeddings | | [Core AI](#core-ai) | ❌ No | iOS | [Core AI](https://github.com/apple/coreai-models) | Text, image, video, speech, embedding and vision models | | [Llama](#llama) | ❌ No | iOS, Android | [llama.rn](https://github.com/mybigday/llama.rn) | Run GGUF models via llama.rn | @@ -115,42 +115,41 @@ See the [Apple documentation](https://react-native-ai.dev/docs/apple/getting-sta --- -### Google ADK +### Android -Run Gemini Nano on-device on Android through Google's [Agent Development Kit](https://developer.android.com/ai/adk). **Built-in** - Gemini Nano is provisioned by the system via AICore, so there are no model files to bundle and no API key. Cloud Gemini is available as an option. +Run Gemini Nano on-device on Android through Google's [ML Kit GenAI](https://developers.google.com/ml-kit/genai) libraries. **Built-in** - Gemini Nano is provided by the system through AICore, so there are no model files to bundle and no API key. -- **Text Generation** - `generateText` and `streamText` with on-device Gemini Nano -- **Tool Calling** - Native ADK agent loop with JavaScript tool executors -- **Cloud Gemini** - Optional cloud inference via ADK `LlmAgent` when you need a larger model +- **Text Generation** - `generateText` and `streamText` with on-device Gemini Nano, with images and thinking +- **Tool Calling** - AI SDK tools with Gemini Nano 4's function calling +- **Transcription** - Speech to text with ML Kit speech recognition +- **Embeddings** - On-device text embeddings #### Installation ```bash -npm install @react-native-ai/adk +npm install @react-native-ai/android ``` > [!IMPORTANT] -> Requires Android `minSdkVersion` 26 and a physical device with AICore support (Android 14+) for Gemini Nano. Your app also needs packaging excludes for duplicate `META-INF` files, see the [getting started guide](https://react-native-ai.dev/docs/adk/getting-started). +> Requires Android `minSdkVersion` 26, Kotlin 2.4 and a physical device with AICore support. See the [getting started guide](https://react-native-ai.dev/docs/android/getting-started). #### Usage ```typescript -import { adk } from '@react-native-ai/adk' +import { android } from '@react-native-ai/android' import { generateText } from 'ai' -if (!(await adk.isAvailable())) { - throw new Error('Gemini Nano is not available on this device') +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(), + prompt: 'Explain quantum computing', + }) } - -await adk.prepareNano() - -const { text } = await generateText({ - model: adk(), - prompt: 'Explain quantum computing', -}) ``` -See the [ADK documentation](https://react-native-ai.dev/docs/adk/getting-started) for device checks, cloud Gemini, and tool calling. +See the [Android documentation](https://react-native-ai.dev/docs/android/getting-started) for device checks, tool calling, transcription and embeddings. --- diff --git a/bun.lock b/bun.lock index 7d977b766..72d8a3268 100644 --- a/bun.lock +++ b/bun.lock @@ -86,20 +86,6 @@ "react-native-harness": "1.6.0", }, }, - "packages/adk": { - "name": "@react-native-ai/adk", - "version": "0.12.1", - "dependencies": { - "@ai-sdk/provider": "^4.0.33", - "@ai-sdk/provider-utils": "^5.0.67", - }, - "devDependencies": { - "react-native": "0.88.0", - }, - "peerDependencies": { - "react-native": ">=0.76.0", - }, - }, "packages/android": { "name": "@react-native-ai/android", "version": "0.12.0", @@ -954,8 +940,6 @@ "@radix-ui/react-use-layout-effect": ["@radix-ui/react-use-layout-effect@1.1.4", "", { "peerDependencies": { "@types/react": "*", "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" }, "optionalPeers": ["@types/react"] }, "sha512-K20DkRkUwDnxEYMBPcg3Y6voLkEy5p5QQmszZgLngKKiC7dzBR/aEuK3w1qlx2JWDUNH6FluahYdgR3BP+QbYw=="], - "@react-native-ai/adk": ["@react-native-ai/adk@workspace:packages/adk"], - "@react-native-ai/android": ["@react-native-ai/android@workspace:packages/android"], "@react-native-ai/apple": ["@react-native-ai/apple@workspace:packages/apple-llm"], diff --git a/docs/DESIGN.md b/docs/DESIGN.md index 0241626c3..987d1fbea 100644 --- a/docs/DESIGN.md +++ b/docs/DESIGN.md @@ -71,7 +71,7 @@ the mark, then the wordmark `REACT NATIVE AI` (`on-accent` on an `accent` block) Both fonts are free and self-hosted in `website/src/fonts/`. Headlines run in two beats with the second in the accent: "On-device AI for your mobile app. *Every platform’s models, one API.*" That is the motto. Mono is for labels only, never full sentences. Platforms are always in the order iOS, Android, and -runtimes in the order Apple, Core AI, ADK, Llama. +runtimes in the order Apple, Core AI, Android, Llama. ## Layout @@ -95,7 +95,7 @@ type sizes scaled from the hero headline. the steps. Inside: a mono status row (`9:41`, `✈ AIRPLANE MODE`), the user's bubble on an accent fill, the reply in a hairline box, and an accent square marking `apple() · on device`. - **Runtime cells:** one per runtime, labelled `NATIVE · BY CALLSTACK` (accent) for Apple, Core AI - and ADK, or `THIRD-PARTY BINDING` (muted) for Llama, which wraps llama.rn. + and Android, or `THIRD-PARTY BINDING` (muted) for Llama, which wraps llama.rn. - **Closing:** a full-width accent CTA bar, the giant accent wordmark, then one footer row. ## Pixel imagery @@ -127,7 +127,7 @@ about 12 fps for dither, and cuts rather than cross-fades. ## Words Plain and specific. Say what becomes possible: on-device AI in a mobile app, using the models -each platform provides, through one drop-in AI SDK API, with no server and no API key. Keep API names out of headlines and ledes. Apple, Core AI and ADK are native (`NATIVE · BY CALLSTACK`): they run on the AI stack the +each platform provides, through one drop-in AI SDK API, with no server and no API key. Keep API names out of headlines and ledes. Apple, Core AI and Android are native (`NATIVE · BY CALLSTACK`): they run on the AI stack the platform provides. Llama is a `THIRD-PARTY BINDING`: a provider over llama.rn. Page titles are `React Native AI: On-device AI for React Native apps` on the home page and `Page title | React Native AI` elsewhere. Facts come only from the docs: no invented benchmarks, adopters or testimonials. Mark JSON UI as experimental. diff --git a/packages/adk/README.md b/packages/adk/README.md deleted file mode 100644 index ac720cbd4..000000000 --- a/packages/adk/README.md +++ /dev/null @@ -1,206 +0,0 @@ -# ADK Provider for Vercel AI SDK - -A Vercel AI SDK provider for [Google's Agent Development Kit (ADK)](https://developer.android.com/ai/adk) on Android. Use on-device Gemini Nano or cloud Gemini with tool calling and multi-turn sessions. - -**Requirements:** - -- Android `minSdkVersion` 26 or greater (required by ML Kit GenAI / Gemini Nano; set in your app, not only in this package) -- React Native New Architecture -- Vercel AI SDK v7 -- If using on-device models: a device capable of running Gemini Nano (Android 14+ with AICore); you can consult the [ML Kit GenAI documentation](https://developers.google.com/ml-kit/genai#feature-device) for more details. - -Consuming apps must set `minSdkVersion` to at least 26. ADK pulls in Google GenAI libraries that duplicate `META-INF/INDEX.LIST`; exclude it in your app packaging. For Expo: - -```json -[ - "expo-build-properties", - { - "android": { - "minSdkVersion": 26, - "packagingOptions": { - "exclude": ["META-INF/INDEX.LIST", "META-INF/DEPENDENCIES"] - } - } - } -] -``` - -For bare React Native, add to `android/app/build.gradle`: - -```gradle -android { - packagingOptions { - excludes += ["META-INF/INDEX.LIST", "META-INF/DEPENDENCIES"] - } -} -``` - -Then run `npx expo prebuild --clean` so the native Android project picks up the change. - -```ts -import { adk } from '@react-native-ai/adk' -import { generateText } from 'ai' - -const { text } = await generateText({ - model: adk(), - prompt: 'What time is it in New York?', -}) -``` - -## Features - -- Cloud Gemini model via ADK `LlmAgent` and `InMemoryRunner` -- On-device Gemini Nano via ML Kit GenAI (`genai-nano` model type) -- Tool calling bridged to JavaScript executors -- Streaming responses with tool-call stream parts -- Post-generation token usage via ADK `UsageMetadata` -- Multimodal user prompts (text + inline images) -- Structured JSON output via `responseMimeType` (schema constraints not yet supported) -- Vercel AI SDK v7 `LanguageModelV4` provider - -## Usage metadata - -ADK returns token usage in response events (`promptTokenCount`, `candidatesTokenCount`, `totalTokenCount`). The provider maps this into AI SDK `usage` on both `generateText` and streaming `finish` events. - -## Multimodal input - -Pass file parts in user messages using the standard AI SDK prompt format: - -```ts -import { generateText } from 'ai' - -const { text } = await generateText({ - model: adk(), - messages: [ - { - role: 'user', - content: [ - { type: 'text', text: 'What is in this image?' }, - { - type: 'file', - mediaType: 'image/jpeg', - data: base64Image, - }, - ], - }, - ], -}) -``` - -## Structured output - -Streaming structured JSON is not supported by ADK yet. - -## Cloud Gemini - -```ts -import { createAdkProvider } from '@react-native-ai/adk' -import { generateText } from 'ai' - -const adk = createAdkProvider({ - apiKey: process.env.GOOGLE_API_KEY, - modelType: 'gemini', - modelName: 'gemini-2.5-flash', - instruction: 'You are a helpful assistant.', -}) - -const { text } = await generateText({ - model: adk(), - prompt: 'Summarize on-device AI in one sentence.', -}) -``` - -> Do not embed API keys in production client apps. Prefer a backend proxy or secure runtime configuration. - -## On-device Gemini Nano - -Gemini Nano has two separate availability checks: - -| API | Label | Question | -| ------------------------------- | --------------------- | ------------------------------------------ | -| `adk.isNanoSupported()` | **Device capability** | Can this device ever run Nano? | -| `adk.isAvailable('genai-nano')` | **Runtime readiness** | Can I call `prepareNano()` / generate now? | - -If `isNanoSupported()` is `false`, `isAvailable('genai-nano')` is also `false`. - -### ML Kit status codes - -Both checks use ML Kit `Generation.getClient().checkStatus()`: - -| Status | `isNanoSupported()` | `isAvailable('genai-nano')` | Suggested UX | -| -------------- | ------------------- | --------------------------- | ---------------------------------------- | -| `0` | `false` | `false` | Hide or disable — not supported | -| `1` | `true` | `true` | Ready — call `prepareNano()` | -| `3` | `true` | `true` | Ready to download — call `prepareNano()` | -| Other non-zero | `true` | `false` | Show disabled — not ready yet | - -See [ML Kit GenAI Prompt API](https://developers.google.com/ml-kit/genai) for details. - -### Recommended flow - -```ts -import { createAdkProvider } from '@react-native-ai/adk' - -const adk = createAdkProvider({ - modelType: 'genai-nano', - modelName: 'gemini-nano', -}) - -const supported = await adk.isNanoSupported() -if (!supported) { - // Device lacks Gemini Nano / AICore support — hide from model picker - return -} - -const ready = await adk.isAvailable('genai-nano') -if (!ready) { - // Device supports Nano but ML Kit is not ready yet (e.g. downloading) - // Show disabled — do not mark as "Ready" - return -} - -await adk.prepareNano() -const model = adk() -``` - -Both checks are cheap native calls (no model download). Safe to cache at app startup and re-check after resume. - -> **Note:** `model.prepare()`, `generateText()`, and `streamText()` auto-call `prepareNano()` for `genai-nano` models, but only gate on `isNanoSupported()`. For UI gating, always use `isAvailable('genai-nano')` and handle prepare/generation errors in your chat flow. - -## Tool calling - -```ts -import { createAdkProvider } from '@react-native-ai/adk' -import { generateText, tool } from 'ai' -import { z } from 'zod' - -const getCurrentTime = tool({ - description: 'Get the current time for a city', - inputSchema: z.object({ - city: z.string(), - }), - execute: async ({ city }) => ({ - city, - time: new Date().toLocaleTimeString(), - }), -}) - -const adk = createAdkProvider({ - apiKey: process.env.GOOGLE_API_KEY, - availableTools: { getCurrentTime }, -}) - -const { text } = await generateText({ - model: adk(), - tools: { getCurrentTime }, - prompt: 'What time is it in Warsaw?', -}) -``` - -During streaming, the provider emits AI SDK `tool-input-*` and `tool-call` stream parts when ADK surfaces function calls from the model. - -Pass tools through the AI SDK `tools` option as usual; the provider bridges execution to JavaScript while ADK orchestrates the agent loop natively. - -## License - -MIT diff --git a/packages/adk/android/build.gradle b/packages/adk/android/build.gradle deleted file mode 100644 index 458bb0e57..000000000 --- a/packages/adk/android/build.gradle +++ /dev/null @@ -1,110 +0,0 @@ -import com.android.Version - -buildscript { - def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["ADK_kotlinVersion"] - - repositories { - google() - mavenCentral() - } - - dependencies { - classpath "com.android.tools.build:gradle:7.2.1" - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - } -} - -apply plugin: "com.android.library" -apply plugin: "kotlin-android" -apply plugin: "com.facebook.react" - -def getExtOrDefault(name) { - return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["ADK_" + name] -} - -def getExtOrIntegerDefault(name) { - return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["ADK_" + name]).toInteger() -} - -static def supportsNamespace() { - def parsed = Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.') - def major = parsed[0].toInteger() - def minor = parsed[1].toInteger() - - return (major == 7 && minor >= 3) || major >= 8 -} - -android { - if (supportsNamespace()) { - namespace "com.callstack.ai.adk" - - sourceSets { - main { - manifest.srcFile "src/main/AndroidManifest.xml" - } - } - } - - compileSdkVersion getExtOrIntegerDefault("compileSdkVersion") - - defaultConfig { - minSdkVersion getExtOrIntegerDefault("minSdkVersion") - targetSdkVersion getExtOrIntegerDefault("targetSdkVersion") - } - - buildFeatures { - buildConfig true - } - - buildTypes { - release { - minifyEnabled false - } - } - - lintOptions { - disable "GradleCompatible" - } - - compileOptions { - sourceCompatibility JavaVersion.VERSION_17 - targetCompatibility JavaVersion.VERSION_17 - } - - sourceSets { - main { - java.srcDirs += [ - "src", - "generated/java", - "generated/jni" - ] - } - } -} - -repositories { - mavenCentral() - google() -} - -def kotlin_version = getExtOrDefault("kotlinVersion") - -dependencies { - implementation "com.facebook.react:react-native:+" - implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" - implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.2" - implementation "com.google.adk:google-adk-kotlin-core-android:0.2.0" - implementation "com.google.mlkit:genai-prompt:1.0.0-beta2" -} - -react { - jsRootDir = file("../src/") - libraryName = "ADK" - codegenJavaPackageName = "com.callstack.ai.adk" -} - -tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach { - kotlinOptions { - jvmTarget = "17" - } -} diff --git a/packages/adk/android/gradle.properties b/packages/adk/android/gradle.properties deleted file mode 100644 index cc84bcaa4..000000000 --- a/packages/adk/android/gradle.properties +++ /dev/null @@ -1,4 +0,0 @@ -ADK_kotlinVersion=2.1.20 -ADK_minSdkVersion=26 -ADK_targetSdkVersion=34 -ADK_compileSdkVersion=34 diff --git a/packages/adk/android/src/main/AndroidManifest.xml b/packages/adk/android/src/main/AndroidManifest.xml deleted file mode 100644 index d6f4979b3..000000000 --- a/packages/adk/android/src/main/AndroidManifest.xml +++ /dev/null @@ -1,3 +0,0 @@ - - diff --git a/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkAgentRunner.kt b/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkAgentRunner.kt deleted file mode 100644 index a82bb2bdf..000000000 --- a/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkAgentRunner.kt +++ /dev/null @@ -1,424 +0,0 @@ -package com.callstack.ai.adk - -import android.util.Base64 -import com.facebook.react.bridge.ReadableArray -import com.facebook.react.bridge.ReadableMap -import com.google.adk.kt.agents.Instruction -import com.google.adk.kt.agents.LlmAgent -import com.google.adk.kt.events.Event -import com.google.adk.kt.models.Gemini -import com.google.adk.kt.models.Model -import com.google.adk.kt.models.mlkit.GenaiPrompt -import com.google.adk.kt.runners.InMemoryRunner -import com.google.adk.kt.sessions.InMemorySessionService -import com.google.adk.kt.sessions.SessionKey -import com.google.adk.kt.tools.BaseTool -import com.google.adk.kt.types.Blob -import com.google.adk.kt.types.Content -import com.google.adk.kt.types.GenerateContentConfig -import com.google.adk.kt.types.Part -import com.google.adk.kt.types.Role -import com.google.adk.kt.types.UsageMetadata -import com.google.adk.kt.utils.mlkit.GenerativeModelHelpers -import com.google.mlkit.genai.prompt.Generation -import com.google.mlkit.genai.prompt.GenerativeModel -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.toList -import kotlinx.coroutines.sync.Mutex -import kotlinx.coroutines.sync.withLock -import java.util.UUID - -data class AdkRunResult( - val content: String, - val finishReason: String?, - val usage: UsageMetadata?, -) - -class AdkAgentRunner( - private val onToolCall: ( - toolCallId: String, - toolId: String, - arguments: String, - scope: ToolCallScope?, - ) -> Unit, -) { - private var generativeModel: GenerativeModel? = null - private val nanoPrepareMutex = Mutex() - - suspend fun prepareNano() { - if (!isNanoSupported()) { - throw IllegalStateException( - "Gemini Nano is not supported on this device or device hasn't fetched the latest configuration to support it", - ) - } - nanoPrepareMutex.withLock { - generativeModel = GenerativeModelHelpers.initGenerativeModel() - } - } - - private suspend fun ensureNanoPrepared() { - if (generativeModel != null) { - return - } - prepareNano() - } - - /** - * Whether this device supports on-device Gemini Nano at all (ML Kit checkStatus != 0). - * Does not download models or run full initialization. - */ - suspend fun isNanoSupported(): Boolean { - return try { - when (Generation.getClient().checkStatus()) { - 0 -> false - else -> true - } - } catch (_: Exception) { - false - } - } - - /** - * Whether Gemini Nano is ready to use now (downloaded or ready to download). - * Returns false when [isNanoSupported] is false. - */ - suspend fun isNanoAvailable(): Boolean { - if (!isNanoSupported()) { - return false - } - - return try { - when (Generation.getClient().checkStatus()) { - 1, 3 -> true - else -> false - } - } catch (_: Exception) { - false - } - } - - suspend fun generateText( - messages: ReadableArray, - config: ReadableMap, - options: ReadableMap?, - tools: ReadableArray?, - runId: String, - ): AdkRunResult { - val events = runAgent(messages, config, options, tools, stream = false, runId = runId).toList() - val text = - events - .lastOrNull { it.isFinalResponse && !it.partial } - ?.content - ?.parts - ?.mapNotNull { it.text } - ?.joinToString("") - ?.takeIf { it.isNotEmpty() } - ?: events - .flatMap { event -> event.content?.parts?.mapNotNull { it.text } ?: emptyList() } - .joinToString("") - - val finishReason = events.lastOrNull()?.finishReason?.name - val usage = AdkUsage.latestFromEvents(events) - return AdkRunResult(content = text, finishReason = finishReason, usage = usage) - } - - suspend fun streamText( - messages: ReadableArray, - config: ReadableMap, - options: ReadableMap?, - tools: ReadableArray?, - streamId: String, - ): Flow { - return runAgent(messages, config, options, tools, stream = true, streamId = streamId) - } - - private suspend fun runAgent( - messages: ReadableArray, - config: ReadableMap, - options: ReadableMap?, - tools: ReadableArray?, - stream: Boolean, - streamId: String? = null, - runId: String? = null, - ): Flow { - val parsedMessages = parseMessages(messages) - val agentConfig = parseAgentConfig(config) - val toolCallScope = when { - stream && streamId != null -> ToolCallScope(streamId = streamId) - !stream && runId != null -> ToolCallScope(runId = runId) - else -> null - } - val agentTools = parseTools(tools, toolCallScope, agentConfig.modelType) - val model = createModel(agentConfig) - val generateContentConfig = parseGenerationConfig(options) - val instructionText = - if (agentConfig.modelType == "genai-nano") { - buildInstruction(agentConfig.instruction, agentTools) - } else { - agentConfig.instruction - } - - val agent = LlmAgent( - name = agentConfig.name, - description = agentConfig.description, - model = model, - instruction = instructionText?.let { Instruction(it) }, - tools = agentTools, - generateContentConfig = generateContentConfig, - ) - - val sessionService = InMemorySessionService() - val runner = InMemoryRunner( - agent = agent, - appName = "react-native-ai-adk", - sessionService = sessionService, - ) - - val userId = "react-native-ai" - val sessionId = UUID.randomUUID().toString() - val sessionKey = SessionKey("react-native-ai-adk", userId, sessionId) - sessionService.createSession(sessionKey) - - val history = parsedMessages.dropLast(1) - val lastMessage = parsedMessages.last() - - val session = sessionService.getSession(sessionKey) - ?: throw IllegalStateException("Failed to create ADK session") - - for (message in history) { - val author = if (message.role == Role.USER) Role.USER else agent.name - val event = Event( - author = author, - content = message.content, - ) - sessionService.appendEvent(session, event) - } - - return runner.runAsync( - userId = userId, - sessionId = sessionId, - newMessage = lastMessage.content, - runConfig = com.google.adk.kt.agents.RunConfig(streamingMode = if (stream) { - com.google.adk.kt.agents.StreamingMode.SSE - } else { - com.google.adk.kt.agents.StreamingMode.NONE - }), - ) - } - - private suspend fun createModel(config: AgentConfig): Model { - return when (config.modelType) { - "genai-nano" -> { - ensureNanoPrepared() - val model = generativeModel - ?: throw IllegalStateException("Failed to initialize Gemini Nano") - NanoFunctionCallingModel(GenaiPrompt.create(model, config.modelName)) - } - else -> Gemini(name = config.modelName, apiKey = config.apiKey) - } - } - - private fun parseAgentConfig(config: ReadableMap): AgentConfig = - parseAgentConfigStatic(config) - - private fun parseGenerationConfig(options: ReadableMap?): GenerateContentConfig? { - if (options == null) return null - - val responseFormat = options.getMap("responseFormat") - if (responseFormat?.hasKey("schema") == true) { - throw IllegalArgumentException( - "ADK GenerateContentConfig does not support responseFormat.schema yet", - ) - } - - val responseMimeType = when { - responseFormat?.hasKey("mimeType") == true -> responseFormat.getString("mimeType") - responseFormat?.getString("type") == "json" -> "application/json" - else -> null - } - return GenerateContentConfig( - temperature = options.takeIf { it.hasKey("temperature") }?.getDouble("temperature")?.toFloat(), - maxOutputTokens = options.takeIf { it.hasKey("maxTokens") }?.getInt("maxTokens"), - topP = options.takeIf { it.hasKey("topP") }?.getDouble("topP")?.toFloat(), - topK = options.takeIf { it.hasKey("topK") }?.getDouble("topK")?.toFloat()?.toInt(), - responseMimeType = responseMimeType, - ) - } - - private fun parseMessages(messages: ReadableArray): List { - if (messages.size() == 0) { - throw IllegalArgumentException("At least one message is required") - } - - val parsed = mutableListOf() - for (index in 0 until messages.size()) { - val message = messages.getMap(index) ?: continue - val role = when (message.getString("role")) { - "assistant" -> Role.MODEL - "system" -> Role.SYSTEM - "user", "tool" -> Role.USER - else -> throw IllegalArgumentException( - "Unsupported message role: ${message.getString("role")}", - ) - } - - val partsArray = message.getArray("parts") - val content = if (partsArray != null) { - parseContentParts(role, partsArray) - } else { - Content.fromText(role, message.getString("content") ?: "") - } - - parsed.add(ParsedMessage(role = role, content = content)) - } - return parsed - } - - private fun parseContentParts(role: ContentRole, partsArray: ReadableArray): Content { - val parts = mutableListOf() - for (index in 0 until partsArray.size()) { - val part = partsArray.getMap(index) ?: continue - when (part.getString("type")) { - "text" -> part.getString("text")?.let { parts.add(Part(text = it)) } - "file" -> { - val data = part.getString("data") ?: continue - val mimeType = part.getString("mimeType") ?: "application/octet-stream" - val bytes = Base64.decode(data, Base64.DEFAULT) - parts.add(Part(inlineData = Blob(mimeType = mimeType, data = bytes))) - } - } - } - - if (parts.isEmpty()) { - return Content.fromText(role, "") - } - - return Content(role = role, parts = parts) - } - - private fun parseTools( - tools: ReadableArray?, - toolCallScope: ToolCallScope?, - modelType: String, - ): List { - if (tools == null) return emptyList() - - val injectPromptDescription = modelType == "genai-nano" - val parsed = mutableListOf() - for (index in 0 until tools.size()) { - val tool = tools.getMap(index) ?: continue - val parametersArray = tool.getArray("parameters") - val parameters = mutableListOf() - - if (parametersArray != null) { - for (paramIndex in 0 until parametersArray.size()) { - val parameter = parametersArray.getMap(paramIndex) ?: continue - parameters.add( - ToolParameterSpec( - name = parameter.getString("name") ?: continue, - description = parameter.getString("description"), - type = parseParameterType(parameter.getString("type")), - required = parameter.hasKey("required") && parameter.getBoolean("required"), - ) - ) - } - } - - val parameterSchema = - tool.getMap("inputSchema")?.let { schemaMap -> - runCatching { AdkSchema.fromReadableMap(schemaMap) }.getOrNull() - } - - parsed.add( - ReactNativeFunctionTool( - toolId = tool.getString("id") ?: UUID.randomUUID().toString(), - name = tool.getString("name") ?: continue, - description = tool.getString("description") ?: "", - parameterSchema = parameterSchema, - parameters = parameters, - injectPromptDescription = injectPromptDescription, - toolCallScope = toolCallScope, - onToolCall = onToolCall, - ) - ) - } - - return parsed - } - - private fun buildInstruction( - baseInstruction: String?, - tools: List, - ): String? { - if (tools.isEmpty()) { - return baseInstruction - } - - val toolSummary = - tools.joinToString("\n") { tool -> - "- ${tool.name}: ${tool.description}" - } - - val toolGuidance = - """ - You have access to the following tools. Use them when they help answer the user: - $toolSummary - """.trimIndent() - - return if (baseInstruction.isNullOrBlank()) { - toolGuidance - } else { - "$baseInstruction\n\n$toolGuidance" - } - } - - private fun parseParameterType(type: String?): com.google.adk.kt.types.Type { - return when (type) { - "number", "integer" -> com.google.adk.kt.types.Type.NUMBER - "boolean" -> com.google.adk.kt.types.Type.BOOLEAN - "array" -> com.google.adk.kt.types.Type.ARRAY - "object" -> com.google.adk.kt.types.Type.OBJECT - else -> com.google.adk.kt.types.Type.STRING - } - } - - companion object { - fun parseAgentConfig(config: ReadableMap): AgentConfig = parseAgentConfigStatic(config) - - private fun parseAgentConfigStatic(config: ReadableMap): AgentConfig { - val model = config.getMap("model") - ?: throw IllegalArgumentException("Agent config requires a model") - - return AgentConfig( - name = config.getString("name") ?: "react_native_adk_agent", - description = config.getString("description") ?: "", - instruction = config.getString("instruction"), - modelType = model.getString("type") ?: "genai-nano", - modelName = model.getString("name") ?: "gemini-nano", - apiKey = model.getString("apiKey"), - ) - } - } -} - -data class AgentConfig( - val name: String, - val description: String, - val instruction: String?, - val modelType: String, - val modelName: String, - val apiKey: String?, -) - -/** ADK content role — one of [Role.USER], [Role.MODEL], or [Role.SYSTEM]. */ -private typealias ContentRole = String - -data class ParsedMessage( - val role: ContentRole, - val content: Content, -) - -data class ToolCallScope( - val streamId: String? = null, - val runId: String? = null, -) diff --git a/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkJson.kt b/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkJson.kt deleted file mode 100644 index da43e387e..000000000 --- a/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkJson.kt +++ /dev/null @@ -1,72 +0,0 @@ -package com.callstack.ai.adk - -import org.json.JSONArray -import org.json.JSONObject - -object AdkJson { - fun encode(value: Map): String { - return mapToJsonObject(value).toString() - } - - fun decodeResult(value: String): Any? { - val trimmed = value.trim() - return when { - trimmed.startsWith("{") -> jsonObjectToMap(JSONObject(trimmed)) - trimmed.startsWith("[") -> jsonArrayToList(JSONArray(trimmed)) - trimmed == "true" -> true - trimmed == "false" -> false - trimmed == "null" -> null - trimmed.toDoubleOrNull() != null -> trimmed.toDouble() - else -> trimmed.removeSurrounding("\"") - } - } - - private fun mapToJsonObject(value: Map): JSONObject { - val json = JSONObject() - value.forEach { (key, entryValue) -> - json.put(key, toJsonValue(entryValue)) - } - return json - } - - private fun toJsonValue(value: Any?): Any? { - return when (value) { - null -> JSONObject.NULL - is Map<*, *> -> { - @Suppress("UNCHECKED_CAST") - mapToJsonObject(value as Map) - } - is Iterable<*> -> { - val array = JSONArray() - value.forEach { item -> array.put(toJsonValue(item)) } - array - } - else -> value - } - } - - private fun jsonObjectToMap(json: JSONObject): Map { - val result = mutableMapOf() - json.keys().forEach { key -> - result[key] = jsonValue(json.get(key)) - } - return result - } - - private fun jsonArrayToList(json: JSONArray): List { - val result = mutableListOf() - for (index in 0 until json.length()) { - result.add(jsonValue(json.get(index))) - } - return result - } - - private fun jsonValue(value: Any?): Any? { - return when (value) { - JSONObject.NULL, null -> null - is JSONObject -> jsonObjectToMap(value) - is JSONArray -> jsonArrayToList(value) - else -> value - } - } -} diff --git a/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkSchema.kt b/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkSchema.kt deleted file mode 100644 index 00c935126..000000000 --- a/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkSchema.kt +++ /dev/null @@ -1,56 +0,0 @@ -package com.callstack.ai.adk - -import com.facebook.react.bridge.ReadableArray -import com.facebook.react.bridge.ReadableMap -import com.facebook.react.bridge.ReadableType -import com.google.adk.kt.types.Schema -import com.google.adk.kt.types.Type - -object AdkSchema { - fun fromReadableMap(map: ReadableMap): Schema { - val properties = map.getMap("properties")?.let { props -> - val result = mutableMapOf() - val iterator = props.keySetIterator() - while (iterator.hasNextKey()) { - val key = iterator.nextKey() - props.getMap(key)?.let { result[key] = fromReadableMap(it) } - } - result - } - - val required = map.getArray("required")?.let { array -> - (0 until array.size()).mapNotNull { index -> array.getString(index) } - } - - val enumValues = map.getArray("enum")?.let { array -> - (0 until array.size()).mapNotNull { index -> - when (array.getType(index)) { - ReadableType.String -> array.getString(index) - ReadableType.Number -> array.getDouble(index).toString() - ReadableType.Boolean -> array.getBoolean(index).toString() - else -> null - } - } - } - - return Schema( - type = parseType(map.getString("type")), - properties = properties, - items = map.getMap("items")?.let { fromReadableMap(it) }, - required = required, - description = map.getString("description"), - enum = enumValues, - ) - } - - private fun parseType(type: String?): Type? { - return when (type) { - "string" -> Type.STRING - "number", "integer" -> Type.NUMBER - "boolean" -> Type.BOOLEAN - "array" -> Type.ARRAY - "object" -> Type.OBJECT - else -> null - } - } -} diff --git a/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkStreamToolCalls.kt b/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkStreamToolCalls.kt deleted file mode 100644 index 928907bc4..000000000 --- a/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkStreamToolCalls.kt +++ /dev/null @@ -1,97 +0,0 @@ -package com.callstack.ai.adk - -import com.facebook.react.bridge.Arguments -import com.facebook.react.bridge.WritableMap -import com.google.adk.kt.events.Event - -class AdkStreamToolCallEmitter( - private val streamId: String, - private val emit: (WritableMap) -> Unit, -) { - private class ActiveCall( - var toolName: String, - var started: Boolean = false, - var completed: Boolean = false, - val inputBuilder: StringBuilder = StringBuilder(), - ) - - private val activeCalls = mutableMapOf() - - fun handleEvent(event: Event) { - for (functionCall in event.functionCalls()) { - val toolCallId = functionCall.id ?: "${functionCall.name}-${activeCalls.size}" - val state = activeCalls.getOrPut(toolCallId) { - ActiveCall(toolName = functionCall.name) - } - - if (functionCall.name.isNotEmpty()) { - state.toolName = functionCall.name - } - - if (!state.started && state.toolName.isNotEmpty()) { - emit( - Arguments.createMap().apply { - putString("streamId", streamId) - putString("phase", "start") - putString("toolCallId", toolCallId) - putString("toolName", state.toolName) - }, - ) - state.started = true - } - - if (!functionCall.partialArgs.isNullOrEmpty()) { - for (partialArg in functionCall.partialArgs) { - val delta = - partialArg.stringValue - ?: partialArg.numberValue?.toString() - ?: partialArg.boolValue?.toString() - ?: continue - - emit( - Arguments.createMap().apply { - putString("streamId", streamId) - putString("phase", "delta") - putString("toolCallId", toolCallId) - putString("inputDelta", delta) - }, - ) - state.inputBuilder.append(delta) - } - } - - if (functionCall.args.isNotEmpty() && !state.completed) { - val input = AdkJson.encode(functionCall.args) - if (state.inputBuilder.isEmpty()) { - emit( - Arguments.createMap().apply { - putString("streamId", streamId) - putString("phase", "delta") - putString("toolCallId", toolCallId) - putString("inputDelta", input) - }, - ) - } - - emitEnd(toolCallId, state.toolName, input) - state.completed = true - } else if (functionCall.willContinue == false && state.started && !state.completed) { - val input = state.inputBuilder.toString().ifEmpty { "{}" } - emitEnd(toolCallId, state.toolName, input) - state.completed = true - } - } - } - - private fun emitEnd(toolCallId: String, toolName: String, input: String) { - emit( - Arguments.createMap().apply { - putString("streamId", streamId) - putString("phase", "end") - putString("toolCallId", toolCallId) - putString("toolName", toolName) - putString("input", input) - }, - ) - } -} diff --git a/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkToolBridge.kt b/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkToolBridge.kt deleted file mode 100644 index 7b99145c4..000000000 --- a/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkToolBridge.kt +++ /dev/null @@ -1,45 +0,0 @@ -package com.callstack.ai.adk - -import kotlinx.coroutines.CompletableDeferred -import java.util.concurrent.ConcurrentHashMap - -object AdkToolBridge { - private val pendingResults = ConcurrentHashMap>() - private val streamToolCalls = ConcurrentHashMap>() - - suspend fun awaitResult(toolCallId: String): String { - val deferred = pendingResults.computeIfAbsent(toolCallId) { CompletableDeferred() } - val result = deferred.await() - pendingResults.remove(toolCallId, deferred) - return result - } - - fun registerToolCall(streamId: String, toolCallId: String) { - streamToolCalls.computeIfAbsent(streamId) { ConcurrentHashMap.newKeySet() }.add(toolCallId) - } - - fun submitResult(toolCallId: String, result: String) { - pendingResults.computeIfAbsent(toolCallId) { CompletableDeferred() }.complete(result) - streamToolCalls.values.forEach { toolCallIds -> toolCallIds.remove(toolCallId) } - } - - fun cancelForStream(streamId: String) { - streamToolCalls.remove(streamId)?.forEach { toolCallId -> - pendingResults.remove(toolCallId)?.let { deferred -> - if (!deferred.isCompleted) { - deferred.complete("{\"error\":\"Tool call cancelled\"}") - } - } - } - } - - fun cancelAll() { - pendingResults.values.forEach { deferred -> - if (!deferred.isCompleted) { - deferred.complete("{\"error\":\"Tool call cancelled\"}") - } - } - pendingResults.clear() - streamToolCalls.clear() - } -} diff --git a/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkUsage.kt b/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkUsage.kt deleted file mode 100644 index 76b926a98..000000000 --- a/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkUsage.kt +++ /dev/null @@ -1,21 +0,0 @@ -package com.callstack.ai.adk - -import com.facebook.react.bridge.Arguments -import com.facebook.react.bridge.WritableMap -import com.google.adk.kt.events.Event -import com.google.adk.kt.types.UsageMetadata - -object AdkUsage { - fun toWritableMap(usage: UsageMetadata?): WritableMap? { - if (usage == null) return null - - return Arguments.createMap().apply { - usage.promptTokenCount?.let { putInt("promptTokenCount", it) } - usage.candidatesTokenCount?.let { putInt("candidatesTokenCount", it) } - usage.totalTokenCount?.let { putInt("totalTokenCount", it) } - } - } - - fun latestFromEvents(events: List): UsageMetadata? = - events.mapNotNull { it.usageMetadata }.lastOrNull() -} diff --git a/packages/adk/android/src/main/java/com/callstack/ai/adk/NanoFunctionCallingModel.kt b/packages/adk/android/src/main/java/com/callstack/ai/adk/NanoFunctionCallingModel.kt deleted file mode 100644 index 97e838b2b..000000000 --- a/packages/adk/android/src/main/java/com/callstack/ai/adk/NanoFunctionCallingModel.kt +++ /dev/null @@ -1,163 +0,0 @@ -package com.callstack.ai.adk - -import com.google.adk.kt.models.LlmRequest -import com.google.adk.kt.models.LlmResponse -import com.google.adk.kt.models.Model -import com.google.adk.kt.models.mlkit.GenaiPrompt -import com.google.adk.kt.types.Content -import com.google.adk.kt.types.FunctionCall -import com.google.adk.kt.types.Part -import com.google.adk.kt.types.Role -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.map -import org.json.JSONException -import org.json.JSONObject -import java.util.UUID - -/** - * Wraps [GenaiPrompt] (Gemini Nano via ML Kit) to bridge text-based tool calling with ADK. - * - * Gemini Nano responds with ```tool_code ...``` blocks with tool calls. This wrapper: - * - * 1. Before each request: converts any [FunctionCall]/[FunctionResponse] parts (produced by - * previous turns) to plain text so that ML Kit can understand the multi-turn context. - * - * 2. After the final (non-partial) response: detects ```tool_code blocks in the text and converts - * each invocation to a proper [FunctionCall] part, allowing ADK's agent loop to execute the - * tools and continue the conversation. - */ -internal class NanoFunctionCallingModel(private val delegate: GenaiPrompt) : Model { - - override val name: String get() = delegate.name - - override fun generateContent(request: LlmRequest, stream: Boolean): Flow = - delegate.generateContent(prepareRequest(request), stream).map { response -> - if (response.partial) response else parseToolCodeBlocks(response) - } - - /** - * Request preparation: convert FunctionCall/FunctionResponse parts to text - */ - private fun prepareRequest(request: LlmRequest): LlmRequest { - val newContents = - request.contents.map { content -> - val newParts = - content.parts.flatMap { part -> - when { - part.functionCall != null -> { - val fc = part.functionCall!! - val argsText = - if (fc.args.isEmpty()) "" else runCatching { AdkJson.encode(fc.args) }.getOrElse { fc.args.toString() } - listOf(Part(text = "[Tool call: ${fc.name}($argsText)]")) - } - part.functionResponse != null -> { - val fr = part.functionResponse!! - val responseMap = - fr.response - .filterValues { it != null } - .mapValues { (_, v) -> v as Any } - val resultText = - if (responseMap.isEmpty()) "{}" - else runCatching { AdkJson.encode(responseMap) }.getOrElse { responseMap.toString() } - listOf(Part(text = "[Tool result for ${fr.name}: $resultText]")) - } - else -> listOf(part) - } - } - content.copy(parts = newParts) - } - return request.copy(contents = newContents) - } - - /** - * Response post-processing: convert ```tool_code blocks to FunctionCall parts - */ - private fun parseToolCodeBlocks(response: LlmResponse): LlmResponse { - val content = response.content ?: return response - val newParts = mutableListOf() - var hasToolCalls = false - - for (part in content.parts) { - val text = part.text - if (text == null) { - newParts.add(part) - continue - } - - val calls = extractToolCalls(text) - if (calls.isEmpty()) { - newParts.add(part) - continue - } - - hasToolCalls = true - val surrounding = removeToolCodeBlocks(text).trim() - if (surrounding.isNotEmpty()) { - newParts.add(Part(text = surrounding)) - } - for (call in calls) { - newParts.add(Part(functionCall = call)) - } - } - - if (!hasToolCalls) return response - return response.copy(content = Content(role = Role.MODEL, parts = newParts)) - } - - private fun extractToolCalls(text: String): List { - val calls = mutableListOf() - val blockRegex = Regex("```tool_code\\s*\\n([\\s\\S]*?)\\n?```") - for (block in blockRegex.findAll(text)) { - val code = block.groupValues[1] - for (line in code.lines()) { - val trimmed = line.trim() - if (trimmed.isNotEmpty()) { - parseFunctionCall(trimmed)?.let { calls.add(it) } - } - } - } - return calls - } - - private fun removeToolCodeBlocks(text: String): String = - Regex("```tool_code\\s*\\n[\\s\\S]*?\\n?```").replace(text, "") - - /** - * Parses a single call line such as: - * - `functionName()` - * - `functionName({"key": "value"})` - * - `print(functionName(...))` (some models wrap in print) - */ - private fun parseFunctionCall(code: String): FunctionCall? { - // Regex: outer function name + everything between the outermost parens (greedy) - val callRegex = Regex("""^([A-Za-z_][A-Za-z0-9_]*)\s*\(([\s\S]*)\)\s*$""") - val outer = callRegex.find(code) ?: return null - val outerName = outer.groupValues[1] - val outerArgs = outer.groupValues[2] - - val (name, argsStr) = - if (outerName == "print") { - val inner = callRegex.find(outerArgs.trim()) ?: return null - inner.groupValues[1] to inner.groupValues[2] - } else { - outerName to outerArgs - } - - if (name.isBlank()) return null - - val args: Map = - if (argsStr.isBlank()) { - emptyMap() - } else { - val jsonStr = argsStr.trim().let { if (it.startsWith("{")) it else "{$it}" } - try { - val jsonObj = JSONObject(jsonStr) - jsonObj.keys().asSequence().associateWith { key -> jsonObj.get(key) as Any } - } catch (_: JSONException) { - emptyMap() - } - } - - return FunctionCall(name = name, args = args, id = UUID.randomUUID().toString()) - } -} diff --git a/packages/adk/android/src/main/java/com/callstack/ai/adk/NativeAdkEngineModule.kt b/packages/adk/android/src/main/java/com/callstack/ai/adk/NativeAdkEngineModule.kt deleted file mode 100644 index 372a2349c..000000000 --- a/packages/adk/android/src/main/java/com/callstack/ai/adk/NativeAdkEngineModule.kt +++ /dev/null @@ -1,256 +0,0 @@ -package com.callstack.ai.adk - -import com.facebook.react.bridge.Arguments -import com.facebook.react.bridge.Promise -import com.facebook.react.bridge.ReactApplicationContext -import com.facebook.react.bridge.ReadableArray -import com.facebook.react.bridge.ReadableMap -import com.google.adk.kt.types.UsageMetadata -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.Job -import kotlinx.coroutines.SupervisorJob -import kotlinx.coroutines.cancel -import kotlinx.coroutines.flow.collect -import kotlinx.coroutines.isActive -import kotlinx.coroutines.launch -import java.util.concurrent.ConcurrentHashMap - -class NativeAdkEngineModule(reactContext: ReactApplicationContext) : - NativeAdkEngineSpec(reactContext) { - - override fun getName(): String = NAME - - private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO) - private val runner = AdkAgentRunner { toolCallId, toolId, arguments, toolCallScope -> - toolCallScope?.streamId?.let { AdkToolBridge.registerToolCall(it, toolCallId) } - toolCallScope?.runId?.let { AdkToolBridge.registerToolCall(it, toolCallId) } - val args = Arguments.createMap().apply { - putString("toolCallId", toolCallId) - putString("toolId", toolId) - putString("arguments", arguments) - toolCallScope?.streamId?.let { putString("streamId", it) } - toolCallScope?.runId?.let { putString("runId", it) } - } - emitOnToolCall(args) - } - private val activeStreams = ConcurrentHashMap() - - override fun isAvailable(modelType: String, promise: Promise) { - scope.launch { - try { - val available = when (modelType) { - "genai-nano" -> runner.isNanoAvailable() - else -> true - } - promise.resolve(available) - } catch (error: Exception) { - promise.resolve(false) - } - } - } - - override fun isNanoSupported(promise: Promise) { - scope.launch { - try { - promise.resolve(runner.isNanoSupported()) - } catch (_: Exception) { - promise.resolve(false) - } - } - } - - override fun prepareNano(promise: Promise) { - scope.launch { - try { - runner.prepareNano() - promise.resolve(null) - } catch (error: Exception) { - promise.reject("ADK_PREPARE_ERROR", error.message, error) - } - } - } - - override fun generateText( - runId: String, - messages: ReadableArray, - config: ReadableMap, - options: ReadableMap?, - tools: ReadableArray?, - promise: Promise, - ) { - scope.launch { - try { - val result = runner.generateText(messages, config, options, tools, runId) - val response = Arguments.createMap().apply { - putString("role", "assistant") - putString("content", result.content) - result.finishReason?.let { putString("finishReason", it) } - AdkUsage.toWritableMap(result.usage)?.let { putMap("usage", it) } - } - promise.resolve(response) - } catch (error: Exception) { - promise.reject("ADK_GENERATION_ERROR", error.message, error) - } finally { - AdkToolBridge.cancelForStream(runId) - } - } - } - - override fun streamText( - streamId: String, - messages: ReadableArray, - config: ReadableMap, - options: ReadableMap?, - tools: ReadableArray?, - promise: Promise, - ) { - if (activeStreams.containsKey(streamId)) { - promise.reject("ADK_STREAM_ERROR", "Stream ID already in use: $streamId") - return - } - - val job = scope.launch { - try { - var latestUsage: UsageMetadata? = null - // Per-turn state. Gemini Nano emits ```tool_code blocks (as streamed text) instead - // of native function calls, and the marker can be split across stream chunks. - // - // ADK/GenaiPrompt may deliver text either cumulatively (each event repeats the full - // text) or incrementally (each event carries only the new delta). We can't rely on - // either, so we reconstruct the full turn text ourselves by appending the delta of - // each event, then run marker detection and hold-back on that reconstruction. - // - // currentTurnText: full reconstructed text of the current agent turn - // emittedText: the prefix of currentTurnText already sent to JS - // suppressCurrentTurn: true once we confirm this turn is a tool-code turn - var currentTurnText = "" - var emittedText = "" - var suppressCurrentTurn = false - val toolCallEmitter = AdkStreamToolCallEmitter(streamId) { args -> - emitOnStreamToolCall(args) - } - - runner.streamText(messages, config, options, tools, streamId).collect { event -> - if (!isActive) return@collect - - event.usageMetadata?.let { latestUsage = it } - toolCallEmitter.handleEvent(event) - - val text = event.content?.parts?.mapNotNull { it.text }?.joinToString("") ?: "" - - if (text.isNotEmpty()) { - // Reconstruct the full turn text. If the event repeats what we already have - // (cumulative mode) take it as-is; otherwise treat it as an incremental delta - // and append it. This keeps a split marker (e.g. "```tool_" + "code") intact. - currentTurnText = - if (text.startsWith(currentTurnText)) text else currentTurnText + text - - // Once the full tool-code marker appears, suppress the rest of this turn. - if (currentTurnText.contains(TOOL_CODE_MARKER)) { - suppressCurrentTurn = true - } - } - - // When a tool-calling turn ends, reset per-turn state so the next turn - // (the actual answer) starts with a clean slate and streams normally. - if (!event.partial && event.functionCalls().isNotEmpty()) { - currentTurnText = "" - emittedText = "" - suppressCurrentTurn = false - } - - if (!suppressCurrentTurn && !(event.isFinalResponse && !event.partial)) { - // Hold back any trailing substring that could be the beginning of the marker, - // so we never emit a partial "```", "```tool", etc. that later completes. - val safeEnd = currentTurnText.length - pendingMarkerPrefixLength(currentTurnText) - if (safeEnd > emittedText.length) { - val delta = currentTurnText.substring(emittedText.length, safeEnd) - emittedText = currentTurnText.substring(0, safeEnd) - - val updateArgs = Arguments.createMap().apply { - putString("streamId", streamId) - putString("delta", delta) - } - emitOnStreamUpdate(updateArgs) - } - } - - if (event.isFinalResponse && !event.partial) { - // Flush any held-back text for a normal turn (e.g. a response legitimately - // ending in backticks that turned out not to be a tool-code marker). - if (!suppressCurrentTurn && currentTurnText.length > emittedText.length) { - val delta = currentTurnText.substring(emittedText.length) - emittedText = currentTurnText - val updateArgs = Arguments.createMap().apply { - putString("streamId", streamId) - putString("delta", delta) - } - emitOnStreamUpdate(updateArgs) - } - - val completeArgs = Arguments.createMap().apply { - putString("streamId", streamId) - putString("finishReason", event.finishReason?.name) - AdkUsage.toWritableMap(latestUsage ?: event.usageMetadata)?.let { - putMap("usage", it) - } - } - emitOnStreamComplete(completeArgs) - } - } - } catch (error: Exception) { - val errorArgs = Arguments.createMap().apply { - putString("streamId", streamId) - putString("error", error.message ?: "Unknown ADK stream error") - } - emitOnStreamError(errorArgs) - } finally { - activeStreams.remove(streamId) - } - } - - activeStreams[streamId] = job - promise.resolve(null) - } - - override fun cancelStream(streamId: String, promise: Promise) { - activeStreams.remove(streamId)?.cancel() - AdkToolBridge.cancelForStream(streamId) - promise.resolve(null) - } - - override fun submitToolResult(toolCallId: String, result: String) { - AdkToolBridge.submitResult(toolCallId, result) - } - - override fun invalidate() { - activeStreams.values.forEach { it.cancel() } - activeStreams.clear() - AdkToolBridge.cancelAll() - scope.cancel() - super.invalidate() - } - - /** - * Returns the length of the trailing suffix of [text] that is a proper prefix of - * [TOOL_CODE_MARKER] (e.g. "`", "``", "```", "```tool"). This portion is held back - * from streaming because a subsequent token could complete the marker, at which point - * the whole turn is suppressed. If no suffix could begin the marker, returns 0. - */ - private fun pendingMarkerPrefixLength(text: String): Int { - val maxLen = minOf(text.length, TOOL_CODE_MARKER.length - 1) - for (len in maxLen downTo 1) { - val suffix = text.substring(text.length - len) - if (TOOL_CODE_MARKER.startsWith(suffix)) { - return len - } - } - return 0 - } - - companion object { - const val NAME = "AdkEngine" - private const val TOOL_CODE_MARKER = "```tool_code" - } -} diff --git a/packages/adk/android/src/main/java/com/callstack/ai/adk/NativeAdkEnginePackage.kt b/packages/adk/android/src/main/java/com/callstack/ai/adk/NativeAdkEnginePackage.kt deleted file mode 100644 index e82cf6b0d..000000000 --- a/packages/adk/android/src/main/java/com/callstack/ai/adk/NativeAdkEnginePackage.kt +++ /dev/null @@ -1,33 +0,0 @@ -package com.callstack.ai.adk - -import com.facebook.react.BaseReactPackage -import com.facebook.react.bridge.NativeModule -import com.facebook.react.bridge.ReactApplicationContext -import com.facebook.react.module.model.ReactModuleInfo -import com.facebook.react.module.model.ReactModuleInfoProvider -import java.util.HashMap - -class NativeAdkEnginePackage : BaseReactPackage() { - override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? = - if (name == NativeAdkEngineModule.NAME) { - NativeAdkEngineModule(reactContext) - } else { - null - } - - override fun getReactModuleInfoProvider(): ReactModuleInfoProvider = - ReactModuleInfoProvider { - val moduleInfos: MutableMap = HashMap() - moduleInfos[NativeAdkEngineModule.NAME] = ReactModuleInfo( - NativeAdkEngineModule.NAME, - NativeAdkEngineModule.NAME, - canOverrideExistingModule = false, - needsEagerInit = false, - hasConstants = false, - isCxxModule = false, - isTurboModule = true - ) - - moduleInfos - } -} diff --git a/packages/adk/android/src/main/java/com/callstack/ai/adk/ReactNativeFunctionTool.kt b/packages/adk/android/src/main/java/com/callstack/ai/adk/ReactNativeFunctionTool.kt deleted file mode 100644 index e9ab8367c..000000000 --- a/packages/adk/android/src/main/java/com/callstack/ai/adk/ReactNativeFunctionTool.kt +++ /dev/null @@ -1,103 +0,0 @@ -package com.callstack.ai.adk - -import com.google.adk.kt.models.LlmRequest -import com.google.adk.kt.tools.FunctionTool -import com.google.adk.kt.tools.ToolContext -import com.google.adk.kt.types.Content -import com.google.adk.kt.types.FunctionDeclaration -import com.google.adk.kt.types.Part -import com.google.adk.kt.types.Schema -import com.google.adk.kt.types.Type -import java.util.UUID - -class ReactNativeFunctionTool( - private val toolId: String, - name: String, - description: String, - private val parameterSchema: Schema?, - private val parameters: List, - private val injectPromptDescription: Boolean, - private val toolCallScope: ToolCallScope?, - private val onToolCall: ( - toolCallId: String, - toolId: String, - arguments: String, - scope: ToolCallScope?, - ) -> Unit, -) : FunctionTool(name = name, description = description) { - - override fun declaration(): FunctionDeclaration { - val schema = - parameterSchema - ?: Schema( - type = Type.OBJECT, - properties = - parameters.associate { parameter -> - parameter.name to - Schema( - type = parameter.type, - description = parameter.description, - ) - }, - required = parameters.filter { it.required }.map { it.name }.ifEmpty { null }, - ) - - return FunctionDeclaration( - name = name, - description = description, - parameters = schema, - ) - } - - override suspend fun processLlmRequest( - toolContext: ToolContext, - llmRequest: LlmRequest, - ): LlmRequest { - val requestWithTool = super.processLlmRequest(toolContext, llmRequest) - - if (!injectPromptDescription) { - return requestWithTool - } - - val declaration = declaration() - val parameterLines = - declaration.parameters?.properties?.entries?.joinToString("\n") { (paramName, paramSchema) -> - val paramType = paramSchema.type?.name?.lowercase() ?: "string" - val paramDescription = paramSchema.description?.let { " — $it" } ?: "" - val required = - declaration.parameters?.required?.contains(paramName) == true - " - $paramName ($paramType, required=$required)$paramDescription" - } ?: "" - - val toolDescription = - buildString { - append("Tool: ${declaration.name}") - if (declaration.description.isNotBlank()) { - append(" — ${declaration.description}") - } - if (parameterLines.isNotBlank()) { - append("\n Parameters:\n$parameterLines") - } - } - - return requestWithTool.appendInstructions( - Content(parts = listOf(Part(text = toolDescription))), - ) - } - - override suspend fun execute(context: ToolContext, args: Map): Any { - val toolCallId = UUID.randomUUID().toString() - val argumentsJson = AdkJson.encode(args) - onToolCall(toolCallId, toolId, argumentsJson, toolCallScope) - val result = AdkToolBridge.awaitResult(toolCallId) - return AdkJson.decodeResult(result) - ?: throw IllegalArgumentException("Failed to decode result in ReactNativeFunctionTool::execute") - } -} - -data class ToolParameterSpec( - val name: String, - val description: String?, - val type: Type, - val required: Boolean, -) diff --git a/packages/adk/babel.config.js b/packages/adk/babel.config.js deleted file mode 100644 index 3e0218e68..000000000 --- a/packages/adk/babel.config.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - presets: ['module:@react-native/babel-preset'], -} diff --git a/packages/adk/package.json b/packages/adk/package.json deleted file mode 100644 index cd3526a6e..000000000 --- a/packages/adk/package.json +++ /dev/null @@ -1,84 +0,0 @@ -{ - "name": "@react-native-ai/adk", - "version": "0.12.1", - "description": "Google ADK agent wrapper compatible with Vercel AI SDK, for Android", - "main": "lib/commonjs/index", - "module": "lib/module/index", - "types": "lib/typescript/index.d.ts", - "react-native": "src/index", - "source": "src/index", - "files": [ - "src", - "lib", - "android", - "!android/build", - "!android/gradle", - "!android/gradlew", - "!android/gradlew.bat", - "!android/local.properties", - "!**/__tests__", - "!**/__fixtures__", - "!**/__mocks__", - "!**/.*" - ], - "scripts": { - "clean": "del-cli lib", - "typecheck": "tsc --noEmit", - "prepare": "bob build" - }, - "keywords": [ - "react-native", - "adk", - "agent", - "gemini", - "ai", - "sdk", - "vercel", - "android" - ], - "repository": { - "type": "git", - "url": "git+https://github.com/callstackincubator/ai.git" - }, - "author": "Artur Morys-Magiera (https://github.com/artus9033)", - "license": "MIT", - "bugs": { - "url": "https://github.com/callstackincubator/ai/issues" - }, - "homepage": "https://github.com/callstackincubator/ai#readme", - "publishConfig": { - "registry": "https://registry.npmjs.org/" - }, - "dependencies": { - "@ai-sdk/provider": "^4.0.33", - "@ai-sdk/provider-utils": "^5.0.67" - }, - "peerDependencies": { - "react-native": ">=0.76.0" - }, - "devDependencies": { - "react-native": "0.88.0" - }, - "react-native-builder-bob": { - "source": "src", - "output": "lib", - "targets": [ - "commonjs", - "module", - [ - "typescript", - { - "project": "tsconfig.build.json" - } - ] - ] - }, - "codegenConfig": { - "name": "NativeAdkEngine", - "type": "modules", - "jsSrcsDir": "src", - "android": { - "javaPackageName": "com.callstack.ai.adk" - } - } -} diff --git a/packages/adk/src/NativeAdkEngine.ts b/packages/adk/src/NativeAdkEngine.ts deleted file mode 100644 index 4862243cc..000000000 --- a/packages/adk/src/NativeAdkEngine.ts +++ /dev/null @@ -1,156 +0,0 @@ -import type { CodegenTypes, TurboModule } from 'react-native' -import { Platform, TurboModuleRegistry } from 'react-native' - -export type AdkMessageRole = 'assistant' | 'system' | 'user' - -export type AdkMessagePart = - | { type: 'text'; text: string } - | { type: 'file'; mimeType: string; data: string } - -export interface AdkMessage { - role: AdkMessageRole - content?: string - parts?: AdkMessagePart[] -} - -export type AdkModelType = 'gemini' | 'genai-nano' - -export interface AdkAgentConfig { - name: string - description?: string - instruction?: string - model: { - type: AdkModelType - name: string - apiKey?: string - } -} - -export interface AdkToolParameter { - name: string - description?: string - type?: 'string' | 'number' | 'boolean' | 'object' | 'array' - required?: boolean -} - -export interface AdkTool { - id: string - name: string - description: string - inputSchema?: CodegenTypes.UnsafeObject - parameters?: AdkToolParameter[] -} - -export interface AdkResponseFormat { - type: 'json' - mimeType?: string - schema?: CodegenTypes.UnsafeObject -} - -export interface AdkGenerationOptions { - temperature?: number - maxTokens?: number - topP?: number - topK?: number - responseFormat?: AdkResponseFormat -} - -export interface AdkUsageMetadata { - promptTokenCount?: number - candidatesTokenCount?: number - totalTokenCount?: number -} - -export interface AdkGeneratedMessage { - role: AdkMessageRole - content: string - finishReason?: string - usage?: AdkUsageMetadata -} - -export interface StreamUpdateEvent { - streamId: string - delta: string -} - -export interface StreamCompleteEvent { - streamId: string - finishReason?: string - usage?: AdkUsageMetadata -} - -export interface StreamErrorEvent { - streamId: string - error: string -} - -export interface StreamToolCallEvent { - streamId: string - phase: 'start' | 'delta' | 'end' - toolCallId: string - toolName?: string - inputDelta?: string - input?: string -} - -export interface ToolCallEvent { - toolCallId: string - toolId: string - arguments: string - streamId?: string - runId?: string -} - -export interface Spec extends TurboModule { - isAvailable(modelType: AdkModelType): Promise - isNanoSupported(): Promise - prepareNano(): Promise - - generateText( - runId: string, - messages: AdkMessage[], - config: AdkAgentConfig, - options?: AdkGenerationOptions, - tools?: AdkTool[] - ): Promise - - streamText( - streamId: string, - messages: AdkMessage[], - config: AdkAgentConfig, - options?: AdkGenerationOptions, - tools?: AdkTool[] - ): Promise - - cancelStream(streamId: string): Promise - submitToolResult(toolCallId: string, result: string): void - - onStreamUpdate: CodegenTypes.EventEmitter - onStreamComplete: CodegenTypes.EventEmitter - onStreamError: CodegenTypes.EventEmitter - onStreamToolCall: CodegenTypes.EventEmitter - onToolCall: CodegenTypes.EventEmitter -} - -declare global { - var __ADK_TOOLS__: Record< - string, - (args: string) => unknown | Promise - > -} - -const NativeAdkEngine: Spec | null = - Platform.OS === 'android' - ? TurboModuleRegistry.getEnforcing('AdkEngine') - : null - -export default NativeAdkEngine - -export function getNativeAdkEngine(): Spec { - if (!NativeAdkEngine) { - throw new Error( - '@react-native-ai/adk is only available on Android. See https://developer.android.com/ai/adk' - ) - } - return NativeAdkEngine -} diff --git a/packages/adk/src/__tests__/ai-sdk.test.ts b/packages/adk/src/__tests__/ai-sdk.test.ts deleted file mode 100644 index 17490adc3..000000000 --- a/packages/adk/src/__tests__/ai-sdk.test.ts +++ /dev/null @@ -1,154 +0,0 @@ -import type { - LanguageModelV4CallOptions, - LanguageModelV4StreamPart, -} from '@ai-sdk/provider' -import { beforeEach, describe, expect, mock, test } from 'bun:test' - -const generateText = mock( - async ( - _runId: string, - _messages: unknown[], - _config: unknown, - _options: unknown, - _tools: unknown - ) => ({ content: 'ok', finishReason: 'STOP' }) -) - -mock.module('react-native', () => ({ - Platform: { OS: 'android' }, - TurboModuleRegistry: { - getEnforcing: () => ({ - generateText, - streamText: async () => {}, - onToolCall: () => ({ remove: () => {} }), - onStreamUpdate: () => ({ remove: () => {} }), - onStreamToolCall: () => ({ remove: () => {} }), - onStreamComplete: () => ({ remove: () => {} }), - onStreamError: () => ({ remove: () => {} }), - }), - }, -})) - -const { createAdkProvider } = await import('../ai-sdk') - -const model = createAdkProvider({ modelType: 'gemini' })() - -// PNG signature followed by padding bytes -const pngBytes = new Uint8Array([ - 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 0, -]) - -function createCallOptions( - overrides: Partial = {} -): LanguageModelV4CallOptions { - return { - prompt: [{ role: 'user', content: [{ type: 'text', text: 'hello' }] }], - ...overrides, - } -} - -beforeEach(() => { - generateText.mockClear() -}) - -describe('adk prompt conversion', () => { - test('resolves top-level file media types to a full mime type', async () => { - await model.doGenerate( - createCallOptions({ - prompt: [ - { - role: 'user', - content: [ - { type: 'text', text: 'describe' }, - { - type: 'file', - mediaType: 'image', - data: { type: 'data', data: pngBytes }, - }, - ], - }, - ], - }) - ) - - expect(generateText.mock.calls.at(-1)![1]).toEqual([ - { - role: 'user', - parts: [ - { type: 'text', text: 'describe' }, - { - type: 'file', - mimeType: 'image/png', - data: 'iVBORw0KGgoAAAAA', - }, - ], - }, - ]) - }) -}) - -const reasoningWarning = { - type: 'unsupported', - feature: 'reasoning', - details: 'ADK models do not support configuring reasoning.', -} - -async function readFirstPart( - stream: ReadableStream -) { - const { value } = await stream.getReader().read() - return value -} - -describe('adk language model warnings', () => { - test('warns that reasoning is unsupported in doGenerate', async () => { - const result = await model.doGenerate( - createCallOptions({ reasoning: 'high' }) - ) - - expect(result.warnings).toEqual([reasoningWarning]) - }) - - test('warns that reasoning is unsupported in doStream', async () => { - const { stream } = await model.doStream( - createCallOptions({ reasoning: 'high' }) - ) - - expect(await readFirstPart(stream)).toEqual({ - type: 'stream-start', - warnings: [reasoningWarning], - }) - }) - - test('does not warn for provider-default reasoning', async () => { - const result = await model.doGenerate( - createCallOptions({ reasoning: 'provider-default' }) - ) - - expect(result.warnings).toEqual([]) - }) -}) - -describe('reasoning none and tool choice warnings', () => { - test('does not warn for reasoning none', async () => { - const result = await model.doGenerate( - createCallOptions({ reasoning: 'none' }) - ) - - expect(result.warnings).toEqual([]) - }) - - test('warns that required tool choice is unsupported', async () => { - const result = await model.doGenerate( - createCallOptions({ toolChoice: { type: 'required' } }) - ) - - expect(result.warnings).toEqual([ - { - type: 'unsupported', - feature: 'toolChoice', - details: 'ADK models only support auto and none tool choices.', - }, - ]) - }) -}) diff --git a/packages/adk/src/ai-sdk.ts b/packages/adk/src/ai-sdk.ts deleted file mode 100644 index 079e1505c..000000000 --- a/packages/adk/src/ai-sdk.ts +++ /dev/null @@ -1,712 +0,0 @@ -import type { - LanguageModelV4, - LanguageModelV4CallOptions, - LanguageModelV4FilePart, - LanguageModelV4FinishReason, - LanguageModelV4FunctionTool, - LanguageModelV4Prompt, - LanguageModelV4ProviderTool, - LanguageModelV4StreamPart, - LanguageModelV4Usage, - SharedV4Warning, -} from '@ai-sdk/provider' -import { - generateId, - isCustomReasoning, - jsonSchema, - parseJSON, - resolveFullMediaType, - type Tool as FullToolDefinition, - type ToolExecutionOptions, -} from '@ai-sdk/provider-utils' -import { Platform } from 'react-native' - -import { - type AdkAgentConfig, - type AdkGenerationOptions, - type AdkMessage, - type AdkMessagePart, - type AdkModelType, - type AdkTool, - type AdkToolParameter, - type AdkUsageMetadata, - getNativeAdkEngine, - type StreamToolCallEvent, - type ToolCallEvent, -} from './NativeAdkEngine' - -export interface AdkProviderOptions { - name?: string - description?: string - instruction?: string - modelType?: AdkModelType - modelName?: string - apiKey?: string - availableTools?: Record -} - -/** - * Whether the device can support on-device Gemini Nano at all - * (ML Kit `checkStatus !== 0`). Does not download models. - */ -async function checkNanoSupported(): Promise { - if (Platform.OS !== 'android') { - return false - } - - return getNativeAdkEngine().isNanoSupported() -} - -export interface AdkProvider { - (): LanguageModelV4 - readonly specificationVersion: 'v4' - languageModel: () => LanguageModelV4 - /** Device capability check (ML Kit `checkStatus !== 0`). Returns `false` on non-Android. */ - isNanoSupported: () => Promise - /** - * Whether `modelType` can be used now. For `genai-nano`, checks ML Kit readiness - * (status 1 or 3). Returns `false` when {@link isNanoSupported} is `false`. - * Cloud `gemini` always resolves to `true` on Android. - */ - isAvailable: (modelType?: AdkModelType) => Promise - /** Downloads and initializes on-device Gemini Nano. No-op for cloud models. */ - prepareNano: () => Promise -} - -export function createAdkProvider( - options: AdkProviderOptions = {} -): AdkProvider { - const createLanguageModel = () => new AdkChatLanguageModel(options) - const provider = function () { - return createLanguageModel() - } - provider.specificationVersion = 'v4' as const - provider.languageModel = createLanguageModel - provider.isNanoSupported = () => checkNanoSupported() - provider.isAvailable = async (modelType: AdkModelType = 'genai-nano') => { - if (modelType === 'genai-nano' && !(await checkNanoSupported())) { - return false - } - return getNativeAdkEngine().isAvailable(modelType).then(Boolean) - } - provider.prepareNano = () => getNativeAdkEngine().prepareNano() - return provider -} - -export const adk = createAdkProvider() - -type Tool = LanguageModelV4FunctionTool | LanguageModelV4ProviderTool -type ToolDefinitionSet = Record - -const createEmptyUsage = (): LanguageModelV4Usage => ({ - inputTokens: { - total: undefined, - noCache: undefined, - cacheRead: undefined, - cacheWrite: undefined, - }, - outputTokens: { - total: undefined, - text: undefined, - reasoning: undefined, - }, -}) - -const mapUsage = (usage?: AdkUsageMetadata): LanguageModelV4Usage => { - if (!usage) { - return createEmptyUsage() - } - - return { - inputTokens: { - total: usage.promptTokenCount, - noCache: undefined, - cacheRead: undefined, - cacheWrite: undefined, - }, - outputTokens: { - total: usage.candidatesTokenCount, - text: undefined, - reasoning: undefined, - }, - raw: { - promptTokenCount: usage.promptTokenCount, - candidatesTokenCount: usage.candidatesTokenCount, - totalTokenCount: usage.totalTokenCount, - }, - } -} - -const convertFinishReason = ( - finishReason?: string -): LanguageModelV4FinishReason => { - let unified: LanguageModelV4FinishReason['unified'] = 'other' - - if (finishReason === 'STOP' || finishReason === 'stop') { - unified = 'stop' - } else if (finishReason === 'MAX_TOKENS' || finishReason === 'length') { - unified = 'length' - } else if (finishReason === 'tool_calls' || finishReason === 'TOOL_CALLS') { - unified = 'tool-calls' - } - - return { - unified, - raw: finishReason, - } -} - -const schemaPropertyToAdkParameter = ( - name: string, - value: unknown, - required: boolean -): AdkToolParameter => { - const schema = value as { - type?: string - description?: string - } - - return { - name, - description: schema?.description, - type: (schema?.type as AdkToolParameter['type']) ?? 'string', - required, - } -} - -const getCallWarnings = ( - options: LanguageModelV4CallOptions -): SharedV4Warning[] => { - const warnings: SharedV4Warning[] = [] - - // `none` is already what a model without reasoning control does - if (isCustomReasoning(options.reasoning) && options.reasoning !== 'none') { - warnings.push({ - type: 'unsupported', - feature: 'reasoning', - details: 'ADK models do not support configuring reasoning.', - }) - } - - if ( - options.toolChoice?.type === 'required' || - options.toolChoice?.type === 'tool' - ) { - warnings.push({ - type: 'unsupported', - feature: 'toolChoice', - details: 'ADK models only support auto and none tool choices.', - }) - } - - return warnings -} - -const uint8ArrayToBase64 = (bytes: Uint8Array): string => { - let binary = '' - for (let index = 0; index < bytes.length; index++) { - binary += String.fromCharCode(bytes[index]) - } - return btoa(binary) -} - -const normalizeFilePart = ( - part: LanguageModelV4FilePart -): { data: string; mediaType: string } => { - if (part.data.type !== 'data') { - throw new Error( - 'ADK wrapper does not support file URLs or references yet. Pass base64 or Uint8Array data.' - ) - } - - const data = - typeof part.data.data === 'string' - ? part.data.data.replace(/^data:[^;]+;base64,/, '') - : uint8ArrayToBase64(part.data.data) - - return { - data, - // v4 allows top-level media types such as `image`, which Gemini rejects - mediaType: resolveFullMediaType({ part }), - } -} - -class AdkChatLanguageModel implements LanguageModelV4 { - readonly specificationVersion = 'v4' - readonly supportedUrls = {} - - readonly provider = 'adk' - readonly modelId: string - - private readonly agentConfig: AdkAgentConfig - private tools: ToolDefinitionSet = {} - private nanoPreparePromise: Promise | null = null - - constructor(options: AdkProviderOptions) { - const modelType = options.modelType ?? 'genai-nano' - this.modelId = options.modelName ?? 'gemini-nano' - this.agentConfig = { - name: options.name ?? 'react_native_adk_agent', - description: options.description ?? 'React Native ADK agent', - instruction: options.instruction, - model: { - type: modelType, - name: this.modelId, - apiKey: options.apiKey, - }, - } - this.updateTools(options.availableTools ?? {}) - } - - updateTools(tools: ToolDefinitionSet) { - this.tools = tools - } - - public isAvailable() { - return getNativeAdkEngine() - .isAvailable(this.agentConfig.model.type) - .then(Boolean) - } - - public prepareNano() { - return getNativeAdkEngine().prepareNano() - } - - async prepare(): Promise { - await this.ensureNanoPrepared() - } - - private ensureNanoPrepared(): Promise { - if (this.agentConfig.model.type !== 'genai-nano') { - return Promise.resolve() - } - - if (!this.nanoPreparePromise) { - this.nanoPreparePromise = checkNanoSupported() - .then((supported) => { - if (!supported) { - throw new Error( - 'Gemini Nano is not supported on this device or device has not fetched the latest configuration to support it' - ) - } - return getNativeAdkEngine().prepareNano() - }) - .catch((error) => { - this.nanoPreparePromise = null - throw error - }) - } - - return this.nanoPreparePromise - } - - private prepareMessages(messages: LanguageModelV4Prompt): AdkMessage[] { - return messages.map((message): AdkMessage => { - if (message.role === 'system') { - return { - role: 'system', - content: message.content, - } - } - - if (message.role === 'user') { - const parts = message.content - .map((part): AdkMessagePart | null => { - if (part.type === 'text') { - return { type: 'text', text: part.text } - } - if (part.type === 'file') { - const { data, mediaType } = normalizeFilePart(part) - return { type: 'file', mimeType: mediaType, data } - } - console.warn('Unsupported user message content type:', part) - return null - }) - .filter((part): part is AdkMessagePart => part !== null) - - if (parts.length === 1 && parts[0]?.type === 'text') { - return { - role: 'user', - content: parts[0].text, - } - } - - return { - role: 'user', - parts, - } - } - - if (message.role === 'assistant') { - const content = message.content.reduce((acc, part) => { - if (part.type === 'text') { - return acc + part.text - } - console.warn('Unsupported assistant message content type:', part) - return acc - }, '') - - return { - role: 'assistant', - content, - } - } - - if (message.role === 'tool') { - const content = message.content - .filter((part) => part.type === 'tool-result') - .map((part) => { - if (part.output.type === 'execution-denied') { - return part.output.reason ?? 'Execution denied' - } - return typeof part.output.value === 'string' - ? part.output.value - : JSON.stringify(part.output.value) - }) - .join('\n') - - return { - role: 'user', - content, - } - } - - throw new Error(`Unsupported message role for ADK provider`) - }) - } - - private prepareGenerationOptions( - options: LanguageModelV4CallOptions - ): AdkGenerationOptions { - if ( - options.responseFormat?.type === 'json' && - options.responseFormat.schema - ) { - throw new Error( - 'ADK does not support responseFormat.schema yet. Use JSON mode without a schema or omit responseFormat.' - ) - } - - return { - temperature: options.temperature, - maxTokens: options.maxOutputTokens, - topP: options.topP, - topK: options.topK, - responseFormat: - options.responseFormat?.type === 'json' - ? { - type: 'json', - mimeType: 'application/json', - } - : undefined, - } - } - - private prepareTools(tools: Tool[] = []) { - return tools - .filter((tool) => tool.type === 'function') - .map((tool) => { - const required = Array.isArray(tool.inputSchema.required) - ? tool.inputSchema.required - : [] - const properties = tool.inputSchema.properties ?? {} - - const parameters = Object.entries(properties).map(([name, value]) => - schemaPropertyToAdkParameter(name, value, required.includes(name)) - ) - - const schema = jsonSchema(tool.inputSchema) - - return { - native: { - id: generateId(), - name: tool.name, - description: tool.description ?? '', - inputSchema: tool.inputSchema, - parameters, - } satisfies AdkTool, - execute: async (modelInput: unknown) => { - const text = - typeof modelInput === 'string' - ? modelInput - : JSON.stringify(modelInput ?? {}) - const toolCallArguments = await parseJSON({ - text, - schema, - }) - const opts: ToolExecutionOptions = { - toolCallId: generateId(), - messages: [], - context: undefined, - } - return this.tools[tool.name]?.execute?.(toolCallArguments, opts) - }, - } - }) - } - - private registerTools( - preparedTools: ReturnType - ) { - globalThis.__ADK_TOOLS__ = globalThis.__ADK_TOOLS__ ?? {} - - for (const tool of preparedTools) { - globalThis.__ADK_TOOLS__[tool.native.id] = tool.execute - } - } - - private unregisterTools( - preparedTools: ReturnType - ) { - for (const tool of preparedTools) { - delete globalThis.__ADK_TOOLS__?.[tool.native.id] - } - } - - private createToolCallListener(scope: { - streamId?: string - runId?: string - ownedToolIds: Set - }) { - const native = getNativeAdkEngine() - - return native.onToolCall(async (event: ToolCallEvent) => { - if (scope.streamId !== undefined && event.streamId !== scope.streamId) { - return - } - if (scope.runId !== undefined && event.runId !== scope.runId) { - return - } - if (!scope.ownedToolIds.has(event.toolId)) { - return - } - - const executor = globalThis.__ADK_TOOLS__?.[event.toolId] - if (!executor) { - native.submitToolResult( - event.toolCallId, - JSON.stringify({ error: `Tool not found: ${event.toolId}` }) - ) - return - } - - try { - const result = await executor(event.arguments) - native.submitToolResult( - event.toolCallId, - typeof result === 'string' ? result : JSON.stringify(result ?? {}) - ) - } catch (error) { - native.submitToolResult( - event.toolCallId, - JSON.stringify({ - error: error instanceof Error ? error.message : String(error), - }) - ) - } - }) - } - - async doGenerate(options: LanguageModelV4CallOptions) { - const messages = this.prepareMessages(options.prompt) - const generationOptions = this.prepareGenerationOptions(options) - const preparedTools = this.prepareTools(options.tools) - const nativeTools = preparedTools.map((tool) => tool.native) - const ownedToolIds = new Set(preparedTools.map((tool) => tool.native.id)) - const runId = generateId() - - this.registerTools(preparedTools) - const toolListener = this.createToolCallListener({ runId, ownedToolIds }) - - try { - await this.ensureNanoPrepared() - - const response = await getNativeAdkEngine().generateText( - runId, - messages, - this.agentConfig, - generationOptions, - nativeTools - ) - - return { - content: [{ type: 'text' as const, text: response.content }], - finishReason: convertFinishReason(response.finishReason), - usage: mapUsage(response.usage), - warnings: getCallWarnings(options), - } - } finally { - toolListener.remove() - this.unregisterTools(preparedTools) - } - } - - async doStream(options: LanguageModelV4CallOptions) { - const messages = this.prepareMessages(options.prompt) - const generationOptions = this.prepareGenerationOptions(options) - const preparedTools = this.prepareTools(options.tools) - const nativeTools = preparedTools.map((tool) => tool.native) - const agentConfig = this.agentConfig - const streamId = generateId() - const ownedToolIds = new Set(preparedTools.map((tool) => tool.native.id)) - const warnings = getCallWarnings(options) - - if (options.responseFormat?.type === 'json') { - throw new Error('Streaming JSON responses is not yet supported by ADK.') - } - - if (typeof ReadableStream === 'undefined') { - throw new Error( - 'ReadableStream is not available in this environment. Please load a polyfill, such as web-streams-polyfill.' - ) - } - - this.registerTools(preparedTools) - - let listeners: { remove(): void }[] = [] - let cleanedUp = false - const toolListener = this.createToolCallListener({ streamId, ownedToolIds }) - - const cleanup = () => { - if (cleanedUp) { - return - } - cleanedUp = true - listeners.forEach((listener) => { - listener.remove() - }) - listeners = [] - toolListener.remove() - this.unregisterTools(preparedTools) - } - - const stream = new ReadableStream({ - start: async (controller) => { - controller.enqueue({ type: 'stream-start', warnings }) - - const addListener = (listener: { remove(): void }) => { - listeners.push(listener) - } - - addListener( - getNativeAdkEngine().onStreamUpdate((data) => { - if (data.streamId === streamId && data.delta) { - controller.enqueue({ - type: 'text-delta', - delta: data.delta, - id: streamId, - }) - } - }) - ) - - addListener( - getNativeAdkEngine().onStreamToolCall((data: StreamToolCallEvent) => { - if (data.streamId !== streamId) { - return - } - - if (data.phase === 'start' && data.toolName) { - controller.enqueue({ - type: 'tool-input-start', - id: data.toolCallId, - toolName: data.toolName, - providerExecuted: true, - }) - return - } - - if (data.phase === 'delta' && data.inputDelta) { - controller.enqueue({ - type: 'tool-input-delta', - id: data.toolCallId, - delta: data.inputDelta, - }) - return - } - - if (data.phase === 'end' && data.toolName && data.input) { - controller.enqueue({ - type: 'tool-input-end', - id: data.toolCallId, - }) - controller.enqueue({ - type: 'tool-call', - toolCallId: data.toolCallId, - toolName: data.toolName, - input: data.input, - providerExecuted: true, - }) - } - }) - ) - - addListener( - getNativeAdkEngine().onStreamComplete((data) => { - if (data.streamId === streamId) { - controller.enqueue({ - type: 'text-end', - id: streamId, - }) - controller.enqueue({ - type: 'finish', - finishReason: convertFinishReason(data.finishReason), - usage: mapUsage(data.usage), - }) - cleanup() - controller.close() - } - }) - ) - - addListener( - getNativeAdkEngine().onStreamError((data) => { - if (data.streamId === streamId) { - controller.enqueue({ - type: 'error', - error: new Error(data.error), - }) - cleanup() - controller.close() - } - }) - ) - - try { - await this.ensureNanoPrepared() - - controller.enqueue({ - type: 'text-start', - id: streamId, - }) - - await getNativeAdkEngine().streamText( - streamId, - messages, - agentConfig, - generationOptions, - nativeTools - ) - } catch (error) { - cleanup() - const message = error instanceof Error ? error.message : String(error) - controller.error(new Error(`ADK stream failed: ${message}`)) - } - }, - cancel: () => { - cleanup() - void getNativeAdkEngine() - .cancelStream(streamId) - .catch((error) => console.warn('[adk] cancelStream failed:', error)) - }, - }) - - return { - stream, - rawCall: { - rawPrompt: options.prompt, - rawSettings: {}, - }, - } - } -} diff --git a/packages/adk/src/index.ts b/packages/adk/src/index.ts deleted file mode 100644 index 405983830..000000000 --- a/packages/adk/src/index.ts +++ /dev/null @@ -1,14 +0,0 @@ -export { - adk, - type AdkProvider, - type AdkProviderOptions, - createAdkProvider, -} from './ai-sdk' -export { - type AdkAgentConfig, - default as AdkEngine, - type AdkMessage, - type AdkModelType, - type AdkTool, - getNativeAdkEngine, -} from './NativeAdkEngine' diff --git a/packages/adk/tsconfig.build.json b/packages/adk/tsconfig.build.json deleted file mode 100644 index acbe349fb..000000000 --- a/packages/adk/tsconfig.build.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "extends": "./tsconfig", - "include": ["src/**/*"], - "exclude": ["**/__tests__/**"] -} diff --git a/packages/adk/tsconfig.json b/packages/adk/tsconfig.json deleted file mode 100644 index 54f5777b4..000000000 --- a/packages/adk/tsconfig.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "extends": "../../tsconfig.base.json", - "include": ["src/**/*"], - "exclude": ["src/**/__tests__/**"] -} diff --git a/website/astro.config.mjs b/website/astro.config.mjs index 3b546c0d4..7f6611552 100644 --- a/website/astro.config.mjs +++ b/website/astro.config.mjs @@ -103,11 +103,6 @@ export default defineConfig({ ['embeddings', 'Embeddings'], ['transcription', 'Transcription'], ]), - group('ADK', 'adk', [ - ['getting-started', 'Getting started'], - ['generating', 'Generating'], - ['models', 'Models and availability'], - ]), group('Core AI', 'core-ai', [ ['getting-started', 'Getting started'], ['generating', 'Generating'], diff --git a/website/src/content/docs/docs/adk/generating.md b/website/src/content/docs/docs/adk/generating.md deleted file mode 100644 index a6539eaf0..000000000 --- a/website/src/content/docs/docs/adk/generating.md +++ /dev/null @@ -1,156 +0,0 @@ ---- -title: "Generating" ---- - -Generate text on Android with Gemini Nano: pass `adk()` to the AI SDK's -`generateText` or `streamText`. The examples on this page run on device. For -cloud Gemini, create the model as shown in -[Models & Availability](/docs/adk/models#cloud-gemini); everything else stays -the same. - -## Text Generation - -```typescript -import { adk } from '@react-native-ai/adk' -import { generateText } from 'ai' - -const { text } = await generateText({ - model: adk(), - prompt: 'Explain quantum computing in simple terms', -}) -``` - -The first call prepares Gemini Nano. To avoid the wait, call `adk.prepareNano()` -ahead of time, once [the availability check](/docs/adk/models#availability-check) -passes. - -## Streaming - -```typescript -import { adk } from '@react-native-ai/adk' -import { streamText } from 'ai' - -const { textStream } = streamText({ - model: adk(), - prompt: 'Write a short story about a robot learning to paint', -}) - -for await (const delta of textStream) { - console.log(delta) -} -``` - -Streaming needs the [polyfills](/docs/polyfills). - -## Token Usage - -`usage` reports Gemini's token counts, on `generateText` results and in the -`finish` part of a stream: - -```typescript -const { usage } = await generateText({ - model: adk(), - prompt: 'Count to five.', -}) - -console.log(usage.inputTokens) -console.log(usage.outputTokens) -console.log(usage.raw?.totalTokenCount) -``` - -## Tool Calling - -Let the model call your JavaScript functions, on device or in the cloud. ADK -runs the tool loop natively, so register each tool with the provider in -`availableTools`, and pass the ones a request may use in `tools` as usual: - -```typescript -import { createAdkProvider } from '@react-native-ai/adk' -import { generateText, tool } from 'ai' -import { z } from 'zod' - -const getCurrentTime = tool({ - description: 'Get the current time for a city', - inputSchema: z.object({ city: z.string() }), - execute: async ({ city }) => ({ - city, - time: new Date().toLocaleTimeString(), - }), -}) - -const adk = createAdkProvider({ - availableTools: { getCurrentTime }, -}) - -const { text } = await generateText({ - model: adk(), - tools: { getCurrentTime }, - prompt: 'What time is it in Warsaw?', -}) -``` - -Because ADK runs the loop: - -- AI SDK step controls, such as `stopWhen`, don't limit it. -- Streamed tool calls are marked `providerExecuted: true`. -- A `required` `toolChoice` is ignored with a warning. - -## Image Input - -Ask about an image by passing it as a file part, as base64, a data URL or a -`Uint8Array`: - -```typescript -const { text } = await generateText({ - model: adk(), - messages: [ - { - role: 'user', - content: [ - { type: 'text', text: 'What is in this image?' }, - { type: 'file', mediaType: 'image/jpeg', data: base64Image }, - ], - }, - ], -}) -``` - -File and HTTP URLs aren't supported yet, so read the image first. - -## JSON Output - -Ask for JSON with `Output.json()`: - -```typescript -import { generateText, Output } from 'ai' - -const { output } = await generateText({ - model: adk(), - prompt: 'Return a JSON object with name and age fields.', - output: Output.json(), -}) -``` - -ADK can't constrain the output to a schema or stream JSON yet, so validate -`output` in your app, for example with zod. - -## Options - -`temperature`, `maxOutputTokens`, `topP` and `topK` work as usual: - -```typescript -const { text } = await generateText({ - model: adk(), - prompt: 'Write a creative story', - temperature: 0.8, - maxOutputTokens: 500, - topP: 0.9, - topK: 40, -}) -``` - -:::tip[Long conversations] -Gemini Nano has a small context window and every request sends the whole chat. -To keep a rolling window and summarize older messages, wrap the model with -`wrapLanguageModelWithHistory`. See [Conversation History](/docs/history). -::: diff --git a/website/src/content/docs/docs/adk/getting-started.mdx b/website/src/content/docs/docs/adk/getting-started.mdx deleted file mode 100644 index b68d91df7..000000000 --- a/website/src/content/docs/docs/adk/getting-started.mdx +++ /dev/null @@ -1,95 +0,0 @@ ---- -title: "Getting Started" ---- - -import PackageManagerTabs from '../../../../components/starlight/PackageManagerTabs.astro' - -Run Gemini Nano on Android, on the device, with the Vercel AI SDK. Android -provides the model through AICore, so there's no model to bundle and no API key. -When you need a larger model or support for every device, the same provider -runs cloud Gemini. It's built on -[Google's Agent Development Kit](https://developer.android.com/ai/adk). - -## Installation - - - -The AI SDK needs its [polyfills](/docs/polyfills). - -## Requirements - -- Android with `minSdkVersion` 26 or higher -- React Native 0.76 or newer, with the New Architecture -- For Gemini Nano, a physical device with AICore support, running Android 14 - or newer. See the - [ML Kit GenAI documentation](https://developers.google.com/ml-kit/genai#feature-device) - for the supported devices. Cloud Gemini works on any device. - -## Native Setup - -Raise `minSdkVersion` to 26, and exclude two files that Google's libraries both ship, so the build doesn't fail on duplicates. - -### Expo - -With `expo-build-properties`: - -```json -{ - "expo": { - "plugins": [ - [ - "expo-build-properties", - { - "android": { - "minSdkVersion": 26, - "packagingOptions": { - "exclude": ["META-INF/INDEX.LIST", "META-INF/DEPENDENCIES"] - } - } - } - ] - ] - } -} -``` - -Then regenerate the native project: - -```bash -npx expo prebuild --clean -``` - -### Bare React Native - -Add this to `android/app/build.gradle`, and set `minSdkVersion` to 26: - -```groovy -android { - packagingOptions { - excludes += ["META-INF/INDEX.LIST", "META-INF/DEPENDENCIES"] - } -} -``` - -## Basic Usage - -Pass `adk()` to the AI SDK. Check that Gemini Nano can run on the device first: - -```typescript -import { adk } from '@react-native-ai/adk' -import { generateText } from 'ai' - -if (await adk.isAvailable('genai-nano')) { - const { text } = await generateText({ - model: adk(), - prompt: 'Summarize on-device AI in one sentence.', - }) -} -``` - -[Models & Availability](/docs/adk/models) covers the availability checks and cloud Gemini. - -## Next Steps - -- [Generating](/docs/adk/generating): streaming, tool calling, image input and JSON output -- [Models & Availability](/docs/adk/models): checking for Gemini Nano, cloud Gemini and provider options diff --git a/website/src/content/docs/docs/adk/models.md b/website/src/content/docs/docs/adk/models.md deleted file mode 100644 index 3d32f5ee8..000000000 --- a/website/src/content/docs/docs/adk/models.md +++ /dev/null @@ -1,105 +0,0 @@ ---- -title: "Models & Availability" ---- - -`adk()` runs Gemini Nano on the device. Android provides it through AICore, so -there's nothing to download or bundle, but only some devices can run it. When -you need a larger model, or support for every device, use cloud Gemini. - -| Model | `modelType` | `modelName` | Runs | -| ------------ | ------------ | ------------------------------ | -------------------------------- | -| Gemini Nano | `genai-nano` | `gemini-nano` (the default) | On device, offline, no API key | -| Cloud Gemini | `gemini` | Required, such as `gemini-2.5-flash` | Google AI API, with your API key | - -## Gemini Nano - -### Availability Check - -Check before you offer Gemini Nano in your app. There are two checks, both -cheap and safe to run at startup and again when the app resumes: - -| Call | Answers | -| ------------------------------- | ------------------------------------------------ | -| `adk.isNanoSupported()` | Can this device ever run Gemini Nano? | -| `adk.isAvailable('genai-nano')` | Can it run now, or is it still being downloaded? | - -```typescript -import { adk } from '@react-native-ai/adk' - -if (!(await adk.isNanoSupported())) { - // Hide Gemini Nano: this device can't run it - return -} - -if (!(await adk.isAvailable('genai-nano'))) { - // Show it as not ready yet, for example while Android downloads it - return -} - -await adk.prepareNano() -const model = adk() -``` - -Both are based on ML Kit's model status. See the -[ML Kit GenAI documentation](https://developers.google.com/ml-kit/genai#feature-device) -for the supported devices. - -### Preparing - -`adk.prepareNano()` loads Gemini Nano ahead of time. If you skip it, the first `generateText` or `streamText` call prepares it. That call -only checks `isNanoSupported()`, so check `isAvailable('genai-nano')` yourself -before showing the model as ready, and handle errors in your chat flow. - -## Cloud Gemini - -Use cloud Gemini on devices without Gemini Nano, on the emulator, or when you -need a more capable model. Create a provider with `modelType: 'gemini'`, a -`modelName` and your API key: - -```typescript -import { createAdkProvider } from '@react-native-ai/adk' -import { generateText } from 'ai' - -const adk = createAdkProvider({ - apiKey: process.env.GOOGLE_API_KEY, - modelType: 'gemini', - modelName: 'gemini-2.5-flash', -}) - -const { text } = await generateText({ - model: adk(), - prompt: 'What time is it in New York?', -}) -``` - -Cloud models need no download or prepare step. - -:::caution -Don't ship API keys in your app. Use a backend proxy or load the key securely -at runtime. -::: - -## Provider Options - -`createAdkProvider` creates a provider with its own settings, for example a -system instruction: - -```typescript -import { createAdkProvider } from '@react-native-ai/adk' - -const adk = createAdkProvider({ - instruction: 'You are a concise, friendly assistant.', -}) - -const model = adk() -``` - -| Option | Type | Description | -| ---------------- | -------------------------- | ----------------------------------------------------------------- | -| `modelType` | `'genai-nano' \| 'gemini'` | On device or cloud (default: `genai-nano`) | -| `modelName` | `string` | Model name (default: `gemini-nano`; set it for cloud Gemini) | -| `apiKey` | `string` | Google AI API key, for cloud Gemini only | -| `instruction` | `string` | System instruction | -| `availableTools` | `Record` | Tools ADK can call, see [Tool Calling](/docs/adk/generating#tool-calling) | -| `name` | `string` | ADK agent name (default: `react_native_adk_agent`) | -| `description` | `string` | ADK agent description | diff --git a/website/src/content/docs/docs/index.md b/website/src/content/docs/docs/index.md index f4de5d690..e9c600c30 100644 --- a/website/src/content/docs/docs/index.md +++ b/website/src/content/docs/docs/index.md @@ -29,7 +29,6 @@ const result = streamText({ | [Apple](/docs/apple/getting-started) | iOS | Built in: Apple Intelligence, speech and embeddings | Native, provided by the platform | | [Core AI](/docs/core-ai/getting-started) | iOS 27 | Yours: language, image, speech and vision models | Native, provided by the platform | | [Android](/docs/android/getting-started) | Android | Built in: Gemini Nano, speech recognition, embeddings | Native, provided by the platform | -| [ADK](/docs/adk/getting-started) | Android | Built in: Gemini Nano, or cloud Gemini | Native, provided by the platform | | [Llama](/docs/llama/getting-started) | iOS and Android | Yours: GGUF models from Hugging Face | Third-party binding, llama.rn | Built-in models need no download and work on supported devices right away. diff --git a/website/src/site/content.ts b/website/src/site/content.ts index b290be57f..b295d9d9f 100644 --- a/website/src/site/content.ts +++ b/website/src/site/content.ts @@ -70,7 +70,7 @@ export const docs = { polyfills: '/docs/polyfills', apple: '/docs/apple/getting-started', coreAi: '/docs/core-ai/getting-started', - adk: '/docs/adk/getting-started', + android: '/docs/android/getting-started', llama: '/docs/llama/getting-started', llms: '/llms.txt', }; @@ -128,7 +128,7 @@ export const faq = [ }, { q: 'Does anything leave the device?', - a: 'No, unless you opt in. Inference runs locally. Apple’s Private Cloud Compute model and ADK’s cloud Gemini are cloud models you choose explicitly.', + a: 'No, unless you opt in. Inference runs locally. Apple’s Private Cloud Compute model is a cloud model you choose explicitly.', }, { q: 'Which AI SDK version do I need?', @@ -174,11 +174,11 @@ const result = streamText({ }), prompt: 'Summarize my notes', })`, - adk: `import { adk } from '@react-native-ai/adk' + android: `import { android } from '@react-native-ai/android' import { streamText } from 'ai' const result = streamText({ - model: adk(), + model: android(), prompt: 'Summarize my notes', })`, llama: `import { downloadModel, llama } from '@react-native-ai/llama' @@ -232,17 +232,17 @@ export const runtimes = [ code: code.coreAi, }, { - id: 'adk', - name: 'ADK', + id: 'android', + name: 'Android', runtime: 'Gemini Nano via AICore', platforms: 'Android 14+', - pkg: '@react-native-ai/adk', + pkg: '@react-native-ai/android', models: 'Built in: Gemini Nano', owner: 'native' as const, - body: 'Google’s on-device Gemini through the Agent Development Kit, with tool calling and an opt-in cloud Gemini.', - href: docs.adk, - file: 'adk.ts', - code: code.adk, + body: 'Google’s on-device Gemini Nano through ML Kit GenAI, with tool calling, transcription and embeddings.', + href: docs.android, + file: 'android.ts', + code: code.android, }, { id: 'llama', @@ -280,7 +280,7 @@ export const home = { headline: ['Swap the model.', 'Keep the code.'], steps: [ { title: 'Install the providers', body: 'Add the provider for each platform you ship to, next to the AI SDK.', cmd: site.install }, - { title: 'Pick a model per platform', body: 'Use `apple()` on iOS and `adk()` on Android, or a model you ship with Core AI or Llama.', cmd: 'npm install @react-native-ai/adk' }, + { title: 'Pick a model per platform', body: 'Use `apple()` on iOS and `android()` on Android, or a model you ship with Core AI or Llama.', cmd: 'npm install @react-native-ai/android' }, { title: 'Call the AI SDK', body: 'Pass the model to `generateText`, `streamText`, `embed` or any other AI SDK function. The rest of your code stays the same, on iOS and Android.', cmd: 'npx expo run:android' }, ], /** One tab per runtime, each with its real setup snippet. */ @@ -299,7 +299,7 @@ export const home = { runtimes: { label: 'Runtimes', headline: ['The fastest runtime on each platform.', 'One API over all of them.'], - lede: 'Apple, Core AI and ADK run on the AI stack the platform provides: native, hardware-accelerated and with its system models. Llama runs GGUF models sized for phones on iOS and Android through llama.rn, a third-party binding.', + lede: 'Apple, Core AI and Android run on the AI stack the platform provides: native, hardware-accelerated and with its system models. Llama runs GGUF models sized for phones on iOS and Android through llama.rn, a third-party binding.', }, feature: { @@ -318,7 +318,7 @@ export const home = { items: [ { icon: 'code', title: 'Text and streaming', body: '`generateText` and `streamText` with every provider, token by token.', href: '/docs/apple/generating' }, { icon: 'form', title: 'Structured output', body: 'Pass a zod schema and get typed objects back from on-device models.', href: '/docs/apple/generating' }, - { icon: 'signpost', title: 'Tool calling', body: 'Models call your JavaScript functions, with Apple and ADK running the loop natively.', href: '/docs/apple/tools' }, + { icon: 'signpost', title: 'Tool calling', body: 'Models call your JavaScript functions, with Apple running the loop natively.', href: '/docs/apple/tools' }, { icon: 'search', title: 'Embeddings and reranking', body: 'Semantic search on device with `embed`, `embedMany` and `rerank`.', href: '/docs/apple/embeddings' }, { icon: 'flag', title: 'Transcription and speech', body: 'Speech to text with SpeechAnalyzer, and text to speech with system or GGUF voices.', href: '/docs/apple/transcription' }, { icon: 'grid', title: 'Image generation', body: 'Generate images on iPhone with Core AI diffusion models through `generateImage`.', href: '/docs/core-ai/images-and-video' }, @@ -334,7 +334,7 @@ export const home = { { icon: 'lock', title: 'Data stays local', body: 'Inference runs on the device, so prompts and replies never reach a server.' }, { icon: 'network', title: 'Works offline', body: 'No network round trip and nothing to wait for. It works in airplane mode.' }, { icon: 'package', title: 'No keys, no bills', body: 'System models need no API key, and there is no per-token cost.' }, - { icon: 'faders', title: 'Cloud is opt-in', body: 'Private Cloud Compute and cloud Gemini run only when you ask for them.' }, + { icon: 'faders', title: 'Cloud is opt-in', body: 'Apple’s Private Cloud Compute runs only when you ask for it.' }, ] as { icon: IconName; title: string; body: string }[], }, }; diff --git a/website/vercel.json b/website/vercel.json index 7d9beeb41..292ffd9d8 100644 --- a/website/vercel.json +++ b/website/vercel.json @@ -6,6 +6,13 @@ "outputDirectory": "dist", "cleanUrls": true, "trailingSlash": false, + "redirects": [ + { + "source": "/docs/adk/:page(getting-started|generating|models)", + "destination": "/docs/android/:page", + "permanent": true + } + ], "rewrites": [ { "source": "/sitemap.xml",