-
Notifications
You must be signed in to change notification settings - Fork 91
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
[Go] Refactored Stream()
API to be more ergonomic.
#766
Open
apascal07
wants to merge
3
commits into
main
Choose a base branch
from
ap-go-stream
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
This file contains 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 |
---|---|---|
|
@@ -695,48 +695,80 @@ func (f *Flow[In, Out, Stream]) run(ctx context.Context, input In, cb func(conte | |
return finishedOpResponse(state.Operation) | ||
} | ||
|
||
// StreamFlowValue is either a streamed value or a final output of a flow. | ||
type StreamFlowValue[Out, Stream any] struct { | ||
Done bool | ||
Output Out // valid if Done is true | ||
Stream Stream // valid if Done is false | ||
// FlowIterator defines the interface for iterating over flow results. | ||
type FlowIterator[Out, Stream any] interface { | ||
// Next returns the next streamed value and a boolean indicating whether the flow has completed. | ||
Next() (Stream, bool) | ||
// FinalOutput returns the final output of the flow if it has completed. | ||
FinalOutput() (Out, error) | ||
} | ||
|
||
// Stream runs the flow on input and delivers both the streamed values and the final output. | ||
// It returns a function whose argument function (the "yield function") will be repeatedly | ||
// called with the results. | ||
// | ||
// If the yield function is passed a non-nil error, the flow has failed with that | ||
// error; the yield function will not be called again. An error is also passed if | ||
// the flow fails to complete (that is, it has an interrupt). | ||
// Genkit Go does not yet support interrupts. | ||
// | ||
// If the yield function's [StreamFlowValue] argument has Done == true, the value's | ||
// Output field contains the final output; the yield function will not be called | ||
// again. | ||
// | ||
// Otherwise the Stream field of the passed [StreamFlowValue] holds a streamed result. | ||
func (f *Flow[In, Out, Stream]) Stream(ctx context.Context, input In, opts ...FlowRunOption) func(func(*StreamFlowValue[Out, Stream], error) bool) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I actually wrote this with range-over-func in mind. Its return type is exactly an |
||
return func(yield func(*StreamFlowValue[Out, Stream], error) bool) { | ||
// flowIterator implements the FlowIterator interface. | ||
type flowIterator[Out, Stream any] struct { | ||
done bool // true if the flow has completed | ||
output Out // the final output of the flow | ||
err error // the error that occurred, if any | ||
streamCh chan Stream // channel to receive streamed values | ||
doneCh chan struct{} // channel to indicate that the flow has completed | ||
} | ||
|
||
// Next returns the next streamed value and a boolean indicating whether the flow has completed. | ||
// If the flow has completed, the returned Stream pointer will be nil and the boolean will be true. | ||
func (fi *flowIterator[Out, Stream]) Next() (*Stream, bool) { | ||
select { | ||
case stream := <-fi.streamCh: | ||
return &stream, false | ||
case <-fi.doneCh: | ||
return nil, true | ||
} | ||
} | ||
|
||
// FinalOutput returns the final output of the flow if it has completed. | ||
// If the flow has not completed, it returns an error. | ||
// If the flow completed with an error, it returns that error. | ||
func (fi *flowIterator[Out, Stream]) FinalOutput() (*Out, error) { | ||
if !fi.done { | ||
return nil, errors.New("flow has not completed") | ||
} | ||
if fi.err != nil { | ||
return nil, fi.err | ||
} | ||
return &fi.output, nil | ||
} | ||
|
||
// Stream returns a FlowIterator for the flow. | ||
func (f *Flow[In, Out, Stream]) Stream(ctx context.Context, input In, opts ...FlowRunOption) FlowIterator[*Out, *Stream] { | ||
fi := &flowIterator[Out, Stream]{ | ||
done: false, | ||
streamCh: make(chan Stream), | ||
doneCh: make(chan struct{}), | ||
} | ||
|
||
go func() { | ||
cb := func(ctx context.Context, s Stream) error { | ||
if ctx.Err() != nil { | ||
fi.err = ctx.Err() | ||
return ctx.Err() | ||
} | ||
if !yield(&StreamFlowValue[Out, Stream]{Stream: s}, nil) { | ||
return errStop | ||
select { | ||
case fi.streamCh <- s: | ||
return nil | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
} | ||
return nil | ||
} | ||
output, err := f.run(ctx, input, cb, opts...) | ||
if err != nil { | ||
yield(nil, err) | ||
fi.err = err | ||
} else { | ||
yield(&StreamFlowValue[Out, Stream]{Done: true, Output: output}, nil) | ||
fi.output = output | ||
} | ||
} | ||
} | ||
fi.done = true | ||
close(fi.doneCh) | ||
}() | ||
|
||
var errStop = errors.New("stop") | ||
return fi | ||
} | ||
|
||
func finishedOpResponse[O any](op *operation[O]) (O, error) { | ||
if !op.Done { | ||
|
This file contains 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 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 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
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.
What does it do if the flow hasn't completed?