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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import androidx.appfunctions.ExecuteAppFunctionRequest
import androidx.appfunctions.ExecuteAppFunctionResponse
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.chatapp.appfunctions.ChatAppFunctionService
import com.example.chatapp.data.MessageRepository
import com.example.chatapp.data.RecipientsRepository
import com.google.common.truth.Truth.assertThat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,16 @@ fun MessageBubble(
}
}
if (message.content.isNotEmpty()) {
val linkColor = if (message.isInbound) {
MaterialTheme.colorScheme.primary
} else {
MaterialTheme.colorScheme.inversePrimary
}
val annotatedText = remember(message.content, linkColor) {
linkifyString(message.content, linkColor = linkColor)
}
val linkColor =
if (message.isInbound) {
MaterialTheme.colorScheme.primary
} else {
MaterialTheme.colorScheme.inversePrimary
}
val annotatedText =
remember(message.content, linkColor) {
linkifyString(message.content, linkColor = linkColor)
}
Text(
modifier = Modifier.padding(16.dp),
text = annotatedText,
Expand Down
2 changes: 1 addition & 1 deletion ChatApp/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ composeMaterialIconsAndroid = "1.11.1"
composeMaterialIconsCore = "1.7.8"
hilt = "2.59.2"
androidxHiltNavigationCompose = "1.3.0"
ksp = "2.3.6"
ksp = "2.3.9"
spotless = "8.5.0"
truth = "1.4.5"
coil = "2.7.0"
Expand Down
1 change: 0 additions & 1 deletion ChatApp/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,4 @@ dependencies {
// App functions
implementation(libs.androidx.appfunctions)
ksp(libs.androidx.appfunctions.compiler)

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
package com.example.chatapp

import android.app.Application

abstract class BaseChatApplication : Application()
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ import android.app.PendingIntent
import android.content.Intent
import android.net.Uri
import androidx.annotation.RequiresApi
import androidx.appfunctions.AppFunction
import androidx.appfunctions.AppFunctionAppUnknownException
import androidx.appfunctions.AppFunctionElementNotFoundException
import androidx.appfunctions.AppFunctionInvalidArgumentException
import androidx.appfunctions.AppFunctionService
import androidx.appfunctions.AppFunctionServiceEntryPoint
import androidx.appfunctions.AppFunctionStringValueConstraint
import androidx.appfunctions.AppFunction
import com.example.chatapp.data.CallManager
import com.example.chatapp.data.MessageRepository
import com.example.chatapp.data.RecipientsRepository
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import kotlinx.coroutines.CancellationException
import javax.inject.Inject

/**
* Service entry point for chat-related AppFunctions such as searching contacts, sending messages, and making calls.
Expand All @@ -44,7 +44,9 @@ import kotlinx.coroutines.CancellationException
)
abstract class BaseChatAppFunctionService : AppFunctionService() {
@Inject lateinit var messageRepository: MessageRepository

@Inject lateinit var recipientsRepository: RecipientsRepository

@Inject lateinit var callManager: CallManager

/**
Expand Down Expand Up @@ -153,9 +155,7 @@ abstract class BaseChatAppFunctionService : AppFunctionService() {
* @throws AppFunctionElementNotFoundException If no recipient exists for endpointValue. If thrown, call "searchContacts" to find the correct ID.
*/
@AppFunction(isDescribedByKDoc = true)
suspend fun makeCall(
endpointValue: String,
): PendingIntent {
suspend fun makeCall(endpointValue: String): PendingIntent {
val recipient =
recipientsRepository.getRecipientById(endpointValue)
?: throw AppFunctionElementNotFoundException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.chatapp.appfunctions

import androidx.appfunctions.AppFunctionSerializable


/**
* Represents a result from a contact or group search.
*/
Expand Down Expand Up @@ -67,4 +65,4 @@ data class ChatGroup(
val name: String,
/** List of members belonging to the group. */
val recipients: List<Recipient>,
)
)
31 changes: 18 additions & 13 deletions ChatApp/shared/src/main/kotlin/com/example/chatapp/util/Linkify.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import androidx.compose.ui.text.TextLinkStyles
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextDecoration

fun linkifyString(text: String, linkColor: Color? = null): AnnotatedString {
fun linkifyString(
text: String,
linkColor: Color? = null,
): AnnotatedString {
val matcher = Patterns.WEB_URL.matcher(text)
var lastIndex = 0
return buildAnnotatedString {
Expand All @@ -35,22 +38,24 @@ fun linkifyString(text: String, linkColor: Color? = null): AnnotatedString {

append(text.substring(lastIndex, start))

val uriStr = if (url.startsWith("http://", ignoreCase = true) || url.startsWith("https://", ignoreCase = true)) {
url
} else {
"https://$url"
}

val linkStyle = SpanStyle(
color = linkColor ?: Color.Unspecified,
textDecoration = TextDecoration.Underline
)
val uriStr =
if (url.startsWith("http://", ignoreCase = true) || url.startsWith("https://", ignoreCase = true)) {
url
} else {
"https://$url"
}

val linkStyle =
SpanStyle(
color = linkColor ?: Color.Unspecified,
textDecoration = TextDecoration.Underline,
)

pushLink(
LinkAnnotation.Url(
url = uriStr,
styles = TextLinkStyles(style = linkStyle)
)
styles = TextLinkStyles(style = linkStyle),
),
)
append(url)
pop()
Expand Down
1 change: 0 additions & 1 deletion ChatApp/wear/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ dependencies {
// App functions
implementation(libs.androidx.appfunctions)
ksp(libs.androidx.appfunctions.compiler)

}

// AppFunctions ksp option
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ fun WearChatScreen(
title = { Text(text = if (message.isInbound) message.senderName ?: "Sender" else "Me") },
) {
val linkColor = MaterialTheme.colorScheme.primary
val annotatedText = remember(message.content, linkColor) {
linkifyString(text = message.content, linkColor = linkColor)
}
val annotatedText =
remember(message.content, linkColor) {
linkifyString(text = message.content, linkColor = linkColor)
}
Text(text = annotatedText)
}
}
Expand Down
2 changes: 1 addition & 1 deletion agent/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ android {

defaultConfig {
applicationId = "com.example.appfunctions.agent"
minSdk = 35
minSdk = 36
targetSdk = 37
versionCode = 1
versionName = "0.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import kotlinx.serialization.json.Json
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Assume.assumeTrue
import org.junit.Before
import org.junit.Test

Expand All @@ -58,7 +59,7 @@ abstract class LlmProviderIntegrationTestBase {
fun setUp() {
val arguments = androidx.test.platform.app.InstrumentationRegistry.getArguments()
apiKey = arguments.getString(apiKeyArgumentKey) ?: ""
assertTrue(
assumeTrue(
"API key is required. Please provide it via instrumentation arguments.",
apiKey.isNotEmpty() && apiKey != "PLACEHOLDER",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import android.content.pm.PackageManager
import android.location.Address
import android.location.Geocoder
import android.location.LocationManager
import androidx.annotation.RequiresApi
import androidx.appfunctions.AppFunction
import androidx.appfunctions.AppFunctionSerializable
import androidx.appfunctions.AppFunctionService
Expand All @@ -35,29 +34,27 @@ import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

/** Built-in AppFunctions for location and geocoding services. */
@RequiresApi(36)
@AndroidEntryPoint
@AppFunctionServiceEntryPoint(
serviceName = "BuiltInAppFunctionService",
appFunctionXmlFileName = "builtin_app_function_service",
)
abstract class BaseBuiltInAppFunctionService : AppFunctionService() {
abstract class AbstractBuiltInAppFunctions : AppFunctionService() {
/**
* Geocode a physical address string into its latitude and longitude coordinates.
* Geocodes a physical address string into its latitude and longitude coordinates.
*
* @param address The physical address to geocode (e.g., "1600 Amphitheatre Pkwy, Mountain View,
* CA").
* @return The latitude and longitude coordinates of the address, or null if geocoding fails.
*/
@AppFunction(isDescribedByKDoc = true)
suspend fun geocodeAddress(
address: String,
): LatLng? {
suspend fun geocodeAddress(address: String): LatLng? {
val context = this
if (!Geocoder.isPresent()) {
return null
}

val geocoder = Geocoder(this)
val geocoder = Geocoder(context)
Comment on lines +51 to +57

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The local variable context is redundant here. Since AbstractBuiltInAppFunctions extends AppFunctionService (which is a Context), this can be passed directly to the Geocoder constructor. Removing the intermediate context variable simplifies the code and reduces boilerplate.

Suggested change
suspend fun geocodeAddress(address: String): LatLng? {
val context = this
if (!Geocoder.isPresent()) {
return null
}
val geocoder = Geocoder(this)
val geocoder = Geocoder(context)
suspend fun geocodeAddress(address: String): LatLng? {
if (!Geocoder.isPresent()) {
return null
}
val geocoder = Geocoder(this)


return withContext(Dispatchers.IO) {
try {
Expand Down Expand Up @@ -90,7 +87,7 @@ abstract class BaseBuiltInAppFunctionService : AppFunctionService() {
}

/**
* Retrieve the current latitude and longitude coordinates of the device.
* Retrieves the current latitude and longitude coordinates of the device.
*
* @return The current location coordinates of the device, or null if location is unavailable or
* permission is denied.
Expand All @@ -99,25 +96,26 @@ abstract class BaseBuiltInAppFunctionService : AppFunctionService() {
@AppFunction(isDescribedByKDoc = true)
suspend fun getCurrentLocation(): LatLng? =
withContext(Dispatchers.Default) {
val context = this@AbstractBuiltInAppFunctions

// Check permissions
val hasFineLocation =
ContextCompat.checkSelfPermission(
this@BaseBuiltInAppFunctionService,
Manifest.permission.ACCESS_FINE_LOCATION,
) == PackageManager.PERMISSION_GRANTED
ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED

val hasCoarseLocation =
ContextCompat.checkSelfPermission(
this@BaseBuiltInAppFunctionService,
context,
Manifest.permission.ACCESS_COARSE_LOCATION,
) == PackageManager.PERMISSION_GRANTED
) ==
PackageManager.PERMISSION_GRANTED

if (!hasFineLocation && !hasCoarseLocation) {
throw IllegalStateException("Location permission is not granted")
}
Comment on lines 113 to 115

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Throwing a generic IllegalStateException when location permissions are missing prevents the AppFunction framework and the LLM agent from handling the error gracefully.

Instead, you should throw androidx.appfunctions.AppFunctionPermissionRequiredException. This allows the framework to recognize the missing permission and potentially prompt the user or return a structured error to the assistant.

Suggested change
if (!hasFineLocation && !hasCoarseLocation) {
throw IllegalStateException("Location permission is not granted")
}
if (!hasFineLocation && !hasCoarseLocation) {
throw androidx.appfunctions.AppFunctionPermissionRequiredException("Location permission is not granted")
}


val locationManager =
this@BaseBuiltInAppFunctionService.getSystemService(Context.LOCATION_SERVICE) as LocationManager
context.getSystemService(Context.LOCATION_SERVICE) as LocationManager

try {
// Try GPS Provider first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,27 @@
*/
package com.example.appfunctions.agent.data

import androidx.annotation.RequiresApi
import androidx.appfunctions.AppFunction
import androidx.appfunctions.AppFunctionSerializable
import androidx.appfunctions.AppFunctionService
import androidx.appfunctions.AppFunctionServiceEntryPoint
import dagger.hilt.android.AndroidEntryPoint

/** Fake AppFunction service used for testing and verification. */
@RequiresApi(36)
@AndroidEntryPoint
@AppFunctionServiceEntryPoint(
serviceName = "FakeAppFunctionService",
appFunctionXmlFileName = "fake_app_function_service",
)
abstract class BaseFakeAppFunctionService : AppFunctionService() {
abstract class AbstractFakeAppFunctionService : AppFunctionService() {
/**
* Execute a fake function for testing purposes.
*
* @param params The test parameters containing input data.
* @return The test response containing output data.
*/
@AppFunction(isDescribedByKDoc = true)
suspend fun fakeFunction(
params: FakeParams,
): FakeResponse {
suspend fun fakeFunction(params: FakeParams): FakeResponse {
return FakeResponse("success")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package com.example.appfunctions.agent.domain

import android.app.PendingIntent
import android.util.Log
import androidx.appfunctions.AppFunctionException
import androidx.appfunctions.metadata.AppFunctionMetadata
import com.example.appfunctions.agent.data.LlmProviderName
import com.example.appfunctions.agent.data.SettingsRepository
Expand All @@ -38,7 +37,6 @@ import com.example.appfunctions.agent.domain.chat.UpdateMessageUseCase
import com.example.appfunctions.agent.domain.chat.UpdateThreadParams
import com.example.appfunctions.agent.domain.chat.UpdateThreadUseCase
import com.example.appfunctions.agent.domain.pendingintent.SavePendingIntentUseCase
import java.util.UUID
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
Expand All @@ -50,6 +48,7 @@ import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.withContext
import java.util.UUID
import javax.inject.Inject
import javax.inject.Singleton

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ object AppFunctionExceptionFormatter {
/**
* Formats the given [exception] including its class name and message.
*/
fun format(exception: AppFunctionException, functionId: String? = null): String {
fun format(
exception: AppFunctionException,
functionId: String? = null,
): String {
val className = exception.javaClass.simpleName
val message = exception.errorMessage ?: exception.message ?: "No error message provided"
val prefix = if (functionId != null) "Tool execution failed for $functionId: " else ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.appfunctions.AppFunctionException
import com.example.appfunctions.agent.R
import com.example.appfunctions.agent.domain.appfunction.AppFunctionExceptionFormatter
import com.example.appfunctions.agent.domain.appfunction.ExecuteAppFunctionResult
Expand Down
Loading