CrowdControl.Protocol (crowd_control v0.1.1)

Copy Markdown View Source

Pure functions for the Claude Code stream-json wire format.

Messages are newline-delimited JSON objects on stdin/stdout.

Summary

Functions

Decodes a single JSON line into a tagged tuple.

Encodes a user prompt as a stream-json stdin message.

Splits a binary buffer into complete lines and a remainder.

Types

message()

@type message() ::
  {:system_init, map()}
  | {:assistant, map()}
  | {:user, map()}
  | {:result, String.t(), map()}
  | {:stream_event, map()}
  | {:unknown, map()}
  | {:invalid_json, String.t()}

Functions

decode_line(json_string)

@spec decode_line(binary()) :: message()

Decodes a single JSON line into a tagged tuple.

Returns one of:

  • {:system_init, map}
  • {:assistant, map}
  • {:user, map}
  • {:result, subtype, map}
  • {:stream_event, map}
  • {:unknown, map}
  • {:invalid_json, raw_line} when the input is not valid JSON

Does not raise; callers can safely pass arbitrary subprocess output.

encode_user_message(prompt)

@spec encode_user_message(binary()) :: binary()

Encodes a user prompt as a stream-json stdin message.

Returns a newline-terminated JSON string.

split_lines(buffer)

@spec split_lines(binary()) :: {[binary()], binary()}

Splits a binary buffer into complete lines and a remainder.

Returns {complete_lines, remainder} where complete_lines is a list of binaries (without the trailing newline) and remainder is the incomplete trailing fragment (possibly empty).