-
Notifications
You must be signed in to change notification settings - Fork 991
feat(ai): Add support for AbortSignal
#8890
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
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: 3a63e57 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
496add1 to
f9950ce
Compare
Changeset File Check ✅
|
Size Report 1Affected Products
Test Logs |
Size Analysis Report 1Affected Products
Test Logs |
b9a363a to
3e0cc6b
Compare
b3e7dd8 to
c616128
Compare
DellaBitta
left a comment
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 good, but won't approve until council review.
| If provided, calling `abort()` on the corresponding `AbortController` will attempt to cancel the underlying HTTP request. An `AbortError` will be thrown if cancellation is successful. | ||
| Note that this will not cancel the request in the backend, so billing will still be applied despite cancellation. |
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.
👍
hsubox76
left a comment
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.
Approved, pending merge of #9011 where you'll have to update the changeset and regenerate docs and the api.md.
|
/gemini review |
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.
Code Review
This pull request introduces support for AbortSignal to allow cancellation of AI requests, which is a valuable feature. The implementation correctly updates GenerativeModel, ImagenModel, and ChatSession and includes a comprehensive set of tests for the new cancellation logic. The API and documentation changes are also well-executed.
However, I've identified a few critical issues. The new singleRequestOptions are not implemented for TemplateGenerativeModel and TemplateImagenModel, with TODO comments left in the code. I also found a potential memory leak in the request handling logic and some issues with test structure, such as duplication and improper nesting. I've provided specific comments with suggestions to address these points. Once these are resolved, the pull request will be in excellent shape.
|
Re-requesting review since changes were made. |
hsubox76
left a comment
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.
Weren't we going to have the iterator throw too if there was an error on that iteration? Does this do that? Did it already do that?
|
/gemini review |
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.
Code Review
This pull request introduces support for AbortSignal across various methods, allowing for cancellation of in-flight requests. The implementation is robust, using AbortSignal.any() to gracefully handle both user-initiated aborts and internal timeouts. The changes are well-tested with new integration and unit tests. I've left a few comments on potential improvements, mainly around ensuring atomicity of history updates in ChatSession to prevent inconsistent states, and a couple of minor cleanup items in generated documentation files. Overall, this is a great addition to the SDK.
common/api-review/ai.api.md
Outdated
| generateContent(templateId: string, templateVariables: object, // anything! | ||
| singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>; |
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.
There appears to be a leftover comment // anything! in the generateContent method signature. This should be removed for clarity in the public API documentation.
| generateContent(templateId: string, templateVariables: object, // anything! | |
| singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>; | |
| generateContent(templateId: string, templateVariables: object, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>; |
| generateContent(templateId: string, templateVariables: object, // anything! | ||
| singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>; |
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.
Similar to the API report file, there's a leftover comment // anything! in the generateContent method signature in this documentation file. It should be removed.
| generateContent(templateId: string, templateVariables: object, // anything! | |
| singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>; | |
| generateContent(templateId: string, templateVariables: object, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>; |
hsubox76
left a comment
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.
LG, thanks for adding the comments and the tests.
|
|
||
| // Simulate a response that is technically valid enough to resolve aggregation, | ||
| // but malformed in a way that causes the history update logic to throw. | ||
| // Passing `null` as a candidate causes `{ ...response.candidates[0].content }` to throw. |
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.
This is a good test and we should keep it, but surfaces that we should probably fix the logic to protect against response.candidates[0].content throwing. Can you add a todo for me? In chat-session.ts line 188.
Add an
AbortSignalto a newSingleRequestOptionsthat can be passed to all methods that make requests to the backend, allowing them to be aborted.Fixes #8859
API Proposal: go/vinf-abort-request-api (internal)