ClaudeSdk
Transport for Claude Code: one query() from @anthropic-ai/claude-agent-sdk
in streaming-input mode, as an Effect service.
import { ClaudeSdk } from "@jpowersdev/effect-agent/claude"ClaudeSdk.layer({ record: "/tmp/run.jsonl" })open spawns the CLI, waits for its initialize handshake and returns a
ClaudeQuery: push user messages, read raw frames, interrupt, end. Closing
the Scope closes the query (the SDK ends stdin, then kills the process after
a short grace), waits for the frame pump to finish, then kills the process
group, so commands the agent left running stop too.
The CLI is spawned here (the SDK’s spawnClaudeCodeProcess), detached into
its own process group, with the command wrapped by a Launcher.Wrap.
Claude Code’s Bash tool starts each command in a new session (its own
process group), so killing the CLI’s group alone would leave (cmd &)
running. The CLI is also given a unique EFFECT_AGENT_PROCESS variable,
which every process it starts inherits, and on Linux processes carrying it
are killed too.
status and history are the account and transcript reads ClaudeAgent
needs outside a session.
With record, every frame (and every status/history answer, and every
SDK hook call with what it returned) is appended to a JSONL file that
ClaudeReplay.layer can play back without a process.
Since v0.1.0
Errors
Section titled “Errors”ClaudeSdkError (class)
Section titled “ClaudeSdkError (class)”A failure of an SDK call, the CLI process or a replay.
Signature
declare class ClaudeSdkErrorSince v0.1.0
Layers
Section titled “Layers”ClaudeSdk backed by the SDK and a real CLI process.
Signature
declare const layer: (options?: LayerOptions) => Layer.Layer<ClaudeSdk>Since v0.1.0
Models
Section titled “Models”ClaudeQuery (interface)
Section titled “ClaudeQuery (interface)”An open query, as returned by ClaudeSdk.open.
Signature
export interface ClaudeQuery { /** * Raw frames, buffered for the life of the query. Single consumer, but safe * to stop and re-read. Ends when the process exits; fails if it dies. */ readonly frames: Stream.Stream<Frame, ClaudeSdkError> /** Queue a user message (set `uuid` and `priority` yourself). */ readonly send: (message: SDKUserMessage) => Effect.Effect<void, ClaudeSdkError> readonly interrupt: Effect.Effect<void, ClaudeSdkError> /** End the input stream: the CLI finishes queued work and exits. */ readonly endInput: Effect.Effect<void> /** Close the query now; the process is stopped. */ readonly close: Effect.Effect<void> /** The CLI process's pid (the wrapped command's); none without a process (replay). */ readonly pid: Option.Option<number> /** * SIGKILL the process group (and marked processes) now. The frame stream then fails * as for any process death. */ readonly kill: Effect.Effect<void>}Since v0.1.0
CommandLifecycle
Section titled “CommandLifecycle”command_lifecycle frames (capability msg_lifecycle_v1). Not part of the
SDK’s SDKMessage union, so frames are checked against this with decodeLifecycle.
Signature
declare const CommandLifecycle: Schema.Struct<{ readonly type: Schema.Literal<"command_lifecycle"> readonly command_uuid: Schema.String readonly state: Schema.Literals< readonly ["queued", "started", "completed", "cancelled", "discarded", "refused"] > readonly uuid: Schema.optionalKey<Schema.String> readonly session_id: Schema.optionalKey<Schema.String>}>Since v0.1.0
CommandLifecycle (type alias)
Section titled “CommandLifecycle (type alias)”Signature
type CommandLifecycle = typeof CommandLifecycle.TypeSince v0.1.0
Frame (type alias)
Section titled “Frame (type alias)”Anything the CLI writes to us.
Signature
type Frame = SDKMessage | CommandLifecycleSince v0.1.0
HistorySnapshot (interface)
Section titled “HistorySnapshot (interface)”A stored session: its metadata (when the SDK found it) and transcript messages (system messages included).
Signature
export interface HistorySnapshot { readonly info: SDKSessionInfo | null readonly messages: ReadonlyArray<SessionMessage>}Since v0.1.0
Launch (interface)
Section titled “Launch (interface)”How to start the CLI process.
Signature
export interface Launch { /** From `Launcher.prepare`; applied to the SDK's command and args at spawn time. */ readonly wrap: Launcher.Wrap}Since v0.1.0
LayerOptions (interface)
Section titled “LayerOptions (interface)”Signature
export interface LayerOptions { /** Append every frame to this JSONL file (see `RecordLine`). */ readonly record?: string}Since v0.1.0
LifecycleState
Section titled “LifecycleState”A command_lifecycle frame’s state.
Signature
declare const LifecycleState: Schema.Literals< readonly ["queued", "started", "completed", "cancelled", "discarded", "refused"]>Since v0.1.0
LifecycleState (type alias)
Section titled “LifecycleState (type alias)”Signature
type LifecycleState = typeof LifecycleState.TypeSince v0.1.0
RecordLine (type alias)
Section titled “RecordLine (type alias)”One line of a JSONL recording.
in,outandctl interrupt: the frames themselves.ctl openandctl end: session boundaries.error: a thrown transport error.hook: one call of an SDK hook callback (Options.hooks). Its event, the input the CLI passed, what the callback returned, andposition: the number ofinframes the session had received when the CLI called it. It is written when the callback returns, so it may come after later lines; replay goes byposition.
Signature
type RecordLine = | { readonly dir: "out"; readonly frame: SDKUserMessage } | { readonly dir: "in"; readonly frame: Frame } | { readonly dir: "ctl"; readonly call: "interrupt" | "end" } | { readonly dir: "ctl"; readonly call: "open"; readonly options: RecordedOpen } | { readonly dir: "hook" readonly event: HookEvent readonly position: number readonly input: unknown readonly output: unknown } | { readonly dir: "status"; readonly status: StatusSnapshot } | { readonly dir: "history" readonly sessionId: string readonly cwd: string readonly history: HistorySnapshot | null } | { readonly dir: "error" readonly error: { readonly operation: string readonly message: string readonly errorClass: string | null readonly signal: string | null readonly exitCode: number | null } }Since v0.1.0
RecordedOpen (interface)
Section titled “RecordedOpen (interface)”The subset of Options written to a recording’s open line (for id remapping on replay).
Signature
export interface RecordedOpen { readonly sessionId?: string readonly resume?: string readonly model?: string}Since v0.1.0
SessionLookup (interface)
Section titled “SessionLookup (interface)”A stored session to look for, as taken by sessionExists and history.
Signature
export interface SessionLookup { readonly sessionId: string readonly cwd: string /** Where the transcript would be (see `transcriptPath`). */ readonly transcript: string}Since v0.1.0
StatusSnapshot (interface)
Section titled “StatusSnapshot (interface)”What status read from an idle CLI.
Signature
export interface StatusSnapshot { readonly account: AccountInfo /** The experimental usage API's answer, or null if it failed (see `usageError`). */ readonly usage: SDKControlGetUsageResponse | null readonly usageError: string | null}Since v0.1.0
Services
Section titled “Services”ClaudeSdk (class)
Section titled “ClaudeSdk (class)”The transport: layer spawns the real CLI, ClaudeReplay.layer plays back a recording.
Signature
declare class ClaudeSdkSince v0.1.0