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
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
root = true

[*.{kt,kts}]
ktlint_code_style = ktlint_official
ktlint_function_naming_ignore_when_annotated_with = Composable
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: 2 additions & 0 deletions ChatApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ fun SpotlessExtension.spotlessConfiguration() {
target("src/**/*.kt")
targetExclude("**/build/**/*.kt")
ktlint(libs.versions.ktlint.get())
.setEditorConfigPath("${rootProject.projectDir.parentFile}/.editorconfig")
licenseHeaderFile(rootProject.file("spotless/copyright.kt"))
}
kotlinGradle {
target("*.kts")
targetExclude("**/build/**/*.kts")
ktlint(libs.versions.ktlint.get())
.setEditorConfigPath("${rootProject.projectDir.parentFile}/.editorconfig")
licenseHeaderFile(rootProject.file("spotless/copyright.kt"), "(^(?![\\\\/ ]\\\\*).*$)")
}
}
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Follow these steps to explore and run the samples:
We'd love to accept your patches and contributions!

- **Contributor License Agreement (CLA)**: All contributors must sign the [Google CLA](https://cla.developers.google.com/).
- **Code Style**: We use [Spotless](https://github.com/diffplug/spotless) to maintain consistent formatting. Run `./gradlew spotlessApply` before committing.
- **Code Style**: We use [Spotless](https://github.com/diffplug/spotless) to maintain consistent formatting. Because this repository contains separate Gradle projects for each sample, run `./agent/gradlew -p agent spotlessApply && ./ChatApp/gradlew -p ChatApp spotlessApply` from the repository root (or `./gradlew spotlessApply` inside a specific sample directory) before committing.
- **Testing**: Ensure all new functionality is covered by unit or instrumentation tests.
- **Reviews**: All submissions are reviewed via GitHub Pull Requests.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ abstract class BaseBuiltInAppFunctionService : AppFunctionService() {
* @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? {
if (!Geocoder.isPresent()) {
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ abstract class BaseFakeAppFunctionService : AppFunctionService() {
* @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
Original file line number Diff line number Diff line change
Expand Up @@ -282,17 +282,18 @@ class AgentOrchestratorTest {

coEvery {
llmProvider.generateResponse(null, any(), any(), any(), any())
} returns LlmResponse.Success(
"interaction_1",
listOf(
LlmResponsePart.ToolCall(
packageName = "com.example.calendar",
functionId = "create_event",
arguments = emptyMap(),
callId = "call_1",
)
} returns
LlmResponse.Success(
"interaction_1",
listOf(
LlmResponsePart.ToolCall(
packageName = "com.example.calendar",
functionId = "create_event",
arguments = emptyMap(),
callId = "call_1",
),
),
)
)

val expectedErrorOutput =
"Tool execution failed for create_event: Error: AppFunctionPermissionRequiredException - Calendar permission required"
Expand All @@ -305,17 +306,18 @@ class AgentOrchestratorTest {
coVerify {
llmProvider.generateResponse(
previousInteractionId = eq("interaction_1"),
input = eq(
LlmInput.ToolResponse(
listOf(
ToolOutput(
functionId = "create_event",
callId = "call_1",
result = expectedErrorOutput,
)
)
)
),
input =
eq(
LlmInput.ToolResponse(
listOf(
ToolOutput(
functionId = "create_event",
callId = "call_1",
result = expectedErrorOutput,
),
),
),
),
tools = listOf(tool1),
apiKey = "dummy_key",
modelName = any(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ class AppFunctionExceptionFormatterTest {
"com.example.chatapp.appfunctions.BaseChatAppFunctionService#send",
)
assertEquals(
"Tool execution failed for com.example.chatapp.appfunctions.BaseChatAppFunctionService#send: Error: AppFunctionInvalidArgumentException - Message body cannot be empty",
"Tool execution failed for com.example.chatapp.appfunctions.BaseChatAppFunctionService#send: " +
"Error: AppFunctionInvalidArgumentException - Message body cannot be empty",
formatted,
)
}
Expand Down
Loading