### Check Generated File Patterns in generation.config.mjs Source: https://github.com/algolia/algoliasearch-client-kotlin/blob/main/AGENTS.md Example from generation.config.mjs showing patterns without '!' are generated and should not be edited. The comment explains that most directories are hand-written by default. ```javascript // In generation.config.mjs - patterns WITHOUT '!' are GENERATED (do not edit) 'clients/algoliasearch-client-kotlin/client/src/commonMain/kotlin/com/algolia/client/api/**', // Generated 'clients/algoliasearch-client-kotlin/client/src/commonMain/kotlin/com/algolia/client/model/**', // Generated // Most directories are hand-written by default (note the '!' pattern) ``` -------------------------------- ### Build & Test Commands (CLI and Gradle) Source: https://github.com/algolia/algoliasearch-client-kotlin/blob/main/AGENTS.md Commands to build, generate, run, and format the Kotlin client. Includes both CLI commands from the repo root and Gradle commands from the client directory. ```bash # From repo root (api-clients-automation) yarn cli build clients kotlin # Build Kotlin client yarn cli cts generate kotlin # Generate CTS tests yarn cli cts run kotlin # Run CTS tests yarn cli playground kotlin search # Interactive playground yarn cli format kotlin clients/algoliasearch-client-kotlin # From client directory cd clients/algoliasearch-client-kotlin ./gradlew build # Build all targets ./gradlew jvmTest # Run JVM tests ./gradlew spotlessApply # Apply formatting ``` -------------------------------- ### Platform Differences: expect/actual Declarations Source: https://github.com/algolia/algoliasearch-client-kotlin/blob/main/AGENTS.md Illustrates how to handle platform-specific APIs using Kotlin's expect/actual mechanism. Some APIs differ by platform; check the expect/actual declarations. ```kotlin // Some APIs differ by platform // Check expect/actual declarations expect fun platformSpecificFunction() // JVM implementation actual fun platformSpecificFunction() { /* JVM code */ } ``` -------------------------------- ### ClientOptions Data Class Source: https://github.com/algolia/algoliasearch-client-kotlin/blob/main/AGENTS.md Data class for client configuration with optional hosts, read/write timeouts, and custom headers. Defaults are 5 seconds read and 30 seconds write. ```kotlin // ClientOptions for client configuration data class ClientOptions( val hosts: List? = null, val readTimeout: Duration = 5.seconds, val writeTimeout: Duration = 30.seconds, val headers: Map = emptyMap(), ) ``` -------------------------------- ### Verify Git Remote Repository Source: https://github.com/algolia/algoliasearch-client-kotlin/blob/main/AGENTS.md Run this command to confirm you are in the correct repository before making changes. The expected output is 'origin .../algolia/api-clients-automation.git'. ```bash git remote -v ``` -------------------------------- ### Import BOM and define dependencies without versions (Groovy) Source: https://github.com/algolia/algoliasearch-client-kotlin/blob/main/client-bom/README.md Use this Groovy snippet in a Gradle build to import the Algolia Kotlin API client BOM and then declare the client and a runtime dependency without specifying versions. The BOM controls the versions of direct and transitive dependencies. ```groovy dependencies { // import Kotlin API client BOM implementation platform('com.algolia:algoliasearch-client-kotlin-bom:{{{{packageVersion}}}}') // define dependencies without versions implementation 'com.algolia:algoliasearch-client-kotlin' runtimeOnly 'io.ktor:ktor-client-okhttp' } ``` -------------------------------- ### Multiplatform Architecture Structure Source: https://github.com/algolia/algoliasearch-client-kotlin/blob/main/AGENTS.md Shows the directory structure for common, JVM, and JavaScript source sets in the Kotlin multiplatform client. Use this as a reference for where to place platform-specific code. ```kotlin // commonMain - shared code // jvmMain - JVM-specific // jsMain - JavaScript-specific (Node/Browser) // Platform-specific transport implementations ``` -------------------------------- ### Serialization: @Serializable and @SerialName Source: https://github.com/algolia/algoliasearch-client-kotlin/blob/main/AGENTS.md Shows how to define a serializable data model using Kotlinx.serialization, with @SerialName to map property names to JSON keys. ```kotlin // Uses Kotlinx.serialization @Serializable data class MyModel( @SerialName("objectID") val objectId: String, ) ``` -------------------------------- ### Coroutine Usage for Search Source: https://github.com/algolia/algoliasearch-client-kotlin/blob/main/AGENTS.md Shows that all API methods are suspend functions and demonstrates calling search from a coroutine scope. Use runBlocking for simple tests or launch from an existing scope. ```kotlin // All API methods are suspend functions suspend fun search(params: SearchParams): SearchResponse ``` ```kotlin // Use from coroutine scope runBlocking { val response = client.search(params) } ``` ```kotlin // Or with custom scope scope.launch { val response = client.search(params) } ``` -------------------------------- ### Add Algolia Kotlin client dependency to Gradle Source: https://github.com/algolia/algoliasearch-client-kotlin/blob/main/README.md Adds the Algolia Kotlin client to a Gradle build. Requires the mavenCentral() repository and a Ktor HTTP client engine dependency. The version placeholder must be replaced with the actual version. ```gradle repositories { mavenCentral() } dependencies { implementation "com.algolia:algoliasearch-client-kotlin:$version" } ``` -------------------------------- ### Nullability: Safe Calls and Smart Casts Source: https://github.com/algolia/algoliasearch-client-kotlin/blob/main/AGENTS.md Demonstrates Kotlin null safety with the Algolia client. Use the Elvis operator for defaults, safe calls for nullable access, and smart casts after null checks. ```kotlin // Kotlin null safety val hits = response.hits ?: emptyList() val first = response.hits?.firstOrNull() // Smart cast after null check if (response.hits != null) { response.hits.forEach { /* ... */ } } ``` -------------------------------- ### Data Classes: Immutable Updates with copy() Source: https://github.com/algolia/algoliasearch-client-kotlin/blob/main/AGENTS.md Shows how to update immutable data class instances using the copy function, preserving immutability. ```kotlin // Use copy for immutable updates val updated = params.copy(hitsPerPage = 20) ``` -------------------------------- ### Transport Layer Requester Class Source: https://github.com/algolia/algoliasearch-client-kotlin/blob/main/AGENTS.md Defines the internal Requester class that holds the HTTP client and retryable hosts. The retry logic is implemented as an extension function in Retry.kt. ```kotlin // client/src/commonMain/kotlin/com/algolia/client/transport/ internal class Requester( private val httpClient: HttpClient, private val hosts: List, ) ``` ```kotlin // Retry logic in extensions/internal/Retry.kt internal suspend fun Requester.retry(...) ``` -------------------------------- ### Suspend Functions: runBlocking and Coroutine Scope Source: https://github.com/algolia/algoliasearch-client-kotlin/blob/main/AGENTS.md Shows correct usage of suspend functions in Kotlin. Calling a suspend function outside a coroutine is a compile error; use runBlocking or a coroutine scope, or call from within another suspend function. ```kotlin // WRONG - can't call suspend outside coroutine val response = client.search(params) // CORRECT - use runBlocking or coroutine scope val response = runBlocking { client.search(params) } // Or in existing coroutine suspend fun myFunction() { val response = client.search(params) } ```