Skip to content

feat: add --format json option to canonical command - #117

Merged
ErikBjare merged 2 commits into
ActivityWatch:masterfrom
TimeToBuildBob:feat/canonical-json-output
Aug 23, 2026
Merged

feat: add --format json option to canonical command#117
ErikBjare merged 2 commits into
ActivityWatch:masterfrom
TimeToBuildBob:feat/canonical-json-output

Conversation

@TimeToBuildBob

Copy link
Copy Markdown
Contributor

Summary

Add JSON output support to the 'canonical' subcommand to enable machine-readable access to canonical events.

Motivation

This addresses the ActivityWatch × AI agent enablement workflow, allowing AI agents to consume categorized activity data without reimplementing the canonical query. The feature was documented in the agent enablement docs but the CLI flag implementation was missing.

Changes

  • Add --format option to canonical command with choices: 'table' (default, preserving existing behavior) and 'json'
  • JSON output emits an array of events with ISO-8601 formatted timestamps
  • Each event includes: timestamp (ISO-8601), duration (seconds), and data object
  • Preserves existing table output format and behavior

Usage

# Human-readable table format (default)
aw-client canonical myhost

# Machine-readable JSON for AI agents
aw-client canonical myhost --format json | jq '.[] | {timestamp, data}'

Testing

The change is backward compatible:

  • Default behavior (table format) is unchanged
  • No breaking changes to existing CLI
  • JSON output follows the same event schema used by aw-core models

Add JSON output support to the 'canonical' subcommand to enable
machine-readable access to canonical events. This supports the
ActivityWatch × AI agent enablement workflow, allowing agents to
consume categorized activity data without reimplementing the canonical
query.

- Add --format option with 'table' (default) and 'json' choices
- JSON output emits event array with ISO-8601 timestamps
- Preserves existing table format behavior (human-readable)

Closes: ActivityWatch/aw-client contribution for agent enablement.
Reference: https://github.com/ActivityWatch/aw-client/issues (agent enablement docs)
@greptile-apps

greptile-apps Bot commented Aug 20, 2026

Copy link
Copy Markdown

Greptile Summary

The canonical command now supports machine-readable JSON output while retaining table output as the default.

  • Adds a --format option with table and json choices.
  • Serializes timestamps as ISO-8601 strings and durations as numeric seconds.
  • Keeps the existing human-readable table path unchanged.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains; the previously reported duration serialization issue is fixed by converting each event duration to seconds before JSON encoding.

Important Files Changed

Filename Overview
aw_client/cli.py Adds JSON formatting to the canonical command and correctly converts event durations to serializable numeric seconds.

Reviews (2): Last reviewed commit: "fix(cli): serialize canonical durations ..." | Re-trigger Greptile

Comment thread aw_client/cli.py Outdated
json_events = [
{
"timestamp": e.timestamp.isoformat(),
"duration": e.duration,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Duration breaks JSON serialization

When a canonical query returns at least one event, e.duration is a timedelta, so passing it directly to json.dumps raises TypeError instead of producing the advertised JSON array.

Suggested change
"duration": e.duration,
"duration": e.duration.total_seconds(),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in fb7cee7: canonical JSON now serializes Event.duration via total_seconds(). I also formatted the option declaration so Black passes. Verified the JSON path with a non-empty event (numeric 1.5 seconds), plus 7 unit tests, mypy, and Black locally.

@TimeToBuildBob

Copy link
Copy Markdown
Contributor Author

@greptileai review

@ErikBjare
ErikBjare merged commit 101b803 into ActivityWatch:master Aug 23, 2026
8 checks passed
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