Skip to content

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

A failure of an SDK call, the CLI process or a replay.

Signature

declare class ClaudeSdkError

Source

Since v0.1.0

ClaudeSdk backed by the SDK and a real CLI process.

Signature

declare const layer: (options?: LayerOptions) => Layer.Layer<ClaudeSdk>

Source

Since v0.1.0

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>
}

Source

Since v0.1.0

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>
}>

Source

Since v0.1.0

Signature

type CommandLifecycle = typeof CommandLifecycle.Type

Source

Since v0.1.0

Anything the CLI writes to us.

Signature

type Frame = SDKMessage | CommandLifecycle

Source

Since v0.1.0

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>
}

Source

Since v0.1.0

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
}

Source

Since v0.1.0

Signature

export interface LayerOptions {
/** Append every frame to this JSONL file (see `RecordLine`). */
readonly record?: string
}

Source

Since v0.1.0

A command_lifecycle frame’s state.

Signature

declare const LifecycleState: Schema.Literals<
readonly ["queued", "started", "completed", "cancelled", "discarded", "refused"]
>

Source

Since v0.1.0

Signature

type LifecycleState = typeof LifecycleState.Type

Source

Since v0.1.0

One line of a JSONL recording.

  • in, out and ctl interrupt: the frames themselves.
  • ctl open and ctl 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, and position: the number of in frames 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 by position.

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
}
}

Source

Since v0.1.0

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
}

Source

Since v0.1.0

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
}

Source

Since v0.1.0

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
}

Source

Since v0.1.0

The transport: layer spawns the real CLI, ClaudeReplay.layer plays back a recording.

Signature

declare class ClaudeSdk

Source

Since v0.1.0