EventStreams: Customisable Terminating Byte Sequence#115
EventStreams: Customisable Terminating Byte Sequence#115czechboy0 merged 26 commits intoapple:mainfrom
Conversation
simonjbeaumont
left a comment
There was a problem hiding this comment.
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.
|
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 |
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. |
a2b9089 to
f14b5e8
Compare
Thanks for the review, @czechboy0! I’ve addressed everything and ran into some questions. See above. |
czechboy0
left a comment
There was a problem hiding this comment.
Two more minor comments, otherwise lgtm
|
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 <honza@apple.com>
`init(upstream:)` as deprecated
…ventsDecoding.swift Co-authored-by: Honza Dvorsky <honza@apple.com>
…ventsDecoding.swift Co-authored-by: Honza Dvorsky <honza@apple.com>
…ventsDecoding.swift Co-authored-by: Honza Dvorsky <honza@apple.com>
…ams/ServerSentEventsDecoding.swift Co-authored-by: Honza Dvorsky <honza@apple.com>
…uence` initialiser
…ling `\n` Co-authored-by: Honza Dvorsky <honza@apple.com>
…it(upstream:)` as deprecated
…ventsDecoding.swift Co-authored-by: Honza Dvorsky <honza@apple.com>
…izationSequence` iterator
a770b5f to
0d4345e
Compare
czechboy0
left a comment
There was a problem hiding this comment.
Okay, added a few more nits, but this is now ready for CI - kicking off. Great work!
…ventsDecoding.swift Co-authored-by: Honza Dvorsky <honza@apple.com>
Co-authored-by: Honza Dvorsky <honza@apple.com>
|
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 <honza@apple.com>
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().