This commit is contained in:
Julius Marminge committed 2026-04-17 16:43:47 -07:00
1 parent e39fa96421
commit 8c5fc4e690
3 files changed
+67 -33

No files matched your search

@@ -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"))
);
}
+46 -19
View File
@@ -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>()("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>()("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>()("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>()(
"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;
@@ -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<ChildProcessWithoutNullStreams, AcpSpawnError, never> {
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));
}
});