Skip to content
Merged
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
4 changes: 0 additions & 4 deletions acp/api/acp.api
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,3 @@ public final class com/agentclientprotocol/transport/TransportKt {
public static final fun asMessageChannel (Lcom/agentclientprotocol/transport/Transport;)Lkotlinx/coroutines/channels/Channel;
}

public final class com/agentclientprotocol/util/CoroutinesUtilKt {
public static final fun catching (Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.agentclientprotocol.model.CancelRequestNotification
import com.agentclientprotocol.rpc.*
import com.agentclientprotocol.transport.Transport
import com.agentclientprotocol.transport.asMessageChannel
import com.agentclientprotocol.util.catching
import com.agentclientprotocol.util.checkCancelled
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.atomicfu.*
import kotlinx.collections.immutable.PersistentMap
Expand Down Expand Up @@ -164,13 +164,11 @@ public class Protocol(

// Start processing incoming messages
scope.launch(CoroutineName("${Protocol::class.simpleName!!}.read-messages")) {
catching {
runCatching {
for (message in transport.asMessageChannel()) {
catching { handleIncomingMessage(message) }.onFailure { logger.error(it) {
"Error processing incoming message: $message" }
}
handleIncomingMessage(message)
}
}.onFailure {
}.checkCancelled().onFailure {
logger.error(it) { "Error processing incoming messages" }
}
}
Expand Down Expand Up @@ -365,8 +363,8 @@ public class Protocol(
handleResponse(message)
}
}
}.onFailure {
logger.error(it) { "Failed to parse message: $message" }
}.checkCancelled().onFailure {
logger.error(it) { "Exception while processing incoming message: $message" }
}
}

Expand Down Expand Up @@ -427,8 +425,12 @@ public class Protocol(
if (handler != null) {
runCatching {
handler(notification)
}.onFailure {
logger.error(it) { "Error handling notification ${notification.method}" }
}.onFailure { t ->
if (t is CancellationException) {
logger.trace(t) { "Notification handler for '${notification.method}' cancelled" }
} else {
logger.error(t) { "Error handling notification ${notification.method}" }
}
}
} else {
logger.debug { "No handler for notification: ${notification.method}" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.agentclientprotocol.rpc.ACPJson
import com.agentclientprotocol.rpc.JsonRpcMessage
import com.agentclientprotocol.rpc.decodeJsonRpcMessage
import com.agentclientprotocol.transport.Transport.State
import com.agentclientprotocol.util.checkCancelled
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package com.agentclientprotocol.util

import kotlin.coroutines.cancellation.CancellationException

/** Use instead of [runCatching] for KT-55480 fix. */
public suspend fun <R> catching(body: suspend () -> R): Result<R> =
try {
Result.success(body())
} catch (c: CancellationException) {
throw c
} catch (t: Throwable) {
Result.failure(t)
}
/**
* Rethrows [CancellationException] if the [Result] execution was cancelled. Use on [runCatching] to avoid swallowing of [CancellationException]
*/
internal fun <T> Result<T>.checkCancelled(): Result<T> {
val throwable = exceptionOrNull()
if (throwable is CancellationException) throw throwable
return this
}
Loading