suspend fun queryGemmaAgent() { val gemmaServiceUrl = System.getenv("GEMMA_SERVICE_URL") ?: "https://sjjuuy.run.app" // 1. Fetch fresh OIDC identity token for this invocation val oidcToken = fetchCloudRunIdToken(gemmaServiceUrl) // 2. Initialize HTTP Client val client = HttpClient(CIO) { install(ContentNegotiation) { json(Json { ignoreUnknownKeys = true }) } install(HttpTimeout) { requestTimeoutMillis = 30_000L // Total time for the request (30s) connectTimeoutMillis = 10_000L // Time to establish TCP connection (10s) socketTimeoutMillis = 30_000L // Max time between two data packets (30s) } } // 3. Construct the payload val payload = ChatRequest( model = "google/gemma-4-E4B-it", messages = listOf( ChatMessage(role = "system", content = "You are a Kotlin-based automation agent."), ChatMessage(role = "user", content = "What is the optimal concurrency strategy for Ktor coroutines?") ) ) // 4. Send authenticated POST request to the inference server val response: HttpResponse = client.post("$gemmaServiceUrl/v1/chat/completions") { contentType(ContentType.Application.Json) // Pass the OIDC token into the header to clear Cloud Run IAM security header(HttpHeaders.Authorization, "Bearer $oidcToken") setBody(payload) } println("Gemma 4 Response: ${response.bodyAsText()}") client.close() }