@runloop/agent-axon-client
    Preparing search index...

    Class ClaudeAxonConnection

    Bidirectional, interactive client for Claude Code via Axon.

    Provides:

    Messages are yielded as SDKMessage from @anthropic-ai/claude-agent-sdk — the exact types the Claude Code CLI emits.

    Index

    Claude SDK

    axonId: string

    The Axon channel ID this connection is bound to.

    devboxId: string

    The Runloop devbox ID.

    • get isDisconnected(): boolean

      Whether the connection is not live after having been connected at least once. Returns false before the first successful connect, and true after a disconnect/teardown until connect runs again.

      Returns boolean

    • get isInitialized(): boolean

      Whether the connection has been fully initialized (transport connected, read loop active, and protocol handshake complete). Returns false before initialize resolves or after the read loop ends.

      Returns boolean

    • Aborts the underlying SSE stream without clearing registered listeners or running the onDisconnect callback.

      Unlike disconnect, listeners remain registered. Note that after calling this method, the connection cannot be reused — create a new ClaudeAxonConnection instance to reconnect.

      Returns void

    • Opens the transport and starts the background read loop.

      Call this before initialize to establish the Axon connection.

      When replay is true (the default), queries the axon for the current head sequence and replays all events up to that point without invoking handlers. Unresolved control requests are dispatched to handlers after replay completes.

      Returns Promise<void>

      If the connection is not reusable after a fatal broker error (code: "terminated").

      If the transport is already connected (code: "already_connected").

      If both replay and afterSequence are set.

    • Closes the transport, fails all pending control requests, and runs the onDisconnect callback if one was provided.

      User-registered listeners (onAxonEvent, onTimelineEvent, onControlRequest) are preserved so they keep working after another connect.

      Idempotent — subsequent calls are no-ops until the next connect.

      Returns Promise<void>

      Resolves once all teardown (including onDisconnect) completes.

    • Runs the Claude agent protocol initialize step with the Claude Code CLI.

      This is required once after connect on first startup: connect() only opens the transport and read loop; the agent does not accept prompts or control traffic until this handshake completes. If ClaudeAxonConnectionOptions.model was set, a set_model control request is sent immediately afterward.

      Returns Promise<void>

      If the connection is not reusable after a fatal broker error (code: "terminated").

      If connect has not been called yet (code: "not_connected").

      If the handshake has already completed (code: "already_initialized").

    • Interrupts the current conversation turn.

      Returns Promise<void>

      On timeout or if the CLI rejects the interrupt.

    • Registers a listener for every Axon event (before origin filtering). Useful for debugging, observability, and building Axon event viewers.

      Parameters

      Returns () => void

      An unsubscribe function that removes the listener.

    • Register a handler for incoming control requests of a given subtype.

      When Claude Code sends a control request (e.g. asking permission to use a tool), the registered handler is called instead of the built-in default. The handler receives the full SDKControlRequest and must return a complete SDKControlResponse. The connection sends it back as-is.

      Only one handler can be registered per subtype. Calling this method again with the same subtype replaces the previous handler.

      Type Parameters

      • S extends any

      Parameters

      • subtype: S

        The control request subtype to handle (e.g. "can_use_tool").

      • handler: ControlRequestHandler<S>

        Async function that processes the request and returns a full SDKControlResponse.

      Returns () => void

      connection.onControlRequest("can_use_tool", async (message) => {
      return {
      type: "control_response",
      response: {
      subtype: "success",
      request_id: message.request_id,
      response: { behavior: "allow", updatedInput: message.request.input },
      },
      };
      });
    • Publishes a custom event to the Axon channel.

      The event will appear in the SSE stream and be classified by the timeline (typically as kind: "unknown" unless the event_type matches a known Claude protocol message type).

      Parameters

      • params: AxonPublishParams

        The event to publish (same shape as Axon.publish()).

      Returns Promise<PublishResultView>

      The publish result with sequence number and timestamp.

    • Async iterator that yields every SDKMessage from Claude indefinitely.

      Does not stop at result messages — use receiveAgentResponse for single-turn convenience. Iteration ends when the transport closes or disconnect is called.

      Returns AsyncGenerator<SDKMessage, void, undefined>

      Each SDKMessage as it arrives from the agent.

    • Async iterator that yields SDKMessages until (and including) a result message, then terminates automatically.

      Convenient for single-turn usage: send a prompt, iterate receiveAgentResponse(), and the loop ends when Claude finishes.

      Returns AsyncGenerator<SDKMessage, void, undefined>

      Each SDKMessage up to and including the result.

      await conn.send("Summarize this file.");
      for await (const msg of conn.receiveAgentResponse()) {
      console.log(msg.type, msg);
      }
    • Sends a single user message to Claude.

      If given a plain string, it is automatically wrapped into an SDKUserMessage with role: "user". Pass a pre-built SDKUserMessage for full control (e.g. to set parent_tool_use_id).

      Parameters

      • prompt: string | SDKUserMessage

        A string message or a fully formed SDKUserMessage.

      Returns Promise<void>

      If not connected (code: "not_connected") or after a fatal error (code: "terminated").

      await conn.send("What files are in this directory?");
      
    • Changes the AI model used by Claude Code.

      Parameters

      • model: string | null

        Anthropic model identifier (e.g. "claude-sonnet-4-5"), or null to revert to the agent's default.

      Returns Promise<void>

      On timeout or if the CLI rejects the model change.