generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 29
refactor: convert printer to hook-based implementation #277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
zastrowm
wants to merge
4
commits into
main
Choose a base branch
from
agent-tasks/270
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9d64f81
refactor: convert printer to hook-based implementation
strands-agent 5f532d7
refactor: add ContentBlockHook for better hook architecture
strands-agent 3de350b
refactor: use BaseContentBlock for better type discrimination
strands-agent 8fc0a89
Additional changes from write operations
strands-agent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
| import type { AgentStreamEvent } from '../types/agent.js' | ||
| import type { HookProvider } from '../hooks/types.js' | ||
| import type { HookRegistry } from '../hooks/registry.js' | ||
| import { ContentBlockHook, ModelStreamEventHook } from '../hooks/events.js' | ||
|
|
||
| /** | ||
| * Creates a default appender function for the current environment. | ||
|
|
@@ -15,28 +18,10 @@ export function getDefaultAppender(): (text: string) => void { | |
| } | ||
|
|
||
| /** | ||
| * Interface for printing agent activity to a destination. | ||
| * Implementations can output to stdout, console, HTML elements, etc. | ||
| */ | ||
| export interface Printer { | ||
| /** | ||
| * Write content to the output destination. | ||
| * @param content - The content to write | ||
| */ | ||
| write(content: string): void | ||
|
|
||
| /** | ||
| * Process a streaming event from the agent. | ||
| * @param event - The event to process | ||
| */ | ||
| processEvent(event: AgentStreamEvent): void | ||
| } | ||
|
|
||
| /** | ||
| * Default implementation of the Printer interface. | ||
| * Default implementation of agent output printing. | ||
| * Outputs text, reasoning, and tool execution activity to the configured appender. | ||
| */ | ||
| export class AgentPrinter implements Printer { | ||
| export class AgentPrinter implements HookProvider { | ||
| private readonly _appender: (text: string) => void | ||
| private _inReasoningBlock: boolean = false | ||
| private _toolCount: number = 0 | ||
|
|
@@ -50,6 +35,31 @@ export class AgentPrinter implements Printer { | |
| this._appender = appender | ||
| } | ||
|
|
||
| /** | ||
| * Register callback functions for specific event types. | ||
| * @param registry - The hook registry to register callbacks with | ||
| */ | ||
| public registerCallbacks(registry: HookRegistry): void { | ||
| registry.addCallback(ModelStreamEventHook, this.handleModelStreamEvent) | ||
| registry.addCallback(ContentBlockHook, this.handleContentBlock) | ||
| } | ||
|
|
||
| /** | ||
| * Handle model stream events and process them for printing. | ||
| * @param event - The model stream event to handle | ||
| */ | ||
| private handleModelStreamEvent = (event: ModelStreamEventHook): void => { | ||
|
||
| this.processEvent(event.event) | ||
| } | ||
|
|
||
| /** | ||
| * Handle content block events and process them for printing. | ||
| * @param event - The content block event to handle | ||
| */ | ||
| private handleContentBlock = (event: ContentBlockHook): void => { | ||
| this.processEvent(event.block) | ||
| } | ||
|
|
||
| /** | ||
| * Write content to the output destination. | ||
| * @param content - The content to write | ||
|
|
@@ -63,7 +73,7 @@ export class AgentPrinter implements Printer { | |
| * Handles text deltas, reasoning content, and tool execution events. | ||
| * @param event - The event to process | ||
| */ | ||
| public processEvent(event: AgentStreamEvent): void { | ||
| private processEvent(event: AgentStreamEvent): void { | ||
| switch (event.type) { | ||
| case 'modelContentBlockDeltaEvent': | ||
| this.handleContentBlockDelta(event) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than doing this much work, let's add a base class to all content blocks,
BaseContentBlockfor now. Make it non-public and add a TODO to the code to api-bar-raise it.Then do the type check here for that using instanceof rather than this event.type checking