Skip to content

Commit

Permalink
chore(internal): update formatter (#220)
Browse files Browse the repository at this point in the history
chore(internal): optimize build and test perf
  • Loading branch information
stainless-app[bot] committed Feb 17, 2025
1 parent e5b8e55 commit fc6bcd7
Show file tree
Hide file tree
Showing 271 changed files with 975 additions and 1,926 deletions.
6 changes: 3 additions & 3 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
`kotlin-dsl`
kotlin("jvm") version "2.1.0"
kotlin("jvm") version "2.1.10"
id("com.vanniktech.maven.publish") version "0.28.0"
}

Expand All @@ -10,7 +10,7 @@ repositories {
}

dependencies {
implementation("com.diffplug.spotless:spotless-plugin-gradle:6.25.0")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.23")
implementation("com.diffplug.spotless:spotless-plugin-gradle:7.0.2")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:2.1.10")
implementation("com.vanniktech:gradle-maven-publish-plugin:0.28.0")
}
6 changes: 5 additions & 1 deletion buildSrc/src/main/kotlin/openai.java.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ tasks.named<Jar>("jar") {
}
}

tasks.named<Test>("test") {
tasks.withType<Test>().configureEach {
useJUnitPlatform()

// Run tests in parallel to some degree.
maxParallelForks = (Runtime.getRuntime().availableProcessors() / 2).coerceAtLeast(1)
forkEvery = 100

testLogging {
exceptionFormat = TestExceptionFormat.FULL
}
Expand Down
11 changes: 9 additions & 2 deletions buildSrc/src/main/kotlin/openai.kotlin.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import com.diffplug.gradle.spotless.SpotlessExtension
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
Expand All @@ -20,13 +21,19 @@ configure<SpotlessExtension> {
}

tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
compilerOptions {
freeCompilerArgs = listOf(
"-Xjvm-default=all",
"-Xjdk-release=1.8",
// Suppress deprecation warnings because we may still reference and test deprecated members.
"-Xsuppress-warning=DEPRECATION"
)
jvmTarget = "1.8"
jvmTarget.set(JvmTarget.JVM_1_8)
}
}

// Run tests in parallel to some degree.
tasks.withType<Test>().configureEach {
maxParallelForks = (Runtime.getRuntime().availableProcessors() / 2).coerceAtLeast(1)
forkEvery = 100
}
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
org.gradle.configuration-cache=true
org.gradle.caching=true
org.gradle.jvmargs=-Xmx4g
org.gradle.parallel=true
org.gradle.daemon=false
org.gradle.jvmargs=-Xmx4g
kotlin.daemon.jvmargs=-Xmx4g
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ class OkHttpClient
private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val baseUrl: HttpUrl) :
HttpClient {

override fun execute(
request: HttpRequest,
requestOptions: RequestOptions,
): HttpResponse {
override fun execute(request: HttpRequest, requestOptions: RequestOptions): HttpResponse {
val call = newCall(request, requestOptions)

return try {
Expand Down Expand Up @@ -120,13 +117,13 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
) {
builder.header(
"X-Stainless-Read-Timeout",
Duration.ofMillis(client.readTimeoutMillis.toLong()).seconds.toString()
Duration.ofMillis(client.readTimeoutMillis.toLong()).seconds.toString(),
)
}
if (!headers.names().contains("X-Stainless-Timeout") && client.callTimeoutMillis != 0) {
builder.header(
"X-Stainless-Timeout",
Duration.ofMillis(client.callTimeoutMillis.toLong()).seconds.toString()
Duration.ofMillis(client.callTimeoutMillis.toLong()).seconds.toString(),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.openai.credential.BearerTokenCredential
@JvmSynthetic
internal fun HttpRequest.Builder.addPathSegmentsForAzure(
clientOptions: ClientOptions,
deploymentModel: String?
deploymentModel: String?,
): HttpRequest.Builder = apply {
if (isAzureEndpoint(clientOptions.baseUrl) && deploymentModel != null) {
addPathSegments("openai", "deployments", deploymentModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ import com.openai.services.async.ModerationServiceAsyncImpl
import com.openai.services.async.UploadServiceAsync
import com.openai.services.async.UploadServiceAsyncImpl

class OpenAIClientAsyncImpl(
private val clientOptions: ClientOptions,
) : OpenAIClientAsync {
class OpenAIClientAsyncImpl(private val clientOptions: ClientOptions) : OpenAIClientAsync {

private val clientOptionsWithUserAgent =
if (clientOptions.headers.names().contains("User-Agent")) clientOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ import com.openai.services.blocking.ModerationServiceImpl
import com.openai.services.blocking.UploadService
import com.openai.services.blocking.UploadServiceImpl

class OpenAIClientImpl(
private val clientOptions: ClientOptions,
) : OpenAIClient {
class OpenAIClientImpl(private val clientOptions: ClientOptions) : OpenAIClient {

private val clientOptionsWithUserAgent =
if (clientOptions.headers.names().contains("User-Agent")) clientOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class BaseDeserializer<T : Any>(type: KClass<T>) :

override fun createContextual(
context: DeserializationContext,
property: BeanProperty?
property: BeanProperty?,
): JsonDeserializer<T> {
return this
}
Expand All @@ -32,7 +32,7 @@ abstract class BaseDeserializer<T : Any>(type: KClass<T>) :
protected fun <T> ObjectCodec.tryDeserialize(
node: JsonNode,
type: TypeReference<T>,
validate: (T) -> Unit = {}
validate: (T) -> Unit = {},
): T? {
return try {
readValue(treeAsTokens(node), type).apply(validate)
Expand All @@ -46,7 +46,7 @@ abstract class BaseDeserializer<T : Any>(type: KClass<T>) :
protected fun <T> ObjectCodec.tryDeserialize(
node: JsonNode,
type: JavaType,
validate: (T) -> Unit = {}
validate: (T) -> Unit = {},
): T? {
return try {
readValue<T>(treeAsTokens(node), type).apply(validate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import java.io.OutputStream
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder

@JvmSynthetic
internal inline fun <reified T> json(
jsonMapper: JsonMapper,
value: T,
): HttpRequestBody {
internal inline fun <reified T> json(jsonMapper: JsonMapper, value: T): HttpRequestBody {
return object : HttpRequestBody {
private var cachedBytes: ByteArray? = null

Expand Down Expand Up @@ -49,7 +46,7 @@ internal inline fun <reified T> json(
@JvmSynthetic
internal fun multipartFormData(
jsonMapper: JsonMapper,
parts: Array<MultipartFormValue<*>?>
parts: Array<MultipartFormValue<*>?>,
): HttpRequestBody {
val builder = MultipartEntityBuilder.create()
parts.forEach { part ->
Expand All @@ -66,14 +63,14 @@ internal fun multipartFormData(
part.name,
buffer.toByteArray(),
part.contentType,
part.filename
part.filename,
)
}
is Boolean ->
builder.addTextBody(
part.name,
if (part.value) "true" else "false",
part.contentType
part.contentType,
)
is Int -> builder.addTextBody(part.name, part.value.toString(), part.contentType)
is Long -> builder.addTextBody(part.name, part.value.toString(), part.contentType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.util.concurrent.CompletableFuture
internal fun HttpRequest.prepare(
clientOptions: ClientOptions,
params: Params,
deploymentModel: String?
deploymentModel: String?,
): HttpRequest =
toBuilder()
// Clear the path segments and add them back below after the Azure path segments.
Expand All @@ -29,7 +29,7 @@ internal fun HttpRequest.prepare(
internal fun HttpRequest.prepareAsync(
clientOptions: ClientOptions,
params: Params,
deploymentModel: String?
deploymentModel: String?,
): CompletableFuture<HttpRequest> =
// This async version exists to make it easier to add async specific preparation logic in the
// future.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ package com.openai.core

import java.time.Duration

class RequestOptions
private constructor(
val responseValidation: Boolean?,
val timeout: Duration?,
) {
class RequestOptions private constructor(val responseValidation: Boolean?, val timeout: Duration?) {
fun applyDefaults(options: RequestOptions): RequestOptions {
return RequestOptions(
responseValidation = this.responseValidation ?: options.responseValidation,
Expand Down
19 changes: 8 additions & 11 deletions openai-java-core/src/main/kotlin/com/openai/core/Values.kt
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ class JsonMissing : JsonValue() {
override fun serialize(
value: JsonMissing,
generator: JsonGenerator,
provider: SerializerProvider
provider: SerializerProvider,
) {
throw RuntimeException("JsonMissing cannot be serialized")
}
Expand Down Expand Up @@ -422,10 +422,7 @@ private constructor(
}

@JacksonAnnotationsInside
@JsonInclude(
JsonInclude.Include.CUSTOM,
valueFilter = JsonField.IsMissing::class,
)
@JsonInclude(JsonInclude.Include.CUSTOM, valueFilter = JsonField.IsMissing::class)
annotation class ExcludeMissing

@JacksonAnnotationsInside
Expand All @@ -434,7 +431,7 @@ annotation class ExcludeMissing
isGetterVisibility = Visibility.NONE,
setterVisibility = Visibility.NONE,
creatorVisibility = Visibility.NONE,
fieldVisibility = Visibility.NONE
fieldVisibility = Visibility.NONE,
)
annotation class NoAutoDetect

Expand All @@ -443,7 +440,7 @@ internal constructor(
val name: String,
val value: T,
val contentType: ContentType,
val filename: String? = null
val filename: String? = null,
) {

private var hashCode: Int = 0
Expand All @@ -462,7 +459,7 @@ internal constructor(
is Long -> value
is Double -> value
else -> value?.hashCode()
}
},
)
}
return hashCode
Expand Down Expand Up @@ -496,7 +493,7 @@ internal constructor(
internal fun fromString(
name: String,
value: String,
contentType: ContentType
contentType: ContentType,
): MultipartFormValue<String> = MultipartFormValue(name, value, contentType)

internal fun fromBoolean(
Expand All @@ -520,14 +517,14 @@ internal constructor(
internal fun <T : Enum> fromEnum(
name: String,
value: T,
contentType: ContentType
contentType: ContentType,
): MultipartFormValue<T> = MultipartFormValue(name, value, contentType)

internal fun fromByteArray(
name: String,
value: ByteArray,
contentType: ContentType,
filename: String? = null
filename: String? = null,
): MultipartFormValue<ByteArray> = MultipartFormValue(name, value, contentType, filename)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private class SseState(
var event: String? = null,
val data: MutableList<String> = mutableListOf(),
var lastId: String? = null,
var retry: Int? = null
var retry: Int? = null,
) {
// https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation
fun decode(line: String): SseMessage? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal fun <T> CompletableFuture<StreamResponse<T>>.toAsync(streamHandlerExecu

override fun subscribe(
handler: Handler<T>,
executor: Executor
executor: Executor,
): AsyncStreamResponse<T> = apply {
// TODO(JDK): Use `compareAndExchange` once targeting JDK 9.
check(state.compareAndSet(State.NEW, State.SUBSCRIBED)) {
Expand Down Expand Up @@ -75,7 +75,7 @@ internal fun <T> CompletableFuture<StreamResponse<T>>.toAsync(streamHandlerExecu
close()
}
},
executor
executor,
)
}

Expand All @@ -93,5 +93,5 @@ internal fun <T> CompletableFuture<StreamResponse<T>>.toAsync(streamHandlerExecu
private enum class State {
NEW,
SUBSCRIBED,
CLOSED
CLOSED,
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.util.TreeMap
class Headers
private constructor(
private val map: Map<String, List<String>>,
@get:JvmName("size") val size: Int
@get:JvmName("size") val size: Int,
) {

fun isEmpty(): Boolean = map.isEmpty()
Expand Down Expand Up @@ -74,7 +74,7 @@ private constructor(
values.toImmutable()
}
.toImmutable(),
size
size,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class PhantomReachableClosingHttpClient(private val httpClient: HttpCli

override fun executeAsync(
request: HttpRequest,
requestOptions: RequestOptions
requestOptions: RequestOptions,
): CompletableFuture<HttpResponse> = httpClient.executeAsync(request, requestOptions)

override fun close() = httpClient.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.openai.core.toImmutable
class QueryParams
private constructor(
private val map: Map<String, List<String>>,
@get:JvmName("size") val size: Int
@get:JvmName("size") val size: Int,
) {

fun isEmpty(): Boolean = map.isEmpty()
Expand Down
Loading

0 comments on commit fc6bcd7

Please sign in to comment.