Skip to content

Add --record flag to record AI API interactions#1091

Merged
dgageot merged 6 commits intodocker:mainfrom
stanislavHamara:cagent-record-mode
Dec 15, 2025
Merged

Add --record flag to record AI API interactions#1091
dgageot merged 6 commits intodocker:mainfrom
stanislavHamara:cagent-record-mode

Conversation

@stanislavHamara
Copy link
Copy Markdown
Contributor

Summary

This PR introduces a --record flag to cagent run, cagent exec, and cagent api commands, enabling users to record AI API interactions to cassette files. These recordings can later be replayed using the existing --fake flag for testing and demo purposes.

Features

  • --record flag for exec command: Record a single question/answer interaction

    cagent exec agent.yaml "What's 2+2?" --record=my-recording
    # Creates my-recording.yaml cassette file
  • --record flag for run command: Record an interactive TUI session

    cagent run agent.yaml --record
    # Auto-generates cagent-recording-<timestamp>.yaml
  • --record flag for api command: Record API server interactions

    cagent api agent.yaml --record=api-recording
  • Auto-generated filenames: When --record is used without a value, a timestamped filename is generated automatically

  • Mutual exclusivity: --fake and --record flags are mutually exclusive on the api command (you can't replay and record at the same time)

Implementation Details

  • Added StartRecordingProxy function in pkg/fake/proxy.go that configures VCR in record mode
  • Extracted APIKeyHeaderUpdater to inject real API keys during recording
  • Created shared setupRecordingProxy helper in cmd/root/record.go to eliminate duplication
  • The recording proxy intercepts requests, forwards them to real AI APIs with proper authentication, and saves responses to cassette files

Testing

Added comprehensive tests:

  • Unit tests (pkg/fake/proxy_test.go): APIKeyHeaderUpdater and TargetURLForHost functions
  • Unit tests (cmd/root/record_test.go): setupRecordingProxy helper function
  • E2E tests (e2e/cagent_record_test.go):
    • TestExec_Record_CreatesCassette - Verifies cassette creation with explicit path
    • TestExec_Record_AutoGeneratesFilename - Verifies auto-generated filenames
    • TestAPI_FakeAndRecord_MutuallyExclusive - Verifies flag mutual exclusivity

Files Changed

File Change
cmd/root/run.go Added --record flag and integration
cmd/root/exec.go Added --record flag
cmd/root/api.go Added --record flag with --fake mutual exclusivity
cmd/root/record.go New shared helper for recording proxy setup
cmd/root/record_test.go Unit tests for record helper
pkg/fake/proxy.go Added StartRecordingProxy and APIKeyHeaderUpdater
pkg/fake/proxy_test.go Unit tests for proxy functions
e2e/cagent_record_test.go E2E tests for --record flag
e2e/proxy_test.go Refactored to use shared APIKeyHeaderUpdater

@stanislavHamara stanislavHamara requested a review from a team as a code owner December 15, 2025 10:30
@dgageot dgageot merged commit 5a41b0e into docker:main Dec 15, 2025
5 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