Skip to content
words-with-agentsPublic

About

Reader-aware CLI output: detect who reads a command (human, agent, CI, pipe) and shape output and input for them.

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

readerly

npm version npm downloads CI license: MIT

docs

readerly works out who reads a CLI command's output: a person at a terminal, a coding agent, CI, a pipe, or a program that asked for JSON. It then shapes the output, and the input the command accepts, for that reader.

Docs · Get started · Readers · For agents · Formats · API

Install

pnpm add readerly

readerly requires Node 22.12 or later and has no dependencies. It is at 0.x, so minor versions can break the API.

Use

import { parseArgs } from "node:util";
import { createOut } from "readerly";

const argv = process.argv.slice(2);
const out = createOut({
  tool: "acme",
  command: "check",
  argv,
  summary: () => "in 212 files (0.8s)",
});

out.error({
  code: "ACME001",
  message: "Route handler reads process.env at module scope",
  file: "src/app.ts",
  line: 14,
  column: 3,
  why: "The value is fixed at build time",
  fix: "Read it inside the handler",
});
out.error({
  code: "ACME004",
  message: "Top-level await in a server entry",
  file: "src/db.ts",
  line: 3,
  column: 1,
  fix: "Move the call into the plugin setup",
});
out.warn({
  code: "ACME010",
  message: "Alias shadows a workspace package",
  file: "vite.config.ts",
  line: 8,
  column: 5,
});

try {
  // With no answer supplied, a person gets a prompt; an agent gets a --fix suggestion.
  const fix = await out.confirm({
    name: "fix",
    flag: "--fix",
    value: parseArgs({ args: argv, strict: false }).values.fix,
    default: false,
    message: "Apply 2 safe fixes?",
    reason: "applies 2 safe fixes",
  });
  if (fix) await applyFixes(); // your own code
  out.next("acme explain <code>", "explains one code");
  process.exitCode = out.end();
} catch (error) {
  process.exitCode = out.fail(error);
}

createOut detects the reader from the flags, the TTY and the environment, and formats the output for it.

One command, three readers

A person at a terminal

$ acme check
✖ 2 errors, 1 warning in 212 files (0.8s)

error ACME001  src/app.ts:14:3  Route handler reads process.env at module scope
├▶ why: The value is fixed at build time
╰▶ fix: Read it inside the handler
error ACME004  src/db.ts:3:1  Top-level await in a server entry
╰▶ fix: Move the call into the plugin setup
warn  ACME010  vite.config.ts:8:5  Alias shadows a workspace package
? Apply 2 safe fixes? (y/N) n

next: acme explain <code>   (explains one code)

A coding agent

$ acme check
acme check: failed. 2 errors, 1 warning in 212 files (0.8s).
error ACME001 src/app.ts:14:3 Route handler reads process.env at module scope. fix: Read it inside the handler
error ACME004 src/db.ts:3:1 Top-level await in a server entry. fix: Move the call into the plugin setup
warn ACME010 vite.config.ts:8:5 Alias shadows a workspace package
next: acme check --fix   (applies 2 safe fixes)
next: acme explain <code>   (explains one code)

GitHub Actions

::error title=ACME001,file=src/app.ts,line=14,col=3::Route handler reads process.env at module scope
::error title=ACME004,file=src/db.ts,line=3,col=1::Top-level await in a server entry
::warning title=ACME010,file=vite.config.ts,line=8,col=5::Alias shadows a workspace package

The person gets color and a prompt. The agent can't answer a prompt, so it gets a next: line with the flag that answers it. GitHub turns each ::error and ::warning line into an annotation on the pull request diff:

GitHub shows ACME004 as a check failure under line 3 of src/db.ts in a pull request diff.

--format json prints one JSON document, and --format jsonl one record per line. A test checks these samples against test/golden.

License

MIT

About

Reader-aware CLI output: detect who reads a command (human, agent, CI, pipe) and shape output and input for them.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages