Skip to content

feat(source): add Amp adapter #59

@ayagmar

Description

@ayagmar

Objective

Add an amp source adapter for local Amp thread files with support for both aggregated ledger usage and per-message fallback parsing.

Source shape to support

  • directory-backed JSON source under ~/.local/share/amp/threads/
  • thread files named T-*.json
  • preferred usage source is an aggregated ledger/event list when present
  • fallback usage source is assistant message usage embedded in the same thread file
  • explicit credits/cost may already be present in the source data

Implementation plan

  1. Adapter file

    • add src/sources/amp/amp-source-adapter.ts
    • implement SourceAdapter with id = 'amp'
  2. Discovery

    • use discoverFiles(..., { extension: '.json' }) plus a T-*.json filename filter or equivalent deterministic discovery helper
    • keep the adapter directory-backed and reachable with --source-dir amp=...
  3. Parsing

    • parse the thread JSON once per file
    • prefer aggregated ledger/events when available because they represent the cleanest usage signal
    • if no usable ledger events exist, fall back to assistant message usage records from the same file
    • preserve explicit cost when present
    • infer provider from model names conservatively
    • use thread-created time and then file mtime as timestamp fallbacks when event timestamps are missing
    • skip zero-token records so empty bookkeeping rows do not create usage events
    • use parseFileWithDiagnostics() if malformed thread files or invalid usage sections should surface skipped-row reasons
  4. Wiring

    • register amp in src/sources/create-default-adapters.ts
    • rely on generic --source-dir support rather than adding a dedicated path flag
    • update supported-source registry/help expectations
  5. Tests and docs

    • add tests/sources/amp-source-adapter.test.ts
    • cover both ledger-first and message-fallback behavior
    • update shared registry/help tests and README.md

Code paths to touch

  • src/sources/amp/amp-source-adapter.ts
  • src/sources/create-default-adapters.ts
  • src/utils/discover-files.ts
  • src/sources/parsing-utils.ts
  • src/domain/usage-event.ts
  • tests/sources/amp-source-adapter.test.ts
  • tests/sources/create-default-adapters.test.ts
  • tests/cli/create-cli.test.ts
  • README.md

Verification

  • parser fixtures cover:
    • ledger-backed usage events
    • assistant-message fallback when no usable ledger exists
    • timestamp fallback order
    • zero-token rows skipped
    • explicit cost preserved
  • llm-usage daily --source amp --source-dir amp=<fixture-dir> works
  • supported-source help output and registry tests are updated
  • full validation passes:
    • pnpm run lint
    • pnpm run typecheck
    • pnpm run test
    • pnpm run format:check

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions