-
Notifications
You must be signed in to change notification settings - Fork 49
EventStreams: Customisable Terminating Byte Sequence #115
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
EventStreams: Customisable Terminating Byte Sequence #115
Conversation
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.
Just a courtesy note to acknowledge this PR—thanks for getting stuck in!
I’m out sick so might not get to this immediately. A quick glance: you’ll need to not remove the existing public initialiser (note that adding a parameter to a public method is technically API breaking because you can refer to functions by their signature.
Thanks @paulhdk, the general shape of this is correct, let's just polish it up a bit together.
|
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.
See comment above
Thank you both for all your help along the way!
Ohh, I wasn't aware. But hadn't we discussed as an initial requirement that this PR shouldn't change the existing API? I thought about providing the changes of this PR in separate functions but I wasn't sure how to avoid duplicate code in that case. Is there an elegant way to preserve the existing API with this PR's changes?
Not sure if I'm missing something, but I'm not seeing your comment in the code? I just see "See comment above" but is not attached to any line of code.
Hm. What about |
The usual steps we take when replacing an API while preserving it in a deprecated form:
We can usually manage to have the deprecated variant call into the new variant. In this case, I believe we should be able to make the deprecated variant call the new variant with a
Apologies, I got distracted and forgot to add the comment 🤦♂️ Let me do that now.
Yeah I expect us to need to invert the value if we use Since it's a closure with a boolean result, maybe it could be called |
Sources/OpenAPIRuntime/EventStreams/ServerSentEventsDecoding.swift
Outdated
Show resolved
Hide resolved
Sources/OpenAPIRuntime/EventStreams/ServerSentEventsDecoding.swift
Outdated
Show resolved
Hide resolved
Sources/OpenAPIRuntime/EventStreams/ServerSentEventsDecoding.swift
Outdated
Show resolved
Hide resolved
9957e84
to
1487eda
Compare
@czechboy0 @simonjbeaumont just giving this a polite bump 🙂 I believe I’ve addressed all of your feedback so far. I just need clarification on one point above, @czechboy0. Let me know if there’s anything missing. |
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.
Thanks for the ping, @paulhdk - I took a more detailed look and suggested a few more changes, mainly around the details of the sequence's state machine.
Sources/OpenAPIRuntime/EventStreams/ServerSentEventsDecoding.swift
Outdated
Show resolved
Hide resolved
Sources/OpenAPIRuntime/EventStreams/ServerSentEventsDecoding.swift
Outdated
Show resolved
Hide resolved
Sources/OpenAPIRuntime/EventStreams/ServerSentEventsDecoding.swift
Outdated
Show resolved
Hide resolved
Sources/OpenAPIRuntime/EventStreams/ServerSentEventsDecoding.swift
Outdated
Show resolved
Hide resolved
Sources/OpenAPIRuntime/EventStreams/ServerSentEventsDecoding.swift
Outdated
Show resolved
Hide resolved
Sources/OpenAPIRuntime/EventStreams/ServerSentEventsDecoding.swift
Outdated
Show resolved
Hide resolved
Sources/OpenAPIRuntime/EventStreams/ServerSentEventsDecoding.swift
Outdated
Show resolved
Hide resolved
Tests/OpenAPIRuntimeTests/EventStreams/Test_ServerSentEventsDecoding.swift
Outdated
Show resolved
Hide resolved
a2b9089
to
f14b5e8
Compare
Thanks for the review, @czechboy0! I’ve addressed everything and ran into some questions. See above. |
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.
Two more minor comments, otherwise lgtm
Sources/OpenAPIRuntime/EventStreams/ServerSentEventsDecoding.swift
Outdated
Show resolved
Hide resolved
Sources/OpenAPIRuntime/EventStreams/ServerSentEventsDecoding.swift
Outdated
Show resolved
Hide resolved
Just a courtesy note to all authors of open PRs. We have just cut over to GitHub Actions for CI so you will see different checks coming past. There is also a note in the |
Use `predicate` when referring to `while` internally, use `while` in the public API Invert the value of the return closure.
Default initialise with `{ _ in true }`
…EventsDecoding.swift Co-authored-by: Honza Dvorsky <[email protected]>
`init(upstream:)` as deprecated
…ventsDecoding.swift Co-authored-by: Honza Dvorsky <[email protected]>
…ventsDecoding.swift Co-authored-by: Honza Dvorsky <[email protected]>
…ventsDecoding.swift Co-authored-by: Honza Dvorsky <[email protected]>
…ams/ServerSentEventsDecoding.swift Co-authored-by: Honza Dvorsky <[email protected]>
…uence` initialiser
…ling `\n` Co-authored-by: Honza Dvorsky <[email protected]>
…it(upstream:)` as deprecated
…ventsDecoding.swift Co-authored-by: Honza Dvorsky <[email protected]>
…izationSequence` iterator
a770b5f
to
0d4345e
Compare
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.
Okay, added a few more nits, but this is now ready for CI - kicking off. Great work!
Sources/OpenAPIRuntime/EventStreams/ServerSentEventsDecoding.swift
Outdated
Show resolved
Hide resolved
Sources/OpenAPIRuntime/EventStreams/ServerSentEventsDecoding.swift
Outdated
Show resolved
Hide resolved
…ventsDecoding.swift Co-authored-by: Honza Dvorsky <[email protected]>
Co-authored-by: Honza Dvorsky <[email protected]>
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.
Looks great, thanks!
You'll still need to rerun the formatter - see the updated CONTRIBUTING.md on how to use the |
PTAL. Ran the formatter locally. All checks should pass now! |
Thanks again for your contribution and for your patience as we've worked through all the details 👏 🙏 |
Congratulations @paulhdk; exciting to see this move into SpeziLLM and the work you have been doing there! 🚀 Thank you @czechboy0 & @simonjbeaumont for the support, reviews, and input! |
Thank you @czechboy0 and @simonjbeaumont for your help! It was fun working this one out and I learnt lots! |
Glad to hear it! If you're ever looking to contribute more, browse through https://github.com./apple/swift-openapi-generator/issues and comment on anything that catches your eye, and we can work together again 🙂 |
### Motivation See: apple/swift-openapi-runtime#115 ### Modifications This PR references the changes introduced by TODO in the documentation. ### Result Users planning to generate code for OpenAPI specs with custom terminating byte sequences will hopefully find it easier to make the necessary changes in their code. Please let me know if there are other places where these changes could be mentioned. ### Test Plan . --------- Co-authored-by: Honza Dvorsky <[email protected]>
Motivation
As discussed in apple/swift-openapi-generator#622, some APIs, e.g., ChatGPT or Claude, may return a non-JSON byte sequence to terminate a stream of events.
If not handled with a workaround (see below)such non-JSON terminating byte sequences cause a decoding error.
Modifications
This PR adds the ability to customise the terminating byte sequence by providing a closure to
asDecodedServerSentEvents()
as well asasDecodedServerSentEventsWithJSONData()
that can match incoming data for the terminating byte sequence before it is decoded into JSON, for instance.Result
Instead of having to decode and re-encode incoming events to filter out the terminating byte sequence - as seen in apple/swift-openapi-generator#622 (comment) - terminating byte sequences can now be cleanly caught by either providing a closure or providing the terminating byte sequence directly when calling
asDecodedServerSentEvents()
andasDecodedServerSentEventsWithJSONData()
.Test Plan
This PR includes unit tests that test the new function parameters as part of the existing tests for
asDecodedServerSentEvents()
as well asasDecodedServerSentEventsWithJSONData()
.