Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 136
chore: Replace OnSnapshot callback with Emitter interface#185
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
78579bd
Replace OnSnapshot callback with Emitter interface
johnstcn bc79eda
refactor: use functional options for NewEventEmitter
johnstcn 0fa554d
Replace OnSnapshot callback with Emitter interface
johnstcn 1cd4dca
address PR review comments
johnstcn a116d63
use uint for subscription buffer size
johnstcn ca5d5d4
Merge branch 'event-emitter-7z1e' into cj/refactor/event-emitter
johnstcn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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
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 |
|---|---|---|
| @@ -68,9 +68,7 @@ type PTYConversationConfig struct { | ||
| FormatToolCall func(message string) (string, []string) | ||
| // InitialPrompt is the initial prompt to send to the agent once ready | ||
| InitialPrompt []MessagePart | ||
| // OnSnapshot is called after each snapshot with current status, messages, and screen content | ||
| OnSnapshot func(status ConversationStatus, messages []ConversationMessage, screen string) | ||
| Logger *slog.Logger | ||
| Logger *slog.Logger | ||
| } | ||
| func (cfg PTYConversationConfig) getStableSnapshotsThreshold() int { | ||
| @@ -86,7 +84,8 @@ func (cfg PTYConversationConfig) getStableSnapshotsThreshold() int { | ||
| // PTYConversation is a conversation that uses a pseudo-terminal (PTY) for communication. | ||
| // It uses a combination of polling and diffs to detect changes in the screen. | ||
| type PTYConversation struct { | ||
| cfg PTYConversationConfig | ||
| cfg PTYConversationConfig | ||
| emitter Emitter | ||
| // How many stable snapshots are required to consider the screen stable | ||
| stableSnapshotsThreshold int | ||
| snapshotBuffer *RingBuffer[screenSnapshot] | ||
| @@ -115,13 +114,23 @@ type PTYConversation struct { | ||
| var _ Conversation = &PTYConversation{} | ||
| func NewPTY(ctx context.Context, cfg PTYConversationConfig) *PTYConversation { | ||
| type noopEmitter struct{} | ||
| func (noopEmitter) EmitMessages([]ConversationMessage) {} | ||
| func (noopEmitter) EmitStatus(ConversationStatus) {} | ||
| func (noopEmitter) EmitScreen(string) {} | ||
| func NewPTY(ctx context.Context, cfg PTYConversationConfig, emitter Emitter) *PTYConversation { | ||
| if cfg.Clock == nil { | ||
| cfg.Clock = quartz.NewReal() | ||
| } | ||
| if emitter == nil { | ||
| emitter = noopEmitter{} | ||
| } | ||
| threshold := cfg.getStableSnapshotsThreshold() | ||
| c := &PTYConversation{ | ||
| cfg: cfg, | ||
| emitter: emitter, | ||
| stableSnapshotsThreshold: threshold, | ||
johnstcn marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| snapshotBuffer: NewRingBuffer[screenSnapshot](threshold), | ||
| messages: []ConversationMessage{ | ||
| @@ -139,9 +148,6 @@ func NewPTY(ctx context.Context, cfg PTYConversationConfig) *PTYConversation { | ||
| if len(cfg.InitialPrompt) > 0 { | ||
| c.outboundQueue <- outboundMessage{parts: cfg.InitialPrompt, errCh: nil} | ||
| } | ||
| if c.cfg.OnSnapshot == nil { | ||
| c.cfg.OnSnapshot = func(ConversationStatus, []ConversationMessage, string) {} | ||
| } | ||
| if c.cfg.ReadyForInitialPrompt == nil { | ||
| c.cfg.ReadyForInitialPrompt = func(string) bool { return true } | ||
| } | ||
| @@ -173,7 +179,9 @@ func (c *PTYConversation) Start(ctx context.Context) { | ||
| } | ||
| c.lock.Unlock() | ||
| c.cfg.OnSnapshot(status, messages, screen) | ||
| c.emitter.EmitStatus(status) | ||
| c.emitter.EmitMessages(messages) | ||
| c.emitter.EmitScreen(screen) | ||
| return nil | ||
| }, "snapshot") | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.