cross.stream / docs
GitHub Discord

Services

cross.stream services use Nushell closures to create streams of data that are emitted as frames into the store.

You can also compose external request-response servers, particularly CLI tools that read requests on stdin and write responses to stdout, using duplex services.

Basic Usage

To create a service, append a Nushell script that evaluates to a configuration record with a run closure using the topic xs.service.<name>.create:

r#'{
  run: {|| ^tail -F http.log | lines }
}'# | .append xs.service.log.create

The service will:

Data channels

TopicDirectionDescription
<name>.recvsystem -> streamOutput value emitted by the service
<name>.senduser -> service stdinInput fed to a duplex service

Both live in the user/app namespace, not under xs.. They are app data, not lifecycle.

Configuration Options

OptionTypeDefaultDescription
duplexbooleanfalseEnable sending input to the service's pipeline via <name>.send
restartstringon-successWhat to do when the pipeline drains cleanly: on-success runs it again, never stops it (see Auto-restart)
return_optionsrecord,Customize output frames (see Return Options)

The return_options field controls the suffix and TTL for the .recv frames produced by the service.

Return Options

OptionTypeDefaultDescription
suffixstring".recv"Suffix appended to the service name for output frames. Include the leading . (e.g., ".output" produces <name>.output)
ttlstring"forever"Time-to-live for output frames
targetstringomittedDefault (omitted): output must be a record, stored as frame metadata. Set to "cas" to store in CAS; any type is accepted.

TTL Options

Example with custom return options:

r#'{
  run: {|| ^tail -F access.log | lines },
  return_options: {
    suffix: ".line",
    ttl: "last:100"  # Keep only last 100 log lines
  }
}'# | .append xs.service.logs.create

This service emits frames with the topic logs.line and automatically maintains only the 100 most recent log entries.

Built-in Store Commands

Inside the run closure a service has the same store helper commands as actions and actors:

Modules

Services can use modules registered via xs.module.<name> topics. A service sees the modules as they existed when it was created. See Module Topics for details.

r#'{
  run: {||
    use my-parser
    ^tail -F data.log | lines | each {|line| my-parser parse $line }
  }
}'# | .append xs.service.log.create

Bi-directional Communication

When duplex is enabled, you can send data into the service's input pipeline via <name>.send frames:

# Create a websocket connection
r#'{
  run: {|| websocat wss://echo.websocket.org | lines },
  duplex: true
}'# | .append xs.service.echo.create

# Send input to the websocket: note the "\n", wss://echo.websocket.org won't
# reply until it sees a complete line.
"hello\n" | .append echo.send

When running this service:

Auto-restart and hot-reload

When the script's pipeline runs to completion without error, the service waits one second and restarts the pipeline. No lifecycle frame is emitted for this internal restart, the service is still considered "running." This is the default restart: "on-success" policy.

A pipeline that errors is not restarted under any policy. The service emits xs.service.<name>.fin.error and stays down. An external command that exits non-zero counts as an error, even mid-pipeline, as in ^my-tool | lines | each { from json }.

restart: "never" makes a clean completion terminal. The service emits xs.service.<name>.fin.ok and stops, and it stays down across xs restarts like any other fin.*. Use it when the pipeline ending is the point, such as a shell the user exited, rather than a source that went away:

r#'{
  restart: "never",
  run: {|| ^my-shell | lines }
}'# | .append xs.service.shell.create

To run it again, append a new xs.service.<name>.create.

Appending a new xs.service.<name>.create frame while the service is running reloads it with the new script:

Lifecycle

Services share the unified lifecycle vocabulary documented in the lifecycle reference. In summary:

User inputRuntime ack on successRuntime ack on failure
xs.service.<name>.createxs.service.<name>.activexs.service.<name>.invalid
xs.service.<name>.termxs.service.<name>.fin.term,

Plus the runtime emits:

See the lifecycle reference for the full event list, the compaction algorithm, and the invariants the dispatcher honors.