@@ -2,7 +2,7 @@ import { Tool } from "@opencode/schema/tool" |
2 | 2 | import type { Rpc } from "@opencode/schema/rpc" |
3 | 3 | import type { OpenCodeEvent } from "@opencode/client" |
4 | 4 | import type { RpcCallOptions, RpcEventPayload, SelectedEvent, SubscribeOptions } from "@opencode/client/promise/api" |
5 | | -import { Effect, Schema, SchemaAST, Stream } from "effect" |
| 5 | +import { Cause, Effect, Exit, Schema, SchemaAST, Stream } from "effect" |
6 | 6 | import type { Scope } from "effect" |
7 | 7 | import { HttpApiEndpoint, HttpApiSchema } from "effect/http-api" |
8 | 8 | import { define } from "../effect/plugin.js" |
@@ -302,9 +302,10 @@ export function fromPromise(plugin: Plugin) { |
302 | 302 | ) => { |
303 | 303 | const compiled = compileEndpoint(endpoint) |
304 | 304 | return ((input?: unknown, options?: { readonly signal?: AbortSignal }) => { |
| 305 | + const signal = options?.signal |
305 | 306 | // The runner evaluates synchronously before it checks the signal, so an aborted call could start a mutation. |
306 | | - if (options?.signal?.aborted) return Promise.reject(options.signal.reason) |
307 | | - return Effect.runPromiseWith(runtime)( |
| 307 | + if (signal?.aborted) return Promise.reject(signal.reason) |
| 308 | + return Effect.runPromiseExitWith(runtime)( |
308 | 309 | Effect.gen(function* () { |
309 | 310 | // Match the generated Promise client, whose request body crosses JSON before endpoint decoding. |
310 | 311 | const normalized = yield* Schema.encodeUnknownEffect(JsonInput)(input ?? {}).pipe( |
@@ -315,9 +316,19 @@ export function fromPromise(plugin: Plugin) { |
315 | 316 | if (compiled.noContent) return undefined |
316 | 317 | return yield* compiled.encode(result) |
317 | 318 | }), |
318 | | - { signal: options?.signal }, |
319 | | - // An abort mid-call interrupts the fiber; reject with the signal's reason, as fetch does. |
320 | | - ).catch((error) => Promise.reject(options?.signal?.aborted ? options.signal.reason : error)) |
| 319 | + { signal }, |
| 320 | + ).then((exit) => { |
| 321 | + if (Exit.isSuccess(exit)) return exit.value |
| 322 | + // Effect records AbortSignal interruptions without an interrupting fiber ID. |
| 323 | + if ( |
| 324 | + signal?.aborted && |
| 325 | + Cause.hasInterruptsOnly(exit.cause) && |
| 326 | + exit.cause.reasons.some((reason) => Cause.isInterruptReason(reason) && reason.fiberId === undefined) |
| 327 | + ) { |
| 328 | + throw signal.reason |
| 329 | + } |
| 330 | + throw Cause.squash(exit.cause) |
| 331 | + }) |
321 | 332 | }) as PromiseMethod |
322 | 333 | } |
323 | 334 |
|
|