The stream module provides utilities for creating bidirectional ACP streams using newline-delimited JSON (NDJSON) encoding. This is the standard way to handle ACP connections over stdio and other byte-oriented transports.
import 'dart:io';import 'package:acp_dart/acp_dart.dart';void main() { // Create stream from stdin/stdout final stream = ndJsonStream(stdin, stdout); // Use with AgentSideConnection final connection = AgentSideConnection( (conn) => MyAgent(conn), stream, ); // Connection now handles all communication}
import 'dart:io';import 'package:acp_dart/acp_dart.dart';void main() { final stream = ndJsonStream(stdin, stdout); try { // This will fail if the message can't be encoded to JSON stream.writable.add({ 'invalid': Object(), // Objects can't be serialized }); } catch (e) { stderr.writeln('Failed to encode message: $e'); }}
import 'dart:async';import 'dart:io';import 'package:acp_dart/acp_dart.dart';void main() async { final stream = ndJsonStream(stdin, stdout); // Stream is active and processing messages // When done, close the writable side await stream.writable.close(); // The readable side will complete when input closes}
The ndJsonStream implementation uses Dart streams, which are single-subscription by default. Messages are processed sequentially in the order they arrive.
Buffering: The NDJSON stream automatically handles buffering and line splitting, so partial UTF-8 sequences and incomplete lines are handled correctly.Backpressure: Dart streams provide natural backpressure handling. If the consumer is slow, the stream will pause reading until it’s ready.Memory: Each message is parsed independently, so memory usage is proportional to individual message size, not total stream size.