diff --git a/packages/adk/README.md b/packages/adk/README.md index 2ad9b3fd3..72f9eec8d 100644 --- a/packages/adk/README.md +++ b/packages/adk/README.md @@ -1,12 +1,11 @@ # ADK Provider for Vercel AI SDK -A Vercel AI SDK provider for [Google's Agent Development Kit (ADK)](https://developer.android.com/ai/adk), enabling on-device Gemini Nano and cloud Gemini in React Native applications on Android. +A Vercel AI SDK provider for [Google's Agent Development Kit (ADK)](https://developer.android.com/ai/adk), running Gemini Nano on device in React Native apps on Android. **Requirements:** - Android only, with `minSdkVersion` 26+ set in your app -- A device that supports Gemini Nano through AICore for on-device inference (see [ML Kit GenAI device support](https://developers.google.com/ml-kit/genai#feature-device)) -- A Google AI API key for cloud Gemini +- A device that supports Gemini Nano through AICore (see [ML Kit GenAI device support](https://developers.google.com/ml-kit/genai#feature-device)) - Vercel AI SDK v7 - React Native New Architecture @@ -25,16 +24,14 @@ const { text } = await generateText({ ## Features - ✅ On-device Gemini Nano through ML Kit GenAI, with model selection -- ✅ Cloud Gemini through ADK `LlmAgent` -- ✅ Availability, download progress and token counting for Gemini Nano +- ✅ Availability, download progress and token counting - ✅ Streaming -- ✅ Tool calling, with the agent loop run natively by ADK -- ✅ Structured outputs -- ✅ Reasoning - ✅ Image prompts - ✅ Token usage - ✅ Error codes +ADK's ML Kit model has no tool calling or structured output for Gemini Nano yet, so tools and JSON output return `unsupported` warnings. + ## Documentation For complete installation instructions and API documentation, visit our [documentation site](https://react-native-ai.com/docs/adk). 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 index 75d4ec2dc..2e7a02750 100644 --- 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 @@ -11,12 +11,9 @@ import com.google.adk.kt.agents.RunConfig import com.google.adk.kt.agents.StreamingMode import com.google.adk.kt.events.Event import com.google.adk.kt.mlkit.GenaiPrompt -import com.google.adk.kt.models.Gemini -import com.google.adk.kt.models.Model 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 @@ -42,15 +39,9 @@ import kotlinx.coroutines.sync.withLock import java.util.UUID import java.util.concurrent.ConcurrentHashMap -data class AdkToolCallRecord(val toolCallId: String, val toolName: String, val input: String) - -data class AdkToolResultRecord(val toolCallId: String, val toolName: String, val output: String) - data class AdkRunResult( val content: String, val reasoning: String?, - val toolCalls: List, - val toolResults: List, val finishReason: String?, val usage: UsageMetadata?, ) @@ -83,12 +74,6 @@ fun Event.textParts(thoughts: Boolean): String = class AdkAgentRunner( private val context: Context, - private val onToolCall: ( - toolCallId: String, - toolId: String, - arguments: String, - scope: ToolCallScope?, - ) -> Unit, private val onDownloadProgress: ( phase: String, bytesDownloaded: Long?, @@ -118,13 +103,6 @@ class AdkAgentRunner( ) } - private suspend fun nanoStatus(config: NanoConfig): Int = - try { - nanoClient(config).checkStatus() - } catch (_: Exception) { - FeatureStatus.UNAVAILABLE - } - /** Downloads Gemini Nano when needed, reporting progress, then warms it up. */ suspend fun prepareNano(config: NanoConfig) { nanoPrepareMutex.withLock { @@ -179,22 +157,7 @@ class AdkAgentRunner( return nanoModels.getValue(config) } - /** Whether this device supports Gemini Nano at all. Does not download the model. */ - suspend fun isNanoSupported(config: NanoConfig): Boolean = - nanoStatus(config) != FeatureStatus.UNAVAILABLE - - /** Whether Gemini Nano is downloaded or can be downloaded now. */ - suspend fun isNanoAvailable(config: NanoConfig): Boolean = - when (nanoStatus(config)) { - FeatureStatus.DOWNLOADABLE, FeatureStatus.AVAILABLE -> true - else -> false - } - - suspend fun getModelInfo(modelType: String, config: NanoConfig): AdkModelInfo { - if (modelType != "genai-nano") { - return AdkModelInfo(availability = "available") - } - + suspend fun getModelInfo(config: NanoConfig): AdkModelInfo { val model = nanoClient(config) val status = try { @@ -224,10 +187,8 @@ class AdkAgentRunner( messages: ReadableArray, config: ReadableMap, options: ReadableMap?, - tools: ReadableArray?, - runId: String, ): AdkRunResult { - val events = runAgent(messages, config, options, tools, stream = false, runId = runId).toList() + val events = runAgent(messages, config, options, stream = false).toList() val completeEvents = events.filterNot { it.partial } val text = completeEvents @@ -242,34 +203,11 @@ class AdkAgentRunner( .joinToString("\n\n") .ifEmpty { null } - val toolCalls = - completeEvents.flatMap { event -> - event.functionCalls().map { call -> - AdkToolCallRecord( - toolCallId = call.id ?: UUID.randomUUID().toString(), - toolName = call.name, - input = AdkJson.encode(call.args), - ) - } - } - val toolResults = - completeEvents.flatMap { event -> - event.functionResponses().map { response -> - AdkToolResultRecord( - toolCallId = response.id ?: "", - toolName = response.name, - output = AdkJson.encode(response.response), - ) - } - } - return AdkRunResult( content = text, reasoning = reasoning, - toolCalls = toolCalls, - toolResults = toolResults, finishReason = events.lastOrNull()?.finishReason?.name, - usage = AdkUsage.latestFromEvents(events), + usage = AdkUsage.fromEvents(events), ) } @@ -277,45 +215,24 @@ class AdkAgentRunner( messages: ReadableArray, config: ReadableMap, options: ReadableMap?, - tools: ReadableArray?, - streamId: String, ): Flow { - return runAgent(messages, config, options, tools, stream = true, streamId = streamId) + return runAgent(messages, config, options, stream = true) } 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, + model = GenaiPrompt.create(NanoSingleTextPrompt(preparedNano(agentConfig.nano))), + instruction = agentConfig.instruction?.let { Instruction(it) }, + generateContentConfig = parseGenerationConfig(options), ) val sessionService = InMemorySessionService() @@ -353,40 +270,10 @@ class AdkAgentRunner( ) } - private suspend fun createModel(config: AgentConfig): Model { - return when (config.modelType) { - "genai-nano" -> - NanoFunctionCallingModel(GenaiPrompt.create(preparedNano(config.nano), config.modelName)) - else -> Gemini(name = config.modelName, apiKey = config.apiKey) - } - } - - private fun parseAgentConfig(config: ReadableMap): AgentConfig = - parseAgentConfigStatic(config) - + /** ADK's ML Kit model passes on temperature, topK, maxOutputTokens and thinking. */ private fun parseGenerationConfig(options: ReadableMap?): GenerateContentConfig? { if (options == null) return null - val responseFormat = options.getMap("responseFormat") - val responseSchema = - responseFormat?.getMap("schema")?.let { schema -> - try { - AdkSchema.fromReadableMap(schema) - } catch (error: Exception) { - throw AdkErrors.AdkException( - AdkErrors.INVALID_SCHEMA, - "Unsupported response schema: ${error.message}", - error, - ) - } - } - - val responseMimeType = when { - responseFormat?.hasKey("mimeType") == true -> responseFormat.getString("mimeType") - responseFormat?.getString("type") == "json" -> "application/json" - else -> null - } - val thinkingConfig = options.getMap("thinking")?.let { thinking -> ThinkingConfig( @@ -398,19 +285,7 @@ class AdkAgentRunner( 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(), - seed = options.takeIf { it.hasKey("seed") }?.getDouble("seed")?.toInt(), - stopSequences = - options.getArray("stopSequences")?.let { array -> - (0 until array.size()).mapNotNull { index -> array.getString(index) } - }, - presencePenalty = - options.takeIf { it.hasKey("presencePenalty") }?.getDouble("presencePenalty")?.toFloat(), - frequencyPenalty = - options.takeIf { it.hasKey("frequencyPenalty") }?.getDouble("frequencyPenalty")?.toFloat(), - responseMimeType = responseMimeType, - responseSchema = responseSchema, thinkingConfig = thinkingConfig, ) } @@ -425,8 +300,7 @@ class AdkAgentRunner( val message = messages.getMap(index) ?: continue val role = when (message.getString("role")) { "assistant" -> Role.MODEL - "system" -> Role.SYSTEM - "user", "tool" -> Role.USER + "user" -> Role.USER else -> throw IllegalArgumentException( "Unsupported message role: ${message.getString("role")}", ) @@ -471,131 +345,28 @@ class AdkAgentRunner( context.contentResolver.openInputStream(Uri.parse(uri))?.use { it.readBytes() } ?: throw IllegalArgumentException("Cannot read file: $uri") - 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"), - nano = NanoConfig.from(model.getMap("nano")), - ) - } - } + // JS always sends the name, with defaults applied + private fun parseAgentConfig(config: ReadableMap): AgentConfig = + AgentConfig( + name = config.getString("name") + ?: throw IllegalArgumentException("Agent config requires a name"), + description = config.getString("description") ?: "", + instruction = config.getString("instruction"), + nano = NanoConfig.from(config.getMap("nano")), + ) } data class AgentConfig( val name: String, val description: String, val instruction: String?, - val modelType: String, - val modelName: String, - val apiKey: String?, val nano: NanoConfig, ) -/** ADK content role — one of [Role.USER], [Role.MODEL], or [Role.SYSTEM]. */ +/** ADK content role, [Role.USER] or [Role.MODEL]. */ 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/AdkErrors.kt b/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkErrors.kt index 107478c33..790d980aa 100644 --- a/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkErrors.kt +++ b/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkErrors.kt @@ -10,7 +10,6 @@ object AdkErrors { const val DOWNLOAD_FAILED = "DOWNLOAD_FAILED" const val GENERATION_ERROR = "GENERATION_ERROR" const val INVALID_MESSAGE = "INVALID_MESSAGE" - const val INVALID_SCHEMA = "INVALID_SCHEMA" const val CONTEXT_WINDOW_EXCEEDED = "CONTEXT_WINDOW_EXCEEDED" const val RATE_LIMITED = "RATE_LIMITED" const val BACKGROUND_USE_BLOCKED = "BACKGROUND_USE_BLOCKED" @@ -29,14 +28,7 @@ object AdkErrors { } current = current.cause } - - // Cloud Gemini surfaces HTTP status codes in the message - val message = error.message.orEmpty() - return when { - message.contains("RESOURCE_EXHAUSTED") || message.contains("429") -> RATE_LIMITED - message.contains("exceeds the maximum number of tokens") -> CONTEXT_WINDOW_EXCEEDED - else -> GENERATION_ERROR - } + return GENERATION_ERROR } private fun codeFor(error: GenAiException): String = 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 a0c45b10e..000000000 --- a/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkSchema.kt +++ /dev/null @@ -1,109 +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 - -/** - * Converts a JSON Schema from the AI SDK to an ADK [Schema]. Keywords that Gemini does not - * support, such as `$ref` or `additionalProperties`, are dropped. - */ -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 - } - } - } - - // `type: ["string", "null"]` and `anyOf: [..., { type: "null" }]` both mean nullable - val typeNames = readTypeNames(map) - var nullable = typeNames.contains("null") || map.getBooleanOrNull("nullable") == true - val type = typeNames.firstOrNull { it != "null" }?.let { parseType(it) } - - val anyOf = (map.getArray("anyOf") ?: map.getArray("oneOf"))?.let { array -> - val schemas = (0 until array.size()).mapNotNull { index -> array.getMap(index) } - val nonNull = schemas.filterNot { readTypeNames(it) == listOf("null") } - if (nonNull.size < schemas.size) nullable = true - nonNull.map { fromReadableMap(it) } - } - - // A nullable value with a single option is that option, marked nullable - if (type == null && anyOf?.size == 1) { - return anyOf.first().copy(nullable = if (nullable) true else anyOf.first().nullable) - } - - return Schema( - type = type, - properties = properties, - items = map.getMap("items")?.let { fromReadableMap(it) }, - required = required, - description = map.getString("description"), - enum = enumValues, - format = map.getString("format"), - nullable = if (nullable) true else null, - anyOf = anyOf?.takeIf { it.isNotEmpty() }, - title = map.getString("title"), - pattern = map.getString("pattern"), - minimum = map.getDoubleOrNull("minimum"), - maximum = map.getDoubleOrNull("maximum"), - minLength = map.getDoubleOrNull("minLength")?.toLong(), - maxLength = map.getDoubleOrNull("maxLength")?.toLong(), - minItems = map.getDoubleOrNull("minItems")?.toLong(), - maxItems = map.getDoubleOrNull("maxItems")?.toLong(), - minProperties = map.getDoubleOrNull("minProperties")?.toLong(), - maxProperties = map.getDoubleOrNull("maxProperties")?.toLong(), - ) - } - - private fun readTypeNames(map: ReadableMap): List { - if (!map.hasKey("type")) return emptyList() - return when (map.getType("type")) { - ReadableType.String -> listOfNotNull(map.getString("type")) - ReadableType.Array -> map.getArray("type")?.strings() ?: emptyList() - else -> emptyList() - } - } - - private fun ReadableArray.strings(): List = - (0 until size()).mapNotNull { index -> getString(index) } - - private fun ReadableMap.getDoubleOrNull(key: String): Double? = - if (hasKey(key) && getType(key) == ReadableType.Number) getDouble(key) else null - - private fun ReadableMap.getBooleanOrNull(key: String): Boolean? = - if (hasKey(key) && getType(key) == ReadableType.Boolean) getBoolean(key) else null - - private fun parseType(type: String?): Type? { - return when (type) { - "string" -> Type.STRING - "number" -> Type.NUMBER - "integer" -> Type.INTEGER - "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 705ad4543..000000000 --- a/packages/adk/android/src/main/java/com/callstack/ai/adk/AdkStreamToolCalls.kt +++ /dev/null @@ -1,113 +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 - } - } - } - - /** Emits the result of each tool call that ADK ran in this event. */ - fun handleResults(event: Event) { - for (functionResponse in event.functionResponses()) { - val toolCallId = functionResponse.id ?: continue - emit( - Arguments.createMap().apply { - putString("streamId", streamId) - putString("phase", "result") - putString("toolCallId", toolCallId) - putString("toolName", functionResponse.name) - putString("output", AdkJson.encode(functionResponse.response)) - }, - ) - } - } - - 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 index 480319291..8acc336f4 100644 --- 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 @@ -18,6 +18,26 @@ object AdkUsage { } } - fun latestFromEvents(events: List): UsageMetadata? = - events.mapNotNull { it.usageMetadata }.lastOrNull() + /** Usage of a whole run: the sum over its complete events, which carry their call's usage. */ + fun fromEvents(events: List): UsageMetadata? = + events.filterNot { it.partial }.fold(null as UsageMetadata?) { total, event -> add(total, event) } + ?: events.mapNotNull { it.usageMetadata }.lastOrNull() + + /** Adds a complete event's usage to [total]. Partial events repeat their call's usage. */ + fun add(total: UsageMetadata?, event: Event): UsageMetadata? { + val usage = event.usageMetadata + if (event.partial || usage == null) return total + if (total == null) return usage + + return UsageMetadata( + promptTokenCount = sum(total.promptTokenCount, usage.promptTokenCount), + candidatesTokenCount = sum(total.candidatesTokenCount, usage.candidatesTokenCount), + totalTokenCount = sum(total.totalTokenCount, usage.totalTokenCount), + thoughtsTokenCount = sum(total.thoughtsTokenCount, usage.thoughtsTokenCount), + toolUsePromptTokenCount = sum(total.toolUsePromptTokenCount, usage.toolUsePromptTokenCount), + cachedContentTokenCount = sum(total.cachedContentTokenCount, usage.cachedContentTokenCount), + ) + } + + private fun sum(a: Int?, b: Int?): Int? = if (a == null && b == null) null else (a ?: 0) + (b ?: 0) } 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 7ae69bf2f..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.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/NanoSingleTextPrompt.kt b/packages/adk/android/src/main/java/com/callstack/ai/adk/NanoSingleTextPrompt.kt new file mode 100644 index 000000000..271f4541f --- /dev/null +++ b/packages/adk/android/src/main/java/com/callstack/ai/adk/NanoSingleTextPrompt.kt @@ -0,0 +1,64 @@ +package com.callstack.ai.adk + +import com.google.mlkit.genai.prompt.GenerateContentRequest +import com.google.mlkit.genai.prompt.GenerateContentResponse +import com.google.mlkit.genai.prompt.GenerativeModel +import com.google.mlkit.genai.prompt.ImagePart +import com.google.mlkit.genai.prompt.TextPart +import com.google.mlkit.genai.prompt.content +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.emitAll +import kotlinx.coroutines.flow.flow + +/** + * Wraps ML Kit's [GenerativeModel] so Gemini Nano sees the whole prompt. + * + * Gemini Nano reads only the first text part of a request. ADK's `GenaiPrompt` sends one content + * per turn, and a system instruction where the device has no system prompt support, so Nano + * answers the instruction and never sees the user's message. This merges the text of every turn + * into one part, after the system instruction when the device does not support one. Images stay + * as their own parts, before the text. + */ +internal class NanoSingleTextPrompt( + private val delegate: GenerativeModel, +) : GenerativeModel by delegate { + + @Volatile private var systemPromptAvailable: Boolean? = null + + override suspend fun generateContent(request: GenerateContentRequest): GenerateContentResponse = + delegate.generateContent(merge(request)) + + override fun generateContentStream(request: GenerateContentRequest): Flow = + flow { emitAll(delegate.generateContentStream(merge(request))) } + + private suspend fun merge(request: GenerateContentRequest): GenerateContentRequest { + val parts = request.contents.flatMap { it.parts } + val systemInstruction = request.systemInstruction?.takeUnless { supportsSystemPrompt() } + if (parts.size <= 1 && systemInstruction == null) { + return request + } + + val text = parts.filterIsInstance().joinToString("\n") { it.textString } + val images = parts.filterIsInstance() + val merged = TextPart(systemInstruction?.let { "${it.textString}\n\n$text" } ?: text) + return GenerateContentRequest.Builder(content { parts(*images.toTypedArray(), merged) }) + .apply { + if (systemInstruction == null) this.systemInstruction = request.systemInstruction + temperature = request.temperature + seed = request.seed + topK = request.topK + candidateCount = request.candidateCount + maxOutputTokens = request.maxOutputTokens + enableThinking = request.enableThinking + promptPrefix = request.promptPrefix + cachedContextName = request.cachedContextName + } + .build() + } + + private suspend fun supportsSystemPrompt(): Boolean = + systemPromptAvailable + ?: runCatching { delegate.isSystemPromptAvailable() } + .getOrDefault(false) + .also { systemPromptAvailable = it } +} 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 index 416c031f3..0348a5470 100644 --- 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 @@ -8,6 +8,7 @@ import com.facebook.react.bridge.ReadableMap import com.google.adk.kt.events.Event import com.google.adk.kt.types.UsageMetadata import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.SupervisorJob @@ -25,18 +26,6 @@ class NativeAdkEngineModule(reactContext: ReactApplicationContext) : private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO) private val runner = AdkAgentRunner( context = reactContext, - onToolCall = { 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) - }, onDownloadProgress = { phase, bytesDownloaded, bytesToDownload -> emitOnNanoDownloadProgress( Arguments.createMap().apply { @@ -55,34 +44,10 @@ class NativeAdkEngineModule(reactContext: ReactApplicationContext) : reject(AdkErrors.codeFor(error), error.message ?: "Unknown ADK error", error) } - override fun isAvailable(modelType: String, nano: ReadableMap?, promise: Promise) { - scope.launch { - try { - val available = when (modelType) { - "genai-nano" -> runner.isNanoAvailable(NanoConfig.from(nano)) - else -> true - } - promise.resolve(available) - } catch (_: Exception) { - promise.resolve(false) - } - } - } - - override fun isNanoSupported(nano: ReadableMap?, promise: Promise) { - scope.launch { - try { - promise.resolve(runner.isNanoSupported(NanoConfig.from(nano))) - } catch (_: Exception) { - promise.resolve(false) - } - } - } - - override fun getModelInfo(modelType: String, nano: ReadableMap?, promise: Promise) { + override fun getModelInfo(nano: ReadableMap?, promise: Promise) { scope.launch { try { - val info = runner.getModelInfo(modelType, NanoConfig.from(nano)) + val info = runner.getModelInfo(NanoConfig.from(nano)) promise.resolve( Arguments.createMap().apply { putString("availability", info.availability) @@ -122,44 +87,16 @@ class NativeAdkEngineModule(reactContext: ReactApplicationContext) : messages: ReadableArray, config: ReadableMap, options: ReadableMap?, - tools: ReadableArray?, promise: Promise, ) { - val job = scope.launch { + // Started after it is tracked, so a run that ends at once is still removed + val job = scope.launch(start = CoroutineStart.LAZY) { try { - val result = runner.generateText(messages, config, options, tools, runId) + val result = runner.generateText(messages, config, options) val response = Arguments.createMap().apply { putString("role", "assistant") putString("content", result.content) result.reasoning?.let { putString("reasoning", it) } - putArray( - "toolCalls", - Arguments.createArray().apply { - result.toolCalls.forEach { call -> - pushMap( - Arguments.createMap().apply { - putString("toolCallId", call.toolCallId) - putString("toolName", call.toolName) - putString("input", call.input) - }, - ) - } - }, - ) - putArray( - "toolResults", - Arguments.createArray().apply { - result.toolResults.forEach { toolResult -> - pushMap( - Arguments.createMap().apply { - putString("toolCallId", toolResult.toolCallId) - putString("toolName", toolResult.toolName) - putString("output", toolResult.output) - }, - ) - } - }, - ) result.finishReason?.let { putString("finishReason", it) } AdkUsage.toWritableMap(result.usage)?.let { putMap("usage", it) } } @@ -168,10 +105,10 @@ class NativeAdkEngineModule(reactContext: ReactApplicationContext) : promise.rejectWithCode(error) } finally { activeRuns.remove(runId) - AdkToolBridge.cancelForStream(runId) } } activeRuns[runId] = job + job.start() } override fun streamText( @@ -179,7 +116,6 @@ class NativeAdkEngineModule(reactContext: ReactApplicationContext) : messages: ReadableArray, config: ReadableMap, options: ReadableMap?, - tools: ReadableArray?, promise: Promise, ) { if (activeRuns.containsKey(streamId)) { @@ -187,36 +123,24 @@ class NativeAdkEngineModule(reactContext: ReactApplicationContext) : return } - val job = scope.launch { + val job = scope.launch(start = CoroutineStart.LAZY) { 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 - // currentTurnReasoning / emittedReasoning: the same for thought parts + // Summed over complete events, with the latest partial usage as a fallback + var totalUsage: UsageMetadata? = null + var partialUsage: UsageMetadata? = null + // ADK may deliver text cumulatively (each event repeats the turn so far) or as + // deltas, so the turn text is rebuilt here and only the new part is sent to JS. + // Reasoning is tracked the same way. var currentTurnText = "" var emittedText = "" - var suppressCurrentTurn = false var currentTurnReasoning = "" var emittedReasoning = "" - val toolCallEmitter = AdkStreamToolCallEmitter(streamId) { args -> - emitOnStreamToolCall(args) - } - runner.streamText(messages, config, options, tools, streamId).collect { event -> + runner.streamText(messages, config, options).collect { event -> if (!isActive) return@collect - event.usageMetadata?.let { latestUsage = it } - toolCallEmitter.handleEvent(event) - toolCallEmitter.handleResults(event) + totalUsage = AdkUsage.add(totalUsage, event) + if (event.partial) event.usageMetadata?.let { partialUsage = it } val reasoning = event.textParts(thoughts = true) if (reasoning.isNotEmpty()) { @@ -232,49 +156,20 @@ class NativeAdkEngineModule(reactContext: ReactApplicationContext) : } val text = event.textParts(thoughts = false) - 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 - currentTurnReasoning = "" - emittedReasoning = "" - } - - 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) { - emitDelta(streamId, currentTurnText.substring(emittedText.length, safeEnd), "text") - emittedText = currentTurnText.substring(0, safeEnd) - } + if (currentTurnText.length > emittedText.length && + currentTurnText.startsWith(emittedText) + ) { + emitDelta(streamId, currentTurnText.substring(emittedText.length), "text") + emittedText = currentTurnText } 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) { - emitDelta(streamId, currentTurnText.substring(emittedText.length), "text") - emittedText = currentTurnText - } - - emitComplete(streamId, event, latestUsage) + emitComplete(streamId, event, totalUsage ?: partialUsage) } } } catch (error: Exception) { @@ -290,6 +185,7 @@ class NativeAdkEngineModule(reactContext: ReactApplicationContext) : } activeRuns[streamId] = job + job.start() promise.resolve(null) } @@ -303,12 +199,12 @@ class NativeAdkEngineModule(reactContext: ReactApplicationContext) : ) } - private fun emitComplete(streamId: String, event: Event, latestUsage: UsageMetadata?) { + private fun emitComplete(streamId: String, event: Event, usage: UsageMetadata?) { emitOnStreamComplete( Arguments.createMap().apply { putString("streamId", streamId) event.finishReason?.name?.let { putString("finishReason", it) } - AdkUsage.toWritableMap(latestUsage ?: event.usageMetadata)?.let { + AdkUsage.toWritableMap(usage)?.let { putMap("usage", it) } }, @@ -317,41 +213,17 @@ class NativeAdkEngineModule(reactContext: ReactApplicationContext) : override fun cancelStream(streamId: String, promise: Promise) { activeRuns.remove(streamId)?.cancel() - AdkToolBridge.cancelForStream(streamId) promise.resolve(null) } - override fun submitToolResult(toolCallId: String, result: String) { - AdkToolBridge.submitResult(toolCallId, result) - } - override fun invalidate() { activeRuns.values.forEach { it.cancel() } activeRuns.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/ReactNativeFunctionTool.kt b/packages/adk/android/src/main/java/com/callstack/ai/adk/ReactNativeFunctionTool.kt deleted file mode 100644 index 734c968f9..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/src/NativeAdkEngine.ts b/packages/adk/src/NativeAdkEngine.ts index 9c5fffc5a..62249194b 100644 --- a/packages/adk/src/NativeAdkEngine.ts +++ b/packages/adk/src/NativeAdkEngine.ts @@ -1,7 +1,9 @@ import type { CodegenTypes, TurboModule } from 'react-native' import { Platform, TurboModuleRegistry } from 'react-native' -export type AdkMessageRole = 'assistant' | 'system' | 'user' +import { AdkErrorCodes, createAdkError } from './errors' + +export type AdkMessageRole = 'assistant' | 'user' export type AdkMessagePart = | { type: 'text'; text: string } @@ -13,8 +15,6 @@ export interface AdkMessage { parts?: AdkMessagePart[] } -export type AdkModelType = 'gemini' | 'genai-nano' - export interface AdkNanoModelConfig { /** `stable` (default) or `preview`, which needs the AICore developer preview */ releaseStage?: 'stable' | 'preview' @@ -26,45 +26,13 @@ export interface AdkAgentConfig { name: string description?: string instruction?: string - model: { - type: AdkModelType - name: string - apiKey?: string - nano?: AdkNanoModelConfig - } -} - -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 + nano?: AdkNanoModelConfig } export interface AdkGenerationOptions { temperature?: number maxTokens?: number - topP?: number topK?: number - seed?: number - stopSequences?: string[] - presencePenalty?: number - frequencyPenalty?: number - responseFormat?: AdkResponseFormat /** Thinking on/off with an optional budget. Gemini Nano only supports on/off */ thinking?: { includeThoughts: boolean @@ -80,24 +48,10 @@ export interface AdkUsageMetadata { cachedContentTokenCount?: number } -export interface AdkToolCallRecord { - toolCallId: string - toolName: string - input: string -} - -export interface AdkToolResultRecord { - toolCallId: string - toolName: string - output: string -} - export interface AdkGeneratedMessage { role: AdkMessageRole content: string reasoning?: string - toolCalls?: AdkToolCallRecord[] - toolResults?: AdkToolResultRecord[] finishReason?: string usage?: AdkUsageMetadata } @@ -127,24 +81,6 @@ export interface StreamErrorEvent { code?: string } -export interface StreamToolCallEvent { - streamId: string - phase: 'start' | 'delta' | 'end' | 'result' - toolCallId: string - toolName?: string - inputDelta?: string - input?: string - output?: string -} - -export interface ToolCallEvent { - toolCallId: string - toolId: string - arguments: string - streamId?: string - runId?: string -} - export interface NanoDownloadProgressEvent { /** `started`, `progress` or `completed` */ phase: string @@ -153,15 +89,7 @@ export interface NanoDownloadProgressEvent { } export interface Spec extends TurboModule { - isAvailable( - modelType: AdkModelType, - nano?: AdkNanoModelConfig - ): Promise - isNanoSupported(nano?: AdkNanoModelConfig): Promise - getModelInfo( - modelType: AdkModelType, - nano?: AdkNanoModelConfig - ): Promise + getModelInfo(nano?: AdkNanoModelConfig): Promise prepareNano(nano?: AdkNanoModelConfig): Promise countTokens(text: string, nano?: AdkNanoModelConfig): Promise @@ -169,37 +97,25 @@ export interface Spec extends TurboModule { runId: string, messages: AdkMessage[], config: AdkAgentConfig, - options?: AdkGenerationOptions, - tools?: AdkTool[] + options?: AdkGenerationOptions ): Promise streamText( streamId: string, messages: AdkMessage[], config: AdkAgentConfig, - options?: AdkGenerationOptions, - tools?: AdkTool[] + options?: AdkGenerationOptions ): Promise - /** Cancels a stream or a `generateText` run by its id */ + /** Cancels a `streamText` or `generateText` run by its id */ 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 onNanoDownloadProgress: CodegenTypes.EventEmitter } -declare global { - var __ADK_TOOLS__: Record< - string, - (args: string) => unknown | Promise - > -} - const NativeAdkEngine: Spec | null = Platform.OS === 'android' ? TurboModuleRegistry.getEnforcing('AdkEngine') @@ -209,11 +125,10 @@ export default NativeAdkEngine export function getNativeAdkEngine(): Spec { if (!NativeAdkEngine) { - const error = new Error( - '@react-native-ai/adk is only available on Android. See https://developer.android.com/ai/adk' - ) as Error & { code: string } - error.code = 'UNSUPPORTED_PLATFORM' - throw error + throw createAdkError( + '@react-native-ai/adk is only available on Android. See https://developer.android.com/ai/adk', + AdkErrorCodes.UnsupportedPlatform + ) } return NativeAdkEngine } diff --git a/packages/adk/src/__tests__/ai-sdk.test.ts b/packages/adk/src/__tests__/ai-sdk.test.ts index 654eb6a6a..d1dec5b97 100644 --- a/packages/adk/src/__tests__/ai-sdk.test.ts +++ b/packages/adk/src/__tests__/ai-sdk.test.ts @@ -21,7 +21,6 @@ function createEmitter() { const onStreamUpdate = createEmitter() const onStreamComplete = createEmitter() const onStreamError = createEmitter() -const onStreamToolCall = createEmitter() const onNanoDownloadProgress = createEmitter() let generateTextResult: Record = { @@ -34,8 +33,7 @@ const generateText = mock( _runId: string, _messages: unknown[], _config: unknown, - _options: unknown, - _tools: unknown + _options: unknown ): Promise> => generateTextResult ) const streamText = mock( @@ -43,13 +41,12 @@ const streamText = mock( _streamId: string, _messages: unknown[], _config: unknown, - _options: unknown, - _tools: unknown + _options: unknown ) => {} ) const prepareNano = mock(async (_nano?: unknown) => {}) const cancelStream = mock(async (_id: string) => {}) -const getModelInfo = mock(async (_modelType: string, _nano?: unknown) => ({ +const getModelInfo = mock(async (_nano?: unknown) => ({ availability: 'downloadable', })) @@ -62,11 +59,7 @@ mock.module('react-native', () => ({ prepareNano, cancelStream, getModelInfo, - isNanoSupported: async () => true, - isAvailable: async () => true, - onToolCall: () => ({ remove: () => {} }), onStreamUpdate, - onStreamToolCall, onStreamComplete, onStreamError, onNanoDownloadProgress, @@ -75,8 +68,8 @@ mock.module('react-native', () => ({ })) const { createAdkProvider } = await import('../ai-sdk') +const { NoSuchModelError } = await import('@ai-sdk/provider') -const gemini = createAdkProvider({ modelType: 'gemini' })() const nano = createAdkProvider()() // PNG signature followed by padding bytes @@ -109,7 +102,7 @@ async function readAll(stream: ReadableStream) { async function runStream( options: LanguageModelV4CallOptions, play: (streamId: string) => void, - model = gemini + model = nano ) { streamText.mockImplementationOnce(async (streamId: string) => { queueMicrotask(() => play(streamId)) @@ -123,49 +116,86 @@ beforeEach(() => { streamText.mockClear() prepareNano.mockClear() cancelStream.mockClear() + getModelInfo.mockClear() generateTextResult = { content: 'ok', finishReason: 'STOP' } }) -describe('model resolution', () => { - test('defaults to on-device Gemini Nano', () => { +describe('provider', () => { + test('creates Gemini Nano models', () => { expect(nano.modelId).toBe('gemini-nano') + expect(nano.provider).toBe('adk') }) - test('defaults cloud Gemini to gemini-2.5-flash', () => { - expect(gemini.modelId).toBe('gemini-2.5-flash') + test('takes the gemini-nano model id', () => { + expect(createAdkProvider()('gemini-nano').modelId).toBe('gemini-nano') + expect( + createAdkProvider().languageModel({ model: 'gemini-nano' }).modelId + ).toBe('gemini-nano') }) - test('infers cloud Gemini from a model name', async () => { - const model = createAdkProvider()('gemini-2.5-pro') - await model.doGenerate(createCallOptions()) - - expect(lastGenerateCall()[2]).toMatchObject({ - model: { type: 'gemini', name: 'gemini-2.5-pro' }, - }) - }) + test('rejects other model ids', () => { + const provider = createAdkProvider() - test('per-model options override provider defaults', () => { - const provider = createAdkProvider({ instruction: 'Be brief.' }) - expect(provider({ modelName: 'gemini-2.5-pro' }).modelId).toBe( - 'gemini-2.5-pro' + expect(() => provider.languageModel('gemini-3.8-flash' as never)).toThrow( + NoSuchModelError + ) + expect(() => provider({ model: 'gemini-3.8-flash' as never })).toThrow( + NoSuchModelError ) - expect(provider.languageModel().modelId).toBe('gemini-nano') }) - test('reports model availability', async () => { + test('reports Gemini Nano availability', async () => { + getModelInfo.mockImplementationOnce(async () => ({ + availability: 'available', + baseModelName: 'nano-v3', + contextSize: 4096, + })) + expect(await createAdkProvider().getModelInfo()).toEqual({ - modelType: 'genai-nano', - modelName: 'gemini-nano', - availability: 'downloadable', - baseModelName: undefined, - contextSize: undefined, + availability: 'available', + baseModelName: 'nano-v3', + contextSize: 4096, }) }) + + test('passes the model selection to the availability check', async () => { + await createAdkProvider({ nano: { preference: 'fast' } }).getModelInfo() + + expect(getModelInfo.mock.calls.at(-1)![0]).toEqual({ preference: 'fast' }) + }) + + test('derives isAvailable from the model info', async () => { + const provider = createAdkProvider() + expect(await provider.isAvailable()).toBe(true) + + getModelInfo.mockImplementationOnce(async () => ({ + availability: 'downloading', + })) + expect(await provider.isAvailable()).toBe(false) + }) }) describe('prompt conversion', () => { + test('moves system messages into the agent instruction', async () => { + const model = createAdkProvider({ instruction: 'Be brief.' })() + await model.doGenerate( + createCallOptions({ + prompt: [ + { role: 'system', content: 'Answer in Polish.' }, + { role: 'user', content: [{ type: 'text', text: 'hello' }] }, + ], + }) + ) + + const [, messages, config] = lastGenerateCall() + expect(messages).toEqual([{ role: 'user', content: 'hello' }]) + expect(config).toMatchObject({ + instruction: 'Be brief.\n\nAnswer in Polish.', + }) + }) + test('resolves top-level file media types to a full mime type', async () => { - await gemini.doGenerate( + await nano.doGenerate( createCallOptions({ prompt: [ { @@ -199,7 +229,7 @@ describe('prompt conversion', () => { }) test('passes local image URLs to native code', async () => { - await gemini.doGenerate( + await nano.doGenerate( createCallOptions({ prompt: [ { @@ -226,8 +256,8 @@ describe('prompt conversion', () => { ]) }) - test('replays earlier tool steps as assistant text', async () => { - await gemini.doGenerate( + test('drops tool steps from the history with a warning', async () => { + const result = await nano.doGenerate( createCallOptions({ prompt: [ { role: 'user', content: [{ type: 'text', text: 'time?' }] }, @@ -238,13 +268,7 @@ describe('prompt conversion', () => { type: 'tool-call', toolCallId: '1', toolName: 'getTime', - input: { city: 'Warsaw' }, - }, - { - type: 'tool-result', - toolCallId: '1', - toolName: 'getTime', - output: { type: 'json', value: { time: '10:00' } }, + input: {}, }, { type: 'text', text: 'It is 10:00.' }, ], @@ -256,75 +280,17 @@ describe('prompt conversion', () => { expect(lastGenerateCall()[1][1]).toEqual({ role: 'assistant', - content: - '[Tool call: getTime({"city":"Warsaw"})]\n[Tool result for getTime: {"time":"10:00"}]\nIt is 10:00.', + content: 'It is 10:00.', }) - }) -}) - -const weatherTool = { - type: 'function' as const, - name: 'getWeather', - inputSchema: { - type: 'object' as const, - properties: { city: { type: 'string' } }, - required: ['city'], - }, -} - -describe('tool choice', () => { - test('sends no tools for toolChoice none', async () => { - await gemini.doGenerate( - createCallOptions({ tools: [weatherTool], toolChoice: { type: 'none' } }) - ) - - expect(lastGenerateCall()[4]).toEqual([]) - }) - - test('warns that required tool choice is unsupported', async () => { - const result = await gemini.doGenerate( - createCallOptions({ toolChoice: { type: 'required' } }) + expect(result.warnings).toContainEqual( + expect.objectContaining({ feature: 'assistant tool-call parts' }) ) - - expect(result.warnings).toEqual([ - { - type: 'unsupported', - feature: 'toolChoice', - details: - 'ADK runs tools natively and cannot be required to call a tool.', - }, - ]) - }) - - test('limits tools to the chosen tool', async () => { - await gemini.doGenerate( - createCallOptions({ - tools: [weatherTool, { ...weatherTool, name: 'getTime' }], - toolChoice: { type: 'tool', toolName: 'getTime' }, - }) - ) - - expect( - (lastGenerateCall()[4] as { name: string }[]).map((tool) => tool.name) - ).toEqual(['getTime']) - }) - - test('warns when a tool has no registered executor', async () => { - const result = await gemini.doGenerate( - createCallOptions({ tools: [weatherTool] }) - ) - - expect(result.warnings).toContainEqual({ - type: 'other', - message: - 'Tool "getWeather" has no executor. Pass it to createAdkProvider via availableTools so ADK can run it.', - }) }) }) describe('generation options', () => { test('maps reasoning levels to a thinking budget', async () => { - await gemini.doGenerate(createCallOptions({ reasoning: 'high' })) + await nano.doGenerate(createCallOptions({ reasoning: 'high' })) expect(lastGenerateCall()[3]).toMatchObject({ thinking: { includeThoughts: true, budget: 24576 }, @@ -332,7 +298,7 @@ describe('generation options', () => { }) test('disables thinking for reasoning none', async () => { - await gemini.doGenerate(createCallOptions({ reasoning: 'none' })) + await nano.doGenerate(createCallOptions({ reasoning: 'none' })) expect(lastGenerateCall()[3]).toMatchObject({ thinking: { includeThoughts: false, budget: 0 }, @@ -340,62 +306,74 @@ describe('generation options', () => { }) test('leaves thinking to the model by default', async () => { - await gemini.doGenerate(createCallOptions()) + await nano.doGenerate(createCallOptions()) expect((lastGenerateCall()[3] as { thinking?: unknown }).thinking).toBe( undefined ) }) - test('passes a response schema to cloud Gemini', async () => { - const schema = { type: 'object', properties: { name: { type: 'string' } } } - await gemini.doGenerate( - createCallOptions({ responseFormat: { type: 'json', schema } }) + test('passes the settings Gemini Nano takes', async () => { + await nano.doGenerate( + createCallOptions({ temperature: 0.2, maxOutputTokens: 50, topK: 3 }) ) - expect(lastGenerateCall()[3]).toMatchObject({ - responseFormat: { type: 'json', schema }, + expect(lastGenerateCall()[3]).toEqual({ + temperature: 0.2, + maxTokens: 50, + topK: 3, + thinking: undefined, }) }) - test('puts the response schema in the Gemini Nano instruction', async () => { - const schema = { type: 'object', properties: { name: { type: 'string' } } } - await nano.doGenerate( - createCallOptions({ responseFormat: { type: 'json', schema } }) + test('warns about settings Gemini Nano ignores', async () => { + const result = await nano.doGenerate( + createCallOptions({ topP: 0.5, seed: 1, stopSequences: ['END'] }) ) - const [, , config, options] = lastGenerateCall() as [ - string, - unknown, - { instruction: string }, - { responseFormat: { schema?: unknown } }, - ] - expect(config.instruction).toContain(JSON.stringify(schema)) - expect(options.responseFormat.schema).toBe(undefined) + expect( + result.warnings.map((warning) => 'feature' in warning && warning.feature) + ).toEqual(['topP', 'seed', 'stopSequences']) }) - test('warns about settings Gemini Nano ignores', async () => { + test('warns that JSON output is unsupported', async () => { const result = await nano.doGenerate( - createCallOptions({ topP: 0.5, stopSequences: ['END'] }) + createCallOptions({ responseFormat: { type: 'json' } }) ) - expect( - result.warnings.map((warning) => 'feature' in warning && warning.feature) - ).toEqual(['topP', 'stopSequences']) + expect(result.warnings).toContainEqual( + expect.objectContaining({ + type: 'unsupported', + feature: 'responseFormat', + }) + ) + }) + + test('warns that tools are unsupported', async () => { + const result = await nano.doGenerate( + createCallOptions({ + tools: [ + { + type: 'function', + name: 'getWeather', + inputSchema: { type: 'object', properties: {} }, + }, + ], + }) + ) + + expect(lastGenerateCall()).toHaveLength(4) + expect(result.warnings).toContainEqual( + expect.objectContaining({ type: 'unsupported', feature: 'tools' }) + ) }) }) describe('doGenerate results', () => { - test('returns reasoning, provider-executed tool steps and text', async () => { + test('returns reasoning, text and usage', async () => { generateTextResult = { content: 'It is sunny.', reasoning: 'Check the weather.', - toolCalls: [ - { toolCallId: '1', toolName: 'getWeather', input: '{"city":"Oslo"}' }, - ], - toolResults: [ - { toolCallId: '1', toolName: 'getWeather', output: '{"sky":"clear"}' }, - ], finishReason: 'STOP', usage: { promptTokenCount: 10, @@ -405,23 +383,10 @@ describe('doGenerate results', () => { }, } - const result = await gemini.doGenerate(createCallOptions()) + const result = await nano.doGenerate(createCallOptions()) expect(result.content).toEqual([ { type: 'reasoning', text: 'Check the weather.' }, - { - type: 'tool-call', - toolCallId: '1', - toolName: 'getWeather', - input: '{"city":"Oslo"}', - providerExecuted: true, - }, - { - type: 'tool-result', - toolCallId: '1', - toolName: 'getWeather', - result: { sky: 'clear' }, - }, { type: 'text', text: 'It is sunny.' }, ]) expect(result.usage.inputTokens).toEqual({ @@ -437,20 +402,10 @@ describe('doGenerate results', () => { }) }) - test('strips Markdown fences from JSON output', async () => { - generateTextResult = { content: '```json\n{"a":1}\n```' } - - const result = await nano.doGenerate( - createCallOptions({ responseFormat: { type: 'json' } }) - ) - - expect(result.content).toEqual([{ type: 'text', text: '{"a":1}' }]) - }) - test('maps safety finish reasons to content-filter', async () => { generateTextResult = { content: '', finishReason: 'SAFETY' } - const result = await gemini.doGenerate(createCallOptions()) + const result = await nano.doGenerate(createCallOptions()) expect(result.finishReason).toEqual({ unified: 'content-filter', @@ -463,7 +418,7 @@ describe('doGenerate results', () => { throw Object.assign(new Error('busy'), { code: 'RATE_LIMITED' }) }) - expect(gemini.doGenerate(createCallOptions())).rejects.toMatchObject({ + expect(nano.doGenerate(createCallOptions())).rejects.toMatchObject({ message: 'busy', code: 'RATE_LIMITED', }) @@ -476,7 +431,7 @@ describe('doGenerate results', () => { throw new Error('cancelled') }) - const error = await gemini + const error = await nano .doGenerate(createCallOptions({ abortSignal: controller.signal })) .catch((caught: unknown) => caught) @@ -486,29 +441,9 @@ describe('doGenerate results', () => { }) describe('doStream', () => { - test('streams reasoning, tool steps and text', async () => { + test('streams reasoning and text', async () => { const parts = await runStream(createCallOptions(), (streamId) => { onStreamUpdate.emit({ streamId, delta: 'Thinking', kind: 'reasoning' }) - onStreamToolCall.emit({ - streamId, - phase: 'start', - toolCallId: '1', - toolName: 'getWeather', - }) - onStreamToolCall.emit({ - streamId, - phase: 'end', - toolCallId: '1', - toolName: 'getWeather', - input: '{}', - }) - onStreamToolCall.emit({ - streamId, - phase: 'result', - toolCallId: '1', - toolName: 'getWeather', - output: '{"sky":"clear"}', - }) onStreamUpdate.emit({ streamId, delta: 'Sunny', kind: 'text' }) onStreamComplete.emit({ streamId, finishReason: 'STOP' }) }) @@ -518,39 +453,11 @@ describe('doStream', () => { 'reasoning-start', 'reasoning-delta', 'reasoning-end', - 'tool-input-start', - 'tool-input-end', - 'tool-call', - 'tool-result', 'text-start', 'text-delta', 'text-end', 'finish', ]) - expect(parts).toContainEqual({ - type: 'tool-result', - toolCallId: '1', - toolName: 'getWeather', - result: { sky: 'clear' }, - }) - }) - - test('streams JSON without Markdown fences', async () => { - const parts = await runStream( - createCallOptions({ responseFormat: { type: 'json' } }), - (streamId) => { - for (const delta of ['``', '`json\n{"a"', ':1}', '\n`', '``']) { - onStreamUpdate.emit({ streamId, delta }) - } - onStreamComplete.emit({ streamId }) - }, - nano - ) - - const text = parts - .flatMap((part) => (part.type === 'text-delta' ? [part.delta] : [])) - .join('') - expect(text).toBe('{"a":1}') }) test('surfaces native stream errors with their code', async () => { @@ -578,7 +485,7 @@ describe('Gemini Nano preparation', () => { onNanoDownloadProgress.emit({ phase: 'completed' }) }) - await provider.prepareNano({ onProgress: (event) => progress.push(event) }) + await provider().prepare({ onProgress: (event) => progress.push(event) }) expect(progress).toEqual([ { phase: 'started', bytesDownloaded: undefined, bytesToDownload: 100 }, diff --git a/packages/adk/src/ai-sdk.ts b/packages/adk/src/ai-sdk.ts index e0ea49dc1..66d440aeb 100644 --- a/packages/adk/src/ai-sdk.ts +++ b/packages/adk/src/ai-sdk.ts @@ -1,25 +1,16 @@ -import type { - JSONValue, - LanguageModelV4, - LanguageModelV4CallOptions, - LanguageModelV4Content, - LanguageModelV4FilePart, - LanguageModelV4FinishReason, - LanguageModelV4FunctionTool, - LanguageModelV4Prompt, - LanguageModelV4ProviderTool, - LanguageModelV4StreamPart, - LanguageModelV4Usage, - SharedV4Warning, -} from '@ai-sdk/provider' import { - generateId, - jsonSchema, - parseJSON, - resolveFullMediaType, - type Tool as FullToolDefinition, - type ToolExecutionOptions, -} from '@ai-sdk/provider-utils' + type LanguageModelV4, + type LanguageModelV4CallOptions, + type LanguageModelV4Content, + type LanguageModelV4FilePart, + type LanguageModelV4FinishReason, + type LanguageModelV4Prompt, + type LanguageModelV4StreamPart, + type LanguageModelV4Usage, + NoSuchModelError, + type SharedV4Warning, +} from '@ai-sdk/provider' +import { generateId, resolveFullMediaType } from '@ai-sdk/provider-utils' import { Platform } from 'react-native' import { AdkErrorCodes, createAdkError, toAdkError } from './errors' @@ -28,48 +19,32 @@ import { type AdkGenerationOptions, type AdkMessage, type AdkMessagePart, - type AdkModelType, type AdkNanoModelConfig, - type AdkTool, - type AdkToolParameter, type AdkUsageMetadata, getNativeAdkEngine, type NanoDownloadProgressEvent, - type StreamToolCallEvent, - type ToolCallEvent, } from './NativeAdkEngine' -const DEFAULT_NANO_MODEL = 'gemini-nano' -const DEFAULT_GEMINI_MODEL = 'gemini-2.5-flash' +const MODEL_ID = 'gemini-nano' -export type AdkToolDefinitionSet = Record - -/** `gemini-nano` runs on device, any other name is a cloud Gemini model */ -export type AdkLanguageModelId = 'gemini-nano' | (string & {}) +/** ADK runs Gemini Nano, the one model ML Kit provides on the device */ +export type AdkLanguageModelId = typeof MODEL_ID export interface AdkLanguageModelOptions { - /** Defaults to `genai-nano`, or `gemini` when `modelName` names a cloud model */ - modelType?: AdkModelType - /** Defaults to `gemini-nano` on device and `gemini-2.5-flash` in the cloud */ - modelName?: AdkLanguageModelId + /** The model to run, `gemini-nano` */ + model?: AdkLanguageModelId /** ADK agent name */ name?: string /** ADK agent description */ description?: string /** System instruction for the agent */ instruction?: string - /** Gemini API key, required for `gemini` */ - apiKey?: string - /** Tools whose `execute` handlers ADK can call */ - availableTools?: AdkToolDefinitionSet /** Gemini Nano model selection */ nano?: AdkNanoModelConfig } /** Per-call options passed as `providerOptions.adk` */ export interface AdkProviderOptions { - /** Thinking token budget for cloud Gemini. `0` disables thinking */ - thinkingBudget?: number /** Whether the model's thoughts come back as reasoning parts */ includeThoughts?: boolean } @@ -82,8 +57,6 @@ export type AdkModelAvailability = | 'unknown' export interface AdkModelInfo { - modelType: AdkModelType - modelName: string availability: AdkModelAvailability /** Name of the Gemini Nano base model, when ML Kit reports it */ baseModelName?: string @@ -102,10 +75,9 @@ export interface AdkPrepareOptions { } export interface AdkLanguageModel extends LanguageModelV4 { - /** Downloads and initializes Gemini Nano. No-op for cloud models */ - prepare: () => Promise - updateTools: (tools: AdkToolDefinitionSet) => void - /** Counts tokens with the Gemini Nano tokenizer. Not available for cloud models */ + /** Downloads Gemini Nano when needed and loads it, reporting download progress */ + prepare: (options?: AdkPrepareOptions) => Promise + /** Counts tokens with the Gemini Nano tokenizer */ countTokens: (text: string) => Promise } @@ -115,47 +87,12 @@ export interface AdkProvider { languageModel: ( options?: AdkLanguageModelId | AdkLanguageModelOptions ) => AdkLanguageModel - /** Whether the device can run Gemini Nano at all. Returns `false` on non-Android */ - isNanoSupported: () => Promise - /** - * Whether `modelType` can be used now. For `genai-nano`, Gemini Nano must be - * available or downloadable. Cloud `gemini` resolves to `true` on Android - */ - isAvailable: (modelType?: AdkModelType) => Promise - /** Availability details for a model type */ + /** Whether Gemini Nano can be used now: it is `available` or `downloadable` */ + isAvailable: () => Promise + /** Gemini Nano's availability, for the provider's model selection or `nano` */ getModelInfo: (options?: { - modelType?: AdkModelType nano?: AdkNanoModelConfig }) => Promise - /** Downloads and initializes Gemini Nano, reporting download progress */ - prepareNano: (options?: AdkPrepareOptions) => Promise -} - -type ResolvedModelOptions = Omit< - AdkLanguageModelOptions, - 'modelType' | 'modelName' -> & { - modelType: AdkModelType - modelName: string -} - -function resolveModelOptions( - defaults: AdkLanguageModelOptions, - options: AdkLanguageModelId | AdkLanguageModelOptions = {} -): ResolvedModelOptions { - const overrides = - typeof options === 'string' ? { modelName: options } : options - const merged = { ...defaults, ...overrides } - const modelType = - merged.modelType ?? - (merged.modelName && merged.modelName !== DEFAULT_NANO_MODEL - ? 'gemini' - : 'genai-nano') - const modelName = - merged.modelName ?? - (modelType === 'gemini' ? DEFAULT_GEMINI_MODEL : DEFAULT_NANO_MODEL) - - return { ...merged, modelType, modelName } } const toModelAvailability = (value: string): AdkModelAvailability => { @@ -170,27 +107,37 @@ const toModelAvailability = (value: string): AdkModelAvailability => { } } -async function prepareNano( - nano: AdkNanoModelConfig | undefined, - options: AdkPrepareOptions = {} -) { - const native = getNativeAdkEngine() - const listener = options.onProgress - ? native.onNanoDownloadProgress((event: NanoDownloadProgressEvent) => { - options.onProgress?.({ - phase: event.phase as AdkDownloadProgress['phase'], - bytesDownloaded: event.bytesDownloaded, - bytesToDownload: event.bytesToDownload, - }) - }) +const listenForDownloadProgress = ( + onProgress: AdkPrepareOptions['onProgress'] +) => + onProgress + ? getNativeAdkEngine().onNanoDownloadProgress( + (event: NanoDownloadProgressEvent) => { + onProgress({ + phase: event.phase as AdkDownloadProgress['phase'], + bytesDownloaded: event.bytesDownloaded, + bytesToDownload: event.bytesToDownload, + }) + } + ) : null - try { - await native.prepareNano(nano) - } catch (error) { - throw toAdkError(error) - } finally { - listener?.remove() +async function getModelInfo( + nano: AdkNanoModelConfig | undefined +): Promise { + if (Platform.OS !== 'android') { + return { availability: 'unavailable' } + } + + const info = await getNativeAdkEngine() + .getModelInfo(nano) + .catch((error: unknown) => { + throw toAdkError(error) + }) + return { + availability: toModelAvailability(info.availability), + baseModelName: info.baseModelName, + contextSize: info.contextSize, } } @@ -198,9 +145,21 @@ export function createAdkProvider( defaults: AdkLanguageModelOptions = {} ): AdkProvider { const createLanguageModel = ( - options?: AdkLanguageModelId | AdkLanguageModelOptions - ): AdkLanguageModel => - new AdkChatLanguageModel(resolveModelOptions(defaults, options)) + options: AdkLanguageModelId | AdkLanguageModelOptions = {} + ) => { + const modelOptions = + typeof options === 'string' ? { model: options } : options + const model = modelOptions.model ?? defaults.model ?? MODEL_ID + // The AI SDK passes any id, such as from a provider registry + if (model !== MODEL_ID) { + throw new NoSuchModelError({ + modelId: model, + modelType: 'languageModel', + message: `ADK only runs ${MODEL_ID} on the device, not ${model}.`, + }) + } + return new AdkChatLanguageModel({ ...defaults, ...modelOptions }) + } const provider = function ( options?: AdkLanguageModelId | AdkLanguageModelOptions @@ -209,81 +168,17 @@ export function createAdkProvider( } provider.specificationVersion = 'v4' as const provider.languageModel = createLanguageModel - provider.isNanoSupported = async () => { - if (Platform.OS !== 'android') { - return false - } - return getNativeAdkEngine().isNanoSupported(defaults.nano) - } - provider.isAvailable = async (modelType: AdkModelType = 'genai-nano') => { - if (Platform.OS !== 'android') { - return false - } - return getNativeAdkEngine() - .isAvailable(modelType, defaults.nano) - .then(Boolean) + provider.isAvailable = async () => { + const { availability } = await getModelInfo(defaults.nano) + return availability === 'available' || availability === 'downloadable' } - provider.getModelInfo = async ( - options: { modelType?: AdkModelType; nano?: AdkNanoModelConfig } = {} - ): Promise => { - const resolved = resolveModelOptions(defaults) - const { modelType, modelName } = - options.modelType && options.modelType !== resolved.modelType - ? resolveModelOptions({ modelType: options.modelType }) - : resolved - const nano = options.nano ?? defaults.nano - - if (Platform.OS !== 'android') { - return { modelType, modelName, availability: 'unavailable' } - } - - const info = await getNativeAdkEngine() - .getModelInfo(modelType, nano) - .catch((error: unknown) => { - throw toAdkError(error) - }) - return { - modelType, - modelName, - availability: toModelAvailability(info.availability), - baseModelName: info.baseModelName, - contextSize: info.contextSize, - } - } - provider.prepareNano = (options?: AdkPrepareOptions) => - prepareNano(defaults.nano, options) + provider.getModelInfo = (options: { nano?: AdkNanoModelConfig } = {}) => + getModelInfo(options.nano ?? defaults.nano) return provider } export const adk = createAdkProvider() -type Tool = LanguageModelV4FunctionTool | LanguageModelV4ProviderTool - -const toolChoiceWarning: SharedV4Warning = { - type: 'unsupported', - feature: 'toolChoice', - details: 'ADK runs tools natively and cannot be required to call a tool.', -} - -function prepareToolChoice( - tools: Tool[] = [], - toolChoice: LanguageModelV4CallOptions['toolChoice'] -): { tools: Tool[]; warnings: SharedV4Warning[] } { - switch (toolChoice?.type) { - case 'none': - return { tools: [], warnings: [] } - case 'required': - return { tools, warnings: [toolChoiceWarning] } - case 'tool': - return { - tools: tools.filter((tool) => tool.name === toolChoice.toolName), - warnings: [toolChoiceWarning], - } - default: - return { tools, warnings: [] } - } -} - // Gemini thinking budgets for the AI SDK reasoning levels const thinkingBudgets = { minimal: 512, @@ -298,14 +193,6 @@ function prepareThinking( reasoning: LanguageModelV4CallOptions['reasoning'], adkOptions: AdkProviderOptions ): AdkGenerationOptions['thinking'] { - if (adkOptions.thinkingBudget !== undefined) { - return { - includeThoughts: - adkOptions.includeThoughts ?? adkOptions.thinkingBudget !== 0, - budget: adkOptions.thinkingBudget, - } - } - switch (reasoning) { case undefined: case 'provider-default': @@ -373,11 +260,6 @@ const convertFinishReason = ( unified = 'length' } else if (contentFilterReasons.has(finishReason)) { unified = 'content-filter' - } else if ( - finishReason === 'MALFORMED_FUNCTION_CALL' || - finishReason === 'UNEXPECTED_TOOL_CALL' - ) { - unified = 'error' } return { @@ -386,24 +268,6 @@ const convertFinishReason = ( } } -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 uint8ArrayToBase64 = (bytes: Uint8Array): string => { let binary = '' for (let index = 0; index < bytes.length; index++) { @@ -456,27 +320,6 @@ const getAbortReason = (signal: AbortSignal) => { return error } -const stringifyToolOutput = (value: unknown) => - typeof value === 'string' ? value : JSON.stringify(value ?? null) - -// Gemini Nano often wraps JSON in a Markdown code fence -const jsonFencePrefix = /^\s*```(?:json)?\s*\n?/ -const stripJsonFences = (text: string) => - text.replace(jsonFencePrefix, '').replace(/\s*```\s*$/, '') - -const parseToolResult = (output: string): NonNullable => { - try { - return (JSON.parse(output) as JSONValue) ?? {} - } catch { - return output - } -} - -const createNanoJsonInstruction = (schema: unknown) => - schema - ? `Respond only with JSON that matches this JSON schema, without Markdown or other text:\n${JSON.stringify(schema)}` - : 'Respond only with valid JSON, without Markdown or other text.' - class AdkChatLanguageModel implements AdkLanguageModel { readonly specificationVersion = 'v4' readonly supportedUrls = { @@ -484,85 +327,58 @@ class AdkChatLanguageModel implements AdkLanguageModel { } readonly provider = 'adk' - readonly modelId: string + readonly modelId = MODEL_ID private readonly agentConfig: AdkAgentConfig - private tools: AdkToolDefinitionSet = {} - private nanoPreparePromise: Promise | null = null + private preparePromise: Promise | null = null - constructor(options: ResolvedModelOptions) { - this.modelId = options.modelName + constructor(options: AdkLanguageModelOptions) { this.agentConfig = { name: options.name ?? 'react_native_adk_agent', description: options.description ?? 'React Native ADK agent', instruction: options.instruction, - model: { - type: options.modelType, - name: options.modelName, - apiKey: options.apiKey, - nano: options.nano, - }, + nano: options.nano, } - this.updateTools(options.availableTools ?? {}) - } - - private get isNano() { - return this.agentConfig.model.type === 'genai-nano' } - updateTools(tools: AdkToolDefinitionSet) { - this.tools = tools - } - - async prepare(): Promise { - await this.ensureNanoPrepared() + async prepare(options: AdkPrepareOptions = {}): Promise { + const listener = listenForDownloadProgress(options.onProgress) + try { + await this.ensurePrepared() + } finally { + listener?.remove() + } } async countTokens(text: string): Promise { - if (!this.isNano) { - throw createAdkError( - 'Token counting is only available for Gemini Nano.', - AdkErrorCodes.ModelUnavailable - ) - } - try { - return await getNativeAdkEngine().countTokens( - text, - this.agentConfig.model.nano - ) + return await getNativeAdkEngine().countTokens(text, this.agentConfig.nano) } catch (error) { throw toAdkError(error) } } - private ensureNanoPrepared(): Promise { - if (!this.isNano) { - return Promise.resolve() - } - - if (!this.nanoPreparePromise) { - this.nanoPreparePromise = prepareNano(this.agentConfig.model.nano).catch( - (error) => { - this.nanoPreparePromise = null - throw error - } - ) + private ensurePrepared(): Promise { + if (!this.preparePromise) { + this.preparePromise = getNativeAdkEngine() + .prepareNano(this.agentConfig.nano) + .catch((error: unknown) => { + this.preparePromise = null + throw toAdkError(error) + }) } - return this.nanoPreparePromise + return this.preparePromise } private prepareMessages( messages: LanguageModelV4Prompt, warnings: SharedV4Warning[] ): AdkMessage[] { - return messages.map((message): AdkMessage => { + // System messages become part of the agent instruction instead + return messages.flatMap((message): AdkMessage[] => { if (message.role === 'system') { - return { - role: 'system', - content: message.content, - } + return [] } if (message.role === 'user') { @@ -574,90 +390,55 @@ class AdkChatLanguageModel implements AdkLanguageModel { }) if (parts.length === 1 && parts[0]?.type === 'text') { - return { - role: 'user', - content: parts[0].text, - } + return [{ role: 'user', content: parts[0].text }] } - return { - role: 'user', - parts, - } + return [{ role: 'user', parts }] } if (message.role === 'assistant') { - // ADK replays history as text, so earlier tool steps become text too const content = message.content .flatMap((part) => { switch (part.type) { case 'text': return [part.text] - case 'tool-call': - return [ - `[Tool call: ${part.toolName}(${stringifyToolOutput(part.input)})]`, - ] - case 'tool-result': - return [ - `[Tool result for ${part.toolName}: ${stringifyToolOutput( - 'value' in part.output ? part.output.value : part.output - )}]`, - ] case 'reasoning': return [] default: warnings.push({ type: 'unsupported', feature: `assistant ${part.type} parts`, - details: 'ADK replays assistant messages as text only.', + details: 'Gemini Nano only takes text history.', }) return [] } }) .join('\n') - 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 stringifyToolOutput(part.output.value) - }) - .join('\n') - - return { - role: 'user', - content, - } + return [{ role: 'assistant', content }] } - throw createAdkError( - 'Unsupported message role for ADK provider', - AdkErrorCodes.InvalidMessage - ) + warnings.push({ + type: 'unsupported', + feature: 'tool messages', + details: "ADK's ML Kit model has no tool calling for Gemini Nano.", + }) + return [] }) } private prepareAgentConfig( options: LanguageModelV4CallOptions ): AdkAgentConfig { - // Gemini Nano has no response schema, so the schema goes in the instruction - if (!this.isNano || options.responseFormat?.type !== 'json') { + // ADK only takes a system prompt as the agent instruction + const systemPrompts = options.prompt.flatMap((message) => + message.role === 'system' ? [message.content] : [] + ) + if (systemPrompts.length === 0) { return this.agentConfig } - const instruction = [ - this.agentConfig.instruction, - createNanoJsonInstruction(options.responseFormat.schema), - ] + const instruction = [this.agentConfig.instruction, ...systemPrompts] .filter(Boolean) .join('\n\n') @@ -671,203 +452,52 @@ class AdkChatLanguageModel implements AdkLanguageModel { const adkOptions = (options.providerOptions?.adk ?? {}) as AdkProviderOptions - if (this.isNano) { - const unsupported = { - topP: options.topP, - stopSequences: options.stopSequences, - presencePenalty: options.presencePenalty, - frequencyPenalty: options.frequencyPenalty, - } - for (const [feature, value] of Object.entries(unsupported)) { - if (value !== undefined) { - warnings.push({ - type: 'unsupported', - feature, - details: 'Gemini Nano does not support this setting.', - }) - } - } - } - - return { - temperature: options.temperature, - maxTokens: options.maxOutputTokens, + // ADK's ML Kit model only passes on temperature, topK, maxOutputTokens + // and thinking, and has no tool calling or JSON mode + const unsupported = { topP: options.topP, - topK: options.topK, seed: options.seed, stopSequences: options.stopSequences, presencePenalty: options.presencePenalty, frequencyPenalty: options.frequencyPenalty, responseFormat: - options.responseFormat?.type === 'json' - ? { - type: 'json', - mimeType: 'application/json', - schema: this.isNano ? undefined : options.responseFormat.schema, - } - : undefined, - thinking: prepareThinking(options.reasoning, adkOptions), + options.responseFormat?.type === 'json' ? 'json' : undefined, + tools: options.tools?.length ? options.tools : undefined, } - } - - private prepareTools(tools: Tool[], warnings: SharedV4Warning[]) { - return tools - .filter((tool): tool is LanguageModelV4FunctionTool => { - if (tool.type === 'function') { - return true - } + for (const [feature, value] of Object.entries(unsupported)) { + if (value !== undefined) { warnings.push({ type: 'unsupported', - feature: `provider-defined tool ${tool.name}`, + feature, + details: 'Gemini Nano does not support this setting.', }) - return false - }) - .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) - - if (!this.tools[tool.name]?.execute) { - warnings.push({ - type: 'other', - message: `Tool "${tool.name}" has no executor. Pass it to createAdkProvider via availableTools so ADK can run it.`, - }) - } - - return { - native: { - id: generateId(), - name: tool.name, - description: tool.description ?? '', - inputSchema: tool.inputSchema, - parameters, - } satisfies AdkTool, - execute: async (modelInput: unknown) => { - const execute = this.tools[tool.name]?.execute - if (!execute) { - return { - error: `Tool "${tool.name}" is not available in this app.`, - } - } + } + } - const text = - typeof modelInput === 'string' - ? modelInput - : JSON.stringify(modelInput ?? {}) - const toolCallArguments = await parseJSON({ - text, - schema, - }) - const opts: ToolExecutionOptions = { - toolCallId: generateId(), - messages: [], - context: undefined, - } - return execute(toolCallArguments, opts) - }, - } - }) + return { + temperature: options.temperature, + maxTokens: options.maxOutputTokens, + topK: options.topK, + thinking: prepareThinking(options.reasoning, adkOptions), + } } private prepareCall(options: LanguageModelV4CallOptions) { - const { tools, warnings } = prepareToolChoice( - options.tools, - options.toolChoice - ) + const warnings: SharedV4Warning[] = [] const messages = this.prepareMessages(options.prompt, warnings) const generationOptions = this.prepareGenerationOptions(options, warnings) - const preparedTools = this.prepareTools(tools, warnings) return { messages, generationOptions, - preparedTools, - nativeTools: preparedTools.map((tool) => tool.native), - ownedToolIds: new Set(preparedTools.map((tool) => tool.native.id)), agentConfig: this.prepareAgentConfig(options), warnings, } } - 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, - generationOptions, - preparedTools, - nativeTools, - ownedToolIds, - agentConfig, - warnings, - } = this.prepareCall(options) + const { messages, generationOptions, agentConfig, warnings } = + this.prepareCall(options) const runId = generateId() const native = getNativeAdkEngine() const abortSignal = options.abortSignal @@ -881,50 +511,23 @@ class AdkChatLanguageModel implements AdkLanguageModel { .cancelStream(runId) .catch((error) => console.warn('[adk] cancel failed:', error)) } - - this.registerTools(preparedTools) - const toolListener = this.createToolCallListener({ runId, ownedToolIds }) abortSignal?.addEventListener('abort', onAbort, { once: true }) try { - await this.ensureNanoPrepared() + await this.ensurePrepared() const response = await native.generateText( runId, messages, agentConfig, - generationOptions, - nativeTools + generationOptions ) const content: LanguageModelV4Content[] = [] if (response.reasoning) { content.push({ type: 'reasoning', text: response.reasoning }) } - for (const toolCall of response.toolCalls ?? []) { - content.push({ - type: 'tool-call', - toolCallId: toolCall.toolCallId, - toolName: toolCall.toolName, - input: toolCall.input, - providerExecuted: true, - }) - } - for (const toolResult of response.toolResults ?? []) { - content.push({ - type: 'tool-result', - toolCallId: toolResult.toolCallId, - toolName: toolResult.toolName, - result: parseToolResult(toolResult.output), - }) - } - content.push({ - type: 'text', - text: - options.responseFormat?.type === 'json' - ? stripJsonFences(response.content) - : response.content, - }) + content.push({ type: 'text', text: response.content }) return { content, @@ -939,25 +542,15 @@ class AdkChatLanguageModel implements AdkLanguageModel { throw toAdkError(error) } finally { abortSignal?.removeEventListener('abort', onAbort) - toolListener.remove() - this.unregisterTools(preparedTools) } } async doStream(options: LanguageModelV4CallOptions) { - const { - messages, - generationOptions, - preparedTools, - nativeTools, - ownedToolIds, - agentConfig, - warnings, - } = this.prepareCall(options) + const { messages, generationOptions, agentConfig, warnings } = + this.prepareCall(options) const streamId = generateId() const native = getNativeAdkEngine() const abortSignal = options.abortSignal - const isJson = options.responseFormat?.type === 'json' if (typeof ReadableStream === 'undefined') { throw new Error( @@ -969,12 +562,8 @@ class AdkChatLanguageModel implements AdkLanguageModel { throw getAbortReason(abortSignal) } - this.registerTools(preparedTools) - let listeners: { remove(): void }[] = [] let cleanedUp = false - const toolListener = this.createToolCallListener({ streamId, ownedToolIds }) - const toolNames = new Map() let onAbort = () => {} @@ -988,8 +577,6 @@ class AdkChatLanguageModel implements AdkLanguageModel { }) listeners = [] abortSignal?.removeEventListener('abort', onAbort) - toolListener.remove() - this.unregisterTools(preparedTools) } const cancelNative = () => { @@ -1004,9 +591,6 @@ class AdkChatLanguageModel implements AdkLanguageModel { let textOpen = false let reasoningId: string | undefined let reasoningCount = 0 - // JSON mode keeps the full text so code fences can be stripped - let rawText = '' - let emittedText = '' const addListener = (listener: { remove(): void }) => { listeners.push(listener) @@ -1031,21 +615,6 @@ class AdkChatLanguageModel implements AdkLanguageModel { controller.enqueue({ type: 'text-delta', id: textId, delta }) } - const emitJsonText = (final: boolean) => { - const stripped = final - ? stripJsonFences(rawText) - : // Hold back trailing backticks that could start a closing fence - rawText.replace(jsonFencePrefix, '').replace(/\s*`{1,3}$/, '') - // Only emit once the opening fence can no longer change the prefix - if (!final && /^\s*`{0,3}(j(s(o(n)?)?)?)?$/.test(rawText)) { - return - } - if (stripped.startsWith(emittedText)) { - emitText(stripped.slice(emittedText.length)) - emittedText = stripped - } - } - controller.enqueue({ type: 'stream-start', warnings }) onAbort = () => { @@ -1074,78 +643,15 @@ class AdkChatLanguageModel implements AdkLanguageModel { return } - if (isJson) { - rawText += data.delta - emitJsonText(false) - return - } - emitText(data.delta) }) ) - addListener( - native.onStreamToolCall((data: StreamToolCallEvent) => { - if (data.streamId !== streamId) { - return - } - - if (data.phase === 'start' && data.toolName) { - closeReasoning() - toolNames.set(data.toolCallId, 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, - }) - return - } - - if (data.phase === 'result' && data.output !== undefined) { - controller.enqueue({ - type: 'tool-result', - toolCallId: data.toolCallId, - toolName: - data.toolName ?? toolNames.get(data.toolCallId) ?? 'unknown', - result: parseToolResult(data.output), - }) - } - }) - ) - addListener( native.onStreamComplete((data) => { if (data.streamId !== streamId) { return } - if (isJson) { - emitJsonText(true) - } closeReasoning() if (textOpen) { controller.enqueue({ type: 'text-end', id: textId }) @@ -1174,14 +680,13 @@ class AdkChatLanguageModel implements AdkLanguageModel { ) try { - await this.ensureNanoPrepared() + await this.ensurePrepared() await native.streamText( streamId, messages, agentConfig, - generationOptions, - nativeTools + generationOptions ) } catch (error) { cleanup() diff --git a/packages/adk/src/errors.ts b/packages/adk/src/errors.ts index 7b76f30d7..45e4edd47 100644 --- a/packages/adk/src/errors.ts +++ b/packages/adk/src/errors.ts @@ -9,8 +9,6 @@ export const AdkErrorCodes = { DownloadFailed: 'DOWNLOAD_FAILED', GenerationError: 'GENERATION_ERROR', InvalidMessage: 'INVALID_MESSAGE', - InvalidSchema: 'INVALID_SCHEMA', - ToolCallError: 'TOOL_CALL_ERROR', ContextWindowExceeded: 'CONTEXT_WINDOW_EXCEEDED', RateLimited: 'RATE_LIMITED', BackgroundUseBlocked: 'BACKGROUND_USE_BLOCKED', diff --git a/packages/adk/src/index.ts b/packages/adk/src/index.ts index 64353ce0b..96f836661 100644 --- a/packages/adk/src/index.ts +++ b/packages/adk/src/index.ts @@ -8,7 +8,6 @@ export type { AdkPrepareOptions, AdkProvider, AdkProviderOptions, - AdkToolDefinitionSet, } from './ai-sdk' export { adk, createAdkProvider } from './ai-sdk' export type { AdkError, AdkErrorCode } from './errors' @@ -17,8 +16,6 @@ export { type AdkAgentConfig, default as AdkEngine, type AdkMessage, - type AdkModelType, type AdkNanoModelConfig, - type AdkTool, getNativeAdkEngine, } from './NativeAdkEngine' diff --git a/website/src/content/docs/docs/adk/generating.md b/website/src/content/docs/docs/adk/generating.md index a6539eaf0..103e465ac 100644 --- a/website/src/content/docs/docs/adk/generating.md +++ b/website/src/content/docs/docs/adk/generating.md @@ -3,10 +3,7 @@ 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. +`generateText` or `streamText`. ## Text Generation @@ -20,9 +17,11 @@ const { text } = await generateText({ }) ``` -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. +The first call prepares Gemini Nano. To avoid the wait, call +[`prepare()`](/docs/adk/models#preparing) ahead of time. + +System messages and the provider's `instruction` become the ADK agent's +instruction. ## Streaming @@ -55,50 +54,12 @@ const { usage } = await generateText({ 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`: +Ask about an image by passing it as a file part, as base64, a data URL, a +`Uint8Array`, or a `file://` or `content://` URL: ```typescript const { text } = await generateText({ @@ -115,28 +76,14 @@ const { text } = await generateText({ }) ``` -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. +HTTP URLs aren't supported, so download the image first. ## Options -`temperature`, `maxOutputTokens`, `topP` and `topK` work as usual: +`temperature`, `maxOutputTokens` and `topK` work as usual. Gemini Nano ignores +`topP`, `seed`, `stopSequences`, `presencePenalty` and `frequencyPenalty` with an +`unsupported` warning. Tools and JSON output return the same warning: ADK's ML +Kit model has no tool calling or structured output yet. ```typescript const { text } = await generateText({ @@ -144,11 +91,38 @@ const { text } = await generateText({ prompt: 'Write a creative story', temperature: 0.8, maxOutputTokens: 500, - topP: 0.9, topK: 40, }) ``` +## Errors + +Errors from the provider have a `code`: + +```typescript +import { AdkErrorCodes } from '@react-native-ai/adk' + +try { + await generateText({ model: adk(), prompt: 'Hello' }) +} catch (error) { + if (error.code === AdkErrorCodes.ModelDownloading) { + // Try again once Android has downloaded Gemini Nano + } +} +``` + +| `code` | Meaning | +| ------------------------- | -------------------------------------------------------- | +| `UNSUPPORTED_PLATFORM` | Not running on Android | +| `MODEL_UNAVAILABLE` | This device can't run Gemini Nano | +| `MODEL_DOWNLOADING` | Android is still downloading Gemini Nano | +| `DOWNLOAD_FAILED` | Downloading Gemini Nano failed | +| `CONTEXT_WINDOW_EXCEEDED` | The request is longer than the model's context | +| `RATE_LIMITED` | Too many requests, or AICore's per-app quota was reached | +| `BACKGROUND_USE_BLOCKED` | Gemini Nano only runs while the app is in the foreground | +| `INVALID_MESSAGE` | The prompt has a part the provider can't send | +| `GENERATION_ERROR` | Any other failure | + :::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 diff --git a/website/src/content/docs/docs/adk/getting-started.mdx b/website/src/content/docs/docs/adk/getting-started.mdx index b68d91df7..ffc2b0166 100644 --- a/website/src/content/docs/docs/adk/getting-started.mdx +++ b/website/src/content/docs/docs/adk/getting-started.mdx @@ -6,9 +6,7 @@ import PackageManagerTabs from '../../../../components/starlight/PackageManagerT 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). +It's built on [Google's Agent Development Kit](https://developer.android.com/ai/adk). ## Installation @@ -20,10 +18,9 @@ The AI SDK needs its [polyfills](/docs/polyfills). - 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 +- 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. + for the supported devices. ## Native Setup @@ -79,7 +76,7 @@ Pass `adk()` to the AI SDK. Check that Gemini Nano can run on the device first: import { adk } from '@react-native-ai/adk' import { generateText } from 'ai' -if (await adk.isAvailable('genai-nano')) { +if (await adk.isAvailable()) { const { text } = await generateText({ model: adk(), prompt: 'Summarize on-device AI in one sentence.', @@ -87,9 +84,9 @@ if (await adk.isAvailable('genai-nano')) { } ``` -[Models & Availability](/docs/adk/models) covers the availability checks and cloud Gemini. +[Models & Availability](/docs/adk/models) covers the availability checks. ## 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 +- [Generating](/docs/adk/generating): streaming, image input, token usage and errors +- [Models & Availability](/docs/adk/models): checking for Gemini Nano, preparing it and provider options diff --git a/website/src/content/docs/docs/adk/models.md b/website/src/content/docs/docs/adk/models.md index 3d32f5ee8..7a9acd081 100644 --- a/website/src/content/docs/docs/adk/models.md +++ b/website/src/content/docs/docs/adk/models.md @@ -3,86 +3,100 @@ 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. +there's nothing to bundle and no API key, but only some devices can run it. -| 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 | +## Availability -## 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? | +Check before you offer Gemini Nano in your app. `adk.getModelInfo()` reports +ML Kit's status for the model: ```typescript import { adk } from '@react-native-ai/adk' -if (!(await adk.isNanoSupported())) { - // Hide Gemini Nano: this device can't run it - return +const info = await adk.getModelInfo() + +switch (info.availability) { + case 'available': + // Ready to generate + break + case 'downloadable': + // Supported: prepare() downloads it + break + case 'downloading': + // Android is downloading it, show it as not ready yet + break + case 'unavailable': + case 'unknown': + // This device can't run Gemini Nano, hide it + break } - -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 +When Gemini Nano is `available`, `getModelInfo()` also returns `baseModelName` +and `contextSize`, its input token limit. + +`adk.isAvailable()` is the yes/no version: it's `true` when Gemini Nano is +`available` or `downloadable`. Both are cheap, never download anything, and are +safe to call at startup and again when the app resumes. See the [ML Kit GenAI documentation](https://developers.google.com/ml-kit/genai#feature-device) for the supported devices. -### Preparing +## 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. +`prepare()` downloads Gemini Nano when needed and loads it, so the first +request doesn't wait. It can report download progress: -## Cloud Gemini +```typescript +import { adk } from '@react-native-ai/adk' -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: +const model = adk() + +await model.prepare({ + onProgress: ({ phase, bytesDownloaded, bytesToDownload }) => { + // phase is 'started', 'progress' or 'completed' + }, +}) +``` + +If you skip it, the first `generateText` or `streamText` call prepares the +model. Preparing fails with `MODEL_UNAVAILABLE` on devices that can't run +Gemini Nano, and with `MODEL_DOWNLOADING` while Android is still downloading +it. + +## Model Selection + +ML Kit can run the preview Gemini Nano model, which needs the AICore developer +preview, and can favor speed or quality: ```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', + nano: { releaseStage: 'preview', preference: 'full' }, }) +``` -const { text } = await generateText({ - model: adk(), - prompt: 'What time is it in New York?', -}) +## Token Counting + +`countTokens` counts tokens with the Gemini Nano tokenizer, for example to +check a prompt against `contextSize`: + +```typescript +const tokens = await adk().countTokens('How long is this prompt?') ``` -Cloud models need no download or prepare step. +## What Gemini Nano Supports -:::caution -Don't ship API keys in your app. Use a backend proxy or load the key securely -at runtime. -::: +ADK runs Gemini Nano through ML Kit's Prompt API, which supports text and image +prompts, streaming, `temperature`, `topK` and `maxOutputTokens`. ADK's ML Kit +model has no tool calling or structured output yet: tools and JSON output +return an `unsupported` warning, and so do `topP`, `seed`, stop sequences and +penalties. ## Provider Options -`createAdkProvider` creates a provider with its own settings, for example a -system instruction: +`createAdkProvider` creates a provider with its own defaults, and `adk()` takes +the same options for a single model: ```typescript import { createAdkProvider } from '@react-native-ai/adk' @@ -90,16 +104,15 @@ 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 | +| Option | Type | Description | +| ------------- | -------- | -------------------------------------------------- | +| `instruction` | `string` | System instruction | +| `nano` | `object` | Gemini Nano [model selection](#model-selection) | +| `name` | `string` | ADK agent name (default: `react_native_adk_agent`) | +| `description` | `string` | ADK agent description | + +`adk()` also takes the model id, `adk('gemini-nano')`, as AI SDK providers do. +Any other id throws the AI SDK's `NoSuchModelError`, since ADK only runs Gemini +Nano.