diff --git a/apps/server/src/provider/Layers/CursorAdapter.ts b/apps/server/src/provider/Layers/CursorAdapter.ts index 681f7746a5..804a806a2e 100644 --- a/apps/server/src/provider/Layers/CursorAdapter.ts +++ b/apps/server/src/provider/Layers/CursorAdapter.ts @@ -33,6 +33,7 @@ import { Layer, Queue, Random, + Schema, Stream, } from "effect"; @@ -106,14 +107,14 @@ function mapAcpToAdapterError( method: string, error: AcpError, ): ProviderAdapterError { - if (error instanceof AcpProcessExitedError) { + if (Schema.is(AcpProcessExitedError)(error)) { return new ProviderAdapterSessionClosedError({ provider: PROVIDER, threadId, cause: error, }); } - if (error instanceof AcpRpcError) { + if (Schema.is(AcpRpcError)(error)) { return new ProviderAdapterRequestError({ provider: PROVIDER, method, @@ -535,7 +536,7 @@ function updateSessionModeState( function isMethodNotFoundRpcError(error: AcpError): boolean { return ( - error instanceof AcpRpcError && + Schema.is(AcpRpcError)(error) && (error.code === -32601 || error.message.toLowerCase().includes("method not found")) ); } diff --git a/apps/server/src/provider/acp/AcpErrors.ts b/apps/server/src/provider/acp/AcpErrors.ts index 40b35ca316..edbc10677d 100644 --- a/apps/server/src/provider/acp/AcpErrors.ts +++ b/apps/server/src/provider/acp/AcpErrors.ts @@ -1,24 +1,51 @@ -import { Data } from "effect"; +import { Schema } from "effect"; -export class AcpSpawnError extends Data.TaggedError("AcpSpawnError")<{ - readonly message: string; - readonly cause?: unknown; -}> {} +export class AcpSpawnError extends Schema.TaggedErrorClass()("AcpSpawnError", { + command: Schema.optional(Schema.String), + args: Schema.optional(Schema.Array(Schema.String)), + shell: Schema.optional(Schema.Boolean), + cause: Schema.optional(Schema.Defect), +}) { + override get message() { + return `Failed to spawn ACP process: ${this.cause instanceof Error ? this.cause.message : String(this.cause)}`; + } +} -export class AcpParseError extends Data.TaggedError("AcpParseError")<{ - readonly line: string; - readonly cause?: unknown; -}> {} +export class AcpParseError extends Schema.TaggedErrorClass()("AcpParseError", { + line: Schema.String, + cause: Schema.optional(Schema.Defect), +}) { + override get message() { + return `Failed to parse ACP message: ${this.line}`; + } +} +export class AcpRpcError extends Schema.TaggedErrorClass()("AcpRpcError", { + code: Schema.Number, + errorMessage: Schema.optional(Schema.String), + data: Schema.optional(Schema.Unknown), +}) { + override get message() { + return `Failed to send ACP RPC message (code: ${this.code}, message: ${this.errorMessage}, data: ${JSON.stringify(this.data)})`; + } +} -export class AcpRpcError extends Data.TaggedError("AcpRpcError")<{ - readonly code: number; - readonly message: string; - readonly data?: unknown; -}> {} +export class AcpProcessExitedError extends Schema.TaggedErrorClass()( + "AcpProcessExitedError", + { + code: Schema.NullOr(Schema.Number), + signal: Schema.NullOr(Schema.String), + }, +) { + override get message() { + return `ACP process exited with code ${this.code} and signal ${this.signal}`; + } +} -export class AcpProcessExitedError extends Data.TaggedError("AcpProcessExitedError")<{ - readonly code: number | null; - readonly signal: NodeJS.Signals | null; -}> {} +export const AcpError = Schema.Union([ + AcpSpawnError, + AcpParseError, + AcpRpcError, + AcpProcessExitedError, +]); -export type AcpError = AcpSpawnError | AcpParseError | AcpRpcError | AcpProcessExitedError; +export type AcpError = typeof AcpError.Type; diff --git a/apps/server/src/provider/acp/AcpJsonRpcConnection.ts b/apps/server/src/provider/acp/AcpJsonRpcConnection.ts index cc44e46af9..bb73a26f49 100644 --- a/apps/server/src/provider/acp/AcpJsonRpcConnection.ts +++ b/apps/server/src/provider/acp/AcpJsonRpcConnection.ts @@ -1,14 +1,25 @@ import { createInterface } from "node:readline"; import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process"; -import { Cause, Deferred, Effect, Exit, Queue, Ref, Scope, Semaphore, Stream } from "effect"; +import { + Cause, + Deferred, + Effect, + Exit, + Queue, + Ref, + Schema, + Scope, + Semaphore, + Stream, +} from "effect"; import { + AcpError, AcpParseError, AcpProcessExitedError, AcpRpcError, AcpSpawnError, - type AcpError, } from "./AcpErrors.ts"; import { decodeAcpInboundFromJsonLine, @@ -43,24 +54,21 @@ export interface AcpJsonRpcConnection { export function spawnAcpChildProcess( input: AcpSpawnInput, ): Effect.Effect { + const shell = process.platform === "win32"; return Effect.try({ try: () => { const c = spawn(input.command, [...input.args], { cwd: input.cwd, env: { ...process.env, ...input.env }, stdio: ["pipe", "pipe", "inherit"], - shell: process.platform === "win32", + shell, }); if (!c.stdin || !c.stdout) { throw new Error("Child process missing stdio pipes."); } return c as unknown as ChildProcessWithoutNullStreams; }, - catch: (cause) => - new AcpSpawnError({ - message: cause instanceof Error ? cause.message : String(cause), - cause, - }), + catch: (cause) => new AcpSpawnError({ command: input.command, args: input.args, shell, cause }), }); } @@ -109,7 +117,6 @@ export const attachAcpJsonRpcConnection = ( }, catch: (cause) => new AcpSpawnError({ - message: cause instanceof Error ? cause.message : String(cause), cause, }), }); @@ -176,7 +183,6 @@ export const attachAcpJsonRpcConnection = ( def, new AcpRpcError({ code: msg.error.code, - message: msg.error.message, ...(msg.error.data !== undefined ? { data: msg.error.data } : {}), }), ); @@ -203,7 +209,7 @@ export const attachAcpJsonRpcConnection = ( yield* respondResult(msg.id, exit.value); } else { const left = Cause.squash(exit.cause); - yield* respondError(msg.id, left instanceof AcpRpcError ? left.message : String(left)); + yield* respondError(msg.id, Schema.is(AcpError)(left) ? left.message : String(left)); } });