Skip to content

Add field projection (-o) to read commands for pipe-friendly column output#199

Merged
alexkroman merged 1 commit into
mainfrom
claude/zen-lovelace-dmwg4n
Jun 16, 2026
Merged

Add field projection (-o) to read commands for pipe-friendly column output#199
alexkroman merged 1 commit into
mainfrom
claude/zen-lovelace-dmwg4n

Conversation

@alexkroman

Copy link
Copy Markdown
Collaborator

Adds a -o/--output field projection option to list and account read commands, letting users extract specific fields from JSON responses without external jq. This replaces common patterns like assembly transcripts list --json | jq '.[].id' with simpler assembly transcripts list -o id.

Key changes

  • New aai_cli/core/project.py module implements field projection:

    • parse_fields() parses comma-separated -o specs with whitespace trimming
    • _lookup() resolves dotted paths (e.g., transform.model) into nested objects
    • render_value() formats scalars and containers for pipe-friendly output (JSON booleans as lowercase true/false, nested objects as compact JSON)
    • project_record(), project_rows(), project_any() dispatch on data shape (single object vs. list) and emit tab-separated columns
  • Updated aai_cli/ui/output.py:

    • emit() now accepts optional fields parameter; -o takes precedence over --json
    • New emit_fields() function prints projected lines to stdout
  • New aai_cli/options.py helper fields_option() provides the shared -o/--output option definition

  • Integrated into read commands:

    • aai_cli/commands/account.py: balance, limits, usage
    • aai_cli/commands/sessions.py: list, get
    • aai_cli/commands/transcripts.py: list
    • aai_cli/commands/keys.py: list
    • aai_cli/commands/audit.py: audit
  • Updated help text and examples across all affected commands to showcase -o usage instead of jq pipelines

  • Comprehensive test coverage:

    • tests/test_project.py: unit tests for parsing, lookup, rendering, and projection logic
    • Integration tests in test_account_command.py, test_sessions_command.py, test_transcripts.py, test_keys.py, test_audit_command.py verify end-to-end behavior
    • Snapshot updates reflect new -o option in help text

Implementation details

  • Tab-separated output (\t) makes results compatible with cut -f and spreadsheet paste
  • Missing fields and null values render as empty columns (no error)
  • Nested objects/lists re-serialize as compact JSON, not Python repr()
  • -o precedence: fields > --json > human renderer
  • Dotted paths enable access to nested fields without requiring the full JSON structure

https://claude.ai/code/session_01GmpgwWwuRStqTN3ZuPE8BE

Read commands that previously only had --json (transcripts list, sessions
list/get, balance, usage, limits, keys list, audit) now accept -o FIELDS to
project columns straight out of the JSON, so a "grab one column" pipeline no
longer needs an external jq.

- core/project.py: dependency-free projection — comma-separated field specs,
  dotted paths into nested objects, pipe-friendly scalar rendering (None -> empty
  column, JSON booleans lowercased, nested values re-serialized as JSON), and a
  shape-aware dispatch (list -> one line per row, object -> one line).
- output.emit gains a `fields` arg that takes precedence over --json; the shared
  options.fields_option() factory keeps the -o contract uniform across commands.
- Shipped epilog examples and REFERENCE.md drop the `--json | jq` column grabs in
  favor of `-o`.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GmpgwWwuRStqTN3ZuPE8BE
@alexkroman alexkroman enabled auto-merge June 16, 2026 22:57
@alexkroman alexkroman added this pull request to the merge queue Jun 16, 2026
Merged via the queue into main with commit 2de6b3d Jun 16, 2026
19 checks passed
@alexkroman alexkroman deleted the claude/zen-lovelace-dmwg4n branch June 16, 2026 23:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants