Skip to content

add ktlint and cleanup code #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
37 changes: 37 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
max_line_length = 120

[*.json]
indent_size = 2

[{*.yaml,*.yml}]
indent_size = 2

[*.{kt,kts}]
ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL

# Disable wildcard imports entirely
ij_kotlin_name_count_to_use_star_import = 2147483647
ij_kotlin_name_count_to_use_star_import_for_members = 2147483647
ij_kotlin_packages_to_use_import_on_demand = unset

ktlint_code_style = intellij_idea
ktlint_experimental = enabled
ktlint_standard_filename = disabled
ktlint_standard_no-empty-first-line-in-class-body = disabled
ktlint_class_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than = 4
ktlint_function_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than = 4
ktlint_standard_chain-method-continuation = disabled
ktlint_ignore_back_ticked_identifier = true
ktlint_standard_multiline-expression-wrapping = disabled
ktlint_standard_when-entry-bracing = disabled

[*/build/**/*]
ktlint = disabled
14 changes: 10 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ plugins {
alias(libs.plugins.kotlin.atomicfu)
alias(libs.plugins.dokka)
alias(libs.plugins.jreleaser)
alias(libs.plugins.ktlint)
`maven-publish`
signing
alias(libs.plugins.kotlinx.binary.compatibility.validator)
Expand Down Expand Up @@ -66,9 +67,9 @@ jreleaser {
if (publication is MavenPublication) {
val pubName = publication.name

if (!pubName.contains("jvm", ignoreCase = true)
&& !pubName.contains("metadata", ignoreCase = true)
&& !pubName.contains("kotlinMultiplatform", ignoreCase = true)
if (!pubName.contains("jvm", ignoreCase = true) &&
!pubName.contains("metadata", ignoreCase = true) &&
!pubName.contains("kotlinMultiplatform", ignoreCase = true)
) {

artifactOverride {
Expand Down Expand Up @@ -188,7 +189,7 @@ abstract class GenerateLibVersionTask @Inject constructor(

public const val LIB_VERSION: String = "$libVersion"

""".trimIndent()
""".trimIndent(),
)
}
}
Expand Down Expand Up @@ -276,3 +277,8 @@ kotlin {
}
}
}

ktlint {
verbose = true
outputToConsole = true
}
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
kotlin = "2.2.0"
dokka = "2.0.0"
atomicfu = "0.29.0"
ktlint = "13.0.0"

# libraries version
serialization = "1.9.0"
Expand Down Expand Up @@ -42,5 +43,6 @@ kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
kotlin-atomicfu = { id = "org.jetbrains.kotlinx.atomicfu", version.ref = "atomicfu" }
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
jreleaser = { id = "org.jreleaser", version.ref = "jreleaser"}
ktlint = { id = "org.jlleitschuh.gradle.ktlint", version.ref = "ktlint" }
jreleaser = { id = "org.jreleaser", version.ref = "jreleaser" }
kotlinx-binary-compatibility-validator = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "binaryCompatibilityValidatorPlugin" }
1 change: 0 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ dependencyResolutionManagement {
}

rootProject.name = "kotlin-sdk"

Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,8 @@ public class ClientOptions(
* @param clientInfo Information about the client implementation (name, version).
* @param options Configuration options for this client.
*/
public open class Client(
private val clientInfo: Implementation,
options: ClientOptions = ClientOptions(),
) : Protocol(options) {
public open class Client(private val clientInfo: Implementation, options: ClientOptions = ClientOptions()) :
Protocol(options) {

/**
* Retrieves the server's reported capabilities after the initialization process completes.
Expand Down Expand Up @@ -144,13 +142,13 @@ public open class Client(
val message = InitializeRequest(
protocolVersion = LATEST_PROTOCOL_VERSION,
capabilities = capabilities,
clientInfo = clientInfo
clientInfo = clientInfo,
)
val result = request<InitializeResult>(message)

if (!SUPPORTED_PROTOCOL_VERSIONS.contains(result.protocolVersion)) {
throw IllegalStateException(
"Server's protocol version is not supported: ${result.protocolVersion}"
"Server's protocol version is not supported: ${result.protocolVersion}",
)
}

Expand All @@ -165,11 +163,9 @@ public open class Client(
}

throw error

}
}


override fun assertCapabilityForMethod(method: Method) {
when (method) {
Method.Defined.LoggingSetLevel -> {
Expand All @@ -181,7 +177,7 @@ public open class Client(
Method.Defined.PromptsGet,
Method.Defined.PromptsList,
Method.Defined.CompletionComplete,
-> {
-> {
if (serverCapabilities?.prompts == null) {
throw IllegalStateException("Server does not support prompts (required for $method)")
}
Expand All @@ -192,28 +188,28 @@ public open class Client(
Method.Defined.ResourcesRead,
Method.Defined.ResourcesSubscribe,
Method.Defined.ResourcesUnsubscribe,
-> {
-> {
val resCaps = serverCapabilities?.resources
?: error("Server does not support resources (required for $method)")

if (method == Method.Defined.ResourcesSubscribe && resCaps.subscribe != true) {
throw IllegalStateException(
"Server does not support resource subscriptions (required for $method)"
"Server does not support resource subscriptions (required for $method)",
)
}
}

Method.Defined.ToolsCall,
Method.Defined.ToolsList,
-> {
-> {
if (serverCapabilities?.tools == null) {
throw IllegalStateException("Server does not support tools (required for $method)")
}
}

Method.Defined.Initialize,
Method.Defined.Ping,
-> {
-> {
// No specific capability required
}

Expand All @@ -228,15 +224,15 @@ public open class Client(
Method.Defined.NotificationsRootsListChanged -> {
if (capabilities.roots?.listChanged != true) {
throw IllegalStateException(
"Client does not support roots list changed notifications (required for $method)"
"Client does not support roots list changed notifications (required for $method)",
)
}
}

Method.Defined.NotificationsInitialized,
Method.Defined.NotificationsCancelled,
Method.Defined.NotificationsProgress,
-> {
-> {
// Always allowed
}

Expand All @@ -251,23 +247,23 @@ public open class Client(
Method.Defined.SamplingCreateMessage -> {
if (capabilities.sampling == null) {
throw IllegalStateException(
"Client does not support sampling capability (required for $method)"
"Client does not support sampling capability (required for $method)",
)
}
}

Method.Defined.RootsList -> {
if (capabilities.roots == null) {
throw IllegalStateException(
"Client does not support roots capability (required for $method)"
"Client does not support roots capability (required for $method)",
)
}
}

Method.Defined.ElicitationCreate -> {
if (capabilities.elicitation == null) {
throw IllegalStateException(
"Client does not support elicitation capability (required for $method)"
"Client does not support elicitation capability (required for $method)",
)
}
}
Expand All @@ -280,16 +276,13 @@ public open class Client(
}
}


/**
* Sends a ping request to the server to check connectivity.
*
* @param options Optional request options.
* @throws IllegalStateException If the server does not support the ping method (unlikely).
*/
public suspend fun ping(options: RequestOptions? = null): EmptyRequestResult {
return request<EmptyRequestResult>(PingRequest(), options)
}
public suspend fun ping(options: RequestOptions? = null): EmptyRequestResult = request(PingRequest(), options)

/**
* Sends a completion request to the server, typically to generate or complete some content.
Expand All @@ -299,9 +292,8 @@ public open class Client(
* @return The completion result returned by the server, or `null` if none.
* @throws IllegalStateException If the server does not support prompts or completion.
*/
public suspend fun complete(params: CompleteRequest, options: RequestOptions? = null): CompleteResult? {
return request<CompleteResult>(params, options)
}
public suspend fun complete(params: CompleteRequest, options: RequestOptions? = null): CompleteResult =
request(params, options)

/**
* Sets the logging level on the server.
Expand All @@ -310,9 +302,8 @@ public open class Client(
* @param options Optional request options.
* @throws IllegalStateException If the server does not support logging.
*/
public suspend fun setLoggingLevel(level: LoggingLevel, options: RequestOptions? = null): EmptyRequestResult {
return request<EmptyRequestResult>(SetLevelRequest(level), options)
}
public suspend fun setLoggingLevel(level: LoggingLevel, options: RequestOptions? = null): EmptyRequestResult =
request(SetLevelRequest(level), options)

/**
* Retrieves a prompt by name from the server.
Expand All @@ -322,9 +313,8 @@ public open class Client(
* @return The requested prompt details, or `null` if not found.
* @throws IllegalStateException If the server does not support prompts.
*/
public suspend fun getPrompt(request: GetPromptRequest, options: RequestOptions? = null): GetPromptResult? {
return request<GetPromptResult>(request, options)
}
public suspend fun getPrompt(request: GetPromptRequest, options: RequestOptions? = null): GetPromptResult =
request(request, options)

/**
* Lists all available prompts from the server.
Expand All @@ -337,9 +327,7 @@ public open class Client(
public suspend fun listPrompts(
request: ListPromptsRequest = ListPromptsRequest(),
options: RequestOptions? = null,
): ListPromptsResult? {
return request<ListPromptsResult>(request, options)
}
): ListPromptsResult = request(request, options)

/**
* Lists all available resources from the server.
Expand All @@ -352,9 +340,7 @@ public open class Client(
public suspend fun listResources(
request: ListResourcesRequest = ListResourcesRequest(),
options: RequestOptions? = null,
): ListResourcesResult? {
return request<ListResourcesResult>(request, options)
}
): ListResourcesResult = request(request, options)

/**
* Lists resource templates available on the server.
Expand All @@ -367,9 +353,7 @@ public open class Client(
public suspend fun listResourceTemplates(
request: ListResourceTemplatesRequest,
options: RequestOptions? = null,
): ListResourceTemplatesResult? {
return request<ListResourceTemplatesResult>(request, options)
}
): ListResourceTemplatesResult = request(request, options)

/**
* Reads a resource from the server by its URI.
Expand All @@ -382,9 +366,7 @@ public open class Client(
public suspend fun readResource(
request: ReadResourceRequest,
options: RequestOptions? = null,
): ReadResourceResult? {
return request<ReadResourceResult>(request, options)
}
): ReadResourceResult = request(request, options)

/**
* Subscribes to resource changes on the server.
Expand All @@ -396,9 +378,7 @@ public open class Client(
public suspend fun subscribeResource(
request: SubscribeRequest,
options: RequestOptions? = null,
): EmptyRequestResult {
return request<EmptyRequestResult>(request, options)
}
): EmptyRequestResult = request(request, options)

/**
* Unsubscribes from resource changes on the server.
Expand All @@ -410,9 +390,7 @@ public open class Client(
public suspend fun unsubscribeResource(
request: UnsubscribeRequest,
options: RequestOptions? = null,
): EmptyRequestResult {
return request<EmptyRequestResult>(request, options)
}
): EmptyRequestResult = request(request, options)

/**
* Calls a tool on the server by name, passing the specified arguments.
Expand Down Expand Up @@ -443,7 +421,7 @@ public open class Client(

val request = CallToolRequest(
name = name,
arguments = JsonObject(jsonArguments)
arguments = JsonObject(jsonArguments),
)
return callTool(request, compatibility, options)
}
Expand All @@ -461,12 +439,10 @@ public open class Client(
request: CallToolRequest,
compatibility: Boolean = false,
options: RequestOptions? = null,
): CallToolResultBase? {
return if (compatibility) {
request<CompatibilityCallToolResult>(request, options)
} else {
request<CallToolResult>(request, options)
}
): CallToolResultBase? = if (compatibility) {
request<CompatibilityCallToolResult>(request, options)
} else {
request<CallToolResult>(request, options)
}

/**
Expand All @@ -480,9 +456,7 @@ public open class Client(
public suspend fun listTools(
request: ListToolsRequest = ListToolsRequest(),
options: RequestOptions? = null,
): ListToolsResult? {
return request<ListToolsResult>(request, options)
}
): ListToolsResult = request(request, options)

/**
* Registers a single root.
Expand All @@ -491,10 +465,7 @@ public open class Client(
* @param name A human-readable name for the root.
* @throws IllegalStateException If the client does not support roots.
*/
public fun addRoot(
uri: String,
name: String,
) {
public fun addRoot(uri: String, name: String) {
if (capabilities.roots == null) {
logger.error { "Failed to add root '$name': Client does not support roots capability" }
throw IllegalStateException("Client does not support roots capability.")
Expand Down
Loading
Loading