generative-ai-js
generative-ai-js copied to clipboard
feat: add request signal configuration
Fixes #48
Adding new
signaloptions
- [x] Tests pass
- [x] Appropriate changes to documentation are included in the PR
🦋 Changeset detected
Latest commit: 904795ee86436348cccf34d9ca8c761500135771
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 1 package
| Name | Type |
|---|---|
| @google/generative-ai | Patch |
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
Could you please take a look at this PR, @hsubox76? Thanks in advance!
Sorry I haven't gotten back to this. One question: what's the expected behavior if both timeout and signal are specified? It seems like it might be unexpected behavior as is. We should probably log a warning?
Also heads-up, there is a plan to change timeout to timeoutMillis or timeoutMilliseconds so we might end up doing that all in one PR.
There is a real-world library(axios) that supports both options simultaneously:
import axios from 'axios';
axios
.get('https://httpbin.org/delay/2', {
// catch AxiosError: timeout of 1000ms exceeded
timeout: 1000,
// catch CanceledError: canceled
signal: AbortSignal.timeout(1000),
})
.then((response) => console.log('then', response.data))
.catch((reason) => console.error('catch', reason));
it behaves like Promise.race
I thought that was actually going to be pretty complex but it looks like we can just collect all the signals the user wants to use (up to 2 in this case) and wrap them with AbortSignal.any(): https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal#aborting_a_fetch_with_timeout_or_explicit_abort
An additional wrinkle is that we don't want an abort signal to be set at the model level, you want to set it on a per-request level, otherwise it cancels all requests. We're working on another PR to do this.
Closing this PR - but we're working on another way of implementing this funcitonality.