Skip to content

refactor(agent): convert printer to plugin pattern - #45

Open
zastrowm wants to merge 1 commit into
mainfrom
agent-tasks/44
Open

refactor(agent): convert printer to plugin pattern#45
zastrowm wants to merge 1 commit into
mainfrom
agent-tasks/44

Conversation

@zastrowm

@zastrowm zastrowm commented Mar 12, 2026

Copy link
Copy Markdown
Owner

Summary

Refactors AgentPrinter from a standalone interface-based implementation to a first-class Plugin. This aligns with the existing plugin architecture and enables printer functionality to be cleanly integrated into the agent's hook system.

Key Changes

printer.ts

  • Removed Printer interface - No longer needed with plugin-based approach
  • AgentPrinter now implements Plugin interface with:
    • name property: 'strands:printer'
    • initAgent() method that registers callbacks via agent.addHook()
  • Hooks registered:
    • ModelStreamUpdateEvent → handles streaming text/reasoning output
    • ToolResultEvent → handles tool completion status

agent.ts

  • Removed _printer field - No longer managed separately
  • Printer registered as plugin in PluginRegistry when enabled
  • Removed manual processEvent() calls - Now handled automatically by hook system

Test Updates

  • Updated printer configuration tests to use plugin pattern
  • Replaced (agent as any)._printer access with proper plugin injection
  • Use vi.spyOn(process.stdout, 'write') for stdout capture tests

Why This Approach

  1. Consistency: Aligns with existing plugin architecture (e.g., ConversationManager, SessionManager)
  2. Cleaner separation: Printer logic decoupled from Agent internals
  3. Extensibility: Users can create custom printers by implementing Plugin
  4. Testability: Plugin injection allows for clean test mocking

Resolves #44

Comment thread src/agent/__tests__/agent.test.ts Outdated
Comment thread src/agent/__tests__/agent.test.ts Outdated
@zastrowm

Copy link
Copy Markdown
Owner Author

/strands

@github-actions

Copy link
Copy Markdown

Issue: PR template has a "Documentation PR" section that should be included.

Suggestion: Add a "Documentation PR" section with a brief justification. Since this is an internal refactor with no public API changes, a simple note like "Not required - internal refactor with no public API changes" would suffice.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Assessment: Comment

Clean refactor that correctly converts the printer to the Plugin pattern, improving architectural consistency.

Review Summary
  • Implementation: AgentPrinter properly implements Plugin interface with name getter and initAgent() method. Good cleanup removing the unused Printer interface.
  • Testing: Author has already identified the need to use vitest mocking instead of manually modifying process.stdout.write (lines 486, 508) - agree this should be addressed for consistency with repo patterns.
  • Documentation: PR description is missing the "Documentation PR" section from the template. Since this is an internal refactor with no public API changes, a brief justification note would be sufficient.

The Plugin implementation follows established patterns well. Once the testing improvements noted by the author are addressed, this looks good to merge.

@zastrowm

Copy link
Copy Markdown
Owner Author

/strands merge in main and fix the conflicts

@zastrowm

Copy link
Copy Markdown
Owner Author

/strands - there's a bunch of changes that are not related to this PR; can you identify what happened and reverse the changes

- Remove Printer interface from printer.ts
- AgentPrinter now implements Plugin interface with:
  - name property: 'strands:printer'
  - initAgent method that registers callbacks via agent.addHook()
- Remove _printer field from Agent class
- Register printer as a plugin in PluginRegistry when enabled
- Remove manual processEvent calls (handled by hooks system)
- Update tests to use plugin pattern with vi.spyOn for stdout capture

Resolves #44
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.

Convert printer to a hook

2 participants