import * as Data from "effect/Data"; import * as Effect from "effect/Effect"; export class AgentFailure extends Data.TaggedError("AgentFailure")<{ message: string; }> {} // Native RPCs do not transport Effect errors or guarantee cancellation. Each // caller supplies its public failure and rechecks authorization after yielding. export function agentCall( operation: () => PromiseLike, message = "Personal runtime unavailable", ) { return Effect.tryPromise({ try: operation, catch: () => new AgentFailure({ message }), }); } export function agentValidation(validate: () => A) { return Effect.try({ try: validate, catch: (error) => error instanceof AgentFailure ? error : new AgentFailure({ message: error instanceof Error ? error.message : "Invalid operation", }), }); } export function runAgent( program: Effect.Effect, signal?: AbortSignal, ): Promise { if (signal?.aborted) return Promise.reject( new AgentFailure({ message: "Operation interrupted" }), ); return Effect.runPromise(program, { signal }); }