♻️ Quality: demo.acp.sdk/kotlin without smells (2 → 0)
- runTurn with a TurnRelay (the answer, the end of the turn, the collector's own exception) and turnError - Operations.kt (permission requests) and StderrTail.kt by theme Checked: ./gradlew test (20 tests: streaming, cancel, leaving the flow early, a crash, permissions…) and the four examples (Complete, Stream, Permission, Cancel) built from the old and the new code against mm with a scripted fake model: identical output. JDK installed in the sandbox for this (default-jdk-headless + ca-certificates-java). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
19f68c0 parent: 0d4a390 modified
demo.acp.sdk/kotlin/src/main/kotlin/minime/Agent.kt +42 -89 | @@ -4,13 +4,9 @@ import com.agentclientprotocol.annotations.UnstableApi | ||
| 4 | 4 | import com.agentclientprotocol.client.Client |
| 5 | 5 | import com.agentclientprotocol.client.ClientInfo |
| 6 | 6 | import com.agentclientprotocol.client.ClientSession |
| 7 | -import com.agentclientprotocol.common.ClientSessionOperations | |
| 8 | 7 | import com.agentclientprotocol.common.SessionCreationParameters |
| 9 | 8 | import com.agentclientprotocol.model.ContentBlock |
| 10 | 9 | import com.agentclientprotocol.model.Implementation |
| 11 | -import com.agentclientprotocol.model.PermissionOption | |
| 12 | -import com.agentclientprotocol.model.RequestPermissionOutcome | |
| 13 | -import com.agentclientprotocol.model.RequestPermissionResponse | |
| 14 | 10 | import com.agentclientprotocol.model.SessionUpdate |
| 15 | 11 | import com.agentclientprotocol.protocol.JsonRpcException |
| 16 | 12 | import com.agentclientprotocol.protocol.Protocol |
| @@ -32,7 +28,6 @@ import kotlinx.coroutines.flow.flowOn | ||
| 32 | 28 | import kotlinx.coroutines.sync.Mutex |
| 33 | 29 | import kotlinx.coroutines.withContext |
| 34 | 30 | import kotlinx.coroutines.withTimeout |
| 35 | -import kotlinx.serialization.json.JsonElement | |
| 36 | 31 | import kotlinx.serialization.json.JsonObject |
| 37 | 32 | import kotlinx.serialization.json.JsonPrimitive |
| 38 | 33 | import java.io.BufferedWriter |
| @@ -192,46 +187,29 @@ public class Agent private constructor( | ||
| 192 | 187 | } |
| 193 | 188 | } |
| 194 | 189 | |
| 195 | - @OptIn(UnstableApi::class) // PromptResponse.usage: ACP marks it unstable, mm sends it | |
| 196 | 190 | private suspend fun FlowCollector<Event>.runTurn(prompt: String) { |
| 197 | 191 | operations.turnCancelled = false |
| 198 | - val answer = StringBuilder() | |
| 199 | - var finished = false | |
| 200 | - var downstreamFailure: Throwable? = null | |
| 201 | - // An exception thrown by the collector's own code must reach it unchanged, not as mm's. | |
| 202 | - suspend fun send(event: Event) { | |
| 203 | - try { | |
| 204 | - emit(event) | |
| 205 | - } catch (e: Throwable) { | |
| 206 | - downstreamFailure = e | |
| 207 | - throw e | |
| 208 | - } | |
| 209 | - } | |
| 192 | + val relay = TurnRelay(this) | |
| 210 | 193 | try { |
| 211 | - session.prompt(listOf(ContentBlock.Text(prompt))).collect { event -> | |
| 212 | - when (event) { | |
| 213 | - is AcpEvent.SessionUpdateEvent -> eventOf(event.update)?.let { | |
| 214 | - if (it is Event.Text) answer.append(it.text) | |
| 215 | - send(it) | |
| 216 | - } | |
| 217 | - is AcpEvent.PromptResponseEvent -> { | |
| 218 | - finished = true | |
| 219 | - val response = event.response | |
| 220 | - send(Event.Done(response.stopReason.name.lowercase(), usageOf(response.usage), answer.toString())) | |
| 221 | - } | |
| 222 | - } | |
| 223 | - } | |
| 194 | + session.prompt(listOf(ContentBlock.Text(prompt))).collect { relay.on(it) } | |
| 224 | 195 | } catch (e: Exception) { |
| 225 | - if (e === downstreamFailure) throw e | |
| 226 | - // A CancellationException while this coroutine is still active is the SDK closing the | |
| 227 | - // protocol under us — mm has gone — not the caller cancelling. | |
| 228 | - if (e is CancellationException && !currentCoroutineContext().isActive) throw e | |
| 229 | - throw failure("turn failed", e) | |
| 196 | + throw turnError(e, relay.downstreamFailure) | |
| 230 | 197 | } finally { |
| 231 | - if (!finished) withContext(NonCancellable) { sendCancel() } | |
| 198 | + if (!relay.finished) withContext(NonCancellable) { sendCancel() } | |
| 232 | 199 | } |
| 233 | 200 | } |
| 234 | 201 | |
| 202 | + /** | |
| 203 | + * What a failed turn throws: the collector's own exception unchanged, a cancellation as it | |
| 204 | + * came — unless it arrives while this coroutine is still active, which is the SDK closing the | |
| 205 | + * protocol under us (mm has gone), not the caller cancelling — and mm's failure otherwise. | |
| 206 | + */ | |
| 207 | + private suspend fun turnError(e: Exception, downstream: Throwable?): Throwable = when { | |
| 208 | + e === downstream -> e | |
| 209 | + e is CancellationException && !currentCoroutineContext().isActive -> e | |
| 210 | + else -> failure("turn failed", e) | |
| 211 | + } | |
| 212 | + | |
| 235 | 213 | /** |
| 236 | 214 | * Cancels the turn in progress, if any: mm receives `session/cancel` and ends the turn with |
| 237 | 215 | * stopReason `cancelled`, which [complete] and [stream] then return as usual. |
| @@ -302,63 +280,38 @@ internal fun wordsOf(e: Throwable): String { | ||
| 302 | 280 | } |
| 303 | 281 | |
| 304 | 282 | /** |
| 305 | - * The client side of ACP for one session: mm's permission requests. Updates outside a turn | |
| 306 | - * (available_commands_update right after session/new) are dropped. | |
| 283 | + * One turn's events on their way to the collector: the answer gathered from the text events, | |
| 284 | + * whether mm ended the turn, and the exception the collector's own code threw, if any — it must | |
| 285 | + * reach the collector unchanged, not as mm's. | |
| 307 | 286 | */ |
| 308 | -internal class Operations( | |
| 309 | - private val onPermission: (suspend (ToolCall) -> Decision)?, | |
| 310 | -) : ClientSessionOperations { | |
| 311 | - /** Set once session/cancel is sent: pending permission requests are answered "cancelled", as ACP requires. */ | |
| 312 | - @Volatile var turnCancelled = false | |
| 313 | - | |
| 314 | - override suspend fun requestPermissions( | |
| 315 | - toolCall: SessionUpdate.ToolCallUpdate, | |
| 316 | - permissions: List<PermissionOption>, | |
| 317 | - _meta: JsonElement?, | |
| 318 | - ): RequestPermissionResponse { | |
| 319 | - val cancelled = RequestPermissionResponse( | |
| 320 | - RequestPermissionOutcome.Cancelled, | |
| 321 | - ) | |
| 322 | - if (turnCancelled) return cancelled | |
| 323 | - val optionId = pickOption(permissions, decide(toolCallOf(toolCall))) ?: return cancelled | |
| 324 | - return RequestPermissionResponse( | |
| 325 | - RequestPermissionOutcome.Selected(optionId), | |
| 326 | - ) | |
| 327 | - } | |
| 287 | +private class TurnRelay(private val out: FlowCollector<Event>) { | |
| 288 | + private val answer = StringBuilder() | |
| 289 | + var finished = false | |
| 290 | + private set | |
| 291 | + var downstreamFailure: Throwable? = null | |
| 292 | + private set | |
| 328 | 293 | |
| 329 | - /** Runs the callback; a missing or failing callback rejects. */ | |
| 330 | - private suspend fun decide(call: ToolCall): Decision { | |
| 331 | - val callback = onPermission ?: return Decision.Reject | |
| 332 | - return try { | |
| 333 | - callback(call) | |
| 334 | - } catch (e: CancellationException) { | |
| 335 | - throw e | |
| 336 | - } catch (e: Exception) { | |
| 337 | - Decision.Reject | |
| 338 | - } | |
| 339 | - } | |
| 340 | - | |
| 341 | - override suspend fun notify(notification: SessionUpdate, _meta: JsonElement?) {} | |
| 342 | -} | |
| 343 | - | |
| 344 | -/** Reads mm's stderr on its own thread, keeping the last non-empty line. */ | |
| 345 | -internal class StderrTail(stream: InputStream, onLine: ((String) -> Unit)?) { | |
| 346 | - @Volatile private var line: String? = null | |
| 347 | - private val thread = Thread({ | |
| 348 | - stream.bufferedReader().useLines { lines -> | |
| 349 | - for (l in lines) { | |
| 350 | - onLine?.invoke(l) | |
| 351 | - if (l.isNotEmpty()) line = l | |
| 294 | + @OptIn(UnstableApi::class) // PromptResponse.usage: ACP marks it unstable, mm sends it | |
| 295 | + suspend fun on(event: AcpEvent) { | |
| 296 | + when (event) { | |
| 297 | + is AcpEvent.SessionUpdateEvent -> eventOf(event.update)?.let { | |
| 298 | + if (it is Event.Text) answer.append(it.text) | |
| 299 | + send(it) | |
| 300 | + } | |
| 301 | + is AcpEvent.PromptResponseEvent -> { | |
| 302 | + finished = true | |
| 303 | + val response = event.response | |
| 304 | + send(Event.Done(response.stopReason.name.lowercase(), usageOf(response.usage), answer.toString())) | |
| 352 | 305 | } |
| 353 | 306 | } |
| 354 | - }, "minime-stderr").apply { | |
| 355 | - isDaemon = true | |
| 356 | - start() | |
| 357 | 307 | } |
| 358 | 308 | |
| 359 | - fun last(): String? = line | |
| 360 | - | |
| 361 | - fun join() { | |
| 362 | - thread.join(1_000) | |
| 309 | + private suspend fun send(event: Event) { | |
| 310 | + try { | |
| 311 | + out.emit(event) | |
| 312 | + } catch (e: Throwable) { | |
| 313 | + downstreamFailure = e | |
| 314 | + throw e | |
| 315 | + } | |
| 363 | 316 | } |
| 364 | 317 | } |
| @@ -4,13 +4,9 @@ import com.agentclientprotocol.annotations.UnstableApi | |||
| 4 | import com.agentclientprotocol.client.Client | 4 | import com.agentclientprotocol.client.Client |
| 5 | import com.agentclientprotocol.client.ClientInfo | 5 | import com.agentclientprotocol.client.ClientInfo |
| 6 | import com.agentclientprotocol.client.ClientSession | 6 | import com.agentclientprotocol.client.ClientSession |
| 7 | -import com.agentclientprotocol.common.ClientSessionOperations | ||
| 8 | import com.agentclientprotocol.common.SessionCreationParameters | 7 | import com.agentclientprotocol.common.SessionCreationParameters |
| 9 | import com.agentclientprotocol.model.ContentBlock | 8 | import com.agentclientprotocol.model.ContentBlock |
| 10 | import com.agentclientprotocol.model.Implementation | 9 | import com.agentclientprotocol.model.Implementation |
| 11 | -import com.agentclientprotocol.model.PermissionOption | ||
| 12 | -import com.agentclientprotocol.model.RequestPermissionOutcome | ||
| 13 | -import com.agentclientprotocol.model.RequestPermissionResponse | ||
| 14 | import com.agentclientprotocol.model.SessionUpdate | 10 | import com.agentclientprotocol.model.SessionUpdate |
| 15 | import com.agentclientprotocol.protocol.JsonRpcException | 11 | import com.agentclientprotocol.protocol.JsonRpcException |
| 16 | import com.agentclientprotocol.protocol.Protocol | 12 | import com.agentclientprotocol.protocol.Protocol |
| @@ -32,7 +28,6 @@ import kotlinx.coroutines.flow.flowOn | |||
| 32 | import kotlinx.coroutines.sync.Mutex | 28 | import kotlinx.coroutines.sync.Mutex |
| 33 | import kotlinx.coroutines.withContext | 29 | import kotlinx.coroutines.withContext |
| 34 | import kotlinx.coroutines.withTimeout | 30 | import kotlinx.coroutines.withTimeout |
| 35 | -import kotlinx.serialization.json.JsonElement | ||
| 36 | import kotlinx.serialization.json.JsonObject | 31 | import kotlinx.serialization.json.JsonObject |
| 37 | import kotlinx.serialization.json.JsonPrimitive | 32 | import kotlinx.serialization.json.JsonPrimitive |
| 38 | import java.io.BufferedWriter | 33 | import java.io.BufferedWriter |
| @@ -192,46 +187,29 @@ public class Agent private constructor( | |||
| 192 | } | 187 | } |
| 193 | } | 188 | } |
| 194 | 189 | ||
| 195 | - @OptIn(UnstableApi::class) // PromptResponse.usage: ACP marks it unstable, mm sends it | ||
| 196 | private suspend fun FlowCollector<Event>.runTurn(prompt: String) { | 190 | private suspend fun FlowCollector<Event>.runTurn(prompt: String) { |
| 197 | operations.turnCancelled = false | 191 | operations.turnCancelled = false |
| 198 | - val answer = StringBuilder() | 192 | + val relay = TurnRelay(this) |
| 199 | - var finished = false | ||
| 200 | - var downstreamFailure: Throwable? = null | ||
| 201 | - // An exception thrown by the collector's own code must reach it unchanged, not as mm's. | ||
| 202 | - suspend fun send(event: Event) { | ||
| 203 | - try { | ||
| 204 | - emit(event) | ||
| 205 | - } catch (e: Throwable) { | ||
| 206 | - downstreamFailure = e | ||
| 207 | - throw e | ||
| 208 | - } | ||
| 209 | - } | ||
| 210 | try { | 193 | try { |
| 211 | - session.prompt(listOf(ContentBlock.Text(prompt))).collect { event -> | 194 | + session.prompt(listOf(ContentBlock.Text(prompt))).collect { relay.on(it) } |
| 212 | - when (event) { | ||
| 213 | - is AcpEvent.SessionUpdateEvent -> eventOf(event.update)?.let { | ||
| 214 | - if (it is Event.Text) answer.append(it.text) | ||
| 215 | - send(it) | ||
| 216 | - } | ||
| 217 | - is AcpEvent.PromptResponseEvent -> { | ||
| 218 | - finished = true | ||
| 219 | - val response = event.response | ||
| 220 | - send(Event.Done(response.stopReason.name.lowercase(), usageOf(response.usage), answer.toString())) | ||
| 221 | - } | ||
| 222 | - } | ||
| 223 | - } | ||
| 224 | } catch (e: Exception) { | 195 | } catch (e: Exception) { |
| 225 | - if (e === downstreamFailure) throw e | 196 | + throw turnError(e, relay.downstreamFailure) |
| 226 | - // A CancellationException while this coroutine is still active is the SDK closing the | ||
| 227 | - // protocol under us — mm has gone — not the caller cancelling. | ||
| 228 | - if (e is CancellationException && !currentCoroutineContext().isActive) throw e | ||
| 229 | - throw failure("turn failed", e) | ||
| 230 | } finally { | 197 | } finally { |
| 231 | - if (!finished) withContext(NonCancellable) { sendCancel() } | 198 | + if (!relay.finished) withContext(NonCancellable) { sendCancel() } |
| 232 | } | 199 | } |
| 233 | } | 200 | } |
| 234 | 201 | ||
| 202 | + /** | ||
| 203 | + * What a failed turn throws: the collector's own exception unchanged, a cancellation as it | ||
| 204 | + * came — unless it arrives while this coroutine is still active, which is the SDK closing the | ||
| 205 | + * protocol under us (mm has gone), not the caller cancelling — and mm's failure otherwise. | ||
| 206 | + */ | ||
| 207 | + private suspend fun turnError(e: Exception, downstream: Throwable?): Throwable = when { | ||
| 208 | + e === downstream -> e | ||
| 209 | + e is CancellationException && !currentCoroutineContext().isActive -> e | ||
| 210 | + else -> failure("turn failed", e) | ||
| 211 | + } | ||
| 212 | + | ||
| 235 | /** | 213 | /** |
| 236 | * Cancels the turn in progress, if any: mm receives `session/cancel` and ends the turn with | 214 | * Cancels the turn in progress, if any: mm receives `session/cancel` and ends the turn with |
| 237 | * stopReason `cancelled`, which [complete] and [stream] then return as usual. | 215 | * stopReason `cancelled`, which [complete] and [stream] then return as usual. |
| @@ -302,63 +280,38 @@ internal fun wordsOf(e: Throwable): String { | |||
| 302 | } | 280 | } |
| 303 | 281 | ||
| 304 | /** | 282 | /** |
| 305 | - * The client side of ACP for one session: mm's permission requests. Updates outside a turn | 283 | + * One turn's events on their way to the collector: the answer gathered from the text events, |
| 306 | - * (available_commands_update right after session/new) are dropped. | 284 | + * whether mm ended the turn, and the exception the collector's own code threw, if any — it must |
| 285 | + * reach the collector unchanged, not as mm's. | ||
| 307 | */ | 286 | */ |
| 308 | -internal class Operations( | 287 | +private class TurnRelay(private val out: FlowCollector<Event>) { |
| 309 | - private val onPermission: (suspend (ToolCall) -> Decision)?, | 288 | + private val answer = StringBuilder() |
| 310 | -) : ClientSessionOperations { | 289 | + var finished = false |
| 311 | - /** Set once session/cancel is sent: pending permission requests are answered "cancelled", as ACP requires. */ | 290 | + private set |
| 312 | - @Volatile var turnCancelled = false | 291 | + var downstreamFailure: Throwable? = null |
| 313 | - | 292 | + private set |
| 314 | - override suspend fun requestPermissions( | ||
| 315 | - toolCall: SessionUpdate.ToolCallUpdate, | ||
| 316 | - permissions: List<PermissionOption>, | ||
| 317 | - _meta: JsonElement?, | ||
| 318 | - ): RequestPermissionResponse { | ||
| 319 | - val cancelled = RequestPermissionResponse( | ||
| 320 | - RequestPermissionOutcome.Cancelled, | ||
| 321 | - ) | ||
| 322 | - if (turnCancelled) return cancelled | ||
| 323 | - val optionId = pickOption(permissions, decide(toolCallOf(toolCall))) ?: return cancelled | ||
| 324 | - return RequestPermissionResponse( | ||
| 325 | - RequestPermissionOutcome.Selected(optionId), | ||
| 326 | - ) | ||
| 327 | - } | ||
| 328 | 293 | ||
| 329 | - /** Runs the callback; a missing or failing callback rejects. */ | 294 | + @OptIn(UnstableApi::class) // PromptResponse.usage: ACP marks it unstable, mm sends it |
| 330 | - private suspend fun decide(call: ToolCall): Decision { | 295 | + suspend fun on(event: AcpEvent) { |
| 331 | - val callback = onPermission ?: return Decision.Reject | 296 | + when (event) { |
| 332 | - return try { | 297 | + is AcpEvent.SessionUpdateEvent -> eventOf(event.update)?.let { |
| 333 | - callback(call) | 298 | + if (it is Event.Text) answer.append(it.text) |
| 334 | - } catch (e: CancellationException) { | 299 | + send(it) |
| 335 | - throw e | 300 | + } |
| 336 | - } catch (e: Exception) { | 301 | + is AcpEvent.PromptResponseEvent -> { |
| 337 | - Decision.Reject | 302 | + finished = true |
| 338 | - } | 303 | + val response = event.response |
| 339 | - } | 304 | + send(Event.Done(response.stopReason.name.lowercase(), usageOf(response.usage), answer.toString())) |
| 340 | - | ||
| 341 | - override suspend fun notify(notification: SessionUpdate, _meta: JsonElement?) {} | ||
| 342 | -} | ||
| 343 | - | ||
| 344 | -/** Reads mm's stderr on its own thread, keeping the last non-empty line. */ | ||
| 345 | -internal class StderrTail(stream: InputStream, onLine: ((String) -> Unit)?) { | ||
| 346 | - @Volatile private var line: String? = null | ||
| 347 | - private val thread = Thread({ | ||
| 348 | - stream.bufferedReader().useLines { lines -> | ||
| 349 | - for (l in lines) { | ||
| 350 | - onLine?.invoke(l) | ||
| 351 | - if (l.isNotEmpty()) line = l | ||
| 352 | } | 305 | } |
| 353 | } | 306 | } |
| 354 | - }, "minime-stderr").apply { | ||
| 355 | - isDaemon = true | ||
| 356 | - start() | ||
| 357 | } | 307 | } |
| 358 | 308 | ||
| 359 | - fun last(): String? = line | 309 | + private suspend fun send(event: Event) { |
| 360 | - | 310 | + try { |
| 361 | - fun join() { | 311 | + out.emit(event) |
| 362 | - thread.join(1_000) | 312 | + } catch (e: Throwable) { |
| 313 | + downstreamFailure = e | ||
| 314 | + throw e | ||
| 315 | + } | ||
| 363 | } | 316 | } |
| 364 | } | 317 | } |
added
demo.acp.sdk/kotlin/src/main/kotlin/minime/Operations.kt +49 -0 | new file mode 100644 | ||
| @@ -0,0 +1,49 @@ | ||
| 1 | +package minime | |
| 2 | + | |
| 3 | +import com.agentclientprotocol.common.ClientSessionOperations | |
| 4 | +import com.agentclientprotocol.model.PermissionOption | |
| 5 | +import com.agentclientprotocol.model.RequestPermissionOutcome | |
| 6 | +import com.agentclientprotocol.model.RequestPermissionResponse | |
| 7 | +import com.agentclientprotocol.model.SessionUpdate | |
| 8 | +import kotlinx.coroutines.CancellationException | |
| 9 | +import kotlinx.serialization.json.JsonElement | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * The client side of ACP for one session: mm's permission requests. Updates outside a turn | |
| 13 | + * (available_commands_update right after session/new) are dropped. | |
| 14 | + */ | |
| 15 | +internal class Operations( | |
| 16 | + private val onPermission: (suspend (ToolCall) -> Decision)?, | |
| 17 | +) : ClientSessionOperations { | |
| 18 | + /** Set once session/cancel is sent: pending permission requests are answered "cancelled", as ACP requires. */ | |
| 19 | + @Volatile var turnCancelled = false | |
| 20 | + | |
| 21 | + override suspend fun requestPermissions( | |
| 22 | + toolCall: SessionUpdate.ToolCallUpdate, | |
| 23 | + permissions: List<PermissionOption>, | |
| 24 | + _meta: JsonElement?, | |
| 25 | + ): RequestPermissionResponse { | |
| 26 | + val cancelled = RequestPermissionResponse( | |
| 27 | + RequestPermissionOutcome.Cancelled, | |
| 28 | + ) | |
| 29 | + if (turnCancelled) return cancelled | |
| 30 | + val optionId = pickOption(permissions, decide(toolCallOf(toolCall))) ?: return cancelled | |
| 31 | + return RequestPermissionResponse( | |
| 32 | + RequestPermissionOutcome.Selected(optionId), | |
| 33 | + ) | |
| 34 | + } | |
| 35 | + | |
| 36 | + /** Runs the callback; a missing or failing callback rejects. */ | |
| 37 | + private suspend fun decide(call: ToolCall): Decision { | |
| 38 | + val callback = onPermission ?: return Decision.Reject | |
| 39 | + return try { | |
| 40 | + callback(call) | |
| 41 | + } catch (e: CancellationException) { | |
| 42 | + throw e | |
| 43 | + } catch (e: Exception) { | |
| 44 | + Decision.Reject | |
| 45 | + } | |
| 46 | + } | |
| 47 | + | |
| 48 | + override suspend fun notify(notification: SessionUpdate, _meta: JsonElement?) {} | |
| 49 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,49 @@ | |||
| 1 | +package minime | ||
| 2 | + | ||
| 3 | +import com.agentclientprotocol.common.ClientSessionOperations | ||
| 4 | +import com.agentclientprotocol.model.PermissionOption | ||
| 5 | +import com.agentclientprotocol.model.RequestPermissionOutcome | ||
| 6 | +import com.agentclientprotocol.model.RequestPermissionResponse | ||
| 7 | +import com.agentclientprotocol.model.SessionUpdate | ||
| 8 | +import kotlinx.coroutines.CancellationException | ||
| 9 | +import kotlinx.serialization.json.JsonElement | ||
| 10 | + | ||
| 11 | +/** | ||
| 12 | + * The client side of ACP for one session: mm's permission requests. Updates outside a turn | ||
| 13 | + * (available_commands_update right after session/new) are dropped. | ||
| 14 | + */ | ||
| 15 | +internal class Operations( | ||
| 16 | + private val onPermission: (suspend (ToolCall) -> Decision)?, | ||
| 17 | +) : ClientSessionOperations { | ||
| 18 | + /** Set once session/cancel is sent: pending permission requests are answered "cancelled", as ACP requires. */ | ||
| 19 | + @Volatile var turnCancelled = false | ||
| 20 | + | ||
| 21 | + override suspend fun requestPermissions( | ||
| 22 | + toolCall: SessionUpdate.ToolCallUpdate, | ||
| 23 | + permissions: List<PermissionOption>, | ||
| 24 | + _meta: JsonElement?, | ||
| 25 | + ): RequestPermissionResponse { | ||
| 26 | + val cancelled = RequestPermissionResponse( | ||
| 27 | + RequestPermissionOutcome.Cancelled, | ||
| 28 | + ) | ||
| 29 | + if (turnCancelled) return cancelled | ||
| 30 | + val optionId = pickOption(permissions, decide(toolCallOf(toolCall))) ?: return cancelled | ||
| 31 | + return RequestPermissionResponse( | ||
| 32 | + RequestPermissionOutcome.Selected(optionId), | ||
| 33 | + ) | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + /** Runs the callback; a missing or failing callback rejects. */ | ||
| 37 | + private suspend fun decide(call: ToolCall): Decision { | ||
| 38 | + val callback = onPermission ?: return Decision.Reject | ||
| 39 | + return try { | ||
| 40 | + callback(call) | ||
| 41 | + } catch (e: CancellationException) { | ||
| 42 | + throw e | ||
| 43 | + } catch (e: Exception) { | ||
| 44 | + Decision.Reject | ||
| 45 | + } | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + override suspend fun notify(notification: SessionUpdate, _meta: JsonElement?) {} | ||
| 49 | +} | ||
added
demo.acp.sdk/kotlin/src/main/kotlin/minime/StderrTail.kt +25 -0 | new file mode 100644 | ||
| @@ -0,0 +1,25 @@ | ||
| 1 | +package minime | |
| 2 | + | |
| 3 | +import java.io.InputStream | |
| 4 | + | |
| 5 | +/** Reads mm's stderr on its own thread, keeping the last non-empty line. */ | |
| 6 | +internal class StderrTail(stream: InputStream, onLine: ((String) -> Unit)?) { | |
| 7 | + @Volatile private var line: String? = null | |
| 8 | + private val thread = Thread({ | |
| 9 | + stream.bufferedReader().useLines { lines -> | |
| 10 | + for (l in lines) { | |
| 11 | + onLine?.invoke(l) | |
| 12 | + if (l.isNotEmpty()) line = l | |
| 13 | + } | |
| 14 | + } | |
| 15 | + }, "minime-stderr").apply { | |
| 16 | + isDaemon = true | |
| 17 | + start() | |
| 18 | + } | |
| 19 | + | |
| 20 | + fun last(): String? = line | |
| 21 | + | |
| 22 | + fun join() { | |
| 23 | + thread.join(1_000) | |
| 24 | + } | |
| 25 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | +package minime | ||
| 2 | + | ||
| 3 | +import java.io.InputStream | ||
| 4 | + | ||
| 5 | +/** Reads mm's stderr on its own thread, keeping the last non-empty line. */ | ||
| 6 | +internal class StderrTail(stream: InputStream, onLine: ((String) -> Unit)?) { | ||
| 7 | + @Volatile private var line: String? = null | ||
| 8 | + private val thread = Thread({ | ||
| 9 | + stream.bufferedReader().useLines { lines -> | ||
| 10 | + for (l in lines) { | ||
| 11 | + onLine?.invoke(l) | ||
| 12 | + if (l.isNotEmpty()) line = l | ||
| 13 | + } | ||
| 14 | + } | ||
| 15 | + }, "minime-stderr").apply { | ||
| 16 | + isDaemon = true | ||
| 17 | + start() | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + fun last(): String? = line | ||
| 21 | + | ||
| 22 | + fun join() { | ||
| 23 | + thread.join(1_000) | ||
| 24 | + } | ||
| 25 | +} | ||